1 | // RUN: %clangxx %gmlt -fsanitize=alignment %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 |
3 | // RUN: %run %t l1 2>&1 | FileCheck %s --check-prefix=CHECK-LOAD --strict-whitespace |
4 | // RUN: %run %t L1 2>&1 | FileCheck %s --check-prefix=CHECK-MEMCPY-LOAD |
5 | // RUN: %run %t S1 2>&1 | FileCheck %s --check-prefix=CHECK-MEMCPY-STORE |
6 | // RUN: %run %t r1 2>&1 | FileCheck %s --check-prefix=CHECK-REFERENCE |
7 | // RUN: %run %t m1 2>&1 | FileCheck %s --check-prefix=CHECK-MEMBER |
8 | // RUN: %run %t f1 2>&1 | FileCheck %s --check-prefix=CHECK-MEMFUN |
9 | // RUN: %run %t n1 2>&1 | FileCheck %s --check-prefix=CHECK-NEW |
10 | // RUN: %run %t u1 2>&1 | FileCheck %s --check-prefix=CHECK-UPCAST |
11 | // RUN: %env_ubsan_opts=print_stacktrace=1 %run %t l1 2>&1 | FileCheck %s --check-prefix=CHECK-LOAD --check-prefix=CHECK-STACK-LOAD |
12 | |
13 | // RUN: %clangxx -fsanitize=alignment -fno-sanitize-recover=alignment %s -O3 -o %t |
14 | // RUN: not %run %t s1 2>&1 | FileCheck %s --check-prefix=CHECK-STORE |
15 | // RUN: not %run %t w1 2>&1 | FileCheck %s --check-prefix=CHECK-WILD |
16 | // Compilation error make the test fails. |
17 | // XFAIL: target={{.*openbsd.*}} |
18 | |
19 | #include <new> |
20 | #include <string.h> |
21 | |
22 | struct S { |
23 | S() {} |
24 | int f() { return 0; } |
25 | int k; |
26 | }; |
27 | |
28 | struct T : S { |
29 | int t; |
30 | }; |
31 | |
32 | int main(int, char **argv) { |
33 | char c[] __attribute__((aligned(8))) = { 0, 0, 0, 0, 1, 2, 3, 4, 5 }; |
34 | |
35 | // Pointer value may be unspecified here, but behavior is not undefined. |
36 | int *p = (int*)&c[4 + argv[1][1] - '0']; |
37 | S *s = (S*)p; |
38 | T *t = (T*)p; |
39 | |
40 | void *wild = reinterpret_cast<void *>(0x123L); |
41 | |
42 | (void)*p; // ok! |
43 | |
44 | switch (argv[1][0]) { |
45 | case 'l': |
46 | // 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 |
47 | // CHECK-LOAD-NEXT: [[PTR]]: note: pointer points here |
48 | // CHECK-LOAD-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
49 | // CHECK-LOAD-NEXT: {{^ \^}} |
50 | return *p && 0; |
51 | // CHECK-STACK-LOAD: #0 {{.*}}main{{.*}}misaligned.cpp |
52 | |
53 | case 'L': { |
54 | int x; |
55 | // CHECK-MEMCPY-LOAD: misaligned.cpp:[[#@LINE+4]]{{(:16)?}}: runtime error: load of misaligned address [[PTR:0x[0-9a-f]*]] for type 'int *', which requires 4 byte alignment |
56 | // CHECK-MEMCPY-LOAD-NEXT: [[PTR]]: note: pointer points here |
57 | // CHECK-MEMCPY-LOAD-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
58 | // CHECK-MEMCPY-LOAD-NEXT: {{^ \^}} |
59 | memcpy(dest: &x, src: p, n: sizeof(x)); |
60 | return x && 0; |
61 | } |
62 | |
63 | case 's': |
64 | // 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 |
65 | // CHECK-STORE-NEXT: [[PTR]]: note: pointer points here |
66 | // CHECK-STORE-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
67 | // CHECK-STORE-NEXT: {{^ \^}} |
68 | *p = 1; |
69 | break; |
70 | |
71 | case 'S': { |
72 | int x = 1; |
73 | // CHECK-MEMCPY-STORE: misaligned.cpp:[[#@LINE+4]]{{(:12)?}}: runtime error: store to misaligned address [[PTR:0x[0-9a-f]*]] for type 'int *', which requires 4 byte alignment |
74 | // CHECK-MEMCPY-STORE-NEXT: [[PTR]]: note: pointer points here |
75 | // CHECK-MEMCPY-STORE-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
76 | // CHECK-MEMCPY-STORE-NEXT: {{^ \^}} |
77 | memcpy(dest: p, src: &x, n: sizeof(x)); |
78 | break; |
79 | } |
80 | |
81 | case 'r': |
82 | // CHECK-REFERENCE: misaligned.cpp:[[@LINE+4]]{{(:(5|15))?}}: runtime error: reference binding to misaligned address [[PTR:0x[0-9a-f]*]] for type 'int', which requires 4 byte alignment |
83 | // CHECK-REFERENCE-NEXT: [[PTR]]: note: pointer points here |
84 | // CHECK-REFERENCE-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
85 | // CHECK-REFERENCE-NEXT: {{^ \^}} |
86 | {int &r = *p;} |
87 | break; |
88 | |
89 | case 'm': |
90 | // 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 |
91 | // CHECK-MEMBER-NEXT: [[PTR]]: note: pointer points here |
92 | // CHECK-MEMBER-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
93 | // CHECK-MEMBER-NEXT: {{^ \^}} |
94 | return s->k && 0; |
95 | |
96 | case 'f': |
97 | // CHECK-MEMFUN: misaligned.cpp:[[@LINE+4]]{{(:15)?}}: runtime error: member call on misaligned address [[PTR:0x[0-9a-f]*]] for type 'S', which requires 4 byte alignment |
98 | // CHECK-MEMFUN-NEXT: [[PTR]]: note: pointer points here |
99 | // CHECK-MEMFUN-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
100 | // CHECK-MEMFUN-NEXT: {{^ \^}} |
101 | return s->f() && 0; |
102 | |
103 | case 'n': |
104 | // 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 |
105 | // CHECK-NEW-NEXT: [[PTR]]: note: pointer points here |
106 | // CHECK-NEW-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
107 | // CHECK-NEW-NEXT: {{^ \^}} |
108 | return (new (s) S)->k && 0; |
109 | |
110 | case 'u': { |
111 | // 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 |
112 | // CHECK-UPCAST-NEXT: [[PTR]]: note: pointer points here |
113 | // CHECK-UPCAST-NEXT: {{^ 00 00 00 01 02 03 04 05}} |
114 | // CHECK-UPCAST-NEXT: {{^ \^}} |
115 | S *s2 = (S*)t; |
116 | return s2->f(); |
117 | } |
118 | |
119 | case 'w': |
120 | // 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 |
121 | // CHECK-WILD-NEXT: 0x{{0+}}123: note: pointer points here |
122 | // CHECK-WILD-NEXT: <memory cannot be printed> |
123 | return static_cast<S*>(wild)->k; |
124 | } |
125 | } |
126 | |