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_pools
= NULL
;
165 sm_state
->pre_merge
= NULL
;
166 sm_state
->possible
= NULL
;
167 add_ptr_list(&sm_state
->possible
, sm_state
);
171 static void free_sm_state(struct sm_state
*sm
)
173 free_slist(&sm
->possible
);
174 free_slist(&sm
->pre_merge
);
175 free_stack(&sm
->my_pools
);
177 * fixme. Free the actual state.
178 * Right now we leave it until the end of the function
179 * because we don't want to double free it.
180 * Use the freelist to not double free things
184 static void free_all_sm_states(struct allocation_blob
*blob
)
186 unsigned int size
= sizeof(struct sm_state
);
187 unsigned int offset
= 0;
189 while (offset
< blob
->offset
) {
190 free_sm_state((struct sm_state
*)(blob
->data
+ offset
));
195 /* At the end of every function we free all the sm_states */
196 void free_every_single_sm_state(void)
198 struct allocator_struct
*desc
= &sm_state_allocator
;
199 struct allocation_blob
*blob
= desc
->blobs
;
202 desc
->allocations
= 0;
203 desc
->total_bytes
= 0;
204 desc
->useful_bytes
= 0;
205 desc
->freelist
= NULL
;
207 struct allocation_blob
*next
= blob
->next
;
208 free_all_sm_states(blob
);
209 blob_free(blob
, desc
->chunking
);
215 struct sm_state
*clone_state(struct sm_state
*s
)
217 struct sm_state
*ret
;
219 ret
= alloc_state_no_name(s
->name
, s
->owner
, s
->sym
, s
->state
);
221 ret
->merged
= s
->merged
;
222 ret
->my_pools
= clone_stack(s
->my_pools
);
223 ret
->possible
= clone_slist(s
->possible
);
224 ret
->pre_merge
= clone_slist(s
->pre_merge
);
228 int is_merged(struct sm_state
*sm
)
233 int slist_has_state(struct state_list
*slist
, struct smatch_state
*state
)
235 struct sm_state
*tmp
;
237 FOR_EACH_PTR(slist
, tmp
) {
238 if (tmp
->state
== state
)
240 } END_FOR_EACH_PTR(tmp
);
244 static void check_order(struct state_list
*slist
)
247 struct sm_state
*state
;
248 struct sm_state
*last
= NULL
;
251 FOR_EACH_PTR(slist
, state
) {
252 if (last
&& cmp_tracker(state
, last
) <= 0) {
253 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
254 "%s vs %s\n", last
->owner
, state
->owner
,
255 last
->sym
, state
->sym
, last
->name
, state
->name
);
259 } END_FOR_EACH_PTR(state
);
266 struct state_list
*clone_slist(struct state_list
*from_slist
)
268 struct sm_state
*state
;
269 struct state_list
*to_slist
= NULL
;
271 FOR_EACH_PTR(from_slist
, state
) {
272 add_ptr_list(&to_slist
, state
);
273 } END_FOR_EACH_PTR(state
);
274 check_order(to_slist
);
278 struct state_list
*clone_slist_and_states(struct state_list
*from_slist
)
280 struct sm_state
*state
;
281 struct sm_state
*tmp
;
282 struct state_list
*to_slist
= NULL
;
284 FOR_EACH_PTR(from_slist
, state
) {
285 tmp
= clone_state(state
);
286 add_ptr_list(&to_slist
, tmp
);
287 } END_FOR_EACH_PTR(state
);
288 check_order(to_slist
);
292 struct state_list_stack
*clone_stack(struct state_list_stack
*from_stack
)
294 struct state_list
*slist
;
295 struct state_list_stack
*to_stack
= NULL
;
297 FOR_EACH_PTR(from_stack
, slist
) {
298 push_slist(&to_stack
, slist
);
299 } END_FOR_EACH_PTR(slist
);
303 struct smatch_state
*merge_states(const char *name
, int owner
,
305 struct smatch_state
*state1
,
306 struct smatch_state
*state2
)
308 struct smatch_state
*ret
;
310 if (state1
== state2
)
312 else if (__has_merge_function(owner
))
313 ret
= __client_merge_function(owner
, name
, sym
, state1
, state2
);
314 else if (!state1
|| !state2
)
322 * add_pool() adds a slist to ->pools. If the slist has already been
323 * added earlier then it doesn't get added a second time.
325 void add_pool(struct state_list_stack
**pools
, struct state_list
*new)
327 struct state_list
*tmp
;
329 FOR_EACH_PTR(*pools
, tmp
) {
332 else if (tmp
== new) {
335 INSERT_CURRENT(new, tmp
);
338 } END_FOR_EACH_PTR(tmp
);
339 add_ptr_list(pools
, new);
342 void merge_pools(struct state_list_stack
**to
, struct state_list_stack
*from
)
344 struct state_list
*tmp
;
346 FOR_EACH_PTR(from
, tmp
) {
348 } END_FOR_EACH_PTR(tmp
);
351 struct sm_state
*merge_sm_states(struct sm_state
*one
, struct sm_state
*two
)
353 struct smatch_state
*s
;
354 struct sm_state
*result
;
358 s
= merge_states(one
->name
, one
->owner
, one
->sym
, one
->state
,
359 (two
?two
->state
:NULL
));
360 result
= alloc_state_no_name(one
->name
, one
->owner
, one
->sym
, s
);
361 if (two
&& one
->line
== two
->line
)
362 result
->line
= one
->line
;
364 add_ptr_list(&result
->pre_merge
, one
);
365 add_ptr_list(&result
->pre_merge
, two
);
366 add_possible(result
, one
);
367 add_possible(result
, two
);
370 struct sm_state
*tmp
;
373 printf("%d merge name='%s' owner=%d: %s + %s => %s (",
374 get_lineno(), one
->name
, one
->owner
,
375 show_state(one
->state
), show_state(two
?two
->state
:NULL
),
378 FOR_EACH_PTR(result
->possible
, tmp
) {
382 printf("%s", show_state(tmp
->state
));
383 } END_FOR_EACH_PTR(tmp
);
390 struct sm_state
*get_sm_state_slist(struct state_list
*slist
, const char *name
,
391 int owner
, struct symbol
*sym
)
393 struct sm_state
*state
;
398 FOR_EACH_PTR(slist
, state
) {
399 if (state
->owner
== owner
&& state
->sym
== sym
400 && !strcmp(state
->name
, name
))
402 } END_FOR_EACH_PTR(state
);
406 struct smatch_state
*get_state_slist(struct state_list
*slist
,
407 const char *name
, int owner
,
410 struct sm_state
*state
;
412 state
= get_sm_state_slist(slist
, name
, owner
, sym
);
418 void overwrite_sm_state(struct state_list
**slist
, struct sm_state
*new)
420 struct sm_state
*tmp
;
422 FOR_EACH_PTR(*slist
, tmp
) {
423 if (cmp_tracker(tmp
, new) < 0)
425 else if (cmp_tracker(tmp
, new) == 0) {
426 REPLACE_CURRENT_PTR(tmp
, new);
429 INSERT_CURRENT(new, tmp
);
432 } END_FOR_EACH_PTR(tmp
);
433 add_ptr_list(slist
, new);
436 void overwrite_sm_state_stack(struct state_list_stack
**stack
,
437 struct sm_state
*state
)
439 struct state_list
*slist
;
441 slist
= pop_slist(stack
);
442 overwrite_sm_state(&slist
, state
);
443 push_slist(stack
, slist
);
446 void set_state_slist(struct state_list
**slist
, const char *name
, int owner
,
447 struct symbol
*sym
, struct smatch_state
*state
)
449 struct sm_state
*tmp
;
450 struct sm_state
*new = alloc_state(name
, owner
, sym
, state
);
452 FOR_EACH_PTR(*slist
, tmp
) {
453 if (cmp_tracker(tmp
, new) < 0)
455 else if (cmp_tracker(tmp
, new) == 0) {
456 REPLACE_CURRENT_PTR(tmp
, new);
459 INSERT_CURRENT(new, tmp
);
462 } END_FOR_EACH_PTR(tmp
);
463 add_ptr_list(slist
, new);
466 void delete_state_slist(struct state_list
**slist
, const char *name
, int owner
,
469 struct sm_state
*state
;
471 FOR_EACH_PTR(*slist
, state
) {
472 if (state
->owner
== owner
&& state
->sym
== sym
473 && !strcmp(state
->name
, name
)){
474 DELETE_CURRENT_PTR(state
);
477 } END_FOR_EACH_PTR(state
);
481 void push_slist(struct state_list_stack
**list_stack
, struct state_list
*slist
)
483 add_ptr_list(list_stack
, slist
);
486 struct state_list
*pop_slist(struct state_list_stack
**list_stack
)
488 struct state_list
*slist
;
490 slist
= last_ptr_list((struct ptr_list
*)*list_stack
);
491 delete_ptr_list_last((struct ptr_list
**)list_stack
);
495 void free_slist(struct state_list
**slist
)
497 __free_ptr_list((struct ptr_list
**)slist
);
500 void free_stack(struct state_list_stack
**stack
)
502 __free_ptr_list((struct ptr_list
**)stack
);
505 void free_stack_and_slists(struct state_list_stack
**slist_stack
)
507 struct state_list
*slist
;
509 FOR_EACH_PTR(*slist_stack
, slist
) {
511 } END_FOR_EACH_PTR(slist
);
512 free_stack(slist_stack
);
516 * set_state_stack() sets the state for the top slist on the stack.
518 void set_state_stack(struct state_list_stack
**stack
, const char *name
,
519 int owner
, struct symbol
*sym
, struct smatch_state
*state
)
521 struct state_list
*slist
;
523 slist
= pop_slist(stack
);
524 set_state_slist(&slist
, name
, owner
, sym
, state
);
525 push_slist(stack
, slist
);
529 * get_sm_state_stack() gets the state for the top slist on the stack.
531 struct sm_state
*get_sm_state_stack(struct state_list_stack
*stack
,
532 const char *name
, int owner
,
535 struct state_list
*slist
;
536 struct sm_state
*ret
;
538 slist
= pop_slist(&stack
);
539 ret
= get_sm_state_slist(slist
, name
, owner
, sym
);
540 push_slist(&stack
, slist
);
545 struct smatch_state
*get_state_stack(struct state_list_stack
*stack
,
546 const char *name
, int owner
,
549 struct sm_state
*state
;
551 state
= get_sm_state_stack(stack
, name
, owner
, sym
);
557 static void match_states(struct state_list
**one
, struct state_list
**two
)
559 struct sm_state
*one_state
;
560 struct sm_state
*two_state
;
561 struct sm_state
*tmp
;
562 struct smatch_state
*tmp_state
;
563 struct state_list
*add_to_one
= NULL
;
564 struct state_list
*add_to_two
= NULL
;
566 PREPARE_PTR_LIST(*one
, one_state
);
567 PREPARE_PTR_LIST(*two
, two_state
);
569 if (!one_state
&& !two_state
)
571 if (cmp_tracker(one_state
, two_state
) < 0) {
572 tmp_state
= __client_unmatched_state_function(one_state
);
573 tmp
= alloc_state_no_name(one_state
->name
,
575 one_state
->sym
, tmp_state
);
576 add_ptr_list(&add_to_two
, tmp
);
577 NEXT_PTR_LIST(one_state
);
578 } else if (cmp_tracker(one_state
, two_state
) == 0) {
579 NEXT_PTR_LIST(one_state
);
580 NEXT_PTR_LIST(two_state
);
582 tmp_state
= __client_unmatched_state_function(two_state
);
583 tmp
= alloc_state_no_name(two_state
->name
,
585 two_state
->sym
, tmp_state
);
586 add_ptr_list(&add_to_one
, tmp
);
587 NEXT_PTR_LIST(two_state
);
590 FINISH_PTR_LIST(two_state
);
591 FINISH_PTR_LIST(one_state
);
593 overwrite_slist(add_to_one
, one
);
594 overwrite_slist(add_to_two
, two
);
598 * merge_slist() is called whenever paths merge, such as after
599 * an if statement. It takes the two slists and creates one.
601 void merge_slist(struct state_list
**to
, struct state_list
*slist
)
603 struct sm_state
*to_state
, *state
, *tmp
;
604 struct state_list
*results
= NULL
;
605 struct state_list
*implied_to
= NULL
;
606 struct state_list
*implied_from
= NULL
;
611 /* merging a null and nonnull path gives you only the nonnull path */
616 *to
= clone_slist(slist
);
620 implied_to
= clone_slist(*to
);
621 implied_from
= clone_slist(slist
);
623 match_states(&implied_to
, &implied_from
);
625 PREPARE_PTR_LIST(implied_to
, to_state
);
626 PREPARE_PTR_LIST(implied_from
, state
);
628 if (!to_state
&& !state
)
630 if (cmp_tracker(to_state
, state
) < 0) {
631 smatch_msg("error: Internal smatch error.");
632 NEXT_PTR_LIST(to_state
);
633 } else if (cmp_tracker(to_state
, state
) == 0) {
634 if (to_state
!= state
) {
635 add_pool(&to_state
->my_pools
, implied_to
);
636 add_pool(&state
->my_pools
, implied_from
);
639 tmp
= merge_sm_states(to_state
, state
);
640 add_ptr_list(&results
, tmp
);
641 NEXT_PTR_LIST(to_state
);
642 NEXT_PTR_LIST(state
);
644 smatch_msg("error: Internal smatch error.");
645 NEXT_PTR_LIST(state
);
648 FINISH_PTR_LIST(state
);
649 FINISH_PTR_LIST(to_state
);
656 * and_slist_stack() is basically the same as popping the top two slists,
657 * overwriting the one with the other and pushing it back on the stack.
658 * The difference is that it checks to see that a mutually exclusive
659 * state isn't included in both stacks. If smatch sees something like
660 * "if (a && !a)" it prints a warning.
662 void and_slist_stack(struct state_list_stack
**slist_stack
)
664 struct sm_state
*tmp
;
665 struct state_list
*right_slist
= pop_slist(slist_stack
);
667 FOR_EACH_PTR(right_slist
, tmp
) {
668 overwrite_sm_state_stack(slist_stack
, tmp
);
669 } END_FOR_EACH_PTR(tmp
);
670 free_slist(&right_slist
);
674 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
675 * It pops the two slists from the top of the stack and merges them
676 * together in a way that preserves the things they have in common
677 * but creates a merged state for most of the rest.
678 * You could have code that had: if (foo || foo) { foo->baz;
679 * It's this function which ensures smatch does the right thing.
681 void or_slist_stack(struct state_list_stack
**pre_conds
,
682 struct state_list
*cur_slist
,
683 struct state_list_stack
**slist_stack
)
685 struct state_list
*new;
686 struct state_list
*old
;
687 struct state_list
*res
= NULL
;
688 struct state_list
*tmp_slist
;
690 new = pop_slist(slist_stack
);
691 old
= pop_slist(slist_stack
);
693 tmp_slist
= pop_slist(pre_conds
);
694 res
= clone_slist(tmp_slist
);
695 push_slist(pre_conds
, tmp_slist
);
696 overwrite_slist(old
, &res
);
698 tmp_slist
= clone_slist(cur_slist
);
699 overwrite_slist(new, &tmp_slist
);
701 merge_slist(&res
, tmp_slist
);
703 push_slist(slist_stack
, res
);
704 free_slist(&tmp_slist
);
710 * get_slist_from_named_stack() is only used for gotos.
712 struct state_list
**get_slist_from_named_stack(struct named_stack
*stack
,
715 struct named_slist
*tmp
;
717 FOR_EACH_PTR(stack
, tmp
) {
718 if (!strcmp(tmp
->name
, name
))
720 } END_FOR_EACH_PTR(tmp
);
724 void overwrite_slist(struct state_list
*from
, struct state_list
**to
)
726 struct sm_state
*tmp
;
728 FOR_EACH_PTR(from
, tmp
) {
729 overwrite_sm_state(to
, tmp
);
730 } END_FOR_EACH_PTR(tmp
);
733 unsigned int __get_allocations()
735 return sm_state_allocator
.allocations
;