2 * Copyright (C) 2008 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * smatch_extra.c is supposed to track the value of every variable.
34 #include "smatch_slist.h"
35 #include "smatch_extra.h"
40 static void match_link_modify(struct sm_state
*sm
, struct expression
*mod_expr
);
42 struct string_list
*__ignored_macros
= NULL
;
43 static int in_warn_on_macro(void)
45 struct statement
*stmt
;
49 stmt
= get_current_statement();
52 macro
= get_macro_name(stmt
->pos
);
56 FOR_EACH_PTR(__ignored_macros
, tmp
) {
57 if (!strcmp(tmp
, macro
))
59 } END_FOR_EACH_PTR(tmp
);
63 typedef void (mod_hook
)(const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
64 DECLARE_PTR_LIST(void_fn_list
, mod_hook
*);
65 static struct void_fn_list
*extra_mod_hooks
;
66 void add_extra_mod_hook(mod_hook
*fn
)
68 mod_hook
**p
= malloc(sizeof(mod_hook
*));
70 add_ptr_list(&extra_mod_hooks
, p
);
73 void call_extra_mod_hooks(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
77 FOR_EACH_PTR(extra_mod_hooks
, fn
) {
78 (*fn
)(name
, sym
, state
);
79 } END_FOR_EACH_PTR(fn
);
82 struct sm_state
*set_extra_mod_helper(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
84 remove_from_equiv(name
, sym
);
85 call_extra_mod_hooks(name
, sym
, state
);
86 if (__in_fake_assign
&& estate_is_unknown(state
) && !get_state(SMATCH_EXTRA
, name
, sym
))
88 return set_state(SMATCH_EXTRA
, name
, sym
, state
);
91 char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
93 struct expression
*assigned
;
94 char *orig_name
= NULL
;
101 if (!sym
|| !sym
->ident
)
104 skip
= strlen(sym
->ident
->name
);
105 if (name
[skip
] != '-' || name
[skip
+ 1] != '>')
109 assigned
= get_assigned_expr_name_sym(sym
->ident
->name
, sym
);
112 if (assigned
->type
== EXPR_PREOP
|| assigned
->op
== '&') {
114 orig_name
= expr_to_var_sym(assigned
, new_sym
);
115 if (!orig_name
|| !*new_sym
)
118 snprintf(buf
, sizeof(buf
), "%s.%s", orig_name
+ 1, name
+ skip
);
119 ret
= alloc_string(buf
);
120 free_string(orig_name
);
124 if (assigned
->type
!= EXPR_DEREF
)
127 orig_name
= expr_to_var_sym(assigned
, new_sym
);
128 if (!orig_name
|| !*new_sym
)
131 snprintf(buf
, sizeof(buf
), "%s->%s", orig_name
, name
+ skip
);
132 ret
= alloc_string(buf
);
133 free_string(orig_name
);
137 free_string(orig_name
);
141 struct sm_state
*set_extra_mod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
144 struct symbol
*new_sym
;
147 sm
= set_extra_mod_helper(name
, sym
, state
);
148 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
149 if (new_name
&& new_sym
)
150 set_extra_mod_helper(new_name
, new_sym
, state
);
151 free_string(new_name
);
155 static void clear_array_states(struct expression
*array
)
159 sm
= get_sm_state_expr(link_id
, array
);
161 match_link_modify(sm
, NULL
);
164 static struct sm_state
*set_extra_array_mod(struct expression
*expr
, struct smatch_state
*state
)
166 struct expression
*array
;
167 struct var_sym_list
*vsl
;
171 struct sm_state
*ret
= NULL
;
173 array
= get_array_base(expr
);
175 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
177 clear_array_states(array
);
181 FOR_EACH_PTR(vsl
, vs
) {
182 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
183 } END_FOR_EACH_PTR(vs
);
185 call_extra_mod_hooks(name
, sym
, state
);
186 ret
= set_state(SMATCH_EXTRA
, name
, sym
, state
);
192 struct sm_state
*set_extra_expr_mod(struct expression
*expr
, struct smatch_state
*state
)
196 struct sm_state
*ret
= NULL
;
199 return set_extra_array_mod(expr
, state
);
201 expr
= strip_expr(expr
);
202 name
= expr_to_var_sym(expr
, &sym
);
205 ret
= set_extra_mod(name
, sym
, state
);
211 void set_extra_nomod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
214 struct symbol
*new_sym
;
215 struct relation
*rel
;
216 struct smatch_state
*orig_state
;
218 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
220 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
221 if (new_name
&& new_sym
)
222 set_state(SMATCH_EXTRA
, new_name
, new_sym
, state
);
223 free_string(new_name
);
225 if (!estate_related(orig_state
)) {
226 set_state(SMATCH_EXTRA
, name
, sym
, state
);
230 set_related(state
, estate_related(orig_state
));
231 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
232 struct smatch_state
*estate
;
234 if (option_debug_related
)
235 sm_msg("%s updating related %s to %s", name
, rel
->name
, state
->name
);
236 estate
= get_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
);
239 set_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
, clone_estate_cast(estate_type(estate
), state
));
240 } END_FOR_EACH_PTR(rel
);
243 void set_extra_nomod_vsl(const char *name
, struct symbol
*sym
, struct var_sym_list
*vsl
, struct smatch_state
*state
)
247 FOR_EACH_PTR(vsl
, vs
) {
248 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
249 } END_FOR_EACH_PTR(vs
);
251 set_extra_nomod(name
, sym
, state
);
255 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
257 void set_extra_expr_nomod(struct expression
*expr
, struct smatch_state
*state
)
259 struct var_sym_list
*vsl
;
264 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
267 FOR_EACH_PTR(vsl
, vs
) {
268 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
269 } END_FOR_EACH_PTR(vs
);
271 set_extra_nomod(name
, sym
, state
);
276 static void set_extra_true_false(const char *name
, struct symbol
*sym
,
277 struct smatch_state
*true_state
,
278 struct smatch_state
*false_state
)
281 struct symbol
*new_sym
;
282 struct relation
*rel
;
283 struct smatch_state
*orig_state
;
285 if (!true_state
&& !false_state
)
288 if (in_warn_on_macro())
291 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
292 if (new_name
&& new_sym
)
293 set_true_false_states(SMATCH_EXTRA
, new_name
, new_sym
, true_state
, false_state
);
294 free_string(new_name
);
296 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
298 if (!estate_related(orig_state
)) {
299 set_true_false_states(SMATCH_EXTRA
, name
, sym
, true_state
, false_state
);
304 set_related(true_state
, estate_related(orig_state
));
306 set_related(false_state
, estate_related(orig_state
));
308 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
309 set_true_false_states(SMATCH_EXTRA
, rel
->name
, rel
->sym
,
310 true_state
, false_state
);
311 } END_FOR_EACH_PTR(rel
);
314 static void set_extra_chunk_true_false(struct expression
*expr
,
315 struct smatch_state
*true_state
,
316 struct smatch_state
*false_state
)
318 struct var_sym_list
*vsl
;
324 if (in_warn_on_macro())
327 type
= get_type(expr
);
331 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
334 FOR_EACH_PTR(vsl
, vs
) {
335 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
336 } END_FOR_EACH_PTR(vs
);
338 set_true_false_states(SMATCH_EXTRA
, name
, sym
,
339 clone_estate(true_state
),
340 clone_estate(false_state
));
345 static void set_extra_expr_true_false(struct expression
*expr
,
346 struct smatch_state
*true_state
,
347 struct smatch_state
*false_state
)
353 if (!true_state
&& !false_state
)
356 if (get_value(expr
, &sval
))
359 expr
= strip_expr(expr
);
360 name
= expr_to_var_sym(expr
, &sym
);
363 set_extra_chunk_true_false(expr
, true_state
, false_state
);
366 set_extra_true_false(name
, sym
, true_state
, false_state
);
370 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
372 struct expression
*iter_var
;
373 struct expression
*condition
;
375 struct smatch_state
*estate
;
378 condition
= strip_expr(loop
->iterator_pre_condition
);
381 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
383 if (condition
->op
!= SPECIAL_DECREMENT
)
386 iter_var
= condition
->unop
;
387 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
390 if (sval_cmp_val(estate_min(sm
->state
), 0) < 0)
392 start
= estate_max(sm
->state
);
393 if (sval_cmp_val(start
, 0) <= 0)
395 if (!sval_is_max(start
))
398 if (condition
->type
== EXPR_PREOP
) {
399 estate
= alloc_estate_range(sval_type_val(start
.type
, 1), start
);
400 if (estate_has_hard_max(sm
->state
))
401 estate_set_hard_max(estate
);
402 estate_copy_fuzzy_max(estate
, sm
->state
);
403 set_extra_expr_mod(iter_var
, estate
);
405 if (condition
->type
== EXPR_POSTOP
) {
406 estate
= alloc_estate_range(sval_type_val(start
.type
, 0), start
);
407 if (estate_has_hard_max(sm
->state
))
408 estate_set_hard_max(estate
);
409 estate_copy_fuzzy_max(estate
, sm
->state
);
410 set_extra_expr_mod(iter_var
, estate
);
412 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
415 static struct sm_state
*handle_canonical_for_inc(struct expression
*iter_expr
,
416 struct expression
*condition
)
418 struct expression
*iter_var
;
420 struct smatch_state
*estate
;
421 sval_t start
, end
, max
;
423 iter_var
= iter_expr
->unop
;
424 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
427 if (!estate_get_single_value(sm
->state
, &start
))
429 if (get_implied_max(condition
->right
, &end
))
430 end
= sval_cast(get_type(iter_var
), end
);
432 end
= sval_type_max(get_type(iter_var
));
434 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
437 switch (condition
->op
) {
438 case SPECIAL_UNSIGNED_LT
:
439 case SPECIAL_NOTEQUAL
:
441 if (!sval_is_min(end
))
444 case SPECIAL_UNSIGNED_LTE
:
450 if (sval_cmp(end
, start
) < 0)
452 estate
= alloc_estate_range(start
, end
);
453 if (get_hard_max(condition
->right
, &max
)) {
454 estate_set_hard_max(estate
);
455 if (condition
->op
== '<' ||
456 condition
->op
== SPECIAL_UNSIGNED_LT
||
457 condition
->op
== SPECIAL_NOTEQUAL
)
459 estate_set_fuzzy_max(estate
, max
);
461 set_extra_expr_mod(iter_var
, estate
);
462 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
465 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
466 struct expression
*condition
)
468 struct expression
*iter_var
;
470 struct smatch_state
*estate
;
473 iter_var
= iter_expr
->unop
;
474 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
477 if (!estate_get_single_value(sm
->state
, &start
))
479 if (!get_implied_min(condition
->right
, &end
))
480 end
= sval_type_min(get_type(iter_var
));
481 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
484 switch (condition
->op
) {
485 case SPECIAL_NOTEQUAL
:
487 if (!sval_is_min(end
) && !sval_is_max(end
))
495 if (sval_cmp(end
, start
) > 0)
497 estate
= alloc_estate_range(end
, start
);
498 estate_set_hard_max(estate
);
499 estate_set_fuzzy_max(estate
, estate_get_fuzzy_max(estate
));
500 set_extra_expr_mod(iter_var
, estate
);
501 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
504 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
506 struct expression
*iter_expr
;
507 struct expression
*condition
;
509 if (!loop
->iterator_post_statement
)
511 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
513 iter_expr
= loop
->iterator_post_statement
->expression
;
514 if (!loop
->iterator_pre_condition
)
516 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
518 condition
= loop
->iterator_pre_condition
;
520 if (iter_expr
->op
== SPECIAL_INCREMENT
)
521 return handle_canonical_for_inc(iter_expr
, condition
);
522 if (iter_expr
->op
== SPECIAL_DECREMENT
)
523 return handle_canonical_for_dec(iter_expr
, condition
);
527 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct stree
**stree
)
529 struct sm_state
*ret
;
531 __push_fake_cur_stree();
532 if (!loop
->iterator_post_statement
)
533 ret
= handle_canonical_while_count_down(loop
);
535 ret
= handle_canonical_for_loops(loop
);
536 *stree
= __pop_fake_cur_stree();
540 int __iterator_unchanged(struct sm_state
*sm
)
544 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
549 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
553 /* paranoid checking. prolly not needed */
554 condition
= strip_expr(condition
);
557 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
559 if (condition
->op
!= SPECIAL_DECREMENT
)
561 after_value
= estate_min(sm
->state
);
563 set_extra_mod(sm
->name
, sm
->sym
, alloc_estate_sval(after_value
));
566 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
567 struct statement
*iterator
,
568 struct expression
*condition
)
570 struct expression
*iter_expr
;
572 struct smatch_state
*state
;
575 while_count_down_after(sm
, condition
);
579 iter_expr
= iterator
->expression
;
581 if (condition
->type
!= EXPR_COMPARE
)
583 if (iter_expr
->op
== SPECIAL_INCREMENT
) {
584 limit
= sval_binop(estate_max(sm
->state
), '+',
585 sval_type_val(estate_type(sm
->state
), 1));
587 limit
= sval_binop(estate_min(sm
->state
), '-',
588 sval_type_val(estate_type(sm
->state
), 1));
590 if (!estate_has_hard_max(sm
->state
) && !__has_breaks()) {
591 if (iter_expr
->op
== SPECIAL_INCREMENT
)
592 state
= alloc_estate_range(estate_min(sm
->state
), limit
);
594 state
= alloc_estate_range(limit
, estate_max(sm
->state
));
596 state
= alloc_estate_sval(limit
);
598 if (!estate_has_hard_max(sm
->state
)) {
599 estate_clear_hard_max(state
);
601 if (estate_has_fuzzy_max(sm
->state
)) {
602 sval_t hmax
= estate_get_fuzzy_max(sm
->state
);
603 sval_t max
= estate_max(sm
->state
);
605 if (sval_cmp(hmax
, max
) != 0)
606 estate_clear_fuzzy_max(state
);
607 } else if (!estate_has_fuzzy_max(sm
->state
)) {
608 estate_clear_fuzzy_max(state
);
611 set_extra_mod(sm
->name
, sm
->sym
, state
);
614 static struct stree
*unmatched_stree
;
615 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
617 struct smatch_state
*state
;
619 if (unmatched_stree
) {
620 state
= get_state_stree(unmatched_stree
, SMATCH_EXTRA
, sm
->name
, sm
->sym
);
624 if (parent_is_gone_var_sym(sm
->name
, sm
->sym
))
625 return alloc_estate_empty();
626 return alloc_estate_whole(estate_type(sm
->state
));
629 static void clear_the_pointed_at(struct expression
*expr
)
634 struct sm_state
*tmp
;
636 name
= expr_to_var_sym(expr
, &sym
);
640 stree
= __get_cur_stree();
641 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
642 if (tmp
->name
[0] != '*')
646 if (strcmp(tmp
->name
+ 1, name
) != 0)
648 set_extra_mod(tmp
->name
, tmp
->sym
, alloc_estate_whole(estate_type(tmp
->state
)));
649 } END_FOR_EACH_SM(tmp
);
655 static void match_function_call(struct expression
*expr
)
657 struct expression
*arg
;
658 struct expression
*tmp
;
660 /* if we have the db this is handled in smatch_function_hooks.c */
663 if (inlinable(expr
->fn
))
666 FOR_EACH_PTR(expr
->args
, arg
) {
667 tmp
= strip_expr(arg
);
668 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
669 set_extra_expr_mod(tmp
->unop
, alloc_estate_whole(get_type(tmp
->unop
)));
671 clear_the_pointed_at(tmp
);
672 } END_FOR_EACH_PTR(arg
);
675 static int values_fit_type(struct expression
*left
, struct expression
*right
)
677 struct range_list
*rl
;
680 type
= get_type(left
);
683 get_absolute_rl(right
, &rl
);
684 if (type_unsigned(type
) && sval_is_negative(rl_min(rl
)))
686 if (sval_cmp(sval_type_min(type
), rl_min(rl
)) > 0)
688 if (sval_cmp(sval_type_max(type
), rl_max(rl
)) < 0)
693 static void save_chunk_info(struct expression
*left
, struct expression
*right
)
695 struct var_sym_list
*vsl
;
697 struct expression
*add_expr
;
703 if (right
->type
!= EXPR_BINOP
|| right
->op
!= '-')
705 if (!get_value(right
->left
, &sval
))
707 if (!expr_to_sym(right
->right
))
710 add_expr
= binop_expression(left
, '+', right
->right
);
711 type
= get_type(add_expr
);
714 name
= expr_to_chunk_sym_vsl(add_expr
, &sym
, &vsl
);
717 FOR_EACH_PTR(vsl
, vs
) {
718 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
719 } END_FOR_EACH_PTR(vs
);
721 set_state(SMATCH_EXTRA
, name
, sym
, alloc_estate_sval(sval_cast(type
, sval
)));
726 static void do_array_assign(struct expression
*left
, int op
, struct expression
*right
)
728 struct range_list
*rl
;
731 get_absolute_rl(right
, &rl
);
732 rl
= cast_rl(get_type(left
), rl
);
734 rl
= alloc_whole_rl(get_type(left
));
737 set_extra_array_mod(left
, alloc_estate_rl(rl
));
740 static void match_untracked_array(struct expression
*call
, int param
)
742 struct expression
*arg
;
744 arg
= get_argument_from_call_expr(call
->args
, param
);
745 arg
= strip_expr(arg
);
747 clear_array_states(arg
);
750 static void match_vanilla_assign(struct expression
*left
, struct expression
*right
)
752 struct range_list
*orig_rl
= NULL
;
753 struct range_list
*rl
= NULL
;
754 struct symbol
*right_sym
;
755 struct symbol
*left_type
;
756 struct symbol
*right_type
;
757 char *right_name
= NULL
;
761 struct smatch_state
*state
;
767 save_chunk_info(left
, right
);
769 name
= expr_to_var_sym(left
, &sym
);
772 do_array_assign(left
, '=', right
);
776 left_type
= get_type(left
);
777 right_type
= get_type(right
);
779 right_name
= expr_to_var_sym(right
, &right_sym
);
781 if (!__in_fake_assign
&&
782 !(right
->type
== EXPR_PREOP
&& right
->op
== '&') &&
783 right_name
&& right_sym
&&
784 values_fit_type(left
, right
) &&
785 !has_symbol(right
, sym
)) {
786 set_equiv(left
, right
);
790 if (is_pointer(right
) && get_address_rl(right
, &rl
)) {
791 state
= alloc_estate_rl(rl
);
795 if (__in_fake_assign
) {
796 struct smatch_state
*right_state
;
799 if (get_value(right
, &sval
)) {
800 sval
= sval_cast(left_type
, sval
);
801 state
= alloc_estate_sval(sval
);
805 right_state
= get_state(SMATCH_EXTRA
, right_name
, right_sym
);
807 /* simple assignment */
808 state
= clone_estate(right_state
);
812 state
= alloc_estate_rl(alloc_whole_rl(left_type
));
816 comparison
= get_comparison(left
, right
);
818 comparison
= flip_comparison(comparison
);
819 get_implied_rl(left
, &orig_rl
);
822 if (get_implied_rl(right
, &rl
)) {
823 rl
= cast_rl(left_type
, rl
);
825 filter_by_comparison(&rl
, comparison
, orig_rl
);
826 state
= alloc_estate_rl(rl
);
827 if (get_hard_max(right
, &max
)) {
828 estate_set_hard_max(state
);
829 estate_set_fuzzy_max(state
, max
);
832 rl
= alloc_whole_rl(right_type
);
833 rl
= cast_rl(left_type
, rl
);
835 filter_by_comparison(&rl
, comparison
, orig_rl
);
836 state
= alloc_estate_rl(rl
);
840 set_extra_mod(name
, sym
, state
);
842 free_string(right_name
);
845 static int op_remove_assign(int op
)
848 case SPECIAL_ADD_ASSIGN
:
850 case SPECIAL_SUB_ASSIGN
:
852 case SPECIAL_MUL_ASSIGN
:
854 case SPECIAL_DIV_ASSIGN
:
856 case SPECIAL_MOD_ASSIGN
:
858 case SPECIAL_AND_ASSIGN
:
860 case SPECIAL_OR_ASSIGN
:
862 case SPECIAL_XOR_ASSIGN
:
864 case SPECIAL_SHL_ASSIGN
:
865 return SPECIAL_LEFTSHIFT
;
866 case SPECIAL_SHR_ASSIGN
:
867 return SPECIAL_RIGHTSHIFT
;
873 static void match_assign(struct expression
*expr
)
875 struct range_list
*rl
= NULL
;
876 struct expression
*left
;
877 struct expression
*right
;
878 struct expression
*binop_expr
;
879 struct symbol
*left_type
;
880 struct symbol
*right_type
;
883 sval_t left_min
, left_max
;
884 sval_t right_min
, right_max
;
885 sval_t res_min
, res_max
;
887 left
= strip_expr(expr
->left
);
889 right
= strip_parens(expr
->right
);
890 if (right
->type
== EXPR_CALL
&& sym_name_is("__builtin_expect", right
->fn
))
891 right
= get_argument_from_call_expr(right
->args
, 0);
892 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
893 right
= strip_parens(right
->left
);
895 if (expr
->op
== '=' && is_condition(expr
->right
))
896 return; /* handled in smatch_condition.c */
897 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
898 return; /* handled in smatch_function_hooks.c */
899 if (expr
->op
== '=') {
900 match_vanilla_assign(left
, right
);
904 name
= expr_to_var_sym(left
, &sym
);
908 left_type
= get_type(left
);
909 right_type
= get_type(right
);
911 res_min
= sval_type_min(left_type
);
912 res_max
= sval_type_max(left_type
);
915 case SPECIAL_ADD_ASSIGN
:
916 get_absolute_max(left
, &left_max
);
917 get_absolute_max(right
, &right_max
);
918 if (sval_binop_overflows(left_max
, '+', right_max
))
920 if (get_implied_min(left
, &left_min
) &&
921 !sval_is_negative_min(left_min
) &&
922 get_implied_min(right
, &right_min
) &&
923 !sval_is_negative_min(right_min
)) {
924 res_min
= sval_binop(left_min
, '+', right_min
);
925 res_min
= sval_cast(right_type
, res_min
);
927 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
929 res_max
= sval_binop(left_max
, '+', right_max
);
930 res_max
= sval_cast(right_type
, res_max
);
932 case SPECIAL_SUB_ASSIGN
:
933 if (get_implied_max(left
, &left_max
) &&
934 !sval_is_max(left_max
) &&
935 get_implied_min(right
, &right_min
) &&
936 !sval_is_min(right_min
)) {
937 res_max
= sval_binop(left_max
, '-', right_min
);
938 res_max
= sval_cast(right_type
, res_max
);
942 if (get_implied_min(left
, &left_min
) &&
943 !sval_is_min(left_min
) &&
944 get_implied_max(right
, &right_max
) &&
945 !sval_is_max(right_max
)) {
946 res_min
= sval_binop(left_min
, '-', right_max
);
947 res_min
= sval_cast(right_type
, res_min
);
950 case SPECIAL_AND_ASSIGN
:
951 case SPECIAL_MOD_ASSIGN
:
952 case SPECIAL_SHL_ASSIGN
:
953 case SPECIAL_SHR_ASSIGN
:
954 case SPECIAL_OR_ASSIGN
:
955 case SPECIAL_XOR_ASSIGN
:
956 case SPECIAL_MUL_ASSIGN
:
957 case SPECIAL_DIV_ASSIGN
:
958 binop_expr
= binop_expression(expr
->left
,
959 op_remove_assign(expr
->op
),
961 if (get_absolute_rl(binop_expr
, &rl
)) {
962 rl
= cast_rl(left_type
, rl
);
963 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
968 rl
= cast_rl(left_type
, alloc_rl(res_min
, res_max
));
969 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
974 static struct smatch_state
*increment_state(struct smatch_state
*state
)
976 sval_t min
= estate_min(state
);
977 sval_t max
= estate_max(state
);
979 if (!estate_rl(state
))
983 max
= sval_type_max(max
.type
);
985 if (!sval_is_min(min
) && !sval_is_max(min
))
987 if (!sval_is_min(max
) && !sval_is_max(max
))
989 return alloc_estate_range(min
, max
);
992 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
994 sval_t min
= estate_min(state
);
995 sval_t max
= estate_max(state
);
997 if (!estate_rl(state
))
1001 min
= sval_type_min(min
.type
);
1003 if (!sval_is_min(min
) && !sval_is_max(min
))
1005 if (!sval_is_min(max
) && !sval_is_max(max
))
1007 return alloc_estate_range(min
, max
);
1010 static void clear_pointed_at_state(struct expression
*expr
)
1012 struct symbol
*type
;
1015 * ALERT: This is sort of a mess. If it's is a struct assigment like
1016 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1017 * the same thing for p++ where "p" is a struct. Most modifications
1018 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1019 * use smatch_modification.c because we have to get the ordering right
1020 * or something. So if you have p++ where p is a pointer to a standard
1021 * c type then we handle that here. What a mess.
1024 type
= get_type(expr
);
1025 if (!type
|| type
->type
!= SYM_PTR
)
1027 type
= get_real_base_type(type
);
1028 if (!type
|| type
->type
!= SYM_BASETYPE
)
1030 set_extra_expr_mod(deref_expression(expr
), alloc_estate_whole(type
));
1033 static void unop_expr(struct expression
*expr
)
1035 struct smatch_state
*state
;
1037 if (expr
->smatch_flags
& Handled
)
1041 case SPECIAL_INCREMENT
:
1042 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
1043 state
= increment_state(state
);
1045 state
= alloc_estate_whole(get_type(expr
));
1046 set_extra_expr_mod(expr
->unop
, state
);
1047 clear_pointed_at_state(expr
->unop
);
1049 case SPECIAL_DECREMENT
:
1050 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
1051 state
= decrement_state(state
);
1053 state
= alloc_estate_whole(get_type(expr
));
1054 set_extra_expr_mod(expr
->unop
, state
);
1055 clear_pointed_at_state(expr
->unop
);
1062 static void asm_expr(struct statement
*stmt
)
1065 struct expression
*expr
;
1066 struct symbol
*type
;
1069 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
1071 case 0: /* identifier */
1072 case 1: /* constraint */
1075 case 2: /* expression */
1077 type
= get_type(strip_expr(expr
));
1078 set_extra_expr_mod(expr
, alloc_estate_whole(type
));
1081 } END_FOR_EACH_PTR(expr
);
1084 static void check_dereference(struct expression
*expr
)
1086 if (__in_fake_assign
)
1088 if (outside_of_function())
1090 if (implied_not_equal(expr
, 0))
1092 set_extra_expr_nomod(expr
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
1095 static void match_dereferences(struct expression
*expr
)
1097 if (expr
->type
!= EXPR_PREOP
)
1099 /* it's saying that foo[1] = bar dereferences foo[1] */
1102 check_dereference(expr
->unop
);
1105 static void match_pointer_as_array(struct expression
*expr
)
1107 if (!is_array(expr
))
1109 check_dereference(get_array_base(expr
));
1112 static void find_dereferences(struct expression
*expr
)
1114 while (expr
->type
== EXPR_PREOP
) {
1115 if (expr
->op
== '*')
1116 check_dereference(expr
->unop
);
1117 expr
= strip_expr(expr
->unop
);
1121 static void set_param_dereferenced(struct expression
*arg
, char *key
, char *unused
)
1126 name
= get_variable_from_key(arg
, key
, &sym
);
1128 set_extra_nomod(name
, sym
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
1131 find_dereferences(arg
);
1134 static sval_t
add_one(sval_t sval
)
1140 static int handle_postop_inc(struct expression
*left
, int op
, struct expression
*right
)
1142 struct statement
*stmt
;
1143 struct expression
*cond
;
1144 struct smatch_state
*true_state
, *false_state
;
1149 * If we're decrementing here then that's a canonical while count down
1150 * so it's handled already. We're only handling loops like:
1152 * do { ... } while (i++ < 3);
1155 if (left
->type
!= EXPR_POSTOP
|| left
->op
!= SPECIAL_INCREMENT
)
1158 stmt
= __cur_stmt
->parent
;
1161 if (stmt
->type
== STMT_COMPOUND
)
1162 stmt
= stmt
->parent
;
1163 if (!stmt
|| stmt
->type
!= STMT_ITERATOR
|| !stmt
->iterator_post_condition
)
1166 cond
= strip_expr(stmt
->iterator_post_condition
);
1167 if (cond
->type
!= EXPR_COMPARE
|| cond
->op
!= op
)
1169 if (left
!= strip_expr(cond
->left
) || right
!= strip_expr(cond
->right
))
1172 if (!get_implied_value(left
->unop
, &start
))
1174 if (!get_implied_value(right
, &limit
))
1177 if (sval_cmp(start
, limit
) > 0)
1182 case SPECIAL_UNSIGNED_LT
:
1185 case SPECIAL_UNSIGNED_LTE
:
1186 limit
= add_one(limit
);
1192 true_state
= alloc_estate_range(add_one(start
), limit
);
1193 false_state
= alloc_estate_range(add_one(limit
), add_one(limit
));
1195 /* Currently we just discard the false state but when two passes is
1196 * implimented correctly then it will use it.
1199 set_extra_expr_true_false(left
->unop
, true_state
, false_state
);
1204 static void handle_comparison(struct symbol
*type
, struct expression
*left
, int op
, struct expression
*right
)
1206 struct range_list
*left_orig
;
1207 struct range_list
*left_true
;
1208 struct range_list
*left_false
;
1209 struct range_list
*right_orig
;
1210 struct range_list
*right_true
;
1211 struct range_list
*right_false
;
1212 struct smatch_state
*left_true_state
;
1213 struct smatch_state
*left_false_state
;
1214 struct smatch_state
*right_true_state
;
1215 struct smatch_state
*right_false_state
;
1216 sval_t dummy
, hard_max
;
1217 int left_postop
= 0;
1218 int right_postop
= 0;
1220 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
1221 if (left
->type
== EXPR_POSTOP
) {
1222 left
->smatch_flags
|= Handled
;
1223 left_postop
= left
->op
;
1224 if (handle_postop_inc(left
, op
, right
))
1227 left
= strip_parens(left
->unop
);
1229 while (left
->type
== EXPR_ASSIGNMENT
)
1230 left
= strip_parens(left
->left
);
1232 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
1233 if (right
->type
== EXPR_POSTOP
) {
1234 right
->smatch_flags
|= Handled
;
1235 right_postop
= right
->op
;
1237 right
= strip_parens(right
->unop
);
1240 get_real_absolute_rl(left
, &left_orig
);
1241 left_orig
= cast_rl(type
, left_orig
);
1243 get_real_absolute_rl(right
, &right_orig
);
1244 right_orig
= cast_rl(type
, right_orig
);
1246 split_comparison_rl(left_orig
, op
, right_orig
, &left_true
, &left_false
, &right_true
, &right_false
);
1248 left_true
= rl_truncate_cast(get_type(strip_expr(left
)), left_true
);
1249 left_false
= rl_truncate_cast(get_type(strip_expr(left
)), left_false
);
1250 right_true
= rl_truncate_cast(get_type(strip_expr(right
)), right_true
);
1251 right_false
= rl_truncate_cast(get_type(strip_expr(right
)), right_false
);
1253 if (!left_true
|| !left_false
) {
1254 struct range_list
*tmp_true
, *tmp_false
;
1256 split_comparison_rl(alloc_whole_rl(type
), op
, right_orig
, &tmp_true
, &tmp_false
, NULL
, NULL
);
1257 tmp_true
= rl_truncate_cast(get_type(strip_expr(left
)), tmp_true
);
1258 tmp_false
= rl_truncate_cast(get_type(strip_expr(left
)), tmp_false
);
1259 if (tmp_true
&& tmp_false
)
1260 __save_imaginary_state(left
, tmp_true
, tmp_false
);
1263 if (!right_true
|| !right_false
) {
1264 struct range_list
*tmp_true
, *tmp_false
;
1266 split_comparison_rl(alloc_whole_rl(type
), op
, right_orig
, NULL
, NULL
, &tmp_true
, &tmp_false
);
1267 tmp_true
= rl_truncate_cast(get_type(strip_expr(right
)), tmp_true
);
1268 tmp_false
= rl_truncate_cast(get_type(strip_expr(right
)), tmp_false
);
1269 if (tmp_true
&& tmp_false
)
1270 __save_imaginary_state(right
, tmp_true
, tmp_false
);
1273 left_true_state
= alloc_estate_rl(left_true
);
1274 left_false_state
= alloc_estate_rl(left_false
);
1275 right_true_state
= alloc_estate_rl(right_true
);
1276 right_false_state
= alloc_estate_rl(right_false
);
1280 case SPECIAL_UNSIGNED_LT
:
1281 case SPECIAL_UNSIGNED_LTE
:
1283 if (get_hard_max(right
, &dummy
))
1284 estate_set_hard_max(left_true_state
);
1285 if (get_hard_max(left
, &dummy
))
1286 estate_set_hard_max(right_false_state
);
1289 case SPECIAL_UNSIGNED_GT
:
1290 case SPECIAL_UNSIGNED_GTE
:
1292 if (get_hard_max(left
, &dummy
))
1293 estate_set_hard_max(right_true_state
);
1294 if (get_hard_max(right
, &dummy
))
1295 estate_set_hard_max(left_false_state
);
1301 case SPECIAL_UNSIGNED_LT
:
1302 case SPECIAL_UNSIGNED_LTE
:
1304 if (get_hard_max(right
, &hard_max
)) {
1305 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1307 estate_set_fuzzy_max(left_true_state
, hard_max
);
1309 if (get_implied_value(right
, &hard_max
)) {
1310 if (op
== SPECIAL_UNSIGNED_LTE
||
1313 estate_set_fuzzy_max(left_false_state
, hard_max
);
1315 if (get_hard_max(left
, &hard_max
)) {
1316 if (op
== SPECIAL_UNSIGNED_LTE
||
1319 estate_set_fuzzy_max(right_false_state
, hard_max
);
1321 if (get_implied_value(left
, &hard_max
)) {
1322 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1324 estate_set_fuzzy_max(right_true_state
, hard_max
);
1328 case SPECIAL_UNSIGNED_GT
:
1329 case SPECIAL_UNSIGNED_GTE
:
1331 if (get_hard_max(left
, &hard_max
)) {
1332 if (op
== '>' || op
== SPECIAL_UNSIGNED_GT
)
1334 estate_set_fuzzy_max(right_true_state
, hard_max
);
1336 if (get_implied_value(left
, &hard_max
)) {
1337 if (op
== SPECIAL_UNSIGNED_GTE
||
1340 estate_set_fuzzy_max(right_false_state
, hard_max
);
1342 if (get_hard_max(right
, &hard_max
)) {
1343 if (op
== SPECIAL_UNSIGNED_LTE
||
1346 estate_set_fuzzy_max(left_false_state
, hard_max
);
1348 if (get_implied_value(right
, &hard_max
)) {
1350 op
== SPECIAL_UNSIGNED_GT
)
1352 estate_set_fuzzy_max(left_true_state
, hard_max
);
1356 if (get_hard_max(left
, &hard_max
))
1357 estate_set_fuzzy_max(right_true_state
, hard_max
);
1358 if (get_hard_max(right
, &hard_max
))
1359 estate_set_fuzzy_max(left_true_state
, hard_max
);
1363 if (get_hard_max(left
, &hard_max
)) {
1364 estate_set_hard_max(left_true_state
);
1365 estate_set_hard_max(left_false_state
);
1367 if (get_hard_max(right
, &hard_max
)) {
1368 estate_set_hard_max(right_true_state
);
1369 estate_set_hard_max(right_false_state
);
1372 if (left_postop
== SPECIAL_INCREMENT
) {
1373 left_true_state
= increment_state(left_true_state
);
1374 left_false_state
= increment_state(left_false_state
);
1376 if (left_postop
== SPECIAL_DECREMENT
) {
1377 left_true_state
= decrement_state(left_true_state
);
1378 left_false_state
= decrement_state(left_false_state
);
1380 if (right_postop
== SPECIAL_INCREMENT
) {
1381 right_true_state
= increment_state(right_true_state
);
1382 right_false_state
= increment_state(right_false_state
);
1384 if (right_postop
== SPECIAL_DECREMENT
) {
1385 right_true_state
= decrement_state(right_true_state
);
1386 right_false_state
= decrement_state(right_false_state
);
1389 if (estate_rl(left_true_state
) && estates_equiv(left_true_state
, left_false_state
)) {
1390 left_true_state
= NULL
;
1391 left_false_state
= NULL
;
1394 if (estate_rl(right_true_state
) && estates_equiv(right_true_state
, right_false_state
)) {
1395 right_true_state
= NULL
;
1396 right_false_state
= NULL
;
1399 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
1400 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
1403 static int is_simple_math(struct expression
*expr
)
1407 if (expr
->type
!= EXPR_BINOP
)
1418 static void move_known_values(struct expression
**left_p
, struct expression
**right_p
)
1420 struct expression
*left
= *left_p
;
1421 struct expression
*right
= *right_p
;
1424 if (get_implied_value(left
, &sval
)) {
1425 if (!is_simple_math(right
))
1427 if (right
->op
== '*') {
1430 if (!get_value(right
->right
, &divisor
))
1432 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1434 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1435 *right_p
= right
->left
;
1438 if (right
->op
== '+' && get_value(right
->left
, &sval
)) {
1439 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->left
);
1440 *right_p
= right
->right
;
1443 if (get_value(right
->right
, &sval
)) {
1444 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1445 *right_p
= right
->left
;
1450 if (get_implied_value(right
, &sval
)) {
1451 if (!is_simple_math(left
))
1453 if (left
->op
== '*') {
1456 if (!get_value(left
->right
, &divisor
))
1458 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1460 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1461 *left_p
= left
->left
;
1464 if (left
->op
== '+' && get_value(left
->left
, &sval
)) {
1465 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->left
);
1466 *left_p
= left
->right
;
1470 if (get_value(left
->right
, &sval
)) {
1471 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1472 *left_p
= left
->left
;
1479 static int match_func_comparison(struct expression
*expr
)
1481 struct expression
*left
= strip_expr(expr
->left
);
1482 struct expression
*right
= strip_expr(expr
->right
);
1486 * fixme: think about this harder. We should always be trying to limit
1487 * the non-call side as well. If we can't determine the limitter does
1488 * that mean we aren't querying the database and are missing important
1492 if (left
->type
== EXPR_CALL
) {
1493 if (get_implied_value(left
, &sval
)) {
1494 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1497 function_comparison(left
, expr
->op
, right
);
1501 if (right
->type
== EXPR_CALL
) {
1502 if (get_implied_value(right
, &sval
)) {
1503 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1506 function_comparison(left
, expr
->op
, right
);
1513 static void match_comparison(struct expression
*expr
)
1515 struct expression
*left_orig
= strip_parens(expr
->left
);
1516 struct expression
*right_orig
= strip_parens(expr
->right
);
1517 struct expression
*left
, *right
;
1518 struct expression
*prev
;
1519 struct symbol
*type
;
1521 if (match_func_comparison(expr
))
1524 type
= get_type(expr
);
1526 type
= &llong_ctype
;
1530 move_known_values(&left
, &right
);
1531 handle_comparison(type
, left
, expr
->op
, right
);
1533 prev
= get_assigned_expr(left_orig
);
1534 if (is_simple_math(prev
) && has_variable(prev
, left_orig
) == 0) {
1537 move_known_values(&left
, &right
);
1538 handle_comparison(type
, left
, expr
->op
, right
);
1541 prev
= get_assigned_expr(right_orig
);
1542 if (is_simple_math(prev
) && has_variable(prev
, right_orig
) == 0) {
1545 move_known_values(&left
, &right
);
1546 handle_comparison(type
, left
, expr
->op
, right
);
1550 static sval_t
get_high_mask(sval_t known
)
1558 for (i
= type_bits(known
.type
) - 1; i
>= 0; i
--) {
1559 if (known
.uvalue
& (1ULL << i
))
1560 ret
.uvalue
|= (1ULL << i
);
1568 static void handle_AND_condition(struct expression
*expr
)
1570 struct range_list
*orig_rl
;
1571 struct range_list
*true_rl
= NULL
;
1572 struct range_list
*false_rl
= NULL
;
1576 if (get_implied_value(expr
->left
, &known
)) {
1577 sval_t low_mask
= known
;
1580 if (known
.value
> 0) {
1581 bit
= ffsll(known
.value
) - 1;
1582 low_mask
.uvalue
= (1ULL << bit
) - 1;
1583 get_absolute_rl(expr
->right
, &orig_rl
);
1584 true_rl
= remove_range(orig_rl
, sval_type_val(known
.type
, 0), low_mask
);
1586 high_mask
= get_high_mask(known
);
1587 if (high_mask
.value
) {
1588 bit
= ffsll(high_mask
.value
) - 1;
1589 low_mask
.uvalue
= (1ULL << bit
) - 1;
1591 get_absolute_rl(expr
->left
, &orig_rl
);
1592 if (sval_is_negative(rl_min(orig_rl
)))
1593 orig_rl
= remove_range(orig_rl
, sval_type_min(known
.type
), sval_type_val(known
.type
, -1));
1594 false_rl
= remove_range(orig_rl
, low_mask
, sval_type_max(known
.type
));
1595 if (type_signed(high_mask
.type
) && type_unsigned(rl_type(false_rl
))) {
1596 false_rl
= remove_range(false_rl
,
1597 sval_type_val(rl_type(false_rl
), sval_type_max(known
.type
).uvalue
),
1598 sval_type_val(rl_type(false_rl
), -1));
1601 set_extra_expr_true_false(expr
->right
,
1602 true_rl
? alloc_estate_rl(true_rl
) : NULL
,
1603 false_rl
? alloc_estate_rl(false_rl
) : NULL
);
1608 if (get_implied_value(expr
->right
, &known
)) {
1609 sval_t low_mask
= known
;
1612 if (known
.value
> 0) {
1613 bit
= ffsll(known
.value
) - 1;
1614 low_mask
.uvalue
= (1ULL << bit
) - 1;
1615 get_absolute_rl(expr
->left
, &orig_rl
);
1616 true_rl
= remove_range(orig_rl
, sval_type_val(known
.type
, 0), low_mask
);
1618 high_mask
= get_high_mask(known
);
1619 if (high_mask
.value
) {
1620 bit
= ffsll(high_mask
.value
) - 1;
1621 low_mask
.uvalue
= (1ULL << bit
) - 1;
1623 get_absolute_rl(expr
->left
, &orig_rl
);
1624 if (sval_is_negative(rl_min(orig_rl
)))
1625 orig_rl
= remove_range(orig_rl
, sval_type_min(known
.type
), sval_type_val(known
.type
, -1));
1626 false_rl
= remove_range(orig_rl
, low_mask
, sval_type_max(known
.type
));
1627 if (type_signed(high_mask
.type
) && type_unsigned(rl_type(false_rl
))) {
1628 false_rl
= remove_range(false_rl
,
1629 sval_type_val(rl_type(false_rl
), sval_type_max(known
.type
).uvalue
),
1630 sval_type_val(rl_type(false_rl
), -1));
1633 set_extra_expr_true_false(expr
->left
,
1634 true_rl
? alloc_estate_rl(true_rl
) : NULL
,
1635 false_rl
? alloc_estate_rl(false_rl
) : NULL
);
1640 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1641 void __extra_match_condition(struct expression
*expr
)
1643 struct smatch_state
*pre_state
;
1644 struct smatch_state
*true_state
;
1645 struct smatch_state
*false_state
;
1647 expr
= strip_expr(expr
);
1648 switch (expr
->type
) {
1650 function_comparison(expr
, SPECIAL_NOTEQUAL
, zero_expr());
1657 zero
= sval_blank(expr
);
1660 pre_state
= get_extra_state(expr
);
1661 true_state
= estate_filter_sval(pre_state
, zero
);
1662 if (possibly_true(expr
, SPECIAL_EQUAL
, zero_expr()))
1663 false_state
= alloc_estate_sval(zero
);
1665 false_state
= alloc_estate_empty();
1666 set_extra_expr_true_false(expr
, true_state
, false_state
);
1670 match_comparison(expr
);
1672 case EXPR_ASSIGNMENT
:
1673 __extra_match_condition(expr
->left
);
1676 if (expr
->op
== '&')
1677 handle_AND_condition(expr
);
1682 static void assume_indexes_are_valid(struct expression
*expr
)
1684 struct expression
*array_expr
;
1686 struct expression
*offset
;
1687 struct symbol
*offset_type
;
1688 struct range_list
*rl_before
;
1689 struct range_list
*rl_after
;
1690 struct range_list
*filter
= NULL
;
1693 expr
= strip_expr(expr
);
1694 if (!is_array(expr
))
1697 offset
= get_array_offset(expr
);
1698 offset_type
= get_type(offset
);
1699 if (offset_type
&& type_signed(offset_type
)) {
1700 filter
= alloc_rl(sval_type_min(offset_type
),
1701 sval_type_val(offset_type
, -1));
1704 array_expr
= get_array_base(expr
);
1705 array_size
= get_real_array_size(array_expr
);
1706 if (array_size
> 1) {
1707 size
= sval_type_val(offset_type
, array_size
);
1708 add_range(&filter
, size
, sval_type_max(offset_type
));
1713 get_absolute_rl(offset
, &rl_before
);
1714 rl_after
= rl_filter(rl_before
, filter
);
1715 if (rl_equiv(rl_before
, rl_after
))
1717 set_extra_expr_nomod(offset
, alloc_estate_rl(rl_after
));
1720 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1721 int implied_not_equal(struct expression
*expr
, long long val
)
1723 return !possibly_false(expr
, SPECIAL_NOTEQUAL
, value_expr(val
));
1726 int implied_not_equal_name_sym(char *name
, struct symbol
*sym
, long long val
)
1728 struct smatch_state
*estate
;
1730 estate
= get_state(SMATCH_EXTRA
, name
, sym
);
1733 if (!rl_has_sval(estate_rl(estate
), sval_type_val(estate_type(estate
), 0)))
1738 int parent_is_null_var_sym(const char *name
, struct symbol
*sym
)
1743 struct smatch_state
*state
;
1745 strncpy(buf
, name
, sizeof(buf
) - 1);
1746 buf
[sizeof(buf
) - 1] = '\0';
1749 while (*start
== '*') {
1751 state
= get_state(SMATCH_EXTRA
, start
, sym
);
1754 if (!estate_rl(state
))
1756 if (estate_min(state
).value
== 0 &&
1757 estate_max(state
).value
== 0)
1762 while (*start
== '&')
1765 while ((end
= strrchr(start
, '-'))) {
1767 state
= get_state(SMATCH_EXTRA
, start
, sym
);
1770 if (estate_min(state
).value
== 0 &&
1771 estate_max(state
).value
== 0)
1777 int parent_is_null(struct expression
*expr
)
1783 expr
= strip_expr(expr
);
1784 var
= expr_to_var_sym(expr
, &sym
);
1787 ret
= parent_is_null_var_sym(var
, sym
);
1793 static int param_used_callback(void *found
, int argc
, char **argv
, char **azColName
)
1799 static int filter_unused_kzalloc_info(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
1804 /* for function pointers assume everything is used */
1805 if (call
->fn
->type
!= EXPR_SYMBOL
)
1809 * kzalloc() information is treated as special because so there is just
1810 * a lot of stuff initialized to zero and it makes building the database
1811 * take hours and hours.
1813 * In theory, we should just remove this line and not pass any unused
1814 * information, but I'm not sure enough that this code works so I want
1815 * to hold off on that for now.
1817 if (!estate_get_single_value(sm
->state
, &sval
) || sval
.value
!= 0)
1820 run_sql(¶m_used_callback
, &found
,
1821 "select * from call_implies where %s and type = %d and parameter = %d and key = '%s';",
1822 get_static_filter(call
->fn
->symbol
), PARAM_USED
, param
, printed_name
);
1826 /* If the database is not built yet, then assume everything is used */
1827 run_sql(¶m_used_callback
, &found
,
1828 "select * from call_implies where %s and type = %d;",
1829 get_static_filter(call
->fn
->symbol
), PARAM_USED
);
1836 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
1838 if (estate_is_whole(sm
->state
))
1840 if (filter_unused_kzalloc_info(call
, param
, printed_name
, sm
))
1842 sql_insert_caller_info(call
, PARAM_VALUE
, param
, printed_name
, sm
->state
->name
);
1843 if (estate_has_fuzzy_max(sm
->state
))
1844 sql_insert_caller_info(call
, FUZZY_MAX
, param
, printed_name
,
1845 sval_to_str(estate_get_fuzzy_max(sm
->state
)));
1848 static void returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
1850 struct symbol
*returned_sym
;
1851 struct sm_state
*sm
;
1852 const char *param_name
;
1856 returned_sym
= expr_to_sym(expr
);
1860 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), sm
) {
1861 if (!estate_rl(sm
->state
))
1863 if (returned_sym
!= sm
->sym
)
1866 param_name
= get_param_name(sm
);
1869 if (strcmp(param_name
, "$") == 0)
1871 compare_str
= name_sym_to_param_comparison(sm
->name
, sm
->sym
);
1872 if (!compare_str
&& estate_is_whole(sm
->state
))
1874 snprintf(buf
, sizeof(buf
), "%s%s", sm
->state
->name
, compare_str
?: "");
1876 sql_insert_return_states(return_id
, return_ranges
, PARAM_VALUE
,
1877 -1, param_name
, buf
);
1878 } END_FOR_EACH_SM(sm
);
1881 static void db_limited_before(void)
1883 unmatched_stree
= clone_stree(__get_cur_stree());
1886 static void db_limited_after(void)
1888 free_stree(&unmatched_stree
);
1891 static int rl_fits_in_type(struct range_list
*rl
, struct symbol
*type
)
1893 if (type_bits(rl_type(rl
)) <= type_bits(type
))
1895 if (sval_cmp(rl_max(rl
), sval_type_max(type
)) > 0)
1897 if (sval_is_negative(rl_min(rl
)) &&
1898 sval_cmp(rl_min(rl
), sval_type_min(type
)) < 0)
1903 static void db_param_limit_filter(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
1905 struct expression
*arg
;
1908 struct var_sym_list
*vsl
= NULL
;
1909 struct sm_state
*sm
;
1910 struct symbol
*compare_type
, *var_type
;
1911 struct range_list
*rl
;
1912 struct range_list
*limit
;
1913 struct range_list
*new;
1915 while (expr
->type
== EXPR_ASSIGNMENT
)
1916 expr
= strip_expr(expr
->right
);
1917 if (expr
->type
!= EXPR_CALL
)
1920 arg
= get_argument_from_call_expr(expr
->args
, param
);
1924 name
= get_chunk_from_key(arg
, key
, &sym
, &vsl
);
1927 if (op
!= PARAM_LIMIT
&& !sym
)
1930 if (strcmp(key
, "$") == 0)
1931 compare_type
= get_arg_type(expr
->fn
, param
);
1933 compare_type
= get_member_type_from_key(arg
, key
);
1935 sm
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
1937 rl
= estate_rl(sm
->state
);
1939 rl
= alloc_whole_rl(compare_type
);
1941 if (op
== PARAM_LIMIT
&& !rl_fits_in_type(rl
, compare_type
))
1944 call_results_to_rl(expr
, compare_type
, value
, &limit
);
1945 new = rl_intersection(rl
, limit
);
1947 var_type
= get_member_type_from_key(arg
, key
);
1948 new = cast_rl(var_type
, new);
1950 /* We want to preserve the implications here */
1951 if (sm
&& rl_equiv(estate_rl(sm
->state
), new))
1954 if (op
== PARAM_LIMIT
)
1955 set_extra_nomod_vsl(name
, sym
, vsl
, alloc_estate_rl(new));
1957 set_extra_mod(name
, sym
, alloc_estate_rl(new));
1964 static void db_param_limit(struct expression
*expr
, int param
, char *key
, char *value
)
1966 db_param_limit_filter(expr
, param
, key
, value
, PARAM_LIMIT
);
1969 static void db_param_filter(struct expression
*expr
, int param
, char *key
, char *value
)
1971 db_param_limit_filter(expr
, param
, key
, value
, PARAM_FILTER
);
1974 static void db_param_add_set(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
1976 struct expression
*arg
;
1979 struct symbol
*type
;
1980 struct smatch_state
*state
;
1981 struct range_list
*new = NULL
;
1982 struct range_list
*added
= NULL
;
1984 while (expr
->type
== EXPR_ASSIGNMENT
)
1985 expr
= strip_expr(expr
->right
);
1986 if (expr
->type
!= EXPR_CALL
)
1989 arg
= get_argument_from_call_expr(expr
->args
, param
);
1992 type
= get_member_type_from_key(arg
, key
);
1993 name
= get_variable_from_key(arg
, key
, &sym
);
1997 state
= get_state(SMATCH_EXTRA
, name
, sym
);
1999 new = estate_rl(state
);
2001 call_results_to_rl(expr
, type
, value
, &added
);
2003 if (op
== PARAM_SET
)
2006 new = rl_union(new, added
);
2008 set_extra_mod(name
, sym
, alloc_estate_rl(new));
2013 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
2015 db_param_add_set(expr
, param
, key
, value
, PARAM_ADD
);
2018 static void db_param_set(struct expression
*expr
, int param
, char *key
, char *value
)
2020 db_param_add_set(expr
, param
, key
, value
, PARAM_SET
);
2023 static void db_param_value(struct expression
*expr
, int param
, char *key
, char *value
)
2025 struct expression
*call
;
2028 struct symbol
*type
;
2029 struct range_list
*rl
= NULL
;
2035 while (call
->type
== EXPR_ASSIGNMENT
)
2036 call
= strip_expr(call
->right
);
2037 if (call
->type
!= EXPR_CALL
)
2040 type
= get_member_type_from_key(expr
->left
, key
);
2041 name
= get_variable_from_key(expr
->left
, key
, &sym
);
2045 call_results_to_rl(call
, type
, value
, &rl
);
2047 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
2052 static void match_call_info(struct expression
*expr
)
2054 struct smatch_state
*state
;
2055 struct range_list
*rl
= NULL
;
2056 struct expression
*arg
;
2057 struct symbol
*type
;
2060 FOR_EACH_PTR(expr
->args
, arg
) {
2061 type
= get_arg_type(expr
->fn
, i
);
2063 if (get_implied_rl(arg
, &rl
))
2064 rl
= cast_rl(type
, rl
);
2066 rl
= cast_rl(type
, alloc_whole_rl(get_type(arg
)));
2068 if (!is_whole_rl(rl
))
2069 sql_insert_caller_info(expr
, PARAM_VALUE
, i
, "$", show_rl(rl
));
2070 state
= get_state_expr(SMATCH_EXTRA
, arg
);
2071 if (estate_has_fuzzy_max(state
)) {
2072 sql_insert_caller_info(expr
, FUZZY_MAX
, i
, "$",
2073 sval_to_str(estate_get_fuzzy_max(state
)));
2076 } END_FOR_EACH_PTR(arg
);
2079 static void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2081 struct range_list
*rl
= NULL
;
2082 struct smatch_state
*state
;
2083 struct symbol
*type
;
2086 if (strcmp(key
, "*$") == 0)
2087 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
2088 else if (strncmp(key
, "$", 1) == 0)
2089 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
2093 type
= get_member_type_from_key(symbol_expression(sym
), key
);
2094 str_to_rl(type
, value
, &rl
);
2095 state
= alloc_estate_rl(rl
);
2096 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
2099 static void set_param_hard_max(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2101 struct range_list
*rl
= NULL
;
2102 struct smatch_state
*state
;
2103 struct symbol
*type
;
2107 if (strcmp(key
, "*$") == 0)
2108 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
2109 else if (strncmp(key
, "$", 1) == 0)
2110 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
2114 state
= get_state(SMATCH_EXTRA
, fullname
, sym
);
2117 type
= get_member_type_from_key(symbol_expression(sym
), key
);
2118 str_to_rl(type
, value
, &rl
);
2119 if (!rl_to_sval(rl
, &max
))
2121 estate_set_fuzzy_max(state
, max
);
2124 struct smatch_state
*get_extra_state(struct expression
*expr
)
2128 struct smatch_state
*ret
= NULL
;
2129 struct range_list
*rl
;
2131 if (is_pointer(expr
) && get_address_rl(expr
, &rl
))
2132 return alloc_estate_rl(rl
);
2134 name
= expr_to_known_chunk_sym(expr
, &sym
);
2138 ret
= get_state(SMATCH_EXTRA
, name
, sym
);
2144 void register_smatch_extra(int id
)
2148 add_merge_hook(my_id
, &merge_estates
);
2149 add_unmatched_state_hook(my_id
, &unmatched_state
);
2150 select_caller_info_hook(set_param_value
, PARAM_VALUE
);
2151 select_caller_info_hook(set_param_hard_max
, FUZZY_MAX
);
2152 select_return_states_before(&db_limited_before
);
2153 select_return_states_hook(PARAM_LIMIT
, &db_param_limit
);
2154 select_return_states_hook(PARAM_FILTER
, &db_param_filter
);
2155 select_return_states_hook(PARAM_ADD
, &db_param_add
);
2156 select_return_states_hook(PARAM_SET
, &db_param_set
);
2157 select_return_states_hook(PARAM_VALUE
, &db_param_value
);
2158 select_return_states_after(&db_limited_after
);
2161 static void match_link_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
2163 struct var_sym_list
*links
;
2164 struct var_sym
*tmp
;
2165 struct smatch_state
*state
;
2167 links
= sm
->state
->data
;
2169 FOR_EACH_PTR(links
, tmp
) {
2170 if (sm
->sym
== tmp
->sym
&&
2171 strcmp(sm
->name
, tmp
->var
) == 0)
2173 state
= get_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
);
2176 set_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
, alloc_estate_whole(estate_type(state
)));
2177 } END_FOR_EACH_PTR(tmp
);
2178 set_state(link_id
, sm
->name
, sm
->sym
, &undefined
);
2181 void register_smatch_extra_links(int id
)
2186 void register_smatch_extra_late(int id
)
2188 add_merge_hook(link_id
, &merge_link_states
);
2189 add_modification_hook(link_id
, &match_link_modify
);
2190 add_hook(&match_dereferences
, DEREF_HOOK
);
2191 add_hook(&match_pointer_as_array
, OP_HOOK
);
2192 select_call_implies_hook(DEREFERENCE
, &set_param_dereferenced
);
2193 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
2194 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
2195 add_hook(&match_assign
, GLOBAL_ASSIGNMENT_HOOK
);
2196 add_hook(&unop_expr
, OP_HOOK
);
2197 add_hook(&asm_expr
, ASM_HOOK
);
2198 add_untracked_param_hook(&match_untracked_array
);
2200 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2201 add_member_info_callback(my_id
, struct_member_callback
);
2202 add_split_return_callback(&returned_struct_members
);
2204 add_hook(&assume_indexes_are_valid
, OP_HOOK
);