Skip to content

Commit

Permalink
Fix: EnumerationType support in lifter
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoQuix committed Oct 1, 2024
1 parent 6a4eda6 commit c1780bd
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions decompiler/frontend/binaryninja/handlers/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
ArrayType,
BoolType,
CharType,
EnumerationType,
FloatType,
FunctionType,
IntegerType,
Expand All @@ -21,7 +22,6 @@
from decompiler.frontend.lifter import Handler
from decompiler.structures.pseudo import ArrayType as PseudoArrayType
from decompiler.structures.pseudo import (
ComplexTypeMember,
Constant,
ConstantComposition,
CustomType,
Expand All @@ -31,7 +31,6 @@
Integer,
OperationType,
Pointer,
Struct,
StructConstant,
Symbol,
UnaryOperation,
Expand Down Expand Up @@ -95,6 +94,7 @@ def __init__(self, lifter):
VoidType: self._lift_void_type,
ArrayType: self._lift_array_type,
PointerType: self._lift_pointer_type,
EnumerationType: self._lift_enum_helper,
NamedTypeReferenceType: self._lift_named_type_ref,
StructureType: self._lift_structure_type,
}
Expand Down Expand Up @@ -255,6 +255,8 @@ def _lift_pointer_type(
init_value=init_value,
ssa_label=parent.ssa_memory_version if parent else 0,
)



def _lift_named_type_ref(self, variable: DataVariable, parent: Optional[MediumLevelILInstruction] = None, **_):
"""Lift a named custom type (Enum, Structs)"""
Expand All @@ -264,17 +266,7 @@ def _lift_named_type_ref(self, variable: DataVariable, parent: Optional[MediumLe
return self._lift_struct_helper(variable, parent, struct_type)

case NamedTypeReferenceClass.EnumNamedTypeClass:
try:
value = Constant(variable.value, self._lifter.lift(variable.type))
return self._build_global_variable(
variable.name,
value.type,
variable.address,
value,
parent.ssa_memory_version if parent else 0,
)
except Exception:
return Constant("Unknown value", self._lifter.lift(variable.type)) # BNinja error
return self._lift_enum_helper(variable, parent)
case _:
raise NotImplementedError(f"No handler for '{variable.type.named_type_class}' in lifter")

Expand All @@ -298,6 +290,20 @@ def _lift_struct_helper(self, variable, parent, struct_type):
return self._build_global_variable(
variable.name, s_type, variable.address, StructConstant(values, s_type), parent.ssa_memory_version if parent else 0
)

def _lift_enum_helper(self, variable: DataVariable, parent: Optional[MediumLevelILInstruction] = None, **_):
"""Lift a Enum type from Binary Ninja. Try/Catch Block because of an upstream problem with PDB on PE files"""
try:
value = Constant(variable.value, self._lifter.lift(variable.type))
return self._build_global_variable(
variable.name,
value.type,
variable.address,
value,
parent.ssa_memory_version if parent else 0,
)
except Exception:
return Constant("Unknown value", self._lifter.lift(variable.type)) # BNinja error

def _get_unknown_value(self, variable: DataVariable):
"""Return string or bytes at dv.address(!) (dv.type must be void)"""
Expand Down

0 comments on commit c1780bd

Please sign in to comment.