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 __push_scope_hooks();
862 loop_name
= get_loop_name(loop_num
);
865 split_declaration(stmt
->iterator_syms
);
866 if (stmt
->iterator_pre_statement
) {
867 __split_stmt(stmt
->iterator_pre_statement
);
868 __prev_stmt
= stmt
->iterator_pre_statement
;
871 once_through
= call_once_through_hooks(stmt
);
877 __merge_gotos(loop_name
, NULL
);
879 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
880 __in_pre_condition
++;
881 __pass_to_client(stmt
, PRELOOP_HOOK
);
882 __split_whole_condition(stmt
->iterator_pre_condition
);
883 __in_pre_condition
--;
884 FOR_EACH_SM(stree
, sm
) {
885 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
886 } END_FOR_EACH_SM(sm
);
889 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
891 __split_stmt(stmt
->iterator_statement
);
892 if (is_forever_loop(stmt
)) {
894 __save_gotos(loop_name
, NULL
);
896 __push_fake_cur_stree();
897 __split_stmt(stmt
->iterator_post_statement
);
898 stree
= __pop_fake_cur_stree();
900 __discard_false_states();
901 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
904 if (!__path_is_null())
905 __merge_stree_into_cur(stree
);
909 unchanged
= __iterator_unchanged(extra_sm
);
910 __split_stmt(stmt
->iterator_post_statement
);
911 __prev_stmt
= stmt
->iterator_post_statement
;
914 __save_gotos(loop_name
, NULL
);
915 __in_pre_condition
++;
916 __split_whole_condition(stmt
->iterator_pre_condition
);
917 __in_pre_condition
--;
919 __merge_false_states();
921 __discard_false_states();
923 __merge_false_states();
925 if (extra_sm
&& unchanged
)
926 __extra_pre_loop_hook_after(extra_sm
,
927 stmt
->iterator_post_statement
,
928 stmt
->iterator_pre_condition
);
929 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
934 __call_scope_hooks();
938 * Post loops are do {} while();
940 static void handle_post_loop(struct statement
*stmt
)
944 loop_name
= get_loop_name(loop_num
);
948 __pass_to_client(stmt
, POSTLOOP_HOOK
);
952 __merge_gotos(loop_name
, NULL
);
953 __split_stmt(stmt
->iterator_statement
);
955 if (!expr_is_zero(stmt
->iterator_post_condition
))
956 __save_gotos(loop_name
, NULL
);
958 if (is_forever_loop(stmt
)) {
959 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
962 __split_whole_condition(stmt
->iterator_post_condition
);
963 __use_false_states();
964 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
970 static int empty_statement(struct statement
*stmt
)
974 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
979 static int last_stmt_on_same_line(void)
981 struct statement
*stmt
;
984 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
987 if (stmt
->pos
.line
== get_lineno())
990 } END_FOR_EACH_PTR_REVERSE(stmt
);
994 static void split_asm_ops(struct asm_operand_list
*ops
)
996 struct asm_operand
*op
;
998 FOR_EACH_PTR(ops
, op
) {
999 __split_expr(op
->expr
);
1000 } END_FOR_EACH_PTR(op
);
1003 static int is_case_val(struct statement
*stmt
, sval_t sval
)
1007 if (stmt
->type
!= STMT_CASE
)
1009 if (!stmt
->case_expression
) {
1013 if (!get_value(stmt
->case_expression
, &case_sval
))
1015 if (case_sval
.value
== sval
.value
)
1020 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
1021 struct expression
*case_expr
,
1022 struct expression
*case_to
)
1025 struct range_list
*rl
= NULL
;
1026 struct symbol
*switch_type
;
1028 switch_type
= get_type(switch_expr
);
1029 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
1030 start
= sval_cast(switch_type
, start
);
1031 end
= sval_cast(switch_type
, end
);
1032 add_range(&rl
, start
, end
);
1033 } else if (get_value(case_expr
, &start
)) {
1034 start
= sval_cast(switch_type
, start
);
1035 add_range(&rl
, start
, start
);
1041 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
1043 struct statement
*tmp
;
1044 struct range_list
*rl
;
1046 __split_expr(stmt
->switch_expression
);
1047 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
1049 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1050 __save_switch_states(top_expression(switch_expr_stack
));
1055 stmt
= stmt
->switch_statement
;
1057 __push_scope_hooks();
1058 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
1059 __smatch_lineno
= tmp
->pos
.line
;
1060 // FIXME: what if default comes before the known case statement?
1061 if (is_case_val(tmp
, sval
)) {
1062 rl
= alloc_rl(sval
, sval
);
1063 __merge_switches(top_expression(switch_expr_stack
), rl
);
1064 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
1065 stmt_set_parent_stmt(tmp
->case_statement
, tmp
);
1066 __split_stmt(tmp
->case_statement
);
1069 if (__path_is_null())
1073 if (__path_is_null()) {
1077 } END_FOR_EACH_PTR(tmp
);
1079 __call_scope_hooks();
1080 if (!__pop_default())
1081 __merge_switches(top_expression(switch_expr_stack
), NULL
);
1082 __discard_switches();
1084 pop_expression(&switch_expr_stack
);
1087 static void split_case(struct statement
*stmt
)
1089 struct range_list
*rl
= NULL
;
1091 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
1092 expr_set_parent_stmt(stmt
->case_to
, stmt
);
1094 rl
= get_case_rl(top_expression(switch_expr_stack
),
1095 stmt
->case_expression
, stmt
->case_to
);
1096 while (stmt
->case_statement
->type
== STMT_CASE
) {
1097 struct range_list
*tmp
;
1099 tmp
= get_case_rl(top_expression(switch_expr_stack
),
1100 stmt
->case_statement
->case_expression
,
1101 stmt
->case_statement
->case_to
);
1104 rl
= rl_union(rl
, tmp
);
1105 if (!stmt
->case_expression
)
1108 stmt
= stmt
->case_statement
;
1111 __merge_switches(top_expression(switch_expr_stack
), rl
);
1113 if (!stmt
->case_expression
)
1116 stmt_set_parent_stmt(stmt
->case_statement
, stmt
);
1117 __split_stmt(stmt
->case_statement
);
1120 int time_parsing_function(void)
1122 return ms_since(&fn_start_time
) / 1000;
1125 bool taking_too_long(void)
1127 if ((ms_since(&outer_fn_start_time
) / 1000) > 60 * 5) /* five minutes */
1132 struct statement
*get_last_stmt(void)
1135 struct statement
*stmt
;
1137 fn
= get_base_type(cur_func_sym
);
1142 stmt
= fn
->inline_stmt
;
1143 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
1145 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
1146 if (stmt
&& stmt
->type
== STMT_LABEL
)
1147 stmt
= stmt
->label_statement
;
1151 int is_last_stmt(struct statement
*cur_stmt
)
1153 struct statement
*last
;
1155 last
= get_last_stmt();
1156 if (last
&& last
== cur_stmt
)
1161 static bool is_function_scope(struct statement
*stmt
)
1163 struct symbol
*base_type
;
1168 base_type
= get_base_type(cur_func_sym
);
1169 if (base_type
->stmt
== stmt
||
1170 base_type
->inline_stmt
== stmt
)
1176 static void handle_backward_goto(struct statement
*goto_stmt
)
1178 const char *goto_name
, *label_name
;
1179 struct statement
*func_stmt
;
1180 struct symbol
*base_type
= get_base_type(cur_func_sym
);
1181 struct statement
*tmp
;
1186 if (last_goto_statement_handled
)
1188 last_goto_statement_handled
= 1;
1190 if (!goto_stmt
->goto_label
||
1191 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
1192 !goto_stmt
->goto_label
->ident
)
1194 goto_name
= goto_stmt
->goto_label
->ident
->name
;
1196 func_stmt
= base_type
->stmt
;
1198 func_stmt
= base_type
->inline_stmt
;
1201 if (func_stmt
->type
!= STMT_COMPOUND
)
1204 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
1206 if (tmp
->type
!= STMT_LABEL
)
1208 if (!tmp
->label_identifier
||
1209 tmp
->label_identifier
->type
!= SYM_LABEL
||
1210 !tmp
->label_identifier
->ident
)
1212 label_name
= tmp
->label_identifier
->ident
->name
;
1213 if (strcmp(goto_name
, label_name
) != 0)
1218 } END_FOR_EACH_PTR(tmp
);
1221 static void fake_a_return(void)
1223 struct expression
*ret
= NULL
;
1228 if (cur_func_return_type() != &void_ctype
)
1229 ret
= unknown_value_expression(NULL
);
1231 __pass_to_client(ret
, RETURN_HOOK
);
1235 static void split_ret_value(struct expression
*expr
)
1237 struct symbol
*type
;
1242 type
= get_real_base_type(cur_func_sym
);
1243 type
= get_real_base_type(type
);
1244 expr
= fake_a_variable_assign(type
, NULL
, expr
, -1);
1246 __in_fake_var_assign
++;
1248 __in_fake_var_assign
--;
1251 static void fake_an_empty_default(struct position pos
)
1253 static struct statement none
= {};
1256 none
.type
= STMT_NONE
;
1257 __merge_switches(top_expression(switch_expr_stack
), NULL
);
1258 __split_stmt(&none
);
1261 static void split_compound(struct statement
*stmt
)
1263 struct statement
*prev
= NULL
;
1264 struct statement
*cur
= NULL
;
1265 struct statement
*next
;
1267 __push_scope_hooks();
1269 FOR_EACH_PTR(stmt
->stmts
, next
) {
1270 /* just set them all ahead of time */
1271 stmt_set_parent_stmt(next
, stmt
);
1281 } END_FOR_EACH_PTR(next
);
1290 * For function scope, then delay calling the scope hooks until the
1291 * end of function hooks can run.
1293 if (!is_function_scope(stmt
))
1294 __call_scope_hooks();
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 static void parse_fn_statements(struct symbol
*base
)
1996 __split_stmt(base
->stmt
);
1997 __split_stmt(base
->inline_stmt
);
2000 void add_function_data(unsigned long *fn_data
)
2002 __add_ptr_list(&fn_data_list
, fn_data
);
2005 static void clear_function_data(void)
2009 FOR_EACH_PTR(fn_data_list
, tmp
) {
2011 } END_FOR_EACH_PTR(tmp
);
2014 static void record_func_time(void)
2016 struct timeval stop
;
2020 gettimeofday(&stop
, NULL
);
2021 func_time
= stop
.tv_sec
- fn_start_time
.tv_sec
;
2022 snprintf(buf
, sizeof(buf
), "%d", func_time
);
2023 sql_insert_return_implies(FUNC_TIME
, 0, "", buf
);
2024 if (option_time
&& func_time
> 2) {
2026 sm_msg("func_time: %d", func_time
);
2031 static void split_function(struct symbol
*sym
)
2033 struct symbol
*base_type
= get_base_type(sym
);
2035 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
2038 gettimeofday(&outer_fn_start_time
, NULL
);
2039 gettimeofday(&fn_start_time
, NULL
);
2042 cur_func
= sym
->ident
->name
;
2043 if (option_process_function
&& cur_func
&&
2044 strcmp(option_process_function
, cur_func
) != 0)
2046 set_position(sym
->pos
);
2047 clear_function_data();
2049 last_goto_statement_handled
= 0;
2050 sm_debug("new function: %s\n", cur_func
);
2052 if (option_two_passes
) {
2056 start_function_definition(sym
);
2057 parse_fn_statements(base_type
);
2058 __call_scope_hooks();
2064 start_function_definition(sym
);
2065 parse_fn_statements(base_type
);
2066 if (!__path_is_null() &&
2067 cur_func_return_type() == &void_ctype
&&
2068 !__bail_on_rest_of_function
) {
2069 __pass_to_client(NULL
, RETURN_HOOK
);
2072 __pass_to_client(sym
, END_FUNC_HOOK
);
2073 __call_scope_hooks();
2074 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
2081 cur_func_sym
= NULL
;
2083 free_data_info_allocs();
2084 free_expression_stack(&switch_expr_stack
);
2085 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
2086 __bail_on_rest_of_function
= 0;
2089 static void save_flow_state(void)
2093 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2));
2094 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2));
2095 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2));
2097 __add_ptr_list(&backup
, big_statement_stack
);
2098 __add_ptr_list(&backup
, big_expression_stack
);
2099 __add_ptr_list(&backup
, big_condition_stack
);
2100 __add_ptr_list(&backup
, switch_expr_stack
);
2102 __add_ptr_list(&backup
, cur_func_sym
);
2104 __add_ptr_list(&backup
, parsed_calls
);
2106 __add_ptr_list(&backup
, __prev_stmt
);
2107 __add_ptr_list(&backup
, __cur_stmt
);
2108 __add_ptr_list(&backup
, __next_stmt
);
2110 FOR_EACH_PTR(fn_data_list
, tmp
) {
2111 __add_ptr_list(&backup
, (void *)*tmp
);
2112 } END_FOR_EACH_PTR(tmp
);
2115 static void *pop_backup(void)
2119 ret
= last_ptr_list(backup
);
2120 delete_ptr_list_last(&backup
);
2124 static void restore_flow_state(void)
2128 FOR_EACH_PTR_REVERSE(fn_data_list
, tmp
) {
2129 *tmp
= (unsigned long)pop_backup();
2130 } END_FOR_EACH_PTR_REVERSE(tmp
);
2132 __next_stmt
= pop_backup();
2133 __cur_stmt
= pop_backup();
2134 __prev_stmt
= pop_backup();
2136 parsed_calls
= pop_backup();
2138 cur_func_sym
= pop_backup();
2139 switch_expr_stack
= pop_backup();
2140 big_condition_stack
= pop_backup();
2141 big_expression_stack
= pop_backup();
2142 big_statement_stack
= pop_backup();
2143 final_pass
= PTR_INT(pop_backup()) >> 2;
2144 loop_count
= PTR_INT(pop_backup()) >> 2;
2145 loop_num
= PTR_INT(pop_backup()) >> 2;
2148 void parse_inline(struct expression
*call
)
2150 struct symbol
*base_type
;
2151 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
2152 struct timeval time_backup
= fn_start_time
;
2153 struct expression
*orig_inline
= __inline_fn
;
2156 if (out_of_memory() || taking_too_long())
2159 if (already_parsed_call(call
))
2164 __pass_to_client(call
, INLINE_FN_START
);
2165 final_pass
= 0; /* don't print anything */
2167 orig_budget
= inline_budget
;
2168 inline_budget
= inline_budget
- 5;
2170 base_type
= get_base_type(call
->fn
->symbol
);
2171 cur_func_sym
= call
->fn
->symbol
;
2172 if (call
->fn
->symbol
->ident
)
2173 cur_func
= call
->fn
->symbol
->ident
->name
;
2176 set_position(call
->fn
->symbol
->pos
);
2179 big_statement_stack
= NULL
;
2180 big_expression_stack
= NULL
;
2181 big_condition_stack
= NULL
;
2182 switch_expr_stack
= NULL
;
2183 parsed_calls
= NULL
;
2185 sm_debug("inline function: %s\n", cur_func
);
2187 clear_function_data();
2190 start_function_definition(call
->fn
->symbol
);
2191 parse_fn_statements(base_type
);
2192 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
2193 __call_scope_hooks();
2194 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
2195 call
->fn
->symbol
->parsed
= true;
2197 free_expression_stack(&switch_expr_stack
);
2198 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
2202 restore_flow_state();
2203 fn_start_time
= time_backup
;
2204 cur_func
= cur_func_bak
;
2206 restore_all_states();
2207 set_position(call
->pos
);
2208 __inline_fn
= orig_inline
;
2209 inline_budget
= orig_budget
;
2210 __pass_to_client(call
, INLINE_FN_END
);
2213 static struct symbol_list
*inlines_called
;
2214 static void add_inline_function(struct symbol
*sym
)
2216 static struct symbol_list
*already_added
;
2219 FOR_EACH_PTR(already_added
, tmp
) {
2222 } END_FOR_EACH_PTR(tmp
);
2224 add_ptr_list(&already_added
, sym
);
2225 add_ptr_list(&inlines_called
, sym
);
2228 static void process_inlines(void)
2232 FOR_EACH_PTR(inlines_called
, tmp
) {
2233 split_function(tmp
);
2234 } END_FOR_EACH_PTR(tmp
);
2235 free_ptr_list(&inlines_called
);
2238 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
2242 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
2245 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
2247 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
2249 } END_FOR_EACH_PTR_REVERSE(sym
);
2254 static bool interesting_function(struct symbol
*sym
)
2256 static int prev_stream
= -1;
2257 static bool prev_answer
;
2258 const char *filename
;
2261 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
2264 if (sym
->pos
.stream
== prev_stream
)
2267 prev_stream
= sym
->pos
.stream
;
2268 prev_answer
= false;
2270 filename
= stream_name(sym
->pos
.stream
);
2271 len
= strlen(filename
);
2272 if (len
> 0 && filename
[len
- 1] == 'c')
2277 static void split_inlines_in_scope(struct symbol
*sym
)
2279 struct symbol
*base
;
2280 struct symbol_list
*scope_list
;
2283 scope_list
= sym
->scope
->symbols
;
2284 stream
= sym
->pos
.stream
;
2286 /* find the last static symbol in the file */
2287 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
2288 if (sym
->pos
.stream
!= stream
)
2290 if (sym
->type
!= SYM_NODE
)
2292 base
= get_base_type(sym
);
2295 if (base
->type
!= SYM_FN
)
2297 if (!base
->inline_stmt
)
2299 if (!interesting_function(sym
))
2301 add_inline_function(sym
);
2302 } END_FOR_EACH_PTR_REVERSE(sym
);
2307 static void split_inlines(struct symbol_list
*sym_list
)
2311 sym
= get_last_scoped_symbol(sym_list
, 0);
2313 split_inlines_in_scope(sym
);
2314 sym
= get_last_scoped_symbol(sym_list
, 1);
2316 split_inlines_in_scope(sym
);
2319 static struct stree
*clone_estates_perm(struct stree
*orig
)
2321 struct stree
*ret
= NULL
;
2322 struct sm_state
*tmp
;
2324 FOR_EACH_SM(orig
, tmp
) {
2325 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
2326 } END_FOR_EACH_SM(tmp
);
2331 struct position last_pos
;
2332 static void split_c_file_functions(struct symbol_list
*sym_list
)
2337 FOR_EACH_PTR(sym_list
, sym
) {
2338 set_position(sym
->pos
);
2339 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
2340 __pass_to_client(sym
, BASE_HOOK
);
2341 fake_global_assign(sym
);
2342 __pass_to_client(sym
, DECLARATION_HOOK_AFTER
);
2344 } END_FOR_EACH_PTR(sym
);
2345 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
2348 FOR_EACH_PTR(sym_list
, sym
) {
2349 set_position(sym
->pos
);
2350 last_pos
= sym
->pos
;
2351 if (!interesting_function(sym
))
2353 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
2354 split_function(sym
);
2357 last_pos
= sym
->pos
;
2358 } END_FOR_EACH_PTR(sym
);
2359 split_inlines(sym_list
);
2360 __pass_to_client(sym_list
, END_FILE_HOOK
);
2363 static int final_before_fake
;
2364 void init_fake_env(void)
2367 final_before_fake
= final_pass
;
2369 __push_fake_cur_stree();
2373 void end_fake_env(void)
2375 __free_fake_cur_stree();
2378 final_pass
= final_before_fake
;
2381 static void open_output_files(char *base_file
)
2385 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
2386 sm_outfd
= fopen(buf
, "w");
2388 sm_fatal("Cannot open %s", buf
);
2393 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
2394 sql_outfd
= fopen(buf
, "w");
2396 sm_fatal("Error: Cannot open %s", buf
);
2398 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
2399 caller_info_fd
= fopen(buf
, "w");
2400 if (!caller_info_fd
)
2401 sm_fatal("Error: Cannot open %s", buf
);
2404 void smatch(struct string_list
*filelist
)
2406 struct symbol_list
*sym_list
;
2407 struct timeval stop
, start
;
2411 gettimeofday(&start
, NULL
);
2413 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
2414 path
= getcwd(NULL
, 0);
2415 free(full_base_file
);
2417 len
= strlen(path
) + 1 + strlen(base_file
) + 1;
2418 full_base_file
= malloc(len
);
2419 snprintf(full_base_file
, len
, "%s/%s", path
, base_file
);
2421 full_base_file
= alloc_string(base_file
);
2423 if (option_file_output
)
2424 open_output_files(base_file
);
2425 base_file_stream
= input_stream_nr
;
2426 sym_list
= sparse_keep_tokens(base_file
);
2427 split_c_file_functions(sym_list
);
2428 } END_FOR_EACH_PTR_NOTAG(base_file
);
2430 gettimeofday(&stop
, NULL
);
2432 set_position(last_pos
);
2435 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
2437 sm_msg("mem: %luKb", get_max_memory());