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
);
801 static struct bool_stmt_fn_list
*once_through_hooks
;
802 void add_once_through_hook(bool_stmt_func
*fn
)
804 add_ptr_list(&once_through_hooks
, fn
);
807 static bool call_once_through_hooks(struct statement
*stmt
)
811 if (implied_condition_true(stmt
->iterator_pre_condition
))
813 if (option_assume_loops
)
816 FOR_EACH_PTR(once_through_hooks
, fn
) {
819 } END_FOR_EACH_PTR(fn
);
825 * Pre Loops are while and for loops.
827 static void handle_pre_loop(struct statement
*stmt
)
829 int once_through
; /* we go through the loop at least once */
830 struct sm_state
*extra_sm
= NULL
;
833 struct stree
*stree
= NULL
;
834 struct sm_state
*sm
= NULL
;
836 loop_name
= get_loop_name(loop_num
);
839 split_declaration(stmt
->iterator_syms
);
840 if (stmt
->iterator_pre_statement
) {
841 __split_stmt(stmt
->iterator_pre_statement
);
842 __prev_stmt
= stmt
->iterator_pre_statement
;
845 once_through
= call_once_through_hooks(stmt
);
851 __merge_gotos(loop_name
, NULL
);
853 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
854 __in_pre_condition
++;
855 __pass_to_client(stmt
, PRELOOP_HOOK
);
856 __split_whole_condition(stmt
->iterator_pre_condition
);
857 __in_pre_condition
--;
858 FOR_EACH_SM(stree
, sm
) {
859 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
860 } END_FOR_EACH_SM(sm
);
863 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
865 __split_stmt(stmt
->iterator_statement
);
866 if (is_forever_loop(stmt
)) {
868 __save_gotos(loop_name
, NULL
);
870 __push_fake_cur_stree();
871 __split_stmt(stmt
->iterator_post_statement
);
872 stree
= __pop_fake_cur_stree();
874 __discard_false_states();
875 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
878 if (!__path_is_null())
879 __merge_stree_into_cur(stree
);
883 unchanged
= __iterator_unchanged(extra_sm
);
884 __split_stmt(stmt
->iterator_post_statement
);
885 __prev_stmt
= stmt
->iterator_post_statement
;
888 __save_gotos(loop_name
, NULL
);
889 __in_pre_condition
++;
890 __split_whole_condition(stmt
->iterator_pre_condition
);
891 __in_pre_condition
--;
893 __merge_false_states();
895 __discard_false_states();
897 __merge_false_states();
899 if (extra_sm
&& unchanged
)
900 __extra_pre_loop_hook_after(extra_sm
,
901 stmt
->iterator_post_statement
,
902 stmt
->iterator_pre_condition
);
903 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
910 * Post loops are do {} while();
912 static void handle_post_loop(struct statement
*stmt
)
916 loop_name
= get_loop_name(loop_num
);
920 __pass_to_client(stmt
, POSTLOOP_HOOK
);
924 __merge_gotos(loop_name
, NULL
);
925 __split_stmt(stmt
->iterator_statement
);
927 if (!expr_is_zero(stmt
->iterator_post_condition
))
928 __save_gotos(loop_name
, NULL
);
930 if (is_forever_loop(stmt
)) {
931 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
934 __split_whole_condition(stmt
->iterator_post_condition
);
935 __use_false_states();
936 __pass_to_client(stmt
, AFTER_LOOP_NO_BREAKS
);
942 static int empty_statement(struct statement
*stmt
)
946 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
951 static int last_stmt_on_same_line(void)
953 struct statement
*stmt
;
956 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
959 if (stmt
->pos
.line
== get_lineno())
962 } END_FOR_EACH_PTR_REVERSE(stmt
);
966 static void split_asm_ops(struct asm_operand_list
*ops
)
968 struct asm_operand
*op
;
970 FOR_EACH_PTR(ops
, op
) {
971 __split_expr(op
->expr
);
972 } END_FOR_EACH_PTR(op
);
975 static int is_case_val(struct statement
*stmt
, sval_t sval
)
979 if (stmt
->type
!= STMT_CASE
)
981 if (!stmt
->case_expression
) {
985 if (!get_value(stmt
->case_expression
, &case_sval
))
987 if (case_sval
.value
== sval
.value
)
992 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
993 struct expression
*case_expr
,
994 struct expression
*case_to
)
997 struct range_list
*rl
= NULL
;
998 struct symbol
*switch_type
;
1000 switch_type
= get_type(switch_expr
);
1001 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
1002 start
= sval_cast(switch_type
, start
);
1003 end
= sval_cast(switch_type
, end
);
1004 add_range(&rl
, start
, end
);
1005 } else if (get_value(case_expr
, &start
)) {
1006 start
= sval_cast(switch_type
, start
);
1007 add_range(&rl
, start
, start
);
1013 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
1015 struct statement
*tmp
;
1016 struct range_list
*rl
;
1018 __split_expr(stmt
->switch_expression
);
1019 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
1021 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1022 __save_switch_states(top_expression(switch_expr_stack
));
1027 stmt
= stmt
->switch_statement
;
1029 __push_scope_hooks();
1030 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
1031 __smatch_lineno
= tmp
->pos
.line
;
1032 // FIXME: what if default comes before the known case statement?
1033 if (is_case_val(tmp
, sval
)) {
1034 rl
= alloc_rl(sval
, sval
);
1035 __merge_switches(top_expression(switch_expr_stack
), rl
);
1036 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
1037 stmt_set_parent_stmt(tmp
->case_statement
, tmp
);
1038 __split_stmt(tmp
->case_statement
);
1041 if (__path_is_null())
1045 if (__path_is_null()) {
1049 } END_FOR_EACH_PTR(tmp
);
1051 __call_scope_hooks();
1052 if (!__pop_default())
1053 __merge_switches(top_expression(switch_expr_stack
), NULL
);
1054 __discard_switches();
1056 pop_expression(&switch_expr_stack
);
1059 static void split_case(struct statement
*stmt
)
1061 struct range_list
*rl
= NULL
;
1063 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
1064 expr_set_parent_stmt(stmt
->case_to
, stmt
);
1066 rl
= get_case_rl(top_expression(switch_expr_stack
),
1067 stmt
->case_expression
, stmt
->case_to
);
1068 while (stmt
->case_statement
->type
== STMT_CASE
) {
1069 struct range_list
*tmp
;
1071 tmp
= get_case_rl(top_expression(switch_expr_stack
),
1072 stmt
->case_statement
->case_expression
,
1073 stmt
->case_statement
->case_to
);
1076 rl
= rl_union(rl
, tmp
);
1077 if (!stmt
->case_expression
)
1080 stmt
= stmt
->case_statement
;
1083 __merge_switches(top_expression(switch_expr_stack
), rl
);
1085 if (!stmt
->case_expression
)
1088 stmt_set_parent_stmt(stmt
->case_statement
, stmt
);
1089 __split_stmt(stmt
->case_statement
);
1092 int time_parsing_function(void)
1094 return ms_since(&fn_start_time
) / 1000;
1097 bool taking_too_long(void)
1099 if ((ms_since(&outer_fn_start_time
) / 1000) > 60 * 5) /* five minutes */
1104 struct statement
*get_last_stmt(void)
1107 struct statement
*stmt
;
1109 fn
= get_base_type(cur_func_sym
);
1114 stmt
= fn
->inline_stmt
;
1115 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
1117 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
1118 if (stmt
&& stmt
->type
== STMT_LABEL
)
1119 stmt
= stmt
->label_statement
;
1123 int is_last_stmt(struct statement
*cur_stmt
)
1125 struct statement
*last
;
1127 last
= get_last_stmt();
1128 if (last
&& last
== cur_stmt
)
1133 static void handle_backward_goto(struct statement
*goto_stmt
)
1135 const char *goto_name
, *label_name
;
1136 struct statement
*func_stmt
;
1137 struct symbol
*base_type
= get_base_type(cur_func_sym
);
1138 struct statement
*tmp
;
1143 if (last_goto_statement_handled
)
1145 last_goto_statement_handled
= 1;
1147 if (!goto_stmt
->goto_label
||
1148 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
1149 !goto_stmt
->goto_label
->ident
)
1151 goto_name
= goto_stmt
->goto_label
->ident
->name
;
1153 func_stmt
= base_type
->stmt
;
1155 func_stmt
= base_type
->inline_stmt
;
1158 if (func_stmt
->type
!= STMT_COMPOUND
)
1161 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
1163 if (tmp
->type
!= STMT_LABEL
)
1165 if (!tmp
->label_identifier
||
1166 tmp
->label_identifier
->type
!= SYM_LABEL
||
1167 !tmp
->label_identifier
->ident
)
1169 label_name
= tmp
->label_identifier
->ident
->name
;
1170 if (strcmp(goto_name
, label_name
) != 0)
1175 } END_FOR_EACH_PTR(tmp
);
1178 static void fake_a_return(void)
1180 struct expression
*ret
= NULL
;
1185 if (cur_func_return_type() != &void_ctype
)
1186 ret
= unknown_value_expression(NULL
);
1188 __pass_to_client(ret
, RETURN_HOOK
);
1192 static void split_ret_value(struct expression
*expr
)
1194 struct symbol
*type
;
1199 type
= get_real_base_type(cur_func_sym
);
1200 type
= get_real_base_type(type
);
1201 expr
= fake_a_variable_assign(type
, NULL
, expr
, -1);
1203 __in_fake_var_assign
++;
1205 __in_fake_var_assign
--;
1208 static void fake_an_empty_default(struct position pos
)
1210 static struct statement none
= {};
1213 none
.type
= STMT_NONE
;
1214 __merge_switches(top_expression(switch_expr_stack
), NULL
);
1215 __split_stmt(&none
);
1218 static void split_compound(struct statement
*stmt
)
1220 struct statement
*prev
= NULL
;
1221 struct statement
*cur
= NULL
;
1222 struct statement
*next
;
1224 __push_scope_hooks();
1226 FOR_EACH_PTR(stmt
->stmts
, next
) {
1227 /* just set them all ahead of time */
1228 stmt_set_parent_stmt(next
, stmt
);
1238 } END_FOR_EACH_PTR(next
);
1247 * For function scope, then delay calling the scope hooks until the
1248 * end of function hooks can run. I'm not positive this is the right
1251 if (!is_last_stmt(cur
))
1252 __call_scope_hooks();
1256 * This is a hack, work around for detecting empty functions.
1258 static int need_delayed_scope_hooks(void)
1260 struct symbol
*fn
= get_base_type(cur_func_sym
);
1261 struct statement
*stmt
;
1267 stmt
= fn
->inline_stmt
;
1268 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
1273 void __split_label_stmt(struct statement
*stmt
)
1275 if (stmt
->label_identifier
&&
1276 stmt
->label_identifier
->type
== SYM_LABEL
&&
1277 stmt
->label_identifier
->ident
) {
1278 loop_count
|= 0x0800000;
1279 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
1283 static void find_asm_gotos(struct statement
*stmt
)
1287 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
1288 __save_gotos(sym
->ident
->name
, sym
);
1289 } END_FOR_EACH_PTR(sym
);
1292 static bool already_parsed_call(struct expression
*call
)
1294 struct expression
*expr
;
1296 FOR_EACH_PTR(parsed_calls
, expr
) {
1299 } END_FOR_EACH_PTR(expr
);
1303 static void free_parsed_call_stuff(bool free_fake_states
)
1305 free_expression_stack(&parsed_calls
);
1306 if (free_fake_states
)
1307 __discard_fake_states(NULL
);
1310 void __split_stmt(struct statement
*stmt
)
1313 struct timeval start
, stop
;
1314 bool skip_after
= false;
1316 gettimeofday(&start
, NULL
);
1321 if (!__in_fake_assign
)
1322 __silence_warnings_for_stmt
= false;
1324 if (__bail_on_rest_of_function
|| is_skipped_function())
1327 if (out_of_memory() || taking_too_long()) {
1328 gettimeofday(&start
, NULL
);
1330 __bail_on_rest_of_function
= 1;
1332 sm_perror("Function too hairy. Giving up. %lu seconds",
1333 start
.tv_sec
- fn_start_time
.tv_sec
);
1335 final_pass
= 0; /* turn off sm_msg() from here */
1341 add_ptr_list(&big_statement_stack
, stmt
);
1342 free_expression_stack(&big_expression_stack
);
1343 free_parsed_call_stuff(indent_cnt
== 1);
1344 set_position(stmt
->pos
);
1345 __pass_to_client(stmt
, STMT_HOOK
);
1347 switch (stmt
->type
) {
1348 case STMT_DECLARATION
:
1349 split_declaration(stmt
->declaration
);
1352 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
1354 split_ret_value(stmt
->ret_value
);
1355 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
1356 __process_post_op_stack();
1359 case STMT_EXPRESSION
:
1360 expr_set_parent_stmt(stmt
->expression
, stmt
);
1361 expr_set_parent_stmt(stmt
->context
, stmt
);
1363 __split_expr(stmt
->expression
);
1366 split_compound(stmt
);
1369 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1370 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1371 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1373 if (known_condition_true(stmt
->if_conditional
)) {
1374 __split_stmt(stmt
->if_true
);
1377 if (known_condition_false(stmt
->if_conditional
)) {
1378 __split_stmt(stmt
->if_false
);
1381 __split_whole_condition(stmt
->if_conditional
);
1382 __split_stmt(stmt
->if_true
);
1383 if (empty_statement(stmt
->if_true
) &&
1384 last_stmt_on_same_line() &&
1385 !get_macro_name(stmt
->if_true
->pos
))
1386 sm_warning("if();");
1387 __push_true_states();
1388 __use_false_states();
1389 __split_stmt(stmt
->if_false
);
1390 __merge_true_states();
1393 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1394 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1395 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1396 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1397 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1399 if (stmt
->iterator_pre_condition
)
1400 handle_pre_loop(stmt
);
1401 else if (stmt
->iterator_post_condition
)
1402 handle_post_loop(stmt
);
1404 // these are for(;;) type loops.
1405 handle_pre_loop(stmt
);
1409 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1410 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1412 if (get_value(stmt
->switch_expression
, &sval
)) {
1413 split_known_switch(stmt
, sval
);
1416 __split_expr(stmt
->switch_expression
);
1417 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1418 __save_switch_states(top_expression(switch_expr_stack
));
1422 __split_stmt(stmt
->switch_statement
);
1423 if (!__pop_default() && have_remaining_cases())
1424 fake_an_empty_default(stmt
->pos
);
1425 __discard_switches();
1427 pop_expression(&switch_expr_stack
);
1433 __split_label_stmt(stmt
);
1434 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1436 __split_stmt(stmt
->label_statement
);
1439 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1441 __split_expr(stmt
->goto_expression
);
1442 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1443 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1445 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1447 __process_continues();
1449 } else if (stmt
->goto_label
&&
1450 stmt
->goto_label
->type
== SYM_LABEL
&&
1451 stmt
->goto_label
->ident
) {
1452 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1455 if (is_last_stmt(stmt
))
1456 handle_backward_goto(stmt
);
1461 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1463 find_asm_gotos(stmt
);
1464 __pass_to_client(stmt
, ASM_HOOK
);
1465 __split_expr(stmt
->asm_string
);
1466 split_asm_ops(stmt
->asm_outputs
);
1467 split_asm_ops(stmt
->asm_inputs
);
1468 split_expr_list(stmt
->asm_clobbers
, NULL
);
1473 __split_expr(stmt
->range_expression
);
1474 __split_expr(stmt
->range_low
);
1475 __split_expr(stmt
->range_high
);
1479 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1480 if (--indent_cnt
== 1)
1481 free_parsed_call_stuff(true);
1484 __process_post_op_stack();
1486 gettimeofday(&stop
, NULL
);
1487 if (option_time_stmt
&& stmt
)
1488 sm_msg("stmt_time%s: %ld",
1489 stmt
->type
== STMT_COMPOUND
? "_block" : "",
1490 stop
.tv_sec
- start
.tv_sec
);
1493 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1495 struct expression
*expr
;
1497 FOR_EACH_PTR(expr_list
, expr
) {
1498 expr_set_parent_expr(expr
, parent
);
1500 __process_post_op_stack();
1501 } END_FOR_EACH_PTR(expr
);
1504 static bool cast_arg(struct symbol
*type
, struct expression
*arg
)
1506 struct symbol
*orig
;
1511 arg
= strip_parens(arg
);
1512 if (arg
!= strip_expr(arg
))
1515 orig
= get_type(arg
);
1518 if (types_equiv(orig
, type
))
1521 if (orig
->type
== SYM_ARRAY
&& type
->type
== SYM_PTR
)
1525 * I would have expected that we could just do use (orig == type) but I
1526 * guess for pointers we need to get the basetype to do that comparison.
1530 if (orig
->type
!= SYM_PTR
||
1531 type
->type
!= SYM_PTR
) {
1532 if (type_fits(type
, orig
))
1536 orig
= get_real_base_type(orig
);
1537 type
= get_real_base_type(type
);
1544 static struct expression
*fake_a_variable_assign(struct symbol
*type
, struct expression
*call
, struct expression
*expr
, int nr
)
1549 if (!expr
|| !cur_func_sym
)
1552 if (already_parsed_call(call
))
1555 if (expr
->type
== EXPR_ASSIGNMENT
)
1558 /* for va_args then we don't know the type */
1560 type
= get_type(expr
);
1562 cast
= cast_arg(type
, expr
);
1564 * Using expr_to_sym() here is a hack. We want to say that we don't
1565 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1566 * It turns out faking these assignments is way more expensive than I
1567 * would have imagined. I'm not sure why exactly.
1572 * if the code is "return *p;" where "p" is a user pointer then
1573 * we want to create a fake assignment so that it sets the state
1574 * in check_kernel_user_data.c.
1577 if (expr
->type
!= EXPR_PREOP
&&
1578 expr
->op
!= '*' && expr
->op
!= '&' &&
1584 snprintf(buf
, sizeof(buf
), "__fake_return_%p", expr
);
1586 snprintf(buf
, sizeof(buf
), "__fake_param_%p_%d", call
, nr
);
1588 return create_fake_assign(buf
, type
, expr
);
1591 static void split_args(struct expression
*expr
)
1593 struct expression
*arg
, *tmp
;
1594 struct symbol
*type
;
1598 FOR_EACH_PTR(expr
->args
, arg
) {
1600 expr_set_parent_expr(arg
, expr
);
1601 type
= get_arg_type(expr
->fn
, i
);
1602 tmp
= fake_a_variable_assign(type
, expr
, arg
, i
);
1604 __in_fake_var_assign
++;
1607 __in_fake_var_assign
--;
1608 __process_post_op_stack();
1609 } END_FOR_EACH_PTR(arg
);
1612 static void split_sym(struct symbol
*sym
)
1616 if (!(sym
->namespace & NS_SYMBOL
))
1619 __split_stmt(sym
->stmt
);
1620 __split_expr(sym
->array_size
);
1621 split_symlist(sym
->arguments
);
1622 split_symlist(sym
->symbol_list
);
1623 __split_stmt(sym
->inline_stmt
);
1624 split_symlist(sym
->inline_symbol_list
);
1627 static void split_symlist(struct symbol_list
*sym_list
)
1631 FOR_EACH_PTR(sym_list
, sym
) {
1633 } END_FOR_EACH_PTR(sym
);
1636 typedef void (fake_cb
)(struct expression
*expr
);
1638 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1640 struct symbol
*type
, *tmp
;
1646 name
= member
->name
;
1648 type
= get_type(expr
);
1649 if (!type
|| type
->type
!= SYM_STRUCT
)
1653 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1657 if (strcmp(name
, tmp
->ident
->name
) == 0)
1659 } END_FOR_EACH_PTR(tmp
);
1663 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1665 struct symbol
*type
, *member
;
1668 type
= get_type(expr
);
1669 if (!type
|| type
->type
!= SYM_STRUCT
)
1672 FOR_EACH_PTR(type
->symbol_list
, member
) {
1674 return member
->ident
;
1676 } END_FOR_EACH_PTR(member
);
1680 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1682 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1684 struct expression
*edge_member
, *assign
;
1685 struct symbol
*base
= get_real_base_type(member
);
1689 expr
= member_expression(expr
, '.', member
->ident
);
1691 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1692 struct symbol
*type
;
1694 type
= get_real_base_type(tmp
);
1698 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1699 if (get_extra_state(edge_member
))
1702 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1703 set_inner_struct_members(expr
, tmp
);
1710 assign
= assign_expression(edge_member
, '=', zero_expr());
1711 __split_expr(assign
);
1712 } END_FOR_EACH_PTR(tmp
);
1717 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1720 struct expression
*member
= NULL
;
1721 struct expression
*assign
;
1723 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1724 type
= get_real_base_type(tmp
);
1729 member
= member_expression(expr
, '.', tmp
->ident
);
1730 if (get_extra_state(member
))
1734 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1735 set_inner_struct_members(expr
, tmp
);
1738 if (type
->type
== SYM_ARRAY
)
1743 assign
= assign_expression(member
, '=', zero_expr());
1744 __split_expr(assign
);
1745 } END_FOR_EACH_PTR(tmp
);
1748 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1750 struct expression
*deref
, *assign
, *tmp
, *right
;
1751 struct symbol
*struct_type
, *type
;
1752 struct ident
*member
;
1755 struct_type
= get_type(symbol
);
1757 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1761 * We're parsing an initializer that could look something like this:
1762 * struct foo foo = {
1764 * .whatever.xxx = 11,
1768 * So what we have here is a list with 42, .whatever, and .zzz. We need
1769 * to break it up into left and right sides of the assignments.
1773 FOR_EACH_PTR(members
, tmp
) {
1775 if (tmp
->type
== EXPR_IDENTIFIER
) {
1776 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1777 while (tmp
->type
== EXPR_IDENTIFIER
) {
1778 member
= tmp
->expr_ident
;
1779 tmp
= tmp
->ident_expression
;
1781 deref
= member_expression(deref
, '.', member
);
1783 deref
= member_expression(symbol
, '.', member
);
1786 member
= number_to_member(symbol
, member_idx
);
1787 deref
= member_expression(symbol
, '.', member
);
1791 if (right
->type
== EXPR_INITIALIZER
) {
1792 type
= get_type(deref
);
1793 if (type
&& type
->type
== SYM_ARRAY
)
1794 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1796 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1798 assign
= assign_expression(deref
, '=', right
);
1801 } END_FOR_EACH_PTR(tmp
);
1803 set_unset_to_zero(struct_type
, symbol
);
1806 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1808 fake_member_assigns_helper(symbol_expression(sym
),
1809 sym
->initializer
->expr_list
, fake_cb
);
1812 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1814 struct expression
*offset
, *binop
, *assign
, *tmp
;
1815 struct symbol
*type
;
1818 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1823 FOR_EACH_PTR(expr_list
, tmp
) {
1824 if (tmp
->type
== EXPR_INDEX
) {
1825 if (tmp
->idx_from
!= tmp
->idx_to
)
1827 idx
= tmp
->idx_from
;
1830 if (!tmp
->idx_expression
)
1832 tmp
= tmp
->idx_expression
;
1834 offset
= value_expr(idx
);
1835 binop
= array_element_expression(array
, offset
);
1836 if (tmp
->type
== EXPR_INITIALIZER
) {
1837 type
= get_type(binop
);
1838 if (type
&& type
->type
== SYM_ARRAY
)
1839 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1841 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1843 assign
= assign_expression(binop
, '=', tmp
);
1850 } END_FOR_EACH_PTR(tmp
);
1852 __call_array_initialized_hooks(array
, max
);
1855 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1857 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1860 static void fake_assign_expr(struct symbol
*sym
)
1862 struct expression
*assign
, *symbol
;
1864 symbol
= symbol_expression(sym
);
1865 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1866 __split_expr(assign
);
1869 static void do_initializer_stuff(struct symbol
*sym
)
1871 if (!sym
->initializer
)
1874 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1875 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1876 fake_element_assigns(sym
, __split_expr
);
1878 fake_member_assigns(sym
, __split_expr
);
1880 fake_assign_expr(sym
);
1884 static void split_declaration(struct symbol_list
*sym_list
)
1888 FOR_EACH_PTR(sym_list
, sym
) {
1889 __pass_to_client(sym
, DECLARATION_HOOK
);
1890 do_initializer_stuff(sym
);
1891 __pass_to_client(sym
, DECLARATION_HOOK_AFTER
);
1893 } END_FOR_EACH_PTR(sym
);
1896 static void call_global_assign_hooks(struct expression
*assign
)
1898 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1901 static void fake_global_assign(struct symbol
*sym
)
1903 struct expression
*assign
, *symbol
;
1905 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1906 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1907 fake_element_assigns(sym
, call_global_assign_hooks
);
1908 } else if (sym
->initializer
) {
1909 symbol
= symbol_expression(sym
);
1910 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1911 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1913 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1915 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1916 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1917 fake_member_assigns(sym
, call_global_assign_hooks
);
1918 } else if (sym
->initializer
) {
1919 symbol
= symbol_expression(sym
);
1920 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1921 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1923 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1926 symbol
= symbol_expression(sym
);
1927 if (sym
->initializer
) {
1928 assign
= assign_expression(symbol
, '=', sym
->initializer
);
1929 __split_expr(assign
);
1931 assign
= assign_expression(symbol
, '=', zero_expr());
1933 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1937 static void start_function_definition(struct symbol
*sym
)
1939 __in_function_def
= 1;
1940 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1941 __in_function_def
= 0;
1942 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1946 void add_function_data(unsigned long *fn_data
)
1948 __add_ptr_list(&fn_data_list
, fn_data
);
1951 static void clear_function_data(void)
1955 FOR_EACH_PTR(fn_data_list
, tmp
) {
1957 } END_FOR_EACH_PTR(tmp
);
1960 static void record_func_time(void)
1962 struct timeval stop
;
1966 gettimeofday(&stop
, NULL
);
1967 func_time
= stop
.tv_sec
- fn_start_time
.tv_sec
;
1968 snprintf(buf
, sizeof(buf
), "%d", func_time
);
1969 sql_insert_return_implies(FUNC_TIME
, 0, "", buf
);
1970 if (option_time
&& func_time
> 2) {
1972 sm_msg("func_time: %d", func_time
);
1977 static void split_function(struct symbol
*sym
)
1979 struct symbol
*base_type
= get_base_type(sym
);
1981 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1984 gettimeofday(&outer_fn_start_time
, NULL
);
1985 gettimeofday(&fn_start_time
, NULL
);
1988 cur_func
= sym
->ident
->name
;
1989 if (option_process_function
&& cur_func
&&
1990 strcmp(option_process_function
, cur_func
) != 0)
1992 set_position(sym
->pos
);
1993 clear_function_data();
1995 last_goto_statement_handled
= 0;
1996 sm_debug("new function: %s\n", cur_func
);
1998 if (option_two_passes
) {
2002 start_function_definition(sym
);
2003 __split_stmt(base_type
->stmt
);
2004 __split_stmt(base_type
->inline_stmt
);
2010 start_function_definition(sym
);
2011 __split_stmt(base_type
->stmt
);
2012 __split_stmt(base_type
->inline_stmt
);
2013 if (!__path_is_null() &&
2014 cur_func_return_type() == &void_ctype
&&
2015 !__bail_on_rest_of_function
) {
2016 __pass_to_client(NULL
, RETURN_HOOK
);
2019 __pass_to_client(sym
, END_FUNC_HOOK
);
2020 if (need_delayed_scope_hooks())
2021 __call_scope_hooks();
2022 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
2029 cur_func_sym
= NULL
;
2031 free_data_info_allocs();
2032 free_expression_stack(&switch_expr_stack
);
2033 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
2034 __bail_on_rest_of_function
= 0;
2037 static void save_flow_state(void)
2041 __add_ptr_list(&backup
, INT_PTR(loop_num
<< 2));
2042 __add_ptr_list(&backup
, INT_PTR(loop_count
<< 2));
2043 __add_ptr_list(&backup
, INT_PTR(final_pass
<< 2));
2045 __add_ptr_list(&backup
, big_statement_stack
);
2046 __add_ptr_list(&backup
, big_expression_stack
);
2047 __add_ptr_list(&backup
, big_condition_stack
);
2048 __add_ptr_list(&backup
, switch_expr_stack
);
2050 __add_ptr_list(&backup
, cur_func_sym
);
2052 __add_ptr_list(&backup
, parsed_calls
);
2054 __add_ptr_list(&backup
, __prev_stmt
);
2055 __add_ptr_list(&backup
, __cur_stmt
);
2056 __add_ptr_list(&backup
, __next_stmt
);
2058 FOR_EACH_PTR(fn_data_list
, tmp
) {
2059 __add_ptr_list(&backup
, (void *)*tmp
);
2060 } END_FOR_EACH_PTR(tmp
);
2063 static void *pop_backup(void)
2067 ret
= last_ptr_list(backup
);
2068 delete_ptr_list_last(&backup
);
2072 static void restore_flow_state(void)
2076 FOR_EACH_PTR_REVERSE(fn_data_list
, tmp
) {
2077 *tmp
= (unsigned long)pop_backup();
2078 } END_FOR_EACH_PTR_REVERSE(tmp
);
2080 __next_stmt
= pop_backup();
2081 __cur_stmt
= pop_backup();
2082 __prev_stmt
= pop_backup();
2084 parsed_calls
= pop_backup();
2086 cur_func_sym
= pop_backup();
2087 switch_expr_stack
= pop_backup();
2088 big_condition_stack
= pop_backup();
2089 big_expression_stack
= pop_backup();
2090 big_statement_stack
= pop_backup();
2091 final_pass
= PTR_INT(pop_backup()) >> 2;
2092 loop_count
= PTR_INT(pop_backup()) >> 2;
2093 loop_num
= PTR_INT(pop_backup()) >> 2;
2096 void parse_inline(struct expression
*call
)
2098 struct symbol
*base_type
;
2099 char *cur_func_bak
= cur_func
; /* not aligned correctly for backup */
2100 struct timeval time_backup
= fn_start_time
;
2101 struct expression
*orig_inline
= __inline_fn
;
2104 if (out_of_memory() || taking_too_long())
2107 if (already_parsed_call(call
))
2112 __pass_to_client(call
, INLINE_FN_START
);
2113 final_pass
= 0; /* don't print anything */
2115 orig_budget
= inline_budget
;
2116 inline_budget
= inline_budget
- 5;
2118 base_type
= get_base_type(call
->fn
->symbol
);
2119 cur_func_sym
= call
->fn
->symbol
;
2120 if (call
->fn
->symbol
->ident
)
2121 cur_func
= call
->fn
->symbol
->ident
->name
;
2124 set_position(call
->fn
->symbol
->pos
);
2127 big_statement_stack
= NULL
;
2128 big_expression_stack
= NULL
;
2129 big_condition_stack
= NULL
;
2130 switch_expr_stack
= NULL
;
2131 parsed_calls
= NULL
;
2133 sm_debug("inline function: %s\n", cur_func
);
2135 clear_function_data();
2138 start_function_definition(call
->fn
->symbol
);
2139 __split_stmt(base_type
->stmt
);
2140 __split_stmt(base_type
->inline_stmt
);
2141 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
2142 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
2143 call
->fn
->symbol
->parsed
= true;
2145 free_expression_stack(&switch_expr_stack
);
2146 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
2150 restore_flow_state();
2151 fn_start_time
= time_backup
;
2152 cur_func
= cur_func_bak
;
2154 restore_all_states();
2155 set_position(call
->pos
);
2156 __inline_fn
= orig_inline
;
2157 inline_budget
= orig_budget
;
2158 __pass_to_client(call
, INLINE_FN_END
);
2161 static struct symbol_list
*inlines_called
;
2162 static void add_inline_function(struct symbol
*sym
)
2164 static struct symbol_list
*already_added
;
2167 FOR_EACH_PTR(already_added
, tmp
) {
2170 } END_FOR_EACH_PTR(tmp
);
2172 add_ptr_list(&already_added
, sym
);
2173 add_ptr_list(&inlines_called
, sym
);
2176 static void process_inlines(void)
2180 FOR_EACH_PTR(inlines_called
, tmp
) {
2181 split_function(tmp
);
2182 } END_FOR_EACH_PTR(tmp
);
2183 free_ptr_list(&inlines_called
);
2186 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
2190 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
2193 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
2195 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
2197 } END_FOR_EACH_PTR_REVERSE(sym
);
2202 static bool interesting_function(struct symbol
*sym
)
2204 static int prev_stream
= -1;
2205 static bool prev_answer
;
2206 const char *filename
;
2209 if (!(sym
->ctype
.modifiers
& MOD_INLINE
))
2212 if (sym
->pos
.stream
== prev_stream
)
2215 prev_stream
= sym
->pos
.stream
;
2216 prev_answer
= false;
2218 filename
= stream_name(sym
->pos
.stream
);
2219 len
= strlen(filename
);
2220 if (len
> 0 && filename
[len
- 1] == 'c')
2225 static void split_inlines_in_scope(struct symbol
*sym
)
2227 struct symbol
*base
;
2228 struct symbol_list
*scope_list
;
2231 scope_list
= sym
->scope
->symbols
;
2232 stream
= sym
->pos
.stream
;
2234 /* find the last static symbol in the file */
2235 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
2236 if (sym
->pos
.stream
!= stream
)
2238 if (sym
->type
!= SYM_NODE
)
2240 base
= get_base_type(sym
);
2243 if (base
->type
!= SYM_FN
)
2245 if (!base
->inline_stmt
)
2247 if (!interesting_function(sym
))
2249 add_inline_function(sym
);
2250 } END_FOR_EACH_PTR_REVERSE(sym
);
2255 static void split_inlines(struct symbol_list
*sym_list
)
2259 sym
= get_last_scoped_symbol(sym_list
, 0);
2261 split_inlines_in_scope(sym
);
2262 sym
= get_last_scoped_symbol(sym_list
, 1);
2264 split_inlines_in_scope(sym
);
2267 static struct stree
*clone_estates_perm(struct stree
*orig
)
2269 struct stree
*ret
= NULL
;
2270 struct sm_state
*tmp
;
2272 FOR_EACH_SM(orig
, tmp
) {
2273 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
2274 } END_FOR_EACH_SM(tmp
);
2279 struct position last_pos
;
2280 static void split_c_file_functions(struct symbol_list
*sym_list
)
2285 FOR_EACH_PTR(sym_list
, sym
) {
2286 set_position(sym
->pos
);
2287 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
2288 __pass_to_client(sym
, BASE_HOOK
);
2289 fake_global_assign(sym
);
2290 __pass_to_client(sym
, DECLARATION_HOOK_AFTER
);
2292 } END_FOR_EACH_PTR(sym
);
2293 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
2296 FOR_EACH_PTR(sym_list
, sym
) {
2297 set_position(sym
->pos
);
2298 last_pos
= sym
->pos
;
2299 if (!interesting_function(sym
))
2301 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
2302 split_function(sym
);
2305 last_pos
= sym
->pos
;
2306 } END_FOR_EACH_PTR(sym
);
2307 split_inlines(sym_list
);
2308 __pass_to_client(sym_list
, END_FILE_HOOK
);
2311 static int final_before_fake
;
2312 void init_fake_env(void)
2315 final_before_fake
= final_pass
;
2317 __push_fake_cur_stree();
2321 void end_fake_env(void)
2323 __free_fake_cur_stree();
2326 final_pass
= final_before_fake
;
2329 static void open_output_files(char *base_file
)
2333 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
2334 sm_outfd
= fopen(buf
, "w");
2336 sm_fatal("Cannot open %s", buf
);
2341 snprintf(buf
, sizeof(buf
), "%s.smatch.sql", base_file
);
2342 sql_outfd
= fopen(buf
, "w");
2344 sm_fatal("Error: Cannot open %s", buf
);
2346 snprintf(buf
, sizeof(buf
), "%s.smatch.caller_info", base_file
);
2347 caller_info_fd
= fopen(buf
, "w");
2348 if (!caller_info_fd
)
2349 sm_fatal("Error: Cannot open %s", buf
);
2352 void smatch(struct string_list
*filelist
)
2354 struct symbol_list
*sym_list
;
2355 struct timeval stop
, start
;
2359 gettimeofday(&start
, NULL
);
2361 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
2362 path
= getcwd(NULL
, 0);
2363 free(full_base_file
);
2365 len
= strlen(path
) + 1 + strlen(base_file
) + 1;
2366 full_base_file
= malloc(len
);
2367 snprintf(full_base_file
, len
, "%s/%s", path
, base_file
);
2369 full_base_file
= alloc_string(base_file
);
2371 if (option_file_output
)
2372 open_output_files(base_file
);
2373 base_file_stream
= input_stream_nr
;
2374 sym_list
= sparse_keep_tokens(base_file
);
2375 split_c_file_functions(sym_list
);
2376 } END_FOR_EACH_PTR_NOTAG(base_file
);
2378 gettimeofday(&stop
, NULL
);
2380 set_position(last_pos
);
2383 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);
2385 sm_msg("mem: %luKb", get_max_memory());