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
;
30 int __in_fake_var_assign
;
35 struct expression
*__inline_fn
;
37 int __smatch_lineno
= 0;
39 static char *base_file
;
40 static const char *filename
;
41 static char *pathname
;
42 static char *full_filename
;
43 static char *full_base_file
;
44 static char *cur_func
;
46 static unsigned int loop_count
;
47 static int last_goto_statement_handled
;
48 int __expr_stmt_count
;
49 int __in_function_def
;
50 int __in_unmatched_hook
;
51 static struct expression_list
*switch_expr_stack
= NULL
;
52 static struct expression_list
*post_op_stack
= NULL
;
54 static struct ptr_list
*fn_data_list
;
55 static struct ptr_list
*backup
;
57 struct expression_list
*big_expression_stack
;
58 struct statement_list
*big_statement_stack
;
59 struct statement
*__prev_stmt
;
60 struct statement
*__cur_stmt
;
61 struct statement
*__next_stmt
;
62 int __in_pre_condition
= 0;
63 int __bail_on_rest_of_function
= 0;
64 static struct timeval fn_start_time
;
65 static struct timeval outer_fn_start_time
;
66 char *get_function(void) { return cur_func
; }
67 int get_lineno(void) { return __smatch_lineno
; }
68 int inside_loop(void) { return !!loop_count
; }
69 int definitely_inside_loop(void) { return !!(loop_count
& ~0x08000000); }
70 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
71 int in_expression_statement(void) { return !!__expr_stmt_count
; }
73 static void split_symlist(struct symbol_list
*sym_list
);
74 static void split_declaration(struct symbol_list
*sym_list
);
75 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
);
76 static void split_args(struct expression
*expr
);
77 static struct expression
*fake_a_variable_assign(struct symbol
*type
, struct expression
*call
, struct expression
*expr
, int nr
);
78 static void add_inline_function(struct symbol
*sym
);
79 static void parse_inline(struct expression
*expr
);
81 int option_assume_loops
= 0;
82 int option_two_passes
= 0;
83 struct symbol
*cur_func_sym
= NULL
;
84 struct stree
*global_states
;
86 const unsigned long valid_ptr_min
= 4096;
87 unsigned long valid_ptr_max
= ULONG_MAX
& ~(MTAG_OFFSET_MASK
);
88 const sval_t valid_ptr_min_sval
= {
92 sval_t valid_ptr_max_sval
= {
94 {.value
= ULONG_MAX
& ~(MTAG_OFFSET_MASK
)},
96 struct range_list
*valid_ptr_rl
;
98 void alloc_valid_ptr_rl(void)
100 valid_ptr_max
= sval_type_max(&ulong_ctype
).value
& ~(MTAG_OFFSET_MASK
);
101 valid_ptr_max_sval
.value
= valid_ptr_max
;
103 valid_ptr_rl
= alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
104 valid_ptr_rl
= cast_rl(&ptr_ctype
, valid_ptr_rl
);
105 valid_ptr_rl
= clone_rl_permanent(valid_ptr_rl
);
108 int outside_of_function(void)
110 return cur_func_sym
== NULL
;
113 const char *get_filename(void)
115 if (option_info
&& option_full_path
)
116 return full_base_file
;
119 if (option_full_path
)
120 return full_filename
;
124 const char *get_base_file(void)
126 if (option_full_path
)
127 return full_base_file
;
131 unsigned long long get_file_id(void)
133 return str_to_llu_hash(get_filename());
136 unsigned long long get_base_file_id(void)
138 return str_to_llu_hash(get_base_file());
141 static void set_position(struct position pos
)
144 static int prev_stream
= -1;
149 if (pos
.stream
== 0 && pos
.line
== 0)
152 __smatch_lineno
= pos
.line
;
154 if (pos
.stream
== prev_stream
)
157 filename
= stream_name(pos
.stream
);
160 pathname
= getcwd(NULL
, 0);
162 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
163 full_filename
= malloc(len
);
164 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
166 full_filename
= alloc_string(filename
);
171 int is_assigned_call(struct expression
*expr
)
173 struct expression
*parent
= expr_get_parent_expr(expr
);
176 parent
->type
== EXPR_ASSIGNMENT
&&
178 strip_expr(parent
->right
) == expr
)
184 int is_fake_assigned_call(struct expression
*expr
)
186 struct expression
*parent
= expr_get_fake_parent_expr(expr
);
189 parent
->type
== EXPR_ASSIGNMENT
&&
191 strip_expr(parent
->right
) == expr
)
197 static bool is_inline_func(struct expression
*expr
)
199 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
201 if (!expr
->symbol
->definition
)
203 if (expr
->symbol
->definition
->ctype
.modifiers
& MOD_INLINE
)
209 static int is_noreturn_func(struct expression
*expr
)
211 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
215 * It's almost impossible for Smatch to handle __builtin_constant_p()
216 * the same way that GCC does so Smatch ends up making some functions
217 * as no return functions incorrectly.
220 if (option_project
== PROJ_KERNEL
&& expr
->symbol
->ident
&&
221 strstr(expr
->symbol
->ident
->name
, "__compiletime_assert"))
224 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
229 static int save_func_time(void *_rl
, int argc
, char **argv
, char **azColName
)
231 unsigned long *rl
= _rl
;
233 *rl
= strtoul(argv
[0], NULL
, 10);
237 static int get_func_time(struct symbol
*sym
)
239 unsigned long time
= 0;
241 run_sql(&save_func_time
, &time
,
242 "select key from return_implies where %s and type = %d;",
243 get_static_filter(sym
), FUNC_TIME
);
248 static int inline_budget
= 20;
250 int inlinable(struct expression
*expr
)
253 struct statement
*last_stmt
= NULL
;
255 if (__inline_fn
) /* don't nest */
258 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
260 if (is_no_inline_function(expr
->symbol
->ident
->name
))
262 sym
= get_base_type(expr
->symbol
);
263 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
264 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
266 if (sym
->stmt
->type
!= STMT_COMPOUND
)
268 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
270 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
271 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
273 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
275 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
281 /* the magic numbers in this function are pulled out of my bum. */
282 if (last_stmt
->pos
.line
> sym
->pos
.line
+ inline_budget
)
285 if (get_func_time(expr
->symbol
) >= 2)
291 void __process_post_op_stack(void)
293 struct expression
*expr
;
295 FOR_EACH_PTR(post_op_stack
, expr
) {
296 __pass_to_client(expr
, OP_HOOK
);
297 } END_FOR_EACH_PTR(expr
);
299 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
302 static int handle_comma_assigns(struct expression
*expr
)
304 struct expression
*right
;
305 struct expression
*assign
;
307 right
= strip_expr(expr
->right
);
308 if (right
->type
!= EXPR_COMMA
)
311 __split_expr(right
->left
);
312 __process_post_op_stack();
314 assign
= assign_expression(expr
->left
, '=', right
->right
);
315 __split_expr(assign
);
320 /* This is to handle *p++ = foo; assignments */
321 static int handle_postop_assigns(struct expression
*expr
)
323 struct expression
*left
, *fake_left
;
324 struct expression
*assign
;
326 left
= strip_expr(expr
->left
);
327 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '*')
329 left
= strip_expr(left
->unop
);
330 if (left
->type
!= EXPR_POSTOP
)
333 fake_left
= deref_expression(strip_expr(left
->unop
));
334 assign
= assign_expression(fake_left
, '=', expr
->right
);
336 __split_expr(assign
);
337 __split_expr(expr
->left
);
342 static int prev_expression_is_getting_address(struct expression
*expr
)
344 struct expression
*parent
;
347 parent
= expr_get_parent_expr(expr
);
351 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
353 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
355 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
357 /* Handle &foo->array[offset] */
358 if (parent
->type
== EXPR_BINOP
&& parent
->op
== '+') {
359 parent
= expr_get_parent_expr(parent
);
362 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '*')
372 int __in_builtin_overflow_func
;
373 static void handle_builtin_overflow_func(struct expression
*expr
)
375 struct expression
*a
, *b
, *res
, *assign
;
378 if (sym_name_is("__builtin_add_overflow", expr
->fn
))
380 else if (sym_name_is("__builtin_sub_overflow", expr
->fn
))
382 else if (sym_name_is("__builtin_mul_overflow", expr
->fn
))
387 a
= get_argument_from_call_expr(expr
->args
, 0);
388 b
= get_argument_from_call_expr(expr
->args
, 1);
389 res
= get_argument_from_call_expr(expr
->args
, 2);
391 assign
= assign_expression(deref_expression(res
), '=', binop_expression(a
, op
, b
));
393 __in_builtin_overflow_func
++;
394 __split_expr(assign
);
395 __in_builtin_overflow_func
--;
398 static int handle__builtin_choose_expr(struct expression
*expr
)
400 struct expression
*const_expr
, *expr1
, *expr2
;
403 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
406 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
407 expr1
= get_argument_from_call_expr(expr
->args
, 1);
408 expr2
= get_argument_from_call_expr(expr
->args
, 2);
410 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
419 static int handle__builtin_choose_expr_assigns(struct expression
*expr
)
421 struct expression
*const_expr
, *right
, *expr1
, *expr2
, *fake
;
424 right
= strip_parens(expr
->right
);
425 if (right
->type
!= EXPR_CALL
)
427 if (!sym_name_is("__builtin_choose_expr", right
->fn
))
430 const_expr
= get_argument_from_call_expr(right
->args
, 0);
431 expr1
= get_argument_from_call_expr(right
->args
, 1);
432 expr2
= get_argument_from_call_expr(right
->args
, 2);
434 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
437 fake
= assign_expression(expr
->left
, '=', sval
.value
? expr1
: expr2
);
442 int is_condition_call(struct expression
*expr
)
444 struct expression
*tmp
;
446 FOR_EACH_PTR_REVERSE(big_condition_stack
, tmp
) {
447 if (expr
== tmp
|| expr_get_parent_expr(expr
) == tmp
)
449 if (tmp
->pos
.line
< expr
->pos
.line
)
451 } END_FOR_EACH_PTR_REVERSE(tmp
);
456 static struct expression
*expr_get_parent_no_parens(struct expression
*expr
)
459 expr
= expr_get_parent_expr(expr
);
461 expr
->type
== EXPR_PREOP
&&
467 static bool gen_fake_function_assign(struct expression
*expr
)
469 static struct expression
*parsed
;
470 struct expression
*assign
, *parent
;
474 /* The rule is that every non-void function call has to be part of an
475 * assignment. TODO: Should we create a fake non-casted assignment
476 * for casted assignments? Also faked assigns for += assignments?
478 type
= get_type(expr
);
479 if (!type
|| type
== &void_ctype
)
482 parent
= expr_get_parent_no_parens(expr
);
483 if (parent
&& parent
->type
== EXPR_ASSIGNMENT
)
486 parent
= expr_get_fake_parent_expr(expr
);
488 struct expression
*left
= parent
->left
;
490 if (parent
== parsed
)
492 if (!left
|| left
->type
!= EXPR_SYMBOL
)
494 if (strncmp(left
->symbol_name
->name
, "__fake_assign_", 14) != 0)
497 __split_expr(parent
);
501 // TODO: faked_assign skipping conditions is a hack
502 if (is_condition_call(expr
))
505 snprintf(buf
, sizeof(buf
), "__fake_assign_%p", expr
);
506 assign
= create_fake_assign(buf
, get_type(expr
), expr
);
509 __split_expr(assign
);
513 static void split_call(struct expression
*expr
)
515 if (gen_fake_function_assign(expr
))
518 expr_set_parent_expr(expr
->fn
, expr
);
520 if (sym_name_is("__builtin_constant_p", expr
->fn
))
522 if (handle__builtin_choose_expr(expr
))
524 __split_expr(expr
->fn
);
526 if (is_inline_func(expr
->fn
))
527 add_inline_function(expr
->fn
->symbol
->definition
);
528 if (inlinable(expr
->fn
))
530 __process_post_op_stack();
531 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
532 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
534 if (inlinable(expr
->fn
))
536 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
537 if (is_noreturn_func(expr
->fn
))
539 if (!expr_get_parent_expr(expr
))
540 __discard_fake_states(expr
);
541 handle_builtin_overflow_func(expr
);
544 void parse_assignment(struct expression
*expr
)
546 struct expression
*right
;
548 expr_set_parent_expr(expr
->left
, expr
);
549 expr_set_parent_expr(expr
->right
, expr
);
551 right
= strip_expr(expr
->right
);
555 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
558 if (__handle_condition_assigns(expr
))
560 /* foo = (x < 5 ? foo : 5); */
561 if (__handle_select_assigns(expr
))
563 /* foo = ({frob(); frob(); frob(); 1;}) */
564 if (__handle_expr_statement_assigns(expr
))
565 return; // FIXME: got after
567 if (handle_comma_assigns(expr
))
569 if (handle__builtin_choose_expr_assigns(expr
))
571 if (handle_postop_assigns(expr
))
572 return; /* no need to goto after_assign */
574 __split_expr(expr
->right
);
575 if (outside_of_function())
576 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
578 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
581 // FIXME: the ordering of this is tricky
582 __fake_struct_member_assignments(expr
);
584 /* Re-examine ->right for inlines. See the commit message */
585 right
= strip_expr(expr
->right
);
586 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
587 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
590 if (get_macro_name(right
->pos
) &&
591 get_macro_name(expr
->pos
) != get_macro_name(right
->pos
))
592 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
594 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
595 __split_expr(expr
->left
);
598 void __split_expr(struct expression
*expr
)
604 // sm_msg("Debug expr_type %d %s expr = '%s'", expr->type, show_special(expr->op), expr_to_str(expr));
606 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
608 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
611 push_expression(&big_expression_stack
, expr
);
612 set_position(expr
->pos
);
613 __pass_to_client(expr
, EXPR_HOOK
);
615 switch (expr
->type
) {
617 expr_set_parent_expr(expr
->unop
, expr
);
619 if (expr
->op
== '*' &&
620 !prev_expression_is_getting_address(expr
))
621 __pass_to_client(expr
, DEREF_HOOK
);
622 __split_expr(expr
->unop
);
623 __pass_to_client(expr
, OP_HOOK
);
626 expr_set_parent_expr(expr
->unop
, expr
);
628 __split_expr(expr
->unop
);
629 push_expression(&post_op_stack
, expr
);
633 if (expr
->statement
&& !expr
->statement
) {
634 stmt_set_parent_stmt(expr
->statement
,
635 last_ptr_list((struct ptr_list
*)big_statement_stack
));
637 __split_stmt(expr
->statement
);
642 expr_set_parent_expr(expr
->left
, expr
);
643 expr_set_parent_expr(expr
->right
, expr
);
645 __pass_to_client(expr
, LOGIC_HOOK
);
646 __handle_logic(expr
);
649 expr_set_parent_expr(expr
->left
, expr
);
650 expr_set_parent_expr(expr
->right
, expr
);
652 __pass_to_client(expr
, BINOP_HOOK
);
653 __split_expr(expr
->left
);
654 __split_expr(expr
->right
);
657 expr_set_parent_expr(expr
->left
, expr
);
658 expr_set_parent_expr(expr
->right
, expr
);
660 __split_expr(expr
->left
);
661 __process_post_op_stack();
662 __split_expr(expr
->right
);
664 case EXPR_ASSIGNMENT
:
665 parse_assignment(expr
);
668 expr_set_parent_expr(expr
->deref
, expr
);
670 __pass_to_client(expr
, DEREF_HOOK
);
671 __split_expr(expr
->deref
);
674 expr_set_parent_expr(expr
->base
, expr
);
676 __split_expr(expr
->base
);
679 case EXPR_FORCE_CAST
:
680 expr_set_parent_expr(expr
->cast_expression
, expr
);
682 __pass_to_client(expr
, CAST_HOOK
);
683 __split_expr(expr
->cast_expression
);
686 if (expr
->cast_expression
)
687 __pass_to_client(strip_parens(expr
->cast_expression
),
693 case EXPR_CONDITIONAL
:
695 expr_set_parent_expr(expr
->conditional
, expr
);
696 expr_set_parent_expr(expr
->cond_true
, expr
);
697 expr_set_parent_expr(expr
->cond_false
, expr
);
699 if (known_condition_true(expr
->conditional
)) {
700 __split_expr(expr
->cond_true
);
703 if (known_condition_false(expr
->conditional
)) {
704 __split_expr(expr
->cond_false
);
707 __pass_to_client(expr
, SELECT_HOOK
);
708 __split_whole_condition(expr
->conditional
);
709 __split_expr(expr
->cond_true
);
710 __push_true_states();
711 __use_false_states();
712 __split_expr(expr
->cond_false
);
713 __merge_true_states();
718 case EXPR_INITIALIZER
:
719 split_expr_list(expr
->expr_list
, expr
);
721 case EXPR_IDENTIFIER
:
722 expr_set_parent_expr(expr
->ident_expression
, expr
);
723 __split_expr(expr
->ident_expression
);
726 expr_set_parent_expr(expr
->idx_expression
, expr
);
727 __split_expr(expr
->idx_expression
);
730 expr_set_parent_expr(expr
->init_expr
, expr
);
731 __split_expr(expr
->init_expr
);
734 __pass_to_client(expr
, SYM_HOOK
);
737 __pass_to_client(expr
, STRING_HOOK
);
740 struct expression
*tmp
;
742 tmp
= strip_Generic(expr
);
750 __pass_to_client(expr
, EXPR_HOOK_AFTER
);
751 pop_expression(&big_expression_stack
);
754 static int is_forever_loop(struct statement
*stmt
)
756 struct expression
*expr
;
759 expr
= strip_expr(stmt
->iterator_pre_condition
);
761 expr
= stmt
->iterator_post_condition
;
763 /* this is a for(;;) loop... */
767 if (get_value(expr
, &sval
) && sval
.value
!= 0)
774 static char *get_loop_name(int num
)
778 snprintf(buf
, 255, "-loop%d", num
);
780 return alloc_sname(buf
);
784 * Pre Loops are while and for loops.
786 static void handle_pre_loop(struct statement
*stmt
)
788 int once_through
; /* we go through the loop at least once */
789 struct sm_state
*extra_sm
= NULL
;
792 struct stree
*stree
= NULL
;
793 struct sm_state
*sm
= NULL
;
795 loop_name
= get_loop_name(loop_num
);
798 if (stmt
->iterator_pre_statement
) {
799 __split_stmt(stmt
->iterator_pre_statement
);
800 __prev_stmt
= stmt
->iterator_pre_statement
;
803 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
809 __merge_gotos(loop_name
, NULL
);
811 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
812 __in_pre_condition
++;
813 __pass_to_client(stmt
, PRELOOP_HOOK
);
814 __split_whole_condition(stmt
->iterator_pre_condition
);
815 __in_pre_condition
--;
816 FOR_EACH_SM(stree
, sm
) {
817 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
818 } END_FOR_EACH_SM(sm
);
821 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
823 if (option_assume_loops
)
826 __split_stmt(stmt
->iterator_statement
);
827 if (is_forever_loop(stmt
)) {
829 __save_gotos(loop_name
, NULL
);
831 __push_fake_cur_stree();
832 __split_stmt(stmt
->iterator_post_statement
);
833 stree
= __pop_fake_cur_stree();
835 __discard_false_states();
836 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
839 if (!__path_is_null())
840 __merge_stree_into_cur(stree
);
844 unchanged
= __iterator_unchanged(extra_sm
);
845 __split_stmt(stmt
->iterator_post_statement
);
846 __prev_stmt
= stmt
->iterator_post_statement
;
849 __save_gotos(loop_name
, NULL
);
850 __in_pre_condition
++;
851 __split_whole_condition(stmt
->iterator_pre_condition
);
852 __in_pre_condition
--;
854 __merge_false_states();
856 __discard_false_states();
858 __merge_false_states();
860 if (extra_sm
&& unchanged
)
861 __extra_pre_loop_hook_after(extra_sm
,
862 stmt
->iterator_post_statement
,
863 stmt
->iterator_pre_condition
);
864 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
871 * Post loops are do {} while();
873 static void handle_post_loop(struct statement
*stmt
)
877 loop_name
= get_loop_name(loop_num
);
881 __pass_to_client(stmt
, POSTLOOP_HOOK
);
885 __merge_gotos(loop_name
, NULL
);
886 __split_stmt(stmt
->iterator_statement
);
888 if (!expr_is_zero(stmt
->iterator_post_condition
))
889 __save_gotos(loop_name
, NULL
);
891 if (is_forever_loop(stmt
)) {
892 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
895 __split_whole_condition(stmt
->iterator_post_condition
);
896 __use_false_states();
897 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
903 static int empty_statement(struct statement
*stmt
)
907 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
912 static int last_stmt_on_same_line(void)
914 struct statement
*stmt
;
917 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
920 if (stmt
->pos
.line
== get_lineno())
923 } END_FOR_EACH_PTR_REVERSE(stmt
);
927 static void split_asm_ops(struct asm_operand_list
*ops
)
929 struct asm_operand
*op
;
931 FOR_EACH_PTR(ops
, op
) {
932 __split_expr(op
->expr
);
933 } END_FOR_EACH_PTR(op
);
936 static int is_case_val(struct statement
*stmt
, sval_t sval
)
940 if (stmt
->type
!= STMT_CASE
)
942 if (!stmt
->case_expression
) {
946 if (!get_value(stmt
->case_expression
, &case_sval
))
948 if (case_sval
.value
== sval
.value
)
953 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
954 struct expression
*case_expr
,
955 struct expression
*case_to
)
958 struct range_list
*rl
= NULL
;
959 struct symbol
*switch_type
;
961 switch_type
= get_type(switch_expr
);
962 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
963 start
= sval_cast(switch_type
, start
);
964 end
= sval_cast(switch_type
, end
);
965 add_range(&rl
, start
, end
);
966 } else if (get_value(case_expr
, &start
)) {
967 start
= sval_cast(switch_type
, start
);
968 add_range(&rl
, start
, start
);
974 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
976 struct statement
*tmp
;
977 struct range_list
*rl
;
979 __split_expr(stmt
->switch_expression
);
980 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
982 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
983 __save_switch_states(top_expression(switch_expr_stack
));
988 stmt
= stmt
->switch_statement
;
990 __push_scope_hooks();
991 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
992 __smatch_lineno
= tmp
->pos
.line
;
993 // FIXME: what if default comes before the known case statement?
994 if (is_case_val(tmp
, sval
)) {
995 rl
= alloc_rl(sval
, sval
);
996 __merge_switches(top_expression(switch_expr_stack
), rl
);
997 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
998 stmt_set_parent_stmt(tmp
->case_statement
, tmp
);
999 __split_stmt(tmp
->case_statement
);
1002 if (__path_is_null())
1006 if (__path_is_null()) {
1010 } END_FOR_EACH_PTR(tmp
);
1012 __call_scope_hooks();
1013 if (!__pop_default())
1014 __merge_switches(top_expression(switch_expr_stack
), NULL
);
1015 __discard_switches();
1017 pop_expression(&switch_expr_stack
);
1020 static void split_case(struct statement
*stmt
)
1022 struct range_list
*rl
= NULL
;
1024 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
1025 expr_set_parent_stmt(stmt
->case_to
, stmt
);
1027 rl
= get_case_rl(top_expression(switch_expr_stack
),
1028 stmt
->case_expression
, stmt
->case_to
);
1029 while (stmt
->case_statement
->type
== STMT_CASE
) {
1030 struct range_list
*tmp
;
1032 tmp
= get_case_rl(top_expression(switch_expr_stack
),
1033 stmt
->case_statement
->case_expression
,
1034 stmt
->case_statement
->case_to
);
1037 rl
= rl_union(rl
, tmp
);
1038 if (!stmt
->case_expression
)
1041 stmt
= stmt
->case_statement
;
1044 __merge_switches(top_expression(switch_expr_stack
), rl
);
1046 if (!stmt
->case_expression
)
1049 stmt_set_parent_stmt(stmt
->case_statement
, stmt
);
1050 __split_stmt(stmt
->case_statement
);
1053 int time_parsing_function(void)
1055 return ms_since(&fn_start_time
) / 1000;
1058 bool taking_too_long(void)
1060 if ((ms_since(&outer_fn_start_time
) / 1000) > 60 * 5) /* five minutes */
1065 struct statement
*get_last_stmt(void)
1068 struct statement
*stmt
;
1070 fn
= get_base_type(cur_func_sym
);
1075 stmt
= fn
->inline_stmt
;
1076 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
1078 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
1079 if (stmt
&& stmt
->type
== STMT_LABEL
)
1080 stmt
= stmt
->label_statement
;
1084 int is_last_stmt(struct statement
*cur_stmt
)
1086 struct statement
*last
;
1088 last
= get_last_stmt();
1089 if (last
&& last
== cur_stmt
)
1094 static void handle_backward_goto(struct statement
*goto_stmt
)
1096 const char *goto_name
, *label_name
;
1097 struct statement
*func_stmt
;
1098 struct symbol
*base_type
= get_base_type(cur_func_sym
);
1099 struct statement
*tmp
;
1104 if (last_goto_statement_handled
)
1106 last_goto_statement_handled
= 1;
1108 if (!goto_stmt
->goto_label
||
1109 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
1110 !goto_stmt
->goto_label
->ident
)
1112 goto_name
= goto_stmt
->goto_label
->ident
->name
;
1114 func_stmt
= base_type
->stmt
;
1116 func_stmt
= base_type
->inline_stmt
;
1119 if (func_stmt
->type
!= STMT_COMPOUND
)
1122 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
1124 if (tmp
->type
!= STMT_LABEL
)
1126 if (!tmp
->label_identifier
||
1127 tmp
->label_identifier
->type
!= SYM_LABEL
||
1128 !tmp
->label_identifier
->ident
)
1130 label_name
= tmp
->label_identifier
->ident
->name
;
1131 if (strcmp(goto_name
, label_name
) != 0)
1136 } END_FOR_EACH_PTR(tmp
);
1139 static void fake_a_return(void)
1141 struct expression
*ret
= NULL
;
1146 if (cur_func_return_type() != &void_ctype
)
1147 ret
= unknown_value_expression(NULL
);
1149 __pass_to_client(ret
, RETURN_HOOK
);
1153 static void split_ret_value(struct expression
*expr
)
1155 struct symbol
*type
;
1160 type
= get_real_base_type(cur_func_sym
);
1161 type
= get_real_base_type(type
);
1162 expr
= fake_a_variable_assign(type
, NULL
, expr
, -1);
1164 __in_fake_var_assign
++;
1166 __in_fake_var_assign
--;
1169 static void fake_an_empty_default(struct position pos
)
1171 static struct statement none
= {};
1174 none
.type
= STMT_NONE
;
1175 __merge_switches(top_expression(switch_expr_stack
), NULL
);
1176 __split_stmt(&none
);
1179 static void split_compound(struct statement
*stmt
)
1181 struct statement
*prev
= NULL
;
1182 struct statement
*cur
= NULL
;
1183 struct statement
*next
;
1185 __push_scope_hooks();
1187 FOR_EACH_PTR(stmt
->stmts
, next
) {
1188 /* just set them all ahead of time */
1189 stmt_set_parent_stmt(next
, stmt
);
1199 } END_FOR_EACH_PTR(next
);
1208 * For function scope, then delay calling the scope hooks until the
1209 * end of function hooks can run. I'm not positive this is the right
1212 if (!is_last_stmt(cur
))
1213 __call_scope_hooks();
1217 * This is a hack, work around for detecting empty functions.
1219 static int need_delayed_scope_hooks(void)
1221 struct symbol
*fn
= get_base_type(cur_func_sym
);
1222 struct statement
*stmt
;
1228 stmt
= fn
->inline_stmt
;
1229 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
1234 void __split_label_stmt(struct statement
*stmt
)
1236 if (stmt
->label_identifier
&&
1237 stmt
->label_identifier
->type
== SYM_LABEL
&&
1238 stmt
->label_identifier
->ident
) {
1239 loop_count
|= 0x0800000;
1240 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
1244 static void find_asm_gotos(struct statement
*stmt
)
1248 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
1249 __save_gotos(sym
->ident
->name
, sym
);
1250 } END_FOR_EACH_PTR(sym
);
1253 void __split_stmt(struct statement
*stmt
)
1255 static int indent_cnt
;
1257 struct timeval start
, stop
;
1258 bool skip_after
= false;
1260 gettimeofday(&start
, NULL
);
1265 if (!__in_fake_assign
)
1266 __silence_warnings_for_stmt
= false;
1268 if (__bail_on_rest_of_function
|| is_skipped_function())
1271 if (out_of_memory() || taking_too_long()) {
1272 gettimeofday(&start
, NULL
);
1274 __bail_on_rest_of_function
= 1;
1276 sm_perror("Function too hairy. Giving up. %lu seconds",
1277 start
.tv_sec
- fn_start_time
.tv_sec
);
1279 final_pass
= 0; /* turn off sm_msg() from here */
1285 add_ptr_list(&big_statement_stack
, stmt
);
1286 free_expression_stack(&big_expression_stack
);
1287 set_position(stmt
->pos
);
1288 __pass_to_client(stmt
, STMT_HOOK
);
1290 switch (stmt
->type
) {
1291 case STMT_DECLARATION
:
1292 split_declaration(stmt
->declaration
);
1295 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1297 split_ret_value(stmt
->ret_value
);
1298 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1299 __process_post_op_stack();
1302 case STMT_EXPRESSION
:
1303 expr_set_parent_stmt(stmt
->expression
, stmt
);
1304 expr_set_parent_stmt(stmt
->context
, stmt
);
1306 __split_expr(stmt
->expression
);
1309 split_compound(stmt
);
1312 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1313 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1314 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1316 if (known_condition_true(stmt
->if_conditional
)) {
1317 __split_stmt(stmt
->if_true
);
1320 if (known_condition_false(stmt
->if_conditional
)) {
1321 __split_stmt(stmt
->if_false
);
1324 __split_whole_condition(stmt
->if_conditional
);
1325 __split_stmt(stmt
->if_true
);
1326 if (empty_statement(stmt
->if_true
) &&
1327 last_stmt_on_same_line() &&
1328 !get_macro_name(stmt
->if_true
->pos
))
1329 sm_warning("if();");
1330 __push_true_states();
1331 __use_false_states();
1332 __split_stmt(stmt
->if_false
);
1333 __merge_true_states();
1336 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1337 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1338 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1339 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1340 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1342 if (stmt
->iterator_pre_condition
)
1343 handle_pre_loop(stmt
);
1344 else if (stmt
->iterator_post_condition
)
1345 handle_post_loop(stmt
);
1347 // these are for(;;) type loops.
1348 handle_pre_loop(stmt
);
1352 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1353 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1355 if (get_value(stmt
->switch_expression
, &sval
)) {
1356 split_known_switch(stmt
, sval
);
1359 __split_expr(stmt
->switch_expression
);
1360 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1361 __save_switch_states(top_expression(switch_expr_stack
));
1365 __split_stmt(stmt
->switch_statement
);
1366 if (!__pop_default() && have_remaining_cases())
1367 fake_an_empty_default(stmt
->pos
);
1368 __discard_switches();
1370 pop_expression(&switch_expr_stack
);
1376 __split_label_stmt(stmt
);
1377 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1379 __split_stmt(stmt
->label_statement
);
1382 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1384 __split_expr(stmt
->goto_expression
);
1385 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1386 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1388 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1390 __process_continues();
1392 } else if (stmt
->goto_label
&&
1393 stmt
->goto_label
->type
== SYM_LABEL
&&
1394 stmt
->goto_label
->ident
) {
1395 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1398 if (is_last_stmt(stmt
))
1399 handle_backward_goto(stmt
);
1404 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1406 find_asm_gotos(stmt
);
1407 __pass_to_client(stmt
, ASM_HOOK
);
1408 __split_expr(stmt
->asm_string
);
1409 split_asm_ops(stmt
->asm_outputs
);
1410 split_asm_ops(stmt
->asm_inputs
);
1411 split_expr_list(stmt
->asm_clobbers
, NULL
);
1416 __split_expr(stmt
->range_expression
);
1417 __split_expr(stmt
->range_low
);
1418 __split_expr(stmt
->range_high
);
1422 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1423 if (--indent_cnt
== 1)
1424 __discard_fake_states(NULL
);
1427 __process_post_op_stack();
1429 gettimeofday(&stop
, NULL
);
1430 if (option_time_stmt
&& stmt
)
1431 sm_msg("stmt_time%s: %ld",
1432 stmt
->type
== STMT_COMPOUND
? "_block" : "",
1433 stop
.tv_sec
- start
.tv_sec
);
1436 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1438 struct expression
*expr
;
1440 FOR_EACH_PTR(expr_list
, expr
) {
1441 expr_set_parent_expr(expr
, parent
);
1443 __process_post_op_stack();
1444 } END_FOR_EACH_PTR(expr
);
1447 static bool cast_arg(struct symbol
*type
, struct expression
*arg
)
1449 struct symbol
*orig
;
1454 arg
= strip_parens(arg
);
1455 if (arg
!= strip_expr(arg
))
1458 orig
= get_type(arg
);
1461 if (types_equiv(orig
, type
))
1464 if (orig
->type
== SYM_ARRAY
&& type
->type
== SYM_PTR
)
1468 * I would have expected that we could just do use (orig == type) but I
1469 * guess for pointers we need to get the basetype to do that comparison.
1473 if (orig
->type
!= SYM_PTR
||
1474 type
->type
!= SYM_PTR
) {
1475 if (type_fits(type
, orig
))
1479 orig
= get_real_base_type(orig
);
1480 type
= get_real_base_type(type
);
1487 static struct expression
*fake_a_variable_assign(struct symbol
*type
, struct expression
*call
, struct expression
*expr
, int nr
)
1492 if (!expr
|| !cur_func_sym
)
1495 if (expr
->type
== EXPR_ASSIGNMENT
)
1498 /* for va_args then we don't know the type */
1500 type
= get_type(expr
);
1502 cast
= cast_arg(type
, expr
);
1504 * Using expr_to_sym() here is a hack. We want to say that we don't
1505 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1506 * It turns out faking these assignments is way more expensive than I
1507 * would have imagined. I'm not sure why exactly.
1512 * if the code is "return *p;" where "p" is a user pointer then
1513 * we want to create a fake assignment so that it sets the state
1514 * in check_kernel_user_data.c.
1517 if (expr
->type
!= EXPR_PREOP
&&
1518 expr
->op
!= '*' && expr
->op
!= '&' &&
1524 snprintf(buf
, sizeof(buf
), "__fake_return_%p", expr
);
1526 snprintf(buf
, sizeof(buf
), "__fake_param_%p_%d", call
, nr
);
1528 return create_fake_assign(buf
, type
, expr
);
1531 static void split_args(struct expression
*expr
)
1533 struct expression
*arg
, *tmp
;
1534 struct symbol
*type
;
1538 FOR_EACH_PTR(expr
->args
, arg
) {
1540 expr_set_parent_expr(arg
, expr
);
1541 type
= get_arg_type(expr
->fn
, i
);
1542 tmp
= fake_a_variable_assign(type
, expr
, arg
, i
);
1544 __in_fake_var_assign
++;
1547 __in_fake_var_assign
--;
1548 __process_post_op_stack();
1549 } END_FOR_EACH_PTR(arg
);
1552 static void split_sym(struct symbol
*sym
)
1556 if (!(sym
->namespace & NS_SYMBOL
))
1559 __split_stmt(sym
->stmt
);
1560 __split_expr(sym
->array_size
);
1561 split_symlist(sym
->arguments
);
1562 split_symlist(sym
->symbol_list
);
1563 __split_stmt(sym
->inline_stmt
);
1564 split_symlist(sym
->inline_symbol_list
);
1567 static void split_symlist(struct symbol_list
*sym_list
)
1571 FOR_EACH_PTR(sym_list
, sym
) {
1573 } END_FOR_EACH_PTR(sym
);
1576 typedef void (fake_cb
)(struct expression
*expr
);
1578 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1580 struct symbol
*type
, *tmp
;
1586 name
= member
->name
;
1588 type
= get_type(expr
);
1589 if (!type
|| type
->type
!= SYM_STRUCT
)
1593 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1597 if (strcmp(name
, tmp
->ident
->name
) == 0)
1599 } END_FOR_EACH_PTR(tmp
);
1603 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1605 struct symbol
*type
, *member
;
1608 type
= get_type(expr
);
1609 if (!type
|| type
->type
!= SYM_STRUCT
)
1612 FOR_EACH_PTR(type
->symbol_list
, member
) {
1614 return member
->ident
;
1616 } END_FOR_EACH_PTR(member
);
1620 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1622 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1624 struct expression
*edge_member
, *assign
;
1625 struct symbol
*base
= get_real_base_type(member
);
1629 expr
= member_expression(expr
, '.', member
->ident
);
1631 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1632 struct symbol
*type
;
1634 type
= get_real_base_type(tmp
);
1638 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1639 if (get_extra_state(edge_member
))
1642 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1643 set_inner_struct_members(expr
, tmp
);
1650 assign
= assign_expression(edge_member
, '=', zero_expr());
1651 __split_expr(assign
);
1652 } END_FOR_EACH_PTR(tmp
);
1657 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1660 struct expression
*member
= NULL
;
1661 struct expression
*assign
;
1663 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1664 type
= get_real_base_type(tmp
);
1669 member
= member_expression(expr
, '.', tmp
->ident
);
1670 if (get_extra_state(member
))
1674 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1675 set_inner_struct_members(expr
, tmp
);
1678 if (type
->type
== SYM_ARRAY
)
1683 assign
= assign_expression(member
, '=', zero_expr());
1684 __split_expr(assign
);
1685 } END_FOR_EACH_PTR(tmp
);
1688 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1690 struct expression
*deref
, *assign
, *tmp
, *right
;
1691 struct symbol
*struct_type
, *type
;
1692 struct ident
*member
;
1695 struct_type
= get_type(symbol
);
1697 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1701 * We're parsing an initializer that could look something like this:
1702 * struct foo foo = {
1704 * .whatever.xxx = 11,
1708 * So what we have here is a list with 42, .whatever, and .zzz. We need
1709 * to break it up into left and right sides of the assignments.
1713 FOR_EACH_PTR(members
, tmp
) {
1715 if (tmp
->type
== EXPR_IDENTIFIER
) {
1716 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1717 while (tmp
->type
== EXPR_IDENTIFIER
) {
1718 member
= tmp
->expr_ident
;
1719 tmp
= tmp
->ident_expression
;
1721 deref
= member_expression(deref
, '.', member
);
1723 deref
= member_expression(symbol
, '.', member
);
1726 member
= number_to_member(symbol
, member_idx
);
1727 deref
= member_expression(symbol
, '.', member
);
1731 if (right
->type
== EXPR_INITIALIZER
) {
1732 type
= get_type(deref
);
1733 if (type
&& type
->type
== SYM_ARRAY
)
1734 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1736 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1738 assign
= assign_expression(deref
, '=', right
);
1741 } END_FOR_EACH_PTR(tmp
);
1743 set_unset_to_zero(struct_type
, symbol
);
1746 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1748 fake_member_assigns_helper(symbol_expression(sym
),
1749 sym
->initializer
->expr_list
, fake_cb
);
1752 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1754 struct expression
*offset
, *binop
, *assign
, *tmp
;
1755 struct symbol
*type
;
1758 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1763 FOR_EACH_PTR(expr_list
, tmp
) {
1764 if (tmp
->type
== EXPR_INDEX
) {
1765 if (tmp
->idx_from
!= tmp
->idx_to
)
1767 idx
= tmp
->idx_from
;
1770 if (!tmp
->idx_expression
)
1772 tmp
= tmp
->idx_expression
;
1774 offset
= value_expr(idx
);
1775 binop
= array_element_expression(array
, offset
);
1776 if (tmp
->type
== EXPR_INITIALIZER
) {
1777 type
= get_type(binop
);
1778 if (type
&& type
->type
== SYM_ARRAY
)
1779 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1781 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1783 assign
= assign_expression(binop
, '=', tmp
);
1790 } END_FOR_EACH_PTR(tmp
);
1792 __call_array_initialized_hooks(array
, max
);
1795 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1797 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1800 static void fake_assign_expr(struct symbol
*sym
)
1802 struct expression
*assign
, *symbol
;
1804 symbol
= symbol_expression(sym
);
1805 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1806 __split_expr(assign
);
1809 static void do_initializer_stuff(struct symbol
*sym
)
1811 if (!sym
->initializer
)
1814 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1815 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1816 fake_element_assigns(sym
, __split_expr
);
1818 fake_member_assigns(sym
, __split_expr
);
1820 fake_assign_expr(sym
);
1824 static void split_declaration(struct symbol_list
*sym_list
)
1828 FOR_EACH_PTR(sym_list
, sym
) {
1829 __pass_to_client(sym
, DECLARATION_HOOK
);
1830 do_initializer_stuff(sym
);
1831 __pass_to_client(sym
, DECLARATION_HOOK_AFTER
);
1833 } END_FOR_EACH_PTR(sym
);
1836 static void call_global_assign_hooks(struct expression
*assign
)
1838 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1841 static void fake_global_assign(struct symbol
*sym
)
1843 struct expression
*assign
, *symbol
;
1845 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1846 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1847 fake_element_assigns(sym
, call_global_assign_hooks
);
1848 } else if (sym
->initializer
) {
1849 symbol
= symbol_expression(sym
);
1850 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1851 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1853 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1855 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1856 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1857 fake_member_assigns(sym
, call_global_assign_hooks
);
1858 } else if (sym
->initializer
) {
1859 symbol
= symbol_expression(sym
);
1860 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1861 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1863 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1866 symbol
= symbol_expression(sym
);
1867 if (sym
->initializer
) {
1868 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1869 __split_expr(assign
);
1871 assign
= assign_expression(symbol
, '=', zero_expr());
1873 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1877 static void start_function_definition(struct symbol
*sym
)
1879 __in_function_def
= 1;
1880 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1881 __in_function_def
= 0;
1882 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1886 void add_function_data(unsigned long *fn_data
)
1888 __add_ptr_list(&fn_data_list
, fn_data
);
1891 static void clear_function_data(void)
1895 FOR_EACH_PTR(fn_data_list
, tmp
) {
1897 } END_FOR_EACH_PTR(tmp
);
1900 static void record_func_time(void)
1902 struct timeval stop
;
1906 gettimeofday(&stop
, NULL
);
1907 func_time
= stop
.tv_sec
- fn_start_time
.tv_sec
;
1908 snprintf(buf
, sizeof(buf
), "%d", func_time
);
1909 sql_insert_return_implies(FUNC_TIME
, 0, "", buf
);
1910 if (option_time
&& func_time
> 2) {
1912 sm_msg("func_time: %d", func_time
);
1917 static void split_function(struct symbol
*sym
)
1919 struct symbol
*base_type
= get_base_type(sym
);
1921 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1924 gettimeofday(&outer_fn_start_time
, NULL
);
1925 gettimeofday(&fn_start_time
, NULL
);
1928 cur_func
= sym
->ident
->name
;
1929 set_position(sym
->pos
);
1930 clear_function_data();
1932 last_goto_statement_handled
= 0;
1933 sm_debug("new function: %s\n", cur_func
);
1935 if (option_two_passes
) {
1939 start_function_definition(sym
);
1940 __split_stmt(base_type
->stmt
);
1941 __split_stmt(base_type
->inline_stmt
);
1947 start_function_definition(sym
);
1948 __split_stmt(base_type
->stmt
);
1949 __split_stmt(base_type
->inline_stmt
);
1950 if (!__path_is_null() &&
1951 cur_func_return_type() == &void_ctype
&&
1952 !__bail_on_rest_of_function
) {
1953 __pass_to_client(NULL
, RETURN_HOOK
);
1956 __pass_to_client(sym
, END_FUNC_HOOK
);
1957 if (need_delayed_scope_hooks())
1958 __call_scope_hooks();
1959 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1966 cur_func_sym
= NULL
;
1968 free_data_info_allocs();
1969 free_expression_stack(&switch_expr_stack
);
1970 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1971 __bail_on_rest_of_function
= 0;
1974 static void save_flow_state(void)
1978 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2));
1979 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2));
1980 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2));
1982 __add_ptr_list(&backup
, big_statement_stack
);
1983 __add_ptr_list(&backup
, big_expression_stack
);
1984 __add_ptr_list(&backup
, big_condition_stack
);
1985 __add_ptr_list(&backup
, switch_expr_stack
);
1987 __add_ptr_list(&backup
, cur_func_sym
);
1989 __add_ptr_list(&backup
, __prev_stmt
);
1990 __add_ptr_list(&backup
, __cur_stmt
);
1991 __add_ptr_list(&backup
, __next_stmt
);
1993 FOR_EACH_PTR(fn_data_list
, tmp
) {
1994 __add_ptr_list(&backup
, (void *)*tmp
);
1995 } END_FOR_EACH_PTR(tmp
);
1998 static void *pop_backup(void)
2002 ret
= last_ptr_list(backup
);
2003 delete_ptr_list_last(&backup
);
2007 static void restore_flow_state(void)
2011 FOR_EACH_PTR_REVERSE(fn_data_list
, tmp
) {
2012 *tmp
= (unsigned long)pop_backup();
2013 } END_FOR_EACH_PTR_REVERSE(tmp
);
2015 __next_stmt
= pop_backup();
2016 __cur_stmt
= pop_backup();
2017 __prev_stmt
= pop_backup();
2019 cur_func_sym
= pop_backup();
2020 switch_expr_stack
= pop_backup();
2021 big_condition_stack
= pop_backup();
2022 big_expression_stack
= pop_backup();
2023 big_statement_stack
= pop_backup();
2024 final_pass
= PTR_INT(pop_backup()) >> 2;
2025 loop_count
= PTR_INT(pop_backup()) >> 2;
2026 loop_num
= PTR_INT(pop_backup()) >> 2;
2029 static void parse_inline(struct expression
*call
)
2031 struct symbol
*base_type
;
2032 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
2033 struct timeval time_backup
= fn_start_time
;
2034 struct expression
*orig_inline
= __inline_fn
;
2037 if (out_of_memory() || taking_too_long())
2042 __pass_to_client(call
, INLINE_FN_START
);
2043 final_pass
= 0; /* don't print anything */
2045 orig_budget
= inline_budget
;
2046 inline_budget
= inline_budget
- 5;
2048 base_type
= get_base_type(call
->fn
->symbol
);
2049 cur_func_sym
= call
->fn
->symbol
;
2050 if (call
->fn
->symbol
->ident
)
2051 cur_func
= call
->fn
->symbol
->ident
->name
;
2054 set_position(call
->fn
->symbol
->pos
);
2057 big_statement_stack
= NULL
;
2058 big_expression_stack
= NULL
;
2059 big_condition_stack
= NULL
;
2060 switch_expr_stack
= NULL
;
2062 sm_debug("inline function: %s\n", cur_func
);
2064 clear_function_data();
2067 start_function_definition(call
->fn
->symbol
);
2068 __split_stmt(base_type
->stmt
);
2069 __split_stmt(base_type
->inline_stmt
);
2070 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
2071 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
2072 call
->fn
->symbol
->parsed
= true;
2074 free_expression_stack(&switch_expr_stack
);
2075 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
2079 restore_flow_state();
2080 fn_start_time
= time_backup
;
2081 cur_func
= cur_func_bak
;
2083 restore_all_states();
2084 set_position(call
->pos
);
2085 __inline_fn
= orig_inline
;
2086 inline_budget
= orig_budget
;
2087 __pass_to_client(call
, INLINE_FN_END
);
2090 static struct symbol_list
*inlines_called
;
2091 static void add_inline_function(struct symbol
*sym
)
2093 static struct symbol_list
*already_added
;
2096 FOR_EACH_PTR(already_added
, tmp
) {
2099 } END_FOR_EACH_PTR(tmp
);
2101 add_ptr_list(&already_added
, sym
);
2102 add_ptr_list(&inlines_called
, sym
);
2105 static void process_inlines(void)
2109 FOR_EACH_PTR(inlines_called
, tmp
) {
2110 split_function(tmp
);
2111 } END_FOR_EACH_PTR(tmp
);
2112 free_ptr_list(&inlines_called
);
2115 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
2119 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
2122 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
2124 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
2126 } END_FOR_EACH_PTR_REVERSE(sym
);
2131 static bool interesting_function(struct symbol
*sym
)
2133 static int prev_stream
= -1;
2134 static bool prev_answer
;
2135 const char *filename
;
2138 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
2141 if (sym
->pos
.stream
== prev_stream
)
2144 prev_stream
= sym
->pos
.stream
;
2145 prev_answer
= false;
2147 filename
= stream_name(sym
->pos
.stream
);
2148 len
= strlen(filename
);
2149 if (len
> 0 && filename
[len
- 1] == 'c')
2154 static void split_inlines_in_scope(struct symbol
*sym
)
2156 struct symbol
*base
;
2157 struct symbol_list
*scope_list
;
2160 scope_list
= sym
->scope
->symbols
;
2161 stream
= sym
->pos
.stream
;
2163 /* find the last static symbol in the file */
2164 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
2165 if (sym
->pos
.stream
!= stream
)
2167 if (sym
->type
!= SYM_NODE
)
2169 base
= get_base_type(sym
);
2172 if (base
->type
!= SYM_FN
)
2174 if (!base
->inline_stmt
)
2176 if (!interesting_function(sym
))
2178 add_inline_function(sym
);
2179 } END_FOR_EACH_PTR_REVERSE(sym
);
2184 static void split_inlines(struct symbol_list
*sym_list
)
2188 sym
= get_last_scoped_symbol(sym_list
, 0);
2190 split_inlines_in_scope(sym
);
2191 sym
= get_last_scoped_symbol(sym_list
, 1);
2193 split_inlines_in_scope(sym
);
2196 static struct stree
*clone_estates_perm(struct stree
*orig
)
2198 struct stree
*ret
= NULL
;
2199 struct sm_state
*tmp
;
2201 FOR_EACH_SM(orig
, tmp
) {
2202 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
2203 } END_FOR_EACH_SM(tmp
);
2208 struct position last_pos
;
2209 static void split_c_file_functions(struct symbol_list
*sym_list
)
2214 FOR_EACH_PTR(sym_list
, sym
) {
2215 set_position(sym
->pos
);
2216 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
2217 __pass_to_client(sym
, BASE_HOOK
);
2218 fake_global_assign(sym
);
2219 __pass_to_client(sym
, DECLARATION_HOOK_AFTER
);
2221 } END_FOR_EACH_PTR(sym
);
2222 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
2225 FOR_EACH_PTR(sym_list
, sym
) {
2226 set_position(sym
->pos
);
2227 last_pos
= sym
->pos
;
2228 if (!interesting_function(sym
))
2230 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
2231 split_function(sym
);
2234 last_pos
= sym
->pos
;
2235 } END_FOR_EACH_PTR(sym
);
2236 split_inlines(sym_list
);
2237 __pass_to_client(sym_list
, END_FILE_HOOK
);
2240 static int final_before_fake
;
2241 void init_fake_env(void)
2244 final_before_fake
= final_pass
;
2246 __push_fake_cur_stree();
2250 void end_fake_env(void)
2252 __free_fake_cur_stree();
2255 final_pass
= final_before_fake
;
2258 static void open_output_files(char *base_file
)
2262 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
2263 sm_outfd
= fopen(buf
, "w");
2265 sm_fatal("Cannot open %s", buf
);
2270 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
2271 sql_outfd
= fopen(buf
, "w");
2273 sm_fatal("Error: Cannot open %s", buf
);
2275 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
2276 caller_info_fd
= fopen(buf
, "w");
2277 if (!caller_info_fd
)
2278 sm_fatal("Error: Cannot open %s", buf
);
2281 void smatch(struct string_list
*filelist
)
2283 struct symbol_list
*sym_list
;
2284 struct timeval stop
, start
;
2288 gettimeofday(&start
, NULL
);
2290 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
2291 path
= getcwd(NULL
, 0);
2292 free(full_base_file
);
2294 len
= strlen(path
) + 1 + strlen(base_file
) + 1;
2295 full_base_file
= malloc(len
);
2296 snprintf(full_base_file
, len
, "%s/%s", path
, base_file
);
2298 full_base_file
= alloc_string(base_file
);
2300 if (option_file_output
)
2301 open_output_files(base_file
);
2302 base_file_stream
= input_stream_nr
;
2303 sym_list
= sparse_keep_tokens(base_file
);
2304 split_c_file_functions(sym_list
);
2305 } END_FOR_EACH_PTR_NOTAG(base_file
);
2307 gettimeofday(&stop
, NULL
);
2309 set_position(last_pos
);
2312 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
2314 sm_msg("mem: %luKb", get_max_memory());