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 void __print_slist(struct state_list
*slist
)
24 struct sm_state
*state
;
25 struct sm_state
*poss
;
28 printf("dumping slist at %d\n", get_lineno());
29 FOR_EACH_PTR(slist
, state
) {
30 printf("[%s] '%s'=%s (", check_name(state
->owner
), state
->name
,
31 show_state(state
->state
));
33 FOR_EACH_PTR(state
->possible
, poss
) {
36 printf("%s", show_state(poss
->state
));
37 } END_FOR_EACH_PTR(poss
);
39 } END_FOR_EACH_PTR(state
);
44 /* NULL states go at the end to simplify merge_slist */
45 int cmp_tracker(const struct sm_state
*a
, const struct sm_state
*b
)
56 if (a
->owner
> b
->owner
)
58 if (a
->owner
< b
->owner
)
61 ret
= strcmp(a
->name
, b
->name
);
65 if (!b
->sym
&& a
->sym
)
67 if (!a
->sym
&& b
->sym
)
77 static int cmp_sm_states(const struct sm_state
*a
, const struct sm_state
*b
)
81 ret
= cmp_tracker(a
, b
);
85 /* todo: add hook for smatch_extra.c */
86 if (a
->state
> b
->state
)
88 if (a
->state
< b
->state
)
93 static struct sm_state
*alloc_state_no_name(int owner
, const char *name
,
95 struct smatch_state
*state
)
99 tmp
= alloc_sm_state(owner
, NULL
, sym
, state
);
104 void add_sm_state_slist(struct state_list
**slist
, struct sm_state
*new)
106 struct sm_state
*tmp
;
108 FOR_EACH_PTR(*slist
, tmp
) {
109 if (cmp_sm_states(tmp
, new) < 0)
111 else if (cmp_sm_states(tmp
, new) == 0) {
114 INSERT_CURRENT(new, tmp
);
117 } END_FOR_EACH_PTR(tmp
);
118 add_ptr_list(slist
, new);
121 static void add_possible(struct sm_state
*sm
, struct sm_state
*new)
123 struct sm_state
*tmp
;
124 struct sm_state
*tmp2
;
127 struct smatch_state
*s
;
129 s
= merge_states(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
, NULL
);
130 tmp
= alloc_state_no_name(sm
->owner
, sm
->name
, sm
->sym
, s
);
131 add_sm_state_slist(&sm
->possible
, tmp
);
135 FOR_EACH_PTR(new->possible
, tmp
) {
136 tmp2
= alloc_state_no_name(tmp
->owner
,tmp
->name
, tmp
->sym
,
138 add_sm_state_slist(&sm
->possible
, tmp2
);
139 } END_FOR_EACH_PTR(tmp
);
142 char *alloc_sname(const char *str
)
148 tmp
= __alloc_sname(strlen(str
) + 1);
153 struct sm_state
*alloc_sm_state(int owner
, const char *name
,
154 struct symbol
*sym
, struct smatch_state
*state
)
156 struct sm_state
*sm_state
= __alloc_sm_state(0);
158 sm_state
->name
= alloc_sname(name
);
159 sm_state
->owner
= owner
;
161 sm_state
->state
= state
;
162 sm_state
->line
= get_lineno();
163 sm_state
->merged
= 0;
164 sm_state
->implied
= 0;
165 sm_state
->my_pool
= NULL
;
166 sm_state
->left
= NULL
;
167 sm_state
->right
= NULL
;
168 sm_state
->nr_children
= 1;
169 sm_state
->possible
= NULL
;
170 add_ptr_list(&sm_state
->possible
, sm_state
);
174 static void free_sm_state(struct sm_state
*sm
)
176 free_slist(&sm
->possible
);
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_sm(struct sm_state
*s
)
218 struct sm_state
*ret
;
220 ret
= alloc_state_no_name(s
->owner
, s
->name
, s
->sym
, s
->state
);
221 ret
->merged
= s
->merged
;
222 ret
->implied
= s
->implied
;
223 /* clone_sm() doesn't copy the my_pools. Each state needs to have
225 ret
->possible
= clone_slist(s
->possible
);
227 ret
->right
= s
->right
;
228 ret
->nr_children
= s
->nr_children
;
232 int is_merged(struct sm_state
*sm
)
237 int is_implied(struct sm_state
*sm
)
242 int slist_has_state(struct state_list
*slist
, struct smatch_state
*state
)
244 struct sm_state
*tmp
;
246 FOR_EACH_PTR(slist
, tmp
) {
247 if (tmp
->state
== state
)
249 } END_FOR_EACH_PTR(tmp
);
253 static void check_order(struct state_list
*slist
)
257 struct sm_state
*last
= NULL
;
260 FOR_EACH_PTR(slist
, sm
) {
261 if (last
&& cmp_tracker(sm
, last
) <= 0) {
262 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
263 "%s vs %s\n", last
->owner
, sm
->owner
,
264 last
->sym
, sm
->sym
, last
->name
, sm
->name
);
268 } END_FOR_EACH_PTR(sm
);
275 struct state_list
*clone_slist(struct state_list
*from_slist
)
278 struct state_list
*to_slist
= NULL
;
280 FOR_EACH_PTR(from_slist
, sm
) {
281 add_ptr_list(&to_slist
, sm
);
282 } END_FOR_EACH_PTR(sm
);
283 check_order(to_slist
);
287 struct state_list_stack
*clone_stack(struct state_list_stack
*from_stack
)
289 struct state_list
*slist
;
290 struct state_list_stack
*to_stack
= NULL
;
292 FOR_EACH_PTR(from_stack
, slist
) {
293 push_slist(&to_stack
, slist
);
294 } END_FOR_EACH_PTR(slist
);
298 struct smatch_state
*merge_states(int owner
, const char *name
,
300 struct smatch_state
*state1
,
301 struct smatch_state
*state2
)
303 struct smatch_state
*ret
;
305 if (state1
== state2
)
307 else if (__has_merge_function(owner
))
308 ret
= __client_merge_function(owner
, name
, sym
, state1
, state2
);
309 else if (!state1
|| !state2
)
317 * add_pool() adds a slist to ->pools. If the slist has already been
318 * added earlier then it doesn't get added a second time.
320 void add_pool(struct state_list_stack
**pools
, struct state_list
*new)
322 struct state_list
*tmp
;
324 FOR_EACH_PTR(*pools
, tmp
) {
327 else if (tmp
== new) {
330 INSERT_CURRENT(new, tmp
);
333 } END_FOR_EACH_PTR(tmp
);
334 add_ptr_list(pools
, new);
337 struct sm_state
*merge_sm_states(struct sm_state
*one
, struct sm_state
*two
)
339 struct smatch_state
*s
;
340 struct sm_state
*result
;
344 s
= merge_states(one
->owner
, one
->name
, one
->sym
, one
->state
, two
->state
);
345 result
= alloc_state_no_name(one
->owner
, one
->name
, one
->sym
, s
);
346 if (one
->line
== two
->line
)
347 result
->line
= one
->line
;
351 result
->nr_children
= one
->nr_children
+ two
->nr_children
;
352 add_possible(result
, one
);
353 add_possible(result
, two
);
356 struct sm_state
*tmp
;
359 printf("%d merge name='%s' [%s] %s(L %d) + %s(L %d) => %s (",
360 get_lineno(), one
->name
, check_name(one
->owner
),
361 show_state(one
->state
), one
->line
,
362 show_state(two
->state
), two
->line
,
365 FOR_EACH_PTR(result
->possible
, tmp
) {
369 printf("%s", show_state(tmp
->state
));
370 } END_FOR_EACH_PTR(tmp
);
377 struct sm_state
*get_sm_state_slist(struct state_list
*slist
, int owner
, const char *name
,
385 FOR_EACH_PTR(slist
, sm
) {
386 if (sm
->owner
== owner
&& sm
->sym
== sym
&& !strcmp(sm
->name
, name
))
388 } END_FOR_EACH_PTR(sm
);
392 struct smatch_state
*get_state_slist(struct state_list
*slist
,
393 int owner
, const char *name
,
398 sm
= get_sm_state_slist(slist
, owner
, name
, sym
);
404 void overwrite_sm_state(struct state_list
**slist
, struct sm_state
*new)
406 struct sm_state
*tmp
;
408 FOR_EACH_PTR(*slist
, tmp
) {
409 if (cmp_tracker(tmp
, new) < 0)
411 else if (cmp_tracker(tmp
, new) == 0) {
412 REPLACE_CURRENT_PTR(tmp
, new);
415 INSERT_CURRENT(new, tmp
);
418 } END_FOR_EACH_PTR(tmp
);
419 add_ptr_list(slist
, new);
422 void overwrite_sm_state_stack(struct state_list_stack
**stack
,
425 struct state_list
*slist
;
427 slist
= pop_slist(stack
);
428 overwrite_sm_state(&slist
, sm
);
429 push_slist(stack
, slist
);
432 struct sm_state
*set_state_slist(struct state_list
**slist
, int owner
, const char *name
,
433 struct symbol
*sym
, struct smatch_state
*state
)
435 struct sm_state
*tmp
;
436 struct sm_state
*new = alloc_sm_state(owner
, name
, sym
, state
);
438 FOR_EACH_PTR(*slist
, tmp
) {
439 if (cmp_tracker(tmp
, new) < 0)
441 else if (cmp_tracker(tmp
, new) == 0) {
442 REPLACE_CURRENT_PTR(tmp
, new);
445 INSERT_CURRENT(new, tmp
);
448 } END_FOR_EACH_PTR(tmp
);
449 add_ptr_list(slist
, new);
453 void delete_state_slist(struct state_list
**slist
, int owner
, const char *name
,
458 FOR_EACH_PTR(*slist
, sm
) {
459 if (sm
->owner
== owner
&& sm
->sym
== sym
&& !strcmp(sm
->name
, name
)){
460 DELETE_CURRENT_PTR(sm
);
463 } END_FOR_EACH_PTR(sm
);
466 void delete_state_stack(struct state_list_stack
**stack
, int owner
, const char *name
,
469 struct state_list
*slist
;
471 slist
= pop_slist(stack
);
472 delete_state_slist(&slist
, owner
, name
, sym
);
473 push_slist(stack
, slist
);
476 void push_slist(struct state_list_stack
**list_stack
, struct state_list
*slist
)
478 add_ptr_list(list_stack
, slist
);
481 struct state_list
*pop_slist(struct state_list_stack
**list_stack
)
483 struct state_list
*slist
;
485 slist
= last_ptr_list((struct ptr_list
*)*list_stack
);
486 delete_ptr_list_last((struct ptr_list
**)list_stack
);
490 void free_slist(struct state_list
**slist
)
492 __free_ptr_list((struct ptr_list
**)slist
);
495 void free_stack(struct state_list_stack
**stack
)
497 __free_ptr_list((struct ptr_list
**)stack
);
500 void free_stack_and_slists(struct state_list_stack
**slist_stack
)
502 struct state_list
*slist
;
504 FOR_EACH_PTR(*slist_stack
, slist
) {
506 } END_FOR_EACH_PTR(slist
);
507 free_stack(slist_stack
);
511 * set_state_stack() sets the state for the top slist on the stack.
513 struct sm_state
*set_state_stack(struct state_list_stack
**stack
, int owner
, const char *name
,
514 struct symbol
*sym
, struct smatch_state
*state
)
516 struct state_list
*slist
;
519 slist
= pop_slist(stack
);
520 sm
= set_state_slist(&slist
, owner
, name
, sym
, state
);
521 push_slist(stack
, slist
);
527 * get_sm_state_stack() gets the state for the top slist on the stack.
529 struct sm_state
*get_sm_state_stack(struct state_list_stack
*stack
,
530 int owner
, const char *name
,
533 struct state_list
*slist
;
534 struct sm_state
*ret
;
536 slist
= pop_slist(&stack
);
537 ret
= get_sm_state_slist(slist
, owner
, name
, sym
);
538 push_slist(&stack
, slist
);
543 struct smatch_state
*get_state_stack(struct state_list_stack
*stack
,
544 int owner
, const char *name
,
549 sm
= get_sm_state_stack(stack
, owner
, name
, sym
);
555 static void match_states(struct state_list
**one
, struct state_list
**two
)
557 struct sm_state
*one_sm
;
558 struct sm_state
*two_sm
;
559 struct sm_state
*tmp
;
560 struct smatch_state
*tmp_state
;
561 struct state_list
*add_to_one
= NULL
;
562 struct state_list
*add_to_two
= NULL
;
564 PREPARE_PTR_LIST(*one
, one_sm
);
565 PREPARE_PTR_LIST(*two
, two_sm
);
567 if (!one_sm
&& !two_sm
)
569 if (cmp_tracker(one_sm
, two_sm
) < 0) {
570 tmp_state
= __client_unmatched_state_function(one_sm
);
571 tmp
= alloc_state_no_name(one_sm
->owner
, one_sm
->name
,
572 one_sm
->sym
, tmp_state
);
573 add_ptr_list(&add_to_two
, tmp
);
574 NEXT_PTR_LIST(one_sm
);
575 } else if (cmp_tracker(one_sm
, two_sm
) == 0) {
576 NEXT_PTR_LIST(one_sm
);
577 NEXT_PTR_LIST(two_sm
);
579 tmp_state
= __client_unmatched_state_function(two_sm
);
580 tmp
= alloc_state_no_name(two_sm
->owner
, two_sm
->name
,
581 two_sm
->sym
, tmp_state
);
582 add_ptr_list(&add_to_one
, tmp
);
583 NEXT_PTR_LIST(two_sm
);
586 FINISH_PTR_LIST(two_sm
);
587 FINISH_PTR_LIST(one_sm
);
589 overwrite_slist(add_to_one
, one
);
590 overwrite_slist(add_to_two
, two
);
593 static void clone_pool_havers(struct state_list
*slist
)
596 struct sm_state
*new;
598 FOR_EACH_PTR(slist
, sm
) {
601 REPLACE_CURRENT_PTR(sm
, new);
603 } END_FOR_EACH_PTR(sm
);
607 * merge_slist() is called whenever paths merge, such as after
608 * an if statement. It takes the two slists and creates one.
610 void merge_slist(struct state_list
**to
, struct state_list
*slist
)
612 struct sm_state
*one_sm
, *two_sm
, *tmp
;
613 struct state_list
*results
= NULL
;
614 struct state_list
*implied_one
= NULL
;
615 struct state_list
*implied_two
= NULL
;
620 /* merging a null and nonnull path gives you only the nonnull path */
625 *to
= clone_slist(slist
);
629 implied_one
= clone_slist(*to
);
630 implied_two
= clone_slist(slist
);
632 match_states(&implied_one
, &implied_two
);
634 clone_pool_havers(implied_one
);
635 clone_pool_havers(implied_two
);
637 PREPARE_PTR_LIST(implied_one
, one_sm
);
638 PREPARE_PTR_LIST(implied_two
, two_sm
);
640 if (!one_sm
&& !two_sm
)
642 if (cmp_tracker(one_sm
, two_sm
) < 0) {
643 sm_msg("error: Internal smatch error.");
644 NEXT_PTR_LIST(one_sm
);
645 } else if (cmp_tracker(one_sm
, two_sm
) == 0) {
646 if (one_sm
!= two_sm
) {
647 one_sm
->my_pool
= implied_one
;
648 two_sm
->my_pool
= implied_two
;
651 tmp
= merge_sm_states(one_sm
, two_sm
);
652 add_ptr_list(&results
, tmp
);
653 NEXT_PTR_LIST(one_sm
);
654 NEXT_PTR_LIST(two_sm
);
656 sm_msg("error: Internal smatch error.");
657 NEXT_PTR_LIST(two_sm
);
660 FINISH_PTR_LIST(two_sm
);
661 FINISH_PTR_LIST(one_sm
);
668 * filter_slist() removes any sm states "slist" holds in common with "filter"
670 void filter_slist(struct state_list
**slist
, struct state_list
*filter
)
672 struct sm_state
*one_sm
, *two_sm
;
673 struct state_list
*results
= NULL
;
675 PREPARE_PTR_LIST(*slist
, one_sm
);
676 PREPARE_PTR_LIST(filter
, two_sm
);
678 if (!one_sm
&& !two_sm
)
680 if (cmp_tracker(one_sm
, two_sm
) < 0) {
681 add_ptr_list(&results
, one_sm
);
682 NEXT_PTR_LIST(one_sm
);
683 } else if (cmp_tracker(one_sm
, two_sm
) == 0) {
684 if (one_sm
!= two_sm
)
685 add_ptr_list(&results
, one_sm
);
686 NEXT_PTR_LIST(one_sm
);
687 NEXT_PTR_LIST(two_sm
);
689 NEXT_PTR_LIST(two_sm
);
692 FINISH_PTR_LIST(two_sm
);
693 FINISH_PTR_LIST(one_sm
);
700 * and_slist_stack() pops the top two slists, overwriting the one with
701 * the other and pushing it back on the stack.
703 void and_slist_stack(struct state_list_stack
**slist_stack
)
705 struct sm_state
*tmp
;
706 struct state_list
*right_slist
= pop_slist(slist_stack
);
708 FOR_EACH_PTR(right_slist
, tmp
) {
709 overwrite_sm_state_stack(slist_stack
, tmp
);
710 } END_FOR_EACH_PTR(tmp
);
711 free_slist(&right_slist
);
715 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
716 * It pops the two slists from the top of the stack and merges them
717 * together in a way that preserves the things they have in common
718 * but creates a merged state for most of the rest.
719 * You could have code that had: if (foo || foo) { foo->baz;
720 * It's this function which ensures smatch does the right thing.
722 void or_slist_stack(struct state_list_stack
**pre_conds
,
723 struct state_list
*cur_slist
,
724 struct state_list_stack
**slist_stack
)
726 struct state_list
*new;
727 struct state_list
*old
;
728 struct state_list
*pre_slist
;
729 struct state_list
*res
;
730 struct state_list
*tmp_slist
;
732 new = pop_slist(slist_stack
);
733 old
= pop_slist(slist_stack
);
735 pre_slist
= pop_slist(pre_conds
);
736 push_slist(pre_conds
, clone_slist(pre_slist
));
738 res
= clone_slist(pre_slist
);
739 overwrite_slist(old
, &res
);
741 tmp_slist
= clone_slist(cur_slist
);
742 overwrite_slist(new, &tmp_slist
);
744 merge_slist(&res
, tmp_slist
);
745 filter_slist(&res
, pre_slist
);
747 push_slist(slist_stack
, res
);
748 free_slist(&tmp_slist
);
749 free_slist(&pre_slist
);
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
);