Repository navigation
cpu_guard: build-time warning when a target feature has no guard row #360
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Warns when a build enables x86_64 target features that `cpu_guard` has no | ||
| // row for. Those features are compiled in but never compared with the running | ||
| // CPU, so the guard would pass a binary that can still die with SIGILL. | ||
| // | ||
| // Limits: on stable rustc, `CARGO_CFG_TARGET_FEATURE` omits unstable features | ||
| // (avx10.x, amx-*), exactly as `cfg!` in the guard does, so they cannot be | ||
| // reported here. Cargo also hides warnings from git and registry dependencies, | ||
| // so this fires when building ndarray itself, not inside a consumer. | ||
|
|
||
| include!("src/cpu_guard_drift.rs"); | ||
|
|
||
| fn main() { | ||
| println!("cargo:rerun-if-changed=build.rs"); | ||
| println!("cargo:rerun-if-changed=src/cpu_guard.rs"); | ||
| println!("cargo:rerun-if-changed=src/cpu_guard_drift.rs"); | ||
|
|
||
| if std::env::var("CARGO_CFG_TARGET_ARCH").as_deref() != Ok("x86_64") { | ||
| return; | ||
| } | ||
| let enabled = std::env::var("CARGO_CFG_TARGET_FEATURE").unwrap_or_default(); | ||
| let guard_src = match std::fs::read_to_string("src/cpu_guard.rs") { | ||
| Ok(src) => src, | ||
| Err(e) => { | ||
| println!("cargo:warning=cpu_guard drift check skipped: cannot read src/cpu_guard.rs: {e}"); | ||
| return; | ||
| } | ||
| }; | ||
| let table = guard_table_names(&guard_src); | ||
| let unchecked = unchecked_features(&enabled, &table); | ||
| if !unchecked.is_empty() { | ||
| println!( | ||
| "cargo:warning=cpu_guard has no row for enabled target features: {} \ | ||
| (the guard cannot detect a CPU that lacks them; add rows in src/cpu_guard.rs)", | ||
| unchecked.join(", ") | ||
| ); | ||
| } | ||
| } |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Shared by `build.rs` and the tests in `cpu_guard.rs` (both `include!` it); | ||
| // not a module of the crate. | ||
| // | ||
| // Which target features a build enables that `cpu_guard` cannot check. The | ||
| // guard asks `cfg!(target_feature = ...)` row by row, so a feature that is | ||
| // compiled in but has no row is never compared with the running CPU: the | ||
| // binary passes the guard and can still die with SIGILL. That happens when a | ||
| // toolchain bump lets LLVM enable a feature the table predates. | ||
|
|
||
| /// Features every x86_64 CPU has; the guard does not need rows for them. | ||
| const X86_64_BASELINE: &[&str] = &["sse", "sse2", "x87"]; | ||
|
|
||
| /// Target features that do not describe the CPU: `crt-static` selects static | ||
| /// C-runtime linkage and is on by default for musl targets. | ||
| const NOT_CPU_FEATURES: &[&str] = &["crt-static"]; | ||
|
|
||
| /// The feature names in the x86_64 table of `cpu_guard.rs` (`"name" =>` rows). | ||
| fn guard_table_names(cpu_guard_src: &str) -> Vec<&str> { | ||
| cpu_guard_src | ||
| .lines() | ||
| .filter_map(|line| { | ||
| let rest = line.trim_start().strip_prefix('"')?; | ||
| let (name, tail) = rest.split_once('"')?; | ||
| tail.trim_start().starts_with("=>").then_some(name) | ||
| }) | ||
| .collect() | ||
| } | ||
|
|
||
| /// The enabled features (`CARGO_CFG_TARGET_FEATURE`, comma-separated) that | ||
| /// are neither in the guard's table, nor x86_64 baseline, nor a non-CPU | ||
| /// feature, in input order. | ||
| fn unchecked_features<'a>(enabled: &'a str, table: &[&str]) -> Vec<&'a str> { | ||
| enabled | ||
| .split(',') | ||
| .filter(|f| { | ||
| !f.is_empty() && !table.contains(f) && !X86_64_BASELINE.contains(f) && !NOT_CPU_FEATURES.contains(f) | ||
| }) | ||
| .collect() | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
crt-staticfrom the CPU feature warningOn targets such as
x86_64-unknown-linux-musl,CARGO_CFG_TARGET_FEATUREincludescrt-staticby default, even though it describes the C runtime linkage rather than a CPU capability. Because it is neither in the guard table nor this exclusion list, every such build warns that the CPU guard cannot detect a CPU lackingcrt-staticand asks for an impossible guard row. Add it to the non-checkable exclusions so static x86_64 targets do not receive this false warning.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed.
rustc --print cfg --target x86_64-unknown-linux-musllistscrt-static, and of the default x86_64 sets (gnu, musl, msvc, darwin) it was the only feature that does not describe the CPU. Fixed in 9869e02 (not yet pushed): it is excluded alongside the baseline. The new testcrt_static_is_not_a_cpu_featurewas red before the exclusion (6ace106) and checks that a real missing row next tocrt-staticis still reported.Generated by Claude Code