Skip to content

Commit

Permalink
[hwasan] Add test case for null pointer dereference (#122186)
Browse files Browse the repository at this point in the history
This shows that HWASan will emit a memaccess intrinsic for null pointer
dereferences, with or without a fixed shadow.

This is a simplification of an internal bug report by dvyukov.
  • Loading branch information
thurstond authored Jan 8, 2025
1 parent 5b76a2e commit b48b99f
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
65 changes: 65 additions & 0 deletions llvm/test/CodeGen/AArch64/hwasan-zero-ptr.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
; RUN: llc -filetype asm -o - %s | FileCheck %s

; This shows that when dereferencing a null pointer, HWASan will call
; __hwasan_check_x4294967071_19_fixed_0_short_v2
; (N.B. 4294967071 == 2**32 - 239 + 14 == 2**32 - X0 + XZR
;
; The source was generated from llvm/test/Instrumentation/HWAddressSanitizer/zero-ptr.ll.

; ModuleID = '<stdin>'
source_filename = "<stdin>"
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
target triple = "aarch64--linux-android10000"

$hwasan.module_ctor = comdat any

@__start_hwasan_globals = external hidden constant [0 x i8]
@__stop_hwasan_globals = external hidden constant [0 x i8]
@hwasan.note = private constant { i32, i32, i32, [8 x i8], i32, i32 } { i32 8, i32 8, i32 3, [8 x i8] c"LLVM\00\00\00\00", i32 trunc (i64 sub (i64 ptrtoint (ptr @__start_hwasan_globals to i64), i64 ptrtoint (ptr @hwasan.note to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr @__stop_hwasan_globals to i64), i64 ptrtoint (ptr @hwasan.note to i64)) to i32) }, section ".note.hwasan.globals", comdat($hwasan.module_ctor), align 4

; Function Attrs: sanitize_hwaddress
define void @test_store_to_zeroptr() #0 {
; CHECK-LABEL: test_store_to_zeroptr:
; CHECK: // %bb.0: // %entry
; CHECK-NEXT: str x30, [sp, #-16]! // 8-byte Folded Spill
; CHECK-NEXT: .cfi_def_cfa_offset 16
; CHECK-NEXT: .cfi_offset w30, -16
; CHECK-NEXT: bl __hwasan_check_x4294967071_19_fixed_0_short_v2
; CHECK-NEXT: mov x8, xzr
; CHECK-NEXT: mov w9, #42 // =0x2a
; CHECK-NEXT: str x9, [x8]
; CHECK-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload
; CHECK-NEXT: ret
entry:
%.hwasan.shadow = call ptr asm "", "=r,0"(ptr null)
%b = inttoptr i64 0 to ptr
call void @llvm.hwasan.check.memaccess.shortgranules.fixedshadow(ptr %b, i32 19, i64 0)
store i64 42, ptr %b, align 8
ret void
}

; Function Attrs: nounwind
declare void @llvm.hwasan.check.memaccess.shortgranules.fixedshadow(ptr, i32 immarg, i64 immarg) #1

attributes #0 = { sanitize_hwaddress }
attributes #1 = { nounwind }

declare void @__hwasan_init()

; Function Attrs: nounwind
define internal void @hwasan.module_ctor() #1 comdat {
; CHECK-LABEL: hwasan.module_ctor:
; CHECK: // %bb.0:
; CHECK-NEXT: str x30, [sp, #-16]! // 8-byte Folded Spill
; CHECK-NEXT: bl __hwasan_init
; CHECK-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload
; CHECK-NEXT: ret
call void @__hwasan_init()
ret void
}

!llvm.module.flags = !{!1}

!0 = !{ptr @hwasan.note}
!1 = !{i32 4, !"nosanitize_hwaddress", i32 1}
35 changes: 35 additions & 0 deletions llvm/test/Instrumentation/HWAddressSanitizer/zero-ptr.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
; RUN: opt < %s -passes=hwasan -S | FileCheck %s
; RUN: opt < %s -passes=hwasan -hwasan-recover=0 -hwasan-mapping-offset=0 -S | FileCheck %s --check-prefixes=ABORT-ZERO-BASED-SHADOW

; This shows that HWASan will emit a memaccess check when dereferencing a null
; pointer.
; The output is used as the source for llvm/test/CodeGen/AArch64/hwasan-zero-ptr.ll.

target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
target triple = "aarch64--linux-android10000"

define void @test_store_to_zeroptr() sanitize_hwaddress {
; CHECK-LABEL: define void @test_store_to_zeroptr
; CHECK-SAME: () #[[ATTR0:[0-9]+]] {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[DOTHWASAN_SHADOW:%.*]] = call ptr asm "", "=r,0"(ptr @__hwasan_shadow)
; CHECK-NEXT: [[B:%.*]] = inttoptr i64 0 to ptr
; CHECK-NEXT: call void @llvm.hwasan.check.memaccess.shortgranules(ptr [[DOTHWASAN_SHADOW]], ptr [[B]], i32 19)
; CHECK-NEXT: store i64 42, ptr [[B]], align 8
; CHECK-NEXT: ret void
;
; ABORT-ZERO-BASED-SHADOW-LABEL: define void @test_store_to_zeroptr
; ABORT-ZERO-BASED-SHADOW-SAME: () #[[ATTR0:[0-9]+]] {
; ABORT-ZERO-BASED-SHADOW-NEXT: entry:
; ABORT-ZERO-BASED-SHADOW-NEXT: [[DOTHWASAN_SHADOW:%.*]] = call ptr asm "", "=r,0"(ptr null)
; ABORT-ZERO-BASED-SHADOW-NEXT: [[B:%.*]] = inttoptr i64 0 to ptr
; ABORT-ZERO-BASED-SHADOW-NEXT: call void @llvm.hwasan.check.memaccess.shortgranules.fixedshadow(ptr [[B]], i32 19, i64 0)
; ABORT-ZERO-BASED-SHADOW-NEXT: store i64 42, ptr [[B]], align 8
; ABORT-ZERO-BASED-SHADOW-NEXT: ret void
;
entry:
%b = inttoptr i64 0 to i64*
store i64 42, ptr %b
ret void
}

0 comments on commit b48b99f

Please sign in to comment.