scientific-method-engine 18.2.0__tar.gz → 18.4.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.
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/PKG-INFO +1 -1
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/pyproject.toml +1 -1
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/cli.py +8 -0
- scientific_method_engine-18.4.0/src/scientific_method_engine/x86/dispatch.py +168 -0
- scientific_method_engine-18.4.0/src/scientific_method_engine/x86/inventory.py +478 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/x86/reach.py +120 -60
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/x86/reports.py +24 -5
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/x86/trace.py +26 -6
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_inventory_check.py +213 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_reach.py +174 -0
- scientific_method_engine-18.2.0/src/scientific_method_engine/x86/dispatch.py +0 -51
- scientific_method_engine-18.2.0/src/scientific_method_engine/x86/inventory.py +0 -262
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/.gitignore +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/LICENSE +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/README.md +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/__init__.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/__main__.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ClearNoReturnFunctions.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/CreateFunctions.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ExportBoundedFlow.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ExportCallEdges.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ExportFunctionFingerprints.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ExportFunctionInventory.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/MergeFallThroughFragment.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/RecoverCitedFunctions.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/RepairReturningCallers.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportCallArguments.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportCallPaths.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportCallSitesWithScalars.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportCallsToRange.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportConstantFirstArgumentCalls.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportDataBytes.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportDecompileMatches.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportDecompileWindow.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportFilePatternInMemory.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportFirstArgumentCallSummary.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportFunctionScalarConstants.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportFunctionSummary.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportInstructionContext.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportInstructionWindow.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportMemoryBlockForFileOffset.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportMemoryBlocks.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportRandomnessCandidates.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportRangeBoundaries.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportReferences.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportScalarConstants.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportStringReferences.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/ReportSymbolReferences.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/scientificmethod/FirstArgumentLookBack.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/scientificmethod/InstructionStart.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/scientificmethod/OperandConstants.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/ghidra/scientificmethod/Xxh3.java +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/x86/__init__.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/x86/argument_frames.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/x86/effect_order.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/x86/image.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/x86/loops.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/x86/machine.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/x86/memory_scopes.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/x86/pcode.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/x86/pcode_backend.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/x86/pe.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/x86/relational.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/x86/result_flow.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/src/scientific_method_engine/x86/values.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/oracle.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_config_encoding.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_continuation_budget.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_dispatch.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_effect_order.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_enter_frames.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_entry_frame.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_indirect_far_transfers.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_interrupt_models.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_join_terms.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_loops.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_memory_scopes.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_nested_frame_request.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_oracle.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_pe.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_relational_controls.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_return_checks.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_successor_rule.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_table_continuations.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_term_limit.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_upstream_gaps.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.0}/tests/test_volatile_memory.py +0 -0
- {scientific_method_engine-18.2.0 → scientific_method_engine-18.4.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: 18.
|
|
3
|
+
Version: 18.4.0
|
|
4
4
|
Summary: Bounded instruction-derived x86 evidence reports for segmented MZ/FBOV and PE32/i386 code.
|
|
5
5
|
Project-URL: Source, https://github.com/kibertoad/refurbished-dinosaurs-toolkit/tree/main/packages/scientific-method-engine
|
|
6
6
|
Author: kibertoad
|
|
@@ -5,7 +5,7 @@ build-backend = "hatchling.build"
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "scientific-method-engine"
|
|
7
7
|
# The release workflow writes the published version from the package's release tag.
|
|
8
|
-
version = "18.
|
|
8
|
+
version = "18.4.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"
|
|
@@ -27,12 +27,20 @@ USAGE = ("Usage: scientific-method-engine <operand|operand-candidates|target|bou
|
|
|
27
27
|
"a call to a noReturn routine and a noReturn interrupt do not continue at the next instruction,\n"
|
|
28
28
|
"and a noReturn routine with a return, or a jump into a returning leaf, on its own read paths\n"
|
|
29
29
|
"is reported contradicted;\n"
|
|
30
|
+
"indirectCalls declares the targets of a computed call as a table read from the build or a list,\n"
|
|
31
|
+
"and the walk calls each of them; one not declared exhaustive stays unresolved;\n"
|
|
30
32
|
"controls are call sites the walk must reach and resolve, instructionControls sites it must decode,\n"
|
|
31
33
|
"and a failed control fails the report saying whether it was reached.\n"
|
|
32
34
|
"inventory-check places every resolved direct call target in the notation of the function inventory TSV\n"
|
|
33
35
|
"that inventory names and lists each target no row starts at: inside another row's body or outside every row,\n"
|
|
34
36
|
"with one calling site, near or far, and whether an entry-path call, a contested one or only raw bytes call it;\n"
|
|
35
37
|
"a target inside an instruction the entry-path walk established from another start names it as insideInstruction.\n"
|
|
38
|
+
"It lists each row whose start lies inside an instruction the walk established (rowStarts), with the\n"
|
|
39
|
+
"routine the walk read it in; a row start in bytes the walk did not decode is counted, not placed.\n"
|
|
40
|
+
"noReturn takes reach's declarations: the walk stops at a call to a declared routine and at a declared\n"
|
|
41
|
+
"interrupt, a declared routine with a return on its own read paths is reported contradicted, and\n"
|
|
42
|
+
"rowsPastNoReturn lists each row whose body continues past such a call or interrupt.\n"
|
|
43
|
+
"indirectCalls takes reach's declarations: each declared target of a reached computed call is a call target.\n"
|
|
36
44
|
"uses matches accesses whose footprint intersects query offset..offset+width; its rawCandidates give each unreached\n"
|
|
37
45
|
"operand's encoded displacement, width, access, segment register and intersection, and never count as uses.\n"
|
|
38
46
|
"trace, arguments, effects, returns, guards, memory and allocation check relationalControls:\n"
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"""Explicit, source-derived indirect jump tables and call targets for CFG discovery, and explicitly conditional path continuations."""
|
|
2
|
+
from capstone.x86 import X86_OP_IMM, X86_OP_MEM
|
|
3
|
+
from .image import integer
|
|
4
|
+
|
|
5
|
+
CALL_NOT_EXHAUSTIVE = "indirect call targets are not declared exhaustive"
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def _word(image, at):
|
|
9
|
+
return int.from_bytes(image.data[at:at + 2], "little")
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _table_operands(image, table, width, label, field_label):
|
|
13
|
+
"""The file offset of each row's target field in a declared ``table`` of ``width``-byte targets."""
|
|
14
|
+
start = integer(table.get("start"), 0, len(image.data) - 1, f"{label} start")
|
|
15
|
+
count = integer(table.get("count"), 1, 256, f"{label} count")
|
|
16
|
+
stride = integer(table.get("stride"), width, 65536, f"{label} stride")
|
|
17
|
+
field = integer(table.get("fieldOffset", 0), 0, stride - width, field_label)
|
|
18
|
+
if start + (count - 1) * stride + field + width > len(image.data):
|
|
19
|
+
raise ValueError(f"{label[0].upper()}{label[1:]} leaves source bounds")
|
|
20
|
+
return [start + index * stride + field for index in range(count)]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def read_indirect_jumps(image):
|
|
24
|
+
declarations = image.config.get("indirectJumps", [])
|
|
25
|
+
if not isinstance(declarations, list) or len(declarations) > 256:
|
|
26
|
+
raise ValueError("indirectJumps must contain at most 256 declarations")
|
|
27
|
+
if declarations and image.flat:
|
|
28
|
+
raise ValueError("indirectJumps currently requires segmented16")
|
|
29
|
+
result = {}
|
|
30
|
+
for claim in declarations:
|
|
31
|
+
if not isinstance(claim, dict):
|
|
32
|
+
raise ValueError("Each indirect jump needs an object")
|
|
33
|
+
site = integer(claim.get("site"), 0, len(image.data) - 1, "indirect jump site")
|
|
34
|
+
if site in result:
|
|
35
|
+
raise ValueError("Duplicate indirect jump site")
|
|
36
|
+
if not isinstance(claim.get("evidence"), str) or not claim["evidence"].strip():
|
|
37
|
+
raise ValueError("An indirect jump needs consumer/mapping evidence")
|
|
38
|
+
if type(claim.get("exhaustive")) is not bool:
|
|
39
|
+
raise ValueError("An indirect jump needs explicit exhaustive true or false")
|
|
40
|
+
ins = image.decode(site)
|
|
41
|
+
if (ins is None or ins.mnemonic != "jmp" or len(ins.operands) != 1
|
|
42
|
+
or ins.operands[0].type == X86_OP_IMM or ins.operands[0].size != 2
|
|
43
|
+
or 0x66 in ins.prefix or 0x67 in ins.prefix):
|
|
44
|
+
raise ValueError("indirectJumps site must decode as an unprefixed computed near word jump")
|
|
45
|
+
table = claim.get("table")
|
|
46
|
+
if not isinstance(table, dict) or not isinstance(table.get("evidence"), str) or not table["evidence"].strip():
|
|
47
|
+
raise ValueError("Indirect table needs layout/count evidence")
|
|
48
|
+
if type(table.get("width", 2)) is not int or table.get("width", 2) != 2:
|
|
49
|
+
raise ValueError("Indirect table targets must be word width 2")
|
|
50
|
+
rows = []
|
|
51
|
+
operands = _table_operands(image, table, 2, "indirect table", "indirect target field offset")
|
|
52
|
+
for index, operand in enumerate(operands):
|
|
53
|
+
raw = _word(image, operand)
|
|
54
|
+
target = image.near_target(site, raw)
|
|
55
|
+
if target is None:
|
|
56
|
+
raise ValueError("Indirect table target leaves declared code mappings")
|
|
57
|
+
rows.append({"index": index, "operandSite": operand, "rawOffset": raw, "target": target})
|
|
58
|
+
result[site] = {"site": site, "evidence": claim["evidence"], "table": table,
|
|
59
|
+
"exhaustive": claim["exhaustive"], "rows": rows,
|
|
60
|
+
"interpretation": "source words under supplied consumer, mapping and exhaustiveness evidence; "
|
|
61
|
+
"not executed dispatch or an overlap-boundary proof"}
|
|
62
|
+
return result
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def indirect_call_declarations(value, image):
|
|
66
|
+
"""The declared targets of computed calls, by call site, as ``reach`` and ``inventory-check`` take them.
|
|
67
|
+
|
|
68
|
+
Each declaration names a ``site`` that decodes as an unprefixed segmented16 computed call (a near
|
|
69
|
+
call through a word register or memory operand, or a far call through memory), its ``evidence``,
|
|
70
|
+
an explicit boolean ``exhaustive``, and exactly one of ``table`` and ``targets``. A table's rows are
|
|
71
|
+
read from the source bytes as ``indirectJumps`` reads them: words placed through the call site's
|
|
72
|
+
region mapping for a near call, and for a far call ``offset, segment`` word pairs whose segment
|
|
73
|
+
word has a declared relocation, admitted as ``Image.far_pointer_target`` admits the pointer a
|
|
74
|
+
traced far call reads; a far row whose pointer names an FBOV trampoline carries its
|
|
75
|
+
``trampoline``. ``targets`` gives file offsets in declared code, which rest on the evidence
|
|
76
|
+
alone; a near call's targets must be bytes some IP in the call site's segment places. Each
|
|
77
|
+
declaration is returned with the call's ``instruction`` text, a table's ``rows``, and its
|
|
78
|
+
distinct ``targets`` in row order.
|
|
79
|
+
"""
|
|
80
|
+
if not isinstance(value, list) or len(value) > 256:
|
|
81
|
+
raise ValueError("indirectCalls must be a list of at most 256 declarations")
|
|
82
|
+
if value and image.flat:
|
|
83
|
+
raise ValueError("indirectCalls currently requires segmented16")
|
|
84
|
+
result = {}
|
|
85
|
+
for claim in value:
|
|
86
|
+
keys = set(claim) if isinstance(claim, dict) else None
|
|
87
|
+
if keys not in ({"site", "evidence", "exhaustive", "table"}, {"site", "evidence", "exhaustive", "targets"}):
|
|
88
|
+
raise ValueError("Each indirect call needs site, evidence, exhaustive and exactly one of table and targets")
|
|
89
|
+
site = integer(claim["site"], 0, len(image.data) - 1, "indirect call site")
|
|
90
|
+
if site in result:
|
|
91
|
+
raise ValueError("Duplicate indirect call site")
|
|
92
|
+
if not isinstance(claim["evidence"], str) or not claim["evidence"].strip():
|
|
93
|
+
raise ValueError("An indirect call needs evidence that connects the call's operand to its targets")
|
|
94
|
+
if type(claim["exhaustive"]) is not bool:
|
|
95
|
+
raise ValueError("An indirect call needs explicit exhaustive true or false")
|
|
96
|
+
ins = image.decode(site)
|
|
97
|
+
far = ins is not None and ins.mnemonic == "lcall"
|
|
98
|
+
if (ins is None or ins.mnemonic not in ("call", "lcall") or len(ins.operands) != 1
|
|
99
|
+
or ins.operands[0].type == X86_OP_IMM or (far and ins.operands[0].type != X86_OP_MEM)
|
|
100
|
+
or (not far and ins.operands[0].size != 2) or 0x66 in ins.prefix or 0x67 in ins.prefix):
|
|
101
|
+
raise ValueError(f"indirect call site {site} must decode as an unprefixed computed near word call "
|
|
102
|
+
"or a far call through memory")
|
|
103
|
+
declaration = {"site": site, "instruction": (ins.mnemonic + " " + ins.op_str).strip(),
|
|
104
|
+
"evidence": claim["evidence"], "exhaustive": claim["exhaustive"]}
|
|
105
|
+
if "table" in claim:
|
|
106
|
+
table, width = claim["table"], 4 if far else 2
|
|
107
|
+
if not isinstance(table, dict) or not isinstance(table.get("evidence"), str) or not table["evidence"].strip():
|
|
108
|
+
raise ValueError("An indirect call table needs layout/count evidence")
|
|
109
|
+
if type(table.get("width", width)) is not int or table.get("width", width) != width:
|
|
110
|
+
raise ValueError(f"An indirect {'far' if far else 'near'} call table holds {width}-byte targets, "
|
|
111
|
+
f"so its width must be {width}")
|
|
112
|
+
rows = []
|
|
113
|
+
operands = _table_operands(image, table, width, "indirect call table", "indirect call target field offset")
|
|
114
|
+
for index, operand in enumerate(operands):
|
|
115
|
+
row = {"index": index, "operandSite": operand, "rawOffset": _word(image, operand)}
|
|
116
|
+
if far:
|
|
117
|
+
# The segment word is relocated at load time, and the pointer is admitted as a traced far
|
|
118
|
+
# call through memory admits the pointer it reads (Image.far_pointer_target).
|
|
119
|
+
fixup = image.fixups.get(operand + 2)
|
|
120
|
+
if fixup is None:
|
|
121
|
+
raise ValueError(f"Indirect call table row {index} has no declared relocation for its segment "
|
|
122
|
+
f"word at {operand + 2}")
|
|
123
|
+
target, admission = image.far_pointer_target(fixup["segment"], row["rawOffset"])
|
|
124
|
+
if target is None:
|
|
125
|
+
raise ValueError(f"Indirect call table row {index} target is not admitted: {admission['reason']}")
|
|
126
|
+
row |= {"rawSegment": _word(image, operand + 2), "resolvedSegment": fixup["segment"]}
|
|
127
|
+
# A pointer at an FBOV trampoline continues at its overlay entry, which the row names.
|
|
128
|
+
if "trampoline" in admission:
|
|
129
|
+
row["trampoline"] = admission["trampoline"]
|
|
130
|
+
else:
|
|
131
|
+
target = image.near_target(site, row["rawOffset"])
|
|
132
|
+
if target is None or image.region(target) is None:
|
|
133
|
+
raise ValueError(f"Indirect call table row {index} target leaves declared code mappings")
|
|
134
|
+
rows.append(row | {"target": target})
|
|
135
|
+
declaration |= {"table": table, "rows": rows}
|
|
136
|
+
targets = [row["target"] for row in rows]
|
|
137
|
+
else:
|
|
138
|
+
targets = claim["targets"]
|
|
139
|
+
if (not isinstance(targets, list) or not 1 <= len(targets) <= 256
|
|
140
|
+
or len(set(map(repr, targets))) != len(targets)):
|
|
141
|
+
raise ValueError("indirect call targets must be a list of 1..256 distinct file offsets")
|
|
142
|
+
for at in targets:
|
|
143
|
+
integer(at, 0, len(image.data) - 1, "indirect call target")
|
|
144
|
+
region = image.region(at)
|
|
145
|
+
if region is None:
|
|
146
|
+
raise ValueError(f"indirect call target {at} is outside declared code")
|
|
147
|
+
if not far:
|
|
148
|
+
# A near call keeps CS, so its target needs an IP that the call site's mapping places there.
|
|
149
|
+
linear = region["segment"] * 16 + region["ip"] + at - region["start"]
|
|
150
|
+
ip = linear - image.region(site)["segment"] * 16
|
|
151
|
+
if not 0 <= ip <= image.mask or image.near_target(site, ip) != at:
|
|
152
|
+
raise ValueError(f"indirect call target {at} is not placed by any IP in the near call "
|
|
153
|
+
f"site {site}'s segment")
|
|
154
|
+
result[site] = declaration | {"targets": list(dict.fromkeys(targets))}
|
|
155
|
+
return result
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def declared_call_ends(declaration, no_return_calls):
|
|
159
|
+
"""Whether a declared indirect call ends its branch: it is exhaustive and every target is in ``no_return_calls``."""
|
|
160
|
+
return declaration["exhaustive"] and all(target in no_return_calls for target in declaration["targets"])
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def call_ends(site, target, indirect_calls, no_return_calls):
|
|
164
|
+
"""Whether the call at ``site`` to ``target`` ends its branch, under ``declared_call_ends`` for a declared site."""
|
|
165
|
+
declaration = indirect_calls.get(site)
|
|
166
|
+
if declaration is not None:
|
|
167
|
+
return declared_call_ends(declaration, no_return_calls)
|
|
168
|
+
return target in no_return_calls
|