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
->type
== EXPR_PREOP
&& 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
;
260 struct expression
*right
;
265 long long min
= whole_range
.min
;
266 long long max
= whole_range
.max
;
269 left
= strip_expr(expr
->left
);
270 name
= get_variable_from_expr(left
, &sym
);
273 right
= strip_expr(expr
->right
);
274 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
275 right
= strip_expr(right
->left
);
276 known
= get_implied_value(right
, &value
);
278 if (expr
->op
== '=') {
280 set_state(my_id
, name
, sym
, alloc_extra_state(value
));
282 set_state(my_id
, name
, sym
, extra_undefined());
285 if (expr
->op
== SPECIAL_ADD_ASSIGN
) {
286 if (get_implied_min(left
, &tmp
)) {
294 if (expr
->op
== SPECIAL_SUB_ASSIGN
) {
295 if (get_implied_max(left
, &tmp
)) {
303 set_state(my_id
, name
, sym
, alloc_extra_state_range(min
, max
));
308 static void unop_expr(struct expression
*expr
)
312 long long min
= whole_range
.min
;
313 long long max
= whole_range
.max
;
321 name
= get_variable_from_expr(expr
->unop
, &sym
);
324 if (expr
->op
== SPECIAL_INCREMENT
) {
325 if (get_implied_min(expr
->unop
, &val
))
328 if (expr
->op
== SPECIAL_DECREMENT
) {
329 if (get_implied_max(expr
->unop
, &val
))
332 set_state(my_id
, name
, sym
, alloc_extra_state_range(min
, max
));
337 static void match_declarations(struct symbol
*sym
)
343 name
= sym
->ident
->name
;
344 if (sym
->initializer
) {
345 if (get_value(sym
->initializer
, &val
))
346 set_state(my_id
, name
, sym
, alloc_extra_state(val
));
348 set_state(my_id
, name
, sym
, extra_undefined());
349 scoped_state(my_id
, name
, sym
);
351 set_state(my_id
, name
, sym
, extra_undefined());
352 scoped_state(my_id
, name
, sym
);
357 static void match_function_def(struct symbol
*sym
)
362 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
366 set_state(my_id
, arg
->ident
->name
, arg
, extra_undefined());
367 } END_FOR_EACH_PTR(arg
);
374 static int get_implied_value_helper(struct expression
*expr
, long long *val
, int what
)
376 struct smatch_state
*state
;
380 if (get_value(expr
, val
))
383 name
= get_variable_from_expr(expr
, &sym
);
386 state
= get_state(my_id
, name
, sym
);
388 if (!state
|| !state
->data
)
390 if (what
== VAL_SINGLE
)
391 return get_single_value_from_range((struct data_info
*)state
->data
, val
);
392 if (what
== VAL_MAX
) {
393 *val
= get_dinfo_max((struct data_info
*)state
->data
);
394 if (*val
== whole_range
.max
) /* this means just guessing */
398 *val
= get_dinfo_min((struct data_info
*)state
->data
);
399 if (*val
== whole_range
.min
)
404 int get_implied_single_val(struct expression
*expr
, long long *val
)
406 return get_implied_value_helper(expr
, val
, VAL_SINGLE
);
409 int get_implied_max(struct expression
*expr
, long long *val
)
411 return get_implied_value_helper(expr
, val
, VAL_MAX
);
414 int get_implied_min(struct expression
*expr
, long long *val
)
416 return get_implied_value_helper(expr
, val
, VAL_MIN
);
419 static int last_stmt_val(struct statement
*stmt
, long long *val
)
421 struct expression
*expr
;
426 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
427 if (stmt
->type
!= STMT_EXPRESSION
)
429 expr
= stmt
->expression
;
430 return get_value(expr
, val
);
433 static void match_comparison(struct expression
*expr
)
438 struct smatch_state
*one_state
;
439 struct smatch_state
*two_state
;
440 struct smatch_state
*orig
;
442 int comparison
= expr
->op
;
443 struct expression
*varies
= expr
->right
;
445 if (!get_value(expr
->left
, &fixed
)) {
446 if (!get_value(expr
->right
, &fixed
))
451 if (varies
->op
== SPECIAL_INCREMENT
|| varies
->op
== SPECIAL_DECREMENT
)
452 varies
= varies
->unop
;
453 if (varies
->type
== EXPR_CALL
) {
454 function_comparison(comparison
, varies
, fixed
, left
);
458 name
= get_variable_from_expr(varies
, &sym
);
462 orig
= get_state(my_id
, name
, sym
);
464 orig
= extra_undefined();
468 case SPECIAL_UNSIGNED_LT
:
469 one_state
= filter_range(orig
, whole_range
.min
, fixed
- 1);
470 two_state
= filter_range(orig
, fixed
, whole_range
.max
);
472 set_true_false_states(my_id
, name
, sym
, two_state
, one_state
);
474 set_true_false_states(my_id
, name
, sym
, one_state
, two_state
);
476 case SPECIAL_UNSIGNED_LTE
:
478 one_state
= filter_range(orig
, whole_range
.min
, fixed
);
479 two_state
= filter_range(orig
, fixed
+ 1, whole_range
.max
);
481 set_true_false_states(my_id
, name
, sym
, two_state
, one_state
);
483 set_true_false_states(my_id
, name
, sym
, one_state
, two_state
);
486 // todo. print a warning here for impossible conditions.
487 one_state
= alloc_extra_state(fixed
);
488 two_state
= filter_range(orig
, fixed
, fixed
);
489 set_true_false_states(my_id
, name
, sym
, one_state
, two_state
);
491 case SPECIAL_UNSIGNED_GTE
:
493 one_state
= filter_range(orig
, whole_range
.min
, fixed
- 1);
494 two_state
= filter_range(orig
, fixed
, whole_range
.max
);
496 set_true_false_states(my_id
, name
, sym
, one_state
, two_state
);
498 set_true_false_states(my_id
, name
, sym
, two_state
, one_state
);
501 case SPECIAL_UNSIGNED_GT
:
502 one_state
= filter_range(orig
, whole_range
.min
, fixed
);
503 two_state
= filter_range(orig
, fixed
+ 1, whole_range
.max
);
505 set_true_false_states(my_id
, name
, sym
, one_state
, two_state
);
507 set_true_false_states(my_id
, name
, sym
, two_state
, one_state
);
509 case SPECIAL_NOTEQUAL
:
510 one_state
= alloc_extra_state(fixed
);
511 two_state
= filter_range(orig
, fixed
, fixed
);
512 set_true_false_states(my_id
, name
, sym
, two_state
, one_state
);
515 sm_msg("unhandled comparison %d\n", comparison
);
523 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
524 void __extra_match_condition(struct expression
*expr
)
528 struct smatch_state
*pre_state
;
529 struct smatch_state
*true_state
;
530 struct smatch_state
*false_state
;
532 expr
= strip_expr(expr
);
535 function_comparison(SPECIAL_NOTEQUAL
, expr
, 0, 1);
540 name
= get_variable_from_expr(expr
, &sym
);
543 pre_state
= get_state(my_id
, name
, sym
);
544 true_state
= add_filter(pre_state
, 0);
545 false_state
= alloc_extra_state(0);
546 set_true_false_states(my_id
, name
, sym
, true_state
, false_state
);
550 match_comparison(expr
);
552 case EXPR_ASSIGNMENT
:
553 __extra_match_condition(expr
->left
);
558 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
559 int implied_not_equal(struct expression
*expr
, long long val
)
563 struct smatch_state
*state
;
566 name
= get_variable_from_expr(expr
, &sym
);
569 state
= get_state(my_id
, name
, sym
);
570 if (!state
|| !state
->data
)
572 ret
= !possibly_false(SPECIAL_NOTEQUAL
, (struct data_info
*)state
->data
, val
, 1);
578 int known_condition_true(struct expression
*expr
)
585 if (get_value(expr
, &tmp
) && tmp
)
588 expr
= strip_expr(expr
);
591 if (expr
->op
== '!') {
592 if (known_condition_false(expr
->unop
))
603 int known_condition_false(struct expression
*expr
)
613 if (expr
->op
== '!') {
614 if (known_condition_true(expr
->unop
))
625 static int do_comparison_range(struct expression
*expr
)
629 struct smatch_state
*state
;
632 int poss_true
, poss_false
;
634 if (!get_value(expr
->left
, &value
)) {
635 if (!get_value(expr
->right
, &value
))
640 name
= get_variable_from_expr(expr
->left
, &sym
);
642 name
= get_variable_from_expr(expr
->right
, &sym
);
645 state
= get_state(SMATCH_EXTRA
, name
, sym
);
648 poss_true
= possibly_true(expr
->op
, (struct data_info
*)state
->data
, value
, left
);
649 poss_false
= possibly_false(expr
->op
, (struct data_info
*)state
->data
, value
, left
);
650 if (!poss_true
&& !poss_false
)
652 if (poss_true
&& !poss_false
)
654 if (!poss_true
&& poss_false
)
656 if (poss_true
&& poss_false
)
663 int implied_condition_true(struct expression
*expr
)
665 struct statement
*stmt
;
672 if (get_value(expr
, &tmp
) && tmp
)
675 expr
= strip_expr(expr
);
678 if (do_comparison_range(expr
) == 1)
682 if (expr
->op
== '!') {
683 if (implied_condition_false(expr
->unop
))
687 stmt
= get_block_thing(expr
);
688 if (last_stmt_val(stmt
, &val
) && val
== 1)
692 if (implied_not_equal(expr
, 0) == 1)
699 int implied_condition_false(struct expression
*expr
)
701 struct statement
*stmt
;
702 struct expression
*tmp
;
713 if (do_comparison_range(expr
) == 2)
716 if (expr
->op
== '!') {
717 if (implied_condition_true(expr
->unop
))
721 stmt
= get_block_thing(expr
);
722 if (last_stmt_val(stmt
, &val
) && val
== 0)
724 tmp
= strip_expr(expr
);
726 return implied_condition_false(tmp
);
729 if (get_implied_value(expr
, &val
) && val
== 0)
736 void register_smatch_extra(int id
)
739 add_merge_hook(my_id
, &merge_func
);
740 add_unmatched_state_hook(my_id
, &unmatched_state
);
741 add_hook(&unop_expr
, OP_HOOK
);
742 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
743 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
744 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
745 add_hook(&match_declarations
, DECLARATION_HOOK
);
747 if (option_project
== PROJ_KERNEL
) {
748 /* I don't know how to test for the ATTRIB_NORET attribute. :( */
749 add_function_hook("panic", &__match_nullify_path_hook
, NULL
);
750 add_function_hook("do_exit", &__match_nullify_path_hook
, NULL
);
751 add_function_hook("complete_and_exit", &__match_nullify_path_hook
, NULL
);
752 add_function_hook("__module_put_and_exit", &__match_nullify_path_hook
, NULL
);
753 add_function_hook("do_group_exit", &__match_nullify_path_hook
, NULL
);