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 sm
= set_extra_mod_helper(name
, sym
, state
);
145 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
146 if (new_name
&& new_sym
)
147 set_extra_mod_helper(new_name
, new_sym
, state
);
148 free_string(new_name
);
152 static void clear_array_states(struct expression
*array
)
156 sm
= get_sm_state_expr(link_id
, array
);
158 match_link_modify(sm
, NULL
);
161 static struct sm_state
*set_extra_array_mod(struct expression
*expr
, struct smatch_state
*state
)
163 struct expression
*array
;
164 struct expression
*offset
;
165 struct var_sym_list
*vsl
;
170 struct sm_state
*ret
= NULL
;
172 array
= get_array_base(expr
);
173 offset
= get_array_offset(expr
);
175 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
177 if (!name
|| !vsl
|| !get_value(offset
, &sval
)) {
178 clear_array_states(array
);
182 FOR_EACH_PTR(vsl
, vs
) {
183 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
184 } END_FOR_EACH_PTR(vs
);
186 ret
= set_state(SMATCH_EXTRA
, name
, sym
, state
);
192 struct sm_state
*set_extra_expr_mod(struct expression
*expr
, struct smatch_state
*state
)
196 struct sm_state
*ret
= NULL
;
199 return set_extra_array_mod(expr
, state
);
201 expr
= strip_expr(expr
);
202 name
= expr_to_var_sym(expr
, &sym
);
205 ret
= set_extra_mod(name
, sym
, state
);
211 void set_extra_nomod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
214 struct symbol
*new_sym
;
215 struct relation
*rel
;
216 struct smatch_state
*orig_state
;
218 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
220 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
221 if (new_name
&& new_sym
)
222 set_state(SMATCH_EXTRA
, new_name
, new_sym
, state
);
223 free_string(new_name
);
225 if (!estate_related(orig_state
)) {
226 set_state(SMATCH_EXTRA
, name
, sym
, state
);
230 set_related(state
, estate_related(orig_state
));
231 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
232 if (option_debug_related
)
233 sm_msg("%s updating related %s to %s", name
, rel
->name
, state
->name
);
234 set_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
, state
);
235 } END_FOR_EACH_PTR(rel
);
239 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
241 void set_extra_expr_nomod(struct expression
*expr
, struct smatch_state
*state
)
246 name
= expr_to_var_sym(expr
, &sym
);
249 set_extra_nomod(name
, sym
, state
);
255 static void set_extra_true_false(const char *name
, struct symbol
*sym
,
256 struct smatch_state
*true_state
,
257 struct smatch_state
*false_state
)
260 struct symbol
*new_sym
;
261 struct relation
*rel
;
262 struct smatch_state
*orig_state
;
264 if (in_warn_on_macro())
267 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
268 if (new_name
&& new_sym
)
269 set_true_false_states(SMATCH_EXTRA
, new_name
, new_sym
, true_state
, false_state
);
270 free_string(new_name
);
272 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
274 if (!estate_related(orig_state
)) {
275 set_true_false_states(SMATCH_EXTRA
, name
, sym
, true_state
, false_state
);
280 set_related(true_state
, estate_related(orig_state
));
282 set_related(false_state
, estate_related(orig_state
));
284 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
285 set_true_false_states(SMATCH_EXTRA
, rel
->name
, rel
->sym
,
286 true_state
, false_state
);
287 } END_FOR_EACH_PTR(rel
);
290 static void set_extra_chunk_true_false(struct expression
*expr
,
291 struct smatch_state
*true_state
,
292 struct smatch_state
*false_state
)
294 struct var_sym_list
*vsl
;
300 if (in_warn_on_macro())
303 type
= get_type(expr
);
307 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
310 FOR_EACH_PTR(vsl
, vs
) {
311 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
312 } END_FOR_EACH_PTR(vs
);
314 set_true_false_states(SMATCH_EXTRA
, name
, sym
,
315 clone_estate(true_state
),
316 clone_estate(false_state
));
321 static void set_extra_expr_true_false(struct expression
*expr
,
322 struct smatch_state
*true_state
,
323 struct smatch_state
*false_state
)
329 if (get_value(expr
, &sval
))
332 expr
= strip_expr(expr
);
333 name
= expr_to_var_sym(expr
, &sym
);
336 set_extra_chunk_true_false(expr
, true_state
, false_state
);
339 set_extra_true_false(name
, sym
, true_state
, false_state
);
343 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
345 struct expression
*iter_var
;
346 struct expression
*condition
;
348 struct smatch_state
*estate
;
351 condition
= strip_expr(loop
->iterator_pre_condition
);
354 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
356 if (condition
->op
!= SPECIAL_DECREMENT
)
359 iter_var
= condition
->unop
;
360 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
363 if (sval_cmp_val(estate_min(sm
->state
), 0) < 0)
365 start
= estate_max(sm
->state
);
366 if (sval_cmp_val(start
, 0) <= 0)
368 if (!sval_is_max(start
))
371 if (condition
->type
== EXPR_PREOP
) {
372 estate
= alloc_estate_range(sval_type_val(start
.type
, 1), start
);
373 if (estate_has_hard_max(sm
->state
))
374 estate_set_hard_max(estate
);
375 estate_copy_fuzzy_max(estate
, sm
->state
);
376 set_extra_expr_mod(iter_var
, estate
);
378 if (condition
->type
== EXPR_POSTOP
) {
379 estate
= alloc_estate_range(sval_type_val(start
.type
, 0), start
);
380 if (estate_has_hard_max(sm
->state
))
381 estate_set_hard_max(estate
);
382 estate_copy_fuzzy_max(estate
, sm
->state
);
383 set_extra_expr_mod(iter_var
, estate
);
385 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
388 static struct sm_state
*handle_canonical_for_inc(struct expression
*iter_expr
,
389 struct expression
*condition
)
391 struct expression
*iter_var
;
393 struct smatch_state
*estate
;
394 sval_t start
, end
, max
;
396 iter_var
= iter_expr
->unop
;
397 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
400 if (!estate_get_single_value(sm
->state
, &start
))
402 if (get_implied_max(condition
->right
, &end
))
403 end
= sval_cast(get_type(iter_var
), end
);
405 end
= sval_type_max(get_type(iter_var
));
407 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
410 switch (condition
->op
) {
411 case SPECIAL_UNSIGNED_LT
:
412 case SPECIAL_NOTEQUAL
:
414 if (!sval_is_min(end
))
417 case SPECIAL_UNSIGNED_LTE
:
423 if (sval_cmp(end
, start
) < 0)
425 estate
= alloc_estate_range(start
, end
);
426 if (get_hard_max(condition
->right
, &max
)) {
427 estate_set_hard_max(estate
);
428 if (condition
->op
== '<' ||
429 condition
->op
== SPECIAL_UNSIGNED_LT
||
430 condition
->op
== SPECIAL_NOTEQUAL
)
432 estate_set_fuzzy_max(estate
, max
);
434 set_extra_expr_mod(iter_var
, estate
);
435 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
438 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
439 struct expression
*condition
)
441 struct expression
*iter_var
;
443 struct smatch_state
*estate
;
446 iter_var
= iter_expr
->unop
;
447 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
450 if (!estate_get_single_value(sm
->state
, &start
))
452 if (!get_implied_min(condition
->right
, &end
))
453 end
= sval_type_min(get_type(iter_var
));
454 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
457 switch (condition
->op
) {
458 case SPECIAL_NOTEQUAL
:
460 if (!sval_is_min(end
) && !sval_is_max(end
))
468 if (sval_cmp(end
, start
) > 0)
470 estate
= alloc_estate_range(end
, start
);
471 estate_set_hard_max(estate
);
472 estate_set_fuzzy_max(estate
, estate_get_fuzzy_max(estate
));
473 set_extra_expr_mod(iter_var
, estate
);
474 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
477 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
479 struct expression
*iter_expr
;
480 struct expression
*condition
;
482 if (!loop
->iterator_post_statement
)
484 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
486 iter_expr
= loop
->iterator_post_statement
->expression
;
487 if (!loop
->iterator_pre_condition
)
489 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
491 condition
= loop
->iterator_pre_condition
;
493 if (iter_expr
->op
== SPECIAL_INCREMENT
)
494 return handle_canonical_for_inc(iter_expr
, condition
);
495 if (iter_expr
->op
== SPECIAL_DECREMENT
)
496 return handle_canonical_for_dec(iter_expr
, condition
);
500 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct stree
**stree
)
502 struct sm_state
*ret
;
504 __push_fake_cur_stree();
505 if (!loop
->iterator_post_statement
)
506 ret
= handle_canonical_while_count_down(loop
);
508 ret
= handle_canonical_for_loops(loop
);
509 *stree
= __pop_fake_cur_stree();
513 int __iterator_unchanged(struct sm_state
*sm
)
517 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
522 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
526 /* paranoid checking. prolly not needed */
527 condition
= strip_expr(condition
);
530 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
532 if (condition
->op
!= SPECIAL_DECREMENT
)
534 after_value
= estate_min(sm
->state
);
536 set_extra_mod(sm
->name
, sm
->sym
, alloc_estate_sval(after_value
));
539 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
540 struct statement
*iterator
,
541 struct expression
*condition
)
543 struct expression
*iter_expr
;
545 struct smatch_state
*state
;
548 while_count_down_after(sm
, condition
);
552 iter_expr
= iterator
->expression
;
554 if (condition
->type
!= EXPR_COMPARE
)
556 if (iter_expr
->op
== SPECIAL_INCREMENT
) {
557 limit
= sval_binop(estate_max(sm
->state
), '+',
558 sval_type_val(estate_type(sm
->state
), 1));
560 limit
= sval_binop(estate_min(sm
->state
), '-',
561 sval_type_val(estate_type(sm
->state
), 1));
563 if (!estate_has_hard_max(sm
->state
) && !__has_breaks()) {
564 if (iter_expr
->op
== SPECIAL_INCREMENT
)
565 state
= alloc_estate_range(estate_min(sm
->state
), limit
);
567 state
= alloc_estate_range(limit
, estate_max(sm
->state
));
569 state
= alloc_estate_sval(limit
);
571 if (!estate_has_hard_max(sm
->state
)) {
572 estate_clear_hard_max(state
);
574 if (estate_has_fuzzy_max(sm
->state
)) {
575 sval_t hmax
= estate_get_fuzzy_max(sm
->state
);
576 sval_t max
= estate_max(sm
->state
);
578 if (sval_cmp(hmax
, max
) != 0)
579 estate_clear_fuzzy_max(state
);
580 } else if (!estate_has_fuzzy_max(sm
->state
)) {
581 estate_clear_fuzzy_max(state
);
584 set_extra_mod(sm
->name
, sm
->sym
, state
);
587 static struct stree
*unmatched_stree
;
588 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
590 struct smatch_state
*state
;
592 if (unmatched_stree
) {
593 state
= get_state_stree(unmatched_stree
, SMATCH_EXTRA
, sm
->name
, sm
->sym
);
597 if (parent_is_gone_var_sym(sm
->name
, sm
->sym
))
598 return alloc_estate_empty();
599 return alloc_estate_whole(estate_type(sm
->state
));
602 static void clear_the_pointed_at(struct expression
*expr
)
607 struct sm_state
*tmp
;
609 name
= expr_to_var_sym(expr
, &sym
);
613 stree
= __get_cur_stree();
614 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
615 if (tmp
->name
[0] != '*')
619 if (strcmp(tmp
->name
+ 1, name
) != 0)
621 set_extra_mod(tmp
->name
, tmp
->sym
, alloc_estate_whole(estate_type(tmp
->state
)));
622 } END_FOR_EACH_SM(tmp
);
628 static void match_function_call(struct expression
*expr
)
630 struct expression
*arg
;
631 struct expression
*tmp
;
633 /* if we have the db this is handled in smatch_function_hooks.c */
636 if (inlinable(expr
->fn
))
639 FOR_EACH_PTR(expr
->args
, arg
) {
640 tmp
= strip_expr(arg
);
641 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
642 set_extra_expr_mod(tmp
->unop
, alloc_estate_whole(get_type(tmp
->unop
)));
644 clear_the_pointed_at(tmp
);
645 } END_FOR_EACH_PTR(arg
);
648 static int types_equiv_or_pointer(struct symbol
*one
, struct symbol
*two
)
652 if (one
->type
== SYM_PTR
&& two
->type
== SYM_PTR
)
654 return types_equiv(one
, two
);
657 static void save_chunk_info(struct expression
*left
, struct expression
*right
)
659 struct var_sym_list
*vsl
;
661 struct expression
*add_expr
;
667 if (right
->type
!= EXPR_BINOP
|| right
->op
!= '-')
669 if (!get_value(right
->left
, &sval
))
671 if (!expr_to_sym(right
->right
))
674 add_expr
= binop_expression(left
, '+', right
->right
);
675 type
= get_type(add_expr
);
678 name
= expr_to_chunk_sym_vsl(add_expr
, &sym
, &vsl
);
681 FOR_EACH_PTR(vsl
, vs
) {
682 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
683 } END_FOR_EACH_PTR(vs
);
685 set_state(SMATCH_EXTRA
, name
, sym
, alloc_estate_sval(sval_cast(type
, sval
)));
690 static void do_array_assign(struct expression
*left
, int op
, struct expression
*right
)
692 struct range_list
*rl
;
695 get_absolute_rl(right
, &rl
);
696 rl
= cast_rl(get_type(left
), rl
);
698 rl
= alloc_whole_rl(get_type(left
));
701 set_extra_array_mod(left
, alloc_estate_rl(rl
));
704 static void match_untracked_array(struct expression
*call
, int param
)
706 struct expression
*arg
;
708 arg
= get_argument_from_call_expr(call
->args
, param
);
709 arg
= strip_expr(arg
);
711 clear_array_states(arg
);
714 static void match_vanilla_assign(struct expression
*left
, struct expression
*right
)
716 struct range_list
*rl
= NULL
;
717 struct symbol
*right_sym
;
718 struct symbol
*left_type
;
719 struct symbol
*right_type
;
720 char *right_name
= NULL
;
724 struct smatch_state
*state
;
729 save_chunk_info(left
, right
);
731 name
= expr_to_var_sym(left
, &sym
);
734 do_array_assign(left
, '=', right
);
738 left_type
= get_type(left
);
739 right_type
= get_type(right
);
741 right_name
= expr_to_var_sym(right
, &right_sym
);
743 if (!__in_fake_assign
&&
744 !(right
->type
== EXPR_PREOP
&& right
->op
== '&') &&
745 right_name
&& right_sym
&&
746 types_equiv_or_pointer(left_type
, right_type
) &&
747 !has_symbol(right
, sym
)) {
748 set_equiv(left
, right
);
752 if (is_pointer(right
) && get_address_rl(right
, &rl
)) {
753 state
= alloc_estate_rl(rl
);
754 } else if (get_implied_rl(right
, &rl
)) {
755 rl
= cast_rl(left_type
, rl
);
756 state
= alloc_estate_rl(rl
);
757 if (get_hard_max(right
, &max
)) {
758 estate_set_hard_max(state
);
759 estate_set_fuzzy_max(state
, max
);
762 rl
= alloc_whole_rl(right_type
);
763 rl
= cast_rl(left_type
, rl
);
764 state
= alloc_estate_rl(rl
);
766 set_extra_mod(name
, sym
, state
);
768 free_string(right_name
);
771 static int op_remove_assign(int op
)
774 case SPECIAL_ADD_ASSIGN
:
776 case SPECIAL_SUB_ASSIGN
:
778 case SPECIAL_MUL_ASSIGN
:
780 case SPECIAL_DIV_ASSIGN
:
782 case SPECIAL_MOD_ASSIGN
:
784 case SPECIAL_AND_ASSIGN
:
786 case SPECIAL_OR_ASSIGN
:
788 case SPECIAL_XOR_ASSIGN
:
790 case SPECIAL_SHL_ASSIGN
:
791 return SPECIAL_LEFTSHIFT
;
792 case SPECIAL_SHR_ASSIGN
:
793 return SPECIAL_RIGHTSHIFT
;
799 static void match_assign(struct expression
*expr
)
801 struct range_list
*rl
= NULL
;
802 struct expression
*left
;
803 struct expression
*right
;
804 struct expression
*binop_expr
;
805 struct symbol
*left_type
;
806 struct symbol
*right_type
;
809 sval_t left_min
, left_max
;
810 sval_t right_min
, right_max
;
811 sval_t res_min
, res_max
;
813 left
= strip_expr(expr
->left
);
815 right
= strip_parens(expr
->right
);
816 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
817 right
= strip_parens(right
->left
);
819 if (expr
->op
== '=' && is_condition(expr
->right
))
820 return; /* handled in smatch_condition.c */
821 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
822 return; /* handled in smatch_function_hooks.c */
823 if (expr
->op
== '=') {
824 match_vanilla_assign(left
, right
);
828 name
= expr_to_var_sym(left
, &sym
);
832 left_type
= get_type(left
);
833 right_type
= get_type(right
);
835 res_min
= sval_type_min(left_type
);
836 res_max
= sval_type_max(left_type
);
839 case SPECIAL_ADD_ASSIGN
:
840 get_absolute_max(left
, &left_max
);
841 get_absolute_max(right
, &right_max
);
842 if (sval_binop_overflows(left_max
, '+', right_max
))
844 if (get_implied_min(left
, &left_min
) &&
845 !sval_is_negative_min(left_min
) &&
846 get_implied_min(right
, &right_min
) &&
847 !sval_is_negative_min(right_min
)) {
848 res_min
= sval_binop(left_min
, '+', right_min
);
849 res_min
= sval_cast(right_type
, res_min
);
851 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
853 res_max
= sval_binop(left_max
, '+', right_max
);
854 res_max
= sval_cast(right_type
, res_max
);
856 case SPECIAL_SUB_ASSIGN
:
857 if (get_implied_max(left
, &left_max
) &&
858 !sval_is_max(left_max
) &&
859 get_implied_min(right
, &right_min
) &&
860 !sval_is_min(right_min
)) {
861 res_max
= sval_binop(left_max
, '-', right_min
);
862 res_max
= sval_cast(right_type
, res_max
);
866 if (get_implied_min(left
, &left_min
) &&
867 !sval_is_min(left_min
) &&
868 get_implied_max(right
, &right_max
) &&
869 !sval_is_max(right_max
)) {
870 res_min
= sval_binop(left_min
, '-', right_max
);
871 res_min
= sval_cast(right_type
, res_min
);
874 case SPECIAL_AND_ASSIGN
:
875 case SPECIAL_MOD_ASSIGN
:
876 case SPECIAL_SHL_ASSIGN
:
877 case SPECIAL_SHR_ASSIGN
:
878 case SPECIAL_OR_ASSIGN
:
879 case SPECIAL_XOR_ASSIGN
:
880 case SPECIAL_MUL_ASSIGN
:
881 case SPECIAL_DIV_ASSIGN
:
882 binop_expr
= binop_expression(expr
->left
,
883 op_remove_assign(expr
->op
),
885 if (get_absolute_rl(binop_expr
, &rl
)) {
886 rl
= cast_rl(left_type
, rl
);
887 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
892 rl
= cast_rl(left_type
, alloc_rl(res_min
, res_max
));
893 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
898 static struct smatch_state
*increment_state(struct smatch_state
*state
)
900 sval_t min
= estate_min(state
);
901 sval_t max
= estate_max(state
);
903 if (!estate_rl(state
))
907 max
= sval_type_max(max
.type
);
909 if (!sval_is_min(min
) && !sval_is_max(min
))
911 if (!sval_is_min(max
) && !sval_is_max(max
))
913 return alloc_estate_range(min
, max
);
916 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
918 sval_t min
= estate_min(state
);
919 sval_t max
= estate_max(state
);
921 if (!estate_rl(state
))
925 min
= sval_type_min(min
.type
);
927 if (!sval_is_min(min
) && !sval_is_max(min
))
929 if (!sval_is_min(max
) && !sval_is_max(max
))
931 return alloc_estate_range(min
, max
);
934 static void unop_expr(struct expression
*expr
)
936 struct smatch_state
*state
;
938 if (expr
->smatch_flags
& Handled
)
942 case SPECIAL_INCREMENT
:
943 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
944 state
= increment_state(state
);
946 state
= alloc_estate_whole(get_type(expr
));
947 set_extra_expr_mod(expr
->unop
, state
);
949 case SPECIAL_DECREMENT
:
950 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
951 state
= decrement_state(state
);
953 state
= alloc_estate_whole(get_type(expr
));
954 set_extra_expr_mod(expr
->unop
, state
);
961 static void asm_expr(struct statement
*stmt
)
964 struct expression
*expr
;
968 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
970 case 0: /* identifier */
971 case 1: /* constraint */
974 case 2: /* expression */
976 type
= get_type(strip_expr(expr
));
977 set_extra_expr_mod(expr
, alloc_estate_whole(type
));
980 } END_FOR_EACH_PTR(expr
);
983 static void check_dereference(struct expression
*expr
)
985 if (outside_of_function())
987 set_extra_expr_nomod(expr
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
990 static void match_dereferences(struct expression
*expr
)
992 if (expr
->type
!= EXPR_PREOP
)
994 check_dereference(expr
->unop
);
997 static void match_pointer_as_array(struct expression
*expr
)
1001 check_dereference(get_array_base(expr
));
1004 static void set_param_dereferenced(struct expression
*arg
, char *key
, char *unused
)
1009 name
= get_variable_from_key(arg
, key
, &sym
);
1013 set_extra_nomod(name
, sym
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
1019 static sval_t
add_one(sval_t sval
)
1025 static sval_t
sub_one(sval_t sval
)
1031 static void handle_comparison(struct symbol
*type
, struct expression
*left
, int op
, struct expression
*right
)
1033 struct range_list
*left_orig
;
1034 struct range_list
*left_true
;
1035 struct range_list
*left_false
;
1036 struct range_list
*right_orig
;
1037 struct range_list
*right_true
;
1038 struct range_list
*right_false
;
1039 struct smatch_state
*left_true_state
;
1040 struct smatch_state
*left_false_state
;
1041 struct smatch_state
*right_true_state
;
1042 struct smatch_state
*right_false_state
;
1043 sval_t min
, max
, dummy
, hard_max
;
1044 int left_postop
= 0;
1045 int right_postop
= 0;
1047 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
1048 if (left
->type
== EXPR_POSTOP
) {
1049 left
->smatch_flags
|= Handled
;
1050 left_postop
= left
->op
;
1052 left
= strip_parens(left
->unop
);
1054 while (left
->type
== EXPR_ASSIGNMENT
)
1055 left
= strip_parens(left
->left
);
1057 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
1058 if (right
->type
== EXPR_POSTOP
) {
1059 right
->smatch_flags
|= Handled
;
1060 right_postop
= right
->op
;
1062 right
= strip_parens(right
->unop
);
1065 if (get_implied_rl(left
, &left_orig
)) {
1066 left_orig
= cast_rl(type
, left_orig
);
1068 min
= sval_type_min(get_type(left
));
1069 max
= sval_type_max(get_type(left
));
1070 left_orig
= cast_rl(type
, alloc_rl(min
, max
));
1073 if (get_implied_rl(right
, &right_orig
)) {
1074 right_orig
= cast_rl(type
, right_orig
);
1076 min
= sval_type_min(get_type(right
));
1077 max
= sval_type_max(get_type(right
));
1078 right_orig
= cast_rl(type
, alloc_rl(min
, max
));
1080 min
= sval_type_min(type
);
1081 max
= sval_type_max(type
);
1083 left_true
= clone_rl(left_orig
);
1084 left_false
= clone_rl(left_orig
);
1085 right_true
= clone_rl(right_orig
);
1086 right_false
= clone_rl(right_orig
);
1090 case SPECIAL_UNSIGNED_LT
:
1091 left_true
= remove_range(left_orig
, rl_max(right_orig
), max
);
1092 if (!sval_is_min(rl_min(right_orig
))) {
1093 left_false
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
1096 right_true
= remove_range(right_orig
, min
, rl_min(left_orig
));
1097 if (!sval_is_max(rl_max(left_orig
)))
1098 right_false
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
1100 case SPECIAL_UNSIGNED_LTE
:
1102 if (!sval_is_max(rl_max(right_orig
)))
1103 left_true
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
1104 left_false
= remove_range(left_orig
, min
, rl_min(right_orig
));
1106 if (!sval_is_min(rl_min(left_orig
)))
1107 right_true
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
1108 right_false
= remove_range(right_orig
, rl_max(left_orig
), max
);
1110 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
1111 left_false
= remove_range(left_false
, rl_min(left_orig
), rl_min(left_orig
));
1112 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
1113 right_false
= remove_range(right_false
, rl_max(left_orig
), rl_max(left_orig
));
1116 if (!sval_is_max(rl_max(right_orig
))) {
1117 left_true
= remove_range(left_true
, add_one(rl_max(right_orig
)), max
);
1119 if (!sval_is_min(rl_min(right_orig
))) {
1120 left_true
= remove_range(left_true
, min
, sub_one(rl_min(right_orig
)));
1122 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
1123 left_false
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
1125 if (!sval_is_max(rl_max(left_orig
)))
1126 right_true
= remove_range(right_true
, add_one(rl_max(left_orig
)), max
);
1127 if (!sval_is_min(rl_min(left_orig
)))
1128 right_true
= remove_range(right_true
, min
, sub_one(rl_min(left_orig
)));
1129 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
1130 right_false
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
1132 case SPECIAL_UNSIGNED_GTE
:
1134 if (!sval_is_min(rl_min(right_orig
)))
1135 left_true
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
1136 left_false
= remove_range(left_orig
, rl_max(right_orig
), max
);
1138 if (!sval_is_max(rl_max(left_orig
)))
1139 right_true
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
1140 right_false
= remove_range(right_orig
, min
, rl_min(left_orig
));
1142 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
1143 right_false
= remove_range(right_false
, rl_min(left_orig
), rl_min(left_orig
));
1144 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
1145 left_false
= remove_range(left_false
, rl_max(left_orig
), rl_max(left_orig
));
1148 case SPECIAL_UNSIGNED_GT
:
1149 left_true
= remove_range(left_orig
, min
, rl_min(right_orig
));
1150 if (!sval_is_max(rl_max(right_orig
)))
1151 left_false
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
1153 right_true
= remove_range(right_orig
, rl_max(left_orig
), max
);
1154 if (!sval_is_min(rl_min(left_orig
)))
1155 right_false
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
1157 case SPECIAL_NOTEQUAL
:
1158 if (!sval_is_max(rl_max(right_orig
)))
1159 left_false
= remove_range(left_false
, add_one(rl_max(right_orig
)), max
);
1160 if (!sval_is_min(rl_min(right_orig
)))
1161 left_false
= remove_range(left_false
, min
, sub_one(rl_min(right_orig
)));
1162 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
1163 left_true
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
1165 if (!sval_is_max(rl_max(left_orig
)))
1166 right_false
= remove_range(right_false
, add_one(rl_max(left_orig
)), max
);
1167 if (!sval_is_min(rl_min(left_orig
)))
1168 right_false
= remove_range(right_false
, min
, sub_one(rl_min(left_orig
)));
1169 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
1170 right_true
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
1176 left_true
= rl_truncate_cast(get_type(strip_expr(left
)), left_true
);
1177 left_false
= rl_truncate_cast(get_type(strip_expr(left
)), left_false
);
1178 right_true
= rl_truncate_cast(get_type(strip_expr(right
)), right_true
);
1179 right_false
= rl_truncate_cast(get_type(strip_expr(right
)), right_false
);
1181 left_true_state
= alloc_estate_rl(left_true
);
1182 left_false_state
= alloc_estate_rl(left_false
);
1183 right_true_state
= alloc_estate_rl(right_true
);
1184 right_false_state
= alloc_estate_rl(right_false
);
1188 case SPECIAL_UNSIGNED_LT
:
1189 case SPECIAL_UNSIGNED_LTE
:
1191 if (get_hard_max(right
, &dummy
))
1192 estate_set_hard_max(left_true_state
);
1193 if (get_hard_max(left
, &dummy
))
1194 estate_set_hard_max(right_false_state
);
1197 case SPECIAL_UNSIGNED_GT
:
1198 case SPECIAL_UNSIGNED_GTE
:
1200 if (get_hard_max(left
, &dummy
))
1201 estate_set_hard_max(right_true_state
);
1202 if (get_hard_max(right
, &dummy
))
1203 estate_set_hard_max(left_false_state
);
1209 case SPECIAL_UNSIGNED_LT
:
1210 case SPECIAL_UNSIGNED_LTE
:
1212 if (get_hard_max(right
, &hard_max
)) {
1213 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1215 estate_set_fuzzy_max(left_true_state
, hard_max
);
1217 if (get_implied_value(right
, &hard_max
)) {
1218 if (op
== SPECIAL_UNSIGNED_LTE
||
1221 estate_set_fuzzy_max(left_false_state
, hard_max
);
1223 if (get_hard_max(left
, &hard_max
)) {
1224 if (op
== SPECIAL_UNSIGNED_LTE
||
1227 estate_set_fuzzy_max(right_false_state
, hard_max
);
1229 if (get_implied_value(left
, &hard_max
)) {
1230 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1232 estate_set_fuzzy_max(right_true_state
, hard_max
);
1236 case SPECIAL_UNSIGNED_GT
:
1237 case SPECIAL_UNSIGNED_GTE
:
1239 if (get_hard_max(left
, &hard_max
)) {
1240 if (op
== '>' || op
== SPECIAL_UNSIGNED_GT
)
1242 estate_set_fuzzy_max(right_true_state
, hard_max
);
1244 if (get_implied_value(left
, &hard_max
)) {
1245 if (op
== SPECIAL_UNSIGNED_GTE
||
1248 estate_set_fuzzy_max(right_false_state
, hard_max
);
1250 if (get_hard_max(right
, &hard_max
)) {
1251 if (op
== SPECIAL_UNSIGNED_LTE
||
1254 estate_set_fuzzy_max(left_false_state
, hard_max
);
1256 if (get_implied_value(right
, &hard_max
)) {
1258 op
== SPECIAL_UNSIGNED_GT
)
1260 estate_set_fuzzy_max(left_true_state
, hard_max
);
1264 if (get_hard_max(left
, &hard_max
))
1265 estate_set_fuzzy_max(right_true_state
, hard_max
);
1266 if (get_hard_max(right
, &hard_max
))
1267 estate_set_fuzzy_max(left_true_state
, hard_max
);
1271 if (get_hard_max(left
, &hard_max
)) {
1272 estate_set_hard_max(left_true_state
);
1273 estate_set_hard_max(left_false_state
);
1275 if (get_hard_max(right
, &hard_max
)) {
1276 estate_set_hard_max(right_true_state
);
1277 estate_set_hard_max(right_false_state
);
1280 if (left_postop
== SPECIAL_INCREMENT
) {
1281 left_true_state
= increment_state(left_true_state
);
1282 left_false_state
= increment_state(left_false_state
);
1284 if (left_postop
== SPECIAL_DECREMENT
) {
1285 left_true_state
= decrement_state(left_true_state
);
1286 left_false_state
= decrement_state(left_false_state
);
1288 if (right_postop
== SPECIAL_INCREMENT
) {
1289 right_true_state
= increment_state(right_true_state
);
1290 right_false_state
= increment_state(right_false_state
);
1292 if (right_postop
== SPECIAL_DECREMENT
) {
1293 right_true_state
= decrement_state(right_true_state
);
1294 right_false_state
= decrement_state(right_false_state
);
1297 if (estate_rl(left_true_state
) && estates_equiv(left_true_state
, left_false_state
)) {
1298 left_true_state
= NULL
;
1299 left_false_state
= NULL
;
1302 if (estate_rl(right_true_state
) && estates_equiv(right_true_state
, right_false_state
)) {
1303 right_true_state
= NULL
;
1304 right_false_state
= NULL
;
1307 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
1308 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
1311 static int is_simple_math(struct expression
*expr
)
1315 if (expr
->type
!= EXPR_BINOP
)
1326 static void move_known_values(struct expression
**left_p
, struct expression
**right_p
)
1328 struct expression
*left
= *left_p
;
1329 struct expression
*right
= *right_p
;
1332 if (get_implied_value(left
, &sval
)) {
1333 if (!is_simple_math(right
))
1335 if (right
->op
== '*') {
1338 if (!get_value(right
->right
, &divisor
))
1340 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1342 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1343 *right_p
= right
->left
;
1346 if (right
->op
== '+' && get_value(right
->left
, &sval
)) {
1347 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->left
);
1348 *right_p
= right
->right
;
1351 if (get_value(right
->right
, &sval
)) {
1352 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1353 *right_p
= right
->left
;
1358 if (get_implied_value(right
, &sval
)) {
1359 if (!is_simple_math(left
))
1361 if (left
->op
== '*') {
1364 if (!get_value(left
->right
, &divisor
))
1366 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1368 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1369 *left_p
= left
->left
;
1372 if (left
->op
== '+' && get_value(left
->left
, &sval
)) {
1373 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->left
);
1374 *left_p
= left
->right
;
1378 if (get_value(left
->right
, &sval
)) {
1379 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1380 *left_p
= left
->left
;
1387 static int match_func_comparison(struct expression
*expr
)
1389 struct expression
*left
= strip_expr(expr
->left
);
1390 struct expression
*right
= strip_expr(expr
->right
);
1394 * fixme: think about this harder. We should always be trying to limit
1395 * the non-call side as well. If we can't determine the limitter does
1396 * that mean we aren't querying the database and are missing important
1400 if (left
->type
== EXPR_CALL
) {
1401 if (get_implied_value(left
, &sval
)) {
1402 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1405 function_comparison(left
, expr
->op
, right
);
1409 if (right
->type
== EXPR_CALL
) {
1410 if (get_implied_value(right
, &sval
)) {
1411 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1414 function_comparison(left
, expr
->op
, right
);
1421 static void match_comparison(struct expression
*expr
)
1423 struct expression
*left_orig
= strip_parens(expr
->left
);
1424 struct expression
*right_orig
= strip_parens(expr
->right
);
1425 struct expression
*left
, *right
;
1426 struct expression
*prev
;
1427 struct symbol
*type
;
1429 if (match_func_comparison(expr
))
1432 type
= get_type(expr
);
1434 type
= &llong_ctype
;
1438 move_known_values(&left
, &right
);
1439 handle_comparison(type
, left
, expr
->op
, right
);
1441 prev
= get_assigned_expr(left_orig
);
1442 if (is_simple_math(prev
) && has_variable(prev
, left_orig
) == 0) {
1445 move_known_values(&left
, &right
);
1446 handle_comparison(type
, left
, expr
->op
, right
);
1449 prev
= get_assigned_expr(right_orig
);
1450 if (is_simple_math(prev
) && has_variable(prev
, right_orig
) == 0) {
1453 move_known_values(&left
, &right
);
1454 handle_comparison(type
, left
, expr
->op
, right
);
1458 static void handle_AND_condition(struct expression
*expr
)
1460 struct range_list
*rl
= NULL
;
1463 if (get_implied_value(expr
->left
, &known
) && known
.value
> 0) {
1465 get_absolute_rl(expr
->right
, &rl
);
1466 rl
= remove_range(rl
, sval_type_val(known
.type
, 0), known
);
1467 set_extra_expr_true_false(expr
->right
, alloc_estate_rl(rl
), NULL
);
1471 if (get_implied_value(expr
->right
, &known
) && known
.value
> 0) {
1473 get_absolute_rl(expr
->left
, &rl
);
1474 rl
= remove_range(rl
, sval_type_val(known
.type
, 0), known
);
1475 set_extra_expr_true_false(expr
->left
, alloc_estate_rl(rl
), NULL
);
1480 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1481 void __extra_match_condition(struct expression
*expr
)
1483 struct smatch_state
*pre_state
;
1484 struct smatch_state
*true_state
;
1485 struct smatch_state
*false_state
;
1487 expr
= strip_expr(expr
);
1488 switch (expr
->type
) {
1490 function_comparison(expr
, SPECIAL_NOTEQUAL
, zero_expr());
1497 zero
= sval_blank(expr
);
1500 pre_state
= get_extra_state(expr
);
1501 true_state
= estate_filter_sval(pre_state
, zero
);
1502 if (possibly_true(expr
, SPECIAL_EQUAL
, zero_expr()))
1503 false_state
= alloc_estate_sval(zero
);
1505 false_state
= alloc_estate_empty();
1506 set_extra_expr_true_false(expr
, true_state
, false_state
);
1510 match_comparison(expr
);
1512 case EXPR_ASSIGNMENT
:
1513 __extra_match_condition(expr
->left
);
1516 if (expr
->op
== '&')
1517 handle_AND_condition(expr
);
1522 static void assume_indexes_are_valid(struct expression
*expr
)
1524 struct expression
*array_expr
;
1526 struct expression
*offset
;
1527 struct symbol
*offset_type
;
1528 struct range_list
*rl_before
;
1529 struct range_list
*rl_after
;
1530 struct range_list
*filter
= NULL
;
1533 expr
= strip_expr(expr
);
1534 if (!is_array(expr
))
1537 offset
= get_array_offset(expr
);
1538 offset_type
= get_type(offset
);
1539 if (offset_type
&& type_signed(offset_type
)) {
1540 filter
= alloc_rl(sval_type_min(offset_type
),
1541 sval_type_val(offset_type
, -1));
1544 array_expr
= get_array_base(expr
);
1545 array_size
= get_real_array_size(array_expr
);
1546 if (array_size
> 1) {
1547 size
= sval_type_val(offset_type
, array_size
);
1548 add_range(&filter
, size
, sval_type_max(offset_type
));
1553 get_absolute_rl(offset
, &rl_before
);
1554 rl_after
= rl_filter(rl_before
, filter
);
1555 if (rl_equiv(rl_before
, rl_after
))
1557 set_extra_expr_nomod(offset
, alloc_estate_rl(rl_after
));
1560 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1561 int implied_not_equal(struct expression
*expr
, long long val
)
1563 return !possibly_false(expr
, SPECIAL_NOTEQUAL
, value_expr(val
));
1566 int implied_not_equal_name_sym(char *name
, struct symbol
*sym
, long long val
)
1568 struct smatch_state
*estate
;
1570 estate
= get_state(SMATCH_EXTRA
, name
, sym
);
1573 if (!rl_has_sval(estate_rl(estate
), sval_type_val(estate_type(estate
), 0)))
1578 int parent_is_null_var_sym(const char *name
, struct symbol
*sym
)
1583 struct smatch_state
*state
;
1585 strncpy(buf
, name
, sizeof(buf
) - 1);
1586 buf
[sizeof(buf
) - 1] = '\0';
1589 while (*start
== '*') {
1591 state
= get_state(SMATCH_EXTRA
, start
, sym
);
1594 if (!estate_rl(state
))
1596 if (estate_min(state
).value
== 0 &&
1597 estate_max(state
).value
== 0)
1602 while (*start
== '&')
1605 while ((end
= strrchr(start
, '-'))) {
1607 state
= get_state(SMATCH_EXTRA
, start
, sym
);
1610 if (estate_min(state
).value
== 0 &&
1611 estate_max(state
).value
== 0)
1617 int parent_is_null(struct expression
*expr
)
1623 expr
= strip_expr(expr
);
1624 var
= expr_to_var_sym(expr
, &sym
);
1627 ret
= parent_is_null_var_sym(var
, sym
);
1633 static int param_used_callback(void *found
, int argc
, char **argv
, char **azColName
)
1639 static int filter_unused_kzalloc_info(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
1644 /* for function pointers assume everything is used */
1645 if (call
->fn
->type
!= EXPR_SYMBOL
)
1649 * kzalloc() information is treated as special because so there is just
1650 * a lot of stuff initialized to zero and it makes building the database
1651 * take hours and hours.
1653 * In theory, we should just remove this line and not pass any unused
1654 * information, but I'm not sure enough that this code works so I want
1655 * to hold off on that for now.
1657 if (!estate_get_single_value(sm
->state
, &sval
) || sval
.value
!= 0)
1660 run_sql(¶m_used_callback
, &found
,
1661 "select * from call_implies where %s and type = %d and parameter = %d and key = '%s';",
1662 get_static_filter(call
->fn
->symbol
), PARAM_USED
, param
, printed_name
);
1666 /* If the database is not built yet, then assume everything is used */
1667 run_sql(¶m_used_callback
, &found
,
1668 "select * from call_implies where %s and type = %d;",
1669 get_static_filter(call
->fn
->symbol
), PARAM_USED
);
1676 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
1678 if (estate_is_whole(sm
->state
))
1680 if (filter_unused_kzalloc_info(call
, param
, printed_name
, sm
))
1682 sql_insert_caller_info(call
, PARAM_VALUE
, param
, printed_name
, sm
->state
->name
);
1683 if (estate_has_fuzzy_max(sm
->state
))
1684 sql_insert_caller_info(call
, FUZZY_MAX
, param
, printed_name
,
1685 sval_to_str(estate_get_fuzzy_max(sm
->state
)));
1688 static void db_limited_before(void)
1690 unmatched_stree
= clone_stree(__get_cur_stree());
1693 static void db_limited_after(void)
1695 free_stree(&unmatched_stree
);
1698 static void db_param_limit_filter(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
1700 struct expression
*arg
;
1703 struct sm_state
*sm
;
1704 struct symbol
*compare_type
, *var_type
;
1705 struct range_list
*rl
;
1706 struct range_list
*limit
;
1707 struct range_list
*new;
1709 while (expr
->type
== EXPR_ASSIGNMENT
)
1710 expr
= strip_expr(expr
->right
);
1711 if (expr
->type
!= EXPR_CALL
)
1714 arg
= get_argument_from_call_expr(expr
->args
, param
);
1718 name
= get_variable_from_key(arg
, key
, &sym
);
1722 if (strcmp(key
, "$") == 0)
1723 compare_type
= get_arg_type(expr
->fn
, param
);
1725 compare_type
= get_member_type_from_key(arg
, key
);
1727 sm
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
1729 rl
= estate_rl(sm
->state
);
1731 rl
= alloc_whole_rl(compare_type
);
1733 call_results_to_rl(expr
, compare_type
, value
, &limit
);
1734 new = rl_intersection(rl
, limit
);
1736 var_type
= get_member_type_from_key(arg
, key
);
1737 new = cast_rl(var_type
, new);
1739 /* We want to preserve the implications here */
1740 if (sm
&& rl_equiv(estate_rl(sm
->state
), new))
1743 if (op
== PARAM_LIMIT
)
1744 set_extra_nomod(name
, sym
, alloc_estate_rl(new));
1746 set_extra_mod(name
, sym
, alloc_estate_rl(new));
1753 static void db_param_limit(struct expression
*expr
, int param
, char *key
, char *value
)
1755 db_param_limit_filter(expr
, param
, key
, value
, PARAM_LIMIT
);
1758 static void db_param_filter(struct expression
*expr
, int param
, char *key
, char *value
)
1760 db_param_limit_filter(expr
, param
, key
, value
, PARAM_FILTER
);
1763 static void db_param_add_set(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
1765 struct expression
*arg
;
1768 struct symbol
*type
;
1769 struct smatch_state
*state
;
1770 struct range_list
*new = NULL
;
1771 struct range_list
*added
= NULL
;
1773 while (expr
->type
== EXPR_ASSIGNMENT
)
1774 expr
= strip_expr(expr
->right
);
1775 if (expr
->type
!= EXPR_CALL
)
1778 arg
= get_argument_from_call_expr(expr
->args
, param
);
1781 type
= get_member_type_from_key(arg
, key
);
1782 name
= get_variable_from_key(arg
, key
, &sym
);
1786 state
= get_state(SMATCH_EXTRA
, name
, sym
);
1788 new = estate_rl(state
);
1790 call_results_to_rl(expr
, type
, value
, &added
);
1792 if (op
== PARAM_SET
)
1795 new = rl_union(new, added
);
1797 set_extra_mod(name
, sym
, alloc_estate_rl(new));
1802 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
1804 db_param_add_set(expr
, param
, key
, value
, PARAM_ADD
);
1807 static void db_param_set(struct expression
*expr
, int param
, char *key
, char *value
)
1809 db_param_add_set(expr
, param
, key
, value
, PARAM_SET
);
1812 static void match_call_info(struct expression
*expr
)
1814 struct smatch_state
*state
;
1815 struct range_list
*rl
= NULL
;
1816 struct expression
*arg
;
1817 struct symbol
*type
;
1820 FOR_EACH_PTR(expr
->args
, arg
) {
1821 type
= get_arg_type(expr
->fn
, i
);
1823 if (get_implied_rl(arg
, &rl
))
1824 rl
= cast_rl(type
, rl
);
1826 rl
= cast_rl(type
, alloc_whole_rl(get_type(arg
)));
1828 if (!is_whole_rl(rl
))
1829 sql_insert_caller_info(expr
, PARAM_VALUE
, i
, "$", show_rl(rl
));
1830 state
= get_state_expr(SMATCH_EXTRA
, arg
);
1831 if (estate_has_fuzzy_max(state
)) {
1832 sql_insert_caller_info(expr
, FUZZY_MAX
, i
, "$",
1833 sval_to_str(estate_get_fuzzy_max(state
)));
1836 } END_FOR_EACH_PTR(arg
);
1839 static void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1841 struct range_list
*rl
= NULL
;
1842 struct smatch_state
*state
;
1843 struct symbol
*type
;
1846 if (strcmp(key
, "*$") == 0)
1847 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
1848 else if (strncmp(key
, "$", 1) == 0)
1849 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
1853 type
= get_member_type_from_key(symbol_expression(sym
), key
);
1854 str_to_rl(type
, value
, &rl
);
1855 state
= alloc_estate_rl(rl
);
1856 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
1859 static void set_param_hard_max(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1861 struct range_list
*rl
= NULL
;
1862 struct smatch_state
*state
;
1863 struct symbol
*type
;
1867 if (strcmp(key
, "*$") == 0)
1868 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
1869 else if (strncmp(key
, "$", 1) == 0)
1870 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
1874 state
= get_state(SMATCH_EXTRA
, fullname
, sym
);
1877 type
= get_member_type_from_key(symbol_expression(sym
), key
);
1878 str_to_rl(type
, value
, &rl
);
1879 if (!rl_to_sval(rl
, &max
))
1881 estate_set_fuzzy_max(state
, max
);
1884 struct smatch_state
*get_extra_state(struct expression
*expr
)
1888 struct smatch_state
*ret
= NULL
;
1889 struct range_list
*rl
;
1891 if (is_pointer(expr
) && get_address_rl(expr
, &rl
))
1892 return alloc_estate_rl(rl
);
1894 name
= expr_to_known_chunk_sym(expr
, &sym
);
1898 ret
= get_state(SMATCH_EXTRA
, name
, sym
);
1904 void register_smatch_extra(int id
)
1908 add_merge_hook(my_id
, &merge_estates
);
1909 add_unmatched_state_hook(my_id
, &unmatched_state
);
1910 select_caller_info_hook(set_param_value
, PARAM_VALUE
);
1911 select_caller_info_hook(set_param_hard_max
, FUZZY_MAX
);
1912 select_return_states_before(&db_limited_before
);
1913 select_return_states_hook(PARAM_LIMIT
, &db_param_limit
);
1914 select_return_states_hook(PARAM_FILTER
, &db_param_filter
);
1915 select_return_states_hook(PARAM_ADD
, &db_param_add
);
1916 select_return_states_hook(PARAM_SET
, &db_param_set
);
1917 select_return_states_after(&db_limited_after
);
1920 static void match_link_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
1922 struct var_sym_list
*links
;
1923 struct var_sym
*tmp
;
1924 struct smatch_state
*state
;
1926 links
= sm
->state
->data
;
1928 FOR_EACH_PTR(links
, tmp
) {
1929 state
= get_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
);
1932 set_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
, alloc_estate_whole(estate_type(state
)));
1933 } END_FOR_EACH_PTR(tmp
);
1934 set_state(link_id
, sm
->name
, sm
->sym
, &undefined
);
1937 void register_smatch_extra_links(int id
)
1942 void register_smatch_extra_late(int id
)
1944 add_merge_hook(link_id
, &merge_link_states
);
1945 add_modification_hook(link_id
, &match_link_modify
);
1946 add_hook(&match_dereferences
, DEREF_HOOK
);
1947 add_hook(&match_pointer_as_array
, OP_HOOK
);
1948 select_call_implies_hook(DEREFERENCE
, &set_param_dereferenced
);
1949 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
1950 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
1951 add_hook(&match_assign
, GLOBAL_ASSIGNMENT_HOOK
);
1952 add_hook(&unop_expr
, OP_HOOK
);
1953 add_hook(&asm_expr
, ASM_HOOK
);
1954 add_untracked_param_hook(&match_untracked_array
);
1956 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1957 add_member_info_callback(my_id
, struct_member_callback
);
1959 add_hook(&assume_indexes_are_valid
, OP_HOOK
);