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
);
45 /* NULL states go at the end to simplify merge_slist */
46 int cmp_tracker(const struct sm_state
*a
, const struct sm_state
*b
)
57 if (a
->owner
> b
->owner
)
59 if (a
->owner
< b
->owner
)
62 ret
= strcmp(a
->name
, b
->name
);
66 if (!b
->sym
&& a
->sym
)
68 if (!a
->sym
&& b
->sym
)
78 static int cmp_sm_states(const struct sm_state
*a
, const struct sm_state
*b
)
82 ret
= cmp_tracker(a
, b
);
86 /* todo: add hook for smatch_extra.c */
87 if (a
->state
> b
->state
)
89 if (a
->state
< b
->state
)
94 static struct sm_state
*alloc_sm_state(int owner
, const char *name
,
95 struct symbol
*sym
, struct smatch_state
*state
)
97 struct sm_state
*sm_state
= __alloc_sm_state(0);
101 sm_state
->name
= alloc_sname(name
);
102 sm_state
->owner
= owner
;
104 sm_state
->state
= state
;
105 sm_state
->line
= get_lineno();
106 sm_state
->merged
= 0;
107 sm_state
->implied
= 0;
108 sm_state
->my_pool
= NULL
;
109 sm_state
->left
= NULL
;
110 sm_state
->right
= NULL
;
111 sm_state
->nr_children
= 1;
112 sm_state
->possible
= NULL
;
113 add_ptr_list(&sm_state
->possible
, sm_state
);
117 static struct sm_state
*alloc_state_no_name(int owner
, const char *name
,
119 struct smatch_state
*state
)
121 struct sm_state
*tmp
;
123 tmp
= alloc_sm_state(owner
, NULL
, sym
, state
);
128 void add_sm_state_slist(struct state_list
**slist
, struct sm_state
*new)
130 struct sm_state
*tmp
;
132 FOR_EACH_PTR(*slist
, tmp
) {
133 if (cmp_sm_states(tmp
, new) < 0)
135 else if (cmp_sm_states(tmp
, new) == 0) {
138 INSERT_CURRENT(new, tmp
);
141 } END_FOR_EACH_PTR(tmp
);
142 add_ptr_list(slist
, new);
145 static void add_possible(struct sm_state
*sm
, struct sm_state
*new)
147 struct sm_state
*tmp
;
148 struct sm_state
*tmp2
;
150 FOR_EACH_PTR(new->possible
, tmp
) {
151 tmp2
= alloc_state_no_name(tmp
->owner
, tmp
->name
, tmp
->sym
,
153 tmp2
->line
= tmp
->line
;
154 add_sm_state_slist(&sm
->possible
, tmp2
);
155 } END_FOR_EACH_PTR(tmp
);
158 char *alloc_sname(const char *str
)
164 tmp
= __alloc_sname(strlen(str
) + 1);
172 * I decided to use 50M here based on trial and error.
173 * It works out OK for the kernel and so it should work
174 * for most other projects as well.
176 if (sm_state_counter
* sizeof(struct sm_state
) >= 50000000)
181 static void free_sm_state(struct sm_state
*sm
)
183 free_slist(&sm
->possible
);
185 * fixme. Free the actual state.
186 * Right now we leave it until the end of the function
187 * because we don't want to double free it.
188 * Use the freelist to not double free things
192 static void free_all_sm_states(struct allocation_blob
*blob
)
194 unsigned int size
= sizeof(struct sm_state
);
195 unsigned int offset
= 0;
197 while (offset
< blob
->offset
) {
198 free_sm_state((struct sm_state
*)(blob
->data
+ offset
));
203 /* At the end of every function we free all the sm_states */
204 void free_every_single_sm_state(void)
206 struct allocator_struct
*desc
= &sm_state_allocator
;
207 struct allocation_blob
*blob
= desc
->blobs
;
210 desc
->allocations
= 0;
211 desc
->total_bytes
= 0;
212 desc
->useful_bytes
= 0;
213 desc
->freelist
= NULL
;
215 struct allocation_blob
*next
= blob
->next
;
216 free_all_sm_states(blob
);
217 blob_free(blob
, desc
->chunking
);
222 sm_state_counter
= 0;
225 struct sm_state
*clone_sm(struct sm_state
*s
)
227 struct sm_state
*ret
;
229 ret
= alloc_state_no_name(s
->owner
, s
->name
, s
->sym
, s
->state
);
230 ret
->merged
= s
->merged
;
231 ret
->implied
= s
->implied
;
233 /* clone_sm() doesn't copy the my_pools. Each state needs to have
235 ret
->possible
= clone_slist(s
->possible
);
237 ret
->right
= s
->right
;
238 ret
->nr_children
= s
->nr_children
;
242 int is_merged(struct sm_state
*sm
)
247 int is_implied(struct sm_state
*sm
)
252 int slist_has_state(struct state_list
*slist
, struct smatch_state
*state
)
254 struct sm_state
*tmp
;
256 FOR_EACH_PTR(slist
, tmp
) {
257 if (tmp
->state
== state
)
259 } END_FOR_EACH_PTR(tmp
);
263 static void check_order(struct state_list
*slist
)
267 struct sm_state
*last
= NULL
;
270 FOR_EACH_PTR(slist
, sm
) {
271 if (last
&& cmp_tracker(sm
, last
) <= 0) {
272 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
273 "%s vs %s\n", last
->owner
, sm
->owner
,
274 last
->sym
, sm
->sym
, last
->name
, sm
->name
);
278 } END_FOR_EACH_PTR(sm
);
285 struct state_list
*clone_slist(struct state_list
*from_slist
)
288 struct state_list
*to_slist
= NULL
;
290 FOR_EACH_PTR(from_slist
, sm
) {
291 add_ptr_list(&to_slist
, sm
);
292 } END_FOR_EACH_PTR(sm
);
293 check_order(to_slist
);
297 struct state_list_stack
*clone_stack(struct state_list_stack
*from_stack
)
299 struct state_list
*slist
;
300 struct state_list_stack
*to_stack
= NULL
;
302 FOR_EACH_PTR(from_stack
, slist
) {
303 push_slist(&to_stack
, slist
);
304 } END_FOR_EACH_PTR(slist
);
308 struct smatch_state
*merge_states(int owner
, const char *name
,
310 struct smatch_state
*state1
,
311 struct smatch_state
*state2
)
313 struct smatch_state
*ret
;
315 if (state1
== state2
)
317 else if (__has_merge_function(owner
))
318 ret
= __client_merge_function(owner
, name
, sym
, state1
, state2
);
319 else if (!state1
|| !state2
)
327 * add_pool() adds a slist to ->pools. If the slist has already been
328 * added earlier then it doesn't get added a second time.
330 void add_pool(struct state_list_stack
**pools
, struct state_list
*new)
332 struct state_list
*tmp
;
334 FOR_EACH_PTR(*pools
, tmp
) {
337 else if (tmp
== new) {
340 INSERT_CURRENT(new, tmp
);
343 } END_FOR_EACH_PTR(tmp
);
344 add_ptr_list(pools
, new);
347 struct sm_state
*merge_sm_states(struct sm_state
*one
, struct sm_state
*two
)
349 struct smatch_state
*s
;
350 struct sm_state
*result
;
354 s
= merge_states(one
->owner
, one
->name
, one
->sym
, one
->state
, two
->state
);
355 result
= alloc_state_no_name(one
->owner
, one
->name
, one
->sym
, s
);
359 result
->nr_children
= one
->nr_children
+ two
->nr_children
;
360 add_possible(result
, one
);
361 add_possible(result
, two
);
364 struct sm_state
*tmp
;
367 printf("%d merge name='%s' [%s] %s(L %d) + %s(L %d) => %s (",
368 get_lineno(), one
->name
, check_name(one
->owner
),
369 show_state(one
->state
), one
->line
,
370 show_state(two
->state
), two
->line
,
373 FOR_EACH_PTR(result
->possible
, tmp
) {
376 printf("%s", show_state(tmp
->state
));
377 } END_FOR_EACH_PTR(tmp
);
384 struct sm_state
*get_sm_state_slist(struct state_list
*slist
, int owner
, const char *name
,
392 FOR_EACH_PTR(slist
, sm
) {
393 if (sm
->owner
== owner
&& sm
->sym
== sym
&& !strcmp(sm
->name
, name
))
395 } END_FOR_EACH_PTR(sm
);
399 struct smatch_state
*get_state_slist(struct state_list
*slist
,
400 int owner
, const char *name
,
405 sm
= get_sm_state_slist(slist
, owner
, name
, sym
);
411 void overwrite_sm_state(struct state_list
**slist
, struct sm_state
*new)
413 struct sm_state
*tmp
;
415 FOR_EACH_PTR(*slist
, tmp
) {
416 if (cmp_tracker(tmp
, new) < 0)
418 else if (cmp_tracker(tmp
, new) == 0) {
419 REPLACE_CURRENT_PTR(tmp
, new);
422 INSERT_CURRENT(new, tmp
);
425 } END_FOR_EACH_PTR(tmp
);
426 add_ptr_list(slist
, new);
429 void overwrite_sm_state_stack(struct state_list_stack
**stack
,
432 struct state_list
*slist
;
434 slist
= pop_slist(stack
);
435 overwrite_sm_state(&slist
, sm
);
436 push_slist(stack
, slist
);
439 struct sm_state
*set_state_slist(struct state_list
**slist
, int owner
, const char *name
,
440 struct symbol
*sym
, struct smatch_state
*state
)
442 struct sm_state
*tmp
;
443 struct sm_state
*new = alloc_sm_state(owner
, name
, sym
, state
);
445 FOR_EACH_PTR(*slist
, tmp
) {
446 if (cmp_tracker(tmp
, new) < 0)
448 else if (cmp_tracker(tmp
, new) == 0) {
449 REPLACE_CURRENT_PTR(tmp
, new);
452 INSERT_CURRENT(new, tmp
);
455 } END_FOR_EACH_PTR(tmp
);
456 add_ptr_list(slist
, new);
460 void delete_state_slist(struct state_list
**slist
, int owner
, const char *name
,
465 FOR_EACH_PTR(*slist
, sm
) {
466 if (sm
->owner
== owner
&& sm
->sym
== sym
&& !strcmp(sm
->name
, name
)) {
467 DELETE_CURRENT_PTR(sm
);
470 } END_FOR_EACH_PTR(sm
);
473 void delete_state_stack(struct state_list_stack
**stack
, int owner
, const char *name
,
476 struct state_list
*slist
;
478 slist
= pop_slist(stack
);
479 delete_state_slist(&slist
, owner
, name
, sym
);
480 push_slist(stack
, slist
);
483 void push_slist(struct state_list_stack
**list_stack
, struct state_list
*slist
)
485 add_ptr_list(list_stack
, slist
);
488 struct state_list
*pop_slist(struct state_list_stack
**list_stack
)
490 struct state_list
*slist
;
492 slist
= last_ptr_list((struct ptr_list
*)*list_stack
);
493 delete_ptr_list_last((struct ptr_list
**)list_stack
);
497 void free_slist(struct state_list
**slist
)
499 __free_ptr_list((struct ptr_list
**)slist
);
502 void free_stack(struct state_list_stack
**stack
)
504 __free_ptr_list((struct ptr_list
**)stack
);
507 void free_stack_and_slists(struct state_list_stack
**slist_stack
)
509 struct state_list
*slist
;
511 FOR_EACH_PTR(*slist_stack
, slist
) {
513 } END_FOR_EACH_PTR(slist
);
514 free_stack(slist_stack
);
518 * set_state_stack() sets the state for the top slist on the stack.
520 struct sm_state
*set_state_stack(struct state_list_stack
**stack
, int owner
, const char *name
,
521 struct symbol
*sym
, struct smatch_state
*state
)
523 struct state_list
*slist
;
526 slist
= pop_slist(stack
);
527 sm
= set_state_slist(&slist
, owner
, name
, sym
, state
);
528 push_slist(stack
, slist
);
534 * get_sm_state_stack() gets the state for the top slist on the stack.
536 struct sm_state
*get_sm_state_stack(struct state_list_stack
*stack
,
537 int owner
, const char *name
,
540 struct state_list
*slist
;
541 struct sm_state
*ret
;
543 slist
= pop_slist(&stack
);
544 ret
= get_sm_state_slist(slist
, owner
, name
, sym
);
545 push_slist(&stack
, slist
);
549 struct smatch_state
*get_state_stack(struct state_list_stack
*stack
,
550 int owner
, const char *name
,
555 sm
= get_sm_state_stack(stack
, owner
, name
, sym
);
561 static void match_states(struct state_list
**one
, struct state_list
**two
)
563 struct sm_state
*one_sm
;
564 struct sm_state
*two_sm
;
565 struct sm_state
*tmp
;
566 struct smatch_state
*tmp_state
;
567 struct state_list
*add_to_one
= NULL
;
568 struct state_list
*add_to_two
= NULL
;
570 PREPARE_PTR_LIST(*one
, one_sm
);
571 PREPARE_PTR_LIST(*two
, two_sm
);
573 if (!one_sm
&& !two_sm
)
575 if (cmp_tracker(one_sm
, two_sm
) < 0) {
576 tmp_state
= __client_unmatched_state_function(one_sm
);
577 tmp
= alloc_state_no_name(one_sm
->owner
, one_sm
->name
,
578 one_sm
->sym
, tmp_state
);
579 add_ptr_list(&add_to_two
, tmp
);
580 NEXT_PTR_LIST(one_sm
);
581 } else if (cmp_tracker(one_sm
, two_sm
) == 0) {
582 NEXT_PTR_LIST(one_sm
);
583 NEXT_PTR_LIST(two_sm
);
585 tmp_state
= __client_unmatched_state_function(two_sm
);
586 tmp
= alloc_state_no_name(two_sm
->owner
, two_sm
->name
,
587 two_sm
->sym
, tmp_state
);
588 add_ptr_list(&add_to_one
, tmp
);
589 NEXT_PTR_LIST(two_sm
);
592 FINISH_PTR_LIST(two_sm
);
593 FINISH_PTR_LIST(one_sm
);
595 overwrite_slist(add_to_one
, one
);
596 overwrite_slist(add_to_two
, two
);
599 static void clone_pool_havers(struct state_list
*slist
)
602 struct sm_state
*new;
604 FOR_EACH_PTR(slist
, sm
) {
607 REPLACE_CURRENT_PTR(sm
, new);
609 } END_FOR_EACH_PTR(sm
);
613 * merge_slist() is called whenever paths merge, such as after
614 * an if statement. It takes the two slists and creates one.
616 void merge_slist(struct state_list
**to
, struct state_list
*slist
)
618 struct sm_state
*one_sm
, *two_sm
, *tmp
;
619 struct state_list
*results
= NULL
;
620 struct state_list
*implied_one
= NULL
;
621 struct state_list
*implied_two
= NULL
;
629 /* merging a null and nonnull path gives you only the nonnull path */
634 *to
= clone_slist(slist
);
638 implied_one
= clone_slist(*to
);
639 implied_two
= clone_slist(slist
);
641 match_states(&implied_one
, &implied_two
);
643 clone_pool_havers(implied_one
);
644 clone_pool_havers(implied_two
);
646 PREPARE_PTR_LIST(implied_one
, one_sm
);
647 PREPARE_PTR_LIST(implied_two
, two_sm
);
649 if (!one_sm
&& !two_sm
)
651 if (cmp_tracker(one_sm
, two_sm
) < 0) {
652 sm_msg("error: Internal smatch error.");
653 NEXT_PTR_LIST(one_sm
);
654 } else if (cmp_tracker(one_sm
, two_sm
) == 0) {
655 if (one_sm
!= two_sm
) {
656 one_sm
->my_pool
= implied_one
;
657 two_sm
->my_pool
= implied_two
;
660 tmp
= merge_sm_states(one_sm
, two_sm
);
661 add_ptr_list(&results
, tmp
);
662 NEXT_PTR_LIST(one_sm
);
663 NEXT_PTR_LIST(two_sm
);
665 sm_msg("error: Internal smatch error.");
666 NEXT_PTR_LIST(two_sm
);
669 FINISH_PTR_LIST(two_sm
);
670 FINISH_PTR_LIST(one_sm
);
677 * filter_slist() removes any sm states "slist" holds in common with "filter"
679 void filter_slist(struct state_list
**slist
, struct state_list
*filter
)
681 struct sm_state
*one_sm
, *two_sm
;
682 struct state_list
*results
= NULL
;
684 PREPARE_PTR_LIST(*slist
, one_sm
);
685 PREPARE_PTR_LIST(filter
, two_sm
);
687 if (!one_sm
&& !two_sm
)
689 if (cmp_tracker(one_sm
, two_sm
) < 0) {
690 add_ptr_list(&results
, one_sm
);
691 NEXT_PTR_LIST(one_sm
);
692 } else if (cmp_tracker(one_sm
, two_sm
) == 0) {
693 if (one_sm
!= two_sm
)
694 add_ptr_list(&results
, one_sm
);
695 NEXT_PTR_LIST(one_sm
);
696 NEXT_PTR_LIST(two_sm
);
698 NEXT_PTR_LIST(two_sm
);
701 FINISH_PTR_LIST(two_sm
);
702 FINISH_PTR_LIST(one_sm
);
709 * and_slist_stack() pops the top two slists, overwriting the one with
710 * the other and pushing it back on the stack.
712 void and_slist_stack(struct state_list_stack
**slist_stack
)
714 struct sm_state
*tmp
;
715 struct state_list
*right_slist
= pop_slist(slist_stack
);
717 FOR_EACH_PTR(right_slist
, tmp
) {
718 overwrite_sm_state_stack(slist_stack
, tmp
);
719 } END_FOR_EACH_PTR(tmp
);
720 free_slist(&right_slist
);
724 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
725 * It pops the two slists from the top of the stack and merges them
726 * together in a way that preserves the things they have in common
727 * but creates a merged state for most of the rest.
728 * You could have code that had: if (foo || foo) { foo->baz;
729 * It's this function which ensures smatch does the right thing.
731 void or_slist_stack(struct state_list_stack
**pre_conds
,
732 struct state_list
*cur_slist
,
733 struct state_list_stack
**slist_stack
)
735 struct state_list
*new;
736 struct state_list
*old
;
737 struct state_list
*pre_slist
;
738 struct state_list
*res
;
739 struct state_list
*tmp_slist
;
741 new = pop_slist(slist_stack
);
742 old
= pop_slist(slist_stack
);
744 pre_slist
= pop_slist(pre_conds
);
745 push_slist(pre_conds
, clone_slist(pre_slist
));
747 res
= clone_slist(pre_slist
);
748 overwrite_slist(old
, &res
);
750 tmp_slist
= clone_slist(cur_slist
);
751 overwrite_slist(new, &tmp_slist
);
753 merge_slist(&res
, tmp_slist
);
754 filter_slist(&res
, pre_slist
);
756 push_slist(slist_stack
, res
);
757 free_slist(&tmp_slist
);
758 free_slist(&pre_slist
);
764 * get_slist_from_named_stack() is only used for gotos.
766 struct state_list
**get_slist_from_named_stack(struct named_stack
*stack
,
769 struct named_slist
*tmp
;
771 FOR_EACH_PTR(stack
, tmp
) {
772 if (!strcmp(tmp
->name
, name
))
774 } END_FOR_EACH_PTR(tmp
);
778 void overwrite_slist(struct state_list
*from
, struct state_list
**to
)
780 struct sm_state
*tmp
;
782 FOR_EACH_PTR(from
, tmp
) {
783 overwrite_sm_state(to
, tmp
);
784 } END_FOR_EACH_PTR(tmp
);