1 /*
2  * Copyright (C) 2013 Oracle.
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 
match_assign(const char * fn,struct expression * expr,void * _size_arg)22 static void match_assign(const char *fn, struct expression *expr, void *_size_arg)
23 {
24 	int size_arg = PTR_INT(_size_arg);
25 	struct expression *left;
26 	struct expression *call;
27 	struct expression *arg;
28 	struct symbol *left_type;
29 	struct symbol *size_type;
30 
31 	left = strip_expr(expr->left);
32 	left_type = get_type(left);
33 	if (!left_type || left_type->type != SYM_PTR)
34 		return;
35 	left_type = get_real_base_type(left_type);
36 	if (!left_type || left_type->type != SYM_STRUCT)
37 		return;
38 
39 	call = strip_expr(expr->right);
40 	arg = get_argument_from_call_expr(call->args, size_arg);
41 	if (!arg || arg->type != EXPR_SIZEOF || !arg->cast_type)
42 		return;
43 	size_type = arg->cast_type;
44 	if (size_type->type != SYM_NODE)
45 		return;
46 	size_type = get_real_base_type(size_type);
47 	if (size_type->type != SYM_STRUCT)
48 		return;
49 	if (strcmp(left_type->ident->name, size_type->ident->name) == 0)
50 		return;
51 	sm_warning("struct type mismatch '%s vs %s'", left_type->ident->name,
52 	       size_type->ident->name);
53 
54 }
55 
check_struct_type(int id)56 void check_struct_type(int id)
57 {
58 	my_id = id;
59 
60 	if (option_project == PROJ_KERNEL) {
61 		add_function_assign_hook("kmalloc", &match_assign, INT_PTR(0));
62 		add_function_assign_hook("kzalloc", &match_assign, INT_PTR(0));
63 	}
64 }
65