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;
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 bool RequireStackProtector = false;
50
51 // A prologue is generated.
52 bool HasPrologue = false;
53
54 // IR checking code is generated.
55 bool HasIRCheck = false;
56
57public:
58 // Return true if StackProtector is supposed to be handled by SelectionDAG.
59 LLVM_ABI bool shouldEmitSDCheck(const BasicBlock &BB) const;
60
62};
63
64class SSPLayoutAnalysis : public AnalysisInfoMixin<SSPLayoutAnalysis> {
66 using SSPLayoutMap = SSPLayoutInfo::SSPLayoutMap;
67
68 static AnalysisKey Key;
69
70public:
72
74
75 /// Check whether or not \p F needs a stack protector based upon the stack
76 /// protector level.
78 SSPLayoutMap *Layout = nullptr);
79};
80
81class StackProtectorPass : public RequiredPassInfoMixin<StackProtectorPass> {
82 const TargetMachine *TM;
83
84public:
85 explicit StackProtectorPass(const TargetMachine &TM) : TM(&TM) {}
87};
88
90private:
91 /// A mapping of AllocaInsts to their required SSP layout.
92 using SSPLayoutMap = SSPLayoutInfo::SSPLayoutMap;
93
94 const TargetMachine *TM = nullptr;
95
96 Function *F = nullptr;
97 Module *M = nullptr;
98
99 std::optional<DomTreeUpdater> DTU;
100
101 SSPLayoutInfo LayoutInfo;
102
103public:
104 static char ID; // Pass identification, replacement for typeid.
105
107
108 SSPLayoutInfo &getLayoutInfo() { return LayoutInfo; }
109
110 void getAnalysisUsage(AnalysisUsage &AU) const override;
111
112 // Return true if StackProtector is supposed to be handled by SelectionDAG.
113 bool shouldEmitSDCheck(const BasicBlock &BB) const {
114 return LayoutInfo.shouldEmitSDCheck(BB);
115 }
116
117 bool runOnFunction(Function &Fn) override;
118
120 LayoutInfo.copyToMachineFrameInfo(MFI);
121 }
122
123 /// Check whether or not \p F needs a stack protector based upon the stack
124 /// protector level.
126 SSPLayoutMap *Layout = nullptr) {
128 }
129};
130
131} // end namespace llvm
132
133#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:62
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
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.
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.