LLVM 23.0.0git
AArch64MCLFIRewriter.h
Go to the documentation of this file.
1//===- AArch64MCLFIRewriter.h -----------------------------------*- 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 declares the AArch64MCLFIRewriter class, the AArch64 specific
10// subclass of MCLFIRewriter.
11//
12//===----------------------------------------------------------------------===//
13#ifndef LLVM_LIB_TARGET_AARCH64_MCTARGETDESC_AARCH64MCLFIREWRITER_H
14#define LLVM_LIB_TARGET_AARCH64_MCTARGETDESC_AARCH64MCLFIREWRITER_H
15
17#include "llvm/MC/MCInstrInfo.h"
19#include "llvm/MC/MCRegister.h"
21
22#include <optional>
23
24namespace llvm {
25class MCContext;
26class MCExpr;
27class MCInst;
28class MCOperand;
29class MCStreamer;
30class MCSubtargetInfo;
31class MCSymbol;
32
33/// Rewrites AArch64 instructions for LFI sandboxing.
34///
35/// This class implements the LFI (Lightweight Fault Isolation) rewriting
36/// for AArch64 instructions. It transforms instructions to ensure memory
37/// accesses and control flow are confined within the sandbox region.
38///
39/// Reserved registers:
40/// - X27: Sandbox base address (always holds the base)
41/// - X28: Safe address register (always within sandbox)
42/// - X26: Scratch register for intermediate calculations
43/// - X25: context register (points to thread-local runtime data)
44/// - SP: Stack pointer (always within sandbox)
45/// - X30: Link register (always within sandbox)
47public:
48 AArch64MCLFIRewriter(MCContext &Ctx, std::unique_ptr<MCRegisterInfo> &&RI,
49 std::unique_ptr<MCInstrInfo> &&II)
50 : MCLFIRewriter(Ctx, std::move(RI), std::move(II)) {}
51
52 bool rewriteInst(const MCInst &Inst, MCStreamer &Out,
53 const MCSubtargetInfo &STI) override;
54
55 void onLabel(const MCSymbol *Symbol) override;
56
57private:
58 /// Recursion guard to prevent infinite loops when emitting instructions.
59 bool Guard = false;
60
61 /// Deferred `.tlsdesccall` symbol. The directive attaches a
62 /// R_AARCH64_TLSDESC_CALL relocation to the following BLR. Since LFI inserts
63 /// a guard before that BLR, the marker is deferred and re-emitted between
64 /// the guard and the branch so the relocation stays on the BLR.
65 const MCExpr *PendingTLSDescCall = nullptr;
66
67 /// Rewriter state for implementing the guard-elimination optimization, which
68 /// allows redundant add masks to be skipped. When it holds a value, x28 is
69 /// known to already hold the guarded value of that register.
70 std::optional<MCRegister> ActiveGuardReg;
71
72 // Instruction classification. Returns the reserved register that may be
73 // modified, or an invalid register if no reserved register is touched.
74 MCRegister mayModifyReserved(const MCInst &Inst) const;
75 bool mayModifySP(const MCInst &Inst) const;
76
77 // Instruction emission.
78 void emitInst(const MCInst &Inst, MCStreamer &Out,
79 const MCSubtargetInfo &STI);
80 void emitAddMask(MCRegister Dest, MCRegister Src, MCStreamer &Out,
81 const MCSubtargetInfo &STI);
82 void emitBranch(unsigned Opcode, MCRegister Target, MCStreamer &Out,
83 const MCSubtargetInfo &STI);
84 void emitPendingTLSDescCall(MCStreamer &Out, const MCSubtargetInfo &STI);
85 void emitMov(MCRegister Dest, MCRegister Src, MCStreamer &Out,
86 const MCSubtargetInfo &STI);
87 void emitAddImm(MCRegister Dest, MCRegister Src, int64_t Imm, MCStreamer &Out,
88 const MCSubtargetInfo &STI);
89 void emitAddReg(MCRegister Dest, MCRegister Src1, MCRegister Src2,
90 unsigned Shift, MCStreamer &Out, const MCSubtargetInfo &STI);
91 void emitAddRegExtend(MCRegister Dest, MCRegister Src1, MCRegister Src2,
92 AArch64_AM::ShiftExtendType ExtType, unsigned Shift,
93 MCStreamer &Out, const MCSubtargetInfo &STI);
94 void emitMemRoW(unsigned Opcode, const MCOperand &DataOp, MCRegister BaseReg,
95 MCStreamer &Out, const MCSubtargetInfo &STI);
96
97 // Rewriting logic.
98 void doRewriteInst(const MCInst &Inst, MCStreamer &Out,
99 const MCSubtargetInfo &STI);
100
101 // Control flow.
102 void rewriteIndirectBranch(const MCInst &Inst, MCStreamer &Out,
103 const MCSubtargetInfo &STI);
104 void rewriteReturn(const MCInst &Inst, MCStreamer &Out,
105 const MCSubtargetInfo &STI);
106
107 // Memory access.
108 void rewriteLoadStore(const MCInst &Inst, MCStreamer &Out,
109 const MCSubtargetInfo &STI);
110 void rewriteLoadStoreBase(const MCInst &Inst, MCStreamer &Out,
111 const MCSubtargetInfo &STI);
112 bool rewriteLoadStoreRoW(const MCInst &Inst, MCStreamer &Out,
113 const MCSubtargetInfo &STI);
114
115 // SP register modification.
116 void rewriteSPModification(const MCInst &Inst, MCStreamer &Out,
117 const MCSubtargetInfo &STI);
118
119 // Link register modification.
120 void rewriteLRModification(const MCInst &Inst, MCStreamer &Out,
121 const MCSubtargetInfo &STI);
122
123 // System instructions.
124 void rewriteSyscall(const MCInst &Inst, MCStreamer &Out,
125 const MCSubtargetInfo &STI);
126 void rewriteTPRead(const MCInst &Inst, MCStreamer &Out,
127 const MCSubtargetInfo &STI);
128 void rewriteTPWrite(const MCInst &Inst, MCStreamer &Out,
129 const MCSubtargetInfo &STI);
130 void rewriteVASysOp(const MCInst &Inst, MCStreamer &Out,
131 const MCSubtargetInfo &STI);
132};
133
134/// Returns true if \p Opcode is a pre- or post-indexed memory access that the
135/// LFI rewriter expands with a base-register update (i.e. an extra
136/// instruction beyond the guard + access pair).
137bool isLFIPrePostMemAccess(unsigned Opcode);
138
139} // namespace llvm
140
141#endif // LLVM_LIB_TARGET_AARCH64_MCTARGETDESC_AARCH64MCLFIREWRITER_H
This file declares the MCLFIRewriter class, an abstract class that encapsulates the rewriting logic f...
uint64_t IntrinsicInst * II
AArch64MCLFIRewriter(MCContext &Ctx, std::unique_ptr< MCRegisterInfo > &&RI, std::unique_ptr< MCInstrInfo > &&II)
void onLabel(const MCSymbol *Symbol) override
bool rewriteInst(const MCInst &Inst, MCStreamer &Out, const MCSubtargetInfo &STI) override
Context object for machine code objects.
Definition MCContext.h:83
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
MCLFIRewriter(MCContext &Ctx, std::unique_ptr< MCRegisterInfo > &&RI, std::unique_ptr< MCInstrInfo > &&II)
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:40
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
Streaming machine code generation interface.
Definition MCStreamer.h:222
Generic base class for all target subtargets.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Target - Wrapper for Target specific information.
This is an optimization pass for GlobalISel generic memory operations.
bool isLFIPrePostMemAccess(unsigned Opcode)
Returns true if Opcode is a pre- or post-indexed memory access that the LFI rewriter expands with a b...
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:860