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 static 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
;
904 if (!sm
|| !sm
->merged
)
907 if (too_many_possible(sm
))
910 /* bail if it gets too complicated */
911 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
912 nr_states
= stree_count(__get_cur_stree());
914 * the main thing option_info because we don't want to print a
915 * million lines of output. If someone else, like check_locking.c
916 * wants this data, then it doesn't cause a slow down to provide it.
918 if (option_info
&& nr_states
* nr_possible
>= 2000)
921 compare_str
= expr_lte_to_param(expr
, -1);
923 FOR_EACH_PTR(sm
->possible
, tmp
) {
928 __push_fake_cur_stree();
930 overwrite_states_using_pool(tmp
);
932 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
933 return_ranges
= show_rl(rl
);
935 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
936 return_ranges
= alloc_sname(buf
);
940 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
941 cb
->callback(return_id
, return_ranges
, expr
);
942 } END_FOR_EACH_PTR(cb
);
944 __free_fake_cur_stree();
945 } END_FOR_EACH_PTR(tmp
);
950 static int call_return_state_hooks_split_possible(struct expression
*expr
)
952 struct returned_state_callback
*cb
;
953 struct range_list
*rl
;
956 struct sm_state
*tmp
;
958 int nr_possible
, nr_states
;
962 if (!expr
|| expr_equal_to_param(expr
, -1))
965 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
966 if (!sm
|| !sm
->merged
)
969 if (too_many_possible(sm
))
972 /* bail if it gets too complicated */
973 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
974 nr_states
= stree_count(__get_cur_stree());
976 * the main thing option_info because we don't want to print a
977 * million lines of output. If someone else, like check_locking.c
978 * wants this data, then it doesn't cause a slow down to provide it.
980 if (option_info
&& nr_states
* nr_possible
>= 2000)
984 FOR_EACH_PTR(sm
->possible
, tmp
) {
989 __push_fake_cur_stree();
991 overwrite_states_using_pool(tmp
);
993 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
994 return_ranges
= show_rl(rl
);
996 compare_str
= expr_lte_to_param(expr
, -1);
998 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
999 return_ranges
= alloc_sname(buf
);
1003 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1004 cb
->callback(return_id
, return_ranges
, expr
);
1005 } END_FOR_EACH_PTR(cb
);
1007 __free_fake_cur_stree();
1008 } END_FOR_EACH_PTR(tmp
);
1013 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
1015 struct range_list
*rl
;
1017 struct returned_state_callback
*cb
;
1018 char *return_ranges
;
1019 int final_pass_orig
= final_pass
;
1022 if (option_project
!= PROJ_KERNEL
)
1025 nr_states
= stree_count(__get_cur_stree());
1026 if (nr_states
> 1500)
1029 if (get_value(expr
, &val
))
1031 if (!get_implied_rl(expr
, &rl
))
1033 if (rl_min(rl
).value
< -4095)
1035 if (rl_max(rl
).value
!= 0)
1038 __push_fake_cur_stree();
1041 __split_whole_condition(expr
);
1042 final_pass
= final_pass_orig
;
1044 return_ranges
= show_rl(rl_filter(rl
, rl_zero()));
1047 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1048 cb
->callback(return_id
, return_ranges
, expr
);
1049 } END_FOR_EACH_PTR(cb
);
1051 __push_true_states();
1052 __use_false_states();
1054 return_ranges
= alloc_sname("0");;
1056 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1057 cb
->callback(return_id
, return_ranges
, expr
);
1058 } END_FOR_EACH_PTR(cb
);
1060 __merge_true_states();
1061 __free_fake_cur_stree();
1066 static const char *get_return_ranges_str(struct expression
*expr
)
1068 struct range_list
*rl
;
1069 char *return_ranges
;
1076 return alloc_sname("");
1078 if (get_implied_value(expr
, &sval
))
1079 return sval_to_str(sval
);
1081 compare_str
= expr_equal_to_param(expr
, -1);
1082 math_str
= get_value_in_terms_of_parameter_math(expr
);
1084 if (get_implied_rl(expr
, &rl
)) {
1085 rl
= cast_rl(cur_func_return_type(), rl
);
1086 return_ranges
= show_rl(rl
);
1088 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
)));
1089 return_ranges
= show_rl(rl
);
1093 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1094 return alloc_sname(buf
);
1098 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1099 return alloc_sname(buf
);
1102 compare_str
= expr_lte_to_param(expr
, -1);
1104 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1105 return alloc_sname(buf
);
1107 return return_ranges
;
1110 static int is_boolean(struct expression
*expr
)
1112 struct range_list
*rl
;
1114 if (!get_implied_rl(expr
, &rl
))
1116 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
1121 static int is_conditional(struct expression
*expr
)
1125 if (expr
->type
== EXPR_CONDITIONAL
|| expr
->type
== EXPR_SELECT
)
1130 static int splitable_function_call(struct expression
*expr
)
1132 struct sm_state
*sm
;
1135 if (!expr
|| expr
->type
!= EXPR_CALL
)
1137 snprintf(buf
, sizeof(buf
), "return %p", expr
);
1138 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
1139 return split_helper(sm
, expr
);
1142 static void call_return_state_hooks(struct expression
*expr
)
1144 struct returned_state_callback
*cb
;
1145 const char *return_ranges
;
1149 expr
= strip_expr(expr
);
1151 if (!get_implied_value(expr
, &sval
) &&
1152 (is_condition(expr
) || is_boolean(expr
))) {
1153 call_return_state_hooks_compare(expr
);
1155 } else if (is_conditional(expr
)) {
1156 call_return_state_hooks_conditional(expr
);
1158 } else if (call_return_state_hooks_split_possible(expr
)) {
1160 } else if (call_return_state_hooks_split_success_fail(expr
)) {
1162 } else if (splitable_function_call(expr
)) {
1166 return_ranges
= get_return_ranges_str(expr
);
1169 nr_states
= stree_count(__get_cur_stree());
1170 if (nr_states
>= 10000) {
1171 match_return_info(return_id
, (char *)return_ranges
, expr
);
1174 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1175 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1176 } END_FOR_EACH_PTR(cb
);
1179 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
1181 struct returned_member_callback
*cb
;
1182 struct stree
*stree
;
1183 struct sm_state
*sm
;
1184 struct symbol
*type
;
1186 char member_name
[256];
1189 type
= get_type(expr
);
1190 if (!type
|| type
->type
!= SYM_PTR
)
1192 name
= expr_to_var(expr
);
1196 member_name
[sizeof(member_name
) - 1] = '\0';
1197 strcpy(member_name
, "$");
1200 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
1201 stree
= __get_cur_stree();
1202 FOR_EACH_MY_SM(cb
->owner
, stree
, sm
) {
1203 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
1204 strcpy(member_name
, "*$");
1205 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1208 if (strncmp(sm
->name
, name
, len
) != 0)
1210 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
1212 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
1213 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1214 } END_FOR_EACH_SM(sm
);
1215 } END_FOR_EACH_PTR(cb
);
1220 static void reset_memdb(void)
1222 mem_sql(NULL
, NULL
, "delete from caller_info;");
1223 mem_sql(NULL
, NULL
, "delete from return_states;");
1224 mem_sql(NULL
, NULL
, "delete from call_implies;");
1227 static void match_end_func_info(struct symbol
*sym
)
1229 if (__path_is_null())
1231 call_return_state_hooks(NULL
);
1236 static void init_memdb(void)
1240 const char *schema_files
[] = {
1242 "db/caller_info.schema",
1243 "db/return_states.schema",
1244 "db/function_type_size.schema",
1245 "db/type_size.schema",
1246 "db/call_implies.schema",
1247 "db/function_ptr.schema",
1248 "db/local_values.schema",
1249 "db/function_type_value.schema",
1250 "db/type_value.schema",
1251 "db/function_type_info.schema",
1252 "db/data_info.schema",
1254 static char buf
[4096];
1259 rc
= sqlite3_open(":memory:", &mem_db
);
1260 if (rc
!= SQLITE_OK
) {
1261 printf("Error starting In-Memory database.");
1265 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
1266 fd
= open_data_file(schema_files
[i
]);
1271 ret
= read(fd
, buf
, sizeof(buf
));
1272 if (ret
== sizeof(buf
)) {
1273 printf("Schema file too large: %s (limit %zd bytes)",
1274 schema_files
[i
], sizeof(buf
));
1277 rc
= sqlite3_exec(mem_db
, buf
, NULL
, 0, &err
);
1278 if (rc
!= SQLITE_OK
) {
1279 fprintf(stderr
, "SQL error #2: %s\n", err
);
1280 fprintf(stderr
, "%s\n", buf
);
1285 void open_smatch_db(void)
1294 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
1295 if (rc
!= SQLITE_OK
) {
1302 static void register_common_funcs(void)
1304 struct token
*token
;
1308 if (option_project
== PROJ_NONE
)
1309 strcpy(filename
, "common_functions");
1311 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
1313 token
= get_tokens_file(filename
);
1316 if (token_type(token
) != TOKEN_STREAMBEGIN
)
1318 token
= token
->next
;
1319 while (token_type(token
) != TOKEN_STREAMEND
) {
1320 if (token_type(token
) != TOKEN_IDENT
)
1322 func
= alloc_string(show_ident(token
->ident
));
1323 add_ptr_list(&common_funcs
, func
);
1324 token
= token
->next
;
1326 clear_token_alloc();
1330 void register_definition_db_callbacks(int id
)
1332 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1333 add_hook(&global_variable
, BASE_HOOK
);
1334 add_hook(&global_variable
, DECLARATION_HOOK
);
1335 add_split_return_callback(match_return_info
);
1336 add_split_return_callback(print_returned_struct_members
);
1337 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
1338 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
1340 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
1341 add_hook(&match_call_implies
, CALL_HOOK_AFTER_INLINE
);
1343 register_common_funcs();
1346 void register_db_call_marker(int id
)
1348 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
1351 char *return_state_to_var_sym(struct expression
*expr
, int param
, char *key
, struct symbol
**sym
)
1353 struct expression
*arg
;
1355 char member_name
[256];
1360 const char *star
= "";
1362 if (expr
->type
!= EXPR_ASSIGNMENT
)
1364 name
= expr_to_var_sym(expr
->left
, sym
);
1367 if (key
[0] == '*') {
1371 if (strncmp(key
, "$", 1) != 0)
1373 snprintf(member_name
, sizeof(member_name
), "%s%s%s", star
, name
, key
+ 1);
1375 return alloc_string(member_name
);
1378 while (expr
->type
== EXPR_ASSIGNMENT
)
1379 expr
= strip_expr(expr
->right
);
1380 if (expr
->type
!= EXPR_CALL
)
1383 arg
= get_argument_from_call_expr(expr
->args
, param
);
1387 return get_variable_from_key(arg
, key
, sym
);
1390 char *get_variable_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
)
1395 arg
= strip_expr(arg
);
1397 if (strcmp(key
, "$") == 0)
1398 return expr_to_var_sym(arg
, sym
);
1400 if (strcmp(key
, "*$") == 0) {
1401 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1402 arg
= strip_expr(arg
->unop
);
1403 return expr_to_var_sym(arg
, sym
);
1405 tmp
= expr_to_var_sym(arg
, sym
);
1408 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
1410 return alloc_string(buf
);
1414 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1415 arg
= strip_expr(arg
->unop
);
1416 tmp
= expr_to_var_sym(arg
, sym
);
1419 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 3);
1420 return alloc_string(buf
);
1423 tmp
= expr_to_var_sym(arg
, sym
);
1426 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 1);
1428 return alloc_string(buf
);
1431 const char *get_param_name(struct sm_state
*sm
)
1435 static char buf
[256];
1437 if (!sm
->sym
->ident
)
1440 param_name
= sm
->sym
->ident
->name
;
1441 name_len
= strlen(param_name
);
1443 if (strcmp(sm
->name
, param_name
) == 0) {
1445 } else if (sm
->name
[name_len
] == '-' && /* check for '-' from "->" */
1446 strncmp(sm
->name
, param_name
, name_len
) == 0) {
1447 snprintf(buf
, sizeof(buf
), "$%s", sm
->name
+ name_len
);
1449 } else if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, param_name
) == 0) {
1455 char *get_data_info_name(struct expression
*expr
)
1462 expr
= strip_expr(expr
);
1463 name
= get_member_name(expr
);
1466 name
= expr_to_var_sym(expr
, &sym
);
1469 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
1471 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1472 snprintf(buf
, sizeof(buf
), "static %s", name
);
1474 snprintf(buf
, sizeof(buf
), "global %s", name
);
1475 ret
= alloc_sname(buf
);