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
23#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 = nullptr;
53 Module *M = nullptr;
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 // A prologue is generated.
67 bool HasPrologue = false;
68
69 // IR checking code is generated.
70 bool HasIRCheck = false;
71
72 /// InsertStackProtectors - Insert code into the prologue and epilogue of
73 /// the function.
74 ///
75 /// - The prologue code loads and stores the stack guard onto the stack.
76 /// - The epilogue checks the value stored in the prologue against the
77 /// original value. It calls __stack_chk_fail if they differ.
78 bool InsertStackProtectors();
79
80 /// CreateFailBB - Create a basic block to jump to when the stack protector
81 /// check fails.
82 BasicBlock *CreateFailBB();
83
84public:
85 static char ID; // Pass identification, replacement for typeid.
86
88
89 void getAnalysisUsage(AnalysisUsage &AU) const override;
90
91 // Return true if StackProtector is supposed to be handled by SelectionDAG.
92 bool shouldEmitSDCheck(const BasicBlock &BB) const;
93
94 bool runOnFunction(Function &Fn) override;
95
97
98 /// Check whether or not \p F needs a stack protector based upon the stack
99 /// protector level.
100 static bool requiresStackProtector(Function *F, SSPLayoutMap *Layout = nullptr);
101
102};
103
104} // end namespace llvm
105
106#endif // LLVM_CODEGEN_STACKPROTECTOR_H
RelocType Type
Definition: COFFYAML.cpp:391
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: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