2 * Copyright (C) 2006 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * You have a lists of states. kernel = locked, foo = NULL, ...
20 * When you hit an if {} else {} statement then you swap the list
21 * of states for a different list of states. The lists are stored
24 * At the beginning of this file there are list of the stacks that
25 * we use. Each function in this file does something to one of
28 * So the smatch_flow.c understands code but it doesn't understand states.
29 * smatch_flow calls functions in this file. This file calls functions
30 * in smatch_slist.c which just has boring generic plumbing for handling
31 * state lists. But really it's this file where all the magic happens.
37 #include "smatch_slist.h"
38 #include "smatch_extra.h"
40 struct smatch_state undefined
= { .name
= "undefined" };
41 struct smatch_state merged
= { .name
= "merged" };
42 struct smatch_state true_state
= { .name
= "true" };
43 struct smatch_state false_state
= { .name
= "false" };
45 static struct AVL
*cur_stree
; /* current states */
47 static struct stree_stack
*true_stack
; /* states after a t/f branch */
48 static struct stree_stack
*false_stack
;
49 static struct stree_stack
*pre_cond_stack
; /* states before a t/f branch */
51 static struct stree_stack
*cond_true_stack
; /* states affected by a branch */
52 static struct stree_stack
*cond_false_stack
;
54 static struct stree_stack
*fake_cur_stree_stack
;
57 static struct stree_stack
*break_stack
;
58 static struct stree_stack
*switch_stack
;
59 static struct range_list_stack
*remaining_cases
;
60 static struct stree_stack
*default_stack
;
61 static struct stree_stack
*continue_stack
;
63 static struct named_stree_stack
*goto_stack
;
65 static struct ptr_list
*backup
;
67 struct state_list_stack
*implied_pools
;
71 void __print_cur_slist(void)
73 __print_stree(cur_stree
);
76 static int in_declarations_bit(void)
78 struct statement
*tmp
;
80 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
81 if (tmp
->type
== STMT_DECLARATION
)
84 } END_FOR_EACH_PTR_REVERSE(tmp
);
90 static int reset_warnings
= 1;
98 if (in_declarations_bit())
101 /* option spammy turns on a noisier version of this */
102 if (reset_warnings
&& !option_spammy
)
103 sm_msg("info: ignoring unreachable code.");
109 struct sm_state
*set_state(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
111 struct sm_state
*ret
;
117 sm_msg("Smatch Internal Error: cur_stree is read only.");
119 if (option_debug
|| strcmp(check_name(owner
), option_debug_check
) == 0) {
120 struct smatch_state
*s
;
122 s
= get_state(owner
, name
, sym
);
124 printf("%d new state. name='%s' [%s] %s\n",
125 get_lineno(), name
, check_name(owner
), show_state(state
));
127 printf("%d state change name='%s' [%s] %s => %s\n",
128 get_lineno(), name
, check_name(owner
), show_state(s
),
132 if (owner
!= -1 && unreachable())
135 if (fake_cur_stree_stack
)
136 set_state_stree_stack(&fake_cur_stree_stack
, owner
, name
, sym
, state
);
138 ret
= set_state_stree(&cur_stree
, owner
, name
, sym
, state
);
140 if (cond_true_stack
) {
141 set_state_stree_stack(&cond_true_stack
, owner
, name
, sym
, state
);
142 set_state_stree_stack(&cond_false_stack
, owner
, name
, sym
, state
);
147 struct sm_state
*set_state_expr(int owner
, struct expression
*expr
, struct smatch_state
*state
)
151 struct sm_state
*ret
= NULL
;
153 expr
= strip_expr(expr
);
154 name
= expr_to_var_sym(expr
, &sym
);
157 ret
= set_state(owner
, name
, sym
, state
);
163 void __push_fake_cur_slist()
165 push_stree(&fake_cur_stree_stack
, NULL
);
166 __save_pre_cond_states();
169 struct AVL
*__pop_fake_cur_slist()
171 __use_pre_cond_states();
172 return pop_stree(&fake_cur_stree_stack
);
175 void __free_fake_cur_slist()
179 __use_pre_cond_states();
180 stree
= pop_stree(&fake_cur_stree_stack
);
184 void __set_fake_cur_slist_fast(struct state_list
*slist
)
186 push_stree(&pre_cond_stack
, cur_stree
);
187 cur_stree
= slist_to_stree(slist
);
191 void __set_fake_cur_stree_fast(struct AVL
*stree
)
193 push_stree(&pre_cond_stack
, cur_stree
);
198 void __pop_fake_cur_slist_fast()
200 cur_stree
= pop_stree(&pre_cond_stack
);
204 void __pop_fake_cur_stree_fast()
206 cur_stree
= pop_stree(&pre_cond_stack
);
210 void __merge_slist_into_cur(struct state_list
*slist
)
213 struct sm_state
*orig
;
214 struct sm_state
*merged
;
216 FOR_EACH_PTR(slist
, sm
) {
217 orig
= get_sm_state(sm
->owner
, sm
->name
, sm
->sym
);
219 merged
= merge_sm_states(orig
, sm
);
223 } END_FOR_EACH_PTR(sm
);
226 void __set_sm(struct sm_state
*sm
)
229 sm_msg("Smatch Internal Error: cur_stree is read only.");
232 strcmp(check_name(sm
->owner
), option_debug_check
) == 0) {
233 struct smatch_state
*s
;
235 s
= get_state(sm
->owner
, sm
->name
, sm
->sym
);
237 printf("%d new state. name='%s' [%s] %s\n",
238 get_lineno(), sm
->name
, check_name(sm
->owner
),
239 show_state(sm
->state
));
241 printf("%d state change name='%s' [%s] %s => %s\n",
242 get_lineno(), sm
->name
, check_name(sm
->owner
), show_state(s
),
243 show_state(sm
->state
));
249 if (fake_cur_stree_stack
)
250 overwrite_sm_state_stree_stack(&fake_cur_stree_stack
, sm
);
252 overwrite_sm_state_stree(&cur_stree
, sm
);
254 if (cond_true_stack
) {
255 overwrite_sm_state_stree_stack(&cond_true_stack
, sm
);
256 overwrite_sm_state_stree_stack(&cond_false_stack
, sm
);
260 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
)
262 return get_state_stree(cur_stree
, owner
, name
, sym
);
265 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
)
269 struct smatch_state
*ret
= NULL
;
271 expr
= strip_expr(expr
);
272 name
= expr_to_var_sym(expr
, &sym
);
275 ret
= get_state(owner
, name
, sym
);
281 struct state_list
*get_possible_states(int owner
, const char *name
, struct symbol
*sym
)
283 struct sm_state
*sms
;
285 sms
= get_sm_state_stree(cur_stree
, owner
, name
, sym
);
287 return sms
->possible
;
291 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
)
295 struct state_list
*ret
= NULL
;
297 expr
= strip_expr(expr
);
298 name
= expr_to_var_sym(expr
, &sym
);
301 ret
= get_possible_states(owner
, name
, sym
);
307 struct sm_state
*get_sm_state(int owner
, const char *name
, struct symbol
*sym
)
309 return get_sm_state_stree(cur_stree
, owner
, name
, sym
);
312 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
)
316 struct sm_state
*ret
= NULL
;
318 expr
= strip_expr(expr
);
319 name
= expr_to_var_sym(expr
, &sym
);
322 ret
= get_sm_state(owner
, name
, sym
);
328 void delete_state(int owner
, const char *name
, struct symbol
*sym
)
330 delete_state_stree(&cur_stree
, owner
, name
, sym
);
331 if (cond_true_stack
) {
332 delete_state_stree_stack(&pre_cond_stack
, owner
, name
, sym
);
333 delete_state_stree_stack(&cond_true_stack
, owner
, name
, sym
);
334 delete_state_stree_stack(&cond_false_stack
, owner
, name
, sym
);
338 void delete_state_expr(int owner
, struct expression
*expr
)
343 expr
= strip_expr(expr
);
344 name
= expr_to_var_sym(expr
, &sym
);
347 delete_state(owner
, name
, sym
);
352 struct state_list
*get_all_states_slist(int owner
, struct state_list
*source
)
354 struct state_list
*slist
= NULL
;
355 struct sm_state
*tmp
;
357 FOR_EACH_PTR(source
, tmp
) {
358 if (tmp
->owner
== owner
)
359 add_ptr_list(&slist
, tmp
);
360 } END_FOR_EACH_PTR(tmp
);
365 struct state_list
*get_all_states(int owner
)
367 struct state_list
*slist
= NULL
;
368 struct sm_state
*tmp
;
370 FOR_EACH_SM(cur_stree
, tmp
) {
371 if (tmp
->owner
== owner
)
372 add_ptr_list(&slist
, tmp
);
373 } END_FOR_EACH_SM(tmp
);
378 struct AVL
*get_all_states_from_stree(int owner
, struct AVL
*source
)
380 struct AVL
*ret
= NULL
;
381 struct sm_state
*tmp
;
383 FOR_EACH_SM(source
, tmp
) {
384 if (tmp
->owner
== owner
)
385 avl_insert(&ret
, tmp
);
386 } END_FOR_EACH_SM(tmp
);
391 struct AVL
*get_all_states_stree(int owner
)
393 return get_all_states_from_stree(owner
, cur_stree
);
396 struct state_list
*__get_cur_slist(void)
398 return stree_to_slist(cur_stree
);
401 struct AVL
*__get_cur_stree(void)
406 int is_reachable(void)
413 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
414 struct smatch_state
*true_state
,
415 struct smatch_state
*false_state
)
417 if (option_debug
|| strcmp(check_name(owner
), option_debug_check
) == 0) {
418 struct smatch_state
*tmp
;
420 tmp
= get_state(owner
, name
, sym
);
421 printf("%d set_true_false '%s'. Was %s. Now T:%s F:%s\n",
422 get_lineno(), name
, show_state(tmp
),
423 show_state(true_state
), show_state(false_state
));
429 if (!cond_false_stack
|| !cond_true_stack
) {
430 printf("Error: missing true/false stacks\n");
435 set_state_stree(&cur_stree
, owner
, name
, sym
, true_state
);
436 set_state_stree_stack(&cond_true_stack
, owner
, name
, sym
, true_state
);
439 set_state_stree_stack(&cond_false_stack
, owner
, name
, sym
, false_state
);
442 void set_true_false_states_expr(int owner
, struct expression
*expr
,
443 struct smatch_state
*true_state
,
444 struct smatch_state
*false_state
)
449 expr
= strip_expr(expr
);
450 name
= expr_to_var_sym(expr
, &sym
);
453 set_true_false_states(owner
, name
, sym
, true_state
, false_state
);
458 void __set_true_false_sm(struct sm_state
*true_sm
, struct sm_state
*false_sm
)
463 if (!cond_false_stack
|| !cond_true_stack
) {
464 printf("Error: missing true/false stacks\n");
469 overwrite_sm_state_stree(&cur_stree
, true_sm
);
470 overwrite_sm_state_stree_stack(&cond_true_stack
, true_sm
);
473 overwrite_sm_state_stree_stack(&cond_false_stack
, false_sm
);
476 void nullify_path(void)
478 free_stree(&cur_stree
);
481 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
488 * At the start of every function we mark the path
489 * as unnull. That way there is always at least one state
490 * in the cur_stree until nullify_path is called. This
491 * is used in merge_slist() for the first null check.
493 void __unnullify_path(void)
495 set_state(-1, "unnull_path", NULL
, &true_state
);
498 int __path_is_null(void)
505 static void check_stree_stack_free(struct stree_stack
**stack
)
508 sm_msg("smatch internal error: stack not empty");
509 free_stack_and_strees(stack
);
513 void save_all_states(void)
515 __add_ptr_list(&backup
, cur_stree
, 0);
517 __add_ptr_list(&backup
, true_stack
, 0);
518 __add_ptr_list(&backup
, false_stack
, 0);
519 __add_ptr_list(&backup
, pre_cond_stack
, 0);
521 __add_ptr_list(&backup
, cond_true_stack
, 0);
522 __add_ptr_list(&backup
, cond_false_stack
, 0);
524 __add_ptr_list(&backup
, fake_cur_stree_stack
, 0);
526 __add_ptr_list(&backup
, break_stack
, 0);
527 __add_ptr_list(&backup
, switch_stack
, 0);
528 __add_ptr_list(&backup
, remaining_cases
, 0);
529 __add_ptr_list(&backup
, default_stack
, 0);
530 __add_ptr_list(&backup
, continue_stack
, 0);
532 __add_ptr_list(&backup
, goto_stack
, 0);
535 void nullify_all_states(void)
537 /* FIXME: These should call free_stree() */
542 pre_cond_stack
= NULL
;
544 cond_true_stack
= NULL
;
545 cond_false_stack
= NULL
;
547 fake_cur_stree_stack
= NULL
;
551 remaining_cases
= NULL
;
552 default_stack
= NULL
;
553 continue_stack
= NULL
;
558 static void *pop_backup(void)
562 ret
= last_ptr_list(backup
);
563 delete_ptr_list_last(&backup
);
567 void restore_all_states(void)
569 goto_stack
= pop_backup();
571 continue_stack
= pop_backup();
572 default_stack
= pop_backup();
573 remaining_cases
= pop_backup();
574 switch_stack
= pop_backup();
575 break_stack
= pop_backup();
577 fake_cur_stree_stack
= pop_backup();
579 cond_false_stack
= pop_backup();
580 cond_true_stack
= pop_backup();
582 pre_cond_stack
= pop_backup();
583 false_stack
= pop_backup();
584 true_stack
= pop_backup();
586 cur_stree
= pop_backup();
589 void clear_all_states(void)
591 struct named_stree
*named_stree
;
594 check_stree_stack_free(&true_stack
);
595 check_stree_stack_free(&false_stack
);
596 check_stree_stack_free(&pre_cond_stack
);
597 check_stree_stack_free(&cond_true_stack
);
598 check_stree_stack_free(&cond_false_stack
);
599 check_stree_stack_free(&break_stack
);
600 check_stree_stack_free(&switch_stack
);
601 check_stree_stack_free(&continue_stack
);
602 free_stack_and_slists(&implied_pools
);
604 FOR_EACH_PTR(goto_stack
, named_stree
) {
605 free_stree(&named_stree
->stree
);
606 } END_FOR_EACH_PTR(named_stree
);
607 __free_ptr_list((struct ptr_list
**)&goto_stack
);
608 free_every_single_sm_state();
611 void __push_cond_stacks(void)
613 push_stree(&cond_true_stack
, NULL
);
614 push_stree(&cond_false_stack
, NULL
);
617 struct AVL
*__copy_cond_true_states(void)
621 ret
= pop_stree(&cond_true_stack
);
622 push_stree(&cond_true_stack
, clone_stree(ret
));
626 struct AVL
*__copy_cond_false_states(void)
630 ret
= pop_stree(&cond_false_stack
);
631 push_stree(&cond_false_stack
, clone_stree(ret
));
635 struct AVL
*__pop_cond_true_stack(void)
637 return pop_stree(&cond_true_stack
);
640 struct AVL
*__pop_cond_false_stack(void)
642 return pop_stree(&cond_false_stack
);
646 * This combines the pre cond states with either the true or false states.
648 * a = kmalloc() ; if (a !! foo(a)
649 * In the pre state a is possibly null. In the true state it is non null.
650 * In the false state it is null. Combine the pre and the false to get
651 * that when we call 'foo', 'a' is null.
653 static void __use_cond_stack(struct stree_stack
**stack
)
657 free_stree(&cur_stree
);
659 cur_stree
= pop_stree(&pre_cond_stack
);
660 push_stree(&pre_cond_stack
, clone_stree(cur_stree
));
662 stree
= pop_stree(stack
);
663 overwrite_stree(stree
, &cur_stree
);
664 push_stree(stack
, stree
);
667 void __use_pre_cond_states(void)
669 free_stree(&cur_stree
);
670 cur_stree
= pop_stree(&pre_cond_stack
);
673 void __use_cond_true_states(void)
675 __use_cond_stack(&cond_true_stack
);
678 void __use_cond_false_states(void)
680 __use_cond_stack(&cond_false_stack
);
683 void __negate_cond_stacks(void)
685 struct AVL
*old_false
, *old_true
;
687 __use_cond_stack(&cond_false_stack
);
688 old_false
= pop_stree(&cond_false_stack
);
689 old_true
= pop_stree(&cond_true_stack
);
690 push_stree(&cond_false_stack
, old_true
);
691 push_stree(&cond_true_stack
, old_false
);
694 void __and_cond_states(void)
696 and_stree_stack(&cond_true_stack
);
697 or_stree_stack(&pre_cond_stack
, cur_stree
, &cond_false_stack
);
700 void __or_cond_states(void)
702 or_stree_stack(&pre_cond_stack
, cur_stree
, &cond_true_stack
);
703 and_stree_stack(&cond_false_stack
);
706 void __save_pre_cond_states(void)
708 push_stree(&pre_cond_stack
, clone_stree(cur_stree
));
711 void __discard_pre_cond_states(void)
715 tmp
= pop_stree(&pre_cond_stack
);
719 void __use_cond_states(void)
721 struct AVL
*pre
, *pre_clone
, *true_states
, *false_states
;
723 pre
= pop_stree(&pre_cond_stack
);
724 pre_clone
= clone_stree(pre
);
726 true_states
= pop_stree(&cond_true_stack
);
727 overwrite_stree(true_states
, &pre
);
728 /* we use the true states right away */
729 free_stree(&cur_stree
);
732 false_states
= pop_stree(&cond_false_stack
);
733 overwrite_stree(false_states
, &pre_clone
);
734 push_stree(&false_stack
, pre_clone
);
737 void __push_true_states(void)
739 push_stree(&true_stack
, clone_stree(cur_stree
));
742 void __use_false_states(void)
744 free_stree(&cur_stree
);
745 cur_stree
= pop_stree(&false_stack
);
748 void __discard_false_states(void)
752 stree
= pop_stree(&false_stack
);
756 void __merge_false_states(void)
760 stree
= pop_stree(&false_stack
);
761 merge_stree(&cur_stree
, stree
);
765 void __merge_true_states(void)
769 stree
= pop_stree(&true_stack
);
770 merge_stree(&cur_stree
, stree
);
774 void __push_continues(void)
776 push_stree(&continue_stack
, NULL
);
779 void __discard_continues(void)
783 stree
= pop_stree(&continue_stack
);
787 void __process_continues(void)
791 stree
= pop_stree(&continue_stack
);
793 stree
= clone_stree(cur_stree
);
795 merge_stree(&stree
, cur_stree
);
797 push_stree(&continue_stack
, stree
);
800 static int top_stree_empty(struct stree_stack
**stack
)
805 tmp
= pop_stree(stack
);
808 push_stree(stack
, tmp
);
812 /* a silly loop does this: while(i--) { return; } */
813 void __warn_on_silly_pre_loops(void)
815 if (!__path_is_null())
817 if (!top_stree_empty(&continue_stack
))
819 if (!top_stree_empty(&break_stack
))
821 /* if the path was nullified before the loop, then we already
822 printed an error earlier */
823 if (top_stree_empty(&false_stack
))
825 sm_msg("info: loop could be replaced with if statement.");
828 void __merge_continues(void)
832 stree
= pop_stree(&continue_stack
);
833 merge_stree(&cur_stree
, stree
);
837 void __push_breaks(void)
839 push_stree(&break_stack
, NULL
);
842 void __process_breaks(void)
846 stree
= pop_stree(&break_stack
);
848 stree
= clone_stree(cur_stree
);
850 merge_stree(&stree
, cur_stree
);
852 push_stree(&break_stack
, stree
);
855 int __has_breaks(void)
860 stree
= pop_stree(&break_stack
);
862 push_stree(&break_stack
, stree
);
866 void __merge_breaks(void)
870 stree
= pop_stree(&break_stack
);
871 merge_stree(&cur_stree
, stree
);
875 void __use_breaks(void)
877 free_stree(&cur_stree
);
878 cur_stree
= pop_stree(&break_stack
);
881 void __save_switch_states(struct expression
*switch_expr
)
883 push_rl(&remaining_cases
, __get_implied_values(switch_expr
));
884 push_stree(&switch_stack
, clone_stree(cur_stree
));
887 void __merge_switches(struct expression
*switch_expr
, struct expression
*case_expr
)
890 struct state_list
*implied_slist
;
892 stree
= pop_stree(&switch_stack
);
893 implied_slist
= __implied_case_slist(switch_expr
, case_expr
, &remaining_cases
, &stree
);
894 merge_stree(&cur_stree
, slist_to_stree(implied_slist
));
895 free_slist(&implied_slist
);
896 push_stree(&switch_stack
, stree
);
899 void __discard_switches(void)
903 pop_rl(&remaining_cases
);
904 stree
= pop_stree(&switch_stack
);
908 void __push_default(void)
910 push_stree(&default_stack
, NULL
);
913 void __set_default(void)
915 set_state_stree_stack(&default_stack
, 0, "has_default", NULL
, &true_state
);
918 int __pop_default(void)
922 stree
= pop_stree(&default_stack
);
930 static struct named_stree
*alloc_named_stree(const char *name
, struct AVL
*stree
)
932 struct named_stree
*named_stree
= __alloc_named_stree(0);
934 named_stree
->name
= (char *)name
;
935 named_stree
->stree
= stree
;
939 void __save_gotos(const char *name
)
944 stree
= get_named_stree(goto_stack
, name
);
946 merge_stree(stree
, cur_stree
);
949 struct named_stree
*named_stree
;
951 clone
= clone_stree(cur_stree
);
952 named_stree
= alloc_named_stree(name
, clone
);
953 add_ptr_list(&goto_stack
, named_stree
);
957 void __merge_gotos(const char *name
)
961 stree
= get_named_stree(goto_stack
, name
);
963 merge_stree(&cur_stree
, *stree
);