llvm.org GIT mirror llvm / 0dcba2f
Recommit r186217 with testcase fix: Use the function attributes to pass along the stack protector buffer size. Now that we have robust function attributes, don't use a command line option to specify the stack protecto buffer size. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@186863 91177308-0d34-0410-b5e6-96231b3b80d8 Bill Wendling 6 years ago
7 changed file(s) with 23 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
219219 cl::value_desc("pass-name"),
220220 cl::init(""));
221221
222 cl::opt
223 SSPBufferSize("stack-protector-buffer-size", cl::init(8),
224 cl::desc("Lower bound for a buffer to be considered for "
225 "stack protection"));
226222 #endif
4747 UseSoftFloat(false), NoZerosInBSS(false),
4848 JITEmitDebugInfo(false), JITEmitDebugInfoToDisk(false),
4949 GuaranteedTailCallOpt(false), DisableTailCalls(false),
50 StackAlignmentOverride(0), RealignStack(true), SSPBufferSize(0),
50 StackAlignmentOverride(0), RealignStack(true),
5151 EnableFastISel(false), PositionIndependentExecutable(false),
5252 EnableSegmentedStacks(false), UseInitArray(false), TrapFuncName(""),
5353 FloatABIType(FloatABI::Default), AllowFPOpFusion(FPOpFusion::Standard)
149149 /// RealignStack - This flag indicates whether the stack should be
150150 /// automatically realigned, if needed.
151151 unsigned RealignStack : 1;
152
153 /// SSPBufferSize - The minimum size of buffers that will receive stack
154 /// smashing protection when -fstack-protection is used.
155 unsigned SSPBufferSize;
156152
157153 /// EnableFastISel - This flag enables fast-path instruction selection
158154 /// which trades away generated code quality in favor of reducing
223219 ARE_EQUAL(DisableTailCalls) &&
224220 ARE_EQUAL(StackAlignmentOverride) &&
225221 ARE_EQUAL(RealignStack) &&
226 ARE_EQUAL(SSPBufferSize) &&
227222 ARE_EQUAL(EnableFastISel) &&
228223 ARE_EQUAL(PositionIndependentExecutable) &&
229224 ARE_EQUAL(EnableSegmentedStacks) &&
3232 #include "llvm/Pass.h"
3333 #include "llvm/Support/CommandLine.h"
3434 #include "llvm/Target/TargetLowering.h"
35 #include
3536 using namespace llvm;
3637
3738 STATISTIC(NumFunProtected, "Number of functions protected");
5152 Module *M;
5253
5354 DominatorTree *DT;
55
56 /// \brief The minimum size of buffers that will receive stack smashing
57 /// protection when -fstack-protection is used.
58 unsigned SSPBufferSize;
5459
5560 /// VisitedPHIs - The set of PHI nodes visited when determining
5661 /// if a variable's reference has been taken. This set
8489 bool RequiresStackProtector();
8590 public:
8691 static char ID; // Pass identification, replacement for typeid.
87 StackProtector() : FunctionPass(ID), TM(0), TLI(0) {
92 StackProtector() : FunctionPass(ID), TM(0), TLI(0), SSPBufferSize(0) {
8893 initializeStackProtectorPass(*PassRegistry::getPassRegistry());
8994 }
9095 StackProtector(const TargetMachine *TM)
91 : FunctionPass(ID), TM(TM), TLI(0), Trip(TM->getTargetTriple()) {
96 : FunctionPass(ID), TM(TM), TLI(0), Trip(TM->getTargetTriple()),
97 SSPBufferSize(8) {
9298 initializeStackProtectorPass(*PassRegistry::getPassRegistry());
9399 }
94100
115121 TLI = TM->getTargetLowering();
116122
117123 if (!RequiresStackProtector()) return false;
124
125 Attribute Attr =
126 Fn.getAttributes().getAttribute(AttributeSet::FunctionIndex,
127 "stack-protector-buffer-size");
128 if (Attr.isStringAttribute())
129 SSPBufferSize = atoi(Attr.getValueAsString().data());
118130
119131 ++NumFunProtected;
120132 return InsertStackProtectors();
131143 // protector
132144 if (Strong)
133145 return true;
134 const TargetMachine &TM = TLI->getTargetMachine();
135146 if (!AT->getElementType()->isIntegerTy(8)) {
136147 // If we're on a non-Darwin platform or we're inside of a structure, don't
137148 // add stack protectors unless the array is a character array.
141152
142153 // If an array has more than SSPBufferSize bytes of allocated space, then we
143154 // emit stack protectors.
144 if (TM.Options.SSPBufferSize <= TLI->getDataLayout()->getTypeAllocSize(AT))
155 if (SSPBufferSize <= TLI->getDataLayout()->getTypeAllocSize(AT))
145156 return true;
146157 }
147158
229240
230241 if (const ConstantInt *CI =
231242 dyn_cast(AI->getArraySize())) {
232 unsigned BufferSize = TLI->getTargetMachine().Options.SSPBufferSize;
233 if (CI->getLimitedValue(BufferSize) >= BufferSize)
243 if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize)
234244 // A call to alloca with size >= SSPBufferSize requires
235245 // stack protectors.
236246 return true;
237 } else // A call to alloca with a variable size requires protectors.
247 } else {
248 // A call to alloca with a variable size requires protectors.
238249 return true;
250 }
239251 }
240252
241253 if (ContainsProtectableArray(AI->getAllocatedType(), Strong))
None ; RUN: llc -filetype=obj -O0 -stack-protector-buffer-size=1 < %s
0 ; RUN: llc -filetype=obj -O0 < %s
11 ; Test that we handle DBG_VALUEs in a register without crashing.
22 ;
33 ; Generated from clang with -fsanitize=address:
2222 @__asan_gen_ = private unnamed_addr constant [16 x i8] c"1 32 4 5 .addr \00", align 1
2323
2424 ; Function Attrs: sanitize_address uwtable
25 define void @_Z4funci(%struct.A* noalias sret %agg.result, i32) #0 {
25 define void @_Z4funci(%struct.A* noalias sret %agg.result, i32) #0 "stack-protector-buffer-size"="1" {
2626 entry:
2727 %MyAlloca = alloca [96 x i8], align 32
2828 %1 = ptrtoint [96 x i8]* %MyAlloca to i64
8888
8989 declare void @_ZN1AC1Ev(%struct.A*) #2
9090
91 define internal void @asan.module_ctor() {
91 define internal void @asan.module_ctor() "stack-protector-buffer-size"="1" {
9292 call void @__asan_init_v3()
9393 %1 = load volatile i64* @__asan_mapping_offset
9494 %2 = load volatile i64* @__asan_mapping_scale
280280 Options.PositionIndependentExecutable = EnablePIE;
281281 Options.EnableSegmentedStacks = SegmentedStacks;
282282 Options.UseInitArray = UseInitArray;
283 Options.SSPBufferSize = SSPBufferSize;
284283
285284 OwningPtr
286285 target(TheTarget->createTargetMachine(TheTriple.getTriple(),
150150 cl::desc("Use .init_array instead of .ctors."),
151151 cl::init(false));
152152
153 static cl::opt
154 SSPBufferSize("stack-protector-buffer-size", cl::init(8),
155 cl::desc("Lower bound for a buffer to be considered for "
156 "stack protection"));
157
158153 LTOModule::LTOModule(llvm::Module *m, llvm::TargetMachine *t)
159154 : _module(m), _target(t),
160155 _context(_target->getMCAsmInfo(), _target->getRegisterInfo(), NULL),
260255 Options.PositionIndependentExecutable = EnablePIE;
261256 Options.EnableSegmentedStacks = SegmentedStacks;
262257 Options.UseInitArray = UseInitArray;
263 Options.SSPBufferSize = SSPBufferSize;
264258 }
265259
266260 LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
509509 Options.PositionIndependentExecutable = EnablePIE;
510510 Options.EnableSegmentedStacks = SegmentedStacks;
511511 Options.UseInitArray = UseInitArray;
512 Options.SSPBufferSize = SSPBufferSize;
513512 return Options;
514513 }
515514