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);
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("%d merge [%s] '%s' %s(L %d) + %s(L %d) => %s (",
381 get_lineno(), check_name(one
->owner
), one
->name
,
382 show_state(one
->state
), one
->line
,
383 show_state(two
->state
), two
->line
,
386 FOR_EACH_PTR(result
->possible
, tmp
) {
389 printf("%s", show_state(tmp
->state
));
390 } END_FOR_EACH_PTR(tmp
);
397 struct sm_state
*get_sm_state_stree(struct stree
*stree
, int owner
, const char *name
,
400 struct tracker tracker
= {
402 .name
= (char *)name
,
410 return avl_lookup(stree
, (struct sm_state
*)&tracker
);
413 struct smatch_state
*get_state_stree(struct stree
*stree
,
414 int owner
, const char *name
,
419 sm
= get_sm_state_stree(stree
, owner
, name
, sym
);
425 /* FIXME: this is almost exactly the same as set_sm_state_slist() */
426 void overwrite_sm_state_stree(struct stree
**stree
, struct sm_state
*new)
428 avl_insert(stree
, new);
431 void overwrite_sm_state_stree_stack(struct stree_stack
**stack
,
436 stree
= pop_stree(stack
);
437 overwrite_sm_state_stree(&stree
, sm
);
438 push_stree(stack
, stree
);
441 struct sm_state
*set_state_stree(struct stree
**stree
, int owner
, const char *name
,
442 struct symbol
*sym
, struct smatch_state
*state
)
444 struct sm_state
*new = alloc_sm_state(owner
, name
, sym
, state
);
446 avl_insert(stree
, new);
450 void delete_state_stree(struct stree
**stree
, int owner
, const char *name
,
453 struct tracker tracker
= {
455 .name
= (char *)name
,
459 avl_remove(stree
, (struct sm_state
*)&tracker
);
462 void delete_state_stree_stack(struct stree_stack
**stack
, int owner
, const char *name
,
467 stree
= pop_stree(stack
);
468 delete_state_stree(&stree
, owner
, name
, sym
);
469 push_stree(stack
, stree
);
472 void push_stree(struct stree_stack
**stack
, struct stree
*stree
)
474 add_ptr_list(stack
, stree
);
477 struct stree
*pop_stree(struct stree_stack
**stack
)
481 stree
= last_ptr_list((struct ptr_list
*)*stack
);
482 delete_ptr_list_last((struct ptr_list
**)stack
);
486 void free_slist(struct state_list
**slist
)
488 __free_ptr_list((struct ptr_list
**)slist
);
491 void free_stree_stack(struct stree_stack
**stack
)
493 __free_ptr_list((struct ptr_list
**)stack
);
496 void free_stack_and_strees(struct stree_stack
**stree_stack
)
500 FOR_EACH_PTR(*stree_stack
, stree
) {
502 } END_FOR_EACH_PTR(stree
);
503 free_stree_stack(stree_stack
);
506 struct sm_state
*set_state_stree_stack(struct stree_stack
**stack
, int owner
, const char *name
,
507 struct symbol
*sym
, struct smatch_state
*state
)
512 stree
= pop_stree(stack
);
513 sm
= set_state_stree(&stree
, owner
, name
, sym
, state
);
514 push_stree(stack
, stree
);
520 * get_sm_state_stack() gets the state for the top slist on the stack.
522 struct sm_state
*get_sm_state_stree_stack(struct stree_stack
*stack
,
523 int owner
, const char *name
,
527 struct sm_state
*ret
;
529 stree
= pop_stree(&stack
);
530 ret
= get_sm_state_stree(stree
, owner
, name
, sym
);
531 push_stree(&stack
, stree
);
535 struct smatch_state
*get_state_stree_stack(struct stree_stack
*stack
,
536 int owner
, const char *name
,
541 sm
= get_sm_state_stree_stack(stack
, owner
, name
, sym
);
547 static void match_states_stree(struct stree
**one
, struct stree
**two
)
549 struct smatch_state
*tmp_state
;
550 struct sm_state
*tmp_sm
;
551 struct stree
*add_to_one
= NULL
;
552 struct stree
*add_to_two
= NULL
;
556 avl_iter_begin(&one_iter
, *one
, FORWARD
);
557 avl_iter_begin(&two_iter
, *two
, FORWARD
);
560 if (!one_iter
.sm
&& !two_iter
.sm
)
562 if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) < 0) {
563 __set_fake_cur_stree_fast(*two
);
564 tmp_state
= __client_unmatched_state_function(one_iter
.sm
);
565 __pop_fake_cur_stree_fast();
566 tmp_sm
= alloc_state_no_name(one_iter
.sm
->owner
, one_iter
.sm
->name
,
567 one_iter
.sm
->sym
, tmp_state
);
568 avl_insert(&add_to_two
, tmp_sm
);
569 avl_iter_next(&one_iter
);
570 } else if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) == 0) {
571 avl_iter_next(&one_iter
);
572 avl_iter_next(&two_iter
);
574 __set_fake_cur_stree_fast(*one
);
575 tmp_state
= __client_unmatched_state_function(two_iter
.sm
);
576 __pop_fake_cur_stree_fast();
577 tmp_sm
= alloc_state_no_name(two_iter
.sm
->owner
, two_iter
.sm
->name
,
578 two_iter
.sm
->sym
, tmp_state
);
579 avl_insert(&add_to_one
, tmp_sm
);
580 avl_iter_next(&two_iter
);
584 overwrite_stree(add_to_one
, one
);
585 overwrite_stree(add_to_two
, two
);
588 static void clone_pool_havers_stree(struct stree
**stree
)
590 struct sm_state
*sm
, *tmp
;
591 struct state_list
*slist
= NULL
;
593 FOR_EACH_SM(*stree
, sm
) {
596 add_ptr_list(&slist
, tmp
);
598 } END_FOR_EACH_SM(sm
);
600 FOR_EACH_PTR(slist
, sm
) {
601 avl_insert(stree
, sm
);
602 } END_FOR_EACH_PTR(sm
);
610 * merge_slist() is called whenever paths merge, such as after
611 * an if statement. It takes the two slists and creates one.
613 void merge_stree(struct stree
**to
, struct stree
*stree
)
615 struct stree
*results
= NULL
;
616 struct stree
*implied_one
= NULL
;
617 struct stree
*implied_two
= NULL
;
620 struct sm_state
*tmp_sm
;
625 /* merging a null and nonnull path gives you only the nonnull path */
630 *to
= clone_stree(stree
);
634 implied_one
= clone_stree(*to
);
635 implied_two
= clone_stree(stree
);
637 match_states_stree(&implied_one
, &implied_two
);
639 clone_pool_havers_stree(&implied_one
);
640 clone_pool_havers_stree(&implied_two
);
642 set_stree_id(implied_one
, ++__stree_id
);
643 set_stree_id(implied_two
, ++__stree_id
);
645 push_stree(&all_pools
, implied_one
);
646 push_stree(&all_pools
, implied_two
);
648 avl_iter_begin(&one_iter
, implied_one
, FORWARD
);
649 avl_iter_begin(&two_iter
, implied_two
, FORWARD
);
652 if (!one_iter
.sm
&& !two_iter
.sm
)
654 if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) < 0) {
655 sm_msg("error: Internal smatch error.");
656 avl_iter_next(&one_iter
);
657 } else if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) == 0) {
658 if (one_iter
.sm
!= two_iter
.sm
) {
659 one_iter
.sm
->pool
= implied_one
;
660 two_iter
.sm
->pool
= implied_two
;
662 tmp_sm
= merge_sm_states(one_iter
.sm
, two_iter
.sm
);
663 avl_insert(&results
, tmp_sm
);
664 avl_iter_next(&one_iter
);
665 avl_iter_next(&two_iter
);
667 sm_msg("error: Internal smatch error.");
668 avl_iter_next(&two_iter
);
677 * filter_slist() removes any sm states "slist" holds in common with "filter"
679 void filter_stree(struct stree
**stree
, struct stree
*filter
)
681 struct stree
*results
= NULL
;
685 avl_iter_begin(&one_iter
, *stree
, FORWARD
);
686 avl_iter_begin(&two_iter
, filter
, FORWARD
);
688 /* FIXME: This should probably be re-written with trees in mind */
691 if (!one_iter
.sm
&& !two_iter
.sm
)
693 if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) < 0) {
694 avl_insert(&results
, one_iter
.sm
);
695 avl_iter_next(&one_iter
);
696 } else if (cmp_tracker(one_iter
.sm
, two_iter
.sm
) == 0) {
697 if (one_iter
.sm
!= two_iter
.sm
)
698 avl_insert(&results
, one_iter
.sm
);
699 avl_iter_next(&one_iter
);
700 avl_iter_next(&two_iter
);
702 avl_iter_next(&two_iter
);
712 * and_slist_stack() pops the top two slists, overwriting the one with
713 * the other and pushing it back on the stack.
715 void and_stree_stack(struct stree_stack
**stack
)
717 struct sm_state
*tmp
;
718 struct stree
*right_stree
= pop_stree(stack
);
720 FOR_EACH_SM(right_stree
, tmp
) {
721 overwrite_sm_state_stree_stack(stack
, tmp
);
722 } END_FOR_EACH_SM(tmp
);
723 free_stree(&right_stree
);
727 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
728 * It pops the two slists from the top of the stack and merges them
729 * together in a way that preserves the things they have in common
730 * but creates a merged state for most of the rest.
731 * You could have code that had: if (foo || foo) { foo->baz;
732 * It's this function which ensures smatch does the right thing.
734 void or_stree_stack(struct stree_stack
**pre_conds
,
735 struct stree
*cur_stree
,
736 struct stree_stack
**stack
)
740 struct stree
*pre_stree
;
742 struct stree
*tmp_stree
;
744 new = pop_stree(stack
);
745 old
= pop_stree(stack
);
747 pre_stree
= pop_stree(pre_conds
);
748 push_stree(pre_conds
, clone_stree(pre_stree
));
750 res
= clone_stree(pre_stree
);
751 overwrite_stree(old
, &res
);
753 tmp_stree
= clone_stree(cur_stree
);
754 overwrite_stree(new, &tmp_stree
);
756 merge_stree(&res
, tmp_stree
);
757 filter_stree(&res
, pre_stree
);
759 push_stree(stack
, res
);
760 free_stree(&tmp_stree
);
761 free_stree(&pre_stree
);
767 * get_named_stree() is only used for gotos.
769 struct stree
**get_named_stree(struct named_stree_stack
*stack
,
772 struct named_stree
*tmp
;
774 FOR_EACH_PTR(stack
, tmp
) {
775 if (!strcmp(tmp
->name
, name
))
777 } END_FOR_EACH_PTR(tmp
);
781 /* FIXME: These parameters are in a different order from expected */
782 void overwrite_stree(struct stree
*from
, struct stree
**to
)
784 struct sm_state
*tmp
;
786 FOR_EACH_SM(from
, tmp
) {
787 overwrite_sm_state_stree(to
, tmp
);
788 } END_FOR_EACH_SM(tmp
);