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"
17 ALLOCATOR(sm_state
, "smatch state");
18 ALLOCATOR(named_slist
, "named slist");
20 void __print_slist(struct state_list
*slist
)
22 struct sm_state
*state
;
23 struct sm_state
*poss
;
26 printf("dumping slist at %d\n", get_lineno());
27 FOR_EACH_PTR(slist
, state
) {
28 printf("%d '%s'=%s (", state
->owner
, state
->name
,
29 show_state(state
->state
));
31 FOR_EACH_PTR(state
->possible
, poss
) {
34 printf("%s", show_state(poss
->state
));
35 } END_FOR_EACH_PTR(poss
);
37 } END_FOR_EACH_PTR(state
);
42 /* NULL states go at the end to simplify merge_slist */
43 int cmp_tracker(const struct sm_state
*a
, const struct sm_state
*b
)
54 if (a
->owner
> b
->owner
)
56 if (a
->owner
< b
->owner
)
59 ret
= strcmp(a
->name
, b
->name
);
63 if (!b
->sym
&& a
->sym
)
65 if (!a
->sym
&& b
->sym
)
75 static int cmp_sm_states(const struct sm_state
*a
, const struct sm_state
*b
)
79 ret
= cmp_tracker(a
, b
);
83 /* todo: add hook for smatch_extra.c */
84 if (a
->state
> b
->state
)
86 if (a
->state
< b
->state
)
91 void add_sm_state_slist(struct state_list
**slist
, struct sm_state
*new)
95 FOR_EACH_PTR(*slist
, tmp
) {
96 if (cmp_sm_states(tmp
, new) < 0)
98 else if (cmp_sm_states(tmp
, new) == 0) {
101 INSERT_CURRENT(new, tmp
);
104 } END_FOR_EACH_PTR(tmp
);
105 add_ptr_list(slist
, new);
108 static void add_possible(struct sm_state
*sm
, struct sm_state
*new)
110 struct sm_state
*tmp
;
111 struct sm_state
*tmp2
;
114 struct smatch_state
*s
;
116 s
= merge_states(sm
->name
, sm
->owner
, sm
->sym
, sm
->state
, NULL
);
117 tmp
= alloc_state(sm
->name
, sm
->owner
, sm
->sym
, s
);
118 add_sm_state_slist(&sm
->possible
, tmp
);
122 FOR_EACH_PTR(new->possible
, tmp
) {
123 tmp2
= alloc_state(tmp
->name
, tmp
->owner
, tmp
->sym
, tmp
->state
);
124 add_sm_state_slist(&sm
->possible
, tmp2
);
125 } END_FOR_EACH_PTR(tmp
);
128 struct sm_state
*alloc_state(const char *name
, int owner
,
129 struct symbol
*sym
, struct smatch_state
*state
)
131 struct sm_state
*sm_state
= __alloc_sm_state(0);
133 sm_state
->name
= alloc_string(name
);
134 sm_state
->owner
= owner
;
136 sm_state
->state
= state
;
137 sm_state
->my_pools
= NULL
;
138 sm_state
->all_pools
= NULL
;
139 sm_state
->possible
= NULL
;
140 add_ptr_list(&sm_state
->possible
, sm_state
);
144 static void free_sm_state_lists(struct allocation_blob
*blob
)
146 unsigned int size
= sizeof(struct sm_state
);
147 unsigned int offset
= 0;
149 while (offset
< blob
->offset
) {
150 struct sm_state
*sm
= (struct sm_state
*)(blob
->data
+ offset
);
152 free_string(sm
->name
);
153 free_slist(&sm
->possible
);
154 free_stack(&sm
->my_pools
);
155 free_stack(&sm
->all_pools
);
160 /* At the end of every function we free all the sm_states */
161 void free_every_single_sm_state()
163 struct allocator_struct
*desc
= &sm_state_allocator
;
164 struct allocation_blob
*blob
= desc
->blobs
;
167 desc
->allocations
= 0;
168 desc
->total_bytes
= 0;
169 desc
->useful_bytes
= 0;
170 desc
->freelist
= NULL
;
172 struct allocation_blob
*next
= blob
->next
;
173 free_sm_state_lists(blob
);
174 blob_free(blob
, desc
->chunking
);
179 struct sm_state
*clone_state(struct sm_state
*s
)
181 struct sm_state
*ret
;
182 struct sm_state
*poss
;
184 ret
= alloc_state(s
->name
, s
->owner
, s
->sym
, s
->state
);
185 ret
->my_pools
= clone_stack(s
->my_pools
);
186 ret
->all_pools
= clone_stack(s
->all_pools
);
187 FOR_EACH_PTR(s
->possible
, poss
) {
188 add_sm_state_slist(&ret
->possible
, poss
);
189 } END_FOR_EACH_PTR(poss
);
193 int slist_has_state(struct state_list
*slist
, struct smatch_state
*state
)
195 struct sm_state
*tmp
;
197 FOR_EACH_PTR(slist
, tmp
) {
198 if (tmp
->state
== state
)
200 } END_FOR_EACH_PTR(tmp
);
205 static void check_order(struct state_list
*slist
)
207 struct sm_state
*state
;
208 struct sm_state
*last
= NULL
;
211 FOR_EACH_PTR(slist
, state
) {
212 if (last
&& cmp_tracker(state
, last
) <= 0) {
213 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
214 "%s vs %s\n", last
->owner
, state
->owner
,
215 last
->sym
, state
->sym
, last
->name
, state
->name
);
219 } END_FOR_EACH_PTR(state
);
226 struct state_list
*clone_slist(struct state_list
*from_slist
)
228 struct sm_state
*state
;
229 struct sm_state
*tmp
;
230 struct state_list
*to_slist
= NULL
;
232 FOR_EACH_PTR(from_slist
, state
) {
233 tmp
= clone_state(state
);
234 add_ptr_list(&to_slist
, tmp
);
235 } END_FOR_EACH_PTR(state
);
237 check_order(to_slist
);
242 struct state_list_stack
*clone_stack(struct state_list_stack
*from_stack
)
244 struct state_list
*slist
;
245 struct state_list_stack
*to_stack
= NULL
;
247 FOR_EACH_PTR(from_stack
, slist
) {
248 push_slist(&to_stack
, slist
);
249 } END_FOR_EACH_PTR(slist
);
253 struct smatch_state
*merge_states(const char *name
, int owner
,
255 struct smatch_state
*state1
,
256 struct smatch_state
*state2
)
258 struct smatch_state
*ret
;
260 if (state1
== state2
)
262 else if (__has_merge_function(owner
))
263 ret
= __client_merge_function(owner
, name
, sym
, state1
, state2
);
264 else if (!state1
|| !state2
)
272 * add_pool() adds a slist to ->pools. If the slist has already been
273 * added earlier then it doesn't get added a second time.
275 static void add_pool(struct state_list_stack
**pools
, struct state_list
*new)
277 struct state_list
*tmp
;
279 FOR_EACH_PTR(*pools
, tmp
) {
282 else if (tmp
== new) {
285 INSERT_CURRENT(new, tmp
);
288 } END_FOR_EACH_PTR(tmp
);
289 add_ptr_list(pools
, new);
292 static void copy_pools(struct sm_state
*to
, struct sm_state
*sm
)
294 struct state_list
*tmp
;
299 FOR_EACH_PTR(sm
->my_pools
, tmp
) {
300 add_pool(&to
->my_pools
, tmp
);
301 } END_FOR_EACH_PTR(tmp
);
303 FOR_EACH_PTR(sm
->all_pools
, tmp
) {
304 add_pool(&to
->all_pools
, tmp
);
305 } END_FOR_EACH_PTR(tmp
);
308 struct sm_state
*merge_sm_states(struct sm_state
*one
, struct sm_state
*two
)
310 struct smatch_state
*s
;
311 struct sm_state
*result
;
313 s
= merge_states(one
->name
, one
->owner
, one
->sym
, one
->state
,
314 (two
?two
->state
:NULL
));
315 result
= alloc_state(one
->name
, one
->owner
, one
->sym
, s
);
316 add_possible(result
, one
);
317 add_possible(result
, two
);
318 copy_pools(result
, one
);
319 copy_pools(result
, two
);
322 struct sm_state
*tmp
;
325 printf("%d merge name='%s' owner=%d: %s + %s => %s (",
326 get_lineno(), one
->name
, one
->owner
,
327 show_state(one
->state
), show_state(two
?two
->state
:NULL
),
330 FOR_EACH_PTR(result
->possible
, tmp
) {
334 printf("%s", show_state(tmp
->state
));
335 } END_FOR_EACH_PTR(tmp
);
342 struct sm_state
*get_sm_state_slist(struct state_list
*slist
, const char *name
,
343 int owner
, struct symbol
*sym
)
345 struct sm_state
*state
;
350 FOR_EACH_PTR(slist
, state
) {
351 if (state
->owner
== owner
&& state
->sym
== sym
352 && !strcmp(state
->name
, name
))
354 } END_FOR_EACH_PTR(state
);
358 struct smatch_state
*get_state_slist(struct state_list
*slist
,
359 const char *name
, int owner
,
362 struct sm_state
*state
;
364 state
= get_sm_state_slist(slist
, name
, owner
, sym
);
370 void overwrite_sm_state(struct state_list
**slist
, struct sm_state
*new)
372 struct sm_state
*tmp
;
374 FOR_EACH_PTR(*slist
, tmp
) {
375 if (cmp_tracker(tmp
, new) < 0)
377 else if (cmp_tracker(tmp
, new) == 0) {
378 REPLACE_CURRENT_PTR(tmp
, new);
381 INSERT_CURRENT(new, tmp
);
384 } END_FOR_EACH_PTR(tmp
);
385 add_ptr_list(slist
, new);
388 void overwrite_sm_state_stack(struct state_list_stack
**stack
,
389 struct sm_state
*state
)
391 struct state_list
*slist
;
393 slist
= pop_slist(stack
);
394 overwrite_sm_state(&slist
, state
);
395 push_slist(stack
, slist
);
398 void set_state_slist(struct state_list
**slist
, const char *name
, int owner
,
399 struct symbol
*sym
, struct smatch_state
*state
)
401 struct sm_state
*tmp
;
402 struct sm_state
*new = alloc_state(name
, owner
, sym
, state
);
404 FOR_EACH_PTR(*slist
, tmp
) {
405 if (cmp_tracker(tmp
, new) < 0)
407 else if (cmp_tracker(tmp
, new) == 0) {
408 REPLACE_CURRENT_PTR(tmp
, new);
411 INSERT_CURRENT(new, tmp
);
414 } END_FOR_EACH_PTR(tmp
);
415 add_ptr_list(slist
, new);
418 void delete_state_slist(struct state_list
**slist
, const char *name
, int owner
,
421 struct sm_state
*state
;
423 FOR_EACH_PTR(*slist
, state
) {
424 if (state
->owner
== owner
&& state
->sym
== sym
425 && !strcmp(state
->name
, name
)){
426 delete_ptr_list_entry((struct ptr_list
**)slist
,
430 } END_FOR_EACH_PTR(state
);
434 void push_slist(struct state_list_stack
**list_stack
, struct state_list
*slist
)
436 add_ptr_list(list_stack
, slist
);
439 struct state_list
*pop_slist(struct state_list_stack
**list_stack
)
441 struct state_list
*slist
;
443 slist
= last_ptr_list((struct ptr_list
*)*list_stack
);
444 delete_ptr_list_last((struct ptr_list
**)list_stack
);
448 void free_slist(struct state_list
**slist
)
450 __free_ptr_list((struct ptr_list
**)slist
);
453 void free_stack(struct state_list_stack
**stack
)
455 __free_ptr_list((struct ptr_list
**)stack
);
458 void free_stack_and_slists(struct state_list_stack
**slist_stack
)
460 struct state_list
*slist
;
462 FOR_EACH_PTR(*slist_stack
, slist
) {
464 } END_FOR_EACH_PTR(slist
);
465 free_stack(slist_stack
);
469 * set_state_stack() sets the state for the top slist on the stack.
471 void set_state_stack(struct state_list_stack
**stack
, const char *name
,
472 int owner
, struct symbol
*sym
, struct smatch_state
*state
)
474 struct state_list
*slist
;
476 slist
= pop_slist(stack
);
477 set_state_slist(&slist
, name
, owner
, sym
, state
);
478 push_slist(stack
, slist
);
482 * get_state_stack() gets the state for the top slist on the stack.
484 struct smatch_state
*get_state_stack(struct state_list_stack
*stack
,
485 const char *name
, int owner
,
488 struct state_list
*slist
;
489 struct smatch_state
*ret
;
491 slist
= pop_slist(&stack
);
492 ret
= get_state_slist(slist
, name
, owner
, sym
);
493 push_slist(&stack
, slist
);
498 * We want to find which states have been modified inside a branch.
499 * If you have 2 &merged states they could be different states really
500 * and maybe one or both were modified. We say it is unchanged if
501 * the ->state pointers are the same and they belong to the same pools.
502 * If they have been modified on both sides of a branch to the same thing,
503 * it's still OK to say they are the same, because that means they won't
504 * belong to any pools.
506 static int is_really_same(struct sm_state
*one
, struct sm_state
*two
)
508 struct state_list
*tmp1
;
509 struct state_list
*tmp2
;
511 if (one
->state
!= two
->state
)
514 PREPARE_PTR_LIST(one
->my_pools
, tmp1
);
515 PREPARE_PTR_LIST(two
->my_pools
, tmp2
);
521 } else if (tmp1
== tmp2
) {
528 FINISH_PTR_LIST(tmp2
);
529 FINISH_PTR_LIST(tmp1
);
534 * merge_slist() is called whenever paths merge, such as after
535 * an if statement. It takes the two slists and creates one.
537 void merge_slist(struct state_list
**to
, struct state_list
*slist
)
539 struct sm_state
*to_state
, *state
, *tmp
;
540 struct state_list
*results
= NULL
;
541 struct state_list
*implied_to
= NULL
;
542 struct state_list
*implied_from
= NULL
;
549 /* merging a null and nonnull path gives you only the nonnull path */
554 *to
= clone_slist(slist
);
558 implied_to
= clone_slist(*to
);
559 implied_from
= clone_slist(slist
);
561 PREPARE_PTR_LIST(*to
, to_state
);
562 PREPARE_PTR_LIST(slist
, state
);
564 if (!to_state
&& !state
)
566 if (cmp_tracker(to_state
, state
) < 0) {
567 tmp
= merge_sm_states(to_state
, NULL
);
568 add_pool(&tmp
->my_pools
, implied_to
);
569 add_pool(&tmp
->all_pools
, implied_to
);
570 add_ptr_list(&results
, tmp
);
571 NEXT_PTR_LIST(to_state
);
572 } else if (cmp_tracker(to_state
, state
) == 0) {
573 tmp
= merge_sm_states(to_state
, state
);
574 if (!is_really_same(to_state
, state
)) {
575 add_pool(&tmp
->my_pools
, implied_to
);
576 add_pool(&tmp
->my_pools
, implied_from
);
578 add_pool(&tmp
->all_pools
, implied_to
);
579 add_pool(&tmp
->all_pools
, implied_from
);
580 add_ptr_list(&results
, tmp
);
581 NEXT_PTR_LIST(to_state
);
582 NEXT_PTR_LIST(state
);
584 tmp
= merge_sm_states(state
, NULL
);
585 add_pool(&tmp
->my_pools
, implied_from
);
586 add_pool(&tmp
->all_pools
, implied_from
);
587 add_ptr_list(&results
, tmp
);
588 NEXT_PTR_LIST(state
);
591 FINISH_PTR_LIST(state
);
592 FINISH_PTR_LIST(to_state
);
597 push_slist(&implied_pools
, implied_from
);
598 push_slist(&implied_pools
, implied_to
);
601 static int pool_in_pools(struct state_list_stack
*pools
,
602 struct state_list
*pool
)
604 struct state_list
*tmp
;
606 FOR_EACH_PTR(pools
, tmp
) {
609 } END_FOR_EACH_PTR(tmp
);
613 struct state_list
*clone_states_in_pool(struct state_list
*pool
,
614 struct state_list
*cur_slist
)
616 struct sm_state
*state
;
617 struct sm_state
*cur_state
;
618 struct sm_state
*tmp
;
619 struct state_list
*to_slist
= NULL
;
621 FOR_EACH_PTR(pool
, state
) {
622 cur_state
= get_sm_state_slist(cur_slist
, state
->name
,
623 state
->owner
, state
->sym
);
626 if (is_really_same(state
, cur_state
))
628 if (pool_in_pools(cur_state
->all_pools
, pool
)) {
629 tmp
= clone_state(state
);
630 add_ptr_list(&to_slist
, tmp
);
632 } END_FOR_EACH_PTR(state
);
637 * merge_implied() takes an implied state and another possibly implied state
638 * from another pool. It checks that the second pool is reachable from
639 * cur_slist then merges the two states and returns the result.
641 struct sm_state
*merge_implied(struct sm_state
*one
, struct sm_state
*two
,
642 struct state_list
*pool
,
643 struct state_list
*cur_slist
)
645 struct sm_state
*cur_state
;
647 cur_state
= get_sm_state_slist(cur_slist
, two
->name
, two
->owner
,
650 return NULL
; /* this can't actually happen */
651 if (!pool_in_pools(cur_state
->all_pools
, pool
))
653 return merge_sm_states(one
, two
);
657 * filter() is used to find what states are the same across
658 * a series of slists.
659 * It takes a **slist and a *filter.
660 * It removes everything from **slist that isn't in *filter.
661 * The reason you would want to do this is if you want to
662 * know what other states are true if one state is true. (smatch_implied).
664 void filter(struct state_list
**slist
, struct state_list
*filter
,
665 struct state_list
*cur_slist
)
667 struct sm_state
*s_one
, *s_two
;
668 struct state_list
*results
= NULL
;
669 struct sm_state
*tmp
;
676 PREPARE_PTR_LIST(*slist
, s_one
);
677 PREPARE_PTR_LIST(filter
, s_two
);
679 if (!s_one
|| !s_two
)
681 if (cmp_tracker(s_one
, s_two
) < 0) {
682 NEXT_PTR_LIST(s_one
);
683 } else if (cmp_tracker(s_one
, s_two
) == 0) {
684 tmp
= merge_implied(s_one
, s_two
, filter
, cur_slist
);
686 add_ptr_list(&results
, tmp
);
687 NEXT_PTR_LIST(s_one
);
688 NEXT_PTR_LIST(s_two
);
690 NEXT_PTR_LIST(s_two
);
693 FINISH_PTR_LIST(s_two
);
694 FINISH_PTR_LIST(s_one
);
701 * and_slist_stack() is basically the same as popping the top two slists,
702 * overwriting the one with the other and pushing it back on the stack.
703 * The difference is that it checks to see that a mutually exclusive
704 * state isn't included in both stacks. If smatch sees something like
705 * "if (a && !a)" it prints a warning.
707 void and_slist_stack(struct state_list_stack
**slist_stack
)
709 struct sm_state
*tmp
;
710 struct smatch_state
*tmp_state
;
711 struct state_list
*tmp_slist
= pop_slist(slist_stack
);
713 FOR_EACH_PTR(tmp_slist
, tmp
) {
714 tmp_state
= get_state_stack(*slist_stack
, tmp
->name
,
715 tmp
->owner
, tmp
->sym
);
716 if (tmp_state
&& tmp_state
!= tmp
->state
) {
717 smatch_msg("mutually exclusive 'and' conditions states "
719 tmp
->name
, show_state(tmp_state
),
720 show_state(tmp
->state
));
722 set_state_stack(slist_stack
, tmp
->name
, tmp
->owner
, tmp
->sym
,
724 } END_FOR_EACH_PTR(tmp
);
725 free_slist(&tmp_slist
);
729 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
730 * It pops the two slists from the top of the stack and merges them
731 * together in a way that preserves the things they have in common
732 * but creates a merged state for most of the rest.
733 * You could have code that had: if (foo || foo) { foo->baz;
734 * It's this function which ensures smatch does the right thing.
736 void or_slist_stack(struct state_list_stack
**pre_conds
,
737 struct state_list
*cur_slist
,
738 struct state_list_stack
**slist_stack
)
740 struct state_list
*new;
741 struct state_list
*old
;
742 struct state_list
*res
= NULL
;
743 struct state_list
*tmp_slist
;
744 struct state_list
*tmp_slist2
;
745 struct sm_state
*tmp
;
747 struct sm_state
*new_sm
;
749 new = pop_slist(slist_stack
);
750 old
= pop_slist(slist_stack
);
752 tmp_slist
= pop_slist(pre_conds
);
753 tmp_slist2
= clone_slist(tmp_slist
);
754 push_slist(pre_conds
, tmp_slist
);
755 overwrite_slist(old
, &tmp_slist2
);
756 FOR_EACH_PTR(new, tmp
) {
757 sm
= get_sm_state_slist(tmp_slist2
, tmp
->name
, tmp
->owner
,
759 new_sm
= merge_sm_states(tmp
, sm
);
760 add_ptr_list(&res
, new_sm
);
761 } END_FOR_EACH_PTR(tmp
);
762 free_slist(&tmp_slist2
);
764 tmp_slist2
= clone_slist(cur_slist
);
765 overwrite_slist(new, &tmp_slist2
);
766 FOR_EACH_PTR(old
, tmp
) {
767 sm
= get_sm_state_slist(tmp_slist2
, tmp
->name
, tmp
->owner
,
769 new_sm
= merge_sm_states(tmp
, sm
);
770 add_ptr_list(&res
, new_sm
);
771 } END_FOR_EACH_PTR(tmp
);
772 free_slist(&tmp_slist2
);
774 push_slist(slist_stack
, res
);
781 * get_slist_from_named_stack() is only used for gotos.
783 struct state_list
**get_slist_from_named_stack(struct named_stack
*stack
,
786 struct named_slist
*tmp
;
788 FOR_EACH_PTR(stack
, tmp
) {
789 if (!strcmp(tmp
->name
, name
))
791 } END_FOR_EACH_PTR(tmp
);
795 void overwrite_slist(struct state_list
*from
, struct state_list
**to
)
797 struct sm_state
*tmp
;
799 FOR_EACH_PTR(from
, tmp
) {
800 overwrite_sm_state(to
, tmp
);
801 } END_FOR_EACH_PTR(tmp
);
804 unsigned int __get_allocations()
806 return sm_state_allocator
.allocations
;