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 struct expression
*expr_get_parent_no_parens(struct expression
*expr
)
445 expr
= expr_get_parent_expr(expr
);
447 expr
->type
== EXPR_PREOP
&&
453 static bool gen_fake_function_assign(struct expression
*expr
)
455 static struct expression
*parsed
;
456 struct expression
*assign
, *parent
;
460 /* The rule is that every non-void function call has to be part of an
461 * assignment. TODO: Should we create a fake non-casted assignment
462 * for casted assignments? Also faked assigns for += assignments?
464 type
= get_type(expr
);
465 if (!type
|| type
== &void_ctype
)
468 parent
= expr_get_parent_no_parens(expr
);
469 if (parent
&& parent
->type
== EXPR_ASSIGNMENT
)
472 parent
= expr_get_fake_parent_expr(expr
);
474 struct expression
*left
= parent
->left
;
476 if (parent
== parsed
)
478 if (!left
|| left
->type
!= EXPR_SYMBOL
)
480 if (strncmp(left
->symbol_name
->name
, "__fake_assign_", 14) != 0)
483 __split_expr(parent
);
487 // TODO: faked_assign skipping conditions is a hack
488 if (is_condition_call(expr
))
491 snprintf(buf
, sizeof(buf
), "__fake_assign_%p", expr
);
492 assign
= create_fake_assign(buf
, get_type(expr
), expr
);
495 __split_expr(assign
);
499 static void split_call(struct expression
*expr
)
501 if (gen_fake_function_assign(expr
))
504 expr_set_parent_expr(expr
->fn
, expr
);
506 if (sym_name_is("__builtin_constant_p", expr
->fn
))
508 if (handle__builtin_choose_expr(expr
))
510 __split_expr(expr
->fn
);
512 if (is_inline_func(expr
->fn
))
513 add_inline_function(expr
->fn
->symbol
->definition
);
514 if (inlinable(expr
->fn
))
516 __process_post_op_stack();
517 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
518 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
520 if (inlinable(expr
->fn
))
522 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
523 if (is_noreturn_func(expr
->fn
))
525 if (!expr_get_parent_expr(expr
))
526 __discard_fake_states(expr
);
527 handle_builtin_overflow_func(expr
);
530 void __split_expr(struct expression
*expr
)
536 // sm_msg("Debug expr_type %d %s expr = '%s'", expr->type, show_special(expr->op), expr_to_str(expr));
538 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
540 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
543 push_expression(&big_expression_stack
, expr
);
544 set_position(expr
->pos
);
545 __pass_to_client(expr
, EXPR_HOOK
);
547 switch (expr
->type
) {
549 expr_set_parent_expr(expr
->unop
, expr
);
551 if (expr
->op
== '*' &&
552 !prev_expression_is_getting_address(expr
))
553 __pass_to_client(expr
, DEREF_HOOK
);
554 __split_expr(expr
->unop
);
555 __pass_to_client(expr
, OP_HOOK
);
558 expr_set_parent_expr(expr
->unop
, expr
);
560 __split_expr(expr
->unop
);
561 push_expression(&post_op_stack
, expr
);
565 if (expr
->statement
&& !expr
->statement
) {
566 stmt_set_parent_stmt(expr
->statement
,
567 last_ptr_list((struct ptr_list
*)big_statement_stack
));
569 __split_stmt(expr
->statement
);
574 expr_set_parent_expr(expr
->left
, expr
);
575 expr_set_parent_expr(expr
->right
, expr
);
577 __pass_to_client(expr
, LOGIC_HOOK
);
578 __handle_logic(expr
);
581 expr_set_parent_expr(expr
->left
, expr
);
582 expr_set_parent_expr(expr
->right
, expr
);
584 __pass_to_client(expr
, BINOP_HOOK
);
585 __split_expr(expr
->left
);
586 __split_expr(expr
->right
);
589 expr_set_parent_expr(expr
->left
, expr
);
590 expr_set_parent_expr(expr
->right
, expr
);
592 __split_expr(expr
->left
);
593 __process_post_op_stack();
594 __split_expr(expr
->right
);
596 case EXPR_ASSIGNMENT
: {
597 struct expression
*right
;
599 expr_set_parent_expr(expr
->left
, expr
);
600 expr_set_parent_expr(expr
->right
, expr
);
602 right
= strip_expr(expr
->right
);
606 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
609 if (__handle_condition_assigns(expr
))
611 /* foo = (x < 5 ? foo : 5); */
612 if (__handle_select_assigns(expr
))
614 /* foo = ({frob(); frob(); frob(); 1;}) */
615 if (__handle_expr_statement_assigns(expr
))
616 break; // FIXME: got after
618 if (handle_comma_assigns(expr
))
620 if (handle__builtin_choose_expr_assigns(expr
))
622 if (handle_postop_assigns(expr
))
623 break; /* no need to goto after_assign */
625 __split_expr(expr
->right
);
626 if (outside_of_function())
627 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
629 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
631 __fake_struct_member_assignments(expr
);
633 /* Re-examine ->right for inlines. See the commit message */
634 right
= strip_expr(expr
->right
);
635 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
636 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
639 if (get_macro_name(right
->pos
) &&
640 get_macro_name(expr
->pos
) != get_macro_name(right
->pos
))
641 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
643 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
644 __split_expr(expr
->left
);
648 expr_set_parent_expr(expr
->deref
, expr
);
650 __pass_to_client(expr
, DEREF_HOOK
);
651 __split_expr(expr
->deref
);
654 expr_set_parent_expr(expr
->base
, expr
);
656 __split_expr(expr
->base
);
659 case EXPR_FORCE_CAST
:
660 expr_set_parent_expr(expr
->cast_expression
, expr
);
662 __pass_to_client(expr
, CAST_HOOK
);
663 __split_expr(expr
->cast_expression
);
666 if (expr
->cast_expression
)
667 __pass_to_client(strip_parens(expr
->cast_expression
),
673 case EXPR_CONDITIONAL
:
675 expr_set_parent_expr(expr
->conditional
, expr
);
676 expr_set_parent_expr(expr
->cond_true
, expr
);
677 expr_set_parent_expr(expr
->cond_false
, expr
);
679 if (known_condition_true(expr
->conditional
)) {
680 __split_expr(expr
->cond_true
);
683 if (known_condition_false(expr
->conditional
)) {
684 __split_expr(expr
->cond_false
);
687 __pass_to_client(expr
, SELECT_HOOK
);
688 __split_whole_condition(expr
->conditional
);
689 __split_expr(expr
->cond_true
);
690 __push_true_states();
691 __use_false_states();
692 __split_expr(expr
->cond_false
);
693 __merge_true_states();
698 case EXPR_INITIALIZER
:
699 split_expr_list(expr
->expr_list
, expr
);
701 case EXPR_IDENTIFIER
:
702 expr_set_parent_expr(expr
->ident_expression
, expr
);
703 __split_expr(expr
->ident_expression
);
706 expr_set_parent_expr(expr
->idx_expression
, expr
);
707 __split_expr(expr
->idx_expression
);
710 expr_set_parent_expr(expr
->init_expr
, expr
);
711 __split_expr(expr
->init_expr
);
714 __pass_to_client(expr
, SYM_HOOK
);
717 __pass_to_client(expr
, STRING_HOOK
);
720 struct expression
*tmp
;
722 tmp
= strip_Generic(expr
);
730 __pass_to_client(expr
, EXPR_HOOK_AFTER
);
731 pop_expression(&big_expression_stack
);
734 static int is_forever_loop(struct statement
*stmt
)
736 struct expression
*expr
;
739 expr
= strip_expr(stmt
->iterator_pre_condition
);
741 expr
= stmt
->iterator_post_condition
;
743 /* this is a for(;;) loop... */
747 if (get_value(expr
, &sval
) && sval
.value
!= 0)
754 static char *get_loop_name(int num
)
758 snprintf(buf
, 255, "-loop%d", num
);
760 return alloc_sname(buf
);
764 * Pre Loops are while and for loops.
766 static void handle_pre_loop(struct statement
*stmt
)
768 int once_through
; /* we go through the loop at least once */
769 struct sm_state
*extra_sm
= NULL
;
772 struct stree
*stree
= NULL
;
773 struct sm_state
*sm
= NULL
;
775 loop_name
= get_loop_name(loop_num
);
778 if (stmt
->iterator_pre_statement
) {
779 __split_stmt(stmt
->iterator_pre_statement
);
780 __prev_stmt
= stmt
->iterator_pre_statement
;
783 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
789 __merge_gotos(loop_name
, NULL
);
791 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
792 __in_pre_condition
++;
793 __pass_to_client(stmt
, PRELOOP_HOOK
);
794 __split_whole_condition(stmt
->iterator_pre_condition
);
795 __in_pre_condition
--;
796 FOR_EACH_SM(stree
, sm
) {
797 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
798 } END_FOR_EACH_SM(sm
);
801 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
803 if (option_assume_loops
)
806 __split_stmt(stmt
->iterator_statement
);
807 if (is_forever_loop(stmt
)) {
809 __save_gotos(loop_name
, NULL
);
811 __push_fake_cur_stree();
812 __split_stmt(stmt
->iterator_post_statement
);
813 stree
= __pop_fake_cur_stree();
815 __discard_false_states();
816 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
819 if (!__path_is_null())
820 __merge_stree_into_cur(stree
);
824 unchanged
= __iterator_unchanged(extra_sm
);
825 __split_stmt(stmt
->iterator_post_statement
);
826 __prev_stmt
= stmt
->iterator_post_statement
;
829 __save_gotos(loop_name
, NULL
);
830 __in_pre_condition
++;
831 __split_whole_condition(stmt
->iterator_pre_condition
);
832 __in_pre_condition
--;
834 __merge_false_states();
836 __discard_false_states();
838 __merge_false_states();
840 if (extra_sm
&& unchanged
)
841 __extra_pre_loop_hook_after(extra_sm
,
842 stmt
->iterator_post_statement
,
843 stmt
->iterator_pre_condition
);
844 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
851 * Post loops are do {} while();
853 static void handle_post_loop(struct statement
*stmt
)
857 loop_name
= get_loop_name(loop_num
);
861 __pass_to_client(stmt
, POSTLOOP_HOOK
);
865 __merge_gotos(loop_name
, NULL
);
866 __split_stmt(stmt
->iterator_statement
);
868 if (!expr_is_zero(stmt
->iterator_post_condition
))
869 __save_gotos(loop_name
, NULL
);
871 if (is_forever_loop(stmt
)) {
872 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
875 __split_whole_condition(stmt
->iterator_post_condition
);
876 __use_false_states();
877 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
883 static int empty_statement(struct statement
*stmt
)
887 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
892 static int last_stmt_on_same_line(void)
894 struct statement
*stmt
;
897 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
900 if (stmt
->pos
.line
== get_lineno())
903 } END_FOR_EACH_PTR_REVERSE(stmt
);
907 static void split_asm_ops(struct asm_operand_list
*ops
)
909 struct asm_operand
*op
;
911 FOR_EACH_PTR(ops
, op
) {
912 __split_expr(op
->expr
);
913 } END_FOR_EACH_PTR(op
);
916 static int is_case_val(struct statement
*stmt
, sval_t sval
)
920 if (stmt
->type
!= STMT_CASE
)
922 if (!stmt
->case_expression
) {
926 if (!get_value(stmt
->case_expression
, &case_sval
))
928 if (case_sval
.value
== sval
.value
)
933 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
934 struct expression
*case_expr
,
935 struct expression
*case_to
)
938 struct range_list
*rl
= NULL
;
939 struct symbol
*switch_type
;
941 switch_type
= get_type(switch_expr
);
942 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
943 start
= sval_cast(switch_type
, start
);
944 end
= sval_cast(switch_type
, end
);
945 add_range(&rl
, start
, end
);
946 } else if (get_value(case_expr
, &start
)) {
947 start
= sval_cast(switch_type
, start
);
948 add_range(&rl
, start
, start
);
954 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
956 struct statement
*tmp
;
957 struct range_list
*rl
;
959 __split_expr(stmt
->switch_expression
);
960 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
962 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
963 __save_switch_states(top_expression(switch_expr_stack
));
968 stmt
= stmt
->switch_statement
;
970 __push_scope_hooks();
971 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
972 __smatch_lineno
= tmp
->pos
.line
;
973 // FIXME: what if default comes before the known case statement?
974 if (is_case_val(tmp
, sval
)) {
975 rl
= alloc_rl(sval
, sval
);
976 __merge_switches(top_expression(switch_expr_stack
), rl
);
977 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
978 stmt_set_parent_stmt(tmp
->case_statement
, tmp
);
979 __split_stmt(tmp
->case_statement
);
982 if (__path_is_null())
986 if (__path_is_null()) {
990 } END_FOR_EACH_PTR(tmp
);
992 __call_scope_hooks();
993 if (!__pop_default())
994 __merge_switches(top_expression(switch_expr_stack
), NULL
);
995 __discard_switches();
997 pop_expression(&switch_expr_stack
);
1000 static void split_case(struct statement
*stmt
)
1002 struct range_list
*rl
= NULL
;
1004 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
1005 expr_set_parent_stmt(stmt
->case_to
, stmt
);
1007 rl
= get_case_rl(top_expression(switch_expr_stack
),
1008 stmt
->case_expression
, stmt
->case_to
);
1009 while (stmt
->case_statement
->type
== STMT_CASE
) {
1010 struct range_list
*tmp
;
1012 tmp
= get_case_rl(top_expression(switch_expr_stack
),
1013 stmt
->case_statement
->case_expression
,
1014 stmt
->case_statement
->case_to
);
1017 rl
= rl_union(rl
, tmp
);
1018 if (!stmt
->case_expression
)
1021 stmt
= stmt
->case_statement
;
1024 __merge_switches(top_expression(switch_expr_stack
), rl
);
1026 if (!stmt
->case_expression
)
1029 stmt_set_parent_stmt(stmt
->case_statement
, stmt
);
1030 __split_stmt(stmt
->case_statement
);
1033 int time_parsing_function(void)
1035 return ms_since(&fn_start_time
) / 1000;
1038 bool taking_too_long(void)
1040 if ((ms_since(&outer_fn_start_time
) / 1000) > 60 * 5) /* five minutes */
1045 struct statement
*get_last_stmt(void)
1048 struct statement
*stmt
;
1050 fn
= get_base_type(cur_func_sym
);
1055 stmt
= fn
->inline_stmt
;
1056 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
1058 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
1059 if (stmt
&& stmt
->type
== STMT_LABEL
)
1060 stmt
= stmt
->label_statement
;
1064 int is_last_stmt(struct statement
*cur_stmt
)
1066 struct statement
*last
;
1068 last
= get_last_stmt();
1069 if (last
&& last
== cur_stmt
)
1074 static void handle_backward_goto(struct statement
*goto_stmt
)
1076 const char *goto_name
, *label_name
;
1077 struct statement
*func_stmt
;
1078 struct symbol
*base_type
= get_base_type(cur_func_sym
);
1079 struct statement
*tmp
;
1084 if (last_goto_statement_handled
)
1086 last_goto_statement_handled
= 1;
1088 if (!goto_stmt
->goto_label
||
1089 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
1090 !goto_stmt
->goto_label
->ident
)
1092 goto_name
= goto_stmt
->goto_label
->ident
->name
;
1094 func_stmt
= base_type
->stmt
;
1096 func_stmt
= base_type
->inline_stmt
;
1099 if (func_stmt
->type
!= STMT_COMPOUND
)
1102 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
1104 if (tmp
->type
!= STMT_LABEL
)
1106 if (!tmp
->label_identifier
||
1107 tmp
->label_identifier
->type
!= SYM_LABEL
||
1108 !tmp
->label_identifier
->ident
)
1110 label_name
= tmp
->label_identifier
->ident
->name
;
1111 if (strcmp(goto_name
, label_name
) != 0)
1116 } END_FOR_EACH_PTR(tmp
);
1119 static void fake_a_return(void)
1121 struct expression
*ret
= NULL
;
1126 if (cur_func_return_type() != &void_ctype
)
1127 ret
= unknown_value_expression(NULL
);
1129 __pass_to_client(ret
, RETURN_HOOK
);
1133 static void split_ret_value(struct expression
*expr
)
1135 struct symbol
*type
;
1140 type
= get_real_base_type(cur_func_sym
);
1141 type
= get_real_base_type(type
);
1142 expr
= fake_a_variable_assign(type
, NULL
, expr
, -1);
1144 __in_fake_var_assign
++;
1146 __in_fake_var_assign
--;
1149 static void fake_an_empty_default(struct position pos
)
1151 static struct statement none
= {};
1154 none
.type
= STMT_NONE
;
1155 __merge_switches(top_expression(switch_expr_stack
), NULL
);
1156 __split_stmt(&none
);
1159 static void split_compound(struct statement
*stmt
)
1161 struct statement
*prev
= NULL
;
1162 struct statement
*cur
= NULL
;
1163 struct statement
*next
;
1165 __push_scope_hooks();
1167 FOR_EACH_PTR(stmt
->stmts
, next
) {
1168 /* just set them all ahead of time */
1169 stmt_set_parent_stmt(next
, stmt
);
1179 } END_FOR_EACH_PTR(next
);
1188 * For function scope, then delay calling the scope hooks until the
1189 * end of function hooks can run. I'm not positive this is the right
1192 if (!is_last_stmt(cur
))
1193 __call_scope_hooks();
1197 * This is a hack, work around for detecting empty functions.
1199 static int need_delayed_scope_hooks(void)
1201 struct symbol
*fn
= get_base_type(cur_func_sym
);
1202 struct statement
*stmt
;
1208 stmt
= fn
->inline_stmt
;
1209 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
1214 void __split_label_stmt(struct statement
*stmt
)
1216 if (stmt
->label_identifier
&&
1217 stmt
->label_identifier
->type
== SYM_LABEL
&&
1218 stmt
->label_identifier
->ident
) {
1219 loop_count
|= 0x0800000;
1220 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
1224 static void find_asm_gotos(struct statement
*stmt
)
1228 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
1229 __save_gotos(sym
->ident
->name
, sym
);
1230 } END_FOR_EACH_PTR(sym
);
1233 void __split_stmt(struct statement
*stmt
)
1235 static int indent_cnt
;
1237 struct timeval start
, stop
;
1239 gettimeofday(&start
, NULL
);
1244 if (!__in_fake_assign
)
1245 __silence_warnings_for_stmt
= false;
1247 if (__bail_on_rest_of_function
|| is_skipped_function())
1250 if (out_of_memory() || taking_too_long()) {
1251 gettimeofday(&start
, NULL
);
1253 __bail_on_rest_of_function
= 1;
1255 sm_perror("Function too hairy. Giving up. %lu seconds",
1256 start
.tv_sec
- fn_start_time
.tv_sec
);
1258 final_pass
= 0; /* turn off sm_msg() from here */
1264 add_ptr_list(&big_statement_stack
, stmt
);
1265 free_expression_stack(&big_expression_stack
);
1266 set_position(stmt
->pos
);
1267 __pass_to_client(stmt
, STMT_HOOK
);
1269 switch (stmt
->type
) {
1270 case STMT_DECLARATION
:
1271 split_declaration(stmt
->declaration
);
1274 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1276 split_ret_value(stmt
->ret_value
);
1277 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1278 __process_post_op_stack();
1281 case STMT_EXPRESSION
:
1282 expr_set_parent_stmt(stmt
->expression
, stmt
);
1283 expr_set_parent_stmt(stmt
->context
, stmt
);
1285 __split_expr(stmt
->expression
);
1288 split_compound(stmt
);
1291 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1292 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1293 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1295 if (known_condition_true(stmt
->if_conditional
)) {
1296 __split_stmt(stmt
->if_true
);
1299 if (known_condition_false(stmt
->if_conditional
)) {
1300 __split_stmt(stmt
->if_false
);
1303 __split_whole_condition(stmt
->if_conditional
);
1304 __split_stmt(stmt
->if_true
);
1305 if (empty_statement(stmt
->if_true
) &&
1306 last_stmt_on_same_line() &&
1307 !get_macro_name(stmt
->if_true
->pos
))
1308 sm_warning("if();");
1309 __push_true_states();
1310 __use_false_states();
1311 __split_stmt(stmt
->if_false
);
1312 __merge_true_states();
1315 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1316 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1317 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1318 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1319 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1321 if (stmt
->iterator_pre_condition
)
1322 handle_pre_loop(stmt
);
1323 else if (stmt
->iterator_post_condition
)
1324 handle_post_loop(stmt
);
1326 // these are for(;;) type loops.
1327 handle_pre_loop(stmt
);
1331 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1332 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1334 if (get_value(stmt
->switch_expression
, &sval
)) {
1335 split_known_switch(stmt
, sval
);
1338 __split_expr(stmt
->switch_expression
);
1339 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1340 __save_switch_states(top_expression(switch_expr_stack
));
1344 __split_stmt(stmt
->switch_statement
);
1345 if (!__pop_default() && have_remaining_cases())
1346 fake_an_empty_default(stmt
->pos
);
1347 __discard_switches();
1349 pop_expression(&switch_expr_stack
);
1355 __split_label_stmt(stmt
);
1356 __split_stmt(stmt
->label_statement
);
1359 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1361 __split_expr(stmt
->goto_expression
);
1362 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1363 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1365 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1367 __process_continues();
1369 } else if (stmt
->goto_label
&&
1370 stmt
->goto_label
->type
== SYM_LABEL
&&
1371 stmt
->goto_label
->ident
) {
1372 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1375 if (is_last_stmt(stmt
))
1376 handle_backward_goto(stmt
);
1381 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1383 find_asm_gotos(stmt
);
1384 __pass_to_client(stmt
, ASM_HOOK
);
1385 __split_expr(stmt
->asm_string
);
1386 split_asm_ops(stmt
->asm_outputs
);
1387 split_asm_ops(stmt
->asm_inputs
);
1388 split_expr_list(stmt
->asm_clobbers
, NULL
);
1393 __split_expr(stmt
->range_expression
);
1394 __split_expr(stmt
->range_low
);
1395 __split_expr(stmt
->range_high
);
1398 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1399 if (--indent_cnt
== 1)
1400 __discard_fake_states(NULL
);
1403 __process_post_op_stack();
1405 gettimeofday(&stop
, NULL
);
1406 if (option_time_stmt
&& stmt
)
1407 sm_msg("stmt_time%s: %ld",
1408 stmt
->type
== STMT_COMPOUND
? "_block" : "",
1409 stop
.tv_sec
- start
.tv_sec
);
1412 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1414 struct expression
*expr
;
1416 FOR_EACH_PTR(expr_list
, expr
) {
1417 expr_set_parent_expr(expr
, parent
);
1419 __process_post_op_stack();
1420 } END_FOR_EACH_PTR(expr
);
1423 static bool cast_arg(struct symbol
*type
, struct expression
*arg
)
1425 struct symbol
*orig
;
1430 arg
= strip_parens(arg
);
1431 if (arg
!= strip_expr(arg
))
1434 orig
= get_type(arg
);
1437 if (types_equiv(orig
, type
))
1440 if (orig
->type
== SYM_ARRAY
&& type
->type
== SYM_PTR
)
1444 * I would have expected that we could just do use (orig == type) but I
1445 * guess for pointers we need to get the basetype to do that comparison.
1449 if (orig
->type
!= SYM_PTR
||
1450 type
->type
!= SYM_PTR
) {
1451 if (type_fits(type
, orig
))
1455 orig
= get_real_base_type(orig
);
1456 type
= get_real_base_type(type
);
1463 static struct expression
*fake_a_variable_assign(struct symbol
*type
, struct expression
*call
, struct expression
*expr
, int nr
)
1468 if (!expr
|| !cur_func_sym
)
1471 if (expr
->type
== EXPR_ASSIGNMENT
)
1474 /* for va_args then we don't know the type */
1476 type
= get_type(expr
);
1478 cast
= cast_arg(type
, expr
);
1480 * Using expr_to_sym() here is a hack. We want to say that we don't
1481 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1482 * It turns out faking these assignments is way more expensive than I
1483 * would have imagined. I'm not sure why exactly.
1488 * if the code is "return *p;" where "p" is a user pointer then
1489 * we want to create a fake assignment so that it sets the state
1490 * in check_kernel_user_data.c.
1493 if (expr
->type
!= EXPR_PREOP
&&
1494 expr
->op
!= '*' && expr
->op
!= '&' &&
1500 snprintf(buf
, sizeof(buf
), "__fake_return_%p", expr
);
1502 snprintf(buf
, sizeof(buf
), "__fake_param_%p_%d", call
, nr
);
1504 return create_fake_assign(buf
, type
, expr
);
1507 static void split_args(struct expression
*expr
)
1509 struct expression
*arg
, *tmp
;
1510 struct symbol
*type
;
1514 FOR_EACH_PTR(expr
->args
, arg
) {
1516 expr_set_parent_expr(arg
, expr
);
1517 type
= get_arg_type(expr
->fn
, i
);
1518 tmp
= fake_a_variable_assign(type
, expr
, arg
, i
);
1520 __in_fake_var_assign
++;
1523 __in_fake_var_assign
--;
1524 __process_post_op_stack();
1525 } END_FOR_EACH_PTR(arg
);
1528 static void split_sym(struct symbol
*sym
)
1532 if (!(sym
->namespace & NS_SYMBOL
))
1535 __split_stmt(sym
->stmt
);
1536 __split_expr(sym
->array_size
);
1537 split_symlist(sym
->arguments
);
1538 split_symlist(sym
->symbol_list
);
1539 __split_stmt(sym
->inline_stmt
);
1540 split_symlist(sym
->inline_symbol_list
);
1543 static void split_symlist(struct symbol_list
*sym_list
)
1547 FOR_EACH_PTR(sym_list
, sym
) {
1549 } END_FOR_EACH_PTR(sym
);
1552 typedef void (fake_cb
)(struct expression
*expr
);
1554 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1556 struct symbol
*type
, *tmp
;
1562 name
= member
->name
;
1564 type
= get_type(expr
);
1565 if (!type
|| type
->type
!= SYM_STRUCT
)
1569 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1573 if (strcmp(name
, tmp
->ident
->name
) == 0)
1575 } END_FOR_EACH_PTR(tmp
);
1579 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1581 struct symbol
*type
, *member
;
1584 type
= get_type(expr
);
1585 if (!type
|| type
->type
!= SYM_STRUCT
)
1588 FOR_EACH_PTR(type
->symbol_list
, member
) {
1590 return member
->ident
;
1592 } END_FOR_EACH_PTR(member
);
1596 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1598 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1600 struct expression
*edge_member
, *assign
;
1601 struct symbol
*base
= get_real_base_type(member
);
1605 expr
= member_expression(expr
, '.', member
->ident
);
1607 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1608 struct symbol
*type
;
1610 type
= get_real_base_type(tmp
);
1614 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1615 if (get_extra_state(edge_member
))
1618 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1619 set_inner_struct_members(expr
, tmp
);
1626 assign
= assign_expression(edge_member
, '=', zero_expr());
1627 __split_expr(assign
);
1628 } END_FOR_EACH_PTR(tmp
);
1633 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1636 struct expression
*member
= NULL
;
1637 struct expression
*assign
;
1639 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1640 type
= get_real_base_type(tmp
);
1645 member
= member_expression(expr
, '.', tmp
->ident
);
1646 if (get_extra_state(member
))
1650 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1651 set_inner_struct_members(expr
, tmp
);
1654 if (type
->type
== SYM_ARRAY
)
1659 assign
= assign_expression(member
, '=', zero_expr());
1660 __split_expr(assign
);
1661 } END_FOR_EACH_PTR(tmp
);
1664 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1666 struct expression
*deref
, *assign
, *tmp
, *right
;
1667 struct symbol
*struct_type
, *type
;
1668 struct ident
*member
;
1671 struct_type
= get_type(symbol
);
1673 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1677 * We're parsing an initializer that could look something like this:
1678 * struct foo foo = {
1680 * .whatever.xxx = 11,
1684 * So what we have here is a list with 42, .whatever, and .zzz. We need
1685 * to break it up into left and right sides of the assignments.
1689 FOR_EACH_PTR(members
, tmp
) {
1691 if (tmp
->type
== EXPR_IDENTIFIER
) {
1692 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1693 while (tmp
->type
== EXPR_IDENTIFIER
) {
1694 member
= tmp
->expr_ident
;
1695 tmp
= tmp
->ident_expression
;
1697 deref
= member_expression(deref
, '.', member
);
1699 deref
= member_expression(symbol
, '.', member
);
1702 member
= number_to_member(symbol
, member_idx
);
1703 deref
= member_expression(symbol
, '.', member
);
1707 if (right
->type
== EXPR_INITIALIZER
) {
1708 type
= get_type(deref
);
1709 if (type
&& type
->type
== SYM_ARRAY
)
1710 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1712 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1714 assign
= assign_expression(deref
, '=', right
);
1717 } END_FOR_EACH_PTR(tmp
);
1719 set_unset_to_zero(struct_type
, symbol
);
1722 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1724 fake_member_assigns_helper(symbol_expression(sym
),
1725 sym
->initializer
->expr_list
, fake_cb
);
1728 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1730 struct expression
*offset
, *binop
, *assign
, *tmp
;
1731 struct symbol
*type
;
1734 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1739 FOR_EACH_PTR(expr_list
, tmp
) {
1740 if (tmp
->type
== EXPR_INDEX
) {
1741 if (tmp
->idx_from
!= tmp
->idx_to
)
1743 idx
= tmp
->idx_from
;
1746 if (!tmp
->idx_expression
)
1748 tmp
= tmp
->idx_expression
;
1750 offset
= value_expr(idx
);
1751 binop
= array_element_expression(array
, offset
);
1752 if (tmp
->type
== EXPR_INITIALIZER
) {
1753 type
= get_type(binop
);
1754 if (type
&& type
->type
== SYM_ARRAY
)
1755 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1757 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1759 assign
= assign_expression(binop
, '=', tmp
);
1766 } END_FOR_EACH_PTR(tmp
);
1768 __call_array_initialized_hooks(array
, max
);
1771 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1773 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1776 static void fake_assign_expr(struct symbol
*sym
)
1778 struct expression
*assign
, *symbol
;
1780 symbol
= symbol_expression(sym
);
1781 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1782 __split_expr(assign
);
1785 static void do_initializer_stuff(struct symbol
*sym
)
1787 if (!sym
->initializer
)
1790 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1791 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1792 fake_element_assigns(sym
, __split_expr
);
1794 fake_member_assigns(sym
, __split_expr
);
1796 fake_assign_expr(sym
);
1800 static void split_declaration(struct symbol_list
*sym_list
)
1804 FOR_EACH_PTR(sym_list
, sym
) {
1805 __pass_to_client(sym
, DECLARATION_HOOK
);
1806 do_initializer_stuff(sym
);
1807 __pass_to_client(sym
, DECLARATION_HOOK_AFTER
);
1809 } END_FOR_EACH_PTR(sym
);
1812 static void call_global_assign_hooks(struct expression
*assign
)
1814 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1817 static void fake_global_assign(struct symbol
*sym
)
1819 struct expression
*assign
, *symbol
;
1821 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1822 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1823 fake_element_assigns(sym
, call_global_assign_hooks
);
1824 } else if (sym
->initializer
) {
1825 symbol
= symbol_expression(sym
);
1826 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1827 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1829 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1831 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1832 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1833 fake_member_assigns(sym
, call_global_assign_hooks
);
1834 } else if (sym
->initializer
) {
1835 symbol
= symbol_expression(sym
);
1836 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1837 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1839 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1842 symbol
= symbol_expression(sym
);
1843 if (sym
->initializer
) {
1844 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1845 __split_expr(assign
);
1847 assign
= assign_expression(symbol
, '=', zero_expr());
1849 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1853 static void start_function_definition(struct symbol
*sym
)
1855 __in_function_def
= 1;
1856 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1857 __in_function_def
= 0;
1858 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1862 void add_function_data(unsigned long *fn_data
)
1864 __add_ptr_list(&fn_data_list
, fn_data
);
1867 static void clear_function_data(void)
1871 FOR_EACH_PTR(fn_data_list
, tmp
) {
1873 } END_FOR_EACH_PTR(tmp
);
1876 static void record_func_time(void)
1878 struct timeval stop
;
1882 gettimeofday(&stop
, NULL
);
1883 func_time
= stop
.tv_sec
- fn_start_time
.tv_sec
;
1884 snprintf(buf
, sizeof(buf
), "%d", func_time
);
1885 sql_insert_return_implies(FUNC_TIME
, 0, "", buf
);
1886 if (option_time
&& func_time
> 2) {
1888 sm_msg("func_time: %d", func_time
);
1893 static void split_function(struct symbol
*sym
)
1895 struct symbol
*base_type
= get_base_type(sym
);
1897 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1900 gettimeofday(&outer_fn_start_time
, NULL
);
1901 gettimeofday(&fn_start_time
, NULL
);
1904 cur_func
= sym
->ident
->name
;
1905 set_position(sym
->pos
);
1906 clear_function_data();
1908 last_goto_statement_handled
= 0;
1909 sm_debug("new function: %s\n", cur_func
);
1911 if (option_two_passes
) {
1915 start_function_definition(sym
);
1916 __split_stmt(base_type
->stmt
);
1917 __split_stmt(base_type
->inline_stmt
);
1923 start_function_definition(sym
);
1924 __split_stmt(base_type
->stmt
);
1925 __split_stmt(base_type
->inline_stmt
);
1926 if (!__path_is_null() &&
1927 cur_func_return_type() == &void_ctype
&&
1928 !__bail_on_rest_of_function
) {
1929 __pass_to_client(NULL
, RETURN_HOOK
);
1932 __pass_to_client(sym
, END_FUNC_HOOK
);
1933 if (need_delayed_scope_hooks())
1934 __call_scope_hooks();
1935 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1942 cur_func_sym
= NULL
;
1944 free_data_info_allocs();
1945 free_expression_stack(&switch_expr_stack
);
1946 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1947 __bail_on_rest_of_function
= 0;
1950 static void save_flow_state(void)
1954 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2));
1955 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2));
1956 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2));
1958 __add_ptr_list(&backup
, big_statement_stack
);
1959 __add_ptr_list(&backup
, big_expression_stack
);
1960 __add_ptr_list(&backup
, big_condition_stack
);
1961 __add_ptr_list(&backup
, switch_expr_stack
);
1963 __add_ptr_list(&backup
, cur_func_sym
);
1965 __add_ptr_list(&backup
, __prev_stmt
);
1966 __add_ptr_list(&backup
, __cur_stmt
);
1967 __add_ptr_list(&backup
, __next_stmt
);
1969 FOR_EACH_PTR(fn_data_list
, tmp
) {
1970 __add_ptr_list(&backup
, (void *)*tmp
);
1971 } END_FOR_EACH_PTR(tmp
);
1974 static void *pop_backup(void)
1978 ret
= last_ptr_list(backup
);
1979 delete_ptr_list_last(&backup
);
1983 static void restore_flow_state(void)
1987 FOR_EACH_PTR_REVERSE(fn_data_list
, tmp
) {
1988 *tmp
= (unsigned long)pop_backup();
1989 } END_FOR_EACH_PTR_REVERSE(tmp
);
1991 __next_stmt
= pop_backup();
1992 __cur_stmt
= pop_backup();
1993 __prev_stmt
= pop_backup();
1995 cur_func_sym
= pop_backup();
1996 switch_expr_stack
= pop_backup();
1997 big_condition_stack
= pop_backup();
1998 big_expression_stack
= pop_backup();
1999 big_statement_stack
= pop_backup();
2000 final_pass
= PTR_INT(pop_backup()) >> 2;
2001 loop_count
= PTR_INT(pop_backup()) >> 2;
2002 loop_num
= PTR_INT(pop_backup()) >> 2;
2005 static void parse_inline(struct expression
*call
)
2007 struct symbol
*base_type
;
2008 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
2009 struct timeval time_backup
= fn_start_time
;
2010 struct expression
*orig_inline
= __inline_fn
;
2013 if (out_of_memory() || taking_too_long())
2018 __pass_to_client(call
, INLINE_FN_START
);
2019 final_pass
= 0; /* don't print anything */
2021 orig_budget
= inline_budget
;
2022 inline_budget
= inline_budget
- 5;
2024 base_type
= get_base_type(call
->fn
->symbol
);
2025 cur_func_sym
= call
->fn
->symbol
;
2026 if (call
->fn
->symbol
->ident
)
2027 cur_func
= call
->fn
->symbol
->ident
->name
;
2030 set_position(call
->fn
->symbol
->pos
);
2033 big_statement_stack
= NULL
;
2034 big_expression_stack
= NULL
;
2035 big_condition_stack
= NULL
;
2036 switch_expr_stack
= NULL
;
2038 sm_debug("inline function: %s\n", cur_func
);
2040 clear_function_data();
2043 start_function_definition(call
->fn
->symbol
);
2044 __split_stmt(base_type
->stmt
);
2045 __split_stmt(base_type
->inline_stmt
);
2046 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
2047 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
2048 call
->fn
->symbol
->parsed
= true;
2050 free_expression_stack(&switch_expr_stack
);
2051 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
2055 restore_flow_state();
2056 fn_start_time
= time_backup
;
2057 cur_func
= cur_func_bak
;
2059 restore_all_states();
2060 set_position(call
->pos
);
2061 __inline_fn
= orig_inline
;
2062 inline_budget
= orig_budget
;
2063 __pass_to_client(call
, INLINE_FN_END
);
2066 static struct symbol_list
*inlines_called
;
2067 static void add_inline_function(struct symbol
*sym
)
2069 static struct symbol_list
*already_added
;
2072 FOR_EACH_PTR(already_added
, tmp
) {
2075 } END_FOR_EACH_PTR(tmp
);
2077 add_ptr_list(&already_added
, sym
);
2078 add_ptr_list(&inlines_called
, sym
);
2081 static void process_inlines(void)
2085 FOR_EACH_PTR(inlines_called
, tmp
) {
2086 split_function(tmp
);
2087 } END_FOR_EACH_PTR(tmp
);
2088 free_ptr_list(&inlines_called
);
2091 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
2095 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
2098 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
2100 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
2102 } END_FOR_EACH_PTR_REVERSE(sym
);
2107 static bool interesting_function(struct symbol
*sym
)
2109 static int prev_stream
= -1;
2110 static bool prev_answer
;
2111 const char *filename
;
2114 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
2117 if (sym
->pos
.stream
== prev_stream
)
2120 prev_stream
= sym
->pos
.stream
;
2121 prev_answer
= false;
2123 filename
= stream_name(sym
->pos
.stream
);
2124 len
= strlen(filename
);
2125 if (len
> 0 && filename
[len
- 1] == 'c')
2130 static void split_inlines_in_scope(struct symbol
*sym
)
2132 struct symbol
*base
;
2133 struct symbol_list
*scope_list
;
2136 scope_list
= sym
->scope
->symbols
;
2137 stream
= sym
->pos
.stream
;
2139 /* find the last static symbol in the file */
2140 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
2141 if (sym
->pos
.stream
!= stream
)
2143 if (sym
->type
!= SYM_NODE
)
2145 base
= get_base_type(sym
);
2148 if (base
->type
!= SYM_FN
)
2150 if (!base
->inline_stmt
)
2152 if (!interesting_function(sym
))
2154 add_inline_function(sym
);
2155 } END_FOR_EACH_PTR_REVERSE(sym
);
2160 static void split_inlines(struct symbol_list
*sym_list
)
2164 sym
= get_last_scoped_symbol(sym_list
, 0);
2166 split_inlines_in_scope(sym
);
2167 sym
= get_last_scoped_symbol(sym_list
, 1);
2169 split_inlines_in_scope(sym
);
2172 static struct stree
*clone_estates_perm(struct stree
*orig
)
2174 struct stree
*ret
= NULL
;
2175 struct sm_state
*tmp
;
2177 FOR_EACH_SM(orig
, tmp
) {
2178 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
2179 } END_FOR_EACH_SM(tmp
);
2184 struct position last_pos
;
2185 static void split_c_file_functions(struct symbol_list
*sym_list
)
2190 FOR_EACH_PTR(sym_list
, sym
) {
2191 set_position(sym
->pos
);
2192 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
2193 __pass_to_client(sym
, BASE_HOOK
);
2194 fake_global_assign(sym
);
2195 __pass_to_client(sym
, DECLARATION_HOOK_AFTER
);
2197 } END_FOR_EACH_PTR(sym
);
2198 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
2201 FOR_EACH_PTR(sym_list
, sym
) {
2202 set_position(sym
->pos
);
2203 last_pos
= sym
->pos
;
2204 if (!interesting_function(sym
))
2206 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
2207 split_function(sym
);
2210 last_pos
= sym
->pos
;
2211 } END_FOR_EACH_PTR(sym
);
2212 split_inlines(sym_list
);
2213 __pass_to_client(sym_list
, END_FILE_HOOK
);
2216 static int final_before_fake
;
2217 void init_fake_env(void)
2220 final_before_fake
= final_pass
;
2222 __push_fake_cur_stree();
2226 void end_fake_env(void)
2228 __free_fake_cur_stree();
2231 final_pass
= final_before_fake
;
2234 static void open_output_files(char *base_file
)
2238 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
2239 sm_outfd
= fopen(buf
, "w");
2241 sm_fatal("Cannot open %s", buf
);
2246 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
2247 sql_outfd
= fopen(buf
, "w");
2249 sm_fatal("Error: Cannot open %s", buf
);
2251 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
2252 caller_info_fd
= fopen(buf
, "w");
2253 if (!caller_info_fd
)
2254 sm_fatal("Error: Cannot open %s", buf
);
2257 void smatch(struct string_list
*filelist
)
2259 struct symbol_list
*sym_list
;
2260 struct timeval stop
, start
;
2264 gettimeofday(&start
, NULL
);
2266 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
2267 path
= getcwd(NULL
, 0);
2268 free(full_base_file
);
2270 len
= strlen(path
) + 1 + strlen(base_file
) + 1;
2271 full_base_file
= malloc(len
);
2272 snprintf(full_base_file
, len
, "%s/%s", path
, base_file
);
2274 full_base_file
= alloc_string(base_file
);
2276 if (option_file_output
)
2277 open_output_files(base_file
);
2278 base_file_stream
= input_stream_nr
;
2279 sym_list
= sparse_keep_tokens(base_file
);
2280 split_c_file_functions(sym_list
);
2281 } END_FOR_EACH_PTR_NOTAG(base_file
);
2283 gettimeofday(&stop
, NULL
);
2285 set_position(last_pos
);
2288 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
2290 sm_msg("mem: %luKb", get_max_memory());