2 * sparse/smatch_ranges.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
12 #include "smatch_extra.h"
13 #include "smatch_slist.h"
15 ALLOCATOR(data_info
, "smatch extra data");
16 ALLOCATOR(data_range
, "data range");
17 __DO_ALLOCATOR(struct data_range
, sizeof(struct data_range
), __alignof__(struct data_range
),
18 "permanent ranges", perm_data_range
);
20 struct data_range whole_range
= {
25 static char *show_num(long long num
)
27 static char buff
[256];
29 if (num
== whole_range
.min
)
30 snprintf(buff
, 255, "min");
31 else if (num
== whole_range
.max
)
32 snprintf(buff
, 255, "max");
34 snprintf(buff
, 255, "(%lld)", num
);
36 snprintf(buff
, 255, "%lld", num
);
42 char *show_ranges(struct range_list
*list
)
44 struct data_range
*tmp
;
50 FOR_EACH_PTR(list
, tmp
) {
52 strncat(full
, ",", 254 - strlen(full
));
53 if (tmp
->min
== tmp
->max
) {
54 strncat(full
, show_num(tmp
->min
), 254 - strlen(full
));
57 strncat(full
, show_num(tmp
->min
), 254 - strlen(full
));
58 strncat(full
, "-", 254 - strlen(full
));
59 strncat(full
, show_num(tmp
->max
), 254 - strlen(full
));
60 } END_FOR_EACH_PTR(tmp
);
61 return alloc_sname(full
);
64 void get_value_ranges(char *value
, struct range_list
**rl
)
77 if (!strncmp(start
, "min", 3)) {
81 while (*c
&& *c
!= ',' && *c
!= '-')
83 val1
= strtoll(start
, &c
, 10);
88 add_range(rl
, val1
, val1
);
92 add_range(rl
, val1
, val1
);
97 c
++; /* skip the dash in eg. 4-5 */
101 if (!strncmp(start
, "max", 3)) {
106 while (*c
&& *c
!= ',' && *c
!= '-')
108 val2
= strtoll(start
, &c
, 10);
110 add_range(rl
, val1
, val2
);
115 c
++; /* skip the comma in eg: 4-5,7 */
120 static struct data_range range_zero
= {
125 static struct data_range range_one
= {
130 int is_whole_range_rl(struct range_list
*rl
)
132 struct data_range
*drange
;
134 if (ptr_list_empty(rl
))
136 drange
= first_ptr_list((struct ptr_list
*)rl
);
137 if (drange
->min
== whole_range
.min
&& drange
->max
== whole_range
.max
)
142 long long rl_min(struct range_list
*rl
)
144 struct data_range
*drange
;
146 if (ptr_list_empty(rl
))
147 return whole_range
.min
;
148 drange
= first_ptr_list((struct ptr_list
*)rl
);
152 long long rl_max(struct range_list
*rl
)
154 struct data_range
*drange
;
156 if (ptr_list_empty(rl
))
157 return whole_range
.max
;
158 drange
= last_ptr_list((struct ptr_list
*)rl
);
162 static struct data_range
*alloc_range_helper(long long min
, long long max
, int perm
)
164 struct data_range
*ret
;
167 sm_msg("Error invalid range %lld to %lld", min
, max
);
168 min
= whole_range
.min
;
169 max
= whole_range
.max
;
171 if (min
== whole_range
.min
&& max
== whole_range
.max
)
173 if (min
== 0 && max
== 0)
175 if (min
== 1 && max
== 1)
179 ret
= __alloc_perm_data_range(0);
181 ret
= __alloc_data_range(0);
187 struct data_range
*alloc_range(long long min
, long long max
)
189 return alloc_range_helper(min
, max
, 0);
192 struct data_range
*alloc_range_perm(long long min
, long long max
)
194 return alloc_range_helper(min
, max
, 1);
197 struct range_list
*alloc_range_list(long long min
, long long max
)
199 struct range_list
*rl
= NULL
;
201 add_range(&rl
, min
, max
);
205 void add_range(struct range_list
**list
, long long min
, long long max
)
207 struct data_range
*tmp
= NULL
;
208 struct data_range
*new = NULL
;
212 * FIXME: This has a problem merging a range_list like: min-0,3-max
213 * with a range like 1-2. You end up with min-2,3-max instead of
216 FOR_EACH_PTR(*list
, tmp
) {
218 /* Sometimes we overlap with more than one range
219 so we have to delete or modify the next range. */
220 if (max
+ 1 == tmp
->min
) {
221 /* join 2 ranges here */
223 DELETE_CURRENT_PTR(tmp
);
227 /* Doesn't overlap with the next one. */
230 /* Partially overlaps with the next one. */
231 if (max
< tmp
->max
) {
235 /* Completely overlaps with the next one. */
236 if (max
>= tmp
->max
) {
237 DELETE_CURRENT_PTR(tmp
);
238 /* there could be more ranges to delete */
242 if (max
!= whole_range
.max
&& max
+ 1 == tmp
->min
) {
243 /* join 2 ranges into a big range */
244 new = alloc_range(min
, tmp
->max
);
245 REPLACE_CURRENT_PTR(tmp
, new);
248 if (max
< tmp
->min
) { /* new range entirely below */
249 new = alloc_range(min
, max
);
250 INSERT_CURRENT(new, tmp
);
253 if (min
< tmp
->min
) { /* new range partially below */
258 new = alloc_range(min
, max
);
259 REPLACE_CURRENT_PTR(tmp
, new);
264 if (max
<= tmp
->max
) /* new range already included */
266 if (min
<= tmp
->max
) { /* new range partially above */
268 new = alloc_range(min
, max
);
269 REPLACE_CURRENT_PTR(tmp
, new);
273 if (min
!= whole_range
.min
&& min
- 1 == tmp
->max
) {
274 /* join 2 ranges into a big range */
275 new = alloc_range(tmp
->min
, max
);
276 REPLACE_CURRENT_PTR(tmp
, new);
280 /* the new range is entirely above the existing ranges */
281 } END_FOR_EACH_PTR(tmp
);
284 new = alloc_range(min
, max
);
285 add_ptr_list(list
, new);
288 struct range_list
*clone_range_list(struct range_list
*list
)
290 struct data_range
*tmp
;
291 struct range_list
*ret
= NULL
;
293 FOR_EACH_PTR(list
, tmp
) {
294 add_ptr_list(&ret
, tmp
);
295 } END_FOR_EACH_PTR(tmp
);
299 struct range_list
*range_list_union(struct range_list
*one
, struct range_list
*two
)
301 struct data_range
*tmp
;
302 struct range_list
*ret
= NULL
;
304 FOR_EACH_PTR(one
, tmp
) {
305 add_range(&ret
, tmp
->min
, tmp
->max
);
306 } END_FOR_EACH_PTR(tmp
);
307 FOR_EACH_PTR(two
, tmp
) {
308 add_range(&ret
, tmp
->min
, tmp
->max
);
309 } END_FOR_EACH_PTR(tmp
);
313 struct range_list
*remove_range(struct range_list
*list
, long long min
, long long max
)
315 struct data_range
*tmp
;
316 struct range_list
*ret
= NULL
;
318 FOR_EACH_PTR(list
, tmp
) {
319 if (tmp
->max
< min
) {
320 add_range(&ret
, tmp
->min
, tmp
->max
);
323 if (tmp
->min
> max
) {
324 add_range(&ret
, tmp
->min
, tmp
->max
);
327 if (tmp
->min
>= min
&& tmp
->max
<= max
)
329 if (tmp
->min
>= min
) {
330 add_range(&ret
, max
+ 1, tmp
->max
);
331 } else if (tmp
->max
<= max
) {
332 add_range(&ret
, tmp
->min
, min
- 1);
334 add_range(&ret
, tmp
->min
, min
- 1);
335 add_range(&ret
, max
+ 1, tmp
->max
);
337 } END_FOR_EACH_PTR(tmp
);
342 * if it can be only one and only value return 1, else return 0
344 int estate_get_single_value(struct smatch_state
*state
, long long *val
)
346 struct data_info
*dinfo
;
347 struct data_range
*tmp
;
350 dinfo
= get_dinfo(state
);
351 if (!dinfo
|| dinfo
->type
!= DATA_RANGE
)
354 FOR_EACH_PTR(dinfo
->value_ranges
, tmp
) {
356 if (tmp
->min
!= tmp
->max
)
362 } END_FOR_EACH_PTR(tmp
);
366 int true_comparison_range(struct data_range
*left
, int comparison
, struct data_range
*right
)
368 switch (comparison
) {
370 case SPECIAL_UNSIGNED_LT
:
371 if (left
->min
< right
->max
)
374 case SPECIAL_UNSIGNED_LTE
:
376 if (left
->min
<= right
->max
)
380 if (left
->max
< right
->min
)
382 if (left
->min
> right
->max
)
385 case SPECIAL_UNSIGNED_GTE
:
387 if (left
->max
>= right
->min
)
391 case SPECIAL_UNSIGNED_GT
:
392 if (left
->max
> right
->min
)
395 case SPECIAL_NOTEQUAL
:
396 if (left
->min
!= left
->max
)
398 if (right
->min
!= right
->max
)
400 if (left
->min
!= right
->min
)
404 sm_msg("unhandled comparison %d\n", comparison
);
410 int true_comparison_range_lr(int comparison
, struct data_range
*var
, struct data_range
*val
, int left
)
413 return true_comparison_range(var
, comparison
, val
);
415 return true_comparison_range(val
, comparison
, var
);
418 static int false_comparison_range(struct data_range
*left
, int comparison
, struct data_range
*right
)
420 switch (comparison
) {
422 case SPECIAL_UNSIGNED_LT
:
423 if (left
->max
>= right
->min
)
426 case SPECIAL_UNSIGNED_LTE
:
428 if (left
->max
> right
->min
)
432 if (left
->min
!= left
->max
)
434 if (right
->min
!= right
->max
)
436 if (left
->min
!= right
->min
)
439 case SPECIAL_UNSIGNED_GTE
:
441 if (left
->min
< right
->max
)
445 case SPECIAL_UNSIGNED_GT
:
446 if (left
->min
<= right
->max
)
449 case SPECIAL_NOTEQUAL
:
450 if (left
->max
< right
->min
)
452 if (left
->min
> right
->max
)
456 sm_msg("unhandled comparison %d\n", comparison
);
462 int false_comparison_range_lr(int comparison
, struct data_range
*var
, struct data_range
*val
, int left
)
465 return false_comparison_range(var
, comparison
, val
);
467 return false_comparison_range(val
, comparison
, var
);
470 int possibly_true(struct expression
*left
, int comparison
, struct expression
*right
)
472 struct range_list
*rl_left
, *rl_right
;
473 struct data_range
*tmp_left
, *tmp_right
;
475 if (!get_implied_range_list(left
, &rl_left
))
477 if (!get_implied_range_list(right
, &rl_right
))
480 FOR_EACH_PTR(rl_left
, tmp_left
) {
481 FOR_EACH_PTR(rl_right
, tmp_right
) {
482 if (true_comparison_range(tmp_left
, comparison
, tmp_right
))
484 } END_FOR_EACH_PTR(tmp_right
);
485 } END_FOR_EACH_PTR(tmp_left
);
489 int possibly_false(struct expression
*left
, int comparison
, struct expression
*right
)
491 struct range_list
*rl_left
, *rl_right
;
492 struct data_range
*tmp_left
, *tmp_right
;
494 if (!get_implied_range_list(left
, &rl_left
))
496 if (!get_implied_range_list(right
, &rl_right
))
499 FOR_EACH_PTR(rl_left
, tmp_left
) {
500 FOR_EACH_PTR(rl_right
, tmp_right
) {
501 if (false_comparison_range(tmp_left
, comparison
, tmp_right
))
503 } END_FOR_EACH_PTR(tmp_right
);
504 } END_FOR_EACH_PTR(tmp_left
);
508 int possibly_true_range_lists(struct range_list
*left_ranges
, int comparison
, struct range_list
*right_ranges
)
510 struct data_range
*left_tmp
, *right_tmp
;
512 if (!left_ranges
|| !right_ranges
)
515 FOR_EACH_PTR(left_ranges
, left_tmp
) {
516 FOR_EACH_PTR(right_ranges
, right_tmp
) {
517 if (true_comparison_range(left_tmp
, comparison
, right_tmp
))
519 } END_FOR_EACH_PTR(right_tmp
);
520 } END_FOR_EACH_PTR(left_tmp
);
524 int possibly_true_range_list_lr(int comparison
, struct smatch_state
*state
, struct range_list
*values
, int left
)
526 struct data_range
*tmp
, *tmp2
;
528 FOR_EACH_PTR(estate_ranges(state
), tmp
) {
529 FOR_EACH_PTR(values
, tmp2
) {
530 if (true_comparison_range_lr(comparison
, tmp
, tmp2
, left
))
532 } END_FOR_EACH_PTR(tmp2
);
533 } END_FOR_EACH_PTR(tmp
);
537 int possibly_false_range_lists(struct range_list
*left_ranges
, int comparison
, struct range_list
*right_ranges
)
539 struct data_range
*left_tmp
, *right_tmp
;
541 if (!left_ranges
|| !right_ranges
)
544 FOR_EACH_PTR(left_ranges
, left_tmp
) {
545 FOR_EACH_PTR(right_ranges
, right_tmp
) {
546 if (false_comparison_range(left_tmp
, comparison
, right_tmp
))
548 } END_FOR_EACH_PTR(right_tmp
);
549 } END_FOR_EACH_PTR(left_tmp
);
553 int possibly_false_range_list_lr(int comparison
, struct smatch_state
*state
, struct range_list
*values
, int left
)
555 struct data_range
*tmp
, *tmp2
;
557 FOR_EACH_PTR(estate_ranges(state
), tmp
) {
558 FOR_EACH_PTR(values
, tmp2
) {
559 if (false_comparison_range_lr(comparison
, tmp
, tmp2
, left
))
561 } END_FOR_EACH_PTR(tmp2
);
562 } END_FOR_EACH_PTR(tmp
);
566 void tack_on(struct range_list
**list
, struct data_range
*drange
)
568 add_ptr_list(list
, drange
);
571 int in_list_exact(struct range_list
*list
, struct data_range
*drange
)
573 struct data_range
*tmp
;
575 FOR_EACH_PTR(list
, tmp
) {
576 if (tmp
->min
== drange
->min
&& tmp
->max
== drange
->max
)
578 } END_FOR_EACH_PTR(tmp
);
583 void push_range_list(struct range_list_stack
**rl_stack
, struct range_list
*rl
)
585 add_ptr_list(rl_stack
, rl
);
588 struct range_list
*pop_range_list(struct range_list_stack
**rl_stack
)
590 struct range_list
*rl
;
592 rl
= last_ptr_list((struct ptr_list
*)*rl_stack
);
593 delete_ptr_list_last((struct ptr_list
**)rl_stack
);
597 struct range_list
*top_range_list(struct range_list_stack
*rl_stack
)
599 struct range_list
*rl
;
601 rl
= last_ptr_list((struct ptr_list
*)rl_stack
);
605 void filter_top_range_list(struct range_list_stack
**rl_stack
, long long num
)
607 struct range_list
*rl
;
609 rl
= pop_range_list(rl_stack
);
610 rl
= remove_range(rl
, num
, num
);
611 push_range_list(rl_stack
, rl
);
614 void free_range_list(struct range_list
**rlist
)
616 __free_ptr_list((struct ptr_list
**)rlist
);
619 static void free_single_dinfo(struct data_info
*dinfo
)
621 if (dinfo
->type
== DATA_RANGE
)
622 free_range_list(&dinfo
->value_ranges
);
625 static void free_dinfos(struct allocation_blob
*blob
)
627 unsigned int size
= sizeof(struct data_info
);
628 unsigned int offset
= 0;
630 while (offset
< blob
->offset
) {
631 free_single_dinfo((struct data_info
*)(blob
->data
+ offset
));
636 void free_data_info_allocs(void)
638 struct allocator_struct
*desc
= &data_info_allocator
;
639 struct allocation_blob
*blob
= desc
->blobs
;
642 desc
->allocations
= 0;
643 desc
->total_bytes
= 0;
644 desc
->useful_bytes
= 0;
645 desc
->freelist
= NULL
;
647 struct allocation_blob
*next
= blob
->next
;
649 blob_free(blob
, desc
->chunking
);
652 clear_data_range_alloc();