stree: move stree_id into the avl root
[smatch.git] / smatch_slist.c
blobd28455d04495392a4d630399af95f520292e2bdb
1 /*
2 * Copyright (C) 2008,2009 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include "smatch.h"
21 #include "smatch_slist.h"
23 #undef CHECKORDER
25 ALLOCATOR(smatch_state, "smatch state");
26 ALLOCATOR(sm_state, "sm state");
27 ALLOCATOR(named_stree, "named slist");
28 __DO_ALLOCATOR(char, 1, 4, "state names", sname);
30 static int sm_state_counter;
32 static struct stree_stack *all_pools;
34 char *show_sm(struct sm_state *sm)
36 static char buf[256];
37 struct sm_state *tmp;
38 int pos;
39 int i;
41 pos = snprintf(buf, sizeof(buf), "[%s] '%s' = %s (",
42 check_name(sm->owner), sm->name, show_state(sm->state));
43 if (pos > sizeof(buf))
44 goto truncate;
46 i = 0;
47 FOR_EACH_PTR(sm->possible, tmp) {
48 if (i++)
49 pos += snprintf(buf + pos, sizeof(buf) - pos, ", ");
50 if (pos > sizeof(buf))
51 goto truncate;
52 pos += snprintf(buf + pos, sizeof(buf) - pos, "%s",
53 show_state(tmp->state));
54 if (pos > sizeof(buf))
55 goto truncate;
56 } END_FOR_EACH_PTR(tmp);
57 snprintf(buf + pos, sizeof(buf) - pos, ")");
59 return buf;
61 truncate:
62 for (i = 0; i < 3; i++)
63 buf[sizeof(buf) - 2 - i] = '.';
64 return buf;
67 void __print_stree(struct stree *stree)
69 struct sm_state *sm;
71 printf("dumping stree at %d\n", get_lineno());
72 FOR_EACH_SM(stree, sm) {
73 printf("%s\n", show_sm(sm));
74 } END_FOR_EACH_SM(sm);
75 printf("---\n");
78 /* NULL states go at the end to simplify merge_slist */
79 int cmp_tracker(const struct sm_state *a, const struct sm_state *b)
81 int ret;
83 if (a == b)
84 return 0;
85 if (!b)
86 return -1;
87 if (!a)
88 return 1;
90 if (a->owner > b->owner)
91 return -1;
92 if (a->owner < b->owner)
93 return 1;
95 ret = strcmp(a->name, b->name);
96 if (ret)
97 return ret;
99 if (!b->sym && a->sym)
100 return -1;
101 if (!a->sym && b->sym)
102 return 1;
103 if (a->sym > b->sym)
104 return -1;
105 if (a->sym < b->sym)
106 return 1;
108 return 0;
111 static int cmp_sm_states(const struct sm_state *a, const struct sm_state *b, int preserve)
113 int ret;
115 ret = cmp_tracker(a, b);
116 if (ret)
117 return ret;
119 /* todo: add hook for smatch_extra.c */
120 if (a->state > b->state)
121 return -1;
122 if (a->state < b->state)
123 return 1;
124 /* This is obviously a massive disgusting hack but we need to preserve
125 * the unmerged states for smatch extra because we use them in
126 * smatch_db.c. Meanwhile if we preserve all the other unmerged states
127 * then it uses a lot of memory and we don't use it. Hence this hack.
129 * Also sometimes even just preserving every possible SMATCH_EXTRA state
130 * takes too much resources so we have to cap that. Capping is probably
131 * not often a problem in real life.
133 if (a->owner == SMATCH_EXTRA && preserve) {
134 if (a == b)
135 return 0;
136 if (a->merged == 1 && b->merged == 0)
137 return -1;
138 if (a->merged == 0)
139 return 1;
142 return 0;
145 static struct sm_state *alloc_sm_state(int owner, const char *name,
146 struct symbol *sym, struct smatch_state *state)
148 struct sm_state *sm_state = __alloc_sm_state(0);
150 sm_state_counter++;
152 sm_state->name = alloc_sname(name);
153 sm_state->owner = owner;
154 sm_state->sym = sym;
155 sm_state->state = state;
156 sm_state->line = get_lineno();
157 sm_state->merged = 0;
158 sm_state->implied = 0;
159 sm_state->pool = NULL;
160 sm_state->left = NULL;
161 sm_state->right = NULL;
162 sm_state->nr_children = 1;
163 sm_state->possible = NULL;
164 add_ptr_list(&sm_state->possible, sm_state);
165 return sm_state;
168 static struct sm_state *alloc_state_no_name(int owner, const char *name,
169 struct symbol *sym,
170 struct smatch_state *state)
172 struct sm_state *tmp;
174 tmp = alloc_sm_state(owner, NULL, sym, state);
175 tmp->name = name;
176 return tmp;
179 int too_many_possible(struct sm_state *sm)
181 if (ptr_list_size((struct ptr_list *)sm->possible) >= 100)
182 return 1;
183 return 0;
186 void add_possible_sm(struct sm_state *to, struct sm_state *new)
188 struct sm_state *tmp;
189 int preserve = 1;
191 if (too_many_possible(to))
192 preserve = 0;
194 FOR_EACH_PTR(to->possible, tmp) {
195 if (cmp_sm_states(tmp, new, preserve) < 0)
196 continue;
197 else if (cmp_sm_states(tmp, new, preserve) == 0) {
198 return;
199 } else {
200 INSERT_CURRENT(new, tmp);
201 return;
203 } END_FOR_EACH_PTR(tmp);
204 add_ptr_list(&to->possible, new);
207 static void copy_possibles(struct sm_state *to, struct sm_state *from)
209 struct sm_state *tmp;
211 FOR_EACH_PTR(from->possible, tmp) {
212 add_possible_sm(to, tmp);
213 } END_FOR_EACH_PTR(tmp);
216 char *alloc_sname(const char *str)
218 char *tmp;
220 if (!str)
221 return NULL;
222 tmp = __alloc_sname(strlen(str) + 1);
223 strcpy(tmp, str);
224 return tmp;
227 int out_of_memory()
230 * I decided to use 50M here based on trial and error.
231 * It works out OK for the kernel and so it should work
232 * for most other projects as well.
234 if (sm_state_counter * sizeof(struct sm_state) >= 50000000)
235 return 1;
236 return 0;
239 int low_on_memory(void)
241 if (sm_state_counter * sizeof(struct sm_state) >= 25000000)
242 return 1;
243 return 0;
246 static void free_sm_state(struct sm_state *sm)
248 free_slist(&sm->possible);
250 * fixme. Free the actual state.
251 * Right now we leave it until the end of the function
252 * because we don't want to double free it.
253 * Use the freelist to not double free things
257 static void free_all_sm_states(struct allocation_blob *blob)
259 unsigned int size = sizeof(struct sm_state);
260 unsigned int offset = 0;
262 while (offset < blob->offset) {
263 free_sm_state((struct sm_state *)(blob->data + offset));
264 offset += size;
268 /* At the end of every function we free all the sm_states */
269 void free_every_single_sm_state(void)
271 struct allocator_struct *desc = &sm_state_allocator;
272 struct allocation_blob *blob = desc->blobs;
274 desc->blobs = NULL;
275 desc->allocations = 0;
276 desc->total_bytes = 0;
277 desc->useful_bytes = 0;
278 desc->freelist = NULL;
279 while (blob) {
280 struct allocation_blob *next = blob->next;
281 free_all_sm_states(blob);
282 blob_free(blob, desc->chunking);
283 blob = next;
285 clear_sname_alloc();
286 clear_smatch_state_alloc();
288 free_stack_and_strees(&all_pools);
289 sm_state_counter = 0;
292 struct sm_state *clone_sm(struct sm_state *s)
294 struct sm_state *ret;
296 ret = alloc_state_no_name(s->owner, s->name, s->sym, s->state);
297 ret->merged = s->merged;
298 ret->implied = s->implied;
299 ret->line = s->line;
300 /* clone_sm() doesn't copy the pools. Each state needs to have
301 only one pool. */
302 ret->possible = clone_slist(s->possible);
303 ret->left = s->left;
304 ret->right = s->right;
305 ret->nr_children = s->nr_children;
306 return ret;
309 int is_merged(struct sm_state *sm)
311 return sm->merged;
314 int is_implied(struct sm_state *sm)
316 return sm->implied;
319 int slist_has_state(struct state_list *slist, struct smatch_state *state)
321 struct sm_state *tmp;
323 FOR_EACH_PTR(slist, tmp) {
324 if (tmp->state == state)
325 return 1;
326 } END_FOR_EACH_PTR(tmp);
327 return 0;
330 struct state_list *clone_slist(struct state_list *from_slist)
332 struct sm_state *sm;
333 struct state_list *to_slist = NULL;
335 FOR_EACH_PTR(from_slist, sm) {
336 add_ptr_list(&to_slist, sm);
337 } END_FOR_EACH_PTR(sm);
338 return to_slist;
341 struct smatch_state *merge_states(int owner, const char *name,
342 struct symbol *sym,
343 struct smatch_state *state1,
344 struct smatch_state *state2)
346 struct smatch_state *ret;
348 if (state1 == state2)
349 ret = state1;
350 else if (__has_merge_function(owner))
351 ret = __client_merge_function(owner, state1, state2);
352 else if (!state1 || !state2)
353 ret = &undefined;
354 else
355 ret = &merged;
356 return ret;
359 struct sm_state *merge_sm_states(struct sm_state *one, struct sm_state *two)
361 struct smatch_state *s;
362 struct sm_state *result;
364 if (one == two)
365 return one;
366 s = merge_states(one->owner, one->name, one->sym, one->state, two->state);
367 result = alloc_state_no_name(one->owner, one->name, one->sym, s);
368 result->merged = 1;
369 result->left = one;
370 result->right = two;
371 result->nr_children = one->nr_children + two->nr_children;
372 copy_possibles(result, one);
373 copy_possibles(result, two);
375 if (option_debug ||
376 strcmp(check_name(one->owner), option_debug_check) == 0) {
377 struct sm_state *tmp;
378 int i = 0;
380 printf("%d merge [%s] '%s' %s(L %d) + %s(L %d) => %s (",
381 get_lineno(), check_name(one->owner), one->name,
382 show_state(one->state), one->line,
383 show_state(two->state), two->line,
384 show_state(s));
386 FOR_EACH_PTR(result->possible, tmp) {
387 if (i++)
388 printf(", ");
389 printf("%s", show_state(tmp->state));
390 } END_FOR_EACH_PTR(tmp);
391 printf(")\n");
394 return result;
397 struct sm_state *get_sm_state_stree(struct stree *stree, int owner, const char *name,
398 struct symbol *sym)
400 struct tracker tracker = {
401 .owner = owner,
402 .name = (char *)name,
403 .sym = sym,
406 if (!name)
407 return NULL;
410 return avl_lookup(stree, (struct sm_state *)&tracker);
413 struct smatch_state *get_state_stree(struct stree *stree,
414 int owner, const char *name,
415 struct symbol *sym)
417 struct sm_state *sm;
419 sm = get_sm_state_stree(stree, owner, name, sym);
420 if (sm)
421 return sm->state;
422 return NULL;
425 /* FIXME: this is almost exactly the same as set_sm_state_slist() */
426 void overwrite_sm_state_stree(struct stree **stree, struct sm_state *new)
428 avl_insert(stree, new);
431 void overwrite_sm_state_stree_stack(struct stree_stack **stack,
432 struct sm_state *sm)
434 struct stree *stree;
436 stree = pop_stree(stack);
437 overwrite_sm_state_stree(&stree, sm);
438 push_stree(stack, stree);
441 struct sm_state *set_state_stree(struct stree **stree, int owner, const char *name,
442 struct symbol *sym, struct smatch_state *state)
444 struct sm_state *new = alloc_sm_state(owner, name, sym, state);
446 avl_insert(stree, new);
447 return new;
450 void delete_state_stree(struct stree **stree, int owner, const char *name,
451 struct symbol *sym)
453 struct tracker tracker = {
454 .owner = owner,
455 .name = (char *)name,
456 .sym = sym,
459 avl_remove(stree, (struct sm_state *)&tracker);
462 void delete_state_stree_stack(struct stree_stack **stack, int owner, const char *name,
463 struct symbol *sym)
465 struct stree *stree;
467 stree = pop_stree(stack);
468 delete_state_stree(&stree, owner, name, sym);
469 push_stree(stack, stree);
472 void push_stree(struct stree_stack **stack, struct stree *stree)
474 add_ptr_list(stack, stree);
477 struct stree *pop_stree(struct stree_stack **stack)
479 struct stree *stree;
481 stree = last_ptr_list((struct ptr_list *)*stack);
482 delete_ptr_list_last((struct ptr_list **)stack);
483 return stree;
486 void free_slist(struct state_list **slist)
488 __free_ptr_list((struct ptr_list **)slist);
491 void free_stree_stack(struct stree_stack **stack)
493 __free_ptr_list((struct ptr_list **)stack);
496 void free_stack_and_strees(struct stree_stack **stree_stack)
498 struct stree *stree;
500 FOR_EACH_PTR(*stree_stack, stree) {
501 free_stree(&stree);
502 } END_FOR_EACH_PTR(stree);
503 free_stree_stack(stree_stack);
506 struct sm_state *set_state_stree_stack(struct stree_stack **stack, int owner, const char *name,
507 struct symbol *sym, struct smatch_state *state)
509 struct stree *stree;
510 struct sm_state *sm;
512 stree = pop_stree(stack);
513 sm = set_state_stree(&stree, owner, name, sym, state);
514 push_stree(stack, stree);
516 return sm;
520 * get_sm_state_stack() gets the state for the top slist on the stack.
522 struct sm_state *get_sm_state_stree_stack(struct stree_stack *stack,
523 int owner, const char *name,
524 struct symbol *sym)
526 struct stree *stree;
527 struct sm_state *ret;
529 stree = pop_stree(&stack);
530 ret = get_sm_state_stree(stree, owner, name, sym);
531 push_stree(&stack, stree);
532 return ret;
535 struct smatch_state *get_state_stree_stack(struct stree_stack *stack,
536 int owner, const char *name,
537 struct symbol *sym)
539 struct sm_state *sm;
541 sm = get_sm_state_stree_stack(stack, owner, name, sym);
542 if (sm)
543 return sm->state;
544 return NULL;
547 static void match_states_stree(struct stree **one, struct stree **two)
549 struct smatch_state *tmp_state;
550 struct sm_state *tmp_sm;
551 struct stree *add_to_one = NULL;
552 struct stree *add_to_two = NULL;
553 AvlIter one_iter;
554 AvlIter two_iter;
556 avl_iter_begin(&one_iter, *one, FORWARD);
557 avl_iter_begin(&two_iter, *two, FORWARD);
559 for (;;) {
560 if (!one_iter.sm && !two_iter.sm)
561 break;
562 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
563 __set_fake_cur_stree_fast(*two);
564 tmp_state = __client_unmatched_state_function(one_iter.sm);
565 __pop_fake_cur_stree_fast();
566 tmp_sm = alloc_state_no_name(one_iter.sm->owner, one_iter.sm->name,
567 one_iter.sm->sym, tmp_state);
568 avl_insert(&add_to_two, tmp_sm);
569 avl_iter_next(&one_iter);
570 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
571 avl_iter_next(&one_iter);
572 avl_iter_next(&two_iter);
573 } else {
574 __set_fake_cur_stree_fast(*one);
575 tmp_state = __client_unmatched_state_function(two_iter.sm);
576 __pop_fake_cur_stree_fast();
577 tmp_sm = alloc_state_no_name(two_iter.sm->owner, two_iter.sm->name,
578 two_iter.sm->sym, tmp_state);
579 avl_insert(&add_to_one, tmp_sm);
580 avl_iter_next(&two_iter);
584 overwrite_stree(add_to_one, one);
585 overwrite_stree(add_to_two, two);
588 static void clone_pool_havers_stree(struct stree **stree)
590 struct sm_state *sm, *tmp;
591 struct state_list *slist = NULL;
593 FOR_EACH_SM(*stree, sm) {
594 if (sm->pool) {
595 tmp = clone_sm(sm);
596 add_ptr_list(&slist, tmp);
598 } END_FOR_EACH_SM(sm);
600 FOR_EACH_PTR(slist, sm) {
601 avl_insert(stree, sm);
602 } END_FOR_EACH_PTR(sm);
604 free_slist(&slist);
607 int __stree_id;
610 * merge_slist() is called whenever paths merge, such as after
611 * an if statement. It takes the two slists and creates one.
613 void merge_stree(struct stree **to, struct stree *stree)
615 struct stree *results = NULL;
616 struct stree *implied_one = NULL;
617 struct stree *implied_two = NULL;
618 AvlIter one_iter;
619 AvlIter two_iter;
620 struct sm_state *tmp_sm;
622 if (out_of_memory())
623 return;
625 /* merging a null and nonnull path gives you only the nonnull path */
626 if (!stree)
627 return;
629 if (!*to) {
630 *to = clone_stree(stree);
631 return;
634 implied_one = clone_stree(*to);
635 implied_two = clone_stree(stree);
637 match_states_stree(&implied_one, &implied_two);
639 clone_pool_havers_stree(&implied_one);
640 clone_pool_havers_stree(&implied_two);
642 set_stree_id(implied_one, ++__stree_id);
643 set_stree_id(implied_two, ++__stree_id);
645 push_stree(&all_pools, implied_one);
646 push_stree(&all_pools, implied_two);
648 avl_iter_begin(&one_iter, implied_one, FORWARD);
649 avl_iter_begin(&two_iter, implied_two, FORWARD);
651 for (;;) {
652 if (!one_iter.sm && !two_iter.sm)
653 break;
654 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
655 sm_msg("error: Internal smatch error.");
656 avl_iter_next(&one_iter);
657 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
658 if (one_iter.sm != two_iter.sm) {
659 one_iter.sm->pool = implied_one;
660 two_iter.sm->pool = implied_two;
662 tmp_sm = merge_sm_states(one_iter.sm, two_iter.sm);
663 avl_insert(&results, tmp_sm);
664 avl_iter_next(&one_iter);
665 avl_iter_next(&two_iter);
666 } else {
667 sm_msg("error: Internal smatch error.");
668 avl_iter_next(&two_iter);
672 free_stree(to);
673 *to = results;
677 * filter_slist() removes any sm states "slist" holds in common with "filter"
679 void filter_stree(struct stree **stree, struct stree *filter)
681 struct stree *results = NULL;
682 AvlIter one_iter;
683 AvlIter two_iter;
685 avl_iter_begin(&one_iter, *stree, FORWARD);
686 avl_iter_begin(&two_iter, filter, FORWARD);
688 /* FIXME: This should probably be re-written with trees in mind */
690 for (;;) {
691 if (!one_iter.sm && !two_iter.sm)
692 break;
693 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
694 avl_insert(&results, one_iter.sm);
695 avl_iter_next(&one_iter);
696 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
697 if (one_iter.sm != two_iter.sm)
698 avl_insert(&results, one_iter.sm);
699 avl_iter_next(&one_iter);
700 avl_iter_next(&two_iter);
701 } else {
702 avl_iter_next(&two_iter);
706 free_stree(stree);
707 *stree = results;
712 * and_slist_stack() pops the top two slists, overwriting the one with
713 * the other and pushing it back on the stack.
715 void and_stree_stack(struct stree_stack **stack)
717 struct sm_state *tmp;
718 struct stree *right_stree = pop_stree(stack);
720 FOR_EACH_SM(right_stree, tmp) {
721 overwrite_sm_state_stree_stack(stack, tmp);
722 } END_FOR_EACH_SM(tmp);
723 free_stree(&right_stree);
727 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
728 * It pops the two slists from the top of the stack and merges them
729 * together in a way that preserves the things they have in common
730 * but creates a merged state for most of the rest.
731 * You could have code that had: if (foo || foo) { foo->baz;
732 * It's this function which ensures smatch does the right thing.
734 void or_stree_stack(struct stree_stack **pre_conds,
735 struct stree *cur_stree,
736 struct stree_stack **stack)
738 struct stree *new;
739 struct stree *old;
740 struct stree *pre_stree;
741 struct stree *res;
742 struct stree *tmp_stree;
744 new = pop_stree(stack);
745 old = pop_stree(stack);
747 pre_stree = pop_stree(pre_conds);
748 push_stree(pre_conds, clone_stree(pre_stree));
750 res = clone_stree(pre_stree);
751 overwrite_stree(old, &res);
753 tmp_stree = clone_stree(cur_stree);
754 overwrite_stree(new, &tmp_stree);
756 merge_stree(&res, tmp_stree);
757 filter_stree(&res, pre_stree);
759 push_stree(stack, res);
760 free_stree(&tmp_stree);
761 free_stree(&pre_stree);
762 free_stree(&new);
763 free_stree(&old);
767 * get_named_stree() is only used for gotos.
769 struct stree **get_named_stree(struct named_stree_stack *stack,
770 const char *name)
772 struct named_stree *tmp;
774 FOR_EACH_PTR(stack, tmp) {
775 if (!strcmp(tmp->name, name))
776 return &tmp->stree;
777 } END_FOR_EACH_PTR(tmp);
778 return NULL;
781 /* FIXME: These parameters are in a different order from expected */
782 void overwrite_stree(struct stree *from, struct stree **to)
784 struct sm_state *tmp;
786 FOR_EACH_SM(from, tmp) {
787 overwrite_sm_state_stree(to, tmp);
788 } END_FOR_EACH_SM(tmp);