buf_clear: do a speed up
[smatch.git] / smatch_flow.c
blob79adb94a28aa4baf9ca3167145cc81549fecacf5
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_buf_clear;
31 int __in_fake_var_assign;
32 int __fake_state_cnt;
33 int in_fake_env;
34 int final_pass;
35 int __inline_call;
36 struct expression *__inline_fn;
38 int __smatch_lineno = 0;
40 static char *base_file;
41 static const char *filename;
42 static char *pathname;
43 static char *full_filename;
44 static char *full_base_file;
45 static char *cur_func;
46 int base_file_stream;
47 static unsigned int loop_count;
48 static int last_goto_statement_handled;
49 int __expr_stmt_count;
50 int __in_function_def;
51 int __in_unmatched_hook;
52 static struct expression_list *switch_expr_stack = NULL;
53 static struct expression_list *post_op_stack = NULL;
55 static struct ptr_list *fn_data_list;
56 static struct ptr_list *backup;
58 struct expression_list *big_expression_stack;
59 struct statement_list *big_statement_stack;
60 struct statement *__prev_stmt;
61 struct statement *__cur_stmt;
62 struct statement *__next_stmt;
63 int __in_pre_condition = 0;
64 int __bail_on_rest_of_function = 0;
65 static struct timeval fn_start_time;
66 static struct timeval outer_fn_start_time;
67 char *get_function(void) { return cur_func; }
68 int get_lineno(void) { return __smatch_lineno; }
69 int inside_loop(void) { return !!loop_count; }
70 int definitely_inside_loop(void) { return !!(loop_count & ~0x08000000); }
71 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
72 int in_expression_statement(void) { return !!__expr_stmt_count; }
74 static void split_symlist(struct symbol_list *sym_list);
75 static void split_declaration(struct symbol_list *sym_list);
76 static void split_expr_list(struct expression_list *expr_list, struct expression *parent);
77 static void split_args(struct expression *expr);
78 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *call, struct expression *expr, int nr);
79 static void add_inline_function(struct symbol *sym);
80 static void parse_inline(struct expression *expr);
82 int option_assume_loops = 0;
83 int option_two_passes = 0;
84 struct symbol *cur_func_sym = NULL;
85 struct stree *global_states;
87 const unsigned long valid_ptr_min = 4096;
88 unsigned long valid_ptr_max = ULONG_MAX & ~(MTAG_OFFSET_MASK);
89 const sval_t valid_ptr_min_sval = {
90 .type = &ptr_ctype,
91 {.value = 4096},
93 sval_t valid_ptr_max_sval = {
94 .type = &ptr_ctype,
95 {.value = ULONG_MAX & ~(MTAG_OFFSET_MASK)},
97 struct range_list *valid_ptr_rl;
99 void alloc_valid_ptr_rl(void)
101 valid_ptr_max = sval_type_max(&ulong_ctype).value & ~(MTAG_OFFSET_MASK);
102 valid_ptr_max_sval.value = valid_ptr_max;
104 valid_ptr_rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
105 valid_ptr_rl = cast_rl(&ptr_ctype, valid_ptr_rl);
106 valid_ptr_rl = clone_rl_permanent(valid_ptr_rl);
109 int outside_of_function(void)
111 return cur_func_sym == NULL;
114 const char *get_filename(void)
116 if (option_info && option_full_path)
117 return full_base_file;
118 if (option_info)
119 return base_file;
120 if (option_full_path)
121 return full_filename;
122 return filename;
125 const char *get_base_file(void)
127 if (option_full_path)
128 return full_base_file;
129 return base_file;
132 unsigned long long get_file_id(void)
134 return str_to_llu_hash(get_filename());
137 unsigned long long get_base_file_id(void)
139 return str_to_llu_hash(get_base_file());
142 static void set_position(struct position pos)
144 int len;
145 static int prev_stream = -1;
147 if (in_fake_env)
148 return;
150 if (pos.stream == 0 && pos.line == 0)
151 return;
153 __smatch_lineno = pos.line;
155 if (pos.stream == prev_stream)
156 return;
158 filename = stream_name(pos.stream);
160 free(full_filename);
161 pathname = getcwd(NULL, 0);
162 if (pathname) {
163 len = strlen(pathname) + 1 + strlen(filename) + 1;
164 full_filename = malloc(len);
165 snprintf(full_filename, len, "%s/%s", pathname, filename);
166 } else {
167 full_filename = alloc_string(filename);
169 free(pathname);
172 int is_assigned_call(struct expression *expr)
174 struct expression *parent = expr_get_parent_expr(expr);
176 if (parent &&
177 parent->type == EXPR_ASSIGNMENT &&
178 parent->op == '=' &&
179 strip_expr(parent->right) == expr)
180 return 1;
182 return 0;
185 int is_fake_assigned_call(struct expression *expr)
187 struct expression *parent = expr_get_fake_parent_expr(expr);
189 if (parent &&
190 parent->type == EXPR_ASSIGNMENT &&
191 parent->op == '=' &&
192 strip_expr(parent->right) == expr)
193 return 1;
195 return 0;
198 static bool is_inline_func(struct expression *expr)
200 if (expr->type != EXPR_SYMBOL || !expr->symbol)
201 return false;
202 if (!expr->symbol->definition)
203 return false;
204 if (expr->symbol->definition->ctype.modifiers & MOD_INLINE)
205 return true;
207 return 0;
210 static int is_noreturn_func(struct expression *expr)
212 if (expr->type != EXPR_SYMBOL || !expr->symbol)
213 return 0;
216 * It's almost impossible for Smatch to handle __builtin_constant_p()
217 * the same way that GCC does so Smatch ends up making some functions
218 * as no return functions incorrectly.
221 if (option_project == PROJ_KERNEL && expr->symbol->ident &&
222 strstr(expr->symbol->ident->name, "__compiletime_assert"))
223 return 0;
225 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
226 return 1;
227 return 0;
230 static int save_func_time(void *_rl, int argc, char **argv, char **azColName)
232 unsigned long *rl = _rl;
234 *rl = strtoul(argv[0], NULL, 10);
235 return 0;
238 static int get_func_time(struct symbol *sym)
240 unsigned long time = 0;
242 run_sql(&save_func_time, &time,
243 "select key from return_implies where %s and type = %d;",
244 get_static_filter(sym), FUNC_TIME);
246 return time;
249 static int inline_budget = 20;
251 int inlinable(struct expression *expr)
253 struct symbol *sym;
254 struct statement *last_stmt = NULL;
256 if (__inline_fn) /* don't nest */
257 return 0;
259 if (expr->type != EXPR_SYMBOL || !expr->symbol)
260 return 0;
261 if (is_no_inline_function(expr->symbol->ident->name))
262 return 0;
263 sym = get_base_type(expr->symbol);
264 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
265 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
266 return 0;
267 if (sym->stmt->type != STMT_COMPOUND)
268 return 0;
269 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
271 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
272 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
273 return 0;
274 if (sym->inline_stmt->type != STMT_COMPOUND)
275 return 0;
276 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
279 if (!last_stmt)
280 return 0;
282 /* the magic numbers in this function are pulled out of my bum. */
283 if (last_stmt->pos.line > sym->pos.line + inline_budget)
284 return 0;
286 if (get_func_time(expr->symbol) >= 2)
287 return 0;
289 return 1;
292 void __process_post_op_stack(void)
294 struct expression *expr;
296 FOR_EACH_PTR(post_op_stack, expr) {
297 __pass_to_client(expr, OP_HOOK);
298 } END_FOR_EACH_PTR(expr);
300 __free_ptr_list((struct ptr_list **)&post_op_stack);
303 static int handle_comma_assigns(struct expression *expr)
305 struct expression *right;
306 struct expression *assign;
308 right = strip_expr(expr->right);
309 if (right->type != EXPR_COMMA)
310 return 0;
312 __split_expr(right->left);
313 __process_post_op_stack();
315 assign = assign_expression(expr->left, '=', right->right);
316 __split_expr(assign);
318 return 1;
321 /* This is to handle *p++ = foo; assignments */
322 static int handle_postop_assigns(struct expression *expr)
324 struct expression *left, *fake_left;
325 struct expression *assign;
327 left = strip_expr(expr->left);
328 if (left->type != EXPR_PREOP || left->op != '*')
329 return 0;
330 left = strip_expr(left->unop);
331 if (left->type != EXPR_POSTOP)
332 return 0;
334 fake_left = deref_expression(strip_expr(left->unop));
335 assign = assign_expression(fake_left, '=', expr->right);
337 __split_expr(assign);
338 __split_expr(expr->left);
340 return 1;
343 static int prev_expression_is_getting_address(struct expression *expr)
345 struct expression *parent;
347 do {
348 parent = expr_get_parent_expr(expr);
350 if (!parent)
351 return 0;
352 if (parent->type == EXPR_PREOP && parent->op == '&')
353 return 1;
354 if (parent->type == EXPR_PREOP && parent->op == '(')
355 goto next;
356 if (parent->type == EXPR_DEREF && parent->op == '.')
357 goto next;
358 /* Handle &foo->array[offset] */
359 if (parent->type == EXPR_BINOP && parent->op == '+') {
360 parent = expr_get_parent_expr(parent);
361 if (!parent)
362 return 0;
363 if (parent->type == EXPR_PREOP && parent->op == '*')
364 goto next;
367 return 0;
368 next:
369 expr = parent;
370 } while (1);
373 int __in_builtin_overflow_func;
374 static void handle_builtin_overflow_func(struct expression *expr)
376 struct expression *a, *b, *res, *assign;
377 int op;
379 if (sym_name_is("__builtin_add_overflow", expr->fn))
380 op = '+';
381 else if (sym_name_is("__builtin_sub_overflow", expr->fn))
382 op = '-';
383 else if (sym_name_is("__builtin_mul_overflow", expr->fn))
384 op = '*';
385 else
386 return;
388 a = get_argument_from_call_expr(expr->args, 0);
389 b = get_argument_from_call_expr(expr->args, 1);
390 res = get_argument_from_call_expr(expr->args, 2);
392 assign = assign_expression(deref_expression(res), '=', binop_expression(a, op, b));
394 __in_builtin_overflow_func++;
395 __split_expr(assign);
396 __in_builtin_overflow_func--;
399 static int handle__builtin_choose_expr(struct expression *expr)
401 struct expression *const_expr, *expr1, *expr2;
402 sval_t sval;
404 if (!sym_name_is("__builtin_choose_expr", expr->fn))
405 return 0;
407 const_expr = get_argument_from_call_expr(expr->args, 0);
408 expr1 = get_argument_from_call_expr(expr->args, 1);
409 expr2 = get_argument_from_call_expr(expr->args, 2);
411 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
412 return 0;
413 if (sval.value)
414 __split_expr(expr1);
415 else
416 __split_expr(expr2);
417 return 1;
420 static int handle__builtin_choose_expr_assigns(struct expression *expr)
422 struct expression *const_expr, *right, *expr1, *expr2, *fake;
423 sval_t sval;
425 right = strip_parens(expr->right);
426 if (right->type != EXPR_CALL)
427 return 0;
428 if (!sym_name_is("__builtin_choose_expr", right->fn))
429 return 0;
431 const_expr = get_argument_from_call_expr(right->args, 0);
432 expr1 = get_argument_from_call_expr(right->args, 1);
433 expr2 = get_argument_from_call_expr(right->args, 2);
435 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
436 return 0;
438 fake = assign_expression(expr->left, '=', sval.value ? expr1 : expr2);
439 __split_expr(fake);
440 return 1;
443 int is_condition_call(struct expression *expr)
445 struct expression *tmp;
447 FOR_EACH_PTR_REVERSE(big_condition_stack, tmp) {
448 if (expr == tmp || expr_get_parent_expr(expr) == tmp)
449 return 1;
450 if (tmp->pos.line < expr->pos.line)
451 return 0;
452 } END_FOR_EACH_PTR_REVERSE(tmp);
454 return 0;
457 static struct expression *expr_get_parent_no_parens(struct expression *expr)
459 do {
460 expr = expr_get_parent_expr(expr);
461 } while (expr &&
462 expr->type == EXPR_PREOP &&
463 expr->op == '(');
465 return expr;
468 static bool gen_fake_function_assign(struct expression *expr)
470 static struct expression *parsed;
471 struct expression *assign, *parent;
472 struct symbol *type;
473 char buf[64];
475 /* The rule is that every non-void function call has to be part of an
476 * assignment. TODO: Should we create a fake non-casted assignment
477 * for casted assignments? Also faked assigns for += assignments?
479 type = get_type(expr);
480 if (!type || type == &void_ctype)
481 return false;
483 parent = expr_get_parent_no_parens(expr);
484 if (parent && parent->type == EXPR_ASSIGNMENT)
485 return false;
487 parent = expr_get_fake_parent_expr(expr);
488 if (parent) {
489 struct expression *left = parent->left;
491 if (parent == parsed)
492 return false;
493 if (!left || left->type != EXPR_SYMBOL)
494 return false;
495 if (strncmp(left->symbol_name->name, "__fake_assign_", 14) != 0)
496 return false;
497 parsed = parent;
498 __split_expr(parent);
499 return true;
502 // TODO: faked_assign skipping conditions is a hack
503 if (is_condition_call(expr))
504 return false;
506 snprintf(buf, sizeof(buf), "__fake_assign_%p", expr);
507 assign = create_fake_assign(buf, get_type(expr), expr);
509 parsed = assign;
510 __split_expr(assign);
511 return true;
514 static void split_call(struct expression *expr)
516 if (gen_fake_function_assign(expr))
517 return;
519 expr_set_parent_expr(expr->fn, expr);
521 if (sym_name_is("__builtin_constant_p", expr->fn))
522 return;
523 if (handle__builtin_choose_expr(expr))
524 return;
525 __split_expr(expr->fn);
526 split_args(expr);
527 if (is_inline_func(expr->fn))
528 add_inline_function(expr->fn->symbol->definition);
529 if (inlinable(expr->fn))
530 __inline_call = 1;
531 __process_post_op_stack();
532 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
533 __pass_to_client(expr, FUNCTION_CALL_HOOK);
534 __inline_call = 0;
535 if (inlinable(expr->fn))
536 parse_inline(expr);
537 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
538 if (is_noreturn_func(expr->fn))
539 nullify_path();
540 if (!expr_get_parent_expr(expr))
541 __discard_fake_states(expr);
542 handle_builtin_overflow_func(expr);
545 void parse_assignment(struct expression *expr)
547 struct expression *right;
549 expr_set_parent_expr(expr->left, expr);
550 expr_set_parent_expr(expr->right, expr);
552 right = strip_expr(expr->right);
553 if (!right)
554 return;
556 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
558 /* foo = !bar() */
559 if (__handle_condition_assigns(expr))
560 goto after_assign;
561 /* foo = (x < 5 ? foo : 5); */
562 if (__handle_select_assigns(expr))
563 goto after_assign;
564 /* foo = ({frob(); frob(); frob(); 1;}) */
565 if (__handle_expr_statement_assigns(expr))
566 return; // FIXME: got after
567 /* foo = (3, 4); */
568 if (handle_comma_assigns(expr))
569 goto after_assign;
570 if (handle__builtin_choose_expr_assigns(expr))
571 goto after_assign;
572 if (handle_postop_assigns(expr))
573 return; /* no need to goto after_assign */
575 __split_expr(expr->right);
576 if (outside_of_function())
577 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
578 else
579 __pass_to_client(expr, ASSIGNMENT_HOOK);
582 // FIXME: the ordering of this is tricky
583 __fake_struct_member_assignments(expr);
585 /* Re-examine ->right for inlines. See the commit message */
586 right = strip_expr(expr->right);
587 if (expr->op == '=' && right->type == EXPR_CALL)
588 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
590 after_assign:
591 if (get_macro_name(right->pos) &&
592 get_macro_name(expr->pos) != get_macro_name(right->pos))
593 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
595 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
596 __split_expr(expr->left);
599 void __split_expr(struct expression *expr)
601 if (!expr)
602 return;
604 // if (local_debug)
605 // sm_msg("Debug expr_type %d %s expr = '%s'", expr->type, show_special(expr->op), expr_to_str(expr));
607 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
608 return;
609 if (__in_fake_assign >= 4) /* don't allow too much nesting */
610 return;
612 push_expression(&big_expression_stack, expr);
613 set_position(expr->pos);
614 __pass_to_client(expr, EXPR_HOOK);
616 switch (expr->type) {
617 case EXPR_PREOP:
618 expr_set_parent_expr(expr->unop, expr);
620 if (expr->op == '*' &&
621 !prev_expression_is_getting_address(expr))
622 __pass_to_client(expr, DEREF_HOOK);
623 __split_expr(expr->unop);
624 __pass_to_client(expr, OP_HOOK);
625 break;
626 case EXPR_POSTOP:
627 expr_set_parent_expr(expr->unop, expr);
629 __split_expr(expr->unop);
630 push_expression(&post_op_stack, expr);
631 break;
632 case EXPR_STATEMENT:
633 __expr_stmt_count++;
634 if (expr->statement && !expr->statement) {
635 stmt_set_parent_stmt(expr->statement,
636 last_ptr_list((struct ptr_list *)big_statement_stack));
638 __split_stmt(expr->statement);
639 __expr_stmt_count--;
640 break;
641 case EXPR_LOGICAL:
642 case EXPR_COMPARE:
643 expr_set_parent_expr(expr->left, expr);
644 expr_set_parent_expr(expr->right, expr);
646 __pass_to_client(expr, LOGIC_HOOK);
647 __handle_logic(expr);
648 break;
649 case EXPR_BINOP:
650 expr_set_parent_expr(expr->left, expr);
651 expr_set_parent_expr(expr->right, expr);
653 __pass_to_client(expr, BINOP_HOOK);
654 __split_expr(expr->left);
655 __split_expr(expr->right);
656 break;
657 case EXPR_COMMA:
658 expr_set_parent_expr(expr->left, expr);
659 expr_set_parent_expr(expr->right, expr);
661 __split_expr(expr->left);
662 __process_post_op_stack();
663 __split_expr(expr->right);
664 break;
665 case EXPR_ASSIGNMENT:
666 parse_assignment(expr);
667 break;
668 case EXPR_DEREF:
669 expr_set_parent_expr(expr->deref, expr);
671 __pass_to_client(expr, DEREF_HOOK);
672 __split_expr(expr->deref);
673 break;
674 case EXPR_SLICE:
675 expr_set_parent_expr(expr->base, expr);
677 __split_expr(expr->base);
678 break;
679 case EXPR_CAST:
680 case EXPR_FORCE_CAST:
681 expr_set_parent_expr(expr->cast_expression, expr);
683 __pass_to_client(expr, CAST_HOOK);
684 __split_expr(expr->cast_expression);
685 break;
686 case EXPR_SIZEOF:
687 if (expr->cast_expression)
688 __pass_to_client(strip_parens(expr->cast_expression),
689 SIZEOF_HOOK);
690 break;
691 case EXPR_OFFSETOF:
692 case EXPR_ALIGNOF:
693 break;
694 case EXPR_CONDITIONAL:
695 case EXPR_SELECT:
696 expr_set_parent_expr(expr->conditional, expr);
697 expr_set_parent_expr(expr->cond_true, expr);
698 expr_set_parent_expr(expr->cond_false, expr);
700 if (known_condition_true(expr->conditional)) {
701 __split_expr(expr->cond_true);
702 break;
704 if (known_condition_false(expr->conditional)) {
705 __split_expr(expr->cond_false);
706 break;
708 __pass_to_client(expr, SELECT_HOOK);
709 __split_whole_condition(expr->conditional);
710 __split_expr(expr->cond_true);
711 __push_true_states();
712 __use_false_states();
713 __split_expr(expr->cond_false);
714 __merge_true_states();
715 break;
716 case EXPR_CALL:
717 split_call(expr);
718 break;
719 case EXPR_INITIALIZER:
720 split_expr_list(expr->expr_list, expr);
721 break;
722 case EXPR_IDENTIFIER:
723 expr_set_parent_expr(expr->ident_expression, expr);
724 __split_expr(expr->ident_expression);
725 break;
726 case EXPR_INDEX:
727 expr_set_parent_expr(expr->idx_expression, expr);
728 __split_expr(expr->idx_expression);
729 break;
730 case EXPR_POS:
731 expr_set_parent_expr(expr->init_expr, expr);
732 __split_expr(expr->init_expr);
733 break;
734 case EXPR_SYMBOL:
735 __pass_to_client(expr, SYM_HOOK);
736 break;
737 case EXPR_STRING:
738 __pass_to_client(expr, STRING_HOOK);
739 break;
740 case EXPR_GENERIC: {
741 struct expression *tmp;
743 tmp = strip_Generic(expr);
744 if (tmp != expr)
745 __split_expr(tmp);
746 break;
748 default:
749 break;
751 __pass_to_client(expr, EXPR_HOOK_AFTER);
752 pop_expression(&big_expression_stack);
755 static int is_forever_loop(struct statement *stmt)
757 struct expression *expr;
758 sval_t sval;
760 expr = strip_expr(stmt->iterator_pre_condition);
761 if (!expr)
762 expr = stmt->iterator_post_condition;
763 if (!expr) {
764 /* this is a for(;;) loop... */
765 return 1;
768 if (get_value(expr, &sval) && sval.value != 0)
769 return 1;
771 return 0;
774 static int loop_num;
775 static char *get_loop_name(int num)
777 char buf[256];
779 snprintf(buf, 255, "-loop%d", num);
780 buf[255] = '\0';
781 return alloc_sname(buf);
785 * Pre Loops are while and for loops.
787 static void handle_pre_loop(struct statement *stmt)
789 int once_through; /* we go through the loop at least once */
790 struct sm_state *extra_sm = NULL;
791 int unchanged = 0;
792 char *loop_name;
793 struct stree *stree = NULL;
794 struct sm_state *sm = NULL;
796 loop_name = get_loop_name(loop_num);
797 loop_num++;
799 if (stmt->iterator_pre_statement) {
800 __split_stmt(stmt->iterator_pre_statement);
801 __prev_stmt = stmt->iterator_pre_statement;
804 once_through = implied_condition_true(stmt->iterator_pre_condition);
806 loop_count++;
807 __push_continues();
808 __push_breaks();
810 __merge_gotos(loop_name, NULL);
812 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
813 __in_pre_condition++;
814 __pass_to_client(stmt, PRELOOP_HOOK);
815 __split_whole_condition(stmt->iterator_pre_condition);
816 __in_pre_condition--;
817 FOR_EACH_SM(stree, sm) {
818 set_state(sm->owner, sm->name, sm->sym, sm->state);
819 } END_FOR_EACH_SM(sm);
820 free_stree(&stree);
821 if (extra_sm)
822 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
824 if (option_assume_loops)
825 once_through = 1;
827 __split_stmt(stmt->iterator_statement);
828 if (is_forever_loop(stmt)) {
829 __merge_continues();
830 __save_gotos(loop_name, NULL);
832 __push_fake_cur_stree();
833 __split_stmt(stmt->iterator_post_statement);
834 stree = __pop_fake_cur_stree();
836 __discard_false_states();
837 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
838 __use_breaks();
840 if (!__path_is_null())
841 __merge_stree_into_cur(stree);
842 free_stree(&stree);
843 } else {
844 __merge_continues();
845 unchanged = __iterator_unchanged(extra_sm);
846 __split_stmt(stmt->iterator_post_statement);
847 __prev_stmt = stmt->iterator_post_statement;
848 __cur_stmt = stmt;
850 __save_gotos(loop_name, NULL);
851 __in_pre_condition++;
852 __split_whole_condition(stmt->iterator_pre_condition);
853 __in_pre_condition--;
854 nullify_path();
855 __merge_false_states();
856 if (once_through)
857 __discard_false_states();
858 else
859 __merge_false_states();
861 if (extra_sm && unchanged)
862 __extra_pre_loop_hook_after(extra_sm,
863 stmt->iterator_post_statement,
864 stmt->iterator_pre_condition);
865 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
866 __merge_breaks();
868 loop_count--;
872 * Post loops are do {} while();
874 static void handle_post_loop(struct statement *stmt)
876 char *loop_name;
878 loop_name = get_loop_name(loop_num);
879 loop_num++;
880 loop_count++;
882 __pass_to_client(stmt, POSTLOOP_HOOK);
884 __push_continues();
885 __push_breaks();
886 __merge_gotos(loop_name, NULL);
887 __split_stmt(stmt->iterator_statement);
888 __merge_continues();
889 if (!expr_is_zero(stmt->iterator_post_condition))
890 __save_gotos(loop_name, NULL);
892 if (is_forever_loop(stmt)) {
893 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
894 __use_breaks();
895 } else {
896 __split_whole_condition(stmt->iterator_post_condition);
897 __use_false_states();
898 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
899 __merge_breaks();
901 loop_count--;
904 static int empty_statement(struct statement *stmt)
906 if (!stmt)
907 return 0;
908 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
909 return 1;
910 return 0;
913 static int last_stmt_on_same_line(void)
915 struct statement *stmt;
916 int i = 0;
918 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
919 if (!i++)
920 continue;
921 if (stmt->pos.line == get_lineno())
922 return 1;
923 return 0;
924 } END_FOR_EACH_PTR_REVERSE(stmt);
925 return 0;
928 static void split_asm_ops(struct asm_operand_list *ops)
930 struct asm_operand *op;
932 FOR_EACH_PTR(ops, op) {
933 __split_expr(op->expr);
934 } END_FOR_EACH_PTR(op);
937 static int is_case_val(struct statement *stmt, sval_t sval)
939 sval_t case_sval;
941 if (stmt->type != STMT_CASE)
942 return 0;
943 if (!stmt->case_expression) {
944 __set_default();
945 return 1;
947 if (!get_value(stmt->case_expression, &case_sval))
948 return 0;
949 if (case_sval.value == sval.value)
950 return 1;
951 return 0;
954 static struct range_list *get_case_rl(struct expression *switch_expr,
955 struct expression *case_expr,
956 struct expression *case_to)
958 sval_t start, end;
959 struct range_list *rl = NULL;
960 struct symbol *switch_type;
962 switch_type = get_type(switch_expr);
963 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
964 start = sval_cast(switch_type, start);
965 end = sval_cast(switch_type, end);
966 add_range(&rl, start, end);
967 } else if (get_value(case_expr, &start)) {
968 start = sval_cast(switch_type, start);
969 add_range(&rl, start, start);
972 return rl;
975 static void split_known_switch(struct statement *stmt, sval_t sval)
977 struct statement *tmp;
978 struct range_list *rl;
980 __split_expr(stmt->switch_expression);
981 sval = sval_cast(get_type(stmt->switch_expression), sval);
983 push_expression(&switch_expr_stack, stmt->switch_expression);
984 __save_switch_states(top_expression(switch_expr_stack));
985 nullify_path();
986 __push_default();
987 __push_breaks();
989 stmt = stmt->switch_statement;
991 __push_scope_hooks();
992 FOR_EACH_PTR(stmt->stmts, tmp) {
993 __smatch_lineno = tmp->pos.line;
994 // FIXME: what if default comes before the known case statement?
995 if (is_case_val(tmp, sval)) {
996 rl = alloc_rl(sval, sval);
997 __merge_switches(top_expression(switch_expr_stack), rl);
998 __pass_case_to_client(top_expression(switch_expr_stack), rl);
999 stmt_set_parent_stmt(tmp->case_statement, tmp);
1000 __split_stmt(tmp->case_statement);
1001 goto next;
1003 if (__path_is_null())
1004 continue;
1005 __split_stmt(tmp);
1006 next:
1007 if (__path_is_null()) {
1008 __set_default();
1009 goto out;
1011 } END_FOR_EACH_PTR(tmp);
1012 out:
1013 __call_scope_hooks();
1014 if (!__pop_default())
1015 __merge_switches(top_expression(switch_expr_stack), NULL);
1016 __discard_switches();
1017 __merge_breaks();
1018 pop_expression(&switch_expr_stack);
1021 static void split_case(struct statement *stmt)
1023 struct range_list *rl = NULL;
1025 expr_set_parent_stmt(stmt->case_expression, stmt);
1026 expr_set_parent_stmt(stmt->case_to, stmt);
1028 rl = get_case_rl(top_expression(switch_expr_stack),
1029 stmt->case_expression, stmt->case_to);
1030 while (stmt->case_statement->type == STMT_CASE) {
1031 struct range_list *tmp;
1033 tmp = get_case_rl(top_expression(switch_expr_stack),
1034 stmt->case_statement->case_expression,
1035 stmt->case_statement->case_to);
1036 if (!tmp)
1037 goto next;
1038 rl = rl_union(rl, tmp);
1039 if (!stmt->case_expression)
1040 __set_default();
1041 next:
1042 stmt = stmt->case_statement;
1045 __merge_switches(top_expression(switch_expr_stack), rl);
1047 if (!stmt->case_expression)
1048 __set_default();
1050 stmt_set_parent_stmt(stmt->case_statement, stmt);
1051 __split_stmt(stmt->case_statement);
1054 int time_parsing_function(void)
1056 return ms_since(&fn_start_time) / 1000;
1059 bool taking_too_long(void)
1061 if ((ms_since(&outer_fn_start_time) / 1000) > 60 * 5) /* five minutes */
1062 return 1;
1063 return 0;
1066 struct statement *get_last_stmt(void)
1068 struct symbol *fn;
1069 struct statement *stmt;
1071 fn = get_base_type(cur_func_sym);
1072 if (!fn)
1073 return NULL;
1074 stmt = fn->stmt;
1075 if (!stmt)
1076 stmt = fn->inline_stmt;
1077 if (!stmt || stmt->type != STMT_COMPOUND)
1078 return NULL;
1079 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1080 if (stmt && stmt->type == STMT_LABEL)
1081 stmt = stmt->label_statement;
1082 return stmt;
1085 int is_last_stmt(struct statement *cur_stmt)
1087 struct statement *last;
1089 last = get_last_stmt();
1090 if (last && last == cur_stmt)
1091 return 1;
1092 return 0;
1095 static void handle_backward_goto(struct statement *goto_stmt)
1097 const char *goto_name, *label_name;
1098 struct statement *func_stmt;
1099 struct symbol *base_type = get_base_type(cur_func_sym);
1100 struct statement *tmp;
1101 int found = 0;
1103 if (!option_info)
1104 return;
1105 if (last_goto_statement_handled)
1106 return;
1107 last_goto_statement_handled = 1;
1109 if (!goto_stmt->goto_label ||
1110 goto_stmt->goto_label->type != SYM_LABEL ||
1111 !goto_stmt->goto_label->ident)
1112 return;
1113 goto_name = goto_stmt->goto_label->ident->name;
1115 func_stmt = base_type->stmt;
1116 if (!func_stmt)
1117 func_stmt = base_type->inline_stmt;
1118 if (!func_stmt)
1119 return;
1120 if (func_stmt->type != STMT_COMPOUND)
1121 return;
1123 FOR_EACH_PTR(func_stmt->stmts, tmp) {
1124 if (!found) {
1125 if (tmp->type != STMT_LABEL)
1126 continue;
1127 if (!tmp->label_identifier ||
1128 tmp->label_identifier->type != SYM_LABEL ||
1129 !tmp->label_identifier->ident)
1130 continue;
1131 label_name = tmp->label_identifier->ident->name;
1132 if (strcmp(goto_name, label_name) != 0)
1133 continue;
1134 found = 1;
1136 __split_stmt(tmp);
1137 } END_FOR_EACH_PTR(tmp);
1140 static void fake_a_return(void)
1142 struct expression *ret = NULL;
1144 nullify_path();
1145 __unnullify_path();
1147 if (cur_func_return_type() != &void_ctype)
1148 ret = unknown_value_expression(NULL);
1150 __pass_to_client(ret, RETURN_HOOK);
1151 nullify_path();
1154 static void split_ret_value(struct expression *expr)
1156 struct symbol *type;
1158 if (!expr)
1159 return;
1161 type = get_real_base_type(cur_func_sym);
1162 type = get_real_base_type(type);
1163 expr = fake_a_variable_assign(type, NULL, expr, -1);
1165 __in_fake_var_assign++;
1166 __split_expr(expr);
1167 __in_fake_var_assign--;
1170 static void fake_an_empty_default(struct position pos)
1172 static struct statement none = {};
1174 none.pos = pos;
1175 none.type = STMT_NONE;
1176 __merge_switches(top_expression(switch_expr_stack), NULL);
1177 __split_stmt(&none);
1180 static void split_compound(struct statement *stmt)
1182 struct statement *prev = NULL;
1183 struct statement *cur = NULL;
1184 struct statement *next;
1186 __push_scope_hooks();
1188 FOR_EACH_PTR(stmt->stmts, next) {
1189 /* just set them all ahead of time */
1190 stmt_set_parent_stmt(next, stmt);
1192 if (cur) {
1193 __prev_stmt = prev;
1194 __next_stmt = next;
1195 __cur_stmt = cur;
1196 __split_stmt(cur);
1198 prev = cur;
1199 cur = next;
1200 } END_FOR_EACH_PTR(next);
1201 if (cur) {
1202 __prev_stmt = prev;
1203 __cur_stmt = cur;
1204 __next_stmt = NULL;
1205 __split_stmt(cur);
1209 * For function scope, then delay calling the scope hooks until the
1210 * end of function hooks can run. I'm not positive this is the right
1211 * thing...
1213 if (!is_last_stmt(cur))
1214 __call_scope_hooks();
1218 * This is a hack, work around for detecting empty functions.
1220 static int need_delayed_scope_hooks(void)
1222 struct symbol *fn = get_base_type(cur_func_sym);
1223 struct statement *stmt;
1225 if (!fn)
1226 return 0;
1227 stmt = fn->stmt;
1228 if (!stmt)
1229 stmt = fn->inline_stmt;
1230 if (stmt && stmt->type == STMT_COMPOUND)
1231 return 1;
1232 return 0;
1235 void __split_label_stmt(struct statement *stmt)
1237 if (stmt->label_identifier &&
1238 stmt->label_identifier->type == SYM_LABEL &&
1239 stmt->label_identifier->ident) {
1240 loop_count |= 0x0800000;
1241 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
1245 static void find_asm_gotos(struct statement *stmt)
1247 struct symbol *sym;
1249 FOR_EACH_PTR(stmt->asm_labels, sym) {
1250 __save_gotos(sym->ident->name, sym);
1251 } END_FOR_EACH_PTR(sym);
1254 void __split_stmt(struct statement *stmt)
1256 static int indent_cnt;
1257 sval_t sval;
1258 struct timeval start, stop;
1259 bool skip_after = false;
1261 gettimeofday(&start, NULL);
1263 if (!stmt)
1264 goto out;
1266 if (!__in_fake_assign)
1267 __silence_warnings_for_stmt = false;
1269 if (__bail_on_rest_of_function || is_skipped_function())
1270 return;
1272 if (out_of_memory() || taking_too_long()) {
1273 gettimeofday(&start, NULL);
1275 __bail_on_rest_of_function = 1;
1276 final_pass = 1;
1277 sm_perror("Function too hairy. Giving up. %lu seconds",
1278 start.tv_sec - fn_start_time.tv_sec);
1279 fake_a_return();
1280 final_pass = 0; /* turn off sm_msg() from here */
1281 return;
1284 indent_cnt++;
1286 add_ptr_list(&big_statement_stack, stmt);
1287 free_expression_stack(&big_expression_stack);
1288 set_position(stmt->pos);
1289 __pass_to_client(stmt, STMT_HOOK);
1291 switch (stmt->type) {
1292 case STMT_DECLARATION:
1293 split_declaration(stmt->declaration);
1294 break;
1295 case STMT_RETURN:
1296 expr_set_parent_stmt(stmt->ret_value, stmt);
1298 split_ret_value(stmt->ret_value);
1299 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1300 __process_post_op_stack();
1301 nullify_path();
1302 break;
1303 case STMT_EXPRESSION:
1304 expr_set_parent_stmt(stmt->expression, stmt);
1305 expr_set_parent_stmt(stmt->context, stmt);
1307 __split_expr(stmt->expression);
1308 break;
1309 case STMT_COMPOUND:
1310 split_compound(stmt);
1311 break;
1312 case STMT_IF:
1313 stmt_set_parent_stmt(stmt->if_true, stmt);
1314 stmt_set_parent_stmt(stmt->if_false, stmt);
1315 expr_set_parent_stmt(stmt->if_conditional, stmt);
1317 if (known_condition_true(stmt->if_conditional)) {
1318 __split_stmt(stmt->if_true);
1319 break;
1321 if (known_condition_false(stmt->if_conditional)) {
1322 __split_stmt(stmt->if_false);
1323 break;
1325 __split_whole_condition(stmt->if_conditional);
1326 __split_stmt(stmt->if_true);
1327 if (empty_statement(stmt->if_true) &&
1328 last_stmt_on_same_line() &&
1329 !get_macro_name(stmt->if_true->pos))
1330 sm_warning("if();");
1331 __push_true_states();
1332 __use_false_states();
1333 __split_stmt(stmt->if_false);
1334 __merge_true_states();
1335 break;
1336 case STMT_ITERATOR:
1337 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1338 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1339 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1340 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1341 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1343 if (stmt->iterator_pre_condition)
1344 handle_pre_loop(stmt);
1345 else if (stmt->iterator_post_condition)
1346 handle_post_loop(stmt);
1347 else {
1348 // these are for(;;) type loops.
1349 handle_pre_loop(stmt);
1351 break;
1352 case STMT_SWITCH:
1353 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1354 expr_set_parent_stmt(stmt->switch_expression, stmt);
1356 if (get_value(stmt->switch_expression, &sval)) {
1357 split_known_switch(stmt, sval);
1358 break;
1360 __split_expr(stmt->switch_expression);
1361 push_expression(&switch_expr_stack, stmt->switch_expression);
1362 __save_switch_states(top_expression(switch_expr_stack));
1363 nullify_path();
1364 __push_default();
1365 __push_breaks();
1366 __split_stmt(stmt->switch_statement);
1367 if (!__pop_default() && have_remaining_cases())
1368 fake_an_empty_default(stmt->pos);
1369 __discard_switches();
1370 __merge_breaks();
1371 pop_expression(&switch_expr_stack);
1372 break;
1373 case STMT_CASE:
1374 split_case(stmt);
1375 break;
1376 case STMT_LABEL:
1377 __split_label_stmt(stmt);
1378 __pass_to_client(stmt, STMT_HOOK_AFTER);
1379 skip_after = true;
1380 __split_stmt(stmt->label_statement);
1381 break;
1382 case STMT_GOTO:
1383 expr_set_parent_stmt(stmt->goto_expression, stmt);
1385 __split_expr(stmt->goto_expression);
1386 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1387 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1388 __process_breaks();
1389 } else if (!strcmp(stmt->goto_label->ident->name,
1390 "continue")) {
1391 __process_continues();
1393 } else if (stmt->goto_label &&
1394 stmt->goto_label->type == SYM_LABEL &&
1395 stmt->goto_label->ident) {
1396 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1398 nullify_path();
1399 if (is_last_stmt(stmt))
1400 handle_backward_goto(stmt);
1401 break;
1402 case STMT_NONE:
1403 break;
1404 case STMT_ASM:
1405 expr_set_parent_stmt(stmt->asm_string, stmt);
1407 find_asm_gotos(stmt);
1408 __pass_to_client(stmt, ASM_HOOK);
1409 __split_expr(stmt->asm_string);
1410 split_asm_ops(stmt->asm_outputs);
1411 split_asm_ops(stmt->asm_inputs);
1412 split_expr_list(stmt->asm_clobbers, NULL);
1413 break;
1414 case STMT_CONTEXT:
1415 break;
1416 case STMT_RANGE:
1417 __split_expr(stmt->range_expression);
1418 __split_expr(stmt->range_low);
1419 __split_expr(stmt->range_high);
1420 break;
1422 if (!skip_after)
1423 __pass_to_client(stmt, STMT_HOOK_AFTER);
1424 if (--indent_cnt == 1)
1425 __discard_fake_states(NULL);
1427 out:
1428 __process_post_op_stack();
1430 gettimeofday(&stop, NULL);
1431 if (option_time_stmt && stmt)
1432 sm_msg("stmt_time%s: %ld",
1433 stmt->type == STMT_COMPOUND ? "_block" : "",
1434 stop.tv_sec - start.tv_sec);
1437 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1439 struct expression *expr;
1441 FOR_EACH_PTR(expr_list, expr) {
1442 expr_set_parent_expr(expr, parent);
1443 __split_expr(expr);
1444 __process_post_op_stack();
1445 } END_FOR_EACH_PTR(expr);
1448 static bool cast_arg(struct symbol *type, struct expression *arg)
1450 struct symbol *orig;
1452 if (!type)
1453 return false;
1455 arg = strip_parens(arg);
1456 if (arg != strip_expr(arg))
1457 return true;
1459 orig = get_type(arg);
1460 if (!orig)
1461 return true;
1462 if (types_equiv(orig, type))
1463 return false;
1465 if (orig->type == SYM_ARRAY && type->type == SYM_PTR)
1466 return true;
1469 * I would have expected that we could just do use (orig == type) but I
1470 * guess for pointers we need to get the basetype to do that comparison.
1474 if (orig->type != SYM_PTR ||
1475 type->type != SYM_PTR) {
1476 if (type_fits(type, orig))
1477 return false;
1478 return true;
1480 orig = get_real_base_type(orig);
1481 type = get_real_base_type(type);
1482 if (orig == type)
1483 return false;
1485 return true;
1488 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *call, struct expression *expr, int nr)
1490 char buf[64];
1491 bool cast;
1493 if (!expr || !cur_func_sym)
1494 return NULL;
1496 if (expr->type == EXPR_ASSIGNMENT)
1497 return expr;
1499 /* for va_args then we don't know the type */
1500 if (!type)
1501 type = get_type(expr);
1503 cast = cast_arg(type, expr);
1505 * Using expr_to_sym() here is a hack. We want to say that we don't
1506 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1507 * It turns out faking these assignments is way more expensive than I
1508 * would have imagined. I'm not sure why exactly.
1511 if (!cast) {
1513 * if the code is "return *p;" where "p" is a user pointer then
1514 * we want to create a fake assignment so that it sets the state
1515 * in check_kernel_user_data.c.
1518 if (expr->type != EXPR_PREOP &&
1519 expr->op != '*' && expr->op != '&' &&
1520 expr_to_sym(expr))
1521 return expr;
1524 if (nr == -1)
1525 snprintf(buf, sizeof(buf), "__fake_return_%p", expr);
1526 else
1527 snprintf(buf, sizeof(buf), "__fake_param_%p_%d", call, nr);
1529 return create_fake_assign(buf, type, expr);
1532 static void split_args(struct expression *expr)
1534 struct expression *arg, *tmp;
1535 struct symbol *type;
1536 int i;
1538 i = -1;
1539 FOR_EACH_PTR(expr->args, arg) {
1540 i++;
1541 expr_set_parent_expr(arg, expr);
1542 type = get_arg_type(expr->fn, i);
1543 tmp = fake_a_variable_assign(type, expr, arg, i);
1544 if (tmp != arg)
1545 __in_fake_var_assign++;
1546 __split_expr(tmp);
1547 if (tmp != arg)
1548 __in_fake_var_assign--;
1549 __process_post_op_stack();
1550 } END_FOR_EACH_PTR(arg);
1553 static void split_sym(struct symbol *sym)
1555 if (!sym)
1556 return;
1557 if (!(sym->namespace & NS_SYMBOL))
1558 return;
1560 __split_stmt(sym->stmt);
1561 __split_expr(sym->array_size);
1562 split_symlist(sym->arguments);
1563 split_symlist(sym->symbol_list);
1564 __split_stmt(sym->inline_stmt);
1565 split_symlist(sym->inline_symbol_list);
1568 static void split_symlist(struct symbol_list *sym_list)
1570 struct symbol *sym;
1572 FOR_EACH_PTR(sym_list, sym) {
1573 split_sym(sym);
1574 } END_FOR_EACH_PTR(sym);
1577 typedef void (fake_cb)(struct expression *expr);
1579 static int member_to_number(struct expression *expr, struct ident *member)
1581 struct symbol *type, *tmp;
1582 char *name;
1583 int i;
1585 if (!member)
1586 return -1;
1587 name = member->name;
1589 type = get_type(expr);
1590 if (!type || type->type != SYM_STRUCT)
1591 return -1;
1593 i = -1;
1594 FOR_EACH_PTR(type->symbol_list, tmp) {
1595 i++;
1596 if (!tmp->ident)
1597 continue;
1598 if (strcmp(name, tmp->ident->name) == 0)
1599 return i;
1600 } END_FOR_EACH_PTR(tmp);
1601 return -1;
1604 static struct ident *number_to_member(struct expression *expr, int num)
1606 struct symbol *type, *member;
1607 int i = 0;
1609 type = get_type(expr);
1610 if (!type || type->type != SYM_STRUCT)
1611 return NULL;
1613 FOR_EACH_PTR(type->symbol_list, member) {
1614 if (i == num)
1615 return member->ident;
1616 i++;
1617 } END_FOR_EACH_PTR(member);
1618 return NULL;
1621 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1623 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1625 struct expression *edge_member, *assign;
1626 struct symbol *base = get_real_base_type(member);
1627 struct symbol *tmp;
1629 if (member->ident)
1630 expr = member_expression(expr, '.', member->ident);
1632 FOR_EACH_PTR(base->symbol_list, tmp) {
1633 struct symbol *type;
1635 type = get_real_base_type(tmp);
1636 if (!type)
1637 continue;
1639 edge_member = member_expression(expr, '.', tmp->ident);
1640 if (get_extra_state(edge_member))
1641 continue;
1643 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1644 set_inner_struct_members(expr, tmp);
1645 continue;
1648 if (!tmp->ident)
1649 continue;
1651 assign = assign_expression(edge_member, '=', zero_expr());
1652 __split_expr(assign);
1653 } END_FOR_EACH_PTR(tmp);
1658 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1660 struct symbol *tmp;
1661 struct expression *member = NULL;
1662 struct expression *assign;
1664 FOR_EACH_PTR(type->symbol_list, tmp) {
1665 type = get_real_base_type(tmp);
1666 if (!type)
1667 continue;
1669 if (tmp->ident) {
1670 member = member_expression(expr, '.', tmp->ident);
1671 if (get_extra_state(member))
1672 continue;
1675 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1676 set_inner_struct_members(expr, tmp);
1677 continue;
1679 if (type->type == SYM_ARRAY)
1680 continue;
1681 if (!tmp->ident)
1682 continue;
1684 assign = assign_expression(member, '=', zero_expr());
1685 __split_expr(assign);
1686 } END_FOR_EACH_PTR(tmp);
1689 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1691 struct expression *deref, *assign, *tmp, *right;
1692 struct symbol *struct_type, *type;
1693 struct ident *member;
1694 int member_idx;
1696 struct_type = get_type(symbol);
1697 if (!struct_type ||
1698 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1699 return;
1702 * We're parsing an initializer that could look something like this:
1703 * struct foo foo = {
1704 * 42,
1705 * .whatever.xxx = 11,
1706 * .zzz = 12,
1707 * };
1709 * So what we have here is a list with 42, .whatever, and .zzz. We need
1710 * to break it up into left and right sides of the assignments.
1713 member_idx = 0;
1714 FOR_EACH_PTR(members, tmp) {
1715 deref = NULL;
1716 if (tmp->type == EXPR_IDENTIFIER) {
1717 member_idx = member_to_number(symbol, tmp->expr_ident);
1718 while (tmp->type == EXPR_IDENTIFIER) {
1719 member = tmp->expr_ident;
1720 tmp = tmp->ident_expression;
1721 if (deref)
1722 deref = member_expression(deref, '.', member);
1723 else
1724 deref = member_expression(symbol, '.', member);
1726 } else {
1727 member = number_to_member(symbol, member_idx);
1728 deref = member_expression(symbol, '.', member);
1730 right = tmp;
1731 member_idx++;
1732 if (right->type == EXPR_INITIALIZER) {
1733 type = get_type(deref);
1734 if (type && type->type == SYM_ARRAY)
1735 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1736 else
1737 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1738 } else {
1739 assign = assign_expression(deref, '=', right);
1740 fake_cb(assign);
1742 } END_FOR_EACH_PTR(tmp);
1744 set_unset_to_zero(struct_type, symbol);
1747 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1749 fake_member_assigns_helper(symbol_expression(sym),
1750 sym->initializer->expr_list, fake_cb);
1753 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1755 struct expression *offset, *binop, *assign, *tmp;
1756 struct symbol *type;
1757 int idx, max;
1759 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1760 return;
1762 max = 0;
1763 idx = 0;
1764 FOR_EACH_PTR(expr_list, tmp) {
1765 if (tmp->type == EXPR_INDEX) {
1766 if (tmp->idx_from != tmp->idx_to)
1767 return;
1768 idx = tmp->idx_from;
1769 if (idx > max)
1770 max = idx;
1771 if (!tmp->idx_expression)
1772 goto next;
1773 tmp = tmp->idx_expression;
1775 offset = value_expr(idx);
1776 binop = array_element_expression(array, offset);
1777 if (tmp->type == EXPR_INITIALIZER) {
1778 type = get_type(binop);
1779 if (type && type->type == SYM_ARRAY)
1780 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1781 else
1782 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1783 } else {
1784 assign = assign_expression(binop, '=', tmp);
1785 fake_cb(assign);
1787 next:
1788 idx++;
1789 if (idx > max)
1790 max = idx;
1791 } END_FOR_EACH_PTR(tmp);
1793 __call_array_initialized_hooks(array, max);
1796 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1798 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1801 static void fake_assign_expr(struct symbol *sym)
1803 struct expression *assign, *symbol;
1805 symbol = symbol_expression(sym);
1806 assign = assign_expression(symbol, '=', sym->initializer);
1807 __split_expr(assign);
1810 static void do_initializer_stuff(struct symbol *sym)
1812 if (!sym->initializer)
1813 return;
1815 if (sym->initializer->type == EXPR_INITIALIZER) {
1816 if (get_real_base_type(sym)->type == SYM_ARRAY)
1817 fake_element_assigns(sym, __split_expr);
1818 else
1819 fake_member_assigns(sym, __split_expr);
1820 } else {
1821 fake_assign_expr(sym);
1825 static void split_declaration(struct symbol_list *sym_list)
1827 struct symbol *sym;
1829 FOR_EACH_PTR(sym_list, sym) {
1830 __pass_to_client(sym, DECLARATION_HOOK);
1831 do_initializer_stuff(sym);
1832 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
1833 split_sym(sym);
1834 } END_FOR_EACH_PTR(sym);
1837 static void call_global_assign_hooks(struct expression *assign)
1839 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1842 static void fake_global_assign(struct symbol *sym)
1844 struct expression *assign, *symbol;
1846 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1847 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1848 fake_element_assigns(sym, call_global_assign_hooks);
1849 } else if (sym->initializer) {
1850 symbol = symbol_expression(sym);
1851 assign = assign_expression(symbol, '=', sym->initializer);
1852 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1853 } else {
1854 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1856 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1857 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1858 fake_member_assigns(sym, call_global_assign_hooks);
1859 } else if (sym->initializer) {
1860 symbol = symbol_expression(sym);
1861 assign = assign_expression(symbol, '=', sym->initializer);
1862 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1863 } else {
1864 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1866 } else {
1867 symbol = symbol_expression(sym);
1868 if (sym->initializer) {
1869 assign = assign_expression(symbol, '=', sym->initializer);
1870 __split_expr(assign);
1871 } else {
1872 assign = assign_expression(symbol, '=', zero_expr());
1874 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1878 static void start_function_definition(struct symbol *sym)
1880 __in_function_def = 1;
1881 __pass_to_client(sym, FUNC_DEF_HOOK);
1882 __in_function_def = 0;
1883 __pass_to_client(sym, AFTER_DEF_HOOK);
1887 void add_function_data(unsigned long *fn_data)
1889 __add_ptr_list(&fn_data_list, fn_data);
1892 static void clear_function_data(void)
1894 unsigned long *tmp;
1896 FOR_EACH_PTR(fn_data_list, tmp) {
1897 *tmp = 0;
1898 } END_FOR_EACH_PTR(tmp);
1901 static void record_func_time(void)
1903 struct timeval stop;
1904 int func_time;
1905 char buf[32];
1907 gettimeofday(&stop, NULL);
1908 func_time = stop.tv_sec - fn_start_time.tv_sec;
1909 snprintf(buf, sizeof(buf), "%d", func_time);
1910 sql_insert_return_implies(FUNC_TIME, 0, "", buf);
1911 if (option_time && func_time > 2) {
1912 final_pass++;
1913 sm_msg("func_time: %d", func_time);
1914 final_pass--;
1918 static void split_function(struct symbol *sym)
1920 struct symbol *base_type = get_base_type(sym);
1922 if (!base_type->stmt && !base_type->inline_stmt)
1923 return;
1925 gettimeofday(&outer_fn_start_time, NULL);
1926 gettimeofday(&fn_start_time, NULL);
1927 cur_func_sym = sym;
1928 if (sym->ident)
1929 cur_func = sym->ident->name;
1930 set_position(sym->pos);
1931 clear_function_data();
1932 loop_count = 0;
1933 last_goto_statement_handled = 0;
1934 sm_debug("new function: %s\n", cur_func);
1935 __stree_id = 0;
1936 if (option_two_passes) {
1937 __unnullify_path();
1938 loop_num = 0;
1939 final_pass = 0;
1940 start_function_definition(sym);
1941 __split_stmt(base_type->stmt);
1942 __split_stmt(base_type->inline_stmt);
1943 nullify_path();
1945 __unnullify_path();
1946 loop_num = 0;
1947 final_pass = 1;
1948 start_function_definition(sym);
1949 __split_stmt(base_type->stmt);
1950 __split_stmt(base_type->inline_stmt);
1951 if (!__path_is_null() &&
1952 cur_func_return_type() == &void_ctype &&
1953 !__bail_on_rest_of_function) {
1954 __pass_to_client(NULL, RETURN_HOOK);
1955 nullify_path();
1957 __pass_to_client(sym, END_FUNC_HOOK);
1958 if (need_delayed_scope_hooks())
1959 __call_scope_hooks();
1960 __pass_to_client(sym, AFTER_FUNC_HOOK);
1961 sym->parsed = true;
1963 clear_all_states();
1965 record_func_time();
1967 cur_func_sym = NULL;
1968 cur_func = NULL;
1969 free_data_info_allocs();
1970 free_expression_stack(&switch_expr_stack);
1971 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1972 __bail_on_rest_of_function = 0;
1975 static void save_flow_state(void)
1977 unsigned long *tmp;
1979 __add_ptr_list(&backup, INT_PTR(loop_num << 2));
1980 __add_ptr_list(&backup, INT_PTR(loop_count << 2));
1981 __add_ptr_list(&backup, INT_PTR(final_pass << 2));
1983 __add_ptr_list(&backup, big_statement_stack);
1984 __add_ptr_list(&backup, big_expression_stack);
1985 __add_ptr_list(&backup, big_condition_stack);
1986 __add_ptr_list(&backup, switch_expr_stack);
1988 __add_ptr_list(&backup, cur_func_sym);
1990 __add_ptr_list(&backup, __prev_stmt);
1991 __add_ptr_list(&backup, __cur_stmt);
1992 __add_ptr_list(&backup, __next_stmt);
1994 FOR_EACH_PTR(fn_data_list, tmp) {
1995 __add_ptr_list(&backup, (void *)*tmp);
1996 } END_FOR_EACH_PTR(tmp);
1999 static void *pop_backup(void)
2001 void *ret;
2003 ret = last_ptr_list(backup);
2004 delete_ptr_list_last(&backup);
2005 return ret;
2008 static void restore_flow_state(void)
2010 unsigned long *tmp;
2012 FOR_EACH_PTR_REVERSE(fn_data_list, tmp) {
2013 *tmp = (unsigned long)pop_backup();
2014 } END_FOR_EACH_PTR_REVERSE(tmp);
2016 __next_stmt = pop_backup();
2017 __cur_stmt = pop_backup();
2018 __prev_stmt = pop_backup();
2020 cur_func_sym = pop_backup();
2021 switch_expr_stack = pop_backup();
2022 big_condition_stack = pop_backup();
2023 big_expression_stack = pop_backup();
2024 big_statement_stack = pop_backup();
2025 final_pass = PTR_INT(pop_backup()) >> 2;
2026 loop_count = PTR_INT(pop_backup()) >> 2;
2027 loop_num = PTR_INT(pop_backup()) >> 2;
2030 static void parse_inline(struct expression *call)
2032 struct symbol *base_type;
2033 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
2034 struct timeval time_backup = fn_start_time;
2035 struct expression *orig_inline = __inline_fn;
2036 int orig_budget;
2038 if (out_of_memory() || taking_too_long())
2039 return;
2041 save_flow_state();
2043 __pass_to_client(call, INLINE_FN_START);
2044 final_pass = 0; /* don't print anything */
2045 __inline_fn = call;
2046 orig_budget = inline_budget;
2047 inline_budget = inline_budget - 5;
2049 base_type = get_base_type(call->fn->symbol);
2050 cur_func_sym = call->fn->symbol;
2051 if (call->fn->symbol->ident)
2052 cur_func = call->fn->symbol->ident->name;
2053 else
2054 cur_func = NULL;
2055 set_position(call->fn->symbol->pos);
2057 save_all_states();
2058 big_statement_stack = NULL;
2059 big_expression_stack = NULL;
2060 big_condition_stack = NULL;
2061 switch_expr_stack = NULL;
2063 sm_debug("inline function: %s\n", cur_func);
2064 __unnullify_path();
2065 clear_function_data();
2066 loop_num = 0;
2067 loop_count = 0;
2068 start_function_definition(call->fn->symbol);
2069 __split_stmt(base_type->stmt);
2070 __split_stmt(base_type->inline_stmt);
2071 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
2072 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
2073 call->fn->symbol->parsed = true;
2075 free_expression_stack(&switch_expr_stack);
2076 __free_ptr_list((struct ptr_list **)&big_statement_stack);
2077 nullify_path();
2078 free_goto_stack();
2080 restore_flow_state();
2081 fn_start_time = time_backup;
2082 cur_func = cur_func_bak;
2084 restore_all_states();
2085 set_position(call->pos);
2086 __inline_fn = orig_inline;
2087 inline_budget = orig_budget;
2088 __pass_to_client(call, INLINE_FN_END);
2091 static struct symbol_list *inlines_called;
2092 static void add_inline_function(struct symbol *sym)
2094 static struct symbol_list *already_added;
2095 struct symbol *tmp;
2097 FOR_EACH_PTR(already_added, tmp) {
2098 if (tmp == sym)
2099 return;
2100 } END_FOR_EACH_PTR(tmp);
2102 add_ptr_list(&already_added, sym);
2103 add_ptr_list(&inlines_called, sym);
2106 static void process_inlines(void)
2108 struct symbol *tmp;
2110 FOR_EACH_PTR(inlines_called, tmp) {
2111 split_function(tmp);
2112 } END_FOR_EACH_PTR(tmp);
2113 free_ptr_list(&inlines_called);
2116 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
2118 struct symbol *sym;
2120 FOR_EACH_PTR_REVERSE(big_list, sym) {
2121 if (!sym->scope)
2122 continue;
2123 if (use_static && sym->ctype.modifiers & MOD_STATIC)
2124 return sym;
2125 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
2126 return sym;
2127 } END_FOR_EACH_PTR_REVERSE(sym);
2129 return NULL;
2132 static bool interesting_function(struct symbol *sym)
2134 static int prev_stream = -1;
2135 static bool prev_answer;
2136 const char *filename;
2137 int len;
2139 if (!(sym->ctype.modifiers & MOD_INLINE))
2140 return true;
2142 if (sym->pos.stream == prev_stream)
2143 return prev_answer;
2145 prev_stream = sym->pos.stream;
2146 prev_answer = false;
2148 filename = stream_name(sym->pos.stream);
2149 len = strlen(filename);
2150 if (len > 0 && filename[len - 1] == 'c')
2151 prev_answer = true;
2152 return prev_answer;
2155 static void split_inlines_in_scope(struct symbol *sym)
2157 struct symbol *base;
2158 struct symbol_list *scope_list;
2159 int stream;
2161 scope_list = sym->scope->symbols;
2162 stream = sym->pos.stream;
2164 /* find the last static symbol in the file */
2165 FOR_EACH_PTR_REVERSE(scope_list, sym) {
2166 if (sym->pos.stream != stream)
2167 continue;
2168 if (sym->type != SYM_NODE)
2169 continue;
2170 base = get_base_type(sym);
2171 if (!base)
2172 continue;
2173 if (base->type != SYM_FN)
2174 continue;
2175 if (!base->inline_stmt)
2176 continue;
2177 if (!interesting_function(sym))
2178 continue;
2179 add_inline_function(sym);
2180 } END_FOR_EACH_PTR_REVERSE(sym);
2182 process_inlines();
2185 static void split_inlines(struct symbol_list *sym_list)
2187 struct symbol *sym;
2189 sym = get_last_scoped_symbol(sym_list, 0);
2190 if (sym)
2191 split_inlines_in_scope(sym);
2192 sym = get_last_scoped_symbol(sym_list, 1);
2193 if (sym)
2194 split_inlines_in_scope(sym);
2197 static struct stree *clone_estates_perm(struct stree *orig)
2199 struct stree *ret = NULL;
2200 struct sm_state *tmp;
2202 FOR_EACH_SM(orig, tmp) {
2203 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
2204 } END_FOR_EACH_SM(tmp);
2206 return ret;
2209 struct position last_pos;
2210 static void split_c_file_functions(struct symbol_list *sym_list)
2212 struct symbol *sym;
2214 __unnullify_path();
2215 FOR_EACH_PTR(sym_list, sym) {
2216 set_position(sym->pos);
2217 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
2218 __pass_to_client(sym, BASE_HOOK);
2219 fake_global_assign(sym);
2220 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
2222 } END_FOR_EACH_PTR(sym);
2223 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
2224 nullify_path();
2226 FOR_EACH_PTR(sym_list, sym) {
2227 set_position(sym->pos);
2228 last_pos = sym->pos;
2229 if (!interesting_function(sym))
2230 continue;
2231 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
2232 split_function(sym);
2233 process_inlines();
2235 last_pos = sym->pos;
2236 } END_FOR_EACH_PTR(sym);
2237 split_inlines(sym_list);
2238 __pass_to_client(sym_list, END_FILE_HOOK);
2241 static int final_before_fake;
2242 void init_fake_env(void)
2244 if (!in_fake_env)
2245 final_before_fake = final_pass;
2246 in_fake_env++;
2247 __push_fake_cur_stree();
2248 final_pass = 0;
2251 void end_fake_env(void)
2253 __free_fake_cur_stree();
2254 in_fake_env--;
2255 if (!in_fake_env)
2256 final_pass = final_before_fake;
2259 static void open_output_files(char *base_file)
2261 char buf[256];
2263 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
2264 sm_outfd = fopen(buf, "w");
2265 if (!sm_outfd)
2266 sm_fatal("Cannot open %s", buf);
2268 if (!option_info)
2269 return;
2271 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
2272 sql_outfd = fopen(buf, "w");
2273 if (!sql_outfd)
2274 sm_fatal("Error: Cannot open %s", buf);
2276 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
2277 caller_info_fd = fopen(buf, "w");
2278 if (!caller_info_fd)
2279 sm_fatal("Error: Cannot open %s", buf);
2282 void smatch(struct string_list *filelist)
2284 struct symbol_list *sym_list;
2285 struct timeval stop, start;
2286 char *path;
2287 int len;
2289 gettimeofday(&start, NULL);
2291 FOR_EACH_PTR_NOTAG(filelist, base_file) {
2292 path = getcwd(NULL, 0);
2293 free(full_base_file);
2294 if (path) {
2295 len = strlen(path) + 1 + strlen(base_file) + 1;
2296 full_base_file = malloc(len);
2297 snprintf(full_base_file, len, "%s/%s", path, base_file);
2298 } else {
2299 full_base_file = alloc_string(base_file);
2301 if (option_file_output)
2302 open_output_files(base_file);
2303 base_file_stream = input_stream_nr;
2304 sym_list = sparse_keep_tokens(base_file);
2305 split_c_file_functions(sym_list);
2306 } END_FOR_EACH_PTR_NOTAG(base_file);
2308 gettimeofday(&stop, NULL);
2310 set_position(last_pos);
2311 final_pass = 1;
2312 if (option_time)
2313 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
2314 if (option_mem)
2315 sm_msg("mem: %luKb", get_max_memory());