2 * Copyright (C) 2006,2008 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
24 #include "smatch_expression_stacks.h"
25 #include "smatch_extra.h"
26 #include "smatch_slist.h"
29 int __in_fake_struct_assign
;
31 int __in_fake_var_assign
;
37 struct expression
*__inline_fn
;
39 int __smatch_lineno
= 0;
41 static char *base_file
;
42 static const char *filename
;
43 static char *pathname
;
44 static char *full_filename
;
45 static char *full_base_file
;
46 static char *cur_func
;
48 static unsigned int loop_count
;
49 static int last_goto_statement_handled
;
50 int __expr_stmt_count
;
51 int __in_function_def
;
52 int __in_unmatched_hook
;
53 static struct expression_list
*switch_expr_stack
= NULL
;
54 static struct expression_list
*post_op_stack
= NULL
;
56 static struct ptr_list
*fn_data_list
;
57 static struct ptr_list
*backup
;
59 struct expression_list
*big_expression_stack
;
60 struct statement_list
*big_statement_stack
;
61 struct statement
*__prev_stmt
;
62 struct statement
*__cur_stmt
;
63 struct statement
*__next_stmt
;
64 static struct expression_list
*parsed_calls
;
65 static int indent_cnt
;
66 int __in_pre_condition
= 0;
67 int __bail_on_rest_of_function
= 0;
68 static struct timeval fn_start_time
;
69 static struct timeval outer_fn_start_time
;
70 char *get_function(void) { return cur_func
; }
71 int get_lineno(void) { return __smatch_lineno
; }
72 int inside_loop(void) { return !!loop_count
; }
73 int definitely_inside_loop(void) { return !!(loop_count
& ~0x08000000); }
74 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
75 int in_expression_statement(void) { return !!__expr_stmt_count
; }
77 static void split_symlist(struct symbol_list
*sym_list
);
78 static void split_declaration(struct symbol_list
*sym_list
);
79 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
);
80 static void split_args(struct expression
*expr
);
81 static struct expression
*fake_a_variable_assign(struct symbol
*type
, struct expression
*call
, struct expression
*expr
, int nr
);
82 static void add_inline_function(struct symbol
*sym
);
83 static void parse_inline(struct expression
*expr
);
85 int option_assume_loops
= 0;
86 int option_two_passes
= 0;
87 struct symbol
*cur_func_sym
= NULL
;
88 struct stree
*global_states
;
90 const unsigned long valid_ptr_min
= 4096;
91 unsigned long valid_ptr_max
= ULONG_MAX
& ~(MTAG_OFFSET_MASK
);
92 const sval_t valid_ptr_min_sval
= {
96 sval_t valid_ptr_max_sval
= {
98 {.value
= ULONG_MAX
& ~(MTAG_OFFSET_MASK
)},
100 struct range_list
*valid_ptr_rl
;
102 void alloc_valid_ptr_rl(void)
104 valid_ptr_max
= sval_type_max(&ulong_ctype
).value
& ~(MTAG_OFFSET_MASK
);
105 valid_ptr_max_sval
.value
= valid_ptr_max
;
107 valid_ptr_rl
= alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
108 valid_ptr_rl
= cast_rl(&ptr_ctype
, valid_ptr_rl
);
109 valid_ptr_rl
= clone_rl_permanent(valid_ptr_rl
);
112 int outside_of_function(void)
114 return cur_func_sym
== NULL
;
117 const char *get_filename(void)
119 if (option_info
&& option_full_path
)
120 return full_base_file
;
123 if (option_full_path
)
124 return full_filename
;
128 const char *get_base_file(void)
130 if (option_full_path
)
131 return full_base_file
;
135 unsigned long long get_file_id(void)
137 return str_to_llu_hash(get_filename());
140 unsigned long long get_base_file_id(void)
142 return str_to_llu_hash(get_base_file());
145 static void set_position(struct position pos
)
148 static int prev_stream
= -1;
153 if (pos
.stream
== 0 && pos
.line
== 0)
156 __smatch_lineno
= pos
.line
;
158 if (pos
.stream
== prev_stream
)
161 filename
= stream_name(pos
.stream
);
164 pathname
= getcwd(NULL
, 0);
166 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
167 full_filename
= malloc(len
);
168 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
170 full_filename
= alloc_string(filename
);
175 int is_assigned_call(struct expression
*expr
)
177 struct expression
*parent
= expr_get_parent_expr(expr
);
180 parent
->type
== EXPR_ASSIGNMENT
&&
182 strip_expr(parent
->right
) == expr
)
188 int is_fake_assigned_call(struct expression
*expr
)
190 struct expression
*parent
= expr_get_fake_parent_expr(expr
);
193 parent
->type
== EXPR_ASSIGNMENT
&&
195 strip_expr(parent
->right
) == expr
)
201 static bool is_inline_func(struct expression
*expr
)
203 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
205 if (!expr
->symbol
->definition
)
207 if (expr
->symbol
->definition
->ctype
.modifiers
& MOD_INLINE
)
213 static int is_noreturn_func(struct expression
*expr
)
215 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
219 * It's almost impossible for Smatch to handle __builtin_constant_p()
220 * the same way that GCC does so Smatch ends up making some functions
221 * as no return functions incorrectly.
224 if (option_project
== PROJ_KERNEL
&& expr
->symbol
->ident
&&
225 strstr(expr
->symbol
->ident
->name
, "__compiletime_assert"))
228 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
233 static int save_func_time(void *_rl
, int argc
, char **argv
, char **azColName
)
235 unsigned long *rl
= _rl
;
237 *rl
= strtoul(argv
[0], NULL
, 10);
241 static int get_func_time(struct symbol
*sym
)
243 unsigned long time
= 0;
245 run_sql(&save_func_time
, &time
,
246 "select key from return_implies where %s and type = %d;",
247 get_static_filter(sym
), FUNC_TIME
);
252 static int inline_budget
= 20;
254 int inlinable(struct expression
*expr
)
257 struct statement
*last_stmt
= NULL
;
259 if (__inline_fn
) /* don't nest */
262 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
264 if (is_no_inline_function(expr
->symbol
->ident
->name
))
266 sym
= get_base_type(expr
->symbol
);
267 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
268 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
270 if (sym
->stmt
->type
!= STMT_COMPOUND
)
272 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
274 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
275 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
277 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
279 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
285 /* the magic numbers in this function are pulled out of my bum. */
286 if (last_stmt
->pos
.line
> sym
->pos
.line
+ inline_budget
)
289 if (get_func_time(expr
->symbol
) >= 2)
295 void __process_post_op_stack(void)
297 struct expression
*expr
;
299 FOR_EACH_PTR(post_op_stack
, expr
) {
300 __pass_to_client(expr
, OP_HOOK
);
301 } END_FOR_EACH_PTR(expr
);
303 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
306 static int handle_comma_assigns(struct expression
*expr
)
308 struct expression
*right
;
309 struct expression
*assign
;
311 right
= strip_expr(expr
->right
);
312 if (right
->type
!= EXPR_COMMA
)
315 __split_expr(right
->left
);
316 __process_post_op_stack();
318 assign
= assign_expression(expr
->left
, '=', right
->right
);
319 __split_expr(assign
);
324 /* This is to handle *p++ = foo; assignments */
325 static int handle_postop_assigns(struct expression
*expr
)
327 struct expression
*left
, *fake_left
;
328 struct expression
*assign
;
330 left
= strip_expr(expr
->left
);
331 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '*')
333 left
= strip_expr(left
->unop
);
334 if (left
->type
!= EXPR_POSTOP
)
337 fake_left
= deref_expression(strip_expr(left
->unop
));
338 assign
= assign_expression(fake_left
, '=', expr
->right
);
340 __split_expr(assign
);
341 __split_expr(expr
->left
);
346 static int prev_expression_is_getting_address(struct expression
*expr
)
348 struct expression
*parent
;
351 parent
= expr_get_parent_expr(expr
);
355 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
357 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
359 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
361 /* Handle &foo->array[offset] */
362 if (parent
->type
== EXPR_BINOP
&& parent
->op
== '+') {
363 parent
= expr_get_parent_expr(parent
);
366 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '*')
376 int __in_builtin_overflow_func
;
377 static void handle_builtin_overflow_func(struct expression
*expr
)
379 struct expression
*a
, *b
, *res
, *assign
;
382 if (sym_name_is("__builtin_add_overflow", expr
->fn
))
384 else if (sym_name_is("__builtin_sub_overflow", expr
->fn
))
386 else if (sym_name_is("__builtin_mul_overflow", expr
->fn
))
391 a
= get_argument_from_call_expr(expr
->args
, 0);
392 b
= get_argument_from_call_expr(expr
->args
, 1);
393 res
= get_argument_from_call_expr(expr
->args
, 2);
395 assign
= assign_expression(deref_expression(res
), '=', binop_expression(a
, op
, b
));
397 __in_builtin_overflow_func
++;
398 __split_expr(assign
);
399 __in_builtin_overflow_func
--;
402 static int handle__builtin_choose_expr(struct expression
*expr
)
404 struct expression
*const_expr
, *expr1
, *expr2
;
407 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
410 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
411 expr1
= get_argument_from_call_expr(expr
->args
, 1);
412 expr2
= get_argument_from_call_expr(expr
->args
, 2);
414 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
423 static int handle__builtin_choose_expr_assigns(struct expression
*expr
)
425 struct expression
*const_expr
, *right
, *expr1
, *expr2
, *fake
;
428 right
= strip_parens(expr
->right
);
429 if (right
->type
!= EXPR_CALL
)
431 if (!sym_name_is("__builtin_choose_expr", right
->fn
))
434 const_expr
= get_argument_from_call_expr(right
->args
, 0);
435 expr1
= get_argument_from_call_expr(right
->args
, 1);
436 expr2
= get_argument_from_call_expr(right
->args
, 2);
438 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
441 fake
= assign_expression(expr
->left
, '=', sval
.value
? expr1
: expr2
);
446 int is_condition_call(struct expression
*expr
)
448 struct expression
*tmp
;
450 FOR_EACH_PTR_REVERSE(big_condition_stack
, tmp
) {
451 if (expr
== tmp
|| expr_get_parent_expr(expr
) == tmp
)
453 if (tmp
->pos
.line
< expr
->pos
.line
)
455 } END_FOR_EACH_PTR_REVERSE(tmp
);
460 static struct expression
*expr_get_parent_no_parens(struct expression
*expr
)
463 expr
= expr_get_parent_expr(expr
);
465 expr
->type
== EXPR_PREOP
&&
471 static bool gen_fake_function_assign(struct expression
*expr
)
473 static struct expression
*parsed
;
474 struct expression
*assign
, *parent
;
478 /* The rule is that every non-void function call has to be part of an
479 * assignment. TODO: Should we create a fake non-casted assignment
480 * for casted assignments? Also faked assigns for += assignments?
482 type
= get_type(expr
);
483 if (!type
|| type
== &void_ctype
)
486 parent
= expr_get_parent_no_parens(expr
);
487 if (parent
&& parent
->type
== EXPR_ASSIGNMENT
)
490 parent
= expr_get_fake_parent_expr(expr
);
492 struct expression
*left
= parent
->left
;
494 if (parent
== parsed
)
496 if (!left
|| left
->type
!= EXPR_SYMBOL
)
498 if (strncmp(left
->symbol_name
->name
, "__fake_assign_", 14) != 0)
501 __split_expr(parent
);
505 // TODO: faked_assign skipping conditions is a hack
506 if (is_condition_call(expr
))
509 snprintf(buf
, sizeof(buf
), "__fake_assign_%p", expr
);
510 assign
= create_fake_assign(buf
, get_type(expr
), expr
);
513 __split_expr(assign
);
517 static void split_call(struct expression
*expr
)
519 if (gen_fake_function_assign(expr
))
522 expr_set_parent_expr(expr
->fn
, expr
);
524 if (sym_name_is("__builtin_constant_p", expr
->fn
))
526 if (handle__builtin_choose_expr(expr
))
528 __split_expr(expr
->fn
);
530 if (is_inline_func(expr
->fn
))
531 add_inline_function(expr
->fn
->symbol
->definition
);
532 if (inlinable(expr
->fn
))
534 __process_post_op_stack();
535 __pass_to_client(expr
, FUNCTION_CALL_HOOK_BEFORE
);
536 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
538 if (inlinable(expr
->fn
))
540 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
541 if (is_noreturn_func(expr
->fn
))
543 if (!expr_get_parent_expr(expr
) && indent_cnt
== 1)
544 __discard_fake_states(expr
);
545 handle_builtin_overflow_func(expr
);
546 __add_ptr_list((struct ptr_list
**)&parsed_calls
, expr
);
549 void parse_assignment(struct expression
*expr
)
551 struct expression
*right
;
553 expr_set_parent_expr(expr
->left
, expr
);
554 expr_set_parent_expr(expr
->right
, expr
);
556 right
= strip_expr(expr
->right
);
560 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
563 if (__handle_condition_assigns(expr
))
565 /* foo = (x < 5 ? foo : 5); */
566 if (__handle_select_assigns(expr
))
568 /* foo = ({frob(); frob(); frob(); 1;}) */
569 if (__handle_expr_statement_assigns(expr
))
570 return; // FIXME: got after
572 if (handle_comma_assigns(expr
))
574 if (handle__builtin_choose_expr_assigns(expr
))
576 if (handle_postop_assigns(expr
))
577 return; /* no need to goto after_assign */
579 __split_expr(expr
->right
);
580 if (outside_of_function())
581 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
583 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
586 // FIXME: the ordering of this is tricky
587 __fake_struct_member_assignments(expr
);
589 /* Re-examine ->right for inlines. See the commit message */
590 right
= strip_expr(expr
->right
);
591 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
592 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
595 if (get_macro_name(right
->pos
) &&
596 get_macro_name(expr
->left
->pos
) != get_macro_name(right
->pos
))
597 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
599 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
600 __split_expr(expr
->left
);
603 static bool skip_split_off(struct expression
*expr
)
605 if (expr
->type
== EXPR_CALL
&&
606 sym_name_is("__smatch_stop_skip", expr
->fn
))
611 void __split_expr(struct expression
*expr
)
616 if (skip_split_off(expr
))
622 // sm_msg("Debug expr_type %d %s expr = '%s'", expr->type, show_special(expr->op), expr_to_str(expr));
624 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
626 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
629 push_expression(&big_expression_stack
, expr
);
630 set_position(expr
->pos
);
631 __pass_to_client(expr
, EXPR_HOOK
);
633 switch (expr
->type
) {
635 expr_set_parent_expr(expr
->unop
, expr
);
637 if (expr
->op
== '*' &&
638 !prev_expression_is_getting_address(expr
))
639 __pass_to_client(expr
, DEREF_HOOK
);
640 __split_expr(expr
->unop
);
641 __pass_to_client(expr
, OP_HOOK
);
644 expr_set_parent_expr(expr
->unop
, expr
);
646 __split_expr(expr
->unop
);
647 push_expression(&post_op_stack
, expr
);
651 if (expr
->statement
&& !expr
->statement
) {
652 stmt_set_parent_stmt(expr
->statement
,
653 last_ptr_list((struct ptr_list
*)big_statement_stack
));
655 __split_stmt(expr
->statement
);
660 expr_set_parent_expr(expr
->left
, expr
);
661 expr_set_parent_expr(expr
->right
, expr
);
663 __pass_to_client(expr
, LOGIC_HOOK
);
664 __handle_logic(expr
);
667 expr_set_parent_expr(expr
->left
, expr
);
668 expr_set_parent_expr(expr
->right
, expr
);
670 __pass_to_client(expr
, BINOP_HOOK
);
671 __split_expr(expr
->left
);
672 __split_expr(expr
->right
);
675 expr_set_parent_expr(expr
->left
, expr
);
676 expr_set_parent_expr(expr
->right
, expr
);
678 __split_expr(expr
->left
);
679 __process_post_op_stack();
680 __split_expr(expr
->right
);
682 case EXPR_ASSIGNMENT
:
683 parse_assignment(expr
);
686 expr_set_parent_expr(expr
->deref
, expr
);
688 __pass_to_client(expr
, DEREF_HOOK
);
689 __split_expr(expr
->deref
);
692 expr_set_parent_expr(expr
->base
, expr
);
694 __split_expr(expr
->base
);
697 case EXPR_FORCE_CAST
:
698 expr_set_parent_expr(expr
->cast_expression
, expr
);
700 __pass_to_client(expr
, CAST_HOOK
);
701 __split_expr(expr
->cast_expression
);
704 if (expr
->cast_expression
)
705 __pass_to_client(strip_parens(expr
->cast_expression
),
711 case EXPR_CONDITIONAL
:
713 expr_set_parent_expr(expr
->conditional
, expr
);
714 expr_set_parent_expr(expr
->cond_true
, expr
);
715 expr_set_parent_expr(expr
->cond_false
, expr
);
717 if (known_condition_true(expr
->conditional
)) {
718 __split_expr(expr
->cond_true
);
721 if (known_condition_false(expr
->conditional
)) {
722 __split_expr(expr
->cond_false
);
725 __pass_to_client(expr
, SELECT_HOOK
);
726 __split_whole_condition(expr
->conditional
);
727 __split_expr(expr
->cond_true
);
728 __push_true_states();
729 __use_false_states();
730 __split_expr(expr
->cond_false
);
731 __merge_true_states();
736 case EXPR_INITIALIZER
:
737 split_expr_list(expr
->expr_list
, expr
);
739 case EXPR_IDENTIFIER
:
740 expr_set_parent_expr(expr
->ident_expression
, expr
);
741 __split_expr(expr
->ident_expression
);
744 expr_set_parent_expr(expr
->idx_expression
, expr
);
745 __split_expr(expr
->idx_expression
);
748 expr_set_parent_expr(expr
->init_expr
, expr
);
749 __split_expr(expr
->init_expr
);
752 __pass_to_client(expr
, SYM_HOOK
);
755 __pass_to_client(expr
, STRING_HOOK
);
758 struct expression
*tmp
;
760 tmp
= strip_Generic(expr
);
768 __pass_to_client(expr
, EXPR_HOOK_AFTER
);
769 pop_expression(&big_expression_stack
);
772 static int is_forever_loop(struct statement
*stmt
)
774 struct expression
*expr
;
777 expr
= strip_expr(stmt
->iterator_pre_condition
);
779 expr
= stmt
->iterator_post_condition
;
781 /* this is a for(;;) loop... */
785 if (get_value(expr
, &sval
) && sval
.value
!= 0)
792 static char *get_loop_name(int num
)
796 snprintf(buf
, 255, "-loop%d", num
);
798 return alloc_sname(buf
);
802 * Pre Loops are while and for loops.
804 static void handle_pre_loop(struct statement
*stmt
)
806 int once_through
; /* we go through the loop at least once */
807 struct sm_state
*extra_sm
= NULL
;
810 struct stree
*stree
= NULL
;
811 struct sm_state
*sm
= NULL
;
813 loop_name
= get_loop_name(loop_num
);
816 if (stmt
->iterator_pre_statement
) {
817 __split_stmt(stmt
->iterator_pre_statement
);
818 __prev_stmt
= stmt
->iterator_pre_statement
;
821 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
827 __merge_gotos(loop_name
, NULL
);
829 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
830 __in_pre_condition
++;
831 __pass_to_client(stmt
, PRELOOP_HOOK
);
832 __split_whole_condition(stmt
->iterator_pre_condition
);
833 __in_pre_condition
--;
834 FOR_EACH_SM(stree
, sm
) {
835 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
836 } END_FOR_EACH_SM(sm
);
839 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
841 if (option_assume_loops
)
844 __split_stmt(stmt
->iterator_statement
);
845 if (is_forever_loop(stmt
)) {
847 __save_gotos(loop_name
, NULL
);
849 __push_fake_cur_stree();
850 __split_stmt(stmt
->iterator_post_statement
);
851 stree
= __pop_fake_cur_stree();
853 __discard_false_states();
854 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
857 if (!__path_is_null())
858 __merge_stree_into_cur(stree
);
862 unchanged
= __iterator_unchanged(extra_sm
);
863 __split_stmt(stmt
->iterator_post_statement
);
864 __prev_stmt
= stmt
->iterator_post_statement
;
867 __save_gotos(loop_name
, NULL
);
868 __in_pre_condition
++;
869 __split_whole_condition(stmt
->iterator_pre_condition
);
870 __in_pre_condition
--;
872 __merge_false_states();
874 __discard_false_states();
876 __merge_false_states();
878 if (extra_sm
&& unchanged
)
879 __extra_pre_loop_hook_after(extra_sm
,
880 stmt
->iterator_post_statement
,
881 stmt
->iterator_pre_condition
);
882 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
889 * Post loops are do {} while();
891 static void handle_post_loop(struct statement
*stmt
)
895 loop_name
= get_loop_name(loop_num
);
899 __pass_to_client(stmt
, POSTLOOP_HOOK
);
903 __merge_gotos(loop_name
, NULL
);
904 __split_stmt(stmt
->iterator_statement
);
906 if (!expr_is_zero(stmt
->iterator_post_condition
))
907 __save_gotos(loop_name
, NULL
);
909 if (is_forever_loop(stmt
)) {
910 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
913 __split_whole_condition(stmt
->iterator_post_condition
);
914 __use_false_states();
915 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
921 static int empty_statement(struct statement
*stmt
)
925 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
930 static int last_stmt_on_same_line(void)
932 struct statement
*stmt
;
935 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
938 if (stmt
->pos
.line
== get_lineno())
941 } END_FOR_EACH_PTR_REVERSE(stmt
);
945 static void split_asm_ops(struct asm_operand_list
*ops
)
947 struct asm_operand
*op
;
949 FOR_EACH_PTR(ops
, op
) {
950 __split_expr(op
->expr
);
951 } END_FOR_EACH_PTR(op
);
954 static int is_case_val(struct statement
*stmt
, sval_t sval
)
958 if (stmt
->type
!= STMT_CASE
)
960 if (!stmt
->case_expression
) {
964 if (!get_value(stmt
->case_expression
, &case_sval
))
966 if (case_sval
.value
== sval
.value
)
971 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
972 struct expression
*case_expr
,
973 struct expression
*case_to
)
976 struct range_list
*rl
= NULL
;
977 struct symbol
*switch_type
;
979 switch_type
= get_type(switch_expr
);
980 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
981 start
= sval_cast(switch_type
, start
);
982 end
= sval_cast(switch_type
, end
);
983 add_range(&rl
, start
, end
);
984 } else if (get_value(case_expr
, &start
)) {
985 start
= sval_cast(switch_type
, start
);
986 add_range(&rl
, start
, start
);
992 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
994 struct statement
*tmp
;
995 struct range_list
*rl
;
997 __split_expr(stmt
->switch_expression
);
998 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
1000 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1001 __save_switch_states(top_expression(switch_expr_stack
));
1006 stmt
= stmt
->switch_statement
;
1008 __push_scope_hooks();
1009 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
1010 __smatch_lineno
= tmp
->pos
.line
;
1011 // FIXME: what if default comes before the known case statement?
1012 if (is_case_val(tmp
, sval
)) {
1013 rl
= alloc_rl(sval
, sval
);
1014 __merge_switches(top_expression(switch_expr_stack
), rl
);
1015 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
1016 stmt_set_parent_stmt(tmp
->case_statement
, tmp
);
1017 __split_stmt(tmp
->case_statement
);
1020 if (__path_is_null())
1024 if (__path_is_null()) {
1028 } END_FOR_EACH_PTR(tmp
);
1030 __call_scope_hooks();
1031 if (!__pop_default())
1032 __merge_switches(top_expression(switch_expr_stack
), NULL
);
1033 __discard_switches();
1035 pop_expression(&switch_expr_stack
);
1038 static void split_case(struct statement
*stmt
)
1040 struct range_list
*rl
= NULL
;
1042 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
1043 expr_set_parent_stmt(stmt
->case_to
, stmt
);
1045 rl
= get_case_rl(top_expression(switch_expr_stack
),
1046 stmt
->case_expression
, stmt
->case_to
);
1047 while (stmt
->case_statement
->type
== STMT_CASE
) {
1048 struct range_list
*tmp
;
1050 tmp
= get_case_rl(top_expression(switch_expr_stack
),
1051 stmt
->case_statement
->case_expression
,
1052 stmt
->case_statement
->case_to
);
1055 rl
= rl_union(rl
, tmp
);
1056 if (!stmt
->case_expression
)
1059 stmt
= stmt
->case_statement
;
1062 __merge_switches(top_expression(switch_expr_stack
), rl
);
1064 if (!stmt
->case_expression
)
1067 stmt_set_parent_stmt(stmt
->case_statement
, stmt
);
1068 __split_stmt(stmt
->case_statement
);
1071 int time_parsing_function(void)
1073 return ms_since(&fn_start_time
) / 1000;
1076 bool taking_too_long(void)
1078 if ((ms_since(&outer_fn_start_time
) / 1000) > 60 * 5) /* five minutes */
1083 struct statement
*get_last_stmt(void)
1086 struct statement
*stmt
;
1088 fn
= get_base_type(cur_func_sym
);
1093 stmt
= fn
->inline_stmt
;
1094 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
1096 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
1097 if (stmt
&& stmt
->type
== STMT_LABEL
)
1098 stmt
= stmt
->label_statement
;
1102 int is_last_stmt(struct statement
*cur_stmt
)
1104 struct statement
*last
;
1106 last
= get_last_stmt();
1107 if (last
&& last
== cur_stmt
)
1112 static void handle_backward_goto(struct statement
*goto_stmt
)
1114 const char *goto_name
, *label_name
;
1115 struct statement
*func_stmt
;
1116 struct symbol
*base_type
= get_base_type(cur_func_sym
);
1117 struct statement
*tmp
;
1122 if (last_goto_statement_handled
)
1124 last_goto_statement_handled
= 1;
1126 if (!goto_stmt
->goto_label
||
1127 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
1128 !goto_stmt
->goto_label
->ident
)
1130 goto_name
= goto_stmt
->goto_label
->ident
->name
;
1132 func_stmt
= base_type
->stmt
;
1134 func_stmt
= base_type
->inline_stmt
;
1137 if (func_stmt
->type
!= STMT_COMPOUND
)
1140 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
1142 if (tmp
->type
!= STMT_LABEL
)
1144 if (!tmp
->label_identifier
||
1145 tmp
->label_identifier
->type
!= SYM_LABEL
||
1146 !tmp
->label_identifier
->ident
)
1148 label_name
= tmp
->label_identifier
->ident
->name
;
1149 if (strcmp(goto_name
, label_name
) != 0)
1154 } END_FOR_EACH_PTR(tmp
);
1157 static void fake_a_return(void)
1159 struct expression
*ret
= NULL
;
1164 if (cur_func_return_type() != &void_ctype
)
1165 ret
= unknown_value_expression(NULL
);
1167 __pass_to_client(ret
, RETURN_HOOK
);
1171 static void split_ret_value(struct expression
*expr
)
1173 struct symbol
*type
;
1178 type
= get_real_base_type(cur_func_sym
);
1179 type
= get_real_base_type(type
);
1180 expr
= fake_a_variable_assign(type
, NULL
, expr
, -1);
1182 __in_fake_var_assign
++;
1184 __in_fake_var_assign
--;
1187 static void fake_an_empty_default(struct position pos
)
1189 static struct statement none
= {};
1192 none
.type
= STMT_NONE
;
1193 __merge_switches(top_expression(switch_expr_stack
), NULL
);
1194 __split_stmt(&none
);
1197 static void split_compound(struct statement
*stmt
)
1199 struct statement
*prev
= NULL
;
1200 struct statement
*cur
= NULL
;
1201 struct statement
*next
;
1203 __push_scope_hooks();
1205 FOR_EACH_PTR(stmt
->stmts
, next
) {
1206 /* just set them all ahead of time */
1207 stmt_set_parent_stmt(next
, stmt
);
1217 } END_FOR_EACH_PTR(next
);
1226 * For function scope, then delay calling the scope hooks until the
1227 * end of function hooks can run. I'm not positive this is the right
1230 if (!is_last_stmt(cur
))
1231 __call_scope_hooks();
1235 * This is a hack, work around for detecting empty functions.
1237 static int need_delayed_scope_hooks(void)
1239 struct symbol
*fn
= get_base_type(cur_func_sym
);
1240 struct statement
*stmt
;
1246 stmt
= fn
->inline_stmt
;
1247 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
1252 void __split_label_stmt(struct statement
*stmt
)
1254 if (stmt
->label_identifier
&&
1255 stmt
->label_identifier
->type
== SYM_LABEL
&&
1256 stmt
->label_identifier
->ident
) {
1257 loop_count
|= 0x0800000;
1258 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
1262 static void find_asm_gotos(struct statement
*stmt
)
1266 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
1267 __save_gotos(sym
->ident
->name
, sym
);
1268 } END_FOR_EACH_PTR(sym
);
1271 static bool already_parsed_call(struct expression
*call
)
1273 struct expression
*expr
;
1275 FOR_EACH_PTR(parsed_calls
, expr
) {
1278 } END_FOR_EACH_PTR(expr
);
1282 static void free_parsed_call_stuff(bool free_fake_states
)
1284 free_expression_stack(&parsed_calls
);
1285 if (free_fake_states
)
1286 __discard_fake_states(NULL
);
1289 void __split_stmt(struct statement
*stmt
)
1292 struct timeval start
, stop
;
1293 bool skip_after
= false;
1295 gettimeofday(&start
, NULL
);
1300 if (!__in_fake_assign
)
1301 __silence_warnings_for_stmt
= false;
1303 if (__bail_on_rest_of_function
|| is_skipped_function())
1306 if (out_of_memory() || taking_too_long()) {
1307 gettimeofday(&start
, NULL
);
1309 __bail_on_rest_of_function
= 1;
1311 sm_perror("Function too hairy. Giving up. %lu seconds",
1312 start
.tv_sec
- fn_start_time
.tv_sec
);
1314 final_pass
= 0; /* turn off sm_msg() from here */
1320 add_ptr_list(&big_statement_stack
, stmt
);
1321 free_expression_stack(&big_expression_stack
);
1322 free_parsed_call_stuff(indent_cnt
== 1);
1323 set_position(stmt
->pos
);
1324 __pass_to_client(stmt
, STMT_HOOK
);
1326 switch (stmt
->type
) {
1327 case STMT_DECLARATION
:
1328 split_declaration(stmt
->declaration
);
1331 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1333 split_ret_value(stmt
->ret_value
);
1334 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1335 __process_post_op_stack();
1338 case STMT_EXPRESSION
:
1339 expr_set_parent_stmt(stmt
->expression
, stmt
);
1340 expr_set_parent_stmt(stmt
->context
, stmt
);
1342 __split_expr(stmt
->expression
);
1345 split_compound(stmt
);
1348 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1349 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1350 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1352 if (known_condition_true(stmt
->if_conditional
)) {
1353 __split_stmt(stmt
->if_true
);
1356 if (known_condition_false(stmt
->if_conditional
)) {
1357 __split_stmt(stmt
->if_false
);
1360 __split_whole_condition(stmt
->if_conditional
);
1361 __split_stmt(stmt
->if_true
);
1362 if (empty_statement(stmt
->if_true
) &&
1363 last_stmt_on_same_line() &&
1364 !get_macro_name(stmt
->if_true
->pos
))
1365 sm_warning("if();");
1366 __push_true_states();
1367 __use_false_states();
1368 __split_stmt(stmt
->if_false
);
1369 __merge_true_states();
1372 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1373 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1374 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1375 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1376 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1378 if (stmt
->iterator_pre_condition
)
1379 handle_pre_loop(stmt
);
1380 else if (stmt
->iterator_post_condition
)
1381 handle_post_loop(stmt
);
1383 // these are for(;;) type loops.
1384 handle_pre_loop(stmt
);
1388 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1389 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1391 if (get_value(stmt
->switch_expression
, &sval
)) {
1392 split_known_switch(stmt
, sval
);
1395 __split_expr(stmt
->switch_expression
);
1396 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1397 __save_switch_states(top_expression(switch_expr_stack
));
1401 __split_stmt(stmt
->switch_statement
);
1402 if (!__pop_default() && have_remaining_cases())
1403 fake_an_empty_default(stmt
->pos
);
1404 __discard_switches();
1406 pop_expression(&switch_expr_stack
);
1412 __split_label_stmt(stmt
);
1413 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1415 __split_stmt(stmt
->label_statement
);
1418 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1420 __split_expr(stmt
->goto_expression
);
1421 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1422 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1424 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1426 __process_continues();
1428 } else if (stmt
->goto_label
&&
1429 stmt
->goto_label
->type
== SYM_LABEL
&&
1430 stmt
->goto_label
->ident
) {
1431 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1434 if (is_last_stmt(stmt
))
1435 handle_backward_goto(stmt
);
1440 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1442 find_asm_gotos(stmt
);
1443 __pass_to_client(stmt
, ASM_HOOK
);
1444 __split_expr(stmt
->asm_string
);
1445 split_asm_ops(stmt
->asm_outputs
);
1446 split_asm_ops(stmt
->asm_inputs
);
1447 split_expr_list(stmt
->asm_clobbers
, NULL
);
1452 __split_expr(stmt
->range_expression
);
1453 __split_expr(stmt
->range_low
);
1454 __split_expr(stmt
->range_high
);
1458 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1459 if (--indent_cnt
== 1)
1460 free_parsed_call_stuff(true);
1463 __process_post_op_stack();
1465 gettimeofday(&stop
, NULL
);
1466 if (option_time_stmt
&& stmt
)
1467 sm_msg("stmt_time%s: %ld",
1468 stmt
->type
== STMT_COMPOUND
? "_block" : "",
1469 stop
.tv_sec
- start
.tv_sec
);
1472 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1474 struct expression
*expr
;
1476 FOR_EACH_PTR(expr_list
, expr
) {
1477 expr_set_parent_expr(expr
, parent
);
1479 __process_post_op_stack();
1480 } END_FOR_EACH_PTR(expr
);
1483 static bool cast_arg(struct symbol
*type
, struct expression
*arg
)
1485 struct symbol
*orig
;
1490 arg
= strip_parens(arg
);
1491 if (arg
!= strip_expr(arg
))
1494 orig
= get_type(arg
);
1497 if (types_equiv(orig
, type
))
1500 if (orig
->type
== SYM_ARRAY
&& type
->type
== SYM_PTR
)
1504 * I would have expected that we could just do use (orig == type) but I
1505 * guess for pointers we need to get the basetype to do that comparison.
1509 if (orig
->type
!= SYM_PTR
||
1510 type
->type
!= SYM_PTR
) {
1511 if (type_fits(type
, orig
))
1515 orig
= get_real_base_type(orig
);
1516 type
= get_real_base_type(type
);
1523 static struct expression
*fake_a_variable_assign(struct symbol
*type
, struct expression
*call
, struct expression
*expr
, int nr
)
1528 if (!expr
|| !cur_func_sym
)
1531 if (already_parsed_call(call
))
1534 if (expr
->type
== EXPR_ASSIGNMENT
)
1537 /* for va_args then we don't know the type */
1539 type
= get_type(expr
);
1541 cast
= cast_arg(type
, expr
);
1543 * Using expr_to_sym() here is a hack. We want to say that we don't
1544 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1545 * It turns out faking these assignments is way more expensive than I
1546 * would have imagined. I'm not sure why exactly.
1551 * if the code is "return *p;" where "p" is a user pointer then
1552 * we want to create a fake assignment so that it sets the state
1553 * in check_kernel_user_data.c.
1556 if (expr
->type
!= EXPR_PREOP
&&
1557 expr
->op
!= '*' && expr
->op
!= '&' &&
1563 snprintf(buf
, sizeof(buf
), "__fake_return_%p", expr
);
1565 snprintf(buf
, sizeof(buf
), "__fake_param_%p_%d", call
, nr
);
1567 return create_fake_assign(buf
, type
, expr
);
1570 static void split_args(struct expression
*expr
)
1572 struct expression
*arg
, *tmp
;
1573 struct symbol
*type
;
1577 FOR_EACH_PTR(expr
->args
, arg
) {
1579 expr_set_parent_expr(arg
, expr
);
1580 type
= get_arg_type(expr
->fn
, i
);
1581 tmp
= fake_a_variable_assign(type
, expr
, arg
, i
);
1583 __in_fake_var_assign
++;
1586 __in_fake_var_assign
--;
1587 __process_post_op_stack();
1588 } END_FOR_EACH_PTR(arg
);
1591 static void split_sym(struct symbol
*sym
)
1595 if (!(sym
->namespace & NS_SYMBOL
))
1598 __split_stmt(sym
->stmt
);
1599 __split_expr(sym
->array_size
);
1600 split_symlist(sym
->arguments
);
1601 split_symlist(sym
->symbol_list
);
1602 __split_stmt(sym
->inline_stmt
);
1603 split_symlist(sym
->inline_symbol_list
);
1606 static void split_symlist(struct symbol_list
*sym_list
)
1610 FOR_EACH_PTR(sym_list
, sym
) {
1612 } END_FOR_EACH_PTR(sym
);
1615 typedef void (fake_cb
)(struct expression
*expr
);
1617 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1619 struct symbol
*type
, *tmp
;
1625 name
= member
->name
;
1627 type
= get_type(expr
);
1628 if (!type
|| type
->type
!= SYM_STRUCT
)
1632 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1636 if (strcmp(name
, tmp
->ident
->name
) == 0)
1638 } END_FOR_EACH_PTR(tmp
);
1642 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1644 struct symbol
*type
, *member
;
1647 type
= get_type(expr
);
1648 if (!type
|| type
->type
!= SYM_STRUCT
)
1651 FOR_EACH_PTR(type
->symbol_list
, member
) {
1653 return member
->ident
;
1655 } END_FOR_EACH_PTR(member
);
1659 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1661 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1663 struct expression
*edge_member
, *assign
;
1664 struct symbol
*base
= get_real_base_type(member
);
1668 expr
= member_expression(expr
, '.', member
->ident
);
1670 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1671 struct symbol
*type
;
1673 type
= get_real_base_type(tmp
);
1677 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1678 if (get_extra_state(edge_member
))
1681 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1682 set_inner_struct_members(expr
, tmp
);
1689 assign
= assign_expression(edge_member
, '=', zero_expr());
1690 __split_expr(assign
);
1691 } END_FOR_EACH_PTR(tmp
);
1696 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1699 struct expression
*member
= NULL
;
1700 struct expression
*assign
;
1702 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1703 type
= get_real_base_type(tmp
);
1708 member
= member_expression(expr
, '.', tmp
->ident
);
1709 if (get_extra_state(member
))
1713 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1714 set_inner_struct_members(expr
, tmp
);
1717 if (type
->type
== SYM_ARRAY
)
1722 assign
= assign_expression(member
, '=', zero_expr());
1723 __split_expr(assign
);
1724 } END_FOR_EACH_PTR(tmp
);
1727 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1729 struct expression
*deref
, *assign
, *tmp
, *right
;
1730 struct symbol
*struct_type
, *type
;
1731 struct ident
*member
;
1734 struct_type
= get_type(symbol
);
1736 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1740 * We're parsing an initializer that could look something like this:
1741 * struct foo foo = {
1743 * .whatever.xxx = 11,
1747 * So what we have here is a list with 42, .whatever, and .zzz. We need
1748 * to break it up into left and right sides of the assignments.
1752 FOR_EACH_PTR(members
, tmp
) {
1754 if (tmp
->type
== EXPR_IDENTIFIER
) {
1755 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1756 while (tmp
->type
== EXPR_IDENTIFIER
) {
1757 member
= tmp
->expr_ident
;
1758 tmp
= tmp
->ident_expression
;
1760 deref
= member_expression(deref
, '.', member
);
1762 deref
= member_expression(symbol
, '.', member
);
1765 member
= number_to_member(symbol
, member_idx
);
1766 deref
= member_expression(symbol
, '.', member
);
1770 if (right
->type
== EXPR_INITIALIZER
) {
1771 type
= get_type(deref
);
1772 if (type
&& type
->type
== SYM_ARRAY
)
1773 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1775 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1777 assign
= assign_expression(deref
, '=', right
);
1780 } END_FOR_EACH_PTR(tmp
);
1782 set_unset_to_zero(struct_type
, symbol
);
1785 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1787 fake_member_assigns_helper(symbol_expression(sym
),
1788 sym
->initializer
->expr_list
, fake_cb
);
1791 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1793 struct expression
*offset
, *binop
, *assign
, *tmp
;
1794 struct symbol
*type
;
1797 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1802 FOR_EACH_PTR(expr_list
, tmp
) {
1803 if (tmp
->type
== EXPR_INDEX
) {
1804 if (tmp
->idx_from
!= tmp
->idx_to
)
1806 idx
= tmp
->idx_from
;
1809 if (!tmp
->idx_expression
)
1811 tmp
= tmp
->idx_expression
;
1813 offset
= value_expr(idx
);
1814 binop
= array_element_expression(array
, offset
);
1815 if (tmp
->type
== EXPR_INITIALIZER
) {
1816 type
= get_type(binop
);
1817 if (type
&& type
->type
== SYM_ARRAY
)
1818 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1820 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1822 assign
= assign_expression(binop
, '=', tmp
);
1829 } END_FOR_EACH_PTR(tmp
);
1831 __call_array_initialized_hooks(array
, max
);
1834 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1836 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1839 static void fake_assign_expr(struct symbol
*sym
)
1841 struct expression
*assign
, *symbol
;
1843 symbol
= symbol_expression(sym
);
1844 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1845 __split_expr(assign
);
1848 static void do_initializer_stuff(struct symbol
*sym
)
1850 if (!sym
->initializer
)
1853 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1854 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1855 fake_element_assigns(sym
, __split_expr
);
1857 fake_member_assigns(sym
, __split_expr
);
1859 fake_assign_expr(sym
);
1863 static void split_declaration(struct symbol_list
*sym_list
)
1867 FOR_EACH_PTR(sym_list
, sym
) {
1868 __pass_to_client(sym
, DECLARATION_HOOK
);
1869 do_initializer_stuff(sym
);
1870 __pass_to_client(sym
, DECLARATION_HOOK_AFTER
);
1872 } END_FOR_EACH_PTR(sym
);
1875 static void call_global_assign_hooks(struct expression
*assign
)
1877 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1880 static void fake_global_assign(struct symbol
*sym
)
1882 struct expression
*assign
, *symbol
;
1884 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1885 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1886 fake_element_assigns(sym
, call_global_assign_hooks
);
1887 } else if (sym
->initializer
) {
1888 symbol
= symbol_expression(sym
);
1889 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1890 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1892 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1894 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1895 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1896 fake_member_assigns(sym
, call_global_assign_hooks
);
1897 } else if (sym
->initializer
) {
1898 symbol
= symbol_expression(sym
);
1899 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1900 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1902 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1905 symbol
= symbol_expression(sym
);
1906 if (sym
->initializer
) {
1907 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1908 __split_expr(assign
);
1910 assign
= assign_expression(symbol
, '=', zero_expr());
1912 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1916 static void start_function_definition(struct symbol
*sym
)
1918 __in_function_def
= 1;
1919 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1920 __in_function_def
= 0;
1921 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1925 void add_function_data(unsigned long *fn_data
)
1927 __add_ptr_list(&fn_data_list
, fn_data
);
1930 static void clear_function_data(void)
1934 FOR_EACH_PTR(fn_data_list
, tmp
) {
1936 } END_FOR_EACH_PTR(tmp
);
1939 static void record_func_time(void)
1941 struct timeval stop
;
1945 gettimeofday(&stop
, NULL
);
1946 func_time
= stop
.tv_sec
- fn_start_time
.tv_sec
;
1947 snprintf(buf
, sizeof(buf
), "%d", func_time
);
1948 sql_insert_return_implies(FUNC_TIME
, 0, "", buf
);
1949 if (option_time
&& func_time
> 2) {
1951 sm_msg("func_time: %d", func_time
);
1956 static void split_function(struct symbol
*sym
)
1958 struct symbol
*base_type
= get_base_type(sym
);
1960 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1963 gettimeofday(&outer_fn_start_time
, NULL
);
1964 gettimeofday(&fn_start_time
, NULL
);
1967 cur_func
= sym
->ident
->name
;
1968 if (option_process_function
&& cur_func
&&
1969 strcmp(option_process_function
, cur_func
) != 0)
1971 set_position(sym
->pos
);
1972 clear_function_data();
1974 last_goto_statement_handled
= 0;
1975 sm_debug("new function: %s\n", cur_func
);
1977 if (option_two_passes
) {
1981 start_function_definition(sym
);
1982 __split_stmt(base_type
->stmt
);
1983 __split_stmt(base_type
->inline_stmt
);
1989 start_function_definition(sym
);
1990 __split_stmt(base_type
->stmt
);
1991 __split_stmt(base_type
->inline_stmt
);
1992 if (!__path_is_null() &&
1993 cur_func_return_type() == &void_ctype
&&
1994 !__bail_on_rest_of_function
) {
1995 __pass_to_client(NULL
, RETURN_HOOK
);
1998 __pass_to_client(sym
, END_FUNC_HOOK
);
1999 if (need_delayed_scope_hooks())
2000 __call_scope_hooks();
2001 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
2008 cur_func_sym
= NULL
;
2010 free_data_info_allocs();
2011 free_expression_stack(&switch_expr_stack
);
2012 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
2013 __bail_on_rest_of_function
= 0;
2016 static void save_flow_state(void)
2020 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2));
2021 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2));
2022 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2));
2024 __add_ptr_list(&backup
, big_statement_stack
);
2025 __add_ptr_list(&backup
, big_expression_stack
);
2026 __add_ptr_list(&backup
, big_condition_stack
);
2027 __add_ptr_list(&backup
, switch_expr_stack
);
2029 __add_ptr_list(&backup
, cur_func_sym
);
2031 __add_ptr_list(&backup
, parsed_calls
);
2033 __add_ptr_list(&backup
, __prev_stmt
);
2034 __add_ptr_list(&backup
, __cur_stmt
);
2035 __add_ptr_list(&backup
, __next_stmt
);
2037 FOR_EACH_PTR(fn_data_list
, tmp
) {
2038 __add_ptr_list(&backup
, (void *)*tmp
);
2039 } END_FOR_EACH_PTR(tmp
);
2042 static void *pop_backup(void)
2046 ret
= last_ptr_list(backup
);
2047 delete_ptr_list_last(&backup
);
2051 static void restore_flow_state(void)
2055 FOR_EACH_PTR_REVERSE(fn_data_list
, tmp
) {
2056 *tmp
= (unsigned long)pop_backup();
2057 } END_FOR_EACH_PTR_REVERSE(tmp
);
2059 __next_stmt
= pop_backup();
2060 __cur_stmt
= pop_backup();
2061 __prev_stmt
= pop_backup();
2063 parsed_calls
= pop_backup();
2065 cur_func_sym
= pop_backup();
2066 switch_expr_stack
= pop_backup();
2067 big_condition_stack
= pop_backup();
2068 big_expression_stack
= pop_backup();
2069 big_statement_stack
= pop_backup();
2070 final_pass
= PTR_INT(pop_backup()) >> 2;
2071 loop_count
= PTR_INT(pop_backup()) >> 2;
2072 loop_num
= PTR_INT(pop_backup()) >> 2;
2075 void parse_inline(struct expression
*call
)
2077 struct symbol
*base_type
;
2078 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
2079 struct timeval time_backup
= fn_start_time
;
2080 struct expression
*orig_inline
= __inline_fn
;
2083 if (out_of_memory() || taking_too_long())
2086 if (already_parsed_call(call
))
2091 __pass_to_client(call
, INLINE_FN_START
);
2092 final_pass
= 0; /* don't print anything */
2094 orig_budget
= inline_budget
;
2095 inline_budget
= inline_budget
- 5;
2097 base_type
= get_base_type(call
->fn
->symbol
);
2098 cur_func_sym
= call
->fn
->symbol
;
2099 if (call
->fn
->symbol
->ident
)
2100 cur_func
= call
->fn
->symbol
->ident
->name
;
2103 set_position(call
->fn
->symbol
->pos
);
2106 big_statement_stack
= NULL
;
2107 big_expression_stack
= NULL
;
2108 big_condition_stack
= NULL
;
2109 switch_expr_stack
= NULL
;
2110 parsed_calls
= NULL
;
2112 sm_debug("inline function: %s\n", cur_func
);
2114 clear_function_data();
2117 start_function_definition(call
->fn
->symbol
);
2118 __split_stmt(base_type
->stmt
);
2119 __split_stmt(base_type
->inline_stmt
);
2120 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
2121 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
2122 call
->fn
->symbol
->parsed
= true;
2124 free_expression_stack(&switch_expr_stack
);
2125 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
2129 restore_flow_state();
2130 fn_start_time
= time_backup
;
2131 cur_func
= cur_func_bak
;
2133 restore_all_states();
2134 set_position(call
->pos
);
2135 __inline_fn
= orig_inline
;
2136 inline_budget
= orig_budget
;
2137 __pass_to_client(call
, INLINE_FN_END
);
2140 static struct symbol_list
*inlines_called
;
2141 static void add_inline_function(struct symbol
*sym
)
2143 static struct symbol_list
*already_added
;
2146 FOR_EACH_PTR(already_added
, tmp
) {
2149 } END_FOR_EACH_PTR(tmp
);
2151 add_ptr_list(&already_added
, sym
);
2152 add_ptr_list(&inlines_called
, sym
);
2155 static void process_inlines(void)
2159 FOR_EACH_PTR(inlines_called
, tmp
) {
2160 split_function(tmp
);
2161 } END_FOR_EACH_PTR(tmp
);
2162 free_ptr_list(&inlines_called
);
2165 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
2169 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
2172 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
2174 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
2176 } END_FOR_EACH_PTR_REVERSE(sym
);
2181 static bool interesting_function(struct symbol
*sym
)
2183 static int prev_stream
= -1;
2184 static bool prev_answer
;
2185 const char *filename
;
2188 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
2191 if (sym
->pos
.stream
== prev_stream
)
2194 prev_stream
= sym
->pos
.stream
;
2195 prev_answer
= false;
2197 filename
= stream_name(sym
->pos
.stream
);
2198 len
= strlen(filename
);
2199 if (len
> 0 && filename
[len
- 1] == 'c')
2204 static void split_inlines_in_scope(struct symbol
*sym
)
2206 struct symbol
*base
;
2207 struct symbol_list
*scope_list
;
2210 scope_list
= sym
->scope
->symbols
;
2211 stream
= sym
->pos
.stream
;
2213 /* find the last static symbol in the file */
2214 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
2215 if (sym
->pos
.stream
!= stream
)
2217 if (sym
->type
!= SYM_NODE
)
2219 base
= get_base_type(sym
);
2222 if (base
->type
!= SYM_FN
)
2224 if (!base
->inline_stmt
)
2226 if (!interesting_function(sym
))
2228 add_inline_function(sym
);
2229 } END_FOR_EACH_PTR_REVERSE(sym
);
2234 static void split_inlines(struct symbol_list
*sym_list
)
2238 sym
= get_last_scoped_symbol(sym_list
, 0);
2240 split_inlines_in_scope(sym
);
2241 sym
= get_last_scoped_symbol(sym_list
, 1);
2243 split_inlines_in_scope(sym
);
2246 static struct stree
*clone_estates_perm(struct stree
*orig
)
2248 struct stree
*ret
= NULL
;
2249 struct sm_state
*tmp
;
2251 FOR_EACH_SM(orig
, tmp
) {
2252 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
2253 } END_FOR_EACH_SM(tmp
);
2258 struct position last_pos
;
2259 static void split_c_file_functions(struct symbol_list
*sym_list
)
2264 FOR_EACH_PTR(sym_list
, sym
) {
2265 set_position(sym
->pos
);
2266 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
2267 __pass_to_client(sym
, BASE_HOOK
);
2268 fake_global_assign(sym
);
2269 __pass_to_client(sym
, DECLARATION_HOOK_AFTER
);
2271 } END_FOR_EACH_PTR(sym
);
2272 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
2275 FOR_EACH_PTR(sym_list
, sym
) {
2276 set_position(sym
->pos
);
2277 last_pos
= sym
->pos
;
2278 if (!interesting_function(sym
))
2280 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
2281 split_function(sym
);
2284 last_pos
= sym
->pos
;
2285 } END_FOR_EACH_PTR(sym
);
2286 split_inlines(sym_list
);
2287 __pass_to_client(sym_list
, END_FILE_HOOK
);
2290 static int final_before_fake
;
2291 void init_fake_env(void)
2294 final_before_fake
= final_pass
;
2296 __push_fake_cur_stree();
2300 void end_fake_env(void)
2302 __free_fake_cur_stree();
2305 final_pass
= final_before_fake
;
2308 static void open_output_files(char *base_file
)
2312 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
2313 sm_outfd
= fopen(buf
, "w");
2315 sm_fatal("Cannot open %s", buf
);
2320 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
2321 sql_outfd
= fopen(buf
, "w");
2323 sm_fatal("Error: Cannot open %s", buf
);
2325 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
2326 caller_info_fd
= fopen(buf
, "w");
2327 if (!caller_info_fd
)
2328 sm_fatal("Error: Cannot open %s", buf
);
2331 void smatch(struct string_list
*filelist
)
2333 struct symbol_list
*sym_list
;
2334 struct timeval stop
, start
;
2338 gettimeofday(&start
, NULL
);
2340 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
2341 path
= getcwd(NULL
, 0);
2342 free(full_base_file
);
2344 len
= strlen(path
) + 1 + strlen(base_file
) + 1;
2345 full_base_file
= malloc(len
);
2346 snprintf(full_base_file
, len
, "%s/%s", path
, base_file
);
2348 full_base_file
= alloc_string(base_file
);
2350 if (option_file_output
)
2351 open_output_files(base_file
);
2352 base_file_stream
= input_stream_nr
;
2353 sym_list
= sparse_keep_tokens(base_file
);
2354 split_c_file_functions(sym_list
);
2355 } END_FOR_EACH_PTR_NOTAG(base_file
);
2357 gettimeofday(&stop
, NULL
);
2359 set_position(last_pos
);
2362 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
2364 sm_msg("mem: %luKb", get_max_memory());