Skip to content

Commit

Permalink
Use TypeID for serializing enum types, replace set_int_enum_display_t…
Browse files Browse the repository at this point in the history
…ypeid with set_int_display_type, add get_int_display_type_and_typeid, bump ABI
  • Loading branch information
0cyn committed Feb 10, 2024
1 parent 5454781 commit ddce31c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 21 deletions.
8 changes: 3 additions & 5 deletions binaryninjaapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -10447,13 +10447,11 @@ namespace BinaryNinja {

BNIntegerDisplayType GetIntegerConstantDisplayType(
Architecture* arch, uint64_t instrAddr, uint64_t value, size_t operand);
void SetIntegerConstantDisplayType(
Architecture* arch, uint64_t instrAddr, uint64_t value, size_t operand, BNIntegerDisplayType type);

Ref<Type> GetIntegerConstantDisplayTypeEnumType(
Architecture* arch, uint64_t instrAddr, uint64_t value, size_t operand);
void SetIntegerConstantDisplayTypeEnumType(
Architecture* arch, uint64_t instrAddr, uint64_t value, size_t operand, Ref<Type> type);
void SetIntegerConstantDisplayType(
Architecture* arch, uint64_t instrAddr, uint64_t value, size_t operand, BNIntegerDisplayType type, Ref<Type> enumType = nullptr);
std::pair<BNIntegerDisplayType, Ref<Type>> GetIntegerConstantDisplayTypeAndEnumType(Architecture* arch, uint64_t instrAddr, uint64_t value, size_t operand);

BNHighlightColor GetInstructionHighlight(Architecture* arch, uint64_t addr);
void SetAutoInstructionHighlight(Architecture* arch, uint64_t addr, BNHighlightColor color);
Expand Down
10 changes: 4 additions & 6 deletions binaryninjacore.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
// Current ABI version for linking to the core. This is incremented any time
// there are changes to the API that affect linking, including new functions,
// new types, or modifications to existing functions or types.
#define BN_CURRENT_CORE_ABI_VERSION 50
#define BN_CURRENT_CORE_ABI_VERSION 51

// Minimum ABI version that is supported for loading of plugins. Plugins that
// are linked to an ABI version less than this will not be able to load and
// will require rebuilding. The minimum version is increased when there are
// incompatible changes that break binary compatibility, such as changes to
// existing types or functions.
#define BN_MINIMUM_CORE_ABI_VERSION 50
#define BN_MINIMUM_CORE_ABI_VERSION 51

#ifdef __GNUC__
#ifdef BINARYNINJACORE_LIBRARY
Expand Down Expand Up @@ -4474,11 +4474,9 @@ extern "C"
BINARYNINJACOREAPI BNIntegerDisplayType BNGetIntegerConstantDisplayType(
BNFunction* func, BNArchitecture* arch, uint64_t instrAddr, uint64_t value, size_t operand);
BINARYNINJACOREAPI void BNSetIntegerConstantDisplayType(BNFunction* func, BNArchitecture* arch, uint64_t instrAddr,
uint64_t value, size_t operand, BNIntegerDisplayType type);
BINARYNINJACOREAPI BNType* BNGetIntegerConstantDisplayTypeEnumerationType(
uint64_t value, size_t operand, BNIntegerDisplayType type, const char* typeID);
BINARYNINJACOREAPI char* BNGetIntegerConstantDisplayTypeEnumerationType(
BNFunction* func, BNArchitecture* arch, uint64_t instrAddr, uint64_t value, size_t operand);
BINARYNINJACOREAPI void BNSetIntegerConstantDisplayTypeEnumerationType(
BNFunction* func, BNArchitecture* arch, uint64_t instrAddr, uint64_t value, size_t operand, BNType* type);

BINARYNINJACOREAPI bool BNIsFunctionTooLarge(BNFunction* func);
BINARYNINJACOREAPI bool BNIsFunctionAnalysisSkipped(BNFunction* func);
Expand Down
27 changes: 19 additions & 8 deletions function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1865,26 +1865,37 @@ BNIntegerDisplayType Function::GetIntegerConstantDisplayType(


void Function::SetIntegerConstantDisplayType(
Architecture* arch, uint64_t instrAddr, uint64_t value, size_t operand, BNIntegerDisplayType type)
Architecture* arch, uint64_t instrAddr, uint64_t value, size_t operand, BNIntegerDisplayType type, Ref<Type> enumType)
{
BNSetIntegerConstantDisplayType(m_object, arch->GetObject(), instrAddr, value, operand, type);
if (enumType)
BNSetIntegerConstantDisplayType(m_object, arch->GetObject(), instrAddr, value, operand, type, enumType->GetRegisteredName()->GetTypeId().c_str());
else
BNSetIntegerConstantDisplayType(m_object, arch->GetObject(), instrAddr, value, operand, type, nullptr);
}


Ref<Type> Function::GetIntegerConstantDisplayTypeEnumType(
Architecture* arch, uint64_t instrAddr, uint64_t value, size_t operand)
{
BNType* apiType = BNGetIntegerConstantDisplayTypeEnumerationType(m_object, arch->GetObject(), instrAddr, value, operand);
char* apiType = BNGetIntegerConstantDisplayTypeEnumerationType(m_object, arch->GetObject(), instrAddr, value, operand);
std::string apiTypeStr = std::string(apiType);
if (apiType)
return new Type(apiType);
return nullptr;
BNFreeString(apiType);
Ref<Type> type = GetView()->GetTypeById(apiTypeStr);
return type;
}


void Function::SetIntegerConstantDisplayTypeEnumType(
Architecture* arch, uint64_t instrAddr, uint64_t value, size_t operand, Ref<Type> type)
std::pair<BNIntegerDisplayType, Ref<Type>> Function::GetIntegerConstantDisplayTypeAndEnumType(Architecture* arch,
uint64_t instrAddr, uint64_t value, size_t operand)
{
BNSetIntegerConstantDisplayTypeEnumerationType(m_object, arch->GetObject(), instrAddr, value, operand, type->m_object);
auto displayType = BNGetIntegerConstantDisplayType(m_object, arch->GetObject(), instrAddr, value, operand);
char* apiType = BNGetIntegerConstantDisplayTypeEnumerationType(m_object, arch->GetObject(), instrAddr, value, operand);
std::string apiTypeStr = std::string(apiType);
if (apiType)
BNFreeString(apiType);
Ref<Type> type = GetView()->GetTypeById(apiTypeStr);
return {displayType, type};
}


Expand Down
41 changes: 39 additions & 2 deletions python/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -2702,6 +2702,8 @@ def get_int_display_type(
"""
Get the current text display type for an integer token in the disassembly or IL views

See also see :py:func:`get_int_display_type_and_typeid`

:param int instr_addr: Address of the instruction or IL line containing the token
:param int value: ``value`` field of the InstructionTextToken object for the token, usually the constant displayed
:param int operand: Operand index of the token, defined as the number of OperandSeparatorTokens in the disassembly line before the token
Expand All @@ -2713,9 +2715,27 @@ def get_int_display_type(
core.BNGetIntegerConstantDisplayType(self.handle, arch.handle, instr_addr, value, operand)
)

def get_int_enum_display_typeid(self, instr_addr: int, value: int, operand: int,
arch: Optional['architecture.Architecture'] = None) -> str:
"""
Get the current text display enum type for an integer token in the disassembly or IL views.

See also see :py:func:`get_int_display_type_and_typeid`

:param int instr_addr: Address of the instruction or IL line containing the token
:param int value: ``value`` field of the InstructionTextToken object for the token, usually the constant displayed
:param int operand: Operand index of the token, defined as the number of OperandSeparatorTokens in the disassembly line before the token
:param Architecture arch: (optional) Architecture of the instruction or IL line containing the token
:return: TypeID for the integer token
"""
if arch is None:
arch = self.arch
type_id = core.BNGetIntegerConstantDisplayTypeEnumerationType(self.handle, arch.handle, instr_addr, value, operand)
return type_id

def set_int_display_type(
self, instr_addr: int, value: int, operand: int, display_type: IntegerDisplayType,
arch: Optional['architecture.Architecture'] = None
arch: Optional['architecture.Architecture'] = None, enum_display_typeid = None
) -> None:
"""
Change the text display type for an integer token in the disassembly or IL views
Expand All @@ -2725,12 +2745,29 @@ def set_int_display_type(
:param int operand: Operand index of the token, defined as the number of OperandSeparatorTokens in the disassembly line before the token
:param enums.IntegerDisplayType display_type: Desired display type
:param Architecture arch: (optional) Architecture of the instruction or IL line containing the token
:param str enum_display_typeid: (optional) Whenever passing EnumDisplayType to ``display_type``, passing a type ID here will specify the Enumeration display type. Must be a valid type ID and resolve to an enumeration type.
"""
if arch is None:
arch = self.arch
if isinstance(display_type, str):
display_type = IntegerDisplayType[display_type]
core.BNSetIntegerConstantDisplayType(self.handle, arch.handle, instr_addr, value, operand, display_type)
core.BNSetIntegerConstantDisplayType(self.handle, arch.handle, instr_addr, value, operand, display_type, enum_display_typeid)

def get_int_display_type_and_typeid(self, instr_addr: int, value: int, operand: int,
arch: Optional['architecture.Architecture'] = None) -> (IntegerDisplayType, str):
"""
Get the current text display type for an integer token in the disassembly or IL views

:param int instr_addr: Address of the instruction or IL line containing the token
:param int value: ``value`` field of the InstructionTextToken object for the token, usually the constant displayed
:param int operand: Operand index of the token, defined as the number of OperandSeparatorTokens in the disassembly line before the token
:param Architecture arch: (optional) Architecture of the instruction or IL line containing the token
"""
if arch is None:
arch = self.arch
display_type = core.BNGetIntegerConstantDisplayType(self.handle, arch.handle, instr_addr, value, operand)
type_id = core.BNGetIntegerConstantDisplayTypeEnumerationType(self.handle, arch.handle, instr_addr, value, operand)
return display_type, type_id

def reanalyze(self, update_type: FunctionUpdateType = FunctionUpdateType.UserFunctionUpdate) -> None:
"""
Expand Down

0 comments on commit ddce31c

Please sign in to comment.