states, stree: change break_stack to stree
[smatch.git] / check_stack.c
blobb685aeadb4eefc486aa8e6e4bb7e36eb84263a82
1 /*
2 * Copyright (C) 2010 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
19 * The kernel has a small stack so putting huge structs and arrays on the
20 * stack is a bug.
24 #include "smatch.h"
26 static int my_id;
28 static int total_size;
29 static int max_size;
30 static int max_lineno;
31 static int complained;
33 #define MAX_ALLOWED 1000
35 static void scope_end(int size)
37 total_size -= size;
40 static void match_declarations(struct symbol *sym)
42 struct symbol *base;
43 const char *name;
45 base = get_base_type(sym);
46 if (sym->ctype.modifiers & MOD_STATIC)
47 return;
48 name = sym->ident->name;
49 total_size += base->bit_size;
50 if (total_size > max_size) {
51 max_size = total_size;
52 max_lineno = get_lineno();
54 if (base->bit_size >= MAX_ALLOWED * 8) {
55 complained = 1;
56 sm_msg("warn: '%s' puts %d bytes on stack", name, base->bit_size / 8);
58 add_scope_hook((scope_hook *)&scope_end, INT_PTR(base->bit_size));
61 static void match_end_func(struct symbol *sym)
63 if (__inline_fn)
64 return;
66 if ((max_size >= MAX_ALLOWED * 8) && !complained) {
67 sm_printf("%s:%d %s() ", get_filename(), max_lineno, get_function());
68 sm_printf("warn: function puts %d bytes on stack\n", max_size / 8);
70 total_size = 0;
71 complained = 0;
72 max_size = 0;
73 max_lineno = 0;
76 void check_stack(int id)
78 if (option_project != PROJ_KERNEL || !option_spammy)
79 return;
81 my_id = id;
82 add_hook(&match_declarations, DECLARATION_HOOK);
83 add_hook(&match_end_func, END_FUNC_HOOK);