Closed
Description
We use conditional compilation in a lot of places (eg the fuzzing
cfg flag is standard to conditionally compile when fuzzing in the rust-fuzz ecosystem) and have a hard rule against unnecessary build.rs
s (as running build-time code is a red flag when auditing a crate and requires special care). Latest rustc nightly now generates a huge pile of warnings encouraging us to add a build.rs
to make them go away, which isn't really acceptable. It seems this is encouraging bad practice to respond to a common practice - is there some way to just list fuzzing
and some other super common flags and allow those?
Activity
saethlin commentedon May 6, 2024
Yes there is a list of well-known cfgs, for example #124742.
fuzzing
would be a good addition. We did already try to add the super common ones, so you'll have to be specific about what others would be helpful.TheBlueMatt commentedon May 6, 2024
Fuzzing is the biggest ecosystem-wide one, but we also use cfg flags to land code that isn't complete yet and we don't want to enable but which requires a long series of steps and we don't want to try to land it all in one go, so we need the ability to add our own in Cargo.toml. I can't imagine this is a super uncommon use?
#![allow(unexpected_cfgs]
to every non-example target rust-bitcoin/rust-bitcoin#2748apoelstra commentedon May 6, 2024
I fairly frequently use
cfg
flags to disable key parts of my crates (mainly: heavy cryptography) for the purpose of speeding up a fuzzer or allowing it to forge signatures so it can hit more paths. The names of these are deliberately uncommon and project-specific.So I would also like a way to whitelist cfg flags. Cargo.toml would be great.
For now I am disabling the lint entirely with
allow
but I would like to have the lint on, since I've been burned by cfg typos many times in the past. But adding abuild.rs
will slow down compilation and raise eyebrows of anybody reviewing my crates.Urgau commentedon May 6, 2024
It wouldn't be unnecessary, it would be used.
Adding a
build.rs
is not a bad practice.The rule of thumb that was agreed in the stabilization of
--check-cfg
was to only include a restricted set of cfgs and only the least common for all users of Rust, including ones that do not use Cargo, like Rust-for-Linux.Yes in a
build.rs
withcargo::rustc-check-cfg=cfg(fuzzing)
. Or using a Cargo feature instead.TheBlueMatt commentedon May 6, 2024
Sorry, but "add additional code that runs at build-time that users have to audit separately just to disable a compile-time warning" is really offensive stance. Indeed, some folks have legitimate uses for build.rs, and within parts of the Rust ecosystem build.rs is a totally accepted practice, but that definitely isn't universal and for those building security-conscious software build.rs is an orange flag that requires extra auditing and care, and is absolutely not something that is universally acceptable without consideration.
More broadly, I also work on some projects that are optionally being built directly with
rustc
rather than cargo. While those builds won't suffer from this issue (AFAIU?), having abuild.rs
is obviously pretty painful in those cases.epage commentedon May 6, 2024
imo this sounds like a case for
features
. If you are concerned about others using it, there is a practice forunstable-
prefixes and a proposal for official unstable feature support.The original RFC proposed a
[cfg]
syntax but the cargo team rejected it as--cfg
s are a low level rustc abstraction that don't fit within cargo (iiuc).I've created the following Pre-RFC that would create something for Cargo but don't have the time to drive it to completion and no one else has picked it up
https://internals.rust-lang.org/t/pre-rfc-mutually-excusive-global-features/19618
TheBlueMatt commentedon May 6, 2024
There's a few issues with this - first of all just calling something "unstable-" doesn't mean no one can enable it, and it means a transitive dependency somewhere having a bug will mean my crate will suddenly generate completely bogus results. More generally, this isn't an "unstable" feature but rather a "you should never, ever, ever, ever enable this feature unless you're fuzzing, don't touch this".
However, more broadly, we rely on transitive fuzzing features in some cases, and the standard, ecosystem-wide
cfg=fuzzing
is really nice for that - I don't have to think about which transitive dependencies I have that have fuzzing-specific optimizations, and those transitive dependencies don't have to worry about exposing any kind of public API for fuzzing code enabling - the fuzzers just automatically setcfg=fuzzing
and we're off to the races.Then maybe the warnings should be off by default until we make progress here? Or, at a minimum, the suggested fix should be to turn off the warnings, rather than add a
build.rs
to all our crates.Yep, that's exactly why we use them - they're "transparent" to cargo which makes them incredibly useful for things like fuzzing and in-progress code. Suddenly throwing tons of warnings at users for what seems like a perfectly reasonable use of a language feature seems like quite a regression (but of course I understand the utility of these warnings - we have existing python scripts that parse our code and detect undefined cfg flags :) ).
saethlin commentedon May 6, 2024
Not the fuzzing component. cfgs like
miri
,fuzzing
, andloom
are used for ecosystem-wide coordination. They exist to be well-known. #124804apoelstra commentedon May 6, 2024
If this is true then why is Cargo giving flags to rustc that complain about them?
As Matt is saying, (one) purpose of using cfg flags is to bypass Cargo and its opinions.
105 remaining items