1*1f5207b7SJohn Levon /*
2*1f5207b7SJohn Levon  * Copyright (C) 2012 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 is an --info recipe.  The goal is to print a message for every parameter
20*1f5207b7SJohn Levon  * which we can not avoid dereferencing.  This is maybe a bit restrictive but it
21*1f5207b7SJohn Levon  * avoids some false positives.
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(derefed);
31*1f5207b7SJohn Levon STATE(ignore);
32*1f5207b7SJohn Levon STATE(param);
33*1f5207b7SJohn Levon 
set_ignore(struct sm_state * sm,struct expression * mod_expr)34*1f5207b7SJohn Levon static void set_ignore(struct sm_state *sm, struct expression *mod_expr)
35*1f5207b7SJohn Levon {
36*1f5207b7SJohn Levon 	if (sm->state == &derefed)
37*1f5207b7SJohn Levon 		return;
38*1f5207b7SJohn Levon 	set_state(my_id, sm->name, sm->sym, &ignore);
39*1f5207b7SJohn Levon }
40*1f5207b7SJohn Levon 
match_function_def(struct symbol * sym)41*1f5207b7SJohn Levon static void match_function_def(struct symbol *sym)
42*1f5207b7SJohn Levon {
43*1f5207b7SJohn Levon 	struct symbol *arg;
44*1f5207b7SJohn Levon 	int i;
45*1f5207b7SJohn Levon 
46*1f5207b7SJohn Levon 	i = -1;
47*1f5207b7SJohn Levon 	FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
48*1f5207b7SJohn Levon 		i++;
49*1f5207b7SJohn Levon 		if (!arg->ident)
50*1f5207b7SJohn Levon 			continue;
51*1f5207b7SJohn Levon 		set_state(my_id, arg->ident->name, arg, &param);
52*1f5207b7SJohn Levon 	} END_FOR_EACH_PTR(arg);
53*1f5207b7SJohn Levon }
54*1f5207b7SJohn Levon 
check_deref(struct expression * expr)55*1f5207b7SJohn Levon static void check_deref(struct expression *expr)
56*1f5207b7SJohn Levon {
57*1f5207b7SJohn Levon 	struct expression *tmp;
58*1f5207b7SJohn Levon 	struct sm_state *sm;
59*1f5207b7SJohn Levon 
60*1f5207b7SJohn Levon 	tmp = get_assigned_expr(expr);
61*1f5207b7SJohn Levon 	if (tmp)
62*1f5207b7SJohn Levon 		expr = tmp;
63*1f5207b7SJohn Levon 	expr = strip_expr(expr);
64*1f5207b7SJohn Levon 
65*1f5207b7SJohn Levon 	if (get_param_num(expr) < 0)
66*1f5207b7SJohn Levon 		return;
67*1f5207b7SJohn Levon 
68*1f5207b7SJohn Levon 	if (param_was_set(expr))
69*1f5207b7SJohn Levon 		return;
70*1f5207b7SJohn Levon 
71*1f5207b7SJohn Levon 	sm = get_sm_state_expr(my_id, expr);
72*1f5207b7SJohn Levon 	if (sm && slist_has_state(sm->possible, &ignore))
73*1f5207b7SJohn Levon 		return;
74*1f5207b7SJohn Levon 	set_state_expr(my_id, expr, &derefed);
75*1f5207b7SJohn Levon }
76*1f5207b7SJohn Levon 
match_dereference(struct expression * expr)77*1f5207b7SJohn Levon static void match_dereference(struct expression *expr)
78*1f5207b7SJohn Levon {
79*1f5207b7SJohn Levon 	if (expr->type != EXPR_PREOP)
80*1f5207b7SJohn Levon 		return;
81*1f5207b7SJohn Levon 	check_deref(expr->unop);
82*1f5207b7SJohn Levon }
83*1f5207b7SJohn Levon 
set_param_dereferenced(struct expression * call,struct expression * arg,char * key,char * unused)84*1f5207b7SJohn Levon static void set_param_dereferenced(struct expression *call, struct expression *arg, char *key, char *unused)
85*1f5207b7SJohn Levon {
86*1f5207b7SJohn Levon 	/* XXX FIXME: param_implies has more information now */
87*1f5207b7SJohn Levon 	if (strcmp(key, "$") != 0)
88*1f5207b7SJohn Levon 		return;
89*1f5207b7SJohn Levon 	check_deref(arg);
90*1f5207b7SJohn Levon }
91*1f5207b7SJohn Levon 
process_states(void)92*1f5207b7SJohn Levon static void process_states(void)
93*1f5207b7SJohn Levon {
94*1f5207b7SJohn Levon 	struct sm_state *tmp;
95*1f5207b7SJohn Levon 	int arg;
96*1f5207b7SJohn Levon 	const char *name;
97*1f5207b7SJohn Levon 
98*1f5207b7SJohn Levon 	FOR_EACH_MY_SM(my_id, __get_cur_stree(), tmp) {
99*1f5207b7SJohn Levon 		if (tmp->state != &derefed)
100*1f5207b7SJohn Levon 			continue;
101*1f5207b7SJohn Levon 		arg = get_param_num_from_sym(tmp->sym);
102*1f5207b7SJohn Levon 		if (arg < 0)
103*1f5207b7SJohn Levon 			continue;
104*1f5207b7SJohn Levon 		name = get_param_name(tmp);
105*1f5207b7SJohn Levon 		if (!name)
106*1f5207b7SJohn Levon 			continue;
107*1f5207b7SJohn Levon 		sql_insert_return_implies(DEREFERENCE, arg, name, "1");
108*1f5207b7SJohn Levon 	} END_FOR_EACH_SM(tmp);
109*1f5207b7SJohn Levon }
110*1f5207b7SJohn Levon 
match_pointer_as_array(struct expression * expr)111*1f5207b7SJohn Levon static void match_pointer_as_array(struct expression *expr)
112*1f5207b7SJohn Levon {
113*1f5207b7SJohn Levon 	if (!is_array(expr))
114*1f5207b7SJohn Levon 		return;
115*1f5207b7SJohn Levon 	check_deref(get_array_base(expr));
116*1f5207b7SJohn Levon }
117*1f5207b7SJohn Levon 
check_dereferences_param(int id)118*1f5207b7SJohn Levon void check_dereferences_param(int id)
119*1f5207b7SJohn Levon {
120*1f5207b7SJohn Levon 	my_id = id;
121*1f5207b7SJohn Levon 
122*1f5207b7SJohn Levon 	add_hook(&match_function_def, FUNC_DEF_HOOK);
123*1f5207b7SJohn Levon 
124*1f5207b7SJohn Levon 	add_hook(&match_dereference, DEREF_HOOK);
125*1f5207b7SJohn Levon 	add_hook(&match_pointer_as_array, OP_HOOK);
126*1f5207b7SJohn Levon 	select_return_implies_hook(DEREFERENCE, &set_param_dereferenced);
127*1f5207b7SJohn Levon 	add_modification_hook(my_id, &set_ignore);
128*1f5207b7SJohn Levon 
129*1f5207b7SJohn Levon 	all_return_states_hook(&process_states);
130*1f5207b7SJohn Levon }
131