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 int prev_expression_is_getting_address(struct expression
*expr
)
356 struct expression
*parent
;
359 parent
= expr_get_parent_expr(expr
);
363 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
365 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
367 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
369 /* Handle &foo->array[offset] */
370 if (parent
->type
== EXPR_BINOP
&& parent
->op
== '+') {
371 parent
= expr_get_parent_expr(parent
);
374 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '*')
384 int __in_builtin_overflow_func
;
385 static void handle_builtin_overflow_func(struct expression
*expr
)
387 struct expression
*a
, *b
, *res
, *assign
;
390 if (sym_name_is("__builtin_add_overflow", expr
->fn
))
392 else if (sym_name_is("__builtin_sub_overflow", expr
->fn
))
394 else if (sym_name_is("__builtin_mul_overflow", expr
->fn
))
399 a
= get_argument_from_call_expr(expr
->args
, 0);
400 b
= get_argument_from_call_expr(expr
->args
, 1);
401 res
= get_argument_from_call_expr(expr
->args
, 2);
403 assign
= assign_expression(deref_expression(res
), '=', binop_expression(a
, op
, b
));
405 __in_builtin_overflow_func
++;
406 __split_expr(assign
);
407 __in_builtin_overflow_func
--;
410 static int handle__builtin_choose_expr(struct expression
*expr
)
412 struct expression
*const_expr
, *expr1
, *expr2
;
415 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
418 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
419 expr1
= get_argument_from_call_expr(expr
->args
, 1);
420 expr2
= get_argument_from_call_expr(expr
->args
, 2);
422 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
431 static int handle__builtin_choose_expr_assigns(struct expression
*expr
)
433 struct expression
*const_expr
, *right
, *expr1
, *expr2
, *fake
;
437 * We can't use strip_no_cast() because it strips out
438 * __builtin_choose_expr() which turns this function into a no-op.
441 right
= strip_parens(expr
->right
);
442 if (right
->type
!= EXPR_CALL
)
444 if (!sym_name_is("__builtin_choose_expr", right
->fn
))
447 const_expr
= get_argument_from_call_expr(right
->args
, 0);
448 expr1
= get_argument_from_call_expr(right
->args
, 1);
449 expr2
= get_argument_from_call_expr(right
->args
, 2);
451 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
454 fake
= assign_expression(expr
->left
, '=', sval
.value
? expr1
: expr2
);
459 int is_condition_call(struct expression
*expr
)
461 struct expression
*tmp
;
463 FOR_EACH_PTR_REVERSE(big_condition_stack
, tmp
) {
464 if (expr
== tmp
|| expr_get_parent_expr(expr
) == tmp
)
466 if (tmp
->pos
.line
< expr
->pos
.line
)
468 } END_FOR_EACH_PTR_REVERSE(tmp
);
473 static struct expression
*expr_get_parent_no_parens(struct expression
*expr
)
476 expr
= expr_get_parent_expr(expr
);
478 expr
->type
== EXPR_PREOP
&&
484 static bool gen_fake_function_assign(struct expression
*expr
)
486 static struct expression
*parsed
;
487 struct expression
*assign
, *parent
;
491 /* The rule is that every non-void function call has to be part of an
492 * assignment. TODO: Should we create a fake non-casted assignment
493 * for casted assignments? Also faked assigns for += assignments?
495 type
= get_type(expr
);
496 if (!type
|| type
== &void_ctype
)
499 parent
= expr_get_parent_no_parens(expr
);
500 if (parent
&& parent
->type
== EXPR_ASSIGNMENT
)
503 parent
= expr_get_fake_parent_expr(expr
);
505 struct expression
*left
= parent
->left
;
507 if (parent
== parsed
)
509 if (!left
|| left
->type
!= EXPR_SYMBOL
)
511 if (strncmp(left
->symbol_name
->name
, "__fake_assign_", 14) != 0)
514 __split_expr(parent
);
518 // TODO: faked_assign skipping conditions is a hack
519 if (is_condition_call(expr
))
522 snprintf(buf
, sizeof(buf
), "__fake_assign_%p", expr
);
523 assign
= create_fake_assign(buf
, get_type(expr
), expr
);
526 __split_expr(assign
);
530 static void split_call(struct expression
*expr
)
532 if (gen_fake_function_assign(expr
))
535 expr_set_parent_expr(expr
->fn
, expr
);
537 if (sym_name_is("__builtin_constant_p", expr
->fn
))
539 if (handle__builtin_choose_expr(expr
))
541 __split_expr(expr
->fn
);
543 if (is_inline_func(expr
->fn
))
544 add_inline_function(expr
->fn
->symbol
->definition
);
545 if (inlinable(expr
->fn
))
547 __process_post_op_stack();
548 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
549 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
551 if (inlinable(expr
->fn
))
553 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
554 if (is_noreturn_func(expr
->fn
))
556 if (!expr_get_parent_expr(expr
) && indent_cnt
== 1)
557 __discard_fake_states(expr
);
558 handle_builtin_overflow_func(expr
);
559 __add_ptr_list((struct ptr_list
**)&parsed_calls
, expr
);
562 static unsigned long skip_split
;
563 void parse_assignment(struct expression
*expr
, bool shallow
)
565 struct expression
*right
;
567 expr_set_parent_expr(expr
->left
, expr
);
568 expr_set_parent_expr(expr
->right
, expr
);
570 right
= strip_expr(expr
->right
);
577 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
580 if (__handle_condition_assigns(expr
))
582 /* foo = (x < 5 ? foo : 5); */
583 if (__handle_select_assigns(expr
))
585 /* foo = ({frob(); frob(); frob(); 1;}) */
586 if (__handle_expr_statement_assigns(expr
))
587 goto done
; // FIXME: goto after
589 if (handle_comma_assigns(expr
))
591 if (handle__builtin_choose_expr_assigns(expr
))
593 if (handle_postop_assigns(expr
))
594 goto done
; /* no need to goto after_assign */
596 __split_expr(expr
->right
);
597 if (outside_of_function())
598 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
600 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
603 // FIXME: the ordering of this is tricky
604 __fake_struct_member_assignments(expr
);
606 /* Re-examine ->right for inlines. See the commit message */
607 right
= strip_expr(expr
->right
);
608 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
609 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
612 if (get_macro_name(right
->pos
) &&
613 get_macro_name(expr
->left
->pos
) != get_macro_name(right
->pos
))
614 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
616 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
617 __split_expr(expr
->left
);
624 static bool skip_split_off(struct expression
*expr
)
626 if (expr
->type
== EXPR_CALL
&&
627 sym_name_is("__smatch_stop_skip", expr
->fn
))
632 void __split_expr(struct expression
*expr
)
637 if (skip_split_off(expr
))
646 // sm_msg("Debug expr_type %d %s expr = '%s'", expr->type, show_special(expr->op), expr_to_str(expr));
648 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
650 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
653 push_expression(&big_expression_stack
, expr
);
654 set_position(expr
->pos
);
655 __pass_to_client(expr
, EXPR_HOOK
);
657 switch (expr
->type
) {
659 expr_set_parent_expr(expr
->unop
, expr
);
661 if (expr
->op
== '*' &&
662 !prev_expression_is_getting_address(expr
))
663 __pass_to_client(expr
, DEREF_HOOK
);
664 __split_expr(expr
->unop
);
665 __pass_to_client(expr
, OP_HOOK
);
668 expr_set_parent_expr(expr
->unop
, expr
);
670 __split_expr(expr
->unop
);
671 push_expression(&post_op_stack
, expr
);
675 if (expr
->statement
&& !expr
->statement
) {
676 stmt_set_parent_stmt(expr
->statement
,
677 last_ptr_list((struct ptr_list
*)big_statement_stack
));
679 __split_stmt(expr
->statement
);
684 expr_set_parent_expr(expr
->left
, expr
);
685 expr_set_parent_expr(expr
->right
, expr
);
687 __pass_to_client(expr
, LOGIC_HOOK
);
688 __handle_logic(expr
);
691 expr_set_parent_expr(expr
->left
, expr
);
692 expr_set_parent_expr(expr
->right
, expr
);
694 __pass_to_client(expr
, BINOP_HOOK
);
695 __split_expr(expr
->left
);
696 __split_expr(expr
->right
);
699 expr_set_parent_expr(expr
->left
, expr
);
700 expr_set_parent_expr(expr
->right
, expr
);
702 __split_expr(expr
->left
);
703 __process_post_op_stack();
704 __split_expr(expr
->right
);
706 case EXPR_ASSIGNMENT
:
707 parse_assignment(expr
, false);
710 expr_set_parent_expr(expr
->deref
, expr
);
712 __pass_to_client(expr
, DEREF_HOOK
);
713 __split_expr(expr
->deref
);
716 expr_set_parent_expr(expr
->base
, expr
);
718 __split_expr(expr
->base
);
721 case EXPR_FORCE_CAST
:
722 expr_set_parent_expr(expr
->cast_expression
, expr
);
724 __pass_to_client(expr
, CAST_HOOK
);
725 __split_expr(expr
->cast_expression
);
728 if (expr
->cast_expression
)
729 __pass_to_client(strip_parens(expr
->cast_expression
),
735 case EXPR_CONDITIONAL
:
737 expr_set_parent_expr(expr
->conditional
, expr
);
738 expr_set_parent_expr(expr
->cond_true
, expr
);
739 expr_set_parent_expr(expr
->cond_false
, expr
);
741 if (known_condition_true(expr
->conditional
)) {
742 __split_expr(expr
->cond_true
);
745 if (known_condition_false(expr
->conditional
)) {
746 __split_expr(expr
->cond_false
);
749 __pass_to_client(expr
, SELECT_HOOK
);
750 __split_whole_condition(expr
->conditional
);
751 __split_expr(expr
->cond_true
);
752 __push_true_states();
753 __use_false_states();
754 __split_expr(expr
->cond_false
);
755 __merge_true_states();
760 case EXPR_INITIALIZER
:
761 split_expr_list(expr
->expr_list
, expr
);
763 case EXPR_IDENTIFIER
:
764 expr_set_parent_expr(expr
->ident_expression
, expr
);
765 __split_expr(expr
->ident_expression
);
768 expr_set_parent_expr(expr
->idx_expression
, expr
);
769 __split_expr(expr
->idx_expression
);
772 expr_set_parent_expr(expr
->init_expr
, expr
);
773 __split_expr(expr
->init_expr
);
776 __pass_to_client(expr
, SYM_HOOK
);
779 __pass_to_client(expr
, STRING_HOOK
);
782 struct expression
*tmp
;
784 tmp
= strip_Generic(expr
);
792 __pass_to_client(expr
, EXPR_HOOK_AFTER
);
793 pop_expression(&big_expression_stack
);
796 static int is_forever_loop(struct statement
*stmt
)
798 struct expression
*expr
;
801 expr
= strip_expr(stmt
->iterator_pre_condition
);
803 expr
= stmt
->iterator_post_condition
;
805 /* this is a for(;;) loop... */
809 if (get_value(expr
, &sval
) && sval
.value
!= 0)
816 static char *get_loop_name(int num
)
820 snprintf(buf
, 255, "-loop%d", num
);
822 return alloc_sname(buf
);
825 static struct bool_stmt_fn_list
*once_through_hooks
;
826 void add_once_through_hook(bool_stmt_func
*fn
)
828 add_ptr_list(&once_through_hooks
, fn
);
831 static bool call_once_through_hooks(struct statement
*stmt
)
835 if (implied_condition_true(stmt
->iterator_pre_condition
))
837 if (option_assume_loops
)
840 FOR_EACH_PTR(once_through_hooks
, fn
) {
843 } END_FOR_EACH_PTR(fn
);
849 * Pre Loops are while and for loops.
851 static void handle_pre_loop(struct statement
*stmt
)
853 int once_through
; /* we go through the loop at least once */
854 struct sm_state
*extra_sm
= NULL
;
857 struct stree
*stree
= NULL
;
858 struct sm_state
*sm
= NULL
;
860 loop_name
= get_loop_name(loop_num
);
863 split_declaration(stmt
->iterator_syms
);
864 if (stmt
->iterator_pre_statement
) {
865 __split_stmt(stmt
->iterator_pre_statement
);
866 __prev_stmt
= stmt
->iterator_pre_statement
;
869 once_through
= call_once_through_hooks(stmt
);
875 __merge_gotos(loop_name
, NULL
);
877 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
878 __in_pre_condition
++;
879 __pass_to_client(stmt
, PRELOOP_HOOK
);
880 __split_whole_condition(stmt
->iterator_pre_condition
);
881 __in_pre_condition
--;
882 FOR_EACH_SM(stree
, sm
) {
883 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
884 } END_FOR_EACH_SM(sm
);
887 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
889 __split_stmt(stmt
->iterator_statement
);
890 if (is_forever_loop(stmt
)) {
892 __save_gotos(loop_name
, NULL
);
894 __push_fake_cur_stree();
895 __split_stmt(stmt
->iterator_post_statement
);
896 stree
= __pop_fake_cur_stree();
898 __discard_false_states();
899 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
902 if (!__path_is_null())
903 __merge_stree_into_cur(stree
);
907 unchanged
= __iterator_unchanged(extra_sm
);
908 __split_stmt(stmt
->iterator_post_statement
);
909 __prev_stmt
= stmt
->iterator_post_statement
;
912 __save_gotos(loop_name
, NULL
);
913 __in_pre_condition
++;
914 __split_whole_condition(stmt
->iterator_pre_condition
);
915 __in_pre_condition
--;
917 __merge_false_states();
919 __discard_false_states();
921 __merge_false_states();
923 if (extra_sm
&& unchanged
)
924 __extra_pre_loop_hook_after(extra_sm
,
925 stmt
->iterator_post_statement
,
926 stmt
->iterator_pre_condition
);
927 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
934 * Post loops are do {} while();
936 static void handle_post_loop(struct statement
*stmt
)
940 loop_name
= get_loop_name(loop_num
);
944 __pass_to_client(stmt
, POSTLOOP_HOOK
);
948 __merge_gotos(loop_name
, NULL
);
949 __split_stmt(stmt
->iterator_statement
);
951 if (!expr_is_zero(stmt
->iterator_post_condition
))
952 __save_gotos(loop_name
, NULL
);
954 if (is_forever_loop(stmt
)) {
955 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
958 __split_whole_condition(stmt
->iterator_post_condition
);
959 __use_false_states();
960 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
966 static int empty_statement(struct statement
*stmt
)
970 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
975 static int last_stmt_on_same_line(void)
977 struct statement
*stmt
;
980 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
983 if (stmt
->pos
.line
== get_lineno())
986 } END_FOR_EACH_PTR_REVERSE(stmt
);
990 static void split_asm_ops(struct asm_operand_list
*ops
)
992 struct asm_operand
*op
;
994 FOR_EACH_PTR(ops
, op
) {
995 __split_expr(op
->expr
);
996 } END_FOR_EACH_PTR(op
);
999 static int is_case_val(struct statement
*stmt
, sval_t sval
)
1003 if (stmt
->type
!= STMT_CASE
)
1005 if (!stmt
->case_expression
) {
1009 if (!get_value(stmt
->case_expression
, &case_sval
))
1011 if (case_sval
.value
== sval
.value
)
1016 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
1017 struct expression
*case_expr
,
1018 struct expression
*case_to
)
1021 struct range_list
*rl
= NULL
;
1022 struct symbol
*switch_type
;
1024 switch_type
= get_type(switch_expr
);
1025 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
1026 start
= sval_cast(switch_type
, start
);
1027 end
= sval_cast(switch_type
, end
);
1028 add_range(&rl
, start
, end
);
1029 } else if (get_value(case_expr
, &start
)) {
1030 start
= sval_cast(switch_type
, start
);
1031 add_range(&rl
, start
, start
);
1037 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
1039 struct statement
*tmp
;
1040 struct range_list
*rl
;
1042 __split_expr(stmt
->switch_expression
);
1043 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
1045 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1046 __save_switch_states(top_expression(switch_expr_stack
));
1051 stmt
= stmt
->switch_statement
;
1053 __push_scope_hooks();
1054 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
1055 __smatch_lineno
= tmp
->pos
.line
;
1056 // FIXME: what if default comes before the known case statement?
1057 if (is_case_val(tmp
, sval
)) {
1058 rl
= alloc_rl(sval
, sval
);
1059 __merge_switches(top_expression(switch_expr_stack
), rl
);
1060 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
1061 stmt_set_parent_stmt(tmp
->case_statement
, tmp
);
1062 __split_stmt(tmp
->case_statement
);
1065 if (__path_is_null())
1069 if (__path_is_null()) {
1073 } END_FOR_EACH_PTR(tmp
);
1075 __call_scope_hooks();
1076 if (!__pop_default())
1077 __merge_switches(top_expression(switch_expr_stack
), NULL
);
1078 __discard_switches();
1080 pop_expression(&switch_expr_stack
);
1083 static void split_case(struct statement
*stmt
)
1085 struct range_list
*rl
= NULL
;
1087 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
1088 expr_set_parent_stmt(stmt
->case_to
, stmt
);
1090 rl
= get_case_rl(top_expression(switch_expr_stack
),
1091 stmt
->case_expression
, stmt
->case_to
);
1092 while (stmt
->case_statement
->type
== STMT_CASE
) {
1093 struct range_list
*tmp
;
1095 tmp
= get_case_rl(top_expression(switch_expr_stack
),
1096 stmt
->case_statement
->case_expression
,
1097 stmt
->case_statement
->case_to
);
1100 rl
= rl_union(rl
, tmp
);
1101 if (!stmt
->case_expression
)
1104 stmt
= stmt
->case_statement
;
1107 __merge_switches(top_expression(switch_expr_stack
), rl
);
1109 if (!stmt
->case_expression
)
1112 stmt_set_parent_stmt(stmt
->case_statement
, stmt
);
1113 __split_stmt(stmt
->case_statement
);
1116 int time_parsing_function(void)
1118 return ms_since(&fn_start_time
) / 1000;
1121 bool taking_too_long(void)
1123 if ((ms_since(&outer_fn_start_time
) / 1000) > 60 * 5) /* five minutes */
1128 struct statement
*get_last_stmt(void)
1131 struct statement
*stmt
;
1133 fn
= get_base_type(cur_func_sym
);
1138 stmt
= fn
->inline_stmt
;
1139 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
1141 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
1142 if (stmt
&& stmt
->type
== STMT_LABEL
)
1143 stmt
= stmt
->label_statement
;
1147 int is_last_stmt(struct statement
*cur_stmt
)
1149 struct statement
*last
;
1151 last
= get_last_stmt();
1152 if (last
&& last
== cur_stmt
)
1157 static void handle_backward_goto(struct statement
*goto_stmt
)
1159 const char *goto_name
, *label_name
;
1160 struct statement
*func_stmt
;
1161 struct symbol
*base_type
= get_base_type(cur_func_sym
);
1162 struct statement
*tmp
;
1167 if (last_goto_statement_handled
)
1169 last_goto_statement_handled
= 1;
1171 if (!goto_stmt
->goto_label
||
1172 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
1173 !goto_stmt
->goto_label
->ident
)
1175 goto_name
= goto_stmt
->goto_label
->ident
->name
;
1177 func_stmt
= base_type
->stmt
;
1179 func_stmt
= base_type
->inline_stmt
;
1182 if (func_stmt
->type
!= STMT_COMPOUND
)
1185 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
1187 if (tmp
->type
!= STMT_LABEL
)
1189 if (!tmp
->label_identifier
||
1190 tmp
->label_identifier
->type
!= SYM_LABEL
||
1191 !tmp
->label_identifier
->ident
)
1193 label_name
= tmp
->label_identifier
->ident
->name
;
1194 if (strcmp(goto_name
, label_name
) != 0)
1199 } END_FOR_EACH_PTR(tmp
);
1202 static void fake_a_return(void)
1204 struct expression
*ret
= NULL
;
1209 if (cur_func_return_type() != &void_ctype
)
1210 ret
= unknown_value_expression(NULL
);
1212 __pass_to_client(ret
, RETURN_HOOK
);
1216 static void split_ret_value(struct expression
*expr
)
1218 struct symbol
*type
;
1223 type
= get_real_base_type(cur_func_sym
);
1224 type
= get_real_base_type(type
);
1225 expr
= fake_a_variable_assign(type
, NULL
, expr
, -1);
1227 __in_fake_var_assign
++;
1229 __in_fake_var_assign
--;
1232 static void fake_an_empty_default(struct position pos
)
1234 static struct statement none
= {};
1237 none
.type
= STMT_NONE
;
1238 __merge_switches(top_expression(switch_expr_stack
), NULL
);
1239 __split_stmt(&none
);
1242 static void split_compound(struct statement
*stmt
)
1244 struct statement
*prev
= NULL
;
1245 struct statement
*cur
= NULL
;
1246 struct statement
*next
;
1248 __push_scope_hooks();
1250 FOR_EACH_PTR(stmt
->stmts
, next
) {
1251 /* just set them all ahead of time */
1252 stmt_set_parent_stmt(next
, stmt
);
1262 } END_FOR_EACH_PTR(next
);
1271 * For function scope, then delay calling the scope hooks until the
1272 * end of function hooks can run. I'm not positive this is the right
1275 if (!is_last_stmt(cur
))
1276 __call_scope_hooks();
1280 * This is a hack, work around for detecting empty functions.
1282 static int need_delayed_scope_hooks(void)
1284 struct symbol
*fn
= get_base_type(cur_func_sym
);
1285 struct statement
*stmt
;
1291 stmt
= fn
->inline_stmt
;
1292 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
1297 void __split_label_stmt(struct statement
*stmt
)
1299 if (stmt
->label_identifier
&&
1300 stmt
->label_identifier
->type
== SYM_LABEL
&&
1301 stmt
->label_identifier
->ident
) {
1302 loop_count
|= 0x0800000;
1303 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
1307 static void find_asm_gotos(struct statement
*stmt
)
1311 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
1312 __save_gotos(sym
->ident
->name
, sym
);
1313 } END_FOR_EACH_PTR(sym
);
1316 static bool already_parsed_call(struct expression
*call
)
1318 struct expression
*expr
;
1320 FOR_EACH_PTR(parsed_calls
, expr
) {
1323 } END_FOR_EACH_PTR(expr
);
1327 static void free_parsed_call_stuff(bool free_fake_states
)
1329 free_expression_stack(&parsed_calls
);
1330 if (free_fake_states
)
1331 __discard_fake_states(NULL
);
1334 void __split_stmt(struct statement
*stmt
)
1337 struct timeval start
, stop
;
1338 bool skip_after
= false;
1340 gettimeofday(&start
, NULL
);
1345 if (!__in_fake_assign
)
1346 __silence_warnings_for_stmt
= false;
1348 if (__bail_on_rest_of_function
|| is_skipped_function())
1351 if (out_of_memory() || taking_too_long()) {
1352 gettimeofday(&start
, NULL
);
1354 __bail_on_rest_of_function
= 1;
1356 sm_perror("Function too hairy. Giving up. %lu seconds",
1357 start
.tv_sec
- fn_start_time
.tv_sec
);
1359 final_pass
= 0; /* turn off sm_msg() from here */
1365 add_ptr_list(&big_statement_stack
, stmt
);
1366 free_expression_stack(&big_expression_stack
);
1367 free_parsed_call_stuff(indent_cnt
== 1);
1368 set_position(stmt
->pos
);
1369 __pass_to_client(stmt
, STMT_HOOK
);
1371 switch (stmt
->type
) {
1372 case STMT_DECLARATION
:
1373 split_declaration(stmt
->declaration
);
1376 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1378 split_ret_value(stmt
->ret_value
);
1379 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1380 __process_post_op_stack();
1383 case STMT_EXPRESSION
:
1384 expr_set_parent_stmt(stmt
->expression
, stmt
);
1385 expr_set_parent_stmt(stmt
->context
, stmt
);
1387 __split_expr(stmt
->expression
);
1390 split_compound(stmt
);
1393 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1394 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1395 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1397 if (known_condition_true(stmt
->if_conditional
)) {
1398 __split_stmt(stmt
->if_true
);
1401 if (known_condition_false(stmt
->if_conditional
)) {
1402 __split_stmt(stmt
->if_false
);
1405 __split_whole_condition(stmt
->if_conditional
);
1406 __split_stmt(stmt
->if_true
);
1407 if (empty_statement(stmt
->if_true
) &&
1408 last_stmt_on_same_line() &&
1409 !get_macro_name(stmt
->if_true
->pos
))
1410 sm_warning("if();");
1411 __push_true_states();
1412 __use_false_states();
1413 __split_stmt(stmt
->if_false
);
1414 __merge_true_states();
1417 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1418 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1419 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1420 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1421 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1423 if (stmt
->iterator_pre_condition
)
1424 handle_pre_loop(stmt
);
1425 else if (stmt
->iterator_post_condition
)
1426 handle_post_loop(stmt
);
1428 // these are for(;;) type loops.
1429 handle_pre_loop(stmt
);
1433 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1434 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1436 if (get_value(stmt
->switch_expression
, &sval
)) {
1437 split_known_switch(stmt
, sval
);
1440 __split_expr(stmt
->switch_expression
);
1441 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1442 __save_switch_states(top_expression(switch_expr_stack
));
1446 __split_stmt(stmt
->switch_statement
);
1447 if (!__pop_default() && have_remaining_cases())
1448 fake_an_empty_default(stmt
->pos
);
1449 __discard_switches();
1451 pop_expression(&switch_expr_stack
);
1457 __split_label_stmt(stmt
);
1458 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1460 __split_stmt(stmt
->label_statement
);
1463 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1465 __split_expr(stmt
->goto_expression
);
1466 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1467 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1469 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1471 __process_continues();
1473 } else if (stmt
->goto_label
&&
1474 stmt
->goto_label
->type
== SYM_LABEL
&&
1475 stmt
->goto_label
->ident
) {
1476 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1479 if (is_last_stmt(stmt
))
1480 handle_backward_goto(stmt
);
1485 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1487 find_asm_gotos(stmt
);
1488 __pass_to_client(stmt
, ASM_HOOK
);
1489 __split_expr(stmt
->asm_string
);
1490 split_asm_ops(stmt
->asm_outputs
);
1491 split_asm_ops(stmt
->asm_inputs
);
1492 split_expr_list(stmt
->asm_clobbers
, NULL
);
1497 __split_expr(stmt
->range_expression
);
1498 __split_expr(stmt
->range_low
);
1499 __split_expr(stmt
->range_high
);
1503 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1504 if (--indent_cnt
== 1)
1505 free_parsed_call_stuff(true);
1508 __process_post_op_stack();
1510 gettimeofday(&stop
, NULL
);
1511 if (option_time_stmt
&& stmt
)
1512 sm_msg("stmt_time%s: %ld",
1513 stmt
->type
== STMT_COMPOUND
? "_block" : "",
1514 stop
.tv_sec
- start
.tv_sec
);
1517 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1519 struct expression
*expr
;
1521 FOR_EACH_PTR(expr_list
, expr
) {
1522 expr_set_parent_expr(expr
, parent
);
1524 __process_post_op_stack();
1525 } END_FOR_EACH_PTR(expr
);
1528 static bool cast_arg(struct symbol
*type
, struct expression
*arg
)
1530 struct symbol
*orig
;
1535 arg
= strip_parens(arg
);
1536 if (arg
!= strip_expr(arg
))
1539 orig
= get_type(arg
);
1542 if (types_equiv(orig
, type
))
1545 if (orig
->type
== SYM_ARRAY
&& type
->type
== SYM_PTR
)
1549 * I would have expected that we could just do use (orig == type) but I
1550 * guess for pointers we need to get the basetype to do that comparison.
1554 if (orig
->type
!= SYM_PTR
||
1555 type
->type
!= SYM_PTR
) {
1556 if (type_fits(type
, orig
))
1560 orig
= get_real_base_type(orig
);
1561 type
= get_real_base_type(type
);
1568 static struct expression
*fake_a_variable_assign(struct symbol
*type
, struct expression
*call
, struct expression
*expr
, int nr
)
1573 if (!expr
|| !cur_func_sym
)
1576 if (already_parsed_call(call
))
1579 if (expr
->type
== EXPR_ASSIGNMENT
)
1582 /* for va_args then we don't know the type */
1584 type
= get_type(expr
);
1586 cast
= cast_arg(type
, expr
);
1588 * Using expr_to_sym() here is a hack. We want to say that we don't
1589 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1590 * It turns out faking these assignments is way more expensive than I
1591 * would have imagined. I'm not sure why exactly.
1596 * if the code is "return *p;" where "p" is a user pointer then
1597 * we want to create a fake assignment so that it sets the state
1598 * in check_kernel_user_data.c.
1601 if (expr
->type
!= EXPR_PREOP
&&
1602 expr
->op
!= '*' && expr
->op
!= '&' &&
1608 snprintf(buf
, sizeof(buf
), "__fake_return_%p", expr
);
1610 snprintf(buf
, sizeof(buf
), "__fake_param_%p_%d", call
, nr
);
1612 return create_fake_assign(buf
, type
, expr
);
1615 static void split_args(struct expression
*expr
)
1617 struct expression
*arg
, *tmp
;
1618 struct symbol
*type
;
1622 FOR_EACH_PTR(expr
->args
, arg
) {
1624 expr_set_parent_expr(arg
, expr
);
1625 type
= get_arg_type(expr
->fn
, i
);
1626 tmp
= fake_a_variable_assign(type
, expr
, arg
, i
);
1628 __in_fake_var_assign
++;
1631 __in_fake_var_assign
--;
1632 __process_post_op_stack();
1633 } END_FOR_EACH_PTR(arg
);
1636 static void call_cleanup_fn(void *_sym
)
1638 struct symbol
*sym
= _sym
;
1639 struct expression
*call
, *arg
;
1640 struct expression_list
*args
= NULL
;
1645 arg
= symbol_expression(sym
);
1646 arg
= preop_expression(arg
, '&');
1647 add_ptr_list(&args
, arg
);
1648 call
= call_expression(sym
->cleanup
, args
);
1653 static void add_cleanup_hook(struct symbol
*sym
)
1655 add_scope_hook(&call_cleanup_fn
, sym
);
1658 static void split_sym(struct symbol
*sym
)
1662 if (!(sym
->namespace & NS_SYMBOL
))
1665 __split_stmt(sym
->stmt
);
1666 __split_expr(sym
->array_size
);
1668 add_cleanup_hook(sym
);
1669 split_symlist(sym
->arguments
);
1670 split_symlist(sym
->symbol_list
);
1671 __split_stmt(sym
->inline_stmt
);
1672 split_symlist(sym
->inline_symbol_list
);
1675 static void split_symlist(struct symbol_list
*sym_list
)
1679 FOR_EACH_PTR(sym_list
, sym
) {
1681 } END_FOR_EACH_PTR(sym
);
1684 typedef void (fake_cb
)(struct expression
*expr
);
1686 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1688 struct symbol
*type
, *tmp
;
1694 name
= member
->name
;
1696 type
= get_type(expr
);
1697 if (!type
|| type
->type
!= SYM_STRUCT
)
1701 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1705 if (strcmp(name
, tmp
->ident
->name
) == 0)
1707 } END_FOR_EACH_PTR(tmp
);
1711 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1713 struct symbol
*type
, *member
;
1716 type
= get_type(expr
);
1717 if (!type
|| type
->type
!= SYM_STRUCT
)
1720 FOR_EACH_PTR(type
->symbol_list
, member
) {
1722 return member
->ident
;
1724 } END_FOR_EACH_PTR(member
);
1728 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1730 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1732 struct expression
*edge_member
, *assign
;
1733 struct symbol
*base
= get_real_base_type(member
);
1737 expr
= member_expression(expr
, '.', member
->ident
);
1739 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1740 struct symbol
*type
;
1742 type
= get_real_base_type(tmp
);
1746 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1747 if (get_extra_state(edge_member
))
1750 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1751 set_inner_struct_members(expr
, tmp
);
1758 assign
= assign_expression(edge_member
, '=', zero_expr());
1759 __split_expr(assign
);
1760 } END_FOR_EACH_PTR(tmp
);
1765 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1768 struct expression
*member
= NULL
;
1769 struct expression
*assign
;
1771 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1772 type
= get_real_base_type(tmp
);
1777 member
= member_expression(expr
, '.', tmp
->ident
);
1778 if (get_extra_state(member
))
1782 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1783 set_inner_struct_members(expr
, tmp
);
1786 if (type
->type
== SYM_ARRAY
)
1791 assign
= assign_expression(member
, '=', zero_expr());
1792 __split_expr(assign
);
1793 } END_FOR_EACH_PTR(tmp
);
1796 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1798 struct expression
*deref
, *assign
, *tmp
, *right
;
1799 struct symbol
*struct_type
, *type
;
1800 struct ident
*member
;
1803 struct_type
= get_type(symbol
);
1805 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1809 * We're parsing an initializer that could look something like this:
1810 * struct foo foo = {
1812 * .whatever.xxx = 11,
1816 * So what we have here is a list with 42, .whatever, and .zzz. We need
1817 * to break it up into left and right sides of the assignments.
1821 FOR_EACH_PTR(members
, tmp
) {
1823 if (tmp
->type
== EXPR_IDENTIFIER
) {
1824 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1825 while (tmp
->type
== EXPR_IDENTIFIER
) {
1826 member
= tmp
->expr_ident
;
1827 tmp
= tmp
->ident_expression
;
1829 deref
= member_expression(deref
, '.', member
);
1831 deref
= member_expression(symbol
, '.', member
);
1834 member
= number_to_member(symbol
, member_idx
);
1835 deref
= member_expression(symbol
, '.', member
);
1839 if (right
->type
== EXPR_INITIALIZER
) {
1840 type
= get_type(deref
);
1841 if (type
&& type
->type
== SYM_ARRAY
)
1842 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1844 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1846 assign
= assign_expression(deref
, '=', right
);
1849 } END_FOR_EACH_PTR(tmp
);
1851 set_unset_to_zero(struct_type
, symbol
);
1854 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1856 fake_member_assigns_helper(symbol_expression(sym
),
1857 sym
->initializer
->expr_list
, fake_cb
);
1860 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1862 struct expression
*offset
, *binop
, *assign
, *tmp
;
1863 struct symbol
*type
;
1866 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1871 FOR_EACH_PTR(expr_list
, tmp
) {
1872 if (tmp
->type
== EXPR_INDEX
) {
1873 if (tmp
->idx_from
!= tmp
->idx_to
)
1875 idx
= tmp
->idx_from
;
1878 if (!tmp
->idx_expression
)
1880 tmp
= tmp
->idx_expression
;
1882 offset
= value_expr(idx
);
1883 binop
= array_element_expression(array
, offset
);
1884 if (tmp
->type
== EXPR_INITIALIZER
) {
1885 type
= get_type(binop
);
1886 if (type
&& type
->type
== SYM_ARRAY
)
1887 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1889 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1891 assign
= assign_expression(binop
, '=', tmp
);
1898 } END_FOR_EACH_PTR(tmp
);
1900 __call_array_initialized_hooks(array
, max
);
1903 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1905 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1908 static void fake_assign_expr(struct symbol
*sym
)
1910 struct expression
*assign
, *symbol
;
1912 symbol
= symbol_expression(sym
);
1913 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1914 __split_expr(assign
);
1917 static void do_initializer_stuff(struct symbol
*sym
)
1919 if (!sym
->initializer
)
1922 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1923 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1924 fake_element_assigns(sym
, __split_expr
);
1926 fake_member_assigns(sym
, __split_expr
);
1928 fake_assign_expr(sym
);
1932 static void split_declaration(struct symbol_list
*sym_list
)
1936 FOR_EACH_PTR(sym_list
, sym
) {
1937 __pass_to_client(sym
, DECLARATION_HOOK
);
1938 do_initializer_stuff(sym
);
1939 __pass_to_client(sym
, DECLARATION_HOOK_AFTER
);
1941 } END_FOR_EACH_PTR(sym
);
1944 static void call_global_assign_hooks(struct expression
*assign
)
1946 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1949 static void fake_global_assign(struct symbol
*sym
)
1951 struct expression
*assign
, *symbol
;
1953 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1954 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1955 fake_element_assigns(sym
, call_global_assign_hooks
);
1956 } else if (sym
->initializer
) {
1957 symbol
= symbol_expression(sym
);
1958 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1959 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1961 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1963 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1964 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1965 fake_member_assigns(sym
, call_global_assign_hooks
);
1966 } else if (sym
->initializer
) {
1967 symbol
= symbol_expression(sym
);
1968 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1969 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1971 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1974 symbol
= symbol_expression(sym
);
1975 if (sym
->initializer
) {
1976 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1977 __split_expr(assign
);
1979 assign
= assign_expression(symbol
, '=', zero_expr());
1981 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1985 static void start_function_definition(struct symbol
*sym
)
1987 __in_function_def
= 1;
1988 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1989 __in_function_def
= 0;
1990 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1994 void add_function_data(unsigned long *fn_data
)
1996 __add_ptr_list(&fn_data_list
, fn_data
);
1999 static void clear_function_data(void)
2003 FOR_EACH_PTR(fn_data_list
, tmp
) {
2005 } END_FOR_EACH_PTR(tmp
);
2008 static void record_func_time(void)
2010 struct timeval stop
;
2014 gettimeofday(&stop
, NULL
);
2015 func_time
= stop
.tv_sec
- fn_start_time
.tv_sec
;
2016 snprintf(buf
, sizeof(buf
), "%d", func_time
);
2017 sql_insert_return_implies(FUNC_TIME
, 0, "", buf
);
2018 if (option_time
&& func_time
> 2) {
2020 sm_msg("func_time: %d", func_time
);
2025 static void split_function(struct symbol
*sym
)
2027 struct symbol
*base_type
= get_base_type(sym
);
2029 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
2032 gettimeofday(&outer_fn_start_time
, NULL
);
2033 gettimeofday(&fn_start_time
, NULL
);
2036 cur_func
= sym
->ident
->name
;
2037 if (option_process_function
&& cur_func
&&
2038 strcmp(option_process_function
, cur_func
) != 0)
2040 set_position(sym
->pos
);
2041 clear_function_data();
2043 last_goto_statement_handled
= 0;
2044 sm_debug("new function: %s\n", cur_func
);
2046 if (option_two_passes
) {
2050 start_function_definition(sym
);
2051 __split_stmt(base_type
->stmt
);
2052 __split_stmt(base_type
->inline_stmt
);
2058 start_function_definition(sym
);
2059 __split_stmt(base_type
->stmt
);
2060 __split_stmt(base_type
->inline_stmt
);
2061 if (!__path_is_null() &&
2062 cur_func_return_type() == &void_ctype
&&
2063 !__bail_on_rest_of_function
) {
2064 __pass_to_client(NULL
, RETURN_HOOK
);
2067 __pass_to_client(sym
, END_FUNC_HOOK
);
2068 if (need_delayed_scope_hooks())
2069 __call_scope_hooks();
2070 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
2077 cur_func_sym
= NULL
;
2079 free_data_info_allocs();
2080 free_expression_stack(&switch_expr_stack
);
2081 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
2082 __bail_on_rest_of_function
= 0;
2085 static void save_flow_state(void)
2089 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2));
2090 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2));
2091 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2));
2093 __add_ptr_list(&backup
, big_statement_stack
);
2094 __add_ptr_list(&backup
, big_expression_stack
);
2095 __add_ptr_list(&backup
, big_condition_stack
);
2096 __add_ptr_list(&backup
, switch_expr_stack
);
2098 __add_ptr_list(&backup
, cur_func_sym
);
2100 __add_ptr_list(&backup
, parsed_calls
);
2102 __add_ptr_list(&backup
, __prev_stmt
);
2103 __add_ptr_list(&backup
, __cur_stmt
);
2104 __add_ptr_list(&backup
, __next_stmt
);
2106 FOR_EACH_PTR(fn_data_list
, tmp
) {
2107 __add_ptr_list(&backup
, (void *)*tmp
);
2108 } END_FOR_EACH_PTR(tmp
);
2111 static void *pop_backup(void)
2115 ret
= last_ptr_list(backup
);
2116 delete_ptr_list_last(&backup
);
2120 static void restore_flow_state(void)
2124 FOR_EACH_PTR_REVERSE(fn_data_list
, tmp
) {
2125 *tmp
= (unsigned long)pop_backup();
2126 } END_FOR_EACH_PTR_REVERSE(tmp
);
2128 __next_stmt
= pop_backup();
2129 __cur_stmt
= pop_backup();
2130 __prev_stmt
= pop_backup();
2132 parsed_calls
= pop_backup();
2134 cur_func_sym
= pop_backup();
2135 switch_expr_stack
= pop_backup();
2136 big_condition_stack
= pop_backup();
2137 big_expression_stack
= pop_backup();
2138 big_statement_stack
= pop_backup();
2139 final_pass
= PTR_INT(pop_backup()) >> 2;
2140 loop_count
= PTR_INT(pop_backup()) >> 2;
2141 loop_num
= PTR_INT(pop_backup()) >> 2;
2144 void parse_inline(struct expression
*call
)
2146 struct symbol
*base_type
;
2147 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
2148 struct timeval time_backup
= fn_start_time
;
2149 struct expression
*orig_inline
= __inline_fn
;
2152 if (out_of_memory() || taking_too_long())
2155 if (already_parsed_call(call
))
2160 __pass_to_client(call
, INLINE_FN_START
);
2161 final_pass
= 0; /* don't print anything */
2163 orig_budget
= inline_budget
;
2164 inline_budget
= inline_budget
- 5;
2166 base_type
= get_base_type(call
->fn
->symbol
);
2167 cur_func_sym
= call
->fn
->symbol
;
2168 if (call
->fn
->symbol
->ident
)
2169 cur_func
= call
->fn
->symbol
->ident
->name
;
2172 set_position(call
->fn
->symbol
->pos
);
2175 big_statement_stack
= NULL
;
2176 big_expression_stack
= NULL
;
2177 big_condition_stack
= NULL
;
2178 switch_expr_stack
= NULL
;
2179 parsed_calls
= NULL
;
2181 sm_debug("inline function: %s\n", cur_func
);
2183 clear_function_data();
2186 start_function_definition(call
->fn
->symbol
);
2187 __split_stmt(base_type
->stmt
);
2188 __split_stmt(base_type
->inline_stmt
);
2189 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
2190 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
2191 call
->fn
->symbol
->parsed
= true;
2193 free_expression_stack(&switch_expr_stack
);
2194 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
2198 restore_flow_state();
2199 fn_start_time
= time_backup
;
2200 cur_func
= cur_func_bak
;
2202 restore_all_states();
2203 set_position(call
->pos
);
2204 __inline_fn
= orig_inline
;
2205 inline_budget
= orig_budget
;
2206 __pass_to_client(call
, INLINE_FN_END
);
2209 static struct symbol_list
*inlines_called
;
2210 static void add_inline_function(struct symbol
*sym
)
2212 static struct symbol_list
*already_added
;
2215 FOR_EACH_PTR(already_added
, tmp
) {
2218 } END_FOR_EACH_PTR(tmp
);
2220 add_ptr_list(&already_added
, sym
);
2221 add_ptr_list(&inlines_called
, sym
);
2224 static void process_inlines(void)
2228 FOR_EACH_PTR(inlines_called
, tmp
) {
2229 split_function(tmp
);
2230 } END_FOR_EACH_PTR(tmp
);
2231 free_ptr_list(&inlines_called
);
2234 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
2238 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
2241 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
2243 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
2245 } END_FOR_EACH_PTR_REVERSE(sym
);
2250 static bool interesting_function(struct symbol
*sym
)
2252 static int prev_stream
= -1;
2253 static bool prev_answer
;
2254 const char *filename
;
2257 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
2260 if (sym
->pos
.stream
== prev_stream
)
2263 prev_stream
= sym
->pos
.stream
;
2264 prev_answer
= false;
2266 filename
= stream_name(sym
->pos
.stream
);
2267 len
= strlen(filename
);
2268 if (len
> 0 && filename
[len
- 1] == 'c')
2273 static void split_inlines_in_scope(struct symbol
*sym
)
2275 struct symbol
*base
;
2276 struct symbol_list
*scope_list
;
2279 scope_list
= sym
->scope
->symbols
;
2280 stream
= sym
->pos
.stream
;
2282 /* find the last static symbol in the file */
2283 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
2284 if (sym
->pos
.stream
!= stream
)
2286 if (sym
->type
!= SYM_NODE
)
2288 base
= get_base_type(sym
);
2291 if (base
->type
!= SYM_FN
)
2293 if (!base
->inline_stmt
)
2295 if (!interesting_function(sym
))
2297 add_inline_function(sym
);
2298 } END_FOR_EACH_PTR_REVERSE(sym
);
2303 static void split_inlines(struct symbol_list
*sym_list
)
2307 sym
= get_last_scoped_symbol(sym_list
, 0);
2309 split_inlines_in_scope(sym
);
2310 sym
= get_last_scoped_symbol(sym_list
, 1);
2312 split_inlines_in_scope(sym
);
2315 static struct stree
*clone_estates_perm(struct stree
*orig
)
2317 struct stree
*ret
= NULL
;
2318 struct sm_state
*tmp
;
2320 FOR_EACH_SM(orig
, tmp
) {
2321 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
2322 } END_FOR_EACH_SM(tmp
);
2327 struct position last_pos
;
2328 static void split_c_file_functions(struct symbol_list
*sym_list
)
2333 FOR_EACH_PTR(sym_list
, sym
) {
2334 set_position(sym
->pos
);
2335 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
2336 __pass_to_client(sym
, BASE_HOOK
);
2337 fake_global_assign(sym
);
2338 __pass_to_client(sym
, DECLARATION_HOOK_AFTER
);
2340 } END_FOR_EACH_PTR(sym
);
2341 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
2344 FOR_EACH_PTR(sym_list
, sym
) {
2345 set_position(sym
->pos
);
2346 last_pos
= sym
->pos
;
2347 if (!interesting_function(sym
))
2349 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
2350 split_function(sym
);
2353 last_pos
= sym
->pos
;
2354 } END_FOR_EACH_PTR(sym
);
2355 split_inlines(sym_list
);
2356 __pass_to_client(sym_list
, END_FILE_HOOK
);
2359 static int final_before_fake
;
2360 void init_fake_env(void)
2363 final_before_fake
= final_pass
;
2365 __push_fake_cur_stree();
2369 void end_fake_env(void)
2371 __free_fake_cur_stree();
2374 final_pass
= final_before_fake
;
2377 static void open_output_files(char *base_file
)
2381 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
2382 sm_outfd
= fopen(buf
, "w");
2384 sm_fatal("Cannot open %s", buf
);
2389 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
2390 sql_outfd
= fopen(buf
, "w");
2392 sm_fatal("Error: Cannot open %s", buf
);
2394 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
2395 caller_info_fd
= fopen(buf
, "w");
2396 if (!caller_info_fd
)
2397 sm_fatal("Error: Cannot open %s", buf
);
2400 void smatch(struct string_list
*filelist
)
2402 struct symbol_list
*sym_list
;
2403 struct timeval stop
, start
;
2407 gettimeofday(&start
, NULL
);
2409 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
2410 path
= getcwd(NULL
, 0);
2411 free(full_base_file
);
2413 len
= strlen(path
) + 1 + strlen(base_file
) + 1;
2414 full_base_file
= malloc(len
);
2415 snprintf(full_base_file
, len
, "%s/%s", path
, base_file
);
2417 full_base_file
= alloc_string(base_file
);
2419 if (option_file_output
)
2420 open_output_files(base_file
);
2421 base_file_stream
= input_stream_nr
;
2422 sym_list
= sparse_keep_tokens(base_file
);
2423 split_c_file_functions(sym_list
);
2424 } END_FOR_EACH_PTR_NOTAG(base_file
);
2426 gettimeofday(&stop
, NULL
);
2428 set_position(last_pos
);
2431 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
2433 sm_msg("mem: %luKb", get_max_memory());