Skip to content

Commit 8df9a34

Browse files
committed
Add type IDs for types to track across renames
1 parent c6ed1dd commit 8df9a34

8 files changed

Lines changed: 354 additions & 38 deletions

File tree

binaryninjaapi.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,12 @@ void BinaryNinja::SetWorkerThreadCount(size_t count)
237237
{
238238
BNSetWorkerThreadCount(count);
239239
}
240+
241+
242+
string BinaryNinja::GetUniqueIdentifierString()
243+
{
244+
char* str = BNGetUniqueIdentifierString();
245+
string result = str;
246+
BNFreeString(str);
247+
return result;
248+
}

binaryninjaapi.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,8 @@ namespace BinaryNinja
409409
BNMessageBoxButtonResult ShowMessageBox(const std::string& title, const std::string& text,
410410
BNMessageBoxButtonSet buttons = OKButtonSet, BNMessageBoxIcon icon = InformationIcon);
411411

412+
std::string GetUniqueIdentifierString();
413+
412414
class QualifiedName
413415
{
414416
std::vector<std::string> m_name;
@@ -1027,11 +1029,15 @@ namespace BinaryNinja
10271029

10281030
std::map<QualifiedName, Ref<Type>> GetTypes();
10291031
Ref<Type> GetTypeByName(const QualifiedName& name);
1032+
Ref<Type> GetTypeById(const std::string& id);
1033+
std::string GetTypeId(const QualifiedName& name);
1034+
QualifiedName GetTypeNameById(const std::string& id);
10301035
bool IsTypeAutoDefined(const QualifiedName& name);
1031-
void DefineType(const QualifiedName& name, Ref<Type> type);
1036+
QualifiedName DefineType(const std::string& id, const QualifiedName& defaultName, Ref<Type> type);
10321037
void DefineUserType(const QualifiedName& name, Ref<Type> type);
1033-
void UndefineType(const QualifiedName& name);
1038+
void UndefineType(const std::string& id);
10341039
void UndefineUserType(const QualifiedName& name);
1040+
void RenameType(const QualifiedName& oldName, const QualifiedName& newName);
10351041

10361042
bool FindNextData(uint64_t start, const DataBuffer& data, uint64_t& result, BNFindFlag flags = NoFindFlags);
10371043

@@ -1611,25 +1617,40 @@ namespace BinaryNinja
16111617
static Ref<Type> StructureType(Structure* strct);
16121618
static Ref<Type> NamedType(NamedTypeReference* ref, size_t width = 0, size_t align = 1);
16131619
static Ref<Type> NamedType(const QualifiedName& name, Type* type);
1620+
static Ref<Type> NamedType(const std::string& id, const QualifiedName& name, Type* type);
1621+
static Ref<Type> NamedType(BinaryView* view, const QualifiedName& name);
16141622
static Ref<Type> EnumerationType(Architecture* arch, Enumeration* enm, size_t width = 0, bool issigned = false);
16151623
static Ref<Type> PointerType(Architecture* arch, Type* type, bool cnst = false, bool vltl = false,
16161624
BNReferenceType refType = PointerReferenceType);
16171625
static Ref<Type> ArrayType(Type* type, uint64_t elem);
16181626
static Ref<Type> FunctionType(Type* returnValue, CallingConvention* callingConvention,
16191627
const std::vector<NameAndType>& params, bool varArg = false);
1628+
1629+
static std::string GenerateAutoTypeId(const std::string& source, const QualifiedName& name);
1630+
static std::string GenerateAutoPlatformTypeId(const QualifiedName& name);
1631+
static std::string GenerateAutoDemangledTypeId(const QualifiedName& name);
16201632
};
16211633

16221634
class NamedTypeReference: public CoreRefCountObject<BNNamedTypeReference, BNNewNamedTypeReference,
16231635
BNFreeNamedTypeReference>
16241636
{
16251637
public:
16261638
NamedTypeReference(BNNamedTypeReference* nt);
1627-
NamedTypeReference(BNNamedTypeReferenceClass cls = UnknownNamedTypeClass,
1639+
NamedTypeReference(BNNamedTypeReferenceClass cls = UnknownNamedTypeClass, const std::string& id = "",
16281640
const QualifiedName& name = QualifiedName());
16291641
BNNamedTypeReferenceClass GetTypeClass() const;
16301642
void SetTypeClass(BNNamedTypeReferenceClass cls);
1643+
std::string GetTypeId() const;
1644+
void SetTypeId(const std::string& id);
16311645
QualifiedName GetName() const;
16321646
void SetName(const QualifiedName& name);
1647+
1648+
static Ref<NamedTypeReference> GenerateAutoTypeReference(BNNamedTypeReferenceClass cls,
1649+
const std::string& source, const QualifiedName& name);
1650+
static Ref<NamedTypeReference> GenerateAutoPlatformTypeReference(BNNamedTypeReferenceClass cls,
1651+
const QualifiedName& name);
1652+
static Ref<NamedTypeReference> GenerateAutoDemangledTypeReference(BNNamedTypeReferenceClass cls,
1653+
const QualifiedName& name);
16331654
};
16341655

16351656
struct StructureMember

binaryninjacore.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,8 @@ extern "C"
12341234
BINARYNINJACOREAPI void BNRegisterObjectDestructionCallbacks(BNObjectDestructionCallbacks* callbacks);
12351235
BINARYNINJACOREAPI void BNUnregisterObjectDestructionCallbacks(BNObjectDestructionCallbacks* callbacks);
12361236

1237+
BINARYNINJACOREAPI char* BNGetUniqueIdentifierString(void);
1238+
12371239
// Plugin initialization
12381240
BINARYNINJACOREAPI void BNInitCorePlugins(void);
12391241
BINARYNINJACOREAPI void BNInitUserPlugins(void);
@@ -1807,11 +1809,19 @@ extern "C"
18071809
BINARYNINJACOREAPI BNQualifiedNameAndType* BNGetAnalysisTypeList(BNBinaryView* view, size_t* count);
18081810
BINARYNINJACOREAPI void BNFreeTypeList(BNQualifiedNameAndType* types, size_t count);
18091811
BINARYNINJACOREAPI BNType* BNGetAnalysisTypeByName(BNBinaryView* view, BNQualifiedName* name);
1812+
BINARYNINJACOREAPI BNType* BNGetAnalysisTypeById(BNBinaryView* view, const char* id);
1813+
BINARYNINJACOREAPI char* BNGetAnalysisTypeId(BNBinaryView* view, BNQualifiedName* name);
1814+
BINARYNINJACOREAPI BNQualifiedName BNGetAnalysisTypeNameById(BNBinaryView* view, const char* id);
18101815
BINARYNINJACOREAPI bool BNIsAnalysisTypeAutoDefined(BNBinaryView* view, BNQualifiedName* name);
1811-
BINARYNINJACOREAPI void BNDefineAnalysisType(BNBinaryView* view, BNQualifiedName* name, BNType* type);
1816+
BINARYNINJACOREAPI BNQualifiedName BNDefineAnalysisType(BNBinaryView* view, const char* id,
1817+
BNQualifiedName* defaultName, BNType* type);
18121818
BINARYNINJACOREAPI void BNDefineUserAnalysisType(BNBinaryView* view, BNQualifiedName* name, BNType* type);
1813-
BINARYNINJACOREAPI void BNUndefineAnalysisType(BNBinaryView* view, BNQualifiedName* name);
1819+
BINARYNINJACOREAPI void BNUndefineAnalysisType(BNBinaryView* view, const char* id);
18141820
BINARYNINJACOREAPI void BNUndefineUserAnalysisType(BNBinaryView* view, BNQualifiedName* name);
1821+
BINARYNINJACOREAPI void BNRenameAnalysisType(BNBinaryView* view, BNQualifiedName* oldName, BNQualifiedName* newName);
1822+
BINARYNINJACOREAPI char* BNGenerateAutoTypeId(const char* source, BNQualifiedName* name);
1823+
BINARYNINJACOREAPI char* BNGenerateAutoPlatformTypeId(BNQualifiedName* name);
1824+
BINARYNINJACOREAPI char* BNGenerateAutoDemangledTypeId(BNQualifiedName* name);
18151825

18161826
BINARYNINJACOREAPI void BNReanalyzeAllFunctions(BNBinaryView* view);
18171827
BINARYNINJACOREAPI void BNReanalyzeFunction(BNFunction* func);
@@ -2006,10 +2016,13 @@ extern "C"
20062016
BINARYNINJACOREAPI void BNFreeTokenList(BNInstructionTextToken* tokens, size_t count);
20072017

20082018
BINARYNINJACOREAPI BNType* BNCreateNamedTypeReference(BNNamedTypeReference* nt, size_t width, size_t align);
2009-
BINARYNINJACOREAPI BNType* BNCreateNamedTypeReferenceFromType(BNQualifiedName* name, BNType* type);
2019+
BINARYNINJACOREAPI BNType* BNCreateNamedTypeReferenceFromTypeAndId(const char* id, BNQualifiedName* name, BNType* type);
2020+
BINARYNINJACOREAPI BNType* BNCreateNamedTypeReferenceFromType(BNBinaryView* view, BNQualifiedName* name);
20102021
BINARYNINJACOREAPI BNNamedTypeReference* BNCreateNamedType(void);
20112022
BINARYNINJACOREAPI void BNSetTypeReferenceClass(BNNamedTypeReference* nt, BNNamedTypeReferenceClass cls);
20122023
BINARYNINJACOREAPI BNNamedTypeReferenceClass BNGetTypeReferenceClass(BNNamedTypeReference* nt);
2024+
BINARYNINJACOREAPI void BNSetTypeReferenceId(BNNamedTypeReference* nt, const char* id);
2025+
BINARYNINJACOREAPI char* BNGetTypeReferenceId(BNNamedTypeReference* nt);
20132026
BINARYNINJACOREAPI void BNSetTypeReferenceName(BNNamedTypeReference* nt, BNQualifiedName* name);
20142027
BINARYNINJACOREAPI BNQualifiedName BNGetTypeReferenceName(BNNamedTypeReference* nt);
20152028
BINARYNINJACOREAPI void BNFreeQualifiedName(BNQualifiedName* name);

binaryview.cpp

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,35 @@ Ref<Type> BinaryView::GetTypeByName(const QualifiedName& name)
14961496
}
14971497

14981498

1499+
Ref<Type> BinaryView::GetTypeById(const string& id)
1500+
{
1501+
BNType* type = BNGetAnalysisTypeById(m_object, id.c_str());
1502+
if (!type)
1503+
return nullptr;
1504+
return new Type(type);
1505+
}
1506+
1507+
1508+
QualifiedName BinaryView::GetTypeNameById(const string& id)
1509+
{
1510+
BNQualifiedName name = BNGetAnalysisTypeNameById(m_object, id.c_str());
1511+
QualifiedName result = QualifiedName::FromAPIObject(&name);
1512+
BNFreeQualifiedName(&name);
1513+
return result;
1514+
}
1515+
1516+
1517+
string BinaryView::GetTypeId(const QualifiedName& name)
1518+
{
1519+
BNQualifiedName nameObj = name.GetAPIObject();
1520+
char* id = BNGetAnalysisTypeId(m_object, &nameObj);
1521+
QualifiedName::FreeAPIObject(&nameObj);
1522+
string result = id;
1523+
BNFreeString(id);
1524+
return result;
1525+
}
1526+
1527+
14991528
bool BinaryView::IsTypeAutoDefined(const QualifiedName& name)
15001529
{
15011530
BNQualifiedName nameObj = name.GetAPIObject();
@@ -1505,11 +1534,14 @@ bool BinaryView::IsTypeAutoDefined(const QualifiedName& name)
15051534
}
15061535

15071536

1508-
void BinaryView::DefineType(const QualifiedName& name, Ref<Type> type)
1537+
QualifiedName BinaryView::DefineType(const string& id, const QualifiedName& defaultName, Ref<Type> type)
15091538
{
1510-
BNQualifiedName nameObj = name.GetAPIObject();
1511-
BNDefineAnalysisType(m_object, &nameObj, type->GetObject());
1539+
BNQualifiedName nameObj = defaultName.GetAPIObject();
1540+
BNQualifiedName regName = BNDefineAnalysisType(m_object, id.c_str(), &nameObj, type->GetObject());
15121541
QualifiedName::FreeAPIObject(&nameObj);
1542+
QualifiedName result = QualifiedName::FromAPIObject(&regName);
1543+
BNFreeQualifiedName(&regName);
1544+
return result;
15131545
}
15141546

15151547

@@ -1521,11 +1553,9 @@ void BinaryView::DefineUserType(const QualifiedName& name, Ref<Type> type)
15211553
}
15221554

15231555

1524-
void BinaryView::UndefineType(const QualifiedName& name)
1556+
void BinaryView::UndefineType(const string& id)
15251557
{
1526-
BNQualifiedName nameObj = name.GetAPIObject();
1527-
BNUndefineAnalysisType(m_object, &nameObj);
1528-
QualifiedName::FreeAPIObject(&nameObj);
1558+
BNUndefineAnalysisType(m_object, id.c_str());
15291559
}
15301560

15311561

@@ -1537,6 +1567,16 @@ void BinaryView::UndefineUserType(const QualifiedName& name)
15371567
}
15381568

15391569

1570+
void BinaryView::RenameType(const QualifiedName& oldName, const QualifiedName& newName)
1571+
{
1572+
BNQualifiedName oldNameObj = oldName.GetAPIObject();
1573+
BNQualifiedName newNameObj = newName.GetAPIObject();
1574+
BNRenameAnalysisType(m_object, &oldNameObj, &newNameObj);
1575+
QualifiedName::FreeAPIObject(&oldNameObj);
1576+
QualifiedName::FreeAPIObject(&newNameObj);
1577+
}
1578+
1579+
15401580
bool BinaryView::FindNextData(uint64_t start, const DataBuffer& data, uint64_t& result, BNFindFlag flags)
15411581
{
15421582
return BNFindNextData(m_object, start, data.GetBufferObject(), &result, flags);

python/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ def shutdown():
5151
core.BNShutdown()
5252

5353

54+
def get_unique_identifier():
55+
return core.BNGetUniqueIdentifierString()
56+
57+
5458
class _DestructionCallbackHandler(object):
5559
def __init__(self):
5660
self._cb = core.BNObjectDestructionCallbacks()

0 commit comments

Comments
 (0)