11f5207b7SJohn Levon /*
21f5207b7SJohn Levon  * Copyright (C) 2010 Dan Carpenter.
31f5207b7SJohn Levon  *
41f5207b7SJohn Levon  * This program is free software; you can redistribute it and/or
51f5207b7SJohn Levon  * modify it under the terms of the GNU General Public License
61f5207b7SJohn Levon  * as published by the Free Software Foundation; either version 2
71f5207b7SJohn Levon  * of the License, or (at your option) any later version.
81f5207b7SJohn Levon  *
91f5207b7SJohn Levon  * This program is distributed in the hope that it will be useful,
101f5207b7SJohn Levon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
111f5207b7SJohn Levon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
121f5207b7SJohn Levon  * GNU General Public License for more details.
131f5207b7SJohn Levon  *
141f5207b7SJohn Levon  * You should have received a copy of the GNU General Public License
151f5207b7SJohn Levon  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
161f5207b7SJohn Levon  */
171f5207b7SJohn Levon 
181f5207b7SJohn Levon /*
191f5207b7SJohn Levon  * check_memory() is getting too big and messy.
201f5207b7SJohn Levon  *
211f5207b7SJohn Levon  */
221f5207b7SJohn Levon 
231f5207b7SJohn Levon #include <string.h>
241f5207b7SJohn Levon #include "smatch.h"
251f5207b7SJohn Levon #include "smatch_slist.h"
261f5207b7SJohn Levon #include "smatch_extra.h"
271f5207b7SJohn Levon 
281f5207b7SJohn Levon static int my_id;
291f5207b7SJohn Levon 
301f5207b7SJohn Levon STATE(freed);
311f5207b7SJohn Levon STATE(ok);
321f5207b7SJohn Levon 
ok_to_use(struct sm_state * sm,struct expression * mod_expr)331f5207b7SJohn Levon static void ok_to_use(struct sm_state *sm, struct expression *mod_expr)
341f5207b7SJohn Levon {
351f5207b7SJohn Levon 	if (sm->state != &ok)
361f5207b7SJohn Levon 		set_state(my_id, sm->name, sm->sym, &ok);
371f5207b7SJohn Levon }
381f5207b7SJohn Levon 
pre_merge_hook(struct sm_state * cur,struct sm_state * other)39*c85f09ccSJohn Levon static void pre_merge_hook(struct sm_state *cur, struct sm_state *other)
401f5207b7SJohn Levon {
411f5207b7SJohn Levon 	if (is_impossible_path())
42*c85f09ccSJohn Levon 		set_state(my_id, cur->name, cur->sym, &ok);
43*c85f09ccSJohn Levon }
44*c85f09ccSJohn Levon 
unmatched_state(struct sm_state * sm)45*c85f09ccSJohn Levon static struct smatch_state *unmatched_state(struct sm_state *sm)
46*c85f09ccSJohn Levon {
47*c85f09ccSJohn Levon 	struct smatch_state *state;
48*c85f09ccSJohn Levon 	sval_t sval;
49*c85f09ccSJohn Levon 
50*c85f09ccSJohn Levon 	if (sm->state != &freed)
51*c85f09ccSJohn Levon 		return &undefined;
52*c85f09ccSJohn Levon 
53*c85f09ccSJohn Levon 	state = get_state(SMATCH_EXTRA, sm->name, sm->sym);
54*c85f09ccSJohn Levon 	if (!state)
55*c85f09ccSJohn Levon 		return &undefined;
56*c85f09ccSJohn Levon 	if (!estate_get_single_value(state, &sval) || sval.value != 0)
57*c85f09ccSJohn Levon 		return &undefined;
58*c85f09ccSJohn Levon 	/* It makes it easier to consider NULL pointers as freed.  */
59*c85f09ccSJohn Levon 	return &freed;
601f5207b7SJohn Levon }
611f5207b7SJohn Levon 
is_freed(struct expression * expr)621f5207b7SJohn Levon static int is_freed(struct expression *expr)
631f5207b7SJohn Levon {
641f5207b7SJohn Levon 	struct sm_state *sm;
651f5207b7SJohn Levon 
661f5207b7SJohn Levon 	sm = get_sm_state_expr(my_id, expr);
671f5207b7SJohn Levon 	if (sm && slist_has_state(sm->possible, &freed))
681f5207b7SJohn Levon 		return 1;
691f5207b7SJohn Levon 	return 0;
701f5207b7SJohn Levon }
711f5207b7SJohn Levon 
match_symbol(struct expression * expr)721f5207b7SJohn Levon static void match_symbol(struct expression *expr)
731f5207b7SJohn Levon {
741f5207b7SJohn Levon 	struct expression *parent;
751f5207b7SJohn Levon 	char *name;
761f5207b7SJohn Levon 
771f5207b7SJohn Levon 	if (is_impossible_path())
781f5207b7SJohn Levon 		return;
791f5207b7SJohn Levon 	if (__in_fake_parameter_assign)
801f5207b7SJohn Levon 		return;
811f5207b7SJohn Levon 
821f5207b7SJohn Levon 	parent = expr_get_parent_expr(expr);
831f5207b7SJohn Levon 	while (parent && parent->type == EXPR_PREOP && parent->op == '(')
841f5207b7SJohn Levon 		parent = expr_get_parent_expr(parent);
851f5207b7SJohn Levon 	if (parent && parent->type == EXPR_PREOP && parent->op == '&')
861f5207b7SJohn Levon 		return;
871f5207b7SJohn Levon 
881f5207b7SJohn Levon 	if (!is_freed(expr))
891f5207b7SJohn Levon 		return;
901f5207b7SJohn Levon 	name = expr_to_var(expr);
911f5207b7SJohn Levon 	sm_warning("'%s' was already freed.", name);
921f5207b7SJohn Levon 	free_string(name);
931f5207b7SJohn Levon }
941f5207b7SJohn Levon 
match_dereferences(struct expression * expr)951f5207b7SJohn Levon static void match_dereferences(struct expression *expr)
961f5207b7SJohn Levon {
971f5207b7SJohn Levon 	char *name;
981f5207b7SJohn Levon 
991f5207b7SJohn Levon 	if (expr->type != EXPR_PREOP)
1001f5207b7SJohn Levon 		return;
1011f5207b7SJohn Levon 
1021f5207b7SJohn Levon 	if (is_impossible_path())
1031f5207b7SJohn Levon 		return;
1041f5207b7SJohn Levon 	if (__in_fake_parameter_assign)
1051f5207b7SJohn Levon 		return;
1061f5207b7SJohn Levon 
1071f5207b7SJohn Levon 	expr = strip_expr(expr->unop);
1081f5207b7SJohn Levon 	if (!is_freed(expr))
1091f5207b7SJohn Levon 		return;
1101f5207b7SJohn Levon 	name = expr_to_var(expr);
1111f5207b7SJohn Levon 	sm_error("dereferencing freed memory '%s'", name);
1121f5207b7SJohn Levon 	set_state_expr(my_id, expr, &ok);
1131f5207b7SJohn Levon 	free_string(name);
1141f5207b7SJohn Levon }
1151f5207b7SJohn Levon 
1161f5207b7SJohn Levon static int ignored_params[16];
1171f5207b7SJohn Levon 
set_ignored_params(struct expression * call)1181f5207b7SJohn Levon static void set_ignored_params(struct expression *call)
1191f5207b7SJohn Levon {
1201f5207b7SJohn Levon 	struct expression *arg;
1211f5207b7SJohn Levon 	const char *p;
1221f5207b7SJohn Levon 	int i;
1231f5207b7SJohn Levon 
1241f5207b7SJohn Levon 	memset(&ignored_params, 0, sizeof(ignored_params));
1251f5207b7SJohn Levon 
1261f5207b7SJohn Levon 	i = -1;
1271f5207b7SJohn Levon 	FOR_EACH_PTR(call->args, arg) {
1281f5207b7SJohn Levon 		i++;
1291f5207b7SJohn Levon 		if (arg->type != EXPR_STRING)
1301f5207b7SJohn Levon 			continue;
1311f5207b7SJohn Levon 		goto found;
1321f5207b7SJohn Levon 	} END_FOR_EACH_PTR(arg);
1331f5207b7SJohn Levon 
1341f5207b7SJohn Levon 	return;
1351f5207b7SJohn Levon 
1361f5207b7SJohn Levon found:
1371f5207b7SJohn Levon 	i++;
1381f5207b7SJohn Levon 	p = arg->string->data;
1391f5207b7SJohn Levon 	while ((p = strchr(p, '%'))) {
1401f5207b7SJohn Levon 		if (i >= ARRAY_SIZE(ignored_params))
1411f5207b7SJohn Levon 			return;
1421f5207b7SJohn Levon 		p++;
1431f5207b7SJohn Levon 		if (*p == '%') {
1441f5207b7SJohn Levon 			p++;
1451f5207b7SJohn Levon 			continue;
1461f5207b7SJohn Levon 		}
1471f5207b7SJohn Levon 		if (*p == '.')
1481f5207b7SJohn Levon 			p++;
1491f5207b7SJohn Levon 		if (*p == '*')
1501f5207b7SJohn Levon 			i++;
1511f5207b7SJohn Levon 		if (*p == 'p')
1521f5207b7SJohn Levon 			ignored_params[i] = 1;
1531f5207b7SJohn Levon 		i++;
1541f5207b7SJohn Levon 	}
1551f5207b7SJohn Levon }
1561f5207b7SJohn Levon 
is_free_func(struct expression * fn)1571f5207b7SJohn Levon static int is_free_func(struct expression *fn)
1581f5207b7SJohn Levon {
1591f5207b7SJohn Levon 	char *name;
1601f5207b7SJohn Levon 	int ret = 0;
1611f5207b7SJohn Levon 
1621f5207b7SJohn Levon 	name = expr_to_str(fn);
1631f5207b7SJohn Levon 	if (!name)
1641f5207b7SJohn Levon 		return 0;
1651f5207b7SJohn Levon 	if (strstr(name, "free"))
1661f5207b7SJohn Levon 		ret = 1;
1671f5207b7SJohn Levon 	free_string(name);
1681f5207b7SJohn Levon 
1691f5207b7SJohn Levon 	return ret;
1701f5207b7SJohn Levon }
1711f5207b7SJohn Levon 
match_call(struct expression * expr)1721f5207b7SJohn Levon static void match_call(struct expression *expr)
1731f5207b7SJohn Levon {
1741f5207b7SJohn Levon 	struct expression *arg;
1751f5207b7SJohn Levon 	char *name;
1761f5207b7SJohn Levon 	int i;
1771f5207b7SJohn Levon 
1781f5207b7SJohn Levon 	if (is_impossible_path())
1791f5207b7SJohn Levon 		return;
1801f5207b7SJohn Levon 
1811f5207b7SJohn Levon 	set_ignored_params(expr);
1821f5207b7SJohn Levon 
1831f5207b7SJohn Levon 	i = -1;
1841f5207b7SJohn Levon 	FOR_EACH_PTR(expr->args, arg) {
1851f5207b7SJohn Levon 		i++;
1861f5207b7SJohn Levon 		if (!is_pointer(arg))
1871f5207b7SJohn Levon 			continue;
1881f5207b7SJohn Levon 		if (!is_freed(arg))
1891f5207b7SJohn Levon 			continue;
1901f5207b7SJohn Levon 		if (ignored_params[i])
1911f5207b7SJohn Levon 			continue;
1921f5207b7SJohn Levon 
1931f5207b7SJohn Levon 		name = expr_to_var(arg);
1941f5207b7SJohn Levon 		if (is_free_func(expr->fn))
1951f5207b7SJohn Levon 			sm_error("double free of '%s'", name);
1961f5207b7SJohn Levon 		else
1971f5207b7SJohn Levon 			sm_warning("passing freed memory '%s'", name);
1981f5207b7SJohn Levon 		set_state_expr(my_id, arg, &ok);
1991f5207b7SJohn Levon 		free_string(name);
2001f5207b7SJohn Levon 	} END_FOR_EACH_PTR(arg);
2011f5207b7SJohn Levon }
2021f5207b7SJohn Levon 
match_return(struct expression * expr)2031f5207b7SJohn Levon static void match_return(struct expression *expr)
2041f5207b7SJohn Levon {
2051f5207b7SJohn Levon 	char *name;
2061f5207b7SJohn Levon 
2071f5207b7SJohn Levon 	if (is_impossible_path())
2081f5207b7SJohn Levon 		return;
2091f5207b7SJohn Levon 
2101f5207b7SJohn Levon 	if (!expr)
2111f5207b7SJohn Levon 		return;
2121f5207b7SJohn Levon 	if (!is_freed(expr))
2131f5207b7SJohn Levon 		return;
2141f5207b7SJohn Levon 
2151f5207b7SJohn Levon 	name = expr_to_var(expr);
2161f5207b7SJohn Levon 	sm_warning("returning freed memory '%s'", name);
2171f5207b7SJohn Levon 	set_state_expr(my_id, expr, &ok);
2181f5207b7SJohn Levon 	free_string(name);
2191f5207b7SJohn Levon }
2201f5207b7SJohn Levon 
match_free(const char * fn,struct expression * expr,void * param)2211f5207b7SJohn Levon static void match_free(const char *fn, struct expression *expr, void *param)
2221f5207b7SJohn Levon {
2231f5207b7SJohn Levon 	struct expression *arg;
2241f5207b7SJohn Levon 
2251f5207b7SJohn Levon 	if (is_impossible_path())
2261f5207b7SJohn Levon 		return;
2271f5207b7SJohn Levon 
2281f5207b7SJohn Levon 	arg = get_argument_from_call_expr(expr->args, PTR_INT(param));
2291f5207b7SJohn Levon 	if (!arg)
2301f5207b7SJohn Levon 		return;
2311f5207b7SJohn Levon 	if (is_freed(arg)) {
2321f5207b7SJohn Levon 		char *name = expr_to_var(arg);
2331f5207b7SJohn Levon 
2341f5207b7SJohn Levon 		sm_error("double free of '%s'", name);
2351f5207b7SJohn Levon 		free_string(name);
2361f5207b7SJohn Levon 	}
2371f5207b7SJohn Levon 	set_state_expr(my_id, arg, &freed);
2381f5207b7SJohn Levon }
2391f5207b7SJohn Levon 
set_param_freed(struct expression * expr,int param,char * key,char * value)2401f5207b7SJohn Levon static void set_param_freed(struct expression *expr, int param, char *key, char *value)
2411f5207b7SJohn Levon {
2421f5207b7SJohn Levon 	struct expression *arg;
2431f5207b7SJohn Levon 	char *name;
2441f5207b7SJohn Levon 	struct symbol *sym;
2451f5207b7SJohn Levon 	struct sm_state *sm;
2461f5207b7SJohn Levon 
2471f5207b7SJohn Levon 	while (expr->type == EXPR_ASSIGNMENT)
2481f5207b7SJohn Levon 		expr = strip_expr(expr->right);
2491f5207b7SJohn Levon 	if (expr->type != EXPR_CALL)
2501f5207b7SJohn Levon 		return;
2511f5207b7SJohn Levon 
2521f5207b7SJohn Levon 	arg = get_argument_from_call_expr(expr->args, param);
2531f5207b7SJohn Levon 	if (!arg)
2541f5207b7SJohn Levon 		return;
2551f5207b7SJohn Levon 	name = get_variable_from_key(arg, key, &sym);
2561f5207b7SJohn Levon 	if (!name || !sym)
2571f5207b7SJohn Levon 		goto free;
2581f5207b7SJohn Levon 
2591f5207b7SJohn Levon 	if (!is_impossible_path()) {
2601f5207b7SJohn Levon 		sm = get_sm_state(my_id, name, sym);
2611f5207b7SJohn Levon 		if (sm && slist_has_state(sm->possible, &freed)) {
2621f5207b7SJohn Levon 			sm_warning("'%s' double freed", name);
2631f5207b7SJohn Levon 			set_state(my_id, name, sym, &ok);  /* fixme: doesn't silence anything.  I know */
2641f5207b7SJohn Levon 		}
2651f5207b7SJohn Levon 	}
2661f5207b7SJohn Levon 
2671f5207b7SJohn Levon 	set_state(my_id, name, sym, &freed);
2681f5207b7SJohn Levon free:
2691f5207b7SJohn Levon 	free_string(name);
2701f5207b7SJohn Levon }
2711f5207b7SJohn Levon 
parent_is_free_var_sym_strict(const char * name,struct symbol * sym)2721f5207b7SJohn Levon int parent_is_free_var_sym_strict(const char *name, struct symbol *sym)
2731f5207b7SJohn Levon {
2741f5207b7SJohn Levon 	char buf[256];
2751f5207b7SJohn Levon 	char *start;
2761f5207b7SJohn Levon 	char *end;
2771f5207b7SJohn Levon 	struct smatch_state *state;
2781f5207b7SJohn Levon 
2791f5207b7SJohn Levon 	strncpy(buf, name, sizeof(buf) - 1);
2801f5207b7SJohn Levon 	buf[sizeof(buf) - 1] = '\0';
2811f5207b7SJohn Levon 
2821f5207b7SJohn Levon 	start = &buf[0];
2831f5207b7SJohn Levon 	while ((*start == '&'))
2841f5207b7SJohn Levon 		start++;
2851f5207b7SJohn Levon 
2861f5207b7SJohn Levon 	while ((end = strrchr(start, '-'))) {
2871f5207b7SJohn Levon 		*end = '\0';
2881f5207b7SJohn Levon 		state = __get_state(my_id, start, sym);
2891f5207b7SJohn Levon 		if (state == &freed)
2901f5207b7SJohn Levon 			return 1;
2911f5207b7SJohn Levon 	}
2921f5207b7SJohn Levon 	return 0;
2931f5207b7SJohn Levon }
2941f5207b7SJohn Levon 
parent_is_free_strict(struct expression * expr)2951f5207b7SJohn Levon int parent_is_free_strict(struct expression *expr)
2961f5207b7SJohn Levon {
2971f5207b7SJohn Levon 	struct symbol *sym;
2981f5207b7SJohn Levon 	char *var;
2991f5207b7SJohn Levon 	int ret = 0;
3001f5207b7SJohn Levon 
3011f5207b7SJohn Levon 	expr = strip_expr(expr);
3021f5207b7SJohn Levon 	var = expr_to_var_sym(expr, &sym);
3031f5207b7SJohn Levon 	if (!var || !sym)
3041f5207b7SJohn Levon 		goto free;
3051f5207b7SJohn Levon 	ret = parent_is_free_var_sym_strict(var, sym);
3061f5207b7SJohn Levon free:
3071f5207b7SJohn Levon 	free_string(var);
3081f5207b7SJohn Levon 	return ret;
3091f5207b7SJohn Levon }
3101f5207b7SJohn Levon 
match_untracked(struct expression * call,int param)311efe51d0cSJohn Levon static void match_untracked(struct expression *call, int param)
312efe51d0cSJohn Levon {
313efe51d0cSJohn Levon 	struct state_list *slist = NULL;
314efe51d0cSJohn Levon 	struct expression *arg;
315efe51d0cSJohn Levon 	struct sm_state *sm;
316efe51d0cSJohn Levon 	char *name;
317efe51d0cSJohn Levon 	char buf[64];
318efe51d0cSJohn Levon 	int len;
319efe51d0cSJohn Levon 
320efe51d0cSJohn Levon 	arg = get_argument_from_call_expr(call->args, param);
321efe51d0cSJohn Levon 	if (!arg)
322efe51d0cSJohn Levon 		return;
323efe51d0cSJohn Levon 
324efe51d0cSJohn Levon 	name = expr_to_var(arg);
325efe51d0cSJohn Levon 	if (!name)
326efe51d0cSJohn Levon 		return;
327efe51d0cSJohn Levon 	snprintf(buf, sizeof(buf), "%s->", name);
328efe51d0cSJohn Levon 	free_string(name);
329efe51d0cSJohn Levon 	len = strlen(buf);
330efe51d0cSJohn Levon 
331efe51d0cSJohn Levon 	FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
332efe51d0cSJohn Levon 		if (strncmp(sm->name, buf, len) == 0)
333efe51d0cSJohn Levon 			add_ptr_list(&slist, sm);
334efe51d0cSJohn Levon 	} END_FOR_EACH_SM(sm);
335efe51d0cSJohn Levon 
336efe51d0cSJohn Levon 	FOR_EACH_PTR(slist, sm) {
337efe51d0cSJohn Levon 		set_state(sm->owner, sm->name, sm->sym, &ok);
338efe51d0cSJohn Levon 	} END_FOR_EACH_PTR(sm);
339efe51d0cSJohn Levon 
340efe51d0cSJohn Levon 	free_slist(&slist);
341efe51d0cSJohn Levon }
342efe51d0cSJohn Levon 
check_free_strict(int id)3431f5207b7SJohn Levon void check_free_strict(int id)
3441f5207b7SJohn Levon {
3451f5207b7SJohn Levon 	my_id = id;
3461f5207b7SJohn Levon 
3471f5207b7SJohn Levon 	if (option_project != PROJ_KERNEL)
3481f5207b7SJohn Levon 		return;
3491f5207b7SJohn Levon 
3501f5207b7SJohn Levon 	add_function_hook("kfree", &match_free, INT_PTR(0));
3511f5207b7SJohn Levon 	add_function_hook("kmem_cache_free", &match_free, INT_PTR(1));
3521f5207b7SJohn Levon 
3531f5207b7SJohn Levon 	if (option_spammy)
3541f5207b7SJohn Levon 		add_hook(&match_symbol, SYM_HOOK);
3551f5207b7SJohn Levon 	add_hook(&match_dereferences, DEREF_HOOK);
3561f5207b7SJohn Levon 	add_hook(&match_call, FUNCTION_CALL_HOOK);
3571f5207b7SJohn Levon 	add_hook(&match_return, RETURN_HOOK);
3581f5207b7SJohn Levon 
3591f5207b7SJohn Levon 	add_modification_hook_late(my_id, &ok_to_use);
3601f5207b7SJohn Levon 	add_pre_merge_hook(my_id, &pre_merge_hook);
361*c85f09ccSJohn Levon 	add_unmatched_state_hook(my_id, &unmatched_state);
3621f5207b7SJohn Levon 
3631f5207b7SJohn Levon 	select_return_states_hook(PARAM_FREED, &set_param_freed);
364efe51d0cSJohn Levon 	add_untracked_param_hook(&match_untracked);
3651f5207b7SJohn Levon }
366