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
);
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_sval
*left_ranges
= NULL
;
232 struct range_list_sval
*right_ranges
= NULL
;
233 int poss_true
, poss_false
;
235 get_implied_range_list_sval(expr
->left
, &left_ranges
);
236 get_implied_range_list_sval(expr
->right
, &right_ranges
);
238 poss_true
= possibly_true_range_lists_sval(left_ranges
, expr
->op
, right_ranges
);
239 poss_false
= possibly_false_range_lists_sval(left_ranges
, expr
->op
, right_ranges
);
241 free_range_list_sval(&left_ranges
);
242 free_range_list_sval(&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_sval tmp_left
, tmp_right
;
263 tmp_right
.min
= right
;
264 tmp_right
.max
= right
;
265 if (true_comparison_range_sval(&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
;
349 /* fixme: this should return the casted value */
351 expr
= strip_expr(expr
);
353 if (get_value_sval(expr
, sval
))
356 name
= get_variable_from_expr(expr
, &sym
);
359 *sval
= sval_blank(expr
);
360 state
= get_state(SMATCH_EXTRA
, name
, sym
);
362 if (!state
|| !state
->data
)
364 if (implied
== IMPLIED
) {
365 if (estate_get_single_value_sval(state
, sval
))
369 if (implied
== IMPLIED_MAX
|| implied
== ABSOLUTE_MAX
) {
370 *sval
= estate_max_sval(state
);
371 if (sval_is_max(*sval
)) /* this means just guessing. fixme. not really */
375 *sval
= estate_min_sval(state
);
376 if (sval_is_min(*sval
)) /* fixme */
381 static int get_fuzzy_max_helper(struct expression
*expr
, sval_t
*max
)
384 struct sm_state
*tmp
;
387 if (get_implied_max_sval(expr
, max
))
390 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
394 sval
= sval_type_min(&llong_ctype
);
395 FOR_EACH_PTR(sm
->possible
, tmp
) {
398 new_min
= estate_min_sval(tmp
->state
);
399 if (sval_cmp(new_min
, sval
) > 0)
401 } END_FOR_EACH_PTR(tmp
);
403 if (sval_is_min(sval
))
406 *max
= sval_cast(sval
, get_type(expr
));
410 static int get_fuzzy_min_helper(struct expression
*expr
, sval_t
*min
)
413 struct sm_state
*tmp
;
416 if (get_implied_min_sval(expr
, min
))
419 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
423 sval
= sval_type_max(&llong_ctype
);
424 FOR_EACH_PTR(sm
->possible
, tmp
) {
427 new_max
= estate_max_sval(tmp
->state
);
428 if (sval_cmp(new_max
, sval
) < 0)
430 } END_FOR_EACH_PTR(tmp
);
432 if (sval_is_max(sval
))
434 *min
= sval_cast(sval
, get_type(expr
));
438 static sval_t
_get_implied_value(struct expression
*expr
, int *undefined
, int implied
)
442 ret
= sval_blank(expr
);
448 if (!get_implied_value_helper(expr
, &ret
, implied
))
452 if (!get_absolute_min_helper(expr
, &ret
))
456 if (!get_absolute_max_helper(expr
, &ret
))
460 if (!get_fuzzy_max_helper(expr
, &ret
))
464 if (!get_fuzzy_min_helper(expr
, &ret
))
473 static int get_const_value(struct expression
*expr
, sval_t
*sval
)
478 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
481 if (!(sym
->ctype
.modifiers
& MOD_CONST
))
483 if (get_value_sval(sym
->initializer
, &right
)) {
484 *sval
= sval_cast(right
, get_type(expr
));
490 static sval_t
_get_value(struct expression
*expr
, int *undefined
, int implied
)
501 expr
= strip_parens(expr
);
503 switch (expr
->type
) {
505 tmp_ret
= sval_from_val(expr
, expr
->value
);
508 tmp_ret
= handle_preop(expr
, undefined
, implied
);
511 tmp_ret
= _get_value(expr
->unop
, undefined
, implied
);
514 case EXPR_FORCE_CAST
:
515 case EXPR_IMPLIED_CAST
:
516 tmp_ret
= _get_value(expr
->cast_expression
, undefined
, implied
);
517 tmp_ret
= sval_cast(tmp_ret
, get_type(expr
));
520 tmp_ret
= handle_binop(expr
, undefined
, implied
);
523 tmp_ret
= handle_comparison(expr
, undefined
, implied
);
526 tmp_ret
= handle_logical(expr
, undefined
, implied
);
530 tmp_ret
= sval_blank(expr
);
531 tmp_ret
.value
= get_expression_value_nomod(expr
);
534 if (get_const_value(expr
, &tmp_ret
)) {
537 tmp_ret
= _get_implied_value(expr
, undefined
, implied
);
540 case EXPR_CONDITIONAL
:
541 tmp_ret
= handle_conditional(expr
, undefined
, implied
);
544 tmp_ret
= _get_implied_value(expr
, undefined
, implied
);
551 /* returns 1 if it can get a value literal or else returns 0 */
552 int get_value_sval(struct expression
*expr
, sval_t
*val
)
556 *val
= _get_value(expr
, &undefined
, NOTIMPLIED
);
562 int get_implied_value_sval(struct expression
*expr
, sval_t
*sval
)
566 *sval
= _get_value(expr
, &undefined
, IMPLIED
);
570 int get_implied_min_sval(struct expression
*expr
, sval_t
*sval
)
574 *sval
= _get_value(expr
, &undefined
, IMPLIED_MIN
);
578 int get_implied_max_sval(struct expression
*expr
, sval_t
*sval
)
582 *sval
= _get_value(expr
, &undefined
, IMPLIED_MAX
);
586 int get_fuzzy_min_sval(struct expression
*expr
, sval_t
*sval
)
590 *sval
= _get_value(expr
, &undefined
, FUZZYMIN
);
594 int get_fuzzy_max_sval(struct expression
*expr
, sval_t
*sval
)
598 *sval
= _get_value(expr
, &undefined
, FUZZYMAX
);
602 int get_absolute_min_sval(struct expression
*expr
, sval_t
*sval
)
607 type
= get_type(expr
);
608 *sval
= _get_value(expr
, &undefined
, ABSOLUTE_MIN
);
610 *sval
= sval_type_min(type
);
614 if (sval_cmp(*sval
, sval_type_min(type
)) < 0)
615 *sval
= sval_type_min(type
);
619 int get_absolute_max_sval(struct expression
*expr
, sval_t
*sval
)
623 *sval
= _get_value(expr
, &undefined
, ABSOLUTE_MAX
);
625 sval
->value
= sval_type_max(sval
->type
).value
;
629 if (sval_cmp(sval_type_max(sval
->type
), *sval
) < 0)
630 sval
->value
= sval_type_max(sval
->type
).value
;
634 int known_condition_true(struct expression
*expr
)
641 if (get_value_sval(expr
, &tmp
) && tmp
.value
)
647 int known_condition_false(struct expression
*expr
)
655 if (expr
->type
== EXPR_CALL
) {
656 if (sym_name_is("__builtin_constant_p", expr
->fn
))
662 int implied_condition_true(struct expression
*expr
)
669 if (known_condition_true(expr
))
671 if (get_implied_value_sval(expr
, &tmp
) && tmp
.value
)
674 if (expr
->type
== EXPR_POSTOP
)
675 return implied_condition_true(expr
->unop
);
677 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_DECREMENT
)
678 return implied_not_equal(expr
->unop
, 1);
679 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_INCREMENT
)
680 return implied_not_equal(expr
->unop
, -1);
682 expr
= strip_expr(expr
);
683 switch (expr
->type
) {
685 if (do_comparison(expr
) == 1)
689 if (expr
->op
== '!') {
690 if (implied_condition_false(expr
->unop
))
696 if (implied_not_equal(expr
, 0) == 1)
703 int implied_condition_false(struct expression
*expr
)
705 struct expression
*tmp
;
711 if (known_condition_false(expr
))
714 switch (expr
->type
) {
716 if (do_comparison(expr
) == 2)
719 if (expr
->op
== '!') {
720 if (implied_condition_true(expr
->unop
))
724 tmp
= strip_expr(expr
);
726 return implied_condition_false(tmp
);
729 if (get_implied_value_sval(expr
, &sval
) && sval
.value
== 0)