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 struct smatch_state
*estate
;
234 if (option_debug_related
)
235 sm_msg("%s updating related %s to %s", name
, rel
->name
, state
->name
);
236 estate
= get_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
);
239 set_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
, clone_estate_cast(estate_type(estate
), state
));
240 } END_FOR_EACH_PTR(rel
);
244 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
246 void set_extra_expr_nomod(struct expression
*expr
, struct smatch_state
*state
)
251 name
= expr_to_var_sym(expr
, &sym
);
254 set_extra_nomod(name
, sym
, state
);
260 static void set_extra_true_false(const char *name
, struct symbol
*sym
,
261 struct smatch_state
*true_state
,
262 struct smatch_state
*false_state
)
265 struct symbol
*new_sym
;
266 struct relation
*rel
;
267 struct smatch_state
*orig_state
;
269 if (!true_state
&& !false_state
)
272 if (in_warn_on_macro())
275 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
276 if (new_name
&& new_sym
)
277 set_true_false_states(SMATCH_EXTRA
, new_name
, new_sym
, true_state
, false_state
);
278 free_string(new_name
);
280 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
282 if (!estate_related(orig_state
)) {
283 set_true_false_states(SMATCH_EXTRA
, name
, sym
, true_state
, false_state
);
288 set_related(true_state
, estate_related(orig_state
));
290 set_related(false_state
, estate_related(orig_state
));
292 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
293 set_true_false_states(SMATCH_EXTRA
, rel
->name
, rel
->sym
,
294 true_state
, false_state
);
295 } END_FOR_EACH_PTR(rel
);
298 static void set_extra_chunk_true_false(struct expression
*expr
,
299 struct smatch_state
*true_state
,
300 struct smatch_state
*false_state
)
302 struct var_sym_list
*vsl
;
308 if (in_warn_on_macro())
311 type
= get_type(expr
);
315 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
318 FOR_EACH_PTR(vsl
, vs
) {
319 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
320 } END_FOR_EACH_PTR(vs
);
322 set_true_false_states(SMATCH_EXTRA
, name
, sym
,
323 clone_estate(true_state
),
324 clone_estate(false_state
));
329 static void set_extra_expr_true_false(struct expression
*expr
,
330 struct smatch_state
*true_state
,
331 struct smatch_state
*false_state
)
337 if (!true_state
&& !false_state
)
340 if (get_value(expr
, &sval
))
343 expr
= strip_expr(expr
);
344 name
= expr_to_var_sym(expr
, &sym
);
347 set_extra_chunk_true_false(expr
, true_state
, false_state
);
350 set_extra_true_false(name
, sym
, true_state
, false_state
);
354 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
356 struct expression
*iter_var
;
357 struct expression
*condition
;
359 struct smatch_state
*estate
;
362 condition
= strip_expr(loop
->iterator_pre_condition
);
365 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
367 if (condition
->op
!= SPECIAL_DECREMENT
)
370 iter_var
= condition
->unop
;
371 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
374 if (sval_cmp_val(estate_min(sm
->state
), 0) < 0)
376 start
= estate_max(sm
->state
);
377 if (sval_cmp_val(start
, 0) <= 0)
379 if (!sval_is_max(start
))
382 if (condition
->type
== EXPR_PREOP
) {
383 estate
= alloc_estate_range(sval_type_val(start
.type
, 1), start
);
384 if (estate_has_hard_max(sm
->state
))
385 estate_set_hard_max(estate
);
386 estate_copy_fuzzy_max(estate
, sm
->state
);
387 set_extra_expr_mod(iter_var
, estate
);
389 if (condition
->type
== EXPR_POSTOP
) {
390 estate
= alloc_estate_range(sval_type_val(start
.type
, 0), start
);
391 if (estate_has_hard_max(sm
->state
))
392 estate_set_hard_max(estate
);
393 estate_copy_fuzzy_max(estate
, sm
->state
);
394 set_extra_expr_mod(iter_var
, estate
);
396 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
399 static struct sm_state
*handle_canonical_for_inc(struct expression
*iter_expr
,
400 struct expression
*condition
)
402 struct expression
*iter_var
;
404 struct smatch_state
*estate
;
405 sval_t start
, end
, max
;
407 iter_var
= iter_expr
->unop
;
408 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
411 if (!estate_get_single_value(sm
->state
, &start
))
413 if (get_implied_max(condition
->right
, &end
))
414 end
= sval_cast(get_type(iter_var
), end
);
416 end
= sval_type_max(get_type(iter_var
));
418 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
421 switch (condition
->op
) {
422 case SPECIAL_UNSIGNED_LT
:
423 case SPECIAL_NOTEQUAL
:
425 if (!sval_is_min(end
))
428 case SPECIAL_UNSIGNED_LTE
:
434 if (sval_cmp(end
, start
) < 0)
436 estate
= alloc_estate_range(start
, end
);
437 if (get_hard_max(condition
->right
, &max
)) {
438 estate_set_hard_max(estate
);
439 if (condition
->op
== '<' ||
440 condition
->op
== SPECIAL_UNSIGNED_LT
||
441 condition
->op
== SPECIAL_NOTEQUAL
)
443 estate_set_fuzzy_max(estate
, max
);
445 set_extra_expr_mod(iter_var
, estate
);
446 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
449 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
450 struct expression
*condition
)
452 struct expression
*iter_var
;
454 struct smatch_state
*estate
;
457 iter_var
= iter_expr
->unop
;
458 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
461 if (!estate_get_single_value(sm
->state
, &start
))
463 if (!get_implied_min(condition
->right
, &end
))
464 end
= sval_type_min(get_type(iter_var
));
465 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
468 switch (condition
->op
) {
469 case SPECIAL_NOTEQUAL
:
471 if (!sval_is_min(end
) && !sval_is_max(end
))
479 if (sval_cmp(end
, start
) > 0)
481 estate
= alloc_estate_range(end
, start
);
482 estate_set_hard_max(estate
);
483 estate_set_fuzzy_max(estate
, estate_get_fuzzy_max(estate
));
484 set_extra_expr_mod(iter_var
, estate
);
485 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
488 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
490 struct expression
*iter_expr
;
491 struct expression
*condition
;
493 if (!loop
->iterator_post_statement
)
495 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
497 iter_expr
= loop
->iterator_post_statement
->expression
;
498 if (!loop
->iterator_pre_condition
)
500 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
502 condition
= loop
->iterator_pre_condition
;
504 if (iter_expr
->op
== SPECIAL_INCREMENT
)
505 return handle_canonical_for_inc(iter_expr
, condition
);
506 if (iter_expr
->op
== SPECIAL_DECREMENT
)
507 return handle_canonical_for_dec(iter_expr
, condition
);
511 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct stree
**stree
)
513 struct sm_state
*ret
;
515 __push_fake_cur_stree();
516 if (!loop
->iterator_post_statement
)
517 ret
= handle_canonical_while_count_down(loop
);
519 ret
= handle_canonical_for_loops(loop
);
520 *stree
= __pop_fake_cur_stree();
524 int __iterator_unchanged(struct sm_state
*sm
)
528 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
533 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
537 /* paranoid checking. prolly not needed */
538 condition
= strip_expr(condition
);
541 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
543 if (condition
->op
!= SPECIAL_DECREMENT
)
545 after_value
= estate_min(sm
->state
);
547 set_extra_mod(sm
->name
, sm
->sym
, alloc_estate_sval(after_value
));
550 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
551 struct statement
*iterator
,
552 struct expression
*condition
)
554 struct expression
*iter_expr
;
556 struct smatch_state
*state
;
559 while_count_down_after(sm
, condition
);
563 iter_expr
= iterator
->expression
;
565 if (condition
->type
!= EXPR_COMPARE
)
567 if (iter_expr
->op
== SPECIAL_INCREMENT
) {
568 limit
= sval_binop(estate_max(sm
->state
), '+',
569 sval_type_val(estate_type(sm
->state
), 1));
571 limit
= sval_binop(estate_min(sm
->state
), '-',
572 sval_type_val(estate_type(sm
->state
), 1));
574 if (!estate_has_hard_max(sm
->state
) && !__has_breaks()) {
575 if (iter_expr
->op
== SPECIAL_INCREMENT
)
576 state
= alloc_estate_range(estate_min(sm
->state
), limit
);
578 state
= alloc_estate_range(limit
, estate_max(sm
->state
));
580 state
= alloc_estate_sval(limit
);
582 if (!estate_has_hard_max(sm
->state
)) {
583 estate_clear_hard_max(state
);
585 if (estate_has_fuzzy_max(sm
->state
)) {
586 sval_t hmax
= estate_get_fuzzy_max(sm
->state
);
587 sval_t max
= estate_max(sm
->state
);
589 if (sval_cmp(hmax
, max
) != 0)
590 estate_clear_fuzzy_max(state
);
591 } else if (!estate_has_fuzzy_max(sm
->state
)) {
592 estate_clear_fuzzy_max(state
);
595 set_extra_mod(sm
->name
, sm
->sym
, state
);
598 static struct stree
*unmatched_stree
;
599 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
601 struct smatch_state
*state
;
603 if (unmatched_stree
) {
604 state
= get_state_stree(unmatched_stree
, SMATCH_EXTRA
, sm
->name
, sm
->sym
);
608 if (parent_is_gone_var_sym(sm
->name
, sm
->sym
))
609 return alloc_estate_empty();
610 return alloc_estate_whole(estate_type(sm
->state
));
613 static void clear_the_pointed_at(struct expression
*expr
)
618 struct sm_state
*tmp
;
620 name
= expr_to_var_sym(expr
, &sym
);
624 stree
= __get_cur_stree();
625 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
626 if (tmp
->name
[0] != '*')
630 if (strcmp(tmp
->name
+ 1, name
) != 0)
632 set_extra_mod(tmp
->name
, tmp
->sym
, alloc_estate_whole(estate_type(tmp
->state
)));
633 } END_FOR_EACH_SM(tmp
);
639 static void match_function_call(struct expression
*expr
)
641 struct expression
*arg
;
642 struct expression
*tmp
;
644 /* if we have the db this is handled in smatch_function_hooks.c */
647 if (inlinable(expr
->fn
))
650 FOR_EACH_PTR(expr
->args
, arg
) {
651 tmp
= strip_expr(arg
);
652 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
653 set_extra_expr_mod(tmp
->unop
, alloc_estate_whole(get_type(tmp
->unop
)));
655 clear_the_pointed_at(tmp
);
656 } END_FOR_EACH_PTR(arg
);
659 static int values_fit_type(struct expression
*left
, struct expression
*right
)
661 struct range_list
*rl
;
664 type
= get_type(left
);
667 get_absolute_rl(right
, &rl
);
668 if (sval_cmp(sval_type_min(type
), rl_min(rl
)) > 0)
670 if (sval_cmp(sval_type_max(type
), rl_max(rl
)) < 0)
675 static void save_chunk_info(struct expression
*left
, struct expression
*right
)
677 struct var_sym_list
*vsl
;
679 struct expression
*add_expr
;
685 if (right
->type
!= EXPR_BINOP
|| right
->op
!= '-')
687 if (!get_value(right
->left
, &sval
))
689 if (!expr_to_sym(right
->right
))
692 add_expr
= binop_expression(left
, '+', right
->right
);
693 type
= get_type(add_expr
);
696 name
= expr_to_chunk_sym_vsl(add_expr
, &sym
, &vsl
);
699 FOR_EACH_PTR(vsl
, vs
) {
700 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
701 } END_FOR_EACH_PTR(vs
);
703 set_state(SMATCH_EXTRA
, name
, sym
, alloc_estate_sval(sval_cast(type
, sval
)));
708 static void do_array_assign(struct expression
*left
, int op
, struct expression
*right
)
710 struct range_list
*rl
;
713 get_absolute_rl(right
, &rl
);
714 rl
= cast_rl(get_type(left
), rl
);
716 rl
= alloc_whole_rl(get_type(left
));
719 set_extra_array_mod(left
, alloc_estate_rl(rl
));
722 static void match_untracked_array(struct expression
*call
, int param
)
724 struct expression
*arg
;
726 arg
= get_argument_from_call_expr(call
->args
, param
);
727 arg
= strip_expr(arg
);
729 clear_array_states(arg
);
732 static void match_vanilla_assign(struct expression
*left
, struct expression
*right
)
734 struct range_list
*orig_rl
= NULL
;
735 struct range_list
*rl
= NULL
;
736 struct symbol
*right_sym
;
737 struct symbol
*left_type
;
738 struct symbol
*right_type
;
739 char *right_name
= NULL
;
743 struct smatch_state
*state
;
749 save_chunk_info(left
, right
);
751 name
= expr_to_var_sym(left
, &sym
);
754 do_array_assign(left
, '=', right
);
758 left_type
= get_type(left
);
759 right_type
= get_type(right
);
761 right_name
= expr_to_var_sym(right
, &right_sym
);
763 if (!__in_fake_assign
&&
764 !(right
->type
== EXPR_PREOP
&& right
->op
== '&') &&
765 right_name
&& right_sym
&&
766 values_fit_type(left
, right
) &&
767 !has_symbol(right
, sym
)) {
768 set_equiv(left
, right
);
772 if (is_pointer(right
) && get_address_rl(right
, &rl
)) {
773 state
= alloc_estate_rl(rl
);
777 comparison
= get_comparison(left
, right
);
779 comparison
= flip_comparison(comparison
);
780 get_implied_rl(left
, &orig_rl
);
783 if (get_implied_rl(right
, &rl
)) {
784 rl
= cast_rl(left_type
, rl
);
786 filter_by_comparison(&rl
, comparison
, orig_rl
);
787 state
= alloc_estate_rl(rl
);
788 if (get_hard_max(right
, &max
)) {
789 estate_set_hard_max(state
);
790 estate_set_fuzzy_max(state
, max
);
793 rl
= alloc_whole_rl(right_type
);
794 rl
= cast_rl(left_type
, rl
);
796 filter_by_comparison(&rl
, comparison
, orig_rl
);
797 state
= alloc_estate_rl(rl
);
801 set_extra_mod(name
, sym
, state
);
803 free_string(right_name
);
806 static int op_remove_assign(int op
)
809 case SPECIAL_ADD_ASSIGN
:
811 case SPECIAL_SUB_ASSIGN
:
813 case SPECIAL_MUL_ASSIGN
:
815 case SPECIAL_DIV_ASSIGN
:
817 case SPECIAL_MOD_ASSIGN
:
819 case SPECIAL_AND_ASSIGN
:
821 case SPECIAL_OR_ASSIGN
:
823 case SPECIAL_XOR_ASSIGN
:
825 case SPECIAL_SHL_ASSIGN
:
826 return SPECIAL_LEFTSHIFT
;
827 case SPECIAL_SHR_ASSIGN
:
828 return SPECIAL_RIGHTSHIFT
;
834 static void match_assign(struct expression
*expr
)
836 struct range_list
*rl
= NULL
;
837 struct expression
*left
;
838 struct expression
*right
;
839 struct expression
*binop_expr
;
840 struct symbol
*left_type
;
841 struct symbol
*right_type
;
844 sval_t left_min
, left_max
;
845 sval_t right_min
, right_max
;
846 sval_t res_min
, res_max
;
848 left
= strip_expr(expr
->left
);
850 right
= strip_parens(expr
->right
);
851 if (right
->type
== EXPR_CALL
&& sym_name_is("__builtin_expect", right
->fn
))
852 right
= get_argument_from_call_expr(right
->args
, 0);
853 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
854 right
= strip_parens(right
->left
);
856 if (expr
->op
== '=' && is_condition(expr
->right
))
857 return; /* handled in smatch_condition.c */
858 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
859 return; /* handled in smatch_function_hooks.c */
860 if (expr
->op
== '=') {
861 match_vanilla_assign(left
, right
);
865 name
= expr_to_var_sym(left
, &sym
);
869 left_type
= get_type(left
);
870 right_type
= get_type(right
);
872 res_min
= sval_type_min(left_type
);
873 res_max
= sval_type_max(left_type
);
876 case SPECIAL_ADD_ASSIGN
:
877 get_absolute_max(left
, &left_max
);
878 get_absolute_max(right
, &right_max
);
879 if (sval_binop_overflows(left_max
, '+', right_max
))
881 if (get_implied_min(left
, &left_min
) &&
882 !sval_is_negative_min(left_min
) &&
883 get_implied_min(right
, &right_min
) &&
884 !sval_is_negative_min(right_min
)) {
885 res_min
= sval_binop(left_min
, '+', right_min
);
886 res_min
= sval_cast(right_type
, res_min
);
888 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
890 res_max
= sval_binop(left_max
, '+', right_max
);
891 res_max
= sval_cast(right_type
, res_max
);
893 case SPECIAL_SUB_ASSIGN
:
894 if (get_implied_max(left
, &left_max
) &&
895 !sval_is_max(left_max
) &&
896 get_implied_min(right
, &right_min
) &&
897 !sval_is_min(right_min
)) {
898 res_max
= sval_binop(left_max
, '-', right_min
);
899 res_max
= sval_cast(right_type
, res_max
);
903 if (get_implied_min(left
, &left_min
) &&
904 !sval_is_min(left_min
) &&
905 get_implied_max(right
, &right_max
) &&
906 !sval_is_max(right_max
)) {
907 res_min
= sval_binop(left_min
, '-', right_max
);
908 res_min
= sval_cast(right_type
, res_min
);
911 case SPECIAL_AND_ASSIGN
:
912 case SPECIAL_MOD_ASSIGN
:
913 case SPECIAL_SHL_ASSIGN
:
914 case SPECIAL_SHR_ASSIGN
:
915 case SPECIAL_OR_ASSIGN
:
916 case SPECIAL_XOR_ASSIGN
:
917 case SPECIAL_MUL_ASSIGN
:
918 case SPECIAL_DIV_ASSIGN
:
919 binop_expr
= binop_expression(expr
->left
,
920 op_remove_assign(expr
->op
),
922 if (get_absolute_rl(binop_expr
, &rl
)) {
923 rl
= cast_rl(left_type
, rl
);
924 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
929 rl
= cast_rl(left_type
, alloc_rl(res_min
, res_max
));
930 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
935 static struct smatch_state
*increment_state(struct smatch_state
*state
)
937 sval_t min
= estate_min(state
);
938 sval_t max
= estate_max(state
);
940 if (!estate_rl(state
))
944 max
= sval_type_max(max
.type
);
946 if (!sval_is_min(min
) && !sval_is_max(min
))
948 if (!sval_is_min(max
) && !sval_is_max(max
))
950 return alloc_estate_range(min
, max
);
953 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
955 sval_t min
= estate_min(state
);
956 sval_t max
= estate_max(state
);
958 if (!estate_rl(state
))
962 min
= sval_type_min(min
.type
);
964 if (!sval_is_min(min
) && !sval_is_max(min
))
966 if (!sval_is_min(max
) && !sval_is_max(max
))
968 return alloc_estate_range(min
, max
);
971 static void unop_expr(struct expression
*expr
)
973 struct smatch_state
*state
;
975 if (expr
->smatch_flags
& Handled
)
979 case SPECIAL_INCREMENT
:
980 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
981 state
= increment_state(state
);
983 state
= alloc_estate_whole(get_type(expr
));
984 set_extra_expr_mod(expr
->unop
, state
);
986 case SPECIAL_DECREMENT
:
987 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
988 state
= decrement_state(state
);
990 state
= alloc_estate_whole(get_type(expr
));
991 set_extra_expr_mod(expr
->unop
, state
);
998 static void asm_expr(struct statement
*stmt
)
1001 struct expression
*expr
;
1002 struct symbol
*type
;
1005 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
1007 case 0: /* identifier */
1008 case 1: /* constraint */
1011 case 2: /* expression */
1013 type
= get_type(strip_expr(expr
));
1014 set_extra_expr_mod(expr
, alloc_estate_whole(type
));
1017 } END_FOR_EACH_PTR(expr
);
1020 static void check_dereference(struct expression
*expr
)
1022 if (outside_of_function())
1024 set_extra_expr_nomod(expr
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
1027 static void match_dereferences(struct expression
*expr
)
1029 if (expr
->type
!= EXPR_PREOP
)
1031 check_dereference(expr
->unop
);
1034 static void match_pointer_as_array(struct expression
*expr
)
1036 if (!is_array(expr
))
1038 check_dereference(get_array_base(expr
));
1041 static void set_param_dereferenced(struct expression
*arg
, char *key
, char *unused
)
1046 name
= get_variable_from_key(arg
, key
, &sym
);
1050 set_extra_nomod(name
, sym
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
1056 static sval_t
add_one(sval_t sval
)
1062 static sval_t
sub_one(sval_t sval
)
1068 static void handle_comparison(struct symbol
*type
, struct expression
*left
, int op
, struct expression
*right
)
1070 struct range_list
*left_orig
;
1071 struct range_list
*left_true
;
1072 struct range_list
*left_false
;
1073 struct range_list
*right_orig
;
1074 struct range_list
*right_true
;
1075 struct range_list
*right_false
;
1076 struct smatch_state
*left_true_state
;
1077 struct smatch_state
*left_false_state
;
1078 struct smatch_state
*right_true_state
;
1079 struct smatch_state
*right_false_state
;
1080 sval_t min
, max
, dummy
, hard_max
;
1081 int left_postop
= 0;
1082 int right_postop
= 0;
1084 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
1085 if (left
->type
== EXPR_POSTOP
) {
1086 left
->smatch_flags
|= Handled
;
1087 left_postop
= left
->op
;
1089 left
= strip_parens(left
->unop
);
1091 while (left
->type
== EXPR_ASSIGNMENT
)
1092 left
= strip_parens(left
->left
);
1094 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
1095 if (right
->type
== EXPR_POSTOP
) {
1096 right
->smatch_flags
|= Handled
;
1097 right_postop
= right
->op
;
1099 right
= strip_parens(right
->unop
);
1102 /* FIXME: we should be able to use get_real_absolute_rl() here but
1103 * apparently that is buggy.
1105 get_absolute_rl(left
, &left_orig
);
1106 left_orig
= cast_rl(type
, left_orig
);
1108 get_absolute_rl(right
, &right_orig
);
1109 right_orig
= cast_rl(type
, right_orig
);
1111 min
= sval_type_min(type
);
1112 max
= sval_type_max(type
);
1114 left_true
= clone_rl(left_orig
);
1115 left_false
= clone_rl(left_orig
);
1116 right_true
= clone_rl(right_orig
);
1117 right_false
= clone_rl(right_orig
);
1121 case SPECIAL_UNSIGNED_LT
:
1122 left_true
= remove_range(left_orig
, rl_max(right_orig
), max
);
1123 if (!sval_is_min(rl_min(right_orig
))) {
1124 left_false
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
1127 right_true
= remove_range(right_orig
, min
, rl_min(left_orig
));
1128 if (!sval_is_max(rl_max(left_orig
)))
1129 right_false
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
1131 case SPECIAL_UNSIGNED_LTE
:
1133 if (!sval_is_max(rl_max(right_orig
)))
1134 left_true
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
1135 left_false
= remove_range(left_orig
, min
, rl_min(right_orig
));
1137 if (!sval_is_min(rl_min(left_orig
)))
1138 right_true
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
1139 right_false
= remove_range(right_orig
, rl_max(left_orig
), max
);
1141 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
1142 left_false
= remove_range(left_false
, rl_min(left_orig
), rl_min(left_orig
));
1143 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
1144 right_false
= remove_range(right_false
, rl_max(left_orig
), rl_max(left_orig
));
1147 if (!sval_is_max(rl_max(right_orig
))) {
1148 left_true
= remove_range(left_true
, add_one(rl_max(right_orig
)), max
);
1150 if (!sval_is_min(rl_min(right_orig
))) {
1151 left_true
= remove_range(left_true
, min
, sub_one(rl_min(right_orig
)));
1153 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
1154 left_false
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
1156 if (!sval_is_max(rl_max(left_orig
)))
1157 right_true
= remove_range(right_true
, add_one(rl_max(left_orig
)), max
);
1158 if (!sval_is_min(rl_min(left_orig
)))
1159 right_true
= remove_range(right_true
, min
, sub_one(rl_min(left_orig
)));
1160 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
1161 right_false
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
1163 case SPECIAL_UNSIGNED_GTE
:
1165 if (!sval_is_min(rl_min(right_orig
)))
1166 left_true
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
1167 left_false
= remove_range(left_orig
, rl_max(right_orig
), max
);
1169 if (!sval_is_max(rl_max(left_orig
)))
1170 right_true
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
1171 right_false
= remove_range(right_orig
, min
, rl_min(left_orig
));
1173 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
1174 right_false
= remove_range(right_false
, rl_min(left_orig
), rl_min(left_orig
));
1175 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
1176 left_false
= remove_range(left_false
, rl_max(left_orig
), rl_max(left_orig
));
1179 case SPECIAL_UNSIGNED_GT
:
1180 left_true
= remove_range(left_orig
, min
, rl_min(right_orig
));
1181 if (!sval_is_max(rl_max(right_orig
)))
1182 left_false
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
1184 right_true
= remove_range(right_orig
, rl_max(left_orig
), max
);
1185 if (!sval_is_min(rl_min(left_orig
)))
1186 right_false
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
1188 case SPECIAL_NOTEQUAL
:
1189 if (!sval_is_max(rl_max(right_orig
)))
1190 left_false
= remove_range(left_false
, add_one(rl_max(right_orig
)), max
);
1191 if (!sval_is_min(rl_min(right_orig
)))
1192 left_false
= remove_range(left_false
, min
, sub_one(rl_min(right_orig
)));
1193 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
1194 left_true
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
1196 if (!sval_is_max(rl_max(left_orig
)))
1197 right_false
= remove_range(right_false
, add_one(rl_max(left_orig
)), max
);
1198 if (!sval_is_min(rl_min(left_orig
)))
1199 right_false
= remove_range(right_false
, min
, sub_one(rl_min(left_orig
)));
1200 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
1201 right_true
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
1207 left_true
= rl_truncate_cast(get_type(strip_expr(left
)), left_true
);
1208 left_false
= rl_truncate_cast(get_type(strip_expr(left
)), left_false
);
1209 right_true
= rl_truncate_cast(get_type(strip_expr(right
)), right_true
);
1210 right_false
= rl_truncate_cast(get_type(strip_expr(right
)), right_false
);
1212 left_true_state
= alloc_estate_rl(left_true
);
1213 left_false_state
= alloc_estate_rl(left_false
);
1214 right_true_state
= alloc_estate_rl(right_true
);
1215 right_false_state
= alloc_estate_rl(right_false
);
1219 case SPECIAL_UNSIGNED_LT
:
1220 case SPECIAL_UNSIGNED_LTE
:
1222 if (get_hard_max(right
, &dummy
))
1223 estate_set_hard_max(left_true_state
);
1224 if (get_hard_max(left
, &dummy
))
1225 estate_set_hard_max(right_false_state
);
1228 case SPECIAL_UNSIGNED_GT
:
1229 case SPECIAL_UNSIGNED_GTE
:
1231 if (get_hard_max(left
, &dummy
))
1232 estate_set_hard_max(right_true_state
);
1233 if (get_hard_max(right
, &dummy
))
1234 estate_set_hard_max(left_false_state
);
1240 case SPECIAL_UNSIGNED_LT
:
1241 case SPECIAL_UNSIGNED_LTE
:
1243 if (get_hard_max(right
, &hard_max
)) {
1244 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1246 estate_set_fuzzy_max(left_true_state
, hard_max
);
1248 if (get_implied_value(right
, &hard_max
)) {
1249 if (op
== SPECIAL_UNSIGNED_LTE
||
1252 estate_set_fuzzy_max(left_false_state
, hard_max
);
1254 if (get_hard_max(left
, &hard_max
)) {
1255 if (op
== SPECIAL_UNSIGNED_LTE
||
1258 estate_set_fuzzy_max(right_false_state
, hard_max
);
1260 if (get_implied_value(left
, &hard_max
)) {
1261 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1263 estate_set_fuzzy_max(right_true_state
, hard_max
);
1267 case SPECIAL_UNSIGNED_GT
:
1268 case SPECIAL_UNSIGNED_GTE
:
1270 if (get_hard_max(left
, &hard_max
)) {
1271 if (op
== '>' || op
== SPECIAL_UNSIGNED_GT
)
1273 estate_set_fuzzy_max(right_true_state
, hard_max
);
1275 if (get_implied_value(left
, &hard_max
)) {
1276 if (op
== SPECIAL_UNSIGNED_GTE
||
1279 estate_set_fuzzy_max(right_false_state
, hard_max
);
1281 if (get_hard_max(right
, &hard_max
)) {
1282 if (op
== SPECIAL_UNSIGNED_LTE
||
1285 estate_set_fuzzy_max(left_false_state
, hard_max
);
1287 if (get_implied_value(right
, &hard_max
)) {
1289 op
== SPECIAL_UNSIGNED_GT
)
1291 estate_set_fuzzy_max(left_true_state
, hard_max
);
1295 if (get_hard_max(left
, &hard_max
))
1296 estate_set_fuzzy_max(right_true_state
, hard_max
);
1297 if (get_hard_max(right
, &hard_max
))
1298 estate_set_fuzzy_max(left_true_state
, hard_max
);
1302 if (get_hard_max(left
, &hard_max
)) {
1303 estate_set_hard_max(left_true_state
);
1304 estate_set_hard_max(left_false_state
);
1306 if (get_hard_max(right
, &hard_max
)) {
1307 estate_set_hard_max(right_true_state
);
1308 estate_set_hard_max(right_false_state
);
1311 if (left_postop
== SPECIAL_INCREMENT
) {
1312 left_true_state
= increment_state(left_true_state
);
1313 left_false_state
= increment_state(left_false_state
);
1315 if (left_postop
== SPECIAL_DECREMENT
) {
1316 left_true_state
= decrement_state(left_true_state
);
1317 left_false_state
= decrement_state(left_false_state
);
1319 if (right_postop
== SPECIAL_INCREMENT
) {
1320 right_true_state
= increment_state(right_true_state
);
1321 right_false_state
= increment_state(right_false_state
);
1323 if (right_postop
== SPECIAL_DECREMENT
) {
1324 right_true_state
= decrement_state(right_true_state
);
1325 right_false_state
= decrement_state(right_false_state
);
1328 if (estate_rl(left_true_state
) && estates_equiv(left_true_state
, left_false_state
)) {
1329 left_true_state
= NULL
;
1330 left_false_state
= NULL
;
1333 if (estate_rl(right_true_state
) && estates_equiv(right_true_state
, right_false_state
)) {
1334 right_true_state
= NULL
;
1335 right_false_state
= NULL
;
1338 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
1339 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
1342 static int is_simple_math(struct expression
*expr
)
1346 if (expr
->type
!= EXPR_BINOP
)
1357 static void move_known_values(struct expression
**left_p
, struct expression
**right_p
)
1359 struct expression
*left
= *left_p
;
1360 struct expression
*right
= *right_p
;
1363 if (get_implied_value(left
, &sval
)) {
1364 if (!is_simple_math(right
))
1366 if (right
->op
== '*') {
1369 if (!get_value(right
->right
, &divisor
))
1371 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1373 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1374 *right_p
= right
->left
;
1377 if (right
->op
== '+' && get_value(right
->left
, &sval
)) {
1378 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->left
);
1379 *right_p
= right
->right
;
1382 if (get_value(right
->right
, &sval
)) {
1383 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1384 *right_p
= right
->left
;
1389 if (get_implied_value(right
, &sval
)) {
1390 if (!is_simple_math(left
))
1392 if (left
->op
== '*') {
1395 if (!get_value(left
->right
, &divisor
))
1397 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1399 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1400 *left_p
= left
->left
;
1403 if (left
->op
== '+' && get_value(left
->left
, &sval
)) {
1404 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->left
);
1405 *left_p
= left
->right
;
1409 if (get_value(left
->right
, &sval
)) {
1410 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1411 *left_p
= left
->left
;
1418 static int match_func_comparison(struct expression
*expr
)
1420 struct expression
*left
= strip_expr(expr
->left
);
1421 struct expression
*right
= strip_expr(expr
->right
);
1425 * fixme: think about this harder. We should always be trying to limit
1426 * the non-call side as well. If we can't determine the limitter does
1427 * that mean we aren't querying the database and are missing important
1431 if (left
->type
== EXPR_CALL
) {
1432 if (get_implied_value(left
, &sval
)) {
1433 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1436 function_comparison(left
, expr
->op
, right
);
1440 if (right
->type
== EXPR_CALL
) {
1441 if (get_implied_value(right
, &sval
)) {
1442 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1445 function_comparison(left
, expr
->op
, right
);
1452 static void match_comparison(struct expression
*expr
)
1454 struct expression
*left_orig
= strip_parens(expr
->left
);
1455 struct expression
*right_orig
= strip_parens(expr
->right
);
1456 struct expression
*left
, *right
;
1457 struct expression
*prev
;
1458 struct symbol
*type
;
1460 if (match_func_comparison(expr
))
1463 type
= get_type(expr
);
1465 type
= &llong_ctype
;
1469 move_known_values(&left
, &right
);
1470 handle_comparison(type
, left
, expr
->op
, right
);
1472 prev
= get_assigned_expr(left_orig
);
1473 if (is_simple_math(prev
) && has_variable(prev
, left_orig
) == 0) {
1476 move_known_values(&left
, &right
);
1477 handle_comparison(type
, left
, expr
->op
, right
);
1480 prev
= get_assigned_expr(right_orig
);
1481 if (is_simple_math(prev
) && has_variable(prev
, right_orig
) == 0) {
1484 move_known_values(&left
, &right
);
1485 handle_comparison(type
, left
, expr
->op
, right
);
1489 static void handle_AND_condition(struct expression
*expr
)
1491 struct range_list
*rl
= NULL
;
1494 if (get_implied_value(expr
->left
, &known
) && known
.value
> 0) {
1496 get_absolute_rl(expr
->right
, &rl
);
1497 rl
= remove_range(rl
, sval_type_val(known
.type
, 0), known
);
1498 set_extra_expr_true_false(expr
->right
, alloc_estate_rl(rl
), NULL
);
1502 if (get_implied_value(expr
->right
, &known
) && known
.value
> 0) {
1504 get_absolute_rl(expr
->left
, &rl
);
1505 rl
= remove_range(rl
, sval_type_val(known
.type
, 0), known
);
1506 set_extra_expr_true_false(expr
->left
, alloc_estate_rl(rl
), NULL
);
1511 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1512 void __extra_match_condition(struct expression
*expr
)
1514 struct smatch_state
*pre_state
;
1515 struct smatch_state
*true_state
;
1516 struct smatch_state
*false_state
;
1518 expr
= strip_expr(expr
);
1519 switch (expr
->type
) {
1521 function_comparison(expr
, SPECIAL_NOTEQUAL
, zero_expr());
1528 zero
= sval_blank(expr
);
1531 pre_state
= get_extra_state(expr
);
1532 true_state
= estate_filter_sval(pre_state
, zero
);
1533 if (possibly_true(expr
, SPECIAL_EQUAL
, zero_expr()))
1534 false_state
= alloc_estate_sval(zero
);
1536 false_state
= alloc_estate_empty();
1537 set_extra_expr_true_false(expr
, true_state
, false_state
);
1541 match_comparison(expr
);
1543 case EXPR_ASSIGNMENT
:
1544 __extra_match_condition(expr
->left
);
1547 if (expr
->op
== '&')
1548 handle_AND_condition(expr
);
1553 static void assume_indexes_are_valid(struct expression
*expr
)
1555 struct expression
*array_expr
;
1557 struct expression
*offset
;
1558 struct symbol
*offset_type
;
1559 struct range_list
*rl_before
;
1560 struct range_list
*rl_after
;
1561 struct range_list
*filter
= NULL
;
1564 expr
= strip_expr(expr
);
1565 if (!is_array(expr
))
1568 offset
= get_array_offset(expr
);
1569 offset_type
= get_type(offset
);
1570 if (offset_type
&& type_signed(offset_type
)) {
1571 filter
= alloc_rl(sval_type_min(offset_type
),
1572 sval_type_val(offset_type
, -1));
1575 array_expr
= get_array_base(expr
);
1576 array_size
= get_real_array_size(array_expr
);
1577 if (array_size
> 1) {
1578 size
= sval_type_val(offset_type
, array_size
);
1579 add_range(&filter
, size
, sval_type_max(offset_type
));
1584 get_absolute_rl(offset
, &rl_before
);
1585 rl_after
= rl_filter(rl_before
, filter
);
1586 if (rl_equiv(rl_before
, rl_after
))
1588 set_extra_expr_nomod(offset
, alloc_estate_rl(rl_after
));
1591 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1592 int implied_not_equal(struct expression
*expr
, long long val
)
1594 return !possibly_false(expr
, SPECIAL_NOTEQUAL
, value_expr(val
));
1597 int implied_not_equal_name_sym(char *name
, struct symbol
*sym
, long long val
)
1599 struct smatch_state
*estate
;
1601 estate
= get_state(SMATCH_EXTRA
, name
, sym
);
1604 if (!rl_has_sval(estate_rl(estate
), sval_type_val(estate_type(estate
), 0)))
1609 int parent_is_null_var_sym(const char *name
, struct symbol
*sym
)
1614 struct smatch_state
*state
;
1616 strncpy(buf
, name
, sizeof(buf
) - 1);
1617 buf
[sizeof(buf
) - 1] = '\0';
1620 while (*start
== '*') {
1622 state
= get_state(SMATCH_EXTRA
, start
, sym
);
1625 if (!estate_rl(state
))
1627 if (estate_min(state
).value
== 0 &&
1628 estate_max(state
).value
== 0)
1633 while (*start
== '&')
1636 while ((end
= strrchr(start
, '-'))) {
1638 state
= get_state(SMATCH_EXTRA
, start
, sym
);
1641 if (estate_min(state
).value
== 0 &&
1642 estate_max(state
).value
== 0)
1648 int parent_is_null(struct expression
*expr
)
1654 expr
= strip_expr(expr
);
1655 var
= expr_to_var_sym(expr
, &sym
);
1658 ret
= parent_is_null_var_sym(var
, sym
);
1664 static int param_used_callback(void *found
, int argc
, char **argv
, char **azColName
)
1670 static int filter_unused_kzalloc_info(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
1675 /* for function pointers assume everything is used */
1676 if (call
->fn
->type
!= EXPR_SYMBOL
)
1680 * kzalloc() information is treated as special because so there is just
1681 * a lot of stuff initialized to zero and it makes building the database
1682 * take hours and hours.
1684 * In theory, we should just remove this line and not pass any unused
1685 * information, but I'm not sure enough that this code works so I want
1686 * to hold off on that for now.
1688 if (!estate_get_single_value(sm
->state
, &sval
) || sval
.value
!= 0)
1691 run_sql(¶m_used_callback
, &found
,
1692 "select * from call_implies where %s and type = %d and parameter = %d and key = '%s';",
1693 get_static_filter(call
->fn
->symbol
), PARAM_USED
, param
, printed_name
);
1697 /* If the database is not built yet, then assume everything is used */
1698 run_sql(¶m_used_callback
, &found
,
1699 "select * from call_implies where %s and type = %d;",
1700 get_static_filter(call
->fn
->symbol
), PARAM_USED
);
1707 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
1709 if (estate_is_whole(sm
->state
))
1711 if (filter_unused_kzalloc_info(call
, param
, printed_name
, sm
))
1713 sql_insert_caller_info(call
, PARAM_VALUE
, param
, printed_name
, sm
->state
->name
);
1714 if (estate_has_fuzzy_max(sm
->state
))
1715 sql_insert_caller_info(call
, FUZZY_MAX
, param
, printed_name
,
1716 sval_to_str(estate_get_fuzzy_max(sm
->state
)));
1719 static void returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
1721 struct symbol
*returned_sym
;
1722 struct sm_state
*sm
;
1723 const char *param_name
;
1727 returned_sym
= expr_to_sym(expr
);
1731 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), sm
) {
1732 if (!estate_rl(sm
->state
))
1734 if (returned_sym
!= sm
->sym
)
1737 param_name
= get_param_name(sm
);
1740 if (strcmp(param_name
, "$") == 0)
1742 compare_str
= name_sym_to_param_comparison(sm
->name
, sm
->sym
);
1743 if (!compare_str
&& estate_is_whole(sm
->state
))
1745 snprintf(buf
, sizeof(buf
), "%s%s", sm
->state
->name
, compare_str
?: "");
1747 sql_insert_return_states(return_id
, return_ranges
, PARAM_VALUE
,
1748 -1, param_name
, buf
);
1749 } END_FOR_EACH_SM(sm
);
1752 static void db_limited_before(void)
1754 unmatched_stree
= clone_stree(__get_cur_stree());
1757 static void db_limited_after(void)
1759 free_stree(&unmatched_stree
);
1762 static void db_param_limit_filter(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
1764 struct expression
*arg
;
1767 struct sm_state
*sm
;
1768 struct symbol
*compare_type
, *var_type
;
1769 struct range_list
*rl
;
1770 struct range_list
*limit
;
1771 struct range_list
*new;
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
);
1782 name
= get_variable_from_key(arg
, key
, &sym
);
1786 if (strcmp(key
, "$") == 0)
1787 compare_type
= get_arg_type(expr
->fn
, param
);
1789 compare_type
= get_member_type_from_key(arg
, key
);
1791 sm
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
1793 rl
= estate_rl(sm
->state
);
1795 rl
= alloc_whole_rl(compare_type
);
1797 call_results_to_rl(expr
, compare_type
, value
, &limit
);
1798 new = rl_intersection(rl
, limit
);
1800 var_type
= get_member_type_from_key(arg
, key
);
1801 new = cast_rl(var_type
, new);
1803 /* We want to preserve the implications here */
1804 if (sm
&& rl_equiv(estate_rl(sm
->state
), new))
1807 if (op
== PARAM_LIMIT
)
1808 set_extra_nomod(name
, sym
, alloc_estate_rl(new));
1810 set_extra_mod(name
, sym
, alloc_estate_rl(new));
1817 static void db_param_limit(struct expression
*expr
, int param
, char *key
, char *value
)
1819 db_param_limit_filter(expr
, param
, key
, value
, PARAM_LIMIT
);
1822 static void db_param_filter(struct expression
*expr
, int param
, char *key
, char *value
)
1824 db_param_limit_filter(expr
, param
, key
, value
, PARAM_FILTER
);
1827 static void db_param_add_set(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
1829 struct expression
*arg
;
1832 struct symbol
*type
;
1833 struct smatch_state
*state
;
1834 struct range_list
*new = NULL
;
1835 struct range_list
*added
= NULL
;
1837 while (expr
->type
== EXPR_ASSIGNMENT
)
1838 expr
= strip_expr(expr
->right
);
1839 if (expr
->type
!= EXPR_CALL
)
1842 arg
= get_argument_from_call_expr(expr
->args
, param
);
1845 type
= get_member_type_from_key(arg
, key
);
1846 name
= get_variable_from_key(arg
, key
, &sym
);
1850 state
= get_state(SMATCH_EXTRA
, name
, sym
);
1852 new = estate_rl(state
);
1854 call_results_to_rl(expr
, type
, value
, &added
);
1856 if (op
== PARAM_SET
)
1859 new = rl_union(new, added
);
1861 set_extra_mod(name
, sym
, alloc_estate_rl(new));
1866 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
1868 db_param_add_set(expr
, param
, key
, value
, PARAM_ADD
);
1871 static void db_param_set(struct expression
*expr
, int param
, char *key
, char *value
)
1873 db_param_add_set(expr
, param
, key
, value
, PARAM_SET
);
1876 static void db_param_value(struct expression
*expr
, int param
, char *key
, char *value
)
1878 struct expression
*call
;
1881 struct symbol
*type
;
1882 struct range_list
*rl
= NULL
;
1888 while (call
->type
== EXPR_ASSIGNMENT
)
1889 call
= strip_expr(call
->right
);
1890 if (call
->type
!= EXPR_CALL
)
1893 type
= get_member_type_from_key(expr
->left
, key
);
1894 name
= get_variable_from_key(expr
->left
, key
, &sym
);
1898 call_results_to_rl(call
, type
, value
, &rl
);
1900 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
1905 static void match_call_info(struct expression
*expr
)
1907 struct smatch_state
*state
;
1908 struct range_list
*rl
= NULL
;
1909 struct expression
*arg
;
1910 struct symbol
*type
;
1913 FOR_EACH_PTR(expr
->args
, arg
) {
1914 type
= get_arg_type(expr
->fn
, i
);
1916 if (get_implied_rl(arg
, &rl
))
1917 rl
= cast_rl(type
, rl
);
1919 rl
= cast_rl(type
, alloc_whole_rl(get_type(arg
)));
1921 if (!is_whole_rl(rl
))
1922 sql_insert_caller_info(expr
, PARAM_VALUE
, i
, "$", show_rl(rl
));
1923 state
= get_state_expr(SMATCH_EXTRA
, arg
);
1924 if (estate_has_fuzzy_max(state
)) {
1925 sql_insert_caller_info(expr
, FUZZY_MAX
, i
, "$",
1926 sval_to_str(estate_get_fuzzy_max(state
)));
1929 } END_FOR_EACH_PTR(arg
);
1932 static void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1934 struct range_list
*rl
= NULL
;
1935 struct smatch_state
*state
;
1936 struct symbol
*type
;
1939 if (strcmp(key
, "*$") == 0)
1940 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
1941 else if (strncmp(key
, "$", 1) == 0)
1942 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
1946 type
= get_member_type_from_key(symbol_expression(sym
), key
);
1947 str_to_rl(type
, value
, &rl
);
1948 state
= alloc_estate_rl(rl
);
1949 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
1952 static void set_param_hard_max(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1954 struct range_list
*rl
= NULL
;
1955 struct smatch_state
*state
;
1956 struct symbol
*type
;
1960 if (strcmp(key
, "*$") == 0)
1961 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
1962 else if (strncmp(key
, "$", 1) == 0)
1963 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
1967 state
= get_state(SMATCH_EXTRA
, fullname
, sym
);
1970 type
= get_member_type_from_key(symbol_expression(sym
), key
);
1971 str_to_rl(type
, value
, &rl
);
1972 if (!rl_to_sval(rl
, &max
))
1974 estate_set_fuzzy_max(state
, max
);
1977 struct smatch_state
*get_extra_state(struct expression
*expr
)
1981 struct smatch_state
*ret
= NULL
;
1982 struct range_list
*rl
;
1984 if (is_pointer(expr
) && get_address_rl(expr
, &rl
))
1985 return alloc_estate_rl(rl
);
1987 name
= expr_to_known_chunk_sym(expr
, &sym
);
1991 ret
= get_state(SMATCH_EXTRA
, name
, sym
);
1997 void register_smatch_extra(int id
)
2001 add_merge_hook(my_id
, &merge_estates
);
2002 add_unmatched_state_hook(my_id
, &unmatched_state
);
2003 select_caller_info_hook(set_param_value
, PARAM_VALUE
);
2004 select_caller_info_hook(set_param_hard_max
, FUZZY_MAX
);
2005 select_return_states_before(&db_limited_before
);
2006 select_return_states_hook(PARAM_LIMIT
, &db_param_limit
);
2007 select_return_states_hook(PARAM_FILTER
, &db_param_filter
);
2008 select_return_states_hook(PARAM_ADD
, &db_param_add
);
2009 select_return_states_hook(PARAM_SET
, &db_param_set
);
2010 select_return_states_hook(PARAM_VALUE
, &db_param_value
);
2011 select_return_states_after(&db_limited_after
);
2014 static void match_link_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
2016 struct var_sym_list
*links
;
2017 struct var_sym
*tmp
;
2018 struct smatch_state
*state
;
2020 links
= sm
->state
->data
;
2022 FOR_EACH_PTR(links
, tmp
) {
2023 state
= get_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
);
2026 set_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
, alloc_estate_whole(estate_type(state
)));
2027 } END_FOR_EACH_PTR(tmp
);
2028 set_state(link_id
, sm
->name
, sm
->sym
, &undefined
);
2031 void register_smatch_extra_links(int id
)
2036 void register_smatch_extra_late(int id
)
2038 add_merge_hook(link_id
, &merge_link_states
);
2039 add_modification_hook(link_id
, &match_link_modify
);
2040 add_hook(&match_dereferences
, DEREF_HOOK
);
2041 add_hook(&match_pointer_as_array
, OP_HOOK
);
2042 select_call_implies_hook(DEREFERENCE
, &set_param_dereferenced
);
2043 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
2044 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
2045 add_hook(&match_assign
, GLOBAL_ASSIGNMENT_HOOK
);
2046 add_hook(&unop_expr
, OP_HOOK
);
2047 add_hook(&asm_expr
, ASM_HOOK
);
2048 add_untracked_param_hook(&match_untracked_array
);
2050 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2051 add_member_info_callback(my_id
, struct_member_callback
);
2052 add_split_return_callback(&returned_struct_members
);
2054 add_hook(&assume_indexes_are_valid
, OP_HOOK
);