Better debugging for check_memory.c
[smatch.git] / smatch_slist.c
blob926754cf11e1f6724cd2a8b20b2a5937625d33ae
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("%d '%s'=%s (", 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(const char *name, int owner,
94 struct symbol *sym,
95 struct smatch_state *state)
97 struct sm_state *tmp;
99 tmp = alloc_state(NULL, owner, 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->name, sm->owner, sm->sym, sm->state, NULL);
130 tmp = alloc_state_no_name(sm->name, sm->owner, 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->name, tmp->owner, 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_state(const char *name, int owner,
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->my_pool = NULL;
165 sm_state->pre_left = NULL;
166 sm_state->pre_right = NULL;
167 sm_state->possible = NULL;
168 add_ptr_list(&sm_state->possible, sm_state);
169 return sm_state;
172 static void free_sm_state(struct sm_state *sm)
174 free_slist(&sm->possible);
176 * fixme. Free the actual state.
177 * Right now we leave it until the end of the function
178 * because we don't want to double free it.
179 * Use the freelist to not double free things
183 static void free_all_sm_states(struct allocation_blob *blob)
185 unsigned int size = sizeof(struct sm_state);
186 unsigned int offset = 0;
188 while (offset < blob->offset) {
189 free_sm_state((struct sm_state *)(blob->data + offset));
190 offset += size;
194 /* At the end of every function we free all the sm_states */
195 void free_every_single_sm_state(void)
197 struct allocator_struct *desc = &sm_state_allocator;
198 struct allocation_blob *blob = desc->blobs;
200 desc->blobs = NULL;
201 desc->allocations = 0;
202 desc->total_bytes = 0;
203 desc->useful_bytes = 0;
204 desc->freelist = NULL;
205 while (blob) {
206 struct allocation_blob *next = blob->next;
207 free_all_sm_states(blob);
208 blob_free(blob, desc->chunking);
209 blob = next;
211 clear_sname_alloc();
214 struct sm_state *clone_state(struct sm_state *s)
216 struct sm_state *ret;
218 ret = alloc_state_no_name(s->name, s->owner, s->sym, s->state);
219 ret->line = s->line;
220 ret->merged = s->merged;
221 /* clone_state() doesn't copy the my_pools. Each state needs to have
222 only one my_pool. */
223 ret->possible = clone_slist(s->possible);
224 ret->pre_left = s->pre_left;
225 ret->pre_right = s->pre_right;
226 return ret;
229 int is_merged(struct sm_state *sm)
231 return sm->merged;
234 int slist_has_state(struct state_list *slist, struct smatch_state *state)
236 struct sm_state *tmp;
238 FOR_EACH_PTR(slist, tmp) {
239 if (tmp->state == state)
240 return 1;
241 } END_FOR_EACH_PTR(tmp);
242 return 0;
245 static void check_order(struct state_list *slist)
247 #ifdef CHECKORDER
248 struct sm_state *state;
249 struct sm_state *last = NULL;
250 int printed = 0;
252 FOR_EACH_PTR(slist, state) {
253 if (last && cmp_tracker(state, last) <= 0) {
254 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
255 "%s vs %s\n", last->owner, state->owner,
256 last->sym, state->sym, last->name, state->name);
257 printed = 1;
259 last = state;
260 } END_FOR_EACH_PTR(state);
262 if (printed)
263 printf("======\n");
264 #endif
267 struct state_list *clone_slist(struct state_list *from_slist)
269 struct sm_state *state;
270 struct state_list *to_slist = NULL;
272 FOR_EACH_PTR(from_slist, state) {
273 add_ptr_list(&to_slist, state);
274 } END_FOR_EACH_PTR(state);
275 check_order(to_slist);
276 return to_slist;
279 struct state_list_stack *clone_stack(struct state_list_stack *from_stack)
281 struct state_list *slist;
282 struct state_list_stack *to_stack = NULL;
284 FOR_EACH_PTR(from_stack, slist) {
285 push_slist(&to_stack, slist);
286 } END_FOR_EACH_PTR(slist);
287 return to_stack;
290 struct smatch_state *merge_states(const char *name, int owner,
291 struct symbol *sym,
292 struct smatch_state *state1,
293 struct smatch_state *state2)
295 struct smatch_state *ret;
297 if (state1 == state2)
298 ret = state1;
299 else if (__has_merge_function(owner))
300 ret = __client_merge_function(owner, name, sym, state1, state2);
301 else if (!state1 || !state2)
302 ret = &undefined;
303 else
304 ret = &merged;
305 return ret;
309 * add_pool() adds a slist to ->pools. If the slist has already been
310 * added earlier then it doesn't get added a second time.
312 void add_pool(struct state_list_stack **pools, struct state_list *new)
314 struct state_list *tmp;
316 FOR_EACH_PTR(*pools, tmp) {
317 if (tmp < new)
318 continue;
319 else if (tmp == new) {
320 return;
321 } else {
322 INSERT_CURRENT(new, tmp);
323 return;
325 } END_FOR_EACH_PTR(tmp);
326 add_ptr_list(pools, new);
329 struct sm_state *merge_sm_states(struct sm_state *one, struct sm_state *two)
331 struct smatch_state *s;
332 struct sm_state *result;
334 if (one == two)
335 return one;
336 s = merge_states(one->name, one->owner, one->sym, one->state, two->state);
337 result = alloc_state_no_name(one->name, one->owner, one->sym, s);
338 if (one->line == two->line)
339 result->line = one->line;
340 result->merged = 1;
341 result->pre_left = one;
342 result->pre_right = two;
343 add_possible(result, one);
344 add_possible(result, two);
346 if (debug_states) {
347 struct sm_state *tmp;
348 int i = 0;
350 printf("%d merge name='%s' owner=%d: %s + %s => %s (",
351 get_lineno(), one->name, one->owner,
352 show_state(one->state), show_state(two->state),
353 show_state(s));
355 FOR_EACH_PTR(result->possible, tmp) {
356 if (i++) {
357 printf(", ");
359 printf("%s", show_state(tmp->state));
360 } END_FOR_EACH_PTR(tmp);
361 printf(")\n");
364 return result;
367 struct sm_state *get_sm_state_slist(struct state_list *slist, const char *name,
368 int owner, struct symbol *sym)
370 struct sm_state *state;
372 if (!name)
373 return NULL;
375 FOR_EACH_PTR(slist, state) {
376 if (state->owner == owner && state->sym == sym
377 && !strcmp(state->name, name))
378 return state;
379 } END_FOR_EACH_PTR(state);
380 return NULL;
383 struct smatch_state *get_state_slist(struct state_list *slist,
384 const char *name, int owner,
385 struct symbol *sym)
387 struct sm_state *state;
389 state = get_sm_state_slist(slist, name, owner, sym);
390 if (state)
391 return state->state;
392 return NULL;
395 void overwrite_sm_state(struct state_list **slist, struct sm_state *new)
397 struct sm_state *tmp;
399 FOR_EACH_PTR(*slist, tmp) {
400 if (cmp_tracker(tmp, new) < 0)
401 continue;
402 else if (cmp_tracker(tmp, new) == 0) {
403 REPLACE_CURRENT_PTR(tmp, new);
404 return;
405 } else {
406 INSERT_CURRENT(new, tmp);
407 return;
409 } END_FOR_EACH_PTR(tmp);
410 add_ptr_list(slist, new);
413 void overwrite_sm_state_stack(struct state_list_stack **stack,
414 struct sm_state *state)
416 struct state_list *slist;
418 slist = pop_slist(stack);
419 overwrite_sm_state(&slist, state);
420 push_slist(stack, slist);
423 void set_state_slist(struct state_list **slist, const char *name, int owner,
424 struct symbol *sym, struct smatch_state *state)
426 struct sm_state *tmp;
427 struct sm_state *new = alloc_state(name, owner, sym, state);
429 FOR_EACH_PTR(*slist, tmp) {
430 if (cmp_tracker(tmp, new) < 0)
431 continue;
432 else if (cmp_tracker(tmp, new) == 0) {
433 REPLACE_CURRENT_PTR(tmp, new);
434 return;
435 } else {
436 INSERT_CURRENT(new, tmp);
437 return;
439 } END_FOR_EACH_PTR(tmp);
440 add_ptr_list(slist, new);
443 void delete_state_slist(struct state_list **slist, const char *name, int owner,
444 struct symbol *sym)
446 struct sm_state *state;
448 FOR_EACH_PTR(*slist, state) {
449 if (state->owner == owner && state->sym == sym
450 && !strcmp(state->name, name)){
451 DELETE_CURRENT_PTR(state);
452 return;
454 } END_FOR_EACH_PTR(state);
458 void push_slist(struct state_list_stack **list_stack, struct state_list *slist)
460 add_ptr_list(list_stack, slist);
463 struct state_list *pop_slist(struct state_list_stack **list_stack)
465 struct state_list *slist;
467 slist = last_ptr_list((struct ptr_list *)*list_stack);
468 delete_ptr_list_last((struct ptr_list **)list_stack);
469 return slist;
472 void free_slist(struct state_list **slist)
474 __free_ptr_list((struct ptr_list **)slist);
477 void free_stack(struct state_list_stack **stack)
479 __free_ptr_list((struct ptr_list **)stack);
482 void free_stack_and_slists(struct state_list_stack **slist_stack)
484 struct state_list *slist;
486 FOR_EACH_PTR(*slist_stack, slist) {
487 free_slist(&slist);
488 } END_FOR_EACH_PTR(slist);
489 free_stack(slist_stack);
493 * set_state_stack() sets the state for the top slist on the stack.
495 void set_state_stack(struct state_list_stack **stack, const char *name,
496 int owner, struct symbol *sym, struct smatch_state *state)
498 struct state_list *slist;
500 slist = pop_slist(stack);
501 set_state_slist(&slist, name, owner, sym, state);
502 push_slist(stack, slist);
506 * get_sm_state_stack() gets the state for the top slist on the stack.
508 struct sm_state *get_sm_state_stack(struct state_list_stack *stack,
509 const char *name, int owner,
510 struct symbol *sym)
512 struct state_list *slist;
513 struct sm_state *ret;
515 slist = pop_slist(&stack);
516 ret = get_sm_state_slist(slist, name, owner, sym);
517 push_slist(&stack, slist);
518 return ret;
522 struct smatch_state *get_state_stack(struct state_list_stack *stack,
523 const char *name, int owner,
524 struct symbol *sym)
526 struct sm_state *state;
528 state = get_sm_state_stack(stack, name, owner, sym);
529 if (state)
530 return state->state;
531 return NULL;
534 static void match_states(struct state_list **one, struct state_list **two)
536 struct sm_state *one_state;
537 struct sm_state *two_state;
538 struct sm_state *tmp;
539 struct smatch_state *tmp_state;
540 struct state_list *add_to_one = NULL;
541 struct state_list *add_to_two = NULL;
543 PREPARE_PTR_LIST(*one, one_state);
544 PREPARE_PTR_LIST(*two, two_state);
545 for (;;) {
546 if (!one_state && !two_state)
547 break;
548 if (cmp_tracker(one_state, two_state) < 0) {
549 tmp_state = __client_unmatched_state_function(one_state);
550 tmp = alloc_state_no_name(one_state->name,
551 one_state->owner,
552 one_state->sym, tmp_state);
553 add_ptr_list(&add_to_two, tmp);
554 NEXT_PTR_LIST(one_state);
555 } else if (cmp_tracker(one_state, two_state) == 0) {
556 NEXT_PTR_LIST(one_state);
557 NEXT_PTR_LIST(two_state);
558 } else {
559 tmp_state = __client_unmatched_state_function(two_state);
560 tmp = alloc_state_no_name(two_state->name,
561 two_state->owner,
562 two_state->sym, tmp_state);
563 add_ptr_list(&add_to_one, tmp);
564 NEXT_PTR_LIST(two_state);
567 FINISH_PTR_LIST(two_state);
568 FINISH_PTR_LIST(one_state);
570 overwrite_slist(add_to_one, one);
571 overwrite_slist(add_to_two, two);
574 static void clone_pool_havers(struct state_list *slist)
576 struct sm_state *state;
577 struct sm_state *new;
579 FOR_EACH_PTR(slist, state) {
580 if (state->my_pool) {
581 new = clone_state(state);
582 REPLACE_CURRENT_PTR(state, new);
584 } END_FOR_EACH_PTR(state);
588 * merge_slist() is called whenever paths merge, such as after
589 * an if statement. It takes the two slists and creates one.
591 void merge_slist(struct state_list **to, struct state_list *slist)
593 struct sm_state *to_state, *state, *tmp;
594 struct state_list *results = NULL;
595 struct state_list *implied_to = NULL;
596 struct state_list *implied_from = NULL;
598 check_order(*to);
599 check_order(slist);
601 /* merging a null and nonnull path gives you only the nonnull path */
602 if (!slist) {
603 return;
605 if (!*to) {
606 *to = clone_slist(slist);
607 return;
610 implied_to = clone_slist(*to);
611 implied_from = clone_slist(slist);
613 match_states(&implied_to, &implied_from);
615 clone_pool_havers(implied_to);
616 clone_pool_havers(implied_from);
618 PREPARE_PTR_LIST(implied_to, to_state);
619 PREPARE_PTR_LIST(implied_from, state);
620 for (;;) {
621 if (!to_state && !state)
622 break;
623 if (cmp_tracker(to_state, state) < 0) {
624 smatch_msg("error: Internal smatch error.");
625 NEXT_PTR_LIST(to_state);
626 } else if (cmp_tracker(to_state, state) == 0) {
627 if (to_state != state) {
628 if (to_state->my_pool)
629 smatch_msg("to_state '%s' has pools", to_state->name);
630 if (state->my_pool)
631 smatch_msg("state '%s' has pools", state->name);
632 to_state->my_pool = implied_to;
633 state->my_pool = implied_from;
636 tmp = merge_sm_states(to_state, state);
637 add_ptr_list(&results, tmp);
638 NEXT_PTR_LIST(to_state);
639 NEXT_PTR_LIST(state);
640 } else {
641 smatch_msg("error: Internal smatch error.");
642 NEXT_PTR_LIST(state);
645 FINISH_PTR_LIST(state);
646 FINISH_PTR_LIST(to_state);
648 free_slist(to);
649 *to = results;
653 * and_slist_stack() is basically the same as popping the top two slists,
654 * overwriting the one with the other and pushing it back on the stack.
655 * The difference is that it checks to see that a mutually exclusive
656 * state isn't included in both stacks. If smatch sees something like
657 * "if (a && !a)" it prints a warning.
659 void and_slist_stack(struct state_list_stack **slist_stack)
661 struct sm_state *tmp;
662 struct state_list *right_slist = pop_slist(slist_stack);
664 FOR_EACH_PTR(right_slist, tmp) {
665 overwrite_sm_state_stack(slist_stack, tmp);
666 } END_FOR_EACH_PTR(tmp);
667 free_slist(&right_slist);
671 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
672 * It pops the two slists from the top of the stack and merges them
673 * together in a way that preserves the things they have in common
674 * but creates a merged state for most of the rest.
675 * You could have code that had: if (foo || foo) { foo->baz;
676 * It's this function which ensures smatch does the right thing.
678 void or_slist_stack(struct state_list_stack **pre_conds,
679 struct state_list *cur_slist,
680 struct state_list_stack **slist_stack)
682 struct state_list *new;
683 struct state_list *old;
684 struct state_list *res = NULL;
685 struct state_list *tmp_slist;
687 new = pop_slist(slist_stack);
688 old = pop_slist(slist_stack);
690 tmp_slist = pop_slist(pre_conds);
691 res = clone_slist(tmp_slist);
692 push_slist(pre_conds, tmp_slist);
693 overwrite_slist(old, &res);
695 tmp_slist = clone_slist(cur_slist);
696 overwrite_slist(new, &tmp_slist);
698 merge_slist(&res, tmp_slist);
700 push_slist(slist_stack, res);
701 free_slist(&tmp_slist);
702 free_slist(&new);
703 free_slist(&old);
707 * get_slist_from_named_stack() is only used for gotos.
709 struct state_list **get_slist_from_named_stack(struct named_stack *stack,
710 const char *name)
712 struct named_slist *tmp;
714 FOR_EACH_PTR(stack, tmp) {
715 if (!strcmp(tmp->name, name))
716 return &tmp->slist;
717 } END_FOR_EACH_PTR(tmp);
718 return NULL;
721 void overwrite_slist(struct state_list *from, struct state_list **to)
723 struct sm_state *tmp;
725 FOR_EACH_PTR(from, tmp) {
726 overwrite_sm_state(to, tmp);
727 } END_FOR_EACH_PTR(tmp);