[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
Thu Apr 3 03:51:11 PDT 2025
https://github.com/gbMattN updated https://github.com/llvm/llvm-project/pull/119387
>From fb5ad588f299bdba727dbf8288b983359ac29480 Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Thu, 3 Apr 2025 11:50:31 +0100
Subject: [PATCH] [ASan] Add metadata to renamed instructions so ASan doesn't
use the incorrect name
---
clang/lib/CodeGen/CGExpr.cpp | 3 ++
.../shadowed-stack-serialization.cpp | 12 ++++++++
.../TestCases/use-after-scope-inlined.cpp | 2 +-
.../Instrumentation/AddressSanitizer.cpp | 28 +++++++++++++++++--
4 files changed, 42 insertions(+), 3 deletions(-)
create mode 100644 compiler-rt/test/asan/TestCases/shadowed-stack-serialization.cpp
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 9676e61cf322d..91c5e2d58f17c 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -136,6 +136,9 @@ llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(llvm::Type *Ty,
Alloca =
new llvm::AllocaInst(Ty, CGM.getDataLayout().getAllocaAddrSpace(),
ArraySize, Name, AllocaInsertPt->getIterator());
+ if (SanOpts.Mask & SanitizerKind::Address) {
+ Alloca->addAnnotationMetadata({"alloca_name_altered", Name.str()});
+ }
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 0000000000000..f2706c671c261
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/shadowed-stack-serialization.cpp
@@ -0,0 +1,12 @@
+// 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/compiler-rt/test/asan/TestCases/use-after-scope-inlined.cpp b/compiler-rt/test/asan/TestCases/use-after-scope-inlined.cpp
index 1014ff919b9ef..fbb67499ab3d7 100644
--- a/compiler-rt/test/asan/TestCases/use-after-scope-inlined.cpp
+++ b/compiler-rt/test/asan/TestCases/use-after-scope-inlined.cpp
@@ -23,5 +23,5 @@ int main(int argc, char *argv[]) {
// CHECK: Address 0x{{.*}} is located in stack of thread T0 at offset [[OFFSET:[^ ]*]] in frame
// CHECK: {{.*}} in main
// CHECK: This frame has
- // CHECK: {{\[}}[[OFFSET]], {{.*}}) 'x.i' (line [[@LINE-15]])
+ // CHECK: {{\[}}[[OFFSET]], {{.*}}) 'x' (line [[@LINE-15]])
}
diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
index bbe7040121649..3bee26a15c0c9 100644
--- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -3392,6 +3392,29 @@ static void findStoresToUninstrumentedArgAllocas(
}
}
+static StringRef getAllocaName(AllocaInst *AI) {
+ // Alloca could have been renamed for uniqueness. Its true name will have been
+ // recorded as an annotation.
+ if (AI->hasMetadata(LLVMContext::MD_annotation)) {
+ MDTuple *AllocaAnnotations =
+ cast<MDTuple>(AI->getMetadata(LLVMContext::MD_annotation));
+ for (auto &Annotation : AllocaAnnotations->operands()) {
+ if (!isa<MDTuple>(Annotation))
+ continue;
+ auto AnnotationTuple = cast<MDTuple>(Annotation);
+ for (int Index = 0; Index < AnnotationTuple->getNumOperands(); Index++) {
+ // All annotations are strings
+ auto MetadataString =
+ cast<MDString>(AnnotationTuple->getOperand(Index));
+ if (MetadataString->getString() == "alloca_name_altered")
+ return cast<MDString>(AnnotationTuple->getOperand(Index + 1))
+ ->getString();
+ }
+ }
+ }
+ return AI->getName();
+}
+
void FunctionStackPoisoner::processStaticAllocas() {
if (AllocaVec.empty()) {
assert(StaticAllocaPoisonCallVec.empty());
@@ -3426,13 +3449,14 @@ void FunctionStackPoisoner::processStaticAllocas() {
ArgInitInst->moveBefore(InsBefore->getIterator());
// If we have a call to llvm.localescape, keep it in the entry block.
- if (LocalEscapeCall)
+ if (LocalEscapeCall)
LocalEscapeCall->moveBefore(InsBefore->getIterator());
SmallVector<ASanStackVariableDescription, 16> SVD;
SVD.reserve(AllocaVec.size());
for (AllocaInst *AI : AllocaVec) {
- ASanStackVariableDescription D = {AI->getName().data(),
+ StringRef Name = getAllocaName(AI);
+ ASanStackVariableDescription D = {Name.data(),
ASan.getAllocaSizeInBytes(*AI),
0,
AI->getAlign().value(),
More information about the llvm-commits
mailing list