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 static void set_position(struct position pos
)
134 static int prev_stream
= -1;
139 if (pos
.stream
== 0 && pos
.line
== 0)
142 __smatch_lineno
= pos
.line
;
144 if (pos
.stream
== prev_stream
)
147 filename
= stream_name(pos
.stream
);
150 pathname
= getcwd(NULL
, 0);
152 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
153 full_filename
= malloc(len
);
154 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
156 full_filename
= alloc_string(filename
);
161 int is_assigned_call(struct expression
*expr
)
163 struct expression
*parent
= expr_get_parent_expr(expr
);
166 parent
->type
== EXPR_ASSIGNMENT
&&
168 strip_expr(parent
->right
) == expr
)
174 int is_fake_assigned_call(struct expression
*expr
)
176 struct expression
*parent
= expr_get_fake_parent_expr(expr
);
179 parent
->type
== EXPR_ASSIGNMENT
&&
181 strip_expr(parent
->right
) == expr
)
187 static bool is_inline_func(struct expression
*expr
)
189 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
191 if (!expr
->symbol
->definition
)
193 if (expr
->symbol
->definition
->ctype
.modifiers
& MOD_INLINE
)
199 static int is_noreturn_func(struct expression
*expr
)
201 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
205 * It's almost impossible for Smatch to handle __builtin_constant_p()
206 * the same way that GCC does so Smatch ends up making some functions
207 * as no return functions incorrectly.
210 if (option_project
== PROJ_KERNEL
&& expr
->symbol
->ident
&&
211 strstr(expr
->symbol
->ident
->name
, "__compiletime_assert"))
214 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
219 static int save_func_time(void *_rl
, int argc
, char **argv
, char **azColName
)
221 unsigned long *rl
= _rl
;
223 *rl
= strtoul(argv
[0], NULL
, 10);
227 static int get_func_time(struct symbol
*sym
)
229 unsigned long time
= 0;
231 run_sql(&save_func_time
, &time
,
232 "select key from return_implies where %s and type = %d;",
233 get_static_filter(sym
), FUNC_TIME
);
238 static int inline_budget
= 20;
240 int inlinable(struct expression
*expr
)
243 struct statement
*last_stmt
= NULL
;
245 if (__inline_fn
) /* don't nest */
248 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
250 if (is_no_inline_function(expr
->symbol
->ident
->name
))
252 sym
= get_base_type(expr
->symbol
);
253 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
254 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
256 if (sym
->stmt
->type
!= STMT_COMPOUND
)
258 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
260 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
261 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
263 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
265 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
271 /* the magic numbers in this function are pulled out of my bum. */
272 if (last_stmt
->pos
.line
> sym
->pos
.line
+ inline_budget
)
275 if (get_func_time(expr
->symbol
) >= 2)
281 void __process_post_op_stack(void)
283 struct expression
*expr
;
285 FOR_EACH_PTR(post_op_stack
, expr
) {
286 __pass_to_client(expr
, OP_HOOK
);
287 } END_FOR_EACH_PTR(expr
);
289 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
292 static int handle_comma_assigns(struct expression
*expr
)
294 struct expression
*right
;
295 struct expression
*assign
;
297 right
= strip_expr(expr
->right
);
298 if (right
->type
!= EXPR_COMMA
)
301 __split_expr(right
->left
);
302 __process_post_op_stack();
304 assign
= assign_expression(expr
->left
, '=', right
->right
);
305 __split_expr(assign
);
310 /* This is to handle *p++ = foo; assignments */
311 static int handle_postop_assigns(struct expression
*expr
)
313 struct expression
*left
, *fake_left
;
314 struct expression
*assign
;
316 left
= strip_expr(expr
->left
);
317 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '*')
319 left
= strip_expr(left
->unop
);
320 if (left
->type
!= EXPR_POSTOP
)
323 fake_left
= deref_expression(strip_expr(left
->unop
));
324 assign
= assign_expression(fake_left
, '=', expr
->right
);
326 __split_expr(assign
);
327 __split_expr(expr
->left
);
332 static int prev_expression_is_getting_address(struct expression
*expr
)
334 struct expression
*parent
;
337 parent
= expr_get_parent_expr(expr
);
341 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
343 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
345 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
347 /* Handle &foo->array[offset] */
348 if (parent
->type
== EXPR_BINOP
&& parent
->op
== '+') {
349 parent
= expr_get_parent_expr(parent
);
352 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '*')
362 static void handle_builtin_overflow_func(struct expression
*expr
)
364 struct expression
*a
, *b
, *res
, *assign
;
367 if (sym_name_is("__builtin_add_overflow", expr
->fn
))
369 else if (sym_name_is("__builtin_sub_overflow", expr
->fn
))
371 else if (sym_name_is("__builtin_mul_overflow", expr
->fn
))
376 a
= get_argument_from_call_expr(expr
->args
, 0);
377 b
= get_argument_from_call_expr(expr
->args
, 1);
378 res
= get_argument_from_call_expr(expr
->args
, 2);
380 assign
= assign_expression(deref_expression(res
), '=', binop_expression(a
, op
, b
));
381 __split_expr(assign
);
384 static int handle__builtin_choose_expr(struct expression
*expr
)
386 struct expression
*const_expr
, *expr1
, *expr2
;
389 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
392 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
393 expr1
= get_argument_from_call_expr(expr
->args
, 1);
394 expr2
= get_argument_from_call_expr(expr
->args
, 2);
396 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
405 static int handle__builtin_choose_expr_assigns(struct expression
*expr
)
407 struct expression
*const_expr
, *right
, *expr1
, *expr2
, *fake
;
410 right
= strip_parens(expr
->right
);
411 if (right
->type
!= EXPR_CALL
)
413 if (!sym_name_is("__builtin_choose_expr", right
->fn
))
416 const_expr
= get_argument_from_call_expr(right
->args
, 0);
417 expr1
= get_argument_from_call_expr(right
->args
, 1);
418 expr2
= get_argument_from_call_expr(right
->args
, 2);
420 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
423 fake
= assign_expression(expr
->left
, '=', sval
.value
? expr1
: expr2
);
428 int is_condition_call(struct expression
*expr
)
430 struct expression
*tmp
;
432 FOR_EACH_PTR_REVERSE(big_condition_stack
, tmp
) {
433 if (expr
== tmp
|| expr_get_parent_expr(expr
) == tmp
)
435 if (tmp
->pos
.line
< expr
->pos
.line
)
437 } END_FOR_EACH_PTR_REVERSE(tmp
);
442 static bool gen_fake_function_assign(struct expression
*expr
)
444 static struct expression
*parsed
;
445 struct expression
*assign
, *parent
;
449 /* The rule is that every non-void function call has to be part of an
450 * assignment. TODO: Should we create a fake non-casted assignment
451 * for casted assignments? Also faked assigns for += assignments?
453 type
= get_type(expr
);
454 if (!type
|| type
== &void_ctype
)
457 parent
= expr_get_parent_expr(expr
);
458 if (parent
&& parent
->type
== EXPR_ASSIGNMENT
)
461 parent
= expr_get_fake_parent_expr(expr
);
463 struct expression
*left
= parent
->left
;
465 if (parent
== parsed
)
467 if (!left
|| left
->type
!= EXPR_SYMBOL
)
469 if (strncmp(left
->symbol_name
->name
, "__fake_assign_", 14) != 0)
472 __split_expr(parent
);
476 // TODO: faked_assign skipping conditions is a hack
477 if (is_condition_call(expr
))
480 snprintf(buf
, sizeof(buf
), "__fake_assign_%p", expr
);
481 assign
= create_fake_assign(buf
, get_type(expr
), expr
);
484 __split_expr(assign
);
488 static void split_call(struct expression
*expr
)
490 if (gen_fake_function_assign(expr
))
493 expr_set_parent_expr(expr
->fn
, expr
);
495 if (sym_name_is("__builtin_constant_p", expr
->fn
))
497 if (handle__builtin_choose_expr(expr
))
499 __split_expr(expr
->fn
);
501 if (is_inline_func(expr
->fn
))
502 add_inline_function(expr
->fn
->symbol
->definition
);
503 if (inlinable(expr
->fn
))
505 __process_post_op_stack();
506 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
507 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
509 if (inlinable(expr
->fn
))
511 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
512 if (is_noreturn_func(expr
->fn
))
514 if (!expr_get_parent_expr(expr
))
515 __discard_fake_states(expr
);
516 handle_builtin_overflow_func(expr
);
519 void __split_expr(struct expression
*expr
)
525 // sm_msg("Debug expr_type %d %s expr = '%s'", expr->type, show_special(expr->op), expr_to_str(expr));
527 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
529 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
532 push_expression(&big_expression_stack
, expr
);
533 set_position(expr
->pos
);
534 __pass_to_client(expr
, EXPR_HOOK
);
536 switch (expr
->type
) {
538 expr_set_parent_expr(expr
->unop
, expr
);
540 if (expr
->op
== '*' &&
541 !prev_expression_is_getting_address(expr
))
542 __pass_to_client(expr
, DEREF_HOOK
);
543 __split_expr(expr
->unop
);
544 __pass_to_client(expr
, OP_HOOK
);
547 expr_set_parent_expr(expr
->unop
, expr
);
549 __split_expr(expr
->unop
);
550 push_expression(&post_op_stack
, expr
);
554 if (expr
->statement
&& !expr
->statement
) {
555 stmt_set_parent_stmt(expr
->statement
,
556 last_ptr_list((struct ptr_list
*)big_statement_stack
));
558 __split_stmt(expr
->statement
);
563 expr_set_parent_expr(expr
->left
, expr
);
564 expr_set_parent_expr(expr
->right
, expr
);
566 __pass_to_client(expr
, LOGIC_HOOK
);
567 __handle_logic(expr
);
570 expr_set_parent_expr(expr
->left
, expr
);
571 expr_set_parent_expr(expr
->right
, expr
);
573 __pass_to_client(expr
, BINOP_HOOK
);
574 __split_expr(expr
->left
);
575 __split_expr(expr
->right
);
578 expr_set_parent_expr(expr
->left
, expr
);
579 expr_set_parent_expr(expr
->right
, expr
);
581 __split_expr(expr
->left
);
582 __process_post_op_stack();
583 __split_expr(expr
->right
);
585 case EXPR_ASSIGNMENT
: {
586 struct expression
*right
;
588 expr_set_parent_expr(expr
->left
, expr
);
589 expr_set_parent_expr(expr
->right
, expr
);
591 right
= strip_expr(expr
->right
);
595 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
598 if (__handle_condition_assigns(expr
))
600 /* foo = (x < 5 ? foo : 5); */
601 if (__handle_select_assigns(expr
))
603 /* foo = ({frob(); frob(); frob(); 1;}) */
604 if (__handle_expr_statement_assigns(expr
))
605 break; // FIXME: got after
607 if (handle_comma_assigns(expr
))
609 if (handle__builtin_choose_expr_assigns(expr
))
611 if (handle_postop_assigns(expr
))
612 break; /* no need to goto after_assign */
614 __split_expr(expr
->right
);
615 if (outside_of_function())
616 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
618 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
620 __fake_struct_member_assignments(expr
);
622 /* Re-examine ->right for inlines. See the commit message */
623 right
= strip_expr(expr
->right
);
624 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
625 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
628 if (get_macro_name(right
->pos
) &&
629 get_macro_name(expr
->pos
) != get_macro_name(right
->pos
))
630 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
632 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
633 __split_expr(expr
->left
);
637 expr_set_parent_expr(expr
->deref
, expr
);
639 __pass_to_client(expr
, DEREF_HOOK
);
640 __split_expr(expr
->deref
);
643 expr_set_parent_expr(expr
->base
, expr
);
645 __split_expr(expr
->base
);
648 case EXPR_FORCE_CAST
:
649 expr_set_parent_expr(expr
->cast_expression
, expr
);
651 __pass_to_client(expr
, CAST_HOOK
);
652 __split_expr(expr
->cast_expression
);
655 if (expr
->cast_expression
)
656 __pass_to_client(strip_parens(expr
->cast_expression
),
662 case EXPR_CONDITIONAL
:
664 expr_set_parent_expr(expr
->conditional
, expr
);
665 expr_set_parent_expr(expr
->cond_true
, expr
);
666 expr_set_parent_expr(expr
->cond_false
, expr
);
668 if (known_condition_true(expr
->conditional
)) {
669 __split_expr(expr
->cond_true
);
672 if (known_condition_false(expr
->conditional
)) {
673 __split_expr(expr
->cond_false
);
676 __pass_to_client(expr
, SELECT_HOOK
);
677 __split_whole_condition(expr
->conditional
);
678 __split_expr(expr
->cond_true
);
679 __push_true_states();
680 __use_false_states();
681 __split_expr(expr
->cond_false
);
682 __merge_true_states();
687 case EXPR_INITIALIZER
:
688 split_expr_list(expr
->expr_list
, expr
);
690 case EXPR_IDENTIFIER
:
691 expr_set_parent_expr(expr
->ident_expression
, expr
);
692 __split_expr(expr
->ident_expression
);
695 expr_set_parent_expr(expr
->idx_expression
, expr
);
696 __split_expr(expr
->idx_expression
);
699 expr_set_parent_expr(expr
->init_expr
, expr
);
700 __split_expr(expr
->init_expr
);
703 __pass_to_client(expr
, SYM_HOOK
);
706 __pass_to_client(expr
, STRING_HOOK
);
711 __pass_to_client(expr
, EXPR_HOOK_AFTER
);
712 pop_expression(&big_expression_stack
);
715 static int is_forever_loop(struct statement
*stmt
)
717 struct expression
*expr
;
720 expr
= strip_expr(stmt
->iterator_pre_condition
);
722 expr
= stmt
->iterator_post_condition
;
724 /* this is a for(;;) loop... */
728 if (get_value(expr
, &sval
) && sval
.value
!= 0)
735 static char *get_loop_name(int num
)
739 snprintf(buf
, 255, "-loop%d", num
);
741 return alloc_sname(buf
);
745 * Pre Loops are while and for loops.
747 static void handle_pre_loop(struct statement
*stmt
)
749 int once_through
; /* we go through the loop at least once */
750 struct sm_state
*extra_sm
= NULL
;
753 struct stree
*stree
= NULL
;
754 struct sm_state
*sm
= NULL
;
756 loop_name
= get_loop_name(loop_num
);
759 if (stmt
->iterator_pre_statement
) {
760 __split_stmt(stmt
->iterator_pre_statement
);
761 __prev_stmt
= stmt
->iterator_pre_statement
;
764 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
770 __merge_gotos(loop_name
, NULL
);
772 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
773 __in_pre_condition
++;
774 __pass_to_client(stmt
, PRELOOP_HOOK
);
775 __split_whole_condition(stmt
->iterator_pre_condition
);
776 __in_pre_condition
--;
777 FOR_EACH_SM(stree
, sm
) {
778 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
779 } END_FOR_EACH_SM(sm
);
782 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
784 if (option_assume_loops
)
787 __split_stmt(stmt
->iterator_statement
);
788 if (is_forever_loop(stmt
)) {
790 __save_gotos(loop_name
, NULL
);
792 __push_fake_cur_stree();
793 __split_stmt(stmt
->iterator_post_statement
);
794 stree
= __pop_fake_cur_stree();
796 __discard_false_states();
797 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
800 if (!__path_is_null())
801 __merge_stree_into_cur(stree
);
805 unchanged
= __iterator_unchanged(extra_sm
);
806 __split_stmt(stmt
->iterator_post_statement
);
807 __prev_stmt
= stmt
->iterator_post_statement
;
810 __save_gotos(loop_name
, NULL
);
811 __in_pre_condition
++;
812 __split_whole_condition(stmt
->iterator_pre_condition
);
813 __in_pre_condition
--;
815 __merge_false_states();
817 __discard_false_states();
819 __merge_false_states();
821 if (extra_sm
&& unchanged
)
822 __extra_pre_loop_hook_after(extra_sm
,
823 stmt
->iterator_post_statement
,
824 stmt
->iterator_pre_condition
);
825 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
832 * Post loops are do {} while();
834 static void handle_post_loop(struct statement
*stmt
)
838 loop_name
= get_loop_name(loop_num
);
842 __pass_to_client(stmt
, POSTLOOP_HOOK
);
846 __merge_gotos(loop_name
, NULL
);
847 __split_stmt(stmt
->iterator_statement
);
849 if (!expr_is_zero(stmt
->iterator_post_condition
))
850 __save_gotos(loop_name
, NULL
);
852 if (is_forever_loop(stmt
)) {
853 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
856 __split_whole_condition(stmt
->iterator_post_condition
);
857 __use_false_states();
858 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
864 static int empty_statement(struct statement
*stmt
)
868 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
873 static int last_stmt_on_same_line(void)
875 struct statement
*stmt
;
878 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
881 if (stmt
->pos
.line
== get_lineno())
884 } END_FOR_EACH_PTR_REVERSE(stmt
);
888 static void split_asm_ops(struct asm_operand_list
*ops
)
890 struct asm_operand
*op
;
892 FOR_EACH_PTR(ops
, op
) {
893 __split_expr(op
->expr
);
894 } END_FOR_EACH_PTR(op
);
897 static int is_case_val(struct statement
*stmt
, sval_t sval
)
901 if (stmt
->type
!= STMT_CASE
)
903 if (!stmt
->case_expression
) {
907 if (!get_value(stmt
->case_expression
, &case_sval
))
909 if (case_sval
.value
== sval
.value
)
914 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
915 struct expression
*case_expr
,
916 struct expression
*case_to
)
919 struct range_list
*rl
= NULL
;
920 struct symbol
*switch_type
;
922 switch_type
= get_type(switch_expr
);
923 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
924 start
= sval_cast(switch_type
, start
);
925 end
= sval_cast(switch_type
, end
);
926 add_range(&rl
, start
, end
);
927 } else if (get_value(case_expr
, &start
)) {
928 start
= sval_cast(switch_type
, start
);
929 add_range(&rl
, start
, start
);
935 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
937 struct statement
*tmp
;
938 struct range_list
*rl
;
940 __split_expr(stmt
->switch_expression
);
941 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
943 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
944 __save_switch_states(top_expression(switch_expr_stack
));
949 stmt
= stmt
->switch_statement
;
951 __push_scope_hooks();
952 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
953 __smatch_lineno
= tmp
->pos
.line
;
954 // FIXME: what if default comes before the known case statement?
955 if (is_case_val(tmp
, sval
)) {
956 rl
= alloc_rl(sval
, sval
);
957 __merge_switches(top_expression(switch_expr_stack
), rl
);
958 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
959 stmt_set_parent_stmt(tmp
->case_statement
, tmp
);
960 __split_stmt(tmp
->case_statement
);
963 if (__path_is_null())
967 if (__path_is_null()) {
971 } END_FOR_EACH_PTR(tmp
);
973 __call_scope_hooks();
974 if (!__pop_default())
975 __merge_switches(top_expression(switch_expr_stack
), NULL
);
976 __discard_switches();
978 pop_expression(&switch_expr_stack
);
981 static void split_case(struct statement
*stmt
)
983 struct range_list
*rl
= NULL
;
985 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
986 expr_set_parent_stmt(stmt
->case_to
, stmt
);
988 rl
= get_case_rl(top_expression(switch_expr_stack
),
989 stmt
->case_expression
, stmt
->case_to
);
990 while (stmt
->case_statement
->type
== STMT_CASE
) {
991 struct range_list
*tmp
;
993 tmp
= get_case_rl(top_expression(switch_expr_stack
),
994 stmt
->case_statement
->case_expression
,
995 stmt
->case_statement
->case_to
);
998 rl
= rl_union(rl
, tmp
);
999 if (!stmt
->case_expression
)
1001 stmt
= stmt
->case_statement
;
1004 __merge_switches(top_expression(switch_expr_stack
), rl
);
1006 if (!stmt
->case_expression
)
1009 stmt_set_parent_stmt(stmt
->case_statement
, stmt
);
1010 __split_stmt(stmt
->case_statement
);
1013 int time_parsing_function(void)
1015 return ms_since(&fn_start_time
) / 1000;
1018 bool taking_too_long(void)
1020 if ((ms_since(&outer_fn_start_time
) / 1000) > 60 * 5) /* five minutes */
1025 struct statement
*get_last_stmt(void)
1028 struct statement
*stmt
;
1030 fn
= get_base_type(cur_func_sym
);
1035 stmt
= fn
->inline_stmt
;
1036 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
1038 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
1039 if (stmt
&& stmt
->type
== STMT_LABEL
)
1040 stmt
= stmt
->label_statement
;
1044 int is_last_stmt(struct statement
*cur_stmt
)
1046 struct statement
*last
;
1048 last
= get_last_stmt();
1049 if (last
&& last
== cur_stmt
)
1054 static void handle_backward_goto(struct statement
*goto_stmt
)
1056 const char *goto_name
, *label_name
;
1057 struct statement
*func_stmt
;
1058 struct symbol
*base_type
= get_base_type(cur_func_sym
);
1059 struct statement
*tmp
;
1064 if (last_goto_statement_handled
)
1066 last_goto_statement_handled
= 1;
1068 if (!goto_stmt
->goto_label
||
1069 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
1070 !goto_stmt
->goto_label
->ident
)
1072 goto_name
= goto_stmt
->goto_label
->ident
->name
;
1074 func_stmt
= base_type
->stmt
;
1076 func_stmt
= base_type
->inline_stmt
;
1079 if (func_stmt
->type
!= STMT_COMPOUND
)
1082 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
1084 if (tmp
->type
!= STMT_LABEL
)
1086 if (!tmp
->label_identifier
||
1087 tmp
->label_identifier
->type
!= SYM_LABEL
||
1088 !tmp
->label_identifier
->ident
)
1090 label_name
= tmp
->label_identifier
->ident
->name
;
1091 if (strcmp(goto_name
, label_name
) != 0)
1096 } END_FOR_EACH_PTR(tmp
);
1099 static void fake_a_return(void)
1101 struct expression
*ret
= NULL
;
1106 if (cur_func_return_type() != &void_ctype
)
1107 ret
= unknown_value_expression(NULL
);
1109 __pass_to_client(ret
, RETURN_HOOK
);
1113 static void split_ret_value(struct expression
*expr
)
1115 struct symbol
*type
;
1120 type
= get_real_base_type(cur_func_sym
);
1121 type
= get_real_base_type(type
);
1122 expr
= fake_a_variable_assign(type
, NULL
, expr
, -1);
1124 __in_fake_var_assign
++;
1126 __in_fake_var_assign
--;
1129 static void fake_an_empty_default(struct position pos
)
1131 static struct statement none
= {};
1134 none
.type
= STMT_NONE
;
1135 __merge_switches(top_expression(switch_expr_stack
), NULL
);
1136 __split_stmt(&none
);
1139 static void split_compound(struct statement
*stmt
)
1141 struct statement
*prev
= NULL
;
1142 struct statement
*cur
= NULL
;
1143 struct statement
*next
;
1145 __push_scope_hooks();
1147 FOR_EACH_PTR(stmt
->stmts
, next
) {
1148 /* just set them all ahead of time */
1149 stmt_set_parent_stmt(next
, stmt
);
1159 } END_FOR_EACH_PTR(next
);
1168 * For function scope, then delay calling the scope hooks until the
1169 * end of function hooks can run. I'm not positive this is the right
1172 if (!is_last_stmt(cur
))
1173 __call_scope_hooks();
1177 * This is a hack, work around for detecting empty functions.
1179 static int need_delayed_scope_hooks(void)
1181 struct symbol
*fn
= get_base_type(cur_func_sym
);
1182 struct statement
*stmt
;
1188 stmt
= fn
->inline_stmt
;
1189 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
1194 void __split_label_stmt(struct statement
*stmt
)
1196 if (stmt
->label_identifier
&&
1197 stmt
->label_identifier
->type
== SYM_LABEL
&&
1198 stmt
->label_identifier
->ident
) {
1199 loop_count
|= 0x0800000;
1200 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
1204 static void find_asm_gotos(struct statement
*stmt
)
1208 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
1209 __save_gotos(sym
->ident
->name
, sym
);
1210 } END_FOR_EACH_PTR(sym
);
1213 void __split_stmt(struct statement
*stmt
)
1215 static int indent_cnt
;
1217 struct timeval start
, stop
;
1219 gettimeofday(&start
, NULL
);
1224 if (!__in_fake_assign
)
1225 __silence_warnings_for_stmt
= false;
1227 if (__bail_on_rest_of_function
|| is_skipped_function())
1230 if (out_of_memory() || taking_too_long()) {
1231 gettimeofday(&start
, NULL
);
1233 __bail_on_rest_of_function
= 1;
1235 sm_perror("Function too hairy. Giving up. %lu seconds",
1236 start
.tv_sec
- fn_start_time
.tv_sec
);
1238 final_pass
= 0; /* turn off sm_msg() from here */
1244 add_ptr_list(&big_statement_stack
, stmt
);
1245 free_expression_stack(&big_expression_stack
);
1246 set_position(stmt
->pos
);
1247 __pass_to_client(stmt
, STMT_HOOK
);
1249 switch (stmt
->type
) {
1250 case STMT_DECLARATION
:
1251 split_declaration(stmt
->declaration
);
1254 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1256 split_ret_value(stmt
->ret_value
);
1257 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1258 __process_post_op_stack();
1261 case STMT_EXPRESSION
:
1262 expr_set_parent_stmt(stmt
->expression
, stmt
);
1263 expr_set_parent_stmt(stmt
->context
, stmt
);
1265 __split_expr(stmt
->expression
);
1268 split_compound(stmt
);
1271 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1272 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1273 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1275 if (known_condition_true(stmt
->if_conditional
)) {
1276 __split_stmt(stmt
->if_true
);
1279 if (known_condition_false(stmt
->if_conditional
)) {
1280 __split_stmt(stmt
->if_false
);
1283 __split_whole_condition(stmt
->if_conditional
);
1284 __split_stmt(stmt
->if_true
);
1285 if (empty_statement(stmt
->if_true
) &&
1286 last_stmt_on_same_line() &&
1287 !get_macro_name(stmt
->if_true
->pos
))
1288 sm_warning("if();");
1289 __push_true_states();
1290 __use_false_states();
1291 __split_stmt(stmt
->if_false
);
1292 __merge_true_states();
1295 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1296 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1297 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1298 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1299 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1301 if (stmt
->iterator_pre_condition
)
1302 handle_pre_loop(stmt
);
1303 else if (stmt
->iterator_post_condition
)
1304 handle_post_loop(stmt
);
1306 // these are for(;;) type loops.
1307 handle_pre_loop(stmt
);
1311 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1312 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1314 if (get_value(stmt
->switch_expression
, &sval
)) {
1315 split_known_switch(stmt
, sval
);
1318 __split_expr(stmt
->switch_expression
);
1319 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1320 __save_switch_states(top_expression(switch_expr_stack
));
1324 __split_stmt(stmt
->switch_statement
);
1325 if (!__pop_default() && have_remaining_cases())
1326 fake_an_empty_default(stmt
->pos
);
1327 __discard_switches();
1329 pop_expression(&switch_expr_stack
);
1335 __split_label_stmt(stmt
);
1336 __split_stmt(stmt
->label_statement
);
1339 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1341 __split_expr(stmt
->goto_expression
);
1342 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1343 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1345 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1347 __process_continues();
1349 } else if (stmt
->goto_label
&&
1350 stmt
->goto_label
->type
== SYM_LABEL
&&
1351 stmt
->goto_label
->ident
) {
1352 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1355 if (is_last_stmt(stmt
))
1356 handle_backward_goto(stmt
);
1361 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1363 find_asm_gotos(stmt
);
1364 __pass_to_client(stmt
, ASM_HOOK
);
1365 __split_expr(stmt
->asm_string
);
1366 split_asm_ops(stmt
->asm_outputs
);
1367 split_asm_ops(stmt
->asm_inputs
);
1368 split_expr_list(stmt
->asm_clobbers
, NULL
);
1373 __split_expr(stmt
->range_expression
);
1374 __split_expr(stmt
->range_low
);
1375 __split_expr(stmt
->range_high
);
1378 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1379 if (--indent_cnt
== 1)
1380 __discard_fake_states(NULL
);
1383 __process_post_op_stack();
1385 gettimeofday(&stop
, NULL
);
1386 if (option_time_stmt
&& stmt
)
1387 sm_msg("stmt_time%s: %ld",
1388 stmt
->type
== STMT_COMPOUND
? "_block" : "",
1389 stop
.tv_sec
- start
.tv_sec
);
1392 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1394 struct expression
*expr
;
1396 FOR_EACH_PTR(expr_list
, expr
) {
1397 expr_set_parent_expr(expr
, parent
);
1399 __process_post_op_stack();
1400 } END_FOR_EACH_PTR(expr
);
1403 static bool cast_arg(struct symbol
*type
, struct expression
*arg
)
1405 struct symbol
*orig
;
1410 arg
= strip_parens(arg
);
1411 if (arg
!= strip_expr(arg
))
1414 orig
= get_type(arg
);
1417 if (types_equiv(orig
, type
))
1420 if (orig
->type
== SYM_ARRAY
&& type
->type
== SYM_PTR
)
1424 * I would have expected that we could just do use (orig == type) but I
1425 * guess for pointers we need to get the basetype to do that comparison.
1429 if (orig
->type
!= SYM_PTR
||
1430 type
->type
!= SYM_PTR
) {
1431 if (type_fits(type
, orig
))
1435 orig
= get_real_base_type(orig
);
1436 type
= get_real_base_type(type
);
1443 static struct expression
*fake_a_variable_assign(struct symbol
*type
, struct expression
*call
, struct expression
*expr
, int nr
)
1448 if (!expr
|| !cur_func_sym
)
1451 if (expr
->type
== EXPR_ASSIGNMENT
)
1454 /* for va_args then we don't know the type */
1456 type
= get_type(expr
);
1458 cast
= cast_arg(type
, expr
);
1460 * Using expr_to_sym() here is a hack. We want to say that we don't
1461 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1462 * It turns out faking these assignments is way more expensive than I
1463 * would have imagined. I'm not sure why exactly.
1468 * if the code is "return *p;" where "p" is a user pointer then
1469 * we want to create a fake assignment so that it sets the state
1470 * in check_kernel_user_data.c.
1473 if (expr
->type
!= EXPR_PREOP
&&
1474 expr
->op
!= '*' && expr
->op
!= '&' &&
1480 snprintf(buf
, sizeof(buf
), "__fake_return_%p", expr
);
1482 snprintf(buf
, sizeof(buf
), "__fake_param_%p_%d", call
, nr
);
1484 return create_fake_assign(buf
, type
, expr
);
1487 static void split_args(struct expression
*expr
)
1489 struct expression
*arg
, *tmp
;
1490 struct symbol
*type
;
1494 FOR_EACH_PTR(expr
->args
, arg
) {
1496 expr_set_parent_expr(arg
, expr
);
1497 type
= get_arg_type(expr
->fn
, i
);
1498 tmp
= fake_a_variable_assign(type
, expr
, arg
, i
);
1500 __in_fake_var_assign
++;
1503 __in_fake_var_assign
--;
1504 __process_post_op_stack();
1505 } END_FOR_EACH_PTR(arg
);
1508 static void split_sym(struct symbol
*sym
)
1512 if (!(sym
->namespace & NS_SYMBOL
))
1515 __split_stmt(sym
->stmt
);
1516 __split_expr(sym
->array_size
);
1517 split_symlist(sym
->arguments
);
1518 split_symlist(sym
->symbol_list
);
1519 __split_stmt(sym
->inline_stmt
);
1520 split_symlist(sym
->inline_symbol_list
);
1523 static void split_symlist(struct symbol_list
*sym_list
)
1527 FOR_EACH_PTR(sym_list
, sym
) {
1529 } END_FOR_EACH_PTR(sym
);
1532 typedef void (fake_cb
)(struct expression
*expr
);
1534 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1536 struct symbol
*type
, *tmp
;
1542 name
= member
->name
;
1544 type
= get_type(expr
);
1545 if (!type
|| type
->type
!= SYM_STRUCT
)
1549 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1553 if (strcmp(name
, tmp
->ident
->name
) == 0)
1555 } END_FOR_EACH_PTR(tmp
);
1559 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1561 struct symbol
*type
, *member
;
1564 type
= get_type(expr
);
1565 if (!type
|| type
->type
!= SYM_STRUCT
)
1568 FOR_EACH_PTR(type
->symbol_list
, member
) {
1570 return member
->ident
;
1572 } END_FOR_EACH_PTR(member
);
1576 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1578 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1580 struct expression
*edge_member
, *assign
;
1581 struct symbol
*base
= get_real_base_type(member
);
1585 expr
= member_expression(expr
, '.', member
->ident
);
1587 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1588 struct symbol
*type
;
1590 type
= get_real_base_type(tmp
);
1594 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1595 if (get_extra_state(edge_member
))
1598 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1599 set_inner_struct_members(expr
, tmp
);
1606 assign
= assign_expression(edge_member
, '=', zero_expr());
1607 __split_expr(assign
);
1608 } END_FOR_EACH_PTR(tmp
);
1613 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1616 struct expression
*member
= NULL
;
1617 struct expression
*assign
;
1619 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1620 type
= get_real_base_type(tmp
);
1625 member
= member_expression(expr
, '.', tmp
->ident
);
1626 if (get_extra_state(member
))
1630 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1631 set_inner_struct_members(expr
, tmp
);
1634 if (type
->type
== SYM_ARRAY
)
1639 assign
= assign_expression(member
, '=', zero_expr());
1640 __split_expr(assign
);
1641 } END_FOR_EACH_PTR(tmp
);
1644 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1646 struct expression
*deref
, *assign
, *tmp
, *right
;
1647 struct symbol
*struct_type
, *type
;
1648 struct ident
*member
;
1651 struct_type
= get_type(symbol
);
1653 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1657 * We're parsing an initializer that could look something like this:
1658 * struct foo foo = {
1660 * .whatever.xxx = 11,
1664 * So what we have here is a list with 42, .whatever, and .zzz. We need
1665 * to break it up into left and right sides of the assignments.
1669 FOR_EACH_PTR(members
, tmp
) {
1671 if (tmp
->type
== EXPR_IDENTIFIER
) {
1672 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1673 while (tmp
->type
== EXPR_IDENTIFIER
) {
1674 member
= tmp
->expr_ident
;
1675 tmp
= tmp
->ident_expression
;
1677 deref
= member_expression(deref
, '.', member
);
1679 deref
= member_expression(symbol
, '.', member
);
1682 member
= number_to_member(symbol
, member_idx
);
1683 deref
= member_expression(symbol
, '.', member
);
1687 if (right
->type
== EXPR_INITIALIZER
) {
1688 type
= get_type(deref
);
1689 if (type
&& type
->type
== SYM_ARRAY
)
1690 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1692 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1694 assign
= assign_expression(deref
, '=', right
);
1697 } END_FOR_EACH_PTR(tmp
);
1699 set_unset_to_zero(struct_type
, symbol
);
1702 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1704 fake_member_assigns_helper(symbol_expression(sym
),
1705 sym
->initializer
->expr_list
, fake_cb
);
1708 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1710 struct expression
*offset
, *binop
, *assign
, *tmp
;
1711 struct symbol
*type
;
1714 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1719 FOR_EACH_PTR(expr_list
, tmp
) {
1720 if (tmp
->type
== EXPR_INDEX
) {
1721 if (tmp
->idx_from
!= tmp
->idx_to
)
1723 idx
= tmp
->idx_from
;
1726 if (!tmp
->idx_expression
)
1728 tmp
= tmp
->idx_expression
;
1730 offset
= value_expr(idx
);
1731 binop
= array_element_expression(array
, offset
);
1732 if (tmp
->type
== EXPR_INITIALIZER
) {
1733 type
= get_type(binop
);
1734 if (type
&& type
->type
== SYM_ARRAY
)
1735 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1737 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1739 assign
= assign_expression(binop
, '=', tmp
);
1746 } END_FOR_EACH_PTR(tmp
);
1748 __call_array_initialized_hooks(array
, max
);
1751 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1753 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1756 static void fake_assign_expr(struct symbol
*sym
)
1758 struct expression
*assign
, *symbol
;
1760 symbol
= symbol_expression(sym
);
1761 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1762 __split_expr(assign
);
1765 static void do_initializer_stuff(struct symbol
*sym
)
1767 if (!sym
->initializer
)
1770 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1771 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1772 fake_element_assigns(sym
, __split_expr
);
1774 fake_member_assigns(sym
, __split_expr
);
1776 fake_assign_expr(sym
);
1780 static void split_declaration(struct symbol_list
*sym_list
)
1784 FOR_EACH_PTR(sym_list
, sym
) {
1785 __pass_to_client(sym
, DECLARATION_HOOK
);
1786 do_initializer_stuff(sym
);
1787 __pass_to_client(sym
, DECLARATION_HOOK_AFTER
);
1789 } END_FOR_EACH_PTR(sym
);
1792 static void call_global_assign_hooks(struct expression
*assign
)
1794 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1797 static void fake_global_assign(struct symbol
*sym
)
1799 struct expression
*assign
, *symbol
;
1801 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1802 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1803 fake_element_assigns(sym
, call_global_assign_hooks
);
1804 } else if (sym
->initializer
) {
1805 symbol
= symbol_expression(sym
);
1806 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1807 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1809 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1811 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1812 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1813 fake_member_assigns(sym
, call_global_assign_hooks
);
1814 } else if (sym
->initializer
) {
1815 symbol
= symbol_expression(sym
);
1816 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1817 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1819 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1822 symbol
= symbol_expression(sym
);
1823 if (sym
->initializer
) {
1824 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1825 __split_expr(assign
);
1827 assign
= assign_expression(symbol
, '=', zero_expr());
1829 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1833 static void start_function_definition(struct symbol
*sym
)
1835 __in_function_def
= 1;
1836 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1837 __in_function_def
= 0;
1838 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1842 void add_function_data(unsigned long *fn_data
)
1844 __add_ptr_list(&fn_data_list
, fn_data
);
1847 static void clear_function_data(void)
1851 FOR_EACH_PTR(fn_data_list
, tmp
) {
1853 } END_FOR_EACH_PTR(tmp
);
1856 static void record_func_time(void)
1858 struct timeval stop
;
1862 gettimeofday(&stop
, NULL
);
1863 func_time
= stop
.tv_sec
- fn_start_time
.tv_sec
;
1864 snprintf(buf
, sizeof(buf
), "%d", func_time
);
1865 sql_insert_return_implies(FUNC_TIME
, 0, "", buf
);
1866 if (option_time
&& func_time
> 2) {
1868 sm_msg("func_time: %d", func_time
);
1873 static void split_function(struct symbol
*sym
)
1875 struct symbol
*base_type
= get_base_type(sym
);
1877 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1880 gettimeofday(&outer_fn_start_time
, NULL
);
1881 gettimeofday(&fn_start_time
, NULL
);
1884 cur_func
= sym
->ident
->name
;
1885 set_position(sym
->pos
);
1886 clear_function_data();
1888 last_goto_statement_handled
= 0;
1889 sm_debug("new function: %s\n", cur_func
);
1891 if (option_two_passes
) {
1895 start_function_definition(sym
);
1896 __split_stmt(base_type
->stmt
);
1897 __split_stmt(base_type
->inline_stmt
);
1903 start_function_definition(sym
);
1904 __split_stmt(base_type
->stmt
);
1905 __split_stmt(base_type
->inline_stmt
);
1906 if (!__path_is_null() &&
1907 cur_func_return_type() == &void_ctype
&&
1908 !__bail_on_rest_of_function
) {
1909 __pass_to_client(NULL
, RETURN_HOOK
);
1912 __pass_to_client(sym
, END_FUNC_HOOK
);
1913 if (need_delayed_scope_hooks())
1914 __call_scope_hooks();
1915 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1922 cur_func_sym
= NULL
;
1924 free_data_info_allocs();
1925 free_expression_stack(&switch_expr_stack
);
1926 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1927 __bail_on_rest_of_function
= 0;
1930 static void save_flow_state(void)
1934 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2));
1935 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2));
1936 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2));
1938 __add_ptr_list(&backup
, big_statement_stack
);
1939 __add_ptr_list(&backup
, big_expression_stack
);
1940 __add_ptr_list(&backup
, big_condition_stack
);
1941 __add_ptr_list(&backup
, switch_expr_stack
);
1943 __add_ptr_list(&backup
, cur_func_sym
);
1945 __add_ptr_list(&backup
, __prev_stmt
);
1946 __add_ptr_list(&backup
, __cur_stmt
);
1947 __add_ptr_list(&backup
, __next_stmt
);
1949 FOR_EACH_PTR(fn_data_list
, tmp
) {
1950 __add_ptr_list(&backup
, (void *)*tmp
);
1951 } END_FOR_EACH_PTR(tmp
);
1954 static void *pop_backup(void)
1958 ret
= last_ptr_list(backup
);
1959 delete_ptr_list_last(&backup
);
1963 static void restore_flow_state(void)
1967 FOR_EACH_PTR_REVERSE(fn_data_list
, tmp
) {
1968 *tmp
= (unsigned long)pop_backup();
1969 } END_FOR_EACH_PTR_REVERSE(tmp
);
1971 __next_stmt
= pop_backup();
1972 __cur_stmt
= pop_backup();
1973 __prev_stmt
= pop_backup();
1975 cur_func_sym
= pop_backup();
1976 switch_expr_stack
= pop_backup();
1977 big_condition_stack
= pop_backup();
1978 big_expression_stack
= pop_backup();
1979 big_statement_stack
= pop_backup();
1980 final_pass
= PTR_INT(pop_backup()) >> 2;
1981 loop_count
= PTR_INT(pop_backup()) >> 2;
1982 loop_num
= PTR_INT(pop_backup()) >> 2;
1985 static void parse_inline(struct expression
*call
)
1987 struct symbol
*base_type
;
1988 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
1989 struct timeval time_backup
= fn_start_time
;
1990 struct expression
*orig_inline
= __inline_fn
;
1993 if (out_of_memory() || taking_too_long())
1998 __pass_to_client(call
, INLINE_FN_START
);
1999 final_pass
= 0; /* don't print anything */
2001 orig_budget
= inline_budget
;
2002 inline_budget
= inline_budget
- 5;
2004 base_type
= get_base_type(call
->fn
->symbol
);
2005 cur_func_sym
= call
->fn
->symbol
;
2006 if (call
->fn
->symbol
->ident
)
2007 cur_func
= call
->fn
->symbol
->ident
->name
;
2010 set_position(call
->fn
->symbol
->pos
);
2013 big_statement_stack
= NULL
;
2014 big_expression_stack
= NULL
;
2015 big_condition_stack
= NULL
;
2016 switch_expr_stack
= NULL
;
2018 sm_debug("inline function: %s\n", cur_func
);
2020 clear_function_data();
2023 start_function_definition(call
->fn
->symbol
);
2024 __split_stmt(base_type
->stmt
);
2025 __split_stmt(base_type
->inline_stmt
);
2026 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
2027 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
2028 call
->fn
->symbol
->parsed
= true;
2030 free_expression_stack(&switch_expr_stack
);
2031 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
2035 restore_flow_state();
2036 fn_start_time
= time_backup
;
2037 cur_func
= cur_func_bak
;
2039 restore_all_states();
2040 set_position(call
->pos
);
2041 __inline_fn
= orig_inline
;
2042 inline_budget
= orig_budget
;
2043 __pass_to_client(call
, INLINE_FN_END
);
2046 static struct symbol_list
*inlines_called
;
2047 static void add_inline_function(struct symbol
*sym
)
2049 static struct symbol_list
*already_added
;
2052 FOR_EACH_PTR(already_added
, tmp
) {
2055 } END_FOR_EACH_PTR(tmp
);
2057 add_ptr_list(&already_added
, sym
);
2058 add_ptr_list(&inlines_called
, sym
);
2061 static void process_inlines(void)
2065 FOR_EACH_PTR(inlines_called
, tmp
) {
2066 split_function(tmp
);
2067 } END_FOR_EACH_PTR(tmp
);
2068 free_ptr_list(&inlines_called
);
2071 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
2075 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
2078 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
2080 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
2082 } END_FOR_EACH_PTR_REVERSE(sym
);
2087 static bool interesting_function(struct symbol
*sym
)
2089 static int prev_stream
= -1;
2090 static bool prev_answer
;
2091 const char *filename
;
2094 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
2097 if (sym
->pos
.stream
== prev_stream
)
2100 prev_stream
= sym
->pos
.stream
;
2101 prev_answer
= false;
2103 filename
= stream_name(sym
->pos
.stream
);
2104 len
= strlen(filename
);
2105 if (len
> 0 && filename
[len
- 1] == 'c')
2110 static void split_inlines_in_scope(struct symbol
*sym
)
2112 struct symbol
*base
;
2113 struct symbol_list
*scope_list
;
2116 scope_list
= sym
->scope
->symbols
;
2117 stream
= sym
->pos
.stream
;
2119 /* find the last static symbol in the file */
2120 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
2121 if (sym
->pos
.stream
!= stream
)
2123 if (sym
->type
!= SYM_NODE
)
2125 base
= get_base_type(sym
);
2128 if (base
->type
!= SYM_FN
)
2130 if (!base
->inline_stmt
)
2132 if (!interesting_function(sym
))
2134 add_inline_function(sym
);
2135 } END_FOR_EACH_PTR_REVERSE(sym
);
2140 static void split_inlines(struct symbol_list
*sym_list
)
2144 sym
= get_last_scoped_symbol(sym_list
, 0);
2146 split_inlines_in_scope(sym
);
2147 sym
= get_last_scoped_symbol(sym_list
, 1);
2149 split_inlines_in_scope(sym
);
2152 static struct stree
*clone_estates_perm(struct stree
*orig
)
2154 struct stree
*ret
= NULL
;
2155 struct sm_state
*tmp
;
2157 FOR_EACH_SM(orig
, tmp
) {
2158 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
2159 } END_FOR_EACH_SM(tmp
);
2164 struct position last_pos
;
2165 static void split_c_file_functions(struct symbol_list
*sym_list
)
2170 FOR_EACH_PTR(sym_list
, sym
) {
2171 set_position(sym
->pos
);
2172 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
2173 __pass_to_client(sym
, BASE_HOOK
);
2174 fake_global_assign(sym
);
2175 __pass_to_client(sym
, DECLARATION_HOOK_AFTER
);
2177 } END_FOR_EACH_PTR(sym
);
2178 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
2181 FOR_EACH_PTR(sym_list
, sym
) {
2182 set_position(sym
->pos
);
2183 last_pos
= sym
->pos
;
2184 if (!interesting_function(sym
))
2186 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
2187 split_function(sym
);
2190 last_pos
= sym
->pos
;
2191 } END_FOR_EACH_PTR(sym
);
2192 split_inlines(sym_list
);
2193 __pass_to_client(sym_list
, END_FILE_HOOK
);
2196 static int final_before_fake
;
2197 void init_fake_env(void)
2200 final_before_fake
= final_pass
;
2202 __push_fake_cur_stree();
2206 void end_fake_env(void)
2208 __free_fake_cur_stree();
2211 final_pass
= final_before_fake
;
2214 static void open_output_files(char *base_file
)
2218 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
2219 sm_outfd
= fopen(buf
, "w");
2221 sm_fatal("Cannot open %s", buf
);
2226 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
2227 sql_outfd
= fopen(buf
, "w");
2229 sm_fatal("Error: Cannot open %s", buf
);
2231 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
2232 caller_info_fd
= fopen(buf
, "w");
2233 if (!caller_info_fd
)
2234 sm_fatal("Error: Cannot open %s", buf
);
2237 void smatch(struct string_list
*filelist
)
2239 struct symbol_list
*sym_list
;
2240 struct timeval stop
, start
;
2244 gettimeofday(&start
, NULL
);
2246 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
2247 path
= getcwd(NULL
, 0);
2248 free(full_base_file
);
2250 len
= strlen(path
) + 1 + strlen(base_file
) + 1;
2251 full_base_file
= malloc(len
);
2252 snprintf(full_base_file
, len
, "%s/%s", path
, base_file
);
2254 full_base_file
= alloc_string(base_file
);
2256 if (option_file_output
)
2257 open_output_files(base_file
);
2258 base_file_stream
= input_stream_nr
;
2259 sym_list
= sparse_keep_tokens(base_file
);
2260 split_c_file_functions(sym_list
);
2261 } END_FOR_EACH_PTR_NOTAG(base_file
);
2263 gettimeofday(&stop
, NULL
);
2265 set_position(last_pos
);
2268 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
2270 sm_msg("mem: %luKb", get_max_memory());