Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Types] Create function pointer type #387

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions decompiler/pipeline/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from decompiler.pipeline.preprocessing import (
Coherence,
CompilerIdiomHandling,
FindFunctionPointer,
InsertMissingDefinitions,
MemPhiConverter,
PhiFunctionFixer,
Expand All @@ -35,6 +36,7 @@
MemPhiConverter,
InsertMissingDefinitions,
PhiFunctionFixer,
FindFunctionPointer,
]

POSTPROCESSING_STAGES = [OutOfSsaTranslation, PatternIndependentRestructuring]
Expand Down
1 change: 1 addition & 0 deletions decompiler/pipeline/preprocessing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .coherence import Coherence
from .compiler_idiom_handling import CompilerIdiomHandling
from .find_function_pointer import FindFunctionPointer
from .mem_phi_conversion import MemPhiConverter
from .missing_definitions import InsertMissingDefinitions
from .phi_predecessors import PhiFunctionFixer
Expand Down
27 changes: 27 additions & 0 deletions decompiler/pipeline/preprocessing/find_function_pointer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Module to find and declare function pointer variables"""

from decompiler.pipeline.stage import PipelineStage
from decompiler.structures.pseudo.instructions import Assignment, Variable
from decompiler.structures.pseudo.operations import Call
from decompiler.structures.pseudo.typing import FunctionPointer, Pointer
from decompiler.task import DecompilerTask


class FindFunctionPointer(PipelineStage):
name = "find-function-pointer"

def run(self, task: DecompilerTask):
for block in task.graph:
0x6e62 marked this conversation as resolved.
Show resolved Hide resolved
for expression in block:
if (
isinstance(expression, Assignment)
and isinstance(expression.value, Call)
and isinstance(expression.value.function, Variable)
):
expression.value.function._type = Pointer(
basetype=FunctionPointer(
size=expression.value.function.type.size,
return_type=expression.value.function.type,
parameters=tuple(expression.value.parameters),
),
)
5 changes: 5 additions & 0 deletions decompiler/structures/pseudo/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ def __str__(self) -> str:
return f"{self.return_type}({', '.join(str(x) for x in self.parameters)})"


@dataclass(frozen=True, order=True)
class FunctionPointer(FunctionTypeDef):
pass
0x6e62 marked this conversation as resolved.
Show resolved Hide resolved


class TypeParser:
"""A type parser in charge of creating types."""

Expand Down
2 changes: 1 addition & 1 deletion decompiler/util/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -507,10 +507,10 @@
"default": [
"expression-propagation",
"bit-field-comparison-unrolling",
"type-propagation",
"dead-path-elimination",
"dead-loop-elimination",
"dead-code-elimination",
"type-propagation",
"expression-propagation-memory",
"expression-propagation-function-call",
"expression-simplification-cfg",
Expand Down
6 changes: 5 additions & 1 deletion tests/backend/test_codegenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
OperationType,
UnaryOperation,
)
from decompiler.structures.pseudo.typing import CustomType, Float, Integer, Pointer, Type
from decompiler.structures.pseudo.typing import CustomType, Float, FunctionPointer, Integer, Pointer, Type
from decompiler.task import DecompilerTask
from decompiler.util.options import Options

Expand Down Expand Up @@ -80,6 +80,8 @@ def logic_cond(name: str, context) -> LogicCondition:
var_p = Variable("p", Pointer(int32))
var_fun_p = Variable("p", Pointer(FunctionTypeDef(0, int32, (int32,))))
var_fun_p0 = Variable("p0", Pointer(FunctionTypeDef(0, int32, (int32,))))
var_fun_ptr = Variable("ptr", Pointer(FunctionPointer(0, int32, (int32,))))
var_fun_ptr0 = Variable("ptr0", Pointer(FunctionPointer(0, int32, (int32,))))

const_0 = Constant(0, int32)
const_1 = Constant(1, int32)
Expand Down Expand Up @@ -1286,6 +1288,8 @@ class TestLocalDeclarationGenerator:
(1, [var_x.copy(), var_y.copy(), var_p.copy()], "int x;\nint y;\nint * p;"),
(1, [var_x.copy(), var_y.copy(), var_fun_p.copy()], "int x;\nint y;\nint (* p)(int);"),
(2, [var_x.copy(), var_y.copy(), var_fun_p.copy(), var_fun_p0.copy()], "int x, y;\nint (* p)(int), (* p0)(int);"),
(1, [var_x.copy(), var_y.copy(), var_fun_ptr.copy()], "int x;\nint y;\nint (* ptr)(int);"),
(2, [var_x.copy(), var_y.copy(), var_fun_ptr.copy(), var_fun_ptr0.copy()], "int x, y;\nint (* ptr)(int), (* ptr0)(int);"),
],
)
def test_variable_declaration(self, vars_per_line: int, variables: List[Variable], expected: str):
Expand Down
101 changes: 101 additions & 0 deletions tests/pipeline/preprocessing/test_find_function_pointer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
from decompiler.pipeline.preprocessing import FindFunctionPointer
from decompiler.structures.graphs.cfg import BasicBlock, ControlFlowGraph, FalseCase, TrueCase, UnconditionalEdge
from decompiler.structures.pseudo import (
Assignment,
Call,
Condition,
Constant,
ImportedFunctionSymbol,
Integer,
ListOperation,
OperationType,
Variable,
)
from decompiler.structures.pseudo.instructions import Branch, Return
from decompiler.structures.pseudo.typing import FunctionPointer, Pointer
from decompiler.task import DecompilerTask


def test_set_variable_to_function_pointer():
"""
Test the change of a variable type to FunctionPointer if there is a call on this variable.

a = 0x0804c020
b = 1
if (a == 0)
return b
else
a()
"""
cfg = ControlFlowGraph()
var_a = Variable("a", Integer.int32_t())
var_b = Variable("b", Integer.int32_t())
cfg.add_nodes_from(
[
n0 := BasicBlock(0, instructions=[Assignment(var_a, Constant(0x0804C020)), Assignment(var_b, Constant(1))]),
n1 := BasicBlock(1, instructions=[Branch(Condition(OperationType.equal, [var_a, Constant(0)]))]),
n2 := BasicBlock(2, instructions=[Return([var_b])]),
n3 := BasicBlock(3, instructions=[Assignment(ListOperation([]), Call(var_a, []))]),
]
)
cfg.add_edges_from([UnconditionalEdge(n0, n1), TrueCase(n1, n2), FalseCase(n1, n3)])
FindFunctionPointer().run(DecompilerTask("test", cfg))
assert var_a.type == Pointer(FunctionPointer(32, Integer.int32_t(), ()))


def test_set_variable_to_function_pointer_with_parameters():
"""
Test the change of a variable type to FunctionPointer if there is a call on this variable with parameters.

a = 0x0804c020
b = 1
if (a == 0)
return b
else
a(c, d)
"""
cfg = ControlFlowGraph()
var_a = Variable("a", Integer.int32_t())
var_b = Variable("b", Integer.int32_t())
var_c = Variable("c", Integer.int32_t())
var_d = Variable("d", Integer.int32_t())
cfg.add_nodes_from(
[
n0 := BasicBlock(0, instructions=[Assignment(var_a, Constant(0x0804C020)), Assignment(var_b, Constant(1))]),
n1 := BasicBlock(1, instructions=[Branch(Condition(OperationType.equal, [var_a, Constant(0)]))]),
n2 := BasicBlock(2, instructions=[Return([var_b])]),
n3 := BasicBlock(3, instructions=[Assignment(ListOperation([]), Call(var_a, [var_c, var_d]))]),
]
)
cfg.add_edges_from([UnconditionalEdge(n0, n1), TrueCase(n1, n2), FalseCase(n1, n3)])
FindFunctionPointer().run(DecompilerTask("test", cfg))
assert var_a.type == Pointer(FunctionPointer(32, Integer.int32_t(), (var_c, var_d)))


def test_skip_set_variable_to_function_pointer():
"""
Test the skip of a change of a variable type to FunctionPointer if there is a call without a variable.

a = 0x0804c020
b = 1
if (a == 0)
return b
else
printf("%d\n", a)
"""
cfg = ControlFlowGraph()
var_a = Variable("a", Integer.int32_t())
var_b = Variable("b", Integer.int32_t())
cfg.add_nodes_from(
[
n0 := BasicBlock(0, instructions=[Assignment(var_a, Constant(0x0804C020)), Assignment(var_b, Constant(1))]),
n1 := BasicBlock(1, instructions=[Branch(Condition(OperationType.equal, [var_a, Constant(0)]))]),
n2 := BasicBlock(2, instructions=[Return([var_b])]),
n3 := BasicBlock(
3, instructions=[Assignment(ListOperation([]), Call(ImportedFunctionSymbol("printf", 0), [Constant("%d\n"), var_a]))]
),
]
)
cfg.add_edges_from([UnconditionalEdge(n0, n1), TrueCase(n1, n2), FalseCase(n1, n3)])
FindFunctionPointer().run(DecompilerTask("test", cfg))
assert not any(isinstance(variable.type, Pointer) for variable in cfg.get_variables())
Loading