Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 1 | // RUN: %clangxx -fsanitize=alignment -g %s -O3 -o %t |
| 2 | // RUN: %run %t l0 && %run %t s0 && %run %t r0 && %run %t m0 && %run %t f0 && %run %t n0 && %run %t u0 |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 3 | // RUN: %run %t l1 2>&1 | FileCheck %s --check-prefix=CHECK-LOAD --strict-whitespace |
| 4 | // RUN: %run %t s1 2>&1 | FileCheck %s --check-prefix=CHECK-STORE |
| 5 | // RUN: %run %t r1 2>&1 | FileCheck %s --check-prefix=CHECK-REFERENCE |
| 6 | // RUN: %run %t m1 2>&1 | FileCheck %s --check-prefix=CHECK-MEMBER |
| 7 | // RUN: %run %t f1 2>&1 | FileCheck %s --check-prefix=CHECK-MEMFUN |
| 8 | // RUN: %run %t n1 2>&1 | FileCheck %s --check-prefix=CHECK-NEW |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 9 | // RUN: %run %t u1 2>&1 | FileCheck %s --check-prefix=CHECK-UPCAST |
| 10 | // RUN: UBSAN_OPTIONS=print_stacktrace=1 %run %t l1 2>&1 | FileCheck %s --check-prefix=CHECK-LOAD --check-prefix=CHECK-%os-STACK-LOAD |
| 11 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 12 | // RUN: %clangxx -fsanitize=alignment -fno-sanitize-recover=alignment %s -O3 -o %t |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 13 | // RUN: not %run %t w1 2>&1 | FileCheck %s --check-prefix=CHECK-WILD |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 14 | |
| 15 | #include <new> |
| 16 | |
| 17 | struct S { |
| 18 | S() {} |
| 19 | int f() { return 0; } |
| 20 | int k; |
| 21 | }; |
| 22 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 23 | struct T : S { |
| 24 | int t; |
| 25 | }; |
| 26 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 27 | int main(int, char **argv) { |
| 28 | char c[] __attribute__((aligned(8))) = { 0, 0, 0, 0, 1, 2, 3, 4, 5 }; |
| 29 | |
| 30 | // Pointer value may be unspecified here, but behavior is not undefined. |
| 31 | int *p = (int*)&c[4 + argv[1][1] - '0']; |
| 32 | S *s = (S*)p; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 33 | T *t = (T*)p; |
| 34 | |
| 35 | void *wild = reinterpret_cast<void *>(0x123L); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 36 | |
| 37 | (void)*p; // ok! |
| 38 | |
| 39 | switch (argv[1][0]) { |
| 40 | case 'l': |
| 41 | // CHECK-LOAD: misaligned.cpp:[[@LINE+4]]:12: runtime error: load of misaligned address [[PTR:0x[0-9a-f]*]] for type 'int', which requires 4 byte alignment |
| 42 | // CHECK-LOAD-NEXT: [[PTR]]: note: pointer points here |
| 43 | // CHECK-LOAD-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
| 44 | // CHECK-LOAD-NEXT: {{^ \^}} |
| 45 | return *p && 0; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 46 | // Slow stack unwinding is disabled on Darwin for now, see |
| 47 | // https://code.google.com/p/address-sanitizer/issues/detail?id=137 |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 48 | // CHECK-Linux-STACK-LOAD: #0 {{.*}}main{{.*}}misaligned.cpp |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 49 | // Check for the already checked line to avoid lit error reports. |
| 50 | // CHECK-Darwin-STACK-LOAD: {{ }} |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 51 | |
| 52 | case 's': |
| 53 | // CHECK-STORE: misaligned.cpp:[[@LINE+4]]:5: runtime error: store to misaligned address [[PTR:0x[0-9a-f]*]] for type 'int', which requires 4 byte alignment |
| 54 | // CHECK-STORE-NEXT: [[PTR]]: note: pointer points here |
| 55 | // CHECK-STORE-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
| 56 | // CHECK-STORE-NEXT: {{^ \^}} |
| 57 | *p = 1; |
| 58 | break; |
| 59 | |
| 60 | case 'r': |
| 61 | // CHECK-REFERENCE: misaligned.cpp:[[@LINE+4]]:15: runtime error: reference binding to misaligned address [[PTR:0x[0-9a-f]*]] for type 'int', which requires 4 byte alignment |
| 62 | // CHECK-REFERENCE-NEXT: [[PTR]]: note: pointer points here |
| 63 | // CHECK-REFERENCE-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
| 64 | // CHECK-REFERENCE-NEXT: {{^ \^}} |
| 65 | {int &r = *p;} |
| 66 | break; |
| 67 | |
| 68 | case 'm': |
| 69 | // CHECK-MEMBER: misaligned.cpp:[[@LINE+4]]:15: runtime error: member access within misaligned address [[PTR:0x[0-9a-f]*]] for type 'S', which requires 4 byte alignment |
| 70 | // CHECK-MEMBER-NEXT: [[PTR]]: note: pointer points here |
| 71 | // CHECK-MEMBER-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
| 72 | // CHECK-MEMBER-NEXT: {{^ \^}} |
| 73 | return s->k && 0; |
| 74 | |
| 75 | case 'f': |
| 76 | // CHECK-MEMFUN: misaligned.cpp:[[@LINE+4]]:12: runtime error: member call on misaligned address [[PTR:0x[0-9a-f]*]] for type 'S', which requires 4 byte alignment |
| 77 | // CHECK-MEMFUN-NEXT: [[PTR]]: note: pointer points here |
| 78 | // CHECK-MEMFUN-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
| 79 | // CHECK-MEMFUN-NEXT: {{^ \^}} |
| 80 | return s->f() && 0; |
| 81 | |
| 82 | case 'n': |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 83 | // CHECK-NEW: misaligned.cpp:[[@LINE+4]]:21: runtime error: constructor call on misaligned address [[PTR:0x[0-9a-f]*]] for type 'S', which requires 4 byte alignment |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 84 | // CHECK-NEW-NEXT: [[PTR]]: note: pointer points here |
| 85 | // CHECK-NEW-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
| 86 | // CHECK-NEW-NEXT: {{^ \^}} |
| 87 | return (new (s) S)->k && 0; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 88 | |
| 89 | case 'u': { |
| 90 | // CHECK-UPCAST: misaligned.cpp:[[@LINE+4]]:17: runtime error: upcast of misaligned address [[PTR:0x[0-9a-f]*]] for type 'T', which requires 4 byte alignment |
| 91 | // CHECK-UPCAST-NEXT: [[PTR]]: note: pointer points here |
| 92 | // CHECK-UPCAST-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
| 93 | // CHECK-UPCAST-NEXT: {{^ \^}} |
| 94 | S *s2 = (S*)t; |
| 95 | return s2->f(); |
| 96 | } |
| 97 | |
| 98 | case 'w': |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 99 | // CHECK-WILD: misaligned.cpp:[[@LINE+3]]:35: runtime error: member access within misaligned address 0x{{0+}}123 for type 'S', which requires 4 byte alignment |
| 100 | // CHECK-WILD-NEXT: 0x{{0+}}123: note: pointer points here |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 101 | // CHECK-WILD-NEXT: <memory cannot be printed> |
| 102 | return static_cast<S*>(wild)->k; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 103 | } |
| 104 | } |