param_key: fix container of when no struct member is referenced
[smatch.git] / smatch_allocations.c
blob8c7bdb2077d9ffb96a2b176132de18d66563a271
1 /*
2 * Copyright (C) 2021 Oracle.
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 #include <fcntl.h>
19 #include <unistd.h>
20 #include "parse.h"
21 #include "smatch.h"
22 #include "smatch_slist.h"
24 static int my_id;
26 DECLARE_PTR_LIST(alloc_hook_list, alloc_hook);
27 static struct alloc_hook_list *hook_funcs;
29 struct alloc_fn_info {
30 const char *name;
31 const char *size;
32 bool zeroed;
35 static struct alloc_fn_info alloc_fns[] = {
36 {"malloc", "$0"},
37 {"calloc", "$0 * $1", .zeroed=true},
38 {"memdup", "$1"},
39 {"realloc", "$1"},
40 { },
43 static struct alloc_fn_info kernel_alloc_funcs[] = {
44 {"__alloc_skb", "$0"},
46 {"devm_kmalloc", "$1"},
47 {"devm_kzalloc", "$1"},
48 {"devm_kmalloc_array", "$1 * $2"},
50 {"dma_alloc_attrs", "$1"},
51 {"dma_alloc_coherent", "$1"},
52 {"dma_alloc_consistent", "$1"},
53 {"dma_alloc_contiguous", "$1"},
55 {"krealloc", "$1"},
57 {"kmalloc", "$0"},
58 {"kmalloc_node", "$0"},
59 {"kzalloc", "$0", .zeroed=true},
60 {"kzalloc_node", "$0", .zeroed=true},
62 {"kmalloc_array", "$0 * $1"},
63 {"kcalloc", "$0 * $1", .zeroed=true},
65 {"vmalloc", "$0"},
66 {"__vmalloc", "$0"},
67 {"vzalloc", "$0", .zeroed=true},
69 {"kvmalloc", "$0"},
70 {"kvmalloc_array", "$0"},
71 {"kvcalloc", "$0 * $1", .zeroed=true},
73 {"kmemdup", "$1"},
74 {"memdup_user", "$1"},
76 {"sock_kmalloc", "$1"},
78 #if 0
79 {"get_zeroed_page", {"PAGE_SIZE", zeroed=true}},
80 {"alloc_page", {"PAGE_SIZE"}},
81 {"alloc_pages", {"(1 < $0) * PAGE_SIZE"}},
82 {"alloc_pages_current", {"(1 < $0) * PAGE_SIZE"}},
83 {"__get_free_pages", {"(1 < $0) * PAGE_SIZE"}},
84 #endif
85 { },
88 void add_allocation_hook(alloc_hook *hook)
90 add_ptr_list(&hook_funcs, hook);
93 static void match_alloc(struct expression *expr, const char *name, struct symbol *sym, void *_info)
95 struct alloc_fn_info *info = _info;
96 struct allocation_info data = { };
97 alloc_hook *fn;
99 data.size_str = info->size;
100 data.zeroed = info->zeroed;
102 FOR_EACH_PTR(hook_funcs, fn) {
103 fn(expr, name, sym, &data);
104 } END_FOR_EACH_PTR(fn);
107 void register_allocations(int id)
109 struct alloc_fn_info *info;
111 my_id = id;
113 if (option_project == PROJ_KERNEL)
114 info = kernel_alloc_funcs;
115 else
116 info = alloc_fns;
118 while (info->name) {
119 add_function_param_key_hook(info->name, &match_alloc, -1, "$", info);
120 info++;