Skip to content

fix(aes-gcm): robust CPU feature detection under -Zbuild-std (CPUID fallback) - #33

Merged
jgowdy merged 1 commit into
mainfrom
feat/cpu-feature-detection-buildstd
Jun 20, 2026
Merged

jgowdy merged 1 commit into
mainfrom
feat/cpu-feature-detection-buildstd

Conversation

@jgowdy

@jgowdy jgowdy commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Problem

hardware_available() relied solely on is_x86_feature_detected!, backed by std_detect. When std is 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, so HardwareAes256GcmKeyState::new returns UnsupportedCpu on 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:

  • Honor 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-cpu builds).
  • On x86/x86-64, query CPUID leaf 1 directly instead of std_detect. CPUID is the architectural source of truth and is affected by neither -Zbuild-std nor the sanitizers. Miri (which can't execute CPUID but models is_x86_feature_detected!) keeps using the detector.
  • On aarch64, add the same cfg!(target_feature) short-circuit; the runtime path keeps using is_aarch64_feature_detected!.

Safety / impact

  • __cpuid is a safe intrinsic on x86-64 and unsafe on 32-bit x86; the one unsafe block carries a SAFETY note (CPUID leaf 1 is unconditionally valid on x86/x86-64).
  • No behavior change on normal builds — every crypto test already requires hardware_available() to be true, so the existing suite is the regression guard.
  • Verified: clippy -D warnings clean on x86_64-unknown-linux-gnu and aarch64, cargo fmt --check clean, full test suite passes natively.

`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
jgowdy force-pushed the feat/cpu-feature-detection-buildstd branch from 0b25225 to 1bd2a58 Compare June 20, 2026 16:14
@jgowdy
jgowdy merged commit 3609861 into main Jun 20, 2026
23 checks passed
jgowdy added a commit that referenced this pull request Jun 20, 2026
Detached AES-256-GCM seal/open primitive (#32) and robust CPU feature
detection under -Zbuild-std / sanitizers (#33).

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>
jgowdy added a commit that referenced this pull request Jun 20, 2026
Detached AES-256-GCM seal/open primitive (#32) and robust CPU feature
detection under -Zbuild-std / sanitizers (#33).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jgowdy
jgowdy deleted the feat/cpu-feature-detection-buildstd branch June 20, 2026 22:42
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
Detached AES-256-GCM seal/open primitive (#32) and robust CPU feature
detection under -Zbuild-std / sanitizers (#33).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant