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
;
172 FOR_EACH_PTR(from
->possible
, tmp
) {
173 add_sm_state_slist(&to
->possible
, tmp
);
174 } END_FOR_EACH_PTR(tmp
);
177 char *alloc_sname(const char *str
)
183 tmp
= __alloc_sname(strlen(str
) + 1);
191 * I decided to use 50M here based on trial and error.
192 * It works out OK for the kernel and so it should work
193 * for most other projects as well.
195 if (sm_state_counter
* sizeof(struct sm_state
) >= 50000000)
200 static void free_sm_state(struct sm_state
*sm
)
202 free_slist(&sm
->possible
);
204 * fixme. Free the actual state.
205 * Right now we leave it until the end of the function
206 * because we don't want to double free it.
207 * Use the freelist to not double free things
211 static void free_all_sm_states(struct allocation_blob
*blob
)
213 unsigned int size
= sizeof(struct sm_state
);
214 unsigned int offset
= 0;
216 while (offset
< blob
->offset
) {
217 free_sm_state((struct sm_state
*)(blob
->data
+ offset
));
222 /* At the end of every function we free all the sm_states */
223 void free_every_single_sm_state(void)
225 struct allocator_struct
*desc
= &sm_state_allocator
;
226 struct allocation_blob
*blob
= desc
->blobs
;
229 desc
->allocations
= 0;
230 desc
->total_bytes
= 0;
231 desc
->useful_bytes
= 0;
232 desc
->freelist
= NULL
;
234 struct allocation_blob
*next
= blob
->next
;
235 free_all_sm_states(blob
);
236 blob_free(blob
, desc
->chunking
);
241 sm_state_counter
= 0;
244 struct sm_state
*clone_sm(struct sm_state
*s
)
246 struct sm_state
*ret
;
248 ret
= alloc_state_no_name(s
->owner
, s
->name
, s
->sym
, s
->state
);
249 ret
->merged
= s
->merged
;
250 ret
->implied
= s
->implied
;
252 /* clone_sm() doesn't copy the pools. Each state needs to have
254 ret
->possible
= clone_slist(s
->possible
);
256 ret
->right
= s
->right
;
257 ret
->nr_children
= s
->nr_children
;
261 int is_merged(struct sm_state
*sm
)
266 int is_implied(struct sm_state
*sm
)
271 int slist_has_state(struct state_list
*slist
, struct smatch_state
*state
)
273 struct sm_state
*tmp
;
275 FOR_EACH_PTR(slist
, tmp
) {
276 if (tmp
->state
== state
)
278 } END_FOR_EACH_PTR(tmp
);
282 static void check_order(struct state_list
*slist
)
286 struct sm_state
*last
= NULL
;
289 FOR_EACH_PTR(slist
, sm
) {
290 if (last
&& cmp_tracker(sm
, last
) <= 0) {
291 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
292 "%s vs %s\n", last
->owner
, sm
->owner
,
293 last
->sym
, sm
->sym
, last
->name
, sm
->name
);
297 } END_FOR_EACH_PTR(sm
);
304 struct state_list
*clone_slist(struct state_list
*from_slist
)
307 struct state_list
*to_slist
= NULL
;
309 FOR_EACH_PTR(from_slist
, sm
) {
310 add_ptr_list(&to_slist
, sm
);
311 } END_FOR_EACH_PTR(sm
);
312 check_order(to_slist
);
316 struct state_list_stack
*clone_stack(struct state_list_stack
*from_stack
)
318 struct state_list
*slist
;
319 struct state_list_stack
*to_stack
= NULL
;
321 FOR_EACH_PTR(from_stack
, slist
) {
322 push_slist(&to_stack
, slist
);
323 } END_FOR_EACH_PTR(slist
);
327 struct smatch_state
*merge_states(int owner
, const char *name
,
329 struct smatch_state
*state1
,
330 struct smatch_state
*state2
)
332 struct smatch_state
*ret
;
334 if (state1
== state2
)
336 else if (__has_merge_function(owner
))
337 ret
= __client_merge_function(owner
, state1
, state2
);
338 else if (!state1
|| !state2
)
345 struct sm_state
*merge_sm_states(struct sm_state
*one
, struct sm_state
*two
)
347 struct smatch_state
*s
;
348 struct sm_state
*result
;
352 s
= merge_states(one
->owner
, one
->name
, one
->sym
, one
->state
, two
->state
);
353 result
= alloc_state_no_name(one
->owner
, one
->name
, one
->sym
, s
);
357 result
->nr_children
= one
->nr_children
+ two
->nr_children
;
358 copy_possibles(result
, one
);
359 copy_possibles(result
, two
);
362 struct sm_state
*tmp
;
365 printf("%d merge [%s] '%s' %s(L %d) + %s(L %d) => %s (",
366 get_lineno(), check_name(one
->owner
), one
->name
,
367 show_state(one
->state
), one
->line
,
368 show_state(two
->state
), two
->line
,
371 FOR_EACH_PTR(result
->possible
, tmp
) {
374 printf("%s", show_state(tmp
->state
));
375 } END_FOR_EACH_PTR(tmp
);
382 struct sm_state
*get_sm_state_slist(struct state_list
*slist
, int owner
, const char *name
,
390 FOR_EACH_PTR(slist
, sm
) {
391 if (sm
->owner
== owner
&& sm
->sym
== sym
&& !strcmp(sm
->name
, name
))
393 } END_FOR_EACH_PTR(sm
);
397 struct smatch_state
*get_state_slist(struct state_list
*slist
,
398 int owner
, const char *name
,
403 sm
= get_sm_state_slist(slist
, owner
, name
, sym
);
409 void overwrite_sm_state(struct state_list
**slist
, struct sm_state
*new)
411 struct sm_state
*tmp
;
413 FOR_EACH_PTR(*slist
, tmp
) {
414 if (cmp_tracker(tmp
, new) < 0)
416 else if (cmp_tracker(tmp
, new) == 0) {
417 REPLACE_CURRENT_PTR(tmp
, new);
420 INSERT_CURRENT(new, tmp
);
423 } END_FOR_EACH_PTR(tmp
);
424 add_ptr_list(slist
, new);
427 void overwrite_sm_state_stack(struct state_list_stack
**stack
,
430 struct state_list
*slist
;
432 slist
= pop_slist(stack
);
433 overwrite_sm_state(&slist
, sm
);
434 push_slist(stack
, slist
);
437 struct sm_state
*set_state_slist(struct state_list
**slist
, int owner
, const char *name
,
438 struct symbol
*sym
, struct smatch_state
*state
)
440 struct sm_state
*tmp
;
441 struct sm_state
*new = alloc_sm_state(owner
, name
, sym
, state
);
443 FOR_EACH_PTR(*slist
, tmp
) {
444 if (cmp_tracker(tmp
, new) < 0)
446 else if (cmp_tracker(tmp
, new) == 0) {
447 REPLACE_CURRENT_PTR(tmp
, new);
450 INSERT_CURRENT(new, tmp
);
453 } END_FOR_EACH_PTR(tmp
);
454 add_ptr_list(slist
, new);
458 void delete_state_slist(struct state_list
**slist
, int owner
, const char *name
,
463 FOR_EACH_PTR(*slist
, sm
) {
464 if (sm
->owner
== owner
&& sm
->sym
== sym
&& !strcmp(sm
->name
, name
)) {
465 DELETE_CURRENT_PTR(sm
);
468 } END_FOR_EACH_PTR(sm
);
471 void delete_state_stack(struct state_list_stack
**stack
, int owner
, const char *name
,
474 struct state_list
*slist
;
476 slist
= pop_slist(stack
);
477 delete_state_slist(&slist
, owner
, name
, sym
);
478 push_slist(stack
, slist
);
481 void push_slist(struct state_list_stack
**list_stack
, struct state_list
*slist
)
483 add_ptr_list(list_stack
, slist
);
486 struct state_list
*pop_slist(struct state_list_stack
**list_stack
)
488 struct state_list
*slist
;
490 slist
= last_ptr_list((struct ptr_list
*)*list_stack
);
491 delete_ptr_list_last((struct ptr_list
**)list_stack
);
495 void free_slist(struct state_list
**slist
)
497 __free_ptr_list((struct ptr_list
**)slist
);
500 void free_stack(struct state_list_stack
**stack
)
502 __free_ptr_list((struct ptr_list
**)stack
);
505 void free_stack_and_slists(struct state_list_stack
**slist_stack
)
507 struct state_list
*slist
;
509 FOR_EACH_PTR(*slist_stack
, slist
) {
511 } END_FOR_EACH_PTR(slist
);
512 free_stack(slist_stack
);
516 * set_state_stack() sets the state for the top slist on the stack.
518 struct sm_state
*set_state_stack(struct state_list_stack
**stack
, int owner
, const char *name
,
519 struct symbol
*sym
, struct smatch_state
*state
)
521 struct state_list
*slist
;
524 slist
= pop_slist(stack
);
525 sm
= set_state_slist(&slist
, owner
, name
, sym
, state
);
526 push_slist(stack
, slist
);
532 * get_sm_state_stack() gets the state for the top slist on the stack.
534 struct sm_state
*get_sm_state_stack(struct state_list_stack
*stack
,
535 int owner
, const char *name
,
538 struct state_list
*slist
;
539 struct sm_state
*ret
;
541 slist
= pop_slist(&stack
);
542 ret
= get_sm_state_slist(slist
, owner
, name
, sym
);
543 push_slist(&stack
, slist
);
547 struct smatch_state
*get_state_stack(struct state_list_stack
*stack
,
548 int owner
, const char *name
,
553 sm
= get_sm_state_stack(stack
, owner
, name
, sym
);
559 static void match_states(struct state_list
**one
, struct state_list
**two
)
561 struct sm_state
*one_sm
;
562 struct sm_state
*two_sm
;
563 struct sm_state
*tmp
;
564 struct smatch_state
*tmp_state
;
565 struct state_list
*add_to_one
= NULL
;
566 struct state_list
*add_to_two
= NULL
;
568 PREPARE_PTR_LIST(*one
, one_sm
);
569 PREPARE_PTR_LIST(*two
, two_sm
);
571 if (!one_sm
&& !two_sm
)
573 if (cmp_tracker(one_sm
, two_sm
) < 0) {
574 tmp_state
= __client_unmatched_state_function(one_sm
);
575 tmp
= alloc_state_no_name(one_sm
->owner
, one_sm
->name
,
576 one_sm
->sym
, tmp_state
);
577 add_ptr_list(&add_to_two
, tmp
);
578 NEXT_PTR_LIST(one_sm
);
579 } else if (cmp_tracker(one_sm
, two_sm
) == 0) {
580 NEXT_PTR_LIST(one_sm
);
581 NEXT_PTR_LIST(two_sm
);
583 tmp_state
= __client_unmatched_state_function(two_sm
);
584 tmp
= alloc_state_no_name(two_sm
->owner
, two_sm
->name
,
585 two_sm
->sym
, tmp_state
);
586 add_ptr_list(&add_to_one
, tmp
);
587 NEXT_PTR_LIST(two_sm
);
590 FINISH_PTR_LIST(two_sm
);
591 FINISH_PTR_LIST(one_sm
);
593 overwrite_slist(add_to_one
, one
);
594 overwrite_slist(add_to_two
, two
);
597 static void clone_pool_havers(struct state_list
*slist
)
600 struct sm_state
*new;
602 FOR_EACH_PTR(slist
, sm
) {
605 REPLACE_CURRENT_PTR(sm
, new);
607 } END_FOR_EACH_PTR(sm
);
612 * Sets the first state to the slist_id.
614 static void set_slist_id(struct state_list
*slist
)
616 struct smatch_state
*state
;
617 struct sm_state
*tmp
, *new;
619 state
= alloc_state_num(++__slist_id
);
620 new = alloc_sm_state(-1, "unnull_path", NULL
, state
);
622 FOR_EACH_PTR(slist
, tmp
) {
623 if (tmp
->owner
!= (unsigned short)-1)
625 REPLACE_CURRENT_PTR(tmp
, new);
627 } END_FOR_EACH_PTR(tmp
);
630 int get_slist_id(struct state_list
*slist
)
632 struct sm_state
*tmp
;
634 FOR_EACH_PTR(slist
, tmp
) {
635 if (tmp
->owner
!= (unsigned short)-1)
637 return PTR_INT(tmp
->state
->data
);
638 } END_FOR_EACH_PTR(tmp
);
643 * merge_slist() is called whenever paths merge, such as after
644 * an if statement. It takes the two slists and creates one.
646 void merge_slist(struct state_list
**to
, struct state_list
*slist
)
648 struct sm_state
*one_sm
, *two_sm
, *tmp
;
649 struct state_list
*results
= NULL
;
650 struct state_list
*implied_one
= NULL
;
651 struct state_list
*implied_two
= NULL
;
659 /* merging a null and nonnull path gives you only the nonnull path */
664 *to
= clone_slist(slist
);
668 implied_one
= clone_slist(*to
);
669 implied_two
= clone_slist(slist
);
671 match_states(&implied_one
, &implied_two
);
673 clone_pool_havers(implied_one
);
674 clone_pool_havers(implied_two
);
676 set_slist_id(implied_one
);
677 set_slist_id(implied_two
);
679 PREPARE_PTR_LIST(implied_one
, one_sm
);
680 PREPARE_PTR_LIST(implied_two
, two_sm
);
682 if (!one_sm
&& !two_sm
)
684 if (cmp_tracker(one_sm
, two_sm
) < 0) {
685 sm_msg("error: Internal smatch error.");
686 NEXT_PTR_LIST(one_sm
);
687 } else if (cmp_tracker(one_sm
, two_sm
) == 0) {
688 if (one_sm
!= two_sm
) {
689 one_sm
->pool
= implied_one
;
690 two_sm
->pool
= implied_two
;
693 tmp
= merge_sm_states(one_sm
, two_sm
);
694 add_ptr_list(&results
, tmp
);
695 NEXT_PTR_LIST(one_sm
);
696 NEXT_PTR_LIST(two_sm
);
698 sm_msg("error: Internal smatch error.");
699 NEXT_PTR_LIST(two_sm
);
702 FINISH_PTR_LIST(two_sm
);
703 FINISH_PTR_LIST(one_sm
);
710 * filter_slist() removes any sm states "slist" holds in common with "filter"
712 void filter_slist(struct state_list
**slist
, struct state_list
*filter
)
714 struct sm_state
*one_sm
, *two_sm
;
715 struct state_list
*results
= NULL
;
717 PREPARE_PTR_LIST(*slist
, one_sm
);
718 PREPARE_PTR_LIST(filter
, two_sm
);
720 if (!one_sm
&& !two_sm
)
722 if (cmp_tracker(one_sm
, two_sm
) < 0) {
723 add_ptr_list(&results
, one_sm
);
724 NEXT_PTR_LIST(one_sm
);
725 } else if (cmp_tracker(one_sm
, two_sm
) == 0) {
726 if (one_sm
!= two_sm
)
727 add_ptr_list(&results
, one_sm
);
728 NEXT_PTR_LIST(one_sm
);
729 NEXT_PTR_LIST(two_sm
);
731 NEXT_PTR_LIST(two_sm
);
734 FINISH_PTR_LIST(two_sm
);
735 FINISH_PTR_LIST(one_sm
);
742 * and_slist_stack() pops the top two slists, overwriting the one with
743 * the other and pushing it back on the stack.
745 void and_slist_stack(struct state_list_stack
**slist_stack
)
747 struct sm_state
*tmp
;
748 struct state_list
*right_slist
= pop_slist(slist_stack
);
750 FOR_EACH_PTR(right_slist
, tmp
) {
751 overwrite_sm_state_stack(slist_stack
, tmp
);
752 } END_FOR_EACH_PTR(tmp
);
753 free_slist(&right_slist
);
757 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
758 * It pops the two slists from the top of the stack and merges them
759 * together in a way that preserves the things they have in common
760 * but creates a merged state for most of the rest.
761 * You could have code that had: if (foo || foo) { foo->baz;
762 * It's this function which ensures smatch does the right thing.
764 void or_slist_stack(struct state_list_stack
**pre_conds
,
765 struct state_list
*cur_slist
,
766 struct state_list_stack
**slist_stack
)
768 struct state_list
*new;
769 struct state_list
*old
;
770 struct state_list
*pre_slist
;
771 struct state_list
*res
;
772 struct state_list
*tmp_slist
;
774 new = pop_slist(slist_stack
);
775 old
= pop_slist(slist_stack
);
777 pre_slist
= pop_slist(pre_conds
);
778 push_slist(pre_conds
, clone_slist(pre_slist
));
780 res
= clone_slist(pre_slist
);
781 overwrite_slist(old
, &res
);
783 tmp_slist
= clone_slist(cur_slist
);
784 overwrite_slist(new, &tmp_slist
);
786 merge_slist(&res
, tmp_slist
);
787 filter_slist(&res
, pre_slist
);
789 push_slist(slist_stack
, res
);
790 free_slist(&tmp_slist
);
791 free_slist(&pre_slist
);
797 * get_slist_from_named_stack() is only used for gotos.
799 struct state_list
**get_slist_from_named_stack(struct named_stack
*stack
,
802 struct named_slist
*tmp
;
804 FOR_EACH_PTR(stack
, tmp
) {
805 if (!strcmp(tmp
->name
, name
))
807 } END_FOR_EACH_PTR(tmp
);
811 void overwrite_slist(struct state_list
*from
, struct state_list
**to
)
813 struct sm_state
*tmp
;
815 FOR_EACH_PTR(from
, tmp
) {
816 overwrite_sm_state(to
, tmp
);
817 } END_FOR_EACH_PTR(tmp
);