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
, 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
);
120 void sql_exec(int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
)
125 if (option_no_db
|| !db
)
128 rc
= sqlite3_exec(db
, sql
, callback
, data
, &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**), void *data
, const char *sql
)
143 rc
= sqlite3_exec(mem_db
, sql
, callback
, data
, &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', 0", get_base_file(), fn
,
218 void sql_insert_call_implies(int type
, int param
, const char *key
, const char *value
)
220 sql_insert(call_implies
, "'%s', '%s', %lu, %d, %d, %d, '%s', %s", get_base_file(),
221 get_function(), (unsigned long)__inline_fn
, fn_static(),
222 type
, param
, key
, value
);
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 void sql_insert_function_type_info(int param
, const char *value
)
242 sql_insert(function_type_info
, "'%s', '%s', %d, %d, '%s'",
243 get_base_file(), get_function(), fn_static(), param
, value
);
246 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
)
250 data_name
= get_data_info_name(data
);
253 sql_insert(data_info
, "'%s', '%s', %d, '%s'", get_base_file(), data_name
, type
, value
);
256 static char *get_static_filter(struct symbol
*sym
)
258 static char sql_filter
[1024];
260 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
261 snprintf(sql_filter
, sizeof(sql_filter
),
262 "file = '%s' and function = '%s' and static = '1'",
263 get_base_file(), sym
->ident
->name
);
265 snprintf(sql_filter
, sizeof(sql_filter
),
266 "function = '%s' and static = '0'", sym
->ident
->name
);
272 static int row_count
;
273 static int get_row_count(void *unused
, int argc
, char **argv
, char **azColName
)
277 row_count
= atoi(argv
[0]);
281 static void sql_select_return_states_pointer(const char *cols
,
282 struct expression
*call
, int (*callback
)(void*, int, char**, char**), void *info
)
286 ptr
= get_fnptr_name(call
->fn
);
290 run_sql(callback
, info
,
291 "select %s from return_states join function_ptr where "
292 "return_states.function == function_ptr.function and ptr = '%s'"
293 "and searchable = 1 order by return_id, type;",
297 void sql_select_return_states(const char *cols
, struct expression
*call
,
298 int (*callback
)(void*, int, char**, char**), void *info
)
300 if (is_fake_call(call
))
303 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
) {
304 sql_select_return_states_pointer(cols
, call
, callback
, info
);
308 if (inlinable(call
->fn
)) {
309 mem_sql(callback
, info
,
310 "select %s from return_states where call_id = '%lu' order by return_id, type;",
311 cols
, (unsigned long)call
);
316 run_sql(get_row_count
, info
, "select count(*) from return_states where %s;",
317 get_static_filter(call
->fn
->symbol
));
318 if (row_count
> 1000)
321 run_sql(callback
, info
, "select %s from return_states where %s order by return_id, type;",
322 cols
, get_static_filter(call
->fn
->symbol
));
325 void sql_select_call_implies(const char *cols
, struct expression
*call
,
326 int (*callback
)(void*, int, char**, char**))
328 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
331 if (inlinable(call
->fn
)) {
332 mem_sql(callback
, NULL
,
333 "select %s from call_implies where call_id = '%lu';",
334 cols
, (unsigned long)call
);
338 run_sql(callback
, NULL
, "select %s from call_implies where %s;",
339 cols
, get_static_filter(call
->fn
->symbol
));
342 void sql_select_caller_info(const char *cols
, struct symbol
*sym
,
343 int (*callback
)(void*, int, char**, char**))
346 mem_sql(callback
, NULL
,
347 "select %s from caller_info where call_id = %lu;",
348 cols
, (unsigned long)__inline_fn
);
352 run_sql(callback
, NULL
,
353 "select %s from caller_info where %s order by call_id;",
354 cols
, get_static_filter(sym
));
357 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
359 struct def_callback
*def_callback
= __alloc_def_callback(0);
361 def_callback
->hook_type
= type
;
362 def_callback
->callback
= callback
;
363 add_ptr_list(&callbacks
, def_callback
);
367 * These call backs are used when the --info option is turned on to print struct
368 * member information. For example foo->bar could have a state in
369 * smatch_extra.c and also check_user.c.
371 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
))
373 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
375 member_callback
->owner
= owner
;
376 member_callback
->callback
= callback
;
377 add_ptr_list(&member_callbacks
, member_callback
);
380 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
382 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
384 callback
->callback
= fn
;
385 add_ptr_list(&returned_state_callbacks
, callback
);
388 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
))
390 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
392 member_callback
->owner
= owner
;
393 member_callback
->callback
= callback
;
394 add_ptr_list(&returned_member_callbacks
, member_callback
);
397 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*arg
, char *key
, char *value
))
399 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
402 cb
->callback
= callback
;
403 add_ptr_list(&call_implies_cb_list
, cb
);
406 static struct expression
*static_call_expr
;
407 static struct expression
*static_returns_call
;
408 static struct symbol
*return_type
;
409 static struct range_list
*return_range_list
;
410 static int db_return_callback(void *unused
, int argc
, char **argv
, char **azColName
)
412 struct range_list
*rl
;
413 struct expression
*call_expr
= static_returns_call
;
417 call_results_to_rl(call_expr
, return_type
, argv
[0], &rl
);
418 return_range_list
= rl_union(return_range_list
, rl
);
422 struct range_list
*db_return_vals(struct expression
*expr
)
424 static_returns_call
= expr
;
425 return_type
= get_type(expr
);
428 if (is_fake_call(expr
))
430 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
433 return_range_list
= NULL
;
434 if (inlinable(expr
->fn
)) {
435 mem_sql(db_return_callback
, NULL
,
436 "select distinct return from return_states where call_id = '%lu';",
437 (unsigned long)expr
);
439 run_sql(db_return_callback
, NULL
,
440 "select distinct return from return_states where %s;",
441 get_static_filter(expr
->fn
->symbol
));
443 return return_range_list
;
446 static void match_call_marker(struct expression
*expr
)
449 * we just want to record something in the database so that if we have
450 * two calls like: frob(4); frob(some_unkown); then on the receiving
451 * side we know that sometimes frob is called with unknown parameters.
454 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", "");
457 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct stree
*stree
,
458 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
))
464 char printed_name
[256];
467 expr
= strip_expr(expr
);
468 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
469 expr
= strip_expr(expr
->unop
);
473 name
= expr_to_var_sym(expr
, &sym
);
478 FOR_EACH_SM(stree
, sm
) {
481 if (strcmp(name
, sm
->name
) == 0) {
483 snprintf(printed_name
, sizeof(printed_name
), "*$$");
484 else /* these are already handled. fixme: handle them here */
486 } else if (sm
->name
[0] == '*' && strcmp(name
, sm
->name
+ 1) == 0) {
487 snprintf(printed_name
, sizeof(printed_name
), "*$$");
488 } else if (strncmp(name
, sm
->name
, len
) == 0) {
490 snprintf(printed_name
, sizeof(printed_name
), "$$->%s", sm
->name
+ len
+ 1);
492 snprintf(printed_name
, sizeof(printed_name
), "$$%s", sm
->name
+ len
);
496 callback(call
, param
, printed_name
, sm
->state
);
497 } END_FOR_EACH_SM(sm
);
502 static void match_call_info(struct expression
*call
)
504 struct member_info_callback
*cb
;
505 struct expression
*arg
;
510 name
= get_fnptr_name(call
->fn
);
514 FOR_EACH_PTR(member_callbacks
, cb
) {
515 stree
= get_all_states_stree(cb
->owner
);
517 FOR_EACH_PTR(call
->args
, arg
) {
518 print_struct_members(call
, arg
, i
, stree
, cb
->callback
);
520 } END_FOR_EACH_PTR(arg
);
522 } END_FOR_EACH_PTR(cb
);
527 static int get_param(int param
, char **name
, struct symbol
**sym
)
533 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
535 * this is a temporary hack to work around a bug (I think in sparse?)
536 * 2.6.37-rc1:fs/reiserfs/journal.o
537 * If there is a function definition without parameter name found
538 * after a function implementation then it causes a crash.
542 if (arg
->ident
->name
< (char *)100)
545 *name
= arg
->ident
->name
;
550 } END_FOR_EACH_PTR(arg
);
555 static struct stree
*final_states
;
556 static int prev_func_id
= -1;
557 static int caller_info_callback(void *unused
, int argc
, char **argv
, char **azColName
)
563 struct symbol
*sym
= NULL
;
564 struct def_callback
*def_callback
;
570 func_id
= atoi(argv
[0]);
572 type
= strtol(argv
[1], NULL
, 10);
573 param
= strtol(argv
[2], NULL
, 10);
577 if (prev_func_id
== -1)
578 prev_func_id
= func_id
;
579 if (func_id
!= prev_func_id
) {
580 stree
= __pop_fake_cur_stree();
581 merge_stree(&final_states
, stree
);
583 __push_fake_cur_stree();
585 prev_func_id
= func_id
;
588 if (type
== INTERNAL
)
590 if (param
>= 0 && !get_param(param
, &name
, &sym
))
593 FOR_EACH_PTR(callbacks
, def_callback
) {
594 if (def_callback
->hook_type
== type
)
595 def_callback
->callback(name
, sym
, argv
[3], argv
[4]);
596 } END_FOR_EACH_PTR(def_callback
);
601 static void get_direct_callers(struct symbol
*sym
)
603 sql_select_caller_info("call_id, type, parameter, key, value", sym
,
604 caller_info_callback
);
607 static struct string_list
*ptr_names_done
;
608 static struct string_list
*ptr_names
;
610 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
612 insert_string(&ptr_names
, alloc_string(argv
[0]));
616 static char *get_next_ptr_name(void)
620 FOR_EACH_PTR(ptr_names
, ptr
) {
621 if (list_has_string(ptr_names_done
, ptr
))
623 insert_string(&ptr_names_done
, ptr
);
625 } END_FOR_EACH_PTR(ptr
);
629 static void get_ptr_names(const char *file
, const char *name
)
631 char sql_filter
[1024];
635 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
638 snprintf(sql_filter
, 1024, "function = '%s';", name
);
641 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
643 run_sql(get_ptr_name
, NULL
,
644 "select distinct ptr from function_ptr where %s",
647 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
651 while ((name
= get_next_ptr_name()))
652 get_ptr_names(NULL
, name
);
655 static void match_data_from_db(struct symbol
*sym
)
660 if (!sym
|| !sym
->ident
)
663 __push_fake_cur_stree();
670 if (sym
->ctype
.modifiers
& MOD_STATIC
)
671 get_ptr_names(get_base_file(), sym
->ident
->name
);
673 get_ptr_names(NULL
, sym
->ident
->name
);
675 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
676 __free_ptr_list((struct ptr_list
**)&ptr_names
);
677 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
678 stree
= __pop_fake_cur_stree();
683 get_direct_callers(sym
);
685 FOR_EACH_PTR(ptr_names
, ptr
) {
686 run_sql(caller_info_callback
, NULL
,
687 "select call_id, type, parameter, key, value"
688 " from caller_info where function = '%s' order by call_id",
691 } END_FOR_EACH_PTR(ptr
);
693 __free_ptr_list((struct ptr_list
**)&ptr_names
);
694 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
696 get_direct_callers(sym
);
699 stree
= __pop_fake_cur_stree();
700 merge_stree(&final_states
, stree
);
703 FOR_EACH_SM(final_states
, sm
) {
705 } END_FOR_EACH_SM(sm
);
707 free_stree(&final_states
);
710 static int call_implies_callbacks(void *unused
, int argc
, char **argv
, char **azColName
)
712 struct expression
*call_expr
= static_call_expr
;
713 struct call_implies_callback
*cb
;
714 struct expression
*arg
= NULL
;
721 type
= atoi(argv
[1]);
722 param
= atoi(argv
[2]);
724 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
725 if (cb
->type
!= type
)
728 arg
= get_argument_from_call_expr(call_expr
->args
, param
);
732 cb
->callback(arg
, argv
[3], argv
[4]);
733 } END_FOR_EACH_PTR(cb
);
738 static void match_call_implies(struct expression
*expr
)
740 static_call_expr
= expr
;
741 sql_select_call_implies("function, type, parameter, key, value", expr
,
742 call_implies_callbacks
);
746 static void print_initializer_list(struct expression_list
*expr_list
,
747 struct symbol
*struct_type
)
749 struct expression
*expr
;
750 struct symbol
*base_type
;
751 char struct_name
[256];
753 FOR_EACH_PTR(expr_list
, expr
) {
754 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
755 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
758 if (expr
->type
!= EXPR_IDENTIFIER
)
760 if (!expr
->expr_ident
)
762 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
764 base_type
= get_type(expr
->ident_expression
);
765 if (!base_type
|| base_type
->type
!= SYM_FN
)
767 snprintf(struct_name
, sizeof(struct_name
), "(struct %s)->%s",
768 struct_type
->ident
->name
, expr
->expr_ident
->name
);
769 sql_insert_function_ptr(expr
->ident_expression
->symbol_name
->name
,
771 } END_FOR_EACH_PTR(expr
);
774 static void global_variable(struct symbol
*sym
)
776 struct symbol
*struct_type
;
780 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
782 struct_type
= get_base_type(sym
);
785 if (struct_type
->type
== SYM_ARRAY
) {
786 struct_type
= get_base_type(struct_type
);
790 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
792 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
795 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
797 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", "");
800 static void call_return_state_hooks_conditional(struct expression
*expr
)
802 struct returned_state_callback
*cb
;
803 struct range_list
*rl
;
805 int final_pass_orig
= final_pass
;
807 __push_fake_cur_stree();
810 __split_whole_condition(expr
->conditional
);
811 final_pass
= final_pass_orig
;
813 if (get_implied_rl(expr
->cond_true
, &rl
))
814 rl
= cast_rl(cur_func_return_type(), rl
);
816 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_true
)));
817 return_ranges
= show_rl(rl
);
820 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
821 cb
->callback(return_id
, return_ranges
, expr
);
822 } END_FOR_EACH_PTR(cb
);
824 __push_true_states();
825 __use_false_states();
827 if (get_implied_rl(expr
->cond_false
, &rl
))
828 rl
= cast_rl(cur_func_return_type(), rl
);
830 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_false
)));
831 return_ranges
= show_rl(rl
);
834 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
835 cb
->callback(return_id
, return_ranges
, expr
);
836 } END_FOR_EACH_PTR(cb
);
838 __merge_true_states();
839 __free_fake_cur_stree();
842 static void call_return_state_hooks_compare(struct expression
*expr
)
844 struct returned_state_callback
*cb
;
846 int final_pass_orig
= final_pass
;
848 __push_fake_cur_stree();
851 __split_whole_condition(expr
);
852 final_pass
= final_pass_orig
;
854 return_ranges
= alloc_sname("1");
857 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
858 cb
->callback(return_id
, return_ranges
, expr
);
859 } END_FOR_EACH_PTR(cb
);
861 __push_true_states();
862 __use_false_states();
864 return_ranges
= alloc_sname("0");;
866 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
867 cb
->callback(return_id
, return_ranges
, expr
);
868 } END_FOR_EACH_PTR(cb
);
870 __merge_true_states();
871 __free_fake_cur_stree();
874 static int split_helper(struct sm_state
*sm
, struct expression
*expr
)
876 struct returned_state_callback
*cb
;
877 struct range_list
*rl
;
879 struct sm_state
*tmp
;
881 int nr_possible
, nr_states
;
885 if (!sm
|| !sm
->merged
)
888 if (too_many_possible(sm
))
891 /* bail if it gets too complicated */
892 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
893 nr_states
= stree_count(__get_cur_stree());
895 * the main thing option_info because we don't want to print a
896 * million lines of output. If someone else, like check_locking.c
897 * wants this data, then it doesn't cause a slow down to provide it.
899 if (option_info
&& nr_states
* nr_possible
>= 2000)
902 compare_str
= expr_lte_to_param(expr
, -1);
904 FOR_EACH_PTR(sm
->possible
, tmp
) {
909 __push_fake_cur_stree();
911 overwrite_states_using_pool(tmp
);
913 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
914 return_ranges
= show_rl(rl
);
916 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
917 return_ranges
= alloc_sname(buf
);
921 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
922 cb
->callback(return_id
, return_ranges
, expr
);
923 } END_FOR_EACH_PTR(cb
);
925 __free_fake_cur_stree();
926 } END_FOR_EACH_PTR(tmp
);
931 static int call_return_state_hooks_split_possible(struct expression
*expr
)
933 struct returned_state_callback
*cb
;
934 struct range_list
*rl
;
937 struct sm_state
*tmp
;
939 int nr_possible
, nr_states
;
943 if (!expr
|| expr_equal_to_param(expr
, -1))
946 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
947 if (!sm
|| !sm
->merged
)
950 if (too_many_possible(sm
))
953 /* bail if it gets too complicated */
954 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
955 nr_states
= stree_count(__get_cur_stree());
957 * the main thing option_info because we don't want to print a
958 * million lines of output. If someone else, like check_locking.c
959 * wants this data, then it doesn't cause a slow down to provide it.
961 if (option_info
&& nr_states
* nr_possible
>= 2000)
965 FOR_EACH_PTR(sm
->possible
, tmp
) {
970 __push_fake_cur_stree();
972 overwrite_states_using_pool(tmp
);
974 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
975 return_ranges
= show_rl(rl
);
977 compare_str
= expr_lte_to_param(expr
, -1);
979 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
980 return_ranges
= alloc_sname(buf
);
984 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
985 cb
->callback(return_id
, return_ranges
, expr
);
986 } END_FOR_EACH_PTR(cb
);
988 __free_fake_cur_stree();
989 } END_FOR_EACH_PTR(tmp
);
994 static const char *get_return_ranges_str(struct expression
*expr
)
996 struct range_list
*rl
;
1004 return alloc_sname("");
1006 if (get_implied_value(expr
, &sval
))
1007 return sval_to_str(sval
);
1009 compare_str
= expr_equal_to_param(expr
, -1);
1010 math_str
= get_value_in_terms_of_parameter_math(expr
);
1012 if (get_implied_rl(expr
, &rl
)) {
1013 rl
= cast_rl(cur_func_return_type(), rl
);
1014 return_ranges
= show_rl(rl
);
1016 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
)));
1017 return_ranges
= show_rl(rl
);
1021 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1022 return alloc_sname(buf
);
1026 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1027 return alloc_sname(buf
);
1030 compare_str
= expr_lte_to_param(expr
, -1);
1032 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1033 return alloc_sname(buf
);
1035 return return_ranges
;
1038 static int is_boolean(struct expression
*expr
)
1040 struct range_list
*rl
;
1042 if (!get_implied_rl(expr
, &rl
))
1044 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
1049 static int is_conditional(struct expression
*expr
)
1053 if (expr
->type
== EXPR_CONDITIONAL
|| expr
->type
== EXPR_SELECT
)
1058 static int splitable_function_call(struct expression
*expr
)
1060 struct sm_state
*sm
;
1063 if (!expr
|| expr
->type
!= EXPR_CALL
)
1065 snprintf(buf
, sizeof(buf
), "return %p", expr
);
1066 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
1067 return split_helper(sm
, expr
);
1070 static void call_return_state_hooks(struct expression
*expr
)
1072 struct returned_state_callback
*cb
;
1073 const char *return_ranges
;
1076 expr
= strip_expr(expr
);
1078 if (is_condition(expr
) || is_boolean(expr
)) {
1079 call_return_state_hooks_compare(expr
);
1081 } else if (is_conditional(expr
)) {
1082 call_return_state_hooks_conditional(expr
);
1084 } else if (call_return_state_hooks_split_possible(expr
)) {
1086 } else if (splitable_function_call(expr
)) {
1090 return_ranges
= get_return_ranges_str(expr
);
1093 nr_states
= stree_count(__get_cur_stree());
1094 if (nr_states
>= 10000) {
1095 match_return_info(return_id
, (char *)return_ranges
, expr
);
1098 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1099 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1100 } END_FOR_EACH_PTR(cb
);
1103 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
1105 struct returned_member_callback
*cb
;
1106 struct stree
*stree
;
1107 struct sm_state
*sm
;
1108 struct symbol
*type
;
1110 char member_name
[256];
1113 type
= get_type(expr
);
1114 if (!type
|| type
->type
!= SYM_PTR
)
1116 name
= expr_to_var(expr
);
1120 member_name
[sizeof(member_name
) - 1] = '\0';
1121 strcpy(member_name
, "$$");
1124 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
1125 stree
= __get_cur_stree();
1126 FOR_EACH_MY_SM(cb
->owner
, stree
, sm
) {
1127 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
1128 strcpy(member_name
, "*$$");
1129 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1132 if (strncmp(sm
->name
, name
, len
) != 0)
1134 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
1136 strcpy(member_name
, "$$");
1137 strncpy(member_name
+ 2, sm
->name
+ len
, sizeof(member_name
) - 2);
1138 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1139 } END_FOR_EACH_SM(sm
);
1140 } END_FOR_EACH_PTR(cb
);
1145 static void reset_memdb(void)
1147 mem_sql(NULL
, NULL
, "delete from caller_info;");
1148 mem_sql(NULL
, NULL
, "delete from return_states;");
1149 mem_sql(NULL
, NULL
, "delete from call_implies;");
1152 static void match_end_func_info(struct symbol
*sym
)
1154 if (__path_is_null())
1156 call_return_state_hooks(NULL
);
1161 static void init_memdb(void)
1165 const char *schema_files
[] = {
1167 "db/caller_info.schema",
1168 "db/return_states.schema",
1169 "db/function_type_size.schema",
1170 "db/type_size.schema",
1171 "db/call_implies.schema",
1172 "db/function_ptr.schema",
1173 "db/local_values.schema",
1174 "db/function_type_value.schema",
1175 "db/type_value.schema",
1176 "db/function_type_info.schema",
1177 "db/data_info.schema",
1179 static char buf
[4096];
1184 rc
= sqlite3_open(":memory:", &mem_db
);
1185 if (rc
!= SQLITE_OK
) {
1186 printf("Error starting In-Memory database.");
1190 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
1191 fd
= open_data_file(schema_files
[i
]);
1196 ret
= read(fd
, buf
, sizeof(buf
));
1197 if (ret
== sizeof(buf
)) {
1198 printf("Schema file too large: %s (limit %zd bytes)",
1199 schema_files
[i
], sizeof(buf
));
1202 rc
= sqlite3_exec(mem_db
, buf
, NULL
, 0, &err
);
1203 if (rc
!= SQLITE_OK
) {
1204 fprintf(stderr
, "SQL error #2: %s\n", err
);
1205 fprintf(stderr
, "%s\n", buf
);
1210 void open_smatch_db(void)
1219 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
1220 if (rc
!= SQLITE_OK
) {
1227 static void register_common_funcs(void)
1229 struct token
*token
;
1233 if (option_project
== PROJ_NONE
)
1234 strcpy(filename
, "common_functions");
1236 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
1238 token
= get_tokens_file(filename
);
1241 if (token_type(token
) != TOKEN_STREAMBEGIN
)
1243 token
= token
->next
;
1244 while (token_type(token
) != TOKEN_STREAMEND
) {
1245 if (token_type(token
) != TOKEN_IDENT
)
1247 func
= alloc_string(show_ident(token
->ident
));
1248 add_ptr_list(&common_funcs
, func
);
1249 token
= token
->next
;
1251 clear_token_alloc();
1255 void register_definition_db_callbacks(int id
)
1257 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1258 add_hook(&global_variable
, BASE_HOOK
);
1259 add_hook(&global_variable
, DECLARATION_HOOK
);
1260 add_split_return_callback(match_return_info
);
1261 add_split_return_callback(print_returned_struct_members
);
1262 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
1263 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
1265 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
1266 add_hook(&match_call_implies
, CALL_HOOK_AFTER_INLINE
);
1268 register_common_funcs();
1271 void register_db_call_marker(int id
)
1273 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
1276 char *return_state_to_var_sym(struct expression
*expr
, int param
, char *key
, struct symbol
**sym
)
1278 struct expression
*arg
;
1280 char member_name
[256];
1285 const char *star
= "";
1287 if (expr
->type
!= EXPR_ASSIGNMENT
)
1289 name
= expr_to_var_sym(expr
->left
, sym
);
1292 if (key
[0] == '*') {
1296 if (strncmp(key
, "$$", 2) != 0)
1298 snprintf(member_name
, sizeof(member_name
), "%s%s%s", star
, name
, key
+ 2);
1300 return alloc_string(member_name
);
1303 while (expr
->type
== EXPR_ASSIGNMENT
)
1304 expr
= strip_expr(expr
->right
);
1305 if (expr
->type
!= EXPR_CALL
)
1308 arg
= get_argument_from_call_expr(expr
->args
, param
);
1312 return get_variable_from_key(arg
, key
, sym
);
1315 char *get_variable_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
)
1320 if (strcmp(key
, "$$") == 0)
1321 return expr_to_var_sym(arg
, sym
);
1323 if (strcmp(key
, "*$$") == 0) {
1324 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1325 arg
= strip_expr(arg
->unop
);
1326 return expr_to_var_sym(arg
, sym
);
1328 tmp
= expr_to_var_sym(arg
, sym
);
1331 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
1333 return alloc_string(buf
);
1337 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1338 arg
= strip_expr(arg
->unop
);
1339 tmp
= expr_to_var_sym(arg
, sym
);
1342 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 4);
1343 return alloc_string(buf
);
1346 tmp
= expr_to_var_sym(arg
, sym
);
1349 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 2);
1351 return alloc_string(buf
);
1354 const char *get_param_name(struct sm_state
*sm
)
1358 static char buf
[256];
1360 if (!sm
->sym
->ident
)
1363 param_name
= sm
->sym
->ident
->name
;
1364 name_len
= strlen(param_name
);
1366 if (strcmp(sm
->name
, param_name
) == 0) {
1368 } else if (sm
->name
[name_len
] == '-' && /* check for '-' from "->" */
1369 strncmp(sm
->name
, param_name
, name_len
) == 0) {
1370 snprintf(buf
, sizeof(buf
), "$$%s", sm
->name
+ name_len
);
1372 } else if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, param_name
) == 0) {
1378 char *get_data_info_name(struct expression
*expr
)
1385 expr
= strip_expr(expr
);
1386 name
= get_member_name(expr
);
1389 name
= expr_to_var_sym(expr
, &sym
);
1392 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
1394 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1395 snprintf(buf
, sizeof(buf
), "static %s", name
);
1397 snprintf(buf
, sizeof(buf
), "global %s", name
);
1398 ret
= alloc_sname(buf
);