Index: include/llvm/Analysis/OptimizationDiagnosticInfo.h =================================================================== --- include/llvm/Analysis/OptimizationDiagnosticInfo.h +++ include/llvm/Analysis/OptimizationDiagnosticInfo.h @@ -91,7 +91,7 @@ /// \brief Same as above but derives the debug location and the code region /// from the debug location and the basic block of \p Inst, respectively. - void emitOptimizationRemark(const char *PassName, Instruction *Inst, + void emitOptimizationRemark(const char *PassName, const Instruction *Inst, const Twine &Msg) { emitOptimizationRemark(PassName, Inst->getDebugLoc(), Inst->getParent(), Msg); Index: lib/CodeGen/StackProtector.cpp =================================================================== --- lib/CodeGen/StackProtector.cpp +++ lib/CodeGen/StackProtector.cpp @@ -19,6 +19,7 @@ #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/BranchProbabilityInfo.h" #include "llvm/Analysis/EHPersonalities.h" +#include "llvm/Analysis/OptimizationDiagnosticInfo.h" #include "llvm/Analysis/ValueTracking.h" #include "llvm/CodeGen/Passes.h" #include "llvm/IR/Attributes.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,15 @@ if (F->hasFnAttribute(Attribute::SafeStack)) return false; + OptimizationRemarkEmitter ORE(F); + Twine ReasonStub = + "stack protection applied to function " + F->getName() + " due to "; + if (F->hasFnAttribute(Attribute::StackProtectReq)) { + OptimizationRemark Remark( + DEBUG_TYPE, *F, F->getSubprogram(), + ReasonStub + "a function attribute or command-line switch"); + ORE.emit(Remark); NeedsProtector = true; Strong = true; // Use the same heuristic as strong to determine SSPLayout } else if (F->hasFnAttribute(Attribute::StackProtectStrong)) @@ -236,20 +245,24 @@ 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)); + ORE.emitOptimizationRemark(DEBUG_TYPE, &I, ReasonStub + AllocaMsg); NeedsProtector = true; } else if (Strong) { // Require protectors for all alloca calls in strong mode. Layout.insert(std::make_pair(AI, SSPLK_SmallArray)); + ORE.emitOptimizationRemark(DEBUG_TYPE, &I, ReasonStub + AllocaMsg); NeedsProtector = true; } } else { // A call to alloca with a variable size requires protectors. Layout.insert(std::make_pair(AI, SSPLK_LargeArray)); + ORE.emitOptimizationRemark(DEBUG_TYPE, &I, ReasonStub + AllocaMsg); NeedsProtector = true; } continue; @@ -259,6 +272,10 @@ if (ContainsProtectableArray(AI->getAllocatedType(), IsLarge, Strong)) { Layout.insert(std::make_pair(AI, IsLarge ? SSPLK_LargeArray : SSPLK_SmallArray)); + ORE.emitOptimizationRemark( + DEBUG_TYPE, &I, + ReasonStub + + "a stack allocated buffer or struct containing a buffer"); NeedsProtector = true; continue; } @@ -266,6 +283,9 @@ if (Strong && HasAddressTaken(AI)) { ++NumAddrTaken; Layout.insert(std::make_pair(AI, SSPLK_AddrOf)); + ORE.emitOptimizationRemark( + DEBUG_TYPE, &I, + ReasonStub + "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,91 @@ +; 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 + +; Check that no remark is emitted when the switch is not specified. +; RUN: llc %s -o /dev/null 2>&1 | FileCheck %s -check-prefix=NOREMARK -allow-empty +; NOREMARK-NOT: ssp + +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 +}