slist: use a fake cur_slist for handling unmatched_states.
[smatch.git] / smatch_slist.c
blob9ed91c1d4422455e09edd96c0477aa4861bbe787
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, 1, 4, "state names", sname);
22 static int sm_state_counter;
24 char *show_sm(struct sm_state *sm)
26 static char buf[256];
27 struct sm_state *tmp;
28 int pos;
29 int i;
31 pos = snprintf(buf, sizeof(buf), "[%s] '%s' = %s (",
32 check_name(sm->owner), sm->name, show_state(sm->state));
33 if (pos > sizeof(buf))
34 goto truncate;
36 i = 0;
37 FOR_EACH_PTR(sm->possible, tmp) {
38 if (i++)
39 pos += snprintf(buf + pos, sizeof(buf) - pos, ", ");
40 if (pos > sizeof(buf))
41 goto truncate;
42 pos += snprintf(buf + pos, sizeof(buf) - pos, "%s",
43 show_state(tmp->state));
44 if (pos > sizeof(buf))
45 goto truncate;
46 } END_FOR_EACH_PTR(tmp);
47 snprintf(buf + pos, sizeof(buf) - pos, ")");
49 return buf;
51 truncate:
52 for (i = 0; i < 3; i++)
53 buf[sizeof(buf) - 2 - i] = '.';
54 return buf;
57 void __print_slist(struct state_list *slist)
59 struct sm_state *sm;
61 printf("dumping slist at %d\n", get_lineno());
62 FOR_EACH_PTR(slist, sm) {
63 printf("%s\n", show_sm(sm));
64 } END_FOR_EACH_PTR(sm);
65 printf("---\n");
68 /* NULL states go at the end to simplify merge_slist */
69 int cmp_tracker(const struct sm_state *a, const struct sm_state *b)
71 int ret;
73 if (a == b)
74 return 0;
75 if (!b)
76 return -1;
77 if (!a)
78 return 1;
80 if (a->owner > b->owner)
81 return -1;
82 if (a->owner < b->owner)
83 return 1;
85 ret = strcmp(a->name, b->name);
86 if (ret)
87 return ret;
89 if (!b->sym && a->sym)
90 return -1;
91 if (!a->sym && b->sym)
92 return 1;
93 if (a->sym > b->sym)
94 return -1;
95 if (a->sym < b->sym)
96 return 1;
98 return 0;
101 static int cmp_sm_states(const struct sm_state *a, const struct sm_state *b)
103 int ret;
105 ret = cmp_tracker(a, b);
106 if (ret)
107 return ret;
109 /* todo: add hook for smatch_extra.c */
110 if (a->state > b->state)
111 return -1;
112 if (a->state < b->state)
113 return 1;
114 return 0;
117 static struct sm_state *alloc_sm_state(int owner, const char *name,
118 struct symbol *sym, struct smatch_state *state)
120 struct sm_state *sm_state = __alloc_sm_state(0);
122 sm_state_counter++;
124 sm_state->name = alloc_sname(name);
125 sm_state->owner = owner;
126 sm_state->sym = sym;
127 sm_state->state = state;
128 sm_state->line = get_lineno();
129 sm_state->merged = 0;
130 sm_state->implied = 0;
131 sm_state->pool = NULL;
132 sm_state->left = NULL;
133 sm_state->right = NULL;
134 sm_state->nr_children = 1;
135 sm_state->possible = NULL;
136 add_ptr_list(&sm_state->possible, sm_state);
137 return sm_state;
140 static struct sm_state *alloc_state_no_name(int owner, const char *name,
141 struct symbol *sym,
142 struct smatch_state *state)
144 struct sm_state *tmp;
146 tmp = alloc_sm_state(owner, NULL, sym, state);
147 tmp->name = name;
148 return tmp;
151 void add_sm_state_slist(struct state_list **slist, struct sm_state *new)
153 struct sm_state *tmp;
155 FOR_EACH_PTR(*slist, tmp) {
156 if (cmp_sm_states(tmp, new) < 0)
157 continue;
158 else if (cmp_sm_states(tmp, new) == 0) {
159 return;
160 } else {
161 INSERT_CURRENT(new, tmp);
162 return;
164 } END_FOR_EACH_PTR(tmp);
165 add_ptr_list(slist, new);
168 static void copy_possibles(struct sm_state *to, struct sm_state *from)
170 struct sm_state *tmp;
172 FOR_EACH_PTR(from->possible, tmp) {
173 add_sm_state_slist(&to->possible, tmp);
174 } END_FOR_EACH_PTR(tmp);
177 char *alloc_sname(const char *str)
179 char *tmp;
181 if (!str)
182 return NULL;
183 tmp = __alloc_sname(strlen(str) + 1);
184 strcpy(tmp, str);
185 return tmp;
188 int out_of_memory()
191 * I decided to use 50M here based on trial and error.
192 * It works out OK for the kernel and so it should work
193 * for most other projects as well.
195 if (sm_state_counter * sizeof(struct sm_state) >= 50000000)
196 return 1;
197 return 0;
200 int low_on_memory(void)
202 if (sm_state_counter * sizeof(struct sm_state) >= 25000000)
203 return 1;
204 return 0;
207 static void free_sm_state(struct sm_state *sm)
209 free_slist(&sm->possible);
211 * fixme. Free the actual state.
212 * Right now we leave it until the end of the function
213 * because we don't want to double free it.
214 * Use the freelist to not double free things
218 static void free_all_sm_states(struct allocation_blob *blob)
220 unsigned int size = sizeof(struct sm_state);
221 unsigned int offset = 0;
223 while (offset < blob->offset) {
224 free_sm_state((struct sm_state *)(blob->data + offset));
225 offset += size;
229 /* At the end of every function we free all the sm_states */
230 void free_every_single_sm_state(void)
232 struct allocator_struct *desc = &sm_state_allocator;
233 struct allocation_blob *blob = desc->blobs;
235 desc->blobs = NULL;
236 desc->allocations = 0;
237 desc->total_bytes = 0;
238 desc->useful_bytes = 0;
239 desc->freelist = NULL;
240 while (blob) {
241 struct allocation_blob *next = blob->next;
242 free_all_sm_states(blob);
243 blob_free(blob, desc->chunking);
244 blob = next;
246 clear_sname_alloc();
248 sm_state_counter = 0;
251 struct sm_state *clone_sm(struct sm_state *s)
253 struct sm_state *ret;
255 ret = alloc_state_no_name(s->owner, s->name, s->sym, s->state);
256 ret->merged = s->merged;
257 ret->implied = s->implied;
258 ret->line = s->line;
259 /* clone_sm() doesn't copy the pools. Each state needs to have
260 only one pool. */
261 ret->possible = clone_slist(s->possible);
262 ret->left = s->left;
263 ret->right = s->right;
264 ret->nr_children = s->nr_children;
265 return ret;
268 int is_merged(struct sm_state *sm)
270 return sm->merged;
273 int is_implied(struct sm_state *sm)
275 return sm->implied;
278 int slist_has_state(struct state_list *slist, struct smatch_state *state)
280 struct sm_state *tmp;
282 FOR_EACH_PTR(slist, tmp) {
283 if (tmp->state == state)
284 return 1;
285 } END_FOR_EACH_PTR(tmp);
286 return 0;
289 static void check_order(struct state_list *slist)
291 #ifdef CHECKORDER
292 struct sm_state *sm;
293 struct sm_state *last = NULL;
294 int printed = 0;
296 FOR_EACH_PTR(slist, sm) {
297 if (last && cmp_tracker(sm, last) <= 0) {
298 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
299 "%s vs %s\n", last->owner, sm->owner,
300 last->sym, sm->sym, last->name, sm->name);
301 printed = 1;
303 last = sm;
304 } END_FOR_EACH_PTR(sm);
306 if (printed)
307 printf("======\n");
308 #endif
311 struct state_list *clone_slist(struct state_list *from_slist)
313 struct sm_state *sm;
314 struct state_list *to_slist = NULL;
316 FOR_EACH_PTR(from_slist, sm) {
317 add_ptr_list(&to_slist, sm);
318 } END_FOR_EACH_PTR(sm);
319 return to_slist;
322 struct state_list_stack *clone_stack(struct state_list_stack *from_stack)
324 struct state_list *slist;
325 struct state_list_stack *to_stack = NULL;
327 FOR_EACH_PTR(from_stack, slist) {
328 push_slist(&to_stack, slist);
329 } END_FOR_EACH_PTR(slist);
330 return to_stack;
333 struct smatch_state *merge_states(int owner, const char *name,
334 struct symbol *sym,
335 struct smatch_state *state1,
336 struct smatch_state *state2)
338 struct smatch_state *ret;
340 if (state1 == state2)
341 ret = state1;
342 else if (__has_merge_function(owner))
343 ret = __client_merge_function(owner, state1, state2);
344 else if (!state1 || !state2)
345 ret = &undefined;
346 else
347 ret = &merged;
348 return ret;
351 struct sm_state *merge_sm_states(struct sm_state *one, struct sm_state *two)
353 struct smatch_state *s;
354 struct sm_state *result;
356 if (one == two)
357 return one;
358 s = merge_states(one->owner, one->name, one->sym, one->state, two->state);
359 result = alloc_state_no_name(one->owner, one->name, one->sym, s);
360 result->merged = 1;
361 result->left = one;
362 result->right = two;
363 result->nr_children = one->nr_children + two->nr_children;
364 copy_possibles(result, one);
365 copy_possibles(result, two);
367 if (option_debug) {
368 struct sm_state *tmp;
369 int i = 0;
371 printf("%d merge [%s] '%s' %s(L %d) + %s(L %d) => %s (",
372 get_lineno(), check_name(one->owner), one->name,
373 show_state(one->state), one->line,
374 show_state(two->state), two->line,
375 show_state(s));
377 FOR_EACH_PTR(result->possible, tmp) {
378 if (i++)
379 printf(", ");
380 printf("%s", show_state(tmp->state));
381 } END_FOR_EACH_PTR(tmp);
382 printf(")\n");
385 return result;
388 struct sm_state *get_sm_state_slist(struct state_list *slist, int owner, const char *name,
389 struct symbol *sym)
391 struct sm_state *sm;
393 if (!name)
394 return NULL;
396 FOR_EACH_PTR(slist, sm) {
397 if (sm->owner == owner && sm->sym == sym && !strcmp(sm->name, name))
398 return sm;
399 } END_FOR_EACH_PTR(sm);
400 return NULL;
403 struct smatch_state *get_state_slist(struct state_list *slist,
404 int owner, const char *name,
405 struct symbol *sym)
407 struct sm_state *sm;
409 sm = get_sm_state_slist(slist, owner, name, sym);
410 if (sm)
411 return sm->state;
412 return NULL;
415 void overwrite_sm_state(struct state_list **slist, struct sm_state *new)
417 struct sm_state *tmp;
419 FOR_EACH_PTR(*slist, tmp) {
420 if (cmp_tracker(tmp, new) < 0)
421 continue;
422 else if (cmp_tracker(tmp, new) == 0) {
423 REPLACE_CURRENT_PTR(tmp, new);
424 return;
425 } else {
426 INSERT_CURRENT(new, tmp);
427 return;
429 } END_FOR_EACH_PTR(tmp);
430 add_ptr_list(slist, new);
433 void overwrite_sm_state_stack(struct state_list_stack **stack,
434 struct sm_state *sm)
436 struct state_list *slist;
438 slist = pop_slist(stack);
439 overwrite_sm_state(&slist, sm);
440 push_slist(stack, slist);
443 struct sm_state *set_state_slist(struct state_list **slist, int owner, const char *name,
444 struct symbol *sym, struct smatch_state *state)
446 struct sm_state *tmp;
447 struct sm_state *new = alloc_sm_state(owner, name, sym, state);
449 FOR_EACH_PTR(*slist, tmp) {
450 if (cmp_tracker(tmp, new) < 0)
451 continue;
452 else if (cmp_tracker(tmp, new) == 0) {
453 REPLACE_CURRENT_PTR(tmp, new);
454 return new;
455 } else {
456 INSERT_CURRENT(new, tmp);
457 return new;
459 } END_FOR_EACH_PTR(tmp);
460 add_ptr_list(slist, new);
461 return new;
464 void delete_state_slist(struct state_list **slist, int owner, const char *name,
465 struct symbol *sym)
467 struct sm_state *sm;
469 FOR_EACH_PTR(*slist, sm) {
470 if (sm->owner == owner && sm->sym == sym && !strcmp(sm->name, name)) {
471 DELETE_CURRENT_PTR(sm);
472 return;
474 } END_FOR_EACH_PTR(sm);
477 void delete_state_stack(struct state_list_stack **stack, int owner, const char *name,
478 struct symbol *sym)
480 struct state_list *slist;
482 slist = pop_slist(stack);
483 delete_state_slist(&slist, owner, name, sym);
484 push_slist(stack, slist);
487 void push_slist(struct state_list_stack **list_stack, struct state_list *slist)
489 add_ptr_list(list_stack, slist);
492 struct state_list *pop_slist(struct state_list_stack **list_stack)
494 struct state_list *slist;
496 slist = last_ptr_list((struct ptr_list *)*list_stack);
497 delete_ptr_list_last((struct ptr_list **)list_stack);
498 return slist;
501 void free_slist(struct state_list **slist)
503 __free_ptr_list((struct ptr_list **)slist);
506 void free_stack(struct state_list_stack **stack)
508 __free_ptr_list((struct ptr_list **)stack);
511 void free_stack_and_slists(struct state_list_stack **slist_stack)
513 struct state_list *slist;
515 FOR_EACH_PTR(*slist_stack, slist) {
516 free_slist(&slist);
517 } END_FOR_EACH_PTR(slist);
518 free_stack(slist_stack);
522 * set_state_stack() sets the state for the top slist on the stack.
524 struct sm_state *set_state_stack(struct state_list_stack **stack, int owner, const char *name,
525 struct symbol *sym, struct smatch_state *state)
527 struct state_list *slist;
528 struct sm_state *sm;
530 slist = pop_slist(stack);
531 sm = set_state_slist(&slist, owner, name, sym, state);
532 push_slist(stack, slist);
534 return sm;
538 * get_sm_state_stack() gets the state for the top slist on the stack.
540 struct sm_state *get_sm_state_stack(struct state_list_stack *stack,
541 int owner, const char *name,
542 struct symbol *sym)
544 struct state_list *slist;
545 struct sm_state *ret;
547 slist = pop_slist(&stack);
548 ret = get_sm_state_slist(slist, owner, name, sym);
549 push_slist(&stack, slist);
550 return ret;
553 struct smatch_state *get_state_stack(struct state_list_stack *stack,
554 int owner, const char *name,
555 struct symbol *sym)
557 struct sm_state *sm;
559 sm = get_sm_state_stack(stack, owner, name, sym);
560 if (sm)
561 return sm->state;
562 return NULL;
565 static void match_states(struct state_list **one, struct state_list **two)
567 struct sm_state *one_sm;
568 struct sm_state *two_sm;
569 struct sm_state *tmp;
570 struct smatch_state *tmp_state;
571 struct state_list *add_to_one = NULL;
572 struct state_list *add_to_two = NULL;
574 PREPARE_PTR_LIST(*one, one_sm);
575 PREPARE_PTR_LIST(*two, two_sm);
576 for (;;) {
577 if (!one_sm && !two_sm)
578 break;
579 if (cmp_tracker(one_sm, two_sm) < 0) {
580 __set_fake_cur_slist_fast(*two);
581 tmp_state = __client_unmatched_state_function(one_sm);
582 __pop_fake_cur_slist_fast();
583 tmp = alloc_state_no_name(one_sm->owner, one_sm->name,
584 one_sm->sym, tmp_state);
585 add_ptr_list(&add_to_two, tmp);
586 NEXT_PTR_LIST(one_sm);
587 } else if (cmp_tracker(one_sm, two_sm) == 0) {
588 NEXT_PTR_LIST(one_sm);
589 NEXT_PTR_LIST(two_sm);
590 } else {
591 __set_fake_cur_slist_fast(*one);
592 tmp_state = __client_unmatched_state_function(two_sm);
593 __pop_fake_cur_slist_fast();
594 tmp = alloc_state_no_name(two_sm->owner, two_sm->name,
595 two_sm->sym, tmp_state);
596 add_ptr_list(&add_to_one, tmp);
597 NEXT_PTR_LIST(two_sm);
600 FINISH_PTR_LIST(two_sm);
601 FINISH_PTR_LIST(one_sm);
603 overwrite_slist(add_to_one, one);
604 overwrite_slist(add_to_two, two);
607 static void clone_pool_havers(struct state_list *slist)
609 struct sm_state *sm;
610 struct sm_state *new;
612 FOR_EACH_PTR(slist, sm) {
613 if (sm->pool) {
614 new = clone_sm(sm);
615 REPLACE_CURRENT_PTR(sm, new);
617 } END_FOR_EACH_PTR(sm);
620 int __slist_id;
622 * Sets the first state to the slist_id.
624 static void set_slist_id(struct state_list *slist)
626 struct smatch_state *state;
627 struct sm_state *tmp, *new;
629 state = alloc_state_num(++__slist_id);
630 new = alloc_sm_state(-1, "unnull_path", NULL, state);
632 FOR_EACH_PTR(slist, tmp) {
633 if (tmp->owner != (unsigned short)-1)
634 return;
635 REPLACE_CURRENT_PTR(tmp, new);
636 return;
637 } END_FOR_EACH_PTR(tmp);
640 int get_slist_id(struct state_list *slist)
642 struct sm_state *tmp;
644 FOR_EACH_PTR(slist, tmp) {
645 if (tmp->owner != (unsigned short)-1)
646 return 0;
647 return PTR_INT(tmp->state->data);
648 } END_FOR_EACH_PTR(tmp);
649 return 0;
653 * merge_slist() is called whenever paths merge, such as after
654 * an if statement. It takes the two slists and creates one.
656 void merge_slist(struct state_list **to, struct state_list *slist)
658 struct sm_state *one_sm, *two_sm, *tmp;
659 struct state_list *results = NULL;
660 struct state_list *implied_one = NULL;
661 struct state_list *implied_two = NULL;
663 if (out_of_memory())
664 return;
666 check_order(*to);
667 check_order(slist);
669 /* merging a null and nonnull path gives you only the nonnull path */
670 if (!slist)
671 return;
673 if (!*to) {
674 *to = clone_slist(slist);
675 return;
678 implied_one = clone_slist(*to);
679 implied_two = clone_slist(slist);
681 match_states(&implied_one, &implied_two);
683 clone_pool_havers(implied_one);
684 clone_pool_havers(implied_two);
686 set_slist_id(implied_one);
687 set_slist_id(implied_two);
689 PREPARE_PTR_LIST(implied_one, one_sm);
690 PREPARE_PTR_LIST(implied_two, two_sm);
691 for (;;) {
692 if (!one_sm && !two_sm)
693 break;
694 if (cmp_tracker(one_sm, two_sm) < 0) {
695 sm_msg("error: Internal smatch error.");
696 NEXT_PTR_LIST(one_sm);
697 } else if (cmp_tracker(one_sm, two_sm) == 0) {
698 if (one_sm != two_sm) {
699 one_sm->pool = implied_one;
700 two_sm->pool = implied_two;
703 tmp = merge_sm_states(one_sm, two_sm);
704 add_ptr_list(&results, tmp);
705 NEXT_PTR_LIST(one_sm);
706 NEXT_PTR_LIST(two_sm);
707 } else {
708 sm_msg("error: Internal smatch error.");
709 NEXT_PTR_LIST(two_sm);
712 FINISH_PTR_LIST(two_sm);
713 FINISH_PTR_LIST(one_sm);
715 free_slist(to);
716 *to = results;
720 * filter_slist() removes any sm states "slist" holds in common with "filter"
722 void filter_slist(struct state_list **slist, struct state_list *filter)
724 struct sm_state *one_sm, *two_sm;
725 struct state_list *results = NULL;
727 PREPARE_PTR_LIST(*slist, one_sm);
728 PREPARE_PTR_LIST(filter, two_sm);
729 for (;;) {
730 if (!one_sm && !two_sm)
731 break;
732 if (cmp_tracker(one_sm, two_sm) < 0) {
733 add_ptr_list(&results, one_sm);
734 NEXT_PTR_LIST(one_sm);
735 } else if (cmp_tracker(one_sm, two_sm) == 0) {
736 if (one_sm != two_sm)
737 add_ptr_list(&results, one_sm);
738 NEXT_PTR_LIST(one_sm);
739 NEXT_PTR_LIST(two_sm);
740 } else {
741 NEXT_PTR_LIST(two_sm);
744 FINISH_PTR_LIST(two_sm);
745 FINISH_PTR_LIST(one_sm);
747 free_slist(slist);
748 *slist = results;
752 * and_slist_stack() pops the top two slists, overwriting the one with
753 * the other and pushing it back on the stack.
755 void and_slist_stack(struct state_list_stack **slist_stack)
757 struct sm_state *tmp;
758 struct state_list *right_slist = pop_slist(slist_stack);
760 FOR_EACH_PTR(right_slist, tmp) {
761 overwrite_sm_state_stack(slist_stack, tmp);
762 } END_FOR_EACH_PTR(tmp);
763 free_slist(&right_slist);
767 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
768 * It pops the two slists from the top of the stack and merges them
769 * together in a way that preserves the things they have in common
770 * but creates a merged state for most of the rest.
771 * You could have code that had: if (foo || foo) { foo->baz;
772 * It's this function which ensures smatch does the right thing.
774 void or_slist_stack(struct state_list_stack **pre_conds,
775 struct state_list *cur_slist,
776 struct state_list_stack **slist_stack)
778 struct state_list *new;
779 struct state_list *old;
780 struct state_list *pre_slist;
781 struct state_list *res;
782 struct state_list *tmp_slist;
784 new = pop_slist(slist_stack);
785 old = pop_slist(slist_stack);
787 pre_slist = pop_slist(pre_conds);
788 push_slist(pre_conds, clone_slist(pre_slist));
790 res = clone_slist(pre_slist);
791 overwrite_slist(old, &res);
793 tmp_slist = clone_slist(cur_slist);
794 overwrite_slist(new, &tmp_slist);
796 merge_slist(&res, tmp_slist);
797 filter_slist(&res, pre_slist);
799 push_slist(slist_stack, res);
800 free_slist(&tmp_slist);
801 free_slist(&pre_slist);
802 free_slist(&new);
803 free_slist(&old);
807 * get_slist_from_named_stack() is only used for gotos.
809 struct state_list **get_slist_from_named_stack(struct named_stack *stack,
810 const char *name)
812 struct named_slist *tmp;
814 FOR_EACH_PTR(stack, tmp) {
815 if (!strcmp(tmp->name, name))
816 return &tmp->slist;
817 } END_FOR_EACH_PTR(tmp);
818 return NULL;
821 void overwrite_slist(struct state_list *from, struct state_list **to)
823 struct sm_state *tmp;
825 FOR_EACH_PTR(from, tmp) {
826 overwrite_sm_state(to, tmp);
827 } END_FOR_EACH_PTR(tmp);