2 * Copyright (C) 2006,2008 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
24 #include "smatch_expression_stacks.h"
25 #include "smatch_extra.h"
26 #include "smatch_slist.h"
29 int __in_fake_struct_assign
;
31 int __in_fake_var_assign
;
36 struct expression
*__inline_fn
;
38 int __smatch_lineno
= 0;
40 static char *base_file
;
41 static const char *filename
;
42 static char *pathname
;
43 static char *full_filename
;
44 static char *full_base_file
;
45 static char *cur_func
;
47 static unsigned int loop_count
;
48 static int last_goto_statement_handled
;
49 int __expr_stmt_count
;
50 int __in_function_def
;
51 int __in_unmatched_hook
;
52 static struct expression_list
*switch_expr_stack
= NULL
;
53 static struct expression_list
*post_op_stack
= NULL
;
55 static struct ptr_list
*fn_data_list
;
56 static struct ptr_list
*backup
;
58 struct expression_list
*big_expression_stack
;
59 struct statement_list
*big_statement_stack
;
60 struct statement
*__prev_stmt
;
61 struct statement
*__cur_stmt
;
62 struct statement
*__next_stmt
;
63 int __in_pre_condition
= 0;
64 int __bail_on_rest_of_function
= 0;
65 static struct timeval fn_start_time
;
66 static struct timeval outer_fn_start_time
;
67 char *get_function(void) { return cur_func
; }
68 int get_lineno(void) { return __smatch_lineno
; }
69 int inside_loop(void) { return !!loop_count
; }
70 int definitely_inside_loop(void) { return !!(loop_count
& ~0x08000000); }
71 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
72 int in_expression_statement(void) { return !!__expr_stmt_count
; }
74 static void split_symlist(struct symbol_list
*sym_list
);
75 static void split_declaration(struct symbol_list
*sym_list
);
76 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
);
77 static void split_args(struct expression
*expr
);
78 static struct expression
*fake_a_variable_assign(struct symbol
*type
, struct expression
*call
, struct expression
*expr
, int nr
);
79 static void add_inline_function(struct symbol
*sym
);
80 static void parse_inline(struct expression
*expr
);
82 int option_assume_loops
= 0;
83 int option_two_passes
= 0;
84 struct symbol
*cur_func_sym
= NULL
;
85 struct stree
*global_states
;
87 const unsigned long valid_ptr_min
= 4096;
88 unsigned long valid_ptr_max
= ULONG_MAX
& ~(MTAG_OFFSET_MASK
);
89 const sval_t valid_ptr_min_sval
= {
93 sval_t valid_ptr_max_sval
= {
95 {.value
= ULONG_MAX
& ~(MTAG_OFFSET_MASK
)},
97 struct range_list
*valid_ptr_rl
;
99 void alloc_valid_ptr_rl(void)
101 valid_ptr_max
= sval_type_max(&ulong_ctype
).value
& ~(MTAG_OFFSET_MASK
);
102 valid_ptr_max_sval
.value
= valid_ptr_max
;
104 valid_ptr_rl
= alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
105 valid_ptr_rl
= cast_rl(&ptr_ctype
, valid_ptr_rl
);
106 valid_ptr_rl
= clone_rl_permanent(valid_ptr_rl
);
109 int outside_of_function(void)
111 return cur_func_sym
== NULL
;
114 const char *get_filename(void)
116 if (option_info
&& option_full_path
)
117 return full_base_file
;
120 if (option_full_path
)
121 return full_filename
;
125 const char *get_base_file(void)
127 if (option_full_path
)
128 return full_base_file
;
132 unsigned long long get_file_id(void)
134 return str_to_llu_hash(get_filename());
137 unsigned long long get_base_file_id(void)
139 return str_to_llu_hash(get_base_file());
142 static void set_position(struct position pos
)
145 static int prev_stream
= -1;
150 if (pos
.stream
== 0 && pos
.line
== 0)
153 __smatch_lineno
= pos
.line
;
155 if (pos
.stream
== prev_stream
)
158 filename
= stream_name(pos
.stream
);
161 pathname
= getcwd(NULL
, 0);
163 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
164 full_filename
= malloc(len
);
165 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
167 full_filename
= alloc_string(filename
);
172 int is_assigned_call(struct expression
*expr
)
174 struct expression
*parent
= expr_get_parent_expr(expr
);
177 parent
->type
== EXPR_ASSIGNMENT
&&
179 strip_expr(parent
->right
) == expr
)
185 int is_fake_assigned_call(struct expression
*expr
)
187 struct expression
*parent
= expr_get_fake_parent_expr(expr
);
190 parent
->type
== EXPR_ASSIGNMENT
&&
192 strip_expr(parent
->right
) == expr
)
198 static bool is_inline_func(struct expression
*expr
)
200 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
202 if (!expr
->symbol
->definition
)
204 if (expr
->symbol
->definition
->ctype
.modifiers
& MOD_INLINE
)
210 static int is_noreturn_func(struct expression
*expr
)
212 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
216 * It's almost impossible for Smatch to handle __builtin_constant_p()
217 * the same way that GCC does so Smatch ends up making some functions
218 * as no return functions incorrectly.
221 if (option_project
== PROJ_KERNEL
&& expr
->symbol
->ident
&&
222 strstr(expr
->symbol
->ident
->name
, "__compiletime_assert"))
225 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
230 static int save_func_time(void *_rl
, int argc
, char **argv
, char **azColName
)
232 unsigned long *rl
= _rl
;
234 *rl
= strtoul(argv
[0], NULL
, 10);
238 static int get_func_time(struct symbol
*sym
)
240 unsigned long time
= 0;
242 run_sql(&save_func_time
, &time
,
243 "select key from return_implies where %s and type = %d;",
244 get_static_filter(sym
), FUNC_TIME
);
249 static int inline_budget
= 20;
251 int inlinable(struct expression
*expr
)
254 struct statement
*last_stmt
= NULL
;
256 if (__inline_fn
) /* don't nest */
259 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
261 if (is_no_inline_function(expr
->symbol
->ident
->name
))
263 sym
= get_base_type(expr
->symbol
);
264 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
265 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
267 if (sym
->stmt
->type
!= STMT_COMPOUND
)
269 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
271 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
272 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
274 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
276 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
282 /* the magic numbers in this function are pulled out of my bum. */
283 if (last_stmt
->pos
.line
> sym
->pos
.line
+ inline_budget
)
286 if (get_func_time(expr
->symbol
) >= 2)
292 void __process_post_op_stack(void)
294 struct expression
*expr
;
296 FOR_EACH_PTR(post_op_stack
, expr
) {
297 __pass_to_client(expr
, OP_HOOK
);
298 } END_FOR_EACH_PTR(expr
);
300 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
303 static int handle_comma_assigns(struct expression
*expr
)
305 struct expression
*right
;
306 struct expression
*assign
;
308 right
= strip_expr(expr
->right
);
309 if (right
->type
!= EXPR_COMMA
)
312 __split_expr(right
->left
);
313 __process_post_op_stack();
315 assign
= assign_expression(expr
->left
, '=', right
->right
);
316 __split_expr(assign
);
321 /* This is to handle *p++ = foo; assignments */
322 static int handle_postop_assigns(struct expression
*expr
)
324 struct expression
*left
, *fake_left
;
325 struct expression
*assign
;
327 left
= strip_expr(expr
->left
);
328 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '*')
330 left
= strip_expr(left
->unop
);
331 if (left
->type
!= EXPR_POSTOP
)
334 fake_left
= deref_expression(strip_expr(left
->unop
));
335 assign
= assign_expression(fake_left
, '=', expr
->right
);
337 __split_expr(assign
);
338 __split_expr(expr
->left
);
343 static int prev_expression_is_getting_address(struct expression
*expr
)
345 struct expression
*parent
;
348 parent
= expr_get_parent_expr(expr
);
352 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
354 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
356 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
358 /* Handle &foo->array[offset] */
359 if (parent
->type
== EXPR_BINOP
&& parent
->op
== '+') {
360 parent
= expr_get_parent_expr(parent
);
363 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '*')
373 int __in_builtin_overflow_func
;
374 static void handle_builtin_overflow_func(struct expression
*expr
)
376 struct expression
*a
, *b
, *res
, *assign
;
379 if (sym_name_is("__builtin_add_overflow", expr
->fn
))
381 else if (sym_name_is("__builtin_sub_overflow", expr
->fn
))
383 else if (sym_name_is("__builtin_mul_overflow", expr
->fn
))
388 a
= get_argument_from_call_expr(expr
->args
, 0);
389 b
= get_argument_from_call_expr(expr
->args
, 1);
390 res
= get_argument_from_call_expr(expr
->args
, 2);
392 assign
= assign_expression(deref_expression(res
), '=', binop_expression(a
, op
, b
));
394 __in_builtin_overflow_func
++;
395 __split_expr(assign
);
396 __in_builtin_overflow_func
--;
399 static int handle__builtin_choose_expr(struct expression
*expr
)
401 struct expression
*const_expr
, *expr1
, *expr2
;
404 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
407 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
408 expr1
= get_argument_from_call_expr(expr
->args
, 1);
409 expr2
= get_argument_from_call_expr(expr
->args
, 2);
411 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
420 static int handle__builtin_choose_expr_assigns(struct expression
*expr
)
422 struct expression
*const_expr
, *right
, *expr1
, *expr2
, *fake
;
425 right
= strip_parens(expr
->right
);
426 if (right
->type
!= EXPR_CALL
)
428 if (!sym_name_is("__builtin_choose_expr", right
->fn
))
431 const_expr
= get_argument_from_call_expr(right
->args
, 0);
432 expr1
= get_argument_from_call_expr(right
->args
, 1);
433 expr2
= get_argument_from_call_expr(right
->args
, 2);
435 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
438 fake
= assign_expression(expr
->left
, '=', sval
.value
? expr1
: expr2
);
443 int is_condition_call(struct expression
*expr
)
445 struct expression
*tmp
;
447 FOR_EACH_PTR_REVERSE(big_condition_stack
, tmp
) {
448 if (expr
== tmp
|| expr_get_parent_expr(expr
) == tmp
)
450 if (tmp
->pos
.line
< expr
->pos
.line
)
452 } END_FOR_EACH_PTR_REVERSE(tmp
);
457 static struct expression
*expr_get_parent_no_parens(struct expression
*expr
)
460 expr
= expr_get_parent_expr(expr
);
462 expr
->type
== EXPR_PREOP
&&
468 static bool gen_fake_function_assign(struct expression
*expr
)
470 static struct expression
*parsed
;
471 struct expression
*assign
, *parent
;
475 /* The rule is that every non-void function call has to be part of an
476 * assignment. TODO: Should we create a fake non-casted assignment
477 * for casted assignments? Also faked assigns for += assignments?
479 type
= get_type(expr
);
480 if (!type
|| type
== &void_ctype
)
483 parent
= expr_get_parent_no_parens(expr
);
484 if (parent
&& parent
->type
== EXPR_ASSIGNMENT
)
487 parent
= expr_get_fake_parent_expr(expr
);
489 struct expression
*left
= parent
->left
;
491 if (parent
== parsed
)
493 if (!left
|| left
->type
!= EXPR_SYMBOL
)
495 if (strncmp(left
->symbol_name
->name
, "__fake_assign_", 14) != 0)
498 __split_expr(parent
);
502 // TODO: faked_assign skipping conditions is a hack
503 if (is_condition_call(expr
))
506 snprintf(buf
, sizeof(buf
), "__fake_assign_%p", expr
);
507 assign
= create_fake_assign(buf
, get_type(expr
), expr
);
510 __split_expr(assign
);
514 static void split_call(struct expression
*expr
)
516 if (gen_fake_function_assign(expr
))
519 expr_set_parent_expr(expr
->fn
, expr
);
521 if (sym_name_is("__builtin_constant_p", expr
->fn
))
523 if (handle__builtin_choose_expr(expr
))
525 __split_expr(expr
->fn
);
527 if (is_inline_func(expr
->fn
))
528 add_inline_function(expr
->fn
->symbol
->definition
);
529 if (inlinable(expr
->fn
))
531 __process_post_op_stack();
532 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
533 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
535 if (inlinable(expr
->fn
))
537 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
538 if (is_noreturn_func(expr
->fn
))
540 if (!expr_get_parent_expr(expr
))
541 __discard_fake_states(expr
);
542 handle_builtin_overflow_func(expr
);
545 void parse_assignment(struct expression
*expr
)
547 struct expression
*right
;
549 expr_set_parent_expr(expr
->left
, expr
);
550 expr_set_parent_expr(expr
->right
, expr
);
552 right
= strip_expr(expr
->right
);
556 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
559 if (__handle_condition_assigns(expr
))
561 /* foo = (x < 5 ? foo : 5); */
562 if (__handle_select_assigns(expr
))
564 /* foo = ({frob(); frob(); frob(); 1;}) */
565 if (__handle_expr_statement_assigns(expr
))
566 return; // FIXME: got after
568 if (handle_comma_assigns(expr
))
570 if (handle__builtin_choose_expr_assigns(expr
))
572 if (handle_postop_assigns(expr
))
573 return; /* no need to goto after_assign */
575 __split_expr(expr
->right
);
576 if (outside_of_function())
577 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
579 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
582 // FIXME: the ordering of this is tricky
583 __fake_struct_member_assignments(expr
);
585 /* Re-examine ->right for inlines. See the commit message */
586 right
= strip_expr(expr
->right
);
587 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
588 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
591 if (get_macro_name(right
->pos
) &&
592 get_macro_name(expr
->pos
) != get_macro_name(right
->pos
))
593 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
595 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
596 __split_expr(expr
->left
);
599 void __split_expr(struct expression
*expr
)
605 // sm_msg("Debug expr_type %d %s expr = '%s'", expr->type, show_special(expr->op), expr_to_str(expr));
607 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
609 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
612 push_expression(&big_expression_stack
, expr
);
613 set_position(expr
->pos
);
614 __pass_to_client(expr
, EXPR_HOOK
);
616 switch (expr
->type
) {
618 expr_set_parent_expr(expr
->unop
, expr
);
620 if (expr
->op
== '*' &&
621 !prev_expression_is_getting_address(expr
))
622 __pass_to_client(expr
, DEREF_HOOK
);
623 __split_expr(expr
->unop
);
624 __pass_to_client(expr
, OP_HOOK
);
627 expr_set_parent_expr(expr
->unop
, expr
);
629 __split_expr(expr
->unop
);
630 push_expression(&post_op_stack
, expr
);
634 if (expr
->statement
&& !expr
->statement
) {
635 stmt_set_parent_stmt(expr
->statement
,
636 last_ptr_list((struct ptr_list
*)big_statement_stack
));
638 __split_stmt(expr
->statement
);
643 expr_set_parent_expr(expr
->left
, expr
);
644 expr_set_parent_expr(expr
->right
, expr
);
646 __pass_to_client(expr
, LOGIC_HOOK
);
647 __handle_logic(expr
);
650 expr_set_parent_expr(expr
->left
, expr
);
651 expr_set_parent_expr(expr
->right
, expr
);
653 __pass_to_client(expr
, BINOP_HOOK
);
654 __split_expr(expr
->left
);
655 __split_expr(expr
->right
);
658 expr_set_parent_expr(expr
->left
, expr
);
659 expr_set_parent_expr(expr
->right
, expr
);
661 __split_expr(expr
->left
);
662 __process_post_op_stack();
663 __split_expr(expr
->right
);
665 case EXPR_ASSIGNMENT
:
666 parse_assignment(expr
);
669 expr_set_parent_expr(expr
->deref
, expr
);
671 __pass_to_client(expr
, DEREF_HOOK
);
672 __split_expr(expr
->deref
);
675 expr_set_parent_expr(expr
->base
, expr
);
677 __split_expr(expr
->base
);
680 case EXPR_FORCE_CAST
:
681 expr_set_parent_expr(expr
->cast_expression
, expr
);
683 __pass_to_client(expr
, CAST_HOOK
);
684 __split_expr(expr
->cast_expression
);
687 if (expr
->cast_expression
)
688 __pass_to_client(strip_parens(expr
->cast_expression
),
694 case EXPR_CONDITIONAL
:
696 expr_set_parent_expr(expr
->conditional
, expr
);
697 expr_set_parent_expr(expr
->cond_true
, expr
);
698 expr_set_parent_expr(expr
->cond_false
, expr
);
700 if (known_condition_true(expr
->conditional
)) {
701 __split_expr(expr
->cond_true
);
704 if (known_condition_false(expr
->conditional
)) {
705 __split_expr(expr
->cond_false
);
708 __pass_to_client(expr
, SELECT_HOOK
);
709 __split_whole_condition(expr
->conditional
);
710 __split_expr(expr
->cond_true
);
711 __push_true_states();
712 __use_false_states();
713 __split_expr(expr
->cond_false
);
714 __merge_true_states();
719 case EXPR_INITIALIZER
:
720 split_expr_list(expr
->expr_list
, expr
);
722 case EXPR_IDENTIFIER
:
723 expr_set_parent_expr(expr
->ident_expression
, expr
);
724 __split_expr(expr
->ident_expression
);
727 expr_set_parent_expr(expr
->idx_expression
, expr
);
728 __split_expr(expr
->idx_expression
);
731 expr_set_parent_expr(expr
->init_expr
, expr
);
732 __split_expr(expr
->init_expr
);
735 __pass_to_client(expr
, SYM_HOOK
);
738 __pass_to_client(expr
, STRING_HOOK
);
741 struct expression
*tmp
;
743 tmp
= strip_Generic(expr
);
751 __pass_to_client(expr
, EXPR_HOOK_AFTER
);
752 pop_expression(&big_expression_stack
);
755 static int is_forever_loop(struct statement
*stmt
)
757 struct expression
*expr
;
760 expr
= strip_expr(stmt
->iterator_pre_condition
);
762 expr
= stmt
->iterator_post_condition
;
764 /* this is a for(;;) loop... */
768 if (get_value(expr
, &sval
) && sval
.value
!= 0)
775 static char *get_loop_name(int num
)
779 snprintf(buf
, 255, "-loop%d", num
);
781 return alloc_sname(buf
);
785 * Pre Loops are while and for loops.
787 static void handle_pre_loop(struct statement
*stmt
)
789 int once_through
; /* we go through the loop at least once */
790 struct sm_state
*extra_sm
= NULL
;
793 struct stree
*stree
= NULL
;
794 struct sm_state
*sm
= NULL
;
796 loop_name
= get_loop_name(loop_num
);
799 if (stmt
->iterator_pre_statement
) {
800 __split_stmt(stmt
->iterator_pre_statement
);
801 __prev_stmt
= stmt
->iterator_pre_statement
;
804 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
810 __merge_gotos(loop_name
, NULL
);
812 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
813 __in_pre_condition
++;
814 __pass_to_client(stmt
, PRELOOP_HOOK
);
815 __split_whole_condition(stmt
->iterator_pre_condition
);
816 __in_pre_condition
--;
817 FOR_EACH_SM(stree
, sm
) {
818 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
819 } END_FOR_EACH_SM(sm
);
822 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
824 if (option_assume_loops
)
827 __split_stmt(stmt
->iterator_statement
);
828 if (is_forever_loop(stmt
)) {
830 __save_gotos(loop_name
, NULL
);
832 __push_fake_cur_stree();
833 __split_stmt(stmt
->iterator_post_statement
);
834 stree
= __pop_fake_cur_stree();
836 __discard_false_states();
837 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
840 if (!__path_is_null())
841 __merge_stree_into_cur(stree
);
845 unchanged
= __iterator_unchanged(extra_sm
);
846 __split_stmt(stmt
->iterator_post_statement
);
847 __prev_stmt
= stmt
->iterator_post_statement
;
850 __save_gotos(loop_name
, NULL
);
851 __in_pre_condition
++;
852 __split_whole_condition(stmt
->iterator_pre_condition
);
853 __in_pre_condition
--;
855 __merge_false_states();
857 __discard_false_states();
859 __merge_false_states();
861 if (extra_sm
&& unchanged
)
862 __extra_pre_loop_hook_after(extra_sm
,
863 stmt
->iterator_post_statement
,
864 stmt
->iterator_pre_condition
);
865 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
872 * Post loops are do {} while();
874 static void handle_post_loop(struct statement
*stmt
)
878 loop_name
= get_loop_name(loop_num
);
882 __pass_to_client(stmt
, POSTLOOP_HOOK
);
886 __merge_gotos(loop_name
, NULL
);
887 __split_stmt(stmt
->iterator_statement
);
889 if (!expr_is_zero(stmt
->iterator_post_condition
))
890 __save_gotos(loop_name
, NULL
);
892 if (is_forever_loop(stmt
)) {
893 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
896 __split_whole_condition(stmt
->iterator_post_condition
);
897 __use_false_states();
898 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
904 static int empty_statement(struct statement
*stmt
)
908 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
913 static int last_stmt_on_same_line(void)
915 struct statement
*stmt
;
918 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
921 if (stmt
->pos
.line
== get_lineno())
924 } END_FOR_EACH_PTR_REVERSE(stmt
);
928 static void split_asm_ops(struct asm_operand_list
*ops
)
930 struct asm_operand
*op
;
932 FOR_EACH_PTR(ops
, op
) {
933 __split_expr(op
->expr
);
934 } END_FOR_EACH_PTR(op
);
937 static int is_case_val(struct statement
*stmt
, sval_t sval
)
941 if (stmt
->type
!= STMT_CASE
)
943 if (!stmt
->case_expression
) {
947 if (!get_value(stmt
->case_expression
, &case_sval
))
949 if (case_sval
.value
== sval
.value
)
954 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
955 struct expression
*case_expr
,
956 struct expression
*case_to
)
959 struct range_list
*rl
= NULL
;
960 struct symbol
*switch_type
;
962 switch_type
= get_type(switch_expr
);
963 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
964 start
= sval_cast(switch_type
, start
);
965 end
= sval_cast(switch_type
, end
);
966 add_range(&rl
, start
, end
);
967 } else if (get_value(case_expr
, &start
)) {
968 start
= sval_cast(switch_type
, start
);
969 add_range(&rl
, start
, start
);
975 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
977 struct statement
*tmp
;
978 struct range_list
*rl
;
980 __split_expr(stmt
->switch_expression
);
981 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
983 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
984 __save_switch_states(top_expression(switch_expr_stack
));
989 stmt
= stmt
->switch_statement
;
991 __push_scope_hooks();
992 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
993 __smatch_lineno
= tmp
->pos
.line
;
994 // FIXME: what if default comes before the known case statement?
995 if (is_case_val(tmp
, sval
)) {
996 rl
= alloc_rl(sval
, sval
);
997 __merge_switches(top_expression(switch_expr_stack
), rl
);
998 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
999 stmt_set_parent_stmt(tmp
->case_statement
, tmp
);
1000 __split_stmt(tmp
->case_statement
);
1003 if (__path_is_null())
1007 if (__path_is_null()) {
1011 } END_FOR_EACH_PTR(tmp
);
1013 __call_scope_hooks();
1014 if (!__pop_default())
1015 __merge_switches(top_expression(switch_expr_stack
), NULL
);
1016 __discard_switches();
1018 pop_expression(&switch_expr_stack
);
1021 static void split_case(struct statement
*stmt
)
1023 struct range_list
*rl
= NULL
;
1025 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
1026 expr_set_parent_stmt(stmt
->case_to
, stmt
);
1028 rl
= get_case_rl(top_expression(switch_expr_stack
),
1029 stmt
->case_expression
, stmt
->case_to
);
1030 while (stmt
->case_statement
->type
== STMT_CASE
) {
1031 struct range_list
*tmp
;
1033 tmp
= get_case_rl(top_expression(switch_expr_stack
),
1034 stmt
->case_statement
->case_expression
,
1035 stmt
->case_statement
->case_to
);
1038 rl
= rl_union(rl
, tmp
);
1039 if (!stmt
->case_expression
)
1042 stmt
= stmt
->case_statement
;
1045 __merge_switches(top_expression(switch_expr_stack
), rl
);
1047 if (!stmt
->case_expression
)
1050 stmt_set_parent_stmt(stmt
->case_statement
, stmt
);
1051 __split_stmt(stmt
->case_statement
);
1054 int time_parsing_function(void)
1056 return ms_since(&fn_start_time
) / 1000;
1059 bool taking_too_long(void)
1061 if ((ms_since(&outer_fn_start_time
) / 1000) > 60 * 5) /* five minutes */
1066 struct statement
*get_last_stmt(void)
1069 struct statement
*stmt
;
1071 fn
= get_base_type(cur_func_sym
);
1076 stmt
= fn
->inline_stmt
;
1077 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
1079 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
1080 if (stmt
&& stmt
->type
== STMT_LABEL
)
1081 stmt
= stmt
->label_statement
;
1085 int is_last_stmt(struct statement
*cur_stmt
)
1087 struct statement
*last
;
1089 last
= get_last_stmt();
1090 if (last
&& last
== cur_stmt
)
1095 static void handle_backward_goto(struct statement
*goto_stmt
)
1097 const char *goto_name
, *label_name
;
1098 struct statement
*func_stmt
;
1099 struct symbol
*base_type
= get_base_type(cur_func_sym
);
1100 struct statement
*tmp
;
1105 if (last_goto_statement_handled
)
1107 last_goto_statement_handled
= 1;
1109 if (!goto_stmt
->goto_label
||
1110 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
1111 !goto_stmt
->goto_label
->ident
)
1113 goto_name
= goto_stmt
->goto_label
->ident
->name
;
1115 func_stmt
= base_type
->stmt
;
1117 func_stmt
= base_type
->inline_stmt
;
1120 if (func_stmt
->type
!= STMT_COMPOUND
)
1123 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
1125 if (tmp
->type
!= STMT_LABEL
)
1127 if (!tmp
->label_identifier
||
1128 tmp
->label_identifier
->type
!= SYM_LABEL
||
1129 !tmp
->label_identifier
->ident
)
1131 label_name
= tmp
->label_identifier
->ident
->name
;
1132 if (strcmp(goto_name
, label_name
) != 0)
1137 } END_FOR_EACH_PTR(tmp
);
1140 static void fake_a_return(void)
1142 struct expression
*ret
= NULL
;
1147 if (cur_func_return_type() != &void_ctype
)
1148 ret
= unknown_value_expression(NULL
);
1150 __pass_to_client(ret
, RETURN_HOOK
);
1154 static void split_ret_value(struct expression
*expr
)
1156 struct symbol
*type
;
1161 type
= get_real_base_type(cur_func_sym
);
1162 type
= get_real_base_type(type
);
1163 expr
= fake_a_variable_assign(type
, NULL
, expr
, -1);
1165 __in_fake_var_assign
++;
1167 __in_fake_var_assign
--;
1170 static void fake_an_empty_default(struct position pos
)
1172 static struct statement none
= {};
1175 none
.type
= STMT_NONE
;
1176 __merge_switches(top_expression(switch_expr_stack
), NULL
);
1177 __split_stmt(&none
);
1180 static void split_compound(struct statement
*stmt
)
1182 struct statement
*prev
= NULL
;
1183 struct statement
*cur
= NULL
;
1184 struct statement
*next
;
1186 __push_scope_hooks();
1188 FOR_EACH_PTR(stmt
->stmts
, next
) {
1189 /* just set them all ahead of time */
1190 stmt_set_parent_stmt(next
, stmt
);
1200 } END_FOR_EACH_PTR(next
);
1209 * For function scope, then delay calling the scope hooks until the
1210 * end of function hooks can run. I'm not positive this is the right
1213 if (!is_last_stmt(cur
))
1214 __call_scope_hooks();
1218 * This is a hack, work around for detecting empty functions.
1220 static int need_delayed_scope_hooks(void)
1222 struct symbol
*fn
= get_base_type(cur_func_sym
);
1223 struct statement
*stmt
;
1229 stmt
= fn
->inline_stmt
;
1230 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
1235 void __split_label_stmt(struct statement
*stmt
)
1237 if (stmt
->label_identifier
&&
1238 stmt
->label_identifier
->type
== SYM_LABEL
&&
1239 stmt
->label_identifier
->ident
) {
1240 loop_count
|= 0x0800000;
1241 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
1245 static void find_asm_gotos(struct statement
*stmt
)
1249 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
1250 __save_gotos(sym
->ident
->name
, sym
);
1251 } END_FOR_EACH_PTR(sym
);
1254 void __split_stmt(struct statement
*stmt
)
1256 static int indent_cnt
;
1258 struct timeval start
, stop
;
1259 bool skip_after
= false;
1261 gettimeofday(&start
, NULL
);
1266 if (!__in_fake_assign
)
1267 __silence_warnings_for_stmt
= false;
1269 if (__bail_on_rest_of_function
|| is_skipped_function())
1272 if (out_of_memory() || taking_too_long()) {
1273 gettimeofday(&start
, NULL
);
1275 __bail_on_rest_of_function
= 1;
1277 sm_perror("Function too hairy. Giving up. %lu seconds",
1278 start
.tv_sec
- fn_start_time
.tv_sec
);
1280 final_pass
= 0; /* turn off sm_msg() from here */
1286 add_ptr_list(&big_statement_stack
, stmt
);
1287 free_expression_stack(&big_expression_stack
);
1288 set_position(stmt
->pos
);
1289 __pass_to_client(stmt
, STMT_HOOK
);
1291 switch (stmt
->type
) {
1292 case STMT_DECLARATION
:
1293 split_declaration(stmt
->declaration
);
1296 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1298 split_ret_value(stmt
->ret_value
);
1299 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1300 __process_post_op_stack();
1303 case STMT_EXPRESSION
:
1304 expr_set_parent_stmt(stmt
->expression
, stmt
);
1305 expr_set_parent_stmt(stmt
->context
, stmt
);
1307 __split_expr(stmt
->expression
);
1310 split_compound(stmt
);
1313 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1314 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1315 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1317 if (known_condition_true(stmt
->if_conditional
)) {
1318 __split_stmt(stmt
->if_true
);
1321 if (known_condition_false(stmt
->if_conditional
)) {
1322 __split_stmt(stmt
->if_false
);
1325 __split_whole_condition(stmt
->if_conditional
);
1326 __split_stmt(stmt
->if_true
);
1327 if (empty_statement(stmt
->if_true
) &&
1328 last_stmt_on_same_line() &&
1329 !get_macro_name(stmt
->if_true
->pos
))
1330 sm_warning("if();");
1331 __push_true_states();
1332 __use_false_states();
1333 __split_stmt(stmt
->if_false
);
1334 __merge_true_states();
1337 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1338 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1339 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1340 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1341 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1343 if (stmt
->iterator_pre_condition
)
1344 handle_pre_loop(stmt
);
1345 else if (stmt
->iterator_post_condition
)
1346 handle_post_loop(stmt
);
1348 // these are for(;;) type loops.
1349 handle_pre_loop(stmt
);
1353 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1354 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1356 if (get_value(stmt
->switch_expression
, &sval
)) {
1357 split_known_switch(stmt
, sval
);
1360 __split_expr(stmt
->switch_expression
);
1361 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1362 __save_switch_states(top_expression(switch_expr_stack
));
1366 __split_stmt(stmt
->switch_statement
);
1367 if (!__pop_default() && have_remaining_cases())
1368 fake_an_empty_default(stmt
->pos
);
1369 __discard_switches();
1371 pop_expression(&switch_expr_stack
);
1377 __split_label_stmt(stmt
);
1378 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1380 __split_stmt(stmt
->label_statement
);
1383 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1385 __split_expr(stmt
->goto_expression
);
1386 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1387 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1389 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1391 __process_continues();
1393 } else if (stmt
->goto_label
&&
1394 stmt
->goto_label
->type
== SYM_LABEL
&&
1395 stmt
->goto_label
->ident
) {
1396 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1399 if (is_last_stmt(stmt
))
1400 handle_backward_goto(stmt
);
1405 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1407 find_asm_gotos(stmt
);
1408 __pass_to_client(stmt
, ASM_HOOK
);
1409 __split_expr(stmt
->asm_string
);
1410 split_asm_ops(stmt
->asm_outputs
);
1411 split_asm_ops(stmt
->asm_inputs
);
1412 split_expr_list(stmt
->asm_clobbers
, NULL
);
1417 __split_expr(stmt
->range_expression
);
1418 __split_expr(stmt
->range_low
);
1419 __split_expr(stmt
->range_high
);
1423 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1424 if (--indent_cnt
== 1)
1425 __discard_fake_states(NULL
);
1428 __process_post_op_stack();
1430 gettimeofday(&stop
, NULL
);
1431 if (option_time_stmt
&& stmt
)
1432 sm_msg("stmt_time%s: %ld",
1433 stmt
->type
== STMT_COMPOUND
? "_block" : "",
1434 stop
.tv_sec
- start
.tv_sec
);
1437 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1439 struct expression
*expr
;
1441 FOR_EACH_PTR(expr_list
, expr
) {
1442 expr_set_parent_expr(expr
, parent
);
1444 __process_post_op_stack();
1445 } END_FOR_EACH_PTR(expr
);
1448 static bool cast_arg(struct symbol
*type
, struct expression
*arg
)
1450 struct symbol
*orig
;
1455 arg
= strip_parens(arg
);
1456 if (arg
!= strip_expr(arg
))
1459 orig
= get_type(arg
);
1462 if (types_equiv(orig
, type
))
1465 if (orig
->type
== SYM_ARRAY
&& type
->type
== SYM_PTR
)
1469 * I would have expected that we could just do use (orig == type) but I
1470 * guess for pointers we need to get the basetype to do that comparison.
1474 if (orig
->type
!= SYM_PTR
||
1475 type
->type
!= SYM_PTR
) {
1476 if (type_fits(type
, orig
))
1480 orig
= get_real_base_type(orig
);
1481 type
= get_real_base_type(type
);
1488 static struct expression
*fake_a_variable_assign(struct symbol
*type
, struct expression
*call
, struct expression
*expr
, int nr
)
1493 if (!expr
|| !cur_func_sym
)
1496 if (expr
->type
== EXPR_ASSIGNMENT
)
1499 /* for va_args then we don't know the type */
1501 type
= get_type(expr
);
1503 cast
= cast_arg(type
, expr
);
1505 * Using expr_to_sym() here is a hack. We want to say that we don't
1506 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1507 * It turns out faking these assignments is way more expensive than I
1508 * would have imagined. I'm not sure why exactly.
1513 * if the code is "return *p;" where "p" is a user pointer then
1514 * we want to create a fake assignment so that it sets the state
1515 * in check_kernel_user_data.c.
1518 if (expr
->type
!= EXPR_PREOP
&&
1519 expr
->op
!= '*' && expr
->op
!= '&' &&
1525 snprintf(buf
, sizeof(buf
), "__fake_return_%p", expr
);
1527 snprintf(buf
, sizeof(buf
), "__fake_param_%p_%d", call
, nr
);
1529 return create_fake_assign(buf
, type
, expr
);
1532 static void split_args(struct expression
*expr
)
1534 struct expression
*arg
, *tmp
;
1535 struct symbol
*type
;
1539 FOR_EACH_PTR(expr
->args
, arg
) {
1541 expr_set_parent_expr(arg
, expr
);
1542 type
= get_arg_type(expr
->fn
, i
);
1543 tmp
= fake_a_variable_assign(type
, expr
, arg
, i
);
1545 __in_fake_var_assign
++;
1548 __in_fake_var_assign
--;
1549 __process_post_op_stack();
1550 } END_FOR_EACH_PTR(arg
);
1553 static void split_sym(struct symbol
*sym
)
1557 if (!(sym
->namespace & NS_SYMBOL
))
1560 __split_stmt(sym
->stmt
);
1561 __split_expr(sym
->array_size
);
1562 split_symlist(sym
->arguments
);
1563 split_symlist(sym
->symbol_list
);
1564 __split_stmt(sym
->inline_stmt
);
1565 split_symlist(sym
->inline_symbol_list
);
1568 static void split_symlist(struct symbol_list
*sym_list
)
1572 FOR_EACH_PTR(sym_list
, sym
) {
1574 } END_FOR_EACH_PTR(sym
);
1577 typedef void (fake_cb
)(struct expression
*expr
);
1579 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1581 struct symbol
*type
, *tmp
;
1587 name
= member
->name
;
1589 type
= get_type(expr
);
1590 if (!type
|| type
->type
!= SYM_STRUCT
)
1594 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1598 if (strcmp(name
, tmp
->ident
->name
) == 0)
1600 } END_FOR_EACH_PTR(tmp
);
1604 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1606 struct symbol
*type
, *member
;
1609 type
= get_type(expr
);
1610 if (!type
|| type
->type
!= SYM_STRUCT
)
1613 FOR_EACH_PTR(type
->symbol_list
, member
) {
1615 return member
->ident
;
1617 } END_FOR_EACH_PTR(member
);
1621 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1623 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1625 struct expression
*edge_member
, *assign
;
1626 struct symbol
*base
= get_real_base_type(member
);
1630 expr
= member_expression(expr
, '.', member
->ident
);
1632 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1633 struct symbol
*type
;
1635 type
= get_real_base_type(tmp
);
1639 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1640 if (get_extra_state(edge_member
))
1643 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1644 set_inner_struct_members(expr
, tmp
);
1651 assign
= assign_expression(edge_member
, '=', zero_expr());
1652 __split_expr(assign
);
1653 } END_FOR_EACH_PTR(tmp
);
1658 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1661 struct expression
*member
= NULL
;
1662 struct expression
*assign
;
1664 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1665 type
= get_real_base_type(tmp
);
1670 member
= member_expression(expr
, '.', tmp
->ident
);
1671 if (get_extra_state(member
))
1675 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1676 set_inner_struct_members(expr
, tmp
);
1679 if (type
->type
== SYM_ARRAY
)
1684 assign
= assign_expression(member
, '=', zero_expr());
1685 __split_expr(assign
);
1686 } END_FOR_EACH_PTR(tmp
);
1689 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1691 struct expression
*deref
, *assign
, *tmp
, *right
;
1692 struct symbol
*struct_type
, *type
;
1693 struct ident
*member
;
1696 struct_type
= get_type(symbol
);
1698 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1702 * We're parsing an initializer that could look something like this:
1703 * struct foo foo = {
1705 * .whatever.xxx = 11,
1709 * So what we have here is a list with 42, .whatever, and .zzz. We need
1710 * to break it up into left and right sides of the assignments.
1714 FOR_EACH_PTR(members
, tmp
) {
1716 if (tmp
->type
== EXPR_IDENTIFIER
) {
1717 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1718 while (tmp
->type
== EXPR_IDENTIFIER
) {
1719 member
= tmp
->expr_ident
;
1720 tmp
= tmp
->ident_expression
;
1722 deref
= member_expression(deref
, '.', member
);
1724 deref
= member_expression(symbol
, '.', member
);
1727 member
= number_to_member(symbol
, member_idx
);
1728 deref
= member_expression(symbol
, '.', member
);
1732 if (right
->type
== EXPR_INITIALIZER
) {
1733 type
= get_type(deref
);
1734 if (type
&& type
->type
== SYM_ARRAY
)
1735 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1737 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1739 assign
= assign_expression(deref
, '=', right
);
1742 } END_FOR_EACH_PTR(tmp
);
1744 set_unset_to_zero(struct_type
, symbol
);
1747 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1749 fake_member_assigns_helper(symbol_expression(sym
),
1750 sym
->initializer
->expr_list
, fake_cb
);
1753 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1755 struct expression
*offset
, *binop
, *assign
, *tmp
;
1756 struct symbol
*type
;
1759 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1764 FOR_EACH_PTR(expr_list
, tmp
) {
1765 if (tmp
->type
== EXPR_INDEX
) {
1766 if (tmp
->idx_from
!= tmp
->idx_to
)
1768 idx
= tmp
->idx_from
;
1771 if (!tmp
->idx_expression
)
1773 tmp
= tmp
->idx_expression
;
1775 offset
= value_expr(idx
);
1776 binop
= array_element_expression(array
, offset
);
1777 if (tmp
->type
== EXPR_INITIALIZER
) {
1778 type
= get_type(binop
);
1779 if (type
&& type
->type
== SYM_ARRAY
)
1780 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1782 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1784 assign
= assign_expression(binop
, '=', tmp
);
1791 } END_FOR_EACH_PTR(tmp
);
1793 __call_array_initialized_hooks(array
, max
);
1796 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1798 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1801 static void fake_assign_expr(struct symbol
*sym
)
1803 struct expression
*assign
, *symbol
;
1805 symbol
= symbol_expression(sym
);
1806 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1807 __split_expr(assign
);
1810 static void do_initializer_stuff(struct symbol
*sym
)
1812 if (!sym
->initializer
)
1815 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1816 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1817 fake_element_assigns(sym
, __split_expr
);
1819 fake_member_assigns(sym
, __split_expr
);
1821 fake_assign_expr(sym
);
1825 static void split_declaration(struct symbol_list
*sym_list
)
1829 FOR_EACH_PTR(sym_list
, sym
) {
1830 __pass_to_client(sym
, DECLARATION_HOOK
);
1831 do_initializer_stuff(sym
);
1832 __pass_to_client(sym
, DECLARATION_HOOK_AFTER
);
1834 } END_FOR_EACH_PTR(sym
);
1837 static void call_global_assign_hooks(struct expression
*assign
)
1839 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1842 static void fake_global_assign(struct symbol
*sym
)
1844 struct expression
*assign
, *symbol
;
1846 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1847 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1848 fake_element_assigns(sym
, call_global_assign_hooks
);
1849 } else if (sym
->initializer
) {
1850 symbol
= symbol_expression(sym
);
1851 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1852 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1854 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1856 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1857 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1858 fake_member_assigns(sym
, call_global_assign_hooks
);
1859 } else if (sym
->initializer
) {
1860 symbol
= symbol_expression(sym
);
1861 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1862 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1864 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1867 symbol
= symbol_expression(sym
);
1868 if (sym
->initializer
) {
1869 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1870 __split_expr(assign
);
1872 assign
= assign_expression(symbol
, '=', zero_expr());
1874 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1878 static void start_function_definition(struct symbol
*sym
)
1880 __in_function_def
= 1;
1881 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1882 __in_function_def
= 0;
1883 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1887 void add_function_data(unsigned long *fn_data
)
1889 __add_ptr_list(&fn_data_list
, fn_data
);
1892 static void clear_function_data(void)
1896 FOR_EACH_PTR(fn_data_list
, tmp
) {
1898 } END_FOR_EACH_PTR(tmp
);
1901 static void record_func_time(void)
1903 struct timeval stop
;
1907 gettimeofday(&stop
, NULL
);
1908 func_time
= stop
.tv_sec
- fn_start_time
.tv_sec
;
1909 snprintf(buf
, sizeof(buf
), "%d", func_time
);
1910 sql_insert_return_implies(FUNC_TIME
, 0, "", buf
);
1911 if (option_time
&& func_time
> 2) {
1913 sm_msg("func_time: %d", func_time
);
1918 static void split_function(struct symbol
*sym
)
1920 struct symbol
*base_type
= get_base_type(sym
);
1922 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1925 gettimeofday(&outer_fn_start_time
, NULL
);
1926 gettimeofday(&fn_start_time
, NULL
);
1929 cur_func
= sym
->ident
->name
;
1930 set_position(sym
->pos
);
1931 clear_function_data();
1933 last_goto_statement_handled
= 0;
1934 sm_debug("new function: %s\n", cur_func
);
1936 if (option_two_passes
) {
1940 start_function_definition(sym
);
1941 __split_stmt(base_type
->stmt
);
1942 __split_stmt(base_type
->inline_stmt
);
1948 start_function_definition(sym
);
1949 __split_stmt(base_type
->stmt
);
1950 __split_stmt(base_type
->inline_stmt
);
1951 if (!__path_is_null() &&
1952 cur_func_return_type() == &void_ctype
&&
1953 !__bail_on_rest_of_function
) {
1954 __pass_to_client(NULL
, RETURN_HOOK
);
1957 __pass_to_client(sym
, END_FUNC_HOOK
);
1958 if (need_delayed_scope_hooks())
1959 __call_scope_hooks();
1960 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1967 cur_func_sym
= NULL
;
1969 free_data_info_allocs();
1970 free_expression_stack(&switch_expr_stack
);
1971 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1972 __bail_on_rest_of_function
= 0;
1975 static void save_flow_state(void)
1979 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2));
1980 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2));
1981 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2));
1983 __add_ptr_list(&backup
, big_statement_stack
);
1984 __add_ptr_list(&backup
, big_expression_stack
);
1985 __add_ptr_list(&backup
, big_condition_stack
);
1986 __add_ptr_list(&backup
, switch_expr_stack
);
1988 __add_ptr_list(&backup
, cur_func_sym
);
1990 __add_ptr_list(&backup
, __prev_stmt
);
1991 __add_ptr_list(&backup
, __cur_stmt
);
1992 __add_ptr_list(&backup
, __next_stmt
);
1994 FOR_EACH_PTR(fn_data_list
, tmp
) {
1995 __add_ptr_list(&backup
, (void *)*tmp
);
1996 } END_FOR_EACH_PTR(tmp
);
1999 static void *pop_backup(void)
2003 ret
= last_ptr_list(backup
);
2004 delete_ptr_list_last(&backup
);
2008 static void restore_flow_state(void)
2012 FOR_EACH_PTR_REVERSE(fn_data_list
, tmp
) {
2013 *tmp
= (unsigned long)pop_backup();
2014 } END_FOR_EACH_PTR_REVERSE(tmp
);
2016 __next_stmt
= pop_backup();
2017 __cur_stmt
= pop_backup();
2018 __prev_stmt
= pop_backup();
2020 cur_func_sym
= pop_backup();
2021 switch_expr_stack
= pop_backup();
2022 big_condition_stack
= pop_backup();
2023 big_expression_stack
= pop_backup();
2024 big_statement_stack
= pop_backup();
2025 final_pass
= PTR_INT(pop_backup()) >> 2;
2026 loop_count
= PTR_INT(pop_backup()) >> 2;
2027 loop_num
= PTR_INT(pop_backup()) >> 2;
2030 static void parse_inline(struct expression
*call
)
2032 struct symbol
*base_type
;
2033 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
2034 struct timeval time_backup
= fn_start_time
;
2035 struct expression
*orig_inline
= __inline_fn
;
2038 if (out_of_memory() || taking_too_long())
2043 __pass_to_client(call
, INLINE_FN_START
);
2044 final_pass
= 0; /* don't print anything */
2046 orig_budget
= inline_budget
;
2047 inline_budget
= inline_budget
- 5;
2049 base_type
= get_base_type(call
->fn
->symbol
);
2050 cur_func_sym
= call
->fn
->symbol
;
2051 if (call
->fn
->symbol
->ident
)
2052 cur_func
= call
->fn
->symbol
->ident
->name
;
2055 set_position(call
->fn
->symbol
->pos
);
2058 big_statement_stack
= NULL
;
2059 big_expression_stack
= NULL
;
2060 big_condition_stack
= NULL
;
2061 switch_expr_stack
= NULL
;
2063 sm_debug("inline function: %s\n", cur_func
);
2065 clear_function_data();
2068 start_function_definition(call
->fn
->symbol
);
2069 __split_stmt(base_type
->stmt
);
2070 __split_stmt(base_type
->inline_stmt
);
2071 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
2072 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
2073 call
->fn
->symbol
->parsed
= true;
2075 free_expression_stack(&switch_expr_stack
);
2076 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
2080 restore_flow_state();
2081 fn_start_time
= time_backup
;
2082 cur_func
= cur_func_bak
;
2084 restore_all_states();
2085 set_position(call
->pos
);
2086 __inline_fn
= orig_inline
;
2087 inline_budget
= orig_budget
;
2088 __pass_to_client(call
, INLINE_FN_END
);
2091 static struct symbol_list
*inlines_called
;
2092 static void add_inline_function(struct symbol
*sym
)
2094 static struct symbol_list
*already_added
;
2097 FOR_EACH_PTR(already_added
, tmp
) {
2100 } END_FOR_EACH_PTR(tmp
);
2102 add_ptr_list(&already_added
, sym
);
2103 add_ptr_list(&inlines_called
, sym
);
2106 static void process_inlines(void)
2110 FOR_EACH_PTR(inlines_called
, tmp
) {
2111 split_function(tmp
);
2112 } END_FOR_EACH_PTR(tmp
);
2113 free_ptr_list(&inlines_called
);
2116 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
2120 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
2123 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
2125 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
2127 } END_FOR_EACH_PTR_REVERSE(sym
);
2132 static bool interesting_function(struct symbol
*sym
)
2134 static int prev_stream
= -1;
2135 static bool prev_answer
;
2136 const char *filename
;
2139 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
2142 if (sym
->pos
.stream
== prev_stream
)
2145 prev_stream
= sym
->pos
.stream
;
2146 prev_answer
= false;
2148 filename
= stream_name(sym
->pos
.stream
);
2149 len
= strlen(filename
);
2150 if (len
> 0 && filename
[len
- 1] == 'c')
2155 static void split_inlines_in_scope(struct symbol
*sym
)
2157 struct symbol
*base
;
2158 struct symbol_list
*scope_list
;
2161 scope_list
= sym
->scope
->symbols
;
2162 stream
= sym
->pos
.stream
;
2164 /* find the last static symbol in the file */
2165 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
2166 if (sym
->pos
.stream
!= stream
)
2168 if (sym
->type
!= SYM_NODE
)
2170 base
= get_base_type(sym
);
2173 if (base
->type
!= SYM_FN
)
2175 if (!base
->inline_stmt
)
2177 if (!interesting_function(sym
))
2179 add_inline_function(sym
);
2180 } END_FOR_EACH_PTR_REVERSE(sym
);
2185 static void split_inlines(struct symbol_list
*sym_list
)
2189 sym
= get_last_scoped_symbol(sym_list
, 0);
2191 split_inlines_in_scope(sym
);
2192 sym
= get_last_scoped_symbol(sym_list
, 1);
2194 split_inlines_in_scope(sym
);
2197 static struct stree
*clone_estates_perm(struct stree
*orig
)
2199 struct stree
*ret
= NULL
;
2200 struct sm_state
*tmp
;
2202 FOR_EACH_SM(orig
, tmp
) {
2203 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
2204 } END_FOR_EACH_SM(tmp
);
2209 struct position last_pos
;
2210 static void split_c_file_functions(struct symbol_list
*sym_list
)
2215 FOR_EACH_PTR(sym_list
, sym
) {
2216 set_position(sym
->pos
);
2217 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
2218 __pass_to_client(sym
, BASE_HOOK
);
2219 fake_global_assign(sym
);
2220 __pass_to_client(sym
, DECLARATION_HOOK_AFTER
);
2222 } END_FOR_EACH_PTR(sym
);
2223 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
2226 FOR_EACH_PTR(sym_list
, sym
) {
2227 set_position(sym
->pos
);
2228 last_pos
= sym
->pos
;
2229 if (!interesting_function(sym
))
2231 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
2232 split_function(sym
);
2235 last_pos
= sym
->pos
;
2236 } END_FOR_EACH_PTR(sym
);
2237 split_inlines(sym_list
);
2238 __pass_to_client(sym_list
, END_FILE_HOOK
);
2241 static int final_before_fake
;
2242 void init_fake_env(void)
2245 final_before_fake
= final_pass
;
2247 __push_fake_cur_stree();
2251 void end_fake_env(void)
2253 __free_fake_cur_stree();
2256 final_pass
= final_before_fake
;
2259 static void open_output_files(char *base_file
)
2263 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
2264 sm_outfd
= fopen(buf
, "w");
2266 sm_fatal("Cannot open %s", buf
);
2271 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
2272 sql_outfd
= fopen(buf
, "w");
2274 sm_fatal("Error: Cannot open %s", buf
);
2276 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
2277 caller_info_fd
= fopen(buf
, "w");
2278 if (!caller_info_fd
)
2279 sm_fatal("Error: Cannot open %s", buf
);
2282 void smatch(struct string_list
*filelist
)
2284 struct symbol_list
*sym_list
;
2285 struct timeval stop
, start
;
2289 gettimeofday(&start
, NULL
);
2291 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
2292 path
= getcwd(NULL
, 0);
2293 free(full_base_file
);
2295 len
= strlen(path
) + 1 + strlen(base_file
) + 1;
2296 full_base_file
= malloc(len
);
2297 snprintf(full_base_file
, len
, "%s/%s", path
, base_file
);
2299 full_base_file
= alloc_string(base_file
);
2301 if (option_file_output
)
2302 open_output_files(base_file
);
2303 base_file_stream
= input_stream_nr
;
2304 sym_list
= sparse_keep_tokens(base_file
);
2305 split_c_file_functions(sym_list
);
2306 } END_FOR_EACH_PTR_NOTAG(base_file
);
2308 gettimeofday(&stop
, NULL
);
2310 set_position(last_pos
);
2313 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
2315 sm_msg("mem: %luKb", get_max_memory());