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 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 expression
*expr
, struct smatch_state
*state
);
64 DECLARE_PTR_LIST(void_fn_list
, mod_hook
*);
65 static struct void_fn_list
*extra_mod_hooks
;
66 static struct void_fn_list
*extra_nomod_hooks
;
68 void add_extra_mod_hook(mod_hook
*fn
)
70 mod_hook
**p
= malloc(sizeof(mod_hook
*));
72 add_ptr_list(&extra_mod_hooks
, p
);
75 void add_extra_nomod_hook(mod_hook
*fn
)
77 mod_hook
**p
= malloc(sizeof(mod_hook
*));
79 add_ptr_list(&extra_nomod_hooks
, p
);
82 void call_extra_hooks(struct void_fn_list
*hooks
, const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
86 FOR_EACH_PTR(hooks
, fn
) {
87 (*fn
)(name
, sym
, expr
, state
);
88 } END_FOR_EACH_PTR(fn
);
91 void call_extra_mod_hooks(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
93 call_extra_hooks(extra_mod_hooks
, name
, sym
, expr
, state
);
96 void call_extra_nomod_hooks(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
98 call_extra_hooks(extra_nomod_hooks
, name
, sym
, expr
, state
);
101 static bool in_param_set
;
102 void set_extra_mod_helper(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
104 remove_from_equiv(name
, sym
);
105 call_extra_mod_hooks(name
, sym
, expr
, state
);
106 if ((__in_fake_assign
|| in_param_set
) &&
107 estate_is_unknown(state
) && !get_state(SMATCH_EXTRA
, name
, sym
))
109 set_state(SMATCH_EXTRA
, name
, sym
, state
);
112 static void set_extra_nomod_helper(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
114 call_extra_nomod_hooks(name
, sym
, expr
, state
);
115 set_state(SMATCH_EXTRA
, name
, sym
, state
);
118 static char *get_pointed_at(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
120 struct expression
*assigned
;
124 if (strcmp(name
+ 1, sym
->ident
->name
) != 0)
127 assigned
= get_assigned_expr_name_sym(sym
->ident
->name
, sym
);
130 assigned
= strip_parens(assigned
);
131 if (assigned
->type
!= EXPR_PREOP
|| assigned
->op
!= '&')
134 return expr_to_var_sym(assigned
->unop
, new_sym
);
137 char *get_other_name_sym_helper(const char *name
, const char *chunk
, int len
, struct symbol
*sym
, struct symbol
**new_sym
)
139 struct expression
*assigned
;
140 char *orig_name
= NULL
;
144 assigned
= get_assigned_expr_name_sym(chunk
, sym
);
147 if (assigned
->type
== EXPR_CALL
)
148 return map_call_to_other_name_sym(name
, sym
, new_sym
);
149 if (assigned
->type
== EXPR_PREOP
&& assigned
->op
== '&') {
151 orig_name
= expr_to_var_sym(assigned
, new_sym
);
152 if (!orig_name
|| !*new_sym
)
155 snprintf(buf
, sizeof(buf
), "%s.%s", orig_name
+ 1, name
+ len
);
156 ret
= alloc_string(buf
);
157 free_string(orig_name
);
161 orig_name
= expr_to_var_sym(assigned
, new_sym
);
162 if (!orig_name
|| !*new_sym
)
165 snprintf(buf
, sizeof(buf
), "%s->%s", orig_name
, name
+ len
);
166 ret
= alloc_string(buf
);
167 free_string(orig_name
);
170 free_string(orig_name
);
174 char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
182 if (!sym
|| !sym
->ident
)
185 ret
= get_pointed_at(name
, sym
, new_sym
);
189 len
= snprintf(buf
, sizeof(buf
), "%s", name
);
190 if (len
>= sizeof(buf
) - 2)
194 if (buf
[len
] == '>' && buf
[len
- 1] == '-') {
197 ret
= get_other_name_sym_helper(name
, buf
, len
+ 2, sym
, new_sym
);
207 void set_extra_mod(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
210 struct symbol
*new_sym
;
212 set_extra_mod_helper(name
, sym
, expr
, state
);
213 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
214 if (new_name
&& new_sym
)
215 set_extra_mod_helper(new_name
, new_sym
, expr
, state
);
216 free_string(new_name
);
219 static struct expression
*chunk_get_array_base(struct expression
*expr
)
222 * The problem with is_array() is that it only returns true for things
223 * like foo[1] but not for foo[1].bar.
226 expr
= strip_expr(expr
);
227 while (expr
&& expr
->type
== EXPR_DEREF
)
228 expr
= strip_expr(expr
->deref
);
229 return get_array_base(expr
);
232 static int chunk_has_array(struct expression
*expr
)
234 return !!chunk_get_array_base(expr
);
237 static void clear_array_states(struct expression
*array
)
241 sm
= get_sm_state_expr(link_id
, array
);
243 match_link_modify(sm
, NULL
);
246 static void set_extra_array_mod(struct expression
*expr
, struct smatch_state
*state
)
248 struct expression
*array
;
249 struct var_sym_list
*vsl
;
254 array
= chunk_get_array_base(expr
);
256 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
258 clear_array_states(array
);
262 FOR_EACH_PTR(vsl
, vs
) {
263 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
264 } END_FOR_EACH_PTR(vs
);
266 call_extra_mod_hooks(name
, sym
, expr
, state
);
267 set_state(SMATCH_EXTRA
, name
, sym
, state
);
272 void set_extra_expr_mod(struct expression
*expr
, struct smatch_state
*state
)
277 if (chunk_has_array(expr
)) {
278 set_extra_array_mod(expr
, state
);
282 expr
= strip_expr(expr
);
283 name
= expr_to_var_sym(expr
, &sym
);
286 set_extra_mod(name
, sym
, expr
, state
);
291 void set_extra_nomod(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
294 struct symbol
*new_sym
;
295 struct relation
*rel
;
296 struct smatch_state
*orig_state
;
298 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
300 /* don't save unknown states if leaving it blank is the same */
301 if (!orig_state
&& estate_is_unknown(state
))
304 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
305 if (new_name
&& new_sym
)
306 set_extra_nomod_helper(new_name
, new_sym
, expr
, state
);
307 free_string(new_name
);
309 if (!estate_related(orig_state
)) {
310 set_extra_nomod_helper(name
, sym
, expr
, state
);
314 set_related(state
, estate_related(orig_state
));
315 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
316 struct smatch_state
*estate
;
318 if (option_debug_related
)
319 sm_msg("%s updating related %s to %s", name
, rel
->name
, state
->name
);
320 estate
= get_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
);
323 set_extra_nomod_helper(rel
->name
, rel
->sym
, expr
, clone_estate_cast(estate_type(estate
), state
));
324 } END_FOR_EACH_PTR(rel
);
327 void set_extra_nomod_vsl(const char *name
, struct symbol
*sym
, struct var_sym_list
*vsl
, struct expression
*expr
, struct smatch_state
*state
)
331 FOR_EACH_PTR(vsl
, vs
) {
332 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
333 } END_FOR_EACH_PTR(vs
);
335 set_extra_nomod(name
, sym
, expr
, state
);
339 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
341 void set_extra_expr_nomod(struct expression
*expr
, struct smatch_state
*state
)
343 struct var_sym_list
*vsl
;
348 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
351 FOR_EACH_PTR(vsl
, vs
) {
352 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
353 } END_FOR_EACH_PTR(vs
);
355 set_extra_nomod(name
, sym
, expr
, state
);
360 static void set_extra_true_false(const char *name
, struct symbol
*sym
,
361 struct smatch_state
*true_state
,
362 struct smatch_state
*false_state
)
365 struct symbol
*new_sym
;
366 struct relation
*rel
;
367 struct smatch_state
*orig_state
;
369 if (!true_state
&& !false_state
)
372 if (in_warn_on_macro())
375 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
376 if (new_name
&& new_sym
)
377 set_true_false_states(SMATCH_EXTRA
, new_name
, new_sym
, true_state
, false_state
);
378 free_string(new_name
);
380 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
382 if (!estate_related(orig_state
)) {
383 set_true_false_states(SMATCH_EXTRA
, name
, sym
, true_state
, false_state
);
388 set_related(true_state
, estate_related(orig_state
));
390 set_related(false_state
, estate_related(orig_state
));
392 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
393 set_true_false_states(SMATCH_EXTRA
, rel
->name
, rel
->sym
,
394 true_state
, false_state
);
395 } END_FOR_EACH_PTR(rel
);
398 static void set_extra_chunk_true_false(struct expression
*expr
,
399 struct smatch_state
*true_state
,
400 struct smatch_state
*false_state
)
402 struct var_sym_list
*vsl
;
408 if (in_warn_on_macro())
411 type
= get_type(expr
);
415 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
418 FOR_EACH_PTR(vsl
, vs
) {
419 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
420 } END_FOR_EACH_PTR(vs
);
422 set_true_false_states(SMATCH_EXTRA
, name
, sym
,
423 clone_estate(true_state
),
424 clone_estate(false_state
));
429 static void set_extra_expr_true_false(struct expression
*expr
,
430 struct smatch_state
*true_state
,
431 struct smatch_state
*false_state
)
437 if (!true_state
&& !false_state
)
440 if (get_value(expr
, &sval
))
443 expr
= strip_expr(expr
);
444 name
= expr_to_var_sym(expr
, &sym
);
447 set_extra_chunk_true_false(expr
, true_state
, false_state
);
450 set_extra_true_false(name
, sym
, true_state
, false_state
);
454 static int get_countdown_info(struct expression
*condition
, struct expression
**unop
, int *op
, sval_t
*right
)
456 struct expression
*unop_expr
;
460 right
->type
= &int_ctype
;
463 condition
= strip_expr(condition
);
465 if (condition
->type
== EXPR_COMPARE
) {
466 comparison
= remove_unsigned_from_comparison(condition
->op
);
468 if (comparison
!= SPECIAL_GTE
&& comparison
!= '>')
470 if (!get_value(condition
->right
, &limit
))
473 unop_expr
= condition
->left
;
474 if (unop_expr
->type
!= EXPR_PREOP
&& unop_expr
->type
!= EXPR_POSTOP
)
476 if (unop_expr
->op
!= SPECIAL_DECREMENT
)
486 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
488 if (condition
->op
!= SPECIAL_DECREMENT
)
497 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
499 struct expression
*iter_var
;
500 struct expression
*condition
, *unop
;
502 struct smatch_state
*estate
;
506 right
.type
= &int_ctype
;
509 condition
= strip_expr(loop
->iterator_pre_condition
);
513 if (!get_countdown_info(condition
, &unop
, &op
, &right
))
516 iter_var
= unop
->unop
;
518 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
521 if (sval_cmp(estate_min(sm
->state
), right
) < 0)
523 start
= estate_max(sm
->state
);
524 if (sval_cmp(start
, right
) <= 0)
526 if (!sval_is_max(start
))
529 if (op
== SPECIAL_GTE
)
532 if (unop
->type
== EXPR_PREOP
) {
534 estate
= alloc_estate_range(right
, start
);
535 if (estate_has_hard_max(sm
->state
))
536 estate_set_hard_max(estate
);
537 estate_copy_fuzzy_max(estate
, sm
->state
);
538 set_extra_expr_mod(iter_var
, estate
);
540 if (unop
->type
== EXPR_POSTOP
) {
541 estate
= alloc_estate_range(right
, start
);
542 if (estate_has_hard_max(sm
->state
))
543 estate_set_hard_max(estate
);
544 estate_copy_fuzzy_max(estate
, sm
->state
);
545 set_extra_expr_mod(iter_var
, estate
);
547 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
550 static struct sm_state
*handle_canonical_for_inc(struct expression
*iter_expr
,
551 struct expression
*condition
)
553 struct expression
*iter_var
;
555 struct smatch_state
*estate
;
556 sval_t start
, end
, max
;
558 iter_var
= iter_expr
->unop
;
559 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
562 if (!estate_get_single_value(sm
->state
, &start
))
564 if (!get_implied_value(condition
->right
, &end
))
567 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
570 switch (condition
->op
) {
571 case SPECIAL_UNSIGNED_LT
:
572 case SPECIAL_NOTEQUAL
:
574 if (!sval_is_min(end
))
577 case SPECIAL_UNSIGNED_LTE
:
583 if (sval_cmp(end
, start
) < 0)
585 estate
= alloc_estate_range(start
, end
);
586 if (get_hard_max(condition
->right
, &max
)) {
587 if (!get_macro_name(condition
->pos
))
588 estate_set_hard_max(estate
);
589 if (condition
->op
== '<' ||
590 condition
->op
== SPECIAL_UNSIGNED_LT
||
591 condition
->op
== SPECIAL_NOTEQUAL
)
593 estate_set_fuzzy_max(estate
, max
);
595 set_extra_expr_mod(iter_var
, estate
);
596 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
599 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
600 struct expression
*condition
)
602 struct expression
*iter_var
;
604 struct smatch_state
*estate
;
607 iter_var
= iter_expr
->unop
;
608 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
611 if (!estate_get_single_value(sm
->state
, &start
))
613 if (!get_implied_min(condition
->right
, &end
))
614 end
= sval_type_min(get_type(iter_var
));
615 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
618 switch (condition
->op
) {
619 case SPECIAL_NOTEQUAL
:
621 if (!sval_is_min(end
) && !sval_is_max(end
))
629 if (sval_cmp(end
, start
) > 0)
631 estate
= alloc_estate_range(end
, start
);
632 estate_set_hard_max(estate
);
633 estate_set_fuzzy_max(estate
, estate_get_fuzzy_max(estate
));
634 set_extra_expr_mod(iter_var
, estate
);
635 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
638 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
640 struct expression
*iter_expr
;
641 struct expression
*condition
;
643 if (!loop
->iterator_post_statement
)
645 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
647 iter_expr
= loop
->iterator_post_statement
->expression
;
648 if (!loop
->iterator_pre_condition
)
650 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
652 condition
= loop
->iterator_pre_condition
;
654 if (iter_expr
->op
== SPECIAL_INCREMENT
)
655 return handle_canonical_for_inc(iter_expr
, condition
);
656 if (iter_expr
->op
== SPECIAL_DECREMENT
)
657 return handle_canonical_for_dec(iter_expr
, condition
);
661 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct stree
**stree
)
663 struct sm_state
*ret
;
666 * Canonical loops are a hack. The proper way to handle this is to
667 * use two passes, but unfortunately, doing two passes makes parsing
668 * code twice as slow.
670 * What we do is we set the inside state here, which overwrites whatever
671 * __extra_match_condition() does. Then we set the outside state in
672 * __extra_pre_loop_hook_after().
675 __push_fake_cur_stree();
676 if (!loop
->iterator_post_statement
)
677 ret
= handle_canonical_while_count_down(loop
);
679 ret
= handle_canonical_for_loops(loop
);
680 *stree
= __pop_fake_cur_stree();
684 int __iterator_unchanged(struct sm_state
*sm
)
688 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
693 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
695 struct expression
*unop
;
697 sval_t limit
, after_value
;
699 if (!get_countdown_info(condition
, &unop
, &op
, &limit
))
701 after_value
= estate_min(sm
->state
);
703 set_extra_mod(sm
->name
, sm
->sym
, condition
->unop
, alloc_estate_sval(after_value
));
706 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
707 struct statement
*iterator
,
708 struct expression
*condition
)
710 struct expression
*iter_expr
;
712 struct smatch_state
*state
;
715 while_count_down_after(sm
, condition
);
719 iter_expr
= iterator
->expression
;
721 if (condition
->type
!= EXPR_COMPARE
)
723 if (iter_expr
->op
== SPECIAL_INCREMENT
) {
724 limit
= sval_binop(estate_max(sm
->state
), '+',
725 sval_type_val(estate_type(sm
->state
), 1));
727 limit
= sval_binop(estate_min(sm
->state
), '-',
728 sval_type_val(estate_type(sm
->state
), 1));
730 if (!estate_has_hard_max(sm
->state
) && !__has_breaks()) {
731 if (iter_expr
->op
== SPECIAL_INCREMENT
)
732 state
= alloc_estate_range(estate_min(sm
->state
), limit
);
734 state
= alloc_estate_range(limit
, estate_max(sm
->state
));
736 state
= alloc_estate_sval(limit
);
738 if (!estate_has_hard_max(sm
->state
)) {
739 estate_clear_hard_max(state
);
741 if (estate_has_fuzzy_max(sm
->state
)) {
742 sval_t hmax
= estate_get_fuzzy_max(sm
->state
);
743 sval_t max
= estate_max(sm
->state
);
745 if (sval_cmp(hmax
, max
) != 0)
746 estate_clear_fuzzy_max(state
);
747 } else if (!estate_has_fuzzy_max(sm
->state
)) {
748 estate_clear_fuzzy_max(state
);
751 set_extra_mod(sm
->name
, sm
->sym
, iter_expr
, state
);
754 static struct stree
*unmatched_stree
;
755 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
757 struct smatch_state
*state
;
759 if (unmatched_stree
) {
760 state
= get_state_stree(unmatched_stree
, SMATCH_EXTRA
, sm
->name
, sm
->sym
);
764 if (parent_is_gone_var_sym(sm
->name
, sm
->sym
))
765 return alloc_estate_empty();
766 return alloc_estate_whole(estate_type(sm
->state
));
769 static void clear_the_pointed_at(struct expression
*expr
)
774 struct sm_state
*tmp
;
776 name
= expr_to_var_sym(expr
, &sym
);
780 stree
= __get_cur_stree();
781 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
782 if (tmp
->name
[0] != '*')
786 if (strcmp(tmp
->name
+ 1, name
) != 0)
788 set_extra_mod(tmp
->name
, tmp
->sym
, expr
, alloc_estate_whole(estate_type(tmp
->state
)));
789 } END_FOR_EACH_SM(tmp
);
795 static int is_const_param(struct expression
*expr
, int param
)
799 type
= get_arg_type(expr
, param
);
802 if (type
->ctype
.modifiers
& MOD_CONST
)
807 static void match_function_call(struct expression
*expr
)
809 struct expression
*arg
;
810 struct expression
*tmp
;
813 /* if we have the db this is handled in smatch_function_hooks.c */
816 if (inlinable(expr
->fn
))
819 FOR_EACH_PTR(expr
->args
, arg
) {
821 if (is_const_param(expr
->fn
, param
))
823 tmp
= strip_expr(arg
);
824 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
825 set_extra_expr_mod(tmp
->unop
, alloc_estate_whole(get_type(tmp
->unop
)));
827 clear_the_pointed_at(tmp
);
828 } END_FOR_EACH_PTR(arg
);
831 static int values_fit_type(struct expression
*left
, struct expression
*right
)
833 struct range_list
*rl
;
836 type
= get_type(left
);
839 get_absolute_rl(right
, &rl
);
840 if (type_unsigned(type
) && sval_is_negative(rl_min(rl
)))
842 if (sval_cmp(sval_type_min(type
), rl_min(rl
)) > 0)
844 if (sval_cmp(sval_type_max(type
), rl_max(rl
)) < 0)
849 static void save_chunk_info(struct expression
*left
, struct expression
*right
)
851 struct var_sym_list
*vsl
;
853 struct expression
*add_expr
;
859 if (right
->type
!= EXPR_BINOP
|| right
->op
!= '-')
861 if (!get_value(right
->left
, &sval
))
863 if (!expr_to_sym(right
->right
))
866 add_expr
= binop_expression(left
, '+', right
->right
);
867 type
= get_type(add_expr
);
870 name
= expr_to_chunk_sym_vsl(add_expr
, &sym
, &vsl
);
873 FOR_EACH_PTR(vsl
, vs
) {
874 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
875 } END_FOR_EACH_PTR(vs
);
877 set_state(SMATCH_EXTRA
, name
, sym
, alloc_estate_sval(sval_cast(type
, sval
)));
882 static void do_array_assign(struct expression
*left
, int op
, struct expression
*right
)
884 struct range_list
*rl
;
887 get_absolute_rl(right
, &rl
);
888 rl
= cast_rl(get_type(left
), rl
);
890 rl
= alloc_whole_rl(get_type(left
));
893 set_extra_array_mod(left
, alloc_estate_rl(rl
));
896 static void match_vanilla_assign(struct expression
*left
, struct expression
*right
)
898 struct range_list
*orig_rl
= NULL
;
899 struct range_list
*rl
= NULL
;
900 struct symbol
*right_sym
;
901 struct symbol
*left_type
;
902 struct symbol
*right_type
;
903 char *right_name
= NULL
;
907 struct smatch_state
*state
;
913 save_chunk_info(left
, right
);
915 name
= expr_to_var_sym(left
, &sym
);
917 if (chunk_has_array(left
))
918 do_array_assign(left
, '=', right
);
922 left_type
= get_type(left
);
923 right_type
= get_type(right
);
925 right_name
= expr_to_var_sym(right
, &right_sym
);
927 if (!__in_fake_assign
&&
928 !(right
->type
== EXPR_PREOP
&& right
->op
== '&') &&
929 right_name
&& right_sym
&&
930 values_fit_type(left
, strip_expr(right
)) &&
931 !has_symbol(right
, sym
)) {
932 set_equiv(left
, right
);
936 if (is_pointer(right
) && get_address_rl(right
, &rl
)) {
937 state
= alloc_estate_rl(rl
);
941 if (get_implied_value(right
, &sval
)) {
942 state
= alloc_estate_sval(sval_cast(left_type
, sval
));
946 if (__in_fake_assign
) {
947 struct smatch_state
*right_state
;
950 if (get_value(right
, &sval
)) {
951 sval
= sval_cast(left_type
, sval
);
952 state
= alloc_estate_sval(sval
);
956 right_state
= get_state(SMATCH_EXTRA
, right_name
, right_sym
);
958 /* simple assignment */
959 state
= clone_estate(right_state
);
963 state
= alloc_estate_rl(alloc_whole_rl(left_type
));
967 comparison
= get_comparison(left
, right
);
969 comparison
= flip_comparison(comparison
);
970 get_implied_rl(left
, &orig_rl
);
973 if (get_implied_rl(right
, &rl
)) {
974 rl
= cast_rl(left_type
, rl
);
976 filter_by_comparison(&rl
, comparison
, orig_rl
);
977 state
= alloc_estate_rl(rl
);
978 if (get_hard_max(right
, &max
)) {
979 estate_set_hard_max(state
);
980 estate_set_fuzzy_max(state
, max
);
983 rl
= alloc_whole_rl(right_type
);
984 rl
= cast_rl(left_type
, rl
);
986 filter_by_comparison(&rl
, comparison
, orig_rl
);
987 state
= alloc_estate_rl(rl
);
991 set_extra_mod(name
, sym
, left
, state
);
993 free_string(right_name
);
996 static int op_remove_assign(int op
)
999 case SPECIAL_ADD_ASSIGN
:
1001 case SPECIAL_SUB_ASSIGN
:
1003 case SPECIAL_MUL_ASSIGN
:
1005 case SPECIAL_DIV_ASSIGN
:
1007 case SPECIAL_MOD_ASSIGN
:
1009 case SPECIAL_AND_ASSIGN
:
1011 case SPECIAL_OR_ASSIGN
:
1013 case SPECIAL_XOR_ASSIGN
:
1015 case SPECIAL_SHL_ASSIGN
:
1016 return SPECIAL_LEFTSHIFT
;
1017 case SPECIAL_SHR_ASSIGN
:
1018 return SPECIAL_RIGHTSHIFT
;
1024 static void match_assign(struct expression
*expr
)
1026 struct range_list
*rl
= NULL
;
1027 struct expression
*left
;
1028 struct expression
*right
;
1029 struct expression
*binop_expr
;
1030 struct symbol
*left_type
;
1033 sval_t left_min
, left_max
;
1034 sval_t right_min
, right_max
;
1035 sval_t res_min
, res_max
;
1037 left
= strip_expr(expr
->left
);
1039 right
= strip_parens(expr
->right
);
1040 if (right
->type
== EXPR_CALL
&& sym_name_is("__builtin_expect", right
->fn
))
1041 right
= get_argument_from_call_expr(right
->args
, 0);
1042 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
1043 right
= strip_parens(right
->left
);
1045 if (expr
->op
== '=' && is_condition(expr
->right
))
1046 return; /* handled in smatch_condition.c */
1047 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
1048 return; /* handled in smatch_function_hooks.c */
1049 if (expr
->op
== '=') {
1050 match_vanilla_assign(left
, right
);
1054 name
= expr_to_var_sym(left
, &sym
);
1058 left_type
= get_type(left
);
1060 res_min
= sval_type_min(left_type
);
1061 res_max
= sval_type_max(left_type
);
1064 case SPECIAL_ADD_ASSIGN
:
1065 get_absolute_max(left
, &left_max
);
1066 get_absolute_max(right
, &right_max
);
1067 if (sval_binop_overflows(left_max
, '+', sval_cast(left_type
, right_max
)))
1069 if (get_implied_min(left
, &left_min
) &&
1070 !sval_is_negative_min(left_min
) &&
1071 get_implied_min(right
, &right_min
) &&
1072 !sval_is_negative_min(right_min
)) {
1073 res_min
= sval_binop(left_min
, '+', right_min
);
1074 res_min
= sval_cast(left_type
, res_min
);
1076 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
1078 res_max
= sval_binop(left_max
, '+', right_max
);
1079 res_max
= sval_cast(left_type
, res_max
);
1081 case SPECIAL_SUB_ASSIGN
:
1082 if (get_implied_max(left
, &left_max
) &&
1083 !sval_is_max(left_max
) &&
1084 get_implied_min(right
, &right_min
) &&
1085 !sval_is_min(right_min
)) {
1086 res_max
= sval_binop(left_max
, '-', right_min
);
1087 res_max
= sval_cast(left_type
, res_max
);
1091 if (get_implied_min(left
, &left_min
) &&
1092 !sval_is_min(left_min
) &&
1093 get_implied_max(right
, &right_max
) &&
1094 !sval_is_max(right_max
)) {
1095 res_min
= sval_binop(left_min
, '-', right_max
);
1096 res_min
= sval_cast(left_type
, res_min
);
1099 case SPECIAL_AND_ASSIGN
:
1100 case SPECIAL_MOD_ASSIGN
:
1101 case SPECIAL_SHL_ASSIGN
:
1102 case SPECIAL_SHR_ASSIGN
:
1103 case SPECIAL_OR_ASSIGN
:
1104 case SPECIAL_XOR_ASSIGN
:
1105 case SPECIAL_MUL_ASSIGN
:
1106 case SPECIAL_DIV_ASSIGN
:
1107 binop_expr
= binop_expression(expr
->left
,
1108 op_remove_assign(expr
->op
),
1110 if (get_absolute_rl(binop_expr
, &rl
)) {
1111 rl
= cast_rl(left_type
, rl
);
1112 set_extra_mod(name
, sym
, left
, alloc_estate_rl(rl
));
1117 rl
= cast_rl(left_type
, alloc_rl(res_min
, res_max
));
1118 set_extra_mod(name
, sym
, left
, alloc_estate_rl(rl
));
1123 static struct smatch_state
*increment_state(struct smatch_state
*state
)
1125 sval_t min
= estate_min(state
);
1126 sval_t max
= estate_max(state
);
1128 if (!estate_rl(state
))
1132 max
= sval_type_max(max
.type
);
1134 if (!sval_is_min(min
) && !sval_is_max(min
))
1136 if (!sval_is_min(max
) && !sval_is_max(max
))
1138 return alloc_estate_range(min
, max
);
1141 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
1143 sval_t min
= estate_min(state
);
1144 sval_t max
= estate_max(state
);
1146 if (!estate_rl(state
))
1150 min
= sval_type_min(min
.type
);
1152 if (!sval_is_min(min
) && !sval_is_max(min
))
1154 if (!sval_is_min(max
) && !sval_is_max(max
))
1156 return alloc_estate_range(min
, max
);
1159 static void clear_pointed_at_state(struct expression
*expr
)
1161 struct symbol
*type
;
1164 * ALERT: This is sort of a mess. If it's is a struct assigment like
1165 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1166 * the same thing for p++ where "p" is a struct. Most modifications
1167 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1168 * use smatch_modification.c because we have to get the ordering right
1169 * or something. So if you have p++ where p is a pointer to a standard
1170 * c type then we handle that here. What a mess.
1172 expr
= strip_expr(expr
);
1173 type
= get_type(expr
);
1174 if (!type
|| type
->type
!= SYM_PTR
)
1176 type
= get_real_base_type(type
);
1177 if (!type
|| type
->type
!= SYM_BASETYPE
)
1179 set_extra_expr_nomod(deref_expression(expr
), alloc_estate_whole(type
));
1182 static void unop_expr(struct expression
*expr
)
1184 struct smatch_state
*state
;
1186 if (expr
->smatch_flags
& Handled
)
1190 case SPECIAL_INCREMENT
:
1191 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
1192 state
= increment_state(state
);
1194 state
= alloc_estate_whole(get_type(expr
));
1195 set_extra_expr_mod(expr
->unop
, state
);
1196 clear_pointed_at_state(expr
->unop
);
1198 case SPECIAL_DECREMENT
:
1199 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
1200 state
= decrement_state(state
);
1202 state
= alloc_estate_whole(get_type(expr
));
1203 set_extra_expr_mod(expr
->unop
, state
);
1204 clear_pointed_at_state(expr
->unop
);
1211 static void asm_expr(struct statement
*stmt
)
1214 struct expression
*expr
;
1215 struct symbol
*type
;
1218 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
1220 case 0: /* identifier */
1221 case 1: /* constraint */
1224 case 2: /* expression */
1226 type
= get_type(strip_expr(expr
));
1227 set_extra_expr_mod(expr
, alloc_estate_whole(type
));
1230 } END_FOR_EACH_PTR(expr
);
1233 static void check_dereference(struct expression
*expr
)
1235 struct smatch_state
*state
;
1237 if (__in_fake_assign
)
1239 if (outside_of_function())
1241 state
= get_extra_state(expr
);
1243 struct range_list
*rl
;
1245 rl
= rl_intersection(estate_rl(state
), valid_ptr_rl
);
1246 if (rl_equiv(rl
, estate_rl(state
)))
1248 set_extra_expr_nomod(expr
, alloc_estate_rl(rl
));
1250 set_extra_expr_nomod(expr
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
1254 static void match_dereferences(struct expression
*expr
)
1256 if (expr
->type
!= EXPR_PREOP
)
1258 /* it's saying that foo[1] = bar dereferences foo[1] */
1261 check_dereference(expr
->unop
);
1264 static void match_pointer_as_array(struct expression
*expr
)
1266 if (!is_array(expr
))
1268 check_dereference(get_array_base(expr
));
1271 static void find_dereferences(struct expression
*expr
)
1273 while (expr
->type
== EXPR_PREOP
) {
1274 if (expr
->op
== '*')
1275 check_dereference(expr
->unop
);
1276 expr
= strip_expr(expr
->unop
);
1280 static void set_param_dereferenced(struct expression
*call
, struct expression
*arg
, char *key
, char *unused
)
1285 name
= get_variable_from_key(arg
, key
, &sym
);
1287 struct smatch_state
*orig
, *new;
1288 struct range_list
*rl
;
1290 orig
= get_state(SMATCH_EXTRA
, name
, sym
);
1292 rl
= rl_intersection(estate_rl(orig
),
1293 alloc_rl(valid_ptr_min_sval
,
1294 valid_ptr_max_sval
));
1295 new = alloc_estate_rl(rl
);
1297 new = alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
);
1300 set_extra_nomod(name
, sym
, NULL
, new);
1304 find_dereferences(arg
);
1307 static sval_t
add_one(sval_t sval
)
1313 static int handle_postop_inc(struct expression
*left
, int op
, struct expression
*right
)
1315 struct statement
*stmt
;
1316 struct expression
*cond
;
1317 struct smatch_state
*true_state
, *false_state
;
1322 * If we're decrementing here then that's a canonical while count down
1323 * so it's handled already. We're only handling loops like:
1325 * do { ... } while (i++ < 3);
1328 if (left
->type
!= EXPR_POSTOP
|| left
->op
!= SPECIAL_INCREMENT
)
1331 stmt
= __cur_stmt
->parent
;
1334 if (stmt
->type
== STMT_COMPOUND
)
1335 stmt
= stmt
->parent
;
1336 if (!stmt
|| stmt
->type
!= STMT_ITERATOR
|| !stmt
->iterator_post_condition
)
1339 cond
= strip_expr(stmt
->iterator_post_condition
);
1340 if (cond
->type
!= EXPR_COMPARE
|| cond
->op
!= op
)
1342 if (left
!= strip_expr(cond
->left
) || right
!= strip_expr(cond
->right
))
1345 if (!get_implied_value(left
->unop
, &start
))
1347 if (!get_implied_value(right
, &limit
))
1350 if (sval_cmp(start
, limit
) > 0)
1355 case SPECIAL_UNSIGNED_LT
:
1358 case SPECIAL_UNSIGNED_LTE
:
1359 limit
= add_one(limit
);
1365 true_state
= alloc_estate_range(add_one(start
), limit
);
1366 false_state
= alloc_estate_range(add_one(limit
), add_one(limit
));
1368 /* Currently we just discard the false state but when two passes is
1369 * implimented correctly then it will use it.
1372 set_extra_expr_true_false(left
->unop
, true_state
, false_state
);
1377 bool is_impossible_variable(struct expression
*expr
)
1379 struct smatch_state
*state
;
1381 state
= get_extra_state(expr
);
1382 if (state
&& !estate_rl(state
))
1387 static bool in_macro(struct expression
*left
, struct expression
*right
)
1389 if (!left
|| !right
)
1391 if (left
->pos
.line
!= right
->pos
.line
|| left
->pos
.pos
!= right
->pos
.pos
)
1393 if (get_macro_name(left
->pos
))
1398 static void handle_comparison(struct symbol
*type
, struct expression
*left
, int op
, struct expression
*right
)
1400 struct range_list
*left_orig
;
1401 struct range_list
*left_true
;
1402 struct range_list
*left_false
;
1403 struct range_list
*right_orig
;
1404 struct range_list
*right_true
;
1405 struct range_list
*right_false
;
1406 struct smatch_state
*left_true_state
;
1407 struct smatch_state
*left_false_state
;
1408 struct smatch_state
*right_true_state
;
1409 struct smatch_state
*right_false_state
;
1410 sval_t dummy
, hard_max
;
1411 int left_postop
= 0;
1412 int right_postop
= 0;
1414 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
1415 if (left
->type
== EXPR_POSTOP
) {
1416 left
->smatch_flags
|= Handled
;
1417 left_postop
= left
->op
;
1418 if (handle_postop_inc(left
, op
, right
))
1421 left
= strip_parens(left
->unop
);
1423 while (left
->type
== EXPR_ASSIGNMENT
)
1424 left
= strip_parens(left
->left
);
1426 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
1427 if (right
->type
== EXPR_POSTOP
) {
1428 right
->smatch_flags
|= Handled
;
1429 right_postop
= right
->op
;
1431 right
= strip_parens(right
->unop
);
1434 if (is_impossible_variable(left
) || is_impossible_variable(right
))
1437 get_real_absolute_rl(left
, &left_orig
);
1438 left_orig
= cast_rl(type
, left_orig
);
1440 get_real_absolute_rl(right
, &right_orig
);
1441 right_orig
= cast_rl(type
, right_orig
);
1443 split_comparison_rl(left_orig
, op
, right_orig
, &left_true
, &left_false
, &right_true
, &right_false
);
1445 left_true
= rl_truncate_cast(get_type(strip_expr(left
)), left_true
);
1446 left_false
= rl_truncate_cast(get_type(strip_expr(left
)), left_false
);
1447 right_true
= rl_truncate_cast(get_type(strip_expr(right
)), right_true
);
1448 right_false
= rl_truncate_cast(get_type(strip_expr(right
)), right_false
);
1450 if (!left_true
|| !left_false
) {
1451 struct range_list
*tmp_true
, *tmp_false
;
1453 split_comparison_rl(alloc_whole_rl(type
), op
, right_orig
, &tmp_true
, &tmp_false
, NULL
, NULL
);
1454 tmp_true
= rl_truncate_cast(get_type(strip_expr(left
)), tmp_true
);
1455 tmp_false
= rl_truncate_cast(get_type(strip_expr(left
)), tmp_false
);
1456 if (tmp_true
&& tmp_false
)
1457 __save_imaginary_state(left
, tmp_true
, tmp_false
);
1460 if (!right_true
|| !right_false
) {
1461 struct range_list
*tmp_true
, *tmp_false
;
1463 split_comparison_rl(alloc_whole_rl(type
), op
, right_orig
, NULL
, NULL
, &tmp_true
, &tmp_false
);
1464 tmp_true
= rl_truncate_cast(get_type(strip_expr(right
)), tmp_true
);
1465 tmp_false
= rl_truncate_cast(get_type(strip_expr(right
)), tmp_false
);
1466 if (tmp_true
&& tmp_false
)
1467 __save_imaginary_state(right
, tmp_true
, tmp_false
);
1470 left_true_state
= alloc_estate_rl(left_true
);
1471 left_false_state
= alloc_estate_rl(left_false
);
1472 right_true_state
= alloc_estate_rl(right_true
);
1473 right_false_state
= alloc_estate_rl(right_false
);
1477 case SPECIAL_UNSIGNED_LT
:
1478 case SPECIAL_UNSIGNED_LTE
:
1480 if (get_implied_value(right
, &dummy
) && !in_macro(left
, right
))
1481 estate_set_hard_max(left_true_state
);
1482 if (get_implied_value(left
, &dummy
) && !in_macro(left
, right
))
1483 estate_set_hard_max(right_false_state
);
1486 case SPECIAL_UNSIGNED_GT
:
1487 case SPECIAL_UNSIGNED_GTE
:
1489 if (get_implied_value(left
, &dummy
) && !in_macro(left
, right
))
1490 estate_set_hard_max(right_true_state
);
1491 if (get_implied_value(right
, &dummy
) && !in_macro(left
, right
))
1492 estate_set_hard_max(left_false_state
);
1498 case SPECIAL_UNSIGNED_LT
:
1499 case SPECIAL_UNSIGNED_LTE
:
1501 if (get_hard_max(right
, &hard_max
)) {
1502 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1504 estate_set_fuzzy_max(left_true_state
, hard_max
);
1506 if (get_implied_value(right
, &hard_max
)) {
1507 if (op
== SPECIAL_UNSIGNED_LTE
||
1510 estate_set_fuzzy_max(left_false_state
, hard_max
);
1512 if (get_hard_max(left
, &hard_max
)) {
1513 if (op
== SPECIAL_UNSIGNED_LTE
||
1516 estate_set_fuzzy_max(right_false_state
, hard_max
);
1518 if (get_implied_value(left
, &hard_max
)) {
1519 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1521 estate_set_fuzzy_max(right_true_state
, hard_max
);
1525 case SPECIAL_UNSIGNED_GT
:
1526 case SPECIAL_UNSIGNED_GTE
:
1528 if (get_hard_max(left
, &hard_max
)) {
1529 if (op
== '>' || op
== SPECIAL_UNSIGNED_GT
)
1531 estate_set_fuzzy_max(right_true_state
, hard_max
);
1533 if (get_implied_value(left
, &hard_max
)) {
1534 if (op
== SPECIAL_UNSIGNED_GTE
||
1537 estate_set_fuzzy_max(right_false_state
, hard_max
);
1539 if (get_hard_max(right
, &hard_max
)) {
1540 if (op
== SPECIAL_UNSIGNED_LTE
||
1543 estate_set_fuzzy_max(left_false_state
, hard_max
);
1545 if (get_implied_value(right
, &hard_max
)) {
1547 op
== SPECIAL_UNSIGNED_GT
)
1549 estate_set_fuzzy_max(left_true_state
, hard_max
);
1553 if (get_hard_max(left
, &hard_max
))
1554 estate_set_fuzzy_max(right_true_state
, hard_max
);
1555 if (get_hard_max(right
, &hard_max
))
1556 estate_set_fuzzy_max(left_true_state
, hard_max
);
1560 if (get_hard_max(left
, &hard_max
)) {
1561 estate_set_hard_max(left_true_state
);
1562 estate_set_hard_max(left_false_state
);
1564 if (get_hard_max(right
, &hard_max
)) {
1565 estate_set_hard_max(right_true_state
);
1566 estate_set_hard_max(right_false_state
);
1569 if (left_postop
== SPECIAL_INCREMENT
) {
1570 left_true_state
= increment_state(left_true_state
);
1571 left_false_state
= increment_state(left_false_state
);
1573 if (left_postop
== SPECIAL_DECREMENT
) {
1574 left_true_state
= decrement_state(left_true_state
);
1575 left_false_state
= decrement_state(left_false_state
);
1577 if (right_postop
== SPECIAL_INCREMENT
) {
1578 right_true_state
= increment_state(right_true_state
);
1579 right_false_state
= increment_state(right_false_state
);
1581 if (right_postop
== SPECIAL_DECREMENT
) {
1582 right_true_state
= decrement_state(right_true_state
);
1583 right_false_state
= decrement_state(right_false_state
);
1586 if (estate_rl(left_true_state
) && estates_equiv(left_true_state
, left_false_state
)) {
1587 left_true_state
= NULL
;
1588 left_false_state
= NULL
;
1591 if (estate_rl(right_true_state
) && estates_equiv(right_true_state
, right_false_state
)) {
1592 right_true_state
= NULL
;
1593 right_false_state
= NULL
;
1596 /* Don't introduce new states for known true/false conditions */
1597 if (rl_equiv(left_orig
, estate_rl(left_true_state
)))
1598 left_true_state
= NULL
;
1599 if (rl_equiv(left_orig
, estate_rl(left_false_state
)))
1600 left_false_state
= NULL
;
1601 if (rl_equiv(right_orig
, estate_rl(right_true_state
)))
1602 right_true_state
= NULL
;
1603 if (rl_equiv(right_orig
, estate_rl(right_false_state
)))
1604 right_false_state
= NULL
;
1606 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
1607 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
1610 static int is_simple_math(struct expression
*expr
)
1614 if (expr
->type
!= EXPR_BINOP
)
1625 static void move_known_values(struct expression
**left_p
, struct expression
**right_p
)
1627 struct expression
*left
= *left_p
;
1628 struct expression
*right
= *right_p
;
1631 if (get_implied_value(left
, &sval
)) {
1632 if (!is_simple_math(right
))
1634 if (get_implied_value(right
, &dummy
))
1636 if (right
->op
== '*') {
1639 if (!get_value(right
->right
, &divisor
))
1641 if (divisor
.value
== 0)
1643 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1644 *right_p
= right
->left
;
1647 if (right
->op
== '+' && get_value(right
->left
, &sval
)) {
1648 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->left
);
1649 *right_p
= right
->right
;
1652 if (get_value(right
->right
, &sval
)) {
1653 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1654 *right_p
= right
->left
;
1659 if (get_implied_value(right
, &sval
)) {
1660 if (!is_simple_math(left
))
1662 if (get_implied_value(left
, &dummy
))
1664 if (left
->op
== '*') {
1667 if (!get_value(left
->right
, &divisor
))
1669 if (divisor
.value
== 0)
1671 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1672 *left_p
= left
->left
;
1675 if (left
->op
== '+' && get_value(left
->left
, &sval
)) {
1676 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->left
);
1677 *left_p
= left
->right
;
1681 if (get_value(left
->right
, &sval
)) {
1682 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1683 *left_p
= left
->left
;
1691 * The reason for do_simple_algebra() is to solve things like:
1692 * if (foo > 66 || foo + bar > 64) {
1693 * "foo" is not really a known variable so it won't be handled by
1694 * move_known_variables() but it's a super common idiom.
1697 static int do_simple_algebra(struct expression
**left_p
, struct expression
**right_p
)
1699 struct expression
*left
= *left_p
;
1700 struct expression
*right
= *right_p
;
1701 struct range_list
*rl
;
1704 if (left
->type
!= EXPR_BINOP
|| left
->op
!= '+')
1706 if (can_integer_overflow(get_type(left
), left
))
1708 if (!get_implied_value(right
, &tmp
))
1711 if (!get_implied_value(left
->left
, &tmp
) &&
1712 get_implied_rl(left
->left
, &rl
) &&
1714 *right_p
= binop_expression(right
, '-', left
->left
);
1715 *left_p
= left
->right
;
1718 if (!get_implied_value(left
->right
, &tmp
) &&
1719 get_implied_rl(left
->right
, &rl
) &&
1721 *right_p
= binop_expression(right
, '-', left
->right
);
1722 *left_p
= left
->left
;
1729 static int match_func_comparison(struct expression
*expr
)
1731 struct expression
*left
= strip_expr(expr
->left
);
1732 struct expression
*right
= strip_expr(expr
->right
);
1734 if (left
->type
== EXPR_CALL
|| right
->type
== EXPR_CALL
) {
1735 function_comparison(left
, expr
->op
, right
);
1742 /* Handle conditions like "if (foo + bar < foo) {" */
1743 static int handle_integer_overflow_test(struct expression
*expr
)
1745 struct expression
*left
, *right
;
1746 struct symbol
*type
;
1747 sval_t left_min
, right_min
, min
, max
;
1749 if (expr
->op
!= '<' && expr
->op
!= SPECIAL_UNSIGNED_LT
)
1752 left
= strip_parens(expr
->left
);
1753 right
= strip_parens(expr
->right
);
1755 if (left
->op
!= '+')
1758 type
= get_type(expr
);
1761 if (type_positive_bits(type
) == 32) {
1762 max
.type
= &uint_ctype
;
1763 max
.uvalue
= (unsigned int)-1;
1764 } else if (type_positive_bits(type
) == 64) {
1765 max
.type
= &ulong_ctype
;
1766 max
.value
= (unsigned long long)-1;
1771 if (!expr_equiv(left
->left
, right
) && !expr_equiv(left
->right
, right
))
1774 get_absolute_min(left
->left
, &left_min
);
1775 get_absolute_min(left
->right
, &right_min
);
1776 min
= sval_binop(left_min
, '+', right_min
);
1778 set_extra_chunk_true_false(left
, NULL
, alloc_estate_range(min
, max
));
1782 static void match_comparison(struct expression
*expr
)
1784 struct expression
*left_orig
= strip_parens(expr
->left
);
1785 struct expression
*right_orig
= strip_parens(expr
->right
);
1786 struct expression
*left
, *right
, *tmp
;
1787 struct expression
*prev
;
1788 struct symbol
*type
;
1791 if (match_func_comparison(expr
))
1794 type
= get_type(expr
);
1796 type
= &llong_ctype
;
1798 if (handle_integer_overflow_test(expr
))
1803 move_known_values(&left
, &right
);
1804 handle_comparison(type
, left
, expr
->op
, right
);
1808 if (do_simple_algebra(&left
, &right
))
1809 handle_comparison(type
, left
, expr
->op
, right
);
1811 prev
= get_assigned_expr(left_orig
);
1812 if (is_simple_math(prev
) && has_variable(prev
, left_orig
) == 0) {
1815 move_known_values(&left
, &right
);
1816 handle_comparison(type
, left
, expr
->op
, right
);
1819 prev
= get_assigned_expr(right_orig
);
1820 if (is_simple_math(prev
) && has_variable(prev
, right_orig
) == 0) {
1823 move_known_values(&left
, &right
);
1824 handle_comparison(type
, left
, expr
->op
, right
);
1830 if (get_last_expr_from_expression_stmt(left_orig
)) {
1831 left
= get_last_expr_from_expression_stmt(left_orig
);
1834 if (get_last_expr_from_expression_stmt(right_orig
)) {
1835 right
= get_last_expr_from_expression_stmt(right_orig
);
1843 while ((tmp
= get_assigned_expr(left
))) {
1846 left
= strip_expr(tmp
);
1849 while ((tmp
= get_assigned_expr(right
))) {
1852 right
= strip_expr(tmp
);
1855 handle_comparison(type
, left
, expr
->op
, right
);
1858 static sval_t
get_high_mask(sval_t known
)
1866 for (i
= type_bits(known
.type
) - 1; i
>= 0; i
--) {
1867 if (known
.uvalue
& (1ULL << i
))
1868 ret
.uvalue
|= (1ULL << i
);
1876 static void handle_AND_op(struct expression
*var
, sval_t known
)
1878 struct range_list
*orig_rl
;
1879 struct range_list
*true_rl
= NULL
;
1880 struct range_list
*false_rl
= NULL
;
1882 sval_t low_mask
= known
;
1886 get_absolute_rl(var
, &orig_rl
);
1888 if (known
.value
> 0) {
1889 bit
= ffsll(known
.value
) - 1;
1890 low_mask
.uvalue
= (1ULL << bit
) - 1;
1891 true_rl
= remove_range(orig_rl
, sval_type_val(known
.type
, 0), low_mask
);
1893 high_mask
= get_high_mask(known
);
1894 if (high_mask
.value
) {
1895 bit
= ffsll(high_mask
.value
) - 1;
1896 low_mask
.uvalue
= (1ULL << bit
) - 1;
1899 if (sval_is_negative(rl_min(orig_rl
)))
1900 false_rl
= remove_range(false_rl
, sval_type_min(known
.type
), sval_type_val(known
.type
, -1));
1901 false_rl
= remove_range(false_rl
, low_mask
, sval_type_max(known
.type
));
1902 if (type_signed(high_mask
.type
) && type_unsigned(rl_type(false_rl
))) {
1903 false_rl
= remove_range(false_rl
,
1904 sval_type_val(rl_type(false_rl
), sval_type_max(known
.type
).uvalue
),
1905 sval_type_val(rl_type(false_rl
), -1));
1907 } else if (known
.value
== 1 &&
1908 get_hard_max(var
, &max
) &&
1909 sval_cmp(max
, rl_max(orig_rl
)) == 0 &&
1911 false_rl
= remove_range(orig_rl
, max
, max
);
1913 set_extra_expr_true_false(var
,
1914 true_rl
? alloc_estate_rl(true_rl
) : NULL
,
1915 false_rl
? alloc_estate_rl(false_rl
) : NULL
);
1918 static void handle_AND_condition(struct expression
*expr
)
1922 if (get_implied_value(expr
->left
, &known
))
1923 handle_AND_op(expr
->right
, known
);
1924 else if (get_implied_value(expr
->right
, &known
))
1925 handle_AND_op(expr
->left
, known
);
1928 static void handle_MOD_condition(struct expression
*expr
)
1930 struct range_list
*orig_rl
;
1931 struct range_list
*true_rl
;
1932 struct range_list
*false_rl
= NULL
;
1938 if (!get_implied_value(expr
->right
, &right
) || right
.value
== 0)
1940 get_absolute_rl(expr
->left
, &orig_rl
);
1942 zero
.type
= rl_type(orig_rl
);
1944 /* We're basically dorking around the min and max here */
1945 true_rl
= remove_range(orig_rl
, zero
, zero
);
1946 if (!sval_is_max(rl_max(true_rl
)) &&
1947 !(rl_max(true_rl
).value
% right
.value
))
1948 true_rl
= remove_range(true_rl
, rl_max(true_rl
), rl_max(true_rl
));
1950 if (rl_equiv(true_rl
, orig_rl
))
1953 if (sval_is_positive(rl_min(orig_rl
)) &&
1954 (rl_max(orig_rl
).value
- rl_min(orig_rl
).value
) / right
.value
< 5) {
1958 add
= rl_min(orig_rl
);
1959 add
.value
+= right
.value
- (add
.value
% right
.value
);
1960 add
.value
-= right
.value
;
1962 for (i
= 0; i
< 5; i
++) {
1963 add
.value
+= right
.value
;
1964 if (add
.value
> rl_max(orig_rl
).value
)
1966 add_range(&false_rl
, add
, add
);
1969 if (rl_min(orig_rl
).uvalue
!= 0 &&
1970 rl_min(orig_rl
).uvalue
< right
.uvalue
) {
1971 sval_t chop
= right
;
1973 false_rl
= remove_range(orig_rl
, zero
, chop
);
1976 if (!sval_is_max(rl_max(orig_rl
)) &&
1977 (rl_max(orig_rl
).value
% right
.value
)) {
1978 sval_t chop
= rl_max(orig_rl
);
1979 chop
.value
-= chop
.value
% right
.value
;
1982 false_rl
= clone_rl(orig_rl
);
1983 false_rl
= remove_range(false_rl
, chop
, rl_max(orig_rl
));
1987 set_extra_expr_true_false(expr
->left
,
1988 true_rl
? alloc_estate_rl(true_rl
) : NULL
,
1989 false_rl
? alloc_estate_rl(false_rl
) : NULL
);
1992 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1993 void __extra_match_condition(struct expression
*expr
)
1995 expr
= strip_expr(expr
);
1996 switch (expr
->type
) {
1998 function_comparison(expr
, SPECIAL_NOTEQUAL
, zero_expr());
2003 handle_comparison(get_type(expr
), expr
, SPECIAL_NOTEQUAL
, zero_expr());
2006 match_comparison(expr
);
2008 case EXPR_ASSIGNMENT
:
2009 __extra_match_condition(expr
->left
);
2012 if (expr
->op
== '&')
2013 handle_AND_condition(expr
);
2014 if (expr
->op
== '%')
2015 handle_MOD_condition(expr
);
2020 static void assume_indexes_are_valid(struct expression
*expr
)
2022 struct expression
*array_expr
;
2024 struct expression
*offset
;
2025 struct symbol
*offset_type
;
2026 struct range_list
*rl_before
;
2027 struct range_list
*rl_after
;
2028 struct range_list
*filter
= NULL
;
2031 expr
= strip_expr(expr
);
2032 if (!is_array(expr
))
2035 offset
= get_array_offset(expr
);
2036 offset_type
= get_type(offset
);
2037 if (offset_type
&& type_signed(offset_type
)) {
2038 filter
= alloc_rl(sval_type_min(offset_type
),
2039 sval_type_val(offset_type
, -1));
2042 array_expr
= get_array_base(expr
);
2043 array_size
= get_real_array_size(array_expr
);
2044 if (array_size
> 1) {
2045 size
= sval_type_val(offset_type
, array_size
);
2046 add_range(&filter
, size
, sval_type_max(offset_type
));
2051 get_absolute_rl(offset
, &rl_before
);
2052 rl_after
= rl_filter(rl_before
, filter
);
2053 if (rl_equiv(rl_before
, rl_after
))
2055 set_extra_expr_nomod(offset
, alloc_estate_rl(rl_after
));
2058 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
2059 int implied_not_equal(struct expression
*expr
, long long val
)
2061 return !possibly_false(expr
, SPECIAL_NOTEQUAL
, value_expr(val
));
2064 int implied_not_equal_name_sym(char *name
, struct symbol
*sym
, long long val
)
2066 struct smatch_state
*estate
;
2068 estate
= get_state(SMATCH_EXTRA
, name
, sym
);
2071 if (!rl_has_sval(estate_rl(estate
), sval_type_val(estate_type(estate
), 0)))
2076 int parent_is_null_var_sym(const char *name
, struct symbol
*sym
)
2081 struct smatch_state
*state
;
2083 strncpy(buf
, name
, sizeof(buf
) - 1);
2084 buf
[sizeof(buf
) - 1] = '\0';
2087 while (*start
== '*') {
2089 state
= get_state(SMATCH_EXTRA
, start
, sym
);
2092 if (!estate_rl(state
))
2094 if (estate_min(state
).value
== 0 &&
2095 estate_max(state
).value
== 0)
2100 while (*start
== '&')
2103 while ((end
= strrchr(start
, '-'))) {
2105 state
= __get_state(SMATCH_EXTRA
, start
, sym
);
2108 if (estate_min(state
).value
== 0 &&
2109 estate_max(state
).value
== 0)
2115 int parent_is_null(struct expression
*expr
)
2121 expr
= strip_expr(expr
);
2122 var
= expr_to_var_sym(expr
, &sym
);
2125 ret
= parent_is_null_var_sym(var
, sym
);
2131 static int param_used_callback(void *found
, int argc
, char **argv
, char **azColName
)
2137 static int is_kzalloc_info(struct sm_state
*sm
)
2142 * kzalloc() information is treated as special because so there is just
2143 * a lot of stuff initialized to zero and it makes building the database
2144 * take hours and hours.
2146 * In theory, we should just remove this line and not pass any unused
2147 * information, but I'm not sure enough that this code works so I want
2148 * to hold off on that for now.
2150 if (!estate_get_single_value(sm
->state
, &sval
))
2152 if (sval
.value
!= 0)
2157 static int is_really_long(struct sm_state
*sm
)
2163 while ((p
= strstr(p
, "->"))) {
2169 strlen(sm
->name
) < 40)
2174 static int filter_unused_param_value_info(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
2178 /* for function pointers assume everything is used */
2179 if (call
->fn
->type
!= EXPR_SYMBOL
)
2183 * This is to handle __builtin_mul_overflow(). In an ideal world we
2184 * would only need this for invalid code.
2187 if (!call
->fn
->symbol
)
2190 if (!is_kzalloc_info(sm
) && !is_really_long(sm
))
2193 run_sql(¶m_used_callback
, &found
,
2194 "select * from return_implies where %s and type = %d and parameter = %d and key = '%s';",
2195 get_static_filter(call
->fn
->symbol
), PARAM_USED
, param
, printed_name
);
2199 /* If the database is not built yet, then assume everything is used */
2200 run_sql(¶m_used_callback
, &found
,
2201 "select * from return_implies where %s and type = %d;",
2202 get_static_filter(call
->fn
->symbol
), PARAM_USED
);
2209 struct range_list
*intersect_with_real_abs_var_sym(const char *name
, struct symbol
*sym
, struct range_list
*start
)
2211 struct smatch_state
*state
;
2214 * Here is the difference between implied value and real absolute, say
2219 * Then you know that a is 0-255. That's real absolute. But you don't
2220 * know for sure that it actually goes up to 255. So it's not implied.
2221 * Implied indicates a degree of certainty.
2223 * But then say you cap "a" at 8. That means you know it goes up to
2224 * 8. So now the implied value is s32min-8. But you can combine it
2225 * with the real absolute to say that actually it's 0-8.
2227 * We are combining it here. But now that I think about it, this is
2228 * probably not the ideal place to combine it because it should proably
2229 * be done earlier. Oh well, this is an improvement on what was there
2230 * before so I'm going to commit this code.
2234 state
= get_real_absolute_state_var_sym(name
, sym
);
2235 if (!state
|| !estate_rl(state
))
2238 return rl_intersection(estate_rl(state
), start
);
2241 struct range_list
*intersect_with_real_abs_expr(struct expression
*expr
, struct range_list
*start
)
2243 struct smatch_state
*state
;
2244 struct range_list
*abs_rl
;
2246 state
= get_real_absolute_state(expr
);
2247 if (!state
|| !estate_rl(state
))
2250 abs_rl
= cast_rl(rl_type(start
), estate_rl(state
));
2251 return rl_intersection(abs_rl
, start
);
2254 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
2256 struct range_list
*rl
;
2258 if (estate_is_whole(sm
->state
))
2260 if (filter_unused_param_value_info(call
, param
, printed_name
, sm
))
2262 rl
= estate_rl(sm
->state
);
2263 rl
= intersect_with_real_abs_var_sym(sm
->name
, sm
->sym
, rl
);
2264 sql_insert_caller_info(call
, PARAM_VALUE
, param
, printed_name
, show_rl(rl
));
2265 if (estate_has_hard_max(sm
->state
))
2266 sql_insert_caller_info(call
, HARD_MAX
, param
, printed_name
,
2267 sval_to_str(estate_max(sm
->state
)));
2268 if (estate_has_fuzzy_max(sm
->state
))
2269 sql_insert_caller_info(call
, FUZZY_MAX
, param
, printed_name
,
2270 sval_to_str(estate_get_fuzzy_max(sm
->state
)));
2273 static void returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
2275 struct symbol
*returned_sym
;
2276 char *returned_name
;
2277 struct sm_state
*sm
;
2285 if (!is_pointer(expr
))
2288 returned_name
= expr_to_var_sym(expr
, &returned_sym
);
2289 if (!returned_name
|| !returned_sym
)
2291 len
= strlen(returned_name
);
2293 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), sm
) {
2294 if (!estate_rl(sm
->state
))
2296 if (returned_sym
!= sm
->sym
)
2298 if (strncmp(returned_name
, sm
->name
, len
) != 0)
2300 if (sm
->name
[len
] != '-')
2303 snprintf(name_buf
, sizeof(name_buf
), "$%s", sm
->name
+ len
);
2305 compare_str
= name_sym_to_param_comparison(sm
->name
, sm
->sym
);
2306 if (!compare_str
&& estate_is_whole(sm
->state
))
2308 snprintf(val_buf
, sizeof(val_buf
), "%s%s", sm
->state
->name
, compare_str
?: "");
2310 sql_insert_return_states(return_id
, return_ranges
, PARAM_VALUE
,
2311 -1, name_buf
, val_buf
);
2312 } END_FOR_EACH_SM(sm
);
2315 free_string(returned_name
);
2318 static void db_limited_before(void)
2320 unmatched_stree
= clone_stree(__get_cur_stree());
2323 static void db_limited_after(void)
2325 free_stree(&unmatched_stree
);
2328 static int rl_fits_in_type(struct range_list
*rl
, struct symbol
*type
)
2330 if (type_bits(rl_type(rl
)) <= type_bits(type
))
2332 if (sval_cmp(rl_max(rl
), sval_type_max(type
)) > 0)
2334 if (sval_is_negative(rl_min(rl
)) &&
2335 sval_cmp(rl_min(rl
), sval_type_min(type
)) < 0)
2340 static int basically_the_same(struct range_list
*orig
, struct range_list
*new)
2342 if (rl_equiv(orig
, new))
2346 * The whole range is essentially the same as 0,4096-27777777777 so
2347 * don't overwrite the implications just to store that.
2350 if (rl_type(orig
)->type
== SYM_PTR
&&
2351 is_whole_rl(orig
) &&
2352 rl_min(new).value
== 0 &&
2353 rl_max(new).value
== valid_ptr_max
)
2358 static void db_param_limit_binops(struct expression
*arg
, char *key
, struct range_list
*rl
)
2360 struct range_list
*left_rl
;
2361 sval_t zero
= { .type
= rl_type(rl
), };
2366 if (!get_implied_value(arg
->right
, &sval
))
2368 if (can_integer_overflow(get_type(arg
), arg
))
2371 left_rl
= rl_binop(rl
, '/', alloc_rl(sval
, sval
));
2372 if (!rl_has_sval(rl
, zero
))
2373 left_rl
= remove_range(left_rl
, zero
, zero
);
2375 set_extra_expr_nomod(arg
->left
, alloc_estate_rl(left_rl
));
2378 static void db_param_limit_filter(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
2380 struct expression
*arg
;
2383 struct var_sym_list
*vsl
= NULL
;
2384 struct sm_state
*sm
;
2385 struct symbol
*compare_type
, *var_type
;
2386 struct range_list
*rl
;
2387 struct range_list
*limit
;
2388 struct range_list
*new;
2390 struct symbol
*other_sym
;
2392 while (expr
->type
== EXPR_ASSIGNMENT
)
2393 expr
= strip_expr(expr
->right
);
2394 if (expr
->type
!= EXPR_CALL
)
2397 arg
= get_argument_from_call_expr(expr
->args
, param
);
2401 name
= get_chunk_from_key(arg
, key
, &sym
, &vsl
);
2404 if (op
!= PARAM_LIMIT
&& !sym
)
2407 if (strcmp(key
, "$") == 0)
2408 compare_type
= get_arg_type(expr
->fn
, param
);
2410 compare_type
= get_member_type_from_key(arg
, key
);
2412 sm
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
2414 rl
= estate_rl(sm
->state
);
2416 rl
= alloc_whole_rl(compare_type
);
2418 if (op
== PARAM_LIMIT
&& !rl_fits_in_type(rl
, compare_type
))
2421 call_results_to_rl(expr
, compare_type
, value
, &limit
);
2422 new = rl_intersection(rl
, limit
);
2424 var_type
= get_member_type_from_key(arg
, key
);
2425 new = cast_rl(var_type
, new);
2427 /* We want to preserve the implications here */
2428 if (sm
&& basically_the_same(estate_rl(sm
->state
), new))
2430 other_name
= map_long_to_short_name_sym(name
, sym
, &other_sym
);
2432 if (op
== PARAM_LIMIT
)
2433 set_extra_nomod_vsl(name
, sym
, vsl
, NULL
, alloc_estate_rl(new));
2435 set_extra_mod(name
, sym
, NULL
, alloc_estate_rl(new));
2437 if (other_name
&& other_sym
) {
2438 if (op
== PARAM_LIMIT
)
2439 set_extra_nomod_vsl(other_name
, other_sym
, vsl
, NULL
, alloc_estate_rl(new));
2441 set_extra_mod(other_name
, other_sym
, NULL
, alloc_estate_rl(new));
2444 if (op
== PARAM_LIMIT
&& arg
->type
== EXPR_BINOP
)
2445 db_param_limit_binops(arg
, key
, new);
2450 static void db_param_limit(struct expression
*expr
, int param
, char *key
, char *value
)
2452 db_param_limit_filter(expr
, param
, key
, value
, PARAM_LIMIT
);
2455 static void db_param_filter(struct expression
*expr
, int param
, char *key
, char *value
)
2457 db_param_limit_filter(expr
, param
, key
, value
, PARAM_FILTER
);
2460 static void db_param_add_set(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
2462 struct expression
*arg
;
2463 char *name
, *tmp_name
;
2464 struct symbol
*sym
, *tmp_sym
;
2465 struct symbol
*param_type
, *arg_type
;
2466 struct smatch_state
*state
;
2467 struct range_list
*new = NULL
;
2468 struct range_list
*added
= NULL
;
2470 while (expr
->type
== EXPR_ASSIGNMENT
)
2471 expr
= strip_expr(expr
->right
);
2472 if (expr
->type
!= EXPR_CALL
)
2475 arg
= get_argument_from_call_expr(expr
->args
, param
);
2479 arg_type
= get_arg_type_from_key(expr
->fn
, param
, arg
, key
);
2480 param_type
= get_member_type_from_key(arg
, key
);
2481 name
= get_variable_from_key(arg
, key
, &sym
);
2485 state
= get_state(SMATCH_EXTRA
, name
, sym
);
2487 new = estate_rl(state
);
2489 call_results_to_rl(expr
, arg_type
, value
, &added
);
2490 added
= cast_rl(param_type
, added
);
2491 if (op
== PARAM_SET
)
2494 new = rl_union(new, added
);
2496 tmp_name
= map_long_to_short_name_sym_nostack(name
, sym
, &tmp_sym
);
2497 if (tmp_name
&& tmp_sym
) {
2502 set_extra_mod(name
, sym
, NULL
, alloc_estate_rl(new));
2507 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
2509 in_param_set
= true;
2510 db_param_add_set(expr
, param
, key
, value
, PARAM_ADD
);
2511 in_param_set
= false;
2514 static void db_param_set(struct expression
*expr
, int param
, char *key
, char *value
)
2516 in_param_set
= true;
2517 db_param_add_set(expr
, param
, key
, value
, PARAM_SET
);
2518 in_param_set
= false;
2521 static void match_lost_param(struct expression
*call
, int param
)
2523 struct expression
*arg
;
2525 if (is_const_param(call
->fn
, param
))
2528 arg
= get_argument_from_call_expr(call
->args
, param
);
2532 arg
= strip_expr(arg
);
2533 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&')
2534 set_extra_expr_mod(arg
->unop
, alloc_estate_whole(get_type(arg
->unop
)));
2536 ; /* if pointer then set struct members, maybe?*/
2539 static void db_param_value(struct expression
*expr
, int param
, char *key
, char *value
)
2541 struct expression
*call
;
2544 struct symbol
*type
;
2545 struct range_list
*rl
= NULL
;
2551 while (call
->type
== EXPR_ASSIGNMENT
)
2552 call
= strip_expr(call
->right
);
2553 if (call
->type
!= EXPR_CALL
)
2556 type
= get_member_type_from_key(expr
->left
, key
);
2557 name
= get_variable_from_key(expr
->left
, key
, &sym
);
2561 call_results_to_rl(call
, type
, value
, &rl
);
2563 set_extra_mod(name
, sym
, NULL
, alloc_estate_rl(rl
));
2568 static void match_call_info(struct expression
*expr
)
2570 struct smatch_state
*state
;
2571 struct range_list
*rl
= NULL
;
2572 struct expression
*arg
;
2573 struct symbol
*type
;
2576 FOR_EACH_PTR(expr
->args
, arg
) {
2577 type
= get_arg_type(expr
->fn
, i
);
2579 get_absolute_rl(arg
, &rl
);
2580 rl
= cast_rl(type
, rl
);
2582 if (!is_whole_rl(rl
)) {
2583 rl
= intersect_with_real_abs_expr(arg
, rl
);
2584 sql_insert_caller_info(expr
, PARAM_VALUE
, i
, "$", show_rl(rl
));
2586 state
= get_state_expr(SMATCH_EXTRA
, arg
);
2587 if (estate_has_hard_max(state
)) {
2588 sql_insert_caller_info(expr
, HARD_MAX
, i
, "$",
2589 sval_to_str(estate_max(state
)));
2591 if (estate_has_fuzzy_max(state
)) {
2592 sql_insert_caller_info(expr
, FUZZY_MAX
, i
, "$",
2593 sval_to_str(estate_get_fuzzy_max(state
)));
2596 } END_FOR_EACH_PTR(arg
);
2599 static void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2601 struct range_list
*rl
= NULL
;
2602 struct smatch_state
*state
;
2603 struct symbol
*type
;
2607 if (strcmp(key
, "*$") == 0)
2608 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
2609 else if (strncmp(key
, "$", 1) == 0)
2610 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
2614 type
= get_member_type_from_key(symbol_expression(sym
), key
);
2615 str_to_rl(type
, value
, &rl
);
2616 state
= alloc_estate_rl(rl
);
2617 if (estate_get_single_value(state
, &dummy
))
2618 estate_set_hard_max(state
);
2619 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
2622 static void set_param_fuzzy_max(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2624 struct range_list
*rl
= NULL
;
2625 struct smatch_state
*state
;
2626 struct symbol
*type
;
2630 if (strcmp(key
, "*$") == 0)
2631 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
2632 else if (strncmp(key
, "$", 1) == 0)
2633 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
2637 state
= get_state(SMATCH_EXTRA
, fullname
, sym
);
2640 type
= estate_type(state
);
2641 str_to_rl(type
, value
, &rl
);
2642 if (!rl_to_sval(rl
, &max
))
2644 estate_set_fuzzy_max(state
, max
);
2647 static void set_param_hard_max(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2649 struct smatch_state
*state
;
2652 if (strcmp(key
, "*$") == 0)
2653 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
2654 else if (strncmp(key
, "$", 1) == 0)
2655 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
2659 state
= get_state(SMATCH_EXTRA
, fullname
, sym
);
2662 estate_set_hard_max(state
);
2665 struct sm_state
*get_extra_sm_state(struct expression
*expr
)
2669 struct sm_state
*ret
= NULL
;
2671 name
= expr_to_known_chunk_sym(expr
, &sym
);
2675 ret
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
2681 struct smatch_state
*get_extra_state(struct expression
*expr
)
2683 struct sm_state
*sm
;
2685 sm
= get_extra_sm_state(expr
);
2691 void register_smatch_extra(int id
)
2695 add_merge_hook(my_id
, &merge_estates
);
2696 add_unmatched_state_hook(my_id
, &unmatched_state
);
2697 select_caller_info_hook(set_param_value
, PARAM_VALUE
);
2698 select_caller_info_hook(set_param_fuzzy_max
, FUZZY_MAX
);
2699 select_caller_info_hook(set_param_hard_max
, HARD_MAX
);
2700 select_return_states_before(&db_limited_before
);
2701 select_return_states_hook(PARAM_LIMIT
, &db_param_limit
);
2702 select_return_states_hook(PARAM_FILTER
, &db_param_filter
);
2703 select_return_states_hook(PARAM_ADD
, &db_param_add
);
2704 select_return_states_hook(PARAM_SET
, &db_param_set
);
2705 add_lost_param_hook(&match_lost_param
);
2706 select_return_states_hook(PARAM_VALUE
, &db_param_value
);
2707 select_return_states_after(&db_limited_after
);
2710 static void match_link_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
2712 struct var_sym_list
*links
;
2713 struct var_sym
*tmp
;
2714 struct smatch_state
*state
;
2716 links
= sm
->state
->data
;
2718 FOR_EACH_PTR(links
, tmp
) {
2719 if (sm
->sym
== tmp
->sym
&&
2720 strcmp(sm
->name
, tmp
->var
) == 0)
2722 state
= get_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
);
2725 set_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
, alloc_estate_whole(estate_type(state
)));
2726 } END_FOR_EACH_PTR(tmp
);
2727 set_state(link_id
, sm
->name
, sm
->sym
, &undefined
);
2730 void register_smatch_extra_links(int id
)
2735 void register_smatch_extra_late(int id
)
2737 add_merge_hook(link_id
, &merge_link_states
);
2738 add_modification_hook(link_id
, &match_link_modify
);
2739 add_hook(&match_dereferences
, DEREF_HOOK
);
2740 add_hook(&match_pointer_as_array
, OP_HOOK
);
2741 select_return_implies_hook(DEREFERENCE
, &set_param_dereferenced
);
2742 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
2743 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
2744 add_hook(&match_assign
, GLOBAL_ASSIGNMENT_HOOK
);
2745 add_hook(&unop_expr
, OP_HOOK
);
2746 add_hook(&asm_expr
, ASM_HOOK
);
2748 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2749 add_member_info_callback(my_id
, struct_member_callback
);
2750 add_split_return_callback(&returned_struct_members
);
2752 // add_hook(&assume_indexes_are_valid, OP_HOOK);