helper: taking the address is not a complicated variable
[smatch.git] / check_stack.c
bloba2b7dc060b1fbfe5facf822a01be69e30cc70cab
1 /*
2 * smatch/check_stack.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * The kernel has a small stack so putting huge structs and arrays on the
12 * stack is a bug.
16 #include "smatch.h"
18 static int my_id;
20 static int total_size;
21 static int max_size;
22 static int max_lineno;
23 static int complained;
25 #define MAX_ALLOWED 1000
27 static void scope_end(int size)
29 total_size -= size;
32 static void match_declarations(struct symbol *sym)
34 struct symbol *base;
35 const char *name;
37 base = get_base_type(sym);
38 if (sym->ctype.modifiers & MOD_STATIC)
39 return;
40 name = sym->ident->name;
41 total_size += base->bit_size;
42 if (total_size > max_size) {
43 max_size = total_size;
44 max_lineno = get_lineno();
46 if (base->bit_size >= MAX_ALLOWED * 8) {
47 complained = 1;
48 sm_msg("warn: '%s' puts %d bytes on stack", name, base->bit_size / 8);
50 add_scope_hook((scope_hook *)&scope_end, INT_PTR(base->bit_size));
53 static void match_end_func(struct symbol *sym)
55 if (__inline_fn)
56 return;
58 if ((max_size >= MAX_ALLOWED * 8) && !complained) {
59 sm_printf("%s:%d %s() ", get_filename(), max_lineno, get_function());
60 sm_printf("warn: function puts %d bytes on stack\n", max_size / 8);
62 total_size = 0;
63 complained = 0;
64 max_size = 0;
65 max_lineno = 0;
68 void check_stack(int id)
70 if (option_project != PROJ_KERNEL || !option_spammy)
71 return;
73 my_id = id;
74 add_hook(&match_declarations, DECLARATION_HOOK);
75 add_hook(&match_end_func, END_FUNC_HOOK);