LLVM 24.0.0git
X86LFIRewritePass.cpp
Go to the documentation of this file.
1//===- X86LFIRewritePass.cpp - Modify code generation for LFI ---*- 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 file implements the X86LFIRewritePass, which prepares machine code for
10// LFI sandboxing by making sure that every address which may legitimately be
11// the destination of an indirect branch is aligned to a bundle boundary.
12//
13//===----------------------------------------------------------------------===//
14
16#include "X86.h"
21
22using namespace llvm;
23
25
26namespace {
27class X86LFIRewriteLegacy : public MachineFunctionPass {
28public:
29 static char ID;
30 X86LFIRewriteLegacy() : MachineFunctionPass(ID) {}
31
32 bool runOnMachineFunction(MachineFunction &MF) override;
33
34 StringRef getPassName() const override { return "X86 LFI rewrites"; }
35};
36} // namespace
37
38char X86LFIRewriteLegacy::ID = 0;
39
41 MBB.setAlignment(std::max(MBB.getAlignment(), BundleAlign), /*MaxBytes=*/0);
42}
43
44// Returns true if MBB may be reached by an indirect branch (does not include
45// jump table targets).
47 const MachineBasicBlock &MBB) {
48 if (MBB.hasAddressTaken() || MBB.isEHPad())
49 return true;
50
51 // With SJLJ exception handling, the dispatch block jumps indirectly to the
52 // block holding the call site's landing pad label, which is no longer marked
53 // as an EH pad by that point.
55 for (const MachineInstr &MI : MBB)
56 if (MI.isEHLabel() &&
57 MF.hasCallSiteLandingPad(MI.getOperand(0).getMCSymbol()))
58 return true;
59
60 return false;
61}
62
64 // Function entry points are reachable through function pointers.
66
67 // Blocks that are the target of a jump table are not considered
68 // address-taken by LLVM, but they are still reached by an indirect branch.
69 if (const MachineJumpTableInfo *JTI = MF.getJumpTableInfo())
70 for (const MachineJumpTableEntry &JTE : JTI->getJumpTables())
71 for (MachineBasicBlock *MBB : JTE.MBBs)
73
74 for (MachineBasicBlock &MBB : MF)
77}
78
79bool X86LFIRewriteLegacy::runOnMachineFunction(MachineFunction &MF) {
81 return true;
82}
83
89
91 return new X86LFIRewriteLegacy();
92}
MachineBasicBlock & MBB
IRTranslator LLVM IR MI
static void alignToBundle(MachineBasicBlock &MBB)
static constexpr Align BundleAlign
static void alignIndirectBranchTargets(MachineFunction &MF)
static bool isIndirectlyReachable(MachineFunction &MF, const MachineBasicBlock &MBB)
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void ensureAlignment(Align A)
ensureAlignment - Make sure the function is at least A bytes aligned.
bool hasCallSiteLandingPad(MCSymbol *Sym)
Return true if the landing pad Eh symbol has an associated call site.
const MachineJumpTableInfo * getJumpTableInfo() const
getJumpTableInfo - Return the jump table info object for the current function.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Representation of each machine instruction.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
TargetOptions Options
ExceptionHandling ExceptionModel
What exception model to use.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
FunctionPass * createX86LFIRewritePass()
@ SjLj
setjmp/longjmp based exceptions
Definition CodeGen.h:58
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
static constexpr Align Constant()
Allow constructions of constexpr Align.
Definition Alignment.h:88
MachineJumpTableEntry - One jump table in the jump table info.