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 struct range_list
*handle_subtract_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
177 struct range_list
*left_rl
, *right_rl
;
178 sval_t max
, min
, tmp
;
181 type
= get_type(expr
);
182 comparison
= get_comparison(expr
->left
, expr
->right
);
184 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
185 left_rl
= cast_rl(type
, left_rl
);
186 right_rl
= _get_rl(expr
->right
, implied
, recurse_cnt
);
187 right_rl
= cast_rl(type
, right_rl
);
189 if ((!left_rl
|| !right_rl
) &&
190 (implied
== RL_EXACT
|| implied
== RL_HARD
|| implied
== RL_FUZZY
))
194 left_rl
= alloc_whole_rl(type
);
196 right_rl
= alloc_whole_rl(type
);
198 /* negative values complicate everything fix this later */
199 if (sval_is_negative(rl_min(right_rl
)))
201 max
= rl_max(left_rl
);
203 switch (comparison
) {
205 case SPECIAL_UNSIGNED_GT
:
206 min
= sval_type_val(type
, 1);
207 max
= rl_max(left_rl
);
210 case SPECIAL_UNSIGNED_GTE
:
211 min
= sval_type_val(type
, 0);
212 max
= rl_max(left_rl
);
215 if (sval_binop_overflows(rl_min(left_rl
), '-', rl_max(right_rl
)))
217 min
= sval_type_min(type
);
220 if (!sval_binop_overflows(rl_min(left_rl
), '-', rl_max(right_rl
))) {
221 tmp
= sval_binop(rl_min(left_rl
), '-', rl_max(right_rl
));
222 if (sval_cmp(tmp
, min
) > 0)
226 if (!sval_is_max(rl_max(left_rl
))) {
227 tmp
= sval_binop(rl_max(left_rl
), '-', rl_min(right_rl
));
228 if (sval_cmp(tmp
, max
) < 0)
232 if (sval_is_min(min
) && sval_is_max(max
))
235 return cast_rl(type
, alloc_rl(min
, max
));
238 static struct range_list
*handle_mod_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
240 struct range_list
*rl
;
241 sval_t left
, right
, sval
;
243 if (implied
== RL_EXACT
) {
244 if (!get_value(expr
->right
, &right
))
246 if (!get_value(expr
->left
, &left
))
248 sval
= sval_binop(left
, '%', right
);
249 return alloc_rl(sval
, sval
);
251 /* if we can't figure out the right side it's probably hopeless */
252 if (!get_implied_value_internal(expr
->right
, &right
, recurse_cnt
))
255 right
= sval_cast(get_type(expr
), right
);
258 rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
259 if (rl
&& rl_max(rl
).uvalue
< right
.uvalue
)
260 right
.uvalue
= rl_max(rl
).uvalue
;
262 return alloc_rl(sval_cast(right
.type
, zero
), right
);
265 static sval_t
sval_lowest_set_bit(sval_t sval
)
270 for (i
= 0; i
< 64; i
++) {
271 if (sval
.uvalue
& 1ULL << i
) {
274 sval
.uvalue
&= ~(1ULL << i
);
280 static struct range_list
*handle_bitwise_AND(struct expression
*expr
, int implied
, int *recurse_cnt
)
283 struct range_list
*left_rl
, *right_rl
;
286 if (implied
!= RL_IMPLIED
&& implied
!= RL_ABSOLUTE
&& implied
!= RL_REAL_ABSOLUTE
)
289 type
= get_type(expr
);
291 if (get_implied_value_internal(expr
->left
, &known
, recurse_cnt
)) {
294 min
= sval_lowest_set_bit(known
);
295 left_rl
= alloc_rl(min
, known
);
296 left_rl
= cast_rl(type
, left_rl
);
297 add_range(&left_rl
, sval_type_val(type
, 0), sval_type_val(type
, 0));
299 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
301 left_rl
= cast_rl(type
, left_rl
);
302 left_rl
= alloc_rl(sval_type_val(type
, 0), rl_max(left_rl
));
304 if (implied
== RL_HARD
)
306 left_rl
= alloc_whole_rl(type
);
310 if (get_implied_value_internal(expr
->right
, &known
, recurse_cnt
)) {
311 sval_t min
, left_max
, mod
;
313 min
= sval_lowest_set_bit(known
);
314 right_rl
= alloc_rl(min
, known
);
315 right_rl
= cast_rl(type
, right_rl
);
316 add_range(&right_rl
, sval_type_val(type
, 0), sval_type_val(type
, 0));
318 if (min
.value
!= 0) {
319 left_max
= rl_max(left_rl
);
320 mod
= sval_binop(left_max
, '%', min
);
322 left_max
= sval_binop(left_max
, '-', mod
);
324 if (left_max
.value
> 0 && sval_cmp(left_max
, rl_max(left_rl
)) < 0)
325 left_rl
= remove_range(left_rl
, left_max
, rl_max(left_rl
));
329 right_rl
= _get_rl(expr
->right
, implied
, recurse_cnt
);
331 right_rl
= cast_rl(type
, right_rl
);
332 right_rl
= alloc_rl(sval_type_val(type
, 0), rl_max(right_rl
));
334 if (implied
== RL_HARD
)
336 right_rl
= alloc_whole_rl(type
);
340 return rl_intersection(left_rl
, right_rl
);
343 static struct range_list
*use_rl_binop(struct expression
*expr
, int implied
, int *recurse_cnt
)
346 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 get_absolute_rl_internal(expr
->left
, &left_rl
, recurse_cnt
);
354 get_absolute_rl_internal(expr
->right
, &right_rl
, recurse_cnt
);
355 left_rl
= cast_rl(type
, left_rl
);
356 right_rl
= cast_rl(type
, right_rl
);
357 if (!left_rl
|| !right_rl
)
360 return rl_binop(left_rl
, expr
->op
, right_rl
);
363 static struct range_list
*handle_right_shift(struct expression
*expr
, int implied
, int *recurse_cnt
)
365 struct range_list
*left_rl
;
369 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
372 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
374 max
= rl_max(left_rl
);
375 min
= rl_min(left_rl
);
377 if (implied
== RL_FUZZY
)
379 max
= sval_type_max(get_type(expr
->left
));
380 min
= sval_type_val(get_type(expr
->left
), 0);
383 if (get_implied_value_internal(expr
->right
, &right
, recurse_cnt
)) {
384 min
= sval_binop(min
, SPECIAL_RIGHTSHIFT
, right
);
385 max
= sval_binop(max
, SPECIAL_RIGHTSHIFT
, right
);
386 } else if (!sval_is_negative(min
)) {
388 max
= sval_type_max(max
.type
);
393 return alloc_rl(min
, max
);
396 static struct range_list
*handle_left_shift(struct expression
*expr
, int implied
, int *recurse_cnt
)
398 struct range_list
*left_rl
, *res
;
403 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
405 /* this is hopeless without the right side */
406 if (!get_implied_value_internal(expr
->right
, &right
, recurse_cnt
))
408 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
410 max
= rl_max(left_rl
);
411 min
= rl_min(left_rl
);
412 if (min
.value
== 0) {
417 if (implied
== RL_FUZZY
)
419 max
= sval_type_max(get_type(expr
->left
));
420 min
= sval_type_val(get_type(expr
->left
), 1);
424 max
= sval_binop(max
, SPECIAL_LEFTSHIFT
, right
);
425 min
= sval_binop(min
, SPECIAL_LEFTSHIFT
, right
);
426 res
= alloc_rl(min
, max
);
428 res
= rl_union(res
, rl_zero());
432 static struct range_list
*handle_known_binop(struct expression
*expr
)
436 if (!get_value(expr
->left
, &left
))
438 if (!get_value(expr
->right
, &right
))
440 left
= sval_binop(left
, expr
->op
, right
);
441 return alloc_rl(left
, left
);
444 static int has_actual_ranges(struct range_list
*rl
)
446 struct data_range
*tmp
;
448 FOR_EACH_PTR(rl
, tmp
) {
449 if (sval_cmp(tmp
->min
, tmp
->max
) != 0)
451 } END_FOR_EACH_PTR(tmp
);
455 static struct range_list
*handle_implied_binop(struct range_list
*left_rl
, int op
, struct range_list
*right_rl
)
457 struct range_list
*res_rl
;
458 struct data_range
*left_drange
, *right_drange
;
461 if (!left_rl
|| !right_rl
)
463 if (has_actual_ranges(left_rl
))
465 if (has_actual_ranges(right_rl
))
468 if (ptr_list_size((struct ptr_list
*)left_rl
) * ptr_list_size((struct ptr_list
*)right_rl
) > 20)
473 FOR_EACH_PTR(left_rl
, left_drange
) {
474 FOR_EACH_PTR(right_rl
, right_drange
) {
475 if ((op
== '%' || op
== '/') &&
476 right_drange
->min
.value
== 0)
478 res
= sval_binop(left_drange
->min
, op
, right_drange
->min
);
479 add_range(&res_rl
, res
, res
);
480 } END_FOR_EACH_PTR(right_drange
);
481 } END_FOR_EACH_PTR(left_drange
);
486 static struct range_list
*handle_binop_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
488 struct smatch_state
*state
;
490 struct range_list
*left_rl
, *right_rl
, *rl
;
493 rl
= handle_known_binop(expr
);
496 if (implied
== RL_EXACT
)
499 state
= get_extra_state(expr
);
500 if (state
&& !is_whole_rl(estate_rl(state
)))
501 return clone_rl(estate_rl(state
));
503 type
= get_type(expr
);
504 left_rl
= _get_rl(expr
->left
, implied
, recurse_cnt
);
505 left_rl
= cast_rl(type
, left_rl
);
506 right_rl
= _get_rl(expr
->right
, implied
, recurse_cnt
);
507 right_rl
= cast_rl(type
, right_rl
);
509 if (!left_rl
&& !right_rl
)
512 rl
= handle_implied_binop(left_rl
, expr
->op
, right_rl
);
518 return handle_mod_rl(expr
, implied
, recurse_cnt
);
520 return handle_bitwise_AND(expr
, implied
, recurse_cnt
);
523 return use_rl_binop(expr
, implied
, recurse_cnt
);
524 case SPECIAL_RIGHTSHIFT
:
525 return handle_right_shift(expr
, implied
, recurse_cnt
);
526 case SPECIAL_LEFTSHIFT
:
527 return handle_left_shift(expr
, implied
, recurse_cnt
);
529 return handle_subtract_rl(expr
, implied
, recurse_cnt
);
531 return handle_divide_rl(expr
, implied
, recurse_cnt
);
534 if (!left_rl
|| !right_rl
)
537 if (sval_binop_overflows(rl_min(left_rl
), expr
->op
, rl_min(right_rl
)))
539 if (sval_binop_overflows(rl_max(left_rl
), expr
->op
, rl_max(right_rl
)))
542 min
= sval_binop(rl_min(left_rl
), expr
->op
, rl_min(right_rl
));
543 max
= sval_binop(rl_max(left_rl
), expr
->op
, rl_max(right_rl
));
545 return alloc_rl(min
, max
);
548 static int do_comparison(struct expression
*expr
)
550 struct range_list
*left_ranges
= NULL
;
551 struct range_list
*right_ranges
= NULL
;
552 int poss_true
, poss_false
;
555 type
= get_type(expr
);
556 get_absolute_rl(expr
->left
, &left_ranges
);
557 get_absolute_rl(expr
->right
, &right_ranges
);
559 left_ranges
= cast_rl(type
, left_ranges
);
560 right_ranges
= cast_rl(type
, right_ranges
);
562 poss_true
= possibly_true_rl(left_ranges
, expr
->op
, right_ranges
);
563 poss_false
= possibly_false_rl(left_ranges
, expr
->op
, right_ranges
);
565 free_rl(&left_ranges
);
566 free_rl(&right_ranges
);
568 if (!poss_true
&& !poss_false
)
570 if (poss_true
&& !poss_false
)
572 if (!poss_true
&& poss_false
)
577 static struct range_list
*handle_comparison_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
582 if (expr
->op
== SPECIAL_EQUAL
&& expr
->left
->type
== EXPR_TYPE
) {
583 struct symbol
*left
, *right
;
585 left
= get_real_base_type(expr
->left
->symbol
);
586 right
= get_real_base_type(expr
->left
->symbol
);
592 if (get_value(expr
->left
, &left
) && get_value(expr
->right
, &right
)) {
593 struct data_range tmp_left
, tmp_right
;
597 tmp_right
.min
= right
;
598 tmp_right
.max
= right
;
599 if (true_comparison_range(&tmp_left
, expr
->op
, &tmp_right
))
604 if (implied
== RL_EXACT
)
607 res
= do_comparison(expr
);
613 return alloc_rl(zero
, one
);
616 static struct range_list
*handle_logical_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
622 if (implied
== RL_EXACT
) {
623 if (get_value(expr
->left
, &left
))
625 if (get_value(expr
->right
, &right
))
628 if (get_implied_value_internal(expr
->left
, &left
, recurse_cnt
))
630 if (get_implied_value_internal(expr
->right
, &right
, recurse_cnt
))
635 case SPECIAL_LOGICAL_OR
:
636 if (left_known
&& left
.value
)
638 if (right_known
&& right
.value
)
640 if (left_known
&& right_known
)
643 case SPECIAL_LOGICAL_AND
:
644 if (left_known
&& right_known
) {
645 if (left
.value
&& right
.value
)
654 if (implied
== RL_EXACT
)
657 return alloc_rl(zero
, one
);
660 static struct range_list
*handle_conditional_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
662 struct range_list
*true_rl
, *false_rl
;
664 int final_pass_orig
= final_pass
;
666 if (known_condition_true(expr
->conditional
))
667 return _get_rl(expr
->cond_true
, implied
, recurse_cnt
);
668 if (known_condition_false(expr
->conditional
))
669 return _get_rl(expr
->cond_false
, implied
, recurse_cnt
);
671 if (implied
== RL_EXACT
)
674 if (implied_condition_true(expr
->conditional
))
675 return _get_rl(expr
->cond_true
, implied
, recurse_cnt
);
676 if (implied_condition_false(expr
->conditional
))
677 return _get_rl(expr
->cond_false
, implied
, recurse_cnt
);
680 /* this becomes a problem with deeply nested conditional statements */
684 type
= get_type(expr
);
686 __push_fake_cur_stree();
688 __split_whole_condition(expr
->conditional
);
689 true_rl
= _get_rl(expr
->cond_true
, implied
, recurse_cnt
);
690 __push_true_states();
691 __use_false_states();
692 false_rl
= _get_rl(expr
->cond_false
, implied
, recurse_cnt
);
693 __merge_true_states();
694 __free_fake_cur_stree();
695 final_pass
= final_pass_orig
;
697 if (!true_rl
|| !false_rl
)
699 true_rl
= cast_rl(type
, true_rl
);
700 false_rl
= cast_rl(type
, false_rl
);
702 return rl_union(true_rl
, false_rl
);
705 static int get_fuzzy_max_helper(struct expression
*expr
, sval_t
*max
)
707 struct smatch_state
*state
;
710 if (get_hard_max(expr
, &sval
)) {
715 state
= get_extra_state(expr
);
716 if (!state
|| !estate_has_fuzzy_max(state
))
718 *max
= sval_cast(get_type(expr
), estate_get_fuzzy_max(state
));
722 static int get_fuzzy_min_helper(struct expression
*expr
, sval_t
*min
)
724 struct smatch_state
*state
;
727 state
= get_extra_state(expr
);
728 if (!state
|| !estate_rl(state
))
731 sval
= estate_min(state
);
732 if (sval_is_negative(sval
) && sval_is_min(sval
))
735 if (sval_is_max(sval
))
738 *min
= sval_cast(get_type(expr
), sval
);
742 int get_const_value(struct expression
*expr
, sval_t
*sval
)
747 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
750 if (!(sym
->ctype
.modifiers
& MOD_CONST
))
752 if (get_value(sym
->initializer
, &right
)) {
753 *sval
= sval_cast(get_type(expr
), right
);
759 struct range_list
*var_to_absolute_rl(struct expression
*expr
)
761 struct smatch_state
*state
;
762 struct range_list
*rl
;
764 state
= get_extra_state(expr
);
765 if (!state
|| is_whole_rl(estate_rl(state
))) {
766 state
= get_real_absolute_state(expr
);
767 if (state
&& state
->data
&& !estate_is_whole(state
))
768 return clone_rl(estate_rl(state
));
769 if (get_local_rl(expr
, &rl
) && !is_whole_rl(rl
))
771 if (get_db_type_rl(expr
, &rl
) && !is_whole_rl(rl
))
773 return alloc_whole_rl(get_type(expr
));
775 /* err on the side of saying things are possible */
776 if (!estate_rl(state
))
777 return alloc_whole_rl(get_type(expr
));
778 return clone_rl(estate_rl(state
));
781 static struct range_list
*handle_variable(struct expression
*expr
, int implied
, int *recurse_cnt
)
783 struct smatch_state
*state
;
784 struct range_list
*rl
;
785 sval_t sval
, min
, max
;
787 if (get_const_value(expr
, &sval
))
788 return alloc_rl(sval
, sval
);
790 if (custom_handle_variable
) {
791 rl
= custom_handle_variable(expr
);
793 return var_to_absolute_rl(expr
);
803 state
= get_extra_state(expr
);
804 if (!state
|| !state
->data
) {
805 if (implied
== RL_HARD
)
807 if (get_local_rl(expr
, &rl
))
809 if (get_db_type_rl(expr
, &rl
))
813 if (implied
== RL_HARD
&& !estate_has_hard_max(state
))
815 return clone_rl(estate_rl(state
));
816 case RL_REAL_ABSOLUTE
: {
817 struct smatch_state
*abs_state
;
819 state
= get_extra_state(expr
);
820 abs_state
= get_real_absolute_state(expr
);
822 if (estate_rl(state
) && estate_rl(abs_state
)) {
823 return clone_rl(rl_intersection(estate_rl(state
),
824 estate_rl(abs_state
)));
825 } else if (estate_rl(state
)) {
826 return clone_rl(estate_rl(state
));
827 } else if (estate_rl(abs_state
)) {
828 return clone_rl(estate_rl(abs_state
));
831 if (get_local_rl(expr
, &rl
))
833 if (get_db_type_rl(expr
, &rl
))
838 if (!get_fuzzy_min_helper(expr
, &min
))
839 min
= sval_type_min(get_type(expr
));
840 if (!get_fuzzy_max_helper(expr
, &max
))
842 /* fuzzy ranges are often inverted */
843 if (sval_cmp(min
, max
) > 0) {
848 return alloc_rl(min
, max
);
853 static sval_t
handle_sizeof(struct expression
*expr
)
858 ret
= sval_blank(expr
);
859 sym
= expr
->cast_type
;
861 sym
= evaluate_expression(expr
->cast_expression
);
866 * Expressions of restricted types will possibly get
867 * promoted - check that here. I'm not sure how this works,
868 * the problem is that sizeof(le16) shouldn't be promoted and
869 * the original code did that... Let's if zero this out and
873 if (is_restricted_type(sym
)) {
874 if (type_bits(sym
) < bits_in_int
)
878 if (is_fouled_type(sym
))
881 examine_symbol_type(sym
);
883 ret
.type
= size_t_ctype
;
884 if (type_bits(sym
) <= 0) /* sizeof(void) */ {
885 if (get_real_base_type(sym
) == &void_ctype
)
890 ret
.value
= type_bytes(sym
);
895 static struct range_list
*handle_strlen(struct expression
*expr
, int implied
, int *recurse_cnt
)
897 struct range_list
*rl
;
898 struct expression
*arg
;
899 sval_t sval
= { .type
= &ulong_ctype
};
901 if (implied
== RL_EXACT
)
904 arg
= get_argument_from_call_expr(expr
->args
, 0);
907 if (arg
->type
== EXPR_STRING
) {
908 sval
.value
= arg
->string
->length
- 1;
909 return alloc_rl(sval
, sval
);
912 if (implied
== RL_HARD
|| implied
== RL_FUZZY
)
915 if (get_implied_return(expr
, &rl
))
921 static struct range_list
*handle_call_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
923 struct range_list
*rl
;
925 if (sym_name_is("__builtin_expect", expr
->fn
) ||
926 sym_name_is("__builtin_bswap16", expr
->fn
) ||
927 sym_name_is("__builtin_bswap32", expr
->fn
) ||
928 sym_name_is("__builtin_bswap64", expr
->fn
)) {
929 struct expression
*arg
;
931 arg
= get_argument_from_call_expr(expr
->args
, 0);
932 return _get_rl(arg
, implied
, recurse_cnt
);
935 if (sym_name_is("strlen", expr
->fn
))
936 return handle_strlen(expr
, implied
, recurse_cnt
);
938 if (implied
== RL_EXACT
|| implied
== RL_HARD
|| implied
== RL_FUZZY
)
941 if (custom_handle_variable
) {
942 rl
= custom_handle_variable(expr
);
947 if (get_implied_return(expr
, &rl
))
949 return db_return_vals(expr
);
952 static struct range_list
*handle_cast(struct expression
*expr
, int implied
, int *recurse_cnt
)
954 struct range_list
*rl
;
957 type
= get_type(expr
);
958 rl
= _get_rl(expr
->cast_expression
, implied
, recurse_cnt
);
960 return cast_rl(type
, rl
);
961 if (implied
== RL_ABSOLUTE
|| implied
== RL_REAL_ABSOLUTE
)
962 return alloc_whole_rl(type
);
963 if (implied
== RL_IMPLIED
&& type
&&
964 type_bits(type
) > 0 && type_bits(type
) < 32)
965 return alloc_whole_rl(type
);
969 static struct range_list
*_get_rl(struct expression
*expr
, int implied
, int *recurse_cnt
)
971 struct range_list
*rl
;
975 type
= get_type(expr
);
976 expr
= strip_parens(expr
);
980 if (++(*recurse_cnt
) >= 200)
985 case EXPR_FORCE_CAST
:
986 case EXPR_IMPLIED_CAST
:
987 rl
= handle_cast(expr
, implied
, recurse_cnt
);
991 expr
= strip_expr(expr
);
995 switch (expr
->type
) {
997 sval
= sval_from_val(expr
, expr
->value
);
998 rl
= alloc_rl(sval
, sval
);
1001 rl
= handle_preop_rl(expr
, implied
, recurse_cnt
);
1004 rl
= _get_rl(expr
->unop
, implied
, recurse_cnt
);
1007 rl
= handle_binop_rl(expr
, implied
, recurse_cnt
);
1010 rl
= handle_comparison_rl(expr
, implied
, recurse_cnt
);
1013 rl
= handle_logical_rl(expr
, implied
, recurse_cnt
);
1015 case EXPR_PTRSIZEOF
:
1017 sval
= handle_sizeof(expr
);
1018 rl
= alloc_rl(sval
, sval
);
1021 case EXPR_CONDITIONAL
:
1022 rl
= handle_conditional_rl(expr
, implied
, recurse_cnt
);
1025 rl
= handle_call_rl(expr
, implied
, recurse_cnt
);
1028 rl
= handle_variable(expr
, implied
, recurse_cnt
);
1034 if (type
&& (implied
== RL_ABSOLUTE
|| implied
== RL_REAL_ABSOLUTE
))
1035 return alloc_whole_rl(type
);
1039 /* returns 1 if it can get a value literal or else returns 0 */
1040 int get_value(struct expression
*expr
, sval_t
*sval
)
1042 struct range_list
*rl
;
1043 int recurse_cnt
= 0;
1045 rl
= _get_rl(expr
, RL_EXACT
, &recurse_cnt
);
1046 if (!rl_to_sval(rl
, sval
))
1051 static int get_implied_value_internal(struct expression
*expr
, sval_t
*sval
, int *recurse_cnt
)
1053 struct range_list
*rl
;
1055 rl
= _get_rl(expr
, RL_IMPLIED
, recurse_cnt
);
1056 if (!rl_to_sval(rl
, sval
))
1061 int get_implied_value(struct expression
*expr
, sval_t
*sval
)
1063 struct range_list
*rl
;
1064 int recurse_cnt
= 0;
1066 rl
= _get_rl(expr
, RL_IMPLIED
, &recurse_cnt
);
1067 if (!rl_to_sval(rl
, sval
))
1072 int get_implied_min(struct expression
*expr
, sval_t
*sval
)
1074 struct range_list
*rl
;
1075 int recurse_cnt
= 0;
1077 rl
= _get_rl(expr
, RL_IMPLIED
, &recurse_cnt
);
1084 int get_implied_max(struct expression
*expr
, sval_t
*sval
)
1086 struct range_list
*rl
;
1087 int recurse_cnt
= 0;
1089 rl
= _get_rl(expr
, RL_IMPLIED
, &recurse_cnt
);
1096 int get_implied_rl(struct expression
*expr
, struct range_list
**rl
)
1098 int recurse_cnt
= 0;
1100 *rl
= _get_rl(expr
, RL_IMPLIED
, &recurse_cnt
);
1106 static int get_absolute_rl_internal(struct expression
*expr
, struct range_list
**rl
, int *recurse_cnt
)
1108 *rl
= _get_rl(expr
, RL_ABSOLUTE
, recurse_cnt
);
1110 *rl
= alloc_whole_rl(get_type(expr
));
1114 int get_absolute_rl(struct expression
*expr
, struct range_list
**rl
)
1116 int recurse_cnt
= 0;
1118 *rl
= _get_rl(expr
, RL_ABSOLUTE
, &recurse_cnt
);
1120 *rl
= alloc_whole_rl(get_type(expr
));
1124 int get_real_absolute_rl(struct expression
*expr
, struct range_list
**rl
)
1126 int recurse_cnt
= 0;
1128 *rl
= _get_rl(expr
, RL_REAL_ABSOLUTE
, &recurse_cnt
);
1130 *rl
= alloc_whole_rl(get_type(expr
));
1134 int custom_get_absolute_rl(struct expression
*expr
,
1135 struct range_list
*(*fn
)(struct expression
*expr
),
1136 struct range_list
**rl
)
1138 int recurse_cnt
= 0;
1141 custom_handle_variable
= fn
;
1142 *rl
= _get_rl(expr
, RL_REAL_ABSOLUTE
, &recurse_cnt
);
1143 custom_handle_variable
= NULL
;
1147 int get_implied_rl_var_sym(const char *var
, struct symbol
*sym
, struct range_list
**rl
)
1149 struct smatch_state
*state
;
1151 state
= get_state(SMATCH_EXTRA
, var
, sym
);
1152 *rl
= estate_rl(state
);
1158 int get_hard_max(struct expression
*expr
, sval_t
*sval
)
1160 struct range_list
*rl
;
1161 int recurse_cnt
= 0;
1163 rl
= _get_rl(expr
, RL_HARD
, &recurse_cnt
);
1170 int get_fuzzy_min(struct expression
*expr
, sval_t
*sval
)
1172 struct range_list
*rl
;
1174 int recurse_cnt
= 0;
1176 rl
= _get_rl(expr
, RL_FUZZY
, &recurse_cnt
);
1180 if (sval_is_negative(tmp
) && sval_is_min(tmp
))
1186 int get_fuzzy_max(struct expression
*expr
, sval_t
*sval
)
1188 struct range_list
*rl
;
1190 int recurse_cnt
= 0;
1192 rl
= _get_rl(expr
, RL_FUZZY
, &recurse_cnt
);
1196 if (max
.uvalue
> INT_MAX
- 10000)
1202 int get_absolute_min(struct expression
*expr
, sval_t
*sval
)
1204 struct range_list
*rl
;
1205 struct symbol
*type
;
1206 int recurse_cnt
= 0;
1208 type
= get_type(expr
);
1210 type
= &llong_ctype
; // FIXME: this is wrong but places assume get type can't fail.
1211 rl
= _get_rl(expr
, RL_ABSOLUTE
, &recurse_cnt
);
1215 *sval
= sval_type_min(type
);
1217 if (sval_cmp(*sval
, sval_type_min(type
)) < 0)
1218 *sval
= sval_type_min(type
);
1222 int get_absolute_max(struct expression
*expr
, sval_t
*sval
)
1224 struct range_list
*rl
;
1225 struct symbol
*type
;
1226 int recurse_cnt
= 0;
1228 type
= get_type(expr
);
1230 type
= &llong_ctype
;
1231 rl
= _get_rl(expr
, RL_ABSOLUTE
, &recurse_cnt
);
1235 *sval
= sval_type_max(type
);
1237 if (sval_cmp(sval_type_max(type
), *sval
) < 0)
1238 *sval
= sval_type_max(type
);
1242 int known_condition_true(struct expression
*expr
)
1249 if (get_value(expr
, &tmp
) && tmp
.value
)
1255 int known_condition_false(struct expression
*expr
)
1263 if (expr
->type
== EXPR_CALL
) {
1264 if (sym_name_is("__builtin_constant_p", expr
->fn
))
1270 int implied_condition_true(struct expression
*expr
)
1277 if (known_condition_true(expr
))
1279 if (get_implied_value(expr
, &tmp
) && tmp
.value
)
1282 if (expr
->type
== EXPR_POSTOP
)
1283 return implied_condition_true(expr
->unop
);
1285 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_DECREMENT
)
1286 return implied_not_equal(expr
->unop
, 1);
1287 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_INCREMENT
)
1288 return implied_not_equal(expr
->unop
, -1);
1290 expr
= strip_expr(expr
);
1291 switch (expr
->type
) {
1293 if (do_comparison(expr
) == 1)
1297 if (expr
->op
== '!') {
1298 if (implied_condition_false(expr
->unop
))
1304 if (implied_not_equal(expr
, 0) == 1)
1311 int implied_condition_false(struct expression
*expr
)
1313 struct expression
*tmp
;
1319 if (known_condition_false(expr
))
1322 switch (expr
->type
) {
1324 if (do_comparison(expr
) == 2)
1327 if (expr
->op
== '!') {
1328 if (implied_condition_true(expr
->unop
))
1332 tmp
= strip_expr(expr
);
1334 return implied_condition_false(tmp
);
1337 if (get_implied_value(expr
, &sval
) && sval
.value
== 0)
1344 int can_integer_overflow(struct symbol
*type
, struct expression
*expr
)
1347 sval_t lmax
, rmax
, res
;
1352 expr
= strip_expr(expr
);
1354 if (expr
->type
== EXPR_ASSIGNMENT
) {
1356 case SPECIAL_MUL_ASSIGN
:
1359 case SPECIAL_ADD_ASSIGN
:
1362 case SPECIAL_SHL_ASSIGN
:
1363 op
= SPECIAL_LEFTSHIFT
;
1368 } else if (expr
->type
== EXPR_BINOP
) {
1369 if (expr
->op
!= '*' && expr
->op
!= '+' && expr
->op
!= SPECIAL_LEFTSHIFT
)
1376 get_absolute_max(expr
->left
, &lmax
);
1377 get_absolute_max(expr
->right
, &rmax
);
1379 if (sval_binop_overflows(lmax
, op
, rmax
))
1382 res
= sval_binop(lmax
, op
, rmax
);
1383 if (sval_cmp(res
, sval_type_max(type
)) > 0)