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 return valid_ptr_min_sval
;
84 if (implied
== IMPLIED_MAX
|| implied
== FUZZYMAX
|| implied
== ABSOLUTE_MAX
)
85 return valid_ptr_max_sval
;
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
, get_type(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
);
122 ret
= sval_cast(ret
, get_type(expr
));
126 static sval_t
handle_divide(struct expression
*expr
, int *undefined
, int implied
)
130 left
= _get_value(expr
->left
, undefined
, implied
);
131 right
= _get_value(expr
->right
, undefined
, opposite_implied(implied
));
133 if (right
.value
== 0)
136 return sval_binop(left
, '/', right
);
139 static sval_t
handle_subtract(struct expression
*expr
, int *undefined
, int implied
)
143 left
= _get_value(expr
->left
, undefined
, implied
);
144 right
= _get_value(expr
->right
, undefined
, opposite_implied(implied
));
146 return sval_binop(left
, '-', right
);
149 static sval_t
handle_binop(struct expression
*expr
, int *undefined
, int implied
)
152 sval_t ret
= {.type
= &int_ctype
, .value
= 123456};
157 left
= _get_value(expr
->left
, &local_undef
, implied
);
159 if (implied
== ABSOLUTE_MIN
) {
160 ret
= sval_blank(expr
->left
);
164 if (implied
!= ABSOLUTE_MAX
)
166 if (!get_absolute_max_sval(expr
->left
, &left
))
169 right
= _get_value(expr
->right
, undefined
, implied
);
170 if (right
.value
== 0)
173 ret
= sval_binop(left
, '%', right
);
177 left
= _get_value(expr
->left
, &local_undef
, implied
);
179 if (implied
== ABSOLUTE_MIN
) {
180 ret
= sval_blank(expr
->left
);
184 if (implied
!= ABSOLUTE_MAX
)
186 if (!get_absolute_max_sval(expr
->left
, &left
))
189 right
= _get_value(expr
->right
, undefined
, implied
);
190 return sval_binop(left
, '&', right
);
192 case SPECIAL_RIGHTSHIFT
:
193 left
= _get_value(expr
->left
, &local_undef
, implied
);
195 if (implied
== ABSOLUTE_MIN
) {
196 ret
= sval_blank(expr
->left
);
200 if (implied
!= ABSOLUTE_MAX
)
202 if (!get_absolute_max_sval(expr
->left
, &left
))
205 right
= _get_value(expr
->right
, undefined
, implied
);
206 return sval_binop(left
, SPECIAL_RIGHTSHIFT
, right
);
209 left
= _get_value(expr
->left
, undefined
, implied
);
210 right
= _get_value(expr
->right
, undefined
, implied
);
214 return handle_divide(expr
, undefined
, implied
);
216 if (right
.value
== 0)
219 ret
= sval_binop(left
, '%', right
);
222 ret
= handle_subtract(expr
, undefined
, implied
);
225 ret
= sval_binop(left
, expr
->op
, right
);
230 static int do_comparison(struct expression
*expr
)
232 struct range_list_sval
*left_ranges
= NULL
;
233 struct range_list_sval
*right_ranges
= NULL
;
234 int poss_true
, poss_false
;
236 get_implied_range_list_sval(expr
->left
, &left_ranges
);
237 get_implied_range_list_sval(expr
->right
, &right_ranges
);
239 poss_true
= possibly_true_range_lists_sval(left_ranges
, expr
->op
, right_ranges
);
240 poss_false
= possibly_false_range_lists_sval(left_ranges
, expr
->op
, right_ranges
);
242 free_range_list_sval(&left_ranges
);
243 free_range_list_sval(&right_ranges
);
245 if (!poss_true
&& !poss_false
)
247 if (poss_true
&& !poss_false
)
249 if (!poss_true
&& poss_false
)
254 static sval_t
handle_comparison(struct expression
*expr
, int *undefined
, int implied
)
259 if (get_value_sval(expr
->left
, &left
) && get_value_sval(expr
->right
, &right
)) {
260 struct data_range_sval tmp_left
, tmp_right
;
264 tmp_right
.min
= right
;
265 tmp_right
.max
= right
;
266 if (true_comparison_range_sval(&tmp_left
, expr
->op
, &tmp_right
))
271 if (implied
== NOTIMPLIED
) {
276 res
= do_comparison(expr
);
282 if (implied
== IMPLIED_MIN
|| implied
== FUZZYMIN
|| implied
== ABSOLUTE_MIN
)
284 if (implied
== IMPLIED_MAX
|| implied
== FUZZYMAX
|| implied
== ABSOLUTE_MAX
)
291 static sval_t
handle_logical(struct expression
*expr
, int *undefined
, int implied
)
295 if ((implied
== NOTIMPLIED
&& get_value_sval(expr
->left
, &left
) &&
296 get_value_sval(expr
->right
, &right
)) ||
297 (implied
!= NOTIMPLIED
&& get_implied_value_sval(expr
->left
, &left
) &&
298 get_implied_value_sval(expr
->right
, &right
))) {
300 case SPECIAL_LOGICAL_OR
:
301 if (left
.value
|| right
.value
)
304 case SPECIAL_LOGICAL_AND
:
305 if (left
.value
&& right
.value
)
314 if (implied
== IMPLIED_MIN
|| implied
== FUZZYMIN
|| implied
== ABSOLUTE_MIN
)
316 if (implied
== IMPLIED_MAX
|| implied
== FUZZYMAX
|| implied
== ABSOLUTE_MAX
)
323 static sval_t
handle_conditional(struct expression
*expr
, int *undefined
, int implied
)
325 if (known_condition_true(expr
->conditional
))
326 return _get_value(expr
->cond_true
, undefined
, implied
);
327 if (known_condition_false(expr
->conditional
))
328 return _get_value(expr
->cond_false
, undefined
, implied
);
330 if (implied
== NOTIMPLIED
) {
335 if (implied_condition_true(expr
->conditional
))
336 return _get_value(expr
->cond_true
, undefined
, implied
);
337 if (implied_condition_false(expr
->conditional
))
338 return _get_value(expr
->cond_false
, undefined
, implied
);
344 static int get_implied_value_helper(struct expression
*expr
, sval_t
*sval
, int implied
)
346 struct smatch_state
*state
;
350 /* fixme: this should return the casted value */
352 expr
= strip_expr(expr
);
354 if (get_value_sval(expr
, sval
))
357 name
= get_variable_from_expr(expr
, &sym
);
360 *sval
= sval_blank(expr
);
361 state
= get_state(SMATCH_EXTRA
, name
, sym
);
363 if (!state
|| !state
->data
)
365 if (implied
== IMPLIED
) {
366 if (estate_get_single_value_sval(state
, sval
))
370 if (implied
== IMPLIED_MAX
|| implied
== ABSOLUTE_MAX
) {
371 *sval
= estate_max_sval(state
);
372 if (sval_is_max(*sval
)) /* this means just guessing. fixme. not really */
376 *sval
= estate_min_sval(state
);
377 if (sval_is_min(*sval
)) /* fixme */
382 static int get_fuzzy_max_helper(struct expression
*expr
, sval_t
*max
)
385 struct sm_state
*tmp
;
388 if (get_implied_max_sval(expr
, max
))
391 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
395 sval
= sval_type_min(&llong_ctype
);
396 FOR_EACH_PTR(sm
->possible
, tmp
) {
399 new_min
= estate_min_sval(tmp
->state
);
400 if (sval_cmp(new_min
, sval
) > 0)
402 } END_FOR_EACH_PTR(tmp
);
404 if (sval_is_min(sval
))
407 *max
= sval_cast(sval
, get_type(expr
));
411 static int get_fuzzy_min_helper(struct expression
*expr
, sval_t
*min
)
414 struct sm_state
*tmp
;
417 if (get_implied_min_sval(expr
, min
))
420 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
424 sval
= sval_type_max(&llong_ctype
);
425 FOR_EACH_PTR(sm
->possible
, tmp
) {
428 new_max
= estate_max_sval(tmp
->state
);
429 if (sval_cmp(new_max
, sval
) < 0)
431 } END_FOR_EACH_PTR(tmp
);
433 if (sval_is_max(sval
))
435 *min
= sval_cast(sval
, get_type(expr
));
439 static sval_t
_get_implied_value(struct expression
*expr
, int *undefined
, int implied
)
443 ret
= sval_blank(expr
);
449 if (!get_implied_value_helper(expr
, &ret
, implied
))
453 if (!get_absolute_min_helper(expr
, &ret
))
457 if (!get_absolute_max_helper(expr
, &ret
))
461 if (!get_fuzzy_max_helper(expr
, &ret
))
465 if (!get_fuzzy_min_helper(expr
, &ret
))
474 static int get_const_value(struct expression
*expr
, sval_t
*sval
)
479 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
482 if (!(sym
->ctype
.modifiers
& MOD_CONST
))
484 if (get_value_sval(sym
->initializer
, &right
)) {
485 *sval
= sval_cast(right
, get_type(expr
));
491 static sval_t
_get_value(struct expression
*expr
, int *undefined
, int implied
)
502 expr
= strip_parens(expr
);
504 switch (expr
->type
) {
506 tmp_ret
= sval_from_val(expr
, expr
->value
);
509 tmp_ret
= handle_preop(expr
, undefined
, implied
);
512 tmp_ret
= _get_value(expr
->unop
, undefined
, implied
);
515 case EXPR_FORCE_CAST
:
516 case EXPR_IMPLIED_CAST
:
517 tmp_ret
= _get_value(expr
->cast_expression
, undefined
, implied
);
518 tmp_ret
= sval_cast(tmp_ret
, get_type(expr
));
521 tmp_ret
= handle_binop(expr
, undefined
, implied
);
524 tmp_ret
= handle_comparison(expr
, undefined
, implied
);
527 tmp_ret
= handle_logical(expr
, undefined
, implied
);
531 tmp_ret
= sval_blank(expr
);
532 tmp_ret
.value
= get_expression_value_nomod(expr
);
535 if (get_const_value(expr
, &tmp_ret
)) {
538 tmp_ret
= _get_implied_value(expr
, undefined
, implied
);
541 case EXPR_CONDITIONAL
:
542 tmp_ret
= handle_conditional(expr
, undefined
, implied
);
545 tmp_ret
= _get_implied_value(expr
, undefined
, implied
);
552 /* returns 1 if it can get a value literal or else returns 0 */
553 int get_value_sval(struct expression
*expr
, sval_t
*sval
)
558 ret
= _get_value(expr
, &undefined
, NOTIMPLIED
);
565 int get_implied_value_sval(struct expression
*expr
, sval_t
*sval
)
570 ret
= _get_value(expr
, &undefined
, IMPLIED
);
577 int get_implied_min_sval(struct expression
*expr
, sval_t
*sval
)
582 ret
= _get_value(expr
, &undefined
, IMPLIED_MIN
);
589 int get_implied_max_sval(struct expression
*expr
, sval_t
*sval
)
594 ret
= _get_value(expr
, &undefined
, IMPLIED_MAX
);
601 int get_fuzzy_min_sval(struct expression
*expr
, sval_t
*sval
)
606 ret
= _get_value(expr
, &undefined
, FUZZYMIN
);
613 int get_fuzzy_max_sval(struct expression
*expr
, sval_t
*sval
)
618 ret
= _get_value(expr
, &undefined
, FUZZYMAX
);
625 int get_absolute_min_sval(struct expression
*expr
, sval_t
*sval
)
630 type
= get_type(expr
);
631 *sval
= _get_value(expr
, &undefined
, ABSOLUTE_MIN
);
633 *sval
= sval_type_min(type
);
637 if (sval_cmp(*sval
, sval_type_min(type
)) < 0)
638 *sval
= sval_type_min(type
);
642 int get_absolute_max_sval(struct expression
*expr
, sval_t
*sval
)
647 type
= get_type(expr
);
648 *sval
= _get_value(expr
, &undefined
, ABSOLUTE_MAX
);
650 *sval
= sval_type_max(type
);
654 if (sval_cmp(sval_type_max(type
), *sval
) < 0)
655 *sval
= sval_type_max(type
);
659 int known_condition_true(struct expression
*expr
)
666 if (get_value_sval(expr
, &tmp
) && tmp
.value
)
672 int known_condition_false(struct expression
*expr
)
680 if (expr
->type
== EXPR_CALL
) {
681 if (sym_name_is("__builtin_constant_p", expr
->fn
))
687 int implied_condition_true(struct expression
*expr
)
694 if (known_condition_true(expr
))
696 if (get_implied_value_sval(expr
, &tmp
) && tmp
.value
)
699 if (expr
->type
== EXPR_POSTOP
)
700 return implied_condition_true(expr
->unop
);
702 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_DECREMENT
)
703 return implied_not_equal(expr
->unop
, 1);
704 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_INCREMENT
)
705 return implied_not_equal(expr
->unop
, -1);
707 expr
= strip_expr(expr
);
708 switch (expr
->type
) {
710 if (do_comparison(expr
) == 1)
714 if (expr
->op
== '!') {
715 if (implied_condition_false(expr
->unop
))
721 if (implied_not_equal(expr
, 0) == 1)
728 int implied_condition_false(struct expression
*expr
)
730 struct expression
*tmp
;
736 if (known_condition_false(expr
))
739 switch (expr
->type
) {
741 if (do_comparison(expr
) == 2)
744 if (expr
->op
== '!') {
745 if (implied_condition_true(expr
->unop
))
749 tmp
= strip_expr(expr
);
751 return implied_condition_false(tmp
);
754 if (get_implied_value_sval(expr
, &sval
) && sval
.value
== 0)