scientific-method-engine 8.1.0__tar.gz → 9.0.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.0 → scientific_method_engine-9.0.0}/PKG-INFO +6 -6
  2. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/README.md +5 -5
  3. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/pyproject.toml +1 -1
  4. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ExportBoundedFlow.java +18 -4
  5. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportInstructionContext.java +7 -0
  6. scientific_method_engine-9.0.0/src/scientific_method_engine/ghidra/ReportInstructionWindow.java +71 -0
  7. scientific_method_engine-9.0.0/src/scientific_method_engine/ghidra/scientificmethod/InstructionStart.java +53 -0
  8. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/relational.py +47 -7
  9. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/tests/test_relational_controls.py +65 -0
  10. scientific_method_engine-8.1.0/src/scientific_method_engine/ghidra/ReportInstructionWindow.java +0 -36
  11. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/.gitignore +0 -0
  12. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/LICENSE +0 -0
  13. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/__init__.py +0 -0
  14. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/__main__.py +0 -0
  15. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/cli.py +0 -0
  16. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ClearNoReturnFunctions.java +0 -0
  17. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/CreateFunctions.java +0 -0
  18. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ExportCallEdges.java +0 -0
  19. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ExportFunctionFingerprints.java +0 -0
  20. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ExportFunctionInventory.java +0 -0
  21. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/MergeFallThroughFragment.java +0 -0
  22. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/RecoverCitedFunctions.java +0 -0
  23. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/RepairReturningCallers.java +0 -0
  24. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportCallArguments.java +0 -0
  25. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportCallPaths.java +0 -0
  26. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportCallSitesWithScalars.java +0 -0
  27. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportCallsToRange.java +0 -0
  28. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportConstantFirstArgumentCalls.java +0 -0
  29. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportDataBytes.java +0 -0
  30. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportDecompileMatches.java +0 -0
  31. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportDecompileWindow.java +0 -0
  32. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportFilePatternInMemory.java +0 -0
  33. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportFirstArgumentCallSummary.java +0 -0
  34. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportFunctionScalarConstants.java +0 -0
  35. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportFunctionSummary.java +0 -0
  36. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportMemoryBlockForFileOffset.java +0 -0
  37. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportMemoryBlocks.java +0 -0
  38. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportRandomnessCandidates.java +0 -0
  39. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportReferences.java +0 -0
  40. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportScalarConstants.java +0 -0
  41. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportStringReferences.java +0 -0
  42. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportSymbolReferences.java +0 -0
  43. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/scientificmethod/FirstArgumentLookBack.java +0 -0
  44. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/__init__.py +0 -0
  45. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/argument_frames.py +0 -0
  46. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/dispatch.py +0 -0
  47. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/effect_order.py +0 -0
  48. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/image.py +0 -0
  49. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/loops.py +0 -0
  50. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/machine.py +0 -0
  51. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/memory_scopes.py +0 -0
  52. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/pcode.py +0 -0
  53. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/pcode_backend.py +0 -0
  54. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/pe.py +0 -0
  55. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/reports.py +0 -0
  56. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/result_flow.py +0 -0
  57. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/trace.py +0 -0
  58. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/values.py +0 -0
  59. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/tests/oracle.py +0 -0
  60. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/tests/test_continuation_budget.py +0 -0
  61. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/tests/test_dispatch.py +0 -0
  62. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/tests/test_effect_order.py +0 -0
  63. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/tests/test_entry_frame.py +0 -0
  64. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/tests/test_interrupt_models.py +0 -0
  65. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/tests/test_loops.py +0 -0
  66. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/tests/test_memory_scopes.py +0 -0
  67. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/tests/test_nested_frame_request.py +0 -0
  68. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/tests/test_oracle.py +0 -0
  69. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/tests/test_pe.py +0 -0
  70. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/tests/test_successor_rule.py +0 -0
  71. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.0}/tests/test_table_continuations.py +0 -0
  72. {scientific_method_engine-8.1.0 → scientific_method_engine-9.0.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.0
3
+ Version: 9.0.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 |
@@ -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 |
@@ -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.0"
8
+ version = "9.0.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,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
+ }
@@ -5,6 +5,7 @@ occurrence is held, violated or undecided. A violation on any path fails the que
5
5
  whose paths were not all read (a stop, a limit, a dropped path) is undecided, never held.
6
6
  """
7
7
  from bisect import bisect_right
8
+ from math import inf
8
9
  from .image import integer
9
10
  from .machine import ALIASES, NO_WRITE, State
10
11
 
@@ -217,11 +218,16 @@ def validate_controls(config, image):
217
218
  return controls
218
219
 
219
220
 
220
- # Linear forms: (constant, {atom: coefficient}); an atom is (term, bits) or ("signed", term, bits).
221
+ # Linear forms: (constant, {atom: coefficient}); an atom is (term, bits), ("signed", term, bits) or
222
+ # (HIDDEN, site, event), the events at a site that modeled callees before the anchor may have added.
223
+ HIDDEN = "hidden-occurrences"
224
+
221
225
 
222
226
  def _atom_range(atom, ranges):
223
227
  if atom in ranges:
224
228
  return ranges[atom]
229
+ if atom[0] == HIDDEN:
230
+ return 0, inf
225
231
  if atom[0] == "signed":
226
232
  half = 1 << (atom[2] - 1)
227
233
  return -half, half - 1
@@ -367,7 +373,8 @@ class _Path:
367
373
  return bisect_right(self.by_site.get((site, kind), ()), through)
368
374
 
369
375
  def modeled_before(self, order):
370
- return [self.events[o]["callSite"] for o in self.modeled[:bisect_right(self.modeled, order - 1)]]
376
+ # A modeled call's own return at the order counts: its callee ran before that event.
377
+ return [self.events[o]["callSite"] for o in self.modeled[:bisect_right(self.modeled, order)]]
371
378
 
372
379
  def value(self, term, anchor):
373
380
  if "entryRegister" in term:
@@ -404,7 +411,12 @@ class _Path:
404
411
  return _scaled(self.form(operand["mul"][0], anchor, ranges, modular), operand["mul"][1])
405
412
  if "occurrences" in operand:
406
413
  o = operand["occurrences"]
407
- return self.count(o["site"], o["event"], anchor["order"]), {}
414
+ count = self.count(o["site"], o["event"], anchor["order"])
415
+ # A modeled callee the path did not read may have run the counted site, so past one the
416
+ # read count is a lower bound: the form adds a hidden count of zero or more.
417
+ if self.modeled_before(anchor["order"]):
418
+ return count, {(HIDDEN, o["site"], o["event"]): 1}
419
+ return count, {}
408
420
  value = self.value(operand, anchor)
409
421
  # A form congruent modulo the value's own width is congruent modulo any narrower modulus.
410
422
  # A narrower value needs its integer value, which only a wrap-free range gives.
@@ -413,6 +425,22 @@ class _Path:
413
425
  return _integer(value["expression"], value["bits"], operand.get("signed", False), ranges)
414
426
 
415
427
 
428
+ def _bound(value):
429
+ """An interval end as reports carry it: None for an end no read event bounds."""
430
+ return None if value in (inf, -inf) else value
431
+
432
+
433
+ def _hidden(path, anchor, *forms):
434
+ """The reason for an undecided result whose forms hold counts that modeled calls may have raised."""
435
+ counted = sorted({(atom[1], atom[2]) for form in forms for atom in form[1] if atom[0] == HIDDEN})
436
+ if not counted:
437
+ return {}
438
+ calls = path.modeled_before(anchor["order"])
439
+ names = ", ".join(f"{event} events at {site}" for site, event in counted)
440
+ return {"reason": f"the count of {names} passed modeled calls at {', '.join(map(str, calls))}, whose callees may "
441
+ f"hold more of them, so the read count is only a lower bound", "modeledCalls": calls}
442
+
443
+
416
444
  def _ranges(control, path, anchor):
417
445
  """The assumed ranges by atom. Raises _Unresolved when this occurrence cannot apply one."""
418
446
  ranges = {}
@@ -489,7 +517,8 @@ def _occurrence(control, path, anchor, image):
489
517
  detail.update(taken=found.get("taken"), decidedBy=found.get("decidedBy"), branchReason=found.get("reason"))
490
518
  if found.get("taken") != control["branch"]["taken"]:
491
519
  # A modeled callee after the read execution may have run the branch again.
492
- modeled = [e["callSite"] for e in between if e["kind"] == "call-return" and e.get("modeled")]
520
+ modeled = [e["callSite"] for e in events[found["order"] + 1:order + 1]
521
+ if e["kind"] == "call-return" and e.get("modeled")]
493
522
  if modeled:
494
523
  detail.update(reason="the last read execution of the branch went the other way; a later modeled call is unread",
495
524
  modeledCalls=modeled)
@@ -554,12 +583,17 @@ def _occurrence(control, path, anchor, image):
554
583
  constant = difference[0] % size
555
584
  atoms = {a: k % size for a, k in difference[1].items() if k % size}
556
585
  if atoms:
557
- return "undecided", {"reason": f"left and right are not shown congruent modulo 2**{modulo}"}
586
+ return "undecided", _hidden(path, anchor, difference) or {
587
+ "reason": f"left and right are not shown congruent modulo 2**{modulo}"}
558
588
  same = constant == 0
559
589
  held = same if control["op"] == "eq" else not same
560
590
  return "held" if held else "violated", {"leftMinusRightModulo": constant}
561
591
  lo, hi = _interval(difference, ranges)
562
- return _decide(difference, control["op"], ranges), {"leftMinusRight": {"min": lo, "max": hi}}
592
+ verdict = _decide(difference, control["op"], ranges)
593
+ detail = {"leftMinusRight": {"min": _bound(lo), "max": _bound(hi)}}
594
+ if verdict == "undecided":
595
+ detail.update(_hidden(path, anchor, difference))
596
+ return verdict, detail
563
597
  if kind == "containment":
564
598
  interval = control["interval"]
565
599
  try:
@@ -571,6 +605,8 @@ def _occurrence(control, path, anchor, image):
571
605
  if segment["expression"] != anchor["segment"]["expression"] or segment["bits"] != anchor["segment"]["bits"]:
572
606
  return "undecided", {"reason": "the write's segment is not shown equal to the interval's segment",
573
607
  "writeSegment": anchor["segment"], "intervalSegment": segment}
608
+ if any(atom[0] == HIDDEN for atom in start[1]):
609
+ return "undecided", _hidden(path, anchor, start)
574
610
  bits = anchor["offset"]["bits"]
575
611
  size = 1 << bits
576
612
  relative = _combine(_modular(anchor["offset"]["expression"], bits, ranges), start, -1)
@@ -583,7 +619,11 @@ def _occurrence(control, path, anchor, image):
583
619
  room = _combine(_combine(length, relative, -1), (anchor["width"], {}), -1)
584
620
  rlo, rhi = _interval(relative, ranges)
585
621
  llo, lhi = _interval(length, ranges)
586
- return _decide(room, "ge", ranges), {"relativeStart": {"min": rlo, "max": rhi}, "width": anchor["width"], "length": {"min": llo, "max": lhi}}
622
+ verdict = _decide(room, "ge", ranges)
623
+ detail = {"relativeStart": {"min": rlo, "max": rhi}, "width": anchor["width"], "length": {"min": _bound(llo), "max": _bound(lhi)}}
624
+ if verdict == "undecided":
625
+ detail.update(_hidden(path, anchor, length))
626
+ return verdict, detail
587
627
  # origin
588
628
  try:
589
629
  value = path.value(control["value"], anchor)
@@ -113,6 +113,25 @@ class OrderTests(unittest.TestCase):
113
113
  with self.assertRaisesRegex(ValueError, "went the other way"):
114
114
  run(self.c, [rule], registers=FRAME)
115
115
 
116
+ def test_an_anchor_on_a_modeled_calls_return_follows_its_callee(self):
117
+ # At the modeled call's own return the callee has run, so it may have produced the before event.
118
+ self.c = (Code().branch("e8", "helper").label("service").branch("e8", "helper").label("use").emit("8a 07 c3")
119
+ .label("helper").emit("85 db").label("test").branch("74", "zero").emit("c3").label("zero").emit("c3"))
120
+ model = [{"site": self.c.labels["service"], "evidence": "synthetic unread helper call", "cases": [{}]}]
121
+ direction = control("direction", "order", before={"site": self.c.labels["test"], "event": "branch"},
122
+ branch={"taken": False}, at={"site": self.c.labels["service"], "event": "call-return"})
123
+ result = verdict(run(self.c, [direction], registers=FRAME, callModels=model), "direction")
124
+ self.assertEqual(result["verdict"], "undecided")
125
+ taken = next(o for p in result["paths"] for o in p["occurrences"] if o["taken"])
126
+ self.assertEqual(taken["modeledCalls"], [self.c.labels["service"]])
127
+ # With the first call modeled, no read event precedes that call's return.
128
+ first = [{**model[0], "site": 0}]
129
+ before = control("first", "order", before={"site": self.c.labels["test"], "event": "branch"},
130
+ at={"site": 0, "event": "call-return"})
131
+ result = verdict(run(self.c, [before], registers=FRAME, callModels=first), "first")
132
+ self.assertEqual(result["verdict"], "undecided")
133
+ self.assertEqual(result["paths"][0]["occurrences"][0]["modeledCalls"], [0])
134
+
116
135
 
117
136
  class LastWriterTests(unittest.TestCase):
118
137
  def cleanup(self):
@@ -407,6 +426,52 @@ class RelationTests(unittest.TestCase):
407
426
  rule = self.appends(16)
408
427
  self.assertEqual(verdict(run(self.c, [rule], visitLimit=8), "capacity")["verdict"], "undecided")
409
428
 
429
+ def test_an_output_count_past_a_modeled_call_is_a_lower_bound(self):
430
+ # The caller runs the append helper twice; the second call may be modeled.
431
+ c = (Code().branch("e8", "append").label("service").branch("e8", "append").label("return").emit("c3")
432
+ .label("append").label("write").emit("88 07 43 c3"))
433
+
434
+ def capacity(n):
435
+ return control("capacity", "relation", at={"site": c.labels["return"], "event": "return"}, op="le",
436
+ left={"occurrences": {"site": c.labels["write"], "event": "write"}}, right=n)
437
+ self.assertEqual(verdict(run(c, [capacity(2)], registers=FRAME), "capacity")["verdict"], "held")
438
+ with self.assertRaisesRegex(ValueError, "capacity violated"):
439
+ run(c, [capacity(1)], registers=FRAME)
440
+ # The modeled call hides the second append, so the path's count of one is only a lower bound.
441
+ model = [{"site": c.labels["service"], "evidence": "synthetic unread helper call", "cases": [{}]}]
442
+ result = verdict(run(c, [capacity(1)], registers=FRAME, callModels=model), "capacity")
443
+ self.assertEqual(result["verdict"], "undecided")
444
+ occurrence = result["paths"][0]["occurrences"][0]
445
+ self.assertIn(f"passed modeled calls at {c.labels['service']}", occurrence["reason"])
446
+ self.assertEqual(occurrence["modeledCalls"], [c.labels["service"]])
447
+ self.assertEqual(occurrence["leftMinusRight"], {"min": 0, "max": None})
448
+ self.assertEqual(result["paths"][0]["modeledCalls"], [c.labels["service"]])
449
+ # The read count already exceeds a capacity of zero, and already meets a minimum of one.
450
+ with self.assertRaisesRegex(ValueError, "capacity violated"):
451
+ run(c, [capacity(0)], registers=FRAME, callModels=model)
452
+ least = control("least", "relation", at={"site": c.labels["return"], "event": "return"}, op="ge",
453
+ left={"occurrences": {"site": c.labels["write"], "event": "write"}}, right=1)
454
+ self.assertEqual(verdict(run(c, [least], registers=FRAME, callModels=model), "least")["verdict"], "held")
455
+ # A congruence needs the exact count.
456
+ parity = control("parity", "relation", at={"site": c.labels["return"], "event": "return"}, op="eq", modulo=1,
457
+ left={"occurrences": {"site": c.labels["write"], "event": "write"}}, right=1)
458
+ result = verdict(run(c, [parity], registers=FRAME, callModels=model), "parity")
459
+ self.assertEqual(result["verdict"], "undecided")
460
+ self.assertIn("lower bound", result["paths"][0]["occurrences"][0]["reason"])
461
+ # The same count on both sides cancels: the hidden events are the same events.
462
+ same = control("same", "relation", at={"site": c.labels["return"], "event": "return"}, op="eq",
463
+ left={"occurrences": {"site": c.labels["write"], "event": "write"}},
464
+ right={"occurrences": {"site": c.labels["write"], "event": "write"}})
465
+ self.assertEqual(verdict(run(c, [same], registers=FRAME, callModels=model), "same")["verdict"], "held")
466
+ # An anchor before the modeled call counts every event up to it, so the count decides there.
467
+ first = control("first", "relation", at={"site": c.labels["write"], "event": "write"}, op="le",
468
+ left={"occurrences": {"site": c.labels["write"], "event": "write"}}, right=1)
469
+ self.assertEqual(verdict(run(c, [first], registers=FRAME, callModels=model), "first")["verdict"], "held")
470
+ # At the modeled call's own return the callee has already run, so its writes may precede the anchor.
471
+ returned = control("returned", "relation", at={"site": c.labels["service"], "event": "call-return"}, op="le",
472
+ left={"occurrences": {"site": c.labels["write"], "event": "write"}}, right=1)
473
+ self.assertEqual(verdict(run(c, [returned], registers=FRAME, callModels=model), "returned")["verdict"], "undecided")
474
+
410
475
  def test_modulo_accepts_only_equality(self):
411
476
  rule = self.terminator("c6 00 00")
412
477
  with self.assertRaisesRegex(ValueError, "modulo"):
@@ -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
- }