Open danakj opened 1 month ago
I am not sure what the actual library name should be here. It's possible it should be nothing at all. In Chromium we are using the clang runtime libraries, and we just build a stub library in the Rust sysroot for ASAN to satisfy rustc.
When targeting Linux instead of Windows, everything links.
RUSTFLAGS="-Zsanitizer=address -Clinker=../c/src/third_party/llvm-build/Release+Asserts/bin/clang -Clink-arg=-fuse-ld=lld" cargo build -Zbuild-std --target x86_64-unknown-linux-gnu
But note that the --sysroot
flag is again ignored for getting the path to the ASAN runtime. With this command line:
RUSTFLAGS="-Zsanitizer=address -Clinker=../c/src/third_party/llvm-build/Release+Asserts/bin/clang -Clink-arg=-fuse-ld=lld --sysroot=a/b/c" cargo build -Zbuild-std --target x86_64-unknown-linux-gnu
We get this rustc invocation:
/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc --crate-name win_asan_repro --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=135 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 --check-cfg 'cfg(docsrs)' --check-cfg 'cfg(feature, values())' -C metadata=e9c525448789edd3 -C extra-filename=-e9c525448789edd3 --out-dir /home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps --target x86_64-unknown-linux-gnu -C incremental=/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/incremental -L dependency=/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps -L dependency=/home/danakj/s/win-asan-repro/target/debug/deps --extern 'noprelude:alloc=/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/liballoc-9e8f7b7d78adf0cc.rlib' --extern 'noprelude:compiler_builtins=/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/libcompiler_builtins-60066699d98e70d7.rlib' --extern 'noprelude:core=/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/libcore-5a2147a47b66f300.rlib' --extern 'noprelude:panic_unwind=/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/libpanic_unwind-6f472e5a3c8f3ca9.rlib' --extern 'noprelude:proc_macro=/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/libproc_macro-eb56ba953250058e.rlib' --extern 'noprelude:std=/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/libstd-70e3eac1b552c4e3.rlib' -Z unstable-options -Zsanitizer=address -Clinker=../c/src/third_party/llvm-build/Release+Asserts/bin/clang -Clink-arg=-fuse-ld=lld --sysroot=a/b/c
And when we inject an invalid -Clink-arg
to see the linker command, it is:
"../c/src/third_party/llvm-build/Release+Asserts/bin/clang" "-m64" "/tmp/rustcclsKpy/symbols.o" "-Wl,-Bstatic" "-Wl,--whole-archive" "-Wl,/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc-nightly_rt.asan.a" "-Wl,--no-whole-archive" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/win_asan_repro-e9c525448789edd3.149k154caliqi0o5prs6b0tju.rcgu.o" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/win_asan_repro-e9c525448789edd3.6xhvv3ztp0cwt7c06xi2tdz71.rcgu.o" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/win_asan_repro-e9c525448789edd3.7lszw8m7qckwnfhnfknph7xgw.rcgu.o" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/win_asan_repro-e9c525448789edd3.cybst40sznlns6zaoju7reyyi.rcgu.o" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/win_asan_repro-e9c525448789edd3.e3w5y3szztjs1fjrn5vriqcnw.rcgu.o" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/win_asan_repro-e9c525448789edd3.eee1xl7jriwhejeomnx8xj31x.rcgu.o" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/win_asan_repro-e9c525448789edd3.50j4g207ietp7nqrpsab4v4p9.rcgu.o" "-Wl,--as-needed" "-L" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps" "-L" "/home/danakj/s/win-asan-repro/target/debug/deps" "-L" "a/b/c/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/libstd-70e3eac1b552c4e3.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/libpanic_unwind-6f472e5a3c8f3ca9.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/libobject-b6d0efb61b5c5e73.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/libmemchr-a13c09c8fec9904a.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/libaddr2line-10601af51d6fb61d.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/libgimli-f3b80b9ff1acc8e2.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/librustc_demangle-3811ea18f1ceebd2.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/libstd_detect-13d58a19dfc162f1.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/libhashbrown-1ef1a11e78d359cd.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/librustc_std_workspace_alloc-8be90772433cc184.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/libminiz_oxide-a301b84838f82e85.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/libadler-a10f950e88d286d1.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/libunwind-ab13166ba3ac7e09.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/libcfg_if-f0d5c7b007950164.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/liblibc-e3c850300c5a848d.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/liballoc-9e8f7b7d78adf0cc.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/librustc_std_workspace_core-a2cedd8e954b8067.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/libcore-5a2147a47b66f300.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/libcompiler_builtins-60066699d98e70d7.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "a/b/c/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-L" "a/b/c/lib/rustlib/x86_64-unknown-linux-gnu/lib/self-contained" "-o" "/home/danakj/s/win-asan-repro/target/x86_64-unknown-linux-gnu/debug/deps/win_asan_repro-e9c525448789edd3" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs" "-fuse-ld=lld" "x"
Which is specifying this ASAN library path: /home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc-nightly_rt.asan.a
There is, however, no ASAN runtime built by -Zbuild-std
in target/x86_64-unknown-linux-gnu/debug/deps
so maybe that's why it was not noticed until now that the --sysroot
is being ignored?
Maybe the sysroot thing is unrelated and it's just searching for and failing to find the ASAN library, then falling back to the default sysroot. And on Windows, which is where I noticed this, it's because it's looking for a Linux library, so it always falls back. In our Chromium build which uses --sysroot
I do see it choosing the ASAN runtime we provide in there: "-Wl,local_rustc_sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc-dev_rt.asan.a"
Edit: Ah yes, indeed: https://github.com/rust-lang/rust/blob/0b16baa570d26224612ea27f76d68e4c6ca135cc/compiler/rustc_codegen_ssa/src/back/link.rs#L1318-L1327
-Zexternal-clangrt
seems to solve this problem for Chromium.
I think that the branches here need to do something for MSVC+LLD that isn't "link the Linux library". Don't know if that is just doing nothing, or generating an error that sanitizers with MSVC+LLD is only supported with -Zexternal-clang-rt, or something else?
When linking with
lld-link
, which provides the MSVC flavour linker with LLD subtype, the ASAN runtime selection falls through to the Linux branch and tries to link the non-existentlibrustc-dev_rt.asan.a
library while targeting Windows.A repro can be seen on Linux, cross-compiling for Windows. It needs a Windows sysroot and a clang/lld build:
The relevant parts of the command line are:
-Zsanitizer=address -Clinker=../c/src/third_party/llvm-build/Release+Asserts/bin/lld-link"
--target x86_64-pc-windows-msvc
The library choice is made here: https://github.com/rust-lang/rust/blob/0b16baa570d26224612ea27f76d68e4c6ca135cc/compiler/rustc_codegen_ssa/src/back/link.rs#L1349-L1351
The branch above would normally be used for MSVC flavour, but it's not used for LLD, so it falls down to the else branch which is for Linux.
Edited to hide the sysroot stuff, this seems okay and unrelated:
Chromium tracking issue: crbug.com/371512562
cc: @rcvalle