Skip to content

Commit 57d2573

Browse files
committed
[hwasan] Optimize lowering of AArch64 check.memaccess for null pointer
If the pointer to be checked is statically known to be zero, the tag check will pass since: 1) the tag is zero 2) shadow memory for address 0 is initialized to 0. We therefore elide the check when lowering. This also updates the test in llvm#122186 Note: the HWASan instrumentation pass will still emit the check.memaccess intrinsic. This patch performs the elision at CodeGen.
1 parent b48b99f commit 57d2573

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,14 @@ void AArch64AsmPrinter::LowerKCFI_CHECK(const MachineInstr &MI) {
605605

606606
void AArch64AsmPrinter::LowerHWASAN_CHECK_MEMACCESS(const MachineInstr &MI) {
607607
Register Reg = MI.getOperand(0).getReg();
608+
609+
// If the pointer is statically known to be zero, it has a zero tag and the
610+
// tag check will pass since the shadow memory corresponding to address 0
611+
// is initialized to zero and never updated. We can therefore elide the tag
612+
// check.
613+
if (Reg == AArch64::XZR)
614+
return;
615+
608616
bool IsShort =
609617
((MI.getOpcode() == AArch64::HWASAN_CHECK_MEMACCESS_SHORTGRANULES) ||
610618
(MI.getOpcode() ==

llvm/test/CodeGen/AArch64/hwasan-zero-ptr.ll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ define void @test_store_to_zeroptr() #0 {
2525
; CHECK-NEXT: str x30, [sp, #-16]! // 8-byte Folded Spill
2626
; CHECK-NEXT: .cfi_def_cfa_offset 16
2727
; CHECK-NEXT: .cfi_offset w30, -16
28-
; CHECK-NEXT: bl __hwasan_check_x4294967071_19_fixed_0_short_v2
2928
; CHECK-NEXT: mov x8, xzr
3029
; CHECK-NEXT: mov w9, #42 // =0x2a
3130
; CHECK-NEXT: str x9, [x8]

0 commit comments

Comments
 (0)