Skip to content
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

Fix std_detect_env_override feature on Windows by avoiding libc #1571

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Install Rust
run: rustup update nightly && rustup default nightly
run: rustup update nightly --no-self-update && rustup default nightly
- run: ci/style.sh

docs:
Expand All @@ -25,7 +25,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Install Rust
run: rustup update nightly && rustup default nightly
run: rustup update nightly --no-self-update && rustup default nightly
- run: ci/dox.sh
env:
CI: 1
Expand All @@ -45,18 +45,22 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Install Rust
run: rustup update nightly && rustup default nightly
run: rustup update nightly --no-self-update && rustup default nightly
- run: cargo test --manifest-path crates/stdarch-verify/Cargo.toml

env_override:
name: Env Override
needs: [style]
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
steps:
- uses: actions/checkout@v4
- name: Install Rust
run: rustup update nightly && rustup default nightly
run: rustup update nightly --no-self-update && rustup default nightly
- run: RUST_STD_DETECT_UNSTABLE=avx cargo test --features=std_detect_env_override --manifest-path crates/std_detect/Cargo.toml env_override_no_avx
shell: bash

test:
needs: [style]
Expand Down
43 changes: 34 additions & 9 deletions crates/std_detect/src/detect/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,42 @@ impl Cache {

cfg_if::cfg_if! {
if #[cfg(feature = "std_detect_env_override")] {
#[inline]
fn disable_features(disable: &[u8], value: &mut Initializer) {
if let Ok(disable) = core::str::from_utf8(disable) {
for v in disable.split(" ") {
let _ = super::Feature::from_str(v).map(|v| value.unset(v as u32));
}
}
}

#[inline]
fn initialize(mut value: Initializer) -> Initializer {
let env = unsafe {
libc::getenv(b"RUST_STD_DETECT_UNSTABLE\0".as_ptr() as *const libc::c_char)
};
if !env.is_null() {
let len = unsafe { libc::strlen(env) };
let env = unsafe { core::slice::from_raw_parts(env as *const u8, len) };
if let Ok(disable) = core::str::from_utf8(env) {
for v in disable.split(" ") {
let _ = super::Feature::from_str(v).map(|v| value.unset(v as u32));
const RUST_STD_DETECT_UNSTABLE: &[u8] = b"RUST_STD_DETECT_UNSTABLE\0";
cfg_if::cfg_if! {
if #[cfg(windows)] {
use alloc::vec;
#[link(name = "kernel32")]
extern "system" {
fn GetEnvironmentVariableA(name: *const u8, buffer: *mut u8, size: u32) -> u32;
}
let len = unsafe { GetEnvironmentVariableA(RUST_STD_DETECT_UNSTABLE.as_ptr(), core::ptr::null_mut(), 0) };
if len > 0 {
// +1 to include the null terminator.
let mut env = vec![0; len as usize + 1];
let len = unsafe { GetEnvironmentVariableA(RUST_STD_DETECT_UNSTABLE.as_ptr(), env.as_mut_ptr(), len + 1) };
if len > 0 {
disable_features(&env[..len as usize], &mut value);
}
}
} else {
let env = unsafe {
libc::getenv(RUST_STD_DETECT_UNSTABLE.as_ptr() as *const libc::c_char)
};
if !env.is_null() {
let len = unsafe { libc::strlen(env) };
let env = unsafe { core::slice::from_raw_parts(env as *const u8, len) };
disable_features(env, &mut value);
}
}
}
Expand Down