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
*tmp
;
171 struct sm_state
*poss
;
173 ret
= alloc_state(s
->name
, s
->owner
, s
->sym
, s
->state
);
174 ret
->my_pools
= clone_stack(s
->my_pools
);
175 ret
->all_pools
= clone_stack(s
->all_pools
);
176 FOR_EACH_PTR(s
->possible
, poss
) {
177 tmp
= alloc_state(s
->name
, s
->owner
, s
->sym
, poss
->state
);
178 add_sm_state_slist(&ret
->possible
, tmp
);
179 } END_FOR_EACH_PTR(poss
);
183 int slist_has_state(struct state_list
*slist
, struct smatch_state
*state
)
185 struct sm_state
*tmp
;
187 FOR_EACH_PTR(slist
, tmp
) {
188 if (tmp
->state
== state
)
190 } END_FOR_EACH_PTR(tmp
);
195 static void check_order(struct state_list
*slist
)
197 struct sm_state
*state
;
198 struct sm_state
*last
= NULL
;
201 FOR_EACH_PTR(slist
, state
) {
202 if (last
&& cmp_tracker(state
, last
) <= 0) {
203 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
204 "%s vs %s\n", last
->owner
, state
->owner
,
205 last
->sym
, state
->sym
, last
->name
, state
->name
);
209 } END_FOR_EACH_PTR(state
);
216 struct state_list
*clone_slist(struct state_list
*from_slist
)
218 struct sm_state
*state
;
219 struct sm_state
*tmp
;
220 struct state_list
*to_slist
= NULL
;
222 FOR_EACH_PTR(from_slist
, state
) {
223 tmp
= clone_state(state
);
224 add_ptr_list(&to_slist
, tmp
);
225 } END_FOR_EACH_PTR(state
);
227 check_order(to_slist
);
232 struct state_list_stack
*clone_stack(struct state_list_stack
*from_stack
)
234 struct state_list
*slist
;
235 struct state_list_stack
*to_stack
= NULL
;
237 FOR_EACH_PTR(from_stack
, slist
) {
238 push_slist(&to_stack
, slist
);
239 } END_FOR_EACH_PTR(slist
);
243 struct smatch_state
*merge_states(const char *name
, int owner
,
245 struct smatch_state
*state1
,
246 struct smatch_state
*state2
)
248 struct smatch_state
*ret
;
250 if (state1
== state2
)
252 else if (__has_merge_function(owner
))
253 ret
= __client_merge_function(owner
, name
, sym
, state1
, state2
);
254 else if (!state1
|| !state2
)
262 * add_pool() adds a slist to ->pools. If the slist has already been
263 * added earlier then it doesn't get added a second time.
265 static void add_pool(struct state_list_stack
**pools
, struct state_list
*new)
267 struct state_list
*tmp
;
269 FOR_EACH_PTR(*pools
, tmp
) {
272 else if (tmp
== new) {
275 INSERT_CURRENT(new, tmp
);
278 } END_FOR_EACH_PTR(tmp
);
279 add_ptr_list(pools
, new);
282 static void copy_pools(struct sm_state
*to
, struct sm_state
*sm
)
284 struct state_list
*tmp
;
289 FOR_EACH_PTR(sm
->my_pools
, tmp
) {
290 add_pool(&to
->my_pools
, tmp
);
291 } END_FOR_EACH_PTR(tmp
);
293 FOR_EACH_PTR(sm
->all_pools
, tmp
) {
294 add_pool(&to
->all_pools
, tmp
);
295 } END_FOR_EACH_PTR(tmp
);
298 struct sm_state
*merge_sm_states(struct sm_state
*one
, struct sm_state
*two
)
300 struct smatch_state
*s
;
301 struct sm_state
*result
;
303 s
= merge_states(one
->name
, one
->owner
, one
->sym
, one
->state
,
304 (two
?two
->state
:NULL
));
305 result
= alloc_state(one
->name
, one
->owner
, one
->sym
, s
);
306 add_possible(result
, one
);
307 add_possible(result
, two
);
308 copy_pools(result
, one
);
309 copy_pools(result
, two
);
312 struct sm_state
*tmp
;
315 printf("%d merge name='%s' owner=%d: %s + %s => %s (",
316 get_lineno(), one
->name
, one
->owner
,
317 show_state(one
->state
), show_state(two
?two
->state
:NULL
),
320 FOR_EACH_PTR(result
->possible
, tmp
) {
324 printf("%s", show_state(tmp
->state
));
325 } END_FOR_EACH_PTR(tmp
);
332 struct sm_state
*get_sm_state_slist(struct state_list
*slist
, const char *name
,
333 int owner
, struct symbol
*sym
)
335 struct sm_state
*state
;
340 FOR_EACH_PTR(slist
, state
) {
341 if (state
->owner
== owner
&& state
->sym
== sym
342 && !strcmp(state
->name
, name
))
344 } END_FOR_EACH_PTR(state
);
348 struct smatch_state
*get_state_slist(struct state_list
*slist
,
349 const char *name
, int owner
,
352 struct sm_state
*state
;
354 state
= get_sm_state_slist(slist
, name
, owner
, sym
);
360 void overwrite_sm_state(struct state_list
**slist
, struct sm_state
*state
)
362 struct sm_state
*tmp
;
363 struct sm_state
*new = clone_state(state
); //fixme. why?
365 FOR_EACH_PTR(*slist
, tmp
) {
366 if (cmp_tracker(tmp
, new) < 0)
368 else if (cmp_tracker(tmp
, new) == 0) {
369 REPLACE_CURRENT_PTR(tmp
, new);
372 INSERT_CURRENT(new, tmp
);
375 } END_FOR_EACH_PTR(tmp
);
376 add_ptr_list(slist
, new);
379 void overwrite_sm_state_stack(struct state_list_stack
**stack
,
380 struct sm_state
*state
)
382 struct state_list
*slist
;
384 slist
= pop_slist(stack
);
385 overwrite_sm_state(&slist
, state
);
386 push_slist(stack
, slist
);
389 void set_state_slist(struct state_list
**slist
, const char *name
, int owner
,
390 struct symbol
*sym
, struct smatch_state
*state
)
392 struct sm_state
*tmp
;
393 struct sm_state
*new = alloc_state(name
, owner
, sym
, state
);
395 FOR_EACH_PTR(*slist
, tmp
) {
396 if (cmp_tracker(tmp
, new) < 0)
398 else if (cmp_tracker(tmp
, new) == 0) {
400 tmp
->my_pools
= NULL
;
401 tmp
->all_pools
= NULL
;
402 tmp
->possible
= NULL
;
403 add_ptr_list(&tmp
->possible
, tmp
);
404 __free_sm_state(new);
407 INSERT_CURRENT(new, tmp
);
410 } END_FOR_EACH_PTR(tmp
);
411 add_ptr_list(slist
, new);
414 void delete_state_slist(struct state_list
**slist
, const char *name
, int owner
,
417 struct sm_state
*state
;
419 FOR_EACH_PTR(*slist
, state
) {
420 if (state
->owner
== owner
&& state
->sym
== sym
421 && !strcmp(state
->name
, name
)){
422 delete_ptr_list_entry((struct ptr_list
**)slist
,
424 __free_sm_state(state
);
427 } END_FOR_EACH_PTR(state
);
431 void push_slist(struct state_list_stack
**list_stack
, struct state_list
*slist
)
433 add_ptr_list(list_stack
, slist
);
436 struct state_list
*pop_slist(struct state_list_stack
**list_stack
)
438 struct state_list
*slist
;
440 slist
= last_ptr_list((struct ptr_list
*)*list_stack
);
441 delete_ptr_list_last((struct ptr_list
**)list_stack
);
445 void free_slist(struct state_list
**slist
)
447 __free_ptr_list((struct ptr_list
**)slist
);
450 void free_stack(struct state_list_stack
**stack
)
452 __free_ptr_list((struct ptr_list
**)stack
);
455 void free_stack_and_slists(struct state_list_stack
**slist_stack
)
457 struct state_list
*slist
;
459 FOR_EACH_PTR(*slist_stack
, slist
) {
461 } END_FOR_EACH_PTR(slist
);
462 free_stack(slist_stack
);
466 * set_state_stack() sets the state for the top slist on the stack.
468 void set_state_stack(struct state_list_stack
**stack
, const char *name
,
469 int owner
, struct symbol
*sym
, struct smatch_state
*state
)
471 struct state_list
*slist
;
473 slist
= pop_slist(stack
);
474 set_state_slist(&slist
, name
, owner
, sym
, state
);
475 push_slist(stack
, slist
);
479 * get_state_stack() gets the state for the top slist on the stack.
481 struct smatch_state
*get_state_stack(struct state_list_stack
*stack
,
482 const char *name
, int owner
,
485 struct state_list
*slist
;
486 struct smatch_state
*ret
;
488 slist
= pop_slist(&stack
);
489 ret
= get_state_slist(slist
, name
, owner
, sym
);
490 push_slist(&stack
, slist
);
495 * We want to find which states have been modified inside a branch.
496 * If you have 2 &merged states they could be different states really
497 * and maybe one or both were modified. We say it is unchanged if
498 * the ->state pointers are the same and they belong to the same pools.
499 * If they have been modified on both sides of a branch to the same thing,
500 * it's still OK to say they are the same, because that means they won't
501 * belong to any pools.
503 static int is_really_same(struct sm_state
*one
, struct sm_state
*two
)
505 struct state_list
*tmp1
;
506 struct state_list
*tmp2
;
508 if (one
->state
!= two
->state
)
511 PREPARE_PTR_LIST(one
->my_pools
, tmp1
);
512 PREPARE_PTR_LIST(two
->my_pools
, tmp2
);
518 } else if (tmp1
== tmp2
) {
525 FINISH_PTR_LIST(tmp2
);
526 FINISH_PTR_LIST(tmp1
);
531 * merge_slist() is called whenever paths merge, such as after
532 * an if statement. It takes the two slists and creates one.
534 void merge_slist(struct state_list
**to
, struct state_list
*slist
)
536 struct sm_state
*to_state
, *state
, *tmp
;
537 struct state_list
*results
= NULL
;
538 struct state_list
*implied_to
= NULL
;
539 struct state_list
*implied_from
= NULL
;
546 /* merging a null and nonnull path gives you only the nonnull path */
551 *to
= clone_slist(slist
);
555 implied_to
= clone_slist(*to
);
556 implied_from
= clone_slist(slist
);
558 PREPARE_PTR_LIST(*to
, to_state
);
559 PREPARE_PTR_LIST(slist
, state
);
561 if (!to_state
&& !state
)
563 if (cmp_tracker(to_state
, state
) < 0) {
564 tmp
= merge_sm_states(to_state
, NULL
);
565 add_pool(&tmp
->my_pools
, implied_to
);
566 add_pool(&tmp
->all_pools
, implied_to
);
567 add_ptr_list(&results
, tmp
);
568 NEXT_PTR_LIST(to_state
);
569 } else if (cmp_tracker(to_state
, state
) == 0) {
570 tmp
= merge_sm_states(to_state
, state
);
571 if (!is_really_same(to_state
, state
)) {
572 add_pool(&tmp
->my_pools
, implied_to
);
573 add_pool(&tmp
->my_pools
, implied_from
);
575 add_pool(&tmp
->all_pools
, implied_to
);
576 add_pool(&tmp
->all_pools
, implied_from
);
577 add_ptr_list(&results
, tmp
);
578 NEXT_PTR_LIST(to_state
);
579 NEXT_PTR_LIST(state
);
581 tmp
= merge_sm_states(state
, NULL
);
582 add_pool(&tmp
->my_pools
, implied_from
);
583 add_pool(&tmp
->all_pools
, implied_from
);
584 add_ptr_list(&results
, tmp
);
585 NEXT_PTR_LIST(state
);
588 FINISH_PTR_LIST(state
);
589 FINISH_PTR_LIST(to_state
);
594 push_slist(&implied_pools
, implied_from
);
595 push_slist(&implied_pools
, implied_to
);
598 static int pool_in_pools(struct state_list_stack
*pools
,
599 struct state_list
*pool
)
601 struct state_list
*tmp
;
603 FOR_EACH_PTR(pools
, tmp
) {
606 } END_FOR_EACH_PTR(tmp
);
610 struct state_list
*clone_states_in_pool(struct state_list
*pool
,
611 struct state_list
*cur_slist
)
613 struct sm_state
*state
;
614 struct sm_state
*cur_state
;
615 struct sm_state
*tmp
;
616 struct state_list
*to_slist
= NULL
;
618 FOR_EACH_PTR(pool
, state
) {
619 cur_state
= get_sm_state_slist(cur_slist
, state
->name
,
620 state
->owner
, state
->sym
);
623 if (is_really_same(state
, cur_state
))
625 if (pool_in_pools(cur_state
->all_pools
, pool
)) {
626 tmp
= clone_state(state
);
627 add_ptr_list(&to_slist
, tmp
);
629 } END_FOR_EACH_PTR(state
);
634 * merge_implied() takes an implied state and another possibly implied state
635 * from another pool. It checks that the second pool is reachable from
636 * cur_slist then merges the two states and returns the result.
638 struct sm_state
*merge_implied(struct sm_state
*one
, struct sm_state
*two
,
639 struct state_list
*pool
,
640 struct state_list
*cur_slist
)
642 struct sm_state
*cur_state
;
644 // fixme: do we not need to check this?
645 cur_state
= get_sm_state_slist(cur_slist
, two
->name
, two
->owner
,
648 return NULL
; /* this can't actually happen */
649 if (!pool_in_pools(cur_state
->all_pools
, pool
))
651 return merge_sm_states(one
, two
);
655 * filter() is used to find what states are the same across
656 * a series of slists.
657 * It takes a **slist and a *filter.
658 * It removes everything from **slist that isn't in *filter.
659 * The reason you would want to do this is if you want to
660 * know what other states are true if one state is true. (smatch_implied).
662 void filter(struct state_list
**slist
, struct state_list
*filter
,
663 struct state_list
*cur_slist
)
665 struct sm_state
*s_one
, *s_two
;
666 struct state_list
*results
= NULL
;
667 struct sm_state
*tmp
;
674 PREPARE_PTR_LIST(*slist
, s_one
);
675 PREPARE_PTR_LIST(filter
, s_two
);
677 if (!s_one
|| !s_two
)
679 if (cmp_tracker(s_one
, s_two
) < 0) {
680 NEXT_PTR_LIST(s_one
);
681 } else if (cmp_tracker(s_one
, s_two
) == 0) {
682 tmp
= merge_implied(s_one
, s_two
, filter
, cur_slist
);
684 add_ptr_list(&results
, tmp
);
685 NEXT_PTR_LIST(s_one
);
686 NEXT_PTR_LIST(s_two
);
688 NEXT_PTR_LIST(s_two
);
691 FINISH_PTR_LIST(s_two
);
692 FINISH_PTR_LIST(s_one
);
699 * and_slist_stack() is basically the same as popping the top two slists,
700 * overwriting the one with the other and pushing it back on the stack.
701 * The difference is that it checks to see that a mutually exclusive
702 * state isn't included in both stacks. If smatch sees something like
703 * "if (a && !a)" it prints a warning.
705 void and_slist_stack(struct state_list_stack
**slist_stack
)
707 struct sm_state
*tmp
;
708 struct smatch_state
*tmp_state
;
709 struct state_list
*tmp_slist
= pop_slist(slist_stack
);
711 FOR_EACH_PTR(tmp_slist
, tmp
) {
712 tmp_state
= get_state_stack(*slist_stack
, tmp
->name
,
713 tmp
->owner
, tmp
->sym
);
714 if (tmp_state
&& tmp_state
!= tmp
->state
) {
715 smatch_msg("mutually exclusive 'and' conditions states "
717 tmp
->name
, show_state(tmp_state
),
718 show_state(tmp
->state
));
720 set_state_stack(slist_stack
, tmp
->name
, tmp
->owner
, tmp
->sym
,
722 } END_FOR_EACH_PTR(tmp
);
723 free_slist(&tmp_slist
);
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_slist_stack(struct state_list_stack
**slist_stack
)
736 struct state_list
*one
;
737 struct state_list
*two
;
738 struct state_list
*res
= NULL
;
739 struct sm_state
*tmp
;
741 struct sm_state
*new_sm
;
743 one
= pop_slist(slist_stack
);
744 two
= pop_slist(slist_stack
);
746 FOR_EACH_PTR(one
, tmp
) {
747 sm
= get_sm_state_slist(two
, tmp
->name
, tmp
->owner
, tmp
->sym
);
748 new_sm
= merge_sm_states(tmp
, sm
);
749 add_ptr_list(&res
, new_sm
);
750 } END_FOR_EACH_PTR(tmp
);
752 FOR_EACH_PTR(two
, tmp
) {
753 sm
= get_sm_state_slist(one
, tmp
->name
, tmp
->owner
, tmp
->sym
);
754 new_sm
= merge_sm_states(tmp
, sm
);
755 add_ptr_list(&res
, new_sm
);
756 } END_FOR_EACH_PTR(tmp
);
758 push_slist(slist_stack
, res
);
765 * get_slist_from_named_stack() is only used for gotos.
767 struct state_list
**get_slist_from_named_stack(struct named_stack
*stack
,
770 struct named_slist
*tmp
;
772 FOR_EACH_PTR(stack
, tmp
) {
773 if (!strcmp(tmp
->name
, name
))
775 } END_FOR_EACH_PTR(tmp
);
779 void overwrite_slist(struct state_list
*from
, struct state_list
**to
)
781 struct sm_state
*tmp
;
783 FOR_EACH_PTR(from
, tmp
) {
784 overwrite_sm_state(to
, tmp
);
785 } END_FOR_EACH_PTR(tmp
);