xref: /llvm-project/compiler-rt/test/asan/TestCases/Posix/high-address-dereference.c (revision 81cd7358a2ce411c99512d055e98d0b9dc119c14)
1 // On x86_64, the kernel does not provide the faulting address for dereferences
2 // of addresses greater than the 48-bit hardware addressable range, i.e.,
3 // `siginfo.si_addr` is zero in ASan's SEGV signal handler. This test checks
4 // that ASan does not misrepresent such cases as "NULL dereferences".
5 
6 // REQUIRES: x86_64-target-arch
7 // RUN: %clang_asan %s -o %t
8 // RUN: %env_asan_opts=print_scariness=1 not %run %t 0x0000000000000000 2>&1 | FileCheck %s --check-prefixes=ZERO,HINT-PAGE0
9 // RUN: %env_asan_opts=print_scariness=1 not %run %t 0x0000000000000FFF 2>&1 | FileCheck %s --check-prefixes=LOW1,HINT-PAGE0
10 // RUN: %env_asan_opts=print_scariness=1 not %run %t 0x0000000000001000 2>&1 | FileCheck %s --check-prefixes=LOW2,HINT-NONE
11 // RUN: %env_asan_opts=print_scariness=1 not %run %t 0x4141414141414141 2>&1 | FileCheck %s --check-prefixes=HIGH,HINT-HIGHADDR
12 // RUN: %env_asan_opts=print_scariness=1 not %run %t 0xFFFFFFFFFFFFFFFF 2>&1 | FileCheck %s --check-prefixes=MAX,HINT-HIGHADDR
13 
14 #include <stdint.h>
15 #include <stdlib.h>
16 
17 int main(int argc, const char *argv[]) {
18   const char *hex = argv[1];
19   uint64_t *addr = (uint64_t *)strtoull(hex, NULL, 16);
20   uint64_t x = *addr;  // segmentation fault
21   return x;
22 }
23 
24 // ZERO:  SEGV on unknown address 0x000000000000 (pc
25 // LOW1:  SEGV on unknown address 0x000000000fff (pc
26 // LOW2:  SEGV on unknown address 0x000000001000 (pc
27 // HIGH:  {{BUS|SEGV}} on unknown address (pc
28 // MAX:   {{BUS|SEGV}} on unknown address (pc
29 
30 // HINT-PAGE0-NOT: Hint: this fault was caused by a dereference of a high value address
31 // HINT-PAGE0:     Hint: address points to the zero page.
32 
33 // HINT-NONE-NOT:  Hint: this fault was caused by a dereference of a high value address
34 // HINT-NONE-NOT:  Hint: address points to the zero page.
35 
36 // HINT-HIGHADDR:     Hint: this fault was caused by a dereference of a high value address
37 // HINT-HIGHADDR-NOT: Hint: address points to the zero page.
38 
39 // ZERO:  SCARINESS: 10 (null-deref)
40 // LOW1:  SCARINESS: 10 (null-deref)
41 // LOW2:  SCARINESS: 20 (wild-addr-read)
42 // HIGH:  SCARINESS: {{(20 \(wild-addr-read\))|(60 \(wild-jump\))}}
43 // MAX:   SCARINESS: {{(20 \(wild-addr-read\))|(60 \(wild-jump\))}}
44 
45 // TODO: Currently, register values are only printed on Mac.  Once this changes,
46 //       remove the 'TODO_' prefix in the following lines.
47 // TODO_HIGH,TODO_MAX: Register values:
48 // TODO_HIGH: = 0x4141414141414141
49 // TODO_MAX:  = 0xffffffffffffffff
50