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"
20
21using namespace llvm;
22
24
25namespace {
26class X86LFIRewriteLegacy : public MachineFunctionPass {
27public:
28 static char ID;
29 X86LFIRewriteLegacy() : MachineFunctionPass(ID) {}
30
31 bool runOnMachineFunction(MachineFunction &MF) override;
32
33 StringRef getPassName() const override { return "X86 LFI rewrites"; }
34};
35} // namespace
36
37char X86LFIRewriteLegacy::ID = 0;
38
40 MBB.setAlignment(std::max(MBB.getAlignment(), BundleAlign), /*MaxBytes=*/0);
41}
42
43// Returns true if MBB may be reached by an indirect branch (does not include
44// jump table targets).
46 return MBB.hasAddressTaken() || MBB.isEHPad();
47}
48
50 // Function entry points are reachable through function pointers.
52
53 // Blocks that are the target of a jump table are not considered
54 // address-taken by LLVM, but they are still reached by an indirect branch.
55 // This also covers the SJLJ landing pads, which EmitSjLjDispatchBlock puts
56 // into a jump table.
57 if (const MachineJumpTableInfo *JTI = MF.getJumpTableInfo())
58 for (const MachineJumpTableEntry &JTE : JTI->getJumpTables())
59 for (MachineBasicBlock *MBB : JTE.MBBs)
61
62 for (MachineBasicBlock &MBB : MF) {
65 }
66}
67
68bool X86LFIRewriteLegacy::runOnMachineFunction(MachineFunction &MF) {
70 return true;
71}
72
78
80 return new X86LFIRewriteLegacy();
81}
MachineBasicBlock & MBB
static void alignToBundle(MachineBasicBlock &MBB)
static constexpr Align BundleAlign
static void alignIndirectBranchTargets(MachineFunction &MF)
static bool isIndirectlyReachable(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.
const MachineJumpTableInfo * getJumpTableInfo() const
getJumpTableInfo - Return the jump table info object for the current function.
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
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
FunctionPass * createX86LFIRewritePass()
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.