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(smatch_state
, "smatch state");
18 ALLOCATOR(sm_state
, "sm state");
19 ALLOCATOR(named_slist
, "named slist");
20 __DO_ALLOCATOR(char, 0, 1, "state names", sname
);
22 static int sm_state_counter
;
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("[%s] '%s'=%s (", check_name(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(int owner
, const char *name
,
97 struct smatch_state
*state
)
101 tmp
= alloc_sm_state(owner
, NULL
, 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
->owner
, sm
->name
, sm
->sym
, sm
->state
, NULL
);
132 tmp
= alloc_state_no_name(sm
->owner
, sm
->name
, 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
->owner
,tmp
->name
, 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);
158 * I decided to use 50M here based on trial and error.
159 * It works out OK for the kernel and so it should work
160 * for most other projects as well.
162 if (sm_state_counter
* sizeof(struct sm_state
) >= 50000000)
167 struct sm_state
*alloc_sm_state(int owner
, const char *name
,
168 struct symbol
*sym
, struct smatch_state
*state
)
170 struct sm_state
*sm_state
= __alloc_sm_state(0);
174 sm_state
->name
= alloc_sname(name
);
175 sm_state
->owner
= owner
;
177 sm_state
->state
= state
;
178 sm_state
->line
= get_lineno();
179 sm_state
->merged
= 0;
180 sm_state
->implied
= 0;
181 sm_state
->my_pool
= NULL
;
182 sm_state
->left
= NULL
;
183 sm_state
->right
= NULL
;
184 sm_state
->nr_children
= 1;
185 sm_state
->possible
= NULL
;
186 add_ptr_list(&sm_state
->possible
, sm_state
);
190 static void free_sm_state(struct sm_state
*sm
)
192 free_slist(&sm
->possible
);
194 * fixme. Free the actual state.
195 * Right now we leave it until the end of the function
196 * because we don't want to double free it.
197 * Use the freelist to not double free things
201 static void free_all_sm_states(struct allocation_blob
*blob
)
203 unsigned int size
= sizeof(struct sm_state
);
204 unsigned int offset
= 0;
206 while (offset
< blob
->offset
) {
207 free_sm_state((struct sm_state
*)(blob
->data
+ offset
));
212 /* At the end of every function we free all the sm_states */
213 void free_every_single_sm_state(void)
215 struct allocator_struct
*desc
= &sm_state_allocator
;
216 struct allocation_blob
*blob
= desc
->blobs
;
219 desc
->allocations
= 0;
220 desc
->total_bytes
= 0;
221 desc
->useful_bytes
= 0;
222 desc
->freelist
= NULL
;
224 struct allocation_blob
*next
= blob
->next
;
225 free_all_sm_states(blob
);
226 blob_free(blob
, desc
->chunking
);
231 sm_state_counter
= 0;;
234 struct sm_state
*clone_sm(struct sm_state
*s
)
236 struct sm_state
*ret
;
238 ret
= alloc_state_no_name(s
->owner
, s
->name
, s
->sym
, s
->state
);
239 ret
->merged
= s
->merged
;
240 ret
->implied
= s
->implied
;
241 /* clone_sm() doesn't copy the my_pools. Each state needs to have
243 ret
->possible
= clone_slist(s
->possible
);
245 ret
->right
= s
->right
;
246 ret
->nr_children
= s
->nr_children
;
250 int is_merged(struct sm_state
*sm
)
255 int is_implied(struct sm_state
*sm
)
260 int slist_has_state(struct state_list
*slist
, struct smatch_state
*state
)
262 struct sm_state
*tmp
;
264 FOR_EACH_PTR(slist
, tmp
) {
265 if (tmp
->state
== state
)
267 } END_FOR_EACH_PTR(tmp
);
271 static void check_order(struct state_list
*slist
)
275 struct sm_state
*last
= NULL
;
278 FOR_EACH_PTR(slist
, sm
) {
279 if (last
&& cmp_tracker(sm
, last
) <= 0) {
280 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
281 "%s vs %s\n", last
->owner
, sm
->owner
,
282 last
->sym
, sm
->sym
, last
->name
, sm
->name
);
286 } END_FOR_EACH_PTR(sm
);
293 struct state_list
*clone_slist(struct state_list
*from_slist
)
296 struct state_list
*to_slist
= NULL
;
298 FOR_EACH_PTR(from_slist
, sm
) {
299 add_ptr_list(&to_slist
, sm
);
300 } END_FOR_EACH_PTR(sm
);
301 check_order(to_slist
);
305 struct state_list_stack
*clone_stack(struct state_list_stack
*from_stack
)
307 struct state_list
*slist
;
308 struct state_list_stack
*to_stack
= NULL
;
310 FOR_EACH_PTR(from_stack
, slist
) {
311 push_slist(&to_stack
, slist
);
312 } END_FOR_EACH_PTR(slist
);
316 struct smatch_state
*merge_states(int owner
, const char *name
,
318 struct smatch_state
*state1
,
319 struct smatch_state
*state2
)
321 struct smatch_state
*ret
;
323 if (state1
== state2
)
325 else if (__has_merge_function(owner
))
326 ret
= __client_merge_function(owner
, name
, sym
, state1
, state2
);
327 else if (!state1
|| !state2
)
335 * add_pool() adds a slist to ->pools. If the slist has already been
336 * added earlier then it doesn't get added a second time.
338 void add_pool(struct state_list_stack
**pools
, struct state_list
*new)
340 struct state_list
*tmp
;
342 FOR_EACH_PTR(*pools
, tmp
) {
345 else if (tmp
== new) {
348 INSERT_CURRENT(new, tmp
);
351 } END_FOR_EACH_PTR(tmp
);
352 add_ptr_list(pools
, new);
355 struct sm_state
*merge_sm_states(struct sm_state
*one
, struct sm_state
*two
)
357 struct smatch_state
*s
;
358 struct sm_state
*result
;
362 s
= merge_states(one
->owner
, one
->name
, one
->sym
, one
->state
, two
->state
);
363 result
= alloc_state_no_name(one
->owner
, one
->name
, one
->sym
, s
);
364 if (one
->line
== two
->line
)
365 result
->line
= one
->line
;
369 result
->nr_children
= one
->nr_children
+ two
->nr_children
;
370 add_possible(result
, one
);
371 add_possible(result
, two
);
374 struct sm_state
*tmp
;
377 printf("%d merge name='%s' [%s] %s(L %d) + %s(L %d) => %s (",
378 get_lineno(), one
->name
, check_name(one
->owner
),
379 show_state(one
->state
), one
->line
,
380 show_state(two
->state
), two
->line
,
383 FOR_EACH_PTR(result
->possible
, tmp
) {
387 printf("%s", show_state(tmp
->state
));
388 } END_FOR_EACH_PTR(tmp
);
395 struct sm_state
*get_sm_state_slist(struct state_list
*slist
, int owner
, const char *name
,
403 FOR_EACH_PTR(slist
, sm
) {
404 if (sm
->owner
== owner
&& sm
->sym
== sym
&& !strcmp(sm
->name
, name
))
406 } END_FOR_EACH_PTR(sm
);
410 struct smatch_state
*get_state_slist(struct state_list
*slist
,
411 int owner
, const char *name
,
416 sm
= get_sm_state_slist(slist
, owner
, name
, sym
);
422 void overwrite_sm_state(struct state_list
**slist
, struct sm_state
*new)
424 struct sm_state
*tmp
;
426 FOR_EACH_PTR(*slist
, tmp
) {
427 if (cmp_tracker(tmp
, new) < 0)
429 else if (cmp_tracker(tmp
, new) == 0) {
430 REPLACE_CURRENT_PTR(tmp
, new);
433 INSERT_CURRENT(new, tmp
);
436 } END_FOR_EACH_PTR(tmp
);
437 add_ptr_list(slist
, new);
440 void overwrite_sm_state_stack(struct state_list_stack
**stack
,
443 struct state_list
*slist
;
445 slist
= pop_slist(stack
);
446 overwrite_sm_state(&slist
, sm
);
447 push_slist(stack
, slist
);
450 struct sm_state
*set_state_slist(struct state_list
**slist
, int owner
, const char *name
,
451 struct symbol
*sym
, struct smatch_state
*state
)
453 struct sm_state
*tmp
;
454 struct sm_state
*new = alloc_sm_state(owner
, name
, sym
, state
);
456 FOR_EACH_PTR(*slist
, tmp
) {
457 if (cmp_tracker(tmp
, new) < 0)
459 else if (cmp_tracker(tmp
, new) == 0) {
460 REPLACE_CURRENT_PTR(tmp
, new);
463 INSERT_CURRENT(new, tmp
);
466 } END_FOR_EACH_PTR(tmp
);
467 add_ptr_list(slist
, new);
471 void delete_state_slist(struct state_list
**slist
, int owner
, const char *name
,
476 FOR_EACH_PTR(*slist
, sm
) {
477 if (sm
->owner
== owner
&& sm
->sym
== sym
&& !strcmp(sm
->name
, name
)){
478 DELETE_CURRENT_PTR(sm
);
481 } END_FOR_EACH_PTR(sm
);
484 void delete_state_stack(struct state_list_stack
**stack
, int owner
, const char *name
,
487 struct state_list
*slist
;
489 slist
= pop_slist(stack
);
490 delete_state_slist(&slist
, owner
, name
, sym
);
491 push_slist(stack
, slist
);
494 void push_slist(struct state_list_stack
**list_stack
, struct state_list
*slist
)
496 add_ptr_list(list_stack
, slist
);
499 struct state_list
*pop_slist(struct state_list_stack
**list_stack
)
501 struct state_list
*slist
;
503 slist
= last_ptr_list((struct ptr_list
*)*list_stack
);
504 delete_ptr_list_last((struct ptr_list
**)list_stack
);
508 void free_slist(struct state_list
**slist
)
510 __free_ptr_list((struct ptr_list
**)slist
);
513 void free_stack(struct state_list_stack
**stack
)
515 __free_ptr_list((struct ptr_list
**)stack
);
518 void free_stack_and_slists(struct state_list_stack
**slist_stack
)
520 struct state_list
*slist
;
522 FOR_EACH_PTR(*slist_stack
, slist
) {
524 } END_FOR_EACH_PTR(slist
);
525 free_stack(slist_stack
);
529 * set_state_stack() sets the state for the top slist on the stack.
531 struct sm_state
*set_state_stack(struct state_list_stack
**stack
, int owner
, const char *name
,
532 struct symbol
*sym
, struct smatch_state
*state
)
534 struct state_list
*slist
;
537 slist
= pop_slist(stack
);
538 sm
= set_state_slist(&slist
, owner
, name
, sym
, state
);
539 push_slist(stack
, slist
);
545 * get_sm_state_stack() gets the state for the top slist on the stack.
547 struct sm_state
*get_sm_state_stack(struct state_list_stack
*stack
,
548 int owner
, const char *name
,
551 struct state_list
*slist
;
552 struct sm_state
*ret
;
554 slist
= pop_slist(&stack
);
555 ret
= get_sm_state_slist(slist
, owner
, name
, sym
);
556 push_slist(&stack
, slist
);
561 struct smatch_state
*get_state_stack(struct state_list_stack
*stack
,
562 int owner
, const char *name
,
567 sm
= get_sm_state_stack(stack
, owner
, name
, sym
);
573 static void match_states(struct state_list
**one
, struct state_list
**two
)
575 struct sm_state
*one_sm
;
576 struct sm_state
*two_sm
;
577 struct sm_state
*tmp
;
578 struct smatch_state
*tmp_state
;
579 struct state_list
*add_to_one
= NULL
;
580 struct state_list
*add_to_two
= NULL
;
582 PREPARE_PTR_LIST(*one
, one_sm
);
583 PREPARE_PTR_LIST(*two
, two_sm
);
585 if (!one_sm
&& !two_sm
)
587 if (cmp_tracker(one_sm
, two_sm
) < 0) {
588 tmp_state
= __client_unmatched_state_function(one_sm
);
589 tmp
= alloc_state_no_name(one_sm
->owner
, one_sm
->name
,
590 one_sm
->sym
, tmp_state
);
591 add_ptr_list(&add_to_two
, tmp
);
592 NEXT_PTR_LIST(one_sm
);
593 } else if (cmp_tracker(one_sm
, two_sm
) == 0) {
594 NEXT_PTR_LIST(one_sm
);
595 NEXT_PTR_LIST(two_sm
);
597 tmp_state
= __client_unmatched_state_function(two_sm
);
598 tmp
= alloc_state_no_name(two_sm
->owner
, two_sm
->name
,
599 two_sm
->sym
, tmp_state
);
600 add_ptr_list(&add_to_one
, tmp
);
601 NEXT_PTR_LIST(two_sm
);
604 FINISH_PTR_LIST(two_sm
);
605 FINISH_PTR_LIST(one_sm
);
607 overwrite_slist(add_to_one
, one
);
608 overwrite_slist(add_to_two
, two
);
611 static void clone_pool_havers(struct state_list
*slist
)
614 struct sm_state
*new;
616 FOR_EACH_PTR(slist
, sm
) {
619 REPLACE_CURRENT_PTR(sm
, new);
621 } END_FOR_EACH_PTR(sm
);
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
*one_sm
, *two_sm
, *tmp
;
631 struct state_list
*results
= NULL
;
632 struct state_list
*implied_one
= NULL
;
633 struct state_list
*implied_two
= NULL
;
641 /* merging a null and nonnull path gives you only the nonnull path */
646 *to
= clone_slist(slist
);
650 implied_one
= clone_slist(*to
);
651 implied_two
= clone_slist(slist
);
653 match_states(&implied_one
, &implied_two
);
655 clone_pool_havers(implied_one
);
656 clone_pool_havers(implied_two
);
658 PREPARE_PTR_LIST(implied_one
, one_sm
);
659 PREPARE_PTR_LIST(implied_two
, two_sm
);
661 if (!one_sm
&& !two_sm
)
663 if (cmp_tracker(one_sm
, two_sm
) < 0) {
664 sm_msg("error: Internal smatch error.");
665 NEXT_PTR_LIST(one_sm
);
666 } else if (cmp_tracker(one_sm
, two_sm
) == 0) {
667 if (one_sm
!= two_sm
) {
668 one_sm
->my_pool
= implied_one
;
669 two_sm
->my_pool
= implied_two
;
672 tmp
= merge_sm_states(one_sm
, two_sm
);
673 add_ptr_list(&results
, tmp
);
674 NEXT_PTR_LIST(one_sm
);
675 NEXT_PTR_LIST(two_sm
);
677 sm_msg("error: Internal smatch error.");
678 NEXT_PTR_LIST(two_sm
);
681 FINISH_PTR_LIST(two_sm
);
682 FINISH_PTR_LIST(one_sm
);
689 * filter_slist() removes any sm states "slist" holds in common with "filter"
691 void filter_slist(struct state_list
**slist
, struct state_list
*filter
)
693 struct sm_state
*one_sm
, *two_sm
;
694 struct state_list
*results
= NULL
;
696 PREPARE_PTR_LIST(*slist
, one_sm
);
697 PREPARE_PTR_LIST(filter
, two_sm
);
699 if (!one_sm
&& !two_sm
)
701 if (cmp_tracker(one_sm
, two_sm
) < 0) {
702 add_ptr_list(&results
, one_sm
);
703 NEXT_PTR_LIST(one_sm
);
704 } else if (cmp_tracker(one_sm
, two_sm
) == 0) {
705 if (one_sm
!= two_sm
)
706 add_ptr_list(&results
, one_sm
);
707 NEXT_PTR_LIST(one_sm
);
708 NEXT_PTR_LIST(two_sm
);
710 NEXT_PTR_LIST(two_sm
);
713 FINISH_PTR_LIST(two_sm
);
714 FINISH_PTR_LIST(one_sm
);
721 * and_slist_stack() pops the top two slists, overwriting the one with
722 * the other and pushing it back on the stack.
724 void and_slist_stack(struct state_list_stack
**slist_stack
)
726 struct sm_state
*tmp
;
727 struct state_list
*right_slist
= pop_slist(slist_stack
);
729 FOR_EACH_PTR(right_slist
, tmp
) {
730 overwrite_sm_state_stack(slist_stack
, tmp
);
731 } END_FOR_EACH_PTR(tmp
);
732 free_slist(&right_slist
);
736 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
737 * It pops the two slists from the top of the stack and merges them
738 * together in a way that preserves the things they have in common
739 * but creates a merged state for most of the rest.
740 * You could have code that had: if (foo || foo) { foo->baz;
741 * It's this function which ensures smatch does the right thing.
743 void or_slist_stack(struct state_list_stack
**pre_conds
,
744 struct state_list
*cur_slist
,
745 struct state_list_stack
**slist_stack
)
747 struct state_list
*new;
748 struct state_list
*old
;
749 struct state_list
*pre_slist
;
750 struct state_list
*res
;
751 struct state_list
*tmp_slist
;
753 new = pop_slist(slist_stack
);
754 old
= pop_slist(slist_stack
);
756 pre_slist
= pop_slist(pre_conds
);
757 push_slist(pre_conds
, clone_slist(pre_slist
));
759 res
= clone_slist(pre_slist
);
760 overwrite_slist(old
, &res
);
762 tmp_slist
= clone_slist(cur_slist
);
763 overwrite_slist(new, &tmp_slist
);
765 merge_slist(&res
, tmp_slist
);
766 filter_slist(&res
, pre_slist
);
768 push_slist(slist_stack
, res
);
769 free_slist(&tmp_slist
);
770 free_slist(&pre_slist
);
776 * get_slist_from_named_stack() is only used for gotos.
778 struct state_list
**get_slist_from_named_stack(struct named_stack
*stack
,
781 struct named_slist
*tmp
;
783 FOR_EACH_PTR(stack
, tmp
) {
784 if (!strcmp(tmp
->name
, name
))
786 } END_FOR_EACH_PTR(tmp
);
790 void overwrite_slist(struct state_list
*from
, struct state_list
**to
)
792 struct sm_state
*tmp
;
794 FOR_EACH_PTR(from
, tmp
) {
795 overwrite_sm_state(to
, tmp
);
796 } END_FOR_EACH_PTR(tmp
);