Skip to content

Commit 0d69b8e

Browse files
committed
uidf refactor
1 parent b546d0c commit 0d69b8e

26 files changed

+668
-104
lines changed

binaryninjaapi.h

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10586,7 +10586,7 @@ namespace BinaryNinja {
1058610586
size_t count;
1058710587

1058810588
static PossibleValueSet FromAPIObject(BNPossibleValueSet& value);
10589-
BNPossibleValueSet ToAPIObject();
10589+
BNPossibleValueSet ToAPIObject() const;
1059010590
static void FreeAPIObject(BNPossibleValueSet* value);
1059110591
};
1059210592

@@ -11143,11 +11143,14 @@ namespace BinaryNinja {
1114311143

1114411144
Ref<FlowGraph> GetUnresolvedStackAdjustmentGraph();
1114511145

11146-
void SetUserVariableValue(const Variable& var, uint64_t defAddr, PossibleValueSet& value);
11147-
void ClearUserVariableValue(const Variable& var, uint64_t defAddr);
11148-
std::map<Variable, std::map<ArchAndAddr, PossibleValueSet>> GetAllUserVariableValues();
11146+
void SetUserVariableValue(const Variable& var, const ArchAndAddr& defAddr, PossibleValueSet& value, bool after = true);
11147+
void ClearUserVariableValue(const Variable& var, const ArchAndAddr& defAddr, bool after = true);
11148+
std::map<Variable, std::map<std::pair<ArchAndAddr, bool>, PossibleValueSet>> GetAllUserVariableValues();
1114911149
void ClearAllUserVariableValues();
1115011150

11151+
void CreateForcedVariableVersion(const Variable& var, const ArchAndAddr& location);
11152+
void ClearForcedVariableVersion(const Variable& var, const ArchAndAddr& location);
11153+
1115111154
void RequestDebugReport(const std::string& name);
1115211155

1115311156
/*! Get the name for a given label ID
@@ -11757,6 +11760,9 @@ namespace BinaryNinja {
1175711760
std::vector<SSARegisterStack> GetSSARegisterStacks();
1175811761
std::vector<SSAFlag> GetSSAFlags();
1175911762

11763+
size_t CachePossibleValueSet(const PossibleValueSet& pvs);
11764+
PossibleValueSet GetCachedPossibleValueSet(size_t idx);
11765+
1176011766
ExprId AddExpr(BNLowLevelILOperation operation, size_t size, uint32_t flags, ExprId a = 0, ExprId b = 0,
1176111767
ExprId c = 0, ExprId d = 0);
1176211768
ExprId AddExprWithLocation(BNLowLevelILOperation operation, uint64_t addr, uint32_t sourceOperand, size_t size,
@@ -11845,6 +11851,12 @@ namespace BinaryNinja {
1184511851
ExprId SetFlag(uint32_t flag, ExprId val, const ILSourceLocation& loc = ILSourceLocation());
1184611852
ExprId SetFlagSSA(const SSAFlag& flag, ExprId val, const ILSourceLocation& loc = ILSourceLocation());
1184711853

11854+
ExprId ForceVer(size_t size, uint32_t reg, const ILSourceLocation& loc = ILSourceLocation());
11855+
ExprId ForceVerSSA(size_t size, SSARegister dst, SSARegister src, const ILSourceLocation& loc = ILSourceLocation());
11856+
11857+
ExprId Assert(size_t size, uint32_t reg, const PossibleValueSet& pvs, const ILSourceLocation& loc = ILSourceLocation());
11858+
ExprId AssertSSA(size_t size, SSARegister reg, const PossibleValueSet& pvs, const ILSourceLocation& loc = ILSourceLocation());
11859+
1184811860
/*! Reads \c size bytes from the expression \c addr
1184911861

1185011862
\param size Number of bytes to read
@@ -13141,6 +13153,9 @@ namespace BinaryNinja {
1314113153
*/
1314213154
BNMediumLevelILLabel* GetLabelForSourceInstruction(size_t i);
1314313155

13156+
size_t CachePossibleValueSet(const PossibleValueSet& pvs);
13157+
PossibleValueSet GetCachedPossibleValueSet(size_t idx);
13158+
1314413159
ExprId AddExpr(BNMediumLevelILOperation operation, size_t size, ExprId a = 0, ExprId b = 0, ExprId c = 0,
1314513160
ExprId d = 0, ExprId e = 0);
1314613161
ExprId AddExprWithLocation(BNMediumLevelILOperation operation, uint64_t addr, uint32_t sourceOperand,
@@ -13164,6 +13179,13 @@ namespace BinaryNinja {
1316413179
const ILSourceLocation& loc = ILSourceLocation());
1316513180
ExprId SetVarAliasedField(size_t size, const Variable& dest, size_t newMemVersion, size_t prevMemVersion,
1316613181
uint64_t offset, ExprId src, const ILSourceLocation& loc = ILSourceLocation());
13182+
13183+
ExprId ForceVer(size_t size, const Variable& dest, const Variable& src, const ILSourceLocation& loc = ILSourceLocation());
13184+
ExprId ForceVerSSA(size_t size, const SSAVariable& dest, const SSAVariable& src, const ILSourceLocation& loc = ILSourceLocation());
13185+
13186+
ExprId Assert(size_t size, const Variable& src, const PossibleValueSet& pvs, const ILSourceLocation& loc = ILSourceLocation());
13187+
ExprId AssertSSA(size_t size, const SSAVariable& src, const PossibleValueSet& pvs, const ILSourceLocation& loc = ILSourceLocation());
13188+
1316713189
ExprId Load(size_t size, ExprId src, const ILSourceLocation& loc = ILSourceLocation());
1316813190
ExprId LoadStruct(size_t size, ExprId src, uint64_t offset, const ILSourceLocation& loc = ILSourceLocation());
1316913191
ExprId LoadSSA(size_t size, ExprId src, size_t memVersion, const ILSourceLocation& loc = ILSourceLocation());
@@ -13511,6 +13533,9 @@ namespace BinaryNinja {
1351113533
void SetRootExpr(ExprId expr);
1351213534
void SetRootExpr(const HighLevelILInstruction& expr);
1351313535

13536+
size_t CachePossibleValueSet(const PossibleValueSet& pvs);
13537+
PossibleValueSet GetCachedPossibleValueSet(size_t idx);
13538+
1351413539
ExprId AddExpr(BNHighLevelILOperation operation, size_t size, ExprId a = 0, ExprId b = 0, ExprId c = 0,
1351513540
ExprId d = 0, ExprId e = 0);
1351613541
ExprId AddExprWithLocation(BNHighLevelILOperation operation, uint64_t addr, uint32_t sourceOperand, size_t size,
@@ -13555,6 +13580,13 @@ namespace BinaryNinja {
1355513580
const ILSourceLocation& loc = ILSourceLocation());
1355613581
ExprId AssignUnpackMemSSA(const std::vector<ExprId>& output, size_t destMemVersion, ExprId src,
1355713582
size_t srcMemVersion, const ILSourceLocation& loc = ILSourceLocation());
13583+
13584+
ExprId ForceVer(size_t size, const Variable& dest, const Variable& src, const ILSourceLocation& loc = ILSourceLocation());
13585+
ExprId ForceVerSSA(size_t size, const SSAVariable& dest, const SSAVariable& src, const ILSourceLocation& loc = ILSourceLocation());
13586+
13587+
ExprId Assert(size_t size, const Variable& src, const PossibleValueSet& pvs, const ILSourceLocation& loc = ILSourceLocation());
13588+
ExprId AssertSSA(size_t size, const SSAVariable& src, const PossibleValueSet& pvs, const ILSourceLocation& loc = ILSourceLocation());
13589+
1355813590
ExprId Var(size_t size, const Variable& src, const ILSourceLocation& loc = ILSourceLocation());
1355913591
ExprId VarSSA(size_t size, const SSAVariable& src, const ILSourceLocation& loc = ILSourceLocation());
1356013592
ExprId VarPhi(const SSAVariable& dest, const std::vector<SSAVariable>& sources,

binaryninjacore.h

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
// Current ABI version for linking to the core. This is incremented any time
3838
// there are changes to the API that affect linking, including new functions,
3939
// new types, or modifications to existing functions or types.
40-
#define BN_CURRENT_CORE_ABI_VERSION 93
40+
#define BN_CURRENT_CORE_ABI_VERSION 94
4141

4242
// Minimum ABI version that is supported for loading of plugins. Plugins that
4343
// are linked to an ABI version less than this will not be able to load and
@@ -508,6 +508,8 @@ extern "C"
508508
LLIL_SET_FLAG, // Not valid in SSA form (see LLIL_SET_FLAG_SSA)
509509
LLIL_SET_REG_STACK_REL, // Not valid in SSA form (see LLIL_SET_REG_STACK_REL_SSA)
510510
LLIL_REG_STACK_PUSH, // Not valid in SSA form (expanded)
511+
LLIL_ASSERT,
512+
LLIL_FORCE_VER,
511513
LLIL_LOAD, // Not valid in SSA form (see LLIL_LOAD_SSA)
512514
LLIL_STORE, // Not valid in SSA form (see LLIL_STORE_SSA)
513515
LLIL_PUSH, // Not valid in SSA form (expanded)
@@ -627,6 +629,8 @@ extern "C"
627629
LLIL_REG_STACK_FREE_REL_SSA,
628630
LLIL_REG_STACK_FREE_ABS_SSA,
629631
LLIL_SET_FLAG_SSA,
632+
LLIL_ASSERT_SSA,
633+
LLIL_FORCE_VER_SSA,
630634
LLIL_FLAG_SSA,
631635
LLIL_FLAG_BIT_SSA,
632636
LLIL_CALL_SSA,
@@ -1199,6 +1203,8 @@ extern "C"
11991203
MLIL_SET_VAR, // Not valid in SSA form (see MLIL_SET_VAR_SSA)
12001204
MLIL_SET_VAR_FIELD, // Not valid in SSA form (see MLIL_SET_VAR_FIELD)
12011205
MLIL_SET_VAR_SPLIT, // Not valid in SSA form (see MLIL_SET_VAR_SPLIT_SSA)
1206+
MLIL_ASSERT,
1207+
MLIL_FORCE_VER,
12021208
MLIL_LOAD, // Not valid in SSA form (see MLIL_LOAD_SSA)
12031209
MLIL_LOAD_STRUCT, // Not valid in SSA form (see MLIL_LOAD_STRUCT_SSA)
12041210
MLIL_STORE, // Not valid in SSA form (see MLIL_STORE_SSA)
@@ -1319,6 +1325,8 @@ extern "C"
13191325
MLIL_VAR_ALIASED,
13201326
MLIL_VAR_ALIASED_FIELD,
13211327
MLIL_VAR_SPLIT_SSA,
1328+
MLIL_ASSERT_SSA,
1329+
MLIL_FORCE_VER_SSA,
13221330
MLIL_CALL_SSA,
13231331
MLIL_CALL_UNTYPED_SSA,
13241332
MLIL_SYSCALL_SSA,
@@ -1395,6 +1403,8 @@ extern "C"
13951403
HLIL_VAR_INIT,
13961404
HLIL_ASSIGN,
13971405
HLIL_ASSIGN_UNPACK,
1406+
HLIL_FORCE_VER,
1407+
HLIL_ASSERT,
13981408
HLIL_VAR,
13991409
HLIL_STRUCT_FIELD,
14001410
HLIL_ARRAY_INDEX,
@@ -1496,6 +1506,8 @@ extern "C"
14961506
HLIL_VAR_INIT_SSA,
14971507
HLIL_ASSIGN_MEM_SSA,
14981508
HLIL_ASSIGN_UNPACK_MEM_SSA,
1509+
HLIL_FORCE_VER_SSA,
1510+
HLIL_ASSERT_SSA,
14991511
HLIL_VAR_SSA,
15001512
HLIL_ARRAY_INDEX_SSA,
15011513
HLIL_DEREF_SSA,
@@ -2609,6 +2621,7 @@ extern "C"
26092621
{
26102622
BNVariable var;
26112623
BNArchitectureAndAddress defSite;
2624+
bool after;
26122625
BNPossibleValueSet value;
26132626
} BNUserVariableValue;
26142627

@@ -5392,14 +5405,17 @@ extern "C"
53925405
BINARYNINJACOREAPI BNFlowGraph* BNGetUnresolvedStackAdjustmentGraph(BNFunction* func);
53935406

53945407
BINARYNINJACOREAPI void BNSetUserVariableValue(BNFunction* func, const BNVariable* var,
5395-
const BNArchitectureAndAddress* defSite, const BNPossibleValueSet* value);
5408+
const BNArchitectureAndAddress* defSite, bool after, const BNPossibleValueSet* value);
53965409
BINARYNINJACOREAPI void BNClearUserVariableValue(
5397-
BNFunction* func, const BNVariable* var, const BNArchitectureAndAddress* defSite);
5410+
BNFunction* func, const BNVariable* var, const BNArchitectureAndAddress* defSite, bool after);
53985411
BINARYNINJACOREAPI BNUserVariableValue* BNGetAllUserVariableValues(BNFunction* func, size_t* count);
53995412
BINARYNINJACOREAPI void BNFreeUserVariableValues(BNUserVariableValue* result);
54005413
BINARYNINJACOREAPI bool BNParsePossibleValueSet(BNBinaryView* view, const char* valueText,
54015414
BNRegisterValueType state, BNPossibleValueSet* result, uint64_t here, char** errors);
54025415

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

54055421
BINARYNINJACOREAPI BNILReferenceSource* BNGetMediumLevelILVariableReferences(
@@ -5743,6 +5759,9 @@ extern "C"
57435759
BNLowLevelILFunction* func, size_t expr, size_t operand, size_t* count);
57445760
BINARYNINJACOREAPI void BNLowLevelILFreeOperandList(uint64_t* operands);
57455761

5762+
BINARYNINJACOREAPI size_t BNCacheLowLevelILPossibleValueSet(BNLowLevelILFunction* func, BNPossibleValueSet* pvs);
5763+
BINARYNINJACOREAPI BNPossibleValueSet BNGetCachedLowLevelILPossibleValueSet(BNLowLevelILFunction* func, size_t idx);
5764+
57465765
BINARYNINJACOREAPI BNLowLevelILInstruction BNGetLowLevelILByIndex(BNLowLevelILFunction* func, size_t i);
57475766
BINARYNINJACOREAPI size_t BNGetLowLevelILIndexForInstruction(BNLowLevelILFunction* func, size_t i);
57485767
BINARYNINJACOREAPI size_t BNGetLowLevelILInstructionForExpr(BNLowLevelILFunction* func, size_t expr);
@@ -5896,6 +5915,9 @@ extern "C"
58965915
BNMediumLevelILFunction* func, size_t expr, size_t operand, size_t* count);
58975916
BINARYNINJACOREAPI void BNMediumLevelILFreeOperandList(uint64_t* operands);
58985917

5918+
BINARYNINJACOREAPI size_t BNCacheMediumLevelILPossibleValueSet(BNMediumLevelILFunction* func, BNPossibleValueSet* pvs);
5919+
BINARYNINJACOREAPI BNPossibleValueSet BNGetCachedMediumLevelILPossibleValueSet(BNMediumLevelILFunction* func, size_t idx);
5920+
58995921
BINARYNINJACOREAPI BNMediumLevelILInstruction BNGetMediumLevelILByIndex(BNMediumLevelILFunction* func, size_t i);
59005922
BINARYNINJACOREAPI size_t BNGetMediumLevelILIndexForInstruction(BNMediumLevelILFunction* func, size_t i);
59015923
BINARYNINJACOREAPI size_t BNGetMediumLevelILInstructionForExpr(BNMediumLevelILFunction* func, size_t expr);
@@ -6054,6 +6076,9 @@ extern "C"
60546076
BNHighLevelILFunction* func, size_t expr, size_t operand, size_t* count);
60556077
BINARYNINJACOREAPI void BNHighLevelILFreeOperandList(uint64_t* operands);
60566078

6079+
BINARYNINJACOREAPI size_t BNCacheHighLevelILPossibleValueSet(BNHighLevelILFunction* func, BNPossibleValueSet* pvs);
6080+
BINARYNINJACOREAPI BNPossibleValueSet BNGetCachedHighLevelILPossibleValueSet(BNHighLevelILFunction* func, size_t idx);
6081+
60576082
BINARYNINJACOREAPI BNHighLevelILInstruction BNGetHighLevelILByIndex(
60586083
BNHighLevelILFunction* func, size_t i, bool asFullAst);
60596084
BINARYNINJACOREAPI size_t BNGetHighLevelILIndexForInstruction(BNHighLevelILFunction* func, size_t i);

function.cpp

Lines changed: 44 additions & 62 deletions
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)