diff --git a/decompiler/frontend/binaryninja/handlers/assignments.py b/decompiler/frontend/binaryninja/handlers/assignments.py index fbd1e1d65..85d81038c 100644 --- a/decompiler/frontend/binaryninja/handlers/assignments.py +++ b/decompiler/frontend/binaryninja/handlers/assignments.py @@ -111,6 +111,7 @@ def lift_get_field(self, instruction: mediumlevelil.MediumLevelILVarField, is_al return UnaryOperation(OperationType.cast, [source], vartype=cast_type, contraction=True) def _get_field_as_member_access(self, instruction: mediumlevelil.MediumLevelILVarField, source: Expression, **kwargs) -> MemberAccess: + """Lift MLIL var_field as struct or union member read access.""" if isinstance(source.type, Struct): member_name = source.type.get_member_by_offset(instruction.offset).name elif parent := kwargs.get("parent", None): @@ -118,7 +119,7 @@ def _get_field_as_member_access(self, instruction: mediumlevelil.MediumLevelILVa member_name = source.type.get_member_by_type(parent_type).name else: logging.warning(f"Cannot get member name for instruction {instruction}") - member_name = None + member_name = f"field_{hex(instruction.offset)}" return MemberAccess( offset=instruction.offset, member_name=member_name, diff --git a/decompiler/frontend/binaryninja/handlers/unary.py b/decompiler/frontend/binaryninja/handlers/unary.py index 977d135e0..91e6c8de8 100644 --- a/decompiler/frontend/binaryninja/handlers/unary.py +++ b/decompiler/frontend/binaryninja/handlers/unary.py @@ -98,8 +98,7 @@ def _lift_load_struct(self, instruction: mediumlevelil.MediumLevelILLoadStruct, """Lift a MLIL_LOAD_STRUCT_SSA (struct member access e.g. var#n->x) instruction.""" struct_variable = self._lifter.lift(instruction.src) struct_ptr: Pointer = self._lifter.lift(instruction.src.expr_type) - struct_type: Struct = struct_ptr.type - struct_member_name = struct_type.get_member_by_offset(instruction.offset).name + struct_member_name = struct_ptr.type.get_member_by_offset(instruction.offset).name return MemberAccess(vartype=struct_ptr, operands=[struct_variable], offset=instruction.offset, member_name=struct_member_name) def _lift_ftrunc(self, instruction: mediumlevelil.MediumLevelILFtrunc, **kwargs) -> UnaryOperation: diff --git a/decompiler/structures/pseudo/complextypes.py b/decompiler/structures/pseudo/complextypes.py index 8477125ab..b32528b4a 100644 --- a/decompiler/structures/pseudo/complextypes.py +++ b/decompiler/structures/pseudo/complextypes.py @@ -47,6 +47,7 @@ def __str__(self) -> str: return f"{self.name}" def declaration(self) -> str: + """Return declaration field for the complex type member.""" if isinstance(self.type, Union): return self.type.declaration() return f"{self.type.__str__()} {self.name}" diff --git a/tests/structures/pseudo/test_complextypes.py b/tests/structures/pseudo/test_complextypes.py index f6b3621e7..3bad97d60 100644 --- a/tests/structures/pseudo/test_complextypes.py +++ b/tests/structures/pseudo/test_complextypes.py @@ -18,9 +18,7 @@ def test_declaration(self, book: Struct, record_id: Union): book.add_member( m := ComplexTypeMember(size=64, name="id", offset=12, type=record_id), ) - # TODO if union is defined not within the struct itself? result = f"struct Book {{\n\tchar * title;\n\tint num_pages;\n\tchar * author;\n\t{m.declaration()};\n}}" - # TODO nest enum assert book.declaration() == result def test_str(self, book: Struct):