scientific-method-engine 14.0.0__tar.gz → 15.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 (82) hide show
  1. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/PKG-INFO +2 -2
  2. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/README.md +1 -1
  3. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/pyproject.toml +1 -1
  4. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ExportFunctionInventory.java +171 -22
  5. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/.gitignore +0 -0
  6. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/LICENSE +0 -0
  7. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/__init__.py +0 -0
  8. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/__main__.py +0 -0
  9. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/cli.py +0 -0
  10. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ClearNoReturnFunctions.java +0 -0
  11. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/CreateFunctions.java +0 -0
  12. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ExportBoundedFlow.java +0 -0
  13. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ExportCallEdges.java +0 -0
  14. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ExportFunctionFingerprints.java +0 -0
  15. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/MergeFallThroughFragment.java +0 -0
  16. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/RecoverCitedFunctions.java +0 -0
  17. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/RepairReturningCallers.java +0 -0
  18. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportCallArguments.java +0 -0
  19. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportCallPaths.java +0 -0
  20. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportCallSitesWithScalars.java +0 -0
  21. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportCallsToRange.java +0 -0
  22. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportConstantFirstArgumentCalls.java +0 -0
  23. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportDataBytes.java +0 -0
  24. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportDecompileMatches.java +0 -0
  25. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportDecompileWindow.java +0 -0
  26. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportFilePatternInMemory.java +0 -0
  27. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportFirstArgumentCallSummary.java +0 -0
  28. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportFunctionScalarConstants.java +0 -0
  29. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportFunctionSummary.java +0 -0
  30. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportInstructionContext.java +0 -0
  31. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportInstructionWindow.java +0 -0
  32. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportMemoryBlockForFileOffset.java +0 -0
  33. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportMemoryBlocks.java +0 -0
  34. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportRandomnessCandidates.java +0 -0
  35. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportRangeBoundaries.java +0 -0
  36. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportReferences.java +0 -0
  37. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportScalarConstants.java +0 -0
  38. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportStringReferences.java +0 -0
  39. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/ReportSymbolReferences.java +0 -0
  40. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/scientificmethod/FirstArgumentLookBack.java +0 -0
  41. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/scientificmethod/InstructionStart.java +0 -0
  42. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/scientificmethod/OperandConstants.java +0 -0
  43. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/ghidra/scientificmethod/Xxh3.java +0 -0
  44. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/x86/__init__.py +0 -0
  45. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/x86/argument_frames.py +0 -0
  46. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/x86/dispatch.py +0 -0
  47. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/x86/effect_order.py +0 -0
  48. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/x86/image.py +0 -0
  49. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/x86/inventory.py +0 -0
  50. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/x86/loops.py +0 -0
  51. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/x86/machine.py +0 -0
  52. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/x86/memory_scopes.py +0 -0
  53. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/x86/pcode.py +0 -0
  54. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/x86/pcode_backend.py +0 -0
  55. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/x86/pe.py +0 -0
  56. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/x86/reach.py +0 -0
  57. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/x86/relational.py +0 -0
  58. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/x86/reports.py +0 -0
  59. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/x86/result_flow.py +0 -0
  60. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/x86/trace.py +0 -0
  61. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/src/scientific_method_engine/x86/values.py +0 -0
  62. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/tests/oracle.py +0 -0
  63. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/tests/test_continuation_budget.py +0 -0
  64. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/tests/test_dispatch.py +0 -0
  65. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/tests/test_effect_order.py +0 -0
  66. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/tests/test_enter_frames.py +0 -0
  67. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/tests/test_entry_frame.py +0 -0
  68. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/tests/test_indirect_far_transfers.py +0 -0
  69. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/tests/test_interrupt_models.py +0 -0
  70. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/tests/test_inventory_check.py +0 -0
  71. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/tests/test_loops.py +0 -0
  72. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/tests/test_memory_scopes.py +0 -0
  73. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/tests/test_nested_frame_request.py +0 -0
  74. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/tests/test_oracle.py +0 -0
  75. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/tests/test_pe.py +0 -0
  76. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/tests/test_reach.py +0 -0
  77. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/tests/test_relational_controls.py +0 -0
  78. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/tests/test_return_checks.py +0 -0
  79. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/tests/test_successor_rule.py +0 -0
  80. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/tests/test_table_continuations.py +0 -0
  81. {scientific_method_engine-14.0.0 → scientific_method_engine-15.0.0}/tests/test_term_limit.py +0 -0
  82. {scientific_method_engine-14.0.0 → scientific_method_engine-15.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: 14.0.0
3
+ Version: 15.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
@@ -93,7 +93,7 @@ Exporting for comparison (each writes one file and refuses to overwrite where no
93
93
  | Script | Arguments | Writes |
94
94
  |---|---|---|
95
95
  | `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 |
96
- | `ExportFunctionInventory` | output path `coverage/<build>/<file>.tsv` (must not exist, nor its `.provenance.tsv`), identifier of the database snapshot the export reads | the function inventory that `standard-coverage` and `inventory-check` read: each function's start in the Standard's notation, its body size and, for a body that is not one range from the start, its ranges; and beside it a `.provenance.tsv` with the imported file's `xxh3` and SHA-256, the Ghidra version, the snapshot and the SHA-256 of the script and of its Xxh3 helper. A body byte it cannot place in the notation fails the export, which then writes nothing ([Exporting a function inventory](https://github.com/kibertoad/refurbished-dinosaurs-toolkit/blob/main/docs/ghidra-workflow.md#exporting-a-function-inventory)) |
96
+ | `ExportFunctionInventory` | output path `coverage/<build>/<file>.tsv` (must not exist, nor its `.provenance.tsv` and `.regions.tsv`), identifier of the database snapshot the export reads | the function inventory that `standard-coverage` and `inventory-check` read: each function's start in the Standard's notation, its body size and, for a body that is not one range from the start, its ranges; and beside it a `.provenance.tsv` with the imported file's `xxh3` and SHA-256, the Ghidra version, the snapshot and the SHA-256 of the script and of its Xxh3 helper; and a `.regions.tsv` that counts the instruction, defined-data and undefined bytes of each executable block, in all and outside every body, with rows for body bytes outside those blocks and for starts where no instruction starts. A body or executable block it cannot place in the notation fails the export, which then writes nothing ([Exporting a function inventory](https://github.com/kibertoad/refurbished-dinosaurs-toolkit/blob/main/docs/ghidra-workflow.md#exporting-a-function-inventory)) |
97
97
  | `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` |
98
98
  | `ExportFunctionFingerprints` | output TSV path (replaced only when the export completes) | per-function and per-instruction fingerprints with addresses normalized, for matching functions across versions |
99
99
 
@@ -77,7 +77,7 @@ Exporting for comparison (each writes one file and refuses to overwrite where no
77
77
  | Script | Arguments | Writes |
78
78
  |---|---|---|
79
79
  | `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 |
80
- | `ExportFunctionInventory` | output path `coverage/<build>/<file>.tsv` (must not exist, nor its `.provenance.tsv`), identifier of the database snapshot the export reads | the function inventory that `standard-coverage` and `inventory-check` read: each function's start in the Standard's notation, its body size and, for a body that is not one range from the start, its ranges; and beside it a `.provenance.tsv` with the imported file's `xxh3` and SHA-256, the Ghidra version, the snapshot and the SHA-256 of the script and of its Xxh3 helper. A body byte it cannot place in the notation fails the export, which then writes nothing ([Exporting a function inventory](https://github.com/kibertoad/refurbished-dinosaurs-toolkit/blob/main/docs/ghidra-workflow.md#exporting-a-function-inventory)) |
80
+ | `ExportFunctionInventory` | output path `coverage/<build>/<file>.tsv` (must not exist, nor its `.provenance.tsv` and `.regions.tsv`), identifier of the database snapshot the export reads | the function inventory that `standard-coverage` and `inventory-check` read: each function's start in the Standard's notation, its body size and, for a body that is not one range from the start, its ranges; and beside it a `.provenance.tsv` with the imported file's `xxh3` and SHA-256, the Ghidra version, the snapshot and the SHA-256 of the script and of its Xxh3 helper; and a `.regions.tsv` that counts the instruction, defined-data and undefined bytes of each executable block, in all and outside every body, with rows for body bytes outside those blocks and for starts where no instruction starts. A body or executable block it cannot place in the notation fails the export, which then writes nothing ([Exporting a function inventory](https://github.com/kibertoad/refurbished-dinosaurs-toolkit/blob/main/docs/ghidra-workflow.md#exporting-a-function-inventory)) |
81
81
  | `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` |
82
82
  | `ExportFunctionFingerprints` | output TSV path (replaced only when the export completes) | per-function and per-instruction fingerprints with addresses normalized, for matching functions across versions |
83
83
 
@@ -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 = "14.0.0"
8
+ version = "15.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"
@@ -1,4 +1,4 @@
1
- // Exports every function's start, size and body ranges in the Standard's notation, with a provenance file.
1
+ // Exports every function's start, size and body ranges in the Standard's notation, with provenance and regions files.
2
2
  // @category Restoration
3
3
 
4
4
  import java.io.BufferedWriter;
@@ -9,6 +9,7 @@ import java.nio.file.Path;
9
9
  import java.security.MessageDigest;
10
10
  import java.util.ArrayList;
11
11
  import java.util.HexFormat;
12
+ import java.util.Iterator;
12
13
  import java.util.LinkedHashMap;
13
14
  import java.util.List;
14
15
  import java.util.Map;
@@ -24,8 +25,10 @@ import ghidra.program.model.address.AddressSetView;
24
25
  import ghidra.program.model.address.AddressSpace;
25
26
  import ghidra.program.model.address.SegmentedAddress;
26
27
  import ghidra.program.model.address.SegmentedAddressSpace;
28
+ import ghidra.program.model.listing.CodeUnit;
27
29
  import ghidra.program.model.listing.Function;
28
30
  import ghidra.program.model.listing.FunctionIterator;
31
+ import ghidra.program.model.listing.Listing;
29
32
  import ghidra.program.model.mem.Memory;
30
33
  import ghidra.program.model.mem.MemoryBlock;
31
34
  import ghidra.program.model.mem.MemoryBlockSourceInfo;
@@ -62,7 +65,9 @@ public class ExportFunctionInventory extends GhidraScript {
62
65
  if (!name.endsWith(".tsv") || name.endsWith(".provenance.tsv") || name.endsWith(".regions.tsv")) {
63
66
  throw new IllegalArgumentException("The inventory path must end in .tsv and not be a side file: " + output);
64
67
  }
65
- Path provenance = output.resolveSibling(name.substring(0, name.length() - 4) + ".provenance.tsv");
68
+ String stem = name.substring(0, name.length() - 4);
69
+ Path provenance = output.resolveSibling(stem + ".provenance.tsv");
70
+ Path regions = output.resolveSibling(stem + ".regions.tsv");
66
71
  String snapshot = arguments[1];
67
72
  if (snapshot.isEmpty() || snapshot.length() > 200 || snapshot.chars().anyMatch(Character::isISOControl)) {
68
73
  throw new IllegalArgumentException("The snapshot identifier must be 1 to 200 characters with no tab or line break.");
@@ -70,11 +75,14 @@ public class ExportFunctionInventory extends GhidraScript {
70
75
  if (Files.exists(output)) {
71
76
  throw new IllegalArgumentException("Output already exists: " + output);
72
77
  }
73
- if (Files.exists(provenance)) {
74
- // The provenance moves into place first, so one without its inventory is left by an export
75
- // that stopped between the two moves. It is not deleted here, since it may be the user's own.
76
- throw new IllegalArgumentException("Output already exists: " + provenance + ". With no inventory beside"
77
- + " it, an earlier export stopped before moving the inventory into place; remove it and export again.");
78
+ for (Path side : List.of(provenance, regions)) {
79
+ if (Files.exists(side)) {
80
+ // The side files move into place before the inventory, so one without its inventory is left
81
+ // by an export that stopped between the moves. It is not deleted here, since it may be the
82
+ // user's own.
83
+ throw new IllegalArgumentException("Output already exists: " + side + ". With no inventory beside"
84
+ + " it, an earlier export stopped before moving the inventory into place; remove it and export again.");
85
+ }
78
86
  }
79
87
 
80
88
  notation = notation();
@@ -85,6 +93,8 @@ public class ExportFunctionInventory extends GhidraScript {
85
93
  Memory memory = currentProgram.getMemory();
86
94
  List<String> rows = new ArrayList<>();
87
95
  Map<String, Address> starts = new LinkedHashMap<>();
96
+ AddressSet bodies = new AddressSet();
97
+ List<Address> entries = new ArrayList<>();
88
98
  long bytes = 0;
89
99
  long withRanges = 0;
90
100
  FunctionIterator functions = currentProgram.getFunctionManager().getFunctions(true);
@@ -94,8 +104,11 @@ public class ExportFunctionInventory extends GhidraScript {
94
104
  }
95
105
  Function function = functions.next();
96
106
  Address entry = function.getEntryPoint();
97
- List<Piece> pieces = place(memory, entry, function.getBody(), true);
98
- Piece first = place(memory, entry, new AddressSet(entry), false).stream().findFirst().orElse(null);
107
+ bodies.add(function.getBody());
108
+ entries.add(entry);
109
+ String owner = "Function at " + entry + ": body";
110
+ List<Piece> pieces = place(memory, owner, function.getBody(), true);
111
+ Piece first = place(memory, owner, new AddressSet(entry), false).stream().findFirst().orElse(null);
99
112
  if (first == null || pieces.isEmpty()) {
100
113
  continue; // The body's problems include the byte at the start, or every byte of the body.
101
114
  }
@@ -135,6 +148,8 @@ public class ExportFunctionInventory extends GhidraScript {
135
148
  bytes += size;
136
149
  }
137
150
 
151
+ List<String> regionRows = regionRows(memory, bodies, entries);
152
+
138
153
  if (!problems.isEmpty()) {
139
154
  for (String line : problems.subList(0, Math.min(problems.size(), MAX_PROBLEM_LINES))) {
140
155
  printerr(line);
@@ -143,15 +158,17 @@ public class ExportFunctionInventory extends GhidraScript {
143
158
  printerr("Output stopped after " + MAX_PROBLEM_LINES + " of " + problems.size() + " problems.");
144
159
  }
145
160
  throw new IllegalStateException((problems.size() == 1 ? "1 problem keeps" : problems.size() + " problems keep")
146
- + " the inventory from listing every function's body in the Standard's notation. Nothing was written.");
161
+ + " the inventory and its regions from being written in the Standard's notation. Nothing was written.");
147
162
  }
148
163
 
149
164
  Files.createDirectories(output.getParent());
150
165
  Path inventoryPart = null;
151
166
  Path provenancePart = null;
167
+ Path regionsPart = null;
152
168
  try {
153
169
  inventoryPart = Files.createTempFile(output.getParent(), "inventory-", ".partial");
154
170
  provenancePart = Files.createTempFile(output.getParent(), "provenance-", ".partial");
171
+ regionsPart = Files.createTempFile(output.getParent(), "regions-", ".partial");
155
172
  try (BufferedWriter writer = Files.newBufferedWriter(inventoryPart, StandardCharsets.UTF_8)) {
156
173
  writer.write("start\tsize\tranges\n");
157
174
  for (String row : rows) {
@@ -164,12 +181,27 @@ public class ExportFunctionInventory extends GhidraScript {
164
181
  writer.write(line.getKey() + "\t" + line.getValue() + "\n");
165
182
  }
166
183
  }
184
+ try (BufferedWriter writer = Files.newBufferedWriter(regionsPart, StandardCharsets.UTF_8)) {
185
+ writer.write(String.join("\t", "kind", "start", "size", "instructions", "instructions_outside", "data",
186
+ "data_outside", "undefined", "undefined_outside") + "\n");
187
+ for (String row : regionRows) {
188
+ writer.write(row);
189
+ writer.write('\n');
190
+ }
191
+ }
167
192
  // No replacement. The inventory moves last, so a run that stops before it leaves no
168
- // inventory, and a provenance file without its inventory is removed.
193
+ // inventory, and side files moved without their inventory are removed.
169
194
  Files.move(provenancePart, provenance);
195
+ boolean regionsMoved = false;
170
196
  try {
197
+ Files.move(regionsPart, regions);
198
+ regionsMoved = true;
171
199
  Files.move(inventoryPart, output);
172
200
  } catch (Exception e) {
201
+ // A regions file that was there when its move failed is not this run's.
202
+ if (regionsMoved) {
203
+ Files.deleteIfExists(regions);
204
+ }
173
205
  Files.deleteIfExists(provenance);
174
206
  throw e;
175
207
  }
@@ -180,9 +212,13 @@ public class ExportFunctionInventory extends GhidraScript {
180
212
  if (provenancePart != null) {
181
213
  Files.deleteIfExists(provenancePart);
182
214
  }
215
+ if (regionsPart != null) {
216
+ Files.deleteIfExists(regionsPart);
217
+ }
183
218
  }
184
219
  println("Exported " + rows.size() + " functions with " + bytes + " body bytes, " + withRanges
185
- + " of them with a ranges column, to " + output + ", and their provenance to " + provenance);
220
+ + " of them with a ranges column, to " + output + ", their provenance to " + provenance + ", and "
221
+ + regionRows.size() + " region and anomaly rows to " + regions);
186
222
  }
187
223
 
188
224
  /** The Standard's address notation for this program, or an exception naming why it has none here. */
@@ -266,11 +302,11 @@ public class ExportFunctionInventory extends GhidraScript {
266
302
  }
267
303
 
268
304
  /**
269
- * The body split into placed pieces. A byte in an overlay block is placed by its offset in the
305
+ * The addresses split into placed pieces. A byte in an overlay block is placed by its offset in the
270
306
  * imported file, which the Standard allows only in an MZ file; any other byte by its address. With
271
- * record, a byte that cannot be placed is recorded as a problem with the reason.
307
+ * record, a byte that cannot be placed is recorded as a problem with the reason, after owner.
272
308
  */
273
- private List<Piece> place(Memory memory, Address entry, AddressSetView body, boolean record) {
309
+ private List<Piece> place(Memory memory, String owner, AddressSetView body, boolean record) {
274
310
  List<Piece> pieces = new ArrayList<>();
275
311
  int before = problems.size();
276
312
  AddressSet left = new AddressSet(body);
@@ -287,7 +323,7 @@ public class ExportFunctionInventory extends GhidraScript {
287
323
  for (AddressRange range : part) {
288
324
  Piece piece = addressPiece(range);
289
325
  if (piece == null) {
290
- problem(entry, range, noWrittenForm(range));
326
+ problem(owner, range, noWrittenForm(range));
291
327
  } else {
292
328
  pieces.add(piece);
293
329
  }
@@ -296,7 +332,7 @@ public class ExportFunctionInventory extends GhidraScript {
296
332
  }
297
333
  if (!fileOffsets) {
298
334
  for (AddressRange range : part) {
299
- problem(entry, range, "is in overlay block " + block.getName()
335
+ problem(owner, range, "is in overlay block " + block.getName()
300
336
  + ", and the Standard places code by file offset only in an MZ file");
301
337
  }
302
338
  continue;
@@ -307,7 +343,7 @@ public class ExportFunctionInventory extends GhidraScript {
307
343
  for (AddressRange range : sourced) {
308
344
  FileBytes from = info.getFileBytes().orElse(null);
309
345
  if (from == null || !from.equals(imported)) {
310
- problem(entry, range, "is in overlay block " + block.getName() + ", whose bytes there come "
346
+ problem(owner, range, "is in overlay block " + block.getName() + ", whose bytes there come "
311
347
  + (from == null ? "from no file" : "from " + from.getFilename() + ", not the imported file"));
312
348
  continue;
313
349
  }
@@ -316,11 +352,11 @@ public class ExportFunctionInventory extends GhidraScript {
316
352
  }
317
353
  }
318
354
  for (AddressRange range : part) {
319
- problem(entry, range, "is in overlay block " + block.getName() + ", which gives it no source");
355
+ problem(owner, range, "is in overlay block " + block.getName() + ", which gives it no source");
320
356
  }
321
357
  }
322
358
  for (AddressRange range : left) {
323
- problem(entry, range, "is in no memory block");
359
+ problem(owner, range, "is in no memory block");
324
360
  }
325
361
  if (!record) {
326
362
  problems.subList(before, problems.size()).clear();
@@ -389,8 +425,121 @@ public class ExportFunctionInventory extends GhidraScript {
389
425
  return segment > 0xFFFF ? null : String.format("%04X:%04X", segment, offset);
390
426
  }
391
427
 
392
- private void problem(Address entry, AddressRange range, String why) {
393
- problems.add("Function at " + entry + ": body " + range.getMinAddress() + ".." + range.getMaxAddress()
428
+ /**
429
+ * The rows of the regions file: a region row for each initialized executable block, or for each
430
+ * part of an overlay block that one file range supplies; then an outside row for each stretch of a
431
+ * function body in no region, split the same way at block and source boundaries; then an entry row
432
+ * for each function start where no instruction starts. Each row counts the instruction, defined-data and undefined bytes of its range, in all
433
+ * and outside every function body. A range that cannot be written in the notation is a problem.
434
+ * The outside and entry rows lie in function bodies, so when a body could not be placed they would
435
+ * repeat its problem, and only the region rows are checked.
436
+ */
437
+ private List<String> regionRows(Memory memory, AddressSet bodies, List<Address> entries) {
438
+ boolean bodiesPlaced = problems.isEmpty();
439
+ List<String> rows = new ArrayList<>();
440
+ AddressSet measured = new AddressSet();
441
+ for (MemoryBlock block : memory.getBlocks()) {
442
+ if (!block.isInitialized() || !block.isExecute()) {
443
+ continue;
444
+ }
445
+ AddressSet whole = new AddressSet(block.getStart(), block.getEnd());
446
+ measured.add(whole);
447
+ for (AddressSet part : blockParts(memory, whole)) {
448
+ row(rows, memory, "region", "Executable block " + block.getName() + ":", part, bodies);
449
+ }
450
+ }
451
+ if (!bodiesPlaced) {
452
+ return rows;
453
+ }
454
+ for (AddressSet part : blockParts(memory, bodies.subtract(measured))) {
455
+ row(rows, memory, "outside", "Function body outside every executable block:", part, bodies);
456
+ }
457
+ Listing listing = currentProgram.getListing();
458
+ for (Address entry : entries) {
459
+ if (listing.getInstructionAt(entry) == null) {
460
+ row(rows, memory, "entry", "Function start without an instruction:", new AddressSet(entry), bodies);
461
+ }
462
+ }
463
+ return rows;
464
+ }
465
+
466
+ /**
467
+ * The set split into the contiguous parts that place() writes as one piece each: a part per memory
468
+ * block and, in an overlay block, per source range, then the parts in no block.
469
+ */
470
+ private static List<AddressSet> blockParts(Memory memory, AddressSetView set) {
471
+ List<AddressSet> parts = new ArrayList<>();
472
+ AddressSet left = new AddressSet(set);
473
+ for (MemoryBlock block : memory.getBlocks()) {
474
+ if (left.isEmpty()) {
475
+ break;
476
+ }
477
+ AddressSet part = left.intersectRange(block.getStart(), block.getEnd());
478
+ if (part.isEmpty()) {
479
+ continue;
480
+ }
481
+ left.delete(part);
482
+ if (block.getStart().getAddressSpace().isOverlaySpace()) {
483
+ for (MemoryBlockSourceInfo info : block.getSourceInfos()) {
484
+ AddressSet sourced = part.intersectRange(info.getMinAddress(), info.getMaxAddress());
485
+ part.delete(sourced);
486
+ for (AddressRange range : sourced) {
487
+ parts.add(new AddressSet(range));
488
+ }
489
+ }
490
+ }
491
+ for (AddressRange range : part) {
492
+ parts.add(new AddressSet(range));
493
+ }
494
+ }
495
+ for (AddressRange range : left) {
496
+ parts.add(new AddressSet(range));
497
+ }
498
+ return parts;
499
+ }
500
+
501
+ /**
502
+ * Adds the row of one part from blockParts(), or records the problem that keeps it from being
503
+ * written. Such a part places as one piece or not at all.
504
+ */
505
+ private void row(List<String> rows, Memory memory, String kind, String what, AddressSet range, AddressSet bodies) {
506
+ List<Piece> pieces = place(memory, what, range, true);
507
+ if (pieces.size() != 1) {
508
+ return; // place() recorded why the range has no written form.
509
+ }
510
+ AddressSet outside = range.subtract(bodies.intersectRange(range.getMinAddress(), range.getMaxAddress()));
511
+ AddressSet instructions = covered(range, true);
512
+ AddressSet data = covered(range, false);
513
+ AddressSet undefined = range.subtract(instructions).subtract(data);
514
+ rows.add(String.join("\t", kind, pieces.get(0).startText(), Long.toString(range.getNumAddresses()),
515
+ count(instructions), count(instructions.intersect(outside)), count(data), count(data.intersect(outside)),
516
+ count(undefined), count(undefined.intersect(outside))));
517
+ }
518
+
519
+ /** The bytes of the range that instructions, or defined data, cover, including a unit that starts before it. */
520
+ private AddressSet covered(AddressSet range, boolean instructions) {
521
+ Listing listing = currentProgram.getListing();
522
+ AddressSet covered = new AddressSet();
523
+ Address first = range.getMinAddress();
524
+ CodeUnit before = instructions ? listing.getInstructionContaining(first) : listing.getDefinedDataContaining(first);
525
+ if (before != null) {
526
+ covered.add(before.getMinAddress(), before.getMaxAddress());
527
+ }
528
+ Iterator<? extends CodeUnit> units = instructions ? listing.getInstructions(range, true)
529
+ : listing.getDefinedData(range, true);
530
+ while (units.hasNext()) {
531
+ CodeUnit unit = units.next();
532
+ covered.add(unit.getMinAddress(), unit.getMaxAddress());
533
+ }
534
+ return covered.intersect(range);
535
+ }
536
+
537
+ private static String count(AddressSetView set) {
538
+ return Long.toString(set.getNumAddresses());
539
+ }
540
+
541
+ private void problem(String owner, AddressRange range, String why) {
542
+ problems.add(owner + " " + range.getMinAddress() + ".." + range.getMaxAddress()
394
543
  + " (" + range.getLength() + " bytes) " + why + ".");
395
544
  }
396
545