From 2bd0591e0d6b8756f2764aa9f5be60a69e093ee0 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 24 Jun 2024 13:40:05 +0000 Subject: [PATCH] [Lifter] Represent BNinja void correctly (#415) * Create draft PR for #405 * Add: uchar8 for void * Remove void* hack --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Spartak Ehrlich Co-authored-by: Manuel Blatt <45859907+blattm@users.noreply.github.com> --- decompiler/backend/cexpressiongenerator.py | 3 +++ decompiler/backend/variabledeclarations.py | 2 +- decompiler/frontend/binaryninja/handlers/constants.py | 3 --- decompiler/frontend/binaryninja/handlers/globals.py | 4 +++- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/decompiler/backend/cexpressiongenerator.py b/decompiler/backend/cexpressiongenerator.py index 77cfbe77e..892f52212 100644 --- a/decompiler/backend/cexpressiongenerator.py +++ b/decompiler/backend/cexpressiongenerator.py @@ -199,6 +199,9 @@ def visit_constant_composition(self, expr: expressions.ConstantComposition): case CustomType(text="wchar16") | CustomType(text="wchar32"): val = "".join([x.value for x in expr.value]) return f'L"{val}"' if len(val) <= MAX_GLOBAL_INIT_LENGTH else f'L"{val[:MAX_GLOBAL_INIT_LENGTH]}..."' + case Integer(size=8, signed=False): + val = "".join([f"\\x{x.value:02X}" for x in expr.value][:MAX_GLOBAL_INIT_LENGTH]) + return f'"{val}"' if len(val) <= MAX_GLOBAL_INIT_LENGTH else f'"{val[:MAX_GLOBAL_INIT_LENGTH]}..."' case Integer(8): val = "".join([x.value for x in expr.value][:MAX_GLOBAL_INIT_LENGTH]) return f'"{val}"' if len(val) <= MAX_GLOBAL_INIT_LENGTH else f'"{val[:MAX_GLOBAL_INIT_LENGTH]}..."' diff --git a/decompiler/backend/variabledeclarations.py b/decompiler/backend/variabledeclarations.py index 27e2152cd..55639f48b 100644 --- a/decompiler/backend/variabledeclarations.py +++ b/decompiler/backend/variabledeclarations.py @@ -63,7 +63,7 @@ def _generate_definitions(global_variables: set[GlobalVariable]) -> Iterator[str match variable.type: case ArrayType(): br, bl = "", "" - if not variable.type.type in [Integer.char(), CustomType.wchar16(), CustomType.wchar32()]: + if not variable.type.type in [Integer.char(), Integer.uint8_t(), CustomType.wchar16(), CustomType.wchar32()]: br, bl = "{", "}" yield f"{base}{variable.type.type} {variable.name}[{hex(variable.type.elements)}] = {br}{CExpressionGenerator().visit(variable.initial_value)}{bl};" case _: diff --git a/decompiler/frontend/binaryninja/handlers/constants.py b/decompiler/frontend/binaryninja/handlers/constants.py index d351a2a2e..e7e5dffdd 100644 --- a/decompiler/frontend/binaryninja/handlers/constants.py +++ b/decompiler/frontend/binaryninja/handlers/constants.py @@ -72,9 +72,6 @@ def lift_constant_pointer(self, pointer: mediumlevelil.MediumLevelILConstPtr, ** if isinstance(res, Constant): # BNinja Error case handling return res - if isinstance(res.type, Pointer) and res.type.type == CustomType.void(): - return res - if isinstance(pointer, mediumlevelil.MediumLevelILImport): # Temp fix for '&' return res diff --git a/decompiler/frontend/binaryninja/handlers/globals.py b/decompiler/frontend/binaryninja/handlers/globals.py index 388cc061e..076610109 100644 --- a/decompiler/frontend/binaryninja/handlers/globals.py +++ b/decompiler/frontend/binaryninja/handlers/globals.py @@ -247,7 +247,9 @@ def _get_unknown_value(self, variable: DataVariable): type = PseudoArrayType(self._lifter.lift(data[1]), len(data[0])) data = ConstantComposition([Constant(x, type.type) for x in data[0]], type) else: - data, type = get_raw_bytes(variable.address, self._view), Pointer(CustomType.void(), self._view.address_size * BYTE_SIZE) + rbytes = get_raw_bytes(variable.address, self._view) + type = PseudoArrayType(Integer.uint8_t(), len(rbytes)) + data = ConstantComposition([Constant(b, type.type) for b in rbytes], type) return data, type def _get_unknown_pointer_value(self, variable: DataVariable, callers: list[int] = None):