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(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
78 if (in_warn_on_macro())
80 remove_from_equiv(name
, sym
);
81 call_extra_mod_hooks(name
, sym
, state
);
82 return set_state(SMATCH_EXTRA
, name
, sym
, state
);
85 struct sm_state
*set_extra_expr_mod(struct expression
*expr
, struct smatch_state
*state
)
89 struct sm_state
*ret
= NULL
;
91 expr
= strip_expr(expr
);
92 name
= expr_to_var_sym(expr
, &sym
);
95 ret
= set_extra_mod(name
, sym
, state
);
101 void set_extra_nomod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
103 struct relation
*rel
;
104 struct smatch_state
*orig_state
;
106 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
108 if (!estate_related(orig_state
)) {
109 set_state(SMATCH_EXTRA
, name
, sym
, state
);
113 set_related(state
, estate_related(orig_state
));
114 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
115 if (option_debug_related
)
116 sm_msg("%s updating related %s to %s", name
, rel
->name
, state
->name
);
117 set_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
, state
);
118 } END_FOR_EACH_PTR(rel
);
122 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
124 void set_extra_expr_nomod(struct expression
*expr
, struct smatch_state
*state
)
129 name
= expr_to_var_sym(expr
, &sym
);
132 set_extra_nomod(name
, sym
, state
);
138 static void set_extra_true_false(const char *name
, struct symbol
*sym
,
139 struct smatch_state
*true_state
,
140 struct smatch_state
*false_state
)
142 struct relation
*rel
;
143 struct smatch_state
*orig_state
;
145 if (in_warn_on_macro())
148 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
150 if (!estate_related(orig_state
)) {
151 set_true_false_states(SMATCH_EXTRA
, name
, sym
, true_state
, false_state
);
156 set_related(true_state
, estate_related(orig_state
));
158 set_related(false_state
, estate_related(orig_state
));
160 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
161 set_true_false_states(SMATCH_EXTRA
, rel
->name
, rel
->sym
,
162 true_state
, false_state
);
163 } END_FOR_EACH_PTR(rel
);
166 static void set_extra_expr_true_false(struct expression
*expr
,
167 struct smatch_state
*true_state
,
168 struct smatch_state
*false_state
)
173 expr
= strip_expr(expr
);
174 name
= expr_to_var_sym(expr
, &sym
);
177 set_extra_true_false(name
, sym
, true_state
, false_state
);
182 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
184 struct expression
*iter_var
;
185 struct expression
*condition
;
187 struct smatch_state
*estate
;
190 condition
= strip_expr(loop
->iterator_pre_condition
);
193 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
195 if (condition
->op
!= SPECIAL_DECREMENT
)
198 iter_var
= condition
->unop
;
199 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
202 if (sval_cmp_val(estate_min(sm
->state
), 0) < 0)
204 start
= estate_max(sm
->state
);
205 if (sval_cmp_val(start
, 0) <= 0)
207 if (!sval_is_max(start
))
210 if (condition
->type
== EXPR_PREOP
) {
211 estate
= alloc_estate_range(sval_type_val(start
.type
, 1), start
);
212 if (estate_has_hard_max(sm
->state
))
213 estate_set_hard_max(estate
);
214 estate_copy_fuzzy_max(estate
, sm
->state
);
215 set_extra_expr_mod(iter_var
, estate
);
217 if (condition
->type
== EXPR_POSTOP
) {
218 estate
= alloc_estate_range(sval_type_val(start
.type
, 0), start
);
219 if (estate_has_hard_max(sm
->state
))
220 estate_set_hard_max(estate
);
221 estate_copy_fuzzy_max(estate
, sm
->state
);
222 set_extra_expr_mod(iter_var
, estate
);
224 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
227 static struct sm_state
*handle_canonical_for_inc(struct expression
*iter_expr
,
228 struct expression
*condition
)
230 struct expression
*iter_var
;
232 struct smatch_state
*estate
;
233 sval_t start
, end
, max
;
235 iter_var
= iter_expr
->unop
;
236 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
239 if (!estate_get_single_value(sm
->state
, &start
))
241 if (get_implied_max(condition
->right
, &end
))
242 end
= sval_cast(get_type(iter_var
), end
);
244 end
= sval_type_max(get_type(iter_var
));
246 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
249 switch (condition
->op
) {
250 case SPECIAL_UNSIGNED_LT
:
251 case SPECIAL_NOTEQUAL
:
253 if (!sval_is_min(end
))
256 case SPECIAL_UNSIGNED_LTE
:
262 if (sval_cmp(end
, start
) < 0)
264 estate
= alloc_estate_range(start
, end
);
265 if (get_hard_max(condition
->right
, &max
)) {
266 estate_set_hard_max(estate
);
267 if (condition
->op
== '<' ||
268 condition
->op
== SPECIAL_UNSIGNED_LT
||
269 condition
->op
== SPECIAL_NOTEQUAL
)
271 estate_set_fuzzy_max(estate
, max
);
273 set_extra_expr_mod(iter_var
, estate
);
274 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
277 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
278 struct expression
*condition
)
280 struct expression
*iter_var
;
282 struct smatch_state
*estate
;
285 iter_var
= iter_expr
->unop
;
286 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
289 if (!estate_get_single_value(sm
->state
, &start
))
291 if (!get_implied_min(condition
->right
, &end
))
292 end
= sval_type_min(get_type(iter_var
));
293 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
296 switch (condition
->op
) {
297 case SPECIAL_NOTEQUAL
:
299 if (!sval_is_min(end
) && !sval_is_max(end
))
307 if (sval_cmp(end
, start
) > 0)
309 estate
= alloc_estate_range(end
, start
);
310 estate_set_hard_max(estate
);
311 estate_set_fuzzy_max(estate
, estate_get_fuzzy_max(estate
));
312 set_extra_expr_mod(iter_var
, estate
);
313 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
316 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
318 struct expression
*iter_expr
;
319 struct expression
*condition
;
321 if (!loop
->iterator_post_statement
)
323 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
325 iter_expr
= loop
->iterator_post_statement
->expression
;
326 if (!loop
->iterator_pre_condition
)
328 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
330 condition
= loop
->iterator_pre_condition
;
332 if (iter_expr
->op
== SPECIAL_INCREMENT
)
333 return handle_canonical_for_inc(iter_expr
, condition
);
334 if (iter_expr
->op
== SPECIAL_DECREMENT
)
335 return handle_canonical_for_dec(iter_expr
, condition
);
339 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct stree
**stree
)
341 struct sm_state
*ret
;
343 __push_fake_cur_stree();
344 if (!loop
->iterator_post_statement
)
345 ret
= handle_canonical_while_count_down(loop
);
347 ret
= handle_canonical_for_loops(loop
);
348 *stree
= __pop_fake_cur_stree();
352 int __iterator_unchanged(struct sm_state
*sm
)
356 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
361 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
365 /* paranoid checking. prolly not needed */
366 condition
= strip_expr(condition
);
369 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
371 if (condition
->op
!= SPECIAL_DECREMENT
)
373 after_value
= estate_min(sm
->state
);
375 set_extra_mod(sm
->name
, sm
->sym
, alloc_estate_sval(after_value
));
378 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
379 struct statement
*iterator
,
380 struct expression
*condition
)
382 struct expression
*iter_expr
;
384 struct smatch_state
*state
;
387 while_count_down_after(sm
, condition
);
391 iter_expr
= iterator
->expression
;
393 if (condition
->type
!= EXPR_COMPARE
)
395 if (iter_expr
->op
== SPECIAL_INCREMENT
) {
396 limit
= sval_binop(estate_max(sm
->state
), '+',
397 sval_type_val(estate_type(sm
->state
), 1));
399 limit
= sval_binop(estate_min(sm
->state
), '-',
400 sval_type_val(estate_type(sm
->state
), 1));
402 if (!estate_has_hard_max(sm
->state
) && !__has_breaks()) {
403 if (iter_expr
->op
== SPECIAL_INCREMENT
)
404 state
= alloc_estate_range(estate_min(sm
->state
), limit
);
406 state
= alloc_estate_range(limit
, estate_max(sm
->state
));
408 state
= alloc_estate_sval(limit
);
410 if (!estate_has_hard_max(sm
->state
)) {
411 estate_clear_hard_max(state
);
413 if (estate_has_fuzzy_max(sm
->state
)) {
414 sval_t hmax
= estate_get_fuzzy_max(sm
->state
);
415 sval_t max
= estate_max(sm
->state
);
417 if (sval_cmp(hmax
, max
) != 0)
418 estate_clear_fuzzy_max(state
);
419 } else if (!estate_has_fuzzy_max(sm
->state
)) {
420 estate_clear_fuzzy_max(state
);
423 set_extra_mod(sm
->name
, sm
->sym
, state
);
426 static struct stree
*unmatched_stree
;
427 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
429 struct smatch_state
*state
;
431 if (unmatched_stree
) {
432 state
= get_state_stree(unmatched_stree
, SMATCH_EXTRA
, sm
->name
, sm
->sym
);
436 if (parent_is_null_var_sym(sm
->name
, sm
->sym
))
437 return alloc_estate_empty();
438 return alloc_estate_whole(estate_type(sm
->state
));
441 static void clear_the_pointed_at(struct expression
*expr
)
446 struct sm_state
*tmp
;
448 name
= expr_to_var_sym(expr
, &sym
);
452 stree
= __get_cur_stree();
453 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
454 if (tmp
->name
[0] != '*')
458 if (strcmp(tmp
->name
+ 1, name
) != 0)
460 set_extra_mod(tmp
->name
, tmp
->sym
, alloc_estate_whole(estate_type(tmp
->state
)));
461 } END_FOR_EACH_SM(tmp
);
467 static void match_function_call(struct expression
*expr
)
469 struct expression
*arg
;
470 struct expression
*tmp
;
472 /* if we have the db this is handled in smatch_function_hooks.c */
475 if (inlinable(expr
->fn
))
478 FOR_EACH_PTR(expr
->args
, arg
) {
479 tmp
= strip_expr(arg
);
480 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
481 set_extra_expr_mod(tmp
->unop
, alloc_estate_whole(get_type(tmp
->unop
)));
483 clear_the_pointed_at(tmp
);
484 } END_FOR_EACH_PTR(arg
);
487 static int types_equiv_or_pointer(struct symbol
*one
, struct symbol
*two
)
491 if (one
->type
== SYM_PTR
&& two
->type
== SYM_PTR
)
493 return types_equiv(one
, two
);
496 static bool is_non_null_array(struct expression
*expr
)
504 type
= get_type(expr
);
505 if (!type
|| type
->type
!= SYM_ARRAY
)
507 if (expr
->type
== EXPR_SYMBOL
)
509 if (implied_not_equal(expr
, 0))
512 /* verify that it's not the first member of the struct */
513 if (expr
->type
!= EXPR_DEREF
|| !expr
->member
)
515 name
= expr_to_var_sym(expr
, &sym
);
519 type
= get_real_base_type(sym
);
520 if (!type
|| type
->type
!= SYM_PTR
)
522 type
= get_real_base_type(type
);
523 if (type
->type
!= SYM_STRUCT
)
527 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
531 if (strcmp(expr
->member
->name
, tmp
->ident
->name
) == 0) {
536 } END_FOR_EACH_PTR(tmp
);
541 static void match_vanilla_assign(struct expression
*left
, struct expression
*right
)
543 struct range_list
*rl
= NULL
;
544 struct symbol
*right_sym
;
545 struct symbol
*left_type
;
546 struct symbol
*right_type
;
547 char *right_name
= NULL
;
551 struct smatch_state
*state
;
556 name
= expr_to_var_sym(left
, &sym
);
560 left_type
= get_type(left
);
561 right_type
= get_type(right
);
563 right_name
= expr_to_var_sym(right
, &right_sym
);
565 if (!(right
->type
== EXPR_PREOP
&& right
->op
== '&') &&
566 right_name
&& right_sym
&&
567 types_equiv_or_pointer(left_type
, right_type
) &&
568 !has_symbol(right
, sym
)) {
569 set_equiv(left
, right
);
573 if (is_non_null_array(right
)) {
574 state
= alloc_estate_range(array_min_sval
, array_max_sval
);
575 } else if (get_implied_rl(right
, &rl
)) {
576 rl
= cast_rl(left_type
, rl
);
577 state
= alloc_estate_rl(rl
);
578 if (get_hard_max(right
, &max
)) {
579 estate_set_hard_max(state
);
580 estate_set_fuzzy_max(state
, max
);
583 rl
= alloc_whole_rl(right_type
);
584 rl
= cast_rl(left_type
, rl
);
585 state
= alloc_estate_rl(rl
);
587 set_extra_mod(name
, sym
, state
);
589 free_string(right_name
);
592 static int op_remove_assign(int op
)
595 case SPECIAL_ADD_ASSIGN
:
597 case SPECIAL_SUB_ASSIGN
:
599 case SPECIAL_MUL_ASSIGN
:
601 case SPECIAL_DIV_ASSIGN
:
603 case SPECIAL_MOD_ASSIGN
:
605 case SPECIAL_AND_ASSIGN
:
607 case SPECIAL_OR_ASSIGN
:
609 case SPECIAL_XOR_ASSIGN
:
611 case SPECIAL_SHL_ASSIGN
:
612 return SPECIAL_LEFTSHIFT
;
613 case SPECIAL_SHR_ASSIGN
:
614 return SPECIAL_RIGHTSHIFT
;
620 static void match_assign(struct expression
*expr
)
622 struct range_list
*rl
= NULL
;
623 struct expression
*left
;
624 struct expression
*right
;
625 struct expression
*binop_expr
;
626 struct symbol
*left_type
;
627 struct symbol
*right_type
;
630 sval_t left_min
, left_max
;
631 sval_t right_min
, right_max
;
632 sval_t res_min
, res_max
;
634 left
= strip_expr(expr
->left
);
636 right
= strip_expr(expr
->right
);
637 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
638 right
= strip_expr(right
->left
);
640 if (is_condition(expr
->right
))
641 return; /* handled in smatch_condition.c */
642 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
643 return; /* handled in smatch_function_hooks.c */
644 if (expr
->op
== '=') {
645 match_vanilla_assign(left
, right
);
649 name
= expr_to_var_sym(left
, &sym
);
653 left_type
= get_type(left
);
654 right_type
= get_type(right
);
656 res_min
= sval_type_min(left_type
);
657 res_max
= sval_type_max(left_type
);
660 case SPECIAL_ADD_ASSIGN
:
661 get_absolute_max(left
, &left_max
);
662 get_absolute_max(right
, &right_max
);
663 if (sval_binop_overflows(left_max
, '+', right_max
))
665 if (get_implied_min(left
, &left_min
) &&
666 !sval_is_negative_min(left_min
) &&
667 get_implied_min(right
, &right_min
) &&
668 !sval_is_negative_min(right_min
)) {
669 res_min
= sval_binop(left_min
, '+', right_min
);
670 res_min
= sval_cast(right_type
, res_min
);
672 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
674 res_max
= sval_binop(left_max
, '+', right_max
);
675 res_max
= sval_cast(right_type
, res_max
);
677 case SPECIAL_SUB_ASSIGN
:
678 if (get_implied_max(left
, &left_max
) &&
679 !sval_is_max(left_max
) &&
680 get_implied_min(right
, &right_min
) &&
681 !sval_is_min(right_min
)) {
682 res_max
= sval_binop(left_max
, '-', right_min
);
683 res_max
= sval_cast(right_type
, res_max
);
687 if (get_implied_min(left
, &left_min
) &&
688 !sval_is_min(left_min
) &&
689 get_implied_max(right
, &right_max
) &&
690 !sval_is_max(right_max
)) {
691 res_min
= sval_binop(left_min
, '-', right_max
);
692 res_min
= sval_cast(right_type
, res_min
);
695 case SPECIAL_AND_ASSIGN
:
696 case SPECIAL_MOD_ASSIGN
:
697 case SPECIAL_SHL_ASSIGN
:
698 case SPECIAL_SHR_ASSIGN
:
699 case SPECIAL_OR_ASSIGN
:
700 case SPECIAL_XOR_ASSIGN
:
701 case SPECIAL_MUL_ASSIGN
:
702 case SPECIAL_DIV_ASSIGN
:
703 binop_expr
= binop_expression(expr
->left
,
704 op_remove_assign(expr
->op
),
706 if (get_absolute_rl(binop_expr
, &rl
)) {
707 rl
= cast_rl(left_type
, rl
);
708 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
713 rl
= cast_rl(left_type
, alloc_rl(res_min
, res_max
));
714 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
719 static struct smatch_state
*increment_state(struct smatch_state
*state
)
721 sval_t min
= estate_min(state
);
722 sval_t max
= estate_max(state
);
724 if (!estate_rl(state
))
728 max
= sval_type_max(max
.type
);
730 if (!sval_is_min(min
) && !sval_is_max(min
))
732 if (!sval_is_min(max
) && !sval_is_max(max
))
734 return alloc_estate_range(min
, max
);
737 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
739 sval_t min
= estate_min(state
);
740 sval_t max
= estate_max(state
);
742 if (!estate_rl(state
))
746 min
= sval_type_min(min
.type
);
748 if (!sval_is_min(min
) && !sval_is_max(min
))
750 if (!sval_is_min(max
) && !sval_is_max(max
))
752 return alloc_estate_range(min
, max
);
755 static void unop_expr(struct expression
*expr
)
757 struct smatch_state
*state
;
759 if (expr
->smatch_flags
& Handled
)
763 case SPECIAL_INCREMENT
:
764 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
765 state
= increment_state(state
);
767 state
= alloc_estate_whole(get_type(expr
));
768 set_extra_expr_mod(expr
->unop
, state
);
770 case SPECIAL_DECREMENT
:
771 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
772 state
= decrement_state(state
);
774 state
= alloc_estate_whole(get_type(expr
));
775 set_extra_expr_mod(expr
->unop
, state
);
782 static void asm_expr(struct statement
*stmt
)
785 struct expression
*expr
;
789 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
791 case 0: /* identifier */
792 case 1: /* constraint */
795 case 2: /* expression */
797 type
= get_type(strip_expr(expr
));
798 set_extra_expr_mod(expr
, alloc_estate_whole(type
));
801 } END_FOR_EACH_PTR(expr
);
804 static void delete_state_tracker(struct tracker
*t
)
806 remove_from_equiv(t
->name
, t
->sym
);
807 delete_state(t
->owner
, t
->name
, t
->sym
);
810 static void scoped_state_extra(const char *name
, struct symbol
*sym
)
814 t
= alloc_tracker(SMATCH_EXTRA
, name
, sym
);
815 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
818 static void match_declarations(struct symbol
*sym
)
823 name
= sym
->ident
->name
;
824 if (!sym
->initializer
) {
825 set_state(SMATCH_EXTRA
, name
, sym
, alloc_estate_whole(get_real_base_type(sym
)));
826 scoped_state_extra(name
, sym
);
831 static void check_dereference(struct expression
*expr
)
833 if (outside_of_function())
835 set_extra_expr_nomod(expr
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
838 static void match_dereferences(struct expression
*expr
)
840 if (expr
->type
!= EXPR_PREOP
)
842 check_dereference(expr
->unop
);
845 static void match_pointer_as_array(struct expression
*expr
)
849 check_dereference(expr
->unop
->left
);
852 static void set_param_dereferenced(struct expression
*arg
, char *key
, char *unused
)
857 name
= get_variable_from_key(arg
, key
, &sym
);
861 set_extra_nomod(name
, sym
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
867 static void match_function_def(struct symbol
*sym
)
871 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
874 set_state(my_id
, arg
->ident
->name
, arg
, alloc_estate_whole(get_real_base_type(arg
)));
875 } END_FOR_EACH_PTR(arg
);
878 static sval_t
add_one(sval_t sval
)
884 static sval_t
sub_one(sval_t sval
)
890 static void handle_comparison(struct symbol
*type
, struct expression
*left
, int op
, struct expression
*right
)
892 struct range_list
*left_orig
;
893 struct range_list
*left_true
;
894 struct range_list
*left_false
;
895 struct range_list
*right_orig
;
896 struct range_list
*right_true
;
897 struct range_list
*right_false
;
898 struct smatch_state
*left_true_state
;
899 struct smatch_state
*left_false_state
;
900 struct smatch_state
*right_true_state
;
901 struct smatch_state
*right_false_state
;
902 sval_t min
, max
, dummy
, hard_max
;
904 int right_postop
= 0;
906 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
907 if (left
->type
== EXPR_POSTOP
) {
908 left
->smatch_flags
|= Handled
;
909 left_postop
= left
->op
;
911 left
= strip_expr(left
->unop
);
913 while (left
->type
== EXPR_ASSIGNMENT
)
914 left
= strip_expr(left
->left
);
916 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
917 if (right
->type
== EXPR_POSTOP
) {
918 right
->smatch_flags
|= Handled
;
919 right_postop
= right
->op
;
921 right
= strip_expr(right
->unop
);
924 if (get_implied_rl(left
, &left_orig
)) {
925 left_orig
= cast_rl(type
, left_orig
);
927 min
= sval_type_min(get_type(left
));
928 max
= sval_type_max(get_type(left
));
929 left_orig
= cast_rl(type
, alloc_rl(min
, max
));
932 if (get_implied_rl(right
, &right_orig
)) {
933 right_orig
= cast_rl(type
, right_orig
);
935 min
= sval_type_min(get_type(right
));
936 max
= sval_type_max(get_type(right
));
937 right_orig
= cast_rl(type
, alloc_rl(min
, max
));
939 min
= sval_type_min(type
);
940 max
= sval_type_max(type
);
942 left_true
= clone_rl(left_orig
);
943 left_false
= clone_rl(left_orig
);
944 right_true
= clone_rl(right_orig
);
945 right_false
= clone_rl(right_orig
);
949 case SPECIAL_UNSIGNED_LT
:
950 left_true
= remove_range(left_orig
, rl_max(right_orig
), max
);
951 if (!sval_is_min(rl_min(right_orig
))) {
952 left_false
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
955 right_true
= remove_range(right_orig
, min
, rl_min(left_orig
));
956 if (!sval_is_max(rl_max(left_orig
)))
957 right_false
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
959 case SPECIAL_UNSIGNED_LTE
:
961 if (!sval_is_max(rl_max(right_orig
)))
962 left_true
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
963 left_false
= remove_range(left_orig
, min
, rl_min(right_orig
));
965 if (!sval_is_min(rl_min(left_orig
)))
966 right_true
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
967 right_false
= remove_range(right_orig
, rl_max(left_orig
), max
);
969 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
970 left_false
= remove_range(left_false
, rl_min(left_orig
), rl_min(left_orig
));
971 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
972 right_false
= remove_range(right_false
, rl_max(left_orig
), rl_max(left_orig
));
975 if (!sval_is_max(rl_max(right_orig
))) {
976 left_true
= remove_range(left_true
, add_one(rl_max(right_orig
)), max
);
978 if (!sval_is_min(rl_min(right_orig
))) {
979 left_true
= remove_range(left_true
, min
, sub_one(rl_min(right_orig
)));
981 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
982 left_false
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
984 if (!sval_is_max(rl_max(left_orig
)))
985 right_true
= remove_range(right_true
, add_one(rl_max(left_orig
)), max
);
986 if (!sval_is_min(rl_min(left_orig
)))
987 right_true
= remove_range(right_true
, min
, sub_one(rl_min(left_orig
)));
988 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
989 right_false
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
991 case SPECIAL_UNSIGNED_GTE
:
993 if (!sval_is_min(rl_min(right_orig
)))
994 left_true
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
995 left_false
= remove_range(left_orig
, rl_max(right_orig
), max
);
997 if (!sval_is_max(rl_max(left_orig
)))
998 right_true
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
999 right_false
= remove_range(right_orig
, min
, rl_min(left_orig
));
1001 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
1002 right_false
= remove_range(right_false
, rl_min(left_orig
), rl_min(left_orig
));
1003 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
1004 left_false
= remove_range(left_false
, rl_max(left_orig
), rl_max(left_orig
));
1007 case SPECIAL_UNSIGNED_GT
:
1008 left_true
= remove_range(left_orig
, min
, rl_min(right_orig
));
1009 if (!sval_is_max(rl_max(right_orig
)))
1010 left_false
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
1012 right_true
= remove_range(right_orig
, rl_max(left_orig
), max
);
1013 if (!sval_is_min(rl_min(left_orig
)))
1014 right_false
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
1016 case SPECIAL_NOTEQUAL
:
1017 if (!sval_is_max(rl_max(right_orig
)))
1018 left_false
= remove_range(left_false
, add_one(rl_max(right_orig
)), max
);
1019 if (!sval_is_min(rl_min(right_orig
)))
1020 left_false
= remove_range(left_false
, min
, sub_one(rl_min(right_orig
)));
1021 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
1022 left_true
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
1024 if (!sval_is_max(rl_max(left_orig
)))
1025 right_false
= remove_range(right_false
, add_one(rl_max(left_orig
)), max
);
1026 if (!sval_is_min(rl_min(left_orig
)))
1027 right_false
= remove_range(right_false
, min
, sub_one(rl_min(left_orig
)));
1028 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
1029 right_true
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
1035 left_true
= rl_truncate_cast(get_type(left
), left_true
);
1036 left_false
= rl_truncate_cast(get_type(left
), left_false
);
1037 right_true
= rl_truncate_cast(get_type(right
), right_true
);
1038 right_false
= rl_truncate_cast(get_type(right
), right_false
);
1040 left_true_state
= alloc_estate_rl(left_true
);
1041 left_false_state
= alloc_estate_rl(left_false
);
1042 right_true_state
= alloc_estate_rl(right_true
);
1043 right_false_state
= alloc_estate_rl(right_false
);
1047 case SPECIAL_UNSIGNED_LT
:
1048 case SPECIAL_UNSIGNED_LTE
:
1050 if (get_hard_max(right
, &dummy
))
1051 estate_set_hard_max(left_true_state
);
1052 if (get_hard_max(left
, &dummy
))
1053 estate_set_hard_max(right_false_state
);
1056 case SPECIAL_UNSIGNED_GT
:
1057 case SPECIAL_UNSIGNED_GTE
:
1059 if (get_hard_max(left
, &dummy
))
1060 estate_set_hard_max(right_true_state
);
1061 if (get_hard_max(right
, &dummy
))
1062 estate_set_hard_max(left_false_state
);
1068 case SPECIAL_UNSIGNED_LT
:
1069 case SPECIAL_UNSIGNED_LTE
:
1071 if (get_hard_max(right
, &hard_max
)) {
1072 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1074 estate_set_fuzzy_max(left_true_state
, hard_max
);
1076 if (get_implied_value(right
, &hard_max
)) {
1077 if (op
== SPECIAL_UNSIGNED_LTE
||
1080 estate_set_fuzzy_max(left_false_state
, hard_max
);
1082 if (get_hard_max(left
, &hard_max
)) {
1083 if (op
== SPECIAL_UNSIGNED_LTE
||
1086 estate_set_fuzzy_max(right_false_state
, hard_max
);
1088 if (get_implied_value(left
, &hard_max
)) {
1089 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1091 estate_set_fuzzy_max(right_true_state
, hard_max
);
1095 case SPECIAL_UNSIGNED_GT
:
1096 case SPECIAL_UNSIGNED_GTE
:
1098 if (get_hard_max(left
, &hard_max
)) {
1099 if (op
== '>' || op
== SPECIAL_UNSIGNED_GT
)
1101 estate_set_fuzzy_max(right_true_state
, hard_max
);
1103 if (get_implied_value(left
, &hard_max
)) {
1104 if (op
== SPECIAL_UNSIGNED_GTE
||
1107 estate_set_fuzzy_max(right_false_state
, hard_max
);
1109 if (get_hard_max(right
, &hard_max
)) {
1110 if (op
== SPECIAL_UNSIGNED_LTE
||
1113 estate_set_fuzzy_max(left_false_state
, hard_max
);
1115 if (get_implied_value(right
, &hard_max
)) {
1117 op
== SPECIAL_UNSIGNED_GT
)
1119 estate_set_fuzzy_max(left_true_state
, hard_max
);
1123 if (get_hard_max(left
, &hard_max
))
1124 estate_set_fuzzy_max(right_true_state
, hard_max
);
1125 if (get_hard_max(right
, &hard_max
))
1126 estate_set_fuzzy_max(left_true_state
, hard_max
);
1130 if (get_hard_max(left
, &hard_max
)) {
1131 estate_set_hard_max(left_true_state
);
1132 estate_set_hard_max(left_false_state
);
1134 if (get_hard_max(right
, &hard_max
)) {
1135 estate_set_hard_max(right_true_state
);
1136 estate_set_hard_max(right_false_state
);
1139 if (left_postop
== SPECIAL_INCREMENT
) {
1140 left_true_state
= increment_state(left_true_state
);
1141 left_false_state
= increment_state(left_false_state
);
1143 if (left_postop
== SPECIAL_DECREMENT
) {
1144 left_true_state
= decrement_state(left_true_state
);
1145 left_false_state
= decrement_state(left_false_state
);
1147 if (right_postop
== SPECIAL_INCREMENT
) {
1148 right_true_state
= increment_state(right_true_state
);
1149 right_false_state
= increment_state(right_false_state
);
1151 if (right_postop
== SPECIAL_DECREMENT
) {
1152 right_true_state
= decrement_state(right_true_state
);
1153 right_false_state
= decrement_state(right_false_state
);
1156 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
1157 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
1160 static int opposite_op(int op
)
1173 static int is_simple_math(struct expression
*expr
)
1177 if (expr
->type
!= EXPR_BINOP
)
1179 if (!opposite_op(expr
->op
))
1184 static void move_known_values(struct expression
**left_p
, struct expression
**right_p
)
1186 struct expression
*left
= *left_p
;
1187 struct expression
*right
= *right_p
;
1190 if (get_implied_value(left
, &sval
)) {
1191 if (!is_simple_math(right
))
1193 if (right
->op
== '*') {
1196 if (!get_value(right
->right
, &divisor
))
1198 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1200 *left_p
= binop_expression(left
, opposite_op(right
->op
), right
->right
);
1201 *right_p
= right
->left
;
1204 if (right
->op
== '+' && get_value(right
->left
, &sval
)) {
1205 *left_p
= binop_expression(left
, opposite_op(right
->op
), right
->left
);
1206 *right_p
= right
->right
;
1209 if (get_value(right
->right
, &sval
)) {
1210 *left_p
= binop_expression(left
, opposite_op(right
->op
), right
->right
);
1211 *right_p
= right
->left
;
1216 if (get_implied_value(right
, &sval
)) {
1217 if (!is_simple_math(left
))
1219 if (left
->op
== '*') {
1222 if (!get_value(left
->right
, &divisor
))
1224 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1226 *right_p
= binop_expression(right
, opposite_op(left
->op
), left
->right
);
1227 *left_p
= left
->left
;
1230 if (left
->op
== '+' && get_value(left
->left
, &sval
)) {
1231 *right_p
= binop_expression(right
, opposite_op(left
->op
), left
->left
);
1232 *left_p
= left
->right
;
1236 if (get_value(left
->right
, &sval
)) {
1237 *right_p
= binop_expression(right
, opposite_op(left
->op
), left
->right
);
1238 *left_p
= left
->left
;
1245 static int match_func_comparison(struct expression
*expr
)
1247 struct expression
*left
= strip_expr(expr
->left
);
1248 struct expression
*right
= strip_expr(expr
->right
);
1252 * fixme: think about this harder. We should always be trying to limit
1253 * the non-call side as well. If we can't determine the limitter does
1254 * that mean we aren't querying the database and are missing important
1258 if (left
->type
== EXPR_CALL
) {
1259 if (get_implied_value(left
, &sval
)) {
1260 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1263 if (!get_implied_value(right
, &sval
))
1265 function_comparison(expr
->op
, left
, sval
, 1);
1269 if (right
->type
== EXPR_CALL
) {
1270 if (get_implied_value(right
, &sval
)) {
1271 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1274 if (!get_implied_value(left
, &sval
))
1276 function_comparison(expr
->op
, right
, sval
, 0);
1283 static void match_comparison(struct expression
*expr
)
1285 struct expression
*left_orig
= strip_expr(expr
->left
);
1286 struct expression
*right_orig
= strip_expr(expr
->right
);
1287 struct expression
*left
, *right
;
1288 struct expression
*prev
;
1289 struct symbol
*type
;
1291 if (match_func_comparison(expr
))
1294 type
= get_type(expr
);
1296 type
= &llong_ctype
;
1300 move_known_values(&left
, &right
);
1301 handle_comparison(type
, left
, expr
->op
, right
);
1303 prev
= get_assigned_expr(left_orig
);
1304 if (is_simple_math(prev
) && has_variable(prev
, left_orig
) == 0) {
1307 move_known_values(&left
, &right
);
1308 handle_comparison(type
, left
, expr
->op
, right
);
1311 prev
= get_assigned_expr(right_orig
);
1312 if (is_simple_math(prev
) && has_variable(prev
, right_orig
) == 0) {
1315 move_known_values(&left
, &right
);
1316 handle_comparison(type
, left
, expr
->op
, right
);
1320 static void handle_AND_condition(struct expression
*expr
)
1322 struct range_list
*rl
= NULL
;
1325 if (get_implied_value(expr
->left
, &known
) && known
.value
> 0) {
1327 get_absolute_rl(expr
->right
, &rl
);
1328 rl
= remove_range(rl
, sval_type_val(known
.type
, 0), known
);
1329 set_extra_expr_true_false(expr
->right
, alloc_estate_rl(rl
), NULL
);
1333 if (get_implied_value(expr
->right
, &known
) && known
.value
> 0) {
1335 get_absolute_rl(expr
->left
, &rl
);
1336 rl
= remove_range(rl
, sval_type_val(known
.type
, 0), known
);
1337 set_extra_expr_true_false(expr
->left
, alloc_estate_rl(rl
), NULL
);
1342 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1343 void __extra_match_condition(struct expression
*expr
)
1347 struct smatch_state
*pre_state
;
1348 struct smatch_state
*true_state
;
1349 struct smatch_state
*false_state
;
1351 expr
= strip_expr(expr
);
1352 switch (expr
->type
) {
1354 function_comparison(SPECIAL_NOTEQUAL
, expr
, ll_to_sval(0), 1);
1361 zero
= sval_blank(expr
);
1364 name
= expr_to_var_sym(expr
, &sym
);
1367 pre_state
= get_state(my_id
, name
, sym
);
1368 true_state
= estate_filter_sval(pre_state
, zero
);
1369 if (possibly_true(expr
, SPECIAL_EQUAL
, zero_expr()))
1370 false_state
= alloc_estate_sval(zero
);
1372 false_state
= alloc_estate_empty();
1373 set_extra_true_false(name
, sym
, true_state
, false_state
);
1378 match_comparison(expr
);
1380 case EXPR_ASSIGNMENT
:
1381 __extra_match_condition(expr
->left
);
1384 if (expr
->op
== '&')
1385 handle_AND_condition(expr
);
1390 static void assume_indexes_are_valid(struct expression
*expr
)
1392 struct expression
*array_expr
;
1394 struct expression
*offset
;
1395 struct symbol
*offset_type
;
1396 struct range_list
*rl_before
;
1397 struct range_list
*rl_after
;
1398 struct range_list
*filter
= NULL
;
1401 expr
= strip_expr(expr
);
1402 if (!is_array(expr
))
1405 offset
= get_array_offset(expr
);
1406 offset_type
= get_type(offset
);
1407 if (offset_type
&& type_signed(offset_type
)) {
1408 filter
= alloc_rl(sval_type_min(offset_type
),
1409 sval_type_val(offset_type
, -1));
1412 array_expr
= strip_parens(expr
->unop
->left
);
1413 array_size
= get_real_array_size(array_expr
);
1414 if (array_size
> 1) {
1415 size
= sval_type_val(offset_type
, array_size
);
1416 add_range(&filter
, size
, sval_type_max(offset_type
));
1421 get_absolute_rl(offset
, &rl_before
);
1422 rl_after
= rl_filter(rl_before
, filter
);
1423 if (rl_equiv(rl_before
, rl_after
))
1425 set_extra_expr_nomod(offset
, alloc_estate_rl(rl_after
));
1428 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1429 int implied_not_equal(struct expression
*expr
, long long val
)
1431 return !possibly_false(expr
, SPECIAL_NOTEQUAL
, value_expr(val
));
1434 int implied_not_equal_name_sym(char *name
, struct symbol
*sym
, long long val
)
1436 struct smatch_state
*estate
;
1438 estate
= get_state(SMATCH_EXTRA
, name
, sym
);
1441 if (!rl_has_sval(estate_rl(estate
), sval_type_val(estate_type(estate
), 0)))
1446 int parent_is_null_var_sym(const char *name
, struct symbol
*sym
)
1451 struct smatch_state
*state
;
1453 strncpy(buf
, name
, sizeof(buf
) - 1);
1454 buf
[sizeof(buf
) - 1] = '\0';
1457 while ((*start
== '&'))
1460 while ((end
= strrchr(start
, '-'))) {
1462 state
= get_state(SMATCH_EXTRA
, start
, sym
);
1465 if (estate_min(state
).value
== 0 &&
1466 estate_max(state
).value
== 0)
1473 int parent_is_null(struct expression
*expr
)
1479 expr
= strip_expr(expr
);
1480 var
= expr_to_var_sym(expr
, &sym
);
1483 ret
= parent_is_null_var_sym(var
, sym
);
1489 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
)
1491 if (estate_is_whole(state
))
1493 sql_insert_caller_info(call
, PARAM_VALUE
, param
, printed_name
, state
->name
);
1494 if (estate_has_fuzzy_max(state
))
1495 sql_insert_caller_info(call
, FUZZY_MAX
, param
, printed_name
,
1496 sval_to_str(estate_get_fuzzy_max(state
)));
1499 static void db_limited_before(void)
1501 unmatched_stree
= clone_stree(__get_cur_stree());
1504 static void db_limited_after(void)
1506 free_stree(&unmatched_stree
);
1509 static void db_param_limit_filter(struct expression
*expr
, int param
, char *key
, char *value
, int mod
)
1511 struct expression
*arg
;
1514 struct sm_state
*sm
;
1515 struct symbol
*type
;
1516 struct range_list
*rl
;
1517 struct range_list
*limit
;
1518 struct range_list
*new;
1520 while (expr
->type
== EXPR_ASSIGNMENT
)
1521 expr
= strip_expr(expr
->right
);
1522 if (expr
->type
!= EXPR_CALL
)
1525 arg
= get_argument_from_call_expr(expr
->args
, param
);
1529 name
= get_variable_from_key(arg
, key
, &sym
);
1533 sm
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
1534 type
= get_member_type_from_key(arg
, key
);
1536 rl
= estate_rl(sm
->state
);
1538 rl
= alloc_whole_rl(type
);
1540 call_results_to_rl(expr
, type
, value
, &limit
);
1541 new = rl_intersection(rl
, limit
);
1543 /* We want to preserve the implications here */
1544 if (sm
&& rl_equiv(estate_rl(sm
->state
), new)) {
1548 set_extra_mod(name
, sym
, alloc_estate_rl(new));
1550 set_extra_nomod(name
, sym
, alloc_estate_rl(new));
1557 static void db_param_limit(struct expression
*expr
, int param
, char *key
, char *value
)
1559 db_param_limit_filter(expr
, param
, key
, value
, 0);
1562 static void db_param_filter(struct expression
*expr
, int param
, char *key
, char *value
)
1564 db_param_limit_filter(expr
, param
, key
, value
, 1);
1567 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
1569 struct expression
*arg
;
1572 struct symbol
*type
;
1573 struct smatch_state
*state
;
1574 struct range_list
*added
= NULL
;
1575 struct range_list
*new;
1577 while (expr
->type
== EXPR_ASSIGNMENT
)
1578 expr
= strip_expr(expr
->right
);
1579 if (expr
->type
!= EXPR_CALL
)
1582 arg
= get_argument_from_call_expr(expr
->args
, param
);
1585 name
= get_variable_from_key(arg
, key
, &sym
);
1589 type
= get_member_type_from_key(arg
, key
);
1590 state
= get_state(SMATCH_EXTRA
, name
, sym
);
1592 call_results_to_rl(expr
, type
, value
, &added
);
1593 new = rl_union(estate_rl(state
), added
);
1595 new = alloc_whole_rl(type
);
1598 set_extra_mod(name
, sym
, alloc_estate_rl(new));
1603 static void db_returned_member_info(struct expression
*expr
, int param
, char *key
, char *value
)
1605 struct symbol
*type
;
1608 struct range_list
*rl
;
1610 if (expr
->type
!= EXPR_ASSIGNMENT
)
1613 name
= return_state_to_var_sym(expr
, param
, key
, &sym
);
1616 type
= get_member_type_from_key(expr
->left
, key
);
1619 call_results_to_rl(expr
->right
, type
, value
, &rl
);
1620 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
1626 static char *in_terms_of_param_math(struct expression
*expr
, char *printed_name
)
1628 struct expression
*assigned
;
1634 var
= expr_to_var_sym(expr
, &sym
);
1637 snprintf(buf
, sizeof(buf
), "%s%s", var
, printed_name
+ 2);
1639 ret
= get_value_in_terms_of_parameter_math_var_sym(buf
, sym
);
1646 static void returned_member_callback(int return_id
, char *return_ranges
, struct expression
*expr
, char *printed_name
, struct smatch_state
*state
)
1650 /* these are handled in smatch_param_filter/set/limit.c */
1651 if (printed_name
[0] != '*' &&
1652 !strchr(printed_name
, '.') && !strchr(printed_name
, '-'))
1655 math_str
= in_terms_of_param_math(expr
, printed_name
);
1657 sql_insert_return_states(return_id
, return_ranges
, RETURN_VALUE
, -1,
1658 printed_name
, math_str
);
1662 if (estate_is_whole(state
))
1665 /* these are handled in smatch_param_filter/set/limit.c */
1666 if (printed_name
[0] != '*' &&
1667 !strchr(printed_name
, '.') && !strchr(printed_name
, '-'))
1670 sql_insert_return_states(return_id
, return_ranges
, RETURN_VALUE
, -1,
1671 printed_name
, state
->name
);
1674 static void match_call_info(struct expression
*expr
)
1676 struct smatch_state
*state
;
1677 struct range_list
*rl
= NULL
;
1678 struct expression
*arg
;
1679 struct symbol
*type
;
1682 FOR_EACH_PTR(expr
->args
, arg
) {
1683 type
= get_arg_type(expr
->fn
, i
);
1685 if (get_implied_rl(arg
, &rl
))
1686 rl
= cast_rl(type
, rl
);
1688 rl
= cast_rl(type
, alloc_whole_rl(get_type(arg
)));
1690 if (!is_whole_rl(rl
))
1691 sql_insert_caller_info(expr
, PARAM_VALUE
, i
, "$$", show_rl(rl
));
1692 state
= get_state_expr(SMATCH_EXTRA
, arg
);
1693 if (estate_has_fuzzy_max(state
)) {
1694 sql_insert_caller_info(expr
, FUZZY_MAX
, i
, "$$",
1695 sval_to_str(estate_get_fuzzy_max(state
)));
1698 } END_FOR_EACH_PTR(arg
);
1701 static void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1703 struct range_list
*rl
= NULL
;
1704 struct smatch_state
*state
;
1705 struct symbol
*type
;
1708 if (strcmp(key
, "*$$") == 0)
1709 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
1710 else if (strncmp(key
, "$$", 2) == 0)
1711 snprintf(fullname
, 256, "%s%s", name
, key
+ 2);
1715 type
= get_member_type_from_key(symbol_expression(sym
), key
);
1716 str_to_rl(type
, value
, &rl
);
1717 state
= alloc_estate_rl(rl
);
1718 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
1721 static void set_param_hard_max(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1723 struct range_list
*rl
= NULL
;
1724 struct smatch_state
*state
;
1725 struct symbol
*type
;
1729 if (strcmp(key
, "*$$") == 0)
1730 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
1731 else if (strncmp(key
, "$$", 2) == 0)
1732 snprintf(fullname
, 256, "%s%s", name
, key
+ 2);
1736 state
= get_state(SMATCH_EXTRA
, fullname
, sym
);
1739 type
= get_member_type_from_key(symbol_expression(sym
), key
);
1740 str_to_rl(type
, value
, &rl
);
1741 if (!rl_to_sval(rl
, &max
))
1743 estate_set_fuzzy_max(state
, max
);
1746 void register_smatch_extra(int id
)
1750 add_merge_hook(my_id
, &merge_estates
);
1751 add_unmatched_state_hook(my_id
, &unmatched_state
);
1752 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
1753 add_hook(&match_declarations
, DECLARATION_HOOK
);
1754 select_caller_info_hook(set_param_value
, PARAM_VALUE
);
1755 select_caller_info_hook(set_param_hard_max
, FUZZY_MAX
);
1756 select_return_states_hook(RETURN_VALUE
, &db_returned_member_info
);
1757 select_return_states_before(&db_limited_before
);
1758 select_return_states_hook(LIMITED_VALUE
, &db_param_limit
);
1759 select_return_states_hook(FILTER_VALUE
, &db_param_filter
);
1760 select_return_states_hook(ADDED_VALUE
, &db_param_add
);
1761 select_return_states_after(&db_limited_after
);
1764 void register_smatch_extra_late(int id
)
1766 add_hook(&match_dereferences
, DEREF_HOOK
);
1767 add_hook(&match_pointer_as_array
, OP_HOOK
);
1768 select_call_implies_hook(DEREFERENCE
, &set_param_dereferenced
);
1769 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
1770 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
1771 add_hook(&match_assign
, GLOBAL_ASSIGNMENT_HOOK
);
1772 add_hook(&unop_expr
, OP_HOOK
);
1773 add_hook(&asm_expr
, ASM_HOOK
);
1775 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1776 add_member_info_callback(my_id
, struct_member_callback
);
1777 add_returned_member_callback(my_id
, returned_member_callback
);
1779 add_hook(&assume_indexes_are_valid
, OP_HOOK
);