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 if (__in_fake_assign
&& estate_is_unknown(state
) && !get_state(SMATCH_EXTRA
, name
, sym
))
82 return set_state(SMATCH_EXTRA
, name
, sym
, state
);
85 static char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
87 struct expression
*assigned
;
88 char *orig_name
= NULL
;
98 skip
= strlen(sym
->ident
->name
);
99 if (name
[skip
] != '-' || name
[skip
+ 1] != '>')
103 assigned
= get_assigned_expr_name_sym(sym
->ident
->name
, sym
);
106 if (assigned
->type
== EXPR_PREOP
|| assigned
->op
== '&') {
108 orig_name
= expr_to_var_sym(assigned
, new_sym
);
109 if (!orig_name
|| !*new_sym
)
112 snprintf(buf
, sizeof(buf
), "%s.%s", orig_name
+ 1, name
+ skip
);
113 ret
= alloc_string(buf
);
114 free_string(orig_name
);
118 if (assigned
->type
!= EXPR_DEREF
)
121 orig_name
= expr_to_var_sym(assigned
, new_sym
);
122 if (!orig_name
|| !*new_sym
)
125 snprintf(buf
, sizeof(buf
), "%s->%s", orig_name
, name
+ skip
);
126 ret
= alloc_string(buf
);
127 free_string(orig_name
);
131 free_string(orig_name
);
135 struct sm_state
*set_extra_mod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
138 struct symbol
*new_sym
;
141 if (in_warn_on_macro())
143 sm
= set_extra_mod_helper(name
, sym
, state
);
144 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
145 if (new_name
&& new_sym
)
146 set_extra_mod_helper(new_name
, new_sym
, state
);
147 free_string(new_name
);
151 struct sm_state
*set_extra_expr_mod(struct expression
*expr
, struct smatch_state
*state
)
155 struct sm_state
*ret
= NULL
;
157 expr
= strip_expr(expr
);
158 name
= expr_to_var_sym(expr
, &sym
);
161 ret
= set_extra_mod(name
, sym
, state
);
167 void set_extra_nomod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
170 struct symbol
*new_sym
;
171 struct relation
*rel
;
172 struct smatch_state
*orig_state
;
174 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
176 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
177 if (new_name
&& new_sym
)
178 set_state(SMATCH_EXTRA
, new_name
, new_sym
, state
);
179 free_string(new_name
);
181 if (!estate_related(orig_state
)) {
182 set_state(SMATCH_EXTRA
, name
, sym
, state
);
186 set_related(state
, estate_related(orig_state
));
187 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
188 if (option_debug_related
)
189 sm_msg("%s updating related %s to %s", name
, rel
->name
, state
->name
);
190 set_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
, state
);
191 } END_FOR_EACH_PTR(rel
);
195 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
197 void set_extra_expr_nomod(struct expression
*expr
, struct smatch_state
*state
)
202 name
= expr_to_var_sym(expr
, &sym
);
205 set_extra_nomod(name
, sym
, state
);
211 static void set_extra_true_false(const char *name
, struct symbol
*sym
,
212 struct smatch_state
*true_state
,
213 struct smatch_state
*false_state
)
216 struct symbol
*new_sym
;
217 struct relation
*rel
;
218 struct smatch_state
*orig_state
;
220 if (in_warn_on_macro())
223 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
224 if (new_name
&& new_sym
)
225 set_true_false_states(SMATCH_EXTRA
, new_name
, new_sym
, true_state
, false_state
);
226 free_string(new_name
);
228 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
230 if (!estate_related(orig_state
)) {
231 set_true_false_states(SMATCH_EXTRA
, name
, sym
, true_state
, false_state
);
236 set_related(true_state
, estate_related(orig_state
));
238 set_related(false_state
, estate_related(orig_state
));
240 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
241 set_true_false_states(SMATCH_EXTRA
, rel
->name
, rel
->sym
,
242 true_state
, false_state
);
243 } END_FOR_EACH_PTR(rel
);
246 static void set_extra_expr_true_false(struct expression
*expr
,
247 struct smatch_state
*true_state
,
248 struct smatch_state
*false_state
)
253 expr
= strip_expr(expr
);
254 name
= expr_to_var_sym(expr
, &sym
);
257 set_extra_true_false(name
, sym
, true_state
, false_state
);
262 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
264 struct expression
*iter_var
;
265 struct expression
*condition
;
267 struct smatch_state
*estate
;
270 condition
= strip_expr(loop
->iterator_pre_condition
);
273 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
275 if (condition
->op
!= SPECIAL_DECREMENT
)
278 iter_var
= condition
->unop
;
279 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
282 if (sval_cmp_val(estate_min(sm
->state
), 0) < 0)
284 start
= estate_max(sm
->state
);
285 if (sval_cmp_val(start
, 0) <= 0)
287 if (!sval_is_max(start
))
290 if (condition
->type
== EXPR_PREOP
) {
291 estate
= alloc_estate_range(sval_type_val(start
.type
, 1), start
);
292 if (estate_has_hard_max(sm
->state
))
293 estate_set_hard_max(estate
);
294 estate_copy_fuzzy_max(estate
, sm
->state
);
295 set_extra_expr_mod(iter_var
, estate
);
297 if (condition
->type
== EXPR_POSTOP
) {
298 estate
= alloc_estate_range(sval_type_val(start
.type
, 0), start
);
299 if (estate_has_hard_max(sm
->state
))
300 estate_set_hard_max(estate
);
301 estate_copy_fuzzy_max(estate
, sm
->state
);
302 set_extra_expr_mod(iter_var
, estate
);
304 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
307 static struct sm_state
*handle_canonical_for_inc(struct expression
*iter_expr
,
308 struct expression
*condition
)
310 struct expression
*iter_var
;
312 struct smatch_state
*estate
;
313 sval_t start
, end
, max
;
315 iter_var
= iter_expr
->unop
;
316 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
319 if (!estate_get_single_value(sm
->state
, &start
))
321 if (get_implied_max(condition
->right
, &end
))
322 end
= sval_cast(get_type(iter_var
), end
);
324 end
= sval_type_max(get_type(iter_var
));
326 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
329 switch (condition
->op
) {
330 case SPECIAL_UNSIGNED_LT
:
331 case SPECIAL_NOTEQUAL
:
333 if (!sval_is_min(end
))
336 case SPECIAL_UNSIGNED_LTE
:
342 if (sval_cmp(end
, start
) < 0)
344 estate
= alloc_estate_range(start
, end
);
345 if (get_hard_max(condition
->right
, &max
)) {
346 estate_set_hard_max(estate
);
347 if (condition
->op
== '<' ||
348 condition
->op
== SPECIAL_UNSIGNED_LT
||
349 condition
->op
== SPECIAL_NOTEQUAL
)
351 estate_set_fuzzy_max(estate
, max
);
353 set_extra_expr_mod(iter_var
, estate
);
354 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
357 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
358 struct expression
*condition
)
360 struct expression
*iter_var
;
362 struct smatch_state
*estate
;
365 iter_var
= iter_expr
->unop
;
366 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
369 if (!estate_get_single_value(sm
->state
, &start
))
371 if (!get_implied_min(condition
->right
, &end
))
372 end
= sval_type_min(get_type(iter_var
));
373 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
376 switch (condition
->op
) {
377 case SPECIAL_NOTEQUAL
:
379 if (!sval_is_min(end
) && !sval_is_max(end
))
387 if (sval_cmp(end
, start
) > 0)
389 estate
= alloc_estate_range(end
, start
);
390 estate_set_hard_max(estate
);
391 estate_set_fuzzy_max(estate
, estate_get_fuzzy_max(estate
));
392 set_extra_expr_mod(iter_var
, estate
);
393 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
396 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
398 struct expression
*iter_expr
;
399 struct expression
*condition
;
401 if (!loop
->iterator_post_statement
)
403 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
405 iter_expr
= loop
->iterator_post_statement
->expression
;
406 if (!loop
->iterator_pre_condition
)
408 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
410 condition
= loop
->iterator_pre_condition
;
412 if (iter_expr
->op
== SPECIAL_INCREMENT
)
413 return handle_canonical_for_inc(iter_expr
, condition
);
414 if (iter_expr
->op
== SPECIAL_DECREMENT
)
415 return handle_canonical_for_dec(iter_expr
, condition
);
419 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct stree
**stree
)
421 struct sm_state
*ret
;
423 __push_fake_cur_stree();
424 if (!loop
->iterator_post_statement
)
425 ret
= handle_canonical_while_count_down(loop
);
427 ret
= handle_canonical_for_loops(loop
);
428 *stree
= __pop_fake_cur_stree();
432 int __iterator_unchanged(struct sm_state
*sm
)
436 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
441 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
445 /* paranoid checking. prolly not needed */
446 condition
= strip_expr(condition
);
449 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
451 if (condition
->op
!= SPECIAL_DECREMENT
)
453 after_value
= estate_min(sm
->state
);
455 set_extra_mod(sm
->name
, sm
->sym
, alloc_estate_sval(after_value
));
458 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
459 struct statement
*iterator
,
460 struct expression
*condition
)
462 struct expression
*iter_expr
;
464 struct smatch_state
*state
;
467 while_count_down_after(sm
, condition
);
471 iter_expr
= iterator
->expression
;
473 if (condition
->type
!= EXPR_COMPARE
)
475 if (iter_expr
->op
== SPECIAL_INCREMENT
) {
476 limit
= sval_binop(estate_max(sm
->state
), '+',
477 sval_type_val(estate_type(sm
->state
), 1));
479 limit
= sval_binop(estate_min(sm
->state
), '-',
480 sval_type_val(estate_type(sm
->state
), 1));
482 if (!estate_has_hard_max(sm
->state
) && !__has_breaks()) {
483 if (iter_expr
->op
== SPECIAL_INCREMENT
)
484 state
= alloc_estate_range(estate_min(sm
->state
), limit
);
486 state
= alloc_estate_range(limit
, estate_max(sm
->state
));
488 state
= alloc_estate_sval(limit
);
490 if (!estate_has_hard_max(sm
->state
)) {
491 estate_clear_hard_max(state
);
493 if (estate_has_fuzzy_max(sm
->state
)) {
494 sval_t hmax
= estate_get_fuzzy_max(sm
->state
);
495 sval_t max
= estate_max(sm
->state
);
497 if (sval_cmp(hmax
, max
) != 0)
498 estate_clear_fuzzy_max(state
);
499 } else if (!estate_has_fuzzy_max(sm
->state
)) {
500 estate_clear_fuzzy_max(state
);
503 set_extra_mod(sm
->name
, sm
->sym
, state
);
506 static struct stree
*unmatched_stree
;
507 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
509 struct smatch_state
*state
;
511 if (unmatched_stree
) {
512 state
= get_state_stree(unmatched_stree
, SMATCH_EXTRA
, sm
->name
, sm
->sym
);
516 if (parent_is_gone_var_sym(sm
->name
, sm
->sym
))
517 return alloc_estate_empty();
518 return alloc_estate_whole(estate_type(sm
->state
));
521 static void clear_the_pointed_at(struct expression
*expr
)
526 struct sm_state
*tmp
;
528 name
= expr_to_var_sym(expr
, &sym
);
532 stree
= __get_cur_stree();
533 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
534 if (tmp
->name
[0] != '*')
538 if (strcmp(tmp
->name
+ 1, name
) != 0)
540 set_extra_mod(tmp
->name
, tmp
->sym
, alloc_estate_whole(estate_type(tmp
->state
)));
541 } END_FOR_EACH_SM(tmp
);
547 static void match_function_call(struct expression
*expr
)
549 struct expression
*arg
;
550 struct expression
*tmp
;
552 /* if we have the db this is handled in smatch_function_hooks.c */
555 if (inlinable(expr
->fn
))
558 FOR_EACH_PTR(expr
->args
, arg
) {
559 tmp
= strip_expr(arg
);
560 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
561 set_extra_expr_mod(tmp
->unop
, alloc_estate_whole(get_type(tmp
->unop
)));
563 clear_the_pointed_at(tmp
);
564 } END_FOR_EACH_PTR(arg
);
567 static int types_equiv_or_pointer(struct symbol
*one
, struct symbol
*two
)
571 if (one
->type
== SYM_PTR
&& two
->type
== SYM_PTR
)
573 return types_equiv(one
, two
);
576 static void match_vanilla_assign(struct expression
*left
, struct expression
*right
)
578 struct range_list
*rl
= NULL
;
579 struct symbol
*right_sym
;
580 struct symbol
*left_type
;
581 struct symbol
*right_type
;
582 char *right_name
= NULL
;
586 struct smatch_state
*state
;
591 name
= expr_to_var_sym(left
, &sym
);
595 left_type
= get_type(left
);
596 right_type
= get_type(right
);
598 right_name
= expr_to_var_sym(right
, &right_sym
);
600 if (!__in_fake_assign
&&
601 !(right
->type
== EXPR_PREOP
&& right
->op
== '&') &&
602 right_name
&& right_sym
&&
603 types_equiv_or_pointer(left_type
, right_type
) &&
604 !has_symbol(right
, sym
)) {
605 set_equiv(left
, right
);
609 if (is_pointer(right
) && get_address_rl(right
, &rl
)) {
610 state
= alloc_estate_rl(rl
);
611 } else if (get_implied_rl(right
, &rl
)) {
612 rl
= cast_rl(left_type
, rl
);
613 state
= alloc_estate_rl(rl
);
614 if (get_hard_max(right
, &max
)) {
615 estate_set_hard_max(state
);
616 estate_set_fuzzy_max(state
, max
);
619 rl
= alloc_whole_rl(right_type
);
620 rl
= cast_rl(left_type
, rl
);
621 state
= alloc_estate_rl(rl
);
623 set_extra_mod(name
, sym
, state
);
625 free_string(right_name
);
628 static int op_remove_assign(int op
)
631 case SPECIAL_ADD_ASSIGN
:
633 case SPECIAL_SUB_ASSIGN
:
635 case SPECIAL_MUL_ASSIGN
:
637 case SPECIAL_DIV_ASSIGN
:
639 case SPECIAL_MOD_ASSIGN
:
641 case SPECIAL_AND_ASSIGN
:
643 case SPECIAL_OR_ASSIGN
:
645 case SPECIAL_XOR_ASSIGN
:
647 case SPECIAL_SHL_ASSIGN
:
648 return SPECIAL_LEFTSHIFT
;
649 case SPECIAL_SHR_ASSIGN
:
650 return SPECIAL_RIGHTSHIFT
;
656 static void match_assign(struct expression
*expr
)
658 struct range_list
*rl
= NULL
;
659 struct expression
*left
;
660 struct expression
*right
;
661 struct expression
*binop_expr
;
662 struct symbol
*left_type
;
663 struct symbol
*right_type
;
666 sval_t left_min
, left_max
;
667 sval_t right_min
, right_max
;
668 sval_t res_min
, res_max
;
670 left
= strip_expr(expr
->left
);
672 right
= strip_expr(expr
->right
);
673 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
674 right
= strip_expr(right
->left
);
676 if (expr
->op
== '=' && is_condition(expr
->right
))
677 return; /* handled in smatch_condition.c */
678 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
679 return; /* handled in smatch_function_hooks.c */
680 if (expr
->op
== '=') {
681 match_vanilla_assign(left
, right
);
685 name
= expr_to_var_sym(left
, &sym
);
689 left_type
= get_type(left
);
690 right_type
= get_type(right
);
692 res_min
= sval_type_min(left_type
);
693 res_max
= sval_type_max(left_type
);
696 case SPECIAL_ADD_ASSIGN
:
697 get_absolute_max(left
, &left_max
);
698 get_absolute_max(right
, &right_max
);
699 if (sval_binop_overflows(left_max
, '+', right_max
))
701 if (get_implied_min(left
, &left_min
) &&
702 !sval_is_negative_min(left_min
) &&
703 get_implied_min(right
, &right_min
) &&
704 !sval_is_negative_min(right_min
)) {
705 res_min
= sval_binop(left_min
, '+', right_min
);
706 res_min
= sval_cast(right_type
, res_min
);
708 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
710 res_max
= sval_binop(left_max
, '+', right_max
);
711 res_max
= sval_cast(right_type
, res_max
);
713 case SPECIAL_SUB_ASSIGN
:
714 if (get_implied_max(left
, &left_max
) &&
715 !sval_is_max(left_max
) &&
716 get_implied_min(right
, &right_min
) &&
717 !sval_is_min(right_min
)) {
718 res_max
= sval_binop(left_max
, '-', right_min
);
719 res_max
= sval_cast(right_type
, res_max
);
723 if (get_implied_min(left
, &left_min
) &&
724 !sval_is_min(left_min
) &&
725 get_implied_max(right
, &right_max
) &&
726 !sval_is_max(right_max
)) {
727 res_min
= sval_binop(left_min
, '-', right_max
);
728 res_min
= sval_cast(right_type
, res_min
);
731 case SPECIAL_AND_ASSIGN
:
732 case SPECIAL_MOD_ASSIGN
:
733 case SPECIAL_SHL_ASSIGN
:
734 case SPECIAL_SHR_ASSIGN
:
735 case SPECIAL_OR_ASSIGN
:
736 case SPECIAL_XOR_ASSIGN
:
737 case SPECIAL_MUL_ASSIGN
:
738 case SPECIAL_DIV_ASSIGN
:
739 binop_expr
= binop_expression(expr
->left
,
740 op_remove_assign(expr
->op
),
742 if (get_absolute_rl(binop_expr
, &rl
)) {
743 rl
= cast_rl(left_type
, rl
);
744 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
749 rl
= cast_rl(left_type
, alloc_rl(res_min
, res_max
));
750 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
755 static struct smatch_state
*increment_state(struct smatch_state
*state
)
757 sval_t min
= estate_min(state
);
758 sval_t max
= estate_max(state
);
760 if (!estate_rl(state
))
764 max
= sval_type_max(max
.type
);
766 if (!sval_is_min(min
) && !sval_is_max(min
))
768 if (!sval_is_min(max
) && !sval_is_max(max
))
770 return alloc_estate_range(min
, max
);
773 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
775 sval_t min
= estate_min(state
);
776 sval_t max
= estate_max(state
);
778 if (!estate_rl(state
))
782 min
= sval_type_min(min
.type
);
784 if (!sval_is_min(min
) && !sval_is_max(min
))
786 if (!sval_is_min(max
) && !sval_is_max(max
))
788 return alloc_estate_range(min
, max
);
791 static void unop_expr(struct expression
*expr
)
793 struct smatch_state
*state
;
795 if (expr
->smatch_flags
& Handled
)
799 case SPECIAL_INCREMENT
:
800 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
801 state
= increment_state(state
);
803 state
= alloc_estate_whole(get_type(expr
));
804 set_extra_expr_mod(expr
->unop
, state
);
806 case SPECIAL_DECREMENT
:
807 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
808 state
= decrement_state(state
);
810 state
= alloc_estate_whole(get_type(expr
));
811 set_extra_expr_mod(expr
->unop
, state
);
818 static void asm_expr(struct statement
*stmt
)
821 struct expression
*expr
;
825 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
827 case 0: /* identifier */
828 case 1: /* constraint */
831 case 2: /* expression */
833 type
= get_type(strip_expr(expr
));
834 set_extra_expr_mod(expr
, alloc_estate_whole(type
));
837 } END_FOR_EACH_PTR(expr
);
840 static void check_dereference(struct expression
*expr
)
842 if (outside_of_function())
844 set_extra_expr_nomod(expr
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
847 static void match_dereferences(struct expression
*expr
)
849 if (expr
->type
!= EXPR_PREOP
)
851 check_dereference(expr
->unop
);
854 static void match_pointer_as_array(struct expression
*expr
)
858 check_dereference(get_array_base(expr
));
861 static void set_param_dereferenced(struct expression
*arg
, char *key
, char *unused
)
866 name
= get_variable_from_key(arg
, key
, &sym
);
870 set_extra_nomod(name
, sym
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
876 static sval_t
add_one(sval_t sval
)
882 static sval_t
sub_one(sval_t sval
)
888 static void handle_comparison(struct symbol
*type
, struct expression
*left
, int op
, struct expression
*right
)
890 struct range_list
*left_orig
;
891 struct range_list
*left_true
;
892 struct range_list
*left_false
;
893 struct range_list
*right_orig
;
894 struct range_list
*right_true
;
895 struct range_list
*right_false
;
896 struct smatch_state
*left_true_state
;
897 struct smatch_state
*left_false_state
;
898 struct smatch_state
*right_true_state
;
899 struct smatch_state
*right_false_state
;
900 sval_t min
, max
, dummy
, hard_max
;
902 int right_postop
= 0;
904 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
905 if (left
->type
== EXPR_POSTOP
) {
906 left
->smatch_flags
|= Handled
;
907 left_postop
= left
->op
;
909 left
= strip_expr(left
->unop
);
911 while (left
->type
== EXPR_ASSIGNMENT
)
912 left
= strip_expr(left
->left
);
914 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
915 if (right
->type
== EXPR_POSTOP
) {
916 right
->smatch_flags
|= Handled
;
917 right_postop
= right
->op
;
919 right
= strip_expr(right
->unop
);
922 if (get_implied_rl(left
, &left_orig
)) {
923 left_orig
= cast_rl(type
, left_orig
);
925 min
= sval_type_min(get_type(left
));
926 max
= sval_type_max(get_type(left
));
927 left_orig
= cast_rl(type
, alloc_rl(min
, max
));
930 if (get_implied_rl(right
, &right_orig
)) {
931 right_orig
= cast_rl(type
, right_orig
);
933 min
= sval_type_min(get_type(right
));
934 max
= sval_type_max(get_type(right
));
935 right_orig
= cast_rl(type
, alloc_rl(min
, max
));
937 min
= sval_type_min(type
);
938 max
= sval_type_max(type
);
940 left_true
= clone_rl(left_orig
);
941 left_false
= clone_rl(left_orig
);
942 right_true
= clone_rl(right_orig
);
943 right_false
= clone_rl(right_orig
);
947 case SPECIAL_UNSIGNED_LT
:
948 left_true
= remove_range(left_orig
, rl_max(right_orig
), max
);
949 if (!sval_is_min(rl_min(right_orig
))) {
950 left_false
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
953 right_true
= remove_range(right_orig
, min
, rl_min(left_orig
));
954 if (!sval_is_max(rl_max(left_orig
)))
955 right_false
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
957 case SPECIAL_UNSIGNED_LTE
:
959 if (!sval_is_max(rl_max(right_orig
)))
960 left_true
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
961 left_false
= remove_range(left_orig
, min
, rl_min(right_orig
));
963 if (!sval_is_min(rl_min(left_orig
)))
964 right_true
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
965 right_false
= remove_range(right_orig
, rl_max(left_orig
), max
);
967 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
968 left_false
= remove_range(left_false
, rl_min(left_orig
), rl_min(left_orig
));
969 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
970 right_false
= remove_range(right_false
, rl_max(left_orig
), rl_max(left_orig
));
973 if (!sval_is_max(rl_max(right_orig
))) {
974 left_true
= remove_range(left_true
, add_one(rl_max(right_orig
)), max
);
976 if (!sval_is_min(rl_min(right_orig
))) {
977 left_true
= remove_range(left_true
, min
, sub_one(rl_min(right_orig
)));
979 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
980 left_false
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
982 if (!sval_is_max(rl_max(left_orig
)))
983 right_true
= remove_range(right_true
, add_one(rl_max(left_orig
)), max
);
984 if (!sval_is_min(rl_min(left_orig
)))
985 right_true
= remove_range(right_true
, min
, sub_one(rl_min(left_orig
)));
986 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
987 right_false
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
989 case SPECIAL_UNSIGNED_GTE
:
991 if (!sval_is_min(rl_min(right_orig
)))
992 left_true
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
993 left_false
= remove_range(left_orig
, rl_max(right_orig
), max
);
995 if (!sval_is_max(rl_max(left_orig
)))
996 right_true
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
997 right_false
= remove_range(right_orig
, min
, rl_min(left_orig
));
999 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
1000 right_false
= remove_range(right_false
, rl_min(left_orig
), rl_min(left_orig
));
1001 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
1002 left_false
= remove_range(left_false
, rl_max(left_orig
), rl_max(left_orig
));
1005 case SPECIAL_UNSIGNED_GT
:
1006 left_true
= remove_range(left_orig
, min
, rl_min(right_orig
));
1007 if (!sval_is_max(rl_max(right_orig
)))
1008 left_false
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
1010 right_true
= remove_range(right_orig
, rl_max(left_orig
), max
);
1011 if (!sval_is_min(rl_min(left_orig
)))
1012 right_false
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
1014 case SPECIAL_NOTEQUAL
:
1015 if (!sval_is_max(rl_max(right_orig
)))
1016 left_false
= remove_range(left_false
, add_one(rl_max(right_orig
)), max
);
1017 if (!sval_is_min(rl_min(right_orig
)))
1018 left_false
= remove_range(left_false
, min
, sub_one(rl_min(right_orig
)));
1019 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
1020 left_true
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
1022 if (!sval_is_max(rl_max(left_orig
)))
1023 right_false
= remove_range(right_false
, add_one(rl_max(left_orig
)), max
);
1024 if (!sval_is_min(rl_min(left_orig
)))
1025 right_false
= remove_range(right_false
, min
, sub_one(rl_min(left_orig
)));
1026 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
1027 right_true
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
1033 left_true
= rl_truncate_cast(get_type(left
), left_true
);
1034 left_false
= rl_truncate_cast(get_type(left
), left_false
);
1035 right_true
= rl_truncate_cast(get_type(right
), right_true
);
1036 right_false
= rl_truncate_cast(get_type(right
), right_false
);
1038 left_true_state
= alloc_estate_rl(left_true
);
1039 left_false_state
= alloc_estate_rl(left_false
);
1040 right_true_state
= alloc_estate_rl(right_true
);
1041 right_false_state
= alloc_estate_rl(right_false
);
1045 case SPECIAL_UNSIGNED_LT
:
1046 case SPECIAL_UNSIGNED_LTE
:
1048 if (get_hard_max(right
, &dummy
))
1049 estate_set_hard_max(left_true_state
);
1050 if (get_hard_max(left
, &dummy
))
1051 estate_set_hard_max(right_false_state
);
1054 case SPECIAL_UNSIGNED_GT
:
1055 case SPECIAL_UNSIGNED_GTE
:
1057 if (get_hard_max(left
, &dummy
))
1058 estate_set_hard_max(right_true_state
);
1059 if (get_hard_max(right
, &dummy
))
1060 estate_set_hard_max(left_false_state
);
1066 case SPECIAL_UNSIGNED_LT
:
1067 case SPECIAL_UNSIGNED_LTE
:
1069 if (get_hard_max(right
, &hard_max
)) {
1070 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1072 estate_set_fuzzy_max(left_true_state
, hard_max
);
1074 if (get_implied_value(right
, &hard_max
)) {
1075 if (op
== SPECIAL_UNSIGNED_LTE
||
1078 estate_set_fuzzy_max(left_false_state
, hard_max
);
1080 if (get_hard_max(left
, &hard_max
)) {
1081 if (op
== SPECIAL_UNSIGNED_LTE
||
1084 estate_set_fuzzy_max(right_false_state
, hard_max
);
1086 if (get_implied_value(left
, &hard_max
)) {
1087 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1089 estate_set_fuzzy_max(right_true_state
, hard_max
);
1093 case SPECIAL_UNSIGNED_GT
:
1094 case SPECIAL_UNSIGNED_GTE
:
1096 if (get_hard_max(left
, &hard_max
)) {
1097 if (op
== '>' || op
== SPECIAL_UNSIGNED_GT
)
1099 estate_set_fuzzy_max(right_true_state
, hard_max
);
1101 if (get_implied_value(left
, &hard_max
)) {
1102 if (op
== SPECIAL_UNSIGNED_GTE
||
1105 estate_set_fuzzy_max(right_false_state
, hard_max
);
1107 if (get_hard_max(right
, &hard_max
)) {
1108 if (op
== SPECIAL_UNSIGNED_LTE
||
1111 estate_set_fuzzy_max(left_false_state
, hard_max
);
1113 if (get_implied_value(right
, &hard_max
)) {
1115 op
== SPECIAL_UNSIGNED_GT
)
1117 estate_set_fuzzy_max(left_true_state
, hard_max
);
1121 if (get_hard_max(left
, &hard_max
))
1122 estate_set_fuzzy_max(right_true_state
, hard_max
);
1123 if (get_hard_max(right
, &hard_max
))
1124 estate_set_fuzzy_max(left_true_state
, hard_max
);
1128 if (get_hard_max(left
, &hard_max
)) {
1129 estate_set_hard_max(left_true_state
);
1130 estate_set_hard_max(left_false_state
);
1132 if (get_hard_max(right
, &hard_max
)) {
1133 estate_set_hard_max(right_true_state
);
1134 estate_set_hard_max(right_false_state
);
1137 if (left_postop
== SPECIAL_INCREMENT
) {
1138 left_true_state
= increment_state(left_true_state
);
1139 left_false_state
= increment_state(left_false_state
);
1141 if (left_postop
== SPECIAL_DECREMENT
) {
1142 left_true_state
= decrement_state(left_true_state
);
1143 left_false_state
= decrement_state(left_false_state
);
1145 if (right_postop
== SPECIAL_INCREMENT
) {
1146 right_true_state
= increment_state(right_true_state
);
1147 right_false_state
= increment_state(right_false_state
);
1149 if (right_postop
== SPECIAL_DECREMENT
) {
1150 right_true_state
= decrement_state(right_true_state
);
1151 right_false_state
= decrement_state(right_false_state
);
1154 if (estate_rl(left_true_state
) && estates_equiv(left_true_state
, left_false_state
)) {
1155 left_true_state
= NULL
;
1156 left_false_state
= NULL
;
1159 if (estate_rl(right_true_state
) && estates_equiv(right_true_state
, right_false_state
)) {
1160 right_true_state
= NULL
;
1161 right_false_state
= NULL
;
1164 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
1165 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
1168 static int is_simple_math(struct expression
*expr
)
1172 if (expr
->type
!= EXPR_BINOP
)
1183 static void move_known_values(struct expression
**left_p
, struct expression
**right_p
)
1185 struct expression
*left
= *left_p
;
1186 struct expression
*right
= *right_p
;
1189 if (get_implied_value(left
, &sval
)) {
1190 if (!is_simple_math(right
))
1192 if (right
->op
== '*') {
1195 if (!get_value(right
->right
, &divisor
))
1197 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1199 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1200 *right_p
= right
->left
;
1203 if (right
->op
== '+' && get_value(right
->left
, &sval
)) {
1204 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->left
);
1205 *right_p
= right
->right
;
1208 if (get_value(right
->right
, &sval
)) {
1209 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1210 *right_p
= right
->left
;
1215 if (get_implied_value(right
, &sval
)) {
1216 if (!is_simple_math(left
))
1218 if (left
->op
== '*') {
1221 if (!get_value(left
->right
, &divisor
))
1223 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1225 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1226 *left_p
= left
->left
;
1229 if (left
->op
== '+' && get_value(left
->left
, &sval
)) {
1230 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->left
);
1231 *left_p
= left
->right
;
1235 if (get_value(left
->right
, &sval
)) {
1236 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1237 *left_p
= left
->left
;
1244 static int match_func_comparison(struct expression
*expr
)
1246 struct expression
*left
= strip_expr(expr
->left
);
1247 struct expression
*right
= strip_expr(expr
->right
);
1251 * fixme: think about this harder. We should always be trying to limit
1252 * the non-call side as well. If we can't determine the limitter does
1253 * that mean we aren't querying the database and are missing important
1257 if (left
->type
== EXPR_CALL
) {
1258 if (get_implied_value(left
, &sval
)) {
1259 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1262 function_comparison(left
, expr
->op
, right
);
1266 if (right
->type
== EXPR_CALL
) {
1267 if (get_implied_value(right
, &sval
)) {
1268 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1271 function_comparison(left
, expr
->op
, right
);
1278 static void match_comparison(struct expression
*expr
)
1280 struct expression
*left_orig
= strip_expr(expr
->left
);
1281 struct expression
*right_orig
= strip_expr(expr
->right
);
1282 struct expression
*left
, *right
;
1283 struct expression
*prev
;
1284 struct symbol
*type
;
1286 if (match_func_comparison(expr
))
1289 type
= get_type(expr
);
1291 type
= &llong_ctype
;
1295 move_known_values(&left
, &right
);
1296 handle_comparison(type
, left
, expr
->op
, right
);
1298 prev
= get_assigned_expr(left_orig
);
1299 if (is_simple_math(prev
) && has_variable(prev
, left_orig
) == 0) {
1302 move_known_values(&left
, &right
);
1303 handle_comparison(type
, left
, expr
->op
, right
);
1306 prev
= get_assigned_expr(right_orig
);
1307 if (is_simple_math(prev
) && has_variable(prev
, right_orig
) == 0) {
1310 move_known_values(&left
, &right
);
1311 handle_comparison(type
, left
, expr
->op
, right
);
1315 static void handle_AND_condition(struct expression
*expr
)
1317 struct range_list
*rl
= NULL
;
1320 if (get_implied_value(expr
->left
, &known
) && known
.value
> 0) {
1322 get_absolute_rl(expr
->right
, &rl
);
1323 rl
= remove_range(rl
, sval_type_val(known
.type
, 0), known
);
1324 set_extra_expr_true_false(expr
->right
, alloc_estate_rl(rl
), NULL
);
1328 if (get_implied_value(expr
->right
, &known
) && known
.value
> 0) {
1330 get_absolute_rl(expr
->left
, &rl
);
1331 rl
= remove_range(rl
, sval_type_val(known
.type
, 0), known
);
1332 set_extra_expr_true_false(expr
->left
, alloc_estate_rl(rl
), NULL
);
1337 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1338 void __extra_match_condition(struct expression
*expr
)
1342 struct smatch_state
*pre_state
;
1343 struct smatch_state
*true_state
;
1344 struct smatch_state
*false_state
;
1346 expr
= strip_expr(expr
);
1347 switch (expr
->type
) {
1349 function_comparison(expr
, SPECIAL_NOTEQUAL
, zero_expr());
1356 zero
= sval_blank(expr
);
1359 name
= expr_to_var_sym(expr
, &sym
);
1362 pre_state
= get_state(my_id
, name
, sym
);
1363 true_state
= estate_filter_sval(pre_state
, zero
);
1364 if (possibly_true(expr
, SPECIAL_EQUAL
, zero_expr()))
1365 false_state
= alloc_estate_sval(zero
);
1367 false_state
= alloc_estate_empty();
1368 set_extra_true_false(name
, sym
, true_state
, false_state
);
1373 match_comparison(expr
);
1375 case EXPR_ASSIGNMENT
:
1376 __extra_match_condition(expr
->left
);
1379 if (expr
->op
== '&')
1380 handle_AND_condition(expr
);
1385 static void assume_indexes_are_valid(struct expression
*expr
)
1387 struct expression
*array_expr
;
1389 struct expression
*offset
;
1390 struct symbol
*offset_type
;
1391 struct range_list
*rl_before
;
1392 struct range_list
*rl_after
;
1393 struct range_list
*filter
= NULL
;
1396 expr
= strip_expr(expr
);
1397 if (!is_array(expr
))
1400 offset
= get_array_offset(expr
);
1401 offset_type
= get_type(offset
);
1402 if (offset_type
&& type_signed(offset_type
)) {
1403 filter
= alloc_rl(sval_type_min(offset_type
),
1404 sval_type_val(offset_type
, -1));
1407 array_expr
= get_array_base(expr
);
1408 array_size
= get_real_array_size(array_expr
);
1409 if (array_size
> 1) {
1410 size
= sval_type_val(offset_type
, array_size
);
1411 add_range(&filter
, size
, sval_type_max(offset_type
));
1416 get_absolute_rl(offset
, &rl_before
);
1417 rl_after
= rl_filter(rl_before
, filter
);
1418 if (rl_equiv(rl_before
, rl_after
))
1420 set_extra_expr_nomod(offset
, alloc_estate_rl(rl_after
));
1423 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1424 int implied_not_equal(struct expression
*expr
, long long val
)
1426 return !possibly_false(expr
, SPECIAL_NOTEQUAL
, value_expr(val
));
1429 int implied_not_equal_name_sym(char *name
, struct symbol
*sym
, long long val
)
1431 struct smatch_state
*estate
;
1433 estate
= get_state(SMATCH_EXTRA
, name
, sym
);
1436 if (!rl_has_sval(estate_rl(estate
), sval_type_val(estate_type(estate
), 0)))
1441 int parent_is_null_var_sym(const char *name
, struct symbol
*sym
)
1446 struct smatch_state
*state
;
1448 strncpy(buf
, name
, sizeof(buf
) - 1);
1449 buf
[sizeof(buf
) - 1] = '\0';
1452 while (*start
== '*') {
1454 state
= get_state(SMATCH_EXTRA
, start
, sym
);
1457 if (!estate_rl(state
))
1459 if (estate_min(state
).value
== 0 &&
1460 estate_max(state
).value
== 0)
1465 while (*start
== '&')
1468 while ((end
= strrchr(start
, '-'))) {
1470 state
= get_state(SMATCH_EXTRA
, start
, sym
);
1473 if (estate_min(state
).value
== 0 &&
1474 estate_max(state
).value
== 0)
1480 int parent_is_null(struct expression
*expr
)
1486 expr
= strip_expr(expr
);
1487 var
= expr_to_var_sym(expr
, &sym
);
1490 ret
= parent_is_null_var_sym(var
, sym
);
1496 static int param_used_callback(void *found
, int argc
, char **argv
, char **azColName
)
1502 static int filter_unused_kzalloc_info(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
1507 /* for function pointers assume everything is used */
1508 if (call
->fn
->type
!= EXPR_SYMBOL
)
1512 * kzalloc() information is treated as special because so there is just
1513 * a lot of stuff initialized to zero and it makes building the database
1514 * take hours and hours.
1516 * In theory, we should just remove this line and not pass any unused
1517 * information, but I'm not sure enough that this code works so I want
1518 * to hold off on that for now.
1520 if (!estate_get_single_value(sm
->state
, &sval
) || sval
.value
!= 0)
1523 run_sql(¶m_used_callback
, &found
,
1524 "select * from call_implies where %s and type = %d and parameter = %d and key = '%s';",
1525 get_static_filter(call
->fn
->symbol
), PARAM_USED
, param
, printed_name
);
1529 /* If the database is not built yet, then assume everything is used */
1530 run_sql(¶m_used_callback
, &found
,
1531 "select * from call_implies where %s and type = %d;",
1532 get_static_filter(call
->fn
->symbol
), PARAM_USED
);
1539 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
1541 if (estate_is_whole(sm
->state
))
1543 if (filter_unused_kzalloc_info(call
, param
, printed_name
, sm
))
1545 sql_insert_caller_info(call
, PARAM_VALUE
, param
, printed_name
, sm
->state
->name
);
1546 if (estate_has_fuzzy_max(sm
->state
))
1547 sql_insert_caller_info(call
, FUZZY_MAX
, param
, printed_name
,
1548 sval_to_str(estate_get_fuzzy_max(sm
->state
)));
1551 static void db_limited_before(void)
1553 unmatched_stree
= clone_stree(__get_cur_stree());
1556 static void db_limited_after(void)
1558 free_stree(&unmatched_stree
);
1561 static void db_param_limit_filter(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
1563 struct expression
*arg
;
1566 struct sm_state
*sm
;
1567 struct symbol
*compare_type
, *var_type
;
1568 struct range_list
*rl
;
1569 struct range_list
*limit
;
1570 struct range_list
*new;
1572 while (expr
->type
== EXPR_ASSIGNMENT
)
1573 expr
= strip_expr(expr
->right
);
1574 if (expr
->type
!= EXPR_CALL
)
1577 arg
= get_argument_from_call_expr(expr
->args
, param
);
1581 name
= get_variable_from_key(arg
, key
, &sym
);
1585 if (strcmp(key
, "$") == 0)
1586 compare_type
= get_arg_type(expr
->fn
, param
);
1588 compare_type
= get_member_type_from_key(arg
, key
);
1590 sm
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
1592 rl
= estate_rl(sm
->state
);
1594 rl
= alloc_whole_rl(compare_type
);
1596 call_results_to_rl(expr
, compare_type
, value
, &limit
);
1597 new = rl_intersection(rl
, limit
);
1599 var_type
= get_member_type_from_key(arg
, key
);
1600 new = cast_rl(var_type
, new);
1602 /* We want to preserve the implications here */
1603 if (sm
&& rl_equiv(estate_rl(sm
->state
), new))
1606 if (op
== PARAM_LIMIT
)
1607 set_extra_nomod(name
, sym
, alloc_estate_rl(new));
1609 set_extra_mod(name
, sym
, alloc_estate_rl(new));
1616 static void db_param_limit(struct expression
*expr
, int param
, char *key
, char *value
)
1618 db_param_limit_filter(expr
, param
, key
, value
, PARAM_LIMIT
);
1621 static void db_param_filter(struct expression
*expr
, int param
, char *key
, char *value
)
1623 db_param_limit_filter(expr
, param
, key
, value
, PARAM_FILTER
);
1626 static void db_param_add_set(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
1628 struct expression
*arg
;
1631 struct symbol
*type
;
1632 struct smatch_state
*state
;
1633 struct range_list
*new = NULL
;
1634 struct range_list
*added
= NULL
;
1636 while (expr
->type
== EXPR_ASSIGNMENT
)
1637 expr
= strip_expr(expr
->right
);
1638 if (expr
->type
!= EXPR_CALL
)
1641 arg
= get_argument_from_call_expr(expr
->args
, param
);
1644 type
= get_member_type_from_key(arg
, key
);
1645 name
= get_variable_from_key(arg
, key
, &sym
);
1649 state
= get_state(SMATCH_EXTRA
, name
, sym
);
1651 new = estate_rl(state
);
1653 call_results_to_rl(expr
, type
, value
, &added
);
1655 if (op
== PARAM_SET
)
1658 new = rl_union(new, added
);
1660 set_extra_mod(name
, sym
, alloc_estate_rl(new));
1665 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
1667 db_param_add_set(expr
, param
, key
, value
, PARAM_ADD
);
1670 static void db_param_set(struct expression
*expr
, int param
, char *key
, char *value
)
1672 db_param_add_set(expr
, param
, key
, value
, PARAM_SET
);
1675 static void db_returned_member_info(struct expression
*expr
, int param
, char *key
, char *value
)
1677 struct symbol
*type
;
1680 struct range_list
*rl
;
1682 if (expr
->type
!= EXPR_ASSIGNMENT
)
1685 name
= return_state_to_var_sym(expr
, param
, key
, &sym
);
1688 type
= get_member_type_from_key(expr
->left
, key
);
1691 call_results_to_rl(expr
->right
, type
, value
, &rl
);
1692 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
1698 static char *in_terms_of_param_math(struct expression
*expr
, char *printed_name
)
1705 var
= expr_to_var_sym(expr
, &sym
);
1708 snprintf(buf
, sizeof(buf
), "%s%s", var
, printed_name
+ 2);
1710 ret
= get_value_in_terms_of_parameter_math_var_sym(buf
, sym
);
1717 static void returned_member_callback(int return_id
, char *return_ranges
, struct expression
*expr
, char *printed_name
, struct smatch_state
*state
)
1723 /* these are handled in smatch_param_filter/set/limit.c */
1724 if (printed_name
[0] != '*' &&
1725 !strchr(printed_name
, '.') && !strchr(printed_name
, '-'))
1728 if (estate_get_single_value(state
, &sval
)) {
1729 sql_insert_return_states(return_id
, return_ranges
, RETURN_VALUE
, -1,
1730 printed_name
, sval_to_str(sval
));
1734 math_str
= in_terms_of_param_math(expr
, printed_name
);
1736 snprintf(buf
, sizeof(buf
), "%s[%s]", state
->name
, math_str
);
1737 sql_insert_return_states(return_id
, return_ranges
, RETURN_VALUE
, -1,
1742 if (estate_is_whole(state
))
1745 sql_insert_return_states(return_id
, return_ranges
, RETURN_VALUE
, -1,
1746 printed_name
, state
->name
);
1749 static void match_call_info(struct expression
*expr
)
1751 struct smatch_state
*state
;
1752 struct range_list
*rl
= NULL
;
1753 struct expression
*arg
;
1754 struct symbol
*type
;
1757 FOR_EACH_PTR(expr
->args
, arg
) {
1758 type
= get_arg_type(expr
->fn
, i
);
1760 if (get_implied_rl(arg
, &rl
))
1761 rl
= cast_rl(type
, rl
);
1763 rl
= cast_rl(type
, alloc_whole_rl(get_type(arg
)));
1765 if (!is_whole_rl(rl
))
1766 sql_insert_caller_info(expr
, PARAM_VALUE
, i
, "$", show_rl(rl
));
1767 state
= get_state_expr(SMATCH_EXTRA
, arg
);
1768 if (estate_has_fuzzy_max(state
)) {
1769 sql_insert_caller_info(expr
, FUZZY_MAX
, i
, "$",
1770 sval_to_str(estate_get_fuzzy_max(state
)));
1773 } END_FOR_EACH_PTR(arg
);
1776 static void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1778 struct range_list
*rl
= NULL
;
1779 struct smatch_state
*state
;
1780 struct symbol
*type
;
1783 if (strcmp(key
, "*$") == 0)
1784 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
1785 else if (strncmp(key
, "$", 1) == 0)
1786 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
1790 type
= get_member_type_from_key(symbol_expression(sym
), key
);
1791 str_to_rl(type
, value
, &rl
);
1792 state
= alloc_estate_rl(rl
);
1793 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
1796 static void set_param_hard_max(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1798 struct range_list
*rl
= NULL
;
1799 struct smatch_state
*state
;
1800 struct symbol
*type
;
1804 if (strcmp(key
, "*$") == 0)
1805 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
1806 else if (strncmp(key
, "$", 1) == 0)
1807 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
1811 state
= get_state(SMATCH_EXTRA
, fullname
, sym
);
1814 type
= get_member_type_from_key(symbol_expression(sym
), key
);
1815 str_to_rl(type
, value
, &rl
);
1816 if (!rl_to_sval(rl
, &max
))
1818 estate_set_fuzzy_max(state
, max
);
1821 void register_smatch_extra(int id
)
1825 add_merge_hook(my_id
, &merge_estates
);
1826 add_unmatched_state_hook(my_id
, &unmatched_state
);
1827 select_caller_info_hook(set_param_value
, PARAM_VALUE
);
1828 select_caller_info_hook(set_param_hard_max
, FUZZY_MAX
);
1829 select_return_states_hook(RETURN_VALUE
, &db_returned_member_info
);
1830 select_return_states_before(&db_limited_before
);
1831 select_return_states_hook(PARAM_LIMIT
, &db_param_limit
);
1832 select_return_states_hook(PARAM_FILTER
, &db_param_filter
);
1833 select_return_states_hook(PARAM_ADD
, &db_param_add
);
1834 select_return_states_hook(PARAM_SET
, &db_param_set
);
1835 select_return_states_after(&db_limited_after
);
1838 void register_smatch_extra_late(int id
)
1840 add_hook(&match_dereferences
, DEREF_HOOK
);
1841 add_hook(&match_pointer_as_array
, OP_HOOK
);
1842 select_call_implies_hook(DEREFERENCE
, &set_param_dereferenced
);
1843 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
1844 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
1845 add_hook(&match_assign
, GLOBAL_ASSIGNMENT_HOOK
);
1846 add_hook(&unop_expr
, OP_HOOK
);
1847 add_hook(&asm_expr
, ASM_HOOK
);
1849 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1850 add_member_info_callback(my_id
, struct_member_callback
);
1851 add_returned_member_callback(my_id
, returned_member_callback
);
1853 add_hook(&assume_indexes_are_valid
, OP_HOOK
);