1// RUN: %clangxx -fsanitize=null -fno-sanitize-recover=null %s -O3 -o %t
2// RUN: not %run %t l 2>&1 | FileCheck %s --check-prefix=CHECK-LOAD
3// RUN: not %run %t s 2>&1 | FileCheck %s --check-prefix=CHECK-STORE
4// RUN: not %run %t r 2>&1 | FileCheck %s --check-prefix=CHECK-REFERENCE
5// RUN: not %run %t m 2>&1 | FileCheck %s --check-prefix=CHECK-MEMBER
6// RUN: not %run %t f 2>&1 | FileCheck %s --check-prefix=CHECK-MEMFUN
7// RUN: not %run %t t 2>&1 | FileCheck %s --check-prefix=CHECK-VCALL
8// RUN: not %run %t u 2>&1 | FileCheck %s --check-prefix=CHECK-VCALL2
9
10struct S {
11 int f() { return 0; }
12 int k;
13};
14
15struct T {
16 virtual int v() { return 1; }
17};
18
19struct U : T {
20 virtual int v() { return 2; }
21};
22
23int main(int, char **argv) {
24 int *p = 0;
25 S *s = 0;
26 T *t = 0;
27 U *u = 0;
28
29 (void)*p; // ok!
30 (void)*t; // ok!
31 (void)*u; // ok!
32
33 switch (argv[1][0]) {
34 case 'l':
35 // CHECK-LOAD: null.cpp:[[@LINE+1]]:12: runtime error: load of null pointer of type 'int'
36 return *p;
37 case 's':
38 // CHECK-STORE: null.cpp:[[@LINE+1]]:5: runtime error: store to null pointer of type 'int'
39 *p = 1;
40 break;
41 case 'r':
42 // CHECK-REFERENCE: null.cpp:[[@LINE+1]]:15: runtime error: reference binding to null pointer of type 'int'
43 {int &r = *p;}
44 break;
45 case 'm':
46 // CHECK-MEMBER: null.cpp:[[@LINE+1]]:15: runtime error: member access within null pointer of type 'S'
47 return s->k;
48 case 'f':
49 // CHECK-MEMFUN: null.cpp:[[@LINE+1]]:15: runtime error: member call on null pointer of type 'S'
50 return s->f();
51 case 't':
52 // CHECK-VCALL: null.cpp:[[@LINE+1]]:15: runtime error: member call on null pointer of type 'T'
53 return t->v();
54 case 'u':
55 // CHECK-VCALL2: null.cpp:[[@LINE+1]]:15: runtime error: member call on null pointer of type 'U'
56 return u->v();
57 }
58}
59

source code of compiler-rt/test/ubsan/TestCases/TypeCheck/null.cpp