LLVM: lib/Target/AArch64/AArch64PointerAuth.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

10

19

20using namespace llvm;

22

23#define AARCH64_POINTER_AUTH_NAME "AArch64 Pointer Authentication"

24

25namespace {

26

28public:

29 static char ID;

30

32

34

36

37private:

40

42

45

47};

48

49}

50

53

55 return new AArch64PointerAuth();

56}

57

58char AArch64PointerAuth::ID = 0;

59

71

72

73

74

75

81

82

83 if (PACSym) {

86 }

87

88

89

90 if (MFnI.branchProtectionPAuthLR() && !Subtarget.hasPAuthLR())

92}

93

96 if (!EmitCFI)

97 return;

98

99 auto &MF = *MBB.getParent();

101

103 MFnI.branchProtectionPAuthLR() ? CFIBuilder.buildNegateRAStateWithPC()

104 : CFIBuilder.buildNegateRAState();

105}

106

109 auto &MFnI = *MF.getInfo();

110 bool UseBKey = MFnI.shouldSignWithBKey();

111 bool EmitCFI = MFnI.needsDwarfUnwindInfo(MF);

112 bool NeedsWinCFI = MF.hasWinCFI();

113

115

116

118

119 if (UseBKey) {

122 }

123

124

125

126 if (MFnI.branchProtectionPAuthLR()) {

128 MFnI.setSigningInstrLabel(PACSym);

129 }

130

131

132

133 if (MFnI.branchProtectionPAuthLR() && Subtarget->hasPAuthLR()) {

136 TII->get(MFnI.shouldSignWithBKey() ? AArch64::PACIBSPPC

137 : AArch64::PACIASPPC))

140 } else {

142 if (MFnI.branchProtectionPAuthLR())

145 TII->get(MFnI.shouldSignWithBKey() ? AArch64::PACIBSP

146 : AArch64::PACIASP))

149 if (!MFnI.branchProtectionPAuthLR())

151 }

152

153 if (!EmitCFI && NeedsWinCFI) {

156 }

157}

158

159void AArch64PointerAuth::authenticateLR(

161 const AArch64FunctionInfo *MFnI = MF.getInfo();

164 bool NeedsWinCFI = MF.hasWinCFI();

165

168

169

170

171

172

174

175

176

177

178

179

180 bool TerminatorIsCombinable =

181 TI != MBB.end() && TI->getOpcode() == AArch64::RET;

183

184 if (Subtarget->hasPAuth() && TerminatorIsCombinable && !NeedsWinCFI &&

187 assert(PACSym && "No PAC instruction to refer to");

190 TII->get(UseBKey ? AArch64::RETABSPPCi : AArch64::RETAASPPCi))

194 } else {

196 BuildMI(MBB, TI, DL, TII->get(UseBKey ? AArch64::RETAB : AArch64::RETAA))

199 }

201 } else {

203 assert(PACSym && "No PAC instruction to refer to");

207 TII->get(UseBKey ? AArch64::AUTIBSPPCi : AArch64::AUTIASPPCi))

210 } else {

215 TII->get(UseBKey ? AArch64::AUTIBSP : AArch64::AUTIASP))

219 }

220

221 if (NeedsWinCFI) {

224 }

225 }

226}

227

229 switch (Method) {

231 return 0;

233 return 4;

235 return 12;

238 return 20;

239 }

241}

242

243bool AArch64PointerAuth::runOnMachineFunction(MachineFunction &MF) {

246

248

250

251 for (auto &MBB : MF) {

252 for (auto &MI : MBB) {

253 switch (MI.getOpcode()) {

254 default:

255 break;

256 case AArch64::PAUTH_PROLOGUE:

257 case AArch64::PAUTH_EPILOGUE:

258 PAuthPseudoInstrs.push_back(MI.getIterator());

259 break;

260 }

261 }

262 }

263

264 for (auto It : PAuthPseudoInstrs) {

265 switch (It->getOpcode()) {

266 case AArch64::PAUTH_PROLOGUE:

267 signLR(MF, It);

268 break;

269 case AArch64::PAUTH_EPILOGUE:

270 authenticateLR(MF, It);

271 break;

272 default:

274 }

275 It->eraseFromParent();

277 }

278

280}

assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")

#define AARCH64_POINTER_AUTH_NAME

Definition AArch64PointerAuth.cpp:23

static void BuildPACM(const AArch64Subtarget &Subtarget, MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, DebugLoc DL, MachineInstr::MIFlag Flags, MCSymbol *PACSym=nullptr)

Definition AArch64PointerAuth.cpp:76

static void emitPACCFI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineInstr::MIFlag Flags, bool EmitCFI)

Definition AArch64PointerAuth.cpp:94

static void emitPACSymOffsetIntoX16(const TargetInstrInfo &TII, MachineBasicBlock &MBB, MachineBasicBlock::iterator I, DebugLoc DL, MCSymbol *PACSym)

Definition AArch64PointerAuth.cpp:60

const TargetInstrInfo & TII

MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL

MachineBasicBlock MachineBasicBlock::iterator MBBI

#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)

AArch64FunctionInfo - This class is derived from MachineFunctionInfo and contains private AArch64-spe...

bool branchProtectionPAuthLR() const

bool needsAsyncDwarfUnwindInfo(const MachineFunction &MF) const

MCSymbol * getSigningInstrLabel() const

bool shouldSignWithBKey() const

const AArch64InstrInfo * getInstrInfo() const override

Helper class for creating CFI instructions and inserting them into MIR.

FunctionPass class - This class is used to implement most global optimizations.

bool hasFnAttribute(Attribute::AttrKind Kind) const

Return true if the function has the attribute.

LLVM_ABI MCSymbol * createTempSymbol()

Create a temporary symbol with a unique name.

const MCInstrDesc & get(unsigned Opcode) const

Return the machine instruction descriptor that corresponds to the specified instruction opcode.

MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...

const MachineFunction * getParent() const

Return the MachineFunction containing this basic block.

LLVM_ABI instr_iterator erase(instr_iterator I)

Remove an instruction from the instruction list and delete it.

LLVM_ABI instr_iterator getFirstInstrTerminator()

Same getFirstTerminator but it ignores bundles and return an instr_iterator instead.

MachineInstrBundleIterator< MachineInstr > iterator

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.

MCContext & getContext() const

Function & getFunction()

Return the LLVM function that this machine code represents.

Ty * getInfo()

getInfo - Keep track of various per-function pieces of information for backends that would like to do...

const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const

const MachineInstrBuilder & addImm(int64_t Val) const

Add a new immediate operand.

const MachineInstrBuilder & addSym(MCSymbol *Sym, unsigned char TargetFlags=0) const

const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const

Add a new virtual register operand.

const MachineInstrBuilder & copyImplicitOps(const MachineInstr &OtherMI) const

Copy all the implicit operands from OtherMI onto this one.

LLVM_ABI void setPreInstrSymbol(MachineFunction &MF, MCSymbol *Symbol)

Set a symbol that will be emitted just prior to the instruction itself.

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.

TargetInstrInfo - Interface to description of machine instruction set.

#define llvm_unreachable(msg)

Marks that the current location is not supposed to be reachable.

@ MO_NC

MO_NC - Indicates whether the linker is expected to check the symbol reference for overflow.

@ MO_PAGEOFF

MO_PAGEOFF - A symbol operand with this flag represents the offset of that symbol within a 4K page.

@ MO_PAGE

MO_PAGE - A symbol operand with this flag represents the pc-relative offset of the 4K page containing...

unsigned getCheckerSizeInBytes(AuthCheckMethod Method)

Returns the number of bytes added by checkAuthenticatedRegister.

Definition AArch64PointerAuth.cpp:228

AuthCheckMethod

Variants of check performed on an authenticated pointer.

@ XPACHint

Check by comparing the authenticated value with an XPAC-ed one without using PAuth instructions not e...

@ DummyLoad

Perform a load to a temporary register.

@ HighBitsNoTBI

Check by comparing bits 62 and 61 of the authenticated address.

@ None

Do not check the value at all.

@ XPAC

Similar to XPACHint but using Armv8.3-only XPAC instruction, thus not restricted to LR:

unsigned ID

LLVM IR allows to use arbitrary numbers as calling convention identifiers.

This is an optimization pass for GlobalISel generic memory operations.

MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)

Builder interface. Specify how to create the initial instruction itself.

FunctionPass * createAArch64PointerAuthPass()