Skip to content

std_detect: Always avoid dlsym on *-linux-{musl,ohos}* targets #1746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ci/build-std-detect.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ set -ex
cd "$(dirname "$0")"/..

targets=(
# Linux
aarch64-unknown-linux-musl
armv5te-unknown-linux-musleabi
aarch64-unknown-linux-ohos
armv7-unknown-linux-ohos

# Android
aarch64-linux-android
arm-linux-androideabi
Expand Down
9 changes: 6 additions & 3 deletions crates/std_detect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ run-time feature detection. When this feature is disabled, `std_detect` assumes
that [`getauxval`] is linked to the binary. If that is not the case the behavior
is undefined.

Note: This feature is ignored on `*-linux-gnu*` and `*-android*` targets
Note: This feature is ignored on `*-linux-{gnu,musl,ohos}*` and `*-android*` targets
because we can safely assume `getauxval` is linked to the binary.
* `*-linux-gnu*` targets ([since Rust 1.64](https://blog.rust-lang.org/2022/08/01/Increasing-glibc-kernel-requirements.html))
have glibc requirements higher than [glibc 2.16 that added `getauxval`](https://sourceware.org/legacy-ml/libc-announce/2012/msg00000.html).
have glibc requirements higher than [glibc 2.16 that added `getauxval`](https://sourceware.org/legacy-ml/libc-announce/2012/msg00000.html).
* `*-linux-musl*` targets ([at least since Rust 1.15](https://github.com/rust-lang/rust/blob/1.15.0/src/ci/docker/x86_64-musl/build-musl.sh#L15))
use musl newer than [musl 1.1.0 that added `getauxval`](https://git.musl-libc.org/cgit/musl/tree/WHATSNEW?h=v1.1.0#n1197)
* `*-linux-ohos*` targets use a [fork of musl 1.2](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/native-lib/musl.md)
* `*-android*` targets ([since Rust 1.68](https://blog.rust-lang.org/2023/01/09/android-ndk-update-r25.html))
have the minimum supported API level higher than [Android 4.3 (API level 18) that added `getauxval`](https://github.com/aosp-mirror/platform_bionic/blob/d3ebc2f7c49a9893b114124d4a6b315f3a328764/libc/include/sys/auxv.h#L49).
have the minimum supported API level higher than [Android 4.3 (API level 18) that added `getauxval`](https://github.com/aosp-mirror/platform_bionic/blob/d3ebc2f7c49a9893b114124d4a6b315f3a328764/libc/include/sys/auxv.h#L49).

* `std_detect_file_io` (enabled by default, requires `std`): Enable to perform run-time feature
detection using file APIs (e.g. `/proc/cpuinfo`, etc.) if other more performant
Expand Down
30 changes: 22 additions & 8 deletions crates/std_detect/src/detect/os/linux/auxvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ pub(crate) struct AuxVec {
/// feature detection on some platforms.
///
/// Note: The `std_detect_dlsym_getauxval` cargo feature is ignored on
/// `*-linux-gnu*` and `*-android*` targets because we can safely assume `getauxval`
/// `*-linux-{gnu,musl,ohos}*` and `*-android*` targets because we can safely assume `getauxval`
/// is linked to the binary.
/// - `*-linux-gnu*` targets ([since Rust 1.64](https://blog.rust-lang.org/2022/08/01/Increasing-glibc-kernel-requirements.html))
/// have glibc requirements higher than [glibc 2.16 that added `getauxval`](https://sourceware.org/legacy-ml/libc-announce/2012/msg00000.html).
/// - `*-linux-musl*` targets ([at least since Rust 1.15](https://github.com/rust-lang/rust/blob/1.15.0/src/ci/docker/x86_64-musl/build-musl.sh#L15))
/// use musl newer than [musl 1.1.0 that added `getauxval`](https://git.musl-libc.org/cgit/musl/tree/WHATSNEW?h=v1.1.0#n1197)
/// - `*-linux-ohos*` targets use a [fork of musl 1.2](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/native-lib/musl.md)
/// - `*-android*` targets ([since Rust 1.68](https://blog.rust-lang.org/2023/01/09/android-ndk-update-r25.html))
/// have the minimum supported API level higher than [Android 4.3 (API level 18) that added `getauxval`](https://github.com/aosp-mirror/platform_bionic/blob/d3ebc2f7c49a9893b114124d4a6b315f3a328764/libc/include/sys/auxv.h#L49).
///
Expand All @@ -73,7 +76,10 @@ pub(crate) struct AuxVec {
pub(crate) fn auxv() -> Result<AuxVec, ()> {
#[cfg(all(
feature = "std_detect_dlsym_getauxval",
not(all(target_os = "linux", target_env = "gnu")),
not(all(
target_os = "linux",
any(target_env = "gnu", target_env = "musl", target_env = "ohos"),
)),
// TODO: libc crate currently doesn't provide getauxval on 32-bit Android.
not(all(target_os = "android", target_pointer_width = "64")),
))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot of duplication in this function which makes it hard to review. I think this is a good opportunity to move the choice of whether to use libc::getauxval or look it up dynamically into the local getauxval function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 2960b07.

I actually found a difference between the two blocks...

// Targets with only AT_HWCAP:
#[cfg(any(
target_arch = "riscv32",
target_arch = "riscv64",
target_arch = "mips",
target_arch = "mips64"
))]

// Targets with only AT_HWCAP:
#[cfg(any(
target_arch = "riscv32",
target_arch = "riscv64",
target_arch = "mips",
target_arch = "mips64",
target_arch = "loongarch64",
))]

Expand Down Expand Up @@ -120,12 +126,15 @@ pub(crate) fn auxv() -> Result<AuxVec, ()> {
}
}

#[cfg(any(
not(feature = "std_detect_dlsym_getauxval"),
all(target_os = "linux", target_env = "gnu"),
#[cfg(not(all(
feature = "std_detect_dlsym_getauxval",
not(all(
target_os = "linux",
any(target_env = "gnu", target_env = "musl", target_env = "ohos"),
)),
// TODO: libc crate currently doesn't provide getauxval on 32-bit Android.
all(target_os = "android", target_pointer_width = "64"),
))]
not(all(target_os = "android", target_pointer_width = "64")),
)))]
{
// Targets with only AT_HWCAP:
#[cfg(any(
Expand Down Expand Up @@ -184,7 +193,12 @@ pub(crate) fn auxv() -> Result<AuxVec, ()> {
test,
all(
feature = "std_detect_dlsym_getauxval",
not(all(target_os = "linux", target_env = "gnu"))
not(all(
target_os = "linux",
any(target_env = "gnu", target_env = "musl", target_env = "ohos"),
)),
// TODO: libc crate currently doesn't provide getauxval on 32-bit Android.
not(all(target_os = "android", target_pointer_width = "64")),
)
))]
fn getauxval(key: usize) -> Result<usize, ()> {
Expand Down