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.
34 #include "smatch_slist.h"
35 #include "smatch_extra.h"
40 static void match_link_modify(struct sm_state
*sm
, struct expression
*mod_expr
);
42 struct string_list
*__ignored_macros
= NULL
;
43 static int in_warn_on_macro(void)
45 struct statement
*stmt
;
49 stmt
= get_current_statement();
52 macro
= get_macro_name(stmt
->pos
);
56 FOR_EACH_PTR(__ignored_macros
, tmp
) {
57 if (!strcmp(tmp
, macro
))
59 } END_FOR_EACH_PTR(tmp
);
63 typedef void (mod_hook
)(const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
64 DECLARE_PTR_LIST(void_fn_list
, mod_hook
*);
65 static struct void_fn_list
*extra_mod_hooks
;
66 static struct void_fn_list
*extra_nomod_hooks
;
68 void add_extra_mod_hook(mod_hook
*fn
)
70 mod_hook
**p
= malloc(sizeof(mod_hook
*));
72 add_ptr_list(&extra_mod_hooks
, p
);
75 void add_extra_nomod_hook(mod_hook
*fn
)
77 mod_hook
**p
= malloc(sizeof(mod_hook
*));
79 add_ptr_list(&extra_nomod_hooks
, p
);
82 void call_extra_hooks(struct void_fn_list
*hooks
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
86 FOR_EACH_PTR(hooks
, fn
) {
87 (*fn
)(name
, sym
, state
);
88 } END_FOR_EACH_PTR(fn
);
91 void call_extra_mod_hooks(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
93 call_extra_hooks(extra_mod_hooks
, name
, sym
, state
);
96 void call_extra_nomod_hooks(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
98 call_extra_hooks(extra_nomod_hooks
, name
, sym
, state
);
101 static bool in_param_set
;
102 static void set_extra_mod_helper(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
104 remove_from_equiv(name
, sym
);
105 call_extra_mod_hooks(name
, sym
, state
);
106 if ((__in_fake_assign
|| in_param_set
) &&
107 estate_is_unknown(state
) && !get_state(SMATCH_EXTRA
, name
, sym
))
109 set_state(SMATCH_EXTRA
, name
, sym
, state
);
112 static void set_extra_nomod_helper(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
114 call_extra_nomod_hooks(name
, sym
, state
);
115 set_state(SMATCH_EXTRA
, name
, sym
, state
);
118 static char *get_pointed_at(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
120 struct expression
*assigned
;
124 if (strcmp(name
+ 1, sym
->ident
->name
) != 0)
127 assigned
= get_assigned_expr_name_sym(sym
->ident
->name
, sym
);
130 assigned
= strip_parens(assigned
);
131 if (assigned
->type
!= EXPR_PREOP
|| assigned
->op
!= '&')
134 return expr_to_var_sym(assigned
->unop
, new_sym
);
137 char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
139 struct expression
*assigned
;
140 char *orig_name
= NULL
;
147 if (!sym
|| !sym
->ident
)
150 ret
= get_pointed_at(name
, sym
, new_sym
);
154 skip
= strlen(sym
->ident
->name
);
155 if (name
[skip
] != '-' || name
[skip
+ 1] != '>')
159 assigned
= get_assigned_expr_name_sym(sym
->ident
->name
, sym
);
162 if (assigned
->type
== EXPR_CALL
)
163 return map_call_to_other_name_sym(name
, sym
, new_sym
);
164 if (assigned
->type
== EXPR_PREOP
|| assigned
->op
== '&') {
166 orig_name
= expr_to_var_sym(assigned
, new_sym
);
167 if (!orig_name
|| !*new_sym
)
170 snprintf(buf
, sizeof(buf
), "%s.%s", orig_name
+ 1, name
+ skip
);
171 ret
= alloc_string(buf
);
172 free_string(orig_name
);
176 if (assigned
->type
!= EXPR_DEREF
)
179 orig_name
= expr_to_var_sym(assigned
, new_sym
);
180 if (!orig_name
|| !*new_sym
)
183 snprintf(buf
, sizeof(buf
), "%s->%s", orig_name
, name
+ skip
);
184 ret
= alloc_string(buf
);
185 free_string(orig_name
);
189 free_string(orig_name
);
193 void set_extra_mod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
196 struct symbol
*new_sym
;
198 set_extra_mod_helper(name
, sym
, state
);
199 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
200 if (new_name
&& new_sym
)
201 set_extra_mod_helper(new_name
, new_sym
, state
);
202 free_string(new_name
);
205 static void clear_array_states(struct expression
*array
)
209 sm
= get_sm_state_expr(link_id
, array
);
211 match_link_modify(sm
, NULL
);
214 static void set_extra_array_mod(struct expression
*expr
, struct smatch_state
*state
)
216 struct expression
*array
;
217 struct var_sym_list
*vsl
;
222 array
= get_array_base(expr
);
224 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
226 clear_array_states(array
);
230 FOR_EACH_PTR(vsl
, vs
) {
231 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
232 } END_FOR_EACH_PTR(vs
);
234 call_extra_mod_hooks(name
, sym
, state
);
235 set_state(SMATCH_EXTRA
, name
, sym
, state
);
240 void set_extra_expr_mod(struct expression
*expr
, struct smatch_state
*state
)
245 if (is_array(expr
)) {
246 set_extra_array_mod(expr
, state
);
250 expr
= strip_expr(expr
);
251 name
= expr_to_var_sym(expr
, &sym
);
254 set_extra_mod(name
, sym
, state
);
259 void set_extra_nomod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
262 struct symbol
*new_sym
;
263 struct relation
*rel
;
264 struct smatch_state
*orig_state
;
266 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
268 /* don't save unknown states if leaving it blank is the same */
269 if (!orig_state
&& estate_is_unknown(state
))
272 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
273 if (new_name
&& new_sym
)
274 set_extra_nomod_helper(new_name
, new_sym
, state
);
275 free_string(new_name
);
277 if (!estate_related(orig_state
)) {
278 set_extra_nomod_helper(name
, sym
, state
);
282 set_related(state
, estate_related(orig_state
));
283 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
284 struct smatch_state
*estate
;
286 if (option_debug_related
)
287 sm_msg("%s updating related %s to %s", name
, rel
->name
, state
->name
);
288 estate
= get_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
);
291 set_extra_nomod_helper(rel
->name
, rel
->sym
, clone_estate_cast(estate_type(estate
), state
));
292 } END_FOR_EACH_PTR(rel
);
295 void set_extra_nomod_vsl(const char *name
, struct symbol
*sym
, struct var_sym_list
*vsl
, struct smatch_state
*state
)
299 FOR_EACH_PTR(vsl
, vs
) {
300 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
301 } END_FOR_EACH_PTR(vs
);
303 set_extra_nomod(name
, sym
, state
);
307 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
309 void set_extra_expr_nomod(struct expression
*expr
, struct smatch_state
*state
)
311 struct var_sym_list
*vsl
;
316 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
319 FOR_EACH_PTR(vsl
, vs
) {
320 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
321 } END_FOR_EACH_PTR(vs
);
323 set_extra_nomod(name
, sym
, state
);
328 static void set_extra_true_false(const char *name
, struct symbol
*sym
,
329 struct smatch_state
*true_state
,
330 struct smatch_state
*false_state
)
333 struct symbol
*new_sym
;
334 struct relation
*rel
;
335 struct smatch_state
*orig_state
;
337 if (!true_state
&& !false_state
)
340 if (in_warn_on_macro())
343 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
344 if (new_name
&& new_sym
)
345 set_true_false_states(SMATCH_EXTRA
, new_name
, new_sym
, true_state
, false_state
);
346 free_string(new_name
);
348 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
350 if (!estate_related(orig_state
)) {
351 set_true_false_states(SMATCH_EXTRA
, name
, sym
, true_state
, false_state
);
356 set_related(true_state
, estate_related(orig_state
));
358 set_related(false_state
, estate_related(orig_state
));
360 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
361 set_true_false_states(SMATCH_EXTRA
, rel
->name
, rel
->sym
,
362 true_state
, false_state
);
363 } END_FOR_EACH_PTR(rel
);
366 static void set_extra_chunk_true_false(struct expression
*expr
,
367 struct smatch_state
*true_state
,
368 struct smatch_state
*false_state
)
370 struct var_sym_list
*vsl
;
376 if (in_warn_on_macro())
379 type
= get_type(expr
);
383 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
386 FOR_EACH_PTR(vsl
, vs
) {
387 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
388 } END_FOR_EACH_PTR(vs
);
390 set_true_false_states(SMATCH_EXTRA
, name
, sym
,
391 clone_estate(true_state
),
392 clone_estate(false_state
));
397 static void set_extra_expr_true_false(struct expression
*expr
,
398 struct smatch_state
*true_state
,
399 struct smatch_state
*false_state
)
405 if (!true_state
&& !false_state
)
408 if (get_value(expr
, &sval
))
411 expr
= strip_expr(expr
);
412 name
= expr_to_var_sym(expr
, &sym
);
415 set_extra_chunk_true_false(expr
, true_state
, false_state
);
418 set_extra_true_false(name
, sym
, true_state
, false_state
);
422 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
424 struct expression
*iter_var
;
425 struct expression
*condition
;
427 struct smatch_state
*estate
;
430 condition
= strip_expr(loop
->iterator_pre_condition
);
433 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
435 if (condition
->op
!= SPECIAL_DECREMENT
)
438 iter_var
= condition
->unop
;
439 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
442 if (sval_cmp_val(estate_min(sm
->state
), 0) < 0)
444 start
= estate_max(sm
->state
);
445 if (sval_cmp_val(start
, 0) <= 0)
447 if (!sval_is_max(start
))
450 if (condition
->type
== EXPR_PREOP
) {
451 estate
= alloc_estate_range(sval_type_val(start
.type
, 1), start
);
452 if (estate_has_hard_max(sm
->state
))
453 estate_set_hard_max(estate
);
454 estate_copy_fuzzy_max(estate
, sm
->state
);
455 set_extra_expr_mod(iter_var
, estate
);
457 if (condition
->type
== EXPR_POSTOP
) {
458 estate
= alloc_estate_range(sval_type_val(start
.type
, 0), start
);
459 if (estate_has_hard_max(sm
->state
))
460 estate_set_hard_max(estate
);
461 estate_copy_fuzzy_max(estate
, sm
->state
);
462 set_extra_expr_mod(iter_var
, estate
);
464 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
467 static struct sm_state
*handle_canonical_for_inc(struct expression
*iter_expr
,
468 struct expression
*condition
)
470 struct expression
*iter_var
;
472 struct smatch_state
*estate
;
473 sval_t start
, end
, max
;
475 iter_var
= iter_expr
->unop
;
476 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
479 if (!estate_get_single_value(sm
->state
, &start
))
481 if (get_implied_max(condition
->right
, &end
))
482 end
= sval_cast(get_type(iter_var
), end
);
484 end
= sval_type_max(get_type(iter_var
));
486 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
489 switch (condition
->op
) {
490 case SPECIAL_UNSIGNED_LT
:
491 case SPECIAL_NOTEQUAL
:
493 if (!sval_is_min(end
))
496 case SPECIAL_UNSIGNED_LTE
:
502 if (sval_cmp(end
, start
) < 0)
504 estate
= alloc_estate_range(start
, end
);
505 if (get_hard_max(condition
->right
, &max
)) {
506 estate_set_hard_max(estate
);
507 if (condition
->op
== '<' ||
508 condition
->op
== SPECIAL_UNSIGNED_LT
||
509 condition
->op
== SPECIAL_NOTEQUAL
)
511 estate_set_fuzzy_max(estate
, max
);
513 set_extra_expr_mod(iter_var
, estate
);
514 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
517 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
518 struct expression
*condition
)
520 struct expression
*iter_var
;
522 struct smatch_state
*estate
;
525 iter_var
= iter_expr
->unop
;
526 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
529 if (!estate_get_single_value(sm
->state
, &start
))
531 if (!get_implied_min(condition
->right
, &end
))
532 end
= sval_type_min(get_type(iter_var
));
533 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
536 switch (condition
->op
) {
537 case SPECIAL_NOTEQUAL
:
539 if (!sval_is_min(end
) && !sval_is_max(end
))
547 if (sval_cmp(end
, start
) > 0)
549 estate
= alloc_estate_range(end
, start
);
550 estate_set_hard_max(estate
);
551 estate_set_fuzzy_max(estate
, estate_get_fuzzy_max(estate
));
552 set_extra_expr_mod(iter_var
, estate
);
553 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
556 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
558 struct expression
*iter_expr
;
559 struct expression
*condition
;
561 if (!loop
->iterator_post_statement
)
563 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
565 iter_expr
= loop
->iterator_post_statement
->expression
;
566 if (!loop
->iterator_pre_condition
)
568 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
570 condition
= loop
->iterator_pre_condition
;
572 if (iter_expr
->op
== SPECIAL_INCREMENT
)
573 return handle_canonical_for_inc(iter_expr
, condition
);
574 if (iter_expr
->op
== SPECIAL_DECREMENT
)
575 return handle_canonical_for_dec(iter_expr
, condition
);
579 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct stree
**stree
)
581 struct sm_state
*ret
;
583 __push_fake_cur_stree();
584 if (!loop
->iterator_post_statement
)
585 ret
= handle_canonical_while_count_down(loop
);
587 ret
= handle_canonical_for_loops(loop
);
588 *stree
= __pop_fake_cur_stree();
592 int __iterator_unchanged(struct sm_state
*sm
)
596 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
601 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
605 /* paranoid checking. prolly not needed */
606 condition
= strip_expr(condition
);
609 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
611 if (condition
->op
!= SPECIAL_DECREMENT
)
613 after_value
= estate_min(sm
->state
);
615 set_extra_mod(sm
->name
, sm
->sym
, alloc_estate_sval(after_value
));
618 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
619 struct statement
*iterator
,
620 struct expression
*condition
)
622 struct expression
*iter_expr
;
624 struct smatch_state
*state
;
627 while_count_down_after(sm
, condition
);
631 iter_expr
= iterator
->expression
;
633 if (condition
->type
!= EXPR_COMPARE
)
635 if (iter_expr
->op
== SPECIAL_INCREMENT
) {
636 limit
= sval_binop(estate_max(sm
->state
), '+',
637 sval_type_val(estate_type(sm
->state
), 1));
639 limit
= sval_binop(estate_min(sm
->state
), '-',
640 sval_type_val(estate_type(sm
->state
), 1));
642 if (!estate_has_hard_max(sm
->state
) && !__has_breaks()) {
643 if (iter_expr
->op
== SPECIAL_INCREMENT
)
644 state
= alloc_estate_range(estate_min(sm
->state
), limit
);
646 state
= alloc_estate_range(limit
, estate_max(sm
->state
));
648 state
= alloc_estate_sval(limit
);
650 if (!estate_has_hard_max(sm
->state
)) {
651 estate_clear_hard_max(state
);
653 if (estate_has_fuzzy_max(sm
->state
)) {
654 sval_t hmax
= estate_get_fuzzy_max(sm
->state
);
655 sval_t max
= estate_max(sm
->state
);
657 if (sval_cmp(hmax
, max
) != 0)
658 estate_clear_fuzzy_max(state
);
659 } else if (!estate_has_fuzzy_max(sm
->state
)) {
660 estate_clear_fuzzy_max(state
);
663 set_extra_mod(sm
->name
, sm
->sym
, state
);
666 static struct stree
*unmatched_stree
;
667 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
669 struct smatch_state
*state
;
671 if (unmatched_stree
) {
672 state
= get_state_stree(unmatched_stree
, SMATCH_EXTRA
, sm
->name
, sm
->sym
);
676 if (parent_is_gone_var_sym(sm
->name
, sm
->sym
))
677 return alloc_estate_empty();
678 return alloc_estate_whole(estate_type(sm
->state
));
681 static void clear_the_pointed_at(struct expression
*expr
)
686 struct sm_state
*tmp
;
688 name
= expr_to_var_sym(expr
, &sym
);
692 stree
= __get_cur_stree();
693 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
694 if (tmp
->name
[0] != '*')
698 if (strcmp(tmp
->name
+ 1, name
) != 0)
700 set_extra_mod(tmp
->name
, tmp
->sym
, alloc_estate_whole(estate_type(tmp
->state
)));
701 } END_FOR_EACH_SM(tmp
);
707 static void match_function_call(struct expression
*expr
)
709 struct expression
*arg
;
710 struct expression
*tmp
;
712 /* if we have the db this is handled in smatch_function_hooks.c */
715 if (inlinable(expr
->fn
))
718 FOR_EACH_PTR(expr
->args
, arg
) {
719 tmp
= strip_expr(arg
);
720 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
721 set_extra_expr_mod(tmp
->unop
, alloc_estate_whole(get_type(tmp
->unop
)));
723 clear_the_pointed_at(tmp
);
724 } END_FOR_EACH_PTR(arg
);
727 static int values_fit_type(struct expression
*left
, struct expression
*right
)
729 struct range_list
*rl
;
732 type
= get_type(left
);
735 get_absolute_rl(right
, &rl
);
736 if (type_unsigned(type
) && sval_is_negative(rl_min(rl
)))
738 if (sval_cmp(sval_type_min(type
), rl_min(rl
)) > 0)
740 if (sval_cmp(sval_type_max(type
), rl_max(rl
)) < 0)
745 static void save_chunk_info(struct expression
*left
, struct expression
*right
)
747 struct var_sym_list
*vsl
;
749 struct expression
*add_expr
;
755 if (right
->type
!= EXPR_BINOP
|| right
->op
!= '-')
757 if (!get_value(right
->left
, &sval
))
759 if (!expr_to_sym(right
->right
))
762 add_expr
= binop_expression(left
, '+', right
->right
);
763 type
= get_type(add_expr
);
766 name
= expr_to_chunk_sym_vsl(add_expr
, &sym
, &vsl
);
769 FOR_EACH_PTR(vsl
, vs
) {
770 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
771 } END_FOR_EACH_PTR(vs
);
773 set_state(SMATCH_EXTRA
, name
, sym
, alloc_estate_sval(sval_cast(type
, sval
)));
778 static void do_array_assign(struct expression
*left
, int op
, struct expression
*right
)
780 struct range_list
*rl
;
783 get_absolute_rl(right
, &rl
);
784 rl
= cast_rl(get_type(left
), rl
);
786 rl
= alloc_whole_rl(get_type(left
));
789 set_extra_array_mod(left
, alloc_estate_rl(rl
));
792 static void match_vanilla_assign(struct expression
*left
, struct expression
*right
)
794 struct range_list
*orig_rl
= NULL
;
795 struct range_list
*rl
= NULL
;
796 struct symbol
*right_sym
;
797 struct symbol
*left_type
;
798 struct symbol
*right_type
;
799 char *right_name
= NULL
;
803 struct smatch_state
*state
;
809 save_chunk_info(left
, right
);
811 name
= expr_to_var_sym(left
, &sym
);
814 do_array_assign(left
, '=', right
);
818 left_type
= get_type(left
);
819 right_type
= get_type(right
);
821 right_name
= expr_to_var_sym(right
, &right_sym
);
823 if (!__in_fake_assign
&&
824 !(right
->type
== EXPR_PREOP
&& right
->op
== '&') &&
825 right_name
&& right_sym
&&
826 values_fit_type(left
, right
) &&
827 !has_symbol(right
, sym
)) {
828 set_equiv(left
, right
);
832 if (is_pointer(right
) && get_address_rl(right
, &rl
)) {
833 state
= alloc_estate_rl(rl
);
837 if (__in_fake_assign
) {
838 struct smatch_state
*right_state
;
841 if (get_value(right
, &sval
)) {
842 sval
= sval_cast(left_type
, sval
);
843 state
= alloc_estate_sval(sval
);
847 right_state
= get_state(SMATCH_EXTRA
, right_name
, right_sym
);
849 /* simple assignment */
850 state
= clone_estate(right_state
);
854 state
= alloc_estate_rl(alloc_whole_rl(left_type
));
858 comparison
= get_comparison(left
, right
);
860 comparison
= flip_comparison(comparison
);
861 get_implied_rl(left
, &orig_rl
);
864 if (get_implied_rl(right
, &rl
)) {
865 rl
= cast_rl(left_type
, rl
);
867 filter_by_comparison(&rl
, comparison
, orig_rl
);
868 state
= alloc_estate_rl(rl
);
869 if (get_hard_max(right
, &max
)) {
870 estate_set_hard_max(state
);
871 estate_set_fuzzy_max(state
, max
);
874 rl
= alloc_whole_rl(right_type
);
875 rl
= cast_rl(left_type
, rl
);
877 filter_by_comparison(&rl
, comparison
, orig_rl
);
878 state
= alloc_estate_rl(rl
);
882 set_extra_mod(name
, sym
, state
);
884 free_string(right_name
);
887 static int op_remove_assign(int op
)
890 case SPECIAL_ADD_ASSIGN
:
892 case SPECIAL_SUB_ASSIGN
:
894 case SPECIAL_MUL_ASSIGN
:
896 case SPECIAL_DIV_ASSIGN
:
898 case SPECIAL_MOD_ASSIGN
:
900 case SPECIAL_AND_ASSIGN
:
902 case SPECIAL_OR_ASSIGN
:
904 case SPECIAL_XOR_ASSIGN
:
906 case SPECIAL_SHL_ASSIGN
:
907 return SPECIAL_LEFTSHIFT
;
908 case SPECIAL_SHR_ASSIGN
:
909 return SPECIAL_RIGHTSHIFT
;
915 static void match_assign(struct expression
*expr
)
917 struct range_list
*rl
= NULL
;
918 struct expression
*left
;
919 struct expression
*right
;
920 struct expression
*binop_expr
;
921 struct symbol
*left_type
;
924 sval_t left_min
, left_max
;
925 sval_t right_min
, right_max
;
926 sval_t res_min
, res_max
;
928 left
= strip_expr(expr
->left
);
930 right
= strip_parens(expr
->right
);
931 if (right
->type
== EXPR_CALL
&& sym_name_is("__builtin_expect", right
->fn
))
932 right
= get_argument_from_call_expr(right
->args
, 0);
933 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
934 right
= strip_parens(right
->left
);
936 if (expr
->op
== '=' && is_condition(expr
->right
))
937 return; /* handled in smatch_condition.c */
938 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
939 return; /* handled in smatch_function_hooks.c */
940 if (expr
->op
== '=') {
941 match_vanilla_assign(left
, right
);
945 name
= expr_to_var_sym(left
, &sym
);
949 left_type
= get_type(left
);
951 res_min
= sval_type_min(left_type
);
952 res_max
= sval_type_max(left_type
);
955 case SPECIAL_ADD_ASSIGN
:
956 get_absolute_max(left
, &left_max
);
957 get_absolute_max(right
, &right_max
);
958 if (sval_binop_overflows(left_max
, '+', sval_cast(left_type
, right_max
)))
960 if (get_implied_min(left
, &left_min
) &&
961 !sval_is_negative_min(left_min
) &&
962 get_implied_min(right
, &right_min
) &&
963 !sval_is_negative_min(right_min
)) {
964 res_min
= sval_binop(left_min
, '+', right_min
);
965 res_min
= sval_cast(left_type
, res_min
);
967 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
969 res_max
= sval_binop(left_max
, '+', right_max
);
970 res_max
= sval_cast(left_type
, res_max
);
972 case SPECIAL_SUB_ASSIGN
:
973 if (get_implied_max(left
, &left_max
) &&
974 !sval_is_max(left_max
) &&
975 get_implied_min(right
, &right_min
) &&
976 !sval_is_min(right_min
)) {
977 res_max
= sval_binop(left_max
, '-', right_min
);
978 res_max
= sval_cast(left_type
, res_max
);
982 if (get_implied_min(left
, &left_min
) &&
983 !sval_is_min(left_min
) &&
984 get_implied_max(right
, &right_max
) &&
985 !sval_is_max(right_max
)) {
986 res_min
= sval_binop(left_min
, '-', right_max
);
987 res_min
= sval_cast(left_type
, res_min
);
990 case SPECIAL_AND_ASSIGN
:
991 case SPECIAL_MOD_ASSIGN
:
992 case SPECIAL_SHL_ASSIGN
:
993 case SPECIAL_SHR_ASSIGN
:
994 case SPECIAL_OR_ASSIGN
:
995 case SPECIAL_XOR_ASSIGN
:
996 case SPECIAL_MUL_ASSIGN
:
997 case SPECIAL_DIV_ASSIGN
:
998 binop_expr
= binop_expression(expr
->left
,
999 op_remove_assign(expr
->op
),
1001 if (get_absolute_rl(binop_expr
, &rl
)) {
1002 rl
= cast_rl(left_type
, rl
);
1003 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
1008 rl
= cast_rl(left_type
, alloc_rl(res_min
, res_max
));
1009 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
1014 static struct smatch_state
*increment_state(struct smatch_state
*state
)
1016 sval_t min
= estate_min(state
);
1017 sval_t max
= estate_max(state
);
1019 if (!estate_rl(state
))
1023 max
= sval_type_max(max
.type
);
1025 if (!sval_is_min(min
) && !sval_is_max(min
))
1027 if (!sval_is_min(max
) && !sval_is_max(max
))
1029 return alloc_estate_range(min
, max
);
1032 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
1034 sval_t min
= estate_min(state
);
1035 sval_t max
= estate_max(state
);
1037 if (!estate_rl(state
))
1041 min
= sval_type_min(min
.type
);
1043 if (!sval_is_min(min
) && !sval_is_max(min
))
1045 if (!sval_is_min(max
) && !sval_is_max(max
))
1047 return alloc_estate_range(min
, max
);
1050 static void clear_pointed_at_state(struct expression
*expr
)
1052 struct symbol
*type
;
1055 * ALERT: This is sort of a mess. If it's is a struct assigment like
1056 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1057 * the same thing for p++ where "p" is a struct. Most modifications
1058 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1059 * use smatch_modification.c because we have to get the ordering right
1060 * or something. So if you have p++ where p is a pointer to a standard
1061 * c type then we handle that here. What a mess.
1064 type
= get_type(expr
);
1065 if (!type
|| type
->type
!= SYM_PTR
)
1067 type
= get_real_base_type(type
);
1068 if (!type
|| type
->type
!= SYM_BASETYPE
)
1070 set_extra_expr_mod(deref_expression(expr
), alloc_estate_whole(type
));
1073 static void unop_expr(struct expression
*expr
)
1075 struct smatch_state
*state
;
1077 if (expr
->smatch_flags
& Handled
)
1081 case SPECIAL_INCREMENT
:
1082 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
1083 state
= increment_state(state
);
1085 state
= alloc_estate_whole(get_type(expr
));
1086 set_extra_expr_mod(expr
->unop
, state
);
1087 clear_pointed_at_state(expr
->unop
);
1089 case SPECIAL_DECREMENT
:
1090 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
1091 state
= decrement_state(state
);
1093 state
= alloc_estate_whole(get_type(expr
));
1094 set_extra_expr_mod(expr
->unop
, state
);
1095 clear_pointed_at_state(expr
->unop
);
1102 static void asm_expr(struct statement
*stmt
)
1105 struct expression
*expr
;
1106 struct symbol
*type
;
1109 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
1111 case 0: /* identifier */
1112 case 1: /* constraint */
1115 case 2: /* expression */
1117 type
= get_type(strip_expr(expr
));
1118 set_extra_expr_mod(expr
, alloc_estate_whole(type
));
1121 } END_FOR_EACH_PTR(expr
);
1124 static void check_dereference(struct expression
*expr
)
1126 struct smatch_state
*state
;
1128 if (__in_fake_assign
)
1130 if (outside_of_function())
1132 state
= get_extra_state(expr
);
1134 static struct range_list
*valid_rl
;
1135 struct range_list
*rl
;
1138 valid_rl
= alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
1139 valid_rl
= clone_rl_permanent(valid_rl
);
1142 rl
= rl_intersection(estate_rl(state
), valid_rl
);
1143 if (rl_equiv(rl
, estate_rl(state
)))
1145 set_extra_expr_nomod(expr
, alloc_estate_rl(rl
));
1147 set_extra_expr_nomod(expr
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
1151 static void match_dereferences(struct expression
*expr
)
1153 if (expr
->type
!= EXPR_PREOP
)
1155 /* it's saying that foo[1] = bar dereferences foo[1] */
1158 check_dereference(expr
->unop
);
1161 static void match_pointer_as_array(struct expression
*expr
)
1163 if (!is_array(expr
))
1165 check_dereference(get_array_base(expr
));
1168 static void find_dereferences(struct expression
*expr
)
1170 while (expr
->type
== EXPR_PREOP
) {
1171 if (expr
->op
== '*')
1172 check_dereference(expr
->unop
);
1173 expr
= strip_expr(expr
->unop
);
1177 static void set_param_dereferenced(struct expression
*arg
, char *key
, char *unused
)
1182 name
= get_variable_from_key(arg
, key
, &sym
);
1184 struct smatch_state
*orig
, *new;
1185 struct range_list
*rl
;
1187 orig
= get_state(SMATCH_EXTRA
, name
, sym
);
1189 rl
= rl_intersection(estate_rl(orig
),
1190 alloc_rl(valid_ptr_min_sval
,
1191 valid_ptr_max_sval
));
1192 new = alloc_estate_rl(rl
);
1194 new = alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
);
1197 set_extra_nomod(name
, sym
, new);
1201 find_dereferences(arg
);
1204 static sval_t
add_one(sval_t sval
)
1210 static int handle_postop_inc(struct expression
*left
, int op
, struct expression
*right
)
1212 struct statement
*stmt
;
1213 struct expression
*cond
;
1214 struct smatch_state
*true_state
, *false_state
;
1219 * If we're decrementing here then that's a canonical while count down
1220 * so it's handled already. We're only handling loops like:
1222 * do { ... } while (i++ < 3);
1225 if (left
->type
!= EXPR_POSTOP
|| left
->op
!= SPECIAL_INCREMENT
)
1228 stmt
= __cur_stmt
->parent
;
1231 if (stmt
->type
== STMT_COMPOUND
)
1232 stmt
= stmt
->parent
;
1233 if (!stmt
|| stmt
->type
!= STMT_ITERATOR
|| !stmt
->iterator_post_condition
)
1236 cond
= strip_expr(stmt
->iterator_post_condition
);
1237 if (cond
->type
!= EXPR_COMPARE
|| cond
->op
!= op
)
1239 if (left
!= strip_expr(cond
->left
) || right
!= strip_expr(cond
->right
))
1242 if (!get_implied_value(left
->unop
, &start
))
1244 if (!get_implied_value(right
, &limit
))
1247 if (sval_cmp(start
, limit
) > 0)
1252 case SPECIAL_UNSIGNED_LT
:
1255 case SPECIAL_UNSIGNED_LTE
:
1256 limit
= add_one(limit
);
1262 true_state
= alloc_estate_range(add_one(start
), limit
);
1263 false_state
= alloc_estate_range(add_one(limit
), add_one(limit
));
1265 /* Currently we just discard the false state but when two passes is
1266 * implimented correctly then it will use it.
1269 set_extra_expr_true_false(left
->unop
, true_state
, false_state
);
1274 static void handle_comparison(struct symbol
*type
, struct expression
*left
, int op
, struct expression
*right
)
1276 struct range_list
*left_orig
;
1277 struct range_list
*left_true
;
1278 struct range_list
*left_false
;
1279 struct range_list
*right_orig
;
1280 struct range_list
*right_true
;
1281 struct range_list
*right_false
;
1282 struct smatch_state
*left_true_state
;
1283 struct smatch_state
*left_false_state
;
1284 struct smatch_state
*right_true_state
;
1285 struct smatch_state
*right_false_state
;
1286 sval_t dummy
, hard_max
;
1287 int left_postop
= 0;
1288 int right_postop
= 0;
1290 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
1291 if (left
->type
== EXPR_POSTOP
) {
1292 left
->smatch_flags
|= Handled
;
1293 left_postop
= left
->op
;
1294 if (handle_postop_inc(left
, op
, right
))
1297 left
= strip_parens(left
->unop
);
1299 while (left
->type
== EXPR_ASSIGNMENT
)
1300 left
= strip_parens(left
->left
);
1302 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
1303 if (right
->type
== EXPR_POSTOP
) {
1304 right
->smatch_flags
|= Handled
;
1305 right_postop
= right
->op
;
1307 right
= strip_parens(right
->unop
);
1310 get_real_absolute_rl(left
, &left_orig
);
1311 left_orig
= cast_rl(type
, left_orig
);
1313 get_real_absolute_rl(right
, &right_orig
);
1314 right_orig
= cast_rl(type
, right_orig
);
1316 split_comparison_rl(left_orig
, op
, right_orig
, &left_true
, &left_false
, &right_true
, &right_false
);
1318 left_true
= rl_truncate_cast(get_type(strip_expr(left
)), left_true
);
1319 left_false
= rl_truncate_cast(get_type(strip_expr(left
)), left_false
);
1320 right_true
= rl_truncate_cast(get_type(strip_expr(right
)), right_true
);
1321 right_false
= rl_truncate_cast(get_type(strip_expr(right
)), right_false
);
1323 if (!left_true
|| !left_false
) {
1324 struct range_list
*tmp_true
, *tmp_false
;
1326 split_comparison_rl(alloc_whole_rl(type
), op
, right_orig
, &tmp_true
, &tmp_false
, NULL
, NULL
);
1327 tmp_true
= rl_truncate_cast(get_type(strip_expr(left
)), tmp_true
);
1328 tmp_false
= rl_truncate_cast(get_type(strip_expr(left
)), tmp_false
);
1329 if (tmp_true
&& tmp_false
)
1330 __save_imaginary_state(left
, tmp_true
, tmp_false
);
1333 if (!right_true
|| !right_false
) {
1334 struct range_list
*tmp_true
, *tmp_false
;
1336 split_comparison_rl(alloc_whole_rl(type
), op
, right_orig
, NULL
, NULL
, &tmp_true
, &tmp_false
);
1337 tmp_true
= rl_truncate_cast(get_type(strip_expr(right
)), tmp_true
);
1338 tmp_false
= rl_truncate_cast(get_type(strip_expr(right
)), tmp_false
);
1339 if (tmp_true
&& tmp_false
)
1340 __save_imaginary_state(right
, tmp_true
, tmp_false
);
1343 left_true_state
= alloc_estate_rl(left_true
);
1344 left_false_state
= alloc_estate_rl(left_false
);
1345 right_true_state
= alloc_estate_rl(right_true
);
1346 right_false_state
= alloc_estate_rl(right_false
);
1350 case SPECIAL_UNSIGNED_LT
:
1351 case SPECIAL_UNSIGNED_LTE
:
1353 if (get_hard_max(right
, &dummy
))
1354 estate_set_hard_max(left_true_state
);
1355 if (get_hard_max(left
, &dummy
))
1356 estate_set_hard_max(right_false_state
);
1359 case SPECIAL_UNSIGNED_GT
:
1360 case SPECIAL_UNSIGNED_GTE
:
1362 if (get_hard_max(left
, &dummy
))
1363 estate_set_hard_max(right_true_state
);
1364 if (get_hard_max(right
, &dummy
))
1365 estate_set_hard_max(left_false_state
);
1371 case SPECIAL_UNSIGNED_LT
:
1372 case SPECIAL_UNSIGNED_LTE
:
1374 if (get_hard_max(right
, &hard_max
)) {
1375 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1377 estate_set_fuzzy_max(left_true_state
, hard_max
);
1379 if (get_implied_value(right
, &hard_max
)) {
1380 if (op
== SPECIAL_UNSIGNED_LTE
||
1383 estate_set_fuzzy_max(left_false_state
, hard_max
);
1385 if (get_hard_max(left
, &hard_max
)) {
1386 if (op
== SPECIAL_UNSIGNED_LTE
||
1389 estate_set_fuzzy_max(right_false_state
, hard_max
);
1391 if (get_implied_value(left
, &hard_max
)) {
1392 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1394 estate_set_fuzzy_max(right_true_state
, hard_max
);
1398 case SPECIAL_UNSIGNED_GT
:
1399 case SPECIAL_UNSIGNED_GTE
:
1401 if (get_hard_max(left
, &hard_max
)) {
1402 if (op
== '>' || op
== SPECIAL_UNSIGNED_GT
)
1404 estate_set_fuzzy_max(right_true_state
, hard_max
);
1406 if (get_implied_value(left
, &hard_max
)) {
1407 if (op
== SPECIAL_UNSIGNED_GTE
||
1410 estate_set_fuzzy_max(right_false_state
, hard_max
);
1412 if (get_hard_max(right
, &hard_max
)) {
1413 if (op
== SPECIAL_UNSIGNED_LTE
||
1416 estate_set_fuzzy_max(left_false_state
, hard_max
);
1418 if (get_implied_value(right
, &hard_max
)) {
1420 op
== SPECIAL_UNSIGNED_GT
)
1422 estate_set_fuzzy_max(left_true_state
, hard_max
);
1426 if (get_hard_max(left
, &hard_max
))
1427 estate_set_fuzzy_max(right_true_state
, hard_max
);
1428 if (get_hard_max(right
, &hard_max
))
1429 estate_set_fuzzy_max(left_true_state
, hard_max
);
1433 if (get_hard_max(left
, &hard_max
)) {
1434 estate_set_hard_max(left_true_state
);
1435 estate_set_hard_max(left_false_state
);
1437 if (get_hard_max(right
, &hard_max
)) {
1438 estate_set_hard_max(right_true_state
);
1439 estate_set_hard_max(right_false_state
);
1442 if (left_postop
== SPECIAL_INCREMENT
) {
1443 left_true_state
= increment_state(left_true_state
);
1444 left_false_state
= increment_state(left_false_state
);
1446 if (left_postop
== SPECIAL_DECREMENT
) {
1447 left_true_state
= decrement_state(left_true_state
);
1448 left_false_state
= decrement_state(left_false_state
);
1450 if (right_postop
== SPECIAL_INCREMENT
) {
1451 right_true_state
= increment_state(right_true_state
);
1452 right_false_state
= increment_state(right_false_state
);
1454 if (right_postop
== SPECIAL_DECREMENT
) {
1455 right_true_state
= decrement_state(right_true_state
);
1456 right_false_state
= decrement_state(right_false_state
);
1459 if (estate_rl(left_true_state
) && estates_equiv(left_true_state
, left_false_state
)) {
1460 left_true_state
= NULL
;
1461 left_false_state
= NULL
;
1464 if (estate_rl(right_true_state
) && estates_equiv(right_true_state
, right_false_state
)) {
1465 right_true_state
= NULL
;
1466 right_false_state
= NULL
;
1469 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
1470 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
1473 static int is_simple_math(struct expression
*expr
)
1477 if (expr
->type
!= EXPR_BINOP
)
1488 static void move_known_values(struct expression
**left_p
, struct expression
**right_p
)
1490 struct expression
*left
= *left_p
;
1491 struct expression
*right
= *right_p
;
1494 if (get_implied_value(left
, &sval
)) {
1495 if (!is_simple_math(right
))
1497 if (get_implied_value(right
, &dummy
))
1499 if (right
->op
== '*') {
1502 if (!get_value(right
->right
, &divisor
))
1504 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1506 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1507 *right_p
= right
->left
;
1510 if (right
->op
== '+' && get_value(right
->left
, &sval
)) {
1511 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->left
);
1512 *right_p
= right
->right
;
1515 if (get_value(right
->right
, &sval
)) {
1516 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1517 *right_p
= right
->left
;
1522 if (get_implied_value(right
, &sval
)) {
1523 if (!is_simple_math(left
))
1525 if (get_implied_value(left
, &dummy
))
1527 if (left
->op
== '*') {
1530 if (!get_value(left
->right
, &divisor
))
1532 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1534 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1535 *left_p
= left
->left
;
1538 if (left
->op
== '+' && get_value(left
->left
, &sval
)) {
1539 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->left
);
1540 *left_p
= left
->right
;
1544 if (get_value(left
->right
, &sval
)) {
1545 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1546 *left_p
= left
->left
;
1554 * The reason for do_simple_algebra() is to solve things like:
1555 * if (foo > 66 || foo + bar > 64) {
1556 * "foo" is not really a known variable so it won't be handled by
1557 * move_known_variables() but it's a super common idiom.
1560 static int do_simple_algebra(struct expression
**left_p
, struct expression
**right_p
)
1562 struct expression
*left
= *left_p
;
1563 struct expression
*right
= *right_p
;
1564 struct range_list
*rl
;
1567 if (left
->type
!= EXPR_BINOP
|| left
->op
!= '+')
1569 if (can_integer_overflow(get_type(left
), left
))
1571 if (!get_implied_value(right
, &tmp
))
1574 if (!get_implied_value(left
->left
, &tmp
) &&
1575 get_implied_rl(left
->left
, &rl
) &&
1577 *right_p
= binop_expression(right
, '-', left
->left
);
1578 *left_p
= left
->right
;
1581 if (!get_implied_value(left
->right
, &tmp
) &&
1582 get_implied_rl(left
->right
, &rl
) &&
1584 *right_p
= binop_expression(right
, '-', left
->right
);
1585 *left_p
= left
->left
;
1592 static int match_func_comparison(struct expression
*expr
)
1594 struct expression
*left
= strip_expr(expr
->left
);
1595 struct expression
*right
= strip_expr(expr
->right
);
1599 * fixme: think about this harder. We should always be trying to limit
1600 * the non-call side as well. If we can't determine the limitter does
1601 * that mean we aren't querying the database and are missing important
1605 if (left
->type
== EXPR_CALL
) {
1606 if (get_implied_value(left
, &sval
)) {
1607 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1610 function_comparison(left
, expr
->op
, right
);
1614 if (right
->type
== EXPR_CALL
) {
1615 if (get_implied_value(right
, &sval
)) {
1616 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1619 function_comparison(left
, expr
->op
, right
);
1626 /* Handle conditions like "if (foo + bar < foo) {" */
1627 static int handle_integer_overflow_test(struct expression
*expr
)
1629 struct expression
*left
, *right
;
1630 struct symbol
*type
;
1631 sval_t left_min
, right_min
, min
, max
;
1633 if (expr
->op
!= '<' && expr
->op
!= SPECIAL_UNSIGNED_LT
)
1636 left
= strip_parens(expr
->left
);
1637 right
= strip_parens(expr
->right
);
1639 if (left
->op
!= '+')
1642 type
= get_type(expr
);
1645 if (type_positive_bits(type
) == 32) {
1646 max
.type
= &uint_ctype
;
1647 max
.uvalue
= (unsigned int)-1;
1648 } else if (type_positive_bits(type
) == 64) {
1649 max
.type
= &ulong_ctype
;
1650 max
.value
= (unsigned long long)-1;
1655 if (!expr_equiv(left
->left
, right
) && !expr_equiv(left
->right
, right
))
1658 get_absolute_min(left
->left
, &left_min
);
1659 get_absolute_min(left
->right
, &right_min
);
1660 min
= sval_binop(left_min
, '+', right_min
);
1662 set_extra_chunk_true_false(left
, NULL
, alloc_estate_range(min
, max
));
1666 static void match_comparison(struct expression
*expr
)
1668 struct expression
*left_orig
= strip_parens(expr
->left
);
1669 struct expression
*right_orig
= strip_parens(expr
->right
);
1670 struct expression
*left
, *right
;
1671 struct expression
*prev
;
1672 struct symbol
*type
;
1674 if (match_func_comparison(expr
))
1677 type
= get_type(expr
);
1679 type
= &llong_ctype
;
1681 if (handle_integer_overflow_test(expr
))
1686 move_known_values(&left
, &right
);
1687 handle_comparison(type
, left
, expr
->op
, right
);
1691 if (do_simple_algebra(&left
, &right
))
1692 handle_comparison(type
, left
, expr
->op
, right
);
1694 prev
= get_assigned_expr(left_orig
);
1695 if (is_simple_math(prev
) && has_variable(prev
, left_orig
) == 0) {
1698 move_known_values(&left
, &right
);
1699 handle_comparison(type
, left
, expr
->op
, right
);
1702 prev
= get_assigned_expr(right_orig
);
1703 if (is_simple_math(prev
) && has_variable(prev
, right_orig
) == 0) {
1706 move_known_values(&left
, &right
);
1707 handle_comparison(type
, left
, expr
->op
, right
);
1711 static sval_t
get_high_mask(sval_t known
)
1719 for (i
= type_bits(known
.type
) - 1; i
>= 0; i
--) {
1720 if (known
.uvalue
& (1ULL << i
))
1721 ret
.uvalue
|= (1ULL << i
);
1729 static void handle_AND_condition(struct expression
*expr
)
1731 struct range_list
*orig_rl
;
1732 struct range_list
*true_rl
= NULL
;
1733 struct range_list
*false_rl
= NULL
;
1737 if (get_implied_value(expr
->left
, &known
)) {
1738 sval_t low_mask
= known
;
1741 if (known
.value
> 0) {
1742 bit
= ffsll(known
.value
) - 1;
1743 low_mask
.uvalue
= (1ULL << bit
) - 1;
1744 get_absolute_rl(expr
->right
, &orig_rl
);
1745 true_rl
= remove_range(orig_rl
, sval_type_val(known
.type
, 0), low_mask
);
1747 high_mask
= get_high_mask(known
);
1748 if (high_mask
.value
) {
1749 bit
= ffsll(high_mask
.value
) - 1;
1750 low_mask
.uvalue
= (1ULL << bit
) - 1;
1752 get_absolute_rl(expr
->left
, &orig_rl
);
1753 if (sval_is_negative(rl_min(orig_rl
)))
1754 orig_rl
= remove_range(orig_rl
, sval_type_min(known
.type
), sval_type_val(known
.type
, -1));
1755 false_rl
= remove_range(orig_rl
, low_mask
, sval_type_max(known
.type
));
1756 if (type_signed(high_mask
.type
) && type_unsigned(rl_type(false_rl
))) {
1757 false_rl
= remove_range(false_rl
,
1758 sval_type_val(rl_type(false_rl
), sval_type_max(known
.type
).uvalue
),
1759 sval_type_val(rl_type(false_rl
), -1));
1762 set_extra_expr_true_false(expr
->right
,
1763 true_rl
? alloc_estate_rl(true_rl
) : NULL
,
1764 false_rl
? alloc_estate_rl(false_rl
) : NULL
);
1769 if (get_implied_value(expr
->right
, &known
)) {
1770 sval_t low_mask
= known
;
1773 if (known
.value
> 0) {
1774 bit
= ffsll(known
.value
) - 1;
1775 low_mask
.uvalue
= (1ULL << bit
) - 1;
1776 get_absolute_rl(expr
->left
, &orig_rl
);
1777 true_rl
= remove_range(orig_rl
, sval_type_val(known
.type
, 0), low_mask
);
1779 high_mask
= get_high_mask(known
);
1780 if (high_mask
.value
) {
1781 bit
= ffsll(high_mask
.value
) - 1;
1782 low_mask
.uvalue
= (1ULL << bit
) - 1;
1784 get_absolute_rl(expr
->left
, &orig_rl
);
1785 if (sval_is_negative(rl_min(orig_rl
)))
1786 orig_rl
= remove_range(orig_rl
, sval_type_min(known
.type
), sval_type_val(known
.type
, -1));
1787 false_rl
= remove_range(orig_rl
, low_mask
, sval_type_max(known
.type
));
1788 if (type_signed(high_mask
.type
) && type_unsigned(rl_type(false_rl
))) {
1789 false_rl
= remove_range(false_rl
,
1790 sval_type_val(rl_type(false_rl
), sval_type_max(known
.type
).uvalue
),
1791 sval_type_val(rl_type(false_rl
), -1));
1794 set_extra_expr_true_false(expr
->left
,
1795 true_rl
? alloc_estate_rl(true_rl
) : NULL
,
1796 false_rl
? alloc_estate_rl(false_rl
) : NULL
);
1801 static void handle_MOD_condition(struct expression
*expr
)
1803 struct range_list
*orig_rl
;
1804 struct range_list
*true_rl
;
1805 struct range_list
*false_rl
= NULL
;
1811 if (!get_implied_value(expr
->right
, &right
) || right
.value
== 0)
1813 get_absolute_rl(expr
->left
, &orig_rl
);
1815 zero
.type
= rl_type(orig_rl
);
1817 /* We're basically dorking around the min and max here */
1818 true_rl
= remove_range(orig_rl
, zero
, zero
);
1819 if (!sval_is_max(rl_max(true_rl
)) &&
1820 !(rl_max(true_rl
).value
% right
.value
))
1821 true_rl
= remove_range(true_rl
, rl_max(true_rl
), rl_max(true_rl
));
1823 if (rl_equiv(true_rl
, orig_rl
))
1826 if (sval_is_positive(rl_min(orig_rl
)) &&
1827 (rl_max(orig_rl
).value
- rl_min(orig_rl
).value
) / right
.value
< 5) {
1831 add
= rl_min(orig_rl
);
1832 add
.value
+= right
.value
- (add
.value
% right
.value
);
1833 add
.value
-= right
.value
;
1835 for (i
= 0; i
< 5; i
++) {
1836 add
.value
+= right
.value
;
1837 if (add
.value
> rl_max(orig_rl
).value
)
1839 add_range(&false_rl
, add
, add
);
1842 if (rl_min(orig_rl
).uvalue
!= 0 &&
1843 rl_min(orig_rl
).uvalue
< right
.uvalue
) {
1844 sval_t chop
= right
;
1846 false_rl
= remove_range(orig_rl
, zero
, chop
);
1849 if (!sval_is_max(rl_max(orig_rl
)) &&
1850 (rl_max(orig_rl
).value
% right
.value
)) {
1851 sval_t chop
= rl_max(orig_rl
);
1852 chop
.value
-= chop
.value
% right
.value
;
1855 false_rl
= clone_rl(orig_rl
);
1856 false_rl
= remove_range(false_rl
, chop
, rl_max(orig_rl
));
1860 set_extra_expr_true_false(expr
->left
,
1861 true_rl
? alloc_estate_rl(true_rl
) : NULL
,
1862 false_rl
? alloc_estate_rl(false_rl
) : NULL
);
1865 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1866 void __extra_match_condition(struct expression
*expr
)
1868 struct smatch_state
*pre_state
;
1869 struct smatch_state
*true_state
;
1870 struct smatch_state
*false_state
;
1871 struct range_list
*pre_rl
;
1873 expr
= strip_expr(expr
);
1874 switch (expr
->type
) {
1876 function_comparison(expr
, SPECIAL_NOTEQUAL
, zero_expr());
1883 zero
= sval_blank(expr
);
1886 pre_state
= get_extra_state(expr
);
1887 if (estate_is_empty(pre_state
))
1890 pre_rl
= estate_rl(pre_state
);
1892 get_absolute_rl(expr
, &pre_rl
);
1893 if (possibly_true_rl(pre_rl
, SPECIAL_EQUAL
, rl_zero()))
1894 false_state
= alloc_estate_sval(zero
);
1896 false_state
= alloc_estate_empty();
1897 true_state
= alloc_estate_rl(remove_range(pre_rl
, zero
, zero
));
1898 set_extra_expr_true_false(expr
, true_state
, false_state
);
1902 match_comparison(expr
);
1904 case EXPR_ASSIGNMENT
:
1905 __extra_match_condition(expr
->left
);
1908 if (expr
->op
== '&')
1909 handle_AND_condition(expr
);
1910 if (expr
->op
== '%')
1911 handle_MOD_condition(expr
);
1916 static void assume_indexes_are_valid(struct expression
*expr
)
1918 struct expression
*array_expr
;
1920 struct expression
*offset
;
1921 struct symbol
*offset_type
;
1922 struct range_list
*rl_before
;
1923 struct range_list
*rl_after
;
1924 struct range_list
*filter
= NULL
;
1927 expr
= strip_expr(expr
);
1928 if (!is_array(expr
))
1931 offset
= get_array_offset(expr
);
1932 offset_type
= get_type(offset
);
1933 if (offset_type
&& type_signed(offset_type
)) {
1934 filter
= alloc_rl(sval_type_min(offset_type
),
1935 sval_type_val(offset_type
, -1));
1938 array_expr
= get_array_base(expr
);
1939 array_size
= get_real_array_size(array_expr
);
1940 if (array_size
> 1) {
1941 size
= sval_type_val(offset_type
, array_size
);
1942 add_range(&filter
, size
, sval_type_max(offset_type
));
1947 get_absolute_rl(offset
, &rl_before
);
1948 rl_after
= rl_filter(rl_before
, filter
);
1949 if (rl_equiv(rl_before
, rl_after
))
1951 set_extra_expr_nomod(offset
, alloc_estate_rl(rl_after
));
1954 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1955 int implied_not_equal(struct expression
*expr
, long long val
)
1957 return !possibly_false(expr
, SPECIAL_NOTEQUAL
, value_expr(val
));
1960 int implied_not_equal_name_sym(char *name
, struct symbol
*sym
, long long val
)
1962 struct smatch_state
*estate
;
1964 estate
= get_state(SMATCH_EXTRA
, name
, sym
);
1967 if (!rl_has_sval(estate_rl(estate
), sval_type_val(estate_type(estate
), 0)))
1972 int parent_is_null_var_sym(const char *name
, struct symbol
*sym
)
1977 struct smatch_state
*state
;
1979 strncpy(buf
, name
, sizeof(buf
) - 1);
1980 buf
[sizeof(buf
) - 1] = '\0';
1983 while (*start
== '*') {
1985 state
= get_state(SMATCH_EXTRA
, start
, sym
);
1988 if (!estate_rl(state
))
1990 if (estate_min(state
).value
== 0 &&
1991 estate_max(state
).value
== 0)
1996 while (*start
== '&')
1999 while ((end
= strrchr(start
, '-'))) {
2001 state
= get_state(SMATCH_EXTRA
, start
, sym
);
2004 if (estate_min(state
).value
== 0 &&
2005 estate_max(state
).value
== 0)
2011 int parent_is_null(struct expression
*expr
)
2017 expr
= strip_expr(expr
);
2018 var
= expr_to_var_sym(expr
, &sym
);
2021 ret
= parent_is_null_var_sym(var
, sym
);
2027 static int param_used_callback(void *found
, int argc
, char **argv
, char **azColName
)
2033 static int filter_unused_kzalloc_info(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
2038 /* for function pointers assume everything is used */
2039 if (call
->fn
->type
!= EXPR_SYMBOL
)
2043 * kzalloc() information is treated as special because so there is just
2044 * a lot of stuff initialized to zero and it makes building the database
2045 * take hours and hours.
2047 * In theory, we should just remove this line and not pass any unused
2048 * information, but I'm not sure enough that this code works so I want
2049 * to hold off on that for now.
2051 if (!estate_get_single_value(sm
->state
, &sval
) || sval
.value
!= 0)
2054 run_sql(¶m_used_callback
, &found
,
2055 "select * from call_implies where %s and type = %d and parameter = %d and key = '%s';",
2056 get_static_filter(call
->fn
->symbol
), PARAM_USED
, param
, printed_name
);
2060 /* If the database is not built yet, then assume everything is used */
2061 run_sql(¶m_used_callback
, &found
,
2062 "select * from call_implies where %s and type = %d;",
2063 get_static_filter(call
->fn
->symbol
), PARAM_USED
);
2070 struct range_list
*intersect_with_real_abs_var_sym(const char *name
, struct symbol
*sym
, struct range_list
*start
)
2072 struct smatch_state
*state
;
2075 * Here is the difference between implied value and real absolute, say
2080 * Then you know that a is 0-255. That's real absolute. But you don't
2081 * know for sure that it actually goes up to 255. So it's not implied.
2082 * Implied indicates a degree of certainty.
2084 * But then say you cap "a" at 8. That means you know it goes up to
2085 * 8. So now the implied value is s32min-8. But you can combine it
2086 * with the real absolute to say that actually it's 0-8.
2088 * We are combining it here. But now that I think about it, this is
2089 * probably not the ideal place to combine it because it should proably
2090 * be done earlier. Oh well, this is an improvement on what was there
2091 * before so I'm going to commit this code.
2095 state
= get_real_absolute_state_var_sym(name
, sym
);
2096 if (!state
|| !estate_rl(state
))
2099 return rl_intersection(estate_rl(state
), start
);
2102 struct range_list
*intersect_with_real_abs_expr(struct expression
*expr
, struct range_list
*start
)
2104 struct smatch_state
*state
;
2106 state
= get_real_absolute_state(expr
);
2107 if (!state
|| !estate_rl(state
))
2110 return rl_intersection(estate_rl(state
), start
);
2113 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
2115 struct range_list
*rl
;
2117 if (estate_is_whole(sm
->state
))
2119 if (filter_unused_kzalloc_info(call
, param
, printed_name
, sm
))
2121 rl
= estate_rl(sm
->state
);
2122 rl
= intersect_with_real_abs_var_sym(sm
->name
, sm
->sym
, rl
);
2123 sql_insert_caller_info(call
, PARAM_VALUE
, param
, printed_name
, show_rl(rl
));
2124 if (estate_has_fuzzy_max(sm
->state
))
2125 sql_insert_caller_info(call
, FUZZY_MAX
, param
, printed_name
,
2126 sval_to_str(estate_get_fuzzy_max(sm
->state
)));
2129 static void returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
2131 struct symbol
*returned_sym
;
2132 struct sm_state
*sm
;
2133 const char *param_name
;
2137 returned_sym
= expr_to_sym(expr
);
2141 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), sm
) {
2142 if (!estate_rl(sm
->state
))
2144 if (returned_sym
!= sm
->sym
)
2147 param_name
= get_param_name(sm
);
2150 if (strcmp(param_name
, "$") == 0)
2152 compare_str
= name_sym_to_param_comparison(sm
->name
, sm
->sym
);
2153 if (!compare_str
&& estate_is_whole(sm
->state
))
2155 snprintf(buf
, sizeof(buf
), "%s%s", sm
->state
->name
, compare_str
?: "");
2157 sql_insert_return_states(return_id
, return_ranges
, PARAM_VALUE
,
2158 -1, param_name
, buf
);
2159 } END_FOR_EACH_SM(sm
);
2162 static void db_limited_before(void)
2164 unmatched_stree
= clone_stree(__get_cur_stree());
2167 static void db_limited_after(void)
2169 free_stree(&unmatched_stree
);
2172 static int rl_fits_in_type(struct range_list
*rl
, struct symbol
*type
)
2174 if (type_bits(rl_type(rl
)) <= type_bits(type
))
2176 if (sval_cmp(rl_max(rl
), sval_type_max(type
)) > 0)
2178 if (sval_is_negative(rl_min(rl
)) &&
2179 sval_cmp(rl_min(rl
), sval_type_min(type
)) < 0)
2184 static int basically_the_same(struct range_list
*orig
, struct range_list
*new)
2186 if (rl_equiv(orig
, new))
2190 * The whole range is essentially the same as 0,4096-27777777777 so
2191 * don't overwrite the implications just to store that.
2194 if (rl_type(orig
)->type
== SYM_PTR
&&
2195 is_whole_rl(orig
) &&
2196 rl_min(new).value
== 0 &&
2197 rl_max(new).value
== valid_ptr_max
)
2202 static void db_param_limit_filter(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
2204 struct expression
*arg
;
2207 struct var_sym_list
*vsl
= NULL
;
2208 struct sm_state
*sm
;
2209 struct symbol
*compare_type
, *var_type
;
2210 struct range_list
*rl
;
2211 struct range_list
*limit
;
2212 struct range_list
*new;
2214 while (expr
->type
== EXPR_ASSIGNMENT
)
2215 expr
= strip_expr(expr
->right
);
2216 if (expr
->type
!= EXPR_CALL
)
2219 arg
= get_argument_from_call_expr(expr
->args
, param
);
2223 name
= get_chunk_from_key(arg
, key
, &sym
, &vsl
);
2226 if (op
!= PARAM_LIMIT
&& !sym
)
2229 if (strcmp(key
, "$") == 0)
2230 compare_type
= get_arg_type(expr
->fn
, param
);
2232 compare_type
= get_member_type_from_key(arg
, key
);
2234 sm
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
2236 rl
= estate_rl(sm
->state
);
2238 rl
= alloc_whole_rl(compare_type
);
2240 if (op
== PARAM_LIMIT
&& !rl_fits_in_type(rl
, compare_type
))
2243 call_results_to_rl(expr
, compare_type
, value
, &limit
);
2244 new = rl_intersection(rl
, limit
);
2246 var_type
= get_member_type_from_key(arg
, key
);
2247 new = cast_rl(var_type
, new);
2249 /* We want to preserve the implications here */
2250 if (sm
&& basically_the_same(estate_rl(sm
->state
), new))
2251 __set_sm(sm
); /* FIXME: Is this really necessary? */
2254 struct symbol
*tmp_sym
;
2256 tmp_name
= map_long_to_short_name_sym(name
, sym
, &tmp_sym
);
2257 if (tmp_name
&& tmp_sym
) {
2263 if (op
== PARAM_LIMIT
)
2264 set_extra_nomod_vsl(name
, sym
, vsl
, alloc_estate_rl(new));
2266 set_extra_mod(name
, sym
, alloc_estate_rl(new));
2273 static void db_param_limit(struct expression
*expr
, int param
, char *key
, char *value
)
2275 db_param_limit_filter(expr
, param
, key
, value
, PARAM_LIMIT
);
2278 static void db_param_filter(struct expression
*expr
, int param
, char *key
, char *value
)
2280 db_param_limit_filter(expr
, param
, key
, value
, PARAM_FILTER
);
2283 static void db_param_add_set(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
2285 struct expression
*arg
;
2286 char *name
, *tmp_name
;
2287 struct symbol
*sym
, *tmp_sym
;
2288 struct symbol
*type
;
2289 struct smatch_state
*state
;
2290 struct range_list
*new = NULL
;
2291 struct range_list
*added
= NULL
;
2293 while (expr
->type
== EXPR_ASSIGNMENT
)
2294 expr
= strip_expr(expr
->right
);
2295 if (expr
->type
!= EXPR_CALL
)
2298 arg
= get_argument_from_call_expr(expr
->args
, param
);
2301 type
= get_member_type_from_key(arg
, key
);
2302 name
= get_variable_from_key(arg
, key
, &sym
);
2306 state
= get_state(SMATCH_EXTRA
, name
, sym
);
2308 new = estate_rl(state
);
2310 call_results_to_rl(expr
, type
, value
, &added
);
2312 if (op
== PARAM_SET
)
2315 new = rl_union(new, added
);
2317 tmp_name
= map_long_to_short_name_sym_nostack(name
, sym
, &tmp_sym
);
2318 if (tmp_name
&& tmp_sym
) {
2323 set_extra_mod(name
, sym
, alloc_estate_rl(new));
2328 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
2330 in_param_set
= true;
2331 db_param_add_set(expr
, param
, key
, value
, PARAM_ADD
);
2332 in_param_set
= false;
2335 static void db_param_set(struct expression
*expr
, int param
, char *key
, char *value
)
2337 in_param_set
= true;
2338 db_param_add_set(expr
, param
, key
, value
, PARAM_SET
);
2339 in_param_set
= false;
2342 static void db_param_value(struct expression
*expr
, int param
, char *key
, char *value
)
2344 struct expression
*call
;
2347 struct symbol
*type
;
2348 struct range_list
*rl
= NULL
;
2354 while (call
->type
== EXPR_ASSIGNMENT
)
2355 call
= strip_expr(call
->right
);
2356 if (call
->type
!= EXPR_CALL
)
2359 type
= get_member_type_from_key(expr
->left
, key
);
2360 name
= get_variable_from_key(expr
->left
, key
, &sym
);
2364 call_results_to_rl(call
, type
, value
, &rl
);
2366 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
2371 static void match_call_info(struct expression
*expr
)
2373 struct smatch_state
*state
;
2374 struct range_list
*rl
= NULL
;
2375 struct expression
*arg
;
2376 struct symbol
*type
;
2379 FOR_EACH_PTR(expr
->args
, arg
) {
2380 type
= get_arg_type(expr
->fn
, i
);
2382 if (get_implied_rl(arg
, &rl
))
2383 rl
= cast_rl(type
, rl
);
2385 rl
= cast_rl(type
, alloc_whole_rl(get_type(arg
)));
2387 if (!is_whole_rl(rl
)) {
2388 rl
= intersect_with_real_abs_expr(arg
, rl
);
2389 sql_insert_caller_info(expr
, PARAM_VALUE
, i
, "$", show_rl(rl
));
2391 state
= get_state_expr(SMATCH_EXTRA
, arg
);
2392 if (estate_has_fuzzy_max(state
)) {
2393 sql_insert_caller_info(expr
, FUZZY_MAX
, i
, "$",
2394 sval_to_str(estate_get_fuzzy_max(state
)));
2397 } END_FOR_EACH_PTR(arg
);
2400 static void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2402 struct range_list
*rl
= NULL
;
2403 struct smatch_state
*state
;
2404 struct symbol
*type
;
2408 if (strcmp(key
, "*$") == 0)
2409 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
2410 else if (strncmp(key
, "$", 1) == 0)
2411 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
2415 type
= get_member_type_from_key(symbol_expression(sym
), key
);
2416 str_to_rl(type
, value
, &rl
);
2417 state
= alloc_estate_rl(rl
);
2418 if (estate_get_single_value(state
, &dummy
))
2419 estate_set_hard_max(state
);
2420 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
2423 static void set_param_hard_max(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2425 struct range_list
*rl
= NULL
;
2426 struct smatch_state
*state
;
2427 struct symbol
*type
;
2431 if (strcmp(key
, "*$") == 0)
2432 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
2433 else if (strncmp(key
, "$", 1) == 0)
2434 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
2438 state
= get_state(SMATCH_EXTRA
, fullname
, sym
);
2441 type
= get_member_type_from_key(symbol_expression(sym
), key
);
2442 str_to_rl(type
, value
, &rl
);
2443 if (!rl_to_sval(rl
, &max
))
2445 estate_set_fuzzy_max(state
, max
);
2448 struct smatch_state
*get_extra_state(struct expression
*expr
)
2452 struct smatch_state
*ret
= NULL
;
2453 struct range_list
*rl
;
2455 if (is_pointer(expr
) && get_address_rl(expr
, &rl
))
2456 return alloc_estate_rl(rl
);
2458 name
= expr_to_known_chunk_sym(expr
, &sym
);
2462 ret
= get_state(SMATCH_EXTRA
, name
, sym
);
2468 void register_smatch_extra(int id
)
2472 add_merge_hook(my_id
, &merge_estates
);
2473 add_unmatched_state_hook(my_id
, &unmatched_state
);
2474 select_caller_info_hook(set_param_value
, PARAM_VALUE
);
2475 select_caller_info_hook(set_param_hard_max
, FUZZY_MAX
);
2476 select_return_states_before(&db_limited_before
);
2477 select_return_states_hook(PARAM_LIMIT
, &db_param_limit
);
2478 select_return_states_hook(PARAM_FILTER
, &db_param_filter
);
2479 select_return_states_hook(PARAM_ADD
, &db_param_add
);
2480 select_return_states_hook(PARAM_SET
, &db_param_set
);
2481 select_return_states_hook(PARAM_VALUE
, &db_param_value
);
2482 select_return_states_after(&db_limited_after
);
2485 static void match_link_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
2487 struct var_sym_list
*links
;
2488 struct var_sym
*tmp
;
2489 struct smatch_state
*state
;
2491 links
= sm
->state
->data
;
2493 FOR_EACH_PTR(links
, tmp
) {
2494 if (sm
->sym
== tmp
->sym
&&
2495 strcmp(sm
->name
, tmp
->var
) == 0)
2497 state
= get_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
);
2500 set_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
, alloc_estate_whole(estate_type(state
)));
2501 } END_FOR_EACH_PTR(tmp
);
2502 set_state(link_id
, sm
->name
, sm
->sym
, &undefined
);
2505 void register_smatch_extra_links(int id
)
2510 void register_smatch_extra_late(int id
)
2512 add_merge_hook(link_id
, &merge_link_states
);
2513 add_modification_hook(link_id
, &match_link_modify
);
2514 add_hook(&match_dereferences
, DEREF_HOOK
);
2515 add_hook(&match_pointer_as_array
, OP_HOOK
);
2516 select_call_implies_hook(DEREFERENCE
, &set_param_dereferenced
);
2517 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
2518 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
2519 add_hook(&match_assign
, GLOBAL_ASSIGNMENT_HOOK
);
2520 add_hook(&unop_expr
, OP_HOOK
);
2521 add_hook(&asm_expr
, ASM_HOOK
);
2523 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2524 add_member_info_callback(my_id
, struct_member_callback
);
2525 add_split_return_callback(&returned_struct_members
);
2527 add_hook(&assume_indexes_are_valid
, OP_HOOK
);