2 * sparse/smatch_extra.c
4 * Copyright (C) 2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * smatch_extra.c is supposed to track the value of every variable.
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
28 struct string_list
*__ignored_macros
= NULL
;
29 static int in_warn_on_macro()
31 struct statement
*stmt
;
35 stmt
= get_current_statement();
36 macro
= get_macro_name(stmt
->pos
);
40 FOR_EACH_PTR(__ignored_macros
, tmp
) {
41 if (!strcmp(tmp
, macro
))
43 } END_FOR_EACH_PTR(tmp
);
47 struct sm_state
*set_extra_mod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
49 if (in_warn_on_macro())
51 remove_from_equiv(name
, sym
);
52 return set_state(SMATCH_EXTRA
, name
, sym
, state
);
55 struct sm_state
*set_extra_expr_mod(struct expression
*expr
, struct smatch_state
*state
)
57 if (in_warn_on_macro())
59 remove_from_equiv_expr(expr
);
60 return set_state_expr(SMATCH_EXTRA
, expr
, state
);
64 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
66 void set_extra_expr_nomod(struct expression
*expr
, struct smatch_state
*state
)
69 struct smatch_state
*orig_state
;
71 orig_state
= get_state_expr(SMATCH_EXTRA
, expr
);
73 if (!estate_related(orig_state
)) {
74 set_state_expr(SMATCH_EXTRA
, expr
, state
);
78 set_related(&state
, estate_related(orig_state
));
79 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
80 if (option_debug_related
)
81 sm_msg("updating related %s to %s", rel
->name
, state
->name
);
82 set_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
, state
);
83 } END_FOR_EACH_PTR(rel
);
86 static void set_extra_true_false(const char *name
, struct symbol
*sym
,
87 struct smatch_state
*true_state
,
88 struct smatch_state
*false_state
)
91 struct smatch_state
*orig_state
;
93 if (in_warn_on_macro())
96 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
98 if (!estate_related(orig_state
)) {
99 set_true_false_states(SMATCH_EXTRA
, name
, sym
, true_state
, false_state
);
104 set_related(&true_state
, estate_related(orig_state
));
106 set_related(&false_state
, estate_related(orig_state
));
108 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
109 set_true_false_states(SMATCH_EXTRA
, rel
->name
, rel
->sym
,
110 true_state
, false_state
);
111 } END_FOR_EACH_PTR(rel
);
114 static void set_extra_expr_true_false(struct expression
*expr
,
115 struct smatch_state
*true_state
,
116 struct smatch_state
*false_state
)
121 expr
= strip_expr(expr
);
122 name
= get_variable_from_expr(expr
, &sym
);
125 set_extra_true_false(name
, sym
, true_state
, false_state
);
130 struct smatch_state
*filter_range(struct smatch_state
*orig
,
131 sval_t filter_min
, sval_t filter_max
)
133 struct range_list_sval
*rl
;
136 orig
= extra_undefined(filter_min
.type
);
138 rl
= remove_range_sval(estate_ranges_sval(orig
), filter_min
, filter_max
);
139 return alloc_estate_range_list_sval(rl
);
142 struct smatch_state
*add_filter(struct smatch_state
*orig
, sval_t sval
)
144 return filter_range(orig
, sval
, sval
);
147 static struct smatch_state
*merge_func(struct smatch_state
*s1
, struct smatch_state
*s2
)
149 struct smatch_state
*tmp
;
150 struct range_list_sval
*value_ranges
;
151 struct related_list
*rlist
;
153 if (estates_equiv(s1
, s2
))
156 value_ranges
= range_list_union_sval(estate_ranges_sval(s1
), estate_ranges_sval(s2
));
157 tmp
= alloc_estate_range_list_sval(value_ranges
);
158 rlist
= get_shared_relations(estate_related(s1
), estate_related(s2
));
159 set_related(&tmp
, rlist
);
164 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
166 struct expression
*iter_var
;
167 struct expression
*condition
;
171 condition
= strip_expr(loop
->iterator_pre_condition
);
174 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
176 if (condition
->op
!= SPECIAL_DECREMENT
)
179 iter_var
= condition
->unop
;
180 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
183 if (sval_cmp_val(estate_min_sval(sm
->state
), 0) < 0)
185 start
= estate_max_sval(sm
->state
);
186 if (sval_cmp_val(start
, 0) <= 0)
188 if (!sval_is_max(start
))
191 if (condition
->type
== EXPR_PREOP
)
192 set_extra_expr_mod(iter_var
, alloc_estate_range_sval(ll_to_sval(1), start
));
193 if (condition
->type
== EXPR_POSTOP
)
194 set_extra_expr_mod(iter_var
, alloc_estate_range_sval(ll_to_sval(0), start
));
195 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
198 static struct sm_state
*handle_canonical_for_inc(struct expression
*iter_expr
,
199 struct expression
*condition
)
201 struct expression
*iter_var
;
205 iter_var
= iter_expr
->unop
;
206 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
209 if (!estate_get_single_value_sval(sm
->state
, &start
))
211 if (!get_implied_value_sval(condition
->right
, &end
))
212 end
= sval_type_max(get_type(iter_var
));
214 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
217 switch (condition
->op
) {
218 case SPECIAL_NOTEQUAL
:
220 if (!sval_is_max(end
))
228 if (sval_cmp(end
, start
) < 0)
230 set_extra_expr_mod(iter_var
, alloc_estate_range_sval(start
, end
));
231 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
234 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
235 struct expression
*condition
)
237 struct expression
*iter_var
;
241 iter_var
= iter_expr
->unop
;
242 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
245 if (!estate_get_single_value_sval(sm
->state
, &start
))
247 if (!get_implied_value_sval(condition
->right
, &end
))
248 end
= sval_type_min(get_type(iter_var
));
249 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
252 switch (condition
->op
) {
253 case SPECIAL_NOTEQUAL
:
255 if (!sval_is_min(end
) && !sval_is_max(end
))
263 if (sval_cmp(end
, start
) > 0)
265 set_extra_expr_mod(iter_var
, alloc_estate_range_sval(end
, start
));
266 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
269 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
271 struct expression
*iter_expr
;
272 struct expression
*condition
;
274 if (!loop
->iterator_post_statement
)
276 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
278 iter_expr
= loop
->iterator_post_statement
->expression
;
279 if (!loop
->iterator_pre_condition
)
281 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
283 condition
= loop
->iterator_pre_condition
;
285 if (iter_expr
->op
== SPECIAL_INCREMENT
)
286 return handle_canonical_for_inc(iter_expr
, condition
);
287 if (iter_expr
->op
== SPECIAL_DECREMENT
)
288 return handle_canonical_for_dec(iter_expr
, condition
);
292 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct state_list
**slist
)
294 struct sm_state
*ret
;
296 __push_fake_cur_slist();
297 if (!loop
->iterator_post_statement
)
298 ret
= handle_canonical_while_count_down(loop
);
300 ret
= handle_canonical_for_loops(loop
);
301 *slist
= __pop_fake_cur_slist();
305 int __iterator_unchanged(struct sm_state
*sm
)
309 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
314 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
318 /* paranoid checking. prolly not needed */
319 condition
= strip_expr(condition
);
322 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
324 if (condition
->op
!= SPECIAL_DECREMENT
)
326 after_value
= estate_min_sval(sm
->state
);
328 set_extra_mod(sm
->name
, sm
->sym
, alloc_estate_sval(after_value
));
331 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
332 struct statement
*iterator
,
333 struct expression
*condition
)
335 struct expression
*iter_expr
;
340 struct smatch_state
*state
;
344 while_count_down_after(sm
, condition
);
348 iter_expr
= iterator
->expression
;
350 if (condition
->type
!= EXPR_COMPARE
)
352 if (!get_value_sval(condition
->left
, &value
)) {
353 if (!get_value_sval(condition
->right
, &value
))
358 name
= get_variable_from_expr(condition
->left
, &sym
);
360 name
= get_variable_from_expr(condition
->right
, &sym
);
363 if (sym
!= sm
->sym
|| strcmp(name
, sm
->name
))
365 state
= get_state(my_id
, name
, sym
);
366 min
= estate_min_sval(state
);
367 max
= estate_max_sval(state
);
368 if (iter_expr
->op
== SPECIAL_INCREMENT
&&
369 !sval_is_min(min
) && sval_is_max(max
))
370 set_extra_mod(name
, sym
, alloc_estate_sval(min
));
371 else if (sval_is_min(min
) && !sval_is_max(max
))
372 set_extra_mod(name
, sym
, alloc_estate_sval(max
));
379 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
381 return extra_undefined(estate_type(sm
->state
));
384 static void match_function_call(struct expression
*expr
)
386 struct expression
*arg
;
387 struct expression
*tmp
;
389 FOR_EACH_PTR(expr
->args
, arg
) {
390 tmp
= strip_expr(arg
);
391 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&') {
392 remove_from_equiv_expr(tmp
->unop
);
393 set_state_expr(SMATCH_EXTRA
, tmp
->unop
, extra_undefined(get_type(tmp
->unop
)));
395 } END_FOR_EACH_PTR(arg
);
398 static void match_assign(struct expression
*expr
)
400 struct range_list_sval
*rl
= NULL
;
401 struct expression
*left
;
402 struct expression
*right
;
403 struct symbol
*right_sym
;
409 sval_t right_min
, right_max
;
412 if (__is_condition_assign(expr
))
414 left
= strip_expr(expr
->left
);
415 name
= get_variable_from_expr(left
, &sym
);
418 right
= strip_parens(expr
->right
);
419 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
420 right
= strip_parens(right
->left
);
422 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
423 return; /* these are handled in match_call_assign() */
425 right_name
= get_variable_from_expr(right
, &right_sym
);
426 if (expr
->op
== '=' && right_name
&& right_sym
&&
427 types_equiv(get_type(expr
->left
), get_type(expr
->right
))) {
428 set_equiv(left
, right
);
432 right_min
= sval_type_min(get_type(expr
->right
));
433 right_max
= sval_type_max(get_type(expr
->right
));
435 known
= get_implied_value_sval(right
, &value
);
438 if (get_implied_range_list_sval(right
, &rl
)) {
439 rl
= cast_rl(rl
, get_type(expr
->left
));
440 set_extra_mod(name
, sym
, alloc_estate_range_list_sval(rl
));
446 case SPECIAL_ADD_ASSIGN
:
447 if (get_implied_min_sval(left
, &tmp
)) {
449 right_min
= sval_binop(tmp
, '+', value
);
453 if (!inside_loop() && known
&& get_implied_max_sval(left
, &tmp
))
454 right_max
= sval_binop(tmp
, '+', value
);
456 case SPECIAL_SUB_ASSIGN
:
457 if (get_implied_max_sval(left
, &tmp
)) {
459 right_max
= sval_binop(tmp
, '-', value
);
463 if (!inside_loop() && known
&& get_implied_min_sval(left
, &tmp
))
464 right_min
= sval_binop(tmp
, '-', value
);
467 if (!sval_is_min(right_min
) || !sval_is_max(right_max
))
468 rl
= cast_rl(alloc_range_list_sval(right_min
, right_max
), get_type(expr
->left
));
470 rl
= whole_range_list_sval(get_type(expr
->left
));
471 set_extra_mod(name
, sym
, alloc_estate_range_list_sval(rl
));
473 free_string(right_name
);
477 static void reset_struct_members(struct sm_state
*sm
)
479 set_extra_mod(sm
->name
, sm
->sym
, extra_undefined(estate_type(sm
->state
)));
482 static struct smatch_state
*increment_state(struct smatch_state
*state
)
484 sval_t min
= estate_min_sval(state
);
485 sval_t max
= estate_max_sval(state
);
488 max
= sval_type_max(max
.type
);
490 if (!sval_is_min(min
))
492 if (!sval_is_max(max
))
494 return alloc_estate_range_sval(min
, max
);
497 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
499 sval_t min
= estate_min_sval(state
);
500 sval_t max
= estate_max_sval(state
);
503 min
= sval_type_min(min
.type
);
505 if (!sval_is_min(min
))
507 if (!sval_is_max(max
))
509 return alloc_estate_range_sval(min
, max
);
512 static void unop_expr(struct expression
*expr
)
514 struct smatch_state
*state
;
516 if (expr
->smatch_flags
& Handled
)
520 case SPECIAL_INCREMENT
:
521 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
522 state
= increment_state(state
);
523 set_extra_expr_mod(expr
->unop
, state
);
525 case SPECIAL_DECREMENT
:
526 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
527 state
= decrement_state(state
);
528 set_extra_expr_mod(expr
->unop
, state
);
535 static void asm_expr(struct statement
*stmt
)
538 struct expression
*expr
;
541 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
543 case 0: /* identifier */
544 case 1: /* constraint */
547 case 2: /* expression */
549 set_extra_expr_mod(expr
, extra_undefined(get_type(expr
)));
552 } END_FOR_EACH_PTR(expr
);
555 static void delete_state_tracker(struct tracker
*t
)
557 remove_from_equiv(t
->name
, t
->sym
);
558 delete_state(t
->owner
, t
->name
, t
->sym
);
561 static void scoped_state_extra(const char *name
, struct symbol
*sym
)
565 t
= alloc_tracker(SMATCH_EXTRA
, name
, sym
);
566 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
569 static void match_declarations(struct symbol
*sym
)
574 name
= sym
->ident
->name
;
575 if (!sym
->initializer
) {
576 set_state(SMATCH_EXTRA
, name
, sym
, extra_undefined(get_real_base_type(sym
)));
577 scoped_state_extra(name
, sym
);
582 static void check_dereference(struct expression
*expr
)
584 set_extra_expr_nomod(expr
, alloc_estate_range_sval(valid_ptr_min_sval
, valid_ptr_max_sval
));
587 static void match_dereferences(struct expression
*expr
)
589 if (expr
->type
!= EXPR_PREOP
)
591 check_dereference(expr
->unop
);
594 static void match_pointer_as_array(struct expression
*expr
)
598 check_dereference(expr
->unop
->left
);
601 static void set_param_dereferenced(struct expression
*arg
, char *unused
)
603 check_dereference(arg
);
606 static void match_function_def(struct symbol
*sym
)
610 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
613 set_state(my_id
, arg
->ident
->name
, arg
, extra_undefined(get_real_base_type(arg
)));
614 } END_FOR_EACH_PTR(arg
);
617 static int match_func_comparison(struct expression
*expr
)
619 struct expression
*left
= strip_expr(expr
->left
);
620 struct expression
*right
= strip_expr(expr
->right
);
623 if (left
->type
== EXPR_CALL
) {
624 if (!get_implied_value_sval(right
, &sval
))
626 function_comparison(expr
->op
, left
, sval
, 1);
630 if (right
->type
== EXPR_CALL
) {
631 if (!get_implied_value_sval(left
, &sval
))
633 function_comparison(expr
->op
, right
, sval
, 0);
640 static sval_t
add_one(sval_t sval
)
646 static sval_t
sub_one(sval_t sval
)
652 static void match_comparison(struct expression
*expr
)
654 struct expression
*left
= strip_expr(expr
->left
);
655 struct expression
*right
= strip_expr(expr
->right
);
657 struct range_list_sval
*left_orig
;
658 struct range_list_sval
*left_true
;
659 struct range_list_sval
*left_false
;
660 struct range_list_sval
*right_orig
;
661 struct range_list_sval
*right_true
;
662 struct range_list_sval
*right_false
;
663 struct smatch_state
*left_true_state
;
664 struct smatch_state
*left_false_state
;
665 struct smatch_state
*right_true_state
;
666 struct smatch_state
*right_false_state
;
669 int right_postop
= 0;
671 if (match_func_comparison(expr
))
674 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
675 if (left
->type
== EXPR_POSTOP
) {
676 left
->smatch_flags
|= Handled
;
677 left_postop
= left
->op
;
679 left
= strip_expr(left
->unop
);
682 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
683 if (right
->type
== EXPR_POSTOP
) {
684 right
->smatch_flags
|= Handled
;
685 right_postop
= right
->op
;
687 right
= strip_expr(right
->unop
);
690 type
= get_type(expr
);
692 sm_msg("debug: failed to get type for '%s'", get_variable_from_expr_complex(expr
, NULL
));
695 min
= sval_type_min(type
);
696 max
= sval_type_max(type
);
697 if (get_implied_range_list_sval(left
, &left_orig
))
698 left_orig
= cast_rl(left_orig
, type
);
700 left_orig
= alloc_range_list_sval(min
, max
);
702 if (get_implied_range_list_sval(right
, &right_orig
))
703 right_orig
= cast_rl(right_orig
, type
);
705 right_orig
= alloc_range_list_sval(min
, max
);
707 left_true
= clone_range_list_sval(left_orig
);
708 left_false
= clone_range_list_sval(left_orig
);
709 right_true
= clone_range_list_sval(right_orig
);
710 right_false
= clone_range_list_sval(right_orig
);
714 case SPECIAL_UNSIGNED_LT
:
715 if (!sval_is_max(rl_max_sval(right_orig
))) {
716 left_true
= remove_range_sval(left_orig
, rl_max_sval(right_orig
), max
);
718 if (!sval_is_min(rl_min_sval(right_orig
))) {
719 left_false
= remove_range_sval(left_orig
, min
, sub_one(rl_min_sval(right_orig
)));
722 if (!sval_is_min(rl_min_sval(left_orig
)))
723 right_true
= remove_range_sval(right_orig
, min
, rl_min_sval(left_orig
));
724 if (!sval_is_max(rl_max_sval(left_orig
)))
725 right_false
= remove_range_sval(right_orig
, add_one(rl_max_sval(left_orig
)), max
);
727 case SPECIAL_UNSIGNED_LTE
:
729 if (!sval_is_max(rl_max_sval(right_orig
)))
730 left_true
= remove_range_sval(left_orig
, add_one(rl_max_sval(right_orig
)), max
);
731 if (!sval_is_min(rl_min_sval(right_orig
)))
732 left_false
= remove_range_sval(left_orig
, min
, rl_min_sval(right_orig
));
734 if (!sval_is_min(rl_min_sval(left_orig
)))
735 right_true
= remove_range_sval(right_orig
, min
, sub_one(rl_min_sval(left_orig
)));
736 if (!sval_is_max(rl_max_sval(left_orig
)))
737 right_false
= remove_range_sval(right_orig
, rl_max_sval(left_orig
), max
);
740 if (!sval_is_max(rl_max_sval(right_orig
))) {
741 left_true
= remove_range_sval(left_true
, add_one(rl_max_sval(right_orig
)), max
);
743 if (!sval_is_min(rl_min_sval(right_orig
))) {
744 left_true
= remove_range_sval(left_true
, min
, sub_one(rl_min_sval(right_orig
)));
746 if (sval_cmp(rl_min_sval(right_orig
), rl_max_sval(right_orig
)) == 0)
747 left_false
= remove_range_sval(left_orig
, rl_min_sval(right_orig
), rl_min_sval(right_orig
));
749 if (!sval_is_max(rl_max_sval(left_orig
)))
750 right_true
= remove_range_sval(right_true
, add_one(rl_max_sval(left_orig
)), max
);
751 if (!sval_is_min(rl_min_sval(left_orig
)))
752 right_true
= remove_range_sval(right_true
, min
, sub_one(rl_min_sval(left_orig
)));
753 if (sval_cmp(rl_min_sval(left_orig
), rl_max_sval(left_orig
)) == 0)
754 right_false
= remove_range_sval(right_orig
, rl_min_sval(left_orig
), rl_min_sval(left_orig
));
756 case SPECIAL_UNSIGNED_GTE
:
758 if (!sval_is_min(rl_min_sval(right_orig
)))
759 left_true
= remove_range_sval(left_orig
, min
, sub_one(rl_min_sval(right_orig
)));
760 if (!sval_is_max(rl_max_sval(right_orig
)))
761 left_false
= remove_range_sval(left_orig
, rl_max_sval(right_orig
), max
);
763 if (!sval_is_max(rl_max_sval(left_orig
)))
764 right_true
= remove_range_sval(right_orig
, add_one(rl_max_sval(left_orig
)), max
);
765 if (!sval_is_min(rl_min_sval(left_orig
)))
766 right_false
= remove_range_sval(right_orig
, min
, rl_min_sval(left_orig
));
769 case SPECIAL_UNSIGNED_GT
:
770 if (!sval_is_min(rl_min_sval(right_orig
)))
771 left_true
= remove_range_sval(left_orig
, min
, rl_min_sval(right_orig
));
772 if (!sval_is_max(rl_max_sval(right_orig
)))
773 left_false
= remove_range_sval(left_orig
, add_one(rl_max_sval(right_orig
)), max
);
775 if (!sval_is_max(rl_max_sval(left_orig
)))
776 right_true
= remove_range_sval(right_orig
, rl_max_sval(left_orig
), max
);
777 if (!sval_is_min(rl_min_sval(left_orig
)))
778 right_false
= remove_range_sval(right_orig
, min
, sub_one(rl_min_sval(left_orig
)));
780 case SPECIAL_NOTEQUAL
:
781 if (!sval_is_max(rl_max_sval(right_orig
)))
782 left_false
= remove_range_sval(left_false
, add_one(rl_max_sval(right_orig
)), max
);
783 if (!sval_is_min(rl_min_sval(right_orig
)))
784 left_false
= remove_range_sval(left_false
, min
, sub_one(rl_min_sval(right_orig
)));
785 if (sval_cmp(rl_min_sval(right_orig
), rl_max_sval(right_orig
)) == 0)
786 left_true
= remove_range_sval(left_orig
, rl_min_sval(right_orig
), rl_min_sval(right_orig
));
788 if (!sval_is_max(rl_max_sval(left_orig
)))
789 right_false
= remove_range_sval(right_false
, add_one(rl_max_sval(left_orig
)), max
);
790 if (!sval_is_min(rl_min_sval(left_orig
)))
791 right_false
= remove_range_sval(right_false
, min
, sub_one(rl_min_sval(left_orig
)));
792 if (sval_cmp(rl_min_sval(left_orig
), rl_max_sval(left_orig
)) == 0)
793 right_true
= remove_range_sval(right_orig
, rl_min_sval(left_orig
), rl_min_sval(left_orig
));
799 left_true_state
= alloc_estate_range_list_sval(cast_rl(left_true
, get_type(left
)));
800 left_false_state
= alloc_estate_range_list_sval(cast_rl(left_false
, get_type(left
)));
801 right_true_state
= alloc_estate_range_list_sval(cast_rl(right_true
, get_type(right
)));
802 right_false_state
= alloc_estate_range_list_sval(cast_rl(right_false
, get_type(right
)));
804 if (left_postop
== SPECIAL_INCREMENT
) {
805 left_true_state
= increment_state(left_true_state
);
806 left_false_state
= increment_state(left_false_state
);
808 if (left_postop
== SPECIAL_DECREMENT
) {
809 left_true_state
= decrement_state(left_true_state
);
810 left_false_state
= decrement_state(left_false_state
);
812 if (right_postop
== SPECIAL_INCREMENT
) {
813 right_true_state
= increment_state(right_true_state
);
814 right_false_state
= increment_state(right_false_state
);
816 if (right_postop
== SPECIAL_DECREMENT
) {
817 right_true_state
= decrement_state(right_true_state
);
818 right_false_state
= decrement_state(right_false_state
);
821 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
822 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
825 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
826 void __extra_match_condition(struct expression
*expr
)
830 struct smatch_state
*pre_state
;
831 struct smatch_state
*true_state
;
832 struct smatch_state
*false_state
;
834 expr
= strip_expr(expr
);
835 switch (expr
->type
) {
837 function_comparison(SPECIAL_NOTEQUAL
, expr
, ll_to_sval(0), 1);
842 name
= get_variable_from_expr(expr
, &sym
);
845 pre_state
= get_state(my_id
, name
, sym
);
846 true_state
= add_filter(pre_state
, ll_to_sval(0));
847 if (possibly_true(expr
, SPECIAL_EQUAL
, zero_expr()))
848 false_state
= alloc_estate_sval(ll_to_sval(0));
850 false_state
= alloc_estate_empty();
851 set_extra_true_false(name
, sym
, true_state
, false_state
);
855 match_comparison(expr
);
857 case EXPR_ASSIGNMENT
:
858 __extra_match_condition(expr
->left
);
863 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
864 int implied_not_equal(struct expression
*expr
, long long val
)
866 return !possibly_false(expr
, SPECIAL_NOTEQUAL
, value_expr(val
));
869 int get_implied_range_list_sval(struct expression
*expr
, struct range_list_sval
**rl
)
872 struct smatch_state
*state
;
877 expr
= strip_parens(expr
);
881 state
= get_state_expr(my_id
, expr
);
883 *rl
= clone_range_list_sval(estate_ranges_sval(state
));
887 if (expr
->type
== EXPR_CALL
) {
888 if (get_implied_return_sval(expr
, rl
))
890 *rl
= db_return_vals(expr
);
894 if (get_implied_value_sval(expr
, &sval
)) {
895 add_range_sval(rl
, sval
, sval
);
899 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '%') {
900 if (!get_implied_value_sval(expr
->right
, &sval
))
902 add_range_sval(rl
, ll_to_sval(0), ll_to_sval(sval
.value
- 1));
906 if (!get_implied_min_sval(expr
, &min
))
908 if (!get_implied_max_sval(expr
, &max
))
911 *rl
= alloc_range_list_sval(min
, max
);
914 if (is_whole_range_rl_sval(*rl
))
919 int is_whole_range(struct smatch_state
*state
)
921 return is_whole_range_rl_sval(estate_ranges_sval(state
));
924 static void struct_member_callback(char *fn
, char *global_static
, int param
, char *printed_name
, struct smatch_state
*state
)
926 if (is_whole_range(state
))
928 sm_msg("info: passes param_value '%s' %d '%s' %s %s", fn
, param
, printed_name
, state
->name
, global_static
);
931 static void match_call_info(struct expression
*expr
)
933 struct range_list_sval
*rl
= NULL
;
934 struct expression
*arg
;
938 name
= get_fnptr_name(expr
->fn
);
942 FOR_EACH_PTR(expr
->args
, arg
) {
943 if (get_implied_range_list_sval(arg
, &rl
) && !is_whole_range_rl_sval(rl
)) {
944 sm_msg("info: passes param_value '%s' %d '$$' %s %s",
945 name
, i
, show_ranges_sval(rl
),
946 is_static(expr
->fn
) ? "static" : "global");
949 } END_FOR_EACH_PTR(arg
);
954 static void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
956 struct range_list_sval
*rl
= NULL
;
957 struct smatch_state
*state
;
960 if (strncmp(key
, "$$", 2))
963 snprintf(fullname
, 256, "%s%s", name
, key
+ 2);
964 get_value_ranges_sval(value
, &rl
);
965 state
= alloc_estate_range_list_sval(rl
);
966 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
969 static void match_call_assign(struct expression
*expr
)
974 /* if we have a db set up this gets set in smatch_function_hooks.c */
976 set_extra_expr_mod(expr
->left
, extra_undefined(get_type(expr
->left
)));
979 void register_smatch_extra(int id
)
983 alloc_estate_undefined();
984 add_merge_hook(my_id
, &merge_func
);
985 add_unmatched_state_hook(my_id
, &unmatched_state
);
986 add_hook(&unop_expr
, OP_HOOK
);
987 add_hook(&asm_expr
, ASM_HOOK
);
988 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
989 add_hook(&match_declarations
, DECLARATION_HOOK
);
991 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
992 add_member_info_callback(my_id
, struct_member_callback
);
994 add_definition_db_callback(set_param_value
, PARAM_VALUE
);
997 void register_smatch_extra_late(int id
)
999 add_hook(&match_dereferences
, DEREF_HOOK
);
1000 add_hook(&match_pointer_as_array
, OP_HOOK
);
1001 add_db_fn_call_callback(DEREFERENCE
, &set_param_dereferenced
);
1002 add_indirect_modification_hook(SMATCH_EXTRA
, reset_struct_members
);
1003 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
1004 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
1005 add_hook(&match_call_assign
, CALL_ASSIGNMENT_HOOK
);