db/kernel.insert: the device_add() function sets the dev->driver pointer
[smatch.git] / smatch_flow.c
blob7ef5abc2011ae8454caa50b1bc896c3005b58de1
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 static 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 static unsigned int loop_count;
46 static int last_goto_statement_handled;
47 int __expr_stmt_count;
48 int __in_function_def;
49 int __in_unmatched_hook;
50 static struct expression_list *switch_expr_stack = NULL;
51 static struct expression_list *post_op_stack = NULL;
53 static struct ptr_list *backup;
55 struct expression_list *big_expression_stack;
56 struct statement_list *big_statement_stack;
57 struct statement *__prev_stmt;
58 struct statement *__cur_stmt;
59 struct statement *__next_stmt;
60 int __in_pre_condition = 0;
61 int __bail_on_rest_of_function = 0;
62 static struct timeval fn_start_time;
63 static struct timeval outer_fn_start_time;
64 char *get_function(void) { return cur_func; }
65 int get_lineno(void) { return __smatch_lineno; }
66 int inside_loop(void) { return !!loop_count; }
67 int definitely_inside_loop(void) { return !!(loop_count & ~0x08000000); }
68 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
69 int in_expression_statement(void) { return !!__expr_stmt_count; }
71 static void split_symlist(struct symbol_list *sym_list);
72 static void split_declaration(struct symbol_list *sym_list);
73 static void split_expr_list(struct expression_list *expr_list, struct expression *parent);
74 static void split_args(struct expression *expr);
75 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *expr);
76 static void add_inline_function(struct symbol *sym);
77 static void parse_inline(struct expression *expr);
79 int option_assume_loops = 0;
80 int option_two_passes = 0;
81 struct symbol *cur_func_sym = NULL;
82 struct stree *global_states;
84 const unsigned long valid_ptr_min = 4096;
85 unsigned long valid_ptr_max = ULONG_MAX & ~(MTAG_OFFSET_MASK);
86 const sval_t valid_ptr_min_sval = {
87 .type = &ptr_ctype,
88 {.value = 4096},
90 sval_t valid_ptr_max_sval = {
91 .type = &ptr_ctype,
92 {.value = ULONG_MAX & ~(MTAG_OFFSET_MASK)},
94 struct range_list *valid_ptr_rl;
96 void alloc_valid_ptr_rl(void)
98 valid_ptr_max = sval_type_max(&ulong_ctype).value & ~(MTAG_OFFSET_MASK);
99 valid_ptr_max_sval.value = valid_ptr_max;
101 valid_ptr_rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
102 valid_ptr_rl = cast_rl(&ptr_ctype, valid_ptr_rl);
103 valid_ptr_rl = clone_rl_permanent(valid_ptr_rl);
106 int outside_of_function(void)
108 return cur_func_sym == NULL;
111 const char *get_filename(void)
113 if (option_info && option_full_path)
114 return full_base_file;
115 if (option_info)
116 return base_file;
117 if (option_full_path)
118 return full_filename;
119 return filename;
122 const char *get_base_file(void)
124 if (option_full_path)
125 return full_base_file;
126 return base_file;
129 static void set_position(struct position pos)
131 int len;
132 static int prev_stream = -1;
134 if (in_fake_env)
135 return;
137 if (pos.stream == 0 && pos.line == 0)
138 return;
140 __smatch_lineno = pos.line;
142 if (pos.stream == prev_stream)
143 return;
145 filename = stream_name(pos.stream);
147 free(full_filename);
148 pathname = getcwd(NULL, 0);
149 if (pathname) {
150 len = strlen(pathname) + 1 + strlen(filename) + 1;
151 full_filename = malloc(len);
152 snprintf(full_filename, len, "%s/%s", pathname, filename);
153 } else {
154 full_filename = alloc_string(filename);
156 free(pathname);
159 int is_assigned_call(struct expression *expr)
161 struct expression *parent = expr_get_parent_expr(expr);
163 if (parent &&
164 parent->type == EXPR_ASSIGNMENT &&
165 parent->op == '=' &&
166 strip_expr(parent->right) == expr)
167 return 1;
169 return 0;
172 int is_fake_assigned_call(struct expression *expr)
174 struct expression *parent = expr_get_fake_parent_expr(expr);
176 if (parent &&
177 parent->type == EXPR_ASSIGNMENT &&
178 parent->op == '=' &&
179 strip_expr(parent->right) == expr)
180 return 1;
182 return 0;
185 static bool is_inline_func(struct expression *expr)
187 if (expr->type != EXPR_SYMBOL || !expr->symbol)
188 return false;
189 if (!expr->symbol->definition)
190 return false;
191 if (expr->symbol->definition->ctype.modifiers & MOD_INLINE)
192 return true;
194 return 0;
197 static int is_noreturn_func(struct expression *expr)
199 if (expr->type != EXPR_SYMBOL || !expr->symbol)
200 return 0;
201 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
202 return 1;
203 return 0;
206 static int inline_budget = 20;
208 int inlinable(struct expression *expr)
210 struct symbol *sym;
211 struct statement *last_stmt = NULL;
213 if (__inline_fn) /* don't nest */
214 return 0;
216 if (expr->type != EXPR_SYMBOL || !expr->symbol)
217 return 0;
218 if (is_no_inline_function(expr->symbol->ident->name))
219 return 0;
220 sym = get_base_type(expr->symbol);
221 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
222 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
223 return 0;
224 if (sym->stmt->type != STMT_COMPOUND)
225 return 0;
226 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
228 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
229 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
230 return 0;
231 if (sym->inline_stmt->type != STMT_COMPOUND)
232 return 0;
233 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
236 if (!last_stmt)
237 return 0;
239 /* the magic numbers in this function are pulled out of my bum. */
240 if (last_stmt->pos.line > sym->pos.line + inline_budget)
241 return 0;
243 return 1;
246 void __process_post_op_stack(void)
248 struct expression *expr;
250 FOR_EACH_PTR(post_op_stack, expr) {
251 __pass_to_client(expr, OP_HOOK);
252 } END_FOR_EACH_PTR(expr);
254 __free_ptr_list((struct ptr_list **)&post_op_stack);
257 static int handle_comma_assigns(struct expression *expr)
259 struct expression *right;
260 struct expression *assign;
262 right = strip_expr(expr->right);
263 if (right->type != EXPR_COMMA)
264 return 0;
266 __split_expr(right->left);
267 __process_post_op_stack();
269 assign = assign_expression(expr->left, '=', right->right);
270 __split_expr(assign);
272 return 1;
275 /* This is to handle *p++ = foo; assignments */
276 static int handle_postop_assigns(struct expression *expr)
278 struct expression *left, *fake_left;
279 struct expression *assign;
281 left = strip_expr(expr->left);
282 if (left->type != EXPR_PREOP || left->op != '*')
283 return 0;
284 left = strip_expr(left->unop);
285 if (left->type != EXPR_POSTOP)
286 return 0;
288 fake_left = deref_expression(strip_expr(left->unop));
289 assign = assign_expression(fake_left, '=', expr->right);
291 __split_expr(assign);
292 __split_expr(expr->left);
294 return 1;
297 static int prev_expression_is_getting_address(struct expression *expr)
299 struct expression *parent;
301 do {
302 parent = expr_get_parent_expr(expr);
304 if (!parent)
305 return 0;
306 if (parent->type == EXPR_PREOP && parent->op == '&')
307 return 1;
308 if (parent->type == EXPR_PREOP && parent->op == '(')
309 goto next;
310 if (parent->type == EXPR_DEREF && parent->op == '.')
311 goto next;
313 return 0;
314 next:
315 expr = parent;
316 } while (1);
319 static void handle_builtin_overflow_func(struct expression *expr)
321 struct expression *a, *b, *res, *assign;
322 int op;
324 if (sym_name_is("__builtin_add_overflow", expr->fn))
325 op = '+';
326 else if (sym_name_is("__builtin_sub_overflow", expr->fn))
327 op = '-';
328 else if (sym_name_is("__builtin_mul_overflow", expr->fn))
329 op = '*';
330 else
331 return;
333 a = get_argument_from_call_expr(expr->args, 0);
334 b = get_argument_from_call_expr(expr->args, 1);
335 res = get_argument_from_call_expr(expr->args, 2);
337 assign = assign_expression(deref_expression(res), '=', binop_expression(a, op, b));
338 __split_expr(assign);
341 static int handle__builtin_choose_expr(struct expression *expr)
343 struct expression *const_expr, *expr1, *expr2;
344 sval_t sval;
346 if (!sym_name_is("__builtin_choose_expr", expr->fn))
347 return 0;
349 const_expr = get_argument_from_call_expr(expr->args, 0);
350 expr1 = get_argument_from_call_expr(expr->args, 1);
351 expr2 = get_argument_from_call_expr(expr->args, 2);
353 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
354 return 0;
355 if (sval.value)
356 __split_expr(expr1);
357 else
358 __split_expr(expr2);
359 return 1;
362 static int handle__builtin_choose_expr_assigns(struct expression *expr)
364 struct expression *const_expr, *right, *expr1, *expr2, *fake;
365 sval_t sval;
367 right = strip_expr(expr->right);
368 if (right->type != EXPR_CALL)
369 return 0;
370 if (!sym_name_is("__builtin_choose_expr", right->fn))
371 return 0;
373 const_expr = get_argument_from_call_expr(right->args, 0);
374 expr1 = get_argument_from_call_expr(right->args, 1);
375 expr2 = get_argument_from_call_expr(right->args, 2);
377 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
378 return 0;
380 fake = assign_expression(expr->left, '=', sval.value ? expr1 : expr2);
381 __split_expr(fake);
382 return 1;
385 void __split_expr(struct expression *expr)
387 if (!expr)
388 return;
390 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
392 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
393 return;
394 if (__in_fake_assign >= 4) /* don't allow too much nesting */
395 return;
397 push_expression(&big_expression_stack, expr);
398 set_position(expr->pos);
399 __pass_to_client(expr, EXPR_HOOK);
401 switch (expr->type) {
402 case EXPR_PREOP:
403 expr_set_parent_expr(expr->unop, expr);
405 if (expr->op == '*' &&
406 !prev_expression_is_getting_address(expr))
407 __pass_to_client(expr, DEREF_HOOK);
408 __split_expr(expr->unop);
409 __pass_to_client(expr, OP_HOOK);
410 break;
411 case EXPR_POSTOP:
412 expr_set_parent_expr(expr->unop, expr);
414 __split_expr(expr->unop);
415 push_expression(&post_op_stack, expr);
416 break;
417 case EXPR_STATEMENT:
418 __expr_stmt_count++;
419 if (expr->statement && !expr->statement) {
420 stmt_set_parent_stmt(expr->statement,
421 last_ptr_list((struct ptr_list *)big_statement_stack));
423 __split_stmt(expr->statement);
424 __expr_stmt_count--;
425 break;
426 case EXPR_LOGICAL:
427 case EXPR_COMPARE:
428 expr_set_parent_expr(expr->left, expr);
429 expr_set_parent_expr(expr->right, expr);
431 __pass_to_client(expr, LOGIC_HOOK);
432 __handle_logic(expr);
433 break;
434 case EXPR_BINOP:
435 expr_set_parent_expr(expr->left, expr);
436 expr_set_parent_expr(expr->right, expr);
438 __pass_to_client(expr, BINOP_HOOK);
439 case EXPR_COMMA:
440 expr_set_parent_expr(expr->left, expr);
441 expr_set_parent_expr(expr->right, expr);
443 __split_expr(expr->left);
444 __process_post_op_stack();
445 __split_expr(expr->right);
446 break;
447 case EXPR_ASSIGNMENT: {
448 struct expression *right;
450 expr_set_parent_expr(expr->left, expr);
451 expr_set_parent_expr(expr->right, expr);
453 right = strip_expr(expr->right);
454 if (!right)
455 break;
457 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
459 /* foo = !bar() */
460 if (__handle_condition_assigns(expr))
461 goto after_assign;
462 /* foo = (x < 5 ? foo : 5); */
463 if (__handle_select_assigns(expr))
464 goto after_assign;
465 /* foo = ({frob(); frob(); frob(); 1;}) */
466 if (__handle_expr_statement_assigns(expr))
467 break; // FIXME: got after
468 /* foo = (3, 4); */
469 if (handle_comma_assigns(expr))
470 goto after_assign;
471 if (handle__builtin_choose_expr_assigns(expr))
472 goto after_assign;
473 if (handle_postop_assigns(expr))
474 break; /* no need to goto after_assign */
476 __split_expr(expr->right);
477 if (outside_of_function())
478 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
479 else
480 __pass_to_client(expr, ASSIGNMENT_HOOK);
482 __fake_struct_member_assignments(expr);
484 /* Re-examine ->right for inlines. See the commit message */
485 right = strip_expr(expr->right);
486 if (expr->op == '=' && right->type == EXPR_CALL)
487 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
489 if (get_macro_name(right->pos) &&
490 get_macro_name(expr->pos) != get_macro_name(right->pos))
491 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
493 after_assign:
494 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
495 __split_expr(expr->left);
496 break;
498 case EXPR_DEREF:
499 expr_set_parent_expr(expr->deref, expr);
501 __pass_to_client(expr, DEREF_HOOK);
502 __split_expr(expr->deref);
503 break;
504 case EXPR_SLICE:
505 expr_set_parent_expr(expr->base, expr);
507 __split_expr(expr->base);
508 break;
509 case EXPR_CAST:
510 case EXPR_FORCE_CAST:
511 expr_set_parent_expr(expr->cast_expression, expr);
513 __pass_to_client(expr, CAST_HOOK);
514 __split_expr(expr->cast_expression);
515 break;
516 case EXPR_SIZEOF:
517 if (expr->cast_expression)
518 __pass_to_client(strip_parens(expr->cast_expression),
519 SIZEOF_HOOK);
520 break;
521 case EXPR_OFFSETOF:
522 case EXPR_ALIGNOF:
523 break;
524 case EXPR_CONDITIONAL:
525 case EXPR_SELECT:
526 expr_set_parent_expr(expr->conditional, expr);
527 expr_set_parent_expr(expr->cond_true, expr);
528 expr_set_parent_expr(expr->cond_false, expr);
530 if (known_condition_true(expr->conditional)) {
531 __split_expr(expr->cond_true);
532 break;
534 if (known_condition_false(expr->conditional)) {
535 __split_expr(expr->cond_false);
536 break;
538 __pass_to_client(expr, SELECT_HOOK);
539 __split_whole_condition(expr->conditional);
540 __split_expr(expr->cond_true);
541 __push_true_states();
542 __use_false_states();
543 __split_expr(expr->cond_false);
544 __merge_true_states();
545 break;
546 case EXPR_CALL:
547 expr_set_parent_expr(expr->fn, expr);
549 if (sym_name_is("__builtin_constant_p", expr->fn))
550 break;
551 if (handle__builtin_choose_expr(expr))
552 break;
553 __split_expr(expr->fn);
554 split_args(expr);
555 if (is_inline_func(expr->fn))
556 add_inline_function(expr->fn->symbol->definition);
557 if (inlinable(expr->fn))
558 __inline_call = 1;
559 __process_post_op_stack();
560 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
561 __pass_to_client(expr, FUNCTION_CALL_HOOK);
562 __inline_call = 0;
563 if (inlinable(expr->fn))
564 parse_inline(expr);
565 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
566 if (is_noreturn_func(expr->fn))
567 nullify_path();
568 handle_builtin_overflow_func(expr);
569 break;
570 case EXPR_INITIALIZER:
571 split_expr_list(expr->expr_list, expr);
572 break;
573 case EXPR_IDENTIFIER:
574 expr_set_parent_expr(expr->ident_expression, expr);
575 __split_expr(expr->ident_expression);
576 break;
577 case EXPR_INDEX:
578 expr_set_parent_expr(expr->idx_expression, expr);
579 __split_expr(expr->idx_expression);
580 break;
581 case EXPR_POS:
582 expr_set_parent_expr(expr->init_expr, expr);
583 __split_expr(expr->init_expr);
584 break;
585 case EXPR_SYMBOL:
586 __pass_to_client(expr, SYM_HOOK);
587 break;
588 case EXPR_STRING:
589 __pass_to_client(expr, STRING_HOOK);
590 break;
591 default:
592 break;
594 __pass_to_client(expr, EXPR_HOOK_AFTER);
595 pop_expression(&big_expression_stack);
598 static int is_forever_loop(struct statement *stmt)
600 struct expression *expr;
601 sval_t sval;
603 expr = strip_expr(stmt->iterator_pre_condition);
604 if (!expr)
605 expr = stmt->iterator_post_condition;
606 if (!expr) {
607 /* this is a for(;;) loop... */
608 return 1;
611 if (get_value(expr, &sval) && sval.value != 0)
612 return 1;
614 return 0;
617 static int loop_num;
618 static char *get_loop_name(int num)
620 char buf[256];
622 snprintf(buf, 255, "-loop%d", num);
623 buf[255] = '\0';
624 return alloc_sname(buf);
628 * Pre Loops are while and for loops.
630 static void handle_pre_loop(struct statement *stmt)
632 int once_through; /* we go through the loop at least once */
633 struct sm_state *extra_sm = NULL;
634 int unchanged = 0;
635 char *loop_name;
636 struct stree *stree = NULL;
637 struct sm_state *sm = NULL;
639 loop_name = get_loop_name(loop_num);
640 loop_num++;
642 __split_stmt(stmt->iterator_pre_statement);
643 __prev_stmt = stmt->iterator_pre_statement;
645 once_through = implied_condition_true(stmt->iterator_pre_condition);
647 loop_count++;
648 __push_continues();
649 __push_breaks();
651 __merge_gotos(loop_name, NULL);
653 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
654 __in_pre_condition++;
655 __pass_to_client(stmt, PRELOOP_HOOK);
656 __split_whole_condition(stmt->iterator_pre_condition);
657 __in_pre_condition--;
658 FOR_EACH_SM(stree, sm) {
659 set_state(sm->owner, sm->name, sm->sym, sm->state);
660 } END_FOR_EACH_SM(sm);
661 free_stree(&stree);
662 if (extra_sm)
663 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
665 if (option_assume_loops)
666 once_through = 1;
668 __split_stmt(stmt->iterator_statement);
669 if (is_forever_loop(stmt)) {
670 __merge_continues();
671 __save_gotos(loop_name, NULL);
673 __push_fake_cur_stree();
674 __split_stmt(stmt->iterator_post_statement);
675 stree = __pop_fake_cur_stree();
677 __discard_false_states();
678 __use_breaks();
680 if (!__path_is_null())
681 __merge_stree_into_cur(stree);
682 free_stree(&stree);
683 } else {
684 __merge_continues();
685 unchanged = __iterator_unchanged(extra_sm);
686 __split_stmt(stmt->iterator_post_statement);
687 __prev_stmt = stmt->iterator_post_statement;
688 __cur_stmt = stmt;
690 __save_gotos(loop_name, NULL);
691 __in_pre_condition++;
692 __split_whole_condition(stmt->iterator_pre_condition);
693 __in_pre_condition--;
694 nullify_path();
695 __merge_false_states();
696 if (once_through)
697 __discard_false_states();
698 else
699 __merge_false_states();
701 if (extra_sm && unchanged)
702 __extra_pre_loop_hook_after(extra_sm,
703 stmt->iterator_post_statement,
704 stmt->iterator_pre_condition);
705 __merge_breaks();
707 loop_count--;
711 * Post loops are do {} while();
713 static void handle_post_loop(struct statement *stmt)
715 char *loop_name;
717 loop_name = get_loop_name(loop_num);
718 loop_num++;
719 loop_count++;
721 __push_continues();
722 __push_breaks();
723 __merge_gotos(loop_name, NULL);
724 __split_stmt(stmt->iterator_statement);
725 __merge_continues();
726 if (!expr_is_zero(stmt->iterator_post_condition))
727 __save_gotos(loop_name, NULL);
729 if (is_forever_loop(stmt)) {
730 __use_breaks();
731 } else {
732 __split_whole_condition(stmt->iterator_post_condition);
733 __use_false_states();
734 __merge_breaks();
736 loop_count--;
739 static int empty_statement(struct statement *stmt)
741 if (!stmt)
742 return 0;
743 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
744 return 1;
745 return 0;
748 static int last_stmt_on_same_line(void)
750 struct statement *stmt;
751 int i = 0;
753 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
754 if (!i++)
755 continue;
756 if (stmt->pos.line == get_lineno())
757 return 1;
758 return 0;
759 } END_FOR_EACH_PTR_REVERSE(stmt);
760 return 0;
763 static void split_asm_ops(struct asm_operand_list *ops)
765 struct asm_operand *op;
767 FOR_EACH_PTR(ops, op) {
768 __split_expr(op->expr);
769 } END_FOR_EACH_PTR(op);
772 static int is_case_val(struct statement *stmt, sval_t sval)
774 sval_t case_sval;
776 if (stmt->type != STMT_CASE)
777 return 0;
778 if (!stmt->case_expression) {
779 __set_default();
780 return 1;
782 if (!get_value(stmt->case_expression, &case_sval))
783 return 0;
784 if (case_sval.value == sval.value)
785 return 1;
786 return 0;
789 static struct range_list *get_case_rl(struct expression *switch_expr,
790 struct expression *case_expr,
791 struct expression *case_to)
793 sval_t start, end;
794 struct range_list *rl = NULL;
795 struct symbol *switch_type;
797 switch_type = get_type(switch_expr);
798 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
799 start = sval_cast(switch_type, start);
800 end = sval_cast(switch_type, end);
801 add_range(&rl, start, end);
802 } else if (get_value(case_expr, &start)) {
803 start = sval_cast(switch_type, start);
804 add_range(&rl, start, start);
807 return rl;
810 static void split_known_switch(struct statement *stmt, sval_t sval)
812 struct statement *tmp;
813 struct range_list *rl;
815 __split_expr(stmt->switch_expression);
816 sval = sval_cast(get_type(stmt->switch_expression), sval);
818 push_expression(&switch_expr_stack, stmt->switch_expression);
819 __save_switch_states(top_expression(switch_expr_stack));
820 nullify_path();
821 __push_default();
822 __push_breaks();
824 stmt = stmt->switch_statement;
826 __push_scope_hooks();
827 FOR_EACH_PTR(stmt->stmts, tmp) {
828 __smatch_lineno = tmp->pos.line;
829 if (is_case_val(tmp, sval)) {
830 rl = alloc_rl(sval, sval);
831 __merge_switches(top_expression(switch_expr_stack), rl);
832 __pass_case_to_client(top_expression(switch_expr_stack), rl);
834 if (__path_is_null())
835 continue;
836 __split_stmt(tmp);
837 if (__path_is_null()) {
838 __set_default();
839 goto out;
841 } END_FOR_EACH_PTR(tmp);
842 out:
843 __call_scope_hooks();
844 if (!__pop_default())
845 __merge_switches(top_expression(switch_expr_stack), NULL);
846 __discard_switches();
847 __merge_breaks();
848 pop_expression(&switch_expr_stack);
851 static void split_case(struct statement *stmt)
853 struct range_list *rl = NULL;
855 expr_set_parent_stmt(stmt->case_expression, stmt);
856 expr_set_parent_stmt(stmt->case_to, stmt);
858 rl = get_case_rl(top_expression(switch_expr_stack),
859 stmt->case_expression, stmt->case_to);
860 while (stmt->case_statement->type == STMT_CASE) {
861 struct range_list *tmp;
863 tmp = get_case_rl(top_expression(switch_expr_stack),
864 stmt->case_statement->case_expression,
865 stmt->case_statement->case_to);
866 if (!tmp)
867 break;
868 rl = rl_union(rl, tmp);
869 if (!stmt->case_expression)
870 __set_default();
871 stmt = stmt->case_statement;
874 __merge_switches(top_expression(switch_expr_stack), rl);
876 if (!stmt->case_expression)
877 __set_default();
878 __split_stmt(stmt->case_statement);
881 int time_parsing_function(void)
883 return ms_since(&fn_start_time) / 1000;
886 bool taking_too_long(void)
888 if ((ms_since(&outer_fn_start_time) / 1000) > 60 * 5) /* five minutes */
889 return 1;
890 return 0;
893 static int is_last_stmt(struct statement *cur_stmt)
895 struct symbol *fn;
896 struct statement *stmt;
898 if (!cur_func_sym)
899 return 0;
900 fn = get_base_type(cur_func_sym);
901 if (!fn)
902 return 0;
903 stmt = fn->stmt;
904 if (!stmt)
905 stmt = fn->inline_stmt;
906 if (!stmt || stmt->type != STMT_COMPOUND)
907 return 0;
908 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
909 if (stmt && stmt->type == STMT_LABEL)
910 stmt = stmt->label_statement;
911 if (stmt == cur_stmt)
912 return 1;
913 return 0;
916 static void handle_backward_goto(struct statement *goto_stmt)
918 const char *goto_name, *label_name;
919 struct statement *func_stmt;
920 struct symbol *base_type = get_base_type(cur_func_sym);
921 struct statement *tmp;
922 int found = 0;
924 if (!option_info)
925 return;
926 if (last_goto_statement_handled)
927 return;
928 last_goto_statement_handled = 1;
930 if (!goto_stmt->goto_label ||
931 goto_stmt->goto_label->type != SYM_LABEL ||
932 !goto_stmt->goto_label->ident)
933 return;
934 goto_name = goto_stmt->goto_label->ident->name;
936 func_stmt = base_type->stmt;
937 if (!func_stmt)
938 func_stmt = base_type->inline_stmt;
939 if (!func_stmt)
940 return;
941 if (func_stmt->type != STMT_COMPOUND)
942 return;
944 FOR_EACH_PTR(func_stmt->stmts, tmp) {
945 if (!found) {
946 if (tmp->type != STMT_LABEL)
947 continue;
948 if (!tmp->label_identifier ||
949 tmp->label_identifier->type != SYM_LABEL ||
950 !tmp->label_identifier->ident)
951 continue;
952 label_name = tmp->label_identifier->ident->name;
953 if (strcmp(goto_name, label_name) != 0)
954 continue;
955 found = 1;
957 __split_stmt(tmp);
958 } END_FOR_EACH_PTR(tmp);
961 static void fake_a_return(void)
963 struct symbol *return_type;
965 nullify_path();
966 __unnullify_path();
968 return_type = get_real_base_type(cur_func_sym);
969 return_type = get_real_base_type(return_type);
970 if (return_type != &void_ctype) {
971 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
972 nullify_path();
976 static void split_ret_value(struct expression *expr)
978 struct symbol *type;
980 if (!expr)
981 return;
983 type = get_real_base_type(cur_func_sym);
984 type = get_real_base_type(type);
985 expr = fake_a_variable_assign(type, expr);
987 __in_fake_var_assign++;
988 __split_expr(expr);
989 __in_fake_var_assign--;
992 static void fake_an_empty_default(struct position pos)
994 static struct statement none = {};
996 none.pos = pos;
997 none.type = STMT_NONE;
998 __merge_switches(top_expression(switch_expr_stack), NULL);
999 __split_stmt(&none);
1002 static void split_compound(struct statement *stmt)
1004 struct statement *prev = NULL;
1005 struct statement *cur = NULL;
1006 struct statement *next;
1008 __push_scope_hooks();
1010 FOR_EACH_PTR(stmt->stmts, next) {
1011 /* just set them all ahead of time */
1012 stmt_set_parent_stmt(next, stmt);
1014 if (cur) {
1015 __prev_stmt = prev;
1016 __next_stmt = next;
1017 __cur_stmt = cur;
1018 __split_stmt(cur);
1020 prev = cur;
1021 cur = next;
1022 } END_FOR_EACH_PTR(next);
1023 if (cur) {
1024 __prev_stmt = prev;
1025 __cur_stmt = cur;
1026 __next_stmt = NULL;
1027 __split_stmt(cur);
1031 * For function scope, then delay calling the scope hooks until the
1032 * end of function hooks can run. I'm not positive this is the right
1033 * thing...
1035 if (!is_last_stmt(cur))
1036 __call_scope_hooks();
1040 * This is a hack, work around for detecting empty functions.
1042 static int need_delayed_scope_hooks(void)
1044 struct symbol *fn = get_base_type(cur_func_sym);
1045 struct statement *stmt;
1047 if (!fn)
1048 return 0;
1049 stmt = fn->stmt;
1050 if (!stmt)
1051 stmt = fn->inline_stmt;
1052 if (stmt && stmt->type == STMT_COMPOUND)
1053 return 1;
1054 return 0;
1057 void __split_label_stmt(struct statement *stmt)
1059 if (stmt->label_identifier &&
1060 stmt->label_identifier->type == SYM_LABEL &&
1061 stmt->label_identifier->ident) {
1062 loop_count |= 0x0800000;
1063 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
1067 static void find_asm_gotos(struct statement *stmt)
1069 struct symbol *sym;
1071 FOR_EACH_PTR(stmt->asm_labels, sym) {
1072 __save_gotos(sym->ident->name, sym);
1073 } END_FOR_EACH_PTR(sym);
1076 void __split_stmt(struct statement *stmt)
1078 static int indent_cnt;
1079 sval_t sval;
1081 if (!stmt)
1082 goto out;
1084 if (!__in_fake_assign)
1085 __silence_warnings_for_stmt = false;
1087 if (__bail_on_rest_of_function || is_skipped_function())
1088 return;
1090 if (out_of_memory() || taking_too_long()) {
1091 struct timeval stop;
1093 gettimeofday(&stop, NULL);
1095 __bail_on_rest_of_function = 1;
1096 final_pass = 1;
1097 sm_perror("Function too hairy. Giving up. %lu seconds",
1098 stop.tv_sec - fn_start_time.tv_sec);
1099 fake_a_return();
1100 final_pass = 0; /* turn off sm_msg() from here */
1101 return;
1104 indent_cnt++;
1106 add_ptr_list(&big_statement_stack, stmt);
1107 free_expression_stack(&big_expression_stack);
1108 set_position(stmt->pos);
1109 __pass_to_client(stmt, STMT_HOOK);
1111 switch (stmt->type) {
1112 case STMT_DECLARATION:
1113 split_declaration(stmt->declaration);
1114 break;
1115 case STMT_RETURN:
1116 expr_set_parent_stmt(stmt->ret_value, stmt);
1118 split_ret_value(stmt->ret_value);
1119 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1120 __process_post_op_stack();
1121 nullify_path();
1122 break;
1123 case STMT_EXPRESSION:
1124 expr_set_parent_stmt(stmt->expression, stmt);
1125 expr_set_parent_stmt(stmt->context, stmt);
1127 __split_expr(stmt->expression);
1128 break;
1129 case STMT_COMPOUND:
1130 split_compound(stmt);
1131 break;
1132 case STMT_IF:
1133 stmt_set_parent_stmt(stmt->if_true, stmt);
1134 stmt_set_parent_stmt(stmt->if_false, stmt);
1135 expr_set_parent_stmt(stmt->if_conditional, stmt);
1137 if (known_condition_true(stmt->if_conditional)) {
1138 __split_stmt(stmt->if_true);
1139 break;
1141 if (known_condition_false(stmt->if_conditional)) {
1142 __split_stmt(stmt->if_false);
1143 break;
1145 __split_whole_condition(stmt->if_conditional);
1146 __split_stmt(stmt->if_true);
1147 if (empty_statement(stmt->if_true) &&
1148 last_stmt_on_same_line() &&
1149 !get_macro_name(stmt->if_true->pos))
1150 sm_warning("if();");
1151 __push_true_states();
1152 __use_false_states();
1153 __split_stmt(stmt->if_false);
1154 __merge_true_states();
1155 break;
1156 case STMT_ITERATOR:
1157 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1158 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1159 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1160 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1161 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1163 if (stmt->iterator_pre_condition)
1164 handle_pre_loop(stmt);
1165 else if (stmt->iterator_post_condition)
1166 handle_post_loop(stmt);
1167 else {
1168 // these are for(;;) type loops.
1169 handle_pre_loop(stmt);
1171 break;
1172 case STMT_SWITCH:
1173 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1174 expr_set_parent_stmt(stmt->switch_expression, stmt);
1176 if (get_value(stmt->switch_expression, &sval)) {
1177 split_known_switch(stmt, sval);
1178 break;
1180 __split_expr(stmt->switch_expression);
1181 push_expression(&switch_expr_stack, stmt->switch_expression);
1182 __save_switch_states(top_expression(switch_expr_stack));
1183 nullify_path();
1184 __push_default();
1185 __push_breaks();
1186 __split_stmt(stmt->switch_statement);
1187 if (!__pop_default() && have_remaining_cases())
1188 fake_an_empty_default(stmt->pos);
1189 __discard_switches();
1190 __merge_breaks();
1191 pop_expression(&switch_expr_stack);
1192 break;
1193 case STMT_CASE:
1194 split_case(stmt);
1195 break;
1196 case STMT_LABEL:
1197 __split_label_stmt(stmt);
1198 __split_stmt(stmt->label_statement);
1199 break;
1200 case STMT_GOTO:
1201 expr_set_parent_stmt(stmt->goto_expression, stmt);
1203 __split_expr(stmt->goto_expression);
1204 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1205 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1206 __process_breaks();
1207 } else if (!strcmp(stmt->goto_label->ident->name,
1208 "continue")) {
1209 __process_continues();
1211 } else if (stmt->goto_label &&
1212 stmt->goto_label->type == SYM_LABEL &&
1213 stmt->goto_label->ident) {
1214 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1216 nullify_path();
1217 if (is_last_stmt(stmt))
1218 handle_backward_goto(stmt);
1219 break;
1220 case STMT_NONE:
1221 break;
1222 case STMT_ASM:
1223 expr_set_parent_stmt(stmt->asm_string, stmt);
1225 find_asm_gotos(stmt);
1226 __pass_to_client(stmt, ASM_HOOK);
1227 __split_expr(stmt->asm_string);
1228 split_asm_ops(stmt->asm_outputs);
1229 split_asm_ops(stmt->asm_inputs);
1230 split_expr_list(stmt->asm_clobbers, NULL);
1231 break;
1232 case STMT_CONTEXT:
1233 break;
1234 case STMT_RANGE:
1235 __split_expr(stmt->range_expression);
1236 __split_expr(stmt->range_low);
1237 __split_expr(stmt->range_high);
1238 break;
1240 __pass_to_client(stmt, STMT_HOOK_AFTER);
1241 if (--indent_cnt == 0)
1242 __discard_fake_states();
1243 out:
1244 __process_post_op_stack();
1247 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1249 struct expression *expr;
1251 FOR_EACH_PTR(expr_list, expr) {
1252 expr_set_parent_expr(expr, parent);
1253 __split_expr(expr);
1254 __process_post_op_stack();
1255 } END_FOR_EACH_PTR(expr);
1258 static bool cast_arg(struct symbol *type, struct expression *arg)
1260 struct symbol *orig;
1262 if (!type)
1263 return false;
1265 arg = strip_parens(arg);
1266 if (arg != strip_expr(arg))
1267 return true;
1269 orig = get_type(arg);
1270 if (!orig)
1271 return true;
1272 if (orig == type)
1273 return false;
1276 * I would have expected that we could just do use (orig == type) but I
1277 * guess for pointers we need to get the basetype to do that comparison.
1281 if (orig->type != SYM_PTR ||
1282 type->type != SYM_PTR) {
1283 if (type_fits(type, orig))
1284 return false;
1285 return true;
1287 orig = get_real_base_type(orig);
1288 type = get_real_base_type(type);
1289 if (orig == type)
1290 return false;
1292 return true;
1295 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *expr)
1297 struct expression *var, *assign, *parent;
1298 char buf[64];
1299 sval_t sval;
1300 bool cast;
1302 if (!expr)
1303 return NULL;
1305 if (expr->type == EXPR_ASSIGNMENT)
1306 return expr;
1308 cast = cast_arg(type, expr);
1310 * Using expr_to_sym() here is a hack. We want to say that we don't
1311 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1312 * It turns out faking these assignments is way more expensive than I
1313 * would have imagined. I'm not sure why exactly.
1316 if (!cast) {
1318 * if the code is "return *p;" where "p" is a user pointer then
1319 * we want to create a fake assignment so that it sets the state
1320 * in check_kernel_user_data.c.
1323 if (expr->type != EXPR_PREOP &&
1324 expr->op != '*' && expr->op != '&' &&
1325 expr_to_sym(expr))
1326 return expr;
1327 if (get_value(expr, &sval))
1328 return expr;
1331 snprintf(buf, sizeof(buf), "__sm_fake_%p", expr);
1332 var = fake_variable(type, buf);
1333 assign = assign_expression(var, '=', expr);
1334 assign->smatch_flags |= Fake;
1336 parent = expr_get_parent_expr(expr);
1337 expr_set_parent_expr(assign, parent);
1338 expr_set_parent_expr(expr, assign);
1340 __fake_state_cnt++;
1342 return assign;
1345 static void split_args(struct expression *expr)
1347 struct expression *arg, *tmp;
1348 struct symbol *type;
1349 int i;
1351 i = -1;
1352 FOR_EACH_PTR(expr->args, arg) {
1353 i++;
1354 expr_set_parent_expr(arg, expr);
1355 type = get_arg_type(expr->fn, i);
1356 tmp = fake_a_variable_assign(type, arg);
1357 if (tmp != arg)
1358 __in_fake_var_assign++;
1359 __split_expr(tmp);
1360 if (tmp != arg)
1361 __in_fake_var_assign--;
1362 __process_post_op_stack();
1363 } END_FOR_EACH_PTR(arg);
1366 static void split_sym(struct symbol *sym)
1368 if (!sym)
1369 return;
1370 if (!(sym->namespace & NS_SYMBOL))
1371 return;
1373 __split_stmt(sym->stmt);
1374 __split_expr(sym->array_size);
1375 split_symlist(sym->arguments);
1376 split_symlist(sym->symbol_list);
1377 __split_stmt(sym->inline_stmt);
1378 split_symlist(sym->inline_symbol_list);
1381 static void split_symlist(struct symbol_list *sym_list)
1383 struct symbol *sym;
1385 FOR_EACH_PTR(sym_list, sym) {
1386 split_sym(sym);
1387 } END_FOR_EACH_PTR(sym);
1390 typedef void (fake_cb)(struct expression *expr);
1392 static int member_to_number(struct expression *expr, struct ident *member)
1394 struct symbol *type, *tmp;
1395 char *name;
1396 int i;
1398 if (!member)
1399 return -1;
1400 name = member->name;
1402 type = get_type(expr);
1403 if (!type || type->type != SYM_STRUCT)
1404 return -1;
1406 i = -1;
1407 FOR_EACH_PTR(type->symbol_list, tmp) {
1408 i++;
1409 if (!tmp->ident)
1410 continue;
1411 if (strcmp(name, tmp->ident->name) == 0)
1412 return i;
1413 } END_FOR_EACH_PTR(tmp);
1414 return -1;
1417 static struct ident *number_to_member(struct expression *expr, int num)
1419 struct symbol *type, *member;
1420 int i = 0;
1422 type = get_type(expr);
1423 if (!type || type->type != SYM_STRUCT)
1424 return NULL;
1426 FOR_EACH_PTR(type->symbol_list, member) {
1427 if (i == num)
1428 return member->ident;
1429 i++;
1430 } END_FOR_EACH_PTR(member);
1431 return NULL;
1434 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1436 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1438 struct expression *edge_member, *assign;
1439 struct symbol *base = get_real_base_type(member);
1440 struct symbol *tmp;
1442 if (member->ident)
1443 expr = member_expression(expr, '.', member->ident);
1445 FOR_EACH_PTR(base->symbol_list, tmp) {
1446 struct symbol *type;
1448 type = get_real_base_type(tmp);
1449 if (!type)
1450 continue;
1452 edge_member = member_expression(expr, '.', tmp->ident);
1453 if (get_extra_state(edge_member))
1454 continue;
1456 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1457 set_inner_struct_members(expr, tmp);
1458 continue;
1461 if (!tmp->ident)
1462 continue;
1464 assign = assign_expression(edge_member, '=', zero_expr());
1465 __split_expr(assign);
1466 } END_FOR_EACH_PTR(tmp);
1471 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1473 struct symbol *tmp;
1474 struct expression *member = NULL;
1475 struct expression *assign;
1476 int op = '*';
1478 if (expr->type == EXPR_PREOP && expr->op == '&') {
1479 expr = strip_expr(expr->unop);
1480 op = '.';
1483 FOR_EACH_PTR(type->symbol_list, tmp) {
1484 type = get_real_base_type(tmp);
1485 if (!type)
1486 continue;
1488 if (tmp->ident) {
1489 member = member_expression(expr, op, tmp->ident);
1490 if (get_extra_state(member))
1491 continue;
1494 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1495 set_inner_struct_members(expr, tmp);
1496 continue;
1498 if (type->type == SYM_ARRAY)
1499 continue;
1500 if (!tmp->ident)
1501 continue;
1503 assign = assign_expression(member, '=', zero_expr());
1504 __split_expr(assign);
1505 } END_FOR_EACH_PTR(tmp);
1508 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1510 struct expression *deref, *assign, *tmp, *right;
1511 struct symbol *struct_type, *type;
1512 struct ident *member;
1513 int member_idx;
1515 struct_type = get_type(symbol);
1516 if (!struct_type ||
1517 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1518 return;
1521 * We're parsing an initializer that could look something like this:
1522 * struct foo foo = {
1523 * 42,
1524 * .whatever.xxx = 11,
1525 * .zzz = 12,
1526 * };
1528 * So what we have here is a list with 42, .whatever, and .zzz. We need
1529 * to break it up into left and right sides of the assignments.
1532 member_idx = 0;
1533 FOR_EACH_PTR(members, tmp) {
1534 deref = NULL;
1535 if (tmp->type == EXPR_IDENTIFIER) {
1536 member_idx = member_to_number(symbol, tmp->expr_ident);
1537 while (tmp->type == EXPR_IDENTIFIER) {
1538 member = tmp->expr_ident;
1539 tmp = tmp->ident_expression;
1540 if (deref)
1541 deref = member_expression(deref, '.', member);
1542 else
1543 deref = member_expression(symbol, '.', member);
1545 } else {
1546 member = number_to_member(symbol, member_idx);
1547 deref = member_expression(symbol, '.', member);
1549 right = tmp;
1550 member_idx++;
1551 if (right->type == EXPR_INITIALIZER) {
1552 type = get_type(deref);
1553 if (type && type->type == SYM_ARRAY)
1554 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1555 else
1556 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1557 } else {
1558 assign = assign_expression(deref, '=', right);
1559 fake_cb(assign);
1561 } END_FOR_EACH_PTR(tmp);
1563 set_unset_to_zero(struct_type, symbol);
1566 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1568 fake_member_assigns_helper(symbol_expression(sym),
1569 sym->initializer->expr_list, fake_cb);
1572 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1574 struct expression *offset, *binop, *assign, *tmp;
1575 struct symbol *type;
1576 int idx;
1578 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1579 return;
1581 idx = 0;
1582 FOR_EACH_PTR(expr_list, tmp) {
1583 if (tmp->type == EXPR_INDEX) {
1584 if (tmp->idx_from != tmp->idx_to)
1585 return;
1586 idx = tmp->idx_from;
1587 if (!tmp->idx_expression)
1588 goto next;
1589 tmp = tmp->idx_expression;
1591 offset = value_expr(idx);
1592 binop = array_element_expression(array, offset);
1593 if (tmp->type == EXPR_INITIALIZER) {
1594 type = get_type(binop);
1595 if (type && type->type == SYM_ARRAY)
1596 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1597 else
1598 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1599 } else {
1600 assign = assign_expression(binop, '=', tmp);
1601 fake_cb(assign);
1603 next:
1604 idx++;
1605 } END_FOR_EACH_PTR(tmp);
1608 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1610 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1613 static void fake_assign_expr(struct symbol *sym)
1615 struct expression *assign, *symbol;
1617 symbol = symbol_expression(sym);
1618 assign = assign_expression(symbol, '=', sym->initializer);
1619 __split_expr(assign);
1622 static void do_initializer_stuff(struct symbol *sym)
1624 if (!sym->initializer)
1625 return;
1627 if (sym->initializer->type == EXPR_INITIALIZER) {
1628 if (get_real_base_type(sym)->type == SYM_ARRAY)
1629 fake_element_assigns(sym, __split_expr);
1630 else
1631 fake_member_assigns(sym, __split_expr);
1632 } else {
1633 fake_assign_expr(sym);
1637 static void split_declaration(struct symbol_list *sym_list)
1639 struct symbol *sym;
1641 FOR_EACH_PTR(sym_list, sym) {
1642 __pass_to_client(sym, DECLARATION_HOOK);
1643 do_initializer_stuff(sym);
1644 split_sym(sym);
1645 } END_FOR_EACH_PTR(sym);
1648 static void call_global_assign_hooks(struct expression *assign)
1650 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1653 static void fake_global_assign(struct symbol *sym)
1655 struct expression *assign, *symbol;
1657 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1658 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1659 fake_element_assigns(sym, call_global_assign_hooks);
1660 } else if (sym->initializer) {
1661 symbol = symbol_expression(sym);
1662 assign = assign_expression(symbol, '=', sym->initializer);
1663 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1664 } else {
1665 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1667 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1668 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1669 fake_member_assigns(sym, call_global_assign_hooks);
1670 } else if (sym->initializer) {
1671 symbol = symbol_expression(sym);
1672 assign = assign_expression(symbol, '=', sym->initializer);
1673 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1674 } else {
1675 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1677 } else {
1678 symbol = symbol_expression(sym);
1679 if (sym->initializer) {
1680 assign = assign_expression(symbol, '=', sym->initializer);
1681 __split_expr(assign);
1682 } else {
1683 assign = assign_expression(symbol, '=', zero_expr());
1685 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1689 static void start_function_definition(struct symbol *sym)
1691 __in_function_def = 1;
1692 __pass_to_client(sym, FUNC_DEF_HOOK);
1693 __in_function_def = 0;
1694 __pass_to_client(sym, AFTER_DEF_HOOK);
1698 static void split_function(struct symbol *sym)
1700 struct symbol *base_type = get_base_type(sym);
1701 struct timeval stop;
1703 if (!base_type->stmt && !base_type->inline_stmt)
1704 return;
1706 gettimeofday(&outer_fn_start_time, NULL);
1707 gettimeofday(&fn_start_time, NULL);
1708 cur_func_sym = sym;
1709 if (sym->ident)
1710 cur_func = sym->ident->name;
1711 set_position(sym->pos);
1712 loop_count = 0;
1713 last_goto_statement_handled = 0;
1714 sm_debug("new function: %s\n", cur_func);
1715 __stree_id = 0;
1716 if (option_two_passes) {
1717 __unnullify_path();
1718 loop_num = 0;
1719 final_pass = 0;
1720 start_function_definition(sym);
1721 __split_stmt(base_type->stmt);
1722 __split_stmt(base_type->inline_stmt);
1723 nullify_path();
1725 __unnullify_path();
1726 loop_num = 0;
1727 final_pass = 1;
1728 start_function_definition(sym);
1729 __split_stmt(base_type->stmt);
1730 __split_stmt(base_type->inline_stmt);
1731 __pass_to_client(sym, END_FUNC_HOOK);
1732 if (need_delayed_scope_hooks())
1733 __call_scope_hooks();
1734 __pass_to_client(sym, AFTER_FUNC_HOOK);
1736 clear_all_states();
1738 gettimeofday(&stop, NULL);
1739 if (option_time && stop.tv_sec - fn_start_time.tv_sec > 2) {
1740 final_pass++;
1741 sm_msg("func_time: %lu", stop.tv_sec - fn_start_time.tv_sec);
1742 final_pass--;
1744 cur_func_sym = NULL;
1745 cur_func = NULL;
1746 free_data_info_allocs();
1747 free_expression_stack(&switch_expr_stack);
1748 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1749 __bail_on_rest_of_function = 0;
1752 static void save_flow_state(void)
1754 __add_ptr_list(&backup, INT_PTR(loop_num << 2));
1755 __add_ptr_list(&backup, INT_PTR(loop_count << 2));
1756 __add_ptr_list(&backup, INT_PTR(final_pass << 2));
1758 __add_ptr_list(&backup, big_statement_stack);
1759 __add_ptr_list(&backup, big_expression_stack);
1760 __add_ptr_list(&backup, big_condition_stack);
1761 __add_ptr_list(&backup, switch_expr_stack);
1763 __add_ptr_list(&backup, cur_func_sym);
1765 __add_ptr_list(&backup, __prev_stmt);
1766 __add_ptr_list(&backup, __cur_stmt);
1767 __add_ptr_list(&backup, __next_stmt);
1770 static void *pop_backup(void)
1772 void *ret;
1774 ret = last_ptr_list(backup);
1775 delete_ptr_list_last(&backup);
1776 return ret;
1779 static void restore_flow_state(void)
1781 __next_stmt = pop_backup();
1782 __cur_stmt = pop_backup();
1783 __prev_stmt = pop_backup();
1785 cur_func_sym = pop_backup();
1786 switch_expr_stack = pop_backup();
1787 big_condition_stack = pop_backup();
1788 big_expression_stack = pop_backup();
1789 big_statement_stack = pop_backup();
1790 final_pass = PTR_INT(pop_backup()) >> 2;
1791 loop_count = PTR_INT(pop_backup()) >> 2;
1792 loop_num = PTR_INT(pop_backup()) >> 2;
1795 static void parse_inline(struct expression *call)
1797 struct symbol *base_type;
1798 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
1799 struct timeval time_backup = fn_start_time;
1800 struct expression *orig_inline = __inline_fn;
1801 int orig_budget;
1803 if (out_of_memory() || taking_too_long())
1804 return;
1806 save_flow_state();
1808 __pass_to_client(call, INLINE_FN_START);
1809 final_pass = 0; /* don't print anything */
1810 __inline_fn = call;
1811 orig_budget = inline_budget;
1812 inline_budget = inline_budget - 5;
1814 base_type = get_base_type(call->fn->symbol);
1815 cur_func_sym = call->fn->symbol;
1816 if (call->fn->symbol->ident)
1817 cur_func = call->fn->symbol->ident->name;
1818 else
1819 cur_func = NULL;
1820 set_position(call->fn->symbol->pos);
1822 save_all_states();
1823 big_statement_stack = NULL;
1824 big_expression_stack = NULL;
1825 big_condition_stack = NULL;
1826 switch_expr_stack = NULL;
1828 sm_debug("inline function: %s\n", cur_func);
1829 __unnullify_path();
1830 loop_num = 0;
1831 loop_count = 0;
1832 start_function_definition(call->fn->symbol);
1833 __split_stmt(base_type->stmt);
1834 __split_stmt(base_type->inline_stmt);
1835 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1836 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1838 free_expression_stack(&switch_expr_stack);
1839 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1840 nullify_path();
1841 free_goto_stack();
1843 restore_flow_state();
1844 fn_start_time = time_backup;
1845 cur_func = cur_func_bak;
1847 restore_all_states();
1848 set_position(call->pos);
1849 __inline_fn = orig_inline;
1850 inline_budget = orig_budget;
1851 __pass_to_client(call, INLINE_FN_END);
1854 static struct symbol_list *inlines_called;
1855 static void add_inline_function(struct symbol *sym)
1857 static struct symbol_list *already_added;
1858 struct symbol *tmp;
1860 FOR_EACH_PTR(already_added, tmp) {
1861 if (tmp == sym)
1862 return;
1863 } END_FOR_EACH_PTR(tmp);
1865 add_ptr_list(&already_added, sym);
1866 add_ptr_list(&inlines_called, sym);
1869 static void process_inlines(void)
1871 struct symbol *tmp;
1873 FOR_EACH_PTR(inlines_called, tmp) {
1874 split_function(tmp);
1875 } END_FOR_EACH_PTR(tmp);
1876 free_ptr_list(&inlines_called);
1879 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1881 struct symbol *sym;
1883 FOR_EACH_PTR_REVERSE(big_list, sym) {
1884 if (!sym->scope)
1885 continue;
1886 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1887 return sym;
1888 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1889 return sym;
1890 } END_FOR_EACH_PTR_REVERSE(sym);
1892 return NULL;
1895 static bool interesting_function(struct symbol *sym)
1897 static int prev_stream = -1;
1898 static bool prev_answer;
1899 const char *filename;
1900 int len;
1902 if (!(sym->ctype.modifiers & MOD_INLINE))
1903 return true;
1905 if (sym->pos.stream == prev_stream)
1906 return prev_answer;
1908 prev_stream = sym->pos.stream;
1909 prev_answer = false;
1911 filename = stream_name(sym->pos.stream);
1912 len = strlen(filename);
1913 if (len > 0 && filename[len - 1] == 'c')
1914 prev_answer = true;
1915 return prev_answer;
1918 static void split_inlines_in_scope(struct symbol *sym)
1920 struct symbol *base;
1921 struct symbol_list *scope_list;
1922 int stream;
1924 scope_list = sym->scope->symbols;
1925 stream = sym->pos.stream;
1927 /* find the last static symbol in the file */
1928 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1929 if (sym->pos.stream != stream)
1930 continue;
1931 if (sym->type != SYM_NODE)
1932 continue;
1933 base = get_base_type(sym);
1934 if (!base)
1935 continue;
1936 if (base->type != SYM_FN)
1937 continue;
1938 if (!base->inline_stmt)
1939 continue;
1940 if (!interesting_function(sym))
1941 continue;
1942 add_inline_function(sym);
1943 } END_FOR_EACH_PTR_REVERSE(sym);
1945 process_inlines();
1948 static void split_inlines(struct symbol_list *sym_list)
1950 struct symbol *sym;
1952 sym = get_last_scoped_symbol(sym_list, 0);
1953 if (sym)
1954 split_inlines_in_scope(sym);
1955 sym = get_last_scoped_symbol(sym_list, 1);
1956 if (sym)
1957 split_inlines_in_scope(sym);
1960 static struct stree *clone_estates_perm(struct stree *orig)
1962 struct stree *ret = NULL;
1963 struct sm_state *tmp;
1965 FOR_EACH_SM(orig, tmp) {
1966 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1967 } END_FOR_EACH_SM(tmp);
1969 return ret;
1972 struct position last_pos;
1973 static void split_c_file_functions(struct symbol_list *sym_list)
1975 struct symbol *sym;
1977 __unnullify_path();
1978 FOR_EACH_PTR(sym_list, sym) {
1979 set_position(sym->pos);
1980 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1981 __pass_to_client(sym, BASE_HOOK);
1982 fake_global_assign(sym);
1984 } END_FOR_EACH_PTR(sym);
1985 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1986 nullify_path();
1988 FOR_EACH_PTR(sym_list, sym) {
1989 set_position(sym->pos);
1990 last_pos = sym->pos;
1991 if (!interesting_function(sym))
1992 continue;
1993 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1994 split_function(sym);
1995 process_inlines();
1997 last_pos = sym->pos;
1998 } END_FOR_EACH_PTR(sym);
1999 split_inlines(sym_list);
2000 __pass_to_client(sym_list, END_FILE_HOOK);
2003 static int final_before_fake;
2004 void init_fake_env(void)
2006 if (!in_fake_env)
2007 final_before_fake = final_pass;
2008 in_fake_env++;
2009 __push_fake_cur_stree();
2010 final_pass = 0;
2013 void end_fake_env(void)
2015 __pop_fake_cur_stree();
2016 in_fake_env--;
2017 if (!in_fake_env)
2018 final_pass = final_before_fake;
2021 static void open_output_files(char *base_file)
2023 char buf[256];
2025 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
2026 sm_outfd = fopen(buf, "w");
2027 if (!sm_outfd)
2028 sm_fatal("Cannot open %s", buf);
2030 if (!option_info)
2031 return;
2033 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
2034 sql_outfd = fopen(buf, "w");
2035 if (!sql_outfd)
2036 sm_fatal("Error: Cannot open %s", buf);
2038 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
2039 caller_info_fd = fopen(buf, "w");
2040 if (!caller_info_fd)
2041 sm_fatal("Error: Cannot open %s", buf);
2044 void smatch(struct string_list *filelist)
2046 struct symbol_list *sym_list;
2047 struct timeval stop, start;
2048 char *path;
2049 int len;
2051 gettimeofday(&start, NULL);
2053 FOR_EACH_PTR_NOTAG(filelist, base_file) {
2054 path = getcwd(NULL, 0);
2055 free(full_base_file);
2056 if (path) {
2057 len = strlen(path) + 1 + strlen(base_file) + 1;
2058 full_base_file = malloc(len);
2059 snprintf(full_base_file, len, "%s/%s", path, base_file);
2060 } else {
2061 full_base_file = alloc_string(base_file);
2063 if (option_file_output)
2064 open_output_files(base_file);
2065 sym_list = sparse_keep_tokens(base_file);
2066 split_c_file_functions(sym_list);
2067 } END_FOR_EACH_PTR_NOTAG(base_file);
2069 gettimeofday(&stop, NULL);
2071 set_position(last_pos);
2072 final_pass = 1;
2073 if (option_time)
2074 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
2075 if (option_mem)
2076 sm_msg("mem: %luKb", get_max_memory());