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
;
49 static struct state_list_stack
*break_stack
;
50 static struct state_list_stack
*switch_stack
;
51 static struct range_list_stack
*remaining_cases
;
52 static struct state_list_stack
*default_stack
;
53 static struct state_list_stack
*continue_stack
;
55 static struct named_stack
*goto_stack
;
57 static struct ptr_list
*backup
;
59 struct state_list_stack
*implied_pools
;
63 void __print_cur_slist(void)
65 __print_slist(cur_slist
);
68 static int in_declarations_bit(void)
70 struct statement
*tmp
;
72 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
73 if (tmp
->type
== STMT_DECLARATION
)
76 } END_FOR_EACH_PTR_REVERSE(tmp
);
82 static int reset_warnings
= 1;
90 if (in_declarations_bit())
93 /* option spammy turns on a noisier version of this */
94 if (reset_warnings
&& !option_spammy
)
95 sm_msg("info: ignoring unreachable code.");
101 struct sm_state
*set_state(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
103 struct sm_state
*ret
;
109 sm_msg("Smatch Internal Error: cur_slist is read only.");
111 if (option_debug
|| strcmp(check_name(owner
), option_debug_check
) == 0) {
112 struct smatch_state
*s
;
114 s
= get_state(owner
, name
, sym
);
116 printf("%d new state. name='%s' [%s] %s\n",
117 get_lineno(), name
, check_name(owner
), show_state(state
));
119 printf("%d state change name='%s' [%s] %s => %s\n",
120 get_lineno(), name
, check_name(owner
), show_state(s
),
124 if (owner
!= -1 && unreachable())
127 if (fake_cur_slist_stack
)
128 set_state_stack(&fake_cur_slist_stack
, owner
, name
, sym
, state
);
130 ret
= set_state_slist(&cur_slist
, owner
, name
, sym
, state
);
132 if (cond_true_stack
) {
133 set_state_stack(&cond_true_stack
, owner
, name
, sym
, state
);
134 set_state_stack(&cond_false_stack
, owner
, name
, sym
, state
);
139 struct sm_state
*set_state_expr(int owner
, struct expression
*expr
, struct smatch_state
*state
)
143 struct sm_state
*ret
= NULL
;
145 expr
= strip_expr(expr
);
146 name
= expr_to_var_sym(expr
, &sym
);
149 ret
= set_state(owner
, name
, sym
, state
);
155 void __push_fake_cur_slist()
157 push_slist(&fake_cur_slist_stack
, NULL
);
158 __save_pre_cond_states();
161 struct state_list
*__pop_fake_cur_slist()
163 __use_pre_cond_states();
164 return pop_slist(&fake_cur_slist_stack
);
167 void __free_fake_cur_slist()
169 struct state_list
*slist
;
171 __use_pre_cond_states();
172 slist
= pop_slist(&fake_cur_slist_stack
);
176 void __set_fake_cur_slist_fast(struct state_list
*slist
)
178 push_slist(&pre_cond_stack
, cur_slist
);
183 void __pop_fake_cur_slist_fast()
185 cur_slist
= pop_slist(&pre_cond_stack
);
189 void __merge_slist_into_cur(struct state_list
*slist
)
192 struct sm_state
*orig
;
193 struct sm_state
*merged
;
195 FOR_EACH_PTR(slist
, sm
) {
196 orig
= get_sm_state(sm
->owner
, sm
->name
, sm
->sym
);
198 merged
= merge_sm_states(orig
, sm
);
202 } END_FOR_EACH_PTR(sm
);
205 void __set_sm(struct sm_state
*sm
)
208 sm_msg("Smatch Internal Error: cur_slist is read only.");
211 strcmp(check_name(sm
->owner
), option_debug_check
) == 0) {
212 struct smatch_state
*s
;
214 s
= get_state(sm
->owner
, sm
->name
, sm
->sym
);
216 printf("%d new state. name='%s' [%s] %s\n",
217 get_lineno(), sm
->name
, check_name(sm
->owner
),
218 show_state(sm
->state
));
220 printf("%d state change name='%s' [%s] %s => %s\n",
221 get_lineno(), sm
->name
, check_name(sm
->owner
), show_state(s
),
222 show_state(sm
->state
));
228 if (fake_cur_slist_stack
)
229 overwrite_sm_state_stack(&fake_cur_slist_stack
, sm
);
231 overwrite_sm_state(&cur_slist
, sm
);
233 if (cond_true_stack
) {
234 overwrite_sm_state_stack(&cond_true_stack
, sm
);
235 overwrite_sm_state_stack(&cond_false_stack
, sm
);
239 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
)
241 return get_state_slist(cur_slist
, owner
, name
, sym
);
244 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
)
248 struct smatch_state
*ret
= NULL
;
250 expr
= strip_expr(expr
);
251 name
= expr_to_var_sym(expr
, &sym
);
254 ret
= get_state(owner
, name
, sym
);
260 struct state_list
*get_possible_states(int owner
, const char *name
, struct symbol
*sym
)
262 struct sm_state
*sms
;
264 sms
= get_sm_state_slist(cur_slist
, owner
, name
, sym
);
266 return sms
->possible
;
270 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
)
274 struct state_list
*ret
= NULL
;
276 expr
= strip_expr(expr
);
277 name
= expr_to_var_sym(expr
, &sym
);
280 ret
= get_possible_states(owner
, name
, sym
);
286 struct sm_state
*get_sm_state(int owner
, const char *name
, struct symbol
*sym
)
288 return get_sm_state_slist(cur_slist
, owner
, name
, sym
);
291 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
)
295 struct sm_state
*ret
= NULL
;
297 expr
= strip_expr(expr
);
298 name
= expr_to_var_sym(expr
, &sym
);
301 ret
= get_sm_state(owner
, name
, sym
);
307 void delete_state(int owner
, const char *name
, struct symbol
*sym
)
309 delete_state_slist(&cur_slist
, owner
, name
, sym
);
310 if (cond_true_stack
) {
311 delete_state_stack(&pre_cond_stack
, owner
, name
, sym
);
312 delete_state_stack(&cond_true_stack
, owner
, name
, sym
);
313 delete_state_stack(&cond_false_stack
, owner
, name
, sym
);
317 void delete_state_expr(int owner
, struct expression
*expr
)
322 expr
= strip_expr(expr
);
323 name
= expr_to_var_sym(expr
, &sym
);
326 delete_state(owner
, name
, sym
);
331 struct state_list
*get_all_states_slist(int owner
, struct state_list
*source
)
333 struct state_list
*slist
= NULL
;
334 struct sm_state
*tmp
;
336 FOR_EACH_PTR(source
, tmp
) {
337 if (tmp
->owner
== owner
)
338 add_ptr_list(&slist
, tmp
);
339 } END_FOR_EACH_PTR(tmp
);
344 struct state_list
*get_all_states(int owner
)
346 struct state_list
*slist
= NULL
;
347 struct sm_state
*tmp
;
349 FOR_EACH_PTR(cur_slist
, tmp
) {
350 if (tmp
->owner
== owner
)
351 add_ptr_list(&slist
, tmp
);
352 } END_FOR_EACH_PTR(tmp
);
357 int is_reachable(void)
364 struct state_list
*__get_cur_slist(void)
369 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
370 struct smatch_state
*true_state
,
371 struct smatch_state
*false_state
)
373 if (option_debug
|| strcmp(check_name(owner
), option_debug_check
) == 0) {
374 struct smatch_state
*tmp
;
376 tmp
= get_state(owner
, name
, sym
);
377 printf("%d set_true_false '%s'. Was %s. Now T:%s F:%s\n",
378 get_lineno(), name
, show_state(tmp
),
379 show_state(true_state
), show_state(false_state
));
385 if (!cond_false_stack
|| !cond_true_stack
) {
386 printf("Error: missing true/false stacks\n");
391 set_state_slist(&cur_slist
, owner
, name
, sym
, true_state
);
392 set_state_stack(&cond_true_stack
, owner
, name
, sym
, true_state
);
395 set_state_stack(&cond_false_stack
, owner
, name
, sym
, false_state
);
398 void set_true_false_states_expr(int owner
, struct expression
*expr
,
399 struct smatch_state
*true_state
,
400 struct smatch_state
*false_state
)
405 expr
= strip_expr(expr
);
406 name
= expr_to_var_sym(expr
, &sym
);
409 set_true_false_states(owner
, name
, sym
, true_state
, false_state
);
414 void __set_true_false_sm(struct sm_state
*true_sm
, struct sm_state
*false_sm
)
419 if (!cond_false_stack
|| !cond_true_stack
) {
420 printf("Error: missing true/false stacks\n");
425 overwrite_sm_state(&cur_slist
, true_sm
);
426 overwrite_sm_state_stack(&cond_true_stack
, true_sm
);
429 overwrite_sm_state_stack(&cond_false_stack
, false_sm
);
432 void nullify_path(void)
434 free_slist(&cur_slist
);
437 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
444 * At the start of every function we mark the path
445 * as unnull. That way there is always at least one state
446 * in the cur_slist until nullify_path is called. This
447 * is used in merge_slist() for the first null check.
449 void __unnullify_path(void)
451 set_state(-1, "unnull_path", NULL
, &true_state
);
454 int __path_is_null(void)
461 static void check_stack_free(struct state_list_stack
**stack
)
464 sm_msg("smatch internal error: stack not empty");
465 free_stack_and_slists(stack
);
469 void save_all_states(void)
471 __add_ptr_list(&backup
, cur_slist
, 0);
473 __add_ptr_list(&backup
, true_stack
, 0);
474 __add_ptr_list(&backup
, false_stack
, 0);
475 __add_ptr_list(&backup
, pre_cond_stack
, 0);
477 __add_ptr_list(&backup
, cond_true_stack
, 0);
478 __add_ptr_list(&backup
, cond_false_stack
, 0);
480 __add_ptr_list(&backup
, fake_cur_slist_stack
, 0);
482 __add_ptr_list(&backup
, break_stack
, 0);
483 __add_ptr_list(&backup
, switch_stack
, 0);
484 __add_ptr_list(&backup
, remaining_cases
, 0);
485 __add_ptr_list(&backup
, default_stack
, 0);
486 __add_ptr_list(&backup
, continue_stack
, 0);
488 __add_ptr_list(&backup
, goto_stack
, 0);
491 void nullify_all_states(void)
497 pre_cond_stack
= NULL
;
499 cond_true_stack
= NULL
;
500 cond_false_stack
= NULL
;
502 fake_cur_slist_stack
= NULL
;
506 remaining_cases
= NULL
;
507 default_stack
= NULL
;
508 continue_stack
= NULL
;
513 static void *pop_backup(void)
517 ret
= last_ptr_list(backup
);
518 delete_ptr_list_last(&backup
);
522 void restore_all_states(void)
524 goto_stack
= pop_backup();
526 continue_stack
= pop_backup();
527 default_stack
= pop_backup();
528 remaining_cases
= pop_backup();
529 switch_stack
= pop_backup();
530 break_stack
= pop_backup();
532 fake_cur_slist_stack
= pop_backup();
534 cond_false_stack
= pop_backup();
535 cond_true_stack
= pop_backup();
537 pre_cond_stack
= pop_backup();
538 false_stack
= pop_backup();
539 true_stack
= pop_backup();
541 cur_slist
= pop_backup();
544 void clear_all_states(void)
546 struct named_slist
*named_slist
;
549 check_stack_free(&true_stack
);
550 check_stack_free(&false_stack
);
551 check_stack_free(&pre_cond_stack
);
552 check_stack_free(&cond_true_stack
);
553 check_stack_free(&cond_false_stack
);
554 check_stack_free(&break_stack
);
555 check_stack_free(&switch_stack
);
556 check_stack_free(&continue_stack
);
557 free_stack_and_slists(&implied_pools
);
559 FOR_EACH_PTR(goto_stack
, named_slist
) {
560 free_slist(&named_slist
->slist
);
561 } END_FOR_EACH_PTR(named_slist
);
562 __free_ptr_list((struct ptr_list
**)&goto_stack
);
563 free_every_single_sm_state();
566 void __push_cond_stacks(void)
568 push_slist(&cond_true_stack
, NULL
);
569 push_slist(&cond_false_stack
, NULL
);
572 struct state_list
*__copy_cond_true_states(void)
574 struct state_list
*ret
;
576 ret
= pop_slist(&cond_true_stack
);
577 push_slist(&cond_true_stack
, clone_slist(ret
));
581 struct state_list
*__copy_cond_false_states(void)
583 struct state_list
*ret
;
585 ret
= pop_slist(&cond_false_stack
);
586 push_slist(&cond_false_stack
, clone_slist(ret
));
590 struct state_list
*__pop_cond_true_stack(void)
592 return pop_slist(&cond_true_stack
);
595 struct state_list
*__pop_cond_false_stack(void)
597 return pop_slist(&cond_false_stack
);
601 * This combines the pre cond states with either the true or false states.
603 * a = kmalloc() ; if (a !! foo(a)
604 * In the pre state a is possibly null. In the true state it is non null.
605 * In the false state it is null. Combine the pre and the false to get
606 * that when we call 'foo', 'a' is null.
608 static void __use_cond_stack(struct state_list_stack
**stack
)
610 struct state_list
*slist
;
612 free_slist(&cur_slist
);
614 cur_slist
= pop_slist(&pre_cond_stack
);
615 push_slist(&pre_cond_stack
, clone_slist(cur_slist
));
617 slist
= pop_slist(stack
);
618 overwrite_slist(slist
, &cur_slist
);
619 push_slist(stack
, slist
);
622 void __use_pre_cond_states(void)
624 free_slist(&cur_slist
);
625 cur_slist
= pop_slist(&pre_cond_stack
);
628 void __use_cond_true_states(void)
630 __use_cond_stack(&cond_true_stack
);
633 void __use_cond_false_states(void)
635 __use_cond_stack(&cond_false_stack
);
638 void __negate_cond_stacks(void)
640 struct state_list
*old_false
, *old_true
;
642 __use_cond_stack(&cond_false_stack
);
643 old_false
= pop_slist(&cond_false_stack
);
644 old_true
= pop_slist(&cond_true_stack
);
645 push_slist(&cond_false_stack
, old_true
);
646 push_slist(&cond_true_stack
, old_false
);
649 void __and_cond_states(void)
651 and_slist_stack(&cond_true_stack
);
652 or_slist_stack(&pre_cond_stack
, cur_slist
, &cond_false_stack
);
655 void __or_cond_states(void)
657 or_slist_stack(&pre_cond_stack
, cur_slist
, &cond_true_stack
);
658 and_slist_stack(&cond_false_stack
);
661 void __save_pre_cond_states(void)
663 push_slist(&pre_cond_stack
, clone_slist(cur_slist
));
666 void __discard_pre_cond_states(void)
668 struct state_list
*tmp
;
670 tmp
= pop_slist(&pre_cond_stack
);
674 void __use_cond_states(void)
676 struct state_list
*pre
, *pre_clone
, *true_states
, *false_states
;
678 pre
= pop_slist(&pre_cond_stack
);
679 pre_clone
= clone_slist(pre
);
681 true_states
= pop_slist(&cond_true_stack
);
682 overwrite_slist(true_states
, &pre
);
683 /* we use the true states right away */
684 free_slist(&cur_slist
);
687 false_states
= pop_slist(&cond_false_stack
);
688 overwrite_slist(false_states
, &pre_clone
);
689 push_slist(&false_stack
, pre_clone
);
692 void __push_true_states(void)
694 push_slist(&true_stack
, clone_slist(cur_slist
));
697 void __use_false_states(void)
699 free_slist(&cur_slist
);
700 cur_slist
= pop_slist(&false_stack
);
703 void __discard_false_states(void)
705 struct state_list
*slist
;
707 slist
= pop_slist(&false_stack
);
711 void __merge_false_states(void)
713 struct state_list
*slist
;
715 slist
= pop_slist(&false_stack
);
716 merge_slist(&cur_slist
, slist
);
720 void __merge_true_states(void)
722 struct state_list
*slist
;
724 slist
= pop_slist(&true_stack
);
725 merge_slist(&cur_slist
, slist
);
729 void __push_continues(void)
731 push_slist(&continue_stack
, NULL
);
734 void __discard_continues(void)
736 struct state_list
*slist
;
738 slist
= pop_slist(&continue_stack
);
742 void __process_continues(void)
744 struct state_list
*slist
;
746 slist
= pop_slist(&continue_stack
);
748 slist
= clone_slist(cur_slist
);
750 merge_slist(&slist
, cur_slist
);
752 push_slist(&continue_stack
, slist
);
755 static int top_slist_empty(struct state_list_stack
**stack
)
757 struct state_list
*tmp
;
760 tmp
= pop_slist(stack
);
763 push_slist(stack
, tmp
);
767 /* a silly loop does this: while(i--) { return; } */
768 void __warn_on_silly_pre_loops(void)
770 if (!__path_is_null())
772 if (!top_slist_empty(&continue_stack
))
774 if (!top_slist_empty(&break_stack
))
776 /* if the path was nullified before the loop, then we already
777 printed an error earlier */
778 if (top_slist_empty(&false_stack
))
780 sm_msg("info: loop could be replaced with if statement.");
783 void __merge_continues(void)
785 struct state_list
*slist
;
787 slist
= pop_slist(&continue_stack
);
788 merge_slist(&cur_slist
, slist
);
792 void __push_breaks(void)
794 push_slist(&break_stack
, NULL
);
797 void __process_breaks(void)
799 struct state_list
*slist
;
801 slist
= pop_slist(&break_stack
);
803 slist
= clone_slist(cur_slist
);
805 merge_slist(&slist
, cur_slist
);
807 push_slist(&break_stack
, slist
);
810 int __has_breaks(void)
812 struct state_list
*slist
;
815 slist
= pop_slist(&break_stack
);
817 push_slist(&break_stack
, slist
);
821 void __merge_breaks(void)
823 struct state_list
*slist
;
825 slist
= pop_slist(&break_stack
);
826 merge_slist(&cur_slist
, slist
);
830 void __use_breaks(void)
832 free_slist(&cur_slist
);
833 cur_slist
= pop_slist(&break_stack
);
836 void __save_switch_states(struct expression
*switch_expr
)
838 push_rl(&remaining_cases
, __get_implied_values(switch_expr
));
839 push_slist(&switch_stack
, clone_slist(cur_slist
));
842 void __merge_switches(struct expression
*switch_expr
, struct expression
*case_expr
)
844 struct state_list
*slist
;
845 struct state_list
*implied_slist
;
847 slist
= pop_slist(&switch_stack
);
848 implied_slist
= __implied_case_slist(switch_expr
, case_expr
, &remaining_cases
, &slist
);
849 merge_slist(&cur_slist
, implied_slist
);
850 free_slist(&implied_slist
);
851 push_slist(&switch_stack
, slist
);
854 void __discard_switches(void)
856 struct state_list
*slist
;
858 pop_rl(&remaining_cases
);
859 slist
= pop_slist(&switch_stack
);
863 void __push_default(void)
865 push_slist(&default_stack
, NULL
);
868 void __set_default(void)
870 set_state_stack(&default_stack
, 0, "has_default", NULL
, &true_state
);
873 int __pop_default(void)
875 struct state_list
*slist
;
877 slist
= pop_slist(&default_stack
);
885 static struct named_slist
*alloc_named_slist(const char *name
, struct state_list
*slist
)
887 struct named_slist
*named_slist
= __alloc_named_slist(0);
889 named_slist
->name
= (char *)name
;
890 named_slist
->slist
= slist
;
894 void __save_gotos(const char *name
)
896 struct state_list
**slist
;
897 struct state_list
*clone
;
899 slist
= get_slist_from_named_stack(goto_stack
, name
);
901 clone
= clone_slist(cur_slist
);
902 merge_slist(slist
, clone
);
906 struct named_slist
*named_slist
;
908 clone
= clone_slist(cur_slist
);
909 named_slist
= alloc_named_slist(name
, clone
);
910 add_ptr_list(&goto_stack
, named_slist
);
914 void __merge_gotos(const char *name
)
916 struct state_list
**slist
;
918 slist
= get_slist_from_named_stack(goto_stack
, name
);
920 merge_slist(&cur_slist
, *slist
);