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 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 expression
*expr
, 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 expression
*expr
, struct smatch_state
*state
)
86 FOR_EACH_PTR(hooks
, fn
) {
87 (*fn
)(name
, sym
, expr
, state
);
88 } END_FOR_EACH_PTR(fn
);
91 void call_extra_mod_hooks(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
93 call_extra_hooks(extra_mod_hooks
, name
, sym
, expr
, state
);
96 void call_extra_nomod_hooks(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
98 call_extra_hooks(extra_nomod_hooks
, name
, sym
, expr
, state
);
101 static void set_union_info(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
103 struct symbol
*type
, *tmp
, *inner_type
, *inner
, *new_type
;
104 struct expression
*deref
, *member_expr
;
105 struct smatch_state
*new;
106 int offset
, inner_offset
;
107 static bool in_recurse
;
110 if (__in_fake_assign
)
117 if (!expr
|| expr
->type
!= EXPR_DEREF
|| !expr
->member
)
119 offset
= get_member_offset_from_deref(expr
);
123 deref
= strip_expr(expr
->deref
);
124 type
= get_type(deref
);
125 if (type_is_ptr(type
))
126 type
= get_real_base_type(type
);
127 if (!type
|| type
->type
!= SYM_STRUCT
)
130 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
131 inner_type
= get_real_base_type(tmp
);
132 if (!inner_type
|| inner_type
->type
!= SYM_UNION
)
135 inner
= first_ptr_list((struct ptr_list
*)inner_type
->symbol_list
);
136 if (!inner
|| !inner
->ident
)
139 inner_offset
= get_member_offset(type
, inner
->ident
->name
);
140 if (inner_offset
< offset
)
142 if (inner_offset
> offset
)
145 FOR_EACH_PTR(inner_type
->symbol_list
, inner
) {
146 struct symbol
*tmp_type
;
148 if (!inner
->ident
|| inner
->ident
== expr
->member
)
150 tmp_type
= get_real_base_type(inner
);
151 if (tmp_type
&& tmp_type
->type
== SYM_STRUCT
)
155 member_expr
= member_expression(member_expr
, '.', tmp
->ident
);
156 member_expr
= member_expression(member_expr
, expr
->op
, inner
->ident
);
157 member_name
= expr_to_var(member_expr
);
160 new_type
= get_real_base_type(inner
);
161 new = alloc_estate_rl(cast_rl(new_type
, estate_rl(state
)));
162 set_extra_mod_helper(member_name
, sym
, member_expr
, new);
163 free_string(member_name
);
164 } END_FOR_EACH_PTR(inner
);
165 } END_FOR_EACH_PTR(tmp
);
171 static void mark_sub_members_gone(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
175 if (__in_fake_assign
|| __in_fake_var_assign
)
178 if (!estate_type(state
) || estate_type(state
)->type
!= SYM_PTR
)
180 if (!is_noderef_ptr_rl(estate_rl(state
)))
183 FOR_EACH_MY_SM(SMATCH_EXTRA
, __get_cur_stree(), sm
) {
186 if (strcmp(name
, sm
->name
) == 0)
188 if (!is_sub_member(name
, sym
, sm
))
190 set_extra_nomod(sm
->name
, sm
->sym
, NULL
, alloc_estate_empty());
191 } END_FOR_EACH_SM(sm
);
194 static void call_update_mtag_data(struct expression
*expr
,
195 struct smatch_state
*state
)
197 struct expression
*faked
;
199 if (__in_fake_var_assign
)
202 faked
= get_faked_expression();
205 if (faked
->type
== EXPR_ASSIGNMENT
&& is_fresh_alloc(faked
->right
))
210 update_mtag_data(expr
, state
);
213 static bool in_param_set
;
214 void set_extra_mod_helper(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
217 expr
= gen_expression_from_name_sym(name
, sym
);
218 remove_from_equiv(name
, sym
);
219 set_union_info(name
, sym
, expr
, state
);
220 mark_sub_members_gone(name
, sym
, state
);
221 call_extra_mod_hooks(name
, sym
, expr
, state
);
222 call_update_mtag_data(expr
, state
);
223 if ((__in_fake_assign
|| in_param_set
) &&
224 estate_is_unknown(state
) && !get_state(SMATCH_EXTRA
, name
, sym
))
226 set_state(SMATCH_EXTRA
, name
, sym
, state
);
229 static void set_extra_nomod_helper(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
231 call_extra_nomod_hooks(name
, sym
, expr
, state
);
232 set_state(SMATCH_EXTRA
, name
, sym
, state
);
235 static char *get_pointed_at(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
237 struct expression
*assigned
;
240 * Imagine we have an assignment: "foo = &addr;" then the other name
246 if (strcmp(name
+ 1, sym
->ident
->name
) != 0)
249 assigned
= get_assigned_expr_name_sym(sym
->ident
->name
, sym
);
252 assigned
= strip_parens(assigned
);
253 if (assigned
->type
!= EXPR_PREOP
|| assigned
->op
!= '&')
256 return expr_to_var_sym(assigned
->unop
, new_sym
);
259 char *get_other_name_sym_from_chunk(const char *name
, const char *chunk
, int len
, struct symbol
*sym
, struct symbol
**new_sym
)
261 struct expression
*assigned
;
262 char *orig_name
= NULL
;
266 assigned
= get_assigned_expr_name_sym(chunk
, sym
);
269 if (assigned
->type
== EXPR_CALL
)
270 return map_call_to_other_name_sym(name
, sym
, new_sym
);
271 if (assigned
->type
== EXPR_PREOP
&& assigned
->op
== '&') {
273 orig_name
= expr_to_var_sym(assigned
, new_sym
);
274 if (!orig_name
|| !*new_sym
)
277 snprintf(buf
, sizeof(buf
), "%s.%s", orig_name
+ 1, name
+ len
);
278 ret
= alloc_string(buf
);
279 free_string(orig_name
);
283 orig_name
= expr_to_var_sym(assigned
, new_sym
);
284 if (!orig_name
|| !*new_sym
)
287 snprintf(buf
, sizeof(buf
), "%s->%s", orig_name
, name
+ len
);
288 ret
= alloc_string(buf
);
289 free_string(orig_name
);
292 free_string(orig_name
);
296 static char *get_long_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
, bool use_stack
)
298 struct expression
*orig
;
299 struct symbol
*orig_sym
;
300 char *orig_name
, *ret
;
302 if (!sym
|| !sym
->ident
)
305 orig
= get_assigned_expr_name_sym(sym
->ident
->name
, sym
);
307 orig_name
= expr_to_var_sym(orig
, &orig_sym
);
311 ret
= swap_names(name
, sym
->ident
->name
, orig_name
);
312 free_string(orig_name
);
321 char *get_other_name_sym_helper(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
, bool use_stack
)
329 if (!sym
|| !sym
->ident
)
332 ret
= get_pointed_at(name
, sym
, new_sym
);
336 ret
= map_long_to_short_name_sym(name
, sym
, new_sym
, use_stack
);
340 len
= snprintf(buf
, sizeof(buf
), "%s", name
);
341 if (len
>= sizeof(buf
) - 2)
344 while (use_stack
&& len
>= 1) {
345 if (buf
[len
] == '>' && buf
[len
- 1] == '-') {
348 ret
= get_other_name_sym_from_chunk(name
, buf
, len
+ 2, sym
, new_sym
);
355 ret
= get_long_name_sym(name
, sym
, new_sym
, use_stack
);
362 char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
364 return get_other_name_sym_helper(name
, sym
, new_sym
, true);
367 char *get_other_name_sym_nostack(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
369 return get_other_name_sym_helper(name
, sym
, new_sym
, false);
372 void set_extra_mod(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
375 struct symbol
*new_sym
;
377 set_extra_mod_helper(name
, sym
, expr
, state
);
378 new_name
= get_other_name_sym_nostack(name
, sym
, &new_sym
);
379 if (new_name
&& new_sym
)
380 set_extra_mod_helper(new_name
, new_sym
, NULL
, state
);
381 free_string(new_name
);
384 static struct expression
*chunk_get_array_base(struct expression
*expr
)
387 * The problem with is_array() is that it only returns true for things
388 * like foo[1] but not for foo[1].bar.
391 expr
= strip_expr(expr
);
392 while (expr
&& expr
->type
== EXPR_DEREF
)
393 expr
= strip_expr(expr
->deref
);
394 return get_array_base(expr
);
397 static int chunk_has_array(struct expression
*expr
)
399 return !!chunk_get_array_base(expr
);
402 static void clear_array_states(struct expression
*array
)
406 sm
= get_sm_state_expr(link_id
, array
);
408 match_link_modify(sm
, NULL
);
411 static void set_extra_array_mod(struct expression
*expr
, struct smatch_state
*state
)
413 struct expression
*array
;
414 struct var_sym_list
*vsl
;
419 array
= chunk_get_array_base(expr
);
421 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
423 clear_array_states(array
);
427 FOR_EACH_PTR(vsl
, vs
) {
428 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
429 } END_FOR_EACH_PTR(vs
);
431 call_extra_mod_hooks(name
, sym
, expr
, state
);
432 set_state(SMATCH_EXTRA
, name
, sym
, state
);
437 void set_extra_expr_mod(struct expression
*expr
, struct smatch_state
*state
)
442 if (chunk_has_array(expr
)) {
443 set_extra_array_mod(expr
, state
);
447 expr
= strip_expr(expr
);
448 name
= expr_to_var_sym(expr
, &sym
);
451 set_extra_mod(name
, sym
, expr
, state
);
456 void set_extra_nomod(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
459 struct symbol
*new_sym
;
460 struct relation
*rel
;
461 struct smatch_state
*orig_state
;
463 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
465 /* don't save unknown states if leaving it blank is the same */
466 if (!orig_state
&& estate_is_unknown(state
))
469 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
470 if (new_name
&& new_sym
)
471 set_extra_nomod_helper(new_name
, new_sym
, expr
, state
);
472 free_string(new_name
);
474 if (!estate_related(orig_state
)) {
475 set_extra_nomod_helper(name
, sym
, expr
, state
);
479 set_related(state
, estate_related(orig_state
));
480 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
481 struct smatch_state
*estate
;
483 estate
= get_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
);
486 set_extra_nomod_helper(rel
->name
, rel
->sym
, expr
, clone_estate_cast(estate_type(estate
), state
));
487 } END_FOR_EACH_PTR(rel
);
490 void set_extra_nomod_vsl(const char *name
, struct symbol
*sym
, struct var_sym_list
*vsl
, struct expression
*expr
, struct smatch_state
*state
)
494 FOR_EACH_PTR(vsl
, vs
) {
495 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
496 } END_FOR_EACH_PTR(vs
);
498 set_extra_nomod(name
, sym
, expr
, state
);
502 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
504 void set_extra_expr_nomod(struct expression
*expr
, struct smatch_state
*state
)
506 struct var_sym_list
*vsl
;
511 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
514 FOR_EACH_PTR(vsl
, vs
) {
515 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
516 } END_FOR_EACH_PTR(vs
);
518 set_extra_nomod(name
, sym
, expr
, state
);
523 static void set_extra_true_false(const char *name
, struct symbol
*sym
,
524 struct smatch_state
*true_state
,
525 struct smatch_state
*false_state
)
528 struct symbol
*new_sym
;
529 struct relation
*rel
;
530 struct smatch_state
*orig_state
;
532 if (!true_state
&& !false_state
)
535 if (in_warn_on_macro())
538 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
539 if (new_name
&& new_sym
)
540 set_true_false_states(SMATCH_EXTRA
, new_name
, new_sym
, true_state
, false_state
);
541 free_string(new_name
);
543 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
545 if (!estate_related(orig_state
)) {
546 set_true_false_states(SMATCH_EXTRA
, name
, sym
, true_state
, false_state
);
551 set_related(true_state
, estate_related(orig_state
));
553 set_related(false_state
, estate_related(orig_state
));
555 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
556 set_true_false_states(SMATCH_EXTRA
, rel
->name
, rel
->sym
,
557 true_state
, false_state
);
558 } END_FOR_EACH_PTR(rel
);
561 static void set_extra_chunk_true_false(struct expression
*expr
,
562 struct smatch_state
*true_state
,
563 struct smatch_state
*false_state
)
565 struct var_sym_list
*vsl
;
571 if (in_warn_on_macro())
574 type
= get_type(expr
);
578 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
581 FOR_EACH_PTR(vsl
, vs
) {
582 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
583 } END_FOR_EACH_PTR(vs
);
585 set_true_false_states(SMATCH_EXTRA
, name
, sym
,
586 clone_estate(true_state
),
587 clone_estate(false_state
));
592 static void set_extra_expr_true_false(struct expression
*expr
,
593 struct smatch_state
*true_state
,
594 struct smatch_state
*false_state
)
600 if (!true_state
&& !false_state
)
603 if (get_value(expr
, &sval
))
606 expr
= strip_expr(expr
);
607 name
= expr_to_var_sym(expr
, &sym
);
610 set_extra_chunk_true_false(expr
, true_state
, false_state
);
613 set_extra_true_false(name
, sym
, true_state
, false_state
);
617 static int get_countdown_info(struct expression
*condition
, struct expression
**unop
, int *op
, sval_t
*right
)
619 struct expression
*unop_expr
;
623 right
->type
= &int_ctype
;
626 condition
= strip_expr(condition
);
628 if (condition
->type
== EXPR_COMPARE
) {
629 comparison
= remove_unsigned_from_comparison(condition
->op
);
631 if (comparison
!= SPECIAL_GTE
&& comparison
!= '>')
633 if (!get_value(condition
->right
, &limit
))
636 unop_expr
= condition
->left
;
637 if (unop_expr
->type
!= EXPR_PREOP
&& unop_expr
->type
!= EXPR_POSTOP
)
639 if (unop_expr
->op
!= SPECIAL_DECREMENT
)
649 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
651 if (condition
->op
!= SPECIAL_DECREMENT
)
660 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
662 struct expression
*iter_var
;
663 struct expression
*condition
, *unop
;
666 struct smatch_state
*estate
;
670 right
.type
= &int_ctype
;
673 condition
= strip_expr(loop
->iterator_pre_condition
);
677 if (!get_countdown_info(condition
, &unop
, &op
, &right
))
680 iter_var
= unop
->unop
;
682 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
685 if (sval_cmp(estate_min(sm
->state
), right
) < 0)
687 start
= estate_max(sm
->state
);
689 type
= get_type(iter_var
);
690 right
= sval_cast(type
, right
);
691 start
= sval_cast(type
, start
);
693 if (sval_cmp(start
, right
) <= 0)
695 if (!sval_is_max(start
))
698 if (op
== SPECIAL_GTE
)
701 if (unop
->type
== EXPR_PREOP
) {
703 estate
= alloc_estate_range(right
, start
);
704 if (estate_has_hard_max(sm
->state
))
705 estate_set_hard_max(estate
);
706 estate_copy_fuzzy_max(estate
, sm
->state
);
707 set_extra_expr_mod(iter_var
, estate
);
709 if (unop
->type
== EXPR_POSTOP
) {
710 estate
= alloc_estate_range(right
, start
);
711 if (estate_has_hard_max(sm
->state
))
712 estate_set_hard_max(estate
);
713 estate_copy_fuzzy_max(estate
, sm
->state
);
714 set_extra_expr_mod(iter_var
, estate
);
716 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
719 static struct sm_state
*handle_canonical_for_inc(struct expression
*iter_expr
,
720 struct expression
*condition
)
722 struct expression
*iter_var
;
724 struct smatch_state
*estate
;
725 sval_t start
, end
, max
;
726 bool unknown_end
= false;
728 iter_var
= iter_expr
->unop
;
729 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
732 if (!estate_get_single_value(sm
->state
, &start
))
734 if (!get_implied_max(condition
->right
, &end
)) {
735 end
= sval_type_max(get_type(condition
->right
));
736 end
= sval_cast(start
.type
, end
);
737 if (sval_is_max(end
))
741 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
744 switch (condition
->op
) {
745 case SPECIAL_UNSIGNED_LT
:
746 case SPECIAL_NOTEQUAL
:
748 if (!sval_is_min(end
) && !unknown_end
)
751 case SPECIAL_UNSIGNED_LTE
:
757 if (sval_cmp(end
, start
) < 0)
759 end
= sval_cast(start
.type
, end
);
760 estate
= alloc_estate_range(start
, end
);
761 if (get_hard_max(condition
->right
, &max
)) {
762 if (!get_macro_name(condition
->pos
))
763 estate_set_hard_max(estate
);
764 if (condition
->op
== '<' ||
765 condition
->op
== SPECIAL_UNSIGNED_LT
||
766 condition
->op
== SPECIAL_NOTEQUAL
)
768 max
= sval_cast(start
.type
, max
);
769 estate_set_fuzzy_max(estate
, max
);
771 set_extra_expr_mod(iter_var
, estate
);
772 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
775 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
776 struct expression
*condition
)
778 struct expression
*iter_var
;
780 struct smatch_state
*estate
;
783 iter_var
= iter_expr
->unop
;
784 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
787 if (!estate_get_single_value(sm
->state
, &start
))
789 if (!get_implied_min(condition
->right
, &end
))
790 end
= sval_type_min(get_type(iter_var
));
791 end
= sval_cast(estate_type(sm
->state
), end
);
792 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
795 switch (condition
->op
) {
796 case SPECIAL_NOTEQUAL
:
798 if (!sval_is_max(end
))
806 if (sval_cmp(end
, start
) > 0)
808 estate
= alloc_estate_range(end
, start
);
809 estate_set_hard_max(estate
);
810 estate_set_fuzzy_max(estate
, estate_get_fuzzy_max(estate
));
811 set_extra_expr_mod(iter_var
, estate
);
812 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
815 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
817 struct expression
*iter_expr
;
818 struct expression
*condition
;
820 if (!loop
->iterator_post_statement
)
822 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
824 iter_expr
= loop
->iterator_post_statement
->expression
;
825 if (!loop
->iterator_pre_condition
)
827 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
829 condition
= loop
->iterator_pre_condition
;
831 if (iter_expr
->op
== SPECIAL_INCREMENT
)
832 return handle_canonical_for_inc(iter_expr
, condition
);
833 if (iter_expr
->op
== SPECIAL_DECREMENT
)
834 return handle_canonical_for_dec(iter_expr
, condition
);
838 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct stree
**stree
)
840 struct sm_state
*ret
;
843 * Canonical loops are a hack. The proper way to handle this is to
844 * use two passes, but unfortunately, doing two passes makes parsing
845 * code twice as slow.
847 * What we do is we set the inside state here, which overwrites whatever
848 * __extra_match_condition() does. Then we set the outside state in
849 * __extra_pre_loop_hook_after().
852 __push_fake_cur_stree();
853 if (!loop
->iterator_post_statement
)
854 ret
= handle_canonical_while_count_down(loop
);
856 ret
= handle_canonical_for_loops(loop
);
857 *stree
= __pop_fake_cur_stree();
861 int __iterator_unchanged(struct sm_state
*sm
)
865 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
870 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
872 struct expression
*unop
;
874 sval_t limit
, after_value
;
876 if (!get_countdown_info(condition
, &unop
, &op
, &limit
))
878 after_value
= estate_min(sm
->state
);
880 set_extra_mod(sm
->name
, sm
->sym
, condition
->unop
, alloc_estate_sval(after_value
));
883 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
884 struct statement
*iterator
,
885 struct expression
*condition
)
887 struct expression
*iter_expr
;
889 struct smatch_state
*state
;
893 while_count_down_after(sm
, condition
);
897 iter_expr
= iterator
->expression
;
899 if (condition
->type
!= EXPR_COMPARE
)
902 if (iter_expr
->op
== SPECIAL_INCREMENT
) {
903 if (!get_implied_value(condition
->right
, &end
) &&
904 sval_is_max(estate_max(sm
->state
)))
905 limit
= estate_max(sm
->state
);
907 limit
= sval_binop(estate_max(sm
->state
), '+',
908 sval_type_val(estate_type(sm
->state
), 1));
910 limit
= sval_binop(estate_min(sm
->state
), '-',
911 sval_type_val(estate_type(sm
->state
), 1));
913 limit
= sval_cast(estate_type(sm
->state
), limit
);
914 if (!estate_has_hard_max(sm
->state
) && !__has_breaks()) {
915 if (iter_expr
->op
== SPECIAL_INCREMENT
)
916 state
= alloc_estate_range(estate_min(sm
->state
), limit
);
918 state
= alloc_estate_range(limit
, estate_max(sm
->state
));
920 state
= alloc_estate_sval(limit
);
922 if (!estate_has_hard_max(sm
->state
)) {
923 estate_clear_hard_max(state
);
925 if (estate_has_fuzzy_max(sm
->state
)) {
926 sval_t hmax
= estate_get_fuzzy_max(sm
->state
);
927 sval_t max
= estate_max(sm
->state
);
929 if (sval_cmp(hmax
, max
) != 0)
930 estate_clear_fuzzy_max(state
);
931 } else if (!estate_has_fuzzy_max(sm
->state
)) {
932 estate_clear_fuzzy_max(state
);
935 set_extra_mod(sm
->name
, sm
->sym
, iter_expr
, state
);
938 static bool get_global_rl(const char *name
, struct symbol
*sym
, struct range_list
**rl
)
940 struct expression
*expr
;
942 if (!sym
|| !(sym
->ctype
.modifiers
& MOD_TOPLEVEL
) || !sym
->ident
)
944 if (strcmp(sym
->ident
->name
, name
) != 0)
947 expr
= symbol_expression(sym
);
948 return get_implied_rl(expr
, rl
);
951 static struct stree
*unmatched_stree
;
952 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
954 struct smatch_state
*state
;
955 struct range_list
*rl
;
957 if (unmatched_stree
) {
958 state
= get_state_stree(unmatched_stree
, SMATCH_EXTRA
, sm
->name
, sm
->sym
);
962 if (parent_is_gone_var_sym(sm
->name
, sm
->sym
))
963 return alloc_estate_empty();
964 if (get_global_rl(sm
->name
, sm
->sym
, &rl
))
965 return alloc_estate_rl(rl
);
966 return alloc_estate_whole(estate_type(sm
->state
));
969 static void clear_the_pointed_at(struct expression
*expr
)
974 struct sm_state
*tmp
;
976 name
= expr_to_var_sym(expr
, &sym
);
980 stree
= __get_cur_stree();
981 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
982 if (tmp
->name
[0] != '*')
986 if (strcmp(tmp
->name
+ 1, name
) != 0)
988 set_extra_mod(tmp
->name
, tmp
->sym
, expr
, alloc_estate_whole(estate_type(tmp
->state
)));
989 } END_FOR_EACH_SM(tmp
);
995 static int is_const_param(struct expression
*expr
, int param
)
999 type
= get_arg_type(expr
, param
);
1002 if (type
->ctype
.modifiers
& MOD_CONST
)
1007 static void match_function_call(struct expression
*expr
)
1009 struct expression
*arg
;
1010 struct expression
*tmp
;
1013 /* if we have the db this is handled in smatch_function_hooks.c */
1016 if (inlinable(expr
->fn
))
1019 FOR_EACH_PTR(expr
->args
, arg
) {
1021 if (is_const_param(expr
->fn
, param
))
1023 tmp
= strip_expr(arg
);
1024 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
1025 set_extra_expr_mod(tmp
->unop
, alloc_estate_whole(get_type(tmp
->unop
)));
1027 clear_the_pointed_at(tmp
);
1028 } END_FOR_EACH_PTR(arg
);
1031 int values_fit_type(struct expression
*left
, struct expression
*right
)
1033 struct range_list
*rl
;
1034 struct symbol
*type
;
1036 type
= get_type(left
);
1039 get_absolute_rl(right
, &rl
);
1040 if (type
== rl_type(rl
))
1042 if (type_unsigned(type
) && sval_is_negative(rl_min(rl
)))
1044 if (sval_cmp(sval_type_min(type
), rl_min(rl
)) > 0)
1046 if (sval_cmp(sval_type_max(type
), rl_max(rl
)) < 0)
1051 static void save_chunk_info(struct expression
*left
, struct expression
*right
)
1053 struct var_sym_list
*vsl
;
1055 struct expression
*add_expr
;
1056 struct symbol
*type
;
1061 if (right
->type
!= EXPR_BINOP
|| right
->op
!= '-')
1063 if (!get_value(right
->left
, &sval
))
1065 if (!expr_to_sym(right
->right
))
1068 add_expr
= binop_expression(left
, '+', right
->right
);
1069 type
= get_type(add_expr
);
1072 name
= expr_to_chunk_sym_vsl(add_expr
, &sym
, &vsl
);
1075 FOR_EACH_PTR(vsl
, vs
) {
1076 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
1077 } END_FOR_EACH_PTR(vs
);
1079 set_state(SMATCH_EXTRA
, name
, sym
, alloc_estate_sval(sval_cast(type
, sval
)));
1084 static void do_array_assign(struct expression
*left
, int op
, struct expression
*right
)
1086 struct range_list
*rl
;
1089 get_absolute_rl(right
, &rl
);
1090 rl
= cast_rl(get_type(left
), rl
);
1092 rl
= alloc_whole_rl(get_type(left
));
1095 set_extra_array_mod(left
, alloc_estate_rl(rl
));
1098 static void match_vanilla_assign(struct expression
*left
, struct expression
*right
)
1100 struct range_list
*orig_rl
= NULL
;
1101 struct range_list
*rl
= NULL
;
1102 struct symbol
*right_sym
;
1103 struct symbol
*left_type
;
1104 struct symbol
*right_type
;
1105 char *right_name
= NULL
;
1109 struct smatch_state
*state
;
1112 if (is_struct(left
))
1115 save_chunk_info(left
, right
);
1117 name
= expr_to_var_sym(left
, &sym
);
1119 if (chunk_has_array(left
))
1120 do_array_assign(left
, '=', right
);
1124 left_type
= get_type(left
);
1125 right_type
= get_type(right
);
1127 right_name
= expr_to_var_sym(right
, &right_sym
);
1129 if (!__in_fake_assign
&&
1130 !(right
->type
== EXPR_PREOP
&& right
->op
== '&') &&
1131 right_name
&& right_sym
&&
1132 values_fit_type(left
, strip_expr(right
)) &&
1133 !has_symbol(right
, sym
)) {
1134 set_equiv(left
, right
);
1138 if (get_implied_value(right
, &sval
)) {
1139 state
= alloc_estate_sval(sval_cast(left_type
, sval
));
1143 if (__in_fake_assign
|| is_fake_var(left
)) {
1144 struct smatch_state
*right_state
;
1145 struct range_list
*rl
;
1147 right_state
= get_state(SMATCH_EXTRA
, right_name
, right_sym
);
1149 /* simple assignment */
1150 state
= clone_estate(right_state
);
1154 if (get_implied_rl(right
, &rl
)) {
1155 rl
= cast_rl(left_type
, rl
);
1156 state
= alloc_estate_rl(rl
);
1160 rl
= alloc_whole_rl(right_type
);
1161 rl
= cast_rl(left_type
, rl
);
1162 state
= alloc_estate_rl(rl
);
1166 comparison
= get_comparison_no_extra(left
, right
);
1168 comparison
= flip_comparison(comparison
);
1169 get_implied_rl(left
, &orig_rl
);
1172 if (get_implied_rl(right
, &rl
)) {
1173 rl
= cast_rl(left_type
, rl
);
1175 filter_by_comparison(&rl
, comparison
, orig_rl
);
1176 state
= alloc_estate_rl(rl
);
1177 if (get_hard_max(right
, &max
)) {
1178 estate_set_hard_max(state
);
1179 estate_set_fuzzy_max(state
, max
);
1182 rl
= alloc_whole_rl(right_type
);
1183 rl
= cast_rl(left_type
, rl
);
1185 filter_by_comparison(&rl
, comparison
, orig_rl
);
1186 state
= alloc_estate_rl(rl
);
1190 set_extra_mod(name
, sym
, left
, state
);
1192 free_string(right_name
);
1195 static void match_assign(struct expression
*expr
)
1197 struct range_list
*rl
= NULL
;
1198 struct expression
*left
;
1199 struct expression
*right
;
1200 struct expression
*binop_expr
;
1201 struct symbol
*left_type
;
1205 left
= strip_expr(expr
->left
);
1207 right
= strip_parens(expr
->right
);
1208 if (right
->type
== EXPR_CALL
&& sym_name_is("__builtin_expect", right
->fn
))
1209 right
= get_argument_from_call_expr(right
->args
, 0);
1210 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
1211 right
= strip_parens(right
->left
);
1213 if (expr
->op
== '=' && is_condition(expr
->right
))
1214 return; /* handled in smatch_condition.c */
1215 if (expr
->op
== '=' && right
->type
== EXPR_CALL
&&
1216 !is_fake_call(right
))
1217 return; /* handled in smatch_function_hooks.c */
1218 if (expr
->op
== '=') {
1219 match_vanilla_assign(left
, right
);
1223 name
= expr_to_var_sym(left
, &sym
);
1227 left_type
= get_type(left
);
1230 case SPECIAL_ADD_ASSIGN
:
1231 case SPECIAL_SUB_ASSIGN
:
1232 case SPECIAL_AND_ASSIGN
:
1233 case SPECIAL_MOD_ASSIGN
:
1234 case SPECIAL_SHL_ASSIGN
:
1235 case SPECIAL_SHR_ASSIGN
:
1236 case SPECIAL_OR_ASSIGN
:
1237 case SPECIAL_XOR_ASSIGN
:
1238 case SPECIAL_MUL_ASSIGN
:
1239 case SPECIAL_DIV_ASSIGN
:
1240 binop_expr
= binop_expression(expr
->left
,
1241 op_remove_assign(expr
->op
),
1243 get_absolute_rl(binop_expr
, &rl
);
1244 rl
= cast_rl(left_type
, rl
);
1245 if (inside_loop()) {
1246 if (expr
->op
== SPECIAL_ADD_ASSIGN
)
1247 add_range(&rl
, rl_max(rl
), sval_type_max(rl_type(rl
)));
1249 if (expr
->op
== SPECIAL_SUB_ASSIGN
&&
1250 !sval_is_negative(rl_min(rl
))) {
1251 sval_t zero
= { .type
= rl_type(rl
) };
1253 add_range(&rl
, rl_min(rl
), zero
);
1256 set_extra_mod(name
, sym
, left
, alloc_estate_rl(rl
));
1259 set_extra_mod(name
, sym
, left
, alloc_estate_whole(left_type
));
1264 static struct smatch_state
*increment_state(struct smatch_state
*state
)
1266 sval_t min
= estate_min(state
);
1267 sval_t max
= estate_max(state
);
1269 if (!estate_rl(state
))
1273 max
= sval_type_max(max
.type
);
1275 if (!sval_is_min(min
) && !sval_is_max(min
))
1277 if (!sval_is_min(max
) && !sval_is_max(max
))
1279 return alloc_estate_range(min
, max
);
1282 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
1284 sval_t min
= estate_min(state
);
1285 sval_t max
= estate_max(state
);
1287 if (!estate_rl(state
))
1291 min
= sval_type_min(min
.type
);
1293 if (!sval_is_min(min
) && !sval_is_max(min
))
1295 if (!sval_is_min(max
) && !sval_is_max(max
))
1297 return alloc_estate_range(min
, max
);
1300 static void clear_pointed_at_state(struct expression
*expr
)
1302 struct symbol
*type
;
1305 * ALERT: This is sort of a mess. If it's is a struct assigment like
1306 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1307 * the same thing for p++ where "p" is a struct. Most modifications
1308 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1309 * use smatch_modification.c because we have to get the ordering right
1310 * or something. So if you have p++ where p is a pointer to a standard
1311 * c type then we handle that here. What a mess.
1313 expr
= strip_expr(expr
);
1314 type
= get_type(expr
);
1315 if (!type
|| type
->type
!= SYM_PTR
)
1317 type
= get_real_base_type(type
);
1318 if (!type
|| type
->type
!= SYM_BASETYPE
)
1320 set_extra_expr_nomod(deref_expression(expr
), alloc_estate_whole(type
));
1323 static void unop_expr(struct expression
*expr
)
1325 struct smatch_state
*state
;
1327 if (expr
->smatch_flags
& Handled
)
1331 case SPECIAL_INCREMENT
:
1332 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
1333 state
= increment_state(state
);
1335 state
= alloc_estate_whole(get_type(expr
));
1336 set_extra_expr_mod(expr
->unop
, state
);
1337 clear_pointed_at_state(expr
->unop
);
1339 case SPECIAL_DECREMENT
:
1340 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
1341 state
= decrement_state(state
);
1343 state
= alloc_estate_whole(get_type(expr
));
1344 set_extra_expr_mod(expr
->unop
, state
);
1345 clear_pointed_at_state(expr
->unop
);
1352 static void asm_expr(struct statement
*stmt
)
1354 struct asm_operand
*op
;
1355 struct symbol
*type
;
1357 FOR_EACH_PTR(stmt
->asm_outputs
, op
) {
1358 type
= get_type(strip_expr(op
->expr
));
1359 set_extra_expr_mod(op
->expr
, alloc_estate_whole(type
));
1360 } END_FOR_EACH_PTR(op
);
1363 static void check_dereference(struct expression
*expr
)
1365 struct smatch_state
*state
;
1367 if (__in_fake_assign
)
1369 if (outside_of_function())
1371 state
= get_extra_state(expr
);
1373 struct range_list
*rl
;
1375 rl
= rl_intersection(estate_rl(state
), valid_ptr_rl
);
1376 if (rl_equiv(rl
, estate_rl(state
)))
1378 set_extra_expr_nomod(expr
, alloc_estate_rl(rl
));
1380 struct range_list
*rl
;
1382 if (get_mtag_rl(expr
, &rl
))
1383 rl
= rl_intersection(rl
, valid_ptr_rl
);
1385 rl
= clone_rl(valid_ptr_rl
);
1387 set_extra_expr_nomod(expr
, alloc_estate_rl(rl
));
1391 static void match_dereferences(struct expression
*expr
)
1393 if (expr
->type
!= EXPR_PREOP
)
1395 if (getting_address(expr
))
1397 /* it's saying that foo[1] = bar dereferences foo[1] */
1400 check_dereference(expr
->unop
);
1403 static void match_pointer_as_array(struct expression
*expr
)
1405 if (!is_array(expr
))
1407 check_dereference(get_array_base(expr
));
1410 static void find_dereferences(struct expression
*expr
)
1412 while (expr
->type
== EXPR_PREOP
) {
1413 if (expr
->op
== '*')
1414 check_dereference(expr
->unop
);
1415 expr
= strip_expr(expr
->unop
);
1419 static void set_param_dereferenced(struct expression
*call
, struct expression
*arg
, char *key
, char *unused
)
1424 name
= get_variable_from_key(arg
, key
, &sym
);
1426 struct smatch_state
*orig
, *new;
1427 struct range_list
*rl
;
1429 orig
= get_state(SMATCH_EXTRA
, name
, sym
);
1431 rl
= rl_intersection(estate_rl(orig
),
1432 alloc_rl(valid_ptr_min_sval
,
1433 valid_ptr_max_sval
));
1434 new = alloc_estate_rl(rl
);
1436 new = alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
);
1439 set_extra_nomod(name
, sym
, NULL
, new);
1443 find_dereferences(arg
);
1446 static sval_t
add_one(sval_t sval
)
1452 static int handle_postop_inc(struct expression
*left
, int op
, struct expression
*right
)
1454 struct statement
*stmt
;
1455 struct expression
*cond
;
1456 struct smatch_state
*true_state
, *false_state
;
1457 struct symbol
*type
;
1462 * If we're decrementing here then that's a canonical while count down
1463 * so it's handled already. We're only handling loops like:
1465 * do { ... } while (i++ < 3);
1468 if (left
->type
!= EXPR_POSTOP
|| left
->op
!= SPECIAL_INCREMENT
)
1471 stmt
= __cur_stmt
->parent
;
1474 if (stmt
->type
== STMT_COMPOUND
)
1475 stmt
= stmt
->parent
;
1476 if (!stmt
|| stmt
->type
!= STMT_ITERATOR
|| !stmt
->iterator_post_condition
)
1479 cond
= strip_expr(stmt
->iterator_post_condition
);
1480 if (cond
->type
!= EXPR_COMPARE
|| cond
->op
!= op
)
1482 if (left
!= strip_expr(cond
->left
) || right
!= strip_expr(cond
->right
))
1485 if (!get_implied_value(left
->unop
, &start
))
1487 if (!get_implied_value(right
, &limit
))
1489 type
= get_type(left
->unop
);
1490 limit
= sval_cast(type
, limit
);
1491 if (sval_cmp(start
, limit
) > 0)
1496 case SPECIAL_UNSIGNED_LT
:
1499 case SPECIAL_UNSIGNED_LTE
:
1500 limit
= add_one(limit
);
1506 true_state
= alloc_estate_range(add_one(start
), limit
);
1507 false_state
= alloc_estate_range(add_one(limit
), add_one(limit
));
1509 /* Currently we just discard the false state but when two passes is
1510 * implimented correctly then it will use it.
1513 set_extra_expr_true_false(left
->unop
, true_state
, false_state
);
1518 bool is_impossible_variable(struct expression
*expr
)
1520 struct smatch_state
*state
;
1522 state
= get_extra_state(expr
);
1523 if (state
&& !estate_rl(state
))
1528 static bool in_macro(struct expression
*left
, struct expression
*right
)
1530 if (!left
|| !right
)
1532 if (left
->pos
.line
!= right
->pos
.line
|| left
->pos
.pos
!= right
->pos
.pos
)
1534 if (get_macro_name(left
->pos
))
1539 static void handle_comparison(struct symbol
*type
, struct expression
*left
, int op
, struct expression
*right
)
1541 struct range_list
*left_orig
;
1542 struct range_list
*left_true
;
1543 struct range_list
*left_false
;
1544 struct range_list
*right_orig
;
1545 struct range_list
*right_true
;
1546 struct range_list
*right_false
;
1547 struct smatch_state
*left_true_state
;
1548 struct smatch_state
*left_false_state
;
1549 struct smatch_state
*right_true_state
;
1550 struct smatch_state
*right_false_state
;
1551 sval_t dummy
, hard_max
;
1552 int left_postop
= 0;
1553 int right_postop
= 0;
1555 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
1556 if (left
->type
== EXPR_POSTOP
) {
1557 left
->smatch_flags
|= Handled
;
1558 left_postop
= left
->op
;
1559 if (handle_postop_inc(left
, op
, right
))
1562 left
= strip_parens(left
->unop
);
1564 while (left
->type
== EXPR_ASSIGNMENT
)
1565 left
= strip_parens(left
->left
);
1567 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
1568 if (right
->type
== EXPR_POSTOP
) {
1569 right
->smatch_flags
|= Handled
;
1570 right_postop
= right
->op
;
1572 right
= strip_parens(right
->unop
);
1575 if (is_impossible_variable(left
) || is_impossible_variable(right
))
1578 get_real_absolute_rl(left
, &left_orig
);
1579 left_orig
= cast_rl(type
, left_orig
);
1581 get_real_absolute_rl(right
, &right_orig
);
1582 right_orig
= cast_rl(type
, right_orig
);
1584 split_comparison_rl(left_orig
, op
, right_orig
, &left_true
, &left_false
, &right_true
, &right_false
);
1586 left_true
= rl_truncate_cast(get_type(strip_expr(left
)), left_true
);
1587 left_false
= rl_truncate_cast(get_type(strip_expr(left
)), left_false
);
1588 right_true
= rl_truncate_cast(get_type(strip_expr(right
)), right_true
);
1589 right_false
= rl_truncate_cast(get_type(strip_expr(right
)), right_false
);
1591 if (!left_true
|| !left_false
) {
1592 struct range_list
*tmp_true
, *tmp_false
;
1594 split_comparison_rl(alloc_whole_rl(type
), op
, right_orig
, &tmp_true
, &tmp_false
, NULL
, NULL
);
1595 tmp_true
= rl_truncate_cast(get_type(strip_expr(left
)), tmp_true
);
1596 tmp_false
= rl_truncate_cast(get_type(strip_expr(left
)), tmp_false
);
1597 if (tmp_true
&& tmp_false
)
1598 __save_imaginary_state(left
, tmp_true
, tmp_false
);
1601 if (!right_true
|| !right_false
) {
1602 struct range_list
*tmp_true
, *tmp_false
;
1604 split_comparison_rl(alloc_whole_rl(type
), op
, right_orig
, NULL
, NULL
, &tmp_true
, &tmp_false
);
1605 tmp_true
= rl_truncate_cast(get_type(strip_expr(right
)), tmp_true
);
1606 tmp_false
= rl_truncate_cast(get_type(strip_expr(right
)), tmp_false
);
1607 if (tmp_true
&& tmp_false
)
1608 __save_imaginary_state(right
, tmp_true
, tmp_false
);
1611 left_true_state
= alloc_estate_rl(left_true
);
1612 left_false_state
= alloc_estate_rl(left_false
);
1613 right_true_state
= alloc_estate_rl(right_true
);
1614 right_false_state
= alloc_estate_rl(right_false
);
1618 case SPECIAL_UNSIGNED_LT
:
1619 case SPECIAL_UNSIGNED_LTE
:
1621 if (get_hard_max(right
, &dummy
) && !in_macro(left
, right
))
1622 estate_set_hard_max(left_true_state
);
1623 if (get_hard_max(left
, &dummy
) && !in_macro(left
, right
))
1624 estate_set_hard_max(right_false_state
);
1627 case SPECIAL_UNSIGNED_GT
:
1628 case SPECIAL_UNSIGNED_GTE
:
1630 if (get_hard_max(left
, &dummy
) && !in_macro(left
, right
))
1631 estate_set_hard_max(right_true_state
);
1632 if (get_hard_max(right
, &dummy
) && !in_macro(left
, right
))
1633 estate_set_hard_max(left_false_state
);
1639 case SPECIAL_UNSIGNED_LT
:
1640 case SPECIAL_UNSIGNED_LTE
:
1642 if (get_hard_max(right
, &hard_max
)) {
1643 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1645 estate_set_fuzzy_max(left_true_state
, hard_max
);
1647 if (get_implied_value(right
, &hard_max
)) {
1648 if (op
== SPECIAL_UNSIGNED_LTE
||
1651 estate_set_fuzzy_max(left_false_state
, hard_max
);
1653 if (get_hard_max(left
, &hard_max
)) {
1654 if (op
== SPECIAL_UNSIGNED_LTE
||
1657 estate_set_fuzzy_max(right_false_state
, hard_max
);
1659 if (get_implied_value(left
, &hard_max
)) {
1660 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1662 estate_set_fuzzy_max(right_true_state
, hard_max
);
1666 case SPECIAL_UNSIGNED_GT
:
1667 case SPECIAL_UNSIGNED_GTE
:
1669 if (get_hard_max(left
, &hard_max
)) {
1670 if (op
== '>' || op
== SPECIAL_UNSIGNED_GT
)
1672 estate_set_fuzzy_max(right_true_state
, hard_max
);
1674 if (get_implied_value(left
, &hard_max
)) {
1675 if (op
== SPECIAL_UNSIGNED_GTE
||
1678 estate_set_fuzzy_max(right_false_state
, hard_max
);
1680 if (get_hard_max(right
, &hard_max
)) {
1681 if (op
== SPECIAL_UNSIGNED_LTE
||
1684 estate_set_fuzzy_max(left_false_state
, hard_max
);
1686 if (get_implied_value(right
, &hard_max
)) {
1688 op
== SPECIAL_UNSIGNED_GT
)
1690 estate_set_fuzzy_max(left_true_state
, hard_max
);
1694 if (get_hard_max(left
, &hard_max
))
1695 estate_set_fuzzy_max(right_true_state
, hard_max
);
1696 if (get_hard_max(right
, &hard_max
))
1697 estate_set_fuzzy_max(left_true_state
, hard_max
);
1701 if (get_hard_max(left
, &hard_max
)) {
1702 estate_set_hard_max(left_true_state
);
1703 estate_set_hard_max(left_false_state
);
1705 if (get_hard_max(right
, &hard_max
)) {
1706 estate_set_hard_max(right_true_state
);
1707 estate_set_hard_max(right_false_state
);
1710 if (left_postop
== SPECIAL_INCREMENT
) {
1711 left_true_state
= increment_state(left_true_state
);
1712 left_false_state
= increment_state(left_false_state
);
1714 if (left_postop
== SPECIAL_DECREMENT
) {
1715 left_true_state
= decrement_state(left_true_state
);
1716 left_false_state
= decrement_state(left_false_state
);
1718 if (right_postop
== SPECIAL_INCREMENT
) {
1719 right_true_state
= increment_state(right_true_state
);
1720 right_false_state
= increment_state(right_false_state
);
1722 if (right_postop
== SPECIAL_DECREMENT
) {
1723 right_true_state
= decrement_state(right_true_state
);
1724 right_false_state
= decrement_state(right_false_state
);
1727 if (estate_rl(left_true_state
) && estates_equiv(left_true_state
, left_false_state
)) {
1728 left_true_state
= NULL
;
1729 left_false_state
= NULL
;
1732 if (estate_rl(right_true_state
) && estates_equiv(right_true_state
, right_false_state
)) {
1733 right_true_state
= NULL
;
1734 right_false_state
= NULL
;
1737 /* Don't introduce new states for known true/false conditions */
1738 if (rl_equiv(left_orig
, estate_rl(left_true_state
)))
1739 left_true_state
= NULL
;
1740 if (rl_equiv(left_orig
, estate_rl(left_false_state
)))
1741 left_false_state
= NULL
;
1742 if (rl_equiv(right_orig
, estate_rl(right_true_state
)))
1743 right_true_state
= NULL
;
1744 if (rl_equiv(right_orig
, estate_rl(right_false_state
)))
1745 right_false_state
= NULL
;
1747 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
1748 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
1751 static int is_simple_math(struct expression
*expr
)
1755 if (expr
->type
!= EXPR_BINOP
)
1766 static int flip_op(int op
)
1768 /* We only care about simple math */
1780 static void move_known_to_rl(struct expression
**expr_p
, struct range_list
**rl_p
)
1782 struct expression
*expr
= *expr_p
;
1783 struct range_list
*rl
= *rl_p
;
1786 if (!is_simple_math(expr
))
1789 if (get_implied_value(expr
->right
, &sval
)) {
1790 *expr_p
= expr
->left
;
1791 *rl_p
= rl_binop(rl
, flip_op(expr
->op
), alloc_rl(sval
, sval
));
1792 move_known_to_rl(expr_p
, rl_p
);
1795 if (expr
->op
== '-')
1797 if (get_implied_value(expr
->left
, &sval
)) {
1798 *expr_p
= expr
->right
;
1799 *rl_p
= rl_binop(rl
, flip_op(expr
->op
), alloc_rl(sval
, sval
));
1800 move_known_to_rl(expr_p
, rl_p
);
1805 static void move_known_values(struct expression
**left_p
, struct expression
**right_p
)
1807 struct expression
*left
= *left_p
;
1808 struct expression
*right
= *right_p
;
1811 if (get_implied_value(left
, &sval
)) {
1812 if (!is_simple_math(right
))
1814 if (get_implied_value(right
, &dummy
))
1816 if (right
->op
== '*') {
1819 if (!get_value(right
->right
, &divisor
))
1821 if (divisor
.value
== 0)
1823 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1824 *right_p
= right
->left
;
1827 if (right
->op
== '+' && get_value(right
->left
, &sval
)) {
1828 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->left
);
1829 *right_p
= right
->right
;
1832 if (get_value(right
->right
, &sval
)) {
1833 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1834 *right_p
= right
->left
;
1839 if (get_implied_value(right
, &sval
)) {
1840 if (!is_simple_math(left
))
1842 if (get_implied_value(left
, &dummy
))
1844 if (left
->op
== '*') {
1847 if (!get_value(left
->right
, &divisor
))
1849 if (divisor
.value
== 0)
1851 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1852 *left_p
= left
->left
;
1855 if (left
->op
== '+' && get_value(left
->left
, &sval
)) {
1856 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->left
);
1857 *left_p
= left
->right
;
1861 if (get_value(left
->right
, &sval
)) {
1862 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1863 *left_p
= left
->left
;
1871 * The reason for do_simple_algebra() is to solve things like:
1872 * if (foo > 66 || foo + bar > 64) {
1873 * "foo" is not really a known variable so it won't be handled by
1874 * move_known_variables() but it's a super common idiom.
1877 static int do_simple_algebra(struct expression
**left_p
, struct expression
**right_p
)
1879 struct expression
*left
= *left_p
;
1880 struct expression
*right
= *right_p
;
1881 struct range_list
*rl
;
1884 if (left
->type
!= EXPR_BINOP
|| left
->op
!= '+')
1886 if (can_integer_overflow(get_type(left
), left
))
1888 if (!get_implied_value(right
, &tmp
))
1891 if (!get_implied_value(left
->left
, &tmp
) &&
1892 get_implied_rl(left
->left
, &rl
) &&
1894 *right_p
= binop_expression(right
, '-', left
->left
);
1895 *left_p
= left
->right
;
1898 if (!get_implied_value(left
->right
, &tmp
) &&
1899 get_implied_rl(left
->right
, &rl
) &&
1901 *right_p
= binop_expression(right
, '-', left
->right
);
1902 *left_p
= left
->left
;
1909 static int match_func_comparison(struct expression
*expr
)
1911 struct expression
*left
= strip_expr(expr
->left
);
1912 struct expression
*right
= strip_expr(expr
->right
);
1914 if (left
->type
== EXPR_CALL
|| right
->type
== EXPR_CALL
) {
1915 function_comparison(left
, expr
->op
, right
);
1922 /* Handle conditions like "if (foo + bar < foo) {" */
1923 static int handle_integer_overflow_test(struct expression
*expr
)
1925 struct expression
*left
, *right
;
1926 struct symbol
*type
;
1927 sval_t left_min
, right_min
, min
, max
;
1929 if (expr
->op
!= '<' && expr
->op
!= SPECIAL_UNSIGNED_LT
)
1932 left
= strip_parens(expr
->left
);
1933 right
= strip_parens(expr
->right
);
1935 if (left
->op
!= '+')
1938 type
= get_type(expr
);
1941 if (type_positive_bits(type
) == 32) {
1942 max
.type
= &uint_ctype
;
1943 max
.uvalue
= (unsigned int)-1;
1944 } else if (type_positive_bits(type
) == 64) {
1945 max
.type
= &ulong_ctype
;
1946 max
.value
= (unsigned long long)-1;
1951 if (!expr_equiv(left
->left
, right
) && !expr_equiv(left
->right
, right
))
1954 get_absolute_min(left
->left
, &left_min
);
1955 get_absolute_min(left
->right
, &right_min
);
1956 min
= sval_binop(left_min
, '+', right_min
);
1958 type
= get_type(left
);
1959 min
= sval_cast(type
, min
);
1960 max
= sval_cast(type
, max
);
1962 set_extra_chunk_true_false(left
, NULL
, alloc_estate_range(min
, max
));
1966 static void match_comparison(struct expression
*expr
)
1968 struct expression
*left_orig
= strip_parens(expr
->left
);
1969 struct expression
*right_orig
= strip_parens(expr
->right
);
1970 struct expression
*left
, *right
, *tmp
;
1971 struct expression
*prev
;
1972 struct symbol
*type
;
1975 if (match_func_comparison(expr
))
1978 type
= get_type(expr
);
1980 type
= &llong_ctype
;
1982 if (handle_integer_overflow_test(expr
))
1987 move_known_values(&left
, &right
);
1988 handle_comparison(type
, left
, expr
->op
, right
);
1992 if (do_simple_algebra(&left
, &right
))
1993 handle_comparison(type
, left
, expr
->op
, right
);
1995 prev
= get_assigned_expr(left_orig
);
1996 if (is_simple_math(prev
) && !has_variable(prev
, left_orig
)) {
1999 move_known_values(&left
, &right
);
2000 handle_comparison(type
, left
, expr
->op
, right
);
2003 prev
= get_assigned_expr(right_orig
);
2004 if (is_simple_math(prev
) && !has_variable(prev
, right_orig
)) {
2007 move_known_values(&left
, &right
);
2008 handle_comparison(type
, left
, expr
->op
, right
);
2014 if (get_last_expr_from_expression_stmt(left_orig
)) {
2015 left
= get_last_expr_from_expression_stmt(left_orig
);
2018 if (get_last_expr_from_expression_stmt(right_orig
)) {
2019 right
= get_last_expr_from_expression_stmt(right_orig
);
2027 while ((tmp
= get_assigned_expr(left
))) {
2030 left
= strip_expr(tmp
);
2033 while ((tmp
= get_assigned_expr(right
))) {
2036 right
= strip_expr(tmp
);
2039 handle_comparison(type
, left
, expr
->op
, right
);
2042 static sval_t
get_high_mask(sval_t known
)
2050 for (i
= type_bits(known
.type
) - 1; i
>= 0; i
--) {
2051 if (known
.uvalue
& (1ULL << i
))
2052 ret
.uvalue
|= (1ULL << i
);
2060 static bool handle_bit_test(struct expression
*expr
)
2062 struct range_list
*orig_rl
, *rlt
, *rlf
, *true_rl
, *false_rl
;
2063 struct expression
*shift
, *mask
, *var
;
2064 struct bit_info
*bit_info
;
2066 sval_t high
= { .type
= &int_ctype
};
2067 sval_t low
= { .type
= &int_ctype
};
2069 shift
= strip_expr(expr
->right
);
2070 mask
= strip_expr(expr
->left
);
2071 if (shift
->type
!= EXPR_BINOP
|| shift
->op
!= SPECIAL_LEFTSHIFT
) {
2072 shift
= strip_expr(expr
->left
);
2073 mask
= strip_expr(expr
->right
);
2074 if (shift
->type
!= EXPR_BINOP
|| shift
->op
!= SPECIAL_LEFTSHIFT
)
2077 if (!get_implied_value(shift
->left
, &sval
) || sval
.value
!= 1)
2079 var
= strip_expr(shift
->right
);
2081 bit_info
= get_bit_info(mask
);
2084 if (!bit_info
->possible
){
2085 set_true_false_states_expr(my_id
, var
, alloc_estate_empty(), NULL
);
2089 get_absolute_rl(var
, &orig_rl
);
2090 if (sval_is_negative(rl_min(orig_rl
)) ||
2091 rl_max(orig_rl
).uvalue
> type_bits(get_type(shift
->left
)))
2094 low
.value
= ffsll(bit_info
->possible
) - 1;
2095 high
.value
= sm_fls64(bit_info
->possible
) - 1;
2096 rlt
= alloc_rl(low
, high
);
2097 rlt
= cast_rl(get_type(var
), rlt
);
2098 true_rl
= rl_intersection(orig_rl
, rlt
);
2100 low
.value
= ffsll(bit_info
->set
) - 1;
2101 high
.value
= sm_fls64(bit_info
->set
) - 1;
2102 rlf
= alloc_rl(low
, high
);
2103 rlf
= cast_rl(get_type(var
), rlf
);
2104 false_rl
= rl_filter(orig_rl
, rlf
);
2106 set_extra_expr_true_false(shift
->right
, alloc_estate_rl(true_rl
), alloc_estate_rl(false_rl
));
2111 static void handle_AND_op(struct expression
*var
, sval_t known
)
2113 struct range_list
*orig_rl
;
2114 struct range_list
*true_rl
= NULL
;
2115 struct range_list
*false_rl
= NULL
;
2117 sval_t low_mask
= known
;
2121 get_absolute_rl(var
, &orig_rl
);
2123 if (known
.value
> 0) {
2124 bit
= ffsll(known
.value
) - 1;
2125 low_mask
.uvalue
= (1ULL << bit
) - 1;
2126 true_rl
= remove_range(orig_rl
, sval_type_val(known
.type
, 0), low_mask
);
2128 high_mask
= get_high_mask(known
);
2129 if (high_mask
.value
) {
2130 bit
= ffsll(high_mask
.value
) - 1;
2131 low_mask
.uvalue
= (1ULL << bit
) - 1;
2134 if (sval_is_negative(rl_min(orig_rl
)))
2135 false_rl
= remove_range(false_rl
, sval_type_min(known
.type
), sval_type_val(known
.type
, -1));
2136 false_rl
= remove_range(false_rl
, low_mask
, sval_type_max(known
.type
));
2137 if (type_signed(high_mask
.type
) && type_unsigned(rl_type(false_rl
))) {
2138 false_rl
= remove_range(false_rl
,
2139 sval_type_val(rl_type(false_rl
), sval_type_max(known
.type
).uvalue
),
2140 sval_type_val(rl_type(false_rl
), -1));
2142 } else if (known
.value
== 1 &&
2143 get_hard_max(var
, &max
) &&
2144 sval_cmp(max
, rl_max(orig_rl
)) == 0 &&
2146 false_rl
= remove_range(orig_rl
, max
, max
);
2148 set_extra_expr_true_false(var
,
2149 true_rl
? alloc_estate_rl(true_rl
) : NULL
,
2150 false_rl
? alloc_estate_rl(false_rl
) : NULL
);
2153 static void handle_AND_condition(struct expression
*expr
)
2157 if (handle_bit_test(expr
))
2160 if (get_implied_value(expr
->left
, &known
))
2161 handle_AND_op(expr
->right
, known
);
2162 else if (get_implied_value(expr
->right
, &known
))
2163 handle_AND_op(expr
->left
, known
);
2166 static void handle_MOD_condition(struct expression
*expr
)
2168 struct range_list
*orig_rl
;
2169 struct range_list
*true_rl
;
2170 struct range_list
*false_rl
= NULL
;
2176 if (!get_implied_value(expr
->right
, &right
) || right
.value
== 0)
2178 get_absolute_rl(expr
->left
, &orig_rl
);
2180 zero
.type
= rl_type(orig_rl
);
2182 /* We're basically dorking around the min and max here */
2183 true_rl
= remove_range(orig_rl
, zero
, zero
);
2184 if (!sval_is_max(rl_max(true_rl
)) &&
2185 !(rl_max(true_rl
).value
% right
.value
))
2186 true_rl
= remove_range(true_rl
, rl_max(true_rl
), rl_max(true_rl
));
2188 if (rl_equiv(true_rl
, orig_rl
))
2191 if (sval_is_positive(rl_min(orig_rl
)) &&
2192 (rl_max(orig_rl
).value
- rl_min(orig_rl
).value
) / right
.value
< 5) {
2196 add
= rl_min(orig_rl
);
2197 add
.value
+= right
.value
- (add
.value
% right
.value
);
2198 add
.value
-= right
.value
;
2200 for (i
= 0; i
< 5; i
++) {
2201 add
.value
+= right
.value
;
2202 if (add
.value
> rl_max(orig_rl
).value
)
2204 add_range(&false_rl
, add
, add
);
2207 if (rl_min(orig_rl
).uvalue
!= 0 &&
2208 rl_min(orig_rl
).uvalue
< right
.uvalue
) {
2209 sval_t chop
= right
;
2211 false_rl
= remove_range(orig_rl
, zero
, chop
);
2214 if (!sval_is_max(rl_max(orig_rl
)) &&
2215 (rl_max(orig_rl
).value
% right
.value
)) {
2216 sval_t chop
= rl_max(orig_rl
);
2217 chop
.value
-= chop
.value
% right
.value
;
2220 false_rl
= clone_rl(orig_rl
);
2221 false_rl
= remove_range(false_rl
, chop
, rl_max(orig_rl
));
2225 set_extra_expr_true_false(expr
->left
,
2226 true_rl
? alloc_estate_rl(true_rl
) : NULL
,
2227 false_rl
? alloc_estate_rl(false_rl
) : NULL
);
2230 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
2231 void __extra_match_condition(struct expression
*expr
)
2233 expr
= strip_expr(expr
);
2234 switch (expr
->type
) {
2236 function_comparison(expr
, SPECIAL_NOTEQUAL
, zero_expr());
2241 handle_comparison(get_type(expr
), expr
, SPECIAL_NOTEQUAL
, zero_expr());
2244 match_comparison(expr
);
2246 case EXPR_ASSIGNMENT
:
2247 __extra_match_condition(expr
->left
);
2250 if (expr
->op
== '&')
2251 handle_AND_condition(expr
);
2252 if (expr
->op
== '%')
2253 handle_MOD_condition(expr
);
2258 static void assume_indexes_are_valid(struct expression
*expr
)
2260 struct expression
*array_expr
;
2262 struct expression
*offset
;
2263 struct symbol
*offset_type
;
2264 struct range_list
*rl_before
;
2265 struct range_list
*rl_after
;
2266 struct range_list
*filter
= NULL
;
2269 expr
= strip_expr(expr
);
2270 if (!is_array(expr
))
2273 offset
= get_array_offset(expr
);
2274 offset_type
= get_type(offset
);
2275 if (offset_type
&& type_signed(offset_type
)) {
2276 filter
= alloc_rl(sval_type_min(offset_type
),
2277 sval_type_val(offset_type
, -1));
2280 array_expr
= get_array_base(expr
);
2281 array_size
= get_real_array_size(array_expr
);
2282 if (array_size
> 1) {
2283 size
= sval_type_val(offset_type
, array_size
);
2284 add_range(&filter
, size
, sval_type_max(offset_type
));
2289 get_absolute_rl(offset
, &rl_before
);
2290 rl_after
= rl_filter(rl_before
, filter
);
2291 if (rl_equiv(rl_before
, rl_after
))
2293 set_extra_expr_nomod(offset
, alloc_estate_rl(rl_after
));
2296 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
2297 int implied_not_equal(struct expression
*expr
, long long val
)
2299 return !possibly_false(expr
, SPECIAL_NOTEQUAL
, value_expr(val
));
2302 int implied_not_equal_name_sym(char *name
, struct symbol
*sym
, long long val
)
2304 struct smatch_state
*estate
;
2306 estate
= get_state(SMATCH_EXTRA
, name
, sym
);
2309 if (!rl_has_sval(estate_rl(estate
), sval_type_val(estate_type(estate
), 0)))
2314 bool is_noderef_ptr(struct expression
*expr
)
2316 struct range_list
*rl
;
2318 if (!get_implied_rl(expr
, &rl
))
2320 return is_noderef_ptr_rl(rl
);
2323 static int parent_is_err_or_null_var_sym_helper(const char *name
, struct symbol
*sym
, bool check_err_ptr
)
2325 struct smatch_state
*state
;
2330 strncpy(buf
, name
, sizeof(buf
) - 1);
2331 buf
[sizeof(buf
) - 1] = '\0';
2334 while (*start
== '*') {
2336 state
= __get_state(SMATCH_EXTRA
, start
, sym
);
2339 if (!estate_rl(state
))
2341 if (is_noderef_ptr_rl(estate_rl(state
)))
2346 while (*start
== '&')
2349 len
= strlen(start
);
2353 if (start
[len
] == '-' ||
2354 start
[len
] == '.') {
2361 state
= __get_state(SMATCH_EXTRA
, start
, sym
);
2364 if (is_noderef_ptr_rl(estate_rl(state
)))
2369 int parent_is_null_var_sym(const char *name
, struct symbol
*sym
)
2371 return parent_is_err_or_null_var_sym_helper(name
, sym
, false);
2374 int parent_is_err_or_null_var_sym(const char *name
, struct symbol
*sym
)
2376 return parent_is_err_or_null_var_sym_helper(name
, sym
, (option_project
== PROJ_KERNEL
));
2379 int parent_is_null(struct expression
*expr
)
2385 expr
= strip_expr(expr
);
2386 var
= expr_to_var_sym(expr
, &sym
);
2389 ret
= parent_is_null_var_sym(var
, sym
);
2395 static int param_used_callback(void *found
, int argc
, char **argv
, char **azColName
)
2401 static int is_kzalloc_info(struct sm_state
*sm
)
2406 * kzalloc() information is treated as special because so there is just
2407 * a lot of stuff initialized to zero and it makes building the database
2408 * take hours and hours.
2410 * In theory, we should just remove this line and not pass any unused
2411 * information, but I'm not sure enough that this code works so I want
2412 * to hold off on that for now.
2414 if (!estate_get_single_value(sm
->state
, &sval
))
2416 if (sval
.value
!= 0)
2421 static int is_really_long(struct sm_state
*sm
)
2427 while ((p
= strstr(p
, "->"))) {
2433 strlen(sm
->name
) < 40)
2438 static int filter_unused_param_value_info(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
2442 /* for function pointers assume everything is used */
2443 if (call
->fn
->type
!= EXPR_SYMBOL
)
2446 if (strcmp(printed_name
, "$") == 0 ||
2447 strcmp(printed_name
, "*$") == 0)
2451 * This is to handle __builtin_mul_overflow(). In an ideal world we
2452 * would only need this for invalid code.
2455 if (!call
->fn
->symbol
)
2458 if (!is_kzalloc_info(sm
) && !is_really_long(sm
))
2461 run_sql(¶m_used_callback
, &found
,
2462 "select * from return_implies where %s and type = %d and parameter = %d and key = '%s';",
2463 get_static_filter(call
->fn
->symbol
), PARAM_USED
, param
, printed_name
);
2467 /* If the database is not built yet, then assume everything is used */
2468 run_sql(¶m_used_callback
, &found
,
2469 "select * from return_implies where %s and type = %d;",
2470 get_static_filter(call
->fn
->symbol
), PARAM_USED
);
2477 struct range_list
*intersect_with_real_abs_var_sym(const char *name
, struct symbol
*sym
, struct range_list
*start
)
2479 struct smatch_state
*state
;
2482 * Here is the difference between implied value and real absolute, say
2487 * Then you know that a is 0-255. That's real absolute. But you don't
2488 * know for sure that it actually goes up to 255. So it's not implied.
2489 * Implied indicates a degree of certainty.
2491 * But then say you cap "a" at 8. That means you know it goes up to
2492 * 8. So now the implied value is s32min-8. But you can combine it
2493 * with the real absolute to say that actually it's 0-8.
2495 * We are combining it here. But now that I think about it, this is
2496 * probably not the ideal place to combine it because it should proably
2497 * be done earlier. Oh well, this is an improvement on what was there
2498 * before so I'm going to commit this code.
2502 state
= get_real_absolute_state_var_sym(name
, sym
);
2503 if (!state
|| !estate_rl(state
))
2506 return rl_intersection(estate_rl(state
), start
);
2509 struct range_list
*intersect_with_real_abs_expr(struct expression
*expr
, struct range_list
*start
)
2511 struct smatch_state
*state
;
2512 struct range_list
*abs_rl
;
2514 state
= get_real_absolute_state(expr
);
2515 if (!state
|| !estate_rl(state
))
2518 abs_rl
= cast_rl(rl_type(start
), estate_rl(state
));
2519 return rl_intersection(abs_rl
, start
);
2522 static void caller_info_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
2524 struct range_list
*rl
;
2527 if (estate_is_whole(sm
->state
) || !estate_rl(sm
->state
))
2529 if (filter_unused_param_value_info(call
, param
, printed_name
, sm
))
2531 rl
= estate_rl(sm
->state
);
2532 rl
= intersect_with_real_abs_var_sym(sm
->name
, sm
->sym
, rl
);
2535 sql_insert_caller_info(call
, PARAM_VALUE
, param
, printed_name
, show_rl(rl
));
2536 if (!estate_get_single_value(sm
->state
, &dummy
)) {
2537 if (estate_has_hard_max(sm
->state
))
2538 sql_insert_caller_info(call
, HARD_MAX
, param
, printed_name
,
2539 sval_to_str(estate_max(sm
->state
)));
2540 if (estate_has_fuzzy_max(sm
->state
))
2541 sql_insert_caller_info(call
, FUZZY_MAX
, param
, printed_name
,
2542 sval_to_str(estate_get_fuzzy_max(sm
->state
)));
2546 static void returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
2548 struct symbol
*returned_sym
;
2549 char *returned_name
;
2550 struct sm_state
*sm
;
2558 if (!is_pointer(expr
))
2560 if (return_ranges
&& strstr(return_ranges
, "[==$"))
2563 returned_name
= expr_to_var_sym(expr
, &returned_sym
);
2564 if (!returned_name
|| !returned_sym
)
2566 len
= strlen(returned_name
);
2568 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), sm
) {
2569 if (!estate_rl(sm
->state
))
2571 if (returned_sym
!= sm
->sym
)
2573 if (strncmp(returned_name
, sm
->name
, len
) != 0)
2575 if (sm
->name
[len
] != '-')
2578 snprintf(name_buf
, sizeof(name_buf
), "$%s", sm
->name
+ len
);
2580 compare_str
= name_sym_to_param_comparison(sm
->name
, sm
->sym
);
2581 if (!compare_str
&& estate_is_whole(sm
->state
))
2583 snprintf(val_buf
, sizeof(val_buf
), "%s%s", sm
->state
->name
, compare_str
?: "");
2585 sql_insert_return_states(return_id
, return_ranges
, PARAM_VALUE
,
2586 -1, name_buf
, val_buf
);
2587 } END_FOR_EACH_SM(sm
);
2590 free_string(returned_name
);
2593 static void db_limited_before(void)
2595 unmatched_stree
= clone_stree(__get_cur_stree());
2598 static void db_limited_after(void)
2600 free_stree(&unmatched_stree
);
2603 static int basically_the_same(struct range_list
*orig
, struct range_list
*new)
2605 if (type_is_ptr(rl_type(orig
)) &&
2606 is_whole_ptr_rl(orig
) &&
2607 is_whole_ptr_rl(new))
2610 return rl_equiv(orig
, new);
2613 static void db_param_limit_binops(struct expression
*arg
, char *key
, struct range_list
*rl
)
2615 struct range_list
*left_rl
;
2616 sval_t zero
= { .type
= rl_type(rl
), };
2619 if (strcmp(key
, "$") != 0)
2623 if (!get_implied_value(arg
->right
, &sval
))
2625 if (can_integer_overflow(get_type(arg
), arg
))
2628 left_rl
= rl_binop(rl
, '/', alloc_rl(sval
, sval
));
2629 if (!rl_has_sval(rl
, zero
))
2630 left_rl
= remove_range(left_rl
, zero
, zero
);
2632 set_extra_expr_nomod(arg
->left
, alloc_estate_rl(left_rl
));
2635 static void db_param_limit_filter(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
2637 struct smatch_state
*state
;
2638 struct expression
*arg
;
2641 struct var_sym_list
*vsl
= NULL
;
2642 struct sm_state
*sm
;
2643 struct symbol
*compare_type
, *var_type
;
2644 struct range_list
*rl
;
2645 struct range_list
*limit
;
2646 struct range_list
*new;
2648 struct symbol
*other_sym
;
2650 while (expr
->type
== EXPR_ASSIGNMENT
)
2651 expr
= strip_expr(expr
->right
);
2652 if (expr
->type
!= EXPR_CALL
)
2655 arg
= get_argument_from_call_expr(expr
->args
, param
);
2659 if (strcmp(key
, "$") == 0)
2660 compare_type
= get_arg_type(expr
->fn
, param
);
2662 compare_type
= get_member_type_from_key(arg
, key
);
2664 call_results_to_rl(expr
, compare_type
, value
, &limit
);
2665 if (strcmp(key
, "$") == 0)
2666 move_known_to_rl(&arg
, &limit
);
2667 name
= get_chunk_from_key(arg
, key
, &sym
, &vsl
);
2670 if (op
!= PARAM_LIMIT
&& !sym
)
2673 sm
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
2675 rl
= estate_rl(sm
->state
);
2677 rl
= alloc_whole_rl(compare_type
);
2679 if (op
== PARAM_LIMIT
&& !rl_fits_in_type(rl
, compare_type
))
2682 new = rl_intersection(rl
, limit
);
2684 var_type
= get_member_type_from_key(arg
, key
);
2685 new = cast_rl(var_type
, new);
2687 /* We want to preserve the implications here */
2688 if (sm
&& basically_the_same(rl
, new))
2690 other_name
= get_other_name_sym(name
, sym
, &other_sym
);
2692 state
= alloc_estate_rl(new);
2693 if (sm
&& estate_has_hard_max(sm
->state
))
2694 estate_set_hard_max(state
);
2696 if (op
== PARAM_LIMIT
) {
2697 set_extra_nomod_vsl(name
, sym
, vsl
, NULL
, state
);
2699 set_extra_mod(name
, sym
, NULL
, state
);
2701 if (other_name
&& other_sym
) {
2702 state
= clone_estate(state
);
2703 if (op
== PARAM_LIMIT
)
2704 set_extra_nomod_vsl(other_name
, other_sym
, vsl
, NULL
, state
);
2706 set_extra_mod(other_name
, other_sym
, NULL
, state
);
2709 if (op
== PARAM_LIMIT
&& arg
->type
== EXPR_BINOP
)
2710 db_param_limit_binops(arg
, key
, new);
2715 static void db_param_limit(struct expression
*expr
, int param
, char *key
, char *value
)
2717 db_param_limit_filter(expr
, param
, key
, value
, PARAM_LIMIT
);
2720 static void db_param_filter(struct expression
*expr
, int param
, char *key
, char *value
)
2722 db_param_limit_filter(expr
, param
, key
, value
, PARAM_FILTER
);
2725 static void db_param_add_set(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
2727 struct expression
*arg
, *gen_expr
;
2729 char *other_name
= NULL
;
2730 struct symbol
*sym
, *other_sym
;
2731 struct symbol
*param_type
, *arg_type
;
2732 struct smatch_state
*state
;
2733 struct range_list
*new = NULL
;
2734 struct range_list
*added
= NULL
;
2736 while (expr
->type
== EXPR_ASSIGNMENT
)
2737 expr
= strip_expr(expr
->right
);
2738 if (expr
->type
!= EXPR_CALL
)
2741 arg
= get_argument_from_call_expr(expr
->args
, param
);
2745 arg_type
= get_arg_type_from_key(expr
->fn
, param
, arg
, key
);
2746 param_type
= get_member_type_from_key(arg
, key
);
2747 if (param_type
&& param_type
->type
== SYM_STRUCT
)
2749 name
= get_variable_from_key(arg
, key
, &sym
);
2752 gen_expr
= gen_expression_from_key(arg
, key
);
2754 state
= get_state(SMATCH_EXTRA
, name
, sym
);
2756 new = estate_rl(state
);
2758 call_results_to_rl(expr
, arg_type
, value
, &added
);
2759 added
= cast_rl(param_type
, added
);
2760 if (op
== PARAM_SET
)
2763 new = rl_union(new, added
);
2765 other_name
= get_other_name_sym_nostack(name
, sym
, &other_sym
);
2766 set_extra_mod(name
, sym
, gen_expr
, alloc_estate_rl(new));
2767 if (other_name
&& other_sym
)
2768 set_extra_mod(other_name
, other_sym
, gen_expr
, alloc_estate_rl(new));
2770 free_string(other_name
);
2774 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
2776 in_param_set
= true;
2777 db_param_add_set(expr
, param
, key
, value
, PARAM_ADD
);
2778 in_param_set
= false;
2781 static void db_param_set(struct expression
*expr
, int param
, char *key
, char *value
)
2783 in_param_set
= true;
2784 db_param_add_set(expr
, param
, key
, value
, PARAM_SET
);
2785 in_param_set
= false;
2788 static void match_lost_param(struct expression
*call
, int param
)
2790 struct expression
*arg
;
2792 if (is_const_param(call
->fn
, param
))
2795 arg
= get_argument_from_call_expr(call
->args
, param
);
2799 arg
= strip_expr(arg
);
2800 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&')
2801 set_extra_expr_mod(arg
->unop
, alloc_estate_whole(get_type(arg
->unop
)));
2803 ; /* if pointer then set struct members, maybe?*/
2806 static void db_param_value(struct expression
*expr
, int param
, char *key
, char *value
)
2808 struct expression
*call
;
2811 struct symbol
*type
;
2812 struct range_list
*rl
= NULL
;
2818 while (call
->type
== EXPR_ASSIGNMENT
)
2819 call
= strip_expr(call
->right
);
2820 if (call
->type
!= EXPR_CALL
)
2823 type
= get_member_type_from_key(expr
->left
, key
);
2824 name
= get_variable_from_key(expr
->left
, key
, &sym
);
2828 call_results_to_rl(call
, type
, value
, &rl
);
2830 set_extra_mod(name
, sym
, NULL
, alloc_estate_rl(rl
));
2835 static void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2837 struct expression
*expr
;
2838 struct range_list
*rl
= NULL
;
2839 struct smatch_state
*state
;
2840 struct symbol
*type
;
2841 char *key_orig
= key
;
2845 expr
= symbol_expression(sym
);
2846 fullname
= get_variable_from_key(expr
, key
, NULL
);
2850 type
= get_member_type_from_key(expr
, key_orig
);
2851 str_to_rl(type
, value
, &rl
);
2852 state
= alloc_estate_rl(rl
);
2853 if (estate_get_single_value(state
, &dummy
))
2854 estate_set_hard_max(state
);
2855 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
2858 static void set_param_fuzzy_max(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2860 struct expression
*expr
;
2861 struct range_list
*rl
= NULL
;
2862 struct smatch_state
*state
;
2863 struct symbol
*type
;
2867 expr
= symbol_expression(sym
);
2868 fullname
= get_variable_from_key(expr
, key
, NULL
);
2872 state
= get_state(SMATCH_EXTRA
, fullname
, sym
);
2875 type
= estate_type(state
);
2876 str_to_rl(type
, value
, &rl
);
2877 if (!rl_to_sval(rl
, &max
))
2879 estate_set_fuzzy_max(state
, max
);
2882 static void set_param_hard_max(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2884 struct smatch_state
*state
;
2885 struct expression
*expr
;
2888 expr
= symbol_expression(sym
);
2889 fullname
= get_variable_from_key(expr
, key
, NULL
);
2893 state
= get_state(SMATCH_EXTRA
, fullname
, sym
);
2896 estate_set_hard_max(state
);
2899 static struct sm_state
*get_sm_from_call(struct expression
*expr
)
2903 if (is_fake_call(expr
))
2906 snprintf(buf
, sizeof(buf
), "return %p", expr
);
2907 return get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
2910 struct sm_state
*get_extra_sm_state(struct expression
*expr
)
2914 struct sm_state
*ret
= NULL
;
2916 expr
= strip_expr(expr
);
2920 if (expr
->type
== EXPR_CALL
)
2921 return get_sm_from_call(expr
);
2923 name
= expr_to_known_chunk_sym(expr
, &sym
);
2927 ret
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
2933 struct smatch_state
*get_extra_state(struct expression
*expr
)
2935 struct sm_state
*sm
;
2937 sm
= get_extra_sm_state(expr
);
2943 void register_smatch_extra(int id
)
2947 set_dynamic_states(my_id
);
2948 add_merge_hook(my_id
, &merge_estates
);
2949 add_unmatched_state_hook(my_id
, &unmatched_state
);
2950 select_caller_info_hook(set_param_value
, PARAM_VALUE
);
2951 select_caller_info_hook(set_param_fuzzy_max
, FUZZY_MAX
);
2952 select_caller_info_hook(set_param_hard_max
, HARD_MAX
);
2953 select_return_states_before(&db_limited_before
);
2954 select_return_states_hook(PARAM_LIMIT
, &db_param_limit
);
2955 select_return_states_hook(PARAM_FILTER
, &db_param_filter
);
2956 select_return_states_hook(PARAM_ADD
, &db_param_add
);
2957 select_return_states_hook(PARAM_SET
, &db_param_set
);
2958 add_lost_param_hook(&match_lost_param
);
2959 select_return_states_hook(PARAM_VALUE
, &db_param_value
);
2960 select_return_states_after(&db_limited_after
);
2963 static void match_link_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
2965 struct var_sym_list
*links
;
2966 struct var_sym
*tmp
;
2967 struct smatch_state
*state
;
2969 links
= sm
->state
->data
;
2971 FOR_EACH_PTR(links
, tmp
) {
2972 if (sm
->sym
== tmp
->sym
&&
2973 strcmp(sm
->name
, tmp
->var
) == 0)
2975 state
= get_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
);
2978 set_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
, alloc_estate_whole(estate_type(state
)));
2979 } END_FOR_EACH_PTR(tmp
);
2980 set_state(link_id
, sm
->name
, sm
->sym
, &undefined
);
2983 void register_smatch_extra_links(int id
)
2986 set_dynamic_states(link_id
);
2989 void register_smatch_extra_late(int id
)
2991 add_merge_hook(link_id
, &merge_link_states
);
2992 add_modification_hook(link_id
, &match_link_modify
);
2993 add_hook(&match_dereferences
, DEREF_HOOK
);
2994 add_hook(&match_pointer_as_array
, OP_HOOK
);
2995 select_return_implies_hook(DEREFERENCE
, &set_param_dereferenced
);
2996 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
2997 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
2998 add_hook(&match_assign
, GLOBAL_ASSIGNMENT_HOOK
);
2999 add_hook(&unop_expr
, OP_HOOK
);
3000 add_hook(&asm_expr
, ASM_HOOK
);
3002 add_caller_info_callback(my_id
, caller_info_callback
);
3003 add_split_return_callback(&returned_struct_members
);
3005 // add_hook(&assume_indexes_are_valid, OP_HOOK);