2 * Copyright (C) 2008,2009 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
21 #include "smatch_slist.h"
25 ALLOCATOR(smatch_state
, "smatch state");
26 ALLOCATOR(sm_state
, "sm state");
27 ALLOCATOR(named_stree
, "named slist");
28 __DO_ALLOCATOR(char, 1, 4, "state names", sname
);
30 static int sm_state_counter
;
32 static struct stree_stack
*all_pools
;
34 char *show_sm(struct sm_state
*sm
)
41 pos
= snprintf(buf
, sizeof(buf
), "[%s] '%s' = '%s'",
42 check_name(sm
->owner
), sm
->name
, show_state(sm
->state
));
43 if (pos
> sizeof(buf
))
46 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
49 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " (");
50 if (pos
> sizeof(buf
))
53 FOR_EACH_PTR(sm
->possible
, tmp
) {
55 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, ", ");
56 if (pos
> sizeof(buf
))
58 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, "%s",
59 show_state(tmp
->state
));
60 if (pos
> sizeof(buf
))
62 } END_FOR_EACH_PTR(tmp
);
63 snprintf(buf
+ pos
, sizeof(buf
) - pos
, ")");
68 for (i
= 0; i
< 3; i
++)
69 buf
[sizeof(buf
) - 2 - i
] = '.';
73 void __print_stree(struct stree
*stree
)
77 printf("dumping stree at %d\n", get_lineno());
78 FOR_EACH_SM(stree
, sm
) {
79 printf("%s\n", show_sm(sm
));
80 } END_FOR_EACH_SM(sm
);
84 /* NULL states go at the end to simplify merge_slist */
85 int cmp_tracker(const struct sm_state
*a
, const struct sm_state
*b
)
96 if (a
->owner
> b
->owner
)
98 if (a
->owner
< b
->owner
)
101 ret
= strcmp(a
->name
, b
->name
);
105 if (!b
->sym
&& a
->sym
)
107 if (!a
->sym
&& b
->sym
)
117 static int cmp_sm_states(const struct sm_state
*a
, const struct sm_state
*b
, int preserve
)
121 ret
= cmp_tracker(a
, b
);
125 /* todo: add hook for smatch_extra.c */
126 if (a
->state
> b
->state
)
128 if (a
->state
< b
->state
)
130 /* This is obviously a massive disgusting hack but we need to preserve
131 * the unmerged states for smatch extra because we use them in
132 * smatch_db.c. Meanwhile if we preserve all the other unmerged states
133 * then it uses a lot of memory and we don't use it. Hence this hack.
135 * Also sometimes even just preserving every possible SMATCH_EXTRA state
136 * takes too much resources so we have to cap that. Capping is probably
137 * not often a problem in real life.
139 if (a
->owner
== SMATCH_EXTRA
&& preserve
) {
142 if (a
->merged
== 1 && b
->merged
== 0)
151 static struct sm_state
*alloc_sm_state(int owner
, const char *name
,
152 struct symbol
*sym
, struct smatch_state
*state
)
154 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
->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 struct sm_state
*alloc_state_no_name(int owner
, const char *name
,
176 struct smatch_state
*state
)
178 struct sm_state
*tmp
;
180 tmp
= alloc_sm_state(owner
, NULL
, sym
, state
);
185 int too_many_possible(struct sm_state
*sm
)
187 if (ptr_list_size((struct ptr_list
*)sm
->possible
) >= 100)
192 void add_possible_sm(struct sm_state
*to
, struct sm_state
*new)
194 struct sm_state
*tmp
;
197 if (too_many_possible(to
))
200 FOR_EACH_PTR(to
->possible
, tmp
) {
201 if (cmp_sm_states(tmp
, new, preserve
) < 0)
203 else if (cmp_sm_states(tmp
, new, preserve
) == 0) {
206 INSERT_CURRENT(new, tmp
);
209 } END_FOR_EACH_PTR(tmp
);
210 add_ptr_list(&to
->possible
, new);
213 static void copy_possibles(struct sm_state
*to
, struct sm_state
*from
)
215 struct sm_state
*tmp
;
217 FOR_EACH_PTR(from
->possible
, tmp
) {
218 add_possible_sm(to
, tmp
);
219 } END_FOR_EACH_PTR(tmp
);
222 char *alloc_sname(const char *str
)
228 tmp
= __alloc_sname(strlen(str
) + 1);
233 int out_of_memory(void)
236 * I decided to use 50M here based on trial and error.
237 * It works out OK for the kernel and so it should work
238 * for most other projects as well.
240 if (sm_state_counter
* sizeof(struct sm_state
) >= 50000000)
245 int low_on_memory(void)
247 if (sm_state_counter
* sizeof(struct sm_state
) >= 25000000)
252 static void free_sm_state(struct sm_state
*sm
)
254 free_slist(&sm
->possible
);
256 * fixme. Free the actual state.
257 * Right now we leave it until the end of the function
258 * because we don't want to double free it.
259 * Use the freelist to not double free things
263 static void free_all_sm_states(struct allocation_blob
*blob
)
265 unsigned int size
= sizeof(struct sm_state
);
266 unsigned int offset
= 0;
268 while (offset
< blob
->offset
) {
269 free_sm_state((struct sm_state
*)(blob
->data
+ offset
));
274 /* At the end of every function we free all the sm_states */
275 void free_every_single_sm_state(void)
277 struct allocator_struct
*desc
= &sm_state_allocator
;
278 struct allocation_blob
*blob
= desc
->blobs
;
281 desc
->allocations
= 0;
282 desc
->total_bytes
= 0;
283 desc
->useful_bytes
= 0;
284 desc
->freelist
= NULL
;
286 struct allocation_blob
*next
= blob
->next
;
287 free_all_sm_states(blob
);
288 blob_free(blob
, desc
->chunking
);
292 clear_smatch_state_alloc();
294 free_stack_and_strees(&all_pools
);
295 sm_state_counter
= 0;
298 struct sm_state
*clone_sm(struct sm_state
*s
)
300 struct sm_state
*ret
;
302 ret
= alloc_state_no_name(s
->owner
, s
->name
, s
->sym
, s
->state
);
303 ret
->merged
= s
->merged
;
304 ret
->implied
= s
->implied
;
306 /* clone_sm() doesn't copy the pools. Each state needs to have
308 ret
->possible
= clone_slist(s
->possible
);
310 ret
->right
= s
->right
;
311 ret
->nr_children
= s
->nr_children
;
315 int is_merged(struct sm_state
*sm
)
320 int is_implied(struct sm_state
*sm
)
325 int slist_has_state(struct state_list
*slist
, struct smatch_state
*state
)
327 struct sm_state
*tmp
;
329 FOR_EACH_PTR(slist
, tmp
) {
330 if (tmp
->state
== state
)
332 } END_FOR_EACH_PTR(tmp
);
336 struct state_list
*clone_slist(struct state_list
*from_slist
)
339 struct state_list
*to_slist
= NULL
;
341 FOR_EACH_PTR(from_slist
, sm
) {
342 add_ptr_list(&to_slist
, sm
);
343 } END_FOR_EACH_PTR(sm
);
347 struct smatch_state
*merge_states(int owner
, const char *name
,
349 struct smatch_state
*state1
,
350 struct smatch_state
*state2
)
352 struct smatch_state
*ret
;
354 if (state1
== state2
)
356 else if (__has_merge_function(owner
))
357 ret
= __client_merge_function(owner
, state1
, state2
);
358 else if (state1
== &ghost
)
360 else if (state2
== &ghost
)
362 else if (!state1
|| !state2
)
369 struct sm_state
*merge_sm_states(struct sm_state
*one
, struct sm_state
*two
)
371 struct smatch_state
*s
;
372 struct sm_state
*result
;
376 s
= merge_states(one
->owner
, one
->name
, one
->sym
, one
->state
, two
->state
);
377 result
= alloc_state_no_name(one
->owner
, one
->name
, one
->sym
, s
);
381 result
->nr_children
= one
->nr_children
+ two
->nr_children
;
382 copy_possibles(result
, one
);
383 copy_possibles(result
, two
);
386 strcmp(check_name(one
->owner
), option_debug_check
) == 0) {
387 struct sm_state
*tmp
;
390 printf("%s:%d %s() merge [%s] '%s' %s(L %d) + %s(L %d) => %s (",
391 get_filename(), get_lineno(), get_function(),
392 check_name(one
->owner
), one
->name
,
393 show_state(one
->state
), one
->line
,
394 show_state(two
->state
), two
->line
,
397 FOR_EACH_PTR(result
->possible
, tmp
) {
400 printf("%s", show_state(tmp
->state
));
401 } END_FOR_EACH_PTR(tmp
);
408 struct sm_state
*get_sm_state_stree(struct stree
*stree
, int owner
, const char *name
,
411 struct tracker tracker
= {
413 .name
= (char *)name
,
421 return avl_lookup(stree
, (struct sm_state
*)&tracker
);
424 struct smatch_state
*get_state_stree(struct stree
*stree
,
425 int owner
, const char *name
,
430 sm
= get_sm_state_stree(stree
, owner
, name
, sym
);
436 /* FIXME: this is almost exactly the same as set_sm_state_slist() */
437 void overwrite_sm_state_stree(struct stree
**stree
, struct sm_state
*new)
439 avl_insert(stree
, new);
442 void overwrite_sm_state_stree_stack(struct stree_stack
**stack
,
447 stree
= pop_stree(stack
);
448 overwrite_sm_state_stree(&stree
, sm
);
449 push_stree(stack
, stree
);
452 struct sm_state
*set_state_stree(struct stree
**stree
, int owner
, const char *name
,
453 struct symbol
*sym
, struct smatch_state
*state
)
455 struct sm_state
*new = alloc_sm_state(owner
, name
, sym
, state
);
457 avl_insert(stree
, new);
461 void set_state_stree_perm(struct stree
**stree
, int owner
, const char *name
,
462 struct symbol
*sym
, struct smatch_state
*state
)
466 sm
= malloc(sizeof(*sm
) + strlen(name
) + 1);
467 memset(sm
, 0, sizeof(*sm
));
469 sm
->name
= (char *)(sm
+ 1);
470 strcpy((char *)sm
->name
, name
);
474 overwrite_sm_state_stree(stree
, sm
);
477 void delete_state_stree(struct stree
**stree
, int owner
, const char *name
,
480 struct tracker tracker
= {
482 .name
= (char *)name
,
486 avl_remove(stree
, (struct sm_state
*)&tracker
);
489 void delete_state_stree_stack(struct stree_stack
**stack
, int owner
, const char *name
,
494 stree
= pop_stree(stack
);
495 delete_state_stree(&stree
, owner
, name
, sym
);
496 push_stree(stack
, stree
);
499 void push_stree(struct stree_stack
**stack
, struct stree
*stree
)
501 add_ptr_list(stack
, stree
);
504 struct stree
*pop_stree(struct stree_stack
**stack
)
508 stree
= last_ptr_list((struct ptr_list
*)*stack
);
509 delete_ptr_list_last((struct ptr_list
**)stack
);
513 void free_slist(struct state_list
**slist
)
515 __free_ptr_list((struct ptr_list
**)slist
);
518 void free_stree_stack(struct stree_stack
**stack
)
520 __free_ptr_list((struct ptr_list
**)stack
);
523 void free_stack_and_strees(struct stree_stack
**stree_stack
)
527 FOR_EACH_PTR(*stree_stack
, stree
) {
529 } END_FOR_EACH_PTR(stree
);
530 free_stree_stack(stree_stack
);
533 struct sm_state
*set_state_stree_stack(struct stree_stack
**stack
, int owner
, const char *name
,
534 struct symbol
*sym
, struct smatch_state
*state
)
539 stree
= pop_stree(stack
);
540 sm
= set_state_stree(&stree
, owner
, name
, sym
, state
);
541 push_stree(stack
, stree
);
547 * get_sm_state_stack() gets the state for the top slist on the stack.
549 struct sm_state
*get_sm_state_stree_stack(struct stree_stack
*stack
,
550 int owner
, const char *name
,
554 struct sm_state
*ret
;
556 stree
= pop_stree(&stack
);
557 ret
= get_sm_state_stree(stree
, owner
, name
, sym
);
558 push_stree(&stack
, stree
);
562 struct smatch_state
*get_state_stree_stack(struct stree_stack
*stack
,
563 int owner
, const char *name
,
568 sm
= get_sm_state_stree_stack(stack
, owner
, name
, sym
);
574 static void match_states_stree(struct stree
**one
, struct stree
**two
)
576 struct smatch_state
*tmp_state
;
578 struct state_list
*add_to_one
= NULL
;
579 struct state_list
*add_to_two
= NULL
;
583 avl_iter_begin(&one_iter
, *one
, FORWARD
);
584 avl_iter_begin(&two_iter
, *two
, FORWARD
);
587 if (!one_iter
.sm
&& !two_iter
.sm
)
589 if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) < 0) {
590 __set_fake_cur_stree_fast(*two
);
591 tmp_state
= __client_unmatched_state_function(one_iter
.sm
);
592 __pop_fake_cur_stree_fast();
593 sm
= alloc_state_no_name(one_iter
.sm
->owner
, one_iter
.sm
->name
,
594 one_iter
.sm
->sym
, tmp_state
);
595 add_ptr_list(&add_to_two
, sm
);
596 avl_iter_next(&one_iter
);
597 } else if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) == 0) {
598 avl_iter_next(&one_iter
);
599 avl_iter_next(&two_iter
);
601 __set_fake_cur_stree_fast(*one
);
602 tmp_state
= __client_unmatched_state_function(two_iter
.sm
);
603 __pop_fake_cur_stree_fast();
604 sm
= alloc_state_no_name(two_iter
.sm
->owner
, two_iter
.sm
->name
,
605 two_iter
.sm
->sym
, tmp_state
);
606 add_ptr_list(&add_to_one
, sm
);
607 avl_iter_next(&two_iter
);
611 FOR_EACH_PTR(add_to_one
, sm
) {
613 } END_FOR_EACH_PTR(sm
);
615 FOR_EACH_PTR(add_to_two
, sm
) {
617 } END_FOR_EACH_PTR(sm
);
619 free_slist(&add_to_one
);
620 free_slist(&add_to_two
);
623 static void call_pre_merge_hooks(struct stree
**one
, struct stree
**two
)
629 __swap_cur_stree(*one
);
630 FOR_EACH_SM(*two
, sm
) {
631 call_pre_merge_hook(sm
);
632 } END_FOR_EACH_SM(sm
);
633 *one
= clone_stree(__get_cur_stree());
635 __swap_cur_stree(*two
);
636 FOR_EACH_SM(*one
, sm
) {
637 call_pre_merge_hook(sm
);
638 } END_FOR_EACH_SM(sm
);
639 *two
= clone_stree(__get_cur_stree());
641 restore_all_states();
644 static void clone_pool_havers_stree(struct stree
**stree
)
646 struct sm_state
*sm
, *tmp
;
647 struct state_list
*slist
= NULL
;
649 FOR_EACH_SM(*stree
, sm
) {
652 add_ptr_list(&slist
, tmp
);
654 } END_FOR_EACH_SM(sm
);
656 FOR_EACH_PTR(slist
, sm
) {
657 avl_insert(stree
, sm
);
658 } END_FOR_EACH_PTR(sm
);
666 * merge_slist() is called whenever paths merge, such as after
667 * an if statement. It takes the two slists and creates one.
669 static void __merge_stree(struct stree
**to
, struct stree
*stree
, int add_pool
)
671 struct stree
*results
= NULL
;
672 struct stree
*implied_one
= NULL
;
673 struct stree
*implied_two
= NULL
;
676 struct sm_state
*tmp_sm
;
681 /* merging a null and nonnull path gives you only the nonnull path */
688 *to
= clone_stree(stree
);
692 implied_one
= clone_stree(*to
);
693 implied_two
= clone_stree(stree
);
695 match_states_stree(&implied_one
, &implied_two
);
696 call_pre_merge_hooks(&implied_one
, &implied_two
);
699 clone_pool_havers_stree(&implied_one
);
700 clone_pool_havers_stree(&implied_two
);
702 set_stree_id(&implied_one
, ++__stree_id
);
703 set_stree_id(&implied_two
, ++__stree_id
);
706 push_stree(&all_pools
, implied_one
);
707 push_stree(&all_pools
, implied_two
);
709 avl_iter_begin(&one_iter
, implied_one
, FORWARD
);
710 avl_iter_begin(&two_iter
, implied_two
, FORWARD
);
713 if (!one_iter
.sm
&& !two_iter
.sm
)
715 if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) < 0) {
716 sm_msg("error: Internal smatch error.");
717 avl_iter_next(&one_iter
);
718 } else if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) == 0) {
719 if (add_pool
&& one_iter
.sm
!= two_iter
.sm
) {
720 one_iter
.sm
->pool
= implied_one
;
721 two_iter
.sm
->pool
= implied_two
;
723 tmp_sm
= merge_sm_states(one_iter
.sm
, two_iter
.sm
);
724 avl_insert(&results
, tmp_sm
);
725 avl_iter_next(&one_iter
);
726 avl_iter_next(&two_iter
);
728 sm_msg("error: Internal smatch error.");
729 avl_iter_next(&two_iter
);
737 void merge_stree(struct stree
**to
, struct stree
*stree
)
739 __merge_stree(to
, stree
, 1);
742 void merge_stree_no_pools(struct stree
**to
, struct stree
*stree
)
744 __merge_stree(to
, stree
, 0);
748 * This is unfortunately a bit subtle... The problem is that if a
749 * state is set on one fake stree but not the other then we should
750 * look up the the original state and use that as the unset state.
751 * Fortunately, after you pop your fake stree then the cur_slist should
752 * reflect the original state.
754 void merge_fake_stree(struct stree
**to
, struct stree
*stree
)
756 struct stree
*one
= *to
;
757 struct stree
*two
= stree
;
759 struct state_list
*add_to_one
= NULL
;
760 struct state_list
*add_to_two
= NULL
;
769 *to
= clone_stree(stree
);
773 avl_iter_begin(&one_iter
, one
, FORWARD
);
774 avl_iter_begin(&two_iter
, two
, FORWARD
);
777 if (!one_iter
.sm
&& !two_iter
.sm
)
779 if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) < 0) {
780 sm
= get_sm_state(one_iter
.sm
->owner
, one_iter
.sm
->name
,
783 add_ptr_list(&add_to_two
, sm
);
784 avl_iter_next(&one_iter
);
785 } else if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) == 0) {
786 avl_iter_next(&one_iter
);
787 avl_iter_next(&two_iter
);
789 sm
= get_sm_state(two_iter
.sm
->owner
, two_iter
.sm
->name
,
792 add_ptr_list(&add_to_one
, sm
);
793 avl_iter_next(&two_iter
);
797 FOR_EACH_PTR(add_to_one
, sm
) {
798 avl_insert(&one
, sm
);
799 } END_FOR_EACH_PTR(sm
);
801 FOR_EACH_PTR(add_to_two
, sm
) {
802 avl_insert(&two
, sm
);
803 } END_FOR_EACH_PTR(sm
);
805 free_slist(&add_to_one
);
806 free_slist(&add_to_two
);
808 __merge_stree(&one
, two
, 1);
814 * filter_slist() removes any sm states "slist" holds in common with "filter"
816 void filter_stree(struct stree
**stree
, struct stree
*filter
)
818 struct stree
*results
= NULL
;
822 avl_iter_begin(&one_iter
, *stree
, FORWARD
);
823 avl_iter_begin(&two_iter
, filter
, FORWARD
);
825 /* FIXME: This should probably be re-written with trees in mind */
828 if (!one_iter
.sm
&& !two_iter
.sm
)
830 if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) < 0) {
831 avl_insert(&results
, one_iter
.sm
);
832 avl_iter_next(&one_iter
);
833 } else if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) == 0) {
834 if (one_iter
.sm
!= two_iter
.sm
)
835 avl_insert(&results
, one_iter
.sm
);
836 avl_iter_next(&one_iter
);
837 avl_iter_next(&two_iter
);
839 avl_iter_next(&two_iter
);
849 * and_slist_stack() pops the top two slists, overwriting the one with
850 * the other and pushing it back on the stack.
852 void and_stree_stack(struct stree_stack
**stack
)
854 struct sm_state
*tmp
;
855 struct stree
*right_stree
= pop_stree(stack
);
857 FOR_EACH_SM(right_stree
, tmp
) {
858 overwrite_sm_state_stree_stack(stack
, tmp
);
859 } END_FOR_EACH_SM(tmp
);
860 free_stree(&right_stree
);
864 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
865 * It pops the two slists from the top of the stack and merges them
866 * together in a way that preserves the things they have in common
867 * but creates a merged state for most of the rest.
868 * You could have code that had: if (foo || foo) { foo->baz;
869 * It's this function which ensures smatch does the right thing.
871 void or_stree_stack(struct stree_stack
**pre_conds
,
872 struct stree
*cur_stree
,
873 struct stree_stack
**stack
)
877 struct stree
*pre_stree
;
879 struct stree
*tmp_stree
;
881 new = pop_stree(stack
);
882 old
= pop_stree(stack
);
884 pre_stree
= pop_stree(pre_conds
);
885 push_stree(pre_conds
, clone_stree(pre_stree
));
887 res
= clone_stree(pre_stree
);
888 overwrite_stree(old
, &res
);
890 tmp_stree
= clone_stree(cur_stree
);
891 overwrite_stree(new, &tmp_stree
);
893 merge_stree(&res
, tmp_stree
);
894 filter_stree(&res
, pre_stree
);
896 push_stree(stack
, res
);
897 free_stree(&tmp_stree
);
898 free_stree(&pre_stree
);
904 * get_named_stree() is only used for gotos.
906 struct stree
**get_named_stree(struct named_stree_stack
*stack
,
909 struct named_stree
*tmp
;
911 FOR_EACH_PTR(stack
, tmp
) {
912 if (!strcmp(tmp
->name
, name
))
914 } END_FOR_EACH_PTR(tmp
);
918 /* FIXME: These parameters are in a different order from expected */
919 void overwrite_stree(struct stree
*from
, struct stree
**to
)
921 struct sm_state
*tmp
;
923 FOR_EACH_SM(from
, tmp
) {
924 overwrite_sm_state_stree(to
, tmp
);
925 } END_FOR_EACH_SM(tmp
);