kunmap: update validation file
[smatch.git] / smatch_states.c
blobe39fd7cc4a85fcf67430ebc486af35e8efe40e36
1 /*
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
22 * on stacks.
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
26 * of the stacks.
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.
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include "smatch.h"
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;
55 static int read_only;
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 int option_debug;
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)
80 return 1;
81 return 0;
82 } END_FOR_EACH_PTR_REVERSE(tmp);
83 return 0;
86 int unreachable(void)
88 static int reset_warnings = 1;
90 if (cur_stree) {
91 if (!__inline_fn)
92 reset_warnings = 1;
93 return 0;
96 if (in_declarations_bit())
97 return 1;
99 /* option spammy turns on a noisier version of this */
100 if (reset_warnings && !option_spammy)
101 sm_msg("info: ignoring unreachable code.");
102 if (!__inline_fn)
103 reset_warnings = 0;
104 return 1;
107 struct sm_state *set_state(int owner, const char *name, struct symbol *sym, struct smatch_state *state)
109 struct sm_state *ret;
111 if (!name)
112 return NULL;
114 if (read_only)
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);
121 if (!s)
122 printf("%d new state. name='%s' [%s] %s\n",
123 get_lineno(), name, check_name(owner), show_state(state));
124 else
125 printf("%d state change name='%s' [%s] %s => %s\n",
126 get_lineno(), name, check_name(owner), show_state(s),
127 show_state(state));
130 if (owner != -1 && unreachable())
131 return NULL;
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);
142 return ret;
145 struct sm_state *set_state_expr(int owner, struct expression *expr, struct smatch_state *state)
147 char *name;
148 struct symbol *sym;
149 struct sm_state *ret = NULL;
151 expr = strip_expr(expr);
152 name = expr_to_var_sym(expr, &sym);
153 if (!name || !sym)
154 goto free;
155 ret = set_state(owner, name, sym, state);
156 free:
157 free_string(name);
158 return ret;
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()
175 struct stree *stree;
177 __use_pre_cond_states();
178 stree = pop_stree(&fake_cur_stree_stack);
179 free_stree(&stree);
182 void __set_fake_cur_stree_fast(struct stree *stree)
184 push_stree(&pre_cond_stack, cur_stree);
185 cur_stree = stree;
186 read_only = 1;
189 void __pop_fake_cur_stree_fast()
191 cur_stree = pop_stree(&pre_cond_stack);
192 read_only = 0;
195 void __merge_stree_into_cur(struct stree *stree)
197 struct sm_state *sm;
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);
203 if (orig)
204 merged = merge_sm_states(orig, sm);
205 else
206 merged = sm;
207 __set_sm(merged);
208 } END_FOR_EACH_SM(sm);
211 void __set_sm(struct sm_state *sm)
213 if (read_only)
214 sm_msg("Smatch Internal Error: cur_stree is read only.");
216 if (option_debug ||
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);
221 if (!s)
222 printf("%d new state. name='%s' [%s] %s\n",
223 get_lineno(), sm->name, check_name(sm->owner),
224 show_state(sm->state));
225 else
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));
231 if (unreachable())
232 return;
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)
252 char *name;
253 struct symbol *sym;
254 struct smatch_state *ret = NULL;
256 expr = strip_expr(expr);
257 name = expr_to_var_sym(expr, &sym);
258 if (!name || !sym)
259 goto free;
260 ret = get_state(owner, name, sym);
261 free:
262 free_string(name);
263 return ret;
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);
271 if (sms)
272 return sms->possible;
273 return NULL;
276 struct state_list *get_possible_states_expr(int owner, struct expression *expr)
278 char *name;
279 struct symbol *sym;
280 struct state_list *ret = NULL;
282 expr = strip_expr(expr);
283 name = expr_to_var_sym(expr, &sym);
284 if (!name || !sym)
285 goto free;
286 ret = get_possible_states(owner, name, sym);
287 free:
288 free_string(name);
289 return ret;
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)
299 char *name;
300 struct symbol *sym;
301 struct sm_state *ret = NULL;
303 expr = strip_expr(expr);
304 name = expr_to_var_sym(expr, &sym);
305 if (!name || !sym)
306 goto free;
307 ret = get_sm_state(owner, name, sym);
308 free:
309 free_string(name);
310 return ret;
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)
325 char *name;
326 struct symbol *sym;
328 expr = strip_expr(expr);
329 name = expr_to_var_sym(expr, &sym);
330 if (!name || !sym)
331 goto free;
332 delete_state(owner, name, sym);
333 free:
334 free_string(name);
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);
347 return ret;
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)
357 return cur_stree;
360 int is_reachable(void)
362 if (cur_stree)
363 return 1;
364 return 0;
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));
380 if (unreachable())
381 return;
383 if (!cond_false_stack || !cond_true_stack) {
384 printf("Error: missing true/false stacks\n");
385 return;
388 if (true_state) {
389 set_state_stree(&cur_stree, owner, name, sym, true_state);
390 set_state_stree_stack(&cond_true_stack, owner, name, sym, true_state);
392 if (false_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)
400 char *name;
401 struct symbol *sym;
403 expr = strip_expr(expr);
404 name = expr_to_var_sym(expr, &sym);
405 if (!name || !sym)
406 goto free;
407 set_true_false_states(owner, name, sym, true_state, false_state);
408 free:
409 free_string(name);
412 void __set_true_false_sm(struct sm_state *true_sm, struct sm_state *false_sm)
414 if (unreachable())
415 return;
417 if (!cond_false_stack || !cond_true_stack) {
418 printf("Error: missing true/false stacks\n");
419 return;
422 if (true_sm) {
423 overwrite_sm_state_stree(&cur_stree, true_sm);
424 overwrite_sm_state_stree_stack(&cond_true_stack, true_sm);
426 if (false_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,
436 void *unused)
438 nullify_path();
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)
454 if (cur_stree)
455 return 0;
456 return 1;
459 static void check_stree_stack_free(struct stree_stack **stack)
461 if (*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() */
492 cur_stree = NULL;
494 true_stack = NULL;
495 false_stack = NULL;
496 pre_cond_stack = NULL;
498 cond_true_stack = NULL;
499 cond_false_stack = NULL;
501 fake_cur_stree_stack = NULL;
503 break_stack = NULL;
504 switch_stack = NULL;
505 remaining_cases = NULL;
506 default_stack = NULL;
507 continue_stack = NULL;
509 goto_stack = NULL;
512 static void *pop_backup(void)
514 void *ret;
516 ret = last_ptr_list(backup);
517 delete_ptr_list_last(&backup);
518 return ret;
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;
547 nullify_path();
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)
572 struct stree *ret;
574 ret = pop_stree(&cond_true_stack);
575 push_stree(&cond_true_stack, clone_stree(ret));
576 return ret;
579 struct stree *__copy_cond_false_states(void)
581 struct stree *ret;
583 ret = pop_stree(&cond_false_stack);
584 push_stree(&cond_false_stack, clone_stree(ret));
585 return 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.
600 * For example:
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)
608 struct stree *stree;
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)
666 struct stree *tmp;
668 tmp = pop_stree(&pre_cond_stack);
669 free_stree(&tmp);
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);
683 cur_stree = pre;
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)
703 struct stree *stree;
705 stree = pop_stree(&false_stack);
706 free_stree(&stree);
709 void __merge_false_states(void)
711 struct stree *stree;
713 stree = pop_stree(&false_stack);
714 merge_stree(&cur_stree, stree);
715 free_stree(&stree);
718 void __merge_true_states(void)
720 struct stree *stree;
722 stree = pop_stree(&true_stack);
723 merge_stree(&cur_stree, stree);
724 free_stree(&stree);
727 void __push_continues(void)
729 push_stree(&continue_stack, NULL);
732 void __discard_continues(void)
734 struct stree *stree;
736 stree = pop_stree(&continue_stack);
737 free_stree(&stree);
740 void __process_continues(void)
742 struct stree *stree;
744 stree = pop_stree(&continue_stack);
745 if (!stree)
746 stree = clone_stree(cur_stree);
747 else
748 merge_stree(&stree, cur_stree);
750 push_stree(&continue_stack, stree);
753 static int top_stree_empty(struct stree_stack **stack)
755 struct stree *tmp;
756 int empty = 0;
758 tmp = pop_stree(stack);
759 if (!tmp)
760 empty = 1;
761 push_stree(stack, tmp);
762 return empty;
765 /* a silly loop does this: while(i--) { return; } */
766 void __warn_on_silly_pre_loops(void)
768 if (!__path_is_null())
769 return;
770 if (!top_stree_empty(&continue_stack))
771 return;
772 if (!top_stree_empty(&break_stack))
773 return;
774 /* if the path was nullified before the loop, then we already
775 printed an error earlier */
776 if (top_stree_empty(&false_stack))
777 return;
778 sm_msg("info: loop could be replaced with if statement.");
781 void __merge_continues(void)
783 struct stree *stree;
785 stree = pop_stree(&continue_stack);
786 merge_stree(&cur_stree, stree);
787 free_stree(&stree);
790 void __push_breaks(void)
792 push_stree(&break_stack, NULL);
795 void __process_breaks(void)
797 struct stree *stree;
799 stree = pop_stree(&break_stack);
800 if (!stree)
801 stree = clone_stree(cur_stree);
802 else
803 merge_stree(&stree, cur_stree);
805 push_stree(&break_stack, stree);
808 int __has_breaks(void)
810 struct stree *stree;
811 int ret;
813 stree = pop_stree(&break_stack);
814 ret = !!stree;
815 push_stree(&break_stack, stree);
816 return ret;
819 void __merge_breaks(void)
821 struct stree *stree;
823 stree = pop_stree(&break_stack);
824 merge_stree(&cur_stree, stree);
825 free_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)
842 struct stree *stree;
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)
854 struct stree *stree;
856 pop_rl(&remaining_cases);
857 stree = pop_stree(&switch_stack);
858 free_stree(&stree);
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)
873 struct stree *stree;
875 stree = pop_stree(&default_stack);
876 if (stree) {
877 free_stree(&stree);
878 return 1;
880 return 0;
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;
889 return named_stree;
892 void __save_gotos(const char *name)
894 struct stree **stree;
895 struct stree *clone;
897 stree = get_named_stree(goto_stack, name);
898 if (stree) {
899 merge_stree(stree, cur_stree);
900 return;
901 } else {
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);
915 if (stree)
916 merge_stree(&cur_stree, *stree);