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
;
32 #define SQLITE_CACHE_PAGES 1000
36 void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
);
38 ALLOCATOR(def_callback
, "definition db hook callbacks");
39 DECLARE_PTR_LIST(callback_list
, struct def_callback
);
40 static struct callback_list
*select_caller_info_callbacks
;
42 struct member_info_callback
{
44 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
);
46 ALLOCATOR(member_info_callback
, "caller_info callbacks");
47 DECLARE_PTR_LIST(member_info_cb_list
, struct member_info_callback
);
48 static struct member_info_cb_list
*member_callbacks
;
50 struct returned_state_callback
{
51 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*return_expr
);
53 ALLOCATOR(returned_state_callback
, "returned state callbacks");
54 DECLARE_PTR_LIST(returned_state_cb_list
, struct returned_state_callback
);
55 static struct returned_state_cb_list
*returned_state_callbacks
;
57 struct returned_member_callback
{
59 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*expr
, char *printed_name
, struct smatch_state
*state
);
61 ALLOCATOR(returned_member_callback
, "returned member callbacks");
62 DECLARE_PTR_LIST(returned_member_cb_list
, struct returned_member_callback
);
63 static struct returned_member_cb_list
*returned_member_callbacks
;
65 struct db_implies_callback
{
67 void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
);
69 ALLOCATOR(db_implies_callback
, "return_implies callbacks");
70 DECLARE_PTR_LIST(db_implies_cb_list
, struct db_implies_callback
);
71 static struct db_implies_cb_list
*return_implies_cb_list
;
72 static struct db_implies_cb_list
*call_implies_cb_list
;
74 /* silently truncates if needed. */
75 char *escape_newlines(const char *str
)
81 for (i
= 0, j
= 0; str
[i
] != '\0' && j
!= sizeof(buf
); i
++, j
++) {
95 return alloc_sname(str
);
99 return alloc_sname(buf
);
102 static int print_sql_output(void *unused
, int argc
, char **argv
, char **azColName
)
106 for (i
= 0; i
< argc
; i
++) {
109 sm_printf("%s", argv
[i
]);
115 void sql_exec(struct sqlite3
*db
, int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
)
125 if (strncasecmp(sql
, "select", strlen("select")) == 0)
126 sqlite3_exec(db
, sql
, print_sql_output
, NULL
, NULL
);
129 rc
= sqlite3_exec(db
, sql
, callback
, data
, &err
);
130 if (rc
!= SQLITE_OK
&& !parse_error
) {
131 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err
);
132 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql
);
137 static int replace_count
;
138 static char **replace_table
;
139 static const char *replace_return_ranges(const char *return_ranges
)
143 if (!get_function()) {
144 /* I have no idea why EXPORT_SYMBOL() is here */
145 return return_ranges
;
147 for (i
= 0; i
< replace_count
; i
+= 3) {
148 if (strcmp(replace_table
[i
+ 0], get_function()) == 0) {
149 if (strcmp(replace_table
[i
+ 1], return_ranges
) == 0)
150 return replace_table
[i
+ 2];
153 return return_ranges
;
157 static char *use_states
;
158 static int get_db_state_count(void)
163 FOR_EACH_SM(__get_cur_stree(), sm
) {
164 if (sm
->owner
== USHRT_MAX
)
166 if (use_states
[sm
->owner
])
168 } END_FOR_EACH_SM(sm
);
172 void db_ignore_states(int id
)
177 void sql_insert_return_states(int return_id
, const char *return_ranges
,
178 int type
, int param
, const char *key
, const char *value
)
180 if (key
&& strlen(key
) >= 80)
182 return_ranges
= replace_return_ranges(return_ranges
);
183 sql_insert(return_states
, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
184 get_base_file(), get_function(), (unsigned long)__inline_fn
,
185 return_id
, return_ranges
, fn_static(), type
, param
, key
, value
);
188 static struct string_list
*common_funcs
;
189 static int is_common_function(const char *fn
)
196 if (strncmp(fn
, "__builtin_", 10) == 0)
199 FOR_EACH_PTR(common_funcs
, tmp
) {
200 if (strcmp(tmp
, fn
) == 0)
202 } END_FOR_EACH_PTR(tmp
);
207 static char *function_signature(void)
209 return type_to_str(get_real_base_type(cur_func_sym
));
212 void sql_insert_caller_info(struct expression
*call
, int type
,
213 int param
, const char *key
, const char *value
)
215 FILE *tmp_fd
= sm_outfd
;
218 if (!option_info
&& !__inline_call
)
221 if (key
&& strlen(key
) >= 80)
224 fn
= get_fnptr_name(call
->fn
);
230 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
231 get_base_file(), get_function(), fn
, (unsigned long)call
,
232 is_static(call
->fn
), type
, param
, key
, value
);
238 if (strncmp(fn
, "__builtin_", 10) == 0)
240 if (type
!= INTERNAL
&& is_common_function(fn
))
243 sm_outfd
= caller_info_fd
;
244 sm_msg("SQL_caller_info: insert into caller_info values ("
245 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
246 get_base_file(), get_function(), fn
, is_static(call
->fn
),
247 type
, param
, key
, value
);
253 void sql_insert_function_ptr(const char *fn
, const char *struct_name
)
255 sql_insert(function_ptr
, "'%s', '%s', '%s', 0", get_base_file(), fn
,
259 void sql_insert_return_implies(int type
, int param
, const char *key
, const char *value
)
261 sql_insert_or_ignore(return_implies
, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
262 get_base_file(), get_function(), (unsigned long)__inline_fn
,
263 fn_static(), type
, param
, key
, value
);
266 void sql_insert_call_implies(int type
, int param
, const char *key
, const char *value
)
268 sql_insert_or_ignore(call_implies
, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
269 get_base_file(), get_function(), (unsigned long)__inline_fn
,
270 fn_static(), type
, param
, key
, value
);
273 void sql_insert_function_type_size(const char *member
, const char *ranges
)
275 sql_insert(function_type_size
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member
, ranges
);
278 void sql_insert_function_type_info(int type
, const char *struct_type
, const char *member
, const char *value
)
280 sql_insert(function_type_info
, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type
, struct_type
, member
, value
);
283 void sql_insert_type_info(int type
, const char *member
, const char *value
)
285 sql_insert_cache(type_info
, "'%s', %d, '%s', '%s'", get_base_file(), type
, member
, value
);
288 void sql_insert_local_values(const char *name
, const char *value
)
290 sql_insert(local_values
, "'%s', '%s', '%s'", get_base_file(), name
, value
);
293 void sql_insert_function_type_value(const char *type
, const char *value
)
295 sql_insert(function_type_value
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type
, value
);
298 void sql_insert_function_type(int param
, const char *value
)
300 sql_insert(function_type
, "'%s', '%s', %d, %d, '%s'",
301 get_base_file(), get_function(), fn_static(), param
, value
);
304 void sql_insert_parameter_name(int param
, const char *value
)
306 sql_insert(parameter_name
, "'%s', '%s', %d, %d, '%s'",
307 get_base_file(), get_function(), fn_static(), param
, value
);
310 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
)
314 data_name
= get_data_info_name(data
);
317 sql_insert(data_info
, "'%s', '%s', %d, '%s'",
318 is_static(data
) ? get_base_file() : "extern",
319 data_name
, type
, value
);
322 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
)
324 sql_insert(data_info
, "'%s', '%s', %d, '%s'",
325 (sym
->ctype
.modifiers
& MOD_STATIC
) ? get_base_file() : "extern",
329 void sql_save_constraint(const char *con
)
334 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", con
);
337 void sql_save_constraint_required(const char *data
, int op
, const char *limit
)
339 sql_insert_or_ignore(constraints_required
, "'%s', '%s', '%s'", data
, show_special(op
), limit
);
342 void sql_copy_constraint_required(const char *new_limit
, const char *old_limit
)
347 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
348 "select constraints_required.data, constraints_required.op, '%s' from "
349 "constraints_required where bound = '%s';", new_limit
, old_limit
);
352 void sql_insert_fn_ptr_data_link(const char *ptr
, const char *data
)
354 sql_insert_or_ignore(fn_ptr_data_link
, "'%s', '%s'", ptr
, data
);
357 void sql_insert_fn_data_link(struct expression
*fn
, int type
, int param
, const char *key
, const char *value
)
359 if (fn
->type
!= EXPR_SYMBOL
|| !fn
->symbol
->ident
)
362 sql_insert(fn_data_link
, "'%s', '%s', %d, %d, %d, '%s', '%s'",
363 (fn
->symbol
->ctype
.modifiers
& MOD_STATIC
) ? get_base_file() : "extern",
364 fn
->symbol
->ident
->name
,
365 !!(fn
->symbol
->ctype
.modifiers
& MOD_STATIC
),
366 type
, param
, key
, value
);
369 void sql_insert_mtag_about(mtag_t tag
, const char *left_name
, const char *right_name
)
371 sql_insert(mtag_about
, "%lld, '%s', '%s', %d, '%s', '%s'",
372 tag
, get_filename(), get_function(), get_lineno(), left_name
, right_name
);
375 void sql_insert_mtag_data(mtag_t tag
, const char *var
, int offset
, int type
, const char *value
)
377 sql_insert(mtag_data
, "%lld, '%s', %d, %d, '%s'", tag
, var
, offset
, type
, value
);
380 void sql_insert_mtag_map(mtag_t tag
, int offset
, mtag_t container
)
382 sql_insert(mtag_map
, "%lld, %d, %lld", tag
, offset
, container
);
385 void sql_insert_mtag_alias(mtag_t orig
, mtag_t alias
)
387 sql_insert(mtag_alias
, "%lld, %lld", orig
, alias
);
390 static int save_mtag(void *_tag
, int argc
, char **argv
, char **azColName
)
392 mtag_t
*saved_tag
= _tag
;
395 new_tag
= strtoll(argv
[0], NULL
, 10);
398 *saved_tag
= new_tag
;
399 else if (*saved_tag
!= new_tag
)
405 int mtag_map_select_container(mtag_t tag
, int offset
, mtag_t
*container
)
409 run_sql(save_mtag
, &tmp
,
410 "select container from mtag_map where tag = %lld and offset = %d;",
413 if (tmp
== 0 || tmp
== -1ULL)
419 int mtag_map_select_tag(mtag_t container
, int offset
, mtag_t
*tag
)
423 run_sql(save_mtag
, &tmp
,
424 "select tag from mtag_map where container = %lld and offset = %d;",
427 if (tmp
== 0 || tmp
== -1ULL)
433 char *get_static_filter(struct symbol
*sym
)
435 static char sql_filter
[1024];
437 /* This can only happen on buggy code. Return invalid SQL. */
439 sql_filter
[0] = '\0';
443 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
444 snprintf(sql_filter
, sizeof(sql_filter
),
445 "file = '%s' and function = '%s' and static = '1'",
446 get_base_file(), sym
->ident
->name
);
448 snprintf(sql_filter
, sizeof(sql_filter
),
449 "function = '%s' and static = '0'", sym
->ident
->name
);
455 static int get_row_count(void *_row_count
, int argc
, char **argv
, char **azColName
)
457 int *row_count
= _row_count
;
462 *row_count
= atoi(argv
[0]);
466 static void mark_call_params_untracked(struct expression
*call
)
468 struct expression
*arg
;
471 FOR_EACH_PTR(call
->args
, arg
) {
472 mark_untracked(call
, i
++, "$", NULL
);
473 } END_FOR_EACH_PTR(arg
);
476 static void sql_select_return_states_pointer(const char *cols
,
477 struct expression
*call
, int (*callback
)(void*, int, char**, char**), void *info
)
480 int return_count
= 0;
482 ptr
= get_fnptr_name(call
->fn
);
486 run_sql(get_row_count
, &return_count
,
487 "select count(*) from return_states join function_ptr "
488 "where return_states.function == function_ptr.function and "
489 "ptr = '%s' and searchable = 1 and type = %d;", ptr
, INTERNAL
);
490 /* The magic number 100 is just from testing on the kernel. */
491 if (return_count
> 100) {
492 mark_call_params_untracked(call
);
496 run_sql(callback
, info
,
497 "select %s from return_states join function_ptr where "
498 "return_states.function == function_ptr.function and ptr = '%s' "
499 "and searchable = 1 "
500 "order by function_ptr.file, return_states.file, return_id, type;",
504 static int is_local_symbol(struct expression
*expr
)
506 if (expr
->type
!= EXPR_SYMBOL
)
508 if (expr
->symbol
->ctype
.modifiers
& (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
))
513 void sql_select_return_states(const char *cols
, struct expression
*call
,
514 int (*callback
)(void*, int, char**, char**), void *info
)
518 if (is_fake_call(call
))
521 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
|| is_local_symbol(call
->fn
)) {
522 sql_select_return_states_pointer(cols
, call
, callback
, info
);
526 if (inlinable(call
->fn
)) {
527 mem_sql(callback
, info
,
528 "select %s from return_states where call_id = '%lu' order by return_id, type;",
529 cols
, (unsigned long)call
);
533 run_sql(get_row_count
, &row_count
, "select count(*) from return_states where %s;",
534 get_static_filter(call
->fn
->symbol
));
535 if (row_count
> 3000)
538 run_sql(callback
, info
, "select %s from return_states where %s order by file, return_id, type;",
539 cols
, get_static_filter(call
->fn
->symbol
));
542 #define CALL_IMPLIES 0
543 #define RETURN_IMPLIES 1
545 struct implies_info
{
547 struct db_implies_cb_list
*cb_list
;
548 struct expression
*expr
;
552 void sql_select_implies(const char *cols
, struct implies_info
*info
,
553 int (*callback
)(void*, int, char**, char**))
555 if (info
->type
== RETURN_IMPLIES
&& inlinable(info
->expr
->fn
)) {
556 mem_sql(callback
, info
,
557 "select %s from return_implies where call_id = '%lu';",
558 cols
, (unsigned long)info
->expr
);
562 run_sql(callback
, info
, "select %s from %s_implies where %s;",
564 info
->type
== CALL_IMPLIES
? "call" : "return",
565 get_static_filter(info
->sym
));
568 struct select_caller_info_data
{
569 struct stree
*final_states
;
570 struct timeval start_time
;
576 static int caller_info_callback(void *_data
, int argc
, char **argv
, char **azColName
);
578 static void sql_select_caller_info(struct select_caller_info_data
*data
,
579 const char *cols
, struct symbol
*sym
)
582 mem_sql(caller_info_callback
, data
,
583 "select %s from caller_info where call_id = %lu;",
584 cols
, (unsigned long)__inline_fn
);
588 if (sym
->ident
->name
&& is_common_function(sym
->ident
->name
))
590 run_sql(caller_info_callback
, data
,
591 "select %s from common_caller_info where %s order by call_id;",
592 cols
, get_static_filter(sym
));
596 run_sql(caller_info_callback
, data
,
597 "select %s from caller_info where %s order by call_id;",
598 cols
, get_static_filter(sym
));
601 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
603 struct def_callback
*def_callback
= __alloc_def_callback(0);
605 def_callback
->hook_type
= type
;
606 def_callback
->callback
= callback
;
607 add_ptr_list(&select_caller_info_callbacks
, def_callback
);
611 * These call backs are used when the --info option is turned on to print struct
612 * member information. For example foo->bar could have a state in
613 * smatch_extra.c and also check_user.c.
615 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
617 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
619 member_callback
->owner
= owner
;
620 member_callback
->callback
= callback
;
621 add_ptr_list(&member_callbacks
, member_callback
);
624 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
626 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
628 callback
->callback
= fn
;
629 add_ptr_list(&returned_state_callbacks
, callback
);
632 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
))
634 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
636 member_callback
->owner
= owner
;
637 member_callback
->callback
= callback
;
638 add_ptr_list(&returned_member_callbacks
, member_callback
);
641 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
))
643 struct db_implies_callback
*cb
= __alloc_db_implies_callback(0);
646 cb
->callback
= callback
;
647 add_ptr_list(&call_implies_cb_list
, cb
);
650 void select_return_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
))
652 struct db_implies_callback
*cb
= __alloc_db_implies_callback(0);
655 cb
->callback
= callback
;
656 add_ptr_list(&return_implies_cb_list
, cb
);
660 struct expression
*static_returns_call
;
661 struct symbol
*return_type
;
662 struct range_list
*return_range_list
;
665 static int db_return_callback(void *_ret_info
, int argc
, char **argv
, char **azColName
)
667 struct return_info
*ret_info
= _ret_info
;
668 struct range_list
*rl
;
669 struct expression
*call_expr
= ret_info
->static_returns_call
;
673 call_results_to_rl(call_expr
, ret_info
->return_type
, argv
[0], &rl
);
674 ret_info
->return_range_list
= rl_union(ret_info
->return_range_list
, rl
);
678 struct range_list
*db_return_vals(struct expression
*expr
)
680 struct return_info ret_info
= {};
684 if (is_fake_call(expr
))
687 snprintf(buf
, sizeof(buf
), "return %p", expr
);
688 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
690 return clone_rl(estate_rl(sm
->state
));
691 ret_info
.static_returns_call
= expr
;
692 ret_info
.return_type
= get_type(expr
);
693 if (!ret_info
.return_type
)
696 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
699 ret_info
.return_range_list
= NULL
;
700 if (inlinable(expr
->fn
)) {
701 mem_sql(db_return_callback
, &ret_info
,
702 "select distinct return from return_states where call_id = '%lu';",
703 (unsigned long)expr
);
705 run_sql(db_return_callback
, &ret_info
,
706 "select distinct return from return_states where %s;",
707 get_static_filter(expr
->fn
->symbol
));
709 return ret_info
.return_range_list
;
712 struct range_list
*db_return_vals_from_str(const char *fn_name
)
714 struct return_info ret_info
;
716 ret_info
.static_returns_call
= NULL
;
717 ret_info
.return_type
= &llong_ctype
;
718 ret_info
.return_range_list
= NULL
;
720 run_sql(db_return_callback
, &ret_info
,
721 "select distinct return from return_states where function = '%s';",
723 return ret_info
.return_range_list
;
726 static void match_call_marker(struct expression
*expr
)
730 type
= get_type(expr
->fn
);
731 if (type
&& type
->type
== SYM_PTR
)
732 type
= get_real_base_type(type
);
735 * we just want to record something in the database so that if we have
736 * two calls like: frob(4); frob(some_unkown); then on the receiving
737 * side we know that sometimes frob is called with unknown parameters.
740 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", type_to_str(type
));
743 static char *show_offset(int offset
)
749 snprintf(buf
, sizeof(buf
), "(-%d)", offset
);
753 int is_recursive_member(const char *name
)
756 const char *p
, *next
;
759 p
= strchr(name
, '>');
764 next
= strchr(p
, '>');
770 if (size
>= sizeof(buf
))
772 memcpy(buf
, p
, size
);
774 if (strstr(next
, buf
))
780 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, int offset
, struct stree
*stree
,
781 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
787 char printed_name
[256];
791 expr
= strip_expr(expr
);
794 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
795 expr
= strip_expr(expr
->unop
);
799 type
= get_type(expr
);
800 if (type
&& type_bits(type
) < type_bits(&ulong_ctype
))
803 name
= expr_to_var_sym(expr
, &sym
);
808 FOR_EACH_SM(stree
, sm
) {
811 if (strcmp(name
, sm
->name
) == 0) {
813 snprintf(printed_name
, sizeof(printed_name
), "*$%s", show_offset(offset
));
814 else /* these are already handled. fixme: handle them here */
816 } else if (sm
->name
[0] == '*' && strcmp(name
, sm
->name
+ 1) == 0) {
817 snprintf(printed_name
, sizeof(printed_name
), "*$%s", show_offset(offset
));
818 } else if (strncmp(name
, sm
->name
, len
) == 0) {
819 if (isalnum(sm
->name
[len
]))
822 snprintf(printed_name
, sizeof(printed_name
), "$%s->%s", show_offset(offset
), sm
->name
+ len
+ 1);
824 snprintf(printed_name
, sizeof(printed_name
), "$%s%s", show_offset(offset
), sm
->name
+ len
);
828 if (is_recursive_member(printed_name
))
830 callback(call
, param
, printed_name
, sm
);
831 } END_FOR_EACH_SM(sm
);
836 static int param_used_callback(void *_container
, int argc
, char **argv
, char **azColName
)
838 char **container
= _container
;
839 static char buf
[256];
841 snprintf(buf
, sizeof(buf
), "%s", argv
[0]);
846 static void print_container_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct stree
*stree
,
847 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
849 struct expression
*tmp
;
850 char *container
= NULL
;
855 if (!call
->fn
|| call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
859 * We can't use the in-mem DB because we have to parse the function
860 * first, then we know if it takes a container, then we know to pass it
861 * the container data.
864 run_sql(¶m_used_callback
, &container
,
865 "select key from return_implies where %s and type = %d and key like '%%$(%%' and parameter = %d limit 1;",
866 get_static_filter(call
->fn
->symbol
), CONTAINER
, param
);
870 p
= strchr(container
, '-');
879 tmp
= get_assigned_expr(expr
);
883 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '&')
885 expr
= strip_expr(expr
->unop
);
886 holder_offset
= get_member_offset_from_deref(expr
);
887 if (-holder_offset
!= offset
)
890 expr
= strip_expr(expr
->deref
);
891 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
892 expr
= strip_expr(expr
->unop
);
894 print_struct_members(call
, expr
, param
, holder_offset
, stree
, callback
);
897 static void match_call_info(struct expression
*call
)
899 struct member_info_callback
*cb
;
900 struct expression
*arg
;
905 name
= get_fnptr_name(call
->fn
);
909 FOR_EACH_PTR(member_callbacks
, cb
) {
910 stree
= get_all_states_stree(cb
->owner
);
912 FOR_EACH_PTR(call
->args
, arg
) {
913 print_struct_members(call
, arg
, i
, -1, stree
, cb
->callback
);
914 print_container_struct_members(call
, arg
, i
, stree
, cb
->callback
);
916 } END_FOR_EACH_PTR(arg
);
918 } END_FOR_EACH_PTR(cb
);
923 static int get_param(int param
, char **name
, struct symbol
**sym
)
929 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
931 * this is a temporary hack to work around a bug (I think in sparse?)
932 * 2.6.37-rc1:fs/reiserfs/journal.o
933 * If there is a function definition without parameter name found
934 * after a function implementation then it causes a crash.
938 if (arg
->ident
->name
< (char *)100)
941 *name
= arg
->ident
->name
;
946 } END_FOR_EACH_PTR(arg
);
951 static int function_signature_matches(const char *sig
)
955 my_sig
= function_signature();
957 return 1; /* default to matching */
958 if (strcmp(my_sig
, sig
) == 0)
963 static int caller_info_callback(void *_data
, int argc
, char **argv
, char **azColName
)
965 struct select_caller_info_data
*data
= _data
;
972 struct symbol
*sym
= NULL
;
973 struct def_callback
*def_callback
;
975 struct timeval cur_time
;
982 gettimeofday(&cur_time
, NULL
);
983 if (cur_time
.tv_sec
- data
->start_time
.tv_sec
> 10)
986 func_id
= atoi(argv
[0]);
988 type
= strtol(argv
[1], NULL
, 10);
989 param
= strtol(argv
[2], NULL
, 10);
995 if (data
->prev_func_id
== -1)
996 data
->prev_func_id
= func_id
;
997 if (func_id
!= data
->prev_func_id
) {
998 stree
= __pop_fake_cur_stree();
1000 merge_stree(&data
->final_states
, stree
);
1002 __push_fake_cur_stree();
1004 data
->prev_func_id
= func_id
;
1010 if (type
== INTERNAL
&&
1011 !function_signature_matches(value
)) {
1016 if (param
>= 0 && !get_param(param
, &name
, &sym
))
1019 FOR_EACH_PTR(select_caller_info_callbacks
, def_callback
) {
1020 if (def_callback
->hook_type
== type
)
1021 def_callback
->callback(name
, sym
, key
, value
);
1022 } END_FOR_EACH_PTR(def_callback
);
1027 static struct string_list
*ptr_names_done
;
1028 static struct string_list
*ptr_names
;
1030 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
1032 insert_string(&ptr_names
, alloc_string(argv
[0]));
1036 static char *get_next_ptr_name(void)
1040 FOR_EACH_PTR(ptr_names
, ptr
) {
1041 if (list_has_string(ptr_names_done
, ptr
))
1043 insert_string(&ptr_names_done
, ptr
);
1045 } END_FOR_EACH_PTR(ptr
);
1049 static void get_ptr_names(const char *file
, const char *name
)
1051 char sql_filter
[1024];
1055 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
1058 snprintf(sql_filter
, 1024, "function = '%s';", name
);
1061 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
1063 run_sql(get_ptr_name
, NULL
,
1064 "select distinct ptr from function_ptr where %s",
1067 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
1068 if (before
== after
)
1071 while ((name
= get_next_ptr_name()))
1072 get_ptr_names(NULL
, name
);
1075 static void match_data_from_db(struct symbol
*sym
)
1077 struct select_caller_info_data data
= { .prev_func_id
= -1 };
1078 struct sm_state
*sm
;
1079 struct stree
*stree
;
1080 struct timeval end_time
;
1082 if (!sym
|| !sym
->ident
)
1085 gettimeofday(&data
.start_time
, NULL
);
1087 __push_fake_cur_stree();
1093 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1094 get_ptr_names(get_base_file(), sym
->ident
->name
);
1096 get_ptr_names(NULL
, sym
->ident
->name
);
1098 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
1099 __free_ptr_list((struct ptr_list
**)&ptr_names
);
1100 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
1101 stree
= __pop_fake_cur_stree();
1106 sql_select_caller_info(&data
,
1107 "call_id, type, parameter, key, value",
1111 stree
= __pop_fake_cur_stree();
1113 merge_stree(&data
.final_states
, stree
);
1115 __push_fake_cur_stree();
1117 data
.prev_func_id
= -1;
1120 FOR_EACH_PTR(ptr_names
, ptr
) {
1121 run_sql(caller_info_callback
, &data
,
1122 "select call_id, type, parameter, key, value"
1123 " from common_caller_info where function = '%s' order by call_id",
1125 } END_FOR_EACH_PTR(ptr
);
1128 FOR_EACH_PTR(ptr_names
, ptr
) {
1130 } END_FOR_EACH_PTR(ptr
);
1131 goto free_ptr_names
;
1134 FOR_EACH_PTR(ptr_names
, ptr
) {
1135 run_sql(caller_info_callback
, &data
,
1136 "select call_id, type, parameter, key, value"
1137 " from caller_info where function = '%s' order by call_id",
1140 } END_FOR_EACH_PTR(ptr
);
1143 __free_ptr_list((struct ptr_list
**)&ptr_names
);
1144 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
1146 sql_select_caller_info(&data
,
1147 "call_id, type, parameter, key, value",
1151 stree
= __pop_fake_cur_stree();
1153 merge_stree(&data
.final_states
, stree
);
1156 gettimeofday(&end_time
, NULL
);
1157 if (end_time
.tv_sec
- data
.start_time
.tv_sec
<= 10) {
1158 FOR_EACH_SM(data
.final_states
, sm
) {
1160 } END_FOR_EACH_SM(sm
);
1163 free_stree(&data
.final_states
);
1166 static int return_implies_callbacks(void *_info
, int argc
, char **argv
, char **azColName
)
1168 struct implies_info
*info
= _info
;
1169 struct db_implies_callback
*cb
;
1170 struct expression
*arg
= NULL
;
1177 type
= atoi(argv
[1]);
1178 param
= atoi(argv
[2]);
1180 FOR_EACH_PTR(info
->cb_list
, cb
) {
1181 if (cb
->type
!= type
)
1184 arg
= get_argument_from_call_expr(info
->expr
->args
, param
);
1188 cb
->callback(info
->expr
, arg
, argv
[3], argv
[4]);
1189 } END_FOR_EACH_PTR(cb
);
1194 static int call_implies_callbacks(void *_info
, int argc
, char **argv
, char **azColName
)
1196 struct implies_info
*info
= _info
;
1197 struct db_implies_callback
*cb
;
1198 struct expression
*arg
;
1207 type
= atoi(argv
[1]);
1208 param
= atoi(argv
[2]);
1210 if (!get_param(param
, &name
, &sym
))
1212 arg
= symbol_expression(sym
);
1216 FOR_EACH_PTR(info
->cb_list
, cb
) {
1217 if (cb
->type
!= type
)
1219 cb
->callback(info
->expr
, arg
, argv
[3], argv
[4]);
1220 } END_FOR_EACH_PTR(cb
);
1225 static void match_return_implies(struct expression
*expr
)
1227 struct implies_info info
= {
1228 .type
= RETURN_IMPLIES
,
1229 .cb_list
= return_implies_cb_list
,
1232 if (expr
->fn
->type
!= EXPR_SYMBOL
||
1236 info
.sym
= expr
->fn
->symbol
;
1237 sql_select_implies("function, type, parameter, key, value", &info
,
1238 return_implies_callbacks
);
1241 static void match_call_implies(struct symbol
*sym
)
1243 struct implies_info info
= {
1244 .type
= CALL_IMPLIES
,
1245 .cb_list
= call_implies_cb_list
,
1248 if (!sym
|| !sym
->ident
)
1252 sql_select_implies("function, type, parameter, key, value", &info
,
1253 call_implies_callbacks
);
1256 static void print_initializer_list(struct expression_list
*expr_list
,
1257 struct symbol
*struct_type
)
1259 struct expression
*expr
;
1260 struct symbol
*base_type
;
1261 char struct_name
[256];
1263 FOR_EACH_PTR(expr_list
, expr
) {
1264 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
1265 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
1268 if (expr
->type
!= EXPR_IDENTIFIER
)
1270 if (!expr
->expr_ident
)
1272 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
1274 base_type
= get_type(expr
->ident_expression
);
1275 if (!base_type
|| base_type
->type
!= SYM_FN
)
1277 snprintf(struct_name
, sizeof(struct_name
), "(struct %s)->%s",
1278 struct_type
->ident
->name
, expr
->expr_ident
->name
);
1279 sql_insert_function_ptr(expr
->ident_expression
->symbol_name
->name
,
1281 } END_FOR_EACH_PTR(expr
);
1284 static void global_variable(struct symbol
*sym
)
1286 struct symbol
*struct_type
;
1290 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
1292 struct_type
= get_base_type(sym
);
1295 if (struct_type
->type
== SYM_ARRAY
) {
1296 struct_type
= get_base_type(struct_type
);
1300 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
1302 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
1305 static char *get_return_compare_is_param(struct expression
*expr
)
1312 param
= get_param_num(expr
);
1316 var
= expr_to_var(expr
);
1319 snprintf(buf
, sizeof(buf
), "%s orig", var
);
1320 comparison
= get_comparison_strings(var
, buf
);
1326 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(comparison
), param
);
1327 return alloc_sname(buf
);
1330 static char *get_return_compare_str(struct expression
*expr
)
1334 compare_str
= get_return_compare_is_param(expr
);
1338 compare_str
= expr_lte_to_param(expr
, -1);
1342 return expr_param_comparison(expr
, -1);
1345 static const char *get_return_ranges_str(struct expression
*expr
, struct range_list
**rl_p
)
1347 struct range_list
*rl
;
1348 char *return_ranges
;
1357 return alloc_sname("");
1359 if (get_implied_value(expr
, &sval
)) {
1360 sval
= sval_cast(cur_func_return_type(), sval
);
1361 *rl_p
= alloc_rl(sval
, sval
);
1362 return sval_to_str_or_err_ptr(sval
);
1365 compare_str
= expr_equal_to_param(expr
, -1);
1366 math_str
= get_value_in_terms_of_parameter_math(expr
);
1368 if (get_implied_rl(expr
, &rl
)) {
1369 rl
= cast_rl(cur_func_return_type(), rl
);
1370 return_ranges
= show_rl(rl
);
1371 } else if (get_imaginary_absolute(expr
, &rl
)){
1372 rl
= cast_rl(cur_func_return_type(), rl
);
1373 return alloc_sname(show_rl(rl
));
1375 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
)));
1376 return_ranges
= show_rl(rl
);
1381 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1382 return alloc_sname(buf
);
1385 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1386 return alloc_sname(buf
);
1388 compare_str
= get_return_compare_str(expr
);
1390 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1391 return alloc_sname(buf
);
1394 return return_ranges
;
1397 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
1399 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", function_signature());
1402 static void call_return_state_hooks_conditional(struct expression
*expr
)
1404 struct returned_state_callback
*cb
;
1405 struct range_list
*rl
;
1406 const char *return_ranges
;
1407 int final_pass_orig
= final_pass
;
1409 __push_fake_cur_stree();
1412 __split_whole_condition(expr
->conditional
);
1413 final_pass
= final_pass_orig
;
1415 return_ranges
= get_return_ranges_str(expr
->cond_true
?: expr
->conditional
, &rl
);
1417 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(rl
));
1420 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1421 cb
->callback(return_id
, (char *)return_ranges
, expr
->cond_true
);
1422 } END_FOR_EACH_PTR(cb
);
1424 __push_true_states();
1425 __use_false_states();
1427 return_ranges
= get_return_ranges_str(expr
->cond_false
, &rl
);
1428 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(rl
));
1431 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1432 cb
->callback(return_id
, (char *)return_ranges
, expr
->cond_false
);
1433 } END_FOR_EACH_PTR(cb
);
1435 __merge_true_states();
1436 __free_fake_cur_stree();
1439 static void call_return_state_hooks_compare(struct expression
*expr
)
1441 struct returned_state_callback
*cb
;
1442 char *return_ranges
;
1443 int final_pass_orig
= final_pass
;
1444 sval_t sval
= { .type
= &int_ctype
};
1447 if (!get_implied_value(expr
, &ret
))
1450 __push_fake_cur_stree();
1453 __split_whole_condition(expr
);
1454 final_pass
= final_pass_orig
;
1456 if (ret
.value
!= 0) {
1457 return_ranges
= alloc_sname("1");
1459 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1462 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1463 cb
->callback(return_id
, return_ranges
, expr
);
1464 } END_FOR_EACH_PTR(cb
);
1467 __push_true_states();
1468 __use_false_states();
1470 if (ret
.value
!= 1) {
1471 return_ranges
= alloc_sname("0");
1473 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1476 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1477 cb
->callback(return_id
, return_ranges
, expr
);
1478 } END_FOR_EACH_PTR(cb
);
1481 __merge_true_states();
1482 __free_fake_cur_stree();
1485 static int ptr_in_list(struct sm_state
*sm
, struct state_list
*slist
)
1487 struct sm_state
*tmp
;
1489 FOR_EACH_PTR(slist
, tmp
) {
1490 if (strcmp(tmp
->state
->name
, sm
->state
->name
) == 0)
1492 } END_FOR_EACH_PTR(tmp
);
1497 static int split_possible_helper(struct sm_state
*sm
, struct expression
*expr
)
1499 struct returned_state_callback
*cb
;
1500 struct range_list
*rl
;
1501 char *return_ranges
;
1502 struct sm_state
*tmp
;
1504 int nr_possible
, nr_states
;
1505 char *compare_str
= NULL
;
1507 struct state_list
*already_handled
= NULL
;
1509 if (!sm
|| !sm
->merged
)
1512 if (too_many_possible(sm
))
1515 /* bail if it gets too complicated */
1516 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
1517 nr_states
= get_db_state_count();
1518 if (nr_states
* nr_possible
>= 2000)
1521 FOR_EACH_PTR(sm
->possible
, tmp
) {
1524 if (ptr_in_list(tmp
, already_handled
))
1526 add_ptr_list(&already_handled
, tmp
);
1529 __push_fake_cur_stree();
1531 overwrite_states_using_pool(sm
, tmp
);
1533 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
1534 return_ranges
= show_rl(rl
);
1535 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(clone_rl(rl
)));
1536 compare_str
= get_return_compare_str(expr
);
1538 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1539 return_ranges
= alloc_sname(buf
);
1543 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1544 cb
->callback(return_id
, return_ranges
, expr
);
1545 } END_FOR_EACH_PTR(cb
);
1547 __free_fake_cur_stree();
1548 } END_FOR_EACH_PTR(tmp
);
1550 free_slist(&already_handled
);
1555 static int call_return_state_hooks_split_possible(struct expression
*expr
)
1557 struct sm_state
*sm
;
1559 if (!expr
|| expr_equal_to_param(expr
, -1))
1562 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1563 return split_possible_helper(sm
, expr
);
1566 static bool has_possible_negative(struct sm_state
*sm
)
1568 struct sm_state
*tmp
;
1570 FOR_EACH_PTR(sm
->possible
, tmp
) {
1571 if (!estate_rl(tmp
->state
))
1573 if (sval_is_negative(estate_min(tmp
->state
)) &&
1574 sval_is_negative(estate_max(tmp
->state
)))
1576 } END_FOR_EACH_PTR(tmp
);
1581 static bool has_possible_zero_null(struct sm_state
*sm
)
1583 struct sm_state
*tmp
;
1586 FOR_EACH_PTR(sm
->possible
, tmp
) {
1587 if (!estate_get_single_value(tmp
->state
, &sval
))
1589 if (sval
.value
== 0)
1591 } END_FOR_EACH_PTR(tmp
);
1596 static int split_positive_from_negative(struct expression
*expr
)
1598 struct sm_state
*sm
;
1599 struct returned_state_callback
*cb
;
1600 struct range_list
*rl
;
1601 const char *return_ranges
;
1602 struct range_list
*ret_rl
;
1606 /* We're going to print the states 3 times */
1607 if (get_db_state_count() > 10000 / 3)
1610 if (!get_implied_rl(expr
, &rl
) || !rl
)
1612 if (is_whole_rl(rl
) || is_whole_rl_non_zero(rl
))
1614 /* Forget about INT_MAX and larger */
1615 if (rl_max(rl
).value
<= 0)
1617 if (!sval_is_negative(rl_min(rl
)))
1620 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1623 if (!has_possible_negative(sm
))
1625 has_zero
= has_possible_zero_null(sm
);
1627 if (!assume(compare_expression(expr
, has_zero
? '>' : SPECIAL_GTE
, zero_expr())))
1631 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1632 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1633 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1634 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1635 } END_FOR_EACH_PTR(cb
);
1640 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
1643 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1644 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1645 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1646 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1647 } END_FOR_EACH_PTR(cb
);
1653 undo
= assume(compare_expression(expr
, '<', zero_expr()));
1656 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1657 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1658 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1659 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1660 } END_FOR_EACH_PTR(cb
);
1668 static int call_return_state_hooks_split_null_non_null(struct expression
*expr
)
1670 struct returned_state_callback
*cb
;
1671 struct range_list
*rl
;
1672 struct range_list
*nonnull_rl
;
1674 struct range_list
*null_rl
= NULL
;
1675 char *return_ranges
;
1676 struct sm_state
*sm
;
1677 struct smatch_state
*state
;
1679 int final_pass_orig
= final_pass
;
1681 if (!expr
|| expr_equal_to_param(expr
, -1))
1683 if (expr
->type
== EXPR_CALL
)
1685 if (!is_pointer(expr
))
1688 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1691 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
1694 if (!estate_rl(state
))
1696 if (estate_min(state
).value
== 0 && estate_max(state
).value
== 0)
1698 if (!has_possible_zero_null(sm
))
1701 nr_states
= get_db_state_count();
1702 if (option_info
&& nr_states
>= 1500)
1705 rl
= estate_rl(state
);
1707 __push_fake_cur_stree();
1710 __split_whole_condition(expr
);
1711 final_pass
= final_pass_orig
;
1713 nonnull_rl
= rl_filter(rl
, rl_zero());
1714 return_ranges
= show_rl(nonnull_rl
);
1715 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonnull_rl
));
1718 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1719 cb
->callback(return_id
, return_ranges
, expr
);
1720 } END_FOR_EACH_PTR(cb
);
1722 __push_true_states();
1723 __use_false_states();
1725 return_ranges
= alloc_sname("0");
1726 null_sval
= sval_type_val(rl_type(rl
), 0);
1727 add_range(&null_rl
, null_sval
, null_sval
);
1728 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(null_rl
));
1730 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1731 cb
->callback(return_id
, return_ranges
, expr
);
1732 } END_FOR_EACH_PTR(cb
);
1734 __merge_true_states();
1735 __free_fake_cur_stree();
1740 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
1742 struct sm_state
*sm
;
1743 struct range_list
*rl
;
1744 struct range_list
*nonzero_rl
;
1746 struct range_list
*zero_rl
= NULL
;
1748 struct returned_state_callback
*cb
;
1749 char *return_ranges
;
1750 int final_pass_orig
= final_pass
;
1752 if (option_project
!= PROJ_KERNEL
)
1755 nr_states
= get_db_state_count();
1756 if (nr_states
> 1500)
1759 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1762 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
1765 rl
= estate_rl(sm
->state
);
1769 if (rl_min(rl
).value
< -4095 || rl_min(rl
).value
>= 0)
1771 if (rl_max(rl
).value
!= 0)
1773 if (!has_possible_zero_null(sm
))
1776 __push_fake_cur_stree();
1779 __split_whole_condition(expr
);
1780 final_pass
= final_pass_orig
;
1782 nonzero_rl
= rl_filter(rl
, rl_zero());
1783 nonzero_rl
= cast_rl(cur_func_return_type(), nonzero_rl
);
1784 return_ranges
= show_rl(nonzero_rl
);
1785 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonzero_rl
));
1788 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1789 cb
->callback(return_id
, return_ranges
, expr
);
1790 } END_FOR_EACH_PTR(cb
);
1792 __push_true_states();
1793 __use_false_states();
1795 return_ranges
= alloc_sname("0");
1796 zero_sval
= sval_type_val(rl_type(rl
), 0);
1797 add_range(&zero_rl
, zero_sval
, zero_sval
);
1798 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(zero_rl
));
1800 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1801 cb
->callback(return_id
, return_ranges
, expr
);
1802 } END_FOR_EACH_PTR(cb
);
1804 __merge_true_states();
1805 __free_fake_cur_stree();
1810 static int is_boolean(struct expression
*expr
)
1812 struct range_list
*rl
;
1814 if (!get_implied_rl(expr
, &rl
))
1816 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
1821 static int is_conditional(struct expression
*expr
)
1825 if (expr
->type
== EXPR_CONDITIONAL
|| expr
->type
== EXPR_SELECT
)
1830 static int splitable_function_call(struct expression
*expr
)
1832 struct sm_state
*sm
;
1835 if (!expr
|| expr
->type
!= EXPR_CALL
)
1837 snprintf(buf
, sizeof(buf
), "return %p", expr
);
1838 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
1839 return split_possible_helper(sm
, expr
);
1842 static struct sm_state
*find_bool_param(void)
1844 struct stree
*start_states
;
1846 struct sm_state
*sm
, *tmp
;
1849 start_states
= get_start_states();
1851 FOR_EACH_PTR_REVERSE(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
1854 sm
= get_sm_state_stree(start_states
, SMATCH_EXTRA
, arg
->ident
->name
, arg
);
1857 if (rl_min(estate_rl(sm
->state
)).value
!= 0 ||
1858 rl_max(estate_rl(sm
->state
)).value
!= 1)
1861 } END_FOR_EACH_PTR_REVERSE(arg
);
1867 * Check if it's splitable. If not, then splitting it up is likely not
1868 * useful for the callers.
1870 FOR_EACH_PTR(sm
->possible
, tmp
) {
1873 if (!estate_get_single_value(tmp
->state
, &sval
))
1875 } END_FOR_EACH_PTR(tmp
);
1880 static int split_on_bool_sm(struct sm_state
*sm
, struct expression
*expr
)
1882 struct returned_state_callback
*cb
;
1883 struct range_list
*ret_rl
;
1884 const char *return_ranges
;
1885 struct sm_state
*tmp
;
1887 int nr_possible
, nr_states
;
1888 char *compare_str
= NULL
;
1890 struct state_list
*already_handled
= NULL
;
1892 if (!sm
|| !sm
->merged
)
1895 if (too_many_possible(sm
))
1898 /* bail if it gets too complicated */
1899 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
1900 nr_states
= get_db_state_count();
1901 if (nr_states
* nr_possible
>= 2000)
1904 FOR_EACH_PTR(sm
->possible
, tmp
) {
1907 if (ptr_in_list(tmp
, already_handled
))
1909 add_ptr_list(&already_handled
, tmp
);
1912 __push_fake_cur_stree();
1914 overwrite_states_using_pool(sm
, tmp
);
1916 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1917 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1918 compare_str
= get_return_compare_str(expr
);
1920 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1921 return_ranges
= alloc_sname(buf
);
1925 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1926 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1927 } END_FOR_EACH_PTR(cb
);
1929 __free_fake_cur_stree();
1930 } END_FOR_EACH_PTR(tmp
);
1932 free_slist(&already_handled
);
1937 static int split_by_bool_param(struct expression
*expr
)
1939 struct sm_state
*start_sm
, *sm
;
1942 start_sm
= find_bool_param();
1945 sm
= get_sm_state(SMATCH_EXTRA
, start_sm
->name
, start_sm
->sym
);
1946 if (!sm
|| estate_get_single_value(sm
->state
, &sval
))
1948 return split_on_bool_sm(sm
, expr
);
1951 static int split_by_null_nonnull_param(struct expression
*expr
)
1954 struct sm_state
*sm
;
1956 .type
= &ulong_ctype
,
1959 /* function must only take one pointer */
1960 if (ptr_list_size((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
) != 1)
1962 arg
= first_ptr_list((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
);
1965 if (get_real_base_type(arg
)->type
!= SYM_PTR
)
1968 if (param_was_set_var_sym(arg
->ident
->name
, arg
))
1970 sm
= get_sm_state(SMATCH_EXTRA
, arg
->ident
->name
, arg
);
1974 if (!rl_has_sval(estate_rl(sm
->state
), zero
))
1977 return split_on_bool_sm(sm
, expr
);
1980 struct expression
*strip_expr_statement(struct expression
*expr
)
1982 struct expression
*orig
= expr
;
1983 struct statement
*stmt
, *last_stmt
;
1987 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '(')
1989 if (expr
->type
!= EXPR_STATEMENT
)
1991 stmt
= expr
->statement
;
1992 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
1995 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
1996 if (!last_stmt
|| last_stmt
->type
== STMT_LABEL
)
1997 last_stmt
= last_stmt
->label_statement
;
1998 if (!last_stmt
|| last_stmt
->type
!= STMT_EXPRESSION
)
2000 return strip_expr(last_stmt
->expression
);
2003 static void call_return_state_hooks(struct expression
*expr
)
2005 struct returned_state_callback
*cb
;
2006 struct range_list
*ret_rl
;
2007 const char *return_ranges
;
2011 if (__path_is_null())
2014 expr
= strip_expr(expr
);
2015 expr
= strip_expr_statement(expr
);
2017 if (is_impossible_path())
2020 if (expr
&& (expr
->type
== EXPR_COMPARE
||
2021 !get_implied_value(expr
, &sval
)) &&
2022 (is_condition(expr
) || is_boolean(expr
))) {
2023 call_return_state_hooks_compare(expr
);
2025 } else if (is_conditional(expr
)) {
2026 call_return_state_hooks_conditional(expr
);
2028 } else if (call_return_state_hooks_split_possible(expr
)) {
2030 } else if (call_return_state_hooks_split_null_non_null(expr
)) {
2032 } else if (call_return_state_hooks_split_success_fail(expr
)) {
2034 } else if (splitable_function_call(expr
)) {
2036 } else if (split_positive_from_negative(expr
)) {
2038 } else if (split_by_bool_param(expr
)) {
2039 } else if (split_by_null_nonnull_param(expr
)) {
2044 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
2045 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
2048 nr_states
= get_db_state_count();
2049 if (nr_states
>= 10000) {
2050 match_return_info(return_id
, (char *)return_ranges
, expr
);
2051 mark_all_params_untracked(return_id
, (char *)return_ranges
, expr
);
2054 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
2055 cb
->callback(return_id
, (char *)return_ranges
, expr
);
2056 } END_FOR_EACH_PTR(cb
);
2059 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
2061 struct returned_member_callback
*cb
;
2062 struct stree
*stree
;
2063 struct sm_state
*sm
;
2064 struct symbol
*type
;
2066 char member_name
[256];
2069 type
= get_type(expr
);
2070 if (!type
|| type
->type
!= SYM_PTR
)
2072 name
= expr_to_var(expr
);
2076 member_name
[sizeof(member_name
) - 1] = '\0';
2077 strcpy(member_name
, "$");
2080 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
2081 stree
= __get_cur_stree();
2082 FOR_EACH_MY_SM(cb
->owner
, stree
, sm
) {
2083 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
2084 strcpy(member_name
, "*$");
2085 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
2088 if (strncmp(sm
->name
, name
, len
) != 0)
2090 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
2092 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
2093 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
2094 } END_FOR_EACH_SM(sm
);
2095 } END_FOR_EACH_PTR(cb
);
2100 static void reset_memdb(struct symbol
*sym
)
2102 mem_sql(NULL
, NULL
, "delete from caller_info;");
2103 mem_sql(NULL
, NULL
, "delete from return_states;");
2104 mem_sql(NULL
, NULL
, "delete from call_implies;");
2105 mem_sql(NULL
, NULL
, "delete from return_implies;");
2108 static void match_end_func_info(struct symbol
*sym
)
2110 if (__path_is_null())
2112 call_return_state_hooks(NULL
);
2115 static void match_after_func(struct symbol
*sym
)
2121 static void init_memdb(void)
2125 const char *schema_files
[] = {
2127 "db/caller_info.schema",
2128 "db/common_caller_info.schema",
2129 "db/return_states.schema",
2130 "db/function_type_size.schema",
2131 "db/type_size.schema",
2132 "db/function_type_info.schema",
2133 "db/type_info.schema",
2134 "db/call_implies.schema",
2135 "db/return_implies.schema",
2136 "db/function_ptr.schema",
2137 "db/local_values.schema",
2138 "db/function_type_value.schema",
2139 "db/type_value.schema",
2140 "db/function_type.schema",
2141 "db/data_info.schema",
2142 "db/parameter_name.schema",
2143 "db/constraints.schema",
2144 "db/constraints_required.schema",
2145 "db/fn_ptr_data_link.schema",
2146 "db/fn_data_link.schema",
2147 "db/mtag_about.schema",
2148 "db/mtag_map.schema",
2149 "db/mtag_data.schema",
2150 "db/mtag_alias.schema",
2152 static char buf
[4096];
2157 rc
= sqlite3_open(":memory:", &mem_db
);
2158 if (rc
!= SQLITE_OK
) {
2159 sm_ierror("starting In-Memory database.");
2163 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
2164 fd
= open_schema_file(schema_files
[i
]);
2167 ret
= read(fd
, buf
, sizeof(buf
));
2169 sm_ierror("failed to read: %s", schema_files
[i
]);
2173 if (ret
== sizeof(buf
)) {
2174 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2175 schema_files
[i
], sizeof(buf
));
2179 rc
= sqlite3_exec(mem_db
, buf
, NULL
, NULL
, &err
);
2180 if (rc
!= SQLITE_OK
) {
2181 sm_ierror("SQL error #2: %s", err
);
2182 sm_ierror("%s", buf
);
2187 static void init_cachedb(void)
2191 const char *schema_files
[] = {
2192 "db/call_implies.schema",
2193 "db/return_implies.schema",
2194 "db/type_info.schema",
2195 "db/mtag_data.schema",
2196 "db/sink_info.schema",
2198 static char buf
[4096];
2203 rc
= sqlite3_open(":memory:", &cache_db
);
2204 if (rc
!= SQLITE_OK
) {
2205 sm_ierror("starting In-Memory database.");
2209 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
2210 fd
= open_schema_file(schema_files
[i
]);
2213 ret
= read(fd
, buf
, sizeof(buf
));
2215 sm_ierror("failed to read: %s", schema_files
[i
]);
2219 if (ret
== sizeof(buf
)) {
2220 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2221 schema_files
[i
], sizeof(buf
));
2225 rc
= sqlite3_exec(cache_db
, buf
, NULL
, NULL
, &err
);
2226 if (rc
!= SQLITE_OK
) {
2227 sm_ierror("SQL error #2: %s", err
);
2228 sm_ierror("%s", buf
);
2233 static int save_cache_data(void *_table
, int argc
, char **argv
, char **azColName
)
2235 static char buf
[4096];
2238 char *table
= _table
;
2242 p
+= snprintf(p
, 4096 - (p
- buf
), "insert or ignore into %s values (", table
);
2243 for (i
= 0; i
< argc
; i
++) {
2245 p
+= snprintf(p
, 4096 - (p
- buf
), ", ");
2246 sqlite3_snprintf(sizeof(tmp
), tmp
, "%q", escape_newlines(argv
[i
]));
2247 p
+= snprintf(p
, 4096 - (p
- buf
), "'%s'", tmp
);
2250 p
+= snprintf(p
, 4096 - (p
- buf
), ");");
2254 sm_msg("SQL: %s", buf
);
2258 static void dump_cache(struct symbol_list
*sym_list
)
2262 cache_sql(&save_cache_data
, (char *)"type_info", "select * from type_info;");
2263 cache_sql(&save_cache_data
, (char *)"return_implies", "select * from return_implies;");
2264 cache_sql(&save_cache_data
, (char *)"call_implies", "select * from call_implies;");
2265 cache_sql(&save_cache_data
, (char *)"mtag_data", "select * from mtag_data;");
2266 cache_sql(&save_cache_data
, (char *)"sink_info", "select * from sink_info;");
2269 void open_smatch_db(char *db_file
)
2276 use_states
= malloc(num_checks
+ 1);
2277 memset(use_states
, 0xff, num_checks
+ 1);
2282 rc
= sqlite3_open_v2(db_file
, &smatch_db
, SQLITE_OPEN_READONLY
, NULL
);
2283 if (rc
!= SQLITE_OK
) {
2288 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES
);
2292 static void register_common_funcs(void)
2294 struct token
*token
;
2298 if (option_project
== PROJ_NONE
)
2299 strcpy(filename
, "common_functions");
2301 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
2303 token
= get_tokens_file(filename
);
2306 if (token_type(token
) != TOKEN_STREAMBEGIN
)
2308 token
= token
->next
;
2309 while (token_type(token
) != TOKEN_STREAMEND
) {
2310 if (token_type(token
) != TOKEN_IDENT
)
2312 func
= alloc_string(show_ident(token
->ident
));
2313 add_ptr_list(&common_funcs
, func
);
2314 token
= token
->next
;
2316 clear_token_alloc();
2319 static char *get_next_string(char **str
)
2321 static char string
[256];
2330 while (*p
!= '\0' && *p
!= ' ' && *p
!= '\n')
2335 memcpy(string
, start
, 255);
2337 sm_ierror("return_fix: '%s' too long", string
);
2341 memcpy(string
, start
, len
);
2349 static void register_return_replacements(void)
2351 char *func
, *orig
, *new;
2357 snprintf(filename
, 256, "db/%s.return_fixes", option_project_str
);
2358 fd
= open_schema_file(filename
);
2361 ret
= read(fd
, buf
, sizeof(buf
));
2365 if (ret
== sizeof(buf
)) {
2366 sm_ierror("file too large: %s (limit %zd bytes)",
2367 filename
, sizeof(buf
));
2374 get_next_string(&p
);
2377 if (replace_count
== 0 || replace_count
% 3 != 0) {
2381 replace_table
= malloc(replace_count
* sizeof(char *));
2386 func
= alloc_string(get_next_string(&p
));
2387 orig
= alloc_string(get_next_string(&p
));
2388 new = alloc_string(get_next_string(&p
));
2390 replace_table
[i
++] = func
;
2391 replace_table
[i
++] = orig
;
2392 replace_table
[i
++] = new;
2396 void register_definition_db_callbacks(int id
)
2398 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2399 add_hook(&global_variable
, BASE_HOOK
);
2400 add_hook(&global_variable
, DECLARATION_HOOK
);
2401 add_split_return_callback(match_return_info
);
2402 add_split_return_callback(print_returned_struct_members
);
2403 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
2404 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
2405 add_hook(&match_after_func
, AFTER_FUNC_HOOK
);
2407 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
2408 add_hook(&match_call_implies
, FUNC_DEF_HOOK
);
2409 add_hook(&match_return_implies
, CALL_HOOK_AFTER_INLINE
);
2411 register_common_funcs();
2412 register_return_replacements();
2414 add_hook(&dump_cache
, END_FILE_HOOK
);
2417 void register_db_call_marker(int id
)
2419 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
2422 char *return_state_to_var_sym(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
)
2424 struct expression
*arg
;
2426 char member_name
[256];
2431 const char *star
= "";
2433 if (expr
->type
!= EXPR_ASSIGNMENT
)
2435 name
= expr_to_var_sym(expr
->left
, sym
);
2438 if (key
[0] == '*') {
2442 if (strncmp(key
, "$", 1) != 0)
2444 snprintf(member_name
, sizeof(member_name
), "%s%s%s", star
, name
, key
+ 1);
2446 return alloc_string(member_name
);
2449 while (expr
->type
== EXPR_ASSIGNMENT
)
2450 expr
= strip_expr(expr
->right
);
2451 if (expr
->type
!= EXPR_CALL
)
2454 arg
= get_argument_from_call_expr(expr
->args
, param
);
2458 return get_variable_from_key(arg
, key
, sym
);
2461 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
)
2469 arg
= strip_expr(arg
);
2471 if (strcmp(key
, "$") == 0)
2472 return expr_to_var_sym(arg
, sym
);
2474 if (strcmp(key
, "*$") == 0) {
2475 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
2476 arg
= strip_expr(arg
->unop
);
2477 return expr_to_var_sym(arg
, sym
);
2479 tmp
= expr_to_var_sym(arg
, sym
);
2482 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
2484 return alloc_string(buf
);
2488 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
2489 arg
= strip_expr(arg
->unop
);
2490 tmp
= expr_to_var_sym(arg
, sym
);
2493 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 3);
2494 return alloc_string(buf
);
2497 tmp
= expr_to_var_sym(arg
, sym
);
2500 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 1);
2502 return alloc_string(buf
);
2505 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
)
2509 if (strcmp("$", key
) == 0)
2510 return expr_to_chunk_sym_vsl(arg
, sym
, vsl
);
2511 return get_variable_from_key(arg
, key
, sym
);
2514 const char *state_name_to_param_name(const char *state_name
, const char *param_name
)
2517 static char buf
[256];
2519 name_len
= strlen(param_name
);
2521 if (strcmp(state_name
, param_name
) == 0) {
2523 } else if (state_name
[name_len
] == '-' && /* check for '-' from "->" */
2524 strncmp(state_name
, param_name
, name_len
) == 0) {
2525 snprintf(buf
, sizeof(buf
), "$%s", state_name
+ name_len
);
2527 } else if (state_name
[0] == '*' && strcmp(state_name
+ 1, param_name
) == 0) {
2533 const char *get_param_name_var_sym(const char *name
, struct symbol
*sym
)
2535 if (!sym
|| !sym
->ident
)
2538 return state_name_to_param_name(name
, sym
->ident
->name
);
2541 const char *get_mtag_name_var_sym(const char *state_name
, struct symbol
*sym
)
2543 struct symbol
*type
;
2544 const char *sym_name
;
2546 static char buf
[256];
2549 * mtag_name is different from param_name because mtags can be a struct
2550 * instead of a struct pointer. But we want to treat it like a pointer
2551 * because really an mtag is a pointer. Or in other words, if you pass
2552 * a struct foo then you want to talk about foo.bar but with an mtag
2553 * you want to refer to it as foo->bar.
2557 if (!sym
|| !sym
->ident
)
2560 type
= get_real_base_type(sym
);
2561 if (type
&& type
->type
== SYM_BASETYPE
)
2564 sym_name
= sym
->ident
->name
;
2565 name_len
= strlen(sym_name
);
2567 if (state_name
[name_len
] == '.' && /* check for '-' from "->" */
2568 strncmp(state_name
, sym_name
, name_len
) == 0) {
2569 snprintf(buf
, sizeof(buf
), "$->%s", state_name
+ name_len
+ 1);
2573 return state_name_to_param_name(state_name
, sym_name
);
2576 const char *get_mtag_name_expr(struct expression
*expr
)
2580 const char *ret
= NULL
;
2582 name
= expr_to_var_sym(expr
, &sym
);
2586 ret
= get_mtag_name_var_sym(name
, sym
);
2592 const char *get_param_name(struct sm_state
*sm
)
2594 return get_param_name_var_sym(sm
->name
, sm
->sym
);
2597 char *get_data_info_name(struct expression
*expr
)
2604 expr
= strip_expr(expr
);
2605 name
= get_member_name(expr
);
2608 name
= expr_to_var_sym(expr
, &sym
);
2611 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
2613 if (sym
->ctype
.modifiers
& MOD_STATIC
)
2614 snprintf(buf
, sizeof(buf
), "static %s", name
);
2616 snprintf(buf
, sizeof(buf
), "global %s", name
);
2617 ret
= alloc_sname(buf
);