pcode-graph 4.1.0__tar.gz → 4.2.1__tar.gz
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-4.1.0 → pcode_graph-4.2.1}/CHANGELOG +8 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/PKG-INFO +1 -1
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/analysis/alias_analysis.py +106 -49
- pcode_graph-4.2.1/pcode_graph/analysis/def_use.py +115 -0
- pcode_graph-4.2.1/pcode_graph/analysis/interning.py +11 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/analysis/program.py +42 -29
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/characterization.py +0 -1
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/tests/analysis_test.py +1 -1
- pcode_graph-4.2.1/tests/def_use_test.py +98 -0
- pcode_graph-4.2.1/tests/memory_state_test.py +65 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/tests/memory_test.py +15 -0
- pcode_graph-4.1.0/pcode_graph/analysis/def_use.py +0 -116
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/.github/workflows/release.yml +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/.github/workflows/test.yml +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/.gitignore +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/LICENSE +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/README.md +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/__init__.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/__main__.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/analysis/cfg.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/analysis/load_store.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/analysis/native_pcode.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/analysis/pass_base.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/analysis/passes.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/analysis/unreachable.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/arch.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/asm.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/basic_blocks.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/bit_utils.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/checker.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/cli.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/emulator.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/gnn_exporter.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/graph.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/lief_importer.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/log.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/maker.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/nx_exporter.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/pcode.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/registers.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/simplifier.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/torch_utils.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/translator.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/view.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pcode_graph/visitor.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/pyproject.toml +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/requirements.txt +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/tests/__init__.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/tests/basic_block_test.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/tests/callother_test.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/tests/charac_test.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/tests/cli_test.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/tests/conftest.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/tests/emulator_test.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/tests/exporter_test.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/tests/fixtures.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/tests/helpers.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/tests/lief_importer_test.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/tests/load_store_test.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/tests/maker_test.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/tests/translator_test.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/tests/utils_test.py +0 -0
- {pcode_graph-4.1.0 → pcode_graph-4.2.1}/todo.md +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: pcode_graph
|
|
3
|
-
Version: 4.1
|
|
3
|
+
Version: 4.2.1
|
|
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
|
|
@@ -15,6 +15,7 @@ import heapq
|
|
|
15
15
|
|
|
16
16
|
from pypcode import OpCode, PcodeOp
|
|
17
17
|
|
|
18
|
+
from pcode_graph.analysis.interning import Interner
|
|
18
19
|
from pcode_graph.analysis.pass_base import PassBase
|
|
19
20
|
from pcode_graph.arch import Arch
|
|
20
21
|
from pcode_graph.analysis.program import (
|
|
@@ -71,42 +72,69 @@ class MemoryAccess:
|
|
|
71
72
|
]
|
|
72
73
|
|
|
73
74
|
|
|
74
|
-
|
|
75
|
+
DefinitionSet = frozenset[OpIndex]
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
@dataclass(slots=True)
|
|
75
79
|
class MemoryState:
|
|
80
|
+
"""Definitions of memory bytes, updated in place.
|
|
81
|
+
|
|
82
|
+
A state must not be shared between blocks: copy it before updating it.
|
|
83
|
+
Definition sets are immutable and interned, so they can be shared.
|
|
84
|
+
"""
|
|
85
|
+
|
|
76
86
|
# Definitions of tracked bytes
|
|
77
|
-
tracked: dict[MemoryByte,
|
|
87
|
+
tracked: dict[MemoryByte, DefinitionSet]
|
|
78
88
|
|
|
79
89
|
# Definitions of untracked bytes
|
|
80
|
-
default:
|
|
90
|
+
default: DefinitionSet
|
|
91
|
+
|
|
92
|
+
def copy(self) -> "MemoryState":
|
|
93
|
+
return MemoryState(dict(self.tracked), self.default)
|
|
81
94
|
|
|
82
|
-
def read(self, access: MemoryAccess) ->
|
|
95
|
+
def read(self, access: MemoryAccess) -> DefinitionSet:
|
|
83
96
|
return frozenset().union(
|
|
84
97
|
*(self.tracked.get(byte, self.default) for byte in access.bytes)
|
|
85
98
|
)
|
|
86
99
|
|
|
87
|
-
def write(
|
|
88
|
-
|
|
89
|
-
|
|
100
|
+
def write(
|
|
101
|
+
self, access: MemoryAccess, index: OpIndex, intern: Interner[DefinitionSet]
|
|
102
|
+
) -> None:
|
|
103
|
+
written = intern(frozenset({index}))
|
|
90
104
|
for byte in access.bytes:
|
|
91
|
-
tracked[byte] = written
|
|
92
|
-
return MemoryState(tracked, self.default)
|
|
105
|
+
self.tracked[byte] = written
|
|
93
106
|
|
|
94
|
-
def clobber(self) ->
|
|
107
|
+
def clobber(self, intern: Interner[DefinitionSet]) -> None:
|
|
95
108
|
unknown = {UnknownWriteIndex}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
109
|
+
for byte, defs in self.tracked.items():
|
|
110
|
+
self.tracked[byte] = intern(defs | unknown)
|
|
111
|
+
self.default = intern(self.default | unknown)
|
|
112
|
+
|
|
113
|
+
def merge(self, other: "MemoryState", intern: Interner[DefinitionSet]) -> bool:
|
|
114
|
+
"""Adds the definitions of the other state, left unchanged.
|
|
115
|
+
Returns True if this state changed.
|
|
116
|
+
"""
|
|
117
|
+
|
|
118
|
+
default_grows = not other.default <= self.default
|
|
119
|
+
changed = default_grows
|
|
120
|
+
for byte, theirs in other.tracked.items():
|
|
121
|
+
mine = self.tracked.get(byte)
|
|
122
|
+
if mine is None:
|
|
123
|
+
if not theirs <= self.default:
|
|
124
|
+
changed = True
|
|
125
|
+
elif not default_grows:
|
|
126
|
+
continue
|
|
127
|
+
self.tracked[byte] = intern(self.default | theirs)
|
|
128
|
+
elif mine is not theirs and not theirs <= mine:
|
|
129
|
+
self.tracked[byte] = intern(mine | theirs)
|
|
130
|
+
changed = True
|
|
131
|
+
for byte, mine in self.tracked.items():
|
|
132
|
+
if byte not in other.tracked and not other.default <= mine:
|
|
133
|
+
self.tracked[byte] = intern(mine | other.default)
|
|
134
|
+
changed = True
|
|
135
|
+
if default_grows:
|
|
136
|
+
self.default = intern(self.default | other.default)
|
|
137
|
+
return changed
|
|
110
138
|
|
|
111
139
|
|
|
112
140
|
ADDRESS_OPCODES = {OpCode.COPY, OpCode.INT_ADD, OpCode.INT_SUB, OpCode.INT_OR}
|
|
@@ -120,6 +148,8 @@ class MemoryAnalyzer(PassBase):
|
|
|
120
148
|
|
|
121
149
|
self.stack_pointer = STACK_POINTERS.get(program.arch)
|
|
122
150
|
self._output_values: dict[OpIndex, AbstractValue] = {}
|
|
151
|
+
# Unevaluated definitions met by _evaluate_from
|
|
152
|
+
self._missing: list[OpIndex] | None = None
|
|
123
153
|
self._loads: dict[OpIndex, MemoryAccess] = {}
|
|
124
154
|
self._stores: dict[OpIndex, MemoryAccess | None] = {}
|
|
125
155
|
|
|
@@ -135,7 +165,6 @@ class MemoryAnalyzer(PassBase):
|
|
|
135
165
|
if not self.program.reachable(index):
|
|
136
166
|
continue
|
|
137
167
|
if op.output is not None:
|
|
138
|
-
# Evaluating in program order bounds the recursion depth
|
|
139
168
|
self._output_value(index, wrap(op.output))
|
|
140
169
|
if op.opcode == OpCode.LOAD:
|
|
141
170
|
access = self._compute_access(index, op.output.size)
|
|
@@ -145,7 +174,6 @@ class MemoryAnalyzer(PassBase):
|
|
|
145
174
|
self._stores[index] = self._compute_access(index, op.inputs[2].size)
|
|
146
175
|
|
|
147
176
|
self._collect_load_defs(self._compute_reads())
|
|
148
|
-
|
|
149
177
|
|
|
150
178
|
def value_of(self, index: OpIndex, var: Var) -> AbstractValue:
|
|
151
179
|
"""Evaluates the given variable when read by the given operation."""
|
|
@@ -171,10 +199,31 @@ class MemoryAnalyzer(PassBase):
|
|
|
171
199
|
if op.output is None or wrap(op.output) != var:
|
|
172
200
|
return None
|
|
173
201
|
if index not in self._output_values:
|
|
202
|
+
if op.opcode not in ADDRESS_OPCODES:
|
|
203
|
+
self._output_values[index] = None
|
|
204
|
+
elif self._missing is None:
|
|
205
|
+
self._evaluate_from(index)
|
|
206
|
+
else:
|
|
207
|
+
# Evaluated later by _evaluate_from
|
|
208
|
+
self._missing.append(index)
|
|
209
|
+
return self._output_values.get(index)
|
|
210
|
+
|
|
211
|
+
def _evaluate_from(self, root: OpIndex) -> None:
|
|
212
|
+
"""Evaluates the given operation after its dependencies, without recursion."""
|
|
213
|
+
|
|
214
|
+
pending = [root]
|
|
215
|
+
while pending:
|
|
216
|
+
index = pending[-1]
|
|
174
217
|
# Breaks cycles in loops
|
|
175
|
-
self._output_values
|
|
176
|
-
self.
|
|
177
|
-
|
|
218
|
+
self._output_values.setdefault(index, None)
|
|
219
|
+
self._missing = []
|
|
220
|
+
value = self._evaluate(index, self.program.operations[index])
|
|
221
|
+
if self._missing:
|
|
222
|
+
pending += self._missing
|
|
223
|
+
else:
|
|
224
|
+
self._output_values[index] = value
|
|
225
|
+
pending.pop()
|
|
226
|
+
self._missing = None
|
|
178
227
|
|
|
179
228
|
def _evaluate(self, index: OpIndex, op: PcodeOp) -> AbstractValue:
|
|
180
229
|
if op.opcode not in ADDRESS_OPCODES:
|
|
@@ -211,52 +260,60 @@ class MemoryAnalyzer(PassBase):
|
|
|
211
260
|
return MemoryAccess(False, absolute, size)
|
|
212
261
|
return None
|
|
213
262
|
|
|
214
|
-
def _apply(
|
|
215
|
-
|
|
263
|
+
def _apply(
|
|
264
|
+
self, index: OpIndex, state: MemoryState, intern: Interner[DefinitionSet]
|
|
265
|
+
) -> None:
|
|
266
|
+
"""Updates the memory state with the effect of the given operation."""
|
|
216
267
|
|
|
217
268
|
op = self.program.operations[index]
|
|
218
269
|
if op.opcode == OpCode.STORE:
|
|
219
270
|
access = self._stores[index]
|
|
220
|
-
|
|
221
|
-
|
|
271
|
+
if access is None:
|
|
272
|
+
state.clobber(intern)
|
|
273
|
+
else:
|
|
274
|
+
state.write(access, index, intern)
|
|
275
|
+
elif op.opcode in CALL_OPCODES and UnknownCodeIndex in self.program.succ_ops(
|
|
222
276
|
index
|
|
223
277
|
):
|
|
224
|
-
|
|
225
|
-
return state
|
|
278
|
+
state.clobber(intern)
|
|
226
279
|
|
|
227
|
-
def _compute_reads(self) -> dict[OpIndex,
|
|
280
|
+
def _compute_reads(self) -> dict[OpIndex, DefinitionSet]:
|
|
228
281
|
"""Computes the definitions of the bytes read by each resolved load."""
|
|
229
282
|
|
|
230
283
|
blocks = self.program.basic_blocks
|
|
284
|
+
intern: Interner[DefinitionSet] = Interner()
|
|
231
285
|
block_inputs: list[MemoryState | None] = [None] * len(blocks)
|
|
232
|
-
block_inputs[0] = MemoryState({}, frozenset({EntryIndex}))
|
|
233
|
-
reads: dict[OpIndex,
|
|
286
|
+
block_inputs[0] = MemoryState({}, intern(frozenset({EntryIndex})))
|
|
287
|
+
reads: dict[OpIndex, DefinitionSet] = {}
|
|
234
288
|
pending = [0]
|
|
235
289
|
queued = {0}
|
|
236
290
|
|
|
237
291
|
while pending:
|
|
238
292
|
block_index = heapq.heappop(pending)
|
|
239
293
|
queued.remove(block_index)
|
|
240
|
-
|
|
241
|
-
assert
|
|
294
|
+
block_input = block_inputs[block_index]
|
|
295
|
+
assert block_input is not None
|
|
296
|
+
state = block_input.copy()
|
|
242
297
|
for index in blocks[block_index].op_indexes:
|
|
243
298
|
if index in self._loads:
|
|
244
|
-
reads[index] = state.read(self._loads[index])
|
|
245
|
-
|
|
299
|
+
reads[index] = intern(state.read(self._loads[index]))
|
|
300
|
+
self._apply(index, state, intern)
|
|
246
301
|
|
|
247
302
|
for successor in blocks[block_index].succ_blocks:
|
|
248
303
|
if successor < 0:
|
|
249
304
|
continue
|
|
250
305
|
previous = block_inputs[successor]
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
306
|
+
if previous is None:
|
|
307
|
+
block_inputs[successor] = state.copy()
|
|
308
|
+
changed = True
|
|
309
|
+
else:
|
|
310
|
+
changed = previous.merge(state, intern)
|
|
311
|
+
if changed and successor not in queued:
|
|
312
|
+
heapq.heappush(pending, successor)
|
|
313
|
+
queued.add(successor)
|
|
257
314
|
return reads
|
|
258
315
|
|
|
259
|
-
def _collect_load_defs(self, reads: dict[OpIndex,
|
|
316
|
+
def _collect_load_defs(self, reads: dict[OpIndex, DefinitionSet]) -> None:
|
|
260
317
|
for index, definitions in reads.items():
|
|
261
318
|
if UnknownWriteIndex in definitions:
|
|
262
319
|
continue
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import heapq
|
|
2
|
+
|
|
3
|
+
from .interning import Interner
|
|
4
|
+
from .pass_base import PassBase
|
|
5
|
+
from .program import (
|
|
6
|
+
BasicBlock,
|
|
7
|
+
Definitions,
|
|
8
|
+
EntryIndex,
|
|
9
|
+
LocalWriters,
|
|
10
|
+
OpIndex,
|
|
11
|
+
Var,
|
|
12
|
+
VarKinds,
|
|
13
|
+
wrap,
|
|
14
|
+
split,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
DefinitionInterner = Interner[frozenset[OpIndex]]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class ReachingDefBuilder(PassBase):
|
|
21
|
+
"""Computes reaching definitions at the entry of each block.
|
|
22
|
+
|
|
23
|
+
Identical definition sets are shared (interned) to reduce the memory retained by
|
|
24
|
+
the results on large functions.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
def __call__(self) -> None:
|
|
28
|
+
block_count = len(self.program.basic_blocks)
|
|
29
|
+
block_inputs: list[Definitions] = [{} for _ in range(block_count)]
|
|
30
|
+
block_writers: list[LocalWriters] = [{} for _ in range(block_count)]
|
|
31
|
+
self.program.block_inputs = block_inputs
|
|
32
|
+
self.program.block_writers = block_writers
|
|
33
|
+
if not block_count:
|
|
34
|
+
return
|
|
35
|
+
|
|
36
|
+
intern: DefinitionInterner = Interner()
|
|
37
|
+
entry = intern(frozenset({EntryIndex}))
|
|
38
|
+
shared_temporaries: set[Var] = set()
|
|
39
|
+
for block_index, block in enumerate(self.program.basic_blocks):
|
|
40
|
+
if not block.reachable:
|
|
41
|
+
continue
|
|
42
|
+
writers = self._collect_writers(block)
|
|
43
|
+
block_writers[block_index] = writers
|
|
44
|
+
for index in block.op_indexes:
|
|
45
|
+
for var in self.program.iter_inputs(index):
|
|
46
|
+
if var.kind == VarKinds.Register:
|
|
47
|
+
for byte in split(var):
|
|
48
|
+
block_inputs[0][byte] = entry
|
|
49
|
+
elif var.kind == VarKinds.Unique:
|
|
50
|
+
# Temporary bytes read in a block before being written in it
|
|
51
|
+
for byte in split(var):
|
|
52
|
+
byte_writers = writers.get(byte)
|
|
53
|
+
if not byte_writers or byte_writers[0] >= index:
|
|
54
|
+
shared_temporaries.add(byte)
|
|
55
|
+
|
|
56
|
+
# Block effects: last writer of each byte written in the block
|
|
57
|
+
generated_by_block: list[Definitions] = [
|
|
58
|
+
{
|
|
59
|
+
byte: intern(frozenset((byte_writers[-1],)))
|
|
60
|
+
for byte, byte_writers in writers.items()
|
|
61
|
+
if byte.kind != VarKinds.Unique or byte in shared_temporaries
|
|
62
|
+
}
|
|
63
|
+
for writers in block_writers
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
# Each reachable block is processed at least once, lowest index first:
|
|
67
|
+
# follows the code layout, close to a reverse post-order.
|
|
68
|
+
# The list is sorted, so it is already a valid heap.
|
|
69
|
+
pending = [
|
|
70
|
+
index
|
|
71
|
+
for index, block in enumerate(self.program.basic_blocks)
|
|
72
|
+
if block.reachable
|
|
73
|
+
]
|
|
74
|
+
queued = set(pending)
|
|
75
|
+
|
|
76
|
+
while pending:
|
|
77
|
+
block_index = heapq.heappop(pending)
|
|
78
|
+
queued.remove(block_index)
|
|
79
|
+
|
|
80
|
+
outputs = block_inputs[block_index] | generated_by_block[block_index]
|
|
81
|
+
for successor in self.program.basic_blocks[block_index].succ_blocks:
|
|
82
|
+
if successor < 0:
|
|
83
|
+
continue
|
|
84
|
+
changed = self._merge_definitions(
|
|
85
|
+
block_inputs[successor], outputs, intern
|
|
86
|
+
)
|
|
87
|
+
if changed and successor not in queued:
|
|
88
|
+
heapq.heappush(pending, successor)
|
|
89
|
+
queued.add(successor)
|
|
90
|
+
|
|
91
|
+
def _collect_writers(self, block: BasicBlock) -> LocalWriters:
|
|
92
|
+
writers: LocalWriters = {}
|
|
93
|
+
for index in block.op_indexes:
|
|
94
|
+
output = self.program.operations[index].output
|
|
95
|
+
if output:
|
|
96
|
+
for byte in split(wrap(output)):
|
|
97
|
+
writers.setdefault(byte, []).append(index)
|
|
98
|
+
return writers
|
|
99
|
+
|
|
100
|
+
@staticmethod
|
|
101
|
+
def _merge_definitions(
|
|
102
|
+
target: Definitions, source: Definitions, intern: DefinitionInterner
|
|
103
|
+
) -> bool:
|
|
104
|
+
"""Adds source definitions to target ones, returns True if target changed."""
|
|
105
|
+
|
|
106
|
+
changed = False
|
|
107
|
+
for var, defs in source.items():
|
|
108
|
+
target_defs = target.get(var)
|
|
109
|
+
if target_defs is None:
|
|
110
|
+
target[var] = defs
|
|
111
|
+
changed = True
|
|
112
|
+
elif target_defs is not defs and not defs <= target_defs:
|
|
113
|
+
target[var] = intern(target_defs | defs)
|
|
114
|
+
changed = True
|
|
115
|
+
return changed
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from collections.abc import Hashable
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Interner[T: Hashable]:
|
|
5
|
+
"""Stores equal, unmutable shared instance to save memory, like identical strings."""
|
|
6
|
+
|
|
7
|
+
def __init__(self) -> None:
|
|
8
|
+
self._values: dict[T, T] = {}
|
|
9
|
+
|
|
10
|
+
def __call__(self, value: T) -> T:
|
|
11
|
+
return self._values.setdefault(value, value)
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
from bisect import bisect_left
|
|
1
2
|
from dataclasses import dataclass, field
|
|
2
3
|
from enum import Enum
|
|
3
4
|
from functools import cache
|
|
@@ -72,23 +73,10 @@ def wrap(var: Varnode) -> Var:
|
|
|
72
73
|
|
|
73
74
|
|
|
74
75
|
# Writing operations by variable byte (see split)
|
|
75
|
-
Definitions = dict[Var,
|
|
76
|
+
Definitions = dict[Var, frozenset[OpIndex]]
|
|
76
77
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
definitions: Definitions,
|
|
80
|
-
index: OpIndex,
|
|
81
|
-
op: PcodeOp,
|
|
82
|
-
) -> Definitions:
|
|
83
|
-
"""Returns the new definitions after the given operation is applied."""
|
|
84
|
-
|
|
85
|
-
if op.output:
|
|
86
|
-
result = dict(definitions)
|
|
87
|
-
written = {index}
|
|
88
|
-
for byte in split(wrap(op.output)):
|
|
89
|
-
result[byte] = written
|
|
90
|
-
return result
|
|
91
|
-
return definitions
|
|
78
|
+
# Writing operations inside a block by variable byte, sorted by index
|
|
79
|
+
LocalWriters = dict[Var, list[OpIndex]]
|
|
92
80
|
|
|
93
81
|
|
|
94
82
|
def split(var: Var) -> tuple[Var, ...]:
|
|
@@ -156,11 +144,15 @@ class Program:
|
|
|
156
144
|
basic_blocks: list[BasicBlock] = field(default_factory=list)
|
|
157
145
|
block_indexes: list[BlockIndex] = field(default_factory=list)
|
|
158
146
|
|
|
159
|
-
# Reaching definitions
|
|
160
|
-
# - definitions (= writing operation index) that reach
|
|
147
|
+
# Reaching definitions at the entry of each block. By variable byte, set of:
|
|
148
|
+
# - definitions (= writing operation index) that reach the block,
|
|
161
149
|
# - or EntryIndex (for initial definitions of inputs).
|
|
162
|
-
#
|
|
163
|
-
|
|
150
|
+
# P-Code temporaries are only kept if read in a block before being written in it.
|
|
151
|
+
# Definitions inside a block are derived on demand from block_writers,
|
|
152
|
+
# which avoids storing a full copy of the definitions for each operation.
|
|
153
|
+
# Both are empty for unreachable blocks.
|
|
154
|
+
block_inputs: list[Definitions] = field(default_factory=list)
|
|
155
|
+
block_writers: list[LocalWriters] = field(default_factory=list)
|
|
164
156
|
|
|
165
157
|
# Memory analysis results
|
|
166
158
|
memory: Memory = field(default_factory=Memory)
|
|
@@ -232,25 +224,46 @@ class Program:
|
|
|
232
224
|
def get_defs(self, index: OpIndex, var: Var) -> set[OpIndex]:
|
|
233
225
|
"""Returns all the operations defining the value at given operation of the given variable."""
|
|
234
226
|
|
|
235
|
-
|
|
227
|
+
block_index = self.block_indexes[index]
|
|
228
|
+
inputs = self.block_inputs[block_index]
|
|
229
|
+
writers = self.block_writers[block_index]
|
|
236
230
|
defs: set[OpIndex] = set()
|
|
237
231
|
for byte in split(var):
|
|
238
|
-
|
|
232
|
+
byte_writers = writers.get(byte)
|
|
233
|
+
if byte_writers:
|
|
234
|
+
position = bisect_left(byte_writers, index)
|
|
235
|
+
if position:
|
|
236
|
+
defs.add(byte_writers[position - 1])
|
|
237
|
+
continue
|
|
238
|
+
defs.update(inputs.get(byte, ()))
|
|
239
239
|
return defs
|
|
240
240
|
|
|
241
|
-
def
|
|
241
|
+
def get_reaching_defs(self, index: OpIndex) -> Definitions:
|
|
242
|
+
"""Returns all the definitions reaching the given operation.
|
|
243
|
+
P-Code temporaries written in other blocks are omitted if not read before being
|
|
244
|
+
written in this block.
|
|
245
|
+
"""
|
|
246
|
+
|
|
247
|
+
return self._definitions_before(self.block_indexes[index], index)
|
|
248
|
+
|
|
249
|
+
def _definitions_before(self, block_index: BlockIndex, end: OpIndex) -> Definitions:
|
|
250
|
+
definitions = dict(self.block_inputs[block_index])
|
|
251
|
+
for byte, writers in self.block_writers[block_index].items():
|
|
252
|
+
position = bisect_left(writers, end)
|
|
253
|
+
if position:
|
|
254
|
+
definitions[byte] = frozenset((writers[position - 1],))
|
|
255
|
+
return definitions
|
|
256
|
+
|
|
257
|
+
def get_exit_defs(self) -> dict[Var, set[OpIndex]]:
|
|
242
258
|
"""Computes side effects of the program."""
|
|
243
259
|
|
|
244
|
-
exit_defs:
|
|
245
|
-
for block in self.basic_blocks:
|
|
260
|
+
exit_defs: dict[Var, set[OpIndex]] = {}
|
|
261
|
+
for block_index, block in enumerate(self.basic_blocks):
|
|
246
262
|
if not block.reachable or not {UnknownCodeIndex, ExitIndex}.intersection(
|
|
247
263
|
block.succ_blocks
|
|
248
264
|
):
|
|
249
265
|
continue
|
|
250
|
-
|
|
251
|
-
outputs = apply_operation(
|
|
252
|
-
self.reaching_defs[index], index, self.operations[index]
|
|
253
|
-
)
|
|
266
|
+
outputs = self._definitions_before(block_index, block.end)
|
|
254
267
|
for byte, defs in outputs.items():
|
|
255
268
|
if byte.kind == VarKinds.Unique:
|
|
256
269
|
# Ignore P-Code temporary
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
from pcode_graph.analysis.program import (
|
|
2
|
+
EntryIndex,
|
|
3
|
+
OpIndex,
|
|
4
|
+
Program,
|
|
5
|
+
Var,
|
|
6
|
+
VarKinds,
|
|
7
|
+
split,
|
|
8
|
+
wrap,
|
|
9
|
+
)
|
|
10
|
+
from tests.helpers import make_program_from_asm
|
|
11
|
+
from .fixtures import ArchContext, arm64, x86_64
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def reference_reaching_defs(program: Program) -> list[dict[Var, set[OpIndex]]]:
|
|
15
|
+
"""Naive operation-level fixed point, storing all definitions for each operation."""
|
|
16
|
+
|
|
17
|
+
entry = {
|
|
18
|
+
byte: {EntryIndex}
|
|
19
|
+
for index in range(len(program.operations))
|
|
20
|
+
if program.reachable(index)
|
|
21
|
+
for var in program.iter_inputs(index)
|
|
22
|
+
if var.kind == VarKinds.Register
|
|
23
|
+
for byte in split(var)
|
|
24
|
+
}
|
|
25
|
+
inputs: list[dict[Var, set[OpIndex]]] = [{} for _ in program.operations]
|
|
26
|
+
changed = True
|
|
27
|
+
while changed:
|
|
28
|
+
changed = False
|
|
29
|
+
for index, op in enumerate(program.operations):
|
|
30
|
+
if not program.reachable(index):
|
|
31
|
+
continue
|
|
32
|
+
merged: dict[Var, set[OpIndex]] = {}
|
|
33
|
+
for pred in program.pred_ops(index):
|
|
34
|
+
if pred == EntryIndex:
|
|
35
|
+
pred_outputs = entry
|
|
36
|
+
else:
|
|
37
|
+
pred_outputs = dict(inputs[pred])
|
|
38
|
+
output = program.operations[pred].output
|
|
39
|
+
if output:
|
|
40
|
+
pred_outputs.update(
|
|
41
|
+
{byte: {pred} for byte in split(wrap(output))}
|
|
42
|
+
)
|
|
43
|
+
for byte, defs in pred_outputs.items():
|
|
44
|
+
merged.setdefault(byte, set()).update(defs)
|
|
45
|
+
if merged != inputs[index]:
|
|
46
|
+
inputs[index] = merged
|
|
47
|
+
changed = True
|
|
48
|
+
return inputs
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def check_against_reference(program: Program) -> None:
|
|
52
|
+
reference = reference_reaching_defs(program)
|
|
53
|
+
for index in range(len(program.operations)):
|
|
54
|
+
for var in program.iter_inputs(index):
|
|
55
|
+
expected = set().union(
|
|
56
|
+
*(reference[index].get(byte, set()) for byte in split(var))
|
|
57
|
+
)
|
|
58
|
+
assert program.get_defs(index, var) == expected, (index, var)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def test_matches_reference_with_shared_temporaries(x86_64: ArchContext):
|
|
62
|
+
program = make_program_from_asm(
|
|
63
|
+
"""
|
|
64
|
+
loop:
|
|
65
|
+
cmovz rax, rbx
|
|
66
|
+
bsf rcx, rax
|
|
67
|
+
mov qword ptr [rsp + 8], rcx
|
|
68
|
+
mov dl, byte ptr [rsp + 8]
|
|
69
|
+
sub rbx, 1
|
|
70
|
+
jnz loop
|
|
71
|
+
ret
|
|
72
|
+
""",
|
|
73
|
+
x86_64,
|
|
74
|
+
)
|
|
75
|
+
# cmovz and bsf use P-Code temporaries across their internal blocks
|
|
76
|
+
assert any(
|
|
77
|
+
byte.kind == VarKinds.Unique
|
|
78
|
+
for inputs in program.block_inputs
|
|
79
|
+
for byte in inputs
|
|
80
|
+
)
|
|
81
|
+
check_against_reference(program)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def test_matches_reference_on_arm64(arm64: ArchContext):
|
|
85
|
+
program = make_program_from_asm(
|
|
86
|
+
"""
|
|
87
|
+
loop:
|
|
88
|
+
csel x0, x1, x2, eq
|
|
89
|
+
add w1, w1, w0
|
|
90
|
+
cbz x0, done
|
|
91
|
+
subs x2, x2, #1
|
|
92
|
+
b.ne loop
|
|
93
|
+
done:
|
|
94
|
+
ret
|
|
95
|
+
""",
|
|
96
|
+
arm64,
|
|
97
|
+
)
|
|
98
|
+
check_against_reference(program)
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import random
|
|
2
|
+
|
|
3
|
+
from pcode_graph.analysis.alias_analysis import (
|
|
4
|
+
DefinitionSet,
|
|
5
|
+
MemoryAccess,
|
|
6
|
+
MemoryByte,
|
|
7
|
+
MemoryState,
|
|
8
|
+
UnknownWriteIndex,
|
|
9
|
+
)
|
|
10
|
+
from pcode_graph.analysis.interning import Interner
|
|
11
|
+
|
|
12
|
+
ALL_BYTES: list[MemoryByte] = [
|
|
13
|
+
(in_stack, offset) for in_stack in (True, False) for offset in range(8)
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def random_state(
|
|
18
|
+
generator: random.Random, intern: Interner[DefinitionSet]
|
|
19
|
+
) -> MemoryState:
|
|
20
|
+
def random_definitions() -> DefinitionSet:
|
|
21
|
+
return intern(frozenset(generator.sample(range(6), generator.randint(0, 3))))
|
|
22
|
+
|
|
23
|
+
tracked = {
|
|
24
|
+
(generator.random() < 0.5, generator.randint(0, 5)): random_definitions()
|
|
25
|
+
for _ in range(generator.randint(0, 6))
|
|
26
|
+
}
|
|
27
|
+
return MemoryState(tracked, random_definitions())
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def definitions_of(state: MemoryState) -> dict[MemoryByte, DefinitionSet]:
|
|
31
|
+
return {byte: state.tracked.get(byte, state.default) for byte in ALL_BYTES}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def test_merge_matches_union_of_definitions():
|
|
35
|
+
generator = random.Random(0)
|
|
36
|
+
intern: Interner[DefinitionSet] = Interner()
|
|
37
|
+
for _ in range(20000):
|
|
38
|
+
state = random_state(generator, intern)
|
|
39
|
+
other = random_state(generator, intern)
|
|
40
|
+
before = definitions_of(state)
|
|
41
|
+
other_before = definitions_of(other)
|
|
42
|
+
expected = {
|
|
43
|
+
byte: definitions | other_before[byte]
|
|
44
|
+
for byte, definitions in before.items()
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
changed = state.merge(other, intern)
|
|
48
|
+
|
|
49
|
+
assert definitions_of(state) == expected
|
|
50
|
+
assert definitions_of(other) == other_before
|
|
51
|
+
assert changed == (before != expected)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def test_write_and_clobber():
|
|
55
|
+
intern: Interner[DefinitionSet] = Interner()
|
|
56
|
+
state = MemoryState({}, intern(frozenset({-1})))
|
|
57
|
+
access = MemoryAccess(True, 2, 4)
|
|
58
|
+
state.write(access, 7, intern)
|
|
59
|
+
copy = state.copy()
|
|
60
|
+
copy.clobber(intern)
|
|
61
|
+
|
|
62
|
+
assert state.read(access) == {7}
|
|
63
|
+
assert state.read(MemoryAccess(True, 0, 4)) == {-1, 7}
|
|
64
|
+
assert copy.read(access) == {7, UnknownWriteIndex}
|
|
65
|
+
assert copy.read(MemoryAccess(False, 0, 1)) == {-1, UnknownWriteIndex}
|
|
@@ -270,6 +270,21 @@ def test_long_stack_pointer_chain(outdir: Path, x86_64: ArchContext, log):
|
|
|
270
270
|
assert len(program.memory.forwarded_loads) == 1
|
|
271
271
|
|
|
272
272
|
|
|
273
|
+
def test_long_chain_defined_after_use(outdir: Path, x86_64: ArchContext, log):
|
|
274
|
+
# The load precedes the stack pointer definitions in program order
|
|
275
|
+
program = make_program_from_asm(
|
|
276
|
+
"jmp body; use: mov rbx, [rsp]; ret; body: mov [rsp], rax;"
|
|
277
|
+
+ "add rsp, 8;" * 2000
|
|
278
|
+
+ "sub rsp, 16000; jmp use",
|
|
279
|
+
x86_64,
|
|
280
|
+
outdir,
|
|
281
|
+
)
|
|
282
|
+
|
|
283
|
+
# Both the mov and the ret read the single store
|
|
284
|
+
assert len(set(map(tuple, program.memory.forwarded_loads.values()))) == 1
|
|
285
|
+
assert len(program.memory.forwarded_loads) == 2
|
|
286
|
+
|
|
287
|
+
|
|
273
288
|
@pytest.mark.parametrize(
|
|
274
289
|
"arch_fixture", ["x86_64", "x86_32", "arm64", "arm32", "mips_64", "mips_32"]
|
|
275
290
|
)
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import heapq
|
|
2
|
-
|
|
3
|
-
from .pass_base import PassBase
|
|
4
|
-
from .program import (
|
|
5
|
-
BasicBlock,
|
|
6
|
-
BlockIndex,
|
|
7
|
-
Definitions,
|
|
8
|
-
EntryIndex,
|
|
9
|
-
Var,
|
|
10
|
-
VarKinds,
|
|
11
|
-
wrap,
|
|
12
|
-
split,
|
|
13
|
-
apply_operation,
|
|
14
|
-
)
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
class ReachingDefBuilder(PassBase):
|
|
18
|
-
|
|
19
|
-
def __call__(self):
|
|
20
|
-
self.program.reaching_defs = [{} for _ in self.program.operations]
|
|
21
|
-
if not self.program.basic_blocks:
|
|
22
|
-
return
|
|
23
|
-
|
|
24
|
-
block_inputs: list[Definitions] = [{} for _ in self.program.basic_blocks]
|
|
25
|
-
|
|
26
|
-
# Initialize
|
|
27
|
-
for index in range(len(self.program.operations)):
|
|
28
|
-
if not self.program.reachable(index):
|
|
29
|
-
continue
|
|
30
|
-
for var in self.program.iter_inputs(index):
|
|
31
|
-
if var.kind == VarKinds.Register:
|
|
32
|
-
for byte in split(var):
|
|
33
|
-
block_inputs[0][byte] = {EntryIndex}
|
|
34
|
-
|
|
35
|
-
# Block effects are computed once, on demand, as (generated defs, killed variables)
|
|
36
|
-
effects: dict[BlockIndex, tuple[Definitions, set[Var]]] = {}
|
|
37
|
-
|
|
38
|
-
# Each reachable block is processed at least once, lowest index first:
|
|
39
|
-
# follows the code layout, close to a reverse post-order.
|
|
40
|
-
# The list is sorted, so it is already a valid heap.
|
|
41
|
-
pending = [
|
|
42
|
-
index
|
|
43
|
-
for index, block in enumerate(self.program.basic_blocks)
|
|
44
|
-
if block.reachable
|
|
45
|
-
]
|
|
46
|
-
queued = set(pending)
|
|
47
|
-
|
|
48
|
-
while pending:
|
|
49
|
-
block_index = heapq.heappop(pending)
|
|
50
|
-
queued.remove(block_index)
|
|
51
|
-
|
|
52
|
-
if block_index not in effects:
|
|
53
|
-
effects[block_index] = self._compute_effect(
|
|
54
|
-
self.program.basic_blocks[block_index]
|
|
55
|
-
)
|
|
56
|
-
generated, killed = effects[block_index]
|
|
57
|
-
outputs = self._apply_effect(block_inputs[block_index], generated, killed)
|
|
58
|
-
|
|
59
|
-
for successor in self.program.basic_blocks[block_index].succ_blocks:
|
|
60
|
-
if successor < 0:
|
|
61
|
-
continue
|
|
62
|
-
changed = self._merge_definitions(block_inputs[successor], outputs)
|
|
63
|
-
if changed and successor not in queued:
|
|
64
|
-
heapq.heappush(pending, successor)
|
|
65
|
-
queued.add(successor)
|
|
66
|
-
|
|
67
|
-
for block, inputs in zip(self.program.basic_blocks, block_inputs):
|
|
68
|
-
if block.reachable:
|
|
69
|
-
self._expand_block(block, inputs)
|
|
70
|
-
|
|
71
|
-
def _compute_effect(self, block: BasicBlock) -> tuple[Definitions, set[Var]]:
|
|
72
|
-
generated: Definitions = {}
|
|
73
|
-
killed: set[Var] = set()
|
|
74
|
-
for index in block.op_indexes:
|
|
75
|
-
op = self.program.operations[index]
|
|
76
|
-
if op.output:
|
|
77
|
-
written = {index}
|
|
78
|
-
for byte in split(wrap(op.output)):
|
|
79
|
-
generated[byte] = written
|
|
80
|
-
killed.add(byte)
|
|
81
|
-
return generated, killed
|
|
82
|
-
|
|
83
|
-
@staticmethod
|
|
84
|
-
def _apply_effect(
|
|
85
|
-
inputs: Definitions, generated: Definitions, killed: set[Var]
|
|
86
|
-
) -> Definitions:
|
|
87
|
-
# Sets are never modified in place, so they can be shared
|
|
88
|
-
outputs = dict(generated)
|
|
89
|
-
for var, defs in inputs.items():
|
|
90
|
-
if var in killed:
|
|
91
|
-
continue
|
|
92
|
-
outputs[var] = outputs[var] | defs if var in outputs else defs
|
|
93
|
-
return outputs
|
|
94
|
-
|
|
95
|
-
def _expand_block(self, block: BasicBlock, inputs: Definitions):
|
|
96
|
-
"""Computes the definitions reaching each operation of the block."""
|
|
97
|
-
|
|
98
|
-
current = inputs
|
|
99
|
-
for index in block.op_indexes:
|
|
100
|
-
self.program.reaching_defs[index] = current
|
|
101
|
-
current = apply_operation(current, index, self.program.operations[index])
|
|
102
|
-
|
|
103
|
-
@staticmethod
|
|
104
|
-
def _merge_definitions(target: Definitions, source: Definitions) -> bool:
|
|
105
|
-
"""Adds source definitions to target ones, returns True if target changed."""
|
|
106
|
-
|
|
107
|
-
changed = False
|
|
108
|
-
for var, defs in source.items():
|
|
109
|
-
target_defs = target.get(var)
|
|
110
|
-
if target_defs is None:
|
|
111
|
-
target[var] = set(defs)
|
|
112
|
-
changed = True
|
|
113
|
-
elif not defs <= target_defs:
|
|
114
|
-
target_defs |= defs
|
|
115
|
-
changed = True
|
|
116
|
-
return changed
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|