mirror of
https://github.com/morluto/rea.git
synced 2026-11-29 18:06:46 +00:00
* test(raw): pin raw-binary admission and identity Record what raw-binary loads produce today, so a later change to how the load is represented has to keep it: - every admission result and pairing message over a grid of architectures, entry spellings and file sizes; - the analysis profile digest for each raw family; - the snapshot target and query identity of a raw ARM load; - the complete raw-binary load image for the ARM fixture. The values were computed atd6f88becdwithout changing production code. * refactor(raw): carry the raw load selection as one admitted value The caller's raw load selection (format, architecture and entry_point) travelled as three loose optional fields through about ten layers: CLI flags, MCP open, direct analysis (five call sites), the session port and open options, and target resolution. Every new load input had to be threaded through each of them. The CLI and MCP boundaries now build one NativeLoadSelection, and target admission is its only reader. Admission now returns one RawBinaryLoad, carried on the raw target as rawLoad. It names the base address of file offset 0 and the entry separately. Before, the provider client, the load-image attestation, the analysis profile and the snapshot target each read the entry and assumed it was also the base. Admission still sets the base to the entry, so nothing a caller sees changes. No contract or behaviour change. The tests pinned in the previous commit pass unchanged: the admission and pairing result grids, the per-family profile digests, the raw snapshot target and query id, and the complete raw load image. Tests that build raw targets use the new rawLoadAtEntry fixture. * feat(raw): state the M-profile raw load contract Record provisional decision D1 (#4246) as code before implementing it, so maintainers can confirm or redirect the contract cheaply. - src/domain/rawArchitectureVariant.ts owns the variant vocabulary: armv6-m, armv7-m, armv7e-m, armv8-m.base and armv8-m.main, each an ARM M profile with Thumb-only code and an M-profile vector table. It also owns the closed sets that persisted entries use: isa_mode, role (the entry's function: entry or reset) and basis (how REA chose it: caller or vector-table). - GHIDRA_RAW_VARIANT_LANGUAGES maps them to ARM:LE:32:Cortex and, for v8-M, ARM:LE:32:v8-m, which has the v8-M encodings Cortex lacks. This follows the GNU architecture names in Ghidra 12.1.4's ARM.ldefs. Provider language ids stay out of the domain. - CLI and MCP gain architecture_variant, and with it base_address and vector_table_address; the open_binary description explains them. Narrow first: a separate base is accepted only with a variant, the one lane verified with it. The pairing check names the input to correct, so MCP error paths come from one owner, and rawBinaryVariantSelectionIssue owns the rules for the new inputs. Legacy pairing messages and paths are unchanged and pinned. - Raw target summaries gain optional base_address, architecture_variant and vector_table_address. The load-image union gains the versioned segments shape (version 1): one file-backed segment, entries with address, isa_mode, role and basis, and, when the basis is the vector table, the table address with the two words read from it. - docs/raw-binary.md describes the inputs and Evidence, and two levels of identity: coordinates from resolved values only, sessions and snapshots also from the provenance that results report. It keeps the decision record, which includes not applying Ghidra's processor-defined labels to variant loads. - Tests are generated from the tables: pairing per variant and family, the Ghidra binding per variant, and the docs table and the skill reference's variant names against the tables. The persisted variant names are pinned. Admission refuses the new inputs until the following commits implement them. The base_address input follows #4927 by Jean Granié. * feat(raw): admit M-profile loads at a separate base Raw admission now implements the D1 inputs: - architecture_variant must match the family. Its code runs in Thumb state, so an even entry is accepted where generic ARM needs 4-byte alignment. An odd entry is refused as a Thumb interworking address. The error diagnostics give the even code_address and suggest omitting entry_point, so the caller's value is never silently changed. An odd generic ARM entry keeps its earlier reason and gains a hint to use the M-profile inputs. - With a variant, base_address maps file offset 0. The entry must lie inside the mapped image, and the address width is measured from the base. - With a variant and a base but no entry_point, admission asks the resolver to read the first two words of the vector table. It reads them at vector_table_address, or at the base when that is omitted. Word 1 must be odd and point inside the image; the entry is that word with bit 0 cleared. Word 0, the initial stack pointer, is recorded as observed and not validated. Admission applies the same rawBinaryVariantSelectionIssue rules as the pairing check, so CLI and MCP refuse the same selections in the same words. RawBinaryLoad records the variant, the entry's instruction set, and whether the entry was the caller's or read from a vector table. It keeps canonical values only: caller spellings are not facts about the load, and a session reused across spellings must report the same load. The grid pin is recomputed atd6f88becdwithout the spelling and is otherwise unchanged. isLegacyRawLoad decides legacy equivalence on resolved values. Target summaries report base_address, architecture_variant and vector_table_address only for loads that are not legacy-equivalent. The load_selection_rejected recovery now lists the new inputs. * feat(raw): give M-profile loads a versioned identity rawLoadIdentity is the one identity function for raw loads. A legacy-equivalent load has no new identity, so its analysis profile, coordinate context, snapshot target and query ids stay exactly as before; the pinned digests prove it. Any other load gets raw_load (version 1), built from canonical resolved values: the base, the variant, the vector table that Reset was read from, and the entry's address, instruction set, role and basis. The role names the entry's function (entry or reset); the basis records how REA chose it (caller or vector-table). Every load fact that summaries and Evidence report is in it, provenance included, so a reused session never reports what the current request did not choose. Spellings are excluded, so another base spelling, or an explicit vector table at the default location, reuses the same session. Absent values are omitted, as in the summaries and Evidence. - The snapshot target gains optional raw_load, compared canonically. Older snapshots have none and keep matching legacy loads. - The Ghidra profile commits raw_load, base_address, the variant's language, the Thumb entry context, processor_labels not-applied-v1 and load_image_evidence independent-raw-segments-v1. - The function coordinate context covers resolved values only. Beside the language and entry it already covers, it gains base_address and the entry's decode context. Analyses that differ only in role, basis, vector table or a variant with the same decoder stay comparable: a caller-named entry and Reset at one address share a coordinate context while their sessions stay separate. - The persisted variant names are pinned in raw_load. * feat(ghidra): open M-profile loads at their base in Thumb state The Ghidra raw lane now reads the committed load instead of assuming that the base is the entry: - The decoder comes from the variant (ARM:LE:32:Cortex or v8-m); generic ARM keeps ARM:LE:32:v7. - BinaryLoader maps the file at the base. A variant load is imported with -loader-applyLabels false: ARMCortex.pspec names the vector table at ram:0x0-0x40 and marks every handler slot an entry, reserved slots included, which at base 0 would cover the image and the code after the table. Loads without a variant keep the language default. - ReaGhidraPrepareRaw receives the base, the entry and the committed entry context. It requires the entry inside the file-backed block and, for Thumb, TMode=1 at the entry, and fails closed otherwise. - The bridge descriptor carries entry_context_register. The bridge accepts only the registers GHIDRA_RAW_ENTRY_CONTEXT can commit, and a launcher test keeps the two equal. It reports that register in load-image observations, so it no longer needs to match a language string to know about TMode. - Load-image attestation keeps the raw-binary image unchanged for legacy-equivalent loads; the pinned digest proves it. Other loads get the segments image (version 1): one file-backed segment at the base, the entry with its isa_mode, role and basis, the vector table address and both words read from it when Reset was derived, an entry_context.TMode check, and a check that the committed entry is the only external entry. Spellings of the same load give the same image, so a reused session reports what the current request would. - Raw limitations are derived from the load. Legacy wording is kept, except the claim that Cortex-M is unsupported. The base and entry separation in the prepare script, the TMode check and the entry-context attestation follow #4927. Co-authored-by: jgranie <78536786+jgranie@users.noreply.github.com> * test(raw): check saved raw snapshots and schemas stay compatible The segments load image is the first persisted shape these inputs add, so this commit adds the historical-snapshot and schema checks with it: - A raw-binary snapshot written atd6f88becd, before base_address, still parses. It matches the same load admitted today, and its load-image Evidence replays and equals what this version produces. An M-profile load of the same file does not match it. - The earlier raw-binary Evidence schema rejects a segments image, so readers that predate it refuse the new shape instead of misreading it. An unknown segments version is rejected as well. - The advertised inspect_native_load_image output accepts both the saved and the segments images. The advertised open_binary input accepts calls from before base_address beside the new inputs, and rejects variants outside the table. * test(ghidra): verify M-profile raw loads against real Ghidra Add npm run verify:ghidra:cortex-m, adapted from the #4927 verifier and its Cortex-M0 fixture. It drives the compiled CLI and stdio MCP server with source-owned images: - the Cortex-M0 (armv6-m) image at bases 0x0 and 0x08000000, and after a 256-byte boot2-style header opened with vector_table_address; - a Thumb-2 image (movw, movt, ldr.w, it, cbz) opened as armv7-m at 0x0 and as armv7e-m at 0x08000000; - an armv8-m.base image with movw, movt, cbz and udiv; - an armv8-m.main image that sets MSPLIM, which only the v8-M decoder names. LLVM is the independent oracle. The lane assembles each fixture's annotated source for its triple, requires identical bytes, and requires Ghidra to decode the same mnemonics in Thumb state. Mnemonics are compared without width qualifiers (ldr.w) or the dot Ghidra puts before an IT condition (mov.eq). The Cortex-M0 fill after its last return is declared as data, so neither decoder counts it as code. The lane also checks that Reset, read from the vector table, is the only entry Ghidra reports, the segments load image and its TMode check, decompilation, odd-entry refusal with code_address while the session is kept, CLI/MCP parity, unchanged legacy identity, refusal of a base without a variant, snapshot reuse across base spellings and refusal across variants, unchanged source bytes, and owned cleanup. Only named settings reach REA, which fixes the #4927 review finding that an exported REA_GHIDRA_LANGUAGE_ID broke the verifier. Verified on Linux x86_64 (Debian 13) with Ghidra 12.1.4, OpenJDK 21.0.12 and LLVM 19.1.7; the oracle also runs with Apple LLVM 21. Not run on macOS or Windows. Co-authored-by: jgranie <78536786+jgranie@users.noreply.github.com> * feat(raw): add the armv8.1-m.main variant ARMv8.1-M Mainline, as in the Cortex-M55 and Cortex-M85, gets its own architecture_variant row, decoded with ARM:LE:32:v8-m. Ghidra 12.1.4's ARM.ldefs lists armv8.1-m.main as a GNU name of that language, and its slaspec builds it with VERSION_8M. The next commit's real-Ghidra case checks that it decodes ARMv8.1-M low-overhead loops and MVE. Folding v8.1-M into armv8-m.main would use the same decoder but drop the profile from identity, and splitting it out later would change identity again. The decision record now says so. The docs table, the skill reference and the pinned persisted names follow the table. The refused-spelling test now uses armv8.1m.main, the LLVM triple's spelling, since armv8.1-m.main is accepted. * fix(ghidra): report Ghidra's ARMv8.1-M loop flow for armv8.1-m.main On real Ghidra 12.1.4, the ARM:LE:32:v8-m decoder names the ARMv8.1-M low-overhead-loop branches correctly but gets their flow wrong. In the Cortex-M55 image the next commit verifies: - WLS at 0x46 branched to 0x5a, 2 bytes short of its target 0x5c and inside the LE instruction; - LE at 0x58 branched unconditionally to 0x4e, 4 bytes past the loop start 0x4a, with no fall-through. So the code after the loop was never disassembled, and a function with such a loop is cut short. Results of armv8.1-m.main sessions now carry that limitation, and the raw-binary docs state it, instead of the claim that the decoder handles the low-overhead loops. MVE loads, stores and VCTP decode as LLVM decodes them. * test(ghidra): verify an ARMv8.1-M image against real Ghidra The cortex-m lane gains a Cortex-M55 image opened as armv8.1-m.main at base 0x0: a WLS/LE low-overhead loop around the MVE instructions VLDRW, VCTP and VSTRW. LLVM assembles it for thumbv8.1m.main with -mcpu=cortex-m55 -mfloat-abi=hard, because clang 19 refuses MVE under the default soft-float ABI. The oracle takes the CPU and float ABI from the fixture. Ghidra must decode each instruction it reaches with LLVM's mnemonic in Thumb state, as in the other cases. A data type that gives only the element size (Ghidra vldrw.32) matches one with signedness (LLVM vldrw.u32); opposite signedness still fails. The lane now reports every disagreement in one run instead of stopping at the first. Ghidra 12.1.4 does not follow the loop's exits (see the previous commit), so this case requires the pop after the loop to stay undisassembled, records both loop branches' flow, and checks that the results report the limitation. Base 0 also checks that the v8-m decoder's vector table gets no processor labels. Verified on Linux x86_64 (Debian 13) with Ghidra 12.1.4, OpenJDK 21.0.12 and LLVM 19.1.7: all eight cases pass. The oracle also runs with Apple LLVM 21. Not run on macOS or Windows. * refactor: name the variant vocabulary after architecture_variant The variant table is no longer only for raw loads: the M-profile ELF lane uses the same vocabulary. Its internal names now follow the public input, architecture_variant, without an ARM M-profile prefix that would need renaming again once other architectures get variants: - src/domain/rawArchitectureVariant.ts becomes architectureVariant.ts, together with its test and the docs conformance test; - RawArchitectureVariant, RAW_ARCHITECTURE_VARIANTS, RAW_ARCHITECTURE_VARIANT_NAMES, rawArchitectureVariantSchema and rawArchitectureVariantRow drop the raw prefix; - GHIDRA_RAW_VARIANT_LANGUAGES becomes GHIDRA_VARIANT_LANGUAGES, and ghidraRawProcessorLabels becomes ghidraVariantProcessorLabels. Identifiers only. Saved values, schemas, lane ids and the processor_labels value are unchanged: the recorded catalog digests and MCP catalog are byte-identical before and after, the pinned profile and snapshot digests pass unchanged, and no snapshot fixture changes. Names that mean the raw load keep theirs: RawBinaryLoad, rawLoad, raw_load, and the vocabularies of its entries (RAW_ENTRY_ISA_MODES, RawEntryIsaMode, RAW_ENTRY_ROLES and RAW_ENTRY_BASES). * refactor(ghidra): keep variant decoder limitations beside the languages The low-overhead-loop limitation lived in the raw-binary limitations, but it describes Ghidra's ARM:LE:32:v8-m decoder for armv8.1-m.main whatever container holds the code. The M-profile ELF lane maps ELF builds to the same variants and must report the same text. GHIDRA_VARIANT_LIMITATIONS now sits beside GHIDRA_VARIANT_LANGUAGES and states the limitations of every variant, so adding a variant also decides them. Raw results read it; their text and order are unchanged. * fix(raw): require base_address with every architecture_variant A variant without base_address mapped the image at its entry, and the odd-entry suggestion led callers there: retrying with the even code_address and no base mapped the vector table at Reset, and the segments image still verified. architecture_variant now requires base_address, the address of file offset 0, and the odd-entry suggestion no longer offers a retry without a base. A code fragment that starts at its entry passes that address as its base. Loads without a variant are unchanged. * fix(raw): align vector tables as VTOR does and refuse Reset inside them VTOR.TBLOFF holds address bits 31:7 wherever VTOR exists (ARMv6-M with VTOR, ARMv7-M, ARMv8-M), so a vector table is at least 128-byte aligned. Admission accepted any 4-byte-aligned table, so naming the Reset slot (0x10000104) instead of the table (0x10000100) read NMI as Reset and reported it with role reset. A named table and the default at the base now need 128-byte alignment. A Reset word that points into the 8 table bytes just read is refused too. Even, outside and inside Reset words stay load_selection_rejected, with the observed words as diagnostics: a raw dump has no format they can violate, and the usual cause is a wrong base, table or variant. * fix(ghidra): key variant coordinates by base, not entry A variant load maps file offset 0 at base_address, so with the decoder and the entry's decode context the base fixes every coordinate. The coordinate context still held the entry address, so functions found from two entries of one image did not compare. Variant coordinate contexts now take the base from the admitted load and leave the entry out. Variant profiles commit raw_load without flat entry_point and base_address copies of it; entry_context stays as Ghidra's seeding commitment, which raw_load does not record. Legacy profiles keep their parameters and digests. * refactor(raw): discriminate the segments load image by layout The segments load image named its shape with variant: "segments", beside architecture_variant, which names the decoder. It is now layout: "segments", as layout names data shapes elsewhere in REA. The shape is unreleased, so no saved Evidence uses the old key. * docs(raw): record why vector-table words keep their full width vector_table.initial_stack_pointer and reset_vector are words read from the table, not addresses: REA reports format words such as GPU code-object flags and metadata tokens as full-width hex, and the entry already holds Reset's canonical code address. The schema, admission and raw-binary guide now say so. * docs(raw): record why the seeded entry function is named entry Ghidra names the function REA seeds at a raw entry entry, whatever the entry's role, as its loaders name a program entry. The load image reports the role; naming the function after it, as CMSIS Reset_Handler, would claim a symbol the image does not carry. The prepare script and the decision record now say so. * fix(raw): name the refused load input in error details Raw admission refused a pairing with the rule's message but dropped the field it named, so callers had to read the input to correct from prose. Each raw admission refusal now names the input to blame, and BinaryTargetError projects it as details.field on the CLI and MCP; the Reset-word refusals and a file too short for a table at its base name none. Pairing refusals from the target resolver keep their field too. MCP refuses invalid pairings in the open_binary input schema before admission; that error reports path [] as it does on main and is left unchanged here. * docs(raw): state that results keep no caller spellings yet The raw_load identity comment claimed Evidence parameters keep the caller's spellings of the load inputs. They do not: summaries, Evidence and snapshots report canonical addresses only, as legacy entry_point did before these inputs. The comment and the raw-binary guide now say so, and the decision record names the gap and the caller_input_path pattern that could close it additively. * docs(raw): explain why reused sessions cannot report stale vector words raw_load leaves the two vector-table words out, yet results report them. They cannot go stale: sessions and snapshots also match the file digest, and with the base and the table address it fixes the bytes the words are read from. The identity comment and the raw-binary guide now say so. * docs(raw): scope architecture_variant to the ARM M profiles The generic raw-language limitation listed 68020, ColdFire and other "processor variants" as unsupported, which read as future architecture_variant values. It now says those languages are not offered and that architecture_variant names only ARM M profiles. The variant table and the decision record also state the scope: the base requirement, Reset derivation and label policy hold for every row because every row is an M profile, and a variant of another family would first make them per-row properties. * docs(raw): state each load pairing rule once in open_binary open_binary said that architecture_variant requires base_address, and that entry_point may then be omitted, in its tool description and again in three field descriptions; the default compact input profile advertises all of them. Each rule now sits on the field it constrains, and the tool description points to the M-profile lane in one sentence. CLI flag help follows the same split. Measured from tools/list of a server built from this source, the trim saves 237 bytes of open_binary in each input-schema profile. * refactor(ghidra): let session limitations own the variant load facts A segments load image restated that only the file is mapped, that processor labels are not applied and where Reset came from, beside the session limitations that every result, the load image included, already carries. The load image now states only what its verification covers and requires; the session limitations are the one owner of the load facts. * refactor(ghidra): launch with the label policy the profile committed The Ghidra client recomputed the processor-label policy from the target's variant instead of reading the processor_labels value the analysis profile commits, so the launch and the profile each held their own copy of the rule. The client now reads the committed value, named once as GHIDRA_PROCESSOR_LABELS_NOT_APPLIED, and a behavior test checks that a variant profile reaches the launcher without labels. * refactor(raw): let the vector-table read carry its length Admission and the target resolver each knew that the vector table read is 8 bytes: the resolver passed a literal 8 and admission checked one. RawBinaryVectorTableRead now carries byteLength beside fileOffset, and admission derives its bounds, inside-table check and message from one constant. * test(raw): compare saved raw images by their facts, not their prose The compatibility test required a snapshot saved before base_address to equal today's raw load image exactly, limitation text included, so any rewording of a limitation would fail it although no fact changed. It now compares every field except limitations. * test(ghidra): expect not-in-listing where the Cortex-M lane stops Since #7823, Ghidra reports an address in memory with no listed instruction or data as not-in-listing instead of undecodable. The Cortex-M55 case expects the pop after its low-overhead loop to stay undisassembled, so the lane now expects not-in-listing there. * test(raw): pin raw identity to main after its instruction-status revision #7823 commits instruction_status in every Ghidra profile, so the legacy raw digests pinned atd6f88becdand the variant digest changed, and the snapshot saved before base_address no longer replays on main either. The legacy pins now hold main's own digests at07dfd1261, computed without these inputs; the merged branch matches them for all six families. The variant digest is re-pinned. The compatibility test now checks that the saved snapshot keeps its target, that its profile differs from today's only by instruction_status, and that its load-image facts equal today's. The raw-binary guide states replay relative to REA without these inputs. * test(ghidra): own the Cortex-M lane's LLVM and CLI commands The lane's LLVM oracle ran clang and llvm-objdump through execFile with no deadline, and its CLI parity launch used execFile directly, so a hung tool could stall the lane and a killed one could leave children behind. All three now run through createVerifierCommands, the owner that stops each process tree, with a 120-second deadline for LLVM and 240 seconds for the CLI, as the ESP image verifier does. The oracle writes under the lane's workspace instead of its own temporary directory, and the lane closes the owner before removing that workspace; a failed close keeps it for inspection. * test(raw): derive the Reset fixtures from one Thumb image The segments tests verified a Reset load admitted for 16 bytes, with vector words supplied separately, against the 4-byte ARM fixture, so the image they called verified held neither the table nor the entry at 0x8000008. fixtureRawBinaryLoadImage now takes the image bytes and the load and derives the digest, size, mapping, entry and bound language from them; its defaults keep the legacy fixture unchanged. The Thumb tests and the compatibility test's segments image use the Cortex-M0 image the real lane verifies, admit Reset from its own first eight bytes, and name a caller entry at its add_three function. A declaration file lets TypeScript tests import that fixture. * docs(raw-binary): say that recording load spellings would not be additive The Evidence envelope is strict, so a later field that keeps a load selection's spellings would make records that REA versions without it refuse, as caller_input_path did. The guide called that change additive. * docs(raw-binary): tell callers how to keep their load spellings Results report canonical addresses, and an omitted vector_table_address reads as one at the base. A caller that needs its exact spelling keeps its own request beside the Evidence, with the record's evidence_id: the raw-binary guide says so for people, and the skill's raw-image paragraph for agents, loaded only when an agent opens that reference. * test(ghidra): keep raw digests at main's byte-search and data-type revisions * test(domain): check raw-binary refusal facts, not sentences The new variant and raw-binary refusal tests compared whole explanations. They now check the rejected field, constraint and diagnostics exactly, and match the parameter names and addresses each reason must state as whole tokens through tests/support/statingFacts.ts. Rewording a refusal passes; dropping its mapped range fails. The admission and pairing grids stay digests of results computed on main before this change. * docs(adr): move the M-profile raw load decision into an ADR The raw binary guide held decision D1 inline. Record it under docs/adr/firmware/ with Nygard sections and Proposed status, index it, and link it from the guide. * test(ghidra): repin digests and replay for main's call-coverage revision main's #8340 bumps call_relationship_coverage from v5 to v7, which changes every Ghidra profile digest. Repin the raw digests this branch owns, and set the revision aside in the replay test of the snapshot saved before base_address, like the other upstream revisions it lists. With the revision reverted in place, the old pins hold; only main's own two pins, which assert v7, then fail. * chore(plugin): regenerate the skill bundle from the authored skill * test(raw): keep the target route diagnostics within the function length limit * docs(raw): record the M-profile lane on Ghidra 12.2 * refactor(raw): read vector words and own the Cortex-M workspace through their shared owners Main's architecture guards require BinaryReader for numeric reads and VerifierFixture for verifier fixture roots. * fix(raw): name raw-binary as the format of vector-table read failures * fix(verify): remove the Cortex-M workspace after a verified run closeEarly stops the command owner but keeps the root, so every successful run left its fixtures, snapshots and LLVM outputs behind while reporting cleanup complete. Release the workspace once process lineage is verified; a failed run still keeps it for diagnosis. * chore(verify): record open_binary's input schema with the M-profile load fields CI's package build compares the committed advertised-schema summaries with the packaged catalog; regenerated with `node scripts/verify-advertised-schemas.mjs --update-summary`. * test(cli): expect open_binary's M-profile load flags in the CLI command table Main's command-table test (#11777) pins the flag of each open_binary field a direct-analysis command takes; D1 adds --architecture-variant, --base-address and --vector-table-address. * test(cli): record the CLI help goldens with the M-profile load flags Commands that open a target list open_binary's options; D1 adds --architecture-variant, --base-address and --vector-table-address. Recorded with REA_RECORD_CLI_PARITY=1 from the built CLI. * docs(raw-binary): qualify snapshot replay under the producer rule A load without the M-profile inputs keeps its analysis profile inputs, snapshot target and load image, but every analysis profile also commits to the REA release and packaged bridge scripts. A snapshot saved by an earlier release is therefore refused after an upgrade either way. Say so, and point to the Ghidra guide's producer rule. * test(ghidra): report why Cortex-M verifier cleanup could not be checked The verifier compared the completed lineage's status and descendants directly, so an unavailable lineage failed as a bare equality without its reason. Use the shared assertVerifierCleanupComplete, which names the reason, as docs/development/testing.md requires. * fix(domain): compare raw-load functions by coordinates, not entry provenance compare_functions and build_call_path required equal target identity, which for a non-legacy raw load includes the entry's address, role and basis and the vector table it was read from. Two loads of the same image at the same base, one with Reset read from the vector table and one with a caller-named entry, therefore stopped comparing, although their function coordinate contexts match and the raw-binary guide says they compare. targetIdentity now owns sameTargetCoordinates, which keeps a raw load's base and variant and leaves out how it chose its entry; a legacy load still places addresses by its entry. Both comparison workflows use it. Session, snapshot and Evidence subject matching keep the full identity. * fix(domain): align coordinateKey canonicalization and target diagnostics with main --------- Co-authored-by: jgranie <78536786+jgranie@users.noreply.github.com> Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
317 lines
17 KiB
Markdown
317 lines
17 KiB
Markdown
# Headerless raw-binary analysis with Ghidra
|
|
|
|
Headerless images are covered here. GameCube/Wii DOL images have a header and
|
|
use their own format selection; see [GameCube DOL analysis](../ghidra-dol.md).
|
|
|
|
REA admits a headerless executable when the caller supplies the CPU family and
|
|
the address where file offset 0 should be mapped. That interpretation is not
|
|
inferred from a `.bin` suffix or from unrecognized bytes. ARM M-profile images
|
|
can also name their profile and take their entry from the vector table; see
|
|
[ARM M-profile images](#arm-m-profile-images).
|
|
|
|
## Open and inspect
|
|
|
|
```bash
|
|
rea inspect /absolute/path/to/kernel.img --target-format raw-binary --architecture arm --entry-point 0x8000 --provider ghidra --json
|
|
rea function /absolute/path/to/kernel.img 0x8000 --target-format raw-binary --architecture arm --entry-point 0x8000 --provider ghidra --json
|
|
rea inspect-native-load-image /absolute/path/to/kernel.img --target-format raw-binary --architecture arm --entry-point 0x8000 --provider ghidra --json
|
|
```
|
|
|
|
```json
|
|
{
|
|
"name": "open_binary",
|
|
"arguments": {
|
|
"path": "/absolute/path/to/kernel.img",
|
|
"format": "raw-binary",
|
|
"architecture": "arm",
|
|
"entry_point": "0x8000",
|
|
"provider_id": "ghidra"
|
|
}
|
|
}
|
|
```
|
|
|
|
The example maps a 32-bit ARM-state image at `0x8000`. For `arm`, decoding
|
|
starts at the entry in ARM (A32) state, so Thumb-only images, including Cortex-M
|
|
firmware, are not supported. See [Limitations](#limitations).
|
|
|
|
`architecture` is one of `x86`, `x86_64`, `arm`, `arm64`, `6502`, or `m68k`. `entry_point` is a
|
|
hexadecimal address of the form `0x…`. The complete file is mapped starting at
|
|
that address, and Ghidra seeds an `entry` function there before auto-analysis.
|
|
ARM and AArch64 entries must be 4-byte aligned; m68k entries must be 2-byte
|
|
aligned; x86 and 6502 entries may be byte-aligned.
|
|
For 6502, the entry and complete mapped file must fit within `0x0000..0xffff`;
|
|
images that cross the 16-bit boundary are rejected before provider startup.
|
|
For m68k, the mapping must fit within `0x0..0xffffffff`.
|
|
`open_binary` and an open `binary_session` return the canonical `entry_point`
|
|
for raw targets so callers can verify the selected mapping directly.
|
|
Omitted `format` keeps header-based detection; architecture and entry without
|
|
`format=raw-binary` are rejected.
|
|
|
|
Ghidra imports a private snapshot with `BinaryLoader` and these generic
|
|
languages and compiler specifications. Each family has one fixed byte order:
|
|
m68k is big-endian and the others are little-endian.
|
|
|
|
| architecture | language_id | compiler_spec_id |
|
|
| ------------ | --------------------- | ---------------- |
|
|
| x86 | `x86:LE:32:default` | `gcc` |
|
|
| x86_64 | `x86:LE:64:default` | `gcc` |
|
|
| arm | `ARM:LE:32:v7` | `default` |
|
|
| arm64 | `AARCH64:LE:64:v8A` | `default` |
|
|
| 6502 | `6502:LE:16:default` | `default` |
|
|
| m68k | `68000:BE:32:default` | `default` |
|
|
|
|
Compiler specifications select analysis conventions; they do not identify the
|
|
original compiler or ABI of a headerless target. Ghidra's x86 languages do not
|
|
provide a compiler specification named `default`.
|
|
|
|
Those settings, the caller-supplied entry, and `language_selection:
|
|
caller-supplied` are committed in the analysis profile. A different entry
|
|
or architecture is a different profile; snapshots from header-detected targets
|
|
do not replay.
|
|
|
|
The mapped start is independent of Ghidra's program image base, which remains
|
|
`0x0` for BinaryLoader. Ghidra may report additional language-defined entry
|
|
points, including unmapped ARM vectors. Load-image verification requires the
|
|
caller-supplied entry and preserves those other entries as observations.
|
|
For 6502, BinaryLoader maps the file before adding its language-defined zero-page
|
|
and stack blocks. Conflicting default blocks are skipped; low-address files stay
|
|
in the default address space rather than an overlay. Non-conflicting uninitialized
|
|
blocks remain observations, not file-backed bytes.
|
|
Independent raw verification streams the complete private snapshot digest and
|
|
extent with bounded memory and supports cancellation during the read.
|
|
It also requires the source-backed block to be initialized, loaded, and
|
|
non-overlay in the bound language's default address space. Contradictory mapping
|
|
observations are retained with `mismatch`, even when the source hashes agree.
|
|
|
|
Hopper and IDA refuse raw-binary targets. Windows Ghidra P0 admits native PE
|
|
applications/DLLs and explicitly selected raw 6502 images. Other raw CPU families
|
|
remain outside the Windows boundary. Native path admission, private DACLs, Job
|
|
Object ownership, immutable snapshots, and the no-mutation policy still apply.
|
|
|
|
## ARM M-profile images
|
|
|
|
Cortex-M flash images start with a vector table and run only Thumb code. A dump
|
|
read from a device, for example with `st-flash read dump.bin 0x08000000 <size>`,
|
|
records neither the address it was read from nor the M profile of the core.
|
|
Name both:
|
|
|
|
```bash
|
|
rea inspect /absolute/path/to/dump.bin --target-format raw-binary --architecture arm --architecture-variant armv7-m --base-address 0x08000000 --provider ghidra --json
|
|
```
|
|
|
|
```json
|
|
{
|
|
"name": "open_binary",
|
|
"arguments": {
|
|
"path": "/absolute/path/to/dump.bin",
|
|
"format": "raw-binary",
|
|
"architecture": "arm",
|
|
"architecture_variant": "armv7-m",
|
|
"base_address": "0x08000000",
|
|
"provider_id": "ghidra"
|
|
}
|
|
}
|
|
```
|
|
|
|
`architecture_variant` names the M profile and selects the Ghidra decoder:
|
|
|
|
| architecture_variant | Profile | language_id | compiler_spec_id |
|
|
| -------------------- | ------------------ | ------------------ | ---------------- |
|
|
| `armv6-m` | ARMv6-M | `ARM:LE:32:Cortex` | `default` |
|
|
| `armv7-m` | ARMv7-M | `ARM:LE:32:Cortex` | `default` |
|
|
| `armv7e-m` | ARMv7E-M | `ARM:LE:32:Cortex` | `default` |
|
|
| `armv8-m.base` | ARMv8-M Baseline | `ARM:LE:32:v8-m` | `default` |
|
|
| `armv8-m.main` | ARMv8-M Mainline | `ARM:LE:32:v8-m` | `default` |
|
|
| `armv8.1-m.main` | ARMv8.1-M Mainline | `ARM:LE:32:v8-m` | `default` |
|
|
|
|
`ARM:LE:32:Cortex` is Ghidra's ARMv7 decoder with Thumb as the default state. It
|
|
has no ARMv8-M encodings, such as the `MSPLIM` and `PSPLIM` system registers that
|
|
v8-M startup code sets, so the v8-M and v8.1-M variants use `ARM:LE:32:v8-m`.
|
|
Ghidra 12.2's `ARM.ldefs` lists `armv8.1-m.main` for that language, which also
|
|
decodes ARMv8.1-M encodings such as MVE and the low-overhead loops (`wls`, `le`);
|
|
the loop branches' flow is wrong, as Limitations describes.
|
|
Neither decoder refuses instructions that a smaller profile lacks: `armv6-m`
|
|
code is decoded with the ARMv7-M instruction set, and `armv8-m.main` code with
|
|
the ARMv8.1-M one.
|
|
|
|
- `base_address` maps file offset 0 at its own address, and `entry_point` must
|
|
then lie inside the mapped image. It is required with `architecture_variant`
|
|
and refused without it, the one lane whose verifier covers a separate base. A
|
|
dump records neither its load address nor where Reset is, and mapping it at
|
|
the entry would put a vector table at Reset; a code fragment that starts at
|
|
its entry passes that address as `base_address`.
|
|
- Without `entry_point`, REA reads the vector table at `vector_table_address`, or
|
|
at `base_address` when that is omitted. Word 1 is the Reset handler with bit 0
|
|
set for Thumb state. The vector value is observed, and the entry is derived by
|
|
clearing bit 0. Word 0, the initial stack pointer, is recorded as observed and
|
|
is not validated.
|
|
- Use `vector_table_address` when a header precedes the vector table, such as the
|
|
256-byte boot2 stage of an RP2040 image or an MCUboot header. It applies only
|
|
when `entry_point` is omitted. The table, whether named or at the base, must be
|
|
128-byte aligned, as VTOR requires (`TBLOFF` holds address bits 31:7), and it
|
|
must lie inside the mapped image.
|
|
- REA refuses a Reset word that is even, that points outside the mapped image or
|
|
into the 8 table bytes it read, or that lies past the end of the file. These
|
|
are `load_selection_rejected` refusals with the observed words in
|
|
`details.diagnostics`: a raw dump has no format they can violate, and the usual
|
|
cause is a wrong `base_address`, `vector_table_address` or variant.
|
|
- An explicit `entry_point` must be even. An odd value is a Thumb interworking
|
|
address, such as a word copied from the vector table. REA refuses it instead
|
|
of clearing bit 0; `details.diagnostics` in the error give the even
|
|
`code_address` and suggest omitting `entry_point`.
|
|
- Each refusal of a load selection is an `invalid_request` with
|
|
`details.constraint: "load_selection_rejected"` and, when one input is to
|
|
blame, that input in `details.field`, such as `entry_point` for an odd Thumb
|
|
entry. The Reset-word refusals name none.
|
|
|
|
Before auto-analysis, REA's preparation script requires Ghidra's Thumb context
|
|
(`TMode=1`) at the entry and seeds the entry there; `inspect_native_load_image`
|
|
attests that context independently. Ghidra imports the file without its
|
|
processor-defined labels. `ARMCortex.pspec` names the vector table at
|
|
`0x0`-`0x40` and marks every handler slot an entry, reserved slots included, so
|
|
at base `0x0` its names would cover the table and the code after it. The load
|
|
image attests that the seeded entry is the only one.
|
|
|
|
`npm run verify:ghidra:cortex-m` checks this lane against real Ghidra 12.2
|
|
with source-owned fixtures and LLVM as the decoding oracle; see
|
|
[testing lanes](../testing-lanes.md).
|
|
|
|
### Identity and Evidence
|
|
|
|
A load whose base is its caller-selected entry and that names no variant is the
|
|
same load as before these inputs existed, decided on these resolved values. Its
|
|
analysis profile, snapshot target, session reuse and `raw-binary` load image are
|
|
unchanged. Any other raw load:
|
|
|
|
- reports `base_address`, `architecture_variant` and, when Reset was read from
|
|
the vector table, `vector_table_address` in `open_binary` and
|
|
`binary_session`;
|
|
- commits `raw_load` (`version: 1`) to its analysis profile and snapshot target:
|
|
the base, the variant, the vector table that Reset was read from, and the
|
|
entry's address, instruction set, `role` and `basis`;
|
|
- returns the `segments` load image (`layout: "segments"`, `version: 1`): one
|
|
segment backed by the whole file at the base, one entry with its `isa_mode`,
|
|
`role` and `basis`, and, when the basis is the vector table, `vector_table`
|
|
with the table address and the two words read from it, as full-width 32-bit
|
|
words such as `0x080001c5`; the entry holds the canonical code address.
|
|
|
|
An entry's `role` names its function: `entry` for an entry the caller named,
|
|
`reset` for the Reset handler. Its `basis` records how REA chose it: `caller`
|
|
or `vector-table`. Ghidra names the function it seeds there `entry` whatever
|
|
the role, as its loaders name a program entry; the role, not that name, says
|
|
which handler it is.
|
|
|
|
Identity has two levels:
|
|
|
|
- Coordinate identity (`function_coordinate_context`) covers only resolved
|
|
values that move coordinates: the decoder, the base, and the entry's decode
|
|
context. Every entry of one image at one base gets the same coordinate
|
|
context, so functions found from a caller-named entry and from Reset compare.
|
|
A legacy load maps file offset 0 at its entry, so there the entry address is
|
|
the base.
|
|
- Session and snapshot identity also cover the provenance that results report:
|
|
`raw_load` adds the variant, the vector-table address, and the entry's role
|
|
and basis. The vector words need no place of their own: they are the file
|
|
bytes at the table address, which the file digest and the base fix. A reused
|
|
session never reports a basis or vector words that the current request did
|
|
not produce.
|
|
|
|
Neither level covers spellings: `0x08000000` and `0x8000000` are the same base,
|
|
an omitted `vector_table_address` equals an explicit one at the base, and REA
|
|
reports canonical addresses only. No result retains the spelling a caller used
|
|
yet. A caller that needs its spelling keeps its own request: an MCP client
|
|
already holds its arguments, and a CLI caller can save its command beside the
|
|
Evidence it writes, together with its `evidence_id`.
|
|
|
|
These inputs leave a load without them exactly as REA loaded it before: its
|
|
analysis profile inputs, snapshot target and `raw-binary` load image are
|
|
unchanged, so its saved load images stay readable. Snapshots still follow the
|
|
[producer rule](ghidra-provider.md#admitted-function-analysis-semantics): every
|
|
analysis profile commits to the REA release and its packaged bridge scripts, so
|
|
a snapshot that an earlier REA release saved is refused after an upgrade, with
|
|
or without these inputs. Open the target again and save a fresh snapshot. A
|
|
snapshot of a load that uses these inputs also has a different analysis profile,
|
|
so REA versions without these inputs cannot replay it.
|
|
|
|
## NMOS 6502
|
|
|
|
```bash
|
|
rea function /absolute/path/to/image.bin 0x8001 --target-format raw-binary --architecture 6502 --entry-point 0x8001 --provider ghidra --json
|
|
rea inspect-native-load-image /absolute/path/to/image.bin --target-format raw-binary --architecture 6502 --entry-point 0x8001 --provider ghidra --json
|
|
```
|
|
|
|
Select only headerless NMOS 6502 code with a known entry at file offset 0.
|
|
The built-in Ghidra processor supplies static instructions, functions,
|
|
pseudocode, and references. Evidence preserves `architecture: "6502"`, the
|
|
selected language, entry, file digest, and verified source mapping. Register and
|
|
calling conventions are analysis conventions, not a recovered source ABI.
|
|
|
|
This lane does not interpret iNES headers, cartridge mappers, bank switching,
|
|
or other console-specific memory layouts. 65C02 and 65816 programs need different
|
|
processor semantics and are not admitted as those variants. REA does not infer
|
|
the CPU from the file suffix, guess the reset vector, or emulate the target.
|
|
|
|
`npm run verify:ghidra:6502` assembles and links the source-owned fixture with
|
|
`ca65` and `ld65` (tested from cc65 tag `V2.19`, whose tools report `V2.18`).
|
|
Set `REA_CA65` and `REA_LD65` to explicit executable paths if needed. The lane
|
|
checks real CLI/MCP load-image parity, mapped bytes, decoded calls, decompilation,
|
|
invalid mappings, unchanged source bytes and runtime cleanup.
|
|
Low-memory checks include a file spanning the zero-page/stack boundary at `0xff`
|
|
and a complete 64-KiB image at `0x0`, with verified default-space mappings through
|
|
both CLI and MCP. On Windows, the lane also checks that a caller-selected
|
|
junction path is rejected at snapshot admission through CLI and MCP, with
|
|
unchanged source bytes and complete runtime cleanup. Windows requires the
|
|
matching native addon; the POSIX provider path has not been exercised by the
|
|
Windows run.
|
|
|
|
## Motorola 68000
|
|
|
|
```bash
|
|
rea function /absolute/path/to/image.bin 0x1000 --target-format raw-binary --architecture m68k --entry-point 0x1000 --provider ghidra --json
|
|
rea inspect-native-load-image /absolute/path/to/image.bin --target-format raw-binary --architecture m68k --entry-point 0x1000 --provider ghidra --json
|
|
```
|
|
|
|
Select only headerless big-endian 68000-family code with a known, even entry at
|
|
file offset 0. Ghidra's `68000:BE:32:default` language decodes the 68040
|
|
superset in the 32-bit `ram` address space. Instructions that a real MC68000
|
|
would not execute are therefore not rejected, and 68020, 68030, CPU32, and
|
|
ColdFire programs are not admitted as those variants. Addresses are not masked
|
|
to the MC68000 24-bit bus. Evidence preserves `architecture: "m68k"`, the
|
|
selected language, entry, file digest, and verified source mapping. Register and
|
|
calling conventions are analysis conventions, not a recovered source ABI.
|
|
|
|
This lane does not interpret Amiga hunks, Atari ST, or other container headers,
|
|
cartridge or console memory maps, or bank switching. REA does not read the
|
|
initial stack pointer and reset vector from the first eight bytes; select the
|
|
entry explicitly. Windows Ghidra P0 does not admit m68k.
|
|
|
|
`npm run verify:ghidra:m68k` uses a source-owned 22-byte fixture written as
|
|
annotated hex in the verifier, so it needs no assembler. The lane checks real
|
|
CLI/MCP load-image parity, mapped bytes, a decoded call, decompilation by address
|
|
and file offset, misaligned and out-of-range mappings, unchanged source bytes and
|
|
runtime cleanup. It passed on Ghidra 12.2 with JDK 25 on macOS arm64; Linux has
|
|
not been exercised.
|
|
|
|
## Limitations
|
|
|
|
- Headers are not discovered from file bytes. REA does not guess load
|
|
addresses, profiles, vector-table locations, or memory maps.
|
|
- Big-endian ARM is unsupported, and m68k is the only big-endian family.
|
|
`architecture_variant` names only the ARM M profiles; no other processor of a
|
|
family, such as the 65C02 or ColdFire, can be selected.
|
|
- An M-profile load maps one segment: the file at its base. SRAM, peripheral and
|
|
system blocks are not created, and only the Reset vector is read. Ghidra's
|
|
processor-defined vector labels are not applied, so other vectors are neither
|
|
entries nor labels. Floating-point state, TrustZone and device memory maps are
|
|
not modelled.
|
|
- Ghidra 12.2, like 12.1.4, decodes the ARMv8.1-M low-overhead-loop branches
|
|
`wls` and `le` with the right mnemonics, but not their flow. In the verifier's Cortex-M55
|
|
image, `wls` branched 2 bytes short of its target, `le` branched 4 bytes past
|
|
the loop start, and `le` had no fall-through. Code after such a loop may be
|
|
left undisassembled; `armv8.1-m.main` results say so.
|
|
- Reset vectors, memory-mapped I/O, and firmware boot sequences are not
|
|
emulated.
|
|
- Independent load-image verification covers source identity, mapped file
|
|
bytes, the bound language, and the caller-supplied entry. Original bytes
|
|
stay unchanged.
|