From dd121fc8471347915e14b54c50409b27813e990b Mon Sep 17 00:00:00 2001 From: Evgeniy Stepanov Date: Tue, 1 Dec 2015 00:40:05 +0000 Subject: [PATCH] [safestack] Protect byval function arguments. Detect unsafe byval function arguments and move them to the unsafe stack. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@254353 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Transforms/Utils/Local.h | 18 ++- lib/Transforms/Instrumentation/SafeStack.cpp | 149 +++++++++++++------ lib/Transforms/Utils/Local.cpp | 16 +- test/Transforms/SafeStack/byval.ll | 51 +++++++ test/Transforms/SafeStack/debug-loc.ll | 120 +++++++-------- 5 files changed, 242 insertions(+), 112 deletions(-) create mode 100644 test/Transforms/SafeStack/byval.ll diff --git a/include/llvm/Transforms/Utils/Local.h b/include/llvm/Transforms/Utils/Local.h index b7d67eaea3a..1d707a1e530 100644 --- a/include/llvm/Transforms/Utils/Local.h +++ b/include/llvm/Transforms/Utils/Local.h @@ -271,10 +271,20 @@ bool LowerDbgDeclare(Function &F); /// an alloca, if any. DbgDeclareInst *FindAllocaDbgDeclare(Value *V); -/// \brief Replaces llvm.dbg.declare instruction when an alloca is replaced with -/// a new value. If Deref is true, an additional DW_OP_deref is prepended to the -/// expression. If Offset is non-zero, a constant displacement is added to the -/// expression (after the optional Deref). Offset can be negative. +/// \brief Replaces llvm.dbg.declare instruction when the address it describes +/// is replaced with a new value. If Deref is true, an additional DW_OP_deref is +/// prepended to the expression. If Offset is non-zero, a constant displacement +/// is added to the expression (after the optional Deref). Offset can be +/// negative. +bool replaceDbgDeclare(Value *Address, Value *NewAddress, + Instruction *InsertBefore, DIBuilder &Builder, + bool Deref, int Offset); + +/// \brief Replaces llvm.dbg.declare instruction when the alloca it describes +/// is replaced with a new value. If Deref is true, an additional DW_OP_deref is +/// prepended to the expression. If Offset is non-zero, a constant displacement +/// is added to the expression (after the optional Deref). Offset can be +/// negative. New llvm.dbg.declare is inserted immediately before AI. bool replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress, DIBuilder &Builder, bool Deref, int Offset = 0); diff --git a/lib/Transforms/Instrumentation/SafeStack.cpp b/lib/Transforms/Instrumentation/SafeStack.cpp index f8c4058ae22..4441663fc6d 100644 --- a/lib/Transforms/Instrumentation/SafeStack.cpp +++ b/lib/Transforms/Instrumentation/SafeStack.cpp @@ -57,6 +57,7 @@ STATISTIC(NumUnsafeStackRestorePointsFunctions, STATISTIC(NumAllocas, "Total number of allocas"); STATISTIC(NumUnsafeStaticAllocas, "Number of unsafe static allocas"); STATISTIC(NumUnsafeDynamicAllocas, "Number of unsafe dynamic allocas"); +STATISTIC(NumUnsafeByValArguments, "Number of unsafe byval arguments"); STATISTIC(NumUnsafeStackRestorePoints, "Number of setjmps and landingpads"); } // namespace llvm @@ -68,14 +69,14 @@ namespace { /// /// The implementation simply replaces all mentions of the alloca with zero. class AllocaOffsetRewriter : public SCEVRewriteVisitor { - const AllocaInst *AI; + const Value *AllocaPtr; public: - AllocaOffsetRewriter(ScalarEvolution &SE, const AllocaInst *AI) - : SCEVRewriteVisitor(SE), AI(AI) {} + AllocaOffsetRewriter(ScalarEvolution &SE, const Value *AllocaPtr) + : SCEVRewriteVisitor(SE), AllocaPtr(AllocaPtr) {} const SCEV *visitUnknown(const SCEVUnknown *Expr) { - if (Expr->getValue() == AI) + if (Expr->getValue() == AllocaPtr) return SE.getZero(Expr->getType()); return Expr; } @@ -115,6 +116,7 @@ class SafeStack : public FunctionPass { /// given function and append them to the respective vectors. void findInsts(Function &F, SmallVectorImpl &StaticAllocas, SmallVectorImpl &DynamicAllocas, + SmallVectorImpl &ByValArguments, SmallVectorImpl &Returns, SmallVectorImpl &StackRestorePoints); @@ -130,6 +132,7 @@ class SafeStack : public FunctionPass { /// allocas are allocated. Value *moveStaticAllocasToUnsafeStack(IRBuilder<> &IRB, Function &F, ArrayRef StaticAllocas, + ArrayRef ByValArguments, ArrayRef Returns); /// \brief Generate code to restore the stack after all stack restore points @@ -149,11 +152,12 @@ class SafeStack : public FunctionPass { AllocaInst *DynamicTop, ArrayRef DynamicAllocas); - bool IsSafeStackAlloca(const AllocaInst *AI); + bool IsSafeStackAlloca(const Value *AllocaPtr, uint64_t AllocaSize); bool IsMemIntrinsicSafe(const MemIntrinsic *MI, const Use &U, - const AllocaInst *AI); - bool IsAccessSafe(Value *Addr, uint64_t Size, const AllocaInst *AI); + const Value *AllocaPtr, uint64_t AllocaSize); + bool IsAccessSafe(Value *Addr, uint64_t Size, const Value *AllocaPtr, + uint64_t AllocaSize); public: static char ID; // Pass identification, replacement for typeid. @@ -192,20 +196,23 @@ uint64_t SafeStack::getStaticAllocaAllocationSize(const AllocaInst* AI) { return Size; } -bool SafeStack::IsAccessSafe(Value *Addr, uint64_t Size, const AllocaInst *AI) { - AllocaOffsetRewriter Rewriter(*SE, AI); +bool SafeStack::IsAccessSafe(Value *Addr, uint64_t AccessSize, + const Value *AllocaPtr, uint64_t AllocaSize) { + AllocaOffsetRewriter Rewriter(*SE, AllocaPtr); const SCEV *Expr = Rewriter.visit(SE->getSCEV(Addr)); uint64_t BitWidth = SE->getTypeSizeInBits(Expr->getType()); ConstantRange AccessStartRange = SE->getUnsignedRange(Expr); ConstantRange SizeRange = - ConstantRange(APInt(BitWidth, 0), APInt(BitWidth, Size)); + ConstantRange(APInt(BitWidth, 0), APInt(BitWidth, AccessSize)); ConstantRange AccessRange = AccessStartRange.add(SizeRange); - ConstantRange AllocaRange = ConstantRange( - APInt(BitWidth, 0), APInt(BitWidth, getStaticAllocaAllocationSize(AI))); + ConstantRange AllocaRange = + ConstantRange(APInt(BitWidth, 0), APInt(BitWidth, AllocaSize)); bool Safe = AllocaRange.contains(AccessRange); - DEBUG(dbgs() << "[SafeStack] Alloca " << *AI << "\n" + DEBUG(dbgs() << "[SafeStack] " + << (isa(AllocaPtr) ? "Alloca " : "ByValArgument ") + << *AllocaPtr << "\n" << " Access " << *Addr << "\n" << " SCEV " << *Expr << " U: " << SE->getUnsignedRange(Expr) @@ -218,36 +225,38 @@ bool SafeStack::IsAccessSafe(Value *Addr, uint64_t Size, const AllocaInst *AI) { } bool SafeStack::IsMemIntrinsicSafe(const MemIntrinsic *MI, const Use &U, - const AllocaInst *AI) { + const Value *AllocaPtr, + uint64_t AllocaSize) { // All MemIntrinsics have destination address in Arg0 and size in Arg2. if (MI->getRawDest() != U) return true; const auto *Len = dyn_cast(MI->getLength()); // Non-constant size => unsafe. FIXME: try SCEV getRange. if (!Len) return false; - return IsAccessSafe(U, Len->getZExtValue(), AI); + return IsAccessSafe(U, Len->getZExtValue(), AllocaPtr, AllocaSize); } -/// Check whether a given alloca instruction (AI) should be put on the safe +/// Check whether a given allocation must be put on the safe /// stack or not. The function analyzes all uses of AI and checks whether it is /// only accessed in a memory safe way (as decided statically). -bool SafeStack::IsSafeStackAlloca(const AllocaInst *AI) { +bool SafeStack::IsSafeStackAlloca(const Value *AllocaPtr, uint64_t AllocaSize) { // Go through all uses of this alloca and check whether all accesses to the // allocated object are statically known to be memory safe and, hence, the // object can be placed on the safe stack. SmallPtrSet Visited; - SmallVector WorkList; - WorkList.push_back(AI); + SmallVector WorkList; + WorkList.push_back(AllocaPtr); // A DFS search through all uses of the alloca in bitcasts/PHI/GEPs/etc. while (!WorkList.empty()) { - const Instruction *V = WorkList.pop_back_val(); + const Value *V = WorkList.pop_back_val(); for (const Use &UI : V->uses()) { auto I = cast(UI.getUser()); assert(V == UI.get()); switch (I->getOpcode()) { case Instruction::Load: { - if (!IsAccessSafe(UI, DL->getTypeStoreSize(I->getType()), AI)) + if (!IsAccessSafe(UI, DL->getTypeStoreSize(I->getType()), AllocaPtr, + AllocaSize)) return false; break; } @@ -257,13 +266,13 @@ bool SafeStack::IsSafeStackAlloca(const AllocaInst *AI) { case Instruction::Store: { if (V == I->getOperand(0)) { // Stored the pointer - conservatively assume it may be unsafe. - DEBUG(dbgs() << "[SafeStack] Unsafe alloca: " << *AI + DEBUG(dbgs() << "[SafeStack] Unsafe alloca: " << *AllocaPtr << "\n store of address: " << *I << "\n"); return false; } - if (!IsAccessSafe( - UI, DL->getTypeStoreSize(I->getOperand(0)->getType()), AI)) + if (!IsAccessSafe(UI, DL->getTypeStoreSize(I->getOperand(0)->getType()), + AllocaPtr, AllocaSize)) return false; break; } @@ -283,8 +292,8 @@ bool SafeStack::IsSafeStackAlloca(const AllocaInst *AI) { } if (const MemIntrinsic *MI = dyn_cast(I)) { - if (!IsMemIntrinsicSafe(MI, UI, AI)) { - DEBUG(dbgs() << "[SafeStack] Unsafe alloca: " << *AI + if (!IsMemIntrinsicSafe(MI, UI, AllocaPtr, AllocaSize)) { + DEBUG(dbgs() << "[SafeStack] Unsafe alloca: " << *AllocaPtr << "\n unsafe memintrinsic: " << *I << "\n"); return false; @@ -302,9 +311,9 @@ bool SafeStack::IsSafeStackAlloca(const AllocaInst *AI) { ImmutableCallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end(); for (ImmutableCallSite::arg_iterator A = B; A != E; ++A) if (A->get() == V) - if (!(CS.doesNotCapture(A - B) && - (CS.doesNotAccessMemory(A - B) || CS.doesNotAccessMemory()))) { - DEBUG(dbgs() << "[SafeStack] Unsafe alloca: " << *AI + if (!(CS.doesNotCapture(A - B) && (CS.doesNotAccessMemory(A - B) || + CS.doesNotAccessMemory()))) { + DEBUG(dbgs() << "[SafeStack] Unsafe alloca: " << *AllocaPtr << "\n unsafe call: " << *I << "\n"); return false; } @@ -355,13 +364,15 @@ Value *SafeStack::getOrCreateUnsafeStackPtr(IRBuilder<> &IRB, Function &F) { void SafeStack::findInsts(Function &F, SmallVectorImpl &StaticAllocas, SmallVectorImpl &DynamicAllocas, + SmallVectorImpl &ByValArguments, SmallVectorImpl &Returns, SmallVectorImpl &StackRestorePoints) { for (Instruction &I : instructions(&F)) { if (auto AI = dyn_cast(&I)) { ++NumAllocas; - if (IsSafeStackAlloca(AI)) + uint64_t Size = getStaticAllocaAllocationSize(AI); + if (IsSafeStackAlloca(AI, Size)) continue; if (AI->isStaticAlloca()) { @@ -386,6 +397,17 @@ void SafeStack::findInsts(Function &F, "gcroot intrinsic not compatible with safestack attribute"); } } + for (Argument &Arg : F.args()) { + if (!Arg.hasByValAttr()) + continue; + uint64_t Size = + DL->getTypeStoreSize(Arg.getType()->getPointerElementType()); + if (IsSafeStackAlloca(&Arg, Size)) + continue; + + ++NumUnsafeByValArguments; + ByValArguments.push_back(&Arg); + } } AllocaInst * @@ -420,7 +442,7 @@ SafeStack::createStackRestorePoints(IRBuilder<> &IRB, Function &F, for (Instruction *I : StackRestorePoints) { ++NumUnsafeStackRestorePoints; - IRB.SetInsertPoint(cast(I->getNextNode())); + IRB.SetInsertPoint(I->getNextNode()); Value *CurrentTop = DynamicTop ? IRB.CreateLoad(DynamicTop) : StaticTop; IRB.CreateStore(CurrentTop, UnsafeStackPtr); } @@ -428,11 +450,10 @@ SafeStack::createStackRestorePoints(IRBuilder<> &IRB, Function &F, return DynamicTop; } -Value * -SafeStack::moveStaticAllocasToUnsafeStack(IRBuilder<> &IRB, Function &F, - ArrayRef StaticAllocas, - ArrayRef Returns) { - if (StaticAllocas.empty()) +Value *SafeStack::moveStaticAllocasToUnsafeStack( + IRBuilder<> &IRB, Function &F, ArrayRef StaticAllocas, + ArrayRef ByValArguments, ArrayRef Returns) { + if (StaticAllocas.empty() && ByValArguments.empty()) return nullptr; DIBuilder DIB(*F.getParent()); @@ -454,6 +475,13 @@ SafeStack::moveStaticAllocasToUnsafeStack(IRBuilder<> &IRB, Function &F, // Compute maximum alignment among static objects on the unsafe stack. unsigned MaxAlignment = 0; + for (Argument *Arg : ByValArguments) { + Type *Ty = Arg->getType()->getPointerElementType(); + unsigned Align = std::max((unsigned)DL->getPrefTypeAlignment(Ty), + Arg->getParamAlignment()); + if (Align > MaxAlignment) + MaxAlignment = Align; + } for (AllocaInst *AI : StaticAllocas) { Type *Ty = AI->getAllocatedType(); unsigned Align = @@ -465,15 +493,46 @@ SafeStack::moveStaticAllocasToUnsafeStack(IRBuilder<> &IRB, Function &F, if (MaxAlignment > StackAlignment) { // Re-align the base pointer according to the max requested alignment. assert(isPowerOf2_32(MaxAlignment)); - IRB.SetInsertPoint(cast(BasePointer->getNextNode())); + IRB.SetInsertPoint(BasePointer->getNextNode()); BasePointer = cast(IRB.CreateIntToPtr( IRB.CreateAnd(IRB.CreatePtrToInt(BasePointer, IntPtrTy), ConstantInt::get(IntPtrTy, ~uint64_t(MaxAlignment - 1))), StackPtrTy)); } - // Allocate space for every unsafe static AllocaInst on the unsafe stack. int64_t StaticOffset = 0; // Current stack top. + IRB.SetInsertPoint(BasePointer->getNextNode()); + + for (Argument *Arg : ByValArguments) { + Type *Ty = Arg->getType()->getPointerElementType(); + + uint64_t Size = DL->getTypeStoreSize(Ty); + if (Size == 0) + Size = 1; // Don't create zero-sized stack objects. + + // Ensure the object is properly aligned. + unsigned Align = std::max((unsigned)DL->getPrefTypeAlignment(Ty), + Arg->getParamAlignment()); + + // Add alignment. + // NOTE: we ensure that BasePointer itself is aligned to >= Align. + StaticOffset += Size; + StaticOffset = RoundUpToAlignment(StaticOffset, Align); + + Value *Off = IRB.CreateGEP(BasePointer, // BasePointer is i8* + ConstantInt::get(Int32Ty, -StaticOffset)); + Value *NewArg = IRB.CreateBitCast(Off, Arg->getType(), + Arg->getName() + ".unsafe-byval"); + + // Replace alloc with the new location. + replaceDbgDeclare(Arg, BasePointer, BasePointer->getNextNode(), DIB, + /*Deref=*/true, -StaticOffset); + Arg->replaceAllUsesWith(NewArg); + IRB.SetInsertPoint(cast(NewArg)->getNextNode()); + IRB.CreateMemCpy(Off, Arg, Size, Arg->getParamAlignment()); + } + + // Allocate space for every unsafe static AllocaInst on the unsafe stack. for (AllocaInst *AI : StaticAllocas) { IRB.SetInsertPoint(AI); @@ -509,7 +568,7 @@ SafeStack::moveStaticAllocasToUnsafeStack(IRBuilder<> &IRB, Function &F, StaticOffset = RoundUpToAlignment(StaticOffset, StackAlignment); // Update shadow stack pointer in the function epilogue. - IRB.SetInsertPoint(cast(BasePointer->getNextNode())); + IRB.SetInsertPoint(BasePointer->getNextNode()); Value *StaticTop = IRB.CreateGEP(BasePointer, ConstantInt::get(Int32Ty, -StaticOffset), @@ -621,6 +680,7 @@ bool SafeStack::runOnFunction(Function &F) { SmallVector StaticAllocas; SmallVector DynamicAllocas; + SmallVector ByValArguments; SmallVector Returns; // Collect all points where stack gets unwound and needs to be restored @@ -632,13 +692,15 @@ bool SafeStack::runOnFunction(Function &F) { // Find all static and dynamic alloca instructions that must be moved to the // unsafe stack, all return instructions and stack restore points. - findInsts(F, StaticAllocas, DynamicAllocas, Returns, StackRestorePoints); + findInsts(F, StaticAllocas, DynamicAllocas, ByValArguments, Returns, + StackRestorePoints); if (StaticAllocas.empty() && DynamicAllocas.empty() && - StackRestorePoints.empty()) + ByValArguments.empty() && StackRestorePoints.empty()) return false; // Nothing to do in this function. - if (!StaticAllocas.empty() || !DynamicAllocas.empty()) + if (!StaticAllocas.empty() || !DynamicAllocas.empty() || + !ByValArguments.empty()) ++NumUnsafeStackFunctions; // This function has the unsafe stack. if (!StackRestorePoints.empty()) @@ -648,7 +710,8 @@ bool SafeStack::runOnFunction(Function &F) { UnsafeStackPtr = getOrCreateUnsafeStackPtr(IRB, F); // The top of the unsafe stack after all unsafe static allocas are allocated. - Value *StaticTop = moveStaticAllocasToUnsafeStack(IRB, F, StaticAllocas, Returns); + Value *StaticTop = moveStaticAllocasToUnsafeStack(IRB, F, StaticAllocas, + ByValArguments, Returns); // Safe stack object that stores the current unsafe stack top. It is updated // as unsafe dynamic (non-constant-sized) allocas are allocated and freed. diff --git a/lib/Transforms/Utils/Local.cpp b/lib/Transforms/Utils/Local.cpp index 0bd5fa9f877..623da675e05 100644 --- a/lib/Transforms/Utils/Local.cpp +++ b/lib/Transforms/Utils/Local.cpp @@ -1136,9 +1136,10 @@ DbgDeclareInst *llvm::FindAllocaDbgDeclare(Value *V) { return nullptr; } -bool llvm::replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress, - DIBuilder &Builder, bool Deref, int Offset) { - DbgDeclareInst *DDI = FindAllocaDbgDeclare(AI); +bool llvm::replaceDbgDeclare(Value *Address, Value *NewAddress, + Instruction *InsertBefore, DIBuilder &Builder, + bool Deref, int Offset) { + DbgDeclareInst *DDI = FindAllocaDbgDeclare(Address); if (!DDI) return false; DebugLoc Loc = DDI->getDebugLoc(); @@ -1168,12 +1169,17 @@ bool llvm::replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress, // Insert llvm.dbg.declare immediately after the original alloca, and remove // old llvm.dbg.declare. - Builder.insertDeclare(NewAllocaAddress, DIVar, DIExpr, Loc, - AI->getNextNode()); + Builder.insertDeclare(NewAddress, DIVar, DIExpr, Loc, InsertBefore); DDI->eraseFromParent(); return true; } +bool llvm::replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress, + DIBuilder &Builder, bool Deref, int Offset) { + return replaceDbgDeclare(AI, NewAllocaAddress, AI->getNextNode(), Builder, + Deref, Offset); +} + /// changeToUnreachable - Insert an unreachable instruction before the specified /// instruction, making it and the rest of the code in the block dead. static void changeToUnreachable(Instruction *I, bool UseLLVMTrap) { diff --git a/test/Transforms/SafeStack/byval.ll b/test/Transforms/SafeStack/byval.ll new file mode 100644 index 00000000000..f9a06e54d2d --- /dev/null +++ b/test/Transforms/SafeStack/byval.ll @@ -0,0 +1,51 @@ +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +%struct.S = type { [100 x i32] } + +; Safe access to a byval argument. +define i32 @ByValSafe(%struct.S* byval nocapture readonly align 8 %zzz) norecurse nounwind readonly safestack uwtable { +entry: + ; CHECK-LABEL: @ByValSafe + ; CHECK-NOT: __safestack_unsafe_stack_ptr + ; CHECK: ret i32 + %arrayidx = getelementptr inbounds %struct.S, %struct.S* %zzz, i64 0, i32 0, i64 3 + %0 = load i32, i32* %arrayidx, align 4 + ret i32 %0 +} + +; Unsafe access to a byval argument. +; Argument is copied to the unsafe stack. +define i32 @ByValUnsafe(%struct.S* byval nocapture readonly align 8 %zzz, i64 %idx) norecurse nounwind readonly safestack uwtable { +entry: + ; CHECK-LABEL: @ByValUnsafe + ; CHECK: %[[A:.*]] = load {{.*}} @__safestack_unsafe_stack_ptr + ; CHECK: store {{.*}} @__safestack_unsafe_stack_ptr + ; CHECK: %[[B:.*]] = getelementptr i8, i8* %[[A]], i32 -400 + ; CHECK: %[[C:.*]] = bitcast %struct.S* %zzz to i8* + ; CHECK: call void @llvm.memcpy.p0i8.p0i8.i64(i8* %[[B]], i8* %[[C]], i64 400, i32 8, i1 false) + ; CHECK: ret i32 + %arrayidx = getelementptr inbounds %struct.S, %struct.S* %zzz, i64 0, i32 0, i64 %idx + %0 = load i32, i32* %arrayidx, align 4 + ret i32 %0 +} + +; Highly aligned byval argument. +define i32 @ByValUnsafeAligned(%struct.S* byval nocapture readonly align 64 %zzz, i64 %idx) norecurse nounwind readonly safestack uwtable { +entry: + ; CHECK-LABEL: @ByValUnsafeAligned + ; CHECK: %[[A:.*]] = load {{.*}} @__safestack_unsafe_stack_ptr + ; CHECK: %[[B:.*]] = ptrtoint i8* %[[A]] to i64 + ; CHECK: and i64 %[[B]], -64 + ; CHECK: ret i32 + %arrayidx = getelementptr inbounds %struct.S, %struct.S* %zzz, i64 0, i32 0, i64 0 + %0 = load i32, i32* %arrayidx, align 64 + %arrayidx2 = getelementptr inbounds %struct.S, %struct.S* %zzz, i64 0, i32 0, i64 %idx + %1 = load i32, i32* %arrayidx2, align 4 + %add = add nsw i32 %1, %0 + ret i32 %add +} + diff --git a/test/Transforms/SafeStack/debug-loc.ll b/test/Transforms/SafeStack/debug-loc.ll index 57d565f8cfd..e72d0e9d2ff 100644 --- a/test/Transforms/SafeStack/debug-loc.ll +++ b/test/Transforms/SafeStack/debug-loc.ll @@ -1,83 +1,83 @@ ; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s ; Test debug location for the local variables moved onto the unsafe stack. -; CHECK: define void @f -; CHECK: %[[USP:.*]] = load i8*, i8** @__safestack_unsafe_stack_ptr - -; dbg.declare for %buf is gone; replaced with dbg.declare based off the unsafe stack pointer -; CHECK-NOT: @llvm.dbg.declare.*%buf -; CHECK: call void @llvm.dbg.declare(metadata i8* %[[USP]], metadata ![[VAR:.*]], metadata ![[EXPR:.*]]) - -; dbg.declare appears before the first use of %buf -; CHECK: getelementptr{{.*}}%buf -; CHECK: call{{.*}}@Capture -; CHECK: ret void - -; dbg.declare describes "buf"... -; CHECK: ![[VAR]] = !DILocalVariable(name: "buf" - -; ... as an offset from the unsafe stack pointer -; CHECK: ![[EXPR]] = !DIExpression(DW_OP_deref, DW_OP_minus, 400) - target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" +%struct.S = type { [100 x i8] } + ; Function Attrs: safestack uwtable -define void @f() #0 !dbg !4 { +define void @f(%struct.S* byval align 8 %zzz) #0 !dbg !12 { +; CHECK: define void @f + entry: - %buf = alloca [100 x i32], align 16 - %0 = bitcast [100 x i32]* %buf to i8*, !dbg !16 - call void @llvm.lifetime.start(i64 400, i8* %0) #4, !dbg !16 - tail call void @llvm.dbg.declare(metadata [100 x i32]* %buf, metadata !8, metadata !17), !dbg !18 +; CHECK: %[[USP:.*]] = load i8*, i8** @__safestack_unsafe_stack_ptr + %xxx = alloca %struct.S, align 1 + call void @llvm.dbg.declare(metadata %struct.S* %zzz, metadata !18, metadata !19), !dbg !20 + call void @llvm.dbg.declare(metadata %struct.S* %xxx, metadata !21, metadata !19), !dbg !22 - %arraydecay = getelementptr inbounds [100 x i32], [100 x i32]* %buf, i64 0, i64 0, !dbg !19 - call void @Capture(i32* %arraydecay), !dbg !20 - call void @llvm.lifetime.end(i64 400, i8* %0) #4, !dbg !21 - ret void, !dbg !21 +; dbg.declare for %zzz and %xxx are gone; replaced with dbg.declare based off the unsafe stack pointer +; CHECK-NOT: call void @llvm.dbg.declare +; CHECK: call void @llvm.dbg.declare(metadata i8* %[[USP]], metadata ![[VAR_ARG:.*]], metadata ![[EXPR_ARG:.*]]) +; CHECK-NOT: call void @llvm.dbg.declare +; CHECK: call void @llvm.dbg.declare(metadata i8* %[[USP]], metadata ![[VAR_LOCAL:.*]], metadata ![[EXPR_LOCAL:.*]]) +; CHECK-NOT: call void @llvm.dbg.declare + + call void @Capture(%struct.S* %zzz), !dbg !23 + call void @Capture(%struct.S* %xxx), !dbg !24 + +; dbg.declare appears before the first use +; CHECK: call void @Capture +; CHECK: call void @Capture + + ret void, !dbg !25 } -; Function Attrs: nounwind argmemonly -declare void @llvm.lifetime.start(i64, i8* nocapture) #1 +; CHECK-DAG: ![[VAR_ARG]] = !DILocalVariable(name: "zzz" +; 100 aligned up to 8 +; CHECK-DAG: ![[EXPR_ARG]] = !DIExpression(DW_OP_deref, DW_OP_minus, 104 + +; CHECK-DAG: ![[VAR_LOCAL]] = !DILocalVariable(name: "xxx" +; CHECK-DAG: ![[EXPR_LOCAL]] = !DIExpression(DW_OP_deref, DW_OP_minus, 208 ; Function Attrs: nounwind readnone -declare void @llvm.dbg.declare(metadata, metadata, metadata) #2 +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 -declare void @Capture(i32*) #3 +declare void @Capture(%struct.S*) #2 -; Function Attrs: nounwind argmemonly -declare void @llvm.lifetime.end(i64, i8* nocapture) #1 - -attributes #0 = { safestack uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } -attributes #1 = { nounwind argmemonly } -attributes #2 = { nounwind readnone } -attributes #3 = { "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } -attributes #4 = { nounwind } +attributes #0 = { safestack uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nounwind readnone } +attributes #2 = { "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } !llvm.dbg.cu = !{!0} -!llvm.module.flags = !{!13, !14} -!llvm.ident = !{!15} +!llvm.module.flags = !{!15, !16} +!llvm.ident = !{!17} -!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 3.8.0 (trunk 248518) (llvm/trunk 248512)", isOptimized: true, runtimeVersion: 0, emissionKind: 1, enums: !2, subprograms: !3) -!1 = !DIFile(filename: "1.cc", directory: "/tmp") +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 3.8.0 (trunk 254019) (llvm/trunk 254036)", isOptimized: false, runtimeVersion: 0, emissionKind: 1, enums: !2, retainedTypes: !3, subprograms: !11) +!1 = !DIFile(filename: "../llvm/2.cc", directory: "/code/build-llvm") !2 = !{} !3 = !{!4} -!4 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 4, type: !5, isLocal: false, isDefinition: true, scopeLine: 4, flags: DIFlagPrototyped, isOptimized: true, variables: !7) -!5 = !DISubroutineType(types: !6) -!6 = !{null} -!7 = !{!8} -!8 = !DILocalVariable(name: "buf", scope: !4, file: !1, line: 5, type: !9) -!9 = !DICompositeType(tag: DW_TAG_array_type, baseType: !10, size: 3200, align: 32, elements: !11) -!10 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed) +!4 = !DICompositeType(tag: DW_TAG_structure_type, name: "S", file: !1, line: 4, size: 800, align: 8, elements: !5, identifier: "_ZTS1S") +!5 = !{!6} +!6 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !"_ZTS1S", file: !1, line: 5, baseType: !7, size: 800, align: 8) +!7 = !DICompositeType(tag: DW_TAG_array_type, baseType: !8, size: 800, align: 8, elements: !9) +!8 = !DIBasicType(name: "char", size: 8, align: 8, encoding: DW_ATE_signed_char) +!9 = !{!10} +!10 = !DISubrange(count: 100) !11 = !{!12} -!12 = !DISubrange(count: 100) -!13 = !{i32 2, !"Dwarf Version", i32 4} -!14 = !{i32 2, !"Debug Info Version", i32 3} -!15 = !{!"clang version 3.8.0 (trunk 248518) (llvm/trunk 248512)"} -!16 = !DILocation(line: 5, column: 3, scope: !4) -!17 = !DIExpression() -!18 = !DILocation(line: 5, column: 7, scope: !4) -!19 = !DILocation(line: 6, column: 11, scope: !4) -!20 = !DILocation(line: 6, column: 3, scope: !4) -!21 = !DILocation(line: 7, column: 1, scope: !4) +!12 = distinct !DISubprogram(name: "f", linkageName: "_Z1f1S", scope: !1, file: !1, line: 10, type: !13, isLocal: false, isDefinition: true, scopeLine: 10, flags: DIFlagPrototyped, isOptimized: false, variables: !2) +!13 = !DISubroutineType(types: !14) +!14 = !{null, !"_ZTS1S"} +!15 = !{i32 2, !"Dwarf Version", i32 4} +!16 = !{i32 2, !"Debug Info Version", i32 3} +!17 = !{!"clang version 3.8.0 (trunk 254019) (llvm/trunk 254036)"} +!18 = !DILocalVariable(name: "zzz", arg: 1, scope: !12, file: !1, line: 10, type: !"_ZTS1S") +!19 = !DIExpression() +!20 = !DILocation(line: 10, column: 10, scope: !12) +!21 = !DILocalVariable(name: "xxx", scope: !12, file: !1, line: 11, type: !"_ZTS1S") +!22 = !DILocation(line: 11, column: 5, scope: !12) +!23 = !DILocation(line: 12, column: 3, scope: !12) +!24 = !DILocation(line: 13, column: 3, scope: !12) +!25 = !DILocation(line: 14, column: 1, scope: !12)