Skip to content

Commit c222a00

Browse files
committed
Create x86_64-unknown-linux-gnuasan target which enables ASAN by default
As suggested, in order to distribute sanitizer instrumented standard libraries without introducing new rustc flags, this adds a new dedicated target. With the target, we can distribute the instrumented standard libraries through a separate rustup component.
1 parent 53b6f89 commit c222a00

8 files changed

Lines changed: 81 additions & 0 deletions

File tree

compiler/rustc_target/src/spec/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,8 @@ supported_targets! {
18011801
("x86_64-lynx-lynxos178", x86_64_lynx_lynxos178),
18021802

18031803
("x86_64-pc-cygwin", x86_64_pc_cygwin),
1804+
1805+
("x86_64-unknown-linux-gnuasan", x86_64_unknown_linux_gnuasan),
18041806
}
18051807

18061808
/// Cow-Vec-Str: Cow<'static, [Cow<'static, str>]>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use crate::spec::{SanitizerSet, Target, TargetMetadata};
2+
3+
pub(crate) fn target() -> Target {
4+
let mut base = super::x86_64_unknown_linux_gnu::target();
5+
base.metadata = TargetMetadata {
6+
description: Some(
7+
"64-bit Linux (kernel 3.2+, glibc 2.17+) with ASAN enabled by default".into(),
8+
),
9+
tier: Some(2),
10+
host_tools: Some(false),
11+
std: Some(true),
12+
};
13+
base.supported_sanitizers = SanitizerSet::ADDRESS;
14+
base.default_sanitizers = SanitizerSet::ADDRESS;
15+
base
16+
}

src/bootstrap/src/core/build_steps/llvm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,7 @@ fn supported_sanitizers(
14841484
"x86_64",
14851485
&["asan", "dfsan", "lsan", "msan", "safestack", "tsan", "rtsan"],
14861486
),
1487+
"x86_64-unknown-linux-gnuasan" => common_libs("linux", "x86_64", &["asan"]),
14871488
"x86_64-unknown-linux-musl" => {
14881489
common_libs("linux", "x86_64", &["asan", "lsan", "msan", "tsan"])
14891490
}

src/bootstrap/src/core/sanity.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub struct Finder {
3838
const STAGE0_MISSING_TARGETS: &[&str] = &[
3939
// just a dummy comment so the list doesn't get onelined
4040
"riscv64im-unknown-none-elf",
41+
"x86_64-unknown-linux-gnuasan",
4142
];
4243

4344
/// Minimum version threshold for libstdc++ required when using prebuilt LLVM

src/doc/rustc/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,5 +151,6 @@
151151
- [x86_64-pc-cygwin](platform-support/x86_64-pc-cygwin.md)
152152
- [x86_64-unknown-linux-none](platform-support/x86_64-unknown-linux-none.md)
153153
- [x86_64-unknown-none](platform-support/x86_64-unknown-none.md)
154+
- [x86_64-unknown-linux-gnuasan](platform-support/x86_64-unknown-linux-gnuasan.md)
154155
- [xtensa-\*-none-elf](platform-support/xtensa.md)
155156
- [\*-nuttx-\*](platform-support/nuttx.md)

src/doc/rustc/src/platform-support.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ target | std | notes
206206
[`x86_64-apple-ios-macabi`](platform-support/apple-ios-macabi.md) | ✓ | Mac Catalyst on x86_64
207207
[`x86_64-fortanix-unknown-sgx`](platform-support/x86_64-fortanix-unknown-sgx.md) | ✓ | [Fortanix ABI] for 64-bit Intel SGX
208208
[`x86_64-linux-android`](platform-support/android.md) | ✓ | 64-bit x86 Android
209+
[`x86_64-unknown-linux-gnuasan`](platform-support/x86_64-unknown-linux-gnuasan.md) | ✓ | 64-bit Linux (kernel 3.2+, glibc 2.17+) with ASAN enabled by default
209210
[`x86_64-unknown-fuchsia`](platform-support/fuchsia.md) | ✓ | 64-bit x86 Fuchsia
210211
`x86_64-unknown-linux-gnux32` | ✓ | 64-bit Linux (x32 ABI) (kernel 4.15+, glibc 2.27)
211212
[`x86_64-unknown-none`](platform-support/x86_64-unknown-none.md) | * | Freestanding/bare-metal x86_64, softfloat
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# `x86_64-unknown-linux-gnuasan`
2+
3+
**Tier: 2**
4+
5+
Target mirroring `x86_64-unknown-linux-gnu` with AddressSanitizer enabled by
6+
default.
7+
The goal of this target is to allow shipping ASAN-instrumented standard
8+
libraries through rustup, enabling a fully instrumented binary without requiring
9+
nightly features (build-std).
10+
Once build-std stabilizes, this target is no longer needed and will be removed.
11+
12+
## Target maintainers
13+
14+
- [@jakos-sec](https://github.com/jakos-sec)
15+
- [@1c3t3a](https://github.com/1c3t3a)
16+
- [@rust-lang/project-exploit-mitigations][project-exploit-mitigations]
17+
18+
## Requirements
19+
20+
The target is for cross-compilation only. Host tools are not supported, since
21+
there is no need to have the host tools instrumented with ASAN. std is fully
22+
supported.
23+
24+
In all other aspects the target is equivalent to `x86_64-unknown-linux-gnu`.
25+
26+
## Building the target
27+
28+
The target can be built by enabling it for a rustc build:
29+
30+
```toml
31+
[build]
32+
target = ["x86_64-unknown-linux-gnuasan"]
33+
```
34+
35+
## Building Rust programs
36+
37+
Rust programs can be compiled by adding this target via rustup:
38+
39+
```sh
40+
$ rustup target add x86_64-unknown-linux-gnuasan
41+
```
42+
43+
and then compiling with the target:
44+
45+
```sh
46+
$ rustc foo.rs --target x86_64-unknown-linux-gnuasan
47+
```
48+
49+
## Testing
50+
51+
Created binaries will run on Linux without any external requirements.
52+
53+
## Cross-compilation toolchains and C code
54+
55+
The target supports C code and should use the same toolchain target as
56+
`x86_64-unknown-linux-gnu`.

tests/assembly-llvm/targets/targets-elf.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,9 @@
673673
//@ revisions: x86_64_unknown_linux_gnux32
674674
//@ [x86_64_unknown_linux_gnux32] compile-flags: --target x86_64-unknown-linux-gnux32
675675
//@ [x86_64_unknown_linux_gnux32] needs-llvm-components: x86
676+
//@ revisions: x86_64_unknown_linux_gnuasan
677+
//@ [x86_64_unknown_linux_gnuasan] compile-flags: --target x86_64-unknown-linux-gnuasan
678+
//@ [x86_64_unknown_linux_gnuasan] needs-llvm-components: x86
676679
//@ revisions: x86_64_unknown_linux_musl
677680
//@ [x86_64_unknown_linux_musl] compile-flags: --target x86_64-unknown-linux-musl
678681
//@ [x86_64_unknown_linux_musl] needs-llvm-components: x86

0 commit comments

Comments
 (0)