4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 #include "smatch_slist.h"
12 #include "smatch_extra.h"
14 static sval_t
_get_value(struct expression
*expr
, int *undefined
, int implied
);
15 static sval_t
_get_implied_value(struct expression
*expr
, int *undefined
, int implied
);
19 static sval_t zero
= {.type
= &int_ctype
, .value
= 0};
20 static sval_t one
= {.type
= &int_ctype
, .value
= 1};
21 static sval_t bogus
= {.type
= &int_ctype
, .value
= BOGUS
};
29 #define ABSOLUTE_MIN 6
30 #define ABSOLUTE_MAX 7
32 static int opposite_implied(int implied
)
34 if (implied
== IMPLIED_MIN
)
36 if (implied
== IMPLIED_MAX
)
38 if (implied
== ABSOLUTE_MIN
)
40 if (implied
== ABSOLUTE_MAX
)
45 static int last_stmt_sval(struct statement
*stmt
, sval_t
*sval
)
47 struct expression
*expr
;
52 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
53 if (stmt
->type
!= STMT_EXPRESSION
)
55 expr
= stmt
->expression
;
56 if (!get_value_sval(expr
, sval
))
61 static sval_t
handle_expression_statement(struct expression
*expr
, int *undefined
, int implied
)
63 struct statement
*stmt
;
66 stmt
= get_expression_statement(expr
);
67 if (!last_stmt_sval(stmt
, &ret
)) {
75 static sval_t
handle_ampersand(int *undefined
, int implied
)
79 ret
.type
= &ptr_ctype
;
82 if (implied
== IMPLIED_MIN
|| implied
== FUZZYMIN
|| implied
== ABSOLUTE_MIN
)
83 ret
.value
= valid_ptr_min
;
84 if (implied
== IMPLIED_MAX
|| implied
== FUZZYMAX
|| implied
== ABSOLUTE_MAX
)
85 ret
.value
= valid_ptr_max
;
91 static sval_t
handle_preop(struct expression
*expr
, int *undefined
, int implied
)
97 ret
= handle_ampersand(undefined
, implied
);
100 ret
= _get_value(expr
->unop
, undefined
, implied
);
101 ret
= sval_preop(ret
, '!');
104 ret
= _get_value(expr
->unop
, undefined
, implied
);
105 ret
= sval_preop(ret
, '~');
106 ret
= sval_cast(ret
, expr
->unop
);
109 ret
= _get_value(expr
->unop
, undefined
, implied
);
110 ret
= sval_preop(ret
, '-');
113 ret
= _get_implied_value(expr
, undefined
, implied
);
116 ret
= handle_expression_statement(expr
, undefined
, implied
);
120 ret
= sval_blank(expr
);
125 static sval_t
handle_divide(struct expression
*expr
, int *undefined
, int implied
)
129 left
= _get_value(expr
->left
, undefined
, implied
);
130 right
= _get_value(expr
->right
, undefined
, opposite_implied(implied
));
132 if (right
.value
== 0)
135 return sval_binop(left
, '/', right
);
138 static sval_t
handle_subtract(struct expression
*expr
, int *undefined
, int implied
)
142 left
= _get_value(expr
->left
, undefined
, implied
);
143 right
= _get_value(expr
->right
, undefined
, opposite_implied(implied
));
145 return sval_binop(left
, '-', right
);
148 static sval_t
handle_binop(struct expression
*expr
, int *undefined
, int implied
)
151 sval_t ret
= {.type
= &int_ctype
, .value
= 123456};
156 left
= _get_value(expr
->left
, &local_undef
, implied
);
158 if (implied
== ABSOLUTE_MIN
) {
159 ret
= sval_blank(expr
->left
);
163 if (implied
!= ABSOLUTE_MAX
)
165 if (!get_absolute_max_sval(expr
->left
, &left
))
168 right
= _get_value(expr
->right
, undefined
, implied
);
169 if (right
.value
== 0)
172 ret
= sval_binop(left
, '%', right
);
176 left
= _get_value(expr
->left
, &local_undef
, implied
);
178 if (implied
== ABSOLUTE_MIN
) {
179 ret
= sval_blank(expr
->left
);
183 if (implied
!= ABSOLUTE_MAX
)
185 if (!get_absolute_max_sval(expr
->left
, &left
))
188 right
= _get_value(expr
->right
, undefined
, implied
);
189 return sval_binop(left
, '&', right
);
191 case SPECIAL_RIGHTSHIFT
:
192 left
= _get_value(expr
->left
, &local_undef
, implied
);
194 if (implied
== ABSOLUTE_MIN
) {
195 ret
= sval_blank(expr
->left
);
199 if (implied
!= ABSOLUTE_MAX
)
201 if (!get_absolute_max_sval(expr
->left
, &left
))
204 right
= _get_value(expr
->right
, undefined
, implied
);
205 return sval_binop(left
, SPECIAL_RIGHTSHIFT
, right
);
208 left
= _get_value(expr
->left
, undefined
, implied
);
209 right
= _get_value(expr
->right
, undefined
, implied
);
213 return handle_divide(expr
, undefined
, implied
);
215 if (right
.value
== 0)
218 ret
= sval_binop(left
, '%', right
);
221 ret
= handle_subtract(expr
, undefined
, implied
);
224 ret
= sval_binop(left
, expr
->op
, right
);
229 static int do_comparison(struct expression
*expr
)
231 struct range_list
*left_ranges
= NULL
;
232 struct range_list
*right_ranges
= NULL
;
233 int poss_true
, poss_false
;
235 get_implied_range_list(expr
->left
, &left_ranges
);
236 get_implied_range_list(expr
->right
, &right_ranges
);
238 poss_true
= possibly_true_range_lists(left_ranges
, expr
->op
, right_ranges
);
239 poss_false
= possibly_false_range_lists(left_ranges
, expr
->op
, right_ranges
);
241 free_range_list(&left_ranges
);
242 free_range_list(&right_ranges
);
244 if (!poss_true
&& !poss_false
)
246 if (poss_true
&& !poss_false
)
248 if (!poss_true
&& poss_false
)
253 static sval_t
handle_comparison(struct expression
*expr
, int *undefined
, int implied
)
258 if (get_value_sval(expr
->left
, &left
) && get_value_sval(expr
->right
, &right
)) {
259 struct data_range tmp_left
, tmp_right
;
261 tmp_left
.min
= left
.value
;
262 tmp_left
.max
= left
.value
;
263 tmp_right
.min
= right
.value
;
264 tmp_right
.max
= right
.value
;
265 if (true_comparison_range(&tmp_left
, expr
->op
, &tmp_right
))
270 if (implied
== NOTIMPLIED
) {
275 res
= do_comparison(expr
);
281 if (implied
== IMPLIED_MIN
|| implied
== FUZZYMIN
|| implied
== ABSOLUTE_MIN
)
283 if (implied
== IMPLIED_MAX
|| implied
== FUZZYMAX
|| implied
== ABSOLUTE_MAX
)
290 static sval_t
handle_logical(struct expression
*expr
, int *undefined
, int implied
)
294 if ((implied
== NOTIMPLIED
&& get_value_sval(expr
->left
, &left
) &&
295 get_value_sval(expr
->right
, &right
)) ||
296 (implied
!= NOTIMPLIED
&& get_implied_value_sval(expr
->left
, &left
) &&
297 get_implied_value_sval(expr
->right
, &right
))) {
299 case SPECIAL_LOGICAL_OR
:
300 if (left
.value
|| right
.value
)
303 case SPECIAL_LOGICAL_AND
:
304 if (left
.value
&& right
.value
)
313 if (implied
== IMPLIED_MIN
|| implied
== FUZZYMIN
|| implied
== ABSOLUTE_MIN
)
315 if (implied
== IMPLIED_MAX
|| implied
== FUZZYMAX
|| implied
== ABSOLUTE_MAX
)
322 static sval_t
handle_conditional(struct expression
*expr
, int *undefined
, int implied
)
324 if (known_condition_true(expr
->conditional
))
325 return _get_value(expr
->cond_true
, undefined
, implied
);
326 if (known_condition_false(expr
->conditional
))
327 return _get_value(expr
->cond_false
, undefined
, implied
);
329 if (implied
== NOTIMPLIED
) {
334 if (implied_condition_true(expr
->conditional
))
335 return _get_value(expr
->cond_true
, undefined
, implied
);
336 if (implied_condition_false(expr
->conditional
))
337 return _get_value(expr
->cond_false
, undefined
, implied
);
343 static int get_implied_value_helper(struct expression
*expr
, sval_t
*sval
, int implied
)
345 struct smatch_state
*state
;
350 expr
= strip_expr(expr
);
352 if (get_value_sval(expr
, sval
))
355 name
= get_variable_from_expr(expr
, &sym
);
358 *sval
= sval_blank(expr
);
359 state
= get_state(SMATCH_EXTRA
, name
, sym
);
361 if (!state
|| !state
->data
)
363 if (implied
== IMPLIED
) {
364 if (estate_get_single_value(state
, &val
)) {
370 if (implied
== IMPLIED_MAX
|| implied
== ABSOLUTE_MAX
) {
371 val
= estate_max(state
);
372 if (val
== whole_range
.max
) /* this means just guessing */
377 val
= estate_min(state
);
378 if (val
== whole_range
.min
)
384 static int get_fuzzy_max_helper(struct expression
*expr
, sval_t
*max
)
387 struct sm_state
*tmp
;
390 *max
= sval_blank(expr
);
391 if (get_implied_max(expr
, &val
)) {
396 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
400 val
= whole_range
.min
;
401 FOR_EACH_PTR(sm
->possible
, tmp
) {
404 new_min
= estate_min(tmp
->state
);
407 } END_FOR_EACH_PTR(tmp
);
409 if (val
> whole_range
.min
) {
416 static int get_fuzzy_min_helper(struct expression
*expr
, sval_t
*min
)
419 struct sm_state
*tmp
;
422 *min
= sval_blank(expr
);
423 if (get_implied_min(expr
, &val
)) {
428 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
432 val
= whole_range
.max
;
433 FOR_EACH_PTR(sm
->possible
, tmp
) {
436 new_max
= estate_max(tmp
->state
);
439 } END_FOR_EACH_PTR(tmp
);
441 if (val
< whole_range
.max
) {
448 static sval_t
_get_implied_value(struct expression
*expr
, int *undefined
, int implied
)
452 ret
= sval_blank(expr
);
458 if (!get_implied_value_helper(expr
, &ret
, implied
))
463 struct smatch_state
*state
;
464 struct data_range
*range
;
466 if (get_implied_value_helper(expr
, &ret
, implied
))
469 state
= get_state_expr(absolute_id
, expr
);
470 if (!state
|| !state
->data
) {
475 if (implied
== ABSOLUTE_MAX
)
476 ret
.value
= range
->max
;
478 ret
.value
= range
->min
;
482 if (!get_fuzzy_max_helper(expr
, &ret
))
486 if (!get_fuzzy_min_helper(expr
, &ret
))
495 static int get_const_value(struct expression
*expr
, sval_t
*sval
)
499 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
502 *sval
= sval_blank(expr
);
503 if (!(sym
->ctype
.modifiers
& MOD_CONST
))
505 if (get_value(sym
->initializer
, &sval
->value
))
510 static sval_t
_get_value(struct expression
*expr
, int *undefined
, int implied
)
521 expr
= strip_parens(expr
);
523 switch (expr
->type
) {
525 tmp_ret
= sval_from_val(expr
, expr
->value
);
528 tmp_ret
= handle_preop(expr
, undefined
, implied
);
531 tmp_ret
= _get_value(expr
->unop
, undefined
, implied
);
534 case EXPR_FORCE_CAST
:
535 case EXPR_IMPLIED_CAST
:
536 tmp_ret
= _get_value(expr
->cast_expression
, undefined
, implied
);
537 tmp_ret
= sval_cast(tmp_ret
, expr
);
540 tmp_ret
= handle_binop(expr
, undefined
, implied
);
543 tmp_ret
= handle_comparison(expr
, undefined
, implied
);
546 tmp_ret
= handle_logical(expr
, undefined
, implied
);
550 tmp_ret
= sval_blank(expr
);
551 tmp_ret
.value
= get_expression_value_nomod(expr
);
554 if (get_const_value(expr
, &tmp_ret
)) {
557 tmp_ret
= _get_implied_value(expr
, undefined
, implied
);
560 case EXPR_CONDITIONAL
:
561 tmp_ret
= handle_conditional(expr
, undefined
, implied
);
564 tmp_ret
= _get_implied_value(expr
, undefined
, implied
);
571 /* returns 1 if it can get a value literal or else returns 0 */
572 int get_value(struct expression
*expr
, long long *val
)
577 tmp_ret
= _get_value(expr
, &undefined
, NOTIMPLIED
);
580 *val
= tmp_ret
.value
;
584 int get_value_sval(struct expression
*expr
, sval_t
*val
)
588 *val
= _get_value(expr
, &undefined
, NOTIMPLIED
);
594 int get_implied_value(struct expression
*expr
, long long *val
)
599 tmp_ret
= _get_value(expr
, &undefined
, IMPLIED
);
600 *val
= tmp_ret
.value
;
604 int get_implied_value_sval(struct expression
*expr
, sval_t
*sval
)
608 *sval
= _get_value(expr
, &undefined
, IMPLIED
);
612 int get_implied_min(struct expression
*expr
, long long *val
)
617 tmp_ret
= _get_value(expr
, &undefined
, IMPLIED_MIN
);
618 *val
= tmp_ret
.value
;
622 int get_implied_min_sval(struct expression
*expr
, sval_t
*sval
)
626 *sval
= _get_value(expr
, &undefined
, IMPLIED_MIN
);
630 int get_implied_max(struct expression
*expr
, long long *val
)
635 tmp_ret
= _get_value(expr
, &undefined
, IMPLIED_MAX
);
636 *val
= tmp_ret
.value
;
640 int get_implied_max_sval(struct expression
*expr
, sval_t
*sval
)
644 *sval
= _get_value(expr
, &undefined
, IMPLIED_MAX
);
648 int get_fuzzy_min(struct expression
*expr
, long long *val
)
653 tmp_ret
= _get_value(expr
, &undefined
, FUZZYMIN
);
654 *val
= tmp_ret
.value
;
658 int get_fuzzy_min_sval(struct expression
*expr
, sval_t
*sval
)
662 *sval
= _get_value(expr
, &undefined
, FUZZYMIN
);
666 int get_fuzzy_max(struct expression
*expr
, long long *val
)
671 tmp_ret
= _get_value(expr
, &undefined
, FUZZYMAX
);
672 *val
= tmp_ret
.value
;
676 int get_fuzzy_max_sval(struct expression
*expr
, sval_t
*sval
)
680 *sval
= _get_value(expr
, &undefined
, FUZZYMAX
);
684 int get_absolute_min(struct expression
*expr
, long long *val
)
690 type
= get_type(expr
);
691 tmp_ret
= _get_value(expr
, &undefined
, ABSOLUTE_MIN
);
692 *val
= tmp_ret
.value
;
694 *val
= type_min(type
);
698 if (sval_cmp_val(tmp_ret
, type_min(type
)) < 0)
699 *val
= type_min(type
);
703 int get_absolute_min_sval(struct expression
*expr
, sval_t
*sval
)
708 type
= get_type(expr
);
709 *sval
= _get_value(expr
, &undefined
, ABSOLUTE_MIN
);
711 sval
->value
= type_min(type
);
715 if (sval_cmp_val(*sval
, type_min(type
)) < 0)
716 sval
->value
= type_min(type
);
720 int get_absolute_max_sval(struct expression
*expr
, sval_t
*sval
)
724 *sval
= _get_value(expr
, &undefined
, ABSOLUTE_MAX
);
726 sval
->value
= sval_type_max(sval
->type
).value
;
730 if (type_max(sval
->type
) < sval
->value
)
731 sval
->value
= sval_type_max(sval
->type
).value
;
735 int known_condition_true(struct expression
*expr
)
742 if (get_value_sval(expr
, &tmp
) && tmp
.value
)
748 int known_condition_false(struct expression
*expr
)
756 if (expr
->type
== EXPR_CALL
) {
757 if (sym_name_is("__builtin_constant_p", expr
->fn
))
763 int implied_condition_true(struct expression
*expr
)
770 if (known_condition_true(expr
))
772 if (get_implied_value_sval(expr
, &tmp
) && tmp
.value
)
775 if (expr
->type
== EXPR_POSTOP
)
776 return implied_condition_true(expr
->unop
);
778 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_DECREMENT
)
779 return implied_not_equal(expr
->unop
, 1);
780 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_INCREMENT
)
781 return implied_not_equal(expr
->unop
, -1);
783 expr
= strip_expr(expr
);
784 switch (expr
->type
) {
786 if (do_comparison(expr
) == 1)
790 if (expr
->op
== '!') {
791 if (implied_condition_false(expr
->unop
))
797 if (implied_not_equal(expr
, 0) == 1)
804 int implied_condition_false(struct expression
*expr
)
806 struct expression
*tmp
;
812 if (known_condition_false(expr
))
815 switch (expr
->type
) {
817 if (do_comparison(expr
) == 2)
820 if (expr
->op
== '!') {
821 if (implied_condition_true(expr
->unop
))
825 tmp
= strip_expr(expr
);
827 return implied_condition_false(tmp
);
830 if (get_implied_value_sval(expr
, &sval
) && sval
.value
== 0)