Skip to content

Commit bcdc0d9

Browse files
committed
uidf refactor
1 parent 0718115 commit bcdc0d9

26 files changed

+667
-103
lines changed

binaryninjaapi.h

+36-4
Original file line numberDiff line numberDiff line change
@@ -10632,7 +10632,7 @@ namespace BinaryNinja {
1063210632
size_t count;
1063310633

1063410634
static PossibleValueSet FromAPIObject(BNPossibleValueSet& value);
10635-
BNPossibleValueSet ToAPIObject();
10635+
BNPossibleValueSet ToAPIObject() const;
1063610636
static void FreeAPIObject(BNPossibleValueSet* value);
1063710637
};
1063810638

@@ -11189,11 +11189,14 @@ namespace BinaryNinja {
1118911189

1119011190
Ref<FlowGraph> GetUnresolvedStackAdjustmentGraph();
1119111191

11192-
void SetUserVariableValue(const Variable& var, uint64_t defAddr, PossibleValueSet& value);
11193-
void ClearUserVariableValue(const Variable& var, uint64_t defAddr);
11194-
std::map<Variable, std::map<ArchAndAddr, PossibleValueSet>> GetAllUserVariableValues();
11192+
void SetUserVariableValue(const Variable& var, const ArchAndAddr& defAddr, PossibleValueSet& value, bool after = true);
11193+
void ClearUserVariableValue(const Variable& var, const ArchAndAddr& defAddr, bool after = true);
11194+
std::map<Variable, std::map<std::pair<ArchAndAddr, bool>, PossibleValueSet>> GetAllUserVariableValues();
1119511195
void ClearAllUserVariableValues();
1119611196

11197+
void CreateForcedVariableVersion(const Variable& var, const ArchAndAddr& location);
11198+
void ClearForcedVariableVersion(const Variable& var, const ArchAndAddr& location);
11199+
1119711200
void RequestDebugReport(const std::string& name);
1119811201

1119911202
/*! Get the name for a given label ID
@@ -11803,6 +11806,9 @@ namespace BinaryNinja {
1180311806
std::vector<SSARegisterStack> GetSSARegisterStacks();
1180411807
std::vector<SSAFlag> GetSSAFlags();
1180511808

11809+
size_t CachePossibleValueSet(const PossibleValueSet& pvs);
11810+
PossibleValueSet GetCachedPossibleValueSet(size_t idx);
11811+
1180611812
ExprId AddExpr(BNLowLevelILOperation operation, size_t size, uint32_t flags, ExprId a = 0, ExprId b = 0,
1180711813
ExprId c = 0, ExprId d = 0);
1180811814
ExprId AddExprWithLocation(BNLowLevelILOperation operation, uint64_t addr, uint32_t sourceOperand, size_t size,
@@ -11891,6 +11897,12 @@ namespace BinaryNinja {
1189111897
ExprId SetFlag(uint32_t flag, ExprId val, const ILSourceLocation& loc = ILSourceLocation());
1189211898
ExprId SetFlagSSA(const SSAFlag& flag, ExprId val, const ILSourceLocation& loc = ILSourceLocation());
1189311899

11900+
ExprId ForceVer(size_t size, uint32_t reg, const ILSourceLocation& loc = ILSourceLocation());
11901+
ExprId ForceVerSSA(size_t size, SSARegister dst, SSARegister src, const ILSourceLocation& loc = ILSourceLocation());
11902+
11903+
ExprId Assert(size_t size, uint32_t reg, const PossibleValueSet& pvs, const ILSourceLocation& loc = ILSourceLocation());
11904+
ExprId AssertSSA(size_t size, SSARegister reg, const PossibleValueSet& pvs, const ILSourceLocation& loc = ILSourceLocation());
11905+
1189411906
/*! Reads \c size bytes from the expression \c addr
1189511907

1189611908
\param size Number of bytes to read
@@ -13187,6 +13199,9 @@ namespace BinaryNinja {
1318713199
*/
1318813200
BNMediumLevelILLabel* GetLabelForSourceInstruction(size_t i);
1318913201

13202+
size_t CachePossibleValueSet(const PossibleValueSet& pvs);
13203+
PossibleValueSet GetCachedPossibleValueSet(size_t idx);
13204+
1319013205
ExprId AddExpr(BNMediumLevelILOperation operation, size_t size, ExprId a = 0, ExprId b = 0, ExprId c = 0,
1319113206
ExprId d = 0, ExprId e = 0);
1319213207
ExprId AddExprWithLocation(BNMediumLevelILOperation operation, uint64_t addr, uint32_t sourceOperand,
@@ -13210,6 +13225,13 @@ namespace BinaryNinja {
1321013225
const ILSourceLocation& loc = ILSourceLocation());
1321113226
ExprId SetVarAliasedField(size_t size, const Variable& dest, size_t newMemVersion, size_t prevMemVersion,
1321213227
uint64_t offset, ExprId src, const ILSourceLocation& loc = ILSourceLocation());
13228+
13229+
ExprId ForceVer(size_t size, const Variable& dest, const Variable& src, const ILSourceLocation& loc = ILSourceLocation());
13230+
ExprId ForceVerSSA(size_t size, const SSAVariable& dest, const SSAVariable& src, const ILSourceLocation& loc = ILSourceLocation());
13231+
13232+
ExprId Assert(size_t size, const Variable& src, const PossibleValueSet& pvs, const ILSourceLocation& loc = ILSourceLocation());
13233+
ExprId AssertSSA(size_t size, const SSAVariable& src, const PossibleValueSet& pvs, const ILSourceLocation& loc = ILSourceLocation());
13234+
1321313235
ExprId Load(size_t size, ExprId src, const ILSourceLocation& loc = ILSourceLocation());
1321413236
ExprId LoadStruct(size_t size, ExprId src, uint64_t offset, const ILSourceLocation& loc = ILSourceLocation());
1321513237
ExprId LoadSSA(size_t size, ExprId src, size_t memVersion, const ILSourceLocation& loc = ILSourceLocation());
@@ -13557,6 +13579,9 @@ namespace BinaryNinja {
1355713579
void SetRootExpr(ExprId expr);
1355813580
void SetRootExpr(const HighLevelILInstruction& expr);
1355913581

13582+
size_t CachePossibleValueSet(const PossibleValueSet& pvs);
13583+
PossibleValueSet GetCachedPossibleValueSet(size_t idx);
13584+
1356013585
ExprId AddExpr(BNHighLevelILOperation operation, size_t size, ExprId a = 0, ExprId b = 0, ExprId c = 0,
1356113586
ExprId d = 0, ExprId e = 0);
1356213587
ExprId AddExprWithLocation(BNHighLevelILOperation operation, uint64_t addr, uint32_t sourceOperand, size_t size,
@@ -13601,6 +13626,13 @@ namespace BinaryNinja {
1360113626
const ILSourceLocation& loc = ILSourceLocation());
1360213627
ExprId AssignUnpackMemSSA(const std::vector<ExprId>& output, size_t destMemVersion, ExprId src,
1360313628
size_t srcMemVersion, const ILSourceLocation& loc = ILSourceLocation());
13629+
13630+
ExprId ForceVer(size_t size, const Variable& dest, const Variable& src, const ILSourceLocation& loc = ILSourceLocation());
13631+
ExprId ForceVerSSA(size_t size, const SSAVariable& dest, const SSAVariable& src, const ILSourceLocation& loc = ILSourceLocation());
13632+
13633+
ExprId Assert(size_t size, const Variable& src, const PossibleValueSet& pvs, const ILSourceLocation& loc = ILSourceLocation());
13634+
ExprId AssertSSA(size_t size, const SSAVariable& src, const PossibleValueSet& pvs, const ILSourceLocation& loc = ILSourceLocation());
13635+
1360413636
ExprId Var(size_t size, const Variable& src, const ILSourceLocation& loc = ILSourceLocation());
1360513637
ExprId VarSSA(size_t size, const SSAVariable& src, const ILSourceLocation& loc = ILSourceLocation());
1360613638
ExprId VarPhi(const SSAVariable& dest, const std::vector<SSAVariable>& sources,

binaryninjacore.h

+27-2
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,8 @@ extern "C"
509509
LLIL_SET_FLAG, // Not valid in SSA form (see LLIL_SET_FLAG_SSA)
510510
LLIL_SET_REG_STACK_REL, // Not valid in SSA form (see LLIL_SET_REG_STACK_REL_SSA)
511511
LLIL_REG_STACK_PUSH, // Not valid in SSA form (expanded)
512+
LLIL_ASSERT,
513+
LLIL_FORCE_VER,
512514
LLIL_LOAD, // Not valid in SSA form (see LLIL_LOAD_SSA)
513515
LLIL_STORE, // Not valid in SSA form (see LLIL_STORE_SSA)
514516
LLIL_PUSH, // Not valid in SSA form (expanded)
@@ -628,6 +630,8 @@ extern "C"
628630
LLIL_REG_STACK_FREE_REL_SSA,
629631
LLIL_REG_STACK_FREE_ABS_SSA,
630632
LLIL_SET_FLAG_SSA,
633+
LLIL_ASSERT_SSA,
634+
LLIL_FORCE_VER_SSA,
631635
LLIL_FLAG_SSA,
632636
LLIL_FLAG_BIT_SSA,
633637
LLIL_CALL_SSA,
@@ -1200,6 +1204,8 @@ extern "C"
12001204
MLIL_SET_VAR, // Not valid in SSA form (see MLIL_SET_VAR_SSA)
12011205
MLIL_SET_VAR_FIELD, // Not valid in SSA form (see MLIL_SET_VAR_FIELD)
12021206
MLIL_SET_VAR_SPLIT, // Not valid in SSA form (see MLIL_SET_VAR_SPLIT_SSA)
1207+
MLIL_ASSERT,
1208+
MLIL_FORCE_VER,
12031209
MLIL_LOAD, // Not valid in SSA form (see MLIL_LOAD_SSA)
12041210
MLIL_LOAD_STRUCT, // Not valid in SSA form (see MLIL_LOAD_STRUCT_SSA)
12051211
MLIL_STORE, // Not valid in SSA form (see MLIL_STORE_SSA)
@@ -1320,6 +1326,8 @@ extern "C"
13201326
MLIL_VAR_ALIASED,
13211327
MLIL_VAR_ALIASED_FIELD,
13221328
MLIL_VAR_SPLIT_SSA,
1329+
MLIL_ASSERT_SSA,
1330+
MLIL_FORCE_VER_SSA,
13231331
MLIL_CALL_SSA,
13241332
MLIL_CALL_UNTYPED_SSA,
13251333
MLIL_SYSCALL_SSA,
@@ -1396,6 +1404,8 @@ extern "C"
13961404
HLIL_VAR_INIT,
13971405
HLIL_ASSIGN,
13981406
HLIL_ASSIGN_UNPACK,
1407+
HLIL_FORCE_VER,
1408+
HLIL_ASSERT,
13991409
HLIL_VAR,
14001410
HLIL_STRUCT_FIELD,
14011411
HLIL_ARRAY_INDEX,
@@ -1497,6 +1507,8 @@ extern "C"
14971507
HLIL_VAR_INIT_SSA,
14981508
HLIL_ASSIGN_MEM_SSA,
14991509
HLIL_ASSIGN_UNPACK_MEM_SSA,
1510+
HLIL_FORCE_VER_SSA,
1511+
HLIL_ASSERT_SSA,
15001512
HLIL_VAR_SSA,
15011513
HLIL_ARRAY_INDEX_SSA,
15021514
HLIL_DEREF_SSA,
@@ -2610,6 +2622,7 @@ extern "C"
26102622
{
26112623
BNVariable var;
26122624
BNArchitectureAndAddress defSite;
2625+
bool after;
26132626
BNPossibleValueSet value;
26142627
} BNUserVariableValue;
26152628

@@ -5393,14 +5406,17 @@ extern "C"
53935406
BINARYNINJACOREAPI BNFlowGraph* BNGetUnresolvedStackAdjustmentGraph(BNFunction* func);
53945407

53955408
BINARYNINJACOREAPI void BNSetUserVariableValue(BNFunction* func, const BNVariable* var,
5396-
const BNArchitectureAndAddress* defSite, const BNPossibleValueSet* value);
5409+
const BNArchitectureAndAddress* defSite, bool after, const BNPossibleValueSet* value);
53975410
BINARYNINJACOREAPI void BNClearUserVariableValue(
5398-
BNFunction* func, const BNVariable* var, const BNArchitectureAndAddress* defSite);
5411+
BNFunction* func, const BNVariable* var, const BNArchitectureAndAddress* defSite, bool after);
53995412
BINARYNINJACOREAPI BNUserVariableValue* BNGetAllUserVariableValues(BNFunction* func, size_t* count);
54005413
BINARYNINJACOREAPI void BNFreeUserVariableValues(BNUserVariableValue* result);
54015414
BINARYNINJACOREAPI bool BNParsePossibleValueSet(BNBinaryView* view, const char* valueText,
54025415
BNRegisterValueType state, BNPossibleValueSet* result, uint64_t here, char** errors);
54035416

5417+
BINARYNINJACOREAPI void BNCreateForcedVariableVersion(BNFunction* func, const BNVariable* var, const BNArchitectureAndAddress* defSite);
5418+
BINARYNINJACOREAPI void BNClearForcedVariableVersion(BNFunction* func, const BNVariable* var, const BNArchitectureAndAddress* defSite);
5419+
54045420
BINARYNINJACOREAPI void BNRequestFunctionDebugReport(BNFunction* func, const char* name);
54055421

54065422
BINARYNINJACOREAPI BNILReferenceSource* BNGetMediumLevelILVariableReferences(
@@ -5747,6 +5763,9 @@ extern "C"
57475763
BNLowLevelILFunction* func, size_t expr, size_t operand, size_t* count);
57485764
BINARYNINJACOREAPI void BNLowLevelILFreeOperandList(uint64_t* operands);
57495765

5766+
BINARYNINJACOREAPI size_t BNCacheLowLevelILPossibleValueSet(BNLowLevelILFunction* func, BNPossibleValueSet* pvs);
5767+
BINARYNINJACOREAPI BNPossibleValueSet BNGetCachedLowLevelILPossibleValueSet(BNLowLevelILFunction* func, size_t idx);
5768+
57505769
BINARYNINJACOREAPI BNLowLevelILInstruction BNGetLowLevelILByIndex(BNLowLevelILFunction* func, size_t i);
57515770
BINARYNINJACOREAPI size_t BNGetLowLevelILIndexForInstruction(BNLowLevelILFunction* func, size_t i);
57525771
BINARYNINJACOREAPI size_t BNGetLowLevelILInstructionForExpr(BNLowLevelILFunction* func, size_t expr);
@@ -5900,6 +5919,9 @@ extern "C"
59005919
BNMediumLevelILFunction* func, size_t expr, size_t operand, size_t* count);
59015920
BINARYNINJACOREAPI void BNMediumLevelILFreeOperandList(uint64_t* operands);
59025921

5922+
BINARYNINJACOREAPI size_t BNCacheMediumLevelILPossibleValueSet(BNMediumLevelILFunction* func, BNPossibleValueSet* pvs);
5923+
BINARYNINJACOREAPI BNPossibleValueSet BNGetCachedMediumLevelILPossibleValueSet(BNMediumLevelILFunction* func, size_t idx);
5924+
59035925
BINARYNINJACOREAPI BNMediumLevelILInstruction BNGetMediumLevelILByIndex(BNMediumLevelILFunction* func, size_t i);
59045926
BINARYNINJACOREAPI size_t BNGetMediumLevelILIndexForInstruction(BNMediumLevelILFunction* func, size_t i);
59055927
BINARYNINJACOREAPI size_t BNGetMediumLevelILInstructionForExpr(BNMediumLevelILFunction* func, size_t expr);
@@ -6058,6 +6080,9 @@ extern "C"
60586080
BNHighLevelILFunction* func, size_t expr, size_t operand, size_t* count);
60596081
BINARYNINJACOREAPI void BNHighLevelILFreeOperandList(uint64_t* operands);
60606082

6083+
BINARYNINJACOREAPI size_t BNCacheHighLevelILPossibleValueSet(BNHighLevelILFunction* func, BNPossibleValueSet* pvs);
6084+
BINARYNINJACOREAPI BNPossibleValueSet BNGetCachedHighLevelILPossibleValueSet(BNHighLevelILFunction* func, size_t idx);
6085+
60616086
BINARYNINJACOREAPI BNHighLevelILInstruction BNGetHighLevelILByIndex(
60626087
BNHighLevelILFunction* func, size_t i, bool asFullAst);
60636088
BINARYNINJACOREAPI size_t BNGetHighLevelILIndexForInstruction(BNHighLevelILFunction* func, size_t i);

function.cpp

+44-62
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ PossibleValueSet PossibleValueSet::FromAPIObject(BNPossibleValueSet& value)
539539
}
540540

541541

542-
BNPossibleValueSet PossibleValueSet::ToAPIObject()
542+
BNPossibleValueSet PossibleValueSet::ToAPIObject() const
543543
{
544544
BNPossibleValueSet result;
545545
result.state = state;
@@ -2601,35 +2601,11 @@ Ref<FlowGraph> Function::GetUnresolvedStackAdjustmentGraph()
26012601
}
26022602

26032603

2604-
void Function::SetUserVariableValue(const Variable& var, uint64_t defAddr, PossibleValueSet& value)
2604+
void Function::SetUserVariableValue(const Variable& var, const ArchAndAddr& defAddr, PossibleValueSet& value, bool after)
26052605
{
2606-
if (var.index != 0)
2607-
{
2608-
Ref<MediumLevelILFunction> mlil = GetMediumLevelIL();
2609-
const set<size_t>& varDefs = mlil->GetVariableDefinitions(var);
2610-
if (varDefs.size() == 0)
2611-
{
2612-
LogError("Could not get definition for Variable");
2613-
return;
2614-
}
2615-
bool found = false;
2616-
for (auto& site : varDefs)
2617-
{
2618-
const MediumLevelILInstruction& instr = mlil->GetInstruction(site);
2619-
if (instr.address == defAddr)
2620-
{
2621-
found = true;
2622-
break;
2623-
}
2624-
}
2625-
if (!found)
2626-
{
2627-
LogError("Could not find definition for variable at given address");
2628-
}
2629-
}
26302606
auto defSite = BNArchitectureAndAddress();
2631-
defSite.arch = GetArchitecture()->m_object;
2632-
defSite.address = defAddr;
2607+
defSite.arch = defAddr.arch->m_object;
2608+
defSite.address = defAddr.address;
26332609

26342610
auto var_data = BNVariable();
26352611
var_data.type = var.type;
@@ -2638,55 +2614,31 @@ void Function::SetUserVariableValue(const Variable& var, uint64_t defAddr, Possi
26382614

26392615
auto valueObj = value.ToAPIObject();
26402616

2641-
BNSetUserVariableValue(m_object, &var_data, &defSite, &valueObj);
2617+
BNSetUserVariableValue(m_object, &var_data, &defSite, after, &valueObj);
26422618

26432619
PossibleValueSet::FreeAPIObject(&valueObj);
26442620
}
26452621

26462622

2647-
void Function::ClearUserVariableValue(const Variable& var, uint64_t defAddr)
2623+
void Function::ClearUserVariableValue(const Variable& var, const ArchAndAddr& defAddr, bool after)
26482624
{
2649-
if (var.index != 0)
2650-
{
2651-
Ref<MediumLevelILFunction> mlil = GetMediumLevelIL();
2652-
const set<size_t>& varDefs = mlil->GetVariableDefinitions(var);
2653-
if (varDefs.size() == 0)
2654-
{
2655-
LogError("Could not get definition for Variable");
2656-
return;
2657-
}
2658-
bool found = false;
2659-
for (auto& site : varDefs)
2660-
{
2661-
const MediumLevelILInstruction& instr = mlil->GetInstruction(site);
2662-
if (instr.address == defAddr)
2663-
{
2664-
found = true;
2665-
break;
2666-
}
2667-
}
2668-
if (!found)
2669-
{
2670-
LogError("Could not find definition for variable at given address");
2671-
}
2672-
}
26732625
auto defSite = BNArchitectureAndAddress();
2674-
defSite.arch = GetArchitecture()->m_object;
2675-
defSite.address = defAddr;
2626+
defSite.arch = defAddr.arch->m_object;
2627+
defSite.address = defAddr.address;
26762628

26772629
auto var_data = BNVariable();
26782630
var_data.type = var.type;
26792631
var_data.index = var.index;
26802632
var_data.storage = var.storage;
26812633

2682-
BNClearUserVariableValue(m_object, &var_data, &defSite);
2634+
BNClearUserVariableValue(m_object, &var_data, &defSite, after);
26832635
}
26842636

26852637

2686-
map<Variable, map<ArchAndAddr, PossibleValueSet>> Function::GetAllUserVariableValues()
2638+
map<Variable, map<pair<ArchAndAddr, bool>, PossibleValueSet>> Function::GetAllUserVariableValues()
26872639
{
26882640
size_t count;
2689-
map<Variable, map<ArchAndAddr, PossibleValueSet>> result;
2641+
map<Variable, map<pair<ArchAndAddr, bool>, PossibleValueSet>> result;
26902642
BNUserVariableValue* var_values = BNGetAllUserVariableValues(m_object, &count);
26912643

26922644
for (size_t i = 0; i < count; i++)
@@ -2696,7 +2648,7 @@ map<Variable, map<ArchAndAddr, PossibleValueSet>> Function::GetAllUserVariableVa
26962648
uint64_t address = var_values[i].defSite.address;
26972649
ArchAndAddr defSite(arch, address);
26982650
PossibleValueSet value = PossibleValueSet::FromAPIObject(var_values[i].value);
2699-
result[var][defSite] = value;
2651+
result[var][{defSite, var_values[i].after}] = value;
27002652
}
27012653

27022654
BNFreeUserVariableValues(var_values);
@@ -2706,17 +2658,47 @@ map<Variable, map<ArchAndAddr, PossibleValueSet>> Function::GetAllUserVariableVa
27062658

27072659
void Function::ClearAllUserVariableValues()
27082660
{
2709-
const map<Variable, map<ArchAndAddr, PossibleValueSet>>& allValues = GetAllUserVariableValues();
2661+
const map<Variable, map<pair<ArchAndAddr, bool>, PossibleValueSet>>& allValues = GetAllUserVariableValues();
27102662
for (auto& valuePair : allValues)
27112663
{
27122664
for (auto& valMap : valuePair.second)
27132665
{
2714-
ClearUserVariableValue(valuePair.first, valMap.first.address);
2666+
ClearUserVariableValue(valuePair.first, valMap.first.first, valMap.first.second);
27152667
}
27162668
}
27172669
}
27182670

27192671

2672+
void Function::CreateForcedVariableVersion(const Variable& var, const ArchAndAddr& location)
2673+
{
2674+
auto defSite = BNArchitectureAndAddress();
2675+
defSite.arch = location.arch->m_object;
2676+
defSite.address = location.address;
2677+
2678+
auto var_data = BNVariable();
2679+
var_data.type = var.type;
2680+
var_data.index = var.index;
2681+
var_data.storage = var.storage;
2682+
2683+
BNCreateForcedVariableVersion(m_object, &var_data, &defSite);
2684+
}
2685+
2686+
2687+
void Function::ClearForcedVariableVersion(const Variable& var, const ArchAndAddr& location)
2688+
{
2689+
auto defSite = BNArchitectureAndAddress();
2690+
defSite.arch = location.arch->m_object;
2691+
defSite.address = location.address;
2692+
2693+
auto var_data = BNVariable();
2694+
var_data.type = var.type;
2695+
var_data.index = var.index;
2696+
var_data.storage = var.storage;
2697+
2698+
BNClearForcedVariableVersion(m_object, &var_data, &defSite);
2699+
}
2700+
2701+
27202702
void Function::RequestDebugReport(const string& name)
27212703
{
27222704
BNRequestFunctionDebugReport(m_object, name.c_str());

0 commit comments

Comments
 (0)