Skip to content

Commit 31b5cc5

Browse files
committed
Share context between BB recovery, lifing, and disassembly
Adds a GetInstructionTextWithContext callback to the architecture class that can be used to pass data from AnalyzeBasicBlocks. This same context is also supplied to LiftFunction and allows for supplying shared function and/or binary view level information across basic block analysis, function lifting, and disassembly text rendering
1 parent 9b7598d commit 31b5cc5

5 files changed

Lines changed: 521 additions & 302 deletions

File tree

architecture.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,26 @@ bool Architecture::GetInstructionTextCallback(
737737
}
738738

739739

740+
bool Architecture::GetInstructionTextWithContextCallback(void* ctxt, const uint8_t* data, uint64_t addr, void* context,
741+
size_t* len, BNInstructionTextToken** result, size_t* count)
742+
{
743+
CallbackRef<Architecture> arch(ctxt);
744+
745+
vector<InstructionTextToken> tokens;
746+
bool ok = arch->GetInstructionTextWithContext(data, addr, context, *len, tokens);
747+
if (!ok)
748+
{
749+
*result = nullptr;
750+
*count = 0;
751+
return false;
752+
}
753+
754+
*count = tokens.size();
755+
*result = InstructionTextToken::CreateInstructionTextTokenList(tokens);
756+
return true;
757+
}
758+
759+
740760
void Architecture::FreeInstructionTextCallback(BNInstructionTextToken* tokens, size_t count)
741761
{
742762
for (size_t i = 0; i < count; i++)
@@ -1274,6 +1294,7 @@ void Architecture::Register(Architecture* arch)
12741294
callbacks.getAssociatedArchitectureByAddress = GetAssociatedArchitectureByAddressCallback;
12751295
callbacks.getInstructionInfo = GetInstructionInfoCallback;
12761296
callbacks.getInstructionText = GetInstructionTextCallback;
1297+
callbacks.getInstructionTextWithContext = GetInstructionTextWithContextCallback;
12771298
callbacks.freeInstructionText = FreeInstructionTextCallback;
12781299
callbacks.getInstructionLowLevelIL = GetInstructionLowLevelILCallback;
12791300
callbacks.analyzeBasicBlocks = AnalyzeBasicBlocksCallback;
@@ -1419,6 +1440,13 @@ bool Architecture::LiftFunction(LowLevelILFunction* function, FunctionLifterCont
14191440
}
14201441

14211442

1443+
bool Architecture::GetInstructionTextWithContext(
1444+
const uint8_t* data, uint64_t addr, void* context, size_t& len, std::vector<InstructionTextToken>& result)
1445+
{
1446+
return GetInstructionText(data, addr, len, result);
1447+
}
1448+
1449+
14221450
void Architecture::FreeArchitectureData(void* data) {}
14231451

14241452

@@ -1977,6 +2005,19 @@ bool CoreArchitecture::GetInstructionText(
19772005
}
19782006

19792007

2008+
bool CoreArchitecture::GetInstructionTextWithContext(
2009+
const uint8_t* data, uint64_t addr, void* context, size_t& len, std::vector<InstructionTextToken>& result)
2010+
{
2011+
BNInstructionTextToken* tokens = nullptr;
2012+
size_t count = 0;
2013+
if (!BNGetInstructionTextWithContext(m_object, data, addr, context, &len, &tokens, &count))
2014+
return false;
2015+
2016+
result = InstructionTextToken::ConvertAndFreeInstructionTextTokenList(tokens, count);
2017+
return true;
2018+
}
2019+
2020+
19802021
bool CoreArchitecture::GetInstructionLowLevelIL(const uint8_t* data, uint64_t addr, size_t& len, LowLevelILFunction& il)
19812022
{
19822023
return BNGetInstructionLowLevelIL(m_object, data, addr, &len, il.GetObject());
@@ -2511,6 +2552,13 @@ bool ArchitectureExtension::GetInstructionText(
25112552
}
25122553

25132554

2555+
bool ArchitectureExtension::GetInstructionTextWithContext(
2556+
const uint8_t* data, uint64_t addr, void* context, size_t& len, vector<InstructionTextToken>& result)
2557+
{
2558+
return m_base->GetInstructionTextWithContext(data, addr, context, len, result);
2559+
}
2560+
2561+
25142562
bool ArchitectureExtension::GetInstructionLowLevelIL(
25152563
const uint8_t* data, uint64_t addr, size_t& len, LowLevelILFunction& il)
25162564
{

binaryninjaapi.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9455,6 +9455,8 @@ namespace BinaryNinja {
94559455
void* ctxt, const uint8_t* data, uint64_t addr, size_t maxLen, BNInstructionInfo* result);
94569456
static bool GetInstructionTextCallback(void* ctxt, const uint8_t* data, uint64_t addr, size_t* len,
94579457
BNInstructionTextToken** result, size_t* count);
9458+
static bool GetInstructionTextWithContextCallback(void* ctxt, const uint8_t* data, uint64_t addr, void* context,
9459+
size_t* len, BNInstructionTextToken** result, size_t* count);
94589460
static void FreeInstructionTextCallback(BNInstructionTextToken* tokens, size_t count);
94599461
static bool GetInstructionLowLevelILCallback(
94609462
void* ctxt, const uint8_t* data, uint64_t addr, size_t* len, BNLowLevelILFunction* il);
@@ -9638,6 +9640,20 @@ namespace BinaryNinja {
96389640
virtual bool GetInstructionText(
96399641
const uint8_t* data, uint64_t addr, size_t& len, std::vector<InstructionTextToken>& result) = 0;
96409642

9643+
/*! Retrieves a list of InstructionTextTokens while supplying contextual information
9644+
9645+
\note Architecture subclasses can implement this method to provide contextual information from AnalyzeBasicBlocks
9646+
9647+
\param[in] data pointer to the instruction data to retrieve text for
9648+
\param[in] addr address of the instruction data to retrieve text for
9649+
\param[in] context context to use when retrieving instruction text
9650+
\param[out] len will be written to with the length of the instruction data which was translated
9651+
\param[out] result
9652+
\return Whether instruction info was successfully retrieved.
9653+
*/
9654+
virtual bool GetInstructionTextWithContext(const uint8_t* data, uint64_t addr, void* context, size_t& len,
9655+
std::vector<InstructionTextToken>& result);
9656+
96419657
/*! Translates an instruction at addr and appends it onto the LowLevelILFunction& il.
96429658

96439659
\note Architecture subclasses should implement this method.
@@ -10061,6 +10077,8 @@ namespace BinaryNinja {
1006110077
const uint8_t* data, uint64_t addr, size_t maxLen, InstructionInfo& result) override;
1006210078
virtual bool GetInstructionText(
1006310079
const uint8_t* data, uint64_t addr, size_t& len, std::vector<InstructionTextToken>& result) override;
10080+
virtual bool GetInstructionTextWithContext(const uint8_t* data, uint64_t addr, void* context, size_t& len,
10081+
std::vector<InstructionTextToken>& result) override;
1006410082
virtual bool GetInstructionLowLevelIL(
1006510083
const uint8_t* data, uint64_t addr, size_t& len, LowLevelILFunction& il) override;
1006610084
virtual void AnalyzeBasicBlocks(Function* function, BasicBlockAnalysisContext& context) override;
@@ -10149,6 +10167,8 @@ namespace BinaryNinja {
1014910167
const uint8_t* data, uint64_t addr, size_t maxLen, InstructionInfo& result) override;
1015010168
virtual bool GetInstructionText(
1015110169
const uint8_t* data, uint64_t addr, size_t& len, std::vector<InstructionTextToken>& result) override;
10170+
virtual bool GetInstructionTextWithContext(const uint8_t* data, uint64_t addr, void* context, size_t& len,
10171+
std::vector<InstructionTextToken>& result) override;
1015210172
virtual bool GetInstructionLowLevelIL(
1015310173
const uint8_t* data, uint64_t addr, size_t& len, LowLevelILFunction& il) override;
1015410174
virtual std::string GetRegisterName(uint32_t reg) override;

binaryninjacore.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2098,6 +2098,8 @@ extern "C"
20982098
void* ctxt, const uint8_t* data, uint64_t addr, size_t maxLen, BNInstructionInfo* result);
20992099
bool (*getInstructionText)(void* ctxt, const uint8_t* data, uint64_t addr, size_t* len,
21002100
BNInstructionTextToken** result, size_t* count);
2101+
bool (*getInstructionTextWithContext)(void* ctxt, const uint8_t* data, uint64_t addr, void* context,
2102+
size_t* len, BNInstructionTextToken** result, size_t* count);
21012103
void (*freeInstructionText)(BNInstructionTextToken* tokens, size_t count);
21022104
bool (*getInstructionLowLevelIL)(
21032105
void* ctxt, const uint8_t* data, uint64_t addr, size_t* len, BNLowLevelILFunction* il);
@@ -4933,6 +4935,8 @@ extern "C"
49334935
BNArchitecture* arch, const uint8_t* data, uint64_t addr, size_t maxLen, BNInstructionInfo* result);
49344936
BINARYNINJACOREAPI bool BNGetInstructionText(BNArchitecture* arch, const uint8_t* data, uint64_t addr, size_t* len,
49354937
BNInstructionTextToken** result, size_t* count);
4938+
BINARYNINJACOREAPI bool BNGetInstructionTextWithContext(BNArchitecture* arch, const uint8_t* data, uint64_t addr, void* context,
4939+
size_t* len, BNInstructionTextToken** result, size_t* count);
49364940
BINARYNINJACOREAPI bool BNGetInstructionLowLevelIL(
49374941
BNArchitecture* arch, const uint8_t* data, uint64_t addr, size_t* len, BNLowLevelILFunction* il);
49384942
BINARYNINJACOREAPI void BNFreeInstructionText(BNInstructionTextToken* tokens, size_t count);

0 commit comments

Comments
 (0)