cpu: lower RDRAND and RDSEED in the shared lowering #789

Closed
opened 2026-11-23 04:55:55 +00:00 by legacy-code-archaeologist · 2 comments
legacy-code-archaeologist commented 2026-11-23 04:55:55 +00:00 (Migrated from github.com)
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-23T04:55:55Z.


Claim and design question: lower RDRAND and RDSEED in the shared lowering

rdrand and rdseed are in the Integer group of docs/coverage/x64_zen2.tsv. Neither has a passing fixture: tools/form_coverage.py --gaps x64_zen2 lists both as unobserved after a full ^cpu_ CTest run with records on main after #789. lower() in tools/ffx_version_query.py rejects both today. I found no open issue or PR that covers them, and I'd like to take them as one small PR, following the pattern agreed in #739.

Proposed behavior (host entropy profile)

  • RDRAND r16/r32/r64 calls _rdrand16_step, _rdrand32_step or _rdrand64_step. RDSEED calls the matching _rdseed*_step intrinsic.
  • The intrinsic's return value becomes CF. The value goes to the destination, so on failure the destination is 0, as the instruction defines.
  • A 32-bit destination zero-extends. A 16-bit destination keeps bits 16–63.
  • The lowering clears ZF and OF, and also PF in the profiles that track it. SF and AF are not modeled in the shared lowering.
  • Each read stays observable when its result is unused, as for RDTSC in #789.

Proposed fixture (source-built MASM, property checks)

The value is non-deterministic, so the check compares properties rather than bytes:

  • The destination width rules above hold: zero extension for 32-bit, upper bits kept for 16-bit. Each register starts as all ones.
  • After a successful read, CF=1 and ZF/OF are clear, from presets that set them first. A failed read (CF=0) must leave 0 in the destination; the harness records such reads but does not require them to occur.
  • Over many reads, the translated values are not all equal, and the 64-bit form produces both high-half bits.
  • An /O2 build still contains every RDRAND and RDSEED, including one whose result is overwritten, using the branch walk from #789.

Negative controls (mutations of the generated C++) must each fail:

  • constant (fixed value);
  • nocarry (CF dropped);
  • noext (no zero extension);
  • wide16 (the 16-bit form overwrites the upper bits);
  • flags (ZF/OF left as preset).

Separate tests: cpu_aot_rdrand and cpu_aot_rdseed are separate tests with separate fixture sources. Each exits 77 only when its own CPUID bit is clear: RDRAND is CPUID.01H:ECX bit 30, RDSEED is CPUID.(EAX=07H,ECX=0):EBX bit 18.

Questions before I start

  1. Is the host entropy pass-through the intended contract, with no reproducibility guarantee? The alternative is a runtime hook, so a replay or deterministic mode could supply values later. I'd keep the first PR to the pass-through and document it as an explicit profile in docs/experiments.md.
  2. Should a failed read stay a failed read (CF=0, destination 0)? Or should the lowering retry until it succeeds? Guest code normally loops on CF itself, so I'd pass the failure through.

Test host: Intel Skylake (i7-6820HQ) on Windows 11 26100.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6552569851 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/795). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-23T04:55:55Z. --- ### Claim and design question: lower `RDRAND` and `RDSEED` in the shared lowering `rdrand` and `rdseed` are in the Integer group of `docs/coverage/x64_zen2.tsv`. Neither has a passing fixture: `tools/form_coverage.py --gaps x64_zen2` lists both as `unobserved` after a full `^cpu_` CTest run with records on `main` after #789. `lower()` in `tools/ffx_version_query.py` rejects both today. I found no open issue or PR that covers them, and I'd like to take them as one small PR, following the pattern agreed in #739. **Proposed behavior (host entropy profile)** - `RDRAND r16/r32/r64` calls `_rdrand16_step`, `_rdrand32_step` or `_rdrand64_step`. `RDSEED` calls the matching `_rdseed*_step` intrinsic. - The intrinsic's return value becomes CF. The value goes to the destination, so on failure the destination is 0, as the instruction defines. - A 32-bit destination zero-extends. A 16-bit destination keeps bits 16–63. - The lowering clears ZF and OF, and also PF in the profiles that track it. SF and AF are not modeled in the shared lowering. - Each read stays observable when its result is unused, as for RDTSC in #789. **Proposed fixture (source-built MASM, property checks)** The value is non-deterministic, so the check compares properties rather than bytes: - The destination width rules above hold: zero extension for 32-bit, upper bits kept for 16-bit. Each register starts as all ones. - After a successful read, CF=1 and ZF/OF are clear, from presets that set them first. A failed read (CF=0) must leave 0 in the destination; the harness records such reads but does not require them to occur. - Over many reads, the translated values are not all equal, and the 64-bit form produces both high-half bits. - An `/O2` build still contains every `RDRAND` and `RDSEED`, including one whose result is overwritten, using the branch walk from #789. **Negative controls** (mutations of the generated C++) must each fail: - `constant` (fixed value); - `nocarry` (CF dropped); - `noext` (no zero extension); - `wide16` (the 16-bit form overwrites the upper bits); - `flags` (ZF/OF left as preset). **Separate tests:** `cpu_aot_rdrand` and `cpu_aot_rdseed` are separate tests with separate fixture sources. Each exits 77 only when its own CPUID bit is clear: RDRAND is CPUID.01H:ECX bit 30, RDSEED is CPUID.(EAX=07H,ECX=0):EBX bit 18. **Questions before I start** 1. Is the host entropy pass-through the intended contract, with no reproducibility guarantee? The alternative is a runtime hook, so a replay or deterministic mode could supply values later. I'd keep the first PR to the pass-through and document it as an explicit profile in `docs/experiments.md`. 2. Should a failed read stay a failed read (CF=0, destination 0)? Or should the lowering retry until it succeeds? Guest code normally loops on CF itself, so I'd pass the failure through. Test host: Intel Skylake (i7-6820HQ) on Windows 11 26100. AI-assisted (Claude Code).
R0Y00 commented 2026-11-24 06:49:01 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T05:30:25Z.


Yes, please proceed with the opt-in host-entropy profile. Execute the same instruction once per guest execution and expose its carry result; on CF=0 write zero at the selected width. Guest code retains control of retrying. Document the corresponding host CPUID bit as a precondition and keep these opcodes rejected in the default profile. The fixture should check width and cleared flags on both outcomes, and count natural failures without requiring a random failure in CI. A deterministic test-only failed-read control would let the fixture check the zero and carry rules even when the host succeeds every time. Keep the /O2 discarded-result check and the separate capability skips. Variation checks can catch a constant translation; they do not assess entropy quality.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7092465573 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/795#issuecomment-7092465573). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T05:30:25Z. --- Yes, please proceed with the opt-in host-entropy profile. Execute the same instruction once per guest execution and expose its carry result; on CF=0 write zero at the selected width. Guest code retains control of retrying. Document the corresponding host CPUID bit as a precondition and keep these opcodes rejected in the default profile. The fixture should check width and cleared flags on both outcomes, and count natural failures without requiring a random failure in CI. A deterministic test-only failed-read control would let the fixture check the zero and carry rules even when the host succeeds every time. Keep the `/O2` discarded-result check and the separate capability skips. Variation checks can catch a constant translation; they do not assess entropy quality.
R0Y00 commented 2026-11-24 06:49:03 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T06:42:43Z.


Implemented in #807 (df6a9c0). The opt-in host profile makes one attempt per guest instruction. The fixtures check both carry outcomes, zero on failure, destination width, cleared modeled flags and retention of discarded reads at /O2. RDRAND and RDSEED retain separate capability checks.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7093601437 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/795#issuecomment-7093601437). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T06:42:43Z. --- Implemented in #807 (df6a9c0). The opt-in host profile makes one attempt per guest instruction. The fixtures check both carry outcomes, zero on failure, destination width, cleared modeled flags and retention of discarded reads at /O2. RDRAND and RDSEED retain separate capability checks.
Sign in to join this conversation.
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#789
No description provided.