tooling: add a hash-gated patch manifest loader #3056

Manually merged
github merged 2 commits from github-bot/gh-3056 into gh-pr/3056/base 2026-11-27 13:32:55 +00:00 AGit
Owner

@⁠Not-Saii on GitHub · 2026-11-27 12:32 UTC · original
Not-Saii:tooling/patch-manifest-loader → main · +313 −0 · 5 files · 2 commits · merged 2026-11-27 13:26 UTC

Refs #3037 (item 1: schema, loader and hash gate). Items 2-4 stay open.

What changes

  • tools/patch_manifest.py reads a TOML manifest with the standard library's tomllib. It validates the whole manifest, then refuses it unless the input file's SHA-256 equals [target] input_sha256 (manifest is for another input). Nothing is returned before both pass, so there is no partial apply.
  • Every [[patch]] needs a unique id, a kind, and non-empty why and remove_when. Unknown and missing keys are refused at every level, so a misspelled key fails instead of being ignored.
  • Only kind = "memory" is defined: rva (integer below 2^32), expect_sha256 (hash of the original bytes, never the bytes) and bytes (lowercase hex). The fields are checked for shape only, and two memory patches may not overlap. Nothing applies them yet; that is item 2. hook and data are refused as not supported until their stages define them.
  • The loader returns manifest_sha256 (over the manifest file's bytes) and the patch ids, for item 4's run reports.
  • The format and commands are in docs/experiments/patch-manifests.md. The README is unchanged because no applier exists yet.

Fixture

tests/patch_manifest_check.py builds a synthetic PE32+ at run time with build_pe32_plus from tests/call_evidence_check.py. No binaries are committed. It is registered for Linux CI in tests/checks/python-linux/patch_manifest.txt and as CTest patch_manifest_controls.

  • Bound input: loads, and the CLI prints the manifest hash and p001.
  • Negative control: the same image with its last byte flipped makes the CLI exit with status 1 with manifest is for another input and empty stdout.
  • Malformed manifests: a misspelled key, kind hook, a missing why, a blank remove_when, a duplicate id, overlapping patches, odd-length bytes, a boolean rva, an upper-case input_sha256, no patches, and a TOML syntax error are each refused.
  • Mutations: with the hash comparison removed, the wrong-input test fails. With the overlap check removed, the overlap case fails.

Tested

Commit 500cc76f on main 232ae71e, Windows 11 build 26300, Python 3.14:

python -B tests/patch_manifest_check.py      # Ran 3 tests ... OK
python -B tests/docs_links_check.py          # Checked 71 Markdown files and 583 links.
python -B tests/bidi_controls_check.py       # no bidirectional controls
python -B tests/run_checks_parallel_check.py # ok

Not run locally on Linux; the python-linux job runs the check on Python 3.12. CTest registration was not exercised locally (no configure run).

🤖 Generated with Claude Code

**@⁠Not-Saii** on GitHub · 2026-11-27 12:32 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3056) `Not-Saii:tooling/patch-manifest-loader` → `main` · +313 −0 · 5 files · 2 commits · merged 2026-11-27 13:26 UTC Refs #3037 (item 1: schema, loader and hash gate). Items 2-4 stay open. ## What changes - `tools/patch_manifest.py` reads a TOML manifest with the standard library's `tomllib`. It validates the whole manifest, then refuses it unless the input file's SHA-256 equals `[target] input_sha256` (`manifest is for another input`). Nothing is returned before both pass, so there is no partial apply. - Every `[[patch]]` needs a unique `id`, a `kind`, and non-empty `why` and `remove_when`. Unknown and missing keys are refused at every level, so a misspelled key fails instead of being ignored. - Only `kind = "memory"` is defined: `rva` (integer below 2^32), `expect_sha256` (hash of the original bytes, never the bytes) and `bytes` (lowercase hex). The fields are checked for shape only, and two memory patches may not overlap. Nothing applies them yet; that is item 2. `hook` and `data` are refused as `not supported` until their stages define them. - The loader returns `manifest_sha256` (over the manifest file's bytes) and the patch ids, for item 4's run reports. - The format and commands are in `docs/experiments/patch-manifests.md`. The README is unchanged because no applier exists yet. ## Fixture `tests/patch_manifest_check.py` builds a synthetic PE32+ at run time with `build_pe32_plus` from `tests/call_evidence_check.py`. No binaries are committed. It is registered for Linux CI in `tests/checks/python-linux/patch_manifest.txt` and as CTest `patch_manifest_controls`. - **Bound input:** loads, and the CLI prints the manifest hash and `p001`. - **Negative control:** the same image with its last byte flipped makes the CLI exit with status 1 with `manifest is for another input` and empty stdout. - **Malformed manifests:** a misspelled key, kind `hook`, a missing `why`, a blank `remove_when`, a duplicate id, overlapping patches, odd-length `bytes`, a boolean `rva`, an upper-case `input_sha256`, no patches, and a TOML syntax error are each refused. - **Mutations:** with the hash comparison removed, the wrong-input test fails. With the overlap check removed, the overlap case fails. ## Tested Commit `500cc76f` on `main` `232ae71e`, Windows 11 build 26300, Python 3.14: ``` python -B tests/patch_manifest_check.py # Ran 3 tests ... OK python -B tests/docs_links_check.py # Checked 71 Markdown files and 583 links. python -B tests/bidi_controls_check.py # no bidirectional controls python -B tests/run_checks_parallel_check.py # ok ``` Not run locally on Linux; the `python-linux` job runs the check on Python 3.12. CTest registration was not exercised locally (no configure run). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
tools/patch_manifest.py reads a TOML patch manifest, validates every
entry and refuses it unless the input's SHA-256 matches [target]
input_sha256, so a manifest applies to one input or not at all. Each
patch needs id, kind, why and remove_when; unknown and missing keys are
refused. Only the memory kind is defined (shape checks, no applier yet);
hook and data are refused until their stages land.

Refs #3037

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 12:40:07 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-27 13:10 UTC · original · requested changes

I tested 500cc76f with synthetic TOML. A kind value of ["memory"] raises TypeError: unhashable type: 'list'; an inline table raises the equivalent dict error. The CLI catches ValueError and OSError, so both escape as tracebacks instead of the normal manifest diagnostic. Please validate that kind is a string before looking it up and add a wrong-type control.

The range check also accepts a two-byte replacement at 0xffffffff and hands the planner a span ending at 0x100000001. Since every byte of a memory patch must have a representable RVA, please validate the entire span. The one-byte case at 0xffffffff is the adjacent shape control and should remain valid at this stage; matching it to an actual mapped section belongs to the runtime stage.

**@⁠R0Y00** on GitHub · 2026-11-27 13:10 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3056#pullrequestreview-6088647457) · **requested changes** I tested `500cc76f` with synthetic TOML. A `kind` value of `["memory"]` raises `TypeError: unhashable type: 'list'`; an inline table raises the equivalent `dict` error. The CLI catches `ValueError` and `OSError`, so both escape as tracebacks instead of the normal manifest diagnostic. Please validate that `kind` is a string before looking it up and add a wrong-type control. The range check also accepts a two-byte replacement at `0xffffffff` and hands the planner a span ending at `0x100000001`. Since every byte of a memory patch must have a representable RVA, please validate the entire span. The one-byte case at `0xffffffff` is the adjacent shape control and should remain valid at this stage; matching it to an actual mapped section belongs to the runtime stage.
A list or inline-table kind raised TypeError on the KINDS lookup and
escaped the CLI as a traceback; kind must now be a string. The RVA range
check covered only the first byte, so a 2-byte patch at 0xffffffff
passed; the whole span must now end at or below 2^32. A 1-byte patch at
0xffffffff stays valid as the edge control.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
Owner

@⁠Not-Saii on GitHub · 2026-11-27 13:14 UTC · original

Thanks, both are fixed in ec7d1e51.

  • kind type: it must now be a string before it is looked up. A list or inline-table kind is refused as kind [...] is not supported. New controls cover both shapes in parse, plus one through the CLI that expects exit 1, the patch manifest: message and no traceback.
  • Span: after the bytes are decoded, the whole span must end at or below 2^32. A 2-byte patch at 0xffffffff is refused with span [0xffffffff, 0x100000001) does not fit in 32-bit RVAs. The 1-byte patch at 0xffffffff still loads and is kept as the edge control. Checking against mapped sections stays with the runtime stage.

Mutations, each run separately:

Change Result
String check on kind removed Both wrong-type cases raise TypeError, and the CLI case sees a traceback.
Span check removed The 2-byte case is accepted.
Span check off by one (>=) The 1-byte edge case is refused.

python -B tests/patch_manifest_check.py: 3 tests OK on Windows 11 build 26300, Python 3.14. docs_links_check and bidi_controls_check pass. docs/experiments/patch-manifests.md lists the new rules and controls.

**@⁠Not-Saii** on GitHub · 2026-11-27 13:14 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3056#issuecomment-7219232146) Thanks, both are fixed in `ec7d1e51`. - **`kind` type:** it must now be a string before it is looked up. A list or inline-table `kind` is refused as `kind [...] is not supported`. New controls cover both shapes in `parse`, plus one through the CLI that expects exit 1, the `patch manifest:` message and no traceback. - **Span:** after the bytes are decoded, the whole span must end at or below 2^32. A 2-byte patch at `0xffffffff` is refused with `span [0xffffffff, 0x100000001) does not fit in 32-bit RVAs`. The 1-byte patch at `0xffffffff` still loads and is kept as the edge control. Checking against mapped sections stays with the runtime stage. Mutations, each run separately: | Change | Result | |---|---| | String check on `kind` removed | Both wrong-type cases raise `TypeError`, and the CLI case sees a traceback. | | Span check removed | The 2-byte case is accepted. | | Span check off by one (`>=`) | The 1-byte edge case is refused. | `python -B tests/patch_manifest_check.py`: 3 tests OK on Windows 11 build 26300, Python 3.14. `docs_links_check` and `bidi_controls_check` pass. `docs/experiments/patch-manifests.md` lists the new rules and controls.
github-bot changed target branch from main to gh-pr/3056/base 2026-11-27 13:32:52 +00:00
github manually merged commit ec7d1e51e6 into gh-pr/3056/base 2026-11-27 13:32:55 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-27 13:26 UTC · original · approved these changes

Retested ec7d1e51 integrated with main db28a796 (tree 1dc1147760082db546e64a48c7b4e6702ac17a47). python -B tests/patch_manifest_check.py passed all three tests, including the wrong-input hash control, and the docs link check passed. The standalone CLI returned the manifest diagnostic with exit 1 for a list kind, an inline-table kind, and a two-byte patch at 0xffffffff, with no traceback. A one-byte patch at 0xffffffff returned the expected plan. Both review findings are addressed.

**@⁠R0Y00** on GitHub · 2026-11-27 13:26 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3056#pullrequestreview-6088858409) · **approved these changes** Retested `ec7d1e51` integrated with `main` `db28a796` (tree `1dc1147760082db546e64a48c7b4e6702ac17a47`). `python -B tests/patch_manifest_check.py` passed all three tests, including the wrong-input hash control, and the docs link check passed. The standalone CLI returned the manifest diagnostic with exit 1 for a list kind, an inline-table kind, and a two-byte patch at `0xffffffff`, with no traceback. A one-byte patch at `0xffffffff` returned the expected plan. Both review findings are addressed.
This discussion has been locked. Commenting is limited to contributors.
No description provided.