Skip to content

rust: Pass -Cunsafe-allow-abi-mismatch to build scripts only with a sanitizer - #1670

Closed
phr4nky wants to merge 2 commits into
meta:mainfrom
phr4nky:fix-buildscript-rustflags-abi-mismatch
Closed

phr4nky wants to merge 2 commits into
meta:mainfrom
phr4nky:fix-buildscript-rustflags-abi-mismatch

Conversation

@phr4nky

@phr4nky phr4nky commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Summary

Since 237a56c, buildscript_run adds -Cunsafe-allow-abi-mismatch=sanitizer to the CARGO_ENCODED_RUSTFLAGS of every build script. That keeps probe compiles from failing against a std built with a sanitizer.

Build scripts pass CARGO_ENCODED_RUSTFLAGS to rustc when they probe for features: autocfg-based crates such as num-traits, plus rustix, proc-macro2 and others. A rustc that does not know the option rejects every probe:

error: unknown codegen option: `unsafe-allow-abi-mismatch`

The build script reads that as "feature not available" and picks its fallback configuration. The build succeeds, so nothing reports that the crate was configured differently. Rust 1.85 rejects the option; 1.93, 1.98 and current nightly accept it. That leaves toolchains pinned to an older Rust silently misconfigured.

This PR adds the option only when the toolchain has a sanitizer, the case it was added for. That is the same field that puts -Zsanitizer= on the Rust compiles (build.bzl).

Affected version

origin/main 00c2ee4.

Reproduction

num-traits 0.2.19 with autocfg 1.5.1, through buildscript_run with the demo toolchains. Its probe checks for f64::total_cmp (Rust 1.62). The [rustc_flags] output:

rustc origin/main this PR
1.85.0 (empty) --cfg=has_total_cmp
1.93.0 --cfg=has_total_cmp --cfg=has_total_cmp

On origin/main with rustc 1.85, removing only -Cunsafe-allow-abi-mismatch=sanitizer brings --cfg=has_total_cmp back.

Test plan

  • New tests/prelude/rust/buildscript_rustflags_probe: a build script compiles an empty crate with $RUSTC and CARGO_ENCODED_RUSTFLAGS, as autocfg does, and the assertion expects its --cfg=probe_compiles.
  • Checked with a locally built buck2 on macOS: that test target and num-traits give the table above (rustc 1.85.0 from an isolated rustup home, and 1.93.0).
  • Not run here: a toolchain with a sanitizer, where the option is still added as before.

…anitizer

237a56c added `-Cunsafe-allow-abi-mismatch=sanitizer` to the
CARGO_ENCODED_RUSTFLAGS of every build script, so that their probe
compiles do not fail against a std built with a sanitizer. Build scripts
pass these flags to rustc for feature probes (autocfg, rustix, ...). A rustc
that does not know the option, such as 1.85, rejects every probe, and the
crate is built with its fallback cfgs without any error: num-traits loses
`has_total_cmp`.

Add the option only when the toolchain has a sanitizer, the case it was
added for.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 27, 2026
@meta-codesync

meta-codesync Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D134832345. (Because this pull request was imported automatically, there will not be any future comments.)

@dtolnay dtolnay left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

In general we have limited interest in supporting rust toolchains older than the most recent stable, but this seems fine.

@meta-codesync meta-codesync Bot closed this in 34b264c Nov 27, 2026
@meta-codesync meta-codesync Bot added the Merged label Nov 27, 2026
@meta-codesync

meta-codesync Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in 34b264c.

meta-codesync Bot pushed a commit to meta/buck2-prelude that referenced this pull request Nov 27, 2026
…anitizer

Summary:
Since 237a56ce94, `buildscript_run` adds `-Cunsafe-allow-abi-mismatch=sanitizer` to the `CARGO_ENCODED_RUSTFLAGS` of every build script. That keeps probe compiles from failing against a std built with a sanitizer.

Build scripts pass `CARGO_ENCODED_RUSTFLAGS` to rustc when they probe for features: autocfg-based crates such as `num-traits`, plus `rustix`, `proc-macro2` and others. A rustc that does not know the option rejects every probe:

```
error: unknown codegen option: `unsafe-allow-abi-mismatch`
```

The build script reads that as "feature not available" and picks its fallback configuration. The build succeeds, so nothing reports that the crate was configured differently. Rust 1.85 rejects the option; 1.93, 1.98 and current nightly accept it. That leaves toolchains pinned to an older Rust silently misconfigured.

This PR adds the option only when the toolchain has a `sanitizer`, the case it was added for. That is the same field that puts `-Zsanitizer=` on the Rust compiles (`build.bzl`).

## Affected version

origin/main 00c2ee4f74.

## Reproduction

`num-traits` 0.2.19 with `autocfg` 1.5.1, through `buildscript_run` with the demo toolchains. Its probe checks for `f64::total_cmp` (Rust 1.62). The `[rustc_flags]` output:

| rustc | origin/main | this PR |
| --- | --- | --- |
| 1.85.0 | (empty) | `--cfg=has_total_cmp` |
| 1.93.0 | `--cfg=has_total_cmp` | `--cfg=has_total_cmp` |

On origin/main with rustc 1.85, removing only `-Cunsafe-allow-abi-mismatch=sanitizer` brings `--cfg=has_total_cmp` back.

X-link: meta/buck2#1670

Reviewed By: dtolnay

Differential Revision: D134832345

fbshipit-source-id: d5cb6884d49343ad5337823f98a9e110014eeb4b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants