From 568aeda9e955ca2a4c043bc2ae7f854e60f6103a Mon Sep 17 00:00:00 2001 From: Ivan Lozano Date: Fri, 3 Dec 2021 16:11:13 -0500 Subject: MemTagSanitizer Support Adds support for the LLVM MemTagSanitizer. --- compiler/rustc_codegen_llvm/src/attributes.rs | 13 +++++++++++++ compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 1 + 2 files changed, 14 insertions(+) (limited to 'compiler/rustc_codegen_llvm/src') diff --git a/compiler/rustc_codegen_llvm/src/attributes.rs b/compiler/rustc_codegen_llvm/src/attributes.rs index 2472789601e..4d054ec3d81 100644 --- a/compiler/rustc_codegen_llvm/src/attributes.rs +++ b/compiler/rustc_codegen_llvm/src/attributes.rs @@ -55,6 +55,19 @@ pub fn sanitize<'ll>(cx: &CodegenCx<'ll, '_>, no_sanitize: SanitizerSet, llfn: & if enabled.contains(SanitizerSet::HWADDRESS) { llvm::Attribute::SanitizeHWAddress.apply_llfn(Function, llfn); } + if enabled.contains(SanitizerSet::MEMTAG) { + // Check to make sure the mte target feature is actually enabled. + let sess = cx.tcx.sess; + let features = llvm_util::llvm_global_features(sess).join(","); + let mte_feature_enabled = features.rfind("+mte"); + let mte_feature_disabled = features.rfind("-mte"); + + if mte_feature_enabled.is_none() || (mte_feature_disabled > mte_feature_enabled) { + sess.err("`-Zsanitizer=memtag` requires `-Ctarget-feature=+mte`"); + } + + llvm::Attribute::SanitizeMemTag.apply_llfn(Function, llfn); + } } /// Tell LLVM to emit or not emit the information necessary to unwind the stack for the function. diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index 4f62c685568..bb6a2cd1cf9 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -190,6 +190,7 @@ pub enum Attribute { StackProtectStrong = 31, StackProtect = 32, NoUndef = 33, + SanitizeMemTag = 34, } /// LLVMIntPredicate -- cgit 1.4.1-3-g733a5