comparison: select the caller_info
[smatch.git] / smatch_flow.c
blob4f3caece49a03cb2fb9d4c25c0ff59fb1002a0a6
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 int base_file_stream;
46 static unsigned int loop_count;
47 static int last_goto_statement_handled;
48 int __expr_stmt_count;
49 int __in_function_def;
50 int __in_unmatched_hook;
51 static struct expression_list *switch_expr_stack = NULL;
52 static struct expression_list *post_op_stack = NULL;
54 static struct ptr_list *fn_data_list;
55 static struct ptr_list *backup;
57 struct expression_list *big_expression_stack;
58 struct statement_list *big_statement_stack;
59 struct statement *__prev_stmt;
60 struct statement *__cur_stmt;
61 struct statement *__next_stmt;
62 int __in_pre_condition = 0;
63 int __bail_on_rest_of_function = 0;
64 static struct timeval fn_start_time;
65 static struct timeval outer_fn_start_time;
66 char *get_function(void) { return cur_func; }
67 int get_lineno(void) { return __smatch_lineno; }
68 int inside_loop(void) { return !!loop_count; }
69 int definitely_inside_loop(void) { return !!(loop_count & ~0x08000000); }
70 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
71 int in_expression_statement(void) { return !!__expr_stmt_count; }
73 static void split_symlist(struct symbol_list *sym_list);
74 static void split_declaration(struct symbol_list *sym_list);
75 static void split_expr_list(struct expression_list *expr_list, struct expression *parent);
76 static void split_args(struct expression *expr);
77 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *call, struct expression *expr, int nr);
78 static void add_inline_function(struct symbol *sym);
79 static void parse_inline(struct expression *expr);
81 int option_assume_loops = 0;
82 int option_two_passes = 0;
83 struct symbol *cur_func_sym = NULL;
84 struct stree *global_states;
86 const unsigned long valid_ptr_min = 4096;
87 unsigned long valid_ptr_max = ULONG_MAX & ~(MTAG_OFFSET_MASK);
88 const sval_t valid_ptr_min_sval = {
89 .type = &ptr_ctype,
90 {.value = 4096},
92 sval_t valid_ptr_max_sval = {
93 .type = &ptr_ctype,
94 {.value = ULONG_MAX & ~(MTAG_OFFSET_MASK)},
96 struct range_list *valid_ptr_rl;
98 void alloc_valid_ptr_rl(void)
100 valid_ptr_max = sval_type_max(&ulong_ctype).value & ~(MTAG_OFFSET_MASK);
101 valid_ptr_max_sval.value = valid_ptr_max;
103 valid_ptr_rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
104 valid_ptr_rl = cast_rl(&ptr_ctype, valid_ptr_rl);
105 valid_ptr_rl = clone_rl_permanent(valid_ptr_rl);
108 int outside_of_function(void)
110 return cur_func_sym == NULL;
113 const char *get_filename(void)
115 if (option_info && option_full_path)
116 return full_base_file;
117 if (option_info)
118 return base_file;
119 if (option_full_path)
120 return full_filename;
121 return filename;
124 const char *get_base_file(void)
126 if (option_full_path)
127 return full_base_file;
128 return base_file;
131 static void set_position(struct position pos)
133 int len;
134 static int prev_stream = -1;
136 if (in_fake_env)
137 return;
139 if (pos.stream == 0 && pos.line == 0)
140 return;
142 __smatch_lineno = pos.line;
144 if (pos.stream == prev_stream)
145 return;
147 filename = stream_name(pos.stream);
149 free(full_filename);
150 pathname = getcwd(NULL, 0);
151 if (pathname) {
152 len = strlen(pathname) + 1 + strlen(filename) + 1;
153 full_filename = malloc(len);
154 snprintf(full_filename, len, "%s/%s", pathname, filename);
155 } else {
156 full_filename = alloc_string(filename);
158 free(pathname);
161 int is_assigned_call(struct expression *expr)
163 struct expression *parent = expr_get_parent_expr(expr);
165 if (parent &&
166 parent->type == EXPR_ASSIGNMENT &&
167 parent->op == '=' &&
168 strip_expr(parent->right) == expr)
169 return 1;
171 return 0;
174 int is_fake_assigned_call(struct expression *expr)
176 struct expression *parent = expr_get_fake_parent_expr(expr);
178 if (parent &&
179 parent->type == EXPR_ASSIGNMENT &&
180 parent->op == '=' &&
181 strip_expr(parent->right) == expr)
182 return 1;
184 return 0;
187 static bool is_inline_func(struct expression *expr)
189 if (expr->type != EXPR_SYMBOL || !expr->symbol)
190 return false;
191 if (!expr->symbol->definition)
192 return false;
193 if (expr->symbol->definition->ctype.modifiers & MOD_INLINE)
194 return true;
196 return 0;
199 static int is_noreturn_func(struct expression *expr)
201 if (expr->type != EXPR_SYMBOL || !expr->symbol)
202 return 0;
203 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
204 return 1;
205 return 0;
208 static int save_func_time(void *_rl, int argc, char **argv, char **azColName)
210 unsigned long *rl = _rl;
212 *rl = strtoul(argv[0], NULL, 10);
213 return 0;
216 static int get_func_time(struct symbol *sym)
218 unsigned long time = 0;
220 run_sql(&save_func_time, &time,
221 "select key from return_implies where %s and type = %d;",
222 get_static_filter(sym), FUNC_TIME);
224 return time;
227 static int inline_budget = 20;
229 int inlinable(struct expression *expr)
231 struct symbol *sym;
232 struct statement *last_stmt = NULL;
234 if (__inline_fn) /* don't nest */
235 return 0;
237 if (expr->type != EXPR_SYMBOL || !expr->symbol)
238 return 0;
239 if (is_no_inline_function(expr->symbol->ident->name))
240 return 0;
241 sym = get_base_type(expr->symbol);
242 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
243 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
244 return 0;
245 if (sym->stmt->type != STMT_COMPOUND)
246 return 0;
247 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
249 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
250 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
251 return 0;
252 if (sym->inline_stmt->type != STMT_COMPOUND)
253 return 0;
254 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
257 if (!last_stmt)
258 return 0;
260 /* the magic numbers in this function are pulled out of my bum. */
261 if (last_stmt->pos.line > sym->pos.line + inline_budget)
262 return 0;
264 if (get_func_time(expr->symbol) >= 2)
265 return 0;
267 return 1;
270 void __process_post_op_stack(void)
272 struct expression *expr;
274 FOR_EACH_PTR(post_op_stack, expr) {
275 __pass_to_client(expr, OP_HOOK);
276 } END_FOR_EACH_PTR(expr);
278 __free_ptr_list((struct ptr_list **)&post_op_stack);
281 static int handle_comma_assigns(struct expression *expr)
283 struct expression *right;
284 struct expression *assign;
286 right = strip_expr(expr->right);
287 if (right->type != EXPR_COMMA)
288 return 0;
290 __split_expr(right->left);
291 __process_post_op_stack();
293 assign = assign_expression(expr->left, '=', right->right);
294 __split_expr(assign);
296 return 1;
299 /* This is to handle *p++ = foo; assignments */
300 static int handle_postop_assigns(struct expression *expr)
302 struct expression *left, *fake_left;
303 struct expression *assign;
305 left = strip_expr(expr->left);
306 if (left->type != EXPR_PREOP || left->op != '*')
307 return 0;
308 left = strip_expr(left->unop);
309 if (left->type != EXPR_POSTOP)
310 return 0;
312 fake_left = deref_expression(strip_expr(left->unop));
313 assign = assign_expression(fake_left, '=', expr->right);
315 __split_expr(assign);
316 __split_expr(expr->left);
318 return 1;
321 static int prev_expression_is_getting_address(struct expression *expr)
323 struct expression *parent;
325 do {
326 parent = expr_get_parent_expr(expr);
328 if (!parent)
329 return 0;
330 if (parent->type == EXPR_PREOP && parent->op == '&')
331 return 1;
332 if (parent->type == EXPR_PREOP && parent->op == '(')
333 goto next;
334 if (parent->type == EXPR_DEREF && parent->op == '.')
335 goto next;
337 return 0;
338 next:
339 expr = parent;
340 } while (1);
343 static void handle_builtin_overflow_func(struct expression *expr)
345 struct expression *a, *b, *res, *assign;
346 int op;
348 if (sym_name_is("__builtin_add_overflow", expr->fn))
349 op = '+';
350 else if (sym_name_is("__builtin_sub_overflow", expr->fn))
351 op = '-';
352 else if (sym_name_is("__builtin_mul_overflow", expr->fn))
353 op = '*';
354 else
355 return;
357 a = get_argument_from_call_expr(expr->args, 0);
358 b = get_argument_from_call_expr(expr->args, 1);
359 res = get_argument_from_call_expr(expr->args, 2);
361 assign = assign_expression(deref_expression(res), '=', binop_expression(a, op, b));
362 __split_expr(assign);
365 static int handle__builtin_choose_expr(struct expression *expr)
367 struct expression *const_expr, *expr1, *expr2;
368 sval_t sval;
370 if (!sym_name_is("__builtin_choose_expr", expr->fn))
371 return 0;
373 const_expr = get_argument_from_call_expr(expr->args, 0);
374 expr1 = get_argument_from_call_expr(expr->args, 1);
375 expr2 = get_argument_from_call_expr(expr->args, 2);
377 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
378 return 0;
379 if (sval.value)
380 __split_expr(expr1);
381 else
382 __split_expr(expr2);
383 return 1;
386 static int handle__builtin_choose_expr_assigns(struct expression *expr)
388 struct expression *const_expr, *right, *expr1, *expr2, *fake;
389 sval_t sval;
391 right = strip_expr(expr->right);
392 if (right->type != EXPR_CALL)
393 return 0;
394 if (!sym_name_is("__builtin_choose_expr", right->fn))
395 return 0;
397 const_expr = get_argument_from_call_expr(right->args, 0);
398 expr1 = get_argument_from_call_expr(right->args, 1);
399 expr2 = get_argument_from_call_expr(right->args, 2);
401 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
402 return 0;
404 fake = assign_expression(expr->left, '=', sval.value ? expr1 : expr2);
405 __split_expr(fake);
406 return 1;
409 void __split_expr(struct expression *expr)
411 if (!expr)
412 return;
414 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
416 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
417 return;
418 if (__in_fake_assign >= 4) /* don't allow too much nesting */
419 return;
421 push_expression(&big_expression_stack, expr);
422 set_position(expr->pos);
423 __pass_to_client(expr, EXPR_HOOK);
425 switch (expr->type) {
426 case EXPR_PREOP:
427 expr_set_parent_expr(expr->unop, expr);
429 if (expr->op == '*' &&
430 !prev_expression_is_getting_address(expr))
431 __pass_to_client(expr, DEREF_HOOK);
432 __split_expr(expr->unop);
433 __pass_to_client(expr, OP_HOOK);
434 break;
435 case EXPR_POSTOP:
436 expr_set_parent_expr(expr->unop, expr);
438 __split_expr(expr->unop);
439 push_expression(&post_op_stack, expr);
440 break;
441 case EXPR_STATEMENT:
442 __expr_stmt_count++;
443 if (expr->statement && !expr->statement) {
444 stmt_set_parent_stmt(expr->statement,
445 last_ptr_list((struct ptr_list *)big_statement_stack));
447 __split_stmt(expr->statement);
448 __expr_stmt_count--;
449 break;
450 case EXPR_LOGICAL:
451 case EXPR_COMPARE:
452 expr_set_parent_expr(expr->left, expr);
453 expr_set_parent_expr(expr->right, expr);
455 __pass_to_client(expr, LOGIC_HOOK);
456 __handle_logic(expr);
457 break;
458 case EXPR_BINOP:
459 expr_set_parent_expr(expr->left, expr);
460 expr_set_parent_expr(expr->right, expr);
462 __pass_to_client(expr, BINOP_HOOK);
463 case EXPR_COMMA:
464 expr_set_parent_expr(expr->left, expr);
465 expr_set_parent_expr(expr->right, expr);
467 __split_expr(expr->left);
468 __process_post_op_stack();
469 __split_expr(expr->right);
470 break;
471 case EXPR_ASSIGNMENT: {
472 struct expression *right;
474 expr_set_parent_expr(expr->left, expr);
475 expr_set_parent_expr(expr->right, expr);
477 right = strip_expr(expr->right);
478 if (!right)
479 break;
481 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
483 /* foo = !bar() */
484 if (__handle_condition_assigns(expr))
485 goto after_assign;
486 /* foo = (x < 5 ? foo : 5); */
487 if (__handle_select_assigns(expr))
488 goto after_assign;
489 /* foo = ({frob(); frob(); frob(); 1;}) */
490 if (__handle_expr_statement_assigns(expr))
491 break; // FIXME: got after
492 /* foo = (3, 4); */
493 if (handle_comma_assigns(expr))
494 goto after_assign;
495 if (handle__builtin_choose_expr_assigns(expr))
496 goto after_assign;
497 if (handle_postop_assigns(expr))
498 break; /* no need to goto after_assign */
500 __split_expr(expr->right);
501 if (outside_of_function())
502 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
503 else
504 __pass_to_client(expr, ASSIGNMENT_HOOK);
506 __fake_struct_member_assignments(expr);
508 /* Re-examine ->right for inlines. See the commit message */
509 right = strip_expr(expr->right);
510 if (expr->op == '=' && right->type == EXPR_CALL)
511 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
513 after_assign:
514 if (get_macro_name(right->pos) &&
515 get_macro_name(expr->pos) != get_macro_name(right->pos))
516 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
518 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
519 __split_expr(expr->left);
520 break;
522 case EXPR_DEREF:
523 expr_set_parent_expr(expr->deref, expr);
525 __pass_to_client(expr, DEREF_HOOK);
526 __split_expr(expr->deref);
527 break;
528 case EXPR_SLICE:
529 expr_set_parent_expr(expr->base, expr);
531 __split_expr(expr->base);
532 break;
533 case EXPR_CAST:
534 case EXPR_FORCE_CAST:
535 expr_set_parent_expr(expr->cast_expression, expr);
537 __pass_to_client(expr, CAST_HOOK);
538 __split_expr(expr->cast_expression);
539 break;
540 case EXPR_SIZEOF:
541 if (expr->cast_expression)
542 __pass_to_client(strip_parens(expr->cast_expression),
543 SIZEOF_HOOK);
544 break;
545 case EXPR_OFFSETOF:
546 case EXPR_ALIGNOF:
547 break;
548 case EXPR_CONDITIONAL:
549 case EXPR_SELECT:
550 expr_set_parent_expr(expr->conditional, expr);
551 expr_set_parent_expr(expr->cond_true, expr);
552 expr_set_parent_expr(expr->cond_false, expr);
554 if (known_condition_true(expr->conditional)) {
555 __split_expr(expr->cond_true);
556 break;
558 if (known_condition_false(expr->conditional)) {
559 __split_expr(expr->cond_false);
560 break;
562 __pass_to_client(expr, SELECT_HOOK);
563 __split_whole_condition(expr->conditional);
564 __split_expr(expr->cond_true);
565 __push_true_states();
566 __use_false_states();
567 __split_expr(expr->cond_false);
568 __merge_true_states();
569 break;
570 case EXPR_CALL:
571 expr_set_parent_expr(expr->fn, expr);
573 if (sym_name_is("__builtin_constant_p", expr->fn))
574 break;
575 if (handle__builtin_choose_expr(expr))
576 break;
577 __split_expr(expr->fn);
578 split_args(expr);
579 if (is_inline_func(expr->fn))
580 add_inline_function(expr->fn->symbol->definition);
581 if (inlinable(expr->fn))
582 __inline_call = 1;
583 __process_post_op_stack();
584 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
585 __pass_to_client(expr, FUNCTION_CALL_HOOK);
586 __inline_call = 0;
587 if (inlinable(expr->fn))
588 parse_inline(expr);
589 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
590 if (is_noreturn_func(expr->fn))
591 nullify_path();
592 if (!expr_get_parent_expr(expr))
593 __discard_fake_states(expr);
594 handle_builtin_overflow_func(expr);
595 break;
596 case EXPR_INITIALIZER:
597 split_expr_list(expr->expr_list, expr);
598 break;
599 case EXPR_IDENTIFIER:
600 expr_set_parent_expr(expr->ident_expression, expr);
601 __split_expr(expr->ident_expression);
602 break;
603 case EXPR_INDEX:
604 expr_set_parent_expr(expr->idx_expression, expr);
605 __split_expr(expr->idx_expression);
606 break;
607 case EXPR_POS:
608 expr_set_parent_expr(expr->init_expr, expr);
609 __split_expr(expr->init_expr);
610 break;
611 case EXPR_SYMBOL:
612 __pass_to_client(expr, SYM_HOOK);
613 break;
614 case EXPR_STRING:
615 __pass_to_client(expr, STRING_HOOK);
616 break;
617 default:
618 break;
620 __pass_to_client(expr, EXPR_HOOK_AFTER);
621 pop_expression(&big_expression_stack);
624 static int is_forever_loop(struct statement *stmt)
626 struct expression *expr;
627 sval_t sval;
629 expr = strip_expr(stmt->iterator_pre_condition);
630 if (!expr)
631 expr = stmt->iterator_post_condition;
632 if (!expr) {
633 /* this is a for(;;) loop... */
634 return 1;
637 if (get_value(expr, &sval) && sval.value != 0)
638 return 1;
640 return 0;
643 static int loop_num;
644 static char *get_loop_name(int num)
646 char buf[256];
648 snprintf(buf, 255, "-loop%d", num);
649 buf[255] = '\0';
650 return alloc_sname(buf);
654 * Pre Loops are while and for loops.
656 static void handle_pre_loop(struct statement *stmt)
658 int once_through; /* we go through the loop at least once */
659 struct sm_state *extra_sm = NULL;
660 int unchanged = 0;
661 char *loop_name;
662 struct stree *stree = NULL;
663 struct sm_state *sm = NULL;
665 loop_name = get_loop_name(loop_num);
666 loop_num++;
668 if (stmt->iterator_pre_statement) {
669 __split_stmt(stmt->iterator_pre_statement);
670 __prev_stmt = stmt->iterator_pre_statement;
673 once_through = implied_condition_true(stmt->iterator_pre_condition);
675 loop_count++;
676 __push_continues();
677 __push_breaks();
679 __merge_gotos(loop_name, NULL);
681 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
682 __in_pre_condition++;
683 __pass_to_client(stmt, PRELOOP_HOOK);
684 __split_whole_condition(stmt->iterator_pre_condition);
685 __in_pre_condition--;
686 FOR_EACH_SM(stree, sm) {
687 set_state(sm->owner, sm->name, sm->sym, sm->state);
688 } END_FOR_EACH_SM(sm);
689 free_stree(&stree);
690 if (extra_sm)
691 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
693 if (option_assume_loops)
694 once_through = 1;
696 __split_stmt(stmt->iterator_statement);
697 if (is_forever_loop(stmt)) {
698 __merge_continues();
699 __save_gotos(loop_name, NULL);
701 __push_fake_cur_stree();
702 __split_stmt(stmt->iterator_post_statement);
703 stree = __pop_fake_cur_stree();
705 __discard_false_states();
706 __use_breaks();
708 if (!__path_is_null())
709 __merge_stree_into_cur(stree);
710 free_stree(&stree);
711 } else {
712 __merge_continues();
713 unchanged = __iterator_unchanged(extra_sm);
714 __split_stmt(stmt->iterator_post_statement);
715 __prev_stmt = stmt->iterator_post_statement;
716 __cur_stmt = stmt;
718 __save_gotos(loop_name, NULL);
719 __in_pre_condition++;
720 __split_whole_condition(stmt->iterator_pre_condition);
721 __in_pre_condition--;
722 nullify_path();
723 __merge_false_states();
724 if (once_through)
725 __discard_false_states();
726 else
727 __merge_false_states();
729 if (extra_sm && unchanged)
730 __extra_pre_loop_hook_after(extra_sm,
731 stmt->iterator_post_statement,
732 stmt->iterator_pre_condition);
733 __merge_breaks();
735 loop_count--;
739 * Post loops are do {} while();
741 static void handle_post_loop(struct statement *stmt)
743 char *loop_name;
745 loop_name = get_loop_name(loop_num);
746 loop_num++;
747 loop_count++;
749 __push_continues();
750 __push_breaks();
751 __merge_gotos(loop_name, NULL);
752 __split_stmt(stmt->iterator_statement);
753 __merge_continues();
754 if (!expr_is_zero(stmt->iterator_post_condition))
755 __save_gotos(loop_name, NULL);
757 if (is_forever_loop(stmt)) {
758 __use_breaks();
759 } else {
760 __split_whole_condition(stmt->iterator_post_condition);
761 __use_false_states();
762 __merge_breaks();
764 loop_count--;
767 static int empty_statement(struct statement *stmt)
769 if (!stmt)
770 return 0;
771 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
772 return 1;
773 return 0;
776 static int last_stmt_on_same_line(void)
778 struct statement *stmt;
779 int i = 0;
781 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
782 if (!i++)
783 continue;
784 if (stmt->pos.line == get_lineno())
785 return 1;
786 return 0;
787 } END_FOR_EACH_PTR_REVERSE(stmt);
788 return 0;
791 static void split_asm_ops(struct asm_operand_list *ops)
793 struct asm_operand *op;
795 FOR_EACH_PTR(ops, op) {
796 __split_expr(op->expr);
797 } END_FOR_EACH_PTR(op);
800 static int is_case_val(struct statement *stmt, sval_t sval)
802 sval_t case_sval;
804 if (stmt->type != STMT_CASE)
805 return 0;
806 if (!stmt->case_expression) {
807 __set_default();
808 return 1;
810 if (!get_value(stmt->case_expression, &case_sval))
811 return 0;
812 if (case_sval.value == sval.value)
813 return 1;
814 return 0;
817 static struct range_list *get_case_rl(struct expression *switch_expr,
818 struct expression *case_expr,
819 struct expression *case_to)
821 sval_t start, end;
822 struct range_list *rl = NULL;
823 struct symbol *switch_type;
825 switch_type = get_type(switch_expr);
826 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
827 start = sval_cast(switch_type, start);
828 end = sval_cast(switch_type, end);
829 add_range(&rl, start, end);
830 } else if (get_value(case_expr, &start)) {
831 start = sval_cast(switch_type, start);
832 add_range(&rl, start, start);
835 return rl;
838 static void split_known_switch(struct statement *stmt, sval_t sval)
840 struct statement *tmp;
841 struct range_list *rl;
843 __split_expr(stmt->switch_expression);
844 sval = sval_cast(get_type(stmt->switch_expression), sval);
846 push_expression(&switch_expr_stack, stmt->switch_expression);
847 __save_switch_states(top_expression(switch_expr_stack));
848 nullify_path();
849 __push_default();
850 __push_breaks();
852 stmt = stmt->switch_statement;
854 __push_scope_hooks();
855 FOR_EACH_PTR(stmt->stmts, tmp) {
856 __smatch_lineno = tmp->pos.line;
857 if (is_case_val(tmp, sval)) {
858 rl = alloc_rl(sval, sval);
859 __merge_switches(top_expression(switch_expr_stack), rl);
860 __pass_case_to_client(top_expression(switch_expr_stack), rl);
862 if (__path_is_null())
863 continue;
864 __split_stmt(tmp);
865 if (__path_is_null()) {
866 __set_default();
867 goto out;
869 } END_FOR_EACH_PTR(tmp);
870 out:
871 __call_scope_hooks();
872 if (!__pop_default())
873 __merge_switches(top_expression(switch_expr_stack), NULL);
874 __discard_switches();
875 __merge_breaks();
876 pop_expression(&switch_expr_stack);
879 static void split_case(struct statement *stmt)
881 struct range_list *rl = NULL;
883 expr_set_parent_stmt(stmt->case_expression, stmt);
884 expr_set_parent_stmt(stmt->case_to, stmt);
886 rl = get_case_rl(top_expression(switch_expr_stack),
887 stmt->case_expression, stmt->case_to);
888 while (stmt->case_statement->type == STMT_CASE) {
889 struct range_list *tmp;
891 tmp = get_case_rl(top_expression(switch_expr_stack),
892 stmt->case_statement->case_expression,
893 stmt->case_statement->case_to);
894 if (!tmp)
895 break;
896 rl = rl_union(rl, tmp);
897 if (!stmt->case_expression)
898 __set_default();
899 stmt = stmt->case_statement;
902 __merge_switches(top_expression(switch_expr_stack), rl);
904 if (!stmt->case_expression)
905 __set_default();
907 stmt_set_parent_stmt(stmt->case_statement, stmt);
908 __split_stmt(stmt->case_statement);
911 int time_parsing_function(void)
913 return ms_since(&fn_start_time) / 1000;
916 bool taking_too_long(void)
918 if ((ms_since(&outer_fn_start_time) / 1000) > 60 * 5) /* five minutes */
919 return 1;
920 return 0;
923 struct statement *get_last_stmt(void)
925 struct symbol *fn;
926 struct statement *stmt;
928 fn = get_base_type(cur_func_sym);
929 if (!fn)
930 return NULL;
931 stmt = fn->stmt;
932 if (!stmt)
933 stmt = fn->inline_stmt;
934 if (!stmt || stmt->type != STMT_COMPOUND)
935 return NULL;
936 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
937 if (stmt && stmt->type == STMT_LABEL)
938 stmt = stmt->label_statement;
939 return stmt;
942 int is_last_stmt(struct statement *cur_stmt)
944 struct statement *last;
946 last = get_last_stmt();
947 if (last && last == cur_stmt)
948 return 1;
949 return 0;
952 static void handle_backward_goto(struct statement *goto_stmt)
954 const char *goto_name, *label_name;
955 struct statement *func_stmt;
956 struct symbol *base_type = get_base_type(cur_func_sym);
957 struct statement *tmp;
958 int found = 0;
960 if (!option_info)
961 return;
962 if (last_goto_statement_handled)
963 return;
964 last_goto_statement_handled = 1;
966 if (!goto_stmt->goto_label ||
967 goto_stmt->goto_label->type != SYM_LABEL ||
968 !goto_stmt->goto_label->ident)
969 return;
970 goto_name = goto_stmt->goto_label->ident->name;
972 func_stmt = base_type->stmt;
973 if (!func_stmt)
974 func_stmt = base_type->inline_stmt;
975 if (!func_stmt)
976 return;
977 if (func_stmt->type != STMT_COMPOUND)
978 return;
980 FOR_EACH_PTR(func_stmt->stmts, tmp) {
981 if (!found) {
982 if (tmp->type != STMT_LABEL)
983 continue;
984 if (!tmp->label_identifier ||
985 tmp->label_identifier->type != SYM_LABEL ||
986 !tmp->label_identifier->ident)
987 continue;
988 label_name = tmp->label_identifier->ident->name;
989 if (strcmp(goto_name, label_name) != 0)
990 continue;
991 found = 1;
993 __split_stmt(tmp);
994 } END_FOR_EACH_PTR(tmp);
997 static void fake_a_return(void)
999 struct expression *ret = NULL;
1001 nullify_path();
1002 __unnullify_path();
1004 if (cur_func_return_type() != &void_ctype)
1005 ret = unknown_value_expression(NULL);
1007 __pass_to_client(ret, RETURN_HOOK);
1008 nullify_path();
1011 static void split_ret_value(struct expression *expr)
1013 struct symbol *type;
1015 if (!expr)
1016 return;
1018 type = get_real_base_type(cur_func_sym);
1019 type = get_real_base_type(type);
1020 expr = fake_a_variable_assign(type, NULL, expr, -1);
1022 __in_fake_var_assign++;
1023 __split_expr(expr);
1024 __in_fake_var_assign--;
1027 static void fake_an_empty_default(struct position pos)
1029 static struct statement none = {};
1031 none.pos = pos;
1032 none.type = STMT_NONE;
1033 __merge_switches(top_expression(switch_expr_stack), NULL);
1034 __split_stmt(&none);
1037 static void split_compound(struct statement *stmt)
1039 struct statement *prev = NULL;
1040 struct statement *cur = NULL;
1041 struct statement *next;
1043 __push_scope_hooks();
1045 FOR_EACH_PTR(stmt->stmts, next) {
1046 /* just set them all ahead of time */
1047 stmt_set_parent_stmt(next, stmt);
1049 if (cur) {
1050 __prev_stmt = prev;
1051 __next_stmt = next;
1052 __cur_stmt = cur;
1053 __split_stmt(cur);
1055 prev = cur;
1056 cur = next;
1057 } END_FOR_EACH_PTR(next);
1058 if (cur) {
1059 __prev_stmt = prev;
1060 __cur_stmt = cur;
1061 __next_stmt = NULL;
1062 __split_stmt(cur);
1066 * For function scope, then delay calling the scope hooks until the
1067 * end of function hooks can run. I'm not positive this is the right
1068 * thing...
1070 if (!is_last_stmt(cur))
1071 __call_scope_hooks();
1075 * This is a hack, work around for detecting empty functions.
1077 static int need_delayed_scope_hooks(void)
1079 struct symbol *fn = get_base_type(cur_func_sym);
1080 struct statement *stmt;
1082 if (!fn)
1083 return 0;
1084 stmt = fn->stmt;
1085 if (!stmt)
1086 stmt = fn->inline_stmt;
1087 if (stmt && stmt->type == STMT_COMPOUND)
1088 return 1;
1089 return 0;
1092 void __split_label_stmt(struct statement *stmt)
1094 if (stmt->label_identifier &&
1095 stmt->label_identifier->type == SYM_LABEL &&
1096 stmt->label_identifier->ident) {
1097 loop_count |= 0x0800000;
1098 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
1102 static void find_asm_gotos(struct statement *stmt)
1104 struct symbol *sym;
1106 FOR_EACH_PTR(stmt->asm_labels, sym) {
1107 __save_gotos(sym->ident->name, sym);
1108 } END_FOR_EACH_PTR(sym);
1111 void __split_stmt(struct statement *stmt)
1113 static int indent_cnt;
1114 sval_t sval;
1115 struct timeval start, stop;
1117 gettimeofday(&start, NULL);
1119 if (!stmt)
1120 goto out;
1122 if (!__in_fake_assign)
1123 __silence_warnings_for_stmt = false;
1125 if (__bail_on_rest_of_function || is_skipped_function())
1126 return;
1128 if (out_of_memory() || taking_too_long()) {
1129 gettimeofday(&start, NULL);
1131 __bail_on_rest_of_function = 1;
1132 final_pass = 1;
1133 sm_perror("Function too hairy. Giving up. %lu seconds",
1134 start.tv_sec - fn_start_time.tv_sec);
1135 fake_a_return();
1136 final_pass = 0; /* turn off sm_msg() from here */
1137 return;
1140 indent_cnt++;
1142 add_ptr_list(&big_statement_stack, stmt);
1143 free_expression_stack(&big_expression_stack);
1144 set_position(stmt->pos);
1145 __pass_to_client(stmt, STMT_HOOK);
1147 switch (stmt->type) {
1148 case STMT_DECLARATION:
1149 split_declaration(stmt->declaration);
1150 break;
1151 case STMT_RETURN:
1152 expr_set_parent_stmt(stmt->ret_value, stmt);
1154 split_ret_value(stmt->ret_value);
1155 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1156 __process_post_op_stack();
1157 nullify_path();
1158 break;
1159 case STMT_EXPRESSION:
1160 expr_set_parent_stmt(stmt->expression, stmt);
1161 expr_set_parent_stmt(stmt->context, stmt);
1163 __split_expr(stmt->expression);
1164 break;
1165 case STMT_COMPOUND:
1166 split_compound(stmt);
1167 break;
1168 case STMT_IF:
1169 stmt_set_parent_stmt(stmt->if_true, stmt);
1170 stmt_set_parent_stmt(stmt->if_false, stmt);
1171 expr_set_parent_stmt(stmt->if_conditional, stmt);
1173 if (known_condition_true(stmt->if_conditional)) {
1174 __split_stmt(stmt->if_true);
1175 break;
1177 if (known_condition_false(stmt->if_conditional)) {
1178 __split_stmt(stmt->if_false);
1179 break;
1181 __split_whole_condition(stmt->if_conditional);
1182 __split_stmt(stmt->if_true);
1183 if (empty_statement(stmt->if_true) &&
1184 last_stmt_on_same_line() &&
1185 !get_macro_name(stmt->if_true->pos))
1186 sm_warning("if();");
1187 __push_true_states();
1188 __use_false_states();
1189 __split_stmt(stmt->if_false);
1190 __merge_true_states();
1191 break;
1192 case STMT_ITERATOR:
1193 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1194 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1195 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1196 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1197 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1199 if (stmt->iterator_pre_condition)
1200 handle_pre_loop(stmt);
1201 else if (stmt->iterator_post_condition)
1202 handle_post_loop(stmt);
1203 else {
1204 // these are for(;;) type loops.
1205 handle_pre_loop(stmt);
1207 break;
1208 case STMT_SWITCH:
1209 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1210 expr_set_parent_stmt(stmt->switch_expression, stmt);
1212 if (get_value(stmt->switch_expression, &sval)) {
1213 split_known_switch(stmt, sval);
1214 break;
1216 __split_expr(stmt->switch_expression);
1217 push_expression(&switch_expr_stack, stmt->switch_expression);
1218 __save_switch_states(top_expression(switch_expr_stack));
1219 nullify_path();
1220 __push_default();
1221 __push_breaks();
1222 __split_stmt(stmt->switch_statement);
1223 if (!__pop_default() && have_remaining_cases())
1224 fake_an_empty_default(stmt->pos);
1225 __discard_switches();
1226 __merge_breaks();
1227 pop_expression(&switch_expr_stack);
1228 break;
1229 case STMT_CASE:
1230 split_case(stmt);
1231 break;
1232 case STMT_LABEL:
1233 __split_label_stmt(stmt);
1234 __split_stmt(stmt->label_statement);
1235 break;
1236 case STMT_GOTO:
1237 expr_set_parent_stmt(stmt->goto_expression, stmt);
1239 __split_expr(stmt->goto_expression);
1240 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1241 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1242 __process_breaks();
1243 } else if (!strcmp(stmt->goto_label->ident->name,
1244 "continue")) {
1245 __process_continues();
1247 } else if (stmt->goto_label &&
1248 stmt->goto_label->type == SYM_LABEL &&
1249 stmt->goto_label->ident) {
1250 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1252 nullify_path();
1253 if (is_last_stmt(stmt))
1254 handle_backward_goto(stmt);
1255 break;
1256 case STMT_NONE:
1257 break;
1258 case STMT_ASM:
1259 expr_set_parent_stmt(stmt->asm_string, stmt);
1261 find_asm_gotos(stmt);
1262 __pass_to_client(stmt, ASM_HOOK);
1263 __split_expr(stmt->asm_string);
1264 split_asm_ops(stmt->asm_outputs);
1265 split_asm_ops(stmt->asm_inputs);
1266 split_expr_list(stmt->asm_clobbers, NULL);
1267 break;
1268 case STMT_CONTEXT:
1269 break;
1270 case STMT_RANGE:
1271 __split_expr(stmt->range_expression);
1272 __split_expr(stmt->range_low);
1273 __split_expr(stmt->range_high);
1274 break;
1276 __pass_to_client(stmt, STMT_HOOK_AFTER);
1277 if (--indent_cnt == 1)
1278 __discard_fake_states(NULL);
1280 out:
1281 __process_post_op_stack();
1283 gettimeofday(&stop, NULL);
1284 if (option_time_stmt && stmt)
1285 sm_msg("stmt_time%s: %ld",
1286 stmt->type == STMT_COMPOUND ? "_block" : "",
1287 stop.tv_sec - start.tv_sec);
1290 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1292 struct expression *expr;
1294 FOR_EACH_PTR(expr_list, expr) {
1295 expr_set_parent_expr(expr, parent);
1296 __split_expr(expr);
1297 __process_post_op_stack();
1298 } END_FOR_EACH_PTR(expr);
1301 static bool cast_arg(struct symbol *type, struct expression *arg)
1303 struct symbol *orig;
1305 if (!type)
1306 return false;
1308 arg = strip_parens(arg);
1309 if (arg != strip_expr(arg))
1310 return true;
1312 orig = get_type(arg);
1313 if (!orig)
1314 return true;
1315 if (orig == type)
1316 return false;
1318 if (orig->type == SYM_ARRAY && type->type == SYM_PTR)
1319 return true;
1322 * I would have expected that we could just do use (orig == type) but I
1323 * guess for pointers we need to get the basetype to do that comparison.
1327 if (orig->type != SYM_PTR ||
1328 type->type != SYM_PTR) {
1329 if (type_fits(type, orig))
1330 return false;
1331 return true;
1333 orig = get_real_base_type(orig);
1334 type = get_real_base_type(type);
1335 if (orig == type)
1336 return false;
1338 return true;
1341 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *call, struct expression *expr, int nr)
1343 struct expression *var, *assign, *parent;
1344 char buf[64];
1345 bool cast;
1347 if (!expr || !cur_func_sym)
1348 return NULL;
1350 if (expr->type == EXPR_ASSIGNMENT)
1351 return expr;
1353 /* for va_args then we don't know the type */
1354 if (!type)
1355 type = get_type(expr);
1357 cast = cast_arg(type, expr);
1359 * Using expr_to_sym() here is a hack. We want to say that we don't
1360 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1361 * It turns out faking these assignments is way more expensive than I
1362 * would have imagined. I'm not sure why exactly.
1365 if (!cast) {
1367 * if the code is "return *p;" where "p" is a user pointer then
1368 * we want to create a fake assignment so that it sets the state
1369 * in check_kernel_user_data.c.
1372 if (expr->type != EXPR_PREOP &&
1373 expr->op != '*' && expr->op != '&' &&
1374 expr_to_sym(expr))
1375 return expr;
1378 if (nr == -1)
1379 snprintf(buf, sizeof(buf), "__sm_fake_%p", expr);
1380 else
1381 snprintf(buf, sizeof(buf), "__fake_param_%p_%d", call, nr);
1382 var = fake_variable(type, buf);
1383 assign = assign_expression(var, '=', expr);
1384 assign->smatch_flags |= Fake;
1386 parent = expr_get_parent_expr(expr);
1387 expr_set_parent_expr(assign, parent);
1388 expr_set_parent_expr(expr, assign);
1390 __fake_state_cnt++;
1392 return assign;
1395 static void split_args(struct expression *expr)
1397 struct expression *arg, *tmp;
1398 struct symbol *type;
1399 int i;
1401 i = -1;
1402 FOR_EACH_PTR(expr->args, arg) {
1403 i++;
1404 expr_set_parent_expr(arg, expr);
1405 type = get_arg_type(expr->fn, i);
1406 tmp = fake_a_variable_assign(type, expr, arg, i);
1407 if (tmp != arg)
1408 __in_fake_var_assign++;
1409 __split_expr(tmp);
1410 if (tmp != arg)
1411 __in_fake_var_assign--;
1412 __process_post_op_stack();
1413 } END_FOR_EACH_PTR(arg);
1416 static void split_sym(struct symbol *sym)
1418 if (!sym)
1419 return;
1420 if (!(sym->namespace & NS_SYMBOL))
1421 return;
1423 __split_stmt(sym->stmt);
1424 __split_expr(sym->array_size);
1425 split_symlist(sym->arguments);
1426 split_symlist(sym->symbol_list);
1427 __split_stmt(sym->inline_stmt);
1428 split_symlist(sym->inline_symbol_list);
1431 static void split_symlist(struct symbol_list *sym_list)
1433 struct symbol *sym;
1435 FOR_EACH_PTR(sym_list, sym) {
1436 split_sym(sym);
1437 } END_FOR_EACH_PTR(sym);
1440 typedef void (fake_cb)(struct expression *expr);
1442 static int member_to_number(struct expression *expr, struct ident *member)
1444 struct symbol *type, *tmp;
1445 char *name;
1446 int i;
1448 if (!member)
1449 return -1;
1450 name = member->name;
1452 type = get_type(expr);
1453 if (!type || type->type != SYM_STRUCT)
1454 return -1;
1456 i = -1;
1457 FOR_EACH_PTR(type->symbol_list, tmp) {
1458 i++;
1459 if (!tmp->ident)
1460 continue;
1461 if (strcmp(name, tmp->ident->name) == 0)
1462 return i;
1463 } END_FOR_EACH_PTR(tmp);
1464 return -1;
1467 static struct ident *number_to_member(struct expression *expr, int num)
1469 struct symbol *type, *member;
1470 int i = 0;
1472 type = get_type(expr);
1473 if (!type || type->type != SYM_STRUCT)
1474 return NULL;
1476 FOR_EACH_PTR(type->symbol_list, member) {
1477 if (i == num)
1478 return member->ident;
1479 i++;
1480 } END_FOR_EACH_PTR(member);
1481 return NULL;
1484 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1486 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1488 struct expression *edge_member, *assign;
1489 struct symbol *base = get_real_base_type(member);
1490 struct symbol *tmp;
1492 if (member->ident)
1493 expr = member_expression(expr, '.', member->ident);
1495 FOR_EACH_PTR(base->symbol_list, tmp) {
1496 struct symbol *type;
1498 type = get_real_base_type(tmp);
1499 if (!type)
1500 continue;
1502 edge_member = member_expression(expr, '.', tmp->ident);
1503 if (get_extra_state(edge_member))
1504 continue;
1506 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1507 set_inner_struct_members(expr, tmp);
1508 continue;
1511 if (!tmp->ident)
1512 continue;
1514 assign = assign_expression(edge_member, '=', zero_expr());
1515 __split_expr(assign);
1516 } END_FOR_EACH_PTR(tmp);
1521 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1523 struct symbol *tmp;
1524 struct expression *member = NULL;
1525 struct expression *assign;
1527 FOR_EACH_PTR(type->symbol_list, tmp) {
1528 type = get_real_base_type(tmp);
1529 if (!type)
1530 continue;
1532 if (tmp->ident) {
1533 member = member_expression(expr, '.', tmp->ident);
1534 if (get_extra_state(member))
1535 continue;
1538 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1539 set_inner_struct_members(expr, tmp);
1540 continue;
1542 if (type->type == SYM_ARRAY)
1543 continue;
1544 if (!tmp->ident)
1545 continue;
1547 assign = assign_expression(member, '=', zero_expr());
1548 __split_expr(assign);
1549 } END_FOR_EACH_PTR(tmp);
1552 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1554 struct expression *deref, *assign, *tmp, *right;
1555 struct symbol *struct_type, *type;
1556 struct ident *member;
1557 int member_idx;
1559 struct_type = get_type(symbol);
1560 if (!struct_type ||
1561 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1562 return;
1565 * We're parsing an initializer that could look something like this:
1566 * struct foo foo = {
1567 * 42,
1568 * .whatever.xxx = 11,
1569 * .zzz = 12,
1570 * };
1572 * So what we have here is a list with 42, .whatever, and .zzz. We need
1573 * to break it up into left and right sides of the assignments.
1576 member_idx = 0;
1577 FOR_EACH_PTR(members, tmp) {
1578 deref = NULL;
1579 if (tmp->type == EXPR_IDENTIFIER) {
1580 member_idx = member_to_number(symbol, tmp->expr_ident);
1581 while (tmp->type == EXPR_IDENTIFIER) {
1582 member = tmp->expr_ident;
1583 tmp = tmp->ident_expression;
1584 if (deref)
1585 deref = member_expression(deref, '.', member);
1586 else
1587 deref = member_expression(symbol, '.', member);
1589 } else {
1590 member = number_to_member(symbol, member_idx);
1591 deref = member_expression(symbol, '.', member);
1593 right = tmp;
1594 member_idx++;
1595 if (right->type == EXPR_INITIALIZER) {
1596 type = get_type(deref);
1597 if (type && type->type == SYM_ARRAY)
1598 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1599 else
1600 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1601 } else {
1602 assign = assign_expression(deref, '=', right);
1603 fake_cb(assign);
1605 } END_FOR_EACH_PTR(tmp);
1607 set_unset_to_zero(struct_type, symbol);
1610 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1612 fake_member_assigns_helper(symbol_expression(sym),
1613 sym->initializer->expr_list, fake_cb);
1616 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1618 struct expression *offset, *binop, *assign, *tmp;
1619 struct symbol *type;
1620 int idx, max;
1622 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1623 return;
1625 max = 0;
1626 idx = 0;
1627 FOR_EACH_PTR(expr_list, tmp) {
1628 if (tmp->type == EXPR_INDEX) {
1629 if (tmp->idx_from != tmp->idx_to)
1630 return;
1631 idx = tmp->idx_from;
1632 if (idx > max)
1633 max = idx;
1634 if (!tmp->idx_expression)
1635 goto next;
1636 tmp = tmp->idx_expression;
1638 offset = value_expr(idx);
1639 binop = array_element_expression(array, offset);
1640 if (tmp->type == EXPR_INITIALIZER) {
1641 type = get_type(binop);
1642 if (type && type->type == SYM_ARRAY)
1643 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1644 else
1645 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1646 } else {
1647 assign = assign_expression(binop, '=', tmp);
1648 fake_cb(assign);
1650 next:
1651 idx++;
1652 if (idx > max)
1653 max = idx;
1654 } END_FOR_EACH_PTR(tmp);
1656 __call_array_initialized_hooks(array, max);
1659 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1661 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1664 static void fake_assign_expr(struct symbol *sym)
1666 struct expression *assign, *symbol;
1668 symbol = symbol_expression(sym);
1669 assign = assign_expression(symbol, '=', sym->initializer);
1670 __split_expr(assign);
1673 static void do_initializer_stuff(struct symbol *sym)
1675 if (!sym->initializer)
1676 return;
1678 if (sym->initializer->type == EXPR_INITIALIZER) {
1679 if (get_real_base_type(sym)->type == SYM_ARRAY)
1680 fake_element_assigns(sym, __split_expr);
1681 else
1682 fake_member_assigns(sym, __split_expr);
1683 } else {
1684 fake_assign_expr(sym);
1688 static void split_declaration(struct symbol_list *sym_list)
1690 struct symbol *sym;
1692 FOR_EACH_PTR(sym_list, sym) {
1693 __pass_to_client(sym, DECLARATION_HOOK);
1694 do_initializer_stuff(sym);
1695 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
1696 split_sym(sym);
1697 } END_FOR_EACH_PTR(sym);
1700 static void call_global_assign_hooks(struct expression *assign)
1702 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1705 static void fake_global_assign(struct symbol *sym)
1707 struct expression *assign, *symbol;
1709 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1710 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1711 fake_element_assigns(sym, call_global_assign_hooks);
1712 } else if (sym->initializer) {
1713 symbol = symbol_expression(sym);
1714 assign = assign_expression(symbol, '=', sym->initializer);
1715 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1716 } else {
1717 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1719 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1720 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1721 fake_member_assigns(sym, call_global_assign_hooks);
1722 } else if (sym->initializer) {
1723 symbol = symbol_expression(sym);
1724 assign = assign_expression(symbol, '=', sym->initializer);
1725 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1726 } else {
1727 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1729 } else {
1730 symbol = symbol_expression(sym);
1731 if (sym->initializer) {
1732 assign = assign_expression(symbol, '=', sym->initializer);
1733 __split_expr(assign);
1734 } else {
1735 assign = assign_expression(symbol, '=', zero_expr());
1737 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1741 static void start_function_definition(struct symbol *sym)
1743 __in_function_def = 1;
1744 __pass_to_client(sym, FUNC_DEF_HOOK);
1745 __in_function_def = 0;
1746 __pass_to_client(sym, AFTER_DEF_HOOK);
1750 void add_function_data(unsigned long *fn_data)
1752 __add_ptr_list(&fn_data_list, fn_data);
1755 static void clear_function_data(void)
1757 unsigned long *tmp;
1759 FOR_EACH_PTR(fn_data_list, tmp) {
1760 *tmp = 0;
1761 } END_FOR_EACH_PTR(tmp);
1764 static void record_func_time(void)
1766 struct timeval stop;
1767 int func_time;
1768 char buf[32];
1770 gettimeofday(&stop, NULL);
1771 func_time = stop.tv_sec - fn_start_time.tv_sec;
1772 snprintf(buf, sizeof(buf), "%d", func_time);
1773 sql_insert_return_implies(FUNC_TIME, 0, "", buf);
1774 if (option_time && func_time > 2) {
1775 final_pass++;
1776 sm_msg("func_time: %d", func_time);
1777 final_pass--;
1781 static void split_function(struct symbol *sym)
1783 struct symbol *base_type = get_base_type(sym);
1785 if (!base_type->stmt && !base_type->inline_stmt)
1786 return;
1788 gettimeofday(&outer_fn_start_time, NULL);
1789 gettimeofday(&fn_start_time, NULL);
1790 cur_func_sym = sym;
1791 if (sym->ident)
1792 cur_func = sym->ident->name;
1793 set_position(sym->pos);
1794 clear_function_data();
1795 loop_count = 0;
1796 last_goto_statement_handled = 0;
1797 sm_debug("new function: %s\n", cur_func);
1798 __stree_id = 0;
1799 if (option_two_passes) {
1800 __unnullify_path();
1801 loop_num = 0;
1802 final_pass = 0;
1803 start_function_definition(sym);
1804 __split_stmt(base_type->stmt);
1805 __split_stmt(base_type->inline_stmt);
1806 nullify_path();
1808 __unnullify_path();
1809 loop_num = 0;
1810 final_pass = 1;
1811 start_function_definition(sym);
1812 __split_stmt(base_type->stmt);
1813 __split_stmt(base_type->inline_stmt);
1814 if (!__path_is_null() &&
1815 cur_func_return_type() == &void_ctype &&
1816 !__bail_on_rest_of_function) {
1817 __pass_to_client(NULL, RETURN_HOOK);
1818 nullify_path();
1820 __pass_to_client(sym, END_FUNC_HOOK);
1821 if (need_delayed_scope_hooks())
1822 __call_scope_hooks();
1823 __pass_to_client(sym, AFTER_FUNC_HOOK);
1824 sym->parsed = true;
1826 clear_all_states();
1828 record_func_time();
1830 cur_func_sym = NULL;
1831 cur_func = NULL;
1832 free_data_info_allocs();
1833 free_expression_stack(&switch_expr_stack);
1834 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1835 __bail_on_rest_of_function = 0;
1838 static void save_flow_state(void)
1840 unsigned long *tmp;
1842 __add_ptr_list(&backup, INT_PTR(loop_num << 2));
1843 __add_ptr_list(&backup, INT_PTR(loop_count << 2));
1844 __add_ptr_list(&backup, INT_PTR(final_pass << 2));
1846 __add_ptr_list(&backup, big_statement_stack);
1847 __add_ptr_list(&backup, big_expression_stack);
1848 __add_ptr_list(&backup, big_condition_stack);
1849 __add_ptr_list(&backup, switch_expr_stack);
1851 __add_ptr_list(&backup, cur_func_sym);
1853 __add_ptr_list(&backup, __prev_stmt);
1854 __add_ptr_list(&backup, __cur_stmt);
1855 __add_ptr_list(&backup, __next_stmt);
1857 FOR_EACH_PTR(fn_data_list, tmp) {
1858 __add_ptr_list(&backup, (void *)*tmp);
1859 } END_FOR_EACH_PTR(tmp);
1862 static void *pop_backup(void)
1864 void *ret;
1866 ret = last_ptr_list(backup);
1867 delete_ptr_list_last(&backup);
1868 return ret;
1871 static void restore_flow_state(void)
1873 unsigned long *tmp;
1875 FOR_EACH_PTR_REVERSE(fn_data_list, tmp) {
1876 *tmp = (unsigned long)pop_backup();
1877 } END_FOR_EACH_PTR_REVERSE(tmp);
1879 __next_stmt = pop_backup();
1880 __cur_stmt = pop_backup();
1881 __prev_stmt = pop_backup();
1883 cur_func_sym = pop_backup();
1884 switch_expr_stack = pop_backup();
1885 big_condition_stack = pop_backup();
1886 big_expression_stack = pop_backup();
1887 big_statement_stack = pop_backup();
1888 final_pass = PTR_INT(pop_backup()) >> 2;
1889 loop_count = PTR_INT(pop_backup()) >> 2;
1890 loop_num = PTR_INT(pop_backup()) >> 2;
1893 static void parse_inline(struct expression *call)
1895 struct symbol *base_type;
1896 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
1897 struct timeval time_backup = fn_start_time;
1898 struct expression *orig_inline = __inline_fn;
1899 int orig_budget;
1901 if (out_of_memory() || taking_too_long())
1902 return;
1904 save_flow_state();
1906 __pass_to_client(call, INLINE_FN_START);
1907 final_pass = 0; /* don't print anything */
1908 __inline_fn = call;
1909 orig_budget = inline_budget;
1910 inline_budget = inline_budget - 5;
1912 base_type = get_base_type(call->fn->symbol);
1913 cur_func_sym = call->fn->symbol;
1914 if (call->fn->symbol->ident)
1915 cur_func = call->fn->symbol->ident->name;
1916 else
1917 cur_func = NULL;
1918 set_position(call->fn->symbol->pos);
1920 save_all_states();
1921 big_statement_stack = NULL;
1922 big_expression_stack = NULL;
1923 big_condition_stack = NULL;
1924 switch_expr_stack = NULL;
1926 sm_debug("inline function: %s\n", cur_func);
1927 __unnullify_path();
1928 clear_function_data();
1929 loop_num = 0;
1930 loop_count = 0;
1931 start_function_definition(call->fn->symbol);
1932 __split_stmt(base_type->stmt);
1933 __split_stmt(base_type->inline_stmt);
1934 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1935 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1936 call->fn->symbol->parsed = true;
1938 free_expression_stack(&switch_expr_stack);
1939 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1940 nullify_path();
1941 free_goto_stack();
1943 restore_flow_state();
1944 fn_start_time = time_backup;
1945 cur_func = cur_func_bak;
1947 restore_all_states();
1948 set_position(call->pos);
1949 __inline_fn = orig_inline;
1950 inline_budget = orig_budget;
1951 __pass_to_client(call, INLINE_FN_END);
1954 static struct symbol_list *inlines_called;
1955 static void add_inline_function(struct symbol *sym)
1957 static struct symbol_list *already_added;
1958 struct symbol *tmp;
1960 FOR_EACH_PTR(already_added, tmp) {
1961 if (tmp == sym)
1962 return;
1963 } END_FOR_EACH_PTR(tmp);
1965 add_ptr_list(&already_added, sym);
1966 add_ptr_list(&inlines_called, sym);
1969 static void process_inlines(void)
1971 struct symbol *tmp;
1973 FOR_EACH_PTR(inlines_called, tmp) {
1974 split_function(tmp);
1975 } END_FOR_EACH_PTR(tmp);
1976 free_ptr_list(&inlines_called);
1979 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1981 struct symbol *sym;
1983 FOR_EACH_PTR_REVERSE(big_list, sym) {
1984 if (!sym->scope)
1985 continue;
1986 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1987 return sym;
1988 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1989 return sym;
1990 } END_FOR_EACH_PTR_REVERSE(sym);
1992 return NULL;
1995 static bool interesting_function(struct symbol *sym)
1997 static int prev_stream = -1;
1998 static bool prev_answer;
1999 const char *filename;
2000 int len;
2002 if (!(sym->ctype.modifiers & MOD_INLINE))
2003 return true;
2005 if (sym->pos.stream == prev_stream)
2006 return prev_answer;
2008 prev_stream = sym->pos.stream;
2009 prev_answer = false;
2011 filename = stream_name(sym->pos.stream);
2012 len = strlen(filename);
2013 if (len > 0 && filename[len - 1] == 'c')
2014 prev_answer = true;
2015 return prev_answer;
2018 static void split_inlines_in_scope(struct symbol *sym)
2020 struct symbol *base;
2021 struct symbol_list *scope_list;
2022 int stream;
2024 scope_list = sym->scope->symbols;
2025 stream = sym->pos.stream;
2027 /* find the last static symbol in the file */
2028 FOR_EACH_PTR_REVERSE(scope_list, sym) {
2029 if (sym->pos.stream != stream)
2030 continue;
2031 if (sym->type != SYM_NODE)
2032 continue;
2033 base = get_base_type(sym);
2034 if (!base)
2035 continue;
2036 if (base->type != SYM_FN)
2037 continue;
2038 if (!base->inline_stmt)
2039 continue;
2040 if (!interesting_function(sym))
2041 continue;
2042 add_inline_function(sym);
2043 } END_FOR_EACH_PTR_REVERSE(sym);
2045 process_inlines();
2048 static void split_inlines(struct symbol_list *sym_list)
2050 struct symbol *sym;
2052 sym = get_last_scoped_symbol(sym_list, 0);
2053 if (sym)
2054 split_inlines_in_scope(sym);
2055 sym = get_last_scoped_symbol(sym_list, 1);
2056 if (sym)
2057 split_inlines_in_scope(sym);
2060 static struct stree *clone_estates_perm(struct stree *orig)
2062 struct stree *ret = NULL;
2063 struct sm_state *tmp;
2065 FOR_EACH_SM(orig, tmp) {
2066 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
2067 } END_FOR_EACH_SM(tmp);
2069 return ret;
2072 struct position last_pos;
2073 static void split_c_file_functions(struct symbol_list *sym_list)
2075 struct symbol *sym;
2077 __unnullify_path();
2078 FOR_EACH_PTR(sym_list, sym) {
2079 set_position(sym->pos);
2080 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
2081 __pass_to_client(sym, BASE_HOOK);
2082 fake_global_assign(sym);
2083 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
2085 } END_FOR_EACH_PTR(sym);
2086 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
2087 nullify_path();
2089 FOR_EACH_PTR(sym_list, sym) {
2090 set_position(sym->pos);
2091 last_pos = sym->pos;
2092 if (!interesting_function(sym))
2093 continue;
2094 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
2095 split_function(sym);
2096 process_inlines();
2098 last_pos = sym->pos;
2099 } END_FOR_EACH_PTR(sym);
2100 split_inlines(sym_list);
2101 __pass_to_client(sym_list, END_FILE_HOOK);
2104 static int final_before_fake;
2105 void init_fake_env(void)
2107 if (!in_fake_env)
2108 final_before_fake = final_pass;
2109 in_fake_env++;
2110 __push_fake_cur_stree();
2111 final_pass = 0;
2114 void end_fake_env(void)
2116 __pop_fake_cur_stree();
2117 in_fake_env--;
2118 if (!in_fake_env)
2119 final_pass = final_before_fake;
2122 static void open_output_files(char *base_file)
2124 char buf[256];
2126 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
2127 sm_outfd = fopen(buf, "w");
2128 if (!sm_outfd)
2129 sm_fatal("Cannot open %s", buf);
2131 if (!option_info)
2132 return;
2134 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
2135 sql_outfd = fopen(buf, "w");
2136 if (!sql_outfd)
2137 sm_fatal("Error: Cannot open %s", buf);
2139 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
2140 caller_info_fd = fopen(buf, "w");
2141 if (!caller_info_fd)
2142 sm_fatal("Error: Cannot open %s", buf);
2145 void smatch(struct string_list *filelist)
2147 struct symbol_list *sym_list;
2148 struct timeval stop, start;
2149 char *path;
2150 int len;
2152 gettimeofday(&start, NULL);
2154 FOR_EACH_PTR_NOTAG(filelist, base_file) {
2155 path = getcwd(NULL, 0);
2156 free(full_base_file);
2157 if (path) {
2158 len = strlen(path) + 1 + strlen(base_file) + 1;
2159 full_base_file = malloc(len);
2160 snprintf(full_base_file, len, "%s/%s", path, base_file);
2161 } else {
2162 full_base_file = alloc_string(base_file);
2164 if (option_file_output)
2165 open_output_files(base_file);
2166 base_file_stream = input_stream_nr;
2167 sym_list = sparse_keep_tokens(base_file);
2168 split_c_file_functions(sym_list);
2169 } END_FOR_EACH_PTR_NOTAG(base_file);
2171 gettimeofday(&stop, NULL);
2173 set_position(last_pos);
2174 final_pass = 1;
2175 if (option_time)
2176 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
2177 if (option_mem)
2178 sm_msg("mem: %luKb", get_max_memory());