kernel: handle with_intel_runtime_pm() macros better
[smatch.git] / smatch_flow.c
blob3647969ee5ff6a95576e328183e9a6b9cd91c503
1 /*
2 * Copyright (C) 2006,2008 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
18 #define _GNU_SOURCE 1
19 #include <unistd.h>
20 #include <stdio.h>
21 #include "token.h"
22 #include "scope.h"
23 #include "smatch.h"
24 #include "smatch_expression_stacks.h"
25 #include "smatch_extra.h"
26 #include "smatch_slist.h"
28 int __in_fake_assign;
29 int __in_fake_struct_assign;
30 int __in_buf_clear;
31 int __in_fake_var_assign;
32 int __fake_state_cnt;
33 int __debug_skip;
34 int in_fake_env;
35 int final_pass;
36 int __inline_call;
37 struct expression *__inline_fn;
39 int __smatch_lineno = 0;
41 static char *base_file;
42 static const char *filename;
43 static char *pathname;
44 static char *full_filename;
45 static char *full_base_file;
46 static char *cur_func;
47 int base_file_stream;
48 static unsigned int loop_count;
49 static int last_goto_statement_handled;
50 int __expr_stmt_count;
51 int __in_function_def;
52 int __in_unmatched_hook;
53 static struct expression_list *switch_expr_stack = NULL;
54 static struct expression_list *post_op_stack = NULL;
56 static struct ptr_list *fn_data_list;
57 static struct ptr_list *backup;
59 struct expression_list *big_expression_stack;
60 struct statement_list *big_statement_stack;
61 struct statement *__prev_stmt;
62 struct statement *__cur_stmt;
63 struct statement *__next_stmt;
64 static struct expression_list *parsed_calls;
65 static int indent_cnt;
66 int __in_pre_condition = 0;
67 int __bail_on_rest_of_function = 0;
68 static struct timeval fn_start_time;
69 static struct timeval outer_fn_start_time;
70 char *get_function(void) { return cur_func; }
71 int get_lineno(void) { return __smatch_lineno; }
72 int inside_loop(void) { return !!loop_count; }
73 int definitely_inside_loop(void) { return !!(loop_count & ~0x08000000); }
74 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
75 int in_expression_statement(void) { return !!__expr_stmt_count; }
77 static void split_symlist(struct symbol_list *sym_list);
78 static void split_declaration(struct symbol_list *sym_list);
79 static void split_expr_list(struct expression_list *expr_list, struct expression *parent);
80 static void split_args(struct expression *expr);
81 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *call, struct expression *expr, int nr);
82 static void add_inline_function(struct symbol *sym);
83 static void parse_inline(struct expression *expr);
85 int option_assume_loops = 0;
86 int option_two_passes = 0;
87 struct symbol *cur_func_sym = NULL;
88 struct stree *global_states;
90 const unsigned long valid_ptr_min = 4096;
91 unsigned long valid_ptr_max = ULONG_MAX & ~(MTAG_OFFSET_MASK);
92 const sval_t valid_ptr_min_sval = {
93 .type = &ptr_ctype,
94 {.value = 4096},
96 sval_t valid_ptr_max_sval = {
97 .type = &ptr_ctype,
98 {.value = ULONG_MAX & ~(MTAG_OFFSET_MASK)},
100 struct range_list *valid_ptr_rl;
102 void alloc_valid_ptr_rl(void)
104 valid_ptr_max = sval_type_max(&ulong_ctype).value & ~(MTAG_OFFSET_MASK);
105 valid_ptr_max_sval.value = valid_ptr_max;
107 valid_ptr_rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
108 valid_ptr_rl = cast_rl(&ptr_ctype, valid_ptr_rl);
109 valid_ptr_rl = clone_rl_permanent(valid_ptr_rl);
112 int outside_of_function(void)
114 return cur_func_sym == NULL;
117 const char *get_filename(void)
119 if (option_info && option_full_path)
120 return full_base_file;
121 if (option_info)
122 return base_file;
123 if (option_full_path)
124 return full_filename;
125 return filename;
128 const char *get_base_file(void)
130 if (option_full_path)
131 return full_base_file;
132 return base_file;
135 unsigned long long get_file_id(void)
137 return str_to_llu_hash(get_filename());
140 unsigned long long get_base_file_id(void)
142 return str_to_llu_hash(get_base_file());
145 static void set_position(struct position pos)
147 int len;
148 static int prev_stream = -1;
150 if (in_fake_env)
151 return;
153 if (pos.stream == 0 && pos.line == 0)
154 return;
156 __smatch_lineno = pos.line;
158 if (pos.stream == prev_stream)
159 return;
161 filename = stream_name(pos.stream);
163 free(full_filename);
164 pathname = getcwd(NULL, 0);
165 if (pathname) {
166 len = strlen(pathname) + 1 + strlen(filename) + 1;
167 full_filename = malloc(len);
168 snprintf(full_filename, len, "%s/%s", pathname, filename);
169 } else {
170 full_filename = alloc_string(filename);
172 free(pathname);
175 int is_assigned_call(struct expression *expr)
177 struct expression *parent = expr_get_parent_expr(expr);
179 if (parent &&
180 parent->type == EXPR_ASSIGNMENT &&
181 parent->op == '=' &&
182 strip_expr(parent->right) == expr)
183 return 1;
185 return 0;
188 int is_fake_assigned_call(struct expression *expr)
190 struct expression *parent = expr_get_fake_parent_expr(expr);
192 if (parent &&
193 parent->type == EXPR_ASSIGNMENT &&
194 parent->op == '=' &&
195 strip_expr(parent->right) == expr)
196 return 1;
198 return 0;
201 static bool is_inline_func(struct expression *expr)
203 if (expr->type != EXPR_SYMBOL || !expr->symbol)
204 return false;
205 if (!expr->symbol->definition)
206 return false;
207 if (expr->symbol->definition->ctype.modifiers & MOD_INLINE)
208 return true;
210 return 0;
213 static int is_noreturn_func(struct expression *expr)
215 if (expr->type != EXPR_SYMBOL || !expr->symbol)
216 return 0;
219 * It's almost impossible for Smatch to handle __builtin_constant_p()
220 * the same way that GCC does so Smatch ends up making some functions
221 * as no return functions incorrectly.
224 if (option_project == PROJ_KERNEL && expr->symbol->ident &&
225 strstr(expr->symbol->ident->name, "__compiletime_assert"))
226 return 0;
228 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
229 return 1;
230 return 0;
233 static int save_func_time(void *_rl, int argc, char **argv, char **azColName)
235 unsigned long *rl = _rl;
237 *rl = strtoul(argv[0], NULL, 10);
238 return 0;
241 static int get_func_time(struct symbol *sym)
243 unsigned long time = 0;
245 run_sql(&save_func_time, &time,
246 "select key from return_implies where %s and type = %d;",
247 get_static_filter(sym), FUNC_TIME);
249 return time;
252 static int inline_budget = 20;
254 int inlinable(struct expression *expr)
256 struct symbol *sym;
257 struct statement *last_stmt = NULL;
259 if (__inline_fn) /* don't nest */
260 return 0;
262 if (expr->type != EXPR_SYMBOL || !expr->symbol)
263 return 0;
264 if (is_no_inline_function(expr->symbol->ident->name))
265 return 0;
266 sym = get_base_type(expr->symbol);
267 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
268 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
269 return 0;
270 if (sym->stmt->type != STMT_COMPOUND)
271 return 0;
272 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
274 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
275 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
276 return 0;
277 if (sym->inline_stmt->type != STMT_COMPOUND)
278 return 0;
279 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
282 if (!last_stmt)
283 return 0;
285 /* the magic numbers in this function are pulled out of my bum. */
286 if (last_stmt->pos.line > sym->pos.line + inline_budget)
287 return 0;
289 if (get_func_time(expr->symbol) >= 2)
290 return 0;
292 return 1;
295 void __process_post_op_stack(void)
297 struct expression *expr;
299 FOR_EACH_PTR(post_op_stack, expr) {
300 __pass_to_client(expr, OP_HOOK);
301 } END_FOR_EACH_PTR(expr);
303 __free_ptr_list((struct ptr_list **)&post_op_stack);
306 static int handle_comma_assigns(struct expression *expr)
308 struct expression *right;
309 struct expression *assign;
311 right = strip_expr(expr->right);
312 if (right->type != EXPR_COMMA)
313 return 0;
315 __split_expr(right->left);
316 __process_post_op_stack();
318 assign = assign_expression(expr->left, '=', right->right);
319 __split_expr(assign);
321 return 1;
324 /* This is to handle *p++ = foo; assignments */
325 static int handle_postop_assigns(struct expression *expr)
327 struct expression *left, *fake_left;
328 struct expression *assign;
330 left = strip_expr(expr->left);
331 if (left->type != EXPR_PREOP || left->op != '*')
332 return 0;
333 left = strip_expr(left->unop);
334 if (left->type != EXPR_POSTOP)
335 return 0;
337 fake_left = deref_expression(strip_expr(left->unop));
338 assign = assign_expression(fake_left, '=', expr->right);
340 __split_expr(assign);
341 __split_expr(expr->left);
343 return 1;
346 static int prev_expression_is_getting_address(struct expression *expr)
348 struct expression *parent;
350 do {
351 parent = expr_get_parent_expr(expr);
353 if (!parent)
354 return 0;
355 if (parent->type == EXPR_PREOP && parent->op == '&')
356 return 1;
357 if (parent->type == EXPR_PREOP && parent->op == '(')
358 goto next;
359 if (parent->type == EXPR_DEREF && parent->op == '.')
360 goto next;
361 /* Handle &foo->array[offset] */
362 if (parent->type == EXPR_BINOP && parent->op == '+') {
363 parent = expr_get_parent_expr(parent);
364 if (!parent)
365 return 0;
366 if (parent->type == EXPR_PREOP && parent->op == '*')
367 goto next;
370 return 0;
371 next:
372 expr = parent;
373 } while (1);
376 int __in_builtin_overflow_func;
377 static void handle_builtin_overflow_func(struct expression *expr)
379 struct expression *a, *b, *res, *assign;
380 int op;
382 if (sym_name_is("__builtin_add_overflow", expr->fn))
383 op = '+';
384 else if (sym_name_is("__builtin_sub_overflow", expr->fn))
385 op = '-';
386 else if (sym_name_is("__builtin_mul_overflow", expr->fn))
387 op = '*';
388 else
389 return;
391 a = get_argument_from_call_expr(expr->args, 0);
392 b = get_argument_from_call_expr(expr->args, 1);
393 res = get_argument_from_call_expr(expr->args, 2);
395 assign = assign_expression(deref_expression(res), '=', binop_expression(a, op, b));
397 __in_builtin_overflow_func++;
398 __split_expr(assign);
399 __in_builtin_overflow_func--;
402 static int handle__builtin_choose_expr(struct expression *expr)
404 struct expression *const_expr, *expr1, *expr2;
405 sval_t sval;
407 if (!sym_name_is("__builtin_choose_expr", expr->fn))
408 return 0;
410 const_expr = get_argument_from_call_expr(expr->args, 0);
411 expr1 = get_argument_from_call_expr(expr->args, 1);
412 expr2 = get_argument_from_call_expr(expr->args, 2);
414 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
415 return 0;
416 if (sval.value)
417 __split_expr(expr1);
418 else
419 __split_expr(expr2);
420 return 1;
423 static int handle__builtin_choose_expr_assigns(struct expression *expr)
425 struct expression *const_expr, *right, *expr1, *expr2, *fake;
426 sval_t sval;
428 right = strip_parens(expr->right);
429 if (right->type != EXPR_CALL)
430 return 0;
431 if (!sym_name_is("__builtin_choose_expr", right->fn))
432 return 0;
434 const_expr = get_argument_from_call_expr(right->args, 0);
435 expr1 = get_argument_from_call_expr(right->args, 1);
436 expr2 = get_argument_from_call_expr(right->args, 2);
438 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
439 return 0;
441 fake = assign_expression(expr->left, '=', sval.value ? expr1 : expr2);
442 __split_expr(fake);
443 return 1;
446 int is_condition_call(struct expression *expr)
448 struct expression *tmp;
450 FOR_EACH_PTR_REVERSE(big_condition_stack, tmp) {
451 if (expr == tmp || expr_get_parent_expr(expr) == tmp)
452 return 1;
453 if (tmp->pos.line < expr->pos.line)
454 return 0;
455 } END_FOR_EACH_PTR_REVERSE(tmp);
457 return 0;
460 static struct expression *expr_get_parent_no_parens(struct expression *expr)
462 do {
463 expr = expr_get_parent_expr(expr);
464 } while (expr &&
465 expr->type == EXPR_PREOP &&
466 expr->op == '(');
468 return expr;
471 static bool gen_fake_function_assign(struct expression *expr)
473 static struct expression *parsed;
474 struct expression *assign, *parent;
475 struct symbol *type;
476 char buf[64];
478 /* The rule is that every non-void function call has to be part of an
479 * assignment. TODO: Should we create a fake non-casted assignment
480 * for casted assignments? Also faked assigns for += assignments?
482 type = get_type(expr);
483 if (!type || type == &void_ctype)
484 return false;
486 parent = expr_get_parent_no_parens(expr);
487 if (parent && parent->type == EXPR_ASSIGNMENT)
488 return false;
490 parent = expr_get_fake_parent_expr(expr);
491 if (parent) {
492 struct expression *left = parent->left;
494 if (parent == parsed)
495 return false;
496 if (!left || left->type != EXPR_SYMBOL)
497 return false;
498 if (strncmp(left->symbol_name->name, "__fake_assign_", 14) != 0)
499 return false;
500 parsed = parent;
501 __split_expr(parent);
502 return true;
505 // TODO: faked_assign skipping conditions is a hack
506 if (is_condition_call(expr))
507 return false;
509 snprintf(buf, sizeof(buf), "__fake_assign_%p", expr);
510 assign = create_fake_assign(buf, get_type(expr), expr);
512 parsed = assign;
513 __split_expr(assign);
514 return true;
517 static void split_call(struct expression *expr)
519 if (gen_fake_function_assign(expr))
520 return;
522 expr_set_parent_expr(expr->fn, expr);
524 if (sym_name_is("__builtin_constant_p", expr->fn))
525 return;
526 if (handle__builtin_choose_expr(expr))
527 return;
528 __split_expr(expr->fn);
529 split_args(expr);
530 if (is_inline_func(expr->fn))
531 add_inline_function(expr->fn->symbol->definition);
532 if (inlinable(expr->fn))
533 __inline_call = 1;
534 __process_post_op_stack();
535 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
536 __pass_to_client(expr, FUNCTION_CALL_HOOK);
537 __inline_call = 0;
538 if (inlinable(expr->fn))
539 parse_inline(expr);
540 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
541 if (is_noreturn_func(expr->fn))
542 nullify_path();
543 if (!expr_get_parent_expr(expr) && indent_cnt == 1)
544 __discard_fake_states(expr);
545 handle_builtin_overflow_func(expr);
546 __add_ptr_list((struct ptr_list **)&parsed_calls, expr);
549 void parse_assignment(struct expression *expr)
551 struct expression *right;
553 expr_set_parent_expr(expr->left, expr);
554 expr_set_parent_expr(expr->right, expr);
556 right = strip_expr(expr->right);
557 if (!right)
558 return;
560 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
562 /* foo = !bar() */
563 if (__handle_condition_assigns(expr))
564 goto after_assign;
565 /* foo = (x < 5 ? foo : 5); */
566 if (__handle_select_assigns(expr))
567 goto after_assign;
568 /* foo = ({frob(); frob(); frob(); 1;}) */
569 if (__handle_expr_statement_assigns(expr))
570 return; // FIXME: got after
571 /* foo = (3, 4); */
572 if (handle_comma_assigns(expr))
573 goto after_assign;
574 if (handle__builtin_choose_expr_assigns(expr))
575 goto after_assign;
576 if (handle_postop_assigns(expr))
577 return; /* no need to goto after_assign */
579 __split_expr(expr->right);
580 if (outside_of_function())
581 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
582 else
583 __pass_to_client(expr, ASSIGNMENT_HOOK);
586 // FIXME: the ordering of this is tricky
587 __fake_struct_member_assignments(expr);
589 /* Re-examine ->right for inlines. See the commit message */
590 right = strip_expr(expr->right);
591 if (expr->op == '=' && right->type == EXPR_CALL)
592 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
594 after_assign:
595 if (get_macro_name(right->pos) &&
596 get_macro_name(expr->left->pos) != get_macro_name(right->pos))
597 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
599 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
600 __split_expr(expr->left);
603 static bool skip_split_off(struct expression *expr)
605 if (expr->type == EXPR_CALL &&
606 sym_name_is("__smatch_stop_skip", expr->fn))
607 return true;
608 return false;
611 void __split_expr(struct expression *expr)
613 if (!expr)
614 return;
616 if (skip_split_off(expr))
617 __debug_skip = 0;
618 if (__debug_skip)
619 return;
621 // if (local_debug)
622 // sm_msg("Debug expr_type %d %s expr = '%s'", expr->type, show_special(expr->op), expr_to_str(expr));
624 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
625 return;
626 if (__in_fake_assign >= 4) /* don't allow too much nesting */
627 return;
629 push_expression(&big_expression_stack, expr);
630 set_position(expr->pos);
631 __pass_to_client(expr, EXPR_HOOK);
633 switch (expr->type) {
634 case EXPR_PREOP:
635 expr_set_parent_expr(expr->unop, expr);
637 if (expr->op == '*' &&
638 !prev_expression_is_getting_address(expr))
639 __pass_to_client(expr, DEREF_HOOK);
640 __split_expr(expr->unop);
641 __pass_to_client(expr, OP_HOOK);
642 break;
643 case EXPR_POSTOP:
644 expr_set_parent_expr(expr->unop, expr);
646 __split_expr(expr->unop);
647 push_expression(&post_op_stack, expr);
648 break;
649 case EXPR_STATEMENT:
650 __expr_stmt_count++;
651 if (expr->statement && !expr->statement) {
652 stmt_set_parent_stmt(expr->statement,
653 last_ptr_list((struct ptr_list *)big_statement_stack));
655 __split_stmt(expr->statement);
656 __expr_stmt_count--;
657 break;
658 case EXPR_LOGICAL:
659 case EXPR_COMPARE:
660 expr_set_parent_expr(expr->left, expr);
661 expr_set_parent_expr(expr->right, expr);
663 __pass_to_client(expr, LOGIC_HOOK);
664 __handle_logic(expr);
665 break;
666 case EXPR_BINOP:
667 expr_set_parent_expr(expr->left, expr);
668 expr_set_parent_expr(expr->right, expr);
670 __pass_to_client(expr, BINOP_HOOK);
671 __split_expr(expr->left);
672 __split_expr(expr->right);
673 break;
674 case EXPR_COMMA:
675 expr_set_parent_expr(expr->left, expr);
676 expr_set_parent_expr(expr->right, expr);
678 __split_expr(expr->left);
679 __process_post_op_stack();
680 __split_expr(expr->right);
681 break;
682 case EXPR_ASSIGNMENT:
683 parse_assignment(expr);
684 break;
685 case EXPR_DEREF:
686 expr_set_parent_expr(expr->deref, expr);
688 __pass_to_client(expr, DEREF_HOOK);
689 __split_expr(expr->deref);
690 break;
691 case EXPR_SLICE:
692 expr_set_parent_expr(expr->base, expr);
694 __split_expr(expr->base);
695 break;
696 case EXPR_CAST:
697 case EXPR_FORCE_CAST:
698 expr_set_parent_expr(expr->cast_expression, expr);
700 __pass_to_client(expr, CAST_HOOK);
701 __split_expr(expr->cast_expression);
702 break;
703 case EXPR_SIZEOF:
704 if (expr->cast_expression)
705 __pass_to_client(strip_parens(expr->cast_expression),
706 SIZEOF_HOOK);
707 break;
708 case EXPR_OFFSETOF:
709 case EXPR_ALIGNOF:
710 break;
711 case EXPR_CONDITIONAL:
712 case EXPR_SELECT:
713 expr_set_parent_expr(expr->conditional, expr);
714 expr_set_parent_expr(expr->cond_true, expr);
715 expr_set_parent_expr(expr->cond_false, expr);
717 if (known_condition_true(expr->conditional)) {
718 __split_expr(expr->cond_true);
719 break;
721 if (known_condition_false(expr->conditional)) {
722 __split_expr(expr->cond_false);
723 break;
725 __pass_to_client(expr, SELECT_HOOK);
726 __split_whole_condition(expr->conditional);
727 __split_expr(expr->cond_true);
728 __push_true_states();
729 __use_false_states();
730 __split_expr(expr->cond_false);
731 __merge_true_states();
732 break;
733 case EXPR_CALL:
734 split_call(expr);
735 break;
736 case EXPR_INITIALIZER:
737 split_expr_list(expr->expr_list, expr);
738 break;
739 case EXPR_IDENTIFIER:
740 expr_set_parent_expr(expr->ident_expression, expr);
741 __split_expr(expr->ident_expression);
742 break;
743 case EXPR_INDEX:
744 expr_set_parent_expr(expr->idx_expression, expr);
745 __split_expr(expr->idx_expression);
746 break;
747 case EXPR_POS:
748 expr_set_parent_expr(expr->init_expr, expr);
749 __split_expr(expr->init_expr);
750 break;
751 case EXPR_SYMBOL:
752 __pass_to_client(expr, SYM_HOOK);
753 break;
754 case EXPR_STRING:
755 __pass_to_client(expr, STRING_HOOK);
756 break;
757 case EXPR_GENERIC: {
758 struct expression *tmp;
760 tmp = strip_Generic(expr);
761 if (tmp != expr)
762 __split_expr(tmp);
763 break;
765 default:
766 break;
768 __pass_to_client(expr, EXPR_HOOK_AFTER);
769 pop_expression(&big_expression_stack);
772 static int is_forever_loop(struct statement *stmt)
774 struct expression *expr;
775 sval_t sval;
777 expr = strip_expr(stmt->iterator_pre_condition);
778 if (!expr)
779 expr = stmt->iterator_post_condition;
780 if (!expr) {
781 /* this is a for(;;) loop... */
782 return 1;
785 if (get_value(expr, &sval) && sval.value != 0)
786 return 1;
788 return 0;
791 static int loop_num;
792 static char *get_loop_name(int num)
794 char buf[256];
796 snprintf(buf, 255, "-loop%d", num);
797 buf[255] = '\0';
798 return alloc_sname(buf);
801 static struct bool_stmt_fn_list *once_through_hooks;
802 void add_once_through_hook(bool_stmt_func *fn)
804 add_ptr_list(&once_through_hooks, fn);
807 static bool call_once_through_hooks(struct statement *stmt)
809 bool_stmt_func *fn;
811 if (implied_condition_true(stmt->iterator_pre_condition))
812 return true;
813 if (option_assume_loops)
814 return true;
816 FOR_EACH_PTR(once_through_hooks, fn) {
817 if ((fn)(stmt))
818 return true;
819 } END_FOR_EACH_PTR(fn);
821 return false;
825 * Pre Loops are while and for loops.
827 static void handle_pre_loop(struct statement *stmt)
829 int once_through; /* we go through the loop at least once */
830 struct sm_state *extra_sm = NULL;
831 int unchanged = 0;
832 char *loop_name;
833 struct stree *stree = NULL;
834 struct sm_state *sm = NULL;
836 loop_name = get_loop_name(loop_num);
837 loop_num++;
839 split_declaration(stmt->iterator_syms);
840 if (stmt->iterator_pre_statement) {
841 __split_stmt(stmt->iterator_pre_statement);
842 __prev_stmt = stmt->iterator_pre_statement;
845 once_through = call_once_through_hooks(stmt);
847 loop_count++;
848 __push_continues();
849 __push_breaks();
851 __merge_gotos(loop_name, NULL);
853 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
854 __in_pre_condition++;
855 __pass_to_client(stmt, PRELOOP_HOOK);
856 __split_whole_condition(stmt->iterator_pre_condition);
857 __in_pre_condition--;
858 FOR_EACH_SM(stree, sm) {
859 set_state(sm->owner, sm->name, sm->sym, sm->state);
860 } END_FOR_EACH_SM(sm);
861 free_stree(&stree);
862 if (extra_sm)
863 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
865 __split_stmt(stmt->iterator_statement);
866 if (is_forever_loop(stmt)) {
867 __merge_continues();
868 __save_gotos(loop_name, NULL);
870 __push_fake_cur_stree();
871 __split_stmt(stmt->iterator_post_statement);
872 stree = __pop_fake_cur_stree();
874 __discard_false_states();
875 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
876 __use_breaks();
878 if (!__path_is_null())
879 __merge_stree_into_cur(stree);
880 free_stree(&stree);
881 } else {
882 __merge_continues();
883 unchanged = __iterator_unchanged(extra_sm);
884 __split_stmt(stmt->iterator_post_statement);
885 __prev_stmt = stmt->iterator_post_statement;
886 __cur_stmt = stmt;
888 __save_gotos(loop_name, NULL);
889 __in_pre_condition++;
890 __split_whole_condition(stmt->iterator_pre_condition);
891 __in_pre_condition--;
892 nullify_path();
893 __merge_false_states();
894 if (once_through)
895 __discard_false_states();
896 else
897 __merge_false_states();
899 if (extra_sm && unchanged)
900 __extra_pre_loop_hook_after(extra_sm,
901 stmt->iterator_post_statement,
902 stmt->iterator_pre_condition);
903 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
904 __merge_breaks();
906 loop_count--;
910 * Post loops are do {} while();
912 static void handle_post_loop(struct statement *stmt)
914 char *loop_name;
916 loop_name = get_loop_name(loop_num);
917 loop_num++;
918 loop_count++;
920 __pass_to_client(stmt, POSTLOOP_HOOK);
922 __push_continues();
923 __push_breaks();
924 __merge_gotos(loop_name, NULL);
925 __split_stmt(stmt->iterator_statement);
926 __merge_continues();
927 if (!expr_is_zero(stmt->iterator_post_condition))
928 __save_gotos(loop_name, NULL);
930 if (is_forever_loop(stmt)) {
931 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
932 __use_breaks();
933 } else {
934 __split_whole_condition(stmt->iterator_post_condition);
935 __use_false_states();
936 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
937 __merge_breaks();
939 loop_count--;
942 static int empty_statement(struct statement *stmt)
944 if (!stmt)
945 return 0;
946 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
947 return 1;
948 return 0;
951 static int last_stmt_on_same_line(void)
953 struct statement *stmt;
954 int i = 0;
956 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
957 if (!i++)
958 continue;
959 if (stmt->pos.line == get_lineno())
960 return 1;
961 return 0;
962 } END_FOR_EACH_PTR_REVERSE(stmt);
963 return 0;
966 static void split_asm_ops(struct asm_operand_list *ops)
968 struct asm_operand *op;
970 FOR_EACH_PTR(ops, op) {
971 __split_expr(op->expr);
972 } END_FOR_EACH_PTR(op);
975 static int is_case_val(struct statement *stmt, sval_t sval)
977 sval_t case_sval;
979 if (stmt->type != STMT_CASE)
980 return 0;
981 if (!stmt->case_expression) {
982 __set_default();
983 return 1;
985 if (!get_value(stmt->case_expression, &case_sval))
986 return 0;
987 if (case_sval.value == sval.value)
988 return 1;
989 return 0;
992 static struct range_list *get_case_rl(struct expression *switch_expr,
993 struct expression *case_expr,
994 struct expression *case_to)
996 sval_t start, end;
997 struct range_list *rl = NULL;
998 struct symbol *switch_type;
1000 switch_type = get_type(switch_expr);
1001 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
1002 start = sval_cast(switch_type, start);
1003 end = sval_cast(switch_type, end);
1004 add_range(&rl, start, end);
1005 } else if (get_value(case_expr, &start)) {
1006 start = sval_cast(switch_type, start);
1007 add_range(&rl, start, start);
1010 return rl;
1013 static void split_known_switch(struct statement *stmt, sval_t sval)
1015 struct statement *tmp;
1016 struct range_list *rl;
1018 __split_expr(stmt->switch_expression);
1019 sval = sval_cast(get_type(stmt->switch_expression), sval);
1021 push_expression(&switch_expr_stack, stmt->switch_expression);
1022 __save_switch_states(top_expression(switch_expr_stack));
1023 nullify_path();
1024 __push_default();
1025 __push_breaks();
1027 stmt = stmt->switch_statement;
1029 __push_scope_hooks();
1030 FOR_EACH_PTR(stmt->stmts, tmp) {
1031 __smatch_lineno = tmp->pos.line;
1032 // FIXME: what if default comes before the known case statement?
1033 if (is_case_val(tmp, sval)) {
1034 rl = alloc_rl(sval, sval);
1035 __merge_switches(top_expression(switch_expr_stack), rl);
1036 __pass_case_to_client(top_expression(switch_expr_stack), rl);
1037 stmt_set_parent_stmt(tmp->case_statement, tmp);
1038 __split_stmt(tmp->case_statement);
1039 goto next;
1041 if (__path_is_null())
1042 continue;
1043 __split_stmt(tmp);
1044 next:
1045 if (__path_is_null()) {
1046 __set_default();
1047 goto out;
1049 } END_FOR_EACH_PTR(tmp);
1050 out:
1051 __call_scope_hooks();
1052 if (!__pop_default())
1053 __merge_switches(top_expression(switch_expr_stack), NULL);
1054 __discard_switches();
1055 __merge_breaks();
1056 pop_expression(&switch_expr_stack);
1059 static void split_case(struct statement *stmt)
1061 struct range_list *rl = NULL;
1063 expr_set_parent_stmt(stmt->case_expression, stmt);
1064 expr_set_parent_stmt(stmt->case_to, stmt);
1066 rl = get_case_rl(top_expression(switch_expr_stack),
1067 stmt->case_expression, stmt->case_to);
1068 while (stmt->case_statement->type == STMT_CASE) {
1069 struct range_list *tmp;
1071 tmp = get_case_rl(top_expression(switch_expr_stack),
1072 stmt->case_statement->case_expression,
1073 stmt->case_statement->case_to);
1074 if (!tmp)
1075 goto next;
1076 rl = rl_union(rl, tmp);
1077 if (!stmt->case_expression)
1078 __set_default();
1079 next:
1080 stmt = stmt->case_statement;
1083 __merge_switches(top_expression(switch_expr_stack), rl);
1085 if (!stmt->case_expression)
1086 __set_default();
1088 stmt_set_parent_stmt(stmt->case_statement, stmt);
1089 __split_stmt(stmt->case_statement);
1092 int time_parsing_function(void)
1094 return ms_since(&fn_start_time) / 1000;
1097 bool taking_too_long(void)
1099 if ((ms_since(&outer_fn_start_time) / 1000) > 60 * 5) /* five minutes */
1100 return 1;
1101 return 0;
1104 struct statement *get_last_stmt(void)
1106 struct symbol *fn;
1107 struct statement *stmt;
1109 fn = get_base_type(cur_func_sym);
1110 if (!fn)
1111 return NULL;
1112 stmt = fn->stmt;
1113 if (!stmt)
1114 stmt = fn->inline_stmt;
1115 if (!stmt || stmt->type != STMT_COMPOUND)
1116 return NULL;
1117 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1118 if (stmt && stmt->type == STMT_LABEL)
1119 stmt = stmt->label_statement;
1120 return stmt;
1123 int is_last_stmt(struct statement *cur_stmt)
1125 struct statement *last;
1127 last = get_last_stmt();
1128 if (last && last == cur_stmt)
1129 return 1;
1130 return 0;
1133 static void handle_backward_goto(struct statement *goto_stmt)
1135 const char *goto_name, *label_name;
1136 struct statement *func_stmt;
1137 struct symbol *base_type = get_base_type(cur_func_sym);
1138 struct statement *tmp;
1139 int found = 0;
1141 if (!option_info)
1142 return;
1143 if (last_goto_statement_handled)
1144 return;
1145 last_goto_statement_handled = 1;
1147 if (!goto_stmt->goto_label ||
1148 goto_stmt->goto_label->type != SYM_LABEL ||
1149 !goto_stmt->goto_label->ident)
1150 return;
1151 goto_name = goto_stmt->goto_label->ident->name;
1153 func_stmt = base_type->stmt;
1154 if (!func_stmt)
1155 func_stmt = base_type->inline_stmt;
1156 if (!func_stmt)
1157 return;
1158 if (func_stmt->type != STMT_COMPOUND)
1159 return;
1161 FOR_EACH_PTR(func_stmt->stmts, tmp) {
1162 if (!found) {
1163 if (tmp->type != STMT_LABEL)
1164 continue;
1165 if (!tmp->label_identifier ||
1166 tmp->label_identifier->type != SYM_LABEL ||
1167 !tmp->label_identifier->ident)
1168 continue;
1169 label_name = tmp->label_identifier->ident->name;
1170 if (strcmp(goto_name, label_name) != 0)
1171 continue;
1172 found = 1;
1174 __split_stmt(tmp);
1175 } END_FOR_EACH_PTR(tmp);
1178 static void fake_a_return(void)
1180 struct expression *ret = NULL;
1182 nullify_path();
1183 __unnullify_path();
1185 if (cur_func_return_type() != &void_ctype)
1186 ret = unknown_value_expression(NULL);
1188 __pass_to_client(ret, RETURN_HOOK);
1189 nullify_path();
1192 static void split_ret_value(struct expression *expr)
1194 struct symbol *type;
1196 if (!expr)
1197 return;
1199 type = get_real_base_type(cur_func_sym);
1200 type = get_real_base_type(type);
1201 expr = fake_a_variable_assign(type, NULL, expr, -1);
1203 __in_fake_var_assign++;
1204 __split_expr(expr);
1205 __in_fake_var_assign--;
1208 static void fake_an_empty_default(struct position pos)
1210 static struct statement none = {};
1212 none.pos = pos;
1213 none.type = STMT_NONE;
1214 __merge_switches(top_expression(switch_expr_stack), NULL);
1215 __split_stmt(&none);
1218 static void split_compound(struct statement *stmt)
1220 struct statement *prev = NULL;
1221 struct statement *cur = NULL;
1222 struct statement *next;
1224 __push_scope_hooks();
1226 FOR_EACH_PTR(stmt->stmts, next) {
1227 /* just set them all ahead of time */
1228 stmt_set_parent_stmt(next, stmt);
1230 if (cur) {
1231 __prev_stmt = prev;
1232 __next_stmt = next;
1233 __cur_stmt = cur;
1234 __split_stmt(cur);
1236 prev = cur;
1237 cur = next;
1238 } END_FOR_EACH_PTR(next);
1239 if (cur) {
1240 __prev_stmt = prev;
1241 __cur_stmt = cur;
1242 __next_stmt = NULL;
1243 __split_stmt(cur);
1247 * For function scope, then delay calling the scope hooks until the
1248 * end of function hooks can run. I'm not positive this is the right
1249 * thing...
1251 if (!is_last_stmt(cur))
1252 __call_scope_hooks();
1256 * This is a hack, work around for detecting empty functions.
1258 static int need_delayed_scope_hooks(void)
1260 struct symbol *fn = get_base_type(cur_func_sym);
1261 struct statement *stmt;
1263 if (!fn)
1264 return 0;
1265 stmt = fn->stmt;
1266 if (!stmt)
1267 stmt = fn->inline_stmt;
1268 if (stmt && stmt->type == STMT_COMPOUND)
1269 return 1;
1270 return 0;
1273 void __split_label_stmt(struct statement *stmt)
1275 if (stmt->label_identifier &&
1276 stmt->label_identifier->type == SYM_LABEL &&
1277 stmt->label_identifier->ident) {
1278 loop_count |= 0x0800000;
1279 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
1283 static void find_asm_gotos(struct statement *stmt)
1285 struct symbol *sym;
1287 FOR_EACH_PTR(stmt->asm_labels, sym) {
1288 __save_gotos(sym->ident->name, sym);
1289 } END_FOR_EACH_PTR(sym);
1292 static bool already_parsed_call(struct expression *call)
1294 struct expression *expr;
1296 FOR_EACH_PTR(parsed_calls, expr) {
1297 if (expr == call)
1298 return true;
1299 } END_FOR_EACH_PTR(expr);
1300 return false;
1303 static void free_parsed_call_stuff(bool free_fake_states)
1305 free_expression_stack(&parsed_calls);
1306 if (free_fake_states)
1307 __discard_fake_states(NULL);
1310 void __split_stmt(struct statement *stmt)
1312 sval_t sval;
1313 struct timeval start, stop;
1314 bool skip_after = false;
1316 gettimeofday(&start, NULL);
1318 if (!stmt)
1319 goto out;
1321 if (!__in_fake_assign)
1322 __silence_warnings_for_stmt = false;
1324 if (__bail_on_rest_of_function || is_skipped_function())
1325 return;
1327 if (out_of_memory() || taking_too_long()) {
1328 gettimeofday(&start, NULL);
1330 __bail_on_rest_of_function = 1;
1331 final_pass = 1;
1332 sm_perror("Function too hairy. Giving up. %lu seconds",
1333 start.tv_sec - fn_start_time.tv_sec);
1334 fake_a_return();
1335 final_pass = 0; /* turn off sm_msg() from here */
1336 return;
1339 indent_cnt++;
1341 add_ptr_list(&big_statement_stack, stmt);
1342 free_expression_stack(&big_expression_stack);
1343 free_parsed_call_stuff(indent_cnt == 1);
1344 set_position(stmt->pos);
1345 __pass_to_client(stmt, STMT_HOOK);
1347 switch (stmt->type) {
1348 case STMT_DECLARATION:
1349 split_declaration(stmt->declaration);
1350 break;
1351 case STMT_RETURN:
1352 expr_set_parent_stmt(stmt->ret_value, stmt);
1354 split_ret_value(stmt->ret_value);
1355 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1356 __process_post_op_stack();
1357 nullify_path();
1358 break;
1359 case STMT_EXPRESSION:
1360 expr_set_parent_stmt(stmt->expression, stmt);
1361 expr_set_parent_stmt(stmt->context, stmt);
1363 __split_expr(stmt->expression);
1364 break;
1365 case STMT_COMPOUND:
1366 split_compound(stmt);
1367 break;
1368 case STMT_IF:
1369 stmt_set_parent_stmt(stmt->if_true, stmt);
1370 stmt_set_parent_stmt(stmt->if_false, stmt);
1371 expr_set_parent_stmt(stmt->if_conditional, stmt);
1373 if (known_condition_true(stmt->if_conditional)) {
1374 __split_stmt(stmt->if_true);
1375 break;
1377 if (known_condition_false(stmt->if_conditional)) {
1378 __split_stmt(stmt->if_false);
1379 break;
1381 __split_whole_condition(stmt->if_conditional);
1382 __split_stmt(stmt->if_true);
1383 if (empty_statement(stmt->if_true) &&
1384 last_stmt_on_same_line() &&
1385 !get_macro_name(stmt->if_true->pos))
1386 sm_warning("if();");
1387 __push_true_states();
1388 __use_false_states();
1389 __split_stmt(stmt->if_false);
1390 __merge_true_states();
1391 break;
1392 case STMT_ITERATOR:
1393 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1394 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1395 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1396 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1397 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1399 if (stmt->iterator_pre_condition)
1400 handle_pre_loop(stmt);
1401 else if (stmt->iterator_post_condition)
1402 handle_post_loop(stmt);
1403 else {
1404 // these are for(;;) type loops.
1405 handle_pre_loop(stmt);
1407 break;
1408 case STMT_SWITCH:
1409 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1410 expr_set_parent_stmt(stmt->switch_expression, stmt);
1412 if (get_value(stmt->switch_expression, &sval)) {
1413 split_known_switch(stmt, sval);
1414 break;
1416 __split_expr(stmt->switch_expression);
1417 push_expression(&switch_expr_stack, stmt->switch_expression);
1418 __save_switch_states(top_expression(switch_expr_stack));
1419 nullify_path();
1420 __push_default();
1421 __push_breaks();
1422 __split_stmt(stmt->switch_statement);
1423 if (!__pop_default() && have_remaining_cases())
1424 fake_an_empty_default(stmt->pos);
1425 __discard_switches();
1426 __merge_breaks();
1427 pop_expression(&switch_expr_stack);
1428 break;
1429 case STMT_CASE:
1430 split_case(stmt);
1431 break;
1432 case STMT_LABEL:
1433 __split_label_stmt(stmt);
1434 __pass_to_client(stmt, STMT_HOOK_AFTER);
1435 skip_after = true;
1436 __split_stmt(stmt->label_statement);
1437 break;
1438 case STMT_GOTO:
1439 expr_set_parent_stmt(stmt->goto_expression, stmt);
1441 __split_expr(stmt->goto_expression);
1442 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1443 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1444 __process_breaks();
1445 } else if (!strcmp(stmt->goto_label->ident->name,
1446 "continue")) {
1447 __process_continues();
1449 } else if (stmt->goto_label &&
1450 stmt->goto_label->type == SYM_LABEL &&
1451 stmt->goto_label->ident) {
1452 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1454 nullify_path();
1455 if (is_last_stmt(stmt))
1456 handle_backward_goto(stmt);
1457 break;
1458 case STMT_NONE:
1459 break;
1460 case STMT_ASM:
1461 expr_set_parent_stmt(stmt->asm_string, stmt);
1463 find_asm_gotos(stmt);
1464 __pass_to_client(stmt, ASM_HOOK);
1465 __split_expr(stmt->asm_string);
1466 split_asm_ops(stmt->asm_outputs);
1467 split_asm_ops(stmt->asm_inputs);
1468 split_expr_list(stmt->asm_clobbers, NULL);
1469 break;
1470 case STMT_CONTEXT:
1471 break;
1472 case STMT_RANGE:
1473 __split_expr(stmt->range_expression);
1474 __split_expr(stmt->range_low);
1475 __split_expr(stmt->range_high);
1476 break;
1478 if (!skip_after)
1479 __pass_to_client(stmt, STMT_HOOK_AFTER);
1480 if (--indent_cnt == 1)
1481 free_parsed_call_stuff(true);
1483 out:
1484 __process_post_op_stack();
1486 gettimeofday(&stop, NULL);
1487 if (option_time_stmt && stmt)
1488 sm_msg("stmt_time%s: %ld",
1489 stmt->type == STMT_COMPOUND ? "_block" : "",
1490 stop.tv_sec - start.tv_sec);
1493 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1495 struct expression *expr;
1497 FOR_EACH_PTR(expr_list, expr) {
1498 expr_set_parent_expr(expr, parent);
1499 __split_expr(expr);
1500 __process_post_op_stack();
1501 } END_FOR_EACH_PTR(expr);
1504 static bool cast_arg(struct symbol *type, struct expression *arg)
1506 struct symbol *orig;
1508 if (!type)
1509 return false;
1511 arg = strip_parens(arg);
1512 if (arg != strip_expr(arg))
1513 return true;
1515 orig = get_type(arg);
1516 if (!orig)
1517 return true;
1518 if (types_equiv(orig, type))
1519 return false;
1521 if (orig->type == SYM_ARRAY && type->type == SYM_PTR)
1522 return true;
1525 * I would have expected that we could just do use (orig == type) but I
1526 * guess for pointers we need to get the basetype to do that comparison.
1530 if (orig->type != SYM_PTR ||
1531 type->type != SYM_PTR) {
1532 if (type_fits(type, orig))
1533 return false;
1534 return true;
1536 orig = get_real_base_type(orig);
1537 type = get_real_base_type(type);
1538 if (orig == type)
1539 return false;
1541 return true;
1544 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *call, struct expression *expr, int nr)
1546 char buf[64];
1547 bool cast;
1549 if (!expr || !cur_func_sym)
1550 return NULL;
1552 if (already_parsed_call(call))
1553 return NULL;
1555 if (expr->type == EXPR_ASSIGNMENT)
1556 return expr;
1558 /* for va_args then we don't know the type */
1559 if (!type)
1560 type = get_type(expr);
1562 cast = cast_arg(type, expr);
1564 * Using expr_to_sym() here is a hack. We want to say that we don't
1565 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1566 * It turns out faking these assignments is way more expensive than I
1567 * would have imagined. I'm not sure why exactly.
1570 if (!cast) {
1572 * if the code is "return *p;" where "p" is a user pointer then
1573 * we want to create a fake assignment so that it sets the state
1574 * in check_kernel_user_data.c.
1577 if (expr->type != EXPR_PREOP &&
1578 expr->op != '*' && expr->op != '&' &&
1579 expr_to_sym(expr))
1580 return expr;
1583 if (nr == -1)
1584 snprintf(buf, sizeof(buf), "__fake_return_%p", expr);
1585 else
1586 snprintf(buf, sizeof(buf), "__fake_param_%p_%d", call, nr);
1588 return create_fake_assign(buf, type, expr);
1591 static void split_args(struct expression *expr)
1593 struct expression *arg, *tmp;
1594 struct symbol *type;
1595 int i;
1597 i = -1;
1598 FOR_EACH_PTR(expr->args, arg) {
1599 i++;
1600 expr_set_parent_expr(arg, expr);
1601 type = get_arg_type(expr->fn, i);
1602 tmp = fake_a_variable_assign(type, expr, arg, i);
1603 if (tmp != arg)
1604 __in_fake_var_assign++;
1605 __split_expr(tmp);
1606 if (tmp != arg)
1607 __in_fake_var_assign--;
1608 __process_post_op_stack();
1609 } END_FOR_EACH_PTR(arg);
1612 static void split_sym(struct symbol *sym)
1614 if (!sym)
1615 return;
1616 if (!(sym->namespace & NS_SYMBOL))
1617 return;
1619 __split_stmt(sym->stmt);
1620 __split_expr(sym->array_size);
1621 split_symlist(sym->arguments);
1622 split_symlist(sym->symbol_list);
1623 __split_stmt(sym->inline_stmt);
1624 split_symlist(sym->inline_symbol_list);
1627 static void split_symlist(struct symbol_list *sym_list)
1629 struct symbol *sym;
1631 FOR_EACH_PTR(sym_list, sym) {
1632 split_sym(sym);
1633 } END_FOR_EACH_PTR(sym);
1636 typedef void (fake_cb)(struct expression *expr);
1638 static int member_to_number(struct expression *expr, struct ident *member)
1640 struct symbol *type, *tmp;
1641 char *name;
1642 int i;
1644 if (!member)
1645 return -1;
1646 name = member->name;
1648 type = get_type(expr);
1649 if (!type || type->type != SYM_STRUCT)
1650 return -1;
1652 i = -1;
1653 FOR_EACH_PTR(type->symbol_list, tmp) {
1654 i++;
1655 if (!tmp->ident)
1656 continue;
1657 if (strcmp(name, tmp->ident->name) == 0)
1658 return i;
1659 } END_FOR_EACH_PTR(tmp);
1660 return -1;
1663 static struct ident *number_to_member(struct expression *expr, int num)
1665 struct symbol *type, *member;
1666 int i = 0;
1668 type = get_type(expr);
1669 if (!type || type->type != SYM_STRUCT)
1670 return NULL;
1672 FOR_EACH_PTR(type->symbol_list, member) {
1673 if (i == num)
1674 return member->ident;
1675 i++;
1676 } END_FOR_EACH_PTR(member);
1677 return NULL;
1680 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1682 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1684 struct expression *edge_member, *assign;
1685 struct symbol *base = get_real_base_type(member);
1686 struct symbol *tmp;
1688 if (member->ident)
1689 expr = member_expression(expr, '.', member->ident);
1691 FOR_EACH_PTR(base->symbol_list, tmp) {
1692 struct symbol *type;
1694 type = get_real_base_type(tmp);
1695 if (!type)
1696 continue;
1698 edge_member = member_expression(expr, '.', tmp->ident);
1699 if (get_extra_state(edge_member))
1700 continue;
1702 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1703 set_inner_struct_members(expr, tmp);
1704 continue;
1707 if (!tmp->ident)
1708 continue;
1710 assign = assign_expression(edge_member, '=', zero_expr());
1711 __split_expr(assign);
1712 } END_FOR_EACH_PTR(tmp);
1717 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1719 struct symbol *tmp;
1720 struct expression *member = NULL;
1721 struct expression *assign;
1723 FOR_EACH_PTR(type->symbol_list, tmp) {
1724 type = get_real_base_type(tmp);
1725 if (!type)
1726 continue;
1728 if (tmp->ident) {
1729 member = member_expression(expr, '.', tmp->ident);
1730 if (get_extra_state(member))
1731 continue;
1734 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1735 set_inner_struct_members(expr, tmp);
1736 continue;
1738 if (type->type == SYM_ARRAY)
1739 continue;
1740 if (!tmp->ident)
1741 continue;
1743 assign = assign_expression(member, '=', zero_expr());
1744 __split_expr(assign);
1745 } END_FOR_EACH_PTR(tmp);
1748 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1750 struct expression *deref, *assign, *tmp, *right;
1751 struct symbol *struct_type, *type;
1752 struct ident *member;
1753 int member_idx;
1755 struct_type = get_type(symbol);
1756 if (!struct_type ||
1757 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1758 return;
1761 * We're parsing an initializer that could look something like this:
1762 * struct foo foo = {
1763 * 42,
1764 * .whatever.xxx = 11,
1765 * .zzz = 12,
1766 * };
1768 * So what we have here is a list with 42, .whatever, and .zzz. We need
1769 * to break it up into left and right sides of the assignments.
1772 member_idx = 0;
1773 FOR_EACH_PTR(members, tmp) {
1774 deref = NULL;
1775 if (tmp->type == EXPR_IDENTIFIER) {
1776 member_idx = member_to_number(symbol, tmp->expr_ident);
1777 while (tmp->type == EXPR_IDENTIFIER) {
1778 member = tmp->expr_ident;
1779 tmp = tmp->ident_expression;
1780 if (deref)
1781 deref = member_expression(deref, '.', member);
1782 else
1783 deref = member_expression(symbol, '.', member);
1785 } else {
1786 member = number_to_member(symbol, member_idx);
1787 deref = member_expression(symbol, '.', member);
1789 right = tmp;
1790 member_idx++;
1791 if (right->type == EXPR_INITIALIZER) {
1792 type = get_type(deref);
1793 if (type && type->type == SYM_ARRAY)
1794 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1795 else
1796 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1797 } else {
1798 assign = assign_expression(deref, '=', right);
1799 fake_cb(assign);
1801 } END_FOR_EACH_PTR(tmp);
1803 set_unset_to_zero(struct_type, symbol);
1806 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1808 fake_member_assigns_helper(symbol_expression(sym),
1809 sym->initializer->expr_list, fake_cb);
1812 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1814 struct expression *offset, *binop, *assign, *tmp;
1815 struct symbol *type;
1816 int idx, max;
1818 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1819 return;
1821 max = 0;
1822 idx = 0;
1823 FOR_EACH_PTR(expr_list, tmp) {
1824 if (tmp->type == EXPR_INDEX) {
1825 if (tmp->idx_from != tmp->idx_to)
1826 return;
1827 idx = tmp->idx_from;
1828 if (idx > max)
1829 max = idx;
1830 if (!tmp->idx_expression)
1831 goto next;
1832 tmp = tmp->idx_expression;
1834 offset = value_expr(idx);
1835 binop = array_element_expression(array, offset);
1836 if (tmp->type == EXPR_INITIALIZER) {
1837 type = get_type(binop);
1838 if (type && type->type == SYM_ARRAY)
1839 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1840 else
1841 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1842 } else {
1843 assign = assign_expression(binop, '=', tmp);
1844 fake_cb(assign);
1846 next:
1847 idx++;
1848 if (idx > max)
1849 max = idx;
1850 } END_FOR_EACH_PTR(tmp);
1852 __call_array_initialized_hooks(array, max);
1855 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1857 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1860 static void fake_assign_expr(struct symbol *sym)
1862 struct expression *assign, *symbol;
1864 symbol = symbol_expression(sym);
1865 assign = assign_expression(symbol, '=', sym->initializer);
1866 __split_expr(assign);
1869 static void do_initializer_stuff(struct symbol *sym)
1871 if (!sym->initializer)
1872 return;
1874 if (sym->initializer->type == EXPR_INITIALIZER) {
1875 if (get_real_base_type(sym)->type == SYM_ARRAY)
1876 fake_element_assigns(sym, __split_expr);
1877 else
1878 fake_member_assigns(sym, __split_expr);
1879 } else {
1880 fake_assign_expr(sym);
1884 static void split_declaration(struct symbol_list *sym_list)
1886 struct symbol *sym;
1888 FOR_EACH_PTR(sym_list, sym) {
1889 __pass_to_client(sym, DECLARATION_HOOK);
1890 do_initializer_stuff(sym);
1891 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
1892 split_sym(sym);
1893 } END_FOR_EACH_PTR(sym);
1896 static void call_global_assign_hooks(struct expression *assign)
1898 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1901 static void fake_global_assign(struct symbol *sym)
1903 struct expression *assign, *symbol;
1905 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1906 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1907 fake_element_assigns(sym, call_global_assign_hooks);
1908 } else if (sym->initializer) {
1909 symbol = symbol_expression(sym);
1910 assign = assign_expression(symbol, '=', sym->initializer);
1911 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1912 } else {
1913 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1915 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1916 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1917 fake_member_assigns(sym, call_global_assign_hooks);
1918 } else if (sym->initializer) {
1919 symbol = symbol_expression(sym);
1920 assign = assign_expression(symbol, '=', sym->initializer);
1921 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1922 } else {
1923 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1925 } else {
1926 symbol = symbol_expression(sym);
1927 if (sym->initializer) {
1928 assign = assign_expression(symbol, '=', sym->initializer);
1929 __split_expr(assign);
1930 } else {
1931 assign = assign_expression(symbol, '=', zero_expr());
1933 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1937 static void start_function_definition(struct symbol *sym)
1939 __in_function_def = 1;
1940 __pass_to_client(sym, FUNC_DEF_HOOK);
1941 __in_function_def = 0;
1942 __pass_to_client(sym, AFTER_DEF_HOOK);
1946 void add_function_data(unsigned long *fn_data)
1948 __add_ptr_list(&fn_data_list, fn_data);
1951 static void clear_function_data(void)
1953 unsigned long *tmp;
1955 FOR_EACH_PTR(fn_data_list, tmp) {
1956 *tmp = 0;
1957 } END_FOR_EACH_PTR(tmp);
1960 static void record_func_time(void)
1962 struct timeval stop;
1963 int func_time;
1964 char buf[32];
1966 gettimeofday(&stop, NULL);
1967 func_time = stop.tv_sec - fn_start_time.tv_sec;
1968 snprintf(buf, sizeof(buf), "%d", func_time);
1969 sql_insert_return_implies(FUNC_TIME, 0, "", buf);
1970 if (option_time && func_time > 2) {
1971 final_pass++;
1972 sm_msg("func_time: %d", func_time);
1973 final_pass--;
1977 static void split_function(struct symbol *sym)
1979 struct symbol *base_type = get_base_type(sym);
1981 if (!base_type->stmt && !base_type->inline_stmt)
1982 return;
1984 gettimeofday(&outer_fn_start_time, NULL);
1985 gettimeofday(&fn_start_time, NULL);
1986 cur_func_sym = sym;
1987 if (sym->ident)
1988 cur_func = sym->ident->name;
1989 if (option_process_function && cur_func &&
1990 strcmp(option_process_function, cur_func) != 0)
1991 return;
1992 set_position(sym->pos);
1993 clear_function_data();
1994 loop_count = 0;
1995 last_goto_statement_handled = 0;
1996 sm_debug("new function: %s\n", cur_func);
1997 __stree_id = 0;
1998 if (option_two_passes) {
1999 __unnullify_path();
2000 loop_num = 0;
2001 final_pass = 0;
2002 start_function_definition(sym);
2003 __split_stmt(base_type->stmt);
2004 __split_stmt(base_type->inline_stmt);
2005 nullify_path();
2007 __unnullify_path();
2008 loop_num = 0;
2009 final_pass = 1;
2010 start_function_definition(sym);
2011 __split_stmt(base_type->stmt);
2012 __split_stmt(base_type->inline_stmt);
2013 if (!__path_is_null() &&
2014 cur_func_return_type() == &void_ctype &&
2015 !__bail_on_rest_of_function) {
2016 __pass_to_client(NULL, RETURN_HOOK);
2017 nullify_path();
2019 __pass_to_client(sym, END_FUNC_HOOK);
2020 if (need_delayed_scope_hooks())
2021 __call_scope_hooks();
2022 __pass_to_client(sym, AFTER_FUNC_HOOK);
2023 sym->parsed = true;
2025 clear_all_states();
2027 record_func_time();
2029 cur_func_sym = NULL;
2030 cur_func = NULL;
2031 free_data_info_allocs();
2032 free_expression_stack(&switch_expr_stack);
2033 __free_ptr_list((struct ptr_list **)&big_statement_stack);
2034 __bail_on_rest_of_function = 0;
2037 static void save_flow_state(void)
2039 unsigned long *tmp;
2041 __add_ptr_list(&backup, INT_PTR(loop_num << 2));
2042 __add_ptr_list(&backup, INT_PTR(loop_count << 2));
2043 __add_ptr_list(&backup, INT_PTR(final_pass << 2));
2045 __add_ptr_list(&backup, big_statement_stack);
2046 __add_ptr_list(&backup, big_expression_stack);
2047 __add_ptr_list(&backup, big_condition_stack);
2048 __add_ptr_list(&backup, switch_expr_stack);
2050 __add_ptr_list(&backup, cur_func_sym);
2052 __add_ptr_list(&backup, parsed_calls);
2054 __add_ptr_list(&backup, __prev_stmt);
2055 __add_ptr_list(&backup, __cur_stmt);
2056 __add_ptr_list(&backup, __next_stmt);
2058 FOR_EACH_PTR(fn_data_list, tmp) {
2059 __add_ptr_list(&backup, (void *)*tmp);
2060 } END_FOR_EACH_PTR(tmp);
2063 static void *pop_backup(void)
2065 void *ret;
2067 ret = last_ptr_list(backup);
2068 delete_ptr_list_last(&backup);
2069 return ret;
2072 static void restore_flow_state(void)
2074 unsigned long *tmp;
2076 FOR_EACH_PTR_REVERSE(fn_data_list, tmp) {
2077 *tmp = (unsigned long)pop_backup();
2078 } END_FOR_EACH_PTR_REVERSE(tmp);
2080 __next_stmt = pop_backup();
2081 __cur_stmt = pop_backup();
2082 __prev_stmt = pop_backup();
2084 parsed_calls = pop_backup();
2086 cur_func_sym = pop_backup();
2087 switch_expr_stack = pop_backup();
2088 big_condition_stack = pop_backup();
2089 big_expression_stack = pop_backup();
2090 big_statement_stack = pop_backup();
2091 final_pass = PTR_INT(pop_backup()) >> 2;
2092 loop_count = PTR_INT(pop_backup()) >> 2;
2093 loop_num = PTR_INT(pop_backup()) >> 2;
2096 void parse_inline(struct expression *call)
2098 struct symbol *base_type;
2099 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
2100 struct timeval time_backup = fn_start_time;
2101 struct expression *orig_inline = __inline_fn;
2102 int orig_budget;
2104 if (out_of_memory() || taking_too_long())
2105 return;
2107 if (already_parsed_call(call))
2108 return;
2110 save_flow_state();
2112 __pass_to_client(call, INLINE_FN_START);
2113 final_pass = 0; /* don't print anything */
2114 __inline_fn = call;
2115 orig_budget = inline_budget;
2116 inline_budget = inline_budget - 5;
2118 base_type = get_base_type(call->fn->symbol);
2119 cur_func_sym = call->fn->symbol;
2120 if (call->fn->symbol->ident)
2121 cur_func = call->fn->symbol->ident->name;
2122 else
2123 cur_func = NULL;
2124 set_position(call->fn->symbol->pos);
2126 save_all_states();
2127 big_statement_stack = NULL;
2128 big_expression_stack = NULL;
2129 big_condition_stack = NULL;
2130 switch_expr_stack = NULL;
2131 parsed_calls = NULL;
2133 sm_debug("inline function: %s\n", cur_func);
2134 __unnullify_path();
2135 clear_function_data();
2136 loop_num = 0;
2137 loop_count = 0;
2138 start_function_definition(call->fn->symbol);
2139 __split_stmt(base_type->stmt);
2140 __split_stmt(base_type->inline_stmt);
2141 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
2142 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
2143 call->fn->symbol->parsed = true;
2145 free_expression_stack(&switch_expr_stack);
2146 __free_ptr_list((struct ptr_list **)&big_statement_stack);
2147 nullify_path();
2148 free_goto_stack();
2150 restore_flow_state();
2151 fn_start_time = time_backup;
2152 cur_func = cur_func_bak;
2154 restore_all_states();
2155 set_position(call->pos);
2156 __inline_fn = orig_inline;
2157 inline_budget = orig_budget;
2158 __pass_to_client(call, INLINE_FN_END);
2161 static struct symbol_list *inlines_called;
2162 static void add_inline_function(struct symbol *sym)
2164 static struct symbol_list *already_added;
2165 struct symbol *tmp;
2167 FOR_EACH_PTR(already_added, tmp) {
2168 if (tmp == sym)
2169 return;
2170 } END_FOR_EACH_PTR(tmp);
2172 add_ptr_list(&already_added, sym);
2173 add_ptr_list(&inlines_called, sym);
2176 static void process_inlines(void)
2178 struct symbol *tmp;
2180 FOR_EACH_PTR(inlines_called, tmp) {
2181 split_function(tmp);
2182 } END_FOR_EACH_PTR(tmp);
2183 free_ptr_list(&inlines_called);
2186 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
2188 struct symbol *sym;
2190 FOR_EACH_PTR_REVERSE(big_list, sym) {
2191 if (!sym->scope)
2192 continue;
2193 if (use_static && sym->ctype.modifiers & MOD_STATIC)
2194 return sym;
2195 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
2196 return sym;
2197 } END_FOR_EACH_PTR_REVERSE(sym);
2199 return NULL;
2202 static bool interesting_function(struct symbol *sym)
2204 static int prev_stream = -1;
2205 static bool prev_answer;
2206 const char *filename;
2207 int len;
2209 if (!(sym->ctype.modifiers & MOD_INLINE))
2210 return true;
2212 if (sym->pos.stream == prev_stream)
2213 return prev_answer;
2215 prev_stream = sym->pos.stream;
2216 prev_answer = false;
2218 filename = stream_name(sym->pos.stream);
2219 len = strlen(filename);
2220 if (len > 0 && filename[len - 1] == 'c')
2221 prev_answer = true;
2222 return prev_answer;
2225 static void split_inlines_in_scope(struct symbol *sym)
2227 struct symbol *base;
2228 struct symbol_list *scope_list;
2229 int stream;
2231 scope_list = sym->scope->symbols;
2232 stream = sym->pos.stream;
2234 /* find the last static symbol in the file */
2235 FOR_EACH_PTR_REVERSE(scope_list, sym) {
2236 if (sym->pos.stream != stream)
2237 continue;
2238 if (sym->type != SYM_NODE)
2239 continue;
2240 base = get_base_type(sym);
2241 if (!base)
2242 continue;
2243 if (base->type != SYM_FN)
2244 continue;
2245 if (!base->inline_stmt)
2246 continue;
2247 if (!interesting_function(sym))
2248 continue;
2249 add_inline_function(sym);
2250 } END_FOR_EACH_PTR_REVERSE(sym);
2252 process_inlines();
2255 static void split_inlines(struct symbol_list *sym_list)
2257 struct symbol *sym;
2259 sym = get_last_scoped_symbol(sym_list, 0);
2260 if (sym)
2261 split_inlines_in_scope(sym);
2262 sym = get_last_scoped_symbol(sym_list, 1);
2263 if (sym)
2264 split_inlines_in_scope(sym);
2267 static struct stree *clone_estates_perm(struct stree *orig)
2269 struct stree *ret = NULL;
2270 struct sm_state *tmp;
2272 FOR_EACH_SM(orig, tmp) {
2273 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
2274 } END_FOR_EACH_SM(tmp);
2276 return ret;
2279 struct position last_pos;
2280 static void split_c_file_functions(struct symbol_list *sym_list)
2282 struct symbol *sym;
2284 __unnullify_path();
2285 FOR_EACH_PTR(sym_list, sym) {
2286 set_position(sym->pos);
2287 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
2288 __pass_to_client(sym, BASE_HOOK);
2289 fake_global_assign(sym);
2290 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
2292 } END_FOR_EACH_PTR(sym);
2293 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
2294 nullify_path();
2296 FOR_EACH_PTR(sym_list, sym) {
2297 set_position(sym->pos);
2298 last_pos = sym->pos;
2299 if (!interesting_function(sym))
2300 continue;
2301 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
2302 split_function(sym);
2303 process_inlines();
2305 last_pos = sym->pos;
2306 } END_FOR_EACH_PTR(sym);
2307 split_inlines(sym_list);
2308 __pass_to_client(sym_list, END_FILE_HOOK);
2311 static int final_before_fake;
2312 void init_fake_env(void)
2314 if (!in_fake_env)
2315 final_before_fake = final_pass;
2316 in_fake_env++;
2317 __push_fake_cur_stree();
2318 final_pass = 0;
2321 void end_fake_env(void)
2323 __free_fake_cur_stree();
2324 in_fake_env--;
2325 if (!in_fake_env)
2326 final_pass = final_before_fake;
2329 static void open_output_files(char *base_file)
2331 char buf[256];
2333 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
2334 sm_outfd = fopen(buf, "w");
2335 if (!sm_outfd)
2336 sm_fatal("Cannot open %s", buf);
2338 if (!option_info)
2339 return;
2341 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
2342 sql_outfd = fopen(buf, "w");
2343 if (!sql_outfd)
2344 sm_fatal("Error: Cannot open %s", buf);
2346 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
2347 caller_info_fd = fopen(buf, "w");
2348 if (!caller_info_fd)
2349 sm_fatal("Error: Cannot open %s", buf);
2352 void smatch(struct string_list *filelist)
2354 struct symbol_list *sym_list;
2355 struct timeval stop, start;
2356 char *path;
2357 int len;
2359 gettimeofday(&start, NULL);
2361 FOR_EACH_PTR_NOTAG(filelist, base_file) {
2362 path = getcwd(NULL, 0);
2363 free(full_base_file);
2364 if (path) {
2365 len = strlen(path) + 1 + strlen(base_file) + 1;
2366 full_base_file = malloc(len);
2367 snprintf(full_base_file, len, "%s/%s", path, base_file);
2368 } else {
2369 full_base_file = alloc_string(base_file);
2371 if (option_file_output)
2372 open_output_files(base_file);
2373 base_file_stream = input_stream_nr;
2374 sym_list = sparse_keep_tokens(base_file);
2375 split_c_file_functions(sym_list);
2376 } END_FOR_EACH_PTR_NOTAG(base_file);
2378 gettimeofday(&stop, NULL);
2380 set_position(last_pos);
2381 final_pass = 1;
2382 if (option_time)
2383 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
2384 if (option_mem)
2385 sm_msg("mem: %luKb", get_max_memory());