smdb_param_buf_size.sh: ask db what size buffers are passed to a function
[smatch.git] / check_stack.c
blob23c00a78433448a13545ce46241eab49c5c7fa9f
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 static void scope_end(int size)
27 total_size -= size;
30 static void match_declarations(struct symbol *sym)
32 struct symbol *base;
33 const char *name;
35 base = get_base_type(sym);
36 if (sym->ctype.modifiers & MOD_STATIC)
37 return;
38 name = sym->ident->name;
39 total_size += base->bit_size;
40 if (total_size > max_size) {
41 max_size = total_size;
42 max_lineno = get_lineno();
44 if (base->bit_size >= 500 * 8) {
45 complained = 1;
46 sm_msg("warn: '%s' puts %d bytes on stack", name, base->bit_size / 8);
48 add_scope_hook((scope_hook *)&scope_end, INT_PTR(base->bit_size));
51 static void match_end_func(struct symbol *sym)
53 if ((max_size >= 500 * 8) && !complained) {
54 sm_printf("%s:%d %s() ", get_filename(), max_lineno, get_function());
55 sm_printf("warn: function puts %d bytes on stack\n", max_size / 8);
57 total_size = 0;
58 complained = 0;
59 max_size = 0;
60 max_lineno = 0;
63 void check_stack(int id)
65 if (option_project != PROJ_KERNEL)
66 return;
68 my_id = id;
69 add_hook(&match_declarations, DECLARATION_HOOK);
70 add_hook(&match_end_func, END_FUNC_HOOK);