Skip to content

Commit b48b99f

Browse files
authored
[hwasan] Add test case for null pointer dereference (#122186)
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.
1 parent 5b76a2e commit b48b99f

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc -filetype asm -o - %s | FileCheck %s
3+
4+
; This shows that when dereferencing a null pointer, HWASan will call
5+
; __hwasan_check_x4294967071_19_fixed_0_short_v2
6+
; (N.B. 4294967071 == 2**32 - 239 + 14 == 2**32 - X0 + XZR
7+
;
8+
; The source was generated from llvm/test/Instrumentation/HWAddressSanitizer/zero-ptr.ll.
9+
10+
; ModuleID = '<stdin>'
11+
source_filename = "<stdin>"
12+
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
13+
target triple = "aarch64--linux-android10000"
14+
15+
$hwasan.module_ctor = comdat any
16+
17+
@__start_hwasan_globals = external hidden constant [0 x i8]
18+
@__stop_hwasan_globals = external hidden constant [0 x i8]
19+
@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
20+
21+
; Function Attrs: sanitize_hwaddress
22+
define void @test_store_to_zeroptr() #0 {
23+
; CHECK-LABEL: test_store_to_zeroptr:
24+
; CHECK: // %bb.0: // %entry
25+
; CHECK-NEXT: str x30, [sp, #-16]! // 8-byte Folded Spill
26+
; CHECK-NEXT: .cfi_def_cfa_offset 16
27+
; CHECK-NEXT: .cfi_offset w30, -16
28+
; CHECK-NEXT: bl __hwasan_check_x4294967071_19_fixed_0_short_v2
29+
; CHECK-NEXT: mov x8, xzr
30+
; CHECK-NEXT: mov w9, #42 // =0x2a
31+
; CHECK-NEXT: str x9, [x8]
32+
; CHECK-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload
33+
; CHECK-NEXT: ret
34+
entry:
35+
%.hwasan.shadow = call ptr asm "", "=r,0"(ptr null)
36+
%b = inttoptr i64 0 to ptr
37+
call void @llvm.hwasan.check.memaccess.shortgranules.fixedshadow(ptr %b, i32 19, i64 0)
38+
store i64 42, ptr %b, align 8
39+
ret void
40+
}
41+
42+
; Function Attrs: nounwind
43+
declare void @llvm.hwasan.check.memaccess.shortgranules.fixedshadow(ptr, i32 immarg, i64 immarg) #1
44+
45+
attributes #0 = { sanitize_hwaddress }
46+
attributes #1 = { nounwind }
47+
48+
declare void @__hwasan_init()
49+
50+
; Function Attrs: nounwind
51+
define internal void @hwasan.module_ctor() #1 comdat {
52+
; CHECK-LABEL: hwasan.module_ctor:
53+
; CHECK: // %bb.0:
54+
; CHECK-NEXT: str x30, [sp, #-16]! // 8-byte Folded Spill
55+
; CHECK-NEXT: bl __hwasan_init
56+
; CHECK-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload
57+
; CHECK-NEXT: ret
58+
call void @__hwasan_init()
59+
ret void
60+
}
61+
62+
!llvm.module.flags = !{!1}
63+
64+
!0 = !{ptr @hwasan.note}
65+
!1 = !{i32 4, !"nosanitize_hwaddress", i32 1}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
2+
; RUN: opt < %s -passes=hwasan -S | FileCheck %s
3+
; RUN: opt < %s -passes=hwasan -hwasan-recover=0 -hwasan-mapping-offset=0 -S | FileCheck %s --check-prefixes=ABORT-ZERO-BASED-SHADOW
4+
5+
; This shows that HWASan will emit a memaccess check when dereferencing a null
6+
; pointer.
7+
; The output is used as the source for llvm/test/CodeGen/AArch64/hwasan-zero-ptr.ll.
8+
9+
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
10+
target triple = "aarch64--linux-android10000"
11+
12+
define void @test_store_to_zeroptr() sanitize_hwaddress {
13+
; CHECK-LABEL: define void @test_store_to_zeroptr
14+
; CHECK-SAME: () #[[ATTR0:[0-9]+]] {
15+
; CHECK-NEXT: entry:
16+
; CHECK-NEXT: [[DOTHWASAN_SHADOW:%.*]] = call ptr asm "", "=r,0"(ptr @__hwasan_shadow)
17+
; CHECK-NEXT: [[B:%.*]] = inttoptr i64 0 to ptr
18+
; CHECK-NEXT: call void @llvm.hwasan.check.memaccess.shortgranules(ptr [[DOTHWASAN_SHADOW]], ptr [[B]], i32 19)
19+
; CHECK-NEXT: store i64 42, ptr [[B]], align 8
20+
; CHECK-NEXT: ret void
21+
;
22+
; ABORT-ZERO-BASED-SHADOW-LABEL: define void @test_store_to_zeroptr
23+
; ABORT-ZERO-BASED-SHADOW-SAME: () #[[ATTR0:[0-9]+]] {
24+
; ABORT-ZERO-BASED-SHADOW-NEXT: entry:
25+
; ABORT-ZERO-BASED-SHADOW-NEXT: [[DOTHWASAN_SHADOW:%.*]] = call ptr asm "", "=r,0"(ptr null)
26+
; ABORT-ZERO-BASED-SHADOW-NEXT: [[B:%.*]] = inttoptr i64 0 to ptr
27+
; ABORT-ZERO-BASED-SHADOW-NEXT: call void @llvm.hwasan.check.memaccess.shortgranules.fixedshadow(ptr [[B]], i32 19, i64 0)
28+
; ABORT-ZERO-BASED-SHADOW-NEXT: store i64 42, ptr [[B]], align 8
29+
; ABORT-ZERO-BASED-SHADOW-NEXT: ret void
30+
;
31+
entry:
32+
%b = inttoptr i64 0 to i64*
33+
store i64 42, ptr %b
34+
ret void
35+
}

0 commit comments

Comments
 (0)