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
);
30 * This hook deals with things like:
31 * ptr = devm_kmalloc(...);
34 * another_val = ptr; <==
36 static void match_reassign(struct expression
*expr
)
38 struct expression
*left
, *right
;
43 right
= strip_expr(expr
->right
);
44 if (!get_state_expr(my_id
, right
))
47 left
= strip_expr(expr
->left
);
49 set_state_expr(my_id
, left
, &devm
);
52 static void match_free_func(const char *fn
, struct expression
*expr
, void *_arg
)
54 struct expression
*arg_expr
;
55 int arg
= PTR_INT(_arg
);
58 arg_expr
= get_argument_from_call_expr(expr
->args
, arg
);
59 if (!get_state_expr(my_id
, arg_expr
))
61 name
= expr_to_str(arg_expr
);
62 sm_warning("passing devm_ allocated variable to %s. '%s'", fn
, name
);
66 static void register_funcs_from_file(void)
72 token
= get_tokens_file("kernel.frees_argument");
75 if (token_type(token
) != TOKEN_STREAMBEGIN
)
78 while (token_type(token
) != TOKEN_STREAMEND
) {
79 if (token_type(token
) != TOKEN_IDENT
)
81 func
= show_ident(token
->ident
);
83 if (token_type(token
) != TOKEN_NUMBER
)
85 arg
= atoi(token
->number
);
86 add_function_hook(func
, &match_free_func
, INT_PTR(arg
));
92 void check_freeing_devm(int id
)
94 if (option_project
!= PROJ_KERNEL
)
99 add_function_assign_hook("devm_kmalloc", &match_assign
, NULL
);
100 add_function_assign_hook("devm_kzalloc", &match_assign
, NULL
);
101 add_function_assign_hook("devm_kcalloc", &match_assign
, NULL
);
102 add_function_assign_hook("devm_kmalloc_array", &match_assign
, NULL
);
104 add_function_assign_hook("devm_kmemdup", &match_assign
, NULL
);
105 add_function_assign_hook("devm_kstrdup", &match_assign
, NULL
);
106 add_function_assign_hook("devm_kasprintf", &match_assign
, NULL
);
107 add_function_assign_hook("devm_kvasprintf", &match_assign
, NULL
);
109 add_function_hook("kfree", &match_free_func
, INT_PTR(0));
110 add_function_hook("krealloc", &match_free_func
, INT_PTR(0));
111 register_funcs_from_file();
113 add_hook(&match_reassign
, ASSIGNMENT_HOOK
);