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
24 #include "smatch_slist.h"
25 #include "smatch_extra.h"
28 static sqlite3
*mem_db
;
32 #define sql_insert(table, values...) \
38 char *err, *p = buf; \
41 p += snprintf(p, buf + sizeof(buf) - p, \
42 "insert into %s values (", #table); \
43 p += snprintf(p, buf + sizeof(buf) - p, values); \
44 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
45 sm_debug("in-mem: %s\n", buf); \
46 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err); \
47 if (rc != SQLITE_OK) { \
48 fprintf(stderr, "SQL error #2: %s\n", err); \
49 fprintf(stderr, "SQL: '%s'\n", buf); \
55 sm_printf("SQL: insert into " #table " values (" values); \
62 void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
);
64 ALLOCATOR(def_callback
, "definition db hook callbacks");
65 DECLARE_PTR_LIST(callback_list
, struct def_callback
);
66 static struct callback_list
*callbacks
;
68 struct member_info_callback
{
70 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
);
72 ALLOCATOR(member_info_callback
, "caller_info callbacks");
73 DECLARE_PTR_LIST(member_info_cb_list
, struct member_info_callback
);
74 static struct member_info_cb_list
*member_callbacks
;
76 struct returned_state_callback
{
77 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*return_expr
);
79 ALLOCATOR(returned_state_callback
, "returned state callbacks");
80 DECLARE_PTR_LIST(returned_state_cb_list
, struct returned_state_callback
);
81 static struct returned_state_cb_list
*returned_state_callbacks
;
83 struct returned_member_callback
{
85 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*expr
, char *printed_name
, struct smatch_state
*state
);
87 ALLOCATOR(returned_member_callback
, "returned member callbacks");
88 DECLARE_PTR_LIST(returned_member_cb_list
, struct returned_member_callback
);
89 static struct returned_member_cb_list
*returned_member_callbacks
;
91 struct call_implies_callback
{
93 void (*callback
)(struct expression
*arg
, char *key
, char *value
);
95 ALLOCATOR(call_implies_callback
, "call_implies callbacks");
96 DECLARE_PTR_LIST(call_implies_cb_list
, struct call_implies_callback
);
97 static struct call_implies_cb_list
*call_implies_cb_list
;
99 static int print_sql_output(void *unused
, int argc
, char **argv
, char **azColName
)
103 for (i
= 0; i
< argc
; i
++) {
106 sm_printf("%s", argv
[i
]);
112 void debug_sql(const char *sql
)
117 sql_exec(print_sql_output
, NULL
, sql
);
120 void debug_mem_sql(const char *sql
)
125 sql_mem_exec(print_sql_output
, NULL
, sql
);
128 void sql_exec(int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
)
133 if (option_no_db
|| !db
)
136 rc
= sqlite3_exec(db
, sql
, callback
, data
, &err
);
137 if (rc
!= SQLITE_OK
) {
138 fprintf(stderr
, "SQL error #2: %s\n", err
);
139 fprintf(stderr
, "SQL: '%s'\n", sql
);
143 void sql_mem_exec(int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
)
151 rc
= sqlite3_exec(mem_db
, sql
, callback
, data
, &err
);
152 if (rc
!= SQLITE_OK
) {
153 fprintf(stderr
, "SQL error #2: %s\n", err
);
154 fprintf(stderr
, "SQL: '%s'\n", sql
);
158 void sql_insert_return_states(int return_id
, const char *return_ranges
,
159 int type
, int param
, const char *key
, const char *value
)
161 if (key
&& strlen(key
) >= 80)
163 sql_insert(return_states
, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
164 get_base_file(), get_function(), (unsigned long)__inline_fn
,
165 return_id
, return_ranges
, fn_static(), type
, param
, key
, value
);
168 static struct string_list
*common_funcs
;
169 static int is_common_function(const char *fn
)
173 if (strncmp(fn
, "__builtin_", 10) == 0)
176 FOR_EACH_PTR(common_funcs
, tmp
) {
177 if (strcmp(tmp
, fn
) == 0)
179 } END_FOR_EACH_PTR(tmp
);
184 void sql_insert_caller_info(struct expression
*call
, int type
,
185 int param
, const char *key
, const char *value
)
189 if (!option_info
&& !__inline_call
)
192 if (key
&& strlen(key
) >= 80)
195 fn
= get_fnptr_name(call
->fn
);
201 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
202 get_base_file(), get_function(), fn
, (unsigned long)call
,
203 is_static(call
->fn
), type
, param
, key
, value
);
209 if (is_common_function(fn
))
212 sm_msg("SQL_caller_info: insert into caller_info values ("
213 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
214 get_base_file(), get_function(), fn
, is_static(call
->fn
),
215 type
, param
, key
, value
);
220 void sql_insert_function_ptr(const char *fn
, const char *struct_name
)
222 sql_insert(function_ptr
, "'%s', '%s', '%s', 0", get_base_file(), fn
,
226 void sql_insert_call_implies(int type
, int param
, const char *key
, const char *value
)
228 sql_insert(call_implies
, "'%s', '%s', %lu, %d, %d, %d, '%s', %s", get_base_file(),
229 get_function(), (unsigned long)__inline_fn
, fn_static(),
230 type
, param
, key
, value
);
233 void sql_insert_function_type_size(const char *member
, const char *ranges
)
235 sql_insert(function_type_size
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member
, ranges
);
238 void sql_insert_local_values(const char *name
, const char *value
)
240 sql_insert(local_values
, "'%s', '%s', '%s'", get_base_file(), name
, value
);
243 void sql_insert_function_type_value(const char *type
, const char *value
)
245 sql_insert(function_type_value
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type
, value
);
248 void sql_insert_function_type_info(int param
, const char *value
)
250 sql_insert(function_type_info
, "'%s', '%s', %d, %d, '%s'",
251 get_base_file(), get_function(), fn_static(), param
, value
);
254 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
)
258 data_name
= get_data_info_name(data
);
261 sql_insert(data_info
, "'%s', '%s', %d, '%s'", get_base_file(), data_name
, type
, value
);
264 char *get_static_filter(struct symbol
*sym
)
266 static char sql_filter
[1024];
268 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
269 snprintf(sql_filter
, sizeof(sql_filter
),
270 "file = '%s' and function = '%s' and static = '1'",
271 get_base_file(), sym
->ident
->name
);
273 snprintf(sql_filter
, sizeof(sql_filter
),
274 "function = '%s' and static = '0'", sym
->ident
->name
);
280 static int row_count
;
281 static int get_row_count(void *unused
, int argc
, char **argv
, char **azColName
)
285 row_count
= atoi(argv
[0]);
289 static void sql_select_return_states_pointer(const char *cols
,
290 struct expression
*call
, int (*callback
)(void*, int, char**, char**), void *info
)
294 ptr
= get_fnptr_name(call
->fn
);
298 run_sql(callback
, info
,
299 "select %s from return_states join function_ptr where "
300 "return_states.function == function_ptr.function and ptr = '%s'"
301 "and searchable = 1 order by return_id, type;",
305 static int is_local_symbol(struct expression
*expr
)
307 if (expr
->type
!= EXPR_SYMBOL
)
309 if (expr
->symbol
->ctype
.modifiers
& (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
))
314 void sql_select_return_states(const char *cols
, struct expression
*call
,
315 int (*callback
)(void*, int, char**, char**), void *info
)
317 if (is_fake_call(call
))
320 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
|| is_local_symbol(call
->fn
)) {
321 sql_select_return_states_pointer(cols
, call
, callback
, info
);
325 if (inlinable(call
->fn
)) {
326 mem_sql(callback
, info
,
327 "select %s from return_states where call_id = '%lu' order by return_id, type;",
328 cols
, (unsigned long)call
);
333 run_sql(get_row_count
, info
, "select count(*) from return_states where %s;",
334 get_static_filter(call
->fn
->symbol
));
335 if (row_count
> 3000)
338 run_sql(callback
, info
, "select %s from return_states where %s order by return_id, type;",
339 cols
, get_static_filter(call
->fn
->symbol
));
342 void sql_select_call_implies(const char *cols
, struct expression
*call
,
343 int (*callback
)(void*, int, char**, char**))
345 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
348 if (inlinable(call
->fn
)) {
349 mem_sql(callback
, NULL
,
350 "select %s from call_implies where call_id = '%lu';",
351 cols
, (unsigned long)call
);
355 run_sql(callback
, NULL
, "select %s from call_implies where %s;",
356 cols
, get_static_filter(call
->fn
->symbol
));
359 void sql_select_caller_info(const char *cols
, struct symbol
*sym
,
360 int (*callback
)(void*, int, char**, char**))
363 mem_sql(callback
, NULL
,
364 "select %s from caller_info where call_id = %lu;",
365 cols
, (unsigned long)__inline_fn
);
369 run_sql(callback
, NULL
,
370 "select %s from caller_info where %s order by call_id;",
371 cols
, get_static_filter(sym
));
374 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
376 struct def_callback
*def_callback
= __alloc_def_callback(0);
378 def_callback
->hook_type
= type
;
379 def_callback
->callback
= callback
;
380 add_ptr_list(&callbacks
, def_callback
);
384 * These call backs are used when the --info option is turned on to print struct
385 * member information. For example foo->bar could have a state in
386 * smatch_extra.c and also check_user.c.
388 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
390 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
392 member_callback
->owner
= owner
;
393 member_callback
->callback
= callback
;
394 add_ptr_list(&member_callbacks
, member_callback
);
397 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
399 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
401 callback
->callback
= fn
;
402 add_ptr_list(&returned_state_callbacks
, callback
);
405 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
))
407 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
409 member_callback
->owner
= owner
;
410 member_callback
->callback
= callback
;
411 add_ptr_list(&returned_member_callbacks
, member_callback
);
414 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*arg
, char *key
, char *value
))
416 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
419 cb
->callback
= callback
;
420 add_ptr_list(&call_implies_cb_list
, cb
);
423 static struct expression
*static_call_expr
;
424 static struct expression
*static_returns_call
;
425 static struct symbol
*return_type
;
426 static struct range_list
*return_range_list
;
427 static int db_return_callback(void *unused
, int argc
, char **argv
, char **azColName
)
429 struct range_list
*rl
;
430 struct expression
*call_expr
= static_returns_call
;
434 call_results_to_rl(call_expr
, return_type
, argv
[0], &rl
);
435 return_range_list
= rl_union(return_range_list
, rl
);
439 struct range_list
*db_return_vals(struct expression
*expr
)
444 if (is_fake_call(expr
))
447 snprintf(buf
, sizeof(buf
), "return %p", expr
);
448 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
450 return clone_rl(estate_rl(sm
->state
));
451 static_returns_call
= expr
;
452 return_type
= get_type(expr
);
456 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
459 return_range_list
= NULL
;
460 if (inlinable(expr
->fn
)) {
461 mem_sql(db_return_callback
, NULL
,
462 "select distinct return from return_states where call_id = '%lu';",
463 (unsigned long)expr
);
465 run_sql(db_return_callback
, NULL
,
466 "select distinct return from return_states where %s;",
467 get_static_filter(expr
->fn
->symbol
));
469 return return_range_list
;
472 struct range_list
*db_return_vals_from_str(const char *fn_name
)
474 static_returns_call
= NULL
;
475 return_type
= &llong_ctype
;
477 return_range_list
= NULL
;
478 run_sql(db_return_callback
, NULL
,
479 "select distinct return from return_states where function = '%s';",
481 return return_range_list
;
484 static void match_call_marker(struct expression
*expr
)
487 * we just want to record something in the database so that if we have
488 * two calls like: frob(4); frob(some_unkown); then on the receiving
489 * side we know that sometimes frob is called with unknown parameters.
492 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", "");
495 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct stree
*stree
,
496 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
502 char printed_name
[256];
505 expr
= strip_expr(expr
);
506 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
507 expr
= strip_expr(expr
->unop
);
511 name
= expr_to_var_sym(expr
, &sym
);
516 FOR_EACH_SM(stree
, sm
) {
519 if (strcmp(name
, sm
->name
) == 0) {
521 snprintf(printed_name
, sizeof(printed_name
), "*$");
522 else /* these are already handled. fixme: handle them here */
524 } else if (sm
->name
[0] == '*' && strcmp(name
, sm
->name
+ 1) == 0) {
525 snprintf(printed_name
, sizeof(printed_name
), "*$");
526 } else if (strncmp(name
, sm
->name
, len
) == 0) {
527 if (isalnum(sm
->name
[len
]))
530 snprintf(printed_name
, sizeof(printed_name
), "$->%s", sm
->name
+ len
+ 1);
532 snprintf(printed_name
, sizeof(printed_name
), "$%s", sm
->name
+ len
);
536 callback(call
, param
, printed_name
, sm
);
537 } END_FOR_EACH_SM(sm
);
542 static void match_call_info(struct expression
*call
)
544 struct member_info_callback
*cb
;
545 struct expression
*arg
;
550 name
= get_fnptr_name(call
->fn
);
554 FOR_EACH_PTR(member_callbacks
, cb
) {
555 stree
= get_all_states_stree(cb
->owner
);
557 FOR_EACH_PTR(call
->args
, arg
) {
558 print_struct_members(call
, arg
, i
, stree
, cb
->callback
);
560 } END_FOR_EACH_PTR(arg
);
562 } END_FOR_EACH_PTR(cb
);
567 static int get_param(int param
, char **name
, struct symbol
**sym
)
573 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
575 * this is a temporary hack to work around a bug (I think in sparse?)
576 * 2.6.37-rc1:fs/reiserfs/journal.o
577 * If there is a function definition without parameter name found
578 * after a function implementation then it causes a crash.
582 if (arg
->ident
->name
< (char *)100)
585 *name
= arg
->ident
->name
;
590 } END_FOR_EACH_PTR(arg
);
595 static struct stree
*final_states
;
596 static int prev_func_id
= -1;
597 static int caller_info_callback(void *unused
, int argc
, char **argv
, char **azColName
)
605 struct symbol
*sym
= NULL
;
606 struct def_callback
*def_callback
;
612 func_id
= atoi(argv
[0]);
614 type
= strtol(argv
[1], NULL
, 10);
615 param
= strtol(argv
[2], NULL
, 10);
622 if (prev_func_id
== -1)
623 prev_func_id
= func_id
;
624 if (func_id
!= prev_func_id
) {
625 stree
= __pop_fake_cur_stree();
626 merge_stree(&final_states
, stree
);
628 __push_fake_cur_stree();
630 prev_func_id
= func_id
;
633 if (param
>= 0 && !get_param(param
, &name
, &sym
))
636 FOR_EACH_PTR(callbacks
, def_callback
) {
637 if (def_callback
->hook_type
== type
)
638 def_callback
->callback(name
, sym
, key
, value
);
639 } END_FOR_EACH_PTR(def_callback
);
644 static void get_direct_callers(struct symbol
*sym
)
646 sql_select_caller_info("call_id, type, parameter, key, value", sym
,
647 caller_info_callback
);
650 static struct string_list
*ptr_names_done
;
651 static struct string_list
*ptr_names
;
653 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
655 insert_string(&ptr_names
, alloc_string(argv
[0]));
659 static char *get_next_ptr_name(void)
663 FOR_EACH_PTR(ptr_names
, ptr
) {
664 if (list_has_string(ptr_names_done
, ptr
))
666 insert_string(&ptr_names_done
, ptr
);
668 } END_FOR_EACH_PTR(ptr
);
672 static void get_ptr_names(const char *file
, const char *name
)
674 char sql_filter
[1024];
678 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
681 snprintf(sql_filter
, 1024, "function = '%s';", name
);
684 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
686 run_sql(get_ptr_name
, NULL
,
687 "select distinct ptr from function_ptr where %s",
690 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
694 while ((name
= get_next_ptr_name()))
695 get_ptr_names(NULL
, name
);
698 static void match_data_from_db(struct symbol
*sym
)
703 if (!sym
|| !sym
->ident
)
706 __push_fake_cur_stree();
713 if (sym
->ctype
.modifiers
& MOD_STATIC
)
714 get_ptr_names(get_base_file(), sym
->ident
->name
);
716 get_ptr_names(NULL
, sym
->ident
->name
);
718 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
719 __free_ptr_list((struct ptr_list
**)&ptr_names
);
720 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
721 stree
= __pop_fake_cur_stree();
726 get_direct_callers(sym
);
728 FOR_EACH_PTR(ptr_names
, ptr
) {
729 run_sql(caller_info_callback
, NULL
,
730 "select call_id, type, parameter, key, value"
731 " from caller_info where function = '%s' order by call_id",
734 } END_FOR_EACH_PTR(ptr
);
736 __free_ptr_list((struct ptr_list
**)&ptr_names
);
737 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
739 get_direct_callers(sym
);
742 stree
= __pop_fake_cur_stree();
743 merge_stree(&final_states
, stree
);
746 FOR_EACH_SM(final_states
, sm
) {
748 } END_FOR_EACH_SM(sm
);
750 free_stree(&final_states
);
753 static int call_implies_callbacks(void *unused
, int argc
, char **argv
, char **azColName
)
755 struct expression
*call_expr
= static_call_expr
;
756 struct call_implies_callback
*cb
;
757 struct expression
*arg
= NULL
;
764 type
= atoi(argv
[1]);
765 param
= atoi(argv
[2]);
767 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
768 if (cb
->type
!= type
)
771 arg
= get_argument_from_call_expr(call_expr
->args
, param
);
775 cb
->callback(arg
, argv
[3], argv
[4]);
776 } END_FOR_EACH_PTR(cb
);
781 static void match_call_implies(struct expression
*expr
)
783 static_call_expr
= expr
;
784 sql_select_call_implies("function, type, parameter, key, value", expr
,
785 call_implies_callbacks
);
788 static void print_initializer_list(struct expression_list
*expr_list
,
789 struct symbol
*struct_type
)
791 struct expression
*expr
;
792 struct symbol
*base_type
;
793 char struct_name
[256];
795 FOR_EACH_PTR(expr_list
, expr
) {
796 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
797 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
800 if (expr
->type
!= EXPR_IDENTIFIER
)
802 if (!expr
->expr_ident
)
804 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
806 base_type
= get_type(expr
->ident_expression
);
807 if (!base_type
|| base_type
->type
!= SYM_FN
)
809 snprintf(struct_name
, sizeof(struct_name
), "(struct %s)->%s",
810 struct_type
->ident
->name
, expr
->expr_ident
->name
);
811 sql_insert_function_ptr(expr
->ident_expression
->symbol_name
->name
,
813 } END_FOR_EACH_PTR(expr
);
816 static void global_variable(struct symbol
*sym
)
818 struct symbol
*struct_type
;
822 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
824 struct_type
= get_base_type(sym
);
827 if (struct_type
->type
== SYM_ARRAY
) {
828 struct_type
= get_base_type(struct_type
);
832 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
834 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
837 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
839 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", "");
842 static void call_return_state_hooks_conditional(struct expression
*expr
)
844 struct returned_state_callback
*cb
;
845 struct range_list
*rl
;
847 int final_pass_orig
= final_pass
;
849 __push_fake_cur_stree();
852 __split_whole_condition(expr
->conditional
);
853 final_pass
= final_pass_orig
;
855 if (get_implied_rl(expr
->cond_true
, &rl
))
856 rl
= cast_rl(cur_func_return_type(), rl
);
858 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_true
)));
859 return_ranges
= show_rl(rl
);
862 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
863 cb
->callback(return_id
, return_ranges
, expr
);
864 } END_FOR_EACH_PTR(cb
);
866 __push_true_states();
867 __use_false_states();
869 if (get_implied_rl(expr
->cond_false
, &rl
))
870 rl
= cast_rl(cur_func_return_type(), rl
);
872 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_false
)));
873 return_ranges
= show_rl(rl
);
876 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
877 cb
->callback(return_id
, return_ranges
, expr
);
878 } END_FOR_EACH_PTR(cb
);
880 __merge_true_states();
881 __free_fake_cur_stree();
884 static void call_return_state_hooks_compare(struct expression
*expr
)
886 struct returned_state_callback
*cb
;
888 int final_pass_orig
= final_pass
;
890 __push_fake_cur_stree();
893 __split_whole_condition(expr
);
894 final_pass
= final_pass_orig
;
896 return_ranges
= alloc_sname("1");
899 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
900 cb
->callback(return_id
, return_ranges
, expr
);
901 } END_FOR_EACH_PTR(cb
);
903 __push_true_states();
904 __use_false_states();
906 return_ranges
= alloc_sname("0");;
908 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
909 cb
->callback(return_id
, return_ranges
, expr
);
910 } END_FOR_EACH_PTR(cb
);
912 __merge_true_states();
913 __free_fake_cur_stree();
916 static int split_helper(struct sm_state
*sm
, struct expression
*expr
)
918 struct returned_state_callback
*cb
;
919 struct range_list
*rl
;
921 struct sm_state
*tmp
;
923 int nr_possible
, nr_states
;
926 struct stree
*orig_stree
;
927 struct sm_state
*orig_sm
;
929 if (!sm
|| !sm
->merged
)
932 if (too_many_possible(sm
))
935 /* bail if it gets too complicated */
936 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
937 nr_states
= stree_count(__get_cur_stree());
939 * the main thing option_info because we don't want to print a
940 * million lines of output. If someone else, like check_locking.c
941 * wants this data, then it doesn't cause a slow down to provide it.
943 if (option_info
&& nr_states
* nr_possible
>= 2000)
946 compare_str
= expr_lte_to_param(expr
, -1);
948 orig_stree
= clone_stree(__get_cur_stree());
949 FOR_EACH_PTR(sm
->possible
, tmp
) {
954 __push_fake_cur_stree();
958 FOR_EACH_SM(orig_stree
, orig_sm
) {
959 __set_sm_cur_stree(orig_sm
);
960 } END_FOR_EACH_SM(orig_sm
);
962 overwrite_states_using_pool(tmp
);
964 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
965 return_ranges
= show_rl(rl
);
967 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
968 return_ranges
= alloc_sname(buf
);
972 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
973 cb
->callback(return_id
, return_ranges
, expr
);
974 } END_FOR_EACH_PTR(cb
);
976 __free_fake_cur_stree();
977 } END_FOR_EACH_PTR(tmp
);
979 free_stree(&orig_stree
);
983 static const char *get_return_ranges_str(struct expression
*expr
)
985 struct range_list
*rl
;
993 return alloc_sname("");
995 if (get_implied_value(expr
, &sval
))
996 return sval_to_str(sval
);
998 compare_str
= expr_equal_to_param(expr
, -1);
999 math_str
= get_value_in_terms_of_parameter_math(expr
);
1001 if (get_implied_rl(expr
, &rl
)) {
1002 rl
= cast_rl(cur_func_return_type(), rl
);
1003 return_ranges
= show_rl(rl
);
1004 } else if (get_imaginary_absolute(expr
, &rl
)){
1005 rl
= cast_rl(cur_func_return_type(), rl
);
1006 return alloc_sname(show_rl(rl
));
1008 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
)));
1009 return_ranges
= show_rl(rl
);
1013 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1014 return alloc_sname(buf
);
1018 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1019 return alloc_sname(buf
);
1022 compare_str
= expr_lte_to_param(expr
, -1);
1024 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1025 return alloc_sname(buf
);
1027 return return_ranges
;
1030 static int split_positive_from_negative(struct expression
*expr
)
1032 struct returned_state_callback
*cb
;
1033 struct range_list
*rl
;
1034 const char *return_ranges
;
1037 /* We're going to print the states 3 times */
1038 if (stree_count(__get_cur_stree()) > 10000 / 3)
1041 if (!get_implied_rl(expr
, &rl
) || !rl
)
1043 if (is_whole_rl(rl
) || is_whole_rl_non_zero(rl
))
1045 /* Forget about INT_MAX and larger */
1046 if (rl_max(rl
).value
<= 0)
1048 if (!sval_is_negative(rl_min(rl
)))
1051 if (!assume(compare_expression(expr
, '>', zero_expr())))
1055 return_ranges
= get_return_ranges_str(expr
);
1056 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1057 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1058 } END_FOR_EACH_PTR(cb
);
1062 if (rl_has_sval(rl
, sval_type_val(rl_type(rl
), 0))) {
1063 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
1066 return_ranges
= get_return_ranges_str(expr
);
1067 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1068 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1069 } END_FOR_EACH_PTR(cb
);
1075 undo
= assume(compare_expression(expr
, '<', zero_expr()));
1078 return_ranges
= get_return_ranges_str(expr
);
1079 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1080 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1081 } END_FOR_EACH_PTR(cb
);
1089 static int call_return_state_hooks_split_possible(struct expression
*expr
)
1091 struct returned_state_callback
*cb
;
1092 struct range_list
*rl
;
1093 char *return_ranges
;
1094 struct sm_state
*sm
;
1095 struct sm_state
*tmp
;
1097 int nr_possible
, nr_states
;
1101 if (!expr
|| expr_equal_to_param(expr
, -1))
1104 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1105 if (!sm
|| !sm
->merged
)
1108 if (too_many_possible(sm
))
1111 /* bail if it gets too complicated */
1112 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
1113 nr_states
= stree_count(__get_cur_stree());
1115 * the main thing option_info because we don't want to print a
1116 * million lines of output. If someone else, like check_locking.c
1117 * wants this data, then it doesn't cause a slow down to provide it.
1119 if (option_info
&& nr_states
* nr_possible
>= 2000)
1123 FOR_EACH_PTR(sm
->possible
, tmp
) {
1128 __push_fake_cur_stree();
1130 overwrite_states_using_pool(tmp
);
1132 if (split_positive_from_negative(expr
)) {
1133 __free_fake_cur_stree();
1136 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
1137 return_ranges
= show_rl(rl
);
1139 compare_str
= expr_lte_to_param(expr
, -1);
1141 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1142 return_ranges
= alloc_sname(buf
);
1146 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1147 cb
->callback(return_id
, return_ranges
, expr
);
1148 } END_FOR_EACH_PTR(cb
);
1150 __free_fake_cur_stree();
1151 } END_FOR_EACH_PTR(tmp
);
1156 static int call_return_state_hooks_split_null_non_null(struct expression
*expr
)
1158 struct returned_state_callback
*cb
;
1159 struct range_list
*rl
;
1160 char *return_ranges
;
1161 struct smatch_state
*state
;
1163 int final_pass_orig
= final_pass
;
1165 if (!expr
|| expr_equal_to_param(expr
, -1))
1167 if (expr
->type
== EXPR_CALL
)
1169 if (!is_pointer(expr
))
1172 state
= get_state_expr(SMATCH_EXTRA
, expr
);
1173 if (!state
|| !estate_rl(state
))
1175 if (estate_min(state
).value
== 0 && estate_max(state
).value
== 0)
1177 if (!rl_has_sval(estate_rl(state
), sval_type_val(estate_type(state
), 0)))
1180 nr_states
= stree_count(__get_cur_stree());
1181 if (option_info
&& nr_states
>= 1500)
1184 rl
= estate_rl(state
);
1186 __push_fake_cur_stree();
1189 __split_whole_condition(expr
);
1190 final_pass
= final_pass_orig
;
1192 return_ranges
= show_rl(rl_filter(rl
, rl_zero()));
1195 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1196 cb
->callback(return_id
, return_ranges
, expr
);
1197 } END_FOR_EACH_PTR(cb
);
1199 __push_true_states();
1200 __use_false_states();
1202 return_ranges
= alloc_sname("0");;
1204 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1205 cb
->callback(return_id
, return_ranges
, expr
);
1206 } END_FOR_EACH_PTR(cb
);
1208 __merge_true_states();
1209 __free_fake_cur_stree();
1214 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
1216 struct range_list
*rl
;
1218 struct returned_state_callback
*cb
;
1219 char *return_ranges
;
1220 int final_pass_orig
= final_pass
;
1223 if (option_project
!= PROJ_KERNEL
)
1226 nr_states
= stree_count(__get_cur_stree());
1227 if (nr_states
> 1500)
1230 if (get_value(expr
, &val
))
1232 if (!get_implied_rl(expr
, &rl
))
1234 if (rl_min(rl
).value
< -4095 || rl_min(rl
).value
>= 0)
1236 if (rl_max(rl
).value
!= 0)
1239 __push_fake_cur_stree();
1242 __split_whole_condition(expr
);
1243 final_pass
= final_pass_orig
;
1245 return_ranges
= show_rl(rl_filter(rl
, rl_zero()));
1248 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1249 cb
->callback(return_id
, return_ranges
, expr
);
1250 } END_FOR_EACH_PTR(cb
);
1252 __push_true_states();
1253 __use_false_states();
1255 return_ranges
= alloc_sname("0");;
1257 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1258 cb
->callback(return_id
, return_ranges
, expr
);
1259 } END_FOR_EACH_PTR(cb
);
1261 __merge_true_states();
1262 __free_fake_cur_stree();
1267 static int is_boolean(struct expression
*expr
)
1269 struct range_list
*rl
;
1271 if (!get_implied_rl(expr
, &rl
))
1273 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
1278 static int is_conditional(struct expression
*expr
)
1282 if (expr
->type
== EXPR_CONDITIONAL
|| expr
->type
== EXPR_SELECT
)
1287 static int splitable_function_call(struct expression
*expr
)
1289 struct sm_state
*sm
;
1292 if (!expr
|| expr
->type
!= EXPR_CALL
)
1294 snprintf(buf
, sizeof(buf
), "return %p", expr
);
1295 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
1296 return split_helper(sm
, expr
);
1299 static void call_return_state_hooks(struct expression
*expr
)
1301 struct returned_state_callback
*cb
;
1302 const char *return_ranges
;
1306 if (__path_is_null())
1309 expr
= strip_expr(expr
);
1311 if (is_impossible_path())
1314 if (!get_implied_value(expr
, &sval
) &&
1315 (is_condition(expr
) || is_boolean(expr
))) {
1316 call_return_state_hooks_compare(expr
);
1318 } else if (is_conditional(expr
)) {
1319 call_return_state_hooks_conditional(expr
);
1321 } else if (call_return_state_hooks_split_possible(expr
)) {
1323 } else if (call_return_state_hooks_split_null_non_null(expr
)) {
1325 } else if (call_return_state_hooks_split_success_fail(expr
)) {
1327 } else if (splitable_function_call(expr
)) {
1329 } else if (split_positive_from_negative(expr
)) {
1334 return_ranges
= get_return_ranges_str(expr
);
1337 nr_states
= stree_count(__get_cur_stree());
1338 if (nr_states
>= 10000) {
1339 match_return_info(return_id
, (char *)return_ranges
, expr
);
1342 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1343 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1344 } END_FOR_EACH_PTR(cb
);
1347 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
1349 struct returned_member_callback
*cb
;
1350 struct stree
*stree
;
1351 struct sm_state
*sm
;
1352 struct symbol
*type
;
1354 char member_name
[256];
1357 type
= get_type(expr
);
1358 if (!type
|| type
->type
!= SYM_PTR
)
1360 name
= expr_to_var(expr
);
1364 member_name
[sizeof(member_name
) - 1] = '\0';
1365 strcpy(member_name
, "$");
1368 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
1369 stree
= __get_cur_stree();
1370 FOR_EACH_MY_SM(cb
->owner
, stree
, sm
) {
1371 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
1372 strcpy(member_name
, "*$");
1373 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1376 if (strncmp(sm
->name
, name
, len
) != 0)
1378 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
1380 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
1381 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1382 } END_FOR_EACH_SM(sm
);
1383 } END_FOR_EACH_PTR(cb
);
1388 static void reset_memdb(void)
1390 mem_sql(NULL
, NULL
, "delete from caller_info;");
1391 mem_sql(NULL
, NULL
, "delete from return_states;");
1392 mem_sql(NULL
, NULL
, "delete from call_implies;");
1395 static void match_end_func_info(struct symbol
*sym
)
1397 if (__path_is_null())
1399 call_return_state_hooks(NULL
);
1404 static void init_memdb(void)
1408 const char *schema_files
[] = {
1410 "db/caller_info.schema",
1411 "db/return_states.schema",
1412 "db/function_type_size.schema",
1413 "db/type_size.schema",
1414 "db/call_implies.schema",
1415 "db/function_ptr.schema",
1416 "db/local_values.schema",
1417 "db/function_type_value.schema",
1418 "db/type_value.schema",
1419 "db/function_type_info.schema",
1420 "db/data_info.schema",
1422 static char buf
[4096];
1427 rc
= sqlite3_open(":memory:", &mem_db
);
1428 if (rc
!= SQLITE_OK
) {
1429 printf("Error starting In-Memory database.");
1433 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
1434 fd
= open_data_file(schema_files
[i
]);
1439 ret
= read(fd
, buf
, sizeof(buf
));
1440 if (ret
== sizeof(buf
)) {
1441 printf("Schema file too large: %s (limit %zd bytes)",
1442 schema_files
[i
], sizeof(buf
));
1445 rc
= sqlite3_exec(mem_db
, buf
, NULL
, 0, &err
);
1446 if (rc
!= SQLITE_OK
) {
1447 fprintf(stderr
, "SQL error #2: %s\n", err
);
1448 fprintf(stderr
, "%s\n", buf
);
1453 void open_smatch_db(void)
1462 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
1463 if (rc
!= SQLITE_OK
) {
1470 static void register_common_funcs(void)
1472 struct token
*token
;
1476 if (option_project
== PROJ_NONE
)
1477 strcpy(filename
, "common_functions");
1479 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
1481 token
= get_tokens_file(filename
);
1484 if (token_type(token
) != TOKEN_STREAMBEGIN
)
1486 token
= token
->next
;
1487 while (token_type(token
) != TOKEN_STREAMEND
) {
1488 if (token_type(token
) != TOKEN_IDENT
)
1490 func
= alloc_string(show_ident(token
->ident
));
1491 add_ptr_list(&common_funcs
, func
);
1492 token
= token
->next
;
1494 clear_token_alloc();
1498 void register_definition_db_callbacks(int id
)
1500 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1501 add_hook(&global_variable
, BASE_HOOK
);
1502 add_hook(&global_variable
, DECLARATION_HOOK
);
1503 add_split_return_callback(match_return_info
);
1504 add_split_return_callback(print_returned_struct_members
);
1505 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
1506 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
1508 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
1509 add_hook(&match_call_implies
, CALL_HOOK_AFTER_INLINE
);
1511 register_common_funcs();
1514 void register_db_call_marker(int id
)
1516 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
1519 char *return_state_to_var_sym(struct expression
*expr
, int param
, char *key
, struct symbol
**sym
)
1521 struct expression
*arg
;
1523 char member_name
[256];
1528 const char *star
= "";
1530 if (expr
->type
!= EXPR_ASSIGNMENT
)
1532 name
= expr_to_var_sym(expr
->left
, sym
);
1535 if (key
[0] == '*') {
1539 if (strncmp(key
, "$", 1) != 0)
1541 snprintf(member_name
, sizeof(member_name
), "%s%s%s", star
, name
, key
+ 1);
1543 return alloc_string(member_name
);
1546 while (expr
->type
== EXPR_ASSIGNMENT
)
1547 expr
= strip_expr(expr
->right
);
1548 if (expr
->type
!= EXPR_CALL
)
1551 arg
= get_argument_from_call_expr(expr
->args
, param
);
1555 return get_variable_from_key(arg
, key
, sym
);
1558 char *get_variable_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
)
1566 arg
= strip_expr(arg
);
1568 if (strcmp(key
, "$") == 0)
1569 return expr_to_var_sym(arg
, sym
);
1571 if (strcmp(key
, "*$") == 0) {
1572 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1573 arg
= strip_expr(arg
->unop
);
1574 return expr_to_var_sym(arg
, sym
);
1576 tmp
= expr_to_var_sym(arg
, sym
);
1579 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
1581 return alloc_string(buf
);
1585 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1586 arg
= strip_expr(arg
->unop
);
1587 tmp
= expr_to_var_sym(arg
, sym
);
1590 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 3);
1591 return alloc_string(buf
);
1594 tmp
= expr_to_var_sym(arg
, sym
);
1597 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 1);
1599 return alloc_string(buf
);
1602 const char *get_param_name(struct sm_state
*sm
)
1606 static char buf
[256];
1608 if (!sm
->sym
|| !sm
->sym
->ident
)
1611 param_name
= sm
->sym
->ident
->name
;
1612 name_len
= strlen(param_name
);
1614 if (strcmp(sm
->name
, param_name
) == 0) {
1616 } else if (sm
->name
[name_len
] == '-' && /* check for '-' from "->" */
1617 strncmp(sm
->name
, param_name
, name_len
) == 0) {
1618 snprintf(buf
, sizeof(buf
), "$%s", sm
->name
+ name_len
);
1620 } else if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, param_name
) == 0) {
1626 char *get_data_info_name(struct expression
*expr
)
1633 expr
= strip_expr(expr
);
1634 name
= get_member_name(expr
);
1637 name
= expr_to_var_sym(expr
, &sym
);
1640 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
1642 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1643 snprintf(buf
, sizeof(buf
), "static %s", name
);
1645 snprintf(buf
, sizeof(buf
), "global %s", name
);
1646 ret
= alloc_sname(buf
);