LLVM 18.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/Pass.h"
24
25namespace llvm {
26
27class BasicBlock;
28class Function;
29class Module;
30class TargetLoweringBase;
31class TargetMachine;
32
34private:
35 static constexpr unsigned DefaultSSPBufferSize = 8;
36
37 /// A mapping of AllocaInsts to their required SSP layout.
38 using SSPLayoutMap = DenseMap<const AllocaInst *,
40
41 const TargetMachine *TM = nullptr;
42
43 /// TLI - Keep a pointer of a TargetLowering to consult for determining
44 /// target type sizes.
45 const TargetLoweringBase *TLI = nullptr;
46 Triple Trip;
47
48 Function *F = nullptr;
49 Module *M = nullptr;
50
51 std::optional<DomTreeUpdater> DTU;
52
53 /// Layout - Mapping of allocations to the required SSPLayoutKind.
54 /// StackProtector analysis will update this map when determining if an
55 /// AllocaInst triggers a stack protector.
56 SSPLayoutMap Layout;
57
58 /// The minimum size of buffers that will receive stack smashing
59 /// protection when -fstack-protection is used.
60 unsigned SSPBufferSize = DefaultSSPBufferSize;
61
62 // A prologue is generated.
63 bool HasPrologue = false;
64
65 // IR checking code is generated.
66 bool HasIRCheck = false;
67
68 /// InsertStackProtectors - Insert code into the prologue and epilogue of
69 /// the function.
70 ///
71 /// - The prologue code loads and stores the stack guard onto the stack.
72 /// - The epilogue checks the value stored in the prologue against the
73 /// original value. It calls __stack_chk_fail if they differ.
74 bool InsertStackProtectors();
75
76 /// CreateFailBB - Create a basic block to jump to when the stack protector
77 /// check fails.
78 BasicBlock *CreateFailBB();
79
80public:
81 static char ID; // Pass identification, replacement for typeid.
82
84
85 void getAnalysisUsage(AnalysisUsage &AU) const override;
86
87 // Return true if StackProtector is supposed to be handled by SelectionDAG.
88 bool shouldEmitSDCheck(const BasicBlock &BB) const;
89
90 bool runOnFunction(Function &Fn) override;
91
93
94 /// Check whether or not \p F needs a stack protector based upon the stack
95 /// protector level.
96 static bool requiresStackProtector(Function *F, SSPLayoutMap *Layout = nullptr);
97
98};
99
100} // end namespace llvm
101
102#endif // LLVM_CODEGEN_STACKPROTECTOR_H
Machine Check Debug Module
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:60
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
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
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
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.
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
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18