function_hooks: do early stuff even earlier
[smatch.git] / smatch_flow.c
blob5b0cd8972d22e84294e611cfb8d73c3880ade774
1 /*
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
18 #define _GNU_SOURCE 1
19 #include <unistd.h>
20 #include <stdio.h>
21 #include "token.h"
22 #include "scope.h"
23 #include "smatch.h"
24 #include "smatch_expression_stacks.h"
25 #include "smatch_extra.h"
26 #include "smatch_slist.h"
28 int __in_fake_assign;
29 int __in_fake_struct_assign;
30 int __in_fake_var_assign;
31 int __fake_state_cnt;
32 int in_fake_env;
33 int final_pass;
34 int __inline_call;
35 struct expression *__inline_fn;
37 int __smatch_lineno = 0;
39 static char *base_file;
40 static const char *filename;
41 static char *pathname;
42 static char *full_filename;
43 static char *full_base_file;
44 static char *cur_func;
45 int base_file_stream;
46 static unsigned int loop_count;
47 static int last_goto_statement_handled;
48 int __expr_stmt_count;
49 int __in_function_def;
50 int __in_unmatched_hook;
51 static struct expression_list *switch_expr_stack = NULL;
52 static struct expression_list *post_op_stack = NULL;
54 static struct ptr_list *fn_data_list;
55 static struct ptr_list *backup;
57 struct expression_list *big_expression_stack;
58 struct statement_list *big_statement_stack;
59 struct statement *__prev_stmt;
60 struct statement *__cur_stmt;
61 struct statement *__next_stmt;
62 int __in_pre_condition = 0;
63 int __bail_on_rest_of_function = 0;
64 static struct timeval fn_start_time;
65 static struct timeval outer_fn_start_time;
66 char *get_function(void) { return cur_func; }
67 int get_lineno(void) { return __smatch_lineno; }
68 int inside_loop(void) { return !!loop_count; }
69 int definitely_inside_loop(void) { return !!(loop_count & ~0x08000000); }
70 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
71 int in_expression_statement(void) { return !!__expr_stmt_count; }
73 static void split_symlist(struct symbol_list *sym_list);
74 static void split_declaration(struct symbol_list *sym_list);
75 static void split_expr_list(struct expression_list *expr_list, struct expression *parent);
76 static void split_args(struct expression *expr);
77 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *call, struct expression *expr, int nr);
78 static void add_inline_function(struct symbol *sym);
79 static void parse_inline(struct expression *expr);
81 int option_assume_loops = 0;
82 int option_two_passes = 0;
83 struct symbol *cur_func_sym = NULL;
84 struct stree *global_states;
86 const unsigned long valid_ptr_min = 4096;
87 unsigned long valid_ptr_max = ULONG_MAX & ~(MTAG_OFFSET_MASK);
88 const sval_t valid_ptr_min_sval = {
89 .type = &ptr_ctype,
90 {.value = 4096},
92 sval_t valid_ptr_max_sval = {
93 .type = &ptr_ctype,
94 {.value = ULONG_MAX & ~(MTAG_OFFSET_MASK)},
96 struct range_list *valid_ptr_rl;
98 void alloc_valid_ptr_rl(void)
100 valid_ptr_max = sval_type_max(&ulong_ctype).value & ~(MTAG_OFFSET_MASK);
101 valid_ptr_max_sval.value = valid_ptr_max;
103 valid_ptr_rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
104 valid_ptr_rl = cast_rl(&ptr_ctype, valid_ptr_rl);
105 valid_ptr_rl = clone_rl_permanent(valid_ptr_rl);
108 int outside_of_function(void)
110 return cur_func_sym == NULL;
113 const char *get_filename(void)
115 if (option_info && option_full_path)
116 return full_base_file;
117 if (option_info)
118 return base_file;
119 if (option_full_path)
120 return full_filename;
121 return filename;
124 const char *get_base_file(void)
126 if (option_full_path)
127 return full_base_file;
128 return base_file;
131 unsigned long long get_file_id(void)
133 return str_to_llu_hash(get_filename());
136 unsigned long long get_base_file_id(void)
138 return str_to_llu_hash(get_base_file());
141 static void set_position(struct position pos)
143 int len;
144 static int prev_stream = -1;
146 if (in_fake_env)
147 return;
149 if (pos.stream == 0 && pos.line == 0)
150 return;
152 __smatch_lineno = pos.line;
154 if (pos.stream == prev_stream)
155 return;
157 filename = stream_name(pos.stream);
159 free(full_filename);
160 pathname = getcwd(NULL, 0);
161 if (pathname) {
162 len = strlen(pathname) + 1 + strlen(filename) + 1;
163 full_filename = malloc(len);
164 snprintf(full_filename, len, "%s/%s", pathname, filename);
165 } else {
166 full_filename = alloc_string(filename);
168 free(pathname);
171 int is_assigned_call(struct expression *expr)
173 struct expression *parent = expr_get_parent_expr(expr);
175 if (parent &&
176 parent->type == EXPR_ASSIGNMENT &&
177 parent->op == '=' &&
178 strip_expr(parent->right) == expr)
179 return 1;
181 return 0;
184 int is_fake_assigned_call(struct expression *expr)
186 struct expression *parent = expr_get_fake_parent_expr(expr);
188 if (parent &&
189 parent->type == EXPR_ASSIGNMENT &&
190 parent->op == '=' &&
191 strip_expr(parent->right) == expr)
192 return 1;
194 return 0;
197 static bool is_inline_func(struct expression *expr)
199 if (expr->type != EXPR_SYMBOL || !expr->symbol)
200 return false;
201 if (!expr->symbol->definition)
202 return false;
203 if (expr->symbol->definition->ctype.modifiers & MOD_INLINE)
204 return true;
206 return 0;
209 static int is_noreturn_func(struct expression *expr)
211 if (expr->type != EXPR_SYMBOL || !expr->symbol)
212 return 0;
215 * It's almost impossible for Smatch to handle __builtin_constant_p()
216 * the same way that GCC does so Smatch ends up making some functions
217 * as no return functions incorrectly.
220 if (option_project == PROJ_KERNEL && expr->symbol->ident &&
221 strstr(expr->symbol->ident->name, "__compiletime_assert"))
222 return 0;
224 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
225 return 1;
226 return 0;
229 static int save_func_time(void *_rl, int argc, char **argv, char **azColName)
231 unsigned long *rl = _rl;
233 *rl = strtoul(argv[0], NULL, 10);
234 return 0;
237 static int get_func_time(struct symbol *sym)
239 unsigned long time = 0;
241 run_sql(&save_func_time, &time,
242 "select key from return_implies where %s and type = %d;",
243 get_static_filter(sym), FUNC_TIME);
245 return time;
248 static int inline_budget = 20;
250 int inlinable(struct expression *expr)
252 struct symbol *sym;
253 struct statement *last_stmt = NULL;
255 if (__inline_fn) /* don't nest */
256 return 0;
258 if (expr->type != EXPR_SYMBOL || !expr->symbol)
259 return 0;
260 if (is_no_inline_function(expr->symbol->ident->name))
261 return 0;
262 sym = get_base_type(expr->symbol);
263 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
264 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
265 return 0;
266 if (sym->stmt->type != STMT_COMPOUND)
267 return 0;
268 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
270 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
271 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
272 return 0;
273 if (sym->inline_stmt->type != STMT_COMPOUND)
274 return 0;
275 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
278 if (!last_stmt)
279 return 0;
281 /* the magic numbers in this function are pulled out of my bum. */
282 if (last_stmt->pos.line > sym->pos.line + inline_budget)
283 return 0;
285 if (get_func_time(expr->symbol) >= 2)
286 return 0;
288 return 1;
291 void __process_post_op_stack(void)
293 struct expression *expr;
295 FOR_EACH_PTR(post_op_stack, expr) {
296 __pass_to_client(expr, OP_HOOK);
297 } END_FOR_EACH_PTR(expr);
299 __free_ptr_list((struct ptr_list **)&post_op_stack);
302 static int handle_comma_assigns(struct expression *expr)
304 struct expression *right;
305 struct expression *assign;
307 right = strip_expr(expr->right);
308 if (right->type != EXPR_COMMA)
309 return 0;
311 __split_expr(right->left);
312 __process_post_op_stack();
314 assign = assign_expression(expr->left, '=', right->right);
315 __split_expr(assign);
317 return 1;
320 /* This is to handle *p++ = foo; assignments */
321 static int handle_postop_assigns(struct expression *expr)
323 struct expression *left, *fake_left;
324 struct expression *assign;
326 left = strip_expr(expr->left);
327 if (left->type != EXPR_PREOP || left->op != '*')
328 return 0;
329 left = strip_expr(left->unop);
330 if (left->type != EXPR_POSTOP)
331 return 0;
333 fake_left = deref_expression(strip_expr(left->unop));
334 assign = assign_expression(fake_left, '=', expr->right);
336 __split_expr(assign);
337 __split_expr(expr->left);
339 return 1;
342 static int prev_expression_is_getting_address(struct expression *expr)
344 struct expression *parent;
346 do {
347 parent = expr_get_parent_expr(expr);
349 if (!parent)
350 return 0;
351 if (parent->type == EXPR_PREOP && parent->op == '&')
352 return 1;
353 if (parent->type == EXPR_PREOP && parent->op == '(')
354 goto next;
355 if (parent->type == EXPR_DEREF && parent->op == '.')
356 goto next;
357 /* Handle &foo->array[offset] */
358 if (parent->type == EXPR_BINOP && parent->op == '+') {
359 parent = expr_get_parent_expr(parent);
360 if (!parent)
361 return 0;
362 if (parent->type == EXPR_PREOP && parent->op == '*')
363 goto next;
366 return 0;
367 next:
368 expr = parent;
369 } while (1);
372 int __in_builtin_overflow_func;
373 static void handle_builtin_overflow_func(struct expression *expr)
375 struct expression *a, *b, *res, *assign;
376 int op;
378 if (sym_name_is("__builtin_add_overflow", expr->fn))
379 op = '+';
380 else if (sym_name_is("__builtin_sub_overflow", expr->fn))
381 op = '-';
382 else if (sym_name_is("__builtin_mul_overflow", expr->fn))
383 op = '*';
384 else
385 return;
387 a = get_argument_from_call_expr(expr->args, 0);
388 b = get_argument_from_call_expr(expr->args, 1);
389 res = get_argument_from_call_expr(expr->args, 2);
391 assign = assign_expression(deref_expression(res), '=', binop_expression(a, op, b));
393 __in_builtin_overflow_func++;
394 __split_expr(assign);
395 __in_builtin_overflow_func--;
398 static int handle__builtin_choose_expr(struct expression *expr)
400 struct expression *const_expr, *expr1, *expr2;
401 sval_t sval;
403 if (!sym_name_is("__builtin_choose_expr", expr->fn))
404 return 0;
406 const_expr = get_argument_from_call_expr(expr->args, 0);
407 expr1 = get_argument_from_call_expr(expr->args, 1);
408 expr2 = get_argument_from_call_expr(expr->args, 2);
410 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
411 return 0;
412 if (sval.value)
413 __split_expr(expr1);
414 else
415 __split_expr(expr2);
416 return 1;
419 static int handle__builtin_choose_expr_assigns(struct expression *expr)
421 struct expression *const_expr, *right, *expr1, *expr2, *fake;
422 sval_t sval;
424 right = strip_parens(expr->right);
425 if (right->type != EXPR_CALL)
426 return 0;
427 if (!sym_name_is("__builtin_choose_expr", right->fn))
428 return 0;
430 const_expr = get_argument_from_call_expr(right->args, 0);
431 expr1 = get_argument_from_call_expr(right->args, 1);
432 expr2 = get_argument_from_call_expr(right->args, 2);
434 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
435 return 0;
437 fake = assign_expression(expr->left, '=', sval.value ? expr1 : expr2);
438 __split_expr(fake);
439 return 1;
442 int is_condition_call(struct expression *expr)
444 struct expression *tmp;
446 FOR_EACH_PTR_REVERSE(big_condition_stack, tmp) {
447 if (expr == tmp || expr_get_parent_expr(expr) == tmp)
448 return 1;
449 if (tmp->pos.line < expr->pos.line)
450 return 0;
451 } END_FOR_EACH_PTR_REVERSE(tmp);
453 return 0;
456 static struct expression *expr_get_parent_no_parens(struct expression *expr)
458 do {
459 expr = expr_get_parent_expr(expr);
460 } while (expr &&
461 expr->type == EXPR_PREOP &&
462 expr->op == '(');
464 return expr;
467 static bool gen_fake_function_assign(struct expression *expr)
469 static struct expression *parsed;
470 struct expression *assign, *parent;
471 struct symbol *type;
472 char buf[64];
474 /* The rule is that every non-void function call has to be part of an
475 * assignment. TODO: Should we create a fake non-casted assignment
476 * for casted assignments? Also faked assigns for += assignments?
478 type = get_type(expr);
479 if (!type || type == &void_ctype)
480 return false;
482 parent = expr_get_parent_no_parens(expr);
483 if (parent && parent->type == EXPR_ASSIGNMENT)
484 return false;
486 parent = expr_get_fake_parent_expr(expr);
487 if (parent) {
488 struct expression *left = parent->left;
490 if (parent == parsed)
491 return false;
492 if (!left || left->type != EXPR_SYMBOL)
493 return false;
494 if (strncmp(left->symbol_name->name, "__fake_assign_", 14) != 0)
495 return false;
496 parsed = parent;
497 __split_expr(parent);
498 return true;
501 // TODO: faked_assign skipping conditions is a hack
502 if (is_condition_call(expr))
503 return false;
505 snprintf(buf, sizeof(buf), "__fake_assign_%p", expr);
506 assign = create_fake_assign(buf, get_type(expr), expr);
508 parsed = assign;
509 __split_expr(assign);
510 return true;
513 static void split_call(struct expression *expr)
515 if (gen_fake_function_assign(expr))
516 return;
518 expr_set_parent_expr(expr->fn, expr);
520 if (sym_name_is("__builtin_constant_p", expr->fn))
521 return;
522 if (handle__builtin_choose_expr(expr))
523 return;
524 __split_expr(expr->fn);
525 split_args(expr);
526 if (is_inline_func(expr->fn))
527 add_inline_function(expr->fn->symbol->definition);
528 if (inlinable(expr->fn))
529 __inline_call = 1;
530 __process_post_op_stack();
531 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
532 __pass_to_client(expr, FUNCTION_CALL_HOOK);
533 __inline_call = 0;
534 if (inlinable(expr->fn))
535 parse_inline(expr);
536 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
537 if (is_noreturn_func(expr->fn))
538 nullify_path();
539 if (!expr_get_parent_expr(expr))
540 __discard_fake_states(expr);
541 handle_builtin_overflow_func(expr);
544 void __split_expr(struct expression *expr)
546 if (!expr)
547 return;
549 // if (local_debug)
550 // sm_msg("Debug expr_type %d %s expr = '%s'", expr->type, show_special(expr->op), expr_to_str(expr));
552 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
553 return;
554 if (__in_fake_assign >= 4) /* don't allow too much nesting */
555 return;
557 push_expression(&big_expression_stack, expr);
558 set_position(expr->pos);
559 __pass_to_client(expr, EXPR_HOOK);
561 switch (expr->type) {
562 case EXPR_PREOP:
563 expr_set_parent_expr(expr->unop, expr);
565 if (expr->op == '*' &&
566 !prev_expression_is_getting_address(expr))
567 __pass_to_client(expr, DEREF_HOOK);
568 __split_expr(expr->unop);
569 __pass_to_client(expr, OP_HOOK);
570 break;
571 case EXPR_POSTOP:
572 expr_set_parent_expr(expr->unop, expr);
574 __split_expr(expr->unop);
575 push_expression(&post_op_stack, expr);
576 break;
577 case EXPR_STATEMENT:
578 __expr_stmt_count++;
579 if (expr->statement && !expr->statement) {
580 stmt_set_parent_stmt(expr->statement,
581 last_ptr_list((struct ptr_list *)big_statement_stack));
583 __split_stmt(expr->statement);
584 __expr_stmt_count--;
585 break;
586 case EXPR_LOGICAL:
587 case EXPR_COMPARE:
588 expr_set_parent_expr(expr->left, expr);
589 expr_set_parent_expr(expr->right, expr);
591 __pass_to_client(expr, LOGIC_HOOK);
592 __handle_logic(expr);
593 break;
594 case EXPR_BINOP:
595 expr_set_parent_expr(expr->left, expr);
596 expr_set_parent_expr(expr->right, expr);
598 __pass_to_client(expr, BINOP_HOOK);
599 __split_expr(expr->left);
600 __split_expr(expr->right);
601 break;
602 case EXPR_COMMA:
603 expr_set_parent_expr(expr->left, expr);
604 expr_set_parent_expr(expr->right, expr);
606 __split_expr(expr->left);
607 __process_post_op_stack();
608 __split_expr(expr->right);
609 break;
610 case EXPR_ASSIGNMENT: {
611 struct expression *right;
613 expr_set_parent_expr(expr->left, expr);
614 expr_set_parent_expr(expr->right, expr);
616 right = strip_expr(expr->right);
617 if (!right)
618 break;
620 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
622 /* foo = !bar() */
623 if (__handle_condition_assigns(expr))
624 goto after_assign;
625 /* foo = (x < 5 ? foo : 5); */
626 if (__handle_select_assigns(expr))
627 goto after_assign;
628 /* foo = ({frob(); frob(); frob(); 1;}) */
629 if (__handle_expr_statement_assigns(expr))
630 break; // FIXME: got after
631 /* foo = (3, 4); */
632 if (handle_comma_assigns(expr))
633 goto after_assign;
634 if (handle__builtin_choose_expr_assigns(expr))
635 goto after_assign;
636 if (handle_postop_assigns(expr))
637 break; /* no need to goto after_assign */
639 __split_expr(expr->right);
640 if (outside_of_function())
641 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
642 else
643 __pass_to_client(expr, ASSIGNMENT_HOOK);
645 __fake_struct_member_assignments(expr);
647 /* Re-examine ->right for inlines. See the commit message */
648 right = strip_expr(expr->right);
649 if (expr->op == '=' && right->type == EXPR_CALL)
650 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
652 after_assign:
653 if (get_macro_name(right->pos) &&
654 get_macro_name(expr->pos) != get_macro_name(right->pos))
655 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
657 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
658 __split_expr(expr->left);
659 break;
661 case EXPR_DEREF:
662 expr_set_parent_expr(expr->deref, expr);
664 __pass_to_client(expr, DEREF_HOOK);
665 __split_expr(expr->deref);
666 break;
667 case EXPR_SLICE:
668 expr_set_parent_expr(expr->base, expr);
670 __split_expr(expr->base);
671 break;
672 case EXPR_CAST:
673 case EXPR_FORCE_CAST:
674 expr_set_parent_expr(expr->cast_expression, expr);
676 __pass_to_client(expr, CAST_HOOK);
677 __split_expr(expr->cast_expression);
678 break;
679 case EXPR_SIZEOF:
680 if (expr->cast_expression)
681 __pass_to_client(strip_parens(expr->cast_expression),
682 SIZEOF_HOOK);
683 break;
684 case EXPR_OFFSETOF:
685 case EXPR_ALIGNOF:
686 break;
687 case EXPR_CONDITIONAL:
688 case EXPR_SELECT:
689 expr_set_parent_expr(expr->conditional, expr);
690 expr_set_parent_expr(expr->cond_true, expr);
691 expr_set_parent_expr(expr->cond_false, expr);
693 if (known_condition_true(expr->conditional)) {
694 __split_expr(expr->cond_true);
695 break;
697 if (known_condition_false(expr->conditional)) {
698 __split_expr(expr->cond_false);
699 break;
701 __pass_to_client(expr, SELECT_HOOK);
702 __split_whole_condition(expr->conditional);
703 __split_expr(expr->cond_true);
704 __push_true_states();
705 __use_false_states();
706 __split_expr(expr->cond_false);
707 __merge_true_states();
708 break;
709 case EXPR_CALL:
710 split_call(expr);
711 break;
712 case EXPR_INITIALIZER:
713 split_expr_list(expr->expr_list, expr);
714 break;
715 case EXPR_IDENTIFIER:
716 expr_set_parent_expr(expr->ident_expression, expr);
717 __split_expr(expr->ident_expression);
718 break;
719 case EXPR_INDEX:
720 expr_set_parent_expr(expr->idx_expression, expr);
721 __split_expr(expr->idx_expression);
722 break;
723 case EXPR_POS:
724 expr_set_parent_expr(expr->init_expr, expr);
725 __split_expr(expr->init_expr);
726 break;
727 case EXPR_SYMBOL:
728 __pass_to_client(expr, SYM_HOOK);
729 break;
730 case EXPR_STRING:
731 __pass_to_client(expr, STRING_HOOK);
732 break;
733 case EXPR_GENERIC: {
734 struct expression *tmp;
736 tmp = strip_Generic(expr);
737 if (tmp != expr)
738 __split_expr(tmp);
739 break;
741 default:
742 break;
744 __pass_to_client(expr, EXPR_HOOK_AFTER);
745 pop_expression(&big_expression_stack);
748 static int is_forever_loop(struct statement *stmt)
750 struct expression *expr;
751 sval_t sval;
753 expr = strip_expr(stmt->iterator_pre_condition);
754 if (!expr)
755 expr = stmt->iterator_post_condition;
756 if (!expr) {
757 /* this is a for(;;) loop... */
758 return 1;
761 if (get_value(expr, &sval) && sval.value != 0)
762 return 1;
764 return 0;
767 static int loop_num;
768 static char *get_loop_name(int num)
770 char buf[256];
772 snprintf(buf, 255, "-loop%d", num);
773 buf[255] = '\0';
774 return alloc_sname(buf);
778 * Pre Loops are while and for loops.
780 static void handle_pre_loop(struct statement *stmt)
782 int once_through; /* we go through the loop at least once */
783 struct sm_state *extra_sm = NULL;
784 int unchanged = 0;
785 char *loop_name;
786 struct stree *stree = NULL;
787 struct sm_state *sm = NULL;
789 loop_name = get_loop_name(loop_num);
790 loop_num++;
792 if (stmt->iterator_pre_statement) {
793 __split_stmt(stmt->iterator_pre_statement);
794 __prev_stmt = stmt->iterator_pre_statement;
797 once_through = implied_condition_true(stmt->iterator_pre_condition);
799 loop_count++;
800 __push_continues();
801 __push_breaks();
803 __merge_gotos(loop_name, NULL);
805 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
806 __in_pre_condition++;
807 __pass_to_client(stmt, PRELOOP_HOOK);
808 __split_whole_condition(stmt->iterator_pre_condition);
809 __in_pre_condition--;
810 FOR_EACH_SM(stree, sm) {
811 set_state(sm->owner, sm->name, sm->sym, sm->state);
812 } END_FOR_EACH_SM(sm);
813 free_stree(&stree);
814 if (extra_sm)
815 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
817 if (option_assume_loops)
818 once_through = 1;
820 __split_stmt(stmt->iterator_statement);
821 if (is_forever_loop(stmt)) {
822 __merge_continues();
823 __save_gotos(loop_name, NULL);
825 __push_fake_cur_stree();
826 __split_stmt(stmt->iterator_post_statement);
827 stree = __pop_fake_cur_stree();
829 __discard_false_states();
830 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
831 __use_breaks();
833 if (!__path_is_null())
834 __merge_stree_into_cur(stree);
835 free_stree(&stree);
836 } else {
837 __merge_continues();
838 unchanged = __iterator_unchanged(extra_sm);
839 __split_stmt(stmt->iterator_post_statement);
840 __prev_stmt = stmt->iterator_post_statement;
841 __cur_stmt = stmt;
843 __save_gotos(loop_name, NULL);
844 __in_pre_condition++;
845 __split_whole_condition(stmt->iterator_pre_condition);
846 __in_pre_condition--;
847 nullify_path();
848 __merge_false_states();
849 if (once_through)
850 __discard_false_states();
851 else
852 __merge_false_states();
854 if (extra_sm && unchanged)
855 __extra_pre_loop_hook_after(extra_sm,
856 stmt->iterator_post_statement,
857 stmt->iterator_pre_condition);
858 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
859 __merge_breaks();
861 loop_count--;
865 * Post loops are do {} while();
867 static void handle_post_loop(struct statement *stmt)
869 char *loop_name;
871 loop_name = get_loop_name(loop_num);
872 loop_num++;
873 loop_count++;
875 __pass_to_client(stmt, POSTLOOP_HOOK);
877 __push_continues();
878 __push_breaks();
879 __merge_gotos(loop_name, NULL);
880 __split_stmt(stmt->iterator_statement);
881 __merge_continues();
882 if (!expr_is_zero(stmt->iterator_post_condition))
883 __save_gotos(loop_name, NULL);
885 if (is_forever_loop(stmt)) {
886 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
887 __use_breaks();
888 } else {
889 __split_whole_condition(stmt->iterator_post_condition);
890 __use_false_states();
891 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
892 __merge_breaks();
894 loop_count--;
897 static int empty_statement(struct statement *stmt)
899 if (!stmt)
900 return 0;
901 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
902 return 1;
903 return 0;
906 static int last_stmt_on_same_line(void)
908 struct statement *stmt;
909 int i = 0;
911 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
912 if (!i++)
913 continue;
914 if (stmt->pos.line == get_lineno())
915 return 1;
916 return 0;
917 } END_FOR_EACH_PTR_REVERSE(stmt);
918 return 0;
921 static void split_asm_ops(struct asm_operand_list *ops)
923 struct asm_operand *op;
925 FOR_EACH_PTR(ops, op) {
926 __split_expr(op->expr);
927 } END_FOR_EACH_PTR(op);
930 static int is_case_val(struct statement *stmt, sval_t sval)
932 sval_t case_sval;
934 if (stmt->type != STMT_CASE)
935 return 0;
936 if (!stmt->case_expression) {
937 __set_default();
938 return 1;
940 if (!get_value(stmt->case_expression, &case_sval))
941 return 0;
942 if (case_sval.value == sval.value)
943 return 1;
944 return 0;
947 static struct range_list *get_case_rl(struct expression *switch_expr,
948 struct expression *case_expr,
949 struct expression *case_to)
951 sval_t start, end;
952 struct range_list *rl = NULL;
953 struct symbol *switch_type;
955 switch_type = get_type(switch_expr);
956 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
957 start = sval_cast(switch_type, start);
958 end = sval_cast(switch_type, end);
959 add_range(&rl, start, end);
960 } else if (get_value(case_expr, &start)) {
961 start = sval_cast(switch_type, start);
962 add_range(&rl, start, start);
965 return rl;
968 static void split_known_switch(struct statement *stmt, sval_t sval)
970 struct statement *tmp;
971 struct range_list *rl;
973 __split_expr(stmt->switch_expression);
974 sval = sval_cast(get_type(stmt->switch_expression), sval);
976 push_expression(&switch_expr_stack, stmt->switch_expression);
977 __save_switch_states(top_expression(switch_expr_stack));
978 nullify_path();
979 __push_default();
980 __push_breaks();
982 stmt = stmt->switch_statement;
984 __push_scope_hooks();
985 FOR_EACH_PTR(stmt->stmts, tmp) {
986 __smatch_lineno = tmp->pos.line;
987 // FIXME: what if default comes before the known case statement?
988 if (is_case_val(tmp, sval)) {
989 rl = alloc_rl(sval, sval);
990 __merge_switches(top_expression(switch_expr_stack), rl);
991 __pass_case_to_client(top_expression(switch_expr_stack), rl);
992 stmt_set_parent_stmt(tmp->case_statement, tmp);
993 __split_stmt(tmp->case_statement);
994 goto next;
996 if (__path_is_null())
997 continue;
998 __split_stmt(tmp);
999 next:
1000 if (__path_is_null()) {
1001 __set_default();
1002 goto out;
1004 } END_FOR_EACH_PTR(tmp);
1005 out:
1006 __call_scope_hooks();
1007 if (!__pop_default())
1008 __merge_switches(top_expression(switch_expr_stack), NULL);
1009 __discard_switches();
1010 __merge_breaks();
1011 pop_expression(&switch_expr_stack);
1014 static void split_case(struct statement *stmt)
1016 struct range_list *rl = NULL;
1018 expr_set_parent_stmt(stmt->case_expression, stmt);
1019 expr_set_parent_stmt(stmt->case_to, stmt);
1021 rl = get_case_rl(top_expression(switch_expr_stack),
1022 stmt->case_expression, stmt->case_to);
1023 while (stmt->case_statement->type == STMT_CASE) {
1024 struct range_list *tmp;
1026 tmp = get_case_rl(top_expression(switch_expr_stack),
1027 stmt->case_statement->case_expression,
1028 stmt->case_statement->case_to);
1029 if (!tmp)
1030 goto next;
1031 rl = rl_union(rl, tmp);
1032 if (!stmt->case_expression)
1033 __set_default();
1034 next:
1035 stmt = stmt->case_statement;
1038 __merge_switches(top_expression(switch_expr_stack), rl);
1040 if (!stmt->case_expression)
1041 __set_default();
1043 stmt_set_parent_stmt(stmt->case_statement, stmt);
1044 __split_stmt(stmt->case_statement);
1047 int time_parsing_function(void)
1049 return ms_since(&fn_start_time) / 1000;
1052 bool taking_too_long(void)
1054 if ((ms_since(&outer_fn_start_time) / 1000) > 60 * 5) /* five minutes */
1055 return 1;
1056 return 0;
1059 struct statement *get_last_stmt(void)
1061 struct symbol *fn;
1062 struct statement *stmt;
1064 fn = get_base_type(cur_func_sym);
1065 if (!fn)
1066 return NULL;
1067 stmt = fn->stmt;
1068 if (!stmt)
1069 stmt = fn->inline_stmt;
1070 if (!stmt || stmt->type != STMT_COMPOUND)
1071 return NULL;
1072 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1073 if (stmt && stmt->type == STMT_LABEL)
1074 stmt = stmt->label_statement;
1075 return stmt;
1078 int is_last_stmt(struct statement *cur_stmt)
1080 struct statement *last;
1082 last = get_last_stmt();
1083 if (last && last == cur_stmt)
1084 return 1;
1085 return 0;
1088 static void handle_backward_goto(struct statement *goto_stmt)
1090 const char *goto_name, *label_name;
1091 struct statement *func_stmt;
1092 struct symbol *base_type = get_base_type(cur_func_sym);
1093 struct statement *tmp;
1094 int found = 0;
1096 if (!option_info)
1097 return;
1098 if (last_goto_statement_handled)
1099 return;
1100 last_goto_statement_handled = 1;
1102 if (!goto_stmt->goto_label ||
1103 goto_stmt->goto_label->type != SYM_LABEL ||
1104 !goto_stmt->goto_label->ident)
1105 return;
1106 goto_name = goto_stmt->goto_label->ident->name;
1108 func_stmt = base_type->stmt;
1109 if (!func_stmt)
1110 func_stmt = base_type->inline_stmt;
1111 if (!func_stmt)
1112 return;
1113 if (func_stmt->type != STMT_COMPOUND)
1114 return;
1116 FOR_EACH_PTR(func_stmt->stmts, tmp) {
1117 if (!found) {
1118 if (tmp->type != STMT_LABEL)
1119 continue;
1120 if (!tmp->label_identifier ||
1121 tmp->label_identifier->type != SYM_LABEL ||
1122 !tmp->label_identifier->ident)
1123 continue;
1124 label_name = tmp->label_identifier->ident->name;
1125 if (strcmp(goto_name, label_name) != 0)
1126 continue;
1127 found = 1;
1129 __split_stmt(tmp);
1130 } END_FOR_EACH_PTR(tmp);
1133 static void fake_a_return(void)
1135 struct expression *ret = NULL;
1137 nullify_path();
1138 __unnullify_path();
1140 if (cur_func_return_type() != &void_ctype)
1141 ret = unknown_value_expression(NULL);
1143 __pass_to_client(ret, RETURN_HOOK);
1144 nullify_path();
1147 static void split_ret_value(struct expression *expr)
1149 struct symbol *type;
1151 if (!expr)
1152 return;
1154 type = get_real_base_type(cur_func_sym);
1155 type = get_real_base_type(type);
1156 expr = fake_a_variable_assign(type, NULL, expr, -1);
1158 __in_fake_var_assign++;
1159 __split_expr(expr);
1160 __in_fake_var_assign--;
1163 static void fake_an_empty_default(struct position pos)
1165 static struct statement none = {};
1167 none.pos = pos;
1168 none.type = STMT_NONE;
1169 __merge_switches(top_expression(switch_expr_stack), NULL);
1170 __split_stmt(&none);
1173 static void split_compound(struct statement *stmt)
1175 struct statement *prev = NULL;
1176 struct statement *cur = NULL;
1177 struct statement *next;
1179 __push_scope_hooks();
1181 FOR_EACH_PTR(stmt->stmts, next) {
1182 /* just set them all ahead of time */
1183 stmt_set_parent_stmt(next, stmt);
1185 if (cur) {
1186 __prev_stmt = prev;
1187 __next_stmt = next;
1188 __cur_stmt = cur;
1189 __split_stmt(cur);
1191 prev = cur;
1192 cur = next;
1193 } END_FOR_EACH_PTR(next);
1194 if (cur) {
1195 __prev_stmt = prev;
1196 __cur_stmt = cur;
1197 __next_stmt = NULL;
1198 __split_stmt(cur);
1202 * For function scope, then delay calling the scope hooks until the
1203 * end of function hooks can run. I'm not positive this is the right
1204 * thing...
1206 if (!is_last_stmt(cur))
1207 __call_scope_hooks();
1211 * This is a hack, work around for detecting empty functions.
1213 static int need_delayed_scope_hooks(void)
1215 struct symbol *fn = get_base_type(cur_func_sym);
1216 struct statement *stmt;
1218 if (!fn)
1219 return 0;
1220 stmt = fn->stmt;
1221 if (!stmt)
1222 stmt = fn->inline_stmt;
1223 if (stmt && stmt->type == STMT_COMPOUND)
1224 return 1;
1225 return 0;
1228 void __split_label_stmt(struct statement *stmt)
1230 if (stmt->label_identifier &&
1231 stmt->label_identifier->type == SYM_LABEL &&
1232 stmt->label_identifier->ident) {
1233 loop_count |= 0x0800000;
1234 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
1238 static void find_asm_gotos(struct statement *stmt)
1240 struct symbol *sym;
1242 FOR_EACH_PTR(stmt->asm_labels, sym) {
1243 __save_gotos(sym->ident->name, sym);
1244 } END_FOR_EACH_PTR(sym);
1247 void __split_stmt(struct statement *stmt)
1249 static int indent_cnt;
1250 sval_t sval;
1251 struct timeval start, stop;
1252 bool skip_after = false;
1254 gettimeofday(&start, NULL);
1256 if (!stmt)
1257 goto out;
1259 if (!__in_fake_assign)
1260 __silence_warnings_for_stmt = false;
1262 if (__bail_on_rest_of_function || is_skipped_function())
1263 return;
1265 if (out_of_memory() || taking_too_long()) {
1266 gettimeofday(&start, NULL);
1268 __bail_on_rest_of_function = 1;
1269 final_pass = 1;
1270 sm_perror("Function too hairy. Giving up. %lu seconds",
1271 start.tv_sec - fn_start_time.tv_sec);
1272 fake_a_return();
1273 final_pass = 0; /* turn off sm_msg() from here */
1274 return;
1277 indent_cnt++;
1279 add_ptr_list(&big_statement_stack, stmt);
1280 free_expression_stack(&big_expression_stack);
1281 set_position(stmt->pos);
1282 __pass_to_client(stmt, STMT_HOOK);
1284 switch (stmt->type) {
1285 case STMT_DECLARATION:
1286 split_declaration(stmt->declaration);
1287 break;
1288 case STMT_RETURN:
1289 expr_set_parent_stmt(stmt->ret_value, stmt);
1291 split_ret_value(stmt->ret_value);
1292 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1293 __process_post_op_stack();
1294 nullify_path();
1295 break;
1296 case STMT_EXPRESSION:
1297 expr_set_parent_stmt(stmt->expression, stmt);
1298 expr_set_parent_stmt(stmt->context, stmt);
1300 __split_expr(stmt->expression);
1301 break;
1302 case STMT_COMPOUND:
1303 split_compound(stmt);
1304 break;
1305 case STMT_IF:
1306 stmt_set_parent_stmt(stmt->if_true, stmt);
1307 stmt_set_parent_stmt(stmt->if_false, stmt);
1308 expr_set_parent_stmt(stmt->if_conditional, stmt);
1310 if (known_condition_true(stmt->if_conditional)) {
1311 __split_stmt(stmt->if_true);
1312 break;
1314 if (known_condition_false(stmt->if_conditional)) {
1315 __split_stmt(stmt->if_false);
1316 break;
1318 __split_whole_condition(stmt->if_conditional);
1319 __split_stmt(stmt->if_true);
1320 if (empty_statement(stmt->if_true) &&
1321 last_stmt_on_same_line() &&
1322 !get_macro_name(stmt->if_true->pos))
1323 sm_warning("if();");
1324 __push_true_states();
1325 __use_false_states();
1326 __split_stmt(stmt->if_false);
1327 __merge_true_states();
1328 break;
1329 case STMT_ITERATOR:
1330 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1331 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1332 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1333 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1334 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1336 if (stmt->iterator_pre_condition)
1337 handle_pre_loop(stmt);
1338 else if (stmt->iterator_post_condition)
1339 handle_post_loop(stmt);
1340 else {
1341 // these are for(;;) type loops.
1342 handle_pre_loop(stmt);
1344 break;
1345 case STMT_SWITCH:
1346 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1347 expr_set_parent_stmt(stmt->switch_expression, stmt);
1349 if (get_value(stmt->switch_expression, &sval)) {
1350 split_known_switch(stmt, sval);
1351 break;
1353 __split_expr(stmt->switch_expression);
1354 push_expression(&switch_expr_stack, stmt->switch_expression);
1355 __save_switch_states(top_expression(switch_expr_stack));
1356 nullify_path();
1357 __push_default();
1358 __push_breaks();
1359 __split_stmt(stmt->switch_statement);
1360 if (!__pop_default() && have_remaining_cases())
1361 fake_an_empty_default(stmt->pos);
1362 __discard_switches();
1363 __merge_breaks();
1364 pop_expression(&switch_expr_stack);
1365 break;
1366 case STMT_CASE:
1367 split_case(stmt);
1368 break;
1369 case STMT_LABEL:
1370 __split_label_stmt(stmt);
1371 __pass_to_client(stmt, STMT_HOOK_AFTER);
1372 skip_after = true;
1373 __split_stmt(stmt->label_statement);
1374 break;
1375 case STMT_GOTO:
1376 expr_set_parent_stmt(stmt->goto_expression, stmt);
1378 __split_expr(stmt->goto_expression);
1379 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1380 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1381 __process_breaks();
1382 } else if (!strcmp(stmt->goto_label->ident->name,
1383 "continue")) {
1384 __process_continues();
1386 } else if (stmt->goto_label &&
1387 stmt->goto_label->type == SYM_LABEL &&
1388 stmt->goto_label->ident) {
1389 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1391 nullify_path();
1392 if (is_last_stmt(stmt))
1393 handle_backward_goto(stmt);
1394 break;
1395 case STMT_NONE:
1396 break;
1397 case STMT_ASM:
1398 expr_set_parent_stmt(stmt->asm_string, stmt);
1400 find_asm_gotos(stmt);
1401 __pass_to_client(stmt, ASM_HOOK);
1402 __split_expr(stmt->asm_string);
1403 split_asm_ops(stmt->asm_outputs);
1404 split_asm_ops(stmt->asm_inputs);
1405 split_expr_list(stmt->asm_clobbers, NULL);
1406 break;
1407 case STMT_CONTEXT:
1408 break;
1409 case STMT_RANGE:
1410 __split_expr(stmt->range_expression);
1411 __split_expr(stmt->range_low);
1412 __split_expr(stmt->range_high);
1413 break;
1415 if (!skip_after)
1416 __pass_to_client(stmt, STMT_HOOK_AFTER);
1417 if (--indent_cnt == 1)
1418 __discard_fake_states(NULL);
1420 out:
1421 __process_post_op_stack();
1423 gettimeofday(&stop, NULL);
1424 if (option_time_stmt && stmt)
1425 sm_msg("stmt_time%s: %ld",
1426 stmt->type == STMT_COMPOUND ? "_block" : "",
1427 stop.tv_sec - start.tv_sec);
1430 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1432 struct expression *expr;
1434 FOR_EACH_PTR(expr_list, expr) {
1435 expr_set_parent_expr(expr, parent);
1436 __split_expr(expr);
1437 __process_post_op_stack();
1438 } END_FOR_EACH_PTR(expr);
1441 static bool cast_arg(struct symbol *type, struct expression *arg)
1443 struct symbol *orig;
1445 if (!type)
1446 return false;
1448 arg = strip_parens(arg);
1449 if (arg != strip_expr(arg))
1450 return true;
1452 orig = get_type(arg);
1453 if (!orig)
1454 return true;
1455 if (types_equiv(orig, type))
1456 return false;
1458 if (orig->type == SYM_ARRAY && type->type == SYM_PTR)
1459 return true;
1462 * I would have expected that we could just do use (orig == type) but I
1463 * guess for pointers we need to get the basetype to do that comparison.
1467 if (orig->type != SYM_PTR ||
1468 type->type != SYM_PTR) {
1469 if (type_fits(type, orig))
1470 return false;
1471 return true;
1473 orig = get_real_base_type(orig);
1474 type = get_real_base_type(type);
1475 if (orig == type)
1476 return false;
1478 return true;
1481 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *call, struct expression *expr, int nr)
1483 char buf[64];
1484 bool cast;
1486 if (!expr || !cur_func_sym)
1487 return NULL;
1489 if (expr->type == EXPR_ASSIGNMENT)
1490 return expr;
1492 /* for va_args then we don't know the type */
1493 if (!type)
1494 type = get_type(expr);
1496 cast = cast_arg(type, expr);
1498 * Using expr_to_sym() here is a hack. We want to say that we don't
1499 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1500 * It turns out faking these assignments is way more expensive than I
1501 * would have imagined. I'm not sure why exactly.
1504 if (!cast) {
1506 * if the code is "return *p;" where "p" is a user pointer then
1507 * we want to create a fake assignment so that it sets the state
1508 * in check_kernel_user_data.c.
1511 if (expr->type != EXPR_PREOP &&
1512 expr->op != '*' && expr->op != '&' &&
1513 expr_to_sym(expr))
1514 return expr;
1517 if (nr == -1)
1518 snprintf(buf, sizeof(buf), "__fake_return_%p", expr);
1519 else
1520 snprintf(buf, sizeof(buf), "__fake_param_%p_%d", call, nr);
1522 return create_fake_assign(buf, type, expr);
1525 static void split_args(struct expression *expr)
1527 struct expression *arg, *tmp;
1528 struct symbol *type;
1529 int i;
1531 i = -1;
1532 FOR_EACH_PTR(expr->args, arg) {
1533 i++;
1534 expr_set_parent_expr(arg, expr);
1535 type = get_arg_type(expr->fn, i);
1536 tmp = fake_a_variable_assign(type, expr, arg, i);
1537 if (tmp != arg)
1538 __in_fake_var_assign++;
1539 __split_expr(tmp);
1540 if (tmp != arg)
1541 __in_fake_var_assign--;
1542 __process_post_op_stack();
1543 } END_FOR_EACH_PTR(arg);
1546 static void split_sym(struct symbol *sym)
1548 if (!sym)
1549 return;
1550 if (!(sym->namespace & NS_SYMBOL))
1551 return;
1553 __split_stmt(sym->stmt);
1554 __split_expr(sym->array_size);
1555 split_symlist(sym->arguments);
1556 split_symlist(sym->symbol_list);
1557 __split_stmt(sym->inline_stmt);
1558 split_symlist(sym->inline_symbol_list);
1561 static void split_symlist(struct symbol_list *sym_list)
1563 struct symbol *sym;
1565 FOR_EACH_PTR(sym_list, sym) {
1566 split_sym(sym);
1567 } END_FOR_EACH_PTR(sym);
1570 typedef void (fake_cb)(struct expression *expr);
1572 static int member_to_number(struct expression *expr, struct ident *member)
1574 struct symbol *type, *tmp;
1575 char *name;
1576 int i;
1578 if (!member)
1579 return -1;
1580 name = member->name;
1582 type = get_type(expr);
1583 if (!type || type->type != SYM_STRUCT)
1584 return -1;
1586 i = -1;
1587 FOR_EACH_PTR(type->symbol_list, tmp) {
1588 i++;
1589 if (!tmp->ident)
1590 continue;
1591 if (strcmp(name, tmp->ident->name) == 0)
1592 return i;
1593 } END_FOR_EACH_PTR(tmp);
1594 return -1;
1597 static struct ident *number_to_member(struct expression *expr, int num)
1599 struct symbol *type, *member;
1600 int i = 0;
1602 type = get_type(expr);
1603 if (!type || type->type != SYM_STRUCT)
1604 return NULL;
1606 FOR_EACH_PTR(type->symbol_list, member) {
1607 if (i == num)
1608 return member->ident;
1609 i++;
1610 } END_FOR_EACH_PTR(member);
1611 return NULL;
1614 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1616 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1618 struct expression *edge_member, *assign;
1619 struct symbol *base = get_real_base_type(member);
1620 struct symbol *tmp;
1622 if (member->ident)
1623 expr = member_expression(expr, '.', member->ident);
1625 FOR_EACH_PTR(base->symbol_list, tmp) {
1626 struct symbol *type;
1628 type = get_real_base_type(tmp);
1629 if (!type)
1630 continue;
1632 edge_member = member_expression(expr, '.', tmp->ident);
1633 if (get_extra_state(edge_member))
1634 continue;
1636 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1637 set_inner_struct_members(expr, tmp);
1638 continue;
1641 if (!tmp->ident)
1642 continue;
1644 assign = assign_expression(edge_member, '=', zero_expr());
1645 __split_expr(assign);
1646 } END_FOR_EACH_PTR(tmp);
1651 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1653 struct symbol *tmp;
1654 struct expression *member = NULL;
1655 struct expression *assign;
1657 FOR_EACH_PTR(type->symbol_list, tmp) {
1658 type = get_real_base_type(tmp);
1659 if (!type)
1660 continue;
1662 if (tmp->ident) {
1663 member = member_expression(expr, '.', tmp->ident);
1664 if (get_extra_state(member))
1665 continue;
1668 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1669 set_inner_struct_members(expr, tmp);
1670 continue;
1672 if (type->type == SYM_ARRAY)
1673 continue;
1674 if (!tmp->ident)
1675 continue;
1677 assign = assign_expression(member, '=', zero_expr());
1678 __split_expr(assign);
1679 } END_FOR_EACH_PTR(tmp);
1682 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1684 struct expression *deref, *assign, *tmp, *right;
1685 struct symbol *struct_type, *type;
1686 struct ident *member;
1687 int member_idx;
1689 struct_type = get_type(symbol);
1690 if (!struct_type ||
1691 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1692 return;
1695 * We're parsing an initializer that could look something like this:
1696 * struct foo foo = {
1697 * 42,
1698 * .whatever.xxx = 11,
1699 * .zzz = 12,
1700 * };
1702 * So what we have here is a list with 42, .whatever, and .zzz. We need
1703 * to break it up into left and right sides of the assignments.
1706 member_idx = 0;
1707 FOR_EACH_PTR(members, tmp) {
1708 deref = NULL;
1709 if (tmp->type == EXPR_IDENTIFIER) {
1710 member_idx = member_to_number(symbol, tmp->expr_ident);
1711 while (tmp->type == EXPR_IDENTIFIER) {
1712 member = tmp->expr_ident;
1713 tmp = tmp->ident_expression;
1714 if (deref)
1715 deref = member_expression(deref, '.', member);
1716 else
1717 deref = member_expression(symbol, '.', member);
1719 } else {
1720 member = number_to_member(symbol, member_idx);
1721 deref = member_expression(symbol, '.', member);
1723 right = tmp;
1724 member_idx++;
1725 if (right->type == EXPR_INITIALIZER) {
1726 type = get_type(deref);
1727 if (type && type->type == SYM_ARRAY)
1728 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1729 else
1730 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1731 } else {
1732 assign = assign_expression(deref, '=', right);
1733 fake_cb(assign);
1735 } END_FOR_EACH_PTR(tmp);
1737 set_unset_to_zero(struct_type, symbol);
1740 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1742 fake_member_assigns_helper(symbol_expression(sym),
1743 sym->initializer->expr_list, fake_cb);
1746 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1748 struct expression *offset, *binop, *assign, *tmp;
1749 struct symbol *type;
1750 int idx, max;
1752 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1753 return;
1755 max = 0;
1756 idx = 0;
1757 FOR_EACH_PTR(expr_list, tmp) {
1758 if (tmp->type == EXPR_INDEX) {
1759 if (tmp->idx_from != tmp->idx_to)
1760 return;
1761 idx = tmp->idx_from;
1762 if (idx > max)
1763 max = idx;
1764 if (!tmp->idx_expression)
1765 goto next;
1766 tmp = tmp->idx_expression;
1768 offset = value_expr(idx);
1769 binop = array_element_expression(array, offset);
1770 if (tmp->type == EXPR_INITIALIZER) {
1771 type = get_type(binop);
1772 if (type && type->type == SYM_ARRAY)
1773 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1774 else
1775 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1776 } else {
1777 assign = assign_expression(binop, '=', tmp);
1778 fake_cb(assign);
1780 next:
1781 idx++;
1782 if (idx > max)
1783 max = idx;
1784 } END_FOR_EACH_PTR(tmp);
1786 __call_array_initialized_hooks(array, max);
1789 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1791 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1794 static void fake_assign_expr(struct symbol *sym)
1796 struct expression *assign, *symbol;
1798 symbol = symbol_expression(sym);
1799 assign = assign_expression(symbol, '=', sym->initializer);
1800 __split_expr(assign);
1803 static void do_initializer_stuff(struct symbol *sym)
1805 if (!sym->initializer)
1806 return;
1808 if (sym->initializer->type == EXPR_INITIALIZER) {
1809 if (get_real_base_type(sym)->type == SYM_ARRAY)
1810 fake_element_assigns(sym, __split_expr);
1811 else
1812 fake_member_assigns(sym, __split_expr);
1813 } else {
1814 fake_assign_expr(sym);
1818 static void split_declaration(struct symbol_list *sym_list)
1820 struct symbol *sym;
1822 FOR_EACH_PTR(sym_list, sym) {
1823 __pass_to_client(sym, DECLARATION_HOOK);
1824 do_initializer_stuff(sym);
1825 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
1826 split_sym(sym);
1827 } END_FOR_EACH_PTR(sym);
1830 static void call_global_assign_hooks(struct expression *assign)
1832 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1835 static void fake_global_assign(struct symbol *sym)
1837 struct expression *assign, *symbol;
1839 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1840 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1841 fake_element_assigns(sym, call_global_assign_hooks);
1842 } else if (sym->initializer) {
1843 symbol = symbol_expression(sym);
1844 assign = assign_expression(symbol, '=', sym->initializer);
1845 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1846 } else {
1847 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1849 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1850 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1851 fake_member_assigns(sym, call_global_assign_hooks);
1852 } else if (sym->initializer) {
1853 symbol = symbol_expression(sym);
1854 assign = assign_expression(symbol, '=', sym->initializer);
1855 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1856 } else {
1857 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1859 } else {
1860 symbol = symbol_expression(sym);
1861 if (sym->initializer) {
1862 assign = assign_expression(symbol, '=', sym->initializer);
1863 __split_expr(assign);
1864 } else {
1865 assign = assign_expression(symbol, '=', zero_expr());
1867 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1871 static void start_function_definition(struct symbol *sym)
1873 __in_function_def = 1;
1874 __pass_to_client(sym, FUNC_DEF_HOOK);
1875 __in_function_def = 0;
1876 __pass_to_client(sym, AFTER_DEF_HOOK);
1880 void add_function_data(unsigned long *fn_data)
1882 __add_ptr_list(&fn_data_list, fn_data);
1885 static void clear_function_data(void)
1887 unsigned long *tmp;
1889 FOR_EACH_PTR(fn_data_list, tmp) {
1890 *tmp = 0;
1891 } END_FOR_EACH_PTR(tmp);
1894 static void record_func_time(void)
1896 struct timeval stop;
1897 int func_time;
1898 char buf[32];
1900 gettimeofday(&stop, NULL);
1901 func_time = stop.tv_sec - fn_start_time.tv_sec;
1902 snprintf(buf, sizeof(buf), "%d", func_time);
1903 sql_insert_return_implies(FUNC_TIME, 0, "", buf);
1904 if (option_time && func_time > 2) {
1905 final_pass++;
1906 sm_msg("func_time: %d", func_time);
1907 final_pass--;
1911 static void split_function(struct symbol *sym)
1913 struct symbol *base_type = get_base_type(sym);
1915 if (!base_type->stmt && !base_type->inline_stmt)
1916 return;
1918 gettimeofday(&outer_fn_start_time, NULL);
1919 gettimeofday(&fn_start_time, NULL);
1920 cur_func_sym = sym;
1921 if (sym->ident)
1922 cur_func = sym->ident->name;
1923 set_position(sym->pos);
1924 clear_function_data();
1925 loop_count = 0;
1926 last_goto_statement_handled = 0;
1927 sm_debug("new function: %s\n", cur_func);
1928 __stree_id = 0;
1929 if (option_two_passes) {
1930 __unnullify_path();
1931 loop_num = 0;
1932 final_pass = 0;
1933 start_function_definition(sym);
1934 __split_stmt(base_type->stmt);
1935 __split_stmt(base_type->inline_stmt);
1936 nullify_path();
1938 __unnullify_path();
1939 loop_num = 0;
1940 final_pass = 1;
1941 start_function_definition(sym);
1942 __split_stmt(base_type->stmt);
1943 __split_stmt(base_type->inline_stmt);
1944 if (!__path_is_null() &&
1945 cur_func_return_type() == &void_ctype &&
1946 !__bail_on_rest_of_function) {
1947 __pass_to_client(NULL, RETURN_HOOK);
1948 nullify_path();
1950 __pass_to_client(sym, END_FUNC_HOOK);
1951 if (need_delayed_scope_hooks())
1952 __call_scope_hooks();
1953 __pass_to_client(sym, AFTER_FUNC_HOOK);
1954 sym->parsed = true;
1956 clear_all_states();
1958 record_func_time();
1960 cur_func_sym = NULL;
1961 cur_func = NULL;
1962 free_data_info_allocs();
1963 free_expression_stack(&switch_expr_stack);
1964 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1965 __bail_on_rest_of_function = 0;
1968 static void save_flow_state(void)
1970 unsigned long *tmp;
1972 __add_ptr_list(&backup, INT_PTR(loop_num << 2));
1973 __add_ptr_list(&backup, INT_PTR(loop_count << 2));
1974 __add_ptr_list(&backup, INT_PTR(final_pass << 2));
1976 __add_ptr_list(&backup, big_statement_stack);
1977 __add_ptr_list(&backup, big_expression_stack);
1978 __add_ptr_list(&backup, big_condition_stack);
1979 __add_ptr_list(&backup, switch_expr_stack);
1981 __add_ptr_list(&backup, cur_func_sym);
1983 __add_ptr_list(&backup, __prev_stmt);
1984 __add_ptr_list(&backup, __cur_stmt);
1985 __add_ptr_list(&backup, __next_stmt);
1987 FOR_EACH_PTR(fn_data_list, tmp) {
1988 __add_ptr_list(&backup, (void *)*tmp);
1989 } END_FOR_EACH_PTR(tmp);
1992 static void *pop_backup(void)
1994 void *ret;
1996 ret = last_ptr_list(backup);
1997 delete_ptr_list_last(&backup);
1998 return ret;
2001 static void restore_flow_state(void)
2003 unsigned long *tmp;
2005 FOR_EACH_PTR_REVERSE(fn_data_list, tmp) {
2006 *tmp = (unsigned long)pop_backup();
2007 } END_FOR_EACH_PTR_REVERSE(tmp);
2009 __next_stmt = pop_backup();
2010 __cur_stmt = pop_backup();
2011 __prev_stmt = pop_backup();
2013 cur_func_sym = pop_backup();
2014 switch_expr_stack = pop_backup();
2015 big_condition_stack = pop_backup();
2016 big_expression_stack = pop_backup();
2017 big_statement_stack = pop_backup();
2018 final_pass = PTR_INT(pop_backup()) >> 2;
2019 loop_count = PTR_INT(pop_backup()) >> 2;
2020 loop_num = PTR_INT(pop_backup()) >> 2;
2023 static void parse_inline(struct expression *call)
2025 struct symbol *base_type;
2026 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
2027 struct timeval time_backup = fn_start_time;
2028 struct expression *orig_inline = __inline_fn;
2029 int orig_budget;
2031 if (out_of_memory() || taking_too_long())
2032 return;
2034 save_flow_state();
2036 __pass_to_client(call, INLINE_FN_START);
2037 final_pass = 0; /* don't print anything */
2038 __inline_fn = call;
2039 orig_budget = inline_budget;
2040 inline_budget = inline_budget - 5;
2042 base_type = get_base_type(call->fn->symbol);
2043 cur_func_sym = call->fn->symbol;
2044 if (call->fn->symbol->ident)
2045 cur_func = call->fn->symbol->ident->name;
2046 else
2047 cur_func = NULL;
2048 set_position(call->fn->symbol->pos);
2050 save_all_states();
2051 big_statement_stack = NULL;
2052 big_expression_stack = NULL;
2053 big_condition_stack = NULL;
2054 switch_expr_stack = NULL;
2056 sm_debug("inline function: %s\n", cur_func);
2057 __unnullify_path();
2058 clear_function_data();
2059 loop_num = 0;
2060 loop_count = 0;
2061 start_function_definition(call->fn->symbol);
2062 __split_stmt(base_type->stmt);
2063 __split_stmt(base_type->inline_stmt);
2064 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
2065 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
2066 call->fn->symbol->parsed = true;
2068 free_expression_stack(&switch_expr_stack);
2069 __free_ptr_list((struct ptr_list **)&big_statement_stack);
2070 nullify_path();
2071 free_goto_stack();
2073 restore_flow_state();
2074 fn_start_time = time_backup;
2075 cur_func = cur_func_bak;
2077 restore_all_states();
2078 set_position(call->pos);
2079 __inline_fn = orig_inline;
2080 inline_budget = orig_budget;
2081 __pass_to_client(call, INLINE_FN_END);
2084 static struct symbol_list *inlines_called;
2085 static void add_inline_function(struct symbol *sym)
2087 static struct symbol_list *already_added;
2088 struct symbol *tmp;
2090 FOR_EACH_PTR(already_added, tmp) {
2091 if (tmp == sym)
2092 return;
2093 } END_FOR_EACH_PTR(tmp);
2095 add_ptr_list(&already_added, sym);
2096 add_ptr_list(&inlines_called, sym);
2099 static void process_inlines(void)
2101 struct symbol *tmp;
2103 FOR_EACH_PTR(inlines_called, tmp) {
2104 split_function(tmp);
2105 } END_FOR_EACH_PTR(tmp);
2106 free_ptr_list(&inlines_called);
2109 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
2111 struct symbol *sym;
2113 FOR_EACH_PTR_REVERSE(big_list, sym) {
2114 if (!sym->scope)
2115 continue;
2116 if (use_static && sym->ctype.modifiers & MOD_STATIC)
2117 return sym;
2118 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
2119 return sym;
2120 } END_FOR_EACH_PTR_REVERSE(sym);
2122 return NULL;
2125 static bool interesting_function(struct symbol *sym)
2127 static int prev_stream = -1;
2128 static bool prev_answer;
2129 const char *filename;
2130 int len;
2132 if (!(sym->ctype.modifiers & MOD_INLINE))
2133 return true;
2135 if (sym->pos.stream == prev_stream)
2136 return prev_answer;
2138 prev_stream = sym->pos.stream;
2139 prev_answer = false;
2141 filename = stream_name(sym->pos.stream);
2142 len = strlen(filename);
2143 if (len > 0 && filename[len - 1] == 'c')
2144 prev_answer = true;
2145 return prev_answer;
2148 static void split_inlines_in_scope(struct symbol *sym)
2150 struct symbol *base;
2151 struct symbol_list *scope_list;
2152 int stream;
2154 scope_list = sym->scope->symbols;
2155 stream = sym->pos.stream;
2157 /* find the last static symbol in the file */
2158 FOR_EACH_PTR_REVERSE(scope_list, sym) {
2159 if (sym->pos.stream != stream)
2160 continue;
2161 if (sym->type != SYM_NODE)
2162 continue;
2163 base = get_base_type(sym);
2164 if (!base)
2165 continue;
2166 if (base->type != SYM_FN)
2167 continue;
2168 if (!base->inline_stmt)
2169 continue;
2170 if (!interesting_function(sym))
2171 continue;
2172 add_inline_function(sym);
2173 } END_FOR_EACH_PTR_REVERSE(sym);
2175 process_inlines();
2178 static void split_inlines(struct symbol_list *sym_list)
2180 struct symbol *sym;
2182 sym = get_last_scoped_symbol(sym_list, 0);
2183 if (sym)
2184 split_inlines_in_scope(sym);
2185 sym = get_last_scoped_symbol(sym_list, 1);
2186 if (sym)
2187 split_inlines_in_scope(sym);
2190 static struct stree *clone_estates_perm(struct stree *orig)
2192 struct stree *ret = NULL;
2193 struct sm_state *tmp;
2195 FOR_EACH_SM(orig, tmp) {
2196 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
2197 } END_FOR_EACH_SM(tmp);
2199 return ret;
2202 struct position last_pos;
2203 static void split_c_file_functions(struct symbol_list *sym_list)
2205 struct symbol *sym;
2207 __unnullify_path();
2208 FOR_EACH_PTR(sym_list, sym) {
2209 set_position(sym->pos);
2210 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
2211 __pass_to_client(sym, BASE_HOOK);
2212 fake_global_assign(sym);
2213 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
2215 } END_FOR_EACH_PTR(sym);
2216 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
2217 nullify_path();
2219 FOR_EACH_PTR(sym_list, sym) {
2220 set_position(sym->pos);
2221 last_pos = sym->pos;
2222 if (!interesting_function(sym))
2223 continue;
2224 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
2225 split_function(sym);
2226 process_inlines();
2228 last_pos = sym->pos;
2229 } END_FOR_EACH_PTR(sym);
2230 split_inlines(sym_list);
2231 __pass_to_client(sym_list, END_FILE_HOOK);
2234 static int final_before_fake;
2235 void init_fake_env(void)
2237 if (!in_fake_env)
2238 final_before_fake = final_pass;
2239 in_fake_env++;
2240 __push_fake_cur_stree();
2241 final_pass = 0;
2244 void end_fake_env(void)
2246 __free_fake_cur_stree();
2247 in_fake_env--;
2248 if (!in_fake_env)
2249 final_pass = final_before_fake;
2252 static void open_output_files(char *base_file)
2254 char buf[256];
2256 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
2257 sm_outfd = fopen(buf, "w");
2258 if (!sm_outfd)
2259 sm_fatal("Cannot open %s", buf);
2261 if (!option_info)
2262 return;
2264 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
2265 sql_outfd = fopen(buf, "w");
2266 if (!sql_outfd)
2267 sm_fatal("Error: Cannot open %s", buf);
2269 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
2270 caller_info_fd = fopen(buf, "w");
2271 if (!caller_info_fd)
2272 sm_fatal("Error: Cannot open %s", buf);
2275 void smatch(struct string_list *filelist)
2277 struct symbol_list *sym_list;
2278 struct timeval stop, start;
2279 char *path;
2280 int len;
2282 gettimeofday(&start, NULL);
2284 FOR_EACH_PTR_NOTAG(filelist, base_file) {
2285 path = getcwd(NULL, 0);
2286 free(full_base_file);
2287 if (path) {
2288 len = strlen(path) + 1 + strlen(base_file) + 1;
2289 full_base_file = malloc(len);
2290 snprintf(full_base_file, len, "%s/%s", path, base_file);
2291 } else {
2292 full_base_file = alloc_string(base_file);
2294 if (option_file_output)
2295 open_output_files(base_file);
2296 base_file_stream = input_stream_nr;
2297 sym_list = sparse_keep_tokens(base_file);
2298 split_c_file_functions(sym_list);
2299 } END_FOR_EACH_PTR_NOTAG(base_file);
2301 gettimeofday(&stop, NULL);
2303 set_position(last_pos);
2304 final_pass = 1;
2305 if (option_time)
2306 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
2307 if (option_mem)
2308 sm_msg("mem: %luKb", get_max_memory());