1*1f5207b7SJohn Levon /*
2*1f5207b7SJohn Levon  * Copyright (C) 2011 Dan Carpenter.
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 
get_data_size(struct expression * ptr)22*1f5207b7SJohn Levon static int get_data_size(struct expression *ptr)
23*1f5207b7SJohn Levon {
24*1f5207b7SJohn Levon 	struct symbol *type;
25*1f5207b7SJohn Levon 
26*1f5207b7SJohn Levon 	type = get_type(ptr);
27*1f5207b7SJohn Levon 	if (!type || type->type != SYM_PTR)
28*1f5207b7SJohn Levon 		return 0;
29*1f5207b7SJohn Levon 	type = get_base_type(type);
30*1f5207b7SJohn Levon 	if (!type)
31*1f5207b7SJohn Levon 		return 0;
32*1f5207b7SJohn Levon 	return type_bytes(type);
33*1f5207b7SJohn Levon }
34*1f5207b7SJohn Levon 
check_size_matches(int data_size,struct expression * size_expr)35*1f5207b7SJohn Levon static void check_size_matches(int data_size, struct expression *size_expr)
36*1f5207b7SJohn Levon {
37*1f5207b7SJohn Levon 	sval_t sval;
38*1f5207b7SJohn Levon 
39*1f5207b7SJohn Levon 	if (data_size == 1)  /* this is generic a buffer */
40*1f5207b7SJohn Levon 		return;
41*1f5207b7SJohn Levon 
42*1f5207b7SJohn Levon 	if (!get_implied_value(size_expr, &sval))
43*1f5207b7SJohn Levon 		return;
44*1f5207b7SJohn Levon 	if (sval_cmp_val(sval, data_size) != 0)
45*1f5207b7SJohn Levon 		sm_warning("double check that we're allocating correct size: %d vs %s", data_size, sval_to_str(sval));
46*1f5207b7SJohn Levon }
47*1f5207b7SJohn Levon 
match_alloc(const char * fn,struct expression * expr,void * unused)48*1f5207b7SJohn Levon static void match_alloc(const char *fn, struct expression *expr, void *unused)
49*1f5207b7SJohn Levon {
50*1f5207b7SJohn Levon 	struct expression *call = strip_expr(expr->right);
51*1f5207b7SJohn Levon 	struct expression *arg;
52*1f5207b7SJohn Levon 	int ptr_size;
53*1f5207b7SJohn Levon 
54*1f5207b7SJohn Levon 	ptr_size = get_data_size(expr->left);
55*1f5207b7SJohn Levon 	if (!ptr_size)
56*1f5207b7SJohn Levon 		return;
57*1f5207b7SJohn Levon 
58*1f5207b7SJohn Levon 	arg = get_argument_from_call_expr(call->args, 0);
59*1f5207b7SJohn Levon 	arg = strip_expr(arg);
60*1f5207b7SJohn Levon 	if (!arg || arg->type != EXPR_BINOP || arg->op != '*')
61*1f5207b7SJohn Levon 		return;
62*1f5207b7SJohn Levon 	if (expr->left->type == EXPR_SIZEOF)
63*1f5207b7SJohn Levon 		check_size_matches(ptr_size, arg->left);
64*1f5207b7SJohn Levon 	if (expr->right->type == EXPR_SIZEOF)
65*1f5207b7SJohn Levon 		check_size_matches(ptr_size, arg->right);
66*1f5207b7SJohn Levon }
67*1f5207b7SJohn Levon 
match_calloc(const char * fn,struct expression * expr,void * _arg_nr)68*1f5207b7SJohn Levon static void match_calloc(const char *fn, struct expression *expr, void *_arg_nr)
69*1f5207b7SJohn Levon {
70*1f5207b7SJohn Levon 	int arg_nr = PTR_INT(_arg_nr);
71*1f5207b7SJohn Levon 	struct expression *call = strip_expr(expr->right);
72*1f5207b7SJohn Levon 	struct expression *arg;
73*1f5207b7SJohn Levon 	int ptr_size;
74*1f5207b7SJohn Levon 
75*1f5207b7SJohn Levon 	ptr_size = get_data_size(expr->left);
76*1f5207b7SJohn Levon 	if (!ptr_size)
77*1f5207b7SJohn Levon 		return;
78*1f5207b7SJohn Levon 
79*1f5207b7SJohn Levon 	arg = get_argument_from_call_expr(call->args, arg_nr);
80*1f5207b7SJohn Levon 	check_size_matches(ptr_size, arg);
81*1f5207b7SJohn Levon }
82*1f5207b7SJohn Levon 
check_kmalloc_wrong_size(int id)83*1f5207b7SJohn Levon void check_kmalloc_wrong_size(int id)
84*1f5207b7SJohn Levon {
85*1f5207b7SJohn Levon 	my_id = id;
86*1f5207b7SJohn Levon 
87*1f5207b7SJohn Levon 	if (option_project != PROJ_KERNEL) {
88*1f5207b7SJohn Levon 		add_function_assign_hook("malloc", &match_alloc, NULL);
89*1f5207b7SJohn Levon 		add_function_assign_hook("calloc", &match_calloc, INT_PTR(1));
90*1f5207b7SJohn Levon 		return;
91*1f5207b7SJohn Levon 	}
92*1f5207b7SJohn Levon 
93*1f5207b7SJohn Levon 	add_function_assign_hook("kmalloc", &match_alloc, NULL);
94*1f5207b7SJohn Levon 	add_function_assign_hook("kcalloc", &match_calloc, INT_PTR(1));
95*1f5207b7SJohn Levon }
96