scientific-method-engine 8.1.1__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.1 → scientific_method_engine-9.0.0}/PKG-INFO +6 -6
  2. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/README.md +5 -5
  3. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/pyproject.toml +1 -1
  4. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ExportBoundedFlow.java +18 -4
  5. {scientific_method_engine-8.1.1 → 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.1/src/scientific_method_engine/ghidra/ReportInstructionWindow.java +0 -36
  9. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/.gitignore +0 -0
  10. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/LICENSE +0 -0
  11. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/__init__.py +0 -0
  12. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/__main__.py +0 -0
  13. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/cli.py +0 -0
  14. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ClearNoReturnFunctions.java +0 -0
  15. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/CreateFunctions.java +0 -0
  16. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ExportCallEdges.java +0 -0
  17. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ExportFunctionFingerprints.java +0 -0
  18. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ExportFunctionInventory.java +0 -0
  19. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/MergeFallThroughFragment.java +0 -0
  20. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/RecoverCitedFunctions.java +0 -0
  21. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/RepairReturningCallers.java +0 -0
  22. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportCallArguments.java +0 -0
  23. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportCallPaths.java +0 -0
  24. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportCallSitesWithScalars.java +0 -0
  25. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportCallsToRange.java +0 -0
  26. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportConstantFirstArgumentCalls.java +0 -0
  27. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportDataBytes.java +0 -0
  28. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportDecompileMatches.java +0 -0
  29. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportDecompileWindow.java +0 -0
  30. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportFilePatternInMemory.java +0 -0
  31. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportFirstArgumentCallSummary.java +0 -0
  32. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportFunctionScalarConstants.java +0 -0
  33. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportFunctionSummary.java +0 -0
  34. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportMemoryBlockForFileOffset.java +0 -0
  35. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportMemoryBlocks.java +0 -0
  36. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportRandomnessCandidates.java +0 -0
  37. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportReferences.java +0 -0
  38. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportScalarConstants.java +0 -0
  39. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportStringReferences.java +0 -0
  40. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/ReportSymbolReferences.java +0 -0
  41. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/ghidra/scientificmethod/FirstArgumentLookBack.java +0 -0
  42. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/__init__.py +0 -0
  43. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/argument_frames.py +0 -0
  44. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/dispatch.py +0 -0
  45. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/effect_order.py +0 -0
  46. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/image.py +0 -0
  47. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/loops.py +0 -0
  48. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/machine.py +0 -0
  49. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/memory_scopes.py +0 -0
  50. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/pcode.py +0 -0
  51. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/pcode_backend.py +0 -0
  52. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/pe.py +0 -0
  53. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/relational.py +0 -0
  54. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/reports.py +0 -0
  55. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/result_flow.py +0 -0
  56. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/trace.py +0 -0
  57. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/src/scientific_method_engine/x86/values.py +0 -0
  58. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/tests/oracle.py +0 -0
  59. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/tests/test_continuation_budget.py +0 -0
  60. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/tests/test_dispatch.py +0 -0
  61. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/tests/test_effect_order.py +0 -0
  62. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/tests/test_entry_frame.py +0 -0
  63. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/tests/test_interrupt_models.py +0 -0
  64. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/tests/test_loops.py +0 -0
  65. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/tests/test_memory_scopes.py +0 -0
  66. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/tests/test_nested_frame_request.py +0 -0
  67. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/tests/test_oracle.py +0 -0
  68. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/tests/test_pe.py +0 -0
  69. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/tests/test_relational_controls.py +0 -0
  70. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/tests/test_successor_rule.py +0 -0
  71. {scientific_method_engine-8.1.1 → scientific_method_engine-9.0.0}/tests/test_table_continuations.py +0 -0
  72. {scientific_method_engine-8.1.1 → 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.1
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.1"
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
+ }
@@ -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
- }