cpu: translate RIP-relative data against an explicitly bound image mapping #410

Merged
bosseurdinternet merged 2 commits from upstream-pr/416 into main 2026-11-22 04:24:02 +00:00
bosseurdinternet commented 2026-11-22 01:01:26 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-22T01:01:26Z.


Change

Refs #317 (RIP-relative data). Builds on #285 and #309 (both merged), and is independent of #321 and #343.

In tools/cpu_switch_aot.py a RIP-relative memory operand is refused, and the result of a RIP-relative LEA is a guest address that may only feed a jump table. A function that reads or writes a global, stores the address of a table or returns the address of a static object is therefore not translated. This adds an opt-in mode in which such data is addressed in a live mapping of the input image that the host supplies.

  • Opt-in, per run. --image-data on the CLI, image_data=True on lift, translate_pe and translate, IMAGE_DATA in cpu_translation(). Without it, the same functions translate, their generated C++ is identical to main's and the refusal messages are unchanged. Translated signatures are the same in both modes.
  • Supplying the mapping. The generated DLL exports int lifted_bind_image(word base) and void lifted_unbind_image(). Bind accepts the base of a live mapping of the input image, made by Windows or by pe_map, and returns 0 when accepted, 1 for a null base, 2 when the headers differ and 3 when read-only contents differ. A refused bind keeps the current binding.
  • Matching the input image. The translator computes, from the file, a hash of header fields (DOS and PE signatures, the file header with TimeDateStamp, SizeOfImage, CheckSum, the section table; ImageBase excluded because loaders rewrite it) and a hash of the file-backed bytes of readable, non-writable sections, excluding DIR64 relocation slots and the import address table. Bind recomputes both on the mapping; the contents are read only after the headers match. The hash is FNV-1a: it detects a different or altered image, not a crafted one. Writable data is not part of the identity.
  • Checked RVA. At translation time every RIP-relative access must lie, for its whole width, in one section's virtual extent; a read needs read access and a write needs a writable, non-executable section. A LEA performs no access and is checked against the image extent only, since MSVC's lea r, __ImageBase targets the headers. Each refusal has a named message.
  • Translation. A RIP-relative operand becomes a load or store at base + RVA through the existing helpers. A RIP-relative LEA yields base + RVA, a fully defined value that can be stored, compared, used as a base and returned. A LEA that loads a recognised jump-table base keeps today's treatment and needs no mapping.
  • Unbound use. A function that addresses image data reads the base once at entry and, when nothing is bound, raises structured exception 0xE0494D47 as noncontinuable before any access. I chose this over std::abort() because a Debug CRT abort can show a dialog; that reason is from documented CRT behaviour, not measured here.
  • Lifetime. Documented in the generated source and in docs/experiments.md: an address a translated function returns or stores is valid only while that mapping stays bound and mapped; bind before use, with no concurrent rebind. This is stated, not enforced.

Verification

Local runs used the same compiler build as CI (MSVC 14.52.37025: cl, link, ml64) under Wine 11.0 on Linux, at 1ad9c0c on main e5e0079. I reran the items below on this head unless a line says otherwise.

  • ctest --no-tests=error -R '^cpu_aot_image_data$' -V: passed. The original runs in one pe_map mapping of the fixture and the translation is bound to a second mapping of the same file, each starting from the image's initial data. After each of 29 calls the test compares return values, the guarded trace, RBX and RSI, and the writable data of the two mappings (1,048 bytes).
  • Addresses compared by RVA. Returned addresses, the pointer stored into the caller's object, and the relocated pointer in writable data are compared as address - base between the two mappings. Which values are addresses comes from the fixture's known behaviour, not from the values.
  • Aliases and shared writes. A global written through one RIP-relative operand is read through another and through a pointer obtained by LEA; the test writes through the address each side returned and both sides read it back; on one mapping, the original and the translation read each other's writes.
  • Bind controls, printed one line each: null base (1); another DLL (2); a copy with one altered byte in TimeDateStamp (2), in a read-only constant (3), in a jump-table entry in .text (3), each leaving the binding unchanged; a copy with one altered byte in writable data is accepted (0); the system loader's own mapping is accepted; after unbind and a bind to a third mapping, results follow the new mapping.
  • Translation controls: a target outside the image (load and LEA), outside every section, an access crossing the end of .data, a write to .rdata; four exports refused without the option, each with today's message; an export named bind_image rejected.
  • Unbound, in a child process: two functions raise 0xE0494D47 before bind and leave the caller's object unchanged; two that need no mapping run; after bind a read returns the initial value; after unbind the exception is raised again.
  • Existing tests: cpu_aot_jump_tables, cpu_aot_entry_order, cpu_aot_narrow_returns and cpu_aot_fourth_argument pass. Other cpu_* tests: same results as main. Under Wine, cpu_aot_import_call, cpu_aot_ordinal_import, cpu_native_reloc_free and cpu_native_tls fail on main too because Wine's loader differs from Windows, and cpu_native_exe_start times out intermittently (#281). The Windows job is the reference for those.
  • Corpus without the option, against main, with no allowed difference (rerun on the previous head 8ed1ca7, whose translator is identical): the same 642, 639 and 15 functions translate at /O2, /O1 and /Od, and all 1,296 have identical C++.
  • Run during development, not rerun by me:
    • with the option on for every corpus function, 32 more translate at /O2 and at /O1 and 7 at /Od (16, 16 and 7 not STL). This is an upper bound that assumes a correct mapping is bound. Of the 32 at /O2, 23 read a LEA result as data, 8 return one and 1 uses a RIP-relative operand;
    • a scratch differential driver loaded each original corpus DLL normally, bound the translation to that same module, restored the module's writable sections and a memory arena before each call, and compared return value, arena and writable sections: 32 of 32 at /O2, 32 of 32 at /O1 and 7 of 7 at /Od with no mismatch, on 32 or more inputs each. What it does not show: no corpus function changed image data on those inputs, restoration hides effects that build up across calls, and with one mapping the comparison by RVA is not exercised. It is evidence on a few inputs, not a proof of equivalence;
    • 19 manual mutants. 16 fail the test or the build (the RVA not added to the base; the identity check accepting any image; contents not checked; the unbound check removed; a write to a read-only section accepted; a LEA still treated as a guest address; a table base treated as data; DIR64 slots not excluded; and others).
  • pyflakes, tests/docs_links_check.py and tests/form_coverage_check.py passed.

Not run locally: Windows itself, the D3D12 tests and anything that needs Windows loader behaviour.

Evidence and follow-up

  • tests/cpu/image_data.asm is original MASM assembled in the build. No binaries were added. image_data is classified in tools/form_coverage.py.
  • Not exercised by any committed test: the read-access check and the write-to-executable check (the fixture has no such section), and the exclusion of the import address table from the content hash (the fixture has no imports; the scratch corpus driver does depend on it).
  • Not measured: whether the Windows loader writes read-only bytes other than relocations and the import table. If it does, for example for control-flow-guard slots, bind would refuse such an image, which fails safe.
  • Points where I interpreted #317, for your review: separate mappings are both made by pe_map, because it gives a fresh mapping on demand under Windows and Wine alike and LoadLibrary of one path twice returns the same module; liveness of the supplied base cannot be checked and is the host's responsibility; the defined failure is a structured exception.
  • Limits of the corpus numbers: 831 of the 1,119 functions are MSVC STL instantiations, and this is desktop code built from this repository, not a title.
  • #321 and #343 add lines next to this PR's in CMakeLists.txt, docs/experiments.md, tools/form_coverage.py and the CLI of the translator. I will rebase as they land.
  • Jump-table translator notes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6536597318 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/416). Originally posted by [bosseurdinternet](https://github.com/bosseurdinternet) on 2026-11-22T01:01:26Z. --- ## Change Refs #317 (RIP-relative data). Builds on #285 and #309 (both merged), and is independent of #321 and #343. In `tools/cpu_switch_aot.py` a RIP-relative memory operand is refused, and the result of a RIP-relative `LEA` is a guest address that may only feed a jump table. A function that reads or writes a global, stores the address of a table or returns the address of a static object is therefore not translated. This adds an opt-in mode in which such data is addressed in a live mapping of the input image that the host supplies. - **Opt-in, per run.** `--image-data` on the CLI, `image_data=True` on `lift`, `translate_pe` and `translate`, `IMAGE_DATA` in `cpu_translation()`. Without it, the same functions translate, their generated C++ is identical to main's and the refusal messages are unchanged. Translated signatures are the same in both modes. - **Supplying the mapping.** The generated DLL exports `int lifted_bind_image(word base)` and `void lifted_unbind_image()`. Bind accepts the base of a live mapping of the input image, made by Windows or by `pe_map`, and returns 0 when accepted, 1 for a null base, 2 when the headers differ and 3 when read-only contents differ. A refused bind keeps the current binding. - **Matching the input image.** The translator computes, from the file, a hash of header fields (DOS and PE signatures, the file header with `TimeDateStamp`, `SizeOfImage`, `CheckSum`, the section table; `ImageBase` excluded because loaders rewrite it) and a hash of the file-backed bytes of readable, non-writable sections, excluding DIR64 relocation slots and the import address table. Bind recomputes both on the mapping; the contents are read only after the headers match. The hash is FNV-1a: it detects a different or altered image, not a crafted one. Writable data is not part of the identity. - **Checked RVA.** At translation time every RIP-relative access must lie, for its whole width, in one section's virtual extent; a read needs read access and a write needs a writable, non-executable section. A `LEA` performs no access and is checked against the image extent only, since MSVC's `lea r, __ImageBase` targets the headers. Each refusal has a named message. - **Translation.** A RIP-relative operand becomes a load or store at base + RVA through the existing helpers. A RIP-relative `LEA` yields base + RVA, a fully defined value that can be stored, compared, used as a base and returned. A `LEA` that loads a recognised jump-table base keeps today's treatment and needs no mapping. - **Unbound use.** A function that addresses image data reads the base once at entry and, when nothing is bound, raises structured exception `0xE0494D47` as noncontinuable before any access. I chose this over `std::abort()` because a Debug CRT abort can show a dialog; that reason is from documented CRT behaviour, not measured here. - **Lifetime.** Documented in the generated source and in `docs/experiments.md`: an address a translated function returns or stores is valid only while that mapping stays bound and mapped; bind before use, with no concurrent rebind. This is stated, not enforced. ## Verification Local runs used the same compiler build as CI (MSVC 14.52.37025: `cl`, `link`, `ml64`) under Wine 11.0 on Linux, at `1ad9c0c` on main `e5e0079`. I reran the items below on this head unless a line says otherwise. - **`ctest --no-tests=error -R '^cpu_aot_image_data$' -V`: passed.** The original runs in one `pe_map` mapping of the fixture and the translation is bound to a second mapping of the same file, each starting from the image's initial data. After each of 29 calls the test compares return values, the guarded trace, RBX and RSI, and the writable data of the two mappings (1,048 bytes). - **Addresses compared by RVA.** Returned addresses, the pointer stored into the caller's object, and the relocated pointer in writable data are compared as `address - base` between the two mappings. Which values are addresses comes from the fixture's known behaviour, not from the values. - **Aliases and shared writes.** A global written through one RIP-relative operand is read through another and through a pointer obtained by `LEA`; the test writes through the address each side returned and both sides read it back; on one mapping, the original and the translation read each other's writes. - **Bind controls, printed one line each:** null base (1); another DLL (2); a copy with one altered byte in `TimeDateStamp` (2), in a read-only constant (3), in a jump-table entry in `.text` (3), each leaving the binding unchanged; a copy with one altered byte in writable data is accepted (0); the system loader's own mapping is accepted; after unbind and a bind to a third mapping, results follow the new mapping. - **Translation controls:** a target outside the image (load and `LEA`), outside every section, an access crossing the end of `.data`, a write to `.rdata`; four exports refused without the option, each with today's message; an export named `bind_image` rejected. - **Unbound, in a child process:** two functions raise `0xE0494D47` before bind and leave the caller's object unchanged; two that need no mapping run; after bind a read returns the initial value; after unbind the exception is raised again. - **Existing tests:** `cpu_aot_jump_tables`, `cpu_aot_entry_order`, `cpu_aot_narrow_returns` and `cpu_aot_fourth_argument` pass. Other `cpu_*` tests: same results as main. Under Wine, `cpu_aot_import_call`, `cpu_aot_ordinal_import`, `cpu_native_reloc_free` and `cpu_native_tls` fail on main too because Wine's loader differs from Windows, and `cpu_native_exe_start` times out intermittently (#281). The Windows job is the reference for those. - **Corpus without the option, against main, with no allowed difference** (rerun on the previous head `8ed1ca7`, whose translator is identical): the same 642, 639 and 15 functions translate at `/O2`, `/O1` and `/Od`, and all 1,296 have identical C++. - **Run during development, not rerun by me:** - with the option on for every corpus function, 32 more translate at `/O2` and at `/O1` and 7 at `/Od` (16, 16 and 7 not STL). This is an upper bound that assumes a correct mapping is bound. Of the 32 at `/O2`, 23 read a `LEA` result as data, 8 return one and 1 uses a RIP-relative operand; - a scratch differential driver loaded each original corpus DLL normally, bound the translation to that same module, restored the module's writable sections and a memory arena before each call, and compared return value, arena and writable sections: 32 of 32 at `/O2`, 32 of 32 at `/O1` and 7 of 7 at `/Od` with no mismatch, on 32 or more inputs each. What it does not show: no corpus function changed image data on those inputs, restoration hides effects that build up across calls, and with one mapping the comparison by RVA is not exercised. It is evidence on a few inputs, not a proof of equivalence; - 19 manual mutants. 16 fail the test or the build (the RVA not added to the base; the identity check accepting any image; contents not checked; the unbound check removed; a write to a read-only section accepted; a `LEA` still treated as a guest address; a table base treated as data; DIR64 slots not excluded; and others). - `pyflakes`, `tests/docs_links_check.py` and `tests/form_coverage_check.py` passed. Not run locally: Windows itself, the D3D12 tests and anything that needs Windows loader behaviour. ## Evidence and follow-up - `tests/cpu/image_data.asm` is original MASM assembled in the build. No binaries were added. `image_data` is classified in `tools/form_coverage.py`. - **Not exercised by any committed test:** the read-access check and the write-to-executable check (the fixture has no such section), and the exclusion of the import address table from the content hash (the fixture has no imports; the scratch corpus driver does depend on it). - **Not measured:** whether the Windows loader writes read-only bytes other than relocations and the import table. If it does, for example for control-flow-guard slots, bind would refuse such an image, which fails safe. - **Points where I interpreted #317, for your review:** separate mappings are both made by `pe_map`, because it gives a fresh mapping on demand under Windows and Wine alike and `LoadLibrary` of one path twice returns the same module; liveness of the supplied base cannot be checked and is the host's responsibility; the defined failure is a structured exception. - Limits of the corpus numbers: 831 of the 1,119 functions are MSVC STL instantiations, and this is desktop code built from this repository, not a title. - #321 and #343 add lines next to this PR's in `CMakeLists.txt`, `docs/experiments.md`, `tools/form_coverage.py` and the CLI of the translator. I will rebase as they land. - [Jump-table translator notes](https://github.com/R0Y00/Xbox2Windows/blob/main/docs/experiments.md#switch-dispatch-through-jump-tables) _AI-assisted (Claude Code)._ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-24 06:42:56 +00:00
R0Y00 commented 2026-11-24 06:48:24 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T04:19:13Z.


I integrated your image binding into the current Program/call-graph translator and retained the fixed-frame and bound-range paths. The image-data, direct-call, fixed-frame and bound-callee Windows CTests all pass together locally. I am adding a separate end-to-end EXE fixture that uses this binding in a nested leaf. Thank you for building the mapping and differential checks.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7074107718 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/416#issuecomment-7074107718). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-22T04:19:13Z. --- I integrated your image binding into the current Program/call-graph translator and retained the fixed-frame and bound-range paths. The image-data, direct-call, fixed-frame and bound-callee Windows CTests all pass together locally. I am adding a separate end-to-end EXE fixture that uses this binding in a nested leaf. Thank you for building the mapping and differential checks.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!410
No description provided.