Skip to content

Commit 82403ea

Browse files
jakos-seceholk
andcommitted
Use lib-asan sysroot when ASAN is enabled
This commit also adds a unstable rustc flag to toggle using prebuilt libraries. Since we are not yet distributing the prebuilt artifacts, it is currently set to `no`. Eventually, with cargo and `-Z build-std` we want to disable using the prebuilt standard libraries (which is why it is should later be `yes` by default). Co-authored-by: Eric Holk <ericholk@microsoft.com>
1 parent c9537a9 commit 82403ea

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

compiler/rustc_session/src/filesearch.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
4949
sysroot.join(rustlib_path).join("lib")
5050
}
5151

52+
pub fn make_target_lib_path_with_asan(sysroot: &Path, target_triple: &str) -> PathBuf {
53+
let rustlib_path = rustc_target::relative_target_rustlib_path(sysroot, target_triple);
54+
sysroot.join(rustlib_path).join("lib-asan")
55+
}
56+
5257
/// Returns a path to the target's `bin` folder within its `rustlib` path in the sysroot. This is
5358
/// where binaries are usually installed, e.g. the self-contained linkers, lld-wrappers, LLVM tools,
5459
/// etc.

compiler/rustc_session/src/options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2591,6 +2591,8 @@ written to standard error output)"),
25912591
"enable origins tracking in MemorySanitizer"),
25922592
sanitizer_recover: SanitizerSet = (SanitizerSet::empty(), parse_sanitizers, [TRACKED],
25932593
"enable recovery for selected sanitizers"),
2594+
sanitizer_use_prebuilt_library: bool = (false, parse_bool, [TRACKED],
2595+
"enable using the prebuilt sanitizer instrumented library artifacts (default: no)"),
25942596
saturating_float_casts: Option<bool> = (None, parse_opt_bool, [TRACKED],
25952597
"make float->int casts UB-free: numbers outside the integer type's range are clipped to \
25962598
the max/min integer respectively, and NaN is mapped to 0 (default: yes)"),

compiler/rustc_session/src/search_paths.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_macros::{Decodable, Encodable, HashStable_Generic};
55
use rustc_target::spec::TargetTuple;
66

77
use crate::EarlyDiagCtxt;
8-
use crate::filesearch::make_target_lib_path;
8+
use crate::filesearch::{make_target_lib_path, make_target_lib_path_with_asan};
99

1010
#[derive(Clone, Debug)]
1111
pub struct SearchPath {
@@ -131,6 +131,10 @@ impl SearchPath {
131131
Self::new(PathKind::All, make_target_lib_path(sysroot, triple))
132132
}
133133

134+
pub fn from_sysroot_and_triple_with_asan(sysroot: &Path, triple: &str) -> Self {
135+
Self::new(PathKind::All, make_target_lib_path_with_asan(sysroot, triple))
136+
}
137+
134138
pub fn new(kind: PathKind, dir: PathBuf) -> Self {
135139
// Get the files within the directory.
136140
let mut files = match std::fs::read_dir(&dir) {

compiler/rustc_session/src/session.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1088,12 +1088,18 @@ pub fn build_session(
10881088
let mut psess = ParseSess::with_dcx(dcx, source_map);
10891089
psess.assume_incomplete_release = sopts.unstable_opts.assume_incomplete_release;
10901090

1091+
let asan = sopts.unstable_opts.sanitizer.contains(SanitizerSet::ADDRESS);
1092+
10911093
let host_triple = config::host_tuple();
10921094
let target_triple = sopts.target_triple.tuple();
10931095
// FIXME use host sysroot?
10941096
let host_tlib_path =
10951097
Arc::new(SearchPath::from_sysroot_and_triple(sopts.sysroot.path(), host_triple));
1096-
let target_tlib_path = if host_triple == target_triple {
1098+
let target_tlib_path = if asan && sopts.unstable_opts.sanitizer_use_prebuilt_library {
1099+
// If ASAN is enabled, we need to use the target lib path with ASAN
1100+
// enabled, which is different from the host lib path.
1101+
Arc::new(SearchPath::from_sysroot_and_triple_with_asan(sopts.sysroot.path(), target_triple))
1102+
} else if host_triple == target_triple {
10971103
// Use the same `SearchPath` if host and target triple are identical to avoid unnecessary
10981104
// rescanning of the target lib path and an unnecessary allocation.
10991105
Arc::clone(&host_tlib_path)

0 commit comments

Comments
 (0)