Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -analyze -checker-cfref --analyzer-store=basic -analyzer-constraints=basic --verify -fblocks %s && |
Ted Kremenek | c037eac | 2009-07-10 00:41:58 +0000 | [diff] [blame] | 2 | // RUN: clang-cc -analyze -checker-cfref --analyzer-store=basic-old-cast -analyzer-constraints=basic --verify -fblocks %s && |
Ted Kremenek | 921109a | 2009-07-01 23:19:52 +0000 | [diff] [blame] | 3 | // RUN: clang-cc -analyze -checker-cfref --analyzer-store=basic -analyzer-constraints=range --verify -fblocks %s && |
Ted Kremenek | c037eac | 2009-07-10 00:41:58 +0000 | [diff] [blame] | 4 | // RUN: clang-cc -analyze -checker-cfref --analyzer-store=basic-old-cast -analyzer-constraints=range --verify -fblocks %s && |
Ted Kremenek | 921109a | 2009-07-01 23:19:52 +0000 | [diff] [blame] | 5 | // 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 Kremenek | 2dabd42 | 2009-01-22 18:53:15 +0000 | [diff] [blame] | 7 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 8 | typedef struct objc_selector *SEL; |
| 9 | typedef signed char BOOL; |
| 10 | typedef int NSInteger; |
| 11 | typedef unsigned int NSUInteger; |
| 12 | typedef struct _NSZone NSZone; |
Ted Kremenek | 5997888 | 2009-07-08 22:42:46 +0000 | [diff] [blame] | 13 | @class NSInvocation, NSArray, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 14 | @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 |
| 19 | extern 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 |
| 28 | extern NSString * const NSConnectionReplyMode; |
Ted Kremenek | 693de5d | 2009-03-23 15:42:58 +0000 | [diff] [blame] | 29 | typedef float CGFloat; |
| 30 | typedef struct _NSPoint { |
| 31 | CGFloat x; |
| 32 | CGFloat y; |
| 33 | } NSPoint; |
| 34 | typedef struct _NSSize { |
| 35 | CGFloat width; |
| 36 | CGFloat height; |
| 37 | } NSSize; |
| 38 | typedef struct _NSRect { |
| 39 | NSPoint origin; |
| 40 | NSSize size; |
| 41 | } NSRect; |
Ted Kremenek | 9f67ede | 2008-10-01 05:05:46 +0000 | [diff] [blame] | 42 | |
| 43 | // Reduced test case from crash in <rdar://problem/6253157> |
Ted Kremenek | 9f67ede | 2008-10-01 05:05:46 +0000 | [diff] [blame] | 44 | @interface A @end |
| 45 | @implementation A |
| 46 | - (void)foo:(void (^)(NSObject *x))block { |
| 47 | if (!((block != ((void *)0)))) {} |
| 48 | } |
| 49 | @end |
| 50 | |
Ted Kremenek | 6dfe2f5 | 2008-10-18 22:20:20 +0000 | [diff] [blame] | 51 | // Reduced test case from crash in PR 2796; |
| 52 | // http://llvm.org/bugs/show_bug.cgi?id=2796 |
| 53 | |
| 54 | unsigned foo(unsigned x) { return __alignof__((x)) + sizeof(x); } |
Ted Kremenek | 9253b0f | 2008-10-20 23:14:31 +0000 | [diff] [blame] | 55 | |
| 56 | // Improvement to path-sensitivity involving compound assignments. |
| 57 | // Addresses false positive in <rdar://problem/6268365> |
| 58 | // |
| 59 | |
| 60 | unsigned r6268365Aux(); |
| 61 | |
| 62 | void 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 Kremenek | c13b6e2 | 2008-10-20 23:40:25 +0000 | [diff] [blame] | 71 | void 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 | |
| 78 | void 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 Kremenek | 76dba7b | 2008-11-13 05:05:34 +0000 | [diff] [blame] | 85 | // InitListExpr processing |
| 86 | |
| 87 | typedef float __m128 __attribute__((__vector_size__(16), __may_alias__)); |
| 88 | __m128 return128() { |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 89 | // This compound literal has a Vector type. We currently just |
| 90 | // return UnknownVal. |
Ted Kremenek | 76dba7b | 2008-11-13 05:05:34 +0000 | [diff] [blame] | 91 | return __extension__(__m128) { 0.0f, 0.0f, 0.0f, 0.0f }; |
| 92 | } |
| 93 | |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 94 | typedef long long __v2di __attribute__ ((__vector_size__ (16))); |
| 95 | typedef 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 Kremenek | 8322d6a | 2008-12-09 00:14:48 +0000 | [diff] [blame] | 102 | // Zero-sized VLAs. |
| 103 | void check_zero_sized_VLA(int x) { |
| 104 | if (x) |
| 105 | return; |
| 106 | |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 107 | int vla[x]; // expected-warning{{Variable-length array 'vla' has zero elements (undefined behavior)}} |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void check_uninit_sized_VLA() { |
| 111 | int x; |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 112 | int vla[x]; // expected-warning{{Variable-length array 'vla' garbage value for array size}} |
Ted Kremenek | 8322d6a | 2008-12-09 00:14:48 +0000 | [diff] [blame] | 113 | } |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 114 | |
Ted Kremenek | 55f7bcb | 2008-12-15 18:51:00 +0000 | [diff] [blame] | 115 | // sizeof(void) |
| 116 | // - Tests a regression reported in PR 3211: http://llvm.org/bugs/show_bug.cgi?id=3211 |
| 117 | void 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 Kremenek | d76d47e | 2009-01-27 18:29:03 +0000 | [diff] [blame] | 140 | // PR 3422 |
| 141 | void pr3422_helper(char *p); |
| 142 | void pr3422() { |
| 143 | char buf[100]; |
| 144 | char *q = &buf[10]; |
| 145 | pr3422_helper(&q[1]); |
| 146 | } |
| 147 | |
Ted Kremenek | a3d1eb8 | 2009-02-14 05:55:08 +0000 | [diff] [blame] | 148 | // PR 3543 (handle empty statement expressions) |
| 149 | int pr_3543(void) { |
| 150 | ({}); |
| 151 | } |
| 152 | |
Ted Kremenek | 265a305 | 2009-02-24 02:23:11 +0000 | [diff] [blame] | 153 | // <rdar://problem/6611677> |
| 154 | // This test case test the use of a vector type within an array subscript |
| 155 | // expression. |
| 156 | typedef long long __a64vector __attribute__((__vector_size__(8))); |
| 157 | typedef long long __a128vector __attribute__((__vector_size__(16))); |
| 158 | static inline __a64vector __attribute__((__always_inline__, __nodebug__)) |
| 159 | my_test_mm_movepi64_pi64(__a128vector a) { |
| 160 | return (__a64vector)a[0]; |
| 161 | } |
| 162 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 163 | // 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 Kremenek | a3d1eb8 | 2009-02-14 05:55:08 +0000 | [diff] [blame] | 182 | |
Ted Kremenek | 7de20fe | 2009-03-11 02:29:48 +0000 | [diff] [blame] | 183 | // PR 3770 |
| 184 | char 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 Kremenek | 344d4c8 | 2009-03-11 18:17:16 +0000 | [diff] [blame] | 196 | // PR 3772 |
Ted Kremenek | fa6228d | 2009-03-11 02:52:39 +0000 | [diff] [blame] | 197 | // - We just want to test that this doesn't crash the analyzer. |
| 198 | typedef struct st ST; |
| 199 | struct st { char *name; }; |
| 200 | extern ST *Cur_Pu; |
| 201 | |
Ted Kremenek | 344d4c8 | 2009-03-11 18:17:16 +0000 | [diff] [blame] | 202 | void pr3772(void) |
Ted Kremenek | fa6228d | 2009-03-11 02:52:39 +0000 | [diff] [blame] | 203 | { |
| 204 | static ST *last_Cur_Pu; |
| 205 | if (last_Cur_Pu == Cur_Pu) { |
| 206 | return; |
| 207 | } |
| 208 | } |
| 209 | |
Ted Kremenek | 344d4c8 | 2009-03-11 18:17:16 +0000 | [diff] [blame] | 210 | // PR 3780 - This tests that StmtIterator isn't broken for VLAs in DeclGroups. |
| 211 | void pr3780(int sz) { typedef double MAT[sz][sz]; } |
Ted Kremenek | fa6228d | 2009-03-11 02:52:39 +0000 | [diff] [blame] | 212 | |
Ted Kremenek | ec099f1 | 2009-03-18 22:10:22 +0000 | [diff] [blame] | 213 | // <rdar://problem/6695527> - Test that we don't symbolicate doubles before |
| 214 | // we are ready to do something with them. |
| 215 | int rdar6695527(double x) { |
| 216 | if (!x) { return 0; } |
| 217 | return 1; |
| 218 | } |
Ted Kremenek | 693de5d | 2009-03-23 15:42:58 +0000 | [diff] [blame] | 219 | |
| 220 | // <rdar://problem/6708148> - Test that we properly invalidate structs |
| 221 | // passed-by-reference to a function. |
| 222 | void pr6708148_invalidate(NSRect *x); |
| 223 | void pr6708148_use(NSRect x); |
| 224 | void pr6708148_test(void) { |
| 225 | NSRect x; |
| 226 | pr6708148_invalidate(&x); |
| 227 | pr6708148_use(x); // no-warning |
| 228 | } |
| 229 | |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 230 | // Handle both kinds of noreturn attributes for pruning paths. |
| 231 | void rdar_6777003_noret() __attribute__((noreturn)); |
| 232 | void rdar_6777003_analyzer_noret() __attribute__((analyzer_noreturn)); |
| 233 | |
| 234 | void 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 Kremenek | af48fdd | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 250 | // 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 Kremenek | af48fdd | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 254 | void 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 Kremenek | b3cfd58 | 2009-04-23 17:49:43 +0000 | [diff] [blame] | 264 | |
| 265 | // PR 4033. A symbolic 'void *' pointer can be used as the address for a |
| 266 | // computed goto. |
| 267 | typedef void *Opcode; |
| 268 | Opcode pr_4033_getOpcode(); |
| 269 | void pr_4033(void) { |
| 270 | next_opcode: |
| 271 | { |
| 272 | Opcode op = pr_4033_getOpcode(); |
| 273 | if (op) goto *op; |
| 274 | } |
| 275 | } |
| 276 | |
Ted Kremenek | 956a37d | 2009-05-01 23:35:18 +0000 | [diff] [blame] | 277 | // 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. |
| 280 | void invalidate_by_ref(char **x); |
| 281 | int 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 Kremenek | ed47fc6 | 2009-07-03 00:10:50 +0000 | [diff] [blame] | 289 | // 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. |
| 293 | int rdar_7027684_aux(); |
| 294 | int rdar_7027684_aux_2() __attribute__((noreturn)); |
| 295 | void 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 Kremenek | 411af40 | 2009-07-06 22:23:45 +0000 | [diff] [blame] | 300 | // Test that we handle casts of string literals to arbitrary types. |
| 301 | unsigned const char *string_literal_test1() { |
| 302 | return (const unsigned char*) "hello"; |
| 303 | } |
| 304 | |
| 305 | const float *string_literal_test2() { |
| 306 | return (const float*) "hello"; |
| 307 | } |
| 308 | |
Ted Kremenek | 169077d | 2009-07-06 23:47:19 +0000 | [diff] [blame] | 309 | // Test that we handle casts *from* incomplete struct types. |
| 310 | extern const struct _FooAssertStruct _cmd; |
| 311 | void test_cast_from_incomplete_struct_aux(volatile const void *x); |
| 312 | void test_cast_from_incomplete_struct() { |
| 313 | test_cast_from_incomplete_struct_aux(&_cmd); |
| 314 | } |
Ted Kremenek | ed47fc6 | 2009-07-03 00:10:50 +0000 | [diff] [blame] | 315 | |
Ted Kremenek | 5997888 | 2009-07-08 22:42:46 +0000 | [diff] [blame] | 316 | // 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. |
| 321 | void test_rdar_7034511(NSArray *y) { |
| 322 | NSObject *x; |
| 323 | for (x in y) {} |
| 324 | if (x == ((void*) 0)) {} |
| 325 | } |
| 326 | |
Ted Kremenek | 8d344ae | 2009-07-10 21:24:45 +0000 | [diff] [blame] | 327 | // Handle casts of function pointers (CodeTextRegions) to arbitrary pointer |
| 328 | // types. This was previously causing a crash in CastRegion. |
| 329 | void handle_funcptr_voidptr_casts() { |
Ted Kremenek | 3f9811b | 2009-07-10 21:11:16 +0000 | [diff] [blame] | 330 | void **ptr; |
| 331 | typedef void *PVOID; |
Ted Kremenek | 8d344ae | 2009-07-10 21:24:45 +0000 | [diff] [blame] | 332 | typedef void *PCHAR; |
Ted Kremenek | 3f9811b | 2009-07-10 21:11:16 +0000 | [diff] [blame] | 333 | typedef long INT_PTR, *PINT_PTR; |
| 334 | typedef INT_PTR (*FARPROC)(); |
Ted Kremenek | 8d344ae | 2009-07-10 21:24:45 +0000 | [diff] [blame] | 335 | 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 Kremenek | 3f9811b | 2009-07-10 21:11:16 +0000 | [diff] [blame] | 338 | |
Ted Kremenek | 8d344ae | 2009-07-10 21:24:45 +0000 | [diff] [blame] | 339 | ptr = (void**) handle_funcptr_voidptr_casts_aux(); |
| 340 | handle_funcptr_voidptr_casts_aux_2(ptr); |
| 341 | handle_funcptr_voidptr_casts_aux_3(ptr); |
Ted Kremenek | 3f9811b | 2009-07-10 21:11:16 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Ted Kremenek | 31ef2b6 | 2009-07-10 21:43:30 +0000 | [diff] [blame] | 344 | // RegionStore::Retrieve previously crashed on this example. This example |
| 345 | // was previously in the test file 'xfail_regionstore_wine_crash.c'. |
| 346 | void testA() { |
| 347 | long x = 0; |
| 348 | char *y = (char *) &x; |
| 349 | if (!*y) |
| 350 | return; |
| 351 | } |
| 352 | |
Ted Kremenek | 43d74a5 | 2009-07-11 04:38:49 +0000 | [diff] [blame^] | 353 | // 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. |
| 359 | typedef struct _BStruct { void *grue; } BStruct; |
| 360 | void testB_aux(void *ptr); |
| 361 | void 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 | |