https://github.com/gbMattN updated https://github.com/llvm/llvm-project/pull/119387
>From b0845c970847aca0f50cc72fec6fb2334b4f10d3 Mon Sep 17 00:00:00 2001 From: gbMattN <matthew.n...@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 | 26 ++++++++++++++++++- 4 files changed, 41 insertions(+), 2 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..51a186e9596a5 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()); @@ -3432,7 +3455,8 @@ void FunctionStackPoisoner::processStaticAllocas() { 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(), _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits