2 * Copyright (C) 2009 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
20 #include "smatch_extra.h"
21 #include "smatch_slist.h"
23 ALLOCATOR(data_info
, "smatch extra data");
24 ALLOCATOR(data_range
, "data range");
25 __DO_ALLOCATOR(struct data_range
, sizeof(struct data_range
), __alignof__(struct data_range
),
26 "permanent ranges", perm_data_range
);
27 __DECLARE_ALLOCATOR(struct ptr_list
, rl_ptrlist
);
29 bool is_err_ptr(sval_t sval
)
31 if (option_project
!= PROJ_KERNEL
)
33 if (!type_is_ptr(sval
.type
))
35 if (sval
.uvalue
< -4095ULL)
40 bool is_err_or_null(struct range_list
*rl
)
42 struct range_list
*no_null
;
47 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 0)
50 if (rl_min(rl
).value
!= 0)
53 no_null
= remove_range(rl
, rl_min(rl
), rl_min(rl
));
55 return is_err_ptr(rl_min(no_null
)) && is_err_ptr(rl_max(no_null
));
58 static bool is_oxdead(struct range_list
*rl
)
62 /* check for 0xdead000000000000 */
63 if (!rl_to_sval(rl
, &sval
))
65 if ((sval
.uvalue
>> (bits_in_pointer
- 16)) == 0xdead)
71 bool is_noderef_ptr_rl(struct range_list
*rl
)
76 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 0)
78 if (is_err_or_null(rl
))
85 bool rl_is_zero(struct range_list
*rl
)
89 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 0)
94 static char *get_err_pointer_str(struct data_range
*drange
)
99 * The kernel has error pointers where you do essentially:
101 * return (void *)(unsigned long)-12;
103 * But what I want here is to print -12 instead of the unsigned version
107 if (!is_err_ptr(drange
->min
))
110 if (drange
->min
.value
== drange
->max
.value
)
111 snprintf(buf
, sizeof(buf
), "(%lld)", drange
->min
.value
);
113 snprintf(buf
, sizeof(buf
), "(%lld)-(%lld)", drange
->min
.value
, drange
->max
.value
);
117 char *show_rl(struct range_list
*list
)
119 struct data_range
*prev_drange
= NULL
;
120 struct data_range
*tmp
;
130 FOR_EACH_PTR(list
, tmp
) {
131 remain
= full
+ sizeof(full
) - p
;
133 snprintf(prev
, full
+ sizeof(full
) - prev
, ",%s-%s",
134 sval_to_str(prev_drange
->min
),
135 sval_to_str(sval_type_max(prev_drange
->min
.type
)));
141 err_ptr
= get_err_pointer_str(tmp
);
143 p
+= snprintf(p
, remain
, "%s%s", i
++ ? "," : "", err_ptr
);
144 } else if (sval_cmp(tmp
->min
, tmp
->max
) == 0) {
145 p
+= snprintf(p
, remain
, "%s%s", i
++ ? "," : "",
146 sval_to_str(tmp
->min
));
148 p
+= snprintf(p
, remain
, "%s%s-%s", i
++ ? "," : "",
149 sval_to_str(tmp
->min
),
150 sval_to_str(tmp
->max
));
152 } END_FOR_EACH_PTR(tmp
);
154 return alloc_sname(full
);
157 void free_all_rl(void)
159 clear_rl_ptrlist_alloc();
162 static int sval_too_big(struct symbol
*type
, sval_t sval
)
164 if (type_bits(type
) >= 32 &&
165 type_bits(sval
.type
) <= type_bits(type
))
167 if (sval
.uvalue
<= ((1ULL << type_bits(type
)) - 1))
169 if (type_signed(sval
.type
)) {
170 if (type_unsigned(type
)) {
171 unsigned long long neg
= ~sval
.uvalue
;
172 if (neg
<= sval_type_max(type
).uvalue
)
175 if (sval
.value
< sval_type_min(type
).value
)
177 if (sval
.value
> sval_type_max(type
).value
)
181 if (sval
.uvalue
> sval_type_max(type
).uvalue
)
186 static int truncates_nicely(struct symbol
*type
, sval_t min
, sval_t max
)
188 unsigned long long mask
;
189 int bits
= type_bits(type
);
191 if (type_is_fp(min
.type
) && !type_is_fp(type
))
194 if (bits
>= type_bits(min
.type
))
197 mask
= -1ULL << bits
;
198 return (min
.uvalue
& mask
) == (max
.uvalue
& mask
);
201 static void add_range_t(struct symbol
*type
, struct range_list
**rl
, sval_t min
, sval_t max
)
203 /* If we're just adding a number, cast it and add it */
204 if (sval_cmp(min
, max
) == 0) {
205 add_range(rl
, sval_cast(type
, min
), sval_cast(type
, max
));
209 /* If the range is within the type range then add it */
210 if (sval_fits(type
, min
) && sval_fits(type
, max
)) {
211 add_range(rl
, sval_cast(type
, min
), sval_cast(type
, max
));
215 if (truncates_nicely(type
, min
, max
)) {
216 add_range(rl
, sval_cast(type
, min
), sval_cast(type
, max
));
221 * If the range we are adding has more bits than the range type then
222 * add the whole range type. Eg:
223 * 0x8000000000000000 - 0xf000000000000000 -> cast to int
226 if (sval_too_big(type
, min
) || sval_too_big(type
, max
)) {
227 add_range(rl
, sval_type_min(type
), sval_type_max(type
));
231 /* Cast negative values to high positive values */
232 if (sval_is_negative(min
) && type_unsigned(type
)) {
233 if (sval_is_positive(max
)) {
234 if (sval_too_high(type
, max
)) {
235 add_range(rl
, sval_type_min(type
), sval_type_max(type
));
238 add_range(rl
, sval_type_val(type
, 0), sval_cast(type
, max
));
239 max
= sval_type_max(type
);
241 max
= sval_cast(type
, max
);
243 min
= sval_cast(type
, min
);
244 add_range(rl
, min
, max
);
247 /* Cast high positive numbers to negative */
248 if (sval_unsigned(max
) && sval_is_negative(sval_cast(type
, max
))) {
249 if (!sval_is_negative(sval_cast(type
, min
))) {
250 add_range(rl
, sval_cast(type
, min
), sval_type_max(type
));
251 min
= sval_type_min(type
);
253 min
= sval_cast(type
, min
);
255 max
= sval_cast(type
, max
);
256 add_range(rl
, min
, max
);
259 add_range(rl
, sval_cast(type
, min
), sval_cast(type
, max
));
263 static int str_to_comparison_arg_helper(const char *str
,
264 struct expression
*call
, int *comparison
,
265 struct expression
**arg
, const char **endp
)
277 *comparison
= SPECIAL_LTE
;
282 } else if (*c
== '=') {
285 *comparison
= SPECIAL_EQUAL
;
286 } else if (*c
== '>') {
289 *comparison
= SPECIAL_GTE
;
294 } else if (*c
== '!') {
297 *comparison
= SPECIAL_NOTEQUAL
;
298 } else if (*c
== '$') {
299 *comparison
= SPECIAL_EQUAL
;
308 param
= strtoll(c
, (char **)&c
, 10);
310 * FIXME: handle parameter math. [==$1 + 100]
316 if (*c
== ',' || *c
== ']')
317 c
++; /* skip the ']' character */
323 *arg
= get_argument_from_call_expr(call
->args
, param
);
326 if (*c
== '-' && *(c
+ 1) == '>') {
330 n
= snprintf(buf
, sizeof(buf
), "$%s", c
);
331 if (n
>= sizeof(buf
))
333 if (buf
[n
- 1] == ']')
335 *arg
= gen_expression_from_key(*arg
, buf
);
336 while (*c
&& *c
!= ']')
342 int str_to_comparison_arg(const char *str
, struct expression
*call
, int *comparison
, struct expression
**arg
)
351 return str_to_comparison_arg_helper(str
, call
, comparison
, arg
, NULL
);
354 static int get_val_from_key(int use_max
, struct symbol
*type
, const char *c
, struct expression
*call
, const char **endp
, sval_t
*sval
)
356 struct expression
*arg
;
361 ret
= sval_type_max(type
);
363 ret
= sval_type_min(type
);
365 if (!str_to_comparison_arg_helper(c
, call
, &comparison
, &arg
, endp
)) {
370 if (use_max
&& get_implied_max(arg
, &tmp
)) {
372 if (comparison
== '<') {
374 ret
= sval_binop(ret
, '-', tmp
);
377 if (!use_max
&& get_implied_min(arg
, &tmp
)) {
379 if (comparison
== '>') {
381 ret
= sval_binop(ret
, '+', tmp
);
389 static sval_t
add_one(sval_t sval
)
395 static sval_t
sub_one(sval_t sval
)
401 void filter_by_comparison(struct range_list
**rl
, int comparison
, struct range_list
*right
)
403 struct range_list
*left_orig
= *rl
;
404 struct range_list
*right_orig
= right
;
405 struct range_list
*ret_rl
= *rl
;
406 struct symbol
*cast_type
;
409 if (comparison
== UNKNOWN_COMPARISON
)
412 cast_type
= rl_type(left_orig
);
413 if (sval_type_max(rl_type(left_orig
)).uvalue
< sval_type_max(rl_type(right_orig
)).uvalue
)
414 cast_type
= rl_type(right_orig
);
415 if (sval_type_max(cast_type
).uvalue
< INT_MAX
)
416 cast_type
= &int_ctype
;
418 min
= sval_type_min(cast_type
);
419 max
= sval_type_max(cast_type
);
420 left_orig
= cast_rl(cast_type
, left_orig
);
421 right_orig
= cast_rl(cast_type
, right_orig
);
423 switch (comparison
) {
425 case SPECIAL_UNSIGNED_LT
:
426 ret_rl
= remove_range(left_orig
, rl_max(right_orig
), max
);
429 case SPECIAL_UNSIGNED_LTE
:
430 if (!sval_is_max(rl_max(right_orig
)))
431 ret_rl
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
434 ret_rl
= rl_intersection(left_orig
, right_orig
);
437 case SPECIAL_UNSIGNED_GTE
:
438 if (!sval_is_min(rl_min(right_orig
)))
439 ret_rl
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
442 case SPECIAL_UNSIGNED_GT
:
443 ret_rl
= remove_range(left_orig
, min
, rl_min(right_orig
));
445 case SPECIAL_NOTEQUAL
:
446 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
447 ret_rl
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
450 sm_perror("unhandled comparison %s", show_special(comparison
));
454 *rl
= cast_rl(rl_type(*rl
), ret_rl
);
457 static struct range_list
*filter_by_comparison_call(const char *c
, struct expression
*call
, const char **endp
, struct range_list
*start_rl
)
460 struct expression
*arg
;
461 struct range_list
*casted_start
, *right_orig
;
464 /* For when we have a function that takes a function pointer. */
465 if (!call
|| call
->type
!= EXPR_CALL
)
468 if (!str_to_comparison_arg_helper(c
, call
, &comparison
, &arg
, endp
))
471 if (!get_implied_rl(arg
, &right_orig
))
475 if (type_positive_bits(rl_type(start_rl
)) > type_positive_bits(type
))
476 type
= rl_type(start_rl
);
477 if (type_positive_bits(rl_type(right_orig
)) > type_positive_bits(type
))
478 type
= rl_type(right_orig
);
480 casted_start
= cast_rl(type
, start_rl
);
481 right_orig
= cast_rl(type
, right_orig
);
483 filter_by_comparison(&casted_start
, comparison
, right_orig
);
484 return cast_rl(rl_type(start_rl
), casted_start
);
487 static sval_t
parse_val(int use_max
, struct expression
*call
, struct symbol
*type
, const char *c
, const char **endp
)
489 const char *start
= c
;
492 if (type
== &float_ctype
)
493 return sval_type_fval(type
, strtof(start
, (char **)endp
));
494 else if (type
== &double_ctype
)
495 return sval_type_fval(type
, strtod(start
, (char **)endp
));
496 else if (type
== &ldouble_ctype
)
497 return sval_type_fval(type
, strtold(start
, (char **)endp
));
499 if (!strncmp(start
, "max", 3)) {
500 ret
= sval_type_max(type
);
502 } else if (!strncmp(start
, "u64max", 6)) {
503 ret
= sval_type_val(type
, ULLONG_MAX
);
505 } else if (!strncmp(start
, "s64max", 6)) {
506 ret
= sval_type_val(type
, LLONG_MAX
);
508 } else if (!strncmp(start
, "u32max", 6)) {
509 ret
= sval_type_val(type
, UINT_MAX
);
511 } else if (!strncmp(start
, "s32max", 6)) {
512 ret
= sval_type_val(type
, INT_MAX
);
514 } else if (!strncmp(start
, "u16max", 6)) {
515 ret
= sval_type_val(type
, USHRT_MAX
);
517 } else if (!strncmp(start
, "s16max", 6)) {
518 ret
= sval_type_val(type
, SHRT_MAX
);
520 } else if (!strncmp(start
, "min", 3)) {
521 ret
= sval_type_min(type
);
523 } else if (!strncmp(start
, "s64min", 6)) {
524 ret
= sval_type_val(type
, LLONG_MIN
);
526 } else if (!strncmp(start
, "s32min", 6)) {
527 ret
= sval_type_val(type
, INT_MIN
);
529 } else if (!strncmp(start
, "s16min", 6)) {
530 ret
= sval_type_val(type
, SHRT_MIN
);
532 } else if (!strncmp(start
, "long_min", 8)) {
533 ret
= sval_type_val(type
, LONG_MIN
);
535 } else if (!strncmp(start
, "long_max", 8)) {
536 ret
= sval_type_val(type
, LONG_MAX
);
538 } else if (!strncmp(start
, "ulong_max", 9)) {
539 ret
= sval_type_val(type
, ULONG_MAX
);
541 } else if (!strncmp(start
, "ptr_max", 7)) {
542 ret
= sval_type_val(type
, valid_ptr_max
);
544 } else if (start
[0] == '[') {
545 /* this parses [==p0] comparisons */
546 get_val_from_key(1, type
, start
, call
, &c
, &ret
);
547 } else if (type_positive_bits(type
) == 64) {
548 ret
= sval_type_val(type
, strtoull(start
, (char **)&c
, 0));
550 ret
= sval_type_val(type
, strtoll(start
, (char **)&c
, 0));
556 static const char *jump_to_call_math(const char *value
)
558 const char *c
= value
;
560 while (*c
&& *c
!= '[')
566 if (*c
== '<' || *c
== '=' || *c
== '>' || *c
== '!')
572 static struct range_list
*get_param_return_rl(struct expression
*call
, const char *call_math
)
574 struct expression
*arg
;
578 param
= atoi(call_math
);
580 arg
= get_argument_from_call_expr(call
->args
, param
);
584 return db_return_vals_no_args(arg
);
587 static void str_to_rl_helper(struct expression
*call
, struct symbol
*type
, const char *str
, const char **endp
, struct range_list
**rl
)
589 struct range_list
*rl_tmp
= NULL
;
590 sval_t prev_min
, min
, max
;
593 prev_min
= sval_type_min(type
);
594 min
= sval_type_min(type
);
595 max
= sval_type_max(type
);
597 while (*c
!= '\0' && *c
!= '[') {
599 if (sval_cmp(min
, sval_type_min(type
)) != 0)
601 max
= sval_type_max(type
);
602 add_range_t(type
, &rl_tmp
, min
, max
);
607 min
= parse_val(0, call
, type
, c
, &c
);
608 if (!sval_fits(type
, min
))
609 min
= sval_type_min(type
);
613 if (*c
== '\0' || *c
== '[') {
614 add_range_t(type
, &rl_tmp
, min
, min
);
618 add_range_t(type
, &rl_tmp
, min
, min
);
624 max
= sval_type_max(type
);
625 add_range_t(type
, &rl_tmp
, min
, max
);
627 if (*c
== '[' || *c
== '\0')
631 sm_msg("debug XXX: trouble parsing %s c = %s", str
, c
);
637 max
= parse_val(1, call
, type
, c
, &c
);
638 if (!sval_fits(type
, max
))
639 max
= sval_type_max(type
);
641 max
= sval_type_max(type
);
642 add_range_t(type
, &rl_tmp
, min
, max
);
644 if (*c
== '[' || *c
== '\0')
648 add_range_t(type
, &rl_tmp
, min
, max
);
659 static void str_to_dinfo(struct expression
*call
, struct symbol
*type
, const char *value
, struct data_info
*dinfo
)
661 struct range_list
*math_rl
;
662 const char *call_math
;
664 struct range_list
*rl
= NULL
;
669 if (strcmp(value
, "empty") == 0)
672 if (strncmp(value
, "[==$", 4) == 0) {
673 struct expression
*arg
;
676 if (!str_to_comparison_arg(value
, call
, &comparison
, &arg
))
678 if (!get_implied_rl(arg
, &rl
))
683 str_to_rl_helper(call
, type
, value
, &c
, &rl
);
687 call_math
= jump_to_call_math(value
);
688 if (call_math
&& call_math
[0] == 'r') {
689 math_rl
= get_param_return_rl(call
, call_math
);
691 rl
= rl_intersection(rl
, math_rl
);
694 if (call_math
&& parse_call_math_rl(call
, call_math
, &math_rl
)) {
695 rl
= rl_intersection(rl
, math_rl
);
700 * For now if we already tried to handle the call math and couldn't
701 * figure it out then bail.
703 if (jump_to_call_math(c
) == c
+ 1)
706 rl
= filter_by_comparison_call(c
, call
, &c
, rl
);
709 rl
= cast_rl(type
, rl
);
710 dinfo
->value_ranges
= rl
;
713 static int rl_is_sane(struct range_list
*rl
)
715 struct data_range
*tmp
;
719 FOR_EACH_PTR(rl
, tmp
) {
720 if (!sval_fits(type
, tmp
->min
))
722 if (!sval_fits(type
, tmp
->max
))
724 if (sval_cmp(tmp
->min
, tmp
->max
) > 0)
726 } END_FOR_EACH_PTR(tmp
);
731 void str_to_rl(struct symbol
*type
, char *value
, struct range_list
**rl
)
733 struct data_info dinfo
= {};
735 str_to_dinfo(NULL
, type
, value
, &dinfo
);
736 if (!rl_is_sane(dinfo
.value_ranges
))
737 dinfo
.value_ranges
= alloc_whole_rl(type
);
738 *rl
= dinfo
.value_ranges
;
741 void call_results_to_rl(struct expression
*expr
, struct symbol
*type
, const char *value
, struct range_list
**rl
)
743 struct data_info dinfo
= {};
745 str_to_dinfo(strip_expr(expr
), type
, value
, &dinfo
);
746 *rl
= dinfo
.value_ranges
;
749 int is_whole_rl(struct range_list
*rl
)
751 struct data_range
*drange
;
753 if (ptr_list_empty((struct ptr_list
*)rl
))
755 drange
= first_ptr_list((struct ptr_list
*)rl
);
756 if (sval_is_min(drange
->min
) && sval_is_max(drange
->max
))
761 int is_unknown_ptr(struct range_list
*rl
)
763 struct data_range
*drange
;
769 FOR_EACH_PTR(rl
, drange
) {
772 if (sval_cmp(drange
->min
, valid_ptr_min_sval
) == 0 &&
773 sval_cmp(drange
->max
, valid_ptr_max_sval
) == 0)
775 } END_FOR_EACH_PTR(drange
);
780 bool is_whole_ptr_rl(struct range_list
*rl
)
782 struct data_range
*drange
;
785 /* A whole pointer range is either 0-ulong_max or NULL, valid range, and
791 if (ptr_list_size((struct ptr_list
*)rl
) != 2)
794 FOR_EACH_PTR(rl
, drange
) {
798 if (drange
->min
.value
!= 0 ||
799 drange
->max
.value
!= 0)
802 if (drange
->min
.value
!= valid_ptr_min
||
803 drange
->max
.value
!= ULONG_MAX
)
806 } END_FOR_EACH_PTR(drange
);
811 int is_whole_rl_non_zero(struct range_list
*rl
)
813 struct data_range
*drange
;
815 if (ptr_list_empty((struct ptr_list
*)rl
))
817 drange
= first_ptr_list((struct ptr_list
*)rl
);
818 if (sval_unsigned(drange
->min
) &&
819 drange
->min
.value
== 1 &&
820 sval_is_max(drange
->max
))
822 if (!sval_is_min(drange
->min
) || drange
->max
.value
!= -1)
824 drange
= last_ptr_list((struct ptr_list
*)rl
);
825 if (drange
->min
.value
!= 1 || !sval_is_max(drange
->max
))
830 sval_t
rl_min(struct range_list
*rl
)
832 struct data_range
*drange
;
835 ret
.type
= &llong_ctype
;
836 ret
.value
= LLONG_MIN
;
837 if (ptr_list_empty((struct ptr_list
*)rl
))
839 drange
= first_ptr_list((struct ptr_list
*)rl
);
843 sval_t
rl_max(struct range_list
*rl
)
845 struct data_range
*drange
;
848 ret
.type
= &llong_ctype
;
849 ret
.value
= LLONG_MAX
;
850 if (ptr_list_empty((struct ptr_list
*)rl
))
852 drange
= last_ptr_list((struct ptr_list
*)rl
);
856 int rl_to_sval(struct range_list
*rl
, sval_t
*sval
)
865 if (sval_cmp(min
, max
) != 0)
871 struct symbol
*rl_type(struct range_list
*rl
)
875 return rl_min(rl
).type
;
878 static struct data_range
*alloc_range_helper_sval(sval_t min
, sval_t max
, int perm
)
880 struct data_range
*ret
;
883 ret
= __alloc_perm_data_range(0);
885 ret
= __alloc_data_range(0);
891 struct data_range
*alloc_range(sval_t min
, sval_t max
)
893 return alloc_range_helper_sval(min
, max
, 0);
896 struct data_range
*alloc_range_perm(sval_t min
, sval_t max
)
898 return alloc_range_helper_sval(min
, max
, 1);
901 struct range_list
*alloc_rl(sval_t min
, sval_t max
)
903 struct range_list
*rl
= NULL
;
905 if (sval_cmp(min
, max
) > 0)
906 return alloc_whole_rl(min
.type
);
908 add_range(&rl
, min
, max
);
912 struct range_list
*alloc_whole_rl(struct symbol
*type
)
914 if (!type
|| type_positive_bits(type
) < 0)
916 if (type
->type
== SYM_ARRAY
)
919 return alloc_rl(sval_type_min(type
), sval_type_max(type
));
922 static bool collapse_pointer_rl(struct range_list
**rl
, sval_t min
, sval_t max
)
924 struct range_list
*new_rl
= NULL
;
925 struct data_range
*tmp
;
931 * With the mtag work, then we end up getting huge lists of mtags.
932 * That seems cool, but the problem is that we can only store about
933 * 8-10 mtags in the DB before we truncate the list. Also the mtags
934 * aren't really used at all so it's a waste of resources for now...
935 * In the future, we maybe will revisit this code.
942 if (!type_is_ptr(min
.type
))
945 if (ptr_list_size((struct ptr_list
*)*rl
) < 8)
947 FOR_EACH_PTR(*rl
, tmp
) {
948 if (!is_err_ptr(tmp
->min
))
950 } END_FOR_EACH_PTR(tmp
);
954 FOR_EACH_PTR(*rl
, tmp
) {
955 if (sval_cmp(tmp
->min
, valid_ptr_min_sval
) >= 0 &&
956 sval_cmp(tmp
->max
, valid_ptr_max_sval
) <= 0)
957 add_range(&new_rl
, valid_ptr_min_sval
, valid_ptr_max_sval
);
959 add_range(&new_rl
, tmp
->min
, tmp
->max
);
960 } END_FOR_EACH_PTR(tmp
);
962 add_range(&new_rl
, min
, max
);
971 extern int rl_ptrlist_hack
;
972 void add_range(struct range_list
**list
, sval_t min
, sval_t max
)
974 struct data_range
*tmp
;
975 struct data_range
*new = NULL
;
979 * There is at least on valid reason why the types might be confusing
980 * and that's when you have a void pointer and on some paths you treat
981 * it as a u8 pointer and on other paths you treat it as a u16 pointer.
982 * This case is hard to deal with.
984 * There are other cases where we probably should be more specific about
985 * the types than we are. For example, we end up merging a lot of ulong
986 * with pointers and I have not figured out why we do that.
988 * But this hack works for both cases, I think. We cast it to pointers
989 * or we use the bigger size.
992 if (*list
&& rl_type(*list
) != min
.type
) {
993 if (rl_type(*list
)->type
== SYM_PTR
) {
994 min
= sval_cast(rl_type(*list
), min
);
995 max
= sval_cast(rl_type(*list
), max
);
996 } else if (min
.type
->type
== SYM_PTR
) {
997 *list
= cast_rl(min
.type
, *list
);
998 } else if (type_bits(rl_type(*list
)) >= type_bits(min
.type
)) {
999 min
= sval_cast(rl_type(*list
), min
);
1000 max
= sval_cast(rl_type(*list
), max
);
1002 *list
= cast_rl(min
.type
, *list
);
1006 if (sval_cmp(min
, max
) > 0) {
1007 min
= sval_type_min(min
.type
);
1008 max
= sval_type_max(min
.type
);
1011 if (collapse_pointer_rl(list
, min
, max
))
1015 * FIXME: This has a problem merging a range_list like: min-0,3-max
1016 * with a range like 1-2. You end up with min-2,3-max instead of
1019 FOR_EACH_PTR(*list
, tmp
) {
1021 /* Sometimes we overlap with more than one range
1022 so we have to delete or modify the next range. */
1023 if (!sval_is_max(max
) && max
.value
+ 1 == tmp
->min
.value
) {
1024 /* join 2 ranges here */
1025 new->max
= tmp
->max
;
1026 DELETE_CURRENT_PTR(tmp
);
1030 /* Doesn't overlap with the next one. */
1031 if (sval_cmp(max
, tmp
->min
) < 0)
1034 if (sval_cmp(max
, tmp
->max
) <= 0) {
1035 /* Partially overlaps the next one. */
1036 new->max
= tmp
->max
;
1037 DELETE_CURRENT_PTR(tmp
);
1040 /* Completely overlaps the next one. */
1041 DELETE_CURRENT_PTR(tmp
);
1042 /* there could be more ranges to delete */
1046 if (!sval_is_max(max
) && max
.value
+ 1 == tmp
->min
.value
) {
1047 /* join 2 ranges into a big range */
1048 new = alloc_range(min
, tmp
->max
);
1049 REPLACE_CURRENT_PTR(tmp
, new);
1052 if (sval_cmp(max
, tmp
->min
) < 0) { /* new range entirely below */
1053 new = alloc_range(min
, max
);
1054 INSERT_CURRENT(new, tmp
);
1057 if (sval_cmp(min
, tmp
->min
) < 0) { /* new range partially below */
1058 if (sval_cmp(max
, tmp
->max
) < 0)
1062 new = alloc_range(min
, max
);
1063 REPLACE_CURRENT_PTR(tmp
, new);
1068 if (sval_cmp(max
, tmp
->max
) <= 0) /* new range already included */
1070 if (sval_cmp(min
, tmp
->max
) <= 0) { /* new range partially above */
1072 new = alloc_range(min
, max
);
1073 REPLACE_CURRENT_PTR(tmp
, new);
1077 if (!sval_is_min(min
) && min
.value
- 1 == tmp
->max
.value
) {
1078 /* join 2 ranges into a big range */
1079 new = alloc_range(tmp
->min
, max
);
1080 REPLACE_CURRENT_PTR(tmp
, new);
1084 /* the new range is entirely above the existing ranges */
1085 } END_FOR_EACH_PTR(tmp
);
1088 new = alloc_range(min
, max
);
1090 rl_ptrlist_hack
= 1;
1091 add_ptr_list(list
, new);
1092 rl_ptrlist_hack
= 0;
1095 struct range_list
*clone_rl(struct range_list
*list
)
1097 struct data_range
*tmp
;
1098 struct range_list
*ret
= NULL
;
1100 FOR_EACH_PTR(list
, tmp
) {
1101 add_ptr_list(&ret
, tmp
);
1102 } END_FOR_EACH_PTR(tmp
);
1106 struct range_list
*clone_rl_permanent(struct range_list
*list
)
1108 struct data_range
*tmp
;
1109 struct data_range
*new;
1110 struct range_list
*ret
= NULL
;
1112 FOR_EACH_PTR(list
, tmp
) {
1113 new = alloc_range_perm(tmp
->min
, tmp
->max
);
1114 add_ptr_list(&ret
, new);
1115 } END_FOR_EACH_PTR(tmp
);
1119 struct range_list
*rl_union(struct range_list
*one
, struct range_list
*two
)
1121 struct data_range
*tmp
;
1122 struct range_list
*ret
= NULL
;
1124 FOR_EACH_PTR(one
, tmp
) {
1125 add_range(&ret
, tmp
->min
, tmp
->max
);
1126 } END_FOR_EACH_PTR(tmp
);
1127 FOR_EACH_PTR(two
, tmp
) {
1128 add_range(&ret
, tmp
->min
, tmp
->max
);
1129 } END_FOR_EACH_PTR(tmp
);
1133 struct range_list
*remove_range(struct range_list
*list
, sval_t min
, sval_t max
)
1135 struct data_range
*tmp
;
1136 struct range_list
*ret
= NULL
;
1141 min
= sval_cast(rl_type(list
), min
);
1142 max
= sval_cast(rl_type(list
), max
);
1143 if (sval_cmp(min
, max
) > 0) {
1149 FOR_EACH_PTR(list
, tmp
) {
1150 if (sval_cmp(tmp
->max
, min
) < 0) {
1151 add_range(&ret
, tmp
->min
, tmp
->max
);
1154 if (sval_cmp(tmp
->min
, max
) > 0) {
1155 add_range(&ret
, tmp
->min
, tmp
->max
);
1158 if (sval_cmp(tmp
->min
, min
) >= 0 && sval_cmp(tmp
->max
, max
) <= 0)
1160 if (sval_cmp(tmp
->min
, min
) >= 0) {
1162 add_range(&ret
, max
, tmp
->max
);
1163 } else if (sval_cmp(tmp
->max
, max
) <= 0) {
1165 add_range(&ret
, tmp
->min
, min
);
1169 add_range(&ret
, tmp
->min
, min
);
1170 add_range(&ret
, max
, tmp
->max
);
1172 } END_FOR_EACH_PTR(tmp
);
1176 int ranges_equiv(struct data_range
*one
, struct data_range
*two
)
1182 if (sval_cmp(one
->min
, two
->min
) != 0)
1184 if (sval_cmp(one
->max
, two
->max
) != 0)
1189 int rl_equiv(struct range_list
*one
, struct range_list
*two
)
1191 struct data_range
*one_range
;
1192 struct data_range
*two_range
;
1197 PREPARE_PTR_LIST(one
, one_range
);
1198 PREPARE_PTR_LIST(two
, two_range
);
1200 if (!one_range
&& !two_range
)
1202 if (!ranges_equiv(one_range
, two_range
))
1204 NEXT_PTR_LIST(one_range
);
1205 NEXT_PTR_LIST(two_range
);
1207 FINISH_PTR_LIST(two_range
);
1208 FINISH_PTR_LIST(one_range
);
1213 int true_comparison_range(struct data_range
*left
, int comparison
, struct data_range
*right
)
1215 switch (comparison
) {
1217 case SPECIAL_UNSIGNED_LT
:
1218 if (sval_cmp(left
->min
, right
->max
) < 0)
1221 case SPECIAL_UNSIGNED_LTE
:
1223 if (sval_cmp(left
->min
, right
->max
) <= 0)
1227 if (sval_cmp(left
->max
, right
->min
) < 0)
1229 if (sval_cmp(left
->min
, right
->max
) > 0)
1232 case SPECIAL_UNSIGNED_GTE
:
1234 if (sval_cmp(left
->max
, right
->min
) >= 0)
1238 case SPECIAL_UNSIGNED_GT
:
1239 if (sval_cmp(left
->max
, right
->min
) > 0)
1242 case SPECIAL_NOTEQUAL
:
1243 if (sval_cmp(left
->min
, left
->max
) != 0)
1245 if (sval_cmp(right
->min
, right
->max
) != 0)
1247 if (sval_cmp(left
->min
, right
->min
) != 0)
1251 sm_perror("unhandled comparison %d", comparison
);
1257 int true_comparison_range_LR(int comparison
, struct data_range
*var
, struct data_range
*val
, int left
)
1260 return true_comparison_range(var
, comparison
, val
);
1262 return true_comparison_range(val
, comparison
, var
);
1265 static int false_comparison_range_sval(struct data_range
*left
, int comparison
, struct data_range
*right
)
1267 switch (comparison
) {
1269 case SPECIAL_UNSIGNED_LT
:
1270 if (sval_cmp(left
->max
, right
->min
) >= 0)
1273 case SPECIAL_UNSIGNED_LTE
:
1275 if (sval_cmp(left
->max
, right
->min
) > 0)
1279 if (sval_cmp(left
->min
, left
->max
) != 0)
1281 if (sval_cmp(right
->min
, right
->max
) != 0)
1283 if (sval_cmp(left
->min
, right
->min
) != 0)
1286 case SPECIAL_UNSIGNED_GTE
:
1288 if (sval_cmp(left
->min
, right
->max
) < 0)
1292 case SPECIAL_UNSIGNED_GT
:
1293 if (sval_cmp(left
->min
, right
->max
) <= 0)
1296 case SPECIAL_NOTEQUAL
:
1297 if (sval_cmp(left
->max
, right
->min
) < 0)
1299 if (sval_cmp(left
->min
, right
->max
) > 0)
1303 sm_perror("unhandled comparison %d", comparison
);
1309 int false_comparison_range_LR(int comparison
, struct data_range
*var
, struct data_range
*val
, int left
)
1312 return false_comparison_range_sval(var
, comparison
, val
);
1314 return false_comparison_range_sval(val
, comparison
, var
);
1317 int possibly_true(struct expression
*left
, int comparison
, struct expression
*right
)
1319 struct range_list
*rl_left
, *rl_right
;
1320 struct data_range
*tmp_left
, *tmp_right
;
1321 struct symbol
*type
;
1323 if (comparison
== UNKNOWN_COMPARISON
)
1325 if (!get_implied_rl(left
, &rl_left
))
1327 if (!get_implied_rl(right
, &rl_right
))
1330 type
= rl_type(rl_left
);
1331 if (type_positive_bits(type
) < type_positive_bits(rl_type(rl_right
)))
1332 type
= rl_type(rl_right
);
1333 if (type_positive_bits(type
) < 31)
1336 rl_left
= cast_rl(type
, rl_left
);
1337 rl_right
= cast_rl(type
, rl_right
);
1339 FOR_EACH_PTR(rl_left
, tmp_left
) {
1340 FOR_EACH_PTR(rl_right
, tmp_right
) {
1341 if (true_comparison_range(tmp_left
, comparison
, tmp_right
))
1343 } END_FOR_EACH_PTR(tmp_right
);
1344 } END_FOR_EACH_PTR(tmp_left
);
1348 int possibly_false(struct expression
*left
, int comparison
, struct expression
*right
)
1350 struct range_list
*rl_left
, *rl_right
;
1351 struct data_range
*tmp_left
, *tmp_right
;
1352 struct symbol
*type
;
1354 if (!get_implied_rl(left
, &rl_left
))
1356 if (!get_implied_rl(right
, &rl_right
))
1359 type
= rl_type(rl_left
);
1360 if (type_positive_bits(type
) < type_positive_bits(rl_type(rl_right
)))
1361 type
= rl_type(rl_right
);
1362 if (type_positive_bits(type
) < 31)
1365 rl_left
= cast_rl(type
, rl_left
);
1366 rl_right
= cast_rl(type
, rl_right
);
1368 FOR_EACH_PTR(rl_left
, tmp_left
) {
1369 FOR_EACH_PTR(rl_right
, tmp_right
) {
1370 if (false_comparison_range_sval(tmp_left
, comparison
, tmp_right
))
1372 } END_FOR_EACH_PTR(tmp_right
);
1373 } END_FOR_EACH_PTR(tmp_left
);
1377 int possibly_true_rl(struct range_list
*left_ranges
, int comparison
, struct range_list
*right_ranges
)
1379 struct data_range
*left_tmp
, *right_tmp
;
1380 struct symbol
*type
;
1382 if (!left_ranges
|| !right_ranges
|| comparison
== UNKNOWN_COMPARISON
)
1385 type
= rl_type(left_ranges
);
1386 if (type_positive_bits(type
) < type_positive_bits(rl_type(right_ranges
)))
1387 type
= rl_type(right_ranges
);
1388 if (type_positive_bits(type
) < 31)
1391 left_ranges
= cast_rl(type
, left_ranges
);
1392 right_ranges
= cast_rl(type
, right_ranges
);
1394 FOR_EACH_PTR(left_ranges
, left_tmp
) {
1395 FOR_EACH_PTR(right_ranges
, right_tmp
) {
1396 if (true_comparison_range(left_tmp
, comparison
, right_tmp
))
1398 } END_FOR_EACH_PTR(right_tmp
);
1399 } END_FOR_EACH_PTR(left_tmp
);
1403 int possibly_false_rl(struct range_list
*left_ranges
, int comparison
, struct range_list
*right_ranges
)
1405 struct data_range
*left_tmp
, *right_tmp
;
1406 struct symbol
*type
;
1408 if (!left_ranges
|| !right_ranges
|| comparison
== UNKNOWN_COMPARISON
)
1411 type
= rl_type(left_ranges
);
1412 if (type_positive_bits(type
) < type_positive_bits(rl_type(right_ranges
)))
1413 type
= rl_type(right_ranges
);
1414 if (type_positive_bits(type
) < 31)
1417 left_ranges
= cast_rl(type
, left_ranges
);
1418 right_ranges
= cast_rl(type
, right_ranges
);
1420 FOR_EACH_PTR(left_ranges
, left_tmp
) {
1421 FOR_EACH_PTR(right_ranges
, right_tmp
) {
1422 if (false_comparison_range_sval(left_tmp
, comparison
, right_tmp
))
1424 } END_FOR_EACH_PTR(right_tmp
);
1425 } END_FOR_EACH_PTR(left_tmp
);
1429 /* FIXME: the _rl here stands for right left so really it should be _lr */
1430 int possibly_true_rl_LR(int comparison
, struct range_list
*a
, struct range_list
*b
, int left
)
1433 return possibly_true_rl(a
, comparison
, b
);
1435 return possibly_true_rl(b
, comparison
, a
);
1438 int possibly_false_rl_LR(int comparison
, struct range_list
*a
, struct range_list
*b
, int left
)
1441 return possibly_false_rl(a
, comparison
, b
);
1443 return possibly_false_rl(b
, comparison
, a
);
1446 int rl_has_sval(struct range_list
*rl
, sval_t sval
)
1448 struct data_range
*tmp
;
1450 FOR_EACH_PTR(rl
, tmp
) {
1451 if (sval_cmp(tmp
->min
, sval
) <= 0 &&
1452 sval_cmp(tmp
->max
, sval
) >= 0)
1454 } END_FOR_EACH_PTR(tmp
);
1458 void tack_on(struct range_list
**list
, struct data_range
*drange
)
1460 add_ptr_list(list
, drange
);
1463 void push_rl(struct range_list_stack
**rl_stack
, struct range_list
*rl
)
1465 add_ptr_list(rl_stack
, rl
);
1468 struct range_list
*pop_rl(struct range_list_stack
**rl_stack
)
1470 struct range_list
*rl
;
1472 rl
= last_ptr_list((struct ptr_list
*)*rl_stack
);
1473 delete_ptr_list_last((struct ptr_list
**)rl_stack
);
1477 struct range_list
*top_rl(struct range_list_stack
*rl_stack
)
1479 struct range_list
*rl
;
1481 rl
= last_ptr_list((struct ptr_list
*)rl_stack
);
1485 void filter_top_rl(struct range_list_stack
**rl_stack
, struct range_list
*filter
)
1487 struct range_list
*rl
;
1489 rl
= pop_rl(rl_stack
);
1490 rl
= rl_filter(rl
, filter
);
1491 push_rl(rl_stack
, rl
);
1494 struct range_list
*rl_truncate_cast(struct symbol
*type
, struct range_list
*rl
)
1496 struct data_range
*tmp
;
1497 struct range_list
*ret
= NULL
;
1503 if (!type
|| type
== rl_type(rl
))
1506 FOR_EACH_PTR(rl
, tmp
) {
1509 if (type_bits(type
) < type_bits(rl_type(rl
))) {
1510 min
.uvalue
= tmp
->min
.uvalue
& ((1ULL << type_bits(type
)) - 1);
1511 max
.uvalue
= tmp
->max
.uvalue
& ((1ULL << type_bits(type
)) - 1);
1513 if (sval_cmp(min
, max
) > 0) {
1514 min
= sval_cast(type
, min
);
1515 max
= sval_cast(type
, max
);
1517 add_range_t(type
, &ret
, min
, max
);
1518 } END_FOR_EACH_PTR(tmp
);
1523 int rl_fits_in_type(struct range_list
*rl
, struct symbol
*type
)
1525 if (type_bits(rl_type(rl
)) <= type_bits(type
))
1527 if (sval_cmp(rl_max(rl
), sval_type_max(type
)) > 0)
1529 if (sval_is_negative(rl_min(rl
)) &&
1530 sval_cmp(rl_min(rl
), sval_type_min(type
)) < 0)
1535 static int rl_type_consistent(struct range_list
*rl
)
1537 struct data_range
*tmp
;
1538 struct symbol
*type
;
1541 FOR_EACH_PTR(rl
, tmp
) {
1542 if (type
!= tmp
->min
.type
|| type
!= tmp
->max
.type
)
1544 } END_FOR_EACH_PTR(tmp
);
1548 static struct range_list
*cast_to_bool(struct range_list
*rl
)
1550 struct data_range
*tmp
;
1551 struct range_list
*ret
= NULL
;
1554 sval_t min
= { .type
= &bool_ctype
};
1555 sval_t max
= { .type
= &bool_ctype
};
1557 FOR_EACH_PTR(rl
, tmp
) {
1558 if (tmp
->min
.value
|| tmp
->max
.value
)
1560 if (sval_is_negative(tmp
->min
) &&
1561 sval_is_negative(tmp
->max
))
1563 if (tmp
->min
.value
== 0 ||
1564 tmp
->max
.value
== 0)
1566 if (sval_is_negative(tmp
->min
) &&
1569 } END_FOR_EACH_PTR(tmp
);
1576 add_range(&ret
, min
, max
);
1580 struct range_list
*cast_rl(struct symbol
*type
, struct range_list
*rl
)
1582 struct data_range
*tmp
;
1583 struct range_list
*ret
= NULL
;
1590 if (!rl_is_sane(rl
))
1591 return alloc_whole_rl(type
);
1592 if (type
== rl_type(rl
) && rl_type_consistent(rl
))
1595 if (type
== &bool_ctype
)
1596 return cast_to_bool(rl
);
1598 FOR_EACH_PTR(rl
, tmp
) {
1599 add_range_t(type
, &ret
, tmp
->min
, tmp
->max
);
1600 } END_FOR_EACH_PTR(tmp
);
1603 return alloc_whole_rl(type
);
1609 * This is the opposite of rl_intersection().
1611 struct range_list
*rl_filter(struct range_list
*rl
, struct range_list
*filter
)
1613 struct data_range
*tmp
;
1615 FOR_EACH_PTR(filter
, tmp
) {
1616 rl
= remove_range(rl
, tmp
->min
, tmp
->max
);
1617 } END_FOR_EACH_PTR(tmp
);
1622 struct range_list
*do_intersection(struct range_list
*one_rl
, struct range_list
*two_rl
)
1624 struct data_range
*one
, *two
;
1625 struct range_list
*ret
= NULL
;
1628 PREPARE_PTR_LIST(one_rl
, one
);
1629 PREPARE_PTR_LIST(two_rl
, two
);
1634 if (sval_cmp(one
->max
, two
->min
) < 0) {
1638 if (sval_cmp(one
->min
, two
->min
) < 0 && sval_cmp(one
->max
, two
->max
) <= 0) {
1639 add_range(&ret
, two
->min
, one
->max
);
1643 if (sval_cmp(one
->min
, two
->min
) >= 0 && sval_cmp(one
->max
, two
->max
) <= 0) {
1644 add_range(&ret
, one
->min
, one
->max
);
1648 if (sval_cmp(one
->min
, two
->min
) < 0 && sval_cmp(one
->max
, two
->max
) > 0) {
1649 add_range(&ret
, two
->min
, two
->max
);
1653 if (sval_cmp(one
->min
, two
->max
) <= 0 && sval_cmp(one
->max
, two
->max
) > 0) {
1654 add_range(&ret
, one
->min
, two
->max
);
1658 if (sval_cmp(one
->min
, two
->max
) <= 0) {
1659 sm_fatal("error calculating intersection of '%s' and '%s'", show_rl(one_rl
), show_rl(two_rl
));
1665 FINISH_PTR_LIST(two
);
1666 FINISH_PTR_LIST(one
);
1671 struct range_list
*rl_intersection(struct range_list
*one
, struct range_list
*two
)
1673 struct range_list
*ret
;
1674 struct symbol
*ret_type
;
1675 struct symbol
*small_type
;
1676 struct symbol
*large_type
;
1681 ret_type
= rl_type(one
);
1682 small_type
= rl_type(one
);
1683 large_type
= rl_type(two
);
1685 if (type_bits(rl_type(two
)) < type_bits(small_type
)) {
1686 small_type
= rl_type(two
);
1687 large_type
= rl_type(one
);
1690 one
= cast_rl(large_type
, one
);
1691 two
= cast_rl(large_type
, two
);
1693 ret
= do_intersection(one
, two
);
1694 return cast_rl(ret_type
, ret
);
1697 static struct range_list
*handle_mod_rl(struct range_list
*left
, struct range_list
*right
)
1702 max
= rl_max(right
);
1703 if (sval_is_max(max
))
1708 if (sval_is_negative(max
))
1710 if (sval_cmp(rl_max(left
), max
) < 0)
1714 return alloc_rl(zero
, max
);
1717 static struct range_list
*get_neg_rl(struct range_list
*rl
)
1719 struct data_range
*tmp
;
1720 struct data_range
*new;
1721 struct range_list
*ret
= NULL
;
1725 if (sval_is_positive(rl_min(rl
)))
1728 FOR_EACH_PTR(rl
, tmp
) {
1729 if (sval_is_positive(tmp
->min
))
1731 if (sval_is_positive(tmp
->max
)) {
1732 new = alloc_range(tmp
->min
, tmp
->max
);
1733 new->max
.value
= -1;
1734 add_range(&ret
, new->min
, new->max
);
1737 add_range(&ret
, tmp
->min
, tmp
->max
);
1738 } END_FOR_EACH_PTR(tmp
);
1743 static struct range_list
*get_pos_rl(struct range_list
*rl
)
1745 struct data_range
*tmp
;
1746 struct data_range
*new;
1747 struct range_list
*ret
= NULL
;
1751 if (sval_is_negative(rl_max(rl
)))
1754 FOR_EACH_PTR(rl
, tmp
) {
1755 if (sval_is_negative(tmp
->max
))
1757 if (sval_is_positive(tmp
->min
)) {
1758 add_range(&ret
, tmp
->min
, tmp
->max
);
1761 new = alloc_range(tmp
->min
, tmp
->max
);
1763 add_range(&ret
, new->min
, new->max
);
1764 } END_FOR_EACH_PTR(tmp
);
1769 static struct range_list
*divide_rl_helper(struct range_list
*left
, struct range_list
*right
)
1771 sval_t right_min
, right_max
;
1774 if (!left
|| !right
)
1777 /* let's assume we never divide by zero */
1778 right_min
= rl_min(right
);
1779 right_max
= rl_max(right
);
1780 if (right_min
.value
== 0 && right_max
.value
== 0)
1782 if (right_min
.value
== 0)
1783 right_min
.value
= 1;
1784 if (right_max
.value
== 0)
1785 right_max
.value
= -1;
1787 max
= sval_binop(rl_max(left
), '/', right_min
);
1788 min
= sval_binop(rl_min(left
), '/', right_max
);
1790 return alloc_rl(min
, max
);
1793 static struct range_list
*handle_divide_rl(struct range_list
*left
, struct range_list
*right
)
1795 struct range_list
*left_neg
, *left_pos
, *right_neg
, *right_pos
;
1796 struct range_list
*neg_neg
, *neg_pos
, *pos_neg
, *pos_pos
;
1797 struct range_list
*ret
;
1799 if (is_whole_rl(right
))
1802 left_neg
= get_neg_rl(left
);
1803 left_pos
= get_pos_rl(left
);
1804 right_neg
= get_neg_rl(right
);
1805 right_pos
= get_pos_rl(right
);
1807 neg_neg
= divide_rl_helper(left_neg
, right_neg
);
1808 neg_pos
= divide_rl_helper(left_neg
, right_pos
);
1809 pos_neg
= divide_rl_helper(left_pos
, right_neg
);
1810 pos_pos
= divide_rl_helper(left_pos
, right_pos
);
1812 ret
= rl_union(neg_neg
, neg_pos
);
1813 ret
= rl_union(ret
, pos_neg
);
1814 return rl_union(ret
, pos_pos
);
1817 static struct range_list
*ptr_add_mult(struct range_list
*left
, int op
, struct range_list
*right
)
1819 struct range_list
*ret
;
1820 sval_t l_sval
, r_sval
, res
;
1823 * This function is sort of the wrong API because it takes two pointer
1824 * and adds them together. The caller is expected to figure out
1825 * alignment. Neither of those are the correct things to do.
1827 * Really this function is quite bogus...
1830 if (rl_to_sval(left
, &l_sval
) && rl_to_sval(right
, &r_sval
)) {
1831 res
= sval_binop(l_sval
, op
, r_sval
);
1832 return alloc_rl(res
, res
);
1835 if (rl_min(left
).value
!= 0 || rl_max(right
).value
!= 0) {
1836 ret
= alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
1837 return cast_rl(rl_type(left
), ret
);
1840 return alloc_whole_rl(rl_type(left
));
1843 static struct range_list
*handle_add_mult_rl(struct range_list
*left
, int op
, struct range_list
*right
)
1847 if (type_is_ptr(rl_type(left
)) || type_is_ptr(rl_type(right
)))
1848 return ptr_add_mult(left
, op
, right
);
1850 if (sval_binop_overflows(rl_min(left
), op
, rl_min(right
)))
1852 min
= sval_binop(rl_min(left
), op
, rl_min(right
));
1854 if (sval_binop_overflows(rl_max(left
), op
, rl_max(right
)))
1856 max
= sval_binop(rl_max(left
), op
, rl_max(right
));
1858 return alloc_rl(min
, max
);
1861 static struct range_list
*handle_sub_rl(struct range_list
*left_orig
, struct range_list
*right_orig
)
1863 struct range_list
*left_rl
, *right_rl
;
1864 struct symbol
*type
;
1866 sval_t min_ll
, max_ll
, res_ll
;
1869 /* TODO: These things should totally be using dranges where possible */
1871 if (!left_orig
|| !right_orig
)
1875 if (type_positive_bits(rl_type(left_orig
)) > type_positive_bits(type
))
1876 type
= rl_type(left_orig
);
1877 if (type_positive_bits(rl_type(right_orig
)) > type_positive_bits(type
))
1878 type
= rl_type(right_orig
);
1880 left_rl
= cast_rl(type
, left_orig
);
1881 right_rl
= cast_rl(type
, right_orig
);
1883 max
= rl_max(left_rl
);
1884 min
= sval_type_min(type
);
1886 min_ll
= rl_min(left_rl
);
1887 min_ll
.type
= &llong_ctype
;
1888 max_ll
= rl_max(right_rl
);
1889 max_ll
.type
= &llong_ctype
;
1891 res_ll
.value
= min_ll
.value
- max_ll
.value
;
1893 if (!sval_binop_overflows(rl_min(left_rl
), '-', rl_max(right_rl
))) {
1894 tmp
= sval_binop(rl_min(left_rl
), '-', rl_max(right_rl
));
1895 if (sval_cmp(tmp
, min
) > 0)
1897 } else if (type_positive_bits(type
) < 63 &&
1898 !sval_binop_overflows(min_ll
, '-', max_ll
) &&
1899 (min
.value
!= 0 && sval_cmp(res_ll
, min
) >= 0)) {
1900 struct range_list
*left_casted
, *right_casted
, *result
;
1902 left_casted
= cast_rl(&llong_ctype
, left_orig
);
1903 right_casted
= cast_rl(&llong_ctype
, right_orig
);
1904 result
= handle_sub_rl(left_casted
, right_casted
);
1905 return cast_rl(type
, result
);
1908 if (!sval_is_max(rl_max(left_rl
))) {
1909 tmp
= sval_binop(rl_max(left_rl
), '-', rl_min(right_rl
));
1910 if (sval_cmp(tmp
, max
) < 0)
1914 if (sval_is_min(min
) && sval_is_max(max
))
1917 return alloc_rl(min
, max
);
1920 static unsigned long long rl_bits_always_set(struct range_list
*rl
)
1922 return sval_fls_mask(rl_min(rl
));
1925 static unsigned long long rl_bits_maybe_set(struct range_list
*rl
)
1927 return sval_fls_mask(rl_max(rl
));
1930 static struct range_list
*handle_OR_rl(struct range_list
*left
, struct range_list
*right
)
1932 unsigned long long left_min
, left_max
, right_min
, right_max
;
1936 if ((rl_to_sval(left
, &sval
) || rl_to_sval(right
, &sval
)) &&
1937 !sval_binop_overflows(rl_max(left
), '+', rl_max(right
)))
1938 return rl_binop(left
, '+', right
);
1940 left_min
= rl_bits_always_set(left
);
1941 left_max
= rl_bits_maybe_set(left
);
1942 right_min
= rl_bits_always_set(right
);
1943 right_max
= rl_bits_maybe_set(right
);
1945 min
.type
= max
.type
= &ullong_ctype
;
1946 min
.uvalue
= left_min
| right_min
;
1947 max
.uvalue
= left_max
| right_max
;
1949 return cast_rl(rl_type(left
), alloc_rl(min
, max
));
1952 static struct range_list
*handle_XOR_rl(struct range_list
*left
, struct range_list
*right
)
1954 unsigned long long left_set
, left_maybe
;
1955 unsigned long long right_set
, right_maybe
;
1958 left_set
= rl_bits_always_set(left
);
1959 left_maybe
= rl_bits_maybe_set(left
);
1961 right_set
= rl_bits_always_set(right
);
1962 right_maybe
= rl_bits_maybe_set(right
);
1964 zero
= max
= rl_min(left
);
1966 max
.uvalue
= fls_mask((left_maybe
| right_maybe
) ^ (left_set
& right_set
));
1968 return cast_rl(rl_type(left
), alloc_rl(zero
, max
));
1971 static sval_t
sval_lowest_set_bit(sval_t sval
)
1973 sval_t ret
= { .type
= sval
.type
};
1976 for (i
= 0; i
< 64; i
++) {
1977 if (sval
.uvalue
& 1ULL << i
) {
1978 ret
.uvalue
= (1ULL << i
);
1985 static struct range_list
*handle_AND_rl(struct range_list
*left
, struct range_list
*right
)
1987 struct bit_info
*one
, *two
;
1988 struct range_list
*rl
;
1989 sval_t min
, max
, zero
, bits_sval
;
1990 unsigned long long bits
;
1992 one
= rl_to_binfo(left
);
1993 two
= rl_to_binfo(right
);
1994 bits
= one
->possible
& two
->possible
;
1995 bits_sval
= rl_max(left
);
1996 bits_sval
.uvalue
= bits
;
1998 max
= sval_min_nonneg(rl_max(left
), rl_max(right
));
1999 min
= sval_lowest_set_bit(bits_sval
);
2001 rl
= alloc_rl(min
, max
);
2005 add_range(&rl
, zero
, zero
);
2010 static struct range_list
*handle_lshift(struct range_list
*left_orig
, struct range_list
*right_orig
)
2012 struct range_list
*left
;
2013 struct data_range
*tmp
;
2014 struct range_list
*ret
= NULL
;
2015 sval_t zero
= { .type
= rl_type(left_orig
), };
2016 sval_t shift
, min
, max
;
2017 bool add_zero
= false;
2019 if (!rl_to_sval(right_orig
, &shift
) || sval_is_negative(shift
))
2021 if (shift
.value
== 0)
2024 /* Cast to unsigned for easier left shift math */
2025 if (type_positive_bits(rl_type(left_orig
)) < 32)
2026 left
= cast_rl(&uint_ctype
, left_orig
);
2027 else if(type_positive_bits(rl_type(left_orig
)) == 63)
2028 left
= cast_rl(&ullong_ctype
, left_orig
);
2032 FOR_EACH_PTR(left
, tmp
) {
2036 if (min
.value
== 0 || max
.value
> sval_type_max(max
.type
).uvalue
>> shift
.uvalue
)
2038 if (min
.value
== 0 && max
.value
== 0)
2042 min
= sval_binop(min
, SPECIAL_LEFTSHIFT
, shift
);
2043 max
= sval_binop(max
, SPECIAL_LEFTSHIFT
, shift
);
2044 add_range(&ret
, min
, max
);
2045 } END_FOR_EACH_PTR(tmp
);
2047 if (!rl_fits_in_type(ret
, rl_type(left_orig
)))
2049 ret
= cast_rl(rl_type(left_orig
), ret
);
2051 add_range(&ret
, zero
, zero
);
2056 static struct range_list
*handle_rshift(struct range_list
*left_orig
, struct range_list
*right_orig
)
2058 struct data_range
*tmp
;
2059 struct range_list
*ret
= NULL
;
2060 sval_t shift
, min
, max
;
2062 if (!rl_to_sval(right_orig
, &shift
) || sval_is_negative(shift
))
2064 if (shift
.value
== 0)
2067 FOR_EACH_PTR(left_orig
, tmp
) {
2068 min
= sval_binop(tmp
->min
, SPECIAL_RIGHTSHIFT
, shift
);
2069 max
= sval_binop(tmp
->max
, SPECIAL_RIGHTSHIFT
, shift
);
2070 add_range(&ret
, min
, max
);
2071 } END_FOR_EACH_PTR(tmp
);
2076 struct range_list
*rl_binop(struct range_list
*left
, int op
, struct range_list
*right
)
2078 struct symbol
*cast_type
;
2079 sval_t left_sval
, right_sval
;
2080 struct range_list
*ret
= NULL
;
2082 cast_type
= rl_type(left
);
2083 if (sval_type_max(rl_type(left
)).uvalue
< sval_type_max(rl_type(right
)).uvalue
)
2084 cast_type
= rl_type(right
);
2085 if (sval_type_max(cast_type
).uvalue
< INT_MAX
)
2086 cast_type
= &int_ctype
;
2088 left
= cast_rl(cast_type
, left
);
2089 right
= cast_rl(cast_type
, right
);
2091 if (!left
&& !right
)
2094 if (rl_to_sval(left
, &left_sval
) && rl_to_sval(right
, &right_sval
)) {
2095 sval_t val
= sval_binop(left_sval
, op
, right_sval
);
2096 return alloc_rl(val
, val
);
2101 ret
= handle_mod_rl(left
, right
);
2104 ret
= handle_divide_rl(left
, right
);
2108 ret
= handle_add_mult_rl(left
, op
, right
);
2111 ret
= handle_OR_rl(left
, right
);
2114 ret
= handle_XOR_rl(left
, right
);
2117 ret
= handle_AND_rl(left
, right
);
2120 ret
= handle_sub_rl(left
, right
);
2122 case SPECIAL_RIGHTSHIFT
:
2123 return handle_rshift(left
, right
);
2124 case SPECIAL_LEFTSHIFT
:
2125 return handle_lshift(left
, right
);
2131 void free_data_info_allocs(void)
2133 struct allocator_struct
*desc
= &data_info_allocator
;
2134 struct allocation_blob
*blob
= desc
->blobs
;
2140 desc
->allocations
= 0;
2141 desc
->total_bytes
= 0;
2142 desc
->useful_bytes
= 0;
2143 desc
->freelist
= NULL
;
2145 struct allocation_blob
*next
= blob
->next
;
2146 blob_free(blob
, desc
->chunking
);
2149 clear_array_values_cache();
2150 clear_type_value_cache();
2151 clear_data_range_alloc();
2154 void split_comparison_rl(struct range_list
*left_orig
, int op
, struct range_list
*right_orig
,
2155 struct range_list
**left_true_rl
, struct range_list
**left_false_rl
,
2156 struct range_list
**right_true_rl
, struct range_list
**right_false_rl
)
2158 struct range_list
*left_true
, *left_false
;
2159 struct range_list
*right_true
, *right_false
;
2162 min
= sval_type_min(rl_type(left_orig
));
2163 max
= sval_type_max(rl_type(left_orig
));
2165 left_true
= clone_rl(left_orig
);
2166 left_false
= clone_rl(left_orig
);
2167 right_true
= clone_rl(right_orig
);
2168 right_false
= clone_rl(right_orig
);
2172 case SPECIAL_UNSIGNED_LT
:
2173 left_true
= remove_range(left_orig
, rl_max(right_orig
), max
);
2174 if (!sval_is_min(rl_min(right_orig
))) {
2175 left_false
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
2178 right_true
= remove_range(right_orig
, min
, rl_min(left_orig
));
2179 if (!sval_is_max(rl_max(left_orig
)))
2180 right_false
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
2182 case SPECIAL_UNSIGNED_LTE
:
2184 if (!sval_is_max(rl_max(right_orig
)))
2185 left_true
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
2186 left_false
= remove_range(left_orig
, min
, rl_min(right_orig
));
2188 if (!sval_is_min(rl_min(left_orig
)))
2189 right_true
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
2190 right_false
= remove_range(right_orig
, rl_max(left_orig
), max
);
2192 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
2193 left_false
= remove_range(left_false
, rl_min(left_orig
), rl_min(left_orig
));
2194 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
2195 right_false
= remove_range(right_false
, rl_max(left_orig
), rl_max(left_orig
));
2198 left_true
= rl_intersection(left_orig
, right_orig
);
2199 right_true
= clone_rl(left_true
);
2201 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
2202 left_false
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
2203 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
2204 right_false
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
2206 case SPECIAL_UNSIGNED_GTE
:
2208 if (!sval_is_min(rl_min(right_orig
)))
2209 left_true
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
2210 left_false
= remove_range(left_orig
, rl_max(right_orig
), max
);
2212 if (!sval_is_max(rl_max(left_orig
)))
2213 right_true
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
2214 right_false
= remove_range(right_orig
, min
, rl_min(left_orig
));
2216 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
2217 right_false
= remove_range(right_false
, rl_min(left_orig
), rl_min(left_orig
));
2218 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
2219 left_false
= remove_range(left_false
, rl_max(left_orig
), rl_max(left_orig
));
2222 case SPECIAL_UNSIGNED_GT
:
2223 left_true
= remove_range(left_orig
, min
, rl_min(right_orig
));
2224 if (!sval_is_max(rl_max(right_orig
)))
2225 left_false
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
2227 right_true
= remove_range(right_orig
, rl_max(left_orig
), max
);
2228 if (!sval_is_min(rl_min(left_orig
)))
2229 right_false
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
2231 case SPECIAL_NOTEQUAL
:
2232 left_false
= rl_intersection(left_orig
, right_orig
);
2233 right_false
= clone_rl(left_false
);
2235 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
2236 left_true
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
2237 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
2238 right_true
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
2241 sm_perror(" unhandled comparison %d", op
);
2245 *left_true_rl
= left_true
;
2246 *left_false_rl
= left_false
;
2248 if (right_true_rl
) {
2249 *right_true_rl
= right_true
;
2250 *right_false_rl
= right_false
;