LLVM 17.0.0git
StackProtector.h
Go to the documentation of this file.
1//===- StackProtector.h - Stack Protector Insertion -------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass inserts stack protectors into functions which need them. A variable
10// with a random value in it is stored onto the stack before the local variables
11// are allocated. Upon exiting the block, the stored value is checked. If it's
12// changed, then there was some sort of violation and the program aborts.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_CODEGEN_STACKPROTECTOR_H
17#define LLVM_CODEGEN_STACKPROTECTOR_H
18
20#include "llvm/ADT/Triple.h"
24#include "llvm/Pass.h"
25
26namespace llvm {
27
28class BasicBlock;
29class DominatorTree;
30class Function;
31class Instruction;
32class Module;
33class TargetLoweringBase;
34class TargetMachine;
35class Type;
36
38private:
39 static constexpr unsigned DefaultSSPBufferSize = 8;
40
41 /// A mapping of AllocaInsts to their required SSP layout.
42 using SSPLayoutMap = DenseMap<const AllocaInst *,
44
45 const TargetMachine *TM = nullptr;
46
47 /// TLI - Keep a pointer of a TargetLowering to consult for determining
48 /// target type sizes.
49 const TargetLoweringBase *TLI = nullptr;
50 Triple Trip;
51
52 Function *F;
53 Module *M;
54
55 std::optional<DomTreeUpdater> DTU;
56
57 /// Layout - Mapping of allocations to the required SSPLayoutKind.
58 /// StackProtector analysis will update this map when determining if an
59 /// AllocaInst triggers a stack protector.
60 SSPLayoutMap Layout;
61
62 /// The minimum size of buffers that will receive stack smashing
63 /// protection when -fstack-protection is used.
64 unsigned SSPBufferSize = DefaultSSPBufferSize;
65
66 /// VisitedPHIs - The set of PHI nodes visited when determining
67 /// if a variable's reference has been taken. This set
68 /// is maintained to ensure we don't visit the same PHI node multiple
69 /// times.
71
72 // A prologue is generated.
73 bool HasPrologue = false;
74
75 // IR checking code is generated.
76 bool HasIRCheck = false;
77
78 /// InsertStackProtectors - Insert code into the prologue and epilogue of
79 /// the function.
80 ///
81 /// - The prologue code loads and stores the stack guard onto the stack.
82 /// - The epilogue checks the value stored in the prologue against the
83 /// original value. It calls __stack_chk_fail if they differ.
84 bool InsertStackProtectors();
85
86 /// CreateFailBB - Create a basic block to jump to when the stack protector
87 /// check fails.
88 BasicBlock *CreateFailBB();
89
90 /// ContainsProtectableArray - Check whether the type either is an array or
91 /// contains an array of sufficient size so that we need stack protectors
92 /// for it.
93 /// \param [out] IsLarge is set to true if a protectable array is found and
94 /// it is "large" ( >= ssp-buffer-size). In the case of a structure with
95 /// multiple arrays, this gets set if any of them is large.
96 bool ContainsProtectableArray(Type *Ty, bool &IsLarge, bool Strong = false,
97 bool InStruct = false) const;
98
99 /// Check whether a stack allocation has its address taken.
100 bool HasAddressTaken(const Instruction *AI, TypeSize AllocSize);
101
102 /// RequiresStackProtector - Check whether or not this function needs a
103 /// stack protector based upon the stack protector level.
104 bool RequiresStackProtector();
105
106public:
107 static char ID; // Pass identification, replacement for typeid.
108
110
111 void getAnalysisUsage(AnalysisUsage &AU) const override;
112
113 // Return true if StackProtector is supposed to be handled by SelectionDAG.
114 bool shouldEmitSDCheck(const BasicBlock &BB) const;
115
116 bool runOnFunction(Function &Fn) override;
117
119};
120
121} // end namespace llvm
122
123#endif // LLVM_CODEGEN_STACKPROTECTOR_H
This header is deprecated in favour of llvm/TargetParser/Triple.h.
RelocType Type
Definition: COFFYAML.cpp:390
Machine Check Debug Module
This file defines the SmallPtrSet class.
an instruction to allocate memory on the stack
Definition: Instructions.h:58
Represent the analysis usage information of a pass.
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
SSPLayoutKind
Stack Smashing Protection (SSP) rules require that vulnerable stack allocations are located close the...
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:450
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
bool shouldEmitSDCheck(const BasicBlock &BB) const
void copyToMachineFrameInfo(MachineFrameInfo &MFI) const
bool runOnFunction(Function &Fn) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
This base class for TargetLowering contains the SelectionDAG-independent parts that can be used from ...
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:78
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18