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 static int in_fake_env
;
32 struct expression
*__inline_fn
;
34 static int __smatch_lineno
= 0;
36 static char *base_file
;
37 static const char *filename
;
38 static char *pathname
;
39 static char *full_filename
;
40 static char *cur_func
;
41 static unsigned int loop_count
;
42 static int last_goto_statement_handled
;
43 int __expr_stmt_count
;
44 int __in_function_def
;
45 static struct expression_list
*switch_expr_stack
= NULL
;
46 static struct expression_list
*post_op_stack
= NULL
;
48 struct expression_list
*big_expression_stack
;
49 struct statement_list
*big_statement_stack
;
50 struct statement
*__prev_stmt
;
51 struct statement
*__cur_stmt
;
52 struct statement
*__next_stmt
;
53 int __in_pre_condition
= 0;
54 int __bail_on_rest_of_function
= 0;
55 static struct timeval fn_start_time
;
56 char *get_function(void) { return cur_func
; }
57 int get_lineno(void) { return __smatch_lineno
; }
58 int inside_loop(void) { return !!loop_count
; }
59 int definitely_inside_loop(void) { return !!(loop_count
& ~0x80000000); }
60 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
61 int in_expression_statement(void) { return !!__expr_stmt_count
; }
63 static void split_symlist(struct symbol_list
*sym_list
);
64 static void split_declaration(struct symbol_list
*sym_list
);
65 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
);
66 static void add_inline_function(struct symbol
*sym
);
67 static void parse_inline(struct expression
*expr
);
69 int option_assume_loops
= 0;
70 int option_two_passes
= 0;
71 struct symbol
*cur_func_sym
= NULL
;
72 struct stree
*global_states
;
74 long long valid_ptr_min
= 4096;
75 long long valid_ptr_max
= 2117777777;
76 sval_t valid_ptr_min_sval
= {
80 sval_t valid_ptr_max_sval
= {
82 {.value
= LONG_MAX
- 100000},
85 static void set_valid_ptr_max(void)
87 if (type_bits(&ptr_ctype
) == 32)
88 valid_ptr_max
= 2117777777;
89 else if (type_bits(&ptr_ctype
) == 64)
90 valid_ptr_max
= 2117777777777777777LL;
92 valid_ptr_max_sval
.value
= valid_ptr_max
;
95 int outside_of_function(void)
97 return cur_func_sym
== NULL
;
100 const char *get_filename(void)
104 if (option_full_path
)
105 return full_filename
;
109 const char *get_base_file(void)
114 static void set_position(struct position pos
)
117 static int prev_stream
= -1;
122 if (pos
.stream
== 0 && pos
.line
== 0)
125 __smatch_lineno
= pos
.line
;
127 if (pos
.stream
== prev_stream
)
130 filename
= stream_name(pos
.stream
);
133 pathname
= getcwd(NULL
, 0);
135 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
136 full_filename
= malloc(len
);
137 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
139 full_filename
= alloc_string(filename
);
144 int is_assigned_call(struct expression
*expr
)
146 struct expression
*parent
= expr_get_parent_expr(expr
);
149 parent
->type
== EXPR_ASSIGNMENT
&&
151 strip_expr(parent
->right
) == expr
)
157 static int is_inline_func(struct expression
*expr
)
159 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
161 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
166 static int is_noreturn_func(struct expression
*expr
)
168 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
170 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
175 int inlinable(struct expression
*expr
)
178 struct statement
*last_stmt
= NULL
;
180 if (__inline_fn
) /* don't nest */
183 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
185 if (is_no_inline_function(expr
->symbol
->ident
->name
))
187 sym
= get_base_type(expr
->symbol
);
188 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
189 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) > 10)
191 if (sym
->stmt
->type
!= STMT_COMPOUND
)
193 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->stmt
->stmts
);
195 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
196 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) > 10)
198 if (sym
->inline_stmt
->type
!= STMT_COMPOUND
)
200 last_stmt
= last_ptr_list((struct ptr_list
*)sym
->inline_stmt
->stmts
);
206 /* the magic numbers in this function are pulled out of my bum. */
207 if (last_stmt
->pos
.line
> sym
->pos
.line
+ 20)
213 void __process_post_op_stack(void)
215 struct expression
*expr
;
217 FOR_EACH_PTR(post_op_stack
, expr
) {
218 __pass_to_client(expr
, OP_HOOK
);
219 } END_FOR_EACH_PTR(expr
);
221 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
224 static int handle_comma_assigns(struct expression
*expr
)
226 struct expression
*right
;
227 struct expression
*assign
;
229 right
= strip_expr(expr
->right
);
230 if (right
->type
!= EXPR_COMMA
)
233 __split_expr(right
->left
);
234 __process_post_op_stack();
236 assign
= assign_expression(expr
->left
, right
->right
);
237 __split_expr(assign
);
242 /* This is to handle *p++ = foo; assignments */
243 static int handle_postop_assigns(struct expression
*expr
)
245 struct expression
*left
, *fake_left
;
246 struct expression
*assign
;
248 left
= strip_expr(expr
->left
);
249 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '*')
251 left
= strip_expr(left
->unop
);
252 if (left
->type
!= EXPR_POSTOP
)
255 fake_left
= deref_expression(strip_expr(left
->unop
));
256 assign
= assign_expression(fake_left
, expr
->right
);
258 __split_expr(assign
);
259 __split_expr(expr
->left
);
264 static int prev_expression_is_getting_address(struct expression
*expr
)
266 struct expression
*parent
;
269 parent
= expr_get_parent_expr(expr
);
273 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
275 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
277 if (parent
->type
== EXPR_DEREF
&& parent
->op
== '.')
286 void __split_expr(struct expression
*expr
)
291 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
293 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
295 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
298 push_expression(&big_expression_stack
, expr
);
299 set_position(expr
->pos
);
300 __pass_to_client(expr
, EXPR_HOOK
);
302 switch (expr
->type
) {
304 expr_set_parent_expr(expr
->unop
, expr
);
306 if (expr
->op
== '*' &&
307 !prev_expression_is_getting_address(expr
))
308 __pass_to_client(expr
, DEREF_HOOK
);
309 __split_expr(expr
->unop
);
310 __pass_to_client(expr
, OP_HOOK
);
313 expr_set_parent_expr(expr
->unop
, expr
);
315 __split_expr(expr
->unop
);
316 push_expression(&post_op_stack
, expr
);
320 if (expr
->statement
&& !expr
->statement
) {
321 stmt_set_parent_stmt(expr
->statement
,
322 last_ptr_list((struct ptr_list
*)big_statement_stack
));
324 __split_stmt(expr
->statement
);
329 expr_set_parent_expr(expr
->left
, expr
);
330 expr_set_parent_expr(expr
->right
, expr
);
332 __pass_to_client(expr
, LOGIC_HOOK
);
333 __handle_logic(expr
);
336 expr_set_parent_expr(expr
->left
, expr
);
337 expr_set_parent_expr(expr
->right
, expr
);
339 __pass_to_client(expr
, BINOP_HOOK
);
341 expr_set_parent_expr(expr
->left
, expr
);
342 expr_set_parent_expr(expr
->right
, expr
);
344 __split_expr(expr
->left
);
345 __process_post_op_stack();
346 __split_expr(expr
->right
);
348 case EXPR_ASSIGNMENT
: {
349 struct expression
*right
;
351 expr_set_parent_expr(expr
->left
, expr
);
352 expr_set_parent_expr(expr
->right
, expr
);
354 right
= strip_expr(expr
->right
);
358 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
361 if (__handle_condition_assigns(expr
))
363 /* foo = (x < 5 ? foo : 5); */
364 if (__handle_select_assigns(expr
))
366 /* foo = ({frob(); frob(); frob(); 1;}) */
367 if (__handle_expr_statement_assigns(expr
))
370 if (handle_comma_assigns(expr
))
372 if (handle_postop_assigns(expr
))
375 __split_expr(expr
->right
);
376 if (outside_of_function())
377 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
379 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
381 __fake_struct_member_assignments(expr
);
383 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
384 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
386 if (get_macro_name(right
->pos
) &&
387 get_macro_name(expr
->pos
) != get_macro_name(right
->pos
))
388 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
390 __pass_to_client(expr
, ASSIGNMENT_HOOK_AFTER
);
392 __split_expr(expr
->left
);
396 expr_set_parent_expr(expr
->deref
, expr
);
398 __pass_to_client(expr
, DEREF_HOOK
);
399 __split_expr(expr
->deref
);
402 expr_set_parent_expr(expr
->base
, expr
);
404 __split_expr(expr
->base
);
407 case EXPR_FORCE_CAST
:
408 expr_set_parent_expr(expr
->cast_expression
, expr
);
410 __pass_to_client(expr
, CAST_HOOK
);
411 __split_expr(expr
->cast_expression
);
414 if (expr
->cast_expression
)
415 __pass_to_client(strip_parens(expr
->cast_expression
),
420 evaluate_expression(expr
);
422 case EXPR_CONDITIONAL
:
424 expr_set_parent_expr(expr
->conditional
, expr
);
425 expr_set_parent_expr(expr
->cond_true
, expr
);
426 expr_set_parent_expr(expr
->cond_false
, expr
);
428 if (known_condition_true(expr
->conditional
)) {
429 __split_expr(expr
->cond_true
);
432 if (known_condition_false(expr
->conditional
)) {
433 __split_expr(expr
->cond_false
);
436 __pass_to_client(expr
, SELECT_HOOK
);
437 __split_whole_condition(expr
->conditional
);
438 __split_expr(expr
->cond_true
);
439 __push_true_states();
440 __use_false_states();
441 __split_expr(expr
->cond_false
);
442 __merge_true_states();
445 expr_set_parent_expr(expr
->fn
, expr
);
447 if (sym_name_is("__builtin_constant_p", expr
->fn
))
449 split_expr_list(expr
->args
, expr
);
450 __split_expr(expr
->fn
);
451 if (is_inline_func(expr
->fn
))
452 add_inline_function(expr
->fn
->symbol
);
453 if (inlinable(expr
->fn
))
455 __process_post_op_stack();
456 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
458 if (inlinable(expr
->fn
)) {
461 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
462 if (is_noreturn_func(expr
->fn
))
465 case EXPR_INITIALIZER
:
466 split_expr_list(expr
->expr_list
, expr
);
468 case EXPR_IDENTIFIER
:
469 expr_set_parent_expr(expr
->ident_expression
, expr
);
470 __split_expr(expr
->ident_expression
);
473 expr_set_parent_expr(expr
->idx_expression
, expr
);
474 __split_expr(expr
->idx_expression
);
477 expr_set_parent_expr(expr
->init_expr
, expr
);
478 __split_expr(expr
->init_expr
);
481 __pass_to_client(expr
, SYM_HOOK
);
484 __pass_to_client(expr
, STRING_HOOK
);
489 pop_expression(&big_expression_stack
);
492 static int is_forever_loop(struct statement
*stmt
)
494 struct expression
*expr
;
497 expr
= strip_expr(stmt
->iterator_pre_condition
);
499 expr
= stmt
->iterator_post_condition
;
501 /* this is a for(;;) loop... */
505 if (get_value(expr
, &sval
) && sval
.value
!= 0)
512 static char *get_loop_name(int num
)
516 snprintf(buf
, 255, "-loop%d", num
);
518 return alloc_sname(buf
);
522 * Pre Loops are while and for loops.
524 static void handle_pre_loop(struct statement
*stmt
)
526 int once_through
; /* we go through the loop at least once */
527 struct sm_state
*extra_sm
= NULL
;
530 struct stree
*stree
= NULL
;
531 struct sm_state
*sm
= NULL
;
533 loop_name
= get_loop_name(loop_num
);
536 __split_stmt(stmt
->iterator_pre_statement
);
537 __prev_stmt
= stmt
->iterator_pre_statement
;
539 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
545 __merge_gotos(loop_name
, NULL
);
547 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
548 __in_pre_condition
++;
549 __pass_to_client(stmt
, PRELOOP_HOOK
);
550 __split_whole_condition(stmt
->iterator_pre_condition
);
551 __in_pre_condition
--;
552 FOR_EACH_SM(stree
, sm
) {
553 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
554 } END_FOR_EACH_SM(sm
);
557 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
559 if (option_assume_loops
)
562 __split_stmt(stmt
->iterator_statement
);
563 if (is_forever_loop(stmt
)) {
565 __save_gotos(loop_name
, NULL
);
567 __push_fake_cur_stree();
568 __split_stmt(stmt
->iterator_post_statement
);
569 stree
= __pop_fake_cur_stree();
571 __discard_false_states();
574 if (!__path_is_null())
575 __merge_stree_into_cur(stree
);
579 unchanged
= __iterator_unchanged(extra_sm
);
580 __split_stmt(stmt
->iterator_post_statement
);
581 __prev_stmt
= stmt
->iterator_post_statement
;
584 __save_gotos(loop_name
, NULL
);
585 __in_pre_condition
++;
586 __split_whole_condition(stmt
->iterator_pre_condition
);
587 __in_pre_condition
--;
589 __merge_false_states();
591 __discard_false_states();
593 __merge_false_states();
595 if (extra_sm
&& unchanged
)
596 __extra_pre_loop_hook_after(extra_sm
,
597 stmt
->iterator_post_statement
,
598 stmt
->iterator_pre_condition
);
605 * Post loops are do {} while();
607 static void handle_post_loop(struct statement
*stmt
)
611 loop_name
= get_loop_name(loop_num
);
617 __merge_gotos(loop_name
, NULL
);
618 __split_stmt(stmt
->iterator_statement
);
620 if (!is_zero(stmt
->iterator_post_condition
))
621 __save_gotos(loop_name
, NULL
);
623 if (is_forever_loop(stmt
)) {
626 __split_whole_condition(stmt
->iterator_post_condition
);
627 __use_false_states();
633 static int empty_statement(struct statement
*stmt
)
637 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
642 static int last_stmt_on_same_line(void)
644 struct statement
*stmt
;
647 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
650 if (stmt
->pos
.line
== get_lineno())
653 } END_FOR_EACH_PTR_REVERSE(stmt
);
657 static void split_asm_constraints(struct expression_list
*expr_list
)
659 struct expression
*expr
;
662 FOR_EACH_PTR(expr_list
, expr
) {
664 case 0: /* identifier */
665 case 1: /* constraint */
668 case 2: /* expression */
673 } END_FOR_EACH_PTR(expr
);
676 static int is_case_val(struct statement
*stmt
, sval_t sval
)
680 if (stmt
->type
!= STMT_CASE
)
682 if (!stmt
->case_expression
) {
686 if (!get_value(stmt
->case_expression
, &case_sval
))
688 if (case_sval
.value
== sval
.value
)
693 static struct range_list
*get_case_rl(struct expression
*switch_expr
,
694 struct expression
*case_expr
,
695 struct expression
*case_to
)
698 struct range_list
*rl
= NULL
;
699 struct symbol
*switch_type
;
701 switch_type
= get_type(switch_expr
);
702 if (get_value(case_to
, &end
) && get_value(case_expr
, &start
)) {
703 start
= sval_cast(switch_type
, start
);
704 end
= sval_cast(switch_type
, end
);
705 add_range(&rl
, start
, end
);
706 } else if (get_value(case_expr
, &start
)) {
707 start
= sval_cast(switch_type
, start
);
708 add_range(&rl
, start
, start
);
714 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
716 struct statement
*tmp
;
717 struct range_list
*rl
;
719 __split_expr(stmt
->switch_expression
);
720 sval
= sval_cast(get_type(stmt
->switch_expression
), sval
);
722 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
723 __save_switch_states(top_expression(switch_expr_stack
));
728 stmt
= stmt
->switch_statement
;
730 __push_scope_hooks();
731 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
732 __smatch_lineno
= tmp
->pos
.line
;
733 if (is_case_val(tmp
, sval
)) {
734 rl
= alloc_rl(sval
, sval
);
735 __merge_switches(top_expression(switch_expr_stack
), rl
);
736 __pass_case_to_client(top_expression(switch_expr_stack
), rl
);
738 if (__path_is_null())
741 if (__path_is_null()) {
745 } END_FOR_EACH_PTR(tmp
);
747 __call_scope_hooks();
748 if (!__pop_default())
749 __merge_switches(top_expression(switch_expr_stack
), NULL
);
750 __discard_switches();
752 pop_expression(&switch_expr_stack
);
755 static void split_case(struct statement
*stmt
)
757 struct range_list
*rl
= NULL
;
759 expr_set_parent_stmt(stmt
->case_expression
, stmt
);
760 expr_set_parent_stmt(stmt
->case_to
, stmt
);
762 rl
= get_case_rl(top_expression(switch_expr_stack
),
763 stmt
->case_expression
, stmt
->case_to
);
764 while (stmt
->case_statement
->type
== STMT_CASE
) {
765 struct range_list
*tmp
;
767 tmp
= get_case_rl(top_expression(switch_expr_stack
),
768 stmt
->case_statement
->case_expression
,
769 stmt
->case_statement
->case_to
);
772 rl
= rl_union(rl
, tmp
);
773 if (!stmt
->case_expression
)
775 stmt
= stmt
->case_statement
;
778 __merge_switches(top_expression(switch_expr_stack
), rl
);
780 if (!stmt
->case_expression
)
782 __split_stmt(stmt
->case_statement
);
785 static int taking_too_long(void)
789 ms
= ms_since(&fn_start_time
);
790 if (ms
> 1000 * 60 * 5) /* five minutes */
795 static int is_last_stmt(struct statement
*cur_stmt
)
797 struct symbol
*fn
= get_base_type(cur_func_sym
);
798 struct statement
*stmt
;
804 stmt
= fn
->inline_stmt
;
805 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
807 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
808 if (stmt
&& stmt
->type
== STMT_LABEL
)
809 stmt
= stmt
->label_statement
;
810 if (stmt
== cur_stmt
)
815 static void handle_backward_goto(struct statement
*goto_stmt
)
817 const char *goto_name
, *label_name
;
818 struct statement
*func_stmt
;
819 struct symbol
*base_type
= get_base_type(cur_func_sym
);
820 struct statement
*tmp
;
825 if (last_goto_statement_handled
)
827 last_goto_statement_handled
= 1;
829 if (!goto_stmt
->goto_label
||
830 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
831 !goto_stmt
->goto_label
->ident
)
833 goto_name
= goto_stmt
->goto_label
->ident
->name
;
835 func_stmt
= base_type
->stmt
;
837 func_stmt
= base_type
->inline_stmt
;
840 if (func_stmt
->type
!= STMT_COMPOUND
)
843 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
845 if (tmp
->type
!= STMT_LABEL
)
847 if (!tmp
->label_identifier
||
848 tmp
->label_identifier
->type
!= SYM_LABEL
||
849 !tmp
->label_identifier
->ident
)
851 label_name
= tmp
->label_identifier
->ident
->name
;
852 if (strcmp(goto_name
, label_name
) != 0)
857 } END_FOR_EACH_PTR(tmp
);
860 static void fake_a_return(void)
862 struct symbol
*return_type
;
867 return_type
= get_real_base_type(cur_func_sym
);
868 return_type
= get_real_base_type(return_type
);
869 if (return_type
!= &void_ctype
) {
870 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
875 static void fake_an_empty_default(struct position pos
)
877 static struct statement none
= {};
880 none
.type
= STMT_NONE
;
881 __merge_switches(top_expression(switch_expr_stack
), NULL
);
885 static void split_compound(struct statement
*stmt
)
887 struct statement
*prev
= NULL
;
888 struct statement
*cur
= NULL
;
889 struct statement
*next
;
891 __push_scope_hooks();
893 FOR_EACH_PTR(stmt
->stmts
, next
) {
894 /* just set them all ahead of time */
895 stmt_set_parent_stmt(next
, stmt
);
905 } END_FOR_EACH_PTR(next
);
914 * For function scope, then delay calling the scope hooks until the
915 * end of function hooks can run. I'm not positive this is the right
918 if (!is_last_stmt(cur
))
919 __call_scope_hooks();
923 * This is a hack, work around for detecting empty functions.
925 static int need_delayed_scope_hooks(void)
927 struct symbol
*fn
= get_base_type(cur_func_sym
);
928 struct statement
*stmt
;
934 stmt
= fn
->inline_stmt
;
935 if (stmt
&& stmt
->type
== STMT_COMPOUND
)
940 void __split_label_stmt(struct statement
*stmt
)
942 if (stmt
->label_identifier
&&
943 stmt
->label_identifier
->type
== SYM_LABEL
&&
944 stmt
->label_identifier
->ident
) {
945 loop_count
|= 0x80000000;
946 __merge_gotos(stmt
->label_identifier
->ident
->name
, stmt
->label_identifier
);
950 static void find_asm_gotos(struct statement
*stmt
)
954 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
955 __save_gotos(sym
->ident
->name
, sym
);
956 } END_FOR_EACH_PTR(sym
);
959 void __split_stmt(struct statement
*stmt
)
966 if (__bail_on_rest_of_function
)
969 if (out_of_memory() || taking_too_long()) {
972 gettimeofday(&stop
, NULL
);
974 __bail_on_rest_of_function
= 1;
976 sm_msg("Function too hairy. Giving up. %lu seconds",
977 stop
.tv_sec
- fn_start_time
.tv_sec
);
979 final_pass
= 0; /* turn off sm_msg() from here */
983 add_ptr_list(&big_statement_stack
, stmt
);
984 free_expression_stack(&big_expression_stack
);
985 set_position(stmt
->pos
);
986 __pass_to_client(stmt
, STMT_HOOK
);
988 switch (stmt
->type
) {
989 case STMT_DECLARATION
:
990 split_declaration(stmt
->declaration
);
993 expr_set_parent_stmt(stmt
->ret_value
, stmt
);
995 __split_expr(stmt
->ret_value
);
996 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
997 __process_post_op_stack();
1000 case STMT_EXPRESSION
:
1001 expr_set_parent_stmt(stmt
->expression
, stmt
);
1002 expr_set_parent_stmt(stmt
->context
, stmt
);
1004 __split_expr(stmt
->expression
);
1007 split_compound(stmt
);
1010 stmt_set_parent_stmt(stmt
->if_true
, stmt
);
1011 stmt_set_parent_stmt(stmt
->if_false
, stmt
);
1012 expr_set_parent_stmt(stmt
->if_conditional
, stmt
);
1014 if (known_condition_true(stmt
->if_conditional
)) {
1015 __split_stmt(stmt
->if_true
);
1018 if (known_condition_false(stmt
->if_conditional
)) {
1019 __split_stmt(stmt
->if_false
);
1022 __split_whole_condition(stmt
->if_conditional
);
1023 __split_stmt(stmt
->if_true
);
1024 if (empty_statement(stmt
->if_true
) &&
1025 last_stmt_on_same_line() &&
1026 !get_macro_name(stmt
->if_true
->pos
))
1027 sm_msg("warn: if();");
1028 __push_true_states();
1029 __use_false_states();
1030 __split_stmt(stmt
->if_false
);
1031 __merge_true_states();
1034 stmt_set_parent_stmt(stmt
->iterator_pre_statement
, stmt
);
1035 stmt_set_parent_stmt(stmt
->iterator_statement
, stmt
);
1036 stmt_set_parent_stmt(stmt
->iterator_post_statement
, stmt
);
1037 expr_set_parent_stmt(stmt
->iterator_pre_condition
, stmt
);
1038 expr_set_parent_stmt(stmt
->iterator_post_condition
, stmt
);
1040 if (stmt
->iterator_pre_condition
)
1041 handle_pre_loop(stmt
);
1042 else if (stmt
->iterator_post_condition
)
1043 handle_post_loop(stmt
);
1045 // these are for(;;) type loops.
1046 handle_pre_loop(stmt
);
1050 stmt_set_parent_stmt(stmt
->switch_statement
, stmt
);
1051 expr_set_parent_stmt(stmt
->switch_expression
, stmt
);
1053 if (get_value(stmt
->switch_expression
, &sval
)) {
1054 split_known_switch(stmt
, sval
);
1057 __split_expr(stmt
->switch_expression
);
1058 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
1059 __save_switch_states(top_expression(switch_expr_stack
));
1063 __split_stmt(stmt
->switch_statement
);
1064 if (!__pop_default())
1065 fake_an_empty_default(stmt
->pos
);
1066 __discard_switches();
1068 pop_expression(&switch_expr_stack
);
1074 __split_label_stmt(stmt
);
1075 __split_stmt(stmt
->label_statement
);
1078 expr_set_parent_stmt(stmt
->goto_expression
, stmt
);
1080 __split_expr(stmt
->goto_expression
);
1081 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
1082 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
1084 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
1086 __process_continues();
1088 } else if (stmt
->goto_label
&&
1089 stmt
->goto_label
->type
== SYM_LABEL
&&
1090 stmt
->goto_label
->ident
) {
1091 __save_gotos(stmt
->goto_label
->ident
->name
, stmt
->goto_label
);
1094 if (is_last_stmt(stmt
))
1095 handle_backward_goto(stmt
);
1100 expr_set_parent_stmt(stmt
->asm_string
, stmt
);
1102 find_asm_gotos(stmt
);
1103 __pass_to_client(stmt
, ASM_HOOK
);
1104 __split_expr(stmt
->asm_string
);
1105 split_asm_constraints(stmt
->asm_outputs
);
1106 split_asm_constraints(stmt
->asm_inputs
);
1107 split_asm_constraints(stmt
->asm_clobbers
);
1112 __split_expr(stmt
->range_expression
);
1113 __split_expr(stmt
->range_low
);
1114 __split_expr(stmt
->range_high
);
1117 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
1119 __process_post_op_stack();
1122 static void split_expr_list(struct expression_list
*expr_list
, struct expression
*parent
)
1124 struct expression
*expr
;
1126 FOR_EACH_PTR(expr_list
, expr
) {
1127 expr_set_parent_expr(expr
, parent
);
1129 __process_post_op_stack();
1130 } END_FOR_EACH_PTR(expr
);
1133 static void split_sym(struct symbol
*sym
)
1137 if (!(sym
->namespace & NS_SYMBOL
))
1140 __split_stmt(sym
->stmt
);
1141 __split_expr(sym
->array_size
);
1142 split_symlist(sym
->arguments
);
1143 split_symlist(sym
->symbol_list
);
1144 __split_stmt(sym
->inline_stmt
);
1145 split_symlist(sym
->inline_symbol_list
);
1148 static void split_symlist(struct symbol_list
*sym_list
)
1152 FOR_EACH_PTR(sym_list
, sym
) {
1154 } END_FOR_EACH_PTR(sym
);
1157 typedef void (fake_cb
)(struct expression
*expr
);
1159 static int member_to_number(struct expression
*expr
, struct ident
*member
)
1161 struct symbol
*type
, *tmp
;
1167 name
= member
->name
;
1169 type
= get_type(expr
);
1170 if (!type
|| type
->type
!= SYM_STRUCT
)
1174 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1178 if (strcmp(name
, tmp
->ident
->name
) == 0)
1180 } END_FOR_EACH_PTR(tmp
);
1184 static struct ident
*number_to_member(struct expression
*expr
, int num
)
1186 struct symbol
*type
, *member
;
1189 type
= get_type(expr
);
1190 if (!type
|| type
->type
!= SYM_STRUCT
)
1193 FOR_EACH_PTR(type
->symbol_list
, member
) {
1195 return member
->ident
;
1197 } END_FOR_EACH_PTR(member
);
1201 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
1203 static void set_inner_struct_members(struct expression
*expr
, struct symbol
*member
)
1205 struct expression
*edge_member
, *assign
;
1206 struct symbol
*base
= get_real_base_type(member
);
1210 expr
= member_expression(expr
, '.', member
->ident
);
1212 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
1213 struct symbol
*type
;
1215 type
= get_real_base_type(tmp
);
1219 edge_member
= member_expression(expr
, '.', tmp
->ident
);
1220 if (get_state_expr(SMATCH_EXTRA
, edge_member
))
1223 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1224 set_inner_struct_members(expr
, tmp
);
1231 assign
= assign_expression(edge_member
, zero_expr());
1232 __split_expr(assign
);
1233 } END_FOR_EACH_PTR(tmp
);
1238 static void set_unset_to_zero(struct symbol
*type
, struct expression
*expr
)
1241 struct expression
*member
= NULL
;
1242 struct expression
*assign
;
1245 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1246 expr
= strip_expr(expr
->unop
);
1250 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
1251 type
= get_real_base_type(tmp
);
1256 member
= member_expression(expr
, op
, tmp
->ident
);
1257 if (get_state_expr(SMATCH_EXTRA
, member
))
1261 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
1262 set_inner_struct_members(expr
, tmp
);
1265 if (type
->type
== SYM_ARRAY
)
1270 assign
= assign_expression(member
, zero_expr());
1271 __split_expr(assign
);
1272 } END_FOR_EACH_PTR(tmp
);
1275 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1277 struct expression
*deref
, *assign
, *tmp
, *right
;
1278 struct symbol
*struct_type
, *type
;
1279 struct ident
*member
;
1282 struct_type
= get_type(symbol
);
1284 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1288 * We're parsing an initializer that could look something like this:
1289 * struct foo foo = {
1291 * .whatever.xxx = 11,
1295 * So what we have here is a list with 42, .whatever, and .zzz. We need
1296 * to break it up into left and right sides of the assignments.
1300 FOR_EACH_PTR(members
, tmp
) {
1302 if (tmp
->type
== EXPR_IDENTIFIER
) {
1303 member_idx
= member_to_number(symbol
, tmp
->expr_ident
);
1304 while (tmp
->type
== EXPR_IDENTIFIER
) {
1305 member
= tmp
->expr_ident
;
1306 tmp
= tmp
->ident_expression
;
1308 deref
= member_expression(deref
, '.', member
);
1310 deref
= member_expression(symbol
, '.', member
);
1313 member
= number_to_member(symbol
, member_idx
);
1314 deref
= member_expression(symbol
, '.', member
);
1318 if (right
->type
== EXPR_INITIALIZER
) {
1319 type
= get_type(deref
);
1320 if (type
&& type
->type
== SYM_ARRAY
)
1321 fake_element_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1323 fake_member_assigns_helper(deref
, right
->expr_list
, fake_cb
);
1325 assign
= assign_expression(deref
, right
);
1328 } END_FOR_EACH_PTR(tmp
);
1330 set_unset_to_zero(struct_type
, symbol
);
1333 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1335 fake_member_assigns_helper(symbol_expression(sym
),
1336 sym
->initializer
->expr_list
, fake_cb
);
1339 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1341 struct expression
*offset
, *binop
, *assign
, *tmp
;
1342 struct symbol
*type
;
1345 if (ptr_list_size((struct ptr_list
*)expr_list
) > 1000)
1349 FOR_EACH_PTR(expr_list
, tmp
) {
1350 if (tmp
->type
== EXPR_INDEX
) {
1351 if (tmp
->idx_from
!= tmp
->idx_to
)
1353 idx
= tmp
->idx_from
;
1354 if (!tmp
->idx_expression
)
1356 tmp
= tmp
->idx_expression
;
1358 offset
= value_expr(idx
);
1359 binop
= array_element_expression(array
, offset
);
1360 if (tmp
->type
== EXPR_INITIALIZER
) {
1361 type
= get_type(binop
);
1362 if (type
&& type
->type
== SYM_ARRAY
)
1363 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1365 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1367 assign
= assign_expression(binop
, tmp
);
1372 } END_FOR_EACH_PTR(tmp
);
1375 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1377 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1380 static void fake_assign_expr(struct symbol
*sym
)
1382 struct expression
*assign
, *symbol
;
1384 symbol
= symbol_expression(sym
);
1385 assign
= assign_expression(symbol
, sym
->initializer
);
1386 __split_expr(assign
);
1389 static void call_split_expr(struct expression
*expr
)
1394 static void do_initializer_stuff(struct symbol
*sym
)
1396 if (!sym
->initializer
)
1399 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1400 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1401 fake_element_assigns(sym
, call_split_expr
);
1403 fake_member_assigns(sym
, call_split_expr
);
1405 fake_assign_expr(sym
);
1409 static void split_declaration(struct symbol_list
*sym_list
)
1413 FOR_EACH_PTR(sym_list
, sym
) {
1414 __pass_to_client(sym
, DECLARATION_HOOK
);
1415 do_initializer_stuff(sym
);
1417 } END_FOR_EACH_PTR(sym
);
1420 static void call_global_assign_hooks(struct expression
*assign
)
1422 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1425 static void fake_global_assign(struct symbol
*sym
)
1427 struct expression
*assign
, *symbol
;
1429 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1430 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1431 fake_element_assigns(sym
, call_global_assign_hooks
);
1432 } else if (sym
->initializer
) {
1433 symbol
= symbol_expression(sym
);
1434 assign
= assign_expression(symbol
, sym
->initializer
);
1435 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1437 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1439 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1440 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1441 fake_member_assigns(sym
, call_global_assign_hooks
);
1442 } else if (sym
->initializer
) {
1443 symbol
= symbol_expression(sym
);
1444 assign
= assign_expression(symbol
, sym
->initializer
);
1445 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1447 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1450 symbol
= symbol_expression(sym
);
1451 if (sym
->initializer
)
1452 assign
= assign_expression(symbol
, sym
->initializer
);
1454 assign
= assign_expression(symbol
, zero_expr());
1455 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1459 static void start_function_definition(struct symbol
*sym
)
1461 __in_function_def
= 1;
1462 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1463 __in_function_def
= 0;
1464 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1468 static void split_function(struct symbol
*sym
)
1470 struct symbol
*base_type
= get_base_type(sym
);
1472 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1475 gettimeofday(&fn_start_time
, NULL
);
1478 cur_func
= sym
->ident
->name
;
1479 set_position(sym
->pos
);
1481 last_goto_statement_handled
= 0;
1482 sm_debug("new function: %s\n", cur_func
);
1484 if (option_two_passes
) {
1488 start_function_definition(sym
);
1489 __split_stmt(base_type
->stmt
);
1490 __split_stmt(base_type
->inline_stmt
);
1496 start_function_definition(sym
);
1497 __split_stmt(base_type
->stmt
);
1498 __split_stmt(base_type
->inline_stmt
);
1499 __pass_to_client(sym
, END_FUNC_HOOK
);
1500 if (need_delayed_scope_hooks())
1501 __call_scope_hooks();
1502 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1505 cur_func_sym
= NULL
;
1507 free_data_info_allocs();
1508 free_expression_stack(&switch_expr_stack
);
1509 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1510 __bail_on_rest_of_function
= 0;
1513 static void parse_inline(struct expression
*call
)
1515 struct symbol
*base_type
;
1516 int loop_num_bak
= loop_num
;
1517 int loop_count_bak
= loop_count
;
1518 int final_pass_bak
= final_pass
;
1519 char *cur_func_bak
= cur_func
;
1520 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
1521 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
1522 struct expression_list
*big_condition_stack_bak
= big_condition_stack
;
1523 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
1524 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
1526 __pass_to_client(call
, INLINE_FN_START
);
1527 final_pass
= 0; /* don't print anything */
1530 base_type
= get_base_type(call
->fn
->symbol
);
1531 cur_func_sym
= call
->fn
->symbol
;
1532 if (call
->fn
->symbol
->ident
)
1533 cur_func
= call
->fn
->symbol
->ident
->name
;
1536 set_position(call
->fn
->symbol
->pos
);
1539 big_statement_stack
= NULL
;
1540 big_expression_stack
= NULL
;
1541 big_condition_stack
= NULL
;
1542 switch_expr_stack
= NULL
;
1544 sm_debug("inline function: %s\n", cur_func
);
1547 start_function_definition(call
->fn
->symbol
);
1548 __split_stmt(base_type
->stmt
);
1549 __split_stmt(base_type
->inline_stmt
);
1550 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1551 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1553 free_expression_stack(&switch_expr_stack
);
1554 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1558 loop_num
= loop_num_bak
;
1559 loop_count
= loop_count_bak
;
1560 final_pass
= final_pass_bak
;
1561 cur_func_sym
= cur_func_sym_bak
;
1562 cur_func
= cur_func_bak
;
1563 big_statement_stack
= big_statement_stack_bak
;
1564 big_expression_stack
= big_expression_stack_bak
;
1565 big_condition_stack
= big_condition_stack_bak
;
1566 switch_expr_stack
= switch_expr_stack_bak
;
1568 restore_all_states();
1569 set_position(call
->pos
);
1571 __pass_to_client(call
, INLINE_FN_END
);
1574 static struct symbol_list
*inlines_called
;
1575 static void add_inline_function(struct symbol
*sym
)
1577 static struct symbol_list
*already_added
;
1580 FOR_EACH_PTR(already_added
, tmp
) {
1583 } END_FOR_EACH_PTR(tmp
);
1585 add_ptr_list(&already_added
, sym
);
1586 add_ptr_list(&inlines_called
, sym
);
1589 static void process_inlines(void)
1593 FOR_EACH_PTR(inlines_called
, tmp
) {
1594 split_function(tmp
);
1595 } END_FOR_EACH_PTR(tmp
);
1596 free_ptr_list(&inlines_called
);
1599 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1603 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1606 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1608 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1610 } END_FOR_EACH_PTR_REVERSE(sym
);
1615 static void split_inlines_in_scope(struct symbol
*sym
)
1617 struct symbol
*base
;
1618 struct symbol_list
*scope_list
;
1621 scope_list
= sym
->scope
->symbols
;
1622 stream
= sym
->pos
.stream
;
1624 /* find the last static symbol in the file */
1625 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1626 if (sym
->pos
.stream
!= stream
)
1628 if (sym
->type
!= SYM_NODE
)
1630 base
= get_base_type(sym
);
1633 if (base
->type
!= SYM_FN
)
1635 if (!base
->inline_stmt
)
1637 add_inline_function(sym
);
1638 } END_FOR_EACH_PTR_REVERSE(sym
);
1643 static void split_inlines(struct symbol_list
*sym_list
)
1647 sym
= get_last_scoped_symbol(sym_list
, 0);
1649 split_inlines_in_scope(sym
);
1650 sym
= get_last_scoped_symbol(sym_list
, 1);
1652 split_inlines_in_scope(sym
);
1655 static struct stree
*clone_estates_perm(struct stree
*orig
)
1657 struct stree
*ret
= NULL
;
1658 struct sm_state
*tmp
;
1660 FOR_EACH_SM(orig
, tmp
) {
1661 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1662 } END_FOR_EACH_SM(tmp
);
1667 struct position last_pos
;
1668 static void split_functions(struct symbol_list
*sym_list
)
1673 FOR_EACH_PTR(sym_list
, sym
) {
1674 set_position(sym
->pos
);
1675 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1676 __pass_to_client(sym
, BASE_HOOK
);
1677 fake_global_assign(sym
);
1679 } END_FOR_EACH_PTR(sym
);
1680 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1683 FOR_EACH_PTR(sym_list
, sym
) {
1684 set_position(sym
->pos
);
1685 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1686 split_function(sym
);
1689 last_pos
= sym
->pos
;
1690 } END_FOR_EACH_PTR(sym
);
1691 split_inlines(sym_list
);
1692 __pass_to_client(sym_list
, END_FILE_HOOK
);
1695 static int final_before_fake
;
1696 void init_fake_env(void)
1699 final_before_fake
= final_pass
;
1701 __push_fake_cur_stree();
1705 void end_fake_env(void)
1707 __pop_fake_cur_stree();
1710 final_pass
= final_before_fake
;
1713 void smatch(int argc
, char **argv
)
1715 struct string_list
*filelist
= NULL
;
1716 struct symbol_list
*sym_list
;
1717 struct timeval stop
, start
;
1719 gettimeofday(&start
, NULL
);
1722 printf("Usage: smatch [--debug] <filename.c>\n");
1725 sparse_initialize(argc
, argv
, &filelist
);
1726 set_valid_ptr_max();
1727 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1728 if (option_file_output
) {
1731 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1732 sm_outfd
= fopen(buf
, "w");
1734 printf("Error: Cannot open %s\n", base_file
);
1738 sym_list
= sparse_keep_tokens(base_file
);
1739 split_functions(sym_list
);
1740 } END_FOR_EACH_PTR_NOTAG(base_file
);
1742 gettimeofday(&stop
, NULL
);
1744 set_position(last_pos
);
1746 sm_msg("time: %lu", stop
.tv_sec
- start
.tv_sec
);