1*1f5207b7SJohn Levon /*
2*1f5207b7SJohn Levon  * Copyright (C) 2013 Oracle.
3*1f5207b7SJohn Levon  *
4*1f5207b7SJohn Levon  * This program is free software; you can redistribute it and/or
5*1f5207b7SJohn Levon  * modify it under the terms of the GNU General Public License
6*1f5207b7SJohn Levon  * as published by the Free Software Foundation; either version 2
7*1f5207b7SJohn Levon  * of the License, or (at your option) any later version.
8*1f5207b7SJohn Levon  *
9*1f5207b7SJohn Levon  * This program is distributed in the hope that it will be useful,
10*1f5207b7SJohn Levon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*1f5207b7SJohn Levon  * GNU General Public License for more details.
13*1f5207b7SJohn Levon  *
14*1f5207b7SJohn Levon  * You should have received a copy of the GNU General Public License
15*1f5207b7SJohn Levon  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16*1f5207b7SJohn Levon  */
17*1f5207b7SJohn Levon 
18*1f5207b7SJohn Levon #include "smatch.h"
19*1f5207b7SJohn Levon 
20*1f5207b7SJohn Levon STATE(devm);
21*1f5207b7SJohn Levon 
22*1f5207b7SJohn Levon static int my_id;
23*1f5207b7SJohn Levon 
match_assign(const char * fn,struct expression * expr,void * unused)24*1f5207b7SJohn Levon static void match_assign(const char *fn, struct expression *expr, void *unused)
25*1f5207b7SJohn Levon {
26*1f5207b7SJohn Levon 	set_state_expr(my_id, expr->left, &devm);
27*1f5207b7SJohn Levon }
28*1f5207b7SJohn Levon 
match_free_func(const char * fn,struct expression * expr,void * _arg)29*1f5207b7SJohn Levon static void match_free_func(const char *fn, struct expression *expr, void *_arg)
30*1f5207b7SJohn Levon {
31*1f5207b7SJohn Levon 	struct expression *arg_expr;
32*1f5207b7SJohn Levon 	int arg = PTR_INT(_arg);
33*1f5207b7SJohn Levon 	char *name;
34*1f5207b7SJohn Levon 
35*1f5207b7SJohn Levon 	arg_expr = get_argument_from_call_expr(expr->args, arg);
36*1f5207b7SJohn Levon 	if (!get_state_expr(my_id, arg_expr))
37*1f5207b7SJohn Levon 		return;
38*1f5207b7SJohn Levon 	name = expr_to_str(arg_expr);
39*1f5207b7SJohn Levon 	sm_warning("passing devm_ allocated variable to kfree. '%s'", name);
40*1f5207b7SJohn Levon 	free_string(name);
41*1f5207b7SJohn Levon }
42*1f5207b7SJohn Levon 
register_funcs_from_file(void)43*1f5207b7SJohn Levon static void register_funcs_from_file(void)
44*1f5207b7SJohn Levon {
45*1f5207b7SJohn Levon 	struct token *token;
46*1f5207b7SJohn Levon 	const char *func;
47*1f5207b7SJohn Levon 	int arg;
48*1f5207b7SJohn Levon 
49*1f5207b7SJohn Levon 	token = get_tokens_file("kernel.frees_argument");
50*1f5207b7SJohn Levon 	if (!token)
51*1f5207b7SJohn Levon 		return;
52*1f5207b7SJohn Levon 	if (token_type(token) != TOKEN_STREAMBEGIN)
53*1f5207b7SJohn Levon 		return;
54*1f5207b7SJohn Levon 	token = token->next;
55*1f5207b7SJohn Levon 	while (token_type(token) != TOKEN_STREAMEND) {
56*1f5207b7SJohn Levon 		if (token_type(token) != TOKEN_IDENT)
57*1f5207b7SJohn Levon 			return;
58*1f5207b7SJohn Levon 		func = show_ident(token->ident);
59*1f5207b7SJohn Levon 		token = token->next;
60*1f5207b7SJohn Levon 		if (token_type(token) != TOKEN_NUMBER)
61*1f5207b7SJohn Levon 			return;
62*1f5207b7SJohn Levon 		arg = atoi(token->number);
63*1f5207b7SJohn Levon 		add_function_hook(func, &match_free_func, INT_PTR(arg));
64*1f5207b7SJohn Levon 		token = token->next;
65*1f5207b7SJohn Levon 	}
66*1f5207b7SJohn Levon 	clear_token_alloc();
67*1f5207b7SJohn Levon }
68*1f5207b7SJohn Levon 
check_freeing_devm(int id)69*1f5207b7SJohn Levon void check_freeing_devm(int id)
70*1f5207b7SJohn Levon {
71*1f5207b7SJohn Levon 	if (option_project != PROJ_KERNEL)
72*1f5207b7SJohn Levon 		return;
73*1f5207b7SJohn Levon 
74*1f5207b7SJohn Levon 	my_id = id;
75*1f5207b7SJohn Levon 
76*1f5207b7SJohn Levon 	add_function_assign_hook("devm_kmalloc", &match_assign, NULL);
77*1f5207b7SJohn Levon 	add_function_assign_hook("devm_kzalloc", &match_assign, NULL);
78*1f5207b7SJohn Levon 	add_function_assign_hook("devm_kcalloc", &match_assign, NULL);
79*1f5207b7SJohn Levon 	add_function_assign_hook("devm_kmalloc_array", &match_assign, NULL);
80*1f5207b7SJohn Levon 
81*1f5207b7SJohn Levon 
82*1f5207b7SJohn Levon 	add_function_hook("kfree", &match_free_func, INT_PTR(0));
83*1f5207b7SJohn Levon 	add_function_hook("krealloc", &match_free_func, INT_PTR(0));
84*1f5207b7SJohn Levon 	register_funcs_from_file();
85*1f5207b7SJohn Levon }