2 * Copyright (C) 2010 Dan Carpenter.
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
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
27 static sqlite3
*mem_db
;
31 #define sql_insert(table, values...) \
37 char *err, *p = buf; \
40 p += snprintf(p, buf + sizeof(buf) - p, \
41 "insert into %s values (", #table); \
42 p += snprintf(p, buf + sizeof(buf) - p, values); \
43 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
44 sm_debug("in-mem: %s\n", buf); \
45 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err); \
46 if (rc != SQLITE_OK) { \
47 fprintf(stderr, "SQL error #2: %s\n", err); \
48 fprintf(stderr, "SQL: '%s'\n", buf); \
54 sm_printf("SQL: insert into " #table " values (" values); \
61 void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
);
63 ALLOCATOR(def_callback
, "definition db hook callbacks");
64 DECLARE_PTR_LIST(callback_list
, struct def_callback
);
65 static struct callback_list
*callbacks
;
67 struct member_info_callback
{
69 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
);
71 ALLOCATOR(member_info_callback
, "caller_info callbacks");
72 DECLARE_PTR_LIST(member_info_cb_list
, struct member_info_callback
);
73 static struct member_info_cb_list
*member_callbacks
;
75 struct returned_state_callback
{
76 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*return_expr
);
78 ALLOCATOR(returned_state_callback
, "returned state callbacks");
79 DECLARE_PTR_LIST(returned_state_cb_list
, struct returned_state_callback
);
80 static struct returned_state_cb_list
*returned_state_callbacks
;
82 struct returned_member_callback
{
84 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*expr
, char *printed_name
, struct smatch_state
*state
);
86 ALLOCATOR(returned_member_callback
, "returned member callbacks");
87 DECLARE_PTR_LIST(returned_member_cb_list
, struct returned_member_callback
);
88 static struct returned_member_cb_list
*returned_member_callbacks
;
90 struct call_implies_callback
{
92 void (*callback
)(struct expression
*arg
, char *key
, char *value
);
94 ALLOCATOR(call_implies_callback
, "call_implies callbacks");
95 DECLARE_PTR_LIST(call_implies_cb_list
, struct call_implies_callback
);
96 static struct call_implies_cb_list
*call_implies_cb_list
;
98 static int print_sql_output(void *unused
, int argc
, char **argv
, char **azColName
)
102 for (i
= 0; i
< argc
; i
++) {
105 sm_printf("%s", argv
[i
]);
111 void debug_sql(const char *sql
)
116 sql_exec(print_sql_output
, NULL
, sql
);
119 void debug_mem_sql(const char *sql
)
124 sql_mem_exec(print_sql_output
, NULL
, sql
);
127 void sql_exec(int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
)
132 if (option_no_db
|| !db
)
135 rc
= sqlite3_exec(db
, sql
, callback
, data
, &err
);
136 if (rc
!= SQLITE_OK
) {
137 fprintf(stderr
, "SQL error #2: %s\n", err
);
138 fprintf(stderr
, "SQL: '%s'\n", sql
);
142 void sql_mem_exec(int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
)
150 rc
= sqlite3_exec(mem_db
, sql
, callback
, data
, &err
);
151 if (rc
!= SQLITE_OK
) {
152 fprintf(stderr
, "SQL error #2: %s\n", err
);
153 fprintf(stderr
, "SQL: '%s'\n", sql
);
157 void sql_insert_return_states(int return_id
, const char *return_ranges
,
158 int type
, int param
, const char *key
, const char *value
)
160 if (key
&& strlen(key
) >= 80)
162 sql_insert(return_states
, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
163 get_base_file(), get_function(), (unsigned long)__inline_fn
,
164 return_id
, return_ranges
, fn_static(), type
, param
, key
, value
);
167 static struct string_list
*common_funcs
;
168 static int is_common_function(const char *fn
)
172 if (strncmp(fn
, "__builtin_", 10) == 0)
175 FOR_EACH_PTR(common_funcs
, tmp
) {
176 if (strcmp(tmp
, fn
) == 0)
178 } END_FOR_EACH_PTR(tmp
);
183 void sql_insert_caller_info(struct expression
*call
, int type
,
184 int param
, const char *key
, const char *value
)
188 if (!option_info
&& !__inline_call
)
191 if (key
&& strlen(key
) >= 80)
194 fn
= get_fnptr_name(call
->fn
);
200 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
201 get_base_file(), get_function(), fn
, (unsigned long)call
,
202 is_static(call
->fn
), type
, param
, key
, value
);
208 if (is_common_function(fn
))
211 sm_msg("SQL_caller_info: insert into caller_info values ("
212 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
213 get_base_file(), get_function(), fn
, is_static(call
->fn
),
214 type
, param
, key
, value
);
219 void sql_insert_function_ptr(const char *fn
, const char *struct_name
)
221 sql_insert(function_ptr
, "'%s', '%s', '%s', 0", get_base_file(), fn
,
225 void sql_insert_call_implies(int type
, int param
, const char *key
, const char *value
)
227 sql_insert(call_implies
, "'%s', '%s', %lu, %d, %d, %d, '%s', %s", get_base_file(),
228 get_function(), (unsigned long)__inline_fn
, fn_static(),
229 type
, param
, key
, value
);
232 void sql_insert_function_type_size(const char *member
, const char *ranges
)
234 sql_insert(function_type_size
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member
, ranges
);
237 void sql_insert_local_values(const char *name
, const char *value
)
239 sql_insert(local_values
, "'%s', '%s', '%s'", get_base_file(), name
, value
);
242 void sql_insert_function_type_value(const char *type
, const char *value
)
244 sql_insert(function_type_value
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type
, value
);
247 void sql_insert_function_type_info(int param
, const char *value
)
249 sql_insert(function_type_info
, "'%s', '%s', %d, %d, '%s'",
250 get_base_file(), get_function(), fn_static(), param
, value
);
253 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
)
257 data_name
= get_data_info_name(data
);
260 sql_insert(data_info
, "'%s', '%s', %d, '%s'", get_base_file(), data_name
, type
, value
);
263 char *get_static_filter(struct symbol
*sym
)
265 static char sql_filter
[1024];
267 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
268 snprintf(sql_filter
, sizeof(sql_filter
),
269 "file = '%s' and function = '%s' and static = '1'",
270 get_base_file(), sym
->ident
->name
);
272 snprintf(sql_filter
, sizeof(sql_filter
),
273 "function = '%s' and static = '0'", sym
->ident
->name
);
279 static int row_count
;
280 static int get_row_count(void *unused
, int argc
, char **argv
, char **azColName
)
284 row_count
= atoi(argv
[0]);
288 static void sql_select_return_states_pointer(const char *cols
,
289 struct expression
*call
, int (*callback
)(void*, int, char**, char**), void *info
)
293 ptr
= get_fnptr_name(call
->fn
);
297 run_sql(callback
, info
,
298 "select %s from return_states join function_ptr where "
299 "return_states.function == function_ptr.function and ptr = '%s'"
300 "and searchable = 1 order by return_id, type;",
304 static int is_local_symbol(struct expression
*expr
)
306 if (expr
->type
!= EXPR_SYMBOL
)
308 if (expr
->symbol
->ctype
.modifiers
& (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
))
313 void sql_select_return_states(const char *cols
, struct expression
*call
,
314 int (*callback
)(void*, int, char**, char**), void *info
)
316 if (is_fake_call(call
))
319 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
|| is_local_symbol(call
->fn
)) {
320 sql_select_return_states_pointer(cols
, call
, callback
, info
);
324 if (inlinable(call
->fn
)) {
325 mem_sql(callback
, info
,
326 "select %s from return_states where call_id = '%lu' order by return_id, type;",
327 cols
, (unsigned long)call
);
332 run_sql(get_row_count
, info
, "select count(*) from return_states where %s;",
333 get_static_filter(call
->fn
->symbol
));
334 if (row_count
> 3000)
337 run_sql(callback
, info
, "select %s from return_states where %s order by return_id, type;",
338 cols
, get_static_filter(call
->fn
->symbol
));
341 void sql_select_call_implies(const char *cols
, struct expression
*call
,
342 int (*callback
)(void*, int, char**, char**))
344 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
347 if (inlinable(call
->fn
)) {
348 mem_sql(callback
, NULL
,
349 "select %s from call_implies where call_id = '%lu';",
350 cols
, (unsigned long)call
);
354 run_sql(callback
, NULL
, "select %s from call_implies where %s;",
355 cols
, get_static_filter(call
->fn
->symbol
));
358 void sql_select_caller_info(const char *cols
, struct symbol
*sym
,
359 int (*callback
)(void*, int, char**, char**))
362 mem_sql(callback
, NULL
,
363 "select %s from caller_info where call_id = %lu;",
364 cols
, (unsigned long)__inline_fn
);
368 run_sql(callback
, NULL
,
369 "select %s from caller_info where %s order by call_id;",
370 cols
, get_static_filter(sym
));
373 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
375 struct def_callback
*def_callback
= __alloc_def_callback(0);
377 def_callback
->hook_type
= type
;
378 def_callback
->callback
= callback
;
379 add_ptr_list(&callbacks
, def_callback
);
383 * These call backs are used when the --info option is turned on to print struct
384 * member information. For example foo->bar could have a state in
385 * smatch_extra.c and also check_user.c.
387 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
389 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
391 member_callback
->owner
= owner
;
392 member_callback
->callback
= callback
;
393 add_ptr_list(&member_callbacks
, member_callback
);
396 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
398 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
400 callback
->callback
= fn
;
401 add_ptr_list(&returned_state_callbacks
, callback
);
404 void add_returned_member_callback(int owner
, void (*callback
)(int return_id
, char *return_ranges
, struct expression
*expr
, char *printed_name
, struct smatch_state
*state
))
406 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
408 member_callback
->owner
= owner
;
409 member_callback
->callback
= callback
;
410 add_ptr_list(&returned_member_callbacks
, member_callback
);
413 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*arg
, char *key
, char *value
))
415 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
418 cb
->callback
= callback
;
419 add_ptr_list(&call_implies_cb_list
, cb
);
422 static struct expression
*static_call_expr
;
423 static struct expression
*static_returns_call
;
424 static struct symbol
*return_type
;
425 static struct range_list
*return_range_list
;
426 static int db_return_callback(void *unused
, int argc
, char **argv
, char **azColName
)
428 struct range_list
*rl
;
429 struct expression
*call_expr
= static_returns_call
;
433 call_results_to_rl(call_expr
, return_type
, argv
[0], &rl
);
434 return_range_list
= rl_union(return_range_list
, rl
);
438 struct range_list
*db_return_vals(struct expression
*expr
)
443 if (is_fake_call(expr
))
446 snprintf(buf
, sizeof(buf
), "return %p", expr
);
447 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
449 return clone_rl(estate_rl(sm
->state
));
450 static_returns_call
= expr
;
451 return_type
= get_type(expr
);
455 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
458 return_range_list
= NULL
;
459 if (inlinable(expr
->fn
)) {
460 mem_sql(db_return_callback
, NULL
,
461 "select distinct return from return_states where call_id = '%lu';",
462 (unsigned long)expr
);
464 run_sql(db_return_callback
, NULL
,
465 "select distinct return from return_states where %s;",
466 get_static_filter(expr
->fn
->symbol
));
468 return return_range_list
;
471 static void match_call_marker(struct expression
*expr
)
474 * we just want to record something in the database so that if we have
475 * two calls like: frob(4); frob(some_unkown); then on the receiving
476 * side we know that sometimes frob is called with unknown parameters.
479 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", "");
482 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct stree
*stree
,
483 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
489 char printed_name
[256];
492 expr
= strip_expr(expr
);
493 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
494 expr
= strip_expr(expr
->unop
);
498 name
= expr_to_var_sym(expr
, &sym
);
503 FOR_EACH_SM(stree
, sm
) {
506 if (strcmp(name
, sm
->name
) == 0) {
508 snprintf(printed_name
, sizeof(printed_name
), "*$");
509 else /* these are already handled. fixme: handle them here */
511 } else if (sm
->name
[0] == '*' && strcmp(name
, sm
->name
+ 1) == 0) {
512 snprintf(printed_name
, sizeof(printed_name
), "*$");
513 } else if (strncmp(name
, sm
->name
, len
) == 0) {
515 snprintf(printed_name
, sizeof(printed_name
), "$->%s", sm
->name
+ len
+ 1);
517 snprintf(printed_name
, sizeof(printed_name
), "$%s", sm
->name
+ len
);
521 callback(call
, param
, printed_name
, sm
);
522 } END_FOR_EACH_SM(sm
);
527 static void match_call_info(struct expression
*call
)
529 struct member_info_callback
*cb
;
530 struct expression
*arg
;
535 name
= get_fnptr_name(call
->fn
);
539 FOR_EACH_PTR(member_callbacks
, cb
) {
540 stree
= get_all_states_stree(cb
->owner
);
542 FOR_EACH_PTR(call
->args
, arg
) {
543 print_struct_members(call
, arg
, i
, stree
, cb
->callback
);
545 } END_FOR_EACH_PTR(arg
);
547 } END_FOR_EACH_PTR(cb
);
552 static int get_param(int param
, char **name
, struct symbol
**sym
)
558 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
560 * this is a temporary hack to work around a bug (I think in sparse?)
561 * 2.6.37-rc1:fs/reiserfs/journal.o
562 * If there is a function definition without parameter name found
563 * after a function implementation then it causes a crash.
567 if (arg
->ident
->name
< (char *)100)
570 *name
= arg
->ident
->name
;
575 } END_FOR_EACH_PTR(arg
);
580 static struct stree
*final_states
;
581 static int prev_func_id
= -1;
582 static int caller_info_callback(void *unused
, int argc
, char **argv
, char **azColName
)
590 struct symbol
*sym
= NULL
;
591 struct def_callback
*def_callback
;
597 func_id
= atoi(argv
[0]);
599 type
= strtol(argv
[1], NULL
, 10);
600 param
= strtol(argv
[2], NULL
, 10);
607 if (prev_func_id
== -1)
608 prev_func_id
= func_id
;
609 if (func_id
!= prev_func_id
) {
610 stree
= __pop_fake_cur_stree();
611 merge_stree(&final_states
, stree
);
613 __push_fake_cur_stree();
615 prev_func_id
= func_id
;
618 if (param
>= 0 && !get_param(param
, &name
, &sym
))
621 FOR_EACH_PTR(callbacks
, def_callback
) {
622 if (def_callback
->hook_type
== type
)
623 def_callback
->callback(name
, sym
, key
, value
);
624 } END_FOR_EACH_PTR(def_callback
);
629 static void get_direct_callers(struct symbol
*sym
)
631 sql_select_caller_info("call_id, type, parameter, key, value", sym
,
632 caller_info_callback
);
635 static struct string_list
*ptr_names_done
;
636 static struct string_list
*ptr_names
;
638 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
640 insert_string(&ptr_names
, alloc_string(argv
[0]));
644 static char *get_next_ptr_name(void)
648 FOR_EACH_PTR(ptr_names
, ptr
) {
649 if (list_has_string(ptr_names_done
, ptr
))
651 insert_string(&ptr_names_done
, ptr
);
653 } END_FOR_EACH_PTR(ptr
);
657 static void get_ptr_names(const char *file
, const char *name
)
659 char sql_filter
[1024];
663 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
666 snprintf(sql_filter
, 1024, "function = '%s';", name
);
669 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
671 run_sql(get_ptr_name
, NULL
,
672 "select distinct ptr from function_ptr where %s",
675 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
679 while ((name
= get_next_ptr_name()))
680 get_ptr_names(NULL
, name
);
683 static void match_data_from_db(struct symbol
*sym
)
688 if (!sym
|| !sym
->ident
)
691 __push_fake_cur_stree();
698 if (sym
->ctype
.modifiers
& MOD_STATIC
)
699 get_ptr_names(get_base_file(), sym
->ident
->name
);
701 get_ptr_names(NULL
, sym
->ident
->name
);
703 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
704 __free_ptr_list((struct ptr_list
**)&ptr_names
);
705 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
706 stree
= __pop_fake_cur_stree();
711 get_direct_callers(sym
);
713 FOR_EACH_PTR(ptr_names
, ptr
) {
714 run_sql(caller_info_callback
, NULL
,
715 "select call_id, type, parameter, key, value"
716 " from caller_info where function = '%s' order by call_id",
719 } END_FOR_EACH_PTR(ptr
);
721 __free_ptr_list((struct ptr_list
**)&ptr_names
);
722 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
724 get_direct_callers(sym
);
727 stree
= __pop_fake_cur_stree();
728 merge_stree(&final_states
, stree
);
731 FOR_EACH_SM(final_states
, sm
) {
733 } END_FOR_EACH_SM(sm
);
735 free_stree(&final_states
);
738 static int call_implies_callbacks(void *unused
, int argc
, char **argv
, char **azColName
)
740 struct expression
*call_expr
= static_call_expr
;
741 struct call_implies_callback
*cb
;
742 struct expression
*arg
= NULL
;
749 type
= atoi(argv
[1]);
750 param
= atoi(argv
[2]);
752 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
753 if (cb
->type
!= type
)
756 arg
= get_argument_from_call_expr(call_expr
->args
, param
);
760 cb
->callback(arg
, argv
[3], argv
[4]);
761 } END_FOR_EACH_PTR(cb
);
766 static void match_call_implies(struct expression
*expr
)
768 static_call_expr
= expr
;
769 sql_select_call_implies("function, type, parameter, key, value", expr
,
770 call_implies_callbacks
);
774 static void print_initializer_list(struct expression_list
*expr_list
,
775 struct symbol
*struct_type
)
777 struct expression
*expr
;
778 struct symbol
*base_type
;
779 char struct_name
[256];
781 FOR_EACH_PTR(expr_list
, expr
) {
782 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
783 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
786 if (expr
->type
!= EXPR_IDENTIFIER
)
788 if (!expr
->expr_ident
)
790 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
792 base_type
= get_type(expr
->ident_expression
);
793 if (!base_type
|| base_type
->type
!= SYM_FN
)
795 snprintf(struct_name
, sizeof(struct_name
), "(struct %s)->%s",
796 struct_type
->ident
->name
, expr
->expr_ident
->name
);
797 sql_insert_function_ptr(expr
->ident_expression
->symbol_name
->name
,
799 } END_FOR_EACH_PTR(expr
);
802 static void global_variable(struct symbol
*sym
)
804 struct symbol
*struct_type
;
808 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
810 struct_type
= get_base_type(sym
);
813 if (struct_type
->type
== SYM_ARRAY
) {
814 struct_type
= get_base_type(struct_type
);
818 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
820 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
823 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
825 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", "");
828 static void call_return_state_hooks_conditional(struct expression
*expr
)
830 struct returned_state_callback
*cb
;
831 struct range_list
*rl
;
833 int final_pass_orig
= final_pass
;
835 __push_fake_cur_stree();
838 __split_whole_condition(expr
->conditional
);
839 final_pass
= final_pass_orig
;
841 if (get_implied_rl(expr
->cond_true
, &rl
))
842 rl
= cast_rl(cur_func_return_type(), rl
);
844 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_true
)));
845 return_ranges
= show_rl(rl
);
848 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
849 cb
->callback(return_id
, return_ranges
, expr
);
850 } END_FOR_EACH_PTR(cb
);
852 __push_true_states();
853 __use_false_states();
855 if (get_implied_rl(expr
->cond_false
, &rl
))
856 rl
= cast_rl(cur_func_return_type(), rl
);
858 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_false
)));
859 return_ranges
= show_rl(rl
);
862 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
863 cb
->callback(return_id
, return_ranges
, expr
);
864 } END_FOR_EACH_PTR(cb
);
866 __merge_true_states();
867 __free_fake_cur_stree();
870 static void call_return_state_hooks_compare(struct expression
*expr
)
872 struct returned_state_callback
*cb
;
874 int final_pass_orig
= final_pass
;
876 __push_fake_cur_stree();
879 __split_whole_condition(expr
);
880 final_pass
= final_pass_orig
;
882 return_ranges
= alloc_sname("1");
885 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
886 cb
->callback(return_id
, return_ranges
, expr
);
887 } END_FOR_EACH_PTR(cb
);
889 __push_true_states();
890 __use_false_states();
892 return_ranges
= alloc_sname("0");;
894 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
895 cb
->callback(return_id
, return_ranges
, expr
);
896 } END_FOR_EACH_PTR(cb
);
898 __merge_true_states();
899 __free_fake_cur_stree();
902 static int split_helper(struct sm_state
*sm
, struct expression
*expr
)
904 struct returned_state_callback
*cb
;
905 struct range_list
*rl
;
907 struct sm_state
*tmp
;
909 int nr_possible
, nr_states
;
912 struct stree
*orig_stree
;
913 struct sm_state
*orig_sm
;
915 if (!sm
|| !sm
->merged
)
918 if (too_many_possible(sm
))
921 /* bail if it gets too complicated */
922 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
923 nr_states
= stree_count(__get_cur_stree());
925 * the main thing option_info because we don't want to print a
926 * million lines of output. If someone else, like check_locking.c
927 * wants this data, then it doesn't cause a slow down to provide it.
929 if (option_info
&& nr_states
* nr_possible
>= 2000)
932 compare_str
= expr_lte_to_param(expr
, -1);
934 orig_stree
= clone_stree(__get_cur_stree());
935 FOR_EACH_PTR(sm
->possible
, tmp
) {
940 __push_fake_cur_stree();
942 FOR_EACH_SM(orig_stree
, orig_sm
) {
944 } END_FOR_EACH_SM(orig_sm
);
946 overwrite_states_using_pool(tmp
);
948 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
949 return_ranges
= show_rl(rl
);
951 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
952 return_ranges
= alloc_sname(buf
);
956 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
957 cb
->callback(return_id
, return_ranges
, expr
);
958 } END_FOR_EACH_PTR(cb
);
960 __free_fake_cur_stree();
961 } END_FOR_EACH_PTR(tmp
);
963 free_stree(&orig_stree
);
967 static const char *get_return_ranges_str(struct expression
*expr
)
969 struct range_list
*rl
;
977 return alloc_sname("");
979 if (get_implied_value(expr
, &sval
))
980 return sval_to_str(sval
);
982 compare_str
= expr_equal_to_param(expr
, -1);
983 math_str
= get_value_in_terms_of_parameter_math(expr
);
985 if (get_implied_rl(expr
, &rl
)) {
986 rl
= cast_rl(cur_func_return_type(), rl
);
987 return_ranges
= show_rl(rl
);
989 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
)));
990 return_ranges
= show_rl(rl
);
994 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
995 return alloc_sname(buf
);
999 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1000 return alloc_sname(buf
);
1003 compare_str
= expr_lte_to_param(expr
, -1);
1005 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1006 return alloc_sname(buf
);
1008 return return_ranges
;
1011 static int split_positive_from_negative(struct expression
*expr
)
1013 struct returned_state_callback
*cb
;
1014 struct range_list
*rl
;
1015 const char *return_ranges
;
1018 /* We're going to print the states 3 times */
1019 if (stree_count(__get_cur_stree()) > 10000 / 3)
1022 if (!get_implied_rl(expr
, &rl
) || !rl
)
1024 if (is_whole_rl(rl
) || is_whole_rl_non_zero(rl
))
1026 /* Forget about INT_MAX and larger */
1027 if (rl_max(rl
).value
<= 0)
1029 if (!sval_is_negative(rl_min(rl
)))
1032 if (!assume(compare_expression(expr
, '>', zero_expr())))
1036 return_ranges
= get_return_ranges_str(expr
);
1037 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1038 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1039 } END_FOR_EACH_PTR(cb
);
1043 if (rl_has_sval(rl
, sval_type_val(rl_type(rl
), 0))) {
1044 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
1047 return_ranges
= get_return_ranges_str(expr
);
1048 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1049 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1050 } END_FOR_EACH_PTR(cb
);
1056 undo
= assume(compare_expression(expr
, '<', zero_expr()));
1059 return_ranges
= get_return_ranges_str(expr
);
1060 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1061 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1062 } END_FOR_EACH_PTR(cb
);
1070 static int call_return_state_hooks_split_possible(struct expression
*expr
)
1072 struct returned_state_callback
*cb
;
1073 struct range_list
*rl
;
1074 char *return_ranges
;
1075 struct sm_state
*sm
;
1076 struct sm_state
*tmp
;
1078 int nr_possible
, nr_states
;
1082 if (!expr
|| expr_equal_to_param(expr
, -1))
1085 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1086 if (!sm
|| !sm
->merged
)
1089 if (too_many_possible(sm
))
1092 /* bail if it gets too complicated */
1093 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
1094 nr_states
= stree_count(__get_cur_stree());
1096 * the main thing option_info because we don't want to print a
1097 * million lines of output. If someone else, like check_locking.c
1098 * wants this data, then it doesn't cause a slow down to provide it.
1100 if (option_info
&& nr_states
* nr_possible
>= 2000)
1104 FOR_EACH_PTR(sm
->possible
, tmp
) {
1109 __push_fake_cur_stree();
1111 overwrite_states_using_pool(tmp
);
1113 if (split_positive_from_negative(expr
)) {
1114 __free_fake_cur_stree();
1117 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
1118 return_ranges
= show_rl(rl
);
1120 compare_str
= expr_lte_to_param(expr
, -1);
1122 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1123 return_ranges
= alloc_sname(buf
);
1127 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1128 cb
->callback(return_id
, return_ranges
, expr
);
1129 } END_FOR_EACH_PTR(cb
);
1131 __free_fake_cur_stree();
1132 } END_FOR_EACH_PTR(tmp
);
1137 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
1139 struct range_list
*rl
;
1141 struct returned_state_callback
*cb
;
1142 char *return_ranges
;
1143 int final_pass_orig
= final_pass
;
1146 if (option_project
!= PROJ_KERNEL
)
1149 nr_states
= stree_count(__get_cur_stree());
1150 if (nr_states
> 1500)
1153 if (get_value(expr
, &val
))
1155 if (!get_implied_rl(expr
, &rl
))
1157 if (rl_min(rl
).value
< -4095)
1159 if (rl_max(rl
).value
!= 0)
1162 __push_fake_cur_stree();
1165 __split_whole_condition(expr
);
1166 final_pass
= final_pass_orig
;
1168 return_ranges
= show_rl(rl_filter(rl
, rl_zero()));
1171 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1172 cb
->callback(return_id
, return_ranges
, expr
);
1173 } END_FOR_EACH_PTR(cb
);
1175 __push_true_states();
1176 __use_false_states();
1178 return_ranges
= alloc_sname("0");;
1180 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1181 cb
->callback(return_id
, return_ranges
, expr
);
1182 } END_FOR_EACH_PTR(cb
);
1184 __merge_true_states();
1185 __free_fake_cur_stree();
1190 static int is_boolean(struct expression
*expr
)
1192 struct range_list
*rl
;
1194 if (!get_implied_rl(expr
, &rl
))
1196 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
1201 static int is_conditional(struct expression
*expr
)
1205 if (expr
->type
== EXPR_CONDITIONAL
|| expr
->type
== EXPR_SELECT
)
1210 static int splitable_function_call(struct expression
*expr
)
1212 struct sm_state
*sm
;
1215 if (!expr
|| expr
->type
!= EXPR_CALL
)
1217 snprintf(buf
, sizeof(buf
), "return %p", expr
);
1218 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
1219 return split_helper(sm
, expr
);
1222 static void call_return_state_hooks(struct expression
*expr
)
1224 struct returned_state_callback
*cb
;
1225 const char *return_ranges
;
1229 expr
= strip_expr(expr
);
1231 if (!get_implied_value(expr
, &sval
) &&
1232 (is_condition(expr
) || is_boolean(expr
))) {
1233 call_return_state_hooks_compare(expr
);
1235 } else if (is_conditional(expr
)) {
1236 call_return_state_hooks_conditional(expr
);
1238 } else if (call_return_state_hooks_split_possible(expr
)) {
1240 } else if (call_return_state_hooks_split_success_fail(expr
)) {
1242 } else if (splitable_function_call(expr
)) {
1244 } else if (split_positive_from_negative(expr
)) {
1248 return_ranges
= get_return_ranges_str(expr
);
1251 nr_states
= stree_count(__get_cur_stree());
1252 if (nr_states
>= 10000) {
1253 match_return_info(return_id
, (char *)return_ranges
, expr
);
1256 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1257 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1258 } END_FOR_EACH_PTR(cb
);
1261 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
1263 struct returned_member_callback
*cb
;
1264 struct stree
*stree
;
1265 struct sm_state
*sm
;
1266 struct symbol
*type
;
1268 char member_name
[256];
1271 type
= get_type(expr
);
1272 if (!type
|| type
->type
!= SYM_PTR
)
1274 name
= expr_to_var(expr
);
1278 member_name
[sizeof(member_name
) - 1] = '\0';
1279 strcpy(member_name
, "$");
1282 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
1283 stree
= __get_cur_stree();
1284 FOR_EACH_MY_SM(cb
->owner
, stree
, sm
) {
1285 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
1286 strcpy(member_name
, "*$");
1287 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1290 if (strncmp(sm
->name
, name
, len
) != 0)
1292 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
1294 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
1295 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1296 } END_FOR_EACH_SM(sm
);
1297 } END_FOR_EACH_PTR(cb
);
1302 static void reset_memdb(void)
1304 mem_sql(NULL
, NULL
, "delete from caller_info;");
1305 mem_sql(NULL
, NULL
, "delete from return_states;");
1306 mem_sql(NULL
, NULL
, "delete from call_implies;");
1309 static void match_end_func_info(struct symbol
*sym
)
1311 if (__path_is_null())
1313 call_return_state_hooks(NULL
);
1318 static void init_memdb(void)
1322 const char *schema_files
[] = {
1324 "db/caller_info.schema",
1325 "db/return_states.schema",
1326 "db/function_type_size.schema",
1327 "db/type_size.schema",
1328 "db/call_implies.schema",
1329 "db/function_ptr.schema",
1330 "db/local_values.schema",
1331 "db/function_type_value.schema",
1332 "db/type_value.schema",
1333 "db/function_type_info.schema",
1334 "db/data_info.schema",
1336 static char buf
[4096];
1341 rc
= sqlite3_open(":memory:", &mem_db
);
1342 if (rc
!= SQLITE_OK
) {
1343 printf("Error starting In-Memory database.");
1347 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
1348 fd
= open_data_file(schema_files
[i
]);
1353 ret
= read(fd
, buf
, sizeof(buf
));
1354 if (ret
== sizeof(buf
)) {
1355 printf("Schema file too large: %s (limit %zd bytes)",
1356 schema_files
[i
], sizeof(buf
));
1359 rc
= sqlite3_exec(mem_db
, buf
, NULL
, 0, &err
);
1360 if (rc
!= SQLITE_OK
) {
1361 fprintf(stderr
, "SQL error #2: %s\n", err
);
1362 fprintf(stderr
, "%s\n", buf
);
1367 void open_smatch_db(void)
1376 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
1377 if (rc
!= SQLITE_OK
) {
1384 static void register_common_funcs(void)
1386 struct token
*token
;
1390 if (option_project
== PROJ_NONE
)
1391 strcpy(filename
, "common_functions");
1393 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
1395 token
= get_tokens_file(filename
);
1398 if (token_type(token
) != TOKEN_STREAMBEGIN
)
1400 token
= token
->next
;
1401 while (token_type(token
) != TOKEN_STREAMEND
) {
1402 if (token_type(token
) != TOKEN_IDENT
)
1404 func
= alloc_string(show_ident(token
->ident
));
1405 add_ptr_list(&common_funcs
, func
);
1406 token
= token
->next
;
1408 clear_token_alloc();
1412 void register_definition_db_callbacks(int id
)
1414 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1415 add_hook(&global_variable
, BASE_HOOK
);
1416 add_hook(&global_variable
, DECLARATION_HOOK
);
1417 add_split_return_callback(match_return_info
);
1418 add_split_return_callback(print_returned_struct_members
);
1419 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
1420 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
1422 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
1423 add_hook(&match_call_implies
, CALL_HOOK_AFTER_INLINE
);
1425 register_common_funcs();
1428 void register_db_call_marker(int id
)
1430 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
1433 char *return_state_to_var_sym(struct expression
*expr
, int param
, char *key
, struct symbol
**sym
)
1435 struct expression
*arg
;
1437 char member_name
[256];
1442 const char *star
= "";
1444 if (expr
->type
!= EXPR_ASSIGNMENT
)
1446 name
= expr_to_var_sym(expr
->left
, sym
);
1449 if (key
[0] == '*') {
1453 if (strncmp(key
, "$", 1) != 0)
1455 snprintf(member_name
, sizeof(member_name
), "%s%s%s", star
, name
, key
+ 1);
1457 return alloc_string(member_name
);
1460 while (expr
->type
== EXPR_ASSIGNMENT
)
1461 expr
= strip_expr(expr
->right
);
1462 if (expr
->type
!= EXPR_CALL
)
1465 arg
= get_argument_from_call_expr(expr
->args
, param
);
1469 return get_variable_from_key(arg
, key
, sym
);
1472 char *get_variable_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
)
1480 arg
= strip_expr(arg
);
1482 if (strcmp(key
, "$") == 0)
1483 return expr_to_var_sym(arg
, sym
);
1485 if (strcmp(key
, "*$") == 0) {
1486 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1487 arg
= strip_expr(arg
->unop
);
1488 return expr_to_var_sym(arg
, sym
);
1490 tmp
= expr_to_var_sym(arg
, sym
);
1493 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
1495 return alloc_string(buf
);
1499 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1500 arg
= strip_expr(arg
->unop
);
1501 tmp
= expr_to_var_sym(arg
, sym
);
1504 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 3);
1505 return alloc_string(buf
);
1508 tmp
= expr_to_var_sym(arg
, sym
);
1511 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 1);
1513 return alloc_string(buf
);
1516 const char *get_param_name(struct sm_state
*sm
)
1520 static char buf
[256];
1522 if (!sm
->sym
|| !sm
->sym
->ident
)
1525 param_name
= sm
->sym
->ident
->name
;
1526 name_len
= strlen(param_name
);
1528 if (strcmp(sm
->name
, param_name
) == 0) {
1530 } else if (sm
->name
[name_len
] == '-' && /* check for '-' from "->" */
1531 strncmp(sm
->name
, param_name
, name_len
) == 0) {
1532 snprintf(buf
, sizeof(buf
), "$%s", sm
->name
+ name_len
);
1534 } else if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, param_name
) == 0) {
1540 char *get_data_info_name(struct expression
*expr
)
1547 expr
= strip_expr(expr
);
1548 name
= get_member_name(expr
);
1551 name
= expr_to_var_sym(expr
, &sym
);
1554 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
1556 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1557 snprintf(buf
, sizeof(buf
), "static %s", name
);
1559 snprintf(buf
, sizeof(buf
), "global %s", name
);
1560 ret
= alloc_sname(buf
);