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:
30 * If we return 1 that means the value of *x has been set to 0. If we return
31 * 0 then we have left *x alone.
36 #include "smatch_slist.h"
37 #include "smatch_extra.h"
41 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
43 return alloc_estate_empty();
46 static int parent_is_set(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
48 struct expression
*faked
;
53 if (!__in_fake_assign
)
55 if (!is_whole_rl(estate_rl(state
)))
57 if (get_state(my_id
, name
, sym
))
60 faked
= get_faked_expression();
63 if ((faked
->type
== EXPR_PREOP
|| faked
->type
== EXPR_POSTOP
) &&
64 (faked
->op
== SPECIAL_INCREMENT
|| faked
->op
== SPECIAL_DECREMENT
)) {
65 faked
= strip_expr(faked
->unop
);
66 if (faked
->type
== EXPR_SYMBOL
)
70 if (faked
->type
!= EXPR_ASSIGNMENT
)
73 left_name
= expr_to_var(faked
->left
);
77 len
= strlen(left_name
);
78 if (strncmp(name
, left_name
, len
) == 0 && name
[len
] == '-')
80 free_string(left_name
);
85 static bool is_probably_worthless(struct expression
*expr
)
87 struct expression
*faked
;
89 if (!__in_fake_struct_assign
)
92 faked
= get_faked_expression();
93 if (!faked
|| faked
->type
!= EXPR_ASSIGNMENT
)
96 if (faked
->left
->type
== EXPR_PREOP
&&
97 faked
->left
->op
== '*')
103 static bool name_is_sym_name(const char *name
, struct symbol
*sym
)
105 if (!name
|| !sym
|| !sym
->ident
)
108 return strcmp(name
, sym
->ident
->name
) == 0;
111 static void extra_mod_hook(const char *name
, struct symbol
*sym
, struct expression
*expr
, struct smatch_state
*state
)
113 struct symbol
*param_sym
;
117 if (expr
&& expr
->smatch_flags
& Fake
)
120 if (is_probably_worthless(expr
))
123 type
= get_type(expr
);
124 if (type
&& (type
->type
== SYM_STRUCT
|| type
->type
== SYM_UNION
))
127 if (name_is_sym_name(name
, sym
))
130 param_name
= get_param_var_sym_var_sym(name
, sym
, NULL
, ¶m_sym
);
131 if (!param_name
|| !param_sym
)
133 if (get_param_num_from_sym(param_sym
) < 0)
135 if (parent_is_set(param_name
, param_sym
, state
))
138 if (get_state(my_id
, param_name
, param_sym
))
144 set_state(my_id
, param_name
, param_sym
, state
);
146 free_string(param_name
);
150 * This function is is a dirty hack because extra_mod_hook is giving us a NULL
151 * sym instead of a vsl.
153 static void match_array_assignment(struct expression
*expr
)
155 struct expression
*array
, *offset
;
158 struct range_list
*rl
;
162 if (__in_fake_assign
)
165 if (!is_array(expr
->left
))
167 array
= get_array_base(expr
->left
);
168 offset
= get_array_offset(expr
->left
);
170 /* These are handled by extra_mod_hook() */
171 if (get_value(offset
, &sval
))
173 name
= expr_to_var_sym(array
, &sym
);
176 if (map_to_param(name
, sym
) < 0)
178 get_absolute_rl(expr
->right
, &rl
);
179 rl
= cast_rl(get_type(expr
->left
), rl
);
181 snprintf(buf
, sizeof(buf
), "*%s", name
);
182 set_state(my_id
, buf
, sym
, alloc_estate_rl(rl
));
187 static char *get_two_dots(const char *name
)
192 for (i
= 0; i
< sizeof(buf
); i
++) {
193 if (name
[i
] == '.') {
206 * This relies on the fact that these states are stored so that
207 * foo->bar is before foo->bar->baz.
209 static int parent_set(struct string_list
*list
, const char *param_name
, struct sm_state
*sm
)
215 if (strncmp(param_name
, "(*$)->", 6) == 0 && sm
->sym
&& sm
->sym
->ident
) {
218 snprintf(buf
, sizeof(buf
), "*%s", sm
->sym
->ident
->name
);
219 if (get_state(my_id
, buf
, sm
->sym
))
223 FOR_EACH_PTR(list
, tmp
) {
225 ret
= strncmp(tmp
, sm
->name
, len
);
230 if (sm
->name
[len
] == '-')
232 } END_FOR_EACH_PTR(tmp
);
237 static void print_return_value_param_helper(int return_id
, char *return_ranges
, struct expression
*expr
, int limit
)
240 struct smatch_state
*extra
;
242 struct range_list
*rl
;
243 const char *param_name
;
244 struct string_list
*set_list
= NULL
;
247 char two_dot
[80] = "";
250 __promote_sets_to_clears(return_id
, return_ranges
, expr
);
252 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), sm
) {
253 bool untracked
= false;
255 if (!estate_rl(sm
->state
))
257 extra
= __get_state(SMATCH_EXTRA
, sm
->name
, sm
->sym
);
259 rl
= rl_intersection(estate_rl(sm
->state
), estate_rl(extra
));
263 rl
= estate_rl(sm
->state
);
266 param
= get_param_key_from_sm(sm
, NULL
, ¶m_name
);
267 if (param
< 0 || !param_name
)
269 if (param_name
[0] == '&')
271 if (strcmp(param_name
, "$") == 0 ||
272 is_recursive_member(param_name
) ||
273 is_ignored_kernel_data(param_name
)) {
274 insert_string(&set_list
, (char *)sm
->name
);
278 if (parent_was_PARAM_CLEAR(sm
->name
, sm
->sym
))
281 sql_insert_return_states(return_id
, return_ranges
,
282 UNTRACKED_PARAM
, param
, param_name
, "");
287 char *new = get_two_dots(param_name
);
289 /* no useful information here. */
290 if (is_whole_rl(rl
) && parent_set(set_list
, param_name
, sm
))
294 if (strcmp(new, two_dot
) == 0)
297 strncpy(two_dot
, new, sizeof(two_dot
));
298 insert_string(&set_list
, (char *)sm
->name
);
299 sql_insert_return_states(return_id
, return_ranges
,
300 PARAM_SET
, param
, new, "s64min-s64max");
305 math_str
= get_value_in_terms_of_parameter_math_var_sym(sm
->name
, sm
->sym
);
306 if (math_str
&& strcmp(show_rl(rl
), math_str
) != 0) {
307 snprintf(buf
, sizeof(buf
), "%s[%s]", show_rl(rl
), math_str
);
308 insert_string(&set_list
, (char *)sm
->name
);
309 sql_insert_return_states(return_id
, return_ranges
,
310 param_has_filter_data(sm
) ? PARAM_ADD
: PARAM_SET
,
311 param
, param_name
, buf
);
315 /* no useful information here. */
316 if (is_whole_rl(rl
) && parent_set(set_list
, param_name
, sm
))
318 if (is_whole_rl(rl
) && parent_was_PARAM_CLEAR(sm
->name
, sm
->sym
))
320 if (rl_is_zero(rl
) && parent_was_PARAM_CLEAR_ZERO(sm
->name
, sm
->sym
))
323 insert_string(&set_list
, (char *)sm
->name
);
325 sql_insert_return_states(return_id
, return_ranges
,
326 param_has_filter_data(sm
) ? PARAM_ADD
: PARAM_SET
,
327 param
, param_name
, show_rl(rl
));
328 if (limit
&& ++count
> limit
)
331 } END_FOR_EACH_SM(sm
);
333 free_ptr_list((struct ptr_list
**)&set_list
);
336 static void print_return_value_param(int return_id
, char *return_ranges
, struct expression
*expr
)
338 print_return_value_param_helper(return_id
, return_ranges
, expr
, 0);
341 void print_limited_param_set(int return_id
, char *return_ranges
, struct expression
*expr
)
343 print_return_value_param_helper(return_id
, return_ranges
, expr
, 1000);
346 static int possibly_empty(struct sm_state
*sm
)
348 struct sm_state
*tmp
;
350 FOR_EACH_PTR(sm
->possible
, tmp
) {
351 if (strcmp(tmp
->name
, "") == 0)
353 } END_FOR_EACH_PTR(tmp
);
357 static bool sym_was_set(struct symbol
*sym
)
361 if (!sym
|| !sym
->ident
)
364 snprintf(buf
, sizeof(buf
), "%s orig", sym
->ident
->name
);
365 if (get_comparison_strings(sym
->ident
->name
, buf
) == SPECIAL_EQUAL
)
371 int param_was_set_var_sym(const char *name
, struct symbol
*sym
)
373 struct symbol
*param_sym
;
374 const char *param_name
;
379 param_name
= get_param_var_sym_var_sym(name
, sym
, NULL
, ¶m_sym
);
380 if (param_name
&& param_sym
) {
391 if (sym_was_set(sym
))
395 if (len
>= sizeof(buf
))
396 len
= sizeof(buf
) - 1;
398 for (i
= 0; i
<= len
; i
++) {
399 if (name
[i
] != '-' && name
[i
] != '\0')
402 memcpy(buf
, name
, i
);
405 sm
= get_sm_state(my_id
, buf
, sym
);
408 if (possibly_empty(sm
))
414 return param_was_set_var_sym(name
+ 1, sym
);
419 static struct expression
*get_unfaked_expr(struct expression
*expr
)
421 struct expression
*tmp
;
423 if (!is_fake_var(expr
))
425 tmp
= expr_get_fake_parent_expr(expr
);
426 if (!tmp
|| tmp
->type
!= EXPR_ASSIGNMENT
)
431 int param_was_set(struct expression
*expr
)
437 expr
= get_unfaked_expr(expr
);
439 name
= expr_to_var_sym(expr
, &sym
);
443 ret
= param_was_set_var_sym(name
, sym
);
449 static void match_ignored(struct expression
*expr
)
451 struct smatch_state
*state
= alloc_estate_whole(get_type(expr
));
453 set_extra_expr_mod(expr
, state
);
456 static void register_ignored_params_from_file(void)
464 * Eventually I realized that we're ignoring uninitialized variables
465 * from these functions because in practice they're always set. I
466 * thought about tracking them as PARAM_LOST, but that's not really
467 * accurate, they're just set to unknown.
471 snprintf(name
, 256, "%s.ignore_uninitialized_param", option_project_str
);
473 token
= get_tokens_file(name
);
476 if (token_type(token
) != TOKEN_STREAMBEGIN
)
479 while (token_type(token
) != TOKEN_STREAMEND
) {
480 if (token_type(token
) != TOKEN_IDENT
)
482 func
= show_ident(token
->ident
);
485 if (token_type(token
) != TOKEN_NUMBER
)
487 param
= atoi(token
->number
);
489 add_param_key_expr_hook(func
, match_ignored
, param
, "*$", NULL
);
496 void register_param_set(int id
)
500 set_dynamic_states(my_id
);
501 add_extra_mod_hook(&extra_mod_hook
);
502 add_hook(match_array_assignment
, ASSIGNMENT_HOOK
);
503 add_unmatched_state_hook(my_id
, &unmatched_state
);
504 add_merge_hook(my_id
, &merge_estates
);
505 add_split_return_callback(&print_return_value_param
);
506 register_ignored_params_from_file();