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 struct range_list
*_get_rl(struct expression
*expr
, int implied
);
16 static struct range_list
*handle_variable(struct expression
*expr
, int implied
);
18 static sval_t zero
= {.type
= &int_ctype
, {.value
= 0} };
19 static sval_t one
= {.type
= &int_ctype
, {.value
= 1} };
21 static struct range_list
*rl_zero(void)
23 return alloc_rl(zero
, zero
);
26 static struct range_list
*rl_one(void)
28 return alloc_rl(one
, one
);
39 static struct range_list
*last_stmt_rl(struct statement
*stmt
, int implied
)
44 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
45 if (stmt
->type
!= STMT_EXPRESSION
)
47 return _get_rl(stmt
->expression
, implied
);
50 static struct range_list
*handle_expression_statement_rl(struct expression
*expr
, int implied
)
52 return last_stmt_rl(get_expression_statement(expr
), implied
);
55 static struct range_list
*handle_ampersand_rl(int implied
)
57 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
59 return alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
62 static struct range_list
*handle_negate_rl(struct expression
*expr
, int implied
)
64 if (known_condition_true(expr
->unop
))
66 if (known_condition_false(expr
->unop
))
69 if (implied
== RL_EXACT
)
72 if (implied_condition_true(expr
->unop
))
74 if (implied_condition_false(expr
->unop
))
76 return alloc_rl(zero
, one
);
79 static struct range_list
*handle_bitwise_negate(struct expression
*expr
, int implied
)
81 struct range_list
*rl
;
84 rl
= _get_rl(expr
->unop
, implied
);
85 if (!rl_to_sval(rl
, &sval
))
87 sval
= sval_preop(sval
, '~');
88 sval_cast(get_type(expr
->unop
), sval
);
89 return alloc_rl(sval
, sval
);
92 static struct range_list
*handle_minus_preop(struct expression
*expr
, int implied
)
94 struct range_list
*rl
;
97 rl
= _get_rl(expr
->unop
, implied
);
98 if (!rl_to_sval(rl
, &sval
))
100 sval
= sval_preop(sval
, '-');
101 return alloc_rl(sval
, sval
);
104 static struct range_list
*handle_preop_rl(struct expression
*expr
, int implied
)
108 return handle_ampersand_rl(implied
);
110 return handle_negate_rl(expr
, implied
);
112 return handle_bitwise_negate(expr
, implied
);
114 return handle_minus_preop(expr
, implied
);
116 return handle_variable(expr
, implied
);
118 return handle_expression_statement_rl(expr
, implied
);
124 static struct range_list
*handle_divide_rl(struct expression
*expr
, int implied
)
126 struct range_list
*left_rl
, *right_rl
;
130 type
= get_type(expr
);
132 left_rl
= _get_rl(expr
->left
, implied
);
133 left_rl
= cast_rl(type
, left_rl
);
134 right_rl
= _get_rl(expr
->right
, implied
);
135 right_rl
= cast_rl(type
, right_rl
);
137 if (!left_rl
|| !right_rl
)
139 if (is_whole_rl(left_rl
) || is_whole_rl(right_rl
))
141 if (sval_is_negative(rl_min(left_rl
)) || sval_cmp_val(rl_min(right_rl
), 0) <= 0)
144 max
= rl_max(left_rl
);
145 if (sval_is_max(max
))
146 max
= sval_binop(max
, '/', rl_min(right_rl
));
147 min
= sval_binop(rl_min(left_rl
), '/', rl_max(right_rl
));
148 return alloc_rl(min
, max
);
151 static struct range_list
*handle_subtract_rl(struct expression
*expr
, int implied
)
154 struct range_list
*left_rl
, *right_rl
;
155 sval_t max
, min
, tmp
;
158 type
= get_type(expr
);
159 left_rl
= _get_rl(expr
->left
, implied
);
160 left_rl
= cast_rl(type
, left_rl
);
162 left_rl
= alloc_whole_rl(type
);
163 right_rl
= _get_rl(expr
->right
, implied
);
164 right_rl
= cast_rl(type
, right_rl
);
166 right_rl
= alloc_whole_rl(type
);
167 comparison
= get_comparison(expr
->left
, expr
->right
);
169 /* negative values complicate everything fix this later */
170 if (sval_is_negative(rl_min(right_rl
)))
172 max
= rl_max(left_rl
);
174 switch (comparison
) {
176 case SPECIAL_UNSIGNED_GT
:
177 min
= sval_type_val(type
, 1);
178 max
= rl_max(left_rl
);
181 case SPECIAL_UNSIGNED_GTE
:
182 min
= sval_type_val(type
, 0);
183 max
= rl_max(left_rl
);
186 if (sval_binop_overflows(rl_min(left_rl
), '-', rl_max(right_rl
)))
188 min
= sval_type_min(type
);
191 if (!sval_binop_overflows(rl_min(left_rl
), '-', rl_max(right_rl
))) {
192 tmp
= sval_binop(rl_min(left_rl
), '-', rl_max(right_rl
));
193 if (sval_cmp(tmp
, min
) > 0)
197 if (!sval_is_max(rl_max(left_rl
))) {
198 tmp
= sval_binop(rl_max(left_rl
), '-', rl_min(right_rl
));
199 if (sval_cmp(tmp
, max
) < 0)
203 if (sval_is_min(min
) && sval_is_max(max
))
206 return cast_rl(type
, alloc_rl(min
, max
));
209 static struct range_list
*handle_mod_rl(struct expression
*expr
, int implied
)
211 struct range_list
*rl
;
212 sval_t left
, right
, sval
;
214 if (implied
== RL_EXACT
) {
215 if (!get_value(expr
->right
, &right
))
217 if (!get_value(expr
->left
, &left
))
219 sval
= sval_binop(left
, '%', right
);
220 return alloc_rl(sval
, sval
);
222 /* if we can't figure out the right side it's probably hopeless */
223 if (!get_implied_value(expr
->right
, &right
))
226 right
= sval_cast(get_type(expr
), right
);
229 rl
= _get_rl(expr
->left
, implied
);
230 if (rl
&& rl_max(rl
).uvalue
< right
.uvalue
)
231 right
.uvalue
= rl_max(rl
).uvalue
;
233 return alloc_rl(zero
, right
);
236 static struct range_list
*handle_bitwise_AND(struct expression
*expr
, int implied
)
239 struct range_list
*left_rl
, *right_rl
;
241 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
243 type
= get_type(expr
);
245 left_rl
= _get_rl(expr
->left
, implied
);
247 left_rl
= cast_rl(type
, left_rl
);
248 left_rl
= alloc_rl(sval_type_val(type
, 0), rl_max(left_rl
));
250 left_rl
= alloc_whole_rl(type
);
253 right_rl
= _get_rl(expr
->right
, implied
);
255 right_rl
= cast_rl(type
, right_rl
);
256 right_rl
= alloc_rl(sval_type_val(type
, 0), rl_max(right_rl
));
258 right_rl
= alloc_whole_rl(type
);
261 return rl_intersection(left_rl
, right_rl
);
264 static struct range_list
*handle_right_shift(struct expression
*expr
, int implied
)
266 struct range_list
*left_rl
;
270 if (implied
== RL_HARD
)
272 /* this is hopeless without the right side */
273 if (!get_implied_value(expr
->right
, &right
))
275 left_rl
= _get_rl(expr
->left
, implied
);
277 max
= rl_max(left_rl
);
278 min
= rl_min(left_rl
);
280 if (implied
== RL_FUZZY
)
282 if (implied
== RL_HARD
)
284 max
= sval_type_max(get_type(expr
->left
));
285 min
= sval_type_val(get_type(expr
->left
), 0);
288 max
= sval_binop(max
, SPECIAL_RIGHTSHIFT
, right
);
289 min
= sval_binop(min
, SPECIAL_RIGHTSHIFT
, right
);
290 return alloc_rl(min
, max
);
293 static struct range_list
*handle_known_binop(struct expression
*expr
)
297 if (!get_value(expr
->left
, &left
))
299 if (!get_value(expr
->right
, &right
))
301 left
= sval_binop(left
, expr
->op
, right
);
302 return alloc_rl(left
, left
);
305 static struct range_list
*handle_binop_rl(struct expression
*expr
, int implied
)
308 struct range_list
*left_rl
, *right_rl
, *rl
;
311 rl
= handle_known_binop(expr
);
314 if (implied
== RL_EXACT
)
319 return handle_mod_rl(expr
, implied
);
321 return handle_bitwise_AND(expr
, implied
);
322 case SPECIAL_RIGHTSHIFT
:
323 return handle_right_shift(expr
, implied
);
325 return handle_subtract_rl(expr
, implied
);
327 return handle_divide_rl(expr
, implied
);
330 type
= get_type(expr
);
331 left_rl
= _get_rl(expr
->left
, implied
);
332 left_rl
= cast_rl(type
, left_rl
);
333 right_rl
= _get_rl(expr
->right
, implied
);
334 right_rl
= cast_rl(type
, right_rl
);
336 if (!left_rl
|| !right_rl
)
339 if (sval_binop_overflows(rl_min(left_rl
), expr
->op
, rl_min(right_rl
)))
341 min
= sval_binop(rl_min(left_rl
), expr
->op
, rl_min(right_rl
));
343 if (sval_binop_overflows(rl_max(left_rl
), expr
->op
, rl_max(right_rl
))) {
349 max
= sval_type_max(get_type(expr
));
352 max
= sval_binop(rl_max(left_rl
), expr
->op
, rl_max(right_rl
));
355 return alloc_rl(min
, max
);
358 static int do_comparison(struct expression
*expr
)
360 struct range_list
*left_ranges
= NULL
;
361 struct range_list
*right_ranges
= NULL
;
362 int poss_true
, poss_false
;
365 type
= get_type(expr
);
367 get_implied_rl(expr
->left
, &left_ranges
);
368 get_implied_rl(expr
->right
, &right_ranges
);
369 left_ranges
= cast_rl(type
, left_ranges
);
370 right_ranges
= cast_rl(type
, right_ranges
);
372 poss_true
= possibly_true_rl(left_ranges
, expr
->op
, right_ranges
);
373 poss_false
= possibly_false_rl(left_ranges
, expr
->op
, right_ranges
);
375 free_rl(&left_ranges
);
376 free_rl(&right_ranges
);
378 if (!poss_true
&& !poss_false
)
380 if (poss_true
&& !poss_false
)
382 if (!poss_true
&& poss_false
)
387 static struct range_list
*handle_comparison_rl(struct expression
*expr
, int implied
)
392 if (get_value(expr
->left
, &left
) && get_value(expr
->right
, &right
)) {
393 struct data_range tmp_left
, tmp_right
;
397 tmp_right
.min
= right
;
398 tmp_right
.max
= right
;
399 if (true_comparison_range(&tmp_left
, expr
->op
, &tmp_right
))
404 if (implied
== RL_EXACT
)
407 res
= do_comparison(expr
);
413 return alloc_rl(zero
, one
);
416 static struct range_list
*handle_logical_rl(struct expression
*expr
, int implied
)
422 if (implied
== RL_EXACT
) {
423 if (get_value(expr
->left
, &left
))
425 if (get_value(expr
->right
, &right
))
428 if (get_implied_value(expr
->left
, &left
))
430 if (get_implied_value(expr
->right
, &right
))
435 case SPECIAL_LOGICAL_OR
:
436 if (left_known
&& left
.value
)
438 if (right_known
&& right
.value
)
440 if (left_known
&& right_known
)
443 case SPECIAL_LOGICAL_AND
:
444 if (left_known
&& right_known
) {
445 if (left
.value
&& right
.value
)
454 if (implied
== RL_EXACT
)
457 return alloc_rl(zero
, one
);
460 static struct range_list
*handle_conditional_rl(struct expression
*expr
, int implied
)
462 struct range_list
*true_rl
, *false_rl
;
464 int final_pass_orig
= final_pass
;
466 if (known_condition_true(expr
->conditional
))
467 return _get_rl(expr
->cond_true
, implied
);
468 if (known_condition_false(expr
->conditional
))
469 return _get_rl(expr
->cond_false
, implied
);
471 if (implied
== RL_EXACT
)
474 if (implied_condition_true(expr
->conditional
))
475 return _get_rl(expr
->cond_true
, implied
);
476 if (implied_condition_false(expr
->conditional
))
477 return _get_rl(expr
->cond_false
, implied
);
480 /* this becomes a problem with deeply nested conditional statements */
484 type
= get_type(expr
);
486 __push_fake_cur_slist();
488 __split_whole_condition(expr
);
489 true_rl
= _get_rl(expr
->cond_true
, implied
);
490 __push_true_states();
491 __use_false_states();
492 false_rl
= _get_rl(expr
->cond_false
, implied
);
493 __merge_true_states();
494 __free_fake_cur_slist();
495 final_pass
= final_pass_orig
;
497 if (!true_rl
|| !false_rl
)
499 true_rl
= cast_rl(type
, true_rl
);
500 false_rl
= cast_rl(type
, false_rl
);
502 return rl_union(true_rl
, false_rl
);
505 static int get_fuzzy_max_helper(struct expression
*expr
, sval_t
*max
)
508 struct sm_state
*tmp
;
511 if (get_hard_max(expr
, &sval
)) {
516 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
520 sval
= sval_type_min(estate_type(sm
->state
));
521 FOR_EACH_PTR(sm
->possible
, tmp
) {
524 new_min
= estate_min(tmp
->state
);
525 if (sval_cmp(new_min
, sval
) > 0)
527 } END_FOR_EACH_PTR(tmp
);
529 if (sval_is_min(sval
))
531 if (sval
.value
== sval_type_min(sval
.type
).value
+ 1) /* it's common to be on off */
534 *max
= sval_cast(get_type(expr
), sval
);
538 static int get_fuzzy_min_helper(struct expression
*expr
, sval_t
*min
)
541 struct sm_state
*tmp
;
544 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
548 if (!sval_is_min(estate_min(sm
->state
))) {
549 *min
= estate_min(sm
->state
);
553 sval
= sval_type_max(estate_type(sm
->state
));
554 FOR_EACH_PTR(sm
->possible
, tmp
) {
557 new_max
= estate_max(tmp
->state
);
558 if (sval_cmp(new_max
, sval
) < 0)
560 } END_FOR_EACH_PTR(tmp
);
562 if (sval_is_max(sval
))
564 *min
= sval_cast(get_type(expr
), sval
);
568 static int get_const_value(struct expression
*expr
, sval_t
*sval
)
573 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
576 if (!(sym
->ctype
.modifiers
& MOD_CONST
))
578 if (get_value(sym
->initializer
, &right
)) {
579 *sval
= sval_cast(get_type(expr
), right
);
585 static struct range_list
*handle_variable(struct expression
*expr
, int implied
)
587 struct smatch_state
*state
;
588 struct range_list
*rl
;
589 sval_t sval
, min
, max
;
591 if (get_const_value(expr
, &sval
))
592 return alloc_rl(sval
, sval
);
600 state
= get_state_expr(SMATCH_EXTRA
, expr
);
601 if (!state
|| !state
->data
) {
602 if (get_local_rl(expr
, &rl
))
606 if (implied
== RL_HARD
&& !estate_has_hard_max(state
))
608 return clone_rl(estate_rl(state
));
610 if (!get_fuzzy_min_helper(expr
, &min
))
611 min
= sval_type_min(get_type(expr
));
612 if (!get_fuzzy_max_helper(expr
, &max
))
614 return alloc_rl(min
, max
);
619 static sval_t
handle_sizeof(struct expression
*expr
)
624 ret
= sval_blank(expr
);
625 sym
= expr
->cast_type
;
627 sym
= evaluate_expression(expr
->cast_expression
);
629 * Expressions of restricted types will possibly get
630 * promoted - check that here
632 if (is_restricted_type(sym
)) {
633 if (sym
->bit_size
< bits_in_int
)
635 } else if (is_fouled_type(sym
)) {
639 examine_symbol_type(sym
);
641 ret
.type
= size_t_ctype
;
642 if (sym
->bit_size
<= 0) /* sizeof(void) */ {
643 if (get_real_base_type(sym
) == &void_ctype
)
648 ret
.value
= bits_to_bytes(sym
->bit_size
);
653 static struct range_list
*handle_call_rl(struct expression
*expr
, int implied
)
655 struct range_list
*rl
;
657 if (implied
== RL_EXACT
)
660 if (get_implied_return(expr
, &rl
))
662 return db_return_vals(expr
);
665 static struct range_list
*_get_rl(struct expression
*expr
, int implied
)
667 struct range_list
*rl
;
671 expr
= strip_parens(expr
);
674 type
= get_type(expr
);
676 switch (expr
->type
) {
678 sval
= sval_from_val(expr
, expr
->value
);
679 rl
= alloc_rl(sval
, sval
);
682 rl
= handle_preop_rl(expr
, implied
);
685 rl
= _get_rl(expr
->unop
, implied
);
688 case EXPR_FORCE_CAST
:
689 case EXPR_IMPLIED_CAST
:
690 rl
= _get_rl(expr
->cast_expression
, implied
);
691 rl
= cast_rl(type
, rl
);
694 rl
= handle_binop_rl(expr
, implied
);
697 rl
= handle_comparison_rl(expr
, implied
);
700 rl
= handle_logical_rl(expr
, implied
);
704 sval
= handle_sizeof(expr
);
705 rl
= alloc_rl(sval
, sval
);
708 case EXPR_CONDITIONAL
:
709 rl
= handle_conditional_rl(expr
, implied
);
712 rl
= handle_call_rl(expr
, implied
);
715 rl
= handle_variable(expr
, implied
);
720 if (type
&& implied
== RL_ABSOLUTE
)
721 return alloc_whole_rl(type
);
725 /* returns 1 if it can get a value literal or else returns 0 */
726 int get_value(struct expression
*expr
, sval_t
*sval
)
728 struct range_list
*rl
;
730 rl
= _get_rl(expr
, RL_EXACT
);
731 if (!rl_to_sval(rl
, sval
))
736 int get_implied_value(struct expression
*expr
, sval_t
*sval
)
738 struct range_list
*rl
;
740 rl
= _get_rl(expr
, RL_IMPLIED
);
741 if (!rl_to_sval(rl
, sval
))
746 int get_implied_min(struct expression
*expr
, sval_t
*sval
)
748 struct range_list
*rl
;
750 rl
= _get_rl(expr
, RL_IMPLIED
);
757 int get_implied_max(struct expression
*expr
, sval_t
*sval
)
759 struct range_list
*rl
;
761 rl
= _get_rl(expr
, RL_IMPLIED
);
768 int get_implied_rl(struct expression
*expr
, struct range_list
**rl
)
770 *rl
= _get_rl(expr
, RL_IMPLIED
);
776 int get_hard_max(struct expression
*expr
, sval_t
*sval
)
778 struct range_list
*rl
;
780 rl
= _get_rl(expr
, RL_HARD
);
787 int get_fuzzy_min(struct expression
*expr
, sval_t
*sval
)
789 struct range_list
*rl
;
791 rl
= _get_rl(expr
, RL_FUZZY
);
798 int get_fuzzy_max(struct expression
*expr
, sval_t
*sval
)
800 struct range_list
*rl
;
803 rl
= _get_rl(expr
, RL_FUZZY
);
807 if (max
.uvalue
> INT_MAX
- 10000)
813 int get_absolute_min(struct expression
*expr
, sval_t
*sval
)
815 struct range_list
*rl
;
818 type
= get_type(expr
);
820 type
= &llong_ctype
; // FIXME: this is wrong but places assume get type can't fail.
821 rl
= _get_rl(expr
, RL_ABSOLUTE
);
825 *sval
= sval_type_min(type
);
827 if (sval_cmp(*sval
, sval_type_min(type
)) < 0)
828 *sval
= sval_type_min(type
);
832 int get_absolute_max(struct expression
*expr
, sval_t
*sval
)
834 struct range_list
*rl
;
837 type
= get_type(expr
);
840 rl
= _get_rl(expr
, RL_ABSOLUTE
);
844 *sval
= sval_type_max(type
);
846 if (sval_cmp(sval_type_max(type
), *sval
) < 0)
847 *sval
= sval_type_max(type
);
851 int known_condition_true(struct expression
*expr
)
858 if (get_value(expr
, &tmp
) && tmp
.value
)
864 int known_condition_false(struct expression
*expr
)
872 if (expr
->type
== EXPR_CALL
) {
873 if (sym_name_is("__builtin_constant_p", expr
->fn
))
879 int implied_condition_true(struct expression
*expr
)
886 if (known_condition_true(expr
))
888 if (get_implied_value(expr
, &tmp
) && tmp
.value
)
891 if (expr
->type
== EXPR_POSTOP
)
892 return implied_condition_true(expr
->unop
);
894 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_DECREMENT
)
895 return implied_not_equal(expr
->unop
, 1);
896 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_INCREMENT
)
897 return implied_not_equal(expr
->unop
, -1);
899 expr
= strip_expr(expr
);
900 switch (expr
->type
) {
902 if (do_comparison(expr
) == 1)
906 if (expr
->op
== '!') {
907 if (implied_condition_false(expr
->unop
))
913 if (implied_not_equal(expr
, 0) == 1)
920 int implied_condition_false(struct expression
*expr
)
922 struct expression
*tmp
;
928 if (known_condition_false(expr
))
931 switch (expr
->type
) {
933 if (do_comparison(expr
) == 2)
936 if (expr
->op
== '!') {
937 if (implied_condition_true(expr
->unop
))
941 tmp
= strip_expr(expr
);
943 return implied_condition_false(tmp
);
946 if (get_implied_value(expr
, &sval
) && sval
.value
== 0)