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 char *show_sm(struct sm_state
*sm
)
31 pos
= snprintf(buf
, sizeof(buf
), "[%s] '%s' = %s (",
32 check_name(sm
->owner
), sm
->name
, show_state(sm
->state
));
33 if (pos
> sizeof(buf
))
37 FOR_EACH_PTR(sm
->possible
, tmp
) {
39 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, ", ");
40 if (pos
> sizeof(buf
))
42 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, "%s",
43 show_state(tmp
->state
));
44 if (pos
> sizeof(buf
))
46 } END_FOR_EACH_PTR(tmp
);
47 snprintf(buf
+ pos
, sizeof(buf
) - pos
, ")");
52 for (i
= 0; i
< 3; i
++)
53 buf
[sizeof(buf
) - 2 - i
] = '.';
57 void __print_slist(struct state_list
*slist
)
61 printf("dumping slist at %d\n", get_lineno());
62 FOR_EACH_PTR(slist
, sm
) {
63 printf("%s\n", show_sm(sm
));
64 } END_FOR_EACH_PTR(sm
);
68 /* NULL states go at the end to simplify merge_slist */
69 int cmp_tracker(const struct sm_state
*a
, const struct sm_state
*b
)
80 if (a
->owner
> b
->owner
)
82 if (a
->owner
< b
->owner
)
85 ret
= strcmp(a
->name
, b
->name
);
89 if (!b
->sym
&& a
->sym
)
91 if (!a
->sym
&& b
->sym
)
101 static int cmp_sm_states(const struct sm_state
*a
, const struct sm_state
*b
)
105 ret
= cmp_tracker(a
, b
);
109 /* todo: add hook for smatch_extra.c */
110 if (a
->state
> b
->state
)
112 if (a
->state
< b
->state
)
117 static struct sm_state
*alloc_sm_state(int owner
, const char *name
,
118 struct symbol
*sym
, struct smatch_state
*state
)
120 struct sm_state
*sm_state
= __alloc_sm_state(0);
124 sm_state
->name
= alloc_sname(name
);
125 sm_state
->owner
= owner
;
127 sm_state
->state
= state
;
128 sm_state
->line
= get_lineno();
129 sm_state
->merged
= 0;
130 sm_state
->implied
= 0;
131 sm_state
->pool
= NULL
;
132 sm_state
->left
= NULL
;
133 sm_state
->right
= NULL
;
134 sm_state
->nr_children
= 1;
135 sm_state
->possible
= NULL
;
136 add_ptr_list(&sm_state
->possible
, sm_state
);
140 static struct sm_state
*alloc_state_no_name(int owner
, const char *name
,
142 struct smatch_state
*state
)
144 struct sm_state
*tmp
;
146 tmp
= alloc_sm_state(owner
, NULL
, sym
, state
);
151 void add_sm_state_slist(struct state_list
**slist
, struct sm_state
*new)
153 struct sm_state
*tmp
;
155 FOR_EACH_PTR(*slist
, tmp
) {
156 if (cmp_sm_states(tmp
, new) < 0)
158 else if (cmp_sm_states(tmp
, new) == 0) {
161 INSERT_CURRENT(new, tmp
);
164 } END_FOR_EACH_PTR(tmp
);
165 add_ptr_list(slist
, new);
168 static void copy_possibles(struct sm_state
*to
, struct sm_state
*from
)
170 struct sm_state
*tmp
;
171 struct sm_state
*tmp2
;
173 FOR_EACH_PTR(from
->possible
, tmp
) {
174 tmp2
= alloc_state_no_name(tmp
->owner
, tmp
->name
, tmp
->sym
,
176 tmp2
->line
= tmp
->line
;
177 add_sm_state_slist(&to
->possible
, tmp2
);
178 } END_FOR_EACH_PTR(tmp
);
181 char *alloc_sname(const char *str
)
187 tmp
= __alloc_sname(strlen(str
) + 1);
195 * I decided to use 50M here based on trial and error.
196 * It works out OK for the kernel and so it should work
197 * for most other projects as well.
199 if (sm_state_counter
* sizeof(struct sm_state
) >= 50000000)
204 static void free_sm_state(struct sm_state
*sm
)
206 free_slist(&sm
->possible
);
208 * fixme. Free the actual state.
209 * Right now we leave it until the end of the function
210 * because we don't want to double free it.
211 * Use the freelist to not double free things
215 static void free_all_sm_states(struct allocation_blob
*blob
)
217 unsigned int size
= sizeof(struct sm_state
);
218 unsigned int offset
= 0;
220 while (offset
< blob
->offset
) {
221 free_sm_state((struct sm_state
*)(blob
->data
+ offset
));
226 /* At the end of every function we free all the sm_states */
227 void free_every_single_sm_state(void)
229 struct allocator_struct
*desc
= &sm_state_allocator
;
230 struct allocation_blob
*blob
= desc
->blobs
;
233 desc
->allocations
= 0;
234 desc
->total_bytes
= 0;
235 desc
->useful_bytes
= 0;
236 desc
->freelist
= NULL
;
238 struct allocation_blob
*next
= blob
->next
;
239 free_all_sm_states(blob
);
240 blob_free(blob
, desc
->chunking
);
245 sm_state_counter
= 0;
248 struct sm_state
*clone_sm(struct sm_state
*s
)
250 struct sm_state
*ret
;
252 ret
= alloc_state_no_name(s
->owner
, s
->name
, s
->sym
, s
->state
);
253 ret
->merged
= s
->merged
;
254 ret
->implied
= s
->implied
;
256 /* clone_sm() doesn't copy the pools. Each state needs to have
258 ret
->possible
= clone_slist(s
->possible
);
260 ret
->right
= s
->right
;
261 ret
->nr_children
= s
->nr_children
;
265 int is_merged(struct sm_state
*sm
)
270 int is_implied(struct sm_state
*sm
)
275 int slist_has_state(struct state_list
*slist
, struct smatch_state
*state
)
277 struct sm_state
*tmp
;
279 FOR_EACH_PTR(slist
, tmp
) {
280 if (tmp
->state
== state
)
282 } END_FOR_EACH_PTR(tmp
);
286 static void check_order(struct state_list
*slist
)
290 struct sm_state
*last
= NULL
;
293 FOR_EACH_PTR(slist
, sm
) {
294 if (last
&& cmp_tracker(sm
, last
) <= 0) {
295 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
296 "%s vs %s\n", last
->owner
, sm
->owner
,
297 last
->sym
, sm
->sym
, last
->name
, sm
->name
);
301 } END_FOR_EACH_PTR(sm
);
308 struct state_list
*clone_slist(struct state_list
*from_slist
)
311 struct state_list
*to_slist
= NULL
;
313 FOR_EACH_PTR(from_slist
, sm
) {
314 add_ptr_list(&to_slist
, sm
);
315 } END_FOR_EACH_PTR(sm
);
316 check_order(to_slist
);
320 struct state_list_stack
*clone_stack(struct state_list_stack
*from_stack
)
322 struct state_list
*slist
;
323 struct state_list_stack
*to_stack
= NULL
;
325 FOR_EACH_PTR(from_stack
, slist
) {
326 push_slist(&to_stack
, slist
);
327 } END_FOR_EACH_PTR(slist
);
331 struct smatch_state
*merge_states(int owner
, const char *name
,
333 struct smatch_state
*state1
,
334 struct smatch_state
*state2
)
336 struct smatch_state
*ret
;
338 if (state1
== state2
)
340 else if (__has_merge_function(owner
))
341 ret
= __client_merge_function(owner
, name
, sym
, state1
, state2
);
342 else if (!state1
|| !state2
)
350 * add_pool() adds a slist to ->pools. If the slist has already been
351 * added earlier then it doesn't get added a second time.
353 void add_pool(struct state_list_stack
**pools
, struct state_list
*new)
355 struct state_list
*tmp
;
357 FOR_EACH_PTR(*pools
, tmp
) {
360 else if (tmp
== new) {
363 INSERT_CURRENT(new, tmp
);
366 } END_FOR_EACH_PTR(tmp
);
367 add_ptr_list(pools
, new);
370 struct sm_state
*merge_sm_states(struct sm_state
*one
, struct sm_state
*two
)
372 struct smatch_state
*s
;
373 struct sm_state
*result
;
377 s
= merge_states(one
->owner
, one
->name
, one
->sym
, one
->state
, two
->state
);
378 result
= alloc_state_no_name(one
->owner
, one
->name
, one
->sym
, s
);
382 result
->nr_children
= one
->nr_children
+ two
->nr_children
;
383 copy_possibles(result
, one
);
384 copy_possibles(result
, two
);
387 struct sm_state
*tmp
;
390 printf("%d merge name='%s' [%s] %s(L %d) + %s(L %d) => %s (",
391 get_lineno(), one
->name
, check_name(one
->owner
),
392 show_state(one
->state
), one
->line
,
393 show_state(two
->state
), two
->line
,
396 FOR_EACH_PTR(result
->possible
, tmp
) {
399 printf("%s", show_state(tmp
->state
));
400 } END_FOR_EACH_PTR(tmp
);
407 struct sm_state
*get_sm_state_slist(struct state_list
*slist
, int owner
, const char *name
,
415 FOR_EACH_PTR(slist
, sm
) {
416 if (sm
->owner
== owner
&& sm
->sym
== sym
&& !strcmp(sm
->name
, name
))
418 } END_FOR_EACH_PTR(sm
);
422 struct smatch_state
*get_state_slist(struct state_list
*slist
,
423 int owner
, const char *name
,
428 sm
= get_sm_state_slist(slist
, owner
, name
, sym
);
434 void overwrite_sm_state(struct state_list
**slist
, struct sm_state
*new)
436 struct sm_state
*tmp
;
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);
452 void overwrite_sm_state_stack(struct state_list_stack
**stack
,
455 struct state_list
*slist
;
457 slist
= pop_slist(stack
);
458 overwrite_sm_state(&slist
, sm
);
459 push_slist(stack
, slist
);
462 struct sm_state
*set_state_slist(struct state_list
**slist
, int owner
, const char *name
,
463 struct symbol
*sym
, struct smatch_state
*state
)
465 struct sm_state
*tmp
;
466 struct sm_state
*new = alloc_sm_state(owner
, name
, sym
, state
);
468 FOR_EACH_PTR(*slist
, tmp
) {
469 if (cmp_tracker(tmp
, new) < 0)
471 else if (cmp_tracker(tmp
, new) == 0) {
472 REPLACE_CURRENT_PTR(tmp
, new);
475 INSERT_CURRENT(new, tmp
);
478 } END_FOR_EACH_PTR(tmp
);
479 add_ptr_list(slist
, new);
483 void delete_state_slist(struct state_list
**slist
, int owner
, const char *name
,
488 FOR_EACH_PTR(*slist
, sm
) {
489 if (sm
->owner
== owner
&& sm
->sym
== sym
&& !strcmp(sm
->name
, name
)) {
490 DELETE_CURRENT_PTR(sm
);
493 } END_FOR_EACH_PTR(sm
);
496 void delete_state_stack(struct state_list_stack
**stack
, int owner
, const char *name
,
499 struct state_list
*slist
;
501 slist
= pop_slist(stack
);
502 delete_state_slist(&slist
, owner
, name
, sym
);
503 push_slist(stack
, slist
);
506 void push_slist(struct state_list_stack
**list_stack
, struct state_list
*slist
)
508 add_ptr_list(list_stack
, slist
);
511 struct state_list
*pop_slist(struct state_list_stack
**list_stack
)
513 struct state_list
*slist
;
515 slist
= last_ptr_list((struct ptr_list
*)*list_stack
);
516 delete_ptr_list_last((struct ptr_list
**)list_stack
);
520 void free_slist(struct state_list
**slist
)
522 __free_ptr_list((struct ptr_list
**)slist
);
525 void free_stack(struct state_list_stack
**stack
)
527 __free_ptr_list((struct ptr_list
**)stack
);
530 void free_stack_and_slists(struct state_list_stack
**slist_stack
)
532 struct state_list
*slist
;
534 FOR_EACH_PTR(*slist_stack
, slist
) {
536 } END_FOR_EACH_PTR(slist
);
537 free_stack(slist_stack
);
541 * set_state_stack() sets the state for the top slist on the stack.
543 struct sm_state
*set_state_stack(struct state_list_stack
**stack
, int owner
, const char *name
,
544 struct symbol
*sym
, struct smatch_state
*state
)
546 struct state_list
*slist
;
549 slist
= pop_slist(stack
);
550 sm
= set_state_slist(&slist
, owner
, name
, sym
, state
);
551 push_slist(stack
, slist
);
557 * get_sm_state_stack() gets the state for the top slist on the stack.
559 struct sm_state
*get_sm_state_stack(struct state_list_stack
*stack
,
560 int owner
, const char *name
,
563 struct state_list
*slist
;
564 struct sm_state
*ret
;
566 slist
= pop_slist(&stack
);
567 ret
= get_sm_state_slist(slist
, owner
, name
, sym
);
568 push_slist(&stack
, slist
);
572 struct smatch_state
*get_state_stack(struct state_list_stack
*stack
,
573 int owner
, const char *name
,
578 sm
= get_sm_state_stack(stack
, owner
, name
, sym
);
584 static void match_states(struct state_list
**one
, struct state_list
**two
)
586 struct sm_state
*one_sm
;
587 struct sm_state
*two_sm
;
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_sm
);
594 PREPARE_PTR_LIST(*two
, two_sm
);
596 if (!one_sm
&& !two_sm
)
598 if (cmp_tracker(one_sm
, two_sm
) < 0) {
599 tmp_state
= __client_unmatched_state_function(one_sm
);
600 tmp
= alloc_state_no_name(one_sm
->owner
, one_sm
->name
,
601 one_sm
->sym
, tmp_state
);
602 add_ptr_list(&add_to_two
, tmp
);
603 NEXT_PTR_LIST(one_sm
);
604 } else if (cmp_tracker(one_sm
, two_sm
) == 0) {
605 NEXT_PTR_LIST(one_sm
);
606 NEXT_PTR_LIST(two_sm
);
608 tmp_state
= __client_unmatched_state_function(two_sm
);
609 tmp
= alloc_state_no_name(two_sm
->owner
, two_sm
->name
,
610 two_sm
->sym
, tmp_state
);
611 add_ptr_list(&add_to_one
, tmp
);
612 NEXT_PTR_LIST(two_sm
);
615 FINISH_PTR_LIST(two_sm
);
616 FINISH_PTR_LIST(one_sm
);
618 overwrite_slist(add_to_one
, one
);
619 overwrite_slist(add_to_two
, two
);
622 static void clone_pool_havers(struct state_list
*slist
)
625 struct sm_state
*new;
627 FOR_EACH_PTR(slist
, sm
) {
630 REPLACE_CURRENT_PTR(sm
, new);
632 } END_FOR_EACH_PTR(sm
);
636 * merge_slist() is called whenever paths merge, such as after
637 * an if statement. It takes the two slists and creates one.
639 void merge_slist(struct state_list
**to
, struct state_list
*slist
)
641 struct sm_state
*one_sm
, *two_sm
, *tmp
;
642 struct state_list
*results
= NULL
;
643 struct state_list
*implied_one
= NULL
;
644 struct state_list
*implied_two
= NULL
;
652 /* merging a null and nonnull path gives you only the nonnull path */
657 *to
= clone_slist(slist
);
661 implied_one
= clone_slist(*to
);
662 implied_two
= clone_slist(slist
);
664 match_states(&implied_one
, &implied_two
);
666 clone_pool_havers(implied_one
);
667 clone_pool_havers(implied_two
);
669 PREPARE_PTR_LIST(implied_one
, one_sm
);
670 PREPARE_PTR_LIST(implied_two
, two_sm
);
672 if (!one_sm
&& !two_sm
)
674 if (cmp_tracker(one_sm
, two_sm
) < 0) {
675 sm_msg("error: Internal smatch error.");
676 NEXT_PTR_LIST(one_sm
);
677 } else if (cmp_tracker(one_sm
, two_sm
) == 0) {
678 if (one_sm
!= two_sm
) {
679 one_sm
->pool
= implied_one
;
680 two_sm
->pool
= implied_two
;
683 tmp
= merge_sm_states(one_sm
, two_sm
);
684 add_ptr_list(&results
, tmp
);
685 NEXT_PTR_LIST(one_sm
);
686 NEXT_PTR_LIST(two_sm
);
688 sm_msg("error: Internal smatch error.");
689 NEXT_PTR_LIST(two_sm
);
692 FINISH_PTR_LIST(two_sm
);
693 FINISH_PTR_LIST(one_sm
);
700 * filter_slist() removes any sm states "slist" holds in common with "filter"
702 void filter_slist(struct state_list
**slist
, struct state_list
*filter
)
704 struct sm_state
*one_sm
, *two_sm
;
705 struct state_list
*results
= NULL
;
707 PREPARE_PTR_LIST(*slist
, one_sm
);
708 PREPARE_PTR_LIST(filter
, two_sm
);
710 if (!one_sm
&& !two_sm
)
712 if (cmp_tracker(one_sm
, two_sm
) < 0) {
713 add_ptr_list(&results
, one_sm
);
714 NEXT_PTR_LIST(one_sm
);
715 } else if (cmp_tracker(one_sm
, two_sm
) == 0) {
716 if (one_sm
!= two_sm
)
717 add_ptr_list(&results
, one_sm
);
718 NEXT_PTR_LIST(one_sm
);
719 NEXT_PTR_LIST(two_sm
);
721 NEXT_PTR_LIST(two_sm
);
724 FINISH_PTR_LIST(two_sm
);
725 FINISH_PTR_LIST(one_sm
);
732 * and_slist_stack() pops the top two slists, overwriting the one with
733 * the other and pushing it back on the stack.
735 void and_slist_stack(struct state_list_stack
**slist_stack
)
737 struct sm_state
*tmp
;
738 struct state_list
*right_slist
= pop_slist(slist_stack
);
740 FOR_EACH_PTR(right_slist
, tmp
) {
741 overwrite_sm_state_stack(slist_stack
, tmp
);
742 } END_FOR_EACH_PTR(tmp
);
743 free_slist(&right_slist
);
747 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
748 * It pops the two slists from the top of the stack and merges them
749 * together in a way that preserves the things they have in common
750 * but creates a merged state for most of the rest.
751 * You could have code that had: if (foo || foo) { foo->baz;
752 * It's this function which ensures smatch does the right thing.
754 void or_slist_stack(struct state_list_stack
**pre_conds
,
755 struct state_list
*cur_slist
,
756 struct state_list_stack
**slist_stack
)
758 struct state_list
*new;
759 struct state_list
*old
;
760 struct state_list
*pre_slist
;
761 struct state_list
*res
;
762 struct state_list
*tmp_slist
;
764 new = pop_slist(slist_stack
);
765 old
= pop_slist(slist_stack
);
767 pre_slist
= pop_slist(pre_conds
);
768 push_slist(pre_conds
, clone_slist(pre_slist
));
770 res
= clone_slist(pre_slist
);
771 overwrite_slist(old
, &res
);
773 tmp_slist
= clone_slist(cur_slist
);
774 overwrite_slist(new, &tmp_slist
);
776 merge_slist(&res
, tmp_slist
);
777 filter_slist(&res
, pre_slist
);
779 push_slist(slist_stack
, res
);
780 free_slist(&tmp_slist
);
781 free_slist(&pre_slist
);
787 * get_slist_from_named_stack() is only used for gotos.
789 struct state_list
**get_slist_from_named_stack(struct named_stack
*stack
,
792 struct named_slist
*tmp
;
794 FOR_EACH_PTR(stack
, tmp
) {
795 if (!strcmp(tmp
->name
, name
))
797 } END_FOR_EACH_PTR(tmp
);
801 void overwrite_slist(struct state_list
*from
, struct state_list
**to
)
803 struct sm_state
*tmp
;
805 FOR_EACH_PTR(from
, tmp
) {
806 overwrite_sm_state(to
, tmp
);
807 } END_FOR_EACH_PTR(tmp
);