mirror of
https://github.com/RPCS3/llvm.git
synced 2024-12-16 16:37:42 +00:00
[ASan] Change fake stack and local variables handling.
This commit changes the way we get fake stack from ASan runtime (to find use-after-return errors) and the way we represent local variables: - __asan_stack_malloc function now returns pointer to newly allocated fake stack frame, or NULL if frame cannot be allocated. It doesn't take pointer to real stack as an input argument, it is calculated inside the runtime. - __asan_stack_free function doesn't take pointer to real stack as an input argument. Now this function is never called if fake stack frame wasn't allocated. - __asan_init version is bumped to reflect changes in the ABI. - new flag "-asan-stack-dynamic-alloca" allows to store all the function local variables in a dynamic alloca, instead of the static one. It reduces the stack space usage in use-after-return mode (dynamic alloca will not be called if the local variables are stored in a fake stack), and improves the debug info quality for local variables (they will not be described relatively to %rbp/%rsp, which are assumed to be clobbered by function calls). This flag is turned off by default for now, but I plan to turn it on after more testing. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@224062 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
f88e4c8e91
commit
05e5839d4f
@ -84,7 +84,7 @@ static const char *const kAsanUnregisterGlobalsName =
|
||||
"__asan_unregister_globals";
|
||||
static const char *const kAsanPoisonGlobalsName = "__asan_before_dynamic_init";
|
||||
static const char *const kAsanUnpoisonGlobalsName = "__asan_after_dynamic_init";
|
||||
static const char *const kAsanInitName = "__asan_init_v4";
|
||||
static const char *const kAsanInitName = "__asan_init_v5";
|
||||
static const char *const kAsanPtrCmp = "__sanitizer_ptr_cmp";
|
||||
static const char *const kAsanPtrSub = "__sanitizer_ptr_sub";
|
||||
static const char *const kAsanHandleNoReturnName = "__asan_handle_no_return";
|
||||
@ -184,6 +184,11 @@ static cl::opt<bool> ClCheckLifetime("asan-check-lifetime",
|
||||
cl::desc("Use llvm.lifetime intrinsics to insert extra checks"),
|
||||
cl::Hidden, cl::init(false));
|
||||
|
||||
static cl::opt<bool> ClDynamicAllocaStack(
|
||||
"asan-stack-dynamic-alloca",
|
||||
cl::desc("Use dynamic alloca to represent stack variables"), cl::Hidden,
|
||||
cl::init(false));
|
||||
|
||||
// Debug flags.
|
||||
static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden,
|
||||
cl::init(0));
|
||||
@ -496,11 +501,15 @@ struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
|
||||
typedef DenseMap<Value*, AllocaInst*> AllocaForValueMapTy;
|
||||
AllocaForValueMapTy AllocaForValue;
|
||||
|
||||
bool HasNonEmptyInlineAsm;
|
||||
std::unique_ptr<CallInst> EmptyInlineAsm;
|
||||
|
||||
FunctionStackPoisoner(Function &F, AddressSanitizer &ASan)
|
||||
: F(F), ASan(ASan), DIB(*F.getParent(), /*AllowUnresolved*/ false),
|
||||
C(ASan.C), IntptrTy(ASan.IntptrTy),
|
||||
IntptrPtrTy(PointerType::get(IntptrTy, 0)), Mapping(ASan.Mapping),
|
||||
StackAlignment(1 << Mapping.Scale) {}
|
||||
StackAlignment(1 << Mapping.Scale), HasNonEmptyInlineAsm(false),
|
||||
EmptyInlineAsm(CallInst::Create(ASan.EmptyAsm)) {}
|
||||
|
||||
bool runOnFunction() {
|
||||
if (!ClStack) return false;
|
||||
@ -617,6 +626,11 @@ struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
|
||||
AllocaPoisonCallVec.push_back(APC);
|
||||
}
|
||||
|
||||
void visitCallInst(CallInst &CI) {
|
||||
HasNonEmptyInlineAsm |=
|
||||
CI.isInlineAsm() && !CI.isIdenticalTo(EmptyInlineAsm.get());
|
||||
}
|
||||
|
||||
// ---------------------- Helpers.
|
||||
void initializeCallbacks(Module &M);
|
||||
|
||||
@ -652,6 +666,10 @@ struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
|
||||
|
||||
void SetShadowToStackAfterReturnInlined(IRBuilder<> &IRB, Value *ShadowBase,
|
||||
int Size);
|
||||
Value *createAllocaForLayout(IRBuilder<> &IRB, const ASanStackFrameLayout &L,
|
||||
bool Dynamic);
|
||||
PHINode *createPHI(IRBuilder<> &IRB, Value *Cond, Value *ValueIfTrue,
|
||||
Instruction *ThenTerm, Value *ValueIfFalse);
|
||||
};
|
||||
|
||||
} // namespace
|
||||
@ -1509,12 +1527,11 @@ void FunctionStackPoisoner::initializeCallbacks(Module &M) {
|
||||
IRBuilder<> IRB(*C);
|
||||
for (int i = 0; i <= kMaxAsanStackMallocSizeClass; i++) {
|
||||
std::string Suffix = itostr(i);
|
||||
AsanStackMallocFunc[i] = checkInterfaceFunction(
|
||||
M.getOrInsertFunction(kAsanStackMallocNameTemplate + Suffix, IntptrTy,
|
||||
IntptrTy, IntptrTy, nullptr));
|
||||
AsanStackFreeFunc[i] = checkInterfaceFunction(M.getOrInsertFunction(
|
||||
kAsanStackFreeNameTemplate + Suffix, IRB.getVoidTy(), IntptrTy,
|
||||
IntptrTy, IntptrTy, nullptr));
|
||||
AsanStackMallocFunc[i] = checkInterfaceFunction(M.getOrInsertFunction(
|
||||
kAsanStackMallocNameTemplate + Suffix, IntptrTy, IntptrTy, nullptr));
|
||||
AsanStackFreeFunc[i] = checkInterfaceFunction(
|
||||
M.getOrInsertFunction(kAsanStackFreeNameTemplate + Suffix,
|
||||
IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
|
||||
}
|
||||
AsanPoisonStackMemoryFunc = checkInterfaceFunction(
|
||||
M.getOrInsertFunction(kAsanPoisonStackMemoryName, IRB.getVoidTy(),
|
||||
@ -1586,6 +1603,36 @@ static DebugLoc getFunctionEntryDebugLocation(Function &F) {
|
||||
return DebugLoc();
|
||||
}
|
||||
|
||||
PHINode *FunctionStackPoisoner::createPHI(IRBuilder<> &IRB, Value *Cond,
|
||||
Value *ValueIfTrue,
|
||||
Instruction *ThenTerm,
|
||||
Value *ValueIfFalse) {
|
||||
PHINode *PHI = IRB.CreatePHI(IntptrTy, 2);
|
||||
BasicBlock *CondBlock = cast<Instruction>(Cond)->getParent();
|
||||
PHI->addIncoming(ValueIfFalse, CondBlock);
|
||||
BasicBlock *ThenBlock = ThenTerm->getParent();
|
||||
PHI->addIncoming(ValueIfTrue, ThenBlock);
|
||||
return PHI;
|
||||
}
|
||||
|
||||
Value *FunctionStackPoisoner::createAllocaForLayout(
|
||||
IRBuilder<> &IRB, const ASanStackFrameLayout &L, bool Dynamic) {
|
||||
AllocaInst *Alloca;
|
||||
if (Dynamic) {
|
||||
Alloca = IRB.CreateAlloca(IRB.getInt8Ty(),
|
||||
ConstantInt::get(IRB.getInt64Ty(), L.FrameSize),
|
||||
"MyAlloca");
|
||||
} else {
|
||||
Alloca = IRB.CreateAlloca(ArrayType::get(IRB.getInt8Ty(), L.FrameSize),
|
||||
nullptr, "MyAlloca");
|
||||
assert(Alloca->isStaticAlloca());
|
||||
}
|
||||
assert((ClRealignStack & (ClRealignStack - 1)) == 0);
|
||||
size_t FrameAlignment = std::max(L.FrameAlignment, (size_t)ClRealignStack);
|
||||
Alloca->setAlignment(FrameAlignment);
|
||||
return IRB.CreatePointerCast(Alloca, IntptrTy);
|
||||
}
|
||||
|
||||
void FunctionStackPoisoner::poisonStack() {
|
||||
assert(AllocaVec.size() > 0 || DynamicAllocaVec.size() > 0);
|
||||
|
||||
@ -1620,42 +1667,56 @@ void FunctionStackPoisoner::poisonStack() {
|
||||
uint64_t LocalStackSize = L.FrameSize;
|
||||
bool DoStackMalloc =
|
||||
ClUseAfterReturn && LocalStackSize <= kMaxStackMallocSize;
|
||||
// Don't do dynamic alloca in presence of inline asm: too often it
|
||||
// makes assumptions on which registers are available.
|
||||
bool DoDynamicAlloca = ClDynamicAllocaStack && !HasNonEmptyInlineAsm;
|
||||
|
||||
Type *ByteArrayTy = ArrayType::get(IRB.getInt8Ty(), LocalStackSize);
|
||||
AllocaInst *MyAlloca =
|
||||
new AllocaInst(ByteArrayTy, "MyAlloca", InsBefore);
|
||||
MyAlloca->setDebugLoc(EntryDebugLocation);
|
||||
assert((ClRealignStack & (ClRealignStack - 1)) == 0);
|
||||
size_t FrameAlignment = std::max(L.FrameAlignment, (size_t)ClRealignStack);
|
||||
MyAlloca->setAlignment(FrameAlignment);
|
||||
assert(MyAlloca->isStaticAlloca());
|
||||
Value *OrigStackBase = IRB.CreatePointerCast(MyAlloca, IntptrTy);
|
||||
Value *LocalStackBase = OrigStackBase;
|
||||
Value *StaticAlloca =
|
||||
DoDynamicAlloca ? nullptr : createAllocaForLayout(IRB, L, false);
|
||||
|
||||
Value *FakeStack;
|
||||
Value *LocalStackBase;
|
||||
|
||||
if (DoStackMalloc) {
|
||||
// LocalStackBase = OrigStackBase
|
||||
// if (__asan_option_detect_stack_use_after_return)
|
||||
// LocalStackBase = __asan_stack_malloc_N(LocalStackBase, OrigStackBase);
|
||||
StackMallocIdx = StackMallocSizeClass(LocalStackSize);
|
||||
assert(StackMallocIdx <= kMaxAsanStackMallocSizeClass);
|
||||
// void *FakeStack = __asan_option_detect_stack_use_after_return
|
||||
// ? __asan_stack_malloc_N(LocalStackSize)
|
||||
// : nullptr;
|
||||
// void *LocalStackBase = (FakeStack) ? FakeStack : alloca(LocalStackSize);
|
||||
Constant *OptionDetectUAR = F.getParent()->getOrInsertGlobal(
|
||||
kAsanOptionDetectUAR, IRB.getInt32Ty());
|
||||
Value *Cmp = IRB.CreateICmpNE(IRB.CreateLoad(OptionDetectUAR),
|
||||
Constant::getNullValue(IRB.getInt32Ty()));
|
||||
Instruction *Term = SplitBlockAndInsertIfThen(Cmp, InsBefore, false);
|
||||
BasicBlock *CmpBlock = cast<Instruction>(Cmp)->getParent();
|
||||
Value *UARIsEnabled =
|
||||
IRB.CreateICmpNE(IRB.CreateLoad(OptionDetectUAR),
|
||||
Constant::getNullValue(IRB.getInt32Ty()));
|
||||
Instruction *Term =
|
||||
SplitBlockAndInsertIfThen(UARIsEnabled, InsBefore, false);
|
||||
IRBuilder<> IRBIf(Term);
|
||||
IRBIf.SetCurrentDebugLocation(EntryDebugLocation);
|
||||
LocalStackBase = IRBIf.CreateCall2(
|
||||
AsanStackMallocFunc[StackMallocIdx],
|
||||
ConstantInt::get(IntptrTy, LocalStackSize), OrigStackBase);
|
||||
BasicBlock *SetBlock = cast<Instruction>(LocalStackBase)->getParent();
|
||||
StackMallocIdx = StackMallocSizeClass(LocalStackSize);
|
||||
assert(StackMallocIdx <= kMaxAsanStackMallocSizeClass);
|
||||
Value *FakeStackValue =
|
||||
IRBIf.CreateCall(AsanStackMallocFunc[StackMallocIdx],
|
||||
ConstantInt::get(IntptrTy, LocalStackSize));
|
||||
IRB.SetInsertPoint(InsBefore);
|
||||
IRB.SetCurrentDebugLocation(EntryDebugLocation);
|
||||
PHINode *Phi = IRB.CreatePHI(IntptrTy, 2);
|
||||
Phi->addIncoming(OrigStackBase, CmpBlock);
|
||||
Phi->addIncoming(LocalStackBase, SetBlock);
|
||||
LocalStackBase = Phi;
|
||||
FakeStack = createPHI(IRB, UARIsEnabled, FakeStackValue, Term,
|
||||
ConstantInt::get(IntptrTy, 0));
|
||||
|
||||
Value *NoFakeStack =
|
||||
IRB.CreateICmpEQ(FakeStack, Constant::getNullValue(IntptrTy));
|
||||
Term = SplitBlockAndInsertIfThen(NoFakeStack, InsBefore, false);
|
||||
IRBIf.SetInsertPoint(Term);
|
||||
IRBIf.SetCurrentDebugLocation(EntryDebugLocation);
|
||||
Value *AllocaValue =
|
||||
DoDynamicAlloca ? createAllocaForLayout(IRBIf, L, true) : StaticAlloca;
|
||||
IRB.SetInsertPoint(InsBefore);
|
||||
IRB.SetCurrentDebugLocation(EntryDebugLocation);
|
||||
LocalStackBase = createPHI(IRB, NoFakeStack, AllocaValue, Term, FakeStack);
|
||||
} else {
|
||||
// void *FakeStack = nullptr;
|
||||
// void *LocalStackBase = alloca(LocalStackSize);
|
||||
FakeStack = ConstantInt::get(IntptrTy, 0);
|
||||
LocalStackBase =
|
||||
DoDynamicAlloca ? createAllocaForLayout(IRB, L, true) : StaticAlloca;
|
||||
}
|
||||
|
||||
// Insert poison calls for lifetime intrinsics for alloca.
|
||||
@ -1712,17 +1773,18 @@ void FunctionStackPoisoner::poisonStack() {
|
||||
BasePlus0);
|
||||
if (DoStackMalloc) {
|
||||
assert(StackMallocIdx >= 0);
|
||||
// if LocalStackBase != OrigStackBase:
|
||||
// if FakeStack != 0 // LocalStackBase == FakeStack
|
||||
// // In use-after-return mode, poison the whole stack frame.
|
||||
// if StackMallocIdx <= 4
|
||||
// // For small sizes inline the whole thing:
|
||||
// memset(ShadowBase, kAsanStackAfterReturnMagic, ShadowSize);
|
||||
// **SavedFlagPtr(LocalStackBase) = 0
|
||||
// **SavedFlagPtr(FakeStack) = 0
|
||||
// else
|
||||
// __asan_stack_free_N(LocalStackBase, OrigStackBase)
|
||||
// __asan_stack_free_N(FakeStack, LocalStackSize)
|
||||
// else
|
||||
// <This is not a fake stack; unpoison the redzones>
|
||||
Value *Cmp = IRBRet.CreateICmpNE(LocalStackBase, OrigStackBase);
|
||||
Value *Cmp =
|
||||
IRBRet.CreateICmpNE(FakeStack, Constant::getNullValue(IntptrTy));
|
||||
TerminatorInst *ThenTerm, *ElseTerm;
|
||||
SplitBlockAndInsertIfThenElse(Cmp, Ret, &ThenTerm, &ElseTerm);
|
||||
|
||||
@ -1732,7 +1794,7 @@ void FunctionStackPoisoner::poisonStack() {
|
||||
SetShadowToStackAfterReturnInlined(IRBPoison, ShadowBase,
|
||||
ClassSize >> Mapping.Scale);
|
||||
Value *SavedFlagPtrPtr = IRBPoison.CreateAdd(
|
||||
LocalStackBase,
|
||||
FakeStack,
|
||||
ConstantInt::get(IntptrTy, ClassSize - ASan.LongSize / 8));
|
||||
Value *SavedFlagPtr = IRBPoison.CreateLoad(
|
||||
IRBPoison.CreateIntToPtr(SavedFlagPtrPtr, IntptrPtrTy));
|
||||
@ -1741,9 +1803,8 @@ void FunctionStackPoisoner::poisonStack() {
|
||||
IRBPoison.CreateIntToPtr(SavedFlagPtr, IRBPoison.getInt8PtrTy()));
|
||||
} else {
|
||||
// For larger frames call __asan_stack_free_*.
|
||||
IRBPoison.CreateCall3(AsanStackFreeFunc[StackMallocIdx], LocalStackBase,
|
||||
ConstantInt::get(IntptrTy, LocalStackSize),
|
||||
OrigStackBase);
|
||||
IRBPoison.CreateCall2(AsanStackFreeFunc[StackMallocIdx], FakeStack,
|
||||
ConstantInt::get(IntptrTy, LocalStackSize));
|
||||
}
|
||||
|
||||
IRBuilder<> IRBElse(ElseTerm);
|
||||
@ -1751,7 +1812,6 @@ void FunctionStackPoisoner::poisonStack() {
|
||||
} else if (HavePoisonedAllocas) {
|
||||
// If we poisoned some allocas in llvm.lifetime analysis,
|
||||
// unpoison whole stack frame now.
|
||||
assert(LocalStackBase == OrigStackBase);
|
||||
poisonAlloca(LocalStackBase, LocalStackSize, IRBRet, false);
|
||||
} else {
|
||||
poisonRedZones(L.ShadowBytes, IRBRet, ShadowBase, false);
|
||||
|
@ -0,0 +1,42 @@
|
||||
; RUN: opt < %s -asan -asan-module -asan-stack-dynamic-alloca \
|
||||
; RUN: -asan-use-after-return -S | FileCheck %s
|
||||
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
|
||||
target triple = "x86_64-unknown-linux-gnu"
|
||||
|
||||
define void @Func1() sanitize_address {
|
||||
entry:
|
||||
; CHECK-LABEL: Func1
|
||||
|
||||
; CHECK: entry:
|
||||
; CHECK: load i32* @__asan_option_detect_stack_use_after_return
|
||||
|
||||
; CHECK: <label>:[[UAR_ENABLED_BB:[0-9]+]]
|
||||
; CHECK: [[FAKE_STACK_RT:%[0-9]+]] = call i64 @__asan_stack_malloc_
|
||||
|
||||
; CHECK: <label>:[[FAKE_STACK_BB:[0-9]+]]
|
||||
; CHECK: [[FAKE_STACK:%[0-9]+]] = phi i64 [ 0, %entry ], [ [[FAKE_STACK_RT]], %[[UAR_ENABLED_BB]] ]
|
||||
; CHECK: icmp eq i64 [[FAKE_STACK]], 0
|
||||
|
||||
; CHECK: <label>:[[NO_FAKE_STACK_BB:[0-9]+]]
|
||||
; CHECK: %MyAlloca = alloca i8, i64
|
||||
; CHECK: [[ALLOCA:%[0-9]+]] = ptrtoint i8* %MyAlloca
|
||||
|
||||
; CHECK: phi i64 [ [[FAKE_STACK]], %[[FAKE_STACK_BB]] ], [ [[ALLOCA]], %[[NO_FAKE_STACK_BB]] ]
|
||||
|
||||
; CHECK: ret void
|
||||
|
||||
%XXX = alloca [20 x i8], align 1
|
||||
ret void
|
||||
}
|
||||
|
||||
; Test that dynamic alloca is not used for functions with inline assembly.
|
||||
define void @Func2() sanitize_address {
|
||||
entry:
|
||||
; CHECK-LABEL: Func2
|
||||
; CHECK: alloca [96 x i8]
|
||||
; CHECK: ret void
|
||||
|
||||
%XXX = alloca [20 x i8], align 1
|
||||
call void asm sideeffect "mov %%rbx, %%rcx", "~{dirflag},~{fpsr},~{flags}"() nounwind
|
||||
ret void
|
||||
}
|
@ -1,6 +1,9 @@
|
||||
; Test the ASan's stack layout.
|
||||
; More tests in tests/Transforms/Utils/ASanStackFrameLayoutTest.cpp
|
||||
; RUN: opt < %s -asan -asan-module -S | FileCheck %s
|
||||
; RUN: opt < %s -asan -asan-module -asan-stack-dynamic-alloca=0 -S \
|
||||
; RUN: | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-STATIC
|
||||
; RUN: opt < %s -asan -asan-module -asan-stack-dynamic-alloca=1 -S \
|
||||
; RUN: | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-DYNAMIC
|
||||
|
||||
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
|
||||
target triple = "x86_64-unknown-linux-gnu"
|
||||
@ -14,7 +17,10 @@ declare void @Use(i8*)
|
||||
define void @Func1() sanitize_address {
|
||||
entry:
|
||||
; CHECK-LABEL: Func1
|
||||
; CHECK: alloca [192 x i8]
|
||||
|
||||
; CHECK-STATIC: alloca [192 x i8]
|
||||
; CHECK-DYNAMIC: alloca i8, i64 192
|
||||
|
||||
; CHECK-NOT: alloca
|
||||
; CHECK: ret void
|
||||
%XXX = alloca [10 x i8], align 1
|
||||
@ -26,7 +32,10 @@ entry:
|
||||
define void @Func2() sanitize_address {
|
||||
entry:
|
||||
; CHECK-LABEL: Func2
|
||||
; CHECK: alloca [864 x i8]
|
||||
|
||||
; CHECK-STATIC: alloca [864 x i8]
|
||||
; CHECK-DYNAMIC: alloca i8, i64 864
|
||||
|
||||
; CHECK-NOT: alloca
|
||||
; CHECK: ret void
|
||||
%AAA = alloca [5 x i8], align 1
|
||||
@ -39,7 +48,10 @@ entry:
|
||||
define void @Func3() sanitize_address {
|
||||
entry:
|
||||
; CHECK-LABEL: Func3
|
||||
; CHECK: alloca [768 x i8]
|
||||
|
||||
; CHECK-STATIC: alloca [768 x i8]
|
||||
; CHECK-DYNAMIC: alloca i8, i64 768
|
||||
|
||||
; CHECK-NOT: alloca
|
||||
; CHECK: ret void
|
||||
%AAA = alloca [128 x i8], align 16
|
||||
|
Loading…
Reference in New Issue
Block a user