Skip to content

Commit 1971438

Browse files
committed
Add kernel-address sanitizer support for freestanding targets
1 parent 0416b1a commit 1971438

File tree

18 files changed

+142
-12
lines changed

18 files changed

+142
-12
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub fn sanitize_attrs<'ll>(
6262
) -> SmallVec<[&'ll Attribute; 4]> {
6363
let mut attrs = SmallVec::new();
6464
let enabled = cx.tcx.sess.opts.unstable_opts.sanitizer - no_sanitize;
65-
if enabled.contains(SanitizerSet::ADDRESS) {
65+
if enabled.contains(SanitizerSet::ADDRESS) || enabled.contains(SanitizerSet::KERNELADDRESS) {
6666
attrs.push(llvm::AttributeKind::SanitizeAddress.create_attr(cx.llcx));
6767
}
6868
if enabled.contains(SanitizerSet::MEMORY) {

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,10 @@ pub(crate) unsafe fn llvm_optimize(
446446
sanitize_thread: config.sanitizer.contains(SanitizerSet::THREAD),
447447
sanitize_hwaddress: config.sanitizer.contains(SanitizerSet::HWADDRESS),
448448
sanitize_hwaddress_recover: config.sanitizer_recover.contains(SanitizerSet::HWADDRESS),
449+
sanitize_kernel_address: config.sanitizer.contains(SanitizerSet::KERNELADDRESS),
450+
sanitize_kernel_address_recover: config
451+
.sanitizer_recover
452+
.contains(SanitizerSet::KERNELADDRESS),
449453
})
450454
} else {
451455
None

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,8 @@ pub struct SanitizerOptions {
482482
pub sanitize_thread: bool,
483483
pub sanitize_hwaddress: bool,
484484
pub sanitize_hwaddress_recover: bool,
485+
pub sanitize_kernel_address: bool,
486+
pub sanitize_kernel_address_recover: bool,
485487
}
486488

487489
/// LLVMRelocMode

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: DefId) -> CodegenFnAttrs {
295295
if let Some(list) = attr.meta_item_list() {
296296
for item in list.iter() {
297297
if item.has_name(sym::address) {
298-
codegen_fn_attrs.no_sanitize |= SanitizerSet::ADDRESS;
298+
codegen_fn_attrs.no_sanitize |=
299+
SanitizerSet::ADDRESS | SanitizerSet::KERNELADDRESS;
299300
} else if item.has_name(sym::cfi) {
300301
codegen_fn_attrs.no_sanitize |= SanitizerSet::CFI;
301302
} else if item.has_name(sym::kcfi) {

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,8 @@ struct LLVMRustSanitizerOptions {
594594
bool SanitizeThread;
595595
bool SanitizeHWAddress;
596596
bool SanitizeHWAddressRecover;
597+
bool SanitizeKernelAddress;
598+
bool SanitizeKernelAddressRecover;
597599
};
598600

599601
extern "C" LLVMRustResult
@@ -765,15 +767,17 @@ LLVMRustOptimize(
765767
);
766768
}
767769

768-
if (SanitizerOptions->SanitizeAddress) {
770+
if (SanitizerOptions->SanitizeAddress || SanitizerOptions->SanitizeKernelAddress) {
769771
OptimizerLastEPCallbacks.push_back(
770772
[SanitizerOptions](ModulePassManager &MPM, OptimizationLevel Level) {
773+
auto CompileKernel = SanitizerOptions->SanitizeKernelAddress;
771774
#if LLVM_VERSION_LT(15, 0)
772775
MPM.addPass(RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
773776
#endif
774777
AddressSanitizerOptions opts = AddressSanitizerOptions{
775-
/*CompileKernel=*/false,
776-
SanitizerOptions->SanitizeAddressRecover,
778+
CompileKernel,
779+
SanitizerOptions->SanitizeAddressRecover
780+
|| SanitizerOptions->SanitizeKernelAddressRecover,
777781
/*UseAfterScope=*/true,
778782
AsanDetectStackUseAfterReturnMode::Runtime,
779783
};

compiler/rustc_session/src/config.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,13 @@ fn default_configuration(sess: &Session) -> CrateConfig {
10221022
let panic_strategy = sess.panic_strategy();
10231023
ret.insert((sym::panic, Some(panic_strategy.desc_symbol())));
10241024

1025-
for s in sess.opts.unstable_opts.sanitizer {
1025+
for mut s in sess.opts.unstable_opts.sanitizer {
1026+
// KASAN should use the same attribute name as ASAN, as it's still ASAN
1027+
// under the hood
1028+
if s == SanitizerSet::KERNELADDRESS {
1029+
s = SanitizerSet::ADDRESS;
1030+
}
1031+
10261032
let symbol = Symbol::intern(&s.to_string());
10271033
ret.insert((sym::sanitize, Some(symbol)));
10281034
}

compiler/rustc_session/src/options.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ mod desc {
370370
pub const parse_opt_panic_strategy: &str = parse_panic_strategy;
371371
pub const parse_oom_strategy: &str = "either `panic` or `abort`";
372372
pub const parse_relro_level: &str = "one of: `full`, `partial`, or `off`";
373-
pub const parse_sanitizers: &str = "comma separated list of sanitizers: `address`, `cfi`, `hwaddress`, `kcfi`, `leak`, `memory`, `memtag`, `shadow-call-stack`, or `thread`";
373+
pub const parse_sanitizers: &str = "comma separated list of sanitizers: `address`, `cfi`, `hwaddress`, `kcfi`, `kernel-address`, `leak`, `memory`, `memtag`, `shadow-call-stack`, or `thread`";
374374
pub const parse_sanitizer_memory_track_origins: &str = "0, 1, or 2";
375375
pub const parse_cfguard: &str =
376376
"either a boolean (`yes`, `no`, `on`, `off`, etc), `checks`, or `nochecks`";
@@ -684,6 +684,7 @@ mod parse {
684684
"address" => SanitizerSet::ADDRESS,
685685
"cfi" => SanitizerSet::CFI,
686686
"kcfi" => SanitizerSet::KCFI,
687+
"kernel-address" => SanitizerSet::KERNELADDRESS,
687688
"leak" => SanitizerSet::LEAK,
688689
"memory" => SanitizerSet::MEMORY,
689690
"memtag" => SanitizerSet::MEMTAG,

compiler/rustc_session/src/session.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -954,10 +954,10 @@ impl Session {
954954
/// Checks if LLVM lifetime markers should be emitted.
955955
pub fn emit_lifetime_markers(&self) -> bool {
956956
self.opts.optimize != config::OptLevel::No
957-
// AddressSanitizer uses lifetimes to detect use after scope bugs.
957+
// AddressSanitizer and KernelAddressSanitizer uses lifetimes to detect use after scope bugs.
958958
// MemorySanitizer uses lifetimes to detect use of uninitialized stack variables.
959959
// HWAddressSanitizer will use lifetimes to detect use after scope bugs in the future.
960-
|| self.opts.unstable_opts.sanitizer.intersects(SanitizerSet::ADDRESS | SanitizerSet::MEMORY | SanitizerSet::HWADDRESS)
960+
|| self.opts.unstable_opts.sanitizer.intersects(SanitizerSet::ADDRESS | SanitizerSet::KERNELADDRESS | SanitizerSet::MEMORY | SanitizerSet::HWADDRESS)
961961
}
962962

963963
pub fn is_proc_macro_attr(&self, attr: &Attribute) -> bool {

compiler/rustc_target/src/spec/aarch64_unknown_none.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn target() -> Target {
1515
linker_flavor: LinkerFlavor::Gnu(Cc::No, Lld::Yes),
1616
linker: Some("rust-lld".into()),
1717
features: "+v8a,+strict-align,+neon,+fp-armv8".into(),
18-
supported_sanitizers: SanitizerSet::KCFI,
18+
supported_sanitizers: SanitizerSet::KCFI | SanitizerSet::KERNELADDRESS,
1919
relocation_model: RelocModel::Static,
2020
disable_redzone: true,
2121
max_atomic_width: Some(128),

compiler/rustc_target/src/spec/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,7 @@ bitflags::bitflags! {
812812
const MEMTAG = 1 << 6;
813813
const SHADOWCALLSTACK = 1 << 7;
814814
const KCFI = 1 << 8;
815+
const KERNELADDRESS = 1 << 9;
815816
}
816817
}
817818

@@ -824,6 +825,7 @@ impl SanitizerSet {
824825
SanitizerSet::ADDRESS => "address",
825826
SanitizerSet::CFI => "cfi",
826827
SanitizerSet::KCFI => "kcfi",
828+
SanitizerSet::KERNELADDRESS => "kernel-address",
827829
SanitizerSet::LEAK => "leak",
828830
SanitizerSet::MEMORY => "memory",
829831
SanitizerSet::MEMTAG => "memtag",
@@ -866,6 +868,7 @@ impl IntoIterator for SanitizerSet {
866868
SanitizerSet::SHADOWCALLSTACK,
867869
SanitizerSet::THREAD,
868870
SanitizerSet::HWADDRESS,
871+
SanitizerSet::KERNELADDRESS,
869872
]
870873
.iter()
871874
.copied()
@@ -2339,6 +2342,7 @@ impl Target {
23392342
Some("address") => SanitizerSet::ADDRESS,
23402343
Some("cfi") => SanitizerSet::CFI,
23412344
Some("kcfi") => SanitizerSet::KCFI,
2345+
Some("kernel-address") => SanitizerSet::KERNELADDRESS,
23422346
Some("leak") => SanitizerSet::LEAK,
23432347
Some("memory") => SanitizerSet::MEMORY,
23442348
Some("memtag") => SanitizerSet::MEMTAG,

0 commit comments

Comments
 (0)