-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[hwasan] Add intrinsics for fixed shadow on Aarch64 #89319
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
Changes from all commits
ef41af2
69cde77
3930a91
49cc1e9
b682275
74140ef
9a04e51
557a636
6510242
9e1cb84
bd20766
272edc2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,7 +116,8 @@ class AArch64AsmPrinter : public AsmPrinter { | |
void LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI); | ||
void LowerPATCHABLE_EVENT_CALL(const MachineInstr &MI, bool Typed); | ||
|
||
typedef std::tuple<unsigned, bool, uint32_t> HwasanMemaccessTuple; | ||
typedef std::tuple<unsigned, bool, uint32_t, bool, uint64_t> | ||
HwasanMemaccessTuple; | ||
std::map<HwasanMemaccessTuple, MCSymbol *> HwasanMemaccessSymbols; | ||
void LowerKCFI_CHECK(const MachineInstr &MI); | ||
void LowerHWASAN_CHECK_MEMACCESS(const MachineInstr &MI); | ||
|
@@ -551,17 +552,27 @@ void AArch64AsmPrinter::LowerKCFI_CHECK(const MachineInstr &MI) { | |
void AArch64AsmPrinter::LowerHWASAN_CHECK_MEMACCESS(const MachineInstr &MI) { | ||
Register Reg = MI.getOperand(0).getReg(); | ||
bool IsShort = | ||
MI.getOpcode() == AArch64::HWASAN_CHECK_MEMACCESS_SHORTGRANULES; | ||
((MI.getOpcode() == AArch64::HWASAN_CHECK_MEMACCESS_SHORTGRANULES) || | ||
(MI.getOpcode() == | ||
AArch64::HWASAN_CHECK_MEMACCESS_SHORTGRANULES_FIXEDSHADOW)); | ||
uint32_t AccessInfo = MI.getOperand(1).getImm(); | ||
MCSymbol *&Sym = | ||
HwasanMemaccessSymbols[HwasanMemaccessTuple(Reg, IsShort, AccessInfo)]; | ||
bool IsFixedShadow = | ||
((MI.getOpcode() == AArch64::HWASAN_CHECK_MEMACCESS_FIXEDSHADOW) || | ||
(MI.getOpcode() == | ||
AArch64::HWASAN_CHECK_MEMACCESS_SHORTGRANULES_FIXEDSHADOW)); | ||
uint64_t FixedShadowOffset = IsFixedShadow ? MI.getOperand(2).getImm() : 0; | ||
|
||
MCSymbol *&Sym = HwasanMemaccessSymbols[HwasanMemaccessTuple( | ||
Reg, IsShort, AccessInfo, IsFixedShadow, FixedShadowOffset)]; | ||
if (!Sym) { | ||
// FIXME: Make this work on non-ELF. | ||
if (!TM.getTargetTriple().isOSBinFormatELF()) | ||
report_fatal_error("llvm.hwasan.check.memaccess only supported on ELF"); | ||
|
||
std::string SymName = "__hwasan_check_x" + utostr(Reg - AArch64::X0) + "_" + | ||
utostr(AccessInfo); | ||
if (IsFixedShadow) | ||
SymName += "_fixed_" + utostr(FixedShadowOffset); | ||
if (IsShort) | ||
SymName += "_short_v2"; | ||
Sym = OutContext.getOrCreateSymbol(SymName); | ||
|
@@ -596,6 +607,8 @@ void AArch64AsmPrinter::emitHwasanMemaccessSymbols(Module &M) { | |
unsigned Reg = std::get<0>(P.first); | ||
bool IsShort = std::get<1>(P.first); | ||
uint32_t AccessInfo = std::get<2>(P.first); | ||
bool IsFixedShadow = std::get<3>(P.first); | ||
uint64_t FixedShadowOffset = std::get<4>(P.first); | ||
const MCSymbolRefExpr *HwasanTagMismatchRef = | ||
IsShort ? HwasanTagMismatchV2Ref : HwasanTagMismatchV1Ref; | ||
MCSymbol *Sym = P.second; | ||
|
@@ -625,14 +638,35 @@ void AArch64AsmPrinter::emitHwasanMemaccessSymbols(Module &M) { | |
.addImm(4) | ||
.addImm(55), | ||
*STI); | ||
OutStreamer->emitInstruction( | ||
MCInstBuilder(AArch64::LDRBBroX) | ||
.addReg(AArch64::W16) | ||
.addReg(IsShort ? AArch64::X20 : AArch64::X9) | ||
.addReg(AArch64::X16) | ||
.addImm(0) | ||
.addImm(0), | ||
*STI); | ||
|
||
if (IsFixedShadow) { | ||
// Aarch64 makes it difficult to embed large constants in the code. | ||
// Fortuitously, kShadowBaseAlignment == 32, so we use the 32-bit | ||
// left-shift option in the MOV instruction. Combined with the 16-bit | ||
// immediate, this is enough to represent any offset up to 2**48. | ||
OutStreamer->emitInstruction(MCInstBuilder(AArch64::MOVZXi) | ||
fmayer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.addReg(AArch64::X17) | ||
.addImm(FixedShadowOffset >> 32) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we probably want a diagnostics or change intrinsic to 16imm for real There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or add |
||
.addImm(32), | ||
*STI); | ||
OutStreamer->emitInstruction(MCInstBuilder(AArch64::LDRBBroX) | ||
vitalybuka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.addReg(AArch64::W16) | ||
.addReg(AArch64::X17) | ||
.addReg(AArch64::X16) | ||
.addImm(0) | ||
.addImm(0), | ||
*STI); | ||
} else { | ||
OutStreamer->emitInstruction( | ||
MCInstBuilder(AArch64::LDRBBroX) | ||
.addReg(AArch64::W16) | ||
.addReg(IsShort ? AArch64::X20 : AArch64::X9) | ||
.addReg(AArch64::X16) | ||
.addImm(0) | ||
.addImm(0), | ||
*STI); | ||
} | ||
|
||
OutStreamer->emitInstruction( | ||
MCInstBuilder(AArch64::SUBSXrs) | ||
.addReg(AArch64::XZR) | ||
|
@@ -1765,6 +1799,8 @@ void AArch64AsmPrinter::emitInstruction(const MachineInstr *MI) { | |
|
||
case AArch64::HWASAN_CHECK_MEMACCESS: | ||
case AArch64::HWASAN_CHECK_MEMACCESS_SHORTGRANULES: | ||
case AArch64::HWASAN_CHECK_MEMACCESS_FIXEDSHADOW: | ||
case AArch64::HWASAN_CHECK_MEMACCESS_SHORTGRANULES_FIXEDSHADOW: | ||
LowerHWASAN_CHECK_MEMACCESS(*MI); | ||
return; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
16-bit immediate looks misleading?
It's
i64imm:$fixed_shadow