From 54a3ed81837a6a04868073ca50dee0fdde0fcb2b Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 22 Nov 2024 14:31:27 +0100 Subject: [PATCH 1/3] [Lifter] Complex Type Lifting (#445) * Create draft PR for #444 * Switch Error fix and already lifted --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Spartak Ehrlich Co-authored-by: Niklas Bergmann <97505753+0x6e62@users.noreply.github.com> --- decompiler/frontend/binaryninja/handlers/globals.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/decompiler/frontend/binaryninja/handlers/globals.py b/decompiler/frontend/binaryninja/handlers/globals.py index 94fb77d0..344e0625 100644 --- a/decompiler/frontend/binaryninja/handlers/globals.py +++ b/decompiler/frontend/binaryninja/handlers/globals.py @@ -158,6 +158,10 @@ def lift_global_variable( if not self._view: self._view = view + # BNinja error cases: nullptr/small numbers (0, -12...) + if not addr_in_section(view, variable.address): + return Constant(variable.address, vartype=Integer(view.address_size * BYTE_SIZE, False)) + # If addr was already lifted: Return lifted GlobalVariable with updated SSA variable_identifier = (variable.address, self._lifter.lift(variable.type)) if variable_identifier in self._lifted_globals.keys(): @@ -167,10 +171,6 @@ def lift_global_variable( else self._lifted_globals[variable_identifier] ) - # BNinja error cases: nullptr/small numbers (0, -12...) - if not addr_in_section(view, variable.address): - return Constant(variable.address, vartype=Integer(view.address_size * BYTE_SIZE, False)) - # Check if there is a cycle between GlobalVariables initial_value if callers and variable.address in callers: return ( From 863764472bd86688054ced0b9dc23ce2d56ae969 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 22 Nov 2024 16:48:00 +0100 Subject: [PATCH 2/3] [TypeError@globals.py:141] TypeError: Type violation: 'unknown type' (#443) * Create draft PR for #442 * Add: Ptr in section check --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Spartak Ehrlich Co-authored-by: Niklas Bergmann <97505753+0x6e62@users.noreply.github.com> --- .../frontend/binaryninja/handlers/globals.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/decompiler/frontend/binaryninja/handlers/globals.py b/decompiler/frontend/binaryninja/handlers/globals.py index 344e0625..ed959d78 100644 --- a/decompiler/frontend/binaryninja/handlers/globals.py +++ b/decompiler/frontend/binaryninja/handlers/globals.py @@ -233,6 +233,8 @@ def _lift_pointer_type( 1. Function pointer: If Bninja already knows it's a function pointer. 2. Type pointer: As normal type pointer (there _should_ be a datavariable at the pointers dest.) 3. Void pointer: Try to extract a datavariable (recover type of void* directly), string (char*) or raw bytes (void*) at the given address + Caution: A pointer can point at a constant instead of a variable (e.g. stdout/stderr) + => 2/3 catch this error with a value in section check """ match variable.type.target: case FunctionType(): # BNinja knows it's a imported function pointer @@ -241,19 +243,23 @@ def _lift_pointer_type( ) case VoidType(): # BNinja knows it's a pointer pointing at something # Extract the initial_value and type from the location where the pointer is pointing to - init_value, type = self._get_unknown_pointer_value(variable, callers) + init_value, vtype = self._get_unknown_pointer_value(variable, callers) case _: if callers: callers.append(variable.address) else: callers = [variable.address] - init_value, type = ( - self._lifter.lift(self._view.get_data_var_at(variable.value), view=self._view, callers=callers), - self._lifter.lift(variable.type), - ) + + vtype = self._lifter.lift(variable.type) + # BNinja error case: Pointer does not point at variable in view + if not addr_in_section(self._view, variable.value): + init_value = Constant(variable.value, vartype=Integer(self._view.address_size * BYTE_SIZE, False)) + else: + self._lifter.lift(self._view.get_data_var_at(variable.value), view=self._view, callers=callers) + return self._build_global_variable( name=self._lifter.lift(variable.symbol).name if variable.symbol else None, - type=type, + type=vtype, addr=variable.address, init_value=init_value, ssa_label=parent.ssa_memory_version if parent else 0, From 87a29c91b95e8294dc5bb3b07c8ae2a7b949ebca Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 12 Dec 2024 09:37:42 +0100 Subject: [PATCH 3/3] [UnboundLocalError@globals.py:264] UnboundLocalError: local variable 'init_value' referenced before assignment (#449) --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: rihi <19492038+rihi@users.noreply.github.com> --- decompiler/frontend/binaryninja/handlers/globals.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/decompiler/frontend/binaryninja/handlers/globals.py b/decompiler/frontend/binaryninja/handlers/globals.py index ed959d78..57e5eb78 100644 --- a/decompiler/frontend/binaryninja/handlers/globals.py +++ b/decompiler/frontend/binaryninja/handlers/globals.py @@ -255,7 +255,7 @@ def _lift_pointer_type( if not addr_in_section(self._view, variable.value): init_value = Constant(variable.value, vartype=Integer(self._view.address_size * BYTE_SIZE, False)) else: - self._lifter.lift(self._view.get_data_var_at(variable.value), view=self._view, callers=callers) + init_value = self._lifter.lift(self._view.get_data_var_at(variable.value), view=self._view, callers=callers) return self._build_global_variable( name=self._lifter.lift(variable.symbol).name if variable.symbol else None,