summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn Smith <glenn@vector35.com>2022-11-15 16:21:43 -0500
committerGlenn Smith <glenn@vector35.com>2022-11-16 13:57:49 -0500
commit2b7ff4a4a4033e9f434da6f6b186d4798dbe59ee (patch)
tree143f9f7850438b7a43df378e971e7ab7fc53558d
parentebfbe25ef9b0ae5f5d6c6d53bd5faee7b9a77d59 (diff)
Add BinaryView::GetDependencySortedTypes
-rw-r--r--binaryninjaapi.h11
-rw-r--r--binaryninjacore.h1
-rw-r--r--binaryview.cpp17
-rw-r--r--python/binaryview.py20
4 files changed, 47 insertions, 2 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h
index b738c92a..8cb0a7b2 100644
--- a/binaryninjaapi.h
+++ b/binaryninjaapi.h
@@ -4423,6 +4423,17 @@ namespace BinaryNinja {
std::string& errors, const std::set<QualifiedName>& typesAllowRedefinition = {});
std::map<QualifiedName, Ref<Type>> GetTypes();
+ /*! List of all types, sorted such that types are after all types on which they depend
+
+ Order is guaranteed for any collection of types with no cycles. If you have cycles
+ in type dependencies, order for types in a cycle is not guaranteed.
+
+ \note Dependency order is based on named type references for all non-structure types, i.e.
+ ``struct Foo m_foo`` will induce a dependency, whereas ``struct Foo* m_pFoo`` will not.
+
+ \return Sorted types as defined above
+ */
+ std::vector<std::pair<QualifiedName, Ref<Type>>> GetDependencySortedTypes();
std::vector<QualifiedName> GetTypeNames(const std::string& matching = "");
Ref<Type> GetTypeByName(const QualifiedName& name);
Ref<Type> GetTypeByRef(Ref<NamedTypeReference> name);
diff --git a/binaryninjacore.h b/binaryninjacore.h
index c7530c22..ba7b60d4 100644
--- a/binaryninjacore.h
+++ b/binaryninjacore.h
@@ -4288,6 +4288,7 @@ extern "C"
BINARYNINJACOREAPI char* BNUnescapeTypeName(const char* name, BNTokenEscapingType escaping);
BINARYNINJACOREAPI BNQualifiedNameAndType* BNGetAnalysisTypeList(BNBinaryView* view, size_t* count);
+ BINARYNINJACOREAPI BNQualifiedNameAndType* BNGetAnalysisDependencySortedTypeList(BNBinaryView* view, size_t* count);
BINARYNINJACOREAPI void BNFreeTypeList(BNQualifiedNameAndType* types, size_t count);
BINARYNINJACOREAPI void BNFreeTypeIdList(BNQualifiedNameTypeAndId* types, size_t count);
BINARYNINJACOREAPI BNQualifiedName* BNGetAnalysisTypeNames(BNBinaryView* view, size_t* count, const char* matching);
diff --git a/binaryview.cpp b/binaryview.cpp
index e8c108a0..9459fdd0 100644
--- a/binaryview.cpp
+++ b/binaryview.cpp
@@ -3426,6 +3426,23 @@ map<QualifiedName, Ref<Type>> BinaryView::GetTypes()
}
+vector<pair<QualifiedName, Ref<Type>>> BinaryView::GetDependencySortedTypes()
+{
+ size_t count;
+ BNQualifiedNameAndType* types = BNGetAnalysisDependencySortedTypeList(m_object, &count);
+
+ vector<pair<QualifiedName, Ref<Type>>> result;
+ for (size_t i = 0; i < count; i++)
+ {
+ QualifiedName name = QualifiedName::FromAPIObject(&types[i].name);
+ result.emplace_back(name, new Type(BNNewTypeReference(types[i].type)));
+ }
+
+ BNFreeTypeList(types, count);
+ return result;
+}
+
+
vector<QualifiedName> BinaryView::GetTypeNames(const string& matching)
{
size_t count;
diff --git a/python/binaryview.py b/python/binaryview.py
index d73c8ebc..9c318f0a 100644
--- a/python/binaryview.py
+++ b/python/binaryview.py
@@ -1705,11 +1705,12 @@ class TypeMapping(collections.abc.Mapping): # type: ignore
>>> print("Found")
"""
- def __init__(self, view: 'BinaryView'):
+ def __init__(self, view: 'BinaryView', get_list_fn=core.BNGetAnalysisTypeList):
self._type_list = None
self._count = None
self._type_cache: Optional[Mapping[_types.QualifiedName, _types.Type]] = None
self._view = view
+ self._get_list_fn = get_list_fn
def __repr__(self):
return f"<TypeMapping {len(self)} symbols: {self._type_cache}>"
@@ -1732,7 +1733,7 @@ class TypeMapping(collections.abc.Mapping): # type: ignore
yield from self._type_cache
count = ctypes.c_ulonglong(0)
- type_list = core.BNGetAnalysisTypeList(self._view.handle, count)
+ type_list = self._get_list_fn(self._view.handle, count)
assert type_list is not None, "core.BNGetAnalysisTypeList returned None"
self._type_list = type_list
self._type_cache = {}
@@ -2647,6 +2648,21 @@ class BinaryView:
return TypeMapping(self)
@property
+ def dependency_sorted_types(self) -> TypeMapping:
+ """
+ List of all types, sorted such that types are after all types on which they depend (read-only)
+
+ Order is guaranteed for any collection of types with no cycles. If you have cycles
+ in type dependencies, order for types in a cycle is not guaranteed.
+
+ .. note:: Dependency order is based on named type references for all non-structure types, i.e.
+ ``struct Foo m_foo`` will induce a dependency, whereas ``struct Foo* m_pFoo`` will not.
+
+ :return: Sorted types as defined above
+ """
+ return TypeMapping(self, core.BNGetAnalysisDependencySortedTypeList)
+
+ @property
def type_names(self) -> List['_types.Type']:
"""List of defined type names (read-only)"""
count = ctypes.c_ulonglong(0)