2 * Copyright (C) 2013 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 * The plan here is to save all the possible values store to a given struct
22 * We will load all the values in to the function_type_val table first then
23 * run a script on that and load all the resulting values into the type_val
26 * So in this file we want to take the union of everything assigned to the
27 * struct member and insert it into the function_type_val at the end.
29 * You would think that we could use smatch_modification_hooks.c or
30 * extra_modification_hook() here to get the information here but in the end we
31 * need to code everything again a third time.
36 #include "smatch_slist.h"
37 #include "smatch_extra.h"
41 struct stree
*fn_type_val
;
42 struct stree
*global_type_val
;
44 static int get_vals(void *_db_vals
, int argc
, char **argv
, char **azColName
)
46 char **db_vals
= _db_vals
;
48 *db_vals
= alloc_string(argv
[0]);
53 struct expression
*expr
;
54 struct range_list
*rl
;
56 static struct expr_rl cached_results
[24];
59 static int get_cached(struct expression
*expr
, struct range_list
**rl
, int *ret
)
65 for (i
= 0; i
< ARRAY_SIZE(cached_results
); i
++) {
66 if (expr
== cached_results
[i
].expr
) {
67 if (cached_results
[i
].rl
) {
68 *rl
= clone_rl(cached_results
[i
].rl
);
78 int get_db_type_rl(struct expression
*expr
, struct range_list
**rl
)
82 struct range_list
*tmp
;
86 if (get_cached(expr
, rl
, &ret
))
89 member
= get_member_name(expr
);
93 res_idx
= (res_idx
+ 1) % ARRAY_SIZE(cached_results
);
94 cached_results
[res_idx
].expr
= expr
;
95 cached_results
[res_idx
].rl
= NULL
;
97 run_sql(get_vals
, &db_vals
,
98 "select value from type_value where type = '%s';", member
);
102 type
= get_type(expr
);
103 str_to_rl(type
, db_vals
, &tmp
);
104 free_string(db_vals
);
105 if (is_whole_rl(tmp
))
109 cached_results
[res_idx
].rl
= clone_rl(tmp
);
114 static void add_type_val(char *member
, struct range_list
*rl
)
116 struct smatch_state
*old
, *add
, *new;
118 member
= alloc_string(member
);
119 old
= get_state_stree(fn_type_val
, my_id
, member
, NULL
);
120 add
= alloc_estate_rl(rl
);
122 new = merge_estates(old
, add
);
125 set_state_stree(&fn_type_val
, my_id
, member
, NULL
, new);
128 static void add_fake_type_val(char *member
, struct range_list
*rl
, int ignore
)
130 struct smatch_state
*old
, *add
, *new;
132 member
= alloc_string(member
);
133 old
= get_state_stree(fn_type_val
, my_id
, member
, NULL
);
134 if (old
&& strcmp(old
->name
, "min-max") == 0)
136 if (ignore
&& old
&& strcmp(old
->name
, "ignore") == 0)
138 add
= alloc_estate_rl(rl
);
140 new = merge_estates(old
, add
);
144 new->name
= alloc_string("ignore");
146 new->name
= alloc_string("min-max");
148 set_state_stree(&fn_type_val
, my_id
, member
, NULL
, new);
151 static void add_global_type_val(char *member
, struct range_list
*rl
)
153 struct smatch_state
*old
, *add
, *new;
155 member
= alloc_string(member
);
156 old
= get_state_stree(global_type_val
, my_id
, member
, NULL
);
157 add
= alloc_estate_rl(rl
);
159 new = merge_estates(old
, add
);
162 new = clone_estate_perm(new);
163 set_state_stree_perm(&global_type_val
, my_id
, member
, NULL
, new);
166 static int has_link_cb(void *has_link
, int argc
, char **argv
, char **azColName
)
168 *(int *)has_link
= 1;
172 static int is_ignored_fake_assignment(void)
174 struct expression
*expr
;
179 expr
= get_faked_expression();
180 if (!expr
|| expr
->type
!= EXPR_ASSIGNMENT
)
182 if (!is_void_pointer(expr
->right
))
184 member_name
= get_member_name(expr
->right
);
188 type
= get_type(expr
->left
);
189 if (!type
|| type
->type
!= SYM_PTR
)
191 type
= get_real_base_type(type
);
192 if (!type
|| type
->type
!= SYM_STRUCT
)
195 run_sql(has_link_cb
, &has_link
,
196 "select * from data_info where type = %d and data = '%s' and value = '%s';",
197 TYPE_LINK
, member_name
, type_to_str(type
));
201 static int is_container_of(void)
203 /* We already check the macro name in is_ignored_macro() */
204 struct expression
*expr
;
207 expr
= get_faked_expression();
208 if (!expr
|| expr
->type
!= EXPR_ASSIGNMENT
)
211 offset
= get_offset_from_container_of(expr
->right
);
217 static bool is_driver_data(void)
219 static struct expression
*prev_expr
;
220 struct expression
*expr
;
222 static bool prev_ret
;
225 expr
= get_faked_expression();
226 if (!expr
|| expr
->type
!= EXPR_ASSIGNMENT
)
229 if (expr
== prev_expr
)
233 name
= expr_to_str(expr
->right
);
239 if (strstr(name
, "get_drvdata(") ||
240 strstr(name
, "dev.driver_data") ||
241 strstr(name
, "dev->driver_data"))
250 static int is_ignored_macro(void)
252 struct expression
*expr
;
255 expr
= get_faked_expression();
256 if (!expr
|| expr
->type
!= EXPR_ASSIGNMENT
|| expr
->op
!= '=')
258 name
= get_macro_name(expr
->right
->pos
);
261 if (strcmp(name
, "container_of") == 0)
263 if (strcmp(name
, "rb_entry") == 0)
265 if (strcmp(name
, "list_entry") == 0)
267 if (strcmp(name
, "list_first_entry") == 0)
269 if (strcmp(name
, "hlist_entry") == 0)
271 if (strcmp(name
, "per_cpu_ptr") == 0)
273 if (strcmp(name
, "raw_cpu_ptr") == 0)
275 if (strcmp(name
, "this_cpu_ptr") == 0)
278 if (strcmp(name
, "TRACE_EVENT") == 0)
280 if (strcmp(name
, "DECLARE_EVENT_CLASS") == 0)
282 if (strcmp(name
, "DEFINE_EVENT") == 0)
285 if (strstr(name
, "for_each"))
290 static int is_ignored_function(void)
292 struct expression
*expr
;
294 expr
= get_faked_expression();
295 if (!expr
|| expr
->type
!= EXPR_ASSIGNMENT
)
297 expr
= strip_expr(expr
->right
);
298 if (!expr
|| expr
->type
!= EXPR_CALL
|| expr
->fn
->type
!= EXPR_SYMBOL
)
301 if (sym_name_is("kmalloc", expr
->fn
))
303 if (sym_name_is("vmalloc", expr
->fn
))
305 if (sym_name_is("kvmalloc", expr
->fn
))
307 if (sym_name_is("kmalloc_array", expr
->fn
))
309 if (sym_name_is("vmalloc_array", expr
->fn
))
311 if (sym_name_is("kvmalloc_array", expr
->fn
))
314 if (sym_name_is("mmu_memory_cache_alloc", expr
->fn
))
316 if (sym_name_is("kmem_alloc", expr
->fn
))
318 if (sym_name_is("alloc_pages", expr
->fn
))
321 if (sym_name_is("netdev_priv", expr
->fn
))
323 if (sym_name_is("dev_get_drvdata", expr
->fn
))
325 if (sym_name_is("i2c_get_clientdata", expr
->fn
))
327 if (sym_name_is("idr_find", expr
->fn
))
333 static int is_uncasted_pointer_assign(void)
335 struct expression
*expr
;
336 struct symbol
*left_type
, *right_type
;
338 expr
= get_faked_expression();
341 if (expr
->type
== EXPR_PREOP
|| expr
->type
== EXPR_POSTOP
) {
342 if (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
)
345 if (expr
->type
!= EXPR_ASSIGNMENT
)
347 left_type
= get_type(expr
->left
);
348 right_type
= get_type(expr
->right
);
350 if (!left_type
|| !right_type
)
353 if (left_type
->type
== SYM_STRUCT
&& left_type
== right_type
)
356 if (left_type
->type
!= SYM_PTR
&&
357 left_type
->type
!= SYM_ARRAY
)
359 if (right_type
->type
!= SYM_PTR
&&
360 right_type
->type
!= SYM_ARRAY
)
362 left_type
= get_real_base_type(left_type
);
363 right_type
= get_real_base_type(right_type
);
365 if (left_type
== right_type
)
370 static int set_param_type(void *_type_str
, int argc
, char **argv
, char **azColName
)
372 char **type_str
= _type_str
;
373 static char type_buf
[128];
376 if (strcmp(*type_str
, argv
[0]) == 0)
378 strncpy(type_buf
, "unknown", sizeof(type_buf
));
381 strncpy(type_buf
, argv
[0], sizeof(type_buf
));
382 *type_str
= type_buf
;
387 static char *db_get_parameter_type(int param
)
394 run_sql(set_param_type
, &ret
,
395 "select value from fn_data_link where "
396 "file = '%s' and function = '%s' and static = %d and type = %d and parameter = %d and key = '$';",
397 (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
) ? get_base_file() : "extern",
398 cur_func_sym
->ident
->name
,
399 !!(cur_func_sym
->ctype
.modifiers
& MOD_STATIC
),
405 static int is_uncasted_fn_param_from_db(void)
407 struct expression
*expr
, *right
;
408 struct symbol
*left_type
;
409 char left_type_name
[128];
411 char *right_type_name
;
412 static struct expression
*prev_expr
;
415 expr
= get_faked_expression();
417 if (expr
== prev_expr
)
422 if (!expr
|| expr
->type
!= EXPR_ASSIGNMENT
)
424 left_type
= get_type(expr
->left
);
425 if (!left_type
|| left_type
->type
!= SYM_PTR
)
427 left_type
= get_real_base_type(left_type
);
428 if (!left_type
|| left_type
->type
!= SYM_STRUCT
)
430 snprintf(left_type_name
, sizeof(left_type_name
), "%s", type_to_str(left_type
));
432 right
= strip_expr(expr
->right
);
433 param
= get_param_num(right
);
436 right_type_name
= db_get_parameter_type(param
);
437 if (!right_type_name
)
440 if (strcmp(right_type_name
, left_type_name
) == 0) {
448 static void match_assign_value(struct expression
*expr
)
450 char *member
, *right_member
;
451 struct range_list
*rl
;
457 type
= get_type(expr
->left
);
458 if (type
&& type
->type
== SYM_STRUCT
)
460 member
= get_member_name(expr
->left
);
464 /* if we're saying foo->mtu = bar->mtu then that doesn't add information */
465 right_member
= get_member_name(expr
->right
);
466 if (right_member
&& strcmp(right_member
, member
) == 0)
469 if (is_fake_call(expr
->right
)) {
470 if (is_ignored_macro())
472 if (is_ignored_function())
474 if (is_uncasted_pointer_assign())
476 if (is_uncasted_fn_param_from_db())
478 if (is_container_of())
480 if (is_driver_data())
482 add_fake_type_val(member
, alloc_whole_rl(get_type(expr
->left
)), is_ignored_fake_assignment());
486 if (expr
->op
== '=') {
487 get_absolute_rl(expr
->right
, &rl
);
488 rl
= cast_rl(type
, rl
);
491 * This is a bit cheating. We order it so this will already be set
492 * by smatch_extra.c and we just look up the value.
494 get_absolute_rl(expr
->left
, &rl
);
496 add_type_val(member
, rl
);
498 free_string(right_member
);
503 * If we too: int *p = &my_struct->member then abandon all hope of tracking
506 static void match_assign_pointer(struct expression
*expr
)
508 struct expression
*right
;
510 struct range_list
*rl
;
513 right
= strip_expr(expr
->right
);
514 if (right
->type
!= EXPR_PREOP
|| right
->op
!= '&')
516 right
= strip_expr(right
->unop
);
518 member
= get_member_name(right
);
521 type
= get_type(right
);
522 rl
= alloc_whole_rl(type
);
523 add_type_val(member
, rl
);
527 static void match_global_assign(struct expression
*expr
)
530 struct range_list
*rl
;
533 type
= get_type(expr
->left
);
534 if (type
&& (type
->type
== SYM_ARRAY
|| type
->type
== SYM_STRUCT
))
536 member
= get_member_name(expr
->left
);
539 get_absolute_rl(expr
->right
, &rl
);
540 rl
= cast_rl(type
, rl
);
541 add_global_type_val(member
, rl
);
545 static void unop_expr(struct expression
*expr
)
547 struct range_list
*rl
;
550 if (expr
->op
!= SPECIAL_DECREMENT
&& expr
->op
!= SPECIAL_INCREMENT
)
553 expr
= strip_expr(expr
->unop
);
554 member
= get_member_name(expr
);
557 rl
= alloc_whole_rl(get_type(expr
));
558 add_type_val(member
, rl
);
562 static void asm_expr(struct statement
*stmt
)
564 struct asm_operand
*op
;
565 struct range_list
*rl
;
568 FOR_EACH_PTR(stmt
->asm_outputs
, op
) {
569 member
= get_member_name(op
->expr
);
572 rl
= alloc_whole_rl(get_type(op
->expr
));
573 add_type_val(member
, rl
);
575 } END_FOR_EACH_PTR(op
);
578 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
580 struct expression
*arg
;
582 struct range_list
*rl
;
585 if (strcmp(key
, "*$") != 0)
588 while (expr
->type
== EXPR_ASSIGNMENT
)
589 expr
= strip_expr(expr
->right
);
590 if (expr
->type
!= EXPR_CALL
)
593 arg
= get_argument_from_call_expr(expr
->args
, param
);
594 arg
= strip_expr(arg
);
597 type
= get_member_type_from_key(arg
, key
);
599 * The situation here is that say we memset() a void pointer to zero
600 * then that's returned to the called as "*$ = 0;" but on the caller's
601 * side it's not void, it's a struct.
603 * So the question is should we be passing that slightly bogus
604 * information back to the caller? Maybe, maybe not, but either way we
605 * are not going to record it here because a struct can't be zero.
608 if (type
&& type
->type
== SYM_STRUCT
)
611 if (arg
->type
!= EXPR_PREOP
|| arg
->op
!= '&')
613 arg
= strip_expr(arg
->unop
);
615 member
= get_member_name(arg
);
618 call_results_to_rl(expr
, type
, value
, &rl
);
619 add_type_val(member
, rl
);
623 static void match_end_func_info(struct symbol
*sym
)
627 FOR_EACH_SM(fn_type_val
, sm
) {
628 sql_insert_function_type_value(sm
->name
, sm
->state
->name
);
629 } END_FOR_EACH_SM(sm
);
632 void clear_type_value_cache(void)
634 memset(cached_results
, 0, sizeof(cached_results
));
637 static void match_after_func(struct symbol
*sym
)
639 free_stree(&fn_type_val
);
642 static void match_end_file(struct symbol_list
*sym_list
)
646 FOR_EACH_SM(global_type_val
, sm
) {
647 sql_insert_function_type_value(sm
->name
, sm
->state
->name
);
648 } END_FOR_EACH_SM(sm
);
651 void register_type_val(int id
)
658 add_hook(&match_assign_value
, ASSIGNMENT_HOOK_AFTER
);
659 add_hook(&match_assign_pointer
, ASSIGNMENT_HOOK
);
660 add_hook(&unop_expr
, OP_HOOK
);
661 add_hook(&asm_expr
, ASM_HOOK
);
662 select_return_states_hook(PARAM_ADD
, &db_param_add
);
663 select_return_states_hook(PARAM_SET
, &db_param_add
);
666 add_function_data((unsigned long *)&fn_type_val
);
668 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
669 add_hook(&match_after_func
, AFTER_FUNC_HOOK
);
671 add_hook(&match_global_assign
, GLOBAL_ASSIGNMENT_HOOK
);
672 add_hook(&match_end_file
, END_FILE_HOOK
);