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
22 #include "smatch_slist.h"
26 DECLARE_PTR_LIST(alloc_hook_list
, alloc_hook
);
27 static struct alloc_hook_list
*hook_funcs
;
29 struct alloc_fn_info
{
35 static struct alloc_fn_info alloc_fns
[] = {
37 {"calloc", "$0 * $1", .zeroed
=true},
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"},
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},
67 {"vzalloc", "$0", .zeroed
=true},
70 {"kvmalloc_array", "$0"},
71 {"kvcalloc", "$0 * $1", .zeroed
=true},
74 {"memdup_user", "$1"},
76 {"sock_kmalloc", "$1"},
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"}},
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
= { };
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
;
113 if (option_project
== PROJ_KERNEL
)
114 info
= kernel_alloc_funcs
;
119 add_function_param_key_hook(info
->name
, &match_alloc
, -1, "$", info
);