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_slist
, "named slist");
28 ALLOCATOR(named_stree
, "named slist");
29 __DO_ALLOCATOR(char, 1, 4, "state names", sname
);
31 static int sm_state_counter
;
33 char *show_sm(struct sm_state
*sm
)
40 pos
= snprintf(buf
, sizeof(buf
), "[%s] '%s' = %s (",
41 check_name(sm
->owner
), sm
->name
, show_state(sm
->state
));
42 if (pos
> sizeof(buf
))
46 FOR_EACH_PTR(sm
->possible
, tmp
) {
48 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, ", ");
49 if (pos
> sizeof(buf
))
51 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, "%s",
52 show_state(tmp
->state
));
53 if (pos
> sizeof(buf
))
55 } END_FOR_EACH_PTR(tmp
);
56 snprintf(buf
+ pos
, sizeof(buf
) - pos
, ")");
61 for (i
= 0; i
< 3; i
++)
62 buf
[sizeof(buf
) - 2 - i
] = '.';
66 void __print_slist(struct state_list
*slist
)
70 printf("dumping slist at %d\n", get_lineno());
71 FOR_EACH_PTR(slist
, sm
) {
72 printf("%s\n", show_sm(sm
));
73 } END_FOR_EACH_PTR(sm
);
77 void __print_stree(struct AVL
*stree
)
81 printf("dumping stree at %d\n", get_lineno());
82 FOR_EACH_SM(stree
, sm
) {
83 printf("%s\n", show_sm(sm
));
84 } END_FOR_EACH_SM(sm
);
88 /* NULL states go at the end to simplify merge_slist */
89 int cmp_tracker(const struct sm_state
*a
, const struct sm_state
*b
)
100 if (a
->owner
> b
->owner
)
102 if (a
->owner
< b
->owner
)
105 ret
= strcmp(a
->name
, b
->name
);
109 if (!b
->sym
&& a
->sym
)
111 if (!a
->sym
&& b
->sym
)
121 static int cmp_sm_states(const struct sm_state
*a
, const struct sm_state
*b
, int preserve
)
125 ret
= cmp_tracker(a
, b
);
129 /* todo: add hook for smatch_extra.c */
130 if (a
->state
> b
->state
)
132 if (a
->state
< b
->state
)
134 /* This is obviously a massive disgusting hack but we need to preserve
135 * the unmerged states for smatch extra because we use them in
136 * smatch_db.c. Meanwhile if we preserve all the other unmerged states
137 * then it uses a lot of memory and we don't use it. Hence this hack.
139 * Also sometimes even just preserving every possible SMATCH_EXTRA state
140 * takes too much resources so we have to cap that. Capping is probably
141 * not often a problem in real life.
143 if (a
->owner
== SMATCH_EXTRA
&& preserve
) {
146 if (a
->merged
== 1 && b
->merged
== 0)
155 static struct sm_state
*alloc_sm_state(int owner
, const char *name
,
156 struct symbol
*sym
, struct smatch_state
*state
)
158 struct sm_state
*sm_state
= __alloc_sm_state(0);
162 sm_state
->name
= alloc_sname(name
);
163 sm_state
->owner
= owner
;
165 sm_state
->state
= state
;
166 sm_state
->line
= get_lineno();
167 sm_state
->merged
= 0;
168 sm_state
->implied
= 0;
169 sm_state
->pool
= NULL
;
170 sm_state
->left
= NULL
;
171 sm_state
->right
= NULL
;
172 sm_state
->nr_children
= 1;
173 sm_state
->possible
= NULL
;
174 add_ptr_list(&sm_state
->possible
, sm_state
);
178 static struct sm_state
*alloc_state_no_name(int owner
, const char *name
,
180 struct smatch_state
*state
)
182 struct sm_state
*tmp
;
184 tmp
= alloc_sm_state(owner
, NULL
, sym
, state
);
189 int too_many_possible(struct sm_state
*sm
)
191 if (ptr_list_size((struct ptr_list
*)sm
->possible
) >= 100)
196 void add_possible_sm(struct sm_state
*to
, struct sm_state
*new)
198 struct sm_state
*tmp
;
201 if (too_many_possible(to
))
204 FOR_EACH_PTR(to
->possible
, tmp
) {
205 if (cmp_sm_states(tmp
, new, preserve
) < 0)
207 else if (cmp_sm_states(tmp
, new, preserve
) == 0) {
210 INSERT_CURRENT(new, tmp
);
213 } END_FOR_EACH_PTR(tmp
);
214 add_ptr_list(&to
->possible
, new);
217 static void copy_possibles(struct sm_state
*to
, struct sm_state
*from
)
219 struct sm_state
*tmp
;
221 FOR_EACH_PTR(from
->possible
, tmp
) {
222 add_possible_sm(to
, tmp
);
223 } END_FOR_EACH_PTR(tmp
);
226 char *alloc_sname(const char *str
)
232 tmp
= __alloc_sname(strlen(str
) + 1);
240 * I decided to use 50M here based on trial and error.
241 * It works out OK for the kernel and so it should work
242 * for most other projects as well.
244 if (sm_state_counter
* sizeof(struct sm_state
) >= 50000000)
249 int low_on_memory(void)
251 if (sm_state_counter
* sizeof(struct sm_state
) >= 25000000)
256 static void free_sm_state(struct sm_state
*sm
)
258 free_slist(&sm
->possible
);
260 * fixme. Free the actual state.
261 * Right now we leave it until the end of the function
262 * because we don't want to double free it.
263 * Use the freelist to not double free things
267 static void free_all_sm_states(struct allocation_blob
*blob
)
269 unsigned int size
= sizeof(struct sm_state
);
270 unsigned int offset
= 0;
272 while (offset
< blob
->offset
) {
273 free_sm_state((struct sm_state
*)(blob
->data
+ offset
));
278 /* At the end of every function we free all the sm_states */
279 void free_every_single_sm_state(void)
281 struct allocator_struct
*desc
= &sm_state_allocator
;
282 struct allocation_blob
*blob
= desc
->blobs
;
285 desc
->allocations
= 0;
286 desc
->total_bytes
= 0;
287 desc
->useful_bytes
= 0;
288 desc
->freelist
= NULL
;
290 struct allocation_blob
*next
= blob
->next
;
291 free_all_sm_states(blob
);
292 blob_free(blob
, desc
->chunking
);
296 clear_smatch_state_alloc();
298 sm_state_counter
= 0;
301 struct sm_state
*clone_sm(struct sm_state
*s
)
303 struct sm_state
*ret
;
305 ret
= alloc_state_no_name(s
->owner
, s
->name
, s
->sym
, s
->state
);
306 ret
->merged
= s
->merged
;
307 ret
->implied
= s
->implied
;
309 /* clone_sm() doesn't copy the pools. Each state needs to have
311 ret
->possible
= clone_slist(s
->possible
);
313 ret
->right
= s
->right
;
314 ret
->nr_children
= s
->nr_children
;
318 int is_merged(struct sm_state
*sm
)
323 int is_implied(struct sm_state
*sm
)
328 int slist_has_state(struct state_list
*slist
, struct smatch_state
*state
)
330 struct sm_state
*tmp
;
332 FOR_EACH_PTR(slist
, tmp
) {
333 if (tmp
->state
== state
)
335 } END_FOR_EACH_PTR(tmp
);
339 static void check_order(struct state_list
*slist
)
343 struct sm_state
*last
= NULL
;
346 FOR_EACH_PTR(slist
, sm
) {
347 if (last
&& cmp_tracker(sm
, last
) <= 0) {
348 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
349 "%s vs %s\n", last
->owner
, sm
->owner
,
350 last
->sym
, sm
->sym
, last
->name
, sm
->name
);
354 } END_FOR_EACH_PTR(sm
);
361 struct state_list
*clone_slist(struct state_list
*from_slist
)
364 struct state_list
*to_slist
= NULL
;
366 FOR_EACH_PTR(from_slist
, sm
) {
367 add_ptr_list(&to_slist
, sm
);
368 } END_FOR_EACH_PTR(sm
);
372 struct state_list_stack
*clone_stack(struct state_list_stack
*from_stack
)
374 struct state_list
*slist
;
375 struct state_list_stack
*to_stack
= NULL
;
377 FOR_EACH_PTR(from_stack
, slist
) {
378 push_slist(&to_stack
, slist
);
379 } END_FOR_EACH_PTR(slist
);
383 struct smatch_state
*merge_states(int owner
, const char *name
,
385 struct smatch_state
*state1
,
386 struct smatch_state
*state2
)
388 struct smatch_state
*ret
;
390 if (state1
== state2
)
392 else if (__has_merge_function(owner
))
393 ret
= __client_merge_function(owner
, state1
, state2
);
394 else if (!state1
|| !state2
)
401 struct sm_state
*merge_sm_states(struct sm_state
*one
, struct sm_state
*two
)
403 struct smatch_state
*s
;
404 struct sm_state
*result
;
408 s
= merge_states(one
->owner
, one
->name
, one
->sym
, one
->state
, two
->state
);
409 result
= alloc_state_no_name(one
->owner
, one
->name
, one
->sym
, s
);
413 result
->nr_children
= one
->nr_children
+ two
->nr_children
;
414 copy_possibles(result
, one
);
415 copy_possibles(result
, two
);
418 strcmp(check_name(one
->owner
), option_debug_check
) == 0) {
419 struct sm_state
*tmp
;
422 printf("%d merge [%s] '%s' %s(L %d) + %s(L %d) => %s (",
423 get_lineno(), check_name(one
->owner
), one
->name
,
424 show_state(one
->state
), one
->line
,
425 show_state(two
->state
), two
->line
,
428 FOR_EACH_PTR(result
->possible
, tmp
) {
431 printf("%s", show_state(tmp
->state
));
432 } END_FOR_EACH_PTR(tmp
);
439 struct sm_state
*get_sm_state_slist(struct state_list
*slist
, int owner
, const char *name
,
447 FOR_EACH_PTR(slist
, sm
) {
448 if (sm
->owner
== owner
&& sm
->sym
== sym
&& !strcmp(sm
->name
, name
))
450 } END_FOR_EACH_PTR(sm
);
454 struct sm_state
*get_sm_state_stree(struct AVL
*stree
, int owner
, const char *name
,
457 struct tracker tracker
= {
459 .name
= (char *)name
,
467 return avl_lookup(stree
, (struct sm_state
*)&tracker
);
470 struct smatch_state
*get_state_slist(struct state_list
*slist
,
471 int owner
, const char *name
,
476 sm
= get_sm_state_slist(slist
, owner
, name
, sym
);
482 struct smatch_state
*get_state_stree(struct AVL
*stree
,
483 int owner
, const char *name
,
488 sm
= get_sm_state_stree(stree
, owner
, name
, sym
);
494 void overwrite_sm_state(struct state_list
**slist
, struct sm_state
*new)
496 struct sm_state
*tmp
;
498 FOR_EACH_PTR(*slist
, tmp
) {
499 if (cmp_tracker(tmp
, new) < 0)
501 else if (cmp_tracker(tmp
, new) == 0) {
502 REPLACE_CURRENT_PTR(tmp
, new);
505 INSERT_CURRENT(new, tmp
);
508 } END_FOR_EACH_PTR(tmp
);
509 add_ptr_list(slist
, new);
512 /* FIXME: this is almost exactly the same as set_sm_state_slist() */
513 void overwrite_sm_state_stree(struct AVL
**stree
, struct sm_state
*new)
515 avl_insert(stree
, new);
518 void overwrite_sm_state_stack(struct state_list_stack
**stack
,
521 struct state_list
*slist
;
523 slist
= pop_slist(stack
);
524 overwrite_sm_state(&slist
, sm
);
525 push_slist(stack
, slist
);
528 void overwrite_sm_state_stree_stack(struct stree_stack
**stack
,
533 stree
= pop_stree(stack
);
534 overwrite_sm_state_stree(&stree
, sm
);
535 push_stree(stack
, stree
);
538 struct sm_state
*set_state_slist(struct state_list
**slist
, int owner
, const char *name
,
539 struct symbol
*sym
, struct smatch_state
*state
)
541 struct sm_state
*tmp
;
542 struct sm_state
*new = alloc_sm_state(owner
, name
, sym
, state
);
544 FOR_EACH_PTR(*slist
, tmp
) {
545 if (cmp_tracker(tmp
, new) < 0)
547 else if (cmp_tracker(tmp
, new) == 0) {
548 REPLACE_CURRENT_PTR(tmp
, new);
551 INSERT_CURRENT(new, tmp
);
554 } END_FOR_EACH_PTR(tmp
);
555 add_ptr_list(slist
, new);
559 struct sm_state
*set_state_stree(struct AVL
**stree
, int owner
, const char *name
,
560 struct symbol
*sym
, struct smatch_state
*state
)
562 struct sm_state
*new = alloc_sm_state(owner
, name
, sym
, state
);
564 avl_insert(stree
, new);
568 void delete_state_slist(struct state_list
**slist
, int owner
, const char *name
,
573 FOR_EACH_PTR(*slist
, sm
) {
574 if (sm
->owner
== owner
&& sm
->sym
== sym
&& !strcmp(sm
->name
, name
)) {
575 DELETE_CURRENT_PTR(sm
);
578 } END_FOR_EACH_PTR(sm
);
581 void delete_state_stree(struct AVL
**stree
, int owner
, const char *name
,
584 struct tracker tracker
= {
586 .name
= (char *)name
,
590 avl_remove(stree
, (struct sm_state
*)&tracker
);
593 void delete_state_stack(struct state_list_stack
**stack
, int owner
, const char *name
,
596 struct state_list
*slist
;
598 slist
= pop_slist(stack
);
599 delete_state_slist(&slist
, owner
, name
, sym
);
600 push_slist(stack
, slist
);
603 void delete_state_stree_stack(struct stree_stack
**stack
, int owner
, const char *name
,
608 stree
= pop_stree(stack
);
609 delete_state_stree(&stree
, owner
, name
, sym
);
610 push_stree(stack
, stree
);
613 void push_slist(struct state_list_stack
**list_stack
, struct state_list
*slist
)
615 add_ptr_list(list_stack
, slist
);
618 struct state_list
*pop_slist(struct state_list_stack
**list_stack
)
620 struct state_list
*slist
;
622 slist
= last_ptr_list((struct ptr_list
*)*list_stack
);
623 delete_ptr_list_last((struct ptr_list
**)list_stack
);
627 void push_stree(struct stree_stack
**stack
, struct AVL
*stree
)
629 add_ptr_list(stack
, stree
);
632 struct AVL
*pop_stree(struct stree_stack
**stack
)
636 stree
= last_ptr_list((struct ptr_list
*)*stack
);
637 delete_ptr_list_last((struct ptr_list
**)stack
);
641 void free_slist(struct state_list
**slist
)
643 __free_ptr_list((struct ptr_list
**)slist
);
646 void free_stack(struct state_list_stack
**stack
)
648 __free_ptr_list((struct ptr_list
**)stack
);
651 void free_stack_and_slists(struct state_list_stack
**slist_stack
)
653 struct state_list
*slist
;
655 FOR_EACH_PTR(*slist_stack
, slist
) {
657 } END_FOR_EACH_PTR(slist
);
658 free_stack(slist_stack
);
661 void free_stree(struct AVL
**stree
)
666 void free_stree_stack(struct stree_stack
**stack
)
668 __free_ptr_list((struct ptr_list
**)stack
);
671 void free_stack_and_strees(struct stree_stack
**stree_stack
)
675 FOR_EACH_PTR(*stree_stack
, stree
) {
677 } END_FOR_EACH_PTR(stree
);
678 free_stree_stack(stree_stack
);
682 * set_state_stack() sets the state for the top slist on the stack.
684 struct sm_state
*set_state_stack(struct state_list_stack
**stack
, int owner
, const char *name
,
685 struct symbol
*sym
, struct smatch_state
*state
)
687 struct state_list
*slist
;
690 slist
= pop_slist(stack
);
691 sm
= set_state_slist(&slist
, owner
, name
, sym
, state
);
692 push_slist(stack
, slist
);
697 struct sm_state
*set_state_stree_stack(struct stree_stack
**stack
, int owner
, const char *name
,
698 struct symbol
*sym
, struct smatch_state
*state
)
703 stree
= pop_stree(stack
);
704 sm
= set_state_stree(&stree
, owner
, name
, sym
, state
);
705 push_stree(stack
, stree
);
711 * get_sm_state_stack() gets the state for the top slist on the stack.
713 struct sm_state
*get_sm_state_stack(struct state_list_stack
*stack
,
714 int owner
, const char *name
,
717 struct state_list
*slist
;
718 struct sm_state
*ret
;
720 slist
= pop_slist(&stack
);
721 ret
= get_sm_state_slist(slist
, owner
, name
, sym
);
722 push_slist(&stack
, slist
);
726 struct sm_state
*get_sm_state_stree_stack(struct stree_stack
*stack
,
727 int owner
, const char *name
,
731 struct sm_state
*ret
;
733 stree
= pop_stree(&stack
);
734 ret
= get_sm_state_stree(stree
, owner
, name
, sym
);
735 push_stree(&stack
, stree
);
739 struct smatch_state
*get_state_stack(struct state_list_stack
*stack
,
740 int owner
, const char *name
,
745 sm
= get_sm_state_stack(stack
, owner
, name
, sym
);
751 struct smatch_state
*get_state_stree_stack(struct stree_stack
*stack
,
752 int owner
, const char *name
,
757 sm
= get_sm_state_stree_stack(stack
, owner
, name
, sym
);
763 static void match_states(struct state_list
**one
, struct state_list
**two
)
765 struct sm_state
*one_sm
;
766 struct sm_state
*two_sm
;
767 struct sm_state
*tmp
;
768 struct smatch_state
*tmp_state
;
769 struct state_list
*add_to_one
= NULL
;
770 struct state_list
*add_to_two
= NULL
;
772 PREPARE_PTR_LIST(*one
, one_sm
);
773 PREPARE_PTR_LIST(*two
, two_sm
);
775 if (!one_sm
&& !two_sm
)
777 if (cmp_tracker(one_sm
, two_sm
) < 0) {
778 __set_fake_cur_slist_fast(*two
);
779 tmp_state
= __client_unmatched_state_function(one_sm
);
780 __pop_fake_cur_slist_fast();
781 tmp
= alloc_state_no_name(one_sm
->owner
, one_sm
->name
,
782 one_sm
->sym
, tmp_state
);
783 add_ptr_list(&add_to_two
, tmp
);
784 NEXT_PTR_LIST(one_sm
);
785 } else if (cmp_tracker(one_sm
, two_sm
) == 0) {
786 NEXT_PTR_LIST(one_sm
);
787 NEXT_PTR_LIST(two_sm
);
789 __set_fake_cur_slist_fast(*one
);
790 tmp_state
= __client_unmatched_state_function(two_sm
);
791 __pop_fake_cur_slist_fast();
792 tmp
= alloc_state_no_name(two_sm
->owner
, two_sm
->name
,
793 two_sm
->sym
, tmp_state
);
794 add_ptr_list(&add_to_one
, tmp
);
795 NEXT_PTR_LIST(two_sm
);
798 FINISH_PTR_LIST(two_sm
);
799 FINISH_PTR_LIST(one_sm
);
801 overwrite_slist(add_to_one
, one
);
802 overwrite_slist(add_to_two
, two
);
805 static void match_states_stree(struct AVL
**one
, struct AVL
**two
)
807 struct smatch_state
*tmp_state
;
808 struct sm_state
*tmp_sm
;
809 struct AVL
*add_to_one
= NULL
;
810 struct AVL
*add_to_two
= NULL
;
814 avl_iter_begin(&one_iter
, *one
, FORWARD
);
815 avl_iter_begin(&two_iter
, *two
, FORWARD
);
818 if (!one_iter
.sm
&& !two_iter
.sm
)
820 if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) < 0) {
821 __set_fake_cur_stree_fast(*two
);
822 tmp_state
= __client_unmatched_state_function(one_iter
.sm
);
823 __pop_fake_cur_stree_fast();
824 tmp_sm
= alloc_state_no_name(one_iter
.sm
->owner
, one_iter
.sm
->name
,
825 one_iter
.sm
->sym
, tmp_state
);
826 avl_insert(&add_to_two
, tmp_sm
);
827 avl_iter_next(&one_iter
);
828 } else if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) == 0) {
829 avl_iter_next(&one_iter
);
830 avl_iter_next(&two_iter
);
832 __set_fake_cur_stree_fast(*one
);
833 tmp_state
= __client_unmatched_state_function(two_iter
.sm
);
834 __pop_fake_cur_stree_fast();
835 tmp_sm
= alloc_state_no_name(two_iter
.sm
->owner
, two_iter
.sm
->name
,
836 two_iter
.sm
->sym
, tmp_state
);
837 avl_insert(&add_to_one
, tmp_sm
);
838 avl_iter_next(&two_iter
);
842 overwrite_stree(add_to_one
, one
);
843 overwrite_stree(add_to_two
, two
);
846 static void clone_pool_havers(struct state_list
*slist
)
849 struct sm_state
*new;
851 FOR_EACH_PTR(slist
, sm
) {
854 REPLACE_CURRENT_PTR(sm
, new);
856 } END_FOR_EACH_PTR(sm
);
859 static void clone_pool_havers_stree(struct AVL
*stree
)
863 avl_foreach(iter
, stree
) {
865 iter
.sm
= clone_sm(iter
.sm
);
871 * Sets the first state to the slist_id.
873 static void set_slist_id(struct state_list
*slist
)
875 struct smatch_state
*state
;
876 struct sm_state
*tmp
, *new;
878 state
= alloc_state_num(++__slist_id
);
879 new = alloc_sm_state(-1, "unnull_path", NULL
, state
);
881 FOR_EACH_PTR(slist
, tmp
) {
882 if (tmp
->owner
!= (unsigned short)-1)
884 REPLACE_CURRENT_PTR(tmp
, new);
886 } END_FOR_EACH_PTR(tmp
);
889 static void set_stree_id(struct AVL
*stree
)
891 struct smatch_state
*state
;
892 struct sm_state
*new;
894 /* FIXME: This is horrible. Anyway, slist_id should be a part of the
897 state
= alloc_state_num(++__slist_id
);
898 new = alloc_sm_state(-1, "unnull_path", NULL
, state
);
900 if (!avl_member(stree
, new))
902 avl_insert(&stree
, new);
905 int get_slist_id(struct state_list
*slist
)
907 struct sm_state
*tmp
;
909 FOR_EACH_PTR(slist
, tmp
) {
910 if (tmp
->owner
!= (unsigned short)-1)
912 return PTR_INT(tmp
->state
->data
);
913 } END_FOR_EACH_PTR(tmp
);
917 int get_stree_id(struct AVL
*stree
)
921 sm
= get_sm_state_stree(stree
, -1, "unnull_path", NULL
);
923 return PTR_INT(sm
->state
->data
);
928 * merge_slist() is called whenever paths merge, such as after
929 * an if statement. It takes the two slists and creates one.
931 void merge_slist(struct state_list
**to
, struct state_list
*slist
)
933 struct sm_state
*one_sm
, *two_sm
, *tmp
;
934 struct state_list
*results
= NULL
;
935 struct state_list
*implied_one
= NULL
;
936 struct state_list
*implied_two
= NULL
;
944 /* merging a null and nonnull path gives you only the nonnull path */
949 *to
= clone_slist(slist
);
953 implied_one
= clone_slist(*to
);
954 implied_two
= clone_slist(slist
);
956 match_states(&implied_one
, &implied_two
);
958 clone_pool_havers(implied_one
);
959 clone_pool_havers(implied_two
);
961 set_slist_id(implied_one
);
962 set_slist_id(implied_two
);
964 PREPARE_PTR_LIST(implied_one
, one_sm
);
965 PREPARE_PTR_LIST(implied_two
, two_sm
);
967 if (!one_sm
&& !two_sm
)
969 if (cmp_tracker(one_sm
, two_sm
) < 0) {
970 sm_msg("error: Internal smatch error.");
971 NEXT_PTR_LIST(one_sm
);
972 } else if (cmp_tracker(one_sm
, two_sm
) == 0) {
973 if (one_sm
!= two_sm
) {
974 one_sm
->pool
= implied_one
;
975 two_sm
->pool
= implied_two
;
978 tmp
= merge_sm_states(one_sm
, two_sm
);
979 add_ptr_list(&results
, tmp
);
980 NEXT_PTR_LIST(one_sm
);
981 NEXT_PTR_LIST(two_sm
);
983 sm_msg("error: Internal smatch error.");
984 NEXT_PTR_LIST(two_sm
);
987 FINISH_PTR_LIST(two_sm
);
988 FINISH_PTR_LIST(one_sm
);
994 void merge_stree(struct AVL
**to
, struct AVL
*stree
)
996 struct AVL
*results
= NULL
;
997 struct AVL
*implied_one
= NULL
;
998 struct AVL
*implied_two
= NULL
;
1001 struct sm_state
*tmp_sm
;
1002 struct state_list
*implied_one_slist
;
1003 struct state_list
*implied_two_slist
;
1005 if (out_of_memory())
1008 /* merging a null and nonnull path gives you only the nonnull path */
1013 *to
= avl_clone(stree
);
1017 implied_one
= avl_clone(*to
);
1018 implied_two
= avl_clone(stree
);
1020 match_states_stree(&implied_one
, &implied_two
);
1022 clone_pool_havers_stree(implied_one
);
1023 clone_pool_havers_stree(implied_two
);
1025 set_stree_id(implied_one
);
1026 set_stree_id(implied_two
);
1028 implied_one_slist
= stree_to_slist(implied_one
);
1029 implied_two_slist
= stree_to_slist(implied_two
);
1031 avl_iter_begin(&one_iter
, implied_one
, FORWARD
);
1032 avl_iter_begin(&two_iter
, implied_two
, FORWARD
);
1035 if (!one_iter
.sm
&& !two_iter
.sm
)
1037 if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) < 0) {
1038 sm_msg("error: Internal smatch error.");
1039 avl_iter_next(&one_iter
);
1040 } else if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) == 0) {
1041 if (one_iter
.sm
!= two_iter
.sm
) {
1042 one_iter
.sm
->pool
= implied_one_slist
;
1043 two_iter
.sm
->pool
= implied_two_slist
;
1045 tmp_sm
= merge_sm_states(one_iter
.sm
, two_iter
.sm
);
1046 avl_insert(&results
, tmp_sm
);
1047 avl_iter_next(&one_iter
);
1048 avl_iter_next(&two_iter
);
1050 sm_msg("error: Internal smatch error.");
1051 avl_iter_next(&two_iter
);
1060 * filter_slist() removes any sm states "slist" holds in common with "filter"
1062 void filter_slist(struct state_list
**slist
, struct state_list
*filter
)
1064 struct sm_state
*one_sm
, *two_sm
;
1065 struct state_list
*results
= NULL
;
1067 PREPARE_PTR_LIST(*slist
, one_sm
);
1068 PREPARE_PTR_LIST(filter
, two_sm
);
1070 if (!one_sm
&& !two_sm
)
1072 if (cmp_tracker(one_sm
, two_sm
) < 0) {
1073 add_ptr_list(&results
, one_sm
);
1074 NEXT_PTR_LIST(one_sm
);
1075 } else if (cmp_tracker(one_sm
, two_sm
) == 0) {
1076 if (one_sm
!= two_sm
)
1077 add_ptr_list(&results
, one_sm
);
1078 NEXT_PTR_LIST(one_sm
);
1079 NEXT_PTR_LIST(two_sm
);
1081 NEXT_PTR_LIST(two_sm
);
1084 FINISH_PTR_LIST(two_sm
);
1085 FINISH_PTR_LIST(one_sm
);
1091 void filter_stree(struct AVL
**stree
, struct AVL
*filter
)
1093 struct AVL
*results
= NULL
;
1097 avl_iter_begin(&one_iter
, *stree
, FORWARD
);
1098 avl_iter_begin(&two_iter
, filter
, FORWARD
);
1100 /* FIXME: This should probably be re-written with trees in mind */
1103 if (!one_iter
.sm
&& !two_iter
.sm
)
1105 if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) < 0) {
1106 avl_insert(&results
, one_iter
.sm
);
1107 avl_iter_next(&one_iter
);
1108 } else if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) == 0) {
1109 if (one_iter
.sm
!= two_iter
.sm
)
1110 avl_insert(&results
, one_iter
.sm
);
1111 avl_iter_next(&one_iter
);
1112 avl_iter_next(&two_iter
);
1114 avl_iter_next(&two_iter
);
1124 * and_slist_stack() pops the top two slists, overwriting the one with
1125 * the other and pushing it back on the stack.
1127 void and_slist_stack(struct state_list_stack
**slist_stack
)
1129 struct sm_state
*tmp
;
1130 struct state_list
*right_slist
= pop_slist(slist_stack
);
1132 FOR_EACH_PTR(right_slist
, tmp
) {
1133 overwrite_sm_state_stack(slist_stack
, tmp
);
1134 } END_FOR_EACH_PTR(tmp
);
1135 free_slist(&right_slist
);
1138 void and_stree_stack(struct stree_stack
**stack
)
1140 struct sm_state
*tmp
;
1141 struct AVL
*right_stree
= pop_stree(stack
);
1143 FOR_EACH_SM(right_stree
, tmp
) {
1144 overwrite_sm_state_stree_stack(stack
, tmp
);
1145 } END_FOR_EACH_SM(tmp
);
1146 free_stree(&right_stree
);
1150 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
1151 * It pops the two slists from the top of the stack and merges them
1152 * together in a way that preserves the things they have in common
1153 * but creates a merged state for most of the rest.
1154 * You could have code that had: if (foo || foo) { foo->baz;
1155 * It's this function which ensures smatch does the right thing.
1157 void or_slist_stack(struct state_list_stack
**pre_conds
,
1158 struct state_list
*cur_slist
,
1159 struct state_list_stack
**slist_stack
)
1161 struct state_list
*new;
1162 struct state_list
*old
;
1163 struct state_list
*pre_slist
;
1164 struct state_list
*res
;
1165 struct state_list
*tmp_slist
;
1167 new = pop_slist(slist_stack
);
1168 old
= pop_slist(slist_stack
);
1170 pre_slist
= pop_slist(pre_conds
);
1171 push_slist(pre_conds
, clone_slist(pre_slist
));
1173 res
= clone_slist(pre_slist
);
1174 overwrite_slist(old
, &res
);
1176 tmp_slist
= clone_slist(cur_slist
);
1177 overwrite_slist(new, &tmp_slist
);
1179 merge_slist(&res
, tmp_slist
);
1180 filter_slist(&res
, pre_slist
);
1182 push_slist(slist_stack
, res
);
1183 free_slist(&tmp_slist
);
1184 free_slist(&pre_slist
);
1189 void or_stree_stack(struct stree_stack
**pre_conds
,
1190 struct AVL
*cur_stree
,
1191 struct stree_stack
**stack
)
1195 struct AVL
*pre_stree
;
1197 struct AVL
*tmp_stree
;
1199 new = pop_stree(stack
);
1200 old
= pop_stree(stack
);
1202 pre_stree
= pop_stree(pre_conds
);
1203 push_stree(pre_conds
, avl_clone(pre_stree
));
1205 res
= avl_clone(pre_stree
);
1206 overwrite_stree(old
, &res
);
1208 tmp_stree
= avl_clone(cur_stree
);
1209 overwrite_stree(new, &tmp_stree
);
1211 merge_stree(&res
, tmp_stree
);
1212 filter_stree(&res
, pre_stree
);
1214 push_stree(stack
, res
);
1215 free_stree(&tmp_stree
);
1216 free_stree(&pre_stree
);
1222 * get_slist_from_named_stack() is only used for gotos.
1224 struct state_list
**get_slist_from_named_stack(struct named_stack
*stack
,
1227 struct named_slist
*tmp
;
1229 FOR_EACH_PTR(stack
, tmp
) {
1230 if (!strcmp(tmp
->name
, name
))
1232 } END_FOR_EACH_PTR(tmp
);
1236 struct AVL
**get_named_stree(struct named_stree_stack
*stack
,
1239 struct named_stree
*tmp
;
1241 FOR_EACH_PTR(stack
, tmp
) {
1242 if (!strcmp(tmp
->name
, name
))
1244 } END_FOR_EACH_PTR(tmp
);
1248 /* FIXME: These parameters are in a different order from expected */
1249 void overwrite_slist(struct state_list
*from
, struct state_list
**to
)
1251 struct sm_state
*tmp
;
1253 FOR_EACH_PTR(from
, tmp
) {
1254 overwrite_sm_state(to
, tmp
);
1255 } END_FOR_EACH_PTR(tmp
);
1258 void overwrite_stree(struct AVL
*from
, struct AVL
**to
)
1260 struct sm_state
*tmp
;
1262 FOR_EACH_SM(from
, tmp
) {
1263 overwrite_sm_state_stree(to
, tmp
);
1264 } END_FOR_EACH_SM(tmp
);
1267 struct state_list
*stree_to_slist(struct AVL
*stree
)
1269 struct state_list
*ret
= NULL
;
1270 struct sm_state
*tmp
;
1272 FOR_EACH_SM(stree
, tmp
) {
1273 overwrite_sm_state(&ret
, tmp
);
1274 } END_FOR_EACH_SM(tmp
);
1279 struct AVL
*slist_to_stree(struct state_list
*slist
)
1281 struct AVL
*ret
= NULL
;
1282 struct sm_state
*tmp
;
1284 FOR_EACH_PTR(slist
, tmp
) {
1285 overwrite_sm_state_stree(&ret
, tmp
);
1286 } END_FOR_EACH_PTR(tmp
);