Skip to content

Commit 9ee5543

Browse files
committed
Create x86_64_asan-unknown-linux-gnu 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 9b82a4f commit 9ee5543

File tree

7 files changed

+90
-0
lines changed

7 files changed

+90
-0
lines changed

compiler/rustc_target/src/spec/mod.rs

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

18021802
("x86_64-pc-cygwin", x86_64_pc_cygwin),
1803+
1804+
("x86_64_asan-unknown-linux-gnu", x86_64_asan_unknown_linux_gnu),
18031805
}
18041806

18051807
/// Cow-Vec-Str: Cow<'static, [Cow<'static, str>]>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use crate::spec::{
2+
Arch, Cc, LinkerFlavor, Lld, SanitizerSet, StackProbeType, Target, TargetOptions, TargetMetadata, base,
3+
};
4+
5+
pub(crate) fn target() -> Target {
6+
let mut base = base::linux_gnu::opts();
7+
base.cpu = "x86-64".into();
8+
base.plt_by_default = false;
9+
base.max_atomic_width = Some(64);
10+
base.add_pre_link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &["-m64"]);
11+
base.stack_probes = StackProbeType::Inline;
12+
base.static_position_independent_executables = true;
13+
base.supported_sanitizers = SanitizerSet::ADDRESS;
14+
base.default_sanitizers = SanitizerSet::ADDRESS;
15+
base.supports_xray = true;
16+
17+
Target {
18+
llvm_target: "x86_64-unknown-linux-gnu".into(),
19+
metadata: TargetMetadata {
20+
description: Some("64-bit Linux (kernel 3.2+, glibc 2.17+)".into()),
21+
tier: Some(1),
22+
host_tools: Some(true),
23+
std: Some(true),
24+
},
25+
pointer_width: 64,
26+
data_layout:
27+
"e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128".into(),
28+
arch: Arch::X86_64,
29+
options: base,
30+
}
31+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ pub(crate) fn is_ci_llvm_available_for_target(
209209
("i686-pc-windows-msvc", false),
210210
("i686-unknown-linux-gnu", false),
211211
("x86_64-unknown-linux-gnu", true),
212+
("x86_64_asan-unknown-linux-gnu", false),
212213
("x86_64-apple-darwin", true),
213214
("x86_64-pc-windows-gnu", true),
214215
("x86_64-pc-windows-msvc", true),
@@ -1293,6 +1294,11 @@ fn supported_sanitizers(
12931294
"x86_64",
12941295
&["asan", "dfsan", "lsan", "msan", "safestack", "tsan", "rtsan"],
12951296
),
1297+
"x86_64_asan-unknown-linux-gnu" => common_libs(
1298+
"linux",
1299+
"x86_64",
1300+
&["asan"],
1301+
),
12961302
"x86_64-unknown-linux-musl" => {
12971303
common_libs("linux", "x86_64", &["asan", "lsan", "msan", "tsan"])
12981304
}

src/bootstrap/src/core/sanity.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const STAGE0_MISSING_TARGETS: &[&str] = &[
4242
// just a dummy comment so the list doesn't get onelined
4343
"riscv64gc-unknown-redox",
4444
"hexagon-unknown-qurt",
45+
"x86_64_asan-unknown-linux-gnu",
4546
];
4647

4748
/// 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
@@ -150,5 +150,6 @@
150150
- [x86_64-pc-cygwin](platform-support/x86_64-pc-cygwin.md)
151151
- [x86_64-unknown-linux-none](platform-support/x86_64-unknown-linux-none.md)
152152
- [x86_64-unknown-none](platform-support/x86_64-unknown-none.md)
153+
- [x86_64_asan-unknown-linux-gnu](platform-support/x86_64_asan-unknown-linux-gnu.md)
153154
- [xtensa-\*-none-elf](platform-support/xtensa.md)
154155
- [\*-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
@@ -448,6 +448,7 @@ target | std | host | notes
448448
[`x86_64-win7-windows-msvc`](platform-support/win7-windows-msvc.md) | ✓ | ✓ | 64-bit Windows 7 support
449449
[`x86_64-wrs-vxworks`](platform-support/vxworks.md) | ✓ | |
450450
[`x86_64h-apple-darwin`](platform-support/x86_64h-apple-darwin.md) | ✓ | ✓ | macOS with late-gen Intel (at least Haswell)
451+
[`x86_64_asan-unknown-linux-gnu`](platform-support/x86_64_asan-unknown_linux-gnu.md) | ✓ | | 64-bit Linux (kernel 3.2+, glibc 2.17+) with ASAN enabled by default
451452
[`xtensa-esp32-espidf`](platform-support/esp-idf.md) | ✓ | | Xtensa ESP32
452453
[`xtensa-esp32-none-elf`](platform-support/xtensa.md) | * | | Xtensa ESP32
453454
[`xtensa-esp32s2-espidf`](platform-support/esp-idf.md) | ✓ | | Xtensa ESP32-S2
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# `x86_64_asan-unknown-linux-gnu`
2+
3+
**Tier: 3**
4+
5+
Target mirroring `x86_64-unknown-linux-gnu` with AddressSanitizer enabled by
6+
default.
7+
8+
## Target maintainers
9+
10+
- [@jakos-sec](https://github.com/jakos-sec)
11+
- [@rust-lang/project-exploit-mitigations][project-exploit-mitigations]
12+
13+
## Requirements
14+
15+
The target is for cross-compilation only. Host tools are not supported, since
16+
there is no need to have the host tools instrumented with ASAN. std is fully
17+
supported.
18+
19+
In all other aspects the target is equivalent to `x86_64-unknown-linux-gnu`.
20+
21+
## Building the target
22+
23+
The target can be built by enabling it for a rustc build:
24+
25+
[build]
26+
target = ["x86_64_asan-unknown-linux-gnu"]
27+
28+
## Building Rust programs
29+
30+
Rust does not yet ship pre-compiled artifacts for this target. To compile for
31+
this target, you will either need to build Rust with the target enabled (see
32+
"Building the target" above), or build your own copy of `core` by using
33+
`build-std` or similar.
34+
35+
Compilation can be done with:
36+
37+
```
38+
rustc --target x86_64_asan-unknown-linux-gnu your-code.rs
39+
```
40+
41+
## Testing
42+
43+
Created binaries will run on Linux without any external requirements.
44+
45+
## Cross-compilation toolchains and C code
46+
47+
The target supports C code and should use the same toolchain target as
48+
`x86_64-unknown-linux-gnu`.

0 commit comments

Comments
 (0)