LLVM 24.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;
31class TargetMachine;
32
34 friend class StackProtectorPass;
35 friend class SSPLayoutAnalysis;
36 friend class StackProtector;
37 static constexpr unsigned DefaultSSPBufferSize = 8;
38
39 /// A mapping of AllocaInsts to their required SSP layout.
40 using SSPLayoutMap =
42
43 /// Layout - Mapping of allocations to the required SSPLayoutKind.
44 /// StackProtector analysis will update this map when determining if an
45 /// AllocaInst triggers a stack protector.
46 SSPLayoutMap Layout;
47
48 bool RequireStackProtector = false;
49
50 // A prologue is generated.
51 bool HasPrologue = false;
52
53 // IR checking code is generated.
54 bool HasIRCheck = false;
55
56public:
57 // Return true if StackProtector is supposed to be handled by SelectionDAG.
58 LLVM_ABI bool shouldEmitSDCheck(const BasicBlock &BB) const;
59
61};
62
63class SSPLayoutAnalysis : public AnalysisInfoMixin<SSPLayoutAnalysis> {
65 using SSPLayoutMap = SSPLayoutInfo::SSPLayoutMap;
66
67 static AnalysisKey Key;
68
69public:
71
73
74 /// Check whether or not \p F needs a stack protector based upon the stack
75 /// protector level.
77 SSPLayoutMap *Layout = nullptr);
78};
79
80class StackProtectorPass : public RequiredPassInfoMixin<StackProtectorPass> {
81 const TargetMachine *TM;
82
83public:
84 explicit StackProtectorPass(const TargetMachine &TM) : TM(&TM) {}
86};
87
89private:
90 /// A mapping of AllocaInsts to their required SSP layout.
91 using SSPLayoutMap = SSPLayoutInfo::SSPLayoutMap;
92
93 const TargetMachine *TM = nullptr;
94
95 Function *F = nullptr;
96 Module *M = nullptr;
97
98 std::optional<DomTreeUpdater> DTU;
99
100 SSPLayoutInfo LayoutInfo;
101
102public:
103 static char ID; // Pass identification, replacement for typeid.
104
106
107 SSPLayoutInfo &getLayoutInfo() { return LayoutInfo; }
108
109 void getAnalysisUsage(AnalysisUsage &AU) const override;
110
111 // Return true if StackProtector is supposed to be handled by SelectionDAG.
112 bool shouldEmitSDCheck(const BasicBlock &BB) const {
113 return LayoutInfo.shouldEmitSDCheck(BB);
114 }
115
116 bool runOnFunction(Function &Fn) override;
117
119 LayoutInfo.copyToMachineFrameInfo(MFI);
120 }
121
122 /// Check whether or not \p F needs a stack protector based upon the stack
123 /// protector level.
125 SSPLayoutMap *Layout = nullptr) {
127 }
128};
129
130} // end namespace llvm
131
132#endif // LLVM_CODEGEN_STACKPROTECTOR_H
#define LLVM_ABI
Definition Compiler.h:211
static bool runOnFunction(Function &F, bool PostInlining)
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
FunctionAnalysisManager FAM
Represent the analysis usage information of a pass.
LLVM Basic Block Representation.
Definition BasicBlock.h:61
FunctionPass(char &pid)
Definition Pass.h:314
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:68
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static LLVM_ABI bool requiresStackProtector(Function *F, SSPLayoutMap *Layout=nullptr)
Check whether or not F needs a stack protector based upon the stack protector level.
LLVM_ABI Result run(Function &F, FunctionAnalysisManager &FAM)
LLVM_ABI void copyToMachineFrameInfo(MachineFrameInfo &MFI) const
friend class StackProtectorPass
LLVM_ABI bool shouldEmitSDCheck(const BasicBlock &BB) const
friend class SSPLayoutAnalysis
friend class StackProtector
StackProtectorPass(const TargetMachine &TM)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
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.
SSPLayoutInfo & getLayoutInfo()
void copyToMachineFrameInfo(MachineFrameInfo &MFI) const
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.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
A CRTP mix-in for passes that should not be skipped.