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 bool in_param_set
;
172 void set_extra_mod_helper(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
174 struct expression
*faked
;
177 expr
= gen_expression_from_name_sym(name
, sym
);
178 remove_from_equiv(name
, sym
);
179 set_union_info(name
, sym
, expr
, state
);
180 call_extra_mod_hooks(name
, sym
, expr
, state
);
181 faked
= get_faked_expression();
183 (faked
->type
== EXPR_ASSIGNMENT
&& is_fresh_alloc(faked
->right
)))
184 update_mtag_data(expr
, state
);
185 if ((__in_fake_assign
|| in_param_set
) &&
186 estate_is_unknown(state
) && !get_state(SMATCH_EXTRA
, name
, sym
))
188 set_state(SMATCH_EXTRA
, name
, sym
, state
);
191 static void set_extra_nomod_helper(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
193 call_extra_nomod_hooks(name
, sym
, expr
, state
);
194 set_state(SMATCH_EXTRA
, name
, sym
, state
);
197 static char *get_pointed_at(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
199 struct expression
*assigned
;
202 * Imagine we have an assignment: "foo = &addr;" then the other name
208 if (strcmp(name
+ 1, sym
->ident
->name
) != 0)
211 assigned
= get_assigned_expr_name_sym(sym
->ident
->name
, sym
);
214 assigned
= strip_parens(assigned
);
215 if (assigned
->type
!= EXPR_PREOP
|| assigned
->op
!= '&')
218 return expr_to_var_sym(assigned
->unop
, new_sym
);
221 char *get_other_name_sym_from_chunk(const char *name
, const char *chunk
, int len
, struct symbol
*sym
, struct symbol
**new_sym
)
223 struct expression
*assigned
;
224 char *orig_name
= NULL
;
228 assigned
= get_assigned_expr_name_sym(chunk
, sym
);
231 if (assigned
->type
== EXPR_CALL
)
232 return map_call_to_other_name_sym(name
, sym
, new_sym
);
233 if (assigned
->type
== EXPR_PREOP
&& assigned
->op
== '&') {
235 orig_name
= expr_to_var_sym(assigned
, new_sym
);
236 if (!orig_name
|| !*new_sym
)
239 snprintf(buf
, sizeof(buf
), "%s.%s", orig_name
+ 1, name
+ len
);
240 ret
= alloc_string(buf
);
241 free_string(orig_name
);
245 orig_name
= expr_to_var_sym(assigned
, new_sym
);
246 if (!orig_name
|| !*new_sym
)
249 snprintf(buf
, sizeof(buf
), "%s->%s", orig_name
, name
+ len
);
250 ret
= alloc_string(buf
);
251 free_string(orig_name
);
254 free_string(orig_name
);
258 static char *get_long_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
, bool use_stack
)
260 struct expression
*tmp
;
265 * Just prepend the name with a different name/sym and return that.
266 * For example, if we set "foo->bar = bar;" then the other name
267 * for "bar->baz" is "foo->bar->baz". Or if we have "foo = bar;" then
268 * the other name for "bar" is "foo". A third option is if we have
269 * "foo = bar;" then another name for "*bar" is "*foo".
272 FOR_EACH_MY_SM(check_assigned_expr_id
, __get_cur_stree(), sm
) {
273 tmp
= sm
->state
->data
;
274 if (!tmp
|| tmp
->type
!= EXPR_SYMBOL
)
276 if (tmp
->symbol
== sym
)
278 } END_FOR_EACH_SM(sm
);
283 if (!use_stack
&& name
[tmp
->symbol
->ident
->len
] != '-')
286 if (name
[0] == '*' && strcmp(name
+ 1, tmp
->symbol_name
->name
) == 0)
287 snprintf(buf
, sizeof(buf
), "*%s", sm
->name
);
288 else if (name
[tmp
->symbol
->ident
->len
] == '-' ||
289 name
[tmp
->symbol
->ident
->len
] == '.')
290 snprintf(buf
, sizeof(buf
), "%s%s", sm
->name
, name
+ tmp
->symbol
->ident
->len
);
291 else if (strcmp(name
, tmp
->symbol_name
->name
) == 0)
292 snprintf(buf
, sizeof(buf
), "%s", sm
->name
);
297 return alloc_string(buf
);
300 char *get_other_name_sym_helper(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
, bool use_stack
)
308 if (!sym
|| !sym
->ident
)
311 ret
= get_pointed_at(name
, sym
, new_sym
);
315 ret
= map_long_to_short_name_sym(name
, sym
, new_sym
, use_stack
);
319 len
= snprintf(buf
, sizeof(buf
), "%s", name
);
320 if (len
>= sizeof(buf
) - 2)
323 while (use_stack
&& len
>= 1) {
324 if (buf
[len
] == '>' && buf
[len
- 1] == '-') {
327 ret
= get_other_name_sym_from_chunk(name
, buf
, len
+ 2, sym
, new_sym
);
334 ret
= get_long_name_sym(name
, sym
, new_sym
, use_stack
);
341 char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
343 return get_other_name_sym_helper(name
, sym
, new_sym
, true);
346 char *get_other_name_sym_nostack(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
348 return get_other_name_sym_helper(name
, sym
, new_sym
, false);
351 void set_extra_mod(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
354 struct symbol
*new_sym
;
356 set_extra_mod_helper(name
, sym
, expr
, state
);
357 new_name
= get_other_name_sym_nostack(name
, sym
, &new_sym
);
358 if (new_name
&& new_sym
)
359 set_extra_mod_helper(new_name
, new_sym
, NULL
, state
);
360 free_string(new_name
);
363 static struct expression
*chunk_get_array_base(struct expression
*expr
)
366 * The problem with is_array() is that it only returns true for things
367 * like foo[1] but not for foo[1].bar.
370 expr
= strip_expr(expr
);
371 while (expr
&& expr
->type
== EXPR_DEREF
)
372 expr
= strip_expr(expr
->deref
);
373 return get_array_base(expr
);
376 static int chunk_has_array(struct expression
*expr
)
378 return !!chunk_get_array_base(expr
);
381 static void clear_array_states(struct expression
*array
)
385 sm
= get_sm_state_expr(link_id
, array
);
387 match_link_modify(sm
, NULL
);
390 static void set_extra_array_mod(struct expression
*expr
, struct smatch_state
*state
)
392 struct expression
*array
;
393 struct var_sym_list
*vsl
;
398 array
= chunk_get_array_base(expr
);
400 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
402 clear_array_states(array
);
406 FOR_EACH_PTR(vsl
, vs
) {
407 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
408 } END_FOR_EACH_PTR(vs
);
410 call_extra_mod_hooks(name
, sym
, expr
, state
);
411 set_state(SMATCH_EXTRA
, name
, sym
, state
);
416 void set_extra_expr_mod(struct expression
*expr
, struct smatch_state
*state
)
421 if (chunk_has_array(expr
)) {
422 set_extra_array_mod(expr
, state
);
426 expr
= strip_expr(expr
);
427 name
= expr_to_var_sym(expr
, &sym
);
430 set_extra_mod(name
, sym
, expr
, state
);
435 void set_extra_nomod(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
438 struct symbol
*new_sym
;
439 struct relation
*rel
;
440 struct smatch_state
*orig_state
;
442 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
444 /* don't save unknown states if leaving it blank is the same */
445 if (!orig_state
&& estate_is_unknown(state
))
448 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
449 if (new_name
&& new_sym
)
450 set_extra_nomod_helper(new_name
, new_sym
, expr
, state
);
451 free_string(new_name
);
453 if (!estate_related(orig_state
)) {
454 set_extra_nomod_helper(name
, sym
, expr
, state
);
458 set_related(state
, estate_related(orig_state
));
459 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
460 struct smatch_state
*estate
;
462 estate
= get_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
);
465 set_extra_nomod_helper(rel
->name
, rel
->sym
, expr
, clone_estate_cast(estate_type(estate
), state
));
466 } END_FOR_EACH_PTR(rel
);
469 void set_extra_nomod_vsl(const char *name
, struct symbol
*sym
, struct var_sym_list
*vsl
, struct expression
*expr
, struct smatch_state
*state
)
473 FOR_EACH_PTR(vsl
, vs
) {
474 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
475 } END_FOR_EACH_PTR(vs
);
477 set_extra_nomod(name
, sym
, expr
, state
);
481 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
483 void set_extra_expr_nomod(struct expression
*expr
, struct smatch_state
*state
)
485 struct var_sym_list
*vsl
;
490 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
493 FOR_EACH_PTR(vsl
, vs
) {
494 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
495 } END_FOR_EACH_PTR(vs
);
497 set_extra_nomod(name
, sym
, expr
, state
);
502 static void set_extra_true_false(const char *name
, struct symbol
*sym
,
503 struct smatch_state
*true_state
,
504 struct smatch_state
*false_state
)
507 struct symbol
*new_sym
;
508 struct relation
*rel
;
509 struct smatch_state
*orig_state
;
511 if (!true_state
&& !false_state
)
514 if (in_warn_on_macro())
517 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
518 if (new_name
&& new_sym
)
519 set_true_false_states(SMATCH_EXTRA
, new_name
, new_sym
, true_state
, false_state
);
520 free_string(new_name
);
522 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
524 if (!estate_related(orig_state
)) {
525 set_true_false_states(SMATCH_EXTRA
, name
, sym
, true_state
, false_state
);
530 set_related(true_state
, estate_related(orig_state
));
532 set_related(false_state
, estate_related(orig_state
));
534 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
535 set_true_false_states(SMATCH_EXTRA
, rel
->name
, rel
->sym
,
536 true_state
, false_state
);
537 } END_FOR_EACH_PTR(rel
);
540 static void set_extra_chunk_true_false(struct expression
*expr
,
541 struct smatch_state
*true_state
,
542 struct smatch_state
*false_state
)
544 struct var_sym_list
*vsl
;
550 if (in_warn_on_macro())
553 type
= get_type(expr
);
557 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
560 FOR_EACH_PTR(vsl
, vs
) {
561 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
562 } END_FOR_EACH_PTR(vs
);
564 set_true_false_states(SMATCH_EXTRA
, name
, sym
,
565 clone_estate(true_state
),
566 clone_estate(false_state
));
571 static void set_extra_expr_true_false(struct expression
*expr
,
572 struct smatch_state
*true_state
,
573 struct smatch_state
*false_state
)
579 if (!true_state
&& !false_state
)
582 if (get_value(expr
, &sval
))
585 expr
= strip_expr(expr
);
586 name
= expr_to_var_sym(expr
, &sym
);
589 set_extra_chunk_true_false(expr
, true_state
, false_state
);
592 set_extra_true_false(name
, sym
, true_state
, false_state
);
596 static int get_countdown_info(struct expression
*condition
, struct expression
**unop
, int *op
, sval_t
*right
)
598 struct expression
*unop_expr
;
602 right
->type
= &int_ctype
;
605 condition
= strip_expr(condition
);
607 if (condition
->type
== EXPR_COMPARE
) {
608 comparison
= remove_unsigned_from_comparison(condition
->op
);
610 if (comparison
!= SPECIAL_GTE
&& comparison
!= '>')
612 if (!get_value(condition
->right
, &limit
))
615 unop_expr
= condition
->left
;
616 if (unop_expr
->type
!= EXPR_PREOP
&& unop_expr
->type
!= EXPR_POSTOP
)
618 if (unop_expr
->op
!= SPECIAL_DECREMENT
)
628 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
630 if (condition
->op
!= SPECIAL_DECREMENT
)
639 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
641 struct expression
*iter_var
;
642 struct expression
*condition
, *unop
;
645 struct smatch_state
*estate
;
649 right
.type
= &int_ctype
;
652 condition
= strip_expr(loop
->iterator_pre_condition
);
656 if (!get_countdown_info(condition
, &unop
, &op
, &right
))
659 iter_var
= unop
->unop
;
661 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
664 if (sval_cmp(estate_min(sm
->state
), right
) < 0)
666 start
= estate_max(sm
->state
);
668 type
= get_type(iter_var
);
669 right
= sval_cast(type
, right
);
670 start
= sval_cast(type
, start
);
672 if (sval_cmp(start
, right
) <= 0)
674 if (!sval_is_max(start
))
677 if (op
== SPECIAL_GTE
)
680 if (unop
->type
== EXPR_PREOP
) {
682 estate
= alloc_estate_range(right
, start
);
683 if (estate_has_hard_max(sm
->state
))
684 estate_set_hard_max(estate
);
685 estate_copy_fuzzy_max(estate
, sm
->state
);
686 set_extra_expr_mod(iter_var
, estate
);
688 if (unop
->type
== EXPR_POSTOP
) {
689 estate
= alloc_estate_range(right
, start
);
690 if (estate_has_hard_max(sm
->state
))
691 estate_set_hard_max(estate
);
692 estate_copy_fuzzy_max(estate
, sm
->state
);
693 set_extra_expr_mod(iter_var
, estate
);
695 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
698 static struct sm_state
*handle_canonical_for_inc(struct expression
*iter_expr
,
699 struct expression
*condition
)
701 struct expression
*iter_var
;
703 struct smatch_state
*estate
;
704 sval_t start
, end
, max
;
707 iter_var
= iter_expr
->unop
;
708 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
711 if (!estate_get_single_value(sm
->state
, &start
))
713 if (!get_implied_value(condition
->right
, &end
))
716 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
719 switch (condition
->op
) {
720 case SPECIAL_UNSIGNED_LT
:
721 case SPECIAL_NOTEQUAL
:
723 if (!sval_is_min(end
))
726 case SPECIAL_UNSIGNED_LTE
:
732 if (sval_cmp(end
, start
) < 0)
734 type
= get_type(iter_var
);
735 start
= sval_cast(type
, start
);
736 end
= sval_cast(type
, end
);
737 estate
= alloc_estate_range(start
, end
);
738 if (get_hard_max(condition
->right
, &max
)) {
739 if (!get_macro_name(condition
->pos
))
740 estate_set_hard_max(estate
);
741 if (condition
->op
== '<' ||
742 condition
->op
== SPECIAL_UNSIGNED_LT
||
743 condition
->op
== SPECIAL_NOTEQUAL
)
745 max
= sval_cast(type
, max
);
746 estate_set_fuzzy_max(estate
, max
);
748 set_extra_expr_mod(iter_var
, estate
);
749 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
752 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
753 struct expression
*condition
)
755 struct expression
*iter_var
;
757 struct smatch_state
*estate
;
760 iter_var
= iter_expr
->unop
;
761 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
764 if (!estate_get_single_value(sm
->state
, &start
))
766 if (!get_implied_min(condition
->right
, &end
))
767 end
= sval_type_min(get_type(iter_var
));
768 end
= sval_cast(estate_type(sm
->state
), end
);
769 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
772 switch (condition
->op
) {
773 case SPECIAL_NOTEQUAL
:
775 if (!sval_is_max(end
))
783 if (sval_cmp(end
, start
) > 0)
785 estate
= alloc_estate_range(end
, start
);
786 estate_set_hard_max(estate
);
787 estate_set_fuzzy_max(estate
, estate_get_fuzzy_max(estate
));
788 set_extra_expr_mod(iter_var
, estate
);
789 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
792 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
794 struct expression
*iter_expr
;
795 struct expression
*condition
;
797 if (!loop
->iterator_post_statement
)
799 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
801 iter_expr
= loop
->iterator_post_statement
->expression
;
802 if (!loop
->iterator_pre_condition
)
804 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
806 condition
= loop
->iterator_pre_condition
;
808 if (iter_expr
->op
== SPECIAL_INCREMENT
)
809 return handle_canonical_for_inc(iter_expr
, condition
);
810 if (iter_expr
->op
== SPECIAL_DECREMENT
)
811 return handle_canonical_for_dec(iter_expr
, condition
);
815 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct stree
**stree
)
817 struct sm_state
*ret
;
820 * Canonical loops are a hack. The proper way to handle this is to
821 * use two passes, but unfortunately, doing two passes makes parsing
822 * code twice as slow.
824 * What we do is we set the inside state here, which overwrites whatever
825 * __extra_match_condition() does. Then we set the outside state in
826 * __extra_pre_loop_hook_after().
829 __push_fake_cur_stree();
830 if (!loop
->iterator_post_statement
)
831 ret
= handle_canonical_while_count_down(loop
);
833 ret
= handle_canonical_for_loops(loop
);
834 *stree
= __pop_fake_cur_stree();
838 int __iterator_unchanged(struct sm_state
*sm
)
842 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
847 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
849 struct expression
*unop
;
851 sval_t limit
, after_value
;
853 if (!get_countdown_info(condition
, &unop
, &op
, &limit
))
855 after_value
= estate_min(sm
->state
);
857 set_extra_mod(sm
->name
, sm
->sym
, condition
->unop
, alloc_estate_sval(after_value
));
860 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
861 struct statement
*iterator
,
862 struct expression
*condition
)
864 struct expression
*iter_expr
;
866 struct smatch_state
*state
;
869 while_count_down_after(sm
, condition
);
873 iter_expr
= iterator
->expression
;
875 if (condition
->type
!= EXPR_COMPARE
)
877 if (iter_expr
->op
== SPECIAL_INCREMENT
) {
878 limit
= sval_binop(estate_max(sm
->state
), '+',
879 sval_type_val(estate_type(sm
->state
), 1));
881 limit
= sval_binop(estate_min(sm
->state
), '-',
882 sval_type_val(estate_type(sm
->state
), 1));
884 limit
= sval_cast(estate_type(sm
->state
), limit
);
885 if (!estate_has_hard_max(sm
->state
) && !__has_breaks()) {
886 if (iter_expr
->op
== SPECIAL_INCREMENT
)
887 state
= alloc_estate_range(estate_min(sm
->state
), limit
);
889 state
= alloc_estate_range(limit
, estate_max(sm
->state
));
891 state
= alloc_estate_sval(limit
);
893 if (!estate_has_hard_max(sm
->state
)) {
894 estate_clear_hard_max(state
);
896 if (estate_has_fuzzy_max(sm
->state
)) {
897 sval_t hmax
= estate_get_fuzzy_max(sm
->state
);
898 sval_t max
= estate_max(sm
->state
);
900 if (sval_cmp(hmax
, max
) != 0)
901 estate_clear_fuzzy_max(state
);
902 } else if (!estate_has_fuzzy_max(sm
->state
)) {
903 estate_clear_fuzzy_max(state
);
906 set_extra_mod(sm
->name
, sm
->sym
, iter_expr
, state
);
909 static bool get_global_rl(const char *name
, struct symbol
*sym
, struct range_list
**rl
)
911 struct expression
*expr
;
913 if (!sym
|| !(sym
->ctype
.modifiers
& MOD_TOPLEVEL
) || !sym
->ident
)
915 if (strcmp(sym
->ident
->name
, name
) != 0)
918 expr
= symbol_expression(sym
);
919 return get_implied_rl(expr
, rl
);
922 static struct stree
*unmatched_stree
;
923 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
925 struct smatch_state
*state
;
926 struct range_list
*rl
;
928 if (unmatched_stree
) {
929 state
= get_state_stree(unmatched_stree
, SMATCH_EXTRA
, sm
->name
, sm
->sym
);
933 if (parent_is_gone_var_sym(sm
->name
, sm
->sym
))
934 return alloc_estate_empty();
935 if (get_global_rl(sm
->name
, sm
->sym
, &rl
))
936 return alloc_estate_rl(rl
);
937 return alloc_estate_whole(estate_type(sm
->state
));
940 static void clear_the_pointed_at(struct expression
*expr
)
945 struct sm_state
*tmp
;
947 name
= expr_to_var_sym(expr
, &sym
);
951 stree
= __get_cur_stree();
952 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
953 if (tmp
->name
[0] != '*')
957 if (strcmp(tmp
->name
+ 1, name
) != 0)
959 set_extra_mod(tmp
->name
, tmp
->sym
, expr
, alloc_estate_whole(estate_type(tmp
->state
)));
960 } END_FOR_EACH_SM(tmp
);
966 static int is_const_param(struct expression
*expr
, int param
)
970 type
= get_arg_type(expr
, param
);
973 if (type
->ctype
.modifiers
& MOD_CONST
)
978 static void match_function_call(struct expression
*expr
)
980 struct expression
*arg
;
981 struct expression
*tmp
;
984 /* if we have the db this is handled in smatch_function_hooks.c */
987 if (inlinable(expr
->fn
))
990 FOR_EACH_PTR(expr
->args
, arg
) {
992 if (is_const_param(expr
->fn
, param
))
994 tmp
= strip_expr(arg
);
995 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
996 set_extra_expr_mod(tmp
->unop
, alloc_estate_whole(get_type(tmp
->unop
)));
998 clear_the_pointed_at(tmp
);
999 } END_FOR_EACH_PTR(arg
);
1002 int values_fit_type(struct expression
*left
, struct expression
*right
)
1004 struct range_list
*rl
;
1005 struct symbol
*type
;
1007 type
= get_type(left
);
1010 get_absolute_rl(right
, &rl
);
1011 if (type
== rl_type(rl
))
1013 if (type_unsigned(type
) && sval_is_negative(rl_min(rl
)))
1015 if (sval_cmp(sval_type_min(type
), rl_min(rl
)) > 0)
1017 if (sval_cmp(sval_type_max(type
), rl_max(rl
)) < 0)
1022 static void save_chunk_info(struct expression
*left
, struct expression
*right
)
1024 struct var_sym_list
*vsl
;
1026 struct expression
*add_expr
;
1027 struct symbol
*type
;
1032 if (right
->type
!= EXPR_BINOP
|| right
->op
!= '-')
1034 if (!get_value(right
->left
, &sval
))
1036 if (!expr_to_sym(right
->right
))
1039 add_expr
= binop_expression(left
, '+', right
->right
);
1040 type
= get_type(add_expr
);
1043 name
= expr_to_chunk_sym_vsl(add_expr
, &sym
, &vsl
);
1046 FOR_EACH_PTR(vsl
, vs
) {
1047 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
1048 } END_FOR_EACH_PTR(vs
);
1050 set_state(SMATCH_EXTRA
, name
, sym
, alloc_estate_sval(sval_cast(type
, sval
)));
1055 static void do_array_assign(struct expression
*left
, int op
, struct expression
*right
)
1057 struct range_list
*rl
;
1060 get_absolute_rl(right
, &rl
);
1061 rl
= cast_rl(get_type(left
), rl
);
1063 rl
= alloc_whole_rl(get_type(left
));
1066 set_extra_array_mod(left
, alloc_estate_rl(rl
));
1069 static void match_vanilla_assign(struct expression
*left
, struct expression
*right
)
1071 struct range_list
*orig_rl
= NULL
;
1072 struct range_list
*rl
= NULL
;
1073 struct symbol
*right_sym
;
1074 struct symbol
*left_type
;
1075 struct symbol
*right_type
;
1076 char *right_name
= NULL
;
1080 struct smatch_state
*state
;
1083 if (is_struct(left
))
1086 save_chunk_info(left
, right
);
1088 name
= expr_to_var_sym(left
, &sym
);
1090 if (chunk_has_array(left
))
1091 do_array_assign(left
, '=', right
);
1095 left_type
= get_type(left
);
1096 right_type
= get_type(right
);
1098 right_name
= expr_to_var_sym(right
, &right_sym
);
1100 if (!__in_fake_assign
&&
1101 !(right
->type
== EXPR_PREOP
&& right
->op
== '&') &&
1102 right_name
&& right_sym
&&
1103 values_fit_type(left
, strip_expr(right
)) &&
1104 !has_symbol(right
, sym
)) {
1105 set_equiv(left
, right
);
1109 if (get_implied_value(right
, &sval
)) {
1110 state
= alloc_estate_sval(sval_cast(left_type
, sval
));
1114 if (__in_fake_assign
) {
1115 struct smatch_state
*right_state
;
1118 if (get_value(right
, &sval
)) {
1119 sval
= sval_cast(left_type
, sval
);
1120 state
= alloc_estate_sval(sval
);
1124 right_state
= get_state(SMATCH_EXTRA
, right_name
, right_sym
);
1126 /* simple assignment */
1127 state
= clone_estate(right_state
);
1131 state
= alloc_estate_rl(alloc_whole_rl(left_type
));
1135 comparison
= get_comparison_no_extra(left
, right
);
1137 comparison
= flip_comparison(comparison
);
1138 get_implied_rl(left
, &orig_rl
);
1141 if (get_implied_rl(right
, &rl
)) {
1142 rl
= cast_rl(left_type
, rl
);
1144 filter_by_comparison(&rl
, comparison
, orig_rl
);
1145 state
= alloc_estate_rl(rl
);
1146 if (get_hard_max(right
, &max
)) {
1147 estate_set_hard_max(state
);
1148 estate_set_fuzzy_max(state
, max
);
1151 rl
= alloc_whole_rl(right_type
);
1152 rl
= cast_rl(left_type
, rl
);
1154 filter_by_comparison(&rl
, comparison
, orig_rl
);
1155 state
= alloc_estate_rl(rl
);
1159 set_extra_mod(name
, sym
, left
, state
);
1161 free_string(right_name
);
1164 static void match_assign(struct expression
*expr
)
1166 struct range_list
*rl
= NULL
;
1167 struct expression
*left
;
1168 struct expression
*right
;
1169 struct expression
*binop_expr
;
1170 struct symbol
*left_type
;
1174 left
= strip_expr(expr
->left
);
1176 right
= strip_parens(expr
->right
);
1177 if (right
->type
== EXPR_CALL
&& sym_name_is("__builtin_expect", right
->fn
))
1178 right
= get_argument_from_call_expr(right
->args
, 0);
1179 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
1180 right
= strip_parens(right
->left
);
1182 if (expr
->op
== '=' && is_condition(expr
->right
))
1183 return; /* handled in smatch_condition.c */
1184 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
1185 return; /* handled in smatch_function_hooks.c */
1186 if (expr
->op
== '=') {
1187 match_vanilla_assign(left
, right
);
1191 name
= expr_to_var_sym(left
, &sym
);
1195 left_type
= get_type(left
);
1198 case SPECIAL_ADD_ASSIGN
:
1199 case SPECIAL_SUB_ASSIGN
:
1200 case SPECIAL_AND_ASSIGN
:
1201 case SPECIAL_MOD_ASSIGN
:
1202 case SPECIAL_SHL_ASSIGN
:
1203 case SPECIAL_SHR_ASSIGN
:
1204 case SPECIAL_OR_ASSIGN
:
1205 case SPECIAL_XOR_ASSIGN
:
1206 case SPECIAL_MUL_ASSIGN
:
1207 case SPECIAL_DIV_ASSIGN
:
1208 binop_expr
= binop_expression(expr
->left
,
1209 op_remove_assign(expr
->op
),
1211 get_absolute_rl(binop_expr
, &rl
);
1212 rl
= cast_rl(left_type
, rl
);
1213 if (inside_loop()) {
1214 if (expr
->op
== SPECIAL_ADD_ASSIGN
)
1215 add_range(&rl
, rl_max(rl
), sval_type_max(rl_type(rl
)));
1217 if (expr
->op
== SPECIAL_SUB_ASSIGN
&&
1218 !sval_is_negative(rl_min(rl
))) {
1219 sval_t zero
= { .type
= rl_type(rl
) };
1221 add_range(&rl
, rl_min(rl
), zero
);
1224 set_extra_mod(name
, sym
, left
, alloc_estate_rl(rl
));
1227 set_extra_mod(name
, sym
, left
, alloc_estate_whole(left_type
));
1232 static struct smatch_state
*increment_state(struct smatch_state
*state
)
1234 sval_t min
= estate_min(state
);
1235 sval_t max
= estate_max(state
);
1237 if (!estate_rl(state
))
1241 max
= sval_type_max(max
.type
);
1243 if (!sval_is_min(min
) && !sval_is_max(min
))
1245 if (!sval_is_min(max
) && !sval_is_max(max
))
1247 return alloc_estate_range(min
, max
);
1250 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
1252 sval_t min
= estate_min(state
);
1253 sval_t max
= estate_max(state
);
1255 if (!estate_rl(state
))
1259 min
= sval_type_min(min
.type
);
1261 if (!sval_is_min(min
) && !sval_is_max(min
))
1263 if (!sval_is_min(max
) && !sval_is_max(max
))
1265 return alloc_estate_range(min
, max
);
1268 static void clear_pointed_at_state(struct expression
*expr
)
1270 struct symbol
*type
;
1273 * ALERT: This is sort of a mess. If it's is a struct assigment like
1274 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1275 * the same thing for p++ where "p" is a struct. Most modifications
1276 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1277 * use smatch_modification.c because we have to get the ordering right
1278 * or something. So if you have p++ where p is a pointer to a standard
1279 * c type then we handle that here. What a mess.
1281 expr
= strip_expr(expr
);
1282 type
= get_type(expr
);
1283 if (!type
|| type
->type
!= SYM_PTR
)
1285 type
= get_real_base_type(type
);
1286 if (!type
|| type
->type
!= SYM_BASETYPE
)
1288 set_extra_expr_nomod(deref_expression(expr
), alloc_estate_whole(type
));
1291 static void unop_expr(struct expression
*expr
)
1293 struct smatch_state
*state
;
1295 if (expr
->smatch_flags
& Handled
)
1299 case SPECIAL_INCREMENT
:
1300 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
1301 state
= increment_state(state
);
1303 state
= alloc_estate_whole(get_type(expr
));
1304 set_extra_expr_mod(expr
->unop
, state
);
1305 clear_pointed_at_state(expr
->unop
);
1307 case SPECIAL_DECREMENT
:
1308 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
1309 state
= decrement_state(state
);
1311 state
= alloc_estate_whole(get_type(expr
));
1312 set_extra_expr_mod(expr
->unop
, state
);
1313 clear_pointed_at_state(expr
->unop
);
1320 static void asm_expr(struct statement
*stmt
)
1322 struct asm_operand
*op
;
1323 struct symbol
*type
;
1325 FOR_EACH_PTR(stmt
->asm_outputs
, op
) {
1326 type
= get_type(strip_expr(op
->expr
));
1327 set_extra_expr_mod(op
->expr
, alloc_estate_whole(type
));
1328 } END_FOR_EACH_PTR(op
);
1331 static void check_dereference(struct expression
*expr
)
1333 struct smatch_state
*state
;
1335 if (__in_fake_assign
)
1337 if (outside_of_function())
1339 state
= get_extra_state(expr
);
1341 struct range_list
*rl
;
1343 rl
= rl_intersection(estate_rl(state
), valid_ptr_rl
);
1344 if (rl_equiv(rl
, estate_rl(state
)))
1346 set_extra_expr_nomod(expr
, alloc_estate_rl(rl
));
1348 struct range_list
*rl
;
1350 if (get_mtag_rl(expr
, &rl
))
1351 rl
= rl_intersection(rl
, valid_ptr_rl
);
1353 rl
= clone_rl(valid_ptr_rl
);
1355 set_extra_expr_nomod(expr
, alloc_estate_rl(rl
));
1359 static void match_dereferences(struct expression
*expr
)
1361 if (expr
->type
!= EXPR_PREOP
)
1363 if (getting_address(expr
))
1365 /* it's saying that foo[1] = bar dereferences foo[1] */
1368 check_dereference(expr
->unop
);
1371 static void match_pointer_as_array(struct expression
*expr
)
1373 if (!is_array(expr
))
1375 check_dereference(get_array_base(expr
));
1378 static void find_dereferences(struct expression
*expr
)
1380 while (expr
->type
== EXPR_PREOP
) {
1381 if (expr
->op
== '*')
1382 check_dereference(expr
->unop
);
1383 expr
= strip_expr(expr
->unop
);
1387 static void set_param_dereferenced(struct expression
*call
, struct expression
*arg
, char *key
, char *unused
)
1392 name
= get_variable_from_key(arg
, key
, &sym
);
1394 struct smatch_state
*orig
, *new;
1395 struct range_list
*rl
;
1397 orig
= get_state(SMATCH_EXTRA
, name
, sym
);
1399 rl
= rl_intersection(estate_rl(orig
),
1400 alloc_rl(valid_ptr_min_sval
,
1401 valid_ptr_max_sval
));
1402 new = alloc_estate_rl(rl
);
1404 new = alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
);
1407 set_extra_nomod(name
, sym
, NULL
, new);
1411 find_dereferences(arg
);
1414 static sval_t
add_one(sval_t sval
)
1420 static int handle_postop_inc(struct expression
*left
, int op
, struct expression
*right
)
1422 struct statement
*stmt
;
1423 struct expression
*cond
;
1424 struct smatch_state
*true_state
, *false_state
;
1425 struct symbol
*type
;
1430 * If we're decrementing here then that's a canonical while count down
1431 * so it's handled already. We're only handling loops like:
1433 * do { ... } while (i++ < 3);
1436 if (left
->type
!= EXPR_POSTOP
|| left
->op
!= SPECIAL_INCREMENT
)
1439 stmt
= __cur_stmt
->parent
;
1442 if (stmt
->type
== STMT_COMPOUND
)
1443 stmt
= stmt
->parent
;
1444 if (!stmt
|| stmt
->type
!= STMT_ITERATOR
|| !stmt
->iterator_post_condition
)
1447 cond
= strip_expr(stmt
->iterator_post_condition
);
1448 if (cond
->type
!= EXPR_COMPARE
|| cond
->op
!= op
)
1450 if (left
!= strip_expr(cond
->left
) || right
!= strip_expr(cond
->right
))
1453 if (!get_implied_value(left
->unop
, &start
))
1455 if (!get_implied_value(right
, &limit
))
1457 type
= get_type(left
->unop
);
1458 limit
= sval_cast(type
, limit
);
1459 if (sval_cmp(start
, limit
) > 0)
1464 case SPECIAL_UNSIGNED_LT
:
1467 case SPECIAL_UNSIGNED_LTE
:
1468 limit
= add_one(limit
);
1474 true_state
= alloc_estate_range(add_one(start
), limit
);
1475 false_state
= alloc_estate_range(add_one(limit
), add_one(limit
));
1477 /* Currently we just discard the false state but when two passes is
1478 * implimented correctly then it will use it.
1481 set_extra_expr_true_false(left
->unop
, true_state
, false_state
);
1486 bool is_impossible_variable(struct expression
*expr
)
1488 struct smatch_state
*state
;
1490 state
= get_extra_state(expr
);
1491 if (state
&& !estate_rl(state
))
1496 static bool in_macro(struct expression
*left
, struct expression
*right
)
1498 if (!left
|| !right
)
1500 if (left
->pos
.line
!= right
->pos
.line
|| left
->pos
.pos
!= right
->pos
.pos
)
1502 if (get_macro_name(left
->pos
))
1507 static void handle_comparison(struct symbol
*type
, struct expression
*left
, int op
, struct expression
*right
)
1509 struct range_list
*left_orig
;
1510 struct range_list
*left_true
;
1511 struct range_list
*left_false
;
1512 struct range_list
*right_orig
;
1513 struct range_list
*right_true
;
1514 struct range_list
*right_false
;
1515 struct smatch_state
*left_true_state
;
1516 struct smatch_state
*left_false_state
;
1517 struct smatch_state
*right_true_state
;
1518 struct smatch_state
*right_false_state
;
1519 sval_t dummy
, hard_max
;
1520 int left_postop
= 0;
1521 int right_postop
= 0;
1523 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
1524 if (left
->type
== EXPR_POSTOP
) {
1525 left
->smatch_flags
|= Handled
;
1526 left_postop
= left
->op
;
1527 if (handle_postop_inc(left
, op
, right
))
1530 left
= strip_parens(left
->unop
);
1532 while (left
->type
== EXPR_ASSIGNMENT
)
1533 left
= strip_parens(left
->left
);
1535 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
1536 if (right
->type
== EXPR_POSTOP
) {
1537 right
->smatch_flags
|= Handled
;
1538 right_postop
= right
->op
;
1540 right
= strip_parens(right
->unop
);
1543 if (is_impossible_variable(left
) || is_impossible_variable(right
))
1546 get_real_absolute_rl(left
, &left_orig
);
1547 left_orig
= cast_rl(type
, left_orig
);
1549 get_real_absolute_rl(right
, &right_orig
);
1550 right_orig
= cast_rl(type
, right_orig
);
1552 split_comparison_rl(left_orig
, op
, right_orig
, &left_true
, &left_false
, &right_true
, &right_false
);
1554 left_true
= rl_truncate_cast(get_type(strip_expr(left
)), left_true
);
1555 left_false
= rl_truncate_cast(get_type(strip_expr(left
)), left_false
);
1556 right_true
= rl_truncate_cast(get_type(strip_expr(right
)), right_true
);
1557 right_false
= rl_truncate_cast(get_type(strip_expr(right
)), right_false
);
1559 if (!left_true
|| !left_false
) {
1560 struct range_list
*tmp_true
, *tmp_false
;
1562 split_comparison_rl(alloc_whole_rl(type
), op
, right_orig
, &tmp_true
, &tmp_false
, NULL
, NULL
);
1563 tmp_true
= rl_truncate_cast(get_type(strip_expr(left
)), tmp_true
);
1564 tmp_false
= rl_truncate_cast(get_type(strip_expr(left
)), tmp_false
);
1565 if (tmp_true
&& tmp_false
)
1566 __save_imaginary_state(left
, tmp_true
, tmp_false
);
1569 if (!right_true
|| !right_false
) {
1570 struct range_list
*tmp_true
, *tmp_false
;
1572 split_comparison_rl(alloc_whole_rl(type
), op
, right_orig
, NULL
, NULL
, &tmp_true
, &tmp_false
);
1573 tmp_true
= rl_truncate_cast(get_type(strip_expr(right
)), tmp_true
);
1574 tmp_false
= rl_truncate_cast(get_type(strip_expr(right
)), tmp_false
);
1575 if (tmp_true
&& tmp_false
)
1576 __save_imaginary_state(right
, tmp_true
, tmp_false
);
1579 left_true_state
= alloc_estate_rl(left_true
);
1580 left_false_state
= alloc_estate_rl(left_false
);
1581 right_true_state
= alloc_estate_rl(right_true
);
1582 right_false_state
= alloc_estate_rl(right_false
);
1586 case SPECIAL_UNSIGNED_LT
:
1587 case SPECIAL_UNSIGNED_LTE
:
1589 if (get_implied_value(right
, &dummy
) && !in_macro(left
, right
))
1590 estate_set_hard_max(left_true_state
);
1591 if (get_implied_value(left
, &dummy
) && !in_macro(left
, right
))
1592 estate_set_hard_max(right_false_state
);
1595 case SPECIAL_UNSIGNED_GT
:
1596 case SPECIAL_UNSIGNED_GTE
:
1598 if (get_implied_value(left
, &dummy
) && !in_macro(left
, right
))
1599 estate_set_hard_max(right_true_state
);
1600 if (get_implied_value(right
, &dummy
) && !in_macro(left
, right
))
1601 estate_set_hard_max(left_false_state
);
1607 case SPECIAL_UNSIGNED_LT
:
1608 case SPECIAL_UNSIGNED_LTE
:
1610 if (get_hard_max(right
, &hard_max
)) {
1611 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1613 estate_set_fuzzy_max(left_true_state
, hard_max
);
1615 if (get_implied_value(right
, &hard_max
)) {
1616 if (op
== SPECIAL_UNSIGNED_LTE
||
1619 estate_set_fuzzy_max(left_false_state
, hard_max
);
1621 if (get_hard_max(left
, &hard_max
)) {
1622 if (op
== SPECIAL_UNSIGNED_LTE
||
1625 estate_set_fuzzy_max(right_false_state
, hard_max
);
1627 if (get_implied_value(left
, &hard_max
)) {
1628 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1630 estate_set_fuzzy_max(right_true_state
, hard_max
);
1634 case SPECIAL_UNSIGNED_GT
:
1635 case SPECIAL_UNSIGNED_GTE
:
1637 if (get_hard_max(left
, &hard_max
)) {
1638 if (op
== '>' || op
== SPECIAL_UNSIGNED_GT
)
1640 estate_set_fuzzy_max(right_true_state
, hard_max
);
1642 if (get_implied_value(left
, &hard_max
)) {
1643 if (op
== SPECIAL_UNSIGNED_GTE
||
1646 estate_set_fuzzy_max(right_false_state
, hard_max
);
1648 if (get_hard_max(right
, &hard_max
)) {
1649 if (op
== SPECIAL_UNSIGNED_LTE
||
1652 estate_set_fuzzy_max(left_false_state
, hard_max
);
1654 if (get_implied_value(right
, &hard_max
)) {
1656 op
== SPECIAL_UNSIGNED_GT
)
1658 estate_set_fuzzy_max(left_true_state
, hard_max
);
1662 if (get_hard_max(left
, &hard_max
))
1663 estate_set_fuzzy_max(right_true_state
, hard_max
);
1664 if (get_hard_max(right
, &hard_max
))
1665 estate_set_fuzzy_max(left_true_state
, hard_max
);
1669 if (get_hard_max(left
, &hard_max
)) {
1670 estate_set_hard_max(left_true_state
);
1671 estate_set_hard_max(left_false_state
);
1673 if (get_hard_max(right
, &hard_max
)) {
1674 estate_set_hard_max(right_true_state
);
1675 estate_set_hard_max(right_false_state
);
1678 if (left_postop
== SPECIAL_INCREMENT
) {
1679 left_true_state
= increment_state(left_true_state
);
1680 left_false_state
= increment_state(left_false_state
);
1682 if (left_postop
== SPECIAL_DECREMENT
) {
1683 left_true_state
= decrement_state(left_true_state
);
1684 left_false_state
= decrement_state(left_false_state
);
1686 if (right_postop
== SPECIAL_INCREMENT
) {
1687 right_true_state
= increment_state(right_true_state
);
1688 right_false_state
= increment_state(right_false_state
);
1690 if (right_postop
== SPECIAL_DECREMENT
) {
1691 right_true_state
= decrement_state(right_true_state
);
1692 right_false_state
= decrement_state(right_false_state
);
1695 if (estate_rl(left_true_state
) && estates_equiv(left_true_state
, left_false_state
)) {
1696 left_true_state
= NULL
;
1697 left_false_state
= NULL
;
1700 if (estate_rl(right_true_state
) && estates_equiv(right_true_state
, right_false_state
)) {
1701 right_true_state
= NULL
;
1702 right_false_state
= NULL
;
1705 /* Don't introduce new states for known true/false conditions */
1706 if (rl_equiv(left_orig
, estate_rl(left_true_state
)))
1707 left_true_state
= NULL
;
1708 if (rl_equiv(left_orig
, estate_rl(left_false_state
)))
1709 left_false_state
= NULL
;
1710 if (rl_equiv(right_orig
, estate_rl(right_true_state
)))
1711 right_true_state
= NULL
;
1712 if (rl_equiv(right_orig
, estate_rl(right_false_state
)))
1713 right_false_state
= NULL
;
1715 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
1716 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
1719 static int is_simple_math(struct expression
*expr
)
1723 if (expr
->type
!= EXPR_BINOP
)
1734 static int flip_op(int op
)
1736 /* We only care about simple math */
1748 static void move_known_to_rl(struct expression
**expr_p
, struct range_list
**rl_p
)
1750 struct expression
*expr
= *expr_p
;
1751 struct range_list
*rl
= *rl_p
;
1754 if (!is_simple_math(expr
))
1757 if (get_implied_value(expr
->right
, &sval
)) {
1758 *expr_p
= expr
->left
;
1759 *rl_p
= rl_binop(rl
, flip_op(expr
->op
), alloc_rl(sval
, sval
));
1760 move_known_to_rl(expr_p
, rl_p
);
1763 if (expr
->op
== '-')
1765 if (get_implied_value(expr
->left
, &sval
)) {
1766 *expr_p
= expr
->right
;
1767 *rl_p
= rl_binop(rl
, flip_op(expr
->op
), alloc_rl(sval
, sval
));
1768 move_known_to_rl(expr_p
, rl_p
);
1773 static void move_known_values(struct expression
**left_p
, struct expression
**right_p
)
1775 struct expression
*left
= *left_p
;
1776 struct expression
*right
= *right_p
;
1779 if (get_implied_value(left
, &sval
)) {
1780 if (!is_simple_math(right
))
1782 if (get_implied_value(right
, &dummy
))
1784 if (right
->op
== '*') {
1787 if (!get_value(right
->right
, &divisor
))
1789 if (divisor
.value
== 0)
1791 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1792 *right_p
= right
->left
;
1795 if (right
->op
== '+' && get_value(right
->left
, &sval
)) {
1796 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->left
);
1797 *right_p
= right
->right
;
1800 if (get_value(right
->right
, &sval
)) {
1801 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1802 *right_p
= right
->left
;
1807 if (get_implied_value(right
, &sval
)) {
1808 if (!is_simple_math(left
))
1810 if (get_implied_value(left
, &dummy
))
1812 if (left
->op
== '*') {
1815 if (!get_value(left
->right
, &divisor
))
1817 if (divisor
.value
== 0)
1819 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1820 *left_p
= left
->left
;
1823 if (left
->op
== '+' && get_value(left
->left
, &sval
)) {
1824 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->left
);
1825 *left_p
= left
->right
;
1829 if (get_value(left
->right
, &sval
)) {
1830 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1831 *left_p
= left
->left
;
1839 * The reason for do_simple_algebra() is to solve things like:
1840 * if (foo > 66 || foo + bar > 64) {
1841 * "foo" is not really a known variable so it won't be handled by
1842 * move_known_variables() but it's a super common idiom.
1845 static int do_simple_algebra(struct expression
**left_p
, struct expression
**right_p
)
1847 struct expression
*left
= *left_p
;
1848 struct expression
*right
= *right_p
;
1849 struct range_list
*rl
;
1852 if (left
->type
!= EXPR_BINOP
|| left
->op
!= '+')
1854 if (can_integer_overflow(get_type(left
), left
))
1856 if (!get_implied_value(right
, &tmp
))
1859 if (!get_implied_value(left
->left
, &tmp
) &&
1860 get_implied_rl(left
->left
, &rl
) &&
1862 *right_p
= binop_expression(right
, '-', left
->left
);
1863 *left_p
= left
->right
;
1866 if (!get_implied_value(left
->right
, &tmp
) &&
1867 get_implied_rl(left
->right
, &rl
) &&
1869 *right_p
= binop_expression(right
, '-', left
->right
);
1870 *left_p
= left
->left
;
1877 static int match_func_comparison(struct expression
*expr
)
1879 struct expression
*left
= strip_expr(expr
->left
);
1880 struct expression
*right
= strip_expr(expr
->right
);
1882 if (left
->type
== EXPR_CALL
|| right
->type
== EXPR_CALL
) {
1883 function_comparison(left
, expr
->op
, right
);
1890 /* Handle conditions like "if (foo + bar < foo) {" */
1891 static int handle_integer_overflow_test(struct expression
*expr
)
1893 struct expression
*left
, *right
;
1894 struct symbol
*type
;
1895 sval_t left_min
, right_min
, min
, max
;
1897 if (expr
->op
!= '<' && expr
->op
!= SPECIAL_UNSIGNED_LT
)
1900 left
= strip_parens(expr
->left
);
1901 right
= strip_parens(expr
->right
);
1903 if (left
->op
!= '+')
1906 type
= get_type(expr
);
1909 if (type_positive_bits(type
) == 32) {
1910 max
.type
= &uint_ctype
;
1911 max
.uvalue
= (unsigned int)-1;
1912 } else if (type_positive_bits(type
) == 64) {
1913 max
.type
= &ulong_ctype
;
1914 max
.value
= (unsigned long long)-1;
1919 if (!expr_equiv(left
->left
, right
) && !expr_equiv(left
->right
, right
))
1922 get_absolute_min(left
->left
, &left_min
);
1923 get_absolute_min(left
->right
, &right_min
);
1924 min
= sval_binop(left_min
, '+', right_min
);
1926 type
= get_type(left
);
1927 min
= sval_cast(type
, min
);
1928 max
= sval_cast(type
, max
);
1930 set_extra_chunk_true_false(left
, NULL
, alloc_estate_range(min
, max
));
1934 static void match_comparison(struct expression
*expr
)
1936 struct expression
*left_orig
= strip_parens(expr
->left
);
1937 struct expression
*right_orig
= strip_parens(expr
->right
);
1938 struct expression
*left
, *right
, *tmp
;
1939 struct expression
*prev
;
1940 struct symbol
*type
;
1943 if (match_func_comparison(expr
))
1946 type
= get_type(expr
);
1948 type
= &llong_ctype
;
1950 if (handle_integer_overflow_test(expr
))
1955 move_known_values(&left
, &right
);
1956 handle_comparison(type
, left
, expr
->op
, right
);
1960 if (do_simple_algebra(&left
, &right
))
1961 handle_comparison(type
, left
, expr
->op
, right
);
1963 prev
= get_assigned_expr(left_orig
);
1964 if (is_simple_math(prev
) && has_variable(prev
, left_orig
) == 0) {
1967 move_known_values(&left
, &right
);
1968 handle_comparison(type
, left
, expr
->op
, right
);
1971 prev
= get_assigned_expr(right_orig
);
1972 if (is_simple_math(prev
) && has_variable(prev
, right_orig
) == 0) {
1975 move_known_values(&left
, &right
);
1976 handle_comparison(type
, left
, expr
->op
, right
);
1982 if (get_last_expr_from_expression_stmt(left_orig
)) {
1983 left
= get_last_expr_from_expression_stmt(left_orig
);
1986 if (get_last_expr_from_expression_stmt(right_orig
)) {
1987 right
= get_last_expr_from_expression_stmt(right_orig
);
1995 while ((tmp
= get_assigned_expr(left
))) {
1998 left
= strip_expr(tmp
);
2001 while ((tmp
= get_assigned_expr(right
))) {
2004 right
= strip_expr(tmp
);
2007 handle_comparison(type
, left
, expr
->op
, right
);
2010 static sval_t
get_high_mask(sval_t known
)
2018 for (i
= type_bits(known
.type
) - 1; i
>= 0; i
--) {
2019 if (known
.uvalue
& (1ULL << i
))
2020 ret
.uvalue
|= (1ULL << i
);
2028 static bool handle_bit_test(struct expression
*expr
)
2030 struct range_list
*orig_rl
, *rl
;
2031 struct expression
*shift
, *mask
, *var
;
2032 struct bit_info
*bit_info
;
2034 sval_t high
= { .type
= &int_ctype
};
2035 sval_t low
= { .type
= &int_ctype
};
2037 shift
= strip_expr(expr
->right
);
2038 mask
= strip_expr(expr
->left
);
2039 if (shift
->type
!= EXPR_BINOP
|| shift
->op
!= SPECIAL_LEFTSHIFT
) {
2040 shift
= strip_expr(expr
->left
);
2041 mask
= strip_expr(expr
->right
);
2042 if (shift
->type
!= EXPR_BINOP
|| shift
->op
!= SPECIAL_LEFTSHIFT
)
2045 if (!get_implied_value(shift
->left
, &sval
) || sval
.value
!= 1)
2047 var
= strip_expr(shift
->right
);
2049 bit_info
= get_bit_info(mask
);
2052 if (!bit_info
->possible
)
2055 get_absolute_rl(var
, &orig_rl
);
2056 if (sval_is_negative(rl_min(orig_rl
)) ||
2057 rl_max(orig_rl
).uvalue
> type_bits(get_type(shift
->left
)))
2060 low
.value
= ffsll(bit_info
->possible
);
2061 high
.value
= sm_fls64(bit_info
->possible
);
2062 rl
= alloc_rl(low
, high
);
2063 rl
= cast_rl(get_type(var
), rl
);
2064 rl
= rl_intersection(orig_rl
, rl
);
2068 set_extra_expr_true_false(shift
->right
, alloc_estate_rl(rl
), NULL
);
2073 static void handle_AND_op(struct expression
*var
, sval_t known
)
2075 struct range_list
*orig_rl
;
2076 struct range_list
*true_rl
= NULL
;
2077 struct range_list
*false_rl
= NULL
;
2079 sval_t low_mask
= known
;
2083 get_absolute_rl(var
, &orig_rl
);
2085 if (known
.value
> 0) {
2086 bit
= ffsll(known
.value
) - 1;
2087 low_mask
.uvalue
= (1ULL << bit
) - 1;
2088 true_rl
= remove_range(orig_rl
, sval_type_val(known
.type
, 0), low_mask
);
2090 high_mask
= get_high_mask(known
);
2091 if (high_mask
.value
) {
2092 bit
= ffsll(high_mask
.value
) - 1;
2093 low_mask
.uvalue
= (1ULL << bit
) - 1;
2096 if (sval_is_negative(rl_min(orig_rl
)))
2097 false_rl
= remove_range(false_rl
, sval_type_min(known
.type
), sval_type_val(known
.type
, -1));
2098 false_rl
= remove_range(false_rl
, low_mask
, sval_type_max(known
.type
));
2099 if (type_signed(high_mask
.type
) && type_unsigned(rl_type(false_rl
))) {
2100 false_rl
= remove_range(false_rl
,
2101 sval_type_val(rl_type(false_rl
), sval_type_max(known
.type
).uvalue
),
2102 sval_type_val(rl_type(false_rl
), -1));
2104 } else if (known
.value
== 1 &&
2105 get_hard_max(var
, &max
) &&
2106 sval_cmp(max
, rl_max(orig_rl
)) == 0 &&
2108 false_rl
= remove_range(orig_rl
, max
, max
);
2110 set_extra_expr_true_false(var
,
2111 true_rl
? alloc_estate_rl(true_rl
) : NULL
,
2112 false_rl
? alloc_estate_rl(false_rl
) : NULL
);
2115 static void handle_AND_condition(struct expression
*expr
)
2119 if (handle_bit_test(expr
))
2122 if (get_implied_value(expr
->left
, &known
))
2123 handle_AND_op(expr
->right
, known
);
2124 else if (get_implied_value(expr
->right
, &known
))
2125 handle_AND_op(expr
->left
, known
);
2128 static void handle_MOD_condition(struct expression
*expr
)
2130 struct range_list
*orig_rl
;
2131 struct range_list
*true_rl
;
2132 struct range_list
*false_rl
= NULL
;
2138 if (!get_implied_value(expr
->right
, &right
) || right
.value
== 0)
2140 get_absolute_rl(expr
->left
, &orig_rl
);
2142 zero
.type
= rl_type(orig_rl
);
2144 /* We're basically dorking around the min and max here */
2145 true_rl
= remove_range(orig_rl
, zero
, zero
);
2146 if (!sval_is_max(rl_max(true_rl
)) &&
2147 !(rl_max(true_rl
).value
% right
.value
))
2148 true_rl
= remove_range(true_rl
, rl_max(true_rl
), rl_max(true_rl
));
2150 if (rl_equiv(true_rl
, orig_rl
))
2153 if (sval_is_positive(rl_min(orig_rl
)) &&
2154 (rl_max(orig_rl
).value
- rl_min(orig_rl
).value
) / right
.value
< 5) {
2158 add
= rl_min(orig_rl
);
2159 add
.value
+= right
.value
- (add
.value
% right
.value
);
2160 add
.value
-= right
.value
;
2162 for (i
= 0; i
< 5; i
++) {
2163 add
.value
+= right
.value
;
2164 if (add
.value
> rl_max(orig_rl
).value
)
2166 add_range(&false_rl
, add
, add
);
2169 if (rl_min(orig_rl
).uvalue
!= 0 &&
2170 rl_min(orig_rl
).uvalue
< right
.uvalue
) {
2171 sval_t chop
= right
;
2173 false_rl
= remove_range(orig_rl
, zero
, chop
);
2176 if (!sval_is_max(rl_max(orig_rl
)) &&
2177 (rl_max(orig_rl
).value
% right
.value
)) {
2178 sval_t chop
= rl_max(orig_rl
);
2179 chop
.value
-= chop
.value
% right
.value
;
2182 false_rl
= clone_rl(orig_rl
);
2183 false_rl
= remove_range(false_rl
, chop
, rl_max(orig_rl
));
2187 set_extra_expr_true_false(expr
->left
,
2188 true_rl
? alloc_estate_rl(true_rl
) : NULL
,
2189 false_rl
? alloc_estate_rl(false_rl
) : NULL
);
2192 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
2193 void __extra_match_condition(struct expression
*expr
)
2195 expr
= strip_expr(expr
);
2196 switch (expr
->type
) {
2198 function_comparison(expr
, SPECIAL_NOTEQUAL
, zero_expr());
2203 handle_comparison(get_type(expr
), expr
, SPECIAL_NOTEQUAL
, zero_expr());
2206 match_comparison(expr
);
2208 case EXPR_ASSIGNMENT
:
2209 __extra_match_condition(expr
->left
);
2212 if (expr
->op
== '&')
2213 handle_AND_condition(expr
);
2214 if (expr
->op
== '%')
2215 handle_MOD_condition(expr
);
2220 static void assume_indexes_are_valid(struct expression
*expr
)
2222 struct expression
*array_expr
;
2224 struct expression
*offset
;
2225 struct symbol
*offset_type
;
2226 struct range_list
*rl_before
;
2227 struct range_list
*rl_after
;
2228 struct range_list
*filter
= NULL
;
2231 expr
= strip_expr(expr
);
2232 if (!is_array(expr
))
2235 offset
= get_array_offset(expr
);
2236 offset_type
= get_type(offset
);
2237 if (offset_type
&& type_signed(offset_type
)) {
2238 filter
= alloc_rl(sval_type_min(offset_type
),
2239 sval_type_val(offset_type
, -1));
2242 array_expr
= get_array_base(expr
);
2243 array_size
= get_real_array_size(array_expr
);
2244 if (array_size
> 1) {
2245 size
= sval_type_val(offset_type
, array_size
);
2246 add_range(&filter
, size
, sval_type_max(offset_type
));
2251 get_absolute_rl(offset
, &rl_before
);
2252 rl_after
= rl_filter(rl_before
, filter
);
2253 if (rl_equiv(rl_before
, rl_after
))
2255 set_extra_expr_nomod(offset
, alloc_estate_rl(rl_after
));
2258 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
2259 int implied_not_equal(struct expression
*expr
, long long val
)
2261 return !possibly_false(expr
, SPECIAL_NOTEQUAL
, value_expr(val
));
2264 int implied_not_equal_name_sym(char *name
, struct symbol
*sym
, long long val
)
2266 struct smatch_state
*estate
;
2268 estate
= get_state(SMATCH_EXTRA
, name
, sym
);
2271 if (!rl_has_sval(estate_rl(estate
), sval_type_val(estate_type(estate
), 0)))
2276 int parent_is_null_var_sym(const char *name
, struct symbol
*sym
)
2281 struct smatch_state
*state
;
2283 strncpy(buf
, name
, sizeof(buf
) - 1);
2284 buf
[sizeof(buf
) - 1] = '\0';
2287 while (*start
== '*') {
2289 state
= __get_state(SMATCH_EXTRA
, start
, sym
);
2292 if (!estate_rl(state
))
2294 if (estate_min(state
).value
== 0 &&
2295 estate_max(state
).value
== 0)
2300 while (*start
== '&')
2303 while ((end
= strrchr(start
, '-'))) {
2305 state
= __get_state(SMATCH_EXTRA
, start
, sym
);
2308 if (estate_min(state
).value
== 0 &&
2309 estate_max(state
).value
== 0)
2315 int parent_is_null(struct expression
*expr
)
2321 expr
= strip_expr(expr
);
2322 var
= expr_to_var_sym(expr
, &sym
);
2325 ret
= parent_is_null_var_sym(var
, sym
);
2331 static int param_used_callback(void *found
, int argc
, char **argv
, char **azColName
)
2337 static int is_kzalloc_info(struct sm_state
*sm
)
2342 * kzalloc() information is treated as special because so there is just
2343 * a lot of stuff initialized to zero and it makes building the database
2344 * take hours and hours.
2346 * In theory, we should just remove this line and not pass any unused
2347 * information, but I'm not sure enough that this code works so I want
2348 * to hold off on that for now.
2350 if (!estate_get_single_value(sm
->state
, &sval
))
2352 if (sval
.value
!= 0)
2357 static int is_really_long(struct sm_state
*sm
)
2363 while ((p
= strstr(p
, "->"))) {
2369 strlen(sm
->name
) < 40)
2374 static int filter_unused_param_value_info(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
2378 /* for function pointers assume everything is used */
2379 if (call
->fn
->type
!= EXPR_SYMBOL
)
2383 * This is to handle __builtin_mul_overflow(). In an ideal world we
2384 * would only need this for invalid code.
2387 if (!call
->fn
->symbol
)
2390 if (!is_kzalloc_info(sm
) && !is_really_long(sm
))
2393 run_sql(¶m_used_callback
, &found
,
2394 "select * from return_implies where %s and type = %d and parameter = %d and key = '%s';",
2395 get_static_filter(call
->fn
->symbol
), PARAM_USED
, param
, printed_name
);
2399 /* If the database is not built yet, then assume everything is used */
2400 run_sql(¶m_used_callback
, &found
,
2401 "select * from return_implies where %s and type = %d;",
2402 get_static_filter(call
->fn
->symbol
), PARAM_USED
);
2409 struct range_list
*intersect_with_real_abs_var_sym(const char *name
, struct symbol
*sym
, struct range_list
*start
)
2411 struct smatch_state
*state
;
2414 * Here is the difference between implied value and real absolute, say
2419 * Then you know that a is 0-255. That's real absolute. But you don't
2420 * know for sure that it actually goes up to 255. So it's not implied.
2421 * Implied indicates a degree of certainty.
2423 * But then say you cap "a" at 8. That means you know it goes up to
2424 * 8. So now the implied value is s32min-8. But you can combine it
2425 * with the real absolute to say that actually it's 0-8.
2427 * We are combining it here. But now that I think about it, this is
2428 * probably not the ideal place to combine it because it should proably
2429 * be done earlier. Oh well, this is an improvement on what was there
2430 * before so I'm going to commit this code.
2434 state
= get_real_absolute_state_var_sym(name
, sym
);
2435 if (!state
|| !estate_rl(state
))
2438 return rl_intersection(estate_rl(state
), start
);
2441 struct range_list
*intersect_with_real_abs_expr(struct expression
*expr
, struct range_list
*start
)
2443 struct smatch_state
*state
;
2444 struct range_list
*abs_rl
;
2446 state
= get_real_absolute_state(expr
);
2447 if (!state
|| !estate_rl(state
))
2450 abs_rl
= cast_rl(rl_type(start
), estate_rl(state
));
2451 return rl_intersection(abs_rl
, start
);
2454 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
2456 struct range_list
*rl
;
2459 if (estate_is_whole(sm
->state
) || !estate_rl(sm
->state
))
2461 if (filter_unused_param_value_info(call
, param
, printed_name
, sm
))
2463 rl
= estate_rl(sm
->state
);
2464 rl
= intersect_with_real_abs_var_sym(sm
->name
, sm
->sym
, rl
);
2467 sql_insert_caller_info(call
, PARAM_VALUE
, param
, printed_name
, show_rl(rl
));
2468 if (!estate_get_single_value(sm
->state
, &dummy
)) {
2469 if (estate_has_hard_max(sm
->state
))
2470 sql_insert_caller_info(call
, HARD_MAX
, param
, printed_name
,
2471 sval_to_str(estate_max(sm
->state
)));
2472 if (estate_has_fuzzy_max(sm
->state
))
2473 sql_insert_caller_info(call
, FUZZY_MAX
, param
, printed_name
,
2474 sval_to_str(estate_get_fuzzy_max(sm
->state
)));
2478 static void returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
2480 struct symbol
*returned_sym
;
2481 char *returned_name
;
2482 struct sm_state
*sm
;
2490 if (!is_pointer(expr
))
2493 returned_name
= expr_to_var_sym(expr
, &returned_sym
);
2494 if (!returned_name
|| !returned_sym
)
2496 len
= strlen(returned_name
);
2498 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), sm
) {
2499 if (!estate_rl(sm
->state
))
2501 if (returned_sym
!= sm
->sym
)
2503 if (strncmp(returned_name
, sm
->name
, len
) != 0)
2505 if (sm
->name
[len
] != '-')
2508 snprintf(name_buf
, sizeof(name_buf
), "$%s", sm
->name
+ len
);
2510 compare_str
= name_sym_to_param_comparison(sm
->name
, sm
->sym
);
2511 if (!compare_str
&& estate_is_whole(sm
->state
))
2513 snprintf(val_buf
, sizeof(val_buf
), "%s%s", sm
->state
->name
, compare_str
?: "");
2515 sql_insert_return_states(return_id
, return_ranges
, PARAM_VALUE
,
2516 -1, name_buf
, val_buf
);
2517 } END_FOR_EACH_SM(sm
);
2520 free_string(returned_name
);
2523 static void db_limited_before(void)
2525 unmatched_stree
= clone_stree(__get_cur_stree());
2528 static void db_limited_after(void)
2530 free_stree(&unmatched_stree
);
2533 static int basically_the_same(struct range_list
*orig
, struct range_list
*new)
2535 if (rl_equiv(orig
, new))
2539 * The whole range is essentially the same as 0,4096-27777777777 so
2540 * don't overwrite the implications just to store that.
2543 if (rl_type(orig
)->type
== SYM_PTR
&&
2544 is_whole_rl(orig
) &&
2545 rl_min(new).value
== 0 &&
2546 rl_max(new).value
== valid_ptr_max
)
2551 static void db_param_limit_binops(struct expression
*arg
, char *key
, struct range_list
*rl
)
2553 struct range_list
*left_rl
;
2554 sval_t zero
= { .type
= rl_type(rl
), };
2559 if (!get_implied_value(arg
->right
, &sval
))
2561 if (can_integer_overflow(get_type(arg
), arg
))
2564 left_rl
= rl_binop(rl
, '/', alloc_rl(sval
, sval
));
2565 if (!rl_has_sval(rl
, zero
))
2566 left_rl
= remove_range(left_rl
, zero
, zero
);
2568 set_extra_expr_nomod(arg
->left
, alloc_estate_rl(left_rl
));
2571 static void db_param_limit_filter(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
2573 struct expression
*arg
;
2576 struct var_sym_list
*vsl
= NULL
;
2577 struct sm_state
*sm
;
2578 struct symbol
*compare_type
, *var_type
;
2579 struct range_list
*rl
;
2580 struct range_list
*limit
;
2581 struct range_list
*new;
2583 struct symbol
*other_sym
;
2585 while (expr
->type
== EXPR_ASSIGNMENT
)
2586 expr
= strip_expr(expr
->right
);
2587 if (expr
->type
!= EXPR_CALL
)
2590 arg
= get_argument_from_call_expr(expr
->args
, param
);
2594 if (strcmp(key
, "$") == 0)
2595 compare_type
= get_arg_type(expr
->fn
, param
);
2597 compare_type
= get_member_type_from_key(arg
, key
);
2599 call_results_to_rl(expr
, compare_type
, value
, &limit
);
2600 if (strcmp(key
, "$") == 0)
2601 move_known_to_rl(&arg
, &limit
);
2602 name
= get_chunk_from_key(arg
, key
, &sym
, &vsl
);
2605 if (op
!= PARAM_LIMIT
&& !sym
)
2608 sm
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
2610 rl
= estate_rl(sm
->state
);
2612 rl
= alloc_whole_rl(compare_type
);
2614 if (op
== PARAM_LIMIT
&& !rl_fits_in_type(rl
, compare_type
))
2617 new = rl_intersection(rl
, limit
);
2619 var_type
= get_member_type_from_key(arg
, key
);
2620 new = cast_rl(var_type
, new);
2622 /* We want to preserve the implications here */
2623 if (sm
&& basically_the_same(rl
, new))
2625 other_name
= get_other_name_sym(name
, sym
, &other_sym
);
2627 if (op
== PARAM_LIMIT
)
2628 set_extra_nomod_vsl(name
, sym
, vsl
, NULL
, alloc_estate_rl(new));
2630 set_extra_mod(name
, sym
, NULL
, alloc_estate_rl(new));
2632 if (other_name
&& other_sym
) {
2633 if (op
== PARAM_LIMIT
)
2634 set_extra_nomod_vsl(other_name
, other_sym
, vsl
, NULL
, alloc_estate_rl(new));
2636 set_extra_mod(other_name
, other_sym
, NULL
, alloc_estate_rl(new));
2639 if (op
== PARAM_LIMIT
&& arg
->type
== EXPR_BINOP
)
2640 db_param_limit_binops(arg
, key
, new);
2645 static void db_param_limit(struct expression
*expr
, int param
, char *key
, char *value
)
2647 db_param_limit_filter(expr
, param
, key
, value
, PARAM_LIMIT
);
2650 static void db_param_filter(struct expression
*expr
, int param
, char *key
, char *value
)
2652 db_param_limit_filter(expr
, param
, key
, value
, PARAM_FILTER
);
2655 static void db_param_add_set(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
2657 struct expression
*arg
, *gen_expr
;
2659 char *other_name
= NULL
;
2660 struct symbol
*sym
, *other_sym
;
2661 struct symbol
*param_type
, *arg_type
;
2662 struct smatch_state
*state
;
2663 struct range_list
*new = NULL
;
2664 struct range_list
*added
= NULL
;
2666 while (expr
->type
== EXPR_ASSIGNMENT
)
2667 expr
= strip_expr(expr
->right
);
2668 if (expr
->type
!= EXPR_CALL
)
2671 arg
= get_argument_from_call_expr(expr
->args
, param
);
2675 arg_type
= get_arg_type_from_key(expr
->fn
, param
, arg
, key
);
2676 param_type
= get_member_type_from_key(arg
, key
);
2677 if (param_type
&& param_type
->type
== SYM_STRUCT
)
2679 name
= get_variable_from_key(arg
, key
, &sym
);
2682 gen_expr
= gen_expression_from_key(arg
, key
);
2684 state
= get_state(SMATCH_EXTRA
, name
, sym
);
2686 new = estate_rl(state
);
2688 call_results_to_rl(expr
, arg_type
, value
, &added
);
2689 added
= cast_rl(param_type
, added
);
2690 if (op
== PARAM_SET
)
2693 new = rl_union(new, added
);
2695 other_name
= get_other_name_sym_nostack(name
, sym
, &other_sym
);
2696 set_extra_mod(name
, sym
, gen_expr
, alloc_estate_rl(new));
2697 if (other_name
&& other_sym
)
2698 set_extra_mod(other_name
, other_sym
, gen_expr
, alloc_estate_rl(new));
2700 free_string(other_name
);
2704 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
2706 in_param_set
= true;
2707 db_param_add_set(expr
, param
, key
, value
, PARAM_ADD
);
2708 in_param_set
= false;
2711 static void db_param_set(struct expression
*expr
, int param
, char *key
, char *value
)
2713 in_param_set
= true;
2714 db_param_add_set(expr
, param
, key
, value
, PARAM_SET
);
2715 in_param_set
= false;
2718 static void match_lost_param(struct expression
*call
, int param
)
2720 struct expression
*arg
;
2722 if (is_const_param(call
->fn
, param
))
2725 arg
= get_argument_from_call_expr(call
->args
, param
);
2729 arg
= strip_expr(arg
);
2730 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&')
2731 set_extra_expr_mod(arg
->unop
, alloc_estate_whole(get_type(arg
->unop
)));
2733 ; /* if pointer then set struct members, maybe?*/
2736 static void db_param_value(struct expression
*expr
, int param
, char *key
, char *value
)
2738 struct expression
*call
;
2741 struct symbol
*type
;
2742 struct range_list
*rl
= NULL
;
2748 while (call
->type
== EXPR_ASSIGNMENT
)
2749 call
= strip_expr(call
->right
);
2750 if (call
->type
!= EXPR_CALL
)
2753 type
= get_member_type_from_key(expr
->left
, key
);
2754 name
= get_variable_from_key(expr
->left
, key
, &sym
);
2758 call_results_to_rl(call
, type
, value
, &rl
);
2760 set_extra_mod(name
, sym
, NULL
, alloc_estate_rl(rl
));
2765 static void match_call_info(struct expression
*expr
)
2767 struct smatch_state
*state
;
2768 struct range_list
*rl
= NULL
;
2769 struct expression
*arg
;
2770 struct symbol
*type
;
2774 FOR_EACH_PTR(expr
->args
, arg
) {
2775 type
= get_arg_type(expr
->fn
, i
);
2777 get_absolute_rl(arg
, &rl
);
2778 rl
= cast_rl(type
, rl
);
2780 if (!is_whole_rl(rl
)) {
2781 rl
= intersect_with_real_abs_expr(arg
, rl
);
2782 sql_insert_caller_info(expr
, PARAM_VALUE
, i
, "$", show_rl(rl
));
2784 state
= get_state_expr(SMATCH_EXTRA
, arg
);
2785 if (!estate_get_single_value(state
, &dummy
) && estate_has_hard_max(state
)) {
2786 sql_insert_caller_info(expr
, HARD_MAX
, i
, "$",
2787 sval_to_str(estate_max(state
)));
2789 if (estate_has_fuzzy_max(state
)) {
2790 sql_insert_caller_info(expr
, FUZZY_MAX
, i
, "$",
2791 sval_to_str(estate_get_fuzzy_max(state
)));
2794 } END_FOR_EACH_PTR(arg
);
2797 static void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2799 struct expression
*expr
;
2800 struct range_list
*rl
= NULL
;
2801 struct smatch_state
*state
;
2802 struct symbol
*type
;
2803 char *key_orig
= key
;
2807 expr
= symbol_expression(sym
);
2808 fullname
= get_variable_from_key(expr
, key
, NULL
);
2812 type
= get_member_type_from_key(expr
, key_orig
);
2813 str_to_rl(type
, value
, &rl
);
2814 state
= alloc_estate_rl(rl
);
2815 if (estate_get_single_value(state
, &dummy
))
2816 estate_set_hard_max(state
);
2817 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
2820 static void set_param_fuzzy_max(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2822 struct expression
*expr
;
2823 struct range_list
*rl
= NULL
;
2824 struct smatch_state
*state
;
2825 struct symbol
*type
;
2829 expr
= symbol_expression(sym
);
2830 fullname
= get_variable_from_key(expr
, key
, NULL
);
2834 state
= get_state(SMATCH_EXTRA
, fullname
, sym
);
2837 type
= estate_type(state
);
2838 str_to_rl(type
, value
, &rl
);
2839 if (!rl_to_sval(rl
, &max
))
2841 estate_set_fuzzy_max(state
, max
);
2844 static void set_param_hard_max(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2846 struct smatch_state
*state
;
2847 struct expression
*expr
;
2850 expr
= symbol_expression(sym
);
2851 fullname
= get_variable_from_key(expr
, key
, NULL
);
2855 state
= get_state(SMATCH_EXTRA
, fullname
, sym
);
2858 estate_set_hard_max(state
);
2861 static struct sm_state
*get_sm_from_call(struct expression
*expr
)
2865 if (is_fake_call(expr
))
2868 snprintf(buf
, sizeof(buf
), "return %p", expr
);
2869 return get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
2872 struct sm_state
*get_extra_sm_state(struct expression
*expr
)
2876 struct sm_state
*ret
= NULL
;
2878 expr
= strip_expr(expr
);
2882 if (expr
->type
== EXPR_CALL
)
2883 return get_sm_from_call(expr
);
2885 name
= expr_to_known_chunk_sym(expr
, &sym
);
2889 ret
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
2895 struct smatch_state
*get_extra_state(struct expression
*expr
)
2897 struct sm_state
*sm
;
2899 sm
= get_extra_sm_state(expr
);
2905 void register_smatch_extra(int id
)
2909 set_dynamic_states(my_id
);
2910 add_merge_hook(my_id
, &merge_estates
);
2911 add_unmatched_state_hook(my_id
, &unmatched_state
);
2912 select_caller_info_hook(set_param_value
, PARAM_VALUE
);
2913 select_caller_info_hook(set_param_fuzzy_max
, FUZZY_MAX
);
2914 select_caller_info_hook(set_param_hard_max
, HARD_MAX
);
2915 select_return_states_before(&db_limited_before
);
2916 select_return_states_hook(PARAM_LIMIT
, &db_param_limit
);
2917 select_return_states_hook(PARAM_FILTER
, &db_param_filter
);
2918 select_return_states_hook(PARAM_ADD
, &db_param_add
);
2919 select_return_states_hook(PARAM_SET
, &db_param_set
);
2920 add_lost_param_hook(&match_lost_param
);
2921 select_return_states_hook(PARAM_VALUE
, &db_param_value
);
2922 select_return_states_after(&db_limited_after
);
2925 static void match_link_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
2927 struct var_sym_list
*links
;
2928 struct var_sym
*tmp
;
2929 struct smatch_state
*state
;
2931 links
= sm
->state
->data
;
2933 FOR_EACH_PTR(links
, tmp
) {
2934 if (sm
->sym
== tmp
->sym
&&
2935 strcmp(sm
->name
, tmp
->var
) == 0)
2937 state
= get_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
);
2940 set_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
, alloc_estate_whole(estate_type(state
)));
2941 } END_FOR_EACH_PTR(tmp
);
2942 set_state(link_id
, sm
->name
, sm
->sym
, &undefined
);
2945 void register_smatch_extra_links(int id
)
2948 set_dynamic_states(link_id
);
2951 void register_smatch_extra_late(int id
)
2953 add_merge_hook(link_id
, &merge_link_states
);
2954 add_modification_hook(link_id
, &match_link_modify
);
2955 add_hook(&match_dereferences
, DEREF_HOOK
);
2956 add_hook(&match_pointer_as_array
, OP_HOOK
);
2957 select_return_implies_hook(DEREFERENCE
, &set_param_dereferenced
);
2958 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
2959 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
2960 add_hook(&match_assign
, GLOBAL_ASSIGNMENT_HOOK
);
2961 add_hook(&unop_expr
, OP_HOOK
);
2962 add_hook(&asm_expr
, ASM_HOOK
);
2964 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2965 add_member_info_callback(my_id
, struct_member_callback
);
2966 add_split_return_callback(&returned_struct_members
);
2968 // add_hook(&assume_indexes_are_valid, OP_HOOK);