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
= (char *)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 /* At the end of every function we free all the sm_states */
145 void free_every_single_sm_state()
147 struct allocator_struct
*desc
= &sm_state_allocator
;
148 struct allocation_blob
*blob
= desc
->blobs
;
151 desc
->allocations
= 0;
152 desc
->total_bytes
= 0;
153 desc
->useful_bytes
= 0;
154 desc
->freelist
= NULL
;
156 struct allocation_blob
*next
= blob
->next
;
157 struct sm_state
*sm
= (struct sm_state
*)blob
->data
;
159 free_slist(&sm
->possible
);
160 free_stack(&sm
->my_pools
);
161 free_stack(&sm
->all_pools
);
162 blob_free(blob
, desc
->chunking
);
167 struct sm_state
*clone_state(struct sm_state
*s
)
169 struct sm_state
*ret
;
170 struct sm_state
*poss
;
172 ret
= alloc_state(s
->name
, s
->owner
, s
->sym
, s
->state
);
173 ret
->my_pools
= clone_stack(s
->my_pools
);
174 ret
->all_pools
= clone_stack(s
->all_pools
);
175 FOR_EACH_PTR(s
->possible
, poss
) {
176 add_sm_state_slist(&ret
->possible
, poss
);
177 } END_FOR_EACH_PTR(poss
);
181 int slist_has_state(struct state_list
*slist
, struct smatch_state
*state
)
183 struct sm_state
*tmp
;
185 FOR_EACH_PTR(slist
, tmp
) {
186 if (tmp
->state
== state
)
188 } END_FOR_EACH_PTR(tmp
);
193 static void check_order(struct state_list
*slist
)
195 struct sm_state
*state
;
196 struct sm_state
*last
= NULL
;
199 FOR_EACH_PTR(slist
, state
) {
200 if (last
&& cmp_tracker(state
, last
) <= 0) {
201 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
202 "%s vs %s\n", last
->owner
, state
->owner
,
203 last
->sym
, state
->sym
, last
->name
, state
->name
);
207 } END_FOR_EACH_PTR(state
);
214 struct state_list
*clone_slist(struct state_list
*from_slist
)
216 struct sm_state
*state
;
217 struct sm_state
*tmp
;
218 struct state_list
*to_slist
= NULL
;
220 FOR_EACH_PTR(from_slist
, state
) {
221 tmp
= clone_state(state
);
222 add_ptr_list(&to_slist
, tmp
);
223 } END_FOR_EACH_PTR(state
);
225 check_order(to_slist
);
230 struct state_list_stack
*clone_stack(struct state_list_stack
*from_stack
)
232 struct state_list
*slist
;
233 struct state_list_stack
*to_stack
= NULL
;
235 FOR_EACH_PTR(from_stack
, slist
) {
236 push_slist(&to_stack
, slist
);
237 } END_FOR_EACH_PTR(slist
);
241 struct smatch_state
*merge_states(const char *name
, int owner
,
243 struct smatch_state
*state1
,
244 struct smatch_state
*state2
)
246 struct smatch_state
*ret
;
248 if (state1
== state2
)
250 else if (__has_merge_function(owner
))
251 ret
= __client_merge_function(owner
, name
, sym
, state1
, state2
);
252 else if (!state1
|| !state2
)
260 * add_pool() adds a slist to ->pools. If the slist has already been
261 * added earlier then it doesn't get added a second time.
263 static void add_pool(struct state_list_stack
**pools
, struct state_list
*new)
265 struct state_list
*tmp
;
267 FOR_EACH_PTR(*pools
, tmp
) {
270 else if (tmp
== new) {
273 INSERT_CURRENT(new, tmp
);
276 } END_FOR_EACH_PTR(tmp
);
277 add_ptr_list(pools
, new);
280 static void copy_pools(struct sm_state
*to
, struct sm_state
*sm
)
282 struct state_list
*tmp
;
287 FOR_EACH_PTR(sm
->my_pools
, tmp
) {
288 add_pool(&to
->my_pools
, tmp
);
289 } END_FOR_EACH_PTR(tmp
);
291 FOR_EACH_PTR(sm
->all_pools
, tmp
) {
292 add_pool(&to
->all_pools
, tmp
);
293 } END_FOR_EACH_PTR(tmp
);
296 struct sm_state
*merge_sm_states(struct sm_state
*one
, struct sm_state
*two
)
298 struct smatch_state
*s
;
299 struct sm_state
*result
;
301 s
= merge_states(one
->name
, one
->owner
, one
->sym
, one
->state
,
302 (two
?two
->state
:NULL
));
303 result
= alloc_state(one
->name
, one
->owner
, one
->sym
, s
);
304 add_possible(result
, one
);
305 add_possible(result
, two
);
306 copy_pools(result
, one
);
307 copy_pools(result
, two
);
310 struct sm_state
*tmp
;
313 printf("%d merge name='%s' owner=%d: %s + %s => %s (",
314 get_lineno(), one
->name
, one
->owner
,
315 show_state(one
->state
), show_state(two
?two
->state
:NULL
),
318 FOR_EACH_PTR(result
->possible
, tmp
) {
322 printf("%s", show_state(tmp
->state
));
323 } END_FOR_EACH_PTR(tmp
);
330 struct sm_state
*get_sm_state_slist(struct state_list
*slist
, const char *name
,
331 int owner
, struct symbol
*sym
)
333 struct sm_state
*state
;
338 FOR_EACH_PTR(slist
, state
) {
339 if (state
->owner
== owner
&& state
->sym
== sym
340 && !strcmp(state
->name
, name
))
342 } END_FOR_EACH_PTR(state
);
346 struct smatch_state
*get_state_slist(struct state_list
*slist
,
347 const char *name
, int owner
,
350 struct sm_state
*state
;
352 state
= get_sm_state_slist(slist
, name
, owner
, sym
);
358 void overwrite_sm_state(struct state_list
**slist
, struct sm_state
*new)
360 struct sm_state
*tmp
;
362 FOR_EACH_PTR(*slist
, tmp
) {
363 if (cmp_tracker(tmp
, new) < 0)
365 else if (cmp_tracker(tmp
, new) == 0) {
366 REPLACE_CURRENT_PTR(tmp
, new);
369 INSERT_CURRENT(new, tmp
);
372 } END_FOR_EACH_PTR(tmp
);
373 add_ptr_list(slist
, new);
376 void overwrite_sm_state_stack(struct state_list_stack
**stack
,
377 struct sm_state
*state
)
379 struct state_list
*slist
;
381 slist
= pop_slist(stack
);
382 overwrite_sm_state(&slist
, state
);
383 push_slist(stack
, slist
);
386 void set_state_slist(struct state_list
**slist
, const char *name
, int owner
,
387 struct symbol
*sym
, struct smatch_state
*state
)
389 struct sm_state
*tmp
;
390 struct sm_state
*new = alloc_state(name
, owner
, sym
, state
);
392 FOR_EACH_PTR(*slist
, tmp
) {
393 if (cmp_tracker(tmp
, new) < 0)
395 else if (cmp_tracker(tmp
, new) == 0) {
396 REPLACE_CURRENT_PTR(tmp
, new);
399 INSERT_CURRENT(new, tmp
);
402 } END_FOR_EACH_PTR(tmp
);
403 add_ptr_list(slist
, new);
406 void delete_state_slist(struct state_list
**slist
, const char *name
, int owner
,
409 struct sm_state
*state
;
411 FOR_EACH_PTR(*slist
, state
) {
412 if (state
->owner
== owner
&& state
->sym
== sym
413 && !strcmp(state
->name
, name
)){
414 delete_ptr_list_entry((struct ptr_list
**)slist
,
418 } END_FOR_EACH_PTR(state
);
422 void push_slist(struct state_list_stack
**list_stack
, struct state_list
*slist
)
424 add_ptr_list(list_stack
, slist
);
427 struct state_list
*pop_slist(struct state_list_stack
**list_stack
)
429 struct state_list
*slist
;
431 slist
= last_ptr_list((struct ptr_list
*)*list_stack
);
432 delete_ptr_list_last((struct ptr_list
**)list_stack
);
436 void free_slist(struct state_list
**slist
)
438 __free_ptr_list((struct ptr_list
**)slist
);
441 void free_stack(struct state_list_stack
**stack
)
443 __free_ptr_list((struct ptr_list
**)stack
);
446 void free_stack_and_slists(struct state_list_stack
**slist_stack
)
448 struct state_list
*slist
;
450 FOR_EACH_PTR(*slist_stack
, slist
) {
452 } END_FOR_EACH_PTR(slist
);
453 free_stack(slist_stack
);
457 * set_state_stack() sets the state for the top slist on the stack.
459 void set_state_stack(struct state_list_stack
**stack
, const char *name
,
460 int owner
, struct symbol
*sym
, struct smatch_state
*state
)
462 struct state_list
*slist
;
464 slist
= pop_slist(stack
);
465 set_state_slist(&slist
, name
, owner
, sym
, state
);
466 push_slist(stack
, slist
);
470 * get_state_stack() gets the state for the top slist on the stack.
472 struct smatch_state
*get_state_stack(struct state_list_stack
*stack
,
473 const char *name
, int owner
,
476 struct state_list
*slist
;
477 struct smatch_state
*ret
;
479 slist
= pop_slist(&stack
);
480 ret
= get_state_slist(slist
, name
, owner
, sym
);
481 push_slist(&stack
, slist
);
486 * We want to find which states have been modified inside a branch.
487 * If you have 2 &merged states they could be different states really
488 * and maybe one or both were modified. We say it is unchanged if
489 * the ->state pointers are the same and they belong to the same pools.
490 * If they have been modified on both sides of a branch to the same thing,
491 * it's still OK to say they are the same, because that means they won't
492 * belong to any pools.
494 static int is_really_same(struct sm_state
*one
, struct sm_state
*two
)
496 struct state_list
*tmp1
;
497 struct state_list
*tmp2
;
499 if (one
->state
!= two
->state
)
502 PREPARE_PTR_LIST(one
->my_pools
, tmp1
);
503 PREPARE_PTR_LIST(two
->my_pools
, tmp2
);
509 } else if (tmp1
== tmp2
) {
516 FINISH_PTR_LIST(tmp2
);
517 FINISH_PTR_LIST(tmp1
);
522 * merge_slist() is called whenever paths merge, such as after
523 * an if statement. It takes the two slists and creates one.
525 void merge_slist(struct state_list
**to
, struct state_list
*slist
)
527 struct sm_state
*to_state
, *state
, *tmp
;
528 struct state_list
*results
= NULL
;
529 struct state_list
*implied_to
= NULL
;
530 struct state_list
*implied_from
= NULL
;
537 /* merging a null and nonnull path gives you only the nonnull path */
542 *to
= clone_slist(slist
);
546 implied_to
= clone_slist(*to
);
547 implied_from
= clone_slist(slist
);
549 PREPARE_PTR_LIST(*to
, to_state
);
550 PREPARE_PTR_LIST(slist
, state
);
552 if (!to_state
&& !state
)
554 if (cmp_tracker(to_state
, state
) < 0) {
555 tmp
= merge_sm_states(to_state
, NULL
);
556 add_pool(&tmp
->my_pools
, implied_to
);
557 add_pool(&tmp
->all_pools
, implied_to
);
558 add_ptr_list(&results
, tmp
);
559 NEXT_PTR_LIST(to_state
);
560 } else if (cmp_tracker(to_state
, state
) == 0) {
561 tmp
= merge_sm_states(to_state
, state
);
562 if (!is_really_same(to_state
, state
)) {
563 add_pool(&tmp
->my_pools
, implied_to
);
564 add_pool(&tmp
->my_pools
, implied_from
);
566 add_pool(&tmp
->all_pools
, implied_to
);
567 add_pool(&tmp
->all_pools
, implied_from
);
568 add_ptr_list(&results
, tmp
);
569 NEXT_PTR_LIST(to_state
);
570 NEXT_PTR_LIST(state
);
572 tmp
= merge_sm_states(state
, NULL
);
573 add_pool(&tmp
->my_pools
, implied_from
);
574 add_pool(&tmp
->all_pools
, implied_from
);
575 add_ptr_list(&results
, tmp
);
576 NEXT_PTR_LIST(state
);
579 FINISH_PTR_LIST(state
);
580 FINISH_PTR_LIST(to_state
);
585 push_slist(&implied_pools
, implied_from
);
586 push_slist(&implied_pools
, implied_to
);
589 static int pool_in_pools(struct state_list_stack
*pools
,
590 struct state_list
*pool
)
592 struct state_list
*tmp
;
594 FOR_EACH_PTR(pools
, tmp
) {
597 } END_FOR_EACH_PTR(tmp
);
601 struct state_list
*clone_states_in_pool(struct state_list
*pool
,
602 struct state_list
*cur_slist
)
604 struct sm_state
*state
;
605 struct sm_state
*cur_state
;
606 struct sm_state
*tmp
;
607 struct state_list
*to_slist
= NULL
;
609 FOR_EACH_PTR(pool
, state
) {
610 cur_state
= get_sm_state_slist(cur_slist
, state
->name
,
611 state
->owner
, state
->sym
);
614 if (is_really_same(state
, cur_state
))
616 if (pool_in_pools(cur_state
->all_pools
, pool
)) {
617 tmp
= clone_state(state
);
618 add_ptr_list(&to_slist
, tmp
);
620 } END_FOR_EACH_PTR(state
);
625 * merge_implied() takes an implied state and another possibly implied state
626 * from another pool. It checks that the second pool is reachable from
627 * cur_slist then merges the two states and returns the result.
629 struct sm_state
*merge_implied(struct sm_state
*one
, struct sm_state
*two
,
630 struct state_list
*pool
,
631 struct state_list
*cur_slist
)
633 struct sm_state
*cur_state
;
635 cur_state
= get_sm_state_slist(cur_slist
, two
->name
, two
->owner
,
638 return NULL
; /* this can't actually happen */
639 if (!pool_in_pools(cur_state
->all_pools
, pool
))
641 return merge_sm_states(one
, two
);
645 * filter() is used to find what states are the same across
646 * a series of slists.
647 * It takes a **slist and a *filter.
648 * It removes everything from **slist that isn't in *filter.
649 * The reason you would want to do this is if you want to
650 * know what other states are true if one state is true. (smatch_implied).
652 void filter(struct state_list
**slist
, struct state_list
*filter
,
653 struct state_list
*cur_slist
)
655 struct sm_state
*s_one
, *s_two
;
656 struct state_list
*results
= NULL
;
657 struct sm_state
*tmp
;
664 PREPARE_PTR_LIST(*slist
, s_one
);
665 PREPARE_PTR_LIST(filter
, s_two
);
667 if (!s_one
|| !s_two
)
669 if (cmp_tracker(s_one
, s_two
) < 0) {
670 NEXT_PTR_LIST(s_one
);
671 } else if (cmp_tracker(s_one
, s_two
) == 0) {
672 tmp
= merge_implied(s_one
, s_two
, filter
, cur_slist
);
674 add_ptr_list(&results
, tmp
);
675 NEXT_PTR_LIST(s_one
);
676 NEXT_PTR_LIST(s_two
);
678 NEXT_PTR_LIST(s_two
);
681 FINISH_PTR_LIST(s_two
);
682 FINISH_PTR_LIST(s_one
);
689 * and_slist_stack() is basically the same as popping the top two slists,
690 * overwriting the one with the other and pushing it back on the stack.
691 * The difference is that it checks to see that a mutually exclusive
692 * state isn't included in both stacks. If smatch sees something like
693 * "if (a && !a)" it prints a warning.
695 void and_slist_stack(struct state_list_stack
**slist_stack
)
697 struct sm_state
*tmp
;
698 struct smatch_state
*tmp_state
;
699 struct state_list
*tmp_slist
= pop_slist(slist_stack
);
701 FOR_EACH_PTR(tmp_slist
, tmp
) {
702 tmp_state
= get_state_stack(*slist_stack
, tmp
->name
,
703 tmp
->owner
, tmp
->sym
);
704 if (tmp_state
&& tmp_state
!= tmp
->state
) {
705 smatch_msg("mutually exclusive 'and' conditions states "
707 tmp
->name
, show_state(tmp_state
),
708 show_state(tmp
->state
));
710 set_state_stack(slist_stack
, tmp
->name
, tmp
->owner
, tmp
->sym
,
712 } END_FOR_EACH_PTR(tmp
);
713 free_slist(&tmp_slist
);
717 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
718 * It pops the two slists from the top of the stack and merges them
719 * together in a way that preserves the things they have in common
720 * but creates a merged state for most of the rest.
721 * You could have code that had: if (foo || foo) { foo->baz;
722 * It's this function which ensures smatch does the right thing.
724 void or_slist_stack(struct state_list_stack
**slist_stack
)
726 struct state_list
*one
;
727 struct state_list
*two
;
728 struct state_list
*res
= NULL
;
729 struct sm_state
*tmp
;
731 struct sm_state
*new_sm
;
733 one
= pop_slist(slist_stack
);
734 two
= pop_slist(slist_stack
);
736 FOR_EACH_PTR(one
, tmp
) {
737 sm
= get_sm_state_slist(two
, tmp
->name
, tmp
->owner
, tmp
->sym
);
738 new_sm
= merge_sm_states(tmp
, sm
);
739 add_ptr_list(&res
, new_sm
);
740 } END_FOR_EACH_PTR(tmp
);
742 FOR_EACH_PTR(two
, tmp
) {
743 sm
= get_sm_state_slist(one
, tmp
->name
, tmp
->owner
, tmp
->sym
);
744 new_sm
= merge_sm_states(tmp
, sm
);
745 add_ptr_list(&res
, new_sm
);
746 } END_FOR_EACH_PTR(tmp
);
748 push_slist(slist_stack
, res
);
755 * get_slist_from_named_stack() is only used for gotos.
757 struct state_list
**get_slist_from_named_stack(struct named_stack
*stack
,
760 struct named_slist
*tmp
;
762 FOR_EACH_PTR(stack
, tmp
) {
763 if (!strcmp(tmp
->name
, name
))
765 } END_FOR_EACH_PTR(tmp
);
769 void overwrite_slist(struct state_list
*from
, struct state_list
**to
)
771 struct sm_state
*tmp
;
773 FOR_EACH_PTR(from
, tmp
) {
774 overwrite_sm_state(to
, tmp
);
775 } END_FOR_EACH_PTR(tmp
);