CFI: Fix LTO for #![no_builtins] crates with CFI#156024
CFI: Fix LTO for #![no_builtins] crates with CFI#156024rcvalle wants to merge 1 commit intorust-lang:mainfrom
#![no_builtins] crates with CFI#156024Conversation
|
r? @jieyouxu rustbot has assigned @jieyouxu. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
58c916f to
02987f4
Compare
This comment has been minimized.
This comment has been minimized.
| // | ||
| // Therefore, with `-Clinker-plugin-lto` and `-Zsanitizer=cfi`, a | ||
| // `#![no_builtins]` crate must still use rustc's `EmitObj::Bitcode` | ||
| // path (and emit LLVM bitcode in the `.o` for linker-based LTO). |
There was a problem hiding this comment.
The reason we don't use LTO for compiler-builtins is that doing so breaks the weak linkage that compiler-builtins uses. We use a legacy interface for implementing LTO that treats weak and strong symbols identically. We tried to make compiler-builtins participate in LTO before in #113923, but this resulted in the regression #118609.
In any case just emitting bitcode is not enough. You also need to updated ignored_for_lto:
rust/compiler/rustc_codegen_ssa/src/back/link.rs
Lines 1406 to 1422 in 0469a92
There was a problem hiding this comment.
This change only makes it participate on LTO when using both -Clinker-plugin-lto (not rustc LTO) and -Zsanitizer=cfi, which expects a clang or ld.lld linker already and thus doesn't cause the regressions listed on #146133, with the regression tests added by @dianqk now passing. However, I see that the regressions you mention are different ones. Are there any regression tests for them I can verify?
There was a problem hiding this comment.
Cargo uses -Clinker-plugin-lto when compiling rlib crates even when not doing linker plugin LTO to skip unnecessary codegen for them.
There was a problem hiding this comment.
I'm a bit confused about what is the issue you're referring to. ignored_for_lto is for rustc LTO (not proper LTO). The #142284 issue is when using both -Clinker-plugin-lto (proper LTO) and -Zsanitizer=cfi, where LTO is deferred to the linker; the fix needed was to ensure those crates emit LLVM bitcode so proper LTO can run the LowerTypeTests pass and lower llvm.type.test intrinsics and related metadata. (Note the issue #142284 doesn't happen with rust LTO: #142284 (comment) because these are properly lowered already.) Would you mind providing more details? Maybe a regression test or a reproducer?
There was a problem hiding this comment.
I was thinking wrong in my earlier comments. I however did just remember that you tried to land pretty much the same PR previously as #145368, which then had to be reverted in #146133 due to regressions. Has anything changed since then to fix those regressions even when compiling compiler-builtins with LTO enabled?
There was a problem hiding this comment.
Yes, in my previous attempt, I changed it to emit LLVM bitcode for whenever -Clinker-plugin-lto was used so when people used it with older non-LLVM (system) linkers, these didn't work with the LLVM bitcode. In this attempt, I changed it to emit LLVM bitcode only when CFI is enabled, which already requires a newer LLVM linker that works with the LLVM bitcode and wouldn't work with older non-LLVM (system) linkers anyway.
What I was unsure is whether there could be any case where emitting LLVM bitcode for compiler_builtins (even only when CFI is enabled) could fail for some other reason such as #118609 that you mentioned, but since it's currently blocking the use of CFI with -Zbuild-std, and I haven't seen any issues so far, and don't have a regression test or a reproducer for it, I'd rather unblock it and fix forward any issues that might appear.
But before merging it, I still want to add a couple more tests and will let you know when it's ready.
There was a problem hiding this comment.
Should be good to go for whenever you have time. Thank you!
02987f4 to
49f46f9
Compare
This comment has been minimized.
This comment has been minimized.
49f46f9 to
1a3cf09
Compare
This comment has been minimized.
This comment has been minimized.
c338aae to
6650fd5
Compare
|
r? bjorn3 (since you're already reviewing this, and know way more about this than I do) |
6650fd5 to
994844b
Compare
This comment has been minimized.
This comment has been minimized.
1e9d441 to
c9526ae
Compare
Fixes LTO for `#![no_builtins]` crates with CFI enabled by using rustc's `EmitObj::Bitcode` path (and emitting LLVM bitcode in the `.o` for linker-based LTO).
c9526ae to
b3be83c
Compare
Fixes LTO for
#![no_builtins]crates with CFI enabled by using rustc'sEmitObj::Bitcodepath (and emitting LLVM bitcode in the.ofor linker-based LTO).It also adds tests that verify that the examples in rust-cfi-examples build and run with
-Zbuild-stdto prevent other future regressions.