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.
31 #include "smatch_slist.h"
32 #include "smatch_extra.h"
37 static void match_link_modify(struct sm_state
*sm
, struct expression
*mod_expr
);
39 struct string_list
*__ignored_macros
= NULL
;
40 static int in_warn_on_macro(void)
42 struct statement
*stmt
;
46 stmt
= get_current_statement();
49 macro
= get_macro_name(stmt
->pos
);
53 FOR_EACH_PTR(__ignored_macros
, tmp
) {
54 if (!strcmp(tmp
, macro
))
56 } END_FOR_EACH_PTR(tmp
);
60 typedef void (mod_hook
)(const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
61 DECLARE_PTR_LIST(void_fn_list
, mod_hook
*);
62 static struct void_fn_list
*extra_mod_hooks
;
63 void add_extra_mod_hook(mod_hook
*fn
)
65 mod_hook
**p
= malloc(sizeof(mod_hook
*));
67 add_ptr_list(&extra_mod_hooks
, p
);
70 void call_extra_mod_hooks(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
74 FOR_EACH_PTR(extra_mod_hooks
, fn
) {
75 (*fn
)(name
, sym
, state
);
76 } END_FOR_EACH_PTR(fn
);
79 struct sm_state
*set_extra_mod_helper(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
81 remove_from_equiv(name
, sym
);
82 call_extra_mod_hooks(name
, sym
, state
);
83 if (__in_fake_assign
&& estate_is_unknown(state
) && !get_state(SMATCH_EXTRA
, name
, sym
))
85 return set_state(SMATCH_EXTRA
, name
, sym
, state
);
88 static char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
90 struct expression
*assigned
;
91 char *orig_name
= NULL
;
101 skip
= strlen(sym
->ident
->name
);
102 if (name
[skip
] != '-' || name
[skip
+ 1] != '>')
106 assigned
= get_assigned_expr_name_sym(sym
->ident
->name
, sym
);
109 if (assigned
->type
== EXPR_PREOP
|| assigned
->op
== '&') {
111 orig_name
= expr_to_var_sym(assigned
, new_sym
);
112 if (!orig_name
|| !*new_sym
)
115 snprintf(buf
, sizeof(buf
), "%s.%s", orig_name
+ 1, name
+ skip
);
116 ret
= alloc_string(buf
);
117 free_string(orig_name
);
121 if (assigned
->type
!= EXPR_DEREF
)
124 orig_name
= expr_to_var_sym(assigned
, new_sym
);
125 if (!orig_name
|| !*new_sym
)
128 snprintf(buf
, sizeof(buf
), "%s->%s", orig_name
, name
+ skip
);
129 ret
= alloc_string(buf
);
130 free_string(orig_name
);
134 free_string(orig_name
);
138 struct sm_state
*set_extra_mod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
141 struct symbol
*new_sym
;
144 if (in_warn_on_macro())
146 sm
= set_extra_mod_helper(name
, sym
, state
);
147 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
148 if (new_name
&& new_sym
)
149 set_extra_mod_helper(new_name
, new_sym
, state
);
150 free_string(new_name
);
154 static void clear_array_states(struct expression
*array
)
158 sm
= get_sm_state_expr(link_id
, array
);
160 match_link_modify(sm
, NULL
);
163 static struct sm_state
*set_extra_array_mod(struct expression
*expr
, struct smatch_state
*state
)
165 struct expression
*array
;
166 struct expression
*offset
;
167 struct var_sym_list
*vsl
;
173 array
= get_array_base(expr
);
174 offset
= get_array_offset(expr
);
176 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
178 if (!name
|| !vsl
|| !get_value(offset
, &sval
)) {
179 clear_array_states(array
);
183 FOR_EACH_PTR(vsl
, vs
) {
184 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
185 } END_FOR_EACH_PTR(vs
);
187 return set_state(SMATCH_EXTRA
, name
, sym
, state
);
193 struct sm_state
*set_extra_expr_mod(struct expression
*expr
, struct smatch_state
*state
)
197 struct sm_state
*ret
= NULL
;
200 return set_extra_array_mod(expr
, state
);
202 expr
= strip_expr(expr
);
203 name
= expr_to_var_sym(expr
, &sym
);
206 ret
= set_extra_mod(name
, sym
, state
);
212 void set_extra_nomod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
215 struct symbol
*new_sym
;
216 struct relation
*rel
;
217 struct smatch_state
*orig_state
;
219 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
221 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
222 if (new_name
&& new_sym
)
223 set_state(SMATCH_EXTRA
, new_name
, new_sym
, state
);
224 free_string(new_name
);
226 if (!estate_related(orig_state
)) {
227 set_state(SMATCH_EXTRA
, name
, sym
, state
);
231 set_related(state
, estate_related(orig_state
));
232 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
233 if (option_debug_related
)
234 sm_msg("%s updating related %s to %s", name
, rel
->name
, state
->name
);
235 set_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
, state
);
236 } END_FOR_EACH_PTR(rel
);
240 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
242 void set_extra_expr_nomod(struct expression
*expr
, struct smatch_state
*state
)
247 name
= expr_to_var_sym(expr
, &sym
);
250 set_extra_nomod(name
, sym
, state
);
256 static void set_extra_true_false(const char *name
, struct symbol
*sym
,
257 struct smatch_state
*true_state
,
258 struct smatch_state
*false_state
)
261 struct symbol
*new_sym
;
262 struct relation
*rel
;
263 struct smatch_state
*orig_state
;
265 if (in_warn_on_macro())
268 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
269 if (new_name
&& new_sym
)
270 set_true_false_states(SMATCH_EXTRA
, new_name
, new_sym
, true_state
, false_state
);
271 free_string(new_name
);
273 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
275 if (!estate_related(orig_state
)) {
276 set_true_false_states(SMATCH_EXTRA
, name
, sym
, true_state
, false_state
);
281 set_related(true_state
, estate_related(orig_state
));
283 set_related(false_state
, estate_related(orig_state
));
285 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
286 set_true_false_states(SMATCH_EXTRA
, rel
->name
, rel
->sym
,
287 true_state
, false_state
);
288 } END_FOR_EACH_PTR(rel
);
291 static void set_extra_chunk_true_false(struct expression
*expr
,
292 struct smatch_state
*true_state
,
293 struct smatch_state
*false_state
)
295 struct var_sym_list
*vsl
;
301 if (in_warn_on_macro())
304 type
= get_type(expr
);
308 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
311 FOR_EACH_PTR(vsl
, vs
) {
312 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
313 } END_FOR_EACH_PTR(vs
);
315 set_true_false_states(SMATCH_EXTRA
, name
, sym
,
316 clone_estate(true_state
),
317 clone_estate(false_state
));
322 static void set_extra_expr_true_false(struct expression
*expr
,
323 struct smatch_state
*true_state
,
324 struct smatch_state
*false_state
)
330 if (get_value(expr
, &sval
))
333 expr
= strip_expr(expr
);
334 name
= expr_to_var_sym(expr
, &sym
);
337 set_extra_chunk_true_false(expr
, true_state
, false_state
);
340 set_extra_true_false(name
, sym
, true_state
, false_state
);
344 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
346 struct expression
*iter_var
;
347 struct expression
*condition
;
349 struct smatch_state
*estate
;
352 condition
= strip_expr(loop
->iterator_pre_condition
);
355 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
357 if (condition
->op
!= SPECIAL_DECREMENT
)
360 iter_var
= condition
->unop
;
361 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
364 if (sval_cmp_val(estate_min(sm
->state
), 0) < 0)
366 start
= estate_max(sm
->state
);
367 if (sval_cmp_val(start
, 0) <= 0)
369 if (!sval_is_max(start
))
372 if (condition
->type
== EXPR_PREOP
) {
373 estate
= alloc_estate_range(sval_type_val(start
.type
, 1), start
);
374 if (estate_has_hard_max(sm
->state
))
375 estate_set_hard_max(estate
);
376 estate_copy_fuzzy_max(estate
, sm
->state
);
377 set_extra_expr_mod(iter_var
, estate
);
379 if (condition
->type
== EXPR_POSTOP
) {
380 estate
= alloc_estate_range(sval_type_val(start
.type
, 0), start
);
381 if (estate_has_hard_max(sm
->state
))
382 estate_set_hard_max(estate
);
383 estate_copy_fuzzy_max(estate
, sm
->state
);
384 set_extra_expr_mod(iter_var
, estate
);
386 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
389 static struct sm_state
*handle_canonical_for_inc(struct expression
*iter_expr
,
390 struct expression
*condition
)
392 struct expression
*iter_var
;
394 struct smatch_state
*estate
;
395 sval_t start
, end
, max
;
397 iter_var
= iter_expr
->unop
;
398 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
401 if (!estate_get_single_value(sm
->state
, &start
))
403 if (get_implied_max(condition
->right
, &end
))
404 end
= sval_cast(get_type(iter_var
), end
);
406 end
= sval_type_max(get_type(iter_var
));
408 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
411 switch (condition
->op
) {
412 case SPECIAL_UNSIGNED_LT
:
413 case SPECIAL_NOTEQUAL
:
415 if (!sval_is_min(end
))
418 case SPECIAL_UNSIGNED_LTE
:
424 if (sval_cmp(end
, start
) < 0)
426 estate
= alloc_estate_range(start
, end
);
427 if (get_hard_max(condition
->right
, &max
)) {
428 estate_set_hard_max(estate
);
429 if (condition
->op
== '<' ||
430 condition
->op
== SPECIAL_UNSIGNED_LT
||
431 condition
->op
== SPECIAL_NOTEQUAL
)
433 estate_set_fuzzy_max(estate
, max
);
435 set_extra_expr_mod(iter_var
, estate
);
436 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
439 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
440 struct expression
*condition
)
442 struct expression
*iter_var
;
444 struct smatch_state
*estate
;
447 iter_var
= iter_expr
->unop
;
448 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
451 if (!estate_get_single_value(sm
->state
, &start
))
453 if (!get_implied_min(condition
->right
, &end
))
454 end
= sval_type_min(get_type(iter_var
));
455 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
458 switch (condition
->op
) {
459 case SPECIAL_NOTEQUAL
:
461 if (!sval_is_min(end
) && !sval_is_max(end
))
469 if (sval_cmp(end
, start
) > 0)
471 estate
= alloc_estate_range(end
, start
);
472 estate_set_hard_max(estate
);
473 estate_set_fuzzy_max(estate
, estate_get_fuzzy_max(estate
));
474 set_extra_expr_mod(iter_var
, estate
);
475 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
478 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
480 struct expression
*iter_expr
;
481 struct expression
*condition
;
483 if (!loop
->iterator_post_statement
)
485 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
487 iter_expr
= loop
->iterator_post_statement
->expression
;
488 if (!loop
->iterator_pre_condition
)
490 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
492 condition
= loop
->iterator_pre_condition
;
494 if (iter_expr
->op
== SPECIAL_INCREMENT
)
495 return handle_canonical_for_inc(iter_expr
, condition
);
496 if (iter_expr
->op
== SPECIAL_DECREMENT
)
497 return handle_canonical_for_dec(iter_expr
, condition
);
501 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct stree
**stree
)
503 struct sm_state
*ret
;
505 __push_fake_cur_stree();
506 if (!loop
->iterator_post_statement
)
507 ret
= handle_canonical_while_count_down(loop
);
509 ret
= handle_canonical_for_loops(loop
);
510 *stree
= __pop_fake_cur_stree();
514 int __iterator_unchanged(struct sm_state
*sm
)
518 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
523 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
527 /* paranoid checking. prolly not needed */
528 condition
= strip_expr(condition
);
531 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
533 if (condition
->op
!= SPECIAL_DECREMENT
)
535 after_value
= estate_min(sm
->state
);
537 set_extra_mod(sm
->name
, sm
->sym
, alloc_estate_sval(after_value
));
540 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
541 struct statement
*iterator
,
542 struct expression
*condition
)
544 struct expression
*iter_expr
;
546 struct smatch_state
*state
;
549 while_count_down_after(sm
, condition
);
553 iter_expr
= iterator
->expression
;
555 if (condition
->type
!= EXPR_COMPARE
)
557 if (iter_expr
->op
== SPECIAL_INCREMENT
) {
558 limit
= sval_binop(estate_max(sm
->state
), '+',
559 sval_type_val(estate_type(sm
->state
), 1));
561 limit
= sval_binop(estate_min(sm
->state
), '-',
562 sval_type_val(estate_type(sm
->state
), 1));
564 if (!estate_has_hard_max(sm
->state
) && !__has_breaks()) {
565 if (iter_expr
->op
== SPECIAL_INCREMENT
)
566 state
= alloc_estate_range(estate_min(sm
->state
), limit
);
568 state
= alloc_estate_range(limit
, estate_max(sm
->state
));
570 state
= alloc_estate_sval(limit
);
572 if (!estate_has_hard_max(sm
->state
)) {
573 estate_clear_hard_max(state
);
575 if (estate_has_fuzzy_max(sm
->state
)) {
576 sval_t hmax
= estate_get_fuzzy_max(sm
->state
);
577 sval_t max
= estate_max(sm
->state
);
579 if (sval_cmp(hmax
, max
) != 0)
580 estate_clear_fuzzy_max(state
);
581 } else if (!estate_has_fuzzy_max(sm
->state
)) {
582 estate_clear_fuzzy_max(state
);
585 set_extra_mod(sm
->name
, sm
->sym
, state
);
588 static struct stree
*unmatched_stree
;
589 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
591 struct smatch_state
*state
;
593 if (unmatched_stree
) {
594 state
= get_state_stree(unmatched_stree
, SMATCH_EXTRA
, sm
->name
, sm
->sym
);
598 if (parent_is_gone_var_sym(sm
->name
, sm
->sym
))
599 return alloc_estate_empty();
600 return alloc_estate_whole(estate_type(sm
->state
));
603 static void clear_the_pointed_at(struct expression
*expr
)
608 struct sm_state
*tmp
;
610 name
= expr_to_var_sym(expr
, &sym
);
614 stree
= __get_cur_stree();
615 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
616 if (tmp
->name
[0] != '*')
620 if (strcmp(tmp
->name
+ 1, name
) != 0)
622 set_extra_mod(tmp
->name
, tmp
->sym
, alloc_estate_whole(estate_type(tmp
->state
)));
623 } END_FOR_EACH_SM(tmp
);
629 static void match_function_call(struct expression
*expr
)
631 struct expression
*arg
;
632 struct expression
*tmp
;
634 /* if we have the db this is handled in smatch_function_hooks.c */
637 if (inlinable(expr
->fn
))
640 FOR_EACH_PTR(expr
->args
, arg
) {
641 tmp
= strip_expr(arg
);
642 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
643 set_extra_expr_mod(tmp
->unop
, alloc_estate_whole(get_type(tmp
->unop
)));
645 clear_the_pointed_at(tmp
);
646 } END_FOR_EACH_PTR(arg
);
649 static int types_equiv_or_pointer(struct symbol
*one
, struct symbol
*two
)
653 if (one
->type
== SYM_PTR
&& two
->type
== SYM_PTR
)
655 return types_equiv(one
, two
);
658 static void save_chunk_info(struct expression
*left
, struct expression
*right
)
660 struct var_sym_list
*vsl
;
662 struct expression
*add_expr
;
668 if (right
->type
!= EXPR_BINOP
|| right
->op
!= '-')
670 if (!get_value(right
->left
, &sval
))
672 if (!expr_to_sym(right
->right
))
675 add_expr
= binop_expression(left
, '+', right
->right
);
676 type
= get_type(add_expr
);
679 name
= expr_to_chunk_sym_vsl(add_expr
, &sym
, &vsl
);
682 FOR_EACH_PTR(vsl
, vs
) {
683 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
684 } END_FOR_EACH_PTR(vs
);
686 set_state(SMATCH_EXTRA
, name
, sym
, alloc_estate_sval(sval_cast(type
, sval
)));
691 static void do_array_assign(struct expression
*left
, int op
, struct expression
*right
)
693 struct range_list
*rl
;
696 get_absolute_rl(right
, &rl
);
697 rl
= cast_rl(get_type(left
), rl
);
699 rl
= alloc_whole_rl(get_type(left
));
702 set_extra_array_mod(left
, alloc_estate_rl(rl
));
705 static void match_untracked_array(struct expression
*call
, int param
)
707 struct expression
*arg
;
709 arg
= get_argument_from_call_expr(call
->args
, param
);
710 arg
= strip_expr(arg
);
712 clear_array_states(arg
);
715 static void match_vanilla_assign(struct expression
*left
, struct expression
*right
)
717 struct range_list
*rl
= NULL
;
718 struct symbol
*right_sym
;
719 struct symbol
*left_type
;
720 struct symbol
*right_type
;
721 char *right_name
= NULL
;
725 struct smatch_state
*state
;
730 save_chunk_info(left
, right
);
732 name
= expr_to_var_sym(left
, &sym
);
735 do_array_assign(left
, '=', right
);
739 left_type
= get_type(left
);
740 right_type
= get_type(right
);
742 right_name
= expr_to_var_sym(right
, &right_sym
);
744 if (!__in_fake_assign
&&
745 !(right
->type
== EXPR_PREOP
&& right
->op
== '&') &&
746 right_name
&& right_sym
&&
747 types_equiv_or_pointer(left_type
, right_type
) &&
748 !has_symbol(right
, sym
)) {
749 set_equiv(left
, right
);
753 if (is_pointer(right
) && get_address_rl(right
, &rl
)) {
754 state
= alloc_estate_rl(rl
);
755 } else if (get_implied_rl(right
, &rl
)) {
756 rl
= cast_rl(left_type
, rl
);
757 state
= alloc_estate_rl(rl
);
758 if (get_hard_max(right
, &max
)) {
759 estate_set_hard_max(state
);
760 estate_set_fuzzy_max(state
, max
);
763 rl
= alloc_whole_rl(right_type
);
764 rl
= cast_rl(left_type
, rl
);
765 state
= alloc_estate_rl(rl
);
767 set_extra_mod(name
, sym
, state
);
769 free_string(right_name
);
772 static int op_remove_assign(int op
)
775 case SPECIAL_ADD_ASSIGN
:
777 case SPECIAL_SUB_ASSIGN
:
779 case SPECIAL_MUL_ASSIGN
:
781 case SPECIAL_DIV_ASSIGN
:
783 case SPECIAL_MOD_ASSIGN
:
785 case SPECIAL_AND_ASSIGN
:
787 case SPECIAL_OR_ASSIGN
:
789 case SPECIAL_XOR_ASSIGN
:
791 case SPECIAL_SHL_ASSIGN
:
792 return SPECIAL_LEFTSHIFT
;
793 case SPECIAL_SHR_ASSIGN
:
794 return SPECIAL_RIGHTSHIFT
;
800 static void match_assign(struct expression
*expr
)
802 struct range_list
*rl
= NULL
;
803 struct expression
*left
;
804 struct expression
*right
;
805 struct expression
*binop_expr
;
806 struct symbol
*left_type
;
807 struct symbol
*right_type
;
810 sval_t left_min
, left_max
;
811 sval_t right_min
, right_max
;
812 sval_t res_min
, res_max
;
814 left
= strip_expr(expr
->left
);
816 right
= strip_parens(expr
->right
);
817 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
818 right
= strip_parens(right
->left
);
820 if (expr
->op
== '=' && is_condition(expr
->right
))
821 return; /* handled in smatch_condition.c */
822 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
823 return; /* handled in smatch_function_hooks.c */
824 if (expr
->op
== '=') {
825 match_vanilla_assign(left
, right
);
829 name
= expr_to_var_sym(left
, &sym
);
833 left_type
= get_type(left
);
834 right_type
= get_type(right
);
836 res_min
= sval_type_min(left_type
);
837 res_max
= sval_type_max(left_type
);
840 case SPECIAL_ADD_ASSIGN
:
841 get_absolute_max(left
, &left_max
);
842 get_absolute_max(right
, &right_max
);
843 if (sval_binop_overflows(left_max
, '+', right_max
))
845 if (get_implied_min(left
, &left_min
) &&
846 !sval_is_negative_min(left_min
) &&
847 get_implied_min(right
, &right_min
) &&
848 !sval_is_negative_min(right_min
)) {
849 res_min
= sval_binop(left_min
, '+', right_min
);
850 res_min
= sval_cast(right_type
, res_min
);
852 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
854 res_max
= sval_binop(left_max
, '+', right_max
);
855 res_max
= sval_cast(right_type
, res_max
);
857 case SPECIAL_SUB_ASSIGN
:
858 if (get_implied_max(left
, &left_max
) &&
859 !sval_is_max(left_max
) &&
860 get_implied_min(right
, &right_min
) &&
861 !sval_is_min(right_min
)) {
862 res_max
= sval_binop(left_max
, '-', right_min
);
863 res_max
= sval_cast(right_type
, res_max
);
867 if (get_implied_min(left
, &left_min
) &&
868 !sval_is_min(left_min
) &&
869 get_implied_max(right
, &right_max
) &&
870 !sval_is_max(right_max
)) {
871 res_min
= sval_binop(left_min
, '-', right_max
);
872 res_min
= sval_cast(right_type
, res_min
);
875 case SPECIAL_AND_ASSIGN
:
876 case SPECIAL_MOD_ASSIGN
:
877 case SPECIAL_SHL_ASSIGN
:
878 case SPECIAL_SHR_ASSIGN
:
879 case SPECIAL_OR_ASSIGN
:
880 case SPECIAL_XOR_ASSIGN
:
881 case SPECIAL_MUL_ASSIGN
:
882 case SPECIAL_DIV_ASSIGN
:
883 binop_expr
= binop_expression(expr
->left
,
884 op_remove_assign(expr
->op
),
886 if (get_absolute_rl(binop_expr
, &rl
)) {
887 rl
= cast_rl(left_type
, rl
);
888 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
893 rl
= cast_rl(left_type
, alloc_rl(res_min
, res_max
));
894 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
899 static struct smatch_state
*increment_state(struct smatch_state
*state
)
901 sval_t min
= estate_min(state
);
902 sval_t max
= estate_max(state
);
904 if (!estate_rl(state
))
908 max
= sval_type_max(max
.type
);
910 if (!sval_is_min(min
) && !sval_is_max(min
))
912 if (!sval_is_min(max
) && !sval_is_max(max
))
914 return alloc_estate_range(min
, max
);
917 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
919 sval_t min
= estate_min(state
);
920 sval_t max
= estate_max(state
);
922 if (!estate_rl(state
))
926 min
= sval_type_min(min
.type
);
928 if (!sval_is_min(min
) && !sval_is_max(min
))
930 if (!sval_is_min(max
) && !sval_is_max(max
))
932 return alloc_estate_range(min
, max
);
935 static void unop_expr(struct expression
*expr
)
937 struct smatch_state
*state
;
939 if (expr
->smatch_flags
& Handled
)
943 case SPECIAL_INCREMENT
:
944 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
945 state
= increment_state(state
);
947 state
= alloc_estate_whole(get_type(expr
));
948 set_extra_expr_mod(expr
->unop
, state
);
950 case SPECIAL_DECREMENT
:
951 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
952 state
= decrement_state(state
);
954 state
= alloc_estate_whole(get_type(expr
));
955 set_extra_expr_mod(expr
->unop
, state
);
962 static void asm_expr(struct statement
*stmt
)
965 struct expression
*expr
;
969 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
971 case 0: /* identifier */
972 case 1: /* constraint */
975 case 2: /* expression */
977 type
= get_type(strip_expr(expr
));
978 set_extra_expr_mod(expr
, alloc_estate_whole(type
));
981 } END_FOR_EACH_PTR(expr
);
984 static void check_dereference(struct expression
*expr
)
986 if (outside_of_function())
988 set_extra_expr_nomod(expr
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
991 static void match_dereferences(struct expression
*expr
)
993 if (expr
->type
!= EXPR_PREOP
)
995 check_dereference(expr
->unop
);
998 static void match_pointer_as_array(struct expression
*expr
)
1000 if (!is_array(expr
))
1002 check_dereference(get_array_base(expr
));
1005 static void set_param_dereferenced(struct expression
*arg
, char *key
, char *unused
)
1010 name
= get_variable_from_key(arg
, key
, &sym
);
1014 set_extra_nomod(name
, sym
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
1020 static sval_t
add_one(sval_t sval
)
1026 static sval_t
sub_one(sval_t sval
)
1032 static void handle_comparison(struct symbol
*type
, struct expression
*left
, int op
, struct expression
*right
)
1034 struct range_list
*left_orig
;
1035 struct range_list
*left_true
;
1036 struct range_list
*left_false
;
1037 struct range_list
*right_orig
;
1038 struct range_list
*right_true
;
1039 struct range_list
*right_false
;
1040 struct smatch_state
*left_true_state
;
1041 struct smatch_state
*left_false_state
;
1042 struct smatch_state
*right_true_state
;
1043 struct smatch_state
*right_false_state
;
1044 sval_t min
, max
, dummy
, hard_max
;
1045 int left_postop
= 0;
1046 int right_postop
= 0;
1048 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
1049 if (left
->type
== EXPR_POSTOP
) {
1050 left
->smatch_flags
|= Handled
;
1051 left_postop
= left
->op
;
1053 left
= strip_expr(left
->unop
);
1055 while (left
->type
== EXPR_ASSIGNMENT
)
1056 left
= strip_expr(left
->left
);
1058 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
1059 if (right
->type
== EXPR_POSTOP
) {
1060 right
->smatch_flags
|= Handled
;
1061 right_postop
= right
->op
;
1063 right
= strip_expr(right
->unop
);
1066 if (get_implied_rl(left
, &left_orig
)) {
1067 left_orig
= cast_rl(type
, left_orig
);
1069 min
= sval_type_min(get_type(left
));
1070 max
= sval_type_max(get_type(left
));
1071 left_orig
= cast_rl(type
, alloc_rl(min
, max
));
1074 if (get_implied_rl(right
, &right_orig
)) {
1075 right_orig
= cast_rl(type
, right_orig
);
1077 min
= sval_type_min(get_type(right
));
1078 max
= sval_type_max(get_type(right
));
1079 right_orig
= cast_rl(type
, alloc_rl(min
, max
));
1081 min
= sval_type_min(type
);
1082 max
= sval_type_max(type
);
1084 left_true
= clone_rl(left_orig
);
1085 left_false
= clone_rl(left_orig
);
1086 right_true
= clone_rl(right_orig
);
1087 right_false
= clone_rl(right_orig
);
1091 case SPECIAL_UNSIGNED_LT
:
1092 left_true
= remove_range(left_orig
, rl_max(right_orig
), max
);
1093 if (!sval_is_min(rl_min(right_orig
))) {
1094 left_false
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
1097 right_true
= remove_range(right_orig
, min
, rl_min(left_orig
));
1098 if (!sval_is_max(rl_max(left_orig
)))
1099 right_false
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
1101 case SPECIAL_UNSIGNED_LTE
:
1103 if (!sval_is_max(rl_max(right_orig
)))
1104 left_true
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
1105 left_false
= remove_range(left_orig
, min
, rl_min(right_orig
));
1107 if (!sval_is_min(rl_min(left_orig
)))
1108 right_true
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
1109 right_false
= remove_range(right_orig
, rl_max(left_orig
), max
);
1111 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
1112 left_false
= remove_range(left_false
, rl_min(left_orig
), rl_min(left_orig
));
1113 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
1114 right_false
= remove_range(right_false
, rl_max(left_orig
), rl_max(left_orig
));
1117 if (!sval_is_max(rl_max(right_orig
))) {
1118 left_true
= remove_range(left_true
, add_one(rl_max(right_orig
)), max
);
1120 if (!sval_is_min(rl_min(right_orig
))) {
1121 left_true
= remove_range(left_true
, min
, sub_one(rl_min(right_orig
)));
1123 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
1124 left_false
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
1126 if (!sval_is_max(rl_max(left_orig
)))
1127 right_true
= remove_range(right_true
, add_one(rl_max(left_orig
)), max
);
1128 if (!sval_is_min(rl_min(left_orig
)))
1129 right_true
= remove_range(right_true
, min
, sub_one(rl_min(left_orig
)));
1130 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
1131 right_false
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
1133 case SPECIAL_UNSIGNED_GTE
:
1135 if (!sval_is_min(rl_min(right_orig
)))
1136 left_true
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
1137 left_false
= remove_range(left_orig
, rl_max(right_orig
), max
);
1139 if (!sval_is_max(rl_max(left_orig
)))
1140 right_true
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
1141 right_false
= remove_range(right_orig
, min
, rl_min(left_orig
));
1143 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
1144 right_false
= remove_range(right_false
, rl_min(left_orig
), rl_min(left_orig
));
1145 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
1146 left_false
= remove_range(left_false
, rl_max(left_orig
), rl_max(left_orig
));
1149 case SPECIAL_UNSIGNED_GT
:
1150 left_true
= remove_range(left_orig
, min
, rl_min(right_orig
));
1151 if (!sval_is_max(rl_max(right_orig
)))
1152 left_false
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
1154 right_true
= remove_range(right_orig
, rl_max(left_orig
), max
);
1155 if (!sval_is_min(rl_min(left_orig
)))
1156 right_false
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
1158 case SPECIAL_NOTEQUAL
:
1159 if (!sval_is_max(rl_max(right_orig
)))
1160 left_false
= remove_range(left_false
, add_one(rl_max(right_orig
)), max
);
1161 if (!sval_is_min(rl_min(right_orig
)))
1162 left_false
= remove_range(left_false
, min
, sub_one(rl_min(right_orig
)));
1163 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
1164 left_true
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
1166 if (!sval_is_max(rl_max(left_orig
)))
1167 right_false
= remove_range(right_false
, add_one(rl_max(left_orig
)), max
);
1168 if (!sval_is_min(rl_min(left_orig
)))
1169 right_false
= remove_range(right_false
, min
, sub_one(rl_min(left_orig
)));
1170 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
1171 right_true
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
1177 left_true
= rl_truncate_cast(get_type(left
), left_true
);
1178 left_false
= rl_truncate_cast(get_type(left
), left_false
);
1179 right_true
= rl_truncate_cast(get_type(right
), right_true
);
1180 right_false
= rl_truncate_cast(get_type(right
), right_false
);
1182 left_true_state
= alloc_estate_rl(left_true
);
1183 left_false_state
= alloc_estate_rl(left_false
);
1184 right_true_state
= alloc_estate_rl(right_true
);
1185 right_false_state
= alloc_estate_rl(right_false
);
1189 case SPECIAL_UNSIGNED_LT
:
1190 case SPECIAL_UNSIGNED_LTE
:
1192 if (get_hard_max(right
, &dummy
))
1193 estate_set_hard_max(left_true_state
);
1194 if (get_hard_max(left
, &dummy
))
1195 estate_set_hard_max(right_false_state
);
1198 case SPECIAL_UNSIGNED_GT
:
1199 case SPECIAL_UNSIGNED_GTE
:
1201 if (get_hard_max(left
, &dummy
))
1202 estate_set_hard_max(right_true_state
);
1203 if (get_hard_max(right
, &dummy
))
1204 estate_set_hard_max(left_false_state
);
1210 case SPECIAL_UNSIGNED_LT
:
1211 case SPECIAL_UNSIGNED_LTE
:
1213 if (get_hard_max(right
, &hard_max
)) {
1214 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1216 estate_set_fuzzy_max(left_true_state
, hard_max
);
1218 if (get_implied_value(right
, &hard_max
)) {
1219 if (op
== SPECIAL_UNSIGNED_LTE
||
1222 estate_set_fuzzy_max(left_false_state
, hard_max
);
1224 if (get_hard_max(left
, &hard_max
)) {
1225 if (op
== SPECIAL_UNSIGNED_LTE
||
1228 estate_set_fuzzy_max(right_false_state
, hard_max
);
1230 if (get_implied_value(left
, &hard_max
)) {
1231 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1233 estate_set_fuzzy_max(right_true_state
, hard_max
);
1237 case SPECIAL_UNSIGNED_GT
:
1238 case SPECIAL_UNSIGNED_GTE
:
1240 if (get_hard_max(left
, &hard_max
)) {
1241 if (op
== '>' || op
== SPECIAL_UNSIGNED_GT
)
1243 estate_set_fuzzy_max(right_true_state
, hard_max
);
1245 if (get_implied_value(left
, &hard_max
)) {
1246 if (op
== SPECIAL_UNSIGNED_GTE
||
1249 estate_set_fuzzy_max(right_false_state
, hard_max
);
1251 if (get_hard_max(right
, &hard_max
)) {
1252 if (op
== SPECIAL_UNSIGNED_LTE
||
1255 estate_set_fuzzy_max(left_false_state
, hard_max
);
1257 if (get_implied_value(right
, &hard_max
)) {
1259 op
== SPECIAL_UNSIGNED_GT
)
1261 estate_set_fuzzy_max(left_true_state
, hard_max
);
1265 if (get_hard_max(left
, &hard_max
))
1266 estate_set_fuzzy_max(right_true_state
, hard_max
);
1267 if (get_hard_max(right
, &hard_max
))
1268 estate_set_fuzzy_max(left_true_state
, hard_max
);
1272 if (get_hard_max(left
, &hard_max
)) {
1273 estate_set_hard_max(left_true_state
);
1274 estate_set_hard_max(left_false_state
);
1276 if (get_hard_max(right
, &hard_max
)) {
1277 estate_set_hard_max(right_true_state
);
1278 estate_set_hard_max(right_false_state
);
1281 if (left_postop
== SPECIAL_INCREMENT
) {
1282 left_true_state
= increment_state(left_true_state
);
1283 left_false_state
= increment_state(left_false_state
);
1285 if (left_postop
== SPECIAL_DECREMENT
) {
1286 left_true_state
= decrement_state(left_true_state
);
1287 left_false_state
= decrement_state(left_false_state
);
1289 if (right_postop
== SPECIAL_INCREMENT
) {
1290 right_true_state
= increment_state(right_true_state
);
1291 right_false_state
= increment_state(right_false_state
);
1293 if (right_postop
== SPECIAL_DECREMENT
) {
1294 right_true_state
= decrement_state(right_true_state
);
1295 right_false_state
= decrement_state(right_false_state
);
1298 if (estate_rl(left_true_state
) && estates_equiv(left_true_state
, left_false_state
)) {
1299 left_true_state
= NULL
;
1300 left_false_state
= NULL
;
1303 if (estate_rl(right_true_state
) && estates_equiv(right_true_state
, right_false_state
)) {
1304 right_true_state
= NULL
;
1305 right_false_state
= NULL
;
1308 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
1309 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
1312 static int is_simple_math(struct expression
*expr
)
1316 if (expr
->type
!= EXPR_BINOP
)
1327 static void move_known_values(struct expression
**left_p
, struct expression
**right_p
)
1329 struct expression
*left
= *left_p
;
1330 struct expression
*right
= *right_p
;
1333 if (get_implied_value(left
, &sval
)) {
1334 if (!is_simple_math(right
))
1336 if (right
->op
== '*') {
1339 if (!get_value(right
->right
, &divisor
))
1341 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1343 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1344 *right_p
= right
->left
;
1347 if (right
->op
== '+' && get_value(right
->left
, &sval
)) {
1348 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->left
);
1349 *right_p
= right
->right
;
1352 if (get_value(right
->right
, &sval
)) {
1353 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1354 *right_p
= right
->left
;
1359 if (get_implied_value(right
, &sval
)) {
1360 if (!is_simple_math(left
))
1362 if (left
->op
== '*') {
1365 if (!get_value(left
->right
, &divisor
))
1367 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1369 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1370 *left_p
= left
->left
;
1373 if (left
->op
== '+' && get_value(left
->left
, &sval
)) {
1374 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->left
);
1375 *left_p
= left
->right
;
1379 if (get_value(left
->right
, &sval
)) {
1380 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1381 *left_p
= left
->left
;
1388 static int match_func_comparison(struct expression
*expr
)
1390 struct expression
*left
= strip_expr(expr
->left
);
1391 struct expression
*right
= strip_expr(expr
->right
);
1395 * fixme: think about this harder. We should always be trying to limit
1396 * the non-call side as well. If we can't determine the limitter does
1397 * that mean we aren't querying the database and are missing important
1401 if (left
->type
== EXPR_CALL
) {
1402 if (get_implied_value(left
, &sval
)) {
1403 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1406 function_comparison(left
, expr
->op
, right
);
1410 if (right
->type
== EXPR_CALL
) {
1411 if (get_implied_value(right
, &sval
)) {
1412 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1415 function_comparison(left
, expr
->op
, right
);
1422 static void match_comparison(struct expression
*expr
)
1424 struct expression
*left_orig
= strip_expr(expr
->left
);
1425 struct expression
*right_orig
= strip_expr(expr
->right
);
1426 struct expression
*left
, *right
;
1427 struct expression
*prev
;
1428 struct symbol
*type
;
1430 if (match_func_comparison(expr
))
1433 type
= get_type(expr
);
1435 type
= &llong_ctype
;
1439 move_known_values(&left
, &right
);
1440 handle_comparison(type
, left
, expr
->op
, right
);
1442 prev
= get_assigned_expr(left_orig
);
1443 if (is_simple_math(prev
) && has_variable(prev
, left_orig
) == 0) {
1446 move_known_values(&left
, &right
);
1447 handle_comparison(type
, left
, expr
->op
, right
);
1450 prev
= get_assigned_expr(right_orig
);
1451 if (is_simple_math(prev
) && has_variable(prev
, right_orig
) == 0) {
1454 move_known_values(&left
, &right
);
1455 handle_comparison(type
, left
, expr
->op
, right
);
1459 static void handle_AND_condition(struct expression
*expr
)
1461 struct range_list
*rl
= NULL
;
1464 if (get_implied_value(expr
->left
, &known
) && known
.value
> 0) {
1466 get_absolute_rl(expr
->right
, &rl
);
1467 rl
= remove_range(rl
, sval_type_val(known
.type
, 0), known
);
1468 set_extra_expr_true_false(expr
->right
, alloc_estate_rl(rl
), NULL
);
1472 if (get_implied_value(expr
->right
, &known
) && known
.value
> 0) {
1474 get_absolute_rl(expr
->left
, &rl
);
1475 rl
= remove_range(rl
, sval_type_val(known
.type
, 0), known
);
1476 set_extra_expr_true_false(expr
->left
, alloc_estate_rl(rl
), NULL
);
1481 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1482 void __extra_match_condition(struct expression
*expr
)
1484 struct smatch_state
*pre_state
;
1485 struct smatch_state
*true_state
;
1486 struct smatch_state
*false_state
;
1488 expr
= strip_expr(expr
);
1489 switch (expr
->type
) {
1491 function_comparison(expr
, SPECIAL_NOTEQUAL
, zero_expr());
1498 zero
= sval_blank(expr
);
1501 pre_state
= get_extra_state(expr
);
1502 true_state
= estate_filter_sval(pre_state
, zero
);
1503 if (possibly_true(expr
, SPECIAL_EQUAL
, zero_expr()))
1504 false_state
= alloc_estate_sval(zero
);
1506 false_state
= alloc_estate_empty();
1507 set_extra_expr_true_false(expr
, true_state
, false_state
);
1511 match_comparison(expr
);
1513 case EXPR_ASSIGNMENT
:
1514 __extra_match_condition(expr
->left
);
1517 if (expr
->op
== '&')
1518 handle_AND_condition(expr
);
1523 static void assume_indexes_are_valid(struct expression
*expr
)
1525 struct expression
*array_expr
;
1527 struct expression
*offset
;
1528 struct symbol
*offset_type
;
1529 struct range_list
*rl_before
;
1530 struct range_list
*rl_after
;
1531 struct range_list
*filter
= NULL
;
1534 expr
= strip_expr(expr
);
1535 if (!is_array(expr
))
1538 offset
= get_array_offset(expr
);
1539 offset_type
= get_type(offset
);
1540 if (offset_type
&& type_signed(offset_type
)) {
1541 filter
= alloc_rl(sval_type_min(offset_type
),
1542 sval_type_val(offset_type
, -1));
1545 array_expr
= get_array_base(expr
);
1546 array_size
= get_real_array_size(array_expr
);
1547 if (array_size
> 1) {
1548 size
= sval_type_val(offset_type
, array_size
);
1549 add_range(&filter
, size
, sval_type_max(offset_type
));
1554 get_absolute_rl(offset
, &rl_before
);
1555 rl_after
= rl_filter(rl_before
, filter
);
1556 if (rl_equiv(rl_before
, rl_after
))
1558 set_extra_expr_nomod(offset
, alloc_estate_rl(rl_after
));
1561 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1562 int implied_not_equal(struct expression
*expr
, long long val
)
1564 return !possibly_false(expr
, SPECIAL_NOTEQUAL
, value_expr(val
));
1567 int implied_not_equal_name_sym(char *name
, struct symbol
*sym
, long long val
)
1569 struct smatch_state
*estate
;
1571 estate
= get_state(SMATCH_EXTRA
, name
, sym
);
1574 if (!rl_has_sval(estate_rl(estate
), sval_type_val(estate_type(estate
), 0)))
1579 int parent_is_null_var_sym(const char *name
, struct symbol
*sym
)
1584 struct smatch_state
*state
;
1586 strncpy(buf
, name
, sizeof(buf
) - 1);
1587 buf
[sizeof(buf
) - 1] = '\0';
1590 while (*start
== '*') {
1592 state
= get_state(SMATCH_EXTRA
, start
, sym
);
1595 if (!estate_rl(state
))
1597 if (estate_min(state
).value
== 0 &&
1598 estate_max(state
).value
== 0)
1603 while (*start
== '&')
1606 while ((end
= strrchr(start
, '-'))) {
1608 state
= get_state(SMATCH_EXTRA
, start
, sym
);
1611 if (estate_min(state
).value
== 0 &&
1612 estate_max(state
).value
== 0)
1618 int parent_is_null(struct expression
*expr
)
1624 expr
= strip_expr(expr
);
1625 var
= expr_to_var_sym(expr
, &sym
);
1628 ret
= parent_is_null_var_sym(var
, sym
);
1634 static int param_used_callback(void *found
, int argc
, char **argv
, char **azColName
)
1640 static int filter_unused_kzalloc_info(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
1645 /* for function pointers assume everything is used */
1646 if (call
->fn
->type
!= EXPR_SYMBOL
)
1650 * kzalloc() information is treated as special because so there is just
1651 * a lot of stuff initialized to zero and it makes building the database
1652 * take hours and hours.
1654 * In theory, we should just remove this line and not pass any unused
1655 * information, but I'm not sure enough that this code works so I want
1656 * to hold off on that for now.
1658 if (!estate_get_single_value(sm
->state
, &sval
) || sval
.value
!= 0)
1661 run_sql(¶m_used_callback
, &found
,
1662 "select * from call_implies where %s and type = %d and parameter = %d and key = '%s';",
1663 get_static_filter(call
->fn
->symbol
), PARAM_USED
, param
, printed_name
);
1667 /* If the database is not built yet, then assume everything is used */
1668 run_sql(¶m_used_callback
, &found
,
1669 "select * from call_implies where %s and type = %d;",
1670 get_static_filter(call
->fn
->symbol
), PARAM_USED
);
1677 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
1679 if (estate_is_whole(sm
->state
))
1681 if (filter_unused_kzalloc_info(call
, param
, printed_name
, sm
))
1683 sql_insert_caller_info(call
, PARAM_VALUE
, param
, printed_name
, sm
->state
->name
);
1684 if (estate_has_fuzzy_max(sm
->state
))
1685 sql_insert_caller_info(call
, FUZZY_MAX
, param
, printed_name
,
1686 sval_to_str(estate_get_fuzzy_max(sm
->state
)));
1689 static void db_limited_before(void)
1691 unmatched_stree
= clone_stree(__get_cur_stree());
1694 static void db_limited_after(void)
1696 free_stree(&unmatched_stree
);
1699 static void db_param_limit_filter(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
1701 struct expression
*arg
;
1704 struct sm_state
*sm
;
1705 struct symbol
*compare_type
, *var_type
;
1706 struct range_list
*rl
;
1707 struct range_list
*limit
;
1708 struct range_list
*new;
1710 while (expr
->type
== EXPR_ASSIGNMENT
)
1711 expr
= strip_expr(expr
->right
);
1712 if (expr
->type
!= EXPR_CALL
)
1715 arg
= get_argument_from_call_expr(expr
->args
, param
);
1719 name
= get_variable_from_key(arg
, key
, &sym
);
1723 if (strcmp(key
, "$") == 0)
1724 compare_type
= get_arg_type(expr
->fn
, param
);
1726 compare_type
= get_member_type_from_key(arg
, key
);
1728 sm
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
1730 rl
= estate_rl(sm
->state
);
1732 rl
= alloc_whole_rl(compare_type
);
1734 call_results_to_rl(expr
, compare_type
, value
, &limit
);
1735 new = rl_intersection(rl
, limit
);
1737 var_type
= get_member_type_from_key(arg
, key
);
1738 new = cast_rl(var_type
, new);
1740 /* We want to preserve the implications here */
1741 if (sm
&& rl_equiv(estate_rl(sm
->state
), new))
1744 if (op
== PARAM_LIMIT
)
1745 set_extra_nomod(name
, sym
, alloc_estate_rl(new));
1747 set_extra_mod(name
, sym
, alloc_estate_rl(new));
1754 static void db_param_limit(struct expression
*expr
, int param
, char *key
, char *value
)
1756 db_param_limit_filter(expr
, param
, key
, value
, PARAM_LIMIT
);
1759 static void db_param_filter(struct expression
*expr
, int param
, char *key
, char *value
)
1761 db_param_limit_filter(expr
, param
, key
, value
, PARAM_FILTER
);
1764 static void db_param_add_set(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
1766 struct expression
*arg
;
1769 struct symbol
*type
;
1770 struct smatch_state
*state
;
1771 struct range_list
*new = NULL
;
1772 struct range_list
*added
= NULL
;
1774 while (expr
->type
== EXPR_ASSIGNMENT
)
1775 expr
= strip_expr(expr
->right
);
1776 if (expr
->type
!= EXPR_CALL
)
1779 arg
= get_argument_from_call_expr(expr
->args
, param
);
1782 type
= get_member_type_from_key(arg
, key
);
1783 name
= get_variable_from_key(arg
, key
, &sym
);
1787 state
= get_state(SMATCH_EXTRA
, name
, sym
);
1789 new = estate_rl(state
);
1791 call_results_to_rl(expr
, type
, value
, &added
);
1793 if (op
== PARAM_SET
)
1796 new = rl_union(new, added
);
1798 set_extra_mod(name
, sym
, alloc_estate_rl(new));
1803 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
1805 db_param_add_set(expr
, param
, key
, value
, PARAM_ADD
);
1808 static void db_param_set(struct expression
*expr
, int param
, char *key
, char *value
)
1810 db_param_add_set(expr
, param
, key
, value
, PARAM_SET
);
1813 static void db_returned_member_info(struct expression
*expr
, int param
, char *key
, char *value
)
1815 struct symbol
*type
;
1818 struct range_list
*rl
;
1820 if (expr
->type
!= EXPR_ASSIGNMENT
)
1823 name
= return_state_to_var_sym(expr
, param
, key
, &sym
);
1826 type
= get_member_type_from_key(expr
->left
, key
);
1829 call_results_to_rl(expr
->right
, type
, value
, &rl
);
1830 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
1836 static char *in_terms_of_param_math(struct expression
*expr
, char *printed_name
)
1843 var
= expr_to_var_sym(expr
, &sym
);
1846 snprintf(buf
, sizeof(buf
), "%s%s", var
, printed_name
+ 2);
1848 ret
= get_value_in_terms_of_parameter_math_var_sym(buf
, sym
);
1855 static void returned_member_callback(int return_id
, char *return_ranges
, struct expression
*expr
, char *printed_name
, struct smatch_state
*state
)
1861 /* these are handled in smatch_param_filter/set/limit.c */
1862 if (printed_name
[0] != '*' &&
1863 !strchr(printed_name
, '.') && !strchr(printed_name
, '-'))
1866 if (estate_get_single_value(state
, &sval
)) {
1867 sql_insert_return_states(return_id
, return_ranges
, RETURN_VALUE
, -1,
1868 printed_name
, sval_to_str(sval
));
1872 math_str
= in_terms_of_param_math(expr
, printed_name
);
1874 snprintf(buf
, sizeof(buf
), "%s[%s]", state
->name
, math_str
);
1875 sql_insert_return_states(return_id
, return_ranges
, RETURN_VALUE
, -1,
1880 if (estate_is_whole(state
))
1883 sql_insert_return_states(return_id
, return_ranges
, RETURN_VALUE
, -1,
1884 printed_name
, state
->name
);
1887 static void match_call_info(struct expression
*expr
)
1889 struct smatch_state
*state
;
1890 struct range_list
*rl
= NULL
;
1891 struct expression
*arg
;
1892 struct symbol
*type
;
1895 FOR_EACH_PTR(expr
->args
, arg
) {
1896 type
= get_arg_type(expr
->fn
, i
);
1898 if (get_implied_rl(arg
, &rl
))
1899 rl
= cast_rl(type
, rl
);
1901 rl
= cast_rl(type
, alloc_whole_rl(get_type(arg
)));
1903 if (!is_whole_rl(rl
))
1904 sql_insert_caller_info(expr
, PARAM_VALUE
, i
, "$", show_rl(rl
));
1905 state
= get_state_expr(SMATCH_EXTRA
, arg
);
1906 if (estate_has_fuzzy_max(state
)) {
1907 sql_insert_caller_info(expr
, FUZZY_MAX
, i
, "$",
1908 sval_to_str(estate_get_fuzzy_max(state
)));
1911 } END_FOR_EACH_PTR(arg
);
1914 static void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1916 struct range_list
*rl
= NULL
;
1917 struct smatch_state
*state
;
1918 struct symbol
*type
;
1921 if (strcmp(key
, "*$") == 0)
1922 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
1923 else if (strncmp(key
, "$", 1) == 0)
1924 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
1928 type
= get_member_type_from_key(symbol_expression(sym
), key
);
1929 str_to_rl(type
, value
, &rl
);
1930 state
= alloc_estate_rl(rl
);
1931 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
1934 static void set_param_hard_max(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1936 struct range_list
*rl
= NULL
;
1937 struct smatch_state
*state
;
1938 struct symbol
*type
;
1942 if (strcmp(key
, "*$") == 0)
1943 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
1944 else if (strncmp(key
, "$", 1) == 0)
1945 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
1949 state
= get_state(SMATCH_EXTRA
, fullname
, sym
);
1952 type
= get_member_type_from_key(symbol_expression(sym
), key
);
1953 str_to_rl(type
, value
, &rl
);
1954 if (!rl_to_sval(rl
, &max
))
1956 estate_set_fuzzy_max(state
, max
);
1959 struct smatch_state
*get_extra_state(struct expression
*expr
)
1963 struct smatch_state
*ret
= NULL
;
1964 struct range_list
*rl
;
1966 if (is_pointer(expr
) && get_address_rl(expr
, &rl
))
1967 return alloc_estate_rl(rl
);
1969 name
= expr_to_known_chunk_sym(expr
, &sym
);
1973 ret
= get_state(SMATCH_EXTRA
, name
, sym
);
1979 void register_smatch_extra(int id
)
1983 add_merge_hook(my_id
, &merge_estates
);
1984 add_unmatched_state_hook(my_id
, &unmatched_state
);
1985 select_caller_info_hook(set_param_value
, PARAM_VALUE
);
1986 select_caller_info_hook(set_param_hard_max
, FUZZY_MAX
);
1987 select_return_states_hook(RETURN_VALUE
, &db_returned_member_info
);
1988 select_return_states_before(&db_limited_before
);
1989 select_return_states_hook(PARAM_LIMIT
, &db_param_limit
);
1990 select_return_states_hook(PARAM_FILTER
, &db_param_filter
);
1991 select_return_states_hook(PARAM_ADD
, &db_param_add
);
1992 select_return_states_hook(PARAM_SET
, &db_param_set
);
1993 select_return_states_after(&db_limited_after
);
1996 static void match_link_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
1998 struct var_sym_list
*links
;
1999 struct var_sym
*tmp
;
2000 struct smatch_state
*state
;
2002 links
= sm
->state
->data
;
2004 FOR_EACH_PTR(links
, tmp
) {
2005 state
= get_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
);
2008 set_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
, alloc_estate_whole(estate_type(state
)));
2009 } END_FOR_EACH_PTR(tmp
);
2010 set_state(link_id
, sm
->name
, sm
->sym
, &undefined
);
2013 void register_smatch_extra_links(int id
)
2018 void register_smatch_extra_late(int id
)
2020 add_merge_hook(link_id
, &merge_link_states
);
2021 add_modification_hook(link_id
, &match_link_modify
);
2022 add_hook(&match_dereferences
, DEREF_HOOK
);
2023 add_hook(&match_pointer_as_array
, OP_HOOK
);
2024 select_call_implies_hook(DEREFERENCE
, &set_param_dereferenced
);
2025 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
2026 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
2027 add_hook(&match_assign
, GLOBAL_ASSIGNMENT_HOOK
);
2028 add_hook(&unop_expr
, OP_HOOK
);
2029 add_hook(&asm_expr
, ASM_HOOK
);
2030 add_untracked_param_hook(&match_untracked_array
);
2032 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2033 add_member_info_callback(my_id
, struct_member_callback
);
2034 add_returned_member_callback(my_id
, returned_member_callback
);
2036 add_hook(&assume_indexes_are_valid
, OP_HOOK
);