checking_for_null_instead_of_err_ptr: use smatch_kernel_err_ptr.c
[smatch/bkmgit.git] / smatch_flow.c
blob142fc141d509cbc94c430a616f5add0d7a64776f
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 __debug_skip;
34 int in_fake_env;
35 int final_pass;
36 int __inline_call;
37 struct expression *__inline_fn;
39 int __smatch_lineno = 0;
41 static char *base_file;
42 static const char *filename;
43 static char *pathname;
44 static char *full_filename;
45 static char *full_base_file;
46 static char *cur_func;
47 int base_file_stream;
48 static unsigned int loop_count;
49 static int last_goto_statement_handled;
50 int __expr_stmt_count;
51 int __in_function_def;
52 int __in_unmatched_hook;
53 static struct expression_list *switch_expr_stack = NULL;
54 static struct expression_list *post_op_stack = NULL;
56 static struct ptr_list *fn_data_list;
57 static struct ptr_list *backup;
59 struct expression_list *big_expression_stack;
60 struct statement_list *big_statement_stack;
61 struct statement *__prev_stmt;
62 struct statement *__cur_stmt;
63 struct statement *__next_stmt;
64 static struct expression_list *parsed_calls;
65 static int indent_cnt;
66 int __in_pre_condition = 0;
67 int __bail_on_rest_of_function = 0;
68 static struct timeval fn_start_time;
69 static struct timeval outer_fn_start_time;
70 char *get_function(void) { return cur_func; }
71 int get_lineno(void) { return __smatch_lineno; }
72 int inside_loop(void) { return !!loop_count; }
73 int definitely_inside_loop(void) { return !!(loop_count & ~0x08000000); }
74 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
75 int in_expression_statement(void) { return !!__expr_stmt_count; }
77 static void split_symlist(struct symbol_list *sym_list);
78 static void split_declaration(struct symbol_list *sym_list);
79 static void split_expr_list(struct expression_list *expr_list, struct expression *parent);
80 static void split_args(struct expression *expr);
81 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *call, struct expression *expr, int nr);
82 static void add_inline_function(struct symbol *sym);
83 static void parse_inline(struct expression *expr);
85 int option_assume_loops = 0;
86 int option_two_passes = 0;
87 struct symbol *cur_func_sym = NULL;
88 struct stree *global_states;
90 const unsigned long valid_ptr_min = 4096;
91 unsigned long valid_ptr_max = ULONG_MAX & ~(MTAG_OFFSET_MASK);
92 const sval_t valid_ptr_min_sval = {
93 .type = &ptr_ctype,
94 {.value = 4096},
96 sval_t valid_ptr_max_sval = {
97 .type = &ptr_ctype,
98 {.value = ULONG_MAX & ~(MTAG_OFFSET_MASK)},
100 struct range_list *valid_ptr_rl;
102 void alloc_valid_ptr_rl(void)
104 valid_ptr_max = sval_type_max(&ulong_ctype).value & ~(MTAG_OFFSET_MASK);
105 valid_ptr_max_sval.value = valid_ptr_max;
107 valid_ptr_rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
108 valid_ptr_rl = cast_rl(&ptr_ctype, valid_ptr_rl);
109 valid_ptr_rl = clone_rl_permanent(valid_ptr_rl);
112 int outside_of_function(void)
114 return cur_func_sym == NULL;
117 const char *get_filename(void)
119 if (option_info && option_full_path)
120 return full_base_file;
121 if (option_info)
122 return base_file;
123 if (option_full_path)
124 return full_filename;
125 return filename;
128 const char *get_base_file(void)
130 if (option_full_path)
131 return full_base_file;
132 return base_file;
135 unsigned long long get_file_id(void)
137 return str_to_llu_hash(get_filename());
140 unsigned long long get_base_file_id(void)
142 return str_to_llu_hash(get_base_file());
145 static void set_position(struct position pos)
147 int len;
148 static int prev_stream = -1;
150 if (in_fake_env)
151 return;
153 if (pos.stream == 0 && pos.line == 0)
154 return;
156 __smatch_lineno = pos.line;
158 if (pos.stream == prev_stream)
159 return;
161 filename = stream_name(pos.stream);
163 free(full_filename);
164 pathname = getcwd(NULL, 0);
165 if (pathname) {
166 len = strlen(pathname) + 1 + strlen(filename) + 1;
167 full_filename = malloc(len);
168 snprintf(full_filename, len, "%s/%s", pathname, filename);
169 } else {
170 full_filename = alloc_string(filename);
172 free(pathname);
175 int is_assigned_call(struct expression *expr)
177 struct expression *parent = expr_get_parent_expr(expr);
179 if (parent &&
180 parent->type == EXPR_ASSIGNMENT &&
181 parent->op == '=' &&
182 strip_expr(parent->right) == expr)
183 return 1;
185 return 0;
188 int is_fake_assigned_call(struct expression *expr)
190 struct expression *parent = expr_get_fake_parent_expr(expr);
192 if (parent &&
193 parent->type == EXPR_ASSIGNMENT &&
194 parent->op == '=' &&
195 strip_expr(parent->right) == expr)
196 return 1;
198 return 0;
201 static bool is_inline_func(struct expression *expr)
203 if (expr->type != EXPR_SYMBOL || !expr->symbol)
204 return false;
205 if (!expr->symbol->definition)
206 return false;
207 if (expr->symbol->definition->ctype.modifiers & MOD_INLINE)
208 return true;
210 return 0;
213 static int is_noreturn_func(struct expression *expr)
215 if (expr->type != EXPR_SYMBOL || !expr->symbol)
216 return 0;
219 * It's almost impossible for Smatch to handle __builtin_constant_p()
220 * the same way that GCC does so Smatch ends up making some functions
221 * as no return functions incorrectly.
224 if (option_project == PROJ_KERNEL && expr->symbol->ident &&
225 strstr(expr->symbol->ident->name, "__compiletime_assert"))
226 return 0;
228 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
229 return 1;
230 return 0;
233 static int save_func_time(void *_rl, int argc, char **argv, char **azColName)
235 unsigned long *rl = _rl;
237 *rl = strtoul(argv[0], NULL, 10);
238 return 0;
241 static int get_func_time(struct symbol *sym)
243 unsigned long time = 0;
245 run_sql(&save_func_time, &time,
246 "select key from return_implies where %s and type = %d;",
247 get_static_filter(sym), FUNC_TIME);
249 return time;
252 static int inline_budget = 20;
254 int inlinable(struct expression *expr)
256 struct symbol *sym;
257 struct statement *last_stmt = NULL;
259 if (__inline_fn) /* don't nest */
260 return 0;
262 if (expr->type != EXPR_SYMBOL || !expr->symbol)
263 return 0;
264 if (is_no_inline_function(expr->symbol->ident->name))
265 return 0;
266 sym = get_base_type(expr->symbol);
267 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
268 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
269 return 0;
270 if (sym->stmt->type != STMT_COMPOUND)
271 return 0;
272 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
274 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
275 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
276 return 0;
277 if (sym->inline_stmt->type != STMT_COMPOUND)
278 return 0;
279 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
282 if (!last_stmt)
283 return 0;
285 /* the magic numbers in this function are pulled out of my bum. */
286 if (last_stmt->pos.line > sym->pos.line + inline_budget)
287 return 0;
289 if (get_func_time(expr->symbol) >= 2)
290 return 0;
292 return 1;
295 void __process_post_op_stack(void)
297 struct expression *expr;
299 FOR_EACH_PTR(post_op_stack, expr) {
300 __pass_to_client(expr, OP_HOOK);
301 } END_FOR_EACH_PTR(expr);
303 __free_ptr_list((struct ptr_list **)&post_op_stack);
306 static int handle_comma_assigns(struct expression *expr)
308 struct expression *right;
309 struct expression *assign;
311 right = strip_expr(expr->right);
312 if (right->type != EXPR_COMMA)
313 return 0;
315 __split_expr(right->left);
316 __process_post_op_stack();
318 assign = assign_expression(expr->left, '=', right->right);
319 __split_expr(assign);
321 return 1;
324 /* This is to handle *p++ = foo; assignments */
325 static int handle_postop_assigns(struct expression *expr)
327 struct expression *left, *fake_left;
328 struct expression *assign;
330 left = strip_expr(expr->left);
331 if (left->type != EXPR_PREOP || left->op != '*')
332 return 0;
333 left = strip_expr(left->unop);
334 if (left->type != EXPR_POSTOP)
335 return 0;
337 fake_left = deref_expression(strip_expr(left->unop));
338 assign = assign_expression(fake_left, '=', expr->right);
340 __split_expr(assign);
341 __split_expr(expr->left);
343 return 1;
346 static int prev_expression_is_getting_address(struct expression *expr)
348 struct expression *parent;
350 do {
351 parent = expr_get_parent_expr(expr);
353 if (!parent)
354 return 0;
355 if (parent->type == EXPR_PREOP && parent->op == '&')
356 return 1;
357 if (parent->type == EXPR_PREOP && parent->op == '(')
358 goto next;
359 if (parent->type == EXPR_DEREF && parent->op == '.')
360 goto next;
361 /* Handle &foo->array[offset] */
362 if (parent->type == EXPR_BINOP && parent->op == '+') {
363 parent = expr_get_parent_expr(parent);
364 if (!parent)
365 return 0;
366 if (parent->type == EXPR_PREOP && parent->op == '*')
367 goto next;
370 return 0;
371 next:
372 expr = parent;
373 } while (1);
376 int __in_builtin_overflow_func;
377 static void handle_builtin_overflow_func(struct expression *expr)
379 struct expression *a, *b, *res, *assign;
380 int op;
382 if (sym_name_is("__builtin_add_overflow", expr->fn))
383 op = '+';
384 else if (sym_name_is("__builtin_sub_overflow", expr->fn))
385 op = '-';
386 else if (sym_name_is("__builtin_mul_overflow", expr->fn))
387 op = '*';
388 else
389 return;
391 a = get_argument_from_call_expr(expr->args, 0);
392 b = get_argument_from_call_expr(expr->args, 1);
393 res = get_argument_from_call_expr(expr->args, 2);
395 assign = assign_expression(deref_expression(res), '=', binop_expression(a, op, b));
397 __in_builtin_overflow_func++;
398 __split_expr(assign);
399 __in_builtin_overflow_func--;
402 static int handle__builtin_choose_expr(struct expression *expr)
404 struct expression *const_expr, *expr1, *expr2;
405 sval_t sval;
407 if (!sym_name_is("__builtin_choose_expr", expr->fn))
408 return 0;
410 const_expr = get_argument_from_call_expr(expr->args, 0);
411 expr1 = get_argument_from_call_expr(expr->args, 1);
412 expr2 = get_argument_from_call_expr(expr->args, 2);
414 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
415 return 0;
416 if (sval.value)
417 __split_expr(expr1);
418 else
419 __split_expr(expr2);
420 return 1;
423 static int handle__builtin_choose_expr_assigns(struct expression *expr)
425 struct expression *const_expr, *right, *expr1, *expr2, *fake;
426 sval_t sval;
428 right = strip_parens(expr->right);
429 if (right->type != EXPR_CALL)
430 return 0;
431 if (!sym_name_is("__builtin_choose_expr", right->fn))
432 return 0;
434 const_expr = get_argument_from_call_expr(right->args, 0);
435 expr1 = get_argument_from_call_expr(right->args, 1);
436 expr2 = get_argument_from_call_expr(right->args, 2);
438 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
439 return 0;
441 fake = assign_expression(expr->left, '=', sval.value ? expr1 : expr2);
442 __split_expr(fake);
443 return 1;
446 int is_condition_call(struct expression *expr)
448 struct expression *tmp;
450 FOR_EACH_PTR_REVERSE(big_condition_stack, tmp) {
451 if (expr == tmp || expr_get_parent_expr(expr) == tmp)
452 return 1;
453 if (tmp->pos.line < expr->pos.line)
454 return 0;
455 } END_FOR_EACH_PTR_REVERSE(tmp);
457 return 0;
460 static struct expression *expr_get_parent_no_parens(struct expression *expr)
462 do {
463 expr = expr_get_parent_expr(expr);
464 } while (expr &&
465 expr->type == EXPR_PREOP &&
466 expr->op == '(');
468 return expr;
471 static bool gen_fake_function_assign(struct expression *expr)
473 static struct expression *parsed;
474 struct expression *assign, *parent;
475 struct symbol *type;
476 char buf[64];
478 /* The rule is that every non-void function call has to be part of an
479 * assignment. TODO: Should we create a fake non-casted assignment
480 * for casted assignments? Also faked assigns for += assignments?
482 type = get_type(expr);
483 if (!type || type == &void_ctype)
484 return false;
486 parent = expr_get_parent_no_parens(expr);
487 if (parent && parent->type == EXPR_ASSIGNMENT)
488 return false;
490 parent = expr_get_fake_parent_expr(expr);
491 if (parent) {
492 struct expression *left = parent->left;
494 if (parent == parsed)
495 return false;
496 if (!left || left->type != EXPR_SYMBOL)
497 return false;
498 if (strncmp(left->symbol_name->name, "__fake_assign_", 14) != 0)
499 return false;
500 parsed = parent;
501 __split_expr(parent);
502 return true;
505 // TODO: faked_assign skipping conditions is a hack
506 if (is_condition_call(expr))
507 return false;
509 snprintf(buf, sizeof(buf), "__fake_assign_%p", expr);
510 assign = create_fake_assign(buf, get_type(expr), expr);
512 parsed = assign;
513 __split_expr(assign);
514 return true;
517 static void split_call(struct expression *expr)
519 if (gen_fake_function_assign(expr))
520 return;
522 expr_set_parent_expr(expr->fn, expr);
524 if (sym_name_is("__builtin_constant_p", expr->fn))
525 return;
526 if (handle__builtin_choose_expr(expr))
527 return;
528 __split_expr(expr->fn);
529 split_args(expr);
530 if (is_inline_func(expr->fn))
531 add_inline_function(expr->fn->symbol->definition);
532 if (inlinable(expr->fn))
533 __inline_call = 1;
534 __process_post_op_stack();
535 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
536 __pass_to_client(expr, FUNCTION_CALL_HOOK);
537 __inline_call = 0;
538 if (inlinable(expr->fn))
539 parse_inline(expr);
540 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
541 if (is_noreturn_func(expr->fn))
542 nullify_path();
543 if (!expr_get_parent_expr(expr) && indent_cnt == 1)
544 __discard_fake_states(expr);
545 handle_builtin_overflow_func(expr);
546 __add_ptr_list((struct ptr_list **)&parsed_calls, expr);
549 void parse_assignment(struct expression *expr)
551 struct expression *right;
553 expr_set_parent_expr(expr->left, expr);
554 expr_set_parent_expr(expr->right, expr);
556 right = strip_expr(expr->right);
557 if (!right)
558 return;
560 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
562 /* foo = !bar() */
563 if (__handle_condition_assigns(expr))
564 goto after_assign;
565 /* foo = (x < 5 ? foo : 5); */
566 if (__handle_select_assigns(expr))
567 goto after_assign;
568 /* foo = ({frob(); frob(); frob(); 1;}) */
569 if (__handle_expr_statement_assigns(expr))
570 return; // FIXME: got after
571 /* foo = (3, 4); */
572 if (handle_comma_assigns(expr))
573 goto after_assign;
574 if (handle__builtin_choose_expr_assigns(expr))
575 goto after_assign;
576 if (handle_postop_assigns(expr))
577 return; /* no need to goto after_assign */
579 __split_expr(expr->right);
580 if (outside_of_function())
581 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
582 else
583 __pass_to_client(expr, ASSIGNMENT_HOOK);
586 // FIXME: the ordering of this is tricky
587 __fake_struct_member_assignments(expr);
589 /* Re-examine ->right for inlines. See the commit message */
590 right = strip_expr(expr->right);
591 if (expr->op == '=' && right->type == EXPR_CALL)
592 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
594 after_assign:
595 if (get_macro_name(right->pos) &&
596 get_macro_name(expr->left->pos) != get_macro_name(right->pos))
597 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
599 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
600 __split_expr(expr->left);
603 static bool skip_split_off(struct expression *expr)
605 if (expr->type == EXPR_CALL &&
606 sym_name_is("__smatch_stop_skip", expr->fn))
607 return true;
608 return false;
611 void __split_expr(struct expression *expr)
613 if (!expr)
614 return;
616 if (skip_split_off(expr))
617 __debug_skip = 0;
618 if (__debug_skip)
619 return;
621 // if (local_debug)
622 // sm_msg("Debug expr_type %d %s expr = '%s'", expr->type, show_special(expr->op), expr_to_str(expr));
624 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
625 return;
626 if (__in_fake_assign >= 4) /* don't allow too much nesting */
627 return;
629 push_expression(&big_expression_stack, expr);
630 set_position(expr->pos);
631 __pass_to_client(expr, EXPR_HOOK);
633 switch (expr->type) {
634 case EXPR_PREOP:
635 expr_set_parent_expr(expr->unop, expr);
637 if (expr->op == '*' &&
638 !prev_expression_is_getting_address(expr))
639 __pass_to_client(expr, DEREF_HOOK);
640 __split_expr(expr->unop);
641 __pass_to_client(expr, OP_HOOK);
642 break;
643 case EXPR_POSTOP:
644 expr_set_parent_expr(expr->unop, expr);
646 __split_expr(expr->unop);
647 push_expression(&post_op_stack, expr);
648 break;
649 case EXPR_STATEMENT:
650 __expr_stmt_count++;
651 if (expr->statement && !expr->statement) {
652 stmt_set_parent_stmt(expr->statement,
653 last_ptr_list((struct ptr_list *)big_statement_stack));
655 __split_stmt(expr->statement);
656 __expr_stmt_count--;
657 break;
658 case EXPR_LOGICAL:
659 case EXPR_COMPARE:
660 expr_set_parent_expr(expr->left, expr);
661 expr_set_parent_expr(expr->right, expr);
663 __pass_to_client(expr, LOGIC_HOOK);
664 __handle_logic(expr);
665 break;
666 case EXPR_BINOP:
667 expr_set_parent_expr(expr->left, expr);
668 expr_set_parent_expr(expr->right, expr);
670 __pass_to_client(expr, BINOP_HOOK);
671 __split_expr(expr->left);
672 __split_expr(expr->right);
673 break;
674 case EXPR_COMMA:
675 expr_set_parent_expr(expr->left, expr);
676 expr_set_parent_expr(expr->right, expr);
678 __split_expr(expr->left);
679 __process_post_op_stack();
680 __split_expr(expr->right);
681 break;
682 case EXPR_ASSIGNMENT:
683 parse_assignment(expr);
684 break;
685 case EXPR_DEREF:
686 expr_set_parent_expr(expr->deref, expr);
688 __pass_to_client(expr, DEREF_HOOK);
689 __split_expr(expr->deref);
690 break;
691 case EXPR_SLICE:
692 expr_set_parent_expr(expr->base, expr);
694 __split_expr(expr->base);
695 break;
696 case EXPR_CAST:
697 case EXPR_FORCE_CAST:
698 expr_set_parent_expr(expr->cast_expression, expr);
700 __pass_to_client(expr, CAST_HOOK);
701 __split_expr(expr->cast_expression);
702 break;
703 case EXPR_SIZEOF:
704 if (expr->cast_expression)
705 __pass_to_client(strip_parens(expr->cast_expression),
706 SIZEOF_HOOK);
707 break;
708 case EXPR_OFFSETOF:
709 case EXPR_ALIGNOF:
710 break;
711 case EXPR_CONDITIONAL:
712 case EXPR_SELECT:
713 expr_set_parent_expr(expr->conditional, expr);
714 expr_set_parent_expr(expr->cond_true, expr);
715 expr_set_parent_expr(expr->cond_false, expr);
717 if (known_condition_true(expr->conditional)) {
718 __split_expr(expr->cond_true);
719 break;
721 if (known_condition_false(expr->conditional)) {
722 __split_expr(expr->cond_false);
723 break;
725 __pass_to_client(expr, SELECT_HOOK);
726 __split_whole_condition(expr->conditional);
727 __split_expr(expr->cond_true);
728 __push_true_states();
729 __use_false_states();
730 __split_expr(expr->cond_false);
731 __merge_true_states();
732 break;
733 case EXPR_CALL:
734 split_call(expr);
735 break;
736 case EXPR_INITIALIZER:
737 split_expr_list(expr->expr_list, expr);
738 break;
739 case EXPR_IDENTIFIER:
740 expr_set_parent_expr(expr->ident_expression, expr);
741 __split_expr(expr->ident_expression);
742 break;
743 case EXPR_INDEX:
744 expr_set_parent_expr(expr->idx_expression, expr);
745 __split_expr(expr->idx_expression);
746 break;
747 case EXPR_POS:
748 expr_set_parent_expr(expr->init_expr, expr);
749 __split_expr(expr->init_expr);
750 break;
751 case EXPR_SYMBOL:
752 __pass_to_client(expr, SYM_HOOK);
753 break;
754 case EXPR_STRING:
755 __pass_to_client(expr, STRING_HOOK);
756 break;
757 case EXPR_GENERIC: {
758 struct expression *tmp;
760 tmp = strip_Generic(expr);
761 if (tmp != expr)
762 __split_expr(tmp);
763 break;
765 default:
766 break;
768 __pass_to_client(expr, EXPR_HOOK_AFTER);
769 pop_expression(&big_expression_stack);
772 static int is_forever_loop(struct statement *stmt)
774 struct expression *expr;
775 sval_t sval;
777 expr = strip_expr(stmt->iterator_pre_condition);
778 if (!expr)
779 expr = stmt->iterator_post_condition;
780 if (!expr) {
781 /* this is a for(;;) loop... */
782 return 1;
785 if (get_value(expr, &sval) && sval.value != 0)
786 return 1;
788 return 0;
791 static int loop_num;
792 static char *get_loop_name(int num)
794 char buf[256];
796 snprintf(buf, 255, "-loop%d", num);
797 buf[255] = '\0';
798 return alloc_sname(buf);
802 * Pre Loops are while and for loops.
804 static void handle_pre_loop(struct statement *stmt)
806 int once_through; /* we go through the loop at least once */
807 struct sm_state *extra_sm = NULL;
808 int unchanged = 0;
809 char *loop_name;
810 struct stree *stree = NULL;
811 struct sm_state *sm = NULL;
813 loop_name = get_loop_name(loop_num);
814 loop_num++;
816 if (stmt->iterator_pre_statement) {
817 __split_stmt(stmt->iterator_pre_statement);
818 __prev_stmt = stmt->iterator_pre_statement;
821 once_through = implied_condition_true(stmt->iterator_pre_condition);
823 loop_count++;
824 __push_continues();
825 __push_breaks();
827 __merge_gotos(loop_name, NULL);
829 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
830 __in_pre_condition++;
831 __pass_to_client(stmt, PRELOOP_HOOK);
832 __split_whole_condition(stmt->iterator_pre_condition);
833 __in_pre_condition--;
834 FOR_EACH_SM(stree, sm) {
835 set_state(sm->owner, sm->name, sm->sym, sm->state);
836 } END_FOR_EACH_SM(sm);
837 free_stree(&stree);
838 if (extra_sm)
839 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
841 if (option_assume_loops)
842 once_through = 1;
844 __split_stmt(stmt->iterator_statement);
845 if (is_forever_loop(stmt)) {
846 __merge_continues();
847 __save_gotos(loop_name, NULL);
849 __push_fake_cur_stree();
850 __split_stmt(stmt->iterator_post_statement);
851 stree = __pop_fake_cur_stree();
853 __discard_false_states();
854 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
855 __use_breaks();
857 if (!__path_is_null())
858 __merge_stree_into_cur(stree);
859 free_stree(&stree);
860 } else {
861 __merge_continues();
862 unchanged = __iterator_unchanged(extra_sm);
863 __split_stmt(stmt->iterator_post_statement);
864 __prev_stmt = stmt->iterator_post_statement;
865 __cur_stmt = stmt;
867 __save_gotos(loop_name, NULL);
868 __in_pre_condition++;
869 __split_whole_condition(stmt->iterator_pre_condition);
870 __in_pre_condition--;
871 nullify_path();
872 __merge_false_states();
873 if (once_through)
874 __discard_false_states();
875 else
876 __merge_false_states();
878 if (extra_sm && unchanged)
879 __extra_pre_loop_hook_after(extra_sm,
880 stmt->iterator_post_statement,
881 stmt->iterator_pre_condition);
882 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
883 __merge_breaks();
885 loop_count--;
889 * Post loops are do {} while();
891 static void handle_post_loop(struct statement *stmt)
893 char *loop_name;
895 loop_name = get_loop_name(loop_num);
896 loop_num++;
897 loop_count++;
899 __pass_to_client(stmt, POSTLOOP_HOOK);
901 __push_continues();
902 __push_breaks();
903 __merge_gotos(loop_name, NULL);
904 __split_stmt(stmt->iterator_statement);
905 __merge_continues();
906 if (!expr_is_zero(stmt->iterator_post_condition))
907 __save_gotos(loop_name, NULL);
909 if (is_forever_loop(stmt)) {
910 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
911 __use_breaks();
912 } else {
913 __split_whole_condition(stmt->iterator_post_condition);
914 __use_false_states();
915 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
916 __merge_breaks();
918 loop_count--;
921 static int empty_statement(struct statement *stmt)
923 if (!stmt)
924 return 0;
925 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
926 return 1;
927 return 0;
930 static int last_stmt_on_same_line(void)
932 struct statement *stmt;
933 int i = 0;
935 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
936 if (!i++)
937 continue;
938 if (stmt->pos.line == get_lineno())
939 return 1;
940 return 0;
941 } END_FOR_EACH_PTR_REVERSE(stmt);
942 return 0;
945 static void split_asm_ops(struct asm_operand_list *ops)
947 struct asm_operand *op;
949 FOR_EACH_PTR(ops, op) {
950 __split_expr(op->expr);
951 } END_FOR_EACH_PTR(op);
954 static int is_case_val(struct statement *stmt, sval_t sval)
956 sval_t case_sval;
958 if (stmt->type != STMT_CASE)
959 return 0;
960 if (!stmt->case_expression) {
961 __set_default();
962 return 1;
964 if (!get_value(stmt->case_expression, &case_sval))
965 return 0;
966 if (case_sval.value == sval.value)
967 return 1;
968 return 0;
971 static struct range_list *get_case_rl(struct expression *switch_expr,
972 struct expression *case_expr,
973 struct expression *case_to)
975 sval_t start, end;
976 struct range_list *rl = NULL;
977 struct symbol *switch_type;
979 switch_type = get_type(switch_expr);
980 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
981 start = sval_cast(switch_type, start);
982 end = sval_cast(switch_type, end);
983 add_range(&rl, start, end);
984 } else if (get_value(case_expr, &start)) {
985 start = sval_cast(switch_type, start);
986 add_range(&rl, start, start);
989 return rl;
992 static void split_known_switch(struct statement *stmt, sval_t sval)
994 struct statement *tmp;
995 struct range_list *rl;
997 __split_expr(stmt->switch_expression);
998 sval = sval_cast(get_type(stmt->switch_expression), sval);
1000 push_expression(&switch_expr_stack, stmt->switch_expression);
1001 __save_switch_states(top_expression(switch_expr_stack));
1002 nullify_path();
1003 __push_default();
1004 __push_breaks();
1006 stmt = stmt->switch_statement;
1008 __push_scope_hooks();
1009 FOR_EACH_PTR(stmt->stmts, tmp) {
1010 __smatch_lineno = tmp->pos.line;
1011 // FIXME: what if default comes before the known case statement?
1012 if (is_case_val(tmp, sval)) {
1013 rl = alloc_rl(sval, sval);
1014 __merge_switches(top_expression(switch_expr_stack), rl);
1015 __pass_case_to_client(top_expression(switch_expr_stack), rl);
1016 stmt_set_parent_stmt(tmp->case_statement, tmp);
1017 __split_stmt(tmp->case_statement);
1018 goto next;
1020 if (__path_is_null())
1021 continue;
1022 __split_stmt(tmp);
1023 next:
1024 if (__path_is_null()) {
1025 __set_default();
1026 goto out;
1028 } END_FOR_EACH_PTR(tmp);
1029 out:
1030 __call_scope_hooks();
1031 if (!__pop_default())
1032 __merge_switches(top_expression(switch_expr_stack), NULL);
1033 __discard_switches();
1034 __merge_breaks();
1035 pop_expression(&switch_expr_stack);
1038 static void split_case(struct statement *stmt)
1040 struct range_list *rl = NULL;
1042 expr_set_parent_stmt(stmt->case_expression, stmt);
1043 expr_set_parent_stmt(stmt->case_to, stmt);
1045 rl = get_case_rl(top_expression(switch_expr_stack),
1046 stmt->case_expression, stmt->case_to);
1047 while (stmt->case_statement->type == STMT_CASE) {
1048 struct range_list *tmp;
1050 tmp = get_case_rl(top_expression(switch_expr_stack),
1051 stmt->case_statement->case_expression,
1052 stmt->case_statement->case_to);
1053 if (!tmp)
1054 goto next;
1055 rl = rl_union(rl, tmp);
1056 if (!stmt->case_expression)
1057 __set_default();
1058 next:
1059 stmt = stmt->case_statement;
1062 __merge_switches(top_expression(switch_expr_stack), rl);
1064 if (!stmt->case_expression)
1065 __set_default();
1067 stmt_set_parent_stmt(stmt->case_statement, stmt);
1068 __split_stmt(stmt->case_statement);
1071 int time_parsing_function(void)
1073 return ms_since(&fn_start_time) / 1000;
1076 bool taking_too_long(void)
1078 if ((ms_since(&outer_fn_start_time) / 1000) > 60 * 5) /* five minutes */
1079 return 1;
1080 return 0;
1083 struct statement *get_last_stmt(void)
1085 struct symbol *fn;
1086 struct statement *stmt;
1088 fn = get_base_type(cur_func_sym);
1089 if (!fn)
1090 return NULL;
1091 stmt = fn->stmt;
1092 if (!stmt)
1093 stmt = fn->inline_stmt;
1094 if (!stmt || stmt->type != STMT_COMPOUND)
1095 return NULL;
1096 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1097 if (stmt && stmt->type == STMT_LABEL)
1098 stmt = stmt->label_statement;
1099 return stmt;
1102 int is_last_stmt(struct statement *cur_stmt)
1104 struct statement *last;
1106 last = get_last_stmt();
1107 if (last && last == cur_stmt)
1108 return 1;
1109 return 0;
1112 static void handle_backward_goto(struct statement *goto_stmt)
1114 const char *goto_name, *label_name;
1115 struct statement *func_stmt;
1116 struct symbol *base_type = get_base_type(cur_func_sym);
1117 struct statement *tmp;
1118 int found = 0;
1120 if (!option_info)
1121 return;
1122 if (last_goto_statement_handled)
1123 return;
1124 last_goto_statement_handled = 1;
1126 if (!goto_stmt->goto_label ||
1127 goto_stmt->goto_label->type != SYM_LABEL ||
1128 !goto_stmt->goto_label->ident)
1129 return;
1130 goto_name = goto_stmt->goto_label->ident->name;
1132 func_stmt = base_type->stmt;
1133 if (!func_stmt)
1134 func_stmt = base_type->inline_stmt;
1135 if (!func_stmt)
1136 return;
1137 if (func_stmt->type != STMT_COMPOUND)
1138 return;
1140 FOR_EACH_PTR(func_stmt->stmts, tmp) {
1141 if (!found) {
1142 if (tmp->type != STMT_LABEL)
1143 continue;
1144 if (!tmp->label_identifier ||
1145 tmp->label_identifier->type != SYM_LABEL ||
1146 !tmp->label_identifier->ident)
1147 continue;
1148 label_name = tmp->label_identifier->ident->name;
1149 if (strcmp(goto_name, label_name) != 0)
1150 continue;
1151 found = 1;
1153 __split_stmt(tmp);
1154 } END_FOR_EACH_PTR(tmp);
1157 static void fake_a_return(void)
1159 struct expression *ret = NULL;
1161 nullify_path();
1162 __unnullify_path();
1164 if (cur_func_return_type() != &void_ctype)
1165 ret = unknown_value_expression(NULL);
1167 __pass_to_client(ret, RETURN_HOOK);
1168 nullify_path();
1171 static void split_ret_value(struct expression *expr)
1173 struct symbol *type;
1175 if (!expr)
1176 return;
1178 type = get_real_base_type(cur_func_sym);
1179 type = get_real_base_type(type);
1180 expr = fake_a_variable_assign(type, NULL, expr, -1);
1182 __in_fake_var_assign++;
1183 __split_expr(expr);
1184 __in_fake_var_assign--;
1187 static void fake_an_empty_default(struct position pos)
1189 static struct statement none = {};
1191 none.pos = pos;
1192 none.type = STMT_NONE;
1193 __merge_switches(top_expression(switch_expr_stack), NULL);
1194 __split_stmt(&none);
1197 static void split_compound(struct statement *stmt)
1199 struct statement *prev = NULL;
1200 struct statement *cur = NULL;
1201 struct statement *next;
1203 __push_scope_hooks();
1205 FOR_EACH_PTR(stmt->stmts, next) {
1206 /* just set them all ahead of time */
1207 stmt_set_parent_stmt(next, stmt);
1209 if (cur) {
1210 __prev_stmt = prev;
1211 __next_stmt = next;
1212 __cur_stmt = cur;
1213 __split_stmt(cur);
1215 prev = cur;
1216 cur = next;
1217 } END_FOR_EACH_PTR(next);
1218 if (cur) {
1219 __prev_stmt = prev;
1220 __cur_stmt = cur;
1221 __next_stmt = NULL;
1222 __split_stmt(cur);
1226 * For function scope, then delay calling the scope hooks until the
1227 * end of function hooks can run. I'm not positive this is the right
1228 * thing...
1230 if (!is_last_stmt(cur))
1231 __call_scope_hooks();
1235 * This is a hack, work around for detecting empty functions.
1237 static int need_delayed_scope_hooks(void)
1239 struct symbol *fn = get_base_type(cur_func_sym);
1240 struct statement *stmt;
1242 if (!fn)
1243 return 0;
1244 stmt = fn->stmt;
1245 if (!stmt)
1246 stmt = fn->inline_stmt;
1247 if (stmt && stmt->type == STMT_COMPOUND)
1248 return 1;
1249 return 0;
1252 void __split_label_stmt(struct statement *stmt)
1254 if (stmt->label_identifier &&
1255 stmt->label_identifier->type == SYM_LABEL &&
1256 stmt->label_identifier->ident) {
1257 loop_count |= 0x0800000;
1258 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
1262 static void find_asm_gotos(struct statement *stmt)
1264 struct symbol *sym;
1266 FOR_EACH_PTR(stmt->asm_labels, sym) {
1267 __save_gotos(sym->ident->name, sym);
1268 } END_FOR_EACH_PTR(sym);
1271 static bool already_parsed_call(struct expression *call)
1273 struct expression *expr;
1275 FOR_EACH_PTR(parsed_calls, expr) {
1276 if (expr == call)
1277 return true;
1278 } END_FOR_EACH_PTR(expr);
1279 return false;
1282 static void free_parsed_call_stuff(bool free_fake_states)
1284 free_expression_stack(&parsed_calls);
1285 if (free_fake_states)
1286 __discard_fake_states(NULL);
1289 void __split_stmt(struct statement *stmt)
1291 sval_t sval;
1292 struct timeval start, stop;
1293 bool skip_after = false;
1295 gettimeofday(&start, NULL);
1297 if (!stmt)
1298 goto out;
1300 if (!__in_fake_assign)
1301 __silence_warnings_for_stmt = false;
1303 if (__bail_on_rest_of_function || is_skipped_function())
1304 return;
1306 if (out_of_memory() || taking_too_long()) {
1307 gettimeofday(&start, NULL);
1309 __bail_on_rest_of_function = 1;
1310 final_pass = 1;
1311 sm_perror("Function too hairy. Giving up. %lu seconds",
1312 start.tv_sec - fn_start_time.tv_sec);
1313 fake_a_return();
1314 final_pass = 0; /* turn off sm_msg() from here */
1315 return;
1318 indent_cnt++;
1320 add_ptr_list(&big_statement_stack, stmt);
1321 free_expression_stack(&big_expression_stack);
1322 free_parsed_call_stuff(indent_cnt == 1);
1323 set_position(stmt->pos);
1324 __pass_to_client(stmt, STMT_HOOK);
1326 switch (stmt->type) {
1327 case STMT_DECLARATION:
1328 split_declaration(stmt->declaration);
1329 break;
1330 case STMT_RETURN:
1331 expr_set_parent_stmt(stmt->ret_value, stmt);
1333 split_ret_value(stmt->ret_value);
1334 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1335 __process_post_op_stack();
1336 nullify_path();
1337 break;
1338 case STMT_EXPRESSION:
1339 expr_set_parent_stmt(stmt->expression, stmt);
1340 expr_set_parent_stmt(stmt->context, stmt);
1342 __split_expr(stmt->expression);
1343 break;
1344 case STMT_COMPOUND:
1345 split_compound(stmt);
1346 break;
1347 case STMT_IF:
1348 stmt_set_parent_stmt(stmt->if_true, stmt);
1349 stmt_set_parent_stmt(stmt->if_false, stmt);
1350 expr_set_parent_stmt(stmt->if_conditional, stmt);
1352 if (known_condition_true(stmt->if_conditional)) {
1353 __split_stmt(stmt->if_true);
1354 break;
1356 if (known_condition_false(stmt->if_conditional)) {
1357 __split_stmt(stmt->if_false);
1358 break;
1360 __split_whole_condition(stmt->if_conditional);
1361 __split_stmt(stmt->if_true);
1362 if (empty_statement(stmt->if_true) &&
1363 last_stmt_on_same_line() &&
1364 !get_macro_name(stmt->if_true->pos))
1365 sm_warning("if();");
1366 __push_true_states();
1367 __use_false_states();
1368 __split_stmt(stmt->if_false);
1369 __merge_true_states();
1370 break;
1371 case STMT_ITERATOR:
1372 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1373 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1374 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1375 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1376 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1378 if (stmt->iterator_pre_condition)
1379 handle_pre_loop(stmt);
1380 else if (stmt->iterator_post_condition)
1381 handle_post_loop(stmt);
1382 else {
1383 // these are for(;;) type loops.
1384 handle_pre_loop(stmt);
1386 break;
1387 case STMT_SWITCH:
1388 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1389 expr_set_parent_stmt(stmt->switch_expression, stmt);
1391 if (get_value(stmt->switch_expression, &sval)) {
1392 split_known_switch(stmt, sval);
1393 break;
1395 __split_expr(stmt->switch_expression);
1396 push_expression(&switch_expr_stack, stmt->switch_expression);
1397 __save_switch_states(top_expression(switch_expr_stack));
1398 nullify_path();
1399 __push_default();
1400 __push_breaks();
1401 __split_stmt(stmt->switch_statement);
1402 if (!__pop_default() && have_remaining_cases())
1403 fake_an_empty_default(stmt->pos);
1404 __discard_switches();
1405 __merge_breaks();
1406 pop_expression(&switch_expr_stack);
1407 break;
1408 case STMT_CASE:
1409 split_case(stmt);
1410 break;
1411 case STMT_LABEL:
1412 __split_label_stmt(stmt);
1413 __pass_to_client(stmt, STMT_HOOK_AFTER);
1414 skip_after = true;
1415 __split_stmt(stmt->label_statement);
1416 break;
1417 case STMT_GOTO:
1418 expr_set_parent_stmt(stmt->goto_expression, stmt);
1420 __split_expr(stmt->goto_expression);
1421 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1422 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1423 __process_breaks();
1424 } else if (!strcmp(stmt->goto_label->ident->name,
1425 "continue")) {
1426 __process_continues();
1428 } else if (stmt->goto_label &&
1429 stmt->goto_label->type == SYM_LABEL &&
1430 stmt->goto_label->ident) {
1431 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1433 nullify_path();
1434 if (is_last_stmt(stmt))
1435 handle_backward_goto(stmt);
1436 break;
1437 case STMT_NONE:
1438 break;
1439 case STMT_ASM:
1440 expr_set_parent_stmt(stmt->asm_string, stmt);
1442 find_asm_gotos(stmt);
1443 __pass_to_client(stmt, ASM_HOOK);
1444 __split_expr(stmt->asm_string);
1445 split_asm_ops(stmt->asm_outputs);
1446 split_asm_ops(stmt->asm_inputs);
1447 split_expr_list(stmt->asm_clobbers, NULL);
1448 break;
1449 case STMT_CONTEXT:
1450 break;
1451 case STMT_RANGE:
1452 __split_expr(stmt->range_expression);
1453 __split_expr(stmt->range_low);
1454 __split_expr(stmt->range_high);
1455 break;
1457 if (!skip_after)
1458 __pass_to_client(stmt, STMT_HOOK_AFTER);
1459 if (--indent_cnt == 1)
1460 free_parsed_call_stuff(true);
1462 out:
1463 __process_post_op_stack();
1465 gettimeofday(&stop, NULL);
1466 if (option_time_stmt && stmt)
1467 sm_msg("stmt_time%s: %ld",
1468 stmt->type == STMT_COMPOUND ? "_block" : "",
1469 stop.tv_sec - start.tv_sec);
1472 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1474 struct expression *expr;
1476 FOR_EACH_PTR(expr_list, expr) {
1477 expr_set_parent_expr(expr, parent);
1478 __split_expr(expr);
1479 __process_post_op_stack();
1480 } END_FOR_EACH_PTR(expr);
1483 static bool cast_arg(struct symbol *type, struct expression *arg)
1485 struct symbol *orig;
1487 if (!type)
1488 return false;
1490 arg = strip_parens(arg);
1491 if (arg != strip_expr(arg))
1492 return true;
1494 orig = get_type(arg);
1495 if (!orig)
1496 return true;
1497 if (types_equiv(orig, type))
1498 return false;
1500 if (orig->type == SYM_ARRAY && type->type == SYM_PTR)
1501 return true;
1504 * I would have expected that we could just do use (orig == type) but I
1505 * guess for pointers we need to get the basetype to do that comparison.
1509 if (orig->type != SYM_PTR ||
1510 type->type != SYM_PTR) {
1511 if (type_fits(type, orig))
1512 return false;
1513 return true;
1515 orig = get_real_base_type(orig);
1516 type = get_real_base_type(type);
1517 if (orig == type)
1518 return false;
1520 return true;
1523 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *call, struct expression *expr, int nr)
1525 char buf[64];
1526 bool cast;
1528 if (!expr || !cur_func_sym)
1529 return NULL;
1531 if (already_parsed_call(call))
1532 return NULL;
1534 if (expr->type == EXPR_ASSIGNMENT)
1535 return expr;
1537 /* for va_args then we don't know the type */
1538 if (!type)
1539 type = get_type(expr);
1541 cast = cast_arg(type, expr);
1543 * Using expr_to_sym() here is a hack. We want to say that we don't
1544 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1545 * It turns out faking these assignments is way more expensive than I
1546 * would have imagined. I'm not sure why exactly.
1549 if (!cast) {
1551 * if the code is "return *p;" where "p" is a user pointer then
1552 * we want to create a fake assignment so that it sets the state
1553 * in check_kernel_user_data.c.
1556 if (expr->type != EXPR_PREOP &&
1557 expr->op != '*' && expr->op != '&' &&
1558 expr_to_sym(expr))
1559 return expr;
1562 if (nr == -1)
1563 snprintf(buf, sizeof(buf), "__fake_return_%p", expr);
1564 else
1565 snprintf(buf, sizeof(buf), "__fake_param_%p_%d", call, nr);
1567 return create_fake_assign(buf, type, expr);
1570 static void split_args(struct expression *expr)
1572 struct expression *arg, *tmp;
1573 struct symbol *type;
1574 int i;
1576 i = -1;
1577 FOR_EACH_PTR(expr->args, arg) {
1578 i++;
1579 expr_set_parent_expr(arg, expr);
1580 type = get_arg_type(expr->fn, i);
1581 tmp = fake_a_variable_assign(type, expr, arg, i);
1582 if (tmp != arg)
1583 __in_fake_var_assign++;
1584 __split_expr(tmp);
1585 if (tmp != arg)
1586 __in_fake_var_assign--;
1587 __process_post_op_stack();
1588 } END_FOR_EACH_PTR(arg);
1591 static void split_sym(struct symbol *sym)
1593 if (!sym)
1594 return;
1595 if (!(sym->namespace & NS_SYMBOL))
1596 return;
1598 __split_stmt(sym->stmt);
1599 __split_expr(sym->array_size);
1600 split_symlist(sym->arguments);
1601 split_symlist(sym->symbol_list);
1602 __split_stmt(sym->inline_stmt);
1603 split_symlist(sym->inline_symbol_list);
1606 static void split_symlist(struct symbol_list *sym_list)
1608 struct symbol *sym;
1610 FOR_EACH_PTR(sym_list, sym) {
1611 split_sym(sym);
1612 } END_FOR_EACH_PTR(sym);
1615 typedef void (fake_cb)(struct expression *expr);
1617 static int member_to_number(struct expression *expr, struct ident *member)
1619 struct symbol *type, *tmp;
1620 char *name;
1621 int i;
1623 if (!member)
1624 return -1;
1625 name = member->name;
1627 type = get_type(expr);
1628 if (!type || type->type != SYM_STRUCT)
1629 return -1;
1631 i = -1;
1632 FOR_EACH_PTR(type->symbol_list, tmp) {
1633 i++;
1634 if (!tmp->ident)
1635 continue;
1636 if (strcmp(name, tmp->ident->name) == 0)
1637 return i;
1638 } END_FOR_EACH_PTR(tmp);
1639 return -1;
1642 static struct ident *number_to_member(struct expression *expr, int num)
1644 struct symbol *type, *member;
1645 int i = 0;
1647 type = get_type(expr);
1648 if (!type || type->type != SYM_STRUCT)
1649 return NULL;
1651 FOR_EACH_PTR(type->symbol_list, member) {
1652 if (i == num)
1653 return member->ident;
1654 i++;
1655 } END_FOR_EACH_PTR(member);
1656 return NULL;
1659 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1661 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1663 struct expression *edge_member, *assign;
1664 struct symbol *base = get_real_base_type(member);
1665 struct symbol *tmp;
1667 if (member->ident)
1668 expr = member_expression(expr, '.', member->ident);
1670 FOR_EACH_PTR(base->symbol_list, tmp) {
1671 struct symbol *type;
1673 type = get_real_base_type(tmp);
1674 if (!type)
1675 continue;
1677 edge_member = member_expression(expr, '.', tmp->ident);
1678 if (get_extra_state(edge_member))
1679 continue;
1681 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1682 set_inner_struct_members(expr, tmp);
1683 continue;
1686 if (!tmp->ident)
1687 continue;
1689 assign = assign_expression(edge_member, '=', zero_expr());
1690 __split_expr(assign);
1691 } END_FOR_EACH_PTR(tmp);
1696 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1698 struct symbol *tmp;
1699 struct expression *member = NULL;
1700 struct expression *assign;
1702 FOR_EACH_PTR(type->symbol_list, tmp) {
1703 type = get_real_base_type(tmp);
1704 if (!type)
1705 continue;
1707 if (tmp->ident) {
1708 member = member_expression(expr, '.', tmp->ident);
1709 if (get_extra_state(member))
1710 continue;
1713 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1714 set_inner_struct_members(expr, tmp);
1715 continue;
1717 if (type->type == SYM_ARRAY)
1718 continue;
1719 if (!tmp->ident)
1720 continue;
1722 assign = assign_expression(member, '=', zero_expr());
1723 __split_expr(assign);
1724 } END_FOR_EACH_PTR(tmp);
1727 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1729 struct expression *deref, *assign, *tmp, *right;
1730 struct symbol *struct_type, *type;
1731 struct ident *member;
1732 int member_idx;
1734 struct_type = get_type(symbol);
1735 if (!struct_type ||
1736 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1737 return;
1740 * We're parsing an initializer that could look something like this:
1741 * struct foo foo = {
1742 * 42,
1743 * .whatever.xxx = 11,
1744 * .zzz = 12,
1745 * };
1747 * So what we have here is a list with 42, .whatever, and .zzz. We need
1748 * to break it up into left and right sides of the assignments.
1751 member_idx = 0;
1752 FOR_EACH_PTR(members, tmp) {
1753 deref = NULL;
1754 if (tmp->type == EXPR_IDENTIFIER) {
1755 member_idx = member_to_number(symbol, tmp->expr_ident);
1756 while (tmp->type == EXPR_IDENTIFIER) {
1757 member = tmp->expr_ident;
1758 tmp = tmp->ident_expression;
1759 if (deref)
1760 deref = member_expression(deref, '.', member);
1761 else
1762 deref = member_expression(symbol, '.', member);
1764 } else {
1765 member = number_to_member(symbol, member_idx);
1766 deref = member_expression(symbol, '.', member);
1768 right = tmp;
1769 member_idx++;
1770 if (right->type == EXPR_INITIALIZER) {
1771 type = get_type(deref);
1772 if (type && type->type == SYM_ARRAY)
1773 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1774 else
1775 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1776 } else {
1777 assign = assign_expression(deref, '=', right);
1778 fake_cb(assign);
1780 } END_FOR_EACH_PTR(tmp);
1782 set_unset_to_zero(struct_type, symbol);
1785 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1787 fake_member_assigns_helper(symbol_expression(sym),
1788 sym->initializer->expr_list, fake_cb);
1791 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1793 struct expression *offset, *binop, *assign, *tmp;
1794 struct symbol *type;
1795 int idx, max;
1797 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1798 return;
1800 max = 0;
1801 idx = 0;
1802 FOR_EACH_PTR(expr_list, tmp) {
1803 if (tmp->type == EXPR_INDEX) {
1804 if (tmp->idx_from != tmp->idx_to)
1805 return;
1806 idx = tmp->idx_from;
1807 if (idx > max)
1808 max = idx;
1809 if (!tmp->idx_expression)
1810 goto next;
1811 tmp = tmp->idx_expression;
1813 offset = value_expr(idx);
1814 binop = array_element_expression(array, offset);
1815 if (tmp->type == EXPR_INITIALIZER) {
1816 type = get_type(binop);
1817 if (type && type->type == SYM_ARRAY)
1818 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1819 else
1820 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1821 } else {
1822 assign = assign_expression(binop, '=', tmp);
1823 fake_cb(assign);
1825 next:
1826 idx++;
1827 if (idx > max)
1828 max = idx;
1829 } END_FOR_EACH_PTR(tmp);
1831 __call_array_initialized_hooks(array, max);
1834 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1836 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1839 static void fake_assign_expr(struct symbol *sym)
1841 struct expression *assign, *symbol;
1843 symbol = symbol_expression(sym);
1844 assign = assign_expression(symbol, '=', sym->initializer);
1845 __split_expr(assign);
1848 static void do_initializer_stuff(struct symbol *sym)
1850 if (!sym->initializer)
1851 return;
1853 if (sym->initializer->type == EXPR_INITIALIZER) {
1854 if (get_real_base_type(sym)->type == SYM_ARRAY)
1855 fake_element_assigns(sym, __split_expr);
1856 else
1857 fake_member_assigns(sym, __split_expr);
1858 } else {
1859 fake_assign_expr(sym);
1863 static void split_declaration(struct symbol_list *sym_list)
1865 struct symbol *sym;
1867 FOR_EACH_PTR(sym_list, sym) {
1868 __pass_to_client(sym, DECLARATION_HOOK);
1869 do_initializer_stuff(sym);
1870 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
1871 split_sym(sym);
1872 } END_FOR_EACH_PTR(sym);
1875 static void call_global_assign_hooks(struct expression *assign)
1877 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1880 static void fake_global_assign(struct symbol *sym)
1882 struct expression *assign, *symbol;
1884 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1885 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1886 fake_element_assigns(sym, call_global_assign_hooks);
1887 } else if (sym->initializer) {
1888 symbol = symbol_expression(sym);
1889 assign = assign_expression(symbol, '=', sym->initializer);
1890 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1891 } else {
1892 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1894 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1895 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1896 fake_member_assigns(sym, call_global_assign_hooks);
1897 } else if (sym->initializer) {
1898 symbol = symbol_expression(sym);
1899 assign = assign_expression(symbol, '=', sym->initializer);
1900 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1901 } else {
1902 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1904 } else {
1905 symbol = symbol_expression(sym);
1906 if (sym->initializer) {
1907 assign = assign_expression(symbol, '=', sym->initializer);
1908 __split_expr(assign);
1909 } else {
1910 assign = assign_expression(symbol, '=', zero_expr());
1912 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1916 static void start_function_definition(struct symbol *sym)
1918 __in_function_def = 1;
1919 __pass_to_client(sym, FUNC_DEF_HOOK);
1920 __in_function_def = 0;
1921 __pass_to_client(sym, AFTER_DEF_HOOK);
1925 void add_function_data(unsigned long *fn_data)
1927 __add_ptr_list(&fn_data_list, fn_data);
1930 static void clear_function_data(void)
1932 unsigned long *tmp;
1934 FOR_EACH_PTR(fn_data_list, tmp) {
1935 *tmp = 0;
1936 } END_FOR_EACH_PTR(tmp);
1939 static void record_func_time(void)
1941 struct timeval stop;
1942 int func_time;
1943 char buf[32];
1945 gettimeofday(&stop, NULL);
1946 func_time = stop.tv_sec - fn_start_time.tv_sec;
1947 snprintf(buf, sizeof(buf), "%d", func_time);
1948 sql_insert_return_implies(FUNC_TIME, 0, "", buf);
1949 if (option_time && func_time > 2) {
1950 final_pass++;
1951 sm_msg("func_time: %d", func_time);
1952 final_pass--;
1956 static void split_function(struct symbol *sym)
1958 struct symbol *base_type = get_base_type(sym);
1960 if (!base_type->stmt && !base_type->inline_stmt)
1961 return;
1963 gettimeofday(&outer_fn_start_time, NULL);
1964 gettimeofday(&fn_start_time, NULL);
1965 cur_func_sym = sym;
1966 if (sym->ident)
1967 cur_func = sym->ident->name;
1968 if (option_process_function && cur_func &&
1969 strcmp(option_process_function, cur_func) != 0)
1970 return;
1971 set_position(sym->pos);
1972 clear_function_data();
1973 loop_count = 0;
1974 last_goto_statement_handled = 0;
1975 sm_debug("new function: %s\n", cur_func);
1976 __stree_id = 0;
1977 if (option_two_passes) {
1978 __unnullify_path();
1979 loop_num = 0;
1980 final_pass = 0;
1981 start_function_definition(sym);
1982 __split_stmt(base_type->stmt);
1983 __split_stmt(base_type->inline_stmt);
1984 nullify_path();
1986 __unnullify_path();
1987 loop_num = 0;
1988 final_pass = 1;
1989 start_function_definition(sym);
1990 __split_stmt(base_type->stmt);
1991 __split_stmt(base_type->inline_stmt);
1992 if (!__path_is_null() &&
1993 cur_func_return_type() == &void_ctype &&
1994 !__bail_on_rest_of_function) {
1995 __pass_to_client(NULL, RETURN_HOOK);
1996 nullify_path();
1998 __pass_to_client(sym, END_FUNC_HOOK);
1999 if (need_delayed_scope_hooks())
2000 __call_scope_hooks();
2001 __pass_to_client(sym, AFTER_FUNC_HOOK);
2002 sym->parsed = true;
2004 clear_all_states();
2006 record_func_time();
2008 cur_func_sym = NULL;
2009 cur_func = NULL;
2010 free_data_info_allocs();
2011 free_expression_stack(&switch_expr_stack);
2012 __free_ptr_list((struct ptr_list **)&big_statement_stack);
2013 __bail_on_rest_of_function = 0;
2016 static void save_flow_state(void)
2018 unsigned long *tmp;
2020 __add_ptr_list(&backup, INT_PTR(loop_num << 2));
2021 __add_ptr_list(&backup, INT_PTR(loop_count << 2));
2022 __add_ptr_list(&backup, INT_PTR(final_pass << 2));
2024 __add_ptr_list(&backup, big_statement_stack);
2025 __add_ptr_list(&backup, big_expression_stack);
2026 __add_ptr_list(&backup, big_condition_stack);
2027 __add_ptr_list(&backup, switch_expr_stack);
2029 __add_ptr_list(&backup, cur_func_sym);
2031 __add_ptr_list(&backup, parsed_calls);
2033 __add_ptr_list(&backup, __prev_stmt);
2034 __add_ptr_list(&backup, __cur_stmt);
2035 __add_ptr_list(&backup, __next_stmt);
2037 FOR_EACH_PTR(fn_data_list, tmp) {
2038 __add_ptr_list(&backup, (void *)*tmp);
2039 } END_FOR_EACH_PTR(tmp);
2042 static void *pop_backup(void)
2044 void *ret;
2046 ret = last_ptr_list(backup);
2047 delete_ptr_list_last(&backup);
2048 return ret;
2051 static void restore_flow_state(void)
2053 unsigned long *tmp;
2055 FOR_EACH_PTR_REVERSE(fn_data_list, tmp) {
2056 *tmp = (unsigned long)pop_backup();
2057 } END_FOR_EACH_PTR_REVERSE(tmp);
2059 __next_stmt = pop_backup();
2060 __cur_stmt = pop_backup();
2061 __prev_stmt = pop_backup();
2063 parsed_calls = pop_backup();
2065 cur_func_sym = pop_backup();
2066 switch_expr_stack = pop_backup();
2067 big_condition_stack = pop_backup();
2068 big_expression_stack = pop_backup();
2069 big_statement_stack = pop_backup();
2070 final_pass = PTR_INT(pop_backup()) >> 2;
2071 loop_count = PTR_INT(pop_backup()) >> 2;
2072 loop_num = PTR_INT(pop_backup()) >> 2;
2075 void parse_inline(struct expression *call)
2077 struct symbol *base_type;
2078 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
2079 struct timeval time_backup = fn_start_time;
2080 struct expression *orig_inline = __inline_fn;
2081 int orig_budget;
2083 if (out_of_memory() || taking_too_long())
2084 return;
2086 if (already_parsed_call(call))
2087 return;
2089 save_flow_state();
2091 __pass_to_client(call, INLINE_FN_START);
2092 final_pass = 0; /* don't print anything */
2093 __inline_fn = call;
2094 orig_budget = inline_budget;
2095 inline_budget = inline_budget - 5;
2097 base_type = get_base_type(call->fn->symbol);
2098 cur_func_sym = call->fn->symbol;
2099 if (call->fn->symbol->ident)
2100 cur_func = call->fn->symbol->ident->name;
2101 else
2102 cur_func = NULL;
2103 set_position(call->fn->symbol->pos);
2105 save_all_states();
2106 big_statement_stack = NULL;
2107 big_expression_stack = NULL;
2108 big_condition_stack = NULL;
2109 switch_expr_stack = NULL;
2110 parsed_calls = NULL;
2112 sm_debug("inline function: %s\n", cur_func);
2113 __unnullify_path();
2114 clear_function_data();
2115 loop_num = 0;
2116 loop_count = 0;
2117 start_function_definition(call->fn->symbol);
2118 __split_stmt(base_type->stmt);
2119 __split_stmt(base_type->inline_stmt);
2120 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
2121 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
2122 call->fn->symbol->parsed = true;
2124 free_expression_stack(&switch_expr_stack);
2125 __free_ptr_list((struct ptr_list **)&big_statement_stack);
2126 nullify_path();
2127 free_goto_stack();
2129 restore_flow_state();
2130 fn_start_time = time_backup;
2131 cur_func = cur_func_bak;
2133 restore_all_states();
2134 set_position(call->pos);
2135 __inline_fn = orig_inline;
2136 inline_budget = orig_budget;
2137 __pass_to_client(call, INLINE_FN_END);
2140 static struct symbol_list *inlines_called;
2141 static void add_inline_function(struct symbol *sym)
2143 static struct symbol_list *already_added;
2144 struct symbol *tmp;
2146 FOR_EACH_PTR(already_added, tmp) {
2147 if (tmp == sym)
2148 return;
2149 } END_FOR_EACH_PTR(tmp);
2151 add_ptr_list(&already_added, sym);
2152 add_ptr_list(&inlines_called, sym);
2155 static void process_inlines(void)
2157 struct symbol *tmp;
2159 FOR_EACH_PTR(inlines_called, tmp) {
2160 split_function(tmp);
2161 } END_FOR_EACH_PTR(tmp);
2162 free_ptr_list(&inlines_called);
2165 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
2167 struct symbol *sym;
2169 FOR_EACH_PTR_REVERSE(big_list, sym) {
2170 if (!sym->scope)
2171 continue;
2172 if (use_static && sym->ctype.modifiers & MOD_STATIC)
2173 return sym;
2174 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
2175 return sym;
2176 } END_FOR_EACH_PTR_REVERSE(sym);
2178 return NULL;
2181 static bool interesting_function(struct symbol *sym)
2183 static int prev_stream = -1;
2184 static bool prev_answer;
2185 const char *filename;
2186 int len;
2188 if (!(sym->ctype.modifiers & MOD_INLINE))
2189 return true;
2191 if (sym->pos.stream == prev_stream)
2192 return prev_answer;
2194 prev_stream = sym->pos.stream;
2195 prev_answer = false;
2197 filename = stream_name(sym->pos.stream);
2198 len = strlen(filename);
2199 if (len > 0 && filename[len - 1] == 'c')
2200 prev_answer = true;
2201 return prev_answer;
2204 static void split_inlines_in_scope(struct symbol *sym)
2206 struct symbol *base;
2207 struct symbol_list *scope_list;
2208 int stream;
2210 scope_list = sym->scope->symbols;
2211 stream = sym->pos.stream;
2213 /* find the last static symbol in the file */
2214 FOR_EACH_PTR_REVERSE(scope_list, sym) {
2215 if (sym->pos.stream != stream)
2216 continue;
2217 if (sym->type != SYM_NODE)
2218 continue;
2219 base = get_base_type(sym);
2220 if (!base)
2221 continue;
2222 if (base->type != SYM_FN)
2223 continue;
2224 if (!base->inline_stmt)
2225 continue;
2226 if (!interesting_function(sym))
2227 continue;
2228 add_inline_function(sym);
2229 } END_FOR_EACH_PTR_REVERSE(sym);
2231 process_inlines();
2234 static void split_inlines(struct symbol_list *sym_list)
2236 struct symbol *sym;
2238 sym = get_last_scoped_symbol(sym_list, 0);
2239 if (sym)
2240 split_inlines_in_scope(sym);
2241 sym = get_last_scoped_symbol(sym_list, 1);
2242 if (sym)
2243 split_inlines_in_scope(sym);
2246 static struct stree *clone_estates_perm(struct stree *orig)
2248 struct stree *ret = NULL;
2249 struct sm_state *tmp;
2251 FOR_EACH_SM(orig, tmp) {
2252 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
2253 } END_FOR_EACH_SM(tmp);
2255 return ret;
2258 struct position last_pos;
2259 static void split_c_file_functions(struct symbol_list *sym_list)
2261 struct symbol *sym;
2263 __unnullify_path();
2264 FOR_EACH_PTR(sym_list, sym) {
2265 set_position(sym->pos);
2266 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
2267 __pass_to_client(sym, BASE_HOOK);
2268 fake_global_assign(sym);
2269 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
2271 } END_FOR_EACH_PTR(sym);
2272 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
2273 nullify_path();
2275 FOR_EACH_PTR(sym_list, sym) {
2276 set_position(sym->pos);
2277 last_pos = sym->pos;
2278 if (!interesting_function(sym))
2279 continue;
2280 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
2281 split_function(sym);
2282 process_inlines();
2284 last_pos = sym->pos;
2285 } END_FOR_EACH_PTR(sym);
2286 split_inlines(sym_list);
2287 __pass_to_client(sym_list, END_FILE_HOOK);
2290 static int final_before_fake;
2291 void init_fake_env(void)
2293 if (!in_fake_env)
2294 final_before_fake = final_pass;
2295 in_fake_env++;
2296 __push_fake_cur_stree();
2297 final_pass = 0;
2300 void end_fake_env(void)
2302 __free_fake_cur_stree();
2303 in_fake_env--;
2304 if (!in_fake_env)
2305 final_pass = final_before_fake;
2308 static void open_output_files(char *base_file)
2310 char buf[256];
2312 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
2313 sm_outfd = fopen(buf, "w");
2314 if (!sm_outfd)
2315 sm_fatal("Cannot open %s", buf);
2317 if (!option_info)
2318 return;
2320 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
2321 sql_outfd = fopen(buf, "w");
2322 if (!sql_outfd)
2323 sm_fatal("Error: Cannot open %s", buf);
2325 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
2326 caller_info_fd = fopen(buf, "w");
2327 if (!caller_info_fd)
2328 sm_fatal("Error: Cannot open %s", buf);
2331 void smatch(struct string_list *filelist)
2333 struct symbol_list *sym_list;
2334 struct timeval stop, start;
2335 char *path;
2336 int len;
2338 gettimeofday(&start, NULL);
2340 FOR_EACH_PTR_NOTAG(filelist, base_file) {
2341 path = getcwd(NULL, 0);
2342 free(full_base_file);
2343 if (path) {
2344 len = strlen(path) + 1 + strlen(base_file) + 1;
2345 full_base_file = malloc(len);
2346 snprintf(full_base_file, len, "%s/%s", path, base_file);
2347 } else {
2348 full_base_file = alloc_string(base_file);
2350 if (option_file_output)
2351 open_output_files(base_file);
2352 base_file_stream = input_stream_nr;
2353 sym_list = sparse_keep_tokens(base_file);
2354 split_c_file_functions(sym_list);
2355 } END_FOR_EACH_PTR_NOTAG(base_file);
2357 gettimeofday(&stop, NULL);
2359 set_position(last_pos);
2360 final_pass = 1;
2361 if (option_time)
2362 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
2363 if (option_mem)
2364 sm_msg("mem: %luKb", get_max_memory());