hooks: add a warning to detect unimplemented hooks
[smatch.git] / smatch_flow.c
blobab654a8c9f2a999fc438f1d9adef86040c359599
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 static void set_position(struct position pos)
133 int len;
134 static int prev_stream = -1;
136 if (in_fake_env)
137 return;
139 if (pos.stream == 0 && pos.line == 0)
140 return;
142 __smatch_lineno = pos.line;
144 if (pos.stream == prev_stream)
145 return;
147 filename = stream_name(pos.stream);
149 free(full_filename);
150 pathname = getcwd(NULL, 0);
151 if (pathname) {
152 len = strlen(pathname) + 1 + strlen(filename) + 1;
153 full_filename = malloc(len);
154 snprintf(full_filename, len, "%s/%s", pathname, filename);
155 } else {
156 full_filename = alloc_string(filename);
158 free(pathname);
161 int is_assigned_call(struct expression *expr)
163 struct expression *parent = expr_get_parent_expr(expr);
165 if (parent &&
166 parent->type == EXPR_ASSIGNMENT &&
167 parent->op == '=' &&
168 strip_expr(parent->right) == expr)
169 return 1;
171 return 0;
174 int is_fake_assigned_call(struct expression *expr)
176 struct expression *parent = expr_get_fake_parent_expr(expr);
178 if (parent &&
179 parent->type == EXPR_ASSIGNMENT &&
180 parent->op == '=' &&
181 strip_expr(parent->right) == expr)
182 return 1;
184 return 0;
187 static bool is_inline_func(struct expression *expr)
189 if (expr->type != EXPR_SYMBOL || !expr->symbol)
190 return false;
191 if (!expr->symbol->definition)
192 return false;
193 if (expr->symbol->definition->ctype.modifiers & MOD_INLINE)
194 return true;
196 return 0;
199 static int is_noreturn_func(struct expression *expr)
201 if (expr->type != EXPR_SYMBOL || !expr->symbol)
202 return 0;
205 * It's almost impossible for Smatch to handle __builtin_constant_p()
206 * the same way that GCC does so Smatch ends up making some functions
207 * as no return functions incorrectly.
210 if (option_project == PROJ_KERNEL && expr->symbol->ident &&
211 strstr(expr->symbol->ident->name, "__compiletime_assert"))
212 return 0;
214 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
215 return 1;
216 return 0;
219 static int save_func_time(void *_rl, int argc, char **argv, char **azColName)
221 unsigned long *rl = _rl;
223 *rl = strtoul(argv[0], NULL, 10);
224 return 0;
227 static int get_func_time(struct symbol *sym)
229 unsigned long time = 0;
231 run_sql(&save_func_time, &time,
232 "select key from return_implies where %s and type = %d;",
233 get_static_filter(sym), FUNC_TIME);
235 return time;
238 static int inline_budget = 20;
240 int inlinable(struct expression *expr)
242 struct symbol *sym;
243 struct statement *last_stmt = NULL;
245 if (__inline_fn) /* don't nest */
246 return 0;
248 if (expr->type != EXPR_SYMBOL || !expr->symbol)
249 return 0;
250 if (is_no_inline_function(expr->symbol->ident->name))
251 return 0;
252 sym = get_base_type(expr->symbol);
253 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
254 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
255 return 0;
256 if (sym->stmt->type != STMT_COMPOUND)
257 return 0;
258 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
260 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
261 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
262 return 0;
263 if (sym->inline_stmt->type != STMT_COMPOUND)
264 return 0;
265 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
268 if (!last_stmt)
269 return 0;
271 /* the magic numbers in this function are pulled out of my bum. */
272 if (last_stmt->pos.line > sym->pos.line + inline_budget)
273 return 0;
275 if (get_func_time(expr->symbol) >= 2)
276 return 0;
278 return 1;
281 void __process_post_op_stack(void)
283 struct expression *expr;
285 FOR_EACH_PTR(post_op_stack, expr) {
286 __pass_to_client(expr, OP_HOOK);
287 } END_FOR_EACH_PTR(expr);
289 __free_ptr_list((struct ptr_list **)&post_op_stack);
292 static int handle_comma_assigns(struct expression *expr)
294 struct expression *right;
295 struct expression *assign;
297 right = strip_expr(expr->right);
298 if (right->type != EXPR_COMMA)
299 return 0;
301 __split_expr(right->left);
302 __process_post_op_stack();
304 assign = assign_expression(expr->left, '=', right->right);
305 __split_expr(assign);
307 return 1;
310 /* This is to handle *p++ = foo; assignments */
311 static int handle_postop_assigns(struct expression *expr)
313 struct expression *left, *fake_left;
314 struct expression *assign;
316 left = strip_expr(expr->left);
317 if (left->type != EXPR_PREOP || left->op != '*')
318 return 0;
319 left = strip_expr(left->unop);
320 if (left->type != EXPR_POSTOP)
321 return 0;
323 fake_left = deref_expression(strip_expr(left->unop));
324 assign = assign_expression(fake_left, '=', expr->right);
326 __split_expr(assign);
327 __split_expr(expr->left);
329 return 1;
332 static int prev_expression_is_getting_address(struct expression *expr)
334 struct expression *parent;
336 do {
337 parent = expr_get_parent_expr(expr);
339 if (!parent)
340 return 0;
341 if (parent->type == EXPR_PREOP && parent->op == '&')
342 return 1;
343 if (parent->type == EXPR_PREOP && parent->op == '(')
344 goto next;
345 if (parent->type == EXPR_DEREF && parent->op == '.')
346 goto next;
347 /* Handle &foo->array[offset] */
348 if (parent->type == EXPR_BINOP && parent->op == '+') {
349 parent = expr_get_parent_expr(parent);
350 if (!parent)
351 return 0;
352 if (parent->type == EXPR_PREOP && parent->op == '*')
353 goto next;
356 return 0;
357 next:
358 expr = parent;
359 } while (1);
362 static void handle_builtin_overflow_func(struct expression *expr)
364 struct expression *a, *b, *res, *assign;
365 int op;
367 if (sym_name_is("__builtin_add_overflow", expr->fn))
368 op = '+';
369 else if (sym_name_is("__builtin_sub_overflow", expr->fn))
370 op = '-';
371 else if (sym_name_is("__builtin_mul_overflow", expr->fn))
372 op = '*';
373 else
374 return;
376 a = get_argument_from_call_expr(expr->args, 0);
377 b = get_argument_from_call_expr(expr->args, 1);
378 res = get_argument_from_call_expr(expr->args, 2);
380 assign = assign_expression(deref_expression(res), '=', binop_expression(a, op, b));
381 __split_expr(assign);
384 static int handle__builtin_choose_expr(struct expression *expr)
386 struct expression *const_expr, *expr1, *expr2;
387 sval_t sval;
389 if (!sym_name_is("__builtin_choose_expr", expr->fn))
390 return 0;
392 const_expr = get_argument_from_call_expr(expr->args, 0);
393 expr1 = get_argument_from_call_expr(expr->args, 1);
394 expr2 = get_argument_from_call_expr(expr->args, 2);
396 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
397 return 0;
398 if (sval.value)
399 __split_expr(expr1);
400 else
401 __split_expr(expr2);
402 return 1;
405 static int handle__builtin_choose_expr_assigns(struct expression *expr)
407 struct expression *const_expr, *right, *expr1, *expr2, *fake;
408 sval_t sval;
410 right = strip_parens(expr->right);
411 if (right->type != EXPR_CALL)
412 return 0;
413 if (!sym_name_is("__builtin_choose_expr", right->fn))
414 return 0;
416 const_expr = get_argument_from_call_expr(right->args, 0);
417 expr1 = get_argument_from_call_expr(right->args, 1);
418 expr2 = get_argument_from_call_expr(right->args, 2);
420 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
421 return 0;
423 fake = assign_expression(expr->left, '=', sval.value ? expr1 : expr2);
424 __split_expr(fake);
425 return 1;
428 int is_condition_call(struct expression *expr)
430 struct expression *tmp;
432 FOR_EACH_PTR_REVERSE(big_condition_stack, tmp) {
433 if (expr == tmp || expr_get_parent_expr(expr) == tmp)
434 return 1;
435 if (tmp->pos.line < expr->pos.line)
436 return 0;
437 } END_FOR_EACH_PTR_REVERSE(tmp);
439 return 0;
442 static struct expression *expr_get_parent_no_parens(struct expression *expr)
444 do {
445 expr = expr_get_parent_expr(expr);
446 } while (expr &&
447 expr->type == EXPR_PREOP &&
448 expr->op == '(');
450 return expr;
453 static bool gen_fake_function_assign(struct expression *expr)
455 static struct expression *parsed;
456 struct expression *assign, *parent;
457 struct symbol *type;
458 char buf[64];
460 /* The rule is that every non-void function call has to be part of an
461 * assignment. TODO: Should we create a fake non-casted assignment
462 * for casted assignments? Also faked assigns for += assignments?
464 type = get_type(expr);
465 if (!type || type == &void_ctype)
466 return false;
468 parent = expr_get_parent_no_parens(expr);
469 if (parent && parent->type == EXPR_ASSIGNMENT)
470 return false;
472 parent = expr_get_fake_parent_expr(expr);
473 if (parent) {
474 struct expression *left = parent->left;
476 if (parent == parsed)
477 return false;
478 if (!left || left->type != EXPR_SYMBOL)
479 return false;
480 if (strncmp(left->symbol_name->name, "__fake_assign_", 14) != 0)
481 return false;
482 parsed = parent;
483 __split_expr(parent);
484 return true;
487 // TODO: faked_assign skipping conditions is a hack
488 if (is_condition_call(expr))
489 return false;
491 snprintf(buf, sizeof(buf), "__fake_assign_%p", expr);
492 assign = create_fake_assign(buf, get_type(expr), expr);
494 parsed = assign;
495 __split_expr(assign);
496 return true;
499 static void split_call(struct expression *expr)
501 if (gen_fake_function_assign(expr))
502 return;
504 expr_set_parent_expr(expr->fn, expr);
506 if (sym_name_is("__builtin_constant_p", expr->fn))
507 return;
508 if (handle__builtin_choose_expr(expr))
509 return;
510 __split_expr(expr->fn);
511 split_args(expr);
512 if (is_inline_func(expr->fn))
513 add_inline_function(expr->fn->symbol->definition);
514 if (inlinable(expr->fn))
515 __inline_call = 1;
516 __process_post_op_stack();
517 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
518 __pass_to_client(expr, FUNCTION_CALL_HOOK);
519 __inline_call = 0;
520 if (inlinable(expr->fn))
521 parse_inline(expr);
522 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
523 if (is_noreturn_func(expr->fn))
524 nullify_path();
525 if (!expr_get_parent_expr(expr))
526 __discard_fake_states(expr);
527 handle_builtin_overflow_func(expr);
530 void __split_expr(struct expression *expr)
532 if (!expr)
533 return;
535 // if (local_debug)
536 // sm_msg("Debug expr_type %d %s expr = '%s'", expr->type, show_special(expr->op), expr_to_str(expr));
538 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
539 return;
540 if (__in_fake_assign >= 4) /* don't allow too much nesting */
541 return;
543 push_expression(&big_expression_stack, expr);
544 set_position(expr->pos);
545 __pass_to_client(expr, EXPR_HOOK);
547 switch (expr->type) {
548 case EXPR_PREOP:
549 expr_set_parent_expr(expr->unop, expr);
551 if (expr->op == '*' &&
552 !prev_expression_is_getting_address(expr))
553 __pass_to_client(expr, DEREF_HOOK);
554 __split_expr(expr->unop);
555 __pass_to_client(expr, OP_HOOK);
556 break;
557 case EXPR_POSTOP:
558 expr_set_parent_expr(expr->unop, expr);
560 __split_expr(expr->unop);
561 push_expression(&post_op_stack, expr);
562 break;
563 case EXPR_STATEMENT:
564 __expr_stmt_count++;
565 if (expr->statement && !expr->statement) {
566 stmt_set_parent_stmt(expr->statement,
567 last_ptr_list((struct ptr_list *)big_statement_stack));
569 __split_stmt(expr->statement);
570 __expr_stmt_count--;
571 break;
572 case EXPR_LOGICAL:
573 case EXPR_COMPARE:
574 expr_set_parent_expr(expr->left, expr);
575 expr_set_parent_expr(expr->right, expr);
577 __pass_to_client(expr, LOGIC_HOOK);
578 __handle_logic(expr);
579 break;
580 case EXPR_BINOP:
581 expr_set_parent_expr(expr->left, expr);
582 expr_set_parent_expr(expr->right, expr);
584 __pass_to_client(expr, BINOP_HOOK);
585 __split_expr(expr->left);
586 __split_expr(expr->right);
587 break;
588 case EXPR_COMMA:
589 expr_set_parent_expr(expr->left, expr);
590 expr_set_parent_expr(expr->right, expr);
592 __split_expr(expr->left);
593 __process_post_op_stack();
594 __split_expr(expr->right);
595 break;
596 case EXPR_ASSIGNMENT: {
597 struct expression *right;
599 expr_set_parent_expr(expr->left, expr);
600 expr_set_parent_expr(expr->right, expr);
602 right = strip_expr(expr->right);
603 if (!right)
604 break;
606 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
608 /* foo = !bar() */
609 if (__handle_condition_assigns(expr))
610 goto after_assign;
611 /* foo = (x < 5 ? foo : 5); */
612 if (__handle_select_assigns(expr))
613 goto after_assign;
614 /* foo = ({frob(); frob(); frob(); 1;}) */
615 if (__handle_expr_statement_assigns(expr))
616 break; // FIXME: got after
617 /* foo = (3, 4); */
618 if (handle_comma_assigns(expr))
619 goto after_assign;
620 if (handle__builtin_choose_expr_assigns(expr))
621 goto after_assign;
622 if (handle_postop_assigns(expr))
623 break; /* no need to goto after_assign */
625 __split_expr(expr->right);
626 if (outside_of_function())
627 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
628 else
629 __pass_to_client(expr, ASSIGNMENT_HOOK);
631 __fake_struct_member_assignments(expr);
633 /* Re-examine ->right for inlines. See the commit message */
634 right = strip_expr(expr->right);
635 if (expr->op == '=' && right->type == EXPR_CALL)
636 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
638 after_assign:
639 if (get_macro_name(right->pos) &&
640 get_macro_name(expr->pos) != get_macro_name(right->pos))
641 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
643 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
644 __split_expr(expr->left);
645 break;
647 case EXPR_DEREF:
648 expr_set_parent_expr(expr->deref, expr);
650 __pass_to_client(expr, DEREF_HOOK);
651 __split_expr(expr->deref);
652 break;
653 case EXPR_SLICE:
654 expr_set_parent_expr(expr->base, expr);
656 __split_expr(expr->base);
657 break;
658 case EXPR_CAST:
659 case EXPR_FORCE_CAST:
660 expr_set_parent_expr(expr->cast_expression, expr);
662 __pass_to_client(expr, CAST_HOOK);
663 __split_expr(expr->cast_expression);
664 break;
665 case EXPR_SIZEOF:
666 if (expr->cast_expression)
667 __pass_to_client(strip_parens(expr->cast_expression),
668 SIZEOF_HOOK);
669 break;
670 case EXPR_OFFSETOF:
671 case EXPR_ALIGNOF:
672 break;
673 case EXPR_CONDITIONAL:
674 case EXPR_SELECT:
675 expr_set_parent_expr(expr->conditional, expr);
676 expr_set_parent_expr(expr->cond_true, expr);
677 expr_set_parent_expr(expr->cond_false, expr);
679 if (known_condition_true(expr->conditional)) {
680 __split_expr(expr->cond_true);
681 break;
683 if (known_condition_false(expr->conditional)) {
684 __split_expr(expr->cond_false);
685 break;
687 __pass_to_client(expr, SELECT_HOOK);
688 __split_whole_condition(expr->conditional);
689 __split_expr(expr->cond_true);
690 __push_true_states();
691 __use_false_states();
692 __split_expr(expr->cond_false);
693 __merge_true_states();
694 break;
695 case EXPR_CALL:
696 split_call(expr);
697 break;
698 case EXPR_INITIALIZER:
699 split_expr_list(expr->expr_list, expr);
700 break;
701 case EXPR_IDENTIFIER:
702 expr_set_parent_expr(expr->ident_expression, expr);
703 __split_expr(expr->ident_expression);
704 break;
705 case EXPR_INDEX:
706 expr_set_parent_expr(expr->idx_expression, expr);
707 __split_expr(expr->idx_expression);
708 break;
709 case EXPR_POS:
710 expr_set_parent_expr(expr->init_expr, expr);
711 __split_expr(expr->init_expr);
712 break;
713 case EXPR_SYMBOL:
714 __pass_to_client(expr, SYM_HOOK);
715 break;
716 case EXPR_STRING:
717 __pass_to_client(expr, STRING_HOOK);
718 break;
719 case EXPR_GENERIC: {
720 struct expression *tmp;
722 tmp = strip_Generic(expr);
723 if (tmp != expr)
724 __split_expr(tmp);
725 break;
727 default:
728 break;
730 __pass_to_client(expr, EXPR_HOOK_AFTER);
731 pop_expression(&big_expression_stack);
734 static int is_forever_loop(struct statement *stmt)
736 struct expression *expr;
737 sval_t sval;
739 expr = strip_expr(stmt->iterator_pre_condition);
740 if (!expr)
741 expr = stmt->iterator_post_condition;
742 if (!expr) {
743 /* this is a for(;;) loop... */
744 return 1;
747 if (get_value(expr, &sval) && sval.value != 0)
748 return 1;
750 return 0;
753 static int loop_num;
754 static char *get_loop_name(int num)
756 char buf[256];
758 snprintf(buf, 255, "-loop%d", num);
759 buf[255] = '\0';
760 return alloc_sname(buf);
764 * Pre Loops are while and for loops.
766 static void handle_pre_loop(struct statement *stmt)
768 int once_through; /* we go through the loop at least once */
769 struct sm_state *extra_sm = NULL;
770 int unchanged = 0;
771 char *loop_name;
772 struct stree *stree = NULL;
773 struct sm_state *sm = NULL;
775 loop_name = get_loop_name(loop_num);
776 loop_num++;
778 if (stmt->iterator_pre_statement) {
779 __split_stmt(stmt->iterator_pre_statement);
780 __prev_stmt = stmt->iterator_pre_statement;
783 once_through = implied_condition_true(stmt->iterator_pre_condition);
785 loop_count++;
786 __push_continues();
787 __push_breaks();
789 __merge_gotos(loop_name, NULL);
791 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
792 __in_pre_condition++;
793 __pass_to_client(stmt, PRELOOP_HOOK);
794 __split_whole_condition(stmt->iterator_pre_condition);
795 __in_pre_condition--;
796 FOR_EACH_SM(stree, sm) {
797 set_state(sm->owner, sm->name, sm->sym, sm->state);
798 } END_FOR_EACH_SM(sm);
799 free_stree(&stree);
800 if (extra_sm)
801 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
803 if (option_assume_loops)
804 once_through = 1;
806 __split_stmt(stmt->iterator_statement);
807 if (is_forever_loop(stmt)) {
808 __merge_continues();
809 __save_gotos(loop_name, NULL);
811 __push_fake_cur_stree();
812 __split_stmt(stmt->iterator_post_statement);
813 stree = __pop_fake_cur_stree();
815 __discard_false_states();
816 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
817 __use_breaks();
819 if (!__path_is_null())
820 __merge_stree_into_cur(stree);
821 free_stree(&stree);
822 } else {
823 __merge_continues();
824 unchanged = __iterator_unchanged(extra_sm);
825 __split_stmt(stmt->iterator_post_statement);
826 __prev_stmt = stmt->iterator_post_statement;
827 __cur_stmt = stmt;
829 __save_gotos(loop_name, NULL);
830 __in_pre_condition++;
831 __split_whole_condition(stmt->iterator_pre_condition);
832 __in_pre_condition--;
833 nullify_path();
834 __merge_false_states();
835 if (once_through)
836 __discard_false_states();
837 else
838 __merge_false_states();
840 if (extra_sm && unchanged)
841 __extra_pre_loop_hook_after(extra_sm,
842 stmt->iterator_post_statement,
843 stmt->iterator_pre_condition);
844 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
845 __merge_breaks();
847 loop_count--;
851 * Post loops are do {} while();
853 static void handle_post_loop(struct statement *stmt)
855 char *loop_name;
857 loop_name = get_loop_name(loop_num);
858 loop_num++;
859 loop_count++;
861 __pass_to_client(stmt, POSTLOOP_HOOK);
863 __push_continues();
864 __push_breaks();
865 __merge_gotos(loop_name, NULL);
866 __split_stmt(stmt->iterator_statement);
867 __merge_continues();
868 if (!expr_is_zero(stmt->iterator_post_condition))
869 __save_gotos(loop_name, NULL);
871 if (is_forever_loop(stmt)) {
872 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
873 __use_breaks();
874 } else {
875 __split_whole_condition(stmt->iterator_post_condition);
876 __use_false_states();
877 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
878 __merge_breaks();
880 loop_count--;
883 static int empty_statement(struct statement *stmt)
885 if (!stmt)
886 return 0;
887 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
888 return 1;
889 return 0;
892 static int last_stmt_on_same_line(void)
894 struct statement *stmt;
895 int i = 0;
897 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
898 if (!i++)
899 continue;
900 if (stmt->pos.line == get_lineno())
901 return 1;
902 return 0;
903 } END_FOR_EACH_PTR_REVERSE(stmt);
904 return 0;
907 static void split_asm_ops(struct asm_operand_list *ops)
909 struct asm_operand *op;
911 FOR_EACH_PTR(ops, op) {
912 __split_expr(op->expr);
913 } END_FOR_EACH_PTR(op);
916 static int is_case_val(struct statement *stmt, sval_t sval)
918 sval_t case_sval;
920 if (stmt->type != STMT_CASE)
921 return 0;
922 if (!stmt->case_expression) {
923 __set_default();
924 return 1;
926 if (!get_value(stmt->case_expression, &case_sval))
927 return 0;
928 if (case_sval.value == sval.value)
929 return 1;
930 return 0;
933 static struct range_list *get_case_rl(struct expression *switch_expr,
934 struct expression *case_expr,
935 struct expression *case_to)
937 sval_t start, end;
938 struct range_list *rl = NULL;
939 struct symbol *switch_type;
941 switch_type = get_type(switch_expr);
942 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
943 start = sval_cast(switch_type, start);
944 end = sval_cast(switch_type, end);
945 add_range(&rl, start, end);
946 } else if (get_value(case_expr, &start)) {
947 start = sval_cast(switch_type, start);
948 add_range(&rl, start, start);
951 return rl;
954 static void split_known_switch(struct statement *stmt, sval_t sval)
956 struct statement *tmp;
957 struct range_list *rl;
959 __split_expr(stmt->switch_expression);
960 sval = sval_cast(get_type(stmt->switch_expression), sval);
962 push_expression(&switch_expr_stack, stmt->switch_expression);
963 __save_switch_states(top_expression(switch_expr_stack));
964 nullify_path();
965 __push_default();
966 __push_breaks();
968 stmt = stmt->switch_statement;
970 __push_scope_hooks();
971 FOR_EACH_PTR(stmt->stmts, tmp) {
972 __smatch_lineno = tmp->pos.line;
973 // FIXME: what if default comes before the known case statement?
974 if (is_case_val(tmp, sval)) {
975 rl = alloc_rl(sval, sval);
976 __merge_switches(top_expression(switch_expr_stack), rl);
977 __pass_case_to_client(top_expression(switch_expr_stack), rl);
978 stmt_set_parent_stmt(tmp->case_statement, tmp);
979 __split_stmt(tmp->case_statement);
980 goto next;
982 if (__path_is_null())
983 continue;
984 __split_stmt(tmp);
985 next:
986 if (__path_is_null()) {
987 __set_default();
988 goto out;
990 } END_FOR_EACH_PTR(tmp);
991 out:
992 __call_scope_hooks();
993 if (!__pop_default())
994 __merge_switches(top_expression(switch_expr_stack), NULL);
995 __discard_switches();
996 __merge_breaks();
997 pop_expression(&switch_expr_stack);
1000 static void split_case(struct statement *stmt)
1002 struct range_list *rl = NULL;
1004 expr_set_parent_stmt(stmt->case_expression, stmt);
1005 expr_set_parent_stmt(stmt->case_to, stmt);
1007 rl = get_case_rl(top_expression(switch_expr_stack),
1008 stmt->case_expression, stmt->case_to);
1009 while (stmt->case_statement->type == STMT_CASE) {
1010 struct range_list *tmp;
1012 tmp = get_case_rl(top_expression(switch_expr_stack),
1013 stmt->case_statement->case_expression,
1014 stmt->case_statement->case_to);
1015 if (!tmp)
1016 goto next;
1017 rl = rl_union(rl, tmp);
1018 if (!stmt->case_expression)
1019 __set_default();
1020 next:
1021 stmt = stmt->case_statement;
1024 __merge_switches(top_expression(switch_expr_stack), rl);
1026 if (!stmt->case_expression)
1027 __set_default();
1029 stmt_set_parent_stmt(stmt->case_statement, stmt);
1030 __split_stmt(stmt->case_statement);
1033 int time_parsing_function(void)
1035 return ms_since(&fn_start_time) / 1000;
1038 bool taking_too_long(void)
1040 if ((ms_since(&outer_fn_start_time) / 1000) > 60 * 5) /* five minutes */
1041 return 1;
1042 return 0;
1045 struct statement *get_last_stmt(void)
1047 struct symbol *fn;
1048 struct statement *stmt;
1050 fn = get_base_type(cur_func_sym);
1051 if (!fn)
1052 return NULL;
1053 stmt = fn->stmt;
1054 if (!stmt)
1055 stmt = fn->inline_stmt;
1056 if (!stmt || stmt->type != STMT_COMPOUND)
1057 return NULL;
1058 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1059 if (stmt && stmt->type == STMT_LABEL)
1060 stmt = stmt->label_statement;
1061 return stmt;
1064 int is_last_stmt(struct statement *cur_stmt)
1066 struct statement *last;
1068 last = get_last_stmt();
1069 if (last && last == cur_stmt)
1070 return 1;
1071 return 0;
1074 static void handle_backward_goto(struct statement *goto_stmt)
1076 const char *goto_name, *label_name;
1077 struct statement *func_stmt;
1078 struct symbol *base_type = get_base_type(cur_func_sym);
1079 struct statement *tmp;
1080 int found = 0;
1082 if (!option_info)
1083 return;
1084 if (last_goto_statement_handled)
1085 return;
1086 last_goto_statement_handled = 1;
1088 if (!goto_stmt->goto_label ||
1089 goto_stmt->goto_label->type != SYM_LABEL ||
1090 !goto_stmt->goto_label->ident)
1091 return;
1092 goto_name = goto_stmt->goto_label->ident->name;
1094 func_stmt = base_type->stmt;
1095 if (!func_stmt)
1096 func_stmt = base_type->inline_stmt;
1097 if (!func_stmt)
1098 return;
1099 if (func_stmt->type != STMT_COMPOUND)
1100 return;
1102 FOR_EACH_PTR(func_stmt->stmts, tmp) {
1103 if (!found) {
1104 if (tmp->type != STMT_LABEL)
1105 continue;
1106 if (!tmp->label_identifier ||
1107 tmp->label_identifier->type != SYM_LABEL ||
1108 !tmp->label_identifier->ident)
1109 continue;
1110 label_name = tmp->label_identifier->ident->name;
1111 if (strcmp(goto_name, label_name) != 0)
1112 continue;
1113 found = 1;
1115 __split_stmt(tmp);
1116 } END_FOR_EACH_PTR(tmp);
1119 static void fake_a_return(void)
1121 struct expression *ret = NULL;
1123 nullify_path();
1124 __unnullify_path();
1126 if (cur_func_return_type() != &void_ctype)
1127 ret = unknown_value_expression(NULL);
1129 __pass_to_client(ret, RETURN_HOOK);
1130 nullify_path();
1133 static void split_ret_value(struct expression *expr)
1135 struct symbol *type;
1137 if (!expr)
1138 return;
1140 type = get_real_base_type(cur_func_sym);
1141 type = get_real_base_type(type);
1142 expr = fake_a_variable_assign(type, NULL, expr, -1);
1144 __in_fake_var_assign++;
1145 __split_expr(expr);
1146 __in_fake_var_assign--;
1149 static void fake_an_empty_default(struct position pos)
1151 static struct statement none = {};
1153 none.pos = pos;
1154 none.type = STMT_NONE;
1155 __merge_switches(top_expression(switch_expr_stack), NULL);
1156 __split_stmt(&none);
1159 static void split_compound(struct statement *stmt)
1161 struct statement *prev = NULL;
1162 struct statement *cur = NULL;
1163 struct statement *next;
1165 __push_scope_hooks();
1167 FOR_EACH_PTR(stmt->stmts, next) {
1168 /* just set them all ahead of time */
1169 stmt_set_parent_stmt(next, stmt);
1171 if (cur) {
1172 __prev_stmt = prev;
1173 __next_stmt = next;
1174 __cur_stmt = cur;
1175 __split_stmt(cur);
1177 prev = cur;
1178 cur = next;
1179 } END_FOR_EACH_PTR(next);
1180 if (cur) {
1181 __prev_stmt = prev;
1182 __cur_stmt = cur;
1183 __next_stmt = NULL;
1184 __split_stmt(cur);
1188 * For function scope, then delay calling the scope hooks until the
1189 * end of function hooks can run. I'm not positive this is the right
1190 * thing...
1192 if (!is_last_stmt(cur))
1193 __call_scope_hooks();
1197 * This is a hack, work around for detecting empty functions.
1199 static int need_delayed_scope_hooks(void)
1201 struct symbol *fn = get_base_type(cur_func_sym);
1202 struct statement *stmt;
1204 if (!fn)
1205 return 0;
1206 stmt = fn->stmt;
1207 if (!stmt)
1208 stmt = fn->inline_stmt;
1209 if (stmt && stmt->type == STMT_COMPOUND)
1210 return 1;
1211 return 0;
1214 void __split_label_stmt(struct statement *stmt)
1216 if (stmt->label_identifier &&
1217 stmt->label_identifier->type == SYM_LABEL &&
1218 stmt->label_identifier->ident) {
1219 loop_count |= 0x0800000;
1220 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
1224 static void find_asm_gotos(struct statement *stmt)
1226 struct symbol *sym;
1228 FOR_EACH_PTR(stmt->asm_labels, sym) {
1229 __save_gotos(sym->ident->name, sym);
1230 } END_FOR_EACH_PTR(sym);
1233 void __split_stmt(struct statement *stmt)
1235 static int indent_cnt;
1236 sval_t sval;
1237 struct timeval start, stop;
1239 gettimeofday(&start, NULL);
1241 if (!stmt)
1242 goto out;
1244 if (!__in_fake_assign)
1245 __silence_warnings_for_stmt = false;
1247 if (__bail_on_rest_of_function || is_skipped_function())
1248 return;
1250 if (out_of_memory() || taking_too_long()) {
1251 gettimeofday(&start, NULL);
1253 __bail_on_rest_of_function = 1;
1254 final_pass = 1;
1255 sm_perror("Function too hairy. Giving up. %lu seconds",
1256 start.tv_sec - fn_start_time.tv_sec);
1257 fake_a_return();
1258 final_pass = 0; /* turn off sm_msg() from here */
1259 return;
1262 indent_cnt++;
1264 add_ptr_list(&big_statement_stack, stmt);
1265 free_expression_stack(&big_expression_stack);
1266 set_position(stmt->pos);
1267 __pass_to_client(stmt, STMT_HOOK);
1269 switch (stmt->type) {
1270 case STMT_DECLARATION:
1271 split_declaration(stmt->declaration);
1272 break;
1273 case STMT_RETURN:
1274 expr_set_parent_stmt(stmt->ret_value, stmt);
1276 split_ret_value(stmt->ret_value);
1277 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1278 __process_post_op_stack();
1279 nullify_path();
1280 break;
1281 case STMT_EXPRESSION:
1282 expr_set_parent_stmt(stmt->expression, stmt);
1283 expr_set_parent_stmt(stmt->context, stmt);
1285 __split_expr(stmt->expression);
1286 break;
1287 case STMT_COMPOUND:
1288 split_compound(stmt);
1289 break;
1290 case STMT_IF:
1291 stmt_set_parent_stmt(stmt->if_true, stmt);
1292 stmt_set_parent_stmt(stmt->if_false, stmt);
1293 expr_set_parent_stmt(stmt->if_conditional, stmt);
1295 if (known_condition_true(stmt->if_conditional)) {
1296 __split_stmt(stmt->if_true);
1297 break;
1299 if (known_condition_false(stmt->if_conditional)) {
1300 __split_stmt(stmt->if_false);
1301 break;
1303 __split_whole_condition(stmt->if_conditional);
1304 __split_stmt(stmt->if_true);
1305 if (empty_statement(stmt->if_true) &&
1306 last_stmt_on_same_line() &&
1307 !get_macro_name(stmt->if_true->pos))
1308 sm_warning("if();");
1309 __push_true_states();
1310 __use_false_states();
1311 __split_stmt(stmt->if_false);
1312 __merge_true_states();
1313 break;
1314 case STMT_ITERATOR:
1315 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1316 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1317 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1318 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1319 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1321 if (stmt->iterator_pre_condition)
1322 handle_pre_loop(stmt);
1323 else if (stmt->iterator_post_condition)
1324 handle_post_loop(stmt);
1325 else {
1326 // these are for(;;) type loops.
1327 handle_pre_loop(stmt);
1329 break;
1330 case STMT_SWITCH:
1331 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1332 expr_set_parent_stmt(stmt->switch_expression, stmt);
1334 if (get_value(stmt->switch_expression, &sval)) {
1335 split_known_switch(stmt, sval);
1336 break;
1338 __split_expr(stmt->switch_expression);
1339 push_expression(&switch_expr_stack, stmt->switch_expression);
1340 __save_switch_states(top_expression(switch_expr_stack));
1341 nullify_path();
1342 __push_default();
1343 __push_breaks();
1344 __split_stmt(stmt->switch_statement);
1345 if (!__pop_default() && have_remaining_cases())
1346 fake_an_empty_default(stmt->pos);
1347 __discard_switches();
1348 __merge_breaks();
1349 pop_expression(&switch_expr_stack);
1350 break;
1351 case STMT_CASE:
1352 split_case(stmt);
1353 break;
1354 case STMT_LABEL:
1355 __split_label_stmt(stmt);
1356 __split_stmt(stmt->label_statement);
1357 break;
1358 case STMT_GOTO:
1359 expr_set_parent_stmt(stmt->goto_expression, stmt);
1361 __split_expr(stmt->goto_expression);
1362 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1363 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1364 __process_breaks();
1365 } else if (!strcmp(stmt->goto_label->ident->name,
1366 "continue")) {
1367 __process_continues();
1369 } else if (stmt->goto_label &&
1370 stmt->goto_label->type == SYM_LABEL &&
1371 stmt->goto_label->ident) {
1372 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1374 nullify_path();
1375 if (is_last_stmt(stmt))
1376 handle_backward_goto(stmt);
1377 break;
1378 case STMT_NONE:
1379 break;
1380 case STMT_ASM:
1381 expr_set_parent_stmt(stmt->asm_string, stmt);
1383 find_asm_gotos(stmt);
1384 __pass_to_client(stmt, ASM_HOOK);
1385 __split_expr(stmt->asm_string);
1386 split_asm_ops(stmt->asm_outputs);
1387 split_asm_ops(stmt->asm_inputs);
1388 split_expr_list(stmt->asm_clobbers, NULL);
1389 break;
1390 case STMT_CONTEXT:
1391 break;
1392 case STMT_RANGE:
1393 __split_expr(stmt->range_expression);
1394 __split_expr(stmt->range_low);
1395 __split_expr(stmt->range_high);
1396 break;
1398 __pass_to_client(stmt, STMT_HOOK_AFTER);
1399 if (--indent_cnt == 1)
1400 __discard_fake_states(NULL);
1402 out:
1403 __process_post_op_stack();
1405 gettimeofday(&stop, NULL);
1406 if (option_time_stmt && stmt)
1407 sm_msg("stmt_time%s: %ld",
1408 stmt->type == STMT_COMPOUND ? "_block" : "",
1409 stop.tv_sec - start.tv_sec);
1412 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1414 struct expression *expr;
1416 FOR_EACH_PTR(expr_list, expr) {
1417 expr_set_parent_expr(expr, parent);
1418 __split_expr(expr);
1419 __process_post_op_stack();
1420 } END_FOR_EACH_PTR(expr);
1423 static bool cast_arg(struct symbol *type, struct expression *arg)
1425 struct symbol *orig;
1427 if (!type)
1428 return false;
1430 arg = strip_parens(arg);
1431 if (arg != strip_expr(arg))
1432 return true;
1434 orig = get_type(arg);
1435 if (!orig)
1436 return true;
1437 if (types_equiv(orig, type))
1438 return false;
1440 if (orig->type == SYM_ARRAY && type->type == SYM_PTR)
1441 return true;
1444 * I would have expected that we could just do use (orig == type) but I
1445 * guess for pointers we need to get the basetype to do that comparison.
1449 if (orig->type != SYM_PTR ||
1450 type->type != SYM_PTR) {
1451 if (type_fits(type, orig))
1452 return false;
1453 return true;
1455 orig = get_real_base_type(orig);
1456 type = get_real_base_type(type);
1457 if (orig == type)
1458 return false;
1460 return true;
1463 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *call, struct expression *expr, int nr)
1465 char buf[64];
1466 bool cast;
1468 if (!expr || !cur_func_sym)
1469 return NULL;
1471 if (expr->type == EXPR_ASSIGNMENT)
1472 return expr;
1474 /* for va_args then we don't know the type */
1475 if (!type)
1476 type = get_type(expr);
1478 cast = cast_arg(type, expr);
1480 * Using expr_to_sym() here is a hack. We want to say that we don't
1481 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1482 * It turns out faking these assignments is way more expensive than I
1483 * would have imagined. I'm not sure why exactly.
1486 if (!cast) {
1488 * if the code is "return *p;" where "p" is a user pointer then
1489 * we want to create a fake assignment so that it sets the state
1490 * in check_kernel_user_data.c.
1493 if (expr->type != EXPR_PREOP &&
1494 expr->op != '*' && expr->op != '&' &&
1495 expr_to_sym(expr))
1496 return expr;
1499 if (nr == -1)
1500 snprintf(buf, sizeof(buf), "__fake_return_%p", expr);
1501 else
1502 snprintf(buf, sizeof(buf), "__fake_param_%p_%d", call, nr);
1504 return create_fake_assign(buf, type, expr);
1507 static void split_args(struct expression *expr)
1509 struct expression *arg, *tmp;
1510 struct symbol *type;
1511 int i;
1513 i = -1;
1514 FOR_EACH_PTR(expr->args, arg) {
1515 i++;
1516 expr_set_parent_expr(arg, expr);
1517 type = get_arg_type(expr->fn, i);
1518 tmp = fake_a_variable_assign(type, expr, arg, i);
1519 if (tmp != arg)
1520 __in_fake_var_assign++;
1521 __split_expr(tmp);
1522 if (tmp != arg)
1523 __in_fake_var_assign--;
1524 __process_post_op_stack();
1525 } END_FOR_EACH_PTR(arg);
1528 static void split_sym(struct symbol *sym)
1530 if (!sym)
1531 return;
1532 if (!(sym->namespace & NS_SYMBOL))
1533 return;
1535 __split_stmt(sym->stmt);
1536 __split_expr(sym->array_size);
1537 split_symlist(sym->arguments);
1538 split_symlist(sym->symbol_list);
1539 __split_stmt(sym->inline_stmt);
1540 split_symlist(sym->inline_symbol_list);
1543 static void split_symlist(struct symbol_list *sym_list)
1545 struct symbol *sym;
1547 FOR_EACH_PTR(sym_list, sym) {
1548 split_sym(sym);
1549 } END_FOR_EACH_PTR(sym);
1552 typedef void (fake_cb)(struct expression *expr);
1554 static int member_to_number(struct expression *expr, struct ident *member)
1556 struct symbol *type, *tmp;
1557 char *name;
1558 int i;
1560 if (!member)
1561 return -1;
1562 name = member->name;
1564 type = get_type(expr);
1565 if (!type || type->type != SYM_STRUCT)
1566 return -1;
1568 i = -1;
1569 FOR_EACH_PTR(type->symbol_list, tmp) {
1570 i++;
1571 if (!tmp->ident)
1572 continue;
1573 if (strcmp(name, tmp->ident->name) == 0)
1574 return i;
1575 } END_FOR_EACH_PTR(tmp);
1576 return -1;
1579 static struct ident *number_to_member(struct expression *expr, int num)
1581 struct symbol *type, *member;
1582 int i = 0;
1584 type = get_type(expr);
1585 if (!type || type->type != SYM_STRUCT)
1586 return NULL;
1588 FOR_EACH_PTR(type->symbol_list, member) {
1589 if (i == num)
1590 return member->ident;
1591 i++;
1592 } END_FOR_EACH_PTR(member);
1593 return NULL;
1596 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1598 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1600 struct expression *edge_member, *assign;
1601 struct symbol *base = get_real_base_type(member);
1602 struct symbol *tmp;
1604 if (member->ident)
1605 expr = member_expression(expr, '.', member->ident);
1607 FOR_EACH_PTR(base->symbol_list, tmp) {
1608 struct symbol *type;
1610 type = get_real_base_type(tmp);
1611 if (!type)
1612 continue;
1614 edge_member = member_expression(expr, '.', tmp->ident);
1615 if (get_extra_state(edge_member))
1616 continue;
1618 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1619 set_inner_struct_members(expr, tmp);
1620 continue;
1623 if (!tmp->ident)
1624 continue;
1626 assign = assign_expression(edge_member, '=', zero_expr());
1627 __split_expr(assign);
1628 } END_FOR_EACH_PTR(tmp);
1633 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1635 struct symbol *tmp;
1636 struct expression *member = NULL;
1637 struct expression *assign;
1639 FOR_EACH_PTR(type->symbol_list, tmp) {
1640 type = get_real_base_type(tmp);
1641 if (!type)
1642 continue;
1644 if (tmp->ident) {
1645 member = member_expression(expr, '.', tmp->ident);
1646 if (get_extra_state(member))
1647 continue;
1650 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1651 set_inner_struct_members(expr, tmp);
1652 continue;
1654 if (type->type == SYM_ARRAY)
1655 continue;
1656 if (!tmp->ident)
1657 continue;
1659 assign = assign_expression(member, '=', zero_expr());
1660 __split_expr(assign);
1661 } END_FOR_EACH_PTR(tmp);
1664 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1666 struct expression *deref, *assign, *tmp, *right;
1667 struct symbol *struct_type, *type;
1668 struct ident *member;
1669 int member_idx;
1671 struct_type = get_type(symbol);
1672 if (!struct_type ||
1673 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1674 return;
1677 * We're parsing an initializer that could look something like this:
1678 * struct foo foo = {
1679 * 42,
1680 * .whatever.xxx = 11,
1681 * .zzz = 12,
1682 * };
1684 * So what we have here is a list with 42, .whatever, and .zzz. We need
1685 * to break it up into left and right sides of the assignments.
1688 member_idx = 0;
1689 FOR_EACH_PTR(members, tmp) {
1690 deref = NULL;
1691 if (tmp->type == EXPR_IDENTIFIER) {
1692 member_idx = member_to_number(symbol, tmp->expr_ident);
1693 while (tmp->type == EXPR_IDENTIFIER) {
1694 member = tmp->expr_ident;
1695 tmp = tmp->ident_expression;
1696 if (deref)
1697 deref = member_expression(deref, '.', member);
1698 else
1699 deref = member_expression(symbol, '.', member);
1701 } else {
1702 member = number_to_member(symbol, member_idx);
1703 deref = member_expression(symbol, '.', member);
1705 right = tmp;
1706 member_idx++;
1707 if (right->type == EXPR_INITIALIZER) {
1708 type = get_type(deref);
1709 if (type && type->type == SYM_ARRAY)
1710 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1711 else
1712 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1713 } else {
1714 assign = assign_expression(deref, '=', right);
1715 fake_cb(assign);
1717 } END_FOR_EACH_PTR(tmp);
1719 set_unset_to_zero(struct_type, symbol);
1722 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1724 fake_member_assigns_helper(symbol_expression(sym),
1725 sym->initializer->expr_list, fake_cb);
1728 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1730 struct expression *offset, *binop, *assign, *tmp;
1731 struct symbol *type;
1732 int idx, max;
1734 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1735 return;
1737 max = 0;
1738 idx = 0;
1739 FOR_EACH_PTR(expr_list, tmp) {
1740 if (tmp->type == EXPR_INDEX) {
1741 if (tmp->idx_from != tmp->idx_to)
1742 return;
1743 idx = tmp->idx_from;
1744 if (idx > max)
1745 max = idx;
1746 if (!tmp->idx_expression)
1747 goto next;
1748 tmp = tmp->idx_expression;
1750 offset = value_expr(idx);
1751 binop = array_element_expression(array, offset);
1752 if (tmp->type == EXPR_INITIALIZER) {
1753 type = get_type(binop);
1754 if (type && type->type == SYM_ARRAY)
1755 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1756 else
1757 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1758 } else {
1759 assign = assign_expression(binop, '=', tmp);
1760 fake_cb(assign);
1762 next:
1763 idx++;
1764 if (idx > max)
1765 max = idx;
1766 } END_FOR_EACH_PTR(tmp);
1768 __call_array_initialized_hooks(array, max);
1771 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1773 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1776 static void fake_assign_expr(struct symbol *sym)
1778 struct expression *assign, *symbol;
1780 symbol = symbol_expression(sym);
1781 assign = assign_expression(symbol, '=', sym->initializer);
1782 __split_expr(assign);
1785 static void do_initializer_stuff(struct symbol *sym)
1787 if (!sym->initializer)
1788 return;
1790 if (sym->initializer->type == EXPR_INITIALIZER) {
1791 if (get_real_base_type(sym)->type == SYM_ARRAY)
1792 fake_element_assigns(sym, __split_expr);
1793 else
1794 fake_member_assigns(sym, __split_expr);
1795 } else {
1796 fake_assign_expr(sym);
1800 static void split_declaration(struct symbol_list *sym_list)
1802 struct symbol *sym;
1804 FOR_EACH_PTR(sym_list, sym) {
1805 __pass_to_client(sym, DECLARATION_HOOK);
1806 do_initializer_stuff(sym);
1807 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
1808 split_sym(sym);
1809 } END_FOR_EACH_PTR(sym);
1812 static void call_global_assign_hooks(struct expression *assign)
1814 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1817 static void fake_global_assign(struct symbol *sym)
1819 struct expression *assign, *symbol;
1821 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1822 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1823 fake_element_assigns(sym, call_global_assign_hooks);
1824 } else if (sym->initializer) {
1825 symbol = symbol_expression(sym);
1826 assign = assign_expression(symbol, '=', sym->initializer);
1827 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1828 } else {
1829 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1831 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1832 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1833 fake_member_assigns(sym, call_global_assign_hooks);
1834 } else if (sym->initializer) {
1835 symbol = symbol_expression(sym);
1836 assign = assign_expression(symbol, '=', sym->initializer);
1837 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1838 } else {
1839 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1841 } else {
1842 symbol = symbol_expression(sym);
1843 if (sym->initializer) {
1844 assign = assign_expression(symbol, '=', sym->initializer);
1845 __split_expr(assign);
1846 } else {
1847 assign = assign_expression(symbol, '=', zero_expr());
1849 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1853 static void start_function_definition(struct symbol *sym)
1855 __in_function_def = 1;
1856 __pass_to_client(sym, FUNC_DEF_HOOK);
1857 __in_function_def = 0;
1858 __pass_to_client(sym, AFTER_DEF_HOOK);
1862 void add_function_data(unsigned long *fn_data)
1864 __add_ptr_list(&fn_data_list, fn_data);
1867 static void clear_function_data(void)
1869 unsigned long *tmp;
1871 FOR_EACH_PTR(fn_data_list, tmp) {
1872 *tmp = 0;
1873 } END_FOR_EACH_PTR(tmp);
1876 static void record_func_time(void)
1878 struct timeval stop;
1879 int func_time;
1880 char buf[32];
1882 gettimeofday(&stop, NULL);
1883 func_time = stop.tv_sec - fn_start_time.tv_sec;
1884 snprintf(buf, sizeof(buf), "%d", func_time);
1885 sql_insert_return_implies(FUNC_TIME, 0, "", buf);
1886 if (option_time && func_time > 2) {
1887 final_pass++;
1888 sm_msg("func_time: %d", func_time);
1889 final_pass--;
1893 static void split_function(struct symbol *sym)
1895 struct symbol *base_type = get_base_type(sym);
1897 if (!base_type->stmt && !base_type->inline_stmt)
1898 return;
1900 gettimeofday(&outer_fn_start_time, NULL);
1901 gettimeofday(&fn_start_time, NULL);
1902 cur_func_sym = sym;
1903 if (sym->ident)
1904 cur_func = sym->ident->name;
1905 set_position(sym->pos);
1906 clear_function_data();
1907 loop_count = 0;
1908 last_goto_statement_handled = 0;
1909 sm_debug("new function: %s\n", cur_func);
1910 __stree_id = 0;
1911 if (option_two_passes) {
1912 __unnullify_path();
1913 loop_num = 0;
1914 final_pass = 0;
1915 start_function_definition(sym);
1916 __split_stmt(base_type->stmt);
1917 __split_stmt(base_type->inline_stmt);
1918 nullify_path();
1920 __unnullify_path();
1921 loop_num = 0;
1922 final_pass = 1;
1923 start_function_definition(sym);
1924 __split_stmt(base_type->stmt);
1925 __split_stmt(base_type->inline_stmt);
1926 if (!__path_is_null() &&
1927 cur_func_return_type() == &void_ctype &&
1928 !__bail_on_rest_of_function) {
1929 __pass_to_client(NULL, RETURN_HOOK);
1930 nullify_path();
1932 __pass_to_client(sym, END_FUNC_HOOK);
1933 if (need_delayed_scope_hooks())
1934 __call_scope_hooks();
1935 __pass_to_client(sym, AFTER_FUNC_HOOK);
1936 sym->parsed = true;
1938 clear_all_states();
1940 record_func_time();
1942 cur_func_sym = NULL;
1943 cur_func = NULL;
1944 free_data_info_allocs();
1945 free_expression_stack(&switch_expr_stack);
1946 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1947 __bail_on_rest_of_function = 0;
1950 static void save_flow_state(void)
1952 unsigned long *tmp;
1954 __add_ptr_list(&backup, INT_PTR(loop_num << 2));
1955 __add_ptr_list(&backup, INT_PTR(loop_count << 2));
1956 __add_ptr_list(&backup, INT_PTR(final_pass << 2));
1958 __add_ptr_list(&backup, big_statement_stack);
1959 __add_ptr_list(&backup, big_expression_stack);
1960 __add_ptr_list(&backup, big_condition_stack);
1961 __add_ptr_list(&backup, switch_expr_stack);
1963 __add_ptr_list(&backup, cur_func_sym);
1965 __add_ptr_list(&backup, __prev_stmt);
1966 __add_ptr_list(&backup, __cur_stmt);
1967 __add_ptr_list(&backup, __next_stmt);
1969 FOR_EACH_PTR(fn_data_list, tmp) {
1970 __add_ptr_list(&backup, (void *)*tmp);
1971 } END_FOR_EACH_PTR(tmp);
1974 static void *pop_backup(void)
1976 void *ret;
1978 ret = last_ptr_list(backup);
1979 delete_ptr_list_last(&backup);
1980 return ret;
1983 static void restore_flow_state(void)
1985 unsigned long *tmp;
1987 FOR_EACH_PTR_REVERSE(fn_data_list, tmp) {
1988 *tmp = (unsigned long)pop_backup();
1989 } END_FOR_EACH_PTR_REVERSE(tmp);
1991 __next_stmt = pop_backup();
1992 __cur_stmt = pop_backup();
1993 __prev_stmt = pop_backup();
1995 cur_func_sym = pop_backup();
1996 switch_expr_stack = pop_backup();
1997 big_condition_stack = pop_backup();
1998 big_expression_stack = pop_backup();
1999 big_statement_stack = pop_backup();
2000 final_pass = PTR_INT(pop_backup()) >> 2;
2001 loop_count = PTR_INT(pop_backup()) >> 2;
2002 loop_num = PTR_INT(pop_backup()) >> 2;
2005 static void parse_inline(struct expression *call)
2007 struct symbol *base_type;
2008 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
2009 struct timeval time_backup = fn_start_time;
2010 struct expression *orig_inline = __inline_fn;
2011 int orig_budget;
2013 if (out_of_memory() || taking_too_long())
2014 return;
2016 save_flow_state();
2018 __pass_to_client(call, INLINE_FN_START);
2019 final_pass = 0; /* don't print anything */
2020 __inline_fn = call;
2021 orig_budget = inline_budget;
2022 inline_budget = inline_budget - 5;
2024 base_type = get_base_type(call->fn->symbol);
2025 cur_func_sym = call->fn->symbol;
2026 if (call->fn->symbol->ident)
2027 cur_func = call->fn->symbol->ident->name;
2028 else
2029 cur_func = NULL;
2030 set_position(call->fn->symbol->pos);
2032 save_all_states();
2033 big_statement_stack = NULL;
2034 big_expression_stack = NULL;
2035 big_condition_stack = NULL;
2036 switch_expr_stack = NULL;
2038 sm_debug("inline function: %s\n", cur_func);
2039 __unnullify_path();
2040 clear_function_data();
2041 loop_num = 0;
2042 loop_count = 0;
2043 start_function_definition(call->fn->symbol);
2044 __split_stmt(base_type->stmt);
2045 __split_stmt(base_type->inline_stmt);
2046 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
2047 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
2048 call->fn->symbol->parsed = true;
2050 free_expression_stack(&switch_expr_stack);
2051 __free_ptr_list((struct ptr_list **)&big_statement_stack);
2052 nullify_path();
2053 free_goto_stack();
2055 restore_flow_state();
2056 fn_start_time = time_backup;
2057 cur_func = cur_func_bak;
2059 restore_all_states();
2060 set_position(call->pos);
2061 __inline_fn = orig_inline;
2062 inline_budget = orig_budget;
2063 __pass_to_client(call, INLINE_FN_END);
2066 static struct symbol_list *inlines_called;
2067 static void add_inline_function(struct symbol *sym)
2069 static struct symbol_list *already_added;
2070 struct symbol *tmp;
2072 FOR_EACH_PTR(already_added, tmp) {
2073 if (tmp == sym)
2074 return;
2075 } END_FOR_EACH_PTR(tmp);
2077 add_ptr_list(&already_added, sym);
2078 add_ptr_list(&inlines_called, sym);
2081 static void process_inlines(void)
2083 struct symbol *tmp;
2085 FOR_EACH_PTR(inlines_called, tmp) {
2086 split_function(tmp);
2087 } END_FOR_EACH_PTR(tmp);
2088 free_ptr_list(&inlines_called);
2091 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
2093 struct symbol *sym;
2095 FOR_EACH_PTR_REVERSE(big_list, sym) {
2096 if (!sym->scope)
2097 continue;
2098 if (use_static && sym->ctype.modifiers & MOD_STATIC)
2099 return sym;
2100 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
2101 return sym;
2102 } END_FOR_EACH_PTR_REVERSE(sym);
2104 return NULL;
2107 static bool interesting_function(struct symbol *sym)
2109 static int prev_stream = -1;
2110 static bool prev_answer;
2111 const char *filename;
2112 int len;
2114 if (!(sym->ctype.modifiers & MOD_INLINE))
2115 return true;
2117 if (sym->pos.stream == prev_stream)
2118 return prev_answer;
2120 prev_stream = sym->pos.stream;
2121 prev_answer = false;
2123 filename = stream_name(sym->pos.stream);
2124 len = strlen(filename);
2125 if (len > 0 && filename[len - 1] == 'c')
2126 prev_answer = true;
2127 return prev_answer;
2130 static void split_inlines_in_scope(struct symbol *sym)
2132 struct symbol *base;
2133 struct symbol_list *scope_list;
2134 int stream;
2136 scope_list = sym->scope->symbols;
2137 stream = sym->pos.stream;
2139 /* find the last static symbol in the file */
2140 FOR_EACH_PTR_REVERSE(scope_list, sym) {
2141 if (sym->pos.stream != stream)
2142 continue;
2143 if (sym->type != SYM_NODE)
2144 continue;
2145 base = get_base_type(sym);
2146 if (!base)
2147 continue;
2148 if (base->type != SYM_FN)
2149 continue;
2150 if (!base->inline_stmt)
2151 continue;
2152 if (!interesting_function(sym))
2153 continue;
2154 add_inline_function(sym);
2155 } END_FOR_EACH_PTR_REVERSE(sym);
2157 process_inlines();
2160 static void split_inlines(struct symbol_list *sym_list)
2162 struct symbol *sym;
2164 sym = get_last_scoped_symbol(sym_list, 0);
2165 if (sym)
2166 split_inlines_in_scope(sym);
2167 sym = get_last_scoped_symbol(sym_list, 1);
2168 if (sym)
2169 split_inlines_in_scope(sym);
2172 static struct stree *clone_estates_perm(struct stree *orig)
2174 struct stree *ret = NULL;
2175 struct sm_state *tmp;
2177 FOR_EACH_SM(orig, tmp) {
2178 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
2179 } END_FOR_EACH_SM(tmp);
2181 return ret;
2184 struct position last_pos;
2185 static void split_c_file_functions(struct symbol_list *sym_list)
2187 struct symbol *sym;
2189 __unnullify_path();
2190 FOR_EACH_PTR(sym_list, sym) {
2191 set_position(sym->pos);
2192 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
2193 __pass_to_client(sym, BASE_HOOK);
2194 fake_global_assign(sym);
2195 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
2197 } END_FOR_EACH_PTR(sym);
2198 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
2199 nullify_path();
2201 FOR_EACH_PTR(sym_list, sym) {
2202 set_position(sym->pos);
2203 last_pos = sym->pos;
2204 if (!interesting_function(sym))
2205 continue;
2206 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
2207 split_function(sym);
2208 process_inlines();
2210 last_pos = sym->pos;
2211 } END_FOR_EACH_PTR(sym);
2212 split_inlines(sym_list);
2213 __pass_to_client(sym_list, END_FILE_HOOK);
2216 static int final_before_fake;
2217 void init_fake_env(void)
2219 if (!in_fake_env)
2220 final_before_fake = final_pass;
2221 in_fake_env++;
2222 __push_fake_cur_stree();
2223 final_pass = 0;
2226 void end_fake_env(void)
2228 __free_fake_cur_stree();
2229 in_fake_env--;
2230 if (!in_fake_env)
2231 final_pass = final_before_fake;
2234 static void open_output_files(char *base_file)
2236 char buf[256];
2238 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
2239 sm_outfd = fopen(buf, "w");
2240 if (!sm_outfd)
2241 sm_fatal("Cannot open %s", buf);
2243 if (!option_info)
2244 return;
2246 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
2247 sql_outfd = fopen(buf, "w");
2248 if (!sql_outfd)
2249 sm_fatal("Error: Cannot open %s", buf);
2251 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
2252 caller_info_fd = fopen(buf, "w");
2253 if (!caller_info_fd)
2254 sm_fatal("Error: Cannot open %s", buf);
2257 void smatch(struct string_list *filelist)
2259 struct symbol_list *sym_list;
2260 struct timeval stop, start;
2261 char *path;
2262 int len;
2264 gettimeofday(&start, NULL);
2266 FOR_EACH_PTR_NOTAG(filelist, base_file) {
2267 path = getcwd(NULL, 0);
2268 free(full_base_file);
2269 if (path) {
2270 len = strlen(path) + 1 + strlen(base_file) + 1;
2271 full_base_file = malloc(len);
2272 snprintf(full_base_file, len, "%s/%s", path, base_file);
2273 } else {
2274 full_base_file = alloc_string(base_file);
2276 if (option_file_output)
2277 open_output_files(base_file);
2278 base_file_stream = input_stream_nr;
2279 sym_list = sparse_keep_tokens(base_file);
2280 split_c_file_functions(sym_list);
2281 } END_FOR_EACH_PTR_NOTAG(base_file);
2283 gettimeofday(&stop, NULL);
2285 set_position(last_pos);
2286 final_pass = 1;
2287 if (option_time)
2288 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
2289 if (option_mem)
2290 sm_msg("mem: %luKb", get_max_memory());