Skip to content

[ASan] Add metadata to renamed instructions so ASan doesn't use the i… #119387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/lib/CodeGen/CGExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
12 changes: 12 additions & 0 deletions compiler-rt/test/asan/TestCases/shadowed-stack-serialization.cpp
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
Expand Up @@ -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]])
}
26 changes: 25 additions & 1 deletion llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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(),
Expand Down
Loading