summaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm18/include/llvm/CodeGen/StackProtector.h
blob: e3fddbe536f071120f8ee6b1cff7c7f87a424be8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#pragma once

#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif

//===- StackProtector.h - Stack Protector Insertion -------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This pass inserts stack protectors into functions which need them. A variable
// with a random value in it is stored onto the stack before the local variables
// are allocated. Upon exiting the block, the stored value is checked. If it's
// changed, then there was some sort of violation and the program aborts.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CODEGEN_STACKPROTECTOR_H
#define LLVM_CODEGEN_STACKPROTECTOR_H

#include "llvm/Analysis/DomTreeUpdater.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Pass.h"
#include "llvm/TargetParser/Triple.h"

namespace llvm {

class BasicBlock;
class Function;
class Module;
class TargetLoweringBase;
class TargetMachine;

class SSPLayoutInfo {
  friend class StackProtectorPass;
  friend class SSPLayoutAnalysis;
  friend class StackProtector;
  static constexpr unsigned DefaultSSPBufferSize = 8;

  /// A mapping of AllocaInsts to their required SSP layout.
  using SSPLayoutMap =
      DenseMap<const AllocaInst *, MachineFrameInfo::SSPLayoutKind>;

  /// Layout - Mapping of allocations to the required SSPLayoutKind.
  /// StackProtector analysis will update this map when determining if an
  /// AllocaInst triggers a stack protector.
  SSPLayoutMap Layout;

  /// The minimum size of buffers that will receive stack smashing
  /// protection when -fstack-protection is used.
  unsigned SSPBufferSize = DefaultSSPBufferSize;

  bool RequireStackProtector = false;

  // A prologue is generated.
  bool HasPrologue = false;

  // IR checking code is generated.
  bool HasIRCheck = false;

public:
  // Return true if StackProtector is supposed to be handled by SelectionDAG.
  bool shouldEmitSDCheck(const BasicBlock &BB) const;

  void copyToMachineFrameInfo(MachineFrameInfo &MFI) const;
};

class SSPLayoutAnalysis : public AnalysisInfoMixin<SSPLayoutAnalysis> {
  friend AnalysisInfoMixin<SSPLayoutAnalysis>;
  using SSPLayoutMap = SSPLayoutInfo::SSPLayoutMap;

  static AnalysisKey Key;

public:
  using Result = SSPLayoutInfo;

  Result run(Function &F, FunctionAnalysisManager &FAM);

  /// Check whether or not \p F needs a stack protector based upon the stack
  /// protector level.
  static bool requiresStackProtector(Function *F,
                                     SSPLayoutMap *Layout = nullptr);
};

class StackProtectorPass : public PassInfoMixin<StackProtectorPass> {
  const TargetMachine *TM;

public:
  explicit StackProtectorPass(const TargetMachine *TM) : TM(TM) {}
  PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
};

class StackProtector : public FunctionPass {
private:
  /// A mapping of AllocaInsts to their required SSP layout.
  using SSPLayoutMap = SSPLayoutInfo::SSPLayoutMap;

  const TargetMachine *TM = nullptr;

  Function *F = nullptr;
  Module *M = nullptr;

  std::optional<DomTreeUpdater> DTU;

  SSPLayoutInfo LayoutInfo;

public:
  static char ID; // Pass identification, replacement for typeid.

  StackProtector();

  void getAnalysisUsage(AnalysisUsage &AU) const override;

  // Return true if StackProtector is supposed to be handled by SelectionDAG.
  bool shouldEmitSDCheck(const BasicBlock &BB) const {
    return LayoutInfo.shouldEmitSDCheck(BB);
  }

  bool runOnFunction(Function &Fn) override;

  void copyToMachineFrameInfo(MachineFrameInfo &MFI) const {
    LayoutInfo.copyToMachineFrameInfo(MFI);
  }

  /// Check whether or not \p F needs a stack protector based upon the stack
  /// protector level.
  static bool requiresStackProtector(Function *F,
                                     SSPLayoutMap *Layout = nullptr) {
    return SSPLayoutAnalysis::requiresStackProtector(F, Layout);
  }
};

} // end namespace llvm

#endif // LLVM_CODEGEN_STACKPROTECTOR_H

#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif