2 * sparse/smatch_states.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * You have a lists of states. kernel = locked, foo = NULL, ...
12 * When you hit an if {} else {} statement then you swap the list
13 * of states for a different list of states. The lists are stored
16 * At the beginning of this file there are list of the stacks that
17 * we use. Each function in this file does something to one of
20 * So the smatch_flow.c understands code but it doesn't understand states.
21 * smatch_flow calls functions in this file. This file calls functions
22 * in smatch_slist.c which just has boring generic plumbing for handling
23 * state lists. But really it's this file where all the magic happens.
29 #include "smatch_slist.h"
30 #include "smatch_extra.h"
32 struct smatch_state undefined
= { .name
= "undefined" };
33 struct smatch_state merged
= { .name
= "merged" };
34 struct smatch_state true_state
= { .name
= "true" };
35 struct smatch_state false_state
= { .name
= "false" };
37 static struct state_list
*cur_slist
; /* current states */
39 static struct state_list_stack
*true_stack
; /* states after a t/f branch */
40 static struct state_list_stack
*false_stack
;
41 static struct state_list_stack
*pre_cond_stack
; /* states before a t/f branch */
43 static struct state_list_stack
*cond_true_stack
; /* states affected by a branch */
44 static struct state_list_stack
*cond_false_stack
;
46 static struct state_list_stack
*fake_cur_slist_stack
;
48 static struct state_list_stack
*break_stack
;
49 static struct state_list_stack
*switch_stack
;
50 static struct range_list_stack
*remaining_cases
;
51 static struct state_list_stack
*default_stack
;
52 static struct state_list_stack
*continue_stack
;
54 static struct named_stack
*goto_stack
;
56 struct state_list_stack
*implied_pools
;
60 void __print_cur_slist(void)
62 __print_slist(cur_slist
);
65 static int in_declarations_bit(void)
67 struct statement
*tmp
;
69 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
70 if (tmp
->type
== STMT_DECLARATION
)
73 } END_FOR_EACH_PTR_REVERSE(tmp
);
79 static int reset_warnings
= 1;
86 if (in_declarations_bit())
89 /* option spammy turns on a noisier version of this */
90 if (reset_warnings
&& !option_spammy
)
91 sm_msg("info: ignoring unreachable code.");
96 struct sm_state
*set_state(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
104 struct smatch_state
*s
;
106 s
= get_state(owner
, name
, sym
);
108 printf("%d new state. name='%s' [%s] %s\n",
109 get_lineno(), name
, check_name(owner
), show_state(state
));
111 printf("%d state change name='%s' [%s] %s => %s\n",
112 get_lineno(), name
, check_name(owner
), show_state(s
),
116 if (owner
!= -1 && unreachable())
119 __use_default_modification_hook(owner
, name
);
121 if (fake_cur_slist_stack
)
122 set_state_stack(&fake_cur_slist_stack
, owner
, name
, sym
, state
);
124 ret
= set_state_slist(&cur_slist
, owner
, name
, sym
, state
);
126 if (cond_true_stack
) {
127 set_state_stack(&cond_true_stack
, owner
, name
, sym
, state
);
128 set_state_stack(&cond_false_stack
, owner
, name
, sym
, state
);
133 struct sm_state
*set_state_expr(int owner
, struct expression
*expr
, struct smatch_state
*state
)
137 struct sm_state
*ret
= NULL
;
139 expr
= strip_expr(expr
);
140 name
= get_variable_from_expr(expr
, &sym
);
143 ret
= set_state(owner
, name
, sym
, state
);
149 void __push_fake_cur_slist()
151 push_slist(&fake_cur_slist_stack
, NULL
);
152 __save_pre_cond_states();
155 struct state_list
*__pop_fake_cur_slist()
157 __use_pre_cond_states();
158 return pop_slist(&fake_cur_slist_stack
);
161 void __set_sm(struct sm_state
*sm
)
164 struct smatch_state
*s
;
166 s
= get_state(sm
->owner
, sm
->name
, sm
->sym
);
168 printf("%d new state. name='%s' [%s] %s\n",
169 get_lineno(), sm
->name
, check_name(sm
->owner
),
170 show_state(sm
->state
));
172 printf("%d state change name='%s' [%s] %s => %s\n",
173 get_lineno(), sm
->name
, check_name(sm
->owner
), show_state(s
),
174 show_state(sm
->state
));
180 overwrite_sm_state(&cur_slist
, sm
);
182 if (cond_true_stack
) {
183 overwrite_sm_state_stack(&cond_true_stack
, sm
);
184 overwrite_sm_state_stack(&cond_false_stack
, sm
);
188 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
)
190 return get_state_slist(cur_slist
, owner
, name
, sym
);
193 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
)
197 struct smatch_state
*ret
= NULL
;
199 expr
= strip_expr(expr
);
200 name
= get_variable_from_expr(expr
, &sym
);
203 ret
= get_state(owner
, name
, sym
);
209 struct state_list
*get_possible_states(int owner
, const char *name
, struct symbol
*sym
)
211 struct sm_state
*sms
;
213 sms
= get_sm_state_slist(cur_slist
, owner
, name
, sym
);
215 return sms
->possible
;
219 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
)
223 struct state_list
*ret
= NULL
;
225 expr
= strip_expr(expr
);
226 name
= get_variable_from_expr(expr
, &sym
);
229 ret
= get_possible_states(owner
, name
, sym
);
235 struct sm_state
*get_sm_state(int owner
, const char *name
, struct symbol
*sym
)
237 return get_sm_state_slist(cur_slist
, owner
, name
, sym
);
240 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
)
244 struct sm_state
*ret
= NULL
;
246 expr
= strip_expr(expr
);
247 name
= get_variable_from_expr(expr
, &sym
);
250 ret
= get_sm_state(owner
, name
, sym
);
256 void delete_state(int owner
, const char *name
, struct symbol
*sym
)
258 delete_state_slist(&cur_slist
, owner
, name
, sym
);
259 if (cond_true_stack
) {
260 delete_state_stack(&pre_cond_stack
, owner
, name
, sym
);
261 delete_state_stack(&cond_true_stack
, owner
, name
, sym
);
262 delete_state_stack(&cond_false_stack
, owner
, name
, sym
);
266 void delete_state_expr(int owner
, struct expression
*expr
)
271 expr
= strip_expr(expr
);
272 name
= get_variable_from_expr(expr
, &sym
);
275 delete_state(owner
, name
, sym
);
280 struct state_list
*get_all_states(int owner
)
282 struct state_list
*slist
= NULL
;
283 struct sm_state
*tmp
;
285 FOR_EACH_PTR(cur_slist
, tmp
) {
286 if (tmp
->owner
== owner
)
287 add_ptr_list(&slist
, tmp
);
288 } END_FOR_EACH_PTR(tmp
);
293 int is_reachable(void)
300 struct state_list
*__get_cur_slist(void)
305 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
306 struct smatch_state
*true_state
,
307 struct smatch_state
*false_state
)
310 struct smatch_state
*tmp
;
312 tmp
= get_state(owner
, name
, sym
);
313 sm_debug("%d set_true_false '%s'. Was %s. Now T:%s F:%s\n",
314 get_lineno(), name
, show_state(tmp
),
315 show_state(true_state
), show_state(false_state
));
321 if (!cond_false_stack
|| !cond_true_stack
) {
322 printf("Error: missing true/false stacks\n");
326 __use_default_modification_hook(owner
, name
);
329 set_state_slist(&cur_slist
, owner
, name
, sym
, true_state
);
330 set_state_stack(&cond_true_stack
, owner
, name
, sym
, true_state
);
333 set_state_stack(&cond_false_stack
, owner
, name
, sym
, false_state
);
336 void set_true_false_states_expr(int owner
, struct expression
*expr
,
337 struct smatch_state
*true_state
,
338 struct smatch_state
*false_state
)
343 expr
= strip_expr(expr
);
344 name
= get_variable_from_expr(expr
, &sym
);
347 set_true_false_states(owner
, name
, sym
, true_state
, false_state
);
352 void __set_true_false_sm(struct sm_state
*true_sm
, struct sm_state
*false_sm
)
357 if (!cond_false_stack
|| !cond_true_stack
) {
358 printf("Error: missing true/false stacks\n");
363 overwrite_sm_state(&cur_slist
, true_sm
);
364 overwrite_sm_state_stack(&cond_true_stack
, true_sm
);
367 overwrite_sm_state_stack(&cond_false_stack
, false_sm
);
370 void nullify_path(void)
372 free_slist(&cur_slist
);
375 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
382 * At the start of every function we mark the path
383 * as unnull. That way there is always at least one state
384 * in the cur_slist until nullify_path is called. This
385 * is used in merge_slist() for the first null check.
387 void __unnullify_path(void)
389 set_state(-1, "unnull_path", NULL
, &true_state
);
392 int __path_is_null(void)
399 static void check_stack_free(struct state_list_stack
**stack
)
402 sm_msg("smatch internal error: stack not empty");
403 free_stack_and_slists(stack
);
407 void clear_all_states(void)
409 struct named_slist
*named_slist
;
412 check_stack_free(&true_stack
);
413 check_stack_free(&false_stack
);
414 check_stack_free(&pre_cond_stack
);
415 check_stack_free(&cond_true_stack
);
416 check_stack_free(&cond_false_stack
);
417 check_stack_free(&break_stack
);
418 check_stack_free(&switch_stack
);
419 check_stack_free(&continue_stack
);
420 free_stack_and_slists(&implied_pools
);
422 FOR_EACH_PTR(goto_stack
, named_slist
) {
423 free_slist(&named_slist
->slist
);
424 } END_FOR_EACH_PTR(named_slist
);
425 __free_ptr_list((struct ptr_list
**)&goto_stack
);
426 free_every_single_sm_state();
429 void __push_cond_stacks(void)
431 push_slist(&cond_true_stack
, NULL
);
432 push_slist(&cond_false_stack
, NULL
);
435 struct state_list
*__copy_cond_true_states(void)
437 struct state_list
*ret
;
439 ret
= pop_slist(&cond_true_stack
);
440 push_slist(&cond_true_stack
, clone_slist(ret
));
444 struct state_list
*__copy_cond_false_states(void)
446 struct state_list
*ret
;
448 ret
= pop_slist(&cond_false_stack
);
449 push_slist(&cond_false_stack
, clone_slist(ret
));
453 struct state_list
*__pop_cond_true_stack(void)
455 return pop_slist(&cond_true_stack
);
458 struct state_list
*__pop_cond_false_stack(void)
460 return pop_slist(&cond_false_stack
);
464 * This combines the pre cond states with either the true or false states.
466 * a = kmalloc() ; if (a !! foo(a)
467 * In the pre state a is possibly null. In the true state it is non null.
468 * In the false state it is null. Combine the pre and the false to get
469 * that when we call 'foo', 'a' is null.
471 static void __use_cond_stack(struct state_list_stack
**stack
)
473 struct state_list
*slist
;
475 free_slist(&cur_slist
);
477 cur_slist
= pop_slist(&pre_cond_stack
);
478 push_slist(&pre_cond_stack
, clone_slist(cur_slist
));
480 slist
= pop_slist(stack
);
481 overwrite_slist(slist
, &cur_slist
);
482 push_slist(stack
, slist
);
485 void __use_pre_cond_states(void)
487 free_slist(&cur_slist
);
488 cur_slist
= pop_slist(&pre_cond_stack
);
491 void __use_cond_true_states(void)
493 __use_cond_stack(&cond_true_stack
);
496 void __use_cond_false_states(void)
498 __use_cond_stack(&cond_false_stack
);
501 void __negate_cond_stacks(void)
503 struct state_list
*old_false
, *old_true
;
505 __use_cond_stack(&cond_false_stack
);
506 old_false
= pop_slist(&cond_false_stack
);
507 old_true
= pop_slist(&cond_true_stack
);
508 push_slist(&cond_false_stack
, old_true
);
509 push_slist(&cond_true_stack
, old_false
);
512 void __and_cond_states(void)
514 and_slist_stack(&cond_true_stack
);
515 or_slist_stack(&pre_cond_stack
, cur_slist
, &cond_false_stack
);
518 void __or_cond_states(void)
520 or_slist_stack(&pre_cond_stack
, cur_slist
, &cond_true_stack
);
521 and_slist_stack(&cond_false_stack
);
524 void __save_pre_cond_states(void)
526 push_slist(&pre_cond_stack
, clone_slist(cur_slist
));
529 void __discard_pre_cond_states(void)
531 struct state_list
*tmp
;
533 tmp
= pop_slist(&pre_cond_stack
);
537 void __use_cond_states(void)
539 struct state_list
*pre
, *pre_clone
, *true_states
, *false_states
;
541 pre
= pop_slist(&pre_cond_stack
);
542 pre_clone
= clone_slist(pre
);
544 true_states
= pop_slist(&cond_true_stack
);
545 overwrite_slist(true_states
, &pre
);
546 /* we use the true states right away */
547 free_slist(&cur_slist
);
550 false_states
= pop_slist(&cond_false_stack
);
551 overwrite_slist(false_states
, &pre_clone
);
552 push_slist(&false_stack
, pre_clone
);
555 void __push_true_states(void)
557 push_slist(&true_stack
, clone_slist(cur_slist
));
560 void __use_false_states(void)
562 free_slist(&cur_slist
);
563 cur_slist
= pop_slist(&false_stack
);
566 void __discard_false_states(void)
568 struct state_list
*slist
;
570 slist
= pop_slist(&false_stack
);
574 void __merge_false_states(void)
576 struct state_list
*slist
;
578 slist
= pop_slist(&false_stack
);
579 merge_slist(&cur_slist
, slist
);
583 void __merge_true_states(void)
585 struct state_list
*slist
;
587 slist
= pop_slist(&true_stack
);
588 merge_slist(&cur_slist
, slist
);
592 void __push_continues(void)
594 push_slist(&continue_stack
, NULL
);
597 void __discard_continues(void)
599 struct state_list
*slist
;
601 slist
= pop_slist(&continue_stack
);
605 void __process_continues(void)
607 struct state_list
*slist
;
609 slist
= pop_slist(&continue_stack
);
611 slist
= clone_slist(cur_slist
);
613 merge_slist(&slist
, cur_slist
);
615 push_slist(&continue_stack
, slist
);
618 static int top_slist_empty(struct state_list_stack
**stack
)
620 struct state_list
*tmp
;
623 tmp
= pop_slist(stack
);
626 push_slist(stack
, tmp
);
630 /* a silly loop does this: while(i--) { return; } */
631 void __warn_on_silly_pre_loops(void)
633 if (!__path_is_null())
635 if (!top_slist_empty(&continue_stack
))
637 if (!top_slist_empty(&break_stack
))
639 /* if the path was nullified before the loop, then we already
640 printed an error earlier */
641 if (top_slist_empty(&false_stack
))
643 sm_msg("info: loop could be replaced with if statement.");
646 void __merge_continues(void)
648 struct state_list
*slist
;
650 slist
= pop_slist(&continue_stack
);
651 merge_slist(&cur_slist
, slist
);
655 void __push_breaks(void)
657 push_slist(&break_stack
, NULL
);
660 void __process_breaks(void)
662 struct state_list
*slist
;
664 slist
= pop_slist(&break_stack
);
666 slist
= clone_slist(cur_slist
);
668 merge_slist(&slist
, cur_slist
);
670 push_slist(&break_stack
, slist
);
673 void __merge_breaks(void)
675 struct state_list
*slist
;
677 slist
= pop_slist(&break_stack
);
678 merge_slist(&cur_slist
, slist
);
682 void __use_breaks(void)
684 free_slist(&cur_slist
);
685 cur_slist
= pop_slist(&break_stack
);
688 void __save_switch_states(struct expression
*switch_expr
)
690 push_range_list(&remaining_cases
, __get_implied_values(switch_expr
));
691 push_slist(&switch_stack
, clone_slist(cur_slist
));
694 void __merge_switches(struct expression
*switch_expr
, struct expression
*case_expr
)
696 struct state_list
*slist
;
697 struct state_list
*implied_slist
;
699 slist
= pop_slist(&switch_stack
);
700 implied_slist
= __implied_case_slist(switch_expr
, case_expr
, &remaining_cases
, &slist
);
701 merge_slist(&cur_slist
, implied_slist
);
702 free_slist(&implied_slist
);
703 push_slist(&switch_stack
, slist
);
706 void __discard_switches(void)
708 struct state_list
*slist
;
710 pop_range_list(&remaining_cases
);
711 slist
= pop_slist(&switch_stack
);
715 void __push_default(void)
717 push_slist(&default_stack
, NULL
);
720 void __set_default(void)
722 set_state_stack(&default_stack
, 0, "has_default", NULL
, &true_state
);
725 int __pop_default(void)
727 struct state_list
*slist
;
729 slist
= pop_slist(&default_stack
);
737 static struct named_slist
*alloc_named_slist(const char *name
, struct state_list
*slist
)
739 struct named_slist
*named_slist
= __alloc_named_slist(0);
741 named_slist
->name
= (char *)name
;
742 named_slist
->slist
= slist
;
746 void __save_gotos(const char *name
)
748 struct state_list
**slist
;
749 struct state_list
*clone
;
751 slist
= get_slist_from_named_stack(goto_stack
, name
);
753 clone
= clone_slist(cur_slist
);
754 merge_slist(slist
, clone
);
758 struct named_slist
*named_slist
;
760 clone
= clone_slist(cur_slist
);
761 named_slist
= alloc_named_slist(name
, clone
);
762 add_ptr_list(&goto_stack
, named_slist
);
766 void __merge_gotos(const char *name
)
768 struct state_list
**slist
;
770 slist
= get_slist_from_named_stack(goto_stack
, name
);
772 merge_slist(&cur_slist
, *slist
);