2 * Copyright (C) 2013 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
24 static void match_assign(const char *fn
, struct expression
*expr
, void *unused
)
26 set_state_expr(my_id
, expr
->left
, &devm
);
29 static void match_allocation(struct expression
*expr
,
30 const char *name
, struct symbol
*sym
,
31 struct allocation_info
*info
)
34 * Only handle devm_ memory allocator
36 if (strncmp(info
->fn_name
, "devm_", 5) == 0)
37 match_assign(name
, expr
, NULL
);
41 * This hook deals with things like:
42 * ptr = devm_kmalloc(...);
45 * another_val = ptr; <==
47 static void match_reassign(struct expression
*expr
)
49 struct expression
*left
, *right
;
54 right
= strip_expr(expr
->right
);
55 if (!get_state_expr(my_id
, right
))
58 left
= strip_expr(expr
->left
);
60 set_state_expr(my_id
, left
, &devm
);
63 static void match_free_func(const char *fn
, struct expression
*expr
, void *_arg
)
65 struct expression
*arg_expr
;
66 int arg
= PTR_INT(_arg
);
69 arg_expr
= get_argument_from_call_expr(expr
->args
, arg
);
70 if (!get_state_expr(my_id
, arg_expr
))
72 name
= expr_to_str(arg_expr
);
73 sm_warning("passing devm_ allocated variable to %s. '%s'", fn
, name
);
77 static void register_funcs_from_file(void)
83 token
= get_tokens_file("kernel.frees_argument");
86 if (token_type(token
) != TOKEN_STREAMBEGIN
)
89 while (token_type(token
) != TOKEN_STREAMEND
) {
90 if (token_type(token
) != TOKEN_IDENT
)
92 func
= show_ident(token
->ident
);
94 if (token_type(token
) != TOKEN_NUMBER
)
96 arg
= atoi(token
->number
);
97 add_function_hook(func
, &match_free_func
, INT_PTR(arg
));
103 void check_freeing_devm(int id
)
105 if (option_project
!= PROJ_KERNEL
)
111 * We register all allocation functions, but only devm_ will be handled
112 * in match_allocation()
114 add_allocation_hook(&match_allocation
);
116 add_function_assign_hook("devm_kstrdup", &match_assign
, NULL
);
117 add_function_assign_hook("devm_kasprintf", &match_assign
, NULL
);
118 add_function_assign_hook("devm_kvasprintf", &match_assign
, NULL
);
120 add_function_hook("kfree", &match_free_func
, INT_PTR(0));
121 add_function_hook("krealloc", &match_free_func
, INT_PTR(0));
122 register_funcs_from_file();
124 add_hook(&match_reassign
, ASSIGNMENT_HOOK
);