blob: 958576f4558de37b5d366089448d9733a6b9b306 [file] [log] [blame]
Daniel Dunbard7d5f022009-03-24 02:24:46 +00001// RUN: clang-cc -analyze -checker-cfref --analyzer-store=basic -analyzer-constraints=basic --verify -fblocks %s &&
Ted Kremenekc037eac2009-07-10 00:41:58 +00002// RUN: clang-cc -analyze -checker-cfref --analyzer-store=basic-old-cast -analyzer-constraints=basic --verify -fblocks %s &&
Ted Kremenek921109a2009-07-01 23:19:52 +00003// RUN: clang-cc -analyze -checker-cfref --analyzer-store=basic -analyzer-constraints=range --verify -fblocks %s &&
Ted Kremenekc037eac2009-07-10 00:41:58 +00004// RUN: clang-cc -analyze -checker-cfref --analyzer-store=basic-old-cast -analyzer-constraints=range --verify -fblocks %s &&
Ted Kremenek921109a2009-07-01 23:19:52 +00005// RUN: clang-cc -analyze -checker-cfref --analyzer-store=region -analyzer-constraints=basic --verify -fblocks %s &&
6// RUN: clang-cc -analyze -checker-cfref --analyzer-store=region -analyzer-constraints=range --verify -fblocks %s
Ted Kremenek2dabd422009-01-22 18:53:15 +00007
Ted Kremenekf684d562009-03-05 18:08:28 +00008typedef struct objc_selector *SEL;
9typedef signed char BOOL;
10typedef int NSInteger;
11typedef unsigned int NSUInteger;
12typedef struct _NSZone NSZone;
Ted Kremenek59978882009-07-08 22:42:46 +000013@class NSInvocation, NSArray, NSMethodSignature, NSCoder, NSString, NSEnumerator;
Ted Kremenekf684d562009-03-05 18:08:28 +000014@protocol NSObject - (BOOL)isEqual:(id)object; @end
15@protocol NSCopying - (id)copyWithZone:(NSZone *)zone; @end
16@protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end
17@protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; @end
18@interface NSObject <NSObject> {} - (id)init; @end
19extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
20@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
21- (NSUInteger)length;
22+ (id)stringWithUTF8String:(const char *)nullTerminatedCString;
23@end extern NSString * const NSBundleDidLoadNotification;
24@interface NSAssertionHandler : NSObject {}
25+ (NSAssertionHandler *)currentHandler;
26- (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format,...;
27@end
28extern NSString * const NSConnectionReplyMode;
Ted Kremenek693de5d2009-03-23 15:42:58 +000029typedef float CGFloat;
30typedef struct _NSPoint {
31 CGFloat x;
32 CGFloat y;
33} NSPoint;
34typedef struct _NSSize {
35 CGFloat width;
36 CGFloat height;
37} NSSize;
38typedef struct _NSRect {
39 NSPoint origin;
40 NSSize size;
41} NSRect;
Ted Kremenek9f67ede2008-10-01 05:05:46 +000042
43// Reduced test case from crash in <rdar://problem/6253157>
Ted Kremenek9f67ede2008-10-01 05:05:46 +000044@interface A @end
45@implementation A
46- (void)foo:(void (^)(NSObject *x))block {
47 if (!((block != ((void *)0)))) {}
48}
49@end
50
Ted Kremenek6dfe2f52008-10-18 22:20:20 +000051// Reduced test case from crash in PR 2796;
52// http://llvm.org/bugs/show_bug.cgi?id=2796
53
54unsigned foo(unsigned x) { return __alignof__((x)) + sizeof(x); }
Ted Kremenek9253b0f2008-10-20 23:14:31 +000055
56// Improvement to path-sensitivity involving compound assignments.
57// Addresses false positive in <rdar://problem/6268365>
58//
59
60unsigned r6268365Aux();
61
62void r6268365() {
63 unsigned x = 0;
64 x &= r6268365Aux();
65 unsigned j = 0;
66
67 if (x == 0) ++j;
68 if (x == 0) x = x / j; // no-warning
69}
70
Ted Kremenekc13b6e22008-10-20 23:40:25 +000071void divzeroassume(unsigned x, unsigned j) {
72 x /= j;
73 if (j == 0) x /= 0; // no-warning
74 if (j == 0) x /= j; // no-warning
75 if (j == 0) x = x / 0; // no-warning
76}
77
78void divzeroassumeB(unsigned x, unsigned j) {
79 x = x / j;
80 if (j == 0) x /= 0; // no-warning
81 if (j == 0) x /= j; // no-warning
82 if (j == 0) x = x / 0; // no-warning
83}
84
Ted Kremenek76dba7b2008-11-13 05:05:34 +000085// InitListExpr processing
86
87typedef float __m128 __attribute__((__vector_size__(16), __may_alias__));
88__m128 return128() {
Ted Kremenek062e2f92008-11-13 06:10:40 +000089 // This compound literal has a Vector type. We currently just
90 // return UnknownVal.
Ted Kremenek76dba7b2008-11-13 05:05:34 +000091 return __extension__(__m128) { 0.0f, 0.0f, 0.0f, 0.0f };
92}
93
Ted Kremenek062e2f92008-11-13 06:10:40 +000094typedef long long __v2di __attribute__ ((__vector_size__ (16)));
95typedef long long __m128i __attribute__ ((__vector_size__ (16), __may_alias__));
96__m128i vec128i(long long __q1, long long __q0) {
97 // This compound literal returns true for both isVectorType() and
98 // isIntegerType().
99 return __extension__ (__m128i)(__v2di){ __q0, __q1 };
100}
101
Ted Kremenek8322d6a2008-12-09 00:14:48 +0000102// Zero-sized VLAs.
103void check_zero_sized_VLA(int x) {
104 if (x)
105 return;
106
Ted Kremenekeaedfea2009-05-10 05:11:21 +0000107 int vla[x]; // expected-warning{{Variable-length array 'vla' has zero elements (undefined behavior)}}
Ted Kremenek159d2482008-12-09 00:44:16 +0000108}
109
110void check_uninit_sized_VLA() {
111 int x;
Ted Kremenekeaedfea2009-05-10 05:11:21 +0000112 int vla[x]; // expected-warning{{Variable-length array 'vla' garbage value for array size}}
Ted Kremenek8322d6a2008-12-09 00:14:48 +0000113}
Ted Kremenek062e2f92008-11-13 06:10:40 +0000114
Ted Kremenek55f7bcb2008-12-15 18:51:00 +0000115// sizeof(void)
116// - Tests a regression reported in PR 3211: http://llvm.org/bugs/show_bug.cgi?id=3211
117void handle_sizeof_void(unsigned flag) {
118 int* p = 0;
119
120 if (flag) {
121 if (sizeof(void) == 1)
122 return;
123 // Infeasible.
124 *p = 1; // no-warning
125 }
126
127 void* q;
128
129 if (!flag) {
130 if (sizeof(*q) == 1)
131 return;
132 // Infeasibe.
133 *p = 1; // no-warning
134 }
135
136 // Infeasible.
137 *p = 1; // no-warning
138}
139
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000140// PR 3422
141void pr3422_helper(char *p);
142void pr3422() {
143 char buf[100];
144 char *q = &buf[10];
145 pr3422_helper(&q[1]);
146}
147
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000148// PR 3543 (handle empty statement expressions)
149int pr_3543(void) {
150 ({});
151}
152
Ted Kremenek265a3052009-02-24 02:23:11 +0000153// <rdar://problem/6611677>
154// This test case test the use of a vector type within an array subscript
155// expression.
156typedef long long __a64vector __attribute__((__vector_size__(8)));
157typedef long long __a128vector __attribute__((__vector_size__(16)));
158static inline __a64vector __attribute__((__always_inline__, __nodebug__))
159my_test_mm_movepi64_pi64(__a128vector a) {
160 return (__a64vector)a[0];
161}
162
Ted Kremenekf684d562009-03-05 18:08:28 +0000163// Test basic tracking of ivars associated with 'self'.
164@interface SelfIvarTest : NSObject {
165 int flag;
166}
167- (void)test_self_tracking;
168@end
169
170@implementation SelfIvarTest
171- (void)test_self_tracking {
172 char *p = 0;
173 char c;
174
175 if (flag)
176 p = "hello";
177
178 if (flag)
179 c = *p; // no-warning
180}
181@end
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000182
Ted Kremenek7de20fe2009-03-11 02:29:48 +0000183// PR 3770
184char pr3770(int x) {
185 int y = x & 0x2;
186 char *p = 0;
187 if (y == 1)
188 p = "hello";
189
190 if (y == 1)
191 return p[0]; // no-warning
192
193 return 'a';
194}
195
Ted Kremenek344d4c82009-03-11 18:17:16 +0000196// PR 3772
Ted Kremenekfa6228d2009-03-11 02:52:39 +0000197// - We just want to test that this doesn't crash the analyzer.
198typedef struct st ST;
199struct st { char *name; };
200extern ST *Cur_Pu;
201
Ted Kremenek344d4c82009-03-11 18:17:16 +0000202void pr3772(void)
Ted Kremenekfa6228d2009-03-11 02:52:39 +0000203{
204 static ST *last_Cur_Pu;
205 if (last_Cur_Pu == Cur_Pu) {
206 return;
207 }
208}
209
Ted Kremenek344d4c82009-03-11 18:17:16 +0000210// PR 3780 - This tests that StmtIterator isn't broken for VLAs in DeclGroups.
211void pr3780(int sz) { typedef double MAT[sz][sz]; }
Ted Kremenekfa6228d2009-03-11 02:52:39 +0000212
Ted Kremenekec099f12009-03-18 22:10:22 +0000213// <rdar://problem/6695527> - Test that we don't symbolicate doubles before
214// we are ready to do something with them.
215int rdar6695527(double x) {
216 if (!x) { return 0; }
217 return 1;
218}
Ted Kremenek693de5d2009-03-23 15:42:58 +0000219
220// <rdar://problem/6708148> - Test that we properly invalidate structs
221// passed-by-reference to a function.
222void pr6708148_invalidate(NSRect *x);
223void pr6708148_use(NSRect x);
224void pr6708148_test(void) {
225 NSRect x;
226 pr6708148_invalidate(&x);
227 pr6708148_use(x); // no-warning
228}
229
Ted Kremenekb7252322009-04-10 00:01:14 +0000230// Handle both kinds of noreturn attributes for pruning paths.
231void rdar_6777003_noret() __attribute__((noreturn));
232void rdar_6777003_analyzer_noret() __attribute__((analyzer_noreturn));
233
234void rdar_6777003(int x) {
235 int *p = 0;
236
237 if (x == 1) {
238 rdar_6777003_noret();
239 *p = 1; // no-warning;
240 }
241
242 if (x == 2) {
243 rdar_6777003_analyzer_noret();
244 *p = 1; // no-warning;
245 }
246
247 *p = 1; // expected-warning{{Dereference of null pointer}}
248}
249
Ted Kremenekaf48fdd2009-04-21 22:38:05 +0000250// For pointer arithmetic, --/++ should be treated as preserving non-nullness,
251// regardless of how well the underlying StoreManager reasons about pointer
252// arithmetic.
253// <rdar://problem/6777209>
Ted Kremenekaf48fdd2009-04-21 22:38:05 +0000254void rdar_6777209(char *p) {
255 if (p == 0)
256 return;
257
258 ++p;
259
260 // This branch should always be infeasible.
261 if (p == 0)
262 *p = 'c'; // no-warning
263}
Ted Kremenekb3cfd582009-04-23 17:49:43 +0000264
265// PR 4033. A symbolic 'void *' pointer can be used as the address for a
266// computed goto.
267typedef void *Opcode;
268Opcode pr_4033_getOpcode();
269void pr_4033(void) {
270next_opcode:
271 {
272 Opcode op = pr_4033_getOpcode();
273 if (op) goto *op;
274 }
275}
276
Ted Kremenek956a37d2009-05-01 23:35:18 +0000277// Test invalidating pointers-to-pointers with slightly different types. This
278// example came from a recent false positive due to a regression where the
279// branch condition was falsely reported as being uninitialized.
280void invalidate_by_ref(char **x);
281int test_invalidate_by_ref() {
282 unsigned short y;
283 invalidate_by_ref((char**) &y);
284 if (y) // no-warning
285 return 1;
286 return 0;
287}
288
Ted Kremeneked47fc62009-07-03 00:10:50 +0000289// Test for <rdar://problem/7027684>. This just tests that the CFG is
290// constructed correctly. Previously, the successor block of the entrance
291// was the block containing the merge for '?', which would trigger an
292// assertion failure.
293int rdar_7027684_aux();
294int rdar_7027684_aux_2() __attribute__((noreturn));
295void rdar_7027684(int x, int y) {
296 {}; // this empty compound statement is critical.
297 (rdar_7027684_aux() ? rdar_7027684_aux_2() : (void) 0);
298}
299
Ted Kremenek411af402009-07-06 22:23:45 +0000300// Test that we handle casts of string literals to arbitrary types.
301unsigned const char *string_literal_test1() {
302 return (const unsigned char*) "hello";
303}
304
305const float *string_literal_test2() {
306 return (const float*) "hello";
307}
308
Ted Kremenek169077d2009-07-06 23:47:19 +0000309// Test that we handle casts *from* incomplete struct types.
310extern const struct _FooAssertStruct _cmd;
311void test_cast_from_incomplete_struct_aux(volatile const void *x);
312void test_cast_from_incomplete_struct() {
313 test_cast_from_incomplete_struct_aux(&_cmd);
314}
Ted Kremeneked47fc62009-07-03 00:10:50 +0000315
Ted Kremenek59978882009-07-08 22:42:46 +0000316// Test for <rdar://problem/7034511>
317// "ValueManager::makeIntVal(uint64_t X, QualType T) should return a 'Loc'
318// when 'T' is a pointer"
319//
320// Previously this case would crash.
321void test_rdar_7034511(NSArray *y) {
322 NSObject *x;
323 for (x in y) {}
324 if (x == ((void*) 0)) {}
325}
326
Ted Kremenek8d344ae2009-07-10 21:24:45 +0000327// Handle casts of function pointers (CodeTextRegions) to arbitrary pointer
328// types. This was previously causing a crash in CastRegion.
329void handle_funcptr_voidptr_casts() {
Ted Kremenek3f9811b2009-07-10 21:11:16 +0000330 void **ptr;
331 typedef void *PVOID;
Ted Kremenek8d344ae2009-07-10 21:24:45 +0000332 typedef void *PCHAR;
Ted Kremenek3f9811b2009-07-10 21:11:16 +0000333 typedef long INT_PTR, *PINT_PTR;
334 typedef INT_PTR (*FARPROC)();
Ted Kremenek8d344ae2009-07-10 21:24:45 +0000335 FARPROC handle_funcptr_voidptr_casts_aux();
336 PVOID handle_funcptr_voidptr_casts_aux_2(PVOID volatile *x);
337 PVOID handle_funcptr_voidptr_casts_aux_3(PCHAR volatile *x);
Ted Kremenek3f9811b2009-07-10 21:11:16 +0000338
Ted Kremenek8d344ae2009-07-10 21:24:45 +0000339 ptr = (void**) handle_funcptr_voidptr_casts_aux();
340 handle_funcptr_voidptr_casts_aux_2(ptr);
341 handle_funcptr_voidptr_casts_aux_3(ptr);
Ted Kremenek3f9811b2009-07-10 21:11:16 +0000342}
343
Ted Kremenek31ef2b62009-07-10 21:43:30 +0000344// RegionStore::Retrieve previously crashed on this example. This example
345// was previously in the test file 'xfail_regionstore_wine_crash.c'.
346void testA() {
347 long x = 0;
348 char *y = (char *) &x;
349 if (!*y)
350 return;
351}
352
Ted Kremenek43d74a52009-07-11 04:38:49 +0000353// RegionStoreManager previously crashed on this example. The problem is that
354// the value bound to the field of b->grue after the call to testB_aux is
355// a symbolic region. The second '*__gruep__' involves performing a load
356// from a 'int*' that really is a 'void**'. The loaded location must be
357// implicitly converted to an integer that wraps a location. Previosly we would
358// get a crash here due to an assertion failure.
359typedef struct _BStruct { void *grue; } BStruct;
360void testB_aux(void *ptr);
361void testB(BStruct *b) {
362 {
363 int *__gruep__ = ((int *)&((b)->grue));
364 int __gruev__ = *__gruep__;
365 testB_aux(__gruep__);
366 }
367 {
368 int *__gruep__ = ((int *)&((b)->grue));
369 int __gruev__ = *__gruep__;
370 if (~0 != __gruev__) {}
371 }
372}
373
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000374void test_trivial_symbolic_comparison(int *x) {
375 int test_trivial_symbolic_comparison_aux();
376 int a = test_trivial_symbolic_comparison_aux();
377 int b = a;
378 if (a != b) {
379 int *p = 0;
380 *p = 0xDEADBEEF; // no-warning
381 }
382
383 a = a == 1;
384 b = b == 1;
385 if (a != b) {
386 int *p = 0;
387 *p = 0xDEADBEEF; // no-warning
388 }
389}
390
Ted Kremenekfde2efe2009-07-15 22:09:25 +0000391// Test for:
392// <rdar://problem/7062158> false positive null dereference due to
393// BasicStoreManager not tracking *static* globals
394//
395// This just tests the proper tracking of symbolic values for globals (both
396// static and non-static).
397//
398static int* x_rdar_7062158;
399void rdar_7062158() {
400 int *current = x_rdar_7062158;
401 if (current == x_rdar_7062158)
402 return;
403
404 int *p = 0;
405 *p = 0xDEADBEEF; // no-warning
406}
407
408int* x_rdar_7062158_2;
409void rdar_7062158_2() {
410 int *current = x_rdar_7062158_2;
411 if (current == x_rdar_7062158_2)
412 return;
413
414 int *p = 0;
415 *p = 0xDEADBEEF; // no-warning
416}
417