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
))
47 FOR_EACH_PTR(sm
->possible
, tmp
) {
49 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, ", ");
50 if (pos
> sizeof(buf
))
52 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, "%s",
53 show_state(tmp
->state
));
54 if (pos
> sizeof(buf
))
56 } END_FOR_EACH_PTR(tmp
);
57 snprintf(buf
+ pos
, sizeof(buf
) - pos
, ")");
62 for (i
= 0; i
< 3; i
++)
63 buf
[sizeof(buf
) - 2 - i
] = '.';
67 void __print_stree(struct stree
*stree
)
71 printf("dumping stree at %d\n", get_lineno());
72 FOR_EACH_SM(stree
, sm
) {
73 printf("%s\n", show_sm(sm
));
74 } END_FOR_EACH_SM(sm
);
78 /* NULL states go at the end to simplify merge_slist */
79 int cmp_tracker(const struct sm_state
*a
, const struct sm_state
*b
)
90 if (a
->owner
> b
->owner
)
92 if (a
->owner
< b
->owner
)
95 ret
= strcmp(a
->name
, b
->name
);
99 if (!b
->sym
&& a
->sym
)
101 if (!a
->sym
&& b
->sym
)
111 static int cmp_sm_states(const struct sm_state
*a
, const struct sm_state
*b
, int preserve
)
115 ret
= cmp_tracker(a
, b
);
119 /* todo: add hook for smatch_extra.c */
120 if (a
->state
> b
->state
)
122 if (a
->state
< b
->state
)
124 /* This is obviously a massive disgusting hack but we need to preserve
125 * the unmerged states for smatch extra because we use them in
126 * smatch_db.c. Meanwhile if we preserve all the other unmerged states
127 * then it uses a lot of memory and we don't use it. Hence this hack.
129 * Also sometimes even just preserving every possible SMATCH_EXTRA state
130 * takes too much resources so we have to cap that. Capping is probably
131 * not often a problem in real life.
133 if (a
->owner
== SMATCH_EXTRA
&& preserve
) {
136 if (a
->merged
== 1 && b
->merged
== 0)
145 static struct sm_state
*alloc_sm_state(int owner
, const char *name
,
146 struct symbol
*sym
, struct smatch_state
*state
)
148 struct sm_state
*sm_state
= __alloc_sm_state(0);
152 sm_state
->name
= alloc_sname(name
);
153 sm_state
->owner
= owner
;
155 sm_state
->state
= state
;
156 sm_state
->line
= get_lineno();
157 sm_state
->merged
= 0;
158 sm_state
->implied
= 0;
159 sm_state
->pool
= NULL
;
160 sm_state
->left
= NULL
;
161 sm_state
->right
= NULL
;
162 sm_state
->nr_children
= 1;
163 sm_state
->possible
= NULL
;
164 add_ptr_list(&sm_state
->possible
, sm_state
);
168 static struct sm_state
*alloc_state_no_name(int owner
, const char *name
,
170 struct smatch_state
*state
)
172 struct sm_state
*tmp
;
174 tmp
= alloc_sm_state(owner
, NULL
, sym
, state
);
179 int too_many_possible(struct sm_state
*sm
)
181 if (ptr_list_size((struct ptr_list
*)sm
->possible
) >= 100)
186 void add_possible_sm(struct sm_state
*to
, struct sm_state
*new)
188 struct sm_state
*tmp
;
191 if (too_many_possible(to
))
194 FOR_EACH_PTR(to
->possible
, tmp
) {
195 if (cmp_sm_states(tmp
, new, preserve
) < 0)
197 else if (cmp_sm_states(tmp
, new, preserve
) == 0) {
200 INSERT_CURRENT(new, tmp
);
203 } END_FOR_EACH_PTR(tmp
);
204 add_ptr_list(&to
->possible
, new);
207 static void copy_possibles(struct sm_state
*to
, struct sm_state
*from
)
209 struct sm_state
*tmp
;
211 FOR_EACH_PTR(from
->possible
, tmp
) {
212 add_possible_sm(to
, tmp
);
213 } END_FOR_EACH_PTR(tmp
);
216 char *alloc_sname(const char *str
)
222 tmp
= __alloc_sname(strlen(str
) + 1);
227 int out_of_memory(void)
230 * I decided to use 50M here based on trial and error.
231 * It works out OK for the kernel and so it should work
232 * for most other projects as well.
234 if (sm_state_counter
* sizeof(struct sm_state
) >= 50000000)
239 int low_on_memory(void)
241 if (sm_state_counter
* sizeof(struct sm_state
) >= 25000000)
246 static void free_sm_state(struct sm_state
*sm
)
248 free_slist(&sm
->possible
);
250 * fixme. Free the actual state.
251 * Right now we leave it until the end of the function
252 * because we don't want to double free it.
253 * Use the freelist to not double free things
257 static void free_all_sm_states(struct allocation_blob
*blob
)
259 unsigned int size
= sizeof(struct sm_state
);
260 unsigned int offset
= 0;
262 while (offset
< blob
->offset
) {
263 free_sm_state((struct sm_state
*)(blob
->data
+ offset
));
268 /* At the end of every function we free all the sm_states */
269 void free_every_single_sm_state(void)
271 struct allocator_struct
*desc
= &sm_state_allocator
;
272 struct allocation_blob
*blob
= desc
->blobs
;
275 desc
->allocations
= 0;
276 desc
->total_bytes
= 0;
277 desc
->useful_bytes
= 0;
278 desc
->freelist
= NULL
;
280 struct allocation_blob
*next
= blob
->next
;
281 free_all_sm_states(blob
);
282 blob_free(blob
, desc
->chunking
);
286 clear_smatch_state_alloc();
288 free_stack_and_strees(&all_pools
);
289 sm_state_counter
= 0;
292 struct sm_state
*clone_sm(struct sm_state
*s
)
294 struct sm_state
*ret
;
296 ret
= alloc_state_no_name(s
->owner
, s
->name
, s
->sym
, s
->state
);
297 ret
->merged
= s
->merged
;
298 ret
->implied
= s
->implied
;
300 /* clone_sm() doesn't copy the pools. Each state needs to have
302 ret
->possible
= clone_slist(s
->possible
);
304 ret
->right
= s
->right
;
305 ret
->nr_children
= s
->nr_children
;
309 int is_merged(struct sm_state
*sm
)
314 int is_implied(struct sm_state
*sm
)
319 int slist_has_state(struct state_list
*slist
, struct smatch_state
*state
)
321 struct sm_state
*tmp
;
323 FOR_EACH_PTR(slist
, tmp
) {
324 if (tmp
->state
== state
)
326 } END_FOR_EACH_PTR(tmp
);
330 struct state_list
*clone_slist(struct state_list
*from_slist
)
333 struct state_list
*to_slist
= NULL
;
335 FOR_EACH_PTR(from_slist
, sm
) {
336 add_ptr_list(&to_slist
, sm
);
337 } END_FOR_EACH_PTR(sm
);
341 struct smatch_state
*merge_states(int owner
, const char *name
,
343 struct smatch_state
*state1
,
344 struct smatch_state
*state2
)
346 struct smatch_state
*ret
;
348 if (state1
== state2
)
350 else if (__has_merge_function(owner
))
351 ret
= __client_merge_function(owner
, state1
, state2
);
352 else if (!state1
|| !state2
)
359 struct sm_state
*merge_sm_states(struct sm_state
*one
, struct sm_state
*two
)
361 struct smatch_state
*s
;
362 struct sm_state
*result
;
366 s
= merge_states(one
->owner
, one
->name
, one
->sym
, one
->state
, two
->state
);
367 result
= alloc_state_no_name(one
->owner
, one
->name
, one
->sym
, s
);
371 result
->nr_children
= one
->nr_children
+ two
->nr_children
;
372 copy_possibles(result
, one
);
373 copy_possibles(result
, two
);
376 strcmp(check_name(one
->owner
), option_debug_check
) == 0) {
377 struct sm_state
*tmp
;
380 printf("%s:%d %s() merge [%s] '%s' %s(L %d) + %s(L %d) => %s (",
381 get_filename(), get_lineno(), get_function(),
382 check_name(one
->owner
), one
->name
,
383 show_state(one
->state
), one
->line
,
384 show_state(two
->state
), two
->line
,
387 FOR_EACH_PTR(result
->possible
, tmp
) {
390 printf("%s", show_state(tmp
->state
));
391 } END_FOR_EACH_PTR(tmp
);
398 struct sm_state
*get_sm_state_stree(struct stree
*stree
, int owner
, const char *name
,
401 struct tracker tracker
= {
403 .name
= (char *)name
,
411 return avl_lookup(stree
, (struct sm_state
*)&tracker
);
414 struct smatch_state
*get_state_stree(struct stree
*stree
,
415 int owner
, const char *name
,
420 sm
= get_sm_state_stree(stree
, owner
, name
, sym
);
426 /* FIXME: this is almost exactly the same as set_sm_state_slist() */
427 void overwrite_sm_state_stree(struct stree
**stree
, struct sm_state
*new)
429 avl_insert(stree
, new);
432 void overwrite_sm_state_stree_stack(struct stree_stack
**stack
,
437 stree
= pop_stree(stack
);
438 overwrite_sm_state_stree(&stree
, sm
);
439 push_stree(stack
, stree
);
442 struct sm_state
*set_state_stree(struct stree
**stree
, int owner
, const char *name
,
443 struct symbol
*sym
, struct smatch_state
*state
)
445 struct sm_state
*new = alloc_sm_state(owner
, name
, sym
, state
);
447 avl_insert(stree
, new);
451 void set_state_stree_perm(struct stree
**stree
, int owner
, const char *name
,
452 struct symbol
*sym
, struct smatch_state
*state
)
456 sm
= malloc(sizeof(*sm
) + strlen(name
) + 1);
457 memset(sm
, 0, sizeof(*sm
));
459 sm
->name
= (char *)(sm
+ 1);
460 strcpy((char *)sm
->name
, name
);
464 overwrite_sm_state_stree(stree
, sm
);
467 void delete_state_stree(struct stree
**stree
, int owner
, const char *name
,
470 struct tracker tracker
= {
472 .name
= (char *)name
,
476 avl_remove(stree
, (struct sm_state
*)&tracker
);
479 void delete_state_stree_stack(struct stree_stack
**stack
, int owner
, const char *name
,
484 stree
= pop_stree(stack
);
485 delete_state_stree(&stree
, owner
, name
, sym
);
486 push_stree(stack
, stree
);
489 void push_stree(struct stree_stack
**stack
, struct stree
*stree
)
491 add_ptr_list(stack
, stree
);
494 struct stree
*pop_stree(struct stree_stack
**stack
)
498 stree
= last_ptr_list((struct ptr_list
*)*stack
);
499 delete_ptr_list_last((struct ptr_list
**)stack
);
503 void free_slist(struct state_list
**slist
)
505 __free_ptr_list((struct ptr_list
**)slist
);
508 void free_stree_stack(struct stree_stack
**stack
)
510 __free_ptr_list((struct ptr_list
**)stack
);
513 void free_stack_and_strees(struct stree_stack
**stree_stack
)
517 FOR_EACH_PTR(*stree_stack
, stree
) {
519 } END_FOR_EACH_PTR(stree
);
520 free_stree_stack(stree_stack
);
523 struct sm_state
*set_state_stree_stack(struct stree_stack
**stack
, int owner
, const char *name
,
524 struct symbol
*sym
, struct smatch_state
*state
)
529 stree
= pop_stree(stack
);
530 sm
= set_state_stree(&stree
, owner
, name
, sym
, state
);
531 push_stree(stack
, stree
);
537 * get_sm_state_stack() gets the state for the top slist on the stack.
539 struct sm_state
*get_sm_state_stree_stack(struct stree_stack
*stack
,
540 int owner
, const char *name
,
544 struct sm_state
*ret
;
546 stree
= pop_stree(&stack
);
547 ret
= get_sm_state_stree(stree
, owner
, name
, sym
);
548 push_stree(&stack
, stree
);
552 struct smatch_state
*get_state_stree_stack(struct stree_stack
*stack
,
553 int owner
, const char *name
,
558 sm
= get_sm_state_stree_stack(stack
, owner
, name
, sym
);
564 static void match_states_stree(struct stree
**one
, struct stree
**two
)
566 struct smatch_state
*tmp_state
;
568 struct state_list
*add_to_one
= NULL
;
569 struct state_list
*add_to_two
= NULL
;
573 avl_iter_begin(&one_iter
, *one
, FORWARD
);
574 avl_iter_begin(&two_iter
, *two
, FORWARD
);
577 if (!one_iter
.sm
&& !two_iter
.sm
)
579 if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) < 0) {
580 __set_fake_cur_stree_fast(*two
);
581 tmp_state
= __client_unmatched_state_function(one_iter
.sm
);
582 __pop_fake_cur_stree_fast();
583 sm
= alloc_state_no_name(one_iter
.sm
->owner
, one_iter
.sm
->name
,
584 one_iter
.sm
->sym
, tmp_state
);
585 add_ptr_list(&add_to_two
, sm
);
586 avl_iter_next(&one_iter
);
587 } else if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) == 0) {
588 avl_iter_next(&one_iter
);
589 avl_iter_next(&two_iter
);
591 __set_fake_cur_stree_fast(*one
);
592 tmp_state
= __client_unmatched_state_function(two_iter
.sm
);
593 __pop_fake_cur_stree_fast();
594 sm
= alloc_state_no_name(two_iter
.sm
->owner
, two_iter
.sm
->name
,
595 two_iter
.sm
->sym
, tmp_state
);
596 add_ptr_list(&add_to_one
, sm
);
597 avl_iter_next(&two_iter
);
601 FOR_EACH_PTR(add_to_one
, sm
) {
603 } END_FOR_EACH_PTR(sm
);
605 FOR_EACH_PTR(add_to_two
, sm
) {
607 } END_FOR_EACH_PTR(sm
);
609 free_slist(&add_to_one
);
610 free_slist(&add_to_two
);
613 static void clone_pool_havers_stree(struct stree
**stree
)
615 struct sm_state
*sm
, *tmp
;
616 struct state_list
*slist
= NULL
;
618 FOR_EACH_SM(*stree
, sm
) {
621 add_ptr_list(&slist
, tmp
);
623 } END_FOR_EACH_SM(sm
);
625 FOR_EACH_PTR(slist
, sm
) {
626 avl_insert(stree
, sm
);
627 } END_FOR_EACH_PTR(sm
);
635 * merge_slist() is called whenever paths merge, such as after
636 * an if statement. It takes the two slists and creates one.
638 static void __merge_stree(struct stree
**to
, struct stree
*stree
, int add_pool
)
640 struct stree
*results
= NULL
;
641 struct stree
*implied_one
= NULL
;
642 struct stree
*implied_two
= NULL
;
645 struct sm_state
*tmp_sm
;
650 /* merging a null and nonnull path gives you only the nonnull path */
657 *to
= clone_stree(stree
);
661 implied_one
= clone_stree(*to
);
662 implied_two
= clone_stree(stree
);
664 match_states_stree(&implied_one
, &implied_two
);
667 clone_pool_havers_stree(&implied_one
);
668 clone_pool_havers_stree(&implied_two
);
670 set_stree_id(&implied_one
, ++__stree_id
);
671 set_stree_id(&implied_two
, ++__stree_id
);
674 push_stree(&all_pools
, implied_one
);
675 push_stree(&all_pools
, implied_two
);
677 avl_iter_begin(&one_iter
, implied_one
, FORWARD
);
678 avl_iter_begin(&two_iter
, implied_two
, FORWARD
);
681 if (!one_iter
.sm
&& !two_iter
.sm
)
683 if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) < 0) {
684 sm_msg("error: Internal smatch error.");
685 avl_iter_next(&one_iter
);
686 } else if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) == 0) {
687 if (add_pool
&& one_iter
.sm
!= two_iter
.sm
) {
688 one_iter
.sm
->pool
= implied_one
;
689 two_iter
.sm
->pool
= implied_two
;
691 tmp_sm
= merge_sm_states(one_iter
.sm
, two_iter
.sm
);
692 avl_insert(&results
, tmp_sm
);
693 avl_iter_next(&one_iter
);
694 avl_iter_next(&two_iter
);
696 sm_msg("error: Internal smatch error.");
697 avl_iter_next(&two_iter
);
705 void merge_stree(struct stree
**to
, struct stree
*stree
)
707 __merge_stree(to
, stree
, 1);
710 void merge_stree_no_pools(struct stree
**to
, struct stree
*stree
)
712 __merge_stree(to
, stree
, 0);
716 * This is unfortunately a bit subtle... The problem is that if a
717 * state is set on one fake stree but not the other then we should
718 * look up the the original state and use that as the unset state.
719 * Fortunately, after you pop your fake stree then the cur_slist should
720 * reflect the original state.
722 void merge_fake_stree(struct stree
**to
, struct stree
*stree
)
724 struct stree
*one
= *to
;
725 struct stree
*two
= stree
;
727 struct state_list
*add_to_one
= NULL
;
728 struct state_list
*add_to_two
= NULL
;
737 *to
= clone_stree(stree
);
741 avl_iter_begin(&one_iter
, one
, FORWARD
);
742 avl_iter_begin(&two_iter
, two
, FORWARD
);
745 if (!one_iter
.sm
&& !two_iter
.sm
)
747 if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) < 0) {
748 sm
= get_sm_state(one_iter
.sm
->owner
, one_iter
.sm
->name
,
751 add_ptr_list(&add_to_two
, sm
);
752 avl_iter_next(&one_iter
);
753 } else if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) == 0) {
754 avl_iter_next(&one_iter
);
755 avl_iter_next(&two_iter
);
757 sm
= get_sm_state(two_iter
.sm
->owner
, two_iter
.sm
->name
,
760 add_ptr_list(&add_to_one
, sm
);
761 avl_iter_next(&two_iter
);
765 FOR_EACH_PTR(add_to_one
, sm
) {
766 avl_insert(&one
, sm
);
767 } END_FOR_EACH_PTR(sm
);
769 FOR_EACH_PTR(add_to_two
, sm
) {
770 avl_insert(&two
, sm
);
771 } END_FOR_EACH_PTR(sm
);
773 free_slist(&add_to_one
);
774 free_slist(&add_to_two
);
776 __merge_stree(&one
, two
, 1);
782 * filter_slist() removes any sm states "slist" holds in common with "filter"
784 void filter_stree(struct stree
**stree
, struct stree
*filter
)
786 struct stree
*results
= NULL
;
790 avl_iter_begin(&one_iter
, *stree
, FORWARD
);
791 avl_iter_begin(&two_iter
, filter
, FORWARD
);
793 /* FIXME: This should probably be re-written with trees in mind */
796 if (!one_iter
.sm
&& !two_iter
.sm
)
798 if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) < 0) {
799 avl_insert(&results
, one_iter
.sm
);
800 avl_iter_next(&one_iter
);
801 } else if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) == 0) {
802 if (one_iter
.sm
!= two_iter
.sm
)
803 avl_insert(&results
, one_iter
.sm
);
804 avl_iter_next(&one_iter
);
805 avl_iter_next(&two_iter
);
807 avl_iter_next(&two_iter
);
817 * and_slist_stack() pops the top two slists, overwriting the one with
818 * the other and pushing it back on the stack.
820 void and_stree_stack(struct stree_stack
**stack
)
822 struct sm_state
*tmp
;
823 struct stree
*right_stree
= pop_stree(stack
);
825 FOR_EACH_SM(right_stree
, tmp
) {
826 overwrite_sm_state_stree_stack(stack
, tmp
);
827 } END_FOR_EACH_SM(tmp
);
828 free_stree(&right_stree
);
832 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
833 * It pops the two slists from the top of the stack and merges them
834 * together in a way that preserves the things they have in common
835 * but creates a merged state for most of the rest.
836 * You could have code that had: if (foo || foo) { foo->baz;
837 * It's this function which ensures smatch does the right thing.
839 void or_stree_stack(struct stree_stack
**pre_conds
,
840 struct stree
*cur_stree
,
841 struct stree_stack
**stack
)
845 struct stree
*pre_stree
;
847 struct stree
*tmp_stree
;
849 new = pop_stree(stack
);
850 old
= pop_stree(stack
);
852 pre_stree
= pop_stree(pre_conds
);
853 push_stree(pre_conds
, clone_stree(pre_stree
));
855 res
= clone_stree(pre_stree
);
856 overwrite_stree(old
, &res
);
858 tmp_stree
= clone_stree(cur_stree
);
859 overwrite_stree(new, &tmp_stree
);
861 merge_stree(&res
, tmp_stree
);
862 filter_stree(&res
, pre_stree
);
864 push_stree(stack
, res
);
865 free_stree(&tmp_stree
);
866 free_stree(&pre_stree
);
872 * get_named_stree() is only used for gotos.
874 struct stree
**get_named_stree(struct named_stree_stack
*stack
,
877 struct named_stree
*tmp
;
879 FOR_EACH_PTR(stack
, tmp
) {
880 if (!strcmp(tmp
->name
, name
))
882 } END_FOR_EACH_PTR(tmp
);
886 /* FIXME: These parameters are in a different order from expected */
887 void overwrite_stree(struct stree
*from
, struct stree
**to
)
889 struct sm_state
*tmp
;
891 FOR_EACH_SM(from
, tmp
) {
892 overwrite_sm_state_stree(to
, tmp
);
893 } END_FOR_EACH_SM(tmp
);