1*1f5207b7SJohn Levon /*
2*1f5207b7SJohn Levon  * Copyright (C) 2014 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 /*
19*1f5207b7SJohn Levon  * This file is sort of like check_dereferences_param.c.  In theory the one
20*1f5207b7SJohn Levon  * difference should be that the param is NULL it should still be counted as a
21*1f5207b7SJohn Levon  * free.  But for now I don't handle that case.
22*1f5207b7SJohn Levon  */
23*1f5207b7SJohn Levon 
24*1f5207b7SJohn Levon #include "smatch.h"
25*1f5207b7SJohn Levon #include "smatch_extra.h"
26*1f5207b7SJohn Levon #include "smatch_slist.h"
27*1f5207b7SJohn Levon 
28*1f5207b7SJohn Levon static int my_id;
29*1f5207b7SJohn Levon 
30*1f5207b7SJohn Levon STATE(freed);
31*1f5207b7SJohn Levon STATE(ignore);
32*1f5207b7SJohn Levon 
set_ignore(struct sm_state * sm,struct expression * mod_expr)33*1f5207b7SJohn Levon static void set_ignore(struct sm_state *sm, struct expression *mod_expr)
34*1f5207b7SJohn Levon {
35*1f5207b7SJohn Levon 	set_state(my_id, sm->name, sm->sym, &ignore);
36*1f5207b7SJohn Levon }
37*1f5207b7SJohn Levon 
freed_variable(struct expression * expr)38*1f5207b7SJohn Levon static void freed_variable(struct expression *expr)
39*1f5207b7SJohn Levon {
40*1f5207b7SJohn Levon 	struct sm_state *sm;
41*1f5207b7SJohn Levon 
42*1f5207b7SJohn Levon 	expr = strip_expr(expr);
43*1f5207b7SJohn Levon 	if (get_param_num(expr) < 0)
44*1f5207b7SJohn Levon 		return;
45*1f5207b7SJohn Levon 
46*1f5207b7SJohn Levon 	sm = get_sm_state_expr(my_id, expr);
47*1f5207b7SJohn Levon 	if (sm && slist_has_state(sm->possible, &ignore))
48*1f5207b7SJohn Levon 		return;
49*1f5207b7SJohn Levon 	set_state_expr(my_id, expr, &freed);
50*1f5207b7SJohn Levon }
51*1f5207b7SJohn Levon 
match_free(const char * fn,struct expression * expr,void * param)52*1f5207b7SJohn Levon static void match_free(const char *fn, struct expression *expr, void *param)
53*1f5207b7SJohn Levon {
54*1f5207b7SJohn Levon 	struct expression *arg;
55*1f5207b7SJohn Levon 
56*1f5207b7SJohn Levon 	arg = get_argument_from_call_expr(expr->args, PTR_INT(param));
57*1f5207b7SJohn Levon 	if (!arg)
58*1f5207b7SJohn Levon 		return;
59*1f5207b7SJohn Levon 	freed_variable(arg);
60*1f5207b7SJohn Levon }
61*1f5207b7SJohn Levon 
set_param_freed(struct expression * call,struct expression * arg,char * key,char * unused)62*1f5207b7SJohn Levon static void set_param_freed(struct expression *call, struct expression *arg, char *key, char *unused)
63*1f5207b7SJohn Levon {
64*1f5207b7SJohn Levon 	/* XXX FIXME: return_implies has been updated with more information */
65*1f5207b7SJohn Levon 	if (strcmp(key, "$") != 0)
66*1f5207b7SJohn Levon 		return;
67*1f5207b7SJohn Levon 	freed_variable(arg);
68*1f5207b7SJohn Levon }
69*1f5207b7SJohn Levon 
process_states(void)70*1f5207b7SJohn Levon static void process_states(void)
71*1f5207b7SJohn Levon {
72*1f5207b7SJohn Levon 	struct sm_state *sm;
73*1f5207b7SJohn Levon 	int param;
74*1f5207b7SJohn Levon 	const char *param_name;
75*1f5207b7SJohn Levon 
76*1f5207b7SJohn Levon 	FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
77*1f5207b7SJohn Levon 		if (sm->state != &freed)
78*1f5207b7SJohn Levon 			continue;
79*1f5207b7SJohn Levon 		param = get_param_num_from_sym(sm->sym);
80*1f5207b7SJohn Levon 		if (param < 0)
81*1f5207b7SJohn Levon 			continue;
82*1f5207b7SJohn Levon 		param_name = get_param_name(sm);
83*1f5207b7SJohn Levon 		if (!param_name)
84*1f5207b7SJohn Levon 			continue;
85*1f5207b7SJohn Levon 		sql_insert_return_implies(PARAM_FREED, param, param_name, "1");
86*1f5207b7SJohn Levon 	} END_FOR_EACH_SM(sm);
87*1f5207b7SJohn Levon 
88*1f5207b7SJohn Levon }
89*1f5207b7SJohn Levon 
check_frees_param(int id)90*1f5207b7SJohn Levon void check_frees_param(int id)
91*1f5207b7SJohn Levon {
92*1f5207b7SJohn Levon 	my_id = id;
93*1f5207b7SJohn Levon 
94*1f5207b7SJohn Levon 	if (option_project == PROJ_KERNEL) {
95*1f5207b7SJohn Levon 		/* The kernel uses check_frees_param_strict.c */
96*1f5207b7SJohn Levon 		return;
97*1f5207b7SJohn Levon 	}
98*1f5207b7SJohn Levon 
99*1f5207b7SJohn Levon 	add_function_hook("free", &match_free, INT_PTR(0));
100*1f5207b7SJohn Levon 
101*1f5207b7SJohn Levon 	select_return_implies_hook(PARAM_FREED, &set_param_freed);
102*1f5207b7SJohn Levon 	add_modification_hook(my_id, &set_ignore);
103*1f5207b7SJohn Levon 
104*1f5207b7SJohn Levon 	all_return_states_hook(&process_states);
105*1f5207b7SJohn Levon }
106