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 void __print_slist(struct state_list
*slist
)
24 struct sm_state
*state
;
25 struct sm_state
*poss
;
28 printf("dumping slist at %d\n", get_lineno());
29 FOR_EACH_PTR(slist
, state
) {
30 printf("%d '%s'=%s (", state
->owner
, state
->name
,
31 show_state(state
->state
));
33 FOR_EACH_PTR(state
->possible
, poss
) {
36 printf("%s", show_state(poss
->state
));
37 } END_FOR_EACH_PTR(poss
);
39 } END_FOR_EACH_PTR(state
);
44 /* NULL states go at the end to simplify merge_slist */
45 int cmp_tracker(const struct sm_state
*a
, const struct sm_state
*b
)
56 if (a
->owner
> b
->owner
)
58 if (a
->owner
< b
->owner
)
61 ret
= strcmp(a
->name
, b
->name
);
65 if (!b
->sym
&& a
->sym
)
67 if (!a
->sym
&& b
->sym
)
77 static int cmp_sm_states(const struct sm_state
*a
, const struct sm_state
*b
)
81 ret
= cmp_tracker(a
, b
);
85 /* todo: add hook for smatch_extra.c */
86 if (a
->state
> b
->state
)
88 if (a
->state
< b
->state
)
93 static struct sm_state
*alloc_state_no_name(const char *name
, int owner
,
95 struct smatch_state
*state
)
99 tmp
= alloc_state(NULL
, owner
, sym
, state
);
104 void add_sm_state_slist(struct state_list
**slist
, struct sm_state
*new)
106 struct sm_state
*tmp
;
108 FOR_EACH_PTR(*slist
, tmp
) {
109 if (cmp_sm_states(tmp
, new) < 0)
111 else if (cmp_sm_states(tmp
, new) == 0) {
114 INSERT_CURRENT(new, tmp
);
117 } END_FOR_EACH_PTR(tmp
);
118 add_ptr_list(slist
, new);
121 static void add_possible(struct sm_state
*sm
, struct sm_state
*new)
123 struct sm_state
*tmp
;
124 struct sm_state
*tmp2
;
127 struct smatch_state
*s
;
129 s
= merge_states(sm
->name
, sm
->owner
, sm
->sym
, sm
->state
, NULL
);
130 tmp
= alloc_state_no_name(sm
->name
, sm
->owner
, sm
->sym
, s
);
131 add_sm_state_slist(&sm
->possible
, tmp
);
135 FOR_EACH_PTR(new->possible
, tmp
) {
136 tmp2
= alloc_state_no_name(tmp
->name
, tmp
->owner
, tmp
->sym
,
138 add_sm_state_slist(&sm
->possible
, tmp2
);
139 } END_FOR_EACH_PTR(tmp
);
142 char *alloc_sname(const char *str
)
148 tmp
= __alloc_sname(strlen(str
) + 1);
153 struct sm_state
*alloc_state(const char *name
, int owner
,
154 struct symbol
*sym
, struct smatch_state
*state
)
156 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
->my_pool
= NULL
;
165 sm_state
->pre_left
= NULL
;
166 sm_state
->pre_right
= NULL
;
167 sm_state
->possible
= NULL
;
168 add_ptr_list(&sm_state
->possible
, sm_state
);
172 static void free_sm_state(struct sm_state
*sm
)
174 free_slist(&sm
->possible
);
176 * fixme. Free the actual state.
177 * Right now we leave it until the end of the function
178 * because we don't want to double free it.
179 * Use the freelist to not double free things
183 static void free_all_sm_states(struct allocation_blob
*blob
)
185 unsigned int size
= sizeof(struct sm_state
);
186 unsigned int offset
= 0;
188 while (offset
< blob
->offset
) {
189 free_sm_state((struct sm_state
*)(blob
->data
+ offset
));
194 /* At the end of every function we free all the sm_states */
195 void free_every_single_sm_state(void)
197 struct allocator_struct
*desc
= &sm_state_allocator
;
198 struct allocation_blob
*blob
= desc
->blobs
;
201 desc
->allocations
= 0;
202 desc
->total_bytes
= 0;
203 desc
->useful_bytes
= 0;
204 desc
->freelist
= NULL
;
206 struct allocation_blob
*next
= blob
->next
;
207 free_all_sm_states(blob
);
208 blob_free(blob
, desc
->chunking
);
214 struct sm_state
*clone_state(struct sm_state
*s
)
216 struct sm_state
*ret
;
218 ret
= alloc_state_no_name(s
->name
, s
->owner
, s
->sym
, s
->state
);
220 ret
->merged
= s
->merged
;
221 /* clone_state() doesn't copy the my_pools. Each state needs to have
223 ret
->possible
= clone_slist(s
->possible
);
224 ret
->pre_left
= s
->pre_left
;
225 ret
->pre_right
= s
->pre_right
;
229 int is_merged(struct sm_state
*sm
)
234 int slist_has_state(struct state_list
*slist
, struct smatch_state
*state
)
236 struct sm_state
*tmp
;
238 FOR_EACH_PTR(slist
, tmp
) {
239 if (tmp
->state
== state
)
241 } END_FOR_EACH_PTR(tmp
);
245 static void check_order(struct state_list
*slist
)
248 struct sm_state
*state
;
249 struct sm_state
*last
= NULL
;
252 FOR_EACH_PTR(slist
, state
) {
253 if (last
&& cmp_tracker(state
, last
) <= 0) {
254 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
255 "%s vs %s\n", last
->owner
, state
->owner
,
256 last
->sym
, state
->sym
, last
->name
, state
->name
);
260 } END_FOR_EACH_PTR(state
);
267 struct state_list
*clone_slist(struct state_list
*from_slist
)
269 struct sm_state
*state
;
270 struct state_list
*to_slist
= NULL
;
272 FOR_EACH_PTR(from_slist
, state
) {
273 add_ptr_list(&to_slist
, state
);
274 } END_FOR_EACH_PTR(state
);
275 check_order(to_slist
);
279 struct state_list
*clone_slist_and_states(struct state_list
*from_slist
)
281 struct sm_state
*state
;
282 struct sm_state
*tmp
;
283 struct state_list
*to_slist
= NULL
;
285 FOR_EACH_PTR(from_slist
, state
) {
286 tmp
= clone_state(state
);
287 add_ptr_list(&to_slist
, tmp
);
288 } END_FOR_EACH_PTR(state
);
289 check_order(to_slist
);
293 struct state_list_stack
*clone_stack(struct state_list_stack
*from_stack
)
295 struct state_list
*slist
;
296 struct state_list_stack
*to_stack
= NULL
;
298 FOR_EACH_PTR(from_stack
, slist
) {
299 push_slist(&to_stack
, slist
);
300 } END_FOR_EACH_PTR(slist
);
304 struct smatch_state
*merge_states(const char *name
, int owner
,
306 struct smatch_state
*state1
,
307 struct smatch_state
*state2
)
309 struct smatch_state
*ret
;
311 if (state1
== state2
)
313 else if (__has_merge_function(owner
))
314 ret
= __client_merge_function(owner
, name
, sym
, state1
, state2
);
315 else if (!state1
|| !state2
)
323 * add_pool() adds a slist to ->pools. If the slist has already been
324 * added earlier then it doesn't get added a second time.
326 void add_pool(struct state_list_stack
**pools
, struct state_list
*new)
328 struct state_list
*tmp
;
330 FOR_EACH_PTR(*pools
, tmp
) {
333 else if (tmp
== new) {
336 INSERT_CURRENT(new, tmp
);
339 } END_FOR_EACH_PTR(tmp
);
340 add_ptr_list(pools
, new);
343 struct sm_state
*merge_sm_states(struct sm_state
*one
, struct sm_state
*two
)
345 struct smatch_state
*s
;
346 struct sm_state
*result
;
350 s
= merge_states(one
->name
, one
->owner
, one
->sym
, one
->state
,
351 (two
?two
->state
:NULL
));
352 result
= alloc_state_no_name(one
->name
, one
->owner
, one
->sym
, s
);
353 if (two
&& one
->line
== two
->line
)
354 result
->line
= one
->line
;
356 result
->pre_left
= one
;
357 result
->pre_right
= two
;
358 add_possible(result
, one
);
359 add_possible(result
, two
);
362 struct sm_state
*tmp
;
365 printf("%d merge name='%s' owner=%d: %s + %s => %s (",
366 get_lineno(), one
->name
, one
->owner
,
367 show_state(one
->state
), show_state(two
?two
->state
:NULL
),
370 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
, const char *name
,
383 int owner
, struct symbol
*sym
)
385 struct sm_state
*state
;
390 FOR_EACH_PTR(slist
, state
) {
391 if (state
->owner
== owner
&& state
->sym
== sym
392 && !strcmp(state
->name
, name
))
394 } END_FOR_EACH_PTR(state
);
398 struct smatch_state
*get_state_slist(struct state_list
*slist
,
399 const char *name
, int owner
,
402 struct sm_state
*state
;
404 state
= get_sm_state_slist(slist
, name
, owner
, sym
);
410 void overwrite_sm_state(struct state_list
**slist
, struct sm_state
*new)
412 struct sm_state
*tmp
;
414 FOR_EACH_PTR(*slist
, tmp
) {
415 if (cmp_tracker(tmp
, new) < 0)
417 else if (cmp_tracker(tmp
, new) == 0) {
418 REPLACE_CURRENT_PTR(tmp
, new);
421 INSERT_CURRENT(new, tmp
);
424 } END_FOR_EACH_PTR(tmp
);
425 add_ptr_list(slist
, new);
428 void overwrite_sm_state_stack(struct state_list_stack
**stack
,
429 struct sm_state
*state
)
431 struct state_list
*slist
;
433 slist
= pop_slist(stack
);
434 overwrite_sm_state(&slist
, state
);
435 push_slist(stack
, slist
);
438 void set_state_slist(struct state_list
**slist
, const char *name
, int owner
,
439 struct symbol
*sym
, struct smatch_state
*state
)
441 struct sm_state
*tmp
;
442 struct sm_state
*new = alloc_state(name
, owner
, sym
, state
);
444 FOR_EACH_PTR(*slist
, tmp
) {
445 if (cmp_tracker(tmp
, new) < 0)
447 else if (cmp_tracker(tmp
, new) == 0) {
448 REPLACE_CURRENT_PTR(tmp
, new);
451 INSERT_CURRENT(new, tmp
);
454 } END_FOR_EACH_PTR(tmp
);
455 add_ptr_list(slist
, new);
458 void delete_state_slist(struct state_list
**slist
, const char *name
, int owner
,
461 struct sm_state
*state
;
463 FOR_EACH_PTR(*slist
, state
) {
464 if (state
->owner
== owner
&& state
->sym
== sym
465 && !strcmp(state
->name
, name
)){
466 DELETE_CURRENT_PTR(state
);
469 } END_FOR_EACH_PTR(state
);
473 void push_slist(struct state_list_stack
**list_stack
, struct state_list
*slist
)
475 add_ptr_list(list_stack
, slist
);
478 struct state_list
*pop_slist(struct state_list_stack
**list_stack
)
480 struct state_list
*slist
;
482 slist
= last_ptr_list((struct ptr_list
*)*list_stack
);
483 delete_ptr_list_last((struct ptr_list
**)list_stack
);
487 void free_slist(struct state_list
**slist
)
489 __free_ptr_list((struct ptr_list
**)slist
);
492 void free_stack(struct state_list_stack
**stack
)
494 __free_ptr_list((struct ptr_list
**)stack
);
497 void free_stack_and_slists(struct state_list_stack
**slist_stack
)
499 struct state_list
*slist
;
501 FOR_EACH_PTR(*slist_stack
, slist
) {
503 } END_FOR_EACH_PTR(slist
);
504 free_stack(slist_stack
);
508 * set_state_stack() sets the state for the top slist on the stack.
510 void set_state_stack(struct state_list_stack
**stack
, const char *name
,
511 int owner
, struct symbol
*sym
, struct smatch_state
*state
)
513 struct state_list
*slist
;
515 slist
= pop_slist(stack
);
516 set_state_slist(&slist
, name
, owner
, sym
, state
);
517 push_slist(stack
, slist
);
521 * get_sm_state_stack() gets the state for the top slist on the stack.
523 struct sm_state
*get_sm_state_stack(struct state_list_stack
*stack
,
524 const char *name
, int owner
,
527 struct state_list
*slist
;
528 struct sm_state
*ret
;
530 slist
= pop_slist(&stack
);
531 ret
= get_sm_state_slist(slist
, name
, owner
, sym
);
532 push_slist(&stack
, slist
);
537 struct smatch_state
*get_state_stack(struct state_list_stack
*stack
,
538 const char *name
, int owner
,
541 struct sm_state
*state
;
543 state
= get_sm_state_stack(stack
, name
, owner
, sym
);
549 static void match_states(struct state_list
**one
, struct state_list
**two
)
551 struct sm_state
*one_state
;
552 struct sm_state
*two_state
;
553 struct sm_state
*tmp
;
554 struct smatch_state
*tmp_state
;
555 struct state_list
*add_to_one
= NULL
;
556 struct state_list
*add_to_two
= NULL
;
558 PREPARE_PTR_LIST(*one
, one_state
);
559 PREPARE_PTR_LIST(*two
, two_state
);
561 if (!one_state
&& !two_state
)
563 if (cmp_tracker(one_state
, two_state
) < 0) {
564 tmp_state
= __client_unmatched_state_function(one_state
);
565 tmp
= alloc_state_no_name(one_state
->name
,
567 one_state
->sym
, tmp_state
);
568 add_ptr_list(&add_to_two
, tmp
);
569 NEXT_PTR_LIST(one_state
);
570 } else if (cmp_tracker(one_state
, two_state
) == 0) {
571 NEXT_PTR_LIST(one_state
);
572 NEXT_PTR_LIST(two_state
);
574 tmp_state
= __client_unmatched_state_function(two_state
);
575 tmp
= alloc_state_no_name(two_state
->name
,
577 two_state
->sym
, tmp_state
);
578 add_ptr_list(&add_to_one
, tmp
);
579 NEXT_PTR_LIST(two_state
);
582 FINISH_PTR_LIST(two_state
);
583 FINISH_PTR_LIST(one_state
);
585 overwrite_slist(add_to_one
, one
);
586 overwrite_slist(add_to_two
, two
);
589 static void clone_modified(struct state_list
**one
, struct state_list
**two
)
591 struct sm_state
*one_state
;
592 struct sm_state
*two_state
;
593 struct sm_state
*clone
;
594 struct state_list
*add_to_one
= NULL
;
595 struct state_list
*add_to_two
= NULL
;
597 PREPARE_PTR_LIST(*one
, one_state
);
598 PREPARE_PTR_LIST(*two
, two_state
);
600 if (!one_state
&& !two_state
)
602 if (cmp_tracker(one_state
, two_state
) < 0) {
603 NEXT_PTR_LIST(one_state
);
604 } else if (cmp_tracker(one_state
, two_state
) == 0) {
605 if (one_state
!= two_state
) {
606 clone
= clone_state(one_state
);
607 add_ptr_list(&add_to_one
, clone
);
608 clone
= clone_state(two_state
);
609 add_ptr_list(&add_to_two
, clone
);
611 NEXT_PTR_LIST(one_state
);
612 NEXT_PTR_LIST(two_state
);
614 NEXT_PTR_LIST(two_state
);
617 FINISH_PTR_LIST(two_state
);
618 FINISH_PTR_LIST(one_state
);
620 overwrite_slist(add_to_one
, one
);
621 overwrite_slist(add_to_two
, two
);
624 static void clone_pool_havers(struct state_list
*slist
)
626 struct sm_state
*state
;
627 struct sm_state
*new;
629 FOR_EACH_PTR(slist
, state
) {
630 if (state
->my_pool
) {
631 new = clone_state(state
);
632 REPLACE_CURRENT_PTR(state
, new);
634 } END_FOR_EACH_PTR(state
);
638 * merge_slist() is called whenever paths merge, such as after
639 * an if statement. It takes the two slists and creates one.
641 static void __merge_slist(struct state_list
**to
, struct state_list
*slist
, int clone
)
643 struct sm_state
*to_state
, *state
, *tmp
;
644 struct state_list
*results
= NULL
;
645 struct state_list
*implied_to
= NULL
;
646 struct state_list
*implied_from
= NULL
;
651 /* merging a null and nonnull path gives you only the nonnull path */
656 *to
= clone_slist(slist
);
660 implied_to
= clone_slist(*to
);
661 implied_from
= clone_slist(slist
);
663 match_states(&implied_to
, &implied_from
);
665 clone_modified(&implied_to
, &implied_from
);
666 clone_pool_havers(implied_to
);
667 clone_pool_havers(implied_from
);
669 PREPARE_PTR_LIST(implied_to
, to_state
);
670 PREPARE_PTR_LIST(implied_from
, state
);
672 if (!to_state
&& !state
)
674 if (cmp_tracker(to_state
, state
) < 0) {
675 smatch_msg("error: Internal smatch error.");
676 NEXT_PTR_LIST(to_state
);
677 } else if (cmp_tracker(to_state
, state
) == 0) {
678 if (to_state
!= state
) {
679 if (to_state
->my_pool
)
680 smatch_msg("to_state '%s' has pools", to_state
->name
);
682 smatch_msg("state '%s' has pools", state
->name
);
683 to_state
->my_pool
= implied_to
;
684 state
->my_pool
= implied_from
;
687 tmp
= merge_sm_states(to_state
, state
);
688 add_ptr_list(&results
, tmp
);
689 NEXT_PTR_LIST(to_state
);
690 NEXT_PTR_LIST(state
);
692 smatch_msg("error: Internal smatch error.");
693 NEXT_PTR_LIST(state
);
696 FINISH_PTR_LIST(state
);
697 FINISH_PTR_LIST(to_state
);
703 void merge_slist(struct state_list
**to
, struct state_list
*slist
)
705 __merge_slist(to
, slist
, 0);
708 void merge_slist_clone(struct state_list
**to
, struct state_list
*slist
)
710 __merge_slist(to
, slist
, 1);
714 * and_slist_stack() is basically the same as popping the top two slists,
715 * overwriting the one with the other and pushing it back on the stack.
716 * The difference is that it checks to see that a mutually exclusive
717 * state isn't included in both stacks. If smatch sees something like
718 * "if (a && !a)" it prints a warning.
720 void and_slist_stack(struct state_list_stack
**slist_stack
)
722 struct sm_state
*tmp
;
723 struct state_list
*right_slist
= pop_slist(slist_stack
);
725 FOR_EACH_PTR(right_slist
, tmp
) {
726 overwrite_sm_state_stack(slist_stack
, tmp
);
727 } END_FOR_EACH_PTR(tmp
);
728 free_slist(&right_slist
);
732 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
733 * It pops the two slists from the top of the stack and merges them
734 * together in a way that preserves the things they have in common
735 * but creates a merged state for most of the rest.
736 * You could have code that had: if (foo || foo) { foo->baz;
737 * It's this function which ensures smatch does the right thing.
739 void or_slist_stack(struct state_list_stack
**pre_conds
,
740 struct state_list
*cur_slist
,
741 struct state_list_stack
**slist_stack
)
743 struct state_list
*new;
744 struct state_list
*old
;
745 struct state_list
*res
= NULL
;
746 struct state_list
*tmp_slist
;
748 new = pop_slist(slist_stack
);
749 old
= pop_slist(slist_stack
);
751 tmp_slist
= pop_slist(pre_conds
);
752 res
= clone_slist(tmp_slist
);
753 push_slist(pre_conds
, tmp_slist
);
754 overwrite_slist(old
, &res
);
756 tmp_slist
= clone_slist(cur_slist
);
757 overwrite_slist(new, &tmp_slist
);
759 merge_slist(&res
, tmp_slist
);
761 push_slist(slist_stack
, res
);
762 free_slist(&tmp_slist
);
768 * get_slist_from_named_stack() is only used for gotos.
770 struct state_list
**get_slist_from_named_stack(struct named_stack
*stack
,
773 struct named_slist
*tmp
;
775 FOR_EACH_PTR(stack
, tmp
) {
776 if (!strcmp(tmp
->name
, name
))
778 } END_FOR_EACH_PTR(tmp
);
782 void overwrite_slist(struct state_list
*from
, struct state_list
**to
)
784 struct sm_state
*tmp
;
786 FOR_EACH_PTR(from
, tmp
) {
787 overwrite_sm_state(to
, tmp
);
788 } END_FOR_EACH_PTR(tmp
);