2 * Copyright (C) 2008 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * smatch_extra.c is supposed to track the value of every variable.
31 #include "smatch_slist.h"
32 #include "smatch_extra.h"
37 static void match_link_modify(struct sm_state
*sm
, struct expression
*mod_expr
);
39 struct string_list
*__ignored_macros
= NULL
;
40 static int in_warn_on_macro(void)
42 struct statement
*stmt
;
46 stmt
= get_current_statement();
49 macro
= get_macro_name(stmt
->pos
);
53 FOR_EACH_PTR(__ignored_macros
, tmp
) {
54 if (!strcmp(tmp
, macro
))
56 } END_FOR_EACH_PTR(tmp
);
60 typedef void (mod_hook
)(const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
61 DECLARE_PTR_LIST(void_fn_list
, mod_hook
*);
62 static struct void_fn_list
*extra_mod_hooks
;
63 void add_extra_mod_hook(mod_hook
*fn
)
65 mod_hook
**p
= malloc(sizeof(mod_hook
*));
67 add_ptr_list(&extra_mod_hooks
, p
);
70 void call_extra_mod_hooks(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
74 FOR_EACH_PTR(extra_mod_hooks
, fn
) {
75 (*fn
)(name
, sym
, state
);
76 } END_FOR_EACH_PTR(fn
);
79 struct sm_state
*set_extra_mod_helper(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
81 remove_from_equiv(name
, sym
);
82 call_extra_mod_hooks(name
, sym
, state
);
83 if (__in_fake_assign
&& estate_is_unknown(state
) && !get_state(SMATCH_EXTRA
, name
, sym
))
85 return set_state(SMATCH_EXTRA
, name
, sym
, state
);
88 static char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
90 struct expression
*assigned
;
91 char *orig_name
= NULL
;
101 skip
= strlen(sym
->ident
->name
);
102 if (name
[skip
] != '-' || name
[skip
+ 1] != '>')
106 assigned
= get_assigned_expr_name_sym(sym
->ident
->name
, sym
);
109 if (assigned
->type
== EXPR_PREOP
|| assigned
->op
== '&') {
111 orig_name
= expr_to_var_sym(assigned
, new_sym
);
112 if (!orig_name
|| !*new_sym
)
115 snprintf(buf
, sizeof(buf
), "%s.%s", orig_name
+ 1, name
+ skip
);
116 ret
= alloc_string(buf
);
117 free_string(orig_name
);
121 if (assigned
->type
!= EXPR_DEREF
)
124 orig_name
= expr_to_var_sym(assigned
, new_sym
);
125 if (!orig_name
|| !*new_sym
)
128 snprintf(buf
, sizeof(buf
), "%s->%s", orig_name
, name
+ skip
);
129 ret
= alloc_string(buf
);
130 free_string(orig_name
);
134 free_string(orig_name
);
138 struct sm_state
*set_extra_mod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
141 struct symbol
*new_sym
;
144 if (in_warn_on_macro())
146 sm
= set_extra_mod_helper(name
, sym
, state
);
147 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
148 if (new_name
&& new_sym
)
149 set_extra_mod_helper(new_name
, new_sym
, state
);
150 free_string(new_name
);
154 struct sm_state
*set_extra_expr_mod(struct expression
*expr
, struct smatch_state
*state
)
158 struct sm_state
*ret
= NULL
;
160 expr
= strip_expr(expr
);
161 name
= expr_to_var_sym(expr
, &sym
);
164 ret
= set_extra_mod(name
, sym
, state
);
170 void set_extra_nomod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
173 struct symbol
*new_sym
;
174 struct relation
*rel
;
175 struct smatch_state
*orig_state
;
177 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
179 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
180 if (new_name
&& new_sym
)
181 set_state(SMATCH_EXTRA
, new_name
, new_sym
, state
);
182 free_string(new_name
);
184 if (!estate_related(orig_state
)) {
185 set_state(SMATCH_EXTRA
, name
, sym
, state
);
189 set_related(state
, estate_related(orig_state
));
190 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
191 if (option_debug_related
)
192 sm_msg("%s updating related %s to %s", name
, rel
->name
, state
->name
);
193 set_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
, state
);
194 } END_FOR_EACH_PTR(rel
);
198 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
200 void set_extra_expr_nomod(struct expression
*expr
, struct smatch_state
*state
)
205 name
= expr_to_var_sym(expr
, &sym
);
208 set_extra_nomod(name
, sym
, state
);
214 static void set_extra_true_false(const char *name
, struct symbol
*sym
,
215 struct smatch_state
*true_state
,
216 struct smatch_state
*false_state
)
219 struct symbol
*new_sym
;
220 struct relation
*rel
;
221 struct smatch_state
*orig_state
;
223 if (in_warn_on_macro())
226 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
227 if (new_name
&& new_sym
)
228 set_true_false_states(SMATCH_EXTRA
, new_name
, new_sym
, true_state
, false_state
);
229 free_string(new_name
);
231 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
233 if (!estate_related(orig_state
)) {
234 set_true_false_states(SMATCH_EXTRA
, name
, sym
, true_state
, false_state
);
239 set_related(true_state
, estate_related(orig_state
));
241 set_related(false_state
, estate_related(orig_state
));
243 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
244 set_true_false_states(SMATCH_EXTRA
, rel
->name
, rel
->sym
,
245 true_state
, false_state
);
246 } END_FOR_EACH_PTR(rel
);
249 static void set_extra_chunk_true_false(struct expression
*expr
,
250 struct smatch_state
*true_state
,
251 struct smatch_state
*false_state
)
253 struct var_sym_list
*vsl
;
259 if (in_warn_on_macro())
262 type
= get_type(expr
);
266 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
269 FOR_EACH_PTR(vsl
, vs
) {
270 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
271 } END_FOR_EACH_PTR(vs
);
273 set_true_false_states(SMATCH_EXTRA
, name
, sym
,
274 clone_estate(true_state
),
275 clone_estate(false_state
));
280 static void set_extra_expr_true_false(struct expression
*expr
,
281 struct smatch_state
*true_state
,
282 struct smatch_state
*false_state
)
288 if (get_value(expr
, &sval
))
291 expr
= strip_expr(expr
);
292 name
= expr_to_var_sym(expr
, &sym
);
295 set_extra_chunk_true_false(expr
, true_state
, false_state
);
298 set_extra_true_false(name
, sym
, true_state
, false_state
);
302 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
304 struct expression
*iter_var
;
305 struct expression
*condition
;
307 struct smatch_state
*estate
;
310 condition
= strip_expr(loop
->iterator_pre_condition
);
313 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
315 if (condition
->op
!= SPECIAL_DECREMENT
)
318 iter_var
= condition
->unop
;
319 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
322 if (sval_cmp_val(estate_min(sm
->state
), 0) < 0)
324 start
= estate_max(sm
->state
);
325 if (sval_cmp_val(start
, 0) <= 0)
327 if (!sval_is_max(start
))
330 if (condition
->type
== EXPR_PREOP
) {
331 estate
= alloc_estate_range(sval_type_val(start
.type
, 1), start
);
332 if (estate_has_hard_max(sm
->state
))
333 estate_set_hard_max(estate
);
334 estate_copy_fuzzy_max(estate
, sm
->state
);
335 set_extra_expr_mod(iter_var
, estate
);
337 if (condition
->type
== EXPR_POSTOP
) {
338 estate
= alloc_estate_range(sval_type_val(start
.type
, 0), start
);
339 if (estate_has_hard_max(sm
->state
))
340 estate_set_hard_max(estate
);
341 estate_copy_fuzzy_max(estate
, sm
->state
);
342 set_extra_expr_mod(iter_var
, estate
);
344 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
347 static struct sm_state
*handle_canonical_for_inc(struct expression
*iter_expr
,
348 struct expression
*condition
)
350 struct expression
*iter_var
;
352 struct smatch_state
*estate
;
353 sval_t start
, end
, max
;
355 iter_var
= iter_expr
->unop
;
356 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
359 if (!estate_get_single_value(sm
->state
, &start
))
361 if (get_implied_max(condition
->right
, &end
))
362 end
= sval_cast(get_type(iter_var
), end
);
364 end
= sval_type_max(get_type(iter_var
));
366 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
369 switch (condition
->op
) {
370 case SPECIAL_UNSIGNED_LT
:
371 case SPECIAL_NOTEQUAL
:
373 if (!sval_is_min(end
))
376 case SPECIAL_UNSIGNED_LTE
:
382 if (sval_cmp(end
, start
) < 0)
384 estate
= alloc_estate_range(start
, end
);
385 if (get_hard_max(condition
->right
, &max
)) {
386 estate_set_hard_max(estate
);
387 if (condition
->op
== '<' ||
388 condition
->op
== SPECIAL_UNSIGNED_LT
||
389 condition
->op
== SPECIAL_NOTEQUAL
)
391 estate_set_fuzzy_max(estate
, max
);
393 set_extra_expr_mod(iter_var
, estate
);
394 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
397 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
398 struct expression
*condition
)
400 struct expression
*iter_var
;
402 struct smatch_state
*estate
;
405 iter_var
= iter_expr
->unop
;
406 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
409 if (!estate_get_single_value(sm
->state
, &start
))
411 if (!get_implied_min(condition
->right
, &end
))
412 end
= sval_type_min(get_type(iter_var
));
413 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
416 switch (condition
->op
) {
417 case SPECIAL_NOTEQUAL
:
419 if (!sval_is_min(end
) && !sval_is_max(end
))
427 if (sval_cmp(end
, start
) > 0)
429 estate
= alloc_estate_range(end
, start
);
430 estate_set_hard_max(estate
);
431 estate_set_fuzzy_max(estate
, estate_get_fuzzy_max(estate
));
432 set_extra_expr_mod(iter_var
, estate
);
433 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
436 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
438 struct expression
*iter_expr
;
439 struct expression
*condition
;
441 if (!loop
->iterator_post_statement
)
443 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
445 iter_expr
= loop
->iterator_post_statement
->expression
;
446 if (!loop
->iterator_pre_condition
)
448 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
450 condition
= loop
->iterator_pre_condition
;
452 if (iter_expr
->op
== SPECIAL_INCREMENT
)
453 return handle_canonical_for_inc(iter_expr
, condition
);
454 if (iter_expr
->op
== SPECIAL_DECREMENT
)
455 return handle_canonical_for_dec(iter_expr
, condition
);
459 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct stree
**stree
)
461 struct sm_state
*ret
;
463 __push_fake_cur_stree();
464 if (!loop
->iterator_post_statement
)
465 ret
= handle_canonical_while_count_down(loop
);
467 ret
= handle_canonical_for_loops(loop
);
468 *stree
= __pop_fake_cur_stree();
472 int __iterator_unchanged(struct sm_state
*sm
)
476 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
481 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
485 /* paranoid checking. prolly not needed */
486 condition
= strip_expr(condition
);
489 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
491 if (condition
->op
!= SPECIAL_DECREMENT
)
493 after_value
= estate_min(sm
->state
);
495 set_extra_mod(sm
->name
, sm
->sym
, alloc_estate_sval(after_value
));
498 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
499 struct statement
*iterator
,
500 struct expression
*condition
)
502 struct expression
*iter_expr
;
504 struct smatch_state
*state
;
507 while_count_down_after(sm
, condition
);
511 iter_expr
= iterator
->expression
;
513 if (condition
->type
!= EXPR_COMPARE
)
515 if (iter_expr
->op
== SPECIAL_INCREMENT
) {
516 limit
= sval_binop(estate_max(sm
->state
), '+',
517 sval_type_val(estate_type(sm
->state
), 1));
519 limit
= sval_binop(estate_min(sm
->state
), '-',
520 sval_type_val(estate_type(sm
->state
), 1));
522 if (!estate_has_hard_max(sm
->state
) && !__has_breaks()) {
523 if (iter_expr
->op
== SPECIAL_INCREMENT
)
524 state
= alloc_estate_range(estate_min(sm
->state
), limit
);
526 state
= alloc_estate_range(limit
, estate_max(sm
->state
));
528 state
= alloc_estate_sval(limit
);
530 if (!estate_has_hard_max(sm
->state
)) {
531 estate_clear_hard_max(state
);
533 if (estate_has_fuzzy_max(sm
->state
)) {
534 sval_t hmax
= estate_get_fuzzy_max(sm
->state
);
535 sval_t max
= estate_max(sm
->state
);
537 if (sval_cmp(hmax
, max
) != 0)
538 estate_clear_fuzzy_max(state
);
539 } else if (!estate_has_fuzzy_max(sm
->state
)) {
540 estate_clear_fuzzy_max(state
);
543 set_extra_mod(sm
->name
, sm
->sym
, state
);
546 static struct stree
*unmatched_stree
;
547 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
549 struct smatch_state
*state
;
551 if (unmatched_stree
) {
552 state
= get_state_stree(unmatched_stree
, SMATCH_EXTRA
, sm
->name
, sm
->sym
);
556 if (parent_is_gone_var_sym(sm
->name
, sm
->sym
))
557 return alloc_estate_empty();
558 return alloc_estate_whole(estate_type(sm
->state
));
561 static void clear_the_pointed_at(struct expression
*expr
)
566 struct sm_state
*tmp
;
568 name
= expr_to_var_sym(expr
, &sym
);
572 stree
= __get_cur_stree();
573 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
574 if (tmp
->name
[0] != '*')
578 if (strcmp(tmp
->name
+ 1, name
) != 0)
580 set_extra_mod(tmp
->name
, tmp
->sym
, alloc_estate_whole(estate_type(tmp
->state
)));
581 } END_FOR_EACH_SM(tmp
);
587 static void match_function_call(struct expression
*expr
)
589 struct expression
*arg
;
590 struct expression
*tmp
;
592 /* if we have the db this is handled in smatch_function_hooks.c */
595 if (inlinable(expr
->fn
))
598 FOR_EACH_PTR(expr
->args
, arg
) {
599 tmp
= strip_expr(arg
);
600 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
601 set_extra_expr_mod(tmp
->unop
, alloc_estate_whole(get_type(tmp
->unop
)));
603 clear_the_pointed_at(tmp
);
604 } END_FOR_EACH_PTR(arg
);
607 static int types_equiv_or_pointer(struct symbol
*one
, struct symbol
*two
)
611 if (one
->type
== SYM_PTR
&& two
->type
== SYM_PTR
)
613 return types_equiv(one
, two
);
616 static void save_chunk_info(struct expression
*left
, struct expression
*right
)
618 struct var_sym_list
*vsl
;
620 struct expression
*add_expr
;
626 if (right
->type
!= EXPR_BINOP
|| right
->op
!= '-')
628 if (!get_value(right
->left
, &sval
))
630 if (!expr_to_sym(right
->right
))
633 add_expr
= binop_expression(left
, '+', right
->right
);
634 type
= get_type(add_expr
);
637 name
= expr_to_chunk_sym_vsl(add_expr
, &sym
, &vsl
);
640 FOR_EACH_PTR(vsl
, vs
) {
641 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
642 } END_FOR_EACH_PTR(vs
);
644 set_state(SMATCH_EXTRA
, name
, sym
, alloc_estate_sval(sval_cast(type
, sval
)));
649 static void do_array_assign(struct expression
*left
, int op
, struct expression
*right
)
651 struct expression
*array
;
652 struct expression
*offset
;
653 struct var_sym_list
*vsl
;
657 struct range_list
*rl
;
660 array
= get_array_base(left
);
661 offset
= get_array_offset(left
);
662 get_absolute_rl(right
, &rl
);
663 rl
= cast_rl(get_type(left
), rl
);
665 name
= expr_to_chunk_sym_vsl(left
, &sym
, &vsl
);
667 if (!name
|| !vsl
|| op
!= '=' || !get_value(offset
, &sval
) || is_whole_rl(rl
)) {
670 sm
= get_sm_state_expr(link_id
, array
);
672 match_link_modify(sm
, NULL
);
676 FOR_EACH_PTR(vsl
, vs
) {
677 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
678 } END_FOR_EACH_PTR(vs
);
680 set_state(SMATCH_EXTRA
, name
, sym
, alloc_estate_rl(rl
));
685 static void match_vanilla_assign(struct expression
*left
, struct expression
*right
)
687 struct range_list
*rl
= NULL
;
688 struct symbol
*right_sym
;
689 struct symbol
*left_type
;
690 struct symbol
*right_type
;
691 char *right_name
= NULL
;
695 struct smatch_state
*state
;
700 save_chunk_info(left
, right
);
702 name
= expr_to_var_sym(left
, &sym
);
705 do_array_assign(left
, '=', right
);
709 left_type
= get_type(left
);
710 right_type
= get_type(right
);
712 right_name
= expr_to_var_sym(right
, &right_sym
);
714 if (!__in_fake_assign
&&
715 !(right
->type
== EXPR_PREOP
&& right
->op
== '&') &&
716 right_name
&& right_sym
&&
717 types_equiv_or_pointer(left_type
, right_type
) &&
718 !has_symbol(right
, sym
)) {
719 set_equiv(left
, right
);
723 if (is_pointer(right
) && get_address_rl(right
, &rl
)) {
724 state
= alloc_estate_rl(rl
);
725 } else if (get_implied_rl(right
, &rl
)) {
726 rl
= cast_rl(left_type
, rl
);
727 state
= alloc_estate_rl(rl
);
728 if (get_hard_max(right
, &max
)) {
729 estate_set_hard_max(state
);
730 estate_set_fuzzy_max(state
, max
);
733 rl
= alloc_whole_rl(right_type
);
734 rl
= cast_rl(left_type
, rl
);
735 state
= alloc_estate_rl(rl
);
737 set_extra_mod(name
, sym
, state
);
739 free_string(right_name
);
742 static int op_remove_assign(int op
)
745 case SPECIAL_ADD_ASSIGN
:
747 case SPECIAL_SUB_ASSIGN
:
749 case SPECIAL_MUL_ASSIGN
:
751 case SPECIAL_DIV_ASSIGN
:
753 case SPECIAL_MOD_ASSIGN
:
755 case SPECIAL_AND_ASSIGN
:
757 case SPECIAL_OR_ASSIGN
:
759 case SPECIAL_XOR_ASSIGN
:
761 case SPECIAL_SHL_ASSIGN
:
762 return SPECIAL_LEFTSHIFT
;
763 case SPECIAL_SHR_ASSIGN
:
764 return SPECIAL_RIGHTSHIFT
;
770 static void match_assign(struct expression
*expr
)
772 struct range_list
*rl
= NULL
;
773 struct expression
*left
;
774 struct expression
*right
;
775 struct expression
*binop_expr
;
776 struct symbol
*left_type
;
777 struct symbol
*right_type
;
780 sval_t left_min
, left_max
;
781 sval_t right_min
, right_max
;
782 sval_t res_min
, res_max
;
784 left
= strip_expr(expr
->left
);
786 right
= strip_parens(expr
->right
);
787 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
788 right
= strip_parens(right
->left
);
790 if (expr
->op
== '=' && is_condition(expr
->right
))
791 return; /* handled in smatch_condition.c */
792 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
793 return; /* handled in smatch_function_hooks.c */
794 if (expr
->op
== '=') {
795 match_vanilla_assign(left
, right
);
799 name
= expr_to_var_sym(left
, &sym
);
803 left_type
= get_type(left
);
804 right_type
= get_type(right
);
806 res_min
= sval_type_min(left_type
);
807 res_max
= sval_type_max(left_type
);
810 case SPECIAL_ADD_ASSIGN
:
811 get_absolute_max(left
, &left_max
);
812 get_absolute_max(right
, &right_max
);
813 if (sval_binop_overflows(left_max
, '+', right_max
))
815 if (get_implied_min(left
, &left_min
) &&
816 !sval_is_negative_min(left_min
) &&
817 get_implied_min(right
, &right_min
) &&
818 !sval_is_negative_min(right_min
)) {
819 res_min
= sval_binop(left_min
, '+', right_min
);
820 res_min
= sval_cast(right_type
, res_min
);
822 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
824 res_max
= sval_binop(left_max
, '+', right_max
);
825 res_max
= sval_cast(right_type
, res_max
);
827 case SPECIAL_SUB_ASSIGN
:
828 if (get_implied_max(left
, &left_max
) &&
829 !sval_is_max(left_max
) &&
830 get_implied_min(right
, &right_min
) &&
831 !sval_is_min(right_min
)) {
832 res_max
= sval_binop(left_max
, '-', right_min
);
833 res_max
= sval_cast(right_type
, res_max
);
837 if (get_implied_min(left
, &left_min
) &&
838 !sval_is_min(left_min
) &&
839 get_implied_max(right
, &right_max
) &&
840 !sval_is_max(right_max
)) {
841 res_min
= sval_binop(left_min
, '-', right_max
);
842 res_min
= sval_cast(right_type
, res_min
);
845 case SPECIAL_AND_ASSIGN
:
846 case SPECIAL_MOD_ASSIGN
:
847 case SPECIAL_SHL_ASSIGN
:
848 case SPECIAL_SHR_ASSIGN
:
849 case SPECIAL_OR_ASSIGN
:
850 case SPECIAL_XOR_ASSIGN
:
851 case SPECIAL_MUL_ASSIGN
:
852 case SPECIAL_DIV_ASSIGN
:
853 binop_expr
= binop_expression(expr
->left
,
854 op_remove_assign(expr
->op
),
856 if (get_absolute_rl(binop_expr
, &rl
)) {
857 rl
= cast_rl(left_type
, rl
);
858 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
863 rl
= cast_rl(left_type
, alloc_rl(res_min
, res_max
));
864 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
869 static struct smatch_state
*increment_state(struct smatch_state
*state
)
871 sval_t min
= estate_min(state
);
872 sval_t max
= estate_max(state
);
874 if (!estate_rl(state
))
878 max
= sval_type_max(max
.type
);
880 if (!sval_is_min(min
) && !sval_is_max(min
))
882 if (!sval_is_min(max
) && !sval_is_max(max
))
884 return alloc_estate_range(min
, max
);
887 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
889 sval_t min
= estate_min(state
);
890 sval_t max
= estate_max(state
);
892 if (!estate_rl(state
))
896 min
= sval_type_min(min
.type
);
898 if (!sval_is_min(min
) && !sval_is_max(min
))
900 if (!sval_is_min(max
) && !sval_is_max(max
))
902 return alloc_estate_range(min
, max
);
905 static void unop_expr(struct expression
*expr
)
907 struct smatch_state
*state
;
909 if (expr
->smatch_flags
& Handled
)
913 case SPECIAL_INCREMENT
:
914 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
915 state
= increment_state(state
);
917 state
= alloc_estate_whole(get_type(expr
));
918 set_extra_expr_mod(expr
->unop
, state
);
920 case SPECIAL_DECREMENT
:
921 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
922 state
= decrement_state(state
);
924 state
= alloc_estate_whole(get_type(expr
));
925 set_extra_expr_mod(expr
->unop
, state
);
932 static void asm_expr(struct statement
*stmt
)
935 struct expression
*expr
;
939 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
941 case 0: /* identifier */
942 case 1: /* constraint */
945 case 2: /* expression */
947 type
= get_type(strip_expr(expr
));
948 set_extra_expr_mod(expr
, alloc_estate_whole(type
));
951 } END_FOR_EACH_PTR(expr
);
954 static void check_dereference(struct expression
*expr
)
956 if (outside_of_function())
958 set_extra_expr_nomod(expr
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
961 static void match_dereferences(struct expression
*expr
)
963 if (expr
->type
!= EXPR_PREOP
)
965 check_dereference(expr
->unop
);
968 static void match_pointer_as_array(struct expression
*expr
)
972 check_dereference(get_array_base(expr
));
975 static void set_param_dereferenced(struct expression
*arg
, char *key
, char *unused
)
980 name
= get_variable_from_key(arg
, key
, &sym
);
984 set_extra_nomod(name
, sym
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
990 static sval_t
add_one(sval_t sval
)
996 static sval_t
sub_one(sval_t sval
)
1002 static void handle_comparison(struct symbol
*type
, struct expression
*left
, int op
, struct expression
*right
)
1004 struct range_list
*left_orig
;
1005 struct range_list
*left_true
;
1006 struct range_list
*left_false
;
1007 struct range_list
*right_orig
;
1008 struct range_list
*right_true
;
1009 struct range_list
*right_false
;
1010 struct smatch_state
*left_true_state
;
1011 struct smatch_state
*left_false_state
;
1012 struct smatch_state
*right_true_state
;
1013 struct smatch_state
*right_false_state
;
1014 sval_t min
, max
, dummy
, hard_max
;
1015 int left_postop
= 0;
1016 int right_postop
= 0;
1018 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
1019 if (left
->type
== EXPR_POSTOP
) {
1020 left
->smatch_flags
|= Handled
;
1021 left_postop
= left
->op
;
1023 left
= strip_expr(left
->unop
);
1025 while (left
->type
== EXPR_ASSIGNMENT
)
1026 left
= strip_expr(left
->left
);
1028 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
1029 if (right
->type
== EXPR_POSTOP
) {
1030 right
->smatch_flags
|= Handled
;
1031 right_postop
= right
->op
;
1033 right
= strip_expr(right
->unop
);
1036 if (get_implied_rl(left
, &left_orig
)) {
1037 left_orig
= cast_rl(type
, left_orig
);
1039 min
= sval_type_min(get_type(left
));
1040 max
= sval_type_max(get_type(left
));
1041 left_orig
= cast_rl(type
, alloc_rl(min
, max
));
1044 if (get_implied_rl(right
, &right_orig
)) {
1045 right_orig
= cast_rl(type
, right_orig
);
1047 min
= sval_type_min(get_type(right
));
1048 max
= sval_type_max(get_type(right
));
1049 right_orig
= cast_rl(type
, alloc_rl(min
, max
));
1051 min
= sval_type_min(type
);
1052 max
= sval_type_max(type
);
1054 left_true
= clone_rl(left_orig
);
1055 left_false
= clone_rl(left_orig
);
1056 right_true
= clone_rl(right_orig
);
1057 right_false
= clone_rl(right_orig
);
1061 case SPECIAL_UNSIGNED_LT
:
1062 left_true
= remove_range(left_orig
, rl_max(right_orig
), max
);
1063 if (!sval_is_min(rl_min(right_orig
))) {
1064 left_false
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
1067 right_true
= remove_range(right_orig
, min
, rl_min(left_orig
));
1068 if (!sval_is_max(rl_max(left_orig
)))
1069 right_false
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
1071 case SPECIAL_UNSIGNED_LTE
:
1073 if (!sval_is_max(rl_max(right_orig
)))
1074 left_true
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
1075 left_false
= remove_range(left_orig
, min
, rl_min(right_orig
));
1077 if (!sval_is_min(rl_min(left_orig
)))
1078 right_true
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
1079 right_false
= remove_range(right_orig
, rl_max(left_orig
), max
);
1081 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
1082 left_false
= remove_range(left_false
, rl_min(left_orig
), rl_min(left_orig
));
1083 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
1084 right_false
= remove_range(right_false
, rl_max(left_orig
), rl_max(left_orig
));
1087 if (!sval_is_max(rl_max(right_orig
))) {
1088 left_true
= remove_range(left_true
, add_one(rl_max(right_orig
)), max
);
1090 if (!sval_is_min(rl_min(right_orig
))) {
1091 left_true
= remove_range(left_true
, min
, sub_one(rl_min(right_orig
)));
1093 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
1094 left_false
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
1096 if (!sval_is_max(rl_max(left_orig
)))
1097 right_true
= remove_range(right_true
, add_one(rl_max(left_orig
)), max
);
1098 if (!sval_is_min(rl_min(left_orig
)))
1099 right_true
= remove_range(right_true
, min
, sub_one(rl_min(left_orig
)));
1100 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
1101 right_false
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
1103 case SPECIAL_UNSIGNED_GTE
:
1105 if (!sval_is_min(rl_min(right_orig
)))
1106 left_true
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
1107 left_false
= remove_range(left_orig
, rl_max(right_orig
), max
);
1109 if (!sval_is_max(rl_max(left_orig
)))
1110 right_true
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
1111 right_false
= remove_range(right_orig
, min
, rl_min(left_orig
));
1113 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
1114 right_false
= remove_range(right_false
, rl_min(left_orig
), rl_min(left_orig
));
1115 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
1116 left_false
= remove_range(left_false
, rl_max(left_orig
), rl_max(left_orig
));
1119 case SPECIAL_UNSIGNED_GT
:
1120 left_true
= remove_range(left_orig
, min
, rl_min(right_orig
));
1121 if (!sval_is_max(rl_max(right_orig
)))
1122 left_false
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
1124 right_true
= remove_range(right_orig
, rl_max(left_orig
), max
);
1125 if (!sval_is_min(rl_min(left_orig
)))
1126 right_false
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
1128 case SPECIAL_NOTEQUAL
:
1129 if (!sval_is_max(rl_max(right_orig
)))
1130 left_false
= remove_range(left_false
, add_one(rl_max(right_orig
)), max
);
1131 if (!sval_is_min(rl_min(right_orig
)))
1132 left_false
= remove_range(left_false
, min
, sub_one(rl_min(right_orig
)));
1133 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
1134 left_true
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
1136 if (!sval_is_max(rl_max(left_orig
)))
1137 right_false
= remove_range(right_false
, add_one(rl_max(left_orig
)), max
);
1138 if (!sval_is_min(rl_min(left_orig
)))
1139 right_false
= remove_range(right_false
, min
, sub_one(rl_min(left_orig
)));
1140 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
1141 right_true
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
1147 left_true
= rl_truncate_cast(get_type(left
), left_true
);
1148 left_false
= rl_truncate_cast(get_type(left
), left_false
);
1149 right_true
= rl_truncate_cast(get_type(right
), right_true
);
1150 right_false
= rl_truncate_cast(get_type(right
), right_false
);
1152 left_true_state
= alloc_estate_rl(left_true
);
1153 left_false_state
= alloc_estate_rl(left_false
);
1154 right_true_state
= alloc_estate_rl(right_true
);
1155 right_false_state
= alloc_estate_rl(right_false
);
1159 case SPECIAL_UNSIGNED_LT
:
1160 case SPECIAL_UNSIGNED_LTE
:
1162 if (get_hard_max(right
, &dummy
))
1163 estate_set_hard_max(left_true_state
);
1164 if (get_hard_max(left
, &dummy
))
1165 estate_set_hard_max(right_false_state
);
1168 case SPECIAL_UNSIGNED_GT
:
1169 case SPECIAL_UNSIGNED_GTE
:
1171 if (get_hard_max(left
, &dummy
))
1172 estate_set_hard_max(right_true_state
);
1173 if (get_hard_max(right
, &dummy
))
1174 estate_set_hard_max(left_false_state
);
1180 case SPECIAL_UNSIGNED_LT
:
1181 case SPECIAL_UNSIGNED_LTE
:
1183 if (get_hard_max(right
, &hard_max
)) {
1184 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1186 estate_set_fuzzy_max(left_true_state
, hard_max
);
1188 if (get_implied_value(right
, &hard_max
)) {
1189 if (op
== SPECIAL_UNSIGNED_LTE
||
1192 estate_set_fuzzy_max(left_false_state
, hard_max
);
1194 if (get_hard_max(left
, &hard_max
)) {
1195 if (op
== SPECIAL_UNSIGNED_LTE
||
1198 estate_set_fuzzy_max(right_false_state
, hard_max
);
1200 if (get_implied_value(left
, &hard_max
)) {
1201 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1203 estate_set_fuzzy_max(right_true_state
, hard_max
);
1207 case SPECIAL_UNSIGNED_GT
:
1208 case SPECIAL_UNSIGNED_GTE
:
1210 if (get_hard_max(left
, &hard_max
)) {
1211 if (op
== '>' || op
== SPECIAL_UNSIGNED_GT
)
1213 estate_set_fuzzy_max(right_true_state
, hard_max
);
1215 if (get_implied_value(left
, &hard_max
)) {
1216 if (op
== SPECIAL_UNSIGNED_GTE
||
1219 estate_set_fuzzy_max(right_false_state
, hard_max
);
1221 if (get_hard_max(right
, &hard_max
)) {
1222 if (op
== SPECIAL_UNSIGNED_LTE
||
1225 estate_set_fuzzy_max(left_false_state
, hard_max
);
1227 if (get_implied_value(right
, &hard_max
)) {
1229 op
== SPECIAL_UNSIGNED_GT
)
1231 estate_set_fuzzy_max(left_true_state
, hard_max
);
1235 if (get_hard_max(left
, &hard_max
))
1236 estate_set_fuzzy_max(right_true_state
, hard_max
);
1237 if (get_hard_max(right
, &hard_max
))
1238 estate_set_fuzzy_max(left_true_state
, hard_max
);
1242 if (get_hard_max(left
, &hard_max
)) {
1243 estate_set_hard_max(left_true_state
);
1244 estate_set_hard_max(left_false_state
);
1246 if (get_hard_max(right
, &hard_max
)) {
1247 estate_set_hard_max(right_true_state
);
1248 estate_set_hard_max(right_false_state
);
1251 if (left_postop
== SPECIAL_INCREMENT
) {
1252 left_true_state
= increment_state(left_true_state
);
1253 left_false_state
= increment_state(left_false_state
);
1255 if (left_postop
== SPECIAL_DECREMENT
) {
1256 left_true_state
= decrement_state(left_true_state
);
1257 left_false_state
= decrement_state(left_false_state
);
1259 if (right_postop
== SPECIAL_INCREMENT
) {
1260 right_true_state
= increment_state(right_true_state
);
1261 right_false_state
= increment_state(right_false_state
);
1263 if (right_postop
== SPECIAL_DECREMENT
) {
1264 right_true_state
= decrement_state(right_true_state
);
1265 right_false_state
= decrement_state(right_false_state
);
1268 if (estate_rl(left_true_state
) && estates_equiv(left_true_state
, left_false_state
)) {
1269 left_true_state
= NULL
;
1270 left_false_state
= NULL
;
1273 if (estate_rl(right_true_state
) && estates_equiv(right_true_state
, right_false_state
)) {
1274 right_true_state
= NULL
;
1275 right_false_state
= NULL
;
1278 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
1279 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
1282 static int is_simple_math(struct expression
*expr
)
1286 if (expr
->type
!= EXPR_BINOP
)
1297 static void move_known_values(struct expression
**left_p
, struct expression
**right_p
)
1299 struct expression
*left
= *left_p
;
1300 struct expression
*right
= *right_p
;
1303 if (get_implied_value(left
, &sval
)) {
1304 if (!is_simple_math(right
))
1306 if (right
->op
== '*') {
1309 if (!get_value(right
->right
, &divisor
))
1311 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1313 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1314 *right_p
= right
->left
;
1317 if (right
->op
== '+' && get_value(right
->left
, &sval
)) {
1318 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->left
);
1319 *right_p
= right
->right
;
1322 if (get_value(right
->right
, &sval
)) {
1323 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1324 *right_p
= right
->left
;
1329 if (get_implied_value(right
, &sval
)) {
1330 if (!is_simple_math(left
))
1332 if (left
->op
== '*') {
1335 if (!get_value(left
->right
, &divisor
))
1337 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1339 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1340 *left_p
= left
->left
;
1343 if (left
->op
== '+' && get_value(left
->left
, &sval
)) {
1344 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->left
);
1345 *left_p
= left
->right
;
1349 if (get_value(left
->right
, &sval
)) {
1350 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1351 *left_p
= left
->left
;
1358 static int match_func_comparison(struct expression
*expr
)
1360 struct expression
*left
= strip_expr(expr
->left
);
1361 struct expression
*right
= strip_expr(expr
->right
);
1365 * fixme: think about this harder. We should always be trying to limit
1366 * the non-call side as well. If we can't determine the limitter does
1367 * that mean we aren't querying the database and are missing important
1371 if (left
->type
== EXPR_CALL
) {
1372 if (get_implied_value(left
, &sval
)) {
1373 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1376 function_comparison(left
, expr
->op
, right
);
1380 if (right
->type
== EXPR_CALL
) {
1381 if (get_implied_value(right
, &sval
)) {
1382 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1385 function_comparison(left
, expr
->op
, right
);
1392 static void match_comparison(struct expression
*expr
)
1394 struct expression
*left_orig
= strip_expr(expr
->left
);
1395 struct expression
*right_orig
= strip_expr(expr
->right
);
1396 struct expression
*left
, *right
;
1397 struct expression
*prev
;
1398 struct symbol
*type
;
1400 if (match_func_comparison(expr
))
1403 type
= get_type(expr
);
1405 type
= &llong_ctype
;
1409 move_known_values(&left
, &right
);
1410 handle_comparison(type
, left
, expr
->op
, right
);
1412 prev
= get_assigned_expr(left_orig
);
1413 if (is_simple_math(prev
) && has_variable(prev
, left_orig
) == 0) {
1416 move_known_values(&left
, &right
);
1417 handle_comparison(type
, left
, expr
->op
, right
);
1420 prev
= get_assigned_expr(right_orig
);
1421 if (is_simple_math(prev
) && has_variable(prev
, right_orig
) == 0) {
1424 move_known_values(&left
, &right
);
1425 handle_comparison(type
, left
, expr
->op
, right
);
1429 static void handle_AND_condition(struct expression
*expr
)
1431 struct range_list
*rl
= NULL
;
1434 if (get_implied_value(expr
->left
, &known
) && known
.value
> 0) {
1436 get_absolute_rl(expr
->right
, &rl
);
1437 rl
= remove_range(rl
, sval_type_val(known
.type
, 0), known
);
1438 set_extra_expr_true_false(expr
->right
, alloc_estate_rl(rl
), NULL
);
1442 if (get_implied_value(expr
->right
, &known
) && known
.value
> 0) {
1444 get_absolute_rl(expr
->left
, &rl
);
1445 rl
= remove_range(rl
, sval_type_val(known
.type
, 0), known
);
1446 set_extra_expr_true_false(expr
->left
, alloc_estate_rl(rl
), NULL
);
1451 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1452 void __extra_match_condition(struct expression
*expr
)
1454 struct smatch_state
*pre_state
;
1455 struct smatch_state
*true_state
;
1456 struct smatch_state
*false_state
;
1458 expr
= strip_expr(expr
);
1459 switch (expr
->type
) {
1461 function_comparison(expr
, SPECIAL_NOTEQUAL
, zero_expr());
1468 zero
= sval_blank(expr
);
1471 pre_state
= get_extra_state(expr
);
1472 true_state
= estate_filter_sval(pre_state
, zero
);
1473 if (possibly_true(expr
, SPECIAL_EQUAL
, zero_expr()))
1474 false_state
= alloc_estate_sval(zero
);
1476 false_state
= alloc_estate_empty();
1477 set_extra_expr_true_false(expr
, true_state
, false_state
);
1481 match_comparison(expr
);
1483 case EXPR_ASSIGNMENT
:
1484 __extra_match_condition(expr
->left
);
1487 if (expr
->op
== '&')
1488 handle_AND_condition(expr
);
1493 static void assume_indexes_are_valid(struct expression
*expr
)
1495 struct expression
*array_expr
;
1497 struct expression
*offset
;
1498 struct symbol
*offset_type
;
1499 struct range_list
*rl_before
;
1500 struct range_list
*rl_after
;
1501 struct range_list
*filter
= NULL
;
1504 expr
= strip_expr(expr
);
1505 if (!is_array(expr
))
1508 offset
= get_array_offset(expr
);
1509 offset_type
= get_type(offset
);
1510 if (offset_type
&& type_signed(offset_type
)) {
1511 filter
= alloc_rl(sval_type_min(offset_type
),
1512 sval_type_val(offset_type
, -1));
1515 array_expr
= get_array_base(expr
);
1516 array_size
= get_real_array_size(array_expr
);
1517 if (array_size
> 1) {
1518 size
= sval_type_val(offset_type
, array_size
);
1519 add_range(&filter
, size
, sval_type_max(offset_type
));
1524 get_absolute_rl(offset
, &rl_before
);
1525 rl_after
= rl_filter(rl_before
, filter
);
1526 if (rl_equiv(rl_before
, rl_after
))
1528 set_extra_expr_nomod(offset
, alloc_estate_rl(rl_after
));
1531 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1532 int implied_not_equal(struct expression
*expr
, long long val
)
1534 return !possibly_false(expr
, SPECIAL_NOTEQUAL
, value_expr(val
));
1537 int implied_not_equal_name_sym(char *name
, struct symbol
*sym
, long long val
)
1539 struct smatch_state
*estate
;
1541 estate
= get_state(SMATCH_EXTRA
, name
, sym
);
1544 if (!rl_has_sval(estate_rl(estate
), sval_type_val(estate_type(estate
), 0)))
1549 int parent_is_null_var_sym(const char *name
, struct symbol
*sym
)
1554 struct smatch_state
*state
;
1556 strncpy(buf
, name
, sizeof(buf
) - 1);
1557 buf
[sizeof(buf
) - 1] = '\0';
1560 while (*start
== '*') {
1562 state
= get_state(SMATCH_EXTRA
, start
, sym
);
1565 if (!estate_rl(state
))
1567 if (estate_min(state
).value
== 0 &&
1568 estate_max(state
).value
== 0)
1573 while (*start
== '&')
1576 while ((end
= strrchr(start
, '-'))) {
1578 state
= get_state(SMATCH_EXTRA
, start
, sym
);
1581 if (estate_min(state
).value
== 0 &&
1582 estate_max(state
).value
== 0)
1588 int parent_is_null(struct expression
*expr
)
1594 expr
= strip_expr(expr
);
1595 var
= expr_to_var_sym(expr
, &sym
);
1598 ret
= parent_is_null_var_sym(var
, sym
);
1604 static int param_used_callback(void *found
, int argc
, char **argv
, char **azColName
)
1610 static int filter_unused_kzalloc_info(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
1615 /* for function pointers assume everything is used */
1616 if (call
->fn
->type
!= EXPR_SYMBOL
)
1620 * kzalloc() information is treated as special because so there is just
1621 * a lot of stuff initialized to zero and it makes building the database
1622 * take hours and hours.
1624 * In theory, we should just remove this line and not pass any unused
1625 * information, but I'm not sure enough that this code works so I want
1626 * to hold off on that for now.
1628 if (!estate_get_single_value(sm
->state
, &sval
) || sval
.value
!= 0)
1631 run_sql(¶m_used_callback
, &found
,
1632 "select * from call_implies where %s and type = %d and parameter = %d and key = '%s';",
1633 get_static_filter(call
->fn
->symbol
), PARAM_USED
, param
, printed_name
);
1637 /* If the database is not built yet, then assume everything is used */
1638 run_sql(¶m_used_callback
, &found
,
1639 "select * from call_implies where %s and type = %d;",
1640 get_static_filter(call
->fn
->symbol
), PARAM_USED
);
1647 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
1649 if (estate_is_whole(sm
->state
))
1651 if (filter_unused_kzalloc_info(call
, param
, printed_name
, sm
))
1653 sql_insert_caller_info(call
, PARAM_VALUE
, param
, printed_name
, sm
->state
->name
);
1654 if (estate_has_fuzzy_max(sm
->state
))
1655 sql_insert_caller_info(call
, FUZZY_MAX
, param
, printed_name
,
1656 sval_to_str(estate_get_fuzzy_max(sm
->state
)));
1659 static void db_limited_before(void)
1661 unmatched_stree
= clone_stree(__get_cur_stree());
1664 static void db_limited_after(void)
1666 free_stree(&unmatched_stree
);
1669 static void db_param_limit_filter(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
1671 struct expression
*arg
;
1674 struct sm_state
*sm
;
1675 struct symbol
*compare_type
, *var_type
;
1676 struct range_list
*rl
;
1677 struct range_list
*limit
;
1678 struct range_list
*new;
1680 while (expr
->type
== EXPR_ASSIGNMENT
)
1681 expr
= strip_expr(expr
->right
);
1682 if (expr
->type
!= EXPR_CALL
)
1685 arg
= get_argument_from_call_expr(expr
->args
, param
);
1689 name
= get_variable_from_key(arg
, key
, &sym
);
1693 if (strcmp(key
, "$") == 0)
1694 compare_type
= get_arg_type(expr
->fn
, param
);
1696 compare_type
= get_member_type_from_key(arg
, key
);
1698 sm
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
1700 rl
= estate_rl(sm
->state
);
1702 rl
= alloc_whole_rl(compare_type
);
1704 call_results_to_rl(expr
, compare_type
, value
, &limit
);
1705 new = rl_intersection(rl
, limit
);
1707 var_type
= get_member_type_from_key(arg
, key
);
1708 new = cast_rl(var_type
, new);
1710 /* We want to preserve the implications here */
1711 if (sm
&& rl_equiv(estate_rl(sm
->state
), new))
1714 if (op
== PARAM_LIMIT
)
1715 set_extra_nomod(name
, sym
, alloc_estate_rl(new));
1717 set_extra_mod(name
, sym
, alloc_estate_rl(new));
1724 static void db_param_limit(struct expression
*expr
, int param
, char *key
, char *value
)
1726 db_param_limit_filter(expr
, param
, key
, value
, PARAM_LIMIT
);
1729 static void db_param_filter(struct expression
*expr
, int param
, char *key
, char *value
)
1731 db_param_limit_filter(expr
, param
, key
, value
, PARAM_FILTER
);
1734 static void db_param_add_set(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
1736 struct expression
*arg
;
1739 struct symbol
*type
;
1740 struct smatch_state
*state
;
1741 struct range_list
*new = NULL
;
1742 struct range_list
*added
= NULL
;
1744 while (expr
->type
== EXPR_ASSIGNMENT
)
1745 expr
= strip_expr(expr
->right
);
1746 if (expr
->type
!= EXPR_CALL
)
1749 arg
= get_argument_from_call_expr(expr
->args
, param
);
1752 type
= get_member_type_from_key(arg
, key
);
1753 name
= get_variable_from_key(arg
, key
, &sym
);
1757 state
= get_state(SMATCH_EXTRA
, name
, sym
);
1759 new = estate_rl(state
);
1761 call_results_to_rl(expr
, type
, value
, &added
);
1763 if (op
== PARAM_SET
)
1766 new = rl_union(new, added
);
1768 set_extra_mod(name
, sym
, alloc_estate_rl(new));
1773 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
1775 db_param_add_set(expr
, param
, key
, value
, PARAM_ADD
);
1778 static void db_param_set(struct expression
*expr
, int param
, char *key
, char *value
)
1780 db_param_add_set(expr
, param
, key
, value
, PARAM_SET
);
1783 static void db_returned_member_info(struct expression
*expr
, int param
, char *key
, char *value
)
1785 struct symbol
*type
;
1788 struct range_list
*rl
;
1790 if (expr
->type
!= EXPR_ASSIGNMENT
)
1793 name
= return_state_to_var_sym(expr
, param
, key
, &sym
);
1796 type
= get_member_type_from_key(expr
->left
, key
);
1799 call_results_to_rl(expr
->right
, type
, value
, &rl
);
1800 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
1806 static char *in_terms_of_param_math(struct expression
*expr
, char *printed_name
)
1813 var
= expr_to_var_sym(expr
, &sym
);
1816 snprintf(buf
, sizeof(buf
), "%s%s", var
, printed_name
+ 2);
1818 ret
= get_value_in_terms_of_parameter_math_var_sym(buf
, sym
);
1825 static void returned_member_callback(int return_id
, char *return_ranges
, struct expression
*expr
, char *printed_name
, struct smatch_state
*state
)
1831 /* these are handled in smatch_param_filter/set/limit.c */
1832 if (printed_name
[0] != '*' &&
1833 !strchr(printed_name
, '.') && !strchr(printed_name
, '-'))
1836 if (estate_get_single_value(state
, &sval
)) {
1837 sql_insert_return_states(return_id
, return_ranges
, RETURN_VALUE
, -1,
1838 printed_name
, sval_to_str(sval
));
1842 math_str
= in_terms_of_param_math(expr
, printed_name
);
1844 snprintf(buf
, sizeof(buf
), "%s[%s]", state
->name
, math_str
);
1845 sql_insert_return_states(return_id
, return_ranges
, RETURN_VALUE
, -1,
1850 if (estate_is_whole(state
))
1853 sql_insert_return_states(return_id
, return_ranges
, RETURN_VALUE
, -1,
1854 printed_name
, state
->name
);
1857 static void match_call_info(struct expression
*expr
)
1859 struct smatch_state
*state
;
1860 struct range_list
*rl
= NULL
;
1861 struct expression
*arg
;
1862 struct symbol
*type
;
1865 FOR_EACH_PTR(expr
->args
, arg
) {
1866 type
= get_arg_type(expr
->fn
, i
);
1868 if (get_implied_rl(arg
, &rl
))
1869 rl
= cast_rl(type
, rl
);
1871 rl
= cast_rl(type
, alloc_whole_rl(get_type(arg
)));
1873 if (!is_whole_rl(rl
))
1874 sql_insert_caller_info(expr
, PARAM_VALUE
, i
, "$", show_rl(rl
));
1875 state
= get_state_expr(SMATCH_EXTRA
, arg
);
1876 if (estate_has_fuzzy_max(state
)) {
1877 sql_insert_caller_info(expr
, FUZZY_MAX
, i
, "$",
1878 sval_to_str(estate_get_fuzzy_max(state
)));
1881 } END_FOR_EACH_PTR(arg
);
1884 static void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1886 struct range_list
*rl
= NULL
;
1887 struct smatch_state
*state
;
1888 struct symbol
*type
;
1891 if (strcmp(key
, "*$") == 0)
1892 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
1893 else if (strncmp(key
, "$", 1) == 0)
1894 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
1898 type
= get_member_type_from_key(symbol_expression(sym
), key
);
1899 str_to_rl(type
, value
, &rl
);
1900 state
= alloc_estate_rl(rl
);
1901 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
1904 static void set_param_hard_max(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1906 struct range_list
*rl
= NULL
;
1907 struct smatch_state
*state
;
1908 struct symbol
*type
;
1912 if (strcmp(key
, "*$") == 0)
1913 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
1914 else if (strncmp(key
, "$", 1) == 0)
1915 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
1919 state
= get_state(SMATCH_EXTRA
, fullname
, sym
);
1922 type
= get_member_type_from_key(symbol_expression(sym
), key
);
1923 str_to_rl(type
, value
, &rl
);
1924 if (!rl_to_sval(rl
, &max
))
1926 estate_set_fuzzy_max(state
, max
);
1929 struct smatch_state
*get_extra_state(struct expression
*expr
)
1933 struct smatch_state
*ret
= NULL
;
1934 struct range_list
*rl
;
1936 if (is_pointer(expr
) && get_address_rl(expr
, &rl
))
1937 return alloc_estate_rl(rl
);
1939 name
= expr_to_known_chunk_sym(expr
, &sym
);
1943 ret
= get_state(SMATCH_EXTRA
, name
, sym
);
1949 void register_smatch_extra(int id
)
1953 add_merge_hook(my_id
, &merge_estates
);
1954 add_unmatched_state_hook(my_id
, &unmatched_state
);
1955 select_caller_info_hook(set_param_value
, PARAM_VALUE
);
1956 select_caller_info_hook(set_param_hard_max
, FUZZY_MAX
);
1957 select_return_states_hook(RETURN_VALUE
, &db_returned_member_info
);
1958 select_return_states_before(&db_limited_before
);
1959 select_return_states_hook(PARAM_LIMIT
, &db_param_limit
);
1960 select_return_states_hook(PARAM_FILTER
, &db_param_filter
);
1961 select_return_states_hook(PARAM_ADD
, &db_param_add
);
1962 select_return_states_hook(PARAM_SET
, &db_param_set
);
1963 select_return_states_after(&db_limited_after
);
1966 static void match_link_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
1968 struct var_sym_list
*links
;
1969 struct var_sym
*tmp
;
1970 struct smatch_state
*state
;
1972 links
= sm
->state
->data
;
1974 FOR_EACH_PTR(links
, tmp
) {
1975 state
= get_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
);
1978 set_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
, alloc_estate_whole(estate_type(state
)));
1979 } END_FOR_EACH_PTR(tmp
);
1980 set_state(link_id
, sm
->name
, sm
->sym
, &undefined
);
1983 void register_smatch_extra_links(int id
)
1988 void register_smatch_extra_late(int id
)
1990 add_merge_hook(link_id
, &merge_link_states
);
1991 add_modification_hook(link_id
, &match_link_modify
);
1992 add_hook(&match_dereferences
, DEREF_HOOK
);
1993 add_hook(&match_pointer_as_array
, OP_HOOK
);
1994 select_call_implies_hook(DEREFERENCE
, &set_param_dereferenced
);
1995 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
1996 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
1997 add_hook(&match_assign
, GLOBAL_ASSIGNMENT_HOOK
);
1998 add_hook(&unop_expr
, OP_HOOK
);
1999 add_hook(&asm_expr
, ASM_HOOK
);
2001 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2002 add_member_info_callback(my_id
, struct_member_callback
);
2003 add_returned_member_callback(my_id
, returned_member_callback
);
2005 add_hook(&assume_indexes_are_valid
, OP_HOOK
);