blob: 9b0b9a1197c0b6317b30c330fd6614e6b9cd1eeb [file] [log] [blame]
Peter Collingbourne49d29ed2013-10-20 21:29:32 +00001// RUN: %clangxx -fsanitize=alignment %s -O3 -o %t
Richard Smith5f116492012-12-18 04:23:18 +00002// RUN: %t l0 && %t s0 && %t r0 && %t m0 && %t f0 && %t n0
Richard Smith25ee97f2012-12-18 06:30:32 +00003// RUN: %t l1 2>&1 | FileCheck %s --check-prefix=CHECK-LOAD --strict-whitespace
Richard Smith6ebe4512012-10-09 19:34:32 +00004// 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 Smith5f116492012-12-18 04:23:18 +00008// RUN: %t n1 2>&1 | FileCheck %s --check-prefix=CHECK-NEW
9
10#include <new>
Richard Smith6ebe4512012-10-09 19:34:32 +000011
12struct S {
Richard Smith5f116492012-12-18 04:23:18 +000013 S() {}
Richard Smith6ebe4512012-10-09 19:34:32 +000014 int f() { return 0; }
15 int k;
16};
17
18int main(int, char **argv) {
Richard Smith25ee97f2012-12-18 06:30:32 +000019 char c[] __attribute__((aligned(8))) = { 0, 0, 0, 0, 1, 2, 3, 4, 5 };
Richard Smith6ebe4512012-10-09 19:34:32 +000020
21 // Pointer value may be unspecified here, but behavior is not undefined.
Richard Smith25ee97f2012-12-18 06:30:32 +000022 int *p = (int*)&c[4 + argv[1][1] - '0'];
Richard Smith6ebe4512012-10-09 19:34:32 +000023 S *s = (S*)p;
24
25 (void)*p; // ok!
26
27 switch (argv[1][0]) {
28 case 'l':
Richard Smith25ee97f2012-12-18 06:30:32 +000029 // 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 Smith6ebe4512012-10-09 19:34:32 +000035 case 's':
Richard Smith25ee97f2012-12-18 06:30:32 +000036 // 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 Smith6ebe4512012-10-09 19:34:32 +000040 *p = 1;
41 break;
Richard Smith25ee97f2012-12-18 06:30:32 +000042
Richard Smith6ebe4512012-10-09 19:34:32 +000043 case 'r':
Richard Smith25ee97f2012-12-18 06:30:32 +000044 // 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 Smith6ebe4512012-10-09 19:34:32 +000048 {int &r = *p;}
49 break;
Richard Smith25ee97f2012-12-18 06:30:32 +000050
Richard Smith6ebe4512012-10-09 19:34:32 +000051 case 'm':
Richard Smith25ee97f2012-12-18 06:30:32 +000052 // 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 Smith6ebe4512012-10-09 19:34:32 +000058 case 'f':
Richard Smith25ee97f2012-12-18 06:30:32 +000059 // 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 Smith5f116492012-12-18 04:23:18 +000065 case 'n':
66 // FIXME: Provide a better source location here.
Alexey Samsonov90b0f1e2013-10-04 08:55:03 +000067 // 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 Smith25ee97f2012-12-18 06:30:32 +000068 // 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 Smith6ebe4512012-10-09 19:34:32 +000072 }
73}