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 stree
*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
;
69 void __print_cur_stree(void)
71 __print_stree(cur_stree
);
74 static int in_declarations_bit(void)
76 struct statement
*tmp
;
78 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
79 if (tmp
->type
== STMT_DECLARATION
)
82 } END_FOR_EACH_PTR_REVERSE(tmp
);
88 static int reset_warnings
= 1;
96 if (in_declarations_bit())
99 /* option spammy turns on a noisier version of this */
100 if (reset_warnings
&& !option_spammy
)
101 sm_msg("info: ignoring unreachable code.");
107 struct sm_state
*set_state(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
109 struct sm_state
*ret
;
115 sm_msg("Smatch Internal Error: cur_stree is read only.");
117 if (option_debug
|| strcmp(check_name(owner
), option_debug_check
) == 0) {
118 struct smatch_state
*s
;
120 s
= get_state(owner
, name
, sym
);
122 printf("%d new state. name='%s' [%s] %s\n",
123 get_lineno(), name
, check_name(owner
), show_state(state
));
125 printf("%d state change name='%s' [%s] %s => %s\n",
126 get_lineno(), name
, check_name(owner
), show_state(s
),
130 if (owner
!= -1 && unreachable())
133 if (fake_cur_stree_stack
)
134 set_state_stree_stack(&fake_cur_stree_stack
, owner
, name
, sym
, state
);
136 ret
= set_state_stree(&cur_stree
, owner
, name
, sym
, state
);
138 if (cond_true_stack
) {
139 set_state_stree_stack(&cond_true_stack
, owner
, name
, sym
, state
);
140 set_state_stree_stack(&cond_false_stack
, owner
, name
, sym
, state
);
145 struct sm_state
*set_state_expr(int owner
, struct expression
*expr
, struct smatch_state
*state
)
149 struct sm_state
*ret
= NULL
;
151 expr
= strip_expr(expr
);
152 name
= expr_to_var_sym(expr
, &sym
);
155 ret
= set_state(owner
, name
, sym
, state
);
161 void __push_fake_cur_stree()
163 push_stree(&fake_cur_stree_stack
, NULL
);
164 __save_pre_cond_states();
167 struct stree
*__pop_fake_cur_stree()
169 __use_pre_cond_states();
170 return pop_stree(&fake_cur_stree_stack
);
173 void __free_fake_cur_stree()
177 __use_pre_cond_states();
178 stree
= pop_stree(&fake_cur_stree_stack
);
182 void __set_fake_cur_stree_fast(struct stree
*stree
)
184 push_stree(&pre_cond_stack
, cur_stree
);
189 void __pop_fake_cur_stree_fast()
191 cur_stree
= pop_stree(&pre_cond_stack
);
195 void __merge_stree_into_cur(struct stree
*stree
)
198 struct sm_state
*orig
;
199 struct sm_state
*merged
;
201 FOR_EACH_SM(stree
, sm
) {
202 orig
= get_sm_state(sm
->owner
, sm
->name
, sm
->sym
);
204 merged
= merge_sm_states(orig
, sm
);
208 } END_FOR_EACH_SM(sm
);
211 void __set_sm(struct sm_state
*sm
)
214 sm_msg("Smatch Internal Error: cur_stree is read only.");
217 strcmp(check_name(sm
->owner
), option_debug_check
) == 0) {
218 struct smatch_state
*s
;
220 s
= get_state(sm
->owner
, sm
->name
, sm
->sym
);
222 printf("%d new state. name='%s' [%s] %s\n",
223 get_lineno(), sm
->name
, check_name(sm
->owner
),
224 show_state(sm
->state
));
226 printf("%d state change name='%s' [%s] %s => %s\n",
227 get_lineno(), sm
->name
, check_name(sm
->owner
), show_state(s
),
228 show_state(sm
->state
));
234 if (fake_cur_stree_stack
)
235 overwrite_sm_state_stree_stack(&fake_cur_stree_stack
, sm
);
237 overwrite_sm_state_stree(&cur_stree
, sm
);
239 if (cond_true_stack
) {
240 overwrite_sm_state_stree_stack(&cond_true_stack
, sm
);
241 overwrite_sm_state_stree_stack(&cond_false_stack
, sm
);
245 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
)
247 return get_state_stree(cur_stree
, owner
, name
, sym
);
250 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
)
254 struct smatch_state
*ret
= NULL
;
256 expr
= strip_expr(expr
);
257 name
= expr_to_var_sym(expr
, &sym
);
260 ret
= get_state(owner
, name
, sym
);
266 struct state_list
*get_possible_states(int owner
, const char *name
, struct symbol
*sym
)
268 struct sm_state
*sms
;
270 sms
= get_sm_state_stree(cur_stree
, owner
, name
, sym
);
272 return sms
->possible
;
276 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
)
280 struct state_list
*ret
= NULL
;
282 expr
= strip_expr(expr
);
283 name
= expr_to_var_sym(expr
, &sym
);
286 ret
= get_possible_states(owner
, name
, sym
);
292 struct sm_state
*get_sm_state(int owner
, const char *name
, struct symbol
*sym
)
294 return get_sm_state_stree(cur_stree
, owner
, name
, sym
);
297 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
)
301 struct sm_state
*ret
= NULL
;
303 expr
= strip_expr(expr
);
304 name
= expr_to_var_sym(expr
, &sym
);
307 ret
= get_sm_state(owner
, name
, sym
);
313 void delete_state(int owner
, const char *name
, struct symbol
*sym
)
315 delete_state_stree(&cur_stree
, owner
, name
, sym
);
316 if (cond_true_stack
) {
317 delete_state_stree_stack(&pre_cond_stack
, owner
, name
, sym
);
318 delete_state_stree_stack(&cond_true_stack
, owner
, name
, sym
);
319 delete_state_stree_stack(&cond_false_stack
, owner
, name
, sym
);
323 void delete_state_expr(int owner
, struct expression
*expr
)
328 expr
= strip_expr(expr
);
329 name
= expr_to_var_sym(expr
, &sym
);
332 delete_state(owner
, name
, sym
);
337 struct stree
*get_all_states_from_stree(int owner
, struct stree
*source
)
339 struct stree
*ret
= NULL
;
340 struct sm_state
*tmp
;
342 FOR_EACH_SM(source
, tmp
) {
343 if (tmp
->owner
== owner
)
344 avl_insert(&ret
, tmp
);
345 } END_FOR_EACH_SM(tmp
);
350 struct stree
*get_all_states_stree(int owner
)
352 return get_all_states_from_stree(owner
, cur_stree
);
355 struct stree
*__get_cur_stree(void)
360 int is_reachable(void)
367 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
368 struct smatch_state
*true_state
,
369 struct smatch_state
*false_state
)
371 if (option_debug
|| strcmp(check_name(owner
), option_debug_check
) == 0) {
372 struct smatch_state
*tmp
;
374 tmp
= get_state(owner
, name
, sym
);
375 printf("%d set_true_false '%s'. Was %s. Now T:%s F:%s\n",
376 get_lineno(), name
, show_state(tmp
),
377 show_state(true_state
), show_state(false_state
));
383 if (!cond_false_stack
|| !cond_true_stack
) {
384 printf("Error: missing true/false stacks\n");
389 set_state_stree(&cur_stree
, owner
, name
, sym
, true_state
);
390 set_state_stree_stack(&cond_true_stack
, owner
, name
, sym
, true_state
);
393 set_state_stree_stack(&cond_false_stack
, owner
, name
, sym
, false_state
);
396 void set_true_false_states_expr(int owner
, struct expression
*expr
,
397 struct smatch_state
*true_state
,
398 struct smatch_state
*false_state
)
403 expr
= strip_expr(expr
);
404 name
= expr_to_var_sym(expr
, &sym
);
407 set_true_false_states(owner
, name
, sym
, true_state
, false_state
);
412 void __set_true_false_sm(struct sm_state
*true_sm
, struct sm_state
*false_sm
)
417 if (!cond_false_stack
|| !cond_true_stack
) {
418 printf("Error: missing true/false stacks\n");
423 overwrite_sm_state_stree(&cur_stree
, true_sm
);
424 overwrite_sm_state_stree_stack(&cond_true_stack
, true_sm
);
427 overwrite_sm_state_stree_stack(&cond_false_stack
, false_sm
);
430 void nullify_path(void)
432 free_stree(&cur_stree
);
435 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
442 * At the start of every function we mark the path
443 * as unnull. That way there is always at least one state
444 * in the cur_stree until nullify_path is called. This
445 * is used in merge_slist() for the first null check.
447 void __unnullify_path(void)
449 set_state(-1, "unnull_path", NULL
, &true_state
);
452 int __path_is_null(void)
459 static void check_stree_stack_free(struct stree_stack
**stack
)
462 sm_msg("smatch internal error: stack not empty");
463 free_stack_and_strees(stack
);
467 void save_all_states(void)
469 __add_ptr_list(&backup
, cur_stree
, 0);
471 __add_ptr_list(&backup
, true_stack
, 0);
472 __add_ptr_list(&backup
, false_stack
, 0);
473 __add_ptr_list(&backup
, pre_cond_stack
, 0);
475 __add_ptr_list(&backup
, cond_true_stack
, 0);
476 __add_ptr_list(&backup
, cond_false_stack
, 0);
478 __add_ptr_list(&backup
, fake_cur_stree_stack
, 0);
480 __add_ptr_list(&backup
, break_stack
, 0);
481 __add_ptr_list(&backup
, switch_stack
, 0);
482 __add_ptr_list(&backup
, remaining_cases
, 0);
483 __add_ptr_list(&backup
, default_stack
, 0);
484 __add_ptr_list(&backup
, continue_stack
, 0);
486 __add_ptr_list(&backup
, goto_stack
, 0);
489 void nullify_all_states(void)
491 /* FIXME: These should call free_stree() */
496 pre_cond_stack
= NULL
;
498 cond_true_stack
= NULL
;
499 cond_false_stack
= NULL
;
501 fake_cur_stree_stack
= NULL
;
505 remaining_cases
= NULL
;
506 default_stack
= NULL
;
507 continue_stack
= NULL
;
512 static void *pop_backup(void)
516 ret
= last_ptr_list(backup
);
517 delete_ptr_list_last(&backup
);
521 void restore_all_states(void)
523 goto_stack
= pop_backup();
525 continue_stack
= pop_backup();
526 default_stack
= pop_backup();
527 remaining_cases
= pop_backup();
528 switch_stack
= pop_backup();
529 break_stack
= pop_backup();
531 fake_cur_stree_stack
= pop_backup();
533 cond_false_stack
= pop_backup();
534 cond_true_stack
= pop_backup();
536 pre_cond_stack
= pop_backup();
537 false_stack
= pop_backup();
538 true_stack
= pop_backup();
540 cur_stree
= pop_backup();
543 void clear_all_states(void)
545 struct named_stree
*named_stree
;
548 check_stree_stack_free(&true_stack
);
549 check_stree_stack_free(&false_stack
);
550 check_stree_stack_free(&pre_cond_stack
);
551 check_stree_stack_free(&cond_true_stack
);
552 check_stree_stack_free(&cond_false_stack
);
553 check_stree_stack_free(&break_stack
);
554 check_stree_stack_free(&switch_stack
);
555 check_stree_stack_free(&continue_stack
);
557 FOR_EACH_PTR(goto_stack
, named_stree
) {
558 free_stree(&named_stree
->stree
);
559 } END_FOR_EACH_PTR(named_stree
);
560 __free_ptr_list((struct ptr_list
**)&goto_stack
);
561 free_every_single_sm_state();
564 void __push_cond_stacks(void)
566 push_stree(&cond_true_stack
, NULL
);
567 push_stree(&cond_false_stack
, NULL
);
570 struct stree
*__copy_cond_true_states(void)
574 ret
= pop_stree(&cond_true_stack
);
575 push_stree(&cond_true_stack
, clone_stree(ret
));
579 struct stree
*__copy_cond_false_states(void)
583 ret
= pop_stree(&cond_false_stack
);
584 push_stree(&cond_false_stack
, clone_stree(ret
));
588 struct stree
*__pop_cond_true_stack(void)
590 return pop_stree(&cond_true_stack
);
593 struct stree
*__pop_cond_false_stack(void)
595 return pop_stree(&cond_false_stack
);
599 * This combines the pre cond states with either the true or false states.
601 * a = kmalloc() ; if (a !! foo(a)
602 * In the pre state a is possibly null. In the true state it is non null.
603 * In the false state it is null. Combine the pre and the false to get
604 * that when we call 'foo', 'a' is null.
606 static void __use_cond_stack(struct stree_stack
**stack
)
610 free_stree(&cur_stree
);
612 cur_stree
= pop_stree(&pre_cond_stack
);
613 push_stree(&pre_cond_stack
, clone_stree(cur_stree
));
615 stree
= pop_stree(stack
);
616 overwrite_stree(stree
, &cur_stree
);
617 push_stree(stack
, stree
);
620 void __use_pre_cond_states(void)
622 free_stree(&cur_stree
);
623 cur_stree
= pop_stree(&pre_cond_stack
);
626 void __use_cond_true_states(void)
628 __use_cond_stack(&cond_true_stack
);
631 void __use_cond_false_states(void)
633 __use_cond_stack(&cond_false_stack
);
636 void __negate_cond_stacks(void)
638 struct stree
*old_false
, *old_true
;
640 __use_cond_stack(&cond_false_stack
);
641 old_false
= pop_stree(&cond_false_stack
);
642 old_true
= pop_stree(&cond_true_stack
);
643 push_stree(&cond_false_stack
, old_true
);
644 push_stree(&cond_true_stack
, old_false
);
647 void __and_cond_states(void)
649 and_stree_stack(&cond_true_stack
);
650 or_stree_stack(&pre_cond_stack
, cur_stree
, &cond_false_stack
);
653 void __or_cond_states(void)
655 or_stree_stack(&pre_cond_stack
, cur_stree
, &cond_true_stack
);
656 and_stree_stack(&cond_false_stack
);
659 void __save_pre_cond_states(void)
661 push_stree(&pre_cond_stack
, clone_stree(cur_stree
));
664 void __discard_pre_cond_states(void)
668 tmp
= pop_stree(&pre_cond_stack
);
672 void __use_cond_states(void)
674 struct stree
*pre
, *pre_clone
, *true_states
, *false_states
;
676 pre
= pop_stree(&pre_cond_stack
);
677 pre_clone
= clone_stree(pre
);
679 true_states
= pop_stree(&cond_true_stack
);
680 overwrite_stree(true_states
, &pre
);
681 /* we use the true states right away */
682 free_stree(&cur_stree
);
685 false_states
= pop_stree(&cond_false_stack
);
686 overwrite_stree(false_states
, &pre_clone
);
687 push_stree(&false_stack
, pre_clone
);
690 void __push_true_states(void)
692 push_stree(&true_stack
, clone_stree(cur_stree
));
695 void __use_false_states(void)
697 free_stree(&cur_stree
);
698 cur_stree
= pop_stree(&false_stack
);
701 void __discard_false_states(void)
705 stree
= pop_stree(&false_stack
);
709 void __merge_false_states(void)
713 stree
= pop_stree(&false_stack
);
714 merge_stree(&cur_stree
, stree
);
718 void __merge_true_states(void)
722 stree
= pop_stree(&true_stack
);
723 merge_stree(&cur_stree
, stree
);
727 void __push_continues(void)
729 push_stree(&continue_stack
, NULL
);
732 void __discard_continues(void)
736 stree
= pop_stree(&continue_stack
);
740 void __process_continues(void)
744 stree
= pop_stree(&continue_stack
);
746 stree
= clone_stree(cur_stree
);
748 merge_stree(&stree
, cur_stree
);
750 push_stree(&continue_stack
, stree
);
753 static int top_stree_empty(struct stree_stack
**stack
)
758 tmp
= pop_stree(stack
);
761 push_stree(stack
, tmp
);
765 /* a silly loop does this: while(i--) { return; } */
766 void __warn_on_silly_pre_loops(void)
768 if (!__path_is_null())
770 if (!top_stree_empty(&continue_stack
))
772 if (!top_stree_empty(&break_stack
))
774 /* if the path was nullified before the loop, then we already
775 printed an error earlier */
776 if (top_stree_empty(&false_stack
))
778 sm_msg("info: loop could be replaced with if statement.");
781 void __merge_continues(void)
785 stree
= pop_stree(&continue_stack
);
786 merge_stree(&cur_stree
, stree
);
790 void __push_breaks(void)
792 push_stree(&break_stack
, NULL
);
795 void __process_breaks(void)
799 stree
= pop_stree(&break_stack
);
801 stree
= clone_stree(cur_stree
);
803 merge_stree(&stree
, cur_stree
);
805 push_stree(&break_stack
, stree
);
808 int __has_breaks(void)
813 stree
= pop_stree(&break_stack
);
815 push_stree(&break_stack
, stree
);
819 void __merge_breaks(void)
823 stree
= pop_stree(&break_stack
);
824 merge_stree(&cur_stree
, stree
);
828 void __use_breaks(void)
830 free_stree(&cur_stree
);
831 cur_stree
= pop_stree(&break_stack
);
834 void __save_switch_states(struct expression
*switch_expr
)
836 push_rl(&remaining_cases
, __get_implied_values(switch_expr
));
837 push_stree(&switch_stack
, clone_stree(cur_stree
));
840 void __merge_switches(struct expression
*switch_expr
, struct expression
*case_expr
)
843 struct stree
*implied_stree
;
845 stree
= pop_stree(&switch_stack
);
846 implied_stree
= __implied_case_stree(switch_expr
, case_expr
, &remaining_cases
, &stree
);
847 merge_stree(&cur_stree
, implied_stree
);
848 free_stree(&implied_stree
);
849 push_stree(&switch_stack
, stree
);
852 void __discard_switches(void)
856 pop_rl(&remaining_cases
);
857 stree
= pop_stree(&switch_stack
);
861 void __push_default(void)
863 push_stree(&default_stack
, NULL
);
866 void __set_default(void)
868 set_state_stree_stack(&default_stack
, 0, "has_default", NULL
, &true_state
);
871 int __pop_default(void)
875 stree
= pop_stree(&default_stack
);
883 static struct named_stree
*alloc_named_stree(const char *name
, struct stree
*stree
)
885 struct named_stree
*named_stree
= __alloc_named_stree(0);
887 named_stree
->name
= (char *)name
;
888 named_stree
->stree
= stree
;
892 void __save_gotos(const char *name
)
894 struct stree
**stree
;
897 stree
= get_named_stree(goto_stack
, name
);
899 merge_stree(stree
, cur_stree
);
902 struct named_stree
*named_stree
;
904 clone
= clone_stree(cur_stree
);
905 named_stree
= alloc_named_stree(name
, clone
);
906 add_ptr_list(&goto_stack
, named_stree
);
910 void __merge_gotos(const char *name
)
912 struct stree
**stree
;
914 stree
= get_named_stree(goto_stack
, name
);
916 merge_stree(&cur_stree
, *stree
);