2 * sparse/smatch_slist.c
4 * Copyright (C) 2008,2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
13 #include "smatch_slist.h"
18 ALLOCATOR(sm_state
, "smatch state");
19 ALLOCATOR(named_slist
, "named slist");
21 void __print_slist(struct state_list
*slist
)
23 struct sm_state
*state
;
24 struct sm_state
*poss
;
27 printf("dumping slist at %d\n", get_lineno());
28 FOR_EACH_PTR(slist
, state
) {
29 printf("%d '%s'=%s (", state
->owner
, state
->name
,
30 show_state(state
->state
));
32 FOR_EACH_PTR(state
->possible
, poss
) {
35 printf("%s", show_state(poss
->state
));
36 } END_FOR_EACH_PTR(poss
);
38 } END_FOR_EACH_PTR(state
);
43 /* NULL states go at the end to simplify merge_slist */
44 int cmp_tracker(const struct sm_state
*a
, const struct sm_state
*b
)
55 if (a
->owner
> b
->owner
)
57 if (a
->owner
< b
->owner
)
60 ret
= strcmp(a
->name
, b
->name
);
64 if (!b
->sym
&& a
->sym
)
66 if (!a
->sym
&& b
->sym
)
76 static int cmp_sm_states(const struct sm_state
*a
, const struct sm_state
*b
)
80 ret
= cmp_tracker(a
, b
);
84 /* todo: add hook for smatch_extra.c */
85 if (a
->state
> b
->state
)
87 if (a
->state
< b
->state
)
92 void add_sm_state_slist(struct state_list
**slist
, struct sm_state
*new)
96 FOR_EACH_PTR(*slist
, tmp
) {
97 if (cmp_sm_states(tmp
, new) < 0)
99 else if (cmp_sm_states(tmp
, new) == 0) {
102 INSERT_CURRENT(new, tmp
);
105 } END_FOR_EACH_PTR(tmp
);
106 add_ptr_list(slist
, new);
109 static void add_possible(struct sm_state
*sm
, struct sm_state
*new)
111 struct sm_state
*tmp
;
112 struct sm_state
*tmp2
;
115 struct smatch_state
*s
;
117 s
= merge_states(sm
->name
, sm
->owner
, sm
->sym
, sm
->state
, NULL
);
118 tmp
= alloc_state(sm
->name
, sm
->owner
, sm
->sym
, s
);
119 add_sm_state_slist(&sm
->possible
, tmp
);
123 FOR_EACH_PTR(new->possible
, tmp
) {
124 tmp2
= alloc_state(tmp
->name
, tmp
->owner
, tmp
->sym
, tmp
->state
);
125 add_sm_state_slist(&sm
->possible
, tmp2
);
126 } END_FOR_EACH_PTR(tmp
);
129 struct sm_state
*alloc_state(const char *name
, int owner
,
130 struct symbol
*sym
, struct smatch_state
*state
)
132 struct sm_state
*sm_state
= __alloc_sm_state(0);
134 sm_state
->name
= alloc_string(name
);
135 sm_state
->owner
= owner
;
137 sm_state
->state
= state
;
138 sm_state
->line
= get_lineno();
139 sm_state
->my_pools
= NULL
;
140 sm_state
->all_pools
= NULL
;
141 sm_state
->possible
= NULL
;
142 add_ptr_list(&sm_state
->possible
, sm_state
);
146 static void free_sm_state(struct sm_state
*sm
)
148 free_string(sm
->name
);
149 free_slist(&sm
->possible
);
150 free_stack(&sm
->my_pools
);
151 free_stack(&sm
->all_pools
);
153 * fixme. Free the actual state.
154 * Right now we leave it until the end of the function
155 * because we don't want to double free it.
156 * Use the freelist to not double free things
160 static void free_all_sm_states(struct allocation_blob
*blob
)
162 unsigned int size
= sizeof(struct sm_state
);
163 unsigned int offset
= 0;
165 while (offset
< blob
->offset
) {
166 free_sm_state((struct sm_state
*)(blob
->data
+ offset
));
171 /* At the end of every function we free all the sm_states */
172 void free_every_single_sm_state()
174 struct allocator_struct
*desc
= &sm_state_allocator
;
175 struct allocation_blob
*blob
= desc
->blobs
;
178 desc
->allocations
= 0;
179 desc
->total_bytes
= 0;
180 desc
->useful_bytes
= 0;
181 desc
->freelist
= NULL
;
183 struct allocation_blob
*next
= blob
->next
;
184 free_all_sm_states(blob
);
185 blob_free(blob
, desc
->chunking
);
190 struct sm_state
*clone_state(struct sm_state
*s
)
192 struct sm_state
*ret
;
193 struct sm_state
*poss
;
195 ret
= alloc_state(s
->name
, s
->owner
, s
->sym
, s
->state
);
197 ret
->my_pools
= clone_stack(s
->my_pools
);
198 ret
->all_pools
= clone_stack(s
->all_pools
);
199 FOR_EACH_PTR(s
->possible
, poss
) {
200 add_sm_state_slist(&ret
->possible
, poss
);
201 } END_FOR_EACH_PTR(poss
);
205 int slist_has_state(struct state_list
*slist
, struct smatch_state
*state
)
207 struct sm_state
*tmp
;
209 FOR_EACH_PTR(slist
, tmp
) {
210 if (tmp
->state
== state
)
212 } END_FOR_EACH_PTR(tmp
);
216 static void check_order(struct state_list
*slist
)
219 struct sm_state
*state
;
220 struct sm_state
*last
= NULL
;
223 FOR_EACH_PTR(slist
, state
) {
224 if (last
&& cmp_tracker(state
, last
) <= 0) {
225 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
226 "%s vs %s\n", last
->owner
, state
->owner
,
227 last
->sym
, state
->sym
, last
->name
, state
->name
);
231 } END_FOR_EACH_PTR(state
);
238 static void check_my_pools(struct sm_state
*sm
)
240 struct sm_state
*poss
;
241 struct state_list
*slist
;
243 if (sm
->state
!= &merged
)
246 FOR_EACH_PTR(sm
->possible
, poss
) {
247 if (poss
->state
== &merged
)
249 FOR_EACH_PTR(sm
->my_pools
, slist
) {
250 if (get_state_slist(slist
, sm
->name
, sm
->owner
, sm
->sym
)
253 } END_FOR_EACH_PTR(slist
);
254 printf("%d pool not found for '%s' possible state \"%s\".\n",
255 get_lineno(), sm
->name
, show_state(poss
->state
));
259 } END_FOR_EACH_PTR(poss
);
263 static void sanity_check_pools(struct state_list
*slist
)
266 struct sm_state
*tmp
;
268 FOR_EACH_PTR(slist
, tmp
) {
270 } END_FOR_EACH_PTR(tmp
);
274 struct state_list
*clone_slist(struct state_list
*from_slist
)
276 struct sm_state
*state
;
277 struct sm_state
*tmp
;
278 struct state_list
*to_slist
= NULL
;
280 FOR_EACH_PTR(from_slist
, state
) {
281 tmp
= clone_state(state
);
282 add_ptr_list(&to_slist
, tmp
);
283 } END_FOR_EACH_PTR(state
);
284 check_order(to_slist
);
288 struct state_list_stack
*clone_stack(struct state_list_stack
*from_stack
)
290 struct state_list
*slist
;
291 struct state_list_stack
*to_stack
= NULL
;
293 FOR_EACH_PTR(from_stack
, slist
) {
294 push_slist(&to_stack
, slist
);
295 } END_FOR_EACH_PTR(slist
);
299 struct smatch_state
*merge_states(const char *name
, int owner
,
301 struct smatch_state
*state1
,
302 struct smatch_state
*state2
)
304 struct smatch_state
*ret
;
306 if (state1
== state2
)
308 else if (__has_merge_function(owner
))
309 ret
= __client_merge_function(owner
, name
, sym
, state1
, state2
);
310 else if (!state1
|| !state2
)
318 * add_pool() adds a slist to ->pools. If the slist has already been
319 * added earlier then it doesn't get added a second time.
321 static void add_pool(struct state_list_stack
**pools
, struct state_list
*new)
323 struct state_list
*tmp
;
325 FOR_EACH_PTR(*pools
, tmp
) {
328 else if (tmp
== new) {
331 INSERT_CURRENT(new, tmp
);
334 } END_FOR_EACH_PTR(tmp
);
335 add_ptr_list(pools
, new);
338 static void copy_pools(struct sm_state
*to
, struct sm_state
*sm
)
340 struct state_list
*tmp
;
345 FOR_EACH_PTR(sm
->my_pools
, tmp
) {
346 add_pool(&to
->my_pools
, tmp
);
347 } END_FOR_EACH_PTR(tmp
);
349 FOR_EACH_PTR(sm
->all_pools
, tmp
) {
350 add_pool(&to
->all_pools
, tmp
);
351 } END_FOR_EACH_PTR(tmp
);
354 struct sm_state
*merge_sm_states(struct sm_state
*one
, struct sm_state
*two
)
356 struct smatch_state
*s
;
357 struct sm_state
*result
;
359 s
= merge_states(one
->name
, one
->owner
, one
->sym
, one
->state
,
360 (two
?two
->state
:NULL
));
361 result
= alloc_state(one
->name
, one
->owner
, one
->sym
, s
);
362 add_possible(result
, one
);
363 add_possible(result
, two
);
364 copy_pools(result
, one
);
365 copy_pools(result
, two
);
368 struct sm_state
*tmp
;
371 printf("%d merge name='%s' owner=%d: %s + %s => %s (",
372 get_lineno(), one
->name
, one
->owner
,
373 show_state(one
->state
), show_state(two
?two
->state
:NULL
),
376 FOR_EACH_PTR(result
->possible
, tmp
) {
380 printf("%s", show_state(tmp
->state
));
381 } END_FOR_EACH_PTR(tmp
);
388 struct sm_state
*get_sm_state_slist(struct state_list
*slist
, const char *name
,
389 int owner
, struct symbol
*sym
)
391 struct sm_state
*state
;
396 FOR_EACH_PTR(slist
, state
) {
397 if (state
->owner
== owner
&& state
->sym
== sym
398 && !strcmp(state
->name
, name
))
400 } END_FOR_EACH_PTR(state
);
404 struct smatch_state
*get_state_slist(struct state_list
*slist
,
405 const char *name
, int owner
,
408 struct sm_state
*state
;
410 state
= get_sm_state_slist(slist
, name
, owner
, sym
);
416 void overwrite_sm_state(struct state_list
**slist
, struct sm_state
*new)
418 struct sm_state
*tmp
;
420 FOR_EACH_PTR(*slist
, tmp
) {
421 if (cmp_tracker(tmp
, new) < 0)
423 else if (cmp_tracker(tmp
, new) == 0) {
424 REPLACE_CURRENT_PTR(tmp
, new);
427 INSERT_CURRENT(new, tmp
);
430 } END_FOR_EACH_PTR(tmp
);
431 add_ptr_list(slist
, new);
434 void overwrite_sm_state_stack(struct state_list_stack
**stack
,
435 struct sm_state
*state
)
437 struct state_list
*slist
;
439 slist
= pop_slist(stack
);
440 overwrite_sm_state(&slist
, state
);
441 push_slist(stack
, slist
);
444 void set_state_slist(struct state_list
**slist
, const char *name
, int owner
,
445 struct symbol
*sym
, struct smatch_state
*state
)
447 struct sm_state
*tmp
;
448 struct sm_state
*new = alloc_state(name
, owner
, sym
, state
);
450 FOR_EACH_PTR(*slist
, tmp
) {
451 if (cmp_tracker(tmp
, new) < 0)
453 else if (cmp_tracker(tmp
, new) == 0) {
454 REPLACE_CURRENT_PTR(tmp
, new);
457 INSERT_CURRENT(new, tmp
);
460 } END_FOR_EACH_PTR(tmp
);
461 add_ptr_list(slist
, new);
464 void delete_state_slist(struct state_list
**slist
, const char *name
, int owner
,
467 struct sm_state
*state
;
469 FOR_EACH_PTR(*slist
, state
) {
470 if (state
->owner
== owner
&& state
->sym
== sym
471 && !strcmp(state
->name
, name
)){
472 delete_ptr_list_entry((struct ptr_list
**)slist
,
476 } END_FOR_EACH_PTR(state
);
480 void push_slist(struct state_list_stack
**list_stack
, struct state_list
*slist
)
482 add_ptr_list(list_stack
, slist
);
485 struct state_list
*pop_slist(struct state_list_stack
**list_stack
)
487 struct state_list
*slist
;
489 slist
= last_ptr_list((struct ptr_list
*)*list_stack
);
490 delete_ptr_list_last((struct ptr_list
**)list_stack
);
494 void free_slist(struct state_list
**slist
)
496 __free_ptr_list((struct ptr_list
**)slist
);
499 void free_stack(struct state_list_stack
**stack
)
501 __free_ptr_list((struct ptr_list
**)stack
);
504 void free_stack_and_slists(struct state_list_stack
**slist_stack
)
506 struct state_list
*slist
;
508 FOR_EACH_PTR(*slist_stack
, slist
) {
510 } END_FOR_EACH_PTR(slist
);
511 free_stack(slist_stack
);
515 * set_state_stack() sets the state for the top slist on the stack.
517 void set_state_stack(struct state_list_stack
**stack
, const char *name
,
518 int owner
, struct symbol
*sym
, struct smatch_state
*state
)
520 struct state_list
*slist
;
522 slist
= pop_slist(stack
);
523 set_state_slist(&slist
, name
, owner
, sym
, state
);
524 push_slist(stack
, slist
);
528 * get_state_stack() gets the state for the top slist on the stack.
530 struct smatch_state
*get_state_stack(struct state_list_stack
*stack
,
531 const char *name
, int owner
,
534 struct state_list
*slist
;
535 struct smatch_state
*ret
;
537 slist
= pop_slist(&stack
);
538 ret
= get_state_slist(slist
, name
, owner
, sym
);
539 push_slist(&stack
, slist
);
544 * We want to find which states have been modified inside a branch.
545 * If you have 2 &merged states they could be different states really
546 * and maybe one or both were modified. We say it is unchanged if
547 * the ->state pointers are the same and they belong to the same pools.
548 * If they have been modified on both sides of a branch to the same thing,
549 * it's still OK to say they are the same, because that means they won't
550 * belong to any pools.
552 static int is_really_same(struct sm_state
*one
, struct sm_state
*two
)
554 struct state_list
*tmp1
;
555 struct state_list
*tmp2
;
557 if (one
->state
!= two
->state
)
560 PREPARE_PTR_LIST(one
->my_pools
, tmp1
);
561 PREPARE_PTR_LIST(two
->my_pools
, tmp2
);
567 } else if (tmp1
== tmp2
) {
574 FINISH_PTR_LIST(tmp2
);
575 FINISH_PTR_LIST(tmp1
);
579 static void register_implied_pool(struct state_list
*pool
)
583 FOR_EACH_PTR(pool
, sm
) {
584 if (sm
->state
!= &merged
)
585 free_stack(&sm
->my_pools
);
587 add_pool(&sm
->my_pools
, pool
);
588 add_pool(&sm
->all_pools
, pool
);
589 } END_FOR_EACH_PTR(sm
);
591 push_slist(&implied_pools
, pool
);
595 * merge_slist() is called whenever paths merge, such as after
596 * an if statement. It takes the two slists and creates one.
598 void merge_slist(struct state_list
**to
, struct state_list
*slist
)
600 struct sm_state
*to_state
, *state
, *tmp
;
601 struct state_list
*results
= NULL
;
602 struct state_list
*implied_to
= NULL
;
603 struct state_list
*implied_from
= NULL
;
607 sanity_check_pools(*to
);
608 sanity_check_pools(slist
);
610 /* merging a null and nonnull path gives you only the nonnull path */
615 *to
= clone_slist(slist
);
619 implied_to
= clone_slist(*to
);
620 implied_from
= clone_slist(slist
);
622 register_implied_pool(implied_to
);
623 register_implied_pool(implied_from
);
625 PREPARE_PTR_LIST(implied_to
, to_state
);
626 PREPARE_PTR_LIST(implied_from
, state
);
628 if (!to_state
&& !state
)
630 if (cmp_tracker(to_state
, state
) < 0) {
631 tmp
= merge_sm_states(to_state
, NULL
);
632 add_ptr_list(&results
, tmp
);
633 NEXT_PTR_LIST(to_state
);
634 } else if (cmp_tracker(to_state
, state
) == 0) {
635 tmp
= merge_sm_states(to_state
, state
);
636 add_ptr_list(&results
, tmp
);
637 NEXT_PTR_LIST(to_state
);
638 NEXT_PTR_LIST(state
);
640 tmp
= merge_sm_states(state
, NULL
);
641 add_ptr_list(&results
, tmp
);
642 NEXT_PTR_LIST(state
);
645 FINISH_PTR_LIST(state
);
646 FINISH_PTR_LIST(to_state
);
652 static int pool_in_pools(struct state_list_stack
*pools
,
653 struct state_list
*pool
)
655 struct state_list
*tmp
;
657 FOR_EACH_PTR(pools
, tmp
) {
660 } END_FOR_EACH_PTR(tmp
);
664 struct state_list
*clone_states_in_pool(struct state_list
*pool
,
665 struct state_list
*cur_slist
)
667 struct sm_state
*state
;
668 struct sm_state
*cur_state
;
669 struct sm_state
*tmp
;
670 struct state_list
*to_slist
= NULL
;
672 FOR_EACH_PTR(pool
, state
) {
673 cur_state
= get_sm_state_slist(cur_slist
, state
->name
,
674 state
->owner
, state
->sym
);
677 if (is_really_same(state
, cur_state
))
679 if (pool_in_pools(cur_state
->all_pools
, pool
)) {
680 tmp
= clone_state(state
);
681 add_ptr_list(&to_slist
, tmp
);
683 } END_FOR_EACH_PTR(state
);
684 sanity_check_pools(to_slist
);
689 * merge_implied() takes an implied state and another possibly implied state
690 * from another pool. It checks that the second pool is reachable from
691 * cur_slist then merges the two states and returns the result.
693 struct sm_state
*merge_implied(struct sm_state
*one
, struct sm_state
*two
,
694 struct state_list
*pool
,
695 struct state_list
*cur_slist
)
697 struct sm_state
*cur_state
;
699 cur_state
= get_sm_state_slist(cur_slist
, two
->name
, two
->owner
,
702 return NULL
; /* this can't actually happen */
703 if (!pool_in_pools(cur_state
->all_pools
, pool
))
705 return merge_sm_states(one
, two
);
709 * filter() is used to find what states are the same across
710 * a series of slists.
711 * It takes a **slist and a *filter.
712 * It removes everything from **slist that isn't in *filter.
713 * The reason you would want to do this is if you want to
714 * know what other states are true if one state is true. (smatch_implied).
716 void filter(struct state_list
**slist
, struct state_list
*filter
,
717 struct state_list
*cur_slist
)
719 struct sm_state
*s_one
, *s_two
;
720 struct state_list
*results
= NULL
;
721 struct sm_state
*tmp
;
726 PREPARE_PTR_LIST(*slist
, s_one
);
727 PREPARE_PTR_LIST(filter
, s_two
);
729 if (!s_one
|| !s_two
)
731 if (cmp_tracker(s_one
, s_two
) < 0) {
732 DIMPLIED("removed %s\n", s_one
->name
);
733 NEXT_PTR_LIST(s_one
);
734 } else if (cmp_tracker(s_one
, s_two
) == 0) {
735 tmp
= merge_implied(s_one
, s_two
, filter
, cur_slist
);
737 add_ptr_list(&results
, tmp
);
739 DIMPLIED("removed %s\n", s_one
->name
);
740 NEXT_PTR_LIST(s_one
);
741 NEXT_PTR_LIST(s_two
);
743 NEXT_PTR_LIST(s_two
);
746 FINISH_PTR_LIST(s_two
);
747 FINISH_PTR_LIST(s_one
);
749 sanity_check_pools(results
);
755 * and_slist_stack() is basically the same as popping the top two slists,
756 * overwriting the one with the other and pushing it back on the stack.
757 * The difference is that it checks to see that a mutually exclusive
758 * state isn't included in both stacks. If smatch sees something like
759 * "if (a && !a)" it prints a warning.
761 void and_slist_stack(struct state_list_stack
**slist_stack
)
763 struct sm_state
*tmp
;
764 struct smatch_state
*tmp_state
;
765 struct state_list
*tmp_slist
= pop_slist(slist_stack
);
767 FOR_EACH_PTR(tmp_slist
, tmp
) {
768 tmp_state
= get_state_stack(*slist_stack
, tmp
->name
,
769 tmp
->owner
, tmp
->sym
);
770 if (tmp_state
&& tmp_state
!= tmp
->state
) {
771 SM_DEBUG("mutually exclusive 'and' conditions states "
773 tmp
->name
, show_state(tmp_state
),
774 show_state(tmp
->state
));
776 set_state_stack(slist_stack
, tmp
->name
, tmp
->owner
, tmp
->sym
,
778 } END_FOR_EACH_PTR(tmp
);
779 free_slist(&tmp_slist
);
783 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
784 * It pops the two slists from the top of the stack and merges them
785 * together in a way that preserves the things they have in common
786 * but creates a merged state for most of the rest.
787 * You could have code that had: if (foo || foo) { foo->baz;
788 * It's this function which ensures smatch does the right thing.
790 void or_slist_stack(struct state_list_stack
**pre_conds
,
791 struct state_list
*cur_slist
,
792 struct state_list_stack
**slist_stack
)
794 struct state_list
*new;
795 struct state_list
*old
;
796 struct state_list
*res
= NULL
;
797 struct state_list
*tmp_slist
;
799 new = pop_slist(slist_stack
);
800 old
= pop_slist(slist_stack
);
802 tmp_slist
= pop_slist(pre_conds
);
803 res
= clone_slist(tmp_slist
);
804 push_slist(pre_conds
, tmp_slist
);
805 overwrite_slist(old
, &res
);
807 tmp_slist
= clone_slist(cur_slist
);
808 overwrite_slist(new, &tmp_slist
);
810 merge_slist(&res
, tmp_slist
);
812 push_slist(slist_stack
, res
);
813 free_slist(&tmp_slist
);
819 * get_slist_from_named_stack() is only used for gotos.
821 struct state_list
**get_slist_from_named_stack(struct named_stack
*stack
,
824 struct named_slist
*tmp
;
826 FOR_EACH_PTR(stack
, tmp
) {
827 if (!strcmp(tmp
->name
, name
))
829 } END_FOR_EACH_PTR(tmp
);
833 void overwrite_slist(struct state_list
*from
, struct state_list
**to
)
835 struct sm_state
*tmp
;
837 FOR_EACH_PTR(from
, tmp
) {
838 overwrite_sm_state(to
, tmp
);
839 } END_FOR_EACH_PTR(tmp
);
842 unsigned int __get_allocations()
844 return sm_state_allocator
.allocations
;