2 * sparse/smatch_extra.c
4 * Copyright (C) 2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
13 #include "smatch_slist.h"
14 #include "smatch_extra.h"
18 struct data_info unknown_num
= {
24 static struct smatch_state extra_undefined
= {
29 static struct smatch_state
*alloc_extra_state_no_name(int val
)
31 struct smatch_state
*state
;
33 state
= __alloc_smatch_state(0);
34 state
->data
= (void *)alloc_data_info(val
);
38 struct smatch_state
*alloc_extra_state(int val
)
40 struct smatch_state
*state
;
44 return &extra_undefined
;
46 state
= alloc_extra_state_no_name(val
);
47 snprintf(name
, 20, "%d", val
);
48 state
->name
= alloc_sname(name
);
52 struct smatch_state
*add_filter(struct smatch_state
*orig
, long long num
)
55 struct smatch_state
*ret
;
56 struct data_info
*orig_info
= NULL
;
57 struct data_info
*ret_info
;
61 orig_info
= (struct data_info
*)orig
->data
;
62 ret
= alloc_extra_state_no_name(UNDEFINED
);
63 snprintf(buf
, 254, "%s f%lld", orig
?orig
->name
:"any", num
);
65 ret
->name
= alloc_sname(buf
);
66 ret_info
= (struct data_info
*)ret
->data
;
67 ret_info
->values
= NULL
;
69 ret_info
->values
= clone_num_list(orig_info
->values
);
70 ret_info
->filter
= NULL
;
72 ret_info
->filter
= clone_num_list(orig_info
->filter
);
73 add_num(&ret_info
->filter
, num
);
77 static struct smatch_state
*merge_func(const char *name
, struct symbol
*sym
,
78 struct smatch_state
*s1
,
79 struct smatch_state
*s2
)
81 struct data_info
*info1
= (struct data_info
*)s1
->data
;
82 struct data_info
*info2
= (struct data_info
*)s2
->data
;
83 struct data_info
*ret_info
;
84 struct smatch_state
*tmp
;
86 tmp
= alloc_extra_state_no_name(UNDEFINED
);
87 tmp
->name
= "extra_merged";
88 ret_info
= (struct data_info
*)tmp
->data
;
90 ret_info
->values
= num_list_union(info1
->values
, info2
->values
);
91 ret_info
->filter
= num_list_intersection(info1
->values
, info2
->values
);
95 struct sm_state
*__extra_merge(struct sm_state
*one
, struct state_list
*slist1
,
96 struct sm_state
*two
, struct state_list
*slist2
)
98 struct data_info
*info1
;
99 struct data_info
*info2
;
101 if (!one
->state
->data
|| !two
->state
->data
) {
102 smatch_msg("internal error in smatch extra '%s = %s or %s'",
103 one
->name
, show_state(one
->state
),
104 show_state(two
->state
));
105 return alloc_state(one
->name
, one
->owner
, one
->sym
,
109 info1
= (struct data_info
*)one
->state
->data
;
110 info2
= (struct data_info
*)two
->state
->data
;
113 free_stack(&one
->my_pools
);
115 free_stack(&two
->my_pools
);
117 if (one
== two
&& !one
->my_pools
) {
118 add_pool(&one
->my_pools
, slist1
);
119 add_pool(&one
->my_pools
, slist2
);
122 add_pool(&one
->my_pools
, slist1
);
124 add_pool(&two
->my_pools
, slist2
);
127 add_pool(&one
->all_pools
, slist1
);
128 add_pool(&two
->all_pools
, slist2
);
129 return merge_sm_states(one
, two
);
132 struct sm_state
*__extra_and_merge(struct sm_state
*sm
,
133 struct state_list_stack
*stack
)
135 struct state_list
*slist
;
136 struct sm_state
*ret
= NULL
;
137 struct sm_state
*tmp
;
140 FOR_EACH_PTR(stack
, slist
) {
142 ret
= get_sm_state_slist(slist
, sm
->name
, sm
->owner
,
145 tmp
= get_sm_state_slist(slist
, sm
->name
, sm
->owner
,
147 ret
= merge_sm_states(ret
, tmp
);
149 } END_FOR_EACH_PTR(slist
);
151 smatch_msg("Internal error in __extra_and_merge");
154 ret
->my_pools
= stack
;
155 ret
->all_pools
= clone_stack(stack
);
159 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
161 return &extra_undefined
;
164 static void match_function_call(struct expression
*expr
)
166 struct expression
*tmp
;
171 FOR_EACH_PTR(expr
->args
, tmp
) {
172 if (tmp
->op
== '&') {
173 name
= get_variable_from_expr(tmp
->unop
, &sym
);
175 set_state(name
, my_id
, sym
, &extra_undefined
);
180 } END_FOR_EACH_PTR(tmp
);
183 static void match_assign(struct expression
*expr
)
185 struct expression
*left
;
189 left
= strip_expr(expr
->left
);
190 name
= get_variable_from_expr(left
, &sym
);
193 set_state(name
, my_id
, sym
, alloc_extra_state(get_value(expr
->right
)));
197 static void undef_expr(struct expression
*expr
)
202 name
= get_variable_from_expr(expr
->unop
, &sym
);
205 if (!get_state(name
, my_id
, sym
)) {
209 set_state(name
, my_id
, sym
, &extra_undefined
);
213 static void match_declarations(struct symbol
*sym
)
218 name
= sym
->ident
->name
;
219 if (sym
->initializer
) {
220 set_state(name
, my_id
, sym
, alloc_extra_state(get_value(sym
->initializer
)));
222 set_state(name
, my_id
, sym
, &extra_undefined
);
227 static void match_function_def(struct symbol
*sym
)
231 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
235 set_state(arg
->ident
->name
, my_id
, arg
, &extra_undefined
);
236 } END_FOR_EACH_PTR(arg
);
239 static void match_unop(struct expression
*expr
)
246 name
= get_variable_from_expr(expr
->unop
, &sym
);
250 tmp
= show_special(expr
->op
);
251 if ((!strcmp(tmp
, "--")) || (!strcmp(tmp
, "++")))
252 set_state(name
, my_id
, sym
, &extra_undefined
);
256 int get_implied_value(struct expression
*expr
)
258 struct smatch_state
*state
;
263 val
= get_value(expr
);
264 if (val
!= UNDEFINED
)
267 name
= get_variable_from_expr(expr
, &sym
);
270 state
= get_state(name
, my_id
, sym
);
272 if (!state
|| !state
->data
)
274 return get_single_value((struct data_info
*)state
->data
);
277 int true_comparison(int left
, int comparison
, int right
)
281 case SPECIAL_UNSIGNED_LT
:
285 case SPECIAL_UNSIGNED_LTE
:
293 case SPECIAL_UNSIGNED_GTE
:
298 case SPECIAL_UNSIGNED_GT
:
302 case SPECIAL_NOTEQUAL
:
307 smatch_msg("unhandled comparison %d\n", comparison
);
313 static int do_comparison(struct expression
*expr
)
315 int left
, right
, ret
;
317 if ((left
= get_implied_value(expr
->left
)) == UNDEFINED
)
320 if ((right
= get_implied_value(expr
->right
)) == UNDEFINED
)
323 ret
= true_comparison(left
, expr
->op
, right
);
325 SM_DEBUG("%d known condition: %d %s %d => true\n",
326 get_lineno(), left
, show_special(expr
->op
), right
);
327 } else if (ret
== 0) {
328 SM_DEBUG("%d known condition: %d %s %d => false\n",
329 get_lineno(), left
, show_special(expr
->op
), right
);
334 int last_stmt_val(struct statement
*stmt
)
336 struct expression
*expr
;
338 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
339 if (stmt
->type
!= STMT_EXPRESSION
)
341 expr
= stmt
->expression
;
342 return get_value(expr
);
345 static void match_comparison(struct expression
*expr
)
350 struct smatch_state
*eq_state
;
351 struct smatch_state
*neq_state
;
353 if (expr
->op
!= SPECIAL_EQUAL
&& expr
->op
!= SPECIAL_NOTEQUAL
)
355 value
= get_value(expr
->left
);
356 if (value
!= UNDEFINED
) {
357 name
= get_variable_from_expr(expr
->right
, &sym
);
359 value
= get_value(expr
->right
);
360 name
= get_variable_from_expr(expr
->left
, &sym
);
362 if (value
== UNDEFINED
|| !name
|| !sym
)
364 eq_state
= alloc_extra_state(value
);
365 neq_state
= alloc_extra_state(UNDEFINED
);
366 neq_state
= add_filter(neq_state
, value
);
367 if (expr
->op
== SPECIAL_EQUAL
)
368 set_true_false_states(name
, my_id
, sym
, eq_state
, neq_state
);
370 set_true_false_states(name
, my_id
, sym
, neq_state
, eq_state
);
375 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
376 void __extra_match_condition(struct expression
*expr
)
380 struct smatch_state
*pre_state
;
381 struct smatch_state
*true_state
;
382 struct smatch_state
*false_state
;
384 expr
= strip_expr(expr
);
389 name
= get_variable_from_expr(expr
, &sym
);
392 pre_state
= get_state(name
, my_id
, sym
);
393 true_state
= add_filter(pre_state
, 0);
394 false_state
= alloc_extra_state(0);
395 set_true_false_states(name
, my_id
, sym
, true_state
, false_state
);
399 match_comparison(expr
);
404 static int variable_non_zero(struct expression
*expr
)
408 struct smatch_state
*state
;
411 name
= get_variable_from_expr(expr
, &sym
);
414 state
= get_state(name
, my_id
, sym
);
415 if (!state
|| !state
->data
)
417 ret
= true_comparison(get_single_value((struct data_info
*)state
->data
),
418 SPECIAL_NOTEQUAL
, 0);
424 int known_condition_true(struct expression
*expr
)
431 tmp
= get_value(expr
);
432 if (tmp
&& tmp
!= UNDEFINED
)
435 expr
= strip_expr(expr
);
438 if (expr
->op
== '!') {
439 if (known_condition_false(expr
->unop
))
450 int known_condition_false(struct expression
*expr
)
460 if (expr
->op
== '!') {
461 if (known_condition_true(expr
->unop
))
472 int implied_condition_true(struct expression
*expr
)
474 struct statement
*stmt
;
480 tmp
= get_value(expr
);
481 if (tmp
&& tmp
!= UNDEFINED
)
484 expr
= strip_expr(expr
);
487 if (do_comparison(expr
) == 1)
491 if (expr
->op
== '!') {
492 if (implied_condition_false(expr
->unop
))
496 stmt
= get_block_thing(expr
);
497 if (stmt
&& (last_stmt_val(stmt
) == 1))
501 if (variable_non_zero(expr
) == 1)
508 int implied_condition_false(struct expression
*expr
)
510 struct statement
*stmt
;
511 struct expression
*tmp
;
521 if (do_comparison(expr
) == 0)
524 if (expr
->op
== '!') {
525 if (implied_condition_true(expr
->unop
))
529 stmt
= get_block_thing(expr
);
530 if (stmt
&& (last_stmt_val(stmt
) == 0))
532 tmp
= strip_expr(expr
);
534 return implied_condition_false(tmp
);
537 if (variable_non_zero(expr
) == 0)
544 void register_smatch_extra(int id
)
547 add_merge_hook(my_id
, &merge_func
);
548 add_unmatched_state_hook(my_id
, &unmatched_state
);
549 add_hook(&undef_expr
, OP_HOOK
);
550 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
551 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
552 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
553 add_hook(&match_declarations
, DECLARATION_HOOK
);
554 add_hook(&match_unop
, OP_HOOK
);
555 add_hook(&free_data_info_allocs
, END_FUNC_HOOK
);
558 /* I don't know how to test for the ATTRIB_NORET attribute. :( */
559 add_function_hook("panic", &__match_nullify_path_hook
, NULL
);
560 add_function_hook("do_exit", &__match_nullify_path_hook
, NULL
);
561 add_function_hook("complete_and_exit", &__match_nullify_path_hook
, NULL
);
562 add_function_hook("__module_put_and_exit", &__match_nullify_path_hook
, NULL
);
563 add_function_hook("do_group_exit", &__match_nullify_path_hook
, NULL
);