1 // RUN: %clang_analyze_cc1 -analyzer-checker=alpha.core.FixedAddr,alpha.core.PointerArithm,debug.ExprInspection -Wno-pointer-to-int-cast -verify -triple x86_64-apple-darwin9 -Wno-tautological-pointer-compare -analyzer-config eagerly-assume=false %s
2 // RUN: %clang_analyze_cc1 -analyzer-checker=alpha.core.FixedAddr,alpha.core.PointerArithm,debug.ExprInspection -Wno-pointer-to-int-cast -verify -triple i686-apple-darwin9 -Wno-tautological-pointer-compare -analyzer-config eagerly-assume=false %s
4 #include "Inputs/system-header-simulator.h"
6 void clang_analyzer_eval(int);
7 void clang_analyzer_dump(int);
22 // This test case checks if we get the right rvalue type of a TypedViewRegion.
23 // The ElementRegion's type depends on the array region's rvalue type. If it was
24 // a pointer type, we would get a loc::SymbolVal for '*p'.
25 void* memchr(const void *, int, __typeof__(sizeof(0)));
27 domain_port (const char *domain_b
, const char *domain_e
,
28 const char **domain_e_ptr
)
33 const char *colon
= memchr (domain_b
, ':', domain_e
- domain_b
);
35 for (p
= colon
+ 1; p
< domain_e
; p
++)
36 port
= 10 * port
+ (*p
- '0');
40 #define FIXED_VALUE (int*) 0x1111
44 p
= (int*) 0x10000; // expected-warning{{Using a fixed address is not portable because that address will probably not be valid in all environments or platforms}}
51 p
= (int*) x
; // expected-warning{{Using a fixed address is not portable because that address will probably not be valid in all environments or platforms}}
54 sa
.sa_handler
= SIG_IGN
; // no warning (exclude macros defined in system header)
55 sigaction(SIGINT
, &sa
, NULL
);
57 p
= FIXED_VALUE
; // expected-warning{{Using a fixed address is not portable because that address will probably not be valid in all environments or platforms}}
63 p
= &x
+ 1; // expected-warning{{Pointer arithmetic on non-array variables relies on memory layout, which is dangerous}}
66 p
= a
+ 1; // no-warning
69 // Allow arithmetic on different symbolic regions.
70 void f6(int *p
, int *q
) {
71 int d
= q
- p
; // no-warning
74 void null_operand(int *a
) {
76 // LHS is a label, RHS is NULL
77 clang_analyzer_eval(&&start
!= 0); // expected-warning{{TRUE}}
78 clang_analyzer_eval(&&start
>= 0); // expected-warning{{TRUE}}
79 clang_analyzer_eval(&&start
> 0); // expected-warning{{TRUE}}
80 clang_analyzer_eval((&&start
- 0) != 0); // expected-warning{{TRUE}}
82 // LHS is a non-symbolic value, RHS is NULL
83 clang_analyzer_eval(&a
!= 0); // expected-warning{{TRUE}}
84 clang_analyzer_eval(&a
>= 0); // expected-warning{{TRUE}}
85 clang_analyzer_eval(&a
> 0); // expected-warning{{TRUE}}
86 clang_analyzer_eval((&a
- 0) != 0); // expected-warning{{TRUE}}
88 // LHS is NULL, RHS is non-symbolic
89 // The same code is used for labels and non-symbolic values.
90 clang_analyzer_eval(0 != &a
); // expected-warning{{TRUE}}
91 clang_analyzer_eval(0 <= &a
); // expected-warning{{TRUE}}
92 clang_analyzer_eval(0 < &a
); // expected-warning{{TRUE}}
94 // LHS is a symbolic value, RHS is NULL
95 clang_analyzer_eval(a
!= 0); // expected-warning{{UNKNOWN}}
96 clang_analyzer_eval(a
>= 0); // expected-warning{{TRUE}}
97 clang_analyzer_eval(a
<= 0); // expected-warning{{UNKNOWN}}
98 clang_analyzer_eval((a
- 0) != 0); // expected-warning{{UNKNOWN}}
100 // LHS is NULL, RHS is a symbolic value
101 clang_analyzer_eval(0 != a
); // expected-warning{{UNKNOWN}}
102 clang_analyzer_eval(0 <= a
); // expected-warning{{TRUE}}
103 clang_analyzer_eval(0 < a
); // expected-warning{{UNKNOWN}}
106 void const_locs(void) {
107 char *a
= (char*)0x1000;
108 char *b
= (char*)0x1100;
110 clang_analyzer_eval(a
!= b
); // expected-warning{{TRUE}}
111 clang_analyzer_eval(a
< b
); // expected-warning{{TRUE}}
112 clang_analyzer_eval(a
<= b
); // expected-warning{{TRUE}}
113 clang_analyzer_eval((b
-a
) == 0x100); // expected-warning{{TRUE}}
115 clang_analyzer_eval(&&start
== a
); // expected-warning{{UNKNOWN}}
116 clang_analyzer_eval(a
== &&start
); // expected-warning{{UNKNOWN}}
117 clang_analyzer_eval(&a
== (char**)a
); // expected-warning{{UNKNOWN}}
118 clang_analyzer_eval((char**)a
== &a
); // expected-warning{{UNKNOWN}}
121 void array_matching_types(void) {
126 clang_analyzer_eval(a
!= b
); // expected-warning{{TRUE}}
127 clang_analyzer_eval(a
< b
); // expected-warning{{TRUE}}
128 clang_analyzer_eval(a
<= b
); // expected-warning{{TRUE}}
129 clang_analyzer_eval((b
-a
) != 0); // expected-warning{{TRUE}}
132 // This takes a different code path than array_matching_types()
133 void array_different_types(void) {
136 char *b
= (char*)&array
[5];
138 clang_analyzer_eval(a
!= b
); // expected-warning{{TRUE}} expected-warning{{comparison of distinct pointer types}}
139 clang_analyzer_eval(a
< b
); // expected-warning{{TRUE}} expected-warning{{comparison of distinct pointer types}}
140 clang_analyzer_eval(a
<= b
); // expected-warning{{TRUE}} expected-warning{{comparison of distinct pointer types}}
143 struct test
{ int x
; int y
; };
144 void struct_fields(void) {
147 clang_analyzer_eval(&a
.x
!= &a
.y
); // expected-warning{{TRUE}}
148 clang_analyzer_eval(&a
.x
< &a
.y
); // expected-warning{{TRUE}}
149 clang_analyzer_eval(&a
.x
<= &a
.y
); // expected-warning{{TRUE}}
151 clang_analyzer_eval(&a
.x
!= &b
.x
); // expected-warning{{TRUE}}
152 clang_analyzer_eval(&a
.x
> &b
.x
); // expected-warning{{UNKNOWN}}
153 clang_analyzer_eval(&a
.x
>= &b
.x
); // expected-warning{{UNKNOWN}}
156 void mixed_region_types(void) {
159 void *a
= &array
, *b
= &s
;
161 clang_analyzer_eval(&a
!= &b
); // expected-warning{{TRUE}}
162 clang_analyzer_eval(&a
> &b
); // expected-warning{{UNKNOWN}}
163 clang_analyzer_eval(&a
>= &b
); // expected-warning{{UNKNOWN}}
166 void symbolic_region(int *p
) {
169 clang_analyzer_eval(&a
!= p
); // expected-warning{{TRUE}}
170 clang_analyzer_eval(&a
> p
); // expected-warning{{UNKNOWN}}
171 clang_analyzer_eval(&a
>= p
); // expected-warning{{UNKNOWN}}
174 void PR7527 (int *p
) {
175 if (((int) p
) & 1) // not crash
179 void use_symbols(int *lhs
, int *rhs
) {
180 clang_analyzer_eval(lhs
< rhs
); // expected-warning{{UNKNOWN}}
183 clang_analyzer_eval(lhs
< rhs
); // expected-warning{{FALSE}}
185 clang_analyzer_eval(lhs
- rhs
); // expected-warning{{UNKNOWN}}
186 if ((lhs
- rhs
) != 5)
188 clang_analyzer_eval((lhs
- rhs
) == 5); // expected-warning{{TRUE}}
191 void equal_implies_zero(int *lhs
, int *rhs
) {
192 clang_analyzer_eval(lhs
== rhs
); // expected-warning{{UNKNOWN}}
194 clang_analyzer_eval(lhs
!= rhs
); // expected-warning{{FALSE}}
195 clang_analyzer_eval((rhs
- lhs
) == 0); // expected-warning{{TRUE}}
198 clang_analyzer_eval(lhs
== rhs
); // expected-warning{{FALSE}}
199 clang_analyzer_eval(lhs
!= rhs
); // expected-warning{{TRUE}}
200 clang_analyzer_eval((rhs
- lhs
) == 0); // expected-warning{{FALSE}}
203 void zero_implies_equal(int *lhs
, int *rhs
) {
204 clang_analyzer_eval((rhs
- lhs
) == 0); // expected-warning{{UNKNOWN}}
205 if ((rhs
- lhs
) == 0) {
206 clang_analyzer_eval(lhs
!= rhs
); // expected-warning{{FALSE}}
207 clang_analyzer_eval(lhs
== rhs
); // expected-warning{{TRUE}}
210 clang_analyzer_eval((rhs
- lhs
) == 0); // expected-warning{{FALSE}}
211 clang_analyzer_eval(lhs
== rhs
); // expected-warning{{FALSE}}
212 clang_analyzer_eval(lhs
!= rhs
); // expected-warning{{TRUE}}
215 void comparisons_imply_size(int *lhs
, int *rhs
) {
216 clang_analyzer_eval(lhs
<= rhs
); // expected-warning{{UNKNOWN}}
219 clang_analyzer_eval((rhs
- lhs
) == 0); // expected-warning{{FALSE}}
223 clang_analyzer_eval(lhs
<= rhs
); // expected-warning{{TRUE}}
224 // FIXME: In Z3ConstraintManager, ptrdiff_t is mapped to signed bitvector. However, this does not directly imply the unsigned comparison.
225 #ifdef ANALYZER_CM_Z3
226 clang_analyzer_eval((rhs
- lhs
) >= 0); // expected-warning{{UNKNOWN}}
228 clang_analyzer_eval((rhs
- lhs
) >= 0); // expected-warning{{TRUE}}
230 clang_analyzer_eval((rhs
- lhs
) > 0); // expected-warning{{UNKNOWN}}
233 clang_analyzer_eval((rhs
- lhs
) == 0); // expected-warning{{TRUE}}
237 clang_analyzer_eval(lhs
== rhs
); // expected-warning{{FALSE}}
238 clang_analyzer_eval(lhs
< rhs
); // expected-warning{{TRUE}}
239 #ifdef ANALYZER_CM_Z3
240 clang_analyzer_eval((rhs
- lhs
) > 0); // expected-warning{{UNKNOWN}}
242 clang_analyzer_eval((rhs
- lhs
) > 0); // expected-warning{{TRUE}}
246 void size_implies_comparison(int *lhs
, int *rhs
) {
247 clang_analyzer_eval(lhs
<= rhs
); // expected-warning{{UNKNOWN}}
249 if ((rhs
- lhs
) < 0) {
250 clang_analyzer_eval(lhs
== rhs
); // expected-warning{{FALSE}}
254 #ifdef ANALYZER_CM_Z3
255 clang_analyzer_eval(lhs
<= rhs
); // expected-warning{{UNKNOWN}}
257 clang_analyzer_eval(lhs
<= rhs
); // expected-warning{{TRUE}}
259 clang_analyzer_eval((rhs
- lhs
) >= 0); // expected-warning{{TRUE}}
260 clang_analyzer_eval((rhs
- lhs
) > 0); // expected-warning{{UNKNOWN}}
262 if ((rhs
- lhs
) <= 0) {
263 clang_analyzer_eval(lhs
== rhs
); // expected-warning{{TRUE}}
267 clang_analyzer_eval(lhs
== rhs
); // expected-warning{{FALSE}}
268 #ifdef ANALYZER_CM_Z3
269 clang_analyzer_eval(lhs
< rhs
); // expected-warning{{UNKNOWN}}
271 clang_analyzer_eval(lhs
< rhs
); // expected-warning{{TRUE}}
273 clang_analyzer_eval((rhs
- lhs
) > 0); // expected-warning{{TRUE}}
276 void zero_implies_reversed_equal(int *lhs
, int *rhs
) {
277 clang_analyzer_eval((rhs
- lhs
) == 0); // expected-warning{{UNKNOWN}}
278 if ((rhs
- lhs
) == 0) {
279 clang_analyzer_eval(rhs
!= lhs
); // expected-warning{{FALSE}}
280 clang_analyzer_eval(rhs
== lhs
); // expected-warning{{TRUE}}
283 clang_analyzer_eval((rhs
- lhs
) == 0); // expected-warning{{FALSE}}
284 clang_analyzer_eval(rhs
== lhs
); // expected-warning{{FALSE}}
285 clang_analyzer_eval(rhs
!= lhs
); // expected-warning{{TRUE}}
288 void canonical_equal(int *lhs
, int *rhs
) {
289 clang_analyzer_eval(lhs
== rhs
); // expected-warning{{UNKNOWN}}
291 clang_analyzer_eval(rhs
== lhs
); // expected-warning{{TRUE}}
294 clang_analyzer_eval(lhs
== rhs
); // expected-warning{{FALSE}}
295 clang_analyzer_eval(rhs
== lhs
); // expected-warning{{FALSE}}
298 void compare_element_region_and_base(int *p
) {
300 clang_analyzer_eval(p
== q
); // expected-warning{{FALSE}}
307 void symbolicFieldRegion(struct Point
*points
, int i
, int j
) {
308 clang_analyzer_eval(&points
[i
].x
== &points
[j
].x
);// expected-warning{{UNKNOWN}}
309 clang_analyzer_eval(&points
[i
].x
== &points
[i
].y
);// expected-warning{{FALSE}}
310 clang_analyzer_eval(&points
[i
].x
< &points
[i
].y
);// expected-warning{{TRUE}}
313 void negativeIndex(char *str
) {
315 clang_analyzer_eval(*(str
+ 1) == 'a'); // expected-warning{{TRUE}}
316 clang_analyzer_eval(*(str
- 1) == 'a'); // expected-warning{{UNKNOWN}}
318 char *ptr1
= str
- 1;
319 clang_analyzer_eval(*ptr1
== 'a'); // expected-warning{{UNKNOWN}}
323 clang_analyzer_eval(*ptr2
== 'a'); // expected-warning{{UNKNOWN}}
327 clang_analyzer_eval(*ptr3
== 'a'); // expected-warning{{UNKNOWN}}
330 void test_no_crash_on_pointer_to_label(void) {
336 typedef __attribute__((__ext_vector_type__(2))) float simd_float2
;
337 float test_nowarning_on_vector_deref(void) {
338 simd_float2 x
= {0, 1};
339 return x
[1]; // no-warning
346 // These three expressions should produce the same sym vals.
347 void struct_pointer_canon(struct s
*ps
) {
349 clang_analyzer_dump((*ps
).v
);
350 // expected-warning-re@-1{{reg_${{[[:digit:]]+}}<int Element{SymRegion{reg_${{[[:digit:]]+}}<struct s * ps>},0 S64b,struct s}.v>}}
351 clang_analyzer_dump(ps
[0].v
);
352 // expected-warning-re@-1{{reg_${{[[:digit:]]+}}<int Element{SymRegion{reg_${{[[:digit:]]+}}<struct s * ps>},0 S64b,struct s}.v>}}
353 clang_analyzer_dump(ps
->v
);
354 // expected-warning-re@-1{{reg_${{[[:digit:]]+}}<int Element{SymRegion{reg_${{[[:digit:]]+}}<struct s * ps>},0 S64b,struct s}.v>}}
355 clang_analyzer_eval((*ps
).v
== ps
[0].v
); // expected-warning{{TRUE}}
356 clang_analyzer_eval((*ps
).v
== ps
->v
); // expected-warning{{TRUE}}
357 clang_analyzer_eval(ps
[0].v
== ps
->v
); // expected-warning{{TRUE}}
360 void struct_pointer_canon_bidim(struct s
**ps
) {
362 clang_analyzer_eval(&(ps
[0][0].v
) == &((*ps
)->v
)); // expected-warning{{TRUE}}
367 void struct_pointer_canon_typedef(T1
*ps
) {
369 clang_analyzer_dump((*ps
).v
);
370 // expected-warning-re@-1{{reg_${{[[:digit:]]+}}<int Element{SymRegion{reg_${{[[:digit:]]+}}<T1 * ps>},0 S64b,struct s}.v>}}
371 clang_analyzer_dump(ps
[0].v
);
372 // expected-warning-re@-1{{reg_${{[[:digit:]]+}}<int Element{SymRegion{reg_${{[[:digit:]]+}}<T1 * ps>},0 S64b,struct s}.v>}}
373 clang_analyzer_dump(ps
->v
);
374 // expected-warning-re@-1{{reg_${{[[:digit:]]+}}<int Element{SymRegion{reg_${{[[:digit:]]+}}<T1 * ps>},0 S64b,struct s}.v>}}
375 clang_analyzer_eval((*ps
).v
== ps
[0].v
); // expected-warning{{TRUE}}
376 clang_analyzer_eval((*ps
).v
== ps
->v
); // expected-warning{{TRUE}}
377 clang_analyzer_eval(ps
[0].v
== ps
->v
); // expected-warning{{TRUE}}
380 void struct_pointer_canon_bidim_typedef(T1
**ps
) {
382 clang_analyzer_eval(&(ps
[0][0].v
) == &((*ps
)->v
)); // expected-warning{{TRUE}}
385 void struct_pointer_canon_const(const struct s
*ps
) {
387 clang_analyzer_dump((*ps
).v
);
388 // expected-warning-re@-1{{reg_${{[[:digit:]]+}}<int Element{SymRegion{reg_${{[[:digit:]]+}}<const struct s * ps>},0 S64b,struct s}.v>}}
389 clang_analyzer_dump(ps
[0].v
);
390 // expected-warning-re@-1{{reg_${{[[:digit:]]+}}<int Element{SymRegion{reg_${{[[:digit:]]+}}<const struct s * ps>},0 S64b,struct s}.v>}}
391 clang_analyzer_dump(ps
->v
);
392 // expected-warning-re@-1{{reg_${{[[:digit:]]+}}<int Element{SymRegion{reg_${{[[:digit:]]+}}<const struct s * ps>},0 S64b,struct s}.v>}}
393 clang_analyzer_eval((*ps
).v
== ps
[0].v
); // expected-warning{{TRUE}}
394 clang_analyzer_eval((*ps
).v
== ps
->v
); // expected-warning{{TRUE}}
395 clang_analyzer_eval(ps
[0].v
== ps
->v
); // expected-warning{{TRUE}}