unwind: paths where the parent is gone are counted as param_released
[smatch.git] / smatch_param_filter.c
blobfe6a90c555b2a281be8b202fe2731e22b75f84fd
1 /*
2 * Copyright (C) 2012 Oracle.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * This is for functions like:
21 * void foo(int *x)
22 * {
23 * if (*x == 42)
24 * *x = 0;
25 * }
27 * The final value of *x depends on the input to the function but with *x == 42
28 * filtered out.
32 #include "smatch.h"
33 #include "smatch_extra.h"
34 #include "smatch_slist.h"
36 static int my_id;
38 static struct smatch_state *unmatched_state(struct sm_state *sm)
40 struct smatch_state *state;
42 if (parent_is_gone_var_sym(sm->name, sm->sym))
43 return alloc_estate_empty();
45 state = __get_state(SMATCH_EXTRA, sm->name, sm->sym);
46 if (state)
47 return state;
48 return alloc_estate_whole(estate_type(sm->state));
51 static void pre_merge_hook(struct sm_state *cur, struct sm_state *other)
53 struct smatch_state *extra;
54 struct range_list *rl;
56 if (estate_rl(other->state))
57 return;
59 extra = get_state(SMATCH_EXTRA, cur->name, cur->sym);
60 if (!extra)
61 return;
63 rl = rl_intersection(estate_rl(extra), estate_rl(cur->state));
64 if (rl_equiv(rl, estate_rl(cur->state)))
65 return;
66 set_state(my_id, cur->name, cur->sym, alloc_estate_rl(clone_rl(rl)));
69 static void extra_mod_hook(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
71 struct symbol *param_sym;
72 char *param_name;
74 param_name = get_param_var_sym_var_sym(name, sym, NULL, &param_sym);
75 if (!param_name || !param_sym)
76 goto free;
77 if (get_param_num_from_sym(param_sym) < 0)
78 goto free;
80 /* on stack parameters are handled in smatch_param_limit.c */
81 if (param_sym->ident && strcmp(param_sym->ident->name, name) == 0)
82 return;
84 set_state(my_id, param_name, param_sym, alloc_estate_empty());
85 free:
86 free_string(param_name);
90 * This relies on the fact that these states are stored so that
91 * foo->bar is before foo->bar->baz.
93 static int parent_set(struct string_list *list, const char *name)
95 char *tmp;
96 int len;
97 int ret;
99 FOR_EACH_PTR(list, tmp) {
100 len = strlen(tmp);
101 ret = strncmp(tmp, name, len);
102 if (ret < 0)
103 continue;
104 if (ret > 0)
105 return 0;
106 if (name[len] == '-')
107 return 1;
108 } END_FOR_EACH_PTR(tmp);
110 return 0;
113 static void print_one_mod_param(int return_id, char *return_ranges,
114 int param, struct sm_state *sm, struct string_list **totally_filtered)
116 const char *param_name;
118 param_name = get_param_name(sm);
119 if (!param_name)
120 return;
121 if (is_whole_rl(estate_rl(sm->state)))
122 return;
123 if (!estate_rl(sm->state)) {
124 insert_string(totally_filtered, (char *)sm->name);
125 return;
128 if (is_ignored_kernel_data(param_name)) {
129 insert_string(totally_filtered, (char *)sm->name);
130 return;
133 sql_insert_return_states(return_id, return_ranges, PARAM_FILTER, param,
134 param_name, show_rl(estate_rl(sm->state)));
137 static void print_return_value_param(int return_id, char *return_ranges, struct expression *expr)
139 struct sm_state *tmp;
140 struct sm_state *sm;
141 struct string_list *totally_filtered = NULL;
142 int param;
144 FOR_EACH_MY_SM(SMATCH_EXTRA, __get_cur_stree(), tmp) {
145 param = get_param_num_from_sym(tmp->sym);
146 if (param < 0)
147 continue;
149 /* on stack parameters are handled in smatch_param_limit.c */
150 if (tmp->sym->ident && strcmp(tmp->sym->ident->name, tmp->name) == 0)
151 continue;
153 if (parent_set(totally_filtered, tmp->name))
154 continue;
156 sm = get_sm_state(my_id, tmp->name, tmp->sym);
157 if (sm)
158 print_one_mod_param(return_id, return_ranges, param, sm, &totally_filtered);
159 } END_FOR_EACH_SM(tmp);
161 free_ptr_list((struct ptr_list **)&totally_filtered);
164 int param_has_filter_data(struct sm_state *sm)
166 struct smatch_state *state;
168 state = get_state(my_id, sm->name, sm->sym);
169 if (!state) {
170 if (get_assigned_expr_name_sym(sm->name, sm->sym))
171 return 0;
172 return 1;
174 if (estate_rl(state))
175 return 1;
176 return 0;
179 void register_param_filter(int id)
181 my_id = id;
183 set_dynamic_states(my_id);
185 add_extra_mod_hook(&extra_mod_hook);
186 add_unmatched_state_hook(my_id, &unmatched_state);
187 add_pre_merge_hook(my_id, &pre_merge_hook);
188 add_merge_hook(my_id, &merge_estates);
190 add_split_return_callback(&print_return_value_param);