1 /*
2 * Copyright (C) 2010 Dan Carpenter.
3 *
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.
8 *
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.
13 *
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
16 */
17
18 #include "smatch.h"
19
20 static int my_id;
21
22 extern int check_assigned_expr_id;
23
24 static int is_kmalloc_call(struct expression *expr)
25 {
26 if (expr->type != EXPR_CALL)
27 return 0;
28 if (expr->fn->type != EXPR_SYMBOL)
29 return 0;
30 if (!strcmp(expr->fn->symbol_name->name, "kmalloc"))
31 return 1;
32 if (!strcmp(expr->fn->symbol_name->name, "kzalloc"))
33 return 1;
34 return 0;
35 }
36
37 static void match_condition(struct expression *expr)
38 {
39 char *macro;
40 struct smatch_state *state;
41 struct expression *right;
42 char *name;
43
44 macro = get_macro_name(expr->pos);
45 if (!macro || strcmp(macro, "BUG_ON") != 0)
46 return;
47 state = get_state_expr(check_assigned_expr_id, expr);
48 if (!state || !state->data)
49 return;
50 right = (struct expression *)state->data;
51 if (!is_kmalloc_call(right))
52 return;
53
54 name = expr_to_var(expr);
55 sm_warning("bug on allocation failure '%s'", name);
56 free_string(name);
57 }
58
59 void check_kmalloc_to_bugon(int id)
60 {
61 if (option_project != PROJ_KERNEL)
62 return;
63 if (!option_spammy)
64 return;
65 my_id = id;
66 add_hook(&match_condition, CONDITION_HOOK);
67 }