pcode-graph 2.0.2__py3-none-any.whl → 3.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pcode_graph/cli.py +6 -4
- pcode_graph/lief_importer.py +11 -4
- pcode_graph/maker.py +50 -14
- pcode_graph/memory.py +271 -0
- {pcode_graph-2.0.2.dist-info → pcode_graph-3.0.0.dist-info}/METADATA +7 -1
- {pcode_graph-2.0.2.dist-info → pcode_graph-3.0.0.dist-info}/RECORD +9 -8
- {pcode_graph-2.0.2.dist-info → pcode_graph-3.0.0.dist-info}/WHEEL +1 -1
- {pcode_graph-2.0.2.dist-info → pcode_graph-3.0.0.dist-info}/entry_points.txt +0 -0
- {pcode_graph-2.0.2.dist-info → pcode_graph-3.0.0.dist-info}/licenses/LICENSE +0 -0
pcode_graph/cli.py
CHANGED
|
@@ -21,6 +21,7 @@ Options:
|
|
|
21
21
|
-b, --base-address <int> Consider the given assembly starts at given address [Default: 0].
|
|
22
22
|
-D, --dataflow-only Only extract dataflow graph (and no control flow), this implies -F.
|
|
23
23
|
-F, --flat-cfg Disable basic block nodes in the graph.
|
|
24
|
+
-M, --no-memory-analysis Disable stack memory analysis and keep all LOADs in the graph.
|
|
24
25
|
-o, --output <path> Where to write the output.
|
|
25
26
|
-h, --help This help.
|
|
26
27
|
-d, --debug Debug mode: do no catch exceptions.
|
|
@@ -43,7 +44,7 @@ from pcode_graph.lief_importer import (
|
|
|
43
44
|
iter_functions,
|
|
44
45
|
get_architecture,
|
|
45
46
|
lookup_chunk,
|
|
46
|
-
|
|
47
|
+
try_parse_binary,
|
|
47
48
|
)
|
|
48
49
|
from pcode_graph.log import setup_logger
|
|
49
50
|
from pcode_graph.translator import Translator
|
|
@@ -64,10 +65,10 @@ def main():
|
|
|
64
65
|
setup_logger()
|
|
65
66
|
|
|
66
67
|
input_path = Path(args["<input_path>"])
|
|
67
|
-
assert input_path.exists()
|
|
68
|
+
assert input_path.exists(), f"{input_path} does not exist"
|
|
68
69
|
|
|
69
70
|
assert input_path.stat().st_size > 0, "Input file is empty"
|
|
70
|
-
binary: Binary | None =
|
|
71
|
+
binary: Binary | None = try_parse_binary(input_path)
|
|
71
72
|
|
|
72
73
|
if args["--arch"] != "auto":
|
|
73
74
|
arch: Arch = Arch(args["--arch"])
|
|
@@ -165,6 +166,7 @@ def main():
|
|
|
165
166
|
flags = MakerFlags(
|
|
166
167
|
build_cfg=not args["--dataflow-only"],
|
|
167
168
|
basic_blocks=not args["--flat-cfg"],
|
|
169
|
+
memory_analysis=not args["--no-memory-analysis"],
|
|
168
170
|
)
|
|
169
171
|
cdg = make_graph(rpl, flags)
|
|
170
172
|
|
|
@@ -190,5 +192,5 @@ def main():
|
|
|
190
192
|
except Exception as e:
|
|
191
193
|
if args["--debug"]:
|
|
192
194
|
raise
|
|
193
|
-
print(str(e), file=sys.stderr)
|
|
195
|
+
print(str(e) or type(e).__name__, file=sys.stderr)
|
|
194
196
|
sys.exit(1)
|
pcode_graph/lief_importer.py
CHANGED
|
@@ -28,12 +28,19 @@ class FunctionOps(ChunkOps):
|
|
|
28
28
|
function_name: str
|
|
29
29
|
|
|
30
30
|
|
|
31
|
-
def
|
|
31
|
+
def try_parse_binary(binary_path: Path) -> lief.Binary | None:
|
|
32
|
+
"""Parses the given file, or returns None if LIEF does not recognize its format."""
|
|
33
|
+
|
|
32
34
|
assert binary_path.is_file(), f"{binary_path} is not a file"
|
|
33
35
|
logger.debug(f"Parse {binary_path}")
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
binary = lief.parse(binary_path.as_posix())
|
|
37
|
+
return binary if isinstance(binary, lief.Binary) else None
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def parse_binary(binary_path: Path) -> lief.Binary:
|
|
41
|
+
binary = try_parse_binary(binary_path)
|
|
42
|
+
assert binary is not None, f"{binary_path} is not a supported binary"
|
|
43
|
+
return binary
|
|
37
44
|
|
|
38
45
|
|
|
39
46
|
def get_architecture(binary: Path | lief.Binary) -> Arch:
|
pcode_graph/maker.py
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
|
-
from dataclasses import dataclass
|
|
1
|
+
from dataclasses import dataclass
|
|
2
2
|
|
|
3
3
|
from pcode_graph.analysis import (
|
|
4
4
|
EntryIndex,
|
|
5
5
|
OpIndex,
|
|
6
6
|
RichPcodeList,
|
|
7
|
+
MEMORY,
|
|
7
8
|
UnknownCodeIndex,
|
|
8
9
|
ExitIndex,
|
|
9
10
|
Var,
|
|
10
11
|
VarKinds,
|
|
12
|
+
wrap,
|
|
11
13
|
)
|
|
12
14
|
from pcode_graph.asm import Assembler
|
|
13
15
|
from pcode_graph.basic_blocks import make_basic_blocks
|
|
14
16
|
from pcode_graph.checker import check_graph
|
|
15
17
|
from pcode_graph.graph import CDG, Edge, EdgeKinds, NodeIndex, NodeKinds, Node
|
|
18
|
+
from pcode_graph.memory import StackMemory, analyze_stack_memory
|
|
16
19
|
from pcode_graph.pcode import from_native_opcode
|
|
17
20
|
from pcode_graph.registers import SPECIAL_REGISTERS
|
|
18
21
|
from pcode_graph.simplifier import GraphSimplifier
|
|
@@ -25,10 +28,10 @@ from loguru import logger
|
|
|
25
28
|
@dataclass
|
|
26
29
|
class MakerFlags:
|
|
27
30
|
build_cfg: bool = True
|
|
28
|
-
"""
|
|
31
|
+
"""Adds control flow links in addition to the data flow. Defaults to True."""
|
|
29
32
|
|
|
30
33
|
reg_outputs_black_list: set[str] | None = None
|
|
31
|
-
"""
|
|
34
|
+
"""Does not consider the given registers as graph outputs to simplify the graphs.
|
|
32
35
|
Arch-dependent register names.
|
|
33
36
|
If white-list is not None, the value of black-list is ignored.
|
|
34
37
|
If both black-list and white-list are None, the content of SPECIAL_REGISTERS is used
|
|
@@ -36,17 +39,23 @@ class MakerFlags:
|
|
|
36
39
|
"""
|
|
37
40
|
|
|
38
41
|
reg_outputs_white_list: set[str] | None = None
|
|
39
|
-
"""
|
|
42
|
+
"""Considers only the given registers as graph outputs to simplify the graphs.
|
|
40
43
|
Arch-dependent register names.
|
|
41
44
|
If white-list is not None, the value of black-list is ignored.
|
|
42
45
|
"""
|
|
43
46
|
|
|
44
47
|
basic_blocks: bool = True
|
|
45
|
-
"""
|
|
48
|
+
"""Groups operations by basic block in the graph. Make it invariant
|
|
46
49
|
by instruction reordering and to reduce its diameter.
|
|
47
50
|
Requires build_cfg. Defaults to True.
|
|
48
51
|
"""
|
|
49
52
|
|
|
53
|
+
memory_analysis: bool = True
|
|
54
|
+
"""Resolves stack accesses to link LOADs to the STORE they read, and forwards
|
|
55
|
+
stored values to the users of LOADs when possible.
|
|
56
|
+
Mainly useful for code compiled without optimization. Defaults to True.
|
|
57
|
+
"""
|
|
58
|
+
|
|
50
59
|
|
|
51
60
|
def make_graph(prg: RichPcodeList, flags: MakerFlags | None = None) -> CDG:
|
|
52
61
|
"""Makes a graph from a list of P-Code operations.
|
|
@@ -66,6 +75,8 @@ def make_graph(prg: RichPcodeList, flags: MakerFlags | None = None) -> CDG:
|
|
|
66
75
|
if black_list is None and white_list is None:
|
|
67
76
|
black_list = SPECIAL_REGISTERS[prg.arch]
|
|
68
77
|
|
|
78
|
+
memory = analyze_stack_memory(prg) if flags.memory_analysis else StackMemory()
|
|
79
|
+
|
|
69
80
|
# Already created input nodes, by related input variable
|
|
70
81
|
input_node_indexes: dict[Var, NodeIndex] = {}
|
|
71
82
|
|
|
@@ -74,7 +85,7 @@ def make_graph(prg: RichPcodeList, flags: MakerFlags | None = None) -> CDG:
|
|
|
74
85
|
|
|
75
86
|
# Already created phi nodes, by (sorted) possible alternatives
|
|
76
87
|
# to avoid duplicate phi nodes
|
|
77
|
-
phi_node_indexes: dict[tuple[NodeIndex,
|
|
88
|
+
phi_node_indexes: dict[tuple[NodeIndex, ...], NodeIndex] = {}
|
|
78
89
|
|
|
79
90
|
# Created nodes of all kinds
|
|
80
91
|
nodes: list[Node] = []
|
|
@@ -169,6 +180,35 @@ def make_graph(prg: RichPcodeList, flags: MakerFlags | None = None) -> CDG:
|
|
|
169
180
|
):
|
|
170
181
|
edges.append(Edge(from_node, to_node, EdgeKinds.Data, operand_number))
|
|
171
182
|
|
|
183
|
+
def get_pred_nodes(var: Var, def_op_indexes: set[OpIndex]) -> list[NodeIndex]:
|
|
184
|
+
"""Returns the nodes defining the given variable,
|
|
185
|
+
replacing forwarded loads by the definitions of the stored values."""
|
|
186
|
+
|
|
187
|
+
pred_node_indexes: list[NodeIndex] = []
|
|
188
|
+
seen_loads: set[OpIndex] = set()
|
|
189
|
+
pending = [(var, def_op_indexes)]
|
|
190
|
+
while pending:
|
|
191
|
+
var, def_op_indexes = pending.pop()
|
|
192
|
+
# Sort defs to remain determinist
|
|
193
|
+
for def_op_index in sorted(def_op_indexes):
|
|
194
|
+
store_indexes = memory.forwarded_loads.get(def_op_index)
|
|
195
|
+
if store_indexes is not None:
|
|
196
|
+
if def_op_index not in seen_loads:
|
|
197
|
+
seen_loads.add(def_op_index)
|
|
198
|
+
for store_index in store_indexes:
|
|
199
|
+
value = wrap(prg.operations[store_index].inputs[2])
|
|
200
|
+
value_defs = prg.get_defs(store_index, value)
|
|
201
|
+
pending.append((value, value_defs or {EntryIndex}))
|
|
202
|
+
continue
|
|
203
|
+
if def_op_index == EntryIndex:
|
|
204
|
+
# Input variable
|
|
205
|
+
pred_node_index = get_or_create_input(var)
|
|
206
|
+
else:
|
|
207
|
+
pred_node_index = get_or_create_operation(def_op_index)
|
|
208
|
+
if pred_node_index not in pred_node_indexes:
|
|
209
|
+
pred_node_indexes.append(pred_node_index)
|
|
210
|
+
return pred_node_indexes
|
|
211
|
+
|
|
172
212
|
def link_defs(
|
|
173
213
|
var: Var,
|
|
174
214
|
def_op_indexes: set[OpIndex],
|
|
@@ -177,14 +217,7 @@ def make_graph(prg: RichPcodeList, flags: MakerFlags | None = None) -> CDG:
|
|
|
177
217
|
):
|
|
178
218
|
|
|
179
219
|
# Collect data preds to see if we need a phi node
|
|
180
|
-
|
|
181
|
-
pred_node_indexes = []
|
|
182
|
-
for def_op_index in sorted(list(def_op_indexes)):
|
|
183
|
-
if def_op_index == EntryIndex:
|
|
184
|
-
# Input variable
|
|
185
|
-
pred_node_indexes.append(get_or_create_input(var))
|
|
186
|
-
else:
|
|
187
|
-
pred_node_indexes.append(get_or_create_operation(def_op_index))
|
|
220
|
+
pred_node_indexes = get_pred_nodes(var, def_op_indexes)
|
|
188
221
|
|
|
189
222
|
if len(pred_node_indexes) == 1:
|
|
190
223
|
# Simple def, direct link
|
|
@@ -264,6 +297,9 @@ def make_graph(prg: RichPcodeList, flags: MakerFlags | None = None) -> CDG:
|
|
|
264
297
|
|
|
265
298
|
for number, (var, defs) in enumerate(prg.iter_input_defs(op_index)):
|
|
266
299
|
|
|
300
|
+
if var == MEMORY and op_index in memory.load_defs:
|
|
301
|
+
defs = memory.load_defs[op_index]
|
|
302
|
+
|
|
267
303
|
if not defs:
|
|
268
304
|
# No definition means this is a constant
|
|
269
305
|
pred_node_index = get_or_create_input(var)
|
pcode_graph/memory.py
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
"""Summary analysis of stack memory accesses.
|
|
2
|
+
|
|
3
|
+
LOAD and STORE addresses are resolved relatively to the stack pointer value at the entry
|
|
4
|
+
of the code, so that loads can be linked to the stores they read. This mainly targets
|
|
5
|
+
spilled variables, as found in code compiled without optimization (-O0).
|
|
6
|
+
|
|
7
|
+
No assumption is made on the use of the stack: calls to unknown code and stores to
|
|
8
|
+
unresolved addresses may write anywhere, so the loads they reach keep the coarse single MEMORY model.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from dataclasses import dataclass, field
|
|
12
|
+
import heapq
|
|
13
|
+
|
|
14
|
+
from pypcode import OpCode, PcodeOp
|
|
15
|
+
|
|
16
|
+
from pcode_graph.arch import Arch
|
|
17
|
+
from pcode_graph.analysis import (
|
|
18
|
+
EntryIndex,
|
|
19
|
+
OpIndex,
|
|
20
|
+
RichPcodeList,
|
|
21
|
+
UnknownCodeIndex,
|
|
22
|
+
Var,
|
|
23
|
+
VarKinds,
|
|
24
|
+
wrap,
|
|
25
|
+
)
|
|
26
|
+
from pcode_graph.bit_utils import unsigned_to_signed
|
|
27
|
+
|
|
28
|
+
# Stack pointer register of each architecture, as identified by pypcode
|
|
29
|
+
STACK_POINTERS: dict[Arch, Var] = {
|
|
30
|
+
Arch.x86_64: Var(VarKinds.Register, 8, 0x20, "RSP"),
|
|
31
|
+
Arch.x86_32: Var(VarKinds.Register, 4, 0x10, "ESP"),
|
|
32
|
+
Arch.arm_64: Var(VarKinds.Register, 8, 0x8, "sp"),
|
|
33
|
+
Arch.arm_32: Var(VarKinds.Register, 4, 0x54, "sp"),
|
|
34
|
+
Arch.mips_64: Var(VarKinds.Register, 8, 0xE8, "sp"),
|
|
35
|
+
Arch.mips_32: Var(VarKinds.Register, 4, 0x74, "sp"),
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
# Pseudo definition of memory written by not analyzed code or an unresolved store
|
|
39
|
+
UnknownWriteIndex = -4
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@dataclass(frozen=True, slots=True)
|
|
43
|
+
class StackAddress:
|
|
44
|
+
"""Address relative to the stack pointer value at entry."""
|
|
45
|
+
|
|
46
|
+
offset: int
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
# Known constant, stack address or unknown value
|
|
50
|
+
AbstractValue = int | StackAddress | None
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass(frozen=True, slots=True)
|
|
54
|
+
class StackAccess:
|
|
55
|
+
offset: int
|
|
56
|
+
size: int
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def offsets(self) -> range:
|
|
60
|
+
return range(self.offset, self.offset + self.size)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@dataclass(frozen=True, slots=True)
|
|
64
|
+
class StackState:
|
|
65
|
+
# Definitions of tracked bytes by offset
|
|
66
|
+
tracked: dict[int, frozenset[OpIndex]]
|
|
67
|
+
|
|
68
|
+
# Definitions of untracked bytes
|
|
69
|
+
default: frozenset[OpIndex]
|
|
70
|
+
|
|
71
|
+
def read(self, access: StackAccess) -> frozenset[OpIndex]:
|
|
72
|
+
return frozenset().union(
|
|
73
|
+
*(self.tracked.get(offset, self.default) for offset in access.offsets)
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
def write(self, access: StackAccess, index: OpIndex) -> "StackState":
|
|
77
|
+
written = frozenset({index})
|
|
78
|
+
tracked = dict(self.tracked)
|
|
79
|
+
for offset in access.offsets:
|
|
80
|
+
tracked[offset] = written
|
|
81
|
+
return StackState(tracked, self.default)
|
|
82
|
+
|
|
83
|
+
def clobber(self) -> "StackState":
|
|
84
|
+
unknown = {UnknownWriteIndex}
|
|
85
|
+
return StackState(
|
|
86
|
+
{offset: defs | unknown for offset, defs in self.tracked.items()},
|
|
87
|
+
self.default | unknown,
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
def merge(self, other: "StackState") -> "StackState":
|
|
91
|
+
return StackState(
|
|
92
|
+
{
|
|
93
|
+
offset: self.tracked.get(offset, self.default)
|
|
94
|
+
| other.tracked.get(offset, other.default)
|
|
95
|
+
for offset in self.tracked.keys() | other.tracked.keys()
|
|
96
|
+
},
|
|
97
|
+
self.default | other.default,
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
ADDRESS_OPCODES = {OpCode.COPY, OpCode.INT_ADD, OpCode.INT_SUB, OpCode.INT_OR}
|
|
102
|
+
CALL_OPCODES = {OpCode.CALL, OpCode.CALLIND, OpCode.CALLOTHER}
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@dataclass
|
|
106
|
+
class StackMemory:
|
|
107
|
+
"""Results of the stack memory analysis."""
|
|
108
|
+
|
|
109
|
+
# Maps a stack-reading LOAD to possible STORE definitions
|
|
110
|
+
load_defs: dict[OpIndex, set[OpIndex]] = field(default_factory=dict)
|
|
111
|
+
|
|
112
|
+
# Maps a load to defining stores if reading exactly the values written by them
|
|
113
|
+
forwarded_loads: dict[OpIndex, list[OpIndex]] = field(default_factory=dict)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def analyze_stack_memory(program: RichPcodeList) -> StackMemory:
|
|
117
|
+
return StackMemoryAnalyzer(program)()
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
class StackMemoryAnalyzer:
|
|
121
|
+
|
|
122
|
+
def __init__(self, program: RichPcodeList) -> None:
|
|
123
|
+
self.program = program
|
|
124
|
+
self.stack_pointer = STACK_POINTERS.get(program.arch)
|
|
125
|
+
self.result = StackMemory()
|
|
126
|
+
self._output_values: dict[OpIndex, AbstractValue] = {}
|
|
127
|
+
self._loads: dict[OpIndex, StackAccess] = {}
|
|
128
|
+
self._stores: dict[OpIndex, StackAccess | None] = {}
|
|
129
|
+
|
|
130
|
+
def __call__(self) -> StackMemory:
|
|
131
|
+
if self.stack_pointer is None or not self.program.basic_blocks:
|
|
132
|
+
return self.result
|
|
133
|
+
|
|
134
|
+
for index, op in enumerate(self.program.operations):
|
|
135
|
+
if not self.program.reachable(index):
|
|
136
|
+
continue
|
|
137
|
+
if op.output is not None:
|
|
138
|
+
# Evaluating in program order bounds the recursion depth
|
|
139
|
+
self._output_value(index, wrap(op.output))
|
|
140
|
+
if op.opcode == OpCode.LOAD:
|
|
141
|
+
access = self._compute_access(index, op.output.size)
|
|
142
|
+
if access is not None:
|
|
143
|
+
self._loads[index] = access
|
|
144
|
+
elif op.opcode == OpCode.STORE:
|
|
145
|
+
self._stores[index] = self._compute_access(index, op.inputs[2].size)
|
|
146
|
+
|
|
147
|
+
self._collect_load_defs(self._compute_reads())
|
|
148
|
+
return self.result
|
|
149
|
+
|
|
150
|
+
def value_of(self, index: OpIndex, var: Var) -> AbstractValue:
|
|
151
|
+
"""Evaluates the given variable when read by the given operation."""
|
|
152
|
+
|
|
153
|
+
match var.kind:
|
|
154
|
+
case VarKinds.Constant:
|
|
155
|
+
return unsigned_to_signed(var.value, var.size)
|
|
156
|
+
case VarKinds.Register | VarKinds.Unique:
|
|
157
|
+
pass
|
|
158
|
+
case _:
|
|
159
|
+
return None
|
|
160
|
+
|
|
161
|
+
definitions = self.program.get_defs(index, var)
|
|
162
|
+
if definitions == {EntryIndex}:
|
|
163
|
+
return StackAddress(0) if var == self.stack_pointer else None
|
|
164
|
+
if not definitions or EntryIndex in definitions:
|
|
165
|
+
return None
|
|
166
|
+
values = {self._output_value(definition, var) for definition in definitions}
|
|
167
|
+
return values.pop() if len(values) == 1 else None
|
|
168
|
+
|
|
169
|
+
def _output_value(self, index: OpIndex, var: Var) -> AbstractValue:
|
|
170
|
+
op = self.program.operations[index]
|
|
171
|
+
if op.output is None or wrap(op.output) != var:
|
|
172
|
+
return None
|
|
173
|
+
if index not in self._output_values:
|
|
174
|
+
# Breaks cycles in loops
|
|
175
|
+
self._output_values[index] = None
|
|
176
|
+
self._output_values[index] = self._evaluate(index, op)
|
|
177
|
+
return self._output_values[index]
|
|
178
|
+
|
|
179
|
+
def _evaluate(self, index: OpIndex, op: PcodeOp) -> AbstractValue:
|
|
180
|
+
if op.opcode not in ADDRESS_OPCODES:
|
|
181
|
+
return None
|
|
182
|
+
assert op.output is not None
|
|
183
|
+
operands = [self.value_of(index, wrap(varnode)) for varnode in op.inputs]
|
|
184
|
+
size = op.output.size
|
|
185
|
+
match op.opcode, operands:
|
|
186
|
+
case OpCode.COPY, [value]:
|
|
187
|
+
return value
|
|
188
|
+
case OpCode.INT_ADD, [StackAddress(offset), int(constant)] | [
|
|
189
|
+
int(constant),
|
|
190
|
+
StackAddress(offset),
|
|
191
|
+
]:
|
|
192
|
+
return StackAddress(truncate(offset + constant, size))
|
|
193
|
+
case OpCode.INT_ADD, [int(left), int(right)]:
|
|
194
|
+
return truncate(left + right, size)
|
|
195
|
+
case OpCode.INT_SUB, [StackAddress(offset), int(constant)]:
|
|
196
|
+
return StackAddress(truncate(offset - constant, size))
|
|
197
|
+
case OpCode.INT_SUB, [StackAddress(left), StackAddress(right)]:
|
|
198
|
+
return truncate(left - right, size)
|
|
199
|
+
case OpCode.INT_SUB, [int(left), int(right)]:
|
|
200
|
+
return truncate(left - right, size)
|
|
201
|
+
case OpCode.INT_OR, [value, 0] | [0, value]:
|
|
202
|
+
return value
|
|
203
|
+
return None
|
|
204
|
+
|
|
205
|
+
def _compute_access(self, index: OpIndex, size: int) -> StackAccess | None:
|
|
206
|
+
address = self.value_of(index, wrap(self.program.operations[index].inputs[1]))
|
|
207
|
+
if isinstance(address, StackAddress):
|
|
208
|
+
return StackAccess(address.offset, size)
|
|
209
|
+
return None
|
|
210
|
+
|
|
211
|
+
def _apply(self, index: OpIndex, state: StackState) -> StackState:
|
|
212
|
+
"""Returns the stack state after the given operation."""
|
|
213
|
+
|
|
214
|
+
op = self.program.operations[index]
|
|
215
|
+
if op.opcode == OpCode.STORE:
|
|
216
|
+
access = self._stores[index]
|
|
217
|
+
return state.clobber() if access is None else state.write(access, index)
|
|
218
|
+
if op.opcode in CALL_OPCODES and UnknownCodeIndex in self.program.succ_ops(
|
|
219
|
+
index
|
|
220
|
+
):
|
|
221
|
+
return state.clobber()
|
|
222
|
+
return state
|
|
223
|
+
|
|
224
|
+
def _compute_reads(self) -> dict[OpIndex, frozenset[OpIndex]]:
|
|
225
|
+
"""Computes the definitions of the bytes read by each resolved load."""
|
|
226
|
+
|
|
227
|
+
blocks = self.program.basic_blocks
|
|
228
|
+
block_inputs: list[StackState | None] = [None] * len(blocks)
|
|
229
|
+
block_inputs[0] = StackState({}, frozenset({EntryIndex}))
|
|
230
|
+
reads: dict[OpIndex, frozenset[OpIndex]] = {}
|
|
231
|
+
pending = [0]
|
|
232
|
+
queued = {0}
|
|
233
|
+
|
|
234
|
+
while pending:
|
|
235
|
+
block_index = heapq.heappop(pending)
|
|
236
|
+
queued.remove(block_index)
|
|
237
|
+
state = block_inputs[block_index]
|
|
238
|
+
assert state is not None
|
|
239
|
+
for index in blocks[block_index].op_indexes:
|
|
240
|
+
if index in self._loads:
|
|
241
|
+
reads[index] = state.read(self._loads[index])
|
|
242
|
+
state = self._apply(index, state)
|
|
243
|
+
|
|
244
|
+
for successor in blocks[block_index].succ_blocks:
|
|
245
|
+
if successor < 0:
|
|
246
|
+
continue
|
|
247
|
+
previous = block_inputs[successor]
|
|
248
|
+
merged = state if previous is None else previous.merge(state)
|
|
249
|
+
if merged != previous:
|
|
250
|
+
block_inputs[successor] = merged
|
|
251
|
+
if successor not in queued:
|
|
252
|
+
heapq.heappush(pending, successor)
|
|
253
|
+
queued.add(successor)
|
|
254
|
+
return reads
|
|
255
|
+
|
|
256
|
+
def _collect_load_defs(self, reads: dict[OpIndex, frozenset[OpIndex]]) -> None:
|
|
257
|
+
for index, definitions in reads.items():
|
|
258
|
+
if UnknownWriteIndex in definitions:
|
|
259
|
+
continue
|
|
260
|
+
self.result.load_defs[index] = set(definitions)
|
|
261
|
+
if EntryIndex not in definitions and all(
|
|
262
|
+
self._stores[definition] == self._loads[index]
|
|
263
|
+
for definition in definitions
|
|
264
|
+
):
|
|
265
|
+
self.result.forwarded_loads[index] = sorted(definitions)
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def truncate(value: int, size: int) -> int:
|
|
269
|
+
"""Wraps the given value as a signed integer of the given size in bytes."""
|
|
270
|
+
|
|
271
|
+
return unsigned_to_signed(value & ((1 << (size * 8)) - 1), size)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: pcode_graph
|
|
3
|
-
Version:
|
|
3
|
+
Version: 3.0.0
|
|
4
4
|
Summary: Build, display and vectorize semantic graphs from binary code
|
|
5
5
|
Project-URL: Homepage, https://github.com/quarkslab/pcode_graph
|
|
6
6
|
Project-URL: Issues, https://github.com/quarkslab/pcode_graph/issues
|
|
@@ -140,6 +140,12 @@ mul x1, x0, x0
|
|
|
140
140
|
graph = make_graph_from_asm(assembler, translator, asm)
|
|
141
141
|
```
|
|
142
142
|
|
|
143
|
+
### Pointer alias analysis
|
|
144
|
+
|
|
145
|
+
Addresses are resolved relatively to the stack pointer at entry, to link each stack LOAD to the STOREs it actually reads. This mainly helps with code compiled without optimization, where variables are spilled.
|
|
146
|
+
|
|
147
|
+
A part from this, no advanced alias analysis is performed and no assumption is made about the used ABI.
|
|
148
|
+
|
|
143
149
|
### View the graph
|
|
144
150
|
|
|
145
151
|
You can unparse the graph into markdown using the mermaid syntax:
|
|
@@ -7,13 +7,14 @@ pcode_graph/basic_blocks.py,sha256=CdXXH4XmIzclAdJ4iuIYJtZJoCOgDLJbrwDe2jgPi3c,7
|
|
|
7
7
|
pcode_graph/bit_utils.py,sha256=rEEfiJevPc-QMYVryZVwWPBOufC3MyA3bZZ8y9Q4aLU,405
|
|
8
8
|
pcode_graph/characterization.py,sha256=CgTdnOwLu2vGKaHTudhqfcFq1q04LGOFU41ETtSRMZ0,3227
|
|
9
9
|
pcode_graph/checker.py,sha256=W_1oziTYRREXqREbrOnLqfnakgsgUiAZikWGSST3pZk,9155
|
|
10
|
-
pcode_graph/cli.py,sha256=
|
|
10
|
+
pcode_graph/cli.py,sha256=hTOnu0L6yTIOHzUTt5SqFP5dV5O5xjlB07ohbzjg0Qk,7213
|
|
11
11
|
pcode_graph/emulator.py,sha256=7r1aMoTKDc6j_cT8V2vCcts4Mg2FMmvdoP8Y3H12RDY,13366
|
|
12
12
|
pcode_graph/gnn_exporter.py,sha256=fd0Mz_2NJaQhYQ196nE7CEM0U1cG_9pjCA4yPrJnJHk,7356
|
|
13
13
|
pcode_graph/graph.py,sha256=gLHkVg7bN6Jrleu7C-n9DQDjkIXeq6g9c-fN3wKZrlc,7614
|
|
14
|
-
pcode_graph/lief_importer.py,sha256=
|
|
14
|
+
pcode_graph/lief_importer.py,sha256=SOgUhF4hjgVetfXjzUQn1M3HAWb-Eo08bTJq_H7i8H0,9910
|
|
15
15
|
pcode_graph/log.py,sha256=qo6gh8bHsl-4-dnzsbHsB5rGism-MtjLJmQ-uBQwOIk,1035
|
|
16
|
-
pcode_graph/maker.py,sha256=
|
|
16
|
+
pcode_graph/maker.py,sha256=gvc9vi20slimzfEi19wEhR8xwk6hL2jw42ig0bxMvdY,16244
|
|
17
|
+
pcode_graph/memory.py,sha256=5SdPZF3XajdWNv0iS0PLOLjCsn2eQpLV8BV9wkqxVxY,10269
|
|
17
18
|
pcode_graph/nx_exporter.py,sha256=lSczsuuyD9XNE_8PgqCEvPdbMMXSdKLLt6YNPsGF-Zk,629
|
|
18
19
|
pcode_graph/pcode.py,sha256=OPLZriOUUBHOlolvgCPYJO8CXg7rcUjyoorTZO8lebw,4593
|
|
19
20
|
pcode_graph/registers.py,sha256=Grw4Ypr8GldG5nR1YeDG4rMf13gvzugSngvq2y51xjk,58328
|
|
@@ -22,8 +23,8 @@ pcode_graph/torch_utils.py,sha256=GwVjWKL5Lqhm94b69o-rCilzNRu237gyJo6T6xWQHQc,96
|
|
|
22
23
|
pcode_graph/translator.py,sha256=h90ay8VnCsgWRpK9fcYa0iu6CKXY0YDKl8CtH_gIx3U,7619
|
|
23
24
|
pcode_graph/view.py,sha256=UybFV8xSDTJdqzzqop-U-90qnyKxiO5q_5B1Ef8DaiQ,2501
|
|
24
25
|
pcode_graph/visitor.py,sha256=bxaQ9q0lTw8ysPOrta5G7LeHZYnq3IBxMafM_-8FruM,6990
|
|
25
|
-
pcode_graph-
|
|
26
|
-
pcode_graph-
|
|
27
|
-
pcode_graph-
|
|
28
|
-
pcode_graph-
|
|
29
|
-
pcode_graph-
|
|
26
|
+
pcode_graph-3.0.0.dist-info/METADATA,sha256=wReA0vphJrcxomhHQTUkpX-l95NULvatd3IQe0ilUQ0,6230
|
|
27
|
+
pcode_graph-3.0.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
28
|
+
pcode_graph-3.0.0.dist-info/entry_points.txt,sha256=7pTTgqjYSRYwmBdA0GwJYXPfOmdk8I_mGsJOHHx-PXQ,45
|
|
29
|
+
pcode_graph-3.0.0.dist-info/licenses/LICENSE,sha256=iVkUrjpGBX_59jOK6a7mKkvjHUCGfiCFQRgrziEh-nY,1066
|
|
30
|
+
pcode_graph-3.0.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|