LLVM  17.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"
29 #include "llvm/ADT/SmallVector.h"
30 #include "llvm/ADT/StringRef.h"
31 #include "llvm/ADT/Triple.h"
38 #include "llvm/MC/MCInstrDesc.h"
39 #include "llvm/Support/Debug.h"
40 
41 using namespace llvm;
42 
43 #define PASS_KEY "x86-return-thunks"
44 #define DEBUG_TYPE PASS_KEY
45 
46 namespace {
47 struct X86ReturnThunks final : public MachineFunctionPass {
48  static char ID;
49  X86ReturnThunks() : MachineFunctionPass(ID) {}
50  StringRef getPassName() const override { return "X86 Return Thunks"; }
51  bool runOnMachineFunction(MachineFunction &MF) override;
52 };
53 } // namespace
54 
55 char X86ReturnThunks::ID = 0;
56 
57 bool X86ReturnThunks::runOnMachineFunction(MachineFunction &MF) {
58  LLVM_DEBUG(dbgs() << getPassName() << "\n");
59 
60  bool Modified = false;
61 
62  if (!MF.getFunction().hasFnAttribute(llvm::Attribute::FnRetThunkExtern))
63  return Modified;
64 
65  StringRef ThunkName = "__x86_return_thunk";
66  if (MF.getFunction().getName() == ThunkName)
67  return Modified;
68 
69  const auto &ST = MF.getSubtarget<X86Subtarget>();
70  const bool Is64Bit = ST.getTargetTriple().getArch() == Triple::x86_64;
71  const unsigned RetOpc = Is64Bit ? X86::RET64 : X86::RET32;
73 
74  for (MachineBasicBlock &MBB : MF)
75  for (MachineInstr &Term : MBB.terminators())
76  if (Term.getOpcode() == RetOpc)
77  Rets.push_back(&Term);
78 
79  bool IndCS =
80  MF.getMMI().getModule()->getModuleFlag("indirect_branch_cs_prefix");
81  const MCInstrDesc &CS = ST.getInstrInfo()->get(X86::CS_PREFIX);
82  const MCInstrDesc &JMP = ST.getInstrInfo()->get(X86::TAILJMPd);
83 
84  for (MachineInstr *Ret : Rets) {
85  if (IndCS)
86  BuildMI(Ret->getParent(), Ret->getDebugLoc(), CS);
87  BuildMI(Ret->getParent(), Ret->getDebugLoc(), JMP)
88  .addExternalSymbol(ThunkName.data());
89  Ret->eraseFromParent();
90  Modified = true;
91  }
92 
93  return Modified;
94 }
95 
96 INITIALIZE_PASS(X86ReturnThunks, PASS_KEY, "X86 Return Thunks", false, false)
97 
99  return new X86ReturnThunks();
100 }
MachineInstr.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::AArch64PACKey::ID
ID
Definition: AArch64BaseInfo.h:824
llvm::M68kBeads::Term
@ Term
Definition: M68kBaseInfo.h:90
X86Subtarget.h
MCInstrDesc.h
StringRef.h
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1199
llvm::X86Subtarget
Definition: X86Subtarget.h:52
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition: MachineFunctionPass.h:30
MachineBasicBlock.h
llvm::Triple::x86_64
@ x86_64
Definition: Triple.h:86
llvm::MipsISD::Ret
@ Ret
Definition: MipsISelLowering.h:119
llvm::MachineBasicBlock::terminators
iterator_range< iterator > terminators()
Definition: MachineBasicBlock.h:329
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
X86.h
INITIALIZE_PASS
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
llvm::MCInstrDesc
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:198
LoopDeletionResult::Modified
@ Modified
llvm::StringRef::data
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
llvm::MachineInstrBuilder::addExternalSymbol
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
Definition: MachineInstrBuilder.h:184
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:94
PASS_KEY
#define PASS_KEY
Definition: X86ReturnThunks.cpp:43
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:672
llvm::Function::hasFnAttribute
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.cpp:640
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:66
llvm::ARM_MB::ST
@ ST
Definition: ARMBaseInfo.h:73
MachineFunctionPass.h
llvm::createX86ReturnThunksPass
FunctionPass * createX86ReturnThunksPass()
This pass replaces ret instructions with jmp's to __x86_return thunk.
Definition: X86ReturnThunks.cpp:98
MachineModuleInfo.h
llvm::MachineFunction
Definition: MachineFunction.h:258
Triple.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:308
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::MachineFunction::getFunction
Function & getFunction()
Return the LLVM function that this machine code represents.
Definition: MachineFunction.h:638
llvm::BuildMI
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Definition: MachineInstrBuilder.h:357
SmallVector.h
MachineInstrBuilder.h
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
MachineFunction.h
X86InstrInfo.h
Debug.h