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 static void 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 set_state(SMATCH_EXTRA
, name
, sym
, state
);
91 static char *get_pointed_at(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
93 struct expression
*assigned
;
97 if (strcmp(name
+ 1, sym
->ident
->name
) != 0)
100 assigned
= get_assigned_expr_name_sym(sym
->ident
->name
, sym
);
103 assigned
= strip_parens(assigned
);
104 if (assigned
->type
!= EXPR_PREOP
|| assigned
->op
!= '&')
107 return expr_to_var_sym(assigned
->unop
, new_sym
);
110 char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
112 struct expression
*assigned
;
113 char *orig_name
= NULL
;
120 if (!sym
|| !sym
->ident
)
123 ret
= get_pointed_at(name
, sym
, new_sym
);
127 skip
= strlen(sym
->ident
->name
);
128 if (name
[skip
] != '-' || name
[skip
+ 1] != '>')
132 assigned
= get_assigned_expr_name_sym(sym
->ident
->name
, sym
);
135 if (assigned
->type
== EXPR_CALL
)
136 return map_call_to_other_name_sym(name
, sym
, new_sym
);
137 if (assigned
->type
== EXPR_PREOP
|| assigned
->op
== '&') {
139 orig_name
= expr_to_var_sym(assigned
, new_sym
);
140 if (!orig_name
|| !*new_sym
)
143 snprintf(buf
, sizeof(buf
), "%s.%s", orig_name
+ 1, name
+ skip
);
144 ret
= alloc_string(buf
);
145 free_string(orig_name
);
149 if (assigned
->type
!= EXPR_DEREF
)
152 orig_name
= expr_to_var_sym(assigned
, new_sym
);
153 if (!orig_name
|| !*new_sym
)
156 snprintf(buf
, sizeof(buf
), "%s->%s", orig_name
, name
+ skip
);
157 ret
= alloc_string(buf
);
158 free_string(orig_name
);
162 free_string(orig_name
);
166 void set_extra_mod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
169 struct symbol
*new_sym
;
171 set_extra_mod_helper(name
, sym
, state
);
172 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
173 if (new_name
&& new_sym
)
174 set_extra_mod_helper(new_name
, new_sym
, state
);
175 free_string(new_name
);
178 static void clear_array_states(struct expression
*array
)
182 sm
= get_sm_state_expr(link_id
, array
);
184 match_link_modify(sm
, NULL
);
187 static void set_extra_array_mod(struct expression
*expr
, struct smatch_state
*state
)
189 struct expression
*array
;
190 struct var_sym_list
*vsl
;
195 array
= get_array_base(expr
);
197 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
199 clear_array_states(array
);
203 FOR_EACH_PTR(vsl
, vs
) {
204 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
205 } END_FOR_EACH_PTR(vs
);
207 call_extra_mod_hooks(name
, sym
, state
);
208 set_state(SMATCH_EXTRA
, name
, sym
, state
);
213 void set_extra_expr_mod(struct expression
*expr
, struct smatch_state
*state
)
218 if (is_array(expr
)) {
219 set_extra_array_mod(expr
, state
);
223 expr
= strip_expr(expr
);
224 name
= expr_to_var_sym(expr
, &sym
);
227 set_extra_mod(name
, sym
, state
);
232 void set_extra_nomod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
235 struct symbol
*new_sym
;
236 struct relation
*rel
;
237 struct smatch_state
*orig_state
;
239 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
241 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
242 if (new_name
&& new_sym
)
243 set_state(SMATCH_EXTRA
, new_name
, new_sym
, state
);
244 free_string(new_name
);
246 if (!estate_related(orig_state
)) {
247 set_state(SMATCH_EXTRA
, name
, sym
, state
);
251 set_related(state
, estate_related(orig_state
));
252 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
253 struct smatch_state
*estate
;
255 if (option_debug_related
)
256 sm_msg("%s updating related %s to %s", name
, rel
->name
, state
->name
);
257 estate
= get_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
);
260 set_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
, clone_estate_cast(estate_type(estate
), state
));
261 } END_FOR_EACH_PTR(rel
);
264 void set_extra_nomod_vsl(const char *name
, struct symbol
*sym
, struct var_sym_list
*vsl
, struct smatch_state
*state
)
268 FOR_EACH_PTR(vsl
, vs
) {
269 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
270 } END_FOR_EACH_PTR(vs
);
272 set_extra_nomod(name
, sym
, state
);
276 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
278 void set_extra_expr_nomod(struct expression
*expr
, struct smatch_state
*state
)
280 struct var_sym_list
*vsl
;
285 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
288 FOR_EACH_PTR(vsl
, vs
) {
289 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
290 } END_FOR_EACH_PTR(vs
);
292 set_extra_nomod(name
, sym
, state
);
297 static void set_extra_true_false(const char *name
, struct symbol
*sym
,
298 struct smatch_state
*true_state
,
299 struct smatch_state
*false_state
)
302 struct symbol
*new_sym
;
303 struct relation
*rel
;
304 struct smatch_state
*orig_state
;
306 if (!true_state
&& !false_state
)
309 if (in_warn_on_macro())
312 new_name
= get_other_name_sym(name
, sym
, &new_sym
);
313 if (new_name
&& new_sym
)
314 set_true_false_states(SMATCH_EXTRA
, new_name
, new_sym
, true_state
, false_state
);
315 free_string(new_name
);
317 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
319 if (!estate_related(orig_state
)) {
320 set_true_false_states(SMATCH_EXTRA
, name
, sym
, true_state
, false_state
);
325 set_related(true_state
, estate_related(orig_state
));
327 set_related(false_state
, estate_related(orig_state
));
329 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
330 set_true_false_states(SMATCH_EXTRA
, rel
->name
, rel
->sym
,
331 true_state
, false_state
);
332 } END_FOR_EACH_PTR(rel
);
335 static void set_extra_chunk_true_false(struct expression
*expr
,
336 struct smatch_state
*true_state
,
337 struct smatch_state
*false_state
)
339 struct var_sym_list
*vsl
;
345 if (in_warn_on_macro())
348 type
= get_type(expr
);
352 name
= expr_to_chunk_sym_vsl(expr
, &sym
, &vsl
);
355 FOR_EACH_PTR(vsl
, vs
) {
356 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
357 } END_FOR_EACH_PTR(vs
);
359 set_true_false_states(SMATCH_EXTRA
, name
, sym
,
360 clone_estate(true_state
),
361 clone_estate(false_state
));
366 static void set_extra_expr_true_false(struct expression
*expr
,
367 struct smatch_state
*true_state
,
368 struct smatch_state
*false_state
)
374 if (!true_state
&& !false_state
)
377 if (get_value(expr
, &sval
))
380 expr
= strip_expr(expr
);
381 name
= expr_to_var_sym(expr
, &sym
);
384 set_extra_chunk_true_false(expr
, true_state
, false_state
);
387 set_extra_true_false(name
, sym
, true_state
, false_state
);
391 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
393 struct expression
*iter_var
;
394 struct expression
*condition
;
396 struct smatch_state
*estate
;
399 condition
= strip_expr(loop
->iterator_pre_condition
);
402 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
404 if (condition
->op
!= SPECIAL_DECREMENT
)
407 iter_var
= condition
->unop
;
408 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
411 if (sval_cmp_val(estate_min(sm
->state
), 0) < 0)
413 start
= estate_max(sm
->state
);
414 if (sval_cmp_val(start
, 0) <= 0)
416 if (!sval_is_max(start
))
419 if (condition
->type
== EXPR_PREOP
) {
420 estate
= alloc_estate_range(sval_type_val(start
.type
, 1), start
);
421 if (estate_has_hard_max(sm
->state
))
422 estate_set_hard_max(estate
);
423 estate_copy_fuzzy_max(estate
, sm
->state
);
424 set_extra_expr_mod(iter_var
, estate
);
426 if (condition
->type
== EXPR_POSTOP
) {
427 estate
= alloc_estate_range(sval_type_val(start
.type
, 0), start
);
428 if (estate_has_hard_max(sm
->state
))
429 estate_set_hard_max(estate
);
430 estate_copy_fuzzy_max(estate
, sm
->state
);
431 set_extra_expr_mod(iter_var
, estate
);
433 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
436 static struct sm_state
*handle_canonical_for_inc(struct expression
*iter_expr
,
437 struct expression
*condition
)
439 struct expression
*iter_var
;
441 struct smatch_state
*estate
;
442 sval_t start
, end
, max
;
444 iter_var
= iter_expr
->unop
;
445 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
448 if (!estate_get_single_value(sm
->state
, &start
))
450 if (get_implied_max(condition
->right
, &end
))
451 end
= sval_cast(get_type(iter_var
), end
);
453 end
= sval_type_max(get_type(iter_var
));
455 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
458 switch (condition
->op
) {
459 case SPECIAL_UNSIGNED_LT
:
460 case SPECIAL_NOTEQUAL
:
462 if (!sval_is_min(end
))
465 case SPECIAL_UNSIGNED_LTE
:
471 if (sval_cmp(end
, start
) < 0)
473 estate
= alloc_estate_range(start
, end
);
474 if (get_hard_max(condition
->right
, &max
)) {
475 estate_set_hard_max(estate
);
476 if (condition
->op
== '<' ||
477 condition
->op
== SPECIAL_UNSIGNED_LT
||
478 condition
->op
== SPECIAL_NOTEQUAL
)
480 estate_set_fuzzy_max(estate
, max
);
482 set_extra_expr_mod(iter_var
, estate
);
483 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
486 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
487 struct expression
*condition
)
489 struct expression
*iter_var
;
491 struct smatch_state
*estate
;
494 iter_var
= iter_expr
->unop
;
495 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
498 if (!estate_get_single_value(sm
->state
, &start
))
500 if (!get_implied_min(condition
->right
, &end
))
501 end
= sval_type_min(get_type(iter_var
));
502 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
505 switch (condition
->op
) {
506 case SPECIAL_NOTEQUAL
:
508 if (!sval_is_min(end
) && !sval_is_max(end
))
516 if (sval_cmp(end
, start
) > 0)
518 estate
= alloc_estate_range(end
, start
);
519 estate_set_hard_max(estate
);
520 estate_set_fuzzy_max(estate
, estate_get_fuzzy_max(estate
));
521 set_extra_expr_mod(iter_var
, estate
);
522 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
525 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
527 struct expression
*iter_expr
;
528 struct expression
*condition
;
530 if (!loop
->iterator_post_statement
)
532 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
534 iter_expr
= loop
->iterator_post_statement
->expression
;
535 if (!loop
->iterator_pre_condition
)
537 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
539 condition
= loop
->iterator_pre_condition
;
541 if (iter_expr
->op
== SPECIAL_INCREMENT
)
542 return handle_canonical_for_inc(iter_expr
, condition
);
543 if (iter_expr
->op
== SPECIAL_DECREMENT
)
544 return handle_canonical_for_dec(iter_expr
, condition
);
548 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct stree
**stree
)
550 struct sm_state
*ret
;
552 __push_fake_cur_stree();
553 if (!loop
->iterator_post_statement
)
554 ret
= handle_canonical_while_count_down(loop
);
556 ret
= handle_canonical_for_loops(loop
);
557 *stree
= __pop_fake_cur_stree();
561 int __iterator_unchanged(struct sm_state
*sm
)
565 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
570 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
574 /* paranoid checking. prolly not needed */
575 condition
= strip_expr(condition
);
578 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
580 if (condition
->op
!= SPECIAL_DECREMENT
)
582 after_value
= estate_min(sm
->state
);
584 set_extra_mod(sm
->name
, sm
->sym
, alloc_estate_sval(after_value
));
587 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
588 struct statement
*iterator
,
589 struct expression
*condition
)
591 struct expression
*iter_expr
;
593 struct smatch_state
*state
;
596 while_count_down_after(sm
, condition
);
600 iter_expr
= iterator
->expression
;
602 if (condition
->type
!= EXPR_COMPARE
)
604 if (iter_expr
->op
== SPECIAL_INCREMENT
) {
605 limit
= sval_binop(estate_max(sm
->state
), '+',
606 sval_type_val(estate_type(sm
->state
), 1));
608 limit
= sval_binop(estate_min(sm
->state
), '-',
609 sval_type_val(estate_type(sm
->state
), 1));
611 if (!estate_has_hard_max(sm
->state
) && !__has_breaks()) {
612 if (iter_expr
->op
== SPECIAL_INCREMENT
)
613 state
= alloc_estate_range(estate_min(sm
->state
), limit
);
615 state
= alloc_estate_range(limit
, estate_max(sm
->state
));
617 state
= alloc_estate_sval(limit
);
619 if (!estate_has_hard_max(sm
->state
)) {
620 estate_clear_hard_max(state
);
622 if (estate_has_fuzzy_max(sm
->state
)) {
623 sval_t hmax
= estate_get_fuzzy_max(sm
->state
);
624 sval_t max
= estate_max(sm
->state
);
626 if (sval_cmp(hmax
, max
) != 0)
627 estate_clear_fuzzy_max(state
);
628 } else if (!estate_has_fuzzy_max(sm
->state
)) {
629 estate_clear_fuzzy_max(state
);
632 set_extra_mod(sm
->name
, sm
->sym
, state
);
635 static struct stree
*unmatched_stree
;
636 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
638 struct smatch_state
*state
;
640 if (unmatched_stree
) {
641 state
= get_state_stree(unmatched_stree
, SMATCH_EXTRA
, sm
->name
, sm
->sym
);
645 if (parent_is_gone_var_sym(sm
->name
, sm
->sym
))
646 return alloc_estate_empty();
647 return alloc_estate_whole(estate_type(sm
->state
));
650 static void clear_the_pointed_at(struct expression
*expr
)
655 struct sm_state
*tmp
;
657 name
= expr_to_var_sym(expr
, &sym
);
661 stree
= __get_cur_stree();
662 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
663 if (tmp
->name
[0] != '*')
667 if (strcmp(tmp
->name
+ 1, name
) != 0)
669 set_extra_mod(tmp
->name
, tmp
->sym
, alloc_estate_whole(estate_type(tmp
->state
)));
670 } END_FOR_EACH_SM(tmp
);
676 static void match_function_call(struct expression
*expr
)
678 struct expression
*arg
;
679 struct expression
*tmp
;
681 /* if we have the db this is handled in smatch_function_hooks.c */
684 if (inlinable(expr
->fn
))
687 FOR_EACH_PTR(expr
->args
, arg
) {
688 tmp
= strip_expr(arg
);
689 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
690 set_extra_expr_mod(tmp
->unop
, alloc_estate_whole(get_type(tmp
->unop
)));
692 clear_the_pointed_at(tmp
);
693 } END_FOR_EACH_PTR(arg
);
696 static int values_fit_type(struct expression
*left
, struct expression
*right
)
698 struct range_list
*rl
;
701 type
= get_type(left
);
704 get_absolute_rl(right
, &rl
);
705 if (type_unsigned(type
) && sval_is_negative(rl_min(rl
)))
707 if (sval_cmp(sval_type_min(type
), rl_min(rl
)) > 0)
709 if (sval_cmp(sval_type_max(type
), rl_max(rl
)) < 0)
714 static void save_chunk_info(struct expression
*left
, struct expression
*right
)
716 struct var_sym_list
*vsl
;
718 struct expression
*add_expr
;
724 if (right
->type
!= EXPR_BINOP
|| right
->op
!= '-')
726 if (!get_value(right
->left
, &sval
))
728 if (!expr_to_sym(right
->right
))
731 add_expr
= binop_expression(left
, '+', right
->right
);
732 type
= get_type(add_expr
);
735 name
= expr_to_chunk_sym_vsl(add_expr
, &sym
, &vsl
);
738 FOR_EACH_PTR(vsl
, vs
) {
739 store_link(link_id
, vs
->var
, vs
->sym
, name
, sym
);
740 } END_FOR_EACH_PTR(vs
);
742 set_state(SMATCH_EXTRA
, name
, sym
, alloc_estate_sval(sval_cast(type
, sval
)));
747 static void do_array_assign(struct expression
*left
, int op
, struct expression
*right
)
749 struct range_list
*rl
;
752 get_absolute_rl(right
, &rl
);
753 rl
= cast_rl(get_type(left
), rl
);
755 rl
= alloc_whole_rl(get_type(left
));
758 set_extra_array_mod(left
, alloc_estate_rl(rl
));
761 static void match_untracked_array(struct expression
*call
, int param
)
763 struct expression
*arg
;
765 arg
= get_argument_from_call_expr(call
->args
, param
);
766 arg
= strip_expr(arg
);
768 clear_array_states(arg
);
771 static void match_vanilla_assign(struct expression
*left
, struct expression
*right
)
773 struct range_list
*orig_rl
= NULL
;
774 struct range_list
*rl
= NULL
;
775 struct symbol
*right_sym
;
776 struct symbol
*left_type
;
777 struct symbol
*right_type
;
778 char *right_name
= NULL
;
782 struct smatch_state
*state
;
788 save_chunk_info(left
, right
);
790 name
= expr_to_var_sym(left
, &sym
);
793 do_array_assign(left
, '=', right
);
797 left_type
= get_type(left
);
798 right_type
= get_type(right
);
800 right_name
= expr_to_var_sym(right
, &right_sym
);
802 if (!__in_fake_assign
&&
803 !(right
->type
== EXPR_PREOP
&& right
->op
== '&') &&
804 right_name
&& right_sym
&&
805 values_fit_type(left
, right
) &&
806 !has_symbol(right
, sym
)) {
807 set_equiv(left
, right
);
811 if (is_pointer(right
) && get_address_rl(right
, &rl
)) {
812 state
= alloc_estate_rl(rl
);
816 if (__in_fake_assign
) {
817 struct smatch_state
*right_state
;
820 if (get_value(right
, &sval
)) {
821 sval
= sval_cast(left_type
, sval
);
822 state
= alloc_estate_sval(sval
);
826 right_state
= get_state(SMATCH_EXTRA
, right_name
, right_sym
);
828 /* simple assignment */
829 state
= clone_estate(right_state
);
833 state
= alloc_estate_rl(alloc_whole_rl(left_type
));
837 comparison
= get_comparison(left
, right
);
839 comparison
= flip_comparison(comparison
);
840 get_implied_rl(left
, &orig_rl
);
843 if (get_implied_rl(right
, &rl
)) {
844 rl
= cast_rl(left_type
, rl
);
846 filter_by_comparison(&rl
, comparison
, orig_rl
);
847 state
= alloc_estate_rl(rl
);
848 if (get_hard_max(right
, &max
)) {
849 estate_set_hard_max(state
);
850 estate_set_fuzzy_max(state
, max
);
853 rl
= alloc_whole_rl(right_type
);
854 rl
= cast_rl(left_type
, rl
);
856 filter_by_comparison(&rl
, comparison
, orig_rl
);
857 state
= alloc_estate_rl(rl
);
861 set_extra_mod(name
, sym
, state
);
863 free_string(right_name
);
866 static int op_remove_assign(int op
)
869 case SPECIAL_ADD_ASSIGN
:
871 case SPECIAL_SUB_ASSIGN
:
873 case SPECIAL_MUL_ASSIGN
:
875 case SPECIAL_DIV_ASSIGN
:
877 case SPECIAL_MOD_ASSIGN
:
879 case SPECIAL_AND_ASSIGN
:
881 case SPECIAL_OR_ASSIGN
:
883 case SPECIAL_XOR_ASSIGN
:
885 case SPECIAL_SHL_ASSIGN
:
886 return SPECIAL_LEFTSHIFT
;
887 case SPECIAL_SHR_ASSIGN
:
888 return SPECIAL_RIGHTSHIFT
;
894 static void match_assign(struct expression
*expr
)
896 struct range_list
*rl
= NULL
;
897 struct expression
*left
;
898 struct expression
*right
;
899 struct expression
*binop_expr
;
900 struct symbol
*left_type
;
903 sval_t left_min
, left_max
;
904 sval_t right_min
, right_max
;
905 sval_t res_min
, res_max
;
907 left
= strip_expr(expr
->left
);
909 right
= strip_parens(expr
->right
);
910 if (right
->type
== EXPR_CALL
&& sym_name_is("__builtin_expect", right
->fn
))
911 right
= get_argument_from_call_expr(right
->args
, 0);
912 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
913 right
= strip_parens(right
->left
);
915 if (expr
->op
== '=' && is_condition(expr
->right
))
916 return; /* handled in smatch_condition.c */
917 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
918 return; /* handled in smatch_function_hooks.c */
919 if (expr
->op
== '=') {
920 match_vanilla_assign(left
, right
);
924 name
= expr_to_var_sym(left
, &sym
);
928 left_type
= get_type(left
);
930 res_min
= sval_type_min(left_type
);
931 res_max
= sval_type_max(left_type
);
934 case SPECIAL_ADD_ASSIGN
:
935 get_absolute_max(left
, &left_max
);
936 get_absolute_max(right
, &right_max
);
937 if (sval_binop_overflows(left_max
, '+', sval_cast(left_type
, right_max
)))
939 if (get_implied_min(left
, &left_min
) &&
940 !sval_is_negative_min(left_min
) &&
941 get_implied_min(right
, &right_min
) &&
942 !sval_is_negative_min(right_min
)) {
943 res_min
= sval_binop(left_min
, '+', right_min
);
944 res_min
= sval_cast(left_type
, res_min
);
946 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
948 res_max
= sval_binop(left_max
, '+', right_max
);
949 res_max
= sval_cast(left_type
, res_max
);
951 case SPECIAL_SUB_ASSIGN
:
952 if (get_implied_max(left
, &left_max
) &&
953 !sval_is_max(left_max
) &&
954 get_implied_min(right
, &right_min
) &&
955 !sval_is_min(right_min
)) {
956 res_max
= sval_binop(left_max
, '-', right_min
);
957 res_max
= sval_cast(left_type
, res_max
);
961 if (get_implied_min(left
, &left_min
) &&
962 !sval_is_min(left_min
) &&
963 get_implied_max(right
, &right_max
) &&
964 !sval_is_max(right_max
) &&
965 sval_cmp(left_min
, right_max
) > 0) {
966 res_min
= sval_binop(left_min
, '-', right_max
);
967 res_min
= sval_cast(left_type
, res_min
);
970 case SPECIAL_AND_ASSIGN
:
971 case SPECIAL_MOD_ASSIGN
:
972 case SPECIAL_SHL_ASSIGN
:
973 case SPECIAL_SHR_ASSIGN
:
974 case SPECIAL_OR_ASSIGN
:
975 case SPECIAL_XOR_ASSIGN
:
976 case SPECIAL_MUL_ASSIGN
:
977 case SPECIAL_DIV_ASSIGN
:
978 binop_expr
= binop_expression(expr
->left
,
979 op_remove_assign(expr
->op
),
981 if (get_absolute_rl(binop_expr
, &rl
)) {
982 rl
= cast_rl(left_type
, rl
);
983 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
988 rl
= cast_rl(left_type
, alloc_rl(res_min
, res_max
));
989 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
994 static struct smatch_state
*increment_state(struct smatch_state
*state
)
996 sval_t min
= estate_min(state
);
997 sval_t max
= estate_max(state
);
999 if (!estate_rl(state
))
1003 max
= sval_type_max(max
.type
);
1005 if (!sval_is_min(min
) && !sval_is_max(min
))
1007 if (!sval_is_min(max
) && !sval_is_max(max
))
1009 return alloc_estate_range(min
, max
);
1012 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
1014 sval_t min
= estate_min(state
);
1015 sval_t max
= estate_max(state
);
1017 if (!estate_rl(state
))
1021 min
= sval_type_min(min
.type
);
1023 if (!sval_is_min(min
) && !sval_is_max(min
))
1025 if (!sval_is_min(max
) && !sval_is_max(max
))
1027 return alloc_estate_range(min
, max
);
1030 static void clear_pointed_at_state(struct expression
*expr
)
1032 struct symbol
*type
;
1035 * ALERT: This is sort of a mess. If it's is a struct assigment like
1036 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1037 * the same thing for p++ where "p" is a struct. Most modifications
1038 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1039 * use smatch_modification.c because we have to get the ordering right
1040 * or something. So if you have p++ where p is a pointer to a standard
1041 * c type then we handle that here. What a mess.
1044 type
= get_type(expr
);
1045 if (!type
|| type
->type
!= SYM_PTR
)
1047 type
= get_real_base_type(type
);
1048 if (!type
|| type
->type
!= SYM_BASETYPE
)
1050 set_extra_expr_mod(deref_expression(expr
), alloc_estate_whole(type
));
1053 static void unop_expr(struct expression
*expr
)
1055 struct smatch_state
*state
;
1057 if (expr
->smatch_flags
& Handled
)
1061 case SPECIAL_INCREMENT
:
1062 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
1063 state
= increment_state(state
);
1065 state
= alloc_estate_whole(get_type(expr
));
1066 set_extra_expr_mod(expr
->unop
, state
);
1067 clear_pointed_at_state(expr
->unop
);
1069 case SPECIAL_DECREMENT
:
1070 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
1071 state
= decrement_state(state
);
1073 state
= alloc_estate_whole(get_type(expr
));
1074 set_extra_expr_mod(expr
->unop
, state
);
1075 clear_pointed_at_state(expr
->unop
);
1082 static void asm_expr(struct statement
*stmt
)
1085 struct expression
*expr
;
1086 struct symbol
*type
;
1089 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
1091 case 0: /* identifier */
1092 case 1: /* constraint */
1095 case 2: /* expression */
1097 type
= get_type(strip_expr(expr
));
1098 set_extra_expr_mod(expr
, alloc_estate_whole(type
));
1101 } END_FOR_EACH_PTR(expr
);
1104 static void check_dereference(struct expression
*expr
)
1106 if (__in_fake_assign
)
1108 if (outside_of_function())
1110 if (implied_not_equal(expr
, 0))
1112 set_extra_expr_nomod(expr
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
1115 static void match_dereferences(struct expression
*expr
)
1117 if (expr
->type
!= EXPR_PREOP
)
1119 /* it's saying that foo[1] = bar dereferences foo[1] */
1122 check_dereference(expr
->unop
);
1125 static void match_pointer_as_array(struct expression
*expr
)
1127 if (!is_array(expr
))
1129 check_dereference(get_array_base(expr
));
1132 static void find_dereferences(struct expression
*expr
)
1134 while (expr
->type
== EXPR_PREOP
) {
1135 if (expr
->op
== '*')
1136 check_dereference(expr
->unop
);
1137 expr
= strip_expr(expr
->unop
);
1141 static void set_param_dereferenced(struct expression
*arg
, char *key
, char *unused
)
1146 name
= get_variable_from_key(arg
, key
, &sym
);
1148 set_extra_nomod(name
, sym
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
1151 find_dereferences(arg
);
1154 static sval_t
add_one(sval_t sval
)
1160 static int handle_postop_inc(struct expression
*left
, int op
, struct expression
*right
)
1162 struct statement
*stmt
;
1163 struct expression
*cond
;
1164 struct smatch_state
*true_state
, *false_state
;
1169 * If we're decrementing here then that's a canonical while count down
1170 * so it's handled already. We're only handling loops like:
1172 * do { ... } while (i++ < 3);
1175 if (left
->type
!= EXPR_POSTOP
|| left
->op
!= SPECIAL_INCREMENT
)
1178 stmt
= __cur_stmt
->parent
;
1181 if (stmt
->type
== STMT_COMPOUND
)
1182 stmt
= stmt
->parent
;
1183 if (!stmt
|| stmt
->type
!= STMT_ITERATOR
|| !stmt
->iterator_post_condition
)
1186 cond
= strip_expr(stmt
->iterator_post_condition
);
1187 if (cond
->type
!= EXPR_COMPARE
|| cond
->op
!= op
)
1189 if (left
!= strip_expr(cond
->left
) || right
!= strip_expr(cond
->right
))
1192 if (!get_implied_value(left
->unop
, &start
))
1194 if (!get_implied_value(right
, &limit
))
1197 if (sval_cmp(start
, limit
) > 0)
1202 case SPECIAL_UNSIGNED_LT
:
1205 case SPECIAL_UNSIGNED_LTE
:
1206 limit
= add_one(limit
);
1212 true_state
= alloc_estate_range(add_one(start
), limit
);
1213 false_state
= alloc_estate_range(add_one(limit
), add_one(limit
));
1215 /* Currently we just discard the false state but when two passes is
1216 * implimented correctly then it will use it.
1219 set_extra_expr_true_false(left
->unop
, true_state
, false_state
);
1224 static void handle_comparison(struct symbol
*type
, struct expression
*left
, int op
, struct expression
*right
)
1226 struct range_list
*left_orig
;
1227 struct range_list
*left_true
;
1228 struct range_list
*left_false
;
1229 struct range_list
*right_orig
;
1230 struct range_list
*right_true
;
1231 struct range_list
*right_false
;
1232 struct smatch_state
*left_true_state
;
1233 struct smatch_state
*left_false_state
;
1234 struct smatch_state
*right_true_state
;
1235 struct smatch_state
*right_false_state
;
1236 sval_t dummy
, hard_max
;
1237 int left_postop
= 0;
1238 int right_postop
= 0;
1240 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
1241 if (left
->type
== EXPR_POSTOP
) {
1242 left
->smatch_flags
|= Handled
;
1243 left_postop
= left
->op
;
1244 if (handle_postop_inc(left
, op
, right
))
1247 left
= strip_parens(left
->unop
);
1249 while (left
->type
== EXPR_ASSIGNMENT
)
1250 left
= strip_parens(left
->left
);
1252 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
1253 if (right
->type
== EXPR_POSTOP
) {
1254 right
->smatch_flags
|= Handled
;
1255 right_postop
= right
->op
;
1257 right
= strip_parens(right
->unop
);
1260 get_real_absolute_rl(left
, &left_orig
);
1261 left_orig
= cast_rl(type
, left_orig
);
1263 get_real_absolute_rl(right
, &right_orig
);
1264 right_orig
= cast_rl(type
, right_orig
);
1266 split_comparison_rl(left_orig
, op
, right_orig
, &left_true
, &left_false
, &right_true
, &right_false
);
1268 left_true
= rl_truncate_cast(get_type(strip_expr(left
)), left_true
);
1269 left_false
= rl_truncate_cast(get_type(strip_expr(left
)), left_false
);
1270 right_true
= rl_truncate_cast(get_type(strip_expr(right
)), right_true
);
1271 right_false
= rl_truncate_cast(get_type(strip_expr(right
)), right_false
);
1273 if (!left_true
|| !left_false
) {
1274 struct range_list
*tmp_true
, *tmp_false
;
1276 split_comparison_rl(alloc_whole_rl(type
), op
, right_orig
, &tmp_true
, &tmp_false
, NULL
, NULL
);
1277 tmp_true
= rl_truncate_cast(get_type(strip_expr(left
)), tmp_true
);
1278 tmp_false
= rl_truncate_cast(get_type(strip_expr(left
)), tmp_false
);
1279 if (tmp_true
&& tmp_false
)
1280 __save_imaginary_state(left
, tmp_true
, tmp_false
);
1283 if (!right_true
|| !right_false
) {
1284 struct range_list
*tmp_true
, *tmp_false
;
1286 split_comparison_rl(alloc_whole_rl(type
), op
, right_orig
, NULL
, NULL
, &tmp_true
, &tmp_false
);
1287 tmp_true
= rl_truncate_cast(get_type(strip_expr(right
)), tmp_true
);
1288 tmp_false
= rl_truncate_cast(get_type(strip_expr(right
)), tmp_false
);
1289 if (tmp_true
&& tmp_false
)
1290 __save_imaginary_state(right
, tmp_true
, tmp_false
);
1293 left_true_state
= alloc_estate_rl(left_true
);
1294 left_false_state
= alloc_estate_rl(left_false
);
1295 right_true_state
= alloc_estate_rl(right_true
);
1296 right_false_state
= alloc_estate_rl(right_false
);
1300 case SPECIAL_UNSIGNED_LT
:
1301 case SPECIAL_UNSIGNED_LTE
:
1303 if (get_hard_max(right
, &dummy
))
1304 estate_set_hard_max(left_true_state
);
1305 if (get_hard_max(left
, &dummy
))
1306 estate_set_hard_max(right_false_state
);
1309 case SPECIAL_UNSIGNED_GT
:
1310 case SPECIAL_UNSIGNED_GTE
:
1312 if (get_hard_max(left
, &dummy
))
1313 estate_set_hard_max(right_true_state
);
1314 if (get_hard_max(right
, &dummy
))
1315 estate_set_hard_max(left_false_state
);
1321 case SPECIAL_UNSIGNED_LT
:
1322 case SPECIAL_UNSIGNED_LTE
:
1324 if (get_hard_max(right
, &hard_max
)) {
1325 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1327 estate_set_fuzzy_max(left_true_state
, hard_max
);
1329 if (get_implied_value(right
, &hard_max
)) {
1330 if (op
== SPECIAL_UNSIGNED_LTE
||
1333 estate_set_fuzzy_max(left_false_state
, hard_max
);
1335 if (get_hard_max(left
, &hard_max
)) {
1336 if (op
== SPECIAL_UNSIGNED_LTE
||
1339 estate_set_fuzzy_max(right_false_state
, hard_max
);
1341 if (get_implied_value(left
, &hard_max
)) {
1342 if (op
== '<' || op
== SPECIAL_UNSIGNED_LT
)
1344 estate_set_fuzzy_max(right_true_state
, hard_max
);
1348 case SPECIAL_UNSIGNED_GT
:
1349 case SPECIAL_UNSIGNED_GTE
:
1351 if (get_hard_max(left
, &hard_max
)) {
1352 if (op
== '>' || op
== SPECIAL_UNSIGNED_GT
)
1354 estate_set_fuzzy_max(right_true_state
, hard_max
);
1356 if (get_implied_value(left
, &hard_max
)) {
1357 if (op
== SPECIAL_UNSIGNED_GTE
||
1360 estate_set_fuzzy_max(right_false_state
, hard_max
);
1362 if (get_hard_max(right
, &hard_max
)) {
1363 if (op
== SPECIAL_UNSIGNED_LTE
||
1366 estate_set_fuzzy_max(left_false_state
, hard_max
);
1368 if (get_implied_value(right
, &hard_max
)) {
1370 op
== SPECIAL_UNSIGNED_GT
)
1372 estate_set_fuzzy_max(left_true_state
, hard_max
);
1376 if (get_hard_max(left
, &hard_max
))
1377 estate_set_fuzzy_max(right_true_state
, hard_max
);
1378 if (get_hard_max(right
, &hard_max
))
1379 estate_set_fuzzy_max(left_true_state
, hard_max
);
1383 if (get_hard_max(left
, &hard_max
)) {
1384 estate_set_hard_max(left_true_state
);
1385 estate_set_hard_max(left_false_state
);
1387 if (get_hard_max(right
, &hard_max
)) {
1388 estate_set_hard_max(right_true_state
);
1389 estate_set_hard_max(right_false_state
);
1392 if (left_postop
== SPECIAL_INCREMENT
) {
1393 left_true_state
= increment_state(left_true_state
);
1394 left_false_state
= increment_state(left_false_state
);
1396 if (left_postop
== SPECIAL_DECREMENT
) {
1397 left_true_state
= decrement_state(left_true_state
);
1398 left_false_state
= decrement_state(left_false_state
);
1400 if (right_postop
== SPECIAL_INCREMENT
) {
1401 right_true_state
= increment_state(right_true_state
);
1402 right_false_state
= increment_state(right_false_state
);
1404 if (right_postop
== SPECIAL_DECREMENT
) {
1405 right_true_state
= decrement_state(right_true_state
);
1406 right_false_state
= decrement_state(right_false_state
);
1409 if (estate_rl(left_true_state
) && estates_equiv(left_true_state
, left_false_state
)) {
1410 left_true_state
= NULL
;
1411 left_false_state
= NULL
;
1414 if (estate_rl(right_true_state
) && estates_equiv(right_true_state
, right_false_state
)) {
1415 right_true_state
= NULL
;
1416 right_false_state
= NULL
;
1419 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
1420 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
1423 static int is_simple_math(struct expression
*expr
)
1427 if (expr
->type
!= EXPR_BINOP
)
1438 static void move_known_values(struct expression
**left_p
, struct expression
**right_p
)
1440 struct expression
*left
= *left_p
;
1441 struct expression
*right
= *right_p
;
1444 if (get_implied_value(left
, &sval
)) {
1445 if (!is_simple_math(right
))
1447 if (get_implied_value(right
, &dummy
))
1449 if (right
->op
== '*') {
1452 if (!get_value(right
->right
, &divisor
))
1454 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1456 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1457 *right_p
= right
->left
;
1460 if (right
->op
== '+' && get_value(right
->left
, &sval
)) {
1461 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->left
);
1462 *right_p
= right
->right
;
1465 if (get_value(right
->right
, &sval
)) {
1466 *left_p
= binop_expression(left
, invert_op(right
->op
), right
->right
);
1467 *right_p
= right
->left
;
1472 if (get_implied_value(right
, &sval
)) {
1473 if (!is_simple_math(left
))
1475 if (get_implied_value(left
, &dummy
))
1477 if (left
->op
== '*') {
1480 if (!get_value(left
->right
, &divisor
))
1482 if (divisor
.value
== 0 && sval
.value
% divisor
.value
)
1484 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1485 *left_p
= left
->left
;
1488 if (left
->op
== '+' && get_value(left
->left
, &sval
)) {
1489 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->left
);
1490 *left_p
= left
->right
;
1494 if (get_value(left
->right
, &sval
)) {
1495 *right_p
= binop_expression(right
, invert_op(left
->op
), left
->right
);
1496 *left_p
= left
->left
;
1503 static int match_func_comparison(struct expression
*expr
)
1505 struct expression
*left
= strip_expr(expr
->left
);
1506 struct expression
*right
= strip_expr(expr
->right
);
1510 * fixme: think about this harder. We should always be trying to limit
1511 * the non-call side as well. If we can't determine the limitter does
1512 * that mean we aren't querying the database and are missing important
1516 if (left
->type
== EXPR_CALL
) {
1517 if (get_implied_value(left
, &sval
)) {
1518 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1521 function_comparison(left
, expr
->op
, right
);
1525 if (right
->type
== EXPR_CALL
) {
1526 if (get_implied_value(right
, &sval
)) {
1527 handle_comparison(get_type(expr
), left
, expr
->op
, right
);
1530 function_comparison(left
, expr
->op
, right
);
1537 /* Handle conditions like "if (foo + bar < foo) {" */
1538 static int handle_integer_overflow_test(struct expression
*expr
)
1540 struct expression
*left
, *right
;
1541 struct symbol
*type
;
1542 sval_t left_min
, right_min
, min
, max
;
1544 if (expr
->op
!= '<' && expr
->op
!= SPECIAL_UNSIGNED_LT
)
1547 left
= strip_parens(expr
->left
);
1548 right
= strip_parens(expr
->right
);
1550 if (left
->op
!= '+')
1553 type
= get_type(expr
);
1556 if (type_positive_bits(type
) == 32) {
1557 max
.type
= &uint_ctype
;
1558 max
.uvalue
= (unsigned int)-1;
1559 } else if (type_positive_bits(type
) == 64) {
1560 max
.type
= &ulong_ctype
;
1561 max
.value
= (unsigned long long)-1;
1566 if (!expr_equiv(left
->left
, right
) && !expr_equiv(left
->right
, right
))
1569 get_absolute_min(left
->left
, &left_min
);
1570 get_absolute_min(left
->right
, &right_min
);
1571 min
= sval_binop(left_min
, '+', right_min
);
1573 set_extra_chunk_true_false(left
, NULL
, alloc_estate_range(min
, max
));
1577 static void match_comparison(struct expression
*expr
)
1579 struct expression
*left_orig
= strip_parens(expr
->left
);
1580 struct expression
*right_orig
= strip_parens(expr
->right
);
1581 struct expression
*left
, *right
;
1582 struct expression
*prev
;
1583 struct symbol
*type
;
1585 if (match_func_comparison(expr
))
1588 type
= get_type(expr
);
1590 type
= &llong_ctype
;
1592 if (handle_integer_overflow_test(expr
))
1597 move_known_values(&left
, &right
);
1598 handle_comparison(type
, left
, expr
->op
, right
);
1600 prev
= get_assigned_expr(left_orig
);
1601 if (is_simple_math(prev
) && has_variable(prev
, left_orig
) == 0) {
1604 move_known_values(&left
, &right
);
1605 handle_comparison(type
, left
, expr
->op
, right
);
1608 prev
= get_assigned_expr(right_orig
);
1609 if (is_simple_math(prev
) && has_variable(prev
, right_orig
) == 0) {
1612 move_known_values(&left
, &right
);
1613 handle_comparison(type
, left
, expr
->op
, right
);
1617 static sval_t
get_high_mask(sval_t known
)
1625 for (i
= type_bits(known
.type
) - 1; i
>= 0; i
--) {
1626 if (known
.uvalue
& (1ULL << i
))
1627 ret
.uvalue
|= (1ULL << i
);
1635 static void handle_AND_condition(struct expression
*expr
)
1637 struct range_list
*orig_rl
;
1638 struct range_list
*true_rl
= NULL
;
1639 struct range_list
*false_rl
= NULL
;
1643 if (get_implied_value(expr
->left
, &known
)) {
1644 sval_t low_mask
= known
;
1647 if (known
.value
> 0) {
1648 bit
= ffsll(known
.value
) - 1;
1649 low_mask
.uvalue
= (1ULL << bit
) - 1;
1650 get_absolute_rl(expr
->right
, &orig_rl
);
1651 true_rl
= remove_range(orig_rl
, sval_type_val(known
.type
, 0), low_mask
);
1653 high_mask
= get_high_mask(known
);
1654 if (high_mask
.value
) {
1655 bit
= ffsll(high_mask
.value
) - 1;
1656 low_mask
.uvalue
= (1ULL << bit
) - 1;
1658 get_absolute_rl(expr
->left
, &orig_rl
);
1659 if (sval_is_negative(rl_min(orig_rl
)))
1660 orig_rl
= remove_range(orig_rl
, sval_type_min(known
.type
), sval_type_val(known
.type
, -1));
1661 false_rl
= remove_range(orig_rl
, low_mask
, sval_type_max(known
.type
));
1662 if (type_signed(high_mask
.type
) && type_unsigned(rl_type(false_rl
))) {
1663 false_rl
= remove_range(false_rl
,
1664 sval_type_val(rl_type(false_rl
), sval_type_max(known
.type
).uvalue
),
1665 sval_type_val(rl_type(false_rl
), -1));
1668 set_extra_expr_true_false(expr
->right
,
1669 true_rl
? alloc_estate_rl(true_rl
) : NULL
,
1670 false_rl
? alloc_estate_rl(false_rl
) : NULL
);
1675 if (get_implied_value(expr
->right
, &known
)) {
1676 sval_t low_mask
= known
;
1679 if (known
.value
> 0) {
1680 bit
= ffsll(known
.value
) - 1;
1681 low_mask
.uvalue
= (1ULL << bit
) - 1;
1682 get_absolute_rl(expr
->left
, &orig_rl
);
1683 true_rl
= remove_range(orig_rl
, sval_type_val(known
.type
, 0), low_mask
);
1685 high_mask
= get_high_mask(known
);
1686 if (high_mask
.value
) {
1687 bit
= ffsll(high_mask
.value
) - 1;
1688 low_mask
.uvalue
= (1ULL << bit
) - 1;
1690 get_absolute_rl(expr
->left
, &orig_rl
);
1691 if (sval_is_negative(rl_min(orig_rl
)))
1692 orig_rl
= remove_range(orig_rl
, sval_type_min(known
.type
), sval_type_val(known
.type
, -1));
1693 false_rl
= remove_range(orig_rl
, low_mask
, sval_type_max(known
.type
));
1694 if (type_signed(high_mask
.type
) && type_unsigned(rl_type(false_rl
))) {
1695 false_rl
= remove_range(false_rl
,
1696 sval_type_val(rl_type(false_rl
), sval_type_max(known
.type
).uvalue
),
1697 sval_type_val(rl_type(false_rl
), -1));
1700 set_extra_expr_true_false(expr
->left
,
1701 true_rl
? alloc_estate_rl(true_rl
) : NULL
,
1702 false_rl
? alloc_estate_rl(false_rl
) : NULL
);
1707 static void handle_MOD_condition(struct expression
*expr
)
1709 struct range_list
*orig_rl
;
1710 struct range_list
*true_rl
;
1711 struct range_list
*false_rl
= NULL
;
1717 if (!get_implied_value(expr
->right
, &right
) || right
.value
== 0)
1719 get_absolute_rl(expr
->left
, &orig_rl
);
1721 zero
.type
= rl_type(orig_rl
);
1723 /* We're basically dorking around the min and max here */
1724 true_rl
= remove_range(orig_rl
, zero
, zero
);
1725 if (!sval_is_max(rl_max(true_rl
)) &&
1726 !(rl_max(true_rl
).value
% right
.value
))
1727 true_rl
= remove_range(true_rl
, rl_max(true_rl
), rl_max(true_rl
));
1729 if (rl_equiv(true_rl
, orig_rl
))
1732 if (sval_is_positive(rl_min(orig_rl
)) &&
1733 (rl_max(orig_rl
).value
- rl_min(orig_rl
).value
) / right
.value
< 5) {
1737 add
= rl_min(orig_rl
);
1738 add
.value
+= right
.value
- (add
.value
% right
.value
);
1739 add
.value
-= right
.value
;
1741 for (i
= 0; i
< 5; i
++) {
1742 add
.value
+= right
.value
;
1743 if (add
.value
> rl_max(orig_rl
).value
)
1745 add_range(&false_rl
, add
, add
);
1748 if (rl_min(orig_rl
).uvalue
!= 0 &&
1749 rl_min(orig_rl
).uvalue
< right
.uvalue
) {
1750 sval_t chop
= right
;
1752 false_rl
= remove_range(orig_rl
, zero
, chop
);
1755 if (!sval_is_max(rl_max(orig_rl
)) &&
1756 (rl_max(orig_rl
).value
% right
.value
)) {
1757 sval_t chop
= rl_max(orig_rl
);
1758 chop
.value
-= chop
.value
% right
.value
;
1761 false_rl
= clone_rl(orig_rl
);
1762 false_rl
= remove_range(false_rl
, chop
, rl_max(orig_rl
));
1766 set_extra_expr_true_false(expr
->left
,
1767 true_rl
? alloc_estate_rl(true_rl
) : NULL
,
1768 false_rl
? alloc_estate_rl(false_rl
) : NULL
);
1771 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1772 void __extra_match_condition(struct expression
*expr
)
1774 struct smatch_state
*pre_state
;
1775 struct smatch_state
*true_state
;
1776 struct smatch_state
*false_state
;
1778 expr
= strip_expr(expr
);
1779 switch (expr
->type
) {
1781 function_comparison(expr
, SPECIAL_NOTEQUAL
, zero_expr());
1788 zero
= sval_blank(expr
);
1791 pre_state
= get_extra_state(expr
);
1792 true_state
= estate_filter_sval(pre_state
, zero
);
1793 if (possibly_true(expr
, SPECIAL_EQUAL
, zero_expr()))
1794 false_state
= alloc_estate_sval(zero
);
1796 false_state
= alloc_estate_empty();
1797 set_extra_expr_true_false(expr
, true_state
, false_state
);
1801 match_comparison(expr
);
1803 case EXPR_ASSIGNMENT
:
1804 __extra_match_condition(expr
->left
);
1807 if (expr
->op
== '&')
1808 handle_AND_condition(expr
);
1809 if (expr
->op
== '%')
1810 handle_MOD_condition(expr
);
1815 static void assume_indexes_are_valid(struct expression
*expr
)
1817 struct expression
*array_expr
;
1819 struct expression
*offset
;
1820 struct symbol
*offset_type
;
1821 struct range_list
*rl_before
;
1822 struct range_list
*rl_after
;
1823 struct range_list
*filter
= NULL
;
1826 expr
= strip_expr(expr
);
1827 if (!is_array(expr
))
1830 offset
= get_array_offset(expr
);
1831 offset_type
= get_type(offset
);
1832 if (offset_type
&& type_signed(offset_type
)) {
1833 filter
= alloc_rl(sval_type_min(offset_type
),
1834 sval_type_val(offset_type
, -1));
1837 array_expr
= get_array_base(expr
);
1838 array_size
= get_real_array_size(array_expr
);
1839 if (array_size
> 1) {
1840 size
= sval_type_val(offset_type
, array_size
);
1841 add_range(&filter
, size
, sval_type_max(offset_type
));
1846 get_absolute_rl(offset
, &rl_before
);
1847 rl_after
= rl_filter(rl_before
, filter
);
1848 if (rl_equiv(rl_before
, rl_after
))
1850 set_extra_expr_nomod(offset
, alloc_estate_rl(rl_after
));
1853 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1854 int implied_not_equal(struct expression
*expr
, long long val
)
1856 return !possibly_false(expr
, SPECIAL_NOTEQUAL
, value_expr(val
));
1859 int implied_not_equal_name_sym(char *name
, struct symbol
*sym
, long long val
)
1861 struct smatch_state
*estate
;
1863 estate
= get_state(SMATCH_EXTRA
, name
, sym
);
1866 if (!rl_has_sval(estate_rl(estate
), sval_type_val(estate_type(estate
), 0)))
1871 int parent_is_null_var_sym(const char *name
, struct symbol
*sym
)
1876 struct smatch_state
*state
;
1878 strncpy(buf
, name
, sizeof(buf
) - 1);
1879 buf
[sizeof(buf
) - 1] = '\0';
1882 while (*start
== '*') {
1884 state
= get_state(SMATCH_EXTRA
, start
, sym
);
1887 if (!estate_rl(state
))
1889 if (estate_min(state
).value
== 0 &&
1890 estate_max(state
).value
== 0)
1895 while (*start
== '&')
1898 while ((end
= strrchr(start
, '-'))) {
1900 state
= get_state(SMATCH_EXTRA
, start
, sym
);
1903 if (estate_min(state
).value
== 0 &&
1904 estate_max(state
).value
== 0)
1910 int parent_is_null(struct expression
*expr
)
1916 expr
= strip_expr(expr
);
1917 var
= expr_to_var_sym(expr
, &sym
);
1920 ret
= parent_is_null_var_sym(var
, sym
);
1926 static int param_used_callback(void *found
, int argc
, char **argv
, char **azColName
)
1932 static int filter_unused_kzalloc_info(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
1937 /* for function pointers assume everything is used */
1938 if (call
->fn
->type
!= EXPR_SYMBOL
)
1942 * kzalloc() information is treated as special because so there is just
1943 * a lot of stuff initialized to zero and it makes building the database
1944 * take hours and hours.
1946 * In theory, we should just remove this line and not pass any unused
1947 * information, but I'm not sure enough that this code works so I want
1948 * to hold off on that for now.
1950 if (!estate_get_single_value(sm
->state
, &sval
) || sval
.value
!= 0)
1953 run_sql(¶m_used_callback
, &found
,
1954 "select * from call_implies where %s and type = %d and parameter = %d and key = '%s';",
1955 get_static_filter(call
->fn
->symbol
), PARAM_USED
, param
, printed_name
);
1959 /* If the database is not built yet, then assume everything is used */
1960 run_sql(¶m_used_callback
, &found
,
1961 "select * from call_implies where %s and type = %d;",
1962 get_static_filter(call
->fn
->symbol
), PARAM_USED
);
1969 struct range_list
*intersect_with_real_abs_var_sym(const char *name
, struct symbol
*sym
, struct range_list
*start
)
1971 struct smatch_state
*state
;
1974 * Here is the difference between implied value and real absolute, say
1979 * Then you know that a is 0-255. That's real absolute. But you don't
1980 * know for sure that it actually goes up to 255. So it's not implied.
1981 * Implied indicates a degree of certainty.
1983 * But then say you cap "a" at 8. That means you know it goes up to
1984 * 8. So now the implied value is s32min-8. But you can combine it
1985 * with the real absolute to say that actually it's 0-8.
1987 * We are combining it here. But now that I think about it, this is
1988 * probably not the ideal place to combine it because it should proably
1989 * be done earlier. Oh well, this is an improvement on what was there
1990 * before so I'm going to commit this code.
1994 state
= get_real_absolute_state_var_sym(name
, sym
);
1995 if (!state
|| !estate_rl(state
))
1998 return rl_intersection(estate_rl(state
), start
);
2001 struct range_list
*intersect_with_real_abs_expr(struct expression
*expr
, struct range_list
*start
)
2003 struct smatch_state
*state
;
2005 state
= get_real_absolute_state(expr
);
2006 if (!state
|| !estate_rl(state
))
2009 return rl_intersection(estate_rl(state
), start
);
2012 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
2014 struct range_list
*rl
;
2016 if (estate_is_whole(sm
->state
))
2018 if (filter_unused_kzalloc_info(call
, param
, printed_name
, sm
))
2020 rl
= estate_rl(sm
->state
);
2021 rl
= intersect_with_real_abs_var_sym(sm
->name
, sm
->sym
, rl
);
2022 sql_insert_caller_info(call
, PARAM_VALUE
, param
, printed_name
, show_rl(rl
));
2023 if (estate_has_fuzzy_max(sm
->state
))
2024 sql_insert_caller_info(call
, FUZZY_MAX
, param
, printed_name
,
2025 sval_to_str(estate_get_fuzzy_max(sm
->state
)));
2028 static void returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
2030 struct symbol
*returned_sym
;
2031 struct sm_state
*sm
;
2032 const char *param_name
;
2036 returned_sym
= expr_to_sym(expr
);
2040 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), sm
) {
2041 if (!estate_rl(sm
->state
))
2043 if (returned_sym
!= sm
->sym
)
2046 param_name
= get_param_name(sm
);
2049 if (strcmp(param_name
, "$") == 0)
2051 compare_str
= name_sym_to_param_comparison(sm
->name
, sm
->sym
);
2052 if (!compare_str
&& estate_is_whole(sm
->state
))
2054 snprintf(buf
, sizeof(buf
), "%s%s", sm
->state
->name
, compare_str
?: "");
2056 sql_insert_return_states(return_id
, return_ranges
, PARAM_VALUE
,
2057 -1, param_name
, buf
);
2058 } END_FOR_EACH_SM(sm
);
2061 static void db_limited_before(void)
2063 unmatched_stree
= clone_stree(__get_cur_stree());
2066 static void db_limited_after(void)
2068 free_stree(&unmatched_stree
);
2071 static int rl_fits_in_type(struct range_list
*rl
, struct symbol
*type
)
2073 if (type_bits(rl_type(rl
)) <= type_bits(type
))
2075 if (sval_cmp(rl_max(rl
), sval_type_max(type
)) > 0)
2077 if (sval_is_negative(rl_min(rl
)) &&
2078 sval_cmp(rl_min(rl
), sval_type_min(type
)) < 0)
2083 static int basically_the_same(struct range_list
*orig
, struct range_list
*new)
2085 if (rl_equiv(orig
, new))
2089 * The whole range is essentially the same as 0,4096-27777777777 so
2090 * don't overwrite the implications just to store that.
2093 if (rl_type(orig
)->type
== SYM_PTR
&&
2094 is_whole_rl(orig
) &&
2095 rl_min(new).value
== 0 &&
2096 rl_max(new).value
== valid_ptr_max
)
2101 static void db_param_limit_filter(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
2103 struct expression
*arg
;
2106 struct var_sym_list
*vsl
= NULL
;
2107 struct sm_state
*sm
;
2108 struct symbol
*compare_type
, *var_type
;
2109 struct range_list
*rl
;
2110 struct range_list
*limit
;
2111 struct range_list
*new;
2113 while (expr
->type
== EXPR_ASSIGNMENT
)
2114 expr
= strip_expr(expr
->right
);
2115 if (expr
->type
!= EXPR_CALL
)
2118 arg
= get_argument_from_call_expr(expr
->args
, param
);
2122 name
= get_chunk_from_key(arg
, key
, &sym
, &vsl
);
2125 if (op
!= PARAM_LIMIT
&& !sym
)
2128 if (strcmp(key
, "$") == 0)
2129 compare_type
= get_arg_type(expr
->fn
, param
);
2131 compare_type
= get_member_type_from_key(arg
, key
);
2133 sm
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
2135 rl
= estate_rl(sm
->state
);
2137 rl
= alloc_whole_rl(compare_type
);
2139 if (op
== PARAM_LIMIT
&& !rl_fits_in_type(rl
, compare_type
))
2142 call_results_to_rl(expr
, compare_type
, value
, &limit
);
2143 new = rl_intersection(rl
, limit
);
2145 var_type
= get_member_type_from_key(arg
, key
);
2146 new = cast_rl(var_type
, new);
2148 /* We want to preserve the implications here */
2149 if (sm
&& basically_the_same(estate_rl(sm
->state
), new))
2150 __set_sm(sm
); /* FIXME: Is this really necessary? */
2153 struct symbol
*tmp_sym
;
2155 tmp_name
= map_long_to_short_name_sym(name
, sym
, &tmp_sym
);
2156 if (tmp_name
&& tmp_sym
) {
2162 if (op
== PARAM_LIMIT
)
2163 set_extra_nomod_vsl(name
, sym
, vsl
, alloc_estate_rl(new));
2165 set_extra_mod(name
, sym
, alloc_estate_rl(new));
2172 static void db_param_limit(struct expression
*expr
, int param
, char *key
, char *value
)
2174 db_param_limit_filter(expr
, param
, key
, value
, PARAM_LIMIT
);
2177 static void db_param_filter(struct expression
*expr
, int param
, char *key
, char *value
)
2179 db_param_limit_filter(expr
, param
, key
, value
, PARAM_FILTER
);
2182 static void db_param_add_set(struct expression
*expr
, int param
, char *key
, char *value
, enum info_type op
)
2184 struct expression
*arg
;
2185 char *name
, *tmp_name
;
2186 struct symbol
*sym
, *tmp_sym
;
2187 struct symbol
*type
;
2188 struct smatch_state
*state
;
2189 struct range_list
*new = NULL
;
2190 struct range_list
*added
= NULL
;
2192 while (expr
->type
== EXPR_ASSIGNMENT
)
2193 expr
= strip_expr(expr
->right
);
2194 if (expr
->type
!= EXPR_CALL
)
2197 arg
= get_argument_from_call_expr(expr
->args
, param
);
2200 type
= get_member_type_from_key(arg
, key
);
2201 name
= get_variable_from_key(arg
, key
, &sym
);
2205 state
= get_state(SMATCH_EXTRA
, name
, sym
);
2207 new = estate_rl(state
);
2209 call_results_to_rl(expr
, type
, value
, &added
);
2211 if (op
== PARAM_SET
)
2214 new = rl_union(new, added
);
2216 tmp_name
= map_long_to_short_name_sym_nostack(name
, sym
, &tmp_sym
);
2217 if (tmp_name
&& tmp_sym
) {
2222 set_extra_mod(name
, sym
, alloc_estate_rl(new));
2227 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
2229 db_param_add_set(expr
, param
, key
, value
, PARAM_ADD
);
2232 static void db_param_set(struct expression
*expr
, int param
, char *key
, char *value
)
2234 db_param_add_set(expr
, param
, key
, value
, PARAM_SET
);
2237 static void db_param_value(struct expression
*expr
, int param
, char *key
, char *value
)
2239 struct expression
*call
;
2242 struct symbol
*type
;
2243 struct range_list
*rl
= NULL
;
2249 while (call
->type
== EXPR_ASSIGNMENT
)
2250 call
= strip_expr(call
->right
);
2251 if (call
->type
!= EXPR_CALL
)
2254 type
= get_member_type_from_key(expr
->left
, key
);
2255 name
= get_variable_from_key(expr
->left
, key
, &sym
);
2259 call_results_to_rl(call
, type
, value
, &rl
);
2261 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
2266 static void match_call_info(struct expression
*expr
)
2268 struct smatch_state
*state
;
2269 struct range_list
*rl
= NULL
;
2270 struct expression
*arg
;
2271 struct symbol
*type
;
2274 FOR_EACH_PTR(expr
->args
, arg
) {
2275 type
= get_arg_type(expr
->fn
, i
);
2277 if (get_implied_rl(arg
, &rl
))
2278 rl
= cast_rl(type
, rl
);
2280 rl
= cast_rl(type
, alloc_whole_rl(get_type(arg
)));
2282 if (!is_whole_rl(rl
)) {
2283 rl
= intersect_with_real_abs_expr(arg
, rl
);
2284 sql_insert_caller_info(expr
, PARAM_VALUE
, i
, "$", show_rl(rl
));
2286 state
= get_state_expr(SMATCH_EXTRA
, arg
);
2287 if (estate_has_fuzzy_max(state
)) {
2288 sql_insert_caller_info(expr
, FUZZY_MAX
, i
, "$",
2289 sval_to_str(estate_get_fuzzy_max(state
)));
2292 } END_FOR_EACH_PTR(arg
);
2295 static void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2297 struct range_list
*rl
= NULL
;
2298 struct smatch_state
*state
;
2299 struct symbol
*type
;
2303 if (strcmp(key
, "*$") == 0)
2304 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
2305 else if (strncmp(key
, "$", 1) == 0)
2306 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
2310 type
= get_member_type_from_key(symbol_expression(sym
), key
);
2311 str_to_rl(type
, value
, &rl
);
2312 state
= alloc_estate_rl(rl
);
2313 if (estate_get_single_value(state
, &dummy
))
2314 estate_set_hard_max(state
);
2315 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
2318 static void set_param_hard_max(const char *name
, struct symbol
*sym
, char *key
, char *value
)
2320 struct range_list
*rl
= NULL
;
2321 struct smatch_state
*state
;
2322 struct symbol
*type
;
2326 if (strcmp(key
, "*$") == 0)
2327 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
2328 else if (strncmp(key
, "$", 1) == 0)
2329 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
2333 state
= get_state(SMATCH_EXTRA
, fullname
, sym
);
2336 type
= get_member_type_from_key(symbol_expression(sym
), key
);
2337 str_to_rl(type
, value
, &rl
);
2338 if (!rl_to_sval(rl
, &max
))
2340 estate_set_fuzzy_max(state
, max
);
2343 struct smatch_state
*get_extra_state(struct expression
*expr
)
2347 struct smatch_state
*ret
= NULL
;
2348 struct range_list
*rl
;
2350 if (is_pointer(expr
) && get_address_rl(expr
, &rl
))
2351 return alloc_estate_rl(rl
);
2353 name
= expr_to_known_chunk_sym(expr
, &sym
);
2357 ret
= get_state(SMATCH_EXTRA
, name
, sym
);
2363 void register_smatch_extra(int id
)
2367 add_merge_hook(my_id
, &merge_estates
);
2368 add_unmatched_state_hook(my_id
, &unmatched_state
);
2369 select_caller_info_hook(set_param_value
, PARAM_VALUE
);
2370 select_caller_info_hook(set_param_hard_max
, FUZZY_MAX
);
2371 select_return_states_before(&db_limited_before
);
2372 select_return_states_hook(PARAM_LIMIT
, &db_param_limit
);
2373 select_return_states_hook(PARAM_FILTER
, &db_param_filter
);
2374 select_return_states_hook(PARAM_ADD
, &db_param_add
);
2375 select_return_states_hook(PARAM_SET
, &db_param_set
);
2376 select_return_states_hook(PARAM_VALUE
, &db_param_value
);
2377 select_return_states_after(&db_limited_after
);
2380 static void match_link_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
2382 struct var_sym_list
*links
;
2383 struct var_sym
*tmp
;
2384 struct smatch_state
*state
;
2386 links
= sm
->state
->data
;
2388 FOR_EACH_PTR(links
, tmp
) {
2389 if (sm
->sym
== tmp
->sym
&&
2390 strcmp(sm
->name
, tmp
->var
) == 0)
2392 state
= get_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
);
2395 set_state(SMATCH_EXTRA
, tmp
->var
, tmp
->sym
, alloc_estate_whole(estate_type(state
)));
2396 } END_FOR_EACH_PTR(tmp
);
2397 set_state(link_id
, sm
->name
, sm
->sym
, &undefined
);
2400 void register_smatch_extra_links(int id
)
2405 void register_smatch_extra_late(int id
)
2407 add_merge_hook(link_id
, &merge_link_states
);
2408 add_modification_hook(link_id
, &match_link_modify
);
2409 add_hook(&match_dereferences
, DEREF_HOOK
);
2410 add_hook(&match_pointer_as_array
, OP_HOOK
);
2411 select_call_implies_hook(DEREFERENCE
, &set_param_dereferenced
);
2412 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
2413 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
2414 add_hook(&match_assign
, GLOBAL_ASSIGNMENT_HOOK
);
2415 add_hook(&unop_expr
, OP_HOOK
);
2416 add_hook(&asm_expr
, ASM_HOOK
);
2417 add_untracked_param_hook(&match_untracked_array
);
2419 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2420 add_member_info_callback(my_id
, struct_member_callback
);
2421 add_split_return_callback(&returned_struct_members
);
2423 add_hook(&assume_indexes_are_valid
, OP_HOOK
);