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 struct range_list
*whole_range_list(void)
207 return alloc_range_list(whole_range
.min
, whole_range
.max
);
210 void add_range(struct range_list
**list
, long long min
, long long max
)
212 struct data_range
*tmp
= NULL
;
213 struct data_range
*new = NULL
;
217 * FIXME: This has a problem merging a range_list like: min-0,3-max
218 * with a range like 1-2. You end up with min-2,3-max instead of
221 FOR_EACH_PTR(*list
, tmp
) {
223 /* Sometimes we overlap with more than one range
224 so we have to delete or modify the next range. */
225 if (max
+ 1 == tmp
->min
) {
226 /* join 2 ranges here */
228 DELETE_CURRENT_PTR(tmp
);
232 /* Doesn't overlap with the next one. */
235 /* Partially overlaps with the next one. */
236 if (max
< tmp
->max
) {
240 /* Completely overlaps with the next one. */
241 if (max
>= tmp
->max
) {
242 DELETE_CURRENT_PTR(tmp
);
243 /* there could be more ranges to delete */
247 if (max
!= whole_range
.max
&& max
+ 1 == tmp
->min
) {
248 /* join 2 ranges into a big range */
249 new = alloc_range(min
, tmp
->max
);
250 REPLACE_CURRENT_PTR(tmp
, new);
253 if (max
< tmp
->min
) { /* new range entirely below */
254 new = alloc_range(min
, max
);
255 INSERT_CURRENT(new, tmp
);
258 if (min
< tmp
->min
) { /* new range partially below */
263 new = alloc_range(min
, max
);
264 REPLACE_CURRENT_PTR(tmp
, new);
269 if (max
<= tmp
->max
) /* new range already included */
271 if (min
<= tmp
->max
) { /* new range partially above */
273 new = alloc_range(min
, max
);
274 REPLACE_CURRENT_PTR(tmp
, new);
278 if (min
!= whole_range
.min
&& min
- 1 == tmp
->max
) {
279 /* join 2 ranges into a big range */
280 new = alloc_range(tmp
->min
, max
);
281 REPLACE_CURRENT_PTR(tmp
, new);
285 /* the new range is entirely above the existing ranges */
286 } END_FOR_EACH_PTR(tmp
);
289 new = alloc_range(min
, max
);
290 add_ptr_list(list
, new);
293 struct range_list
*clone_range_list(struct range_list
*list
)
295 struct data_range
*tmp
;
296 struct range_list
*ret
= NULL
;
298 FOR_EACH_PTR(list
, tmp
) {
299 add_ptr_list(&ret
, tmp
);
300 } END_FOR_EACH_PTR(tmp
);
304 struct range_list
*clone_permanent(struct range_list
*list
)
306 struct data_range
*tmp
;
307 struct data_range
*new;
308 struct range_list
*ret
= NULL
;
310 FOR_EACH_PTR(list
, tmp
) {
311 new = alloc_range_perm(tmp
->min
, tmp
->max
);
312 add_ptr_list(&ret
, new);
313 } END_FOR_EACH_PTR(tmp
);
317 struct range_list
*range_list_union(struct range_list
*one
, struct range_list
*two
)
319 struct data_range
*tmp
;
320 struct range_list
*ret
= NULL
;
322 FOR_EACH_PTR(one
, tmp
) {
323 add_range(&ret
, tmp
->min
, tmp
->max
);
324 } END_FOR_EACH_PTR(tmp
);
325 FOR_EACH_PTR(two
, tmp
) {
326 add_range(&ret
, tmp
->min
, tmp
->max
);
327 } END_FOR_EACH_PTR(tmp
);
331 struct range_list
*remove_range(struct range_list
*list
, long long min
, long long max
)
333 struct data_range
*tmp
;
334 struct range_list
*ret
= NULL
;
336 FOR_EACH_PTR(list
, tmp
) {
337 if (tmp
->max
< min
) {
338 add_range(&ret
, tmp
->min
, tmp
->max
);
341 if (tmp
->min
> max
) {
342 add_range(&ret
, tmp
->min
, tmp
->max
);
345 if (tmp
->min
>= min
&& tmp
->max
<= max
)
347 if (tmp
->min
>= min
) {
348 add_range(&ret
, max
+ 1, tmp
->max
);
349 } else if (tmp
->max
<= max
) {
350 add_range(&ret
, tmp
->min
, min
- 1);
352 add_range(&ret
, tmp
->min
, min
- 1);
353 add_range(&ret
, max
+ 1, tmp
->max
);
355 } END_FOR_EACH_PTR(tmp
);
360 * if it can be only one and only value return 1, else return 0
362 int estate_get_single_value(struct smatch_state
*state
, long long *val
)
364 struct data_info
*dinfo
;
365 struct data_range
*tmp
;
368 dinfo
= get_dinfo(state
);
369 if (!dinfo
|| dinfo
->type
!= DATA_RANGE
)
372 FOR_EACH_PTR(dinfo
->value_ranges
, tmp
) {
374 if (tmp
->min
!= tmp
->max
)
380 } END_FOR_EACH_PTR(tmp
);
384 int true_comparison_range(struct data_range
*left
, int comparison
, struct data_range
*right
)
386 switch (comparison
) {
388 case SPECIAL_UNSIGNED_LT
:
389 if (left
->min
< right
->max
)
392 case SPECIAL_UNSIGNED_LTE
:
394 if (left
->min
<= right
->max
)
398 if (left
->max
< right
->min
)
400 if (left
->min
> right
->max
)
403 case SPECIAL_UNSIGNED_GTE
:
405 if (left
->max
>= right
->min
)
409 case SPECIAL_UNSIGNED_GT
:
410 if (left
->max
> right
->min
)
413 case SPECIAL_NOTEQUAL
:
414 if (left
->min
!= left
->max
)
416 if (right
->min
!= right
->max
)
418 if (left
->min
!= right
->min
)
422 sm_msg("unhandled comparison %d\n", comparison
);
428 int true_comparison_range_lr(int comparison
, struct data_range
*var
, struct data_range
*val
, int left
)
431 return true_comparison_range(var
, comparison
, val
);
433 return true_comparison_range(val
, comparison
, var
);
436 static int false_comparison_range(struct data_range
*left
, int comparison
, struct data_range
*right
)
438 switch (comparison
) {
440 case SPECIAL_UNSIGNED_LT
:
441 if (left
->max
>= right
->min
)
444 case SPECIAL_UNSIGNED_LTE
:
446 if (left
->max
> right
->min
)
450 if (left
->min
!= left
->max
)
452 if (right
->min
!= right
->max
)
454 if (left
->min
!= right
->min
)
457 case SPECIAL_UNSIGNED_GTE
:
459 if (left
->min
< right
->max
)
463 case SPECIAL_UNSIGNED_GT
:
464 if (left
->min
<= right
->max
)
467 case SPECIAL_NOTEQUAL
:
468 if (left
->max
< right
->min
)
470 if (left
->min
> right
->max
)
474 sm_msg("unhandled comparison %d\n", comparison
);
480 int false_comparison_range_lr(int comparison
, struct data_range
*var
, struct data_range
*val
, int left
)
483 return false_comparison_range(var
, comparison
, val
);
485 return false_comparison_range(val
, comparison
, var
);
488 int possibly_true(struct expression
*left
, int comparison
, struct expression
*right
)
490 struct range_list
*rl_left
, *rl_right
;
491 struct data_range
*tmp_left
, *tmp_right
;
493 if (!get_implied_range_list(left
, &rl_left
))
495 if (!get_implied_range_list(right
, &rl_right
))
498 FOR_EACH_PTR(rl_left
, tmp_left
) {
499 FOR_EACH_PTR(rl_right
, tmp_right
) {
500 if (true_comparison_range(tmp_left
, comparison
, tmp_right
))
502 } END_FOR_EACH_PTR(tmp_right
);
503 } END_FOR_EACH_PTR(tmp_left
);
507 int possibly_false(struct expression
*left
, int comparison
, struct expression
*right
)
509 struct range_list
*rl_left
, *rl_right
;
510 struct data_range
*tmp_left
, *tmp_right
;
512 if (!get_implied_range_list(left
, &rl_left
))
514 if (!get_implied_range_list(right
, &rl_right
))
517 FOR_EACH_PTR(rl_left
, tmp_left
) {
518 FOR_EACH_PTR(rl_right
, tmp_right
) {
519 if (false_comparison_range(tmp_left
, comparison
, tmp_right
))
521 } END_FOR_EACH_PTR(tmp_right
);
522 } END_FOR_EACH_PTR(tmp_left
);
526 int possibly_true_range_lists(struct range_list
*left_ranges
, int comparison
, struct range_list
*right_ranges
)
528 struct data_range
*left_tmp
, *right_tmp
;
530 if (!left_ranges
|| !right_ranges
)
533 FOR_EACH_PTR(left_ranges
, left_tmp
) {
534 FOR_EACH_PTR(right_ranges
, right_tmp
) {
535 if (true_comparison_range(left_tmp
, comparison
, right_tmp
))
537 } END_FOR_EACH_PTR(right_tmp
);
538 } END_FOR_EACH_PTR(left_tmp
);
542 int possibly_false_range_lists(struct range_list
*left_ranges
, int comparison
, struct range_list
*right_ranges
)
544 struct data_range
*left_tmp
, *right_tmp
;
546 if (!left_ranges
|| !right_ranges
)
549 FOR_EACH_PTR(left_ranges
, left_tmp
) {
550 FOR_EACH_PTR(right_ranges
, right_tmp
) {
551 if (false_comparison_range(left_tmp
, comparison
, right_tmp
))
553 } END_FOR_EACH_PTR(right_tmp
);
554 } END_FOR_EACH_PTR(left_tmp
);
558 void tack_on(struct range_list
**list
, struct data_range
*drange
)
560 add_ptr_list(list
, drange
);
563 int in_list_exact(struct range_list
*list
, struct data_range
*drange
)
565 struct data_range
*tmp
;
567 FOR_EACH_PTR(list
, tmp
) {
568 if (tmp
->min
== drange
->min
&& tmp
->max
== drange
->max
)
570 } END_FOR_EACH_PTR(tmp
);
575 void push_range_list(struct range_list_stack
**rl_stack
, struct range_list
*rl
)
577 add_ptr_list(rl_stack
, rl
);
580 struct range_list
*pop_range_list(struct range_list_stack
**rl_stack
)
582 struct range_list
*rl
;
584 rl
= last_ptr_list((struct ptr_list
*)*rl_stack
);
585 delete_ptr_list_last((struct ptr_list
**)rl_stack
);
589 struct range_list
*top_range_list(struct range_list_stack
*rl_stack
)
591 struct range_list
*rl
;
593 rl
= last_ptr_list((struct ptr_list
*)rl_stack
);
597 void filter_top_range_list(struct range_list_stack
**rl_stack
, long long num
)
599 struct range_list
*rl
;
601 rl
= pop_range_list(rl_stack
);
602 rl
= remove_range(rl
, num
, num
);
603 push_range_list(rl_stack
, rl
);
606 void free_range_list(struct range_list
**rlist
)
608 __free_ptr_list((struct ptr_list
**)rlist
);
611 static void free_single_dinfo(struct data_info
*dinfo
)
613 if (dinfo
->type
== DATA_RANGE
)
614 free_range_list(&dinfo
->value_ranges
);
617 static void free_dinfos(struct allocation_blob
*blob
)
619 unsigned int size
= sizeof(struct data_info
);
620 unsigned int offset
= 0;
622 while (offset
< blob
->offset
) {
623 free_single_dinfo((struct data_info
*)(blob
->data
+ offset
));
628 void free_data_info_allocs(void)
630 struct allocator_struct
*desc
= &data_info_allocator
;
631 struct allocation_blob
*blob
= desc
->blobs
;
634 desc
->allocations
= 0;
635 desc
->total_bytes
= 0;
636 desc
->useful_bytes
= 0;
637 desc
->freelist
= NULL
;
639 struct allocation_blob
*next
= blob
->next
;
641 blob_free(blob
, desc
->chunking
);
644 clear_data_range_alloc();