Index: include/llvm/CodeGen/StackProtector.h =================================================================== --- include/llvm/CodeGen/StackProtector.h +++ include/llvm/CodeGen/StackProtector.h @@ -19,6 +19,7 @@ #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/Triple.h" +#include "llvm/IR/DiagnosticInfo.h" #include "llvm/IR/Dominators.h" #include "llvm/IR/ValueMap.h" #include "llvm/Pass.h" @@ -134,6 +135,36 @@ bool runOnFunction(Function &Fn) override; }; + +/// Diagnostic information for why SSP was applied. +class DiagnosticInfoSSP : public DiagnosticInfoWithDebugLocBase { +public: + enum SSPReason { + Unknown = 0, + Alloca = 1, + BufferOrStruct = 2, + AddressTaken = 3, + Attribute = 4 + }; + + /// \p Fn is the function where the diagnostic is being emitted. \p Reason is + /// an enum value representing why the function has stack protection. + DiagnosticInfoSSP(const Function &Fn, SSPReason Reason) + : DiagnosticInfoWithDebugLocBase(DK_SSPReason, DS_Remark, Fn, DebugLoc()), + Func(Fn), Why(Reason) {} + + static bool classof(const DiagnosticInfo *DI) { + return DI->getKind() == DK_SSPReason; + } + + void print(DiagnosticPrinter &DP) const override; + + SSPReason Reason() const { return Why; } + +private: + const Function &Func; + const SSPReason Why; +}; } // end namespace llvm #endif // LLVM_CODEGEN_STACKPROTECTOR_H Index: include/llvm/IR/DiagnosticInfo.h =================================================================== --- include/llvm/IR/DiagnosticInfo.h +++ include/llvm/IR/DiagnosticInfo.h @@ -72,6 +72,7 @@ DK_MIRParser, DK_PGOProfile, DK_Unsupported, + DK_SSPReason, DK_FirstPluginKind }; 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/DiagnosticPrinter.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); @@ -223,6 +224,8 @@ return false; if (F->hasFnAttribute(Attribute::StackProtectReq)) { + F->getContext().diagnose( + DiagnosticInfoSSP(*F, DiagnosticInfoSSP::SSPReason::Attribute)); NeedsProtector = true; Strong = true; // Use the same heuristic as strong to determine SSPLayout } else if (F->hasFnAttribute(Attribute::StackProtectStrong)) @@ -241,15 +244,21 @@ // A call to alloca with size >= SSPBufferSize requires // stack protectors. Layout.insert(std::make_pair(AI, SSPLK_LargeArray)); + F->getContext().diagnose( + DiagnosticInfoSSP(*F, DiagnosticInfoSSP::SSPReason::Alloca)); NeedsProtector = true; } else if (Strong) { // Require protectors for all alloca calls in strong mode. Layout.insert(std::make_pair(AI, SSPLK_SmallArray)); + F->getContext().diagnose( + DiagnosticInfoSSP(*F, DiagnosticInfoSSP::SSPReason::Alloca)); NeedsProtector = true; } } else { // A call to alloca with a variable size requires protectors. Layout.insert(std::make_pair(AI, SSPLK_LargeArray)); + F->getContext().diagnose( + DiagnosticInfoSSP(*F, DiagnosticInfoSSP::SSPReason::Alloca)); NeedsProtector = true; } continue; @@ -259,6 +268,8 @@ if (ContainsProtectableArray(AI->getAllocatedType(), IsLarge, Strong)) { Layout.insert(std::make_pair(AI, IsLarge ? SSPLK_LargeArray : SSPLK_SmallArray)); + F->getContext().diagnose(DiagnosticInfoSSP( + *F, DiagnosticInfoSSP::SSPReason::BufferOrStruct)); NeedsProtector = true; continue; } @@ -266,6 +277,8 @@ if (Strong && HasAddressTaken(AI)) { ++NumAddrTaken; Layout.insert(std::make_pair(AI, SSPLK_AddrOf)); + F->getContext().diagnose(DiagnosticInfoSSP( + *F, DiagnosticInfoSSP::SSPReason::AddressTaken)); NeedsProtector = true; } } @@ -464,3 +477,33 @@ bool StackProtector::shouldEmitSDCheck(const BasicBlock &BB) const { return HasPrologue && !HasIRCheck && dyn_cast(BB.getTerminator()); } + +void DiagnosticInfoSSP::print(DiagnosticPrinter &DP) const { + std::string Str; + raw_string_ostream OS(Str); + + std::string ReasonStr; + switch (Reason()) + { + case Unknown: + ReasonStr = "an unknown reason"; + break; + case Alloca: + ReasonStr = "a call to alloca"; + break; + case BufferOrStruct: + ReasonStr = "a stack allocated buffer or struct containing a buffer"; + break; + case AddressTaken: + ReasonStr = "the address of a local variable being taken"; + break; + case Attribute: + ReasonStr = "a function attribute or use of -fstack-protector-all"; + break; + } + + OS << getLocationStr() << ": SSP applied to function " << Func.getName() + << " due to " << ReasonStr << '\n'; + OS.flush(); + DP << Str; +}