1 // RUN: %clang --driver-mode=g++ -fsanitize=vptr %s -O3 -o %t 2 // RUN: %t rT && %t mT && %t fT && %t cT 3 // RUN: %t rU && %t mU && %t fU && %t cU 4 // RUN: %t rS && %t rV && %t oV 5 // RUN: %t mS 2>&1 | FileCheck %s --check-prefix=CHECK-MEMBER --strict-whitespace 6 // RUN: %t fS 2>&1 | FileCheck %s --check-prefix=CHECK-MEMFUN --strict-whitespace 7 // RUN: %t cS 2>&1 | FileCheck %s --check-prefix=CHECK-DOWNCAST --strict-whitespace 8 // RUN: %t mV 2>&1 | FileCheck %s --check-prefix=CHECK-MEMBER --strict-whitespace 9 // RUN: %t fV 2>&1 | FileCheck %s --check-prefix=CHECK-MEMFUN --strict-whitespace 10 // RUN: %t cV 2>&1 | FileCheck %s --check-prefix=CHECK-DOWNCAST --strict-whitespace 11 // RUN: %t oU 2>&1 | FileCheck %s --check-prefix=CHECK-OFFSET --strict-whitespace 12 // RUN: %t m0 2>&1 | FileCheck %s --check-prefix=CHECK-NULL-MEMBER --strict-whitespace 13 14 // FIXME: This test produces linker errors on Darwin. 15 // XFAIL: darwin 16 17 struct S { 18 S() : a(0) {} 19 ~S() {} 20 int a; 21 int f() { return 0; } 22 virtual int v() { return 0; } 23 }; 24 25 struct T : S { 26 T() : b(0) {} 27 int b; 28 int g() { return 0; } 29 virtual int v() { return 1; } 30 }; 31 32 struct U : S, T { virtual int v() { return 2; } }; 33 34 int main(int, char **argv) { 35 T t; 36 (void)t.a; 37 (void)t.b; 38 (void)t.f(); 39 (void)t.g(); 40 (void)t.v(); 41 (void)t.S::v(); 42 43 U u; 44 (void)u.T::a; 45 (void)u.b; 46 (void)u.T::f(); 47 (void)u.g(); 48 (void)u.v(); 49 (void)u.T::v(); 50 (void)((T&)u).S::v(); 51 52 T *p = 0; 53 char Buffer[sizeof(U)] = {}; 54 switch (argv[1][1]) { 55 case '0': 56 p = reinterpret_cast<T*>(Buffer); 57 break; 58 case 'S': 59 p = reinterpret_cast<T*>(new S); 60 break; 61 case 'T': 62 p = new T; 63 break; 64 case 'U': 65 p = new U; 66 break; 67 case 'V': 68 p = reinterpret_cast<T*>(new U); 69 break; 70 } 71 72 switch (argv[1][0]) { 73 case 'r': 74 // Binding a reference to storage of appropriate size and alignment is OK. 75 {T &r = *p;} 76 break; 77 78 case 'm': 79 // CHECK-MEMBER: vptr.cpp:[[@LINE+5]]:15: runtime error: member access within address [[PTR:0x[0-9a-f]*]] which does not point to an object of type 'T' 80 // CHECK-MEMBER-NEXT: [[PTR]]: note: object is of type [[DYN_TYPE:'S'|'U']] 81 // CHECK-MEMBER-NEXT: {{^ .. .. .. .. .. .. .. .. .. .. .. .. }} 82 // CHECK-MEMBER-NEXT: {{^ \^~~~~~~~~~~(~~~~~~~~~~~~)? *$}} 83 // CHECK-MEMBER-NEXT: {{^ vptr for}} [[DYN_TYPE]] 84 return p->b; 85 86 // CHECK-NULL-MEMBER: vptr.cpp:[[@LINE-2]]:15: runtime error: member access within address [[PTR:0x[0-9a-f]*]] which does not point to an object of type 'T' 87 // CHECK-NULL-MEMBER-NEXT: [[PTR]]: note: object has invalid vptr 88 // CHECK-NULL-MEMBER-NEXT: {{^ .. .. .. .. 00 00 00 00 00 00 00 00 }} 89 // CHECK-NULL-MEMBER-NEXT: {{^ \^~~~~~~~~~~(~~~~~~~~~~~~)? *$}} 90 // CHECK-NULL-MEMBER-NEXT: {{^ invalid vptr}} 91 92 case 'f': 93 // CHECK-MEMFUN: vptr.cpp:[[@LINE+5]]:12: runtime error: member call on address [[PTR:0x[0-9a-f]*]] which does not point to an object of type 'T' 94 // CHECK-MEMFUN-NEXT: [[PTR]]: note: object is of type [[DYN_TYPE:'S'|'U']] 95 // CHECK-MEMFUN-NEXT: {{^ .. .. .. .. .. .. .. .. .. .. .. .. }} 96 // CHECK-MEMFUN-NEXT: {{^ \^~~~~~~~~~~(~~~~~~~~~~~~)? *$}} 97 // CHECK-MEMFUN-NEXT: {{^ vptr for}} [[DYN_TYPE]] 98 return p->g(); 99 100 case 'o': 101 // CHECK-OFFSET: vptr.cpp:[[@LINE+5]]:12: runtime error: member call on address [[PTR:0x[0-9a-f]*]] which does not point to an object of type 'U' 102 // CHECK-OFFSET-NEXT: 0x{{[0-9a-f]*}}: note: object is base class subobject at offset {{8|16}} within object of type [[DYN_TYPE:'U']] 103 // CHECK-OFFSET-NEXT: {{^ .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. }} 104 // CHECK-OFFSET-NEXT: {{^ \^ ( ~~~~~~~~~~~~)~~~~~~~~~~~ *$}} 105 // CHECK-OFFSET-NEXT: {{^ ( )?vptr for}} 'T' base class of [[DYN_TYPE]] 106 return reinterpret_cast<U*>(p)->v() - 2; 107 108 case 'c': 109 // CHECK-DOWNCAST: vptr.cpp:[[@LINE+5]]:5: runtime error: downcast of address [[PTR:0x[0-9a-f]*]] which does not point to an object of type 'T' 110 // CHECK-DOWNCAST-NEXT: [[PTR]]: note: object is of type [[DYN_TYPE:'S'|'U']] 111 // CHECK-DOWNCAST-NEXT: {{^ .. .. .. .. .. .. .. .. .. .. .. .. }} 112 // CHECK-DOWNCAST-NEXT: {{^ \^~~~~~~~~~~(~~~~~~~~~~~~)? *$}} 113 // CHECK-DOWNCAST-NEXT: {{^ vptr for}} [[DYN_TYPE]] 114 static_cast<T*>(reinterpret_cast<S*>(p)); 115 return 0; 116 } 117 } 118