rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.76k stars 12.65k forks source link

Rustc tries to link the Linux ASAN runtime when linker flavour is MSVC+LLD #131363

Open danakj opened 1 week ago

danakj commented 1 week ago

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-existent librustc-dev_rt.asan.alibrary while targeting Windows.

A repro can be seen on Linux, cross-compiling for Windows. It needs a Windows sysroot and a clang/lld build:

% cargo init
    Creating binary (application) package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
% RUSTFLAGS="-Zsanitizer=address -Clinker=../c/src/third_party/llvm-build/Release+Asserts/bin/lld-link -Clink-arg=/libpath:../c/src/third_party/llvm-build/Release+Asserts/lib/clang/20/lib/windows -Clink-arg=/winsysroot:../c/src/third_party/depot_tools/win_toolchain/vs_files/7393122652" cargo build -Zbuild-std --target x86_64-pc-windows-msvc
   Compiling compiler_builtins v0.1.109
   Compiling core v0.0.0 (/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core)
   Compiling std v0.0.0 (/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std)
   Compiling rustc-std-workspace-core v1.99.0 (/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/rustc-std-workspace-core)
   Compiling alloc v0.0.0 (/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc)
   Compiling cfg-if v1.0.0
   Compiling rustc-demangle v0.1.24
   Compiling unwind v0.0.0 (/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/unwind)
   Compiling rustc-std-workspace-alloc v1.99.0 (/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/rustc-std-workspace-alloc)
   Compiling panic_unwind v0.0.0 (/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/panic_unwind)
   Compiling panic_abort v0.0.0 (/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/panic_abort)
   Compiling hashbrown v0.14.5
   Compiling std_detect v0.1.5 (/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/stdarch/crates/std_detect)
   Compiling proc_macro v0.0.0 (/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/proc_macro)
   Compiling win-asan-repro v0.1.0 (/home/danakj/s/win-asan-repro)
error: linking with `../c/src/third_party/llvm-build/Release+Asserts/bin/lld-link` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/danakj/local/bin:/home/danakj/s/gsutil:/home/danakj/s/depot_tools:/home/danakj/s/ninja:/home/danakj/s/arcanist/bin:/home/danakj/s/brew/bin:/opt/firefox:/home/danakj/.cargo/bin:/bin:/usr/bin:/home/danakj/s/quickopen:/home/danakj/.cargo/bin" VSLANG="1033" "../c/src/third_party/llvm-build/Release+Asserts/bin/lld-link" "-flavor" "link" "/NOLOGO" "/tmp/rustcCJDwzU/symbols.o" "/WHOLEARCHIVE:/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-pc-windows-msvc/lib/librustc-nightly_rt.asan.a" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/win_asan_repro.0tdtfeujdyhjtaeasbm0d4fkx.rcgu.o" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/win_asan_repro.199i99wnv1nalvuu3t1v5kp3t.rcgu.o" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/win_asan_repro.20h9jqk82sswuuobak2qzucv5.rcgu.o" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/win_asan_repro.3t56hhym87ffrajopna60eke0.rcgu.o" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/win_asan_repro.6vz5im55mbl3ovk6o6y9izcq6.rcgu.o" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/win_asan_repro.8i9ptnb83exgtut8kf28gftgy.rcgu.o" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/win_asan_repro.0vi8a9ni8tzhrhb9j22qz2uyj.rcgu.o" "/LIBPATH:/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps" "/LIBPATH:/home/danakj/s/win-asan-repro/target/debug/deps" "/LIBPATH:/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-pc-windows-msvc/lib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/libstd-34f4d1dc3afbf24b.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/libpanic_unwind-edca54b671061ee7.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/librustc_demangle-05840c8895460fd0.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/libstd_detect-fb81dcb01cb8e1fb.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/libhashbrown-2af4722702436af9.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/librustc_std_workspace_alloc-0c0095e7aff20699.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/libunwind-dc807fd73b64a6ed.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/libcfg_if-300e1ea37d4d7a24.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/liballoc-e1954f2d1c71f747.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/librustc_std_workspace_core-a5e34549c1c04a14.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/libcore-259c5404709a9645.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/libcompiler_builtins-dda445e51fa2ca93.rlib" "kernel32.lib" "advapi32.lib" "kernel32.lib" "ntdll.lib" "userenv.lib" "ws2_32.lib" "kernel32.lib" "ws2_32.lib" "kernel32.lib" "/defaultlib:msvcrt" "/NXCOMPAT" "/LIBPATH:/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-pc-windows-msvc/lib" "/OUT:/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/win_asan_repro.exe" "/OPT:REF,NOICF" "/DEBUG" "/PDBALTPATH:%_PDB%" "/libpath:../c/src/third_party/llvm-build/Release+Asserts/lib/clang/20/lib/windows" "/winsysroot:../c/src/third_party/depot_tools/win_toolchain/vs_files/7393122652"
  = note: lld-link: error: could not open '/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-pc-windows-msvc/lib/librustc-nightly_rt.asan.a': No such file or directory

The relevant parts of the command line are:

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:

## --sysroot The code here is _also ignoring `--sysroot`_. If we include a bogus but relative path `--sysroot=a/b/c` in `RUSTFLAGS`, the ASAN library should be specified as`a/b/c/lib/rustlib/x86_64-pc-windows-msvc/lib/` but it is always using the absolute path to the default sysroot.

Chromium tracking issue: crbug.com/371512562

cc: @rcvalle

danakj commented 1 week 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?

danakj commented 1 week ago

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

danakj commented 1 week ago

-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?

danakj commented 1 week ago

cc: https://github.com/rust-lang/rust/issues/39699