LLVM 23.0.0git
X86ReturnThunks.cpp
Go to the documentation of this file.
1//==- X86ReturnThunks.cpp - Replace rets with thunks or inline thunks --=//
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/// \file
9///
10/// Pass that replaces ret instructions with a jmp to __x86_return_thunk.
11///
12/// This corresponds to -mfunction-return=thunk-extern or
13/// __attribute__((function_return("thunk-extern").
14///
15/// This pass is a minimal implementation necessary to help mitigate
16/// RetBleed for the Linux kernel.
17///
18/// Should support for thunk or thunk-inline be necessary in the future, then
19/// this pass should be combined with x86-retpoline-thunks which already has
20/// machinery to emit thunks. Until then, YAGNI.
21///
22/// This pass is very similar to x86-lvi-ret.
23///
24//===----------------------------------------------------------------------===//
25
26#include "X86.h"
27#include "X86InstrInfo.h"
28#include "X86Subtarget.h"
30#include "llvm/ADT/StringRef.h"
37#include "llvm/IR/Module.h"
38#include "llvm/MC/MCInstrDesc.h"
39#include "llvm/Support/Debug.h"
41
42using namespace llvm;
43
44#define PASS_KEY "x86-return-thunks"
45#define DEBUG_TYPE PASS_KEY
46
47constexpr StringRef X86ReturnThunksPassName = "X86 Return Thunks";
48
49namespace {
50struct X86ReturnThunksLegacy final : public MachineFunctionPass {
51 static char ID;
52 X86ReturnThunksLegacy() : MachineFunctionPass(ID) {}
53 StringRef getPassName() const override { return X86ReturnThunksPassName; }
54 bool runOnMachineFunction(MachineFunction &MF) override;
55};
56} // namespace
57
58char X86ReturnThunksLegacy::ID = 0;
59
62
63 bool Modified = false;
64
65 if (!MF.getFunction().hasFnAttribute(llvm::Attribute::FnRetThunkExtern))
66 return Modified;
67
68 StringRef ThunkName = "__x86_return_thunk";
69 if (MF.getFunction().getName() == ThunkName)
70 return Modified;
71
72 const auto &ST = MF.getSubtarget<X86Subtarget>();
73 const bool Is64Bit = ST.getTargetTriple().isX86_64();
74 const unsigned RetOpc = Is64Bit ? X86::RET64 : X86::RET32;
76
77 for (MachineBasicBlock &MBB : MF)
78 for (MachineInstr &Term : MBB.terminators())
79 if (Term.getOpcode() == RetOpc)
80 Rets.push_back(&Term);
81
82 bool IndCS =
83 MF.getFunction().getParent()->getModuleFlag("indirect_branch_cs_prefix");
84 const MCInstrDesc &CS = ST.getInstrInfo()->get(X86::CS_PREFIX);
85 const MCInstrDesc &JMP = ST.getInstrInfo()->get(X86::TAILJMPd);
86
87 for (MachineInstr *Ret : Rets) {
88 if (IndCS)
89 BuildMI(Ret->getParent(), Ret->getDebugLoc(), CS);
90 BuildMI(Ret->getParent(), Ret->getDebugLoc(), JMP)
91 .addExternalSymbol(ThunkName.data());
92 Ret->eraseFromParent();
93 Modified = true;
94 }
95
96 return Modified;
97}
98
99bool X86ReturnThunksLegacy::runOnMachineFunction(MachineFunction &MF) {
100 return runX86ReturnThunks(MF);
101}
102
103PreservedAnalyses
110
111INITIALIZE_PASS(X86ReturnThunksLegacy, PASS_KEY, "X86 Return Thunks", false,
112 false)
113
115 return new X86ReturnThunksLegacy();
116}
MachineBasicBlock & MBB
Module.h This file contains the declarations for the Module class.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:114
#define PASS_KEY
constexpr StringRef X86ReturnThunksPassName
static bool runX86ReturnThunks(MachineFunction &MF)
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:730
Module * getParent()
Get the module that this global value is contained inside of...
Describe properties that are true of each instruction in the target description file.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
Representation of each machine instruction.
LLVM_ABI void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
Metadata * getModuleFlag(StringRef Key) const
Return the corresponding value if Key appears in module flags, otherwise return null.
Definition Module.cpp:358
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:140
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
FunctionPass * createX86ReturnThunksLegacyPass()