cpu: lower RDRAND and RDSEED in an opt-in host-entropy profile #801

Merged
legacy-code-archaeologist merged 1 commit from upstream-pr/807 into main 2026-11-23 06:41:23 +00:00
legacy-code-archaeologist commented 2026-11-23 06:17:32 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-23T06:17:32Z.


Change

Lowers RDRAND and RDSEED in tools/ffx_version_query.py as the opt-in host-entropy profile agreed in #795. The profile is enabled by lower(..., host_entropy=True); the default profile still rejects both instructions.

  • Each guest execution runs the host instruction once, through _rdrand16_step … _rdseed64_step. There is no retry and no fence, so guest code keeps control of retrying.
  • CF is the intrinsic's result. On CF=0 the destination gets zero at its width.
  • A 16-bit destination keeps bits 16–63, and a 32-bit destination clears the upper half.
  • ZF and OF are cleared. PF is cleared only in a checked x87 function whose body reads it. SF and AF are not modelled by the shared lowering. This is the documented exception to "cleared flags"; a native check shows what the hardware does.
  • The result is a volatile local, so a read stays in the code when the guest discards it. A probe before this change showed that MSVC /O2 removes such an _rdrand32_step or _rdseed64_step without volatile, and the plain control below repeats that inside the test.
  • Preconditions (documented in docs/experiments.md): RDRAND needs CPUID.01H:ECX bit 30, RDSEED needs CPUID.(EAX=07H,ECX=0):EBX bit 18.

Tests

cpu_aot_rdrand (rdrand.asm) and cpu_aot_rdseed (rdseed.asm) have separate fixtures. Each exits 77 only when its own CPUID bit is clear, before any native read runs. A native harness (rdrand_harness.asm) is never translated and has no coverage header.

Each check:

  1. Confirms on the host that OF, SF, ZF, AF and PF are 0 after the 64-bit instruction. Two add presets set these flags beforehand.

  2. Confirms that the default profile rejects every read.

  3. Runs 2,000 rounds per width, natively and translated. Four add r8, r8 presets set ZF, CF and OF before each read. Every read must be either:

    • a success: CF 1, ZF and OF clear, width rules hold; or
    • a failure: CF 0, zero at the width, ZF and OF clear.

    Natural failures are counted and printed, never required.

  4. Checks variation: at each width at least two distinct successful values, and one 64-bit success with a nonzero upper half. This catches a constant translation; it does not assess entropy quality.

  5. Requires that the reads whose result is discarded return the overwritten value.

Deterministic variants (must pass on every host):

  • succeed: the host read is replaced by CF 1 with a fixed pattern.
  • fail: the host read stores the pattern and reports CF 0, so the zero and carry rules are checked even on a host that never fails.

Controls (must each break a rule):

  • constant: a fixed successful value.
  • nocarry: CF dropped.
  • noext: the 32-bit form keeps the upper half.
  • wide16: the 16-bit form replaces the upper bits.
  • flags: ZF and OF stay as preset.
  • nozero: the pattern survives a failed read.

Every control regex is anchored on the rnd / rnd_ok locals of the new statement.

/O2: the translation is also built with cl /O2. A walk that follows every branch must find one RDRAND or RDSEED per export, including the discarded one, and the rules must still hold. The plain control removes volatile and is built only at /O2; there the walk must report the discarded read missing.

A host with fewer than two natural successes at a width still runs every deterministic check. It then exits 77, naming variation as not measurable.

Verification

At a0f28fa (on main 186711d) on Windows 11 Pro (build 26100), Intel Core i7-6820HQ (both CPUID bits set), MSVC 19.52, Python 3.12:

  • ctest --preset dev --no-tests=error -R '^cpu_aot_rd(rand|seed)$' -V: both pass. Natural failures were 0 at every width for both instructions. Every control reports its broken rule, for example:
    • control nozero: rdrand_16 preset 0x0: failed read left 0x5a5a, expected 0 at 16 bits
    • cpu_rdseed_plain_o2: … {'rdseed_16': 1, 'rdseed_32': 1, 'rdseed_64': 1, 'rdseed_unused': 0}
  • Simulated hosts without either CPUID bit (has_* forced to 0; every native and lifted call raises): each part exits 77 before any read.
  • A translation that always fails (the fail DLL in place of lifted): the deterministic checks pass, and the test exits 77 because variation is not measurable.
  • ctest --preset dev --no-tests=error -R '^cpu_' -j 4: 144 of 145 pass, with three capability skips (cpu_aot_fences_hints_clwb, cpu_aot_crypto_sha, cpu_shim_chain_hardware).
    • The failure is cpu_shim_shadow_hardware in its native run: D3D12XboxCreateDevice failed (HRESULT 0x887a0004). That run doesn't use this lowering.
    • On this laptop the high-performance adapter (Quadro M1000M) creates a D3D12 device at feature level 11_0 but not 12_0, which src/d3d12_xs_shim.cpp requests. I'll report it separately.
  • tools/form_coverage.py on that JUnit run no longer lists rdrand or rdseed as gaps.
  • tests/docs_links_check.py, tests/bidi_controls_check.py and tests/form_coverage_check.py pass.

No README row lists individual lowered instructions (as for RDTSC in #789), so the README is unchanged.

Limits

  • Not run on AMD, on a host without either instruction (only simulated), or under a hypervisor that filters them.
  • Natural failures did not occur here, so the failure path was checked only through the deterministic fail variant.

Refs #795

  • I did not include game files, dumps, distribution links or secrets.

AI-assisted: written with Claude Code; design reviewed in three rounds and the diff in a precommit review by fresh agents; checked and submitted by the account owner.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6553538513 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/807). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-23T06:17:32Z. --- ## Change Lowers RDRAND and RDSEED in `tools/ffx_version_query.py` as the opt-in host-entropy profile agreed in #795. The profile is enabled by `lower(..., host_entropy=True)`; the default profile still rejects both instructions. - Each guest execution runs the host instruction once, through `_rdrand16_step` … `_rdseed64_step`. There is no retry and no fence, so guest code keeps control of retrying. - CF is the intrinsic's result. On CF=0 the destination gets zero at its width. - A 16-bit destination keeps bits 16–63, and a 32-bit destination clears the upper half. - ZF and OF are cleared. PF is cleared only in a checked x87 function whose body reads it. SF and AF are not modelled by the shared lowering. This is the documented exception to "cleared flags"; a native check shows what the hardware does. - The result is a `volatile` local, so a read stays in the code when the guest discards it. A probe before this change showed that MSVC `/O2` removes such an `_rdrand32_step` or `_rdseed64_step` without `volatile`, and the `plain` control below repeats that inside the test. - Preconditions (documented in `docs/experiments.md`): RDRAND needs CPUID.01H:ECX bit 30, RDSEED needs CPUID.(EAX=07H,ECX=0):EBX bit 18. ## Tests `cpu_aot_rdrand` ([`rdrand.asm`](tests/cpu/rdrand.asm)) and `cpu_aot_rdseed` ([`rdseed.asm`](tests/cpu/rdseed.asm)) have separate fixtures. Each exits 77 only when its own CPUID bit is clear, before any native read runs. A native harness ([`rdrand_harness.asm`](tests/cpu/rdrand_harness.asm)) is never translated and has no coverage header. Each check: 1. Confirms on the host that OF, SF, ZF, AF and PF are 0 after the 64-bit instruction. Two `add` presets set these flags beforehand. 2. Confirms that the default profile rejects every read. 3. Runs 2,000 rounds per width, natively and translated. Four `add r8, r8` presets set ZF, CF and OF before each read. Every read must be either: - a success: CF 1, ZF and OF clear, width rules hold; or - a failure: CF 0, zero at the width, ZF and OF clear. Natural failures are counted and printed, never required. 4. Checks variation: at each width at least two distinct successful values, and one 64-bit success with a nonzero upper half. This catches a constant translation; it does not assess entropy quality. 5. Requires that the reads whose result is discarded return the overwritten value. **Deterministic variants (must pass on every host):** - `succeed`: the host read is replaced by CF 1 with a fixed pattern. - `fail`: the host read stores the pattern and reports CF 0, so the zero and carry rules are checked even on a host that never fails. **Controls (must each break a rule):** - `constant`: a fixed successful value. - `nocarry`: CF dropped. - `noext`: the 32-bit form keeps the upper half. - `wide16`: the 16-bit form replaces the upper bits. - `flags`: ZF and OF stay as preset. - `nozero`: the pattern survives a failed read. Every control regex is anchored on the `rnd` / `rnd_ok` locals of the new statement. **`/O2`:** the translation is also built with `cl /O2`. A walk that follows every branch must find one RDRAND or RDSEED per export, including the discarded one, and the rules must still hold. The `plain` control removes `volatile` and is built only at `/O2`; there the walk must report the discarded read missing. A host with fewer than two natural successes at a width still runs every deterministic check. It then exits 77, naming variation as not measurable. ## Verification At `a0f28fa` (on `main` `186711d`) on Windows 11 Pro (build 26100), Intel Core i7-6820HQ (both CPUID bits set), MSVC 19.52, Python 3.12: - `ctest --preset dev --no-tests=error -R '^cpu_aot_rd(rand|seed)$' -V`: both pass. Natural failures were 0 at every width for both instructions. Every control reports its broken rule, for example: - `control nozero: rdrand_16 preset 0x0: failed read left 0x5a5a, expected 0 at 16 bits` - `cpu_rdseed_plain_o2: … {'rdseed_16': 1, 'rdseed_32': 1, 'rdseed_64': 1, 'rdseed_unused': 0}` - Simulated hosts without either CPUID bit (`has_*` forced to 0; every native and lifted call raises): each part exits 77 before any read. - A translation that always fails (the `fail` DLL in place of `lifted`): the deterministic checks pass, and the test exits 77 because variation is not measurable. - `ctest --preset dev --no-tests=error -R '^cpu_' -j 4`: 144 of 145 pass, with three capability skips (`cpu_aot_fences_hints_clwb`, `cpu_aot_crypto_sha`, `cpu_shim_chain_hardware`). - The failure is `cpu_shim_shadow_hardware` in its **native** run: `D3D12XboxCreateDevice failed (HRESULT 0x887a0004)`. That run doesn't use this lowering. - On this laptop the high-performance adapter (Quadro M1000M) creates a D3D12 device at feature level 11_0 but not 12_0, which `src/d3d12_xs_shim.cpp` requests. I'll report it separately. - `tools/form_coverage.py` on that JUnit run no longer lists `rdrand` or `rdseed` as gaps. - `tests/docs_links_check.py`, `tests/bidi_controls_check.py` and `tests/form_coverage_check.py` pass. No README row lists individual lowered instructions (as for RDTSC in #789), so the README is unchanged. ## Limits - Not run on AMD, on a host without either instruction (only simulated), or under a hypervisor that filters them. - Natural failures did not occur here, so the failure path was checked only through the deterministic `fail` variant. Refs #795 - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted: written with Claude Code; design reviewed in three rounds and the diff in a precommit review by fresh agents; checked and submitted by the account owner. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-24 06:44:53 +00:00
MatyEjsr commented 2026-11-24 06:49:03 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by MatyEjsr on 2026-11-23T06:41:16Z.


Verdict: Approve (static/ISA and Python review of head a0f28faedec8a4fe1b60bd935e42e3e4827d6c01, compared against main ab5d3a5).

Verification

Environment: Windows 11 x64, Python 3.12.10. Zero-GPU static/ISA and Python review (native MSVC DLL build/execution was not run locally; Windows dev and release /O2 CI jobs on this head both passed).

Checks on a0f28fa:

  • python -B tests/docs_links_check.py (13 files, 154 links) and python -B tests/bidi_controls_check.py (654 files): both passed.
  • git merge-tree --write-tree main a0f28fa: clean against ab5d3a5 (exit 0).
  • Audited tools/ffx_version_query.py, tools/form_coverage.py, CMakeLists.txt, docs/experiments.md, tests/cpu/rdrand.asm, tests/cpu/rdseed.asm, tests/cpu/rdrand_harness.asm, and tests/cpu/rdrand.py:
    • Default profile still rejects RDRAND and RDSEED; lowering is gated behind host_entropy=True.
    • _rdrand{16,32,64}_step / _rdseed{16,32,64}_step match uint16_t / uint32_t / uint64_t widths; if (!rnd_ok) rnd = 0; guarantees destination zeroing on failure; write_register() preserves bits 16–63 on 16-bit writes and clears bits 32–63 on 32-bit writes; const volatile int rnd_ok keeps discarded reads alive at /O2 (verified by the plain control).
    • rdrand_harness.asm properly saves/restores rbx around cpuid, checks cpuid(0).eax >= 7 with ecx = 0 before leaf 7, has no ; coverage: header, and is registered as None in tools/form_coverage.py.
    • CMakeLists.txt sets SKIP_RETURN_CODE 77 on both cpu_aot_rdrand and cpu_aot_rdseed, and docs/experiments.md accurately states the CPUID preconditions and the unmodelled SF/AF exception.
    • Fixtures are source-owned MASM with deterministic succeed/fail variants and targeted mutating negative controls (constant, nocarry, noext, wide16, flags, nozero, plain).

Non-blocking notes:

  • Cross-PR coordination: if #805 lands (which adds sign to lower()), RDRAND/RDSEED will need sign = false; so SF is cleared alongside ZF/OF.
  • In tests/cpu/rdrand.py:252, the /O2 lifted check checks variation(part, values)[0] but ignores the second return value (ok); propagating measurable &= ok there would match lines 232–235 on a host with < 2 natural successes during the /O2 pass.

AI-assisted review (static code/ISA inspection and local Python checks executed locally).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7093577883 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/807#issuecomment-7093577883). Originally posted by [MatyEjsr](https://github.com/MatyEjsr) on 2026-11-23T06:41:16Z. --- **Verdict: Approve** (static/ISA and Python review of head `a0f28faedec8a4fe1b60bd935e42e3e4827d6c01`, compared against `main` `ab5d3a5`). ### Verification Environment: Windows 11 x64, Python 3.12.10. Zero-GPU static/ISA and Python review (native MSVC DLL build/execution was not run locally; Windows `dev` and `release` `/O2` CI jobs on this head both passed). Checks on `a0f28fa`: - `python -B tests/docs_links_check.py` (13 files, 154 links) and `python -B tests/bidi_controls_check.py` (654 files): both passed. - `git merge-tree --write-tree main a0f28fa`: clean against `ab5d3a5` (exit 0). - Audited `tools/ffx_version_query.py`, `tools/form_coverage.py`, `CMakeLists.txt`, `docs/experiments.md`, `tests/cpu/rdrand.asm`, `tests/cpu/rdseed.asm`, `tests/cpu/rdrand_harness.asm`, and `tests/cpu/rdrand.py`: - Default profile still rejects `RDRAND` and `RDSEED`; lowering is gated behind `host_entropy=True`. - `_rdrand{16,32,64}_step` / `_rdseed{16,32,64}_step` match `uint16_t` / `uint32_t` / `uint64_t` widths; `if (!rnd_ok) rnd = 0;` guarantees destination zeroing on failure; `write_register()` preserves bits 16–63 on 16-bit writes and clears bits 32–63 on 32-bit writes; `const volatile int rnd_ok` keeps discarded reads alive at `/O2` (verified by the `plain` control). - `rdrand_harness.asm` properly saves/restores `rbx` around `cpuid`, checks `cpuid(0).eax >= 7` with `ecx = 0` before leaf 7, has no `; coverage:` header, and is registered as `None` in `tools/form_coverage.py`. - `CMakeLists.txt` sets `SKIP_RETURN_CODE 77` on both `cpu_aot_rdrand` and `cpu_aot_rdseed`, and `docs/experiments.md` accurately states the CPUID preconditions and the unmodelled SF/AF exception. - Fixtures are source-owned MASM with deterministic `succeed`/`fail` variants and targeted mutating negative controls (`constant`, `nocarry`, `noext`, `wide16`, `flags`, `nozero`, `plain`). Non-blocking notes: - Cross-PR coordination: if #805 lands (which adds `sign` to `lower()`), `RDRAND`/`RDSEED` will need `sign = false;` so `SF` is cleared alongside `ZF`/`OF`. - In `tests/cpu/rdrand.py:252`, the `/O2` lifted check checks `variation(part, values)[0]` but ignores the second return value (`ok`); propagating `measurable &= ok` there would match lines 232–235 on a host with `< 2` natural successes during the `/O2` pass. *AI-assisted review (static code/ISA inspection and local Python checks executed locally).*
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!801
No description provided.