Ted Kremenek | e0a5807 | 2009-09-18 22:37:37 +0000 | [diff] [blame] | 1 | // NOTE: Use '-fobjc-gc' to test the analysis being run twice, and multiple reports are not issued. |
Daniel Dunbar | 4fcfde4 | 2009-11-08 01:45:36 +0000 | [diff] [blame] | 2 | // RUN: clang-cc -analyze -checker-cfref --analyzer-store=basic -fobjc-gc -analyzer-constraints=basic --verify -fblocks %s |
| 3 | // RUN: clang-cc -analyze -checker-cfref --analyzer-store=basic -analyzer-constraints=range --verify -fblocks %s |
| 4 | // RUN: clang-cc -analyze -checker-cfref --analyzer-store=region -analyzer-constraints=basic --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=range --verify -fblocks %s |
Ted Kremenek | 2dabd42 | 2009-01-22 18:53:15 +0000 | [diff] [blame] | 6 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 7 | typedef struct objc_ivar *Ivar; |
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 | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 14 | @protocol NSObject |
| 15 | - (BOOL)isEqual:(id)object; |
| 16 | - (id)autorelease; |
| 17 | @end |
| 18 | @protocol NSCopying |
| 19 | - (id)copyWithZone:(NSZone *)zone; |
| 20 | @end |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 21 | @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 22 | @protocol NSCoding |
| 23 | - (void)encodeWithCoder:(NSCoder *)aCoder; |
| 24 | @end |
| 25 | @interface NSObject <NSObject> {} |
| 26 | - (id)init; |
| 27 | + (id)allocWithZone:(NSZone *)zone; |
| 28 | @end |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 29 | extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone); |
| 30 | @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> |
| 31 | - (NSUInteger)length; |
| 32 | + (id)stringWithUTF8String:(const char *)nullTerminatedCString; |
| 33 | @end extern NSString * const NSBundleDidLoadNotification; |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 34 | @interface NSValue : NSObject <NSCopying, NSCoding> |
| 35 | - (void)getValue:(void *)value; |
| 36 | @end |
| 37 | @interface NSNumber : NSValue |
| 38 | - (char)charValue; |
| 39 | - (id)initWithBool:(BOOL)value; |
| 40 | @end |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 41 | @interface NSAssertionHandler : NSObject {} |
| 42 | + (NSAssertionHandler *)currentHandler; |
| 43 | - (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format,...; |
| 44 | @end |
| 45 | extern NSString * const NSConnectionReplyMode; |
Ted Kremenek | 693de5d | 2009-03-23 15:42:58 +0000 | [diff] [blame] | 46 | typedef float CGFloat; |
| 47 | typedef struct _NSPoint { |
| 48 | CGFloat x; |
| 49 | CGFloat y; |
| 50 | } NSPoint; |
| 51 | typedef struct _NSSize { |
| 52 | CGFloat width; |
| 53 | CGFloat height; |
| 54 | } NSSize; |
| 55 | typedef struct _NSRect { |
| 56 | NSPoint origin; |
| 57 | NSSize size; |
| 58 | } NSRect; |
Ted Kremenek | 9f67ede | 2008-10-01 05:05:46 +0000 | [diff] [blame] | 59 | |
| 60 | // Reduced test case from crash in <rdar://problem/6253157> |
Ted Kremenek | 9f67ede | 2008-10-01 05:05:46 +0000 | [diff] [blame] | 61 | @interface A @end |
| 62 | @implementation A |
| 63 | - (void)foo:(void (^)(NSObject *x))block { |
| 64 | if (!((block != ((void *)0)))) {} |
| 65 | } |
| 66 | @end |
| 67 | |
Ted Kremenek | 6dfe2f5 | 2008-10-18 22:20:20 +0000 | [diff] [blame] | 68 | // Reduced test case from crash in PR 2796; |
| 69 | // http://llvm.org/bugs/show_bug.cgi?id=2796 |
| 70 | |
| 71 | unsigned foo(unsigned x) { return __alignof__((x)) + sizeof(x); } |
Ted Kremenek | 9253b0f | 2008-10-20 23:14:31 +0000 | [diff] [blame] | 72 | |
| 73 | // Improvement to path-sensitivity involving compound assignments. |
| 74 | // Addresses false positive in <rdar://problem/6268365> |
| 75 | // |
| 76 | |
| 77 | unsigned r6268365Aux(); |
| 78 | |
| 79 | void r6268365() { |
| 80 | unsigned x = 0; |
| 81 | x &= r6268365Aux(); |
| 82 | unsigned j = 0; |
| 83 | |
| 84 | if (x == 0) ++j; |
| 85 | if (x == 0) x = x / j; // no-warning |
| 86 | } |
| 87 | |
Ted Kremenek | c13b6e2 | 2008-10-20 23:40:25 +0000 | [diff] [blame] | 88 | void divzeroassume(unsigned x, unsigned j) { |
| 89 | x /= j; |
| 90 | if (j == 0) x /= 0; // no-warning |
| 91 | if (j == 0) x /= j; // no-warning |
| 92 | if (j == 0) x = x / 0; // no-warning |
| 93 | } |
| 94 | |
| 95 | void divzeroassumeB(unsigned x, unsigned j) { |
| 96 | x = x / j; |
| 97 | if (j == 0) x /= 0; // no-warning |
| 98 | if (j == 0) x /= j; // no-warning |
| 99 | if (j == 0) x = x / 0; // no-warning |
| 100 | } |
| 101 | |
Ted Kremenek | 76dba7b | 2008-11-13 05:05:34 +0000 | [diff] [blame] | 102 | // InitListExpr processing |
| 103 | |
| 104 | typedef float __m128 __attribute__((__vector_size__(16), __may_alias__)); |
| 105 | __m128 return128() { |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 106 | // This compound literal has a Vector type. We currently just |
| 107 | // return UnknownVal. |
Ted Kremenek | 76dba7b | 2008-11-13 05:05:34 +0000 | [diff] [blame] | 108 | return __extension__(__m128) { 0.0f, 0.0f, 0.0f, 0.0f }; |
| 109 | } |
| 110 | |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 111 | typedef long long __v2di __attribute__ ((__vector_size__ (16))); |
| 112 | typedef long long __m128i __attribute__ ((__vector_size__ (16), __may_alias__)); |
| 113 | __m128i vec128i(long long __q1, long long __q0) { |
| 114 | // This compound literal returns true for both isVectorType() and |
| 115 | // isIntegerType(). |
| 116 | return __extension__ (__m128i)(__v2di){ __q0, __q1 }; |
| 117 | } |
| 118 | |
Ted Kremenek | 8322d6a | 2008-12-09 00:14:48 +0000 | [diff] [blame] | 119 | // Zero-sized VLAs. |
| 120 | void check_zero_sized_VLA(int x) { |
| 121 | if (x) |
| 122 | return; |
| 123 | |
Ted Kremenek | ae78447 | 2009-11-05 08:30:12 +0000 | [diff] [blame] | 124 | int vla[x]; // expected-warning{{Declared variable-length array (VLA) has zero size}} |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void check_uninit_sized_VLA() { |
| 128 | int x; |
Ted Kremenek | ae78447 | 2009-11-05 08:30:12 +0000 | [diff] [blame] | 129 | int vla[x]; // expected-warning{{Declared variable-length array (VLA) uses a garbage value as its size}} |
Ted Kremenek | 8322d6a | 2008-12-09 00:14:48 +0000 | [diff] [blame] | 130 | } |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | 55f7bcb | 2008-12-15 18:51:00 +0000 | [diff] [blame] | 132 | // sizeof(void) |
| 133 | // - Tests a regression reported in PR 3211: http://llvm.org/bugs/show_bug.cgi?id=3211 |
| 134 | void handle_sizeof_void(unsigned flag) { |
| 135 | int* p = 0; |
| 136 | |
| 137 | if (flag) { |
| 138 | if (sizeof(void) == 1) |
| 139 | return; |
| 140 | // Infeasible. |
| 141 | *p = 1; // no-warning |
| 142 | } |
| 143 | |
| 144 | void* q; |
| 145 | |
| 146 | if (!flag) { |
| 147 | if (sizeof(*q) == 1) |
| 148 | return; |
| 149 | // Infeasibe. |
| 150 | *p = 1; // no-warning |
| 151 | } |
| 152 | |
| 153 | // Infeasible. |
| 154 | *p = 1; // no-warning |
| 155 | } |
| 156 | |
Ted Kremenek | 3f214b3 | 2009-10-29 05:33:39 +0000 | [diff] [blame] | 157 | // check deference of undefined values |
| 158 | void check_deref_undef(void) { |
| 159 | int *p; |
| 160 | *p = 0xDEADBEEF; // expected-warning{{Dereference of undefined pointer value}} |
| 161 | } |
| 162 | |
Ted Kremenek | d76d47e | 2009-01-27 18:29:03 +0000 | [diff] [blame] | 163 | // PR 3422 |
| 164 | void pr3422_helper(char *p); |
| 165 | void pr3422() { |
| 166 | char buf[100]; |
| 167 | char *q = &buf[10]; |
| 168 | pr3422_helper(&q[1]); |
| 169 | } |
| 170 | |
Ted Kremenek | a3d1eb8 | 2009-02-14 05:55:08 +0000 | [diff] [blame] | 171 | // PR 3543 (handle empty statement expressions) |
Mike Stump | 006105d | 2009-07-21 18:59:16 +0000 | [diff] [blame] | 172 | void pr_3543(void) { |
Ted Kremenek | a3d1eb8 | 2009-02-14 05:55:08 +0000 | [diff] [blame] | 173 | ({}); |
| 174 | } |
| 175 | |
Ted Kremenek | 265a305 | 2009-02-24 02:23:11 +0000 | [diff] [blame] | 176 | // <rdar://problem/6611677> |
| 177 | // This test case test the use of a vector type within an array subscript |
| 178 | // expression. |
| 179 | typedef long long __a64vector __attribute__((__vector_size__(8))); |
| 180 | typedef long long __a128vector __attribute__((__vector_size__(16))); |
| 181 | static inline __a64vector __attribute__((__always_inline__, __nodebug__)) |
| 182 | my_test_mm_movepi64_pi64(__a128vector a) { |
| 183 | return (__a64vector)a[0]; |
| 184 | } |
| 185 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 186 | // Test basic tracking of ivars associated with 'self'. |
| 187 | @interface SelfIvarTest : NSObject { |
| 188 | int flag; |
| 189 | } |
| 190 | - (void)test_self_tracking; |
| 191 | @end |
| 192 | |
| 193 | @implementation SelfIvarTest |
| 194 | - (void)test_self_tracking { |
| 195 | char *p = 0; |
| 196 | char c; |
| 197 | |
| 198 | if (flag) |
| 199 | p = "hello"; |
| 200 | |
| 201 | if (flag) |
| 202 | c = *p; // no-warning |
| 203 | } |
| 204 | @end |
Ted Kremenek | a3d1eb8 | 2009-02-14 05:55:08 +0000 | [diff] [blame] | 205 | |
Ted Kremenek | 7de20fe | 2009-03-11 02:29:48 +0000 | [diff] [blame] | 206 | // PR 3770 |
| 207 | char pr3770(int x) { |
| 208 | int y = x & 0x2; |
| 209 | char *p = 0; |
| 210 | if (y == 1) |
| 211 | p = "hello"; |
| 212 | |
| 213 | if (y == 1) |
| 214 | return p[0]; // no-warning |
| 215 | |
| 216 | return 'a'; |
| 217 | } |
| 218 | |
Ted Kremenek | 344d4c8 | 2009-03-11 18:17:16 +0000 | [diff] [blame] | 219 | // PR 3772 |
Ted Kremenek | fa6228d | 2009-03-11 02:52:39 +0000 | [diff] [blame] | 220 | // - We just want to test that this doesn't crash the analyzer. |
| 221 | typedef struct st ST; |
| 222 | struct st { char *name; }; |
| 223 | extern ST *Cur_Pu; |
| 224 | |
Ted Kremenek | 344d4c8 | 2009-03-11 18:17:16 +0000 | [diff] [blame] | 225 | void pr3772(void) |
Ted Kremenek | fa6228d | 2009-03-11 02:52:39 +0000 | [diff] [blame] | 226 | { |
| 227 | static ST *last_Cur_Pu; |
| 228 | if (last_Cur_Pu == Cur_Pu) { |
| 229 | return; |
| 230 | } |
| 231 | } |
| 232 | |
Ted Kremenek | 344d4c8 | 2009-03-11 18:17:16 +0000 | [diff] [blame] | 233 | // PR 3780 - This tests that StmtIterator isn't broken for VLAs in DeclGroups. |
| 234 | void pr3780(int sz) { typedef double MAT[sz][sz]; } |
Ted Kremenek | fa6228d | 2009-03-11 02:52:39 +0000 | [diff] [blame] | 235 | |
Ted Kremenek | ec099f1 | 2009-03-18 22:10:22 +0000 | [diff] [blame] | 236 | // <rdar://problem/6695527> - Test that we don't symbolicate doubles before |
| 237 | // we are ready to do something with them. |
| 238 | int rdar6695527(double x) { |
| 239 | if (!x) { return 0; } |
| 240 | return 1; |
| 241 | } |
Ted Kremenek | 693de5d | 2009-03-23 15:42:58 +0000 | [diff] [blame] | 242 | |
| 243 | // <rdar://problem/6708148> - Test that we properly invalidate structs |
| 244 | // passed-by-reference to a function. |
| 245 | void pr6708148_invalidate(NSRect *x); |
| 246 | void pr6708148_use(NSRect x); |
| 247 | void pr6708148_test(void) { |
| 248 | NSRect x; |
| 249 | pr6708148_invalidate(&x); |
| 250 | pr6708148_use(x); // no-warning |
| 251 | } |
| 252 | |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 253 | // Handle both kinds of noreturn attributes for pruning paths. |
| 254 | void rdar_6777003_noret() __attribute__((noreturn)); |
| 255 | void rdar_6777003_analyzer_noret() __attribute__((analyzer_noreturn)); |
| 256 | |
| 257 | void rdar_6777003(int x) { |
| 258 | int *p = 0; |
| 259 | |
| 260 | if (x == 1) { |
| 261 | rdar_6777003_noret(); |
| 262 | *p = 1; // no-warning; |
| 263 | } |
| 264 | |
| 265 | if (x == 2) { |
| 266 | rdar_6777003_analyzer_noret(); |
| 267 | *p = 1; // no-warning; |
| 268 | } |
| 269 | |
| 270 | *p = 1; // expected-warning{{Dereference of null pointer}} |
| 271 | } |
| 272 | |
Ted Kremenek | af48fdd | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 273 | // For pointer arithmetic, --/++ should be treated as preserving non-nullness, |
| 274 | // regardless of how well the underlying StoreManager reasons about pointer |
| 275 | // arithmetic. |
| 276 | // <rdar://problem/6777209> |
Ted Kremenek | af48fdd | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 277 | void rdar_6777209(char *p) { |
| 278 | if (p == 0) |
| 279 | return; |
| 280 | |
| 281 | ++p; |
| 282 | |
| 283 | // This branch should always be infeasible. |
| 284 | if (p == 0) |
| 285 | *p = 'c'; // no-warning |
| 286 | } |
Ted Kremenek | b3cfd58 | 2009-04-23 17:49:43 +0000 | [diff] [blame] | 287 | |
| 288 | // PR 4033. A symbolic 'void *' pointer can be used as the address for a |
| 289 | // computed goto. |
| 290 | typedef void *Opcode; |
| 291 | Opcode pr_4033_getOpcode(); |
| 292 | void pr_4033(void) { |
| 293 | next_opcode: |
| 294 | { |
| 295 | Opcode op = pr_4033_getOpcode(); |
| 296 | if (op) goto *op; |
| 297 | } |
| 298 | } |
| 299 | |
Ted Kremenek | 956a37d | 2009-05-01 23:35:18 +0000 | [diff] [blame] | 300 | // Test invalidating pointers-to-pointers with slightly different types. This |
| 301 | // example came from a recent false positive due to a regression where the |
| 302 | // branch condition was falsely reported as being uninitialized. |
| 303 | void invalidate_by_ref(char **x); |
| 304 | int test_invalidate_by_ref() { |
| 305 | unsigned short y; |
| 306 | invalidate_by_ref((char**) &y); |
| 307 | if (y) // no-warning |
| 308 | return 1; |
| 309 | return 0; |
| 310 | } |
| 311 | |
Ted Kremenek | ed47fc6 | 2009-07-03 00:10:50 +0000 | [diff] [blame] | 312 | // Test for <rdar://problem/7027684>. This just tests that the CFG is |
| 313 | // constructed correctly. Previously, the successor block of the entrance |
| 314 | // was the block containing the merge for '?', which would trigger an |
| 315 | // assertion failure. |
| 316 | int rdar_7027684_aux(); |
| 317 | int rdar_7027684_aux_2() __attribute__((noreturn)); |
| 318 | void rdar_7027684(int x, int y) { |
| 319 | {}; // this empty compound statement is critical. |
| 320 | (rdar_7027684_aux() ? rdar_7027684_aux_2() : (void) 0); |
| 321 | } |
| 322 | |
Ted Kremenek | 411af40 | 2009-07-06 22:23:45 +0000 | [diff] [blame] | 323 | // Test that we handle casts of string literals to arbitrary types. |
| 324 | unsigned const char *string_literal_test1() { |
| 325 | return (const unsigned char*) "hello"; |
| 326 | } |
| 327 | |
| 328 | const float *string_literal_test2() { |
| 329 | return (const float*) "hello"; |
| 330 | } |
| 331 | |
Ted Kremenek | 169077d | 2009-07-06 23:47:19 +0000 | [diff] [blame] | 332 | // Test that we handle casts *from* incomplete struct types. |
| 333 | extern const struct _FooAssertStruct _cmd; |
| 334 | void test_cast_from_incomplete_struct_aux(volatile const void *x); |
| 335 | void test_cast_from_incomplete_struct() { |
| 336 | test_cast_from_incomplete_struct_aux(&_cmd); |
| 337 | } |
Ted Kremenek | ed47fc6 | 2009-07-03 00:10:50 +0000 | [diff] [blame] | 338 | |
Ted Kremenek | 5997888 | 2009-07-08 22:42:46 +0000 | [diff] [blame] | 339 | // Test for <rdar://problem/7034511> |
| 340 | // "ValueManager::makeIntVal(uint64_t X, QualType T) should return a 'Loc' |
| 341 | // when 'T' is a pointer" |
| 342 | // |
| 343 | // Previously this case would crash. |
| 344 | void test_rdar_7034511(NSArray *y) { |
| 345 | NSObject *x; |
| 346 | for (x in y) {} |
| 347 | if (x == ((void*) 0)) {} |
| 348 | } |
| 349 | |
Ted Kremenek | 8d344ae | 2009-07-10 21:24:45 +0000 | [diff] [blame] | 350 | // Handle casts of function pointers (CodeTextRegions) to arbitrary pointer |
| 351 | // types. This was previously causing a crash in CastRegion. |
| 352 | void handle_funcptr_voidptr_casts() { |
Ted Kremenek | 3f9811b | 2009-07-10 21:11:16 +0000 | [diff] [blame] | 353 | void **ptr; |
| 354 | typedef void *PVOID; |
Ted Kremenek | 8d344ae | 2009-07-10 21:24:45 +0000 | [diff] [blame] | 355 | typedef void *PCHAR; |
Ted Kremenek | 3f9811b | 2009-07-10 21:11:16 +0000 | [diff] [blame] | 356 | typedef long INT_PTR, *PINT_PTR; |
| 357 | typedef INT_PTR (*FARPROC)(); |
Ted Kremenek | 8d344ae | 2009-07-10 21:24:45 +0000 | [diff] [blame] | 358 | FARPROC handle_funcptr_voidptr_casts_aux(); |
| 359 | PVOID handle_funcptr_voidptr_casts_aux_2(PVOID volatile *x); |
| 360 | PVOID handle_funcptr_voidptr_casts_aux_3(PCHAR volatile *x); |
Ted Kremenek | 3f9811b | 2009-07-10 21:11:16 +0000 | [diff] [blame] | 361 | |
Ted Kremenek | 8d344ae | 2009-07-10 21:24:45 +0000 | [diff] [blame] | 362 | ptr = (void**) handle_funcptr_voidptr_casts_aux(); |
| 363 | handle_funcptr_voidptr_casts_aux_2(ptr); |
| 364 | handle_funcptr_voidptr_casts_aux_3(ptr); |
Ted Kremenek | 3f9811b | 2009-07-10 21:11:16 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Ted Kremenek | 31ef2b6 | 2009-07-10 21:43:30 +0000 | [diff] [blame] | 367 | // RegionStore::Retrieve previously crashed on this example. This example |
| 368 | // was previously in the test file 'xfail_regionstore_wine_crash.c'. |
| 369 | void testA() { |
| 370 | long x = 0; |
| 371 | char *y = (char *) &x; |
| 372 | if (!*y) |
| 373 | return; |
| 374 | } |
| 375 | |
Ted Kremenek | 43d74a5 | 2009-07-11 04:38:49 +0000 | [diff] [blame] | 376 | // RegionStoreManager previously crashed on this example. The problem is that |
| 377 | // the value bound to the field of b->grue after the call to testB_aux is |
| 378 | // a symbolic region. The second '*__gruep__' involves performing a load |
| 379 | // from a 'int*' that really is a 'void**'. The loaded location must be |
| 380 | // implicitly converted to an integer that wraps a location. Previosly we would |
| 381 | // get a crash here due to an assertion failure. |
| 382 | typedef struct _BStruct { void *grue; } BStruct; |
| 383 | void testB_aux(void *ptr); |
| 384 | void testB(BStruct *b) { |
| 385 | { |
| 386 | int *__gruep__ = ((int *)&((b)->grue)); |
| 387 | int __gruev__ = *__gruep__; |
| 388 | testB_aux(__gruep__); |
| 389 | } |
| 390 | { |
| 391 | int *__gruep__ = ((int *)&((b)->grue)); |
| 392 | int __gruev__ = *__gruep__; |
| 393 | if (~0 != __gruev__) {} |
| 394 | } |
| 395 | } |
| 396 | |
Ted Kremenek | 54ca9b1 | 2009-07-13 21:55:12 +0000 | [diff] [blame] | 397 | void test_trivial_symbolic_comparison(int *x) { |
| 398 | int test_trivial_symbolic_comparison_aux(); |
| 399 | int a = test_trivial_symbolic_comparison_aux(); |
| 400 | int b = a; |
| 401 | if (a != b) { |
| 402 | int *p = 0; |
| 403 | *p = 0xDEADBEEF; // no-warning |
| 404 | } |
| 405 | |
| 406 | a = a == 1; |
| 407 | b = b == 1; |
| 408 | if (a != b) { |
| 409 | int *p = 0; |
| 410 | *p = 0xDEADBEEF; // no-warning |
| 411 | } |
| 412 | } |
| 413 | |
Ted Kremenek | fde2efe | 2009-07-15 22:09:25 +0000 | [diff] [blame] | 414 | // Test for: |
| 415 | // <rdar://problem/7062158> false positive null dereference due to |
| 416 | // BasicStoreManager not tracking *static* globals |
| 417 | // |
| 418 | // This just tests the proper tracking of symbolic values for globals (both |
| 419 | // static and non-static). |
| 420 | // |
| 421 | static int* x_rdar_7062158; |
| 422 | void rdar_7062158() { |
| 423 | int *current = x_rdar_7062158; |
| 424 | if (current == x_rdar_7062158) |
| 425 | return; |
| 426 | |
| 427 | int *p = 0; |
| 428 | *p = 0xDEADBEEF; // no-warning |
| 429 | } |
| 430 | |
| 431 | int* x_rdar_7062158_2; |
| 432 | void rdar_7062158_2() { |
| 433 | int *current = x_rdar_7062158_2; |
| 434 | if (current == x_rdar_7062158_2) |
| 435 | return; |
| 436 | |
| 437 | int *p = 0; |
| 438 | *p = 0xDEADBEEF; // no-warning |
| 439 | } |
| 440 | |
Ted Kremenek | 4653739 | 2009-07-16 01:33:37 +0000 | [diff] [blame] | 441 | // This test reproduces a case for a crash when analyzing ClamAV using |
| 442 | // RegionStoreManager (the crash doesn't exhibit in BasicStoreManager because |
| 443 | // it isn't doing anything smart about arrays). The problem is that on the |
| 444 | // second line, 'p = &p[i]', p is assigned an ElementRegion whose index |
| 445 | // is a 16-bit integer. On the third line, a new ElementRegion is created |
| 446 | // based on the previous region, but there the region uses a 32-bit integer, |
| 447 | // resulting in a clash of values (an assertion failure at best). We resolve |
| 448 | // this problem by implicitly converting index values to 'int' when the |
| 449 | // ElementRegion is created. |
| 450 | unsigned char test_array_index_bitwidth(const unsigned char *p) { |
| 451 | unsigned short i = 0; |
| 452 | for (i = 0; i < 2; i++) p = &p[i]; |
| 453 | return p[i+1]; |
| 454 | } |
| 455 | |
Ted Kremenek | 63b9cfe | 2009-07-18 06:27:51 +0000 | [diff] [blame] | 456 | // This case tests that CastRegion handles casts involving BlockPointerTypes. |
| 457 | // It should not crash. |
| 458 | void test_block_cast() { |
| 459 | id test_block_cast_aux(); |
| 460 | (void (^)(void *))test_block_cast_aux(); // expected-warning{{expression result unused}} |
| 461 | } |
| 462 | |
Ted Kremenek | 293769a | 2009-07-20 21:00:55 +0000 | [diff] [blame] | 463 | // Test comparison of 'id' instance variable to a null void* constant after |
| 464 | // performing an OSAtomicCompareAndSwap32Barrier. |
| 465 | // This previously was a crash in RegionStoreManager. |
| 466 | @interface TestIdNull { |
| 467 | id x; |
| 468 | } |
| 469 | -(int)foo; |
| 470 | @end |
| 471 | @implementation TestIdNull |
| 472 | -(int)foo { |
| 473 | OSAtomicCompareAndSwap32Barrier(0, (signed)2, (signed*)&x); |
| 474 | if (x == (void*) 0) { return 0; } |
| 475 | return 1; |
| 476 | } |
| 477 | @end |
| 478 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 479 | // PR 4594 - This was a crash when handling casts in SimpleSValuator. |
| 480 | void PR4594() { |
| 481 | char *buf[1]; |
| 482 | char **foo = buf; |
| 483 | *foo = "test"; |
| 484 | } |
Ted Kremenek | 0b331e3 | 2009-07-22 04:23:20 +0000 | [diff] [blame] | 485 | |
| 486 | // Test invalidation logic where an integer is casted to an array with a |
| 487 | // different sign and then invalidated. |
| 488 | void test_invalidate_cast_int() { |
| 489 | void test_invalidate_cast_int_aux(unsigned *i); |
| 490 | signed i; |
| 491 | test_invalidate_cast_int_aux((unsigned*) &i); |
| 492 | if (i < 0) |
| 493 | return; |
| 494 | } |
| 495 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 496 | // Reduced from a crash involving the cast of an Objective-C symbolic region to |
| 497 | // 'char *' |
| 498 | static NSNumber *test_ivar_offset(id self, SEL _cmd, Ivar inIvar) { |
| 499 | return [[[NSNumber allocWithZone:((void*)0)] initWithBool:*(_Bool *)((char *)self + ivar_getOffset(inIvar))] autorelease]; |
| 500 | } |
Ted Kremenek | 9a108eb | 2009-08-02 04:12:53 +0000 | [diff] [blame] | 501 | |
| 502 | // Reduced from a crash in StoreManager::CastRegion involving a divide-by-zero. |
| 503 | // This resulted from not properly handling region casts to 'const void*'. |
| 504 | void test_cast_const_voidptr() { |
| 505 | char x[10]; |
| 506 | char *p = &x[1]; |
| 507 | const void* q = p; |
| 508 | } |
Ted Kremenek | 968f0a6 | 2009-08-03 21:41:46 +0000 | [diff] [blame] | 509 | |
| 510 | // Reduced from a crash when analyzing Wine. This test handles loads from |
| 511 | // function addresses. |
| 512 | typedef long (*FARPROC)(); |
| 513 | FARPROC test_load_func(FARPROC origfun) { |
| 514 | if (!*(unsigned char*) origfun) |
| 515 | return origfun; |
| 516 | return 0; |
| 517 | } |
Ted Kremenek | 48775d5 | 2009-08-03 23:22:53 +0000 | [diff] [blame] | 518 | |
| 519 | // Test passing-by-value an initialized struct variable. |
| 520 | struct test_pass_val { |
| 521 | int x; |
| 522 | int y; |
| 523 | }; |
| 524 | void test_pass_val_aux(struct test_pass_val s); |
| 525 | void test_pass_val() { |
| 526 | struct test_pass_val s; |
| 527 | s.x = 1; |
Ted Kremenek | 8eec7c0 | 2009-08-04 00:58:45 +0000 | [diff] [blame] | 528 | s.y = 2; |
Ted Kremenek | 48775d5 | 2009-08-03 23:22:53 +0000 | [diff] [blame] | 529 | test_pass_val_aux(s); |
| 530 | } |
| 531 | |
Ted Kremenek | 4ed4598 | 2009-08-05 05:31:02 +0000 | [diff] [blame] | 532 | // This is a reduced test case of a false positive that previously appeared |
| 533 | // in RegionStoreManager. Previously the array access resulted in dereferencing |
| 534 | // an undefined value. |
| 535 | int test_array_compound(int *q, int *r, int *z) { |
| 536 | int *array[] = { q, r, z }; |
| 537 | int j = 0; |
| 538 | for (unsigned i = 0; i < 3 ; ++i) |
| 539 | if (*array[i]) ++j; // no-warning |
| 540 | return j; |
| 541 | } |
| 542 | |
Ted Kremenek | 1894dce | 2009-08-25 20:51:30 +0000 | [diff] [blame] | 543 | // This test case previously crashed with -analyzer-store=basic because the |
| 544 | // symbolic value stored in 'x' wouldn't be implicitly casted to a signed value |
| 545 | // during the comparison. |
| 546 | int rdar_7124210(unsigned int x) { |
| 547 | enum { SOME_CONSTANT = 123 }; |
| 548 | int compare = ((signed) SOME_CONSTANT) == *((signed *) &x); |
| 549 | return compare ? 0 : 1; // Forces the evaluation of the symbolic constraint. |
| 550 | } |
| 551 | |
Ted Kremenek | ab2f43c | 2009-08-25 23:29:04 +0000 | [diff] [blame] | 552 | void pr4781(unsigned long *raw1) { |
| 553 | unsigned long *cook, *raw0; |
| 554 | unsigned long dough[32]; |
| 555 | int i; |
| 556 | cook = dough; |
| 557 | for( i = 0; i < 16; i++, raw1++ ) { |
| 558 | raw0 = raw1++; |
| 559 | *cook = (*raw0 & 0x00fc0000L) << 6; |
| 560 | *cook |= (*raw0 & 0x00000fc0L) << 10; |
| 561 | } |
| 562 | } |
| 563 | |
Ted Kremenek | cfcd7fd | 2009-09-09 20:36:12 +0000 | [diff] [blame] | 564 | // <rdar://problem/7185647> - 'self' should be treated as being non-null |
| 565 | // upon entry to an objective-c method. |
| 566 | @interface RDar7185647 |
| 567 | - (id)foo; |
| 568 | @end |
| 569 | @implementation RDar7185647 |
| 570 | - (id) foo { |
| 571 | if (self) |
| 572 | return self; |
| 573 | *((int *) 0x0) = 0xDEADBEEF; // no-warning |
| 574 | return self; |
| 575 | } |
| 576 | @end |
Ted Kremenek | e2b5744 | 2009-09-15 00:40:32 +0000 | [diff] [blame] | 577 | |
| 578 | // Test reasoning of __builtin_offsetof; |
| 579 | struct test_offsetof_A { |
| 580 | int x; |
| 581 | int y; |
| 582 | }; |
| 583 | struct test_offsetof_B { |
| 584 | int w; |
| 585 | int z; |
| 586 | }; |
| 587 | void test_offsetof_1() { |
| 588 | if (__builtin_offsetof(struct test_offsetof_A, x) == |
| 589 | __builtin_offsetof(struct test_offsetof_B, w)) |
| 590 | return; |
| 591 | int *p = 0; |
| 592 | *p = 0xDEADBEEF; // no-warning |
| 593 | } |
| 594 | void test_offsetof_2() { |
| 595 | if (__builtin_offsetof(struct test_offsetof_A, y) == |
| 596 | __builtin_offsetof(struct test_offsetof_B, z)) |
| 597 | return; |
| 598 | int *p = 0; |
| 599 | *p = 0xDEADBEEF; // no-warning |
| 600 | } |
| 601 | void test_offsetof_3() { |
| 602 | if (__builtin_offsetof(struct test_offsetof_A, y) - |
| 603 | __builtin_offsetof(struct test_offsetof_A, x) |
| 604 | == |
| 605 | __builtin_offsetof(struct test_offsetof_B, z) - |
| 606 | __builtin_offsetof(struct test_offsetof_B, w)) |
| 607 | return; |
| 608 | int *p = 0; |
| 609 | *p = 0xDEADBEEF; // no-warning |
| 610 | } |
| 611 | void test_offsetof_4() { |
| 612 | if (__builtin_offsetof(struct test_offsetof_A, y) == |
| 613 | __builtin_offsetof(struct test_offsetof_B, w)) |
| 614 | return; |
| 615 | int *p = 0; |
| 616 | *p = 0xDEADBEEF; // expected-warning{{Dereference of null pointer}} |
| 617 | } |
| 618 | |
Ted Kremenek | c32b244 | 2009-09-21 23:22:11 +0000 | [diff] [blame] | 619 | // <rdar://problem/6829164> "nil receiver" false positive: make tracking |
| 620 | // of the MemRegion for 'self' path-sensitive |
| 621 | @interface RDar6829164 : NSObject { |
| 622 | double x; int y; |
| 623 | } |
| 624 | - (id) init; |
| 625 | @end |
| 626 | |
| 627 | id rdar_6829164_1(); |
| 628 | double rdar_6829164_2(); |
| 629 | |
| 630 | @implementation RDar6829164 |
| 631 | - (id) init { |
| 632 | if((self = [super init]) != 0) { |
| 633 | id z = rdar_6829164_1(); |
| 634 | y = (z != 0); |
| 635 | if (y) |
| 636 | x = rdar_6829164_2(); |
| 637 | } |
| 638 | return self; |
| 639 | } |
| 640 | @end |
Ted Kremenek | cc969fd | 2009-09-22 04:48:39 +0000 | [diff] [blame] | 641 | |
| 642 | // <rdar://problem/7242015> - Invalidate values passed-by-reference |
| 643 | // to functions when the pointer to the value is passed as an integer. |
| 644 | void test_7242015_aux(unsigned long); |
| 645 | int rdar_7242015() { |
| 646 | int x; |
| 647 | test_7242015_aux((unsigned long) &x); // no-warning |
| 648 | return x; // Previously we return and uninitialized value when |
| 649 | // using RegionStore. |
| 650 | } |
| 651 | |
Ted Kremenek | cf54959 | 2009-09-22 21:19:14 +0000 | [diff] [blame] | 652 | // <rdar://problem/7242006> [RegionStore] compound literal assignment with |
| 653 | // floats not honored |
| 654 | CGFloat rdar7242006(CGFloat x) { |
| 655 | NSSize y = (NSSize){x, 10}; |
| 656 | return y.width; // no-warning |
| 657 | } |
| 658 | |
Ted Kremenek | 657406d | 2009-09-23 01:30:01 +0000 | [diff] [blame] | 659 | // PR 4988 - This test exhibits a case where a function can be referenced |
| 660 | // when not explicitly used in an "lvalue" context (as far as the analyzer is |
| 661 | // concerned). This previously triggered a crash due to an invalid assertion. |
| 662 | void pr_4988(void) { |
| 663 | pr_4988; // expected-warning{{expression result unused}} |
| 664 | } |
| 665 | |
Ted Kremenek | 022a125 | 2009-09-26 17:18:44 +0000 | [diff] [blame] | 666 | // <rdar://problem/7152418> - A 'signed char' is used as a flag, which is |
| 667 | // implicitly converted to an int. |
| 668 | void *rdar7152418_bar(); |
| 669 | @interface RDar7152418 { |
| 670 | signed char x; |
| 671 | } |
| 672 | -(char)foo; |
| 673 | @end; |
| 674 | @implementation RDar7152418 |
| 675 | -(char)foo { |
| 676 | char *p = 0; |
| 677 | void *result = 0; |
| 678 | if (x) { |
| 679 | result = rdar7152418_bar(); |
| 680 | p = "hello"; |
| 681 | } |
| 682 | if (!result) { |
| 683 | result = rdar7152418_bar(); |
| 684 | if (result && x) |
| 685 | return *p; // no-warning |
| 686 | } |
| 687 | return 1; |
| 688 | } |
| 689 | |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 690 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b1d0422 | 2009-10-06 03:44:49 +0000 | [diff] [blame] | 691 | // Test constant-folding of symbolic values, automatically handling type |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 692 | // conversions of the symbol as necessary. |
| 693 | //===----------------------------------------------------------------------===// |
| 694 | |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 695 | // Previously this would crash once we started eagerly evaluating symbols whose |
| 696 | // values were constrained to a single value. |
| 697 | void test_symbol_fold_1(signed char x) { |
Ted Kremenek | b1d0422 | 2009-10-06 03:44:49 +0000 | [diff] [blame] | 698 | while (1) { |
| 699 | if (x == ((signed char) 0)) {} |
| 700 | } |
| 701 | } |
| 702 | |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 703 | // This previously caused a crash because it triggered an assertion in APSInt. |
| 704 | void test_symbol_fold_2(unsigned int * p, unsigned int n, |
| 705 | const unsigned int * grumpkin, unsigned int dn) { |
Ted Kremenek | b5deae5 | 2009-10-16 20:46:24 +0000 | [diff] [blame] | 706 | unsigned int i; |
| 707 | unsigned int tempsub[8]; |
| 708 | unsigned int *solgrumpkin = tempsub + n; |
| 709 | for (i = 0; i < n; i++) |
| 710 | solgrumpkin[i] = (i < dn) ? ~grumpkin[i] : 0xFFFFFFFF; |
| 711 | for (i <<= 5; i < (n << 5); i++) {} |
| 712 | } |
Ted Kremenek | b1d0422 | 2009-10-06 03:44:49 +0000 | [diff] [blame] | 713 | |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 714 | // This previously caused a crash because it triggered an assertion in APSInt. |
| 715 | // 'x' would evaluate to a 8-bit constant (because of the return value of |
| 716 | // test_symbol_fold_3_aux()) which would not get properly promoted to an |
| 717 | // integer. |
| 718 | char test_symbol_fold_3_aux(void); |
| 719 | unsigned test_symbol_fold_3(void) { |
| 720 | unsigned x = test_symbol_fold_3_aux(); |
| 721 | if (x == 54) |
| 722 | return (x << 8) | 0x5; |
| 723 | return 0; |
Ted Kremenek | de7d800 | 2009-11-11 06:43:42 +0000 | [diff] [blame^] | 724 | } |
| 725 | |
| 726 | //===----------------------------------------------------------------------===// |
| 727 | // Tests for the warning of casting a non-struct type to a struct type |
| 728 | //===----------------------------------------------------------------------===// |
| 729 | |
| 730 | typedef struct {unsigned int v;} NSSwappedFloat; |
| 731 | |
| 732 | NSSwappedFloat test_cast_nonstruct_to_struct(float x) { |
| 733 | struct hodor { |
| 734 | float number; |
| 735 | NSSwappedFloat sf; |
| 736 | }; |
| 737 | return ((struct hodor *)&x)->sf; // expected-warning{{Casting a non-structure type to a structure type and accessing a field can lead to memory access errors or data corruption}} |
| 738 | } |
| 739 | |
| 740 | NSSwappedFloat test_cast_nonstruct_to_union(float x) { |
| 741 | union bran { |
| 742 | float number; |
| 743 | NSSwappedFloat sf; |
| 744 | }; |
| 745 | return ((union bran *)&x)->sf; // no-warning |
| 746 | } |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 747 | |