cpu: lower RDRAND and RDSEED in an opt-in host-entropy profile #801
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/807"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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.pyas the opt-in host-entropy profile agreed in #795. The profile is enabled bylower(..., host_entropy=True); the default profile still rejects both instructions._rdrand16_step…_rdseed64_step. There is no retry and no fence, so guest code keeps control of retrying.volatilelocal, so a read stays in the code when the guest discards it. A probe before this change showed that MSVC/O2removes such an_rdrand32_stepor_rdseed64_stepwithoutvolatile, and theplaincontrol below repeats that inside the test.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) andcpu_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:
Confirms on the host that OF, SF, ZF, AF and PF are 0 after the 64-bit instruction. Two
addpresets set these flags beforehand.Confirms that the default profile rejects every read.
Runs 2,000 rounds per width, natively and translated. Four
add r8, r8presets set ZF, CF and OF before each read. Every read must be either:Natural failures are counted and printed, never required.
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.
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_oklocals of the new statement./O2: the translation is also built withcl /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. Theplaincontrol removesvolatileand 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(onmain186711d) 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 bitscpu_rdseed_plain_o2: … {'rdseed_16': 1, 'rdseed_32': 1, 'rdseed_64': 1, 'rdseed_unused': 0}has_*forced to 0; every native and lifted call raises): each part exits 77 before any read.failDLL in place oflifted): 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).cpu_shim_shadow_hardwarein its native run:D3D12XboxCreateDevice failed (HRESULT 0x887a0004). That run doesn't use this lowering.src/d3d12_xs_shim.cpprequests. I'll report it separately.tools/form_coverage.pyon that JUnit run no longer listsrdrandorrdseedas gaps.tests/docs_links_check.py,tests/bidi_controls_check.pyandtests/form_coverage_check.pypass.No README row lists individual lowered instructions (as for RDTSC in #789), so the README is unchanged.
Limits
failvariant.Refs #795
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
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 againstmainab5d3a5).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
devandrelease/O2CI jobs on this head both passed).Checks on
a0f28fa:python -B tests/docs_links_check.py(13 files, 154 links) andpython -B tests/bidi_controls_check.py(654 files): both passed.git merge-tree --write-tree main a0f28fa: clean againstab5d3a5(exit 0).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, andtests/cpu/rdrand.py:RDRANDandRDSEED; lowering is gated behindhost_entropy=True._rdrand{16,32,64}_step/_rdseed{16,32,64}_stepmatchuint16_t/uint32_t/uint64_twidths;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_okkeeps discarded reads alive at/O2(verified by theplaincontrol).rdrand_harness.asmproperly saves/restoresrbxaroundcpuid, checkscpuid(0).eax >= 7withecx = 0before leaf 7, has no; coverage:header, and is registered asNoneintools/form_coverage.py.CMakeLists.txtsetsSKIP_RETURN_CODE 77on bothcpu_aot_rdrandandcpu_aot_rdseed, anddocs/experiments.mdaccurately states the CPUID preconditions and the unmodelled SF/AF exception.succeed/failvariants and targeted mutating negative controls (constant,nocarry,noext,wide16,flags,nozero,plain).Non-blocking notes:
signtolower()),RDRAND/RDSEEDwill needsign = false;soSFis cleared alongsideZF/OF.tests/cpu/rdrand.py:252, the/O2lifted check checksvariation(part, values)[0]but ignores the second return value (ok); propagatingmeasurable &= okthere would match lines 232–235 on a host with< 2natural successes during the/O2pass.AI-assisted review (static code/ISA inspection and local Python checks executed locally).