forked from error27/smatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_allocation_funcs.c
133 lines (120 loc) · 2.94 KB
/
check_allocation_funcs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Copyright (C) 2009 Dan Carpenter.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
*/
#include <fcntl.h>
#include <unistd.h>
#include "parse.h"
#include "smatch.h"
#include "smatch_slist.h"
static int my_id;
/*
* Print a list of functions that return newly allocated memory.
*/
static struct tracker_list *allocated;
static const char *allocation_funcs[] = {
"kmalloc",
"kmalloc_node",
"kzalloc",
"kzalloc_node",
"vmalloc",
"vzalloc",
"__vmalloc",
"kvmalloc",
"kcalloc",
"__alloc_skb",
"kvcalloc",
"kmalloc_array",
"devm_kmalloc_array",
"sock_kmalloc",
"kmemdup",
"memdup_user",
"dma_alloc_attrs",
"pci_alloc_consistent",
"pci_alloc_coherent",
"devm_kmalloc",
"devm_kzalloc",
"krealloc",
"__alloc_bootmem",
"alloc_bootmem",
"get_zeroed_page",
"alloc_page",
"alloc_pages",
"alloc_pages_current",
"__get_free_pages",
"dma_alloc_contiguous",
"dma_alloc_coherent",
NULL,
};
static void match_allocation(const char *fn, struct expression *expr,
void *info)
{
char *left_name;
struct symbol *left_sym;
left_name = expr_to_var_sym(expr->left, &left_sym);
if (!left_name || !left_sym)
goto free;
if (left_sym->ctype.modifiers &
(MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
goto free;
add_tracker(&allocated, my_id, left_name, left_sym);
free:
free_string(left_name);
}
static int returns_new_stuff = 0;
static int returns_old_stuff = 0;
static void match_return(struct expression *ret_value)
{
char *name;
struct symbol *sym;
sval_t tmp;
if (__inline_fn)
return;
if (get_value(ret_value, &tmp) && tmp.value == 0)
return;
returns_new_stuff = 1;
name = expr_to_var_sym(ret_value, &sym);
if (!name || !sym) {
returns_old_stuff = 1;
goto free;
}
if (!in_tracker_list(allocated, my_id, name, sym))
returns_old_stuff = 1;
free:
free_string(name);
}
static void match_end_func(struct symbol *sym)
{
if (__inline_fn)
return;
if (returns_new_stuff && !returns_old_stuff)
sm_info("allocation func");
free_trackers_and_list(&allocated);
returns_new_stuff = 0;
returns_old_stuff = 0;
}
void check_allocation_funcs(int id)
{
int i;
if (!option_info || option_project != PROJ_KERNEL)
return;
my_id = id;
add_hook(&match_return, RETURN_HOOK);
add_hook(&match_end_func, AFTER_FUNC_HOOK);
for (i = 0; allocation_funcs[i]; i++) {
add_function_assign_hook(allocation_funcs[i],
&match_allocation, NULL);
}
}