pcode-graph 4.1.0__py3-none-any.whl → 4.2.1__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/analysis/alias_analysis.py +106 -49
- pcode_graph/analysis/def_use.py +63 -64
- pcode_graph/analysis/interning.py +11 -0
- pcode_graph/analysis/program.py +42 -29
- pcode_graph/characterization.py +0 -1
- {pcode_graph-4.1.0.dist-info → pcode_graph-4.2.1.dist-info}/METADATA +1 -1
- {pcode_graph-4.1.0.dist-info → pcode_graph-4.2.1.dist-info}/RECORD +10 -9
- {pcode_graph-4.1.0.dist-info → pcode_graph-4.2.1.dist-info}/WHEEL +0 -0
- {pcode_graph-4.1.0.dist-info → pcode_graph-4.2.1.dist-info}/entry_points.txt +0 -0
- {pcode_graph-4.1.0.dist-info → pcode_graph-4.2.1.dist-info}/licenses/LICENSE +0 -0
|
@@ -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
|
pcode_graph/analysis/def_use.py
CHANGED
|
@@ -1,39 +1,67 @@
|
|
|
1
1
|
import heapq
|
|
2
2
|
|
|
3
|
+
from .interning import Interner
|
|
3
4
|
from .pass_base import PassBase
|
|
4
5
|
from .program import (
|
|
5
6
|
BasicBlock,
|
|
6
|
-
BlockIndex,
|
|
7
7
|
Definitions,
|
|
8
8
|
EntryIndex,
|
|
9
|
+
LocalWriters,
|
|
10
|
+
OpIndex,
|
|
9
11
|
Var,
|
|
10
12
|
VarKinds,
|
|
11
13
|
wrap,
|
|
12
14
|
split,
|
|
13
|
-
apply_operation,
|
|
14
15
|
)
|
|
15
16
|
|
|
17
|
+
DefinitionInterner = Interner[frozenset[OpIndex]]
|
|
16
18
|
|
|
17
|
-
class ReachingDefBuilder(PassBase):
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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:
|
|
22
34
|
return
|
|
23
35
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
for
|
|
28
|
-
if not
|
|
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:
|
|
29
41
|
continue
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
+
]
|
|
37
65
|
|
|
38
66
|
# Each reachable block is processed at least once, lowest index first:
|
|
39
67
|
# follows the code layout, close to a reverse post-order.
|
|
@@ -49,68 +77,39 @@ class ReachingDefBuilder(PassBase):
|
|
|
49
77
|
block_index = heapq.heappop(pending)
|
|
50
78
|
queued.remove(block_index)
|
|
51
79
|
|
|
52
|
-
|
|
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
|
-
|
|
80
|
+
outputs = block_inputs[block_index] | generated_by_block[block_index]
|
|
59
81
|
for successor in self.program.basic_blocks[block_index].succ_blocks:
|
|
60
82
|
if successor < 0:
|
|
61
83
|
continue
|
|
62
|
-
changed = self._merge_definitions(
|
|
84
|
+
changed = self._merge_definitions(
|
|
85
|
+
block_inputs[successor], outputs, intern
|
|
86
|
+
)
|
|
63
87
|
if changed and successor not in queued:
|
|
64
88
|
heapq.heappush(pending, successor)
|
|
65
89
|
queued.add(successor)
|
|
66
90
|
|
|
67
|
-
|
|
68
|
-
|
|
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
|
|
91
|
+
def _collect_writers(self, block: BasicBlock) -> LocalWriters:
|
|
92
|
+
writers: LocalWriters = {}
|
|
99
93
|
for index in block.op_indexes:
|
|
100
|
-
self.program.
|
|
101
|
-
|
|
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
|
|
102
99
|
|
|
103
100
|
@staticmethod
|
|
104
|
-
def _merge_definitions(
|
|
101
|
+
def _merge_definitions(
|
|
102
|
+
target: Definitions, source: Definitions, intern: DefinitionInterner
|
|
103
|
+
) -> bool:
|
|
105
104
|
"""Adds source definitions to target ones, returns True if target changed."""
|
|
106
105
|
|
|
107
106
|
changed = False
|
|
108
107
|
for var, defs in source.items():
|
|
109
108
|
target_defs = target.get(var)
|
|
110
109
|
if target_defs is None:
|
|
111
|
-
target[var] =
|
|
110
|
+
target[var] = defs
|
|
112
111
|
changed = True
|
|
113
|
-
elif not defs <= target_defs:
|
|
114
|
-
target_defs
|
|
112
|
+
elif target_defs is not defs and not defs <= target_defs:
|
|
113
|
+
target[var] = intern(target_defs | defs)
|
|
115
114
|
changed = True
|
|
116
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)
|
pcode_graph/analysis/program.py
CHANGED
|
@@ -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
|
pcode_graph/characterization.py
CHANGED
|
@@ -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
|
|
@@ -4,7 +4,7 @@ pcode_graph/arch.py,sha256=5_nRqA2sgz7Ne_8M7_NT9YDUftB88EeOSt1o0ARhksg,324
|
|
|
4
4
|
pcode_graph/asm.py,sha256=qXG8hkVbhD9AicjOMEjAWUt6UxpHZJkYhijaWgmou6A,1752
|
|
5
5
|
pcode_graph/basic_blocks.py,sha256=bgVkXS2lNWsCxsVM8L7vQQdgZuhW_s61XBQ0iliknXQ,7289
|
|
6
6
|
pcode_graph/bit_utils.py,sha256=rEEfiJevPc-QMYVryZVwWPBOufC3MyA3bZZ8y9Q4aLU,405
|
|
7
|
-
pcode_graph/characterization.py,sha256=
|
|
7
|
+
pcode_graph/characterization.py,sha256=2JcM6wxCABaItCxKUnXDkGQj1SLgTvmtxLfnGqFPUg8,3183
|
|
8
8
|
pcode_graph/checker.py,sha256=OieaYXDnk0QHhYVO12ckzCNPH16DQ3vb0Q9pOO7TR-0,9293
|
|
9
9
|
pcode_graph/cli.py,sha256=eVGxXeg4QUf8MRfs7df3sqTf9iV5w501HQNWHdZ8F1o,7143
|
|
10
10
|
pcode_graph/emulator.py,sha256=7r1aMoTKDc6j_cT8V2vCcts4Mg2FMmvdoP8Y3H12RDY,13366
|
|
@@ -21,17 +21,18 @@ pcode_graph/torch_utils.py,sha256=GwVjWKL5Lqhm94b69o-rCilzNRu237gyJo6T6xWQHQc,96
|
|
|
21
21
|
pcode_graph/translator.py,sha256=h90ay8VnCsgWRpK9fcYa0iu6CKXY0YDKl8CtH_gIx3U,7619
|
|
22
22
|
pcode_graph/view.py,sha256=Qj935NNo7n0-ecGMusEy_QLb6yx3pckLAbztji9Po2U,2452
|
|
23
23
|
pcode_graph/visitor.py,sha256=bxaQ9q0lTw8ysPOrta5G7LeHZYnq3IBxMafM_-8FruM,6990
|
|
24
|
-
pcode_graph/analysis/alias_analysis.py,sha256=
|
|
24
|
+
pcode_graph/analysis/alias_analysis.py,sha256=7GcN62AYvRN2nI1iUBoUeGQExSWPNnHEw4Kqhx0dVM4,12429
|
|
25
25
|
pcode_graph/analysis/cfg.py,sha256=PhMLzMcNhvNTNjO7MvhffKFUWzG_UFf8tO7r5hlQ_Zc,4484
|
|
26
|
-
pcode_graph/analysis/def_use.py,sha256=
|
|
26
|
+
pcode_graph/analysis/def_use.py,sha256=XkBZpb4aaO1gJMdQDbd-kjsR-MbcPUnYKChzbAX_YX4,4300
|
|
27
|
+
pcode_graph/analysis/interning.py,sha256=a6RcoeYH6JeoX-bAsebAjnFDzx8N5kTJPXsq8QLygAc,322
|
|
27
28
|
pcode_graph/analysis/load_store.py,sha256=SOgDN4gaKf4S4fUFIoCsCB65LAICv-hZAWwTFSIOT4I,6054
|
|
28
29
|
pcode_graph/analysis/native_pcode.py,sha256=u7kESyKKSgoUWQwWGgSxlBe19RTbTJDArGWoCMU5_Jo,1957
|
|
29
30
|
pcode_graph/analysis/pass_base.py,sha256=IeG5NiEz_Mez-ftjPA2GmC_oT7j7gPAQ6fRClGhce2E,235
|
|
30
31
|
pcode_graph/analysis/passes.py,sha256=aRzbEqP5uoXZxPcwQppMOZQencOhI8A6xUbyEQoeEKQ,429
|
|
31
|
-
pcode_graph/analysis/program.py,sha256=
|
|
32
|
+
pcode_graph/analysis/program.py,sha256=VjSSgoASXma7Oj0nSXRGJdpnDmPmHKzMatBZC5HRD_M,10097
|
|
32
33
|
pcode_graph/analysis/unreachable.py,sha256=Poza5XjP5Nf9fAR_5srGLU2E0Jsl7pmk3m3QEYBgDgo,505
|
|
33
|
-
pcode_graph-4.1.
|
|
34
|
-
pcode_graph-4.1.
|
|
35
|
-
pcode_graph-4.1.
|
|
36
|
-
pcode_graph-4.1.
|
|
37
|
-
pcode_graph-4.1.
|
|
34
|
+
pcode_graph-4.2.1.dist-info/METADATA,sha256=HOz5Cpu8kZck45w9MWT7W3Up7dQPHz3mXvJKKEFtSYs,6675
|
|
35
|
+
pcode_graph-4.2.1.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
36
|
+
pcode_graph-4.2.1.dist-info/entry_points.txt,sha256=7pTTgqjYSRYwmBdA0GwJYXPfOmdk8I_mGsJOHHx-PXQ,45
|
|
37
|
+
pcode_graph-4.2.1.dist-info/licenses/LICENSE,sha256=iVkUrjpGBX_59jOK6a7mKkvjHUCGfiCFQRgrziEh-nY,1066
|
|
38
|
+
pcode_graph-4.2.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|