Skip to content

Commit 80fb3f3

Browse files
committed
Auto merge of #74576 - myfreeweb:freebsd-sanitizers, r=oli-obk
Add sanitizer support on FreeBSD Restarting #47337. Everything is better now, no more weird llvm problems, well not everything: Unfortunately, the sanitizers don't have proper support for versioned symbols (google/sanitizers#628), so `libc`'s usage of `stat@FBSD_1.0` and so on explodes, e.g. in calling `std::fs::metadata`. Building std (now easy thanks to cargo `-Zbuild-std`) and libc with `freebsd12/13` config via the `LIBC_CI=1` env variable is a good workaround… ``` LIBC_CI=1 RUSTFLAGS="-Z sanitizer=address" cargo +san-test -Zbuild-std run --target x86_64-unknown-freebsd --verbose ``` …*except* std won't build because there's no `st_lspare` in the ino64 version of the struct, so an std patch is required: ```diff --- i/src/libstd/os/freebsd/fs.rs +++ w/src/libstd/os/freebsd/fs.rs @@ -66,8 +66,6 @@ pub trait MetadataExt { fn st_flags(&self) -> u32; #[stable(feature = "metadata_ext2", since = "1.8.0")] fn st_gen(&self) -> u32; - #[stable(feature = "metadata_ext2", since = "1.8.0")] - fn st_lspare(&self) -> u32; } #[stable(feature = "metadata_ext", since = "1.1.0")] @@ -136,7 +134,4 @@ impl MetadataExt for Metadata { fn st_flags(&self) -> u32 { self.as_inner().as_inner().st_flags as u32 } - fn st_lspare(&self) -> u32 { - self.as_inner().as_inner().st_lspare as u32 - } } ``` I guess std could like.. detect that `libc` isn't built for the old ABI, and replace the implementation of `st_lspare` with a panic?
2 parents 1e58871 + ddbc456 commit 80fb3f3

File tree

7 files changed

+35
-8
lines changed

7 files changed

+35
-8
lines changed

Diff for: library/std/build.rs

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ fn main() {
1616
} else if target.contains("freebsd") {
1717
println!("cargo:rustc-link-lib=execinfo");
1818
println!("cargo:rustc-link-lib=pthread");
19+
if env::var("RUST_STD_FREEBSD_12_ABI").is_ok() {
20+
println!("cargo:rustc-cfg=freebsd12");
21+
}
1922
} else if target.contains("netbsd") {
2023
println!("cargo:rustc-link-lib=pthread");
2124
println!("cargo:rustc-link-lib=rt");

Diff for: library/std/src/os/freebsd/fs.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,14 @@ pub trait MetadataExt {
7474
impl MetadataExt for Metadata {
7575
#[allow(deprecated)]
7676
fn as_raw_stat(&self) -> &raw::stat {
77-
unsafe { &*(self.as_inner().as_inner() as *const libc::stat as *const raw::stat) }
77+
// The methods below use libc::stat, so they work fine when libc is built with FreeBSD 12 ABI.
78+
// This method would just return nonsense.
79+
#[cfg(freebsd12)]
80+
panic!("as_raw_stat not supported with FreeBSD 12 ABI");
81+
#[cfg(not(freebsd12))]
82+
unsafe {
83+
&*(self.as_inner().as_inner() as *const libc::stat as *const raw::stat)
84+
}
7885
}
7986
fn st_dev(&self) -> u64 {
8087
self.as_inner().as_inner().st_dev as u64
@@ -136,6 +143,11 @@ impl MetadataExt for Metadata {
136143
fn st_flags(&self) -> u32 {
137144
self.as_inner().as_inner().st_flags as u32
138145
}
146+
#[cfg(freebsd12)]
147+
fn st_lspare(&self) -> u32 {
148+
panic!("st_lspare not supported with FreeBSD 12 ABI");
149+
}
150+
#[cfg(not(freebsd12))]
139151
fn st_lspare(&self) -> u32 {
140152
self.as_inner().as_inner().st_lspare as u32
141153
}

Diff for: src/bootstrap/native.rs

+1
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,7 @@ fn supported_sanitizers(
794794
}
795795
"x86_64-apple-darwin" => darwin_libs("osx", &["asan", "lsan", "tsan"]),
796796
"x86_64-fuchsia" => common_libs("fuchsia", "x86_64", &["asan"]),
797+
"x86_64-unknown-freebsd" => common_libs("freebsd", "x86_64", &["asan", "msan", "tsan"]),
797798
"x86_64-unknown-linux-gnu" => {
798799
common_libs("linux", "x86_64", &["asan", "lsan", "msan", "tsan"])
799800
}

Diff for: src/ci/docker/host-x86_64/dist-x86_64-freebsd/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ ENV \
2929

3030
ENV HOSTS=x86_64-unknown-freebsd
3131

32-
ENV RUST_CONFIGURE_ARGS --enable-extended --enable-profiler --disable-docs
32+
ENV RUST_CONFIGURE_ARGS --enable-extended --enable-profiler --enable-sanitizers --disable-docs
3333
ENV SCRIPT python3 ../x.py dist --host $HOSTS --target $HOSTS

Diff for: src/librustc_codegen_ssa/back/link.rs

+1
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ fn link_sanitizer_runtime(sess: &Session, linker: &mut dyn Linker, name: &str) {
822822
"aarch64-fuchsia"
823823
| "aarch64-unknown-linux-gnu"
824824
| "x86_64-fuchsia"
825+
| "x86_64-unknown-freebsd"
825826
| "x86_64-unknown-linux-gnu" => {
826827
let filename = format!("librustc{}_rt.{}.a", channel, name);
827828
let path = default_tlib.join(&filename);

Diff for: src/librustc_session/session.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1453,14 +1453,19 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
14531453
"aarch64-unknown-linux-gnu",
14541454
"x86_64-apple-darwin",
14551455
"x86_64-fuchsia",
1456+
"x86_64-unknown-freebsd",
14561457
"x86_64-unknown-linux-gnu",
14571458
];
14581459
const LSAN_SUPPORTED_TARGETS: &[&str] =
14591460
&["aarch64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu"];
14601461
const MSAN_SUPPORTED_TARGETS: &[&str] =
1461-
&["aarch64-unknown-linux-gnu", "x86_64-unknown-linux-gnu"];
1462-
const TSAN_SUPPORTED_TARGETS: &[&str] =
1463-
&["aarch64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu"];
1462+
&["aarch64-unknown-linux-gnu", "x86_64-unknown-freebsd", "x86_64-unknown-linux-gnu"];
1463+
const TSAN_SUPPORTED_TARGETS: &[&str] = &[
1464+
"aarch64-unknown-linux-gnu",
1465+
"x86_64-apple-darwin",
1466+
"x86_64-unknown-freebsd",
1467+
"x86_64-unknown-linux-gnu",
1468+
];
14641469

14651470
// Sanitizers can only be used on some tested platforms.
14661471
for s in sess.opts.debugging_opts.sanitizer {

Diff for: src/tools/compiletest/src/util.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,22 @@ pub const ASAN_SUPPORTED_TARGETS: &'static [&'static str] = &[
8787
"aarch64-unknown-linux-gnu",
8888
"x86_64-apple-darwin",
8989
"x86_64-fuchsia",
90+
"x86_64-unknown-freebsd",
9091
"x86_64-unknown-linux-gnu",
9192
];
9293

9394
pub const LSAN_SUPPORTED_TARGETS: &'static [&'static str] =
9495
&["aarch64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu"];
9596

9697
pub const MSAN_SUPPORTED_TARGETS: &'static [&'static str] =
97-
&["aarch64-unknown-linux-gnu", "x86_64-unknown-linux-gnu"];
98+
&["aarch64-unknown-linux-gnu", "x86_64-unknown-freebsd", "x86_64-unknown-linux-gnu"];
9899

99-
pub const TSAN_SUPPORTED_TARGETS: &'static [&'static str] =
100-
&["aarch64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu"];
100+
pub const TSAN_SUPPORTED_TARGETS: &'static [&'static str] = &[
101+
"aarch64-unknown-linux-gnu",
102+
"x86_64-apple-darwin",
103+
"x86_64-unknown-freebsd",
104+
"x86_64-unknown-linux-gnu",
105+
];
101106

102107
const BIG_ENDIAN: &'static [&'static str] = &[
103108
"armebv7r",

0 commit comments

Comments
 (0)