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