Put some validation scripts in the closet.
[smatch.git] / smatch_states.c
blob63ebb328f6d68c413778261783a2f823624e14a8
1 /*
2 * sparse/smatch_states.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
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
14 * on stacks.
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
18 * of the stacks.
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.
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include "smatch.h"
30 #include "smatch_slist.h"
32 struct smatch_state undefined = { .name = "undefined" };
33 struct smatch_state merged = { .name = "merged" };
34 struct smatch_state true_state = { .name = "true" };
35 struct smatch_state false_state = { .name = "false" };
37 static struct state_list *cur_slist; /* current states */
39 static struct state_list_stack *true_stack; /* states after a t/f branch */
40 static struct state_list_stack *false_stack;
41 static struct state_list_stack *pre_cond_stack; /* states before a t/f branch */
43 static struct state_list_stack *cond_true_stack; /* states affected by a branch */
44 static struct state_list_stack *cond_false_stack;
46 static struct state_list_stack *break_stack;
47 static struct state_list_stack *switch_stack;
48 static struct state_list_stack *default_stack;
49 static struct state_list_stack *continue_stack;
50 static struct state_list_stack *false_only_stack;
52 static struct named_stack *goto_stack;
54 struct state_list_stack *implied_pools;
56 int debug_states;
58 void __print_cur_slist()
60 __print_slist(cur_slist);
63 static int unreachable()
65 static int reset_warnings = 1;
67 if (cur_slist) {
68 reset_warnings = 1;
69 return 0;
72 if (reset_warnings || debug_states)
73 smatch_msg("info: ignoring unreachable code.");
74 reset_warnings = 0;
75 return 1;
78 void set_state(const char *name, int owner, struct symbol *sym,
79 struct smatch_state *state)
81 if (!name)
82 return;
84 if (debug_states) {
85 struct smatch_state *s;
87 s = get_state(name, owner, sym);
88 if (!s)
89 printf("%d new state. name='%s' owner=%d: %s\n",
90 get_lineno(), name, owner, show_state(state));
91 else
92 printf("%d state change name='%s' owner=%d: %s => %s\n",
93 get_lineno(), name, owner, show_state(s),
94 show_state(state));
97 if (owner != -1 && unreachable())
98 return;
100 set_state_slist(&cur_slist, name, owner, sym, state);
102 if (cond_true_stack) {
103 set_state_stack(&cond_true_stack, name, owner, sym, state);
104 set_state_stack(&cond_false_stack, name, owner, sym, state);
108 struct smatch_state *get_state(const char *name, int owner, struct symbol *sym)
110 return get_state_slist(cur_slist, name, owner, sym);
113 struct state_list *get_possible_states(const char *name, int owner,
114 struct symbol *sym)
116 struct sm_state *sms;
118 sms = get_sm_state_slist(cur_slist, name, owner, sym);
119 if (sms)
120 return sms->possible;
121 return NULL;
124 struct sm_state *get_sm_state(const char *name, int owner, struct symbol *sym)
126 return get_sm_state_slist(cur_slist, name, owner, sym);
129 void delete_state(const char *name, int owner, struct symbol *sym)
131 delete_state_slist(&cur_slist, name, owner, sym);
134 struct state_list *get_all_states(int owner)
136 struct state_list *slist = NULL;
137 struct sm_state *tmp;
139 FOR_EACH_PTR(cur_slist, tmp) {
140 if (tmp->owner == owner) {
141 add_ptr_list(&slist, tmp);
143 } END_FOR_EACH_PTR(tmp);
145 return slist;
148 int is_reachable()
150 if (cur_slist)
151 return 1;
152 return 0;
155 struct state_list *__get_cur_slist()
157 return cur_slist;
160 void set_true_false_states(const char *name, int owner, struct symbol *sym,
161 struct smatch_state *true_state,
162 struct smatch_state *false_state)
164 if (debug_states) {
165 struct smatch_state *tmp;
167 tmp = get_state(name, owner, sym);
168 SM_DEBUG("%d set_true_false '%s'. Was %s. Now T:%s F:%s\n",
169 get_lineno(), name, show_state(tmp),
170 show_state(true_state), show_state(false_state));
173 if (unreachable())
174 return;
176 if (!cond_false_stack || !cond_true_stack) {
177 printf("Error: missing true/false stacks\n");
178 return;
181 if (true_state) {
182 set_state_slist(&cur_slist, name, owner, sym, true_state);
183 set_state_stack(&cond_true_stack, name, owner, sym, true_state);
185 if (false_state)
186 set_state_stack(&cond_false_stack, name, owner, sym, false_state);
189 void __set_true_false_sm(struct sm_state *true_state,
190 struct sm_state *false_state)
192 if (unreachable())
193 return;
195 if (!cond_false_stack || !cond_true_stack) {
196 printf("Error: missing true/false stacks\n");
197 return;
200 if (true_state) {
201 overwrite_sm_state(&cur_slist, true_state);
202 overwrite_sm_state_stack(&cond_true_stack, true_state);
204 if (false_state)
205 overwrite_sm_state_stack(&cond_false_stack, false_state);
208 void nullify_path()
210 free_slist(&cur_slist);
213 void __match_nullify_path_hook(const char *fn, struct expression *expr,
214 void *unused)
216 nullify_path();
219 * At the start of every function we mark the path
220 * as unnull. That there is always at least one state
221 * in the cur_slist until nullify_path is called. This
222 * is used in merge_slist() for the first null check.
225 void __unnullify_path()
227 set_state("unnull_path", -1, NULL, &true_state);
230 int __path_is_null()
232 if (cur_slist)
233 return 0;
234 return 1;
237 void clear_all_states()
239 struct named_slist *named_slist;
241 nullify_path();
242 free_stack_and_slists(&true_stack);
243 free_stack_and_slists(&false_stack);
244 free_stack_and_slists(&false_only_stack);
245 free_stack_and_slists(&pre_cond_stack);
246 free_stack_and_slists(&cond_true_stack);
247 free_stack_and_slists(&cond_false_stack);
248 free_stack_and_slists(&break_stack);
249 free_stack_and_slists(&switch_stack);
250 free_stack_and_slists(&continue_stack);
251 free_stack_and_slists(&implied_pools);
253 FOR_EACH_PTR(goto_stack, named_slist) {
254 free_slist(&named_slist->slist);
255 } END_FOR_EACH_PTR(named_slist);
256 __free_ptr_list((struct ptr_list **)&goto_stack);
257 free_every_single_sm_state();
261 void __push_cond_stacks()
263 push_slist(&cond_true_stack, NULL);
264 push_slist(&cond_false_stack, NULL);
268 * This combines the pre cond states with either the true or false states.
269 * For example:
270 * a = kmalloc() ; if (a !! foo(a)
271 * In the pre state a is possibly null. In the true state it is non null.
272 * In the false state it is null. Combine the pre and the false to get
273 * that when we call 'foo', 'a' is null.
276 static void __use_cond_stack(struct state_list_stack **stack)
278 struct state_list *slist;
280 free_slist(&cur_slist);
282 cur_slist = pop_slist(&pre_cond_stack);
283 push_slist(&pre_cond_stack, clone_slist(cur_slist));
285 slist = pop_slist(stack);
286 overwrite_slist(slist, &cur_slist);
287 push_slist(stack, slist);
290 void __save_false_states_for_later()
292 struct state_list *pre_conditions;
293 struct state_list *false_states;
294 struct state_list *tmp;
296 pre_conditions = pop_slist(&pre_cond_stack);
297 false_states = pop_slist(&cond_false_stack);
298 tmp = clone_slist(pre_conditions);
300 overwrite_slist(false_states, &tmp);
302 push_slist(&pre_cond_stack, tmp);
303 push_slist(&pre_cond_stack, pre_conditions);
304 push_slist(&cond_false_stack, false_states);
307 void __use_previously_stored_false_states()
309 free_slist(&cur_slist);
310 cur_slist = pop_slist(&pre_cond_stack);
313 void __use_cond_true_states()
315 __use_cond_stack(&cond_true_stack);
318 void __use_cond_false_states()
320 __use_cond_stack(&cond_false_stack);
323 void __negate_cond_stacks()
325 struct state_list *old_false, *old_true;
327 old_false = pop_slist(&cond_false_stack);
328 old_true = pop_slist(&cond_true_stack);
330 overwrite_slist(old_false, &cur_slist);
332 push_slist(&cond_false_stack, old_true);
333 push_slist(&cond_true_stack, old_false);
336 void __and_cond_states()
338 and_slist_stack(&cond_true_stack);
339 or_slist_stack(&pre_cond_stack, cur_slist, &cond_false_stack);
342 void __or_cond_states()
344 or_slist_stack(&pre_cond_stack, cur_slist, &cond_true_stack);
345 and_slist_stack(&cond_false_stack);
348 void __save_pre_cond_states()
350 push_slist(&pre_cond_stack, clone_slist(cur_slist));
353 void __pop_pre_cond_states()
355 struct state_list *tmp;
357 tmp = pop_slist(&pre_cond_stack);
358 free_slist(&tmp);
361 void __use_false_only_stack()
363 struct state_list *slist;
365 slist = pop_slist(&false_only_stack);
366 overwrite_slist(slist, &cur_slist);
367 free_slist(&slist);
370 void __pop_false_only_stack()
372 struct state_list *slist;
374 slist = pop_slist(&false_only_stack);
375 free_slist(&slist);
378 void __use_cond_states()
380 struct state_list *pre, *pre_clone, *true_states, *false_states;
382 pre = pop_slist(&pre_cond_stack);
383 pre_clone = clone_slist(pre);
385 true_states = pop_slist(&cond_true_stack);
386 overwrite_slist(true_states, &pre);
387 /* we use the true states right away */
388 free_slist(&cur_slist);
389 cur_slist = pre;
392 false_states = pop_slist(&cond_false_stack);
393 push_slist(&false_only_stack, clone_slist(false_states));
394 overwrite_slist(false_states, &pre_clone);
395 push_slist(&false_stack, pre_clone);
398 void __push_true_states()
400 push_slist(&true_stack, clone_slist(cur_slist));
403 void __use_false_states()
405 free_slist(&cur_slist);
406 cur_slist = pop_slist(&false_stack);
409 void __pop_false_states()
411 struct state_list *slist;
413 slist = pop_slist(&false_stack);
414 free_slist(&slist);
417 void __merge_false_states()
419 struct state_list *slist;
421 slist = pop_slist(&false_stack);
422 merge_slist(&cur_slist, slist);
423 free_slist(&slist);
426 void __merge_true_states()
428 struct state_list *slist;
430 slist = pop_slist(&true_stack);
431 merge_slist(&cur_slist, slist);
432 free_slist(&slist);
435 void __push_continues()
437 push_slist(&continue_stack, NULL);
440 void __pop_continues()
442 struct state_list *slist;
444 slist = pop_slist(&continue_stack);
445 free_slist(&slist);
448 void __process_continues()
450 struct state_list *slist;
452 slist = pop_slist(&continue_stack);
453 if (!slist) {
454 slist = clone_slist(cur_slist);
455 } else {
456 merge_slist(&slist, cur_slist);
458 push_slist(&continue_stack, slist);
461 static int top_slist_empty(struct state_list_stack **stack)
463 struct state_list *tmp;
464 int empty = 0;
466 tmp = pop_slist(stack);
467 if (!tmp)
468 empty = 1;
469 push_slist(stack, tmp);
470 return empty;
473 /* a silly loop does this: while(i--) { return; } */
474 void __warn_on_silly_pre_loops()
476 if (!__path_is_null())
477 return;
478 if (!top_slist_empty(&continue_stack))
479 return;
480 if (!top_slist_empty(&break_stack))
481 return;
482 /* if the path was nullified before the loop, then we already
483 printed an error earlier */
484 if (top_slist_empty(&false_stack))
485 return;
486 smatch_msg("info: loop could be replaced with if statement.");
489 void __merge_continues()
491 struct state_list *slist;
493 slist = pop_slist(&continue_stack);
494 merge_slist(&cur_slist, slist);
495 free_slist(&slist);
498 void __push_breaks()
500 push_slist(&break_stack, NULL);
503 void __process_breaks()
505 struct state_list *slist;
507 slist = pop_slist(&break_stack);
508 if (!slist) {
509 slist = clone_slist(cur_slist);
510 } else {
511 merge_slist(&slist, cur_slist);
513 push_slist(&break_stack, slist);
516 void __merge_breaks()
518 struct state_list *slist;
520 slist = pop_slist(&break_stack);
521 merge_slist(&cur_slist, slist);
522 free_slist(&slist);
525 void __use_breaks()
527 free_slist(&cur_slist);
528 cur_slist = pop_slist(&break_stack);
531 void __save_switch_states()
533 push_slist(&switch_stack, clone_slist(cur_slist));
536 void __merge_switches()
538 struct state_list *slist;
540 slist = pop_slist(&switch_stack);
541 merge_slist(&cur_slist, slist);
542 push_slist(&switch_stack, slist);
545 void __pop_switches()
547 struct state_list *slist;
549 slist = pop_slist(&switch_stack);
550 free_slist(&slist);
553 void __push_default()
555 push_slist(&default_stack, NULL);
558 void __set_default()
560 set_state_stack(&default_stack, "has_default", 0, NULL, &true_state);
563 int __pop_default()
565 struct state_list *slist;
567 slist = pop_slist(&default_stack);
568 if (slist) {
569 free_slist(&slist);
570 return 1;
572 return 0;
575 static struct named_slist *alloc_named_slist(const char *name,
576 struct state_list *slist)
578 struct named_slist *named_slist = __alloc_named_slist(0);
580 named_slist->name = (char *)name;
581 named_slist->slist = slist;
582 return named_slist;
585 void __save_gotos(const char *name)
587 struct state_list **slist;
589 slist = get_slist_from_named_stack(goto_stack, name);
590 if (slist) {
591 merge_slist(slist, cur_slist);
592 return;
593 } else {
594 struct state_list *slist;
595 struct named_slist *named_slist;
597 slist = clone_slist(cur_slist);
598 named_slist = alloc_named_slist(name, slist);
599 add_ptr_list(&goto_stack, named_slist);
603 void __merge_gotos(const char *name)
605 struct state_list **slist;
607 slist = get_slist_from_named_stack(goto_stack, name);
608 if (slist)
609 merge_slist(&cur_slist, *slist);