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 return alloc_rl(zero
, zero
);
38 struct range_list
*rl_one(void)
40 return alloc_rl(one
, one
);
51 static struct range_list
*last_stmt_rl(struct statement
*stmt
, int implied
, int *recurse_cnt
)
56 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
57 if (stmt
->type
!= STMT_EXPRESSION
)
59 return _get_rl(stmt
->expression
, implied
, recurse_cnt
);
62 static struct range_list
*handle_expression_statement_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
64 return last_stmt_rl(get_expression_statement(expr
), implied
, recurse_cnt
);
67 static struct range_list
*handle_ampersand_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
69 struct range_list
*rl
;
71 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
73 if (get_address_rl(expr
, &rl
))
75 return alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
78 static struct range_list
*handle_negate_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
80 if (known_condition_true(expr
->unop
))
82 if (known_condition_false(expr
->unop
))
85 if (implied
== RL_EXACT
)
88 if (implied_condition_true(expr
->unop
))
90 if (implied_condition_false(expr
->unop
))
92 return alloc_rl(zero
, one
);
95 static struct range_list
*handle_bitwise_negate(struct expression
*expr
, int implied
, int *recurse_cnt
)
97 struct range_list
*rl
;
100 rl
= _get_rl(expr
->unop
, implied
, recurse_cnt
);
101 if (!rl_to_sval(rl
, &sval
))
103 sval
= sval_preop(sval
, '~');
104 sval_cast(get_type(expr
->unop
), sval
);
105 return alloc_rl(sval
, sval
);
108 static struct range_list
*handle_minus_preop(struct expression
*expr
, int implied
, int *recurse_cnt
)
110 struct range_list
*rl
;
113 rl
= _get_rl(expr
->unop
, implied
, recurse_cnt
);
114 if (!rl_to_sval(rl
, &sval
))
116 sval
= sval_preop(sval
, '-');
117 return alloc_rl(sval
, sval
);
120 static struct range_list
*handle_preop_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
124 return handle_ampersand_rl(expr
, implied
, recurse_cnt
);
126 return handle_negate_rl(expr
, implied
, recurse_cnt
);
128 return handle_bitwise_negate(expr
, implied
, recurse_cnt
);
130 return handle_minus_preop(expr
, implied
, recurse_cnt
);
132 return handle_variable(expr
, implied
, recurse_cnt
);
134 return handle_expression_statement_rl(expr
, implied
, recurse_cnt
);
140 static struct range_list
*handle_divide_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
142 struct range_list
*left_rl
, *right_rl
;
145 type
= get_type(expr
);
147 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
148 left_rl
= cast_rl(type
, left_rl
);
149 right_rl
= _get_rl(expr
->right
, implied
, recurse_cnt
);
150 right_rl
= cast_rl(type
, right_rl
);
152 if (!left_rl
|| !right_rl
)
154 if (is_whole_rl(left_rl
) || is_whole_rl(right_rl
))
157 return rl_binop(left_rl
, '/', right_rl
);
160 static struct range_list
*handle_subtract_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
163 struct range_list
*left_rl
, *right_rl
;
164 sval_t max
, min
, tmp
;
167 type
= get_type(expr
);
168 comparison
= get_comparison(expr
->left
, expr
->right
);
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
) &&
176 (implied
== RL_EXACT
|| implied
== RL_HARD
|| implied
== RL_FUZZY
))
180 left_rl
= alloc_whole_rl(type
);
182 right_rl
= alloc_whole_rl(type
);
184 /* negative values complicate everything fix this later */
185 if (sval_is_negative(rl_min(right_rl
)))
187 max
= rl_max(left_rl
);
189 switch (comparison
) {
191 case SPECIAL_UNSIGNED_GT
:
192 min
= sval_type_val(type
, 1);
193 max
= rl_max(left_rl
);
196 case SPECIAL_UNSIGNED_GTE
:
197 min
= sval_type_val(type
, 0);
198 max
= rl_max(left_rl
);
201 if (sval_binop_overflows(rl_min(left_rl
), '-', rl_max(right_rl
)))
203 min
= sval_type_min(type
);
206 if (!sval_binop_overflows(rl_min(left_rl
), '-', rl_max(right_rl
))) {
207 tmp
= sval_binop(rl_min(left_rl
), '-', rl_max(right_rl
));
208 if (sval_cmp(tmp
, min
) > 0)
212 if (!sval_is_max(rl_max(left_rl
))) {
213 tmp
= sval_binop(rl_max(left_rl
), '-', rl_min(right_rl
));
214 if (sval_cmp(tmp
, max
) < 0)
218 if (sval_is_min(min
) && sval_is_max(max
))
221 return cast_rl(type
, alloc_rl(min
, max
));
224 static struct range_list
*handle_mod_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
226 struct range_list
*rl
;
227 sval_t left
, right
, sval
;
229 if (implied
== RL_EXACT
) {
230 if (!get_value(expr
->right
, &right
))
232 if (!get_value(expr
->left
, &left
))
234 sval
= sval_binop(left
, '%', right
);
235 return alloc_rl(sval
, sval
);
237 /* if we can't figure out the right side it's probably hopeless */
238 if (!get_implied_value_internal(expr
->right
, &right
, recurse_cnt
))
241 right
= sval_cast(get_type(expr
), right
);
244 rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
245 if (rl
&& rl_max(rl
).uvalue
< right
.uvalue
)
246 right
.uvalue
= rl_max(rl
).uvalue
;
248 return alloc_rl(zero
, right
);
251 static sval_t
sval_lowest_set_bit(sval_t sval
)
256 for (i
= 0; i
< 64; i
++) {
257 if (sval
.uvalue
& 1ULL << i
) {
260 sval
.uvalue
&= ~(1ULL << i
);
266 static struct range_list
*handle_bitwise_AND(struct expression
*expr
, int implied
, int *recurse_cnt
)
269 struct range_list
*left_rl
, *right_rl
;
272 if (implied
!= RL_IMPLIED
&& implied
!= RL_ABSOLUTE
)
275 type
= get_type(expr
);
277 if (get_implied_value_internal(expr
->left
, &known
, recurse_cnt
)) {
280 min
= sval_lowest_set_bit(known
);
281 left_rl
= alloc_rl(min
, known
);
282 left_rl
= cast_rl(type
, left_rl
);
283 add_range(&left_rl
, sval_type_val(type
, 0), sval_type_val(type
, 0));
285 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
287 left_rl
= cast_rl(type
, left_rl
);
288 left_rl
= alloc_rl(sval_type_val(type
, 0), rl_max(left_rl
));
290 if (implied
== RL_HARD
)
292 left_rl
= alloc_whole_rl(type
);
296 if (get_implied_value_internal(expr
->right
, &known
, recurse_cnt
)) {
297 sval_t min
, left_max
, mod
;
299 min
= sval_lowest_set_bit(known
);
300 right_rl
= alloc_rl(min
, known
);
301 right_rl
= cast_rl(type
, right_rl
);
302 add_range(&right_rl
, sval_type_val(type
, 0), sval_type_val(type
, 0));
304 if (min
.value
!= 0) {
305 left_max
= rl_max(left_rl
);
306 mod
= sval_binop(left_max
, '%', min
);
308 left_max
= sval_binop(left_max
, '-', mod
);
310 if (left_max
.value
> 0 && sval_cmp(left_max
, rl_max(left_rl
)) < 0)
311 left_rl
= remove_range(left_rl
, left_max
, rl_max(left_rl
));
315 right_rl
= _get_rl(expr
->right
, implied
, recurse_cnt
);
317 right_rl
= cast_rl(type
, right_rl
);
318 right_rl
= alloc_rl(sval_type_val(type
, 0), rl_max(right_rl
));
320 if (implied
== RL_HARD
)
322 right_rl
= alloc_whole_rl(type
);
326 return rl_intersection(left_rl
, right_rl
);
329 static struct range_list
*handle_bitwise_OR(struct expression
*expr
, int implied
, int *recurse_cnt
)
332 struct range_list
*left_rl
, *right_rl
;
334 if (implied
!= RL_IMPLIED
&& implied
!= RL_ABSOLUTE
)
337 type
= get_type(expr
);
339 get_absolute_rl_internal(expr
->left
, &left_rl
, recurse_cnt
);
340 get_absolute_rl_internal(expr
->right
, &right_rl
, recurse_cnt
);
341 left_rl
= cast_rl(type
, left_rl
);
342 right_rl
= cast_rl(type
, right_rl
);
343 if (!left_rl
|| !right_rl
)
346 return rl_binop(left_rl
, '|', right_rl
);
349 static struct range_list
*handle_right_shift(struct expression
*expr
, int implied
, int *recurse_cnt
)
351 struct range_list
*left_rl
;
355 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
358 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
360 max
= rl_max(left_rl
);
361 min
= rl_min(left_rl
);
363 if (implied
== RL_FUZZY
)
365 max
= sval_type_max(get_type(expr
->left
));
366 min
= sval_type_val(get_type(expr
->left
), 0);
369 if (get_implied_value_internal(expr
->right
, &right
, recurse_cnt
)) {
370 min
= sval_binop(min
, SPECIAL_RIGHTSHIFT
, right
);
371 max
= sval_binop(max
, SPECIAL_RIGHTSHIFT
, right
);
372 } else if (!sval_is_negative(min
)) {
374 max
= sval_type_max(max
.type
);
379 return alloc_rl(min
, max
);
382 static struct range_list
*handle_left_shift(struct expression
*expr
, int implied
, int *recurse_cnt
)
384 struct range_list
*left_rl
, *res
;
389 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
391 /* this is hopeless without the right side */
392 if (!get_implied_value_internal(expr
->right
, &right
, recurse_cnt
))
394 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
396 max
= rl_max(left_rl
);
397 min
= rl_min(left_rl
);
398 if (min
.value
== 0) {
403 if (implied
== RL_FUZZY
)
405 max
= sval_type_max(get_type(expr
->left
));
406 min
= sval_type_val(get_type(expr
->left
), 1);
410 max
= sval_binop(max
, SPECIAL_LEFTSHIFT
, right
);
411 min
= sval_binop(min
, SPECIAL_LEFTSHIFT
, right
);
412 res
= alloc_rl(min
, max
);
414 res
= rl_union(res
, rl_zero());
418 static struct range_list
*handle_known_binop(struct expression
*expr
)
422 if (!get_value(expr
->left
, &left
))
424 if (!get_value(expr
->right
, &right
))
426 left
= sval_binop(left
, expr
->op
, right
);
427 return alloc_rl(left
, left
);
430 static int has_actual_ranges(struct range_list
*rl
)
432 struct data_range
*tmp
;
434 FOR_EACH_PTR(rl
, tmp
) {
435 if (sval_cmp(tmp
->min
, tmp
->max
) != 0)
437 } END_FOR_EACH_PTR(tmp
);
441 static struct range_list
*handle_implied_binop(struct range_list
*left_rl
, int op
, struct range_list
*right_rl
)
443 struct range_list
*res_rl
;
444 struct data_range
*left_drange
, *right_drange
;
447 if (!left_rl
|| !right_rl
)
449 if (has_actual_ranges(left_rl
))
451 if (has_actual_ranges(right_rl
))
454 if (ptr_list_size((struct ptr_list
*)left_rl
) * ptr_list_size((struct ptr_list
*)right_rl
) > 20)
459 FOR_EACH_PTR(left_rl
, left_drange
) {
460 FOR_EACH_PTR(right_rl
, right_drange
) {
461 if ((op
== '%' || op
== '/') &&
462 right_drange
->min
.value
== 0)
464 res
= sval_binop(left_drange
->min
, op
, right_drange
->min
);
465 add_range(&res_rl
, res
, res
);
466 } END_FOR_EACH_PTR(right_drange
);
467 } END_FOR_EACH_PTR(left_drange
);
472 static struct range_list
*handle_binop_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
474 struct smatch_state
*state
;
476 struct range_list
*left_rl
, *right_rl
, *rl
;
479 rl
= handle_known_binop(expr
);
482 if (implied
== RL_EXACT
)
485 state
= get_extra_state(expr
);
486 if (state
&& !is_whole_rl(estate_rl(state
)))
487 return clone_rl(estate_rl(state
));
489 type
= get_type(expr
);
490 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
491 left_rl
= cast_rl(type
, left_rl
);
492 right_rl
= _get_rl(expr
->right
, implied
, recurse_cnt
);
493 right_rl
= cast_rl(type
, right_rl
);
495 if (!left_rl
&& !right_rl
)
498 rl
= handle_implied_binop(left_rl
, expr
->op
, right_rl
);
504 return handle_mod_rl(expr
, implied
, recurse_cnt
);
506 return handle_bitwise_AND(expr
, implied
, recurse_cnt
);
508 return handle_bitwise_OR(expr
, implied
, recurse_cnt
);
509 case SPECIAL_RIGHTSHIFT
:
510 return handle_right_shift(expr
, implied
, recurse_cnt
);
511 case SPECIAL_LEFTSHIFT
:
512 return handle_left_shift(expr
, implied
, recurse_cnt
);
514 return handle_subtract_rl(expr
, implied
, recurse_cnt
);
516 return handle_divide_rl(expr
, implied
, recurse_cnt
);
519 if (!left_rl
|| !right_rl
)
522 if (sval_binop_overflows(rl_min(left_rl
), expr
->op
, rl_min(right_rl
)))
524 min
= sval_binop(rl_min(left_rl
), expr
->op
, rl_min(right_rl
));
526 if (sval_binop_overflows(rl_max(left_rl
), expr
->op
, rl_max(right_rl
))) {
532 max
= sval_type_max(get_type(expr
));
535 max
= sval_binop(rl_max(left_rl
), expr
->op
, rl_max(right_rl
));
538 return alloc_rl(min
, max
);
541 static int do_comparison(struct expression
*expr
)
543 struct range_list
*left_ranges
= NULL
;
544 struct range_list
*right_ranges
= NULL
;
545 int poss_true
, poss_false
;
548 type
= get_type(expr
);
549 get_absolute_rl(expr
->left
, &left_ranges
);
550 get_absolute_rl(expr
->right
, &right_ranges
);
552 left_ranges
= cast_rl(type
, left_ranges
);
553 right_ranges
= cast_rl(type
, right_ranges
);
555 poss_true
= possibly_true_rl(left_ranges
, expr
->op
, right_ranges
);
556 poss_false
= possibly_false_rl(left_ranges
, expr
->op
, right_ranges
);
558 free_rl(&left_ranges
);
559 free_rl(&right_ranges
);
561 if (!poss_true
&& !poss_false
)
563 if (poss_true
&& !poss_false
)
565 if (!poss_true
&& poss_false
)
570 static struct range_list
*handle_comparison_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
575 if (get_value(expr
->left
, &left
) && get_value(expr
->right
, &right
)) {
576 struct data_range tmp_left
, tmp_right
;
580 tmp_right
.min
= right
;
581 tmp_right
.max
= right
;
582 if (true_comparison_range(&tmp_left
, expr
->op
, &tmp_right
))
587 if (implied
== RL_EXACT
)
590 res
= do_comparison(expr
);
596 return alloc_rl(zero
, one
);
599 static struct range_list
*handle_logical_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
605 if (implied
== RL_EXACT
) {
606 if (get_value(expr
->left
, &left
))
608 if (get_value(expr
->right
, &right
))
611 if (get_implied_value_internal(expr
->left
, &left
, recurse_cnt
))
613 if (get_implied_value_internal(expr
->right
, &right
, recurse_cnt
))
618 case SPECIAL_LOGICAL_OR
:
619 if (left_known
&& left
.value
)
621 if (right_known
&& right
.value
)
623 if (left_known
&& right_known
)
626 case SPECIAL_LOGICAL_AND
:
627 if (left_known
&& right_known
) {
628 if (left
.value
&& right
.value
)
637 if (implied
== RL_EXACT
)
640 return alloc_rl(zero
, one
);
643 static struct range_list
*handle_conditional_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
645 struct range_list
*true_rl
, *false_rl
;
647 int final_pass_orig
= final_pass
;
649 if (known_condition_true(expr
->conditional
))
650 return _get_rl(expr
->cond_true
, implied
, recurse_cnt
);
651 if (known_condition_false(expr
->conditional
))
652 return _get_rl(expr
->cond_false
, implied
, recurse_cnt
);
654 if (implied
== RL_EXACT
)
657 if (implied_condition_true(expr
->conditional
))
658 return _get_rl(expr
->cond_true
, implied
, recurse_cnt
);
659 if (implied_condition_false(expr
->conditional
))
660 return _get_rl(expr
->cond_false
, implied
, recurse_cnt
);
663 /* this becomes a problem with deeply nested conditional statements */
667 type
= get_type(expr
);
669 __push_fake_cur_stree();
671 __split_whole_condition(expr
->conditional
);
672 true_rl
= _get_rl(expr
->cond_true
, implied
, recurse_cnt
);
673 __push_true_states();
674 __use_false_states();
675 false_rl
= _get_rl(expr
->cond_false
, implied
, recurse_cnt
);
676 __merge_true_states();
677 __free_fake_cur_stree();
678 final_pass
= final_pass_orig
;
680 if (!true_rl
|| !false_rl
)
682 true_rl
= cast_rl(type
, true_rl
);
683 false_rl
= cast_rl(type
, false_rl
);
685 return rl_union(true_rl
, false_rl
);
688 static int get_fuzzy_max_helper(struct expression
*expr
, sval_t
*max
)
690 struct smatch_state
*state
;
693 if (get_hard_max(expr
, &sval
)) {
698 state
= get_extra_state(expr
);
699 if (!state
|| !estate_has_fuzzy_max(state
))
701 *max
= sval_cast(get_type(expr
), estate_get_fuzzy_max(state
));
705 static int get_fuzzy_min_helper(struct expression
*expr
, sval_t
*min
)
707 struct smatch_state
*state
;
710 state
= get_extra_state(expr
);
711 if (!state
|| !estate_rl(state
))
714 sval
= estate_min(state
);
715 if (sval_is_negative(sval
) && sval_is_min(sval
))
718 if (sval_is_max(sval
))
721 *min
= sval_cast(get_type(expr
), sval
);
725 int get_const_value(struct expression
*expr
, sval_t
*sval
)
730 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
733 if (!(sym
->ctype
.modifiers
& MOD_CONST
))
735 if (get_value(sym
->initializer
, &right
)) {
736 *sval
= sval_cast(get_type(expr
), right
);
742 struct range_list
*var_to_absolute_rl(struct expression
*expr
)
744 struct smatch_state
*state
;
745 struct range_list
*rl
;
747 state
= get_extra_state(expr
);
749 if (get_local_rl(expr
, &rl
))
751 if (get_db_type_rl(expr
, &rl
))
753 return alloc_whole_rl(get_type(expr
));
755 /* err on the side of saying things are possible */
756 if (!estate_rl(state
))
757 return alloc_whole_rl(get_type(expr
));
758 return clone_rl(estate_rl(state
));
761 static struct range_list
*handle_variable(struct expression
*expr
, int implied
, int *recurse_cnt
)
763 struct smatch_state
*state
;
764 struct range_list
*rl
;
765 sval_t sval
, min
, max
;
767 if (get_const_value(expr
, &sval
))
768 return alloc_rl(sval
, sval
);
770 if (custom_handle_variable
) {
771 rl
= custom_handle_variable(expr
);
773 return var_to_absolute_rl(expr
);
783 state
= get_extra_state(expr
);
784 if (!state
|| !state
->data
) {
785 if (implied
== RL_HARD
)
787 if (get_local_rl(expr
, &rl
))
789 if (get_db_type_rl(expr
, &rl
))
793 if (implied
== RL_HARD
&& !estate_has_hard_max(state
))
795 return clone_rl(estate_rl(state
));
797 if (!get_fuzzy_min_helper(expr
, &min
))
798 min
= sval_type_min(get_type(expr
));
799 if (!get_fuzzy_max_helper(expr
, &max
))
801 /* fuzzy ranges are often inverted */
802 if (sval_cmp(min
, max
) > 0) {
807 return alloc_rl(min
, max
);
812 static sval_t
handle_sizeof(struct expression
*expr
)
817 ret
= sval_blank(expr
);
818 sym
= expr
->cast_type
;
820 sym
= evaluate_expression(expr
->cast_expression
);
822 * Expressions of restricted types will possibly get
823 * promoted - check that here
825 if (is_restricted_type(sym
)) {
826 if (type_bits(sym
) < bits_in_int
)
828 } else if (is_fouled_type(sym
)) {
832 examine_symbol_type(sym
);
834 ret
.type
= size_t_ctype
;
835 if (type_bits(sym
) <= 0) /* sizeof(void) */ {
836 if (get_real_base_type(sym
) == &void_ctype
)
841 ret
.value
= type_bytes(sym
);
846 static struct range_list
*handle_call_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
848 struct range_list
*rl
;
850 if (sym_name_is("__builtin_expect", expr
->fn
)) {
851 struct expression
*arg
;
853 arg
= get_argument_from_call_expr(expr
->args
, 0);
854 return _get_rl(arg
, implied
, recurse_cnt
);
857 if (implied
== RL_EXACT
|| implied
== RL_HARD
|| implied
== RL_FUZZY
)
860 if (get_implied_return(expr
, &rl
))
862 return db_return_vals(expr
);
865 static struct range_list
*handle_cast(struct expression
*expr
, int implied
, int *recurse_cnt
)
867 struct range_list
*rl
;
870 type
= get_type(expr
);
871 rl
= _get_rl(expr
->cast_expression
, implied
, recurse_cnt
);
873 return cast_rl(type
, rl
);
874 if (implied
== RL_ABSOLUTE
)
875 return alloc_whole_rl(type
);
876 if (implied
== RL_IMPLIED
&& type
&&
877 type_bits(type
) > 0 && type_bits(type
) < 32)
878 return alloc_whole_rl(type
);
882 static struct range_list
*_get_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
884 struct range_list
*rl
;
888 type
= get_type(expr
);
889 expr
= strip_parens(expr
);
893 if (++(*recurse_cnt
) >= 200)
898 case EXPR_FORCE_CAST
:
899 case EXPR_IMPLIED_CAST
:
900 rl
= handle_cast(expr
, implied
, recurse_cnt
);
904 expr
= strip_expr(expr
);
908 switch (expr
->type
) {
910 sval
= sval_from_val(expr
, expr
->value
);
911 rl
= alloc_rl(sval
, sval
);
914 rl
= handle_preop_rl(expr
, implied
, recurse_cnt
);
917 rl
= _get_rl(expr
->unop
, implied
, recurse_cnt
);
920 rl
= handle_binop_rl(expr
, implied
, recurse_cnt
);
923 rl
= handle_comparison_rl(expr
, implied
, recurse_cnt
);
926 rl
= handle_logical_rl(expr
, implied
, recurse_cnt
);
930 sval
= handle_sizeof(expr
);
931 rl
= alloc_rl(sval
, sval
);
934 case EXPR_CONDITIONAL
:
935 rl
= handle_conditional_rl(expr
, implied
, recurse_cnt
);
938 rl
= handle_call_rl(expr
, implied
, recurse_cnt
);
941 rl
= handle_variable(expr
, implied
, recurse_cnt
);
947 if (type
&& implied
== RL_ABSOLUTE
)
948 return alloc_whole_rl(type
);
952 /* returns 1 if it can get a value literal or else returns 0 */
953 int get_value(struct expression
*expr
, sval_t
*sval
)
955 struct range_list
*rl
;
958 rl
= _get_rl(expr
, RL_EXACT
, &recurse_cnt
);
959 if (!rl_to_sval(rl
, sval
))
964 static int get_implied_value_internal(struct expression
*expr
, sval_t
*sval
, int *recurse_cnt
)
966 struct range_list
*rl
;
968 rl
= _get_rl(expr
, RL_IMPLIED
, recurse_cnt
);
969 if (!rl_to_sval(rl
, sval
))
974 int get_implied_value(struct expression
*expr
, sval_t
*sval
)
976 struct range_list
*rl
;
979 rl
= _get_rl(expr
, RL_IMPLIED
, &recurse_cnt
);
980 if (!rl_to_sval(rl
, sval
))
985 int get_implied_min(struct expression
*expr
, sval_t
*sval
)
987 struct range_list
*rl
;
990 rl
= _get_rl(expr
, RL_IMPLIED
, &recurse_cnt
);
997 int get_implied_max(struct expression
*expr
, sval_t
*sval
)
999 struct range_list
*rl
;
1000 int recurse_cnt
= 0;
1002 rl
= _get_rl(expr
, RL_IMPLIED
, &recurse_cnt
);
1009 int get_implied_rl(struct expression
*expr
, struct range_list
**rl
)
1011 int recurse_cnt
= 0;
1013 *rl
= _get_rl(expr
, RL_IMPLIED
, &recurse_cnt
);
1019 static int get_absolute_rl_internal(struct expression
*expr
, struct range_list
**rl
, int *recurse_cnt
)
1021 *rl
= _get_rl(expr
, RL_ABSOLUTE
, recurse_cnt
);
1023 *rl
= alloc_whole_rl(get_type(expr
));
1027 int get_absolute_rl(struct expression
*expr
, struct range_list
**rl
)
1029 int recurse_cnt
= 0;
1031 *rl
= _get_rl(expr
, RL_ABSOLUTE
, &recurse_cnt
);
1033 *rl
= alloc_whole_rl(get_type(expr
));
1037 int custom_get_absolute_rl(struct expression
*expr
,
1038 struct range_list
*(*fn
)(struct expression
*expr
),
1039 struct range_list
**rl
)
1041 int recurse_cnt
= 0;
1044 custom_handle_variable
= fn
;
1045 *rl
= _get_rl(expr
, RL_ABSOLUTE
, &recurse_cnt
);
1046 custom_handle_variable
= NULL
;
1050 int get_implied_rl_var_sym(const char *var
, struct symbol
*sym
, struct range_list
**rl
)
1052 struct smatch_state
*state
;
1054 state
= get_state(SMATCH_EXTRA
, var
, sym
);
1055 *rl
= estate_rl(state
);
1061 int get_hard_max(struct expression
*expr
, sval_t
*sval
)
1063 struct range_list
*rl
;
1064 int recurse_cnt
= 0;
1066 rl
= _get_rl(expr
, RL_HARD
, &recurse_cnt
);
1073 int get_fuzzy_min(struct expression
*expr
, sval_t
*sval
)
1075 struct range_list
*rl
;
1077 int recurse_cnt
= 0;
1079 rl
= _get_rl(expr
, RL_FUZZY
, &recurse_cnt
);
1083 if (sval_is_negative(tmp
) && sval_is_min(tmp
))
1089 int get_fuzzy_max(struct expression
*expr
, sval_t
*sval
)
1091 struct range_list
*rl
;
1093 int recurse_cnt
= 0;
1095 rl
= _get_rl(expr
, RL_FUZZY
, &recurse_cnt
);
1099 if (max
.uvalue
> INT_MAX
- 10000)
1105 int get_absolute_min(struct expression
*expr
, sval_t
*sval
)
1107 struct range_list
*rl
;
1108 struct symbol
*type
;
1109 int recurse_cnt
= 0;
1111 type
= get_type(expr
);
1113 type
= &llong_ctype
; // FIXME: this is wrong but places assume get type can't fail.
1114 rl
= _get_rl(expr
, RL_ABSOLUTE
, &recurse_cnt
);
1118 *sval
= sval_type_min(type
);
1120 if (sval_cmp(*sval
, sval_type_min(type
)) < 0)
1121 *sval
= sval_type_min(type
);
1125 int get_absolute_max(struct expression
*expr
, sval_t
*sval
)
1127 struct range_list
*rl
;
1128 struct symbol
*type
;
1129 int recurse_cnt
= 0;
1131 type
= get_type(expr
);
1133 type
= &llong_ctype
;
1134 rl
= _get_rl(expr
, RL_ABSOLUTE
, &recurse_cnt
);
1138 *sval
= sval_type_max(type
);
1140 if (sval_cmp(sval_type_max(type
), *sval
) < 0)
1141 *sval
= sval_type_max(type
);
1145 int known_condition_true(struct expression
*expr
)
1152 if (get_value(expr
, &tmp
) && tmp
.value
)
1158 int known_condition_false(struct expression
*expr
)
1166 if (expr
->type
== EXPR_CALL
) {
1167 if (sym_name_is("__builtin_constant_p", expr
->fn
))
1173 int implied_condition_true(struct expression
*expr
)
1180 if (known_condition_true(expr
))
1182 if (get_implied_value(expr
, &tmp
) && tmp
.value
)
1185 if (expr
->type
== EXPR_POSTOP
)
1186 return implied_condition_true(expr
->unop
);
1188 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_DECREMENT
)
1189 return implied_not_equal(expr
->unop
, 1);
1190 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_INCREMENT
)
1191 return implied_not_equal(expr
->unop
, -1);
1193 expr
= strip_expr(expr
);
1194 switch (expr
->type
) {
1196 if (do_comparison(expr
) == 1)
1200 if (expr
->op
== '!') {
1201 if (implied_condition_false(expr
->unop
))
1207 if (implied_not_equal(expr
, 0) == 1)
1214 int implied_condition_false(struct expression
*expr
)
1216 struct expression
*tmp
;
1222 if (known_condition_false(expr
))
1225 switch (expr
->type
) {
1227 if (do_comparison(expr
) == 2)
1230 if (expr
->op
== '!') {
1231 if (implied_condition_true(expr
->unop
))
1235 tmp
= strip_expr(expr
);
1237 return implied_condition_false(tmp
);
1240 if (get_implied_value(expr
, &sval
) && sval
.value
== 0)
1247 int can_integer_overflow(struct symbol
*type
, struct expression
*expr
)
1250 sval_t lmax
, rmax
, res
;
1255 expr
= strip_expr(expr
);
1257 if (expr
->type
== EXPR_ASSIGNMENT
) {
1259 case SPECIAL_MUL_ASSIGN
:
1262 case SPECIAL_ADD_ASSIGN
:
1265 case SPECIAL_SHL_ASSIGN
:
1266 op
= SPECIAL_LEFTSHIFT
;
1271 } else if (expr
->type
== EXPR_BINOP
) {
1272 if (expr
->op
!= '*' && expr
->op
!= '+' && expr
->op
!= SPECIAL_LEFTSHIFT
)
1279 get_absolute_max(expr
->left
, &lmax
);
1280 get_absolute_max(expr
->right
, &rmax
);
1282 if (sval_binop_overflows(lmax
, op
, rmax
))
1285 res
= sval_binop(lmax
, op
, rmax
);
1286 if (sval_cmp(res
, sval_type_max(type
)) > 0)