Skip to content

Commit d8386fb

Browse files
committed
Auto merge of #118521 - dpaoliello:asan, r=<try>
Enable address sanitizer for MSVC targets using INFERASANLIBS linker flag
2 parents 835ed00 + 182ea6f commit d8386fb

File tree

7 files changed

+74
-20
lines changed

7 files changed

+74
-20
lines changed

.github/workflows/ci.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -578,10 +578,12 @@ jobs:
578578
strategy:
579579
matrix:
580580
include:
581-
- name: dist-x86_64-linux
581+
- name: dist-x86_64-msvc
582582
env:
583-
CODEGEN_BACKENDS: "llvm,cranelift"
584-
os: ubuntu-20.04-16core-64gb
583+
RUST_CONFIGURE_ARGS: "--build=x86_64-pc-windows-msvc --host=x86_64-pc-windows-msvc --target=x86_64-pc-windows-msvc --enable-full-tools --enable-profiler"
584+
SCRIPT: python x.py build --set rust.debug=true opt-dist && PGO_HOST=x86_64-pc-windows-msvc ./build/x86_64-pc-windows-msvc/stage0-tools-bin/opt-dist windows-ci -- python x.py dist bootstrap --include-default-paths
585+
DIST_REQUIRE_ALL_TOOLS: 1
586+
os: windows-2019-8core-32gb
585587
timeout-minutes: 600
586588
runs-on: "${{ matrix.os }}"
587589
steps:

compiler/rustc_codegen_ssa/src/back/link.rs

+26-10
Original file line numberDiff line numberDiff line change
@@ -1186,15 +1186,22 @@ mod win {
11861186
}
11871187
}
11881188

1189-
fn add_sanitizer_libraries(sess: &Session, crate_type: CrateType, linker: &mut dyn Linker) {
1189+
fn add_sanitizer_libraries(
1190+
sess: &Session,
1191+
flavor: LinkerFlavor,
1192+
crate_type: CrateType,
1193+
linker: &mut dyn Linker,
1194+
) {
11901195
// On macOS the runtimes are distributed as dylibs which should be linked to
11911196
// both executables and dynamic shared objects. Everywhere else the runtimes
11921197
// are currently distributed as static libraries which should be linked to
11931198
// executables only.
11941199
let needs_runtime = !sess.target.is_like_android
11951200
&& match crate_type {
11961201
CrateType::Executable => true,
1197-
CrateType::Dylib | CrateType::Cdylib | CrateType::ProcMacro => sess.target.is_like_osx,
1202+
CrateType::Dylib | CrateType::Cdylib | CrateType::ProcMacro => {
1203+
sess.target.is_like_osx || sess.target.is_like_msvc
1204+
}
11981205
CrateType::Rlib | CrateType::Staticlib => false,
11991206
};
12001207

@@ -1204,26 +1211,31 @@ fn add_sanitizer_libraries(sess: &Session, crate_type: CrateType, linker: &mut d
12041211

12051212
let sanitizer = sess.opts.unstable_opts.sanitizer;
12061213
if sanitizer.contains(SanitizerSet::ADDRESS) {
1207-
link_sanitizer_runtime(sess, linker, "asan");
1214+
link_sanitizer_runtime(sess, flavor, linker, "asan");
12081215
}
12091216
if sanitizer.contains(SanitizerSet::LEAK) {
1210-
link_sanitizer_runtime(sess, linker, "lsan");
1217+
link_sanitizer_runtime(sess, flavor, linker, "lsan");
12111218
}
12121219
if sanitizer.contains(SanitizerSet::MEMORY) {
1213-
link_sanitizer_runtime(sess, linker, "msan");
1220+
link_sanitizer_runtime(sess, flavor, linker, "msan");
12141221
}
12151222
if sanitizer.contains(SanitizerSet::THREAD) {
1216-
link_sanitizer_runtime(sess, linker, "tsan");
1223+
link_sanitizer_runtime(sess, flavor, linker, "tsan");
12171224
}
12181225
if sanitizer.contains(SanitizerSet::HWADDRESS) {
1219-
link_sanitizer_runtime(sess, linker, "hwasan");
1226+
link_sanitizer_runtime(sess, flavor, linker, "hwasan");
12201227
}
12211228
if sanitizer.contains(SanitizerSet::SAFESTACK) {
1222-
link_sanitizer_runtime(sess, linker, "safestack");
1229+
link_sanitizer_runtime(sess, flavor, linker, "safestack");
12231230
}
12241231
}
12251232

1226-
fn link_sanitizer_runtime(sess: &Session, linker: &mut dyn Linker, name: &str) {
1233+
fn link_sanitizer_runtime(
1234+
sess: &Session,
1235+
flavor: LinkerFlavor,
1236+
linker: &mut dyn Linker,
1237+
name: &str,
1238+
) {
12271239
fn find_sanitizer_runtime(sess: &Session, filename: &str) -> PathBuf {
12281240
let session_tlib =
12291241
filesearch::make_target_lib_path(&sess.sysroot, sess.opts.target_triple.triple());
@@ -1254,6 +1266,10 @@ fn link_sanitizer_runtime(sess: &Session, linker: &mut dyn Linker, name: &str) {
12541266
let rpath = path.to_str().expect("non-utf8 component in path");
12551267
linker.args(&["-Wl,-rpath", "-Xlinker", rpath]);
12561268
linker.link_dylib(&filename, false, true);
1269+
} else if sess.target.is_like_msvc && flavor == LinkerFlavor::Msvc(Lld::No) && name == "asan" {
1270+
// MSVC provides the `/INFERASANLIBS` argument to automatically find the
1271+
// compatible ASAN library.
1272+
linker.arg("/INFERASANLIBS");
12571273
} else {
12581274
let filename = format!("librustc{channel}_rt.{name}.a");
12591275
let path = find_sanitizer_runtime(sess, &filename).join(&filename);
@@ -2076,7 +2092,7 @@ fn linker_with_args<'a>(
20762092
);
20772093

20782094
// Sanitizer libraries.
2079-
add_sanitizer_libraries(sess, crate_type, cmd);
2095+
add_sanitizer_libraries(sess, flavor, crate_type, cmd);
20802096

20812097
// Object code from the current crate.
20822098
// Take careful note of the ordering of the arguments we pass to the linker

compiler/rustc_session/src/session.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1574,7 +1574,10 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
15741574
}
15751575

15761576
// Cannot enable crt-static with sanitizers on Linux
1577-
if sess.crt_static(None) && !sess.opts.unstable_opts.sanitizer.is_empty() {
1577+
if sess.crt_static(None)
1578+
&& !sess.opts.unstable_opts.sanitizer.is_empty()
1579+
&& !sess.target.is_like_msvc
1580+
{
15781581
sess.emit_err(errors::CannotEnableCrtStaticLinux);
15791582
}
15801583

compiler/rustc_target/src/spec/targets/i686_pc_windows_msvc.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use crate::spec::{base, LinkerFlavor, Lld, Target};
1+
use crate::spec::{base, LinkerFlavor, Lld, SanitizerSet, Target};
22

33
pub fn target() -> Target {
44
let mut base = base::windows_msvc::opts();
55
base.cpu = "pentium4".into();
66
base.max_atomic_width = Some(64);
7+
base.supported_sanitizers = SanitizerSet::ADDRESS;
78

89
base.add_pre_link_args(
910
LinkerFlavor::Msvc(Lld::No),

compiler/rustc_target/src/spec/targets/x86_64_pc_windows_msvc.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use crate::spec::{base, Target};
1+
use crate::spec::{base, SanitizerSet, Target};
22

33
pub fn target() -> Target {
44
let mut base = base::windows_msvc::opts();
55
base.cpu = "x86-64".into();
66
base.plt_by_default = false;
77
base.max_atomic_width = Some(64);
8+
base.supported_sanitizers = SanitizerSet::ADDRESS;
89

910
Target {
1011
llvm_target: "x86_64-pc-windows-msvc".into(),

src/bootstrap/src/core/build_steps/test.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1940,6 +1940,30 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the
19401940
}
19411941
}
19421942

1943+
// Special setup to enable running with sanitizers enabled on MSVC.
1944+
if !builder.config.dry_run()
1945+
&& matches!(suite, "run-make" | "run-make-fulldeps")
1946+
&& target.contains("msvc")
1947+
&& builder.config.sanitizers_enabled(target)
1948+
{
1949+
// Ingore interception failures: not all dlls in the process will have been built with
1950+
// address sanitizer enabled (e.g., ntdll.dll).
1951+
cmd.env("ASAN_WIN_CONTINUE_ON_INTERCEPTION_FAILURE", "1");
1952+
// Add the address sanitizer runtime to the PATH.
1953+
let asan_runtime_path =
1954+
builder.cc.borrow()[&target].path().parent().unwrap().to_path_buf();
1955+
let old_path = cmd
1956+
.get_envs()
1957+
.find_map(|(k, v)| (k == "PATH").then_some(v))
1958+
.flatten()
1959+
.map_or_else(|| env::var_os("PATH").unwrap_or_default(), |v| v.to_owned());
1960+
let new_path = env::join_paths(
1961+
env::split_paths(&old_path).chain(std::iter::once(asan_runtime_path)),
1962+
)
1963+
.expect("Could not add ASAN runtime path to PATH");
1964+
cmd.env("PATH", new_path);
1965+
}
1966+
19431967
// Some UI tests trigger behavior in rustc where it reads $CARGO and changes behavior if it exists.
19441968
// To make the tests work that rely on it not being set, make sure it is not set.
19451969
cmd.env_remove("CARGO");

src/ci/github-actions/ci.yml

+11-4
Original file line numberDiff line numberDiff line change
@@ -727,11 +727,18 @@ jobs:
727727
strategy:
728728
matrix:
729729
include:
730-
- &dist-x86_64-linux
731-
name: dist-x86_64-linux
730+
- &dist-x86_64-msvc
731+
name: dist-x86_64-msvc
732732
env:
733-
CODEGEN_BACKENDS: llvm,cranelift
734-
<<: *job-linux-16c
733+
RUST_CONFIGURE_ARGS: >-
734+
--build=x86_64-pc-windows-msvc
735+
--host=x86_64-pc-windows-msvc
736+
--target=x86_64-pc-windows-msvc
737+
--enable-full-tools
738+
--enable-profiler
739+
SCRIPT: python x.py build --set rust.debug=true opt-dist && PGO_HOST=x86_64-pc-windows-msvc ./build/x86_64-pc-windows-msvc/stage0-tools-bin/opt-dist windows-ci -- python x.py dist bootstrap --include-default-paths
740+
DIST_REQUIRE_ALL_TOOLS: 1
741+
<<: *job-windows-8c
735742

736743

737744
master:

0 commit comments

Comments
 (0)