Skip to content

Commit

Permalink
cpu arm: Fix static feature detection initialization.
Browse files Browse the repository at this point in the history
Commit f932b94 was incomplete and
wrong. On targets where we do any static or dynamic feature detection
and where we have these global variables, we need to unconditionally
write the detected features to the global variable so that assembly
can see them. Since we do static feature detection regardless of
operating system, the initialization of the global most be done
without any conditions on the operating system.
  • Loading branch information
briansmith committed Dec 1, 2023
1 parent e530025 commit 6b187be
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ name = "ring"
getrandom = { version = "0.2.10" }
untrusted = { version = "0.9" }

[target.'cfg(any(target_arch = "x86",target_arch = "x86_64", all(any(target_arch = "aarch64", target_arch = "arm"), any(target_os = "android", target_os = "fuchsia", target_os = "linux", target_os = "windows"))))'.dependencies]
[target.'cfg(any(target_arch = "aarch64", target_arch = "arm", target_arch = "x86",target_arch = "x86_64"))'.dependencies]
spin = { version = "0.9.8", default-features = false, features = ["once"] }

[target.'cfg(all(any(target_os = "android", target_os = "linux"), any(target_arch = "aarch64", target_arch = "arm")))'.dependencies]
Expand Down
38 changes: 10 additions & 28 deletions src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,21 @@ pub(crate) struct Features(());

#[inline(always)]
pub(crate) fn features() -> Features {
// We don't do runtime feature detection on aarch64-apple-* as all AAarch64
// features we use are available on every device since the first devices.
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
use arm::init_global_shared_with_assembly;

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use intel::init_global_shared_with_assembly;

#[cfg(any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "x86",
target_arch = "x86_64",
all(
any(target_arch = "aarch64", target_arch = "arm"),
any(
target_os = "android",
target_os = "fuchsia",
target_os = "linux",
target_os = "windows"
)
)
))]
{
static INIT: spin::Once<()> = spin::Once::new();
let () = INIT.call_once(|| {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
{
prefixed_extern! {
fn OPENSSL_cpuid_setup();
}
unsafe {
OPENSSL_cpuid_setup();
}
}

#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
{
unsafe { arm::initialize_OPENSSL_armcap_P() }
}
});
static INIT: spin::once::Once<()> = spin::once::Once::new();
let () = *INIT.call_once(|| unsafe { init_global_shared_with_assembly() });
}

Features(())
Expand Down
2 changes: 1 addition & 1 deletion src/cpu/arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ features! {
// `INIT`.
// - See the safety invariants of `OPENSSL_armcap_P` below.
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
pub unsafe fn initialize_OPENSSL_armcap_P() {
pub unsafe fn init_global_shared_with_assembly() {
let detected = detect_features();
let filtered = (if cfg!(feature = "unstable-testing-arm-no-hw") {
AES.mask | SHA256.mask | PMULL.mask
Expand Down
10 changes: 10 additions & 0 deletions src/cpu/intel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ pub(crate) struct Feature {
mask: u32,
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub(super) unsafe fn init_global_shared_with_assembly() {
prefixed_extern! {
fn OPENSSL_cpuid_setup();
}
unsafe {
OPENSSL_cpuid_setup();
}
}

impl Feature {
#[allow(clippy::needless_return)]
#[inline(always)]
Expand Down

0 comments on commit 6b187be

Please sign in to comment.