2 * Copyright (C) 2006,2008 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
24 #include "smatch_expression_stacks.h"
25 #include "smatch_extra.h"
26 #include "smatch_slist.h"
29 int __in_fake_struct_assign
;
31 int __in_fake_var_assign
;
37 struct expression
*__inline_fn
;
39 int __smatch_lineno
= 0;
41 static char *base_file
;
42 static const char *filename
;
43 static char *pathname
;
44 static char *full_filename
;
45 static char *full_base_file
;
46 static char *cur_func
;
48 static unsigned int loop_count
;
49 static int last_goto_statement_handled
;
50 int __expr_stmt_count
;
51 int __in_function_def
;
52 int __in_unmatched_hook
;
53 static struct expression_list
*switch_expr_stack
= NULL
;
54 static struct expression_list
*post_op_stack
= NULL
;
56 static struct ptr_list
*fn_data_list
;
57 static struct ptr_list
*backup
;
59 struct expression_list
*big_expression_stack
;
60 struct statement_list
*big_statement_stack
;
61 struct statement
*__prev_stmt
;
62 struct statement
*__cur_stmt
;
63 struct statement
*__next_stmt
;
64 static struct expression_list
*parsed_calls
;
65 static int indent_cnt
;
66 int __in_pre_condition
= 0;
67 int __bail_on_rest_of_function
= 0;
68 static struct timeval fn_start_time
;
69 static struct timeval outer_fn_start_time
;
70 char *get_function(void) { return cur_func
; }
71 int get_lineno(void) { return __smatch_lineno
; }
72 int inside_loop(void) { return !!loop_count
; }
73 int definitely_inside_loop(void) { return !!(loop_count
& ~0x08000000); }
74 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
75 int in_expression_statement(void) { return !!__expr_stmt_count
; }
77 static void split_symlist(struct symbol_list
*sym_list
);
78 static void split_declaration(struct symbol_list
*sym_list
);
79 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
);
80 static void split_args(struct expression
*expr
);
81 static struct expression
*fake_a_variable_assign(struct symbol
*type
, struct expression
*call
, struct expression
*expr
, int nr
);
82 static void add_inline_function(struct symbol
*sym
);
83 static void parse_inline(struct expression
*expr
);
85 int option_assume_loops
= 0;
86 int option_two_passes
= 0;
87 struct symbol
*cur_func_sym
= NULL
;
88 struct stree
*global_states
;
90 const unsigned long valid_ptr_min
= 4096;
91 unsigned long valid_ptr_max
= ULONG_MAX
& ~(MTAG_OFFSET_MASK
);
92 const sval_t valid_ptr_min_sval
= {
96 sval_t ptr_err_min
= { .type
= &ptr_ctype
};
97 sval_t ptr_err_max
= { .type
= &ptr_ctype
};
98 sval_t ulong_ULONG_MAX
= { .type
= &ulong_ctype
};
100 sval_t valid_ptr_max_sval
= {
102 {.value
= ULONG_MAX
& ~(MTAG_OFFSET_MASK
)},
104 struct range_list
*valid_ptr_rl
;
106 void alloc_ptr_constants(void)
108 valid_ptr_max
= sval_type_max(&ulong_ctype
).value
& ~(MTAG_OFFSET_MASK
);
109 valid_ptr_max_sval
.value
= valid_ptr_max
;
111 valid_ptr_rl
= alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
112 valid_ptr_rl
= cast_rl(&ptr_ctype
, valid_ptr_rl
);
113 valid_ptr_rl
= clone_rl_permanent(valid_ptr_rl
);
115 ptr_err_min
= sval_cast(&ptr_ctype
, err_min
);
116 ptr_err_max
= sval_cast(&ptr_ctype
, err_max
);
117 ulong_ULONG_MAX
= sval_type_max(&ulong_ctype
);
120 int outside_of_function(void)
122 return cur_func_sym
== NULL
;
125 const char *get_filename(void)
127 if (option_info
&& option_full_path
)
128 return full_base_file
;
131 if (option_full_path
)
132 return full_filename
;
136 const char *get_base_file(void)
138 if (option_full_path
)
139 return full_base_file
;
143 unsigned long long get_file_id(void)
145 return str_to_llu_hash(get_filename());
148 unsigned long long get_base_file_id(void)
150 return str_to_llu_hash(get_base_file());
153 static void set_position(struct position pos
)
156 static int prev_stream
= -1;
161 if (pos
.stream
== 0 && pos
.line
== 0)
164 __smatch_lineno
= pos
.line
;
166 if (pos
.stream
== prev_stream
)
169 filename
= stream_name(pos
.stream
);
172 pathname
= getcwd(NULL
, 0);
174 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
175 full_filename
= malloc(len
);
176 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
178 full_filename
= alloc_string(filename
);
183 int is_assigned_call(struct expression
*expr
)
185 struct expression
*parent
= expr_get_parent_expr(expr
);
188 parent
->type
== EXPR_ASSIGNMENT
&&
190 strip_expr(parent
->right
) == expr
)
196 int is_fake_assigned_call(struct expression
*expr
)
198 struct expression
*parent
= expr_get_fake_parent_expr(expr
);
201 parent
->type
== EXPR_ASSIGNMENT
&&
203 strip_expr(parent
->right
) == expr
)
209 static bool is_inline_func(struct expression
*expr
)
211 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
213 if (!expr
->symbol
->definition
)
215 if (expr
->symbol
->definition
->ctype
.modifiers
& MOD_INLINE
)
221 static int is_noreturn_func(struct expression
*expr
)
223 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
227 * It's almost impossible for Smatch to handle __builtin_constant_p()
228 * the same way that GCC does so Smatch ends up making some functions
229 * as no return functions incorrectly.
232 if (option_project
== PROJ_KERNEL
&& expr
->symbol
->ident
&&
233 strstr(expr
->symbol
->ident
->name
, "__compiletime_assert"))
236 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
241 static int save_func_time(void *_rl
, int argc
, char **argv
, char **azColName
)
243 unsigned long *rl
= _rl
;
245 *rl
= strtoul(argv
[0], NULL
, 10);
249 static int get_func_time(struct symbol
*sym
)
251 unsigned long time
= 0;
253 run_sql(&save_func_time
, &time
,
254 "select key from return_implies where %s and type = %d;",
255 get_static_filter(sym
), FUNC_TIME
);
260 static int inline_budget
= 20;
262 int inlinable(struct expression
*expr
)
265 struct statement
*last_stmt
= NULL
;
267 if (__inline_fn
) /* don't nest */
270 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
272 if (is_no_inline_function(expr
->symbol
->ident
->name
))
274 sym
= get_base_type(expr
->symbol
);
275 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
276 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
278 if (sym
->stmt
->type
!= STMT_COMPOUND
)
280 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
282 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
283 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
285 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
287 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
293 /* the magic numbers in this function are pulled out of my bum. */
294 if (last_stmt
->pos
.line
> sym
->pos
.line
+ inline_budget
)
297 if (get_func_time(expr
->symbol
) >= 2)
303 void __process_post_op_stack(void)
305 struct expression
*expr
;
307 FOR_EACH_PTR(post_op_stack
, expr
) {
308 __pass_to_client(expr
, OP_HOOK
);
309 } END_FOR_EACH_PTR(expr
);
311 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
314 static int handle_comma_assigns(struct expression
*expr
)
316 struct expression
*right
;
317 struct expression
*assign
;
319 right
= strip_expr(expr
->right
);
320 if (right
->type
!= EXPR_COMMA
)
323 __split_expr(right
->left
);
324 __process_post_op_stack();
326 assign
= assign_expression(expr
->left
, '=', right
->right
);
327 __split_expr(assign
);
332 /* This is to handle *p++ = foo; assignments */
333 static int handle_postop_assigns(struct expression
*expr
)
335 struct expression
*left
, *fake_left
;
336 struct expression
*assign
;
338 left
= strip_expr(expr
->left
);
339 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '*')
341 left
= strip_expr(left
->unop
);
342 if (left
->type
!= EXPR_POSTOP
)
345 fake_left
= deref_expression(strip_expr(left
->unop
));
346 assign
= assign_expression(fake_left
, '=', expr
->right
);
348 __split_expr(assign
);
349 __split_expr(expr
->left
);
354 static bool parent_is_dereference(struct expression
*expr
)
356 struct expression
*parent
;
359 while ((parent
= expr_get_parent_expr(parent
))) {
360 if (parent
->type
== EXPR_DEREF
)
362 if (parent
->type
== EXPR_PREOP
&&
370 static int prev_expression_is_getting_address(struct expression
*expr
)
372 struct expression
*parent
;
375 parent
= expr_get_parent_expr(expr
);
379 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&') {
380 if (parent_is_dereference(parent
))
384 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
386 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
388 /* Handle &foo->array[offset] */
389 if (parent
->type
== EXPR_BINOP
&& parent
->op
== '+') {
390 parent
= expr_get_parent_expr(parent
);
393 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '*')
403 int __in_builtin_overflow_func
;
404 static void handle_builtin_overflow_func(struct expression
*expr
)
406 struct expression
*a
, *b
, *res
, *assign
;
409 if (sym_name_is("__builtin_add_overflow", expr
->fn
))
411 else if (sym_name_is("__builtin_sub_overflow", expr
->fn
))
413 else if (sym_name_is("__builtin_mul_overflow", expr
->fn
))
418 a
= get_argument_from_call_expr(expr
->args
, 0);
419 b
= get_argument_from_call_expr(expr
->args
, 1);
420 res
= get_argument_from_call_expr(expr
->args
, 2);
422 assign
= assign_expression(deref_expression(res
), '=', binop_expression(a
, op
, b
));
424 __in_builtin_overflow_func
++;
425 __split_expr(assign
);
426 __in_builtin_overflow_func
--;
429 static int handle__builtin_choose_expr(struct expression
*expr
)
431 struct expression
*const_expr
, *expr1
, *expr2
;
434 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
437 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
438 expr1
= get_argument_from_call_expr(expr
->args
, 1);
439 expr2
= get_argument_from_call_expr(expr
->args
, 2);
441 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
450 static int handle__builtin_choose_expr_assigns(struct expression
*expr
)
452 struct expression
*const_expr
, *right
, *expr1
, *expr2
, *fake
;
456 * We can't use strip_no_cast() because it strips out
457 * __builtin_choose_expr() which turns this function into a no-op.
460 right
= strip_parens(expr
->right
);
461 if (right
->type
!= EXPR_CALL
)
463 if (!sym_name_is("__builtin_choose_expr", right
->fn
))
466 const_expr
= get_argument_from_call_expr(right
->args
, 0);
467 expr1
= get_argument_from_call_expr(right
->args
, 1);
468 expr2
= get_argument_from_call_expr(right
->args
, 2);
470 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
473 fake
= assign_expression(expr
->left
, '=', sval
.value
? expr1
: expr2
);
478 int is_condition_call(struct expression
*expr
)
480 struct expression
*tmp
;
482 FOR_EACH_PTR_REVERSE(big_condition_stack
, tmp
) {
483 if (expr
== tmp
|| expr_get_parent_expr(expr
) == tmp
)
485 if (tmp
->pos
.line
< expr
->pos
.line
)
487 } END_FOR_EACH_PTR_REVERSE(tmp
);
492 static struct expression
*expr_get_parent_no_parens(struct expression
*expr
)
495 expr
= expr_get_parent_expr(expr
);
497 expr
->type
== EXPR_PREOP
&&
503 static bool gen_fake_function_assign(struct expression
*expr
)
505 static struct expression
*parsed
;
506 struct expression
*assign
, *parent
;
510 /* The rule is that every non-void function call has to be part of an
511 * assignment. TODO: Should we create a fake non-casted assignment
512 * for casted assignments? Also faked assigns for += assignments?
514 type
= get_type(expr
);
515 if (!type
|| type
== &void_ctype
)
518 parent
= expr_get_parent_no_parens(expr
);
519 if (parent
&& parent
->type
== EXPR_ASSIGNMENT
)
522 parent
= expr_get_fake_parent_expr(expr
);
524 struct expression
*left
= parent
->left
;
526 if (parent
== parsed
)
528 if (!left
|| left
->type
!= EXPR_SYMBOL
)
530 if (strncmp(left
->symbol_name
->name
, "__fake_assign_", 14) != 0)
533 __split_expr(parent
);
537 // TODO: faked_assign skipping conditions is a hack
538 if (is_condition_call(expr
))
541 snprintf(buf
, sizeof(buf
), "__fake_assign_%p", expr
);
542 assign
= create_fake_assign(buf
, get_type(expr
), expr
);
545 __split_expr(assign
);
549 static void split_call(struct expression
*expr
)
551 if (gen_fake_function_assign(expr
))
554 expr_set_parent_expr(expr
->fn
, expr
);
556 if (sym_name_is("__builtin_constant_p", expr
->fn
))
558 if (handle__builtin_choose_expr(expr
))
560 __split_expr(expr
->fn
);
562 if (is_inline_func(expr
->fn
))
563 add_inline_function(expr
->fn
->symbol
->definition
);
564 if (inlinable(expr
->fn
))
566 __process_post_op_stack();
567 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
568 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
570 if (inlinable(expr
->fn
))
572 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
573 if (is_noreturn_func(expr
->fn
))
575 if (!expr_get_parent_expr(expr
) && indent_cnt
== 1)
576 __discard_fake_states(expr
);
577 handle_builtin_overflow_func(expr
);
578 __add_ptr_list((struct ptr_list
**)&parsed_calls
, expr
);
581 static unsigned long skip_split
;
582 void parse_assignment(struct expression
*expr
, bool shallow
)
584 struct expression
*right
;
586 expr_set_parent_expr(expr
->left
, expr
);
587 expr_set_parent_expr(expr
->right
, expr
);
589 right
= strip_expr(expr
->right
);
596 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
599 if (__handle_condition_assigns(expr
))
601 /* foo = (x < 5 ? foo : 5); */
602 if (__handle_select_assigns(expr
))
604 /* foo = ({frob(); frob(); frob(); 1;}) */
605 if (__handle_expr_statement_assigns(expr
))
606 goto done
; // FIXME: goto after
608 if (handle_comma_assigns(expr
))
610 if (handle__builtin_choose_expr_assigns(expr
))
612 if (handle_postop_assigns(expr
))
613 goto done
; /* no need to goto after_assign */
615 __split_expr(expr
->right
);
616 if (outside_of_function())
617 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
619 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
622 // FIXME: the ordering of this is tricky
623 __fake_struct_member_assignments(expr
);
625 /* Re-examine ->right for inlines. See the commit message */
626 right
= strip_expr(expr
->right
);
627 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
628 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
631 if (get_macro_name(right
->pos
) &&
632 get_macro_name(expr
->left
->pos
) != get_macro_name(right
->pos
))
633 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
635 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
636 __split_expr(expr
->left
);
643 static bool skip_split_off(struct expression
*expr
)
645 if (expr
->type
== EXPR_CALL
&&
646 sym_name_is("__smatch_stop_skip", expr
->fn
))
651 void __split_expr(struct expression
*expr
)
656 if (skip_split_off(expr
))
665 // sm_msg("Debug expr_type %d %s expr = '%s'", expr->type, show_special(expr->op), expr_to_str(expr));
667 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
669 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
672 push_expression(&big_expression_stack
, expr
);
673 set_position(expr
->pos
);
674 __pass_to_client(expr
, EXPR_HOOK
);
676 switch (expr
->type
) {
678 expr_set_parent_expr(expr
->unop
, expr
);
680 __split_expr(expr
->unop
);
681 if (expr
->op
== '*' &&
682 !prev_expression_is_getting_address(expr
))
683 __pass_to_client(expr
, DEREF_HOOK
);
684 __pass_to_client(expr
, OP_HOOK
);
687 expr_set_parent_expr(expr
->unop
, expr
);
689 __split_expr(expr
->unop
);
690 push_expression(&post_op_stack
, expr
);
694 if (expr
->statement
&& !expr
->statement
) {
695 stmt_set_parent_stmt(expr
->statement
,
696 last_ptr_list((struct ptr_list
*)big_statement_stack
));
698 __split_stmt(expr
->statement
);
703 expr_set_parent_expr(expr
->left
, expr
);
704 expr_set_parent_expr(expr
->right
, expr
);
706 __pass_to_client(expr
, LOGIC_HOOK
);
707 __handle_logic(expr
);
710 expr_set_parent_expr(expr
->left
, expr
);
711 expr_set_parent_expr(expr
->right
, expr
);
713 __pass_to_client(expr
, BINOP_HOOK
);
714 __split_expr(expr
->left
);
715 __split_expr(expr
->right
);
718 expr_set_parent_expr(expr
->left
, expr
);
719 expr_set_parent_expr(expr
->right
, expr
);
721 __split_expr(expr
->left
);
722 __process_post_op_stack();
723 __split_expr(expr
->right
);
725 case EXPR_ASSIGNMENT
:
726 parse_assignment(expr
, false);
729 expr_set_parent_expr(expr
->deref
, expr
);
731 __split_expr(expr
->deref
);
732 __pass_to_client(expr
, DEREF_HOOK
);
735 expr_set_parent_expr(expr
->base
, expr
);
737 __split_expr(expr
->base
);
740 case EXPR_FORCE_CAST
:
741 expr_set_parent_expr(expr
->cast_expression
, expr
);
743 __pass_to_client(expr
, CAST_HOOK
);
744 __split_expr(expr
->cast_expression
);
747 if (expr
->cast_expression
)
748 __pass_to_client(strip_parens(expr
->cast_expression
),
754 case EXPR_CONDITIONAL
:
756 expr_set_parent_expr(expr
->conditional
, expr
);
757 expr_set_parent_expr(expr
->cond_true
, expr
);
758 expr_set_parent_expr(expr
->cond_false
, expr
);
760 if (known_condition_true(expr
->conditional
)) {
761 __split_expr(expr
->cond_true
);
764 if (known_condition_false(expr
->conditional
)) {
765 __split_expr(expr
->cond_false
);
768 __pass_to_client(expr
, SELECT_HOOK
);
769 __split_whole_condition(expr
->conditional
);
770 __split_expr(expr
->cond_true
);
771 __push_true_states();
772 __use_false_states();
773 __split_expr(expr
->cond_false
);
774 __merge_true_states();
779 case EXPR_INITIALIZER
:
780 split_expr_list(expr
->expr_list
, expr
);
782 case EXPR_IDENTIFIER
:
783 expr_set_parent_expr(expr
->ident_expression
, expr
);
784 __split_expr(expr
->ident_expression
);
787 expr_set_parent_expr(expr
->idx_expression
, expr
);
788 __split_expr(expr
->idx_expression
);
791 expr_set_parent_expr(expr
->init_expr
, expr
);
792 __split_expr(expr
->init_expr
);
795 __pass_to_client(expr
, SYM_HOOK
);
798 __pass_to_client(expr
, STRING_HOOK
);
801 struct expression
*tmp
;
803 tmp
= strip_Generic(expr
);
811 __pass_to_client(expr
, EXPR_HOOK_AFTER
);
812 pop_expression(&big_expression_stack
);
815 static int is_forever_loop(struct statement
*stmt
)
817 struct expression
*expr
;
820 expr
= strip_expr(stmt
->iterator_pre_condition
);
822 expr
= stmt
->iterator_post_condition
;
824 /* this is a for(;;) loop... */
828 if (get_value(expr
, &sval
) && sval
.value
!= 0)
835 static char *get_loop_name(int num
)
839 snprintf(buf
, 255, "-loop%d", num
);
841 return alloc_sname(buf
);
844 static struct bool_stmt_fn_list
*once_through_hooks
;
845 void add_once_through_hook(bool_stmt_func
*fn
)
847 add_ptr_list(&once_through_hooks
, fn
);
850 static bool call_once_through_hooks(struct statement
*stmt
)
854 if (option_assume_loops
)
857 FOR_EACH_PTR(once_through_hooks
, fn
) {
860 } END_FOR_EACH_PTR(fn
);
866 * Pre Loops are while and for loops.
868 static void handle_pre_loop(struct statement
*stmt
)
870 int once_through
; /* we go through the loop at least once */
871 struct sm_state
*extra_sm
= NULL
;
874 struct stree
*stree
= NULL
;
875 struct sm_state
*sm
= NULL
;
877 __push_scope_hooks();
879 loop_name
= get_loop_name(loop_num
);
882 split_declaration(stmt
->iterator_syms
);
883 if (stmt
->iterator_pre_statement
) {
884 __split_stmt(stmt
->iterator_pre_statement
);
885 __prev_stmt
= stmt
->iterator_pre_statement
;
892 __merge_gotos(loop_name
, NULL
);
894 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
895 __in_pre_condition
++;
896 __set_confidence_implied();
897 __split_whole_condition_tf(stmt
->iterator_pre_condition
, &once_through
);
898 __unset_confidence();
899 if (once_through
!= true)
900 once_through
= call_once_through_hooks(stmt
);
901 __pass_to_client(stmt
, PRELOOP_HOOK
);
902 __in_pre_condition
--;
903 FOR_EACH_SM(stree
, sm
) {
904 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
905 } END_FOR_EACH_SM(sm
);
908 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
910 __split_stmt(stmt
->iterator_statement
);
911 if (is_forever_loop(stmt
)) {
913 __save_gotos(loop_name
, NULL
);
915 __push_fake_cur_stree();
916 __split_stmt(stmt
->iterator_post_statement
);
917 stree
= __pop_fake_cur_stree();
919 __discard_false_states();
920 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
923 if (!__path_is_null())
924 __merge_stree_into_cur(stree
);
928 unchanged
= __iterator_unchanged(extra_sm
);
929 __split_stmt(stmt
->iterator_post_statement
);
930 __prev_stmt
= stmt
->iterator_post_statement
;
933 __save_gotos(loop_name
, NULL
);
934 __in_pre_condition
++;
935 __split_whole_condition(stmt
->iterator_pre_condition
);
936 __in_pre_condition
--;
938 __merge_false_states();
939 if (once_through
== true)
940 __discard_false_states();
942 __merge_false_states();
944 if (extra_sm
&& unchanged
)
945 __extra_pre_loop_hook_after(extra_sm
,
946 stmt
->iterator_post_statement
,
947 stmt
->iterator_pre_condition
);
948 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
953 __call_scope_hooks();
957 * Post loops are do {} while();
959 static void handle_post_loop(struct statement
*stmt
)
963 loop_name
= get_loop_name(loop_num
);
967 __pass_to_client(stmt
, POSTLOOP_HOOK
);
971 __merge_gotos(loop_name
, NULL
);
972 __split_stmt(stmt
->iterator_statement
);
974 if (!expr_is_zero(stmt
->iterator_post_condition
))
975 __save_gotos(loop_name
, NULL
);
977 if (is_forever_loop(stmt
)) {
978 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
981 __split_whole_condition(stmt
->iterator_post_condition
);
982 __use_false_states();
983 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
989 static int empty_statement(struct statement
*stmt
)
993 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
998 static int last_stmt_on_same_line(void)
1000 struct statement
*stmt
;
1003 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
1006 if (stmt
->pos
.line
== get_lineno())
1009 } END_FOR_EACH_PTR_REVERSE(stmt
);
1013 static void split_asm_ops(struct asm_operand_list
*ops
)
1015 struct asm_operand
*op
;
1017 FOR_EACH_PTR(ops
, op
) {
1018 __split_expr(op
->expr
);
1019 } END_FOR_EACH_PTR(op
);
1022 static int is_case_val(struct statement
*stmt
, sval_t sval
)
1026 if (stmt
->type
!= STMT_CASE
)
1028 if (!stmt
->case_expression
) {
1032 if (!get_value(stmt
->case_expression
, &case_sval
))
1034 if (case_sval
.value
== sval
.value
)
1039 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
1040 struct expression
*case_expr
,
1041 struct expression
*case_to
)
1044 struct range_list
*rl
= NULL
;
1045 struct symbol
*switch_type
;
1047 switch_type
= get_type(switch_expr
);
1048 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
1049 start
= sval_cast(switch_type
, start
);
1050 end
= sval_cast(switch_type
, end
);
1051 add_range(&rl
, start
, end
);
1052 } else if (get_value(case_expr
, &start
)) {
1053 start
= sval_cast(switch_type
, start
);
1054 add_range(&rl
, start
, start
);
1060 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
1062 struct statement
*tmp
;
1063 struct range_list
*rl
;
1065 __split_expr(stmt
->switch_expression
);
1066 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
1068 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1069 __save_switch_states(top_expression(switch_expr_stack
));
1074 stmt
= stmt
->switch_statement
;
1076 __push_scope_hooks();
1077 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
1078 __smatch_lineno
= tmp
->pos
.line
;
1079 // FIXME: what if default comes before the known case statement?
1080 if (is_case_val(tmp
, sval
)) {
1081 rl
= alloc_rl(sval
, sval
);
1082 __merge_switches(top_expression(switch_expr_stack
), rl
);
1083 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
1084 stmt_set_parent_stmt(tmp
->case_statement
, tmp
);
1085 __split_stmt(tmp
->case_statement
);
1088 if (__path_is_null())
1092 if (__path_is_null()) {
1096 } END_FOR_EACH_PTR(tmp
);
1098 __call_scope_hooks();
1099 if (!__pop_default())
1100 __merge_switches(top_expression(switch_expr_stack
), NULL
);
1101 __discard_switches();
1103 pop_expression(&switch_expr_stack
);
1106 static void split_case(struct statement
*stmt
)
1108 struct range_list
*rl
= NULL
;
1110 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
1111 expr_set_parent_stmt(stmt
->case_to
, stmt
);
1113 rl
= get_case_rl(top_expression(switch_expr_stack
),
1114 stmt
->case_expression
, stmt
->case_to
);
1115 while (stmt
->case_statement
->type
== STMT_CASE
) {
1116 struct range_list
*tmp
;
1118 tmp
= get_case_rl(top_expression(switch_expr_stack
),
1119 stmt
->case_statement
->case_expression
,
1120 stmt
->case_statement
->case_to
);
1123 rl
= rl_union(rl
, tmp
);
1124 if (!stmt
->case_expression
)
1127 stmt
= stmt
->case_statement
;
1130 __merge_switches(top_expression(switch_expr_stack
), rl
);
1132 if (!stmt
->case_expression
)
1135 stmt_set_parent_stmt(stmt
->case_statement
, stmt
);
1136 __split_stmt(stmt
->case_statement
);
1139 int time_parsing_function(void)
1141 return ms_since(&fn_start_time
) / 1000;
1144 bool taking_too_long(void)
1146 if ((ms_since(&outer_fn_start_time
) / 1000) > 60 * 5) /* five minutes */
1151 struct statement
*get_last_stmt(void)
1154 struct statement
*stmt
;
1156 fn
= get_base_type(cur_func_sym
);
1161 stmt
= fn
->inline_stmt
;
1162 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
1164 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
1165 if (stmt
&& stmt
->type
== STMT_LABEL
)
1166 stmt
= stmt
->label_statement
;
1170 int is_last_stmt(struct statement
*cur_stmt
)
1172 struct statement
*last
;
1174 last
= get_last_stmt();
1175 if (last
&& last
== cur_stmt
)
1180 static bool is_function_scope(struct statement
*stmt
)
1182 struct symbol
*base_type
;
1187 base_type
= get_base_type(cur_func_sym
);
1188 if (base_type
->stmt
== stmt
||
1189 base_type
->inline_stmt
== stmt
)
1195 static void handle_backward_goto(struct statement
*goto_stmt
)
1197 const char *goto_name
, *label_name
;
1198 struct statement
*func_stmt
;
1199 struct symbol
*base_type
= get_base_type(cur_func_sym
);
1200 struct statement
*tmp
;
1205 if (last_goto_statement_handled
)
1207 last_goto_statement_handled
= 1;
1209 if (!goto_stmt
->goto_label
||
1210 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
1211 !goto_stmt
->goto_label
->ident
)
1213 goto_name
= goto_stmt
->goto_label
->ident
->name
;
1215 func_stmt
= base_type
->stmt
;
1217 func_stmt
= base_type
->inline_stmt
;
1220 if (func_stmt
->type
!= STMT_COMPOUND
)
1223 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
1225 if (tmp
->type
!= STMT_LABEL
)
1227 if (!tmp
->label_identifier
||
1228 tmp
->label_identifier
->type
!= SYM_LABEL
||
1229 !tmp
->label_identifier
->ident
)
1231 label_name
= tmp
->label_identifier
->ident
->name
;
1232 if (strcmp(goto_name
, label_name
) != 0)
1237 } END_FOR_EACH_PTR(tmp
);
1240 static void fake_a_return(void)
1242 struct expression
*ret
= NULL
;
1247 if (cur_func_return_type() != &void_ctype
)
1248 ret
= unknown_value_expression(NULL
);
1250 __pass_to_client(ret
, RETURN_HOOK
);
1254 static void split_ret_value(struct expression
*expr
)
1256 struct symbol
*type
;
1261 type
= get_real_base_type(cur_func_sym
);
1262 type
= get_real_base_type(type
);
1263 expr
= fake_a_variable_assign(type
, NULL
, expr
, -1);
1265 __in_fake_var_assign
++;
1267 __in_fake_var_assign
--;
1270 static void fake_an_empty_default(struct position pos
)
1272 static struct statement none
= {};
1275 none
.type
= STMT_NONE
;
1276 __merge_switches(top_expression(switch_expr_stack
), NULL
);
1277 __split_stmt(&none
);
1280 static void split_compound(struct statement
*stmt
)
1282 struct statement
*prev
= NULL
;
1283 struct statement
*cur
= NULL
;
1284 struct statement
*next
;
1286 __push_scope_hooks();
1288 FOR_EACH_PTR(stmt
->stmts
, next
) {
1289 /* just set them all ahead of time */
1290 stmt_set_parent_stmt(next
, stmt
);
1300 } END_FOR_EACH_PTR(next
);
1309 * For function scope, then delay calling the scope hooks until the
1310 * end of function hooks can run.
1312 if (!is_function_scope(stmt
))
1313 __call_scope_hooks();
1316 void __split_label_stmt(struct statement
*stmt
)
1318 if (stmt
->label_identifier
&&
1319 stmt
->label_identifier
->type
== SYM_LABEL
&&
1320 stmt
->label_identifier
->ident
) {
1321 loop_count
|= 0x0800000;
1322 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
1326 static void find_asm_gotos(struct statement
*stmt
)
1330 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
1331 __save_gotos(sym
->ident
->name
, sym
);
1332 } END_FOR_EACH_PTR(sym
);
1335 static void split_if_statement(struct statement
*stmt
)
1339 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1340 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1341 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1343 if (empty_statement(stmt
->if_true
) &&
1344 last_stmt_on_same_line() &&
1345 !get_macro_name(stmt
->if_true
->pos
))
1346 sm_warning("if();");
1348 __split_whole_condition_tf(stmt
->if_conditional
, &known_tf
);
1349 if (known_tf
== true) {
1350 __split_stmt(stmt
->if_true
);
1351 __discard_false_states();
1353 } else if (known_tf
== false) {
1354 __use_false_states();
1355 __split_stmt(stmt
->if_false
);
1359 __split_stmt(stmt
->if_true
);
1360 __push_true_states();
1361 __use_false_states();
1362 __split_stmt(stmt
->if_false
);
1363 __merge_true_states();
1366 static bool already_parsed_call(struct expression
*call
)
1368 struct expression
*expr
;
1370 FOR_EACH_PTR(parsed_calls
, expr
) {
1373 } END_FOR_EACH_PTR(expr
);
1377 static void free_parsed_call_stuff(bool free_fake_states
)
1379 free_expression_stack(&parsed_calls
);
1380 if (free_fake_states
)
1381 __discard_fake_states(NULL
);
1384 void __split_stmt(struct statement
*stmt
)
1387 struct timeval start
, stop
;
1388 bool skip_after
= false;
1390 gettimeofday(&start
, NULL
);
1395 if (!__in_fake_assign
)
1396 __silence_warnings_for_stmt
= false;
1398 if (__bail_on_rest_of_function
|| is_skipped_function())
1401 if (out_of_memory() || taking_too_long()) {
1402 gettimeofday(&start
, NULL
);
1404 __bail_on_rest_of_function
= 1;
1406 sm_perror("Function too hairy. Giving up. %lu seconds",
1407 start
.tv_sec
- fn_start_time
.tv_sec
);
1409 final_pass
= 0; /* turn off sm_msg() from here */
1415 add_ptr_list(&big_statement_stack
, stmt
);
1416 free_expression_stack(&big_expression_stack
);
1417 free_parsed_call_stuff(indent_cnt
== 1);
1418 set_position(stmt
->pos
);
1419 __pass_to_client(stmt
, STMT_HOOK
);
1421 switch (stmt
->type
) {
1422 case STMT_DECLARATION
:
1423 split_declaration(stmt
->declaration
);
1426 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1428 split_ret_value(stmt
->ret_value
);
1429 __process_post_op_stack();
1430 __call_all_scope_hooks();
1431 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1434 case STMT_EXPRESSION
:
1435 expr_set_parent_stmt(stmt
->expression
, stmt
);
1436 expr_set_parent_stmt(stmt
->context
, stmt
);
1438 __split_expr(stmt
->expression
);
1441 split_compound(stmt
);
1444 split_if_statement(stmt
);
1447 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1448 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1449 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1450 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1451 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1453 if (stmt
->iterator_pre_condition
)
1454 handle_pre_loop(stmt
);
1455 else if (stmt
->iterator_post_condition
)
1456 handle_post_loop(stmt
);
1458 // these are for(;;) type loops.
1459 handle_pre_loop(stmt
);
1463 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1464 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1466 if (get_value(stmt
->switch_expression
, &sval
)) {
1467 split_known_switch(stmt
, sval
);
1470 __split_expr(stmt
->switch_expression
);
1471 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1472 __save_switch_states(top_expression(switch_expr_stack
));
1476 __split_stmt(stmt
->switch_statement
);
1477 if (!__pop_default() && have_remaining_cases())
1478 fake_an_empty_default(stmt
->pos
);
1479 __discard_switches();
1481 pop_expression(&switch_expr_stack
);
1487 __split_label_stmt(stmt
);
1488 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1490 __split_stmt(stmt
->label_statement
);
1493 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1495 __split_expr(stmt
->goto_expression
);
1496 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1497 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1499 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1501 __process_continues();
1503 } else if (stmt
->goto_label
&&
1504 stmt
->goto_label
->type
== SYM_LABEL
&&
1505 stmt
->goto_label
->ident
) {
1506 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1509 if (is_last_stmt(stmt
))
1510 handle_backward_goto(stmt
);
1515 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1517 find_asm_gotos(stmt
);
1518 __pass_to_client(stmt
, ASM_HOOK
);
1519 __split_expr(stmt
->asm_string
);
1520 split_asm_ops(stmt
->asm_outputs
);
1521 split_asm_ops(stmt
->asm_inputs
);
1522 split_expr_list(stmt
->asm_clobbers
, NULL
);
1527 __split_expr(stmt
->range_expression
);
1528 __split_expr(stmt
->range_low
);
1529 __split_expr(stmt
->range_high
);
1533 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1534 if (--indent_cnt
== 1)
1535 free_parsed_call_stuff(true);
1538 __process_post_op_stack();
1540 gettimeofday(&stop
, NULL
);
1541 if (option_time_stmt
&& stmt
)
1542 sm_msg("stmt_time%s: %ld",
1543 stmt
->type
== STMT_COMPOUND
? "_block" : "",
1544 stop
.tv_sec
- start
.tv_sec
);
1547 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1549 struct expression
*expr
;
1551 FOR_EACH_PTR(expr_list
, expr
) {
1552 expr_set_parent_expr(expr
, parent
);
1554 __process_post_op_stack();
1555 } END_FOR_EACH_PTR(expr
);
1558 static bool cast_arg(struct symbol
*type
, struct expression
*arg
)
1560 struct symbol
*orig
;
1565 arg
= strip_parens(arg
);
1566 if (arg
!= strip_expr(arg
))
1569 orig
= get_type(arg
);
1572 if (types_equiv(orig
, type
))
1575 if (orig
->type
== SYM_ARRAY
&& type
->type
== SYM_PTR
)
1579 * I would have expected that we could just do use (orig == type) but I
1580 * guess for pointers we need to get the basetype to do that comparison.
1584 if (orig
->type
!= SYM_PTR
||
1585 type
->type
!= SYM_PTR
) {
1586 if (type_fits(type
, orig
))
1590 orig
= get_real_base_type(orig
);
1591 type
= get_real_base_type(type
);
1598 static struct expression
*fake_a_variable_assign(struct symbol
*type
, struct expression
*call
, struct expression
*expr
, int nr
)
1603 if (!expr
|| !cur_func_sym
)
1606 if (already_parsed_call(call
))
1609 if (expr
->type
== EXPR_ASSIGNMENT
)
1612 /* for va_args then we don't know the type */
1614 type
= get_type(expr
);
1616 cast
= cast_arg(type
, expr
);
1618 * Using expr_to_sym() here is a hack. We want to say that we don't
1619 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1620 * It turns out faking these assignments is way more expensive than I
1621 * would have imagined. I'm not sure why exactly.
1626 * if the code is "return *p;" where "p" is a user pointer then
1627 * we want to create a fake assignment so that it sets the state
1628 * in check_kernel_user_data.c.
1631 if (expr
->type
!= EXPR_PREOP
&&
1632 expr
->op
!= '*' && expr
->op
!= '&' &&
1638 snprintf(buf
, sizeof(buf
), "__fake_return_%p", expr
);
1640 snprintf(buf
, sizeof(buf
), "__fake_param_%p_%d", call
, nr
);
1642 return create_fake_assign(buf
, type
, expr
);
1645 static void split_args(struct expression
*expr
)
1647 struct expression
*arg
, *tmp
;
1648 struct symbol
*type
;
1652 FOR_EACH_PTR(expr
->args
, arg
) {
1654 expr_set_parent_expr(arg
, expr
);
1655 type
= get_arg_type(expr
->fn
, i
);
1656 tmp
= fake_a_variable_assign(type
, expr
, arg
, i
);
1658 __in_fake_var_assign
++;
1661 __in_fake_var_assign
--;
1662 __process_post_op_stack();
1663 } END_FOR_EACH_PTR(arg
);
1666 static void call_cleanup_fn(void *_sym
)
1668 struct symbol
*sym
= _sym
;
1669 struct expression
*call
, *arg
;
1670 struct expression_list
*args
= NULL
;
1672 arg
= symbol_expression(sym
);
1673 arg
= preop_expression(arg
, '&');
1674 add_ptr_list(&args
, arg
);
1675 call
= call_expression(sym
->cleanup
, args
);
1680 static void add_cleanup_hook(struct symbol
*sym
)
1684 add_scope_hook(&call_cleanup_fn
, sym
);
1687 static void split_sym(struct symbol
*sym
)
1691 if (!(sym
->namespace & NS_SYMBOL
))
1694 __split_stmt(sym
->stmt
);
1695 __split_expr(sym
->array_size
);
1697 add_cleanup_hook(sym
);
1698 split_symlist(sym
->arguments
);
1699 split_symlist(sym
->symbol_list
);
1700 __split_stmt(sym
->inline_stmt
);
1701 split_symlist(sym
->inline_symbol_list
);
1704 static void split_symlist(struct symbol_list
*sym_list
)
1708 FOR_EACH_PTR(sym_list
, sym
) {
1710 } END_FOR_EACH_PTR(sym
);
1713 typedef void (fake_cb
)(struct expression
*expr
);
1715 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1717 struct symbol
*type
, *tmp
;
1723 name
= member
->name
;
1725 type
= get_type(expr
);
1726 if (!type
|| type
->type
!= SYM_STRUCT
)
1730 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1734 if (strcmp(name
, tmp
->ident
->name
) == 0)
1736 } END_FOR_EACH_PTR(tmp
);
1740 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1742 struct symbol
*type
, *member
;
1745 type
= get_type(expr
);
1746 if (!type
|| type
->type
!= SYM_STRUCT
)
1749 FOR_EACH_PTR(type
->symbol_list
, member
) {
1751 return member
->ident
;
1753 } END_FOR_EACH_PTR(member
);
1757 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1759 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1761 struct expression
*edge_member
, *assign
;
1762 struct symbol
*base
= get_real_base_type(member
);
1766 expr
= member_expression(expr
, '.', member
->ident
);
1768 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1769 struct symbol
*type
;
1771 type
= get_real_base_type(tmp
);
1775 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1776 if (get_extra_state(edge_member
))
1779 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1780 set_inner_struct_members(expr
, tmp
);
1787 assign
= assign_expression(edge_member
, '=', zero_expr());
1788 __split_expr(assign
);
1789 } END_FOR_EACH_PTR(tmp
);
1794 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1797 struct expression
*member
= NULL
;
1798 struct expression
*assign
;
1800 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1801 type
= get_real_base_type(tmp
);
1806 member
= member_expression(expr
, '.', tmp
->ident
);
1807 if (get_extra_state(member
))
1811 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1812 set_inner_struct_members(expr
, tmp
);
1815 if (type
->type
== SYM_ARRAY
)
1820 assign
= assign_expression(member
, '=', zero_expr());
1821 __split_expr(assign
);
1822 } END_FOR_EACH_PTR(tmp
);
1825 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1827 struct expression
*deref
, *assign
, *tmp
, *right
;
1828 struct symbol
*struct_type
, *type
;
1829 struct ident
*member
;
1832 struct_type
= get_type(symbol
);
1834 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1838 * We're parsing an initializer that could look something like this:
1839 * struct foo foo = {
1841 * .whatever.xxx = 11,
1845 * So what we have here is a list with 42, .whatever, and .zzz. We need
1846 * to break it up into left and right sides of the assignments.
1850 FOR_EACH_PTR(members
, tmp
) {
1852 if (tmp
->type
== EXPR_IDENTIFIER
) {
1853 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1854 while (tmp
->type
== EXPR_IDENTIFIER
) {
1855 member
= tmp
->expr_ident
;
1856 tmp
= tmp
->ident_expression
;
1858 deref
= member_expression(deref
, '.', member
);
1860 deref
= member_expression(symbol
, '.', member
);
1863 member
= number_to_member(symbol
, member_idx
);
1864 deref
= member_expression(symbol
, '.', member
);
1868 if (right
->type
== EXPR_INITIALIZER
) {
1869 type
= get_type(deref
);
1870 if (type
&& type
->type
== SYM_ARRAY
)
1871 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1873 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1875 assign
= assign_expression(deref
, '=', right
);
1878 } END_FOR_EACH_PTR(tmp
);
1880 set_unset_to_zero(struct_type
, symbol
);
1883 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1885 fake_member_assigns_helper(symbol_expression(sym
),
1886 sym
->initializer
->expr_list
, fake_cb
);
1889 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1891 struct expression
*offset
, *binop
, *assign
, *tmp
;
1892 struct symbol
*type
;
1895 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1900 FOR_EACH_PTR(expr_list
, tmp
) {
1901 if (tmp
->type
== EXPR_INDEX
) {
1902 if (tmp
->idx_from
!= tmp
->idx_to
)
1904 idx
= tmp
->idx_from
;
1907 if (!tmp
->idx_expression
)
1909 tmp
= tmp
->idx_expression
;
1911 offset
= value_expr(idx
);
1912 binop
= array_element_expression(array
, offset
);
1913 if (tmp
->type
== EXPR_INITIALIZER
) {
1914 type
= get_type(binop
);
1915 if (type
&& type
->type
== SYM_ARRAY
)
1916 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1918 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1920 assign
= assign_expression(binop
, '=', tmp
);
1927 } END_FOR_EACH_PTR(tmp
);
1929 __call_array_initialized_hooks(array
, max
);
1932 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1934 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1937 static void fake_assign_expr(struct symbol
*sym
)
1939 struct expression
*assign
, *symbol
;
1941 symbol
= symbol_expression(sym
);
1942 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1943 __split_expr(assign
);
1946 static void do_initializer_stuff(struct symbol
*sym
)
1948 if (!sym
->initializer
)
1951 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1952 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1953 fake_element_assigns(sym
, __split_expr
);
1955 fake_member_assigns(sym
, __split_expr
);
1957 fake_assign_expr(sym
);
1961 static void split_declaration(struct symbol_list
*sym_list
)
1965 FOR_EACH_PTR(sym_list
, sym
) {
1966 __pass_to_client(sym
, DECLARATION_HOOK
);
1967 do_initializer_stuff(sym
);
1968 __pass_to_client(sym
, DECLARATION_HOOK_AFTER
);
1970 } END_FOR_EACH_PTR(sym
);
1973 static void call_global_assign_hooks(struct expression
*assign
)
1975 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1978 static void fake_global_assign(struct symbol
*sym
)
1980 struct expression
*assign
, *symbol
;
1982 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1983 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1984 fake_element_assigns(sym
, call_global_assign_hooks
);
1985 } else if (sym
->initializer
) {
1986 symbol
= symbol_expression(sym
);
1987 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1988 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1990 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1992 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1993 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1994 fake_member_assigns(sym
, call_global_assign_hooks
);
1995 } else if (sym
->initializer
) {
1996 symbol
= symbol_expression(sym
);
1997 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1998 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
2000 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
2003 symbol
= symbol_expression(sym
);
2004 if (sym
->initializer
) {
2005 assign
= assign_expression(symbol
, '=', sym
->initializer
);
2006 __split_expr(assign
);
2008 assign
= assign_expression(symbol
, '=', zero_expr());
2010 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
2014 static void start_function_definition(struct symbol
*sym
)
2016 __in_function_def
= 1;
2017 __pass_to_client(sym
, FUNC_DEF_HOOK
);
2018 __in_function_def
= 0;
2019 __pass_to_client(sym
, AFTER_DEF_HOOK
);
2023 static void parse_fn_statements(struct symbol
*base
)
2025 __split_stmt(base
->stmt
);
2026 __split_stmt(base
->inline_stmt
);
2029 void add_function_data(unsigned long *fn_data
)
2031 __add_ptr_list(&fn_data_list
, fn_data
);
2034 static void clear_function_data(void)
2038 FOR_EACH_PTR(fn_data_list
, tmp
) {
2040 } END_FOR_EACH_PTR(tmp
);
2043 static void record_func_time(void)
2045 struct timeval stop
;
2049 gettimeofday(&stop
, NULL
);
2050 func_time
= stop
.tv_sec
- fn_start_time
.tv_sec
;
2051 snprintf(buf
, sizeof(buf
), "%d", func_time
);
2052 sql_insert_return_implies(FUNC_TIME
, 0, "", buf
);
2053 if (option_time
&& func_time
> 2) {
2055 sm_msg("func_time: %d", func_time
);
2060 static void split_function(struct symbol
*sym
)
2062 struct symbol
*base_type
= get_base_type(sym
);
2064 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
2067 gettimeofday(&outer_fn_start_time
, NULL
);
2068 gettimeofday(&fn_start_time
, NULL
);
2071 cur_func
= sym
->ident
->name
;
2072 if (option_process_function
&& cur_func
&&
2073 strcmp(option_process_function
, cur_func
) != 0)
2075 set_position(sym
->pos
);
2076 clear_function_data();
2078 last_goto_statement_handled
= 0;
2079 sm_debug("new function: %s\n", cur_func
);
2081 if (option_two_passes
) {
2085 start_function_definition(sym
);
2086 parse_fn_statements(base_type
);
2087 __call_scope_hooks();
2093 start_function_definition(sym
);
2094 parse_fn_statements(base_type
);
2095 if (!__path_is_null() &&
2096 cur_func_return_type() == &void_ctype
&&
2097 !__bail_on_rest_of_function
) {
2098 __call_all_scope_hooks();
2099 __pass_to_client(NULL
, RETURN_HOOK
);
2102 __pass_to_client(sym
, END_FUNC_HOOK
);
2103 __free_scope_hooks();
2104 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
2111 cur_func_sym
= NULL
;
2113 free_data_info_allocs();
2114 free_expression_stack(&switch_expr_stack
);
2115 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
2116 __bail_on_rest_of_function
= 0;
2119 static void save_flow_state(void)
2123 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2));
2124 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2));
2125 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2));
2127 __add_ptr_list(&backup
, big_statement_stack
);
2128 __add_ptr_list(&backup
, big_expression_stack
);
2129 __add_ptr_list(&backup
, big_condition_stack
);
2130 __add_ptr_list(&backup
, switch_expr_stack
);
2132 __add_ptr_list(&backup
, cur_func_sym
);
2134 __add_ptr_list(&backup
, parsed_calls
);
2136 __add_ptr_list(&backup
, __prev_stmt
);
2137 __add_ptr_list(&backup
, __cur_stmt
);
2138 __add_ptr_list(&backup
, __next_stmt
);
2140 FOR_EACH_PTR(fn_data_list
, tmp
) {
2141 __add_ptr_list(&backup
, (void *)*tmp
);
2142 } END_FOR_EACH_PTR(tmp
);
2145 static void *pop_backup(void)
2149 ret
= last_ptr_list(backup
);
2150 delete_ptr_list_last(&backup
);
2154 static void restore_flow_state(void)
2158 FOR_EACH_PTR_REVERSE(fn_data_list
, tmp
) {
2159 *tmp
= (unsigned long)pop_backup();
2160 } END_FOR_EACH_PTR_REVERSE(tmp
);
2162 __next_stmt
= pop_backup();
2163 __cur_stmt
= pop_backup();
2164 __prev_stmt
= pop_backup();
2166 parsed_calls
= pop_backup();
2168 cur_func_sym
= pop_backup();
2169 switch_expr_stack
= pop_backup();
2170 big_condition_stack
= pop_backup();
2171 big_expression_stack
= pop_backup();
2172 big_statement_stack
= pop_backup();
2173 final_pass
= PTR_INT(pop_backup()) >> 2;
2174 loop_count
= PTR_INT(pop_backup()) >> 2;
2175 loop_num
= PTR_INT(pop_backup()) >> 2;
2178 void parse_inline(struct expression
*call
)
2180 struct symbol
*base_type
;
2181 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
2182 struct timeval time_backup
= fn_start_time
;
2183 struct expression
*orig_inline
= __inline_fn
;
2186 if (out_of_memory() || taking_too_long())
2189 if (already_parsed_call(call
))
2194 __pass_to_client(call
, INLINE_FN_START
);
2195 final_pass
= 0; /* don't print anything */
2197 orig_budget
= inline_budget
;
2198 inline_budget
= inline_budget
- 5;
2200 base_type
= get_base_type(call
->fn
->symbol
);
2201 cur_func_sym
= call
->fn
->symbol
;
2202 if (call
->fn
->symbol
->ident
)
2203 cur_func
= call
->fn
->symbol
->ident
->name
;
2206 set_position(call
->fn
->symbol
->pos
);
2209 big_statement_stack
= NULL
;
2210 big_expression_stack
= NULL
;
2211 big_condition_stack
= NULL
;
2212 switch_expr_stack
= NULL
;
2213 parsed_calls
= NULL
;
2215 sm_debug("inline function: %s\n", cur_func
);
2217 clear_function_data();
2220 start_function_definition(call
->fn
->symbol
);
2221 parse_fn_statements(base_type
);
2222 if (!__path_is_null() &&
2223 cur_func_return_type() == &void_ctype
&&
2224 !__bail_on_rest_of_function
) {
2225 __call_all_scope_hooks();
2226 __pass_to_client(NULL
, RETURN_HOOK
);
2229 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
2230 __free_scope_hooks();
2231 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
2232 call
->fn
->symbol
->parsed
= true;
2234 free_expression_stack(&switch_expr_stack
);
2235 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
2239 restore_flow_state();
2240 fn_start_time
= time_backup
;
2241 cur_func
= cur_func_bak
;
2243 restore_all_states();
2244 set_position(call
->pos
);
2245 __inline_fn
= orig_inline
;
2246 inline_budget
= orig_budget
;
2247 __pass_to_client(call
, INLINE_FN_END
);
2250 static struct symbol_list
*inlines_called
;
2251 static void add_inline_function(struct symbol
*sym
)
2253 static struct symbol_list
*already_added
;
2256 FOR_EACH_PTR(already_added
, tmp
) {
2259 } END_FOR_EACH_PTR(tmp
);
2261 add_ptr_list(&already_added
, sym
);
2262 add_ptr_list(&inlines_called
, sym
);
2265 static void process_inlines(void)
2269 FOR_EACH_PTR(inlines_called
, tmp
) {
2270 split_function(tmp
);
2271 } END_FOR_EACH_PTR(tmp
);
2272 free_ptr_list(&inlines_called
);
2275 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
2279 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
2282 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
2284 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
2286 } END_FOR_EACH_PTR_REVERSE(sym
);
2291 static bool interesting_function(struct symbol
*sym
)
2293 static int prev_stream
= -1;
2294 static bool prev_answer
;
2295 const char *filename
;
2298 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
2301 if (sym
->pos
.stream
== prev_stream
)
2304 prev_stream
= sym
->pos
.stream
;
2305 prev_answer
= false;
2307 filename
= stream_name(sym
->pos
.stream
);
2308 len
= strlen(filename
);
2309 if (len
> 0 && filename
[len
- 1] == 'c')
2314 static void split_inlines_in_scope(struct symbol
*sym
)
2316 struct symbol
*base
;
2317 struct symbol_list
*scope_list
;
2320 scope_list
= sym
->scope
->symbols
;
2321 stream
= sym
->pos
.stream
;
2323 /* find the last static symbol in the file */
2324 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
2325 if (sym
->pos
.stream
!= stream
)
2327 if (sym
->type
!= SYM_NODE
)
2329 base
= get_base_type(sym
);
2332 if (base
->type
!= SYM_FN
)
2334 if (!base
->inline_stmt
)
2336 if (!interesting_function(sym
))
2338 add_inline_function(sym
);
2339 } END_FOR_EACH_PTR_REVERSE(sym
);
2344 static void split_inlines(struct symbol_list
*sym_list
)
2348 sym
= get_last_scoped_symbol(sym_list
, 0);
2350 split_inlines_in_scope(sym
);
2351 sym
= get_last_scoped_symbol(sym_list
, 1);
2353 split_inlines_in_scope(sym
);
2356 static struct stree
*clone_estates_perm(struct stree
*orig
)
2358 struct stree
*ret
= NULL
;
2359 struct sm_state
*tmp
;
2361 FOR_EACH_SM(orig
, tmp
) {
2362 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
2363 } END_FOR_EACH_SM(tmp
);
2368 struct position last_pos
;
2369 static void split_c_file_functions(struct symbol_list
*sym_list
)
2374 FOR_EACH_PTR(sym_list
, sym
) {
2375 set_position(sym
->pos
);
2376 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
2377 __pass_to_client(sym
, BASE_HOOK
);
2378 fake_global_assign(sym
);
2379 __pass_to_client(sym
, DECLARATION_HOOK_AFTER
);
2381 } END_FOR_EACH_PTR(sym
);
2382 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
2385 FOR_EACH_PTR(sym_list
, sym
) {
2386 set_position(sym
->pos
);
2387 last_pos
= sym
->pos
;
2388 if (!interesting_function(sym
))
2390 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
2391 split_function(sym
);
2394 last_pos
= sym
->pos
;
2395 } END_FOR_EACH_PTR(sym
);
2396 split_inlines(sym_list
);
2397 __pass_to_client(sym_list
, END_FILE_HOOK
);
2400 static int final_before_fake
;
2401 void init_fake_env(void)
2404 final_before_fake
= final_pass
;
2406 __push_fake_cur_stree();
2410 void end_fake_env(void)
2412 __free_fake_cur_stree();
2415 final_pass
= final_before_fake
;
2418 static void open_output_files(char *base_file
)
2422 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
2423 sm_outfd
= fopen(buf
, "w");
2425 sm_fatal("Cannot open %s", buf
);
2430 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
2431 sql_outfd
= fopen(buf
, "w");
2433 sm_fatal("Error: Cannot open %s", buf
);
2435 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
2436 caller_info_fd
= fopen(buf
, "w");
2437 if (!caller_info_fd
)
2438 sm_fatal("Error: Cannot open %s", buf
);
2441 void smatch(struct string_list
*filelist
)
2443 struct symbol_list
*sym_list
;
2444 struct timeval stop
, start
;
2448 gettimeofday(&start
, NULL
);
2450 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
2451 path
= getcwd(NULL
, 0);
2452 free(full_base_file
);
2454 len
= strlen(path
) + 1 + strlen(base_file
) + 1;
2455 full_base_file
= malloc(len
);
2456 snprintf(full_base_file
, len
, "%s/%s", path
, base_file
);
2458 full_base_file
= alloc_string(base_file
);
2460 if (option_file_output
)
2461 open_output_files(base_file
);
2462 base_file_stream
= input_stream_nr
;
2463 sym_list
= sparse_keep_tokens(base_file
);
2464 split_c_file_functions(sym_list
);
2465 } END_FOR_EACH_PTR_NOTAG(base_file
);
2467 gettimeofday(&stop
, NULL
);
2469 set_position(last_pos
);
2472 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
2474 sm_msg("mem: %luKb", get_max_memory());