[clang] [compiler-rt] [llvm] [ASan] Add metadata to renamed instructions so ASan doesn't use the i… (PR #119387)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 18 03:24:41 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-compiler-rt-sanitizer
Author: None (gbMattN)
<details>
<summary>Changes</summary>
…ncorrect name
---
Full diff: https://github.com/llvm/llvm-project/pull/119387.diff
4 Files Affected:
- (modified) clang/lib/CodeGen/CGExpr.cpp (+8)
- (added) compiler-rt/test/asan/TestCases/shadowed-stack-serialization.cpp (+13)
- (modified) llvm/include/llvm/IR/FixedMetadataKinds.def (+1)
- (modified) llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp (+9-7)
``````````diff
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 5fccc9cbb37ec1..d8fdacf30e12e3 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -137,6 +137,14 @@ llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(llvm::Type *Ty,
Alloca =
new llvm::AllocaInst(Ty, CGM.getDataLayout().getAllocaAddrSpace(),
ArraySize, Name, AllocaInsertPt->getIterator());
+ if (Alloca->getName() != Name.str() &&
+ SanOpts.Mask & SanitizerKind::Address) {
+
+ llvm::LLVMContext &ctx = Alloca->getContext();
+ llvm::MDString *trueNameMetadata = llvm::MDString::get(ctx, Name.str());
+ llvm::MDTuple *tuple = llvm::MDTuple::get(ctx, trueNameMetadata);
+ Alloca->setMetadata(llvm::LLVMContext::MD_unaltered_name, tuple);
+ }
if (Allocas) {
Allocas->Add(Alloca);
}
diff --git a/compiler-rt/test/asan/TestCases/shadowed-stack-serialization.cpp b/compiler-rt/test/asan/TestCases/shadowed-stack-serialization.cpp
new file mode 100644
index 00000000000000..f4d9ad5f6ea5f7
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/shadowed-stack-serialization.cpp
@@ -0,0 +1,13 @@
+// RUN: %clangxx_asan -O0 %s -o %t
+// RUN: not %run %t 2>&1 | FileCheck %s
+
+int main()
+{
+ int x;
+ {
+ int x;
+ delete &x;
+ }
+}
+
+// CHECK: [32, 36) 'x'
diff --git a/llvm/include/llvm/IR/FixedMetadataKinds.def b/llvm/include/llvm/IR/FixedMetadataKinds.def
index df572e8791e13b..41fa34bf09ff65 100644
--- a/llvm/include/llvm/IR/FixedMetadataKinds.def
+++ b/llvm/include/llvm/IR/FixedMetadataKinds.def
@@ -53,3 +53,4 @@ LLVM_FIXED_MD_KIND(MD_DIAssignID, "DIAssignID", 38)
LLVM_FIXED_MD_KIND(MD_coro_outside_frame, "coro.outside.frame", 39)
LLVM_FIXED_MD_KIND(MD_mmra, "mmra", 40)
LLVM_FIXED_MD_KIND(MD_noalias_addrspace, "noalias.addrspace", 41)
+LLVM_FIXED_MD_KIND(MD_unaltered_name, "unaltered.name", 42)
diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
index cb84588318496c..87f79bdaa16429 100644
--- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -3430,13 +3430,15 @@ void FunctionStackPoisoner::processStaticAllocas() {
SmallVector<ASanStackVariableDescription, 16> SVD;
SVD.reserve(AllocaVec.size());
for (AllocaInst *AI : AllocaVec) {
- ASanStackVariableDescription D = {AI->getName().data(),
- ASan.getAllocaSizeInBytes(*AI),
- 0,
- AI->getAlign().value(),
- AI,
- 0,
- 0};
+ const char *Name = AI->getName().data();
+ if (AI->hasMetadata(LLVMContext::MD_unaltered_name)) {
+ MDTuple *tuple =
+ dyn_cast<MDTuple>(AI->getMetadata(LLVMContext::MD_unaltered_name));
+ Name = dyn_cast<MDString>(tuple->getOperand(0))->getString().data();
+ }
+ ASanStackVariableDescription D = {
+ Name, ASan.getAllocaSizeInBytes(*AI), 0, AI->getAlign().value(), AI, 0,
+ 0};
SVD.push_back(D);
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/119387
More information about the llvm-commits
mailing list