Skip to content

Commit 0824604

Browse files
committed
Expose Type::PointerSuffix
1 parent 3375478 commit 0824604

File tree

4 files changed

+163
-2
lines changed

4 files changed

+163
-2
lines changed

binaryninjaapi.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8595,6 +8595,10 @@ namespace BinaryNinja {
85958595
uint64_t GetElementCount() const;
85968596
uint64_t GetOffset() const;
85978597

8598+
std::set<BNPointerSuffix> GetPointerSuffix() const;
8599+
std::string GetPointerSuffixString() const;
8600+
std::vector<InstructionTextToken> GetPointerSuffixTokens(uint8_t baseConfidence = BN_FULL_CONFIDENCE) const;
8601+
85988602
std::string GetString(Platform* platform = nullptr, BNTokenEscapingType escaping = NoTokenEscapingType) const;
85998603
std::string GetTypeAndName(const QualifiedName& name, BNTokenEscapingType escaping = NoTokenEscapingType) const;
86008604
std::string GetStringBeforeName(Platform* platform = nullptr, BNTokenEscapingType escaping = NoTokenEscapingType) const;
@@ -8986,6 +8990,13 @@ namespace BinaryNinja {
89868990
TypeBuilder& SetPure(const Confidence<bool>& pure);
89878991
TypeBuilder& SetParameters(const std::vector<FunctionParameter>& params);
89888992

8993+
std::set<BNPointerSuffix> GetPointerSuffix() const;
8994+
std::string GetPointerSuffixString() const;
8995+
std::vector<InstructionTextToken> GetPointerSuffixTokens(uint8_t baseConfidence = BN_FULL_CONFIDENCE) const;
8996+
8997+
TypeBuilder& AddPointerSuffix(BNPointerSuffix ps);
8998+
TypeBuilder& SetPointerSuffix(const std::set<BNPointerSuffix>& suffix);
8999+
89899000
std::string GetString(Platform* platform = nullptr) const;
89909001
std::string GetTypeAndName(const QualifiedName& name) const;
89919002
std::string GetStringBeforeName(Platform* platform = nullptr) const;

binaryninjacore.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ extern "C"
802802
UnalignedSuffix,
803803
RestrictSuffix,
804804
ReferenceSuffix,
805-
LvalueSuffix
805+
LvalueSuffix,
806806
} BNPointerSuffix;
807807

808808
// Caution: these enumeration values are used a lookups into the static NameTypeStrings in the core
@@ -5871,6 +5871,10 @@ extern "C"
58715871
BINARYNINJACOREAPI char* BNGetTypeAlternateName(BNType* type);
58725872
BINARYNINJACOREAPI uint32_t BNTypeGetSystemCallNumber(BNType* type);
58735873
BINARYNINJACOREAPI bool BNTypeIsSystemCall(BNType* type);
5874+
BINARYNINJACOREAPI BNPointerSuffix* BNGetTypePointerSuffix(BNType* type, size_t* count);
5875+
BINARYNINJACOREAPI char* BNGetTypePointerSuffixString(BNType* type);
5876+
BINARYNINJACOREAPI BNInstructionTextToken* BNGetTypePointerSuffixTokens(BNType* type, uint8_t baseConfidence, size_t* count);
5877+
BINARYNINJACOREAPI void BNFreePointerSuffixList(BNPointerSuffix* suffix, size_t count);
58745878

58755879
BINARYNINJACOREAPI char* BNGetTypeString(BNType* type, BNPlatform* platform, BNTokenEscapingType escaping);
58765880
BINARYNINJACOREAPI char* BNGetTypeStringBeforeName(BNType* type, BNPlatform* platform, BNTokenEscapingType escaping);
@@ -5933,6 +5937,11 @@ extern "C"
59335937
BINARYNINJACOREAPI bool BNTypeBuilderIsSystemCall(BNTypeBuilder* type);
59345938
BINARYNINJACOREAPI uint32_t BNTypeBuilderGetSystemCallNumber(BNTypeBuilder* type);
59355939
BINARYNINJACOREAPI void BNTypeBuilderSetStackAdjustment(BNTypeBuilder* type, BNOffsetWithConfidence* adjust);
5940+
BINARYNINJACOREAPI BNPointerSuffix* BNGetTypeBuilderPointerSuffix(BNTypeBuilder* type, size_t* count);
5941+
BINARYNINJACOREAPI char* BNGetTypeBuilderPointerSuffixString(BNTypeBuilder* type);
5942+
BINARYNINJACOREAPI BNInstructionTextToken* BNGetTypeBuilderPointerSuffixTokens(BNTypeBuilder* type, uint8_t baseConfidence, size_t* count);
5943+
BINARYNINJACOREAPI void BNAddTypeBuilderPointerSuffix(BNTypeBuilder* type, BNPointerSuffix ps);
5944+
BINARYNINJACOREAPI void BNSetTypeBuilderPointerSuffix(BNTypeBuilder* type, BNPointerSuffix* suffix, size_t count);
59365945

59375946
BINARYNINJACOREAPI char* BNGetTypeBuilderString(BNTypeBuilder* type, BNPlatform* platform);
59385947
BINARYNINJACOREAPI char* BNGetTypeBuilderStringBeforeName(BNTypeBuilder* type, BNPlatform* platform);

python/types.py

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from .enums import (
3030
StructureVariant, SymbolType, SymbolBinding, TypeClass, NamedTypeReferenceClass, ReferenceType, VariableSourceType,
3131
TypeReferenceType, MemberAccess, MemberScope, TypeDefinitionLineType, TokenEscapingType,
32-
NameType
32+
NameType, PointerSuffix
3333
)
3434
from . import callingconvention
3535
from . import function as _function
@@ -963,6 +963,41 @@ def origin(self) -> Optional[Tuple['QualifiedName', int]]:
963963
def origin(self, origin: 'NamedTypeReferenceType'):
964964
core.BNSetTypeBuilderNamedTypeReference(self._handle, origin.ntr_handle)
965965

966+
@property
967+
def pointer_suffix(self) -> List[PointerSuffix]:
968+
count = ctypes.c_size_t(0)
969+
suffix = core.BNGetTypeBuilderPointerSuffix(self._handle, count)
970+
assert suffix is not None, "core.BNGetTypeBuilderPointerSuffix returned None"
971+
try:
972+
result = []
973+
for i in range(count.value):
974+
result.append(PointerSuffix(suffix[i]))
975+
return result
976+
finally:
977+
core.BNFreePointerSuffixList(suffix, count)
978+
979+
@pointer_suffix.setter
980+
def pointer_suffix(self, value: List[PointerSuffix]):
981+
suffix = (core.PointerSuffixEnum * len(value))()
982+
for i, s in enumerate(value):
983+
suffix[i] = core.PointerSuffixEnum(s)
984+
core.BNSetTypeBuilderPointerSuffix(self._handle, suffix, len(value))
985+
986+
def add_pointer_suffix(self, suffix: PointerSuffix):
987+
core.BNAddTypeBuilderPointerSuffix(self._handle, suffix)
988+
989+
@property
990+
def pointer_suffix_string(self) -> str:
991+
return core.BNGetTypeBuilderPointerSuffixString(self._handle)
992+
993+
def get_pointer_suffix_tokens(self, base_confidence: int = core.max_confidence) -> List['_function.InstructionTextToken']:
994+
count = ctypes.c_ulonglong()
995+
tokens = core.BNGetTypeBuilderPointerSuffixTokens(self._handle, base_confidence, count)
996+
assert tokens is not None, "core.BNGetTypeBuilderPointerSuffixTokens returned None"
997+
result = _function.InstructionTextToken._from_core_struct(tokens, count.value)
998+
core.BNFreeInstructionText(tokens, count.value)
999+
return result
1000+
9661001

9671002
class ArrayBuilder(TypeBuilder):
9681003
@classmethod
@@ -2794,6 +2829,34 @@ def target(self) -> Type:
27942829
def children(self) -> List[Type]:
27952830
return [self.target]
27962831

2832+
@property
2833+
def pointer_suffix(self) -> List[PointerSuffix]:
2834+
count = ctypes.c_size_t(0)
2835+
suffix = core.BNGetTypePointerSuffix(self.handle, count)
2836+
assert suffix is not None, "core.BNGetTypePointerSuffix returned None"
2837+
try:
2838+
result = []
2839+
for i in range(count.value):
2840+
result.append(suffix[i])
2841+
return result
2842+
finally:
2843+
core.BNFreePointerSuffixList(suffix, count)
2844+
2845+
@property
2846+
def pointer_suffix_string(self) -> str:
2847+
return core.BNGetTypePointerSuffixString(self.handle)
2848+
2849+
def get_pointer_suffix_tokens(self, base_confidence: int = core.max_confidence) -> List['_function.InstructionTextToken']:
2850+
count = ctypes.c_ulonglong()
2851+
platform = None
2852+
if self._platform is not None:
2853+
platform = self._platform.handle
2854+
tokens = core.BNGetTypePointerSuffixTokens(self._handle, base_confidence, count)
2855+
assert tokens is not None, "core.BNGetTypePointerSuffixTokens returned None"
2856+
result = _function.InstructionTextToken._from_core_struct(tokens, count.value)
2857+
core.BNFreeInstructionText(tokens, count.value)
2858+
return result
2859+
27972860

27982861
class ArrayType(Type):
27992862
@classmethod

type.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,36 @@ Confidence<int64_t> Type::GetStackAdjustment() const
693693
}
694694

695695

696+
std::set<BNPointerSuffix> Type::GetPointerSuffix() const
697+
{
698+
size_t count = 0;
699+
BNPointerSuffix* suffix = BNGetTypePointerSuffix(m_object, &count);
700+
std::set<BNPointerSuffix> result(suffix, suffix + count);
701+
BNFreePointerSuffixList(suffix, count);
702+
return result;
703+
704+
}
705+
706+
707+
std::string Type::GetPointerSuffixString() const
708+
{
709+
char* string = BNGetTypePointerSuffixString(m_object);
710+
std::string result(string);
711+
BNFreeString(string);
712+
return result;
713+
}
714+
715+
716+
std::vector<InstructionTextToken> Type::GetPointerSuffixTokens(uint8_t baseConfidence) const
717+
{
718+
size_t count = 0;
719+
BNInstructionTextToken* tokens = BNGetTypePointerSuffixTokens(m_object, baseConfidence, &count);
720+
std::vector<InstructionTextToken> result = InstructionTextToken::ConvertInstructionTextTokenList(tokens, count);
721+
BNFreeInstructionText(tokens, count);
722+
return result;
723+
}
724+
725+
696726
string Type::GetString(Platform* platform, BNTokenEscapingType escaping) const
697727
{
698728
char* str = BNGetTypeString(m_object, platform ? platform->GetObject() : nullptr, escaping);
@@ -1936,6 +1966,54 @@ TypeBuilder& TypeBuilder::SetParameters(const std::vector<FunctionParameter>& pa
19361966
}
19371967

19381968

1969+
std::set<BNPointerSuffix> TypeBuilder::GetPointerSuffix() const
1970+
{
1971+
size_t count = 0;
1972+
BNPointerSuffix* suffix = BNGetTypeBuilderPointerSuffix(m_object, &count);
1973+
std::set<BNPointerSuffix> result(suffix, suffix + count);
1974+
BNFreePointerSuffixList(suffix, count);
1975+
return result;
1976+
}
1977+
1978+
1979+
std::string TypeBuilder::GetPointerSuffixString() const
1980+
{
1981+
char* string = BNGetTypeBuilderPointerSuffixString(m_object);
1982+
std::string result(string);
1983+
BNFreeString(string);
1984+
return result;
1985+
}
1986+
1987+
1988+
std::vector<InstructionTextToken> TypeBuilder::GetPointerSuffixTokens(uint8_t baseConfidence) const
1989+
{
1990+
size_t count = 0;
1991+
BNInstructionTextToken* tokens = BNGetTypeBuilderPointerSuffixTokens(m_object, baseConfidence, &count);
1992+
std::vector<InstructionTextToken> result = InstructionTextToken::ConvertInstructionTextTokenList(tokens, count);
1993+
BNFreeInstructionText(tokens, count);
1994+
return result;
1995+
}
1996+
1997+
1998+
TypeBuilder& TypeBuilder::AddPointerSuffix(BNPointerSuffix ps)
1999+
{
2000+
BNAddTypeBuilderPointerSuffix(m_object, ps);
2001+
return *this;
2002+
}
2003+
2004+
2005+
TypeBuilder& TypeBuilder::SetPointerSuffix(const std::set<BNPointerSuffix>& suffix)
2006+
{
2007+
std::vector<BNPointerSuffix> apiSuffix;
2008+
for (auto& s: suffix)
2009+
{
2010+
apiSuffix.push_back(s);
2011+
}
2012+
BNSetTypeBuilderPointerSuffix(m_object, apiSuffix.data(), apiSuffix.size());
2013+
return *this;
2014+
}
2015+
2016+
19392017
QualifiedName TypeBuilder::GetTypeName() const
19402018
{
19412019
BNQualifiedName name = BNTypeBuilderGetTypeName(m_object);

0 commit comments

Comments
 (0)