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 smatch_state
*state
);
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
, 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 *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
, sql
);
120 void sql_exec(int (*callback
)(void*, int, char**, char**), const char *sql
)
125 if (option_no_db
|| !db
)
128 rc
= sqlite3_exec(db
, sql
, callback
, 0, &err
);
129 if (rc
!= SQLITE_OK
) {
130 fprintf(stderr
, "SQL error #2: %s\n", err
);
131 fprintf(stderr
, "SQL: '%s'\n", sql
);
135 void sql_mem_exec(int (*callback
)(void*, int, char**, char**), const char *sql
)
143 rc
= sqlite3_exec(mem_db
, sql
, callback
, 0, &err
);
144 if (rc
!= SQLITE_OK
) {
145 fprintf(stderr
, "SQL error #2: %s\n", err
);
146 fprintf(stderr
, "SQL: '%s'\n", sql
);
150 void sql_insert_return_states(int return_id
, const char *return_ranges
,
151 int type
, int param
, const char *key
, const char *value
)
153 if (key
&& strlen(key
) >= 80)
155 sql_insert(return_states
, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
156 get_base_file(), get_function(), (unsigned long)__inline_fn
,
157 return_id
, return_ranges
, fn_static(), type
, param
, key
, value
);
160 static struct string_list
*common_funcs
;
161 static int is_common_function(const char *fn
)
165 if (strncmp(fn
, "__builtin_", 10) == 0)
168 FOR_EACH_PTR(common_funcs
, tmp
) {
169 if (strcmp(tmp
, fn
) == 0)
171 } END_FOR_EACH_PTR(tmp
);
176 void sql_insert_caller_info(struct expression
*call
, int type
,
177 int param
, const char *key
, const char *value
)
181 if (!option_info
&& !__inline_call
)
184 if (key
&& strlen(key
) >= 80)
187 fn
= get_fnptr_name(call
->fn
);
193 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
194 get_base_file(), get_function(), fn
, (unsigned long)call
,
195 is_static(call
->fn
), type
, param
, key
, value
);
201 if (is_common_function(fn
))
204 sm_msg("SQL_caller_info: insert into caller_info values ("
205 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
206 get_base_file(), get_function(), fn
, is_static(call
->fn
),
207 type
, param
, key
, value
);
212 void sql_insert_function_ptr(const char *fn
, const char *struct_name
)
214 sql_insert(function_ptr
, "'%s', '%s', '%s'", get_base_file(), fn
,
218 void sql_insert_call_implies(int type
, int param
, int value
)
220 sql_insert(call_implies
, "'%s', '%s', %lu, %d, %d, %d, %d", get_base_file(),
221 get_function(), (unsigned long)__inline_fn
, fn_static(),
225 void sql_insert_function_type_size(const char *member
, const char *ranges
)
227 sql_insert(function_type_size
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member
, ranges
);
230 void sql_insert_local_values(const char *name
, const char *value
)
232 sql_insert(local_values
, "'%s', '%s', '%s'", get_base_file(), name
, value
);
235 void sql_insert_function_type_value(const char *type
, const char *value
)
237 sql_insert(function_type_value
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type
, value
);
240 static char *get_static_filter(struct symbol
*sym
)
242 static char sql_filter
[1024];
244 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
245 snprintf(sql_filter
, sizeof(sql_filter
),
246 "file = '%s' and function = '%s' and static = '1'",
247 get_base_file(), sym
->ident
->name
);
249 snprintf(sql_filter
, sizeof(sql_filter
),
250 "function = '%s' and static = '0'", sym
->ident
->name
);
256 static int row_count
;
257 static int get_row_count(void *unused
, int argc
, char **argv
, char **azColName
)
261 row_count
= atoi(argv
[0]);
265 static void sql_select_return_states_pointer(const char *cols
,
266 struct expression
*call
, int (*callback
)(void*, int, char**, char**))
270 ptr
= get_fnptr_name(call
);
275 run_sql(get_row_count
,
276 "select count(*) from return_states join function_ptr where "
277 "return_states.function == function_ptr.function and ptr = '%s';",
279 if (row_count
> 1000)
283 "select %s from return_states join function_ptr where "
284 "return_states.function == function_ptr.function and ptr = '%s' "
285 "order by return_id, type;",
289 void sql_select_return_states(const char *cols
, struct expression
*call
,
290 int (*callback
)(void*, int, char**, char**))
292 if (is_fake_call(call
))
295 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
) {
296 sql_select_return_states_pointer(cols
, call
, callback
);
300 if (inlinable(call
->fn
)) {
302 "select %s from return_states where call_id = '%lu' order by return_id, type;",
303 cols
, (unsigned long)call
);
308 run_sql(get_row_count
, "select count(*) from return_states where %s;",
309 get_static_filter(call
->fn
->symbol
));
310 if (row_count
> 1000)
313 run_sql(callback
, "select %s from return_states where %s order by return_id, type;",
314 cols
, get_static_filter(call
->fn
->symbol
));
317 void sql_select_call_implies(const char *cols
, struct expression
*call
,
318 int (*callback
)(void*, int, char**, char**))
320 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
323 if (inlinable(call
->fn
)) {
325 "select %s from call_implies where call_id = '%lu';",
326 cols
, (unsigned long)call
);
330 run_sql(callback
, "select %s from call_implies where %s;",
331 cols
, get_static_filter(call
->fn
->symbol
));
334 void sql_select_caller_info(const char *cols
, struct symbol
*sym
,
335 int (*callback
)(void*, int, char**, char**))
338 mem_sql(callback
, "select %s from caller_info where call_id = %lu;",
339 cols
, (unsigned long)__inline_fn
);
344 "select %s from caller_info where %s order by call_id;",
345 cols
, get_static_filter(sym
));
348 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
350 struct def_callback
*def_callback
= __alloc_def_callback(0);
352 def_callback
->hook_type
= type
;
353 def_callback
->callback
= callback
;
354 add_ptr_list(&callbacks
, def_callback
);
358 * These call backs are used when the --info option is turned on to print struct
359 * member information. For example foo->bar could have a state in
360 * smatch_extra.c and also check_user.c.
362 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
))
364 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
366 member_callback
->owner
= owner
;
367 member_callback
->callback
= callback
;
368 add_ptr_list(&member_callbacks
, member_callback
);
371 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
373 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
375 callback
->callback
= fn
;
376 add_ptr_list(&returned_state_callbacks
, callback
);
379 void add_returned_member_callback(int owner
, void (*callback
)(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
))
381 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
383 member_callback
->owner
= owner
;
384 member_callback
->callback
= callback
;
385 add_ptr_list(&returned_member_callbacks
, member_callback
);
388 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*arg
, char *value
))
390 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
393 cb
->callback
= callback
;
394 add_ptr_list(&call_implies_cb_list
, cb
);
397 static struct expression
*static_call_expr
;
398 static struct symbol
*return_type
;
399 static struct range_list
*return_range_list
;
400 static int db_return_callback(void *unused
, int argc
, char **argv
, char **azColName
)
402 struct range_list
*rl
;
406 call_results_to_rl(static_call_expr
, return_type
, argv
[0], &rl
);
407 return_range_list
= rl_union(return_range_list
, rl
);
411 struct range_list
*db_return_vals(struct expression
*expr
)
413 static_call_expr
= expr
;
414 return_type
= get_type(expr
);
417 if (is_fake_call(expr
))
419 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
422 return_range_list
= NULL
;
423 if (inlinable(expr
->fn
)) {
424 mem_sql(db_return_callback
,
425 "select distinct return from return_states where call_id = '%lu';",
426 (unsigned long)expr
);
428 run_sql(db_return_callback
,
429 "select distinct return from return_states where %s;",
430 get_static_filter(expr
->fn
->symbol
));
432 return return_range_list
;
435 static void match_call_marker(struct expression
*expr
)
438 * we just want to record something in the database so that if we have
439 * two calls like: frob(4); frob(some_unkown); then on the receiving
440 * side we know that sometimes frob is called with unknown parameters.
443 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", "");
446 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct stree
*stree
,
447 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
))
453 char printed_name
[256];
456 expr
= strip_expr(expr
);
457 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
458 expr
= strip_expr(expr
->unop
);
462 name
= expr_to_var_sym(expr
, &sym
);
467 FOR_EACH_SM(stree
, sm
) {
470 if (strcmp(name
, sm
->name
) == 0) {
472 snprintf(printed_name
, sizeof(printed_name
), "*$$");
473 else /* these are already handled. fixme: handle them here */
475 } else if (sm
->name
[0] == '*' && strcmp(name
, sm
->name
+ 1) == 0) {
476 snprintf(printed_name
, sizeof(printed_name
), "*$$");
477 } else if (strncmp(name
, sm
->name
, len
) == 0) {
479 snprintf(printed_name
, sizeof(printed_name
), "$$->%s", sm
->name
+ len
+ 1);
481 snprintf(printed_name
, sizeof(printed_name
), "$$%s", sm
->name
+ len
);
485 callback(call
, param
, printed_name
, sm
->state
);
486 } END_FOR_EACH_SM(sm
);
491 static void match_call_info(struct expression
*call
)
493 struct member_info_callback
*cb
;
494 struct expression
*arg
;
499 name
= get_fnptr_name(call
->fn
);
503 FOR_EACH_PTR(member_callbacks
, cb
) {
504 stree
= get_all_states_stree(cb
->owner
);
506 FOR_EACH_PTR(call
->args
, arg
) {
507 print_struct_members(call
, arg
, i
, stree
, cb
->callback
);
509 } END_FOR_EACH_PTR(arg
);
511 } END_FOR_EACH_PTR(cb
);
516 static int get_param(int param
, char **name
, struct symbol
**sym
)
522 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
524 * this is a temporary hack to work around a bug (I think in sparse?)
525 * 2.6.37-rc1:fs/reiserfs/journal.o
526 * If there is a function definition without parameter name found
527 * after a function implementation then it causes a crash.
531 if (arg
->ident
->name
< (char *)100)
533 if (i
== param
&& arg
->ident
->name
) {
534 *name
= arg
->ident
->name
;
539 } END_FOR_EACH_PTR(arg
);
544 static struct stree
*final_states
;
545 static int prev_func_id
= -1;
546 static int db_callback(void *unused
, int argc
, char **argv
, char **azColName
)
552 struct symbol
*sym
= NULL
;
553 struct def_callback
*def_callback
;
559 func_id
= atoi(argv
[0]);
561 type
= strtol(argv
[1], NULL
, 10);
562 param
= strtol(argv
[2], NULL
, 10);
566 if (prev_func_id
== -1)
567 prev_func_id
= func_id
;
568 if (func_id
!= prev_func_id
) {
569 stree
= __pop_fake_cur_stree();
570 merge_stree(&final_states
, stree
);
572 __push_fake_cur_stree();
574 prev_func_id
= func_id
;
577 if (type
== INTERNAL
)
579 if (param
>= 0 && !get_param(param
, &name
, &sym
))
582 FOR_EACH_PTR(callbacks
, def_callback
) {
583 if (def_callback
->hook_type
== type
)
584 def_callback
->callback(name
, sym
, argv
[3], argv
[4]);
585 } END_FOR_EACH_PTR(def_callback
);
590 static void get_direct_callers(struct symbol
*sym
)
592 sql_select_caller_info("call_id, type, parameter, key, value", sym
,
596 static struct string_list
*ptr_names_done
;
597 static struct string_list
*ptr_names
;
599 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
601 insert_string(&ptr_names
, alloc_string(argv
[0]));
605 static char *get_next_ptr_name(void)
609 FOR_EACH_PTR(ptr_names
, ptr
) {
610 if (list_has_string(ptr_names_done
, ptr
))
612 insert_string(&ptr_names_done
, ptr
);
614 } END_FOR_EACH_PTR(ptr
);
618 static void get_ptr_names(const char *file
, const char *name
)
620 char sql_filter
[1024];
624 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
627 snprintf(sql_filter
, 1024, "function = '%s';", name
);
630 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
632 run_sql(get_ptr_name
,
633 "select distinct ptr from function_ptr where %s",
636 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
640 while ((name
= get_next_ptr_name()))
641 get_ptr_names(NULL
, name
);
644 static void get_function_pointer_callers(struct symbol
*sym
)
648 if (sym
->ctype
.modifiers
& MOD_STATIC
)
649 get_ptr_names(get_base_file(), sym
->ident
->name
);
651 get_ptr_names(NULL
, sym
->ident
->name
);
653 FOR_EACH_PTR(ptr_names
, ptr
) {
654 run_sql(db_callback
, "select call_id, type, parameter, key, value"
655 " from caller_info where function = '%s' order by call_id",
658 } END_FOR_EACH_PTR(ptr
);
660 __free_ptr_list((struct ptr_list
**)&ptr_names
);
661 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
664 static void match_data_from_db(struct symbol
*sym
)
669 if (!sym
|| !sym
->ident
|| !sym
->ident
->name
)
672 __push_fake_cur_stree();
676 get_direct_callers(sym
);
678 get_function_pointer_callers(sym
);
680 stree
= __pop_fake_cur_stree();
681 merge_stree(&final_states
, stree
);
684 FOR_EACH_SM(final_states
, sm
) {
686 } END_FOR_EACH_SM(sm
);
688 free_stree(&final_states
);
691 static int call_implies_callbacks(void *unused
, int argc
, char **argv
, char **azColName
)
693 struct call_implies_callback
*cb
;
694 struct expression
*arg
= NULL
;
701 type
= atoi(argv
[1]);
702 param
= atoi(argv
[2]);
704 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
705 if (cb
->type
!= type
)
708 arg
= get_argument_from_call_expr(static_call_expr
->args
, param
);
712 cb
->callback(arg
, argv
[3]);
713 } END_FOR_EACH_PTR(cb
);
718 static void match_call_implies(struct expression
*expr
)
720 static_call_expr
= expr
;
721 sql_select_call_implies("function, type, parameter, value", expr
,
722 call_implies_callbacks
);
726 static void print_initializer_list(struct expression_list
*expr_list
,
727 struct symbol
*struct_type
)
729 struct expression
*expr
;
730 struct symbol
*base_type
;
731 char struct_name
[256];
733 FOR_EACH_PTR(expr_list
, expr
) {
734 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
735 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
738 if (expr
->type
!= EXPR_IDENTIFIER
)
740 if (!expr
->expr_ident
)
742 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
744 base_type
= get_type(expr
->ident_expression
);
745 if (!base_type
|| base_type
->type
!= SYM_FN
)
747 snprintf(struct_name
, sizeof(struct_name
), "(struct %s)->%s",
748 struct_type
->ident
->name
, expr
->expr_ident
->name
);
749 sql_insert_function_ptr(expr
->ident_expression
->symbol_name
->name
,
751 } END_FOR_EACH_PTR(expr
);
754 static void global_variable(struct symbol
*sym
)
756 struct symbol
*struct_type
;
760 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
762 struct_type
= get_base_type(sym
);
765 if (struct_type
->type
== SYM_ARRAY
) {
766 struct_type
= get_base_type(struct_type
);
770 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
772 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
775 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
777 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", "");
780 static void call_return_state_hooks_conditional(struct expression
*expr
)
782 struct returned_state_callback
*cb
;
783 struct range_list
*rl
;
785 int final_pass_orig
= final_pass
;
787 __push_fake_cur_stree();
790 __split_whole_condition(expr
->conditional
);
791 final_pass
= final_pass_orig
;
793 if (get_implied_rl(expr
->cond_true
, &rl
))
794 rl
= cast_rl(cur_func_return_type(), rl
);
796 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_true
)));
797 return_ranges
= show_rl(rl
);
800 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
801 cb
->callback(return_id
, return_ranges
, expr
);
802 } END_FOR_EACH_PTR(cb
);
804 __push_true_states();
805 __use_false_states();
807 if (get_implied_rl(expr
->cond_false
, &rl
))
808 rl
= cast_rl(cur_func_return_type(), rl
);
810 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_false
)));
811 return_ranges
= show_rl(rl
);
814 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
815 cb
->callback(return_id
, return_ranges
, expr
);
816 } END_FOR_EACH_PTR(cb
);
818 __merge_true_states();
819 __free_fake_cur_stree();
822 static void call_return_state_hooks_compare(struct expression
*expr
)
824 struct returned_state_callback
*cb
;
826 int final_pass_orig
= final_pass
;
828 __push_fake_cur_stree();
831 __split_whole_condition(expr
);
832 final_pass
= final_pass_orig
;
834 return_ranges
= alloc_sname("1");
837 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
838 cb
->callback(return_id
, return_ranges
, expr
);
839 } END_FOR_EACH_PTR(cb
);
841 __push_true_states();
842 __use_false_states();
844 return_ranges
= alloc_sname("0");;
846 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
847 cb
->callback(return_id
, return_ranges
, expr
);
848 } END_FOR_EACH_PTR(cb
);
850 __merge_true_states();
851 __free_fake_cur_stree();
854 static int call_return_state_hooks_split_possible(struct expression
*expr
)
856 struct returned_state_callback
*cb
;
857 struct range_list
*rl
;
860 struct sm_state
*tmp
;
862 int nr_possible
, nr_states
;
866 if (!expr
|| expr_equal_to_param(expr
))
869 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
870 if (!sm
|| !sm
->merged
)
873 if (too_many_possible(sm
))
876 /* bail if it gets too complicated */
877 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
878 nr_states
= stree_count(__get_cur_stree());
880 * the main thing option_info because we don't want to print a
881 * million lines of output. If someone else, like check_locking.c
882 * wants this data, then it doesn't cause a slow down to provide it.
884 if (option_info
&& nr_states
* nr_possible
>= 2000)
887 compare_str
= expr_lte_to_param(expr
);
889 FOR_EACH_PTR(sm
->possible
, tmp
) {
894 __push_fake_cur_stree();
896 overwrite_states_using_pool(tmp
);
898 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
899 return_ranges
= show_rl(rl
);
901 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
902 return_ranges
= alloc_sname(buf
);
906 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
907 cb
->callback(return_id
, return_ranges
, expr
);
908 } END_FOR_EACH_PTR(cb
);
910 __free_fake_cur_stree();
911 } END_FOR_EACH_PTR(tmp
);
916 static char *get_return_ranges_str(struct expression
*expr
)
918 struct range_list
*rl
;
924 return alloc_sname("");
925 compare_str
= expr_equal_to_param(expr
);
929 if (get_implied_rl(expr
, &rl
)) {
930 rl
= cast_rl(cur_func_return_type(), rl
);
931 return_ranges
= show_rl(rl
);
933 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
)));
934 return_ranges
= show_rl(rl
);
936 compare_str
= expr_lte_to_param(expr
);
938 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
939 return alloc_sname(buf
);
941 return return_ranges
;
944 static int is_conditional(struct expression
*expr
)
948 if (expr
->type
== EXPR_CONDITIONAL
|| expr
->type
== EXPR_SELECT
)
953 static void call_return_state_hooks(struct expression
*expr
)
955 struct returned_state_callback
*cb
;
959 expr
= strip_expr(expr
);
961 if (is_condition(expr
)) {
962 call_return_state_hooks_compare(expr
);
964 } else if (is_conditional(expr
)) {
965 call_return_state_hooks_conditional(expr
);
967 } else if (call_return_state_hooks_split_possible(expr
)) {
971 return_ranges
= get_return_ranges_str(expr
);
974 nr_states
= stree_count(__get_cur_stree());
975 if (nr_states
>= 10000) {
976 match_return_info(return_id
, return_ranges
, expr
);
979 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
980 cb
->callback(return_id
, return_ranges
, expr
);
981 } END_FOR_EACH_PTR(cb
);
984 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
986 struct returned_member_callback
*cb
;
987 struct stree
*my_stree
;
991 char member_name
[256];
994 type
= get_type(expr
);
995 if (!type
|| type
->type
!= SYM_PTR
)
997 name
= expr_to_var(expr
);
1001 member_name
[sizeof(member_name
) - 1] = '\0';
1002 strcpy(member_name
, "$$");
1005 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
1006 my_stree
= get_all_states_stree(cb
->owner
);
1007 FOR_EACH_SM(my_stree
, sm
) {
1008 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
1009 strcpy(member_name
, "*$$");
1010 cb
->callback(return_id
, return_ranges
, member_name
, sm
->state
);
1013 if (strncmp(sm
->name
, name
, len
) != 0)
1015 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
1017 strcpy(member_name
, "$$");
1018 strncpy(member_name
+ 2, sm
->name
+ len
, sizeof(member_name
) - 2);
1019 cb
->callback(return_id
, return_ranges
, member_name
, sm
->state
);
1020 } END_FOR_EACH_SM(sm
);
1021 free_stree(&my_stree
);
1022 } END_FOR_EACH_PTR(cb
);
1027 static void reset_memdb(void)
1029 mem_sql(NULL
, "delete from caller_info;");
1030 mem_sql(NULL
, "delete from return_states;");
1031 mem_sql(NULL
, "delete from call_implies;");
1034 static void match_end_func_info(struct symbol
*sym
)
1036 if (__path_is_null())
1038 call_return_state_hooks(NULL
);
1043 static void init_memdb(void)
1047 const char *schema_files
[] = {
1049 "db/caller_info.schema",
1050 "db/return_states.schema",
1051 "db/function_type_size.schema",
1052 "db/type_size.schema",
1053 "db/call_implies.schema",
1054 "db/function_ptr.schema",
1055 "db/local_values.schema",
1056 "db/function_type_value.schema",
1057 "db/type_value.schema",
1059 static char buf
[4096];
1064 rc
= sqlite3_open(":memory:", &mem_db
);
1065 if (rc
!= SQLITE_OK
) {
1066 printf("Error starting In-Memory database.");
1070 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
1071 fd
= open_data_file(schema_files
[i
]);
1076 ret
= read(fd
, buf
, sizeof(buf
));
1077 if (ret
== sizeof(buf
)) {
1078 printf("Schema file too large: %s (limit %zd bytes)",
1079 schema_files
[i
], sizeof(buf
));
1082 rc
= sqlite3_exec(mem_db
, buf
, NULL
, 0, &err
);
1083 if (rc
!= SQLITE_OK
) {
1084 fprintf(stderr
, "SQL error #2: %s\n", err
);
1085 fprintf(stderr
, "%s\n", buf
);
1090 void open_smatch_db(void)
1099 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
1100 if (rc
!= SQLITE_OK
) {
1107 static void register_common_funcs(void)
1109 struct token
*token
;
1113 if (option_project
== PROJ_NONE
)
1114 strcpy(filename
, "common_functions");
1116 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
1118 token
= get_tokens_file(filename
);
1121 if (token_type(token
) != TOKEN_STREAMBEGIN
)
1123 token
= token
->next
;
1124 while (token_type(token
) != TOKEN_STREAMEND
) {
1125 if (token_type(token
) != TOKEN_IDENT
)
1127 func
= alloc_string(show_ident(token
->ident
));
1128 add_ptr_list(&common_funcs
, func
);
1129 token
= token
->next
;
1131 clear_token_alloc();
1135 void register_definition_db_callbacks(int id
)
1137 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1138 add_hook(&global_variable
, BASE_HOOK
);
1139 add_hook(&global_variable
, DECLARATION_HOOK
);
1140 add_split_return_callback(match_return_info
);
1141 add_split_return_callback(print_returned_struct_members
);
1142 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
1143 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
1145 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
1146 add_hook(&match_call_implies
, CALL_HOOK_AFTER_INLINE
);
1148 register_common_funcs();
1151 void register_db_call_marker(int id
)
1153 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
1156 char *return_state_to_var_sym(struct expression
*expr
, int param
, char *key
, struct symbol
**sym
)
1158 struct expression
*arg
;
1160 char member_name
[256];
1165 const char *star
= "";
1167 if (expr
->type
!= EXPR_ASSIGNMENT
)
1169 name
= expr_to_var_sym(expr
->left
, sym
);
1172 if (key
[0] == '*') {
1176 if (strncmp(key
, "$$", 2) != 0)
1178 snprintf(member_name
, sizeof(member_name
), "%s%s%s", star
, name
, key
+ 2);
1180 return alloc_string(member_name
);
1183 while (expr
->type
== EXPR_ASSIGNMENT
)
1184 expr
= strip_expr(expr
->right
);
1185 if (expr
->type
!= EXPR_CALL
)
1188 arg
= get_argument_from_call_expr(expr
->args
, param
);
1192 return get_variable_from_key(arg
, key
, sym
);
1195 char *get_variable_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
)
1200 if (strcmp(key
, "$$") == 0)
1201 return expr_to_var_sym(arg
, sym
);
1203 if (strcmp(key
, "*$$") == 0) {
1204 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1205 arg
= strip_expr(arg
->unop
);
1206 return expr_to_var_sym(arg
, sym
);
1208 tmp
= expr_to_var_sym(arg
, sym
);
1211 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
1213 return alloc_string(buf
);
1217 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1218 arg
= strip_expr(arg
->unop
);
1219 tmp
= expr_to_var_sym(arg
, sym
);
1222 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 4);
1223 return alloc_string(buf
);
1226 tmp
= expr_to_var_sym(arg
, sym
);
1229 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 2);
1231 return alloc_string(buf
);
1234 const char *get_param_name(struct sm_state
*sm
)
1238 static char buf
[256];
1240 if (!sm
->sym
->ident
)
1243 param_name
= sm
->sym
->ident
->name
;
1244 name_len
= strlen(param_name
);
1246 if (strcmp(sm
->name
, param_name
) == 0) {
1248 } else if (sm
->name
[name_len
] == '-' && /* check for '-' from "->" */
1249 strncmp(sm
->name
, param_name
, name_len
) == 0) {
1250 snprintf(buf
, sizeof(buf
), "$$%s", sm
->name
+ name_len
);
1252 } else if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, param_name
) == 0) {