memcpy_overflow: improve struct member boundary calculation for anon unions
[smatch.git] / smatch_states.c
blobef4c36ae6fcaa8dcc41e8515c317843a46145c63
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 ghost = { .name = "ghost" };
42 struct smatch_state merged = { .name = "merged" };
43 struct smatch_state true_state = { .name = "true" };
44 struct smatch_state false_state = { .name = "false" };
46 static struct stree *cur_stree; /* current states */
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 *switch_stack;
60 static struct range_list_stack *remaining_cases;
61 static struct stree_stack *default_stack;
62 static struct stree_stack *continue_stack;
64 static struct named_stree_stack *goto_stack;
66 static struct ptr_list *backup;
68 int option_debug;
70 void __print_cur_stree(void)
72 __print_stree(cur_stree);
75 int unreachable(void)
77 if (!cur_stree)
78 return 1;
79 return 0;
82 struct sm_state *set_state(int owner, const char *name, struct symbol *sym, struct smatch_state *state)
84 struct sm_state *ret;
86 if (!name)
87 return NULL;
89 if (read_only)
90 sm_msg("Smatch Internal Error: cur_stree is read only.");
92 if (option_debug || strcmp(check_name(owner), option_debug_check) == 0) {
93 struct smatch_state *s;
95 s = get_state(owner, name, sym);
96 if (!s)
97 sm_msg("new state. name='%s' [%s] %s",
98 name, check_name(owner), show_state(state));
99 else
100 sm_msg("state change name='%s' [%s] %s => %s",
101 name, check_name(owner), show_state(s),
102 show_state(state));
105 if (owner != -1 && unreachable())
106 return NULL;
108 if (fake_cur_stree_stack)
109 set_state_stree_stack(&fake_cur_stree_stack, owner, name, sym, state);
111 ret = set_state_stree(&cur_stree, owner, name, sym, state);
113 if (cond_true_stack) {
114 set_state_stree_stack(&cond_true_stack, owner, name, sym, state);
115 set_state_stree_stack(&cond_false_stack, owner, name, sym, state);
117 return ret;
120 struct sm_state *set_state_expr(int owner, struct expression *expr, struct smatch_state *state)
122 char *name;
123 struct symbol *sym;
124 struct sm_state *ret = NULL;
126 expr = strip_expr(expr);
127 name = expr_to_var_sym(expr, &sym);
128 if (!name || !sym)
129 goto free;
130 ret = set_state(owner, name, sym, state);
131 free:
132 free_string(name);
133 return ret;
136 void __swap_cur_stree(struct stree *stree)
138 free_stree(&cur_stree);
139 cur_stree = stree;
142 void __push_fake_cur_stree(void)
144 push_stree(&fake_cur_stree_stack, NULL);
145 __save_pre_cond_states();
148 struct stree *__pop_fake_cur_stree(void)
150 if (!fake_cur_stree_stack)
151 sm_msg("INTERNAL error: popping too many fake cur strees.");
152 __use_pre_cond_states();
153 return pop_stree(&fake_cur_stree_stack);
156 void __free_fake_cur_stree(void)
158 struct stree *stree;
160 stree = __pop_fake_cur_stree();
161 free_stree(&stree);
164 void __set_fake_cur_stree_fast(struct stree *stree)
166 push_stree(&pre_cond_stack, cur_stree);
167 cur_stree = stree;
168 read_only = 1;
171 void __pop_fake_cur_stree_fast(void)
173 cur_stree = pop_stree(&pre_cond_stack);
174 read_only = 0;
177 void __merge_stree_into_cur(struct stree *stree)
179 struct sm_state *sm;
180 struct sm_state *orig;
181 struct sm_state *merged;
183 FOR_EACH_SM(stree, sm) {
184 orig = get_sm_state(sm->owner, sm->name, sm->sym);
185 if (orig)
186 merged = merge_sm_states(orig, sm);
187 else
188 merged = sm;
189 __set_sm(merged);
190 } END_FOR_EACH_SM(sm);
193 void __set_sm(struct sm_state *sm)
195 if (read_only)
196 sm_msg("Smatch Internal Error: cur_stree is read only.");
198 if (option_debug ||
199 strcmp(check_name(sm->owner), option_debug_check) == 0) {
200 struct smatch_state *s;
202 s = get_state(sm->owner, sm->name, sm->sym);
203 if (!s)
204 sm_msg("new state. name='%s' [%s] %s",
205 sm->name, check_name(sm->owner),
206 show_state(sm->state));
207 else
208 sm_msg("state change name='%s' [%s] %s => %s",
209 sm->name, check_name(sm->owner), show_state(s),
210 show_state(sm->state));
213 if (unreachable())
214 return;
216 if (fake_cur_stree_stack)
217 overwrite_sm_state_stree_stack(&fake_cur_stree_stack, sm);
219 overwrite_sm_state_stree(&cur_stree, sm);
221 if (cond_true_stack) {
222 overwrite_sm_state_stree_stack(&cond_true_stack, sm);
223 overwrite_sm_state_stree_stack(&cond_false_stack, sm);
227 void __use_orig_if_not_set(struct sm_state *sm)
229 if (read_only)
230 sm_msg("Smatch Internal Error: cur_stree is read only.");
232 if (option_debug ||
233 strcmp(check_name(sm->owner), option_debug_check) == 0) {
234 struct smatch_state *s;
236 s = get_state(sm->owner, sm->name, sm->sym);
237 if (!s)
238 sm_msg("new state. name='%s' [%s] %s",
239 sm->name, check_name(sm->owner),
240 show_state(sm->state));
241 else
242 sm_msg("state change name='%s' [%s] %s => %s",
243 sm->name, check_name(sm->owner), show_state(s),
244 show_state(sm->state));
247 if (unreachable())
248 return;
250 if (fake_cur_stree_stack)
251 overwrite_sm_state_stree_stack(&fake_cur_stree_stack, sm);
253 overwrite_sm_state_stree(&cur_stree, sm);
255 if (cond_true_stack) {
256 struct stree *stree;
258 stree = pop_stree(&cond_true_stack);
259 if (!get_sm_state_stree(stree, sm->owner, sm->name, sm->sym))
260 overwrite_sm_state_stree(&stree, sm);
261 push_stree(&cond_true_stack, stree);
263 stree = pop_stree(&cond_false_stack);
264 if (!get_sm_state_stree(stree, sm->owner, sm->name, sm->sym))
265 overwrite_sm_state_stree(&stree, sm);
266 push_stree(&cond_false_stack, stree);
270 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym)
272 __get_state_hook(owner, name, sym);
274 return get_state_stree(cur_stree, owner, name, sym);
277 struct smatch_state *get_state_expr(int owner, struct expression *expr)
279 char *name;
280 struct symbol *sym;
281 struct smatch_state *ret = NULL;
283 expr = strip_expr(expr);
284 name = expr_to_var_sym(expr, &sym);
285 if (!name || !sym)
286 goto free;
287 ret = get_state(owner, name, sym);
288 free:
289 free_string(name);
290 return ret;
293 struct state_list *get_possible_states(int owner, const char *name, struct symbol *sym)
295 struct sm_state *sms;
297 sms = get_sm_state_stree(cur_stree, owner, name, sym);
298 if (sms)
299 return sms->possible;
300 return NULL;
303 struct state_list *get_possible_states_expr(int owner, struct expression *expr)
305 char *name;
306 struct symbol *sym;
307 struct state_list *ret = NULL;
309 expr = strip_expr(expr);
310 name = expr_to_var_sym(expr, &sym);
311 if (!name || !sym)
312 goto free;
313 ret = get_possible_states(owner, name, sym);
314 free:
315 free_string(name);
316 return ret;
319 struct sm_state *get_sm_state(int owner, const char *name, struct symbol *sym)
321 return get_sm_state_stree(cur_stree, owner, name, sym);
324 struct sm_state *get_sm_state_expr(int owner, struct expression *expr)
326 char *name;
327 struct symbol *sym;
328 struct sm_state *ret = NULL;
330 expr = strip_expr(expr);
331 name = expr_to_var_sym(expr, &sym);
332 if (!name || !sym)
333 goto free;
334 ret = get_sm_state(owner, name, sym);
335 free:
336 free_string(name);
337 return ret;
340 void delete_state(int owner, const char *name, struct symbol *sym)
342 delete_state_stree(&cur_stree, owner, name, sym);
343 if (cond_true_stack) {
344 delete_state_stree_stack(&pre_cond_stack, owner, name, sym);
345 delete_state_stree_stack(&cond_true_stack, owner, name, sym);
346 delete_state_stree_stack(&cond_false_stack, owner, name, sym);
350 void delete_state_expr(int owner, struct expression *expr)
352 char *name;
353 struct symbol *sym;
355 expr = strip_expr(expr);
356 name = expr_to_var_sym(expr, &sym);
357 if (!name || !sym)
358 goto free;
359 delete_state(owner, name, sym);
360 free:
361 free_string(name);
364 struct stree *get_all_states_from_stree(int owner, struct stree *source)
366 struct stree *ret = NULL;
367 struct sm_state *tmp;
369 FOR_EACH_SM(source, tmp) {
370 if (tmp->owner == owner)
371 avl_insert(&ret, tmp);
372 } END_FOR_EACH_SM(tmp);
374 return ret;
377 struct stree *get_all_states_stree(int owner)
379 return get_all_states_from_stree(owner, cur_stree);
382 struct stree *__get_cur_stree(void)
384 return cur_stree;
387 int is_reachable(void)
389 if (cur_stree)
390 return 1;
391 return 0;
394 void set_true_false_states(int owner, const char *name, struct symbol *sym,
395 struct smatch_state *true_state,
396 struct smatch_state *false_state)
398 if (read_only)
399 sm_msg("Smatch Internal Error: cur_stree is read only.");
401 if (option_debug || strcmp(check_name(owner), option_debug_check) == 0) {
402 struct smatch_state *tmp;
404 tmp = get_state(owner, name, sym);
405 sm_msg("set_true_false '%s'. Was %s. Now T:%s F:%s\n",
406 name, show_state(tmp),
407 show_state(true_state), show_state(false_state));
410 if (unreachable())
411 return;
413 if (!cond_false_stack || !cond_true_stack) {
414 printf("Error: missing true/false stacks\n");
415 return;
418 if (true_state) {
419 set_state_stree(&cur_stree, owner, name, sym, true_state);
420 set_state_stree_stack(&cond_true_stack, owner, name, sym, true_state);
422 if (false_state)
423 set_state_stree_stack(&cond_false_stack, owner, name, sym, false_state);
426 void set_true_false_states_expr(int owner, struct expression *expr,
427 struct smatch_state *true_state,
428 struct smatch_state *false_state)
430 char *name;
431 struct symbol *sym;
433 expr = strip_expr(expr);
434 name = expr_to_var_sym(expr, &sym);
435 if (!name || !sym)
436 goto free;
437 set_true_false_states(owner, name, sym, true_state, false_state);
438 free:
439 free_string(name);
442 void __set_true_false_sm(struct sm_state *true_sm, struct sm_state *false_sm)
444 if (unreachable())
445 return;
447 if (!cond_false_stack || !cond_true_stack) {
448 printf("Error: missing true/false stacks\n");
449 return;
452 if (true_sm) {
453 overwrite_sm_state_stree(&cur_stree, true_sm);
454 overwrite_sm_state_stree_stack(&cond_true_stack, true_sm);
456 if (false_sm)
457 overwrite_sm_state_stree_stack(&cond_false_stack, false_sm);
460 void nullify_path(void)
462 free_stree(&cur_stree);
465 void __match_nullify_path_hook(const char *fn, struct expression *expr,
466 void *unused)
468 nullify_path();
472 * At the start of every function we mark the path
473 * as unnull. That way there is always at least one state
474 * in the cur_stree until nullify_path is called. This
475 * is used in merge_slist() for the first null check.
477 void __unnullify_path(void)
479 if (!cur_stree)
480 set_state(-1, "unnull_path", NULL, &true_state);
483 int __path_is_null(void)
485 if (cur_stree)
486 return 0;
487 return 1;
490 static void check_stree_stack_free(struct stree_stack **stack)
492 if (*stack) {
493 sm_msg("smatch internal error: stack not empty");
494 free_stack_and_strees(stack);
498 void save_all_states(void)
500 __add_ptr_list(&backup, cur_stree, 0);
501 cur_stree = NULL;
503 __add_ptr_list(&backup, true_stack, 0);
504 true_stack = NULL;
505 __add_ptr_list(&backup, false_stack, 0);
506 false_stack = NULL;
507 __add_ptr_list(&backup, pre_cond_stack, 0);
508 pre_cond_stack = NULL;
510 __add_ptr_list(&backup, cond_true_stack, 0);
511 cond_true_stack = NULL;
512 __add_ptr_list(&backup, cond_false_stack, 0);
513 cond_false_stack = NULL;
515 __add_ptr_list(&backup, fake_cur_stree_stack, 0);
516 fake_cur_stree_stack = NULL;
518 __add_ptr_list(&backup, break_stack, 0);
519 break_stack = NULL;
520 __add_ptr_list(&backup, switch_stack, 0);
521 switch_stack = NULL;
522 __add_ptr_list(&backup, remaining_cases, 0);
523 remaining_cases = NULL;
524 __add_ptr_list(&backup, default_stack, 0);
525 default_stack = NULL;
526 __add_ptr_list(&backup, continue_stack, 0);
527 continue_stack = NULL;
529 __add_ptr_list(&backup, goto_stack, 0);
530 goto_stack = NULL;
533 static void *pop_backup(void)
535 void *ret;
537 ret = last_ptr_list(backup);
538 delete_ptr_list_last(&backup);
539 return ret;
542 void restore_all_states(void)
544 goto_stack = pop_backup();
546 continue_stack = pop_backup();
547 default_stack = pop_backup();
548 remaining_cases = pop_backup();
549 switch_stack = pop_backup();
550 break_stack = pop_backup();
552 fake_cur_stree_stack = pop_backup();
554 cond_false_stack = pop_backup();
555 cond_true_stack = pop_backup();
557 pre_cond_stack = pop_backup();
558 false_stack = pop_backup();
559 true_stack = pop_backup();
561 cur_stree = pop_backup();
564 void free_goto_stack(void)
566 struct named_stree *named_stree;
568 FOR_EACH_PTR(goto_stack, named_stree) {
569 free_stree(&named_stree->stree);
570 } END_FOR_EACH_PTR(named_stree);
571 __free_ptr_list((struct ptr_list **)&goto_stack);
574 void clear_all_states(void)
576 nullify_path();
577 check_stree_stack_free(&true_stack);
578 check_stree_stack_free(&false_stack);
579 check_stree_stack_free(&pre_cond_stack);
580 check_stree_stack_free(&cond_true_stack);
581 check_stree_stack_free(&cond_false_stack);
582 check_stree_stack_free(&break_stack);
583 check_stree_stack_free(&switch_stack);
584 check_stree_stack_free(&continue_stack);
586 free_goto_stack();
588 free_every_single_sm_state();
591 void __push_cond_stacks(void)
593 push_stree(&cond_true_stack, NULL);
594 push_stree(&cond_false_stack, NULL);
597 struct stree *__copy_cond_true_states(void)
599 struct stree *ret;
601 ret = pop_stree(&cond_true_stack);
602 push_stree(&cond_true_stack, clone_stree(ret));
603 return ret;
606 struct stree *__copy_cond_false_states(void)
608 struct stree *ret;
610 ret = pop_stree(&cond_false_stack);
611 push_stree(&cond_false_stack, clone_stree(ret));
612 return ret;
615 struct stree *__pop_cond_true_stack(void)
617 return pop_stree(&cond_true_stack);
620 struct stree *__pop_cond_false_stack(void)
622 return pop_stree(&cond_false_stack);
626 * This combines the pre cond states with either the true or false states.
627 * For example:
628 * a = kmalloc() ; if (a !! foo(a)
629 * In the pre state a is possibly null. In the true state it is non null.
630 * In the false state it is null. Combine the pre and the false to get
631 * that when we call 'foo', 'a' is null.
633 static void __use_cond_stack(struct stree_stack **stack)
635 struct stree *stree;
637 free_stree(&cur_stree);
639 cur_stree = pop_stree(&pre_cond_stack);
640 push_stree(&pre_cond_stack, clone_stree(cur_stree));
642 stree = pop_stree(stack);
643 overwrite_stree(stree, &cur_stree);
644 push_stree(stack, stree);
647 void __use_pre_cond_states(void)
649 free_stree(&cur_stree);
650 cur_stree = pop_stree(&pre_cond_stack);
653 void __use_cond_true_states(void)
655 __use_cond_stack(&cond_true_stack);
658 void __use_cond_false_states(void)
660 __use_cond_stack(&cond_false_stack);
663 void __negate_cond_stacks(void)
665 struct stree *old_false, *old_true;
667 __use_cond_stack(&cond_false_stack);
668 old_false = pop_stree(&cond_false_stack);
669 old_true = pop_stree(&cond_true_stack);
670 push_stree(&cond_false_stack, old_true);
671 push_stree(&cond_true_stack, old_false);
674 void __and_cond_states(void)
676 and_stree_stack(&cond_true_stack);
677 or_stree_stack(&pre_cond_stack, cur_stree, &cond_false_stack);
680 void __or_cond_states(void)
682 or_stree_stack(&pre_cond_stack, cur_stree, &cond_true_stack);
683 and_stree_stack(&cond_false_stack);
686 void __save_pre_cond_states(void)
688 push_stree(&pre_cond_stack, clone_stree(cur_stree));
691 void __discard_pre_cond_states(void)
693 struct stree *tmp;
695 tmp = pop_stree(&pre_cond_stack);
696 free_stree(&tmp);
699 void __use_cond_states(void)
701 struct stree *pre, *pre_clone, *true_states, *false_states;
703 pre = pop_stree(&pre_cond_stack);
704 pre_clone = clone_stree(pre);
706 true_states = pop_stree(&cond_true_stack);
707 overwrite_stree(true_states, &pre);
708 free_stree(&true_states);
709 /* we use the true states right away */
710 free_stree(&cur_stree);
711 cur_stree = pre;
713 false_states = pop_stree(&cond_false_stack);
714 overwrite_stree(false_states, &pre_clone);
715 free_stree(&false_states);
716 push_stree(&false_stack, pre_clone);
719 void __push_true_states(void)
721 push_stree(&true_stack, clone_stree(cur_stree));
724 void __use_false_states(void)
726 free_stree(&cur_stree);
727 cur_stree = pop_stree(&false_stack);
730 void __discard_false_states(void)
732 struct stree *stree;
734 stree = pop_stree(&false_stack);
735 free_stree(&stree);
738 void __merge_false_states(void)
740 struct stree *stree;
742 stree = pop_stree(&false_stack);
743 merge_stree(&cur_stree, stree);
744 free_stree(&stree);
747 void __merge_true_states(void)
749 struct stree *stree;
751 stree = pop_stree(&true_stack);
752 merge_stree(&cur_stree, stree);
753 free_stree(&stree);
756 void __push_continues(void)
758 push_stree(&continue_stack, NULL);
761 void __discard_continues(void)
763 struct stree *stree;
765 stree = pop_stree(&continue_stack);
766 free_stree(&stree);
769 void __process_continues(void)
771 struct stree *stree;
773 stree = pop_stree(&continue_stack);
774 if (!stree)
775 stree = clone_stree(cur_stree);
776 else
777 merge_stree(&stree, cur_stree);
779 push_stree(&continue_stack, stree);
782 void __merge_continues(void)
784 struct stree *stree;
786 stree = pop_stree(&continue_stack);
787 merge_stree(&cur_stree, stree);
788 free_stree(&stree);
791 void __push_breaks(void)
793 push_stree(&break_stack, NULL);
796 void __process_breaks(void)
798 struct stree *stree;
800 stree = pop_stree(&break_stack);
801 if (!stree)
802 stree = clone_stree(cur_stree);
803 else
804 merge_stree(&stree, cur_stree);
806 push_stree(&break_stack, stree);
809 int __has_breaks(void)
811 struct stree *stree;
812 int ret;
814 stree = pop_stree(&break_stack);
815 ret = !!stree;
816 push_stree(&break_stack, stree);
817 return ret;
820 void __merge_breaks(void)
822 struct stree *stree;
824 stree = pop_stree(&break_stack);
825 merge_stree(&cur_stree, stree);
826 free_stree(&stree);
829 void __use_breaks(void)
831 free_stree(&cur_stree);
832 cur_stree = pop_stree(&break_stack);
835 void __save_switch_states(struct expression *switch_expr)
837 push_rl(&remaining_cases, __get_implied_values(switch_expr));
838 push_stree(&switch_stack, clone_stree(cur_stree));
841 void __merge_switches(struct expression *switch_expr, struct expression *case_expr, struct expression *case_to)
843 struct stree *stree;
844 struct stree *implied_stree;
846 stree = pop_stree(&switch_stack);
847 implied_stree = __implied_case_stree(switch_expr, case_expr, case_to, &remaining_cases, &stree);
848 merge_stree(&cur_stree, implied_stree);
849 free_stree(&implied_stree);
850 push_stree(&switch_stack, stree);
853 void __discard_switches(void)
855 struct stree *stree;
857 pop_rl(&remaining_cases);
858 stree = pop_stree(&switch_stack);
859 free_stree(&stree);
862 void __push_default(void)
864 push_stree(&default_stack, NULL);
867 void __set_default(void)
869 set_state_stree_stack(&default_stack, 0, "has_default", NULL, &true_state);
872 int __pop_default(void)
874 struct stree *stree;
876 stree = pop_stree(&default_stack);
877 if (stree) {
878 free_stree(&stree);
879 return 1;
881 return 0;
884 static struct named_stree *alloc_named_stree(const char *name, struct stree *stree)
886 struct named_stree *named_stree = __alloc_named_stree(0);
888 named_stree->name = (char *)name;
889 named_stree->stree = stree;
890 return named_stree;
893 void __save_gotos(const char *name)
895 struct stree **stree;
896 struct stree *clone;
898 stree = get_named_stree(goto_stack, name);
899 if (stree) {
900 merge_stree(stree, cur_stree);
901 return;
902 } else {
903 struct named_stree *named_stree;
905 clone = clone_stree(cur_stree);
906 named_stree = alloc_named_stree(name, clone);
907 add_ptr_list(&goto_stack, named_stree);
911 void __merge_gotos(const char *name)
913 struct stree **stree;
915 stree = get_named_stree(goto_stack, name);
916 if (stree)
917 merge_stree(&cur_stree, *stree);