2 * Copyright (C) 2008 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * smatch_extra.c is supposed to track the value of every variable.
34 #include "smatch_slist.h"
35 #include "smatch_extra.h"
40 static void match_link_modify(struct sm_state
*sm
, struct expression
*mod_expr
);
42 struct string_list
*__ignored_macros
= NULL
;
43 int in_warn_on_macro(void)
45 struct statement
*stmt
;
49 stmt
= get_current_statement();
52 macro
= get_macro_name(stmt
->pos
);
56 FOR_EACH_PTR(__ignored_macros
, tmp
) {
57 if (!strcmp(tmp
, macro
))
59 } END_FOR_EACH_PTR(tmp
);
63 typedef void (mod_hook
)(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
);
64 DECLARE_PTR_LIST(void_fn_list
, mod_hook
*);
65 static struct void_fn_list
*extra_mod_hooks
;
66 static struct void_fn_list
*extra_nomod_hooks
;
68 void add_extra_mod_hook(mod_hook
*fn
)
70 mod_hook
**p
= malloc(sizeof(mod_hook
*));
72 add_ptr_list(&extra_mod_hooks
, p
);
75 void add_extra_nomod_hook(mod_hook
*fn
)
77 mod_hook
**p
= malloc(sizeof(mod_hook
*));
79 add_ptr_list(&extra_nomod_hooks
, p
);
82 void call_extra_hooks(struct void_fn_list
*hooks
, const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
86 FOR_EACH_PTR(hooks
, fn
) {
87 (*fn
)(name
, sym
, expr
, state
);
88 } END_FOR_EACH_PTR(fn
);
91 void call_extra_mod_hooks(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
93 call_extra_hooks(extra_mod_hooks
, name
, sym
, expr
, state
);
96 void call_extra_nomod_hooks(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
98 call_extra_hooks(extra_nomod_hooks
, name
, sym
, expr
, state
);
101 static void set_union_info(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
103 struct symbol
*type
, *tmp
, *inner_type
, *inner
, *new_type
;
104 struct expression
*deref
, *member_expr
;
105 struct smatch_state
*new;
106 int offset
, inner_offset
;
107 static bool in_recurse
;
110 if (__in_fake_assign
)
117 if (!expr
|| expr
->type
!= EXPR_DEREF
|| !expr
->member
)
119 offset
= get_member_offset_from_deref(expr
);
123 deref
= strip_expr(expr
->deref
);
124 type
= get_type(deref
);
125 if (type_is_ptr(type
))
126 type
= get_real_base_type(type
);
127 if (!type
|| type
->type
!= SYM_STRUCT
)
130 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
131 inner_type
= get_real_base_type(tmp
);
132 if (!inner_type
|| inner_type
->type
!= SYM_UNION
)
135 inner
= first_ptr_list((struct ptr_list
*)inner_type
->symbol_list
);
136 if (!inner
|| !inner
->ident
)
139 inner_offset
= get_member_offset(type
, inner
->ident
->name
);
140 if (inner_offset
< offset
)
142 if (inner_offset
> offset
)
145 FOR_EACH_PTR(inner_type
->symbol_list
, inner
) {
146 struct symbol
*tmp_type
;
148 if (!inner
->ident
|| inner
->ident
== expr
->member
)
150 tmp_type
= get_real_base_type(inner
);
151 if (tmp_type
&& tmp_type
->type
== SYM_STRUCT
)
155 member_expr
= member_expression(member_expr
, '.', tmp
->ident
);
156 member_expr
= member_expression(member_expr
, expr
->op
, inner
->ident
);
157 member_name
= expr_to_var(member_expr
);
160 new_type
= get_real_base_type(inner
);
161 new = alloc_estate_rl(cast_rl(new_type
, estate_rl(state
)));
162 set_extra_mod_helper(member_name
, sym
, member_expr
, new);
163 free_string(member_name
);
164 } END_FOR_EACH_PTR(inner
);
165 } END_FOR_EACH_PTR(tmp
);
171 static void mark_sub_members_gone(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
175 if (__in_fake_assign
|| __in_fake_var_assign
)
178 if (!estate_type(state
) || estate_type(state
)->type
!= SYM_PTR
)
180 if (!is_noderef_ptr_rl(estate_rl(state
)))
183 FOR_EACH_MY_SM(SMATCH_EXTRA
, __get_cur_stree(), sm
) {
186 if (strcmp(name
, sm
->name
) == 0)
188 if (!is_sub_member(name
, sym
, sm
))
190 set_extra_nomod(sm
->name
, sm
->sym
, NULL
, alloc_estate_empty());
191 } END_FOR_EACH_SM(sm
);
194 static void call_update_mtag_data(struct expression
*expr
,
195 struct smatch_state
*state
)
197 struct expression
*faked
;
199 if (__in_fake_var_assign
)
202 faked
= get_faked_expression();
205 if (faked
->type
== EXPR_ASSIGNMENT
&& is_fresh_alloc(faked
->right
))
210 update_mtag_data(expr
, state
);
213 static bool in_param_set
;
214 void set_extra_mod_helper(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
217 expr
= gen_expression_from_name_sym(name
, sym
);
218 remove_from_equiv(name
, sym
);
219 set_union_info(name
, sym
, expr
, state
);
220 mark_sub_members_gone(name
, sym
, state
);
221 call_extra_mod_hooks(name
, sym
, expr
, state
);
222 call_update_mtag_data(expr
, state
);
223 if ((__in_fake_assign
|| in_param_set
) &&
224 estate_is_unknown(state
) && !get_state(SMATCH_EXTRA
, name
, sym
))
226 set_state(SMATCH_EXTRA
, name
, sym
, state
);
229 static void set_extra_nomod_helper(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
231 call_extra_nomod_hooks(name
, sym
, expr
, state
);
232 set_state(SMATCH_EXTRA
, name
, sym
, state
);
235 static char *get_pointed_at(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
237 struct expression
*assigned
;
240 * Imagine we have an assignment: "foo = &addr;" then the other name
246 if (strcmp(name
+ 1, sym
->ident
->name
) != 0)
249 assigned
= get_assigned_expr_name_sym(sym
->ident
->name
, sym
);
252 assigned
= strip_parens(assigned
);
253 if (assigned
->type
!= EXPR_PREOP
|| assigned
->op
!= '&')
256 return expr_to_var_sym(assigned
->unop
, new_sym
);
259 char *get_other_name_sym_from_chunk(const char *name
, const char *chunk
, int len
, struct symbol
*sym
, struct symbol
**new_sym
)
261 struct expression
*assigned
;
262 char *orig_name
= NULL
;
266 assigned
= get_assigned_expr_name_sym(chunk
, sym
);
269 if (assigned
->type
== EXPR_CALL
)
270 return map_call_to_other_name_sym(name
, sym
, new_sym
);
271 if (assigned
->type
== EXPR_PREOP
&& assigned
->op
== '&') {
273 orig_name
= expr_to_var_sym(assigned
, new_sym
);
274 if (!orig_name
|| !*new_sym
)
277 snprintf(buf
, sizeof(buf
), "%s.%s", orig_name
+ 1, name
+ len
);
278 ret
= alloc_string(buf
);
279 free_string(orig_name
);
283 orig_name
= expr_to_var_sym(assigned
, new_sym
);
284 if (!orig_name
|| !*new_sym
)
287 snprintf(buf
, sizeof(buf
), "%s->%s", orig_name
, name
+ len
);
288 ret
= alloc_string(buf
);
289 free_string(orig_name
);
292 free_string(orig_name
);
296 static char *get_long_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
, bool use_stack
)
298 struct expression
*tmp
;
304 * Just prepend the name with a different name/sym and return that.
305 * For example, if we set "foo->bar = bar;" then the other name
306 * for "bar->baz" is "foo->bar->baz". Or if we have "foo = bar;" then
307 * the other name for "bar" is "foo". A third option is if we have
308 * "foo = bar;" then another name for "*bar" is "*foo".
311 FOR_EACH_MY_SM(check_assigned_expr_id
, __get_cur_stree(), sm
) {
312 tmp
= sm
->state
->data
;
313 if (!tmp
|| tmp
->type
!= EXPR_SYMBOL
)
315 if (tmp
->symbol
== sym
)
317 } END_FOR_EACH_SM(sm
);
323 sym_len
= tmp
->symbol
->ident
->len
;
324 if (!use_stack
&& sym_len
< len
&& name
[sym_len
] != '-')
327 if (name
[0] == '*' && strcmp(name
+ 1, tmp
->symbol_name
->name
) == 0)
328 snprintf(buf
, sizeof(buf
), "*%s", sm
->name
);
329 else if (sym_len
< len
&& (name
[tmp
->symbol
->ident
->len
] == '-' ||
330 name
[tmp
->symbol
->ident
->len
] == '.'))
331 snprintf(buf
, sizeof(buf
), "%s%s", sm
->name
, name
+ tmp
->symbol
->ident
->len
);
332 else if (strcmp(name
, tmp
->symbol_name
->name
) == 0)
333 snprintf(buf
, sizeof(buf
), "%s", sm
->name
);
338 return alloc_string(buf
);
341 char *get_other_name_sym_helper(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
, bool use_stack
)
349 if (!sym
|| !sym
->ident
)
352 ret
= get_pointed_at(name
, sym
, new_sym
);
356 ret
= map_long_to_short_name_sym(name
, sym
, new_sym
, use_stack
);
360 len
= snprintf(buf
, sizeof(buf
), "%s", name
);
361 if (len
>= sizeof(buf
) - 2)
364 while (use_stack
&& len
>= 1) {
365 if (buf
[len
] == '>' && buf
[len
- 1] == '-') {
368 ret
= get_other_name_sym_from_chunk(name
, buf
, len
+ 2, sym
, new_sym
);
375 ret
= get_long_name_sym(name
, sym
, new_sym
, use_stack
);
382 char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
384 return get_other_name_sym_helper(name
, sym
, new_sym
, true);
387 char *get_other_name_sym_nostack(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
389 return get_other_name_sym_helper(name
, sym
, new_sym
, false);
392 void set_extra_mod(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
395 struct symbol
*new_sym
;
397 set_extra_mod_helper(name
, sym
, expr
, state
);
398 new_name
= get_other_name_sym_nostack(name
, sym
, &new_sym
);
399 if (new_name
&& new_sym
)
400 set_extra_mod_helper(new_name
, new_sym
, NULL
, state
);
401 free_string(new_name
);
404 static struct expression
*chunk_get_array_base(struct expression
*expr
)
407 * The problem with is_array() is that it only returns true for things
408 * like foo[1] but not for foo[1].bar.
411 expr
= strip_expr(expr
);
412 while (expr
&& expr
->type
== EXPR_DEREF
)
413 expr
= strip_expr(expr
->deref
);
414 return get_array_base(expr
);
417 static int chunk_has_array(struct expression
*expr
)
419 return !!chunk_get_array_base(expr
);
422 static void clear_array_states(struct expression
*array
)
426 sm
= get_sm_state_expr(link_id
, array
);
428 match_link_modify(sm
, NULL
);
431 static void set_extra_array_mod(struct expression
*expr
, struct smatch_state
*state
)
433 struct expression
*array
;
434 struct var_sym_list
*vsl
;
439 array
= chunk_get_array_base(expr
);
441 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
443 clear_array_states(array
);
447 FOR_EACH_PTR(vsl
, vs
) {
448 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
449 } END_FOR_EACH_PTR(vs
);
451 call_extra_mod_hooks(name
, sym
, expr
, state
);
452 set_state(SMATCH_EXTRA
, name
, sym
, state
);
457 void set_extra_expr_mod(struct expression
*expr
, struct smatch_state
*state
)
462 if (chunk_has_array(expr
)) {
463 set_extra_array_mod(expr
, state
);
467 expr
= strip_expr(expr
);
468 name
= expr_to_var_sym(expr
, &sym
);
471 set_extra_mod(name
, sym
, expr
, state
);
476 void set_extra_nomod(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
479 struct symbol
*new_sym
;
480 struct relation
*rel
;
481 struct smatch_state
*orig_state
;
483 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
485 /* don't save unknown states if leaving it blank is the same */
486 if (!orig_state
&& estate_is_unknown(state
))
489 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
490 if (new_name
&& new_sym
)
491 set_extra_nomod_helper(new_name
, new_sym
, expr
, state
);
492 free_string(new_name
);
494 if (!estate_related(orig_state
)) {
495 set_extra_nomod_helper(name
, sym
, expr
, state
);
499 set_related(state
, estate_related(orig_state
));
500 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
501 struct smatch_state
*estate
;
503 estate
= get_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
);
506 set_extra_nomod_helper(rel
->name
, rel
->sym
, expr
, clone_estate_cast(estate_type(estate
), state
));
507 } END_FOR_EACH_PTR(rel
);
510 void set_extra_nomod_vsl(const char *name
, struct symbol
*sym
, struct var_sym_list
*vsl
, struct expression
*expr
, struct smatch_state
*state
)
514 FOR_EACH_PTR(vsl
, vs
) {
515 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
516 } END_FOR_EACH_PTR(vs
);
518 set_extra_nomod(name
, sym
, expr
, state
);
522 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
524 void set_extra_expr_nomod(struct expression
*expr
, struct smatch_state
*state
)
526 struct var_sym_list
*vsl
;
531 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
534 FOR_EACH_PTR(vsl
, vs
) {
535 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
536 } END_FOR_EACH_PTR(vs
);
538 set_extra_nomod(name
, sym
, expr
, state
);
543 static void set_extra_true_false(const char *name
, struct symbol
*sym
,
544 struct smatch_state
*true_state
,
545 struct smatch_state
*false_state
)
548 struct symbol
*new_sym
;
549 struct relation
*rel
;
550 struct smatch_state
*orig_state
;
552 if (!true_state
&& !false_state
)
555 if (in_warn_on_macro())
558 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
559 if (new_name
&& new_sym
)
560 set_true_false_states(SMATCH_EXTRA
, new_name
, new_sym
, true_state
, false_state
);
561 free_string(new_name
);
563 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
565 if (!estate_related(orig_state
)) {
566 set_true_false_states(SMATCH_EXTRA
, name
, sym
, true_state
, false_state
);
571 set_related(true_state
, estate_related(orig_state
));
573 set_related(false_state
, estate_related(orig_state
));
575 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
576 set_true_false_states(SMATCH_EXTRA
, rel
->name
, rel
->sym
,
577 true_state
, false_state
);
578 } END_FOR_EACH_PTR(rel
);
581 static void set_extra_chunk_true_false(struct expression
*expr
,
582 struct smatch_state
*true_state
,
583 struct smatch_state
*false_state
)
585 struct var_sym_list
*vsl
;
591 if (in_warn_on_macro())
594 type
= get_type(expr
);
598 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
601 FOR_EACH_PTR(vsl
, vs
) {
602 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
603 } END_FOR_EACH_PTR(vs
);
605 set_true_false_states(SMATCH_EXTRA
, name
, sym
,
606 clone_estate(true_state
),
607 clone_estate(false_state
));
612 static void set_extra_expr_true_false(struct expression
*expr
,
613 struct smatch_state
*true_state
,
614 struct smatch_state
*false_state
)
620 if (!true_state
&& !false_state
)
623 if (get_value(expr
, &sval
))
626 expr
= strip_expr(expr
);
627 name
= expr_to_var_sym(expr
, &sym
);
630 set_extra_chunk_true_false(expr
, true_state
, false_state
);
633 set_extra_true_false(name
, sym
, true_state
, false_state
);
637 static int get_countdown_info(struct expression
*condition
, struct expression
**unop
, int *op
, sval_t
*right
)
639 struct expression
*unop_expr
;
643 right
->type
= &int_ctype
;
646 condition
= strip_expr(condition
);
648 if (condition
->type
== EXPR_COMPARE
) {
649 comparison
= remove_unsigned_from_comparison(condition
->op
);
651 if (comparison
!= SPECIAL_GTE
&& comparison
!= '>')
653 if (!get_value(condition
->right
, &limit
))
656 unop_expr
= condition
->left
;
657 if (unop_expr
->type
!= EXPR_PREOP
&& unop_expr
->type
!= EXPR_POSTOP
)
659 if (unop_expr
->op
!= SPECIAL_DECREMENT
)
669 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
671 if (condition
->op
!= SPECIAL_DECREMENT
)
680 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
682 struct expression
*iter_var
;
683 struct expression
*condition
, *unop
;
686 struct smatch_state
*estate
;
690 right
.type
= &int_ctype
;
693 condition
= strip_expr(loop
->iterator_pre_condition
);
697 if (!get_countdown_info(condition
, &unop
, &op
, &right
))
700 iter_var
= unop
->unop
;
702 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
705 if (sval_cmp(estate_min(sm
->state
), right
) < 0)
707 start
= estate_max(sm
->state
);
709 type
= get_type(iter_var
);
710 right
= sval_cast(type
, right
);
711 start
= sval_cast(type
, start
);
713 if (sval_cmp(start
, right
) <= 0)
715 if (!sval_is_max(start
))
718 if (op
== SPECIAL_GTE
)
721 if (unop
->type
== EXPR_PREOP
) {
723 estate
= alloc_estate_range(right
, start
);
724 if (estate_has_hard_max(sm
->state
))
725 estate_set_hard_max(estate
);
726 estate_copy_fuzzy_max(estate
, sm
->state
);
727 set_extra_expr_mod(iter_var
, estate
);
729 if (unop
->type
== EXPR_POSTOP
) {
730 estate
= alloc_estate_range(right
, start
);
731 if (estate_has_hard_max(sm
->state
))
732 estate_set_hard_max(estate
);
733 estate_copy_fuzzy_max(estate
, sm
->state
);
734 set_extra_expr_mod(iter_var
, estate
);
736 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
739 static struct sm_state
*handle_canonical_for_inc(struct expression
*iter_expr
,
740 struct expression
*condition
)
742 struct expression
*iter_var
;
744 struct smatch_state
*estate
;
745 sval_t start
, end
, max
;
746 bool unknown_end
= false;
748 iter_var
= iter_expr
->unop
;
749 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
752 if (!estate_get_single_value(sm
->state
, &start
))
754 if (!get_implied_max(condition
->right
, &end
)) {
755 end
= sval_type_max(get_type(condition
->right
));
756 end
= sval_cast(start
.type
, end
);
757 if (sval_is_max(end
))
761 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
764 switch (condition
->op
) {
765 case SPECIAL_UNSIGNED_LT
:
766 case SPECIAL_NOTEQUAL
:
768 if (!sval_is_min(end
) && !unknown_end
)
771 case SPECIAL_UNSIGNED_LTE
:
777 if (sval_cmp(end
, start
) < 0)
779 end
= sval_cast(start
.type
, end
);
780 estate
= alloc_estate_range(start
, end
);
781 if (get_hard_max(condition
->right
, &max
)) {
782 if (!get_macro_name(condition
->pos
))
783 estate_set_hard_max(estate
);
784 if (condition
->op
== '<' ||
785 condition
->op
== SPECIAL_UNSIGNED_LT
||
786 condition
->op
== SPECIAL_NOTEQUAL
)
788 max
= sval_cast(start
.type
, max
);
789 estate_set_fuzzy_max(estate
, max
);
791 set_extra_expr_mod(iter_var
, estate
);
792 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
795 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
796 struct expression
*condition
)
798 struct expression
*iter_var
;
800 struct smatch_state
*estate
;
803 iter_var
= iter_expr
->unop
;
804 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
807 if (!estate_get_single_value(sm
->state
, &start
))
809 if (!get_implied_min(condition
->right
, &end
))
810 end
= sval_type_min(get_type(iter_var
));
811 end
= sval_cast(estate_type(sm
->state
), end
);
812 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
815 switch (condition
->op
) {
816 case SPECIAL_NOTEQUAL
:
818 if (!sval_is_max(end
))
826 if (sval_cmp(end
, start
) > 0)
828 estate
= alloc_estate_range(end
, start
);
829 estate_set_hard_max(estate
);
830 estate_set_fuzzy_max(estate
, estate_get_fuzzy_max(estate
));
831 set_extra_expr_mod(iter_var
, estate
);
832 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
835 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
837 struct expression
*iter_expr
;
838 struct expression
*condition
;
840 if (!loop
->iterator_post_statement
)
842 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
844 iter_expr
= loop
->iterator_post_statement
->expression
;
845 if (!loop
->iterator_pre_condition
)
847 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
849 condition
= loop
->iterator_pre_condition
;
851 if (iter_expr
->op
== SPECIAL_INCREMENT
)
852 return handle_canonical_for_inc(iter_expr
, condition
);
853 if (iter_expr
->op
== SPECIAL_DECREMENT
)
854 return handle_canonical_for_dec(iter_expr
, condition
);
858 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct stree
**stree
)
860 struct sm_state
*ret
;
863 * Canonical loops are a hack. The proper way to handle this is to
864 * use two passes, but unfortunately, doing two passes makes parsing
865 * code twice as slow.
867 * What we do is we set the inside state here, which overwrites whatever
868 * __extra_match_condition() does. Then we set the outside state in
869 * __extra_pre_loop_hook_after().
872 __push_fake_cur_stree();
873 if (!loop
->iterator_post_statement
)
874 ret
= handle_canonical_while_count_down(loop
);
876 ret
= handle_canonical_for_loops(loop
);
877 *stree
= __pop_fake_cur_stree();
881 int __iterator_unchanged(struct sm_state
*sm
)
885 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
890 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
892 struct expression
*unop
;
894 sval_t limit
, after_value
;
896 if (!get_countdown_info(condition
, &unop
, &op
, &limit
))
898 after_value
= estate_min(sm
->state
);
900 set_extra_mod(sm
->name
, sm
->sym
, condition
->unop
, alloc_estate_sval(after_value
));
903 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
904 struct statement
*iterator
,
905 struct expression
*condition
)
907 struct expression
*iter_expr
;
909 struct smatch_state
*state
;
913 while_count_down_after(sm
, condition
);
917 iter_expr
= iterator
->expression
;
919 if (condition
->type
!= EXPR_COMPARE
)
922 if (iter_expr
->op
== SPECIAL_INCREMENT
) {
923 if (!get_implied_value(condition
->right
, &end
) &&
924 sval_is_max(estate_max(sm
->state
)))
925 limit
= estate_max(sm
->state
);
927 limit
= sval_binop(estate_max(sm
->state
), '+',
928 sval_type_val(estate_type(sm
->state
), 1));
930 limit
= sval_binop(estate_min(sm
->state
), '-',
931 sval_type_val(estate_type(sm
->state
), 1));
933 limit
= sval_cast(estate_type(sm
->state
), limit
);
934 if (!estate_has_hard_max(sm
->state
) && !__has_breaks()) {
935 if (iter_expr
->op
== SPECIAL_INCREMENT
)
936 state
= alloc_estate_range(estate_min(sm
->state
), limit
);
938 state
= alloc_estate_range(limit
, estate_max(sm
->state
));
940 state
= alloc_estate_sval(limit
);
942 if (!estate_has_hard_max(sm
->state
)) {
943 estate_clear_hard_max(state
);
945 if (estate_has_fuzzy_max(sm
->state
)) {
946 sval_t hmax
= estate_get_fuzzy_max(sm
->state
);
947 sval_t max
= estate_max(sm
->state
);
949 if (sval_cmp(hmax
, max
) != 0)
950 estate_clear_fuzzy_max(state
);
951 } else if (!estate_has_fuzzy_max(sm
->state
)) {
952 estate_clear_fuzzy_max(state
);
955 set_extra_mod(sm
->name
, sm
->sym
, iter_expr
, state
);
958 static bool get_global_rl(const char *name
, struct symbol
*sym
, struct range_list
**rl
)
960 struct expression
*expr
;
962 if (!sym
|| !(sym
->ctype
.modifiers
& MOD_TOPLEVEL
) || !sym
->ident
)
964 if (strcmp(sym
->ident
->name
, name
) != 0)
967 expr
= symbol_expression(sym
);
968 return get_implied_rl(expr
, rl
);
971 static struct stree
*unmatched_stree
;
972 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
974 struct smatch_state
*state
;
975 struct range_list
*rl
;
977 if (unmatched_stree
) {
978 state
= get_state_stree(unmatched_stree
, SMATCH_EXTRA
, sm
->name
, sm
->sym
);
982 if (parent_is_gone_var_sym(sm
->name
, sm
->sym
))
983 return alloc_estate_empty();
984 if (get_global_rl(sm
->name
, sm
->sym
, &rl
))
985 return alloc_estate_rl(rl
);
986 return alloc_estate_whole(estate_type(sm
->state
));
989 static void clear_the_pointed_at(struct expression
*expr
)
994 struct sm_state
*tmp
;
996 name
= expr_to_var_sym(expr
, &sym
);
1000 stree
= __get_cur_stree();
1001 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
1002 if (tmp
->name
[0] != '*')
1004 if (tmp
->sym
!= sym
)
1006 if (strcmp(tmp
->name
+ 1, name
) != 0)
1008 set_extra_mod(tmp
->name
, tmp
->sym
, expr
, alloc_estate_whole(estate_type(tmp
->state
)));
1009 } END_FOR_EACH_SM(tmp
);
1015 static int is_const_param(struct expression
*expr
, int param
)
1017 struct symbol
*type
;
1019 type
= get_arg_type(expr
, param
);
1022 if (type
->ctype
.modifiers
& MOD_CONST
)
1027 static void match_function_call(struct expression
*expr
)
1029 struct expression
*arg
;
1030 struct expression
*tmp
;
1033 /* if we have the db this is handled in smatch_function_hooks.c */
1036 if (inlinable(expr
->fn
))
1039 FOR_EACH_PTR(expr
->args
, arg
) {
1041 if (is_const_param(expr
->fn
, param
))
1043 tmp
= strip_expr(arg
);
1044 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
1045 set_extra_expr_mod(tmp
->unop
, alloc_estate_whole(get_type(tmp
->unop
)));
1047 clear_the_pointed_at(tmp
);
1048 } END_FOR_EACH_PTR(arg
);
1051 int values_fit_type(struct expression
*left
, struct expression
*right
)
1053 struct range_list
*rl
;
1054 struct symbol
*type
;
1056 type
= get_type(left
);
1059 get_absolute_rl(right
, &rl
);
1060 if (type
== rl_type(rl
))
1062 if (type_unsigned(type
) && sval_is_negative(rl_min(rl
)))
1064 if (sval_cmp(sval_type_min(type
), rl_min(rl
)) > 0)
1066 if (sval_cmp(sval_type_max(type
), rl_max(rl
)) < 0)
1071 static void save_chunk_info(struct expression
*left
, struct expression
*right
)
1073 struct var_sym_list
*vsl
;
1075 struct expression
*add_expr
;
1076 struct symbol
*type
;
1081 if (right
->type
!= EXPR_BINOP
|| right
->op
!= '-')
1083 if (!get_value(right
->left
, &sval
))
1085 if (!expr_to_sym(right
->right
))
1088 add_expr
= binop_expression(left
, '+', right
->right
);
1089 type
= get_type(add_expr
);
1092 name
= expr_to_chunk_sym_vsl(add_expr
, &sym
, &vsl
);
1095 FOR_EACH_PTR(vsl
, vs
) {
1096 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
1097 } END_FOR_EACH_PTR(vs
);
1099 set_state(SMATCH_EXTRA
, name
, sym
, alloc_estate_sval(sval_cast(type
, sval
)));
1104 static void do_array_assign(struct expression
*left
, int op
, struct expression
*right
)
1106 struct range_list
*rl
;
1109 get_absolute_rl(right
, &rl
);
1110 rl
= cast_rl(get_type(left
), rl
);
1112 rl
= alloc_whole_rl(get_type(left
));
1115 set_extra_array_mod(left
, alloc_estate_rl(rl
));
1118 static void match_vanilla_assign(struct expression
*left
, struct expression
*right
)
1120 struct range_list
*orig_rl
= NULL
;
1121 struct range_list
*rl
= NULL
;
1122 struct symbol
*right_sym
;
1123 struct symbol
*left_type
;
1124 struct symbol
*right_type
;
1125 char *right_name
= NULL
;
1129 struct smatch_state
*state
;
1132 if (is_struct(left
))
1135 save_chunk_info(left
, right
);
1137 name
= expr_to_var_sym(left
, &sym
);
1139 if (chunk_has_array(left
))
1140 do_array_assign(left
, '=', right
);
1144 left_type
= get_type(left
);
1145 right_type
= get_type(right
);
1147 right_name
= expr_to_var_sym(right
, &right_sym
);
1149 if (!__in_fake_assign
&&
1150 !(right
->type
== EXPR_PREOP
&& right
->op
== '&') &&
1151 right_name
&& right_sym
&&
1152 values_fit_type(left
, strip_expr(right
)) &&
1153 !has_symbol(right
, sym
)) {
1154 set_equiv(left
, right
);
1158 if (get_implied_value(right
, &sval
)) {
1159 state
= alloc_estate_sval(sval_cast(left_type
, sval
));
1163 if (__in_fake_assign
|| is_fake_var(left
)) {
1164 struct smatch_state
*right_state
;
1165 struct range_list
*rl
;
1167 right_state
= get_state(SMATCH_EXTRA
, right_name
, right_sym
);
1169 /* simple assignment */
1170 state
= clone_estate(right_state
);
1174 if (get_implied_rl(right
, &rl
)) {
1175 rl
= cast_rl(left_type
, rl
);
1176 state
= alloc_estate_rl(rl
);
1180 state
= alloc_estate_rl(alloc_whole_rl(left_type
));
1184 comparison
= get_comparison_no_extra(left
, right
);
1186 comparison
= flip_comparison(comparison
);
1187 get_implied_rl(left
, &orig_rl
);
1190 if (get_implied_rl(right
, &rl
)) {
1191 rl
= cast_rl(left_type
, rl
);
1193 filter_by_comparison(&rl
, comparison
, orig_rl
);
1194 state
= alloc_estate_rl(rl
);
1195 if (get_hard_max(right
, &max
)) {
1196 estate_set_hard_max(state
);
1197 estate_set_fuzzy_max(state
, max
);
1200 rl
= alloc_whole_rl(right_type
);
1201 rl
= cast_rl(left_type
, rl
);
1203 filter_by_comparison(&rl
, comparison
, orig_rl
);
1204 state
= alloc_estate_rl(rl
);
1208 set_extra_mod(name
, sym
, left
, state
);
1210 free_string(right_name
);
1213 static void match_assign(struct expression
*expr
)
1215 struct range_list
*rl
= NULL
;
1216 struct expression
*left
;
1217 struct expression
*right
;
1218 struct expression
*binop_expr
;
1219 struct symbol
*left_type
;
1223 left
= strip_expr(expr
->left
);
1225 right
= strip_parens(expr
->right
);
1226 if (right
->type
== EXPR_CALL
&& sym_name_is("__builtin_expect", right
->fn
))
1227 right
= get_argument_from_call_expr(right
->args
, 0);
1228 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
1229 right
= strip_parens(right
->left
);
1231 if (expr
->op
== '=' && is_condition(expr
->right
))
1232 return; /* handled in smatch_condition.c */
1233 if (expr
->op
== '=' && right
->type
== EXPR_CALL
&&
1234 !is_fake_call(right
))
1235 return; /* handled in smatch_function_hooks.c */
1236 if (expr
->op
== '=') {
1237 match_vanilla_assign(left
, right
);
1241 name
= expr_to_var_sym(left
, &sym
);
1245 left_type
= get_type(left
);
1248 case SPECIAL_ADD_ASSIGN
:
1249 case SPECIAL_SUB_ASSIGN
:
1250 case SPECIAL_AND_ASSIGN
:
1251 case SPECIAL_MOD_ASSIGN
:
1252 case SPECIAL_SHL_ASSIGN
:
1253 case SPECIAL_SHR_ASSIGN
:
1254 case SPECIAL_OR_ASSIGN
:
1255 case SPECIAL_XOR_ASSIGN
:
1256 case SPECIAL_MUL_ASSIGN
:
1257 case SPECIAL_DIV_ASSIGN
:
1258 binop_expr
= binop_expression(expr
->left
,
1259 op_remove_assign(expr
->op
),
1261 get_absolute_rl(binop_expr
, &rl
);
1262 rl
= cast_rl(left_type
, rl
);
1263 if (inside_loop()) {
1264 if (expr
->op
== SPECIAL_ADD_ASSIGN
)
1265 add_range(&rl
, rl_max(rl
), sval_type_max(rl_type(rl
)));
1267 if (expr
->op
== SPECIAL_SUB_ASSIGN
&&
1268 !sval_is_negative(rl_min(rl
))) {
1269 sval_t zero
= { .type
= rl_type(rl
) };
1271 add_range(&rl
, rl_min(rl
), zero
);
1274 set_extra_mod(name
, sym
, left
, alloc_estate_rl(rl
));
1277 set_extra_mod(name
, sym
, left
, alloc_estate_whole(left_type
));
1282 static struct smatch_state
*increment_state(struct smatch_state
*state
)
1284 sval_t min
= estate_min(state
);
1285 sval_t max
= estate_max(state
);
1287 if (!estate_rl(state
))
1291 max
= sval_type_max(max
.type
);
1293 if (!sval_is_min(min
) && !sval_is_max(min
))
1295 if (!sval_is_min(max
) && !sval_is_max(max
))
1297 return alloc_estate_range(min
, max
);
1300 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
1302 sval_t min
= estate_min(state
);
1303 sval_t max
= estate_max(state
);
1305 if (!estate_rl(state
))
1309 min
= sval_type_min(min
.type
);
1311 if (!sval_is_min(min
) && !sval_is_max(min
))
1313 if (!sval_is_min(max
) && !sval_is_max(max
))
1315 return alloc_estate_range(min
, max
);
1318 static void clear_pointed_at_state(struct expression
*expr
)
1320 struct symbol
*type
;
1323 * ALERT: This is sort of a mess. If it's is a struct assigment like
1324 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1325 * the same thing for p++ where "p" is a struct. Most modifications
1326 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1327 * use smatch_modification.c because we have to get the ordering right
1328 * or something. So if you have p++ where p is a pointer to a standard
1329 * c type then we handle that here. What a mess.
1331 expr
= strip_expr(expr
);
1332 type
= get_type(expr
);
1333 if (!type
|| type
->type
!= SYM_PTR
)
1335 type
= get_real_base_type(type
);
1336 if (!type
|| type
->type
!= SYM_BASETYPE
)
1338 set_extra_expr_nomod(deref_expression(expr
), alloc_estate_whole(type
));
1341 static void unop_expr(struct expression
*expr
)
1343 struct smatch_state
*state
;
1345 if (expr
->smatch_flags
& Handled
)
1349 case SPECIAL_INCREMENT
:
1350 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
1351 state
= increment_state(state
);
1353 state
= alloc_estate_whole(get_type(expr
));
1354 set_extra_expr_mod(expr
->unop
, state
);
1355 clear_pointed_at_state(expr
->unop
);
1357 case SPECIAL_DECREMENT
:
1358 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
1359 state
= decrement_state(state
);
1361 state
= alloc_estate_whole(get_type(expr
));
1362 set_extra_expr_mod(expr
->unop
, state
);
1363 clear_pointed_at_state(expr
->unop
);
1370 static void asm_expr(struct statement
*stmt
)
1372 struct asm_operand
*op
;
1373 struct symbol
*type
;
1375 FOR_EACH_PTR(stmt
->asm_outputs
, op
) {
1376 type
= get_type(strip_expr(op
->expr
));
1377 set_extra_expr_mod(op
->expr
, alloc_estate_whole(type
));
1378 } END_FOR_EACH_PTR(op
);
1381 static void check_dereference(struct expression
*expr
)
1383 struct smatch_state
*state
;
1385 if (__in_fake_assign
)
1387 if (outside_of_function())
1389 state
= get_extra_state(expr
);
1391 struct range_list
*rl
;
1393 rl
= rl_intersection(estate_rl(state
), valid_ptr_rl
);
1394 if (rl_equiv(rl
, estate_rl(state
)))
1396 set_extra_expr_nomod(expr
, alloc_estate_rl(rl
));
1398 struct range_list
*rl
;
1400 if (get_mtag_rl(expr
, &rl
))
1401 rl
= rl_intersection(rl
, valid_ptr_rl
);
1403 rl
= clone_rl(valid_ptr_rl
);
1405 set_extra_expr_nomod(expr
, alloc_estate_rl(rl
));
1409 static void match_dereferences(struct expression
*expr
)
1411 if (expr
->type
!= EXPR_PREOP
)
1413 if (getting_address(expr
))
1415 /* it's saying that foo[1] = bar dereferences foo[1] */
1418 check_dereference(expr
->unop
);
1421 static void match_pointer_as_array(struct expression
*expr
)
1423 if (!is_array(expr
))
1425 check_dereference(get_array_base(expr
));
1428 static void find_dereferences(struct expression
*expr
)
1430 while (expr
->type
== EXPR_PREOP
) {
1431 if (expr
->op
== '*')
1432 check_dereference(expr
->unop
);
1433 expr
= strip_expr(expr
->unop
);
1437 static void set_param_dereferenced(struct expression
*call
, struct expression
*arg
, char *key
, char *unused
)
1442 name
= get_variable_from_key(arg
, key
, &sym
);
1444 struct smatch_state
*orig
, *new;
1445 struct range_list
*rl
;
1447 orig
= get_state(SMATCH_EXTRA
, name
, sym
);
1449 rl
= rl_intersection(estate_rl(orig
),
1450 alloc_rl(valid_ptr_min_sval
,
1451 valid_ptr_max_sval
));
1452 new = alloc_estate_rl(rl
);
1454 new = alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
);
1457 set_extra_nomod(name
, sym
, NULL
, new);
1461 find_dereferences(arg
);
1464 static sval_t
add_one(sval_t sval
)
1470 static int handle_postop_inc(struct expression
*left
, int op
, struct expression
*right
)
1472 struct statement
*stmt
;
1473 struct expression
*cond
;
1474 struct smatch_state
*true_state
, *false_state
;
1475 struct symbol
*type
;
1480 * If we're decrementing here then that's a canonical while count down
1481 * so it's handled already. We're only handling loops like:
1483 * do { ... } while (i++ < 3);
1486 if (left
->type
!= EXPR_POSTOP
|| left
->op
!= SPECIAL_INCREMENT
)
1489 stmt
= __cur_stmt
->parent
;
1492 if (stmt
->type
== STMT_COMPOUND
)
1493 stmt
= stmt
->parent
;
1494 if (!stmt
|| stmt
->type
!= STMT_ITERATOR
|| !stmt
->iterator_post_condition
)
1497 cond
= strip_expr(stmt
->iterator_post_condition
);
1498 if (cond
->type
!= EXPR_COMPARE
|| cond
->op
!= op
)
1500 if (left
!= strip_expr(cond
->left
) || right
!= strip_expr(cond
->right
))
1503 if (!get_implied_value(left
->unop
, &start
))
1505 if (!get_implied_value(right
, &limit
))
1507 type
= get_type(left
->unop
);
1508 limit
= sval_cast(type
, limit
);
1509 if (sval_cmp(start
, limit
) > 0)
1514 case SPECIAL_UNSIGNED_LT
:
1517 case SPECIAL_UNSIGNED_LTE
:
1518 limit
= add_one(limit
);
1524 true_state
= alloc_estate_range(add_one(start
), limit
);
1525 false_state
= alloc_estate_range(add_one(limit
), add_one(limit
));
1527 /* Currently we just discard the false state but when two passes is
1528 * implimented correctly then it will use it.
1531 set_extra_expr_true_false(left
->unop
, true_state
, false_state
);
1536 bool is_impossible_variable(struct expression
*expr
)
1538 struct smatch_state
*state
;
1540 state
= get_extra_state(expr
);
1541 if (state
&& !estate_rl(state
))
1546 static bool in_macro(struct expression
*left
, struct expression
*right
)
1548 if (!left
|| !right
)
1550 if (left
->pos
.line
!= right
->pos
.line
|| left
->pos
.pos
!= right
->pos
.pos
)
1552 if (get_macro_name(left
->pos
))
1557 static void handle_comparison(struct symbol
*type
, struct expression
*left
, int op
, struct expression
*right
)
1559 struct range_list
*left_orig
;
1560 struct range_list
*left_true
;
1561 struct range_list
*left_false
;
1562 struct range_list
*right_orig
;
1563 struct range_list
*right_true
;
1564 struct range_list
*right_false
;
1565 struct smatch_state
*left_true_state
;
1566 struct smatch_state
*left_false_state
;
1567 struct smatch_state
*right_true_state
;
1568 struct smatch_state
*right_false_state
;
1569 sval_t dummy
, hard_max
;
1570 int left_postop
= 0;
1571 int right_postop
= 0;
1573 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
1574 if (left
->type
== EXPR_POSTOP
) {
1575 left
->smatch_flags
|= Handled
;
1576 left_postop
= left
->op
;
1577 if (handle_postop_inc(left
, op
, right
))
1580 left
= strip_parens(left
->unop
);
1582 while (left
->type
== EXPR_ASSIGNMENT
)
1583 left
= strip_parens(left
->left
);
1585 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
1586 if (right
->type
== EXPR_POSTOP
) {
1587 right
->smatch_flags
|= Handled
;
1588 right_postop
= right
->op
;
1590 right
= strip_parens(right
->unop
);
1593 if (is_impossible_variable(left
) || is_impossible_variable(right
))
1596 get_real_absolute_rl(left
, &left_orig
);
1597 left_orig
= cast_rl(type
, left_orig
);
1599 get_real_absolute_rl(right
, &right_orig
);
1600 right_orig
= cast_rl(type
, right_orig
);
1602 split_comparison_rl(left_orig
, op
, right_orig
, &left_true
, &left_false
, &right_true
, &right_false
);
1604 left_true
= rl_truncate_cast(get_type(strip_expr(left
)), left_true
);
1605 left_false
= rl_truncate_cast(get_type(strip_expr(left
)), left_false
);
1606 right_true
= rl_truncate_cast(get_type(strip_expr(right
)), right_true
);
1607 right_false
= rl_truncate_cast(get_type(strip_expr(right
)), right_false
);
1609 if (!left_true
|| !left_false
) {
1610 struct range_list
*tmp_true
, *tmp_false
;
1612 split_comparison_rl(alloc_whole_rl(type
), op
, right_orig
, &tmp_true
, &tmp_false
, NULL
, NULL
);
1613 tmp_true
= rl_truncate_cast(get_type(strip_expr(left
)), tmp_true
);
1614 tmp_false
= rl_truncate_cast(get_type(strip_expr(left
)), tmp_false
);
1615 if (tmp_true
&& tmp_false
)
1616 __save_imaginary_state(left
, tmp_true
, tmp_false
);
1619 if (!right_true
|| !right_false
) {
1620 struct range_list
*tmp_true
, *tmp_false
;
1622 split_comparison_rl(alloc_whole_rl(type
), op
, right_orig
, NULL
, NULL
, &tmp_true
, &tmp_false
);
1623 tmp_true
= rl_truncate_cast(get_type(strip_expr(right
)), tmp_true
);
1624 tmp_false
= rl_truncate_cast(get_type(strip_expr(right
)), tmp_false
);
1625 if (tmp_true
&& tmp_false
)
1626 __save_imaginary_state(right
, tmp_true
, tmp_false
);
1629 left_true_state
= alloc_estate_rl(left_true
);
1630 left_false_state
= alloc_estate_rl(left_false
);
1631 right_true_state
= alloc_estate_rl(right_true
);
1632 right_false_state
= alloc_estate_rl(right_false
);
1636 case SPECIAL_UNSIGNED_LT
:
1637 case SPECIAL_UNSIGNED_LTE
:
1639 if (get_hard_max(right
, &dummy
) && !in_macro(left
, right
))
1640 estate_set_hard_max(left_true_state
);
1641 if (get_hard_max(left
, &dummy
) && !in_macro(left
, right
))
1642 estate_set_hard_max(right_false_state
);
1645 case SPECIAL_UNSIGNED_GT
:
1646 case SPECIAL_UNSIGNED_GTE
:
1648 if (get_hard_max(left
, &dummy
) && !in_macro(left
, right
))
1649 estate_set_hard_max(right_true_state
);
1650 if (get_hard_max(right
, &dummy
) && !in_macro(left
, right
))
1651 estate_set_hard_max(left_false_state
);
1657 case SPECIAL_UNSIGNED_LT
:
1658 case SPECIAL_UNSIGNED_LTE
:
1660 if (get_hard_max(right
, &hard_max
)) {
1661 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1663 estate_set_fuzzy_max(left_true_state
, hard_max
);
1665 if (get_implied_value(right
, &hard_max
)) {
1666 if (op
== SPECIAL_UNSIGNED_LTE
||
1669 estate_set_fuzzy_max(left_false_state
, hard_max
);
1671 if (get_hard_max(left
, &hard_max
)) {
1672 if (op
== SPECIAL_UNSIGNED_LTE
||
1675 estate_set_fuzzy_max(right_false_state
, hard_max
);
1677 if (get_implied_value(left
, &hard_max
)) {
1678 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1680 estate_set_fuzzy_max(right_true_state
, hard_max
);
1684 case SPECIAL_UNSIGNED_GT
:
1685 case SPECIAL_UNSIGNED_GTE
:
1687 if (get_hard_max(left
, &hard_max
)) {
1688 if (op
== '>' || op
== SPECIAL_UNSIGNED_GT
)
1690 estate_set_fuzzy_max(right_true_state
, hard_max
);
1692 if (get_implied_value(left
, &hard_max
)) {
1693 if (op
== SPECIAL_UNSIGNED_GTE
||
1696 estate_set_fuzzy_max(right_false_state
, hard_max
);
1698 if (get_hard_max(right
, &hard_max
)) {
1699 if (op
== SPECIAL_UNSIGNED_LTE
||
1702 estate_set_fuzzy_max(left_false_state
, hard_max
);
1704 if (get_implied_value(right
, &hard_max
)) {
1706 op
== SPECIAL_UNSIGNED_GT
)
1708 estate_set_fuzzy_max(left_true_state
, hard_max
);
1712 if (get_hard_max(left
, &hard_max
))
1713 estate_set_fuzzy_max(right_true_state
, hard_max
);
1714 if (get_hard_max(right
, &hard_max
))
1715 estate_set_fuzzy_max(left_true_state
, hard_max
);
1719 if (get_hard_max(left
, &hard_max
)) {
1720 estate_set_hard_max(left_true_state
);
1721 estate_set_hard_max(left_false_state
);
1723 if (get_hard_max(right
, &hard_max
)) {
1724 estate_set_hard_max(right_true_state
);
1725 estate_set_hard_max(right_false_state
);
1728 if (left_postop
== SPECIAL_INCREMENT
) {
1729 left_true_state
= increment_state(left_true_state
);
1730 left_false_state
= increment_state(left_false_state
);
1732 if (left_postop
== SPECIAL_DECREMENT
) {
1733 left_true_state
= decrement_state(left_true_state
);
1734 left_false_state
= decrement_state(left_false_state
);
1736 if (right_postop
== SPECIAL_INCREMENT
) {
1737 right_true_state
= increment_state(right_true_state
);
1738 right_false_state
= increment_state(right_false_state
);
1740 if (right_postop
== SPECIAL_DECREMENT
) {
1741 right_true_state
= decrement_state(right_true_state
);
1742 right_false_state
= decrement_state(right_false_state
);
1745 if (estate_rl(left_true_state
) && estates_equiv(left_true_state
, left_false_state
)) {
1746 left_true_state
= NULL
;
1747 left_false_state
= NULL
;
1750 if (estate_rl(right_true_state
) && estates_equiv(right_true_state
, right_false_state
)) {
1751 right_true_state
= NULL
;
1752 right_false_state
= NULL
;
1755 /* Don't introduce new states for known true/false conditions */
1756 if (rl_equiv(left_orig
, estate_rl(left_true_state
)))
1757 left_true_state
= NULL
;
1758 if (rl_equiv(left_orig
, estate_rl(left_false_state
)))
1759 left_false_state
= NULL
;
1760 if (rl_equiv(right_orig
, estate_rl(right_true_state
)))
1761 right_true_state
= NULL
;
1762 if (rl_equiv(right_orig
, estate_rl(right_false_state
)))
1763 right_false_state
= NULL
;
1765 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
1766 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
1769 static int is_simple_math(struct expression
*expr
)
1773 if (expr
->type
!= EXPR_BINOP
)
1784 static int flip_op(int op
)
1786 /* We only care about simple math */
1798 static void move_known_to_rl(struct expression
**expr_p
, struct range_list
**rl_p
)
1800 struct expression
*expr
= *expr_p
;
1801 struct range_list
*rl
= *rl_p
;
1804 if (!is_simple_math(expr
))
1807 if (get_implied_value(expr
->right
, &sval
)) {
1808 *expr_p
= expr
->left
;
1809 *rl_p
= rl_binop(rl
, flip_op(expr
->op
), alloc_rl(sval
, sval
));
1810 move_known_to_rl(expr_p
, rl_p
);
1813 if (expr
->op
== '-')
1815 if (get_implied_value(expr
->left
, &sval
)) {
1816 *expr_p
= expr
->right
;
1817 *rl_p
= rl_binop(rl
, flip_op(expr
->op
), alloc_rl(sval
, sval
));
1818 move_known_to_rl(expr_p
, rl_p
);
1823 static void move_known_values(struct expression
**left_p
, struct expression
**right_p
)
1825 struct expression
*left
= *left_p
;
1826 struct expression
*right
= *right_p
;
1829 if (get_implied_value(left
, &sval
)) {
1830 if (!is_simple_math(right
))
1832 if (get_implied_value(right
, &dummy
))
1834 if (right
->op
== '*') {
1837 if (!get_value(right
->right
, &divisor
))
1839 if (divisor
.value
== 0)
1841 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1842 *right_p
= right
->left
;
1845 if (right
->op
== '+' && get_value(right
->left
, &sval
)) {
1846 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->left
);
1847 *right_p
= right
->right
;
1850 if (get_value(right
->right
, &sval
)) {
1851 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1852 *right_p
= right
->left
;
1857 if (get_implied_value(right
, &sval
)) {
1858 if (!is_simple_math(left
))
1860 if (get_implied_value(left
, &dummy
))
1862 if (left
->op
== '*') {
1865 if (!get_value(left
->right
, &divisor
))
1867 if (divisor
.value
== 0)
1869 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1870 *left_p
= left
->left
;
1873 if (left
->op
== '+' && get_value(left
->left
, &sval
)) {
1874 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->left
);
1875 *left_p
= left
->right
;
1879 if (get_value(left
->right
, &sval
)) {
1880 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1881 *left_p
= left
->left
;
1889 * The reason for do_simple_algebra() is to solve things like:
1890 * if (foo > 66 || foo + bar > 64) {
1891 * "foo" is not really a known variable so it won't be handled by
1892 * move_known_variables() but it's a super common idiom.
1895 static int do_simple_algebra(struct expression
**left_p
, struct expression
**right_p
)
1897 struct expression
*left
= *left_p
;
1898 struct expression
*right
= *right_p
;
1899 struct range_list
*rl
;
1902 if (left
->type
!= EXPR_BINOP
|| left
->op
!= '+')
1904 if (can_integer_overflow(get_type(left
), left
))
1906 if (!get_implied_value(right
, &tmp
))
1909 if (!get_implied_value(left
->left
, &tmp
) &&
1910 get_implied_rl(left
->left
, &rl
) &&
1912 *right_p
= binop_expression(right
, '-', left
->left
);
1913 *left_p
= left
->right
;
1916 if (!get_implied_value(left
->right
, &tmp
) &&
1917 get_implied_rl(left
->right
, &rl
) &&
1919 *right_p
= binop_expression(right
, '-', left
->right
);
1920 *left_p
= left
->left
;
1927 static int match_func_comparison(struct expression
*expr
)
1929 struct expression
*left
= strip_expr(expr
->left
);
1930 struct expression
*right
= strip_expr(expr
->right
);
1932 if (left
->type
== EXPR_CALL
|| right
->type
== EXPR_CALL
) {
1933 function_comparison(left
, expr
->op
, right
);
1940 /* Handle conditions like "if (foo + bar < foo) {" */
1941 static int handle_integer_overflow_test(struct expression
*expr
)
1943 struct expression
*left
, *right
;
1944 struct symbol
*type
;
1945 sval_t left_min
, right_min
, min
, max
;
1947 if (expr
->op
!= '<' && expr
->op
!= SPECIAL_UNSIGNED_LT
)
1950 left
= strip_parens(expr
->left
);
1951 right
= strip_parens(expr
->right
);
1953 if (left
->op
!= '+')
1956 type
= get_type(expr
);
1959 if (type_positive_bits(type
) == 32) {
1960 max
.type
= &uint_ctype
;
1961 max
.uvalue
= (unsigned int)-1;
1962 } else if (type_positive_bits(type
) == 64) {
1963 max
.type
= &ulong_ctype
;
1964 max
.value
= (unsigned long long)-1;
1969 if (!expr_equiv(left
->left
, right
) && !expr_equiv(left
->right
, right
))
1972 get_absolute_min(left
->left
, &left_min
);
1973 get_absolute_min(left
->right
, &right_min
);
1974 min
= sval_binop(left_min
, '+', right_min
);
1976 type
= get_type(left
);
1977 min
= sval_cast(type
, min
);
1978 max
= sval_cast(type
, max
);
1980 set_extra_chunk_true_false(left
, NULL
, alloc_estate_range(min
, max
));
1984 static void match_comparison(struct expression
*expr
)
1986 struct expression
*left_orig
= strip_parens(expr
->left
);
1987 struct expression
*right_orig
= strip_parens(expr
->right
);
1988 struct expression
*left
, *right
, *tmp
;
1989 struct expression
*prev
;
1990 struct symbol
*type
;
1993 if (match_func_comparison(expr
))
1996 type
= get_type(expr
);
1998 type
= &llong_ctype
;
2000 if (handle_integer_overflow_test(expr
))
2005 move_known_values(&left
, &right
);
2006 handle_comparison(type
, left
, expr
->op
, right
);
2010 if (do_simple_algebra(&left
, &right
))
2011 handle_comparison(type
, left
, expr
->op
, right
);
2013 prev
= get_assigned_expr(left_orig
);
2014 if (is_simple_math(prev
) && !has_variable(prev
, left_orig
)) {
2017 move_known_values(&left
, &right
);
2018 handle_comparison(type
, left
, expr
->op
, right
);
2021 prev
= get_assigned_expr(right_orig
);
2022 if (is_simple_math(prev
) && !has_variable(prev
, right_orig
)) {
2025 move_known_values(&left
, &right
);
2026 handle_comparison(type
, left
, expr
->op
, right
);
2032 if (get_last_expr_from_expression_stmt(left_orig
)) {
2033 left
= get_last_expr_from_expression_stmt(left_orig
);
2036 if (get_last_expr_from_expression_stmt(right_orig
)) {
2037 right
= get_last_expr_from_expression_stmt(right_orig
);
2045 while ((tmp
= get_assigned_expr(left
))) {
2048 left
= strip_expr(tmp
);
2051 while ((tmp
= get_assigned_expr(right
))) {
2054 right
= strip_expr(tmp
);
2057 handle_comparison(type
, left
, expr
->op
, right
);
2060 static sval_t
get_high_mask(sval_t known
)
2068 for (i
= type_bits(known
.type
) - 1; i
>= 0; i
--) {
2069 if (known
.uvalue
& (1ULL << i
))
2070 ret
.uvalue
|= (1ULL << i
);
2078 static bool handle_bit_test(struct expression
*expr
)
2080 struct range_list
*orig_rl
, *rl
;
2081 struct expression
*shift
, *mask
, *var
;
2082 struct bit_info
*bit_info
;
2084 sval_t high
= { .type
= &int_ctype
};
2085 sval_t low
= { .type
= &int_ctype
};
2087 shift
= strip_expr(expr
->right
);
2088 mask
= strip_expr(expr
->left
);
2089 if (shift
->type
!= EXPR_BINOP
|| shift
->op
!= SPECIAL_LEFTSHIFT
) {
2090 shift
= strip_expr(expr
->left
);
2091 mask
= strip_expr(expr
->right
);
2092 if (shift
->type
!= EXPR_BINOP
|| shift
->op
!= SPECIAL_LEFTSHIFT
)
2095 if (!get_implied_value(shift
->left
, &sval
) || sval
.value
!= 1)
2097 var
= strip_expr(shift
->right
);
2099 bit_info
= get_bit_info(mask
);
2102 if (!bit_info
->possible
)
2105 get_absolute_rl(var
, &orig_rl
);
2106 if (sval_is_negative(rl_min(orig_rl
)) ||
2107 rl_max(orig_rl
).uvalue
> type_bits(get_type(shift
->left
)))
2110 low
.value
= ffsll(bit_info
->possible
);
2111 high
.value
= sm_fls64(bit_info
->possible
);
2112 rl
= alloc_rl(low
, high
);
2113 rl
= cast_rl(get_type(var
), rl
);
2114 rl
= rl_intersection(orig_rl
, rl
);
2118 set_extra_expr_true_false(shift
->right
, alloc_estate_rl(rl
), NULL
);
2123 static void handle_AND_op(struct expression
*var
, sval_t known
)
2125 struct range_list
*orig_rl
;
2126 struct range_list
*true_rl
= NULL
;
2127 struct range_list
*false_rl
= NULL
;
2129 sval_t low_mask
= known
;
2133 get_absolute_rl(var
, &orig_rl
);
2135 if (known
.value
> 0) {
2136 bit
= ffsll(known
.value
) - 1;
2137 low_mask
.uvalue
= (1ULL << bit
) - 1;
2138 true_rl
= remove_range(orig_rl
, sval_type_val(known
.type
, 0), low_mask
);
2140 high_mask
= get_high_mask(known
);
2141 if (high_mask
.value
) {
2142 bit
= ffsll(high_mask
.value
) - 1;
2143 low_mask
.uvalue
= (1ULL << bit
) - 1;
2146 if (sval_is_negative(rl_min(orig_rl
)))
2147 false_rl
= remove_range(false_rl
, sval_type_min(known
.type
), sval_type_val(known
.type
, -1));
2148 false_rl
= remove_range(false_rl
, low_mask
, sval_type_max(known
.type
));
2149 if (type_signed(high_mask
.type
) && type_unsigned(rl_type(false_rl
))) {
2150 false_rl
= remove_range(false_rl
,
2151 sval_type_val(rl_type(false_rl
), sval_type_max(known
.type
).uvalue
),
2152 sval_type_val(rl_type(false_rl
), -1));
2154 } else if (known
.value
== 1 &&
2155 get_hard_max(var
, &max
) &&
2156 sval_cmp(max
, rl_max(orig_rl
)) == 0 &&
2158 false_rl
= remove_range(orig_rl
, max
, max
);
2160 set_extra_expr_true_false(var
,
2161 true_rl
? alloc_estate_rl(true_rl
) : NULL
,
2162 false_rl
? alloc_estate_rl(false_rl
) : NULL
);
2165 static void handle_AND_condition(struct expression
*expr
)
2169 if (handle_bit_test(expr
))
2172 if (get_implied_value(expr
->left
, &known
))
2173 handle_AND_op(expr
->right
, known
);
2174 else if (get_implied_value(expr
->right
, &known
))
2175 handle_AND_op(expr
->left
, known
);
2178 static void handle_MOD_condition(struct expression
*expr
)
2180 struct range_list
*orig_rl
;
2181 struct range_list
*true_rl
;
2182 struct range_list
*false_rl
= NULL
;
2188 if (!get_implied_value(expr
->right
, &right
) || right
.value
== 0)
2190 get_absolute_rl(expr
->left
, &orig_rl
);
2192 zero
.type
= rl_type(orig_rl
);
2194 /* We're basically dorking around the min and max here */
2195 true_rl
= remove_range(orig_rl
, zero
, zero
);
2196 if (!sval_is_max(rl_max(true_rl
)) &&
2197 !(rl_max(true_rl
).value
% right
.value
))
2198 true_rl
= remove_range(true_rl
, rl_max(true_rl
), rl_max(true_rl
));
2200 if (rl_equiv(true_rl
, orig_rl
))
2203 if (sval_is_positive(rl_min(orig_rl
)) &&
2204 (rl_max(orig_rl
).value
- rl_min(orig_rl
).value
) / right
.value
< 5) {
2208 add
= rl_min(orig_rl
);
2209 add
.value
+= right
.value
- (add
.value
% right
.value
);
2210 add
.value
-= right
.value
;
2212 for (i
= 0; i
< 5; i
++) {
2213 add
.value
+= right
.value
;
2214 if (add
.value
> rl_max(orig_rl
).value
)
2216 add_range(&false_rl
, add
, add
);
2219 if (rl_min(orig_rl
).uvalue
!= 0 &&
2220 rl_min(orig_rl
).uvalue
< right
.uvalue
) {
2221 sval_t chop
= right
;
2223 false_rl
= remove_range(orig_rl
, zero
, chop
);
2226 if (!sval_is_max(rl_max(orig_rl
)) &&
2227 (rl_max(orig_rl
).value
% right
.value
)) {
2228 sval_t chop
= rl_max(orig_rl
);
2229 chop
.value
-= chop
.value
% right
.value
;
2232 false_rl
= clone_rl(orig_rl
);
2233 false_rl
= remove_range(false_rl
, chop
, rl_max(orig_rl
));
2237 set_extra_expr_true_false(expr
->left
,
2238 true_rl
? alloc_estate_rl(true_rl
) : NULL
,
2239 false_rl
? alloc_estate_rl(false_rl
) : NULL
);
2242 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
2243 void __extra_match_condition(struct expression
*expr
)
2245 expr
= strip_expr(expr
);
2246 switch (expr
->type
) {
2248 function_comparison(expr
, SPECIAL_NOTEQUAL
, zero_expr());
2253 handle_comparison(get_type(expr
), expr
, SPECIAL_NOTEQUAL
, zero_expr());
2256 match_comparison(expr
);
2258 case EXPR_ASSIGNMENT
:
2259 __extra_match_condition(expr
->left
);
2262 if (expr
->op
== '&')
2263 handle_AND_condition(expr
);
2264 if (expr
->op
== '%')
2265 handle_MOD_condition(expr
);
2270 static void assume_indexes_are_valid(struct expression
*expr
)
2272 struct expression
*array_expr
;
2274 struct expression
*offset
;
2275 struct symbol
*offset_type
;
2276 struct range_list
*rl_before
;
2277 struct range_list
*rl_after
;
2278 struct range_list
*filter
= NULL
;
2281 expr
= strip_expr(expr
);
2282 if (!is_array(expr
))
2285 offset
= get_array_offset(expr
);
2286 offset_type
= get_type(offset
);
2287 if (offset_type
&& type_signed(offset_type
)) {
2288 filter
= alloc_rl(sval_type_min(offset_type
),
2289 sval_type_val(offset_type
, -1));
2292 array_expr
= get_array_base(expr
);
2293 array_size
= get_real_array_size(array_expr
);
2294 if (array_size
> 1) {
2295 size
= sval_type_val(offset_type
, array_size
);
2296 add_range(&filter
, size
, sval_type_max(offset_type
));
2301 get_absolute_rl(offset
, &rl_before
);
2302 rl_after
= rl_filter(rl_before
, filter
);
2303 if (rl_equiv(rl_before
, rl_after
))
2305 set_extra_expr_nomod(offset
, alloc_estate_rl(rl_after
));
2308 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
2309 int implied_not_equal(struct expression
*expr
, long long val
)
2311 return !possibly_false(expr
, SPECIAL_NOTEQUAL
, value_expr(val
));
2314 int implied_not_equal_name_sym(char *name
, struct symbol
*sym
, long long val
)
2316 struct smatch_state
*estate
;
2318 estate
= get_state(SMATCH_EXTRA
, name
, sym
);
2321 if (!rl_has_sval(estate_rl(estate
), sval_type_val(estate_type(estate
), 0)))
2326 bool is_noderef_ptr(struct expression
*expr
)
2328 struct range_list
*rl
;
2330 if (!get_implied_rl(expr
, &rl
))
2332 return is_noderef_ptr_rl(rl
);
2335 static int parent_is_err_or_null_var_sym_helper(const char *name
, struct symbol
*sym
, bool check_err_ptr
)
2337 struct smatch_state
*state
;
2342 strncpy(buf
, name
, sizeof(buf
) - 1);
2343 buf
[sizeof(buf
) - 1] = '\0';
2346 while (*start
== '*') {
2348 state
= __get_state(SMATCH_EXTRA
, start
, sym
);
2351 if (!estate_rl(state
))
2353 if (is_noderef_ptr_rl(estate_rl(state
)))
2358 while (*start
== '&')
2361 len
= strlen(start
);
2365 if (start
[len
] == '-' ||
2366 start
[len
] == '.') {
2373 state
= __get_state(SMATCH_EXTRA
, start
, sym
);
2376 if (is_noderef_ptr_rl(estate_rl(state
)))
2381 int parent_is_null_var_sym(const char *name
, struct symbol
*sym
)
2383 return parent_is_err_or_null_var_sym_helper(name
, sym
, false);
2386 int parent_is_err_or_null_var_sym(const char *name
, struct symbol
*sym
)
2388 return parent_is_err_or_null_var_sym_helper(name
, sym
, (option_project
== PROJ_KERNEL
));
2391 int parent_is_null(struct expression
*expr
)
2397 expr
= strip_expr(expr
);
2398 var
= expr_to_var_sym(expr
, &sym
);
2401 ret
= parent_is_null_var_sym(var
, sym
);
2407 static int param_used_callback(void *found
, int argc
, char **argv
, char **azColName
)
2413 static int is_kzalloc_info(struct sm_state
*sm
)
2418 * kzalloc() information is treated as special because so there is just
2419 * a lot of stuff initialized to zero and it makes building the database
2420 * take hours and hours.
2422 * In theory, we should just remove this line and not pass any unused
2423 * information, but I'm not sure enough that this code works so I want
2424 * to hold off on that for now.
2426 if (!estate_get_single_value(sm
->state
, &sval
))
2428 if (sval
.value
!= 0)
2433 static int is_really_long(struct sm_state
*sm
)
2439 while ((p
= strstr(p
, "->"))) {
2445 strlen(sm
->name
) < 40)
2450 static int filter_unused_param_value_info(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
2454 /* for function pointers assume everything is used */
2455 if (call
->fn
->type
!= EXPR_SYMBOL
)
2458 if (strcmp(printed_name
, "$") == 0 ||
2459 strcmp(printed_name
, "*$") == 0)
2463 * This is to handle __builtin_mul_overflow(). In an ideal world we
2464 * would only need this for invalid code.
2467 if (!call
->fn
->symbol
)
2470 if (!is_kzalloc_info(sm
) && !is_really_long(sm
))
2473 run_sql(¶m_used_callback
, &found
,
2474 "select * from return_implies where %s and type = %d and parameter = %d and key = '%s';",
2475 get_static_filter(call
->fn
->symbol
), PARAM_USED
, param
, printed_name
);
2479 /* If the database is not built yet, then assume everything is used */
2480 run_sql(¶m_used_callback
, &found
,
2481 "select * from return_implies where %s and type = %d;",
2482 get_static_filter(call
->fn
->symbol
), PARAM_USED
);
2489 struct range_list
*intersect_with_real_abs_var_sym(const char *name
, struct symbol
*sym
, struct range_list
*start
)
2491 struct smatch_state
*state
;
2494 * Here is the difference between implied value and real absolute, say
2499 * Then you know that a is 0-255. That's real absolute. But you don't
2500 * know for sure that it actually goes up to 255. So it's not implied.
2501 * Implied indicates a degree of certainty.
2503 * But then say you cap "a" at 8. That means you know it goes up to
2504 * 8. So now the implied value is s32min-8. But you can combine it
2505 * with the real absolute to say that actually it's 0-8.
2507 * We are combining it here. But now that I think about it, this is
2508 * probably not the ideal place to combine it because it should proably
2509 * be done earlier. Oh well, this is an improvement on what was there
2510 * before so I'm going to commit this code.
2514 state
= get_real_absolute_state_var_sym(name
, sym
);
2515 if (!state
|| !estate_rl(state
))
2518 return rl_intersection(estate_rl(state
), start
);
2521 struct range_list
*intersect_with_real_abs_expr(struct expression
*expr
, struct range_list
*start
)
2523 struct smatch_state
*state
;
2524 struct range_list
*abs_rl
;
2526 state
= get_real_absolute_state(expr
);
2527 if (!state
|| !estate_rl(state
))
2530 abs_rl
= cast_rl(rl_type(start
), estate_rl(state
));
2531 return rl_intersection(abs_rl
, start
);
2534 static void caller_info_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
2536 struct range_list
*rl
;
2539 if (estate_is_whole(sm
->state
) || !estate_rl(sm
->state
))
2541 if (filter_unused_param_value_info(call
, param
, printed_name
, sm
))
2543 rl
= estate_rl(sm
->state
);
2544 rl
= intersect_with_real_abs_var_sym(sm
->name
, sm
->sym
, rl
);
2547 sql_insert_caller_info(call
, PARAM_VALUE
, param
, printed_name
, show_rl(rl
));
2548 if (!estate_get_single_value(sm
->state
, &dummy
)) {
2549 if (estate_has_hard_max(sm
->state
))
2550 sql_insert_caller_info(call
, HARD_MAX
, param
, printed_name
,
2551 sval_to_str(estate_max(sm
->state
)));
2552 if (estate_has_fuzzy_max(sm
->state
))
2553 sql_insert_caller_info(call
, FUZZY_MAX
, param
, printed_name
,
2554 sval_to_str(estate_get_fuzzy_max(sm
->state
)));
2558 static void returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
2560 struct symbol
*returned_sym
;
2561 char *returned_name
;
2562 struct sm_state
*sm
;
2570 if (!is_pointer(expr
))
2572 if (return_ranges
&& strstr(return_ranges
, "[==$"))
2575 returned_name
= expr_to_var_sym(expr
, &returned_sym
);
2576 if (!returned_name
|| !returned_sym
)
2578 len
= strlen(returned_name
);
2580 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), sm
) {
2581 if (!estate_rl(sm
->state
))
2583 if (returned_sym
!= sm
->sym
)
2585 if (strncmp(returned_name
, sm
->name
, len
) != 0)
2587 if (sm
->name
[len
] != '-')
2590 snprintf(name_buf
, sizeof(name_buf
), "$%s", sm
->name
+ len
);
2592 compare_str
= name_sym_to_param_comparison(sm
->name
, sm
->sym
);
2593 if (!compare_str
&& estate_is_whole(sm
->state
))
2595 snprintf(val_buf
, sizeof(val_buf
), "%s%s", sm
->state
->name
, compare_str
?: "");
2597 sql_insert_return_states(return_id
, return_ranges
, PARAM_VALUE
,
2598 -1, name_buf
, val_buf
);
2599 } END_FOR_EACH_SM(sm
);
2602 free_string(returned_name
);
2605 static void db_limited_before(void)
2607 unmatched_stree
= clone_stree(__get_cur_stree());
2610 static void db_limited_after(void)
2612 free_stree(&unmatched_stree
);
2615 static int basically_the_same(struct range_list
*orig
, struct range_list
*new)
2617 if (type_is_ptr(rl_type(orig
)) &&
2618 is_whole_ptr_rl(orig
) &&
2619 is_whole_ptr_rl(new))
2622 return rl_equiv(orig
, new);
2625 static void db_param_limit_binops(struct expression
*arg
, char *key
, struct range_list
*rl
)
2627 struct range_list
*left_rl
;
2628 sval_t zero
= { .type
= rl_type(rl
), };
2631 if (strcmp(key
, "$") != 0)
2635 if (!get_implied_value(arg
->right
, &sval
))
2637 if (can_integer_overflow(get_type(arg
), arg
))
2640 left_rl
= rl_binop(rl
, '/', alloc_rl(sval
, sval
));
2641 if (!rl_has_sval(rl
, zero
))
2642 left_rl
= remove_range(left_rl
, zero
, zero
);
2644 set_extra_expr_nomod(arg
->left
, alloc_estate_rl(left_rl
));
2647 static void db_param_limit_filter(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
2649 struct expression
*arg
;
2652 struct var_sym_list
*vsl
= NULL
;
2653 struct sm_state
*sm
;
2654 struct symbol
*compare_type
, *var_type
;
2655 struct range_list
*rl
;
2656 struct range_list
*limit
;
2657 struct range_list
*new;
2659 struct symbol
*other_sym
;
2661 while (expr
->type
== EXPR_ASSIGNMENT
)
2662 expr
= strip_expr(expr
->right
);
2663 if (expr
->type
!= EXPR_CALL
)
2666 arg
= get_argument_from_call_expr(expr
->args
, param
);
2670 if (strcmp(key
, "$") == 0)
2671 compare_type
= get_arg_type(expr
->fn
, param
);
2673 compare_type
= get_member_type_from_key(arg
, key
);
2675 call_results_to_rl(expr
, compare_type
, value
, &limit
);
2676 if (strcmp(key
, "$") == 0)
2677 move_known_to_rl(&arg
, &limit
);
2678 name
= get_chunk_from_key(arg
, key
, &sym
, &vsl
);
2681 if (op
!= PARAM_LIMIT
&& !sym
)
2684 sm
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
2686 rl
= estate_rl(sm
->state
);
2688 rl
= alloc_whole_rl(compare_type
);
2690 if (op
== PARAM_LIMIT
&& !rl_fits_in_type(rl
, compare_type
))
2693 new = rl_intersection(rl
, limit
);
2695 var_type
= get_member_type_from_key(arg
, key
);
2696 new = cast_rl(var_type
, new);
2698 /* We want to preserve the implications here */
2699 if (sm
&& basically_the_same(rl
, new))
2701 other_name
= get_other_name_sym(name
, sym
, &other_sym
);
2703 if (op
== PARAM_LIMIT
)
2704 set_extra_nomod_vsl(name
, sym
, vsl
, NULL
, alloc_estate_rl(new));
2706 set_extra_mod(name
, sym
, NULL
, alloc_estate_rl(new));
2708 if (other_name
&& other_sym
) {
2709 if (op
== PARAM_LIMIT
)
2710 set_extra_nomod_vsl(other_name
, other_sym
, vsl
, NULL
, alloc_estate_rl(new));
2712 set_extra_mod(other_name
, other_sym
, NULL
, alloc_estate_rl(new));
2715 if (op
== PARAM_LIMIT
&& arg
->type
== EXPR_BINOP
)
2716 db_param_limit_binops(arg
, key
, new);
2721 static void db_param_limit(struct expression
*expr
, int param
, char *key
, char *value
)
2723 db_param_limit_filter(expr
, param
, key
, value
, PARAM_LIMIT
);
2726 static void db_param_filter(struct expression
*expr
, int param
, char *key
, char *value
)
2728 db_param_limit_filter(expr
, param
, key
, value
, PARAM_FILTER
);
2731 static void db_param_add_set(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
2733 struct expression
*arg
, *gen_expr
;
2735 char *other_name
= NULL
;
2736 struct symbol
*sym
, *other_sym
;
2737 struct symbol
*param_type
, *arg_type
;
2738 struct smatch_state
*state
;
2739 struct range_list
*new = NULL
;
2740 struct range_list
*added
= NULL
;
2742 while (expr
->type
== EXPR_ASSIGNMENT
)
2743 expr
= strip_expr(expr
->right
);
2744 if (expr
->type
!= EXPR_CALL
)
2747 arg
= get_argument_from_call_expr(expr
->args
, param
);
2751 arg_type
= get_arg_type_from_key(expr
->fn
, param
, arg
, key
);
2752 param_type
= get_member_type_from_key(arg
, key
);
2753 if (param_type
&& param_type
->type
== SYM_STRUCT
)
2755 name
= get_variable_from_key(arg
, key
, &sym
);
2758 gen_expr
= gen_expression_from_key(arg
, key
);
2760 state
= get_state(SMATCH_EXTRA
, name
, sym
);
2762 new = estate_rl(state
);
2764 call_results_to_rl(expr
, arg_type
, value
, &added
);
2765 added
= cast_rl(param_type
, added
);
2766 if (op
== PARAM_SET
)
2769 new = rl_union(new, added
);
2771 other_name
= get_other_name_sym_nostack(name
, sym
, &other_sym
);
2772 set_extra_mod(name
, sym
, gen_expr
, alloc_estate_rl(new));
2773 if (other_name
&& other_sym
)
2774 set_extra_mod(other_name
, other_sym
, gen_expr
, alloc_estate_rl(new));
2776 free_string(other_name
);
2780 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
2782 in_param_set
= true;
2783 db_param_add_set(expr
, param
, key
, value
, PARAM_ADD
);
2784 in_param_set
= false;
2787 static void db_param_set(struct expression
*expr
, int param
, char *key
, char *value
)
2789 in_param_set
= true;
2790 db_param_add_set(expr
, param
, key
, value
, PARAM_SET
);
2791 in_param_set
= false;
2794 static void match_lost_param(struct expression
*call
, int param
)
2796 struct expression
*arg
;
2798 if (is_const_param(call
->fn
, param
))
2801 arg
= get_argument_from_call_expr(call
->args
, param
);
2805 arg
= strip_expr(arg
);
2806 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&')
2807 set_extra_expr_mod(arg
->unop
, alloc_estate_whole(get_type(arg
->unop
)));
2809 ; /* if pointer then set struct members, maybe?*/
2812 static void db_param_value(struct expression
*expr
, int param
, char *key
, char *value
)
2814 struct expression
*call
;
2817 struct symbol
*type
;
2818 struct range_list
*rl
= NULL
;
2824 while (call
->type
== EXPR_ASSIGNMENT
)
2825 call
= strip_expr(call
->right
);
2826 if (call
->type
!= EXPR_CALL
)
2829 type
= get_member_type_from_key(expr
->left
, key
);
2830 name
= get_variable_from_key(expr
->left
, key
, &sym
);
2834 call_results_to_rl(call
, type
, value
, &rl
);
2836 set_extra_mod(name
, sym
, NULL
, alloc_estate_rl(rl
));
2841 static void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2843 struct expression
*expr
;
2844 struct range_list
*rl
= NULL
;
2845 struct smatch_state
*state
;
2846 struct symbol
*type
;
2847 char *key_orig
= key
;
2851 expr
= symbol_expression(sym
);
2852 fullname
= get_variable_from_key(expr
, key
, NULL
);
2856 type
= get_member_type_from_key(expr
, key_orig
);
2857 str_to_rl(type
, value
, &rl
);
2858 state
= alloc_estate_rl(rl
);
2859 if (estate_get_single_value(state
, &dummy
))
2860 estate_set_hard_max(state
);
2861 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
2864 static void set_param_fuzzy_max(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2866 struct expression
*expr
;
2867 struct range_list
*rl
= NULL
;
2868 struct smatch_state
*state
;
2869 struct symbol
*type
;
2873 expr
= symbol_expression(sym
);
2874 fullname
= get_variable_from_key(expr
, key
, NULL
);
2878 state
= get_state(SMATCH_EXTRA
, fullname
, sym
);
2881 type
= estate_type(state
);
2882 str_to_rl(type
, value
, &rl
);
2883 if (!rl_to_sval(rl
, &max
))
2885 estate_set_fuzzy_max(state
, max
);
2888 static void set_param_hard_max(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2890 struct smatch_state
*state
;
2891 struct expression
*expr
;
2894 expr
= symbol_expression(sym
);
2895 fullname
= get_variable_from_key(expr
, key
, NULL
);
2899 state
= get_state(SMATCH_EXTRA
, fullname
, sym
);
2902 estate_set_hard_max(state
);
2905 static struct sm_state
*get_sm_from_call(struct expression
*expr
)
2909 if (is_fake_call(expr
))
2912 snprintf(buf
, sizeof(buf
), "return %p", expr
);
2913 return get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
2916 struct sm_state
*get_extra_sm_state(struct expression
*expr
)
2920 struct sm_state
*ret
= NULL
;
2922 expr
= strip_expr(expr
);
2926 if (expr
->type
== EXPR_CALL
)
2927 return get_sm_from_call(expr
);
2929 name
= expr_to_known_chunk_sym(expr
, &sym
);
2933 ret
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
2939 struct smatch_state
*get_extra_state(struct expression
*expr
)
2941 struct sm_state
*sm
;
2943 sm
= get_extra_sm_state(expr
);
2949 void register_smatch_extra(int id
)
2953 set_dynamic_states(my_id
);
2954 add_merge_hook(my_id
, &merge_estates
);
2955 add_unmatched_state_hook(my_id
, &unmatched_state
);
2956 select_caller_info_hook(set_param_value
, PARAM_VALUE
);
2957 select_caller_info_hook(set_param_fuzzy_max
, FUZZY_MAX
);
2958 select_caller_info_hook(set_param_hard_max
, HARD_MAX
);
2959 select_return_states_before(&db_limited_before
);
2960 select_return_states_hook(PARAM_LIMIT
, &db_param_limit
);
2961 select_return_states_hook(PARAM_FILTER
, &db_param_filter
);
2962 select_return_states_hook(PARAM_ADD
, &db_param_add
);
2963 select_return_states_hook(PARAM_SET
, &db_param_set
);
2964 add_lost_param_hook(&match_lost_param
);
2965 select_return_states_hook(PARAM_VALUE
, &db_param_value
);
2966 select_return_states_after(&db_limited_after
);
2969 static void match_link_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
2971 struct var_sym_list
*links
;
2972 struct var_sym
*tmp
;
2973 struct smatch_state
*state
;
2975 links
= sm
->state
->data
;
2977 FOR_EACH_PTR(links
, tmp
) {
2978 if (sm
->sym
== tmp
->sym
&&
2979 strcmp(sm
->name
, tmp
->var
) == 0)
2981 state
= get_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
);
2984 set_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
, alloc_estate_whole(estate_type(state
)));
2985 } END_FOR_EACH_PTR(tmp
);
2986 set_state(link_id
, sm
->name
, sm
->sym
, &undefined
);
2989 void register_smatch_extra_links(int id
)
2992 set_dynamic_states(link_id
);
2995 void register_smatch_extra_late(int id
)
2997 add_merge_hook(link_id
, &merge_link_states
);
2998 add_modification_hook(link_id
, &match_link_modify
);
2999 add_hook(&match_dereferences
, DEREF_HOOK
);
3000 add_hook(&match_pointer_as_array
, OP_HOOK
);
3001 select_return_implies_hook(DEREFERENCE
, &set_param_dereferenced
);
3002 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
3003 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
3004 add_hook(&match_assign
, GLOBAL_ASSIGNMENT_HOOK
);
3005 add_hook(&unop_expr
, OP_HOOK
);
3006 add_hook(&asm_expr
, ASM_HOOK
);
3008 add_caller_info_callback(my_id
, caller_info_callback
);
3009 add_split_return_callback(&returned_struct_members
);
3011 // add_hook(&assume_indexes_are_valid, OP_HOOK);