2 * Copyright (C) 2010 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_slist.h"
21 #include "smatch_extra.h"
23 static struct range_list
*_get_rl(struct expression
*expr
, int implied
, int *recurse_cnt
);
24 static struct range_list
*handle_variable(struct expression
*expr
, int implied
, int *recurse_cnt
);
25 static struct range_list
*(*custom_handle_variable
)(struct expression
*expr
);
27 static int get_implied_value_internal(struct expression
*expr
, sval_t
*sval
, int *recurse_cnt
);
28 static int get_absolute_rl_internal(struct expression
*expr
, struct range_list
**rl
, int *recurse_cnt
);
30 static sval_t zero
= {.type
= &int_ctype
, {.value
= 0} };
31 static sval_t one
= {.type
= &int_ctype
, {.value
= 1} };
33 struct range_list
*rl_zero(void)
35 static struct range_list
*zero_perm
;
38 zero_perm
= clone_rl_permanent(alloc_rl(zero
, zero
));
42 struct range_list
*rl_one(void)
44 static struct range_list
*one_perm
;
47 one_perm
= clone_rl_permanent(alloc_rl(one
, one
));
61 static struct range_list
*last_stmt_rl(struct statement
*stmt
, int implied
, int *recurse_cnt
)
63 struct expression
*expr
;
68 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
69 if (stmt
->type
== STMT_LABEL
) {
70 if (stmt
->label_statement
&&
71 stmt
->label_statement
->type
== STMT_EXPRESSION
)
72 expr
= stmt
->label_statement
->expression
;
75 } else if (stmt
->type
== STMT_EXPRESSION
) {
76 expr
= stmt
->expression
;
80 return _get_rl(expr
, implied
, recurse_cnt
);
83 static struct range_list
*handle_expression_statement_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
85 return last_stmt_rl(get_expression_statement(expr
), implied
, recurse_cnt
);
88 static struct range_list
*handle_ampersand_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
90 struct range_list
*rl
;
93 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
95 if (get_mtag_sval(expr
, &sval
))
96 return alloc_rl(sval
, sval
);
97 if (get_address_rl(expr
, &rl
))
99 return alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
102 static struct range_list
*handle_negate_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
104 if (known_condition_true(expr
->unop
))
106 if (known_condition_false(expr
->unop
))
109 if (implied
== RL_EXACT
)
112 if (implied_condition_true(expr
->unop
))
114 if (implied_condition_false(expr
->unop
))
116 return alloc_rl(zero
, one
);
119 static struct range_list
*handle_bitwise_negate(struct expression
*expr
, int implied
, int *recurse_cnt
)
121 struct range_list
*rl
;
124 rl
= _get_rl(expr
->unop
, implied
, recurse_cnt
);
125 if (!rl_to_sval(rl
, &sval
))
127 sval
= sval_preop(sval
, '~');
128 sval_cast(get_type(expr
->unop
), sval
);
129 return alloc_rl(sval
, sval
);
132 static struct range_list
*handle_minus_preop(struct expression
*expr
, int implied
, int *recurse_cnt
)
134 struct range_list
*rl
;
137 rl
= _get_rl(expr
->unop
, implied
, recurse_cnt
);
138 min
= sval_preop(rl_max(rl
), '-');
139 max
= sval_preop(rl_min(rl
), '-');
140 return alloc_rl(min
, max
);
143 static struct range_list
*handle_preop_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
147 return handle_ampersand_rl(expr
, implied
, recurse_cnt
);
149 return handle_negate_rl(expr
, implied
, recurse_cnt
);
151 return handle_bitwise_negate(expr
, implied
, recurse_cnt
);
153 return handle_minus_preop(expr
, implied
, recurse_cnt
);
155 return handle_variable(expr
, implied
, recurse_cnt
);
157 return handle_expression_statement_rl(expr
, implied
, recurse_cnt
);
163 static struct range_list
*handle_divide_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
165 struct range_list
*left_rl
, *right_rl
;
168 type
= get_type(expr
);
170 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
171 left_rl
= cast_rl(type
, left_rl
);
172 right_rl
= _get_rl(expr
->right
, implied
, recurse_cnt
);
173 right_rl
= cast_rl(type
, right_rl
);
175 if (!left_rl
|| !right_rl
)
178 if (implied
!= RL_REAL_ABSOLUTE
) {
179 if (is_whole_rl(left_rl
) || is_whole_rl(right_rl
))
183 return rl_binop(left_rl
, '/', right_rl
);
186 static int handle_offset_subtraction(struct expression
*expr
)
188 struct expression
*left
, *right
;
189 struct symbol
*left_sym
, *right_sym
;
191 int left_offset
, right_offset
;
193 type
= get_type(expr
);
194 if (!type
|| type
->type
!= SYM_PTR
)
196 type
= get_real_base_type(type
);
197 if (!type
|| (type_bits(type
) != 8 && (type
!= &void_ctype
)))
200 left
= strip_expr(expr
->left
);
201 right
= strip_expr(expr
->right
);
203 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '&')
205 left
= strip_expr(left
->unop
);
207 left_sym
= expr_to_sym(left
);
208 right_sym
= expr_to_sym(right
);
209 if (!left_sym
|| left_sym
!= right_sym
)
212 left_offset
= get_member_offset_from_deref(left
);
213 if (right
->type
== EXPR_SYMBOL
)
216 if (right
->type
!= EXPR_PREOP
|| right
->op
!= '&')
218 right
= strip_expr(right
->unop
);
219 right_offset
= get_member_offset_from_deref(right
);
221 if (left_offset
< 0 || right_offset
< 0)
224 return left_offset
- right_offset
;
227 static struct range_list
*handle_subtract_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
230 struct range_list
*left_orig
, *right_orig
;
231 struct range_list
*left_rl
, *right_rl
;
232 sval_t max
, min
, tmp
;
236 type
= get_type(expr
);
238 offset
= handle_offset_subtraction(expr
);
243 return alloc_rl(tmp
, tmp
);
246 comparison
= get_comparison(expr
->left
, expr
->right
);
248 left_orig
= _get_rl(expr
->left
, implied
, recurse_cnt
);
249 left_rl
= cast_rl(type
, left_orig
);
250 right_orig
= _get_rl(expr
->right
, implied
, recurse_cnt
);
251 right_rl
= cast_rl(type
, right_orig
);
253 if ((!left_rl
|| !right_rl
) &&
254 (implied
== RL_EXACT
|| implied
== RL_HARD
|| implied
== RL_FUZZY
))
258 left_rl
= alloc_whole_rl(type
);
260 right_rl
= alloc_whole_rl(type
);
262 /* negative values complicate everything fix this later */
263 if (sval_is_negative(rl_min(right_rl
)))
265 max
= rl_max(left_rl
);
266 min
= sval_type_min(type
);
268 switch (comparison
) {
270 case SPECIAL_UNSIGNED_GT
:
271 min
= sval_type_val(type
, 1);
272 max
= rl_max(left_rl
);
275 case SPECIAL_UNSIGNED_GTE
:
276 min
= sval_type_val(type
, 0);
277 max
= rl_max(left_rl
);
280 min
= sval_type_val(type
, 0);
281 max
= sval_type_val(type
, 0);
284 case SPECIAL_UNSIGNED_LT
:
285 max
= sval_type_val(type
, -1);
288 case SPECIAL_UNSIGNED_LTE
:
289 max
= sval_type_val(type
, 0);
292 if (!left_orig
|| !right_orig
)
294 return rl_binop(left_rl
, '-', right_rl
);
297 if (!sval_binop_overflows(rl_min(left_rl
), '-', rl_max(right_rl
))) {
298 tmp
= sval_binop(rl_min(left_rl
), '-', rl_max(right_rl
));
299 if (sval_cmp(tmp
, min
) > 0)
303 if (!sval_is_max(rl_max(left_rl
))) {
304 tmp
= sval_binop(rl_max(left_rl
), '-', rl_min(right_rl
));
305 if (sval_cmp(tmp
, max
) < 0)
309 if (sval_is_min(min
) && sval_is_max(max
))
312 return cast_rl(type
, alloc_rl(min
, max
));
315 static struct range_list
*handle_mod_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
317 struct range_list
*rl
;
318 sval_t left
, right
, sval
;
320 if (implied
== RL_EXACT
) {
321 if (!get_implied_value(expr
->right
, &right
))
323 if (!get_implied_value(expr
->left
, &left
))
325 sval
= sval_binop(left
, '%', right
);
326 return alloc_rl(sval
, sval
);
328 /* if we can't figure out the right side it's probably hopeless */
329 if (!get_implied_value_internal(expr
->right
, &right
, recurse_cnt
))
332 right
= sval_cast(get_type(expr
), right
);
335 rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
336 if (rl
&& rl_max(rl
).uvalue
< right
.uvalue
)
337 right
.uvalue
= rl_max(rl
).uvalue
;
339 return alloc_rl(sval_cast(right
.type
, zero
), right
);
342 static sval_t
sval_lowest_set_bit(sval_t sval
)
347 for (i
= 0; i
< 64; i
++) {
348 if (sval
.uvalue
& 1ULL << i
) {
351 sval
.uvalue
&= ~(1ULL << i
);
357 static struct range_list
*handle_bitwise_AND(struct expression
*expr
, int implied
, int *recurse_cnt
)
360 struct range_list
*left_rl
, *right_rl
;
364 if (implied
!= RL_IMPLIED
&& implied
!= RL_ABSOLUTE
&& implied
!= RL_REAL_ABSOLUTE
)
367 type
= get_type(expr
);
369 if (get_implied_value_internal(expr
->left
, &known
, recurse_cnt
)) {
372 min
= sval_lowest_set_bit(known
);
373 left_rl
= alloc_rl(min
, known
);
374 left_rl
= cast_rl(type
, left_rl
);
375 add_range(&left_rl
, sval_type_val(type
, 0), sval_type_val(type
, 0));
377 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
379 left_rl
= cast_rl(type
, left_rl
);
380 left_rl
= alloc_rl(sval_type_val(type
, 0), rl_max(left_rl
));
382 if (implied
== RL_HARD
)
384 left_rl
= alloc_whole_rl(type
);
388 new_recurse
= *recurse_cnt
;
389 if (*recurse_cnt
>= 200)
390 new_recurse
= 100; /* Let's try super hard to get the mask */
391 if (get_implied_value_internal(expr
->right
, &known
, &new_recurse
)) {
392 sval_t min
, left_max
, mod
;
394 *recurse_cnt
= new_recurse
;
396 min
= sval_lowest_set_bit(known
);
397 right_rl
= alloc_rl(min
, known
);
398 right_rl
= cast_rl(type
, right_rl
);
399 add_range(&right_rl
, sval_type_val(type
, 0), sval_type_val(type
, 0));
401 if (min
.value
!= 0) {
402 left_max
= rl_max(left_rl
);
403 mod
= sval_binop(left_max
, '%', min
);
405 left_max
= sval_binop(left_max
, '-', mod
);
407 if (left_max
.value
> 0 && sval_cmp(left_max
, rl_max(left_rl
)) < 0)
408 left_rl
= remove_range(left_rl
, left_max
, rl_max(left_rl
));
412 right_rl
= _get_rl(expr
->right
, implied
, recurse_cnt
);
414 right_rl
= cast_rl(type
, right_rl
);
415 right_rl
= alloc_rl(sval_type_val(type
, 0), rl_max(right_rl
));
417 if (implied
== RL_HARD
)
419 right_rl
= alloc_whole_rl(type
);
423 return rl_intersection(left_rl
, right_rl
);
426 static struct range_list
*use_rl_binop(struct expression
*expr
, int implied
, int *recurse_cnt
)
429 struct range_list
*left_rl
, *right_rl
;
431 if (implied
!= RL_IMPLIED
&& implied
!= RL_ABSOLUTE
&& implied
!= RL_REAL_ABSOLUTE
)
434 type
= get_type(expr
);
436 get_absolute_rl_internal(expr
->left
, &left_rl
, recurse_cnt
);
437 get_absolute_rl_internal(expr
->right
, &right_rl
, recurse_cnt
);
438 left_rl
= cast_rl(type
, left_rl
);
439 right_rl
= cast_rl(type
, right_rl
);
440 if (!left_rl
|| !right_rl
)
443 return rl_binop(left_rl
, expr
->op
, right_rl
);
446 static struct range_list
*handle_right_shift(struct expression
*expr
, int implied
, int *recurse_cnt
)
448 struct range_list
*left_rl
;
452 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
455 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
457 max
= rl_max(left_rl
);
458 min
= rl_min(left_rl
);
460 if (implied
== RL_FUZZY
)
462 max
= sval_type_max(get_type(expr
->left
));
463 min
= sval_type_val(get_type(expr
->left
), 0);
466 if (get_implied_value_internal(expr
->right
, &right
, recurse_cnt
)) {
467 min
= sval_binop(min
, SPECIAL_RIGHTSHIFT
, right
);
468 max
= sval_binop(max
, SPECIAL_RIGHTSHIFT
, right
);
469 } else if (!sval_is_negative(min
)) {
471 max
= sval_type_max(max
.type
);
476 return alloc_rl(min
, max
);
479 static struct range_list
*handle_left_shift(struct expression
*expr
, int implied
, int *recurse_cnt
)
481 struct range_list
*left_rl
, *res
;
486 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
488 /* this is hopeless without the right side */
489 if (!get_implied_value_internal(expr
->right
, &right
, recurse_cnt
))
491 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
493 max
= rl_max(left_rl
);
494 min
= rl_min(left_rl
);
495 if (min
.value
== 0) {
500 if (implied
== RL_FUZZY
)
502 max
= sval_type_max(get_type(expr
->left
));
503 min
= sval_type_val(get_type(expr
->left
), 1);
507 max
= sval_binop(max
, SPECIAL_LEFTSHIFT
, right
);
508 min
= sval_binop(min
, SPECIAL_LEFTSHIFT
, right
);
509 res
= alloc_rl(min
, max
);
511 res
= rl_union(res
, rl_zero());
515 static struct range_list
*handle_known_binop(struct expression
*expr
)
519 if (!get_value(expr
->left
, &left
))
521 if (!get_value(expr
->right
, &right
))
523 left
= sval_binop(left
, expr
->op
, right
);
524 return alloc_rl(left
, left
);
527 static int has_actual_ranges(struct range_list
*rl
)
529 struct data_range
*tmp
;
531 FOR_EACH_PTR(rl
, tmp
) {
532 if (sval_cmp(tmp
->min
, tmp
->max
) != 0)
534 } END_FOR_EACH_PTR(tmp
);
538 static struct range_list
*handle_implied_binop(struct range_list
*left_rl
, int op
, struct range_list
*right_rl
)
540 struct range_list
*res_rl
;
541 struct data_range
*left_drange
, *right_drange
;
544 if (!left_rl
|| !right_rl
)
546 if (has_actual_ranges(left_rl
))
548 if (has_actual_ranges(right_rl
))
551 if (ptr_list_size((struct ptr_list
*)left_rl
) * ptr_list_size((struct ptr_list
*)right_rl
) > 20)
556 FOR_EACH_PTR(left_rl
, left_drange
) {
557 FOR_EACH_PTR(right_rl
, right_drange
) {
558 if ((op
== '%' || op
== '/') &&
559 right_drange
->min
.value
== 0)
561 res
= sval_binop(left_drange
->min
, op
, right_drange
->min
);
562 add_range(&res_rl
, res
, res
);
563 } END_FOR_EACH_PTR(right_drange
);
564 } END_FOR_EACH_PTR(left_drange
);
569 static struct range_list
*handle_binop_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
571 struct smatch_state
*state
;
573 struct range_list
*left_rl
, *right_rl
, *rl
;
576 rl
= handle_known_binop(expr
);
579 if (implied
== RL_EXACT
)
582 if (custom_handle_variable
) {
583 rl
= custom_handle_variable(expr
);
588 state
= get_extra_state(expr
);
589 if (state
&& !is_whole_rl(estate_rl(state
))) {
590 if (implied
!= RL_HARD
|| estate_has_hard_max(state
))
591 return clone_rl(estate_rl(state
));
594 type
= get_type(expr
);
595 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
596 left_rl
= cast_rl(type
, left_rl
);
597 right_rl
= _get_rl(expr
->right
, implied
, recurse_cnt
);
598 right_rl
= cast_rl(type
, right_rl
);
600 if (!left_rl
&& !right_rl
)
603 rl
= handle_implied_binop(left_rl
, expr
->op
, right_rl
);
609 return handle_mod_rl(expr
, implied
, recurse_cnt
);
611 return handle_bitwise_AND(expr
, implied
, recurse_cnt
);
614 return use_rl_binop(expr
, implied
, recurse_cnt
);
615 case SPECIAL_RIGHTSHIFT
:
616 return handle_right_shift(expr
, implied
, recurse_cnt
);
617 case SPECIAL_LEFTSHIFT
:
618 return handle_left_shift(expr
, implied
, recurse_cnt
);
620 return handle_subtract_rl(expr
, implied
, recurse_cnt
);
622 return handle_divide_rl(expr
, implied
, recurse_cnt
);
625 if (!left_rl
|| !right_rl
)
628 if (sval_binop_overflows(rl_min(left_rl
), expr
->op
, rl_min(right_rl
)))
630 if (sval_binop_overflows(rl_max(left_rl
), expr
->op
, rl_max(right_rl
)))
633 min
= sval_binop(rl_min(left_rl
), expr
->op
, rl_min(right_rl
));
634 max
= sval_binop(rl_max(left_rl
), expr
->op
, rl_max(right_rl
));
636 return alloc_rl(min
, max
);
639 static int do_comparison(struct expression
*expr
)
641 struct range_list
*left_ranges
= NULL
;
642 struct range_list
*right_ranges
= NULL
;
643 int poss_true
, poss_false
;
646 type
= get_type(expr
);
647 get_absolute_rl(expr
->left
, &left_ranges
);
648 get_absolute_rl(expr
->right
, &right_ranges
);
650 left_ranges
= cast_rl(type
, left_ranges
);
651 right_ranges
= cast_rl(type
, right_ranges
);
653 poss_true
= possibly_true_rl(left_ranges
, expr
->op
, right_ranges
);
654 poss_false
= possibly_false_rl(left_ranges
, expr
->op
, right_ranges
);
656 if (!poss_true
&& !poss_false
)
658 if (poss_true
&& !poss_false
)
660 if (!poss_true
&& poss_false
)
665 static struct range_list
*handle_comparison_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
670 if (expr
->op
== SPECIAL_EQUAL
&& expr
->left
->type
== EXPR_TYPE
) {
671 struct symbol
*left
, *right
;
673 left
= get_real_base_type(expr
->left
->symbol
);
674 right
= get_real_base_type(expr
->left
->symbol
);
680 if (get_value(expr
->left
, &left
) && get_value(expr
->right
, &right
)) {
681 struct data_range tmp_left
, tmp_right
;
685 tmp_right
.min
= right
;
686 tmp_right
.max
= right
;
687 if (true_comparison_range(&tmp_left
, expr
->op
, &tmp_right
))
692 if (implied
== RL_EXACT
)
695 res
= do_comparison(expr
);
701 return alloc_rl(zero
, one
);
704 static struct range_list
*handle_logical_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
710 if (implied
== RL_EXACT
) {
711 if (get_value(expr
->left
, &left
))
713 if (get_value(expr
->right
, &right
))
716 if (get_implied_value_internal(expr
->left
, &left
, recurse_cnt
))
718 if (get_implied_value_internal(expr
->right
, &right
, recurse_cnt
))
723 case SPECIAL_LOGICAL_OR
:
724 if (left_known
&& left
.value
)
726 if (right_known
&& right
.value
)
728 if (left_known
&& right_known
)
731 case SPECIAL_LOGICAL_AND
:
732 if (left_known
&& right_known
) {
733 if (left
.value
&& right
.value
)
742 if (implied
== RL_EXACT
)
745 return alloc_rl(zero
, one
);
748 static struct range_list
*handle_conditional_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
750 struct expression
*cond_true
;
751 struct range_list
*true_rl
, *false_rl
;
753 int final_pass_orig
= final_pass
;
755 cond_true
= expr
->cond_true
;
757 cond_true
= expr
->conditional
;
759 if (known_condition_true(expr
->conditional
))
760 return _get_rl(cond_true
, implied
, recurse_cnt
);
761 if (known_condition_false(expr
->conditional
))
762 return _get_rl(expr
->cond_false
, implied
, recurse_cnt
);
764 if (implied
== RL_EXACT
)
767 if (implied_condition_true(expr
->conditional
))
768 return _get_rl(cond_true
, implied
, recurse_cnt
);
769 if (implied_condition_false(expr
->conditional
))
770 return _get_rl(expr
->cond_false
, implied
, recurse_cnt
);
773 /* this becomes a problem with deeply nested conditional statements */
777 type
= get_type(expr
);
779 __push_fake_cur_stree();
781 __split_whole_condition(expr
->conditional
);
782 true_rl
= _get_rl(cond_true
, implied
, recurse_cnt
);
783 __push_true_states();
784 __use_false_states();
785 false_rl
= _get_rl(expr
->cond_false
, implied
, recurse_cnt
);
786 __merge_true_states();
787 __free_fake_cur_stree();
788 final_pass
= final_pass_orig
;
790 if (!true_rl
|| !false_rl
)
792 true_rl
= cast_rl(type
, true_rl
);
793 false_rl
= cast_rl(type
, false_rl
);
795 return rl_union(true_rl
, false_rl
);
798 static int get_fuzzy_max_helper(struct expression
*expr
, sval_t
*max
)
800 struct smatch_state
*state
;
803 if (get_hard_max(expr
, &sval
)) {
808 state
= get_extra_state(expr
);
809 if (!state
|| !estate_has_fuzzy_max(state
))
811 *max
= sval_cast(get_type(expr
), estate_get_fuzzy_max(state
));
815 static int get_fuzzy_min_helper(struct expression
*expr
, sval_t
*min
)
817 struct smatch_state
*state
;
820 state
= get_extra_state(expr
);
821 if (!state
|| !estate_rl(state
))
824 sval
= estate_min(state
);
825 if (sval_is_negative(sval
) && sval_is_min(sval
))
828 if (sval_is_max(sval
))
831 *min
= sval_cast(get_type(expr
), sval
);
835 int get_const_value(struct expression
*expr
, sval_t
*sval
)
840 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
843 if (!(sym
->ctype
.modifiers
& MOD_CONST
))
845 if (get_value(sym
->initializer
, &right
)) {
846 *sval
= sval_cast(get_type(expr
), right
);
852 struct range_list
*var_to_absolute_rl(struct expression
*expr
)
854 struct smatch_state
*state
;
855 struct range_list
*rl
;
857 state
= get_extra_state(expr
);
858 if (!state
|| is_whole_rl(estate_rl(state
))) {
859 state
= get_real_absolute_state(expr
);
860 if (state
&& state
->data
&& !estate_is_whole(state
))
861 return clone_rl(estate_rl(state
));
862 if (get_local_rl(expr
, &rl
) && !is_whole_rl(rl
))
864 if (get_mtag_rl(expr
, &rl
))
866 if (get_db_type_rl(expr
, &rl
) && !is_whole_rl(rl
))
868 return alloc_whole_rl(get_type(expr
));
870 /* err on the side of saying things are possible */
871 if (!estate_rl(state
))
872 return alloc_whole_rl(get_type(expr
));
873 return clone_rl(estate_rl(state
));
876 static struct range_list
*handle_variable(struct expression
*expr
, int implied
, int *recurse_cnt
)
878 struct smatch_state
*state
;
879 struct range_list
*rl
;
880 sval_t sval
, min
, max
;
883 if (get_const_value(expr
, &sval
))
884 return alloc_rl(sval
, sval
);
886 if (custom_handle_variable
) {
887 rl
= custom_handle_variable(expr
);
889 return var_to_absolute_rl(expr
);
893 if (implied
== RL_EXACT
)
896 if (get_mtag_sval(expr
, &sval
))
897 return alloc_rl(sval
, sval
);
899 type
= get_type(expr
);
900 if (type
&& type
->type
== SYM_FN
)
901 return alloc_rl(fn_ptr_min
, fn_ptr_max
);
907 state
= get_extra_state(expr
);
908 if (!state
|| !state
->data
) {
909 if (implied
== RL_HARD
)
911 if (get_local_rl(expr
, &rl
))
913 if (get_mtag_rl(expr
, &rl
))
915 if (get_db_type_rl(expr
, &rl
))
917 if (is_array(expr
) && get_array_rl(expr
, &rl
))
921 if (implied
== RL_HARD
&& !estate_has_hard_max(state
))
923 return clone_rl(estate_rl(state
));
924 case RL_REAL_ABSOLUTE
: {
925 struct smatch_state
*abs_state
;
927 state
= get_extra_state(expr
);
928 abs_state
= get_real_absolute_state(expr
);
930 if (estate_rl(state
) && estate_rl(abs_state
)) {
931 return clone_rl(rl_intersection(estate_rl(state
),
932 estate_rl(abs_state
)));
933 } else if (estate_rl(state
)) {
934 return clone_rl(estate_rl(state
));
935 } else if (estate_is_empty(state
)) {
937 * FIXME: we don't handle empty extra states correctly.
939 * The real abs rl is supposed to be filtered by the
940 * extra state if there is one. We don't bother keeping
941 * the abs state in sync all the time because we know it
942 * will be filtered later.
944 * It's not totally obvious to me how they should be
945 * handled. Perhaps we should take the whole rl and
946 * filter by the imaginary states. Perhaps we should
947 * just go with the empty state.
949 * Anyway what we currently do is return NULL here and
950 * that gets translated into the whole range in
951 * get_real_absolute_rl().
955 } else if (estate_rl(abs_state
)) {
956 return clone_rl(estate_rl(abs_state
));
959 if (get_local_rl(expr
, &rl
))
961 if (get_mtag_rl(expr
, &rl
))
963 if (get_db_type_rl(expr
, &rl
))
965 if (is_array(expr
) && get_array_rl(expr
, &rl
))
970 if (!get_fuzzy_min_helper(expr
, &min
))
971 min
= sval_type_min(get_type(expr
));
972 if (!get_fuzzy_max_helper(expr
, &max
))
974 /* fuzzy ranges are often inverted */
975 if (sval_cmp(min
, max
) > 0) {
980 return alloc_rl(min
, max
);
985 static sval_t
handle_sizeof(struct expression
*expr
)
990 ret
= sval_blank(expr
);
991 sym
= expr
->cast_type
;
993 sym
= evaluate_expression(expr
->cast_expression
);
995 __silence_warnings_for_stmt
= true;
1000 * Expressions of restricted types will possibly get
1001 * promoted - check that here. I'm not sure how this works,
1002 * the problem is that sizeof(le16) shouldn't be promoted and
1003 * the original code did that... Let's if zero this out and
1007 if (is_restricted_type(sym
)) {
1008 if (type_bits(sym
) < bits_in_int
)
1012 if (is_fouled_type(sym
))
1015 examine_symbol_type(sym
);
1017 ret
.type
= size_t_ctype
;
1018 if (type_bits(sym
) <= 0) /* sizeof(void) */ {
1019 if (get_real_base_type(sym
) == &void_ctype
)
1024 ret
.value
= type_bytes(sym
);
1029 static struct range_list
*handle_strlen(struct expression
*expr
, int implied
, int *recurse_cnt
)
1031 struct range_list
*rl
;
1032 struct expression
*arg
, *tmp
;
1034 sval_t ret
= { .type
= &ulong_ctype
};
1036 if (implied
== RL_EXACT
)
1039 arg
= get_argument_from_call_expr(expr
->args
, 0);
1042 if (arg
->type
== EXPR_STRING
) {
1043 ret
.value
= arg
->string
->length
- 1;
1044 return alloc_rl(ret
, ret
);
1046 if (get_implied_value(arg
, &tag
) &&
1047 (tmp
= fake_string_from_mtag(tag
.uvalue
))) {
1048 ret
.value
= tmp
->string
->length
- 1;
1049 return alloc_rl(ret
, ret
);
1052 if (implied
== RL_HARD
|| implied
== RL_FUZZY
)
1055 if (get_implied_return(expr
, &rl
))
1061 static struct range_list
*handle_builtin_constant_p(struct expression
*expr
, int implied
, int *recurse_cnt
)
1063 struct expression
*arg
;
1064 struct range_list
*rl
;
1067 arg
= get_argument_from_call_expr(expr
->args
, 0);
1068 rl
= _get_rl(arg
, RL_EXACT
, recurse_cnt
);
1069 if (rl_to_sval(rl
, &sval
))
1074 static struct range_list
*handle__builtin_choose_expr(struct expression
*expr
, int implied
, int *recurse_cnt
)
1076 struct expression
*const_expr
, *expr1
, *expr2
;
1079 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
1080 expr1
= get_argument_from_call_expr(expr
->args
, 1);
1081 expr2
= get_argument_from_call_expr(expr
->args
, 2);
1083 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
1086 return _get_rl(expr1
, implied
, recurse_cnt
);
1087 return _get_rl(expr2
, implied
, recurse_cnt
);
1090 static struct range_list
*handle_call_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
1092 struct range_list
*rl
;
1094 if (sym_name_is("__builtin_constant_p", expr
->fn
))
1095 return handle_builtin_constant_p(expr
, implied
, recurse_cnt
);
1097 if (sym_name_is("__builtin_choose_expr", expr
->fn
))
1098 return handle__builtin_choose_expr(expr
, implied
, recurse_cnt
);
1100 if (sym_name_is("__builtin_expect", expr
->fn
) ||
1101 sym_name_is("__builtin_bswap16", expr
->fn
) ||
1102 sym_name_is("__builtin_bswap32", expr
->fn
) ||
1103 sym_name_is("__builtin_bswap64", expr
->fn
)) {
1104 struct expression
*arg
;
1106 arg
= get_argument_from_call_expr(expr
->args
, 0);
1107 return _get_rl(arg
, implied
, recurse_cnt
);
1110 if (sym_name_is("strlen", expr
->fn
))
1111 return handle_strlen(expr
, implied
, recurse_cnt
);
1113 if (implied
== RL_EXACT
|| implied
== RL_HARD
|| implied
== RL_FUZZY
)
1116 if (custom_handle_variable
) {
1117 rl
= custom_handle_variable(expr
);
1122 if (get_implied_return(expr
, &rl
))
1124 return db_return_vals(expr
);
1127 static struct range_list
*handle_cast(struct expression
*expr
, int implied
, int *recurse_cnt
)
1129 struct range_list
*rl
;
1130 struct symbol
*type
;
1132 type
= get_type(expr
);
1133 rl
= _get_rl(expr
->cast_expression
, implied
, recurse_cnt
);
1135 return cast_rl(type
, rl
);
1136 if (implied
== RL_ABSOLUTE
|| implied
== RL_REAL_ABSOLUTE
)
1137 return alloc_whole_rl(type
);
1138 if (implied
== RL_IMPLIED
&& type
&&
1139 type_bits(type
) > 0 && type_bits(type
) < 32)
1140 return alloc_whole_rl(type
);
1144 static struct range_list
*_get_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
1146 struct range_list
*rl
;
1147 struct symbol
*type
;
1150 type
= get_type(expr
);
1151 expr
= strip_parens(expr
);
1155 if (++(*recurse_cnt
) >= 200)
1158 switch(expr
->type
) {
1160 case EXPR_FORCE_CAST
:
1161 case EXPR_IMPLIED_CAST
:
1162 rl
= handle_cast(expr
, implied
, recurse_cnt
);
1166 expr
= strip_expr(expr
);
1170 switch (expr
->type
) {
1172 sval
= sval_from_val(expr
, expr
->value
);
1173 rl
= alloc_rl(sval
, sval
);
1176 rl
= handle_preop_rl(expr
, implied
, recurse_cnt
);
1179 rl
= _get_rl(expr
->unop
, implied
, recurse_cnt
);
1182 rl
= handle_binop_rl(expr
, implied
, recurse_cnt
);
1185 rl
= handle_comparison_rl(expr
, implied
, recurse_cnt
);
1188 rl
= handle_logical_rl(expr
, implied
, recurse_cnt
);
1190 case EXPR_PTRSIZEOF
:
1192 sval
= handle_sizeof(expr
);
1193 rl
= alloc_rl(sval
, sval
);
1196 case EXPR_CONDITIONAL
:
1197 rl
= handle_conditional_rl(expr
, implied
, recurse_cnt
);
1200 rl
= handle_call_rl(expr
, implied
, recurse_cnt
);
1204 if (get_mtag_sval(expr
, &sval
))
1205 rl
= alloc_rl(sval
, sval
);
1208 rl
= handle_variable(expr
, implied
, recurse_cnt
);
1214 if (type
&& (implied
== RL_ABSOLUTE
|| implied
== RL_REAL_ABSOLUTE
))
1215 return alloc_whole_rl(type
);
1220 struct expression
*expr
;
1221 struct range_list
*rl
;
1222 } cached_results
[24];
1223 static int cache_idx
;
1225 void clear_math_cache(void)
1227 memset(cached_results
, 0, sizeof(cached_results
));
1230 /* returns 1 if it can get a value literal or else returns 0 */
1231 int get_value(struct expression
*expr
, sval_t
*sval
)
1233 struct range_list
*(*orig_custom_fn
)(struct expression
*expr
);
1234 struct range_list
*rl
;
1235 int recurse_cnt
= 0;
1240 * This only handles RL_EXACT because other expr statements can be
1241 * different at different points. Like the list iterator, for example.
1243 for (i
= 0; i
< ARRAY_SIZE(cached_results
); i
++) {
1244 if (expr
== cached_results
[i
].expr
)
1245 return rl_to_sval(cached_results
[i
].rl
, sval
);
1248 orig_custom_fn
= custom_handle_variable
;
1249 custom_handle_variable
= NULL
;
1250 rl
= _get_rl(expr
, RL_EXACT
, &recurse_cnt
);
1251 if (!rl_to_sval(rl
, &tmp
))
1253 custom_handle_variable
= orig_custom_fn
;
1255 cached_results
[cache_idx
].expr
= expr
;
1256 cached_results
[cache_idx
].rl
= rl
;
1257 cache_idx
= (cache_idx
+ 1) % ARRAY_SIZE(cached_results
);
1266 static int get_implied_value_internal(struct expression
*expr
, sval_t
*sval
, int *recurse_cnt
)
1268 struct range_list
*rl
;
1270 rl
= _get_rl(expr
, RL_IMPLIED
, recurse_cnt
);
1271 if (!rl_to_sval(rl
, sval
))
1276 int get_implied_value(struct expression
*expr
, sval_t
*sval
)
1278 struct range_list
*rl
;
1279 int recurse_cnt
= 0;
1281 rl
= _get_rl(expr
, RL_IMPLIED
, &recurse_cnt
);
1282 if (!rl_to_sval(rl
, sval
))
1287 int get_implied_min(struct expression
*expr
, sval_t
*sval
)
1289 struct range_list
*rl
;
1290 int recurse_cnt
= 0;
1292 rl
= _get_rl(expr
, RL_IMPLIED
, &recurse_cnt
);
1299 int get_implied_max(struct expression
*expr
, sval_t
*sval
)
1301 struct range_list
*rl
;
1302 int recurse_cnt
= 0;
1304 rl
= _get_rl(expr
, RL_IMPLIED
, &recurse_cnt
);
1311 int get_implied_rl(struct expression
*expr
, struct range_list
**rl
)
1313 int recurse_cnt
= 0;
1315 *rl
= _get_rl(expr
, RL_IMPLIED
, &recurse_cnt
);
1321 static int get_absolute_rl_internal(struct expression
*expr
, struct range_list
**rl
, int *recurse_cnt
)
1323 *rl
= _get_rl(expr
, RL_ABSOLUTE
, recurse_cnt
);
1325 *rl
= alloc_whole_rl(get_type(expr
));
1329 int get_absolute_rl(struct expression
*expr
, struct range_list
**rl
)
1331 int recurse_cnt
= 0;
1333 *rl
= _get_rl(expr
, RL_ABSOLUTE
, &recurse_cnt
);
1335 *rl
= alloc_whole_rl(get_type(expr
));
1339 int get_real_absolute_rl(struct expression
*expr
, struct range_list
**rl
)
1341 int recurse_cnt
= 0;
1343 *rl
= _get_rl(expr
, RL_REAL_ABSOLUTE
, &recurse_cnt
);
1345 *rl
= alloc_whole_rl(get_type(expr
));
1349 int custom_get_absolute_rl(struct expression
*expr
,
1350 struct range_list
*(*fn
)(struct expression
*expr
),
1351 struct range_list
**rl
)
1353 int recurse_cnt
= 0;
1356 custom_handle_variable
= fn
;
1357 *rl
= _get_rl(expr
, RL_REAL_ABSOLUTE
, &recurse_cnt
);
1358 custom_handle_variable
= NULL
;
1362 int get_implied_rl_var_sym(const char *var
, struct symbol
*sym
, struct range_list
**rl
)
1364 struct smatch_state
*state
;
1366 state
= get_state(SMATCH_EXTRA
, var
, sym
);
1367 *rl
= estate_rl(state
);
1373 int get_hard_max(struct expression
*expr
, sval_t
*sval
)
1375 struct range_list
*rl
;
1376 int recurse_cnt
= 0;
1378 rl
= _get_rl(expr
, RL_HARD
, &recurse_cnt
);
1385 int get_fuzzy_min(struct expression
*expr
, sval_t
*sval
)
1387 struct range_list
*rl
;
1389 int recurse_cnt
= 0;
1391 rl
= _get_rl(expr
, RL_FUZZY
, &recurse_cnt
);
1395 if (sval_is_negative(tmp
) && sval_is_min(tmp
))
1401 int get_fuzzy_max(struct expression
*expr
, sval_t
*sval
)
1403 struct range_list
*rl
;
1405 int recurse_cnt
= 0;
1407 rl
= _get_rl(expr
, RL_FUZZY
, &recurse_cnt
);
1411 if (max
.uvalue
> INT_MAX
- 10000)
1417 int get_absolute_min(struct expression
*expr
, sval_t
*sval
)
1419 struct range_list
*rl
;
1420 struct symbol
*type
;
1421 int recurse_cnt
= 0;
1423 type
= get_type(expr
);
1425 type
= &llong_ctype
; // FIXME: this is wrong but places assume get type can't fail.
1426 rl
= _get_rl(expr
, RL_REAL_ABSOLUTE
, &recurse_cnt
);
1430 *sval
= sval_type_min(type
);
1432 if (sval_cmp(*sval
, sval_type_min(type
)) < 0)
1433 *sval
= sval_type_min(type
);
1437 int get_absolute_max(struct expression
*expr
, sval_t
*sval
)
1439 struct range_list
*rl
;
1440 struct symbol
*type
;
1441 int recurse_cnt
= 0;
1443 type
= get_type(expr
);
1445 type
= &llong_ctype
;
1446 rl
= _get_rl(expr
, RL_REAL_ABSOLUTE
, &recurse_cnt
);
1450 *sval
= sval_type_max(type
);
1452 if (sval_cmp(sval_type_max(type
), *sval
) < 0)
1453 *sval
= sval_type_max(type
);
1457 int known_condition_true(struct expression
*expr
)
1464 if (get_value(expr
, &tmp
) && tmp
.value
)
1470 int known_condition_false(struct expression
*expr
)
1481 int implied_condition_true(struct expression
*expr
)
1488 if (known_condition_true(expr
))
1490 if (get_implied_value(expr
, &tmp
) && tmp
.value
)
1493 if (expr
->type
== EXPR_POSTOP
)
1494 return implied_condition_true(expr
->unop
);
1496 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_DECREMENT
)
1497 return implied_not_equal(expr
->unop
, 1);
1498 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_INCREMENT
)
1499 return implied_not_equal(expr
->unop
, -1);
1501 expr
= strip_expr(expr
);
1502 switch (expr
->type
) {
1504 if (do_comparison(expr
) == 1)
1508 if (expr
->op
== '!') {
1509 if (implied_condition_false(expr
->unop
))
1515 if (implied_not_equal(expr
, 0) == 1)
1522 int implied_condition_false(struct expression
*expr
)
1524 struct expression
*tmp
;
1530 if (known_condition_false(expr
))
1533 switch (expr
->type
) {
1535 if (do_comparison(expr
) == 2)
1538 if (expr
->op
== '!') {
1539 if (implied_condition_true(expr
->unop
))
1543 tmp
= strip_expr(expr
);
1545 return implied_condition_false(tmp
);
1548 if (get_implied_value(expr
, &sval
) && sval
.value
== 0)
1555 int can_integer_overflow(struct symbol
*type
, struct expression
*expr
)
1558 sval_t lmax
, rmax
, res
;
1563 expr
= strip_expr(expr
);
1565 if (expr
->type
== EXPR_ASSIGNMENT
) {
1567 case SPECIAL_MUL_ASSIGN
:
1570 case SPECIAL_ADD_ASSIGN
:
1573 case SPECIAL_SHL_ASSIGN
:
1574 op
= SPECIAL_LEFTSHIFT
;
1579 } else if (expr
->type
== EXPR_BINOP
) {
1580 if (expr
->op
!= '*' && expr
->op
!= '+' && expr
->op
!= SPECIAL_LEFTSHIFT
)
1587 get_absolute_max(expr
->left
, &lmax
);
1588 get_absolute_max(expr
->right
, &rmax
);
1590 if (sval_binop_overflows(lmax
, op
, rmax
))
1593 res
= sval_binop(lmax
, op
, rmax
);
1594 if (sval_cmp(res
, sval_type_max(type
)) > 0)