Skip to content

Commit 59074a3

Browse files
authored
[ASan] Add metadata to renamed instructions so ASan doesn't use the i… (llvm#119387)
…ncorrect name Clang needs variables to be represented with unique names. This means that if a variable shadows another, its given a different name internally to ensure it has a unique name. If ASan tries to use this name when printing an error, it will print the modified unique name, rather than the variable's source code name Fixes llvm#47326
1 parent b61e387 commit 59074a3

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
@@ -143,6 +143,9 @@ llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(llvm::Type *Ty,
143143
Alloca =
144144
new llvm::AllocaInst(Ty, CGM.getDataLayout().getAllocaAddrSpace(),
145145
ArraySize, Name, AllocaInsertPt->getIterator());
146+
if (SanOpts.Mask & SanitizerKind::Address) {
147+
Alloca->addAnnotationMetadata({"alloca_name_altered", Name.str()});
148+
}
146149
if (Allocas) {
147150
Allocas->Add(Alloca);
148151
}
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
@@ -27,5 +27,5 @@ int main(int argc, char *argv[]) {
2727
// CHECK: Address 0x{{.*}} is located in stack of thread T0 at offset [[OFFSET:[^ ]*]] in frame
2828
// CHECK: {{.*}} in main
2929
// CHECK: This frame has
30-
// CHECK: {{\[}}[[OFFSET]], {{.*}}) 'x.i' (line [[@LINE-15]])
30+
// CHECK: {{\[}}[[OFFSET]], {{.*}}) 'x' (line [[@LINE-15]])
3131
}

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3437,6 +3437,29 @@ static void findStoresToUninstrumentedArgAllocas(
34373437
}
34383438
}
34393439

3440+
static StringRef getAllocaName(AllocaInst *AI) {
3441+
// Alloca could have been renamed for uniqueness. Its true name will have been
3442+
// recorded as an annotation.
3443+
if (AI->hasMetadata(LLVMContext::MD_annotation)) {
3444+
MDTuple *AllocaAnnotations =
3445+
cast<MDTuple>(AI->getMetadata(LLVMContext::MD_annotation));
3446+
for (auto &Annotation : AllocaAnnotations->operands()) {
3447+
if (!isa<MDTuple>(Annotation))
3448+
continue;
3449+
auto AnnotationTuple = cast<MDTuple>(Annotation);
3450+
for (int Index = 0; Index < AnnotationTuple->getNumOperands(); Index++) {
3451+
// All annotations are strings
3452+
auto MetadataString =
3453+
cast<MDString>(AnnotationTuple->getOperand(Index));
3454+
if (MetadataString->getString() == "alloca_name_altered")
3455+
return cast<MDString>(AnnotationTuple->getOperand(Index + 1))
3456+
->getString();
3457+
}
3458+
}
3459+
}
3460+
return AI->getName();
3461+
}
3462+
34403463
void FunctionStackPoisoner::processStaticAllocas() {
34413464
if (AllocaVec.empty()) {
34423465
assert(StaticAllocaPoisonCallVec.empty());
@@ -3477,7 +3500,8 @@ void FunctionStackPoisoner::processStaticAllocas() {
34773500
SmallVector<ASanStackVariableDescription, 16> SVD;
34783501
SVD.reserve(AllocaVec.size());
34793502
for (AllocaInst *AI : AllocaVec) {
3480-
ASanStackVariableDescription D = {AI->getName().data(),
3503+
StringRef Name = getAllocaName(AI);
3504+
ASanStackVariableDescription D = {Name.data(),
34813505
ASan.getAllocaSizeInBytes(*AI),
34823506
0,
34833507
AI->getAlign().value(),

0 commit comments

Comments
 (0)