Index: lib/CodeGen/StackProtector.cpp =================================================================== --- lib/CodeGen/StackProtector.cpp +++ lib/CodeGen/StackProtector.cpp @@ -26,6 +26,7 @@ #include "llvm/IR/DataLayout.h" #include "llvm/IR/DebugInfo.h" #include "llvm/IR/DerivedTypes.h" +#include "llvm/IR/DiagnosticInfo.h" #include "llvm/IR/Function.h" #include "llvm/IR/GlobalValue.h" #include "llvm/IR/GlobalVariable.h" @@ -51,7 +52,7 @@ char StackProtector::ID = 0; INITIALIZE_TM_PASS(StackProtector, "stack-protector", "Insert stack protectors", - false, true) + false, true) FunctionPass *llvm::createStackProtectorPass(const TargetMachine *TM) { return new StackProtector(TM); @@ -222,7 +223,14 @@ if (F->hasFnAttribute(Attribute::SafeStack)) return false; + LLVMContext & Ctx = F->getContext(); + Twine StackProtectorReasonMessage = + "stack protection applied to function " + F->getName() + " due to "; + if (F->hasFnAttribute(Attribute::StackProtectReq)) { + emitOptimizationRemark(Ctx, DEBUG_TYPE, *F, F->getSubprogram(), + StackProtectorReasonMessage + + "a function attribute or command-line switch"); NeedsProtector = true; Strong = true; // Use the same heuristic as strong to determine SSPLayout } else if (F->hasFnAttribute(Attribute::StackProtectStrong)) @@ -236,20 +244,27 @@ for (const Instruction &I : BB) { if (const AllocaInst *AI = dyn_cast(&I)) { if (AI->isArrayAllocation()) { + Twine AllocaMsg = "a call to alloca or use of a variable length array"; if (const auto *CI = dyn_cast(AI->getArraySize())) { if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize) { // A call to alloca with size >= SSPBufferSize requires // stack protectors. Layout.insert(std::make_pair(AI, SSPLK_LargeArray)); + emitOptimizationRemark(Ctx, DEBUG_TYPE, *F, I.getDebugLoc(), + StackProtectorReasonMessage + AllocaMsg); NeedsProtector = true; } else if (Strong) { // Require protectors for all alloca calls in strong mode. Layout.insert(std::make_pair(AI, SSPLK_SmallArray)); + emitOptimizationRemark(Ctx, DEBUG_TYPE, *F, I.getDebugLoc(), + StackProtectorReasonMessage + AllocaMsg); NeedsProtector = true; } } else { // A call to alloca with a variable size requires protectors. Layout.insert(std::make_pair(AI, SSPLK_LargeArray)); + emitOptimizationRemark(Ctx, DEBUG_TYPE, *F, I.getDebugLoc(), + StackProtectorReasonMessage + AllocaMsg); NeedsProtector = true; } continue; @@ -259,6 +274,10 @@ if (ContainsProtectableArray(AI->getAllocatedType(), IsLarge, Strong)) { Layout.insert(std::make_pair(AI, IsLarge ? SSPLK_LargeArray : SSPLK_SmallArray)); + emitOptimizationRemark( + Ctx, DEBUG_TYPE, *F, I.getDebugLoc(), + StackProtectorReasonMessage + + "a stack allocated buffer or struct containing a buffer"); NeedsProtector = true; continue; } @@ -266,6 +285,10 @@ if (Strong && HasAddressTaken(AI)) { ++NumAddrTaken; Layout.insert(std::make_pair(AI, SSPLK_AddrOf)); + emitOptimizationRemark( + Ctx, DEBUG_TYPE, *F, I.getDebugLoc(), + StackProtectorReasonMessage + + "the address of a local variable being taken"); NeedsProtector = true; } } Index: test/CodeGen/X86/stack-protector-remarks.ll =================================================================== --- test/CodeGen/X86/stack-protector-remarks.ll +++ test/CodeGen/X86/stack-protector-remarks.ll @@ -0,0 +1,87 @@ +; RUN: llc %s -pass-remarks=stack-protector -o /dev/null 2>&1 | FileCheck %s +; CHECK-NOT: nossp +; CHECK-NOT: alloca_fixed_small_nossp +; CHECK: function attribute_ssp +; CHECK-SAME: a function attribute or command-line switch +; CHECK: function alloca_fixed_small_ssp +; CHECK-SAME: a call to alloca or use of a variable length array +; CHECK: function alloca_fixed_large_ssp +; CHECK-SAME: a call to alloca or use of a variable length array +; CHECK: function alloca_variable_ssp +; CHECK-SAME: a call to alloca or use of a variable length array +; CHECK: function buffer_ssp +; CHECK-SAME: a stack allocated buffer or struct containing a buffer +; CHECK: function struct_ssp +; CHECK-SAME: a stack allocated buffer or struct containing a buffer +; CHECK: function address_ssp +; CHECK-SAME: the address of a local variable being taken +; CHECK: function multiple_ssp +; CHECK-SAME: a function attribute or command-line switch +; CHECK: function multiple_ssp +; CHECK-SAME: a stack allocated buffer or struct containing a buffer +; CHECK: function multiple_ssp +; CHECK-SAME: a stack allocated buffer or struct containing a buffer +; CHECK: function multiple_ssp +; CHECK-SAME: the address of a local variable being taken +; CHECK: function multiple_ssp +; CHECK-SAME: a call to alloca or use of a variable length array + +define void @nossp() sspstrong { + ret void +} + +define void @attribute_ssp() sspreq { + ret void +} + +define void @alloca_fixed_small_nossp() ssp { + %1 = alloca i8, i64 2, align 16 + ret void +} + +define void @alloca_fixed_small_ssp() sspstrong { + %1 = alloca i8, i64 2, align 16 + ret void +} + +define void @alloca_fixed_large_ssp() ssp { + %1 = alloca i8, i64 64, align 16 + ret void +} + +define void @alloca_variable_ssp(i64 %x) ssp { + %1 = alloca i8, i64 %x, align 16 + ret void +} + +define void @buffer_ssp() sspstrong { + %x = alloca [64 x i32], align 16 + ret void +} + +%struct.X = type { [64 x i32] } +define void @struct_ssp() sspstrong { + %x = alloca %struct.X, align 4 + ret void +} + +define void @address_ssp() sspstrong { +entry: + %x = alloca i32, align 4 + %y = alloca i32*, align 8 + store i32 32, i32* %x, align 4 + store i32* %x, i32** %y, align 8 + ret void +} + +define void @multiple_ssp() sspreq { +entry: + %x = alloca %struct.X, align 4 + %y = alloca [64 x i32], align 16 + %a = alloca i32, align 4 + %b = alloca i32*, align 8 + %0 = alloca i8, i64 2, align 16 + store i32 32, i32* %a, align 4 + store i32* %a, i32** %b, align 8 + ret void +}