4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
12 #include "smatch_slist.h"
13 #include "smatch_extra.h"
15 static sval_t
_get_value(struct expression
*expr
, int *undefined
, int implied
);
16 static sval_t
_get_implied_value(struct expression
*expr
, int *undefined
, int implied
);
20 static sval_t zero
= {.type
= &int_ctype
, {.value
= 0} };
21 static sval_t one
= {.type
= &int_ctype
, {.value
= 1} };
22 static sval_t bogus
= {.type
= &int_ctype
, {.value
= BOGUS
} };
36 static int opposite_implied(int implied
)
38 if (implied
== IMPLIED_MIN
)
40 if (implied
== IMPLIED_MAX
)
42 if (implied
== FUZZY_MIN
)
44 if (implied
== FUZZY_MAX
)
46 if (implied
== ABSOLUTE_MIN
)
48 if (implied
== ABSOLUTE_MAX
)
50 if (implied
== HARD_MAX
) /* we don't have a hard min. */
56 static int last_stmt_sval(struct statement
*stmt
, sval_t
*sval
)
58 struct expression
*expr
;
63 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
64 if (stmt
->type
!= STMT_EXPRESSION
)
66 expr
= stmt
->expression
;
67 if (!get_value(expr
, sval
))
72 static sval_t
handle_expression_statement(struct expression
*expr
, int *undefined
, int implied
)
74 struct statement
*stmt
;
77 stmt
= get_expression_statement(expr
);
78 if (!last_stmt_sval(stmt
, &ret
)) {
86 static sval_t
handle_ampersand(int *undefined
, int implied
)
90 ret
.type
= &ptr_ctype
;
93 if (implied
== IMPLIED_MIN
|| implied
== FUZZY_MIN
|| implied
== ABSOLUTE_MIN
)
94 return valid_ptr_min_sval
;
95 if (implied
== IMPLIED_MAX
|| implied
== FUZZY_MAX
|| implied
== ABSOLUTE_MAX
)
96 return valid_ptr_max_sval
;
102 static sval_t
handle_negate(struct expression
*expr
, int *undefined
, int implied
)
106 ret
= sval_blank(expr
->unop
);
108 if (known_condition_true(expr
->unop
)) {
113 if (implied
== NOTIMPLIED
) {
118 if (implied_condition_true(expr
->unop
)) {
122 if (implied_condition_false(expr
->unop
)) {
126 if (implied
== IMPLIED_MIN
|| implied
== FUZZY_MIN
|| implied
== ABSOLUTE_MIN
) {
130 if (implied
== IMPLIED_MAX
|| implied
== FUZZY_MAX
|| implied
== ABSOLUTE_MAX
) {
138 static sval_t
handle_preop(struct expression
*expr
, int *undefined
, int implied
)
144 ret
= handle_ampersand(undefined
, implied
);
147 ret
= handle_negate(expr
, undefined
, implied
);
150 ret
= _get_value(expr
->unop
, undefined
, implied
);
151 ret
= sval_preop(ret
, '~');
152 ret
= sval_cast(get_type(expr
->unop
), ret
);
155 ret
= _get_value(expr
->unop
, undefined
, implied
);
156 ret
= sval_preop(ret
, '-');
159 ret
= _get_implied_value(expr
, undefined
, implied
);
162 ret
= handle_expression_statement(expr
, undefined
, implied
);
166 ret
= sval_blank(expr
);
168 ret
= sval_cast(get_type(expr
), ret
);
172 static sval_t
handle_divide(struct expression
*expr
, int *undefined
, int implied
)
176 left
= _get_value(expr
->left
, undefined
, implied
);
177 right
= _get_value(expr
->right
, undefined
, opposite_implied(implied
));
179 if (right
.value
== 0) {
184 return sval_binop(left
, '/', right
);
187 static sval_t
handle_subtract(struct expression
*expr
, int *undefined
, int implied
)
190 sval_t left
, right
, ret
;
191 int left_undefined
= 0;
192 int right_undefined
= 0;
193 int known_but_negative
= 0;
196 left
= _get_value(expr
->left
, &left_undefined
, implied
);
197 right
= _get_value(expr
->right
, &right_undefined
, opposite_implied(implied
));
199 if (!left_undefined
&& !right_undefined
) {
200 ret
= sval_binop(left
, '-', right
);
201 if (sval_is_negative(ret
))
202 known_but_negative
= 1;
204 return ret
; /* best case scenario */
207 comparison
= get_comparison(expr
->left
, expr
->right
);
211 type
= get_type(expr
);
213 switch (comparison
) {
215 case SPECIAL_UNSIGNED_GT
:
220 return sval_type_val(type
, 1);
224 return _get_value(expr
->left
, undefined
, implied
);
228 case SPECIAL_UNSIGNED_GTE
:
233 return sval_type_val(type
, 0);
237 return _get_value(expr
->left
, undefined
, implied
);
242 if (known_but_negative
)
250 static sval_t
handle_mod(struct expression
*expr
, int *undefined
, int implied
)
254 /* if we can't figure out the right side it's probably hopeless */
255 right
= _get_value(expr
->right
, undefined
, implied
);
256 if (*undefined
|| right
.value
== 0) {
264 left
= _get_value(expr
->left
, undefined
, implied
);
266 return sval_binop(left
, '%', right
);
272 return sval_type_val(get_type(expr
->left
), 0);
277 right
= sval_cast(get_type(expr
), right
);
284 static sval_t
handle_binop(struct expression
*expr
, int *undefined
, int implied
)
288 sval_t ret
= {.type
= &int_ctype
, {.value
= 123456} };
293 return handle_mod(expr
, undefined
, implied
);
295 left
= _get_value(expr
->left
, &local_undef
, implied
);
297 if (implied
== IMPLIED_MIN
|| implied
== ABSOLUTE_MIN
) {
298 ret
= sval_blank(expr
->left
);
302 if (implied
!= IMPLIED_MAX
&& implied
!= ABSOLUTE_MAX
)
304 if (!get_absolute_max(expr
->left
, &left
))
307 right
= _get_value(expr
->right
, undefined
, implied
);
310 return sval_binop(left
, '&', right
);
312 case SPECIAL_RIGHTSHIFT
:
313 left
= _get_value(expr
->left
, &local_undef
, implied
);
315 if (implied
== IMPLIED_MIN
|| implied
== ABSOLUTE_MIN
) {
316 ret
= sval_blank(expr
->left
);
320 if (implied
!= IMPLIED_MAX
&& implied
!= ABSOLUTE_MAX
)
322 if (!get_absolute_max(expr
->left
, &left
))
325 right
= _get_value(expr
->right
, undefined
, implied
);
328 return sval_binop(left
, SPECIAL_RIGHTSHIFT
, right
);
330 return handle_subtract(expr
, undefined
, implied
);
333 left
= _get_value(expr
->left
, undefined
, implied
);
334 right
= _get_value(expr
->right
, undefined
, implied
);
339 type
= get_type(expr
);
340 left
= sval_cast(type
, left
);
341 right
= sval_cast(type
, right
);
346 if (sval_binop_overflows(left
, expr
->op
, right
))
347 return sval_type_max(get_type(expr
));
351 if (sval_binop_overflows(left
, expr
->op
, right
)) {
359 return handle_divide(expr
, undefined
, implied
);
361 if (right
.value
== 0) {
365 return sval_binop(left
, '%', right
);
368 ret
= sval_binop(left
, expr
->op
, right
);
373 static int do_comparison(struct expression
*expr
)
375 struct range_list
*left_ranges
= NULL
;
376 struct range_list
*right_ranges
= NULL
;
377 int poss_true
, poss_false
;
379 get_implied_rl(expr
->left
, &left_ranges
);
380 get_implied_rl(expr
->right
, &right_ranges
);
382 poss_true
= possibly_true_rl(left_ranges
, expr
->op
, right_ranges
);
383 poss_false
= possibly_false_rl(left_ranges
, expr
->op
, right_ranges
);
385 free_rl(&left_ranges
);
386 free_rl(&right_ranges
);
388 if (!poss_true
&& !poss_false
)
390 if (poss_true
&& !poss_false
)
392 if (!poss_true
&& poss_false
)
397 static sval_t
handle_comparison(struct expression
*expr
, int *undefined
, int implied
)
402 if (get_value(expr
->left
, &left
) && get_value(expr
->right
, &right
)) {
403 struct data_range tmp_left
, tmp_right
;
407 tmp_right
.min
= right
;
408 tmp_right
.max
= right
;
409 if (true_comparison_range(&tmp_left
, expr
->op
, &tmp_right
))
414 if (implied
== NOTIMPLIED
) {
419 res
= do_comparison(expr
);
425 if (implied
== IMPLIED_MIN
|| implied
== FUZZY_MIN
|| implied
== ABSOLUTE_MIN
)
427 if (implied
== IMPLIED_MAX
|| implied
== FUZZY_MAX
|| implied
== ABSOLUTE_MAX
)
434 static sval_t
handle_logical(struct expression
*expr
, int *undefined
, int implied
)
440 if (implied
== NOTIMPLIED
) {
441 if (get_value(expr
->left
, &left
))
443 if (get_value(expr
->right
, &right
))
446 if (get_implied_value(expr
->left
, &left
))
448 if (get_implied_value(expr
->right
, &right
))
453 case SPECIAL_LOGICAL_OR
:
454 if (left_known
&& left
.value
)
456 if (right_known
&& right
.value
)
458 if (left_known
&& right_known
)
461 case SPECIAL_LOGICAL_AND
:
462 if (left_known
&& right_known
) {
463 if (left
.value
&& right
.value
)
473 if (implied
== IMPLIED_MIN
|| implied
== FUZZY_MIN
|| implied
== ABSOLUTE_MIN
)
475 if (implied
== IMPLIED_MAX
|| implied
== FUZZY_MAX
|| implied
== ABSOLUTE_MAX
)
482 static sval_t
handle_conditional(struct expression
*expr
, int *undefined
, int implied
)
484 if (known_condition_true(expr
->conditional
))
485 return _get_value(expr
->cond_true
, undefined
, implied
);
486 if (known_condition_false(expr
->conditional
))
487 return _get_value(expr
->cond_false
, undefined
, implied
);
489 if (implied
== NOTIMPLIED
) {
494 if (implied_condition_true(expr
->conditional
))
495 return _get_value(expr
->cond_true
, undefined
, implied
);
496 if (implied_condition_false(expr
->conditional
))
497 return _get_value(expr
->cond_false
, undefined
, implied
);
503 static int get_local_value(struct expression
*expr
, sval_t
*sval
, int implied
)
512 return get_local_min_helper(expr
, sval
);
517 return get_local_max_helper(expr
, sval
);
522 static int get_implied_value_helper(struct expression
*expr
, sval_t
*sval
, int implied
)
524 struct smatch_state
*state
;
528 /* fixme: this should return the casted value */
530 expr
= strip_expr(expr
);
532 if (get_value(expr
, sval
))
535 name
= expr_to_var_sym(expr
, &sym
);
538 *sval
= sval_blank(expr
);
539 state
= get_state(SMATCH_EXTRA
, name
, sym
);
541 if (!state
|| !state
->data
)
542 return get_local_value(expr
, sval
, implied
);
543 if (implied
== IMPLIED
) {
544 if (estate_get_single_value(state
, sval
))
548 if (implied
== HARD_MAX
) {
549 if (estate_get_hard_max(state
, sval
))
553 if (implied
== IMPLIED_MAX
|| implied
== ABSOLUTE_MAX
) {
554 *sval
= estate_max(state
);
557 *sval
= estate_min(state
);
561 static int get_fuzzy_max_helper(struct expression
*expr
, sval_t
*max
)
564 struct sm_state
*tmp
;
567 if (get_hard_max(expr
, &sval
)) {
572 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
576 sval
= sval_type_min(estate_type(sm
->state
));
577 FOR_EACH_PTR(sm
->possible
, tmp
) {
580 new_min
= estate_min(tmp
->state
);
581 if (sval_cmp(new_min
, sval
) > 0)
583 } END_FOR_EACH_PTR(tmp
);
585 if (sval_is_min(sval
))
587 if (sval
.value
== sval_type_min(sval
.type
).value
+ 1) /* it's common to be on off */
590 *max
= sval_cast(get_type(expr
), sval
);
594 static int get_fuzzy_min_helper(struct expression
*expr
, sval_t
*min
)
597 struct sm_state
*tmp
;
600 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
604 if (!sval_is_min(estate_min(sm
->state
))) {
605 *min
= estate_min(sm
->state
);
609 sval
= sval_type_max(estate_type(sm
->state
));
610 FOR_EACH_PTR(sm
->possible
, tmp
) {
613 new_max
= estate_max(tmp
->state
);
614 if (sval_cmp(new_max
, sval
) < 0)
616 } END_FOR_EACH_PTR(tmp
);
618 if (sval_is_max(sval
))
620 *min
= sval_cast(get_type(expr
), sval
);
624 static sval_t
_get_implied_value(struct expression
*expr
, int *undefined
, int implied
)
628 ret
= sval_blank(expr
);
637 if (!get_implied_value_helper(expr
, &ret
, implied
))
641 if (!get_fuzzy_max_helper(expr
, &ret
))
645 if (!get_fuzzy_min_helper(expr
, &ret
))
654 static int get_const_value(struct expression
*expr
, sval_t
*sval
)
659 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
662 if (!(sym
->ctype
.modifiers
& MOD_CONST
))
664 if (get_value(sym
->initializer
, &right
)) {
665 *sval
= sval_cast(get_type(expr
), right
);
671 static sval_t
handle_sizeof(struct expression
*expr
)
676 ret
= sval_blank(expr
);
677 sym
= expr
->cast_type
;
679 sym
= evaluate_expression(expr
->cast_expression
);
681 * Expressions of restricted types will possibly get
682 * promoted - check that here
684 if (is_restricted_type(sym
)) {
685 if (sym
->bit_size
< bits_in_int
)
687 } else if (is_fouled_type(sym
)) {
691 examine_symbol_type(sym
);
693 ret
.type
= size_t_ctype
;
694 if (sym
->bit_size
<= 0) /* sizeof(void) */
697 ret
.value
= bits_to_bytes(sym
->bit_size
);
702 static sval_t
_get_value(struct expression
*expr
, int *undefined
, int implied
)
713 expr
= strip_parens(expr
);
715 switch (expr
->type
) {
717 ret
= sval_from_val(expr
, expr
->value
);
720 ret
= handle_preop(expr
, undefined
, implied
);
723 ret
= _get_value(expr
->unop
, undefined
, implied
);
726 case EXPR_FORCE_CAST
:
727 case EXPR_IMPLIED_CAST
:
728 ret
= _get_value(expr
->cast_expression
, undefined
, implied
);
729 ret
= sval_cast(get_type(expr
), ret
);
732 ret
= handle_binop(expr
, undefined
, implied
);
735 ret
= handle_comparison(expr
, undefined
, implied
);
738 ret
= handle_logical(expr
, undefined
, implied
);
742 ret
= handle_sizeof(expr
);
745 if (get_const_value(expr
, &ret
)) {
748 ret
= _get_implied_value(expr
, undefined
, implied
);
751 case EXPR_CONDITIONAL
:
752 ret
= handle_conditional(expr
, undefined
, implied
);
755 ret
= _get_implied_value(expr
, undefined
, implied
);
762 /* returns 1 if it can get a value literal or else returns 0 */
763 int get_value(struct expression
*expr
, sval_t
*sval
)
768 ret
= _get_value(expr
, &undefined
, NOTIMPLIED
);
775 int get_implied_value(struct expression
*expr
, sval_t
*sval
)
780 ret
= _get_value(expr
, &undefined
, IMPLIED
);
787 int get_implied_min(struct expression
*expr
, sval_t
*sval
)
792 ret
= _get_value(expr
, &undefined
, IMPLIED_MIN
);
799 int get_implied_max(struct expression
*expr
, sval_t
*sval
)
804 ret
= _get_value(expr
, &undefined
, IMPLIED_MAX
);
811 int get_implied_rl(struct expression
*expr
, struct range_list
**rl
)
814 struct smatch_state
*state
;
821 expr
= strip_parens(expr
);
825 state
= get_state_expr(SMATCH_EXTRA
, expr
);
827 *rl
= clone_rl(estate_rl(state
));
831 if (expr
->type
== EXPR_CALL
) {
832 if (get_implied_return(expr
, rl
))
834 *rl
= db_return_vals(expr
);
840 if (get_implied_value(expr
, &sval
)) {
841 add_range(rl
, sval
, sval
);
845 if (get_local_rl(expr
, rl
))
848 min_known
= get_implied_min(expr
, &min
);
849 max_known
= get_implied_max(expr
, &max
);
850 if (!min_known
&& !max_known
)
853 get_absolute_min(expr
, &min
);
855 get_absolute_max(expr
, &max
);
857 *rl
= alloc_rl(min
, max
);
861 int get_hard_max(struct expression
*expr
, sval_t
*sval
)
866 ret
= _get_value(expr
, &undefined
, HARD_MAX
);
873 int get_fuzzy_min(struct expression
*expr
, sval_t
*sval
)
878 ret
= _get_value(expr
, &undefined
, FUZZY_MIN
);
885 int get_fuzzy_max(struct expression
*expr
, sval_t
*sval
)
890 ret
= _get_value(expr
, &undefined
, FUZZY_MAX
);
893 if (ret
.uvalue
> INT_MAX
- 10000)
899 int get_absolute_min(struct expression
*expr
, sval_t
*sval
)
904 type
= get_type(expr
);
906 type
= &llong_ctype
; // FIXME: this is wrong but places assume get type can't fail.
907 *sval
= _get_value(expr
, &undefined
, ABSOLUTE_MIN
);
909 *sval
= sval_type_min(type
);
913 if (sval_cmp(*sval
, sval_type_min(type
)) < 0)
914 *sval
= sval_type_min(type
);
918 int get_absolute_max(struct expression
*expr
, sval_t
*sval
)
923 type
= get_type(expr
);
926 *sval
= _get_value(expr
, &undefined
, ABSOLUTE_MAX
);
928 *sval
= sval_type_max(type
);
932 if (sval_cmp(sval_type_max(type
), *sval
) < 0)
933 *sval
= sval_type_max(type
);
937 int known_condition_true(struct expression
*expr
)
944 if (get_value(expr
, &tmp
) && tmp
.value
)
950 int known_condition_false(struct expression
*expr
)
958 if (expr
->type
== EXPR_CALL
) {
959 if (sym_name_is("__builtin_constant_p", expr
->fn
))
965 int implied_condition_true(struct expression
*expr
)
972 if (known_condition_true(expr
))
974 if (get_implied_value(expr
, &tmp
) && tmp
.value
)
977 if (expr
->type
== EXPR_POSTOP
)
978 return implied_condition_true(expr
->unop
);
980 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_DECREMENT
)
981 return implied_not_equal(expr
->unop
, 1);
982 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_INCREMENT
)
983 return implied_not_equal(expr
->unop
, -1);
985 expr
= strip_expr(expr
);
986 switch (expr
->type
) {
988 if (do_comparison(expr
) == 1)
992 if (expr
->op
== '!') {
993 if (implied_condition_false(expr
->unop
))
999 if (implied_not_equal(expr
, 0) == 1)
1006 int implied_condition_false(struct expression
*expr
)
1008 struct expression
*tmp
;
1014 if (known_condition_false(expr
))
1017 switch (expr
->type
) {
1019 if (do_comparison(expr
) == 2)
1022 if (expr
->op
== '!') {
1023 if (implied_condition_true(expr
->unop
))
1027 tmp
= strip_expr(expr
);
1029 return implied_condition_false(tmp
);
1032 if (get_implied_value(expr
, &sval
) && sval
.value
== 0)