Skip to content

Commit b0845c9

Browse files
committed
[ASan] Add metadata to renamed instructions so ASan doesn't use the incorrect name
1 parent 025541d commit b0845c9

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(llvm::Type *Ty,
136136
Alloca =
137137
new llvm::AllocaInst(Ty, CGM.getDataLayout().getAllocaAddrSpace(),
138138
ArraySize, Name, AllocaInsertPt->getIterator());
139+
if (SanOpts.Mask & SanitizerKind::Address) {
140+
Alloca->addAnnotationMetadata({"alloca_name_altered", Name.str()});
141+
}
139142
if (Allocas) {
140143
Allocas->Add(Alloca);
141144
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %clangxx_asan -O0 %s -o %t
2+
// RUN: not %run %t 2>&1 | FileCheck %s
3+
4+
int main() {
5+
int x;
6+
{
7+
int x;
8+
delete &x;
9+
}
10+
}
11+
12+
// CHECK: [32, 36) 'x'

compiler-rt/test/asan/TestCases/use-after-scope-inlined.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ int main(int argc, char *argv[]) {
2323
// CHECK: Address 0x{{.*}} is located in stack of thread T0 at offset [[OFFSET:[^ ]*]] in frame
2424
// CHECK: {{.*}} in main
2525
// CHECK: This frame has
26-
// CHECK: {{\[}}[[OFFSET]], {{.*}}) 'x.i' (line [[@LINE-15]])
26+
// CHECK: {{\[}}[[OFFSET]], {{.*}}) 'x' (line [[@LINE-15]])
2727
}

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3392,6 +3392,29 @@ static void findStoresToUninstrumentedArgAllocas(
33923392
}
33933393
}
33943394

3395+
static StringRef getAllocaName(AllocaInst *AI) {
3396+
// Alloca could have been renamed for uniqueness. Its true name will have been
3397+
// recorded as an annotation.
3398+
if (AI->hasMetadata(LLVMContext::MD_annotation)) {
3399+
MDTuple *AllocaAnnotations =
3400+
cast<MDTuple>(AI->getMetadata(LLVMContext::MD_annotation));
3401+
for (auto &Annotation : AllocaAnnotations->operands()) {
3402+
if (!isa<MDTuple>(Annotation))
3403+
continue;
3404+
auto AnnotationTuple = cast<MDTuple>(Annotation);
3405+
for (int Index = 0; Index < AnnotationTuple->getNumOperands(); Index++) {
3406+
// All annotations are strings
3407+
auto MetadataString =
3408+
cast<MDString>(AnnotationTuple->getOperand(Index));
3409+
if (MetadataString->getString() == "alloca_name_altered")
3410+
return cast<MDString>(AnnotationTuple->getOperand(Index + 1))
3411+
->getString();
3412+
}
3413+
}
3414+
}
3415+
return AI->getName();
3416+
}
3417+
33953418
void FunctionStackPoisoner::processStaticAllocas() {
33963419
if (AllocaVec.empty()) {
33973420
assert(StaticAllocaPoisonCallVec.empty());
@@ -3432,7 +3455,8 @@ void FunctionStackPoisoner::processStaticAllocas() {
34323455
SmallVector<ASanStackVariableDescription, 16> SVD;
34333456
SVD.reserve(AllocaVec.size());
34343457
for (AllocaInst *AI : AllocaVec) {
3435-
ASanStackVariableDescription D = {AI->getName().data(),
3458+
StringRef Name = getAllocaName(AI);
3459+
ASanStackVariableDescription D = {Name.data(),
34363460
ASan.getAllocaSizeInBytes(*AI),
34373461
0,
34383462
AI->getAlign().value(),

0 commit comments

Comments
 (0)