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
};
35 static int opposite_implied(int implied
)
37 if (implied
== IMPLIED_MIN
)
39 if (implied
== IMPLIED_MAX
)
41 if (implied
== FUZZY_MIN
)
43 if (implied
== FUZZY_MAX
)
45 if (implied
== ABSOLUTE_MIN
)
47 if (implied
== ABSOLUTE_MAX
)
53 static int last_stmt_sval(struct statement
*stmt
, sval_t
*sval
)
55 struct expression
*expr
;
60 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
61 if (stmt
->type
!= STMT_EXPRESSION
)
63 expr
= stmt
->expression
;
64 if (!get_value(expr
, sval
))
69 static sval_t
handle_expression_statement(struct expression
*expr
, int *undefined
, int implied
)
71 struct statement
*stmt
;
74 stmt
= get_expression_statement(expr
);
75 if (!last_stmt_sval(stmt
, &ret
)) {
83 static sval_t
handle_ampersand(int *undefined
, int implied
)
87 ret
.type
= &ptr_ctype
;
90 if (implied
== IMPLIED_MIN
|| implied
== FUZZY_MIN
|| implied
== ABSOLUTE_MIN
)
91 return valid_ptr_min_sval
;
92 if (implied
== IMPLIED_MAX
|| implied
== FUZZY_MAX
|| implied
== ABSOLUTE_MAX
)
93 return valid_ptr_max_sval
;
99 static sval_t
handle_negate(struct expression
*expr
, int *undefined
, int implied
)
103 ret
= sval_blank(expr
->unop
);
105 if (known_condition_true(expr
->unop
)) {
110 if (implied
== NOTIMPLIED
) {
115 if (implied_condition_true(expr
->unop
)) {
119 if (implied_condition_false(expr
->unop
)) {
123 if (implied
== IMPLIED_MIN
|| implied
== FUZZY_MIN
|| implied
== ABSOLUTE_MIN
) {
127 if (implied
== IMPLIED_MAX
|| implied
== FUZZY_MAX
|| implied
== ABSOLUTE_MAX
) {
135 static sval_t
handle_preop(struct expression
*expr
, int *undefined
, int implied
)
141 ret
= handle_ampersand(undefined
, implied
);
144 ret
= handle_negate(expr
, undefined
, implied
);
147 ret
= _get_value(expr
->unop
, undefined
, implied
);
148 ret
= sval_preop(ret
, '~');
149 ret
= sval_cast(get_type(expr
->unop
), ret
);
152 ret
= _get_value(expr
->unop
, undefined
, implied
);
153 ret
= sval_preop(ret
, '-');
156 ret
= _get_implied_value(expr
, undefined
, implied
);
159 ret
= handle_expression_statement(expr
, undefined
, implied
);
163 ret
= sval_blank(expr
);
165 ret
= sval_cast(get_type(expr
), ret
);
169 static sval_t
handle_divide(struct expression
*expr
, int *undefined
, int implied
)
173 left
= _get_value(expr
->left
, undefined
, implied
);
174 right
= _get_value(expr
->right
, undefined
, opposite_implied(implied
));
176 if (right
.value
== 0) {
181 return sval_binop(left
, '/', right
);
184 static sval_t
handle_subtract(struct expression
*expr
, int *undefined
, int implied
)
188 left
= _get_value(expr
->left
, undefined
, implied
);
189 right
= _get_value(expr
->right
, undefined
, opposite_implied(implied
));
191 return sval_binop(left
, '-', right
);
194 static sval_t
handle_binop(struct expression
*expr
, int *undefined
, int implied
)
197 sval_t ret
= {.type
= &int_ctype
, .value
= 123456};
202 left
= _get_value(expr
->left
, &local_undef
, implied
);
204 if (implied
== ABSOLUTE_MIN
) {
205 ret
= sval_blank(expr
->left
);
209 if (implied
!= ABSOLUTE_MAX
)
211 if (!get_absolute_max(expr
->left
, &left
))
214 right
= _get_value(expr
->right
, undefined
, implied
);
215 if (right
.value
== 0)
219 return sval_binop(left
, '%', right
);
222 left
= _get_value(expr
->left
, &local_undef
, implied
);
224 if (implied
== ABSOLUTE_MIN
) {
225 ret
= sval_blank(expr
->left
);
229 if (implied
!= ABSOLUTE_MAX
)
231 if (!get_absolute_max(expr
->left
, &left
))
234 right
= _get_value(expr
->right
, undefined
, implied
);
237 return sval_binop(left
, '&', right
);
239 case SPECIAL_RIGHTSHIFT
:
240 left
= _get_value(expr
->left
, &local_undef
, implied
);
242 if (implied
== ABSOLUTE_MIN
) {
243 ret
= sval_blank(expr
->left
);
247 if (implied
!= ABSOLUTE_MAX
)
249 if (!get_absolute_max(expr
->left
, &left
))
252 right
= _get_value(expr
->right
, undefined
, implied
);
255 return sval_binop(left
, SPECIAL_RIGHTSHIFT
, right
);
258 left
= _get_value(expr
->left
, undefined
, implied
);
259 right
= _get_value(expr
->right
, undefined
, implied
);
266 return handle_divide(expr
, undefined
, implied
);
268 if (right
.value
== 0) {
272 return sval_binop(left
, '%', right
);
275 ret
= handle_subtract(expr
, undefined
, implied
);
278 ret
= sval_binop(left
, expr
->op
, right
);
283 static int do_comparison(struct expression
*expr
)
285 struct range_list
*left_ranges
= NULL
;
286 struct range_list
*right_ranges
= NULL
;
287 int poss_true
, poss_false
;
289 get_implied_range_list(expr
->left
, &left_ranges
);
290 get_implied_range_list(expr
->right
, &right_ranges
);
292 poss_true
= possibly_true_range_lists(left_ranges
, expr
->op
, right_ranges
);
293 poss_false
= possibly_false_range_lists(left_ranges
, expr
->op
, right_ranges
);
295 free_range_list(&left_ranges
);
296 free_range_list(&right_ranges
);
298 if (!poss_true
&& !poss_false
)
300 if (poss_true
&& !poss_false
)
302 if (!poss_true
&& poss_false
)
307 static sval_t
handle_comparison(struct expression
*expr
, int *undefined
, int implied
)
312 if (get_value(expr
->left
, &left
) && get_value(expr
->right
, &right
)) {
313 struct data_range tmp_left
, tmp_right
;
317 tmp_right
.min
= right
;
318 tmp_right
.max
= right
;
319 if (true_comparison_range(&tmp_left
, expr
->op
, &tmp_right
))
324 if (implied
== NOTIMPLIED
) {
329 res
= do_comparison(expr
);
335 if (implied
== IMPLIED_MIN
|| implied
== FUZZY_MIN
|| implied
== ABSOLUTE_MIN
)
337 if (implied
== IMPLIED_MAX
|| implied
== FUZZY_MAX
|| implied
== ABSOLUTE_MAX
)
344 static sval_t
handle_logical(struct expression
*expr
, int *undefined
, int implied
)
348 if ((implied
== NOTIMPLIED
&& get_value(expr
->left
, &left
) &&
349 get_value(expr
->right
, &right
)) ||
350 (implied
!= NOTIMPLIED
&& get_implied_value(expr
->left
, &left
) &&
351 get_implied_value(expr
->right
, &right
))) {
353 case SPECIAL_LOGICAL_OR
:
354 if (left
.value
|| right
.value
)
357 case SPECIAL_LOGICAL_AND
:
358 if (left
.value
&& right
.value
)
367 if (implied
== IMPLIED_MIN
|| implied
== FUZZY_MIN
|| implied
== ABSOLUTE_MIN
)
369 if (implied
== IMPLIED_MAX
|| implied
== FUZZY_MAX
|| implied
== ABSOLUTE_MAX
)
376 static sval_t
handle_conditional(struct expression
*expr
, int *undefined
, int implied
)
378 if (known_condition_true(expr
->conditional
))
379 return _get_value(expr
->cond_true
, undefined
, implied
);
380 if (known_condition_false(expr
->conditional
))
381 return _get_value(expr
->cond_false
, undefined
, implied
);
383 if (implied
== NOTIMPLIED
) {
388 if (implied_condition_true(expr
->conditional
))
389 return _get_value(expr
->cond_true
, undefined
, implied
);
390 if (implied_condition_false(expr
->conditional
))
391 return _get_value(expr
->cond_false
, undefined
, implied
);
397 static int get_implied_value_helper(struct expression
*expr
, sval_t
*sval
, int implied
)
399 struct smatch_state
*state
;
403 /* fixme: this should return the casted value */
405 expr
= strip_expr(expr
);
407 if (get_value(expr
, sval
))
410 name
= get_variable_from_expr(expr
, &sym
);
413 *sval
= sval_blank(expr
);
414 state
= get_state(SMATCH_EXTRA
, name
, sym
);
416 if (!state
|| !state
->data
)
418 if (implied
== IMPLIED
) {
419 if (estate_get_single_value(state
, sval
))
423 if (implied
== HARD_MAX
) {
424 if (estate_get_hard_max(state
, sval
))
428 if (implied
== IMPLIED_MAX
) {
429 *sval
= estate_max(state
);
430 if (sval_is_max(*sval
)) /* this means just guessing. fixme. not really */
434 *sval
= estate_min(state
);
435 if (sval_is_min(*sval
)) /* fixme */
440 static int get_fuzzy_max_helper(struct expression
*expr
, sval_t
*max
)
443 struct sm_state
*tmp
;
446 if (get_hard_max(expr
, &sval
)) {
451 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
455 sval
= sval_type_min(estate_type(sm
->state
));
456 FOR_EACH_PTR(sm
->possible
, tmp
) {
459 new_min
= estate_min(tmp
->state
);
460 if (sval_cmp(new_min
, sval
) > 0)
462 } END_FOR_EACH_PTR(tmp
);
464 if (sval_is_min(sval
))
467 *max
= sval_cast(get_type(expr
), sval
);
471 static int get_fuzzy_min_helper(struct expression
*expr
, sval_t
*min
)
474 struct sm_state
*tmp
;
477 if (get_implied_min(expr
, min
))
480 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
484 sval
= sval_type_max(estate_type(sm
->state
));
485 FOR_EACH_PTR(sm
->possible
, tmp
) {
488 new_max
= estate_max(tmp
->state
);
489 if (sval_cmp(new_max
, sval
) < 0)
491 } END_FOR_EACH_PTR(tmp
);
493 if (sval_is_max(sval
))
495 *min
= sval_cast(get_type(expr
), sval
);
499 static sval_t
_get_implied_value(struct expression
*expr
, int *undefined
, int implied
)
503 ret
= sval_blank(expr
);
510 if (!get_implied_value_helper(expr
, &ret
, implied
))
514 if (!get_absolute_min_helper(expr
, &ret
))
518 if (!get_absolute_max_helper(expr
, &ret
))
522 if (!get_fuzzy_max_helper(expr
, &ret
))
526 if (!get_fuzzy_min_helper(expr
, &ret
))
535 static int get_const_value(struct expression
*expr
, sval_t
*sval
)
540 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
543 if (!(sym
->ctype
.modifiers
& MOD_CONST
))
545 if (get_value(sym
->initializer
, &right
)) {
546 *sval
= sval_cast(get_type(expr
), right
);
552 static sval_t
_get_value(struct expression
*expr
, int *undefined
, int implied
)
563 expr
= strip_parens(expr
);
565 switch (expr
->type
) {
567 ret
= sval_from_val(expr
, expr
->value
);
570 ret
= handle_preop(expr
, undefined
, implied
);
573 ret
= _get_value(expr
->unop
, undefined
, implied
);
576 case EXPR_FORCE_CAST
:
577 case EXPR_IMPLIED_CAST
:
578 ret
= _get_value(expr
->cast_expression
, undefined
, implied
);
579 ret
= sval_cast(get_type(expr
), ret
);
582 ret
= handle_binop(expr
, undefined
, implied
);
585 ret
= handle_comparison(expr
, undefined
, implied
);
588 ret
= handle_logical(expr
, undefined
, implied
);
592 ret
= sval_blank(expr
);
593 ret
.value
= get_expression_value_nomod(expr
);
596 if (get_const_value(expr
, &ret
)) {
599 ret
= _get_implied_value(expr
, undefined
, implied
);
602 case EXPR_CONDITIONAL
:
603 ret
= handle_conditional(expr
, undefined
, implied
);
606 ret
= _get_implied_value(expr
, undefined
, implied
);
613 /* returns 1 if it can get a value literal or else returns 0 */
614 int get_value(struct expression
*expr
, sval_t
*sval
)
619 ret
= _get_value(expr
, &undefined
, NOTIMPLIED
);
626 int get_implied_value(struct expression
*expr
, sval_t
*sval
)
631 ret
= _get_value(expr
, &undefined
, IMPLIED
);
638 int get_implied_min(struct expression
*expr
, sval_t
*sval
)
643 ret
= _get_value(expr
, &undefined
, IMPLIED_MIN
);
650 int get_implied_max(struct expression
*expr
, sval_t
*sval
)
655 ret
= _get_value(expr
, &undefined
, IMPLIED_MAX
);
662 int get_hard_max(struct expression
*expr
, sval_t
*sval
)
667 ret
= _get_value(expr
, &undefined
, HARD_MAX
);
674 int get_fuzzy_min(struct expression
*expr
, sval_t
*sval
)
679 ret
= _get_value(expr
, &undefined
, FUZZY_MIN
);
686 int get_fuzzy_max(struct expression
*expr
, sval_t
*sval
)
691 ret
= _get_value(expr
, &undefined
, FUZZY_MAX
);
698 int get_absolute_min(struct expression
*expr
, sval_t
*sval
)
703 type
= get_type(expr
);
705 type
= &llong_ctype
; // FIXME: this is wrong but places assume get type can't fail.
706 *sval
= _get_value(expr
, &undefined
, ABSOLUTE_MIN
);
708 *sval
= sval_type_min(type
);
712 if (sval_cmp(*sval
, sval_type_min(type
)) < 0)
713 *sval
= sval_type_min(type
);
717 int get_absolute_max(struct expression
*expr
, sval_t
*sval
)
722 type
= get_type(expr
);
725 *sval
= _get_value(expr
, &undefined
, ABSOLUTE_MAX
);
727 *sval
= sval_type_max(type
);
731 if (sval_cmp(sval_type_max(type
), *sval
) < 0)
732 *sval
= sval_type_max(type
);
736 int known_condition_true(struct expression
*expr
)
743 if (get_value(expr
, &tmp
) && tmp
.value
)
749 int known_condition_false(struct expression
*expr
)
757 if (expr
->type
== EXPR_CALL
) {
758 if (sym_name_is("__builtin_constant_p", expr
->fn
))
764 int implied_condition_true(struct expression
*expr
)
771 if (known_condition_true(expr
))
773 if (get_implied_value(expr
, &tmp
) && tmp
.value
)
776 if (expr
->type
== EXPR_POSTOP
)
777 return implied_condition_true(expr
->unop
);
779 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_DECREMENT
)
780 return implied_not_equal(expr
->unop
, 1);
781 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_INCREMENT
)
782 return implied_not_equal(expr
->unop
, -1);
784 expr
= strip_expr(expr
);
785 switch (expr
->type
) {
787 if (do_comparison(expr
) == 1)
791 if (expr
->op
== '!') {
792 if (implied_condition_false(expr
->unop
))
798 if (implied_not_equal(expr
, 0) == 1)
805 int implied_condition_false(struct expression
*expr
)
807 struct expression
*tmp
;
813 if (known_condition_false(expr
))
816 switch (expr
->type
) {
818 if (do_comparison(expr
) == 2)
821 if (expr
->op
== '!') {
822 if (implied_condition_true(expr
->unop
))
826 tmp
= strip_expr(expr
);
828 return implied_condition_false(tmp
);
831 if (get_implied_value(expr
, &sval
) && sval
.value
== 0)