Skip to content

Commit

Permalink
MR changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mari-mari committed Sep 7, 2023
1 parent 4c91671 commit 4938c8d
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 5 deletions.
3 changes: 2 additions & 1 deletion decompiler/frontend/binaryninja/handlers/assignments.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,15 @@ 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):
parent_type = self._lifter.lift(parent.dest.type)
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,
Expand Down
3 changes: 1 addition & 2 deletions decompiler/frontend/binaryninja/handlers/unary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions decompiler/structures/pseudo/complextypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
2 changes: 0 additions & 2 deletions tests/structures/pseudo/test_complextypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 4938c8d

Please sign in to comment.