blob: 0d09a133d438a490781a1b7f3e2b7d955b08b769 [file] [log] [blame]
Kostya Serebryany293dc9b2014-10-16 20:54:52 +00001// Test -fsanitize-address-field-padding
2// RUN: echo 'type:SomeNamespace::BlacklistedByName=field-padding' > %t.type.blacklist
3// RUN: echo 'src:*sanitize-address-field-padding.cpp=field-padding' > %t.file.blacklist
NAKAMURA Takumie3167222014-10-17 12:48:01 +00004// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsanitize=address -fsanitize-address-field-padding=1 -fsanitize-blacklist=%t.type.blacklist -Rsanitize-address -emit-llvm -o - %s 2>&1 | FileCheck %s
Kostya Serebryany5f1b4e82014-10-31 19:01:02 +00005// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsanitize=address -fsanitize-address-field-padding=1 -fsanitize-blacklist=%t.type.blacklist -Rsanitize-address -emit-llvm -o - %s -O1 -mconstructor-aliases 2>&1 | FileCheck %s --check-prefix=WITH_CTOR_ALIASES
NAKAMURA Takumie3167222014-10-17 12:48:01 +00006// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsanitize=address -fsanitize-address-field-padding=1 -fsanitize-blacklist=%t.file.blacklist -Rsanitize-address -emit-llvm -o - %s 2>&1 | FileCheck %s --check-prefix=FILE_BLACKLIST
Kostya Serebryany293dc9b2014-10-16 20:54:52 +00007// RUN: %clang_cc1 -fsanitize=address -emit-llvm -o - %s 2>&1 | FileCheck %s --check-prefix=NO_PADDING
8// REQUIRES: shell
9//
10
11// The reasons to ignore a particular class are not set in stone and will change.
12//
13// CHECK: -fsanitize-address-field-padding applied to Positive1
14// CHECK: -fsanitize-address-field-padding ignored for Negative1 because it is trivially copyable
15// CHECK: -fsanitize-address-field-padding ignored for Negative2 because it is trivially copyable
16// CHECK: -fsanitize-address-field-padding ignored for Negative3 because it is a union
17// CHECK: -fsanitize-address-field-padding ignored for Negative4 because it is trivially copyable
18// CHECK: -fsanitize-address-field-padding ignored for Negative5 because it is packed
19// CHECK: -fsanitize-address-field-padding ignored for SomeNamespace::BlacklistedByName because it is blacklisted
Kostya Serebryany233877542014-10-17 00:47:30 +000020// CHECK: -fsanitize-address-field-padding ignored for ExternCStruct because it is not C++
Kostya Serebryany293dc9b2014-10-16 20:54:52 +000021//
22// FILE_BLACKLIST: -fsanitize-address-field-padding ignored for Positive1 because it is in a blacklisted file
23// FILE_BLACKLIST-NOT: __asan_poison_intra_object_redzone
24// NO_PADDING-NOT: __asan_poison_intra_object_redzone
25
26
27class Positive1 {
28 public:
29 Positive1() {}
30 ~Positive1() {}
31 int make_it_non_standard_layout;
32 private:
33 char private1;
34 int private2;
35 short private_array[6];
36 long long private3;
37};
38
39Positive1 positive1;
40// Positive1 with extra paddings
41// CHECK: type { i32, [12 x i8], i8, [15 x i8], i32, [12 x i8], [6 x i16], [12 x i8], i64, [8 x i8] }
42
Kostya Serebryany64449212014-10-17 21:02:13 +000043struct VirtualBase {
44 int foo;
45};
46
47class ClassWithVirtualBase : public virtual VirtualBase {
48 public:
49 ClassWithVirtualBase() {}
50 ~ClassWithVirtualBase() {}
51 int make_it_non_standard_layout;
52 private:
53 char x[7];
54 char y[9];
55};
56
57ClassWithVirtualBase class_with_virtual_base;
58
Kostya Serebryany68c29da2014-10-27 19:34:10 +000059class WithFlexibleArray1 {
60 public:
61 WithFlexibleArray1() {}
62 ~WithFlexibleArray1() {}
63 int make_it_non_standard_layout;
64 private:
65 char private1[33];
66 int flexible[]; // Don't insert padding after this field.
67};
68
69WithFlexibleArray1 with_flexible_array1;
70// CHECK: %class.WithFlexibleArray1 = type { i32, [12 x i8], [33 x i8], [15 x i8], [0 x i32] }
71
72class WithFlexibleArray2 {
73 public:
74 char x[21];
75 WithFlexibleArray1 flex1; // Don't insert padding after this field.
76};
77
78WithFlexibleArray2 with_flexible_array2;
79// CHECK: %class.WithFlexibleArray2 = type { [21 x i8], [11 x i8], %class.WithFlexibleArray1 }
80
81class WithFlexibleArray3 {
82 public:
83 char x[13];
84 WithFlexibleArray2 flex2; // Don't insert padding after this field.
85};
86
87WithFlexibleArray3 with_flexible_array3;
88
Kostya Serebryany64449212014-10-17 21:02:13 +000089
Kostya Serebryany293dc9b2014-10-16 20:54:52 +000090class Negative1 {
91 public:
92 Negative1() {}
93 int public1, public2;
94};
95Negative1 negative1;
96// CHECK: type { i32, i32 }
97
98class Negative2 {
99 public:
100 Negative2() {}
101 private:
102 int private1, private2;
103};
104Negative2 negative2;
105// CHECK: type { i32, i32 }
106
107union Negative3 {
108 char m1[8];
109 long long m2;
110};
111
112Negative3 negative3;
113// CHECK: type { i64 }
114
115class Negative4 {
116 public:
117 Negative4() {}
118 // No DTOR
119 int make_it_non_standard_layout;
120 private:
121 char private1;
122 int private2;
123};
124
125Negative4 negative4;
126// CHECK: type { i32, i8, i32 }
127
128class __attribute__((packed)) Negative5 {
129 public:
130 Negative5() {}
131 ~Negative5() {}
132 int make_it_non_standard_layout;
133 private:
134 char private1;
135 int private2;
136};
137
138Negative5 negative5;
139// CHECK: type <{ i32, i8, i32 }>
140
141
142namespace SomeNamespace {
143class BlacklistedByName {
144 public:
145 BlacklistedByName() {}
146 ~BlacklistedByName() {}
147 int make_it_non_standard_layout;
148 private:
149 char private1;
150 int private2;
151};
152} // SomeNamespace
153
154SomeNamespace::BlacklistedByName blacklisted_by_name;
155
156extern "C" {
157class ExternCStruct {
158 public:
159 ExternCStruct() {}
160 ~ExternCStruct() {}
161 int make_it_non_standard_layout;
162 private:
163 char private1;
164 int private2;
165};
166} // extern "C"
167
168ExternCStruct extern_C_struct;
169
170// CTOR
Renato Golin031e8172014-10-17 10:09:25 +0000171// CHECK-LABEL: define {{.*}}Positive1C1Ev
Kostya Serebryany330e9f62014-10-16 21:22:40 +0000172// CHECK: call void @__asan_poison_intra_object_redzone({{.*}}12)
173// CHECK: call void @__asan_poison_intra_object_redzone({{.*}}15)
174// CHECK: call void @__asan_poison_intra_object_redzone({{.*}}12)
175// CHECK: call void @__asan_poison_intra_object_redzone({{.*}}12)
176// CHECK: call void @__asan_poison_intra_object_redzone({{.*}}8)
Kostya Serebryany293dc9b2014-10-16 20:54:52 +0000177// CHECK-NOT: __asan_poison_intra_object_redzone
178// CHECK: ret void
Kostya Serebryany233877542014-10-17 00:47:30 +0000179//
Kostya Serebryany293dc9b2014-10-16 20:54:52 +0000180// DTOR
Kostya Serebryany330e9f62014-10-16 21:22:40 +0000181// CHECK: call void @__asan_unpoison_intra_object_redzone({{.*}}12)
182// CHECK: call void @__asan_unpoison_intra_object_redzone({{.*}}15)
183// CHECK: call void @__asan_unpoison_intra_object_redzone({{.*}}12)
184// CHECK: call void @__asan_unpoison_intra_object_redzone({{.*}}12)
185// CHECK: call void @__asan_unpoison_intra_object_redzone({{.*}}8)
Kostya Serebryany293dc9b2014-10-16 20:54:52 +0000186// CHECK-NOT: __asan_unpoison_intra_object_redzone
187// CHECK: ret void
Kostya Serebryany64449212014-10-17 21:02:13 +0000188//
189//
190// CHECK-LABEL: define linkonce_odr void @_ZN20ClassWithVirtualBaseC1Ev
191// CHECK: call void @__asan_poison_intra_object_redzone({{.*}} 12)
192// CHECK: call void @__asan_poison_intra_object_redzone({{.*}} 9)
193// CHECK: call void @__asan_poison_intra_object_redzone({{.*}} 15)
194// CHECK-NOT: __asan_poison_intra_object_redzone
195// CHECK: ret void
196//
Kostya Serebryany5f1b4e82014-10-31 19:01:02 +0000197
198struct WithVirtualDtor {
199 virtual ~WithVirtualDtor();
200 int x, y;
201};
202struct InheritsFrom_WithVirtualDtor: WithVirtualDtor {
203 int a, b;
204 InheritsFrom_WithVirtualDtor() {}
205 ~InheritsFrom_WithVirtualDtor() {}
206};
207
208void Create_InheritsFrom_WithVirtualDtor() {
209 InheritsFrom_WithVirtualDtor x;
210}
211
212
213// Make sure the dtor of InheritsFrom_WithVirtualDtor remains in the code,
214// i.e. we ignore -mconstructor-aliases when field paddings are added
215// because the paddings in InheritsFrom_WithVirtualDtor needs to be unpoisoned
216// in the dtor.
217// WITH_CTOR_ALIASES-LABEL: define void @_Z35Create_InheritsFrom_WithVirtualDtor
218// WITH_CTOR_ALIASES-NOT: call void @_ZN15WithVirtualDtorD2Ev
219// WITH_CTOR_ALIASES: call void @_ZN28InheritsFrom_WithVirtualDtorD2Ev
220// WITH_CTOR_ALIASES: ret void