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 __use_cond_stack(&cond_false_stack
);
471 old_false
= pop_slist(&cond_false_stack
);
472 old_true
= pop_slist(&cond_true_stack
);
473 push_slist(&cond_false_stack
, old_true
);
474 push_slist(&cond_true_stack
, old_false
);
477 void __and_cond_states()
479 and_slist_stack(&cond_true_stack
);
480 or_slist_stack(&pre_cond_stack
, cur_slist
, &cond_false_stack
);
483 void __or_cond_states()
485 or_slist_stack(&pre_cond_stack
, cur_slist
, &cond_true_stack
);
486 and_slist_stack(&cond_false_stack
);
489 void __save_pre_cond_states()
491 push_slist(&pre_cond_stack
, clone_slist(cur_slist
));
494 void __pop_pre_cond_states()
496 struct state_list
*tmp
;
498 tmp
= pop_slist(&pre_cond_stack
);
502 void __use_false_only_stack()
504 struct state_list
*slist
;
506 slist
= pop_slist(&false_only_stack
);
507 overwrite_slist(slist
, &cur_slist
);
511 void __pop_false_only_stack()
513 struct state_list
*slist
;
515 slist
= pop_slist(&false_only_stack
);
519 void __use_cond_states()
521 struct state_list
*pre
, *pre_clone
, *true_states
, *false_states
;
523 pre
= pop_slist(&pre_cond_stack
);
524 pre_clone
= clone_slist(pre
);
526 true_states
= pop_slist(&cond_true_stack
);
527 overwrite_slist(true_states
, &pre
);
528 /* we use the true states right away */
529 free_slist(&cur_slist
);
533 false_states
= pop_slist(&cond_false_stack
);
534 push_slist(&false_only_stack
, clone_slist(false_states
));
535 overwrite_slist(false_states
, &pre_clone
);
536 push_slist(&false_stack
, pre_clone
);
539 void __push_true_states()
541 push_slist(&true_stack
, clone_slist(cur_slist
));
544 void __use_false_states()
546 free_slist(&cur_slist
);
547 cur_slist
= pop_slist(&false_stack
);
550 void __pop_false_states()
552 struct state_list
*slist
;
554 slist
= pop_slist(&false_stack
);
558 void __merge_false_states()
560 struct state_list
*slist
;
562 slist
= pop_slist(&false_stack
);
563 merge_slist(&cur_slist
, slist
);
567 void __merge_true_states()
569 struct state_list
*slist
;
571 slist
= pop_slist(&true_stack
);
572 merge_slist(&cur_slist
, slist
);
576 void __push_continues()
578 push_slist(&continue_stack
, NULL
);
581 void __pop_continues()
583 struct state_list
*slist
;
585 slist
= pop_slist(&continue_stack
);
589 void __process_continues()
591 struct state_list
*slist
;
593 slist
= pop_slist(&continue_stack
);
595 slist
= clone_slist(cur_slist
);
597 merge_slist(&slist
, cur_slist
);
599 push_slist(&continue_stack
, slist
);
602 static int top_slist_empty(struct state_list_stack
**stack
)
604 struct state_list
*tmp
;
607 tmp
= pop_slist(stack
);
610 push_slist(stack
, tmp
);
614 /* a silly loop does this: while(i--) { return; } */
615 void __warn_on_silly_pre_loops()
617 if (!__path_is_null())
619 if (!top_slist_empty(&continue_stack
))
621 if (!top_slist_empty(&break_stack
))
623 /* if the path was nullified before the loop, then we already
624 printed an error earlier */
625 if (top_slist_empty(&false_stack
))
627 sm_msg("info: loop could be replaced with if statement.");
630 void __merge_continues()
632 struct state_list
*slist
;
634 slist
= pop_slist(&continue_stack
);
635 merge_slist(&cur_slist
, slist
);
641 push_slist(&break_stack
, NULL
);
644 void __process_breaks()
646 struct state_list
*slist
;
648 slist
= pop_slist(&break_stack
);
650 slist
= clone_slist(cur_slist
);
652 merge_slist(&slist
, cur_slist
);
654 push_slist(&break_stack
, slist
);
657 void __merge_breaks()
659 struct state_list
*slist
;
661 slist
= pop_slist(&break_stack
);
662 merge_slist(&cur_slist
, slist
);
668 free_slist(&cur_slist
);
669 cur_slist
= pop_slist(&break_stack
);
672 void __save_switch_states(struct expression
*switch_expr
)
674 push_range_list(&remaining_cases
, __get_implied_values(switch_expr
));
675 push_slist(&switch_stack
, clone_slist(cur_slist
));
678 void __merge_switches(struct expression
*switch_expr
, struct expression
*case_expr
)
680 struct state_list
*slist
;
681 struct state_list
*implied_slist
;
683 slist
= pop_slist(&switch_stack
);
684 implied_slist
= __implied_case_slist(switch_expr
, case_expr
, &remaining_cases
, &slist
);
685 merge_slist(&cur_slist
, implied_slist
);
686 free_slist(&implied_slist
);
687 push_slist(&switch_stack
, slist
);
690 void __pop_switches()
692 struct state_list
*slist
;
694 pop_range_list(&remaining_cases
);
695 slist
= pop_slist(&switch_stack
);
699 void __push_default()
701 push_slist(&default_stack
, NULL
);
706 set_state_stack(&default_stack
, 0, "has_default", NULL
, &true_state
);
711 struct state_list
*slist
;
713 slist
= pop_slist(&default_stack
);
721 static struct named_slist
*alloc_named_slist(const char *name
,
722 struct state_list
*slist
)
724 struct named_slist
*named_slist
= __alloc_named_slist(0);
726 named_slist
->name
= (char *)name
;
727 named_slist
->slist
= slist
;
731 void __save_gotos(const char *name
)
733 struct state_list
**slist
;
734 struct state_list
*clone
;
736 slist
= get_slist_from_named_stack(goto_stack
, name
);
738 clone
= clone_slist(cur_slist
);
739 merge_slist(slist
, clone
);
743 struct named_slist
*named_slist
;
745 clone
= clone_slist(cur_slist
);
746 named_slist
= alloc_named_slist(name
, clone
);
747 add_ptr_list(&goto_stack
, named_slist
);
751 void __merge_gotos(const char *name
)
753 struct state_list
**slist
;
755 slist
= get_slist_from_named_stack(goto_stack
, name
);
757 merge_slist(&cur_slist
, *slist
);