LLVM 22.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
22#include "llvm/IR/PassManager.h"
23#include "llvm/Pass.h"
25
26namespace llvm {
27
28class BasicBlock;
29class Function;
30class Module;
32class TargetMachine;
33
35 friend class StackProtectorPass;
36 friend class SSPLayoutAnalysis;
37 friend class StackProtector;
38 static constexpr unsigned DefaultSSPBufferSize = 8;
39
40 /// A mapping of AllocaInsts to their required SSP layout.
41 using SSPLayoutMap =
43
44 /// Layout - Mapping of allocations to the required SSPLayoutKind.
45 /// StackProtector analysis will update this map when determining if an
46 /// AllocaInst triggers a stack protector.
47 SSPLayoutMap Layout;
48
49 /// The minimum size of buffers that will receive stack smashing
50 /// protection when -fstack-protection is used.
51 unsigned SSPBufferSize = DefaultSSPBufferSize;
52
53 bool RequireStackProtector = false;
54
55 // A prologue is generated.
56 bool HasPrologue = false;
57
58 // IR checking code is generated.
59 bool HasIRCheck = false;
60
61public:
62 // Return true if StackProtector is supposed to be handled by SelectionDAG.
63 bool shouldEmitSDCheck(const BasicBlock &BB) const;
64
66};
67
68class SSPLayoutAnalysis : public AnalysisInfoMixin<SSPLayoutAnalysis> {
70 using SSPLayoutMap = SSPLayoutInfo::SSPLayoutMap;
71
72 static AnalysisKey Key;
73
74public:
76
78
79 /// Check whether or not \p F needs a stack protector based upon the stack
80 /// protector level.
82 SSPLayoutMap *Layout = nullptr);
83};
84
85class StackProtectorPass : public PassInfoMixin<StackProtectorPass> {
86 const TargetMachine *TM;
87
88public:
89 explicit StackProtectorPass(const TargetMachine *TM) : TM(TM) {}
91};
92
94private:
95 /// A mapping of AllocaInsts to their required SSP layout.
96 using SSPLayoutMap = SSPLayoutInfo::SSPLayoutMap;
97
98 const TargetMachine *TM = nullptr;
99
100 Function *F = nullptr;
101 Module *M = nullptr;
102
103 std::optional<DomTreeUpdater> DTU;
104
105 SSPLayoutInfo LayoutInfo;
106
107public:
108 static char ID; // Pass identification, replacement for typeid.
109
111
112 SSPLayoutInfo &getLayoutInfo() { return LayoutInfo; }
113
114 void getAnalysisUsage(AnalysisUsage &AU) const override;
115
116 // Return true if StackProtector is supposed to be handled by SelectionDAG.
117 bool shouldEmitSDCheck(const BasicBlock &BB) const {
118 return LayoutInfo.shouldEmitSDCheck(BB);
119 }
120
121 bool runOnFunction(Function &Fn) override;
122
124 LayoutInfo.copyToMachineFrameInfo(MFI);
125 }
126
127 /// Check whether or not \p F needs a stack protector based upon the stack
128 /// protector level.
130 SSPLayoutMap *Layout = nullptr) {
132 }
133};
134
135} // end namespace llvm
136
137#endif // LLVM_CODEGEN_STACKPROTECTOR_H
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:55
FunctionAnalysisManager FAM
Represent the analysis usage information of a pass.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
FunctionPass(char &pid)
Definition Pass.h:316
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static bool requiresStackProtector(Function *F, SSPLayoutMap *Layout=nullptr)
Check whether or not F needs a stack protector based upon the stack protector level.
Result run(Function &F, FunctionAnalysisManager &FAM)
void copyToMachineFrameInfo(MachineFrameInfo &MFI) const
friend class StackProtectorPass
bool shouldEmitSDCheck(const BasicBlock &BB) const
friend class SSPLayoutAnalysis
friend class StackProtector
StackProtectorPass(const TargetMachine *TM)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
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
static bool requiresStackProtector(Function *F, SSPLayoutMap *Layout=nullptr)
Check whether or not F needs a stack protector based upon the stack protector level.
bool runOnFunction(Function &Fn) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
SSPLayoutInfo & getLayoutInfo()
void copyToMachineFrameInfo(MachineFrameInfo &MFI) const
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.
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition PassManager.h:93
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition PassManager.h:70