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.
19 #include "smatch_slist.h"
20 #include "smatch_extra.h"
24 static struct symbol
*cur_func
;
26 struct data_range whole_range
= {
31 static struct smatch_state
*alloc_extra_state_empty()
33 struct smatch_state
*state
;
34 struct data_info
*dinfo
;
36 dinfo
= __alloc_data_info(0);
37 dinfo
->type
= DATA_RANGE
;
38 dinfo
->value_ranges
= NULL
;
39 state
= __alloc_smatch_state(0);
44 static struct smatch_state
*alloc_extra_state_no_name(int val
)
46 struct smatch_state
*state
;
48 state
= __alloc_smatch_state(0);
49 state
->data
= (void *)alloc_dinfo_range(val
, val
);
53 /* We do this because ->value_ranges is a list */
54 struct smatch_state
*extra_undefined()
56 struct data_info
*dinfo
;
57 static struct smatch_state
*ret
;
58 static struct symbol
*prev_func
;
60 if (prev_func
== cur_func
)
64 dinfo
= alloc_dinfo_range(whole_range
.min
, whole_range
.max
);
65 ret
= __alloc_smatch_state(0);
66 ret
->name
= "unknown";
71 struct smatch_state
*alloc_extra_state(int val
)
73 struct smatch_state
*state
;
75 state
= alloc_extra_state_no_name(val
);
76 state
->name
= show_ranges(((struct data_info
*)state
->data
)->value_ranges
);
80 struct smatch_state
*alloc_extra_state_range(long long min
, long long max
)
82 struct smatch_state
*state
;
84 if (min
== whole_range
.min
&& max
== whole_range
.max
)
85 return extra_undefined();
86 state
= __alloc_smatch_state(0);
87 state
->data
= (void *)alloc_dinfo_range(min
, max
);
88 state
->name
= show_ranges(((struct data_info
*)state
->data
)->value_ranges
);
92 struct smatch_state
*alloc_extra_state_range_list(struct range_list
*rl
)
94 struct smatch_state
*state
;
96 state
= __alloc_smatch_state(0);
97 state
->data
= (void *)alloc_dinfo_range_list(rl
);
98 state
->name
= show_ranges(((struct data_info
*)state
->data
)->value_ranges
);
102 struct smatch_state
*filter_range(struct smatch_state
*orig
,
103 long long filter_min
, long long filter_max
)
105 struct smatch_state
*ret
;
106 struct data_info
*orig_info
;
107 struct data_info
*ret_info
;
110 orig
= extra_undefined();
111 orig_info
= (struct data_info
*)orig
->data
;
112 ret
= alloc_extra_state_empty();
113 ret_info
= (struct data_info
*)ret
->data
;
114 ret_info
->value_ranges
= remove_range(orig_info
->value_ranges
, filter_min
, filter_max
);
115 ret
->name
= show_ranges(ret_info
->value_ranges
);
119 struct smatch_state
*add_filter(struct smatch_state
*orig
, long long num
)
121 return filter_range(orig
, num
, num
);
124 static struct smatch_state
*merge_func(const char *name
, struct symbol
*sym
,
125 struct smatch_state
*s1
,
126 struct smatch_state
*s2
)
128 struct data_info
*info1
= (struct data_info
*)s1
->data
;
129 struct data_info
*info2
= (struct data_info
*)s2
->data
;
130 struct data_info
*ret_info
;
131 struct smatch_state
*tmp
;
132 struct range_list
*value_ranges
;
134 value_ranges
= range_list_union(info1
->value_ranges
, info2
->value_ranges
);
135 tmp
= alloc_extra_state_empty();
136 ret_info
= (struct data_info
*)tmp
->data
;
137 ret_info
->value_ranges
= value_ranges
;
138 tmp
->name
= show_ranges(ret_info
->value_ranges
);
142 struct sm_state
*__extra_pre_loop_hook_before(struct statement
*iterator_pre_statement
)
144 struct expression
*expr
;
147 struct sm_state
*ret
= NULL
;
149 if (!iterator_pre_statement
)
151 if (iterator_pre_statement
->type
!= STMT_EXPRESSION
)
153 expr
= iterator_pre_statement
->expression
;
154 if (expr
->type
!= EXPR_ASSIGNMENT
)
156 name
= get_variable_from_expr(expr
->left
, &sym
);
159 ret
= get_sm_state(my_id
, name
, sym
);
165 int __iterator_unchanged(struct sm_state
*sm
, struct statement
*iterator
)
167 struct expression
*iter_expr
;
174 if (iterator
->type
!= STMT_EXPRESSION
)
176 iter_expr
= iterator
->expression
;
177 if (iter_expr
->op
!= SPECIAL_INCREMENT
&& iter_expr
->op
!= SPECIAL_DECREMENT
)
179 name
= get_variable_from_expr(iter_expr
->unop
, &sym
);
182 if (get_sm_state(my_id
, name
, sym
) == sm
)
189 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
190 struct statement
*iterator
,
191 struct expression
*condition
)
193 struct expression
*iter_expr
;
198 struct smatch_state
*state
;
199 struct data_info
*dinfo
;
202 iter_expr
= iterator
->expression
;
204 if (condition
->type
!= EXPR_COMPARE
)
206 if (!get_value(condition
->left
, &value
)) {
207 if (!get_value(condition
->right
, &value
))
212 name
= get_variable_from_expr(condition
->left
, &sym
);
214 name
= get_variable_from_expr(condition
->right
, &sym
);
217 if (sym
!= sm
->sym
|| strcmp(name
, sm
->name
))
219 state
= get_state(my_id
, name
, sym
);
220 dinfo
= (struct data_info
*)state
->data
;
221 min
= get_dinfo_min(dinfo
);
222 max
= get_dinfo_max(dinfo
);
223 if (iter_expr
->op
== SPECIAL_INCREMENT
&& min
!= whole_range
.min
&& max
== whole_range
.max
) {
224 set_state(my_id
, name
, sym
, alloc_extra_state(min
));
225 } else if (min
== whole_range
.min
&& max
!= whole_range
.max
) {
226 set_state(my_id
, name
, sym
, alloc_extra_state(max
));
233 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
235 return extra_undefined();
238 static void match_function_call(struct expression
*expr
)
240 struct expression
*tmp
;
245 FOR_EACH_PTR(expr
->args
, tmp
) {
246 if (tmp
->op
== '&') {
247 name
= get_variable_from_expr(tmp
->unop
, &sym
);
249 set_state(my_id
, name
, sym
, extra_undefined());
254 } END_FOR_EACH_PTR(tmp
);
257 static void match_assign(struct expression
*expr
)
259 struct expression
*left
;
264 long long min
= whole_range
.min
;
265 long long max
= whole_range
.max
;
268 left
= strip_expr(expr
->left
);
269 name
= get_variable_from_expr(left
, &sym
);
272 known
= get_value(expr
->right
, &value
);
274 if (expr
->op
== '=') {
276 set_state(my_id
, name
, sym
, alloc_extra_state(value
));
278 set_state(my_id
, name
, sym
, extra_undefined());
281 if (expr
->op
== SPECIAL_ADD_ASSIGN
) {
282 if (get_implied_min(expr
->unop
, &tmp
)) {
290 if (expr
->op
== SPECIAL_SUB_ASSIGN
) {
291 if (get_implied_max(expr
->unop
, &tmp
)) {
299 set_state(my_id
, name
, sym
, alloc_extra_state_range(min
, max
));
304 static void undef_expr(struct expression
*expr
)
308 long long min
= whole_range
.min
;
309 long long max
= whole_range
.max
;
317 name
= get_variable_from_expr(expr
->unop
, &sym
);
320 if (expr
->op
== SPECIAL_INCREMENT
) {
321 if (get_implied_min(expr
->unop
, &val
))
324 if (expr
->op
== SPECIAL_DECREMENT
) {
325 if (get_implied_max(expr
->unop
, &val
))
328 set_state(my_id
, name
, sym
, alloc_extra_state_range(min
, max
));
333 static void match_declarations(struct symbol
*sym
)
339 name
= sym
->ident
->name
;
340 if (sym
->initializer
) {
341 if (get_value(sym
->initializer
, &val
))
342 set_state(my_id
, name
, sym
, alloc_extra_state(val
));
344 set_state(my_id
, name
, sym
, extra_undefined());
345 scoped_state(name
, my_id
, sym
);
347 set_state(my_id
, name
, sym
, extra_undefined());
348 scoped_state(name
, my_id
, sym
);
353 static void match_function_def(struct symbol
*sym
)
358 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
362 set_state(my_id
, arg
->ident
->name
, arg
, extra_undefined());
363 } END_FOR_EACH_PTR(arg
);
370 static int get_implied_value_helper(struct expression
*expr
, long long *val
, int what
)
372 struct smatch_state
*state
;
376 if (get_value(expr
, val
))
379 name
= get_variable_from_expr(expr
, &sym
);
382 state
= get_state(my_id
, name
, sym
);
384 if (!state
|| !state
->data
)
386 if (what
== VAL_SINGLE
)
387 return get_single_value_from_range((struct data_info
*)state
->data
, val
);
388 if (what
== VAL_MAX
) {
389 *val
= get_dinfo_max((struct data_info
*)state
->data
);
390 if (*val
== whole_range
.max
) /* this means just guessing */
394 *val
= get_dinfo_min((struct data_info
*)state
->data
);
395 if (*val
== whole_range
.min
)
400 int get_implied_single_val(struct expression
*expr
, long long *val
)
402 return get_implied_value_helper(expr
, val
, VAL_SINGLE
);
405 int get_implied_max(struct expression
*expr
, long long *val
)
407 return get_implied_value_helper(expr
, val
, VAL_MAX
);
410 int get_implied_min(struct expression
*expr
, long long *val
)
412 return get_implied_value_helper(expr
, val
, VAL_MIN
);
415 static int last_stmt_val(struct statement
*stmt
, long long *val
)
417 struct expression
*expr
;
422 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
423 if (stmt
->type
!= STMT_EXPRESSION
)
425 expr
= stmt
->expression
;
426 return get_value(expr
, val
);
429 static void match_comparison(struct expression
*expr
)
434 struct smatch_state
*one_state
;
435 struct smatch_state
*two_state
;
436 struct smatch_state
*orig
;
438 int comparison
= expr
->op
;
439 struct expression
*varies
= expr
->right
;
441 if (!get_value(expr
->left
, &fixed
)) {
442 if (!get_value(expr
->right
, &fixed
))
447 if (varies
->type
== EXPR_PREOP
|| varies
->type
== EXPR_POSTOP
) {
448 varies
= varies
->unop
;
450 if (varies
->type
== EXPR_CALL
) {
451 function_comparison(comparison
, varies
, fixed
, left
);
454 name
= get_variable_from_expr(varies
, &sym
);
458 orig
= get_state(my_id
, name
, sym
);
460 orig
= extra_undefined();
464 case SPECIAL_UNSIGNED_LT
:
465 one_state
= filter_range(orig
, whole_range
.min
, fixed
- 1);
466 two_state
= filter_range(orig
, fixed
, whole_range
.max
);
468 set_true_false_states(my_id
, name
, sym
, two_state
, one_state
);
470 set_true_false_states(my_id
, name
, sym
, one_state
, two_state
);
472 case SPECIAL_UNSIGNED_LTE
:
474 one_state
= filter_range(orig
, whole_range
.min
, fixed
);
475 two_state
= filter_range(orig
, fixed
+ 1, whole_range
.max
);
477 set_true_false_states(my_id
, name
, sym
, two_state
, one_state
);
479 set_true_false_states(my_id
, name
, sym
, one_state
, two_state
);
482 // todo. print a warning here for impossible conditions.
483 one_state
= alloc_extra_state(fixed
);
484 two_state
= filter_range(orig
, fixed
, fixed
);
485 set_true_false_states(my_id
, name
, sym
, one_state
, two_state
);
487 case SPECIAL_UNSIGNED_GTE
:
489 one_state
= filter_range(orig
, whole_range
.min
, fixed
- 1);
490 two_state
= filter_range(orig
, fixed
, whole_range
.max
);
492 set_true_false_states(my_id
, name
, sym
, one_state
, two_state
);
494 set_true_false_states(my_id
, name
, sym
, two_state
, one_state
);
497 case SPECIAL_UNSIGNED_GT
:
498 one_state
= filter_range(orig
, whole_range
.min
, fixed
);
499 two_state
= filter_range(orig
, fixed
+ 1, whole_range
.max
);
501 set_true_false_states(my_id
, name
, sym
, one_state
, two_state
);
503 set_true_false_states(my_id
, name
, sym
, two_state
, one_state
);
505 case SPECIAL_NOTEQUAL
:
506 one_state
= alloc_extra_state(fixed
);
507 two_state
= filter_range(orig
, fixed
, fixed
);
508 set_true_false_states(my_id
, name
, sym
, two_state
, one_state
);
511 sm_msg("unhandled comparison %d\n", comparison
);
519 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
520 void __extra_match_condition(struct expression
*expr
)
524 struct smatch_state
*pre_state
;
525 struct smatch_state
*true_state
;
526 struct smatch_state
*false_state
;
528 expr
= strip_expr(expr
);
531 function_comparison(SPECIAL_NOTEQUAL
, expr
, 0, 1);
536 name
= get_variable_from_expr(expr
, &sym
);
539 pre_state
= get_state(my_id
, name
, sym
);
540 true_state
= add_filter(pre_state
, 0);
541 false_state
= alloc_extra_state(0);
542 set_true_false_states(my_id
, name
, sym
, true_state
, false_state
);
546 match_comparison(expr
);
548 case EXPR_ASSIGNMENT
:
549 __extra_match_condition(expr
->right
);
550 __extra_match_condition(expr
->left
);
555 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
556 int implied_not_equal(struct expression
*expr
, long long val
)
560 struct smatch_state
*state
;
563 name
= get_variable_from_expr(expr
, &sym
);
566 state
= get_state(my_id
, name
, sym
);
567 if (!state
|| !state
->data
)
569 ret
= !possibly_false(SPECIAL_NOTEQUAL
, (struct data_info
*)state
->data
, val
, 1);
575 int known_condition_true(struct expression
*expr
)
582 if (get_value(expr
, &tmp
) && tmp
)
585 expr
= strip_expr(expr
);
588 if (expr
->op
== '!') {
589 if (known_condition_false(expr
->unop
))
600 int known_condition_false(struct expression
*expr
)
610 if (expr
->op
== '!') {
611 if (known_condition_true(expr
->unop
))
622 static int do_comparison_range(struct expression
*expr
)
626 struct smatch_state
*state
;
629 int poss_true
, poss_false
;
631 if (!get_value(expr
->left
, &value
)) {
632 if (!get_value(expr
->right
, &value
))
637 name
= get_variable_from_expr(expr
->left
, &sym
);
639 name
= get_variable_from_expr(expr
->right
, &sym
);
642 state
= get_state(SMATCH_EXTRA
, name
, sym
);
645 poss_true
= possibly_true(expr
->op
, (struct data_info
*)state
->data
, value
, left
);
646 poss_false
= possibly_false(expr
->op
, (struct data_info
*)state
->data
, value
, left
);
647 if (!poss_true
&& !poss_false
)
649 if (poss_true
&& !poss_false
)
651 if (!poss_true
&& poss_false
)
653 if (poss_true
&& poss_false
)
660 int implied_condition_true(struct expression
*expr
)
662 struct statement
*stmt
;
669 if (get_value(expr
, &tmp
) && tmp
)
672 expr
= strip_expr(expr
);
675 if (do_comparison_range(expr
) == 1)
679 if (expr
->op
== '!') {
680 if (implied_condition_false(expr
->unop
))
684 stmt
= get_block_thing(expr
);
685 if (last_stmt_val(stmt
, &val
) && val
== 1)
689 if (implied_not_equal(expr
, 0) == 1)
696 int implied_condition_false(struct expression
*expr
)
698 struct statement
*stmt
;
699 struct expression
*tmp
;
710 if (do_comparison_range(expr
) == 2)
713 if (expr
->op
== '!') {
714 if (implied_condition_true(expr
->unop
))
718 stmt
= get_block_thing(expr
);
719 if (last_stmt_val(stmt
, &val
) && val
== 0)
721 tmp
= strip_expr(expr
);
723 return implied_condition_false(tmp
);
726 if (get_implied_value(expr
, &val
) && val
== 0)
733 void register_smatch_extra(int id
)
736 add_merge_hook(my_id
, &merge_func
);
737 add_unmatched_state_hook(my_id
, &unmatched_state
);
738 add_hook(&undef_expr
, OP_HOOK
);
739 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
740 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
741 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
742 add_hook(&match_declarations
, DECLARATION_HOOK
);
744 if (option_project
== PROJ_KERNEL
) {
745 /* I don't know how to test for the ATTRIB_NORET attribute. :( */
746 add_function_hook("panic", &__match_nullify_path_hook
, NULL
);
747 add_function_hook("do_exit", &__match_nullify_path_hook
, NULL
);
748 add_function_hook("complete_and_exit", &__match_nullify_path_hook
, NULL
);
749 add_function_hook("__module_put_and_exit", &__match_nullify_path_hook
, NULL
);
750 add_function_hook("do_group_exit", &__match_nullify_path_hook
, NULL
);