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 void sql_select_return_states(const char *cols
, struct expression
*call
,
305 int (*callback
)(void*, int, char**, char**), void *info
)
307 if (is_fake_call(call
))
310 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
) {
311 sql_select_return_states_pointer(cols
, call
, callback
, info
);
315 if (inlinable(call
->fn
)) {
316 mem_sql(callback
, info
,
317 "select %s from return_states where call_id = '%lu' order by return_id, type;",
318 cols
, (unsigned long)call
);
323 run_sql(get_row_count
, info
, "select count(*) from return_states where %s;",
324 get_static_filter(call
->fn
->symbol
));
325 if (row_count
> 3000)
328 run_sql(callback
, info
, "select %s from return_states where %s order by return_id, type;",
329 cols
, get_static_filter(call
->fn
->symbol
));
332 void sql_select_call_implies(const char *cols
, struct expression
*call
,
333 int (*callback
)(void*, int, char**, char**))
335 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
338 if (inlinable(call
->fn
)) {
339 mem_sql(callback
, NULL
,
340 "select %s from call_implies where call_id = '%lu';",
341 cols
, (unsigned long)call
);
345 run_sql(callback
, NULL
, "select %s from call_implies where %s;",
346 cols
, get_static_filter(call
->fn
->symbol
));
349 void sql_select_caller_info(const char *cols
, struct symbol
*sym
,
350 int (*callback
)(void*, int, char**, char**))
353 mem_sql(callback
, NULL
,
354 "select %s from caller_info where call_id = %lu;",
355 cols
, (unsigned long)__inline_fn
);
359 run_sql(callback
, NULL
,
360 "select %s from caller_info where %s order by call_id;",
361 cols
, get_static_filter(sym
));
364 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
366 struct def_callback
*def_callback
= __alloc_def_callback(0);
368 def_callback
->hook_type
= type
;
369 def_callback
->callback
= callback
;
370 add_ptr_list(&callbacks
, def_callback
);
374 * These call backs are used when the --info option is turned on to print struct
375 * member information. For example foo->bar could have a state in
376 * smatch_extra.c and also check_user.c.
378 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
380 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
382 member_callback
->owner
= owner
;
383 member_callback
->callback
= callback
;
384 add_ptr_list(&member_callbacks
, member_callback
);
387 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
389 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
391 callback
->callback
= fn
;
392 add_ptr_list(&returned_state_callbacks
, callback
);
395 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
))
397 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
399 member_callback
->owner
= owner
;
400 member_callback
->callback
= callback
;
401 add_ptr_list(&returned_member_callbacks
, member_callback
);
404 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*arg
, char *key
, char *value
))
406 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
409 cb
->callback
= callback
;
410 add_ptr_list(&call_implies_cb_list
, cb
);
413 static struct expression
*static_call_expr
;
414 static struct expression
*static_returns_call
;
415 static struct symbol
*return_type
;
416 static struct range_list
*return_range_list
;
417 static int db_return_callback(void *unused
, int argc
, char **argv
, char **azColName
)
419 struct range_list
*rl
;
420 struct expression
*call_expr
= static_returns_call
;
424 call_results_to_rl(call_expr
, return_type
, argv
[0], &rl
);
425 return_range_list
= rl_union(return_range_list
, rl
);
429 struct range_list
*db_return_vals(struct expression
*expr
)
434 if (is_fake_call(expr
))
437 snprintf(buf
, sizeof(buf
), "return %p", expr
);
438 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
440 return clone_rl(estate_rl(sm
->state
));
441 static_returns_call
= expr
;
442 return_type
= get_type(expr
);
446 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
449 return_range_list
= NULL
;
450 if (inlinable(expr
->fn
)) {
451 mem_sql(db_return_callback
, NULL
,
452 "select distinct return from return_states where call_id = '%lu';",
453 (unsigned long)expr
);
455 run_sql(db_return_callback
, NULL
,
456 "select distinct return from return_states where %s;",
457 get_static_filter(expr
->fn
->symbol
));
459 return return_range_list
;
462 static void match_call_marker(struct expression
*expr
)
465 * we just want to record something in the database so that if we have
466 * two calls like: frob(4); frob(some_unkown); then on the receiving
467 * side we know that sometimes frob is called with unknown parameters.
470 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", "");
473 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct stree
*stree
,
474 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
480 char printed_name
[256];
483 expr
= strip_expr(expr
);
484 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
485 expr
= strip_expr(expr
->unop
);
489 name
= expr_to_var_sym(expr
, &sym
);
494 FOR_EACH_SM(stree
, sm
) {
497 if (strcmp(name
, sm
->name
) == 0) {
499 snprintf(printed_name
, sizeof(printed_name
), "*$");
500 else /* these are already handled. fixme: handle them here */
502 } else if (sm
->name
[0] == '*' && strcmp(name
, sm
->name
+ 1) == 0) {
503 snprintf(printed_name
, sizeof(printed_name
), "*$");
504 } else if (strncmp(name
, sm
->name
, len
) == 0) {
506 snprintf(printed_name
, sizeof(printed_name
), "$->%s", sm
->name
+ len
+ 1);
508 snprintf(printed_name
, sizeof(printed_name
), "$%s", sm
->name
+ len
);
512 callback(call
, param
, printed_name
, sm
);
513 } END_FOR_EACH_SM(sm
);
518 static void match_call_info(struct expression
*call
)
520 struct member_info_callback
*cb
;
521 struct expression
*arg
;
526 name
= get_fnptr_name(call
->fn
);
530 FOR_EACH_PTR(member_callbacks
, cb
) {
531 stree
= get_all_states_stree(cb
->owner
);
533 FOR_EACH_PTR(call
->args
, arg
) {
534 print_struct_members(call
, arg
, i
, stree
, cb
->callback
);
536 } END_FOR_EACH_PTR(arg
);
538 } END_FOR_EACH_PTR(cb
);
543 static int get_param(int param
, char **name
, struct symbol
**sym
)
549 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
551 * this is a temporary hack to work around a bug (I think in sparse?)
552 * 2.6.37-rc1:fs/reiserfs/journal.o
553 * If there is a function definition without parameter name found
554 * after a function implementation then it causes a crash.
558 if (arg
->ident
->name
< (char *)100)
561 *name
= arg
->ident
->name
;
566 } END_FOR_EACH_PTR(arg
);
571 static struct stree
*final_states
;
572 static int prev_func_id
= -1;
573 static int caller_info_callback(void *unused
, int argc
, char **argv
, char **azColName
)
581 struct symbol
*sym
= NULL
;
582 struct def_callback
*def_callback
;
588 func_id
= atoi(argv
[0]);
590 type
= strtol(argv
[1], NULL
, 10);
591 param
= strtol(argv
[2], NULL
, 10);
598 if (prev_func_id
== -1)
599 prev_func_id
= func_id
;
600 if (func_id
!= prev_func_id
) {
601 stree
= __pop_fake_cur_stree();
602 merge_stree(&final_states
, stree
);
604 __push_fake_cur_stree();
606 prev_func_id
= func_id
;
609 if (param
>= 0 && !get_param(param
, &name
, &sym
))
612 FOR_EACH_PTR(callbacks
, def_callback
) {
613 if (def_callback
->hook_type
== type
)
614 def_callback
->callback(name
, sym
, key
, value
);
615 } END_FOR_EACH_PTR(def_callback
);
620 static void get_direct_callers(struct symbol
*sym
)
622 sql_select_caller_info("call_id, type, parameter, key, value", sym
,
623 caller_info_callback
);
626 static struct string_list
*ptr_names_done
;
627 static struct string_list
*ptr_names
;
629 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
631 insert_string(&ptr_names
, alloc_string(argv
[0]));
635 static char *get_next_ptr_name(void)
639 FOR_EACH_PTR(ptr_names
, ptr
) {
640 if (list_has_string(ptr_names_done
, ptr
))
642 insert_string(&ptr_names_done
, ptr
);
644 } END_FOR_EACH_PTR(ptr
);
648 static void get_ptr_names(const char *file
, const char *name
)
650 char sql_filter
[1024];
654 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
657 snprintf(sql_filter
, 1024, "function = '%s';", name
);
660 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
662 run_sql(get_ptr_name
, NULL
,
663 "select distinct ptr from function_ptr where %s",
666 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
670 while ((name
= get_next_ptr_name()))
671 get_ptr_names(NULL
, name
);
674 static void match_data_from_db(struct symbol
*sym
)
679 if (!sym
|| !sym
->ident
)
682 __push_fake_cur_stree();
689 if (sym
->ctype
.modifiers
& MOD_STATIC
)
690 get_ptr_names(get_base_file(), sym
->ident
->name
);
692 get_ptr_names(NULL
, sym
->ident
->name
);
694 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
695 __free_ptr_list((struct ptr_list
**)&ptr_names
);
696 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
697 stree
= __pop_fake_cur_stree();
702 get_direct_callers(sym
);
704 FOR_EACH_PTR(ptr_names
, ptr
) {
705 run_sql(caller_info_callback
, NULL
,
706 "select call_id, type, parameter, key, value"
707 " from caller_info where function = '%s' order by call_id",
710 } END_FOR_EACH_PTR(ptr
);
712 __free_ptr_list((struct ptr_list
**)&ptr_names
);
713 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
715 get_direct_callers(sym
);
718 stree
= __pop_fake_cur_stree();
719 merge_stree(&final_states
, stree
);
722 FOR_EACH_SM(final_states
, sm
) {
724 } END_FOR_EACH_SM(sm
);
726 free_stree(&final_states
);
729 static int call_implies_callbacks(void *unused
, int argc
, char **argv
, char **azColName
)
731 struct expression
*call_expr
= static_call_expr
;
732 struct call_implies_callback
*cb
;
733 struct expression
*arg
= NULL
;
740 type
= atoi(argv
[1]);
741 param
= atoi(argv
[2]);
743 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
744 if (cb
->type
!= type
)
747 arg
= get_argument_from_call_expr(call_expr
->args
, param
);
751 cb
->callback(arg
, argv
[3], argv
[4]);
752 } END_FOR_EACH_PTR(cb
);
757 static void match_call_implies(struct expression
*expr
)
759 static_call_expr
= expr
;
760 sql_select_call_implies("function, type, parameter, key, value", expr
,
761 call_implies_callbacks
);
765 static void print_initializer_list(struct expression_list
*expr_list
,
766 struct symbol
*struct_type
)
768 struct expression
*expr
;
769 struct symbol
*base_type
;
770 char struct_name
[256];
772 FOR_EACH_PTR(expr_list
, expr
) {
773 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
774 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
777 if (expr
->type
!= EXPR_IDENTIFIER
)
779 if (!expr
->expr_ident
)
781 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
783 base_type
= get_type(expr
->ident_expression
);
784 if (!base_type
|| base_type
->type
!= SYM_FN
)
786 snprintf(struct_name
, sizeof(struct_name
), "(struct %s)->%s",
787 struct_type
->ident
->name
, expr
->expr_ident
->name
);
788 sql_insert_function_ptr(expr
->ident_expression
->symbol_name
->name
,
790 } END_FOR_EACH_PTR(expr
);
793 static void global_variable(struct symbol
*sym
)
795 struct symbol
*struct_type
;
799 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
801 struct_type
= get_base_type(sym
);
804 if (struct_type
->type
== SYM_ARRAY
) {
805 struct_type
= get_base_type(struct_type
);
809 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
811 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
814 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
816 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", "");
819 static void call_return_state_hooks_conditional(struct expression
*expr
)
821 struct returned_state_callback
*cb
;
822 struct range_list
*rl
;
824 int final_pass_orig
= final_pass
;
826 __push_fake_cur_stree();
829 __split_whole_condition(expr
->conditional
);
830 final_pass
= final_pass_orig
;
832 if (get_implied_rl(expr
->cond_true
, &rl
))
833 rl
= cast_rl(cur_func_return_type(), rl
);
835 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_true
)));
836 return_ranges
= show_rl(rl
);
839 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
840 cb
->callback(return_id
, return_ranges
, expr
);
841 } END_FOR_EACH_PTR(cb
);
843 __push_true_states();
844 __use_false_states();
846 if (get_implied_rl(expr
->cond_false
, &rl
))
847 rl
= cast_rl(cur_func_return_type(), rl
);
849 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_false
)));
850 return_ranges
= show_rl(rl
);
853 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
854 cb
->callback(return_id
, return_ranges
, expr
);
855 } END_FOR_EACH_PTR(cb
);
857 __merge_true_states();
858 __free_fake_cur_stree();
861 static void call_return_state_hooks_compare(struct expression
*expr
)
863 struct returned_state_callback
*cb
;
865 int final_pass_orig
= final_pass
;
867 __push_fake_cur_stree();
870 __split_whole_condition(expr
);
871 final_pass
= final_pass_orig
;
873 return_ranges
= alloc_sname("1");
876 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
877 cb
->callback(return_id
, return_ranges
, expr
);
878 } END_FOR_EACH_PTR(cb
);
880 __push_true_states();
881 __use_false_states();
883 return_ranges
= alloc_sname("0");;
885 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
886 cb
->callback(return_id
, return_ranges
, expr
);
887 } END_FOR_EACH_PTR(cb
);
889 __merge_true_states();
890 __free_fake_cur_stree();
893 static int split_helper(struct sm_state
*sm
, struct expression
*expr
)
895 struct returned_state_callback
*cb
;
896 struct range_list
*rl
;
898 struct sm_state
*tmp
;
900 int nr_possible
, nr_states
;
903 struct stree
*orig_stree
;
904 struct sm_state
*orig_sm
;
906 if (!sm
|| !sm
->merged
)
909 if (too_many_possible(sm
))
912 /* bail if it gets too complicated */
913 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
914 nr_states
= stree_count(__get_cur_stree());
916 * the main thing option_info because we don't want to print a
917 * million lines of output. If someone else, like check_locking.c
918 * wants this data, then it doesn't cause a slow down to provide it.
920 if (option_info
&& nr_states
* nr_possible
>= 2000)
923 compare_str
= expr_lte_to_param(expr
, -1);
925 orig_stree
= clone_stree(__get_cur_stree());
926 FOR_EACH_PTR(sm
->possible
, tmp
) {
931 __push_fake_cur_stree();
933 FOR_EACH_SM(orig_stree
, orig_sm
) {
935 } END_FOR_EACH_SM(orig_sm
);
937 overwrite_states_using_pool(tmp
);
939 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
940 return_ranges
= show_rl(rl
);
942 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
943 return_ranges
= alloc_sname(buf
);
947 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
948 cb
->callback(return_id
, return_ranges
, expr
);
949 } END_FOR_EACH_PTR(cb
);
951 __free_fake_cur_stree();
952 } END_FOR_EACH_PTR(tmp
);
954 free_stree(&orig_stree
);
958 static int call_return_state_hooks_split_possible(struct expression
*expr
)
960 struct returned_state_callback
*cb
;
961 struct range_list
*rl
;
964 struct sm_state
*tmp
;
966 int nr_possible
, nr_states
;
970 if (!expr
|| expr_equal_to_param(expr
, -1))
973 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
974 if (!sm
|| !sm
->merged
)
977 if (too_many_possible(sm
))
980 /* bail if it gets too complicated */
981 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
982 nr_states
= stree_count(__get_cur_stree());
984 * the main thing option_info because we don't want to print a
985 * million lines of output. If someone else, like check_locking.c
986 * wants this data, then it doesn't cause a slow down to provide it.
988 if (option_info
&& nr_states
* nr_possible
>= 2000)
992 FOR_EACH_PTR(sm
->possible
, tmp
) {
997 __push_fake_cur_stree();
999 overwrite_states_using_pool(tmp
);
1001 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
1002 return_ranges
= show_rl(rl
);
1004 compare_str
= expr_lte_to_param(expr
, -1);
1006 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1007 return_ranges
= alloc_sname(buf
);
1011 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1012 cb
->callback(return_id
, return_ranges
, expr
);
1013 } END_FOR_EACH_PTR(cb
);
1015 __free_fake_cur_stree();
1016 } END_FOR_EACH_PTR(tmp
);
1021 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
1023 struct range_list
*rl
;
1025 struct returned_state_callback
*cb
;
1026 char *return_ranges
;
1027 int final_pass_orig
= final_pass
;
1030 if (option_project
!= PROJ_KERNEL
)
1033 nr_states
= stree_count(__get_cur_stree());
1034 if (nr_states
> 1500)
1037 if (get_value(expr
, &val
))
1039 if (!get_implied_rl(expr
, &rl
))
1041 if (rl_min(rl
).value
< -4095)
1043 if (rl_max(rl
).value
!= 0)
1046 __push_fake_cur_stree();
1049 __split_whole_condition(expr
);
1050 final_pass
= final_pass_orig
;
1052 return_ranges
= show_rl(rl_filter(rl
, rl_zero()));
1055 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1056 cb
->callback(return_id
, return_ranges
, expr
);
1057 } END_FOR_EACH_PTR(cb
);
1059 __push_true_states();
1060 __use_false_states();
1062 return_ranges
= alloc_sname("0");;
1064 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1065 cb
->callback(return_id
, return_ranges
, expr
);
1066 } END_FOR_EACH_PTR(cb
);
1068 __merge_true_states();
1069 __free_fake_cur_stree();
1074 static const char *get_return_ranges_str(struct expression
*expr
)
1076 struct range_list
*rl
;
1077 char *return_ranges
;
1084 return alloc_sname("");
1086 if (get_implied_value(expr
, &sval
))
1087 return sval_to_str(sval
);
1089 compare_str
= expr_equal_to_param(expr
, -1);
1090 math_str
= get_value_in_terms_of_parameter_math(expr
);
1092 if (get_implied_rl(expr
, &rl
)) {
1093 rl
= cast_rl(cur_func_return_type(), rl
);
1094 return_ranges
= show_rl(rl
);
1096 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
)));
1097 return_ranges
= show_rl(rl
);
1101 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1102 return alloc_sname(buf
);
1106 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1107 return alloc_sname(buf
);
1110 compare_str
= expr_lte_to_param(expr
, -1);
1112 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1113 return alloc_sname(buf
);
1115 return return_ranges
;
1118 static int is_boolean(struct expression
*expr
)
1120 struct range_list
*rl
;
1122 if (!get_implied_rl(expr
, &rl
))
1124 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
1129 static int is_conditional(struct expression
*expr
)
1133 if (expr
->type
== EXPR_CONDITIONAL
|| expr
->type
== EXPR_SELECT
)
1138 static int splitable_function_call(struct expression
*expr
)
1140 struct sm_state
*sm
;
1143 if (!expr
|| expr
->type
!= EXPR_CALL
)
1145 snprintf(buf
, sizeof(buf
), "return %p", expr
);
1146 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
1147 return split_helper(sm
, expr
);
1150 static void call_return_state_hooks(struct expression
*expr
)
1152 struct returned_state_callback
*cb
;
1153 const char *return_ranges
;
1157 expr
= strip_expr(expr
);
1159 if (!get_implied_value(expr
, &sval
) &&
1160 (is_condition(expr
) || is_boolean(expr
))) {
1161 call_return_state_hooks_compare(expr
);
1163 } else if (is_conditional(expr
)) {
1164 call_return_state_hooks_conditional(expr
);
1166 } else if (call_return_state_hooks_split_possible(expr
)) {
1168 } else if (call_return_state_hooks_split_success_fail(expr
)) {
1170 } else if (splitable_function_call(expr
)) {
1174 return_ranges
= get_return_ranges_str(expr
);
1177 nr_states
= stree_count(__get_cur_stree());
1178 if (nr_states
>= 10000) {
1179 match_return_info(return_id
, (char *)return_ranges
, expr
);
1182 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1183 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1184 } END_FOR_EACH_PTR(cb
);
1187 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
1189 struct returned_member_callback
*cb
;
1190 struct stree
*stree
;
1191 struct sm_state
*sm
;
1192 struct symbol
*type
;
1194 char member_name
[256];
1197 type
= get_type(expr
);
1198 if (!type
|| type
->type
!= SYM_PTR
)
1200 name
= expr_to_var(expr
);
1204 member_name
[sizeof(member_name
) - 1] = '\0';
1205 strcpy(member_name
, "$");
1208 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
1209 stree
= __get_cur_stree();
1210 FOR_EACH_MY_SM(cb
->owner
, stree
, sm
) {
1211 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
1212 strcpy(member_name
, "*$");
1213 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1216 if (strncmp(sm
->name
, name
, len
) != 0)
1218 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
1220 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
1221 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1222 } END_FOR_EACH_SM(sm
);
1223 } END_FOR_EACH_PTR(cb
);
1228 static void reset_memdb(void)
1230 mem_sql(NULL
, NULL
, "delete from caller_info;");
1231 mem_sql(NULL
, NULL
, "delete from return_states;");
1232 mem_sql(NULL
, NULL
, "delete from call_implies;");
1235 static void match_end_func_info(struct symbol
*sym
)
1237 if (__path_is_null())
1239 call_return_state_hooks(NULL
);
1244 static void init_memdb(void)
1248 const char *schema_files
[] = {
1250 "db/caller_info.schema",
1251 "db/return_states.schema",
1252 "db/function_type_size.schema",
1253 "db/type_size.schema",
1254 "db/call_implies.schema",
1255 "db/function_ptr.schema",
1256 "db/local_values.schema",
1257 "db/function_type_value.schema",
1258 "db/type_value.schema",
1259 "db/function_type_info.schema",
1260 "db/data_info.schema",
1262 static char buf
[4096];
1267 rc
= sqlite3_open(":memory:", &mem_db
);
1268 if (rc
!= SQLITE_OK
) {
1269 printf("Error starting In-Memory database.");
1273 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
1274 fd
= open_data_file(schema_files
[i
]);
1279 ret
= read(fd
, buf
, sizeof(buf
));
1280 if (ret
== sizeof(buf
)) {
1281 printf("Schema file too large: %s (limit %zd bytes)",
1282 schema_files
[i
], sizeof(buf
));
1285 rc
= sqlite3_exec(mem_db
, buf
, NULL
, 0, &err
);
1286 if (rc
!= SQLITE_OK
) {
1287 fprintf(stderr
, "SQL error #2: %s\n", err
);
1288 fprintf(stderr
, "%s\n", buf
);
1293 void open_smatch_db(void)
1302 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
1303 if (rc
!= SQLITE_OK
) {
1310 static void register_common_funcs(void)
1312 struct token
*token
;
1316 if (option_project
== PROJ_NONE
)
1317 strcpy(filename
, "common_functions");
1319 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
1321 token
= get_tokens_file(filename
);
1324 if (token_type(token
) != TOKEN_STREAMBEGIN
)
1326 token
= token
->next
;
1327 while (token_type(token
) != TOKEN_STREAMEND
) {
1328 if (token_type(token
) != TOKEN_IDENT
)
1330 func
= alloc_string(show_ident(token
->ident
));
1331 add_ptr_list(&common_funcs
, func
);
1332 token
= token
->next
;
1334 clear_token_alloc();
1338 void register_definition_db_callbacks(int id
)
1340 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1341 add_hook(&global_variable
, BASE_HOOK
);
1342 add_hook(&global_variable
, DECLARATION_HOOK
);
1343 add_split_return_callback(match_return_info
);
1344 add_split_return_callback(print_returned_struct_members
);
1345 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
1346 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
1348 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
1349 add_hook(&match_call_implies
, CALL_HOOK_AFTER_INLINE
);
1351 register_common_funcs();
1354 void register_db_call_marker(int id
)
1356 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
1359 char *return_state_to_var_sym(struct expression
*expr
, int param
, char *key
, struct symbol
**sym
)
1361 struct expression
*arg
;
1363 char member_name
[256];
1368 const char *star
= "";
1370 if (expr
->type
!= EXPR_ASSIGNMENT
)
1372 name
= expr_to_var_sym(expr
->left
, sym
);
1375 if (key
[0] == '*') {
1379 if (strncmp(key
, "$", 1) != 0)
1381 snprintf(member_name
, sizeof(member_name
), "%s%s%s", star
, name
, key
+ 1);
1383 return alloc_string(member_name
);
1386 while (expr
->type
== EXPR_ASSIGNMENT
)
1387 expr
= strip_expr(expr
->right
);
1388 if (expr
->type
!= EXPR_CALL
)
1391 arg
= get_argument_from_call_expr(expr
->args
, param
);
1395 return get_variable_from_key(arg
, key
, sym
);
1398 char *get_variable_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
)
1406 arg
= strip_expr(arg
);
1408 if (strcmp(key
, "$") == 0)
1409 return expr_to_var_sym(arg
, sym
);
1411 if (strcmp(key
, "*$") == 0) {
1412 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1413 arg
= strip_expr(arg
->unop
);
1414 return expr_to_var_sym(arg
, sym
);
1416 tmp
= expr_to_var_sym(arg
, sym
);
1419 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
1421 return alloc_string(buf
);
1425 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1426 arg
= strip_expr(arg
->unop
);
1427 tmp
= expr_to_var_sym(arg
, sym
);
1430 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 3);
1431 return alloc_string(buf
);
1434 tmp
= expr_to_var_sym(arg
, sym
);
1437 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 1);
1439 return alloc_string(buf
);
1442 const char *get_param_name(struct sm_state
*sm
)
1446 static char buf
[256];
1448 if (!sm
->sym
|| !sm
->sym
->ident
)
1451 param_name
= sm
->sym
->ident
->name
;
1452 name_len
= strlen(param_name
);
1454 if (strcmp(sm
->name
, param_name
) == 0) {
1456 } else if (sm
->name
[name_len
] == '-' && /* check for '-' from "->" */
1457 strncmp(sm
->name
, param_name
, name_len
) == 0) {
1458 snprintf(buf
, sizeof(buf
), "$%s", sm
->name
+ name_len
);
1460 } else if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, param_name
) == 0) {
1466 char *get_data_info_name(struct expression
*expr
)
1473 expr
= strip_expr(expr
);
1474 name
= get_member_name(expr
);
1477 name
= expr_to_var_sym(expr
, &sym
);
1480 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
1482 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1483 snprintf(buf
, sizeof(buf
), "static %s", name
);
1485 snprintf(buf
, sizeof(buf
), "global %s", name
);
1486 ret
= alloc_sname(buf
);