avl: change some void pointers to struct sm_state pointers
[smatch.git] / smatch_states.c
blob3b5c6b778e6efcdb9462277549eb2e1680156f0e
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 state_list *cur_slist; /* current states */
47 static struct state_list_stack *true_stack; /* states after a t/f branch */
48 static struct state_list_stack *false_stack;
49 static struct state_list_stack *pre_cond_stack; /* states before a t/f branch */
51 static struct state_list_stack *cond_true_stack; /* states affected by a branch */
52 static struct state_list_stack *cond_false_stack;
54 static struct state_list_stack *fake_cur_slist_stack;
55 static int read_only;
57 static struct state_list_stack *break_stack;
58 static struct state_list_stack *switch_stack;
59 static struct range_list_stack *remaining_cases;
60 static struct state_list_stack *default_stack;
61 static struct state_list_stack *continue_stack;
63 static struct named_stack *goto_stack;
65 static struct ptr_list *backup;
67 struct state_list_stack *implied_pools;
69 int option_debug;
71 void __print_cur_slist(void)
73 __print_slist(cur_slist);
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)
82 return 1;
83 return 0;
84 } END_FOR_EACH_PTR_REVERSE(tmp);
85 return 0;
88 int unreachable(void)
90 static int reset_warnings = 1;
92 if (cur_slist) {
93 if (!__inline_fn)
94 reset_warnings = 1;
95 return 0;
98 if (in_declarations_bit())
99 return 1;
101 /* option spammy turns on a noisier version of this */
102 if (reset_warnings && !option_spammy)
103 sm_msg("info: ignoring unreachable code.");
104 if (!__inline_fn)
105 reset_warnings = 0;
106 return 1;
109 struct sm_state *set_state(int owner, const char *name, struct symbol *sym, struct smatch_state *state)
111 struct sm_state *ret;
113 if (!name)
114 return NULL;
116 if (read_only)
117 sm_msg("Smatch Internal Error: cur_slist 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);
123 if (!s)
124 printf("%d new state. name='%s' [%s] %s\n",
125 get_lineno(), name, check_name(owner), show_state(state));
126 else
127 printf("%d state change name='%s' [%s] %s => %s\n",
128 get_lineno(), name, check_name(owner), show_state(s),
129 show_state(state));
132 if (owner != -1 && unreachable())
133 return NULL;
135 if (fake_cur_slist_stack)
136 set_state_stack(&fake_cur_slist_stack, owner, name, sym, state);
138 ret = set_state_slist(&cur_slist, owner, name, sym, state);
140 if (cond_true_stack) {
141 set_state_stack(&cond_true_stack, owner, name, sym, state);
142 set_state_stack(&cond_false_stack, owner, name, sym, state);
144 return ret;
147 struct sm_state *set_state_expr(int owner, struct expression *expr, struct smatch_state *state)
149 char *name;
150 struct symbol *sym;
151 struct sm_state *ret = NULL;
153 expr = strip_expr(expr);
154 name = expr_to_var_sym(expr, &sym);
155 if (!name || !sym)
156 goto free;
157 ret = set_state(owner, name, sym, state);
158 free:
159 free_string(name);
160 return ret;
163 void __push_fake_cur_slist()
165 push_slist(&fake_cur_slist_stack, NULL);
166 __save_pre_cond_states();
169 struct state_list *__pop_fake_cur_slist()
171 __use_pre_cond_states();
172 return pop_slist(&fake_cur_slist_stack);
175 void __free_fake_cur_slist()
177 struct state_list *slist;
179 __use_pre_cond_states();
180 slist = pop_slist(&fake_cur_slist_stack);
181 free_slist(&slist);
184 void __set_fake_cur_slist_fast(struct state_list *slist)
186 push_slist(&pre_cond_stack, cur_slist);
187 cur_slist = slist;
188 read_only = 1;
191 void __pop_fake_cur_slist_fast()
193 cur_slist = pop_slist(&pre_cond_stack);
194 read_only = 0;
197 void __merge_slist_into_cur(struct state_list *slist)
199 struct sm_state *sm;
200 struct sm_state *orig;
201 struct sm_state *merged;
203 FOR_EACH_PTR(slist, sm) {
204 orig = get_sm_state(sm->owner, sm->name, sm->sym);
205 if (orig)
206 merged = merge_sm_states(orig, sm);
207 else
208 merged = sm;
209 __set_sm(merged);
210 } END_FOR_EACH_PTR(sm);
213 void __set_sm(struct sm_state *sm)
215 if (read_only)
216 sm_msg("Smatch Internal Error: cur_slist is read only.");
218 if (option_debug ||
219 strcmp(check_name(sm->owner), option_debug_check) == 0) {
220 struct smatch_state *s;
222 s = get_state(sm->owner, sm->name, sm->sym);
223 if (!s)
224 printf("%d new state. name='%s' [%s] %s\n",
225 get_lineno(), sm->name, check_name(sm->owner),
226 show_state(sm->state));
227 else
228 printf("%d state change name='%s' [%s] %s => %s\n",
229 get_lineno(), sm->name, check_name(sm->owner), show_state(s),
230 show_state(sm->state));
233 if (unreachable())
234 return;
236 if (fake_cur_slist_stack)
237 overwrite_sm_state_stack(&fake_cur_slist_stack, sm);
239 overwrite_sm_state(&cur_slist, sm);
241 if (cond_true_stack) {
242 overwrite_sm_state_stack(&cond_true_stack, sm);
243 overwrite_sm_state_stack(&cond_false_stack, sm);
247 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym)
249 return get_state_slist(cur_slist, owner, name, sym);
252 struct smatch_state *get_state_expr(int owner, struct expression *expr)
254 char *name;
255 struct symbol *sym;
256 struct smatch_state *ret = NULL;
258 expr = strip_expr(expr);
259 name = expr_to_var_sym(expr, &sym);
260 if (!name || !sym)
261 goto free;
262 ret = get_state(owner, name, sym);
263 free:
264 free_string(name);
265 return ret;
268 struct state_list *get_possible_states(int owner, const char *name, struct symbol *sym)
270 struct sm_state *sms;
272 sms = get_sm_state_slist(cur_slist, owner, name, sym);
273 if (sms)
274 return sms->possible;
275 return NULL;
278 struct state_list *get_possible_states_expr(int owner, struct expression *expr)
280 char *name;
281 struct symbol *sym;
282 struct state_list *ret = NULL;
284 expr = strip_expr(expr);
285 name = expr_to_var_sym(expr, &sym);
286 if (!name || !sym)
287 goto free;
288 ret = get_possible_states(owner, name, sym);
289 free:
290 free_string(name);
291 return ret;
294 struct sm_state *get_sm_state(int owner, const char *name, struct symbol *sym)
296 return get_sm_state_slist(cur_slist, owner, name, sym);
299 struct sm_state *get_sm_state_expr(int owner, struct expression *expr)
301 char *name;
302 struct symbol *sym;
303 struct sm_state *ret = NULL;
305 expr = strip_expr(expr);
306 name = expr_to_var_sym(expr, &sym);
307 if (!name || !sym)
308 goto free;
309 ret = get_sm_state(owner, name, sym);
310 free:
311 free_string(name);
312 return ret;
315 void delete_state(int owner, const char *name, struct symbol *sym)
317 delete_state_slist(&cur_slist, owner, name, sym);
318 if (cond_true_stack) {
319 delete_state_stack(&pre_cond_stack, owner, name, sym);
320 delete_state_stack(&cond_true_stack, owner, name, sym);
321 delete_state_stack(&cond_false_stack, owner, name, sym);
325 void delete_state_expr(int owner, struct expression *expr)
327 char *name;
328 struct symbol *sym;
330 expr = strip_expr(expr);
331 name = expr_to_var_sym(expr, &sym);
332 if (!name || !sym)
333 goto free;
334 delete_state(owner, name, sym);
335 free:
336 free_string(name);
339 struct state_list *get_all_states_slist(int owner, struct state_list *source)
341 struct state_list *slist = NULL;
342 struct sm_state *tmp;
344 FOR_EACH_PTR(source, tmp) {
345 if (tmp->owner == owner)
346 add_ptr_list(&slist, tmp);
347 } END_FOR_EACH_PTR(tmp);
349 return slist;
352 struct state_list *get_all_states(int owner)
354 struct state_list *slist = NULL;
355 struct sm_state *tmp;
357 FOR_EACH_PTR(cur_slist, tmp) {
358 if (tmp->owner == owner)
359 add_ptr_list(&slist, tmp);
360 } END_FOR_EACH_PTR(tmp);
362 return slist;
365 int is_reachable(void)
367 if (cur_slist)
368 return 1;
369 return 0;
372 struct state_list *__get_cur_slist(void)
374 return cur_slist;
377 void set_true_false_states(int owner, const char *name, struct symbol *sym,
378 struct smatch_state *true_state,
379 struct smatch_state *false_state)
381 if (option_debug || strcmp(check_name(owner), option_debug_check) == 0) {
382 struct smatch_state *tmp;
384 tmp = get_state(owner, name, sym);
385 printf("%d set_true_false '%s'. Was %s. Now T:%s F:%s\n",
386 get_lineno(), name, show_state(tmp),
387 show_state(true_state), show_state(false_state));
390 if (unreachable())
391 return;
393 if (!cond_false_stack || !cond_true_stack) {
394 printf("Error: missing true/false stacks\n");
395 return;
398 if (true_state) {
399 set_state_slist(&cur_slist, owner, name, sym, true_state);
400 set_state_stack(&cond_true_stack, owner, name, sym, true_state);
402 if (false_state)
403 set_state_stack(&cond_false_stack, owner, name, sym, false_state);
406 void set_true_false_states_expr(int owner, struct expression *expr,
407 struct smatch_state *true_state,
408 struct smatch_state *false_state)
410 char *name;
411 struct symbol *sym;
413 expr = strip_expr(expr);
414 name = expr_to_var_sym(expr, &sym);
415 if (!name || !sym)
416 goto free;
417 set_true_false_states(owner, name, sym, true_state, false_state);
418 free:
419 free_string(name);
422 void __set_true_false_sm(struct sm_state *true_sm, struct sm_state *false_sm)
424 if (unreachable())
425 return;
427 if (!cond_false_stack || !cond_true_stack) {
428 printf("Error: missing true/false stacks\n");
429 return;
432 if (true_sm) {
433 overwrite_sm_state(&cur_slist, true_sm);
434 overwrite_sm_state_stack(&cond_true_stack, true_sm);
436 if (false_sm)
437 overwrite_sm_state_stack(&cond_false_stack, false_sm);
440 void nullify_path(void)
442 free_slist(&cur_slist);
445 void __match_nullify_path_hook(const char *fn, struct expression *expr,
446 void *unused)
448 nullify_path();
452 * At the start of every function we mark the path
453 * as unnull. That way there is always at least one state
454 * in the cur_slist until nullify_path is called. This
455 * is used in merge_slist() for the first null check.
457 void __unnullify_path(void)
459 set_state(-1, "unnull_path", NULL, &true_state);
462 int __path_is_null(void)
464 if (cur_slist)
465 return 0;
466 return 1;
469 static void check_stack_free(struct state_list_stack **stack)
471 if (*stack) {
472 sm_msg("smatch internal error: stack not empty");
473 free_stack_and_slists(stack);
477 void save_all_states(void)
479 __add_ptr_list(&backup, cur_slist, 0);
481 __add_ptr_list(&backup, true_stack, 0);
482 __add_ptr_list(&backup, false_stack, 0);
483 __add_ptr_list(&backup, pre_cond_stack, 0);
485 __add_ptr_list(&backup, cond_true_stack, 0);
486 __add_ptr_list(&backup, cond_false_stack, 0);
488 __add_ptr_list(&backup, fake_cur_slist_stack, 0);
490 __add_ptr_list(&backup, break_stack, 0);
491 __add_ptr_list(&backup, switch_stack, 0);
492 __add_ptr_list(&backup, remaining_cases, 0);
493 __add_ptr_list(&backup, default_stack, 0);
494 __add_ptr_list(&backup, continue_stack, 0);
496 __add_ptr_list(&backup, goto_stack, 0);
499 void nullify_all_states(void)
501 cur_slist = NULL;
503 true_stack = NULL;
504 false_stack = NULL;
505 pre_cond_stack = NULL;
507 cond_true_stack = NULL;
508 cond_false_stack = NULL;
510 fake_cur_slist_stack = NULL;
512 break_stack = NULL;
513 switch_stack = NULL;
514 remaining_cases = NULL;
515 default_stack = NULL;
516 continue_stack = NULL;
518 goto_stack = NULL;
521 static void *pop_backup(void)
523 void *ret;
525 ret = last_ptr_list(backup);
526 delete_ptr_list_last(&backup);
527 return ret;
530 void restore_all_states(void)
532 goto_stack = pop_backup();
534 continue_stack = pop_backup();
535 default_stack = pop_backup();
536 remaining_cases = pop_backup();
537 switch_stack = pop_backup();
538 break_stack = pop_backup();
540 fake_cur_slist_stack = pop_backup();
542 cond_false_stack = pop_backup();
543 cond_true_stack = pop_backup();
545 pre_cond_stack = pop_backup();
546 false_stack = pop_backup();
547 true_stack = pop_backup();
549 cur_slist = pop_backup();
552 void clear_all_states(void)
554 struct named_slist *named_slist;
556 nullify_path();
557 check_stack_free(&true_stack);
558 check_stack_free(&false_stack);
559 check_stack_free(&pre_cond_stack);
560 check_stack_free(&cond_true_stack);
561 check_stack_free(&cond_false_stack);
562 check_stack_free(&break_stack);
563 check_stack_free(&switch_stack);
564 check_stack_free(&continue_stack);
565 free_stack_and_slists(&implied_pools);
567 FOR_EACH_PTR(goto_stack, named_slist) {
568 free_slist(&named_slist->slist);
569 } END_FOR_EACH_PTR(named_slist);
570 __free_ptr_list((struct ptr_list **)&goto_stack);
571 free_every_single_sm_state();
574 void __push_cond_stacks(void)
576 push_slist(&cond_true_stack, NULL);
577 push_slist(&cond_false_stack, NULL);
580 struct state_list *__copy_cond_true_states(void)
582 struct state_list *ret;
584 ret = pop_slist(&cond_true_stack);
585 push_slist(&cond_true_stack, clone_slist(ret));
586 return ret;
589 struct state_list *__copy_cond_false_states(void)
591 struct state_list *ret;
593 ret = pop_slist(&cond_false_stack);
594 push_slist(&cond_false_stack, clone_slist(ret));
595 return ret;
598 struct state_list *__pop_cond_true_stack(void)
600 return pop_slist(&cond_true_stack);
603 struct state_list *__pop_cond_false_stack(void)
605 return pop_slist(&cond_false_stack);
609 * This combines the pre cond states with either the true or false states.
610 * For example:
611 * a = kmalloc() ; if (a !! foo(a)
612 * In the pre state a is possibly null. In the true state it is non null.
613 * In the false state it is null. Combine the pre and the false to get
614 * that when we call 'foo', 'a' is null.
616 static void __use_cond_stack(struct state_list_stack **stack)
618 struct state_list *slist;
620 free_slist(&cur_slist);
622 cur_slist = pop_slist(&pre_cond_stack);
623 push_slist(&pre_cond_stack, clone_slist(cur_slist));
625 slist = pop_slist(stack);
626 overwrite_slist(slist, &cur_slist);
627 push_slist(stack, slist);
630 void __use_pre_cond_states(void)
632 free_slist(&cur_slist);
633 cur_slist = pop_slist(&pre_cond_stack);
636 void __use_cond_true_states(void)
638 __use_cond_stack(&cond_true_stack);
641 void __use_cond_false_states(void)
643 __use_cond_stack(&cond_false_stack);
646 void __negate_cond_stacks(void)
648 struct state_list *old_false, *old_true;
650 __use_cond_stack(&cond_false_stack);
651 old_false = pop_slist(&cond_false_stack);
652 old_true = pop_slist(&cond_true_stack);
653 push_slist(&cond_false_stack, old_true);
654 push_slist(&cond_true_stack, old_false);
657 void __and_cond_states(void)
659 and_slist_stack(&cond_true_stack);
660 or_slist_stack(&pre_cond_stack, cur_slist, &cond_false_stack);
663 void __or_cond_states(void)
665 or_slist_stack(&pre_cond_stack, cur_slist, &cond_true_stack);
666 and_slist_stack(&cond_false_stack);
669 void __save_pre_cond_states(void)
671 push_slist(&pre_cond_stack, clone_slist(cur_slist));
674 void __discard_pre_cond_states(void)
676 struct state_list *tmp;
678 tmp = pop_slist(&pre_cond_stack);
679 free_slist(&tmp);
682 void __use_cond_states(void)
684 struct state_list *pre, *pre_clone, *true_states, *false_states;
686 pre = pop_slist(&pre_cond_stack);
687 pre_clone = clone_slist(pre);
689 true_states = pop_slist(&cond_true_stack);
690 overwrite_slist(true_states, &pre);
691 /* we use the true states right away */
692 free_slist(&cur_slist);
693 cur_slist = pre;
695 false_states = pop_slist(&cond_false_stack);
696 overwrite_slist(false_states, &pre_clone);
697 push_slist(&false_stack, pre_clone);
700 void __push_true_states(void)
702 push_slist(&true_stack, clone_slist(cur_slist));
705 void __use_false_states(void)
707 free_slist(&cur_slist);
708 cur_slist = pop_slist(&false_stack);
711 void __discard_false_states(void)
713 struct state_list *slist;
715 slist = pop_slist(&false_stack);
716 free_slist(&slist);
719 void __merge_false_states(void)
721 struct state_list *slist;
723 slist = pop_slist(&false_stack);
724 merge_slist(&cur_slist, slist);
725 free_slist(&slist);
728 void __merge_true_states(void)
730 struct state_list *slist;
732 slist = pop_slist(&true_stack);
733 merge_slist(&cur_slist, slist);
734 free_slist(&slist);
737 void __push_continues(void)
739 push_slist(&continue_stack, NULL);
742 void __discard_continues(void)
744 struct state_list *slist;
746 slist = pop_slist(&continue_stack);
747 free_slist(&slist);
750 void __process_continues(void)
752 struct state_list *slist;
754 slist = pop_slist(&continue_stack);
755 if (!slist)
756 slist = clone_slist(cur_slist);
757 else
758 merge_slist(&slist, cur_slist);
760 push_slist(&continue_stack, slist);
763 static int top_slist_empty(struct state_list_stack **stack)
765 struct state_list *tmp;
766 int empty = 0;
768 tmp = pop_slist(stack);
769 if (!tmp)
770 empty = 1;
771 push_slist(stack, tmp);
772 return empty;
775 /* a silly loop does this: while(i--) { return; } */
776 void __warn_on_silly_pre_loops(void)
778 if (!__path_is_null())
779 return;
780 if (!top_slist_empty(&continue_stack))
781 return;
782 if (!top_slist_empty(&break_stack))
783 return;
784 /* if the path was nullified before the loop, then we already
785 printed an error earlier */
786 if (top_slist_empty(&false_stack))
787 return;
788 sm_msg("info: loop could be replaced with if statement.");
791 void __merge_continues(void)
793 struct state_list *slist;
795 slist = pop_slist(&continue_stack);
796 merge_slist(&cur_slist, slist);
797 free_slist(&slist);
800 void __push_breaks(void)
802 push_slist(&break_stack, NULL);
805 void __process_breaks(void)
807 struct state_list *slist;
809 slist = pop_slist(&break_stack);
810 if (!slist)
811 slist = clone_slist(cur_slist);
812 else
813 merge_slist(&slist, cur_slist);
815 push_slist(&break_stack, slist);
818 int __has_breaks(void)
820 struct state_list *slist;
821 int ret;
823 slist = pop_slist(&break_stack);
824 ret = !!slist;
825 push_slist(&break_stack, slist);
826 return ret;
829 void __merge_breaks(void)
831 struct state_list *slist;
833 slist = pop_slist(&break_stack);
834 merge_slist(&cur_slist, slist);
835 free_slist(&slist);
838 void __use_breaks(void)
840 free_slist(&cur_slist);
841 cur_slist = pop_slist(&break_stack);
844 void __save_switch_states(struct expression *switch_expr)
846 push_rl(&remaining_cases, __get_implied_values(switch_expr));
847 push_slist(&switch_stack, clone_slist(cur_slist));
850 void __merge_switches(struct expression *switch_expr, struct expression *case_expr)
852 struct state_list *slist;
853 struct state_list *implied_slist;
855 slist = pop_slist(&switch_stack);
856 implied_slist = __implied_case_slist(switch_expr, case_expr, &remaining_cases, &slist);
857 merge_slist(&cur_slist, implied_slist);
858 free_slist(&implied_slist);
859 push_slist(&switch_stack, slist);
862 void __discard_switches(void)
864 struct state_list *slist;
866 pop_rl(&remaining_cases);
867 slist = pop_slist(&switch_stack);
868 free_slist(&slist);
871 void __push_default(void)
873 push_slist(&default_stack, NULL);
876 void __set_default(void)
878 set_state_stack(&default_stack, 0, "has_default", NULL, &true_state);
881 int __pop_default(void)
883 struct state_list *slist;
885 slist = pop_slist(&default_stack);
886 if (slist) {
887 free_slist(&slist);
888 return 1;
890 return 0;
893 static struct named_slist *alloc_named_slist(const char *name, struct state_list *slist)
895 struct named_slist *named_slist = __alloc_named_slist(0);
897 named_slist->name = (char *)name;
898 named_slist->slist = slist;
899 return named_slist;
902 void __save_gotos(const char *name)
904 struct state_list **slist;
905 struct state_list *clone;
907 slist = get_slist_from_named_stack(goto_stack, name);
908 if (slist) {
909 clone = clone_slist(cur_slist);
910 merge_slist(slist, clone);
911 free_slist(&clone);
912 return;
913 } else {
914 struct named_slist *named_slist;
916 clone = clone_slist(cur_slist);
917 named_slist = alloc_named_slist(name, clone);
918 add_ptr_list(&goto_stack, named_slist);
922 void __merge_gotos(const char *name)
924 struct state_list **slist;
926 slist = get_slist_from_named_stack(goto_stack, name);
927 if (slist)
928 merge_slist(&cur_slist, *slist);