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"
14 #include "smatch_extra.h"
19 ALLOCATOR(smatch_state
, "smatch state");
20 ALLOCATOR(sm_state
, "sm state");
21 ALLOCATOR(named_slist
, "named slist");
22 __DO_ALLOCATOR(char, 0, 1, "state names", sname
);
24 void __print_slist(struct state_list
*slist
)
26 struct sm_state
*state
;
27 struct sm_state
*poss
;
30 printf("dumping slist at %d\n", get_lineno());
31 FOR_EACH_PTR(slist
, state
) {
32 printf("%d '%s'=%s (", state
->owner
, state
->name
,
33 show_state(state
->state
));
35 FOR_EACH_PTR(state
->possible
, poss
) {
38 printf("%s", show_state(poss
->state
));
39 } END_FOR_EACH_PTR(poss
);
41 } END_FOR_EACH_PTR(state
);
46 /* NULL states go at the end to simplify merge_slist */
47 int cmp_tracker(const struct sm_state
*a
, const struct sm_state
*b
)
58 if (a
->owner
> b
->owner
)
60 if (a
->owner
< b
->owner
)
63 ret
= strcmp(a
->name
, b
->name
);
67 if (!b
->sym
&& a
->sym
)
69 if (!a
->sym
&& b
->sym
)
79 static int cmp_sm_states(const struct sm_state
*a
, const struct sm_state
*b
)
83 ret
= cmp_tracker(a
, b
);
87 /* todo: add hook for smatch_extra.c */
88 if (a
->state
> b
->state
)
90 if (a
->state
< b
->state
)
95 static struct sm_state
*alloc_state_no_name(const char *name
, int owner
,
97 struct smatch_state
*state
)
101 tmp
= alloc_state(NULL
, owner
, sym
, state
);
106 void add_sm_state_slist(struct state_list
**slist
, struct sm_state
*new)
108 struct sm_state
*tmp
;
110 FOR_EACH_PTR(*slist
, tmp
) {
111 if (cmp_sm_states(tmp
, new) < 0)
113 else if (cmp_sm_states(tmp
, new) == 0) {
116 INSERT_CURRENT(new, tmp
);
119 } END_FOR_EACH_PTR(tmp
);
120 add_ptr_list(slist
, new);
123 static void add_possible(struct sm_state
*sm
, struct sm_state
*new)
125 struct sm_state
*tmp
;
126 struct sm_state
*tmp2
;
129 struct smatch_state
*s
;
131 s
= merge_states(sm
->name
, sm
->owner
, sm
->sym
, sm
->state
, NULL
);
132 tmp
= alloc_state_no_name(sm
->name
, sm
->owner
, sm
->sym
, s
);
133 add_sm_state_slist(&sm
->possible
, tmp
);
137 FOR_EACH_PTR(new->possible
, tmp
) {
138 tmp2
= alloc_state_no_name(tmp
->name
, tmp
->owner
, tmp
->sym
,
140 add_sm_state_slist(&sm
->possible
, tmp2
);
141 } END_FOR_EACH_PTR(tmp
);
144 char *alloc_sname(const char *str
)
150 tmp
= __alloc_sname(strlen(str
) + 1);
155 struct sm_state
*alloc_state(const char *name
, int owner
,
156 struct symbol
*sym
, struct smatch_state
*state
)
158 struct sm_state
*sm_state
= __alloc_sm_state(0);
160 sm_state
->name
= alloc_sname(name
);
161 sm_state
->owner
= owner
;
163 sm_state
->state
= state
;
164 sm_state
->line
= get_lineno();
165 sm_state
->my_pools
= NULL
;
166 sm_state
->all_pools
= NULL
;
167 sm_state
->possible
= NULL
;
168 add_ptr_list(&sm_state
->possible
, sm_state
);
172 static void free_sm_state(struct sm_state
*sm
)
174 free_slist(&sm
->possible
);
175 free_stack(&sm
->my_pools
);
176 free_stack(&sm
->all_pools
);
178 * fixme. Free the actual state.
179 * Right now we leave it until the end of the function
180 * because we don't want to double free it.
181 * Use the freelist to not double free things
185 static void free_all_sm_states(struct allocation_blob
*blob
)
187 unsigned int size
= sizeof(struct sm_state
);
188 unsigned int offset
= 0;
190 while (offset
< blob
->offset
) {
191 free_sm_state((struct sm_state
*)(blob
->data
+ offset
));
196 /* At the end of every function we free all the sm_states */
197 void free_every_single_sm_state(void)
199 struct allocator_struct
*desc
= &sm_state_allocator
;
200 struct allocation_blob
*blob
= desc
->blobs
;
203 desc
->allocations
= 0;
204 desc
->total_bytes
= 0;
205 desc
->useful_bytes
= 0;
206 desc
->freelist
= NULL
;
208 struct allocation_blob
*next
= blob
->next
;
209 free_all_sm_states(blob
);
210 blob_free(blob
, desc
->chunking
);
216 struct sm_state
*clone_state(struct sm_state
*s
)
218 struct sm_state
*ret
;
219 struct sm_state
*poss
;
221 ret
= alloc_state_no_name(s
->name
, s
->owner
, s
->sym
, s
->state
);
223 ret
->my_pools
= clone_stack(s
->my_pools
);
224 ret
->all_pools
= clone_stack(s
->all_pools
);
225 FOR_EACH_PTR(s
->possible
, poss
) {
226 add_sm_state_slist(&ret
->possible
, poss
);
227 } END_FOR_EACH_PTR(poss
);
231 int slist_has_state(struct state_list
*slist
, struct smatch_state
*state
)
233 struct sm_state
*tmp
;
235 FOR_EACH_PTR(slist
, tmp
) {
236 if (tmp
->state
== state
)
238 } END_FOR_EACH_PTR(tmp
);
242 static void check_order(struct state_list
*slist
)
245 struct sm_state
*state
;
246 struct sm_state
*last
= NULL
;
249 FOR_EACH_PTR(slist
, state
) {
250 if (last
&& cmp_tracker(state
, last
) <= 0) {
251 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
252 "%s vs %s\n", last
->owner
, state
->owner
,
253 last
->sym
, state
->sym
, last
->name
, state
->name
);
257 } END_FOR_EACH_PTR(state
);
264 static void check_my_pools(struct sm_state
*sm
)
266 struct sm_state
*poss
;
267 struct state_list
*slist
;
269 if (sm
->state
!= &merged
)
272 FOR_EACH_PTR(sm
->possible
, poss
) {
273 if (poss
->state
== &merged
)
275 FOR_EACH_PTR(sm
->my_pools
, slist
) {
276 if (get_state_slist(slist
, sm
->name
, sm
->owner
, sm
->sym
)
279 } END_FOR_EACH_PTR(slist
);
280 printf("%d pool not found for '%s' possible state \"%s\".\n",
281 get_lineno(), sm
->name
, show_state(poss
->state
));
285 } END_FOR_EACH_PTR(poss
);
289 static void sanity_check_pools(struct state_list
*slist
)
292 struct sm_state
*tmp
;
294 FOR_EACH_PTR(slist
, tmp
) {
296 } END_FOR_EACH_PTR(tmp
);
300 struct state_list
*clone_slist(struct state_list
*from_slist
)
302 struct sm_state
*state
;
303 struct state_list
*to_slist
= NULL
;
305 FOR_EACH_PTR(from_slist
, state
) {
306 add_ptr_list(&to_slist
, state
);
307 } END_FOR_EACH_PTR(state
);
308 check_order(to_slist
);
312 struct state_list_stack
*clone_stack(struct state_list_stack
*from_stack
)
314 struct state_list
*slist
;
315 struct state_list_stack
*to_stack
= NULL
;
317 FOR_EACH_PTR(from_stack
, slist
) {
318 push_slist(&to_stack
, slist
);
319 } END_FOR_EACH_PTR(slist
);
323 struct smatch_state
*merge_states(const char *name
, int owner
,
325 struct smatch_state
*state1
,
326 struct smatch_state
*state2
)
328 struct smatch_state
*ret
;
330 if (state1
== state2
)
332 else if (__has_merge_function(owner
))
333 ret
= __client_merge_function(owner
, name
, sym
, state1
, state2
);
334 else if (!state1
|| !state2
)
342 * add_pool() adds a slist to ->pools. If the slist has already been
343 * added earlier then it doesn't get added a second time.
345 void add_pool(struct state_list_stack
**pools
, struct state_list
*new)
347 struct state_list
*tmp
;
349 FOR_EACH_PTR(*pools
, tmp
) {
352 else if (tmp
== new) {
355 INSERT_CURRENT(new, tmp
);
358 } END_FOR_EACH_PTR(tmp
);
359 add_ptr_list(pools
, new);
362 static void copy_pools(struct sm_state
*to
, struct sm_state
*sm
)
364 struct state_list
*tmp
;
369 FOR_EACH_PTR(sm
->my_pools
, tmp
) {
370 add_pool(&to
->my_pools
, tmp
);
371 } END_FOR_EACH_PTR(tmp
);
373 FOR_EACH_PTR(sm
->all_pools
, tmp
) {
374 add_pool(&to
->all_pools
, tmp
);
375 } END_FOR_EACH_PTR(tmp
);
378 struct sm_state
*merge_sm_states(struct sm_state
*one
, struct sm_state
*two
)
380 struct smatch_state
*s
;
381 struct sm_state
*result
;
385 s
= merge_states(one
->name
, one
->owner
, one
->sym
, one
->state
,
386 (two
?two
->state
:NULL
));
387 result
= alloc_state_no_name(one
->name
, one
->owner
, one
->sym
, s
);
388 if (two
&& one
->line
== two
->line
)
389 result
->line
= one
->line
;
390 add_possible(result
, one
);
391 add_possible(result
, two
);
392 copy_pools(result
, one
);
393 copy_pools(result
, two
);
396 struct sm_state
*tmp
;
399 printf("%d merge name='%s' owner=%d: %s + %s => %s (",
400 get_lineno(), one
->name
, one
->owner
,
401 show_state(one
->state
), show_state(two
?two
->state
:NULL
),
404 FOR_EACH_PTR(result
->possible
, tmp
) {
408 printf("%s", show_state(tmp
->state
));
409 } END_FOR_EACH_PTR(tmp
);
416 struct sm_state
*get_sm_state_slist(struct state_list
*slist
, const char *name
,
417 int owner
, struct symbol
*sym
)
419 struct sm_state
*state
;
424 FOR_EACH_PTR(slist
, state
) {
425 if (state
->owner
== owner
&& state
->sym
== sym
426 && !strcmp(state
->name
, name
))
428 } END_FOR_EACH_PTR(state
);
432 struct smatch_state
*get_state_slist(struct state_list
*slist
,
433 const char *name
, int owner
,
436 struct sm_state
*state
;
438 state
= get_sm_state_slist(slist
, name
, owner
, sym
);
444 void overwrite_sm_state(struct state_list
**slist
, struct sm_state
*new)
446 struct sm_state
*tmp
;
448 FOR_EACH_PTR(*slist
, tmp
) {
449 if (cmp_tracker(tmp
, new) < 0)
451 else if (cmp_tracker(tmp
, new) == 0) {
452 REPLACE_CURRENT_PTR(tmp
, new);
455 INSERT_CURRENT(new, tmp
);
458 } END_FOR_EACH_PTR(tmp
);
459 add_ptr_list(slist
, new);
462 void overwrite_sm_state_stack(struct state_list_stack
**stack
,
463 struct sm_state
*state
)
465 struct state_list
*slist
;
467 slist
= pop_slist(stack
);
468 overwrite_sm_state(&slist
, state
);
469 push_slist(stack
, slist
);
472 void set_state_slist(struct state_list
**slist
, const char *name
, int owner
,
473 struct symbol
*sym
, struct smatch_state
*state
)
475 struct sm_state
*tmp
;
476 struct sm_state
*new = alloc_state(name
, owner
, sym
, state
);
478 FOR_EACH_PTR(*slist
, tmp
) {
479 if (cmp_tracker(tmp
, new) < 0)
481 else if (cmp_tracker(tmp
, new) == 0) {
482 REPLACE_CURRENT_PTR(tmp
, new);
485 INSERT_CURRENT(new, tmp
);
488 } END_FOR_EACH_PTR(tmp
);
489 add_ptr_list(slist
, new);
492 void delete_state_slist(struct state_list
**slist
, const char *name
, int owner
,
495 struct sm_state
*state
;
497 FOR_EACH_PTR(*slist
, state
) {
498 if (state
->owner
== owner
&& state
->sym
== sym
499 && !strcmp(state
->name
, name
)){
500 delete_ptr_list_entry((struct ptr_list
**)slist
,
504 } END_FOR_EACH_PTR(state
);
508 void push_slist(struct state_list_stack
**list_stack
, struct state_list
*slist
)
510 add_ptr_list(list_stack
, slist
);
513 struct state_list
*pop_slist(struct state_list_stack
**list_stack
)
515 struct state_list
*slist
;
517 slist
= last_ptr_list((struct ptr_list
*)*list_stack
);
518 delete_ptr_list_last((struct ptr_list
**)list_stack
);
522 void free_slist(struct state_list
**slist
)
524 __free_ptr_list((struct ptr_list
**)slist
);
527 void free_stack(struct state_list_stack
**stack
)
529 __free_ptr_list((struct ptr_list
**)stack
);
532 void free_stack_and_slists(struct state_list_stack
**slist_stack
)
534 struct state_list
*slist
;
536 FOR_EACH_PTR(*slist_stack
, slist
) {
538 } END_FOR_EACH_PTR(slist
);
539 free_stack(slist_stack
);
543 * set_state_stack() sets the state for the top slist on the stack.
545 void set_state_stack(struct state_list_stack
**stack
, const char *name
,
546 int owner
, struct symbol
*sym
, struct smatch_state
*state
)
548 struct state_list
*slist
;
550 slist
= pop_slist(stack
);
551 set_state_slist(&slist
, name
, owner
, sym
, state
);
552 push_slist(stack
, slist
);
556 * get_sm_state_stack() gets the state for the top slist on the stack.
558 struct sm_state
*get_sm_state_stack(struct state_list_stack
*stack
,
559 const char *name
, int owner
,
562 struct state_list
*slist
;
563 struct sm_state
*ret
;
565 slist
= pop_slist(&stack
);
566 ret
= get_sm_state_slist(slist
, name
, owner
, sym
);
567 push_slist(&stack
, slist
);
572 struct smatch_state
*get_state_stack(struct state_list_stack
*stack
,
573 const char *name
, int owner
,
576 struct sm_state
*state
;
578 state
= get_sm_state_stack(stack
, name
, owner
, sym
);
584 static void match_states(struct state_list
**one
, struct state_list
**two
)
586 struct sm_state
*one_state
;
587 struct sm_state
*two_state
;
588 struct sm_state
*tmp
;
589 struct smatch_state
*tmp_state
;
590 struct state_list
*add_to_one
= NULL
;
591 struct state_list
*add_to_two
= NULL
;
593 PREPARE_PTR_LIST(*one
, one_state
);
594 PREPARE_PTR_LIST(*two
, two_state
);
596 if (!one_state
&& !two_state
)
598 if (cmp_tracker(one_state
, two_state
) < 0) {
599 tmp_state
= __client_unmatched_state_function(one_state
);
600 tmp
= alloc_state_no_name(one_state
->name
,
602 one_state
->sym
, tmp_state
);
603 add_ptr_list(&add_to_two
, tmp
);
604 NEXT_PTR_LIST(one_state
);
605 } else if (cmp_tracker(one_state
, two_state
) == 0) {
606 NEXT_PTR_LIST(one_state
);
607 NEXT_PTR_LIST(two_state
);
609 tmp_state
= __client_unmatched_state_function(two_state
);
610 tmp
= alloc_state_no_name(two_state
->name
,
612 two_state
->sym
, tmp_state
);
613 add_ptr_list(&add_to_one
, tmp
);
614 NEXT_PTR_LIST(two_state
);
617 FINISH_PTR_LIST(two_state
);
618 FINISH_PTR_LIST(one_state
);
620 overwrite_slist(add_to_one
, one
);
621 overwrite_slist(add_to_two
, two
);
625 * merge_slist() is called whenever paths merge, such as after
626 * an if statement. It takes the two slists and creates one.
628 void merge_slist(struct state_list
**to
, struct state_list
*slist
)
630 struct sm_state
*to_state
, *state
, *tmp
;
631 struct state_list
*results
= NULL
;
632 struct state_list
*implied_to
= NULL
;
633 struct state_list
*implied_from
= NULL
;
637 sanity_check_pools(*to
);
638 sanity_check_pools(slist
);
640 /* merging a null and nonnull path gives you only the nonnull path */
645 *to
= clone_slist(slist
);
649 implied_to
= clone_slist(*to
);
650 implied_from
= clone_slist(slist
);
652 match_states(&implied_to
, &implied_from
);
654 PREPARE_PTR_LIST(implied_to
, to_state
);
655 PREPARE_PTR_LIST(implied_from
, state
);
657 if (!to_state
&& !state
)
659 if (cmp_tracker(to_state
, state
) < 0) {
660 smatch_msg("error: Internal smatch error.");
661 NEXT_PTR_LIST(to_state
);
662 } else if (cmp_tracker(to_state
, state
) == 0) {
663 if (state
->owner
== SMATCH_EXTRA
) {
664 tmp
= __extra_merge(to_state
, implied_to
,
665 state
, implied_from
);
666 add_ptr_list(&results
, tmp
);
667 NEXT_PTR_LIST(to_state
);
668 NEXT_PTR_LIST(state
);
671 if (to_state
->state
!= &merged
)
672 free_stack(&to_state
->my_pools
);
673 if (state
->state
!= &merged
)
674 free_stack(&state
->my_pools
);
676 if (to_state
== state
&& !state
->my_pools
) {
677 add_pool(&state
->my_pools
, implied_to
);
678 add_pool(&state
->my_pools
, implied_from
);
680 if (!to_state
->my_pools
)
681 add_pool(&to_state
->my_pools
, implied_to
);
682 if (!state
->my_pools
)
683 add_pool(&state
->my_pools
, implied_from
);
686 add_pool(&to_state
->all_pools
, implied_to
);
687 add_pool(&state
->all_pools
, implied_from
);
689 tmp
= merge_sm_states(to_state
, state
);
690 add_ptr_list(&results
, tmp
);
691 NEXT_PTR_LIST(to_state
);
692 NEXT_PTR_LIST(state
);
694 smatch_msg("error: Internal smatch error.");
695 NEXT_PTR_LIST(state
);
698 FINISH_PTR_LIST(state
);
699 FINISH_PTR_LIST(to_state
);
705 static struct sm_state
*find_intersection(struct sm_state
*one
,
706 struct sm_state
*two
)
708 struct state_list
*tmp1
, *tmp2
;
709 struct state_list_stack
*stack
= NULL
;
710 struct sm_state
*tmp_state
;
711 struct sm_state
*ret
;
717 if (one
->owner
!= SMATCH_EXTRA
&& one
->state
!= &merged
) {
718 if (one
->state
== two
->state
)
720 if (two
->state
!= &merged
) {
721 SM_DEBUG("mutually exclusive 'and' conditions states "
722 "'%s': %s + %s\n", one
->name
,
723 show_state(one
->state
),
724 show_state(two
->state
));
728 if (one
->owner
== SMATCH_EXTRA
) {
729 if (one
->state
== two
->state
)
733 if (!one
->my_pools
) {
736 if (!two
->my_pools
) {
743 PREPARE_PTR_LIST(one
->my_pools
, tmp1
);
744 PREPARE_PTR_LIST(two
->my_pools
, tmp2
);
748 if (!tmp2
|| (tmp1
&& tmp1
< tmp2
)) {
750 } else if (tmp1
== tmp2
) {
751 push_slist(&stack
, tmp1
);
759 FINISH_PTR_LIST(tmp2
);
760 FINISH_PTR_LIST(tmp1
);
763 SM_DEBUG("mutually eXclusive 'and' conditions states "
764 "'%s': %s + %s\n", one
->name
, show_state(one
->state
),
765 show_state(two
->state
));
769 return get_sm_state_stack(stack
, one
->name
, one
->owner
,
772 if (one
->owner
== SMATCH_EXTRA
)
773 return __extra_and_merge(one
, stack
);
775 ret
= alloc_state_no_name(one
->name
, one
->owner
, one
->sym
, &merged
);
776 FOR_EACH_PTR(stack
, tmp1
) {
777 tmp_state
= get_sm_state_slist(tmp1
, one
->name
, one
->owner
,
779 add_possible(ret
, tmp_state
);
780 } END_FOR_EACH_PTR(tmp1
);
781 ret
->my_pools
= stack
;
782 ret
->all_pools
= clone_stack(stack
);
787 * and_slist_stack() is basically the same as popping the top two slists,
788 * overwriting the one with the other and pushing it back on the stack.
789 * The difference is that it checks to see that a mutually exclusive
790 * state isn't included in both stacks. If smatch sees something like
791 * "if (a && !a)" it prints a warning.
793 void and_slist_stack(struct state_list_stack
**slist_stack
)
795 struct sm_state
*tmp
;
796 struct sm_state
*left_state
;
797 struct sm_state
*res
;
798 struct state_list
*right_slist
= pop_slist(slist_stack
);
800 FOR_EACH_PTR(right_slist
, tmp
) {
801 left_state
= get_sm_state_stack(*slist_stack
, tmp
->name
,
802 tmp
->owner
, tmp
->sym
);
803 res
= find_intersection(left_state
, tmp
);
804 overwrite_sm_state_stack(slist_stack
, res
);
805 } END_FOR_EACH_PTR(tmp
);
806 free_slist(&right_slist
);
810 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
811 * It pops the two slists from the top of the stack and merges them
812 * together in a way that preserves the things they have in common
813 * but creates a merged state for most of the rest.
814 * You could have code that had: if (foo || foo) { foo->baz;
815 * It's this function which ensures smatch does the right thing.
817 void or_slist_stack(struct state_list_stack
**pre_conds
,
818 struct state_list
*cur_slist
,
819 struct state_list_stack
**slist_stack
)
821 struct state_list
*new;
822 struct state_list
*old
;
823 struct state_list
*res
= NULL
;
824 struct state_list
*tmp_slist
;
826 new = pop_slist(slist_stack
);
827 old
= pop_slist(slist_stack
);
829 tmp_slist
= pop_slist(pre_conds
);
830 res
= clone_slist(tmp_slist
);
831 push_slist(pre_conds
, tmp_slist
);
832 overwrite_slist(old
, &res
);
834 tmp_slist
= clone_slist(cur_slist
);
835 overwrite_slist(new, &tmp_slist
);
837 merge_slist(&res
, tmp_slist
);
839 push_slist(slist_stack
, res
);
840 free_slist(&tmp_slist
);
846 * get_slist_from_named_stack() is only used for gotos.
848 struct state_list
**get_slist_from_named_stack(struct named_stack
*stack
,
851 struct named_slist
*tmp
;
853 FOR_EACH_PTR(stack
, tmp
) {
854 if (!strcmp(tmp
->name
, name
))
856 } END_FOR_EACH_PTR(tmp
);
860 void overwrite_slist(struct state_list
*from
, struct state_list
**to
)
862 struct sm_state
*tmp
;
864 FOR_EACH_PTR(from
, tmp
) {
865 overwrite_sm_state(to
, tmp
);
866 } END_FOR_EACH_PTR(tmp
);
869 unsigned int __get_allocations()
871 return sm_state_allocator
.allocations
;