2 * sparse/smatch_states.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * You have a lists of states. kernel = locked, foo = NULL, ...
12 * When you hit an if {} else {} statement then you swap the list
13 * of states for a different list of states. The lists are stored
16 * At the beginning of this file there are list of the stacks that
17 * we use. Each function in this file does something to one of
20 * So the smatch_flow.c understands code but it doesn't understand states.
21 * smatch_flow calls functions in this file. This file calls functions
22 * in smatch_slist.c which just has boring generic plumbing for handling
23 * state lists. But really it's this file where all the magic happens.
30 #include "smatch_slist.h"
31 #include "smatch_extra.h"
33 struct smatch_state undefined
= { .name
= "undefined" };
34 struct smatch_state merged
= { .name
= "merged" };
35 struct smatch_state true_state
= { .name
= "true" };
36 struct smatch_state false_state
= { .name
= "false" };
38 static struct state_list
*cur_slist
; /* current states */
40 static struct state_list_stack
*true_stack
; /* states after a t/f branch */
41 static struct state_list_stack
*false_stack
;
42 static struct state_list_stack
*pre_cond_stack
; /* states before a t/f branch */
44 static struct state_list_stack
*cond_true_stack
; /* states affected by a branch */
45 static struct state_list_stack
*cond_false_stack
;
48 struct state_list
*__fake_cur_slist
= NULL
;
49 int __fake_conditions
= 0;
50 struct state_list
*__fake_cond_true
= NULL
;
51 struct state_list
*__fake_cond_false
= NULL
;
53 static struct state_list_stack
*break_stack
;
54 static struct state_list_stack
*switch_stack
;
55 static struct range_list_stack
*remaining_cases
;
56 static struct state_list_stack
*default_stack
;
57 static struct state_list_stack
*continue_stack
;
58 static struct state_list_stack
*false_only_stack
;
60 static struct named_stack
*goto_stack
;
62 struct state_list_stack
*implied_pools
;
66 void __print_cur_slist()
68 __print_slist(cur_slist
);
71 static int unreachable()
73 static int reset_warnings
= 1;
80 if (reset_warnings
|| debug_states
)
81 sm_msg("info: ignoring unreachable code.");
86 void set_state(int owner
, const char *name
, struct symbol
*sym
,
87 struct smatch_state
*state
)
93 struct smatch_state
*s
;
95 s
= get_state(owner
, name
, sym
);
97 printf("%d new state. name='%s' owner=%d: %s\n",
98 get_lineno(), name
, owner
, show_state(state
));
100 printf("%d state change name='%s' owner=%d: %s => %s\n",
101 get_lineno(), name
, owner
, show_state(s
),
105 if (owner
!= -1 && unreachable())
109 set_state_slist(&__fake_cur_slist
, owner
, name
, sym
, state
);
113 set_state_slist(&cur_slist
, owner
, name
, sym
, state
);
115 if (cond_true_stack
) {
116 set_state_stack(&cond_true_stack
, owner
, name
, sym
, state
);
117 set_state_stack(&cond_false_stack
, owner
, name
, sym
, state
);
121 void set_state_expr(int owner
, struct expression
*expr
,
122 struct smatch_state
*state
)
127 expr
= strip_expr(expr
);
128 name
= get_variable_from_expr(expr
, &sym
);
131 reset_on_container_modified(owner
, expr
);
132 set_state(owner
, name
, sym
, state
);
138 void __set_state(struct sm_state
*sm
)
141 struct smatch_state
*s
;
143 s
= get_state(sm
->owner
, sm
->name
, sm
->sym
);
145 printf("%d new state. name='%s' owner=%d: %s\n",
146 get_lineno(), sm
->name
, sm
->owner
, show_state(sm
->state
));
148 printf("%d state change name='%s' owner=%d: %s => %s\n",
149 get_lineno(), sm
->name
, sm
->owner
, show_state(s
),
150 show_state(sm
->state
));
156 overwrite_sm_state(&cur_slist
, sm
);
158 if (cond_true_stack
) {
159 overwrite_sm_state_stack(&cond_true_stack
, sm
);
160 overwrite_sm_state_stack(&cond_false_stack
, sm
);
164 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
)
166 return get_state_slist(cur_slist
, owner
, name
, sym
);
169 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
)
173 struct smatch_state
*ret
= NULL
;
175 expr
= strip_expr(expr
);
176 name
= get_variable_from_expr(expr
, &sym
);
179 ret
= get_state(owner
, name
, sym
);
185 struct state_list
*get_possible_states(int owner
, const char *name
,
188 struct sm_state
*sms
;
190 sms
= get_sm_state_slist(cur_slist
, owner
, name
, sym
);
192 return sms
->possible
;
196 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
)
200 struct state_list
*ret
= NULL
;
202 expr
= strip_expr(expr
);
203 name
= get_variable_from_expr(expr
, &sym
);
206 ret
= get_possible_states(owner
, name
, sym
);
212 struct sm_state
*get_sm_state(int owner
, const char *name
, struct symbol
*sym
)
214 return get_sm_state_slist(cur_slist
, owner
, name
, sym
);
217 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
)
221 struct sm_state
*ret
= NULL
;
223 expr
= strip_expr(expr
);
224 name
= get_variable_from_expr(expr
, &sym
);
227 ret
= get_sm_state(owner
, name
, sym
);
233 void delete_state(int owner
, const char *name
, struct symbol
*sym
)
235 delete_state_slist(&cur_slist
, owner
, name
, sym
);
238 void delete_state_expr(int owner
, struct expression
*expr
)
243 expr
= strip_expr(expr
);
244 name
= get_variable_from_expr(expr
, &sym
);
247 delete_state(owner
, name
, sym
);
252 struct state_list
*get_all_states(int owner
)
254 struct state_list
*slist
= NULL
;
255 struct sm_state
*tmp
;
257 FOR_EACH_PTR(cur_slist
, tmp
) {
258 if (tmp
->owner
== owner
) {
259 add_ptr_list(&slist
, tmp
);
261 } END_FOR_EACH_PTR(tmp
);
273 struct state_list
*__get_cur_slist()
278 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
279 struct smatch_state
*true_state
,
280 struct smatch_state
*false_state
)
283 struct smatch_state
*tmp
;
285 tmp
= get_state(owner
, name
, sym
);
286 sm_debug("%d set_true_false '%s'. Was %s. Now T:%s F:%s\n",
287 get_lineno(), name
, show_state(tmp
),
288 show_state(true_state
), show_state(false_state
));
294 if (__fake_conditions
) {
296 set_state_slist(&__fake_cond_true
, owner
, name
, sym
, true_state
);
298 set_state_slist(&__fake_cond_false
, owner
, name
, sym
, false_state
);
302 if (!cond_false_stack
|| !cond_true_stack
) {
303 printf("Error: missing true/false stacks\n");
308 set_state_slist(&cur_slist
, owner
, name
, sym
, true_state
);
309 set_state_stack(&cond_true_stack
, owner
, name
, sym
, true_state
);
312 set_state_stack(&cond_false_stack
, owner
, name
, sym
, false_state
);
315 void set_true_false_states_expr(int owner
, struct expression
*expr
,
316 struct smatch_state
*true_state
,
317 struct smatch_state
*false_state
)
322 expr
= strip_expr(expr
);
323 name
= get_variable_from_expr(expr
, &sym
);
326 reset_on_container_modified(owner
, expr
);
327 set_true_false_states(owner
, name
, sym
, true_state
, false_state
);
332 void __set_true_false_sm(struct sm_state
*true_state
,
333 struct sm_state
*false_state
)
338 if (!cond_false_stack
|| !cond_true_stack
) {
339 printf("Error: missing true/false stacks\n");
344 overwrite_sm_state(&cur_slist
, true_state
);
345 overwrite_sm_state_stack(&cond_true_stack
, true_state
);
348 overwrite_sm_state_stack(&cond_false_stack
, false_state
);
353 free_slist(&cur_slist
);
356 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
362 * At the start of every function we mark the path
363 * as unnull. That there is always at least one state
364 * in the cur_slist until nullify_path is called. This
365 * is used in merge_slist() for the first null check.
368 void __unnullify_path()
370 set_state(-1, "unnull_path", NULL
, &true_state
);
380 void clear_all_states()
382 struct named_slist
*named_slist
;
385 free_stack_and_slists(&true_stack
);
386 free_stack_and_slists(&false_stack
);
387 free_stack_and_slists(&false_only_stack
);
388 free_stack_and_slists(&pre_cond_stack
);
389 free_stack_and_slists(&cond_true_stack
);
390 free_stack_and_slists(&cond_false_stack
);
391 free_stack_and_slists(&break_stack
);
392 free_stack_and_slists(&switch_stack
);
393 free_stack_and_slists(&continue_stack
);
394 free_stack_and_slists(&implied_pools
);
396 FOR_EACH_PTR(goto_stack
, named_slist
) {
397 free_slist(&named_slist
->slist
);
398 } END_FOR_EACH_PTR(named_slist
);
399 __free_ptr_list((struct ptr_list
**)&goto_stack
);
400 free_every_single_sm_state();
404 void __push_cond_stacks()
406 push_slist(&cond_true_stack
, NULL
);
407 push_slist(&cond_false_stack
, NULL
);
411 * This combines the pre cond states with either the true or false states.
413 * a = kmalloc() ; if (a !! foo(a)
414 * In the pre state a is possibly null. In the true state it is non null.
415 * In the false state it is null. Combine the pre and the false to get
416 * that when we call 'foo', 'a' is null.
419 static void __use_cond_stack(struct state_list_stack
**stack
)
421 struct state_list
*slist
;
423 free_slist(&cur_slist
);
425 cur_slist
= pop_slist(&pre_cond_stack
);
426 push_slist(&pre_cond_stack
, clone_slist(cur_slist
));
428 slist
= pop_slist(stack
);
429 overwrite_slist(slist
, &cur_slist
);
430 push_slist(stack
, slist
);
433 void __save_false_states_for_later()
435 struct state_list
*pre_conditions
;
436 struct state_list
*false_states
;
437 struct state_list
*tmp
;
439 pre_conditions
= pop_slist(&pre_cond_stack
);
440 false_states
= pop_slist(&cond_false_stack
);
441 tmp
= clone_slist(pre_conditions
);
443 overwrite_slist(false_states
, &tmp
);
445 push_slist(&pre_cond_stack
, tmp
);
446 push_slist(&pre_cond_stack
, pre_conditions
);
447 push_slist(&cond_false_stack
, false_states
);
450 void __use_previously_stored_false_states()
452 free_slist(&cur_slist
);
453 cur_slist
= pop_slist(&pre_cond_stack
);
456 void __use_cond_true_states()
458 __use_cond_stack(&cond_true_stack
);
461 void __use_cond_false_states()
463 __use_cond_stack(&cond_false_stack
);
466 void __negate_cond_stacks()
468 struct state_list
*old_false
, *old_true
;
470 old_false
= pop_slist(&cond_false_stack
);
471 old_true
= pop_slist(&cond_true_stack
);
473 overwrite_slist(old_false
, &cur_slist
);
475 push_slist(&cond_false_stack
, old_true
);
476 push_slist(&cond_true_stack
, old_false
);
479 void __and_cond_states()
481 and_slist_stack(&cond_true_stack
);
482 or_slist_stack(&pre_cond_stack
, cur_slist
, &cond_false_stack
);
485 void __or_cond_states()
487 or_slist_stack(&pre_cond_stack
, cur_slist
, &cond_true_stack
);
488 and_slist_stack(&cond_false_stack
);
491 void __save_pre_cond_states()
493 push_slist(&pre_cond_stack
, clone_slist(cur_slist
));
496 void __pop_pre_cond_states()
498 struct state_list
*tmp
;
500 tmp
= pop_slist(&pre_cond_stack
);
504 void __use_false_only_stack()
506 struct state_list
*slist
;
508 slist
= pop_slist(&false_only_stack
);
509 overwrite_slist(slist
, &cur_slist
);
513 void __pop_false_only_stack()
515 struct state_list
*slist
;
517 slist
= pop_slist(&false_only_stack
);
521 void __use_cond_states()
523 struct state_list
*pre
, *pre_clone
, *true_states
, *false_states
;
525 pre
= pop_slist(&pre_cond_stack
);
526 pre_clone
= clone_slist(pre
);
528 true_states
= pop_slist(&cond_true_stack
);
529 overwrite_slist(true_states
, &pre
);
530 /* we use the true states right away */
531 free_slist(&cur_slist
);
535 false_states
= pop_slist(&cond_false_stack
);
536 push_slist(&false_only_stack
, clone_slist(false_states
));
537 overwrite_slist(false_states
, &pre_clone
);
538 push_slist(&false_stack
, pre_clone
);
541 void __push_true_states()
543 push_slist(&true_stack
, clone_slist(cur_slist
));
546 void __use_false_states()
548 free_slist(&cur_slist
);
549 cur_slist
= pop_slist(&false_stack
);
552 void __pop_false_states()
554 struct state_list
*slist
;
556 slist
= pop_slist(&false_stack
);
560 void __merge_false_states()
562 struct state_list
*slist
;
564 slist
= pop_slist(&false_stack
);
565 merge_slist(&cur_slist
, slist
);
569 void __merge_true_states()
571 struct state_list
*slist
;
573 slist
= pop_slist(&true_stack
);
574 merge_slist(&cur_slist
, slist
);
578 void __push_continues()
580 push_slist(&continue_stack
, NULL
);
583 void __pop_continues()
585 struct state_list
*slist
;
587 slist
= pop_slist(&continue_stack
);
591 void __process_continues()
593 struct state_list
*slist
;
595 slist
= pop_slist(&continue_stack
);
597 slist
= clone_slist(cur_slist
);
599 merge_slist(&slist
, cur_slist
);
601 push_slist(&continue_stack
, slist
);
604 static int top_slist_empty(struct state_list_stack
**stack
)
606 struct state_list
*tmp
;
609 tmp
= pop_slist(stack
);
612 push_slist(stack
, tmp
);
616 /* a silly loop does this: while(i--) { return; } */
617 void __warn_on_silly_pre_loops()
619 if (!__path_is_null())
621 if (!top_slist_empty(&continue_stack
))
623 if (!top_slist_empty(&break_stack
))
625 /* if the path was nullified before the loop, then we already
626 printed an error earlier */
627 if (top_slist_empty(&false_stack
))
629 sm_msg("info: loop could be replaced with if statement.");
632 void __merge_continues()
634 struct state_list
*slist
;
636 slist
= pop_slist(&continue_stack
);
637 merge_slist(&cur_slist
, slist
);
643 push_slist(&break_stack
, NULL
);
646 void __process_breaks()
648 struct state_list
*slist
;
650 slist
= pop_slist(&break_stack
);
652 slist
= clone_slist(cur_slist
);
654 merge_slist(&slist
, cur_slist
);
656 push_slist(&break_stack
, slist
);
659 void __merge_breaks()
661 struct state_list
*slist
;
663 slist
= pop_slist(&break_stack
);
664 merge_slist(&cur_slist
, slist
);
670 free_slist(&cur_slist
);
671 cur_slist
= pop_slist(&break_stack
);
674 void __save_switch_states(struct expression
*switch_expr
)
676 push_range_list(&remaining_cases
, __get_implied_values(switch_expr
));
677 push_slist(&switch_stack
, clone_slist(cur_slist
));
680 void __merge_switches(struct expression
*switch_expr
, struct expression
*case_expr
)
682 struct state_list
*slist
;
683 struct state_list
*implied_slist
;
685 slist
= pop_slist(&switch_stack
);
686 implied_slist
= __implied_case_slist(switch_expr
, case_expr
, &remaining_cases
, &slist
);
687 merge_slist(&cur_slist
, implied_slist
);
688 free_slist(&implied_slist
);
689 push_slist(&switch_stack
, slist
);
692 void __pop_switches()
694 struct state_list
*slist
;
696 pop_range_list(&remaining_cases
);
697 slist
= pop_slist(&switch_stack
);
701 void __push_default()
703 push_slist(&default_stack
, NULL
);
708 set_state_stack(&default_stack
, 0, "has_default", NULL
, &true_state
);
713 struct state_list
*slist
;
715 slist
= pop_slist(&default_stack
);
723 static struct named_slist
*alloc_named_slist(const char *name
,
724 struct state_list
*slist
)
726 struct named_slist
*named_slist
= __alloc_named_slist(0);
728 named_slist
->name
= (char *)name
;
729 named_slist
->slist
= slist
;
733 void __save_gotos(const char *name
)
735 struct state_list
**slist
;
736 struct state_list
*clone
;
738 slist
= get_slist_from_named_stack(goto_stack
, name
);
740 clone
= clone_slist(cur_slist
);
741 merge_slist(slist
, clone
);
745 struct named_slist
*named_slist
;
747 clone
= clone_slist(cur_slist
);
748 named_slist
= alloc_named_slist(name
, clone
);
749 add_ptr_list(&goto_stack
, named_slist
);
753 void __merge_gotos(const char *name
)
755 struct state_list
**slist
;
757 slist
= get_slist_from_named_stack(goto_stack
, name
);
759 merge_slist(&cur_slist
, *slist
);