Repository navigation
fix(aes-gcm): robust CPU feature detection under -Zbuild-std (CPUID fallback) - #33
Merged
Merged
Conversation
`hardware_available()` relied solely on `is_x86_feature_detected!`, backed by `std_detect`. When `std` is rebuilt from source (`cargo -Zbuild-std`, as the AddressSanitizer/ThreadSanitizer CI does), that detector mis-reports AES / PCLMULQDQ / SSSE3 as *absent* on CPUs that support them, so `HardwareAes256GcmKeyState::new` fails with `UnsupportedCpu` on fully-capable hardware (e.g. GitHub x86_64 runners, which the normal non-build-std jobs detect correctly). Detect more robustly, in both the AES and GHASH backends, without disturbing the common-path codegen: - Honor `cfg!(target_feature = ...)` first: a statically-enabled feature is guaranteed present at runtime (also a fast path for `-Ctarget-feature` / `target-cpu` builds). - Then keep the existing `is_x86_feature_detected!` check. A *positive* result is authoritative, so the normal runtime path is byte-for-byte unchanged. - Only when the detector reports a feature *absent* fall back to a `#[cold]` `#[inline(never)]` `CPUID` leaf-1 query (x86/x86-64). CPUID is the architectural source of truth and is unaffected by `-Zbuild-std`. Miri (which can't execute CPUID) trusts the negative `is_x86_feature_detected!` result. - aarch64 gains the same `cfg!(target_feature)` short-circuit; its runtime path keeps using `is_aarch64_feature_detected!`. No behavior change on normal builds (every crypto test already requires `hardware_available()` to be true). Verified clippy `-D warnings` clean on x86_64-unknown-linux-gnu and aarch64, fmt clean, full suite passes natively. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
jgowdy
force-pushed
the
feat/cpu-feature-detection-buildstd
branch
from
June 20, 2026 16:14
0b25225 to
1bd2a58
Compare
jgowdy
added a commit
that referenced
this pull request
Jun 20, 2026
`hardware_available()` relied solely on `is_x86_feature_detected!`, backed by `std_detect`. When `std` is rebuilt from source (`cargo -Zbuild-std`, as the AddressSanitizer/ThreadSanitizer CI does), that detector mis-reports AES / PCLMULQDQ / SSSE3 as *absent* on CPUs that support them, so `HardwareAes256GcmKeyState::new` fails with `UnsupportedCpu` on fully-capable hardware (e.g. GitHub x86_64 runners, which the normal non-build-std jobs detect correctly). Detect more robustly, in both the AES and GHASH backends, without disturbing the common-path codegen: - Honor `cfg!(target_feature = ...)` first: a statically-enabled feature is guaranteed present at runtime (also a fast path for `-Ctarget-feature` / `target-cpu` builds). - Then keep the existing `is_x86_feature_detected!` check. A *positive* result is authoritative, so the normal runtime path is byte-for-byte unchanged. - Only when the detector reports a feature *absent* fall back to a `#[cold]` `#[inline(never)]` `CPUID` leaf-1 query (x86/x86-64). CPUID is the architectural source of truth and is unaffected by `-Zbuild-std`. Miri (which can't execute CPUID) trusts the negative `is_x86_feature_detected!` result. - aarch64 gains the same `cfg!(target_feature)` short-circuit; its runtime path keeps using `is_aarch64_feature_detected!`. No behavior change on normal builds (every crypto test already requires `hardware_available()` to be true). Verified clippy `-D warnings` clean on x86_64-unknown-linux-gnu and aarch64, fmt clean, full suite passes natively. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
jgowdy
added a commit
that referenced
this pull request
Jun 20, 2026
`hardware_available()` relied solely on `is_x86_feature_detected!`, backed by `std_detect`. When `std` is rebuilt from source (`cargo -Zbuild-std`, as the AddressSanitizer/ThreadSanitizer CI does), that detector mis-reports AES / PCLMULQDQ / SSSE3 as *absent* on CPUs that support them, so `HardwareAes256GcmKeyState::new` fails with `UnsupportedCpu` on fully-capable hardware (e.g. GitHub x86_64 runners, which the normal non-build-std jobs detect correctly). Detect more robustly, in both the AES and GHASH backends, without disturbing the common-path codegen: - Honor `cfg!(target_feature = ...)` first: a statically-enabled feature is guaranteed present at runtime (also a fast path for `-Ctarget-feature` / `target-cpu` builds). - Then keep the existing `is_x86_feature_detected!` check. A *positive* result is authoritative, so the normal runtime path is byte-for-byte unchanged. - Only when the detector reports a feature *absent* fall back to a `#[cold]` `#[inline(never)]` `CPUID` leaf-1 query (x86/x86-64). CPUID is the architectural source of truth and is unaffected by `-Zbuild-std`. Miri (which can't execute CPUID) trusts the negative `is_x86_feature_detected!` result. - aarch64 gains the same `cfg!(target_feature)` short-circuit; its runtime path keeps using `is_aarch64_feature_detected!`. No behavior change on normal builds (every crypto test already requires `hardware_available()` to be true). Verified clippy `-D warnings` clean on x86_64-unknown-linux-gnu and aarch64, fmt clean, full suite passes natively. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
hardware_available()relied solely onis_x86_feature_detected!, backed bystd_detect. Whenstdis rebuilt from source (cargo -Zbuild-std, which the AddressSanitizer/ThreadSanitizer CI uses), that detector mis-reports AES / PCLMULQDQ / SSSE3 as absent on CPUs that have them, soHardwareAes256GcmKeyState::newreturnsUnsupportedCpuon fully-capable hardware.This surfaced downstream: a consumer's sanitizer job (
-Zbuild-std -Zsanitizer=address) failed with "required AES-256-GCM hardware support is unavailable" on a GitHub x86_64 runner — while the same runner's normal (non-build-std) jobs ran the identical hardware-backed tests and passed. The runner has AES-NI; the rebuilt-from-source detector was lying.Fix
Detect more robustly in both the AES and GHASH x86/aarch64 backends:
cfg!(target_feature = ...)first — a statically-enabled feature is guaranteed present at runtime, so trust it with no runtime query (also a fast path for-Ctarget-feature/target-cpubuilds).CPUIDleaf 1 directly instead ofstd_detect. CPUID is the architectural source of truth and is affected by neither-Zbuild-stdnor the sanitizers. Miri (which can't execute CPUID but modelsis_x86_feature_detected!) keeps using the detector.cfg!(target_feature)short-circuit; the runtime path keeps usingis_aarch64_feature_detected!.Safety / impact
__cpuidis a safe intrinsic on x86-64 andunsafeon 32-bit x86; the oneunsafeblock carries aSAFETYnote (CPUID leaf 1 is unconditionally valid on x86/x86-64).hardware_available()to be true, so the existing suite is the regression guard.-D warningsclean onx86_64-unknown-linux-gnuand aarch64,cargo fmt --checkclean, full test suite passes natively.