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

Is passing -Clink-dead-code by default still a good idea? #391

Closed
saethlin opened this issue Jan 15, 2025 · 5 comments · Fixed by #394
Closed

Is passing -Clink-dead-code by default still a good idea? #391

saethlin opened this issue Jan 15, 2025 · 5 comments · Fixed by #394
Assignees

Comments

@saethlin
Copy link
Contributor

saethlin commented Jan 15, 2025

I work on the monomorphization code in the compiler a fair bit, and any use of -Clink-dead-code makes me uncomfortable because that flag isn't just about linkage, it has to toggle off other dead code pruning mechanisms in the compiler in order to produce all the dead code. And many of those code paths that it exercises are not well-tested and certainly are not well-exercised in production use-cases.

cargo-fuzz has run into two ICEs that I know of as a result of using this flag:
rust-lang/rust#135515
#323
(both of these are bugs in the compiler that wouldn't be hit without -Clink-dead-code)

The LLVM issue that motivated using -Clink-dead-code when also using coverage instrumentation is llvm/llvm-project#33984, and it has been closed for years now. It sounds like this is fully fixed with LLD which is the default on nightly toolchains, but not on stable.

Someone should check if GNU's linker still causes trouble if used with --strip-dead-code.

It looks like in some builds, -Clink-dead-code can have a dramatic impact on disk space: bytecodealliance/wasmtime#3875 which might be another motivation for swapping the default behavior of cargo-fuzz.

@saethlin
Copy link
Contributor Author

-Clink-dead-code was originally introduced by #193, which reports trouble running fuzzers on https://github.com/cloudflare/quiche, and based on the dates I'm guessing the repo was at commit fe87e498eb03e3794988d8482dbba49b6bda4ad8 at that time.

I can run the cited quiche fuzzer on Debian bullseye using the provided command cargo fuzz run packet_recv_client --release on nightly-2019-11-17, using cargo-fuzz 0.4.5.

So my only guess is that the reason this now works even if I follow all the directions for the reproducer is that Debian bullseye shipped a fix to the GNU ld linker, my bullseye container says GNU ld (GNU Binutils for Debian) 2.35.2.

@Manishearth
Copy link
Member

I'm in favor of removing this flag!

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 16, 2025
Update docs for `-Clink-dead-code` to discourage its use

The `-Clink-dead-code` flag was originally added way back in rust-lang#31368, apparently to help improve the output of some older forms of code coverage measurement, and also to address some use-cases for wanting to suppress linker flags like `-dead_strip` and `--gc-section`.

In the past it might have also been useful in conjunction with `-Cinstrument-coverage`, but subsequent improvements to coverage instrumentation have made it unnecessary there.

[It is also currently used by cargo-fuzz by default](rust-fuzz/cargo-fuzz#391), for reasons that are possibly no longer relevant.

---

The flag currently does more than its name suggests, affecting not just linker flags, but also monomorphization decisions. It has also contributed to ICEs (e.g. rust-lang#135515) that would not have occurred without link-dead-code.

---

For now, this PR just updates the documentation to be more realistic about what the flag does, and when it should be used (approximately never). In the future, it might be worth looking into properly deprecating this flag, and perhaps making it a no-op if feasible.
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 17, 2025
Rollup merge of rust-lang#135561 - Zalathar:link-dead-code, r=saethlin

Update docs for `-Clink-dead-code` to discourage its use

The `-Clink-dead-code` flag was originally added way back in rust-lang#31368, apparently to help improve the output of some older forms of code coverage measurement, and also to address some use-cases for wanting to suppress linker flags like `-dead_strip` and `--gc-section`.

In the past it might have also been useful in conjunction with `-Cinstrument-coverage`, but subsequent improvements to coverage instrumentation have made it unnecessary there.

[It is also currently used by cargo-fuzz by default](rust-fuzz/cargo-fuzz#391), for reasons that are possibly no longer relevant.

---

The flag currently does more than its name suggests, affecting not just linker flags, but also monomorphization decisions. It has also contributed to ICEs (e.g. rust-lang#135515) that would not have occurred without link-dead-code.

---

For now, this PR just updates the documentation to be more realistic about what the flag does, and when it should be used (approximately never). In the future, it might be worth looking into properly deprecating this flag, and perhaps making it a no-op if feasible.
@Xuanwo
Copy link
Contributor

Xuanwo commented Jan 17, 2025

Hi, I'm happy to help with the implementation. To avoid disrupting existing users, perhaps we could simply change the default value of stripe-dead-code to true?

@fitzgen
Copy link
Member

fitzgen commented Jan 22, 2025

perhaps we could simply change the default value of stripe-dead-code to true?

Sounds great 👍

@Xuanwo
Copy link
Contributor

Xuanwo commented Jan 22, 2025

Nice, please assign this issue to me. I will give it fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants