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.
21 #include "smatch_slist.h"
22 #include "smatch_extra.h"
26 static struct symbol
*cur_func
;
28 struct data_range whole_range
= {
33 static struct smatch_state
*alloc_extra_state_empty(void)
35 struct smatch_state
*state
;
36 struct data_info
*dinfo
;
38 dinfo
= __alloc_data_info(0);
39 dinfo
->type
= DATA_RANGE
;
40 dinfo
->value_ranges
= NULL
;
41 state
= __alloc_smatch_state(0);
46 static struct smatch_state
*alloc_extra_state_no_name(int val
)
48 struct smatch_state
*state
;
50 state
= __alloc_smatch_state(0);
51 state
->data
= (void *)alloc_dinfo_range(val
, val
);
55 /* We do this because ->value_ranges is a list */
56 struct smatch_state
*extra_undefined(void)
58 struct data_info
*dinfo
;
59 static struct smatch_state
*ret
;
60 static struct symbol
*prev_func
;
62 if (prev_func
== cur_func
)
66 dinfo
= alloc_dinfo_range(whole_range
.min
, whole_range
.max
);
67 ret
= __alloc_smatch_state(0);
68 ret
->name
= "unknown";
73 struct smatch_state
*alloc_extra_state(int val
)
75 struct smatch_state
*state
;
77 state
= alloc_extra_state_no_name(val
);
78 state
->name
= show_ranges(get_dinfo(state
)->value_ranges
);
82 struct smatch_state
*alloc_extra_state_range(long long min
, long long max
)
84 struct smatch_state
*state
;
86 if (min
== whole_range
.min
&& max
== whole_range
.max
)
87 return extra_undefined();
88 state
= __alloc_smatch_state(0);
89 state
->data
= (void *)alloc_dinfo_range(min
, max
);
90 state
->name
= show_ranges(get_dinfo(state
)->value_ranges
);
94 struct smatch_state
*alloc_extra_state_range_list(struct range_list
*rl
)
96 struct smatch_state
*state
;
98 state
= __alloc_smatch_state(0);
99 state
->data
= (void *)alloc_dinfo_range_list(rl
);
100 state
->name
= show_ranges(get_dinfo(state
)->value_ranges
);
104 struct data_info
*get_dinfo(struct smatch_state
*state
)
108 return (struct data_info
*)state
->data
;
112 struct smatch_state
*filter_range(struct smatch_state
*orig
,
113 long long filter_min
, long long filter_max
)
115 struct smatch_state
*ret
;
116 struct data_info
*orig_info
;
117 struct data_info
*ret_info
;
120 orig
= extra_undefined();
121 orig_info
= get_dinfo(orig
);
122 ret
= alloc_extra_state_empty();
123 ret_info
= get_dinfo(ret
);
124 ret_info
->value_ranges
= remove_range(orig_info
->value_ranges
, filter_min
, filter_max
);
125 ret
->name
= show_ranges(ret_info
->value_ranges
);
129 struct smatch_state
*add_filter(struct smatch_state
*orig
, long long num
)
131 return filter_range(orig
, num
, num
);
134 static struct smatch_state
*merge_func(const char *name
, struct symbol
*sym
,
135 struct smatch_state
*s1
,
136 struct smatch_state
*s2
)
138 struct data_info
*info1
= get_dinfo(s1
);
139 struct data_info
*info2
= get_dinfo(s2
);
140 struct data_info
*ret_info
;
141 struct smatch_state
*tmp
;
142 struct range_list
*value_ranges
;
144 value_ranges
= range_list_union(info1
->value_ranges
, info2
->value_ranges
);
145 tmp
= alloc_extra_state_empty();
146 ret_info
= get_dinfo(tmp
);
147 ret_info
->value_ranges
= value_ranges
;
148 tmp
->name
= show_ranges(ret_info
->value_ranges
);
152 struct sm_state
*__extra_pre_loop_hook_before(struct statement
*iterator_pre_statement
)
154 struct expression
*expr
;
157 struct sm_state
*ret
= NULL
;
159 if (!iterator_pre_statement
)
161 if (iterator_pre_statement
->type
!= STMT_EXPRESSION
)
163 expr
= iterator_pre_statement
->expression
;
164 if (expr
->type
!= EXPR_ASSIGNMENT
)
166 name
= get_variable_from_expr(expr
->left
, &sym
);
169 ret
= get_sm_state(my_id
, name
, sym
);
175 int __iterator_unchanged(struct sm_state
*sm
, struct statement
*iterator
)
177 struct expression
*iter_expr
;
184 if (iterator
->type
!= STMT_EXPRESSION
)
186 iter_expr
= iterator
->expression
;
187 if (iter_expr
->op
!= SPECIAL_INCREMENT
&& iter_expr
->op
!= SPECIAL_DECREMENT
)
189 name
= get_variable_from_expr(iter_expr
->unop
, &sym
);
192 if (get_sm_state(my_id
, name
, sym
) == sm
)
199 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
200 struct statement
*iterator
,
201 struct expression
*condition
)
203 struct expression
*iter_expr
;
208 struct smatch_state
*state
;
209 struct data_info
*dinfo
;
212 iter_expr
= iterator
->expression
;
214 if (condition
->type
!= EXPR_COMPARE
)
216 if (!get_value(condition
->left
, &value
)) {
217 if (!get_value(condition
->right
, &value
))
222 name
= get_variable_from_expr(condition
->left
, &sym
);
224 name
= get_variable_from_expr(condition
->right
, &sym
);
227 if (sym
!= sm
->sym
|| strcmp(name
, sm
->name
))
229 state
= get_state(my_id
, name
, sym
);
230 dinfo
= get_dinfo(state
);
231 min
= get_dinfo_min(dinfo
);
232 max
= get_dinfo_max(dinfo
);
233 if (iter_expr
->op
== SPECIAL_INCREMENT
&& min
!= whole_range
.min
&& max
== whole_range
.max
) {
234 set_state(my_id
, name
, sym
, alloc_extra_state(min
));
235 } else if (min
== whole_range
.min
&& max
!= whole_range
.max
) {
236 set_state(my_id
, name
, sym
, alloc_extra_state(max
));
243 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
245 return extra_undefined();
248 static void match_function_call(struct expression
*expr
)
250 struct expression
*tmp
;
255 FOR_EACH_PTR(expr
->args
, tmp
) {
256 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&') {
257 name
= get_variable_from_expr(tmp
->unop
, &sym
);
259 set_state(my_id
, name
, sym
, extra_undefined());
264 } END_FOR_EACH_PTR(tmp
);
267 static void match_assign(struct expression
*expr
)
269 struct expression
*left
;
270 struct expression
*right
;
275 long long min
= whole_range
.min
;
276 long long max
= whole_range
.max
;
278 struct range_list
*rl
= NULL
;
280 left
= strip_expr(expr
->left
);
281 name
= get_variable_from_expr(left
, &sym
);
284 right
= strip_expr(expr
->right
);
285 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
286 right
= strip_expr(right
->left
);
288 known
= get_implied_range_list(right
, &rl
);
289 if (expr
->op
== '=') {
291 set_state(my_id
, name
, sym
, alloc_extra_state_range_list(rl
));
293 set_state(my_id
, name
, sym
, extra_undefined());
297 known
= get_implied_value(right
, &value
);
298 if (expr
->op
== SPECIAL_ADD_ASSIGN
) {
299 if (get_implied_min(left
, &tmp
)) {
307 if (expr
->op
== SPECIAL_SUB_ASSIGN
) {
308 if (get_implied_max(left
, &tmp
)) {
316 set_state(my_id
, name
, sym
, alloc_extra_state_range(min
, max
));
321 static void unop_expr(struct expression
*expr
)
325 long long min
= whole_range
.min
;
326 long long max
= whole_range
.max
;
334 name
= get_variable_from_expr(expr
->unop
, &sym
);
337 if (expr
->op
== SPECIAL_INCREMENT
) {
338 if (get_implied_min(expr
->unop
, &val
))
341 if (expr
->op
== SPECIAL_DECREMENT
) {
342 if (get_implied_max(expr
->unop
, &val
))
345 set_state(my_id
, name
, sym
, alloc_extra_state_range(min
, max
));
350 static void match_declarations(struct symbol
*sym
)
356 name
= sym
->ident
->name
;
357 if (sym
->initializer
) {
358 if (get_value(sym
->initializer
, &val
))
359 set_state(my_id
, name
, sym
, alloc_extra_state(val
));
361 set_state(my_id
, name
, sym
, extra_undefined());
362 scoped_state(my_id
, name
, sym
);
364 set_state(my_id
, name
, sym
, extra_undefined());
365 scoped_state(my_id
, name
, sym
);
370 static void match_function_def(struct symbol
*sym
)
375 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
379 set_state(my_id
, arg
->ident
->name
, arg
, extra_undefined());
380 } END_FOR_EACH_PTR(arg
);
387 static int get_implied_value_helper(struct expression
*expr
, long long *val
, int what
)
389 struct smatch_state
*state
;
393 if (get_value(expr
, val
))
396 name
= get_variable_from_expr(expr
, &sym
);
399 state
= get_state(my_id
, name
, sym
);
401 if (!state
|| !state
->data
)
403 if (what
== VAL_SINGLE
)
404 return get_single_value_from_range(get_dinfo(state
), val
);
405 if (what
== VAL_MAX
) {
406 *val
= get_dinfo_max(get_dinfo(state
));
407 if (*val
== whole_range
.max
) /* this means just guessing */
411 *val
= get_dinfo_min(get_dinfo(state
));
412 if (*val
== whole_range
.min
)
417 int get_implied_single_val(struct expression
*expr
, long long *val
)
419 return get_implied_value_helper(expr
, val
, VAL_SINGLE
);
422 int get_implied_max(struct expression
*expr
, long long *val
)
424 return get_implied_value_helper(expr
, val
, VAL_MAX
);
427 int get_implied_min(struct expression
*expr
, long long *val
)
429 return get_implied_value_helper(expr
, val
, VAL_MIN
);
432 int get_implied_single_fuzzy_max(struct expression
*expr
, long long *max
)
435 struct sm_state
*tmp
;
437 if (get_implied_max(expr
, max
))
440 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
444 *max
= whole_range
.min
;
445 FOR_EACH_PTR(sm
->possible
, tmp
) {
448 new_min
= get_dinfo_min(get_dinfo(tmp
->state
));
451 } END_FOR_EACH_PTR(tmp
);
453 if (*max
> whole_range
.min
)
458 static int last_stmt_val(struct statement
*stmt
, long long *val
)
460 struct expression
*expr
;
465 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
466 if (stmt
->type
!= STMT_EXPRESSION
)
468 expr
= stmt
->expression
;
469 return get_value(expr
, val
);
472 static void match_comparison(struct expression
*expr
)
477 struct smatch_state
*one_state
;
478 struct smatch_state
*two_state
;
479 struct smatch_state
*orig
;
481 int comparison
= expr
->op
;
482 struct expression
*varies
= expr
->right
;
484 if (!get_value(expr
->left
, &fixed
)) {
485 if (!get_value(expr
->right
, &fixed
))
490 if (varies
->op
== SPECIAL_INCREMENT
|| varies
->op
== SPECIAL_DECREMENT
)
491 varies
= varies
->unop
;
492 if (varies
->type
== EXPR_CALL
) {
493 function_comparison(comparison
, varies
, fixed
, left
);
497 name
= get_variable_from_expr(varies
, &sym
);
501 orig
= get_state(my_id
, name
, sym
);
503 orig
= extra_undefined();
505 switch (comparison
) {
507 case SPECIAL_UNSIGNED_LT
:
508 one_state
= filter_range(orig
, whole_range
.min
, fixed
- 1);
509 two_state
= filter_range(orig
, fixed
, whole_range
.max
);
511 set_true_false_states(my_id
, name
, sym
, two_state
, one_state
);
513 set_true_false_states(my_id
, name
, sym
, one_state
, two_state
);
515 case SPECIAL_UNSIGNED_LTE
:
517 one_state
= filter_range(orig
, whole_range
.min
, fixed
);
518 two_state
= filter_range(orig
, fixed
+ 1, whole_range
.max
);
520 set_true_false_states(my_id
, name
, sym
, two_state
, one_state
);
522 set_true_false_states(my_id
, name
, sym
, one_state
, two_state
);
525 // todo. print a warning here for impossible conditions.
526 one_state
= alloc_extra_state(fixed
);
527 two_state
= filter_range(orig
, fixed
, fixed
);
528 set_true_false_states(my_id
, name
, sym
, one_state
, two_state
);
530 case SPECIAL_UNSIGNED_GTE
:
532 one_state
= filter_range(orig
, whole_range
.min
, fixed
- 1);
533 two_state
= filter_range(orig
, fixed
, whole_range
.max
);
535 set_true_false_states(my_id
, name
, sym
, one_state
, two_state
);
537 set_true_false_states(my_id
, name
, sym
, two_state
, one_state
);
540 case SPECIAL_UNSIGNED_GT
:
541 one_state
= filter_range(orig
, whole_range
.min
, fixed
);
542 two_state
= filter_range(orig
, fixed
+ 1, whole_range
.max
);
544 set_true_false_states(my_id
, name
, sym
, one_state
, two_state
);
546 set_true_false_states(my_id
, name
, sym
, two_state
, one_state
);
548 case SPECIAL_NOTEQUAL
:
549 one_state
= alloc_extra_state(fixed
);
550 two_state
= filter_range(orig
, fixed
, fixed
);
551 set_true_false_states(my_id
, name
, sym
, two_state
, one_state
);
554 sm_msg("unhandled comparison %d\n", comparison
);
562 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
563 void __extra_match_condition(struct expression
*expr
)
567 struct smatch_state
*pre_state
;
568 struct smatch_state
*true_state
;
569 struct smatch_state
*false_state
;
571 expr
= strip_expr(expr
);
572 switch (expr
->type
) {
574 function_comparison(SPECIAL_NOTEQUAL
, expr
, 0, 1);
579 name
= get_variable_from_expr(expr
, &sym
);
582 pre_state
= get_state(my_id
, name
, sym
);
583 true_state
= add_filter(pre_state
, 0);
584 false_state
= alloc_extra_state(0);
585 set_true_false_states(my_id
, name
, sym
, true_state
, false_state
);
589 match_comparison(expr
);
591 case EXPR_ASSIGNMENT
:
592 __extra_match_condition(expr
->left
);
597 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
598 int implied_not_equal(struct expression
*expr
, long long val
)
602 struct smatch_state
*state
;
605 name
= get_variable_from_expr(expr
, &sym
);
608 state
= get_state(my_id
, name
, sym
);
609 if (!state
|| !state
->data
)
611 ret
= !possibly_false(SPECIAL_NOTEQUAL
, get_dinfo(state
), val
, 1);
617 int known_condition_true(struct expression
*expr
)
624 if (get_value(expr
, &tmp
) && tmp
)
627 expr
= strip_expr(expr
);
628 switch (expr
->type
) {
630 if (expr
->op
== '!') {
631 if (known_condition_false(expr
->unop
))
642 int known_condition_false(struct expression
*expr
)
650 switch (expr
->type
) {
652 if (expr
->op
== '!') {
653 if (known_condition_true(expr
->unop
))
664 static int do_comparison_range(struct expression
*expr
)
668 struct smatch_state
*state
;
671 int poss_true
, poss_false
;
673 if (!get_value(expr
->left
, &value
)) {
674 if (!get_value(expr
->right
, &value
))
679 name
= get_variable_from_expr(expr
->left
, &sym
);
681 name
= get_variable_from_expr(expr
->right
, &sym
);
684 state
= get_state(SMATCH_EXTRA
, name
, sym
);
687 poss_true
= possibly_true(expr
->op
, get_dinfo(state
), value
, left
);
688 poss_false
= possibly_false(expr
->op
, get_dinfo(state
), value
, left
);
689 if (!poss_true
&& !poss_false
)
691 if (poss_true
&& !poss_false
)
693 if (!poss_true
&& poss_false
)
695 if (poss_true
&& poss_false
)
702 int implied_condition_true(struct expression
*expr
)
704 struct statement
*stmt
;
711 if (get_value(expr
, &tmp
) && tmp
)
714 expr
= strip_expr(expr
);
715 switch (expr
->type
) {
717 if (do_comparison_range(expr
) == 1)
721 if (expr
->op
== '!') {
722 if (implied_condition_false(expr
->unop
))
726 stmt
= get_block_thing(expr
);
727 if (last_stmt_val(stmt
, &val
) && val
== 1)
731 if (implied_not_equal(expr
, 0) == 1)
738 int implied_condition_false(struct expression
*expr
)
740 struct statement
*stmt
;
741 struct expression
*tmp
;
750 switch (expr
->type
) {
752 if (do_comparison_range(expr
) == 2)
755 if (expr
->op
== '!') {
756 if (implied_condition_true(expr
->unop
))
760 stmt
= get_block_thing(expr
);
761 if (last_stmt_val(stmt
, &val
) && val
== 0)
763 tmp
= strip_expr(expr
);
765 return implied_condition_false(tmp
);
768 if (get_implied_value(expr
, &val
) && val
== 0)
775 int get_implied_range_list(struct expression
*expr
, struct range_list
**rl
)
778 struct smatch_state
*state
;
780 expr
= strip_expr(expr
);
782 state
= get_state_expr(my_id
, expr
);
784 *rl
= clone_range_list(get_dinfo(state
)->value_ranges
);
788 if (get_implied_value(expr
, &val
)) {
790 add_range(rl
, val
, val
);
794 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '%') {
795 if (!get_implied_value(expr
->right
, &val
))
798 add_range(rl
, 0, val
- 1);
805 void register_smatch_extra(int id
)
808 add_merge_hook(my_id
, &merge_func
);
809 add_unmatched_state_hook(my_id
, &unmatched_state
);
810 add_hook(&unop_expr
, OP_HOOK
);
811 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
812 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
813 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
814 add_hook(&match_declarations
, DECLARATION_HOOK
);
816 if (option_project
== PROJ_KERNEL
) {
817 /* I don't know how to test for the ATTRIB_NORET attribute. :( */
818 add_function_hook("panic", &__match_nullify_path_hook
, NULL
);
819 add_function_hook("do_exit", &__match_nullify_path_hook
, NULL
);
820 add_function_hook("complete_and_exit", &__match_nullify_path_hook
, NULL
);
821 add_function_hook("__module_put_and_exit", &__match_nullify_path_hook
, NULL
);
822 add_function_hook("do_group_exit", &__match_nullify_path_hook
, NULL
);