scientific-method-engine 7.0.0__tar.gz → 7.2.0__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.
Files changed (68) hide show
  1. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/PKG-INFO +1 -1
  2. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/pyproject.toml +1 -1
  3. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/x86/argument_frames.py +57 -57
  4. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/x86/machine.py +44 -7
  5. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/x86/reports.py +145 -19
  6. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/x86/trace.py +26 -4
  7. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/tests/test_pe.py +114 -0
  8. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/tests/test_x86.py +149 -2
  9. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/.gitignore +0 -0
  10. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/LICENSE +0 -0
  11. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/README.md +0 -0
  12. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/__init__.py +0 -0
  13. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/__main__.py +0 -0
  14. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/cli.py +0 -0
  15. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ClearNoReturnFunctions.java +0 -0
  16. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/CreateFunctions.java +0 -0
  17. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ExportBoundedFlow.java +0 -0
  18. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ExportCallEdges.java +0 -0
  19. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ExportFunctionFingerprints.java +0 -0
  20. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ExportFunctionInventory.java +0 -0
  21. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/MergeFallThroughFragment.java +0 -0
  22. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/RecoverCitedFunctions.java +0 -0
  23. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/RepairReturningCallers.java +0 -0
  24. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ReportCallArguments.java +0 -0
  25. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ReportCallPaths.java +0 -0
  26. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ReportCallSitesWithScalars.java +0 -0
  27. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ReportCallsToRange.java +0 -0
  28. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ReportConstantFirstArgumentCalls.java +0 -0
  29. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ReportDataBytes.java +0 -0
  30. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ReportDecompileMatches.java +0 -0
  31. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ReportDecompileWindow.java +0 -0
  32. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ReportFilePatternInMemory.java +0 -0
  33. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ReportFirstArgumentCallSummary.java +0 -0
  34. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ReportFunctionScalarConstants.java +0 -0
  35. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ReportFunctionSummary.java +0 -0
  36. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ReportInstructionContext.java +0 -0
  37. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ReportInstructionWindow.java +0 -0
  38. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ReportMemoryBlockForFileOffset.java +0 -0
  39. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ReportMemoryBlocks.java +0 -0
  40. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ReportRandomnessCandidates.java +0 -0
  41. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ReportReferences.java +0 -0
  42. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ReportScalarConstants.java +0 -0
  43. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ReportStringReferences.java +0 -0
  44. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/ReportSymbolReferences.java +0 -0
  45. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/ghidra/scientificmethod/FirstArgumentLookBack.java +0 -0
  46. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/x86/__init__.py +0 -0
  47. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/x86/dispatch.py +0 -0
  48. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/x86/effect_order.py +0 -0
  49. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/x86/image.py +0 -0
  50. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/x86/loops.py +0 -0
  51. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/x86/memory_scopes.py +0 -0
  52. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/x86/pcode.py +0 -0
  53. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/x86/pcode_backend.py +0 -0
  54. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/x86/pe.py +0 -0
  55. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/x86/relational.py +0 -0
  56. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/x86/result_flow.py +0 -0
  57. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/src/scientific_method_engine/x86/values.py +0 -0
  58. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/tests/oracle.py +0 -0
  59. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/tests/test_continuation_budget.py +0 -0
  60. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/tests/test_dispatch.py +0 -0
  61. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/tests/test_effect_order.py +0 -0
  62. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/tests/test_entry_frame.py +0 -0
  63. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/tests/test_loops.py +0 -0
  64. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/tests/test_memory_scopes.py +0 -0
  65. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/tests/test_nested_frame_request.py +0 -0
  66. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/tests/test_oracle.py +0 -0
  67. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/tests/test_relational_controls.py +0 -0
  68. {scientific_method_engine-7.0.0 → scientific_method_engine-7.2.0}/tests/test_table_continuations.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: scientific-method-engine
3
- Version: 7.0.0
3
+ Version: 7.2.0
4
4
  Summary: Bounded instruction-derived x86 evidence reports for segmented MZ/FBOV and PE32/i386 code.
5
5
  Project-URL: Source, https://github.com/kibertoad/refurbished-dinosaurs-toolkit/tree/main/packages/scientific-method-engine
6
6
  Author: kibertoad
@@ -5,7 +5,7 @@ build-backend = "hatchling.build"
5
5
  [project]
6
6
  name = "scientific-method-engine"
7
7
  # The release workflow writes the published version from the package's release tag.
8
- version = "7.0.0"
8
+ version = "7.2.0"
9
9
  description = "Bounded instruction-derived x86 evidence reports for segmented MZ/FBOV and PE32/i386 code."
10
10
  readme = "README.md"
11
11
  requires-python = ">=3.12"
@@ -7,9 +7,6 @@ amount never join or split slots. Whatever a read did not settle on the path sta
7
7
  """
8
8
  from capstone.x86 import X86_OP_IMM, X86_OP_REG
9
9
 
10
- from .machine import may_alias, written_domain
11
- from .memory_scopes import model_scopes
12
-
13
10
  # The argument bytes one frame maps; a wider frame is mapped up to here and stays open.
14
11
  WINDOW_BYTES = 256
15
12
 
@@ -39,60 +36,43 @@ def _covered(event, segment, base, start, width, modulus):
39
36
  return [at for at in offsets if 0 <= at < width]
40
37
 
41
38
 
42
- def _writers(events, before, segment, base, start, width, modulus, image, models):
43
- """The last write before index `before` that covered each frame byte, and why each byte without one has none.
39
+ def _slot_states(events, recorded, width):
40
+ """The write that last stored each frame byte when the call ran, and why each byte without one has none.
44
41
 
45
- A modeled call drops every byte outside its `preservesMemory` scopes. Scopes name linear bytes,
46
- so they keep only frame bytes of a linear stack; the search goes on past the call for those.
47
- A write through another segment or base may have stored every frame byte it may alias, whether
48
- or not it dropped a cached byte. The rule is the one the machine's ``unwritten`` applies, so a
49
- slot and a callee read of it that runs before any callee write through another segment or base
50
- name the same write.
42
+ `recorded` is the call event's ``argumentSlots``, from ``State.argument_slots``: the machine's own
43
+ record of each byte, so a slot and a callee read of it that runs before any callee write through
44
+ another segment or base name the same write.
51
45
  """
52
- writers, invalidated = {}, {}
53
- keys = [(segment, base, (start + at) % modulus if modulus else start + at) for at in range(width)]
54
- for j in range(before - 1, -1, -1):
55
- event = events[j]
56
- interval = event.get("interval")
57
- if event["kind"] == "write" and interval and (interval["segment"], interval["base"]) != (segment, base):
58
- written = written_domain(interval["segment"], interval["base"], interval["start"],
59
- interval["end"] - interval["start"], image.bits, image.flat)
60
- for at, key in enumerate(keys):
61
- if at not in writers and at not in invalidated and may_alias(key, written, image.bits, image.flat):
62
- invalidated[at] = f"memory possibly overwritten through another address by the write at {event['site']}"
63
- if event["kind"] == "call-return" and event.get("unknownMemoryEffects"):
64
- kept = set()
65
- if segment == ("linear",):
66
- kept = {at for scope in model_scopes(models, event)
67
- for at in range(scope["linearStart"], scope["linearEnd"])}
68
- for at in range(width):
69
- if at not in writers and at not in invalidated and start + at not in kept:
70
- invalidated[at] = "memory invalidated by the modeled call at " + str(event["callSite"])
71
- for at in _covered(event, segment, base, start, width, modulus):
72
- if at not in invalidated:
73
- writers.setdefault(at, event)
74
- if len(writers) + len(invalidated) == width:
46
+ writers, reasons = {}, {}
47
+ for offset, run, order, unwritten in recorded["runs"]:
48
+ if offset >= width:
75
49
  break
76
- return writers, {at: invalidated.get(at, "no write on this path") for at in range(width) if at not in writers}
50
+ target, value = (writers, events[order]) if unwritten is None else (reasons, _reason(events, unwritten))
51
+ for at in range(offset, min(offset + run, width)):
52
+ target[at] = value
53
+ return writers, reasons
54
+
77
55
 
56
+ def _reason(events, unwritten):
57
+ """A slot's reason for a byte's ``unwritten`` cause, naming the event's site."""
58
+ cause, order = unwritten["cause"], unwritten["order"]
59
+ if cause in ("possibly written by an aliasing write", "dropped by a possibly aliasing write"):
60
+ return f"memory possibly overwritten through another address by the write at {events[order]['site']}"
61
+ if cause == "dropped by a modeled call":
62
+ return "memory invalidated by the modeled call at " + str(events[order]["callSite"])
63
+ return cause
78
64
 
79
- def _frame(image, events, index, models):
65
+
66
+ def _frame(image, events, index):
80
67
  call = events[index]
81
68
  depth = call["depth"]
82
- pushes = [e for e in events[index + 1:index + 3] if e["kind"] == "write" and e.get("role") == "push" and e["site"] == call["site"]]
69
+ recorded = call.pop("argumentSlots")
83
70
  end = next((j for j in range(index + 1, len(events))
84
71
  if events[j]["kind"] == "call-return" and events[j].get("callSite") == call["site"] and events[j]["depth"] == depth), None)
85
72
  inside = events[index + 1:end if end is not None else len(events)]
86
73
  open_reasons = []
87
- if not pushes:
88
- return None
89
- # The return-address push comes last and sits at the callee's entry SP; the frame's return bytes lie above it.
90
- entry = pushes[-1]["interval"]
91
- segment, base = entry["segment"], entry["base"]
74
+ segment, base, start = recorded["segment"], recorded["base"], recorded["start"]
92
75
  modulus = None if segment == ("linear",) else 1 << image.bits
93
- start = entry["start"] + call["returnFrameBytes"]
94
- if modulus:
95
- start %= modulus
96
76
  reads = [e for e in inside if e["kind"] == "read" and e.get("argument") and e["depth"] == depth + 1]
97
77
  returned = next((e for e in inside if e["kind"] == "return" and e["depth"] == depth + 1 and e.get("callSite") == call["site"]), None)
98
78
  callee_cleanup = returned["cleanupBytes"] if returned else None
@@ -112,7 +92,7 @@ def _frame(image, events, index, models):
112
92
 
113
93
  # Slots: runs of argument bytes that one write event last covered.
114
94
  slots, by_byte = [], {}
115
- writers, reasons = _writers(events, index, segment, base, start, width, modulus, image, models)
95
+ writers, reasons = _slot_states(events, recorded, width)
116
96
  for at in range(width):
117
97
  writer = writers.get(at)
118
98
  key = writer["order"] if writer else reasons[at]
@@ -202,36 +182,56 @@ def argument_frames(report, image):
202
182
 
203
183
  A path gets one map per traced call, from caller-written slots to callee reads. A site's
204
184
  groupings agree only when every path that traced a call there settled on the same read widths
205
- and groupings. Separately, a site's widths are consistent when its paths made at least one read
206
- and no two reads on any of them cover the same byte with a different offset, width or grouping;
207
- a path that skipped a read leaves the site consistent but not agreed.
185
+ and groupings. Separately, a site's widths are consistent when its paths read at least one byte
186
+ the caller wrote and no two reads with a different offset, width or grouping both saw one shared
187
+ byte from the caller; a path that skipped a read leaves the site consistent but not agreed.
188
+ A read that saw bytes the callee stored itself, or bytes with no known caller writer, stays
189
+ listed and marked with the paths it was made on that way, and its conflicts stay listed, but
190
+ those bytes do not decide whether the widths are consistent. The report must come from
191
+ ``trace`` with ``argument_window=WINDOW_BYTES``: each traced call's slots come from its
192
+ ``argumentSlots`` record, which this removes from every path, declared continuations included.
208
193
  """
209
194
  sites = {}
210
195
  for path_index, path in enumerate(report["paths"]):
211
196
  events = path["events"]
212
- frames = [_frame(image, events, i, path["conditionalModels"]) for i, e in enumerate(events) if e["kind"] == "call" and "returnFrameBytes" in e]
213
- path["argumentFrames"] = [f for f in frames if f is not None]
197
+ path["argumentFrames"] = [_frame(image, events, i) for i, e in enumerate(events) if e["kind"] == "call" and "returnFrameBytes" in e]
214
198
  for frame in path["argumentFrames"]:
215
199
  sites.setdefault(frame["callSite"], []).append((path_index, frame))
200
+ # Continuations get no frames; their calls' slot records are dropped unread.
201
+ for path in report.get("declaredContinuationPaths", ()):
202
+ for event in path["events"]:
203
+ event.pop("argumentSlots", None)
216
204
  report["argumentFrameSites"] = []
217
205
  for site, rows in sorted(sites.items()):
218
206
  # A set holds each read's grouping too: a far-pointer load and a plain dword over the same bytes disagree.
219
207
  widths = sorted({tuple(sorted({(g["offset"], g["width"], g["grouping"]) for g in f["groupings"]})) for _, f in rows})
220
- read_on = {}
208
+ # Each read's paths, split by whether every byte it saw came from the caller's slot writer, and the
209
+ # frame bytes it saw from the slot writer on any path.
210
+ read_on, from_caller, not_from_caller, caller_bytes = {}, {}, {}, {}
221
211
  for i, f in rows:
222
212
  for g in f["groupings"]:
223
- read_on.setdefault((g["offset"], g["width"], g["grouping"]), set()).add(i)
213
+ key = (g["offset"], g["width"], g["grouping"])
214
+ read_on.setdefault(key, set()).add(i)
215
+ (not_from_caller if g["bytesNotFromSlotWriter"] else from_caller).setdefault(key, set()).add(i)
216
+ mapped = range(min(g["width"], f["mappedBytes"] - g["offset"]))
217
+ caller_bytes.setdefault(key, set()).update(g["offset"] + at for at in mapped if at not in g["bytesNotFromSlotWriter"])
224
218
  reads = sorted(read_on)
225
219
  # Distinct reads that share a byte conflict: different intervals, or one interval grouped two ways.
226
- conflicting = [[{"offset": o, "width": w, "grouping": k} for o, w, k in (a, b)]
227
- for n, a in enumerate(reads) for b in reads[n + 1:] if _overlap(a, b)]
220
+ pairs = [(a, b) for n, a in enumerate(reads) for b in reads[n + 1:] if _overlap(a, b)]
221
+ conflicting = [[{"offset": o, "width": w, "grouping": k} for o, w, k in pair] for pair in pairs]
222
+ # Only the caller's bytes decide consistency: a pair counts when both reads saw one shared byte from
223
+ # the slot writer. A callee reusing its argument slot as a local neither breaks nor supplies the
224
+ # width the caller's argument is read at, but a read past the caller's bytes still reads them.
225
+ caller_conflict = any(caller_bytes[a] & caller_bytes[b] for a, b in pairs)
228
226
  report["argumentFrameSites"].append({
229
227
  "callSite": site, "paths": sorted({i for i, _ in rows}), "frames": len(rows),
230
228
  "unsettledPaths": sorted({i for i, f in rows if not f["settledOnThisPath"]}),
231
229
  "readWidthSets": [[{"offset": o, "width": w, "grouping": k} for o, w, k in group] for group in widths],
232
- "readWidths": [{"offset": o, "width": w, "grouping": k, "paths": sorted(read_on[(o, w, k)])} for o, w, k in reads],
230
+ "readWidths": [{"offset": o, "width": w, "grouping": k, "paths": sorted(read_on[(o, w, k)]),
231
+ "fromCallerOnPaths": sorted(from_caller.get((o, w, k), ())),
232
+ "notFromCallerOnPaths": sorted(not_from_caller.get((o, w, k), ()))} for o, w, k in reads],
233
233
  "conflictingWidths": conflicting,
234
- "widthsConsistent": bool(reads) and not conflicting,
234
+ "widthsConsistent": any(caller_bytes.values()) and not caller_conflict,
235
235
  "agreed": len(widths) == 1 and all(f["settledOnThisPath"] for _, f in rows),
236
236
  "interpretation": "read widths per traced path; paths that never reached this call are not represented"})
237
237
  return report
@@ -136,6 +136,8 @@ class State:
136
136
  # name they had before the modeled call. They stay unread: no value, no producer.
137
137
  self.unread_memory = {}
138
138
  self.events = []
139
+ # Bytes above each traced call's return frame that the call event records (argument_slots); 0 records none.
140
+ self.argument_window = 0
139
141
  # Value transfers are recorded only for queries that trace declared return results.
140
142
  self.value_transfers = bool(config.get("returnContracts"))
141
143
  self.guards = []
@@ -267,23 +269,30 @@ class State:
267
269
  """The modeled value of one memory byte, or an unknown term produced by the current site."""
268
270
  return self.memory[key] if key in self.memory else unknown(self.unread_term(key), 8, self.at)
269
271
 
270
- def unwritten(self, key):
271
- """Why a byte has no modeled value: the cause and the order of the event behind it."""
272
+ def latest_aliasing_write(self, group, domain):
273
+ """The order of the newest write outside `group` that may have stored a byte of `domain`, or None."""
272
274
  # Writes to the key's own (segment, base) group store other offsets; any other write may alias.
273
- # A later aliasing write is newer than the one that dropped the byte, so it is the one named.
274
- lost = self.lost_memory.get(key)
275
- group, a = key[:2], key_domain(key, self.bits, self.flat)
276
275
  latest = None
277
276
  for other, domains in self.writes.items():
278
277
  if other == group:
279
278
  continue
280
- if a is None:
279
+ if domain is None:
281
280
  # Every write may alias a byte with no concrete domain; the group's newest decides.
282
281
  order = max(domains.values())
283
282
  else:
284
- order = max((o for b, o in domains.items() if b is None or not (a[1] <= b[0] or b[1] <= a[0])), default=None)
283
+ order = max((o for b, o in domains.items() if b is None or not (domain[1] <= b[0] or b[1] <= domain[0])), default=None)
285
284
  if order is not None and (latest is None or order > latest):
286
285
  latest = order
286
+ return latest
287
+
288
+ def unwritten(self, key):
289
+ """Why a byte has no modeled value: the cause and the order of the event behind it."""
290
+ return self._unwritten(key, self.latest_aliasing_write(key[:2], key_domain(key, self.bits, self.flat)))
291
+
292
+ def _unwritten(self, key, latest):
293
+ # `latest` is latest_aliasing_write for the key's group and domain. A later aliasing write is
294
+ # newer than the one that dropped the byte, so it is the one named.
295
+ lost = self.lost_memory.get(key)
287
296
  if latest is not None and (lost is None or lost["order"] is None or latest > lost["order"]):
288
297
  return {"cause": "possibly written by an aliasing write", "order": latest}
289
298
  if lost is not None:
@@ -298,6 +307,34 @@ class State:
298
307
  return {"index": index, "producers": producers(self.memory[key]), "writeOrder": self.memory_writers.get(key)}
299
308
  return {"index": index, "producers": [], "writeOrder": None, "unwritten": self.unwritten(key)}
300
309
 
310
+ def argument_slots(self, return_bytes, window):
311
+ """The stack bytes above a call's return frame as a read of them would see them now.
312
+
313
+ Called right after a traced call pushed its return frame. Covers `window` bytes from the first
314
+ byte above the return frame and returns ``{"segment", "base", "start", "runs"}``: `start` is
315
+ that byte's offset in the stack's (segment, base) group, and each run is
316
+ ``(offset, width, writeOrder, unwritten)`` for adjacent bytes that ``byte_writer`` would give
317
+ the same write order, or the same ``unwritten`` cause and order.
318
+ """
319
+ segment, base, delta = self.location(self.segment("ss"), self.reg(self.sp))
320
+ linear = segment == ("linear",)
321
+ start = delta + return_bytes if linear else (delta + return_bytes) % (1 << self.bits)
322
+ runs, aliasing = [], {}
323
+ for at in range(window):
324
+ key = (segment, base, start + at if linear else (start + at) % (1 << self.bits))
325
+ if key in self.memory:
326
+ seen = (self.memory_writers.get(key), None)
327
+ else:
328
+ domain = key_domain(key, self.bits, self.flat)
329
+ if domain not in aliasing:
330
+ aliasing[domain] = self.latest_aliasing_write(key[:2], domain)
331
+ seen = (None, self._unwritten(key, aliasing[domain]))
332
+ if runs and runs[-1][2:] == seen:
333
+ runs[-1] = (runs[-1][0], runs[-1][1] + 1, *seen)
334
+ else:
335
+ runs.append((at, 1, *seen))
336
+ return {"segment": segment, "base": base, "start": start, "runs": runs}
337
+
301
338
  def reg(self, name):
302
339
  root, low, bits = alias(name)
303
340
  value = extract(self.regs[root], low, bits)
@@ -8,7 +8,7 @@ from .machine import State, StopPath, REGISTERS, ALIASES, segment_register, stri
8
8
  from .values import unknown
9
9
  from .effect_order import effect_ordering
10
10
  from .relational import validate_controls, evaluate_controls
11
- from .argument_frames import argument_frames, stack_cleanup
11
+ from .argument_frames import WINDOW_BYTES, argument_frames, stack_cleanup
12
12
  from .memory_scopes import model_scopes
13
13
  from .result_flow import return_flows
14
14
  from .image import Image, integer
@@ -204,6 +204,107 @@ CFG_OPERAND = "entry-CFG operand past a stop; values and callee effects unresolv
204
204
  PORT_OPERAND = "operand past a PE32 port access; values and continuation unresolved"
205
205
 
206
206
 
207
+ # dependsOn reasons in ``uses`` for the calls and port accesses a conditionalAccesses row is reached past.
208
+ STEPPED_CALL = "call past a stop; assumed to return"
209
+ STEPPED_PORT = "port access past a stop; assumed to continue"
210
+ OPEN_CALL = "call open at a stop inside its callee; continued at its return site, assumed to return"
211
+
212
+
213
+ def _function_exit(image, start, limit, follow_flat_ports, cache):
214
+ """Walk one function's CFG from ``start``, stepping over calls, and say whether it reaches a return.
215
+
216
+ Returns the decoded sites on a route from ``start`` to a return instruction, whether one was
217
+ reached, and whether the walk stopped at ``limit`` first. A PE32 port access ends its branch
218
+ unless ``follow_flat_ports``. In the PE32 model an IRET stops ``trace``, so it is no return here.
219
+ ``cache`` keeps each walk's result, as several open calls and stops share the same walks.
220
+ """
221
+ key = (start, follow_flat_ports)
222
+ if key in cache:
223
+ return cache[key]
224
+ pending, seen, successors, exits = [start], {}, {}, []
225
+ while pending:
226
+ at = pending.pop()
227
+ if at in seen:
228
+ continue
229
+ if len(seen) >= limit:
230
+ cache[key] = {}, False, True
231
+ return cache[key]
232
+ ins = image.decode(at)
233
+ if ins is None:
234
+ continue
235
+ seen[at] = ins
236
+ m = base_mnemonic(ins)
237
+ if unsupported_transfer(image, ins):
238
+ continue
239
+ following = successors[at] = []
240
+ if at in image.indirect_jumps:
241
+ following.extend(row["target"] for row in image.indirect_jumps[at]["rows"])
242
+ elif m in RETURNS:
243
+ if not (image.flat and m in ("iret", "iretd")):
244
+ exits.append(at)
245
+ else:
246
+ if m == "ljmp" or m.startswith(("j", "loop")):
247
+ target, _ = call_target(image, at, ins)
248
+ if target is not None:
249
+ following.append(target)
250
+ if not (m in ("jmp", "ljmp") or m in INTERRUPTS or m == "hlt"
251
+ or (m in PORTS and image.flat and not follow_flat_ports)):
252
+ following.append(at + ins.size)
253
+ pending.extend(following)
254
+ # Only the sites a return is reachable from lie on the way to it; a branch that never returns is left out.
255
+ callers = {}
256
+ for at, following in successors.items():
257
+ for target in following:
258
+ callers.setdefault(target, []).append(at)
259
+ on_route, pending = set(exits), list(exits)
260
+ while pending:
261
+ for at in callers.get(pending.pop(), ()):
262
+ if at not in on_route:
263
+ on_route.add(at)
264
+ pending.append(at)
265
+ cache[key] = {at: seen[at] for at in on_route}, bool(exits), False
266
+ return cache[key]
267
+
268
+
269
+ def _caller_continuations(image, stop, reason, stack, limit, cache):
270
+ """Return sites at which ``uses`` continues its inventory past a stop inside a called function.
271
+
272
+ ``stack`` holds the traced calls still open at ``stop`` as (call site, return site) pairs,
273
+ outermost first. A return site is continued only when the CFG from the stop, or from the
274
+ return site inside it, reaches a return of the called function. Each continuation depends on
275
+ the stop, on every open call from the stop out to that return site, and on every call or PE32
276
+ port access stepped over on the way to those returns. Returns (return site, dependsOn,
277
+ reached without crossing a PE32 port access) rows and the gaps of walks that reached ``limit``.
278
+ """
279
+ rows = []
280
+ ins = image.decode(stop)
281
+ # A stop at a return instruction is the return itself failing, so no caller continuation is assumed.
282
+ if ins is None or base_mnemonic(ins) in RETURNS:
283
+ return rows, []
284
+ depends = [{"site": stop, "reason": reason}]
285
+ start, port_free = stop, not (image.flat and base_mnemonic(ins) in PORTS)
286
+ for call_site, return_site in reversed(stack):
287
+ route, exits, truncated = _function_exit(image, start, limit, True, cache)
288
+ if truncated:
289
+ return rows, [{"site": start, "reason": "instruction limit"}]
290
+ if not exits:
291
+ break
292
+ if port_free:
293
+ # This walk decodes a subset of the one above, so it cannot reach the limit.
294
+ port_free = _function_exit(image, start, limit, False, cache)[1]
295
+ for at, ins in sorted(route.items()):
296
+ if at == stop:
297
+ continue
298
+ if ins.mnemonic in ("call", "lcall"):
299
+ depends.append({"site": at, "reason": STEPPED_CALL})
300
+ elif image.flat and base_mnemonic(ins) in PORTS:
301
+ depends.append({"site": at, "reason": STEPPED_PORT})
302
+ depends.append({"site": call_site, "reason": OPEN_CALL})
303
+ start = return_site
304
+ rows.append((start, list(depends), port_free))
305
+ return rows, []
306
+
307
+
207
308
  def uses(image, config):
208
309
  query = config.get("query", {})
209
310
  offset = integer(query.get("offset"), 0, image.mask, "query offset")
@@ -233,6 +334,8 @@ def uses(image, config):
233
334
  entry_limit = integer(config.get("entryLimit", 64), 1, 256, "entryLimit")
234
335
  # CFG points where value propagation stopped (or never started), with why; operands after them are inventoried below.
235
336
  stops = {}
337
+ # The traced calls still open at each stop, so the inventory can continue at their return sites.
338
+ open_calls = {}
236
339
  established = entries(image)
237
340
  for index, at in enumerate(established):
238
341
  if remaining <= 0 or index >= entry_limit:
@@ -241,17 +344,18 @@ def uses(image, config):
241
344
  stops.setdefault(root, "entry not traced: entry or total instruction budget exhausted")
242
345
  break
243
346
  report = trace(image, {**config, "entry": at, "totalSteps": remaining, "stringIterations": string_remaining},
244
- continue_declared_jumps=False, track_loops=False)
347
+ continue_declared_jumps=False, track_loops=False, call_stacks=True)
245
348
  remaining -= report["stepsUsed"]
246
349
  string_remaining -= report["stringIterationsUsed"]
247
350
  if not report["completeWithinModel"]:
248
351
  gaps.append({"entry": at, "reason": "incomplete path effects", "stops": list({p["stop"] for p in report["paths"] if p["stop"]})})
249
- for p in report["paths"]:
250
- if p["stop"] and p["stopSite"] is not None:
251
- stops.setdefault(p["stopSite"], p["stop"])
252
- for g in report["gaps"]:
253
- if "site" in g:
254
- stops.setdefault(g["site"], g["reason"])
352
+ stopped = [(p["stopSite"], p["stop"], p["callStack"]) for p in report["paths"]
353
+ if p["stop"] and p["stopSite"] is not None]
354
+ stopped += [(g["site"], g["reason"], g.get("callStack")) for g in report["gaps"] if "site" in g]
355
+ for site, reason, stack in stopped:
356
+ stops.setdefault(site, reason)
357
+ if stack:
358
+ open_calls.setdefault(site, set()).add(tuple((f["callSite"], f["continuation"]) for f in stack))
255
359
  for path in report["paths"]:
256
360
  for e in path["events"]:
257
361
  if e["kind"] not in ("read", "write") or mode not in ("both", e["kind"]):
@@ -289,29 +393,51 @@ def uses(image, config):
289
393
  instruction_limit = config.get("instructionLimit", 10000)
290
394
  # This inventory assumes execution continues past each stop, so it also follows PE32 port accesses
291
395
  # and names each one below.
292
- after_stop, stop_gaps, _, _, _ = (walk(image, list(stops), instruction_limit, follow_flat_ports=True)
293
- if stops else ({}, [], None, None, None))
396
+ # A stop inside a called function would end the inventory at that function's return. The code
397
+ # after each call still open at the stop is inventoried too, from the call's return site, and
398
+ # depends on the stop and on every call between them returning.
399
+ returning, exit_walks = [], {}
400
+ for root, stacks in sorted(open_calls.items()):
401
+ for stack in sorted(stacks):
402
+ rows, frame_gaps = _caller_continuations(image, root, stops[root], stack, instruction_limit, exit_walks)
403
+ returning.extend(rows)
404
+ gaps.extend(g for g in frame_gaps if g not in gaps)
405
+ seeds = list(stops) + [start for start, _, _ in returning]
406
+ after_stop, stop_gaps, _, _, _ = (walk(image, seeds, instruction_limit, follow_flat_ports=True)
407
+ if seeds else ({}, [], None, None, None))
294
408
  gaps.extend(g for g in stop_gaps if g["reason"] == "instruction limit")
295
409
  # In PE32 a site the stops reach only by continuing past a port access is named as such, even when
296
410
  # it also depends on an unread call. The walk that ends at port accesses must finish within the
297
- # limit for that claim; otherwise every row keeps the shared value.
411
+ # limit for that claim; otherwise every row keeps the shared value. A return site counts as
412
+ # reached without a port access only when its callees return without crossing one.
298
413
  port_only = set()
299
- if image.flat and any(base_mnemonic(ins) in PORTS for ins in after_stop.values()):
300
- before_ports, port_gaps, _, _, _ = walk(image, list(stops), instruction_limit)
414
+ if image.flat and (any(base_mnemonic(ins) in PORTS for ins in after_stop.values())
415
+ or any(not port_free for _, _, port_free in returning)):
416
+ port_free_seeds = list(stops) + [start for start, _, port_free in returning if port_free]
417
+ before_ports, port_gaps, _, _, _ = walk(image, port_free_seeds, instruction_limit)
301
418
  if not any(g["reason"] == "instruction limit" for g in port_gaps):
302
419
  port_only = set(after_stop) - set(before_ports)
303
420
  # A call past a stop was never traced either, so code after it also depends on it returning.
304
- starts = [(root, root, reason) for root, reason in stops.items()]
305
- starts += [(at, at + ins.size, "call past a stop; assumed to return")
421
+ starts = [(root, [{"site": root, "reason": reason}]) for root, reason in stops.items()]
422
+ starts += [(at + ins.size, [{"site": at, "reason": STEPPED_CALL}])
306
423
  for at, ins in after_stop.items() if ins.mnemonic in ("call", "lcall") and at not in stops]
307
424
  if image.flat:
308
- starts += [(at, at + ins.size, "port access past a stop; assumed to continue")
425
+ starts += [(at + ins.size, [{"site": at, "reason": STEPPED_PORT}])
309
426
  for at, ins in after_stop.items() if base_mnemonic(ins) in PORTS and at not in stops]
427
+ starts += [(start, row_depends) for start, row_depends, _ in returning]
428
+ # Several rows can share a start (one return site of many open calls), so each start is walked once.
429
+ by_start = {}
430
+ for start, row_depends in starts:
431
+ named = by_start.setdefault(start, [])
432
+ named.extend(d for d in row_depends if d not in named)
310
433
  depends = {}
311
- for site, start, reason in sorted(starts):
434
+ for start, row_depends in by_start.items():
312
435
  reached, _, _, _, _ = walk(image, [start], instruction_limit, follow_flat_ports=True)
313
436
  for at in reached:
314
- depends.setdefault(at, []).append({"site": site, "reason": reason})
437
+ named = depends.setdefault(at, [])
438
+ named.extend(d for d in row_depends if d not in named)
439
+ for named in depends.values():
440
+ named.sort(key=lambda d: (d["site"], d["reason"]))
315
441
  conditional = []
316
442
  for at, ins in sorted(after_stop.items()):
317
443
  if ins.mnemonic == "lea":
@@ -1610,7 +1736,7 @@ def _run_report(image, config, command):
1610
1736
  if a.get(field):
1611
1737
  checkpoints.add(a[field]["site"])
1612
1738
  config = {**config, "checkpoints": sorted(checkpoints)}
1613
- report = trace(image, config)
1739
+ report = trace(image, config, argument_window=WINDOW_BYTES if command == "arguments" else 0)
1614
1740
  # Controls read the complete event stream, before any command narrows it.
1615
1741
  controls = evaluate_controls(report, config, image)
1616
1742
  if command in ("arguments", "effects"):
@@ -371,7 +371,7 @@ def entry_frame(image, config, entry):
371
371
  "traced paths under the query's own inputs; bp null leaves BP unknown"}
372
372
 
373
373
 
374
- def trace(image, config, continue_declared_jumps=True, track_loops=True, arrive=None):
374
+ def trace(image, config, continue_declared_jumps=True, track_loops=True, arrive=None, call_stacks=False, argument_window=0):
375
375
  """Trace bounded paths, preserving declared-table continuations as separate conditional evidence.
376
376
 
377
377
  Ordinary paths run first. A path stopped at a declared indirect jump is then
@@ -382,6 +382,13 @@ def trace(image, config, continue_declared_jumps=True, track_loops=True, arrive=
382
382
  Callers that never report the paths pass track_loops=False, and their paths
383
383
  carry no ``loops`` record. entry_frame passes arrive, the query entry:
384
384
  each path stops at its first arrival at the site with an ``arrival`` record.
385
+ Callers that continue past a stop at the return sites of its callers pass
386
+ call_stacks=True, and each stopped path carries ``callStack``: the traced
387
+ calls still open at the stop, outermost first, with each one's return site.
388
+ A path limit gap inside a called function carries the same ``callStack``.
389
+ A positive ``argument_window`` gives each traced call event an ``argumentSlots`` entry: the
390
+ first ``argument_window`` bytes above its return frame as ``State.argument_slots`` saw them
391
+ when the call ran. ``argument_frames`` reads and removes it.
385
392
  """
386
393
  entry = integer(config.get("entry"), 0, len(image.data) - 1, "entry")
387
394
  if not any(entry in r["entries"] for r in image.regions):
@@ -421,6 +428,7 @@ def trace(image, config, continue_declared_jumps=True, track_loops=True, arrive=
421
428
  observed_frame = entry_frame(image, config, entry) if arrive is None else None
422
429
  root = State(entry, image, config)
423
430
  root.enter_frame(observed_frame)
431
+ root.argument_window = argument_window
424
432
  # Each path carries its own loop record; forks copy it with the rest of the state.
425
433
  root.loops = (LoopTracker(integer(config.get("loopIterationLimit", 64), 1, 1024, "loopIterationLimit"))
426
434
  if track_loops else None)
@@ -461,6 +469,16 @@ def trace(image, config, continue_declared_jumps=True, track_loops=True, arrive=
461
469
  charge(count.number)
462
470
  string_effect(s, ins, count, remaining)
463
471
 
472
+ def open_calls(s):
473
+ return [{"callSite": f["callSite"], "continuation": f["continuation"]} for f in s.frames[1:]]
474
+
475
+ def path_limit(site, s, reason):
476
+ # A path dropped at site shares the open calls of s; a caller continuing past it needs them.
477
+ gap = {"site": site, "reason": reason}
478
+ if call_stacks and len(s.frames) > 1:
479
+ gap["callStack"] = open_calls(s)
480
+ global_gaps.append(gap)
481
+
464
482
  def finish(s, reason=None, returned=False):
465
483
  if continuing and reason and reason.startswith(STRING_BUDGET_STOP):
466
484
  # machine.py names no budget; a continuation's string iterations come from continuationBudget.
@@ -473,6 +491,8 @@ def trace(image, config, continue_declared_jumps=True, track_loops=True, arrive=
473
491
  "bp": s.frame_offset(s.reg(s.bp))}
474
492
  if s.loops is not None:
475
493
  path["loops"] = s.loops.report()
494
+ if call_stacks and not returned:
495
+ path["callStack"] = open_calls(s)
476
496
  assumptions = getattr(s, "declared_jump_assumptions", [])
477
497
  if assumptions:
478
498
  path["declaredJumpAssumptions"] = assumptions
@@ -637,7 +657,7 @@ def trace(image, config, continue_declared_jumps=True, track_loops=True, arrive=
637
657
  for choice in (0, 1):
638
658
  if choice == 0:
639
659
  if created >= max_paths:
640
- global_gaps.append({"site": at, "reason": "path limit at unknown direction flag"})
660
+ path_limit(at, state, "path limit at unknown direction flag")
641
661
  continue
642
662
  child = deepcopy(state); created += 1
643
663
  else:
@@ -697,7 +717,7 @@ def trace(image, config, continue_declared_jumps=True, track_loops=True, arrive=
697
717
  raise StopPath("modeled far return segment changed")
698
718
  for case in model["cases"]:
699
719
  if created >= max_paths:
700
- global_gaps.append({"site": at, "reason": "path limit at modeled call"})
720
+ path_limit(at, state, "path limit at modeled call")
701
721
  break
702
722
  child = deepcopy(state)
703
723
  created += 1
@@ -744,6 +764,8 @@ def trace(image, config, continue_declared_jumps=True, track_loops=True, arrive=
744
764
  flags_frame = False
745
765
  # A traced call records its return-frame width; argumentFrames maps the slots above it.
746
766
  call_event["returnFrameBytes"] = 4 if m == "lcall" or push_cs else image.bits // 8
767
+ if state.argument_window:
768
+ call_event["argumentSlots"] = state.argument_slots(call_event["returnFrameBytes"], state.argument_window)
747
769
  state.frames.append({"entry": target, "sp": state.reg(state.sp), "returnBytes": call_event["returnFrameBytes"],
748
770
  "frameSource": "push-CS/near-call; matching far return required" if push_cs else m,
749
771
  "continuation": following, "returnIP": return_ip, "callSite": at,
@@ -847,7 +869,7 @@ def trace(image, config, continue_declared_jumps=True, track_loops=True, arrive=
847
869
  state = branches.pop()
848
870
  for child in branches:
849
871
  if created >= max_paths:
850
- global_gaps.append({"site": at, "reason": "path limit"})
872
+ path_limit(at, state, "path limit")
851
873
  else:
852
874
  pending.append(child)
853
875
  created += 1
@@ -194,6 +194,120 @@ class PEReporterTests(unittest.TestCase):
194
194
  r = report('ff d0 d9 1d 00 20 40 00 c3', 'uses', query={'offset': DATA_VA, 'width': 4, 'access': 'read'})
195
195
  self.assertEqual(r['conditionalAccesses'], [])
196
196
 
197
+ def test_uses_continues_at_the_return_site_of_a_call_open_at_a_stop(self):
198
+ cfg = 'entry-CFG operand past a stop; values and callee effects unresolved'
199
+ port = 'operand past a PE32 port access; values and continuation unresolved'
200
+ port_reason = 'port access in the flat model depends on I/O privilege, which is not modeled'
201
+ call_reason = 'unresolved call: computed transfer remains unresolved'
202
+ open_call = 'call open at a stop inside its callee; continued at its return site, assumed to return'
203
+ # call f; mov [DATA_VA], eax; ret; f: <stop>; ret
204
+ for name, code, stop, reason, classification in (
205
+ ('port access', 'e8 06 00 00 00 a3 00 20 40 00 c3 66 ba c8 03 ee c3', 15, port_reason, port),
206
+ ('unread call', 'e8 06 00 00 00 a3 00 20 40 00 c3 ff d0 c3', 11, call_reason, cfg)):
207
+ with self.subTest(name):
208
+ r = report(code, 'uses', query={'offset': DATA_VA, 'width': 4}, controls=[CODE_RAW + 5])
209
+ self.assertEqual(r['matches'], [])
210
+ row, = r['conditionalAccesses']
211
+ self.assertEqual((row['site'] - CODE_RAW, row['classification']), (5, classification))
212
+ self.assertEqual([(d['site'] - CODE_RAW, d['reason']) for d in row['dependsOn']],
213
+ [(0, open_call), (stop, reason)])
214
+ self.assertFalse(r['negativeUsable'])
215
+
216
+ def test_uses_continues_at_every_return_site_of_a_nested_stop(self):
217
+ # call f; mov [DATA_VA], eax; ret; f: call g; mov [DATA_VA+4], eax; ret; g: push eax; call eax; pop eax; ret
218
+ c = Code().branch('e8', 'f').emit('a3 00 20 40 00 c3')
219
+ c.label('f').branch('e8', 'g').emit('a3 04 20 40 00 c3')
220
+ c.label('g').emit('50 ff d0 58 c3')
221
+ f, g = c.labels['f'], c.labels['g']
222
+ stop = g + 1
223
+ open_call = 'call open at a stop inside its callee; continued at its return site, assumed to return'
224
+ r = report(c, 'uses', query={'offset': DATA_VA, 'width': 8}, controls=[CODE_RAW + 5, CODE_RAW + f + 5])
225
+ rows = [(e['site'] - CODE_RAW, e['classification'], [(d['site'] - CODE_RAW, d['reason']) for d in e['dependsOn']])
226
+ for e in r['conditionalAccesses']]
227
+ unread = (stop, 'unresolved call: computed transfer remains unresolved')
228
+ cfg = 'entry-CFG operand past a stop; values and callee effects unresolved'
229
+ # Each caller's store depends on the stop and on every open call from the stop out to it.
230
+ self.assertEqual(rows, [(5, cfg, [(0, open_call), (f, open_call), unread]),
231
+ (f + 5, cfg, [(f, open_call), unread])])
232
+ self.assertFalse(r['negativeUsable'])
233
+
234
+ def test_uses_names_a_call_stepped_over_on_the_way_to_a_return_site(self):
235
+ # call f; mov [DATA_VA], eax; ret; f: call eax; call h; ret; h: ret
236
+ c = Code().branch('e8', 'f').emit('a3 00 20 40 00 c3')
237
+ c.label('f').emit('ff d0').branch('e8', 'h').emit('c3').label('h').emit('c3')
238
+ f = c.labels['f']
239
+ r = report(c, 'uses', query={'offset': DATA_VA, 'width': 4}, controls=[CODE_RAW + 5])
240
+ row, = r['conditionalAccesses']
241
+ self.assertEqual([(d['site'] - CODE_RAW, d['reason']) for d in row['dependsOn']],
242
+ [(0, 'call open at a stop inside its callee; continued at its return site, assumed to return'),
243
+ (f, 'unresolved call: computed transfer remains unresolved'),
244
+ (f + 2, 'call past a stop; assumed to return')])
245
+
246
+ def test_uses_leaves_out_a_call_on_a_branch_that_never_returns(self):
247
+ # call f; mov [DATA_VA], eax; ret; f: call eax; test eax, eax; jz L; ret; L: call g; jmp $; g: ret
248
+ c = Code().branch('e8', 'f').emit('a3 00 20 40 00 c3')
249
+ c.label('f').emit('ff d0 85 c0').branch('74', 'L').emit('c3')
250
+ c.label('L').branch('e8', 'g').emit('eb fe').label('g').emit('c3')
251
+ f = c.labels['f']
252
+ r = report(c, 'uses', query={'offset': DATA_VA, 'width': 4}, controls=[CODE_RAW + 5])
253
+ row, = r['conditionalAccesses']
254
+ self.assertEqual([(d['site'] - CODE_RAW, d['reason']) for d in row['dependsOn']],
255
+ [(0, 'call open at a stop inside its callee; continued at its return site, assumed to return'),
256
+ (f, 'unresolved call: computed transfer remains unresolved')])
257
+
258
+ def test_uses_continues_at_the_return_site_of_a_path_limit_inside_a_callee(self):
259
+ # call f; mov [ebx], eax; ret; f: test eax, eax; jz L; mov ebx, DATA_VA; ret; L: mov ebx, CODE_VA; ret
260
+ # One path: the branch that points ebx at the query is dropped at the path limit.
261
+ c = Code().branch('e8', 'f').emit('89 03 c3')
262
+ c.label('f').emit('85 c0').branch('74', 'L').emit('bb 00 20 40 00 c3').label('L').emit('bb 00 10 40 00 c3')
263
+ branch = c.labels['f'] + 2
264
+ r = report(c, 'uses', query={'offset': DATA_VA, 'width': 4}, maxPaths=1)
265
+ self.assertEqual([(e['site'] - CODE_RAW, e['address'], [(d['site'] - CODE_RAW, d['reason']) for d in e['dependsOn']])
266
+ for e in r['conditionalAccesses']],
267
+ [(5, 'possible alias',
268
+ [(0, 'call open at a stop inside its callee; continued at its return site, assumed to return'),
269
+ (branch, 'path limit')])])
270
+ self.assertFalse(r['negativeUsable'])
271
+ # The trace command's gaps carry no call stack.
272
+ self.assertNotIn('callStack', report(c, maxPaths=1)['gaps'][0])
273
+
274
+ def test_uses_does_not_continue_past_a_pe32_iret(self):
275
+ # call f; mov [DATA_VA], eax; ret; f: call eax; iretd. IRET stops the PE32 trace, so it returns to no caller.
276
+ r = report('e8 06 00 00 00 a3 00 20 40 00 c3 ff d0 cf', 'uses', query={'offset': DATA_VA, 'width': 4})
277
+ self.assertEqual(r['conditionalAccesses'], [])
278
+ self.assertFalse(r['negativeUsable'])
279
+
280
+ def test_uses_keeps_a_stop_in_the_entry_function_without_a_caller_continuation(self):
281
+ # call eax; mov [DATA_VA], eax; ret: no call is open at the stop, so dependsOn names the stop alone.
282
+ r = report('ff d0 a3 00 20 40 00 c3', 'uses', query={'offset': DATA_VA, 'width': 4}, controls=[CODE_RAW + 2])
283
+ row, = r['conditionalAccesses']
284
+ self.assertEqual(row['dependsOn'], [{'site': CODE_RAW, 'reason': 'unresolved call: computed transfer remains unresolved'}])
285
+ self.assertFalse(r['negativeUsable'])
286
+
287
+ def test_uses_does_not_continue_past_a_callee_that_cannot_return(self):
288
+ # call f; mov [DATA_VA], eax; ret; f: call eax; jmp $
289
+ code = 'e8 06 00 00 00 a3 00 20 40 00 c3 ff d0 eb fe'
290
+ r = report(code, 'uses', query={'offset': DATA_VA, 'width': 4})
291
+ self.assertEqual(r['conditionalAccesses'], [])
292
+ self.assertFalse(r['negativeUsable'])
293
+ with self.assertRaisesRegex(ValueError, 'Positive variable-use control 517 missed'):
294
+ report(code, 'uses', query={'offset': DATA_VA, 'width': 4}, controls=[CODE_RAW + 5])
295
+ # A stop at the callee's return is that return failing, so its caller is not continued either.
296
+ # call f; mov [DATA_VA], eax; ret; f: push 0; ret
297
+ r = report('e8 06 00 00 00 a3 00 20 40 00 c3 6a 00 c3', 'uses', query={'offset': DATA_VA, 'width': 4})
298
+ self.assertEqual(r['conditionalAccesses'], [])
299
+ self.assertFalse(r['negativeUsable'])
300
+
301
+ def test_uses_records_the_limit_of_a_walk_to_a_callee_return(self):
302
+ # call f; mov [DATA_VA], eax; ret; f: mov dx, 0x3c8; out dx, al; nop * 10; ret
303
+ code = 'e8 06 00 00 00 a3 00 20 40 00 c3 66 ba c8 03 ee' + ' 90' * 10 + ' c3'
304
+ for limit, rows in ((100, [5]), (6, [])):
305
+ with self.subTest(limit=limit):
306
+ r = report(code, 'uses', query={'offset': DATA_VA, 'width': 4}, instructionLimit=limit)
307
+ self.assertEqual([e['site'] - CODE_RAW for e in r['conditionalAccesses']], rows)
308
+ self.assertEqual({'site': CODE_RAW + 15, 'reason': 'instruction limit'} in r['gaps'], limit == 6)
309
+ self.assertFalse(r['negativeUsable'])
310
+
197
311
  def test_pop_addresses_its_destination_after_the_stack_pointer_moves(self):
198
312
  # push 1; push 2; push 3; pop dword [esp+4]; pop eax; pop ebx; ret
199
313
  regs = report('6a 01 6a 02 6a 03 8f 44 24 04 58 5b c3')['paths'][0]['registers']
@@ -1,5 +1,6 @@
1
1
  """Synthetic machine code only. No original binaries or analysis artifacts."""
2
2
  import hashlib
3
+ import itertools
3
4
  import json
4
5
  import os
5
6
  from pathlib import Path
@@ -603,8 +604,76 @@ class ArgumentFrameTests(unittest.TestCase):
603
604
  self.assertIn("the slot at 2 was not read by the callee on this path", r["paths"][bypass]["argumentFrames"][0]["openReasons"])
604
605
  self.assertTrue(site["widthsConsistent"])
605
606
  self.assertEqual(site["conflictingWidths"], [])
606
- self.assertEqual(site["readWidths"], [{"offset": 0, "width": 2, "grouping": "consumed width only", "paths": [0, 1]},
607
- {"offset": 2, "width": 2, "grouping": "consumed width only", "paths": [1 - bypass]}])
607
+ self.assertEqual(site["readWidths"], [{"offset": 0, "width": 2, "grouping": "consumed width only", "paths": [0, 1],
608
+ "fromCallerOnPaths": [0, 1], "notFromCallerOnPaths": []},
609
+ {"offset": 2, "width": 2, "grouping": "consumed width only", "paths": [1 - bypass],
610
+ "fromCallerOnPaths": [1 - bypass], "notFromCallerOnPaths": []}])
611
+
612
+ def test_a_callee_reusing_its_argument_slot_does_not_decide_consistency(self):
613
+ # One path stores a word over the first argument and reads four bytes there as a dword; the
614
+ # other reads the caller's word. The pair is listed, but only the word read saw the caller's bytes.
615
+ c = Code().emit("6a 01 6a 02").branch("e8", "callee").emit("83 c4 04 c3")
616
+ c.label("callee").emit("55 89 e5 85 f6").branch("74", "reuse").emit("8b 46 04").branch("eb", "done")
617
+ c.label("reuse").emit("c7 46 04 00 00 66 8b 46 04").label("done").emit("5d c3")
618
+ r, frames = self.frames(c)
619
+ self.assertEqual(len(frames), 2)
620
+ reuse = next(i for i, path in enumerate(r["paths"]) if path["argumentFrames"][0]["groupings"][0]["width"] == 4)
621
+ self.assertEqual(r["paths"][reuse]["argumentFrames"][0]["groupings"][0]["bytesNotFromSlotWriter"], [0, 1])
622
+ site = r["argumentFrameSites"][0]
623
+ self.assertEqual(site["readWidths"], [{"offset": 0, "width": 2, "grouping": "consumed width only", "paths": [1 - reuse],
624
+ "fromCallerOnPaths": [1 - reuse], "notFromCallerOnPaths": []},
625
+ {"offset": 0, "width": 4, "grouping": "consumed width only", "paths": [reuse],
626
+ "fromCallerOnPaths": [], "notFromCallerOnPaths": [reuse]}])
627
+ self.assertEqual(site["conflictingWidths"], [[{"offset": 0, "width": 2, "grouping": "consumed width only"},
628
+ {"offset": 0, "width": 4, "grouping": "consumed width only"}]])
629
+ self.assertTrue(site["widthsConsistent"])
630
+ self.assertFalse(site["agreed"])
631
+ # Control: when the dword read sees the caller's bytes, the same pair makes the site inconsistent.
632
+ c = Code().emit("6a 01 6a 02").branch("e8", "callee").emit("83 c4 04 c3")
633
+ c.label("callee").emit("55 89 e5 85 f6").branch("74", "wide").emit("8b 46 04").branch("eb", "done")
634
+ c.label("wide").emit("66 8b 46 04").label("done").emit("5d c3")
635
+ site = self.frames(c)[0]["argumentFrameSites"][0]
636
+ self.assertEqual(len(site["conflictingWidths"]), 1)
637
+ self.assertEqual([w["notFromCallerOnPaths"] for w in site["readWidths"]], [[], []])
638
+ self.assertFalse(site["widthsConsistent"])
639
+
640
+ def test_a_read_past_the_callers_bytes_still_conflicts_on_the_bytes_it_saw(self):
641
+ # One pushed word, read as a dword on one path and as a word on the other. The dword's upper
642
+ # bytes have no writer, but its lower bytes are the caller's word, so the widths disagree.
643
+ c = Code().emit("6a 01").branch("e8", "callee").emit("83 c4 02 c3")
644
+ c.label("callee").emit("55 89 e5 85 f6").branch("74", "wide").emit("8b 46 04").branch("eb", "done")
645
+ c.label("wide").emit("66 8b 46 04").label("done").emit("5d c3")
646
+ r, _ = self.frames(c)
647
+ wide = next(i for i, path in enumerate(r["paths"]) if path["argumentFrames"][0]["groupings"][0]["width"] == 4)
648
+ self.assertEqual(r["paths"][wide]["argumentFrames"][0]["groupings"][0]["bytesNotFromSlotWriter"], [2, 3])
649
+ site = r["argumentFrameSites"][0]
650
+ self.assertEqual([(w["width"], w["fromCallerOnPaths"], w["notFromCallerOnPaths"]) for w in site["readWidths"]],
651
+ [(2, [1 - wide], []), (4, [], [wide])])
652
+ self.assertEqual(len(site["conflictingWidths"]), 1)
653
+ self.assertFalse(site["widthsConsistent"])
654
+ # A dword over a stored first word and the caller's second word conflicts with a word read of the second.
655
+ c = Code().emit("6a 01 6a 02").branch("e8", "callee").emit("83 c4 04 c3")
656
+ c.label("callee").emit("55 89 e5 85 f6").branch("74", "reuse").emit("8b 46 04 8b 5e 06").branch("eb", "done")
657
+ c.label("reuse").emit("c7 46 04 00 00 66 8b 46 04").label("done").emit("5d c3")
658
+ site = self.frames(c)[0]["argumentFrameSites"][0]
659
+ self.assertEqual([(w["offset"], w["width"]) for w in site["readWidths"]], [(0, 2), (0, 4), (2, 2)])
660
+ self.assertEqual(len(site["conflictingWidths"]), 2)
661
+ self.assertFalse(site["widthsConsistent"])
662
+
663
+ def test_a_site_read_only_after_the_callee_stored_its_slot_is_not_consistent(self):
664
+ # The callee stores over its argument and then reads it: the read is listed, but none saw the caller's word.
665
+ c = Code().emit("6a 01").branch("e8", "callee").emit("83 c4 02 c3").label("callee").emit("55 89 e5 c7 46 04 09 00 8b 46 04 5d c3")
666
+ site = self.frames(c)[0]["argumentFrameSites"][0]
667
+ self.assertEqual(site["readWidths"], [{"offset": 0, "width": 2, "grouping": "consumed width only", "paths": [0],
668
+ "fromCallerOnPaths": [], "notFromCallerOnPaths": [0]}])
669
+ self.assertEqual(site["conflictingWidths"], [])
670
+ self.assertFalse(site["widthsConsistent"])
671
+ # A path that reads the caller's word and then the stored word at the same width lists the path both ways.
672
+ c = Code().emit("6a 01").branch("e8", "callee").emit("83 c4 02 c3")
673
+ c.label("callee").emit("55 89 e5 8b 46 04 c7 46 04 09 00 8b 5e 04 5d c3")
674
+ site = self.frames(c)[0]["argumentFrameSites"][0]
675
+ self.assertEqual([(w["paths"], w["fromCallerOnPaths"], w["notFromCallerOnPaths"]) for w in site["readWidths"]], [([0], [0], [0])])
676
+ self.assertTrue(site["widthsConsistent"])
608
677
 
609
678
  def test_paths_that_group_the_same_bytes_differently_keep_the_site_open(self):
610
679
  # Both paths read four bytes at offset 0: one as a far pointer with LES, one as a 32-bit dword.
@@ -658,6 +727,84 @@ class ArgumentFrameTests(unittest.TestCase):
658
727
  self.assertEqual([(s["offset"], s["width"], s["writerSite"]) for s in frame["slots"]], [(0, 2, 0)])
659
728
  self.assertTrue(frame["settledOnThisPath"], frame["openReasons"])
660
729
 
730
+ def test_every_slot_agrees_with_the_callee_read_of_the_same_byte(self):
731
+ # Each caller fills a frame through pushes, a reserved word, stores through DS or ES and a
732
+ # modeled service, on a symbolic or concrete stack. The callee reads every pushed word, on
733
+ # one variant after an ES store of its own. A read that runs before any callee write
734
+ # through another segment or base sees each byte as the slot does: the same write order,
735
+ # or an unwritten cause naming the event the slot's reason names.
736
+ callers = [("6a 01 6a 02", 2), ("6a 01 c7 06 00 01 05 00 6a 02", 2), ("83 ec 02 88 07", 1),
737
+ ("6a 01 26 c6 07 01", 1), ("6a 01 SERVICE 6a 02", 2), ("83 ec 02 SERVICE 26 c6 07 01", 1),
738
+ ("6a 01 89 e3 c7 07 09 00", 1), ("6a 01 SERVICE", 1)]
739
+ registers = [{}, {"ss": 0x2000, "sp": 0x100}, {"ss": 0x2000, "sp": 0x100, "ds": 0x2000},
740
+ {"ss": 0x2000, "sp": 0x100, "ds": 0x3000}]
741
+ scopes = [None, {"segment": "ss", "base": "sp", "displacement": 0, "bytes": 1, "evidence": "synthetic kept byte"}]
742
+ compared, causes = 0, set()
743
+ for (caller, words), regs, scope, callee_store in itertools.product(callers, registers, scopes, (False, True)):
744
+ c, sites = Code(), []
745
+ for n, part in enumerate(caller.split("SERVICE")):
746
+ if n:
747
+ sites.append(len(c.data))
748
+ c.branch("e8", "service")
749
+ c.emit(part)
750
+ c.branch("e8", "callee").emit("83 c4 %02x c3" % (2 * words)).label("service").emit("c3")
751
+ c.label("callee").emit("55 89 e5" + (" 26 c6 07 01" if callee_store else "")
752
+ + "".join(" 8b 46 %02x" % (4 + 2 * n) for n in range(words)) + " 5d c3")
753
+ models = [{"site": site, "evidence": "synthetic service", "preserves": ["ss", "esp", "ds", "es", "ebx"], "cases": [{}],
754
+ **({"preservesMemory": [scope]} if scope else {})} for site in sites]
755
+ extra = {"registers": regs, "callModels": models}
756
+ traced, mapped = report(c, "trace", **extra), report(c, "arguments", **extra)
757
+ self.assertEqual(len(traced["paths"]), len(mapped["paths"]))
758
+ for path, mapped_path in zip(traced["paths"], mapped["paths"]):
759
+ every = path["events"]
760
+ for frame in mapped_path["argumentFrames"]:
761
+ push = every[frame["callOrder"] + 1]["interval"]
762
+ group = (push["segment"], push["base"])
763
+ for grouping in frame["groupings"]:
764
+ between = every[frame["callOrder"] + 1:grouping["readOrder"]]
765
+ if any(e["kind"] == "write" and (e["interval"]["segment"], e["interval"]["base"]) != group for e in between):
766
+ continue
767
+ read = every[grouping["readOrder"]]
768
+ for i, row in enumerate(read["byteProducers"]):
769
+ at = grouping["offset"] + i
770
+ slot = next(s for s in frame["slots"] if s["offset"] <= at < s["offset"] + s["width"])
771
+ if slot["writerSite"] is not None:
772
+ self.assertEqual(row["writeOrder"], slot["writerOrder"], (c.data.hex(), regs, scope))
773
+ causes.add("written")
774
+ else:
775
+ cause, order = row["unwritten"]["cause"], row["unwritten"]["order"]
776
+ expected = ("no write on this path" if cause == "no write on this path" else
777
+ "memory invalidated by the modeled call at " + str(every[order]["callSite"])
778
+ if cause == "dropped by a modeled call" else
779
+ f"memory possibly overwritten through another address by the write at {every[order]['site']}")
780
+ self.assertEqual(slot["reason"], expected, (c.data.hex(), regs, scope))
781
+ causes.add(cause)
782
+ compared += 1
783
+ self.assertGreater(compared, 100)
784
+ self.assertEqual(causes, {"written", "no write on this path", "dropped by a modeled call",
785
+ "possibly written by an aliasing write", "dropped by a possibly aliasing write"})
786
+
787
+ def test_the_slot_record_stays_out_of_every_reported_event(self):
788
+ # The ordinary path stops at a declared table jump; its continuation calls the callee.
789
+ c = Code().emit("ff e3").label("target").emit("6a 01").branch("e8", "callee").emit("83 c4 02 c3")
790
+ c.label("callee").emit("55 89 e5 8b 46 04 5d c3").label("table")
791
+ data = c.bytes() + c.labels["target"].to_bytes(2, "little")
792
+ cfg = configuration(data, indirectJumps=[{"site": 0, "evidence": "synthetic table consumer", "exhaustive": True,
793
+ "table": {"start": c.labels["table"], "count": 1, "stride": 2,
794
+ "evidence": "synthetic table words"}}])
795
+ cfg["regions"][0]["end"] = c.labels["table"]
796
+ for command in ("arguments", "trace"):
797
+ r = run_report(data, cfg, command)
798
+ self.assertEqual(len(r["declaredContinuationPaths"]), 1)
799
+ calls = [e for group in ("paths", "declaredContinuationPaths") for path in r[group] for e in path["events"] if e["kind"] == "call"]
800
+ self.assertTrue(calls)
801
+ self.assertFalse([e for e in calls if "argumentSlots" in e])
802
+ # A call on an ordinary path keeps no record either.
803
+ c = Code().emit("6a 01").branch("e8", "callee").emit("83 c4 02 c3").label("callee").emit("55 89 e5 8b 46 04 5d c3")
804
+ r, frames = self.frames(c)
805
+ self.assertEqual(len(frames), 1)
806
+ self.assertFalse([e for e in events(r, "call") if "argumentSlots" in e])
807
+
661
808
 
662
809
  class GhidraCrossCheckTests(unittest.TestCase):
663
810
  # root: call a (site 0); call b (site 3); call bx (site 6); ret. a and b return.