struct_assignment: add copy_from_user()
[smatch.git] / smatch_states.c
blobcc62ac5f609b814fb65de9708b5e9d554234d207
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 */
46 static struct stree *fast_overlay;
48 static struct stree_stack *true_stack; /* states after a t/f branch */
49 static struct stree_stack *false_stack;
50 static struct stree_stack *pre_cond_stack; /* states before a t/f branch */
52 static struct stree_stack *cond_true_stack; /* states affected by a branch */
53 static struct stree_stack *cond_false_stack;
55 static struct stree_stack *fake_cur_stree_stack;
56 static int read_only;
58 static struct stree_stack *break_stack;
59 static struct stree_stack *fake_break_stack;
60 static struct stree_stack *switch_stack;
61 static struct range_list_stack *remaining_cases;
62 static struct stree_stack *default_stack;
63 static struct stree_stack *continue_stack;
65 static struct named_stree_stack *goto_stack;
67 static struct ptr_list *backup;
69 int option_debug;
71 void __print_cur_stree(void)
73 __print_stree(cur_stree);
76 bool __print_states(const char *owner)
78 struct sm_state *sm;
79 bool found = false;
81 if (!owner)
82 return false;
84 FOR_EACH_SM(__get_cur_stree(), sm) {
85 if (!strstr(check_name(sm->owner), owner))
86 continue;
87 sm_msg("%s", show_sm(sm));
88 found = true;
89 } END_FOR_EACH_SM(sm);
91 if (!found)
92 sm_msg("no states found for '%s'", owner);
94 return found;
97 int unreachable(void)
99 if (!cur_stree)
100 return 1;
101 return 0;
104 void __set_cur_stree_readonly(void)
106 read_only++;
109 void __set_cur_stree_writable(void)
111 read_only--;
114 DECLARE_PTR_LIST(check_tracker_list, check_tracker_hook *);
115 static struct check_tracker_list **tracker_hooks;
117 void add_check_tracker(const char *check_name, check_tracker_hook *fn)
119 check_tracker_hook **p;
120 int owner;
122 owner = id_from_name(check_name);
123 if (!owner) {
124 printf("check not found. '%s'\n", check_name);
125 return;
128 p = malloc(sizeof(check_tracker_hook *));
129 *p = fn;
130 add_ptr_list(&tracker_hooks[owner], p);
133 static void call_tracker_hooks(int owner, const char *name, struct symbol *sym, struct smatch_state *state)
135 struct check_tracker_list *hooks;
136 check_tracker_hook **fn;
138 if ((unsigned short)owner == USHRT_MAX)
139 return;
141 hooks = tracker_hooks[owner];
142 FOR_EACH_PTR(hooks, fn) {
143 (*fn)(owner, name, sym, state);
144 } END_FOR_EACH_PTR(fn);
147 void allocate_tracker_array(int num_checks)
149 tracker_hooks = malloc(num_checks * sizeof(void *));
150 memset(tracker_hooks, 0, num_checks * sizeof(void *));
153 bool debug_on(const char *check_name, const char *var)
155 if (option_debug)
156 return true;
157 if (option_debug_check && strstr(check_name, option_debug_check))
158 return true;
159 if (option_debug_var && strcmp(var, option_debug_var) == 0)
160 return true;
161 return false;
164 struct sm_state *set_state(int owner, const char *name, struct symbol *sym, struct smatch_state *state)
166 struct sm_state *ret;
168 if (!name || !state)
169 return NULL;
171 if (read_only)
172 sm_perror("cur_stree is read only.");
174 if (debug_on(check_name(owner), name)) {
175 struct smatch_state *s;
177 s = __get_state(owner, name, sym);
178 if (!s)
179 sm_msg("%s new [%s] '%s' %s", __func__,
180 check_name(owner), name, show_state(state));
181 else
182 sm_msg("%s change [%s] '%s' %s => %s",
183 __func__, check_name(owner), name, show_state(s),
184 show_state(state));
187 call_tracker_hooks(owner, name, sym, state);
189 if (owner != -1 && unreachable())
190 return NULL;
192 if (fake_cur_stree_stack)
193 set_state_stree_stack(&fake_cur_stree_stack, owner, name, sym, state);
195 ret = set_state_stree(&cur_stree, owner, name, sym, state);
197 return ret;
200 struct sm_state *set_state_expr(int owner, struct expression *expr, struct smatch_state *state)
202 char *name;
203 struct symbol *sym;
204 struct sm_state *ret = NULL;
206 expr = strip_expr(expr);
207 name = expr_to_var_sym(expr, &sym);
208 if (!name || !sym)
209 goto free;
210 ret = set_state(owner, name, sym, state);
211 free:
212 free_string(name);
213 return ret;
216 struct stree *__swap_cur_stree(struct stree *stree)
218 struct stree *orig = cur_stree;
220 cur_stree = stree;
221 return orig;
224 void __push_fake_cur_stree(void)
226 push_stree(&fake_cur_stree_stack, NULL);
227 __save_pre_cond_states();
230 struct stree *__pop_fake_cur_stree(void)
232 if (!fake_cur_stree_stack)
233 sm_perror("popping too many fake cur strees.");
234 __use_pre_cond_states();
235 return pop_stree(&fake_cur_stree_stack);
238 void __free_fake_cur_stree(void)
240 struct stree *stree;
242 stree = __pop_fake_cur_stree();
243 free_stree(&stree);
246 void __set_fake_cur_stree_fast(struct stree *stree)
248 if (fast_overlay) {
249 sm_perror("cannot nest fast overlay");
250 return;
252 fast_overlay = stree;
253 set_fast_math_only();
256 void __pop_fake_cur_stree_fast(void)
258 fast_overlay = NULL;
259 clear_fast_math_only();
262 void __merge_stree_into_cur(struct stree *stree)
264 struct sm_state *sm;
265 struct sm_state *orig;
266 struct sm_state *merged;
268 FOR_EACH_SM(stree, sm) {
269 orig = get_sm_state(sm->owner, sm->name, sm->sym);
270 if (orig)
271 merged = merge_sm_states(orig, sm);
272 else
273 merged = sm;
274 __set_sm(merged);
275 } END_FOR_EACH_SM(sm);
278 void __set_sm(struct sm_state *sm)
280 if (read_only)
281 sm_perror("cur_stree is read only.");
283 if (debug_on(check_name(sm->owner), sm->name)) {
284 struct smatch_state *s;
286 s = __get_state(sm->owner, sm->name, sm->sym);
287 if (!s)
288 sm_msg("%s new %s", __func__, show_sm(sm));
289 else
290 sm_msg("%s change %s (was %s)", __func__, show_sm(sm),
291 show_state(s));
294 if (unreachable())
295 return;
297 if (fake_cur_stree_stack)
298 overwrite_sm_state_stree_stack(&fake_cur_stree_stack, sm);
300 overwrite_sm_state_stree(&cur_stree, sm);
303 void __set_sm_cur_stree(struct sm_state *sm)
305 if (read_only)
306 sm_perror("cur_stree is read only.");
308 if (debug_on(check_name(sm->owner), sm->name)) {
309 struct smatch_state *s;
311 s = __get_state(sm->owner, sm->name, sm->sym);
312 if (!s)
313 sm_msg("%s new %s", __func__, show_sm(sm));
314 else
315 sm_msg("%s change %s (was %s)",
316 __func__, show_sm(sm), show_state(s));
319 if (unreachable())
320 return;
322 overwrite_sm_state_stree(&cur_stree, sm);
325 typedef void (get_state_hook)(int owner, const char *name, struct symbol *sym);
326 DECLARE_PTR_LIST(fn_list, get_state_hook *);
327 static struct fn_list *get_state_hooks;
329 void add_get_state_hook(get_state_hook *fn)
331 get_state_hook **p = malloc(sizeof(get_state_hook *));
332 *p = fn;
333 add_ptr_list(&get_state_hooks, p);
336 static void call_get_state_hooks(int owner, const char *name, struct symbol *sym)
338 static int recursion;
339 get_state_hook **fn;
341 if (recursion)
342 return;
343 recursion = 1;
345 FOR_EACH_PTR(get_state_hooks, fn) {
346 (*fn)(owner, name, sym);
347 } END_FOR_EACH_PTR(fn);
349 recursion = 0;
352 bool has_states(struct stree *stree, int owner)
354 if (owner < 0 || owner > USHRT_MAX)
355 return false;
356 if (!stree)
357 return false;
358 if (owner == USHRT_MAX)
359 return true;
360 return stree->has_states[owner];
363 struct smatch_state *__get_state(int owner, const char *name, struct symbol *sym)
365 struct sm_state *sm;
367 sm = get_sm_state(owner, name, sym);
368 if (!sm)
369 return NULL;
370 return sm->state;
373 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym)
375 call_get_state_hooks(owner, name, sym);
377 return __get_state(owner, name, sym);
380 struct smatch_state *get_state_expr(int owner, struct expression *expr)
382 struct expression *fake_parent;
383 char *name;
384 struct symbol *sym;
385 struct smatch_state *ret = NULL;
387 expr = strip_expr(expr);
388 name = expr_to_var_sym(expr, &sym);
389 if (!name || !sym) {
390 fake_parent = expr_get_fake_parent_expr(expr);
391 if (!fake_parent)
392 goto free;
393 name = expr_to_var_sym(fake_parent->left, &sym);
394 if (!name || !sym)
395 goto free;
397 ret = get_state(owner, name, sym);
398 free:
399 free_string(name);
400 return ret;
403 bool has_possible_state(int owner, const char *name, struct symbol *sym, struct smatch_state *state)
405 struct sm_state *sm;
407 sm = get_sm_state(owner, name, sym);
408 if (!sm)
409 return false;
411 return slist_has_state(sm->possible, state);
414 bool expr_has_possible_state(int owner, struct expression *expr, struct smatch_state *state)
416 struct sm_state *sm;
418 sm = get_sm_state_expr(owner, expr);
419 if (!sm)
420 return false;
422 return slist_has_state(sm->possible, state);
425 struct state_list *get_possible_states(int owner, const char *name, struct symbol *sym)
427 struct sm_state *sms;
429 sms = get_sm_state_stree(cur_stree, owner, name, sym);
430 if (sms)
431 return sms->possible;
432 return NULL;
435 struct state_list *get_possible_states_expr(int owner, struct expression *expr)
437 char *name;
438 struct symbol *sym;
439 struct state_list *ret = NULL;
441 expr = strip_expr(expr);
442 name = expr_to_var_sym(expr, &sym);
443 if (!name || !sym)
444 goto free;
445 ret = get_possible_states(owner, name, sym);
446 free:
447 free_string(name);
448 return ret;
451 struct sm_state *get_sm_state(int owner, const char *name, struct symbol *sym)
453 struct sm_state *ret;
455 ret = get_sm_state_stree(fast_overlay, owner, name, sym);
456 if (ret)
457 return ret;
459 return get_sm_state_stree(cur_stree, owner, name, sym);
462 struct sm_state *get_sm_state_expr(int owner, struct expression *expr)
464 char *name;
465 struct symbol *sym;
466 struct sm_state *ret = NULL;
468 expr = strip_expr(expr);
469 name = expr_to_var_sym(expr, &sym);
470 if (!name || !sym)
471 goto free;
472 ret = get_sm_state(owner, name, sym);
473 free:
474 free_string(name);
475 return ret;
478 void __delete_state(int owner, const char *name, struct symbol *sym)
480 delete_state_stree(&cur_stree, owner, name, sym);
481 if (cond_true_stack) {
482 delete_state_stree_stack(&pre_cond_stack, owner, name, sym);
483 delete_state_stree_stack(&cond_true_stack, owner, name, sym);
484 delete_state_stree_stack(&cond_false_stack, owner, name, sym);
488 static void delete_all_states_stree_sym(struct stree **stree, struct symbol *sym)
490 struct state_list *slist = NULL;
491 struct sm_state *sm;
493 FOR_EACH_SM(*stree, sm) {
494 if (sm->sym == sym)
495 add_ptr_list(&slist, sm);
496 } END_FOR_EACH_SM(sm);
498 FOR_EACH_PTR(slist, sm) {
499 delete_state_stree(stree, sm->owner, sm->name, sm->sym);
500 } END_FOR_EACH_PTR(sm);
502 free_slist(&slist);
505 static void delete_all_states_stree_stack_sym(struct stree_stack **stack, struct symbol *sym)
507 struct stree *stree;
509 if (!*stack)
510 return;
512 stree = pop_stree(stack);
513 delete_all_states_stree_sym(&stree, sym);
514 push_stree(stack, stree);
517 void __delete_all_states_sym(struct symbol *sym)
519 delete_all_states_stree_sym(&cur_stree, sym);
521 delete_all_states_stree_stack_sym(&true_stack, sym);
522 delete_all_states_stree_stack_sym(&true_stack, sym);
523 delete_all_states_stree_stack_sym(&false_stack, sym);
524 delete_all_states_stree_stack_sym(&pre_cond_stack, sym);
525 delete_all_states_stree_stack_sym(&cond_true_stack, sym);
526 delete_all_states_stree_stack_sym(&cond_false_stack, sym);
527 delete_all_states_stree_stack_sym(&fake_cur_stree_stack, sym);
528 delete_all_states_stree_stack_sym(&break_stack, sym);
529 delete_all_states_stree_stack_sym(&fake_break_stack, sym);
530 delete_all_states_stree_stack_sym(&switch_stack, sym);
531 delete_all_states_stree_stack_sym(&continue_stack, sym);
534 * deleting from the goto stack is problematic because we don't know
535 * if the label is in scope and also we need the value for --two-passes.
539 struct stree *get_all_states_from_stree(int owner, struct stree *source)
541 struct stree *ret = NULL;
542 struct sm_state *tmp;
544 FOR_EACH_SM(source, tmp) {
545 if (tmp->owner == owner)
546 avl_insert(&ret, tmp);
547 } END_FOR_EACH_SM(tmp);
549 return ret;
552 struct stree *get_all_states_stree(int owner)
554 return get_all_states_from_stree(owner, cur_stree);
557 struct stree *__get_cur_stree(void)
559 return cur_stree;
562 int is_reachable(void)
564 if (cur_stree)
565 return 1;
566 return 0;
569 void set_true_false_states(int owner, const char *name, struct symbol *sym,
570 struct smatch_state *true_state,
571 struct smatch_state *false_state)
573 if (read_only)
574 sm_perror("cur_stree is read only.");
576 if (!name)
577 return;
578 if (!true_state && !false_state)
579 return;
581 if (debug_on(check_name(owner), name)) {
582 struct smatch_state *tmp;
584 tmp = __get_state(owner, name, sym);
585 sm_msg("%s [%s] '%s'. Was %s. Now T:%s F:%s", __func__,
586 check_name(owner), name, show_state(tmp),
587 show_state(true_state), show_state(false_state));
590 if (unreachable())
591 return;
593 if (!cond_false_stack || !cond_true_stack) {
594 sm_perror("missing true/false stacks");
595 return;
598 if (true_state)
599 set_state_stree_stack(&cond_true_stack, owner, name, sym, true_state);
600 if (false_state)
601 set_state_stree_stack(&cond_false_stack, owner, name, sym, false_state);
604 void set_true_false_states_expr(int owner, struct expression *expr,
605 struct smatch_state *true_state,
606 struct smatch_state *false_state)
608 char *name;
609 struct symbol *sym;
611 expr = strip_expr(expr);
612 name = expr_to_var_sym(expr, &sym);
613 if (!name || !sym)
614 goto free;
615 set_true_false_states(owner, name, sym, true_state, false_state);
616 free:
617 free_string(name);
620 void __set_true_false_sm(struct sm_state *true_sm, struct sm_state *false_sm)
622 int owner;
623 const char *name;
624 struct symbol *sym;
626 if (!true_sm && !false_sm)
627 return;
629 if (unreachable())
630 return;
632 owner = true_sm ? true_sm->owner : false_sm->owner;
633 name = true_sm ? true_sm->name : false_sm->name;
634 sym = true_sm ? true_sm->sym : false_sm->sym;
635 if (debug_on(check_name(owner), name)) {
636 struct smatch_state *tmp;
638 tmp = __get_state(owner, name, sym);
639 sm_msg("%s [%s] '%s'. Was %s. Now T:%s F:%s", __func__,
640 check_name(owner), name, show_state(tmp),
641 show_state(true_sm ? true_sm->state : NULL),
642 show_state(false_sm ? false_sm->state : NULL));
645 if (!cond_false_stack || !cond_true_stack) {
646 sm_perror("missing true/false stacks");
647 return;
650 if (true_sm)
651 overwrite_sm_state_stree_stack(&cond_true_stack, true_sm);
652 if (false_sm)
653 overwrite_sm_state_stree_stack(&cond_false_stack, false_sm);
656 void nullify_path(void)
658 if (fake_cur_stree_stack) {
659 __free_fake_cur_stree();
660 __push_fake_cur_stree();
662 free_stree(&cur_stree);
665 void __match_nullify_path_hook(const char *fn, struct expression *expr,
666 void *unused)
668 nullify_path();
672 * At the start of every function we mark the path
673 * as unnull. That way there is always at least one state
674 * in the cur_stree until nullify_path is called. This
675 * is used in merge_slist() for the first null check.
677 void __unnullify_path(void)
679 if (!cur_stree)
680 set_state(-1, "unnull_path", NULL, &true_state);
683 int __path_is_null(void)
685 if (cur_stree)
686 return 0;
687 return 1;
690 #define check_stree_stack_free(stack) do { \
691 if (*stack) { \
692 sm_perror("stack not empty %s", #stack); \
693 free_stack_and_strees(stack); \
695 } while(0)
697 void save_all_states(void)
699 __add_ptr_list(&backup, cur_stree);
700 cur_stree = NULL;
702 __add_ptr_list(&backup, true_stack);
703 true_stack = NULL;
704 __add_ptr_list(&backup, false_stack);
705 false_stack = NULL;
706 __add_ptr_list(&backup, pre_cond_stack);
707 pre_cond_stack = NULL;
709 __add_ptr_list(&backup, cond_true_stack);
710 cond_true_stack = NULL;
711 __add_ptr_list(&backup, cond_false_stack);
712 cond_false_stack = NULL;
714 __add_ptr_list(&backup, fake_cur_stree_stack);
715 fake_cur_stree_stack = NULL;
717 __add_ptr_list(&backup, break_stack);
718 break_stack = NULL;
719 __add_ptr_list(&backup, fake_break_stack);
720 fake_break_stack = NULL;
722 __add_ptr_list(&backup, switch_stack);
723 switch_stack = NULL;
724 __add_ptr_list(&backup, remaining_cases);
725 remaining_cases = NULL;
726 __add_ptr_list(&backup, default_stack);
727 default_stack = NULL;
728 __add_ptr_list(&backup, continue_stack);
729 continue_stack = NULL;
731 __add_ptr_list(&backup, goto_stack);
732 goto_stack = NULL;
735 static void *pop_backup(void)
737 void *ret;
739 ret = last_ptr_list(backup);
740 delete_ptr_list_last(&backup);
741 return ret;
744 void restore_all_states(void)
746 goto_stack = pop_backup();
748 continue_stack = pop_backup();
749 default_stack = pop_backup();
750 remaining_cases = pop_backup();
751 switch_stack = pop_backup();
752 fake_break_stack = pop_backup();
753 break_stack = pop_backup();
755 fake_cur_stree_stack = pop_backup();
757 cond_false_stack = pop_backup();
758 cond_true_stack = pop_backup();
760 pre_cond_stack = pop_backup();
761 false_stack = pop_backup();
762 true_stack = pop_backup();
764 cur_stree = pop_backup();
767 void free_goto_stack(void)
769 struct named_stree *named_stree;
771 FOR_EACH_PTR(goto_stack, named_stree) {
772 free_stree(&named_stree->stree);
773 } END_FOR_EACH_PTR(named_stree);
774 __free_ptr_list((struct ptr_list **)&goto_stack);
777 void clear_all_states(void)
779 nullify_path();
780 check_stree_stack_free(&true_stack);
781 check_stree_stack_free(&false_stack);
782 check_stree_stack_free(&pre_cond_stack);
783 check_stree_stack_free(&cond_true_stack);
784 check_stree_stack_free(&cond_false_stack);
785 check_stree_stack_free(&break_stack);
786 check_stree_stack_free(&fake_break_stack);
787 check_stree_stack_free(&switch_stack);
788 check_stree_stack_free(&continue_stack);
789 check_stree_stack_free(&fake_cur_stree_stack);
791 free_goto_stack();
793 clear_array_values_cache();
794 free_every_single_sm_state();
795 free_tmp_expressions();
798 void __push_cond_stacks(void)
800 push_stree(&cond_true_stack, NULL);
801 push_stree(&cond_false_stack, NULL);
802 __push_fake_cur_stree();
805 void __fold_in_set_states(void)
807 struct stree *new_states;
808 struct sm_state *sm;
810 new_states = __pop_fake_cur_stree();
811 FOR_EACH_SM(new_states, sm) {
812 __set_sm(sm);
813 __set_true_false_sm(sm, sm);
814 } END_FOR_EACH_SM(sm);
815 free_stree(&new_states);
818 void __free_set_states(void)
820 struct stree *new_states;
822 new_states = __pop_fake_cur_stree();
823 free_stree(&new_states);
826 struct stree *__copy_cond_true_states(void)
828 struct stree *ret;
830 ret = pop_stree(&cond_true_stack);
831 push_stree(&cond_true_stack, clone_stree(ret));
832 return ret;
835 struct stree *__copy_cond_false_states(void)
837 struct stree *ret;
839 ret = pop_stree(&cond_false_stack);
840 push_stree(&cond_false_stack, clone_stree(ret));
841 return ret;
844 struct stree *__pop_cond_true_stack(void)
846 return pop_stree(&cond_true_stack);
849 struct stree *__pop_cond_false_stack(void)
851 return pop_stree(&cond_false_stack);
855 * This combines the pre cond states with either the true or false states.
856 * For example:
857 * a = kmalloc() ; if (a !! foo(a)
858 * In the pre state a is possibly null. In the true state it is non null.
859 * In the false state it is null. Combine the pre and the false to get
860 * that when we call 'foo', 'a' is null.
862 static void __use_cond_stack(struct stree_stack **stack)
864 struct stree *stree;
866 free_stree(&cur_stree);
868 cur_stree = pop_stree(&pre_cond_stack);
869 push_stree(&pre_cond_stack, clone_stree(cur_stree));
871 stree = pop_stree(stack);
872 overwrite_stree(stree, &cur_stree);
873 push_stree(stack, stree);
876 void __use_pre_cond_states(void)
878 free_stree(&cur_stree);
879 cur_stree = pop_stree(&pre_cond_stack);
882 void __use_cond_true_states(void)
884 __use_cond_stack(&cond_true_stack);
887 void __use_cond_false_states(void)
889 __use_cond_stack(&cond_false_stack);
892 void __negate_cond_stacks(void)
894 struct stree *old_false, *old_true;
896 old_false = pop_stree(&cond_false_stack);
897 old_true = pop_stree(&cond_true_stack);
898 push_stree(&cond_false_stack, old_true);
899 push_stree(&cond_true_stack, old_false);
902 void __and_cond_states(void)
904 and_stree_stack(&cond_true_stack);
905 or_stree_stack(&pre_cond_stack, cur_stree, &cond_false_stack);
908 void __or_cond_states(void)
910 or_stree_stack(&pre_cond_stack, cur_stree, &cond_true_stack);
911 and_stree_stack(&cond_false_stack);
914 void __save_pre_cond_states(void)
916 push_stree(&pre_cond_stack, clone_stree(cur_stree));
919 void __discard_pre_cond_states(void)
921 struct stree *tmp;
923 tmp = pop_stree(&pre_cond_stack);
924 free_stree(&tmp);
927 struct stree *__get_true_states(void)
929 return clone_stree(top_stree(cond_true_stack));
932 struct stree *__get_false_states(void)
934 return clone_stree(top_stree(cond_false_stack));
937 void __use_cond_states(void)
939 struct stree *pre, *pre_clone, *true_states, *false_states;
941 pre = pop_stree(&pre_cond_stack);
942 pre_clone = clone_stree(pre);
944 true_states = pop_stree(&cond_true_stack);
945 overwrite_stree(true_states, &pre);
946 free_stree(&true_states);
947 /* we use the true states right away */
948 free_stree(&cur_stree);
949 cur_stree = pre;
951 false_states = pop_stree(&cond_false_stack);
952 overwrite_stree(false_states, &pre_clone);
953 free_stree(&false_states);
954 push_stree(&false_stack, pre_clone);
957 void __push_true_states(void)
959 push_stree(&true_stack, clone_stree(cur_stree));
962 void __use_false_states(void)
964 free_stree(&cur_stree);
965 cur_stree = pop_stree(&false_stack);
968 void __discard_false_states(void)
970 struct stree *stree;
972 stree = pop_stree(&false_stack);
973 free_stree(&stree);
976 void __merge_false_states(void)
978 struct stree *stree;
980 stree = pop_stree(&false_stack);
981 merge_stree(&cur_stree, stree);
982 free_stree(&stree);
986 * This function probably seemed common sensical when I wrote it but, oh wow,
987 * does it look subtle in retrospect. Say we set a state on one side of the if
988 * else path but not on the other, then what we should record in the fake stree
989 * is the merged state.
991 * This function relies on the fact that the we always set the cur_stree as well
992 * and we already have the infrastructure to merge things correctly into the
993 * cur_stree.
995 * So instead of merging fake strees together which is probably a lot of work,
996 * we just use it as a list of set states and look up the actual current values
997 * in the cur_stree.
1000 static void update_stree_with_merged(struct stree **stree)
1002 struct state_list *slist = NULL;
1003 struct sm_state *sm, *new;
1005 FOR_EACH_SM(*stree, sm) {
1006 new = get_sm_state(sm->owner, sm->name, sm->sym);
1007 if (!new) /* This can happen if we go out of scope */
1008 continue;
1009 add_ptr_list(&slist, new);
1010 } END_FOR_EACH_SM(sm);
1012 FOR_EACH_PTR(slist, sm) {
1013 overwrite_sm_state_stree(stree, sm);
1014 } END_FOR_EACH_PTR(sm);
1016 free_slist(&slist);
1019 static void update_fake_stree_with_merged(void)
1021 struct stree *stree;
1023 if (!fake_cur_stree_stack)
1024 return;
1025 stree = pop_stree(&fake_cur_stree_stack);
1026 update_stree_with_merged(&stree);
1027 push_stree(&fake_cur_stree_stack, stree);
1030 void __merge_true_states(void)
1032 struct stree *stree;
1034 stree = pop_stree(&true_stack);
1035 merge_stree(&cur_stree, stree);
1036 update_fake_stree_with_merged();
1037 free_stree(&stree);
1040 void __push_continues(void)
1042 push_stree(&continue_stack, NULL);
1045 void __discard_continues(void)
1047 struct stree *stree;
1049 stree = pop_stree(&continue_stack);
1050 free_stree(&stree);
1053 void __process_continues(void)
1055 struct stree *stree;
1057 stree = pop_stree(&continue_stack);
1058 if (!stree)
1059 stree = clone_stree(cur_stree);
1060 else
1061 merge_stree(&stree, cur_stree);
1063 push_stree(&continue_stack, stree);
1066 void __merge_continues(void)
1068 struct stree *stree;
1070 stree = pop_stree(&continue_stack);
1071 merge_stree(&cur_stree, stree);
1072 free_stree(&stree);
1075 void __push_breaks(void)
1077 push_stree(&break_stack, NULL);
1078 if (fake_cur_stree_stack)
1079 push_stree(&fake_break_stack, NULL);
1082 void __process_breaks(void)
1084 struct stree *stree;
1086 stree = pop_stree(&break_stack);
1087 if (!stree)
1088 stree = clone_stree(cur_stree);
1089 else
1090 merge_stree(&stree, cur_stree);
1091 push_stree(&break_stack, stree);
1093 if (!fake_cur_stree_stack)
1094 return;
1096 stree = pop_stree(&fake_break_stack);
1097 if (!stree)
1098 stree = clone_stree(top_stree(fake_cur_stree_stack));
1099 else
1100 merge_stree(&stree, top_stree(fake_cur_stree_stack));
1101 push_stree(&fake_break_stack, stree);
1104 int __has_breaks(void)
1106 struct stree *stree;
1107 int ret;
1109 stree = pop_stree(&break_stack);
1110 ret = !!stree;
1111 push_stree(&break_stack, stree);
1112 return ret;
1115 void __merge_breaks(void)
1117 struct stree *stree;
1118 struct sm_state *sm;
1120 stree = pop_stree(&break_stack);
1121 merge_stree(&cur_stree, stree);
1122 free_stree(&stree);
1124 if (!fake_cur_stree_stack)
1125 return;
1127 stree = pop_stree(&fake_break_stack);
1128 update_stree_with_merged(&stree);
1129 FOR_EACH_SM(stree, sm) {
1130 overwrite_sm_state_stree_stack(&fake_cur_stree_stack, sm);
1131 } END_FOR_EACH_SM(sm);
1132 free_stree(&stree);
1135 void __use_breaks(void)
1137 struct stree *stree;
1138 struct sm_state *sm;
1140 free_stree(&cur_stree);
1141 cur_stree = pop_stree(&break_stack);
1143 if (!fake_cur_stree_stack)
1144 return;
1145 stree = pop_stree(&fake_break_stack);
1146 FOR_EACH_SM(stree, sm) {
1147 overwrite_sm_state_stree_stack(&fake_cur_stree_stack, sm);
1148 } END_FOR_EACH_SM(sm);
1149 free_stree(&stree);
1154 void __save_switch_states(struct expression *switch_expr)
1156 struct range_list *rl;
1158 get_absolute_rl(switch_expr, &rl);
1160 push_rl(&remaining_cases, rl);
1161 push_stree(&switch_stack, clone_stree(cur_stree));
1164 int have_remaining_cases(void)
1166 return !!top_rl(remaining_cases);
1169 void __merge_switches(struct expression *switch_expr, struct range_list *case_rl)
1171 struct stree *stree;
1172 struct stree *implied_stree;
1174 stree = pop_stree(&switch_stack);
1175 if (!stree) {
1177 * If the cur_stree was NULL before the start of the switch
1178 * statement then we don't want to unnullify it.
1181 push_stree(&switch_stack, stree);
1182 return;
1184 implied_stree = __implied_case_stree(switch_expr, case_rl, &remaining_cases, &stree);
1185 merge_stree(&cur_stree, implied_stree);
1186 free_stree(&implied_stree);
1187 push_stree(&switch_stack, stree);
1190 void __discard_switches(void)
1192 struct stree *stree;
1194 pop_rl(&remaining_cases);
1195 stree = pop_stree(&switch_stack);
1196 free_stree(&stree);
1199 void __push_default(void)
1201 push_stree(&default_stack, NULL);
1204 void __set_default(void)
1206 set_state_stree_stack(&default_stack, 0, "has_default", NULL, &true_state);
1209 bool __has_default_case(void)
1211 struct stree *stree;
1213 stree = last_ptr_list((struct ptr_list *)default_stack);
1214 return !!stree;
1217 int __pop_default(void)
1219 struct stree *stree;
1221 stree = pop_stree(&default_stack);
1222 if (stree) {
1223 free_stree(&stree);
1224 return 1;
1226 return 0;
1229 static struct named_stree *alloc_named_stree(const char *name, struct symbol *sym, struct stree *stree)
1231 struct named_stree *named_stree = __alloc_named_stree(0);
1233 named_stree->name = (char *)name;
1234 named_stree->stree = stree;
1235 named_stree->sym = sym;
1236 return named_stree;
1239 void __save_gotos(const char *name, struct symbol *sym)
1241 struct stree **stree;
1242 struct stree *clone;
1244 stree = get_named_stree(goto_stack, name, sym);
1245 if (stree) {
1246 merge_stree(stree, cur_stree);
1247 return;
1248 } else {
1249 struct named_stree *named_stree;
1251 clone = clone_stree(cur_stree);
1252 named_stree = alloc_named_stree(name, sym, clone);
1253 add_ptr_list(&goto_stack, named_stree);
1257 void __merge_gotos(const char *name, struct symbol *sym)
1259 struct stree **stree;
1261 stree = get_named_stree(goto_stack, name, sym);
1262 if (stree)
1263 merge_stree(&cur_stree, *stree);
1266 void __discard_fake_states(struct expression *call)
1268 struct stree *new = NULL;
1269 struct sm_state *sm;
1270 char buf[64];
1271 int len;
1273 if (__fake_state_cnt == 0)
1274 return;
1277 * This is just a best effort type of thing. There could be
1278 * fake states in the true/false trees already. They might
1279 * eventually get cleared out too because we call after probably
1280 * 50% of function calls. But the point is that I don't want to
1281 * waste resources tracking them.
1283 FOR_EACH_SM(cur_stree, sm) {
1284 if (call) {
1285 len = snprintf(buf, sizeof(buf), "__fake_param_%p_", call);
1286 if (strncmp(sm->name, buf, len) != 0)
1287 avl_insert(&new, sm);
1288 continue;
1290 if (strncmp(sm->name, "__fake_param_", 13) != 0)
1291 avl_insert(&new, sm);
1292 } END_FOR_EACH_SM(sm);
1294 free_stree(&cur_stree);
1295 cur_stree = new;
1296 __fake_state_cnt = 0;