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"
36 struct string_list
*__ignored_macros
= NULL
;
37 static int in_warn_on_macro(void)
39 struct statement
*stmt
;
43 stmt
= get_current_statement();
46 macro
= get_macro_name(stmt
->pos
);
50 FOR_EACH_PTR(__ignored_macros
, tmp
) {
51 if (!strcmp(tmp
, macro
))
53 } END_FOR_EACH_PTR(tmp
);
57 typedef void (mod_hook
)(const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
58 DECLARE_PTR_LIST(void_fn_list
, mod_hook
*);
59 static struct void_fn_list
*extra_mod_hooks
;
60 void add_extra_mod_hook(mod_hook
*fn
)
62 mod_hook
**p
= malloc(sizeof(mod_hook
*));
64 add_ptr_list(&extra_mod_hooks
, p
);
67 void call_extra_mod_hooks(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
71 FOR_EACH_PTR(extra_mod_hooks
, fn
) {
72 (*fn
)(name
, sym
, state
);
73 } END_FOR_EACH_PTR(fn
);
76 struct sm_state
*set_extra_mod_helper(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
78 remove_from_equiv(name
, sym
);
79 call_extra_mod_hooks(name
, sym
, state
);
80 return set_state(SMATCH_EXTRA
, name
, sym
, state
);
83 static char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
85 struct expression
*assigned
;
96 skip
= strlen(sym
->ident
->name
);
97 if (name
[skip
] != '-' || name
[skip
+ 1] != '>')
101 assigned
= get_assigned_expr_name_sym(sym
->ident
->name
, sym
);
104 if (assigned
->type
!= EXPR_PREOP
|| assigned
->op
!= '&')
107 orig_name
= expr_to_var_sym(assigned
, new_sym
);
108 if (!orig_name
|| !*new_sym
)
111 snprintf(buf
, sizeof(buf
), "%s.%s", orig_name
+ 1, name
+ skip
);
112 ret
= alloc_string(buf
);
114 free_string(orig_name
);
118 struct sm_state
*set_extra_mod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
121 struct symbol
*new_sym
;
124 if (in_warn_on_macro())
126 sm
= set_extra_mod_helper(name
, sym
, state
);
127 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
128 if (new_name
&& new_sym
)
129 set_extra_mod_helper(new_name
, new_sym
, state
);
130 free_string(new_name
);
134 struct sm_state
*set_extra_expr_mod(struct expression
*expr
, struct smatch_state
*state
)
138 struct sm_state
*ret
= NULL
;
140 expr
= strip_expr(expr
);
141 name
= expr_to_var_sym(expr
, &sym
);
144 ret
= set_extra_mod(name
, sym
, state
);
150 void set_extra_nomod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
153 struct symbol
*new_sym
;
154 struct relation
*rel
;
155 struct smatch_state
*orig_state
;
157 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
159 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
160 if (new_name
&& new_sym
)
161 set_state(SMATCH_EXTRA
, new_name
, new_sym
, state
);
162 free_string(new_name
);
164 if (!estate_related(orig_state
)) {
165 set_state(SMATCH_EXTRA
, name
, sym
, state
);
169 set_related(state
, estate_related(orig_state
));
170 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
171 if (option_debug_related
)
172 sm_msg("%s updating related %s to %s", name
, rel
->name
, state
->name
);
173 set_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
, state
);
174 } END_FOR_EACH_PTR(rel
);
178 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
180 void set_extra_expr_nomod(struct expression
*expr
, struct smatch_state
*state
)
185 name
= expr_to_var_sym(expr
, &sym
);
188 set_extra_nomod(name
, sym
, state
);
194 static void set_extra_true_false(const char *name
, struct symbol
*sym
,
195 struct smatch_state
*true_state
,
196 struct smatch_state
*false_state
)
199 struct symbol
*new_sym
;
200 struct relation
*rel
;
201 struct smatch_state
*orig_state
;
203 if (in_warn_on_macro())
206 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
207 if (new_name
&& new_sym
)
208 set_true_false_states(SMATCH_EXTRA
, new_name
, new_sym
, true_state
, false_state
);
209 free_string(new_name
);
211 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
213 if (!estate_related(orig_state
)) {
214 set_true_false_states(SMATCH_EXTRA
, name
, sym
, true_state
, false_state
);
219 set_related(true_state
, estate_related(orig_state
));
221 set_related(false_state
, estate_related(orig_state
));
223 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
224 set_true_false_states(SMATCH_EXTRA
, rel
->name
, rel
->sym
,
225 true_state
, false_state
);
226 } END_FOR_EACH_PTR(rel
);
229 static void set_extra_expr_true_false(struct expression
*expr
,
230 struct smatch_state
*true_state
,
231 struct smatch_state
*false_state
)
236 expr
= strip_expr(expr
);
237 name
= expr_to_var_sym(expr
, &sym
);
240 set_extra_true_false(name
, sym
, true_state
, false_state
);
245 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
247 struct expression
*iter_var
;
248 struct expression
*condition
;
250 struct smatch_state
*estate
;
253 condition
= strip_expr(loop
->iterator_pre_condition
);
256 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
258 if (condition
->op
!= SPECIAL_DECREMENT
)
261 iter_var
= condition
->unop
;
262 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
265 if (sval_cmp_val(estate_min(sm
->state
), 0) < 0)
267 start
= estate_max(sm
->state
);
268 if (sval_cmp_val(start
, 0) <= 0)
270 if (!sval_is_max(start
))
273 if (condition
->type
== EXPR_PREOP
) {
274 estate
= alloc_estate_range(sval_type_val(start
.type
, 1), start
);
275 if (estate_has_hard_max(sm
->state
))
276 estate_set_hard_max(estate
);
277 estate_copy_fuzzy_max(estate
, sm
->state
);
278 set_extra_expr_mod(iter_var
, estate
);
280 if (condition
->type
== EXPR_POSTOP
) {
281 estate
= alloc_estate_range(sval_type_val(start
.type
, 0), start
);
282 if (estate_has_hard_max(sm
->state
))
283 estate_set_hard_max(estate
);
284 estate_copy_fuzzy_max(estate
, sm
->state
);
285 set_extra_expr_mod(iter_var
, estate
);
287 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
290 static struct sm_state
*handle_canonical_for_inc(struct expression
*iter_expr
,
291 struct expression
*condition
)
293 struct expression
*iter_var
;
295 struct smatch_state
*estate
;
296 sval_t start
, end
, max
;
298 iter_var
= iter_expr
->unop
;
299 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
302 if (!estate_get_single_value(sm
->state
, &start
))
304 if (get_implied_max(condition
->right
, &end
))
305 end
= sval_cast(get_type(iter_var
), end
);
307 end
= sval_type_max(get_type(iter_var
));
309 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
312 switch (condition
->op
) {
313 case SPECIAL_UNSIGNED_LT
:
314 case SPECIAL_NOTEQUAL
:
316 if (!sval_is_min(end
))
319 case SPECIAL_UNSIGNED_LTE
:
325 if (sval_cmp(end
, start
) < 0)
327 estate
= alloc_estate_range(start
, end
);
328 if (get_hard_max(condition
->right
, &max
)) {
329 estate_set_hard_max(estate
);
330 if (condition
->op
== '<' ||
331 condition
->op
== SPECIAL_UNSIGNED_LT
||
332 condition
->op
== SPECIAL_NOTEQUAL
)
334 estate_set_fuzzy_max(estate
, max
);
336 set_extra_expr_mod(iter_var
, estate
);
337 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
340 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
341 struct expression
*condition
)
343 struct expression
*iter_var
;
345 struct smatch_state
*estate
;
348 iter_var
= iter_expr
->unop
;
349 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
352 if (!estate_get_single_value(sm
->state
, &start
))
354 if (!get_implied_min(condition
->right
, &end
))
355 end
= sval_type_min(get_type(iter_var
));
356 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
359 switch (condition
->op
) {
360 case SPECIAL_NOTEQUAL
:
362 if (!sval_is_min(end
) && !sval_is_max(end
))
370 if (sval_cmp(end
, start
) > 0)
372 estate
= alloc_estate_range(end
, start
);
373 estate_set_hard_max(estate
);
374 estate_set_fuzzy_max(estate
, estate_get_fuzzy_max(estate
));
375 set_extra_expr_mod(iter_var
, estate
);
376 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
379 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
381 struct expression
*iter_expr
;
382 struct expression
*condition
;
384 if (!loop
->iterator_post_statement
)
386 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
388 iter_expr
= loop
->iterator_post_statement
->expression
;
389 if (!loop
->iterator_pre_condition
)
391 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
393 condition
= loop
->iterator_pre_condition
;
395 if (iter_expr
->op
== SPECIAL_INCREMENT
)
396 return handle_canonical_for_inc(iter_expr
, condition
);
397 if (iter_expr
->op
== SPECIAL_DECREMENT
)
398 return handle_canonical_for_dec(iter_expr
, condition
);
402 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct stree
**stree
)
404 struct sm_state
*ret
;
406 __push_fake_cur_stree();
407 if (!loop
->iterator_post_statement
)
408 ret
= handle_canonical_while_count_down(loop
);
410 ret
= handle_canonical_for_loops(loop
);
411 *stree
= __pop_fake_cur_stree();
415 int __iterator_unchanged(struct sm_state
*sm
)
419 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
424 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
428 /* paranoid checking. prolly not needed */
429 condition
= strip_expr(condition
);
432 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
434 if (condition
->op
!= SPECIAL_DECREMENT
)
436 after_value
= estate_min(sm
->state
);
438 set_extra_mod(sm
->name
, sm
->sym
, alloc_estate_sval(after_value
));
441 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
442 struct statement
*iterator
,
443 struct expression
*condition
)
445 struct expression
*iter_expr
;
447 struct smatch_state
*state
;
450 while_count_down_after(sm
, condition
);
454 iter_expr
= iterator
->expression
;
456 if (condition
->type
!= EXPR_COMPARE
)
458 if (iter_expr
->op
== SPECIAL_INCREMENT
) {
459 limit
= sval_binop(estate_max(sm
->state
), '+',
460 sval_type_val(estate_type(sm
->state
), 1));
462 limit
= sval_binop(estate_min(sm
->state
), '-',
463 sval_type_val(estate_type(sm
->state
), 1));
465 if (!estate_has_hard_max(sm
->state
) && !__has_breaks()) {
466 if (iter_expr
->op
== SPECIAL_INCREMENT
)
467 state
= alloc_estate_range(estate_min(sm
->state
), limit
);
469 state
= alloc_estate_range(limit
, estate_max(sm
->state
));
471 state
= alloc_estate_sval(limit
);
473 if (!estate_has_hard_max(sm
->state
)) {
474 estate_clear_hard_max(state
);
476 if (estate_has_fuzzy_max(sm
->state
)) {
477 sval_t hmax
= estate_get_fuzzy_max(sm
->state
);
478 sval_t max
= estate_max(sm
->state
);
480 if (sval_cmp(hmax
, max
) != 0)
481 estate_clear_fuzzy_max(state
);
482 } else if (!estate_has_fuzzy_max(sm
->state
)) {
483 estate_clear_fuzzy_max(state
);
486 set_extra_mod(sm
->name
, sm
->sym
, state
);
489 static struct stree
*unmatched_stree
;
490 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
492 struct smatch_state
*state
;
494 if (unmatched_stree
) {
495 state
= get_state_stree(unmatched_stree
, SMATCH_EXTRA
, sm
->name
, sm
->sym
);
499 if (parent_is_gone_var_sym(sm
->name
, sm
->sym
))
500 return alloc_estate_empty();
501 return alloc_estate_whole(estate_type(sm
->state
));
504 static void clear_the_pointed_at(struct expression
*expr
)
509 struct sm_state
*tmp
;
511 name
= expr_to_var_sym(expr
, &sym
);
515 stree
= __get_cur_stree();
516 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
517 if (tmp
->name
[0] != '*')
521 if (strcmp(tmp
->name
+ 1, name
) != 0)
523 set_extra_mod(tmp
->name
, tmp
->sym
, alloc_estate_whole(estate_type(tmp
->state
)));
524 } END_FOR_EACH_SM(tmp
);
530 static void match_function_call(struct expression
*expr
)
532 struct expression
*arg
;
533 struct expression
*tmp
;
535 /* if we have the db this is handled in smatch_function_hooks.c */
538 if (inlinable(expr
->fn
))
541 FOR_EACH_PTR(expr
->args
, arg
) {
542 tmp
= strip_expr(arg
);
543 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
544 set_extra_expr_mod(tmp
->unop
, alloc_estate_whole(get_type(tmp
->unop
)));
546 clear_the_pointed_at(tmp
);
547 } END_FOR_EACH_PTR(arg
);
550 static int types_equiv_or_pointer(struct symbol
*one
, struct symbol
*two
)
554 if (one
->type
== SYM_PTR
&& two
->type
== SYM_PTR
)
556 return types_equiv(one
, two
);
559 static bool is_non_null_array(struct expression
*expr
)
567 type
= get_type(expr
);
568 if (!type
|| type
->type
!= SYM_ARRAY
)
570 if (expr
->type
== EXPR_SYMBOL
)
572 if (implied_not_equal(expr
, 0))
575 /* verify that it's not the first member of the struct */
576 if (expr
->type
!= EXPR_DEREF
|| !expr
->member
)
578 name
= expr_to_var_sym(expr
, &sym
);
582 type
= get_real_base_type(sym
);
583 if (!type
|| type
->type
!= SYM_PTR
)
585 type
= get_real_base_type(type
);
586 if (type
->type
!= SYM_STRUCT
)
590 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
594 if (strcmp(expr
->member
->name
, tmp
->ident
->name
) == 0) {
599 } END_FOR_EACH_PTR(tmp
);
604 static void match_vanilla_assign(struct expression
*left
, struct expression
*right
)
606 struct range_list
*rl
= NULL
;
607 struct symbol
*right_sym
;
608 struct symbol
*left_type
;
609 struct symbol
*right_type
;
610 char *right_name
= NULL
;
614 struct smatch_state
*state
;
619 name
= expr_to_var_sym(left
, &sym
);
623 left_type
= get_type(left
);
624 right_type
= get_type(right
);
626 right_name
= expr_to_var_sym(right
, &right_sym
);
628 if (!(right
->type
== EXPR_PREOP
&& right
->op
== '&') &&
629 right_name
&& right_sym
&&
630 types_equiv_or_pointer(left_type
, right_type
) &&
631 !has_symbol(right
, sym
)) {
632 set_equiv(left
, right
);
636 if (is_non_null_array(right
)) {
637 state
= alloc_estate_range(array_min_sval
, array_max_sval
);
638 } else if (get_implied_rl(right
, &rl
)) {
639 rl
= cast_rl(left_type
, rl
);
640 state
= alloc_estate_rl(rl
);
641 if (get_hard_max(right
, &max
)) {
642 estate_set_hard_max(state
);
643 estate_set_fuzzy_max(state
, max
);
646 rl
= alloc_whole_rl(right_type
);
647 rl
= cast_rl(left_type
, rl
);
648 state
= alloc_estate_rl(rl
);
650 set_extra_mod(name
, sym
, state
);
652 free_string(right_name
);
655 static int op_remove_assign(int op
)
658 case SPECIAL_ADD_ASSIGN
:
660 case SPECIAL_SUB_ASSIGN
:
662 case SPECIAL_MUL_ASSIGN
:
664 case SPECIAL_DIV_ASSIGN
:
666 case SPECIAL_MOD_ASSIGN
:
668 case SPECIAL_AND_ASSIGN
:
670 case SPECIAL_OR_ASSIGN
:
672 case SPECIAL_XOR_ASSIGN
:
674 case SPECIAL_SHL_ASSIGN
:
675 return SPECIAL_LEFTSHIFT
;
676 case SPECIAL_SHR_ASSIGN
:
677 return SPECIAL_RIGHTSHIFT
;
683 static void match_assign(struct expression
*expr
)
685 struct range_list
*rl
= NULL
;
686 struct expression
*left
;
687 struct expression
*right
;
688 struct expression
*binop_expr
;
689 struct symbol
*left_type
;
690 struct symbol
*right_type
;
693 sval_t left_min
, left_max
;
694 sval_t right_min
, right_max
;
695 sval_t res_min
, res_max
;
697 left
= strip_expr(expr
->left
);
699 right
= strip_expr(expr
->right
);
700 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
701 right
= strip_expr(right
->left
);
703 if (is_condition(expr
->right
))
704 return; /* handled in smatch_condition.c */
705 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
706 return; /* handled in smatch_function_hooks.c */
707 if (expr
->op
== '=') {
708 match_vanilla_assign(left
, right
);
712 name
= expr_to_var_sym(left
, &sym
);
716 left_type
= get_type(left
);
717 right_type
= get_type(right
);
719 res_min
= sval_type_min(left_type
);
720 res_max
= sval_type_max(left_type
);
723 case SPECIAL_ADD_ASSIGN
:
724 get_absolute_max(left
, &left_max
);
725 get_absolute_max(right
, &right_max
);
726 if (sval_binop_overflows(left_max
, '+', right_max
))
728 if (get_implied_min(left
, &left_min
) &&
729 !sval_is_negative_min(left_min
) &&
730 get_implied_min(right
, &right_min
) &&
731 !sval_is_negative_min(right_min
)) {
732 res_min
= sval_binop(left_min
, '+', right_min
);
733 res_min
= sval_cast(right_type
, res_min
);
735 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
737 res_max
= sval_binop(left_max
, '+', right_max
);
738 res_max
= sval_cast(right_type
, res_max
);
740 case SPECIAL_SUB_ASSIGN
:
741 if (get_implied_max(left
, &left_max
) &&
742 !sval_is_max(left_max
) &&
743 get_implied_min(right
, &right_min
) &&
744 !sval_is_min(right_min
)) {
745 res_max
= sval_binop(left_max
, '-', right_min
);
746 res_max
= sval_cast(right_type
, res_max
);
750 if (get_implied_min(left
, &left_min
) &&
751 !sval_is_min(left_min
) &&
752 get_implied_max(right
, &right_max
) &&
753 !sval_is_max(right_max
)) {
754 res_min
= sval_binop(left_min
, '-', right_max
);
755 res_min
= sval_cast(right_type
, res_min
);
758 case SPECIAL_AND_ASSIGN
:
759 case SPECIAL_MOD_ASSIGN
:
760 case SPECIAL_SHL_ASSIGN
:
761 case SPECIAL_SHR_ASSIGN
:
762 case SPECIAL_OR_ASSIGN
:
763 case SPECIAL_XOR_ASSIGN
:
764 case SPECIAL_MUL_ASSIGN
:
765 case SPECIAL_DIV_ASSIGN
:
766 binop_expr
= binop_expression(expr
->left
,
767 op_remove_assign(expr
->op
),
769 if (get_absolute_rl(binop_expr
, &rl
)) {
770 rl
= cast_rl(left_type
, rl
);
771 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
776 rl
= cast_rl(left_type
, alloc_rl(res_min
, res_max
));
777 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
782 static struct smatch_state
*increment_state(struct smatch_state
*state
)
784 sval_t min
= estate_min(state
);
785 sval_t max
= estate_max(state
);
787 if (!estate_rl(state
))
791 max
= sval_type_max(max
.type
);
793 if (!sval_is_min(min
) && !sval_is_max(min
))
795 if (!sval_is_min(max
) && !sval_is_max(max
))
797 return alloc_estate_range(min
, max
);
800 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
802 sval_t min
= estate_min(state
);
803 sval_t max
= estate_max(state
);
805 if (!estate_rl(state
))
809 min
= sval_type_min(min
.type
);
811 if (!sval_is_min(min
) && !sval_is_max(min
))
813 if (!sval_is_min(max
) && !sval_is_max(max
))
815 return alloc_estate_range(min
, max
);
818 static void unop_expr(struct expression
*expr
)
820 struct smatch_state
*state
;
822 if (expr
->smatch_flags
& Handled
)
826 case SPECIAL_INCREMENT
:
827 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
828 state
= increment_state(state
);
830 state
= alloc_estate_whole(get_type(expr
));
831 set_extra_expr_mod(expr
->unop
, state
);
833 case SPECIAL_DECREMENT
:
834 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
835 state
= decrement_state(state
);
837 state
= alloc_estate_whole(get_type(expr
));
838 set_extra_expr_mod(expr
->unop
, state
);
845 static void asm_expr(struct statement
*stmt
)
848 struct expression
*expr
;
852 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
854 case 0: /* identifier */
855 case 1: /* constraint */
858 case 2: /* expression */
860 type
= get_type(strip_expr(expr
));
861 set_extra_expr_mod(expr
, alloc_estate_whole(type
));
864 } END_FOR_EACH_PTR(expr
);
867 static void delete_state_tracker(struct tracker
*t
)
869 remove_from_equiv(t
->name
, t
->sym
);
870 delete_state(t
->owner
, t
->name
, t
->sym
);
873 static void scoped_state_extra(const char *name
, struct symbol
*sym
)
877 t
= alloc_tracker(SMATCH_EXTRA
, name
, sym
);
878 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
881 static void match_declarations(struct symbol
*sym
)
886 name
= sym
->ident
->name
;
887 if (!sym
->initializer
) {
888 set_state(SMATCH_EXTRA
, name
, sym
, alloc_estate_whole(get_real_base_type(sym
)));
889 scoped_state_extra(name
, sym
);
894 static void check_dereference(struct expression
*expr
)
896 if (outside_of_function())
898 set_extra_expr_nomod(expr
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
901 static void match_dereferences(struct expression
*expr
)
903 if (expr
->type
!= EXPR_PREOP
)
905 check_dereference(expr
->unop
);
908 static void match_pointer_as_array(struct expression
*expr
)
912 check_dereference(get_array_base(expr
));
915 static void set_param_dereferenced(struct expression
*arg
, char *key
, char *unused
)
920 name
= get_variable_from_key(arg
, key
, &sym
);
924 set_extra_nomod(name
, sym
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
930 static void match_function_def(struct symbol
*sym
)
934 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
937 set_state(my_id
, arg
->ident
->name
, arg
, alloc_estate_whole(get_real_base_type(arg
)));
938 } END_FOR_EACH_PTR(arg
);
941 static sval_t
add_one(sval_t sval
)
947 static sval_t
sub_one(sval_t sval
)
953 static void handle_comparison(struct symbol
*type
, struct expression
*left
, int op
, struct expression
*right
)
955 struct range_list
*left_orig
;
956 struct range_list
*left_true
;
957 struct range_list
*left_false
;
958 struct range_list
*right_orig
;
959 struct range_list
*right_true
;
960 struct range_list
*right_false
;
961 struct smatch_state
*left_true_state
;
962 struct smatch_state
*left_false_state
;
963 struct smatch_state
*right_true_state
;
964 struct smatch_state
*right_false_state
;
965 sval_t min
, max
, dummy
, hard_max
;
967 int right_postop
= 0;
969 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
970 if (left
->type
== EXPR_POSTOP
) {
971 left
->smatch_flags
|= Handled
;
972 left_postop
= left
->op
;
974 left
= strip_expr(left
->unop
);
976 while (left
->type
== EXPR_ASSIGNMENT
)
977 left
= strip_expr(left
->left
);
979 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
980 if (right
->type
== EXPR_POSTOP
) {
981 right
->smatch_flags
|= Handled
;
982 right_postop
= right
->op
;
984 right
= strip_expr(right
->unop
);
987 if (get_implied_rl(left
, &left_orig
)) {
988 left_orig
= cast_rl(type
, left_orig
);
990 min
= sval_type_min(get_type(left
));
991 max
= sval_type_max(get_type(left
));
992 left_orig
= cast_rl(type
, alloc_rl(min
, max
));
995 if (get_implied_rl(right
, &right_orig
)) {
996 right_orig
= cast_rl(type
, right_orig
);
998 min
= sval_type_min(get_type(right
));
999 max
= sval_type_max(get_type(right
));
1000 right_orig
= cast_rl(type
, alloc_rl(min
, max
));
1002 min
= sval_type_min(type
);
1003 max
= sval_type_max(type
);
1005 left_true
= clone_rl(left_orig
);
1006 left_false
= clone_rl(left_orig
);
1007 right_true
= clone_rl(right_orig
);
1008 right_false
= clone_rl(right_orig
);
1012 case SPECIAL_UNSIGNED_LT
:
1013 left_true
= remove_range(left_orig
, rl_max(right_orig
), max
);
1014 if (!sval_is_min(rl_min(right_orig
))) {
1015 left_false
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
1018 right_true
= remove_range(right_orig
, min
, rl_min(left_orig
));
1019 if (!sval_is_max(rl_max(left_orig
)))
1020 right_false
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
1022 case SPECIAL_UNSIGNED_LTE
:
1024 if (!sval_is_max(rl_max(right_orig
)))
1025 left_true
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
1026 left_false
= remove_range(left_orig
, min
, rl_min(right_orig
));
1028 if (!sval_is_min(rl_min(left_orig
)))
1029 right_true
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
1030 right_false
= remove_range(right_orig
, rl_max(left_orig
), max
);
1032 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
1033 left_false
= remove_range(left_false
, rl_min(left_orig
), rl_min(left_orig
));
1034 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
1035 right_false
= remove_range(right_false
, rl_max(left_orig
), rl_max(left_orig
));
1038 if (!sval_is_max(rl_max(right_orig
))) {
1039 left_true
= remove_range(left_true
, add_one(rl_max(right_orig
)), max
);
1041 if (!sval_is_min(rl_min(right_orig
))) {
1042 left_true
= remove_range(left_true
, min
, sub_one(rl_min(right_orig
)));
1044 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
1045 left_false
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
1047 if (!sval_is_max(rl_max(left_orig
)))
1048 right_true
= remove_range(right_true
, add_one(rl_max(left_orig
)), max
);
1049 if (!sval_is_min(rl_min(left_orig
)))
1050 right_true
= remove_range(right_true
, min
, sub_one(rl_min(left_orig
)));
1051 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
1052 right_false
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
1054 case SPECIAL_UNSIGNED_GTE
:
1056 if (!sval_is_min(rl_min(right_orig
)))
1057 left_true
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
1058 left_false
= remove_range(left_orig
, rl_max(right_orig
), max
);
1060 if (!sval_is_max(rl_max(left_orig
)))
1061 right_true
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
1062 right_false
= remove_range(right_orig
, min
, rl_min(left_orig
));
1064 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
1065 right_false
= remove_range(right_false
, rl_min(left_orig
), rl_min(left_orig
));
1066 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
1067 left_false
= remove_range(left_false
, rl_max(left_orig
), rl_max(left_orig
));
1070 case SPECIAL_UNSIGNED_GT
:
1071 left_true
= remove_range(left_orig
, min
, rl_min(right_orig
));
1072 if (!sval_is_max(rl_max(right_orig
)))
1073 left_false
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
1075 right_true
= remove_range(right_orig
, rl_max(left_orig
), max
);
1076 if (!sval_is_min(rl_min(left_orig
)))
1077 right_false
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
1079 case SPECIAL_NOTEQUAL
:
1080 if (!sval_is_max(rl_max(right_orig
)))
1081 left_false
= remove_range(left_false
, add_one(rl_max(right_orig
)), max
);
1082 if (!sval_is_min(rl_min(right_orig
)))
1083 left_false
= remove_range(left_false
, min
, sub_one(rl_min(right_orig
)));
1084 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
1085 left_true
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
1087 if (!sval_is_max(rl_max(left_orig
)))
1088 right_false
= remove_range(right_false
, add_one(rl_max(left_orig
)), max
);
1089 if (!sval_is_min(rl_min(left_orig
)))
1090 right_false
= remove_range(right_false
, min
, sub_one(rl_min(left_orig
)));
1091 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
1092 right_true
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
1098 left_true
= rl_truncate_cast(get_type(left
), left_true
);
1099 left_false
= rl_truncate_cast(get_type(left
), left_false
);
1100 right_true
= rl_truncate_cast(get_type(right
), right_true
);
1101 right_false
= rl_truncate_cast(get_type(right
), right_false
);
1103 left_true_state
= alloc_estate_rl(left_true
);
1104 left_false_state
= alloc_estate_rl(left_false
);
1105 right_true_state
= alloc_estate_rl(right_true
);
1106 right_false_state
= alloc_estate_rl(right_false
);
1110 case SPECIAL_UNSIGNED_LT
:
1111 case SPECIAL_UNSIGNED_LTE
:
1113 if (get_hard_max(right
, &dummy
))
1114 estate_set_hard_max(left_true_state
);
1115 if (get_hard_max(left
, &dummy
))
1116 estate_set_hard_max(right_false_state
);
1119 case SPECIAL_UNSIGNED_GT
:
1120 case SPECIAL_UNSIGNED_GTE
:
1122 if (get_hard_max(left
, &dummy
))
1123 estate_set_hard_max(right_true_state
);
1124 if (get_hard_max(right
, &dummy
))
1125 estate_set_hard_max(left_false_state
);
1131 case SPECIAL_UNSIGNED_LT
:
1132 case SPECIAL_UNSIGNED_LTE
:
1134 if (get_hard_max(right
, &hard_max
)) {
1135 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1137 estate_set_fuzzy_max(left_true_state
, hard_max
);
1139 if (get_implied_value(right
, &hard_max
)) {
1140 if (op
== SPECIAL_UNSIGNED_LTE
||
1143 estate_set_fuzzy_max(left_false_state
, hard_max
);
1145 if (get_hard_max(left
, &hard_max
)) {
1146 if (op
== SPECIAL_UNSIGNED_LTE
||
1149 estate_set_fuzzy_max(right_false_state
, hard_max
);
1151 if (get_implied_value(left
, &hard_max
)) {
1152 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1154 estate_set_fuzzy_max(right_true_state
, hard_max
);
1158 case SPECIAL_UNSIGNED_GT
:
1159 case SPECIAL_UNSIGNED_GTE
:
1161 if (get_hard_max(left
, &hard_max
)) {
1162 if (op
== '>' || op
== SPECIAL_UNSIGNED_GT
)
1164 estate_set_fuzzy_max(right_true_state
, hard_max
);
1166 if (get_implied_value(left
, &hard_max
)) {
1167 if (op
== SPECIAL_UNSIGNED_GTE
||
1170 estate_set_fuzzy_max(right_false_state
, hard_max
);
1172 if (get_hard_max(right
, &hard_max
)) {
1173 if (op
== SPECIAL_UNSIGNED_LTE
||
1176 estate_set_fuzzy_max(left_false_state
, hard_max
);
1178 if (get_implied_value(right
, &hard_max
)) {
1180 op
== SPECIAL_UNSIGNED_GT
)
1182 estate_set_fuzzy_max(left_true_state
, hard_max
);
1186 if (get_hard_max(left
, &hard_max
))
1187 estate_set_fuzzy_max(right_true_state
, hard_max
);
1188 if (get_hard_max(right
, &hard_max
))
1189 estate_set_fuzzy_max(left_true_state
, hard_max
);
1193 if (get_hard_max(left
, &hard_max
)) {
1194 estate_set_hard_max(left_true_state
);
1195 estate_set_hard_max(left_false_state
);
1197 if (get_hard_max(right
, &hard_max
)) {
1198 estate_set_hard_max(right_true_state
);
1199 estate_set_hard_max(right_false_state
);
1202 if (left_postop
== SPECIAL_INCREMENT
) {
1203 left_true_state
= increment_state(left_true_state
);
1204 left_false_state
= increment_state(left_false_state
);
1206 if (left_postop
== SPECIAL_DECREMENT
) {
1207 left_true_state
= decrement_state(left_true_state
);
1208 left_false_state
= decrement_state(left_false_state
);
1210 if (right_postop
== SPECIAL_INCREMENT
) {
1211 right_true_state
= increment_state(right_true_state
);
1212 right_false_state
= increment_state(right_false_state
);
1214 if (right_postop
== SPECIAL_DECREMENT
) {
1215 right_true_state
= decrement_state(right_true_state
);
1216 right_false_state
= decrement_state(right_false_state
);
1219 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
1220 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
1223 static int is_simple_math(struct expression
*expr
)
1227 if (expr
->type
!= EXPR_BINOP
)
1238 static void move_known_values(struct expression
**left_p
, struct expression
**right_p
)
1240 struct expression
*left
= *left_p
;
1241 struct expression
*right
= *right_p
;
1244 if (get_implied_value(left
, &sval
)) {
1245 if (!is_simple_math(right
))
1247 if (right
->op
== '*') {
1250 if (!get_value(right
->right
, &divisor
))
1252 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1254 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1255 *right_p
= right
->left
;
1258 if (right
->op
== '+' && get_value(right
->left
, &sval
)) {
1259 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->left
);
1260 *right_p
= right
->right
;
1263 if (get_value(right
->right
, &sval
)) {
1264 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1265 *right_p
= right
->left
;
1270 if (get_implied_value(right
, &sval
)) {
1271 if (!is_simple_math(left
))
1273 if (left
->op
== '*') {
1276 if (!get_value(left
->right
, &divisor
))
1278 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1280 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1281 *left_p
= left
->left
;
1284 if (left
->op
== '+' && get_value(left
->left
, &sval
)) {
1285 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->left
);
1286 *left_p
= left
->right
;
1290 if (get_value(left
->right
, &sval
)) {
1291 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1292 *left_p
= left
->left
;
1299 static int match_func_comparison(struct expression
*expr
)
1301 struct expression
*left
= strip_expr(expr
->left
);
1302 struct expression
*right
= strip_expr(expr
->right
);
1306 * fixme: think about this harder. We should always be trying to limit
1307 * the non-call side as well. If we can't determine the limitter does
1308 * that mean we aren't querying the database and are missing important
1312 if (left
->type
== EXPR_CALL
) {
1313 if (get_implied_value(left
, &sval
)) {
1314 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1317 function_comparison(left
, expr
->op
, right
);
1321 if (right
->type
== EXPR_CALL
) {
1322 if (get_implied_value(right
, &sval
)) {
1323 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1326 function_comparison(left
, expr
->op
, right
);
1333 static void match_comparison(struct expression
*expr
)
1335 struct expression
*left_orig
= strip_expr(expr
->left
);
1336 struct expression
*right_orig
= strip_expr(expr
->right
);
1337 struct expression
*left
, *right
;
1338 struct expression
*prev
;
1339 struct symbol
*type
;
1341 if (match_func_comparison(expr
))
1344 type
= get_type(expr
);
1346 type
= &llong_ctype
;
1350 move_known_values(&left
, &right
);
1351 handle_comparison(type
, left
, expr
->op
, right
);
1353 prev
= get_assigned_expr(left_orig
);
1354 if (is_simple_math(prev
) && has_variable(prev
, left_orig
) == 0) {
1357 move_known_values(&left
, &right
);
1358 handle_comparison(type
, left
, expr
->op
, right
);
1361 prev
= get_assigned_expr(right_orig
);
1362 if (is_simple_math(prev
) && has_variable(prev
, right_orig
) == 0) {
1365 move_known_values(&left
, &right
);
1366 handle_comparison(type
, left
, expr
->op
, right
);
1370 static void handle_AND_condition(struct expression
*expr
)
1372 struct range_list
*rl
= NULL
;
1375 if (get_implied_value(expr
->left
, &known
) && known
.value
> 0) {
1377 get_absolute_rl(expr
->right
, &rl
);
1378 rl
= remove_range(rl
, sval_type_val(known
.type
, 0), known
);
1379 set_extra_expr_true_false(expr
->right
, alloc_estate_rl(rl
), NULL
);
1383 if (get_implied_value(expr
->right
, &known
) && known
.value
> 0) {
1385 get_absolute_rl(expr
->left
, &rl
);
1386 rl
= remove_range(rl
, sval_type_val(known
.type
, 0), known
);
1387 set_extra_expr_true_false(expr
->left
, alloc_estate_rl(rl
), NULL
);
1392 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1393 void __extra_match_condition(struct expression
*expr
)
1397 struct smatch_state
*pre_state
;
1398 struct smatch_state
*true_state
;
1399 struct smatch_state
*false_state
;
1401 expr
= strip_expr(expr
);
1402 switch (expr
->type
) {
1404 function_comparison(expr
, SPECIAL_NOTEQUAL
, zero_expr());
1411 zero
= sval_blank(expr
);
1414 name
= expr_to_var_sym(expr
, &sym
);
1417 pre_state
= get_state(my_id
, name
, sym
);
1418 true_state
= estate_filter_sval(pre_state
, zero
);
1419 if (possibly_true(expr
, SPECIAL_EQUAL
, zero_expr()))
1420 false_state
= alloc_estate_sval(zero
);
1422 false_state
= alloc_estate_empty();
1423 set_extra_true_false(name
, sym
, true_state
, false_state
);
1428 match_comparison(expr
);
1430 case EXPR_ASSIGNMENT
:
1431 __extra_match_condition(expr
->left
);
1434 if (expr
->op
== '&')
1435 handle_AND_condition(expr
);
1440 static void assume_indexes_are_valid(struct expression
*expr
)
1442 struct expression
*array_expr
;
1444 struct expression
*offset
;
1445 struct symbol
*offset_type
;
1446 struct range_list
*rl_before
;
1447 struct range_list
*rl_after
;
1448 struct range_list
*filter
= NULL
;
1451 expr
= strip_expr(expr
);
1452 if (!is_array(expr
))
1455 offset
= get_array_offset(expr
);
1456 offset_type
= get_type(offset
);
1457 if (offset_type
&& type_signed(offset_type
)) {
1458 filter
= alloc_rl(sval_type_min(offset_type
),
1459 sval_type_val(offset_type
, -1));
1462 array_expr
= get_array_base(expr
);
1463 array_size
= get_real_array_size(array_expr
);
1464 if (array_size
> 1) {
1465 size
= sval_type_val(offset_type
, array_size
);
1466 add_range(&filter
, size
, sval_type_max(offset_type
));
1471 get_absolute_rl(offset
, &rl_before
);
1472 rl_after
= rl_filter(rl_before
, filter
);
1473 if (rl_equiv(rl_before
, rl_after
))
1475 set_extra_expr_nomod(offset
, alloc_estate_rl(rl_after
));
1478 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1479 int implied_not_equal(struct expression
*expr
, long long val
)
1481 return !possibly_false(expr
, SPECIAL_NOTEQUAL
, value_expr(val
));
1484 int implied_not_equal_name_sym(char *name
, struct symbol
*sym
, long long val
)
1486 struct smatch_state
*estate
;
1488 estate
= get_state(SMATCH_EXTRA
, name
, sym
);
1491 if (!rl_has_sval(estate_rl(estate
), sval_type_val(estate_type(estate
), 0)))
1496 int parent_is_null_var_sym(const char *name
, struct symbol
*sym
)
1501 struct smatch_state
*state
;
1503 strncpy(buf
, name
, sizeof(buf
) - 1);
1504 buf
[sizeof(buf
) - 1] = '\0';
1507 while (*start
== '*') {
1509 state
= get_state(SMATCH_EXTRA
, start
, sym
);
1512 if (!estate_rl(state
))
1514 if (estate_min(state
).value
== 0 &&
1515 estate_max(state
).value
== 0)
1520 while (*start
== '&')
1523 while ((end
= strrchr(start
, '-'))) {
1525 state
= get_state(SMATCH_EXTRA
, start
, sym
);
1528 if (estate_min(state
).value
== 0 &&
1529 estate_max(state
).value
== 0)
1535 int parent_is_null(struct expression
*expr
)
1541 expr
= strip_expr(expr
);
1542 var
= expr_to_var_sym(expr
, &sym
);
1545 ret
= parent_is_null_var_sym(var
, sym
);
1551 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
1553 if (estate_is_whole(sm
->state
))
1555 sql_insert_caller_info(call
, PARAM_VALUE
, param
, printed_name
, sm
->state
->name
);
1556 if (estate_has_fuzzy_max(sm
->state
))
1557 sql_insert_caller_info(call
, FUZZY_MAX
, param
, printed_name
,
1558 sval_to_str(estate_get_fuzzy_max(sm
->state
)));
1561 static void db_limited_before(void)
1563 unmatched_stree
= clone_stree(__get_cur_stree());
1566 static void db_limited_after(void)
1568 free_stree(&unmatched_stree
);
1571 static void db_param_limit_filter(struct expression
*expr
, int param
, char *key
, char *value
)
1573 struct expression
*arg
;
1576 struct sm_state
*sm
;
1577 struct symbol
*compare_type
, *var_type
;
1578 struct range_list
*rl
;
1579 struct range_list
*limit
;
1580 struct range_list
*new;
1582 while (expr
->type
== EXPR_ASSIGNMENT
)
1583 expr
= strip_expr(expr
->right
);
1584 if (expr
->type
!= EXPR_CALL
)
1587 arg
= get_argument_from_call_expr(expr
->args
, param
);
1591 name
= get_variable_from_key(arg
, key
, &sym
);
1595 if (strcmp(key
, "$") == 0)
1596 compare_type
= get_arg_type(expr
->fn
, param
);
1598 compare_type
= get_member_type_from_key(arg
, key
);
1600 sm
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
1602 rl
= estate_rl(sm
->state
);
1604 rl
= alloc_whole_rl(compare_type
);
1606 call_results_to_rl(expr
, compare_type
, value
, &limit
);
1607 new = rl_intersection(rl
, limit
);
1609 var_type
= get_member_type_from_key(arg
, key
);
1610 new = cast_rl(var_type
, new);
1612 /* We want to preserve the implications here */
1613 if (sm
&& rl_equiv(estate_rl(sm
->state
), new))
1616 set_extra_nomod(name
, sym
, alloc_estate_rl(new));
1622 static void db_param_add_set(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
1624 struct expression
*arg
;
1627 struct symbol
*type
;
1628 struct smatch_state
*state
;
1629 struct range_list
*added
= NULL
;
1630 struct range_list
*new;
1632 while (expr
->type
== EXPR_ASSIGNMENT
)
1633 expr
= strip_expr(expr
->right
);
1634 if (expr
->type
!= EXPR_CALL
)
1637 arg
= get_argument_from_call_expr(expr
->args
, param
);
1640 name
= get_variable_from_key(arg
, key
, &sym
);
1644 type
= get_member_type_from_key(arg
, key
);
1645 call_results_to_rl(expr
, type
, value
, &added
);
1647 if (op
== PARAM_SET
) {
1650 state
= get_state(SMATCH_EXTRA
, name
, sym
);
1652 new = rl_union(estate_rl(state
), added
);
1654 new = alloc_whole_rl(type
);
1657 set_extra_mod(name
, sym
, alloc_estate_rl(new));
1662 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
1664 db_param_add_set(expr
, param
, key
, value
, ADDED_VALUE
);
1667 static void db_param_set(struct expression
*expr
, int param
, char *key
, char *value
)
1669 db_param_add_set(expr
, param
, key
, value
, PARAM_SET
);
1672 static void db_returned_member_info(struct expression
*expr
, int param
, char *key
, char *value
)
1674 struct symbol
*type
;
1677 struct range_list
*rl
;
1679 if (expr
->type
!= EXPR_ASSIGNMENT
)
1682 name
= return_state_to_var_sym(expr
, param
, key
, &sym
);
1685 type
= get_member_type_from_key(expr
->left
, key
);
1688 call_results_to_rl(expr
->right
, type
, value
, &rl
);
1689 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
1695 static char *in_terms_of_param_math(struct expression
*expr
, char *printed_name
)
1702 var
= expr_to_var_sym(expr
, &sym
);
1705 snprintf(buf
, sizeof(buf
), "%s%s", var
, printed_name
+ 2);
1707 ret
= get_value_in_terms_of_parameter_math_var_sym(buf
, sym
);
1714 static void returned_member_callback(int return_id
, char *return_ranges
, struct expression
*expr
, char *printed_name
, struct smatch_state
*state
)
1720 /* these are handled in smatch_param_filter/set/limit.c */
1721 if (printed_name
[0] != '*' &&
1722 !strchr(printed_name
, '.') && !strchr(printed_name
, '-'))
1725 if (estate_get_single_value(state
, &sval
)) {
1726 sql_insert_return_states(return_id
, return_ranges
, RETURN_VALUE
, -1,
1727 printed_name
, sval_to_str(sval
));
1731 math_str
= in_terms_of_param_math(expr
, printed_name
);
1733 snprintf(buf
, sizeof(buf
), "%s[%s]", state
->name
, math_str
);
1734 sql_insert_return_states(return_id
, return_ranges
, RETURN_VALUE
, -1,
1739 if (estate_is_whole(state
))
1742 sql_insert_return_states(return_id
, return_ranges
, RETURN_VALUE
, -1,
1743 printed_name
, state
->name
);
1746 static void match_call_info(struct expression
*expr
)
1748 struct smatch_state
*state
;
1749 struct range_list
*rl
= NULL
;
1750 struct expression
*arg
;
1751 struct symbol
*type
;
1754 FOR_EACH_PTR(expr
->args
, arg
) {
1755 type
= get_arg_type(expr
->fn
, i
);
1757 if (get_implied_rl(arg
, &rl
))
1758 rl
= cast_rl(type
, rl
);
1760 rl
= cast_rl(type
, alloc_whole_rl(get_type(arg
)));
1762 if (!is_whole_rl(rl
))
1763 sql_insert_caller_info(expr
, PARAM_VALUE
, i
, "$", show_rl(rl
));
1764 state
= get_state_expr(SMATCH_EXTRA
, arg
);
1765 if (estate_has_fuzzy_max(state
)) {
1766 sql_insert_caller_info(expr
, FUZZY_MAX
, i
, "$",
1767 sval_to_str(estate_get_fuzzy_max(state
)));
1770 } END_FOR_EACH_PTR(arg
);
1773 static void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1775 struct range_list
*rl
= NULL
;
1776 struct smatch_state
*state
;
1777 struct symbol
*type
;
1780 if (strcmp(key
, "*$") == 0)
1781 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
1782 else if (strncmp(key
, "$", 1) == 0)
1783 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
1787 type
= get_member_type_from_key(symbol_expression(sym
), key
);
1788 str_to_rl(type
, value
, &rl
);
1789 state
= alloc_estate_rl(rl
);
1790 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
1793 static void set_param_hard_max(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1795 struct range_list
*rl
= NULL
;
1796 struct smatch_state
*state
;
1797 struct symbol
*type
;
1801 if (strcmp(key
, "*$") == 0)
1802 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
1803 else if (strncmp(key
, "$", 1) == 0)
1804 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
1808 state
= get_state(SMATCH_EXTRA
, fullname
, sym
);
1811 type
= get_member_type_from_key(symbol_expression(sym
), key
);
1812 str_to_rl(type
, value
, &rl
);
1813 if (!rl_to_sval(rl
, &max
))
1815 estate_set_fuzzy_max(state
, max
);
1818 void register_smatch_extra(int id
)
1822 add_merge_hook(my_id
, &merge_estates
);
1823 add_unmatched_state_hook(my_id
, &unmatched_state
);
1824 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
1825 add_hook(&match_declarations
, DECLARATION_HOOK
);
1826 select_caller_info_hook(set_param_value
, PARAM_VALUE
);
1827 select_caller_info_hook(set_param_hard_max
, FUZZY_MAX
);
1828 select_return_states_hook(RETURN_VALUE
, &db_returned_member_info
);
1829 select_return_states_before(&db_limited_before
);
1830 select_return_states_hook(LIMITED_VALUE
, &db_param_limit_filter
);
1831 select_return_states_hook(FILTER_VALUE
, &db_param_limit_filter
);
1832 select_return_states_hook(ADDED_VALUE
, &db_param_add
);
1833 select_return_states_hook(PARAM_SET
, &db_param_set
);
1834 select_return_states_after(&db_limited_after
);
1837 void register_smatch_extra_late(int id
)
1839 add_hook(&match_dereferences
, DEREF_HOOK
);
1840 add_hook(&match_pointer_as_array
, OP_HOOK
);
1841 select_call_implies_hook(DEREFERENCE
, &set_param_dereferenced
);
1842 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
1843 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
1844 add_hook(&match_assign
, GLOBAL_ASSIGNMENT_HOOK
);
1845 add_hook(&unop_expr
, OP_HOOK
);
1846 add_hook(&asm_expr
, ASM_HOOK
);
1848 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1849 add_member_info_callback(my_id
, struct_member_callback
);
1850 add_returned_member_callback(my_id
, returned_member_callback
);
1852 add_hook(&assume_indexes_are_valid
, OP_HOOK
);