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
);
860 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(rl
));
863 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
864 cb
->callback(return_id
, return_ranges
, expr
);
865 } END_FOR_EACH_PTR(cb
);
867 __push_true_states();
868 __use_false_states();
870 if (get_implied_rl(expr
->cond_false
, &rl
))
871 rl
= cast_rl(cur_func_return_type(), rl
);
873 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_false
)));
874 return_ranges
= show_rl(rl
);
875 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(rl
));
878 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
879 cb
->callback(return_id
, return_ranges
, expr
);
880 } END_FOR_EACH_PTR(cb
);
882 __merge_true_states();
883 __free_fake_cur_stree();
886 static void call_return_state_hooks_compare(struct expression
*expr
)
888 struct returned_state_callback
*cb
;
890 int final_pass_orig
= final_pass
;
891 sval_t sval
= { .type
= &int_ctype
};
893 __push_fake_cur_stree();
896 __split_whole_condition(expr
);
897 final_pass
= final_pass_orig
;
899 return_ranges
= alloc_sname("1");
901 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
904 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
905 cb
->callback(return_id
, return_ranges
, expr
);
906 } END_FOR_EACH_PTR(cb
);
908 __push_true_states();
909 __use_false_states();
911 return_ranges
= alloc_sname("0");
913 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
915 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
916 cb
->callback(return_id
, return_ranges
, expr
);
917 } END_FOR_EACH_PTR(cb
);
919 __merge_true_states();
920 __free_fake_cur_stree();
923 static int ptr_in_list(struct sm_state
*sm
, struct state_list
*slist
)
925 struct sm_state
*tmp
;
927 FOR_EACH_PTR(slist
, tmp
) {
928 if (strcmp(tmp
->state
->name
, sm
->state
->name
) == 0)
930 } END_FOR_EACH_PTR(tmp
);
935 static int split_possible_helper(struct sm_state
*sm
, struct expression
*expr
)
937 struct returned_state_callback
*cb
;
938 struct range_list
*rl
;
940 struct sm_state
*tmp
;
942 int nr_possible
, nr_states
;
945 struct state_list
*already_handled
= NULL
;
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());
956 if (option_info
&& nr_states
* nr_possible
>= 2000)
959 FOR_EACH_PTR(sm
->possible
, tmp
) {
962 if (ptr_in_list(tmp
, already_handled
))
964 add_ptr_list(&already_handled
, tmp
);
967 __push_fake_cur_stree();
969 overwrite_states_using_pool(sm
, tmp
);
971 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
972 return_ranges
= show_rl(rl
);
973 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(clone_rl(rl
)));
974 compare_str
= expr_lte_to_param(expr
, -1);
976 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
977 return_ranges
= alloc_sname(buf
);
981 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
982 cb
->callback(return_id
, return_ranges
, expr
);
983 } END_FOR_EACH_PTR(cb
);
985 __free_fake_cur_stree();
986 } END_FOR_EACH_PTR(tmp
);
988 free_slist(&already_handled
);
993 static int call_return_state_hooks_split_possible(struct expression
*expr
)
997 if (!expr
|| expr_equal_to_param(expr
, -1))
1000 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1001 return split_possible_helper(sm
, expr
);
1004 static const char *get_return_ranges_str(struct expression
*expr
, struct range_list
**rl_p
)
1006 struct range_list
*rl
;
1007 char *return_ranges
;
1016 return alloc_sname("");
1018 if (get_implied_value(expr
, &sval
)) {
1019 *rl_p
= alloc_rl(sval
, sval
);
1020 return sval_to_str(sval
);
1023 compare_str
= expr_equal_to_param(expr
, -1);
1024 math_str
= get_value_in_terms_of_parameter_math(expr
);
1026 if (get_implied_rl(expr
, &rl
)) {
1027 rl
= cast_rl(cur_func_return_type(), rl
);
1028 return_ranges
= show_rl(rl
);
1029 } else if (get_imaginary_absolute(expr
, &rl
)){
1030 rl
= cast_rl(cur_func_return_type(), rl
);
1031 return alloc_sname(show_rl(rl
));
1033 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
)));
1034 return_ranges
= show_rl(rl
);
1039 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1040 return alloc_sname(buf
);
1044 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1045 return alloc_sname(buf
);
1048 compare_str
= expr_lte_to_param(expr
, -1);
1050 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1051 return alloc_sname(buf
);
1053 return return_ranges
;
1056 static int split_positive_from_negative(struct expression
*expr
)
1058 struct returned_state_callback
*cb
;
1059 struct range_list
*rl
;
1060 const char *return_ranges
;
1061 struct range_list
*ret_rl
;
1064 /* We're going to print the states 3 times */
1065 if (stree_count(__get_cur_stree()) > 10000 / 3)
1068 if (!get_implied_rl(expr
, &rl
) || !rl
)
1070 if (is_whole_rl(rl
) || is_whole_rl_non_zero(rl
))
1072 /* Forget about INT_MAX and larger */
1073 if (rl_max(rl
).value
<= 0)
1075 if (!sval_is_negative(rl_min(rl
)))
1078 if (!assume(compare_expression(expr
, '>', zero_expr())))
1082 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1083 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1084 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1085 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1086 } END_FOR_EACH_PTR(cb
);
1090 if (rl_has_sval(rl
, sval_type_val(rl_type(rl
), 0))) {
1091 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
1094 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1095 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1096 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1097 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1098 } END_FOR_EACH_PTR(cb
);
1104 undo
= assume(compare_expression(expr
, '<', zero_expr()));
1107 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1108 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1109 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1110 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1111 } END_FOR_EACH_PTR(cb
);
1119 static int call_return_state_hooks_split_null_non_null(struct expression
*expr
)
1121 struct returned_state_callback
*cb
;
1122 struct range_list
*rl
;
1123 struct range_list
*nonnull_rl
;
1125 struct range_list
*null_rl
= NULL
;
1126 char *return_ranges
;
1127 struct smatch_state
*state
;
1129 int final_pass_orig
= final_pass
;
1131 if (!expr
|| expr_equal_to_param(expr
, -1))
1133 if (expr
->type
== EXPR_CALL
)
1135 if (!is_pointer(expr
))
1138 state
= get_state_expr(SMATCH_EXTRA
, expr
);
1139 if (!state
|| !estate_rl(state
))
1141 if (estate_min(state
).value
== 0 && estate_max(state
).value
== 0)
1143 if (!rl_has_sval(estate_rl(state
), sval_type_val(estate_type(state
), 0)))
1146 nr_states
= stree_count(__get_cur_stree());
1147 if (option_info
&& nr_states
>= 1500)
1150 rl
= estate_rl(state
);
1152 __push_fake_cur_stree();
1155 __split_whole_condition(expr
);
1156 final_pass
= final_pass_orig
;
1158 nonnull_rl
= rl_filter(rl
, rl_zero());
1159 return_ranges
= show_rl(nonnull_rl
);
1160 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonnull_rl
));
1163 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1164 cb
->callback(return_id
, return_ranges
, expr
);
1165 } END_FOR_EACH_PTR(cb
);
1167 __push_true_states();
1168 __use_false_states();
1170 return_ranges
= alloc_sname("0");
1171 null_sval
= sval_type_val(rl_type(rl
), 0);
1172 add_range(&null_rl
, null_sval
, null_sval
);
1173 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(null_rl
));
1175 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1176 cb
->callback(return_id
, return_ranges
, expr
);
1177 } END_FOR_EACH_PTR(cb
);
1179 __merge_true_states();
1180 __free_fake_cur_stree();
1185 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
1187 struct range_list
*rl
;
1188 struct range_list
*nonzero_rl
;
1190 struct range_list
*zero_rl
= NULL
;
1192 struct returned_state_callback
*cb
;
1193 char *return_ranges
;
1194 int final_pass_orig
= final_pass
;
1197 if (option_project
!= PROJ_KERNEL
)
1200 nr_states
= stree_count(__get_cur_stree());
1201 if (nr_states
> 1500)
1204 if (get_value(expr
, &val
))
1206 if (!get_implied_rl(expr
, &rl
))
1208 if (rl_min(rl
).value
< -4095 || rl_min(rl
).value
>= 0)
1210 if (rl_max(rl
).value
!= 0)
1213 __push_fake_cur_stree();
1216 __split_whole_condition(expr
);
1217 final_pass
= final_pass_orig
;
1219 nonzero_rl
= rl_filter(rl
, rl_zero());
1220 return_ranges
= show_rl(nonzero_rl
);
1221 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonzero_rl
));
1224 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1225 cb
->callback(return_id
, return_ranges
, expr
);
1226 } END_FOR_EACH_PTR(cb
);
1228 __push_true_states();
1229 __use_false_states();
1231 return_ranges
= alloc_sname("0");
1232 zero_sval
= sval_type_val(rl_type(rl
), 0);
1233 add_range(&zero_rl
, zero_sval
, zero_sval
);
1234 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(zero_rl
));
1236 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1237 cb
->callback(return_id
, return_ranges
, expr
);
1238 } END_FOR_EACH_PTR(cb
);
1240 __merge_true_states();
1241 __free_fake_cur_stree();
1246 static int is_boolean(struct expression
*expr
)
1248 struct range_list
*rl
;
1250 if (!get_implied_rl(expr
, &rl
))
1252 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
1257 static int is_conditional(struct expression
*expr
)
1261 if (expr
->type
== EXPR_CONDITIONAL
|| expr
->type
== EXPR_SELECT
)
1266 static int splitable_function_call(struct expression
*expr
)
1268 struct sm_state
*sm
;
1271 if (!expr
|| expr
->type
!= EXPR_CALL
)
1273 snprintf(buf
, sizeof(buf
), "return %p", expr
);
1274 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
1275 return split_possible_helper(sm
, expr
);
1278 static void call_return_state_hooks(struct expression
*expr
)
1280 struct returned_state_callback
*cb
;
1281 struct range_list
*ret_rl
;
1282 const char *return_ranges
;
1286 if (__path_is_null())
1289 expr
= strip_expr(expr
);
1291 if (is_impossible_path())
1294 if (!get_implied_value(expr
, &sval
) &&
1295 (is_condition(expr
) || is_boolean(expr
))) {
1296 call_return_state_hooks_compare(expr
);
1298 } else if (is_conditional(expr
)) {
1299 call_return_state_hooks_conditional(expr
);
1301 } else if (call_return_state_hooks_split_possible(expr
)) {
1303 } else if (call_return_state_hooks_split_null_non_null(expr
)) {
1305 } else if (call_return_state_hooks_split_success_fail(expr
)) {
1307 } else if (splitable_function_call(expr
)) {
1309 } else if (split_positive_from_negative(expr
)) {
1314 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1315 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1318 nr_states
= stree_count(__get_cur_stree());
1319 if (nr_states
>= 10000) {
1320 match_return_info(return_id
, (char *)return_ranges
, expr
);
1323 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1324 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1325 } END_FOR_EACH_PTR(cb
);
1328 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
1330 struct returned_member_callback
*cb
;
1331 struct stree
*stree
;
1332 struct sm_state
*sm
;
1333 struct symbol
*type
;
1335 char member_name
[256];
1338 type
= get_type(expr
);
1339 if (!type
|| type
->type
!= SYM_PTR
)
1341 name
= expr_to_var(expr
);
1345 member_name
[sizeof(member_name
) - 1] = '\0';
1346 strcpy(member_name
, "$");
1349 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
1350 stree
= __get_cur_stree();
1351 FOR_EACH_MY_SM(cb
->owner
, stree
, sm
) {
1352 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
1353 strcpy(member_name
, "*$");
1354 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1357 if (strncmp(sm
->name
, name
, len
) != 0)
1359 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
1361 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
1362 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1363 } END_FOR_EACH_SM(sm
);
1364 } END_FOR_EACH_PTR(cb
);
1369 static void reset_memdb(struct symbol
*sym
)
1371 mem_sql(NULL
, NULL
, "delete from caller_info;");
1372 mem_sql(NULL
, NULL
, "delete from return_states;");
1373 mem_sql(NULL
, NULL
, "delete from call_implies;");
1376 static void match_end_func_info(struct symbol
*sym
)
1378 if (__path_is_null())
1380 call_return_state_hooks(NULL
);
1383 static void match_after_func(struct symbol
*sym
)
1389 static void init_memdb(void)
1393 const char *schema_files
[] = {
1395 "db/caller_info.schema",
1396 "db/return_states.schema",
1397 "db/function_type_size.schema",
1398 "db/type_size.schema",
1399 "db/call_implies.schema",
1400 "db/function_ptr.schema",
1401 "db/local_values.schema",
1402 "db/function_type_value.schema",
1403 "db/type_value.schema",
1404 "db/function_type_info.schema",
1405 "db/data_info.schema",
1407 static char buf
[4096];
1412 rc
= sqlite3_open(":memory:", &mem_db
);
1413 if (rc
!= SQLITE_OK
) {
1414 printf("Error starting In-Memory database.");
1418 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
1419 fd
= open_data_file(schema_files
[i
]);
1424 ret
= read(fd
, buf
, sizeof(buf
));
1425 if (ret
== sizeof(buf
)) {
1426 printf("Schema file too large: %s (limit %zd bytes)",
1427 schema_files
[i
], sizeof(buf
));
1430 rc
= sqlite3_exec(mem_db
, buf
, NULL
, 0, &err
);
1431 if (rc
!= SQLITE_OK
) {
1432 fprintf(stderr
, "SQL error #2: %s\n", err
);
1433 fprintf(stderr
, "%s\n", buf
);
1438 void open_smatch_db(void)
1447 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
1448 if (rc
!= SQLITE_OK
) {
1455 static void register_common_funcs(void)
1457 struct token
*token
;
1461 if (option_project
== PROJ_NONE
)
1462 strcpy(filename
, "common_functions");
1464 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
1466 token
= get_tokens_file(filename
);
1469 if (token_type(token
) != TOKEN_STREAMBEGIN
)
1471 token
= token
->next
;
1472 while (token_type(token
) != TOKEN_STREAMEND
) {
1473 if (token_type(token
) != TOKEN_IDENT
)
1475 func
= alloc_string(show_ident(token
->ident
));
1476 add_ptr_list(&common_funcs
, func
);
1477 token
= token
->next
;
1479 clear_token_alloc();
1483 void register_definition_db_callbacks(int id
)
1485 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1486 add_hook(&global_variable
, BASE_HOOK
);
1487 add_hook(&global_variable
, DECLARATION_HOOK
);
1488 add_split_return_callback(match_return_info
);
1489 add_split_return_callback(print_returned_struct_members
);
1490 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
1491 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
1492 add_hook(&match_after_func
, AFTER_FUNC_HOOK
);
1494 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
1495 add_hook(&match_call_implies
, CALL_HOOK_AFTER_INLINE
);
1497 register_common_funcs();
1500 void register_db_call_marker(int id
)
1502 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
1505 char *return_state_to_var_sym(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
)
1507 struct expression
*arg
;
1509 char member_name
[256];
1514 const char *star
= "";
1516 if (expr
->type
!= EXPR_ASSIGNMENT
)
1518 name
= expr_to_var_sym(expr
->left
, sym
);
1521 if (key
[0] == '*') {
1525 if (strncmp(key
, "$", 1) != 0)
1527 snprintf(member_name
, sizeof(member_name
), "%s%s%s", star
, name
, key
+ 1);
1529 return alloc_string(member_name
);
1532 while (expr
->type
== EXPR_ASSIGNMENT
)
1533 expr
= strip_expr(expr
->right
);
1534 if (expr
->type
!= EXPR_CALL
)
1537 arg
= get_argument_from_call_expr(expr
->args
, param
);
1541 return get_variable_from_key(arg
, key
, sym
);
1544 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
)
1552 arg
= strip_expr(arg
);
1554 if (strcmp(key
, "$") == 0)
1555 return expr_to_var_sym(arg
, sym
);
1557 if (strcmp(key
, "*$") == 0) {
1558 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1559 arg
= strip_expr(arg
->unop
);
1560 return expr_to_var_sym(arg
, sym
);
1562 tmp
= expr_to_var_sym(arg
, sym
);
1565 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
1567 return alloc_string(buf
);
1571 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1572 arg
= strip_expr(arg
->unop
);
1573 tmp
= expr_to_var_sym(arg
, sym
);
1576 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 3);
1577 return alloc_string(buf
);
1580 tmp
= expr_to_var_sym(arg
, sym
);
1583 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 1);
1585 return alloc_string(buf
);
1588 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
)
1592 if (strcmp("$", key
) == 0)
1593 return expr_to_chunk_sym_vsl(arg
, sym
, vsl
);
1594 return get_variable_from_key(arg
, key
, sym
);
1597 const char *get_param_name_var_sym(const char *name
, struct symbol
*sym
)
1601 static char buf
[256];
1603 if (!sym
|| !sym
->ident
)
1606 param_name
= sym
->ident
->name
;
1607 name_len
= strlen(param_name
);
1609 if (strcmp(name
, param_name
) == 0) {
1611 } else if (name
[name_len
] == '-' && /* check for '-' from "->" */
1612 strncmp(name
, param_name
, name_len
) == 0) {
1613 snprintf(buf
, sizeof(buf
), "$%s", name
+ name_len
);
1615 } else if (name
[0] == '*' && strcmp(name
+ 1, param_name
) == 0) {
1621 const char *get_param_name(struct sm_state
*sm
)
1623 return get_param_name_var_sym(sm
->name
, sm
->sym
);
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
);