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 min
= sval_preop(rl_max(rl
), '-');
127 max
= sval_preop(rl_min(rl
), '-');
128 return alloc_rl(min
, max
);
131 static struct range_list
*handle_preop_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
135 return handle_ampersand_rl(expr
, implied
, recurse_cnt
);
137 return handle_negate_rl(expr
, implied
, recurse_cnt
);
139 return handle_bitwise_negate(expr
, implied
, recurse_cnt
);
141 return handle_minus_preop(expr
, implied
, recurse_cnt
);
143 return handle_variable(expr
, implied
, recurse_cnt
);
145 return handle_expression_statement_rl(expr
, implied
, recurse_cnt
);
151 static struct range_list
*handle_divide_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
153 struct range_list
*left_rl
, *right_rl
;
156 type
= get_type(expr
);
158 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
159 left_rl
= cast_rl(type
, left_rl
);
160 right_rl
= _get_rl(expr
->right
, implied
, recurse_cnt
);
161 right_rl
= cast_rl(type
, right_rl
);
163 if (!left_rl
|| !right_rl
)
166 if (implied
!= RL_REAL_ABSOLUTE
) {
167 if (is_whole_rl(left_rl
) || is_whole_rl(right_rl
))
171 return rl_binop(left_rl
, '/', right_rl
);
174 static int handle_offset_subtraction(struct expression
*expr
)
176 struct expression
*left
, *right
;
177 struct symbol
*left_sym
, *right_sym
;
179 int left_offset
, right_offset
;
181 type
= get_type(expr
);
184 if (!type
|| type
->type
!= SYM_PTR
)
186 type
= get_real_base_type(type
);
187 if (!type
|| (type_bits(type
) != 8 && (type
!= &void_ctype
)))
190 left
= strip_expr(expr
->left
);
191 right
= strip_expr(expr
->right
);
193 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '&')
195 left
= strip_expr(left
->unop
);
197 left_sym
= expr_to_sym(left
);
198 right_sym
= expr_to_sym(right
);
199 if (!left_sym
|| left_sym
!= right_sym
)
202 left_offset
= get_member_offset_from_deref(left
);
203 if (right
->type
== EXPR_SYMBOL
)
206 right_offset
= get_member_offset_from_deref(right
);
207 if (left_offset
< 0 || right_offset
< 0)
210 return left_offset
- right_offset
;
213 static struct range_list
*handle_subtract_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
216 struct range_list
*left_rl
, *right_rl
;
217 sval_t max
, min
, tmp
;
221 type
= get_type(expr
);
223 offset
= handle_offset_subtraction(expr
);
228 return alloc_rl(tmp
, tmp
);
231 comparison
= get_comparison(expr
->left
, expr
->right
);
233 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
234 left_rl
= cast_rl(type
, left_rl
);
235 right_rl
= _get_rl(expr
->right
, implied
, recurse_cnt
);
236 right_rl
= cast_rl(type
, right_rl
);
238 if ((!left_rl
|| !right_rl
) &&
239 (implied
== RL_EXACT
|| implied
== RL_HARD
|| implied
== RL_FUZZY
))
243 left_rl
= alloc_whole_rl(type
);
245 right_rl
= alloc_whole_rl(type
);
247 /* negative values complicate everything fix this later */
248 if (sval_is_negative(rl_min(right_rl
)))
250 max
= rl_max(left_rl
);
252 switch (comparison
) {
254 case SPECIAL_UNSIGNED_GT
:
255 min
= sval_type_val(type
, 1);
256 max
= rl_max(left_rl
);
259 case SPECIAL_UNSIGNED_GTE
:
260 min
= sval_type_val(type
, 0);
261 max
= rl_max(left_rl
);
264 min
= sval_type_val(type
, 0);
265 max
= sval_type_val(type
, 0);
268 case SPECIAL_UNSIGNED_LT
:
269 max
= sval_type_val(type
, -1);
272 case SPECIAL_UNSIGNED_LTE
:
273 max
= sval_type_val(type
, 0);
276 if (sval_binop_overflows(rl_min(left_rl
), '-', rl_max(right_rl
)))
278 min
= sval_type_min(type
);
281 if (!sval_binop_overflows(rl_min(left_rl
), '-', rl_max(right_rl
))) {
282 tmp
= sval_binop(rl_min(left_rl
), '-', rl_max(right_rl
));
283 if (sval_cmp(tmp
, min
) > 0)
287 if (!sval_is_max(rl_max(left_rl
))) {
288 tmp
= sval_binop(rl_max(left_rl
), '-', rl_min(right_rl
));
289 if (sval_cmp(tmp
, max
) < 0)
293 if (sval_is_min(min
) && sval_is_max(max
))
296 return cast_rl(type
, alloc_rl(min
, max
));
299 static struct range_list
*handle_mod_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
301 struct range_list
*rl
;
302 sval_t left
, right
, sval
;
304 if (implied
== RL_EXACT
) {
305 if (!get_implied_value(expr
->right
, &right
))
307 if (!get_implied_value(expr
->left
, &left
))
309 sval
= sval_binop(left
, '%', right
);
310 return alloc_rl(sval
, sval
);
312 /* if we can't figure out the right side it's probably hopeless */
313 if (!get_implied_value_internal(expr
->right
, &right
, recurse_cnt
))
316 right
= sval_cast(get_type(expr
), right
);
319 rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
320 if (rl
&& rl_max(rl
).uvalue
< right
.uvalue
)
321 right
.uvalue
= rl_max(rl
).uvalue
;
323 return alloc_rl(sval_cast(right
.type
, zero
), right
);
326 static sval_t
sval_lowest_set_bit(sval_t sval
)
331 for (i
= 0; i
< 64; i
++) {
332 if (sval
.uvalue
& 1ULL << i
) {
335 sval
.uvalue
&= ~(1ULL << i
);
341 static struct range_list
*handle_bitwise_AND(struct expression
*expr
, int implied
, int *recurse_cnt
)
344 struct range_list
*left_rl
, *right_rl
;
348 if (implied
!= RL_IMPLIED
&& implied
!= RL_ABSOLUTE
&& implied
!= RL_REAL_ABSOLUTE
)
351 type
= get_type(expr
);
353 if (get_implied_value_internal(expr
->left
, &known
, recurse_cnt
)) {
356 min
= sval_lowest_set_bit(known
);
357 left_rl
= alloc_rl(min
, known
);
358 left_rl
= cast_rl(type
, left_rl
);
359 add_range(&left_rl
, sval_type_val(type
, 0), sval_type_val(type
, 0));
361 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
363 left_rl
= cast_rl(type
, left_rl
);
364 left_rl
= alloc_rl(sval_type_val(type
, 0), rl_max(left_rl
));
366 if (implied
== RL_HARD
)
368 left_rl
= alloc_whole_rl(type
);
372 if (*recurse_cnt
>= 200)
373 new_recurse
= 100; /* Let's try super hard to get the mask */
374 if (get_implied_value_internal(expr
->right
, &known
, &new_recurse
)) {
375 sval_t min
, left_max
, mod
;
377 *recurse_cnt
= new_recurse
;
379 min
= sval_lowest_set_bit(known
);
380 right_rl
= alloc_rl(min
, known
);
381 right_rl
= cast_rl(type
, right_rl
);
382 add_range(&right_rl
, sval_type_val(type
, 0), sval_type_val(type
, 0));
384 if (min
.value
!= 0) {
385 left_max
= rl_max(left_rl
);
386 mod
= sval_binop(left_max
, '%', min
);
388 left_max
= sval_binop(left_max
, '-', mod
);
390 if (left_max
.value
> 0 && sval_cmp(left_max
, rl_max(left_rl
)) < 0)
391 left_rl
= remove_range(left_rl
, left_max
, rl_max(left_rl
));
395 right_rl
= _get_rl(expr
->right
, implied
, recurse_cnt
);
397 right_rl
= cast_rl(type
, right_rl
);
398 right_rl
= alloc_rl(sval_type_val(type
, 0), rl_max(right_rl
));
400 if (implied
== RL_HARD
)
402 right_rl
= alloc_whole_rl(type
);
406 return rl_intersection(left_rl
, right_rl
);
409 static struct range_list
*use_rl_binop(struct expression
*expr
, int implied
, int *recurse_cnt
)
412 struct range_list
*left_rl
, *right_rl
;
414 if (implied
!= RL_IMPLIED
&& implied
!= RL_ABSOLUTE
&& implied
!= RL_REAL_ABSOLUTE
)
417 type
= get_type(expr
);
419 get_absolute_rl_internal(expr
->left
, &left_rl
, recurse_cnt
);
420 get_absolute_rl_internal(expr
->right
, &right_rl
, recurse_cnt
);
421 left_rl
= cast_rl(type
, left_rl
);
422 right_rl
= cast_rl(type
, right_rl
);
423 if (!left_rl
|| !right_rl
)
426 return rl_binop(left_rl
, expr
->op
, right_rl
);
429 static struct range_list
*handle_right_shift(struct expression
*expr
, int implied
, int *recurse_cnt
)
431 struct range_list
*left_rl
;
435 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
438 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
440 max
= rl_max(left_rl
);
441 min
= rl_min(left_rl
);
443 if (implied
== RL_FUZZY
)
445 max
= sval_type_max(get_type(expr
->left
));
446 min
= sval_type_val(get_type(expr
->left
), 0);
449 if (get_implied_value_internal(expr
->right
, &right
, recurse_cnt
)) {
450 min
= sval_binop(min
, SPECIAL_RIGHTSHIFT
, right
);
451 max
= sval_binop(max
, SPECIAL_RIGHTSHIFT
, right
);
452 } else if (!sval_is_negative(min
)) {
454 max
= sval_type_max(max
.type
);
459 return alloc_rl(min
, max
);
462 static struct range_list
*handle_left_shift(struct expression
*expr
, int implied
, int *recurse_cnt
)
464 struct range_list
*left_rl
, *res
;
469 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
471 /* this is hopeless without the right side */
472 if (!get_implied_value_internal(expr
->right
, &right
, recurse_cnt
))
474 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
476 max
= rl_max(left_rl
);
477 min
= rl_min(left_rl
);
478 if (min
.value
== 0) {
483 if (implied
== RL_FUZZY
)
485 max
= sval_type_max(get_type(expr
->left
));
486 min
= sval_type_val(get_type(expr
->left
), 1);
490 max
= sval_binop(max
, SPECIAL_LEFTSHIFT
, right
);
491 min
= sval_binop(min
, SPECIAL_LEFTSHIFT
, right
);
492 res
= alloc_rl(min
, max
);
494 res
= rl_union(res
, rl_zero());
498 static struct range_list
*handle_known_binop(struct expression
*expr
)
502 if (!get_value(expr
->left
, &left
))
504 if (!get_value(expr
->right
, &right
))
506 left
= sval_binop(left
, expr
->op
, right
);
507 return alloc_rl(left
, left
);
510 static int has_actual_ranges(struct range_list
*rl
)
512 struct data_range
*tmp
;
514 FOR_EACH_PTR(rl
, tmp
) {
515 if (sval_cmp(tmp
->min
, tmp
->max
) != 0)
517 } END_FOR_EACH_PTR(tmp
);
521 static struct range_list
*handle_implied_binop(struct range_list
*left_rl
, int op
, struct range_list
*right_rl
)
523 struct range_list
*res_rl
;
524 struct data_range
*left_drange
, *right_drange
;
527 if (!left_rl
|| !right_rl
)
529 if (has_actual_ranges(left_rl
))
531 if (has_actual_ranges(right_rl
))
534 if (ptr_list_size((struct ptr_list
*)left_rl
) * ptr_list_size((struct ptr_list
*)right_rl
) > 20)
539 FOR_EACH_PTR(left_rl
, left_drange
) {
540 FOR_EACH_PTR(right_rl
, right_drange
) {
541 if ((op
== '%' || op
== '/') &&
542 right_drange
->min
.value
== 0)
544 res
= sval_binop(left_drange
->min
, op
, right_drange
->min
);
545 add_range(&res_rl
, res
, res
);
546 } END_FOR_EACH_PTR(right_drange
);
547 } END_FOR_EACH_PTR(left_drange
);
552 static struct range_list
*handle_binop_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
554 struct smatch_state
*state
;
556 struct range_list
*left_rl
, *right_rl
, *rl
;
559 rl
= handle_known_binop(expr
);
562 if (implied
== RL_EXACT
)
565 if (custom_handle_variable
) {
566 rl
= custom_handle_variable(expr
);
571 state
= get_extra_state(expr
);
572 if (state
&& !is_whole_rl(estate_rl(state
)))
573 return clone_rl(estate_rl(state
));
575 type
= get_type(expr
);
576 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
577 left_rl
= cast_rl(type
, left_rl
);
578 right_rl
= _get_rl(expr
->right
, implied
, recurse_cnt
);
579 right_rl
= cast_rl(type
, right_rl
);
581 if (!left_rl
&& !right_rl
)
584 rl
= handle_implied_binop(left_rl
, expr
->op
, right_rl
);
590 return handle_mod_rl(expr
, implied
, recurse_cnt
);
592 return handle_bitwise_AND(expr
, implied
, recurse_cnt
);
595 return use_rl_binop(expr
, implied
, recurse_cnt
);
596 case SPECIAL_RIGHTSHIFT
:
597 return handle_right_shift(expr
, implied
, recurse_cnt
);
598 case SPECIAL_LEFTSHIFT
:
599 return handle_left_shift(expr
, implied
, recurse_cnt
);
601 return handle_subtract_rl(expr
, implied
, recurse_cnt
);
603 return handle_divide_rl(expr
, implied
, recurse_cnt
);
606 if (!left_rl
|| !right_rl
)
609 if (sval_binop_overflows(rl_min(left_rl
), expr
->op
, rl_min(right_rl
)))
611 if (sval_binop_overflows(rl_max(left_rl
), expr
->op
, rl_max(right_rl
)))
614 min
= sval_binop(rl_min(left_rl
), expr
->op
, rl_min(right_rl
));
615 max
= sval_binop(rl_max(left_rl
), expr
->op
, rl_max(right_rl
));
617 return alloc_rl(min
, max
);
620 static int do_comparison(struct expression
*expr
)
622 struct range_list
*left_ranges
= NULL
;
623 struct range_list
*right_ranges
= NULL
;
624 int poss_true
, poss_false
;
627 type
= get_type(expr
);
628 get_absolute_rl(expr
->left
, &left_ranges
);
629 get_absolute_rl(expr
->right
, &right_ranges
);
631 left_ranges
= cast_rl(type
, left_ranges
);
632 right_ranges
= cast_rl(type
, right_ranges
);
634 poss_true
= possibly_true_rl(left_ranges
, expr
->op
, right_ranges
);
635 poss_false
= possibly_false_rl(left_ranges
, expr
->op
, right_ranges
);
637 free_rl(&left_ranges
);
638 free_rl(&right_ranges
);
640 if (!poss_true
&& !poss_false
)
642 if (poss_true
&& !poss_false
)
644 if (!poss_true
&& poss_false
)
649 static struct range_list
*handle_comparison_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
654 if (expr
->op
== SPECIAL_EQUAL
&& expr
->left
->type
== EXPR_TYPE
) {
655 struct symbol
*left
, *right
;
657 left
= get_real_base_type(expr
->left
->symbol
);
658 right
= get_real_base_type(expr
->left
->symbol
);
664 if (get_value(expr
->left
, &left
) && get_value(expr
->right
, &right
)) {
665 struct data_range tmp_left
, tmp_right
;
669 tmp_right
.min
= right
;
670 tmp_right
.max
= right
;
671 if (true_comparison_range(&tmp_left
, expr
->op
, &tmp_right
))
676 if (implied
== RL_EXACT
)
679 res
= do_comparison(expr
);
685 return alloc_rl(zero
, one
);
688 static struct range_list
*handle_logical_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
694 if (implied
== RL_EXACT
) {
695 if (get_value(expr
->left
, &left
))
697 if (get_value(expr
->right
, &right
))
700 if (get_implied_value_internal(expr
->left
, &left
, recurse_cnt
))
702 if (get_implied_value_internal(expr
->right
, &right
, recurse_cnt
))
707 case SPECIAL_LOGICAL_OR
:
708 if (left_known
&& left
.value
)
710 if (right_known
&& right
.value
)
712 if (left_known
&& right_known
)
715 case SPECIAL_LOGICAL_AND
:
716 if (left_known
&& right_known
) {
717 if (left
.value
&& right
.value
)
726 if (implied
== RL_EXACT
)
729 return alloc_rl(zero
, one
);
732 static struct range_list
*handle_conditional_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
734 struct range_list
*true_rl
, *false_rl
;
736 int final_pass_orig
= final_pass
;
738 if (known_condition_true(expr
->conditional
))
739 return _get_rl(expr
->cond_true
, implied
, recurse_cnt
);
740 if (known_condition_false(expr
->conditional
))
741 return _get_rl(expr
->cond_false
, implied
, recurse_cnt
);
743 if (implied
== RL_EXACT
)
746 if (implied_condition_true(expr
->conditional
))
747 return _get_rl(expr
->cond_true
, implied
, recurse_cnt
);
748 if (implied_condition_false(expr
->conditional
))
749 return _get_rl(expr
->cond_false
, implied
, recurse_cnt
);
752 /* this becomes a problem with deeply nested conditional statements */
756 type
= get_type(expr
);
758 __push_fake_cur_stree();
760 __split_whole_condition(expr
->conditional
);
761 true_rl
= _get_rl(expr
->cond_true
, implied
, recurse_cnt
);
762 __push_true_states();
763 __use_false_states();
764 false_rl
= _get_rl(expr
->cond_false
, implied
, recurse_cnt
);
765 __merge_true_states();
766 __free_fake_cur_stree();
767 final_pass
= final_pass_orig
;
769 if (!true_rl
|| !false_rl
)
771 true_rl
= cast_rl(type
, true_rl
);
772 false_rl
= cast_rl(type
, false_rl
);
774 return rl_union(true_rl
, false_rl
);
777 static int get_fuzzy_max_helper(struct expression
*expr
, sval_t
*max
)
779 struct smatch_state
*state
;
782 if (get_hard_max(expr
, &sval
)) {
787 state
= get_extra_state(expr
);
788 if (!state
|| !estate_has_fuzzy_max(state
))
790 *max
= sval_cast(get_type(expr
), estate_get_fuzzy_max(state
));
794 static int get_fuzzy_min_helper(struct expression
*expr
, sval_t
*min
)
796 struct smatch_state
*state
;
799 state
= get_extra_state(expr
);
800 if (!state
|| !estate_rl(state
))
803 sval
= estate_min(state
);
804 if (sval_is_negative(sval
) && sval_is_min(sval
))
807 if (sval_is_max(sval
))
810 *min
= sval_cast(get_type(expr
), sval
);
814 int get_const_value(struct expression
*expr
, sval_t
*sval
)
819 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
822 if (!(sym
->ctype
.modifiers
& MOD_CONST
))
824 if (get_value(sym
->initializer
, &right
)) {
825 *sval
= sval_cast(get_type(expr
), right
);
831 struct range_list
*var_to_absolute_rl(struct expression
*expr
)
833 struct smatch_state
*state
;
834 struct range_list
*rl
;
836 state
= get_extra_state(expr
);
837 if (!state
|| is_whole_rl(estate_rl(state
))) {
838 state
= get_real_absolute_state(expr
);
839 if (state
&& state
->data
&& !estate_is_whole(state
))
840 return clone_rl(estate_rl(state
));
841 if (get_local_rl(expr
, &rl
) && !is_whole_rl(rl
))
843 if (get_db_type_rl(expr
, &rl
) && !is_whole_rl(rl
))
845 return alloc_whole_rl(get_type(expr
));
847 /* err on the side of saying things are possible */
848 if (!estate_rl(state
))
849 return alloc_whole_rl(get_type(expr
));
850 return clone_rl(estate_rl(state
));
853 static struct range_list
*handle_variable(struct expression
*expr
, int implied
, int *recurse_cnt
)
855 struct smatch_state
*state
;
856 struct range_list
*rl
;
857 sval_t sval
, min
, max
;
859 if (get_const_value(expr
, &sval
))
860 return alloc_rl(sval
, sval
);
862 if (custom_handle_variable
) {
863 rl
= custom_handle_variable(expr
);
865 return var_to_absolute_rl(expr
);
875 state
= get_extra_state(expr
);
876 if (!state
|| !state
->data
) {
877 if (implied
== RL_HARD
)
879 if (get_local_rl(expr
, &rl
))
881 if (get_db_type_rl(expr
, &rl
))
885 if (implied
== RL_HARD
&& !estate_has_hard_max(state
))
887 return clone_rl(estate_rl(state
));
888 case RL_REAL_ABSOLUTE
: {
889 struct smatch_state
*abs_state
;
891 state
= get_extra_state(expr
);
892 abs_state
= get_real_absolute_state(expr
);
894 if (estate_rl(state
) && estate_rl(abs_state
)) {
895 return clone_rl(rl_intersection(estate_rl(state
),
896 estate_rl(abs_state
)));
897 } else if (estate_rl(state
)) {
898 return clone_rl(estate_rl(state
));
899 } else if (state
&& estate_is_empty(state
)) {
901 * FIXME: we don't handle empty extra states correctly.
903 * The real abs rl is supposed to be filtered by the
904 * extra state if there is one. We don't bother keeping
905 * the abs state in sync all the time because we know it
906 * will be filtered later.
908 * It's not totally obvious to me how they should be
909 * handled. Perhaps we should take the whole rl and
910 * filter by the imaginary states. Perhaps we should
911 * just go with the empty state.
913 * Anyway what we currently do is return NULL here and
914 * that gets translated into the whole range in
915 * get_real_absolute_rl().
919 } else if (estate_rl(abs_state
)) {
920 return clone_rl(estate_rl(abs_state
));
923 if (get_local_rl(expr
, &rl
))
925 if (get_db_type_rl(expr
, &rl
))
930 if (!get_fuzzy_min_helper(expr
, &min
))
931 min
= sval_type_min(get_type(expr
));
932 if (!get_fuzzy_max_helper(expr
, &max
))
934 /* fuzzy ranges are often inverted */
935 if (sval_cmp(min
, max
) > 0) {
940 return alloc_rl(min
, max
);
945 static sval_t
handle_sizeof(struct expression
*expr
)
950 ret
= sval_blank(expr
);
951 sym
= expr
->cast_type
;
953 sym
= evaluate_expression(expr
->cast_expression
);
958 * Expressions of restricted types will possibly get
959 * promoted - check that here. I'm not sure how this works,
960 * the problem is that sizeof(le16) shouldn't be promoted and
961 * the original code did that... Let's if zero this out and
965 if (is_restricted_type(sym
)) {
966 if (type_bits(sym
) < bits_in_int
)
970 if (is_fouled_type(sym
))
973 examine_symbol_type(sym
);
975 ret
.type
= size_t_ctype
;
976 if (type_bits(sym
) <= 0) /* sizeof(void) */ {
977 if (get_real_base_type(sym
) == &void_ctype
)
982 ret
.value
= type_bytes(sym
);
987 static struct range_list
*handle_strlen(struct expression
*expr
, int implied
, int *recurse_cnt
)
989 struct range_list
*rl
;
990 struct expression
*arg
;
991 sval_t sval
= { .type
= &ulong_ctype
};
993 if (implied
== RL_EXACT
)
996 arg
= get_argument_from_call_expr(expr
->args
, 0);
999 if (arg
->type
== EXPR_STRING
) {
1000 sval
.value
= arg
->string
->length
- 1;
1001 return alloc_rl(sval
, sval
);
1004 if (implied
== RL_HARD
|| implied
== RL_FUZZY
)
1007 if (get_implied_return(expr
, &rl
))
1013 static struct range_list
*handle_call_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
1015 struct range_list
*rl
;
1017 if (sym_name_is("__builtin_expect", expr
->fn
) ||
1018 sym_name_is("__builtin_bswap16", expr
->fn
) ||
1019 sym_name_is("__builtin_bswap32", expr
->fn
) ||
1020 sym_name_is("__builtin_bswap64", expr
->fn
)) {
1021 struct expression
*arg
;
1023 arg
= get_argument_from_call_expr(expr
->args
, 0);
1024 return _get_rl(arg
, implied
, recurse_cnt
);
1027 if (sym_name_is("strlen", expr
->fn
))
1028 return handle_strlen(expr
, implied
, recurse_cnt
);
1030 if (implied
== RL_EXACT
|| implied
== RL_HARD
|| implied
== RL_FUZZY
)
1033 if (custom_handle_variable
) {
1034 rl
= custom_handle_variable(expr
);
1039 if (get_implied_return(expr
, &rl
))
1041 return db_return_vals(expr
);
1044 static struct range_list
*handle_cast(struct expression
*expr
, int implied
, int *recurse_cnt
)
1046 struct range_list
*rl
;
1047 struct symbol
*type
;
1049 type
= get_type(expr
);
1050 rl
= _get_rl(expr
->cast_expression
, implied
, recurse_cnt
);
1052 return cast_rl(type
, rl
);
1053 if (implied
== RL_ABSOLUTE
|| implied
== RL_REAL_ABSOLUTE
)
1054 return alloc_whole_rl(type
);
1055 if (implied
== RL_IMPLIED
&& type
&&
1056 type_bits(type
) > 0 && type_bits(type
) < 32)
1057 return alloc_whole_rl(type
);
1061 static struct range_list
*_get_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
1063 struct range_list
*rl
;
1064 struct symbol
*type
;
1067 type
= get_type(expr
);
1068 expr
= strip_parens(expr
);
1072 if (++(*recurse_cnt
) >= 200)
1075 switch(expr
->type
) {
1077 case EXPR_FORCE_CAST
:
1078 case EXPR_IMPLIED_CAST
:
1079 rl
= handle_cast(expr
, implied
, recurse_cnt
);
1083 expr
= strip_expr(expr
);
1087 switch (expr
->type
) {
1089 sval
= sval_from_val(expr
, expr
->value
);
1090 rl
= alloc_rl(sval
, sval
);
1093 rl
= handle_preop_rl(expr
, implied
, recurse_cnt
);
1096 rl
= _get_rl(expr
->unop
, implied
, recurse_cnt
);
1099 rl
= handle_binop_rl(expr
, implied
, recurse_cnt
);
1102 rl
= handle_comparison_rl(expr
, implied
, recurse_cnt
);
1105 rl
= handle_logical_rl(expr
, implied
, recurse_cnt
);
1107 case EXPR_PTRSIZEOF
:
1109 sval
= handle_sizeof(expr
);
1110 rl
= alloc_rl(sval
, sval
);
1113 case EXPR_CONDITIONAL
:
1114 rl
= handle_conditional_rl(expr
, implied
, recurse_cnt
);
1117 rl
= handle_call_rl(expr
, implied
, recurse_cnt
);
1120 rl
= handle_variable(expr
, implied
, recurse_cnt
);
1126 if (type
&& (implied
== RL_ABSOLUTE
|| implied
== RL_REAL_ABSOLUTE
))
1127 return alloc_whole_rl(type
);
1131 /* returns 1 if it can get a value literal or else returns 0 */
1132 int get_value(struct expression
*expr
, sval_t
*sval
)
1134 struct range_list
*rl
;
1135 int recurse_cnt
= 0;
1137 rl
= _get_rl(expr
, RL_EXACT
, &recurse_cnt
);
1138 if (!rl_to_sval(rl
, sval
))
1143 static int get_implied_value_internal(struct expression
*expr
, sval_t
*sval
, int *recurse_cnt
)
1145 struct range_list
*rl
;
1147 rl
= _get_rl(expr
, RL_IMPLIED
, recurse_cnt
);
1148 if (!rl_to_sval(rl
, sval
))
1153 int get_implied_value(struct expression
*expr
, sval_t
*sval
)
1155 struct range_list
*rl
;
1156 int recurse_cnt
= 0;
1158 rl
= _get_rl(expr
, RL_IMPLIED
, &recurse_cnt
);
1159 if (!rl_to_sval(rl
, sval
))
1164 int get_implied_min(struct expression
*expr
, sval_t
*sval
)
1166 struct range_list
*rl
;
1167 int recurse_cnt
= 0;
1169 rl
= _get_rl(expr
, RL_IMPLIED
, &recurse_cnt
);
1176 int get_implied_max(struct expression
*expr
, sval_t
*sval
)
1178 struct range_list
*rl
;
1179 int recurse_cnt
= 0;
1181 rl
= _get_rl(expr
, RL_IMPLIED
, &recurse_cnt
);
1188 int get_implied_rl(struct expression
*expr
, struct range_list
**rl
)
1190 int recurse_cnt
= 0;
1192 *rl
= _get_rl(expr
, RL_IMPLIED
, &recurse_cnt
);
1198 static int get_absolute_rl_internal(struct expression
*expr
, struct range_list
**rl
, int *recurse_cnt
)
1200 *rl
= _get_rl(expr
, RL_ABSOLUTE
, recurse_cnt
);
1202 *rl
= alloc_whole_rl(get_type(expr
));
1206 int get_absolute_rl(struct expression
*expr
, struct range_list
**rl
)
1208 int recurse_cnt
= 0;
1210 *rl
= _get_rl(expr
, RL_ABSOLUTE
, &recurse_cnt
);
1212 *rl
= alloc_whole_rl(get_type(expr
));
1216 int get_real_absolute_rl(struct expression
*expr
, struct range_list
**rl
)
1218 int recurse_cnt
= 0;
1220 *rl
= _get_rl(expr
, RL_REAL_ABSOLUTE
, &recurse_cnt
);
1222 *rl
= alloc_whole_rl(get_type(expr
));
1226 int custom_get_absolute_rl(struct expression
*expr
,
1227 struct range_list
*(*fn
)(struct expression
*expr
),
1228 struct range_list
**rl
)
1230 int recurse_cnt
= 0;
1233 custom_handle_variable
= fn
;
1234 *rl
= _get_rl(expr
, RL_REAL_ABSOLUTE
, &recurse_cnt
);
1235 custom_handle_variable
= NULL
;
1239 int get_implied_rl_var_sym(const char *var
, struct symbol
*sym
, struct range_list
**rl
)
1241 struct smatch_state
*state
;
1243 state
= get_state(SMATCH_EXTRA
, var
, sym
);
1244 *rl
= estate_rl(state
);
1250 int get_hard_max(struct expression
*expr
, sval_t
*sval
)
1252 struct range_list
*rl
;
1253 int recurse_cnt
= 0;
1255 rl
= _get_rl(expr
, RL_HARD
, &recurse_cnt
);
1262 int get_fuzzy_min(struct expression
*expr
, sval_t
*sval
)
1264 struct range_list
*rl
;
1266 int recurse_cnt
= 0;
1268 rl
= _get_rl(expr
, RL_FUZZY
, &recurse_cnt
);
1272 if (sval_is_negative(tmp
) && sval_is_min(tmp
))
1278 int get_fuzzy_max(struct expression
*expr
, sval_t
*sval
)
1280 struct range_list
*rl
;
1282 int recurse_cnt
= 0;
1284 rl
= _get_rl(expr
, RL_FUZZY
, &recurse_cnt
);
1288 if (max
.uvalue
> INT_MAX
- 10000)
1294 int get_absolute_min(struct expression
*expr
, sval_t
*sval
)
1296 struct range_list
*rl
;
1297 struct symbol
*type
;
1298 int recurse_cnt
= 0;
1300 type
= get_type(expr
);
1302 type
= &llong_ctype
; // FIXME: this is wrong but places assume get type can't fail.
1303 rl
= _get_rl(expr
, RL_REAL_ABSOLUTE
, &recurse_cnt
);
1307 *sval
= sval_type_min(type
);
1309 if (sval_cmp(*sval
, sval_type_min(type
)) < 0)
1310 *sval
= sval_type_min(type
);
1314 int get_absolute_max(struct expression
*expr
, sval_t
*sval
)
1316 struct range_list
*rl
;
1317 struct symbol
*type
;
1318 int recurse_cnt
= 0;
1320 type
= get_type(expr
);
1322 type
= &llong_ctype
;
1323 rl
= _get_rl(expr
, RL_REAL_ABSOLUTE
, &recurse_cnt
);
1327 *sval
= sval_type_max(type
);
1329 if (sval_cmp(sval_type_max(type
), *sval
) < 0)
1330 *sval
= sval_type_max(type
);
1334 int known_condition_true(struct expression
*expr
)
1341 if (get_value(expr
, &tmp
) && tmp
.value
)
1347 int known_condition_false(struct expression
*expr
)
1355 if (expr
->type
== EXPR_CALL
) {
1356 if (sym_name_is("__builtin_constant_p", expr
->fn
))
1362 int implied_condition_true(struct expression
*expr
)
1369 if (known_condition_true(expr
))
1371 if (get_implied_value(expr
, &tmp
) && tmp
.value
)
1374 if (expr
->type
== EXPR_POSTOP
)
1375 return implied_condition_true(expr
->unop
);
1377 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_DECREMENT
)
1378 return implied_not_equal(expr
->unop
, 1);
1379 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_INCREMENT
)
1380 return implied_not_equal(expr
->unop
, -1);
1382 expr
= strip_expr(expr
);
1383 switch (expr
->type
) {
1385 if (do_comparison(expr
) == 1)
1389 if (expr
->op
== '!') {
1390 if (implied_condition_false(expr
->unop
))
1396 if (implied_not_equal(expr
, 0) == 1)
1403 int implied_condition_false(struct expression
*expr
)
1405 struct expression
*tmp
;
1411 if (known_condition_false(expr
))
1414 switch (expr
->type
) {
1416 if (do_comparison(expr
) == 2)
1419 if (expr
->op
== '!') {
1420 if (implied_condition_true(expr
->unop
))
1424 tmp
= strip_expr(expr
);
1426 return implied_condition_false(tmp
);
1429 if (get_implied_value(expr
, &sval
) && sval
.value
== 0)
1436 int can_integer_overflow(struct symbol
*type
, struct expression
*expr
)
1439 sval_t lmax
, rmax
, res
;
1444 expr
= strip_expr(expr
);
1446 if (expr
->type
== EXPR_ASSIGNMENT
) {
1448 case SPECIAL_MUL_ASSIGN
:
1451 case SPECIAL_ADD_ASSIGN
:
1454 case SPECIAL_SHL_ASSIGN
:
1455 op
= SPECIAL_LEFTSHIFT
;
1460 } else if (expr
->type
== EXPR_BINOP
) {
1461 if (expr
->op
!= '*' && expr
->op
!= '+' && expr
->op
!= SPECIAL_LEFTSHIFT
)
1468 get_absolute_max(expr
->left
, &lmax
);
1469 get_absolute_max(expr
->right
, &rmax
);
1471 if (sval_binop_overflows(lmax
, op
, rmax
))
1474 res
= sval_binop(lmax
, op
, rmax
);
1475 if (sval_cmp(res
, sval_type_max(type
)) > 0)