Peter Collingbourne | 49d29ed | 2013-10-20 21:29:32 +0000 | [diff] [blame^] | 1 | // RUN: %clangxx -fsanitize=alignment %s -O3 -o %t |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 2 | // RUN: %t l0 && %t s0 && %t r0 && %t m0 && %t f0 && %t n0 |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 3 | // RUN: %t l1 2>&1 | FileCheck %s --check-prefix=CHECK-LOAD --strict-whitespace |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 4 | // RUN: %t s1 2>&1 | FileCheck %s --check-prefix=CHECK-STORE |
| 5 | // RUN: %t r1 2>&1 | FileCheck %s --check-prefix=CHECK-REFERENCE |
| 6 | // RUN: %t m1 2>&1 | FileCheck %s --check-prefix=CHECK-MEMBER |
| 7 | // RUN: %t f1 2>&1 | FileCheck %s --check-prefix=CHECK-MEMFUN |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 8 | // RUN: %t n1 2>&1 | FileCheck %s --check-prefix=CHECK-NEW |
| 9 | |
| 10 | #include <new> |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 11 | |
| 12 | struct S { |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 13 | S() {} |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 14 | int f() { return 0; } |
| 15 | int k; |
| 16 | }; |
| 17 | |
| 18 | int main(int, char **argv) { |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 19 | char c[] __attribute__((aligned(8))) = { 0, 0, 0, 0, 1, 2, 3, 4, 5 }; |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 20 | |
| 21 | // Pointer value may be unspecified here, but behavior is not undefined. |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 22 | int *p = (int*)&c[4 + argv[1][1] - '0']; |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 23 | S *s = (S*)p; |
| 24 | |
| 25 | (void)*p; // ok! |
| 26 | |
| 27 | switch (argv[1][0]) { |
| 28 | case 'l': |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 29 | // 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 |
| 30 | // CHECK-LOAD-NEXT: [[PTR]]: note: pointer points here |
| 31 | // CHECK-LOAD-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
| 32 | // CHECK-LOAD-NEXT: {{^ \^}} |
| 33 | return *p && 0; |
| 34 | |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 35 | case 's': |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 36 | // 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 |
| 37 | // CHECK-STORE-NEXT: [[PTR]]: note: pointer points here |
| 38 | // CHECK-STORE-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
| 39 | // CHECK-STORE-NEXT: {{^ \^}} |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 40 | *p = 1; |
| 41 | break; |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 42 | |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 43 | case 'r': |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 44 | // 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 |
| 45 | // CHECK-REFERENCE-NEXT: [[PTR]]: note: pointer points here |
| 46 | // CHECK-REFERENCE-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
| 47 | // CHECK-REFERENCE-NEXT: {{^ \^}} |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 48 | {int &r = *p;} |
| 49 | break; |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 50 | |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 51 | case 'm': |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 52 | // 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 |
| 53 | // CHECK-MEMBER-NEXT: [[PTR]]: note: pointer points here |
| 54 | // CHECK-MEMBER-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
| 55 | // CHECK-MEMBER-NEXT: {{^ \^}} |
| 56 | return s->k && 0; |
| 57 | |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 58 | case 'f': |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 59 | // 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 |
| 60 | // CHECK-MEMFUN-NEXT: [[PTR]]: note: pointer points here |
| 61 | // CHECK-MEMFUN-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
| 62 | // CHECK-MEMFUN-NEXT: {{^ \^}} |
| 63 | return s->f() && 0; |
| 64 | |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 65 | case 'n': |
| 66 | // FIXME: Provide a better source location here. |
Alexey Samsonov | 90b0f1e | 2013-10-04 08:55:03 +0000 | [diff] [blame] | 67 | // CHECK-NEW: misaligned{{.*}}+0x{{[0-9a-f]*}}): runtime error: constructor call on misaligned address [[PTR:0x[0-9a-f]*]] for type 'S', which requires 4 byte alignment |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 68 | // CHECK-NEW-NEXT: [[PTR]]: note: pointer points here |
| 69 | // CHECK-NEW-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
| 70 | // CHECK-NEW-NEXT: {{^ \^}} |
| 71 | return (new (s) S)->k && 0; |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 72 | } |
| 73 | } |