2 * Copyright (C) 2008 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * smatch_extra.c is supposed to track the value of every variable.
34 #include "smatch_slist.h"
35 #include "smatch_extra.h"
40 static void match_link_modify(struct sm_state
*sm
, struct expression
*mod_expr
);
42 struct string_list
*__ignored_macros
= NULL
;
43 static int in_warn_on_macro(void)
45 struct statement
*stmt
;
49 stmt
= get_current_statement();
52 macro
= get_macro_name(stmt
->pos
);
56 FOR_EACH_PTR(__ignored_macros
, tmp
) {
57 if (!strcmp(tmp
, macro
))
59 } END_FOR_EACH_PTR(tmp
);
63 typedef void (mod_hook
)(const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
64 DECLARE_PTR_LIST(void_fn_list
, mod_hook
*);
65 static struct void_fn_list
*extra_mod_hooks
;
66 void add_extra_mod_hook(mod_hook
*fn
)
68 mod_hook
**p
= malloc(sizeof(mod_hook
*));
70 add_ptr_list(&extra_mod_hooks
, p
);
73 void call_extra_mod_hooks(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
77 FOR_EACH_PTR(extra_mod_hooks
, fn
) {
78 (*fn
)(name
, sym
, state
);
79 } END_FOR_EACH_PTR(fn
);
82 struct sm_state
*set_extra_mod_helper(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
84 remove_from_equiv(name
, sym
);
85 call_extra_mod_hooks(name
, sym
, state
);
86 if (__in_fake_assign
&& estate_is_unknown(state
) && !get_state(SMATCH_EXTRA
, name
, sym
))
88 return set_state(SMATCH_EXTRA
, name
, sym
, state
);
91 static char *get_pointed_at(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
93 struct expression
*assigned
;
97 if (strcmp(name
+ 1, sym
->ident
->name
) != 0)
100 assigned
= get_assigned_expr_name_sym(sym
->ident
->name
, sym
);
103 assigned
= strip_parens(assigned
);
104 if (assigned
->type
!= EXPR_PREOP
|| assigned
->op
!= '&')
107 return expr_to_var_sym(assigned
->unop
, new_sym
);
110 char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
112 struct expression
*assigned
;
113 char *orig_name
= NULL
;
120 if (!sym
|| !sym
->ident
)
123 ret
= get_pointed_at(name
, sym
, new_sym
);
127 skip
= strlen(sym
->ident
->name
);
128 if (name
[skip
] != '-' || name
[skip
+ 1] != '>')
132 assigned
= get_assigned_expr_name_sym(sym
->ident
->name
, sym
);
135 if (assigned
->type
== EXPR_CALL
)
136 return map_call_to_other_name_sym(name
, sym
, new_sym
);
137 if (assigned
->type
== EXPR_PREOP
|| assigned
->op
== '&') {
139 orig_name
= expr_to_var_sym(assigned
, new_sym
);
140 if (!orig_name
|| !*new_sym
)
143 snprintf(buf
, sizeof(buf
), "%s.%s", orig_name
+ 1, name
+ skip
);
144 ret
= alloc_string(buf
);
145 free_string(orig_name
);
149 if (assigned
->type
!= EXPR_DEREF
)
152 orig_name
= expr_to_var_sym(assigned
, new_sym
);
153 if (!orig_name
|| !*new_sym
)
156 snprintf(buf
, sizeof(buf
), "%s->%s", orig_name
, name
+ skip
);
157 ret
= alloc_string(buf
);
158 free_string(orig_name
);
162 free_string(orig_name
);
166 struct sm_state
*set_extra_mod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
169 struct symbol
*new_sym
;
172 sm
= set_extra_mod_helper(name
, sym
, state
);
173 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
174 if (new_name
&& new_sym
)
175 set_extra_mod_helper(new_name
, new_sym
, state
);
176 free_string(new_name
);
180 static void clear_array_states(struct expression
*array
)
184 sm
= get_sm_state_expr(link_id
, array
);
186 match_link_modify(sm
, NULL
);
189 static struct sm_state
*set_extra_array_mod(struct expression
*expr
, struct smatch_state
*state
)
191 struct expression
*array
;
192 struct var_sym_list
*vsl
;
196 struct sm_state
*ret
= NULL
;
198 array
= get_array_base(expr
);
200 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
202 clear_array_states(array
);
206 FOR_EACH_PTR(vsl
, vs
) {
207 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
208 } END_FOR_EACH_PTR(vs
);
210 call_extra_mod_hooks(name
, sym
, state
);
211 ret
= set_state(SMATCH_EXTRA
, name
, sym
, state
);
217 struct sm_state
*set_extra_expr_mod(struct expression
*expr
, struct smatch_state
*state
)
221 struct sm_state
*ret
= NULL
;
224 return set_extra_array_mod(expr
, state
);
226 expr
= strip_expr(expr
);
227 name
= expr_to_var_sym(expr
, &sym
);
230 ret
= set_extra_mod(name
, sym
, state
);
236 void set_extra_nomod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
239 struct symbol
*new_sym
;
240 struct relation
*rel
;
241 struct smatch_state
*orig_state
;
243 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
245 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
246 if (new_name
&& new_sym
)
247 set_state(SMATCH_EXTRA
, new_name
, new_sym
, state
);
248 free_string(new_name
);
250 if (!estate_related(orig_state
)) {
251 set_state(SMATCH_EXTRA
, name
, sym
, state
);
255 set_related(state
, estate_related(orig_state
));
256 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
257 struct smatch_state
*estate
;
259 if (option_debug_related
)
260 sm_msg("%s updating related %s to %s", name
, rel
->name
, state
->name
);
261 estate
= get_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
);
264 set_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
, clone_estate_cast(estate_type(estate
), state
));
265 } END_FOR_EACH_PTR(rel
);
268 void set_extra_nomod_vsl(const char *name
, struct symbol
*sym
, struct var_sym_list
*vsl
, struct smatch_state
*state
)
272 FOR_EACH_PTR(vsl
, vs
) {
273 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
274 } END_FOR_EACH_PTR(vs
);
276 set_extra_nomod(name
, sym
, state
);
280 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
282 void set_extra_expr_nomod(struct expression
*expr
, struct smatch_state
*state
)
284 struct var_sym_list
*vsl
;
289 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
292 FOR_EACH_PTR(vsl
, vs
) {
293 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
294 } END_FOR_EACH_PTR(vs
);
296 set_extra_nomod(name
, sym
, state
);
301 static void set_extra_true_false(const char *name
, struct symbol
*sym
,
302 struct smatch_state
*true_state
,
303 struct smatch_state
*false_state
)
306 struct symbol
*new_sym
;
307 struct relation
*rel
;
308 struct smatch_state
*orig_state
;
310 if (!true_state
&& !false_state
)
313 if (in_warn_on_macro())
316 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
317 if (new_name
&& new_sym
)
318 set_true_false_states(SMATCH_EXTRA
, new_name
, new_sym
, true_state
, false_state
);
319 free_string(new_name
);
321 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
323 if (!estate_related(orig_state
)) {
324 set_true_false_states(SMATCH_EXTRA
, name
, sym
, true_state
, false_state
);
329 set_related(true_state
, estate_related(orig_state
));
331 set_related(false_state
, estate_related(orig_state
));
333 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
334 set_true_false_states(SMATCH_EXTRA
, rel
->name
, rel
->sym
,
335 true_state
, false_state
);
336 } END_FOR_EACH_PTR(rel
);
339 static void set_extra_chunk_true_false(struct expression
*expr
,
340 struct smatch_state
*true_state
,
341 struct smatch_state
*false_state
)
343 struct var_sym_list
*vsl
;
349 if (in_warn_on_macro())
352 type
= get_type(expr
);
356 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
359 FOR_EACH_PTR(vsl
, vs
) {
360 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
361 } END_FOR_EACH_PTR(vs
);
363 set_true_false_states(SMATCH_EXTRA
, name
, sym
,
364 clone_estate(true_state
),
365 clone_estate(false_state
));
370 static void set_extra_expr_true_false(struct expression
*expr
,
371 struct smatch_state
*true_state
,
372 struct smatch_state
*false_state
)
378 if (!true_state
&& !false_state
)
381 if (get_value(expr
, &sval
))
384 expr
= strip_expr(expr
);
385 name
= expr_to_var_sym(expr
, &sym
);
388 set_extra_chunk_true_false(expr
, true_state
, false_state
);
391 set_extra_true_false(name
, sym
, true_state
, false_state
);
395 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
397 struct expression
*iter_var
;
398 struct expression
*condition
;
400 struct smatch_state
*estate
;
403 condition
= strip_expr(loop
->iterator_pre_condition
);
406 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
408 if (condition
->op
!= SPECIAL_DECREMENT
)
411 iter_var
= condition
->unop
;
412 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
415 if (sval_cmp_val(estate_min(sm
->state
), 0) < 0)
417 start
= estate_max(sm
->state
);
418 if (sval_cmp_val(start
, 0) <= 0)
420 if (!sval_is_max(start
))
423 if (condition
->type
== EXPR_PREOP
) {
424 estate
= alloc_estate_range(sval_type_val(start
.type
, 1), start
);
425 if (estate_has_hard_max(sm
->state
))
426 estate_set_hard_max(estate
);
427 estate_copy_fuzzy_max(estate
, sm
->state
);
428 set_extra_expr_mod(iter_var
, estate
);
430 if (condition
->type
== EXPR_POSTOP
) {
431 estate
= alloc_estate_range(sval_type_val(start
.type
, 0), start
);
432 if (estate_has_hard_max(sm
->state
))
433 estate_set_hard_max(estate
);
434 estate_copy_fuzzy_max(estate
, sm
->state
);
435 set_extra_expr_mod(iter_var
, estate
);
437 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
440 static struct sm_state
*handle_canonical_for_inc(struct expression
*iter_expr
,
441 struct expression
*condition
)
443 struct expression
*iter_var
;
445 struct smatch_state
*estate
;
446 sval_t start
, end
, max
;
448 iter_var
= iter_expr
->unop
;
449 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
452 if (!estate_get_single_value(sm
->state
, &start
))
454 if (get_implied_max(condition
->right
, &end
))
455 end
= sval_cast(get_type(iter_var
), end
);
457 end
= sval_type_max(get_type(iter_var
));
459 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
462 switch (condition
->op
) {
463 case SPECIAL_UNSIGNED_LT
:
464 case SPECIAL_NOTEQUAL
:
466 if (!sval_is_min(end
))
469 case SPECIAL_UNSIGNED_LTE
:
475 if (sval_cmp(end
, start
) < 0)
477 estate
= alloc_estate_range(start
, end
);
478 if (get_hard_max(condition
->right
, &max
)) {
479 estate_set_hard_max(estate
);
480 if (condition
->op
== '<' ||
481 condition
->op
== SPECIAL_UNSIGNED_LT
||
482 condition
->op
== SPECIAL_NOTEQUAL
)
484 estate_set_fuzzy_max(estate
, max
);
486 set_extra_expr_mod(iter_var
, estate
);
487 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
490 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
491 struct expression
*condition
)
493 struct expression
*iter_var
;
495 struct smatch_state
*estate
;
498 iter_var
= iter_expr
->unop
;
499 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
502 if (!estate_get_single_value(sm
->state
, &start
))
504 if (!get_implied_min(condition
->right
, &end
))
505 end
= sval_type_min(get_type(iter_var
));
506 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
509 switch (condition
->op
) {
510 case SPECIAL_NOTEQUAL
:
512 if (!sval_is_min(end
) && !sval_is_max(end
))
520 if (sval_cmp(end
, start
) > 0)
522 estate
= alloc_estate_range(end
, start
);
523 estate_set_hard_max(estate
);
524 estate_set_fuzzy_max(estate
, estate_get_fuzzy_max(estate
));
525 set_extra_expr_mod(iter_var
, estate
);
526 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
529 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
531 struct expression
*iter_expr
;
532 struct expression
*condition
;
534 if (!loop
->iterator_post_statement
)
536 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
538 iter_expr
= loop
->iterator_post_statement
->expression
;
539 if (!loop
->iterator_pre_condition
)
541 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
543 condition
= loop
->iterator_pre_condition
;
545 if (iter_expr
->op
== SPECIAL_INCREMENT
)
546 return handle_canonical_for_inc(iter_expr
, condition
);
547 if (iter_expr
->op
== SPECIAL_DECREMENT
)
548 return handle_canonical_for_dec(iter_expr
, condition
);
552 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct stree
**stree
)
554 struct sm_state
*ret
;
556 __push_fake_cur_stree();
557 if (!loop
->iterator_post_statement
)
558 ret
= handle_canonical_while_count_down(loop
);
560 ret
= handle_canonical_for_loops(loop
);
561 *stree
= __pop_fake_cur_stree();
565 int __iterator_unchanged(struct sm_state
*sm
)
569 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
574 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
578 /* paranoid checking. prolly not needed */
579 condition
= strip_expr(condition
);
582 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
584 if (condition
->op
!= SPECIAL_DECREMENT
)
586 after_value
= estate_min(sm
->state
);
588 set_extra_mod(sm
->name
, sm
->sym
, alloc_estate_sval(after_value
));
591 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
592 struct statement
*iterator
,
593 struct expression
*condition
)
595 struct expression
*iter_expr
;
597 struct smatch_state
*state
;
600 while_count_down_after(sm
, condition
);
604 iter_expr
= iterator
->expression
;
606 if (condition
->type
!= EXPR_COMPARE
)
608 if (iter_expr
->op
== SPECIAL_INCREMENT
) {
609 limit
= sval_binop(estate_max(sm
->state
), '+',
610 sval_type_val(estate_type(sm
->state
), 1));
612 limit
= sval_binop(estate_min(sm
->state
), '-',
613 sval_type_val(estate_type(sm
->state
), 1));
615 if (!estate_has_hard_max(sm
->state
) && !__has_breaks()) {
616 if (iter_expr
->op
== SPECIAL_INCREMENT
)
617 state
= alloc_estate_range(estate_min(sm
->state
), limit
);
619 state
= alloc_estate_range(limit
, estate_max(sm
->state
));
621 state
= alloc_estate_sval(limit
);
623 if (!estate_has_hard_max(sm
->state
)) {
624 estate_clear_hard_max(state
);
626 if (estate_has_fuzzy_max(sm
->state
)) {
627 sval_t hmax
= estate_get_fuzzy_max(sm
->state
);
628 sval_t max
= estate_max(sm
->state
);
630 if (sval_cmp(hmax
, max
) != 0)
631 estate_clear_fuzzy_max(state
);
632 } else if (!estate_has_fuzzy_max(sm
->state
)) {
633 estate_clear_fuzzy_max(state
);
636 set_extra_mod(sm
->name
, sm
->sym
, state
);
639 static struct stree
*unmatched_stree
;
640 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
642 struct smatch_state
*state
;
644 if (unmatched_stree
) {
645 state
= get_state_stree(unmatched_stree
, SMATCH_EXTRA
, sm
->name
, sm
->sym
);
649 if (parent_is_gone_var_sym(sm
->name
, sm
->sym
))
650 return alloc_estate_empty();
651 return alloc_estate_whole(estate_type(sm
->state
));
654 static void clear_the_pointed_at(struct expression
*expr
)
659 struct sm_state
*tmp
;
661 name
= expr_to_var_sym(expr
, &sym
);
665 stree
= __get_cur_stree();
666 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
667 if (tmp
->name
[0] != '*')
671 if (strcmp(tmp
->name
+ 1, name
) != 0)
673 set_extra_mod(tmp
->name
, tmp
->sym
, alloc_estate_whole(estate_type(tmp
->state
)));
674 } END_FOR_EACH_SM(tmp
);
680 static void match_function_call(struct expression
*expr
)
682 struct expression
*arg
;
683 struct expression
*tmp
;
685 /* if we have the db this is handled in smatch_function_hooks.c */
688 if (inlinable(expr
->fn
))
691 FOR_EACH_PTR(expr
->args
, arg
) {
692 tmp
= strip_expr(arg
);
693 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
694 set_extra_expr_mod(tmp
->unop
, alloc_estate_whole(get_type(tmp
->unop
)));
696 clear_the_pointed_at(tmp
);
697 } END_FOR_EACH_PTR(arg
);
700 static int values_fit_type(struct expression
*left
, struct expression
*right
)
702 struct range_list
*rl
;
705 type
= get_type(left
);
708 get_absolute_rl(right
, &rl
);
709 if (type_unsigned(type
) && sval_is_negative(rl_min(rl
)))
711 if (sval_cmp(sval_type_min(type
), rl_min(rl
)) > 0)
713 if (sval_cmp(sval_type_max(type
), rl_max(rl
)) < 0)
718 static void save_chunk_info(struct expression
*left
, struct expression
*right
)
720 struct var_sym_list
*vsl
;
722 struct expression
*add_expr
;
728 if (right
->type
!= EXPR_BINOP
|| right
->op
!= '-')
730 if (!get_value(right
->left
, &sval
))
732 if (!expr_to_sym(right
->right
))
735 add_expr
= binop_expression(left
, '+', right
->right
);
736 type
= get_type(add_expr
);
739 name
= expr_to_chunk_sym_vsl(add_expr
, &sym
, &vsl
);
742 FOR_EACH_PTR(vsl
, vs
) {
743 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
744 } END_FOR_EACH_PTR(vs
);
746 set_state(SMATCH_EXTRA
, name
, sym
, alloc_estate_sval(sval_cast(type
, sval
)));
751 static void do_array_assign(struct expression
*left
, int op
, struct expression
*right
)
753 struct range_list
*rl
;
756 get_absolute_rl(right
, &rl
);
757 rl
= cast_rl(get_type(left
), rl
);
759 rl
= alloc_whole_rl(get_type(left
));
762 set_extra_array_mod(left
, alloc_estate_rl(rl
));
765 static void match_untracked_array(struct expression
*call
, int param
)
767 struct expression
*arg
;
769 arg
= get_argument_from_call_expr(call
->args
, param
);
770 arg
= strip_expr(arg
);
772 clear_array_states(arg
);
775 static void match_vanilla_assign(struct expression
*left
, struct expression
*right
)
777 struct range_list
*orig_rl
= NULL
;
778 struct range_list
*rl
= NULL
;
779 struct symbol
*right_sym
;
780 struct symbol
*left_type
;
781 struct symbol
*right_type
;
782 char *right_name
= NULL
;
786 struct smatch_state
*state
;
792 save_chunk_info(left
, right
);
794 name
= expr_to_var_sym(left
, &sym
);
797 do_array_assign(left
, '=', right
);
801 left_type
= get_type(left
);
802 right_type
= get_type(right
);
804 right_name
= expr_to_var_sym(right
, &right_sym
);
806 if (!__in_fake_assign
&&
807 !(right
->type
== EXPR_PREOP
&& right
->op
== '&') &&
808 right_name
&& right_sym
&&
809 values_fit_type(left
, right
) &&
810 !has_symbol(right
, sym
)) {
811 set_equiv(left
, right
);
815 if (is_pointer(right
) && get_address_rl(right
, &rl
)) {
816 state
= alloc_estate_rl(rl
);
820 if (__in_fake_assign
) {
821 struct smatch_state
*right_state
;
824 if (get_value(right
, &sval
)) {
825 sval
= sval_cast(left_type
, sval
);
826 state
= alloc_estate_sval(sval
);
830 right_state
= get_state(SMATCH_EXTRA
, right_name
, right_sym
);
832 /* simple assignment */
833 state
= clone_estate(right_state
);
837 state
= alloc_estate_rl(alloc_whole_rl(left_type
));
841 comparison
= get_comparison(left
, right
);
843 comparison
= flip_comparison(comparison
);
844 get_implied_rl(left
, &orig_rl
);
847 if (get_implied_rl(right
, &rl
)) {
848 rl
= cast_rl(left_type
, rl
);
850 filter_by_comparison(&rl
, comparison
, orig_rl
);
851 state
= alloc_estate_rl(rl
);
852 if (get_hard_max(right
, &max
)) {
853 estate_set_hard_max(state
);
854 estate_set_fuzzy_max(state
, max
);
857 rl
= alloc_whole_rl(right_type
);
858 rl
= cast_rl(left_type
, rl
);
860 filter_by_comparison(&rl
, comparison
, orig_rl
);
861 state
= alloc_estate_rl(rl
);
865 set_extra_mod(name
, sym
, state
);
867 free_string(right_name
);
870 static int op_remove_assign(int op
)
873 case SPECIAL_ADD_ASSIGN
:
875 case SPECIAL_SUB_ASSIGN
:
877 case SPECIAL_MUL_ASSIGN
:
879 case SPECIAL_DIV_ASSIGN
:
881 case SPECIAL_MOD_ASSIGN
:
883 case SPECIAL_AND_ASSIGN
:
885 case SPECIAL_OR_ASSIGN
:
887 case SPECIAL_XOR_ASSIGN
:
889 case SPECIAL_SHL_ASSIGN
:
890 return SPECIAL_LEFTSHIFT
;
891 case SPECIAL_SHR_ASSIGN
:
892 return SPECIAL_RIGHTSHIFT
;
898 static void match_assign(struct expression
*expr
)
900 struct range_list
*rl
= NULL
;
901 struct expression
*left
;
902 struct expression
*right
;
903 struct expression
*binop_expr
;
904 struct symbol
*left_type
;
907 sval_t left_min
, left_max
;
908 sval_t right_min
, right_max
;
909 sval_t res_min
, res_max
;
911 left
= strip_expr(expr
->left
);
913 right
= strip_parens(expr
->right
);
914 if (right
->type
== EXPR_CALL
&& sym_name_is("__builtin_expect", right
->fn
))
915 right
= get_argument_from_call_expr(right
->args
, 0);
916 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
917 right
= strip_parens(right
->left
);
919 if (expr
->op
== '=' && is_condition(expr
->right
))
920 return; /* handled in smatch_condition.c */
921 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
922 return; /* handled in smatch_function_hooks.c */
923 if (expr
->op
== '=') {
924 match_vanilla_assign(left
, right
);
928 name
= expr_to_var_sym(left
, &sym
);
932 left_type
= get_type(left
);
934 res_min
= sval_type_min(left_type
);
935 res_max
= sval_type_max(left_type
);
938 case SPECIAL_ADD_ASSIGN
:
939 get_absolute_max(left
, &left_max
);
940 get_absolute_max(right
, &right_max
);
941 if (sval_binop_overflows(left_max
, '+', right_max
))
943 if (get_implied_min(left
, &left_min
) &&
944 !sval_is_negative_min(left_min
) &&
945 get_implied_min(right
, &right_min
) &&
946 !sval_is_negative_min(right_min
)) {
947 res_min
= sval_binop(left_min
, '+', right_min
);
948 res_min
= sval_cast(left_type
, res_min
);
950 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
952 res_max
= sval_binop(left_max
, '+', right_max
);
953 res_max
= sval_cast(left_type
, res_max
);
955 case SPECIAL_SUB_ASSIGN
:
956 if (get_implied_max(left
, &left_max
) &&
957 !sval_is_max(left_max
) &&
958 get_implied_min(right
, &right_min
) &&
959 !sval_is_min(right_min
)) {
960 res_max
= sval_binop(left_max
, '-', right_min
);
961 res_max
= sval_cast(left_type
, res_max
);
965 if (get_implied_min(left
, &left_min
) &&
966 !sval_is_min(left_min
) &&
967 get_implied_max(right
, &right_max
) &&
968 !sval_is_max(right_max
) &&
969 sval_cmp(left_min
, right_max
) > 0) {
970 res_min
= sval_binop(left_min
, '-', right_max
);
971 res_min
= sval_cast(left_type
, res_min
);
974 case SPECIAL_AND_ASSIGN
:
975 case SPECIAL_MOD_ASSIGN
:
976 case SPECIAL_SHL_ASSIGN
:
977 case SPECIAL_SHR_ASSIGN
:
978 case SPECIAL_OR_ASSIGN
:
979 case SPECIAL_XOR_ASSIGN
:
980 case SPECIAL_MUL_ASSIGN
:
981 case SPECIAL_DIV_ASSIGN
:
982 binop_expr
= binop_expression(expr
->left
,
983 op_remove_assign(expr
->op
),
985 if (get_absolute_rl(binop_expr
, &rl
)) {
986 rl
= cast_rl(left_type
, rl
);
987 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
992 rl
= cast_rl(left_type
, alloc_rl(res_min
, res_max
));
993 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
998 static struct smatch_state
*increment_state(struct smatch_state
*state
)
1000 sval_t min
= estate_min(state
);
1001 sval_t max
= estate_max(state
);
1003 if (!estate_rl(state
))
1007 max
= sval_type_max(max
.type
);
1009 if (!sval_is_min(min
) && !sval_is_max(min
))
1011 if (!sval_is_min(max
) && !sval_is_max(max
))
1013 return alloc_estate_range(min
, max
);
1016 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
1018 sval_t min
= estate_min(state
);
1019 sval_t max
= estate_max(state
);
1021 if (!estate_rl(state
))
1025 min
= sval_type_min(min
.type
);
1027 if (!sval_is_min(min
) && !sval_is_max(min
))
1029 if (!sval_is_min(max
) && !sval_is_max(max
))
1031 return alloc_estate_range(min
, max
);
1034 static void clear_pointed_at_state(struct expression
*expr
)
1036 struct symbol
*type
;
1039 * ALERT: This is sort of a mess. If it's is a struct assigment like
1040 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1041 * the same thing for p++ where "p" is a struct. Most modifications
1042 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1043 * use smatch_modification.c because we have to get the ordering right
1044 * or something. So if you have p++ where p is a pointer to a standard
1045 * c type then we handle that here. What a mess.
1048 type
= get_type(expr
);
1049 if (!type
|| type
->type
!= SYM_PTR
)
1051 type
= get_real_base_type(type
);
1052 if (!type
|| type
->type
!= SYM_BASETYPE
)
1054 set_extra_expr_mod(deref_expression(expr
), alloc_estate_whole(type
));
1057 static void unop_expr(struct expression
*expr
)
1059 struct smatch_state
*state
;
1061 if (expr
->smatch_flags
& Handled
)
1065 case SPECIAL_INCREMENT
:
1066 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
1067 state
= increment_state(state
);
1069 state
= alloc_estate_whole(get_type(expr
));
1070 set_extra_expr_mod(expr
->unop
, state
);
1071 clear_pointed_at_state(expr
->unop
);
1073 case SPECIAL_DECREMENT
:
1074 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
1075 state
= decrement_state(state
);
1077 state
= alloc_estate_whole(get_type(expr
));
1078 set_extra_expr_mod(expr
->unop
, state
);
1079 clear_pointed_at_state(expr
->unop
);
1086 static void asm_expr(struct statement
*stmt
)
1089 struct expression
*expr
;
1090 struct symbol
*type
;
1093 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
1095 case 0: /* identifier */
1096 case 1: /* constraint */
1099 case 2: /* expression */
1101 type
= get_type(strip_expr(expr
));
1102 set_extra_expr_mod(expr
, alloc_estate_whole(type
));
1105 } END_FOR_EACH_PTR(expr
);
1108 static void check_dereference(struct expression
*expr
)
1110 if (__in_fake_assign
)
1112 if (outside_of_function())
1114 if (implied_not_equal(expr
, 0))
1116 set_extra_expr_nomod(expr
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
1119 static void match_dereferences(struct expression
*expr
)
1121 if (expr
->type
!= EXPR_PREOP
)
1123 /* it's saying that foo[1] = bar dereferences foo[1] */
1126 check_dereference(expr
->unop
);
1129 static void match_pointer_as_array(struct expression
*expr
)
1131 if (!is_array(expr
))
1133 check_dereference(get_array_base(expr
));
1136 static void find_dereferences(struct expression
*expr
)
1138 while (expr
->type
== EXPR_PREOP
) {
1139 if (expr
->op
== '*')
1140 check_dereference(expr
->unop
);
1141 expr
= strip_expr(expr
->unop
);
1145 static void set_param_dereferenced(struct expression
*arg
, char *key
, char *unused
)
1150 name
= get_variable_from_key(arg
, key
, &sym
);
1152 set_extra_nomod(name
, sym
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
1155 find_dereferences(arg
);
1158 static sval_t
add_one(sval_t sval
)
1164 static int handle_postop_inc(struct expression
*left
, int op
, struct expression
*right
)
1166 struct statement
*stmt
;
1167 struct expression
*cond
;
1168 struct smatch_state
*true_state
, *false_state
;
1173 * If we're decrementing here then that's a canonical while count down
1174 * so it's handled already. We're only handling loops like:
1176 * do { ... } while (i++ < 3);
1179 if (left
->type
!= EXPR_POSTOP
|| left
->op
!= SPECIAL_INCREMENT
)
1182 stmt
= __cur_stmt
->parent
;
1185 if (stmt
->type
== STMT_COMPOUND
)
1186 stmt
= stmt
->parent
;
1187 if (!stmt
|| stmt
->type
!= STMT_ITERATOR
|| !stmt
->iterator_post_condition
)
1190 cond
= strip_expr(stmt
->iterator_post_condition
);
1191 if (cond
->type
!= EXPR_COMPARE
|| cond
->op
!= op
)
1193 if (left
!= strip_expr(cond
->left
) || right
!= strip_expr(cond
->right
))
1196 if (!get_implied_value(left
->unop
, &start
))
1198 if (!get_implied_value(right
, &limit
))
1201 if (sval_cmp(start
, limit
) > 0)
1206 case SPECIAL_UNSIGNED_LT
:
1209 case SPECIAL_UNSIGNED_LTE
:
1210 limit
= add_one(limit
);
1216 true_state
= alloc_estate_range(add_one(start
), limit
);
1217 false_state
= alloc_estate_range(add_one(limit
), add_one(limit
));
1219 /* Currently we just discard the false state but when two passes is
1220 * implimented correctly then it will use it.
1223 set_extra_expr_true_false(left
->unop
, true_state
, false_state
);
1228 static void handle_comparison(struct symbol
*type
, struct expression
*left
, int op
, struct expression
*right
)
1230 struct range_list
*left_orig
;
1231 struct range_list
*left_true
;
1232 struct range_list
*left_false
;
1233 struct range_list
*right_orig
;
1234 struct range_list
*right_true
;
1235 struct range_list
*right_false
;
1236 struct smatch_state
*left_true_state
;
1237 struct smatch_state
*left_false_state
;
1238 struct smatch_state
*right_true_state
;
1239 struct smatch_state
*right_false_state
;
1240 sval_t dummy
, hard_max
;
1241 int left_postop
= 0;
1242 int right_postop
= 0;
1244 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
1245 if (left
->type
== EXPR_POSTOP
) {
1246 left
->smatch_flags
|= Handled
;
1247 left_postop
= left
->op
;
1248 if (handle_postop_inc(left
, op
, right
))
1251 left
= strip_parens(left
->unop
);
1253 while (left
->type
== EXPR_ASSIGNMENT
)
1254 left
= strip_parens(left
->left
);
1256 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
1257 if (right
->type
== EXPR_POSTOP
) {
1258 right
->smatch_flags
|= Handled
;
1259 right_postop
= right
->op
;
1261 right
= strip_parens(right
->unop
);
1264 get_real_absolute_rl(left
, &left_orig
);
1265 left_orig
= cast_rl(type
, left_orig
);
1267 get_real_absolute_rl(right
, &right_orig
);
1268 right_orig
= cast_rl(type
, right_orig
);
1270 split_comparison_rl(left_orig
, op
, right_orig
, &left_true
, &left_false
, &right_true
, &right_false
);
1272 left_true
= rl_truncate_cast(get_type(strip_expr(left
)), left_true
);
1273 left_false
= rl_truncate_cast(get_type(strip_expr(left
)), left_false
);
1274 right_true
= rl_truncate_cast(get_type(strip_expr(right
)), right_true
);
1275 right_false
= rl_truncate_cast(get_type(strip_expr(right
)), right_false
);
1277 if (!left_true
|| !left_false
) {
1278 struct range_list
*tmp_true
, *tmp_false
;
1280 split_comparison_rl(alloc_whole_rl(type
), op
, right_orig
, &tmp_true
, &tmp_false
, NULL
, NULL
);
1281 tmp_true
= rl_truncate_cast(get_type(strip_expr(left
)), tmp_true
);
1282 tmp_false
= rl_truncate_cast(get_type(strip_expr(left
)), tmp_false
);
1283 if (tmp_true
&& tmp_false
)
1284 __save_imaginary_state(left
, tmp_true
, tmp_false
);
1287 if (!right_true
|| !right_false
) {
1288 struct range_list
*tmp_true
, *tmp_false
;
1290 split_comparison_rl(alloc_whole_rl(type
), op
, right_orig
, NULL
, NULL
, &tmp_true
, &tmp_false
);
1291 tmp_true
= rl_truncate_cast(get_type(strip_expr(right
)), tmp_true
);
1292 tmp_false
= rl_truncate_cast(get_type(strip_expr(right
)), tmp_false
);
1293 if (tmp_true
&& tmp_false
)
1294 __save_imaginary_state(right
, tmp_true
, tmp_false
);
1297 left_true_state
= alloc_estate_rl(left_true
);
1298 left_false_state
= alloc_estate_rl(left_false
);
1299 right_true_state
= alloc_estate_rl(right_true
);
1300 right_false_state
= alloc_estate_rl(right_false
);
1304 case SPECIAL_UNSIGNED_LT
:
1305 case SPECIAL_UNSIGNED_LTE
:
1307 if (get_hard_max(right
, &dummy
))
1308 estate_set_hard_max(left_true_state
);
1309 if (get_hard_max(left
, &dummy
))
1310 estate_set_hard_max(right_false_state
);
1313 case SPECIAL_UNSIGNED_GT
:
1314 case SPECIAL_UNSIGNED_GTE
:
1316 if (get_hard_max(left
, &dummy
))
1317 estate_set_hard_max(right_true_state
);
1318 if (get_hard_max(right
, &dummy
))
1319 estate_set_hard_max(left_false_state
);
1325 case SPECIAL_UNSIGNED_LT
:
1326 case SPECIAL_UNSIGNED_LTE
:
1328 if (get_hard_max(right
, &hard_max
)) {
1329 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1331 estate_set_fuzzy_max(left_true_state
, hard_max
);
1333 if (get_implied_value(right
, &hard_max
)) {
1334 if (op
== SPECIAL_UNSIGNED_LTE
||
1337 estate_set_fuzzy_max(left_false_state
, hard_max
);
1339 if (get_hard_max(left
, &hard_max
)) {
1340 if (op
== SPECIAL_UNSIGNED_LTE
||
1343 estate_set_fuzzy_max(right_false_state
, hard_max
);
1345 if (get_implied_value(left
, &hard_max
)) {
1346 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1348 estate_set_fuzzy_max(right_true_state
, hard_max
);
1352 case SPECIAL_UNSIGNED_GT
:
1353 case SPECIAL_UNSIGNED_GTE
:
1355 if (get_hard_max(left
, &hard_max
)) {
1356 if (op
== '>' || op
== SPECIAL_UNSIGNED_GT
)
1358 estate_set_fuzzy_max(right_true_state
, hard_max
);
1360 if (get_implied_value(left
, &hard_max
)) {
1361 if (op
== SPECIAL_UNSIGNED_GTE
||
1364 estate_set_fuzzy_max(right_false_state
, hard_max
);
1366 if (get_hard_max(right
, &hard_max
)) {
1367 if (op
== SPECIAL_UNSIGNED_LTE
||
1370 estate_set_fuzzy_max(left_false_state
, hard_max
);
1372 if (get_implied_value(right
, &hard_max
)) {
1374 op
== SPECIAL_UNSIGNED_GT
)
1376 estate_set_fuzzy_max(left_true_state
, hard_max
);
1380 if (get_hard_max(left
, &hard_max
))
1381 estate_set_fuzzy_max(right_true_state
, hard_max
);
1382 if (get_hard_max(right
, &hard_max
))
1383 estate_set_fuzzy_max(left_true_state
, hard_max
);
1387 if (get_hard_max(left
, &hard_max
)) {
1388 estate_set_hard_max(left_true_state
);
1389 estate_set_hard_max(left_false_state
);
1391 if (get_hard_max(right
, &hard_max
)) {
1392 estate_set_hard_max(right_true_state
);
1393 estate_set_hard_max(right_false_state
);
1396 if (left_postop
== SPECIAL_INCREMENT
) {
1397 left_true_state
= increment_state(left_true_state
);
1398 left_false_state
= increment_state(left_false_state
);
1400 if (left_postop
== SPECIAL_DECREMENT
) {
1401 left_true_state
= decrement_state(left_true_state
);
1402 left_false_state
= decrement_state(left_false_state
);
1404 if (right_postop
== SPECIAL_INCREMENT
) {
1405 right_true_state
= increment_state(right_true_state
);
1406 right_false_state
= increment_state(right_false_state
);
1408 if (right_postop
== SPECIAL_DECREMENT
) {
1409 right_true_state
= decrement_state(right_true_state
);
1410 right_false_state
= decrement_state(right_false_state
);
1413 if (estate_rl(left_true_state
) && estates_equiv(left_true_state
, left_false_state
)) {
1414 left_true_state
= NULL
;
1415 left_false_state
= NULL
;
1418 if (estate_rl(right_true_state
) && estates_equiv(right_true_state
, right_false_state
)) {
1419 right_true_state
= NULL
;
1420 right_false_state
= NULL
;
1423 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
1424 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
1427 static int is_simple_math(struct expression
*expr
)
1431 if (expr
->type
!= EXPR_BINOP
)
1442 static void move_known_values(struct expression
**left_p
, struct expression
**right_p
)
1444 struct expression
*left
= *left_p
;
1445 struct expression
*right
= *right_p
;
1448 if (get_implied_value(left
, &sval
)) {
1449 if (!is_simple_math(right
))
1451 if (get_implied_value(right
, &dummy
))
1453 if (right
->op
== '*') {
1456 if (!get_value(right
->right
, &divisor
))
1458 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1460 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1461 *right_p
= right
->left
;
1464 if (right
->op
== '+' && get_value(right
->left
, &sval
)) {
1465 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->left
);
1466 *right_p
= right
->right
;
1469 if (get_value(right
->right
, &sval
)) {
1470 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1471 *right_p
= right
->left
;
1476 if (get_implied_value(right
, &sval
)) {
1477 if (!is_simple_math(left
))
1479 if (get_implied_value(left
, &dummy
))
1481 if (left
->op
== '*') {
1484 if (!get_value(left
->right
, &divisor
))
1486 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1488 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1489 *left_p
= left
->left
;
1492 if (left
->op
== '+' && get_value(left
->left
, &sval
)) {
1493 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->left
);
1494 *left_p
= left
->right
;
1498 if (get_value(left
->right
, &sval
)) {
1499 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1500 *left_p
= left
->left
;
1507 static int match_func_comparison(struct expression
*expr
)
1509 struct expression
*left
= strip_expr(expr
->left
);
1510 struct expression
*right
= strip_expr(expr
->right
);
1514 * fixme: think about this harder. We should always be trying to limit
1515 * the non-call side as well. If we can't determine the limitter does
1516 * that mean we aren't querying the database and are missing important
1520 if (left
->type
== EXPR_CALL
) {
1521 if (get_implied_value(left
, &sval
)) {
1522 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1525 function_comparison(left
, expr
->op
, right
);
1529 if (right
->type
== EXPR_CALL
) {
1530 if (get_implied_value(right
, &sval
)) {
1531 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1534 function_comparison(left
, expr
->op
, right
);
1541 /* Handle conditions like "if (foo + bar < foo) {" */
1542 static int handle_integer_overflow_test(struct expression
*expr
)
1544 struct expression
*left
, *right
;
1545 struct symbol
*type
;
1546 sval_t left_min
, right_min
, min
, max
;
1548 if (expr
->op
!= '<' && expr
->op
!= SPECIAL_UNSIGNED_LT
)
1551 left
= strip_parens(expr
->left
);
1552 right
= strip_parens(expr
->right
);
1554 if (left
->op
!= '+')
1557 type
= get_type(expr
);
1560 if (type_positive_bits(type
) == 32) {
1561 max
.type
= &uint_ctype
;
1562 max
.uvalue
= (unsigned int)-1;
1563 } else if (type_positive_bits(type
) == 64) {
1564 max
.type
= &ulong_ctype
;
1565 max
.value
= (unsigned long long)-1;
1570 if (!expr_equiv(left
->left
, right
) && !expr_equiv(left
->right
, right
))
1573 get_absolute_min(left
->left
, &left_min
);
1574 get_absolute_min(left
->right
, &right_min
);
1575 min
= sval_binop(left_min
, '+', right_min
);
1577 set_extra_chunk_true_false(left
, NULL
, alloc_estate_range(min
, max
));
1581 static void match_comparison(struct expression
*expr
)
1583 struct expression
*left_orig
= strip_parens(expr
->left
);
1584 struct expression
*right_orig
= strip_parens(expr
->right
);
1585 struct expression
*left
, *right
;
1586 struct expression
*prev
;
1587 struct symbol
*type
;
1589 if (match_func_comparison(expr
))
1592 type
= get_type(expr
);
1594 type
= &llong_ctype
;
1596 if (handle_integer_overflow_test(expr
))
1601 move_known_values(&left
, &right
);
1602 handle_comparison(type
, left
, expr
->op
, right
);
1604 prev
= get_assigned_expr(left_orig
);
1605 if (is_simple_math(prev
) && has_variable(prev
, left_orig
) == 0) {
1608 move_known_values(&left
, &right
);
1609 handle_comparison(type
, left
, expr
->op
, right
);
1612 prev
= get_assigned_expr(right_orig
);
1613 if (is_simple_math(prev
) && has_variable(prev
, right_orig
) == 0) {
1616 move_known_values(&left
, &right
);
1617 handle_comparison(type
, left
, expr
->op
, right
);
1621 static sval_t
get_high_mask(sval_t known
)
1629 for (i
= type_bits(known
.type
) - 1; i
>= 0; i
--) {
1630 if (known
.uvalue
& (1ULL << i
))
1631 ret
.uvalue
|= (1ULL << i
);
1639 static void handle_AND_condition(struct expression
*expr
)
1641 struct range_list
*orig_rl
;
1642 struct range_list
*true_rl
= NULL
;
1643 struct range_list
*false_rl
= NULL
;
1647 if (get_implied_value(expr
->left
, &known
)) {
1648 sval_t low_mask
= known
;
1651 if (known
.value
> 0) {
1652 bit
= ffsll(known
.value
) - 1;
1653 low_mask
.uvalue
= (1ULL << bit
) - 1;
1654 get_absolute_rl(expr
->right
, &orig_rl
);
1655 true_rl
= remove_range(orig_rl
, sval_type_val(known
.type
, 0), low_mask
);
1657 high_mask
= get_high_mask(known
);
1658 if (high_mask
.value
) {
1659 bit
= ffsll(high_mask
.value
) - 1;
1660 low_mask
.uvalue
= (1ULL << bit
) - 1;
1662 get_absolute_rl(expr
->left
, &orig_rl
);
1663 if (sval_is_negative(rl_min(orig_rl
)))
1664 orig_rl
= remove_range(orig_rl
, sval_type_min(known
.type
), sval_type_val(known
.type
, -1));
1665 false_rl
= remove_range(orig_rl
, low_mask
, sval_type_max(known
.type
));
1666 if (type_signed(high_mask
.type
) && type_unsigned(rl_type(false_rl
))) {
1667 false_rl
= remove_range(false_rl
,
1668 sval_type_val(rl_type(false_rl
), sval_type_max(known
.type
).uvalue
),
1669 sval_type_val(rl_type(false_rl
), -1));
1672 set_extra_expr_true_false(expr
->right
,
1673 true_rl
? alloc_estate_rl(true_rl
) : NULL
,
1674 false_rl
? alloc_estate_rl(false_rl
) : NULL
);
1679 if (get_implied_value(expr
->right
, &known
)) {
1680 sval_t low_mask
= known
;
1683 if (known
.value
> 0) {
1684 bit
= ffsll(known
.value
) - 1;
1685 low_mask
.uvalue
= (1ULL << bit
) - 1;
1686 get_absolute_rl(expr
->left
, &orig_rl
);
1687 true_rl
= remove_range(orig_rl
, sval_type_val(known
.type
, 0), low_mask
);
1689 high_mask
= get_high_mask(known
);
1690 if (high_mask
.value
) {
1691 bit
= ffsll(high_mask
.value
) - 1;
1692 low_mask
.uvalue
= (1ULL << bit
) - 1;
1694 get_absolute_rl(expr
->left
, &orig_rl
);
1695 if (sval_is_negative(rl_min(orig_rl
)))
1696 orig_rl
= remove_range(orig_rl
, sval_type_min(known
.type
), sval_type_val(known
.type
, -1));
1697 false_rl
= remove_range(orig_rl
, low_mask
, sval_type_max(known
.type
));
1698 if (type_signed(high_mask
.type
) && type_unsigned(rl_type(false_rl
))) {
1699 false_rl
= remove_range(false_rl
,
1700 sval_type_val(rl_type(false_rl
), sval_type_max(known
.type
).uvalue
),
1701 sval_type_val(rl_type(false_rl
), -1));
1704 set_extra_expr_true_false(expr
->left
,
1705 true_rl
? alloc_estate_rl(true_rl
) : NULL
,
1706 false_rl
? alloc_estate_rl(false_rl
) : NULL
);
1711 static void handle_MOD_condition(struct expression
*expr
)
1713 struct range_list
*orig_rl
;
1714 struct range_list
*true_rl
;
1715 struct range_list
*false_rl
= NULL
;
1721 if (!get_implied_value(expr
->right
, &right
) || right
.value
== 0)
1723 get_absolute_rl(expr
->left
, &orig_rl
);
1725 zero
.type
= rl_type(orig_rl
);
1727 /* We're basically dorking around the min and max here */
1728 true_rl
= remove_range(orig_rl
, zero
, zero
);
1729 if (!sval_is_max(rl_max(true_rl
)) &&
1730 !(rl_max(true_rl
).value
% right
.value
))
1731 true_rl
= remove_range(true_rl
, rl_max(true_rl
), rl_max(true_rl
));
1733 if (rl_equiv(true_rl
, orig_rl
))
1736 if (sval_is_positive(rl_min(orig_rl
)) &&
1737 (rl_max(orig_rl
).value
- rl_min(orig_rl
).value
) / right
.value
< 5) {
1741 add
= rl_min(orig_rl
);
1742 add
.value
+= right
.value
- (add
.value
% right
.value
);
1743 add
.value
-= right
.value
;
1745 for (i
= 0; i
< 5; i
++) {
1746 add
.value
+= right
.value
;
1747 if (add
.value
> rl_max(orig_rl
).value
)
1749 add_range(&false_rl
, add
, add
);
1752 if (rl_min(orig_rl
).uvalue
!= 0 &&
1753 rl_min(orig_rl
).uvalue
< right
.uvalue
) {
1754 sval_t chop
= right
;
1756 false_rl
= remove_range(orig_rl
, zero
, chop
);
1759 if (!sval_is_max(rl_max(orig_rl
)) &&
1760 (rl_max(orig_rl
).value
% right
.value
)) {
1761 sval_t chop
= rl_max(orig_rl
);
1762 chop
.value
-= chop
.value
% right
.value
;
1765 false_rl
= clone_rl(orig_rl
);
1766 false_rl
= remove_range(false_rl
, chop
, rl_max(orig_rl
));
1770 set_extra_expr_true_false(expr
->left
,
1771 true_rl
? alloc_estate_rl(true_rl
) : NULL
,
1772 false_rl
? alloc_estate_rl(false_rl
) : NULL
);
1775 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1776 void __extra_match_condition(struct expression
*expr
)
1778 struct smatch_state
*pre_state
;
1779 struct smatch_state
*true_state
;
1780 struct smatch_state
*false_state
;
1782 expr
= strip_expr(expr
);
1783 switch (expr
->type
) {
1785 function_comparison(expr
, SPECIAL_NOTEQUAL
, zero_expr());
1792 zero
= sval_blank(expr
);
1795 pre_state
= get_extra_state(expr
);
1796 true_state
= estate_filter_sval(pre_state
, zero
);
1797 if (possibly_true(expr
, SPECIAL_EQUAL
, zero_expr()))
1798 false_state
= alloc_estate_sval(zero
);
1800 false_state
= alloc_estate_empty();
1801 set_extra_expr_true_false(expr
, true_state
, false_state
);
1805 match_comparison(expr
);
1807 case EXPR_ASSIGNMENT
:
1808 __extra_match_condition(expr
->left
);
1811 if (expr
->op
== '&')
1812 handle_AND_condition(expr
);
1813 if (expr
->op
== '%')
1814 handle_MOD_condition(expr
);
1819 static void assume_indexes_are_valid(struct expression
*expr
)
1821 struct expression
*array_expr
;
1823 struct expression
*offset
;
1824 struct symbol
*offset_type
;
1825 struct range_list
*rl_before
;
1826 struct range_list
*rl_after
;
1827 struct range_list
*filter
= NULL
;
1830 expr
= strip_expr(expr
);
1831 if (!is_array(expr
))
1834 offset
= get_array_offset(expr
);
1835 offset_type
= get_type(offset
);
1836 if (offset_type
&& type_signed(offset_type
)) {
1837 filter
= alloc_rl(sval_type_min(offset_type
),
1838 sval_type_val(offset_type
, -1));
1841 array_expr
= get_array_base(expr
);
1842 array_size
= get_real_array_size(array_expr
);
1843 if (array_size
> 1) {
1844 size
= sval_type_val(offset_type
, array_size
);
1845 add_range(&filter
, size
, sval_type_max(offset_type
));
1850 get_absolute_rl(offset
, &rl_before
);
1851 rl_after
= rl_filter(rl_before
, filter
);
1852 if (rl_equiv(rl_before
, rl_after
))
1854 set_extra_expr_nomod(offset
, alloc_estate_rl(rl_after
));
1857 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1858 int implied_not_equal(struct expression
*expr
, long long val
)
1860 return !possibly_false(expr
, SPECIAL_NOTEQUAL
, value_expr(val
));
1863 int implied_not_equal_name_sym(char *name
, struct symbol
*sym
, long long val
)
1865 struct smatch_state
*estate
;
1867 estate
= get_state(SMATCH_EXTRA
, name
, sym
);
1870 if (!rl_has_sval(estate_rl(estate
), sval_type_val(estate_type(estate
), 0)))
1875 int parent_is_null_var_sym(const char *name
, struct symbol
*sym
)
1880 struct smatch_state
*state
;
1882 strncpy(buf
, name
, sizeof(buf
) - 1);
1883 buf
[sizeof(buf
) - 1] = '\0';
1886 while (*start
== '*') {
1888 state
= get_state(SMATCH_EXTRA
, start
, sym
);
1891 if (!estate_rl(state
))
1893 if (estate_min(state
).value
== 0 &&
1894 estate_max(state
).value
== 0)
1899 while (*start
== '&')
1902 while ((end
= strrchr(start
, '-'))) {
1904 state
= get_state(SMATCH_EXTRA
, start
, sym
);
1907 if (estate_min(state
).value
== 0 &&
1908 estate_max(state
).value
== 0)
1914 int parent_is_null(struct expression
*expr
)
1920 expr
= strip_expr(expr
);
1921 var
= expr_to_var_sym(expr
, &sym
);
1924 ret
= parent_is_null_var_sym(var
, sym
);
1930 static int param_used_callback(void *found
, int argc
, char **argv
, char **azColName
)
1936 static int filter_unused_kzalloc_info(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
1941 /* for function pointers assume everything is used */
1942 if (call
->fn
->type
!= EXPR_SYMBOL
)
1946 * kzalloc() information is treated as special because so there is just
1947 * a lot of stuff initialized to zero and it makes building the database
1948 * take hours and hours.
1950 * In theory, we should just remove this line and not pass any unused
1951 * information, but I'm not sure enough that this code works so I want
1952 * to hold off on that for now.
1954 if (!estate_get_single_value(sm
->state
, &sval
) || sval
.value
!= 0)
1957 run_sql(¶m_used_callback
, &found
,
1958 "select * from call_implies where %s and type = %d and parameter = %d and key = '%s';",
1959 get_static_filter(call
->fn
->symbol
), PARAM_USED
, param
, printed_name
);
1963 /* If the database is not built yet, then assume everything is used */
1964 run_sql(¶m_used_callback
, &found
,
1965 "select * from call_implies where %s and type = %d;",
1966 get_static_filter(call
->fn
->symbol
), PARAM_USED
);
1973 struct range_list
*intersect_with_real_abs_var_sym(const char *name
, struct symbol
*sym
, struct range_list
*start
)
1975 struct smatch_state
*state
;
1978 * Here is the difference between implied value and real absolute, say
1983 * Then you know that a is 0-255. That's real absolute. But you don't
1984 * know for sure that it actually goes up to 255. So it's not implied.
1985 * Implied indicates a degree of certainty.
1987 * But then say you cap "a" at 8. That means you know it goes up to
1988 * 8. So now the implied value is s32min-8. But you can combine it
1989 * with the real absolute to say that actually it's 0-8.
1991 * We are combining it here. But now that I think about it, this is
1992 * probably not the ideal place to combine it because it should proably
1993 * be done earlier. Oh well, this is an improvement on what was there
1994 * before so I'm going to commit this code.
1998 state
= get_real_absolute_state_var_sym(name
, sym
);
1999 if (!state
|| !estate_rl(state
))
2002 return rl_intersection(estate_rl(state
), start
);
2005 struct range_list
*intersect_with_real_abs_expr(struct expression
*expr
, struct range_list
*start
)
2007 struct smatch_state
*state
;
2009 state
= get_real_absolute_state(expr
);
2010 if (!state
|| !estate_rl(state
))
2013 return rl_intersection(estate_rl(state
), start
);
2016 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
2018 struct range_list
*rl
;
2020 if (estate_is_whole(sm
->state
))
2022 if (filter_unused_kzalloc_info(call
, param
, printed_name
, sm
))
2024 rl
= estate_rl(sm
->state
);
2025 rl
= intersect_with_real_abs_var_sym(sm
->name
, sm
->sym
, rl
);
2026 sql_insert_caller_info(call
, PARAM_VALUE
, param
, printed_name
, show_rl(rl
));
2027 if (estate_has_fuzzy_max(sm
->state
))
2028 sql_insert_caller_info(call
, FUZZY_MAX
, param
, printed_name
,
2029 sval_to_str(estate_get_fuzzy_max(sm
->state
)));
2032 static void returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
2034 struct symbol
*returned_sym
;
2035 struct sm_state
*sm
;
2036 const char *param_name
;
2040 returned_sym
= expr_to_sym(expr
);
2044 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), sm
) {
2045 if (!estate_rl(sm
->state
))
2047 if (returned_sym
!= sm
->sym
)
2050 param_name
= get_param_name(sm
);
2053 if (strcmp(param_name
, "$") == 0)
2055 compare_str
= name_sym_to_param_comparison(sm
->name
, sm
->sym
);
2056 if (!compare_str
&& estate_is_whole(sm
->state
))
2058 snprintf(buf
, sizeof(buf
), "%s%s", sm
->state
->name
, compare_str
?: "");
2060 sql_insert_return_states(return_id
, return_ranges
, PARAM_VALUE
,
2061 -1, param_name
, buf
);
2062 } END_FOR_EACH_SM(sm
);
2065 static void db_limited_before(void)
2067 unmatched_stree
= clone_stree(__get_cur_stree());
2070 static void db_limited_after(void)
2072 free_stree(&unmatched_stree
);
2075 static int rl_fits_in_type(struct range_list
*rl
, struct symbol
*type
)
2077 if (type_bits(rl_type(rl
)) <= type_bits(type
))
2079 if (sval_cmp(rl_max(rl
), sval_type_max(type
)) > 0)
2081 if (sval_is_negative(rl_min(rl
)) &&
2082 sval_cmp(rl_min(rl
), sval_type_min(type
)) < 0)
2087 static void db_param_limit_filter(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
2089 struct expression
*arg
;
2092 struct var_sym_list
*vsl
= NULL
;
2093 struct sm_state
*sm
;
2094 struct symbol
*compare_type
, *var_type
;
2095 struct range_list
*rl
;
2096 struct range_list
*limit
;
2097 struct range_list
*new;
2099 while (expr
->type
== EXPR_ASSIGNMENT
)
2100 expr
= strip_expr(expr
->right
);
2101 if (expr
->type
!= EXPR_CALL
)
2104 arg
= get_argument_from_call_expr(expr
->args
, param
);
2108 name
= get_chunk_from_key(arg
, key
, &sym
, &vsl
);
2111 if (op
!= PARAM_LIMIT
&& !sym
)
2114 if (strcmp(key
, "$") == 0)
2115 compare_type
= get_arg_type(expr
->fn
, param
);
2117 compare_type
= get_member_type_from_key(arg
, key
);
2119 sm
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
2121 rl
= estate_rl(sm
->state
);
2123 rl
= alloc_whole_rl(compare_type
);
2125 if (op
== PARAM_LIMIT
&& !rl_fits_in_type(rl
, compare_type
))
2128 call_results_to_rl(expr
, compare_type
, value
, &limit
);
2129 new = rl_intersection(rl
, limit
);
2131 var_type
= get_member_type_from_key(arg
, key
);
2132 new = cast_rl(var_type
, new);
2134 /* We want to preserve the implications here */
2135 if (sm
&& rl_equiv(estate_rl(sm
->state
), new))
2139 struct symbol
*tmp_sym
;
2141 tmp_name
= map_long_to_short_name_sym(name
, sym
, &tmp_sym
);
2142 if (tmp_name
&& tmp_sym
) {
2148 if (op
== PARAM_LIMIT
)
2149 set_extra_nomod_vsl(name
, sym
, vsl
, alloc_estate_rl(new));
2151 set_extra_mod(name
, sym
, alloc_estate_rl(new));
2158 static void db_param_limit(struct expression
*expr
, int param
, char *key
, char *value
)
2160 db_param_limit_filter(expr
, param
, key
, value
, PARAM_LIMIT
);
2163 static void db_param_filter(struct expression
*expr
, int param
, char *key
, char *value
)
2165 db_param_limit_filter(expr
, param
, key
, value
, PARAM_FILTER
);
2168 static void db_param_add_set(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
2170 struct expression
*arg
;
2171 char *name
, *tmp_name
;
2172 struct symbol
*sym
, *tmp_sym
;
2173 struct symbol
*type
;
2174 struct smatch_state
*state
;
2175 struct range_list
*new = NULL
;
2176 struct range_list
*added
= NULL
;
2178 while (expr
->type
== EXPR_ASSIGNMENT
)
2179 expr
= strip_expr(expr
->right
);
2180 if (expr
->type
!= EXPR_CALL
)
2183 arg
= get_argument_from_call_expr(expr
->args
, param
);
2186 type
= get_member_type_from_key(arg
, key
);
2187 name
= get_variable_from_key(arg
, key
, &sym
);
2191 state
= get_state(SMATCH_EXTRA
, name
, sym
);
2193 new = estate_rl(state
);
2195 call_results_to_rl(expr
, type
, value
, &added
);
2197 if (op
== PARAM_SET
)
2200 new = rl_union(new, added
);
2202 tmp_name
= map_long_to_short_name_sym(name
, sym
, &tmp_sym
);
2203 if (tmp_name
&& tmp_sym
) {
2208 set_extra_mod(name
, sym
, alloc_estate_rl(new));
2213 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
2215 db_param_add_set(expr
, param
, key
, value
, PARAM_ADD
);
2218 static void db_param_set(struct expression
*expr
, int param
, char *key
, char *value
)
2220 db_param_add_set(expr
, param
, key
, value
, PARAM_SET
);
2223 static void db_param_value(struct expression
*expr
, int param
, char *key
, char *value
)
2225 struct expression
*call
;
2228 struct symbol
*type
;
2229 struct range_list
*rl
= NULL
;
2235 while (call
->type
== EXPR_ASSIGNMENT
)
2236 call
= strip_expr(call
->right
);
2237 if (call
->type
!= EXPR_CALL
)
2240 type
= get_member_type_from_key(expr
->left
, key
);
2241 name
= get_variable_from_key(expr
->left
, key
, &sym
);
2245 call_results_to_rl(call
, type
, value
, &rl
);
2247 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
2252 static void match_call_info(struct expression
*expr
)
2254 struct smatch_state
*state
;
2255 struct range_list
*rl
= NULL
;
2256 struct expression
*arg
;
2257 struct symbol
*type
;
2260 FOR_EACH_PTR(expr
->args
, arg
) {
2261 type
= get_arg_type(expr
->fn
, i
);
2263 if (get_implied_rl(arg
, &rl
))
2264 rl
= cast_rl(type
, rl
);
2266 rl
= cast_rl(type
, alloc_whole_rl(get_type(arg
)));
2268 if (!is_whole_rl(rl
)) {
2269 rl
= intersect_with_real_abs_expr(arg
, rl
);
2270 sql_insert_caller_info(expr
, PARAM_VALUE
, i
, "$", show_rl(rl
));
2272 state
= get_state_expr(SMATCH_EXTRA
, arg
);
2273 if (estate_has_fuzzy_max(state
)) {
2274 sql_insert_caller_info(expr
, FUZZY_MAX
, i
, "$",
2275 sval_to_str(estate_get_fuzzy_max(state
)));
2278 } END_FOR_EACH_PTR(arg
);
2281 static void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2283 struct range_list
*rl
= NULL
;
2284 struct smatch_state
*state
;
2285 struct symbol
*type
;
2289 if (strcmp(key
, "*$") == 0)
2290 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
2291 else if (strncmp(key
, "$", 1) == 0)
2292 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
2296 type
= get_member_type_from_key(symbol_expression(sym
), key
);
2297 str_to_rl(type
, value
, &rl
);
2298 state
= alloc_estate_rl(rl
);
2299 if (estate_get_single_value(state
, &dummy
))
2300 estate_set_hard_max(state
);
2301 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
2304 static void set_param_hard_max(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2306 struct range_list
*rl
= NULL
;
2307 struct smatch_state
*state
;
2308 struct symbol
*type
;
2312 if (strcmp(key
, "*$") == 0)
2313 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
2314 else if (strncmp(key
, "$", 1) == 0)
2315 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
2319 state
= get_state(SMATCH_EXTRA
, fullname
, sym
);
2322 type
= get_member_type_from_key(symbol_expression(sym
), key
);
2323 str_to_rl(type
, value
, &rl
);
2324 if (!rl_to_sval(rl
, &max
))
2326 estate_set_fuzzy_max(state
, max
);
2329 struct smatch_state
*get_extra_state(struct expression
*expr
)
2333 struct smatch_state
*ret
= NULL
;
2334 struct range_list
*rl
;
2336 if (is_pointer(expr
) && get_address_rl(expr
, &rl
))
2337 return alloc_estate_rl(rl
);
2339 name
= expr_to_known_chunk_sym(expr
, &sym
);
2343 ret
= get_state(SMATCH_EXTRA
, name
, sym
);
2349 void register_smatch_extra(int id
)
2353 add_merge_hook(my_id
, &merge_estates
);
2354 add_unmatched_state_hook(my_id
, &unmatched_state
);
2355 select_caller_info_hook(set_param_value
, PARAM_VALUE
);
2356 select_caller_info_hook(set_param_hard_max
, FUZZY_MAX
);
2357 select_return_states_before(&db_limited_before
);
2358 select_return_states_hook(PARAM_LIMIT
, &db_param_limit
);
2359 select_return_states_hook(PARAM_FILTER
, &db_param_filter
);
2360 select_return_states_hook(PARAM_ADD
, &db_param_add
);
2361 select_return_states_hook(PARAM_SET
, &db_param_set
);
2362 select_return_states_hook(PARAM_VALUE
, &db_param_value
);
2363 select_return_states_after(&db_limited_after
);
2366 static void match_link_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
2368 struct var_sym_list
*links
;
2369 struct var_sym
*tmp
;
2370 struct smatch_state
*state
;
2372 links
= sm
->state
->data
;
2374 FOR_EACH_PTR(links
, tmp
) {
2375 if (sm
->sym
== tmp
->sym
&&
2376 strcmp(sm
->name
, tmp
->var
) == 0)
2378 state
= get_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
);
2381 set_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
, alloc_estate_whole(estate_type(state
)));
2382 } END_FOR_EACH_PTR(tmp
);
2383 set_state(link_id
, sm
->name
, sm
->sym
, &undefined
);
2386 void register_smatch_extra_links(int id
)
2391 void register_smatch_extra_late(int id
)
2393 add_merge_hook(link_id
, &merge_link_states
);
2394 add_modification_hook(link_id
, &match_link_modify
);
2395 add_hook(&match_dereferences
, DEREF_HOOK
);
2396 add_hook(&match_pointer_as_array
, OP_HOOK
);
2397 select_call_implies_hook(DEREFERENCE
, &set_param_dereferenced
);
2398 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
2399 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
2400 add_hook(&match_assign
, GLOBAL_ASSIGNMENT_HOOK
);
2401 add_hook(&unop_expr
, OP_HOOK
);
2402 add_hook(&asm_expr
, ASM_HOOK
);
2403 add_untracked_param_hook(&match_untracked_array
);
2405 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2406 add_member_info_callback(my_id
, struct_member_callback
);
2407 add_split_return_callback(&returned_struct_members
);
2409 add_hook(&assume_indexes_are_valid
, OP_HOOK
);