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(mod_hook_list
, mod_hook
*);
65 static struct mod_hook_list
*extra_mod_hooks
;
66 static struct mod_hook_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 mod_hook_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 expression
*expr
, struct smatch_state
*state
)
175 if (__in_fake_assign
)
177 if (is_fake_var_assign(expr
))
180 if (!estate_type(state
) || estate_type(state
)->type
!= SYM_PTR
)
182 if (!is_noderef_ptr_rl(estate_rl(state
)))
185 FOR_EACH_MY_SM(SMATCH_EXTRA
, __get_cur_stree(), sm
) {
188 if (strcmp(name
, sm
->name
) == 0)
190 if (!is_sub_member(name
, sym
, sm
))
192 set_extra_nomod(sm
->name
, sm
->sym
, NULL
, alloc_estate_empty());
193 } END_FOR_EACH_SM(sm
);
196 static bool is_fake_assign(struct expression
*expr
)
198 struct expression
*faked
;
200 if (is_fake_var_assign(expr
))
203 faked
= get_faked_expression();
204 if (!faked
|| faked
->type
!= EXPR_ASSIGNMENT
|| faked
->op
!= '=')
207 faked
= strip_expr(faked
->right
);
208 if (faked
->type
== EXPR_PREOP
&& faked
->op
== '&')
214 static void call_update_mtag_data(struct expression
*expr
,
215 struct smatch_state
*state
)
217 if (is_fake_assign(expr
))
220 update_mtag_data(expr
, state
);
223 static bool in_param_set
;
224 void set_extra_mod_helper(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
227 expr
= gen_expression_from_name_sym(name
, sym
);
228 remove_from_equiv(name
, sym
);
229 set_union_info(name
, sym
, expr
, state
);
230 mark_sub_members_gone(name
, sym
, expr
, state
);
231 call_extra_mod_hooks(name
, sym
, expr
, state
);
232 call_update_mtag_data(expr
, state
);
233 if ((__in_fake_assign
|| in_param_set
) &&
234 estate_is_unknown(state
) && !get_state(SMATCH_EXTRA
, name
, sym
))
236 set_state(SMATCH_EXTRA
, name
, sym
, state
);
239 static void set_extra_nomod_helper(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
241 call_extra_nomod_hooks(name
, sym
, expr
, state
);
242 set_state(SMATCH_EXTRA
, name
, sym
, state
);
245 static char *get_pointed_at(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
247 struct expression
*assigned
;
250 * Imagine we have an assignment: "foo = &addr;" then the other name
256 if (strcmp(name
+ 1, sym
->ident
->name
) != 0)
259 assigned
= get_assigned_expr_name_sym(sym
->ident
->name
, sym
);
262 assigned
= strip_parens(assigned
);
263 if (assigned
->type
!= EXPR_PREOP
|| assigned
->op
!= '&')
266 return expr_to_var_sym(assigned
->unop
, new_sym
);
269 char *get_other_name_sym_from_chunk(const char *name
, const char *chunk
, int len
, struct symbol
*sym
, struct symbol
**new_sym
)
271 struct expression
*assigned
;
272 char *orig_name
= NULL
;
276 assigned
= get_assigned_expr_name_sym(chunk
, sym
);
279 if (assigned
->type
== EXPR_CALL
)
280 return map_call_to_other_name_sym(name
, sym
, new_sym
);
281 if (assigned
->type
== EXPR_PREOP
&& assigned
->op
== '&') {
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
+ 1, name
+ len
);
288 ret
= alloc_string(buf
);
289 free_string(orig_name
);
293 orig_name
= expr_to_var_sym(assigned
, new_sym
);
294 if (!orig_name
|| !*new_sym
)
297 snprintf(buf
, sizeof(buf
), "%s->%s", orig_name
, name
+ len
);
298 ret
= alloc_string(buf
);
299 free_string(orig_name
);
302 free_string(orig_name
);
306 static char *get_long_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
, bool use_stack
)
308 struct expression
*orig
;
309 struct symbol
*orig_sym
;
310 char *orig_name
, *ret
;
312 if (!sym
|| !sym
->ident
)
315 orig
= get_assigned_expr_name_sym(sym
->ident
->name
, sym
);
317 orig_name
= expr_to_var_sym(orig
, &orig_sym
);
321 ret
= swap_names(name
, sym
->ident
->name
, orig_name
);
322 free_string(orig_name
);
331 char *get_other_name_sym_helper(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
, bool use_stack
)
339 if (!sym
|| !sym
->ident
)
342 ret
= get_pointed_at(name
, sym
, new_sym
);
346 ret
= map_long_to_short_name_sym(name
, sym
, new_sym
, use_stack
);
350 len
= snprintf(buf
, sizeof(buf
), "%s", name
);
351 if (len
>= sizeof(buf
) - 2)
354 while (use_stack
&& len
>= 1) {
355 if (buf
[len
] == '>' && buf
[len
- 1] == '-') {
358 ret
= get_other_name_sym_from_chunk(name
, buf
, len
+ 2, sym
, new_sym
);
365 ret
= get_long_name_sym(name
, sym
, new_sym
, use_stack
);
372 char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
374 return get_other_name_sym_helper(name
, sym
, new_sym
, true);
377 char *get_other_name_sym_nostack(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
379 return get_other_name_sym_helper(name
, sym
, new_sym
, false);
382 void set_extra_mod(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
385 struct symbol
*new_sym
;
387 set_extra_mod_helper(name
, sym
, expr
, state
);
388 new_name
= get_other_name_sym_nostack(name
, sym
, &new_sym
);
389 if (new_name
&& new_sym
)
390 set_extra_mod_helper(new_name
, new_sym
, NULL
, state
);
391 free_string(new_name
);
394 static struct expression
*chunk_get_array_base(struct expression
*expr
)
397 * The problem with is_array() is that it only returns true for things
398 * like foo[1] but not for foo[1].bar.
401 expr
= strip_expr(expr
);
402 while (expr
&& expr
->type
== EXPR_DEREF
)
403 expr
= strip_expr(expr
->deref
);
404 return get_array_base(expr
);
407 static int chunk_has_array(struct expression
*expr
)
409 return !!chunk_get_array_base(expr
);
412 static void clear_array_states(struct expression
*array
)
416 sm
= get_sm_state_expr(link_id
, array
);
418 match_link_modify(sm
, NULL
);
421 static void set_extra_array_mod(struct expression
*expr
, struct smatch_state
*state
)
423 struct expression
*array
;
424 struct var_sym_list
*vsl
;
429 array
= chunk_get_array_base(expr
);
431 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
433 clear_array_states(array
);
437 FOR_EACH_PTR(vsl
, vs
) {
438 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
439 } END_FOR_EACH_PTR(vs
);
441 call_extra_mod_hooks(name
, sym
, expr
, state
);
442 set_state(SMATCH_EXTRA
, name
, sym
, state
);
447 void set_extra_expr_mod(struct expression
*expr
, struct smatch_state
*state
)
452 if (chunk_has_array(expr
)) {
453 set_extra_array_mod(expr
, state
);
457 expr
= strip_expr(expr
);
458 name
= expr_to_var_sym(expr
, &sym
);
461 set_extra_mod(name
, sym
, expr
, state
);
466 void set_extra_nomod(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
469 struct symbol
*new_sym
;
470 struct relation
*rel
;
471 struct smatch_state
*orig_state
;
473 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
475 /* don't save unknown states if leaving it blank is the same */
476 if (!orig_state
&& estate_is_unknown(state
))
479 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
480 if (new_name
&& new_sym
)
481 set_extra_nomod_helper(new_name
, new_sym
, expr
, state
);
482 free_string(new_name
);
484 if (!estate_related(orig_state
)) {
485 set_extra_nomod_helper(name
, sym
, expr
, state
);
489 set_related(state
, estate_related(orig_state
));
490 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
491 struct smatch_state
*estate
;
493 estate
= get_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
);
496 set_extra_nomod_helper(rel
->name
, rel
->sym
, expr
, clone_estate_cast(estate_type(estate
), state
));
497 } END_FOR_EACH_PTR(rel
);
500 void set_extra_nomod_vsl(const char *name
, struct symbol
*sym
, struct var_sym_list
*vsl
, struct expression
*expr
, struct smatch_state
*state
)
504 FOR_EACH_PTR(vsl
, vs
) {
505 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
506 } END_FOR_EACH_PTR(vs
);
508 set_extra_nomod(name
, sym
, expr
, state
);
512 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
514 void set_extra_expr_nomod(struct expression
*expr
, struct smatch_state
*state
)
516 struct var_sym_list
*vsl
;
521 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
524 FOR_EACH_PTR(vsl
, vs
) {
525 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
526 } END_FOR_EACH_PTR(vs
);
528 set_extra_nomod(name
, sym
, expr
, state
);
533 static void set_extra_true_false(const char *name
, struct symbol
*sym
,
534 struct smatch_state
*true_state
,
535 struct smatch_state
*false_state
)
538 struct symbol
*new_sym
;
539 struct relation
*rel
;
540 struct smatch_state
*orig_state
;
542 if (!true_state
&& !false_state
)
545 if (in_warn_on_macro())
548 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
549 if (new_name
&& new_sym
)
550 set_true_false_states(SMATCH_EXTRA
, new_name
, new_sym
, true_state
, false_state
);
551 free_string(new_name
);
553 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
555 if (!estate_related(orig_state
)) {
556 set_true_false_states(SMATCH_EXTRA
, name
, sym
, true_state
, false_state
);
561 set_related(true_state
, estate_related(orig_state
));
563 set_related(false_state
, estate_related(orig_state
));
565 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
566 set_true_false_states(SMATCH_EXTRA
, rel
->name
, rel
->sym
,
567 true_state
, false_state
);
568 } END_FOR_EACH_PTR(rel
);
571 static void set_extra_chunk_true_false(struct expression
*expr
,
572 struct smatch_state
*true_state
,
573 struct smatch_state
*false_state
)
575 struct var_sym_list
*vsl
;
581 if (in_warn_on_macro())
584 type
= get_type(expr
);
588 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
591 FOR_EACH_PTR(vsl
, vs
) {
592 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
593 } END_FOR_EACH_PTR(vs
);
595 set_true_false_states(SMATCH_EXTRA
, name
, sym
,
596 clone_estate(true_state
),
597 clone_estate(false_state
));
602 static void set_extra_expr_true_false(struct expression
*expr
,
603 struct smatch_state
*true_state
,
604 struct smatch_state
*false_state
)
610 if (!true_state
&& !false_state
)
613 if (get_value(expr
, &sval
))
616 expr
= strip_expr(expr
);
617 name
= expr_to_var_sym(expr
, &sym
);
620 set_extra_chunk_true_false(expr
, true_state
, false_state
);
623 set_extra_true_false(name
, sym
, true_state
, false_state
);
627 static int get_countdown_info(struct expression
*condition
, struct expression
**unop
, int *op
, sval_t
*right
)
629 struct expression
*unop_expr
;
633 right
->type
= &int_ctype
;
636 condition
= strip_expr(condition
);
638 if (condition
->type
== EXPR_COMPARE
) {
639 comparison
= remove_unsigned_from_comparison(condition
->op
);
641 if (comparison
!= SPECIAL_GTE
&& comparison
!= '>')
643 if (!get_value(condition
->right
, &limit
))
646 unop_expr
= condition
->left
;
647 if (unop_expr
->type
!= EXPR_PREOP
&& unop_expr
->type
!= EXPR_POSTOP
)
649 if (unop_expr
->op
!= SPECIAL_DECREMENT
)
659 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
661 if (condition
->op
!= SPECIAL_DECREMENT
)
670 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
672 struct expression
*iter_var
;
673 struct expression
*condition
, *unop
;
676 struct smatch_state
*estate
;
680 right
.type
= &int_ctype
;
683 condition
= strip_expr(loop
->iterator_pre_condition
);
687 if (!get_countdown_info(condition
, &unop
, &op
, &right
))
690 iter_var
= unop
->unop
;
692 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
695 if (sval_cmp(estate_min(sm
->state
), right
) < 0)
697 start
= estate_max(sm
->state
);
699 type
= get_type(iter_var
);
700 right
= sval_cast(type
, right
);
701 start
= sval_cast(type
, start
);
703 if (sval_cmp(start
, right
) <= 0)
705 if (!sval_is_max(start
))
708 if (op
== SPECIAL_GTE
)
711 if (unop
->type
== EXPR_PREOP
) {
713 estate
= alloc_estate_range(right
, start
);
714 if (estate_has_hard_max(sm
->state
))
715 estate_set_hard_max(estate
);
716 estate_copy_fuzzy_max(estate
, sm
->state
);
717 set_extra_expr_mod(iter_var
, estate
);
719 if (unop
->type
== EXPR_POSTOP
) {
720 estate
= alloc_estate_range(right
, start
);
721 if (estate_has_hard_max(sm
->state
))
722 estate_set_hard_max(estate
);
723 estate_copy_fuzzy_max(estate
, sm
->state
);
724 set_extra_expr_mod(iter_var
, estate
);
726 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
729 static struct sm_state
*handle_canonical_for_inc(struct expression
*iter_expr
,
730 struct expression
*condition
)
732 struct expression
*iter_var
;
734 struct smatch_state
*estate
;
735 sval_t start
, end
, max
;
736 bool unknown_end
= false;
738 iter_var
= iter_expr
->unop
;
739 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
742 if (!estate_get_single_value(sm
->state
, &start
))
744 if (!get_implied_max(condition
->right
, &end
)) {
745 end
= sval_type_max(get_type(condition
->right
));
746 end
= sval_cast(start
.type
, end
);
747 if (sval_is_max(end
))
751 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
754 switch (condition
->op
) {
755 case SPECIAL_UNSIGNED_LT
:
756 case SPECIAL_NOTEQUAL
:
758 if (!sval_is_min(end
) && !unknown_end
)
761 case SPECIAL_UNSIGNED_LTE
:
767 if (sval_cmp(end
, start
) < 0)
769 end
= sval_cast(start
.type
, end
);
770 estate
= alloc_estate_range(start
, end
);
771 if (get_hard_max(condition
->right
, &max
)) {
772 if (!get_macro_name(condition
->pos
))
773 estate_set_hard_max(estate
);
774 if (condition
->op
== '<' ||
775 condition
->op
== SPECIAL_UNSIGNED_LT
||
776 condition
->op
== SPECIAL_NOTEQUAL
)
778 max
= sval_cast(start
.type
, max
);
779 estate_set_fuzzy_max(estate
, max
);
781 set_extra_expr_mod(iter_var
, estate
);
782 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
785 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
786 struct expression
*condition
)
788 struct expression
*iter_var
;
790 struct smatch_state
*estate
;
793 iter_var
= iter_expr
->unop
;
794 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
797 if (!estate_get_single_value(sm
->state
, &start
))
799 if (!get_implied_min(condition
->right
, &end
))
800 end
= sval_type_min(get_type(iter_var
));
801 end
= sval_cast(estate_type(sm
->state
), end
);
802 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
805 switch (condition
->op
) {
806 case SPECIAL_NOTEQUAL
:
808 if (!sval_is_max(end
))
816 if (sval_cmp(end
, start
) > 0)
818 estate
= alloc_estate_range(end
, start
);
819 estate_set_hard_max(estate
);
820 estate_set_fuzzy_max(estate
, estate_get_fuzzy_max(estate
));
821 set_extra_expr_mod(iter_var
, estate
);
822 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
825 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
827 struct expression
*iter_expr
;
828 struct expression
*condition
;
830 if (!loop
->iterator_post_statement
)
832 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
834 iter_expr
= loop
->iterator_post_statement
->expression
;
835 if (!loop
->iterator_pre_condition
)
837 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
839 condition
= loop
->iterator_pre_condition
;
841 if (iter_expr
->op
== SPECIAL_INCREMENT
)
842 return handle_canonical_for_inc(iter_expr
, condition
);
843 if (iter_expr
->op
== SPECIAL_DECREMENT
)
844 return handle_canonical_for_dec(iter_expr
, condition
);
848 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct stree
**stree
)
850 struct sm_state
*ret
;
853 * Canonical loops are a hack. The proper way to handle this is to
854 * use two passes, but unfortunately, doing two passes makes parsing
855 * code twice as slow.
857 * What we do is we set the inside state here, which overwrites whatever
858 * __extra_match_condition() does. Then we set the outside state in
859 * __extra_pre_loop_hook_after().
862 __push_fake_cur_stree();
863 if (!loop
->iterator_post_statement
)
864 ret
= handle_canonical_while_count_down(loop
);
866 ret
= handle_canonical_for_loops(loop
);
867 *stree
= __pop_fake_cur_stree();
871 int __iterator_unchanged(struct sm_state
*sm
)
875 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
880 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
882 struct expression
*unop
;
884 sval_t limit
, after_value
;
886 if (!get_countdown_info(condition
, &unop
, &op
, &limit
))
888 after_value
= estate_min(sm
->state
);
890 set_extra_mod(sm
->name
, sm
->sym
, condition
->unop
, alloc_estate_sval(after_value
));
893 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
894 struct statement
*iterator
,
895 struct expression
*condition
)
897 struct expression
*iter_expr
;
899 struct smatch_state
*state
;
903 while_count_down_after(sm
, condition
);
907 iter_expr
= iterator
->expression
;
909 if (condition
->type
!= EXPR_COMPARE
)
912 if (iter_expr
->op
== SPECIAL_INCREMENT
) {
913 if (!get_implied_value(condition
->right
, &end
) &&
914 sval_is_max(estate_max(sm
->state
)))
915 limit
= estate_max(sm
->state
);
917 limit
= sval_binop(estate_max(sm
->state
), '+',
918 sval_type_val(estate_type(sm
->state
), 1));
920 limit
= sval_binop(estate_min(sm
->state
), '-',
921 sval_type_val(estate_type(sm
->state
), 1));
923 limit
= sval_cast(estate_type(sm
->state
), limit
);
924 if (!estate_has_hard_max(sm
->state
) && !__has_breaks()) {
925 if (iter_expr
->op
== SPECIAL_INCREMENT
)
926 state
= alloc_estate_range(estate_min(sm
->state
), limit
);
928 state
= alloc_estate_range(limit
, estate_max(sm
->state
));
930 state
= alloc_estate_sval(limit
);
932 if (!estate_has_hard_max(sm
->state
)) {
933 estate_clear_hard_max(state
);
935 if (estate_has_fuzzy_max(sm
->state
)) {
936 sval_t hmax
= estate_get_fuzzy_max(sm
->state
);
937 sval_t max
= estate_max(sm
->state
);
939 if (sval_cmp(hmax
, max
) != 0)
940 estate_clear_fuzzy_max(state
);
941 } else if (!estate_has_fuzzy_max(sm
->state
)) {
942 estate_clear_fuzzy_max(state
);
945 set_extra_mod(sm
->name
, sm
->sym
, iter_expr
, state
);
948 static bool get_global_rl(const char *name
, struct symbol
*sym
, struct range_list
**rl
)
950 struct expression
*expr
;
952 if (!sym
|| !(sym
->ctype
.modifiers
& MOD_TOPLEVEL
) || !sym
->ident
)
954 if (strcmp(sym
->ident
->name
, name
) != 0)
957 expr
= symbol_expression(sym
);
958 return get_implied_rl(expr
, rl
);
961 static struct stree
*unmatched_stree
;
962 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
964 struct smatch_state
*state
;
965 struct expression
*expr
;
966 struct range_list
*rl
;
968 if (unmatched_stree
) {
969 state
= get_state_stree(unmatched_stree
, SMATCH_EXTRA
, sm
->name
, sm
->sym
);
973 if (parent_is_gone_var_sym(sm
->name
, sm
->sym
))
974 return alloc_estate_empty();
975 if (get_global_rl(sm
->name
, sm
->sym
, &rl
))
976 return alloc_estate_rl(rl
);
978 expr
= gen_expression_from_name_sym(sm
->name
, sm
->sym
);
980 return alloc_estate_whole(estate_type(sm
->state
));
981 get_absolute_rl(expr
, &rl
);
982 return alloc_estate_rl(rl
);
985 static void clear_the_pointed_at(struct expression
*expr
)
990 struct sm_state
*tmp
;
992 name
= expr_to_var_sym(expr
, &sym
);
996 stree
= __get_cur_stree();
997 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
998 if (tmp
->name
[0] != '*')
1000 if (tmp
->sym
!= sym
)
1002 if (strcmp(tmp
->name
+ 1, name
) != 0)
1004 set_extra_mod(tmp
->name
, tmp
->sym
, expr
, alloc_estate_whole(estate_type(tmp
->state
)));
1005 } END_FOR_EACH_SM(tmp
);
1011 static int is_const_param(struct expression
*expr
, int param
)
1013 struct symbol
*type
;
1015 type
= get_arg_type(expr
, param
);
1018 if (type
->ctype
.modifiers
& MOD_CONST
)
1023 static void match_function_call(struct expression
*expr
)
1025 struct expression
*arg
;
1026 struct expression
*tmp
;
1029 /* if we have the db this is handled in smatch_function_hooks.c */
1032 if (inlinable(expr
->fn
))
1035 FOR_EACH_PTR(expr
->args
, arg
) {
1037 if (is_const_param(expr
->fn
, param
))
1039 tmp
= strip_expr(arg
);
1040 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
1041 set_extra_expr_mod(tmp
->unop
, alloc_estate_whole(get_type(tmp
->unop
)));
1043 clear_the_pointed_at(tmp
);
1044 } END_FOR_EACH_PTR(arg
);
1047 int values_fit_type(struct expression
*left
, struct expression
*right
)
1049 struct range_list
*rl
;
1050 struct symbol
*type
;
1052 type
= get_type(left
);
1055 get_absolute_rl(right
, &rl
);
1056 if (type
== rl_type(rl
))
1058 if (sval_is_negative(rl_min(rl
))) {
1059 if (type_unsigned(type
))
1061 if (sval_cmp(sval_type_min(type
), rl_min(rl
)) > 0)
1064 if (sval_cmp(sval_type_max(type
), rl_max(rl
)) < 0)
1069 static void save_chunk_info(struct expression
*left
, struct expression
*right
)
1071 struct var_sym_list
*vsl
;
1073 struct expression
*add_expr
;
1074 struct symbol
*type
;
1079 if (right
->type
!= EXPR_BINOP
|| right
->op
!= '-')
1081 if (!get_value(right
->left
, &sval
))
1083 if (!expr_to_sym(right
->right
))
1086 add_expr
= binop_expression(left
, '+', right
->right
);
1087 type
= get_type(add_expr
);
1090 name
= expr_to_chunk_sym_vsl(add_expr
, &sym
, &vsl
);
1093 FOR_EACH_PTR(vsl
, vs
) {
1094 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
1095 } END_FOR_EACH_PTR(vs
);
1097 set_state(SMATCH_EXTRA
, name
, sym
, alloc_estate_sval(sval_cast(type
, sval
)));
1102 static void do_array_assign(struct expression
*left
, int op
, struct expression
*right
)
1104 struct range_list
*rl
;
1107 get_absolute_rl(right
, &rl
);
1108 rl
= cast_rl(get_type(left
), rl
);
1110 rl
= alloc_whole_rl(get_type(left
));
1113 set_extra_array_mod(left
, alloc_estate_rl(rl
));
1116 static void match_vanilla_assign(struct expression
*left
, struct expression
*right
)
1118 struct range_list
*orig_rl
= NULL
;
1119 struct range_list
*rl
= NULL
;
1120 struct symbol
*right_sym
;
1121 struct symbol
*left_type
;
1122 struct symbol
*right_type
;
1123 char *right_name
= NULL
;
1127 struct smatch_state
*state
;
1130 if (is_struct(left
))
1133 if (expr_equiv(left
, right
))
1136 save_chunk_info(left
, right
);
1138 name
= expr_to_var_sym(left
, &sym
);
1140 if (chunk_has_array(left
))
1141 do_array_assign(left
, '=', right
);
1145 left_type
= get_type(left
);
1146 right_type
= get_type(right
);
1148 right_name
= expr_to_var_sym(right
, &right_sym
);
1150 if (!__in_fake_assign
&&
1151 !(right
->type
== EXPR_PREOP
&& right
->op
== '&') &&
1152 right_name
&& right_sym
&&
1153 values_fit_type(left
, strip_expr(right
)) &&
1154 !has_symbol(right
, sym
)) {
1155 set_equiv(left
, right
);
1159 if (get_implied_value(right
, &sval
)) {
1160 state
= alloc_estate_sval(sval_cast(left_type
, sval
));
1164 if (__in_fake_assign
|| is_fake_var(left
)) {
1165 struct smatch_state
*right_state
;
1166 struct range_list
*rl
;
1168 right_state
= get_state(SMATCH_EXTRA
, right_name
, right_sym
);
1170 state
= clone_estate_cast(left_type
, right_state
);
1174 if (get_implied_rl(right
, &rl
)) {
1175 rl
= cast_rl(left_type
, rl
);
1176 state
= alloc_estate_rl(rl
);
1180 rl
= alloc_whole_rl(right_type
);
1181 rl
= cast_rl(left_type
, rl
);
1182 state
= alloc_estate_rl(rl
);
1186 comparison
= get_comparison_no_extra(left
, right
);
1188 comparison
= flip_comparison(comparison
);
1189 get_implied_rl(left
, &orig_rl
);
1192 if (get_implied_rl(right
, &rl
)) {
1193 rl
= cast_rl(left_type
, rl
);
1195 filter_by_comparison(&rl
, comparison
, orig_rl
);
1196 state
= alloc_estate_rl(rl
);
1197 if (get_hard_max(right
, &max
)) {
1198 estate_set_hard_max(state
);
1199 estate_set_fuzzy_max(state
, max
);
1202 rl
= alloc_whole_rl(right_type
);
1203 rl
= cast_rl(left_type
, rl
);
1205 filter_by_comparison(&rl
, comparison
, orig_rl
);
1206 state
= alloc_estate_rl(rl
);
1210 set_extra_mod(name
, sym
, left
, state
);
1212 free_string(right_name
);
1215 static void match_assign(struct expression
*expr
)
1217 struct range_list
*rl
= NULL
;
1218 struct expression
*left
;
1219 struct expression
*right
;
1220 struct expression
*binop_expr
;
1221 struct symbol
*left_type
;
1225 left
= strip_expr(expr
->left
);
1227 right
= strip_parens(expr
->right
);
1228 if (right
->type
== EXPR_CALL
&& sym_name_is("__builtin_expect", right
->fn
))
1229 right
= get_argument_from_call_expr(right
->args
, 0);
1230 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
1231 right
= strip_parens(right
->left
);
1233 if (expr
->op
== '=' && is_condition(expr
->right
))
1234 return; /* handled in smatch_condition.c */
1235 if (expr
->op
== '=' && right
->type
== EXPR_CALL
&&
1236 !is_fake_call(right
))
1237 return; /* handled in smatch_function_hooks.c */
1238 if (expr
->op
== '=') {
1239 match_vanilla_assign(left
, right
);
1243 name
= expr_to_var_sym(left
, &sym
);
1247 left_type
= get_type(left
);
1250 case SPECIAL_ADD_ASSIGN
:
1251 case SPECIAL_SUB_ASSIGN
:
1252 case SPECIAL_AND_ASSIGN
:
1253 case SPECIAL_MOD_ASSIGN
:
1254 case SPECIAL_SHL_ASSIGN
:
1255 case SPECIAL_SHR_ASSIGN
:
1256 case SPECIAL_OR_ASSIGN
:
1257 case SPECIAL_XOR_ASSIGN
:
1258 case SPECIAL_MUL_ASSIGN
:
1259 case SPECIAL_DIV_ASSIGN
:
1260 binop_expr
= binop_expression(expr
->left
,
1261 op_remove_assign(expr
->op
),
1263 get_absolute_rl(binop_expr
, &rl
);
1264 rl
= cast_rl(left_type
, rl
);
1265 if (inside_loop()) {
1266 if (expr
->op
== SPECIAL_ADD_ASSIGN
)
1267 add_range(&rl
, rl_max(rl
), sval_type_max(rl_type(rl
)));
1269 if (expr
->op
== SPECIAL_SUB_ASSIGN
&&
1270 !sval_is_negative(rl_min(rl
))) {
1271 sval_t zero
= { .type
= rl_type(rl
) };
1273 add_range(&rl
, rl_min(rl
), zero
);
1276 set_extra_mod(name
, sym
, left
, alloc_estate_rl(rl
));
1279 set_extra_mod(name
, sym
, left
, alloc_estate_whole(left_type
));
1284 static struct smatch_state
*increment_state(struct smatch_state
*state
)
1286 sval_t min
= estate_min(state
);
1287 sval_t max
= estate_max(state
);
1289 if (!estate_rl(state
))
1293 max
= sval_type_max(max
.type
);
1295 if (!sval_is_min(min
) && !sval_is_max(min
))
1297 if (!sval_is_min(max
) && !sval_is_max(max
))
1299 return alloc_estate_range(min
, max
);
1302 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
1304 sval_t min
= estate_min(state
);
1305 sval_t max
= estate_max(state
);
1307 if (!estate_rl(state
))
1311 min
= sval_type_min(min
.type
);
1313 if (!sval_is_min(min
) && !sval_is_max(min
))
1315 if (!sval_is_min(max
) && !sval_is_max(max
))
1317 return alloc_estate_range(min
, max
);
1320 static void clear_pointed_at_state(struct expression
*expr
)
1322 struct symbol
*type
;
1325 * ALERT: This is sort of a mess. If it's is a struct assigment like
1326 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1327 * the same thing for p++ where "p" is a struct. Most modifications
1328 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1329 * use smatch_modification.c because we have to get the ordering right
1330 * or something. So if you have p++ where p is a pointer to a standard
1331 * c type then we handle that here. What a mess.
1333 expr
= strip_expr(expr
);
1334 type
= get_type(expr
);
1335 if (!type
|| type
->type
!= SYM_PTR
)
1337 type
= get_real_base_type(type
);
1338 if (!type
|| type
->type
!= SYM_BASETYPE
)
1340 set_extra_expr_nomod(deref_expression(expr
), alloc_estate_whole(type
));
1343 static void unop_expr(struct expression
*expr
)
1345 struct smatch_state
*state
;
1347 if (expr
->smatch_flags
& Handled
)
1351 case SPECIAL_INCREMENT
:
1352 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
1353 state
= increment_state(state
);
1355 state
= alloc_estate_whole(get_type(expr
));
1356 set_extra_expr_mod(expr
->unop
, state
);
1357 clear_pointed_at_state(expr
->unop
);
1359 case SPECIAL_DECREMENT
:
1360 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
1361 state
= decrement_state(state
);
1363 state
= alloc_estate_whole(get_type(expr
));
1364 set_extra_expr_mod(expr
->unop
, state
);
1365 clear_pointed_at_state(expr
->unop
);
1372 static void asm_expr(struct statement
*stmt
)
1374 struct asm_operand
*op
;
1375 struct symbol
*type
;
1377 FOR_EACH_PTR(stmt
->asm_outputs
, op
) {
1378 type
= get_type(strip_expr(op
->expr
));
1379 set_extra_expr_mod(op
->expr
, alloc_estate_whole(type
));
1380 } END_FOR_EACH_PTR(op
);
1383 extern int __no_limits
;
1384 static void check_dereference(struct expression
*expr
)
1386 struct smatch_state
*state
;
1388 if (__in_fake_assign
)
1390 if (outside_of_function())
1392 state
= get_extra_state(expr
);
1394 struct range_list
*rl
;
1396 rl
= rl_intersection(estate_rl(state
), valid_ptr_rl
);
1397 if (rl_equiv(rl
, estate_rl(state
)))
1400 set_extra_expr_nomod(expr
, alloc_estate_rl(rl
));
1403 struct range_list
*rl
;
1405 if (get_mtag_rl(expr
, &rl
))
1406 rl
= rl_intersection(rl
, valid_ptr_rl
);
1408 rl
= clone_rl(valid_ptr_rl
);
1411 set_extra_expr_nomod(expr
, alloc_estate_rl(rl
));
1416 static void match_dereferences(struct expression
*expr
)
1418 if (expr
->type
!= EXPR_PREOP
)
1420 if (getting_address(expr
))
1422 /* it's saying that foo[1] = bar dereferences foo[1] */
1425 check_dereference(expr
->unop
);
1428 static void match_pointer_as_array(struct expression
*expr
)
1430 if (!is_array(expr
))
1432 check_dereference(get_array_base(expr
));
1435 static void find_dereferences(struct expression
*expr
)
1437 while (expr
->type
== EXPR_PREOP
) {
1438 if (expr
->op
== '*')
1439 check_dereference(expr
->unop
);
1440 expr
= strip_expr(expr
->unop
);
1444 static void set_param_dereferenced(struct expression
*call
, struct expression
*arg
, char *key
, char *unused
)
1449 if (strcmp(key
, "$") == 0 && arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1450 struct expression
*tmp
;
1452 tmp
= strip_expr(arg
->unop
);
1453 if (tmp
->type
== EXPR_DEREF
) {
1454 tmp
= strip_expr(tmp
->deref
);
1455 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '*')
1456 arg
= strip_expr(tmp
->unop
);
1460 name
= get_variable_from_key(arg
, key
, &sym
);
1462 struct smatch_state
*orig
, *new;
1463 struct range_list
*rl
;
1465 orig
= get_state(SMATCH_EXTRA
, name
, sym
);
1467 rl
= rl_intersection(estate_rl(orig
),
1468 alloc_rl(valid_ptr_min_sval
,
1469 valid_ptr_max_sval
));
1470 new = alloc_estate_rl(rl
);
1472 new = alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
);
1476 set_extra_nomod(name
, sym
, NULL
, new);
1481 find_dereferences(arg
);
1484 static sval_t
add_one(sval_t sval
)
1490 static int handle_postop_inc(struct expression
*left
, int op
, struct expression
*right
)
1492 struct statement
*stmt
;
1493 struct expression
*cond
;
1494 struct smatch_state
*true_state
, *false_state
;
1495 struct symbol
*type
;
1500 * If we're decrementing here then that's a canonical while count down
1501 * so it's handled already. We're only handling loops like:
1503 * do { ... } while (i++ < 3);
1506 if (left
->type
!= EXPR_POSTOP
|| left
->op
!= SPECIAL_INCREMENT
)
1509 stmt
= __cur_stmt
->parent
;
1512 if (stmt
->type
== STMT_COMPOUND
)
1513 stmt
= stmt
->parent
;
1514 if (!stmt
|| stmt
->type
!= STMT_ITERATOR
|| !stmt
->iterator_post_condition
)
1517 cond
= strip_expr(stmt
->iterator_post_condition
);
1518 if (cond
->type
!= EXPR_COMPARE
|| cond
->op
!= op
)
1520 if (left
!= strip_expr(cond
->left
) || right
!= strip_expr(cond
->right
))
1523 if (!get_implied_value(left
->unop
, &start
))
1525 if (!get_implied_value(right
, &limit
))
1527 type
= get_type(left
->unop
);
1528 limit
= sval_cast(type
, limit
);
1529 if (sval_cmp(start
, limit
) > 0)
1534 case SPECIAL_UNSIGNED_LT
:
1537 case SPECIAL_UNSIGNED_LTE
:
1538 limit
= add_one(limit
);
1544 true_state
= alloc_estate_range(add_one(start
), limit
);
1545 false_state
= alloc_estate_range(add_one(limit
), add_one(limit
));
1547 /* Currently we just discard the false state but when two passes is
1548 * implimented correctly then it will use it.
1551 set_extra_expr_true_false(left
->unop
, true_state
, false_state
);
1556 bool is_impossible_variable(struct expression
*expr
)
1558 struct smatch_state
*state
;
1560 state
= get_extra_state(expr
);
1561 if (state
&& !estate_rl(state
))
1566 static bool in_macro(struct expression
*left
, struct expression
*right
)
1568 if (!left
|| !right
)
1570 if (left
->pos
.line
!= right
->pos
.line
|| left
->pos
.pos
!= right
->pos
.pos
)
1572 if (get_macro_name(left
->pos
))
1577 static void handle_comparison(struct symbol
*type
, struct expression
*left
, int op
, struct expression
*right
)
1579 struct smatch_state
*left_state_orig
, *right_state_orig
;
1580 struct range_list
*left_orig
;
1581 struct range_list
*left_true
;
1582 struct range_list
*left_false
;
1583 struct range_list
*right_orig
;
1584 struct range_list
*right_true
;
1585 struct range_list
*right_false
;
1586 struct smatch_state
*left_true_state
;
1587 struct smatch_state
*left_false_state
;
1588 struct smatch_state
*right_true_state
;
1589 struct smatch_state
*right_false_state
;
1590 sval_t dummy
, hard_max
;
1591 int left_postop
= 0;
1592 int right_postop
= 0;
1594 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
1595 if (left
->type
== EXPR_POSTOP
) {
1596 left
->smatch_flags
|= Handled
;
1597 left_postop
= left
->op
;
1598 if (handle_postop_inc(left
, op
, right
))
1601 left
= strip_parens(left
->unop
);
1603 while (left
->type
== EXPR_ASSIGNMENT
)
1604 left
= strip_parens(left
->left
);
1606 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
1607 if (right
->type
== EXPR_POSTOP
) {
1608 right
->smatch_flags
|= Handled
;
1609 right_postop
= right
->op
;
1611 right
= strip_parens(right
->unop
);
1614 if (is_impossible_variable(left
) || is_impossible_variable(right
))
1617 left_state_orig
= get_extra_state(left
);
1618 get_real_absolute_rl(left
, &left_orig
);
1619 left_orig
= cast_rl(type
, left_orig
);
1621 right_state_orig
= get_extra_state(right
);
1622 get_real_absolute_rl(right
, &right_orig
);
1623 right_orig
= cast_rl(type
, right_orig
);
1625 split_comparison_rl(left_orig
, op
, right_orig
, &left_true
, &left_false
, &right_true
, &right_false
);
1627 left_true
= rl_truncate_cast(get_type(strip_expr(left
)), left_true
);
1628 left_false
= rl_truncate_cast(get_type(strip_expr(left
)), left_false
);
1629 right_true
= rl_truncate_cast(get_type(strip_expr(right
)), right_true
);
1630 right_false
= rl_truncate_cast(get_type(strip_expr(right
)), right_false
);
1632 if (!left_true
|| !left_false
) {
1633 struct range_list
*tmp_true
, *tmp_false
;
1635 split_comparison_rl(alloc_whole_rl(type
), op
, right_orig
, &tmp_true
, &tmp_false
, NULL
, NULL
);
1636 tmp_true
= rl_truncate_cast(get_type(strip_expr(left
)), tmp_true
);
1637 tmp_false
= rl_truncate_cast(get_type(strip_expr(left
)), tmp_false
);
1638 if (tmp_true
&& tmp_false
)
1639 __save_imaginary_state(left
, tmp_true
, tmp_false
);
1642 if (!right_true
|| !right_false
) {
1643 struct range_list
*tmp_true
, *tmp_false
;
1645 split_comparison_rl(alloc_whole_rl(type
), op
, right_orig
, NULL
, NULL
, &tmp_true
, &tmp_false
);
1646 tmp_true
= rl_truncate_cast(get_type(strip_expr(right
)), tmp_true
);
1647 tmp_false
= rl_truncate_cast(get_type(strip_expr(right
)), tmp_false
);
1648 if (tmp_true
&& tmp_false
)
1649 __save_imaginary_state(right
, tmp_true
, tmp_false
);
1652 left_true_state
= alloc_estate_rl(left_true
);
1653 left_false_state
= alloc_estate_rl(left_false
);
1654 right_true_state
= alloc_estate_rl(right_true
);
1655 right_false_state
= alloc_estate_rl(right_false
);
1659 case SPECIAL_UNSIGNED_LT
:
1660 case SPECIAL_UNSIGNED_LTE
:
1662 if (get_hard_max(right
, &dummy
) && !in_macro(left
, right
))
1663 estate_set_hard_max(left_true_state
);
1664 if (get_hard_max(left
, &dummy
) && !in_macro(left
, right
))
1665 estate_set_hard_max(right_false_state
);
1668 case SPECIAL_UNSIGNED_GT
:
1669 case SPECIAL_UNSIGNED_GTE
:
1671 if (get_hard_max(left
, &dummy
) && !in_macro(left
, right
))
1672 estate_set_hard_max(right_true_state
);
1673 if (get_hard_max(right
, &dummy
) && !in_macro(left
, right
))
1674 estate_set_hard_max(left_false_state
);
1680 case SPECIAL_UNSIGNED_LT
:
1681 case SPECIAL_UNSIGNED_LTE
:
1683 if (get_hard_max(right
, &hard_max
)) {
1684 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1686 estate_set_fuzzy_max(left_true_state
, hard_max
);
1688 if (get_implied_value(right
, &hard_max
)) {
1689 if (op
== SPECIAL_UNSIGNED_LTE
||
1692 estate_set_fuzzy_max(left_false_state
, hard_max
);
1694 if (get_hard_max(left
, &hard_max
)) {
1695 if (op
== SPECIAL_UNSIGNED_LTE
||
1698 estate_set_fuzzy_max(right_false_state
, hard_max
);
1700 if (get_implied_value(left
, &hard_max
)) {
1701 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1703 estate_set_fuzzy_max(right_true_state
, hard_max
);
1707 case SPECIAL_UNSIGNED_GT
:
1708 case SPECIAL_UNSIGNED_GTE
:
1710 if (get_hard_max(left
, &hard_max
)) {
1711 if (op
== '>' || op
== SPECIAL_UNSIGNED_GT
)
1713 estate_set_fuzzy_max(right_true_state
, hard_max
);
1715 if (get_implied_value(left
, &hard_max
)) {
1716 if (op
== SPECIAL_UNSIGNED_GTE
||
1719 estate_set_fuzzy_max(right_false_state
, hard_max
);
1721 if (get_hard_max(right
, &hard_max
)) {
1722 if (op
== SPECIAL_UNSIGNED_LTE
||
1725 estate_set_fuzzy_max(left_false_state
, hard_max
);
1727 if (get_implied_value(right
, &hard_max
)) {
1729 op
== SPECIAL_UNSIGNED_GT
)
1731 estate_set_fuzzy_max(left_true_state
, hard_max
);
1735 if (get_hard_max(left
, &hard_max
))
1736 estate_set_fuzzy_max(right_true_state
, hard_max
);
1737 if (get_hard_max(right
, &hard_max
))
1738 estate_set_fuzzy_max(left_true_state
, hard_max
);
1742 if (get_hard_max(left
, &hard_max
)) {
1743 estate_set_hard_max(left_true_state
);
1744 estate_set_hard_max(left_false_state
);
1746 if (get_hard_max(right
, &hard_max
)) {
1747 estate_set_hard_max(right_true_state
);
1748 estate_set_hard_max(right_false_state
);
1751 if (left_postop
== SPECIAL_INCREMENT
) {
1752 left_true_state
= increment_state(left_true_state
);
1753 left_false_state
= increment_state(left_false_state
);
1755 if (left_postop
== SPECIAL_DECREMENT
) {
1756 left_true_state
= decrement_state(left_true_state
);
1757 left_false_state
= decrement_state(left_false_state
);
1759 if (right_postop
== SPECIAL_INCREMENT
) {
1760 right_true_state
= increment_state(right_true_state
);
1761 right_false_state
= increment_state(right_false_state
);
1763 if (right_postop
== SPECIAL_DECREMENT
) {
1764 right_true_state
= decrement_state(right_true_state
);
1765 right_false_state
= decrement_state(right_false_state
);
1768 if (estate_rl(left_true_state
) && estates_equiv(left_true_state
, left_false_state
)) {
1769 left_true_state
= NULL
;
1770 left_false_state
= NULL
;
1773 if (estate_rl(right_true_state
) && estates_equiv(right_true_state
, right_false_state
)) {
1774 right_true_state
= NULL
;
1775 right_false_state
= NULL
;
1778 /* Don't introduce new states for known true/false conditions */
1779 if (rl_equiv(estate_rl(left_state_orig
), estate_rl(left_true_state
)))
1780 left_true_state
= NULL
;
1781 if (rl_equiv(estate_rl(left_state_orig
), estate_rl(left_false_state
)))
1782 left_false_state
= NULL
;
1783 if (rl_equiv(estate_rl(right_state_orig
), estate_rl(right_true_state
)))
1784 right_true_state
= NULL
;
1785 if (rl_equiv(estate_rl(right_state_orig
), estate_rl(right_false_state
)))
1786 right_false_state
= NULL
;
1788 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
1789 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
1792 static int is_simple_math(struct expression
*expr
)
1796 if (expr
->type
!= EXPR_BINOP
)
1807 static int flip_op(int op
)
1809 /* We only care about simple math */
1821 static void move_known_to_rl(struct expression
**expr_p
, struct range_list
**rl_p
)
1823 struct expression
*expr
= *expr_p
;
1824 struct range_list
*rl
= *rl_p
;
1827 if (!is_simple_math(expr
))
1830 if (get_implied_value(expr
->right
, &sval
)) {
1831 *expr_p
= expr
->left
;
1832 *rl_p
= rl_binop(rl
, flip_op(expr
->op
), alloc_rl(sval
, sval
));
1833 move_known_to_rl(expr_p
, rl_p
);
1836 if (expr
->op
== '-')
1838 if (get_implied_value(expr
->left
, &sval
)) {
1839 *expr_p
= expr
->right
;
1840 *rl_p
= rl_binop(rl
, flip_op(expr
->op
), alloc_rl(sval
, sval
));
1841 move_known_to_rl(expr_p
, rl_p
);
1846 static void move_known_values(struct expression
**left_p
, struct expression
**right_p
)
1848 struct expression
*left
= *left_p
;
1849 struct expression
*right
= *right_p
;
1852 if (get_implied_value(left
, &sval
)) {
1853 if (!is_simple_math(right
))
1855 if (get_implied_value(right
, &dummy
))
1857 if (right
->op
== '*') {
1860 if (!get_value(right
->right
, &divisor
))
1862 if (divisor
.value
== 0)
1864 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1865 *right_p
= right
->left
;
1868 if (right
->op
== '+' && get_value(right
->left
, &sval
)) {
1869 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->left
);
1870 *right_p
= right
->right
;
1873 if (get_value(right
->right
, &sval
)) {
1874 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1875 *right_p
= right
->left
;
1880 if (get_implied_value(right
, &sval
)) {
1881 if (!is_simple_math(left
))
1883 if (get_implied_value(left
, &dummy
))
1885 if (left
->op
== '*') {
1888 if (!get_value(left
->right
, &divisor
))
1890 if (divisor
.value
== 0)
1892 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1893 *left_p
= left
->left
;
1896 if (left
->op
== '+' && get_value(left
->left
, &sval
)) {
1897 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->left
);
1898 *left_p
= left
->right
;
1902 if (get_value(left
->right
, &sval
)) {
1903 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1904 *left_p
= left
->left
;
1912 * The reason for do_simple_algebra() is to solve things like:
1913 * if (foo > 66 || foo + bar > 64) {
1914 * "foo" is not really a known variable so it won't be handled by
1915 * move_known_variables() but it's a super common idiom.
1918 static int do_simple_algebra(struct expression
**left_p
, struct expression
**right_p
)
1920 struct expression
*left
= *left_p
;
1921 struct expression
*right
= *right_p
;
1922 struct range_list
*rl
;
1925 if (left
->type
!= EXPR_BINOP
|| left
->op
!= '+')
1927 if (can_integer_overflow(get_type(left
), left
))
1929 if (!get_implied_value(right
, &tmp
))
1932 if (!get_implied_value(left
->left
, &tmp
) &&
1933 get_implied_rl(left
->left
, &rl
) &&
1935 *right_p
= binop_expression(right
, '-', left
->left
);
1936 *left_p
= left
->right
;
1939 if (!get_implied_value(left
->right
, &tmp
) &&
1940 get_implied_rl(left
->right
, &rl
) &&
1942 *right_p
= binop_expression(right
, '-', left
->right
);
1943 *left_p
= left
->left
;
1950 static int match_func_comparison(struct expression
*expr
)
1952 struct expression
*left
= strip_expr(expr
->left
);
1953 struct expression
*right
= strip_expr(expr
->right
);
1955 if (left
->type
== EXPR_CALL
|| right
->type
== EXPR_CALL
) {
1956 // TODO: faked_assign this should be handled as a fake assignment instead
1957 function_comparison(left
, expr
->op
, right
);
1964 /* Handle conditions like "if (foo + bar < foo) {" */
1965 static int handle_integer_overflow_test(struct expression
*expr
)
1967 struct expression
*left
, *right
;
1968 struct symbol
*type
;
1969 sval_t left_min
, right_min
, min
, max
;
1971 if (expr
->op
!= '<' && expr
->op
!= SPECIAL_UNSIGNED_LT
)
1974 left
= strip_parens(expr
->left
);
1975 right
= strip_parens(expr
->right
);
1977 if (left
->op
!= '+')
1980 type
= get_type(expr
);
1983 if (type_positive_bits(type
) == 32) {
1984 max
.type
= &uint_ctype
;
1985 max
.uvalue
= (unsigned int)-1;
1986 } else if (type_positive_bits(type
) == 64) {
1987 max
.type
= &ulong_ctype
;
1988 max
.value
= (unsigned long long)-1;
1993 if (!expr_equiv(left
->left
, right
) && !expr_equiv(left
->right
, right
))
1996 get_absolute_min(left
->left
, &left_min
);
1997 get_absolute_min(left
->right
, &right_min
);
1998 min
= sval_binop(left_min
, '+', right_min
);
2000 type
= get_type(left
);
2001 min
= sval_cast(type
, min
);
2002 max
= sval_cast(type
, max
);
2004 set_extra_chunk_true_false(left
, NULL
, alloc_estate_range(min
, max
));
2008 static void match_comparison(struct expression
*expr
)
2010 struct expression
*left_orig
= strip_parens(expr
->left
);
2011 struct expression
*right_orig
= strip_parens(expr
->right
);
2012 struct expression
*left
, *right
, *tmp
;
2013 struct expression
*prev
;
2014 struct symbol
*type
;
2017 if (match_func_comparison(expr
))
2020 type
= get_type(expr
);
2022 type
= &llong_ctype
;
2024 if (handle_integer_overflow_test(expr
))
2029 move_known_values(&left
, &right
);
2030 handle_comparison(type
, left
, expr
->op
, right
);
2034 if (do_simple_algebra(&left
, &right
))
2035 handle_comparison(type
, left
, expr
->op
, right
);
2037 prev
= get_assigned_expr(left_orig
);
2038 if (is_simple_math(prev
) && !has_variable(prev
, left_orig
)) {
2041 move_known_values(&left
, &right
);
2042 handle_comparison(type
, left
, expr
->op
, right
);
2045 prev
= get_assigned_expr(right_orig
);
2046 if (is_simple_math(prev
) && !has_variable(prev
, right_orig
)) {
2049 move_known_values(&left
, &right
);
2050 handle_comparison(type
, left
, expr
->op
, right
);
2056 if (get_last_expr_from_expression_stmt(left_orig
)) {
2057 left
= get_last_expr_from_expression_stmt(left_orig
);
2060 if (get_last_expr_from_expression_stmt(right_orig
)) {
2061 right
= get_last_expr_from_expression_stmt(right_orig
);
2069 while ((tmp
= get_assigned_expr(left
))) {
2072 left
= strip_expr(tmp
);
2075 while ((tmp
= get_assigned_expr(right
))) {
2078 right
= strip_expr(tmp
);
2081 handle_comparison(type
, left
, expr
->op
, right
);
2084 static sval_t
get_high_mask(sval_t known
)
2092 for (i
= type_bits(known
.type
) - 1; i
>= 0; i
--) {
2093 if (known
.uvalue
& (1ULL << i
))
2094 ret
.uvalue
|= (1ULL << i
);
2102 static bool handle_bit_test(struct expression
*expr
)
2104 struct range_list
*orig_rl
, *rlt
, *rlf
, *true_rl
, *false_rl
;
2105 struct expression
*shift
, *mask
, *var
;
2106 struct bit_info
*bit_info
;
2108 sval_t high
= { .type
= &int_ctype
};
2109 sval_t low
= { .type
= &int_ctype
};
2111 shift
= strip_expr(expr
->right
);
2112 mask
= strip_expr(expr
->left
);
2113 if (shift
->type
!= EXPR_BINOP
|| shift
->op
!= SPECIAL_LEFTSHIFT
) {
2114 shift
= strip_expr(expr
->left
);
2115 mask
= strip_expr(expr
->right
);
2116 if (shift
->type
!= EXPR_BINOP
|| shift
->op
!= SPECIAL_LEFTSHIFT
)
2119 if (!get_implied_value(shift
->left
, &sval
) || sval
.value
!= 1)
2121 var
= strip_expr(shift
->right
);
2123 bit_info
= get_bit_info(mask
);
2126 if (!bit_info
->possible
){
2127 set_true_false_states_expr(my_id
, var
, alloc_estate_empty(), NULL
);
2131 get_absolute_rl(var
, &orig_rl
);
2132 if (sval_is_negative(rl_min(orig_rl
)) ||
2133 rl_max(orig_rl
).uvalue
> type_bits(get_type(shift
->left
)))
2136 low
.value
= ffsll(bit_info
->possible
) - 1;
2137 high
.value
= sm_fls64(bit_info
->possible
) - 1;
2138 rlt
= alloc_rl(low
, high
);
2139 rlt
= cast_rl(get_type(var
), rlt
);
2140 true_rl
= rl_intersection(orig_rl
, rlt
);
2142 low
.value
= ffsll(bit_info
->set
) - 1;
2143 high
.value
= sm_fls64(bit_info
->set
) - 1;
2144 rlf
= alloc_rl(low
, high
);
2145 rlf
= cast_rl(get_type(var
), rlf
);
2146 false_rl
= rl_filter(orig_rl
, rlf
);
2148 set_extra_expr_true_false(var
, alloc_estate_rl(true_rl
), alloc_estate_rl(false_rl
));
2153 static void handle_AND_op(struct expression
*var
, sval_t known
)
2155 struct range_list
*orig_rl
;
2156 struct range_list
*true_rl
= NULL
;
2157 struct range_list
*false_rl
= NULL
;
2159 sval_t low_mask
= known
;
2163 get_absolute_rl(var
, &orig_rl
);
2165 if (known
.value
> 0) {
2166 bit
= ffsll(known
.value
) - 1;
2167 low_mask
.uvalue
= (1ULL << bit
) - 1;
2168 true_rl
= remove_range(orig_rl
, sval_type_val(known
.type
, 0), low_mask
);
2170 high_mask
= get_high_mask(known
);
2171 if (high_mask
.value
) {
2172 bit
= ffsll(high_mask
.value
) - 1;
2173 low_mask
.uvalue
= (1ULL << bit
) - 1;
2176 if (sval_is_negative(rl_min(orig_rl
)))
2177 false_rl
= remove_range(false_rl
, sval_type_min(known
.type
), sval_type_val(known
.type
, -1));
2178 false_rl
= remove_range(false_rl
, low_mask
, sval_type_max(known
.type
));
2179 if (type_signed(high_mask
.type
) && type_unsigned(rl_type(false_rl
))) {
2180 false_rl
= remove_range(false_rl
,
2181 sval_type_val(rl_type(false_rl
), sval_type_max(known
.type
).uvalue
),
2182 sval_type_val(rl_type(false_rl
), -1));
2184 } else if (known
.value
== 1 &&
2185 get_hard_max(var
, &max
) &&
2186 sval_cmp(max
, rl_max(orig_rl
)) == 0 &&
2188 false_rl
= remove_range(orig_rl
, max
, max
);
2190 set_extra_expr_true_false(var
,
2191 true_rl
? alloc_estate_rl(true_rl
) : NULL
,
2192 false_rl
? alloc_estate_rl(false_rl
) : NULL
);
2195 static void handle_AND_condition(struct expression
*expr
)
2199 if (handle_bit_test(expr
))
2202 if (get_implied_value(expr
->left
, &known
))
2203 handle_AND_op(expr
->right
, known
);
2204 else if (get_implied_value(expr
->right
, &known
))
2205 handle_AND_op(expr
->left
, known
);
2208 static void handle_MOD_condition(struct expression
*expr
)
2210 struct range_list
*orig_rl
;
2211 struct range_list
*true_rl
;
2212 struct range_list
*false_rl
= NULL
;
2218 if (!get_implied_value(expr
->right
, &right
) || right
.value
== 0)
2220 get_absolute_rl(expr
->left
, &orig_rl
);
2222 zero
.type
= rl_type(orig_rl
);
2224 /* We're basically dorking around the min and max here */
2225 true_rl
= remove_range(orig_rl
, zero
, zero
);
2226 if (!sval_is_max(rl_max(true_rl
)) &&
2227 !(rl_max(true_rl
).value
% right
.value
))
2228 true_rl
= remove_range(true_rl
, rl_max(true_rl
), rl_max(true_rl
));
2230 if (rl_equiv(true_rl
, orig_rl
))
2233 if (sval_is_positive(rl_min(orig_rl
)) &&
2234 (rl_max(orig_rl
).value
- rl_min(orig_rl
).value
) / right
.value
< 5) {
2238 add
= rl_min(orig_rl
);
2239 add
.value
+= right
.value
- (add
.value
% right
.value
);
2240 add
.value
-= right
.value
;
2242 for (i
= 0; i
< 5; i
++) {
2243 add
.value
+= right
.value
;
2244 if (add
.value
> rl_max(orig_rl
).value
)
2246 add_range(&false_rl
, add
, add
);
2249 if (rl_min(orig_rl
).uvalue
!= 0 &&
2250 rl_min(orig_rl
).uvalue
< right
.uvalue
) {
2251 sval_t chop
= right
;
2253 false_rl
= remove_range(orig_rl
, zero
, chop
);
2256 if (!sval_is_max(rl_max(orig_rl
)) &&
2257 (rl_max(orig_rl
).value
% right
.value
)) {
2258 sval_t chop
= rl_max(orig_rl
);
2259 chop
.value
-= chop
.value
% right
.value
;
2262 false_rl
= clone_rl(orig_rl
);
2263 false_rl
= remove_range(false_rl
, chop
, rl_max(orig_rl
));
2267 set_extra_expr_true_false(expr
->left
,
2268 true_rl
? alloc_estate_rl(true_rl
) : NULL
,
2269 false_rl
? alloc_estate_rl(false_rl
) : NULL
);
2272 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
2273 void __extra_match_condition(struct expression
*expr
)
2275 expr
= strip_expr(expr
);
2276 switch (expr
->type
) {
2278 function_comparison(expr
, SPECIAL_NOTEQUAL
, zero_expr());
2283 handle_comparison(get_type(expr
), expr
, SPECIAL_NOTEQUAL
, zero_expr());
2286 match_comparison(expr
);
2288 case EXPR_ASSIGNMENT
:
2289 __extra_match_condition(expr
->left
);
2292 if (expr
->op
== '&')
2293 handle_AND_condition(expr
);
2294 if (expr
->op
== '%')
2295 handle_MOD_condition(expr
);
2300 static void assume_indexes_are_valid(struct expression
*expr
)
2302 struct expression
*array_expr
;
2304 struct expression
*offset
;
2305 struct symbol
*offset_type
;
2306 struct range_list
*rl_before
;
2307 struct range_list
*rl_after
;
2308 struct range_list
*filter
= NULL
;
2311 expr
= strip_expr(expr
);
2312 if (!is_array(expr
))
2315 offset
= get_array_offset(expr
);
2316 offset_type
= get_type(offset
);
2317 if (offset_type
&& type_signed(offset_type
)) {
2318 filter
= alloc_rl(sval_type_min(offset_type
),
2319 sval_type_val(offset_type
, -1));
2322 array_expr
= get_array_base(expr
);
2323 array_size
= get_real_array_size(array_expr
);
2324 if (array_size
> 1) {
2325 size
= sval_type_val(offset_type
, array_size
);
2326 add_range(&filter
, size
, sval_type_max(offset_type
));
2331 get_absolute_rl(offset
, &rl_before
);
2332 rl_after
= rl_filter(rl_before
, filter
);
2333 if (rl_equiv(rl_before
, rl_after
))
2335 set_extra_expr_nomod(offset
, alloc_estate_rl(rl_after
));
2338 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
2339 int implied_not_equal(struct expression
*expr
, long long val
)
2341 return !possibly_false(expr
, SPECIAL_NOTEQUAL
, value_expr(val
));
2344 int implied_not_equal_name_sym(char *name
, struct symbol
*sym
, long long val
)
2346 struct smatch_state
*estate
;
2348 estate
= get_state(SMATCH_EXTRA
, name
, sym
);
2351 if (!rl_has_sval(estate_rl(estate
), sval_type_val(estate_type(estate
), 0)))
2356 bool is_noderef_ptr(struct expression
*expr
)
2358 struct range_list
*rl
;
2360 if (!get_implied_rl(expr
, &rl
))
2362 return is_noderef_ptr_rl(rl
);
2365 static int parent_is_err_or_null_var_sym_helper(const char *name
, struct symbol
*sym
, bool check_err_ptr
)
2367 struct smatch_state
*state
;
2372 strncpy(buf
, name
, sizeof(buf
) - 1);
2373 buf
[sizeof(buf
) - 1] = '\0';
2376 while (*start
== '*') {
2378 state
= __get_state(SMATCH_EXTRA
, start
, sym
);
2381 if (!estate_rl(state
))
2383 if (is_noderef_ptr_rl(estate_rl(state
)))
2388 while (*start
== '&')
2391 len
= strlen(start
);
2395 if (start
[len
] == '-' ||
2396 start
[len
] == '.') {
2403 state
= __get_state(SMATCH_EXTRA
, start
, sym
);
2406 if (is_noderef_ptr_rl(estate_rl(state
)))
2411 int parent_is_null_var_sym(const char *name
, struct symbol
*sym
)
2413 return parent_is_err_or_null_var_sym_helper(name
, sym
, false);
2416 int parent_is_err_or_null_var_sym(const char *name
, struct symbol
*sym
)
2418 return parent_is_err_or_null_var_sym_helper(name
, sym
, (option_project
== PROJ_KERNEL
));
2421 int parent_is_null(struct expression
*expr
)
2427 expr
= strip_expr(expr
);
2428 var
= expr_to_var_sym(expr
, &sym
);
2431 ret
= parent_is_null_var_sym(var
, sym
);
2437 static int param_used_callback(void *found
, int argc
, char **argv
, char **azColName
)
2443 static int is_kzalloc_info(struct sm_state
*sm
)
2448 * kzalloc() information is treated as special because so there is just
2449 * a lot of stuff initialized to zero and it makes building the database
2450 * take hours and hours.
2452 * In theory, we should just remove this line and not pass any unused
2453 * information, but I'm not sure enough that this code works so I want
2454 * to hold off on that for now.
2456 if (!estate_get_single_value(sm
->state
, &sval
))
2458 if (sval
.value
!= 0)
2463 static int is_really_long(struct sm_state
*sm
)
2469 while ((p
= strstr(p
, "->"))) {
2475 strlen(sm
->name
) < 40)
2480 static int filter_unused_param_value_info(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
2484 /* for function pointers assume everything is used */
2485 if (call
->fn
->type
!= EXPR_SYMBOL
)
2488 if (strcmp(printed_name
, "$") == 0 ||
2489 strcmp(printed_name
, "*$") == 0)
2493 * This is to handle __builtin_mul_overflow(). In an ideal world we
2494 * would only need this for invalid code.
2497 if (!call
->fn
->symbol
)
2500 if (!is_kzalloc_info(sm
) && !is_really_long(sm
))
2503 run_sql(¶m_used_callback
, &found
,
2504 "select * from return_implies where %s and type = %d and parameter = %d and key = '%s';",
2505 get_static_filter(call
->fn
->symbol
), PARAM_USED
, param
, printed_name
);
2509 /* If the database is not built yet, then assume everything is used */
2510 run_sql(¶m_used_callback
, &found
,
2511 "select * from return_implies where %s and type = %d;",
2512 get_static_filter(call
->fn
->symbol
), PARAM_USED
);
2519 struct range_list
*intersect_with_real_abs_var_sym(const char *name
, struct symbol
*sym
, struct range_list
*start
)
2521 struct smatch_state
*state
;
2524 * Here is the difference between implied value and real absolute, say
2529 * Then you know that a is 0-255. That's real absolute. But you don't
2530 * know for sure that it actually goes up to 255. So it's not implied.
2531 * Implied indicates a degree of certainty.
2533 * But then say you cap "a" at 8. That means you know it goes up to
2534 * 8. So now the implied value is s32min-8. But you can combine it
2535 * with the real absolute to say that actually it's 0-8.
2537 * We are combining it here. But now that I think about it, this is
2538 * probably not the ideal place to combine it because it should proably
2539 * be done earlier. Oh well, this is an improvement on what was there
2540 * before so I'm going to commit this code.
2544 state
= get_real_absolute_state_var_sym(name
, sym
);
2545 if (!state
|| !estate_rl(state
))
2548 return rl_intersection(estate_rl(state
), start
);
2551 struct range_list
*intersect_with_real_abs_expr(struct expression
*expr
, struct range_list
*start
)
2553 struct smatch_state
*state
;
2554 struct range_list
*abs_rl
;
2556 state
= get_real_absolute_state(expr
);
2557 if (!state
|| !estate_rl(state
))
2560 abs_rl
= cast_rl(rl_type(start
), estate_rl(state
));
2561 return rl_intersection(abs_rl
, start
);
2564 static void caller_info_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
2566 struct range_list
*rl
;
2569 if (printed_name
[0] == '&')
2571 if (estate_is_whole(sm
->state
) || !estate_rl(sm
->state
))
2573 if (filter_unused_param_value_info(call
, param
, printed_name
, sm
))
2575 rl
= estate_rl(sm
->state
);
2576 rl
= intersect_with_real_abs_var_sym(sm
->name
, sm
->sym
, rl
);
2579 sql_insert_caller_info(call
, PARAM_VALUE
, param
, printed_name
, show_rl(rl
));
2580 if (!estate_get_single_value(sm
->state
, &dummy
)) {
2581 if (estate_has_hard_max(sm
->state
))
2582 sql_insert_caller_info(call
, HARD_MAX
, param
, printed_name
,
2583 sval_to_str(estate_max(sm
->state
)));
2584 if (estate_has_fuzzy_max(sm
->state
))
2585 sql_insert_caller_info(call
, FUZZY_MAX
, param
, printed_name
,
2586 sval_to_str(estate_get_fuzzy_max(sm
->state
)));
2590 static void returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
2592 struct symbol
*returned_sym
;
2593 char *returned_name
;
2594 struct sm_state
*sm
;
2602 if (!is_pointer(expr
))
2604 if (return_ranges
&& strstr(return_ranges
, "[==$"))
2607 returned_name
= expr_to_var_sym(expr
, &returned_sym
);
2608 if (!returned_name
|| !returned_sym
)
2610 len
= strlen(returned_name
);
2612 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), sm
) {
2613 if (!estate_rl(sm
->state
))
2615 if (returned_sym
!= sm
->sym
)
2617 if (strncmp(returned_name
, sm
->name
, len
) != 0)
2619 if (sm
->name
[len
] != '-')
2622 snprintf(name_buf
, sizeof(name_buf
), "$%s", sm
->name
+ len
);
2624 compare_str
= name_sym_to_param_comparison(sm
->name
, sm
->sym
);
2625 if (!compare_str
&& estate_is_whole(sm
->state
))
2627 snprintf(val_buf
, sizeof(val_buf
), "%s%s", sm
->state
->name
, compare_str
?: "");
2629 sql_insert_return_states(return_id
, return_ranges
, PARAM_VALUE
,
2630 -1, name_buf
, val_buf
);
2631 } END_FOR_EACH_SM(sm
);
2634 free_string(returned_name
);
2637 static void db_limited_before(void)
2639 unmatched_stree
= clone_stree(__get_cur_stree());
2642 static void db_limited_after(void)
2644 free_stree(&unmatched_stree
);
2647 static int basically_the_same(struct range_list
*orig
, struct range_list
*new)
2649 if (type_is_ptr(rl_type(orig
)) &&
2650 is_whole_ptr_rl(orig
) &&
2651 is_whole_ptr_rl(new))
2654 return rl_equiv(orig
, new);
2657 static void db_param_limit_binops(struct expression
*arg
, char *key
, struct range_list
*rl
)
2659 struct range_list
*left_rl
;
2660 sval_t zero
= { .type
= rl_type(rl
), };
2663 if (strcmp(key
, "$") != 0)
2667 if (!get_implied_value(arg
->right
, &sval
))
2669 if (can_integer_overflow(get_type(arg
), arg
))
2672 left_rl
= rl_binop(rl
, '/', alloc_rl(sval
, sval
));
2673 if (!rl_has_sval(rl
, zero
))
2674 left_rl
= remove_range(left_rl
, zero
, zero
);
2676 set_extra_expr_nomod(arg
->left
, alloc_estate_rl(left_rl
));
2679 static void db_param_limit_filter(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
2681 struct smatch_state
*state
;
2682 struct expression
*arg
;
2685 struct var_sym_list
*vsl
= NULL
;
2686 struct sm_state
*sm
;
2687 struct symbol
*compare_type
, *var_type
;
2688 struct range_list
*rl
;
2689 struct range_list
*limit
;
2690 struct range_list
*new;
2692 struct symbol
*other_sym
;
2694 while (expr
->type
== EXPR_ASSIGNMENT
)
2695 expr
= strip_expr(expr
->right
);
2696 if (expr
->type
!= EXPR_CALL
)
2699 arg
= get_argument_from_call_expr(expr
->args
, param
);
2703 if (strcmp(key
, "$") == 0)
2704 compare_type
= get_arg_type(expr
->fn
, param
);
2706 compare_type
= get_member_type_from_key(arg
, key
);
2708 call_results_to_rl(expr
, compare_type
, value
, &limit
);
2709 if (strcmp(key
, "$") == 0)
2710 move_known_to_rl(&arg
, &limit
);
2711 name
= get_chunk_from_key(arg
, key
, &sym
, &vsl
);
2714 if (op
!= PARAM_LIMIT
&& !sym
)
2717 sm
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
2719 rl
= estate_rl(sm
->state
);
2721 rl
= alloc_whole_rl(compare_type
);
2723 if (op
== PARAM_LIMIT
&& !rl_fits_in_type(rl
, compare_type
))
2726 new = rl_intersection(rl
, limit
);
2728 var_type
= get_member_type_from_key(arg
, key
);
2729 new = cast_rl(var_type
, new);
2731 /* We want to preserve the implications here */
2732 if (sm
&& basically_the_same(rl
, new))
2734 other_name
= get_other_name_sym(name
, sym
, &other_sym
);
2736 state
= alloc_estate_rl(new);
2737 if (sm
&& estate_has_hard_max(sm
->state
))
2738 estate_set_hard_max(state
);
2740 if (op
== PARAM_LIMIT
) {
2741 set_extra_nomod_vsl(name
, sym
, vsl
, NULL
, state
);
2743 set_extra_mod(name
, sym
, NULL
, state
);
2745 if (other_name
&& other_sym
) {
2746 state
= clone_estate(state
);
2747 if (op
== PARAM_LIMIT
)
2748 set_extra_nomod_vsl(other_name
, other_sym
, vsl
, NULL
, state
);
2750 set_extra_mod(other_name
, other_sym
, NULL
, state
);
2753 if (op
== PARAM_LIMIT
&& arg
->type
== EXPR_BINOP
)
2754 db_param_limit_binops(arg
, key
, new);
2759 static void db_param_limit(struct expression
*expr
, int param
, char *key
, char *value
)
2761 db_param_limit_filter(expr
, param
, key
, value
, PARAM_LIMIT
);
2764 static void db_param_filter(struct expression
*expr
, int param
, char *key
, char *value
)
2766 db_param_limit_filter(expr
, param
, key
, value
, PARAM_FILTER
);
2769 static struct expression
*get_star_pointer_hack(struct expression
*arg
, const char *key
)
2771 /* if we are setting *(p + offset) then we are setting *p */
2772 if (strcmp(key
, "*$") != 0)
2775 arg
= strip_expr(arg
);
2778 arg
= strip_expr(arg
->left
);
2779 if (!is_pointer(arg
))
2784 static void db_param_add_set(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
2786 struct expression
*arg
, *gen_expr
;
2788 char *other_name
= NULL
;
2789 struct symbol
*sym
, *other_sym
;
2790 struct symbol
*param_type
, *arg_type
;
2791 struct smatch_state
*state
;
2792 struct range_list
*new = NULL
;
2793 struct range_list
*added
= NULL
;
2795 while (expr
->type
== EXPR_ASSIGNMENT
)
2796 expr
= strip_expr(expr
->right
);
2797 if (expr
->type
!= EXPR_CALL
)
2800 arg
= get_argument_from_call_expr(expr
->args
, param
);
2804 arg_type
= get_arg_type_from_key(expr
->fn
, param
, arg
, key
);
2805 param_type
= get_member_type_from_key(arg
, key
);
2806 if (param_type
&& param_type
->type
== SYM_STRUCT
)
2808 name
= get_variable_from_key(arg
, key
, &sym
);
2809 if (!name
|| !sym
) {
2810 arg
= get_star_pointer_hack(arg
, key
);
2813 name
= get_variable_from_key(arg
, key
, &sym
);
2817 gen_expr
= gen_expression_from_key(arg
, key
);
2819 state
= get_state(SMATCH_EXTRA
, name
, sym
);
2821 new = estate_rl(state
);
2823 call_results_to_rl(expr
, arg_type
, value
, &added
);
2824 added
= cast_rl(param_type
, added
);
2825 if (op
== PARAM_SET
)
2828 new = rl_union(new, added
);
2830 other_name
= get_other_name_sym_nostack(name
, sym
, &other_sym
);
2831 set_extra_mod(name
, sym
, gen_expr
, alloc_estate_rl(new));
2832 if (other_name
&& other_sym
)
2833 set_extra_mod(other_name
, other_sym
, gen_expr
, alloc_estate_rl(new));
2835 free_string(other_name
);
2839 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
2841 in_param_set
= true;
2842 db_param_add_set(expr
, param
, key
, value
, PARAM_ADD
);
2843 in_param_set
= false;
2846 static void db_param_set(struct expression
*expr
, int param
, char *key
, char *value
)
2848 in_param_set
= true;
2849 db_param_add_set(expr
, param
, key
, value
, PARAM_SET
);
2850 in_param_set
= false;
2853 static void match_lost_param(struct expression
*call
, int param
)
2855 struct expression
*arg
;
2857 if (is_const_param(call
->fn
, param
))
2860 arg
= get_argument_from_call_expr(call
->args
, param
);
2864 arg
= strip_expr(arg
);
2865 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&')
2866 set_extra_expr_mod(arg
->unop
, alloc_estate_whole(get_type(arg
->unop
)));
2868 ; /* if pointer then set struct members, maybe?*/
2871 static void db_param_value(struct expression
*expr
, int param
, char *key
, char *value
)
2873 struct expression
*call
;
2876 struct symbol
*type
;
2877 struct range_list
*rl
= NULL
;
2883 while (call
->type
== EXPR_ASSIGNMENT
)
2884 call
= strip_expr(call
->right
);
2885 if (call
->type
!= EXPR_CALL
)
2888 type
= get_member_type_from_key(expr
->left
, key
);
2889 name
= get_variable_from_key(expr
->left
, key
, &sym
);
2893 call_results_to_rl(call
, type
, value
, &rl
);
2895 set_extra_mod(name
, sym
, NULL
, alloc_estate_rl(rl
));
2900 static void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2902 struct expression
*expr
;
2903 struct range_list
*rl
= NULL
;
2904 struct smatch_state
*state
;
2905 struct symbol
*type
;
2906 char *key_orig
= key
;
2910 expr
= symbol_expression(sym
);
2911 fullname
= get_variable_from_key(expr
, key
, NULL
);
2915 type
= get_member_type_from_key(expr
, key_orig
);
2916 str_to_rl(type
, value
, &rl
);
2917 state
= alloc_estate_rl(rl
);
2918 if (estate_get_single_value(state
, &dummy
))
2919 estate_set_hard_max(state
);
2920 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
2923 static void set_param_fuzzy_max(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2925 struct expression
*expr
;
2926 struct range_list
*rl
= NULL
;
2927 struct smatch_state
*state
;
2928 struct symbol
*type
;
2932 expr
= symbol_expression(sym
);
2933 fullname
= get_variable_from_key(expr
, key
, NULL
);
2937 state
= get_state(SMATCH_EXTRA
, fullname
, sym
);
2940 type
= estate_type(state
);
2941 str_to_rl(type
, value
, &rl
);
2942 if (!rl_to_sval(rl
, &max
))
2944 estate_set_fuzzy_max(state
, max
);
2947 static void set_param_hard_max(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2949 struct smatch_state
*state
;
2950 struct expression
*expr
;
2953 expr
= symbol_expression(sym
);
2954 fullname
= get_variable_from_key(expr
, key
, NULL
);
2958 state
= get_state(SMATCH_EXTRA
, fullname
, sym
);
2961 estate_set_hard_max(state
);
2964 static struct sm_state
*get_sm_from_call(struct expression
*expr
)
2966 struct expression
*fake
;
2967 struct sm_state
*ret
;
2970 if (is_fake_call(expr
))
2973 fake
= expr_get_fake_parent_expr(expr
);
2974 if (fake
&& fake
->type
== EXPR_ASSIGNMENT
) {
2975 ret
= get_sm_state_expr(SMATCH_EXTRA
, fake
->left
);
2980 snprintf(buf
, sizeof(buf
), "return %p", expr
);
2981 return get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
2984 struct sm_state
*get_extra_sm_state(struct expression
*expr
)
2988 struct sm_state
*ret
= NULL
;
2990 expr
= strip_expr(expr
);
2994 if (expr
->type
== EXPR_CALL
)
2995 return get_sm_from_call(expr
);
2997 name
= expr_to_known_chunk_sym(expr
, &sym
);
3001 ret
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
3007 struct smatch_state
*get_extra_state(struct expression
*expr
)
3009 struct sm_state
*sm
;
3011 sm
= get_extra_sm_state(expr
);
3017 void register_smatch_extra(int id
)
3021 set_dynamic_states(my_id
);
3022 add_merge_hook(my_id
, &merge_estates
);
3023 add_unmatched_state_hook(my_id
, &unmatched_state
);
3024 select_caller_info_hook(set_param_value
, PARAM_VALUE
);
3025 select_caller_info_hook(set_param_fuzzy_max
, FUZZY_MAX
);
3026 select_caller_info_hook(set_param_hard_max
, HARD_MAX
);
3027 select_return_states_before(&db_limited_before
);
3028 select_return_states_hook(PARAM_LIMIT
, &db_param_limit
);
3029 select_return_states_hook(PARAM_FILTER
, &db_param_filter
);
3030 select_return_states_hook(PARAM_ADD
, &db_param_add
);
3031 select_return_states_hook(PARAM_SET
, &db_param_set
);
3032 add_lost_param_hook(&match_lost_param
);
3033 select_return_states_hook(PARAM_VALUE
, &db_param_value
);
3034 select_return_states_after(&db_limited_after
);
3037 static void match_link_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
3039 struct var_sym_list
*links
;
3040 struct var_sym
*tmp
;
3041 struct smatch_state
*state
;
3043 links
= sm
->state
->data
;
3045 FOR_EACH_PTR(links
, tmp
) {
3046 if (sm
->sym
== tmp
->sym
&&
3047 strcmp(sm
->name
, tmp
->var
) == 0)
3049 state
= get_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
);
3052 set_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
, alloc_estate_whole(estate_type(state
)));
3053 } END_FOR_EACH_PTR(tmp
);
3054 set_state(link_id
, sm
->name
, sm
->sym
, &undefined
);
3057 void register_smatch_extra_links(int id
)
3060 set_dynamic_states(link_id
);
3063 void register_smatch_extra_late(int id
)
3065 add_merge_hook(link_id
, &merge_link_states
);
3066 add_modification_hook(link_id
, &match_link_modify
);
3067 add_hook(&match_dereferences
, DEREF_HOOK
);
3068 add_hook(&match_pointer_as_array
, OP_HOOK
);
3069 select_return_implies_hook_early(DEREFERENCE
, &set_param_dereferenced
);
3070 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
3071 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
3072 add_hook(&match_assign
, GLOBAL_ASSIGNMENT_HOOK
);
3073 add_hook(&unop_expr
, OP_HOOK
);
3074 add_hook(&asm_expr
, ASM_HOOK
);
3076 add_caller_info_callback(my_id
, caller_info_callback
);
3077 add_split_return_callback(&returned_struct_members
);
3079 // add_hook(&assume_indexes_are_valid, OP_HOOK);