tooling: apply memory patches in exe_aot (Refs #3037) #3877

Open
github-bot wants to merge 1 commit from github-bot/gh-3875 into main AGit
Owner

@⁠Ammmae0x on GitHub · 2026-12-02 16:59 UTC · original
Ammmae0x:tooling/exe-aot-memory-patches → main · +518 −13 · 8 files · 1 commits · open

Refs #3037 — Stage 2: memory patches in the CPU converter.

What landed (single commit, off origin/main 8121ca99, pushed to the fork, no force-push):

  • tools/patch_manifest.py: new apply_memory(data, patches). Every patch is validated before anything is modified: kind must be memory, each span [rva, rva + len(bytes)) must lie strictly inside a single PE section (PE headers, section gaps and virtual bytes beyond SizeOfRawData / Misc_VirtualSize refused, mapped with pefile.PE(data=bytes(data), fast_load=True)), and the SHA-256 of the original bytes at that section file slice must equal expect_sha256 (refused naming the patch id). Returns patched bytes; the input bytes are untouched.
  • tools/cpu_stdio_exe_aot.py: translate(..., data=None) takes optional in-memory bytes, defaulting to Path(image).read_bytes(); existing callers unchanged.
  • tools/exe_aot.py: --patches PATH and patches=None on convert(). Reads the input once, report["sha256"] is the original bytes; with --patches it runs load_data + apply_memory before the header stage and lifts from the patched copy (read_header(working), translate(..., data=working)). Any ValueError / OSError / pefile.PEFormatError becomes Refusal("patches", ...) (stale artifacts cleaned, exit 1, refused at patches: ...). report.json records manifest_sha256 and patch_ids when --patches is given.
  • tests/exe_aot_patches_check.py + tests/cmake/exe_aot_patches.cmake + tests/checks/python-linux/exe_aot_patches.txt + docs/experiments/exe-aot-patches.md (and a two-line pointer update in docs/experiments/patch-manifests.md).

Fixture: synthetic PE32+ whose entry is mov eax, 1; ret in .text (.pdata-registered, no imports). A one-byte patch at the immediate's RVA turns 1 into 42: conversion exits 0, report.json records the original sha256, manifest_sha256 and patch_ids ["p001"], the input file on disk is byte-identical, and the lifted C++ contains 0x2aull. Where cl is present the check also builds and runs the converted EXE, asserting exit code 42. Negative controls (wrong expect_sha256, wrong input_sha256, second patch wrong = no partial apply, PE-header / out-of-section / cross-section / unbacked-slack RVAs, non-memory shader patch, missing manifest) each exit 1 with stage patches and leave no _aot.cpp / _aot.exe. Mutation controls: removing the expect_sha256 gate or the section-bounds check makes the fixture accept a bad patch.

Linux verification (Debian 13, Python 3.12, pefile 2024.8.26, targeted checks only; GitHub Actions runs the full suite on push):

  • tests/exe_aot_patches_check.py: 10 tests, all passed
  • tests/patch_manifest_check.py: 9 tests, all passed
  • tests/cpu_exe_aot_cli_check.py: 9 tests, all passed
  • tests/cpu_stdio_exe_aot_check.py: 10 tests, all passed
  • tests/form_coverage_check.py: passed
  • tests/docs_links_check.py: passed (252 Markdown files, 960 links)
  • tests/bidi_controls_check.py: passed (2989 files, no bidirectional controls)
  • git diff --check origin/main...HEAD: clean; git merge-tree --write-tree origin/main HEAD: exits 0

No cl on this Linux host, so the build-and-run leg of the positive case did not execute here; Windows verification is the arbiter.

Windows Verification (59f56aca)

  • Clean git merge-tree --write-tree upstream/main HEAD (bdf85910) and git diff --check.
  • MSVC dev (cmake --preset dev, built cpu_crc_line, cpu_crc_castagnoli, cpu_exe_original, cpu_exe_io): ctest --preset dev --no-tests=error -R '^(exe_aot_patches|patch_manifest_controls|cpu_exe_aot|cpu_stdio_exe_aot)' --output-on-failure passed 5/5 (18.23s), including exe_aot_patches (10 tests, 4.78s, with cl-compiled patched EXE exiting 42).
  • MSVC release (cmake --preset release, built same targets): ctest --preset release --no-tests=error -R '^(exe_aot_patches|patch_manifest_controls|cpu_exe_aot|cpu_stdio_exe_aot)' --output-on-failure passed 5/5 (18.49s), including exe_aot_patches (4.96s).
  • Repo static checks on Windows: py_compile, tests/docs_links_check.py (252 files, 960 links), and tests/bidi_controls_check.py (2991 files) passed.

AI-assisted.

**@⁠Ammmae0x** on GitHub · 2026-12-02 16:59 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3875) `Ammmae0x:tooling/exe-aot-memory-patches` → `main` · +518 −13 · 8 files · 1 commits · open Refs #3037 — Stage 2: memory patches in the CPU converter. **What landed** (single commit, off `origin/main` `8121ca99`, pushed to the fork, no force-push): - `tools/patch_manifest.py`: new `apply_memory(data, patches)`. Every patch is validated before anything is modified: kind must be `memory`, each span `[rva, rva + len(bytes))` must lie strictly inside a single PE section (PE headers, section gaps and virtual bytes beyond `SizeOfRawData` / `Misc_VirtualSize` refused, mapped with `pefile.PE(data=bytes(data), fast_load=True)`), and the SHA-256 of the original bytes at that section file slice must equal `expect_sha256` (refused naming the patch `id`). Returns patched `bytes`; the input bytes are untouched. - `tools/cpu_stdio_exe_aot.py`: `translate(..., data=None)` takes optional in-memory bytes, defaulting to `Path(image).read_bytes()`; existing callers unchanged. - `tools/exe_aot.py`: `--patches PATH` and `patches=None` on `convert()`. Reads the input once, `report["sha256"]` is the original bytes; with `--patches` it runs `load_data` + `apply_memory` before the header stage and lifts from the patched copy (`read_header(working)`, `translate(..., data=working)`). Any `ValueError` / `OSError` / `pefile.PEFormatError` becomes `Refusal("patches", ...)` (stale artifacts cleaned, exit 1, `refused at patches: ...`). `report.json` records `manifest_sha256` and `patch_ids` when `--patches` is given. - `tests/exe_aot_patches_check.py` + `tests/cmake/exe_aot_patches.cmake` + `tests/checks/python-linux/exe_aot_patches.txt` + `docs/experiments/exe-aot-patches.md` (and a two-line pointer update in `docs/experiments/patch-manifests.md`). **Fixture:** synthetic PE32+ whose entry is `mov eax, 1; ret` in `.text` (`.pdata`-registered, no imports). A one-byte patch at the immediate's RVA turns 1 into 42: conversion exits 0, `report.json` records the original `sha256`, `manifest_sha256` and `patch_ids ["p001"]`, the input file on disk is byte-identical, and the lifted C++ contains `0x2aull`. Where `cl` is present the check also builds and runs the converted EXE, asserting exit code 42. Negative controls (wrong `expect_sha256`, wrong `input_sha256`, second patch wrong = no partial apply, PE-header / out-of-section / cross-section / unbacked-slack RVAs, non-`memory` shader patch, missing manifest) each exit 1 with stage `patches` and leave no `_aot.cpp` / `_aot.exe`. Mutation controls: removing the `expect_sha256` gate or the section-bounds check makes the fixture accept a bad patch. **Linux verification** (Debian 13, Python 3.12, pefile 2024.8.26, targeted checks only; GitHub Actions runs the full suite on push): - `tests/exe_aot_patches_check.py`: 10 tests, all passed - `tests/patch_manifest_check.py`: 9 tests, all passed - `tests/cpu_exe_aot_cli_check.py`: 9 tests, all passed - `tests/cpu_stdio_exe_aot_check.py`: 10 tests, all passed - `tests/form_coverage_check.py`: passed - `tests/docs_links_check.py`: passed (252 Markdown files, 960 links) - `tests/bidi_controls_check.py`: passed (2989 files, no bidirectional controls) - `git diff --check origin/main...HEAD`: clean; `git merge-tree --write-tree origin/main HEAD`: exits 0 No `cl` on this Linux host, so the build-and-run leg of the positive case did not execute here; Windows verification is the arbiter. ## Windows Verification (`59f56aca`) - Clean `git merge-tree --write-tree upstream/main HEAD` (`bdf85910`) and `git diff --check`. - MSVC `dev` (`cmake --preset dev`, built `cpu_crc_line`, `cpu_crc_castagnoli`, `cpu_exe_original`, `cpu_exe_io`): `ctest --preset dev --no-tests=error -R '^(exe_aot_patches|patch_manifest_controls|cpu_exe_aot|cpu_stdio_exe_aot)' --output-on-failure` passed `5/5` (`18.23s`), including `exe_aot_patches` (`10 tests`, `4.78s`, with `cl`-compiled patched EXE exiting `42`). - MSVC `release` (`cmake --preset release`, built same targets): `ctest --preset release --no-tests=error -R '^(exe_aot_patches|patch_manifest_controls|cpu_exe_aot|cpu_stdio_exe_aot)' --output-on-failure` passed `5/5` (`18.49s`), including `exe_aot_patches` (`4.96s`). - Repo static checks on Windows: `py_compile`, `tests/docs_links_check.py` (252 files, 960 links), and `tests/bidi_controls_check.py` (2991 files) passed. AI-assisted. <!-- WIN-VERIFY: passed | sha=59f56aca411a118464fa255fd97f3ea87bf0d695 | ctest=^(exe_aot_patches|patch_manifest_controls|cpu_exe_aot|cpu_stdio_exe_aot) | dev=5/5 | release=5/5 -->
Stage 2 of #3037: patch_manifest.apply_memory validates every memory
patch (kind, single-section span, expect_sha256) before touching a
byte; exe_aot --patches lifts from the patched in-memory copy and
records manifest_sha256 and patch_ids in report.json; cpu_stdio
translate accepts in-memory data. Synthetic PE fixture turns entry
return 1 into 42, with patches-stage refusals and mutation controls.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 19:11:20 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-12-02 19:35 UTC · original · requested changes

Independent Windows/MSVC Debug ctest --preset dev --no-tests=error -V -j 1 -R '^(exe_aot_patches|patch_manifest_controls|cpu_exe_aot|cpu_stdio_exe_aot)' passed 5/5. One validation gap remains. apply_memory treats a section-table match as sufficient proof that its raw file range is outside the headers. It also takes the first matching section without rejecting ambiguous ranges. Please verify the resolved file span independently of the RVA check, reject header-overlapping or ambiguous mappings, and add a source-built rejection control. This preserves the documented refusal for malformed inputs.

**@⁠R0Y00** on GitHub · 2026-12-02 19:35 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3875#pullrequestreview-6157562118) · **requested changes** Independent Windows/MSVC Debug `ctest --preset dev --no-tests=error -V -j 1 -R '^(exe_aot_patches|patch_manifest_controls|cpu_exe_aot|cpu_stdio_exe_aot)'` passed 5/5. One validation gap remains. `apply_memory` treats a section-table match as sufficient proof that its raw file range is outside the headers. It also takes the first matching section without rejecting ambiguous ranges. Please verify the resolved file span independently of the RVA check, reject header-overlapping or ambiguous mappings, and add a source-built rejection control. This preserves the documented refusal for malformed inputs.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3877/head:github-bot/gh-3875'
git switch 'github-bot/gh-3875'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3875'
git switch 'github-bot/gh-3875'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3875'
git switch 'github-bot/gh-3875'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3875'
git switch 'main'
git merge --squash 'github-bot/gh-3875'
git switch 'main'
git merge --ff-only 'github-bot/gh-3875'
git switch 'main'
git merge 'github-bot/gh-3875'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.