extra: handle PARAM_SET for buffers better
[smatch.git] / smatch_flow.c
bloba5dff2d8d3b1ff0f38cbcdf4d5f53ec4dcdba0c9
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 bool gen_fake_function_assign(struct expression *expr)
444 static struct expression *parsed;
445 struct expression *assign, *parent;
446 struct symbol *type;
447 char buf[64];
449 /* The rule is that every non-void function call has to be part of an
450 * assignment. TODO: Should we create a fake non-casted assignment
451 * for casted assignments? Also faked assigns for += assignments?
453 type = get_type(expr);
454 if (!type || type == &void_ctype)
455 return false;
457 parent = expr_get_parent_expr(expr);
458 if (parent && parent->type == EXPR_ASSIGNMENT)
459 return false;
461 parent = expr_get_fake_parent_expr(expr);
462 if (parent) {
463 struct expression *left = parent->left;
465 if (parent == parsed)
466 return false;
467 if (!left || left->type != EXPR_SYMBOL)
468 return false;
469 if (strncmp(left->symbol_name->name, "__fake_assign_", 14) != 0)
470 return false;
471 parsed = parent;
472 __split_expr(parent);
473 return true;
476 // TODO: faked_assign skipping conditions is a hack
477 if (is_condition_call(expr))
478 return false;
480 snprintf(buf, sizeof(buf), "__fake_assign_%p", expr);
481 assign = create_fake_assign(buf, get_type(expr), expr);
483 parsed = assign;
484 __split_expr(assign);
485 return true;
488 static void split_call(struct expression *expr)
490 if (gen_fake_function_assign(expr))
491 return;
493 expr_set_parent_expr(expr->fn, expr);
495 if (sym_name_is("__builtin_constant_p", expr->fn))
496 return;
497 if (handle__builtin_choose_expr(expr))
498 return;
499 __split_expr(expr->fn);
500 split_args(expr);
501 if (is_inline_func(expr->fn))
502 add_inline_function(expr->fn->symbol->definition);
503 if (inlinable(expr->fn))
504 __inline_call = 1;
505 __process_post_op_stack();
506 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
507 __pass_to_client(expr, FUNCTION_CALL_HOOK);
508 __inline_call = 0;
509 if (inlinable(expr->fn))
510 parse_inline(expr);
511 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
512 if (is_noreturn_func(expr->fn))
513 nullify_path();
514 if (!expr_get_parent_expr(expr))
515 __discard_fake_states(expr);
516 handle_builtin_overflow_func(expr);
519 void __split_expr(struct expression *expr)
521 if (!expr)
522 return;
524 // if (local_debug)
525 // sm_msg("Debug expr_type %d %s expr = '%s'", expr->type, show_special(expr->op), expr_to_str(expr));
527 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
528 return;
529 if (__in_fake_assign >= 4) /* don't allow too much nesting */
530 return;
532 push_expression(&big_expression_stack, expr);
533 set_position(expr->pos);
534 __pass_to_client(expr, EXPR_HOOK);
536 switch (expr->type) {
537 case EXPR_PREOP:
538 expr_set_parent_expr(expr->unop, expr);
540 if (expr->op == '*' &&
541 !prev_expression_is_getting_address(expr))
542 __pass_to_client(expr, DEREF_HOOK);
543 __split_expr(expr->unop);
544 __pass_to_client(expr, OP_HOOK);
545 break;
546 case EXPR_POSTOP:
547 expr_set_parent_expr(expr->unop, expr);
549 __split_expr(expr->unop);
550 push_expression(&post_op_stack, expr);
551 break;
552 case EXPR_STATEMENT:
553 __expr_stmt_count++;
554 if (expr->statement && !expr->statement) {
555 stmt_set_parent_stmt(expr->statement,
556 last_ptr_list((struct ptr_list *)big_statement_stack));
558 __split_stmt(expr->statement);
559 __expr_stmt_count--;
560 break;
561 case EXPR_LOGICAL:
562 case EXPR_COMPARE:
563 expr_set_parent_expr(expr->left, expr);
564 expr_set_parent_expr(expr->right, expr);
566 __pass_to_client(expr, LOGIC_HOOK);
567 __handle_logic(expr);
568 break;
569 case EXPR_BINOP:
570 expr_set_parent_expr(expr->left, expr);
571 expr_set_parent_expr(expr->right, expr);
573 __pass_to_client(expr, BINOP_HOOK);
574 __split_expr(expr->left);
575 __split_expr(expr->right);
576 break;
577 case EXPR_COMMA:
578 expr_set_parent_expr(expr->left, expr);
579 expr_set_parent_expr(expr->right, expr);
581 __split_expr(expr->left);
582 __process_post_op_stack();
583 __split_expr(expr->right);
584 break;
585 case EXPR_ASSIGNMENT: {
586 struct expression *right;
588 expr_set_parent_expr(expr->left, expr);
589 expr_set_parent_expr(expr->right, expr);
591 right = strip_expr(expr->right);
592 if (!right)
593 break;
595 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
597 /* foo = !bar() */
598 if (__handle_condition_assigns(expr))
599 goto after_assign;
600 /* foo = (x < 5 ? foo : 5); */
601 if (__handle_select_assigns(expr))
602 goto after_assign;
603 /* foo = ({frob(); frob(); frob(); 1;}) */
604 if (__handle_expr_statement_assigns(expr))
605 break; // FIXME: got after
606 /* foo = (3, 4); */
607 if (handle_comma_assigns(expr))
608 goto after_assign;
609 if (handle__builtin_choose_expr_assigns(expr))
610 goto after_assign;
611 if (handle_postop_assigns(expr))
612 break; /* no need to goto after_assign */
614 __split_expr(expr->right);
615 if (outside_of_function())
616 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
617 else
618 __pass_to_client(expr, ASSIGNMENT_HOOK);
620 __fake_struct_member_assignments(expr);
622 /* Re-examine ->right for inlines. See the commit message */
623 right = strip_expr(expr->right);
624 if (expr->op == '=' && right->type == EXPR_CALL)
625 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
627 after_assign:
628 if (get_macro_name(right->pos) &&
629 get_macro_name(expr->pos) != get_macro_name(right->pos))
630 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
632 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
633 __split_expr(expr->left);
634 break;
636 case EXPR_DEREF:
637 expr_set_parent_expr(expr->deref, expr);
639 __pass_to_client(expr, DEREF_HOOK);
640 __split_expr(expr->deref);
641 break;
642 case EXPR_SLICE:
643 expr_set_parent_expr(expr->base, expr);
645 __split_expr(expr->base);
646 break;
647 case EXPR_CAST:
648 case EXPR_FORCE_CAST:
649 expr_set_parent_expr(expr->cast_expression, expr);
651 __pass_to_client(expr, CAST_HOOK);
652 __split_expr(expr->cast_expression);
653 break;
654 case EXPR_SIZEOF:
655 if (expr->cast_expression)
656 __pass_to_client(strip_parens(expr->cast_expression),
657 SIZEOF_HOOK);
658 break;
659 case EXPR_OFFSETOF:
660 case EXPR_ALIGNOF:
661 break;
662 case EXPR_CONDITIONAL:
663 case EXPR_SELECT:
664 expr_set_parent_expr(expr->conditional, expr);
665 expr_set_parent_expr(expr->cond_true, expr);
666 expr_set_parent_expr(expr->cond_false, expr);
668 if (known_condition_true(expr->conditional)) {
669 __split_expr(expr->cond_true);
670 break;
672 if (known_condition_false(expr->conditional)) {
673 __split_expr(expr->cond_false);
674 break;
676 __pass_to_client(expr, SELECT_HOOK);
677 __split_whole_condition(expr->conditional);
678 __split_expr(expr->cond_true);
679 __push_true_states();
680 __use_false_states();
681 __split_expr(expr->cond_false);
682 __merge_true_states();
683 break;
684 case EXPR_CALL:
685 split_call(expr);
686 break;
687 case EXPR_INITIALIZER:
688 split_expr_list(expr->expr_list, expr);
689 break;
690 case EXPR_IDENTIFIER:
691 expr_set_parent_expr(expr->ident_expression, expr);
692 __split_expr(expr->ident_expression);
693 break;
694 case EXPR_INDEX:
695 expr_set_parent_expr(expr->idx_expression, expr);
696 __split_expr(expr->idx_expression);
697 break;
698 case EXPR_POS:
699 expr_set_parent_expr(expr->init_expr, expr);
700 __split_expr(expr->init_expr);
701 break;
702 case EXPR_SYMBOL:
703 __pass_to_client(expr, SYM_HOOK);
704 break;
705 case EXPR_STRING:
706 __pass_to_client(expr, STRING_HOOK);
707 break;
708 case EXPR_GENERIC: {
709 struct expression *tmp;
711 tmp = strip_Generic(expr);
712 if (tmp != expr)
713 __split_expr(tmp);
714 break;
716 default:
717 break;
719 __pass_to_client(expr, EXPR_HOOK_AFTER);
720 pop_expression(&big_expression_stack);
723 static int is_forever_loop(struct statement *stmt)
725 struct expression *expr;
726 sval_t sval;
728 expr = strip_expr(stmt->iterator_pre_condition);
729 if (!expr)
730 expr = stmt->iterator_post_condition;
731 if (!expr) {
732 /* this is a for(;;) loop... */
733 return 1;
736 if (get_value(expr, &sval) && sval.value != 0)
737 return 1;
739 return 0;
742 static int loop_num;
743 static char *get_loop_name(int num)
745 char buf[256];
747 snprintf(buf, 255, "-loop%d", num);
748 buf[255] = '\0';
749 return alloc_sname(buf);
753 * Pre Loops are while and for loops.
755 static void handle_pre_loop(struct statement *stmt)
757 int once_through; /* we go through the loop at least once */
758 struct sm_state *extra_sm = NULL;
759 int unchanged = 0;
760 char *loop_name;
761 struct stree *stree = NULL;
762 struct sm_state *sm = NULL;
764 loop_name = get_loop_name(loop_num);
765 loop_num++;
767 if (stmt->iterator_pre_statement) {
768 __split_stmt(stmt->iterator_pre_statement);
769 __prev_stmt = stmt->iterator_pre_statement;
772 once_through = implied_condition_true(stmt->iterator_pre_condition);
774 loop_count++;
775 __push_continues();
776 __push_breaks();
778 __merge_gotos(loop_name, NULL);
780 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
781 __in_pre_condition++;
782 __pass_to_client(stmt, PRELOOP_HOOK);
783 __split_whole_condition(stmt->iterator_pre_condition);
784 __in_pre_condition--;
785 FOR_EACH_SM(stree, sm) {
786 set_state(sm->owner, sm->name, sm->sym, sm->state);
787 } END_FOR_EACH_SM(sm);
788 free_stree(&stree);
789 if (extra_sm)
790 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
792 if (option_assume_loops)
793 once_through = 1;
795 __split_stmt(stmt->iterator_statement);
796 if (is_forever_loop(stmt)) {
797 __merge_continues();
798 __save_gotos(loop_name, NULL);
800 __push_fake_cur_stree();
801 __split_stmt(stmt->iterator_post_statement);
802 stree = __pop_fake_cur_stree();
804 __discard_false_states();
805 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
806 __use_breaks();
808 if (!__path_is_null())
809 __merge_stree_into_cur(stree);
810 free_stree(&stree);
811 } else {
812 __merge_continues();
813 unchanged = __iterator_unchanged(extra_sm);
814 __split_stmt(stmt->iterator_post_statement);
815 __prev_stmt = stmt->iterator_post_statement;
816 __cur_stmt = stmt;
818 __save_gotos(loop_name, NULL);
819 __in_pre_condition++;
820 __split_whole_condition(stmt->iterator_pre_condition);
821 __in_pre_condition--;
822 nullify_path();
823 __merge_false_states();
824 if (once_through)
825 __discard_false_states();
826 else
827 __merge_false_states();
829 if (extra_sm && unchanged)
830 __extra_pre_loop_hook_after(extra_sm,
831 stmt->iterator_post_statement,
832 stmt->iterator_pre_condition);
833 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
834 __merge_breaks();
836 loop_count--;
840 * Post loops are do {} while();
842 static void handle_post_loop(struct statement *stmt)
844 char *loop_name;
846 loop_name = get_loop_name(loop_num);
847 loop_num++;
848 loop_count++;
850 __pass_to_client(stmt, POSTLOOP_HOOK);
852 __push_continues();
853 __push_breaks();
854 __merge_gotos(loop_name, NULL);
855 __split_stmt(stmt->iterator_statement);
856 __merge_continues();
857 if (!expr_is_zero(stmt->iterator_post_condition))
858 __save_gotos(loop_name, NULL);
860 if (is_forever_loop(stmt)) {
861 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
862 __use_breaks();
863 } else {
864 __split_whole_condition(stmt->iterator_post_condition);
865 __use_false_states();
866 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
867 __merge_breaks();
869 loop_count--;
872 static int empty_statement(struct statement *stmt)
874 if (!stmt)
875 return 0;
876 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
877 return 1;
878 return 0;
881 static int last_stmt_on_same_line(void)
883 struct statement *stmt;
884 int i = 0;
886 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
887 if (!i++)
888 continue;
889 if (stmt->pos.line == get_lineno())
890 return 1;
891 return 0;
892 } END_FOR_EACH_PTR_REVERSE(stmt);
893 return 0;
896 static void split_asm_ops(struct asm_operand_list *ops)
898 struct asm_operand *op;
900 FOR_EACH_PTR(ops, op) {
901 __split_expr(op->expr);
902 } END_FOR_EACH_PTR(op);
905 static int is_case_val(struct statement *stmt, sval_t sval)
907 sval_t case_sval;
909 if (stmt->type != STMT_CASE)
910 return 0;
911 if (!stmt->case_expression) {
912 __set_default();
913 return 1;
915 if (!get_value(stmt->case_expression, &case_sval))
916 return 0;
917 if (case_sval.value == sval.value)
918 return 1;
919 return 0;
922 static struct range_list *get_case_rl(struct expression *switch_expr,
923 struct expression *case_expr,
924 struct expression *case_to)
926 sval_t start, end;
927 struct range_list *rl = NULL;
928 struct symbol *switch_type;
930 switch_type = get_type(switch_expr);
931 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
932 start = sval_cast(switch_type, start);
933 end = sval_cast(switch_type, end);
934 add_range(&rl, start, end);
935 } else if (get_value(case_expr, &start)) {
936 start = sval_cast(switch_type, start);
937 add_range(&rl, start, start);
940 return rl;
943 static void split_known_switch(struct statement *stmt, sval_t sval)
945 struct statement *tmp;
946 struct range_list *rl;
948 __split_expr(stmt->switch_expression);
949 sval = sval_cast(get_type(stmt->switch_expression), sval);
951 push_expression(&switch_expr_stack, stmt->switch_expression);
952 __save_switch_states(top_expression(switch_expr_stack));
953 nullify_path();
954 __push_default();
955 __push_breaks();
957 stmt = stmt->switch_statement;
959 __push_scope_hooks();
960 FOR_EACH_PTR(stmt->stmts, tmp) {
961 __smatch_lineno = tmp->pos.line;
962 // FIXME: what if default comes before the known case statement?
963 if (is_case_val(tmp, sval)) {
964 rl = alloc_rl(sval, sval);
965 __merge_switches(top_expression(switch_expr_stack), rl);
966 __pass_case_to_client(top_expression(switch_expr_stack), rl);
967 stmt_set_parent_stmt(tmp->case_statement, tmp);
968 __split_stmt(tmp->case_statement);
969 goto next;
971 if (__path_is_null())
972 continue;
973 __split_stmt(tmp);
974 next:
975 if (__path_is_null()) {
976 __set_default();
977 goto out;
979 } END_FOR_EACH_PTR(tmp);
980 out:
981 __call_scope_hooks();
982 if (!__pop_default())
983 __merge_switches(top_expression(switch_expr_stack), NULL);
984 __discard_switches();
985 __merge_breaks();
986 pop_expression(&switch_expr_stack);
989 static void split_case(struct statement *stmt)
991 struct range_list *rl = NULL;
993 expr_set_parent_stmt(stmt->case_expression, stmt);
994 expr_set_parent_stmt(stmt->case_to, stmt);
996 rl = get_case_rl(top_expression(switch_expr_stack),
997 stmt->case_expression, stmt->case_to);
998 while (stmt->case_statement->type == STMT_CASE) {
999 struct range_list *tmp;
1001 tmp = get_case_rl(top_expression(switch_expr_stack),
1002 stmt->case_statement->case_expression,
1003 stmt->case_statement->case_to);
1004 if (!tmp)
1005 goto next;
1006 rl = rl_union(rl, tmp);
1007 if (!stmt->case_expression)
1008 __set_default();
1009 next:
1010 stmt = stmt->case_statement;
1013 __merge_switches(top_expression(switch_expr_stack), rl);
1015 if (!stmt->case_expression)
1016 __set_default();
1018 stmt_set_parent_stmt(stmt->case_statement, stmt);
1019 __split_stmt(stmt->case_statement);
1022 int time_parsing_function(void)
1024 return ms_since(&fn_start_time) / 1000;
1027 bool taking_too_long(void)
1029 if ((ms_since(&outer_fn_start_time) / 1000) > 60 * 5) /* five minutes */
1030 return 1;
1031 return 0;
1034 struct statement *get_last_stmt(void)
1036 struct symbol *fn;
1037 struct statement *stmt;
1039 fn = get_base_type(cur_func_sym);
1040 if (!fn)
1041 return NULL;
1042 stmt = fn->stmt;
1043 if (!stmt)
1044 stmt = fn->inline_stmt;
1045 if (!stmt || stmt->type != STMT_COMPOUND)
1046 return NULL;
1047 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1048 if (stmt && stmt->type == STMT_LABEL)
1049 stmt = stmt->label_statement;
1050 return stmt;
1053 int is_last_stmt(struct statement *cur_stmt)
1055 struct statement *last;
1057 last = get_last_stmt();
1058 if (last && last == cur_stmt)
1059 return 1;
1060 return 0;
1063 static void handle_backward_goto(struct statement *goto_stmt)
1065 const char *goto_name, *label_name;
1066 struct statement *func_stmt;
1067 struct symbol *base_type = get_base_type(cur_func_sym);
1068 struct statement *tmp;
1069 int found = 0;
1071 if (!option_info)
1072 return;
1073 if (last_goto_statement_handled)
1074 return;
1075 last_goto_statement_handled = 1;
1077 if (!goto_stmt->goto_label ||
1078 goto_stmt->goto_label->type != SYM_LABEL ||
1079 !goto_stmt->goto_label->ident)
1080 return;
1081 goto_name = goto_stmt->goto_label->ident->name;
1083 func_stmt = base_type->stmt;
1084 if (!func_stmt)
1085 func_stmt = base_type->inline_stmt;
1086 if (!func_stmt)
1087 return;
1088 if (func_stmt->type != STMT_COMPOUND)
1089 return;
1091 FOR_EACH_PTR(func_stmt->stmts, tmp) {
1092 if (!found) {
1093 if (tmp->type != STMT_LABEL)
1094 continue;
1095 if (!tmp->label_identifier ||
1096 tmp->label_identifier->type != SYM_LABEL ||
1097 !tmp->label_identifier->ident)
1098 continue;
1099 label_name = tmp->label_identifier->ident->name;
1100 if (strcmp(goto_name, label_name) != 0)
1101 continue;
1102 found = 1;
1104 __split_stmt(tmp);
1105 } END_FOR_EACH_PTR(tmp);
1108 static void fake_a_return(void)
1110 struct expression *ret = NULL;
1112 nullify_path();
1113 __unnullify_path();
1115 if (cur_func_return_type() != &void_ctype)
1116 ret = unknown_value_expression(NULL);
1118 __pass_to_client(ret, RETURN_HOOK);
1119 nullify_path();
1122 static void split_ret_value(struct expression *expr)
1124 struct symbol *type;
1126 if (!expr)
1127 return;
1129 type = get_real_base_type(cur_func_sym);
1130 type = get_real_base_type(type);
1131 expr = fake_a_variable_assign(type, NULL, expr, -1);
1133 __in_fake_var_assign++;
1134 __split_expr(expr);
1135 __in_fake_var_assign--;
1138 static void fake_an_empty_default(struct position pos)
1140 static struct statement none = {};
1142 none.pos = pos;
1143 none.type = STMT_NONE;
1144 __merge_switches(top_expression(switch_expr_stack), NULL);
1145 __split_stmt(&none);
1148 static void split_compound(struct statement *stmt)
1150 struct statement *prev = NULL;
1151 struct statement *cur = NULL;
1152 struct statement *next;
1154 __push_scope_hooks();
1156 FOR_EACH_PTR(stmt->stmts, next) {
1157 /* just set them all ahead of time */
1158 stmt_set_parent_stmt(next, stmt);
1160 if (cur) {
1161 __prev_stmt = prev;
1162 __next_stmt = next;
1163 __cur_stmt = cur;
1164 __split_stmt(cur);
1166 prev = cur;
1167 cur = next;
1168 } END_FOR_EACH_PTR(next);
1169 if (cur) {
1170 __prev_stmt = prev;
1171 __cur_stmt = cur;
1172 __next_stmt = NULL;
1173 __split_stmt(cur);
1177 * For function scope, then delay calling the scope hooks until the
1178 * end of function hooks can run. I'm not positive this is the right
1179 * thing...
1181 if (!is_last_stmt(cur))
1182 __call_scope_hooks();
1186 * This is a hack, work around for detecting empty functions.
1188 static int need_delayed_scope_hooks(void)
1190 struct symbol *fn = get_base_type(cur_func_sym);
1191 struct statement *stmt;
1193 if (!fn)
1194 return 0;
1195 stmt = fn->stmt;
1196 if (!stmt)
1197 stmt = fn->inline_stmt;
1198 if (stmt && stmt->type == STMT_COMPOUND)
1199 return 1;
1200 return 0;
1203 void __split_label_stmt(struct statement *stmt)
1205 if (stmt->label_identifier &&
1206 stmt->label_identifier->type == SYM_LABEL &&
1207 stmt->label_identifier->ident) {
1208 loop_count |= 0x0800000;
1209 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
1213 static void find_asm_gotos(struct statement *stmt)
1215 struct symbol *sym;
1217 FOR_EACH_PTR(stmt->asm_labels, sym) {
1218 __save_gotos(sym->ident->name, sym);
1219 } END_FOR_EACH_PTR(sym);
1222 void __split_stmt(struct statement *stmt)
1224 static int indent_cnt;
1225 sval_t sval;
1226 struct timeval start, stop;
1228 gettimeofday(&start, NULL);
1230 if (!stmt)
1231 goto out;
1233 if (!__in_fake_assign)
1234 __silence_warnings_for_stmt = false;
1236 if (__bail_on_rest_of_function || is_skipped_function())
1237 return;
1239 if (out_of_memory() || taking_too_long()) {
1240 gettimeofday(&start, NULL);
1242 __bail_on_rest_of_function = 1;
1243 final_pass = 1;
1244 sm_perror("Function too hairy. Giving up. %lu seconds",
1245 start.tv_sec - fn_start_time.tv_sec);
1246 fake_a_return();
1247 final_pass = 0; /* turn off sm_msg() from here */
1248 return;
1251 indent_cnt++;
1253 add_ptr_list(&big_statement_stack, stmt);
1254 free_expression_stack(&big_expression_stack);
1255 set_position(stmt->pos);
1256 __pass_to_client(stmt, STMT_HOOK);
1258 switch (stmt->type) {
1259 case STMT_DECLARATION:
1260 split_declaration(stmt->declaration);
1261 break;
1262 case STMT_RETURN:
1263 expr_set_parent_stmt(stmt->ret_value, stmt);
1265 split_ret_value(stmt->ret_value);
1266 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1267 __process_post_op_stack();
1268 nullify_path();
1269 break;
1270 case STMT_EXPRESSION:
1271 expr_set_parent_stmt(stmt->expression, stmt);
1272 expr_set_parent_stmt(stmt->context, stmt);
1274 __split_expr(stmt->expression);
1275 break;
1276 case STMT_COMPOUND:
1277 split_compound(stmt);
1278 break;
1279 case STMT_IF:
1280 stmt_set_parent_stmt(stmt->if_true, stmt);
1281 stmt_set_parent_stmt(stmt->if_false, stmt);
1282 expr_set_parent_stmt(stmt->if_conditional, stmt);
1284 if (known_condition_true(stmt->if_conditional)) {
1285 __split_stmt(stmt->if_true);
1286 break;
1288 if (known_condition_false(stmt->if_conditional)) {
1289 __split_stmt(stmt->if_false);
1290 break;
1292 __split_whole_condition(stmt->if_conditional);
1293 __split_stmt(stmt->if_true);
1294 if (empty_statement(stmt->if_true) &&
1295 last_stmt_on_same_line() &&
1296 !get_macro_name(stmt->if_true->pos))
1297 sm_warning("if();");
1298 __push_true_states();
1299 __use_false_states();
1300 __split_stmt(stmt->if_false);
1301 __merge_true_states();
1302 break;
1303 case STMT_ITERATOR:
1304 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1305 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1306 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1307 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1308 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1310 if (stmt->iterator_pre_condition)
1311 handle_pre_loop(stmt);
1312 else if (stmt->iterator_post_condition)
1313 handle_post_loop(stmt);
1314 else {
1315 // these are for(;;) type loops.
1316 handle_pre_loop(stmt);
1318 break;
1319 case STMT_SWITCH:
1320 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1321 expr_set_parent_stmt(stmt->switch_expression, stmt);
1323 if (get_value(stmt->switch_expression, &sval)) {
1324 split_known_switch(stmt, sval);
1325 break;
1327 __split_expr(stmt->switch_expression);
1328 push_expression(&switch_expr_stack, stmt->switch_expression);
1329 __save_switch_states(top_expression(switch_expr_stack));
1330 nullify_path();
1331 __push_default();
1332 __push_breaks();
1333 __split_stmt(stmt->switch_statement);
1334 if (!__pop_default() && have_remaining_cases())
1335 fake_an_empty_default(stmt->pos);
1336 __discard_switches();
1337 __merge_breaks();
1338 pop_expression(&switch_expr_stack);
1339 break;
1340 case STMT_CASE:
1341 split_case(stmt);
1342 break;
1343 case STMT_LABEL:
1344 __split_label_stmt(stmt);
1345 __split_stmt(stmt->label_statement);
1346 break;
1347 case STMT_GOTO:
1348 expr_set_parent_stmt(stmt->goto_expression, stmt);
1350 __split_expr(stmt->goto_expression);
1351 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1352 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1353 __process_breaks();
1354 } else if (!strcmp(stmt->goto_label->ident->name,
1355 "continue")) {
1356 __process_continues();
1358 } else if (stmt->goto_label &&
1359 stmt->goto_label->type == SYM_LABEL &&
1360 stmt->goto_label->ident) {
1361 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1363 nullify_path();
1364 if (is_last_stmt(stmt))
1365 handle_backward_goto(stmt);
1366 break;
1367 case STMT_NONE:
1368 break;
1369 case STMT_ASM:
1370 expr_set_parent_stmt(stmt->asm_string, stmt);
1372 find_asm_gotos(stmt);
1373 __pass_to_client(stmt, ASM_HOOK);
1374 __split_expr(stmt->asm_string);
1375 split_asm_ops(stmt->asm_outputs);
1376 split_asm_ops(stmt->asm_inputs);
1377 split_expr_list(stmt->asm_clobbers, NULL);
1378 break;
1379 case STMT_CONTEXT:
1380 break;
1381 case STMT_RANGE:
1382 __split_expr(stmt->range_expression);
1383 __split_expr(stmt->range_low);
1384 __split_expr(stmt->range_high);
1385 break;
1387 __pass_to_client(stmt, STMT_HOOK_AFTER);
1388 if (--indent_cnt == 1)
1389 __discard_fake_states(NULL);
1391 out:
1392 __process_post_op_stack();
1394 gettimeofday(&stop, NULL);
1395 if (option_time_stmt && stmt)
1396 sm_msg("stmt_time%s: %ld",
1397 stmt->type == STMT_COMPOUND ? "_block" : "",
1398 stop.tv_sec - start.tv_sec);
1401 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1403 struct expression *expr;
1405 FOR_EACH_PTR(expr_list, expr) {
1406 expr_set_parent_expr(expr, parent);
1407 __split_expr(expr);
1408 __process_post_op_stack();
1409 } END_FOR_EACH_PTR(expr);
1412 static bool cast_arg(struct symbol *type, struct expression *arg)
1414 struct symbol *orig;
1416 if (!type)
1417 return false;
1419 arg = strip_parens(arg);
1420 if (arg != strip_expr(arg))
1421 return true;
1423 orig = get_type(arg);
1424 if (!orig)
1425 return true;
1426 if (types_equiv(orig, type))
1427 return false;
1429 if (orig->type == SYM_ARRAY && type->type == SYM_PTR)
1430 return true;
1433 * I would have expected that we could just do use (orig == type) but I
1434 * guess for pointers we need to get the basetype to do that comparison.
1438 if (orig->type != SYM_PTR ||
1439 type->type != SYM_PTR) {
1440 if (type_fits(type, orig))
1441 return false;
1442 return true;
1444 orig = get_real_base_type(orig);
1445 type = get_real_base_type(type);
1446 if (orig == type)
1447 return false;
1449 return true;
1452 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *call, struct expression *expr, int nr)
1454 char buf[64];
1455 bool cast;
1457 if (!expr || !cur_func_sym)
1458 return NULL;
1460 if (expr->type == EXPR_ASSIGNMENT)
1461 return expr;
1463 /* for va_args then we don't know the type */
1464 if (!type)
1465 type = get_type(expr);
1467 cast = cast_arg(type, expr);
1469 * Using expr_to_sym() here is a hack. We want to say that we don't
1470 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1471 * It turns out faking these assignments is way more expensive than I
1472 * would have imagined. I'm not sure why exactly.
1475 if (!cast) {
1477 * if the code is "return *p;" where "p" is a user pointer then
1478 * we want to create a fake assignment so that it sets the state
1479 * in check_kernel_user_data.c.
1482 if (expr->type != EXPR_PREOP &&
1483 expr->op != '*' && expr->op != '&' &&
1484 expr_to_sym(expr))
1485 return expr;
1488 if (nr == -1)
1489 snprintf(buf, sizeof(buf), "__fake_return_%p", expr);
1490 else
1491 snprintf(buf, sizeof(buf), "__fake_param_%p_%d", call, nr);
1493 return create_fake_assign(buf, type, expr);
1496 static void split_args(struct expression *expr)
1498 struct expression *arg, *tmp;
1499 struct symbol *type;
1500 int i;
1502 i = -1;
1503 FOR_EACH_PTR(expr->args, arg) {
1504 i++;
1505 expr_set_parent_expr(arg, expr);
1506 type = get_arg_type(expr->fn, i);
1507 tmp = fake_a_variable_assign(type, expr, arg, i);
1508 if (tmp != arg)
1509 __in_fake_var_assign++;
1510 __split_expr(tmp);
1511 if (tmp != arg)
1512 __in_fake_var_assign--;
1513 __process_post_op_stack();
1514 } END_FOR_EACH_PTR(arg);
1517 static void split_sym(struct symbol *sym)
1519 if (!sym)
1520 return;
1521 if (!(sym->namespace & NS_SYMBOL))
1522 return;
1524 __split_stmt(sym->stmt);
1525 __split_expr(sym->array_size);
1526 split_symlist(sym->arguments);
1527 split_symlist(sym->symbol_list);
1528 __split_stmt(sym->inline_stmt);
1529 split_symlist(sym->inline_symbol_list);
1532 static void split_symlist(struct symbol_list *sym_list)
1534 struct symbol *sym;
1536 FOR_EACH_PTR(sym_list, sym) {
1537 split_sym(sym);
1538 } END_FOR_EACH_PTR(sym);
1541 typedef void (fake_cb)(struct expression *expr);
1543 static int member_to_number(struct expression *expr, struct ident *member)
1545 struct symbol *type, *tmp;
1546 char *name;
1547 int i;
1549 if (!member)
1550 return -1;
1551 name = member->name;
1553 type = get_type(expr);
1554 if (!type || type->type != SYM_STRUCT)
1555 return -1;
1557 i = -1;
1558 FOR_EACH_PTR(type->symbol_list, tmp) {
1559 i++;
1560 if (!tmp->ident)
1561 continue;
1562 if (strcmp(name, tmp->ident->name) == 0)
1563 return i;
1564 } END_FOR_EACH_PTR(tmp);
1565 return -1;
1568 static struct ident *number_to_member(struct expression *expr, int num)
1570 struct symbol *type, *member;
1571 int i = 0;
1573 type = get_type(expr);
1574 if (!type || type->type != SYM_STRUCT)
1575 return NULL;
1577 FOR_EACH_PTR(type->symbol_list, member) {
1578 if (i == num)
1579 return member->ident;
1580 i++;
1581 } END_FOR_EACH_PTR(member);
1582 return NULL;
1585 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1587 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1589 struct expression *edge_member, *assign;
1590 struct symbol *base = get_real_base_type(member);
1591 struct symbol *tmp;
1593 if (member->ident)
1594 expr = member_expression(expr, '.', member->ident);
1596 FOR_EACH_PTR(base->symbol_list, tmp) {
1597 struct symbol *type;
1599 type = get_real_base_type(tmp);
1600 if (!type)
1601 continue;
1603 edge_member = member_expression(expr, '.', tmp->ident);
1604 if (get_extra_state(edge_member))
1605 continue;
1607 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1608 set_inner_struct_members(expr, tmp);
1609 continue;
1612 if (!tmp->ident)
1613 continue;
1615 assign = assign_expression(edge_member, '=', zero_expr());
1616 __split_expr(assign);
1617 } END_FOR_EACH_PTR(tmp);
1622 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1624 struct symbol *tmp;
1625 struct expression *member = NULL;
1626 struct expression *assign;
1628 FOR_EACH_PTR(type->symbol_list, tmp) {
1629 type = get_real_base_type(tmp);
1630 if (!type)
1631 continue;
1633 if (tmp->ident) {
1634 member = member_expression(expr, '.', tmp->ident);
1635 if (get_extra_state(member))
1636 continue;
1639 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1640 set_inner_struct_members(expr, tmp);
1641 continue;
1643 if (type->type == SYM_ARRAY)
1644 continue;
1645 if (!tmp->ident)
1646 continue;
1648 assign = assign_expression(member, '=', zero_expr());
1649 __split_expr(assign);
1650 } END_FOR_EACH_PTR(tmp);
1653 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1655 struct expression *deref, *assign, *tmp, *right;
1656 struct symbol *struct_type, *type;
1657 struct ident *member;
1658 int member_idx;
1660 struct_type = get_type(symbol);
1661 if (!struct_type ||
1662 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1663 return;
1666 * We're parsing an initializer that could look something like this:
1667 * struct foo foo = {
1668 * 42,
1669 * .whatever.xxx = 11,
1670 * .zzz = 12,
1671 * };
1673 * So what we have here is a list with 42, .whatever, and .zzz. We need
1674 * to break it up into left and right sides of the assignments.
1677 member_idx = 0;
1678 FOR_EACH_PTR(members, tmp) {
1679 deref = NULL;
1680 if (tmp->type == EXPR_IDENTIFIER) {
1681 member_idx = member_to_number(symbol, tmp->expr_ident);
1682 while (tmp->type == EXPR_IDENTIFIER) {
1683 member = tmp->expr_ident;
1684 tmp = tmp->ident_expression;
1685 if (deref)
1686 deref = member_expression(deref, '.', member);
1687 else
1688 deref = member_expression(symbol, '.', member);
1690 } else {
1691 member = number_to_member(symbol, member_idx);
1692 deref = member_expression(symbol, '.', member);
1694 right = tmp;
1695 member_idx++;
1696 if (right->type == EXPR_INITIALIZER) {
1697 type = get_type(deref);
1698 if (type && type->type == SYM_ARRAY)
1699 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1700 else
1701 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1702 } else {
1703 assign = assign_expression(deref, '=', right);
1704 fake_cb(assign);
1706 } END_FOR_EACH_PTR(tmp);
1708 set_unset_to_zero(struct_type, symbol);
1711 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1713 fake_member_assigns_helper(symbol_expression(sym),
1714 sym->initializer->expr_list, fake_cb);
1717 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1719 struct expression *offset, *binop, *assign, *tmp;
1720 struct symbol *type;
1721 int idx, max;
1723 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1724 return;
1726 max = 0;
1727 idx = 0;
1728 FOR_EACH_PTR(expr_list, tmp) {
1729 if (tmp->type == EXPR_INDEX) {
1730 if (tmp->idx_from != tmp->idx_to)
1731 return;
1732 idx = tmp->idx_from;
1733 if (idx > max)
1734 max = idx;
1735 if (!tmp->idx_expression)
1736 goto next;
1737 tmp = tmp->idx_expression;
1739 offset = value_expr(idx);
1740 binop = array_element_expression(array, offset);
1741 if (tmp->type == EXPR_INITIALIZER) {
1742 type = get_type(binop);
1743 if (type && type->type == SYM_ARRAY)
1744 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1745 else
1746 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1747 } else {
1748 assign = assign_expression(binop, '=', tmp);
1749 fake_cb(assign);
1751 next:
1752 idx++;
1753 if (idx > max)
1754 max = idx;
1755 } END_FOR_EACH_PTR(tmp);
1757 __call_array_initialized_hooks(array, max);
1760 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1762 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1765 static void fake_assign_expr(struct symbol *sym)
1767 struct expression *assign, *symbol;
1769 symbol = symbol_expression(sym);
1770 assign = assign_expression(symbol, '=', sym->initializer);
1771 __split_expr(assign);
1774 static void do_initializer_stuff(struct symbol *sym)
1776 if (!sym->initializer)
1777 return;
1779 if (sym->initializer->type == EXPR_INITIALIZER) {
1780 if (get_real_base_type(sym)->type == SYM_ARRAY)
1781 fake_element_assigns(sym, __split_expr);
1782 else
1783 fake_member_assigns(sym, __split_expr);
1784 } else {
1785 fake_assign_expr(sym);
1789 static void split_declaration(struct symbol_list *sym_list)
1791 struct symbol *sym;
1793 FOR_EACH_PTR(sym_list, sym) {
1794 __pass_to_client(sym, DECLARATION_HOOK);
1795 do_initializer_stuff(sym);
1796 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
1797 split_sym(sym);
1798 } END_FOR_EACH_PTR(sym);
1801 static void call_global_assign_hooks(struct expression *assign)
1803 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1806 static void fake_global_assign(struct symbol *sym)
1808 struct expression *assign, *symbol;
1810 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1811 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1812 fake_element_assigns(sym, call_global_assign_hooks);
1813 } else if (sym->initializer) {
1814 symbol = symbol_expression(sym);
1815 assign = assign_expression(symbol, '=', sym->initializer);
1816 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1817 } else {
1818 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1820 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1821 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1822 fake_member_assigns(sym, call_global_assign_hooks);
1823 } else if (sym->initializer) {
1824 symbol = symbol_expression(sym);
1825 assign = assign_expression(symbol, '=', sym->initializer);
1826 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1827 } else {
1828 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1830 } else {
1831 symbol = symbol_expression(sym);
1832 if (sym->initializer) {
1833 assign = assign_expression(symbol, '=', sym->initializer);
1834 __split_expr(assign);
1835 } else {
1836 assign = assign_expression(symbol, '=', zero_expr());
1838 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1842 static void start_function_definition(struct symbol *sym)
1844 __in_function_def = 1;
1845 __pass_to_client(sym, FUNC_DEF_HOOK);
1846 __in_function_def = 0;
1847 __pass_to_client(sym, AFTER_DEF_HOOK);
1851 void add_function_data(unsigned long *fn_data)
1853 __add_ptr_list(&fn_data_list, fn_data);
1856 static void clear_function_data(void)
1858 unsigned long *tmp;
1860 FOR_EACH_PTR(fn_data_list, tmp) {
1861 *tmp = 0;
1862 } END_FOR_EACH_PTR(tmp);
1865 static void record_func_time(void)
1867 struct timeval stop;
1868 int func_time;
1869 char buf[32];
1871 gettimeofday(&stop, NULL);
1872 func_time = stop.tv_sec - fn_start_time.tv_sec;
1873 snprintf(buf, sizeof(buf), "%d", func_time);
1874 sql_insert_return_implies(FUNC_TIME, 0, "", buf);
1875 if (option_time && func_time > 2) {
1876 final_pass++;
1877 sm_msg("func_time: %d", func_time);
1878 final_pass--;
1882 static void split_function(struct symbol *sym)
1884 struct symbol *base_type = get_base_type(sym);
1886 if (!base_type->stmt && !base_type->inline_stmt)
1887 return;
1889 gettimeofday(&outer_fn_start_time, NULL);
1890 gettimeofday(&fn_start_time, NULL);
1891 cur_func_sym = sym;
1892 if (sym->ident)
1893 cur_func = sym->ident->name;
1894 set_position(sym->pos);
1895 clear_function_data();
1896 loop_count = 0;
1897 last_goto_statement_handled = 0;
1898 sm_debug("new function: %s\n", cur_func);
1899 __stree_id = 0;
1900 if (option_two_passes) {
1901 __unnullify_path();
1902 loop_num = 0;
1903 final_pass = 0;
1904 start_function_definition(sym);
1905 __split_stmt(base_type->stmt);
1906 __split_stmt(base_type->inline_stmt);
1907 nullify_path();
1909 __unnullify_path();
1910 loop_num = 0;
1911 final_pass = 1;
1912 start_function_definition(sym);
1913 __split_stmt(base_type->stmt);
1914 __split_stmt(base_type->inline_stmt);
1915 if (!__path_is_null() &&
1916 cur_func_return_type() == &void_ctype &&
1917 !__bail_on_rest_of_function) {
1918 __pass_to_client(NULL, RETURN_HOOK);
1919 nullify_path();
1921 __pass_to_client(sym, END_FUNC_HOOK);
1922 if (need_delayed_scope_hooks())
1923 __call_scope_hooks();
1924 __pass_to_client(sym, AFTER_FUNC_HOOK);
1925 sym->parsed = true;
1927 clear_all_states();
1929 record_func_time();
1931 cur_func_sym = NULL;
1932 cur_func = NULL;
1933 free_data_info_allocs();
1934 free_expression_stack(&switch_expr_stack);
1935 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1936 __bail_on_rest_of_function = 0;
1939 static void save_flow_state(void)
1941 unsigned long *tmp;
1943 __add_ptr_list(&backup, INT_PTR(loop_num << 2));
1944 __add_ptr_list(&backup, INT_PTR(loop_count << 2));
1945 __add_ptr_list(&backup, INT_PTR(final_pass << 2));
1947 __add_ptr_list(&backup, big_statement_stack);
1948 __add_ptr_list(&backup, big_expression_stack);
1949 __add_ptr_list(&backup, big_condition_stack);
1950 __add_ptr_list(&backup, switch_expr_stack);
1952 __add_ptr_list(&backup, cur_func_sym);
1954 __add_ptr_list(&backup, __prev_stmt);
1955 __add_ptr_list(&backup, __cur_stmt);
1956 __add_ptr_list(&backup, __next_stmt);
1958 FOR_EACH_PTR(fn_data_list, tmp) {
1959 __add_ptr_list(&backup, (void *)*tmp);
1960 } END_FOR_EACH_PTR(tmp);
1963 static void *pop_backup(void)
1965 void *ret;
1967 ret = last_ptr_list(backup);
1968 delete_ptr_list_last(&backup);
1969 return ret;
1972 static void restore_flow_state(void)
1974 unsigned long *tmp;
1976 FOR_EACH_PTR_REVERSE(fn_data_list, tmp) {
1977 *tmp = (unsigned long)pop_backup();
1978 } END_FOR_EACH_PTR_REVERSE(tmp);
1980 __next_stmt = pop_backup();
1981 __cur_stmt = pop_backup();
1982 __prev_stmt = pop_backup();
1984 cur_func_sym = pop_backup();
1985 switch_expr_stack = pop_backup();
1986 big_condition_stack = pop_backup();
1987 big_expression_stack = pop_backup();
1988 big_statement_stack = pop_backup();
1989 final_pass = PTR_INT(pop_backup()) >> 2;
1990 loop_count = PTR_INT(pop_backup()) >> 2;
1991 loop_num = PTR_INT(pop_backup()) >> 2;
1994 static void parse_inline(struct expression *call)
1996 struct symbol *base_type;
1997 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
1998 struct timeval time_backup = fn_start_time;
1999 struct expression *orig_inline = __inline_fn;
2000 int orig_budget;
2002 if (out_of_memory() || taking_too_long())
2003 return;
2005 save_flow_state();
2007 __pass_to_client(call, INLINE_FN_START);
2008 final_pass = 0; /* don't print anything */
2009 __inline_fn = call;
2010 orig_budget = inline_budget;
2011 inline_budget = inline_budget - 5;
2013 base_type = get_base_type(call->fn->symbol);
2014 cur_func_sym = call->fn->symbol;
2015 if (call->fn->symbol->ident)
2016 cur_func = call->fn->symbol->ident->name;
2017 else
2018 cur_func = NULL;
2019 set_position(call->fn->symbol->pos);
2021 save_all_states();
2022 big_statement_stack = NULL;
2023 big_expression_stack = NULL;
2024 big_condition_stack = NULL;
2025 switch_expr_stack = NULL;
2027 sm_debug("inline function: %s\n", cur_func);
2028 __unnullify_path();
2029 clear_function_data();
2030 loop_num = 0;
2031 loop_count = 0;
2032 start_function_definition(call->fn->symbol);
2033 __split_stmt(base_type->stmt);
2034 __split_stmt(base_type->inline_stmt);
2035 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
2036 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
2037 call->fn->symbol->parsed = true;
2039 free_expression_stack(&switch_expr_stack);
2040 __free_ptr_list((struct ptr_list **)&big_statement_stack);
2041 nullify_path();
2042 free_goto_stack();
2044 restore_flow_state();
2045 fn_start_time = time_backup;
2046 cur_func = cur_func_bak;
2048 restore_all_states();
2049 set_position(call->pos);
2050 __inline_fn = orig_inline;
2051 inline_budget = orig_budget;
2052 __pass_to_client(call, INLINE_FN_END);
2055 static struct symbol_list *inlines_called;
2056 static void add_inline_function(struct symbol *sym)
2058 static struct symbol_list *already_added;
2059 struct symbol *tmp;
2061 FOR_EACH_PTR(already_added, tmp) {
2062 if (tmp == sym)
2063 return;
2064 } END_FOR_EACH_PTR(tmp);
2066 add_ptr_list(&already_added, sym);
2067 add_ptr_list(&inlines_called, sym);
2070 static void process_inlines(void)
2072 struct symbol *tmp;
2074 FOR_EACH_PTR(inlines_called, tmp) {
2075 split_function(tmp);
2076 } END_FOR_EACH_PTR(tmp);
2077 free_ptr_list(&inlines_called);
2080 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
2082 struct symbol *sym;
2084 FOR_EACH_PTR_REVERSE(big_list, sym) {
2085 if (!sym->scope)
2086 continue;
2087 if (use_static && sym->ctype.modifiers & MOD_STATIC)
2088 return sym;
2089 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
2090 return sym;
2091 } END_FOR_EACH_PTR_REVERSE(sym);
2093 return NULL;
2096 static bool interesting_function(struct symbol *sym)
2098 static int prev_stream = -1;
2099 static bool prev_answer;
2100 const char *filename;
2101 int len;
2103 if (!(sym->ctype.modifiers & MOD_INLINE))
2104 return true;
2106 if (sym->pos.stream == prev_stream)
2107 return prev_answer;
2109 prev_stream = sym->pos.stream;
2110 prev_answer = false;
2112 filename = stream_name(sym->pos.stream);
2113 len = strlen(filename);
2114 if (len > 0 && filename[len - 1] == 'c')
2115 prev_answer = true;
2116 return prev_answer;
2119 static void split_inlines_in_scope(struct symbol *sym)
2121 struct symbol *base;
2122 struct symbol_list *scope_list;
2123 int stream;
2125 scope_list = sym->scope->symbols;
2126 stream = sym->pos.stream;
2128 /* find the last static symbol in the file */
2129 FOR_EACH_PTR_REVERSE(scope_list, sym) {
2130 if (sym->pos.stream != stream)
2131 continue;
2132 if (sym->type != SYM_NODE)
2133 continue;
2134 base = get_base_type(sym);
2135 if (!base)
2136 continue;
2137 if (base->type != SYM_FN)
2138 continue;
2139 if (!base->inline_stmt)
2140 continue;
2141 if (!interesting_function(sym))
2142 continue;
2143 add_inline_function(sym);
2144 } END_FOR_EACH_PTR_REVERSE(sym);
2146 process_inlines();
2149 static void split_inlines(struct symbol_list *sym_list)
2151 struct symbol *sym;
2153 sym = get_last_scoped_symbol(sym_list, 0);
2154 if (sym)
2155 split_inlines_in_scope(sym);
2156 sym = get_last_scoped_symbol(sym_list, 1);
2157 if (sym)
2158 split_inlines_in_scope(sym);
2161 static struct stree *clone_estates_perm(struct stree *orig)
2163 struct stree *ret = NULL;
2164 struct sm_state *tmp;
2166 FOR_EACH_SM(orig, tmp) {
2167 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
2168 } END_FOR_EACH_SM(tmp);
2170 return ret;
2173 struct position last_pos;
2174 static void split_c_file_functions(struct symbol_list *sym_list)
2176 struct symbol *sym;
2178 __unnullify_path();
2179 FOR_EACH_PTR(sym_list, sym) {
2180 set_position(sym->pos);
2181 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
2182 __pass_to_client(sym, BASE_HOOK);
2183 fake_global_assign(sym);
2184 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
2186 } END_FOR_EACH_PTR(sym);
2187 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
2188 nullify_path();
2190 FOR_EACH_PTR(sym_list, sym) {
2191 set_position(sym->pos);
2192 last_pos = sym->pos;
2193 if (!interesting_function(sym))
2194 continue;
2195 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
2196 split_function(sym);
2197 process_inlines();
2199 last_pos = sym->pos;
2200 } END_FOR_EACH_PTR(sym);
2201 split_inlines(sym_list);
2202 __pass_to_client(sym_list, END_FILE_HOOK);
2205 static int final_before_fake;
2206 void init_fake_env(void)
2208 if (!in_fake_env)
2209 final_before_fake = final_pass;
2210 in_fake_env++;
2211 __push_fake_cur_stree();
2212 final_pass = 0;
2215 void end_fake_env(void)
2217 __free_fake_cur_stree();
2218 in_fake_env--;
2219 if (!in_fake_env)
2220 final_pass = final_before_fake;
2223 static void open_output_files(char *base_file)
2225 char buf[256];
2227 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
2228 sm_outfd = fopen(buf, "w");
2229 if (!sm_outfd)
2230 sm_fatal("Cannot open %s", buf);
2232 if (!option_info)
2233 return;
2235 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
2236 sql_outfd = fopen(buf, "w");
2237 if (!sql_outfd)
2238 sm_fatal("Error: Cannot open %s", buf);
2240 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
2241 caller_info_fd = fopen(buf, "w");
2242 if (!caller_info_fd)
2243 sm_fatal("Error: Cannot open %s", buf);
2246 void smatch(struct string_list *filelist)
2248 struct symbol_list *sym_list;
2249 struct timeval stop, start;
2250 char *path;
2251 int len;
2253 gettimeofday(&start, NULL);
2255 FOR_EACH_PTR_NOTAG(filelist, base_file) {
2256 path = getcwd(NULL, 0);
2257 free(full_base_file);
2258 if (path) {
2259 len = strlen(path) + 1 + strlen(base_file) + 1;
2260 full_base_file = malloc(len);
2261 snprintf(full_base_file, len, "%s/%s", path, base_file);
2262 } else {
2263 full_base_file = alloc_string(base_file);
2265 if (option_file_output)
2266 open_output_files(base_file);
2267 base_file_stream = input_stream_nr;
2268 sym_list = sparse_keep_tokens(base_file);
2269 split_c_file_functions(sym_list);
2270 } END_FOR_EACH_PTR_NOTAG(base_file);
2272 gettimeofday(&stop, NULL);
2274 set_position(last_pos);
2275 final_pass = 1;
2276 if (option_time)
2277 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
2278 if (option_mem)
2279 sm_msg("mem: %luKb", get_max_memory());