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"
26 struct sqlite3
*smatch_db
;
27 struct sqlite3
*mem_db
;
28 struct sqlite3
*cache_db
;
37 static void call_return_state_hooks(struct expression
*expr
);
38 static void call_return_states_callbacks(const char *return_ranges
, struct expression
*expr
);
40 #define SQLITE_CACHE_PAGES 1000
44 void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
);
46 ALLOCATOR(def_callback
, "definition db hook callbacks");
47 DECLARE_PTR_LIST(callback_list
, struct def_callback
);
48 static struct callback_list
*select_caller_info_callbacks
;
50 struct def_name_sym_callback
{
52 void (*callback
)(const char *name
, struct symbol
*sym
, char *value
);
54 ALLOCATOR(def_name_sym_callback
, "definition db hook callbacks");
55 DECLARE_PTR_LIST(name_sym_callback_list
, struct def_name_sym_callback
);
56 static struct name_sym_callback_list
*select_caller_name_sym_callbacks
;
58 struct member_info_callback
{
60 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
);
62 ALLOCATOR(member_info_callback
, "caller_info callbacks");
63 DECLARE_PTR_LIST(member_info_cb_list
, struct member_info_callback
);
64 static struct member_info_cb_list
*member_callbacks
;
65 static struct member_info_cb_list
*member_callbacks_new
;
67 struct return_info_callback
{
69 void (*callback
)(int return_id
, char *return_ranges
,
70 struct expression
*returned_expr
,
72 const char *printed_name
,
75 ALLOCATOR(return_info_callback
, "return_info callbacks");
76 DECLARE_PTR_LIST(return_info_cb_list
, struct return_info_callback
);
77 static struct return_info_cb_list
*return_callbacks
;
79 struct returned_state_callback
{
80 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*return_expr
);
82 ALLOCATOR(returned_state_callback
, "returned state callbacks");
83 DECLARE_PTR_LIST(returned_state_cb_list
, struct returned_state_callback
);
84 static struct returned_state_cb_list
*returned_state_callbacks
;
86 struct returned_member_callback
{
88 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*expr
, char *printed_name
, struct smatch_state
*state
);
90 ALLOCATOR(returned_member_callback
, "returned member callbacks");
91 DECLARE_PTR_LIST(returned_member_cb_list
, struct returned_member_callback
);
92 static struct returned_member_cb_list
*returned_member_callbacks
;
94 struct db_implies_callback
{
96 void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
);
98 ALLOCATOR(db_implies_callback
, "return_implies callbacks");
99 DECLARE_PTR_LIST(db_implies_cb_list
, struct db_implies_callback
);
100 static struct db_implies_cb_list
*return_implies_cb_list_early
;
101 static struct db_implies_cb_list
*return_implies_cb_list_late
;
102 static struct db_implies_cb_list
*call_implies_cb_list
;
104 DECLARE_PTR_LIST(delete_list
, delete_hook
);
105 static struct delete_list
*delete_hooks
;
108 const char *func
, *rl
;
110 static struct split_data
**forced_splits
;
111 static int split_count
;
113 /* silently truncates if needed. */
114 char *escape_newlines(const char *str
)
120 for (i
= 0, j
= 0; str
[i
] != '\0' && j
!= sizeof(buf
); i
++, j
++) {
121 if (str
[i
] != '\r' && str
[i
] != '\n') {
128 if (j
== sizeof(buf
))
134 return alloc_sname(str
);
136 if (j
== sizeof(buf
))
138 return alloc_sname(buf
);
141 static int print_sql_output(void *unused
, int argc
, char **argv
, char **azColName
)
145 for (i
= 0; i
< argc
; i
++) {
148 sm_printf("%s", argv
[i
]);
154 void sql_exec(struct sqlite3
*db
, int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
)
162 if (option_debug
|| debug_db
) {
164 if (strncasecmp(sql
, "select", strlen("select")) == 0)
165 sqlite3_exec(db
, sql
, print_sql_output
, NULL
, NULL
);
168 rc
= sqlite3_exec(db
, sql
, callback
, data
, &err
);
169 if (rc
!= SQLITE_OK
&& !parse_error
) {
170 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err
);
171 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql
);
176 static int replace_count
;
177 static char **replace_table
;
178 static const char *replace_return_ranges(const char *return_ranges
)
182 if (!get_function()) {
183 /* I have no idea why EXPORT_SYMBOL() is here */
184 return return_ranges
;
186 for (i
= 0; i
< replace_count
; i
+= 3) {
187 if (strcmp(replace_table
[i
+ 0], get_function()) == 0) {
188 if (strcmp(replace_table
[i
+ 1], return_ranges
) == 0)
189 return replace_table
[i
+ 2];
192 return return_ranges
;
195 static int delete_count
;
196 static char **delete_table
;
197 static bool is_delete_return(const char *return_ranges
)
204 for (i
= 0; i
< delete_count
; i
+= 2) {
205 if (strcmp(delete_table
[i
], get_function()) == 0 &&
206 strcmp(delete_table
[i
+ 1], return_ranges
) == 0)
213 void add_delete_return_hook(delete_hook
*hook
)
215 add_ptr_list(&delete_hooks
, hook
);
218 static bool is_project_delete_return(struct expression
*expr
)
222 FOR_EACH_PTR(delete_hooks
, hook
) {
225 } END_FOR_EACH_PTR(hook
);
229 static char *use_states
;
230 static int get_db_state_count(void)
235 FOR_EACH_SM(__get_cur_stree(), sm
) {
236 if (sm
->owner
== USHRT_MAX
)
238 if (use_states
[sm
->owner
])
240 } END_FOR_EACH_SM(sm
);
244 static bool in_base_file(struct symbol
*sym
)
246 return sym
->pos
.stream
== base_file_stream
;
249 static bool is_local(struct symbol
*sym
)
251 if (sym
->ctype
.modifiers
& MOD_STATIC
)
253 if ((sym
->ctype
.modifiers
& MOD_EXTERN
) &&
254 (sym
->ctype
.modifiers
& MOD_INLINE
) &&
258 if (!sym
->definition
)
261 if ((sym
->definition
->ctype
.modifiers
& MOD_EXTERN
) &&
262 (sym
->definition
->ctype
.modifiers
& MOD_INLINE
) &&
263 !in_base_file(sym
->definition
))
269 void db_ignore_states(int id
)
274 unsigned long long __fn_mtag
;
275 static void set_fn_mtag(struct symbol
*sym
)
279 if (is_local(cur_func_sym
))
280 snprintf(buf
, sizeof(buf
), "%s %s", get_base_file(), get_function());
282 snprintf(buf
, sizeof(buf
), "extern %s", get_function());
284 __fn_mtag
= str_to_mtag(buf
);
287 void sql_insert_return_states(int return_id
, const char *return_ranges
,
288 int type
, int param
, const char *key
, const char *value
)
290 unsigned long long id
;
293 if (key
&& strlen(key
) >= 80)
296 id
= (unsigned long)__inline_fn
;
300 sql_insert(return_states
, "0x%llx, '%s', %llu, %d, '%s', %d, %d, %d, '%s', '%s'",
301 get_base_file_id(), get_function(), id
, return_id
,
302 return_ranges
, is_local(cur_func_sym
), type
, param
, key
, value
);
305 static struct string_list
*common_funcs
;
306 static int is_common_function(const char *fn
)
313 if (strncmp(fn
, "__builtin_", 10) == 0)
316 FOR_EACH_PTR(common_funcs
, tmp
) {
317 if (strcmp(tmp
, fn
) == 0)
319 } END_FOR_EACH_PTR(tmp
);
324 static char *function_signature(void)
326 return type_to_str(get_real_base_type(cur_func_sym
));
329 void sql_insert_caller_info(struct expression
*call
, int type
,
330 int param
, const char *key
, const char *value
)
332 FILE *tmp_fd
= sm_outfd
;
335 if (!option_info
&& !__inline_call
)
340 if (key
&& strlen(key
) >= 80)
343 fn
= get_fnptr_name(call
->fn
);
349 "insert into caller_info values (0x%llx, '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
350 get_base_file_id(), get_function(), fn
, (unsigned long)call
,
351 is_static(call
->fn
), type
, param
, key
, value
);
357 if (strncmp(fn
, "__builtin_", 10) == 0)
359 if (type
!= INTERNAL
&& is_common_function(fn
))
362 sm_outfd
= caller_info_fd
;
363 sm_msg("SQL_caller_info: insert into caller_info values ("
364 "0x%llx, '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
365 get_base_file_id(), get_function(), fn
, is_static(call
->fn
),
366 type
, param
, key
, value
);
372 void sql_insert_function_ptr(const char *fn
, const char *struct_name
)
374 sql_insert_or_ignore(function_ptr
, "0x%llx, '%s', '%s', 0",
375 get_base_file_id(), fn
, struct_name
);
378 void sql_insert_return_implies(int type
, int param
, const char *key
, const char *value
)
380 unsigned long long id
;
383 id
= (unsigned long)__inline_fn
;
387 sql_insert_or_ignore(return_implies
, "0x%llx, '%s', %llu, %d, %d, %d, '%s', '%s'",
388 get_base_file_id(), get_function(), id
, fn_static(), type
,
392 void sql_insert_call_implies(int type
, int param
, const char *key
, const char *value
)
394 sql_insert_or_ignore(call_implies
, "0x%llx, '%s', %lu, %d, %d, %d, '%s', '%s'",
395 get_base_file_id(), get_function(), (unsigned long)__inline_fn
,
396 fn_static(), type
, param
, key
, value
);
399 void sql_insert_function_type_size(const char *member
, const char *ranges
)
401 sql_insert(function_type_size
, "0x%llx, '%s', '%s', '%s'", get_base_file_id(), get_function(), member
, ranges
);
404 void sql_insert_function_type_info(int type
, const char *struct_type
, const char *member
, const char *value
)
406 sql_insert(function_type_info
, "0x%llx, '%s', %d, '%s', '%s', '%s'", get_base_file_id(), get_function(), type
, struct_type
, member
, value
);
409 void sql_insert_type_info(int type
, const char *member
, const char *value
)
411 sql_insert_cache(type_info
, "0x%llx, %d, '%s', '%s'", get_base_file_id(), type
, member
, value
);
414 void sql_insert_local_values(const char *name
, const char *value
)
416 sql_insert(local_values
, "0x%llx, '%s', '%s'", get_base_file_id(), name
, value
);
419 void sql_insert_function_type_value(const char *type
, const char *value
)
421 sql_insert(function_type_value
, "0x%llx, '%s', '%s', '%s'", get_base_file_id(), get_function(), type
, value
);
424 void sql_insert_function_type(int param
, const char *value
)
426 sql_insert(function_type
, "0x%llx, '%s', %d, %d, '%s'",
427 get_base_file_id(), get_function(), fn_static(), param
, value
);
430 void sql_insert_parameter_name(int param
, const char *value
)
432 sql_insert(parameter_name
, "0x%llx, '%s', %d, %d, '%s'",
433 get_base_file_id(), get_function(), fn_static(), param
, value
);
436 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
)
440 data_name
= get_data_info_name(data
);
443 sql_insert(data_info
, "0x%llx, '%s', %d, '%s'",
444 is_static(data
) ? get_base_file_id() : 0,
445 data_name
, type
, value
);
448 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
)
450 sql_insert(data_info
, "0x%llx, '%s', %d, '%s'",
451 (sym
->ctype
.modifiers
& MOD_STATIC
) ? get_base_file_id() : 0,
455 void sql_save_constraint(const char *con
)
460 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con
));
463 void sql_save_constraint_required(const char *data
, int op
, const char *limit
)
465 sql_insert_or_ignore(constraints_required
, "'%s', '%s', '%s'", data
, show_special(op
), limit
);
468 void sql_copy_constraint_required(const char *new_limit
, const char *old_limit
)
473 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
474 "select constraints_required.data, constraints_required.op, '%s' from "
475 "constraints_required where bound = '%s';", new_limit
, old_limit
);
478 void sql_insert_fn_ptr_data_link(const char *ptr
, const char *data
)
480 sql_insert_or_ignore(fn_ptr_data_link
, "'%s', '%s'", ptr
, data
);
483 void sql_insert_fn_data_link(struct expression
*fn
, int type
, int param
, const char *key
, const char *value
)
485 if (fn
->type
!= EXPR_SYMBOL
|| !fn
->symbol
->ident
)
488 sql_insert(fn_data_link
, "0x%llx, '%s', %d, %d, %d, '%s', '%s'",
489 is_local(fn
->symbol
) ? get_base_file_id() : 0,
490 fn
->symbol
->ident
->name
,
491 is_local(fn
->symbol
),
492 type
, param
, key
, value
);
495 void sql_insert_mtag_about(mtag_t tag
, const char *left_name
, const char *right_name
)
497 sql_insert_cache(mtag_about
, "%lld, '%s', '%s', %d, '%s', '%s'",
498 tag
, get_filename(), get_function(), get_lineno(),
499 left_name
, right_name
);
502 void sql_insert_mtag_info(mtag_t tag
, int type
, const char *value
)
504 sql_insert_cache(mtag_info
, "'%s', %lld, %d, '%s'", get_filename(), tag
, type
, value
);
507 void sql_insert_mtag_map(mtag_t container
, int container_offset
, mtag_t tag
, int tag_offset
)
509 sql_insert(mtag_map
, "%lld, %d, %lld, %d", container
, container_offset
, tag
, tag_offset
);
512 void sql_insert_mtag_alias(mtag_t orig
, mtag_t alias
)
514 sql_insert(mtag_alias
, "%lld, %lld", orig
, alias
);
517 static int save_mtag(void *_tag
, int argc
, char **argv
, char **azColName
)
519 mtag_t
*saved_tag
= _tag
;
522 new_tag
= strtoll(argv
[0], NULL
, 10);
525 *saved_tag
= new_tag
;
526 else if (*saved_tag
!= new_tag
)
532 int mtag_map_select_container(mtag_t tag
, int container_offset
, mtag_t
*container
)
536 run_sql(save_mtag
, &tmp
,
537 "select container from mtag_map where tag = %lld and container_offset = %d and tag_offset = 0;",
538 tag
, container_offset
);
540 if (tmp
== 0 || tmp
== -1ULL)
546 int mtag_map_select_tag(mtag_t container
, int offset
, mtag_t
*tag
)
550 run_sql(save_mtag
, &tmp
,
551 "select tag from mtag_map where container = %lld and container_offset = %d;",
554 if (tmp
== 0 || tmp
== -1ULL)
560 char *get_static_filter(struct symbol
*sym
)
562 static char sql_filter
[1024];
564 /* This can only happen on buggy code. Return invalid SQL. */
566 sql_filter
[0] = '\0';
571 snprintf(sql_filter
, sizeof(sql_filter
),
572 "file = 0x%llx and function = '%s' and static = '1'",
573 get_base_file_id(), sym
->ident
->name
);
575 snprintf(sql_filter
, sizeof(sql_filter
),
576 "function = '%s' and static = '0'", sym
->ident
->name
);
582 int get_row_count(void *_row_count
, int argc
, char **argv
, char **azColName
)
584 int *row_count
= _row_count
;
589 *row_count
= atoi(argv
[0]);
593 static void sql_select_return_states_pointer(const char *cols
,
594 struct expression
*call
, int (*callback
)(void*, int, char**, char**), void *info
)
597 int return_count
= 0;
599 ptr
= get_fnptr_name(call
->fn
);
603 run_sql(get_row_count
, &return_count
,
604 "select count(*) from return_states join function_ptr "
605 "where return_states.function == function_ptr.function and "
606 "ptr = '%s' and searchable = 1 and type = %d;", ptr
, INTERNAL
);
607 /* The magic number 100 is just from testing on the kernel. */
608 if (return_count
== 0 || return_count
> 100) {
609 run_sql(callback
, info
,
610 "select distinct %s from return_states join function_ptr where "
611 "return_states.function == function_ptr.function and ptr = '%s' "
612 "and searchable = 1 and type = %d "
613 "order by function_ptr.file, return_states.file, return_id, type;",
614 cols
, ptr
, INTERNAL
);
615 mark_call_params_untracked(call
);
619 run_sql(callback
, info
,
620 "select %s from return_states join function_ptr where "
621 "return_states.function == function_ptr.function and ptr = '%s' "
622 "and searchable = 1 "
623 "order by function_ptr.file, return_states.file, return_id, type;",
627 static int is_local_symbol(struct expression
*expr
)
629 if (expr
->type
!= EXPR_SYMBOL
)
631 if (expr
->symbol
->ctype
.modifiers
& (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
))
636 bool is_fn_ptr(struct expression
*fn
)
639 if (fn
->type
!= EXPR_SYMBOL
)
643 if (is_local_symbol(fn
))
648 void sql_select_return_states(const char *cols
, struct expression
*call
,
649 int (*callback
)(void*, int, char**, char**), void *info
)
651 struct expression
*fn
;
654 if (is_fake_call(call
))
657 fn
= strip_expr(call
->fn
);
659 sql_select_return_states_pointer(cols
, call
, callback
, info
);
664 mem_sql(callback
, info
,
665 "select %s from return_states where call_id = '%lu' order by return_id, type;",
666 cols
, (unsigned long)call
);
670 run_sql(get_row_count
, &row_count
, "select count(*) from return_states where %s;",
671 get_static_filter(fn
->symbol
));
672 if (row_count
== 0 && fn
->symbol
&& fn
->symbol
->definition
&&
673 !(fn
->symbol
->ident
&& strncmp(fn
->symbol
->ident
->name
, "__smatch", 8)))
674 set_state(my_id
, "db_incomplete", NULL
, &incomplete
);
675 if (row_count
== 0 || row_count
> 3000) {
676 mark_call_params_untracked(call
);
680 run_sql(callback
, info
, "select %s from return_states where %s order by file, return_id, type;",
681 cols
, get_static_filter(fn
->symbol
));
684 bool db_incomplete(void)
686 return !!get_state(my_id
, "db_incomplete", NULL
);
689 #define CALL_IMPLIES 0
690 #define RETURN_IMPLIES 1
692 struct implies_info
{
694 struct db_implies_cb_list
*cb_list
;
695 struct expression
*expr
;
699 void sql_select_implies(const char *cols
, struct implies_info
*info
,
700 int (*callback
)(void*, int, char**, char**))
702 if (info
->type
== RETURN_IMPLIES
&& inlinable(info
->expr
->fn
)) {
703 mem_sql(callback
, info
,
704 "select %s from return_implies where call_id = '%lu';",
705 cols
, (unsigned long)info
->expr
);
709 run_sql(callback
, info
, "select %s from %s_implies where %s;",
711 info
->type
== CALL_IMPLIES
? "call" : "return",
712 get_static_filter(info
->sym
));
715 struct select_caller_info_data
{
716 struct stree
*final_states
;
717 struct timeval start_time
;
723 static int caller_info_callback(void *_data
, int argc
, char **argv
, char **azColName
);
725 static bool too_much_caller_info_data(struct symbol
*sym
)
729 run_sql(get_row_count
, &count
,
730 "select count(*) from caller_info where %s;",
731 get_static_filter(sym
));
737 static void sql_select_caller_info(struct select_caller_info_data
*data
,
738 const char *cols
, struct symbol
*sym
)
741 mem_sql(caller_info_callback
, data
,
742 "select %s from caller_info where call_id = %lu;",
743 cols
, (unsigned long)__inline_fn
);
747 if (is_common_function(sym
->ident
->name
))
749 run_sql(caller_info_callback
, data
,
750 "select %s from common_caller_info where %s order by call_id;",
751 cols
, get_static_filter(sym
));
755 if (too_much_caller_info_data(sym
))
758 run_sql(caller_info_callback
, data
,
759 "select %s from caller_info where %s order by call_id;",
760 cols
, get_static_filter(sym
));
763 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
765 struct def_callback
*def_callback
= __alloc_def_callback(0);
767 def_callback
->hook_type
= type
;
768 def_callback
->callback
= callback
;
769 add_ptr_list(&select_caller_info_callbacks
, def_callback
);
772 void select_caller_name_sym(void (*fn
)(const char *name
, struct symbol
*sym
, char *value
), int type
)
774 struct def_name_sym_callback
*callback
= __alloc_def_name_sym_callback(0);
776 callback
->hook_type
= type
;
777 callback
->callback
= fn
;
778 add_ptr_list(&select_caller_name_sym_callbacks
, callback
);
782 * These call backs are used when the --info option is turned on to print struct
783 * member information. For example foo->bar could have a state in
784 * smatch_extra.c and also check_user.c.
786 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
788 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
790 member_callback
->owner
= owner
;
791 member_callback
->callback
= callback
;
792 add_ptr_list(&member_callbacks
, member_callback
);
795 void add_caller_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
797 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
799 member_callback
->owner
= owner
;
800 member_callback
->callback
= callback
;
801 add_ptr_list(&member_callbacks_new
, member_callback
);
804 void add_return_info_callback(int owner
,
805 void (*callback
)(int return_id
, char *return_ranges
,
806 struct expression
*returned_expr
,
808 const char *printed_name
,
809 struct sm_state
*sm
))
811 struct return_info_callback
*return_callback
= __alloc_return_info_callback(0);
813 return_callback
->owner
= owner
;
814 return_callback
->callback
= callback
;
815 add_ptr_list(&return_callbacks
, return_callback
);
818 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
820 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
822 callback
->callback
= fn
;
823 add_ptr_list(&returned_state_callbacks
, callback
);
826 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
))
828 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
830 member_callback
->owner
= owner
;
831 member_callback
->callback
= callback
;
832 add_ptr_list(&returned_member_callbacks
, member_callback
);
835 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
))
837 struct db_implies_callback
*cb
= __alloc_db_implies_callback(0);
840 cb
->callback
= callback
;
841 add_ptr_list(&call_implies_cb_list
, cb
);
844 void select_return_implies_hook_early(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
))
846 struct db_implies_callback
*cb
= __alloc_db_implies_callback(0);
849 cb
->callback
= callback
;
850 add_ptr_list(&return_implies_cb_list_early
, cb
);
853 void select_return_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
))
855 struct db_implies_callback
*cb
= __alloc_db_implies_callback(0);
858 cb
->callback
= callback
;
859 add_ptr_list(&return_implies_cb_list_late
, cb
);
863 struct expression
*static_returns_call
;
864 struct symbol
*return_type
;
865 struct range_list
*return_range_list
;
868 static int db_return_callback(void *_ret_info
, int argc
, char **argv
, char **azColName
)
870 struct return_info
*ret_info
= _ret_info
;
871 struct range_list
*rl
;
872 struct expression
*call_expr
= ret_info
->static_returns_call
;
876 call_results_to_rl(call_expr
, ret_info
->return_type
, argv
[0], &rl
);
877 ret_info
->return_range_list
= rl_union(ret_info
->return_range_list
, rl
);
881 static struct expression
*cached_expr
, *cached_no_args
;
882 static const char *cached_str
;
883 static struct range_list
*cached_rl
, *cached_str_rl
, *cached_no_args_rl
;
885 static void clear_cached_return_vals(void)
890 cached_str_rl
= NULL
;
891 cached_no_args
= NULL
;
892 cached_no_args_rl
= NULL
;
895 struct range_list
*db_return_vals(struct expression
*expr
)
897 struct return_info ret_info
= {};
903 if (is_fake_call(expr
))
906 if (expr
== cached_expr
)
907 return clone_rl(cached_rl
);
912 sm
= get_extra_sm_state(expr
);
914 cached_rl
= clone_rl(estate_rl(sm
->state
));
915 return clone_rl(estate_rl(sm
->state
));
917 ret_info
.static_returns_call
= expr
;
918 ret_info
.return_type
= get_type(expr
);
919 if (!ret_info
.return_type
)
922 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
925 ret_info
.return_range_list
= NULL
;
926 if (inlinable(expr
->fn
)) {
927 mem_sql(db_return_callback
, &ret_info
,
928 "select distinct return from return_states where call_id = '%lu';",
929 (unsigned long)expr
);
931 run_sql(db_return_callback
, &ret_info
,
932 "select distinct return from return_states where %s;",
933 get_static_filter(expr
->fn
->symbol
));
935 cached_rl
= clone_rl(ret_info
.return_range_list
);
936 return ret_info
.return_range_list
;
939 struct range_list
*db_return_vals_from_str(const char *fn_name
)
941 struct return_info ret_info
;
945 if (fn_name
== cached_str
)
946 return clone_rl(cached_str_rl
);
947 cached_str
= fn_name
;
948 cached_str_rl
= NULL
;
950 ret_info
.static_returns_call
= NULL
;
951 ret_info
.return_type
= &llong_ctype
;
952 ret_info
.return_range_list
= NULL
;
954 run_sql(db_return_callback
, &ret_info
,
955 "select distinct return from return_states where function = '%s';",
957 cached_str_rl
= clone_rl(ret_info
.return_range_list
);
958 return ret_info
.return_range_list
;
962 * This is used when we have a function that takes a function pointer as a
963 * parameter. "frob(blah, blah, my_function);" We know that the return values
964 * from frob() come from my_funcion() so we want to find the possible returns
965 * of my_function(), but we don't know which arguments are passed to it.
968 struct range_list
*db_return_vals_no_args(struct expression
*expr
)
970 struct return_info ret_info
= {};
972 if (!expr
|| expr
->type
!= EXPR_SYMBOL
)
975 if (expr
== cached_no_args
)
976 return clone_rl(cached_no_args_rl
);
977 cached_no_args
= expr
;
978 cached_no_args_rl
= NULL
;
980 ret_info
.static_returns_call
= expr
;
981 ret_info
.return_type
= get_type(expr
);
982 ret_info
.return_type
= get_real_base_type(ret_info
.return_type
);
983 if (!ret_info
.return_type
)
986 run_sql(db_return_callback
, &ret_info
,
987 "select distinct return from return_states where %s;",
988 get_static_filter(expr
->symbol
));
990 cached_no_args_rl
= clone_rl(ret_info
.return_range_list
);
991 return ret_info
.return_range_list
;
994 static void match_call_marker(struct expression
*expr
)
998 type
= get_type(expr
->fn
);
999 if (type
&& type
->type
== SYM_PTR
)
1000 type
= get_real_base_type(type
);
1003 * we just want to record something in the database so that if we have
1004 * two calls like: frob(4); frob(some_unkown); then on the receiving
1005 * side we know that sometimes frob is called with unknown parameters.
1008 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", type_to_str(type
));
1011 int is_recursive_member(const char *name
)
1014 const char *p
, *next
;
1017 p
= strchr(name
, '>');
1022 next
= strchr(p
, '>');
1028 if (size
>= sizeof(buf
))
1030 memcpy(buf
, p
, size
);
1032 if (strstr(next
, buf
))
1038 char *sm_to_arg_name(struct expression
*expr
, struct sm_state
*sm
)
1041 const char *sm_name
;
1043 bool is_address
= false;
1044 bool add_star
= false;
1049 expr
= strip_expr(expr
);
1053 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1054 expr
= strip_expr(expr
->unop
);
1058 name
= expr_to_var_sym(expr
, &sym
);
1066 if (sm_name
[0] == '*') {
1072 if (strncmp(name
, sm_name
, len
) != 0)
1074 if (sm_name
[len
] == '\0') {
1075 snprintf(buf
, sizeof(buf
), "%s%s$",
1076 add_star
? "*" : "", is_address
? "*" : "");
1078 if (sm_name
[len
] != '.' && sm_name
[len
] != '-')
1080 if (sm_name
[len
] == '-')
1082 // FIXME does is_address really imply that sm_name[len] == '-'
1083 snprintf(buf
, sizeof(buf
), "%s$->%s", add_star
? "*" : "",
1087 ret
= alloc_sname(buf
);
1093 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
,
1095 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
),
1098 struct sm_state
*sm
;
1099 const char *sm_name
;
1103 char printed_name
[256];
1106 struct symbol
*type
;
1108 expr
= strip_expr(expr
);
1111 type
= get_type(expr
);
1112 if (!new && type
&& type_bits(type
) < type_bits(&ulong_ctype
))
1115 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1116 expr
= strip_expr(expr
->unop
);
1120 name
= expr_to_var_sym(expr
, &sym
);
1125 FOR_EACH_SM(__get_cur_stree(), sm
) {
1126 if (sm
->owner
!= owner
|| sm
->sym
!= sym
)
1131 if (sm_name
[0] == '*') {
1136 if (!add_star
&& strcmp(name
, sm_name
) == 0) {
1138 snprintf(printed_name
, sizeof(printed_name
), "*$");
1141 snprintf(printed_name
, sizeof(printed_name
), "$");
1145 } else if (add_star
&& strcmp(name
, sm_name
) == 0) {
1146 snprintf(printed_name
, sizeof(printed_name
), "%s*$",
1147 is_address
? "*" : "");
1148 } else if (strncmp(name
, sm_name
, len
) == 0) {
1149 if (sm_name
[len
] != '.' && sm_name
[len
] != '-')
1151 if (is_address
&& sm_name
[len
] == '.') {
1152 snprintf(printed_name
, sizeof(printed_name
),
1153 "%s$->%s", add_star
? "*" : "",
1155 } else if (is_address
&& sm_name
[len
] == '-') {
1156 snprintf(printed_name
, sizeof(printed_name
),
1157 "%s(*$)%s", add_star
? "*" : "",
1160 snprintf(printed_name
, sizeof(printed_name
),
1161 "%s$%s", add_star
? "*" : "",
1164 } else if (sm_name
[0] == '&' && strncmp(name
, sm_name
+ 1, len
) == 0) {
1165 if (sm_name
[len
+ 1] != '.' && sm_name
[len
+ 1] != '-')
1167 if (is_address
&& sm_name
[len
+ 1] == '.') {
1168 snprintf(printed_name
, sizeof(printed_name
),
1169 "&%s$->%s", add_star
? "*" : "",
1171 } else if (is_address
&& sm_name
[len
] == '-') {
1172 snprintf(printed_name
, sizeof(printed_name
),
1173 "&%s(*$)%s", add_star
? "*" : "",
1176 snprintf(printed_name
, sizeof(printed_name
),
1177 "&%s$%s", add_star
? "*" : "",
1183 if (is_recursive_member(printed_name
))
1185 callback(call
, param
, printed_name
, sm
);
1186 } END_FOR_EACH_SM(sm
);
1191 static void match_call_info(struct expression
*call
)
1193 struct member_info_callback
*cb
;
1194 struct expression
*arg
;
1197 FOR_EACH_PTR(member_callbacks
, cb
) {
1199 FOR_EACH_PTR(call
->args
, arg
) {
1201 print_struct_members(call
, arg
, i
, cb
->owner
, cb
->callback
, 0);
1202 } END_FOR_EACH_PTR(arg
);
1203 } END_FOR_EACH_PTR(cb
);
1206 static struct expression
*get_fake_variable(struct expression
*expr
)
1208 struct expression
*tmp
;
1210 tmp
= expr_get_fake_parent_expr(expr
);
1211 if (!tmp
|| tmp
->type
!= EXPR_ASSIGNMENT
)
1217 static struct sm_state
*get_returned_sm(struct expression
*expr
)
1219 struct expression
*fake
;
1221 fake
= get_fake_variable(expr
);
1225 return get_sm_state_expr(SMATCH_EXTRA
, expr
);
1228 static void match_call_info_new(struct expression
*call
)
1230 struct member_info_callback
*cb
;
1231 struct expression
*arg
, *tmp
;
1234 if (!option_info
&& !__inline_call
&& !local_debug
)
1237 FOR_EACH_PTR(member_callbacks_new
, cb
) {
1239 FOR_EACH_PTR(call
->args
, arg
) {
1241 tmp
= get_fake_variable(arg
);
1244 __ignore_param_used
++;
1245 print_struct_members(call
, tmp
, i
, cb
->owner
, cb
->callback
, 1);
1246 __ignore_param_used
--;
1247 } END_FOR_EACH_PTR(arg
);
1248 } END_FOR_EACH_PTR(cb
);
1251 static int get_param(int param
, char **name
, struct symbol
**sym
)
1257 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
1259 *name
= arg
->ident
->name
;
1264 } END_FOR_EACH_PTR(arg
);
1269 static int function_signature_matches(const char *sig
)
1273 my_sig
= function_signature();
1274 if (!sig
|| !my_sig
)
1275 return 1; /* default to matching */
1276 if (strcmp(my_sig
, sig
) == 0)
1281 static int caller_info_callback(void *_data
, int argc
, char **argv
, char **azColName
)
1283 struct select_caller_info_data
*data
= _data
;
1290 struct symbol
*sym
= NULL
;
1291 struct def_callback
*def_callback
;
1292 struct def_name_sym_callback
*ns_callback
;
1293 struct stree
*stree
;
1294 struct timeval cur_time
;
1303 gettimeofday(&cur_time
, NULL
);
1304 if (cur_time
.tv_sec
- data
->start_time
.tv_sec
> 10)
1307 func_id
= atoi(argv
[0]);
1309 type
= strtol(argv
[1], NULL
, 10);
1310 param
= strtol(argv
[2], NULL
, 10);
1316 if (data
->prev_func_id
== -1)
1317 data
->prev_func_id
= func_id
;
1318 if (func_id
!= data
->prev_func_id
) {
1319 stree
= __pop_fake_cur_stree();
1321 merge_stree(&data
->final_states
, stree
);
1323 __push_fake_cur_stree();
1325 data
->prev_func_id
= func_id
;
1331 if (type
== INTERNAL
&&
1332 !function_signature_matches(value
)) {
1337 if (param
>= 0 && !get_param(param
, &name
, &sym
))
1340 FOR_EACH_PTR(select_caller_info_callbacks
, def_callback
) {
1341 if (def_callback
->hook_type
== type
)
1342 def_callback
->callback(name
, sym
, key
, value
);
1343 } END_FOR_EACH_PTR(def_callback
);
1345 p
= strchr(key
, '$');
1347 snprintf(fullname
, sizeof(fullname
), "%.*s%s%s", (int)(p
- key
), key
, name
, p
+ 1);
1349 snprintf(fullname
, sizeof(fullname
), "%s", key
);
1351 FOR_EACH_PTR(select_caller_name_sym_callbacks
, ns_callback
) {
1352 if (ns_callback
->hook_type
== type
)
1353 ns_callback
->callback(fullname
, sym
, value
);
1354 } END_FOR_EACH_PTR(ns_callback
);
1359 static struct string_list
*ptr_names_done
;
1360 static struct string_list
*ptr_names
;
1362 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
1364 insert_string(&ptr_names
, alloc_string(argv
[0]));
1368 static char *get_next_ptr_name(void)
1372 FOR_EACH_PTR(ptr_names
, ptr
) {
1373 if (!insert_string(&ptr_names_done
, ptr
))
1376 } END_FOR_EACH_PTR(ptr
);
1380 static void get_ptr_names(unsigned long long file
, const char *name
)
1382 char sql_filter
[1024];
1386 snprintf(sql_filter
, 1024, "file = 0x%llx and function = '%s';",
1389 snprintf(sql_filter
, 1024, "function = '%s';", name
);
1392 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
1394 run_sql(get_ptr_name
, NULL
,
1395 "select distinct ptr from function_ptr where %s",
1398 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
1399 if (before
== after
)
1402 while ((name
= get_next_ptr_name()))
1403 get_ptr_names(0, name
);
1406 static void match_data_from_db(struct symbol
*sym
)
1408 struct select_caller_info_data data
= { .prev_func_id
= -1 };
1409 struct sm_state
*sm
;
1410 struct stree
*stree
;
1411 struct timeval end_time
;
1413 if (!sym
|| !sym
->ident
)
1417 gettimeofday(&data
.start_time
, NULL
);
1419 __push_fake_cur_stree();
1425 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1426 get_ptr_names(get_base_file_id(), sym
->ident
->name
);
1428 get_ptr_names(0, sym
->ident
->name
);
1430 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
1431 __free_ptr_list((struct ptr_list
**)&ptr_names
);
1432 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
1433 __free_fake_cur_stree();
1437 sql_select_caller_info(&data
,
1438 "call_id, type, parameter, key, value",
1442 stree
= __pop_fake_cur_stree();
1444 merge_stree(&data
.final_states
, stree
);
1446 __push_fake_cur_stree();
1448 data
.prev_func_id
= -1;
1452 FOR_EACH_PTR(ptr_names
, ptr
) {
1453 run_sql(caller_info_callback
, &data
,
1454 "select call_id, type, parameter, key, value"
1455 " from common_caller_info where function = '%s' order by call_id",
1457 } END_FOR_EACH_PTR(ptr
);
1460 FOR_EACH_PTR(ptr_names
, ptr
) {
1462 } END_FOR_EACH_PTR(ptr
);
1463 goto free_ptr_names
;
1466 FOR_EACH_PTR(ptr_names
, ptr
) {
1467 run_sql(caller_info_callback
, &data
,
1468 "select call_id, type, parameter, key, value"
1469 " from caller_info where function = '%s' order by call_id",
1472 } END_FOR_EACH_PTR(ptr
);
1475 __free_ptr_list((struct ptr_list
**)&ptr_names
);
1476 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
1478 sql_select_caller_info(&data
,
1479 "call_id, type, parameter, key, value",
1483 stree
= __pop_fake_cur_stree();
1485 merge_stree(&data
.final_states
, stree
);
1488 gettimeofday(&end_time
, NULL
);
1489 if (end_time
.tv_sec
- data
.start_time
.tv_sec
<= 10) {
1490 FOR_EACH_SM(data
.final_states
, sm
) {
1492 } END_FOR_EACH_SM(sm
);
1495 free_stree(&data
.final_states
);
1498 static int return_implies_callbacks(void *_info
, int argc
, char **argv
, char **azColName
)
1500 struct implies_info
*info
= _info
;
1501 struct db_implies_callback
*cb
;
1502 struct expression
*arg
= NULL
;
1509 type
= atoi(argv
[1]);
1510 param
= atoi(argv
[2]);
1512 /* The caller doesn't pass the assignment so -1 can't be useful */
1516 arg
= get_argument_from_call_expr(info
->expr
->args
, param
);
1521 FOR_EACH_PTR(info
->cb_list
, cb
) {
1522 if (cb
->type
!= type
)
1524 cb
->callback(info
->expr
, arg
, argv
[3], argv
[4]);
1525 } END_FOR_EACH_PTR(cb
);
1530 static int call_implies_callbacks(void *_info
, int argc
, char **argv
, char **azColName
)
1532 struct implies_info
*info
= _info
;
1533 struct db_implies_callback
*cb
;
1534 struct expression
*arg
;
1543 type
= atoi(argv
[1]);
1544 param
= atoi(argv
[2]);
1546 if (!get_param(param
, &name
, &sym
))
1548 arg
= symbol_expression(sym
);
1552 FOR_EACH_PTR(info
->cb_list
, cb
) {
1553 if (cb
->type
!= type
)
1555 cb
->callback(info
->expr
, arg
, argv
[3], argv
[4]);
1556 } END_FOR_EACH_PTR(cb
);
1561 static void match_return_implies_helper(struct expression
*expr
, struct db_implies_cb_list
*cb_list
)
1563 struct implies_info info
= {
1564 .type
= RETURN_IMPLIES
,
1568 if (expr
->fn
->type
!= EXPR_SYMBOL
||
1572 info
.sym
= expr
->fn
->symbol
;
1573 sql_select_implies("function, type, parameter, key, value", &info
,
1574 return_implies_callbacks
);
1577 static void match_return_implies_early(struct expression
*expr
)
1579 match_return_implies_helper(expr
, return_implies_cb_list_early
);
1582 static void match_return_implies_late(struct expression
*expr
)
1584 match_return_implies_helper(expr
, return_implies_cb_list_late
);
1587 static void match_call_implies(struct symbol
*sym
)
1589 struct implies_info info
= {
1590 .type
= CALL_IMPLIES
,
1591 .cb_list
= call_implies_cb_list
,
1594 if (!sym
|| !sym
->ident
)
1598 sql_select_implies("function, type, parameter, key, value", &info
,
1599 call_implies_callbacks
);
1602 static char *get_fn_param_str(struct expression
*expr
)
1604 struct expression
*tmp
;
1608 tmp
= get_assigned_expr(expr
);
1611 expr
= strip_expr(expr
);
1612 if (!expr
|| expr
->type
!= EXPR_CALL
)
1614 expr
= strip_expr(expr
->fn
);
1615 if (!expr
|| expr
->type
!= EXPR_SYMBOL
)
1617 param
= get_param_num(expr
);
1621 snprintf(buf
, sizeof(buf
), "[r $%d]", param
);
1622 return alloc_sname(buf
);
1625 static char *get_return_compare_is_param(struct expression
*expr
)
1632 param
= get_param_num(expr
);
1636 var
= expr_to_var(expr
);
1639 snprintf(buf
, sizeof(buf
), "%s orig", var
);
1640 comparison
= get_comparison_strings(var
, buf
);
1646 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(comparison
), param
);
1647 return alloc_sname(buf
);
1650 static char *get_return_compare_str(struct expression
*expr
)
1654 compare_str
= get_return_compare_is_param(expr
);
1658 compare_str
= expr_lte_to_param(expr
, -1);
1662 return expr_param_comparison(expr
, -1);
1665 static const char *get_return_ranges_str(struct expression
*expr
, struct range_list
**rl_p
)
1667 struct expression
*fake
;
1668 struct range_list
*rl
;
1669 const char *return_ranges
;
1671 const char *container_of_str
;
1672 const char *math_str
;
1680 return alloc_sname("");
1682 fake
= get_fake_variable(expr
);
1686 container_of_str
= get_container_of_str(expr
);
1688 if (get_implied_value(expr
, &sval
)) {
1689 sval
= sval_cast(cur_func_return_type(), sval
);
1690 *rl_p
= alloc_rl(sval
, sval
);
1691 return_ranges
= sval_to_str_or_err_ptr(sval
);
1692 if (container_of_str
) {
1693 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, container_of_str
);
1694 return alloc_sname(buf
);
1696 return return_ranges
;
1699 fn_param_str
= get_fn_param_str(expr
);
1700 math_str
= get_param_key_swap_dollar(expr
);
1701 compare_str
= expr_equal_to_param(expr
, -1);
1703 math_str
= get_value_in_terms_of_parameter_math(expr
);
1705 if (get_implied_rl(expr
, &rl
) && !is_whole_rl(rl
)) {
1706 rl
= cast_rl(cur_func_return_type(), rl
);
1707 return_ranges
= show_rl(rl
);
1708 } else if (get_imaginary_absolute(expr
, &rl
)){
1709 rl
= cast_rl(cur_func_return_type(), rl
);
1710 return alloc_sname(show_rl(rl
));
1712 get_absolute_rl(expr
, &rl
);
1713 rl
= cast_rl(cur_func_return_type(), rl
);
1714 return_ranges
= show_rl(rl
);
1718 if (container_of_str
) {
1719 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, container_of_str
);
1720 return alloc_sname(buf
);
1723 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, fn_param_str
);
1724 return alloc_sname(buf
);
1727 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1728 return alloc_sname(buf
);
1731 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1732 return alloc_sname(buf
);
1734 compare_str
= get_return_compare_str(expr
);
1736 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1737 return alloc_sname(buf
);
1740 return return_ranges
;
1743 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
1745 char line_number
[16];
1747 snprintf(line_number
, sizeof(line_number
), "%d", get_lineno());
1748 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, line_number
, function_signature());
1751 static bool call_return_state_hooks_conditional(struct expression
*expr
)
1753 int final_pass_orig
= final_pass
;
1759 (expr
->type
!= EXPR_CONDITIONAL
&& expr
->type
!= EXPR_SELECT
))
1764 __push_fake_cur_stree();
1767 __split_whole_condition(expr
->conditional
);
1768 final_pass
= final_pass_orig
;
1770 call_return_state_hooks(expr
->cond_true
?: expr
->conditional
);
1772 __push_true_states();
1773 __use_false_states();
1775 call_return_state_hooks(expr
->cond_false
);
1777 __merge_true_states();
1778 __free_fake_cur_stree();
1784 static bool handle_forced_split(const char *return_ranges
, struct expression
*expr
)
1786 struct split_data
*data
= NULL
;
1787 struct expression
*compare
;
1788 struct range_list
*rl
;
1789 static bool recurse
;
1796 for (i
= 0; i
< split_count
; i
++) {
1797 if (get_function() &&
1798 strcmp(get_function(), forced_splits
[i
]->func
) == 0) {
1799 data
= forced_splits
[i
];
1806 // FIXME: this works for copy_to/from_user() because the only thing we
1807 // care about is zero/non-zero
1808 if (strcmp(data
->rl
, "0") != 0)
1811 compare
= compare_expression(expr
, SPECIAL_EQUAL
, zero_expr());
1814 if (get_implied_value(compare
, &sval
))
1821 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
1822 call_return_states_callbacks("0", expr
);
1826 undo
= assume(compare_expression(expr
, SPECIAL_NOTEQUAL
, zero_expr()));
1827 if (get_implied_rl(expr
, &rl
)) {
1828 math
= strchr(return_ranges
, '[');
1829 snprintf(buf
, sizeof(buf
), "%s%s", show_rl(rl
), math
?: "");
1831 snprintf(buf
, sizeof(buf
), "%s", return_ranges
);
1833 call_return_states_callbacks(buf
, expr
);
1841 static void call_return_states_callbacks(const char *return_ranges
, struct expression
*expr
)
1843 struct returned_state_callback
*cb
;
1845 return_ranges
= replace_return_ranges(return_ranges
);
1846 if (is_delete_return(return_ranges
))
1848 if (is_project_delete_return(expr
))
1850 if (handle_forced_split(return_ranges
, expr
))
1854 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1855 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1856 } END_FOR_EACH_PTR(cb
);
1859 static void call_return_state_hooks_compare(struct expression
*expr
)
1861 char *return_ranges
;
1862 int final_pass_orig
= final_pass
;
1863 sval_t sval
= { .type
= &int_ctype
};
1866 if (!get_implied_value(expr
, &ret
))
1869 __push_fake_cur_stree();
1872 __split_whole_condition(expr
);
1873 final_pass
= final_pass_orig
;
1875 if (ret
.value
!= 0) {
1876 return_ranges
= alloc_sname("1");
1878 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1880 call_return_states_callbacks(return_ranges
, expr
);
1883 __push_true_states();
1884 __use_false_states();
1886 if (ret
.value
!= 1) {
1887 return_ranges
= alloc_sname("0");
1889 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1891 call_return_states_callbacks(return_ranges
, expr
);
1894 __merge_true_states();
1895 __free_fake_cur_stree();
1898 static bool is_implies_function(struct expression
*expr
)
1900 struct range_list
*rl
;
1905 rl
= get_range_implications(get_function());
1909 sm_msg("%s: is implied", __func__
);
1913 static int ptr_in_list(struct sm_state
*sm
, struct state_list
*slist
)
1915 struct sm_state
*tmp
;
1917 FOR_EACH_PTR(slist
, tmp
) {
1918 if (strcmp(tmp
->state
->name
, sm
->state
->name
) == 0)
1920 } END_FOR_EACH_PTR(tmp
);
1925 static int split_possible_helper(struct sm_state
*sm
, struct expression
*expr
)
1927 struct range_list
*rl
;
1928 char *return_ranges
;
1929 struct sm_state
*tmp
;
1931 int nr_possible
, nr_states
;
1934 struct state_list
*already_handled
= NULL
;
1937 if (!sm
|| !sm
->merged
)
1940 if (too_many_possible(sm
) && !is_implies_function(expr
))
1943 /* bail if it gets too complicated */
1945 FOR_EACH_PTR(sm
->possible
, tmp
) {
1948 if (ptr_in_list(tmp
, already_handled
))
1950 add_ptr_list(&already_handled
, tmp
);
1952 } END_FOR_EACH_PTR(tmp
);
1953 free_slist(&already_handled
);
1954 nr_states
= get_db_state_count();
1955 if (nr_states
* nr_possible
>= 2000 && !is_implies_function(expr
))
1958 FOR_EACH_PTR(sm
->possible
, tmp
) {
1961 if (ptr_in_list(tmp
, already_handled
))
1963 add_ptr_list(&already_handled
, tmp
);
1966 __push_fake_cur_stree();
1968 overwrite_states_using_pool(sm
, tmp
);
1970 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
1971 return_ranges
= show_rl(rl
);
1972 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(clone_rl(rl
)));
1973 compare_str
= get_return_compare_str(expr
);
1974 /* ignore obvious stuff like 0 <= param */
1975 /* Is this worthile when we have PARAM_COMPARE? */
1977 strncmp(compare_str
, "[=", 2) != 0 &&
1978 rl_to_sval(rl
, &sval
))
1981 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1982 return_ranges
= alloc_sname(buf
);
1985 call_return_states_callbacks(return_ranges
, expr
);
1987 __free_fake_cur_stree();
1988 } END_FOR_EACH_PTR(tmp
);
1990 free_slist(&already_handled
);
1995 static int call_return_state_hooks_split_possible(struct expression
*expr
)
1997 struct sm_state
*sm
;
2002 sm
= get_returned_sm(expr
);
2003 return split_possible_helper(sm
, expr
);
2006 static bool has_empty_state(struct sm_state
*sm
)
2008 struct sm_state
*tmp
;
2010 FOR_EACH_PTR(sm
->possible
, tmp
) {
2011 if (!estate_rl(tmp
->state
))
2013 } END_FOR_EACH_PTR(tmp
);
2018 static bool has_possible_negative(struct sm_state
*sm
)
2020 struct sm_state
*tmp
;
2022 if (!type_signed(estate_type(sm
->state
)))
2025 FOR_EACH_PTR(sm
->possible
, tmp
) {
2026 if (!estate_rl(tmp
->state
))
2028 if (sval_is_negative(estate_min(tmp
->state
)) &&
2029 sval_is_negative(estate_max(tmp
->state
)))
2031 } END_FOR_EACH_PTR(tmp
);
2036 static bool has_separate_zero_null(struct sm_state
*sm
)
2038 struct sm_state
*tmp
;
2041 FOR_EACH_PTR(sm
->possible
, tmp
) {
2042 if (!estate_get_single_value(tmp
->state
, &sval
))
2044 if (sval
.value
== 0)
2046 } END_FOR_EACH_PTR(tmp
);
2051 static int split_positive_from_negative(struct expression
*expr
)
2053 struct sm_state
*sm
;
2054 struct range_list
*rl
;
2055 const char *return_ranges
;
2056 struct range_list
*ret_rl
;
2060 /* We're going to print the states 3 times */
2061 if (get_db_state_count() > 10000 / 3)
2064 if (!get_implied_rl(expr
, &rl
) || !rl
)
2066 /* Forget about INT_MAX and larger */
2067 if (rl_max(rl
).value
<= 0)
2069 if (!sval_is_negative(rl_min(rl
)))
2072 sm
= get_returned_sm(expr
);
2075 if (has_empty_state(sm
))
2077 if (!has_possible_negative(sm
))
2079 separate_zero
= has_separate_zero_null(sm
);
2081 if (!assume(compare_expression(expr
, separate_zero
? '>' : SPECIAL_GTE
, zero_expr())))
2084 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
2085 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
2086 call_return_states_callbacks(return_ranges
, expr
);
2090 if (separate_zero
) {
2091 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
2093 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
2094 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
2095 call_return_states_callbacks(return_ranges
, expr
);
2101 undo
= assume(compare_expression(expr
, '<', zero_expr()));
2103 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
2104 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
2105 call_return_states_callbacks(return_ranges
, expr
);
2113 static int call_return_state_hooks_split_null_non_null_zero(struct expression
*expr
)
2115 struct range_list
*rl
;
2116 struct range_list
*nonnull_rl
;
2118 struct range_list
*null_rl
= NULL
;
2119 char *return_ranges
;
2120 struct sm_state
*sm
;
2121 struct smatch_state
*state
;
2123 int final_pass_orig
= final_pass
;
2125 if (!expr
|| expr_equal_to_param(expr
, -1))
2127 if (expr
->type
== EXPR_CALL
)
2130 sm
= get_returned_sm(expr
);
2133 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
2136 if (!estate_rl(state
))
2138 if (estate_min(state
).value
== 0 && estate_max(state
).value
== 0)
2140 if (has_possible_negative(sm
))
2142 if (!has_separate_zero_null(sm
))
2145 nr_states
= get_db_state_count();
2146 if (option_info
&& nr_states
>= 1500)
2149 rl
= estate_rl(state
);
2151 __push_fake_cur_stree();
2154 __split_whole_condition(expr
);
2155 final_pass
= final_pass_orig
;
2157 nonnull_rl
= rl_filter(rl
, rl_zero());
2158 return_ranges
= show_rl(nonnull_rl
);
2159 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonnull_rl
));
2161 call_return_states_callbacks(return_ranges
, expr
);
2163 __push_true_states();
2164 __use_false_states();
2166 return_ranges
= alloc_sname("0");
2167 null_sval
= sval_type_val(rl_type(rl
), 0);
2168 add_range(&null_rl
, null_sval
, null_sval
);
2169 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(null_rl
));
2170 call_return_states_callbacks(return_ranges
, expr
);
2172 __merge_true_states();
2173 __free_fake_cur_stree();
2178 static bool is_neg_and_pos_err_code(struct range_list
*rl
)
2180 struct data_range
*tmp
, *last
;
2182 if (option_project
!= PROJ_KERNEL
)
2187 /* Assume s32min-(14),(-12)-(-1),1-s32max is an error code. */
2188 last
= last_ptr_list((struct ptr_list
*)rl
);
2189 if (last
->max
.value
>= 0 &&
2190 (last
->min
.value
!= 1 ||
2191 last
->max
.value
!= INT_MAX
))
2195 FOR_EACH_PTR(rl
, tmp
) {
2198 if (tmp
->min
.value
!= INT_MIN
&& tmp
->min
.value
< -4095)
2200 if (tmp
->max
.value
< -4095 || tmp
->max
.value
>= 0)
2202 } END_FOR_EACH_PTR(tmp
);
2207 static bool is_kernel_success_fail(struct sm_state
*sm
)
2209 struct sm_state
*tmp
;
2210 struct range_list
*rl
;
2211 bool has_zero
= false;
2212 bool has_neg
= false;
2217 if (!type_signed(estate_type(sm
->state
)))
2220 FOR_EACH_PTR(sm
->possible
, tmp
) {
2221 rl
= estate_rl(tmp
->state
);
2226 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 0) {
2231 if (is_neg_and_pos_err_code(estate_rl(tmp
->state
)))
2234 } END_FOR_EACH_PTR(tmp
);
2236 return has_zero
&& has_neg
;
2239 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
2241 struct expression
*tmp_ret
;
2242 struct sm_state
*sm
;
2243 struct range_list
*rl
;
2244 struct range_list
*nonzero_rl
;
2246 struct range_list
*zero_rl
= NULL
;
2248 char *return_ranges
;
2249 int final_pass_orig
= final_pass
;
2251 if (option_project
!= PROJ_KERNEL
)
2254 nr_states
= get_db_state_count();
2255 if (nr_states
> 2000)
2258 tmp_ret
= get_fake_variable(expr
);
2261 sm
= get_returned_sm(tmp_ret
);
2264 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
2266 if (!is_kernel_success_fail(sm
))
2269 rl
= estate_rl(sm
->state
);
2273 __push_fake_cur_stree();
2276 __split_whole_condition(tmp_ret
);
2277 final_pass
= final_pass_orig
;
2279 nonzero_rl
= rl_filter(rl
, rl_zero());
2280 nonzero_rl
= cast_rl(cur_func_return_type(), nonzero_rl
);
2281 return_ranges
= show_rl(nonzero_rl
);
2282 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonzero_rl
));
2284 call_return_states_callbacks(return_ranges
, expr
);
2286 __push_true_states();
2287 __use_false_states();
2289 return_ranges
= alloc_sname("0");
2290 zero_sval
= sval_type_val(rl_type(rl
), 0);
2291 add_range(&zero_rl
, zero_sval
, zero_sval
);
2292 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(zero_rl
));
2293 call_return_states_callbacks(return_ranges
, expr
);
2295 __merge_true_states();
2296 __free_fake_cur_stree();
2301 static int is_boolean(struct expression
*expr
)
2303 struct range_list
*rl
;
2305 if (!get_implied_rl(expr
, &rl
))
2307 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
2312 static int splitable_function_call(struct expression
*expr
)
2314 struct sm_state
*sm
;
2316 if (!expr
|| expr
->type
!= EXPR_CALL
)
2318 sm
= get_extra_sm_state(expr
);
2319 return split_possible_helper(sm
, expr
);
2322 static struct sm_state
*find_bool_param(void)
2324 struct stree
*start_states
;
2326 struct sm_state
*sm
, *tmp
;
2329 start_states
= get_start_states();
2331 FOR_EACH_PTR_REVERSE(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
2334 sm
= get_sm_state_stree(start_states
, SMATCH_EXTRA
, arg
->ident
->name
, arg
);
2337 if (rl_min(estate_rl(sm
->state
)).value
!= 0 ||
2338 rl_max(estate_rl(sm
->state
)).value
!= 1)
2341 } END_FOR_EACH_PTR_REVERSE(arg
);
2347 * Check if it's splitable. If not, then splitting it up is likely not
2348 * useful for the callers.
2350 FOR_EACH_PTR(sm
->possible
, tmp
) {
2353 if (!estate_get_single_value(tmp
->state
, &sval
))
2355 } END_FOR_EACH_PTR(tmp
);
2360 static int split_on_bool_sm(struct sm_state
*sm
, struct expression
*expr
)
2362 struct range_list
*ret_rl
;
2363 const char *return_ranges
;
2364 struct sm_state
*tmp
;
2366 struct state_list
*already_handled
= NULL
;
2368 if (!sm
|| !sm
->merged
)
2371 if (too_many_possible(sm
))
2374 FOR_EACH_PTR(sm
->possible
, tmp
) {
2377 if (ptr_in_list(tmp
, already_handled
))
2379 add_ptr_list(&already_handled
, tmp
);
2382 __push_fake_cur_stree();
2384 overwrite_states_using_pool(sm
, tmp
);
2386 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
2387 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
2388 call_return_states_callbacks(return_ranges
, expr
);
2390 __free_fake_cur_stree();
2391 } END_FOR_EACH_PTR(tmp
);
2393 free_slist(&already_handled
);
2398 static int split_by_bool_param(struct expression
*expr
)
2400 struct sm_state
*start_sm
, *sm
;
2403 start_sm
= find_bool_param();
2406 sm
= get_sm_state(SMATCH_EXTRA
, start_sm
->name
, start_sm
->sym
);
2407 if (!sm
|| estate_get_single_value(sm
->state
, &sval
))
2410 if (get_db_state_count() * 2 >= 2000)
2413 return split_on_bool_sm(sm
, expr
);
2416 static int split_by_null_nonnull_param(struct expression
*expr
)
2419 struct sm_state
*sm
;
2422 arg
= first_ptr_list((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
);
2423 if (!arg
|| !arg
->ident
)
2425 if (get_real_base_type(arg
)->type
!= SYM_PTR
)
2428 if (param_was_set_var_sym(arg
->ident
->name
, arg
))
2430 sm
= get_sm_state(SMATCH_EXTRA
, arg
->ident
->name
, arg
);
2434 if (!has_separate_zero_null(sm
))
2437 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
2438 if (get_db_state_count() * nr_possible
>= 2000)
2441 return split_on_bool_sm(sm
, expr
);
2444 static void call_hooks_based_on_pool(struct expression
*expr
, struct sm_state
*gate_sm
, struct sm_state
*pool_sm
)
2446 struct range_list
*ret_rl
;
2447 const char *return_ranges
;
2449 __push_fake_cur_stree();
2451 overwrite_states_using_pool(gate_sm
, pool_sm
);
2453 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
2454 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
2455 call_return_states_callbacks(return_ranges
, expr
);
2457 __free_fake_cur_stree();
2460 static bool split_by_impossible(struct expression
*expr
)
2462 static int impossible_id
;
2463 struct sm_state
*sm
, *tmp
;
2467 impossible_id
= id_from_name("register_impossible_return");
2472 * The only states for register_impossible_return are &impossible,
2473 * &undefined and &merged. This function will break otherwise.
2476 sm
= get_sm_state(impossible_id
, "impossible", NULL
);
2477 if (!sm
|| sm
->state
!= &merged
)
2480 nr_states
= get_db_state_count();
2481 if (nr_states
>= 1000)
2484 /* handle possible */
2485 FOR_EACH_PTR(sm
->possible
, tmp
) {
2488 if (tmp
->state
!= &undefined
)
2490 call_hooks_based_on_pool(expr
, sm
, tmp
);
2492 } END_FOR_EACH_PTR(tmp
);
2495 /* handle impossible */
2496 FOR_EACH_PTR(sm
->possible
, tmp
) {
2499 if (strcmp(tmp
->state
->name
, "impossible") != 0)
2501 call_hooks_based_on_pool(expr
, sm
, tmp
);
2503 } END_FOR_EACH_PTR(tmp
);
2508 struct expression
*strip_expr_statement(struct expression
*expr
)
2510 struct expression
*orig
= expr
;
2511 struct statement
*stmt
, *last_stmt
;
2515 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '(')
2517 if (expr
->type
!= EXPR_STATEMENT
)
2519 stmt
= expr
->statement
;
2520 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
2523 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
2524 if (!last_stmt
|| last_stmt
->type
== STMT_LABEL
)
2525 last_stmt
= last_stmt
->label_statement
;
2526 if (!last_stmt
|| last_stmt
->type
!= STMT_EXPRESSION
)
2528 return strip_expr(last_stmt
->expression
);
2531 static bool is_kernel_error_path(struct expression
*expr
)
2533 struct range_list
*rl
;
2535 if (option_project
!= PROJ_KERNEL
)
2538 if (!get_implied_rl(expr
, &rl
))
2540 if (rl_type(rl
) != &int_ctype
)
2542 if (!is_neg_and_pos_err_code(rl
))
2547 static void call_return_state_hooks(struct expression
*expr
)
2549 struct range_list
*ret_rl
;
2550 const char *return_ranges
;
2555 struct range_list
*rl
= NULL
;
2557 get_absolute_rl(expr
, &rl
);
2558 sm_msg("RETURN: expr='%s' rl='%s' %lu states%s", expr_to_str(expr
),
2559 show_rl(rl
), stree_count(__get_cur_stree()),
2560 is_impossible_path() ? " (impossible path)" : "");
2563 if (__path_is_null())
2566 if (is_impossible_path())
2569 if (expr
&& (expr
->type
== EXPR_COMPARE
||
2570 !get_implied_value(expr
, &sval
)) &&
2571 (is_condition(expr
) || is_boolean(expr
))) {
2572 call_return_state_hooks_compare(expr
);
2574 sm_msg("%s: bool", __func__
);
2576 } else if (call_return_state_hooks_conditional(expr
)) {
2578 sm_msg("%s: condition", __func__
);
2580 } else if (is_kernel_error_path(expr
)) {
2582 sm_msg("%s: kernel error path", __func__
);
2584 } else if (call_return_state_hooks_split_success_fail(expr
)) {
2586 sm_msg("%s: success_fail", __func__
);
2588 } else if (call_return_state_hooks_split_possible(expr
)) {
2590 sm_msg("%s: split_possible", __func__
);
2592 } else if (split_positive_from_negative(expr
)) {
2594 sm_msg("%s: positive negative", __func__
);
2596 } else if (call_return_state_hooks_split_null_non_null_zero(expr
)) {
2598 sm_msg("%s: split zero non-zero", __func__
);
2600 } else if (splitable_function_call(expr
)) {
2602 sm_msg("%s: split_function_call", __func__
);
2604 } else if (split_by_bool_param(expr
)) {
2606 sm_msg("%s: bool param", __func__
);
2608 } else if (split_by_null_nonnull_param(expr
)) {
2610 sm_msg("%s: null non-null param", __func__
);
2612 } else if (split_by_impossible(expr
)) {
2614 sm_msg("%s: split by impossible", __func__
);
2619 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
2620 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
2622 nr_states
= get_db_state_count();
2623 if (nr_states
>= 10000) {
2625 match_return_info(return_id
, (char *)return_ranges
, expr
);
2626 print_limited_param_set(return_id
, (char *)return_ranges
, expr
);
2627 mark_all_params_untracked(return_id
, (char *)return_ranges
, expr
);
2630 call_return_states_callbacks(return_ranges
, expr
);
2632 sm_msg("%s: vanilla", __func__
);
2635 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
2637 struct returned_member_callback
*cb
;
2638 struct sm_state
*sm
;
2639 struct symbol
*type
;
2641 char member_name
[256];
2644 type
= get_type(expr
);
2645 if (!type
|| type
->type
!= SYM_PTR
)
2647 name
= expr_to_var(expr
);
2652 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
2653 FOR_EACH_MY_SM(cb
->owner
, __get_cur_stree(), sm
) {
2654 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
2655 strcpy(member_name
, "*$");
2656 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
2659 if (strncmp(sm
->name
, name
, len
) != 0)
2661 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
2663 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
2664 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
2665 } END_FOR_EACH_SM(sm
);
2666 } END_FOR_EACH_PTR(cb
);
2671 static void print_return_struct_info(int return_id
, char *return_ranges
,
2672 struct expression
*expr
,
2674 struct return_info_callback
*cb
)
2676 struct sm_state
*sm
;
2677 const char *printed_name
;
2680 FOR_EACH_MY_SM(cb
->owner
, __get_cur_stree(), sm
) {
2681 param
= get_param_key_from_var_sym(sm
->name
, sm
->sym
, expr
, &printed_name
);
2686 cb
->callback(return_id
, return_ranges
, expr
, param
, printed_name
, sm
);
2687 } END_FOR_EACH_SM(sm
);
2689 /* always print returned states after processing param states */
2690 FOR_EACH_MY_SM(cb
->owner
, __get_cur_stree(), sm
) {
2691 param
= get_return_param_key_from_var_sym(sm
->name
, sm
->sym
, expr
, &printed_name
);
2692 if (param
!= -1 || !printed_name
)
2694 cb
->callback(return_id
, return_ranges
, expr
, -1, printed_name
, sm
);
2695 } END_FOR_EACH_SM(sm
);
2698 static void print_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
2700 struct return_info_callback
*cb
;
2701 struct expression
*tmp
;
2704 if (!option_info
&& !__inline_fn
&&
2705 !local_debug
&& !option_debug
)
2708 tmp
= get_fake_variable(expr
);
2711 sym
= expr_to_sym(expr
);
2713 FOR_EACH_PTR(return_callbacks
, cb
) {
2714 __ignore_param_used
++;
2715 print_return_struct_info(return_id
, return_ranges
, expr
, sym
, cb
);
2716 __ignore_param_used
--;
2717 } END_FOR_EACH_PTR(cb
);
2720 static void reset_memdb(struct symbol
*sym
)
2722 mem_sql(NULL
, NULL
, "delete from caller_info;");
2723 mem_sql(NULL
, NULL
, "delete from return_states;");
2724 mem_sql(NULL
, NULL
, "delete from call_implies;");
2725 mem_sql(NULL
, NULL
, "delete from return_implies;");
2728 static void match_end_func_info(struct symbol
*sym
)
2730 if (__path_is_null())
2732 call_return_state_hooks(NULL
);
2735 static void match_after_func(struct symbol
*sym
)
2737 clear_cached_return_vals();
2742 static void init_memdb(void)
2746 const char *schema_files
[] = {
2748 "db/caller_info.schema",
2749 "db/common_caller_info.schema",
2750 "db/return_states.schema",
2751 "db/function_type_size.schema",
2752 "db/type_size.schema",
2753 "db/function_type_info.schema",
2754 "db/type_info.schema",
2755 "db/call_implies.schema",
2756 "db/return_implies.schema",
2757 "db/function_ptr.schema",
2758 "db/local_values.schema",
2759 "db/function_type_value.schema",
2760 "db/type_value.schema",
2761 "db/function_type.schema",
2762 "db/data_info.schema",
2763 "db/parameter_name.schema",
2764 "db/constraints.schema",
2765 "db/constraints_required.schema",
2766 "db/fn_ptr_data_link.schema",
2767 "db/fn_data_link.schema",
2768 "db/mtag_about.schema",
2769 "db/mtag_info.schema",
2770 "db/mtag_map.schema",
2771 "db/mtag_data.schema",
2772 "db/mtag_alias.schema",
2774 static char buf
[4096];
2779 rc
= sqlite3_open(":memory:", &mem_db
);
2780 if (rc
!= SQLITE_OK
) {
2781 sm_ierror("starting In-Memory database.");
2785 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
2786 fd
= open_schema_file(schema_files
[i
]);
2789 ret
= read(fd
, buf
, sizeof(buf
));
2791 sm_ierror("failed to read: %s", schema_files
[i
]);
2795 if (ret
== sizeof(buf
)) {
2796 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2797 schema_files
[i
], sizeof(buf
));
2801 rc
= sqlite3_exec(mem_db
, buf
, NULL
, NULL
, &err
);
2802 if (rc
!= SQLITE_OK
) {
2803 sm_ierror("SQL error #2: %s", err
);
2804 sm_ierror("%s", buf
);
2809 static void init_cachedb(void)
2813 const char *schema_files
[] = {
2814 "db/call_implies.schema",
2815 "db/return_implies.schema",
2816 "db/type_info.schema",
2817 "db/mtag_about.schema",
2818 "db/mtag_data.schema",
2819 "db/mtag_info.schema",
2820 "db/sink_info.schema",
2821 "db/hash_string.schema",
2823 static char buf
[4096];
2828 rc
= sqlite3_open(":memory:", &cache_db
);
2829 if (rc
!= SQLITE_OK
) {
2830 sm_ierror("starting In-Memory database.");
2834 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
2835 fd
= open_schema_file(schema_files
[i
]);
2838 ret
= read(fd
, buf
, sizeof(buf
));
2840 sm_ierror("failed to read: %s", schema_files
[i
]);
2844 if (ret
== sizeof(buf
)) {
2845 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2846 schema_files
[i
], sizeof(buf
));
2850 rc
= sqlite3_exec(cache_db
, buf
, NULL
, NULL
, &err
);
2851 if (rc
!= SQLITE_OK
) {
2852 sm_ierror("SQL error #2: %s", err
);
2853 sm_ierror("%s", buf
);
2858 static int save_cache_data(void *_table
, int argc
, char **argv
, char **azColName
)
2860 static char buf
[4096];
2863 char *table
= _table
;
2867 p
+= snprintf(p
, 4096 - (p
- buf
), "insert or ignore into %s values (", table
);
2868 for (i
= 0; i
< argc
; i
++) {
2870 p
+= snprintf(p
, 4096 - (p
- buf
), ", ");
2871 sqlite3_snprintf(sizeof(tmp
), tmp
, "%q", escape_newlines(argv
[i
]));
2872 p
+= snprintf(p
, 4096 - (p
- buf
), "'%s'", tmp
);
2875 p
+= snprintf(p
, 4096 - (p
- buf
), ");");
2879 sm_msg("SQL: %s", buf
);
2883 static void dump_cache(struct symbol_list
*sym_list
)
2885 const char *cache_tables
[] = {
2886 "type_info", "return_implies", "call_implies", "mtag_data",
2887 "mtag_info", "mtag_about", "sink_info", "hash_string",
2895 for (i
= 0; i
< ARRAY_SIZE(cache_tables
); i
++) {
2896 snprintf(buf
, sizeof(buf
), "select * from %s;", cache_tables
[i
]);
2897 cache_sql(&save_cache_data
, (char *)cache_tables
[i
], buf
);
2901 void open_smatch_db(char *db_file
)
2908 use_states
= malloc(num_checks
);
2909 memset(use_states
, 0xff, num_checks
);
2914 rc
= sqlite3_open_v2(db_file
, &smatch_db
, SQLITE_OPEN_READONLY
, NULL
);
2915 if (rc
!= SQLITE_OK
) {
2920 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES
);
2924 static char *get_next_string(char **str
)
2926 static char string
[256];
2935 while (*p
!= '\0' && *p
!= '\n') {
2936 if (*p
== '\\' && *(p
+ 1) == ' ') {
2946 if (len
>= sizeof(string
)) {
2947 memcpy(string
, start
, sizeof(string
));
2948 string
[sizeof(string
) - 1] = '\0';
2949 sm_ierror("return_fix: '%s' too long", string
);
2953 memcpy(string
, start
, len
);
2955 for (i
= 0; i
< sizeof(string
) - 1; i
++) {
2956 if (string
[i
] == '\\' && string
[i
+ 1] == ' ') {
2957 for (j
= i
; string
[j
] != '\0'; j
++)
2958 string
[j
] = string
[j
+ 1];
2967 static void register_return_deletes(void)
2969 char *func
, *ret_str
;
2975 snprintf(filename
, 256, "db/%s.delete.return_states", option_project_str
);
2976 fd
= open_schema_file(filename
);
2979 ret
= read(fd
, buf
, sizeof(buf
));
2983 if (ret
== sizeof(buf
)) {
2984 sm_ierror("file too large: %s (limit %zd bytes)",
2985 filename
, sizeof(buf
));
2992 get_next_string(&p
);
2995 if (delete_count
== 0)
2997 if (delete_count
% 2 != 0) {
2998 printf("error parsing '%s' delete_count=%d\n", filename
, delete_count
);
3002 delete_table
= malloc(delete_count
* sizeof(char *));
3007 func
= alloc_string(get_next_string(&p
));
3008 ret_str
= alloc_string(get_next_string(&p
));
3010 delete_table
[i
++] = func
;
3011 delete_table
[i
++] = ret_str
;
3015 #define RETURN_FIX_SIZE 8196
3016 static void register_return_replacements(void)
3018 char *func
, *orig
, *new;
3024 snprintf(filename
, 256, "db/%s.return_fixes", option_project_str
);
3025 fd
= open_schema_file(filename
);
3028 buf
= malloc(RETURN_FIX_SIZE
);
3029 ret
= read(fd
, buf
, RETURN_FIX_SIZE
);
3035 if (ret
== RETURN_FIX_SIZE
) {
3036 sm_ierror("file too large: %s (limit %d bytes)",
3037 filename
, RETURN_FIX_SIZE
);
3045 get_next_string(&p
);
3048 if (replace_count
== 0) {
3052 if (replace_count
% 3 != 0) {
3053 printf("error parsing '%s' replace_count=%d\n", filename
, replace_count
);
3058 replace_table
= malloc(replace_count
* sizeof(char *));
3063 func
= alloc_string(get_next_string(&p
));
3064 orig
= alloc_string(get_next_string(&p
));
3065 new = alloc_string(get_next_string(&p
));
3067 replace_table
[i
++] = func
;
3068 replace_table
[i
++] = orig
;
3069 replace_table
[i
++] = new;
3074 static void register_forced_return_splits(void)
3076 int struct_members
= sizeof(struct split_data
) / sizeof(char *);
3082 snprintf(filename
, 256, "db/%s.forced_return_splits", option_project_str
);
3083 fd
= open_schema_file(filename
);
3086 ret
= read(fd
, buf
, sizeof(buf
));
3090 if (ret
== sizeof(buf
)) {
3091 sm_ierror("file too large: %s (limit %zd bytes)",
3092 filename
, sizeof(buf
));
3099 get_next_string(&p
);
3102 if (split_count
== 0)
3104 if (split_count
% struct_members
!= 0) {
3105 printf("error parsing '%s' split_count=%d\n", filename
, split_count
);
3109 split_count
/= struct_members
;
3110 forced_splits
= malloc(split_count
* sizeof(void *));
3115 struct split_data
*split
= malloc(sizeof(*split
));
3117 split
->func
= alloc_string(get_next_string(&p
));
3118 split
->rl
= alloc_string(get_next_string(&p
));
3119 forced_splits
[i
++] = split
;
3123 void register_definition_db_callbacks(int id
)
3127 add_hook(&match_call_info
, FUNCTION_CALL_HOOK_BEFORE
);
3128 add_hook(&match_call_info_new
, FUNCTION_CALL_HOOK_BEFORE
);
3129 add_split_return_callback(match_return_info
);
3130 add_split_return_callback(print_returned_struct_members
);
3131 add_split_return_callback(print_return_info
);
3132 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
3133 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
3134 add_hook(&match_after_func
, AFTER_FUNC_HOOK
);
3136 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
3137 add_hook(&match_call_implies
, FUNC_DEF_HOOK
);
3138 add_hook(&match_return_implies_early
, CALL_HOOK_AFTER_INLINE
);
3140 common_funcs
= load_strings_from_file(option_project_str
, "common_functions");
3141 register_return_deletes();
3142 register_return_replacements();
3143 register_forced_return_splits();
3145 add_hook(&dump_cache
, END_FILE_HOOK
);
3148 void register_definition_db_callbacks_late(int id
)
3150 add_hook(&match_return_implies_late
, CALL_HOOK_AFTER_INLINE
);
3153 void register_db_call_marker(int id
)
3155 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK_BEFORE
);
3158 char *get_data_info_name(struct expression
*expr
)
3165 expr
= strip_expr(expr
);
3166 name
= get_member_name(expr
);
3169 name
= expr_to_var_sym(expr
, &sym
);
3172 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
3174 if (sym
->ctype
.modifiers
& MOD_STATIC
)
3175 snprintf(buf
, sizeof(buf
), "static %s", name
);
3177 snprintf(buf
, sizeof(buf
), "global %s", name
);
3178 ret
= alloc_sname(buf
);