Debian Codegraph @

clang/test/Analysis/call-invalidation.cpp

raw
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify -analyzer-config eagerly-assume=false %s
2
3 template <class T> void clang_analyzer_dump(T);
4 void clang_analyzer_eval(bool);
5 void clang_analyzer_value(int);
6
7 void usePointer(int * const *);
8 void useReference(int * const &);
9
10 template <typename... Ts> void opaque(Ts...);
11
12 void testPointer() {
13 int x;
14 int *p;
15
16 p = &x;
17 x = 42;
18 clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
19 usePointer(&p);
20 clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}}
21
22 p = &x;
23 x = 42;
24 clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
25 useReference(p);
26 clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}}
27
28 int * const cp1 = &x;
29 x = 42;
30 clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
31 usePointer(&cp1);
32 clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}}
33
34 int * const cp2 = &x;
35 x = 42;
36 clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
37 useReference(cp2);
38 clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}}
39 }
40
41
42 struct Wrapper {
43 int *ptr;
44 };
45
46 void useStruct(Wrapper &w);
47 void useConstStruct(const Wrapper &w);
48
49 void testPointerStruct() {
50 int x;
51 Wrapper w;
52
53 w.ptr = &x;
54 x = 42;
55 clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
56 useStruct(w);
57 clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}}
58
59 w.ptr = &x;
60 x = 42;
61 clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
62 useConstStruct(w);
63 clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}}
64 }
65
66
67 struct RefWrapper {
68 int &ref;
69 };
70
71 void useStruct(RefWrapper &w);
72 void useConstStruct(const RefWrapper &w);
73
74 void testReferenceStruct() {
75 int x;
76 RefWrapper w = { x };
77
78 x = 42;
79 clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
80 useStruct(w);
81 clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}}
82 }
83
84 // FIXME: This test is split into two functions because region invalidation
85 // does not preserve reference bindings.
86 void testConstReferenceStruct() {
87 int x;
88 RefWrapper w = { x };
89
90 x = 42;
91 clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
92 useConstStruct(w);
93 clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}}
94 }
95
96
97 int usePointerPure(int * const *) __attribute__((pure));
98 int usePointerConst(int * const *) __attribute__((const));
99
100 void testPureConst() {
101 extern int global;
102 int x;
103 int *p;
104
105 p = &x;
106 x = 42;
107 global = -5;
108 clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
109 clang_analyzer_eval(global == -5); // expected-warning{{TRUE}}
110
111 (void)usePointerPure(&p);
112 clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
113 clang_analyzer_eval(global == -5); // expected-warning{{TRUE}}
114
115 (void)usePointerConst(&p);
116 clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
117 clang_analyzer_eval(global == -5); // expected-warning{{TRUE}}
118
119 usePointer(&p);
120 clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}}
121 clang_analyzer_eval(global == -5); // expected-warning{{UNKNOWN}}
122 }
123
124
125 struct PlainStruct {
126 int x, y;
127 mutable int z;
128 };
129
130 PlainStruct glob;
131
132 void useAnything(void *);
133 void useAnythingConst(const void *);
134
135 void testInvalidationThroughBaseRegionPointer() {
136 PlainStruct s1;
137 s1.x = 1;
138 s1.z = 1;
139 clang_analyzer_eval(s1.x == 1); // expected-warning{{TRUE}}
140 clang_analyzer_eval(s1.z == 1); // expected-warning{{TRUE}}
141 // Not only passing a structure pointer through const pointer parameter,
142 // but also passing a field pointer through const pointer parameter
143 // should preserve the contents of the structure.
144 useAnythingConst(&(s1.y));
145 clang_analyzer_eval(s1.x == 1); // expected-warning{{TRUE}}
146 // FIXME: Should say "UNKNOWN", because it is not uncommon to
147 // modify a mutable member variable through const pointer.
148 clang_analyzer_eval(s1.z == 1); // expected-warning{{TRUE}}
149 useAnything(&(s1.y));
150 clang_analyzer_eval(s1.x == 1); // expected-warning{{UNKNOWN}}
151 }
152
153
154 void useFirstConstSecondNonConst(const void *x, void *y);
155 void useFirstNonConstSecondConst(void *x, const void *y);
156
157 void testMixedConstNonConstCalls() {
158 PlainStruct s2;
159 s2.x = 1;
160 useFirstConstSecondNonConst(&(s2.x), &(s2.y));
161 clang_analyzer_eval(s2.x == 1); // expected-warning{{UNKNOWN}}
162 s2.x = 1;
163 useFirstNonConstSecondConst(&(s2.x), &(s2.y));
164 clang_analyzer_eval(s2.x == 1); // expected-warning{{UNKNOWN}}
165 s2.y = 1;
166 useFirstConstSecondNonConst(&(s2.x), &(s2.y));
167 clang_analyzer_eval(s2.y == 1); // expected-warning{{UNKNOWN}}
168 s2.y = 1;
169 useFirstNonConstSecondConst(&(s2.x), &(s2.y));
170 clang_analyzer_eval(s2.y == 1); // expected-warning{{UNKNOWN}}
171 }
172
173 namespace std {
174 class Opaque {
175 public:
176 Opaque();
177 int nested_member;
178 };
179 } // namespace std
180
181 struct StdWrappingOpaque {
182 std::Opaque o; // first member
183 int uninit;
184 };
185 struct StdWrappingOpaqueSwapped {
186 int uninit; // first member
187 std::Opaque o;
188 };
189
190 int testStdCtorDoesNotInvalidateParentObject() {
191 StdWrappingOpaque obj;
192 int x = obj.o.nested_member; // no-garbage: std::Opaque::ctor might initialized this
193 int y = obj.uninit; // FIXME: We should have a garbage read here. Read the details.
194 // As the first member ("obj.o") is invalidated, a conjured default binding is bound
195 // to the offset 0 within cluster "obj", and this masks every uninitialized fields
196 // that follows. We need a better store with extents to fix this.
197 return x + y;
198 }
199
200 int testStdCtorDoesNotInvalidateParentObjectSwapped() {
201 StdWrappingOpaqueSwapped obj;
202 int x = obj.o.nested_member; // no-garbage: std::Opaque::ctor might initialized this
203 int y = obj.uninit; // expected-warning {{Assigned value is uninitialized}}
204 return x + y;
205 }
206
207 class UserProvidedOpaque {
208 public:
209 UserProvidedOpaque(); // might reinterpret_cast(this)
210 int nested_member;
211 };
212
213 struct WrappingUserProvidedOpaque {
214 UserProvidedOpaque o; // first member
215 int uninit;
216 };
217 struct WrappingUserProvidedOpaqueSwapped {
218 int uninit; // first member
219 UserProvidedOpaque o;
220 };
221
222 int testUserProvidedCtorInvalidatesParentObject() {
223 WrappingUserProvidedOpaque obj;
224 int x = obj.o.nested_member; // no-garbage: UserProvidedOpaque::ctor might initialized this
225 int y = obj.uninit; // no-garbage: UserProvidedOpaque::ctor might reinterpret_cast(this) and write to the "uninit" member.
226 return x + y;
227 }
228
229 int testUserProvidedCtorInvalidatesParentObjectSwapped() {
230 WrappingUserProvidedOpaqueSwapped obj;
231 int x = obj.o.nested_member; // no-garbage: same as above
232 int y = obj.uninit; // no-garbage: same as above
233 return x + y;
234 }
235
236 struct WrappingStdWrappingOpaqueOuterInits {
237 int first = 1;
238 std::Opaque second;
239 int third = 3;
240 WrappingStdWrappingOpaqueOuterInits() {
241 clang_analyzer_dump(first); // expected-warning {{1 S32b}}
242 clang_analyzer_dump(second.nested_member); // expected-warning {{derived_}}
243 clang_analyzer_dump(third); // expected-warning {{3 S32b}}
244 }
245 };
246
247 struct WrappingUserProvidedOpaqueOuterInits {
248 int first = 1; // Potentially overwritten by UserProvidedOpaque::ctor
249 UserProvidedOpaque second; // Invalidates the object so far.
250 int third = 3; // Happens after UserProvidedOpaque::ctor, thus preserved!
251 WrappingUserProvidedOpaqueOuterInits() {
252 clang_analyzer_dump(first); // expected-warning {{derived_}}
253 clang_analyzer_dump(second.nested_member); // expected-warning {{derived_}}
254 clang_analyzer_dump(third); // expected-warning {{3 S32b}}
255 }
256 };
257
258 extern "C++" {
259 namespace std {
260 inline namespace v1 {
261 namespace custom_ranges {
262 struct Fancy {
263 struct iterator {
264 struct Opaque {
265 Opaque();
266 int nested_member;
267 }; // struct Opaque
268 }; // struct iterator
269 }; // struct Fancy
270 } // namespace custom_ranges
271 } // namespace v1
272 } // namespace std
273 } // extern "C++"
274
275 struct StdWrappingFancyOpaque {
276 int uninit;
277 std::custom_ranges::Fancy::iterator::Opaque o;
278 };
279
280 int testNestedStdNamespacesAndRecords() {
281 StdWrappingFancyOpaque obj;
282 int x = obj.o.nested_member; // no-garbage: ctor
283 int y = obj.uninit; // expected-warning {{Assigned value is uninitialized}}
284 return x + y;
285 }
286
287 struct SpecialVector {
288 SpecialVector(const void *); // Takes a const pointer!
289 int size() const {
290 return Size; // no-warning: We should not warn "uninitialized Size" because the ctor might have initialized it.
291 }
292 int Size;
293 };
294
295 void selfPtrPassedAsConstPointerToOpaqueCtorCall() {
296 // We construct a "SpecialVector" that takes the address of itself
297 // (or to a subobject somewhere itself) by a const-pointer.
298 // Despite the var region "buf" is mentioned via a const argument, the opaque
299 // ctor call should still take precedent and invalidate the underlying object.
300 SpecialVector buf(&buf);
301 buf.size();
302 }
303
304 int aliasing_ptrs_via_mutable_and_const_ptrs() {
305 int x = 1;
306 opaque<const int*, int*>(&x, &x);
307 // expected-warning@+1 {{32s:1}} FIXME: We should not be sure it's 1.
308 clang_analyzer_value(x);
309 return 100 / (x - 1);
310 // expected-warning@-1 {{Division by zero}} FIXME: We shouldn't report this.
311 }