Skip to content

Commit 0996601

Browse files
committed
Support MLIL expr mappings in C++
1 parent 2060680 commit 0996601

File tree

6 files changed

+273
-33
lines changed

6 files changed

+273
-33
lines changed

binaryninjaapi.h

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11106,11 +11106,20 @@ namespace BinaryNinja {
1110611106
*/
1110711107
void SetLowLevelILFunction(Ref<LowLevelILFunction> lowLevelIL);
1110811108

11109-
/*! Set the new Medium Level IL for the current analysis context
11109+
/*! Set the new Medium Level IL for the current analysis context.
11110+
11111+
If mapping parameters are left as default (empty), then they will be automatically
11112+
computed for you based on previous calls to AddExpr() and AddInstruction()
1111011113

1111111114
\param mediumLevelIL the new Medium Level IL
11115+
\param llilSsaToMlilInstrMap New mappings from LLIL SSA -> MLIL instruction indices
11116+
\param llilSsaToMlilExprMap New mappings from LLIL SSA -> MLIL expression indices
1111211117
*/
11113-
void SetMediumLevelILFunction(Ref<MediumLevelILFunction> mediumLevelIL);
11118+
void SetMediumLevelILFunction(
11119+
Ref<MediumLevelILFunction> mediumLevelIL,
11120+
std::unordered_map<size_t /* llil ssa */, size_t /* mlil */> llilSsaToMlilInstrMap = {},
11121+
std::vector<BNExprMapInfo> llilSsaToMlilExprMap = {}
11122+
);
1111411123

1111511124
/*! Set the new High Level IL for the current analysis context
1111611125

@@ -13071,21 +13080,16 @@ namespace BinaryNinja {
1307113080
uint32_t sourceOperand;
1307213081
bool valid;
1307313082

13074-
ILSourceLocation() : valid(false) {}
13083+
bool ilBased;
13084+
bool ilDirect;
13085+
size_t ilExprIndex;
1307513086

13076-
ILSourceLocation(uint64_t addr, uint32_t operand) : address(addr), sourceOperand(operand), valid(true) {}
13077-
13078-
ILSourceLocation(const BNLowLevelILInstruction& instr) :
13079-
address(instr.address), sourceOperand(instr.sourceOperand), valid(true)
13080-
{}
13087+
ILSourceLocation() : valid(false), ilBased(false) {}
1308113088

13082-
ILSourceLocation(const BNMediumLevelILInstruction& instr) :
13083-
address(instr.address), sourceOperand(instr.sourceOperand), valid(true)
13084-
{}
13085-
13086-
ILSourceLocation(const BNHighLevelILInstruction& instr) :
13087-
address(instr.address), sourceOperand(instr.sourceOperand), valid(true)
13088-
{}
13089+
ILSourceLocation(uint64_t addr, uint32_t operand) : address(addr), sourceOperand(operand), valid(true), ilBased(false) {}
13090+
ILSourceLocation(const struct LowLevelILInstruction& instr);
13091+
ILSourceLocation(const struct MediumLevelILInstruction& instr);
13092+
ILSourceLocation(const struct HighLevelILInstruction& instr);
1308913093
};
1309013094

1309113095
struct LowLevelILInstruction;
@@ -14535,6 +14539,22 @@ namespace BinaryNinja {
1453514539
public CoreRefCountObject<BNMediumLevelILFunction, BNNewMediumLevelILFunctionReference,
1453614540
BNFreeMediumLevelILFunction>
1453714541
{
14542+
struct TranslationData
14543+
{
14544+
MediumLevelILFunction* copyingFunction = nullptr;
14545+
std::unordered_map<size_t /* old function expr index */, std::vector<std::tuple<size_t /* new function expr index */, bool /* direct */>>> mlilToMlilExprMap;
14546+
std::unordered_map<size_t /* old function instr index */, std::vector<std::tuple<size_t /* new function instr index */, bool /* direct */>>> mlilToMlilInstrMap;
14547+
// todo maybe: llil ssa -> mlil mappings
14548+
};
14549+
std::unique_ptr<TranslationData> m_translationData;
14550+
14551+
void RecordMLILToMLILExprMap(size_t newExprIndex, const ILSourceLocation& location);
14552+
void RecordMLILToMLILInstrMap(size_t newInstrIndex, const ILSourceLocation& location);
14553+
std::unordered_map<size_t /* llil ssa */, size_t /* mlil */> GetLLILSSAToMLILInstrMap(bool fromTranslation);
14554+
std::vector<BNExprMapInfo> GetLLILSSAToMLILExprMap(bool fromTranslation);
14555+
14556+
friend class AnalysisContext;
14557+
1453814558
public:
1453914559
MediumLevelILFunction(Architecture* arch, Function* func = nullptr, LowLevelILFunction* lowLevelIL = nullptr);
1454014560
MediumLevelILFunction(BNMediumLevelILFunction* func);
@@ -14774,7 +14794,7 @@ namespace BinaryNinja {
1477414794
const ILSourceLocation& loc = ILSourceLocation());
1477514795
void MarkLabel(BNMediumLevelILLabel& label);
1477614796

14777-
ExprId AddInstruction(ExprId expr);
14797+
ExprId AddInstruction(ExprId expr, const ILSourceLocation& loc = ILSourceLocation());
1477814798

1477914799
std::vector<uint64_t> GetOperandList(ExprId i, size_t listOperand);
1478014800
ExprId AddLabelMap(const std::map<uint64_t, BNMediumLevelILLabel*>& labels);

examples/workflows/unflatten/library.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ void RewriteAction(Ref<AnalysisContext> context, bool doIt)
235235
{
236236
node->SetHighlight(GetHighlightColor(RedHighlightColor));
237237
}
238-
else if (std::find_if(path.begin(), path.end(), [node](Ref<BasicBlock> b) { return b->GetStart() == node->GetBasicBlock()->GetStart(); }) != path.end())
238+
else if (std::find_if(path.begin(), path.end(), [node](const Ref<BasicBlock>& b) { return b->GetStart() == node->GetBasicBlock()->GetStart(); }) != path.end())
239239
{
240240
node->SetHighlight(GetHighlightColor(GreenHighlightColor));
241241
}
@@ -286,7 +286,7 @@ void RewriteAction(Ref<AnalysisContext> context, bool doIt)
286286
// Copy instruction as-is
287287
auto copyBlockInstr = oldMLIL->GetInstruction(copyBlockInstrIndex);
288288
newMLIL->SetCurrentAddress(copyBlock->GetArchitecture(), copyBlockInstr.address);
289-
newMLIL->AddInstruction(copyBlockInstr.CopyTo(newMLIL));
289+
newMLIL->AddInstruction(copyBlockInstr.CopyTo(newMLIL), copyBlockInstr);
290290
}
291291
}
292292
continue;
@@ -295,7 +295,7 @@ void RewriteAction(Ref<AnalysisContext> context, bool doIt)
295295

296296
// Otherwise, copy the instruction as-is
297297
newMLIL->SetCurrentAddress(block->GetArchitecture(), oldInstr.address);
298-
newMLIL->AddInstruction(oldInstr.CopyTo(newMLIL));
298+
newMLIL->AddInstruction(oldInstr.CopyTo(newMLIL), oldInstr);
299299
}
300300
}
301301

@@ -350,7 +350,7 @@ void RewriteAction(Ref<AnalysisContext> context, bool doIt)
350350
{
351351
size_t destValue = oldInstr.GetDestExpr<MLIL_JUMP_TO>().GetValue().value;
352352
auto targets = oldInstr.GetTargets<MLIL_JUMP_TO>();
353-
if (std::find_if(targets.begin(), targets.end(), [&](std::pair<size_t, size_t> target) {
353+
if (std::find_if(targets.begin(), targets.end(), [&](const std::pair<size_t, size_t>& target) {
354354
return target.first == destValue;
355355
}) != targets.end()) {
356356
auto oldTargetIndex = targets[destValue];
@@ -359,15 +359,15 @@ void RewriteAction(Ref<AnalysisContext> context, bool doIt)
359359
blockLabels[oldTargetIndex] = MediumLevelILLabel{};
360360
}
361361
MediumLevelILLabel* targetLabel = &blockLabels[oldTargetIndex];
362-
newMLIL->AddInstruction(newMLIL->Goto(*targetLabel, oldInstr));
362+
newMLIL->AddInstruction(newMLIL->Goto(*targetLabel, oldInstr), oldInstr);
363363
continue;
364364
}
365365
}
366366
}
367367

368368
// Otherwise, copy the instruction as-is
369369
newMLIL->SetCurrentAddress(block->GetArchitecture(), oldInstr.address);
370-
newMLIL->AddInstruction(oldInstr.CopyTo(newMLIL));
370+
newMLIL->AddInstruction(oldInstr.CopyTo(newMLIL), oldInstr);
371371
}
372372
}
373373

highlevelil.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ using namespace BinaryNinja;
2626
using namespace std;
2727

2828

29+
ILSourceLocation::ILSourceLocation(const struct HighLevelILInstruction& instr):
30+
address(instr.address), sourceOperand(instr.sourceOperand), valid(true),
31+
ilBased(true), ilDirect(true), ilExprIndex(instr.exprIndex)
32+
{}
33+
34+
2935
HighLevelILFunction::HighLevelILFunction(Architecture* arch, Function* func)
3036
{
3137
m_object = BNCreateHighLevelILFunction(arch->GetObject(), func ? func->GetObject() : nullptr);

lowlevelil.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ using namespace BinaryNinja;
2525
using namespace std;
2626

2727

28+
ILSourceLocation::ILSourceLocation(const struct LowLevelILInstruction& instr):
29+
address(instr.address), sourceOperand(instr.sourceOperand), valid(true),
30+
ilBased(true), ilDirect(true), ilExprIndex(instr.exprIndex)
31+
{}
32+
33+
2834
LowLevelILLabel::LowLevelILLabel()
2935
{
3036
BNLowLevelILInitLabel(this);

0 commit comments

Comments
 (0)