blob: 3092bacffe5a258f161b6f6c3f602b57d4112dcc [file] [log] [blame]
Qin Zhao4175a6d2016-06-02 18:45:25 +00001// RUN: %clang_esan_frag -O0 %s -DPART1 -c -o %t-part1.o 2>&1
2// RUN: %clang_esan_frag -O0 %s -DPART2 -c -o %t-part2.o 2>&1
Derek Bruening515c15c2016-05-24 23:03:52 +00003// RUN: %clang_esan_frag -O0 %s -DMAIN -c -o %t-main.o 2>&1
Qin Zhao4175a6d2016-06-02 18:45:25 +00004// RUN: %clang_esan_frag -O0 %t-part1.o %t-part2.o %t-main.o -o %t 2>&1
Derek Bruening515c15c2016-05-24 23:03:52 +00005// RUN: %env_esan_opts=verbosity=2 %run %t 2>&1 | FileCheck %s
6
7// We generate two different object files from this file with different
8// macros, and then link them together. We do this to test how we handle
9// separate compilation with multiple compilation units.
10
11#include <stdio.h>
12
13extern "C" {
Qin Zhao4175a6d2016-06-02 18:45:25 +000014 void part1();
15 void part2();
Derek Bruening515c15c2016-05-24 23:03:52 +000016}
17
Qin Zhao4175a6d2016-06-02 18:45:25 +000018//===-- compilation unit part1 without main function ----------------------===//
Derek Bruening515c15c2016-05-24 23:03:52 +000019
Qin Zhao4175a6d2016-06-02 18:45:25 +000020#ifdef PART1
21struct A {
22 int x;
23 int y;
24};
25
26struct B {
27 float m;
28 double n;
29};
30
31union U {
32 float f;
33 double d;
34};
35
36// Same struct in both main and part1.
37struct S {
38 int s1;
39 int s2;
40};
41
42// Different structs with the same name in main and part1.
43struct D {
44 int d1;
45 int d2;
46};
47
48void part1()
Derek Bruening515c15c2016-05-24 23:03:52 +000049{
Qin Zhao4175a6d2016-06-02 18:45:25 +000050 struct A a;
51 struct B b;
52 union U u;
53 struct S s;
54 struct D d;
55 for (int i = 0; i < (1 << 11); i++)
56 a.x = 0;
57 a.y = 1;
58 b.m = 2.0;
59 for (int i = 0; i < (1 << 21); i++)
60 b.n = 3.0;
61 u.f = 0.0;
62 u.d = 1.0;
63 s.s1 = 0;
64 d.d1 = 0;
Derek Bruening515c15c2016-05-24 23:03:52 +000065}
Qin Zhao4175a6d2016-06-02 18:45:25 +000066#endif // PART1
67
68//===-- compilation unit part2 without main function ----------------------===//
69#ifdef PART2
70// No struct in this part.
71void part2()
72{
73 // do nothing
74}
75#endif // PART2
Derek Bruening515c15c2016-05-24 23:03:52 +000076
77//===-- compilation unit with main function -------------------------------===//
78
79#ifdef MAIN
Qin Zhao4175a6d2016-06-02 18:45:25 +000080class C {
81public:
82 struct {
83 int x;
84 int y;
85 } cs;
86 union {
87 float f;
88 double d;
89 } cu;
90 char c[10];
91};
92
93// Same struct in both main and part1.
94struct S {
95 int s1;
96 int s2;
97};
98
99// Different structs with the same name in main and part1.
100struct D {
101 int d1;
102 int d2;
103 int d3;
104};
105
Derek Bruening515c15c2016-05-24 23:03:52 +0000106int main(int argc, char **argv) {
107 // CHECK: in esan::initializeLibrary
Qin Zhao7e4933f2016-05-25 17:49:00 +0000108 // CHECK: in esan::initializeCacheFrag
109 // CHECK-NEXT: in esan::processCompilationUnitInit
Qin Zhao4175a6d2016-06-02 18:45:25 +0000110 // CHECK-NEXT: in esan::processCacheFragCompilationUnitInit: {{.*}}struct-simple.cpp with 5 class(es)/struct(s)
111 // CHECK-NEXT: Register struct.A#2#11#11: 2 fields
112 // CHECK-NEXT: Register struct.B#2#3#2: 2 fields
113 // CHECK-NEXT: Register union.U#1#3: 1 fields
114 // CHECK-NEXT: Register struct.S#2#11#11: 2 fields
115 // CHECK-NEXT: Register struct.D#2#11#11: 2 fields
Qin Zhao7e4933f2016-05-25 17:49:00 +0000116 // CHECK-NEXT: in esan::processCompilationUnitInit
Qin Zhao9e396382016-05-31 21:27:39 +0000117 // CHECK-NEXT: in esan::processCacheFragCompilationUnitInit: {{.*}}struct-simple.cpp with 0 class(es)/struct(s)
Qin Zhao4175a6d2016-06-02 18:45:25 +0000118 // CHECK-NEXT: in esan::processCompilationUnitInit
119 // CHECK-NEXT: in esan::processCacheFragCompilationUnitInit: {{.*}}struct-simple.cpp with 5 class(es)/struct(s)
120 // CHECK-NEXT: Register class.C#3#14#13#13: 3 fields
121 // CHECK-NEXT: Register struct.anon#2#11#11: 2 fields
122 // CHECK-NEXT: Register union.anon#1#3: 1 fields
123 // CHECK-NEXT: Duplicated struct.S#2#11#11: 2 fields
124 // CHECK-NEXT: Register struct.D#3#11#11#11: 3 fields
125 struct C c[2];
126 struct S s;
127 struct D d;
128 c[0].cs.x = 0;
129 c[1].cs.y = 1;
130 c[0].cu.f = 0.0;
131 c[1].cu.d = 1.0;
132 c[0].c[2] = 0;
133 s.s1 = 0;
134 d.d1 = 0;
135 d.d2 = 0;
136 part1();
137 part2();
Derek Bruening515c15c2016-05-24 23:03:52 +0000138 return 0;
Qin Zhao7e4933f2016-05-25 17:49:00 +0000139 // CHECK: in esan::finalizeLibrary
140 // CHECK-NEXT: in esan::finalizeCacheFrag
Qin Zhao7e4933f2016-05-25 17:49:00 +0000141 // CHECK-NEXT: in esan::processCompilationUnitExit
Qin Zhao4175a6d2016-06-02 18:45:25 +0000142 // CHECK-NEXT: in esan::processCacheFragCompilationUnitExit: {{.*}}struct-simple.cpp with 5 class(es)/struct(s)
143 // CHECK-NEXT: Unregister class.C#3#14#13#13: 3 fields
Qin Zhaobc929e42016-06-03 20:48:17 +0000144 // CHECK-NEXT: {{.*}} class C
Qin Zhaoa4a72202016-06-10 02:10:34 +0000145 // CHECK-NEXT: {{.*}} size = 32, count = 5, ratio = 3
Qin Zhaoe24bc362016-06-17 04:50:11 +0000146 // CHECK-NEXT: {{.*}} # 0: offset = 0, size = 8, count = 2, type = %struct.anon = type { i32, i32 }
147 // CHECK-NEXT: {{.*}} # 1: offset = 8, size = 8, count = 2, type = %union.anon = type { double }
148 // CHECK-NEXT: {{.*}} # 2: offset = 16, size = 10, count = 1, type = [10 x i8]
Qin Zhao4175a6d2016-06-02 18:45:25 +0000149 // CHECK-NEXT: Unregister struct.anon#2#11#11: 2 fields
Qin Zhaobc929e42016-06-03 20:48:17 +0000150 // CHECK-NEXT: {{.*}} struct anon
Qin Zhaoa4a72202016-06-10 02:10:34 +0000151 // CHECK-NEXT: {{.*}} size = 8, count = 2, ratio = 1
Qin Zhaoe24bc362016-06-17 04:50:11 +0000152 // CHECK-NEXT: {{.*}} # 0: offset = 0, size = 4, count = 1, type = i32
153 // CHECK-NEXT: {{.*}} # 1: offset = 4, size = 4, count = 1, type = i32
Qin Zhao4175a6d2016-06-02 18:45:25 +0000154 // CHECK-NEXT: Unregister union.anon#1#3: 1 fields
155 // CHECK-NEXT: Unregister struct.S#2#11#11: 2 fields
Qin Zhaobc929e42016-06-03 20:48:17 +0000156 // CHECK-NEXT: {{.*}} struct S
Qin Zhaoa4a72202016-06-10 02:10:34 +0000157 // CHECK-NEXT: {{.*}} size = 8, count = 2, ratio = 2
Qin Zhaoe24bc362016-06-17 04:50:11 +0000158 // CHECK-NEXT: {{.*}} # 0: offset = 0, size = 4, count = 2, type = i32
159 // CHECK-NEXT: {{.*}} # 1: offset = 4, size = 4, count = 0, type = i32
Qin Zhao4175a6d2016-06-02 18:45:25 +0000160 // CHECK-NEXT: Unregister struct.D#3#11#11#11: 3 fields
Qin Zhaobc929e42016-06-03 20:48:17 +0000161 // CHECK-NEXT: {{.*}} struct D
Qin Zhaoa4a72202016-06-10 02:10:34 +0000162 // CHECK-NEXT: {{.*}} size = 12, count = 2, ratio = 2
Qin Zhaoe24bc362016-06-17 04:50:11 +0000163 // CHECK-NEXT: {{.*}} # 0: offset = 0, size = 4, count = 1, type = i32
164 // CHECK-NEXT: {{.*}} # 1: offset = 4, size = 4, count = 1, type = i32
165 // CHECK-NEXT: {{.*}} # 2: offset = 8, size = 4, count = 0, type = i32
Qin Zhao7e4933f2016-05-25 17:49:00 +0000166 // CHECK-NEXT: in esan::processCompilationUnitExit
Qin Zhao9e396382016-05-31 21:27:39 +0000167 // CHECK-NEXT: in esan::processCacheFragCompilationUnitExit: {{.*}}struct-simple.cpp with 0 class(es)/struct(s)
Qin Zhao4175a6d2016-06-02 18:45:25 +0000168 // CHECK-NEXT: in esan::processCompilationUnitExit
169 // CHECK-NEXT: in esan::processCacheFragCompilationUnitExit: {{.*}}struct-simple.cpp with 5 class(es)/struct(s)
170 // CHECK-NEXT: Unregister struct.A#2#11#11: 2 fields
Qin Zhaobc929e42016-06-03 20:48:17 +0000171 // CHECK-NEXT: {{.*}} struct A
Qin Zhaoa4a72202016-06-10 02:10:34 +0000172 // CHECK-NEXT: {{.*}} size = 8, count = 2049, ratio = 2048
Qin Zhaoe24bc362016-06-17 04:50:11 +0000173 // CHECK-NEXT: {{.*}} # 0: offset = 0, size = 4, count = 2048, type = i32
174 // CHECK-NEXT: {{.*}} # 1: offset = 4, size = 4, count = 1, type = i32
Qin Zhao4175a6d2016-06-02 18:45:25 +0000175 // CHECK-NEXT: Unregister struct.B#2#3#2: 2 fields
Qin Zhaobc929e42016-06-03 20:48:17 +0000176 // CHECK-NEXT: {{.*}} struct B
Qin Zhaoa4a72202016-06-10 02:10:34 +0000177 // CHECK-NEXT: {{.*}} size = 16, count = 2097153, ratio = 2097152
Qin Zhaoe24bc362016-06-17 04:50:11 +0000178 // CHECK-NEXT: {{.*}} # 0: offset = 0, size = 4, count = 1, type = float
179 // CHECK-NEXT: {{.*}} # 1: offset = 8, size = 8, count = 2097152, type = double
Qin Zhao4175a6d2016-06-02 18:45:25 +0000180 // CHECK-NEXT: Unregister union.U#1#3: 1 fields
181 // CHECK-NEXT: Duplicated struct.S#2#11#11: 2 fields
182 // CHECK-NEXT: Unregister struct.D#2#11#11: 2 fields
Qin Zhaobc929e42016-06-03 20:48:17 +0000183 // CHECK-NEXT: {{.*}} struct D
Qin Zhaoa4a72202016-06-10 02:10:34 +0000184 // CHECK-NEXT: {{.*}} size = 8, count = 1, ratio = 1
Qin Zhaoe24bc362016-06-17 04:50:11 +0000185 // CHECK-NEXT: {{.*}} # 0: offset = 0, size = 4, count = 1, type = i32
186 // CHECK-NEXT: {{.*}} # 1: offset = 4, size = 4, count = 0, type = i32
Qin Zhaobc929e42016-06-03 20:48:17 +0000187 // CHECK-NEXT: {{.*}}EfficiencySanitizer: total struct field access count = 2099214
Derek Bruening515c15c2016-05-24 23:03:52 +0000188}
189#endif // MAIN