scientific-method-engine 17.0.1__tar.gz → 18.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/PKG-INFO +3 -3
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/pyproject.toml +2 -2
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/cli.py +5 -3
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/x86/image.py +24 -3
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/x86/pcode.py +9 -3
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/x86/pcode_backend.py +14 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/x86/reach.py +64 -20
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/x86/relational.py +3 -3
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/x86/trace.py +3 -1
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/x86/values.py +6 -3
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_oracle.py +41 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_reach.py +74 -2
- scientific_method_engine-18.1.0/tests/test_upstream_gaps.py +36 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_x86.py +43 -1
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/.gitignore +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/LICENSE +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/README.md +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/__init__.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/__main__.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ClearNoReturnFunctions.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/CreateFunctions.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ExportBoundedFlow.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ExportCallEdges.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ExportFunctionFingerprints.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ExportFunctionInventory.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/MergeFallThroughFragment.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/RecoverCitedFunctions.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/RepairReturningCallers.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportCallArguments.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportCallPaths.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportCallSitesWithScalars.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportCallsToRange.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportConstantFirstArgumentCalls.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportDataBytes.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportDecompileMatches.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportDecompileWindow.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportFilePatternInMemory.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportFirstArgumentCallSummary.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportFunctionScalarConstants.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportFunctionSummary.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportInstructionContext.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportInstructionWindow.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportMemoryBlockForFileOffset.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportMemoryBlocks.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportRandomnessCandidates.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportRangeBoundaries.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportReferences.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportScalarConstants.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportStringReferences.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/ReportSymbolReferences.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/scientificmethod/FirstArgumentLookBack.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/scientificmethod/InstructionStart.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/scientificmethod/OperandConstants.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/ghidra/scientificmethod/Xxh3.java +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/x86/__init__.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/x86/argument_frames.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/x86/dispatch.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/x86/effect_order.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/x86/inventory.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/x86/loops.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/x86/machine.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/x86/memory_scopes.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/x86/pe.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/x86/reports.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/src/scientific_method_engine/x86/result_flow.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/oracle.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_config_encoding.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_continuation_budget.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_dispatch.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_effect_order.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_enter_frames.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_entry_frame.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_indirect_far_transfers.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_interrupt_models.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_inventory_check.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_join_terms.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_loops.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_memory_scopes.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_nested_frame_request.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_pe.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_relational_controls.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_return_checks.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_successor_rule.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_table_continuations.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_term_limit.py +0 -0
- {scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_volatile_memory.py +0 -0
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: scientific-method-engine
|
|
3
|
-
Version:
|
|
3
|
+
Version: 18.1.0
|
|
4
4
|
Summary: Bounded instruction-derived x86 evidence reports for segmented MZ/FBOV and PE32/i386 code.
|
|
5
5
|
Project-URL: Source, https://github.com/kibertoad/refurbished-dinosaurs-toolkit/tree/main/packages/scientific-method-engine
|
|
6
6
|
Author: kibertoad
|
|
7
7
|
License-Expression: MIT
|
|
8
8
|
License-File: LICENSE
|
|
9
9
|
Requires-Python: >=3.12
|
|
10
|
-
Requires-Dist: capstone==5.0.
|
|
11
|
-
Requires-Dist: pypcode==4.0.
|
|
10
|
+
Requires-Dist: capstone==5.0.9
|
|
11
|
+
Requires-Dist: pypcode==4.0.1
|
|
12
12
|
Requires-Dist: xxhash==4.0.1
|
|
13
13
|
Provides-Extra: test
|
|
14
14
|
Requires-Dist: unicorn==2.1.4; extra == 'test'
|
|
@@ -5,13 +5,13 @@ 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
|
+
version = "18.1.0"
|
|
9
9
|
description = "Bounded instruction-derived x86 evidence reports for segmented MZ/FBOV and PE32/i386 code."
|
|
10
10
|
readme = "README.md"
|
|
11
11
|
requires-python = ">=3.12"
|
|
12
12
|
license = "MIT"
|
|
13
13
|
authors = [{ name = "kibertoad" }]
|
|
14
|
-
dependencies = ["capstone==5.0.
|
|
14
|
+
dependencies = ["capstone==5.0.9", "pypcode==4.0.1", "xxhash==4.0.1"]
|
|
15
15
|
|
|
16
16
|
[project.optional-dependencies]
|
|
17
17
|
# Unicorn is the tests' concrete oracle (ADR 0003, decision 4). Its core is GPLv2, so it never
|
|
@@ -3,15 +3,15 @@ import json
|
|
|
3
3
|
import sys
|
|
4
4
|
from pathlib import Path
|
|
5
5
|
|
|
6
|
-
import capstone
|
|
7
6
|
import pypcode
|
|
8
7
|
|
|
9
8
|
from . import PREPARED_PROTOCOL
|
|
9
|
+
from .x86.image import CAPSTONE_VERSION
|
|
10
10
|
|
|
11
11
|
CONFIG_LIMIT = 1024 * 1024
|
|
12
12
|
PREPARED_CONFIG_LIMIT = 16 * 1024 * 1024
|
|
13
13
|
# The header names the decoder and instruction semantics that actually ran, not the pins in pyproject.toml.
|
|
14
|
-
DECODER = "capstone
|
|
14
|
+
DECODER = f"capstone {CAPSTONE_VERSION or 'without distribution metadata'}"
|
|
15
15
|
INSTRUCTION_SEMANTICS = f"pypcode {pypcode.__version__} (Ghidra SLEIGH x86)"
|
|
16
16
|
USAGE = ("Usage: scientific-method-engine <operand|operand-candidates|target|bounds|owner|callees|reach|trace|uses|arguments|"
|
|
17
17
|
"effects|returns|memory|incoming|inventory-check|call-order|guards|allocation|dispatch> <config.json|->\n"
|
|
@@ -23,7 +23,9 @@ USAGE = ("Usage: scientific-method-engine <operand|operand-candidates|target|bou
|
|
|
23
23
|
"or continues at another address than the next instruction (ghidraFallsThroughElsewhere).\n"
|
|
24
24
|
"reach lists the target sites the starts reach over resolved calls and jumps, with the fewest-call chain\n"
|
|
25
25
|
"and the routines every read route passes, and lists every reached transfer it could not resolve;\n"
|
|
26
|
-
"a leaves routine is reached but not read, and its reason is repeated in the report
|
|
26
|
+
"a leaves routine is reached but not read, and its reason is repeated in the report;\n"
|
|
27
|
+
"controls are call sites the walk must reach and resolve, instructionControls sites it must decode,\n"
|
|
28
|
+
"and a failed control fails the report saying whether it was reached.\n"
|
|
27
29
|
"inventory-check places every resolved direct call target in the notation of the function inventory TSV\n"
|
|
28
30
|
"that inventory names and lists each target no row starts at: inside another row's body or outside every row,\n"
|
|
29
31
|
"with one calling site, near or far, and whether an entry-path call, a contested one or only raw bytes call it;\n"
|
|
@@ -1,12 +1,30 @@
|
|
|
1
1
|
"""Bounded explicit code mappings. No guessed linear disassembly domains."""
|
|
2
2
|
import re
|
|
3
|
+
from importlib import metadata
|
|
3
4
|
from pathlib import Path
|
|
5
|
+
import pypcode
|
|
4
6
|
import xxhash
|
|
5
7
|
from capstone import Cs, CS_ARCH_X86, CS_MODE_16, CS_MODE_32
|
|
6
8
|
from capstone.x86 import X86_OP_MEM
|
|
7
9
|
from .pe import prepare_pe
|
|
8
|
-
import capstone
|
|
9
10
|
|
|
11
|
+
|
|
12
|
+
def installed(name):
|
|
13
|
+
"""The installed distribution's version, or None when it has no distribution metadata."""
|
|
14
|
+
try:
|
|
15
|
+
return metadata.version(name)
|
|
16
|
+
except metadata.PackageNotFoundError:
|
|
17
|
+
return None
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# The installed Capstone distribution's version. capstone.__version__ cannot stand in for it: the
|
|
21
|
+
# 5.0.8 and 5.0.9 bindings still report 5.0.7, because their CS_VERSION_EXTRA was not bumped.
|
|
22
|
+
CAPSTONE_VERSION = installed("capstone")
|
|
23
|
+
REQUIRED_CAPSTONE = "5.0.9"
|
|
24
|
+
# The engine's rules for which flags to keep are checked against this SLEIGH specification, such as
|
|
25
|
+
# the OF of SHR, which pypcode 4.0.0 writes as 0 where 4.0.1 writes the CPU's value.
|
|
26
|
+
PYPCODE_VERSION = pypcode.__version__
|
|
27
|
+
REQUIRED_PYPCODE = "4.0.1"
|
|
10
28
|
MAX_SOURCE = 256 * 1024 * 1024
|
|
11
29
|
XXH3_FORM = re.compile("[0-9a-f]{32}")
|
|
12
30
|
|
|
@@ -65,8 +83,11 @@ def read_source(config, base):
|
|
|
65
83
|
|
|
66
84
|
class Image:
|
|
67
85
|
def __init__(self, data, config):
|
|
68
|
-
|
|
69
|
-
|
|
86
|
+
for name, required, found in (("capstone", REQUIRED_CAPSTONE, CAPSTONE_VERSION),
|
|
87
|
+
("pypcode", REQUIRED_PYPCODE, PYPCODE_VERSION)):
|
|
88
|
+
if found != required:
|
|
89
|
+
present = f"{name} {found} is installed" if found else f"the installed {name} has no distribution metadata"
|
|
90
|
+
raise ValueError(f"This reporter requires {name}=={required}; {present}")
|
|
70
91
|
if config.get("sourceKind") == "pe32":
|
|
71
92
|
config = prepare_pe(data, config)
|
|
72
93
|
self.config = config
|
|
@@ -315,9 +315,6 @@ def evaluate(code, args, bits, site):
|
|
|
315
315
|
and x.term[1][2] + x.term[2][1] == bits):
|
|
316
316
|
high, low = Value(x.term[1][2], x.term[1][1]), Value(y.term[2], y.term[1])
|
|
317
317
|
return Value(bits, join([low, high]).term, origin)
|
|
318
|
-
if name in ("shl", "shr", "sar") and b.term[0] == "and" and b.term[2] == ("constant", 31):
|
|
319
|
-
# x86 masks shift counts to five bits; the engine's shift terms carry that mask already.
|
|
320
|
-
b = Value(b.bits, b.term[1], b.sources)
|
|
321
318
|
if name in ("and", "or", "xor") and not known:
|
|
322
319
|
# Identities that keep flag expressions small; constants fold in values.op.
|
|
323
320
|
mask = (1 << bits) - 1
|
|
@@ -331,6 +328,15 @@ def evaluate(code, args, bits, site):
|
|
|
331
328
|
return Value(bits, y.term, origin)
|
|
332
329
|
if name == "or" and (x.number == mask):
|
|
333
330
|
return Value(bits, ("constant", mask), origin)
|
|
331
|
+
if name == "mul" and not known and bits == 8:
|
|
332
|
+
# SLEIGH's conditionalAssign selects a flag as (c * new) | (!c * old); with c known,
|
|
333
|
+
# one arm is a product by zero and the other a product by one. Those products are one
|
|
334
|
+
# byte wide. A wider product is a MUL or IMUL, which keeps its multiply in reports.
|
|
335
|
+
for x, y in ((a, b), (b, a)):
|
|
336
|
+
if x.number == 0:
|
|
337
|
+
return Value(bits, ("constant", 0), origin)
|
|
338
|
+
if x.number == 1:
|
|
339
|
+
return Value(bits, y.term, origin)
|
|
334
340
|
return op(name, a, b, site)
|
|
335
341
|
if code == "INT_NEGATE":
|
|
336
342
|
return op("xor", a, const((1 << bits) - 1, bits), site)
|
|
@@ -746,9 +746,21 @@ def shift(state, ins, image):
|
|
|
746
746
|
state.carry = carry if carry is not None else unknown(f"carry:{state.at}:{state.flag_serial}", 1, state.at)
|
|
747
747
|
flags_written(state, f.run)
|
|
748
748
|
state.flag_values["CF"] = resize(state.carry, 8)
|
|
749
|
+
# SLEIGH's SHR with a count operand (C0, C1, D2, D3) swaps the arms of its OF selection,
|
|
750
|
+
# writing 0 for a count of 1; only the D0 and D1 forms write OF as the CPU does.
|
|
751
|
+
if n != 1 or (m == "shr" and ins.opcode[0] not in (0xD0, 0xD1)):
|
|
752
|
+
undefined_overflow(state)
|
|
749
753
|
state.event("arithmetic", operation=m, left=a.report(), right=b.report(), result=result.report(), modulus=1 << a.bits)
|
|
750
754
|
|
|
751
755
|
|
|
756
|
+
def undefined_overflow(state):
|
|
757
|
+
"""Drop OF after a shift or rotate whose OF the CPU leaves undefined or SLEIGH writes wrongly.
|
|
758
|
+
|
|
759
|
+
The CPU defines OF only for a count of 1. A branch that reads OF afterwards stays undecided.
|
|
760
|
+
"""
|
|
761
|
+
state.flag_values.pop("OF", None)
|
|
762
|
+
|
|
763
|
+
|
|
752
764
|
def rotate(state, ins, image):
|
|
753
765
|
m = ins.mnemonic
|
|
754
766
|
operands = ins.operands
|
|
@@ -790,6 +802,8 @@ def rotate(state, ins, image):
|
|
|
790
802
|
state.carry = Value(1, carry.term, sources(a, count, site=state.at))
|
|
791
803
|
flags_written(state, f.run)
|
|
792
804
|
state.flag_values["CF"] = resize(state.carry, 8)
|
|
805
|
+
if count.number & 31 != 1:
|
|
806
|
+
undefined_overflow(state)
|
|
793
807
|
state.event("arithmetic", operation=m, left=a.report(), count=n, result=value.report(),
|
|
794
808
|
carryOut=state.carry.report(), modulus=1 << bits)
|
|
795
809
|
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"""Transitive reachability from established starts to target sites over the entry-path CFG."""
|
|
2
2
|
from collections import deque
|
|
3
3
|
from .image import integer
|
|
4
|
-
from .trace import walk, cfg_step, base_mnemonic, unsupported_transfer, holding_instruction, INTERRUPTS, RETURNS,
|
|
4
|
+
from .trace import (walk, cfg_step, base_mnemonic, unsupported_transfer, holding_instruction, INTERRUPTS, RETURNS,
|
|
5
|
+
LIMIT_REASON, OVERLAP_REASON, UNDECODED_REASON)
|
|
5
6
|
from .pcode_backend import interrupt_vector
|
|
6
7
|
|
|
7
8
|
# The virtual root of the dominator computation; no file offset is negative.
|
|
@@ -11,9 +12,13 @@ UNSUPPORTED = "unsupported control-transfer frame encoding"
|
|
|
11
12
|
LEAF_OVERLAP = "leaf start inside a reached instruction; the leaf is not decoded, so its boundary is unchecked"
|
|
12
13
|
|
|
13
14
|
|
|
14
|
-
def
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
def _text(ins):
|
|
16
|
+
return (ins.mnemonic + " " + ins.op_str).strip()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _sites(value, label, image, limit=256, least=1):
|
|
20
|
+
if not isinstance(value, list) or not least <= len(value) <= limit or len(set(map(repr, value))) != len(value):
|
|
21
|
+
raise ValueError(f"{label} must be a list of {least}..{limit} distinct file offsets")
|
|
17
22
|
for at in value:
|
|
18
23
|
integer(at, 0, len(image.data) - 1, label)
|
|
19
24
|
if image.region(at) is None:
|
|
@@ -106,10 +111,20 @@ def reach(image, config):
|
|
|
106
111
|
raise ValueError(f"Reach start {at} must be an established region entry")
|
|
107
112
|
targets = _sites(config.get("targets"), "targets", image)
|
|
108
113
|
leaves = _leaves(config.get("leaves", []), image, set(starts))
|
|
109
|
-
controls = config.get("controls", [])
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
114
|
+
controls = _sites(config.get("controls", []), "controls", image, least=0)
|
|
115
|
+
instruction_controls = _sites(config.get("instructionControls", []), "instructionControls", image, least=0)
|
|
116
|
+
# The walk decodes a site as image.decode does, so a control whose bytes are no call can never pass.
|
|
117
|
+
for at in controls:
|
|
118
|
+
ins = image.decode(at)
|
|
119
|
+
if ins is None or base_mnemonic(ins) not in CALLS:
|
|
120
|
+
decoded = f"decodes as {_text(ins)}" if ins is not None else "does not decode"
|
|
121
|
+
raise ValueError(f"control {at} is not a call site ({decoded}); controls are call sites the walk must "
|
|
122
|
+
"reach and resolve, and instructionControls are sites the walk must decode")
|
|
123
|
+
# The walk decodes every start it is given, so a start as an instruction control would pass whatever the walk misses.
|
|
124
|
+
for at in instruction_controls:
|
|
125
|
+
if at in starts:
|
|
126
|
+
raise ValueError(f"instruction control {at} is a start; the walk decodes every start, so it shows nothing "
|
|
127
|
+
"the walk found")
|
|
113
128
|
limit = integer(config.get("limit", 1000), 1, 10000, "result limit")
|
|
114
129
|
instruction_limit = config.get("instructionLimit", 10000)
|
|
115
130
|
seen, walk_gaps, _, _, contested = walk(image, starts, instruction_limit, follow_interrupts=True, stops=frozenset(leaves))
|
|
@@ -119,7 +134,7 @@ def reach(image, config):
|
|
|
119
134
|
step = cfg_step(image, at, ins, follow_interrupts=True)
|
|
120
135
|
graph[at] = [(s, kind) for s, kind in _kinds(at, ins, step) if s in seen or s in leaves]
|
|
121
136
|
m = base_mnemonic(ins)
|
|
122
|
-
text = (ins
|
|
137
|
+
text = _text(ins)
|
|
123
138
|
kind = "call" if m in CALLS else "return" if m in RETURNS else "jump"
|
|
124
139
|
if unsupported_transfer(image, ins):
|
|
125
140
|
unresolved.append({"site": at, "instruction": text, "kind": kind, "reason": UNSUPPORTED})
|
|
@@ -191,6 +206,19 @@ def reach(image, config):
|
|
|
191
206
|
"route": {"assumedReturns": assumed_returns[::-1], "declaredTableJumps": tables[::-1],
|
|
192
207
|
"interruptsContinued": continued[::-1]}}
|
|
193
208
|
|
|
209
|
+
# The walk decodes both instructions of an unresolved overlap and keeps neither, so their starts are not in seen.
|
|
210
|
+
overlapping = {g["site"] for g in walk_gaps if g["reason"] == OVERLAP_REASON}
|
|
211
|
+
# A site an edge leads to whose bytes do not decode is reached, though no instruction starts there.
|
|
212
|
+
undecodable = {g["site"] for g in walk_gaps if g["reason"] == UNDECODED_REASON}
|
|
213
|
+
|
|
214
|
+
def unreached(site):
|
|
215
|
+
holder = holding_instruction(seen, site)
|
|
216
|
+
if holder is not None:
|
|
217
|
+
return {"status": "inside a reached instruction", "insideInstruction": holder}
|
|
218
|
+
return {"status": "start of a contested instruction" if site in contested
|
|
219
|
+
else "start of an unresolved overlapping instruction" if site in overlapping
|
|
220
|
+
else "reached but not decodable" if site in undecodable else "not reached"}
|
|
221
|
+
|
|
194
222
|
rows = []
|
|
195
223
|
for target in targets:
|
|
196
224
|
row = {"target": target}
|
|
@@ -203,10 +231,7 @@ def reach(image, config):
|
|
|
203
231
|
row |= {"reached": True, "routine": routine[target], "leaf": target in leaves, **chain(target),
|
|
204
232
|
"throughEveryRoute": cut[::-1]}
|
|
205
233
|
else:
|
|
206
|
-
|
|
207
|
-
row |= {"reached": False, "status": "inside a reached instruction" if holder is not None
|
|
208
|
-
else "start of a contested instruction" if target in contested else "not reached",
|
|
209
|
-
**({"insideInstruction": holder} if holder is not None else {})}
|
|
234
|
+
row |= {"reached": False, **unreached(target)}
|
|
210
235
|
rows.append(row)
|
|
211
236
|
|
|
212
237
|
for row in unresolved + interrupts:
|
|
@@ -216,16 +241,33 @@ def reach(image, config):
|
|
|
216
241
|
# The walk never decodes a leaf, so its overlap check cannot see a leaf start inside a decoded instruction.
|
|
217
242
|
gaps += [{"site": at, "reason": LEAF_OVERLAP, "insideInstruction": holding_instruction(seen, at)}
|
|
218
243
|
for at in sorted(leaves) if at in distance and holding_instruction(seen, at) is not None]
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
244
|
+
# Kept apart from the gap rows, which the result limit can cut.
|
|
245
|
+
stopped = any(g["reason"] == LIMIT_REASON for g in walk_gaps)
|
|
246
|
+
# Not reached and reached-but-unresolved need different fixes, so each failure says which it is.
|
|
247
|
+
failures = []
|
|
248
|
+
for label, sites, passed in (("control", controls, resolved_calls), ("instruction control", instruction_controls, seen)):
|
|
249
|
+
for at in sites:
|
|
250
|
+
if at in passed:
|
|
251
|
+
continue
|
|
252
|
+
# Only a call-site control can be decoded and still fail.
|
|
253
|
+
if at in seen:
|
|
254
|
+
reasons = sorted({row["reason"] for row in unresolved if row["site"] == at})
|
|
255
|
+
failures.append(f"{label} {at} is reached but its call target is unresolved ({'; '.join(reasons)})")
|
|
256
|
+
elif at in leaves and at in distance:
|
|
257
|
+
failures.append(f"{label} {at} is the start of a leaf, which the walk reaches but does not decode")
|
|
258
|
+
else:
|
|
259
|
+
state = unreached(at)
|
|
260
|
+
detail = (f"inside the reached instruction at {state['insideInstruction']}" if "insideInstruction" in state
|
|
261
|
+
else "the " + state["status"] if state["status"].startswith("start") else state["status"])
|
|
262
|
+
failures.append(f"{label} {at} is {detail}"
|
|
263
|
+
+ (" (the walk stopped at its instruction limit)" if stopped else ""))
|
|
264
|
+
if failures:
|
|
265
|
+
raise ValueError("Positive controls failed: " + "; ".join(failures))
|
|
222
266
|
call_sites = {}
|
|
223
267
|
for site, target in sorted(resolved_calls.items()):
|
|
224
268
|
call_sites.setdefault(target, []).append(site)
|
|
225
269
|
leaf_rows = [{"routine": at, "reason": reason, "reached": at in distance, "callSites": call_sites.get(at, [])}
|
|
226
270
|
for at, reason in leaves.items()]
|
|
227
|
-
# Kept apart from the gap rows, which the result limit can cut.
|
|
228
|
-
stopped = any(g["reason"] == LIMIT_REASON for g in walk_gaps)
|
|
229
271
|
counts = {"routines": len(routine_starts), "instructions": len(seen), "unresolved": len(unresolved),
|
|
230
272
|
"interrupts": len(interrupts), "gaps": len(gaps), "contested": len(contested)}
|
|
231
273
|
return {"starts": starts, "targets": rows, "leaves": leaf_rows,
|
|
@@ -235,7 +277,9 @@ def reach(image, config):
|
|
|
235
277
|
"truncated": any(len(x) > limit for x in (unresolved, interrupts, gaps, contested)),
|
|
236
278
|
"instructionLimitReached": stopped,
|
|
237
279
|
"controls": [{"site": at, "target": resolved_calls[at]} for at in controls],
|
|
238
|
-
"
|
|
280
|
+
"instructionControls": [{"site": at, "instruction": _text(seen[at]), "routine": routine.get(at)}
|
|
281
|
+
for at in instruction_controls],
|
|
282
|
+
"negativeUsable": bool(controls or instruction_controls) and not (stopped or unresolved or gaps or contested),
|
|
239
283
|
"assumptions": ["each reached call returns to its next instruction",
|
|
240
284
|
"each reached interrupt returns to its next instruction; interrupt handlers are not read",
|
|
241
285
|
"each leaf calls nothing, for the reason it gives",
|
|
@@ -245,7 +289,7 @@ def reach(image, config):
|
|
|
245
289
|
"interpretation": "Routes over the decoded entry-path CFG from the starts. A chain has the fewest calls; "
|
|
246
290
|
"throughEveryRoute lists the routine starts every read route to the target passes, "
|
|
247
291
|
"and an unresolved transfer may add a route that passes none of them. "
|
|
248
|
-
"negativeUsable needs
|
|
292
|
+
"negativeUsable needs a control of either kind, a walk that did not stop at its instruction limit, "
|
|
249
293
|
"and no unresolved transfer, gap or contested instruction, "
|
|
250
294
|
"and still rests on the listed assumptions and leaves. "
|
|
251
295
|
"When instructionLimitReached holds, the walk stopped before reading all it reaches: "
|
|
@@ -331,9 +331,9 @@ def _unsigned(term, bits, ranges):
|
|
|
331
331
|
return 0, top
|
|
332
332
|
if tag in ("shr", "sar"):
|
|
333
333
|
# With the sign bit clear, an arithmetic shift right is the logical one.
|
|
334
|
-
#
|
|
335
|
-
s = k
|
|
336
|
-
return lo >> s
|
|
334
|
+
# A p-code shift takes the whole count (values.op): SLEIGH masks an x86 count itself.
|
|
335
|
+
s = min(k, bits)
|
|
336
|
+
return lo >> s, hi >> s
|
|
337
337
|
if k == 0:
|
|
338
338
|
return 0, top
|
|
339
339
|
return (lo // k, hi // k) if tag == "udiv" else (0, min(hi, k - 1))
|
|
@@ -88,6 +88,8 @@ def far_pointer_transfer(state, image):
|
|
|
88
88
|
# The gap reason walk() records when it stops at its instruction limit with code left to read.
|
|
89
89
|
LIMIT_REASON = "instruction limit"
|
|
90
90
|
OVERLAP_REASON = "overlapping entry-path instructions; boundary unresolved"
|
|
91
|
+
# The gap reason walk() records at a site it reaches but cannot decode.
|
|
92
|
+
UNDECODED_REASON = "undecoded or unmapped edge"
|
|
91
93
|
RETURNS = {"ret": "near return", "retf": "far return", "iret": "interrupt return", "iretd": "interrupt return"}
|
|
92
94
|
INTERRUPTS = ("int", "int1", "int3", "into")
|
|
93
95
|
PORTS = ("in", "out", "insb", "insw", "insd", "outsb", "outsw", "outsd")
|
|
@@ -259,7 +261,7 @@ def walk(image, entries, limit=10000, follow_flat_ports=False, follow_interrupts
|
|
|
259
261
|
break
|
|
260
262
|
ins = image.decode(at)
|
|
261
263
|
if ins is None:
|
|
262
|
-
gaps.append({"site": at, "reason":
|
|
264
|
+
gaps.append({"site": at, "reason": UNDECODED_REASON})
|
|
263
265
|
continue
|
|
264
266
|
seen[at] = ins
|
|
265
267
|
step = cfg_step(image, at, ins, follow_flat_ports, follow_interrupts=follow_interrupts,
|
|
@@ -183,9 +183,12 @@ def op(name, a, b, site=None):
|
|
|
183
183
|
elif name == "and": n = x & y
|
|
184
184
|
elif name == "or": n = x | y
|
|
185
185
|
elif name == "xor": n = x ^ y
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
186
|
+
# P-code shifts take the whole count: SLEIGH masks an x86 count itself, and its 33-bit
|
|
187
|
+
# rotate through CF shifts a 64-bit temporary by 32. A count of the width or more leaves 0,
|
|
188
|
+
# or the sign for an arithmetic shift.
|
|
189
|
+
elif name == "shl": n = x << min(y, bits)
|
|
190
|
+
elif name == "shr": n = x >> min(y, bits)
|
|
191
|
+
elif name == "sar": n = (x - (1 << bits) if x & (1 << (bits - 1)) else x) >> min(y, bits)
|
|
189
192
|
elif name == "mul": n = x * y
|
|
190
193
|
else: raise ValueError("Unsupported expression operation: " + name)
|
|
191
194
|
return Value(bits, const(n, bits).term, origin)
|
|
@@ -187,6 +187,39 @@ class ShiftsAndRotates(unittest.TestCase):
|
|
|
187
187
|
result = check(self, "b90100 31d2 d1e0 d1d2 d1e0 d1d2 bb0000 11db c3", resolved=("cx", "bx"))
|
|
188
188
|
self.assertEqual(result["paths"][0]["registers"]["dx"]["expression"][0], "or")
|
|
189
189
|
|
|
190
|
+
def test_overflow_of_one_bit_shifts_and_rotates(self):
|
|
191
|
+
# The engine keeps OF after every shift and rotate by a count of 1 except SHR by a count
|
|
192
|
+
# operand, so each such encoding gets its own routine: JNO skips INC BX when OF is clear.
|
|
193
|
+
# The operands set and clear the top two bits and bit 0, and RCL and RCR run from both carries.
|
|
194
|
+
# 8-bit forms work on AL, 16-bit forms on AX and 32-bit forms (66h) on EAX; CL holds 1.
|
|
195
|
+
forms = {"d0": ("", 8), "d1": ("", 16), "66d1": ("", 32), "c0": ("01", 8), "c1": ("01", 16),
|
|
196
|
+
"66c1": ("01", 32), "d2": ("", 8), "d3": ("", 16), "66d3": ("", 32)}
|
|
197
|
+
for field, mnemonic in enumerate(("rol", "ror", "rcl", "rcr", "shl", "shr", "sal", "sar")):
|
|
198
|
+
for opcode, (immediate, bits) in forms.items():
|
|
199
|
+
if mnemonic == "shr" and opcode[-2:] not in ("d0", "d1"):
|
|
200
|
+
continue
|
|
201
|
+
for top in (0b00, 0b01, 0b10, 0b11):
|
|
202
|
+
load = "66b8" if bits == 32 else "b8"
|
|
203
|
+
value = (top << (bits - 2) | (top & 1)).to_bytes(4 if bits == 32 else 2, "little").hex()
|
|
204
|
+
for carry in (("f8", "f9") if mnemonic in ("rcl", "rcr") else ("",)):
|
|
205
|
+
shift = f"{opcode}{0xC0 | field << 3:02x}{immediate}"
|
|
206
|
+
with self.subTest(mnemonic=mnemonic, shift=shift, value=value, carry=carry):
|
|
207
|
+
check(self, f"bb0000 b90100 {load}{value} {carry} {shift} 7101 43 c3", resolved=("bx",))
|
|
208
|
+
|
|
209
|
+
def test_shift_by_an_unknown_count_keeps_the_count_mask(self):
|
|
210
|
+
# A p-code shift takes its whole count, so the term keeps SLEIGH's five-bit mask of CL.
|
|
211
|
+
result = check(self, "bb0300 d3e0 c3", resolved=("bx",))
|
|
212
|
+
ax = result["paths"][0]["registers"]["ax"]["expression"]
|
|
213
|
+
self.assertEqual((ax[0], ax[2][0], ax[2][1][0], ax[2][1][2]), ("shl", "zeroExtend", "and", ("constant", 31)))
|
|
214
|
+
|
|
215
|
+
def test_double_word_rotates_through_carry(self):
|
|
216
|
+
# SLEIGH rotates EAX through CF in a 64-bit temporary built with zext(CF) << 32, a p-code
|
|
217
|
+
# shift by 32 that x86's five-bit count mask does not apply to.
|
|
218
|
+
for rotate in ("66d1d0", "66d1d8", "66c1d005", "66c1d81f", "66d3d0", "66d3d8"):
|
|
219
|
+
for carry in ("f8", "f9"):
|
|
220
|
+
with self.subTest(rotate=rotate, carry=carry):
|
|
221
|
+
check(self, f"b90300 66b8f00f0180 {carry} {rotate} bb0000 83d300 c3", resolved=("eax", "bx"))
|
|
222
|
+
|
|
190
223
|
def test_memory_operands(self):
|
|
191
224
|
# RCL by one on memory: Capstone reports its implicit count with a size of 0.
|
|
192
225
|
check(self, "bb1000 c7070180 d107 c12f04 f9 d117 8b07 c7070180 f8 d017 8b07 c3", registers=DATA, resolved=("ax",))
|
|
@@ -203,6 +236,14 @@ class MultiplyAndDivide(unittest.TestCase):
|
|
|
203
236
|
self.assertEqual(registers["cx"]["expression"][0], "mul")
|
|
204
237
|
self.assertEqual(registers["dx"]["expression"][0], "mul")
|
|
205
238
|
|
|
239
|
+
def test_product_by_zero_or_one_keeps_the_multiply(self):
|
|
240
|
+
# BX is unknown; the engine folds only the one-byte products of SLEIGH's flag selections.
|
|
241
|
+
result = check(self, "b90300 6bc301 66 6bd301 6bf300 c3", resolved=("cx",))
|
|
242
|
+
registers = result["paths"][0]["registers"]
|
|
243
|
+
self.assertEqual(registers["ax"]["expression"][0], "mul")
|
|
244
|
+
self.assertEqual(registers["dx"]["expression"][1][0], "mul")
|
|
245
|
+
self.assertEqual(registers["si"]["expression"][0], "mul")
|
|
246
|
+
|
|
206
247
|
def test_low_products_of_extended_operands(self):
|
|
207
248
|
# AX holds the sign extension of an unknown byte; the products name AX, not the byte.
|
|
208
249
|
result = check(self, "a00000 98 0fafc8 6bd0fd b80100 c3", registers=DATA, resolved=("ax",))
|
|
@@ -172,7 +172,75 @@ class ReachTests(unittest.TestCase):
|
|
|
172
172
|
self.assertEqual(r["counts"]["unresolved"], 2)
|
|
173
173
|
self.assertTrue(r["truncated"])
|
|
174
174
|
|
|
175
|
+
def test_a_control_that_is_no_call_is_refused_before_the_walk_and_an_instruction_control_takes_it(self):
|
|
176
|
+
# 0000 calls 0004, which stores AX at [0100] and returns.
|
|
177
|
+
data = bytes.fromhex("e80100c3" "a30001" "c3")
|
|
178
|
+
with self.assertRaisesRegex(ValueError, r"control 4 is not a call site \(decodes as mov .*\); controls are "
|
|
179
|
+
r"call sites the walk must reach and resolve"):
|
|
180
|
+
run_report(data, config(data, targets=[7], controls=[4]), "reach")
|
|
181
|
+
r = run_report(data, config(data, targets=[7], instructionControls=[4]), "reach")
|
|
182
|
+
self.assertEqual(r["instructionControls"], [{"site": 4, "instruction": "mov word ptr [0x100], ax", "routine": 4}])
|
|
183
|
+
self.assertEqual(r["controls"], [])
|
|
184
|
+
self.assertTrue(r["negativeUsable"])
|
|
185
|
+
# The call that enters the store's routine passes as a call-site control.
|
|
186
|
+
r = run_report(data, config(data, targets=[7], controls=[0]), "reach")
|
|
187
|
+
self.assertEqual(r["controls"], [{"site": 0, "target": 4}])
|
|
188
|
+
self.assertEqual(r["instructionControls"], [])
|
|
189
|
+
|
|
190
|
+
def test_a_control_that_does_not_decode_is_refused_before_the_walk(self):
|
|
191
|
+
data = bytes.fromhex("e80100c3" "0f")
|
|
192
|
+
with self.assertRaisesRegex(ValueError, r"control 4 is not a call site \(does not decode\)"):
|
|
193
|
+
run_report(data, config(data, targets=[3], controls=[4]), "reach")
|
|
194
|
+
|
|
195
|
+
def test_a_site_the_walk_reaches_but_cannot_decode_is_not_called_unreached(self):
|
|
196
|
+
# 0000 calls 0004, whose single byte does not decode.
|
|
197
|
+
data = bytes.fromhex("e80100c3" "0f")
|
|
198
|
+
r = run_report(data, config(data, targets=[4]), "reach")
|
|
199
|
+
self.assertEqual(r["targets"][0]["status"], "reached but not decodable")
|
|
200
|
+
with self.assertRaisesRegex(ValueError, r"Positive controls failed: instruction control 4 is reached but not "
|
|
201
|
+
r"decodable$"):
|
|
202
|
+
run_report(data, config(data, targets=[3], instructionControls=[4]), "reach")
|
|
203
|
+
|
|
204
|
+
def test_each_failed_control_says_whether_it_was_reached(self):
|
|
205
|
+
LEAF = {"routine": 0x18, "reason": "synthetic leaf"}
|
|
206
|
+
cases = [({"controls": [0x6]}, "control 6 is reached but its call target is unresolved "
|
|
207
|
+
r"\(computed transfer remains unresolved\)"),
|
|
208
|
+
# Without the table declaration the walk never arrives at 0020.
|
|
209
|
+
({"controls": [0x20]}, r"control 32 is not reached$"),
|
|
210
|
+
# With 0018 a leaf and no table, nothing reaches 0028.
|
|
211
|
+
({"instructionControls": [0x28], "leaves": [LEAF]}, r"instruction control 40 is not reached$"),
|
|
212
|
+
({"indirectJumps": [TABLE], "instructionControls": [0x29]},
|
|
213
|
+
"instruction control 41 is inside the reached instruction at 40"),
|
|
214
|
+
({"indirectJumps": [TABLE], "instructionControls": [0x18], "leaves": [LEAF]},
|
|
215
|
+
"instruction control 24 is the start of a leaf, which the walk reaches but does not decode"),
|
|
216
|
+
({"indirectJumps": [TABLE], "instructionLimit": 3, "instructionControls": [0x28]},
|
|
217
|
+
r"instruction control 40 is not reached \(the walk stopped at its instruction limit\)")]
|
|
218
|
+
for extra, message in cases:
|
|
219
|
+
with self.subTest(extra=extra), self.assertRaisesRegex(ValueError, "Positive controls failed: " + message):
|
|
220
|
+
reach(**extra)
|
|
221
|
+
# No table declaration means nothing reaches 0020, so the leaf there is not reached either.
|
|
222
|
+
with self.assertRaisesRegex(ValueError, r"Positive controls failed: control 32 is not reached$"):
|
|
223
|
+
reach(controls=[0x20], leaves=[{"routine": 0x20, "reason": "synthetic leaf"}])
|
|
224
|
+
# Every failed control is named at once, in both lists.
|
|
225
|
+
with self.assertRaisesRegex(ValueError, "control 6 is reached but .*; control 32 is not reached; "
|
|
226
|
+
"instruction control 40 is not reached"):
|
|
227
|
+
reach(controls=[0x3, 0x6, 0x20], instructionControls=[0x3, 0x28], leaves=[LEAF])
|
|
228
|
+
|
|
229
|
+
def test_the_starts_of_an_unresolved_overlap_are_decoded_and_not_called_unreached(self):
|
|
230
|
+
# 0000 calls 0007, which jumps through a one-row table to 0005, inside the mov at 0003 (the return site).
|
|
231
|
+
# Neither boundary is proven, so the walk keeps neither instruction.
|
|
232
|
+
data = bytes.fromhex("e80400" "b890c3" "c3" "2effa71000" "90909090" "0500")
|
|
233
|
+
table = {"site": 7, "exhaustive": True, "evidence": "synthetic: one row",
|
|
234
|
+
"table": {"start": 0x10, "count": 1, "stride": 2, "evidence": "synthetic: one row"}}
|
|
235
|
+
r = run_report(data, config(data, targets=[3, 5], indirectJumps=[table]), "reach")
|
|
236
|
+
self.assertEqual([t["status"] for t in r["targets"]], ["start of an unresolved overlapping instruction"] * 2)
|
|
237
|
+
with self.assertRaisesRegex(ValueError, "Positive controls failed: instruction control 3 is the start of an "
|
|
238
|
+
"unresolved overlapping instruction; instruction control 5 is the "
|
|
239
|
+
"start of an unresolved overlapping instruction$"):
|
|
240
|
+
run_report(data, config(data, targets=[6], indirectJumps=[table], instructionControls=[3, 5]), "reach")
|
|
241
|
+
|
|
175
242
|
def test_rejected_inputs(self):
|
|
243
|
+
too_many = list(range(257))
|
|
176
244
|
for extra, message in [({"starts": [0x18]}, "established region entry"),
|
|
177
245
|
({"starts": []}, "starts must be"),
|
|
178
246
|
({"targets": [0x1000]}, "targets"),
|
|
@@ -180,8 +248,12 @@ class ReachTests(unittest.TestCase):
|
|
|
180
248
|
({"leaves": [{"routine": 0x18, "reason": " "}]}, "nonempty reason"),
|
|
181
249
|
({"leaves": [{"routine": 0, "reason": "x"}]}, "start cannot be a leaf"),
|
|
182
250
|
({"controls": [0x3, 0x3]}, "distinct file offsets"),
|
|
183
|
-
({"controls": [
|
|
184
|
-
({"
|
|
251
|
+
({"controls": [0x1000]}, "controls must be an integer"),
|
|
252
|
+
({"instructionControls": [0x3, 0x3]}, "distinct file offsets"),
|
|
253
|
+
({"instructionControls": [0x1000]}, "instructionControls must be an integer"),
|
|
254
|
+
({"instructionControls": [0x0]}, "instruction control 0 is a start"),
|
|
255
|
+
({"controls": too_many}, r"controls must be a list of 0\.\.256"),
|
|
256
|
+
({"instructionControls": too_many}, r"instructionControls must be a list of 0\.\.256")]:
|
|
185
257
|
with self.subTest(extra=extra), self.assertRaisesRegex(ValueError, message):
|
|
186
258
|
reach(**extra)
|
|
187
259
|
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""Canaries for the upstream gaps the engine works around (upstream-gaps.md). Synthetic bytes only.
|
|
2
|
+
|
|
3
|
+
Each test lifts with the pinned pypcode and asserts the gap is still there. A pypcode that fixes
|
|
4
|
+
one fails its test, which is the signal to drop the workaround instead of letting it match nothing.
|
|
5
|
+
"""
|
|
6
|
+
import sys
|
|
7
|
+
import unittest
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
|
|
11
|
+
from scientific_method_engine.x86.pcode import Lifter, cs_idiom # noqa: E402
|
|
12
|
+
|
|
13
|
+
CS_GAP = ("upstream-gaps.md, 'Real mode: a CS override writes CS from the instruction address': the "
|
|
14
|
+
"pinned pypcode no longer emits the sequence pcode.cs_idiom drops. If it carries ghidra#9703, "
|
|
15
|
+
"drop cs_idiom, keep CS from the declared region, and check that the pspec's tracked CS does "
|
|
16
|
+
"not reach reports.")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class RealModeCsOverride(unittest.TestCase):
|
|
20
|
+
def test_cs_override_still_writes_cs_from_the_instruction_address(self):
|
|
21
|
+
lifter = Lifter()
|
|
22
|
+
# mov ax,cs:[bx]; jmp cs:[bx+0030]
|
|
23
|
+
for code in ("2e8b07", "2effa73000"):
|
|
24
|
+
with self.subTest(code=code):
|
|
25
|
+
ops, _ = lifter.ops(False, bytes.fromhex(code), 0x10000)
|
|
26
|
+
skip = sorted(cs_idiom(ops))
|
|
27
|
+
# One sequence per CS-relative access; the indirect JMP's lift has two.
|
|
28
|
+
self.assertTrue(skip and len(skip) % 3 == 0, CS_GAP)
|
|
29
|
+
for start in skip[::3]:
|
|
30
|
+
shift, mask, copy = ops[start:start + 3]
|
|
31
|
+
self.assertEqual((shift.code, mask.code, copy.code), ("INT_RIGHT", "INT_AND", "COPY"), CS_GAP)
|
|
32
|
+
self.assertEqual(lifter.register(False, copy.output[1], copy.output[2]), "CS", CS_GAP)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
if __name__ == "__main__":
|
|
36
|
+
unittest.main()
|
|
@@ -18,7 +18,9 @@ sys.path.insert(0, str(SRC))
|
|
|
18
18
|
ENGINE = [sys.executable, "-B", "-m", "scientific_method_engine"]
|
|
19
19
|
ENGINE_ENV = {**os.environ, "PYTHONPATH": os.pathsep.join(filter(None, [str(SRC), os.environ.get("PYTHONPATH")]))}
|
|
20
20
|
import capstone
|
|
21
|
+
from importlib import metadata
|
|
21
22
|
import pypcode
|
|
23
|
+
from scientific_method_engine.x86 import image as image_module
|
|
22
24
|
from scientific_method_engine.x86.image import Image
|
|
23
25
|
from scientific_method_engine.x86 import reports
|
|
24
26
|
from scientific_method_engine.x86.reports import run_report
|
|
@@ -2292,6 +2294,22 @@ class ReporterTests(unittest.TestCase):
|
|
|
2292
2294
|
unknown_divisor = report("f7 f3 c3")
|
|
2293
2295
|
self.assertEqual(unknown_divisor["paths"][0]["conditionalModels"][0]["assumption"], "no divide error")
|
|
2294
2296
|
|
|
2297
|
+
def test_overflow_after_a_shift_or_rotate_is_read_only_for_a_count_of_one(self):
|
|
2298
|
+
# CMP AX,AX clears OF first, so a branch that read a stale OF would be decided.
|
|
2299
|
+
def branches(shift):
|
|
2300
|
+
return events(report(f"b8 00 40 b1 01 39 c0 {shift} 70 01 43 c3"), "branch")
|
|
2301
|
+
for shift in ("d1 e0", "d1 e8", "d1 f8", "d1 c0", "d1 c8", "d1 d0", "d1 d8", "d0 e8",
|
|
2302
|
+
"c1 e0 01", "c1 f8 01", "c1 c0 01", "c1 c8 01", "c1 d0 01", "c1 d8 01", "d3 e0", "d3 d8"):
|
|
2303
|
+
with self.subTest(shift=shift):
|
|
2304
|
+
branch, = branches(shift)
|
|
2305
|
+
self.assertEqual(branch["decidedBy"], "p-code flags")
|
|
2306
|
+
# The CPU leaves OF undefined past a count of 1. SLEIGH's SHR with a count operand writes OF
|
|
2307
|
+
# as 0 for a count of 1, where the CPU writes the operand's top bit.
|
|
2308
|
+
for shift in ("c1 e0 03", "c1 e8 02", "c1 f8 02", "c1 c0 03", "c1 c8 03", "c1 d0 03", "c1 d8 03",
|
|
2309
|
+
"c1 e8 01", "d3 e8", "c0 e8 01", "d2 e8", "66 c1 e8 01"):
|
|
2310
|
+
with self.subTest(shift=shift):
|
|
2311
|
+
self.assertEqual([b.get("decidedBy") for b in branches(shift)], [None, None])
|
|
2312
|
+
|
|
2295
2313
|
def test_carry_review_regressions(self):
|
|
2296
2314
|
# A wide rotate of an unknown value stays a bounded expression.
|
|
2297
2315
|
self.assertIsNone(report("c1 c0 08 c3")["paths"][0]["stop"])
|
|
@@ -2958,6 +2976,30 @@ class ReporterTests(unittest.TestCase):
|
|
|
2958
2976
|
result = subprocess.run([sys.executable, "-B", "-c", code], capture_output=True, text=True, env=ENGINE_ENV)
|
|
2959
2977
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
2960
2978
|
|
|
2979
|
+
def test_decoder_identity_is_the_installed_capstone_distribution(self):
|
|
2980
|
+
# The Capstone 5.0.8 and 5.0.9 bindings report 5.0.7 as capstone.__version__, so the guard and
|
|
2981
|
+
# the report header read the distribution's own version.
|
|
2982
|
+
self.assertEqual(image_module.CAPSTONE_VERSION, metadata.version("capstone"))
|
|
2983
|
+
self.assertEqual(image_module.CAPSTONE_VERSION, image_module.REQUIRED_CAPSTONE)
|
|
2984
|
+
data = bytes.fromhex("c3")
|
|
2985
|
+
with mock.patch.object(image_module, "CAPSTONE_VERSION", "5.0.7"):
|
|
2986
|
+
with self.assertRaisesRegex(ValueError, r"requires capstone==5\.0\.9; capstone 5\.0\.7 is installed"):
|
|
2987
|
+
Image(data, configuration(data))
|
|
2988
|
+
with mock.patch.object(image_module, "CAPSTONE_VERSION", None):
|
|
2989
|
+
with self.assertRaisesRegex(ValueError, r"requires capstone==5\.0\.9; the installed capstone has no distribution metadata"):
|
|
2990
|
+
Image(data, configuration(data))
|
|
2991
|
+
with mock.patch.object(image_module.metadata, "version", side_effect=metadata.PackageNotFoundError("capstone")):
|
|
2992
|
+
self.assertIsNone(image_module.installed("capstone"))
|
|
2993
|
+
|
|
2994
|
+
def test_pypcode_guard_refuses_another_sleigh_specification(self):
|
|
2995
|
+
# Which flags the engine keeps after a shift is checked against pypcode 4.0.1's SLEIGH files;
|
|
2996
|
+
# pypcode 4.0.0 writes the OF of SHR by one as 0.
|
|
2997
|
+
self.assertEqual(image_module.PYPCODE_VERSION, image_module.REQUIRED_PYPCODE)
|
|
2998
|
+
data = bytes.fromhex("c3")
|
|
2999
|
+
with mock.patch.object(image_module, "PYPCODE_VERSION", "4.0.0"):
|
|
3000
|
+
with self.assertRaisesRegex(ValueError, r"requires pypcode==4\.0\.1; pypcode 4\.0\.0 is installed"):
|
|
3001
|
+
Image(data, configuration(data))
|
|
3002
|
+
|
|
2961
3003
|
def test_cli_identity_and_errors(self):
|
|
2962
3004
|
with tempfile.TemporaryDirectory() as folder:
|
|
2963
3005
|
root = Path(folder); data = bytes.fromhex("b8 01 00 c3")
|
|
@@ -2970,7 +3012,7 @@ class ReporterTests(unittest.TestCase):
|
|
|
2970
3012
|
header = json.loads(result.stdout)
|
|
2971
3013
|
self.assertEqual(header["sourceIdentity"], {"size": 4, "xxh3": cfg["xxh3"]})
|
|
2972
3014
|
self.assertEqual((header["decoder"], header["instructionSemantics"]),
|
|
2973
|
-
("capstone " + capstone
|
|
3015
|
+
("capstone " + metadata.version("capstone"), f"pypcode {pypcode.__version__} (Ghidra SLEIGH x86)"))
|
|
2974
3016
|
cfg["xxh3"] = "0" * 32; path.write_text(json.dumps(cfg))
|
|
2975
3017
|
result = subprocess.run(args, capture_output=True, text=True, env=ENGINE_ENV)
|
|
2976
3018
|
self.assertEqual(result.returncode, 1)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_config_encoding.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_effect_order.py
RENAMED
|
File without changes
|
{scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_enter_frames.py
RENAMED
|
File without changes
|
{scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_entry_frame.py
RENAMED
|
File without changes
|
|
File without changes
|
{scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_interrupt_models.py
RENAMED
|
File without changes
|
{scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_inventory_check.py
RENAMED
|
File without changes
|
{scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_join_terms.py
RENAMED
|
File without changes
|
|
File without changes
|
{scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_memory_scopes.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_return_checks.py
RENAMED
|
File without changes
|
{scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_successor_rule.py
RENAMED
|
File without changes
|
|
File without changes
|
{scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_term_limit.py
RENAMED
|
File without changes
|
{scientific_method_engine-17.0.1 → scientific_method_engine-18.1.0}/tests/test_volatile_memory.py
RENAMED
|
File without changes
|