scientific-method-engine 8.1.1__tar.gz → 9.1.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 (72) hide show
  1. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/PKG-INFO +8 -7
  2. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/README.md +7 -6
  3. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/pyproject.toml +1 -1
  4. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/cli.py +1 -0
  5. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ExportBoundedFlow.java +18 -4
  6. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ReportInstructionContext.java +7 -0
  7. scientific_method_engine-9.1.0/src/scientific_method_engine/ghidra/ReportInstructionWindow.java +71 -0
  8. scientific_method_engine-9.1.0/src/scientific_method_engine/ghidra/scientificmethod/InstructionStart.java +53 -0
  9. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/x86/relational.py +84 -7
  10. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/x86/trace.py +7 -2
  11. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/tests/test_interrupt_models.py +51 -3
  12. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/tests/test_relational_controls.py +105 -0
  13. scientific_method_engine-8.1.1/src/scientific_method_engine/ghidra/ReportInstructionWindow.java +0 -36
  14. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/.gitignore +0 -0
  15. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/LICENSE +0 -0
  16. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/__init__.py +0 -0
  17. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/__main__.py +0 -0
  18. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ClearNoReturnFunctions.java +0 -0
  19. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/CreateFunctions.java +0 -0
  20. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ExportCallEdges.java +0 -0
  21. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ExportFunctionFingerprints.java +0 -0
  22. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ExportFunctionInventory.java +0 -0
  23. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/MergeFallThroughFragment.java +0 -0
  24. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/RecoverCitedFunctions.java +0 -0
  25. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/RepairReturningCallers.java +0 -0
  26. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ReportCallArguments.java +0 -0
  27. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ReportCallPaths.java +0 -0
  28. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ReportCallSitesWithScalars.java +0 -0
  29. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ReportCallsToRange.java +0 -0
  30. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ReportConstantFirstArgumentCalls.java +0 -0
  31. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ReportDataBytes.java +0 -0
  32. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ReportDecompileMatches.java +0 -0
  33. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ReportDecompileWindow.java +0 -0
  34. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ReportFilePatternInMemory.java +0 -0
  35. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ReportFirstArgumentCallSummary.java +0 -0
  36. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ReportFunctionScalarConstants.java +0 -0
  37. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ReportFunctionSummary.java +0 -0
  38. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ReportMemoryBlockForFileOffset.java +0 -0
  39. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ReportMemoryBlocks.java +0 -0
  40. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ReportRandomnessCandidates.java +0 -0
  41. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ReportReferences.java +0 -0
  42. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ReportScalarConstants.java +0 -0
  43. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ReportStringReferences.java +0 -0
  44. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/ReportSymbolReferences.java +0 -0
  45. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/ghidra/scientificmethod/FirstArgumentLookBack.java +0 -0
  46. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/x86/__init__.py +0 -0
  47. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/x86/argument_frames.py +0 -0
  48. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/x86/dispatch.py +0 -0
  49. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/x86/effect_order.py +0 -0
  50. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/x86/image.py +0 -0
  51. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/x86/loops.py +0 -0
  52. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/x86/machine.py +0 -0
  53. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/x86/memory_scopes.py +0 -0
  54. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/x86/pcode.py +0 -0
  55. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/x86/pcode_backend.py +0 -0
  56. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/x86/pe.py +0 -0
  57. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/x86/reports.py +0 -0
  58. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/x86/result_flow.py +0 -0
  59. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/src/scientific_method_engine/x86/values.py +0 -0
  60. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/tests/oracle.py +0 -0
  61. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/tests/test_continuation_budget.py +0 -0
  62. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/tests/test_dispatch.py +0 -0
  63. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/tests/test_effect_order.py +0 -0
  64. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/tests/test_entry_frame.py +0 -0
  65. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/tests/test_loops.py +0 -0
  66. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/tests/test_memory_scopes.py +0 -0
  67. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/tests/test_nested_frame_request.py +0 -0
  68. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/tests/test_oracle.py +0 -0
  69. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/tests/test_pe.py +0 -0
  70. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/tests/test_successor_rule.py +0 -0
  71. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/tests/test_table_continuations.py +0 -0
  72. {scientific_method_engine-8.1.1 → scientific_method_engine-9.1.0}/tests/test_x86.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: scientific-method-engine
3
- Version: 8.1.1
3
+ Version: 9.1.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
@@ -51,16 +51,16 @@ for the other report scripts, a result count equal to the cap means the same.
51
51
  `analyzeHeadless` can exit with code 0 after a script failed to load, so check the log for the
52
52
  script's own result lines ([the Ghidra workflow](https://github.com/kibertoad/refurbished-dinosaurs-toolkit/blob/main/docs/ghidra-workflow.md)
53
53
  says what a run must show). The scripts compile against Ghidra 12.1. `ReportConstantFirstArgumentCalls`,
54
- `ReportFirstArgumentCallSummary` and `ReportCallSitesWithScalars` load a shared helper from the
55
- `scientificmethod/` directory beside them, so pass the directory the command prints as `-scriptPath`,
54
+ `ReportFirstArgumentCallSummary`, `ReportCallSitesWithScalars`, `ReportInstructionWindow` and
55
+ `ExportBoundedFlow` load shared helpers from the `scientificmethod/` directory beside them, so pass the directory the command prints as `-scriptPath`,
56
56
  or copy that subdirectory along with the scripts.
57
57
 
58
58
  Reading code and data:
59
59
 
60
60
  | Script | Arguments | Prints |
61
61
  |---|---|---|
62
- | `ReportInstructionContext` | one or more instruction addresses | a bounded instruction window around each |
63
- | `ReportInstructionWindow` | address, instruction count | instructions from the address onward |
62
+ | `ReportInstructionContext` | one or more instruction addresses | a bounded instruction window around the instruction containing each address; the header names the requested address when it is inside that instruction |
63
+ | `ReportInstructionWindow` | address where an instruction starts, instruction count (1..200) | a header, the instructions from the address onward with a `gap:` line wherever the listing skips bytes, then the count printed or where the listing ended. An address inside an instruction, in data, in undisassembled bytes or outside memory prints an error naming what is there and the next instruction start, and no window |
64
64
  | `ReportDataBytes` | address, byte count (1..256) | the bytes at the address |
65
65
  | `ReportFunctionSummary` | one or more addresses | focused decompiler output of each containing function, its body ranges and each call without a fall-through (a sign of a wrong no-return flag), then the addresses with no function or a failed decompile |
66
66
  | `ReportDecompileWindow` | address, first line (1-based), line count (a count above 160 is cut to 160) | a window of one function's decompilation, its total line count and where the next window starts |
@@ -90,7 +90,7 @@ Exporting for comparison (each writes one file and refuses to overwrite where no
90
90
 
91
91
  | Script | Arguments | Writes |
92
92
  |---|---|---|
93
- | `ExportBoundedFlow` | entry, instruction limit (1..10000), output path under `analysis/original/` | instruction metadata of one bounded flow as JSON |
93
+ | `ExportBoundedFlow` | entry where an instruction starts, instruction limit (1..10000, the most instruction records exported), output path under `analysis/original/` | instruction metadata of one bounded flow as JSON, with `limitReached` (the walk stopped at the limit with flow left unread) and `noInstruction` (flow targets where no instruction starts). An entry where no instruction starts fails the script and writes nothing |
94
94
  | `ExportFunctionInventory` | output TSV path (must not exist) | every function's start and body size |
95
95
  | `ExportCallEdges` | output JSON path (must not exist), function limit (1..128), one or more function entries | the call and tail-jump edges of the functions Ghidra reaches breadth-first from the entries, with file offsets, Ghidra's flow type (after any flow override), `fallsThrough` (whether Ghidra continues to the next instruction at the site, after any fall-through override) and `fallsThroughTo`/`fallsThroughToAddress` (where a fall-through override sends Ghidra instead, or null), as the `ghidraCallEdges` input of `callees` |
96
96
  | `ExportFunctionFingerprints` | output TSV path (replaced only when the export completes) | per-function and per-instruction fingerprints with addresses normalized, for matching functions across versions |
@@ -169,7 +169,8 @@ The trace-family commands (`trace`, `arguments`, `effects`, `returns`, `guards`,
169
169
  writer, containment, value relation and value origin) evaluated on every bounded path. A violated
170
170
  control fails the report. A control left undecided by a stop, a limit or an unread call is
171
171
  reported as `undecided` and never counts as held. Access reports also give each byte's
172
- `writeOrder`, or why it has no modeled value. See the
172
+ `writeOrder`, or why it has no modeled value. A last-writer control with an `address` asks the
173
+ same of memory the program never reads back, at a checkpoint. See the
173
174
  [controls contract](https://github.com/kibertoad/refurbished-dinosaurs-toolkit/blob/main/docs/bounded-evidence-reporters.md#relational-controls).
174
175
 
175
176
  A trace-family query whose entry lies inside a function body names the function's entry in
@@ -35,16 +35,16 @@ for the other report scripts, a result count equal to the cap means the same.
35
35
  `analyzeHeadless` can exit with code 0 after a script failed to load, so check the log for the
36
36
  script's own result lines ([the Ghidra workflow](https://github.com/kibertoad/refurbished-dinosaurs-toolkit/blob/main/docs/ghidra-workflow.md)
37
37
  says what a run must show). The scripts compile against Ghidra 12.1. `ReportConstantFirstArgumentCalls`,
38
- `ReportFirstArgumentCallSummary` and `ReportCallSitesWithScalars` load a shared helper from the
39
- `scientificmethod/` directory beside them, so pass the directory the command prints as `-scriptPath`,
38
+ `ReportFirstArgumentCallSummary`, `ReportCallSitesWithScalars`, `ReportInstructionWindow` and
39
+ `ExportBoundedFlow` load shared helpers from the `scientificmethod/` directory beside them, so pass the directory the command prints as `-scriptPath`,
40
40
  or copy that subdirectory along with the scripts.
41
41
 
42
42
  Reading code and data:
43
43
 
44
44
  | Script | Arguments | Prints |
45
45
  |---|---|---|
46
- | `ReportInstructionContext` | one or more instruction addresses | a bounded instruction window around each |
47
- | `ReportInstructionWindow` | address, instruction count | instructions from the address onward |
46
+ | `ReportInstructionContext` | one or more instruction addresses | a bounded instruction window around the instruction containing each address; the header names the requested address when it is inside that instruction |
47
+ | `ReportInstructionWindow` | address where an instruction starts, instruction count (1..200) | a header, the instructions from the address onward with a `gap:` line wherever the listing skips bytes, then the count printed or where the listing ended. An address inside an instruction, in data, in undisassembled bytes or outside memory prints an error naming what is there and the next instruction start, and no window |
48
48
  | `ReportDataBytes` | address, byte count (1..256) | the bytes at the address |
49
49
  | `ReportFunctionSummary` | one or more addresses | focused decompiler output of each containing function, its body ranges and each call without a fall-through (a sign of a wrong no-return flag), then the addresses with no function or a failed decompile |
50
50
  | `ReportDecompileWindow` | address, first line (1-based), line count (a count above 160 is cut to 160) | a window of one function's decompilation, its total line count and where the next window starts |
@@ -74,7 +74,7 @@ Exporting for comparison (each writes one file and refuses to overwrite where no
74
74
 
75
75
  | Script | Arguments | Writes |
76
76
  |---|---|---|
77
- | `ExportBoundedFlow` | entry, instruction limit (1..10000), output path under `analysis/original/` | instruction metadata of one bounded flow as JSON |
77
+ | `ExportBoundedFlow` | entry where an instruction starts, instruction limit (1..10000, the most instruction records exported), output path under `analysis/original/` | instruction metadata of one bounded flow as JSON, with `limitReached` (the walk stopped at the limit with flow left unread) and `noInstruction` (flow targets where no instruction starts). An entry where no instruction starts fails the script and writes nothing |
78
78
  | `ExportFunctionInventory` | output TSV path (must not exist) | every function's start and body size |
79
79
  | `ExportCallEdges` | output JSON path (must not exist), function limit (1..128), one or more function entries | the call and tail-jump edges of the functions Ghidra reaches breadth-first from the entries, with file offsets, Ghidra's flow type (after any flow override), `fallsThrough` (whether Ghidra continues to the next instruction at the site, after any fall-through override) and `fallsThroughTo`/`fallsThroughToAddress` (where a fall-through override sends Ghidra instead, or null), as the `ghidraCallEdges` input of `callees` |
80
80
  | `ExportFunctionFingerprints` | output TSV path (replaced only when the export completes) | per-function and per-instruction fingerprints with addresses normalized, for matching functions across versions |
@@ -153,7 +153,8 @@ The trace-family commands (`trace`, `arguments`, `effects`, `returns`, `guards`,
153
153
  writer, containment, value relation and value origin) evaluated on every bounded path. A violated
154
154
  control fails the report. A control left undecided by a stop, a limit or an unread call is
155
155
  reported as `undecided` and never counts as held. Access reports also give each byte's
156
- `writeOrder`, or why it has no modeled value. See the
156
+ `writeOrder`, or why it has no modeled value. A last-writer control with an `address` asks the
157
+ same of memory the program never reads back, at a checkpoint. See the
157
158
  [controls contract](https://github.com/kibertoad/refurbished-dinosaurs-toolkit/blob/main/docs/bounded-evidence-reporters.md#relational-controls).
158
159
 
159
160
  A trace-family query whose entry lies inside a function body names the function's entry in
@@ -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 = "8.1.1"
8
+ version = "9.1.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"
@@ -23,6 +23,7 @@ USAGE = ("Usage: scientific-method-engine <operand|operand-candidates|target|bou
23
23
  "or continues at another address than the next instruction (ghidraFallsThroughElsewhere).\n"
24
24
  "trace, arguments, effects, returns, guards, memory and allocation check relationalControls:\n"
25
25
  "a violated control fails the report; an undecided one is reported and never counts as held.\n"
26
+ "A lastWriter control with an address inspects that memory at its checkpoint anchors without a read.\n"
26
27
  "entryFrame {from} starts an entry inside the function at from, in the frame a trace from there observed.\n"
27
28
  "Declared table continuations are separate conditional paths that spend continuationBudget\n"
28
29
  "(paths, totalSteps, maxSteps, visitLimit, stringIterations); ordinary computed transfers remain stopped.\n"
@@ -7,12 +7,16 @@ import ghidra.program.model.listing.Function;
7
7
  import java.nio.file.*;
8
8
  import java.nio.charset.StandardCharsets;
9
9
  import java.util.*;
10
+ import scientificmethod.InstructionStart;
10
11
 
11
12
  public class ExportBoundedFlow extends GhidraScript {
12
13
  @Override protected void run() throws Exception {
13
14
  String[] args = getScriptArgs();
14
15
  if (args.length != 3) throw new IllegalArgumentException("Supply entry, instruction limit (1..10000), ignored analysis/original/output.json");
15
16
  Address entry = toAddr(args[0]);
17
+ if (entry == null) throw new IllegalArgumentException(args[0] + ": not an address in this program");
18
+ String missingEntry = InstructionStart.missingStart(currentProgram, entry);
19
+ if (missingEntry != null) throw new IllegalArgumentException(missingEntry + " Nothing exported.");
16
20
  int limit = Integer.parseInt(args[1]);
17
21
  if (limit < 1 || limit > 10000) throw new IllegalArgumentException("Limit must be 1..10000");
18
22
  Path output = Path.of(args[2]).toAbsolutePath().normalize();
@@ -21,18 +25,24 @@ public class ExportBoundedFlow extends GhidraScript {
21
25
  Deque<Address> pending = new ArrayDeque<>();
22
26
  List<String> rows = new ArrayList<>();
23
27
  Set<Long> entries = new TreeSet<>();
28
+ // Flow targets where no instruction starts (inside an instruction, data or undisassembled
29
+ // bytes) are listed, so the export never reads as covering them.
30
+ Set<Long> noInstruction = new TreeSet<>();
31
+ boolean limitReached = false;
24
32
  entries.add(entry.getOffset()); pending.push(entry);
25
33
  while (!pending.isEmpty()) {
26
34
  if (monitor.isCancelled()) throw new InterruptedException("Cancelled");
27
35
  Address at = pending.pop();
28
36
  if (visited.contains(at)) continue;
29
- if (visited.size() >= limit) break; // Missing edge targets remain explicit review gaps.
30
37
  visited.add(at);
31
38
  Function owner = currentProgram.getFunctionManager().getFunctionContaining(at);
32
39
  Function exact = currentProgram.getFunctionManager().getFunctionAt(at);
33
40
  if (!at.equals(entry) && exact != null) { entries.add(at.getOffset()); continue; }
34
41
  Instruction ins = currentProgram.getListing().getInstructionAt(at);
35
- if (ins == null) continue;
42
+ if (ins == null) { noInstruction.add(at.getOffset()); continue; }
43
+ // The limit counts exported instruction records. Function-entry stops and
44
+ // noInstruction targets do not use it up, so a full export holds exactly limit rows.
45
+ if (rows.size() >= limit) { limitReached = true; break; }
36
46
  List<Long> next = new ArrayList<>(), calls = new ArrayList<>();
37
47
  var type = ins.getFlowType();
38
48
  Address fall = ins.getFallThrough();
@@ -52,8 +62,12 @@ public class ExportBoundedFlow extends GhidraScript {
52
62
  + ",\"owner\":" + (owner == null ? "null" : owner.getEntryPoint().getOffset()) + "}");
53
63
  }
54
64
  Files.createDirectories(output.getParent());
55
- String text = "{\"entries\":" + entries + ",\"instructions\":[" + String.join(",", rows) + "]}";
65
+ String text = "{\"entries\":" + entries + ",\"limitReached\":" + limitReached
66
+ + ",\"noInstruction\":" + noInstruction + ",\"instructions\":[" + String.join(",", rows) + "]}";
56
67
  Files.writeString(output, text, StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW);
57
- println("Exported " + rows.size() + " instruction metadata records. Ghidra addresses are view-specific; retain the import mapping separately.");
68
+ println("Exported " + rows.size() + " instruction metadata records"
69
+ + (limitReached ? "; the walk stopped at the limit of " + limit + " with flow left unread" : "")
70
+ + (noInstruction.isEmpty() ? "" : "; " + noInstruction.size() + " flow targets start no instruction")
71
+ + ". Ghidra addresses are view-specific; retain the import mapping separately.");
58
72
  }
59
73
  }
@@ -24,6 +24,10 @@ public class ReportInstructionContext extends GhidraScript {
24
24
  Listing listing = currentProgram.getListing();
25
25
  for (String argument : arguments) {
26
26
  Address address = toAddr(argument);
27
+ if (address == null) {
28
+ printerr(argument + ": not an address in this program");
29
+ continue;
30
+ }
27
31
  Instruction focus = listing.getInstructionContaining(address);
28
32
  if (focus == null) {
29
33
  printerr(argument + ": no containing instruction");
@@ -32,7 +36,10 @@ public class ReportInstructionContext extends GhidraScript {
32
36
 
33
37
  Function function = currentProgram.getFunctionManager()
34
38
  .getFunctionContaining(focus.getAddress());
39
+ // An address inside an instruction shows the instruction that contains it, and the
40
+ // header names the requested address so the two are never read as the same.
35
41
  println("===== " + focus.getAddress()
42
+ + (focus.getAddress().equals(address) ? "" : " (contains " + address + ")")
36
43
  + (function == null ? "" : " in " + function.getEntryPoint()
37
44
  + " " + function.getName()) + " =====");
38
45
 
@@ -0,0 +1,71 @@
1
+ // Prints a bounded instruction window that begins at an instruction start.
2
+ // @category Restoration
3
+
4
+ import ghidra.app.script.GhidraScript;
5
+ import ghidra.program.model.address.Address;
6
+ import ghidra.program.model.listing.Instruction;
7
+
8
+ import scientificmethod.InstructionStart;
9
+
10
+ public class ReportInstructionWindow extends GhidraScript {
11
+ private static final int MAX_INSTRUCTIONS = 200;
12
+
13
+ @Override
14
+ protected void run() throws Exception {
15
+ String[] arguments = getScriptArgs();
16
+ if (arguments.length != 2) {
17
+ printerr("Supply a virtual address and instruction count.");
18
+ return;
19
+ }
20
+
21
+ Address address = toAddr(arguments[0]);
22
+ if (address == null) {
23
+ printerr(arguments[0] + ": not an address in this program");
24
+ return;
25
+ }
26
+ int count;
27
+ try {
28
+ count = Integer.parseInt(arguments[1]);
29
+ } catch (NumberFormatException exception) {
30
+ count = 0;
31
+ }
32
+ if (count < 1 || count > MAX_INSTRUCTIONS) {
33
+ printerr("Instruction count must be between 1 and " + MAX_INSTRUCTIONS + ".");
34
+ return;
35
+ }
36
+
37
+ // The window starts only where an instruction starts. Starting at the next instruction
38
+ // instead would print a window that looks like the requested one but is not.
39
+ String missing = InstructionStart.missingStart(currentProgram, address);
40
+ if (missing != null) {
41
+ printerr(missing + " No window printed.");
42
+ return;
43
+ }
44
+
45
+ println("===== up to " + count + " instructions from " + address + " =====");
46
+ Instruction instruction = currentProgram.getListing().getInstructionAt(address);
47
+ int printed = 0;
48
+ while (instruction != null && printed < count) {
49
+ println(instruction.getAddress() + ": " + instruction);
50
+ printed++;
51
+ Instruction next = instruction.getNext();
52
+ if (printed < count && next != null) {
53
+ // next() is null when the instruction ends its address space, so the gap is
54
+ // measured from the instruction's last byte.
55
+ Address last = instruction.getMaxAddress();
56
+ Address end = last.next();
57
+ if (end == null) {
58
+ println("gap: no instruction after " + last + " up to " + next.getAddress());
59
+ } else if (!next.getAddress().equals(end)) {
60
+ println("gap: no instruction from " + end + " up to " + next.getAddress());
61
+ }
62
+ }
63
+ instruction = next;
64
+ }
65
+ if (printed < count) {
66
+ println("The listing ends after " + printed + " of " + count + " instructions.");
67
+ } else {
68
+ println("Printed " + printed + " instructions.");
69
+ }
70
+ }
71
+ }
@@ -0,0 +1,53 @@
1
+ package scientificmethod;
2
+
3
+ import ghidra.program.model.address.Address;
4
+ import ghidra.program.model.listing.Data;
5
+ import ghidra.program.model.listing.Instruction;
6
+ import ghidra.program.model.listing.Listing;
7
+ import ghidra.program.model.listing.Program;
8
+ import ghidra.program.model.mem.MemoryBlock;
9
+
10
+ /**
11
+ * The exact-start lookup that ReportInstructionWindow and ExportBoundedFlow share. A script that
12
+ * reads from an instruction start refuses an address where none starts and says what is there
13
+ * instead, so a mistyped or misaligned start never reads as an empty result.
14
+ */
15
+ public final class InstructionStart {
16
+ private InstructionStart() {
17
+ }
18
+
19
+ /**
20
+ * Null when an instruction starts at the address. Otherwise a sentence that names what holds
21
+ * the address (no memory block, the instruction or defined data that contains it, an
22
+ * uninitialized block, or undisassembled bytes) and the next instruction start after it, if there is one.
23
+ */
24
+ public static String missingStart(Program program, Address address) {
25
+ Listing listing = program.getListing();
26
+ if (listing.getInstructionAt(address) != null) {
27
+ return null;
28
+ }
29
+
30
+ String holder;
31
+ MemoryBlock block = program.getMemory().getBlock(address);
32
+ Instruction containing = listing.getInstructionContaining(address);
33
+ Data data = listing.getDefinedDataContaining(address);
34
+ if (block == null) {
35
+ holder = "it is in no memory block";
36
+ } else if (containing != null) {
37
+ holder = "it is inside the " + containing.getLength() + "-byte instruction at "
38
+ + containing.getAddress();
39
+ } else if (data != null) {
40
+ holder = "it is inside defined data at " + data.getAddress();
41
+ } else if (!block.isInitialized()) {
42
+ holder = "it is in uninitialized memory block " + block.getName()
43
+ + ", which holds no bytes to disassemble";
44
+ } else {
45
+ holder = "it is undisassembled in memory block " + block.getName();
46
+ }
47
+
48
+ Instruction next = listing.getInstructionAfter(address);
49
+ return "No instruction starts at " + address + ": " + holder + ". "
50
+ + (next == null ? "No instruction follows it."
51
+ : "The next instruction starts at " + next.getAddress() + ".");
52
+ }
53
+ }
@@ -7,7 +7,9 @@ whose paths were not all read (a stop, a limit, a dropped path) is undecided, ne
7
7
  from bisect import bisect_right
8
8
  from math import inf
9
9
  from .image import integer
10
- from .machine import ALIASES, NO_WRITE, State
10
+ from .machine import ALIASES, NO_WRITE, State, StopPath
11
+ from .memory_scopes import SEGMENTS
12
+ from .values import const, op
11
13
 
12
14
  KINDS = ("reach", "order", "lastWriter", "containment", "relation", "origin")
13
15
  OPERATORS = ("eq", "ne", "lt", "le", "gt", "ge")
@@ -91,6 +93,69 @@ def _operand(operand, image, name, budget):
91
93
  _term(operand, image, name)
92
94
 
93
95
 
96
+ def _address(control, image):
97
+ """Check a lastWriter address: a segment register, an optional base register, a displacement and a width."""
98
+ address, name = control["address"], control["name"]
99
+ if not isinstance(address, dict) or set(address) - {"segment", "base", "displacement", "width"} or "segment" not in address:
100
+ raise ValueError(f"Relational control {name} address names segment, and optionally base, displacement and width")
101
+ if address["segment"] not in SEGMENTS:
102
+ raise ValueError(f"Relational control {name} address segment must be a segment register")
103
+ if "base" in address:
104
+ base = address["base"]
105
+ if (not isinstance(base, str) or base not in ALIASES or ALIASES[base][0] in SEGMENTS or ALIASES[base][1] != 0
106
+ or ALIASES[base][2] != image.bits):
107
+ raise ValueError(f"Relational control {name} address base must be a {image.bits}-bit general register")
108
+ # Signed or unsigned in the address width; a wider value would silently name another offset.
109
+ displacement = address.get("displacement", 0)
110
+ if type(displacement) is not int or not -(1 << (image.bits - 1)) <= displacement < 1 << image.bits:
111
+ raise ValueError(f"Relational control {name} address displacement must be a {image.bits}-bit integer")
112
+ integer(address.get("width"), 1, 32, "lastWriter address width")
113
+
114
+
115
+ def checkpoint_anchors(config):
116
+ """The sites of every relational control's checkpoint anchors, which the trace adds to checkpoints.
117
+
118
+ Reads controls that validate_controls accepted.
119
+ """
120
+ sites = set()
121
+ for control in config.get("relationalControls", []):
122
+ anchors = control["at"] if isinstance(control["at"], list) else [control["at"]]
123
+ sites.update(a["site"] for a in anchors if a.get("event") == "checkpoint")
124
+ return sites
125
+
126
+
127
+ def memory_probes(config):
128
+ """The lastWriter addresses to inspect at each checkpoint site: {site: [(control name, address)]}.
129
+
130
+ Reads controls that validate_controls accepted.
131
+ """
132
+ probes = {}
133
+ for control in config.get("relationalControls", []):
134
+ if control.get("kind") == "lastWriter" and "address" in control:
135
+ anchors = control["at"] if isinstance(control["at"], list) else [control["at"]]
136
+ for site in {a["site"] for a in anchors}:
137
+ probes.setdefault(site, []).append((control["name"], control["address"]))
138
+ return probes
139
+
140
+
141
+ def probe_memory(state, name, address):
142
+ """What the model holds at a lastWriter address before the instruction runs, without a read event.
143
+
144
+ The row carries ``byteProducers`` as a read of those bytes would report them, or ``unresolved``
145
+ when the address cannot be inspected.
146
+ """
147
+ offset = const(address.get("displacement", 0), state.bits)
148
+ if "base" in address:
149
+ offset = op("add", state.reg(address["base"]), offset)
150
+ segment = state.segment(address["segment"])
151
+ row = {"control": name, "segment": segment.report(), "offset": offset.report(), "width": address["width"]}
152
+ try:
153
+ keys = state.keys(segment, offset, address["width"])[3]
154
+ except StopPath as error:
155
+ return {**row, "unresolved": str(error)}
156
+ return {**row, "byteProducers": [state.byte_writer(i, key) for i, key in enumerate(keys)]}
157
+
158
+
94
159
  def validate_controls(config, image):
95
160
  """Check every relational control before tracing; return them unchanged."""
96
161
  controls = config.get("relationalControls", [])
@@ -145,11 +210,15 @@ def validate_controls(config, image):
145
210
  if not isinstance(same, dict) or set(same) != {"before", "at"} or not all(isinstance(v, str) and v for v in same.values()):
146
211
  raise ValueError(f"Relational control {name} sameValue names a before field and an at field")
147
212
  elif kind == "lastWriter":
148
- allowed |= {"writers", "byteWriters"}
149
- _anchors(control, image, events=("read",))
213
+ allowed |= {"writers", "byteWriters", "address"}
214
+ _anchors(control, image, events=("checkpoint",) if "address" in control else ("read",))
215
+ if "address" in control:
216
+ _address(control, image)
150
217
  lists = [control["writers"]] if "writers" in control else control.get("byteWriters")
151
218
  if ("writers" in control) == ("byteWriters" in control) or not isinstance(lists, list) or not 1 <= len(lists) <= 32:
152
219
  raise ValueError(f"Relational control {name} needs writers or byteWriters")
220
+ if "address" in control and "byteWriters" in control and len(lists) != control["address"]["width"]:
221
+ raise ValueError(f"Relational control {name} byteWriters lists {len(lists)} bytes; its address has {control['address']['width']}")
153
222
  for writers in lists:
154
223
  if not isinstance(writers, list) or not 1 <= len(writers) <= 64:
155
224
  raise ValueError(f"Relational control {name} writer lists hold 1..64 entries")
@@ -542,11 +611,17 @@ def _occurrence(control, path, anchor, image):
542
611
  verdicts.append(verdict)
543
612
  return _worst(verdicts), detail
544
613
  if kind == "lastWriter":
545
- lists = control.get("byteWriters") or [control["writers"]] * anchor["width"]
546
- if len(lists) != anchor["width"]:
547
- raise ValueError(f"Relational control {control['name']} byteWriters lists {len(lists)} bytes; the read at {anchor['site']} has {anchor['width']}")
614
+ accessed = anchor
615
+ if "address" in control:
616
+ accessed = next(p for p in anchor["memoryProbes"] if p["control"] == control["name"])
617
+ if "unresolved" in accessed:
618
+ return "undecided", {"reason": f"the address cannot be inspected here: {accessed['unresolved']}",
619
+ "address": {k: accessed[k] for k in ("segment", "offset", "width")}}
620
+ lists = control.get("byteWriters") or [control["writers"]] * accessed["width"]
621
+ if len(lists) != accessed["width"]:
622
+ raise ValueError(f"Relational control {control['name']} byteWriters lists {len(lists)} bytes; the access at {anchor['site']} has {accessed['width']}")
548
623
  rows, verdicts = [], []
549
- for row, allowed in zip(anchor["byteProducers"], lists):
624
+ for row, allowed in zip(accessed["byteProducers"], lists):
550
625
  if row.get("writeOrder") is not None:
551
626
  writer = events[row["writeOrder"]]
552
627
  verdict = "held" if writer["site"] in allowed else "violated"
@@ -560,6 +635,8 @@ def _occurrence(control, path, anchor, image):
560
635
  verdicts.append(verdict)
561
636
  via = path.frame_branch[order]
562
637
  detail = {"bytes": rows, "via": via and {"site": via["site"], "taken": via["taken"], "order": via["order"]}}
638
+ if accessed is not anchor:
639
+ detail["address"] = {k: accessed[k] for k in ("segment", "offset", "width")}
563
640
  wrong = [r for r in rows if r["verdict"] == "violated"]
564
641
  if wrong:
565
642
  detail["reason"] = "; ".join(f"byte {r['index']} was written at site {r['writer']['site']}, not a listed writer" if r["writer"]
@@ -11,6 +11,7 @@ from .result_flow import validate_contracts, result_contracts
11
11
  from .loops import LoopTracker
12
12
  from .memory_scopes import validate_scopes, capture_scopes, retain_scopes, scope_history
13
13
  from .pcode_backend import interrupt_vector
14
+ from .relational import checkpoint_anchors, memory_probes, probe_memory
14
15
 
15
16
  def call_target(image, site, ins):
16
17
  if ins.mnemonic in ("lcall", "ljmp"):
@@ -499,7 +500,10 @@ def trace(image, config, continue_declared_jumps=True, track_loops=True, arrive=
499
500
  total_string_steps = 0
500
501
  string_limit = budget_input(config, "stringIterations")
501
502
  total_limit = budget_input(config, "totalSteps")
502
- checkpoints = set(config.get("checkpoints", []))
503
+ # A control anchored at a checkpoint gets that checkpoint; a lastWriter control with an address
504
+ # inspects it there.
505
+ probes = memory_probes(config)
506
+ checkpoints = set(config.get("checkpoints", [])) | checkpoint_anchors(config)
503
507
  # How often one path may pass the same instruction; a loop with a known bound needs it raised.
504
508
  visit_limit = budget_input(config, "visitLimit")
505
509
  # An unset field takes the value of the matching ordinary input.
@@ -732,7 +736,8 @@ def trace(image, config, continue_declared_jumps=True, track_loops=True, arrive=
732
736
  elif state.visits[at] > visit_limit:
733
737
  raise StopPath(f"instruction repeated more than {visit_limit} times; raise visitLimit or read the loop's bound")
734
738
  if at in checkpoints:
735
- state.event("checkpoint", registers=snapshot(state))
739
+ state.event("checkpoint", registers=snapshot(state),
740
+ **({"memoryProbes": [probe_memory(state, n, a) for n, a in probes[at]]} if at in probes else {}))
736
741
  m, following = ins.mnemonic, at + ins.size
737
742
  is_string = string_instruction(ins)
738
743
  if (0xf2 in ins.prefix or 0xf3 in ins.prefix) and not is_string:
@@ -19,14 +19,15 @@ def verdict(result, name):
19
19
  return next(c for c in result["relationalControls"]["controls"] if c["name"] == name)
20
20
 
21
21
 
22
- def wrapper_case(test="a8 01"):
22
+ def wrapper_case(test="a8 01", read=True):
23
23
  """A caller passes one far scratch pointer twice to a wrapper around INT 33h.
24
24
 
25
25
  The wrapper stores CX then DX through the two pointers and copies BX to AX; the caller reads
26
- the scratch word into CX and tests bit zero of AL (``test``), or of CL to test the scratch word.
26
+ the scratch word into CX (unless ``read`` is false) and tests bit zero of AL (``test``), or of
27
+ CL to test the scratch word.
27
28
  """
28
29
  c = (Code().emit("1e b8 40 00 50 1e 50").branch("e8", "wrapper").emit("83 c4 08")
29
- .label("scratch").emit("8b 0e 40 00").label("test").emit(test).branch("74", "zero").emit("c3")
30
+ .label("scratch").emit("8b 0e 40 00" if read else "").label("test").emit(test).branch("74", "zero").emit("c3")
30
31
  .label("zero").emit("c3")
31
32
  .label("wrapper").emit("55 89 e5").label("int").emit("cd 33")
32
33
  .emit("c4 7e 04").label("first").emit("26 89 0d").emit("c4 7e 08").label("second").emit("26 89 15")
@@ -200,6 +201,53 @@ class InterruptModelTests(unittest.TestCase):
200
201
  self.assertEqual(verdict(r, "later store")["verdict"], "undecided")
201
202
  self.assertEqual(verdict(r, "predicate")["verdict"], "undecided")
202
203
 
204
+ def test_a_caller_that_never_reads_the_scratch_word_names_its_last_writer_at_a_checkpoint(self):
205
+ c, service = wrapper_case(read=False)
206
+ address = {"segment": "ds", "displacement": 0x40, "width": 2}
207
+
208
+ def later(writer="second"):
209
+ return control("later store", "lastWriter", at={"site": c.labels["test"], "event": "checkpoint"},
210
+ address=address, writers=[c.labels[writer]])
211
+ r = report(c, "effects", registers=FRAME, callModels=[service], relationalControls=[later(), self.wrapper_controls(c)[1]])
212
+ self.assertTrue(r["relationalControls"]["allHeld"])
213
+ self.assertFalse([e for e in r["paths"][0]["events"] if e["kind"] == "read" and e["offset"].get("value") == 0x40])
214
+ with self.assertRaisesRegex(ValueError, "later store violated"):
215
+ report(c, registers=FRAME, callModels=[service], relationalControls=[later("first")])
216
+ # Without the frame scope the path stops in the wrapper and never reaches the checkpoint.
217
+ unscoped = {k: v for k, v in service.items() if k != "preservesMemory"}
218
+ r = report(c, registers=FRAME, callModels=[unscoped], relationalControls=[later()])
219
+ self.assertEqual(verdict(r, "later store")["verdict"], "undecided")
220
+
221
+ def test_a_query_inside_the_callers_body_returns_with_the_caller_in_entry_frame(self):
222
+ # The caller passes the far address of its own frame word twice and never reads it back.
223
+ c = (Code().emit("55 89 e5 83 ec 02").label("pushes").emit("16 8d 46 fe 50 16 50").branch("e8", "wrapper")
224
+ .emit("83 c4 08").label("test").emit("a8 01").branch("74", "zero").emit("89 ec 5d c3")
225
+ .label("zero").emit("89 ec 5d c3")
226
+ .label("wrapper").emit("55 89 e5").label("int").emit("cd 33")
227
+ .emit("c4 7e 04").label("first").emit("26 89 0d").emit("c4 7e 08").label("second").emit("26 89 15")
228
+ .emit("89 d8 5d c3"))
229
+ frame = {"segment": "ss", "base": "bp", "bytes": 12, "evidence": "synthetic saved BP, return word and four argument words"}
230
+ service = model(c.labels["int"], [{"registers": {"cx": 5, "dx": 7}}], preserves=["ds", "ss", "ebp"],
231
+ preservesMemory=[frame])
232
+ regions = [{"name": "synthetic", "start": 0, "end": len(c.bytes()), "ip": 0, "segment": 0x1000, "resident": True,
233
+ "entries": [0, c.labels["pushes"]], "evidence": "synthetic declared code extent"}]
234
+ local = {"segment": "ss", "base": "bp", "displacement": -2, "width": 2}
235
+ controls = [control("predicate", "origin", at={"site": c.labels["test"], "event": "compare"}, value={"field": "left"},
236
+ expect={"inputs": {"include": [{"modeledCall": c.labels["int"], "register": "bx"}]}}),
237
+ control("later store", "lastWriter", at={"site": c.labels["test"], "event": "checkpoint"}, address=local,
238
+ writers=[c.labels["second"]])]
239
+ query = {"regions": regions, "entry": c.labels["pushes"], "registers": {"ss": 0x3000}, "callModels": [service],
240
+ "relationalControls": controls}
241
+ # Without the frame, BP and SP are unrelated unknowns: the store through BP may alias the
242
+ # wrapper's return word, and no path returns.
243
+ r = report(c, **query)
244
+ self.assertFalse([p for p in r["paths"] if p["returned"]])
245
+ self.assertEqual(verdict(r, "predicate")["verdict"], "undecided")
246
+ r = report(c, **query, entryFrame={"from": 0})
247
+ self.assertTrue(r["entryFrame"]["established"])
248
+ self.assertTrue(all(p["returned"] for p in r["paths"]))
249
+ self.assertTrue(r["relationalControls"]["allHeld"])
250
+
203
251
  def test_an_entry_frame_trace_continues_through_a_modeled_interrupt(self):
204
252
  # From the wrapper's entry, the frame trace reaches the code after the interrupt only through the model.
205
253
  c, service = wrapper_case()
@@ -36,6 +36,20 @@ class ReachTests(unittest.TestCase):
36
36
  with self.assertRaisesRegex(ValueError, "bypass violated on path"):
37
37
  run(c, [control("bypass", "reach", at={"site": c.labels["store"], "event": "write"}, expect="never")])
38
38
 
39
+ def test_a_checkpoint_anchor_gets_its_checkpoint_without_listing_it(self):
40
+ # The store site is not in checkpoints, yet the path that reaches it reports a checkpoint
41
+ # there, so a never control is violated instead of holding for want of the event.
42
+ c = self.code()
43
+ never = control("bypass", "reach", at={"site": c.labels["store"], "event": "checkpoint"}, expect="never")
44
+ with self.assertRaisesRegex(ValueError, "bypass violated on path"):
45
+ run(c, [never])
46
+ held = verdict(run(c, [never], registers={"ax": 0}), "bypass")
47
+ self.assertEqual(held["verdict"], "held")
48
+ always = control("always", "reach", at={"site": c.labels["store"], "event": "checkpoint"}, expect="always")
49
+ result = run(c, [always], registers={"ax": 1})
50
+ self.assertEqual(verdict(result, "always")["verdict"], "held")
51
+ self.assertIn(c.labels["store"], [e["site"] for p in result["paths"] for e in p["events"] if e["kind"] == "checkpoint"])
52
+
39
53
  def test_a_returned_path_without_the_site_violates_always(self):
40
54
  c = self.code()
41
55
  with self.assertRaisesRegex(ValueError, "never reached the anchor"):
@@ -292,6 +306,97 @@ class LastWriterTests(unittest.TestCase):
292
306
  with self.assertRaisesRegex(ValueError, "lists 1 bytes"):
293
307
  run(c, [control("short", "lastWriter", at=at, byteWriters=[[c.labels["low"]]])], registers=FRAME)
294
308
 
309
+ def outputs(self):
310
+ # Two stores to one word and no read of it before the return.
311
+ return Code().label("first").emit("a3 22 00").label("second").emit("89 1e 22 00").label("end").emit("c3")
312
+
313
+ def at_end(self, c, name="slot", **fields):
314
+ fields.setdefault("address", {"segment": "ds", "displacement": 0x22, "width": 2})
315
+ return control(name, "lastWriter", at={"site": c.labels["end"], "event": "checkpoint"}, **fields)
316
+
317
+ def test_an_address_at_a_checkpoint_names_the_later_store_without_a_read(self):
318
+ c = self.outputs()
319
+ r = run(c, [self.at_end(c, writers=[c.labels["second"]])], registers=FRAME)
320
+ result = verdict(r, "slot")
321
+ self.assertEqual(result["verdict"], "held")
322
+ occurrence = result["paths"][0]["occurrences"][0]
323
+ self.assertEqual([b["writer"]["site"] for b in occurrence["bytes"]], [c.labels["second"]] * 2)
324
+ self.assertEqual((occurrence["address"]["segment"]["value"], occurrence["address"]["offset"]["value"]), (0x2000, 0x22))
325
+ # The checkpoint carries the inspected bytes; the path reports no read of them.
326
+ events = r["paths"][0]["events"]
327
+ checkpoint = next(e for e in events if e["kind"] == "checkpoint")
328
+ self.assertEqual(checkpoint["memoryProbes"][0]["control"], "slot")
329
+ self.assertFalse([e for e in events if e["kind"] == "read" and e["site"] != c.labels["end"]])
330
+ with self.assertRaisesRegex(ValueError, "first violated.*byte 0 was written at site 3"):
331
+ run(c, [self.at_end(c, "first", writers=[c.labels["first"]])], registers=FRAME)
332
+
333
+ def test_an_address_through_a_base_register_reads_the_register_at_the_checkpoint(self):
334
+ c = self.outputs()
335
+ address = {"segment": "ds", "base": "bx", "displacement": 2, "width": 2}
336
+ held = self.at_end(c, writers=[c.labels["second"]], address=address)
337
+ self.assertEqual(verdict(run(c, [held], registers={**FRAME, "bx": 0x20}), "slot")["verdict"], "held")
338
+ # With BX unknown the address may be any word, which either store may or may not have reached.
339
+ result = verdict(run(c, [held], registers=FRAME), "slot")
340
+ self.assertEqual(result["verdict"], "undecided")
341
+ self.assertEqual(result["paths"][0]["occurrences"][0]["bytes"][0]["unwritten"]["cause"],
342
+ "possibly written by an aliasing write")
343
+
344
+ def test_an_address_past_a_modeled_call_or_never_written_keeps_its_cause(self):
345
+ c = (Code().label("second").emit("89 1e 22 00").label("service").branch("e8", "external").label("end").emit("c3")
346
+ .label("external").emit("c3"))
347
+ model = [{"site": c.labels["service"], "evidence": "synthetic unread service", "cases": [{}]}]
348
+ result = verdict(run(c, [self.at_end(c, writers=[c.labels["second"]])], registers=FRAME, callModels=model), "slot")
349
+ self.assertEqual(result["verdict"], "undecided")
350
+ self.assertEqual(result["paths"][0]["occurrences"][0]["bytes"][0]["unwritten"]["cause"], "dropped by a modeled call")
351
+ other = {"segment": "ds", "displacement": 0x40, "width": 1}
352
+ held = self.at_end(c, writers=["entryState"], address=other)
353
+ self.assertEqual(verdict(run(c, [held], registers=FRAME), "slot")["verdict"], "held")
354
+ with self.assertRaisesRegex(ValueError, "slot violated.*entryState is not listed"):
355
+ run(c, [self.at_end(c, writers=[c.labels["second"]], address=other)], registers=FRAME)
356
+
357
+ def test_an_address_that_cannot_be_inspected_is_undecided(self):
358
+ c = self.outputs()
359
+ crossing = {"segment": "ds", "displacement": 0xffff, "width": 2}
360
+ result = verdict(run(c, [self.at_end(c, writers=["entryState"], address=crossing)], registers=FRAME), "slot")
361
+ self.assertEqual(result["verdict"], "undecided")
362
+ self.assertIn("cannot be inspected", result["paths"][0]["occurrences"][0]["reason"])
363
+
364
+ def test_an_address_is_checked_before_tracing(self):
365
+ c = self.outputs()
366
+ end = {"site": c.labels["end"], "event": "checkpoint"}
367
+ cases = [(control("a", "lastWriter", at={"site": c.labels["end"], "event": "read"}, writers=["entryState"],
368
+ address={"segment": "ds", "width": 2}), "checkpoint events"),
369
+ (control("a", "lastWriter", at=end, writers=["entryState"], address={"segment": "ax", "width": 2}), "segment register"),
370
+ (control("a", "lastWriter", at=end, writers=["entryState"], address={"segment": "ds", "base": "bl", "width": 2}),
371
+ "16-bit general register"),
372
+ (control("a", "lastWriter", at=end, writers=["entryState"], address={"segment": "ds", "base": "ds", "width": 2}),
373
+ "16-bit general register"),
374
+ (control("a", "lastWriter", at=end, writers=["entryState"], address={"segment": "ds", "base": None, "width": 2}),
375
+ "16-bit general register"),
376
+ (control("a", "lastWriter", at=end, writers=["entryState"], address={"segment": "ds", "base": ["bx"], "width": 2}),
377
+ "16-bit general register"),
378
+ (control("a", "lastWriter", at=end, writers=["entryState"], address={"segment": "ds", "displacement": "2", "width": 2}),
379
+ "displacement must be a 16-bit integer"),
380
+ (control("a", "lastWriter", at=end, writers=["entryState"], address={"segment": "ds", "displacement": 0x10022, "width": 2}),
381
+ "displacement must be a 16-bit integer"),
382
+ (control("a", "lastWriter", at=end, writers=["entryState"], address={"segment": "ds", "displacement": -0x8001, "width": 2}),
383
+ "displacement must be a 16-bit integer"),
384
+ (control("a", "lastWriter", at=end, writers=["entryState"], address={"segment": "ds", "width": 0}), "width"),
385
+ (control("a", "lastWriter", at=end, writers=["entryState"], address={"segment": "ds", "width": 33}), "width"),
386
+ (control("a", "lastWriter", at=end, writers=["entryState"], address={"segment": "ds", "width": 2, "index": "si"}),
387
+ "address names segment"),
388
+ (control("a", "lastWriter", at=end, byteWriters=[["entryState"]], address={"segment": "ds", "width": 2}),
389
+ "lists 1 bytes; its address has 2")]
390
+ for rule, message in cases:
391
+ with self.assertRaisesRegex(ValueError, message):
392
+ run(c, [rule], registers=FRAME)
393
+ # The widest address and a negative displacement are accepted.
394
+ widest = control("a", "lastWriter", at=end, writers=["entryState"], address={"segment": "ds", "displacement": 0x40, "width": 32})
395
+ self.assertEqual(verdict(run(c, [widest], registers=FRAME), "a")["verdict"], "held")
396
+ below = control("a", "lastWriter", at=end, writers=["entryState"], address={"segment": "ds", "displacement": -2, "width": 2})
397
+ occurrence = verdict(run(c, [below], registers=FRAME), "a")["paths"][0]["occurrences"][0]
398
+ self.assertEqual(occurrence["address"]["offset"]["value"], 0xfffe)
399
+
295
400
 
296
401
  class ContainmentTests(unittest.TestCase):
297
402
  def buffer(self, length, **extra):
@@ -1,36 +0,0 @@
1
- // Prints a bounded instruction window beginning at a virtual address.
2
- // @category Restoration
3
-
4
- import ghidra.app.script.GhidraScript;
5
- import ghidra.program.model.address.Address;
6
- import ghidra.program.model.listing.Instruction;
7
-
8
- public class ReportInstructionWindow extends GhidraScript {
9
- private static final int MAX_INSTRUCTIONS = 200;
10
-
11
- @Override
12
- protected void run() throws Exception {
13
- String[] arguments = getScriptArgs();
14
- if (arguments.length != 2) {
15
- printerr("Supply a virtual address and instruction count.");
16
- return;
17
- }
18
-
19
- Address address = toAddr(arguments[0]);
20
- int count = Integer.parseInt(arguments[1]);
21
- if (count < 1 || count > MAX_INSTRUCTIONS) {
22
- printerr("Instruction count must be between 1 and " + MAX_INSTRUCTIONS + ".");
23
- return;
24
- }
25
-
26
- Instruction instruction = currentProgram.getListing().getInstructionAt(address);
27
- if (instruction == null) {
28
- instruction = currentProgram.getListing().getInstructionAfter(address);
29
- }
30
-
31
- for (int index = 0; instruction != null && index < count; index++) {
32
- println(instruction.getAddress() + ": " + instruction);
33
- instruction = instruction.getNext();
34
- }
35
- }
36
- }