-
Notifications
You must be signed in to change notification settings - Fork 14.3k
CodeGen: Optionally emit PAuth relocations as IRELATIVE relocations. #133533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pcc
wants to merge
3
commits into
users/pcc/spr/main.codegen-optionally-emit-pauth-relocations-as-irelative-relocations
Choose a base branch
from
users/pcc/spr/codegen-optionally-emit-pauth-relocations-as-irelative-relocations
base: users/pcc/spr/main.codegen-optionally-emit-pauth-relocations-as-irelative-relocations
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Created using spr 1.3.6-beta.1
@llvm/pr-subscribers-backend-aarch64 Author: Peter Collingbourne (pcc) ChangesThis supports the following use cases:
For more information see the RFC: TODO:
Full diff: https://github.com/llvm/llvm-project/pull/133533.diff 1 Files Affected:
diff --git a/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp b/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
index 8f26e9b791dff..cbff94f4dc227 100644
--- a/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
+++ b/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
@@ -54,6 +54,7 @@
#include "llvm/MC/MCSectionMachO.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSymbol.h"
+#include "llvm/MC/MCValue.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
@@ -84,6 +85,7 @@ class AArch64AsmPrinter : public AsmPrinter {
bool EnableImportCallOptimization = false;
DenseMap<MCSection *, std::vector<std::pair<MCSymbol *, MCSymbol *>>>
SectionToImportedFunctionCalls;
+ unsigned PAuthIFuncNextUniqueID = 1;
public:
AArch64AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
@@ -191,6 +193,10 @@ class AArch64AsmPrinter : public AsmPrinter {
// authenticating)
void LowerLOADgotAUTH(const MachineInstr &MI);
+ const MCExpr *emitPAuthRelocationAsIRelative(
+ const MCExpr *Target, uint16_t Disc, AArch64PACKey::ID KeyID,
+ bool HasAddressDiversity, bool IsDSOLocal);
+
/// tblgen'erated driver function for lowering simple MI->MC
/// pseudo instructions.
bool lowerPseudoInstExpansion(const MachineInstr *MI, MCInst &Inst);
@@ -2218,6 +2224,145 @@ void AArch64AsmPrinter::emitPtrauthBranch(const MachineInstr *MI) {
EmitToStreamer(*OutStreamer, BRInst);
}
+static void emitAddress(MCStreamer &Streamer, MCRegister Reg,
+ const MCExpr *Expr, bool DSOLocal,
+ const MCSubtargetInfo &STI) {
+ MCValue Val;
+ if (!Expr->evaluateAsRelocatable(Val, nullptr))
+ report_fatal_error("emitAddress could not evaluate");
+ if (DSOLocal) {
+ Streamer.emitInstruction(
+ MCInstBuilder(AArch64::ADRP)
+ .addReg(Reg)
+ .addExpr(AArch64MCExpr::create(Expr, AArch64MCExpr::VK_ABS_PAGE,
+ Streamer.getContext())),
+ STI);
+ Streamer.emitInstruction(
+ MCInstBuilder(AArch64::ADDXri)
+ .addReg(Reg)
+ .addReg(Reg)
+ .addExpr(AArch64MCExpr::create(Expr, AArch64MCExpr::VK_LO12,
+ Streamer.getContext()))
+ .addImm(0),
+ STI);
+ } else {
+ Streamer.emitInstruction(MCInstBuilder(AArch64::ADRP)
+ .addReg(Reg)
+ .addExpr(AArch64MCExpr::create(
+ Val.getSymA(), AArch64MCExpr::VK_GOT_PAGE,
+ Streamer.getContext())),
+ STI);
+ Streamer.emitInstruction(MCInstBuilder(AArch64::LDRXui)
+ .addReg(Reg)
+ .addReg(Reg)
+ .addExpr(AArch64MCExpr::create(
+ Val.getSymA(), AArch64MCExpr::VK_GOT_LO12,
+ Streamer.getContext())),
+ STI);
+ if (Val.getConstant())
+ Streamer.emitInstruction(MCInstBuilder(AArch64::ADDXri)
+ .addReg(Reg)
+ .addReg(Reg)
+ .addImm(Val.getConstant())
+ .addImm(0),
+ STI);
+ }
+}
+
+static bool targetSupportsPAuthRelocation(const Triple &TT,
+ const MCExpr *Target) {
+ // No released version of glibc supports PAuth relocations.
+ if (TT.isOSGlibc())
+ return false;
+
+ // We emit PAuth constants as IRELATIVE relocations in cases where the
+ // constant cannot be represented as a PAuth relocation:
+ // 1) The signed value is not a symbol.
+ return !isa<MCConstantExpr>(Target);
+}
+
+static bool targetSupportsIRelativeRelocation(const Triple &TT) {
+ // IFUNCs are ELF-only.
+ if (!TT.isOSBinFormatELF())
+ return false;
+
+ // musl doesn't support IFUNCs.
+ if (TT.isMusl())
+ return false;
+
+ return true;
+}
+
+const MCExpr *AArch64AsmPrinter::emitPAuthRelocationAsIRelative(
+ const MCExpr *Target, uint16_t Disc, AArch64PACKey::ID KeyID,
+ bool HasAddressDiversity, bool IsDSOLocal) {
+ const Triple &TT = TM.getTargetTriple();
+
+ // We only emit an IRELATIVE relocation if the target supports IRELATIVE and
+ // does not support the kind of PAuth relocation that we are trying to emit.
+ if (targetSupportsPAuthRelocation(TT, Target, DSExpr) ||
+ !targetSupportsIRelativeRelocation(TT))
+ return nullptr;
+
+ // For now, only the DA key is supported.
+ if (KeyID != AArch64PACKey::DA)
+ return nullptr;
+
+ std::unique_ptr<MCSubtargetInfo> STI(
+ TM.getTarget().createMCSubtargetInfo(TT.str(), "", ""));
+ assert(STI && "Unable to create subtarget info");
+
+ MCSymbol *Place = OutStreamer->getContext().createTempSymbol();
+ OutStreamer->emitLabel(Place);
+ OutStreamer->pushSection();
+
+ OutStreamer->switchSection(OutStreamer->getContext().getELFSection(
+ ".text.startup", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_EXECINSTR,
+ 0, "", true, PAuthIFuncNextUniqueID++, nullptr));
+
+ MCSymbol *IFuncSym =
+ OutStreamer->getContext().createLinkerPrivateSymbol("pauth_ifunc");
+ OutStreamer->emitSymbolAttribute(IFuncSym, MCSA_ELF_TypeIndFunction);
+ OutStreamer->emitLabel(IFuncSym);
+ if (isa<MCConstantExpr>(Target)) {
+ OutStreamer->emitInstruction(MCInstBuilder(AArch64::MOVZXi)
+ .addReg(AArch64::X0)
+ .addExpr(Target)
+ .addImm(0),
+ *STI);
+ } else {
+ emitAddress(*OutStreamer, AArch64::X0, Target, IsDSOLocal, *STI);
+ }
+ if (HasAddressDiversity) {
+ auto *PlacePlusDisc = MCBinaryExpr::createAdd(
+ MCSymbolRefExpr::create(Place, OutStreamer->getContext()),
+ MCConstantExpr::create(static_cast<int16_t>(Disc),
+ OutStreamer->getContext()),
+ OutStreamer->getContext());
+ emitAddress(*OutStreamer, AArch64::X1, PlacePlusDisc, /*IsDSOLocal=*/true,
+ *STI);
+ } else {
+ emitMOVZ(AArch64::X1, Disc, 0);
+ }
+
+ MCSymbol *PrePACInst = OutStreamer->getContext().createTempSymbol();
+ OutStreamer->emitLabel(PrePACInst);
+
+ // We don't know the subtarget because this is being emitted for a global
+ // initializer. Because the performance of IFUNC resolvers is unimportant, we
+ // always call the EmuPAC runtime, which will end up using the PAC instruction
+ // if the target supports PAC.
+ MCSymbol *EmuPAC =
+ OutStreamer->getContext().getOrCreateSymbol("__emupac_pacda");
+ const MCSymbolRefExpr *EmuPACRef =
+ MCSymbolRefExpr::create(EmuPAC, OutStreamer->getContext());
+ OutStreamer->emitInstruction(MCInstBuilder(AArch64::B).addExpr(EmuPACRef),
+ *STI);
+ OutStreamer->popSection();
+
+ return MCSymbolRefExpr::create(IFuncSym, OutStreamer->getContext());
+}
+
const MCExpr *
AArch64AsmPrinter::lowerConstantPtrAuth(const ConstantPtrAuth &CPA) {
MCContext &Ctx = OutContext;
@@ -2229,23 +2374,20 @@ AArch64AsmPrinter::lowerConstantPtrAuth(const ConstantPtrAuth &CPA) {
auto *BaseGVB = dyn_cast<GlobalValue>(BaseGV);
- // If we can't understand the referenced ConstantExpr, there's nothing
- // else we can do: emit an error.
- if (!BaseGVB) {
- BaseGV->getContext().emitError(
- "cannot resolve target base/addend of ptrauth constant");
- return nullptr;
+ const MCExpr *Sym;
+ if (BaseGVB) {
+ // If there is an addend, turn that into the appropriate MCExpr.
+ Sym = MCSymbolRefExpr::create(getSymbol(BaseGVB), Ctx);
+ if (Offset.sgt(0))
+ Sym = MCBinaryExpr::createAdd(
+ Sym, MCConstantExpr::create(Offset.getSExtValue(), Ctx), Ctx);
+ else if (Offset.slt(0))
+ Sym = MCBinaryExpr::createSub(
+ Sym, MCConstantExpr::create((-Offset).getSExtValue(), Ctx), Ctx);
+ } else {
+ Sym = MCConstantExpr::create(Offset.getSExtValue(), Ctx);
}
- // If there is an addend, turn that into the appropriate MCExpr.
- const MCExpr *Sym = MCSymbolRefExpr::create(getSymbol(BaseGVB), Ctx);
- if (Offset.sgt(0))
- Sym = MCBinaryExpr::createAdd(
- Sym, MCConstantExpr::create(Offset.getSExtValue(), Ctx), Ctx);
- else if (Offset.slt(0))
- Sym = MCBinaryExpr::createSub(
- Sym, MCConstantExpr::create((-Offset).getSExtValue(), Ctx), Ctx);
-
uint64_t KeyID = CPA.getKey()->getZExtValue();
// We later rely on valid KeyID value in AArch64PACKeyIDToString call from
// AArch64AuthMCExpr::printImpl, so fail fast.
@@ -2259,6 +2401,12 @@ AArch64AsmPrinter::lowerConstantPtrAuth(const ConstantPtrAuth &CPA) {
report_fatal_error("AArch64 PAC Discriminator '" + Twine(Disc) +
"' out of range [0, 0xFFFF]");
+ // Check if we need to represent this with an IRELATIVE and emit it if so.
+ if (auto *IFuncSym = emitPAuthRelocationAsIRelative(
+ Sym, Disc, AArch64PACKey::ID(KeyID), CPA.hasAddressDiscriminator(),
+ BaseGVB && BaseGVB->isDSOLocal()))
+ return IFuncSym;
+
// Finally build the complete @AUTH expr.
return AArch64AuthMCExpr::create(Sym, Disc, AArch64PACKey::ID(KeyID),
CPA.hasAddressDiscriminator(), Ctx);
|
You can test this locally with the following command:git-clang-format --diff HEAD~1 HEAD --extensions cpp -- llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp View the diff from clang-format here.diff --git a/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp b/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
index 079036388..e1188d1e0 100644
--- a/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
+++ b/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
@@ -211,9 +211,11 @@ public:
// authenticating)
void LowerLOADgotAUTH(const MachineInstr &MI);
- const MCExpr *emitPAuthRelocationAsIRelative(
- const MCExpr *Target, uint16_t Disc, AArch64PACKey::ID KeyID,
- bool HasAddressDiversity, bool IsDSOLocal);
+ const MCExpr *emitPAuthRelocationAsIRelative(const MCExpr *Target,
+ uint16_t Disc,
+ AArch64PACKey::ID KeyID,
+ bool HasAddressDiversity,
+ bool IsDSOLocal);
/// tblgen'erated driver function for lowering simple MI->MC
/// pseudo instructions.
|
pcc
added a commit
to pcc/llvm-project
that referenced
this pull request
Apr 3, 2025
This supports the following use cases: - ConstantPtrAuth expressions that are unrepresentable using standard PAuth relocations such as expressions involving an integer operand or deactivation symbols. - libc implementations that do not support PAuth relocations. For more information see the RFC: https://discourse.llvm.org/t/rfc-structure-protection-a-family-of-uaf-mitigation-techniques/85555 TODO: - Add tests. Pull Request: llvm#133533
pcc
added a commit
to pcc/llvm-project
that referenced
this pull request
Apr 4, 2025
This supports the following use cases: - ConstantPtrAuth expressions that are unrepresentable using standard PAuth relocations such as expressions involving an integer operand or deactivation symbols. - libc implementations that do not support PAuth relocations. For more information see the RFC: https://discourse.llvm.org/t/rfc-structure-protection-a-family-of-uaf-mitigation-techniques/85555 TODO: - Add tests. Pull Request: llvm#133533
Created using spr 1.3.6-beta.1
pcc
added a commit
to pcc/llvm-project
that referenced
this pull request
May 24, 2025
This supports the following use cases: - ConstantPtrAuth expressions that are unrepresentable using standard PAuth relocations such as expressions involving an integer operand or deactivation symbols. - libc implementations that do not support PAuth relocations. For more information see the RFC: https://discourse.llvm.org/t/rfc-structure-protection-a-family-of-uaf-mitigation-techniques/85555 TODO: - Add tests. Pull Request: llvm#133533
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This supports the following use cases:
relocations such as expressions involving an integer operand or
deactivation symbols.
For more information see the RFC:
https://discourse.llvm.org/t/rfc-structure-protection-a-family-of-uaf-mitigation-techniques/85555
TODO: