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
);
52 static struct range_list
*last_stmt_rl(struct statement
*stmt
, int implied
, int *recurse_cnt
)
54 struct expression
*expr
;
59 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
60 if (stmt
->type
== STMT_LABEL
) {
61 if (stmt
->label_statement
&&
62 stmt
->label_statement
->type
== STMT_EXPRESSION
)
63 expr
= stmt
->label_statement
->expression
;
66 } else if (stmt
->type
== STMT_EXPRESSION
) {
67 expr
= stmt
->expression
;
71 return _get_rl(expr
, implied
, recurse_cnt
);
74 static struct range_list
*handle_expression_statement_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
76 return last_stmt_rl(get_expression_statement(expr
), implied
, recurse_cnt
);
79 static struct range_list
*handle_ampersand_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
81 struct range_list
*rl
;
83 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
85 if (get_address_rl(expr
, &rl
))
87 return alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
90 static struct range_list
*handle_negate_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
92 if (known_condition_true(expr
->unop
))
94 if (known_condition_false(expr
->unop
))
97 if (implied
== RL_EXACT
)
100 if (implied_condition_true(expr
->unop
))
102 if (implied_condition_false(expr
->unop
))
104 return alloc_rl(zero
, one
);
107 static struct range_list
*handle_bitwise_negate(struct expression
*expr
, int implied
, int *recurse_cnt
)
109 struct range_list
*rl
;
112 rl
= _get_rl(expr
->unop
, implied
, recurse_cnt
);
113 if (!rl_to_sval(rl
, &sval
))
115 sval
= sval_preop(sval
, '~');
116 sval_cast(get_type(expr
->unop
), sval
);
117 return alloc_rl(sval
, sval
);
120 static struct range_list
*handle_minus_preop(struct expression
*expr
, int implied
, int *recurse_cnt
)
122 struct range_list
*rl
;
125 rl
= _get_rl(expr
->unop
, implied
, recurse_cnt
);
126 if (!rl_to_sval(rl
, &sval
))
128 sval
= sval_preop(sval
, '-');
129 return alloc_rl(sval
, sval
);
132 static struct range_list
*handle_preop_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
136 return handle_ampersand_rl(expr
, implied
, recurse_cnt
);
138 return handle_negate_rl(expr
, implied
, recurse_cnt
);
140 return handle_bitwise_negate(expr
, implied
, recurse_cnt
);
142 return handle_minus_preop(expr
, implied
, recurse_cnt
);
144 return handle_variable(expr
, implied
, recurse_cnt
);
146 return handle_expression_statement_rl(expr
, implied
, recurse_cnt
);
152 static struct range_list
*handle_divide_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
154 struct range_list
*left_rl
, *right_rl
;
157 type
= get_type(expr
);
159 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
160 left_rl
= cast_rl(type
, left_rl
);
161 right_rl
= _get_rl(expr
->right
, implied
, recurse_cnt
);
162 right_rl
= cast_rl(type
, right_rl
);
164 if (!left_rl
|| !right_rl
)
167 if (implied
!= RL_REAL_ABSOLUTE
) {
168 if (is_whole_rl(left_rl
) || is_whole_rl(right_rl
))
172 return rl_binop(left_rl
, '/', right_rl
);
175 static struct range_list
*handle_subtract_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
178 struct range_list
*left_rl
, *right_rl
;
179 sval_t max
, min
, tmp
;
182 type
= get_type(expr
);
183 comparison
= get_comparison(expr
->left
, expr
->right
);
185 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
186 left_rl
= cast_rl(type
, left_rl
);
187 right_rl
= _get_rl(expr
->right
, implied
, recurse_cnt
);
188 right_rl
= cast_rl(type
, right_rl
);
190 if ((!left_rl
|| !right_rl
) &&
191 (implied
== RL_EXACT
|| implied
== RL_HARD
|| implied
== RL_FUZZY
))
195 left_rl
= alloc_whole_rl(type
);
197 right_rl
= alloc_whole_rl(type
);
199 /* negative values complicate everything fix this later */
200 if (sval_is_negative(rl_min(right_rl
)))
202 max
= rl_max(left_rl
);
204 switch (comparison
) {
206 case SPECIAL_UNSIGNED_GT
:
207 min
= sval_type_val(type
, 1);
208 max
= rl_max(left_rl
);
211 case SPECIAL_UNSIGNED_GTE
:
212 min
= sval_type_val(type
, 0);
213 max
= rl_max(left_rl
);
216 if (sval_binop_overflows(rl_min(left_rl
), '-', rl_max(right_rl
)))
218 min
= sval_type_min(type
);
221 if (!sval_binop_overflows(rl_min(left_rl
), '-', rl_max(right_rl
))) {
222 tmp
= sval_binop(rl_min(left_rl
), '-', rl_max(right_rl
));
223 if (sval_cmp(tmp
, min
) > 0)
227 if (!sval_is_max(rl_max(left_rl
))) {
228 tmp
= sval_binop(rl_max(left_rl
), '-', rl_min(right_rl
));
229 if (sval_cmp(tmp
, max
) < 0)
233 if (sval_is_min(min
) && sval_is_max(max
))
236 return cast_rl(type
, alloc_rl(min
, max
));
239 static struct range_list
*handle_mod_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
241 struct range_list
*rl
;
242 sval_t left
, right
, sval
;
244 if (implied
== RL_EXACT
) {
245 if (!get_value(expr
->right
, &right
))
247 if (!get_value(expr
->left
, &left
))
249 sval
= sval_binop(left
, '%', right
);
250 return alloc_rl(sval
, sval
);
252 /* if we can't figure out the right side it's probably hopeless */
253 if (!get_implied_value_internal(expr
->right
, &right
, recurse_cnt
))
256 right
= sval_cast(get_type(expr
), right
);
259 rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
260 if (rl
&& rl_max(rl
).uvalue
< right
.uvalue
)
261 right
.uvalue
= rl_max(rl
).uvalue
;
263 return alloc_rl(zero
, right
);
266 static sval_t
sval_lowest_set_bit(sval_t sval
)
271 for (i
= 0; i
< 64; i
++) {
272 if (sval
.uvalue
& 1ULL << i
) {
275 sval
.uvalue
&= ~(1ULL << i
);
281 static struct range_list
*handle_bitwise_AND(struct expression
*expr
, int implied
, int *recurse_cnt
)
284 struct range_list
*left_rl
, *right_rl
;
287 if (implied
!= RL_IMPLIED
&& implied
!= RL_ABSOLUTE
&& implied
!= RL_REAL_ABSOLUTE
)
290 type
= get_type(expr
);
292 if (get_implied_value_internal(expr
->left
, &known
, recurse_cnt
)) {
295 min
= sval_lowest_set_bit(known
);
296 left_rl
= alloc_rl(min
, known
);
297 left_rl
= cast_rl(type
, left_rl
);
298 add_range(&left_rl
, sval_type_val(type
, 0), sval_type_val(type
, 0));
300 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
302 left_rl
= cast_rl(type
, left_rl
);
303 left_rl
= alloc_rl(sval_type_val(type
, 0), rl_max(left_rl
));
305 if (implied
== RL_HARD
)
307 left_rl
= alloc_whole_rl(type
);
311 if (get_implied_value_internal(expr
->right
, &known
, recurse_cnt
)) {
312 sval_t min
, left_max
, mod
;
314 min
= sval_lowest_set_bit(known
);
315 right_rl
= alloc_rl(min
, known
);
316 right_rl
= cast_rl(type
, right_rl
);
317 add_range(&right_rl
, sval_type_val(type
, 0), sval_type_val(type
, 0));
319 if (min
.value
!= 0) {
320 left_max
= rl_max(left_rl
);
321 mod
= sval_binop(left_max
, '%', min
);
323 left_max
= sval_binop(left_max
, '-', mod
);
325 if (left_max
.value
> 0 && sval_cmp(left_max
, rl_max(left_rl
)) < 0)
326 left_rl
= remove_range(left_rl
, left_max
, rl_max(left_rl
));
330 right_rl
= _get_rl(expr
->right
, implied
, recurse_cnt
);
332 right_rl
= cast_rl(type
, right_rl
);
333 right_rl
= alloc_rl(sval_type_val(type
, 0), rl_max(right_rl
));
335 if (implied
== RL_HARD
)
337 right_rl
= alloc_whole_rl(type
);
341 return rl_intersection(left_rl
, right_rl
);
344 static struct range_list
*use_rl_binop(struct expression
*expr
, int implied
, int *recurse_cnt
)
347 struct range_list
*left_rl
, *right_rl
;
349 if (implied
!= RL_IMPLIED
&& implied
!= RL_ABSOLUTE
&& implied
!= RL_REAL_ABSOLUTE
)
352 type
= get_type(expr
);
354 get_absolute_rl_internal(expr
->left
, &left_rl
, recurse_cnt
);
355 get_absolute_rl_internal(expr
->right
, &right_rl
, recurse_cnt
);
356 left_rl
= cast_rl(type
, left_rl
);
357 right_rl
= cast_rl(type
, right_rl
);
358 if (!left_rl
|| !right_rl
)
361 return rl_binop(left_rl
, expr
->op
, right_rl
);
364 static struct range_list
*handle_right_shift(struct expression
*expr
, int implied
, int *recurse_cnt
)
366 struct range_list
*left_rl
;
370 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
373 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
375 max
= rl_max(left_rl
);
376 min
= rl_min(left_rl
);
378 if (implied
== RL_FUZZY
)
380 max
= sval_type_max(get_type(expr
->left
));
381 min
= sval_type_val(get_type(expr
->left
), 0);
384 if (get_implied_value_internal(expr
->right
, &right
, recurse_cnt
)) {
385 min
= sval_binop(min
, SPECIAL_RIGHTSHIFT
, right
);
386 max
= sval_binop(max
, SPECIAL_RIGHTSHIFT
, right
);
387 } else if (!sval_is_negative(min
)) {
389 max
= sval_type_max(max
.type
);
394 return alloc_rl(min
, max
);
397 static struct range_list
*handle_left_shift(struct expression
*expr
, int implied
, int *recurse_cnt
)
399 struct range_list
*left_rl
, *res
;
404 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
406 /* this is hopeless without the right side */
407 if (!get_implied_value_internal(expr
->right
, &right
, recurse_cnt
))
409 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
411 max
= rl_max(left_rl
);
412 min
= rl_min(left_rl
);
413 if (min
.value
== 0) {
418 if (implied
== RL_FUZZY
)
420 max
= sval_type_max(get_type(expr
->left
));
421 min
= sval_type_val(get_type(expr
->left
), 1);
425 max
= sval_binop(max
, SPECIAL_LEFTSHIFT
, right
);
426 min
= sval_binop(min
, SPECIAL_LEFTSHIFT
, right
);
427 res
= alloc_rl(min
, max
);
429 res
= rl_union(res
, rl_zero());
433 static struct range_list
*handle_known_binop(struct expression
*expr
)
437 if (!get_value(expr
->left
, &left
))
439 if (!get_value(expr
->right
, &right
))
441 left
= sval_binop(left
, expr
->op
, right
);
442 return alloc_rl(left
, left
);
445 static int has_actual_ranges(struct range_list
*rl
)
447 struct data_range
*tmp
;
449 FOR_EACH_PTR(rl
, tmp
) {
450 if (sval_cmp(tmp
->min
, tmp
->max
) != 0)
452 } END_FOR_EACH_PTR(tmp
);
456 static struct range_list
*handle_implied_binop(struct range_list
*left_rl
, int op
, struct range_list
*right_rl
)
458 struct range_list
*res_rl
;
459 struct data_range
*left_drange
, *right_drange
;
462 if (!left_rl
|| !right_rl
)
464 if (has_actual_ranges(left_rl
))
466 if (has_actual_ranges(right_rl
))
469 if (ptr_list_size((struct ptr_list
*)left_rl
) * ptr_list_size((struct ptr_list
*)right_rl
) > 20)
474 FOR_EACH_PTR(left_rl
, left_drange
) {
475 FOR_EACH_PTR(right_rl
, right_drange
) {
476 if ((op
== '%' || op
== '/') &&
477 right_drange
->min
.value
== 0)
479 res
= sval_binop(left_drange
->min
, op
, right_drange
->min
);
480 add_range(&res_rl
, res
, res
);
481 } END_FOR_EACH_PTR(right_drange
);
482 } END_FOR_EACH_PTR(left_drange
);
487 static struct range_list
*handle_binop_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
489 struct smatch_state
*state
;
491 struct range_list
*left_rl
, *right_rl
, *rl
;
494 rl
= handle_known_binop(expr
);
497 if (implied
== RL_EXACT
)
500 state
= get_extra_state(expr
);
501 if (state
&& !is_whole_rl(estate_rl(state
)))
502 return clone_rl(estate_rl(state
));
504 type
= get_type(expr
);
505 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
506 left_rl
= cast_rl(type
, left_rl
);
507 right_rl
= _get_rl(expr
->right
, implied
, recurse_cnt
);
508 right_rl
= cast_rl(type
, right_rl
);
510 if (!left_rl
&& !right_rl
)
513 rl
= handle_implied_binop(left_rl
, expr
->op
, right_rl
);
519 return handle_mod_rl(expr
, implied
, recurse_cnt
);
521 return handle_bitwise_AND(expr
, implied
, recurse_cnt
);
524 return use_rl_binop(expr
, implied
, recurse_cnt
);
525 case SPECIAL_RIGHTSHIFT
:
526 return handle_right_shift(expr
, implied
, recurse_cnt
);
527 case SPECIAL_LEFTSHIFT
:
528 return handle_left_shift(expr
, implied
, recurse_cnt
);
530 return handle_subtract_rl(expr
, implied
, recurse_cnt
);
532 return handle_divide_rl(expr
, implied
, recurse_cnt
);
535 if (!left_rl
|| !right_rl
)
538 if (sval_binop_overflows(rl_min(left_rl
), expr
->op
, rl_min(right_rl
)))
540 if (sval_binop_overflows(rl_max(left_rl
), expr
->op
, rl_max(right_rl
)))
543 min
= sval_binop(rl_min(left_rl
), expr
->op
, rl_min(right_rl
));
544 max
= sval_binop(rl_max(left_rl
), expr
->op
, rl_max(right_rl
));
546 return alloc_rl(min
, max
);
549 static int do_comparison(struct expression
*expr
)
551 struct range_list
*left_ranges
= NULL
;
552 struct range_list
*right_ranges
= NULL
;
553 int poss_true
, poss_false
;
556 type
= get_type(expr
);
557 get_absolute_rl(expr
->left
, &left_ranges
);
558 get_absolute_rl(expr
->right
, &right_ranges
);
560 left_ranges
= cast_rl(type
, left_ranges
);
561 right_ranges
= cast_rl(type
, right_ranges
);
563 poss_true
= possibly_true_rl(left_ranges
, expr
->op
, right_ranges
);
564 poss_false
= possibly_false_rl(left_ranges
, expr
->op
, right_ranges
);
566 free_rl(&left_ranges
);
567 free_rl(&right_ranges
);
569 if (!poss_true
&& !poss_false
)
571 if (poss_true
&& !poss_false
)
573 if (!poss_true
&& poss_false
)
578 static struct range_list
*handle_comparison_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
583 if (get_value(expr
->left
, &left
) && get_value(expr
->right
, &right
)) {
584 struct data_range tmp_left
, tmp_right
;
588 tmp_right
.min
= right
;
589 tmp_right
.max
= right
;
590 if (true_comparison_range(&tmp_left
, expr
->op
, &tmp_right
))
595 if (implied
== RL_EXACT
)
598 res
= do_comparison(expr
);
604 return alloc_rl(zero
, one
);
607 static struct range_list
*handle_logical_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
613 if (implied
== RL_EXACT
) {
614 if (get_value(expr
->left
, &left
))
616 if (get_value(expr
->right
, &right
))
619 if (get_implied_value_internal(expr
->left
, &left
, recurse_cnt
))
621 if (get_implied_value_internal(expr
->right
, &right
, recurse_cnt
))
626 case SPECIAL_LOGICAL_OR
:
627 if (left_known
&& left
.value
)
629 if (right_known
&& right
.value
)
631 if (left_known
&& right_known
)
634 case SPECIAL_LOGICAL_AND
:
635 if (left_known
&& right_known
) {
636 if (left
.value
&& right
.value
)
645 if (implied
== RL_EXACT
)
648 return alloc_rl(zero
, one
);
651 static struct range_list
*handle_conditional_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
653 struct range_list
*true_rl
, *false_rl
;
655 int final_pass_orig
= final_pass
;
657 if (known_condition_true(expr
->conditional
))
658 return _get_rl(expr
->cond_true
, implied
, recurse_cnt
);
659 if (known_condition_false(expr
->conditional
))
660 return _get_rl(expr
->cond_false
, implied
, recurse_cnt
);
662 if (implied
== RL_EXACT
)
665 if (implied_condition_true(expr
->conditional
))
666 return _get_rl(expr
->cond_true
, implied
, recurse_cnt
);
667 if (implied_condition_false(expr
->conditional
))
668 return _get_rl(expr
->cond_false
, implied
, recurse_cnt
);
671 /* this becomes a problem with deeply nested conditional statements */
675 type
= get_type(expr
);
677 __push_fake_cur_stree();
679 __split_whole_condition(expr
->conditional
);
680 true_rl
= _get_rl(expr
->cond_true
, implied
, recurse_cnt
);
681 __push_true_states();
682 __use_false_states();
683 false_rl
= _get_rl(expr
->cond_false
, implied
, recurse_cnt
);
684 __merge_true_states();
685 __free_fake_cur_stree();
686 final_pass
= final_pass_orig
;
688 if (!true_rl
|| !false_rl
)
690 true_rl
= cast_rl(type
, true_rl
);
691 false_rl
= cast_rl(type
, false_rl
);
693 return rl_union(true_rl
, false_rl
);
696 static int get_fuzzy_max_helper(struct expression
*expr
, sval_t
*max
)
698 struct smatch_state
*state
;
701 if (get_hard_max(expr
, &sval
)) {
706 state
= get_extra_state(expr
);
707 if (!state
|| !estate_has_fuzzy_max(state
))
709 *max
= sval_cast(get_type(expr
), estate_get_fuzzy_max(state
));
713 static int get_fuzzy_min_helper(struct expression
*expr
, sval_t
*min
)
715 struct smatch_state
*state
;
718 state
= get_extra_state(expr
);
719 if (!state
|| !estate_rl(state
))
722 sval
= estate_min(state
);
723 if (sval_is_negative(sval
) && sval_is_min(sval
))
726 if (sval_is_max(sval
))
729 *min
= sval_cast(get_type(expr
), sval
);
733 int get_const_value(struct expression
*expr
, sval_t
*sval
)
738 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
741 if (!(sym
->ctype
.modifiers
& MOD_CONST
))
743 if (get_value(sym
->initializer
, &right
)) {
744 *sval
= sval_cast(get_type(expr
), right
);
750 struct range_list
*var_to_absolute_rl(struct expression
*expr
)
752 struct smatch_state
*state
;
753 struct range_list
*rl
;
755 state
= get_extra_state(expr
);
756 if (!state
|| is_whole_rl(estate_rl(state
))) {
757 state
= get_real_absolute_state(expr
);
758 if (state
&& state
->data
&& !estate_is_whole(state
))
759 return clone_rl(estate_rl(state
));
760 if (get_local_rl(expr
, &rl
) && !is_whole_rl(rl
))
762 if (get_db_type_rl(expr
, &rl
) && !is_whole_rl(rl
))
764 return alloc_whole_rl(get_type(expr
));
766 /* err on the side of saying things are possible */
767 if (!estate_rl(state
))
768 return alloc_whole_rl(get_type(expr
));
769 return clone_rl(estate_rl(state
));
772 static struct range_list
*handle_variable(struct expression
*expr
, int implied
, int *recurse_cnt
)
774 struct smatch_state
*state
;
775 struct range_list
*rl
;
776 sval_t sval
, min
, max
;
778 if (get_const_value(expr
, &sval
))
779 return alloc_rl(sval
, sval
);
781 if (custom_handle_variable
) {
782 rl
= custom_handle_variable(expr
);
784 return var_to_absolute_rl(expr
);
794 state
= get_extra_state(expr
);
795 if (!state
|| !state
->data
) {
796 if (implied
== RL_HARD
)
798 if (get_local_rl(expr
, &rl
))
800 if (get_db_type_rl(expr
, &rl
))
804 if (implied
== RL_HARD
&& !estate_has_hard_max(state
))
806 return clone_rl(estate_rl(state
));
807 case RL_REAL_ABSOLUTE
:
808 state
= get_extra_state(expr
);
809 if (!state
|| !state
->data
|| is_whole_rl(estate_rl(state
))) {
810 state
= get_real_absolute_state(expr
);
811 if (state
&& state
->data
)
812 return clone_rl(estate_rl(state
));
813 if (get_local_rl(expr
, &rl
))
815 if (get_db_type_rl(expr
, &rl
))
819 return clone_rl(estate_rl(state
));
821 if (!get_fuzzy_min_helper(expr
, &min
))
822 min
= sval_type_min(get_type(expr
));
823 if (!get_fuzzy_max_helper(expr
, &max
))
825 /* fuzzy ranges are often inverted */
826 if (sval_cmp(min
, max
) > 0) {
831 return alloc_rl(min
, max
);
836 static sval_t
handle_sizeof(struct expression
*expr
)
841 ret
= sval_blank(expr
);
842 sym
= expr
->cast_type
;
844 sym
= evaluate_expression(expr
->cast_expression
);
846 * Expressions of restricted types will possibly get
847 * promoted - check that here
849 if (is_restricted_type(sym
)) {
850 if (type_bits(sym
) < bits_in_int
)
852 } else if (is_fouled_type(sym
)) {
856 examine_symbol_type(sym
);
858 ret
.type
= size_t_ctype
;
859 if (type_bits(sym
) <= 0) /* sizeof(void) */ {
860 if (get_real_base_type(sym
) == &void_ctype
)
865 ret
.value
= type_bytes(sym
);
870 static struct range_list
*handle_strlen(struct expression
*expr
, int implied
, int *recurse_cnt
)
872 struct range_list
*rl
;
873 struct expression
*arg
;
874 sval_t sval
= { .type
= &ulong_ctype
};
876 if (implied
== RL_EXACT
)
879 arg
= get_argument_from_call_expr(expr
->args
, 0);
882 if (arg
->type
== EXPR_STRING
) {
883 sval
.value
= arg
->string
->length
- 1;
884 return alloc_rl(sval
, sval
);
887 if (implied
== RL_HARD
|| implied
== RL_FUZZY
)
890 if (get_implied_return(expr
, &rl
))
896 static struct range_list
*handle_call_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
898 struct range_list
*rl
;
900 if (sym_name_is("__builtin_expect", expr
->fn
)) {
901 struct expression
*arg
;
903 arg
= get_argument_from_call_expr(expr
->args
, 0);
904 return _get_rl(arg
, implied
, recurse_cnt
);
907 if (sym_name_is("strlen", expr
->fn
))
908 return handle_strlen(expr
, implied
, recurse_cnt
);
910 if (implied
== RL_EXACT
|| implied
== RL_HARD
|| implied
== RL_FUZZY
)
913 if (get_implied_return(expr
, &rl
))
915 return db_return_vals(expr
);
918 static struct range_list
*handle_cast(struct expression
*expr
, int implied
, int *recurse_cnt
)
920 struct range_list
*rl
;
923 type
= get_type(expr
);
924 rl
= _get_rl(expr
->cast_expression
, implied
, recurse_cnt
);
926 return cast_rl(type
, rl
);
927 if (implied
== RL_ABSOLUTE
|| implied
== RL_REAL_ABSOLUTE
)
928 return alloc_whole_rl(type
);
929 if (implied
== RL_IMPLIED
&& type
&&
930 type_bits(type
) > 0 && type_bits(type
) < 32)
931 return alloc_whole_rl(type
);
935 static struct range_list
*_get_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
937 struct range_list
*rl
;
941 type
= get_type(expr
);
942 expr
= strip_parens(expr
);
946 if (++(*recurse_cnt
) >= 200)
951 case EXPR_FORCE_CAST
:
952 case EXPR_IMPLIED_CAST
:
953 rl
= handle_cast(expr
, implied
, recurse_cnt
);
957 expr
= strip_expr(expr
);
961 switch (expr
->type
) {
963 sval
= sval_from_val(expr
, expr
->value
);
964 rl
= alloc_rl(sval
, sval
);
967 rl
= handle_preop_rl(expr
, implied
, recurse_cnt
);
970 rl
= _get_rl(expr
->unop
, implied
, recurse_cnt
);
973 rl
= handle_binop_rl(expr
, implied
, recurse_cnt
);
976 rl
= handle_comparison_rl(expr
, implied
, recurse_cnt
);
979 rl
= handle_logical_rl(expr
, implied
, recurse_cnt
);
983 sval
= handle_sizeof(expr
);
984 rl
= alloc_rl(sval
, sval
);
987 case EXPR_CONDITIONAL
:
988 rl
= handle_conditional_rl(expr
, implied
, recurse_cnt
);
991 rl
= handle_call_rl(expr
, implied
, recurse_cnt
);
994 rl
= handle_variable(expr
, implied
, recurse_cnt
);
1000 if (type
&& (implied
== RL_ABSOLUTE
|| implied
== RL_REAL_ABSOLUTE
))
1001 return alloc_whole_rl(type
);
1005 /* returns 1 if it can get a value literal or else returns 0 */
1006 int get_value(struct expression
*expr
, sval_t
*sval
)
1008 struct range_list
*rl
;
1009 int recurse_cnt
= 0;
1011 rl
= _get_rl(expr
, RL_EXACT
, &recurse_cnt
);
1012 if (!rl_to_sval(rl
, sval
))
1017 static int get_implied_value_internal(struct expression
*expr
, sval_t
*sval
, int *recurse_cnt
)
1019 struct range_list
*rl
;
1021 rl
= _get_rl(expr
, RL_IMPLIED
, recurse_cnt
);
1022 if (!rl_to_sval(rl
, sval
))
1027 int get_implied_value(struct expression
*expr
, sval_t
*sval
)
1029 struct range_list
*rl
;
1030 int recurse_cnt
= 0;
1032 rl
= _get_rl(expr
, RL_IMPLIED
, &recurse_cnt
);
1033 if (!rl_to_sval(rl
, sval
))
1038 int get_implied_min(struct expression
*expr
, sval_t
*sval
)
1040 struct range_list
*rl
;
1041 int recurse_cnt
= 0;
1043 rl
= _get_rl(expr
, RL_IMPLIED
, &recurse_cnt
);
1050 int get_implied_max(struct expression
*expr
, sval_t
*sval
)
1052 struct range_list
*rl
;
1053 int recurse_cnt
= 0;
1055 rl
= _get_rl(expr
, RL_IMPLIED
, &recurse_cnt
);
1062 int get_implied_rl(struct expression
*expr
, struct range_list
**rl
)
1064 int recurse_cnt
= 0;
1066 *rl
= _get_rl(expr
, RL_IMPLIED
, &recurse_cnt
);
1072 static int get_absolute_rl_internal(struct expression
*expr
, struct range_list
**rl
, int *recurse_cnt
)
1074 *rl
= _get_rl(expr
, RL_ABSOLUTE
, recurse_cnt
);
1076 *rl
= alloc_whole_rl(get_type(expr
));
1080 int get_absolute_rl(struct expression
*expr
, struct range_list
**rl
)
1082 int recurse_cnt
= 0;
1084 *rl
= _get_rl(expr
, RL_ABSOLUTE
, &recurse_cnt
);
1086 *rl
= alloc_whole_rl(get_type(expr
));
1090 int get_real_absolute_rl(struct expression
*expr
, struct range_list
**rl
)
1092 int recurse_cnt
= 0;
1094 *rl
= _get_rl(expr
, RL_REAL_ABSOLUTE
, &recurse_cnt
);
1096 *rl
= alloc_whole_rl(get_type(expr
));
1100 int custom_get_absolute_rl(struct expression
*expr
,
1101 struct range_list
*(*fn
)(struct expression
*expr
),
1102 struct range_list
**rl
)
1104 int recurse_cnt
= 0;
1107 custom_handle_variable
= fn
;
1108 *rl
= _get_rl(expr
, RL_REAL_ABSOLUTE
, &recurse_cnt
);
1109 custom_handle_variable
= NULL
;
1113 int get_implied_rl_var_sym(const char *var
, struct symbol
*sym
, struct range_list
**rl
)
1115 struct smatch_state
*state
;
1117 state
= get_state(SMATCH_EXTRA
, var
, sym
);
1118 *rl
= estate_rl(state
);
1124 int get_hard_max(struct expression
*expr
, sval_t
*sval
)
1126 struct range_list
*rl
;
1127 int recurse_cnt
= 0;
1129 rl
= _get_rl(expr
, RL_HARD
, &recurse_cnt
);
1136 int get_fuzzy_min(struct expression
*expr
, sval_t
*sval
)
1138 struct range_list
*rl
;
1140 int recurse_cnt
= 0;
1142 rl
= _get_rl(expr
, RL_FUZZY
, &recurse_cnt
);
1146 if (sval_is_negative(tmp
) && sval_is_min(tmp
))
1152 int get_fuzzy_max(struct expression
*expr
, sval_t
*sval
)
1154 struct range_list
*rl
;
1156 int recurse_cnt
= 0;
1158 rl
= _get_rl(expr
, RL_FUZZY
, &recurse_cnt
);
1162 if (max
.uvalue
> INT_MAX
- 10000)
1168 int get_absolute_min(struct expression
*expr
, sval_t
*sval
)
1170 struct range_list
*rl
;
1171 struct symbol
*type
;
1172 int recurse_cnt
= 0;
1174 type
= get_type(expr
);
1176 type
= &llong_ctype
; // FIXME: this is wrong but places assume get type can't fail.
1177 rl
= _get_rl(expr
, RL_ABSOLUTE
, &recurse_cnt
);
1181 *sval
= sval_type_min(type
);
1183 if (sval_cmp(*sval
, sval_type_min(type
)) < 0)
1184 *sval
= sval_type_min(type
);
1188 int get_absolute_max(struct expression
*expr
, sval_t
*sval
)
1190 struct range_list
*rl
;
1191 struct symbol
*type
;
1192 int recurse_cnt
= 0;
1194 type
= get_type(expr
);
1196 type
= &llong_ctype
;
1197 rl
= _get_rl(expr
, RL_ABSOLUTE
, &recurse_cnt
);
1201 *sval
= sval_type_max(type
);
1203 if (sval_cmp(sval_type_max(type
), *sval
) < 0)
1204 *sval
= sval_type_max(type
);
1208 int known_condition_true(struct expression
*expr
)
1215 if (get_value(expr
, &tmp
) && tmp
.value
)
1221 int known_condition_false(struct expression
*expr
)
1229 if (expr
->type
== EXPR_CALL
) {
1230 if (sym_name_is("__builtin_constant_p", expr
->fn
))
1236 int implied_condition_true(struct expression
*expr
)
1243 if (known_condition_true(expr
))
1245 if (get_implied_value(expr
, &tmp
) && tmp
.value
)
1248 if (expr
->type
== EXPR_POSTOP
)
1249 return implied_condition_true(expr
->unop
);
1251 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_DECREMENT
)
1252 return implied_not_equal(expr
->unop
, 1);
1253 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_INCREMENT
)
1254 return implied_not_equal(expr
->unop
, -1);
1256 expr
= strip_expr(expr
);
1257 switch (expr
->type
) {
1259 if (do_comparison(expr
) == 1)
1263 if (expr
->op
== '!') {
1264 if (implied_condition_false(expr
->unop
))
1270 if (implied_not_equal(expr
, 0) == 1)
1277 int implied_condition_false(struct expression
*expr
)
1279 struct expression
*tmp
;
1285 if (known_condition_false(expr
))
1288 switch (expr
->type
) {
1290 if (do_comparison(expr
) == 2)
1293 if (expr
->op
== '!') {
1294 if (implied_condition_true(expr
->unop
))
1298 tmp
= strip_expr(expr
);
1300 return implied_condition_false(tmp
);
1303 if (get_implied_value(expr
, &sval
) && sval
.value
== 0)
1310 int can_integer_overflow(struct symbol
*type
, struct expression
*expr
)
1313 sval_t lmax
, rmax
, res
;
1318 expr
= strip_expr(expr
);
1320 if (expr
->type
== EXPR_ASSIGNMENT
) {
1322 case SPECIAL_MUL_ASSIGN
:
1325 case SPECIAL_ADD_ASSIGN
:
1328 case SPECIAL_SHL_ASSIGN
:
1329 op
= SPECIAL_LEFTSHIFT
;
1334 } else if (expr
->type
== EXPR_BINOP
) {
1335 if (expr
->op
!= '*' && expr
->op
!= '+' && expr
->op
!= SPECIAL_LEFTSHIFT
)
1342 get_absolute_max(expr
->left
, &lmax
);
1343 get_absolute_max(expr
->right
, &rmax
);
1345 if (sval_binop_overflows(lmax
, op
, rmax
))
1348 res
= sval_binop(lmax
, op
, rmax
);
1349 if (sval_cmp(res
, sval_type_max(type
)) > 0)