stored_conditions: ignore known conditions more thoroughly
[smatch.git] / smatch_flow.c
blobdb6f9c35407fac7e625de845fc1bc5b66c5fd5b1
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 *fn_data_list;
54 static struct ptr_list *backup;
56 struct expression_list *big_expression_stack;
57 struct statement_list *big_statement_stack;
58 struct statement *__prev_stmt;
59 struct statement *__cur_stmt;
60 struct statement *__next_stmt;
61 int __in_pre_condition = 0;
62 int __bail_on_rest_of_function = 0;
63 static struct timeval fn_start_time;
64 static struct timeval outer_fn_start_time;
65 char *get_function(void) { return cur_func; }
66 int get_lineno(void) { return __smatch_lineno; }
67 int inside_loop(void) { return !!loop_count; }
68 int definitely_inside_loop(void) { return !!(loop_count & ~0x08000000); }
69 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
70 int in_expression_statement(void) { return !!__expr_stmt_count; }
72 static void split_symlist(struct symbol_list *sym_list);
73 static void split_declaration(struct symbol_list *sym_list);
74 static void split_expr_list(struct expression_list *expr_list, struct expression *parent);
75 static void split_args(struct expression *expr);
76 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *call, struct expression *expr, int nr);
77 static void add_inline_function(struct symbol *sym);
78 static void parse_inline(struct expression *expr);
80 int option_assume_loops = 0;
81 int option_two_passes = 0;
82 struct symbol *cur_func_sym = NULL;
83 struct stree *global_states;
85 const unsigned long valid_ptr_min = 4096;
86 unsigned long valid_ptr_max = ULONG_MAX & ~(MTAG_OFFSET_MASK);
87 const sval_t valid_ptr_min_sval = {
88 .type = &ptr_ctype,
89 {.value = 4096},
91 sval_t valid_ptr_max_sval = {
92 .type = &ptr_ctype,
93 {.value = ULONG_MAX & ~(MTAG_OFFSET_MASK)},
95 struct range_list *valid_ptr_rl;
97 void alloc_valid_ptr_rl(void)
99 valid_ptr_max = sval_type_max(&ulong_ctype).value & ~(MTAG_OFFSET_MASK);
100 valid_ptr_max_sval.value = valid_ptr_max;
102 valid_ptr_rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
103 valid_ptr_rl = cast_rl(&ptr_ctype, valid_ptr_rl);
104 valid_ptr_rl = clone_rl_permanent(valid_ptr_rl);
107 int outside_of_function(void)
109 return cur_func_sym == NULL;
112 const char *get_filename(void)
114 if (option_info && option_full_path)
115 return full_base_file;
116 if (option_info)
117 return base_file;
118 if (option_full_path)
119 return full_filename;
120 return filename;
123 const char *get_base_file(void)
125 if (option_full_path)
126 return full_base_file;
127 return base_file;
130 static void set_position(struct position pos)
132 int len;
133 static int prev_stream = -1;
135 if (in_fake_env)
136 return;
138 if (pos.stream == 0 && pos.line == 0)
139 return;
141 __smatch_lineno = pos.line;
143 if (pos.stream == prev_stream)
144 return;
146 filename = stream_name(pos.stream);
148 free(full_filename);
149 pathname = getcwd(NULL, 0);
150 if (pathname) {
151 len = strlen(pathname) + 1 + strlen(filename) + 1;
152 full_filename = malloc(len);
153 snprintf(full_filename, len, "%s/%s", pathname, filename);
154 } else {
155 full_filename = alloc_string(filename);
157 free(pathname);
160 int is_assigned_call(struct expression *expr)
162 struct expression *parent = expr_get_parent_expr(expr);
164 if (parent &&
165 parent->type == EXPR_ASSIGNMENT &&
166 parent->op == '=' &&
167 strip_expr(parent->right) == expr)
168 return 1;
170 return 0;
173 int is_fake_assigned_call(struct expression *expr)
175 struct expression *parent = expr_get_fake_parent_expr(expr);
177 if (parent &&
178 parent->type == EXPR_ASSIGNMENT &&
179 parent->op == '=' &&
180 strip_expr(parent->right) == expr)
181 return 1;
183 return 0;
186 static bool is_inline_func(struct expression *expr)
188 if (expr->type != EXPR_SYMBOL || !expr->symbol)
189 return false;
190 if (!expr->symbol->definition)
191 return false;
192 if (expr->symbol->definition->ctype.modifiers & MOD_INLINE)
193 return true;
195 return 0;
198 static int is_noreturn_func(struct expression *expr)
200 if (expr->type != EXPR_SYMBOL || !expr->symbol)
201 return 0;
202 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
203 return 1;
204 return 0;
207 static int save_func_time(void *_rl, int argc, char **argv, char **azColName)
209 unsigned long *rl = _rl;
211 *rl = strtoul(argv[0], NULL, 10);
212 return 0;
215 static int get_func_time(struct symbol *sym)
217 unsigned long time = 0;
219 run_sql(&save_func_time, &time,
220 "select key from return_implies where %s and type = %d;",
221 get_static_filter(sym), FUNC_TIME);
223 return time;
226 static int inline_budget = 20;
228 int inlinable(struct expression *expr)
230 struct symbol *sym;
231 struct statement *last_stmt = NULL;
233 if (__inline_fn) /* don't nest */
234 return 0;
236 if (expr->type != EXPR_SYMBOL || !expr->symbol)
237 return 0;
238 if (is_no_inline_function(expr->symbol->ident->name))
239 return 0;
240 sym = get_base_type(expr->symbol);
241 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
242 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
243 return 0;
244 if (sym->stmt->type != STMT_COMPOUND)
245 return 0;
246 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
248 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
249 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
250 return 0;
251 if (sym->inline_stmt->type != STMT_COMPOUND)
252 return 0;
253 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
256 if (!last_stmt)
257 return 0;
259 /* the magic numbers in this function are pulled out of my bum. */
260 if (last_stmt->pos.line > sym->pos.line + inline_budget)
261 return 0;
263 if (get_func_time(expr->symbol) >= 2)
264 return 0;
266 return 1;
269 void __process_post_op_stack(void)
271 struct expression *expr;
273 FOR_EACH_PTR(post_op_stack, expr) {
274 __pass_to_client(expr, OP_HOOK);
275 } END_FOR_EACH_PTR(expr);
277 __free_ptr_list((struct ptr_list **)&post_op_stack);
280 static int handle_comma_assigns(struct expression *expr)
282 struct expression *right;
283 struct expression *assign;
285 right = strip_expr(expr->right);
286 if (right->type != EXPR_COMMA)
287 return 0;
289 __split_expr(right->left);
290 __process_post_op_stack();
292 assign = assign_expression(expr->left, '=', right->right);
293 __split_expr(assign);
295 return 1;
298 /* This is to handle *p++ = foo; assignments */
299 static int handle_postop_assigns(struct expression *expr)
301 struct expression *left, *fake_left;
302 struct expression *assign;
304 left = strip_expr(expr->left);
305 if (left->type != EXPR_PREOP || left->op != '*')
306 return 0;
307 left = strip_expr(left->unop);
308 if (left->type != EXPR_POSTOP)
309 return 0;
311 fake_left = deref_expression(strip_expr(left->unop));
312 assign = assign_expression(fake_left, '=', expr->right);
314 __split_expr(assign);
315 __split_expr(expr->left);
317 return 1;
320 static int prev_expression_is_getting_address(struct expression *expr)
322 struct expression *parent;
324 do {
325 parent = expr_get_parent_expr(expr);
327 if (!parent)
328 return 0;
329 if (parent->type == EXPR_PREOP && parent->op == '&')
330 return 1;
331 if (parent->type == EXPR_PREOP && parent->op == '(')
332 goto next;
333 if (parent->type == EXPR_DEREF && parent->op == '.')
334 goto next;
336 return 0;
337 next:
338 expr = parent;
339 } while (1);
342 static void handle_builtin_overflow_func(struct expression *expr)
344 struct expression *a, *b, *res, *assign;
345 int op;
347 if (sym_name_is("__builtin_add_overflow", expr->fn))
348 op = '+';
349 else if (sym_name_is("__builtin_sub_overflow", expr->fn))
350 op = '-';
351 else if (sym_name_is("__builtin_mul_overflow", expr->fn))
352 op = '*';
353 else
354 return;
356 a = get_argument_from_call_expr(expr->args, 0);
357 b = get_argument_from_call_expr(expr->args, 1);
358 res = get_argument_from_call_expr(expr->args, 2);
360 assign = assign_expression(deref_expression(res), '=', binop_expression(a, op, b));
361 __split_expr(assign);
364 static int handle__builtin_choose_expr(struct expression *expr)
366 struct expression *const_expr, *expr1, *expr2;
367 sval_t sval;
369 if (!sym_name_is("__builtin_choose_expr", expr->fn))
370 return 0;
372 const_expr = get_argument_from_call_expr(expr->args, 0);
373 expr1 = get_argument_from_call_expr(expr->args, 1);
374 expr2 = get_argument_from_call_expr(expr->args, 2);
376 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
377 return 0;
378 if (sval.value)
379 __split_expr(expr1);
380 else
381 __split_expr(expr2);
382 return 1;
385 static int handle__builtin_choose_expr_assigns(struct expression *expr)
387 struct expression *const_expr, *right, *expr1, *expr2, *fake;
388 sval_t sval;
390 right = strip_expr(expr->right);
391 if (right->type != EXPR_CALL)
392 return 0;
393 if (!sym_name_is("__builtin_choose_expr", right->fn))
394 return 0;
396 const_expr = get_argument_from_call_expr(right->args, 0);
397 expr1 = get_argument_from_call_expr(right->args, 1);
398 expr2 = get_argument_from_call_expr(right->args, 2);
400 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
401 return 0;
403 fake = assign_expression(expr->left, '=', sval.value ? expr1 : expr2);
404 __split_expr(fake);
405 return 1;
408 void __split_expr(struct expression *expr)
410 if (!expr)
411 return;
413 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
415 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
416 return;
417 if (__in_fake_assign >= 4) /* don't allow too much nesting */
418 return;
420 push_expression(&big_expression_stack, expr);
421 set_position(expr->pos);
422 __pass_to_client(expr, EXPR_HOOK);
424 switch (expr->type) {
425 case EXPR_PREOP:
426 expr_set_parent_expr(expr->unop, expr);
428 if (expr->op == '*' &&
429 !prev_expression_is_getting_address(expr))
430 __pass_to_client(expr, DEREF_HOOK);
431 __split_expr(expr->unop);
432 __pass_to_client(expr, OP_HOOK);
433 break;
434 case EXPR_POSTOP:
435 expr_set_parent_expr(expr->unop, expr);
437 __split_expr(expr->unop);
438 push_expression(&post_op_stack, expr);
439 break;
440 case EXPR_STATEMENT:
441 __expr_stmt_count++;
442 if (expr->statement && !expr->statement) {
443 stmt_set_parent_stmt(expr->statement,
444 last_ptr_list((struct ptr_list *)big_statement_stack));
446 __split_stmt(expr->statement);
447 __expr_stmt_count--;
448 break;
449 case EXPR_LOGICAL:
450 case EXPR_COMPARE:
451 expr_set_parent_expr(expr->left, expr);
452 expr_set_parent_expr(expr->right, expr);
454 __pass_to_client(expr, LOGIC_HOOK);
455 __handle_logic(expr);
456 break;
457 case EXPR_BINOP:
458 expr_set_parent_expr(expr->left, expr);
459 expr_set_parent_expr(expr->right, expr);
461 __pass_to_client(expr, BINOP_HOOK);
462 case EXPR_COMMA:
463 expr_set_parent_expr(expr->left, expr);
464 expr_set_parent_expr(expr->right, expr);
466 __split_expr(expr->left);
467 __process_post_op_stack();
468 __split_expr(expr->right);
469 break;
470 case EXPR_ASSIGNMENT: {
471 struct expression *right;
473 expr_set_parent_expr(expr->left, expr);
474 expr_set_parent_expr(expr->right, expr);
476 right = strip_expr(expr->right);
477 if (!right)
478 break;
480 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
482 /* foo = !bar() */
483 if (__handle_condition_assigns(expr))
484 goto after_assign;
485 /* foo = (x < 5 ? foo : 5); */
486 if (__handle_select_assigns(expr))
487 goto after_assign;
488 /* foo = ({frob(); frob(); frob(); 1;}) */
489 if (__handle_expr_statement_assigns(expr))
490 break; // FIXME: got after
491 /* foo = (3, 4); */
492 if (handle_comma_assigns(expr))
493 goto after_assign;
494 if (handle__builtin_choose_expr_assigns(expr))
495 goto after_assign;
496 if (handle_postop_assigns(expr))
497 break; /* no need to goto after_assign */
499 __split_expr(expr->right);
500 if (outside_of_function())
501 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
502 else
503 __pass_to_client(expr, ASSIGNMENT_HOOK);
505 __fake_struct_member_assignments(expr);
507 /* Re-examine ->right for inlines. See the commit message */
508 right = strip_expr(expr->right);
509 if (expr->op == '=' && right->type == EXPR_CALL)
510 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
512 after_assign:
513 if (get_macro_name(right->pos) &&
514 get_macro_name(expr->pos) != get_macro_name(right->pos))
515 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
517 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
518 __split_expr(expr->left);
519 break;
521 case EXPR_DEREF:
522 expr_set_parent_expr(expr->deref, expr);
524 __pass_to_client(expr, DEREF_HOOK);
525 __split_expr(expr->deref);
526 break;
527 case EXPR_SLICE:
528 expr_set_parent_expr(expr->base, expr);
530 __split_expr(expr->base);
531 break;
532 case EXPR_CAST:
533 case EXPR_FORCE_CAST:
534 expr_set_parent_expr(expr->cast_expression, expr);
536 __pass_to_client(expr, CAST_HOOK);
537 __split_expr(expr->cast_expression);
538 break;
539 case EXPR_SIZEOF:
540 if (expr->cast_expression)
541 __pass_to_client(strip_parens(expr->cast_expression),
542 SIZEOF_HOOK);
543 break;
544 case EXPR_OFFSETOF:
545 case EXPR_ALIGNOF:
546 break;
547 case EXPR_CONDITIONAL:
548 case EXPR_SELECT:
549 expr_set_parent_expr(expr->conditional, expr);
550 expr_set_parent_expr(expr->cond_true, expr);
551 expr_set_parent_expr(expr->cond_false, expr);
553 if (known_condition_true(expr->conditional)) {
554 __split_expr(expr->cond_true);
555 break;
557 if (known_condition_false(expr->conditional)) {
558 __split_expr(expr->cond_false);
559 break;
561 __pass_to_client(expr, SELECT_HOOK);
562 __split_whole_condition(expr->conditional);
563 __split_expr(expr->cond_true);
564 __push_true_states();
565 __use_false_states();
566 __split_expr(expr->cond_false);
567 __merge_true_states();
568 break;
569 case EXPR_CALL:
570 expr_set_parent_expr(expr->fn, expr);
572 if (sym_name_is("__builtin_constant_p", expr->fn))
573 break;
574 if (handle__builtin_choose_expr(expr))
575 break;
576 __split_expr(expr->fn);
577 split_args(expr);
578 if (is_inline_func(expr->fn))
579 add_inline_function(expr->fn->symbol->definition);
580 if (inlinable(expr->fn))
581 __inline_call = 1;
582 __process_post_op_stack();
583 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
584 __pass_to_client(expr, FUNCTION_CALL_HOOK);
585 __inline_call = 0;
586 if (inlinable(expr->fn))
587 parse_inline(expr);
588 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
589 if (is_noreturn_func(expr->fn))
590 nullify_path();
591 if (!expr_get_parent_expr(expr))
592 __discard_fake_states(expr);
593 handle_builtin_overflow_func(expr);
594 break;
595 case EXPR_INITIALIZER:
596 split_expr_list(expr->expr_list, expr);
597 break;
598 case EXPR_IDENTIFIER:
599 expr_set_parent_expr(expr->ident_expression, expr);
600 __split_expr(expr->ident_expression);
601 break;
602 case EXPR_INDEX:
603 expr_set_parent_expr(expr->idx_expression, expr);
604 __split_expr(expr->idx_expression);
605 break;
606 case EXPR_POS:
607 expr_set_parent_expr(expr->init_expr, expr);
608 __split_expr(expr->init_expr);
609 break;
610 case EXPR_SYMBOL:
611 __pass_to_client(expr, SYM_HOOK);
612 break;
613 case EXPR_STRING:
614 __pass_to_client(expr, STRING_HOOK);
615 break;
616 default:
617 break;
619 __pass_to_client(expr, EXPR_HOOK_AFTER);
620 pop_expression(&big_expression_stack);
623 static int is_forever_loop(struct statement *stmt)
625 struct expression *expr;
626 sval_t sval;
628 expr = strip_expr(stmt->iterator_pre_condition);
629 if (!expr)
630 expr = stmt->iterator_post_condition;
631 if (!expr) {
632 /* this is a for(;;) loop... */
633 return 1;
636 if (get_value(expr, &sval) && sval.value != 0)
637 return 1;
639 return 0;
642 static int loop_num;
643 static char *get_loop_name(int num)
645 char buf[256];
647 snprintf(buf, 255, "-loop%d", num);
648 buf[255] = '\0';
649 return alloc_sname(buf);
653 * Pre Loops are while and for loops.
655 static void handle_pre_loop(struct statement *stmt)
657 int once_through; /* we go through the loop at least once */
658 struct sm_state *extra_sm = NULL;
659 int unchanged = 0;
660 char *loop_name;
661 struct stree *stree = NULL;
662 struct sm_state *sm = NULL;
664 loop_name = get_loop_name(loop_num);
665 loop_num++;
667 if (stmt->iterator_pre_statement) {
668 __split_stmt(stmt->iterator_pre_statement);
669 __prev_stmt = stmt->iterator_pre_statement;
672 once_through = implied_condition_true(stmt->iterator_pre_condition);
674 loop_count++;
675 __push_continues();
676 __push_breaks();
678 __merge_gotos(loop_name, NULL);
680 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
681 __in_pre_condition++;
682 __pass_to_client(stmt, PRELOOP_HOOK);
683 __split_whole_condition(stmt->iterator_pre_condition);
684 __in_pre_condition--;
685 FOR_EACH_SM(stree, sm) {
686 set_state(sm->owner, sm->name, sm->sym, sm->state);
687 } END_FOR_EACH_SM(sm);
688 free_stree(&stree);
689 if (extra_sm)
690 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
692 if (option_assume_loops)
693 once_through = 1;
695 __split_stmt(stmt->iterator_statement);
696 if (is_forever_loop(stmt)) {
697 __merge_continues();
698 __save_gotos(loop_name, NULL);
700 __push_fake_cur_stree();
701 __split_stmt(stmt->iterator_post_statement);
702 stree = __pop_fake_cur_stree();
704 __discard_false_states();
705 __use_breaks();
707 if (!__path_is_null())
708 __merge_stree_into_cur(stree);
709 free_stree(&stree);
710 } else {
711 __merge_continues();
712 unchanged = __iterator_unchanged(extra_sm);
713 __split_stmt(stmt->iterator_post_statement);
714 __prev_stmt = stmt->iterator_post_statement;
715 __cur_stmt = stmt;
717 __save_gotos(loop_name, NULL);
718 __in_pre_condition++;
719 __split_whole_condition(stmt->iterator_pre_condition);
720 __in_pre_condition--;
721 nullify_path();
722 __merge_false_states();
723 if (once_through)
724 __discard_false_states();
725 else
726 __merge_false_states();
728 if (extra_sm && unchanged)
729 __extra_pre_loop_hook_after(extra_sm,
730 stmt->iterator_post_statement,
731 stmt->iterator_pre_condition);
732 __merge_breaks();
734 loop_count--;
738 * Post loops are do {} while();
740 static void handle_post_loop(struct statement *stmt)
742 char *loop_name;
744 loop_name = get_loop_name(loop_num);
745 loop_num++;
746 loop_count++;
748 __push_continues();
749 __push_breaks();
750 __merge_gotos(loop_name, NULL);
751 __split_stmt(stmt->iterator_statement);
752 __merge_continues();
753 if (!expr_is_zero(stmt->iterator_post_condition))
754 __save_gotos(loop_name, NULL);
756 if (is_forever_loop(stmt)) {
757 __use_breaks();
758 } else {
759 __split_whole_condition(stmt->iterator_post_condition);
760 __use_false_states();
761 __merge_breaks();
763 loop_count--;
766 static int empty_statement(struct statement *stmt)
768 if (!stmt)
769 return 0;
770 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
771 return 1;
772 return 0;
775 static int last_stmt_on_same_line(void)
777 struct statement *stmt;
778 int i = 0;
780 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
781 if (!i++)
782 continue;
783 if (stmt->pos.line == get_lineno())
784 return 1;
785 return 0;
786 } END_FOR_EACH_PTR_REVERSE(stmt);
787 return 0;
790 static void split_asm_ops(struct asm_operand_list *ops)
792 struct asm_operand *op;
794 FOR_EACH_PTR(ops, op) {
795 __split_expr(op->expr);
796 } END_FOR_EACH_PTR(op);
799 static int is_case_val(struct statement *stmt, sval_t sval)
801 sval_t case_sval;
803 if (stmt->type != STMT_CASE)
804 return 0;
805 if (!stmt->case_expression) {
806 __set_default();
807 return 1;
809 if (!get_value(stmt->case_expression, &case_sval))
810 return 0;
811 if (case_sval.value == sval.value)
812 return 1;
813 return 0;
816 static struct range_list *get_case_rl(struct expression *switch_expr,
817 struct expression *case_expr,
818 struct expression *case_to)
820 sval_t start, end;
821 struct range_list *rl = NULL;
822 struct symbol *switch_type;
824 switch_type = get_type(switch_expr);
825 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
826 start = sval_cast(switch_type, start);
827 end = sval_cast(switch_type, end);
828 add_range(&rl, start, end);
829 } else if (get_value(case_expr, &start)) {
830 start = sval_cast(switch_type, start);
831 add_range(&rl, start, start);
834 return rl;
837 static void split_known_switch(struct statement *stmt, sval_t sval)
839 struct statement *tmp;
840 struct range_list *rl;
842 __split_expr(stmt->switch_expression);
843 sval = sval_cast(get_type(stmt->switch_expression), sval);
845 push_expression(&switch_expr_stack, stmt->switch_expression);
846 __save_switch_states(top_expression(switch_expr_stack));
847 nullify_path();
848 __push_default();
849 __push_breaks();
851 stmt = stmt->switch_statement;
853 __push_scope_hooks();
854 FOR_EACH_PTR(stmt->stmts, tmp) {
855 __smatch_lineno = tmp->pos.line;
856 if (is_case_val(tmp, sval)) {
857 rl = alloc_rl(sval, sval);
858 __merge_switches(top_expression(switch_expr_stack), rl);
859 __pass_case_to_client(top_expression(switch_expr_stack), rl);
861 if (__path_is_null())
862 continue;
863 __split_stmt(tmp);
864 if (__path_is_null()) {
865 __set_default();
866 goto out;
868 } END_FOR_EACH_PTR(tmp);
869 out:
870 __call_scope_hooks();
871 if (!__pop_default())
872 __merge_switches(top_expression(switch_expr_stack), NULL);
873 __discard_switches();
874 __merge_breaks();
875 pop_expression(&switch_expr_stack);
878 static void split_case(struct statement *stmt)
880 struct range_list *rl = NULL;
882 expr_set_parent_stmt(stmt->case_expression, stmt);
883 expr_set_parent_stmt(stmt->case_to, stmt);
885 rl = get_case_rl(top_expression(switch_expr_stack),
886 stmt->case_expression, stmt->case_to);
887 while (stmt->case_statement->type == STMT_CASE) {
888 struct range_list *tmp;
890 tmp = get_case_rl(top_expression(switch_expr_stack),
891 stmt->case_statement->case_expression,
892 stmt->case_statement->case_to);
893 if (!tmp)
894 break;
895 rl = rl_union(rl, tmp);
896 if (!stmt->case_expression)
897 __set_default();
898 stmt = stmt->case_statement;
901 __merge_switches(top_expression(switch_expr_stack), rl);
903 if (!stmt->case_expression)
904 __set_default();
906 stmt_set_parent_stmt(stmt->case_statement, stmt);
907 __split_stmt(stmt->case_statement);
910 int time_parsing_function(void)
912 return ms_since(&fn_start_time) / 1000;
915 bool taking_too_long(void)
917 if ((ms_since(&outer_fn_start_time) / 1000) > 60 * 5) /* five minutes */
918 return 1;
919 return 0;
922 struct statement *get_last_stmt(void)
924 struct symbol *fn;
925 struct statement *stmt;
927 fn = get_base_type(cur_func_sym);
928 if (!fn)
929 return NULL;
930 stmt = fn->stmt;
931 if (!stmt)
932 stmt = fn->inline_stmt;
933 if (!stmt || stmt->type != STMT_COMPOUND)
934 return NULL;
935 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
936 if (stmt && stmt->type == STMT_LABEL)
937 stmt = stmt->label_statement;
938 return stmt;
941 int is_last_stmt(struct statement *cur_stmt)
943 struct statement *last;
945 last = get_last_stmt();
946 if (last && last == cur_stmt)
947 return 1;
948 return 0;
951 static void handle_backward_goto(struct statement *goto_stmt)
953 const char *goto_name, *label_name;
954 struct statement *func_stmt;
955 struct symbol *base_type = get_base_type(cur_func_sym);
956 struct statement *tmp;
957 int found = 0;
959 if (!option_info)
960 return;
961 if (last_goto_statement_handled)
962 return;
963 last_goto_statement_handled = 1;
965 if (!goto_stmt->goto_label ||
966 goto_stmt->goto_label->type != SYM_LABEL ||
967 !goto_stmt->goto_label->ident)
968 return;
969 goto_name = goto_stmt->goto_label->ident->name;
971 func_stmt = base_type->stmt;
972 if (!func_stmt)
973 func_stmt = base_type->inline_stmt;
974 if (!func_stmt)
975 return;
976 if (func_stmt->type != STMT_COMPOUND)
977 return;
979 FOR_EACH_PTR(func_stmt->stmts, tmp) {
980 if (!found) {
981 if (tmp->type != STMT_LABEL)
982 continue;
983 if (!tmp->label_identifier ||
984 tmp->label_identifier->type != SYM_LABEL ||
985 !tmp->label_identifier->ident)
986 continue;
987 label_name = tmp->label_identifier->ident->name;
988 if (strcmp(goto_name, label_name) != 0)
989 continue;
990 found = 1;
992 __split_stmt(tmp);
993 } END_FOR_EACH_PTR(tmp);
996 static void fake_a_return(void)
998 struct expression *ret = NULL;
1000 nullify_path();
1001 __unnullify_path();
1003 if (cur_func_return_type() != &void_ctype)
1004 ret = unknown_value_expression(NULL);
1006 __pass_to_client(ret, RETURN_HOOK);
1007 nullify_path();
1010 static void split_ret_value(struct expression *expr)
1012 struct symbol *type;
1014 if (!expr)
1015 return;
1017 type = get_real_base_type(cur_func_sym);
1018 type = get_real_base_type(type);
1019 expr = fake_a_variable_assign(type, NULL, expr, -1);
1021 __in_fake_var_assign++;
1022 __split_expr(expr);
1023 __in_fake_var_assign--;
1026 static void fake_an_empty_default(struct position pos)
1028 static struct statement none = {};
1030 none.pos = pos;
1031 none.type = STMT_NONE;
1032 __merge_switches(top_expression(switch_expr_stack), NULL);
1033 __split_stmt(&none);
1036 static void split_compound(struct statement *stmt)
1038 struct statement *prev = NULL;
1039 struct statement *cur = NULL;
1040 struct statement *next;
1042 __push_scope_hooks();
1044 FOR_EACH_PTR(stmt->stmts, next) {
1045 /* just set them all ahead of time */
1046 stmt_set_parent_stmt(next, stmt);
1048 if (cur) {
1049 __prev_stmt = prev;
1050 __next_stmt = next;
1051 __cur_stmt = cur;
1052 __split_stmt(cur);
1054 prev = cur;
1055 cur = next;
1056 } END_FOR_EACH_PTR(next);
1057 if (cur) {
1058 __prev_stmt = prev;
1059 __cur_stmt = cur;
1060 __next_stmt = NULL;
1061 __split_stmt(cur);
1065 * For function scope, then delay calling the scope hooks until the
1066 * end of function hooks can run. I'm not positive this is the right
1067 * thing...
1069 if (!is_last_stmt(cur))
1070 __call_scope_hooks();
1074 * This is a hack, work around for detecting empty functions.
1076 static int need_delayed_scope_hooks(void)
1078 struct symbol *fn = get_base_type(cur_func_sym);
1079 struct statement *stmt;
1081 if (!fn)
1082 return 0;
1083 stmt = fn->stmt;
1084 if (!stmt)
1085 stmt = fn->inline_stmt;
1086 if (stmt && stmt->type == STMT_COMPOUND)
1087 return 1;
1088 return 0;
1091 void __split_label_stmt(struct statement *stmt)
1093 if (stmt->label_identifier &&
1094 stmt->label_identifier->type == SYM_LABEL &&
1095 stmt->label_identifier->ident) {
1096 loop_count |= 0x0800000;
1097 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
1101 static void find_asm_gotos(struct statement *stmt)
1103 struct symbol *sym;
1105 FOR_EACH_PTR(stmt->asm_labels, sym) {
1106 __save_gotos(sym->ident->name, sym);
1107 } END_FOR_EACH_PTR(sym);
1110 void __split_stmt(struct statement *stmt)
1112 static int indent_cnt;
1113 sval_t sval;
1114 struct timeval start, stop;
1116 gettimeofday(&start, NULL);
1118 if (!stmt)
1119 goto out;
1121 if (!__in_fake_assign)
1122 __silence_warnings_for_stmt = false;
1124 if (__bail_on_rest_of_function || is_skipped_function())
1125 return;
1127 if (out_of_memory() || taking_too_long()) {
1128 gettimeofday(&start, NULL);
1130 __bail_on_rest_of_function = 1;
1131 final_pass = 1;
1132 sm_perror("Function too hairy. Giving up. %lu seconds",
1133 start.tv_sec - fn_start_time.tv_sec);
1134 fake_a_return();
1135 final_pass = 0; /* turn off sm_msg() from here */
1136 return;
1139 indent_cnt++;
1141 add_ptr_list(&big_statement_stack, stmt);
1142 free_expression_stack(&big_expression_stack);
1143 set_position(stmt->pos);
1144 __pass_to_client(stmt, STMT_HOOK);
1146 switch (stmt->type) {
1147 case STMT_DECLARATION:
1148 split_declaration(stmt->declaration);
1149 break;
1150 case STMT_RETURN:
1151 expr_set_parent_stmt(stmt->ret_value, stmt);
1153 split_ret_value(stmt->ret_value);
1154 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1155 __process_post_op_stack();
1156 nullify_path();
1157 break;
1158 case STMT_EXPRESSION:
1159 expr_set_parent_stmt(stmt->expression, stmt);
1160 expr_set_parent_stmt(stmt->context, stmt);
1162 __split_expr(stmt->expression);
1163 break;
1164 case STMT_COMPOUND:
1165 split_compound(stmt);
1166 break;
1167 case STMT_IF:
1168 stmt_set_parent_stmt(stmt->if_true, stmt);
1169 stmt_set_parent_stmt(stmt->if_false, stmt);
1170 expr_set_parent_stmt(stmt->if_conditional, stmt);
1172 if (known_condition_true(stmt->if_conditional)) {
1173 __split_stmt(stmt->if_true);
1174 break;
1176 if (known_condition_false(stmt->if_conditional)) {
1177 __split_stmt(stmt->if_false);
1178 break;
1180 __split_whole_condition(stmt->if_conditional);
1181 __split_stmt(stmt->if_true);
1182 if (empty_statement(stmt->if_true) &&
1183 last_stmt_on_same_line() &&
1184 !get_macro_name(stmt->if_true->pos))
1185 sm_warning("if();");
1186 __push_true_states();
1187 __use_false_states();
1188 __split_stmt(stmt->if_false);
1189 __merge_true_states();
1190 break;
1191 case STMT_ITERATOR:
1192 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1193 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1194 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1195 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1196 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1198 if (stmt->iterator_pre_condition)
1199 handle_pre_loop(stmt);
1200 else if (stmt->iterator_post_condition)
1201 handle_post_loop(stmt);
1202 else {
1203 // these are for(;;) type loops.
1204 handle_pre_loop(stmt);
1206 break;
1207 case STMT_SWITCH:
1208 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1209 expr_set_parent_stmt(stmt->switch_expression, stmt);
1211 if (get_value(stmt->switch_expression, &sval)) {
1212 split_known_switch(stmt, sval);
1213 break;
1215 __split_expr(stmt->switch_expression);
1216 push_expression(&switch_expr_stack, stmt->switch_expression);
1217 __save_switch_states(top_expression(switch_expr_stack));
1218 nullify_path();
1219 __push_default();
1220 __push_breaks();
1221 __split_stmt(stmt->switch_statement);
1222 if (!__pop_default() && have_remaining_cases())
1223 fake_an_empty_default(stmt->pos);
1224 __discard_switches();
1225 __merge_breaks();
1226 pop_expression(&switch_expr_stack);
1227 break;
1228 case STMT_CASE:
1229 split_case(stmt);
1230 break;
1231 case STMT_LABEL:
1232 __split_label_stmt(stmt);
1233 __split_stmt(stmt->label_statement);
1234 break;
1235 case STMT_GOTO:
1236 expr_set_parent_stmt(stmt->goto_expression, stmt);
1238 __split_expr(stmt->goto_expression);
1239 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1240 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1241 __process_breaks();
1242 } else if (!strcmp(stmt->goto_label->ident->name,
1243 "continue")) {
1244 __process_continues();
1246 } else if (stmt->goto_label &&
1247 stmt->goto_label->type == SYM_LABEL &&
1248 stmt->goto_label->ident) {
1249 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1251 nullify_path();
1252 if (is_last_stmt(stmt))
1253 handle_backward_goto(stmt);
1254 break;
1255 case STMT_NONE:
1256 break;
1257 case STMT_ASM:
1258 expr_set_parent_stmt(stmt->asm_string, stmt);
1260 find_asm_gotos(stmt);
1261 __pass_to_client(stmt, ASM_HOOK);
1262 __split_expr(stmt->asm_string);
1263 split_asm_ops(stmt->asm_outputs);
1264 split_asm_ops(stmt->asm_inputs);
1265 split_expr_list(stmt->asm_clobbers, NULL);
1266 break;
1267 case STMT_CONTEXT:
1268 break;
1269 case STMT_RANGE:
1270 __split_expr(stmt->range_expression);
1271 __split_expr(stmt->range_low);
1272 __split_expr(stmt->range_high);
1273 break;
1275 __pass_to_client(stmt, STMT_HOOK_AFTER);
1276 if (--indent_cnt == 1)
1277 __discard_fake_states(NULL);
1279 out:
1280 __process_post_op_stack();
1282 gettimeofday(&stop, NULL);
1283 if (option_time_stmt && stmt)
1284 sm_msg("stmt_time%s: %ld",
1285 stmt->type == STMT_COMPOUND ? "_block" : "",
1286 stop.tv_sec - start.tv_sec);
1289 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1291 struct expression *expr;
1293 FOR_EACH_PTR(expr_list, expr) {
1294 expr_set_parent_expr(expr, parent);
1295 __split_expr(expr);
1296 __process_post_op_stack();
1297 } END_FOR_EACH_PTR(expr);
1300 static bool cast_arg(struct symbol *type, struct expression *arg)
1302 struct symbol *orig;
1304 if (!type)
1305 return false;
1307 arg = strip_parens(arg);
1308 if (arg != strip_expr(arg))
1309 return true;
1311 orig = get_type(arg);
1312 if (!orig)
1313 return true;
1314 if (orig == type)
1315 return false;
1317 if (orig->type == SYM_ARRAY && type->type == SYM_PTR)
1318 return true;
1321 * I would have expected that we could just do use (orig == type) but I
1322 * guess for pointers we need to get the basetype to do that comparison.
1326 if (orig->type != SYM_PTR ||
1327 type->type != SYM_PTR) {
1328 if (type_fits(type, orig))
1329 return false;
1330 return true;
1332 orig = get_real_base_type(orig);
1333 type = get_real_base_type(type);
1334 if (orig == type)
1335 return false;
1337 return true;
1340 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *call, struct expression *expr, int nr)
1342 struct expression *var, *assign, *parent;
1343 char buf[64];
1344 bool cast;
1346 if (!expr || !cur_func_sym)
1347 return NULL;
1349 if (expr->type == EXPR_ASSIGNMENT)
1350 return expr;
1352 /* for va_args then we don't know the type */
1353 if (!type)
1354 type = get_type(expr);
1356 cast = cast_arg(type, expr);
1358 * Using expr_to_sym() here is a hack. We want to say that we don't
1359 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1360 * It turns out faking these assignments is way more expensive than I
1361 * would have imagined. I'm not sure why exactly.
1364 if (!cast) {
1366 * if the code is "return *p;" where "p" is a user pointer then
1367 * we want to create a fake assignment so that it sets the state
1368 * in check_kernel_user_data.c.
1371 if (expr->type != EXPR_PREOP &&
1372 expr->op != '*' && expr->op != '&' &&
1373 expr_to_sym(expr))
1374 return expr;
1377 if (nr == -1)
1378 snprintf(buf, sizeof(buf), "__sm_fake_%p", expr);
1379 else
1380 snprintf(buf, sizeof(buf), "__fake_param_%p_%d", call, nr);
1381 var = fake_variable(type, buf);
1382 assign = assign_expression(var, '=', expr);
1383 assign->smatch_flags |= Fake;
1385 parent = expr_get_parent_expr(expr);
1386 expr_set_parent_expr(assign, parent);
1387 expr_set_parent_expr(expr, assign);
1389 __fake_state_cnt++;
1391 return assign;
1394 static void split_args(struct expression *expr)
1396 struct expression *arg, *tmp;
1397 struct symbol *type;
1398 int i;
1400 i = -1;
1401 FOR_EACH_PTR(expr->args, arg) {
1402 i++;
1403 expr_set_parent_expr(arg, expr);
1404 type = get_arg_type(expr->fn, i);
1405 tmp = fake_a_variable_assign(type, expr, arg, i);
1406 if (tmp != arg)
1407 __in_fake_var_assign++;
1408 __split_expr(tmp);
1409 if (tmp != arg)
1410 __in_fake_var_assign--;
1411 __process_post_op_stack();
1412 } END_FOR_EACH_PTR(arg);
1415 static void split_sym(struct symbol *sym)
1417 if (!sym)
1418 return;
1419 if (!(sym->namespace & NS_SYMBOL))
1420 return;
1422 __split_stmt(sym->stmt);
1423 __split_expr(sym->array_size);
1424 split_symlist(sym->arguments);
1425 split_symlist(sym->symbol_list);
1426 __split_stmt(sym->inline_stmt);
1427 split_symlist(sym->inline_symbol_list);
1430 static void split_symlist(struct symbol_list *sym_list)
1432 struct symbol *sym;
1434 FOR_EACH_PTR(sym_list, sym) {
1435 split_sym(sym);
1436 } END_FOR_EACH_PTR(sym);
1439 typedef void (fake_cb)(struct expression *expr);
1441 static int member_to_number(struct expression *expr, struct ident *member)
1443 struct symbol *type, *tmp;
1444 char *name;
1445 int i;
1447 if (!member)
1448 return -1;
1449 name = member->name;
1451 type = get_type(expr);
1452 if (!type || type->type != SYM_STRUCT)
1453 return -1;
1455 i = -1;
1456 FOR_EACH_PTR(type->symbol_list, tmp) {
1457 i++;
1458 if (!tmp->ident)
1459 continue;
1460 if (strcmp(name, tmp->ident->name) == 0)
1461 return i;
1462 } END_FOR_EACH_PTR(tmp);
1463 return -1;
1466 static struct ident *number_to_member(struct expression *expr, int num)
1468 struct symbol *type, *member;
1469 int i = 0;
1471 type = get_type(expr);
1472 if (!type || type->type != SYM_STRUCT)
1473 return NULL;
1475 FOR_EACH_PTR(type->symbol_list, member) {
1476 if (i == num)
1477 return member->ident;
1478 i++;
1479 } END_FOR_EACH_PTR(member);
1480 return NULL;
1483 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1485 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1487 struct expression *edge_member, *assign;
1488 struct symbol *base = get_real_base_type(member);
1489 struct symbol *tmp;
1491 if (member->ident)
1492 expr = member_expression(expr, '.', member->ident);
1494 FOR_EACH_PTR(base->symbol_list, tmp) {
1495 struct symbol *type;
1497 type = get_real_base_type(tmp);
1498 if (!type)
1499 continue;
1501 edge_member = member_expression(expr, '.', tmp->ident);
1502 if (get_extra_state(edge_member))
1503 continue;
1505 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1506 set_inner_struct_members(expr, tmp);
1507 continue;
1510 if (!tmp->ident)
1511 continue;
1513 assign = assign_expression(edge_member, '=', zero_expr());
1514 __split_expr(assign);
1515 } END_FOR_EACH_PTR(tmp);
1520 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1522 struct symbol *tmp;
1523 struct expression *member = NULL;
1524 struct expression *assign;
1526 FOR_EACH_PTR(type->symbol_list, tmp) {
1527 type = get_real_base_type(tmp);
1528 if (!type)
1529 continue;
1531 if (tmp->ident) {
1532 member = member_expression(expr, '.', tmp->ident);
1533 if (get_extra_state(member))
1534 continue;
1537 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1538 set_inner_struct_members(expr, tmp);
1539 continue;
1541 if (type->type == SYM_ARRAY)
1542 continue;
1543 if (!tmp->ident)
1544 continue;
1546 assign = assign_expression(member, '=', zero_expr());
1547 __split_expr(assign);
1548 } END_FOR_EACH_PTR(tmp);
1551 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1553 struct expression *deref, *assign, *tmp, *right;
1554 struct symbol *struct_type, *type;
1555 struct ident *member;
1556 int member_idx;
1558 struct_type = get_type(symbol);
1559 if (!struct_type ||
1560 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1561 return;
1564 * We're parsing an initializer that could look something like this:
1565 * struct foo foo = {
1566 * 42,
1567 * .whatever.xxx = 11,
1568 * .zzz = 12,
1569 * };
1571 * So what we have here is a list with 42, .whatever, and .zzz. We need
1572 * to break it up into left and right sides of the assignments.
1575 member_idx = 0;
1576 FOR_EACH_PTR(members, tmp) {
1577 deref = NULL;
1578 if (tmp->type == EXPR_IDENTIFIER) {
1579 member_idx = member_to_number(symbol, tmp->expr_ident);
1580 while (tmp->type == EXPR_IDENTIFIER) {
1581 member = tmp->expr_ident;
1582 tmp = tmp->ident_expression;
1583 if (deref)
1584 deref = member_expression(deref, '.', member);
1585 else
1586 deref = member_expression(symbol, '.', member);
1588 } else {
1589 member = number_to_member(symbol, member_idx);
1590 deref = member_expression(symbol, '.', member);
1592 right = tmp;
1593 member_idx++;
1594 if (right->type == EXPR_INITIALIZER) {
1595 type = get_type(deref);
1596 if (type && type->type == SYM_ARRAY)
1597 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1598 else
1599 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1600 } else {
1601 assign = assign_expression(deref, '=', right);
1602 fake_cb(assign);
1604 } END_FOR_EACH_PTR(tmp);
1606 set_unset_to_zero(struct_type, symbol);
1609 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1611 fake_member_assigns_helper(symbol_expression(sym),
1612 sym->initializer->expr_list, fake_cb);
1615 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1617 struct expression *offset, *binop, *assign, *tmp;
1618 struct symbol *type;
1619 int idx, max;
1621 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1622 return;
1624 max = 0;
1625 idx = 0;
1626 FOR_EACH_PTR(expr_list, tmp) {
1627 if (tmp->type == EXPR_INDEX) {
1628 if (tmp->idx_from != tmp->idx_to)
1629 return;
1630 idx = tmp->idx_from;
1631 if (idx > max)
1632 max = idx;
1633 if (!tmp->idx_expression)
1634 goto next;
1635 tmp = tmp->idx_expression;
1637 offset = value_expr(idx);
1638 binop = array_element_expression(array, offset);
1639 if (tmp->type == EXPR_INITIALIZER) {
1640 type = get_type(binop);
1641 if (type && type->type == SYM_ARRAY)
1642 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1643 else
1644 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1645 } else {
1646 assign = assign_expression(binop, '=', tmp);
1647 fake_cb(assign);
1649 next:
1650 idx++;
1651 if (idx > max)
1652 max = idx;
1653 } END_FOR_EACH_PTR(tmp);
1655 __call_array_initialized_hooks(array, max);
1658 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1660 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1663 static void fake_assign_expr(struct symbol *sym)
1665 struct expression *assign, *symbol;
1667 symbol = symbol_expression(sym);
1668 assign = assign_expression(symbol, '=', sym->initializer);
1669 __split_expr(assign);
1672 static void do_initializer_stuff(struct symbol *sym)
1674 if (!sym->initializer)
1675 return;
1677 if (sym->initializer->type == EXPR_INITIALIZER) {
1678 if (get_real_base_type(sym)->type == SYM_ARRAY)
1679 fake_element_assigns(sym, __split_expr);
1680 else
1681 fake_member_assigns(sym, __split_expr);
1682 } else {
1683 fake_assign_expr(sym);
1687 static void split_declaration(struct symbol_list *sym_list)
1689 struct symbol *sym;
1691 FOR_EACH_PTR(sym_list, sym) {
1692 __pass_to_client(sym, DECLARATION_HOOK);
1693 do_initializer_stuff(sym);
1694 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
1695 split_sym(sym);
1696 } END_FOR_EACH_PTR(sym);
1699 static void call_global_assign_hooks(struct expression *assign)
1701 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1704 static void fake_global_assign(struct symbol *sym)
1706 struct expression *assign, *symbol;
1708 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1709 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1710 fake_element_assigns(sym, call_global_assign_hooks);
1711 } else if (sym->initializer) {
1712 symbol = symbol_expression(sym);
1713 assign = assign_expression(symbol, '=', sym->initializer);
1714 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1715 } else {
1716 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1718 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1719 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1720 fake_member_assigns(sym, call_global_assign_hooks);
1721 } else if (sym->initializer) {
1722 symbol = symbol_expression(sym);
1723 assign = assign_expression(symbol, '=', sym->initializer);
1724 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1725 } else {
1726 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1728 } else {
1729 symbol = symbol_expression(sym);
1730 if (sym->initializer) {
1731 assign = assign_expression(symbol, '=', sym->initializer);
1732 __split_expr(assign);
1733 } else {
1734 assign = assign_expression(symbol, '=', zero_expr());
1736 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1740 static void start_function_definition(struct symbol *sym)
1742 __in_function_def = 1;
1743 __pass_to_client(sym, FUNC_DEF_HOOK);
1744 __in_function_def = 0;
1745 __pass_to_client(sym, AFTER_DEF_HOOK);
1749 void add_function_data(unsigned long *fn_data)
1751 __add_ptr_list(&fn_data_list, fn_data);
1754 static void clear_function_data(void)
1756 unsigned long *tmp;
1758 FOR_EACH_PTR(fn_data_list, tmp) {
1759 *tmp = 0;
1760 } END_FOR_EACH_PTR(tmp);
1763 static void record_func_time(void)
1765 struct timeval stop;
1766 int func_time;
1767 char buf[32];
1769 gettimeofday(&stop, NULL);
1770 func_time = stop.tv_sec - fn_start_time.tv_sec;
1771 snprintf(buf, sizeof(buf), "%d", func_time);
1772 sql_insert_return_implies(FUNC_TIME, 0, "", buf);
1773 if (option_time && func_time > 2) {
1774 final_pass++;
1775 sm_msg("func_time: %d", func_time);
1776 final_pass--;
1780 static void split_function(struct symbol *sym)
1782 struct symbol *base_type = get_base_type(sym);
1784 if (!base_type->stmt && !base_type->inline_stmt)
1785 return;
1787 gettimeofday(&outer_fn_start_time, NULL);
1788 gettimeofday(&fn_start_time, NULL);
1789 cur_func_sym = sym;
1790 if (sym->ident)
1791 cur_func = sym->ident->name;
1792 set_position(sym->pos);
1793 clear_function_data();
1794 loop_count = 0;
1795 last_goto_statement_handled = 0;
1796 sm_debug("new function: %s\n", cur_func);
1797 __stree_id = 0;
1798 if (option_two_passes) {
1799 __unnullify_path();
1800 loop_num = 0;
1801 final_pass = 0;
1802 start_function_definition(sym);
1803 __split_stmt(base_type->stmt);
1804 __split_stmt(base_type->inline_stmt);
1805 nullify_path();
1807 __unnullify_path();
1808 loop_num = 0;
1809 final_pass = 1;
1810 start_function_definition(sym);
1811 __split_stmt(base_type->stmt);
1812 __split_stmt(base_type->inline_stmt);
1813 if (!__path_is_null() &&
1814 cur_func_return_type() == &void_ctype &&
1815 !__bail_on_rest_of_function) {
1816 __pass_to_client(NULL, RETURN_HOOK);
1817 nullify_path();
1819 __pass_to_client(sym, END_FUNC_HOOK);
1820 if (need_delayed_scope_hooks())
1821 __call_scope_hooks();
1822 __pass_to_client(sym, AFTER_FUNC_HOOK);
1823 sym->parsed = true;
1825 clear_all_states();
1827 record_func_time();
1829 cur_func_sym = NULL;
1830 cur_func = NULL;
1831 free_data_info_allocs();
1832 free_expression_stack(&switch_expr_stack);
1833 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1834 __bail_on_rest_of_function = 0;
1837 static void save_flow_state(void)
1839 unsigned long *tmp;
1841 __add_ptr_list(&backup, INT_PTR(loop_num << 2));
1842 __add_ptr_list(&backup, INT_PTR(loop_count << 2));
1843 __add_ptr_list(&backup, INT_PTR(final_pass << 2));
1845 __add_ptr_list(&backup, big_statement_stack);
1846 __add_ptr_list(&backup, big_expression_stack);
1847 __add_ptr_list(&backup, big_condition_stack);
1848 __add_ptr_list(&backup, switch_expr_stack);
1850 __add_ptr_list(&backup, cur_func_sym);
1852 __add_ptr_list(&backup, __prev_stmt);
1853 __add_ptr_list(&backup, __cur_stmt);
1854 __add_ptr_list(&backup, __next_stmt);
1856 FOR_EACH_PTR(fn_data_list, tmp) {
1857 __add_ptr_list(&backup, (void *)*tmp);
1858 } END_FOR_EACH_PTR(tmp);
1861 static void *pop_backup(void)
1863 void *ret;
1865 ret = last_ptr_list(backup);
1866 delete_ptr_list_last(&backup);
1867 return ret;
1870 static void restore_flow_state(void)
1872 unsigned long *tmp;
1874 FOR_EACH_PTR_REVERSE(fn_data_list, tmp) {
1875 *tmp = (unsigned long)pop_backup();
1876 } END_FOR_EACH_PTR_REVERSE(tmp);
1878 __next_stmt = pop_backup();
1879 __cur_stmt = pop_backup();
1880 __prev_stmt = pop_backup();
1882 cur_func_sym = pop_backup();
1883 switch_expr_stack = pop_backup();
1884 big_condition_stack = pop_backup();
1885 big_expression_stack = pop_backup();
1886 big_statement_stack = pop_backup();
1887 final_pass = PTR_INT(pop_backup()) >> 2;
1888 loop_count = PTR_INT(pop_backup()) >> 2;
1889 loop_num = PTR_INT(pop_backup()) >> 2;
1892 static void parse_inline(struct expression *call)
1894 struct symbol *base_type;
1895 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
1896 struct timeval time_backup = fn_start_time;
1897 struct expression *orig_inline = __inline_fn;
1898 int orig_budget;
1900 if (out_of_memory() || taking_too_long())
1901 return;
1903 save_flow_state();
1905 __pass_to_client(call, INLINE_FN_START);
1906 final_pass = 0; /* don't print anything */
1907 __inline_fn = call;
1908 orig_budget = inline_budget;
1909 inline_budget = inline_budget - 5;
1911 base_type = get_base_type(call->fn->symbol);
1912 cur_func_sym = call->fn->symbol;
1913 if (call->fn->symbol->ident)
1914 cur_func = call->fn->symbol->ident->name;
1915 else
1916 cur_func = NULL;
1917 set_position(call->fn->symbol->pos);
1919 save_all_states();
1920 big_statement_stack = NULL;
1921 big_expression_stack = NULL;
1922 big_condition_stack = NULL;
1923 switch_expr_stack = NULL;
1925 sm_debug("inline function: %s\n", cur_func);
1926 __unnullify_path();
1927 clear_function_data();
1928 loop_num = 0;
1929 loop_count = 0;
1930 start_function_definition(call->fn->symbol);
1931 __split_stmt(base_type->stmt);
1932 __split_stmt(base_type->inline_stmt);
1933 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1934 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1935 call->fn->symbol->parsed = true;
1937 free_expression_stack(&switch_expr_stack);
1938 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1939 nullify_path();
1940 free_goto_stack();
1942 restore_flow_state();
1943 fn_start_time = time_backup;
1944 cur_func = cur_func_bak;
1946 restore_all_states();
1947 set_position(call->pos);
1948 __inline_fn = orig_inline;
1949 inline_budget = orig_budget;
1950 __pass_to_client(call, INLINE_FN_END);
1953 static struct symbol_list *inlines_called;
1954 static void add_inline_function(struct symbol *sym)
1956 static struct symbol_list *already_added;
1957 struct symbol *tmp;
1959 FOR_EACH_PTR(already_added, tmp) {
1960 if (tmp == sym)
1961 return;
1962 } END_FOR_EACH_PTR(tmp);
1964 add_ptr_list(&already_added, sym);
1965 add_ptr_list(&inlines_called, sym);
1968 static void process_inlines(void)
1970 struct symbol *tmp;
1972 FOR_EACH_PTR(inlines_called, tmp) {
1973 split_function(tmp);
1974 } END_FOR_EACH_PTR(tmp);
1975 free_ptr_list(&inlines_called);
1978 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1980 struct symbol *sym;
1982 FOR_EACH_PTR_REVERSE(big_list, sym) {
1983 if (!sym->scope)
1984 continue;
1985 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1986 return sym;
1987 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1988 return sym;
1989 } END_FOR_EACH_PTR_REVERSE(sym);
1991 return NULL;
1994 static bool interesting_function(struct symbol *sym)
1996 static int prev_stream = -1;
1997 static bool prev_answer;
1998 const char *filename;
1999 int len;
2001 if (!(sym->ctype.modifiers & MOD_INLINE))
2002 return true;
2004 if (sym->pos.stream == prev_stream)
2005 return prev_answer;
2007 prev_stream = sym->pos.stream;
2008 prev_answer = false;
2010 filename = stream_name(sym->pos.stream);
2011 len = strlen(filename);
2012 if (len > 0 && filename[len - 1] == 'c')
2013 prev_answer = true;
2014 return prev_answer;
2017 static void split_inlines_in_scope(struct symbol *sym)
2019 struct symbol *base;
2020 struct symbol_list *scope_list;
2021 int stream;
2023 scope_list = sym->scope->symbols;
2024 stream = sym->pos.stream;
2026 /* find the last static symbol in the file */
2027 FOR_EACH_PTR_REVERSE(scope_list, sym) {
2028 if (sym->pos.stream != stream)
2029 continue;
2030 if (sym->type != SYM_NODE)
2031 continue;
2032 base = get_base_type(sym);
2033 if (!base)
2034 continue;
2035 if (base->type != SYM_FN)
2036 continue;
2037 if (!base->inline_stmt)
2038 continue;
2039 if (!interesting_function(sym))
2040 continue;
2041 add_inline_function(sym);
2042 } END_FOR_EACH_PTR_REVERSE(sym);
2044 process_inlines();
2047 static void split_inlines(struct symbol_list *sym_list)
2049 struct symbol *sym;
2051 sym = get_last_scoped_symbol(sym_list, 0);
2052 if (sym)
2053 split_inlines_in_scope(sym);
2054 sym = get_last_scoped_symbol(sym_list, 1);
2055 if (sym)
2056 split_inlines_in_scope(sym);
2059 static struct stree *clone_estates_perm(struct stree *orig)
2061 struct stree *ret = NULL;
2062 struct sm_state *tmp;
2064 FOR_EACH_SM(orig, tmp) {
2065 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
2066 } END_FOR_EACH_SM(tmp);
2068 return ret;
2071 struct position last_pos;
2072 static void split_c_file_functions(struct symbol_list *sym_list)
2074 struct symbol *sym;
2076 __unnullify_path();
2077 FOR_EACH_PTR(sym_list, sym) {
2078 set_position(sym->pos);
2079 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
2080 __pass_to_client(sym, BASE_HOOK);
2081 fake_global_assign(sym);
2082 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
2084 } END_FOR_EACH_PTR(sym);
2085 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
2086 nullify_path();
2088 FOR_EACH_PTR(sym_list, sym) {
2089 set_position(sym->pos);
2090 last_pos = sym->pos;
2091 if (!interesting_function(sym))
2092 continue;
2093 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
2094 split_function(sym);
2095 process_inlines();
2097 last_pos = sym->pos;
2098 } END_FOR_EACH_PTR(sym);
2099 split_inlines(sym_list);
2100 __pass_to_client(sym_list, END_FILE_HOOK);
2103 static int final_before_fake;
2104 void init_fake_env(void)
2106 if (!in_fake_env)
2107 final_before_fake = final_pass;
2108 in_fake_env++;
2109 __push_fake_cur_stree();
2110 final_pass = 0;
2113 void end_fake_env(void)
2115 __pop_fake_cur_stree();
2116 in_fake_env--;
2117 if (!in_fake_env)
2118 final_pass = final_before_fake;
2121 static void open_output_files(char *base_file)
2123 char buf[256];
2125 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
2126 sm_outfd = fopen(buf, "w");
2127 if (!sm_outfd)
2128 sm_fatal("Cannot open %s", buf);
2130 if (!option_info)
2131 return;
2133 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
2134 sql_outfd = fopen(buf, "w");
2135 if (!sql_outfd)
2136 sm_fatal("Error: Cannot open %s", buf);
2138 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
2139 caller_info_fd = fopen(buf, "w");
2140 if (!caller_info_fd)
2141 sm_fatal("Error: Cannot open %s", buf);
2144 void smatch(struct string_list *filelist)
2146 struct symbol_list *sym_list;
2147 struct timeval stop, start;
2148 char *path;
2149 int len;
2151 gettimeofday(&start, NULL);
2153 FOR_EACH_PTR_NOTAG(filelist, base_file) {
2154 path = getcwd(NULL, 0);
2155 free(full_base_file);
2156 if (path) {
2157 len = strlen(path) + 1 + strlen(base_file) + 1;
2158 full_base_file = malloc(len);
2159 snprintf(full_base_file, len, "%s/%s", path, base_file);
2160 } else {
2161 full_base_file = alloc_string(base_file);
2163 if (option_file_output)
2164 open_output_files(base_file);
2165 sym_list = sparse_keep_tokens(base_file);
2166 split_c_file_functions(sym_list);
2167 } END_FOR_EACH_PTR_NOTAG(base_file);
2169 gettimeofday(&stop, NULL);
2171 set_position(last_pos);
2172 final_pass = 1;
2173 if (option_time)
2174 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
2175 if (option_mem)
2176 sm_msg("mem: %luKb", get_max_memory());