locking: handle spin_lock_irqsave_nested()
[smatch.git] / smatch_slist.c
bloba2f8cfe4810ab66445c8df3500fd6e464a8555a9
1 /*
2 * sparse/smatch_slist.c
4 * Copyright (C) 2008,2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include "smatch.h"
13 #include "smatch_slist.h"
15 #undef CHECKORDER
17 ALLOCATOR(smatch_state, "smatch state");
18 ALLOCATOR(sm_state, "sm state");
19 ALLOCATOR(named_slist, "named slist");
20 __DO_ALLOCATOR(char, 0, 1, "state names", sname);
22 void __print_slist(struct state_list *slist)
24 struct sm_state *state;
25 struct sm_state *poss;
26 int i;
28 printf("dumping slist at %d\n", get_lineno());
29 FOR_EACH_PTR(slist, state) {
30 printf("[%s] '%s'=%s (", check_name(state->owner), state->name,
31 show_state(state->state));
32 i = 0;
33 FOR_EACH_PTR(state->possible, poss) {
34 if (i++)
35 printf(", ");
36 printf("%s", show_state(poss->state));
37 } END_FOR_EACH_PTR(poss);
38 printf(")\n");
39 } END_FOR_EACH_PTR(state);
40 printf("---\n");
44 /* NULL states go at the end to simplify merge_slist */
45 int cmp_tracker(const struct sm_state *a, const struct sm_state *b)
47 int ret;
49 if (a == b)
50 return 0;
51 if (!b)
52 return -1;
53 if (!a)
54 return 1;
56 if (a->owner > b->owner)
57 return -1;
58 if (a->owner < b->owner)
59 return 1;
61 ret = strcmp(a->name, b->name);
62 if (ret)
63 return ret;
65 if (!b->sym && a->sym)
66 return -1;
67 if (!a->sym && b->sym)
68 return 1;
69 if (a->sym > b->sym)
70 return -1;
71 if (a->sym < b->sym)
72 return 1;
74 return 0;
77 static int cmp_sm_states(const struct sm_state *a, const struct sm_state *b)
79 int ret;
81 ret = cmp_tracker(a, b);
82 if (ret)
83 return ret;
85 /* todo: add hook for smatch_extra.c */
86 if (a->state > b->state)
87 return -1;
88 if (a->state < b->state)
89 return 1;
90 return 0;
93 static struct sm_state *alloc_state_no_name(int owner, const char *name,
94 struct symbol *sym,
95 struct smatch_state *state)
97 struct sm_state *tmp;
99 tmp = alloc_sm_state(owner, NULL, sym, state);
100 tmp->name = name;
101 return tmp;
104 void add_sm_state_slist(struct state_list **slist, struct sm_state *new)
106 struct sm_state *tmp;
108 FOR_EACH_PTR(*slist, tmp) {
109 if (cmp_sm_states(tmp, new) < 0)
110 continue;
111 else if (cmp_sm_states(tmp, new) == 0) {
112 return;
113 } else {
114 INSERT_CURRENT(new, tmp);
115 return;
117 } END_FOR_EACH_PTR(tmp);
118 add_ptr_list(slist, new);
121 static void add_possible(struct sm_state *sm, struct sm_state *new)
123 struct sm_state *tmp;
124 struct sm_state *tmp2;
126 if (!new) {
127 struct smatch_state *s;
129 s = merge_states(sm->owner, sm->name, sm->sym, sm->state, NULL);
130 tmp = alloc_state_no_name(sm->owner, sm->name, sm->sym, s);
131 add_sm_state_slist(&sm->possible, tmp);
132 return;
135 FOR_EACH_PTR(new->possible, tmp) {
136 tmp2 = alloc_state_no_name(tmp->owner,tmp->name, tmp->sym,
137 tmp->state);
138 add_sm_state_slist(&sm->possible, tmp2);
139 } END_FOR_EACH_PTR(tmp);
142 char *alloc_sname(const char *str)
144 char *tmp;
146 if (!str)
147 return NULL;
148 tmp = __alloc_sname(strlen(str) + 1);
149 strcpy(tmp, str);
150 return tmp;
153 struct sm_state *alloc_sm_state(int owner, const char *name,
154 struct symbol *sym, struct smatch_state *state)
156 struct sm_state *sm_state = __alloc_sm_state(0);
158 sm_state->name = alloc_sname(name);
159 sm_state->owner = owner;
160 sm_state->sym = sym;
161 sm_state->state = state;
162 sm_state->line = get_lineno();
163 sm_state->merged = 0;
164 sm_state->implied = 0;
165 sm_state->my_pool = NULL;
166 sm_state->left = NULL;
167 sm_state->right = NULL;
168 sm_state->nr_children = 1;
169 sm_state->possible = NULL;
170 add_ptr_list(&sm_state->possible, sm_state);
171 return sm_state;
174 static void free_sm_state(struct sm_state *sm)
176 free_slist(&sm->possible);
178 * fixme. Free the actual state.
179 * Right now we leave it until the end of the function
180 * because we don't want to double free it.
181 * Use the freelist to not double free things
185 static void free_all_sm_states(struct allocation_blob *blob)
187 unsigned int size = sizeof(struct sm_state);
188 unsigned int offset = 0;
190 while (offset < blob->offset) {
191 free_sm_state((struct sm_state *)(blob->data + offset));
192 offset += size;
196 /* At the end of every function we free all the sm_states */
197 void free_every_single_sm_state(void)
199 struct allocator_struct *desc = &sm_state_allocator;
200 struct allocation_blob *blob = desc->blobs;
202 desc->blobs = NULL;
203 desc->allocations = 0;
204 desc->total_bytes = 0;
205 desc->useful_bytes = 0;
206 desc->freelist = NULL;
207 while (blob) {
208 struct allocation_blob *next = blob->next;
209 free_all_sm_states(blob);
210 blob_free(blob, desc->chunking);
211 blob = next;
213 clear_sname_alloc();
216 struct sm_state *clone_sm(struct sm_state *s)
218 struct sm_state *ret;
220 ret = alloc_state_no_name(s->owner, s->name, s->sym, s->state);
221 ret->merged = s->merged;
222 ret->implied = s->implied;
223 /* clone_sm() doesn't copy the my_pools. Each state needs to have
224 only one my_pool. */
225 ret->possible = clone_slist(s->possible);
226 ret->left = s->left;
227 ret->right = s->right;
228 ret->nr_children = s->nr_children;
229 return ret;
232 int is_merged(struct sm_state *sm)
234 return sm->merged;
237 int is_implied(struct sm_state *sm)
239 return sm->implied;
242 int slist_has_state(struct state_list *slist, struct smatch_state *state)
244 struct sm_state *tmp;
246 FOR_EACH_PTR(slist, tmp) {
247 if (tmp->state == state)
248 return 1;
249 } END_FOR_EACH_PTR(tmp);
250 return 0;
253 static void check_order(struct state_list *slist)
255 #ifdef CHECKORDER
256 struct sm_state *state;
257 struct sm_state *last = NULL;
258 int printed = 0;
260 FOR_EACH_PTR(slist, state) {
261 if (last && cmp_tracker(state, last) <= 0) {
262 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
263 "%s vs %s\n", last->owner, state->owner,
264 last->sym, state->sym, last->name, state->name);
265 printed = 1;
267 last = state;
268 } END_FOR_EACH_PTR(state);
270 if (printed)
271 printf("======\n");
272 #endif
275 struct state_list *clone_slist(struct state_list *from_slist)
277 struct sm_state *state;
278 struct state_list *to_slist = NULL;
280 FOR_EACH_PTR(from_slist, state) {
281 add_ptr_list(&to_slist, state);
282 } END_FOR_EACH_PTR(state);
283 check_order(to_slist);
284 return to_slist;
287 struct state_list_stack *clone_stack(struct state_list_stack *from_stack)
289 struct state_list *slist;
290 struct state_list_stack *to_stack = NULL;
292 FOR_EACH_PTR(from_stack, slist) {
293 push_slist(&to_stack, slist);
294 } END_FOR_EACH_PTR(slist);
295 return to_stack;
298 struct smatch_state *merge_states(int owner, const char *name,
299 struct symbol *sym,
300 struct smatch_state *state1,
301 struct smatch_state *state2)
303 struct smatch_state *ret;
305 if (state1 == state2)
306 ret = state1;
307 else if (__has_merge_function(owner))
308 ret = __client_merge_function(owner, name, sym, state1, state2);
309 else if (!state1 || !state2)
310 ret = &undefined;
311 else
312 ret = &merged;
313 return ret;
317 * add_pool() adds a slist to ->pools. If the slist has already been
318 * added earlier then it doesn't get added a second time.
320 void add_pool(struct state_list_stack **pools, struct state_list *new)
322 struct state_list *tmp;
324 FOR_EACH_PTR(*pools, tmp) {
325 if (tmp < new)
326 continue;
327 else if (tmp == new) {
328 return;
329 } else {
330 INSERT_CURRENT(new, tmp);
331 return;
333 } END_FOR_EACH_PTR(tmp);
334 add_ptr_list(pools, new);
337 struct sm_state *merge_sm_states(struct sm_state *one, struct sm_state *two)
339 struct smatch_state *s;
340 struct sm_state *result;
342 if (one == two)
343 return one;
344 s = merge_states(one->owner, one->name, one->sym, one->state, two->state);
345 result = alloc_state_no_name(one->owner, one->name, one->sym, s);
346 if (one->line == two->line)
347 result->line = one->line;
348 result->merged = 1;
349 result->left = one;
350 result->right = two;
351 result->nr_children = one->nr_children + two->nr_children;
352 add_possible(result, one);
353 add_possible(result, two);
355 if (option_debug) {
356 struct sm_state *tmp;
357 int i = 0;
359 printf("%d merge name='%s' [%s] %s(L %d) + %s(L %d) => %s (",
360 get_lineno(), one->name, check_name(one->owner),
361 show_state(one->state), one->line,
362 show_state(two->state), two->line,
363 show_state(s));
365 FOR_EACH_PTR(result->possible, tmp) {
366 if (i++) {
367 printf(", ");
369 printf("%s", show_state(tmp->state));
370 } END_FOR_EACH_PTR(tmp);
371 printf(")\n");
374 return result;
377 struct sm_state *get_sm_state_slist(struct state_list *slist, int owner, const char *name,
378 struct symbol *sym)
380 struct sm_state *state;
382 if (!name)
383 return NULL;
385 FOR_EACH_PTR(slist, state) {
386 if (state->owner == owner && state->sym == sym
387 && !strcmp(state->name, name))
388 return state;
389 } END_FOR_EACH_PTR(state);
390 return NULL;
393 struct smatch_state *get_state_slist(struct state_list *slist,
394 int owner, const char *name,
395 struct symbol *sym)
397 struct sm_state *state;
399 state = get_sm_state_slist(slist, owner, name, sym);
400 if (state)
401 return state->state;
402 return NULL;
405 void overwrite_sm_state(struct state_list **slist, struct sm_state *new)
407 struct sm_state *tmp;
409 FOR_EACH_PTR(*slist, tmp) {
410 if (cmp_tracker(tmp, new) < 0)
411 continue;
412 else if (cmp_tracker(tmp, new) == 0) {
413 REPLACE_CURRENT_PTR(tmp, new);
414 return;
415 } else {
416 INSERT_CURRENT(new, tmp);
417 return;
419 } END_FOR_EACH_PTR(tmp);
420 add_ptr_list(slist, new);
423 void overwrite_sm_state_stack(struct state_list_stack **stack,
424 struct sm_state *state)
426 struct state_list *slist;
428 slist = pop_slist(stack);
429 overwrite_sm_state(&slist, state);
430 push_slist(stack, slist);
433 struct sm_state *set_state_slist(struct state_list **slist, int owner, const char *name,
434 struct symbol *sym, struct smatch_state *state)
436 struct sm_state *tmp;
437 struct sm_state *new = alloc_sm_state(owner, name, sym, state);
439 FOR_EACH_PTR(*slist, tmp) {
440 if (cmp_tracker(tmp, new) < 0)
441 continue;
442 else if (cmp_tracker(tmp, new) == 0) {
443 REPLACE_CURRENT_PTR(tmp, new);
444 return new;
445 } else {
446 INSERT_CURRENT(new, tmp);
447 return new;
449 } END_FOR_EACH_PTR(tmp);
450 add_ptr_list(slist, new);
451 return new;
454 void delete_state_slist(struct state_list **slist, int owner, const char *name,
455 struct symbol *sym)
457 struct sm_state *state;
459 FOR_EACH_PTR(*slist, state) {
460 if (state->owner == owner && state->sym == sym
461 && !strcmp(state->name, name)){
462 DELETE_CURRENT_PTR(state);
463 return;
465 } END_FOR_EACH_PTR(state);
468 void delete_state_stack(struct state_list_stack **stack, int owner, const char *name,
469 struct symbol *sym)
471 struct state_list *slist;
473 slist = pop_slist(stack);
474 delete_state_slist(&slist, owner, name, sym);
475 push_slist(stack, slist);
478 void push_slist(struct state_list_stack **list_stack, struct state_list *slist)
480 add_ptr_list(list_stack, slist);
483 struct state_list *pop_slist(struct state_list_stack **list_stack)
485 struct state_list *slist;
487 slist = last_ptr_list((struct ptr_list *)*list_stack);
488 delete_ptr_list_last((struct ptr_list **)list_stack);
489 return slist;
492 void free_slist(struct state_list **slist)
494 __free_ptr_list((struct ptr_list **)slist);
497 void free_stack(struct state_list_stack **stack)
499 __free_ptr_list((struct ptr_list **)stack);
502 void free_stack_and_slists(struct state_list_stack **slist_stack)
504 struct state_list *slist;
506 FOR_EACH_PTR(*slist_stack, slist) {
507 free_slist(&slist);
508 } END_FOR_EACH_PTR(slist);
509 free_stack(slist_stack);
513 * set_state_stack() sets the state for the top slist on the stack.
515 struct sm_state *set_state_stack(struct state_list_stack **stack, int owner, const char *name,
516 struct symbol *sym, struct smatch_state *state)
518 struct state_list *slist;
519 struct sm_state *sm;
521 slist = pop_slist(stack);
522 sm = set_state_slist(&slist, owner, name, sym, state);
523 push_slist(stack, slist);
525 return sm;
529 * get_sm_state_stack() gets the state for the top slist on the stack.
531 struct sm_state *get_sm_state_stack(struct state_list_stack *stack,
532 int owner, const char *name,
533 struct symbol *sym)
535 struct state_list *slist;
536 struct sm_state *ret;
538 slist = pop_slist(&stack);
539 ret = get_sm_state_slist(slist, owner, name, sym);
540 push_slist(&stack, slist);
541 return ret;
545 struct smatch_state *get_state_stack(struct state_list_stack *stack,
546 int owner, const char *name,
547 struct symbol *sym)
549 struct sm_state *state;
551 state = get_sm_state_stack(stack, owner, name, sym);
552 if (state)
553 return state->state;
554 return NULL;
557 static void match_states(struct state_list **one, struct state_list **two)
559 struct sm_state *one_state;
560 struct sm_state *two_state;
561 struct sm_state *tmp;
562 struct smatch_state *tmp_state;
563 struct state_list *add_to_one = NULL;
564 struct state_list *add_to_two = NULL;
566 PREPARE_PTR_LIST(*one, one_state);
567 PREPARE_PTR_LIST(*two, two_state);
568 for (;;) {
569 if (!one_state && !two_state)
570 break;
571 if (cmp_tracker(one_state, two_state) < 0) {
572 tmp_state = __client_unmatched_state_function(one_state);
573 tmp = alloc_state_no_name(one_state->owner,one_state->name,
574 one_state->sym, tmp_state);
575 add_ptr_list(&add_to_two, tmp);
576 NEXT_PTR_LIST(one_state);
577 } else if (cmp_tracker(one_state, two_state) == 0) {
578 NEXT_PTR_LIST(one_state);
579 NEXT_PTR_LIST(two_state);
580 } else {
581 tmp_state = __client_unmatched_state_function(two_state);
582 tmp = alloc_state_no_name(two_state->owner,two_state->name,
583 two_state->sym, tmp_state);
584 add_ptr_list(&add_to_one, tmp);
585 NEXT_PTR_LIST(two_state);
588 FINISH_PTR_LIST(two_state);
589 FINISH_PTR_LIST(one_state);
591 overwrite_slist(add_to_one, one);
592 overwrite_slist(add_to_two, two);
595 static void clone_pool_havers(struct state_list *slist)
597 struct sm_state *state;
598 struct sm_state *new;
600 FOR_EACH_PTR(slist, state) {
601 if (state->my_pool) {
602 new = clone_sm(state);
603 REPLACE_CURRENT_PTR(state, new);
605 } END_FOR_EACH_PTR(state);
609 * merge_slist() is called whenever paths merge, such as after
610 * an if statement. It takes the two slists and creates one.
612 void merge_slist(struct state_list **to, struct state_list *slist)
614 struct sm_state *one_state, *two_state, *tmp;
615 struct state_list *results = NULL;
616 struct state_list *implied_one = NULL;
617 struct state_list *implied_two = NULL;
619 check_order(*to);
620 check_order(slist);
622 /* merging a null and nonnull path gives you only the nonnull path */
623 if (!slist) {
624 return;
626 if (!*to) {
627 *to = clone_slist(slist);
628 return;
631 implied_one = clone_slist(*to);
632 implied_two = clone_slist(slist);
634 match_states(&implied_one, &implied_two);
636 clone_pool_havers(implied_one);
637 clone_pool_havers(implied_two);
639 PREPARE_PTR_LIST(implied_one, one_state);
640 PREPARE_PTR_LIST(implied_two, two_state);
641 for (;;) {
642 if (!one_state && !two_state)
643 break;
644 if (cmp_tracker(one_state, two_state) < 0) {
645 sm_msg("error: Internal smatch error.");
646 NEXT_PTR_LIST(one_state);
647 } else if (cmp_tracker(one_state, two_state) == 0) {
648 if (one_state != two_state) {
649 one_state->my_pool = implied_one;
650 two_state->my_pool = implied_two;
653 tmp = merge_sm_states(one_state, two_state);
654 add_ptr_list(&results, tmp);
655 NEXT_PTR_LIST(one_state);
656 NEXT_PTR_LIST(two_state);
657 } else {
658 sm_msg("error: Internal smatch error.");
659 NEXT_PTR_LIST(two_state);
662 FINISH_PTR_LIST(two_state);
663 FINISH_PTR_LIST(one_state);
665 free_slist(to);
666 *to = results;
670 * and_slist_stack() is basically the same as popping the top two slists,
671 * overwriting the one with the other and pushing it back on the stack.
672 * The difference is that it checks to see that a mutually exclusive
673 * state isn't included in both stacks. If smatch sees something like
674 * "if (a && !a)" it assumes the second one is true.
676 void and_slist_stack(struct state_list_stack **slist_stack)
678 struct sm_state *tmp;
679 struct state_list *right_slist = pop_slist(slist_stack);
681 FOR_EACH_PTR(right_slist, tmp) {
682 overwrite_sm_state_stack(slist_stack, tmp);
683 } END_FOR_EACH_PTR(tmp);
684 free_slist(&right_slist);
688 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
689 * It pops the two slists from the top of the stack and merges them
690 * together in a way that preserves the things they have in common
691 * but creates a merged state for most of the rest.
692 * You could have code that had: if (foo || foo) { foo->baz;
693 * It's this function which ensures smatch does the right thing.
695 void or_slist_stack(struct state_list_stack **pre_conds,
696 struct state_list *cur_slist,
697 struct state_list_stack **slist_stack)
699 struct state_list *new;
700 struct state_list *old;
701 struct state_list *res = NULL;
702 struct state_list *tmp_slist;
704 new = pop_slist(slist_stack);
705 old = pop_slist(slist_stack);
707 tmp_slist = pop_slist(pre_conds);
708 res = clone_slist(tmp_slist);
709 push_slist(pre_conds, tmp_slist);
710 overwrite_slist(old, &res);
712 tmp_slist = clone_slist(cur_slist);
713 overwrite_slist(new, &tmp_slist);
715 merge_slist(&res, tmp_slist);
717 push_slist(slist_stack, res);
718 free_slist(&tmp_slist);
719 free_slist(&new);
720 free_slist(&old);
724 * get_slist_from_named_stack() is only used for gotos.
726 struct state_list **get_slist_from_named_stack(struct named_stack *stack,
727 const char *name)
729 struct named_slist *tmp;
731 FOR_EACH_PTR(stack, tmp) {
732 if (!strcmp(tmp->name, name))
733 return &tmp->slist;
734 } END_FOR_EACH_PTR(tmp);
735 return NULL;
738 void overwrite_slist(struct state_list *from, struct state_list **to)
740 struct sm_state *tmp;
742 FOR_EACH_PTR(from, tmp) {
743 overwrite_sm_state(to, tmp);
744 } END_FOR_EACH_PTR(tmp);