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 char *escape_newlines(char *str
)
80 for (i
= 0; i
< sizeof(buf
); i
++) {
95 if (buf
[i
] == sizeof(buf
))
97 return alloc_sname(buf
);
100 void sql_exec(struct sqlite3
*db
, int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
)
108 rc
= sqlite3_exec(db
, sql
, callback
, data
, &err
);
109 if (rc
!= SQLITE_OK
&& !parse_error
) {
110 fprintf(stderr
, "SQL error #2: %s\n", err
);
111 fprintf(stderr
, "SQL: '%s'\n", sql
);
116 static int print_sql_output(void *unused
, int argc
, char **argv
, char **azColName
)
120 for (i
= 0; i
< argc
; i
++) {
123 sm_printf("%s", argv
[i
]);
129 void debug_sql(struct sqlite3
*db
, const char *sql
)
134 sql_exec(db
, print_sql_output
, NULL
, 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 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, int offset
, struct stree
*stree
,
754 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
760 char printed_name
[256];
763 expr
= strip_expr(expr
);
766 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
767 expr
= strip_expr(expr
->unop
);
771 name
= expr_to_var_sym(expr
, &sym
);
776 FOR_EACH_SM(stree
, sm
) {
779 if (strcmp(name
, sm
->name
) == 0) {
781 snprintf(printed_name
, sizeof(printed_name
), "*$%s", show_offset(offset
));
782 else /* these are already handled. fixme: handle them here */
784 } else if (sm
->name
[0] == '*' && strcmp(name
, sm
->name
+ 1) == 0) {
785 snprintf(printed_name
, sizeof(printed_name
), "*$%s", show_offset(offset
));
786 } else if (strncmp(name
, sm
->name
, len
) == 0) {
787 if (isalnum(sm
->name
[len
]))
790 snprintf(printed_name
, sizeof(printed_name
), "$%s->%s", show_offset(offset
), sm
->name
+ len
+ 1);
792 snprintf(printed_name
, sizeof(printed_name
), "$%s%s", show_offset(offset
), sm
->name
+ len
);
796 callback(call
, param
, printed_name
, sm
);
797 } END_FOR_EACH_SM(sm
);
802 static int param_used_callback(void *_container
, int argc
, char **argv
, char **azColName
)
804 char **container
= _container
;
805 static char buf
[256];
807 snprintf(buf
, sizeof(buf
), "%s", argv
[0]);
812 static void print_container_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct stree
*stree
,
813 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
815 struct expression
*tmp
;
816 char *container
= NULL
;
821 if (!call
->fn
|| call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
825 * We can't use the in-mem DB because we have to parse the function
826 * first, then we know if it takes a container, then we know to pass it
827 * the container data.
830 run_sql(¶m_used_callback
, &container
,
831 "select key from return_implies where %s and type = %d and key like '%%$(%%' and parameter = %d limit 1;",
832 get_static_filter(call
->fn
->symbol
), CONTAINER
, param
);
836 p
= strchr(container
, '-');
845 tmp
= get_assigned_expr(expr
);
849 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '&')
851 expr
= strip_expr(expr
->unop
);
852 holder_offset
= get_member_offset_from_deref(expr
);
853 if (-holder_offset
!= offset
)
856 expr
= strip_expr(expr
->deref
);
857 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
858 expr
= strip_expr(expr
->unop
);
860 print_struct_members(call
, expr
, param
, holder_offset
, stree
, callback
);
863 static void match_call_info(struct expression
*call
)
865 struct member_info_callback
*cb
;
866 struct expression
*arg
;
871 name
= get_fnptr_name(call
->fn
);
875 FOR_EACH_PTR(member_callbacks
, cb
) {
876 stree
= get_all_states_stree(cb
->owner
);
878 FOR_EACH_PTR(call
->args
, arg
) {
879 print_struct_members(call
, arg
, i
, -1, stree
, cb
->callback
);
880 print_container_struct_members(call
, arg
, i
, stree
, cb
->callback
);
882 } END_FOR_EACH_PTR(arg
);
884 } END_FOR_EACH_PTR(cb
);
889 static int get_param(int param
, char **name
, struct symbol
**sym
)
895 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
897 * this is a temporary hack to work around a bug (I think in sparse?)
898 * 2.6.37-rc1:fs/reiserfs/journal.o
899 * If there is a function definition without parameter name found
900 * after a function implementation then it causes a crash.
904 if (arg
->ident
->name
< (char *)100)
907 *name
= arg
->ident
->name
;
912 } END_FOR_EACH_PTR(arg
);
917 static int function_signature_matches(const char *sig
)
921 my_sig
= function_signature();
923 return 1; /* default to matching */
924 if (strcmp(my_sig
, sig
) == 0)
929 static int caller_info_callback(void *_data
, int argc
, char **argv
, char **azColName
)
931 struct select_caller_info_data
*data
= _data
;
938 struct symbol
*sym
= NULL
;
939 struct def_callback
*def_callback
;
941 struct timeval cur_time
;
948 gettimeofday(&cur_time
, NULL
);
949 if (cur_time
.tv_sec
- data
->start_time
.tv_sec
> 10)
952 func_id
= atoi(argv
[0]);
954 type
= strtol(argv
[1], NULL
, 10);
955 param
= strtol(argv
[2], NULL
, 10);
961 if (data
->prev_func_id
== -1)
962 data
->prev_func_id
= func_id
;
963 if (func_id
!= data
->prev_func_id
) {
964 stree
= __pop_fake_cur_stree();
966 merge_stree(&data
->final_states
, stree
);
968 __push_fake_cur_stree();
970 data
->prev_func_id
= func_id
;
976 if (type
== INTERNAL
&&
977 !function_signature_matches(value
)) {
982 if (param
>= 0 && !get_param(param
, &name
, &sym
))
985 FOR_EACH_PTR(select_caller_info_callbacks
, def_callback
) {
986 if (def_callback
->hook_type
== type
)
987 def_callback
->callback(name
, sym
, key
, value
);
988 } END_FOR_EACH_PTR(def_callback
);
993 static struct string_list
*ptr_names_done
;
994 static struct string_list
*ptr_names
;
996 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
998 insert_string(&ptr_names
, alloc_string(argv
[0]));
1002 static char *get_next_ptr_name(void)
1006 FOR_EACH_PTR(ptr_names
, ptr
) {
1007 if (list_has_string(ptr_names_done
, ptr
))
1009 insert_string(&ptr_names_done
, ptr
);
1011 } END_FOR_EACH_PTR(ptr
);
1015 static void get_ptr_names(const char *file
, const char *name
)
1017 char sql_filter
[1024];
1021 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
1024 snprintf(sql_filter
, 1024, "function = '%s';", name
);
1027 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
1029 run_sql(get_ptr_name
, NULL
,
1030 "select distinct ptr from function_ptr where %s",
1033 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
1034 if (before
== after
)
1037 while ((name
= get_next_ptr_name()))
1038 get_ptr_names(NULL
, name
);
1041 static void match_data_from_db(struct symbol
*sym
)
1043 struct select_caller_info_data data
= { .prev_func_id
= -1 };
1044 struct sm_state
*sm
;
1045 struct stree
*stree
;
1046 struct timeval end_time
;
1048 if (!sym
|| !sym
->ident
)
1051 gettimeofday(&data
.start_time
, NULL
);
1053 __push_fake_cur_stree();
1059 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1060 get_ptr_names(get_base_file(), sym
->ident
->name
);
1062 get_ptr_names(NULL
, sym
->ident
->name
);
1064 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
1065 __free_ptr_list((struct ptr_list
**)&ptr_names
);
1066 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
1067 stree
= __pop_fake_cur_stree();
1072 sql_select_caller_info(&data
,
1073 "call_id, type, parameter, key, value",
1077 stree
= __pop_fake_cur_stree();
1079 merge_stree(&data
.final_states
, stree
);
1081 __push_fake_cur_stree();
1083 data
.prev_func_id
= -1;
1086 FOR_EACH_PTR(ptr_names
, ptr
) {
1087 run_sql(caller_info_callback
, &data
,
1088 "select call_id, type, parameter, key, value"
1089 " from common_caller_info where function = '%s' order by call_id",
1091 } END_FOR_EACH_PTR(ptr
);
1094 FOR_EACH_PTR(ptr_names
, ptr
) {
1096 } END_FOR_EACH_PTR(ptr
);
1097 goto free_ptr_names
;
1100 FOR_EACH_PTR(ptr_names
, ptr
) {
1101 run_sql(caller_info_callback
, &data
,
1102 "select call_id, type, parameter, key, value"
1103 " from caller_info where function = '%s' order by call_id",
1106 } END_FOR_EACH_PTR(ptr
);
1109 __free_ptr_list((struct ptr_list
**)&ptr_names
);
1110 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
1112 sql_select_caller_info(&data
,
1113 "call_id, type, parameter, key, value",
1117 stree
= __pop_fake_cur_stree();
1119 merge_stree(&data
.final_states
, stree
);
1122 gettimeofday(&end_time
, NULL
);
1123 if (end_time
.tv_sec
- data
.start_time
.tv_sec
<= 10) {
1124 FOR_EACH_SM(data
.final_states
, sm
) {
1126 } END_FOR_EACH_SM(sm
);
1129 free_stree(&data
.final_states
);
1132 static int return_implies_callbacks(void *_info
, int argc
, char **argv
, char **azColName
)
1134 struct implies_info
*info
= _info
;
1135 struct db_implies_callback
*cb
;
1136 struct expression
*arg
= NULL
;
1143 type
= atoi(argv
[1]);
1144 param
= atoi(argv
[2]);
1146 FOR_EACH_PTR(info
->cb_list
, cb
) {
1147 if (cb
->type
!= type
)
1150 arg
= get_argument_from_call_expr(info
->expr
->args
, param
);
1154 cb
->callback(info
->expr
, arg
, argv
[3], argv
[4]);
1155 } END_FOR_EACH_PTR(cb
);
1160 static int call_implies_callbacks(void *_info
, int argc
, char **argv
, char **azColName
)
1162 struct implies_info
*info
= _info
;
1163 struct db_implies_callback
*cb
;
1164 struct expression
*arg
;
1173 type
= atoi(argv
[1]);
1174 param
= atoi(argv
[2]);
1176 if (!get_param(param
, &name
, &sym
))
1178 arg
= symbol_expression(sym
);
1182 FOR_EACH_PTR(info
->cb_list
, cb
) {
1183 if (cb
->type
!= type
)
1185 cb
->callback(info
->expr
, arg
, argv
[3], argv
[4]);
1186 } END_FOR_EACH_PTR(cb
);
1191 static void match_return_implies(struct expression
*expr
)
1193 struct implies_info info
= {
1194 .type
= RETURN_IMPLIES
,
1195 .cb_list
= return_implies_cb_list
,
1198 if (expr
->fn
->type
!= EXPR_SYMBOL
||
1202 info
.sym
= expr
->fn
->symbol
;
1203 sql_select_implies("function, type, parameter, key, value", &info
,
1204 return_implies_callbacks
);
1207 static void match_call_implies(struct symbol
*sym
)
1209 struct implies_info info
= {
1210 .type
= CALL_IMPLIES
,
1211 .cb_list
= call_implies_cb_list
,
1214 if (!sym
|| !sym
->ident
)
1218 sql_select_implies("function, type, parameter, key, value", &info
,
1219 call_implies_callbacks
);
1222 static void print_initializer_list(struct expression_list
*expr_list
,
1223 struct symbol
*struct_type
)
1225 struct expression
*expr
;
1226 struct symbol
*base_type
;
1227 char struct_name
[256];
1229 FOR_EACH_PTR(expr_list
, expr
) {
1230 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
1231 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
1234 if (expr
->type
!= EXPR_IDENTIFIER
)
1236 if (!expr
->expr_ident
)
1238 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
1240 base_type
= get_type(expr
->ident_expression
);
1241 if (!base_type
|| base_type
->type
!= SYM_FN
)
1243 snprintf(struct_name
, sizeof(struct_name
), "(struct %s)->%s",
1244 struct_type
->ident
->name
, expr
->expr_ident
->name
);
1245 sql_insert_function_ptr(expr
->ident_expression
->symbol_name
->name
,
1247 } END_FOR_EACH_PTR(expr
);
1250 static void global_variable(struct symbol
*sym
)
1252 struct symbol
*struct_type
;
1256 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
1258 struct_type
= get_base_type(sym
);
1261 if (struct_type
->type
== SYM_ARRAY
) {
1262 struct_type
= get_base_type(struct_type
);
1266 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
1268 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
1271 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
1273 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", function_signature());
1276 static void call_return_state_hooks_conditional(struct expression
*expr
)
1278 struct returned_state_callback
*cb
;
1279 struct range_list
*rl
;
1280 char *return_ranges
;
1281 int final_pass_orig
= final_pass
;
1283 __push_fake_cur_stree();
1286 __split_whole_condition(expr
->conditional
);
1287 final_pass
= final_pass_orig
;
1289 if (get_implied_rl(expr
->cond_true
, &rl
))
1290 rl
= cast_rl(cur_func_return_type(), rl
);
1292 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_true
)));
1293 return_ranges
= show_rl(rl
);
1294 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(rl
));
1297 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1298 cb
->callback(return_id
, return_ranges
, expr
->cond_true
);
1299 } END_FOR_EACH_PTR(cb
);
1301 __push_true_states();
1302 __use_false_states();
1304 if (get_implied_rl(expr
->cond_false
, &rl
))
1305 rl
= cast_rl(cur_func_return_type(), rl
);
1307 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_false
)));
1308 return_ranges
= show_rl(rl
);
1309 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(rl
));
1312 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1313 cb
->callback(return_id
, return_ranges
, expr
->cond_false
);
1314 } END_FOR_EACH_PTR(cb
);
1316 __merge_true_states();
1317 __free_fake_cur_stree();
1320 static void call_return_state_hooks_compare(struct expression
*expr
)
1322 struct returned_state_callback
*cb
;
1323 char *return_ranges
;
1324 int final_pass_orig
= final_pass
;
1325 sval_t sval
= { .type
= &int_ctype
};
1328 if (!get_implied_value(expr
, &ret
))
1331 __push_fake_cur_stree();
1334 __split_whole_condition(expr
);
1335 final_pass
= final_pass_orig
;
1337 if (ret
.value
!= 0) {
1338 return_ranges
= alloc_sname("1");
1340 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1343 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1344 cb
->callback(return_id
, return_ranges
, expr
);
1345 } END_FOR_EACH_PTR(cb
);
1348 __push_true_states();
1349 __use_false_states();
1351 if (ret
.value
!= 1) {
1352 return_ranges
= alloc_sname("0");
1354 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1357 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1358 cb
->callback(return_id
, return_ranges
, expr
);
1359 } END_FOR_EACH_PTR(cb
);
1362 __merge_true_states();
1363 __free_fake_cur_stree();
1366 static int ptr_in_list(struct sm_state
*sm
, struct state_list
*slist
)
1368 struct sm_state
*tmp
;
1370 FOR_EACH_PTR(slist
, tmp
) {
1371 if (strcmp(tmp
->state
->name
, sm
->state
->name
) == 0)
1373 } END_FOR_EACH_PTR(tmp
);
1378 static char *get_return_compare_str(struct expression
*expr
)
1386 compare_str
= expr_lte_to_param(expr
, -1);
1389 param
= get_param_num(expr
);
1393 var
= expr_to_var(expr
);
1396 snprintf(buf
, sizeof(buf
), "%s orig", var
);
1397 comparison
= get_comparison_strings(var
, buf
);
1403 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(comparison
), param
);
1404 return alloc_sname(buf
);
1407 static int split_possible_helper(struct sm_state
*sm
, struct expression
*expr
)
1409 struct returned_state_callback
*cb
;
1410 struct range_list
*rl
;
1411 char *return_ranges
;
1412 struct sm_state
*tmp
;
1414 int nr_possible
, nr_states
;
1415 char *compare_str
= NULL
;
1417 struct state_list
*already_handled
= NULL
;
1419 if (!sm
|| !sm
->merged
)
1422 if (too_many_possible(sm
))
1425 /* bail if it gets too complicated */
1426 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
1427 nr_states
= get_db_state_count();
1428 if (nr_states
* nr_possible
>= 2000)
1431 FOR_EACH_PTR(sm
->possible
, tmp
) {
1434 if (ptr_in_list(tmp
, already_handled
))
1436 add_ptr_list(&already_handled
, tmp
);
1439 __push_fake_cur_stree();
1441 overwrite_states_using_pool(sm
, tmp
);
1443 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
1444 return_ranges
= show_rl(rl
);
1445 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(clone_rl(rl
)));
1446 compare_str
= get_return_compare_str(expr
);
1448 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1449 return_ranges
= alloc_sname(buf
);
1453 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1454 cb
->callback(return_id
, return_ranges
, expr
);
1455 } END_FOR_EACH_PTR(cb
);
1457 __free_fake_cur_stree();
1458 } END_FOR_EACH_PTR(tmp
);
1460 free_slist(&already_handled
);
1465 static int call_return_state_hooks_split_possible(struct expression
*expr
)
1467 struct sm_state
*sm
;
1469 if (!expr
|| expr_equal_to_param(expr
, -1))
1472 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1473 return split_possible_helper(sm
, expr
);
1476 static const char *get_return_ranges_str(struct expression
*expr
, struct range_list
**rl_p
)
1478 struct range_list
*rl
;
1479 char *return_ranges
;
1488 return alloc_sname("");
1490 if (get_implied_value(expr
, &sval
)) {
1491 sval
= sval_cast(cur_func_return_type(), sval
);
1492 *rl_p
= alloc_rl(sval
, sval
);
1493 return sval_to_str(sval
);
1496 compare_str
= expr_equal_to_param(expr
, -1);
1497 math_str
= get_value_in_terms_of_parameter_math(expr
);
1499 if (get_implied_rl(expr
, &rl
)) {
1500 rl
= cast_rl(cur_func_return_type(), rl
);
1501 return_ranges
= show_rl(rl
);
1502 } else if (get_imaginary_absolute(expr
, &rl
)){
1503 rl
= cast_rl(cur_func_return_type(), rl
);
1504 return alloc_sname(show_rl(rl
));
1506 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
)));
1507 return_ranges
= show_rl(rl
);
1512 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1513 return alloc_sname(buf
);
1516 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1517 return alloc_sname(buf
);
1519 compare_str
= get_return_compare_str(expr
);
1521 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1522 return alloc_sname(buf
);
1525 return return_ranges
;
1528 static bool has_possible_negative(struct sm_state
*sm
)
1530 struct sm_state
*tmp
;
1532 FOR_EACH_PTR(sm
->possible
, tmp
) {
1533 if (!estate_rl(tmp
->state
))
1535 if (sval_is_negative(estate_min(tmp
->state
)) &&
1536 sval_is_negative(estate_max(tmp
->state
)))
1538 } END_FOR_EACH_PTR(tmp
);
1543 static bool has_possible_zero_null(struct sm_state
*sm
)
1545 struct sm_state
*tmp
;
1548 FOR_EACH_PTR(sm
->possible
, tmp
) {
1549 if (!estate_get_single_value(tmp
->state
, &sval
))
1551 if (sval
.value
== 0)
1553 } END_FOR_EACH_PTR(tmp
);
1558 static int split_positive_from_negative(struct expression
*expr
)
1560 struct sm_state
*sm
;
1561 struct returned_state_callback
*cb
;
1562 struct range_list
*rl
;
1563 const char *return_ranges
;
1564 struct range_list
*ret_rl
;
1567 /* We're going to print the states 3 times */
1568 if (get_db_state_count() > 10000 / 3)
1571 if (!get_implied_rl(expr
, &rl
) || !rl
)
1573 if (is_whole_rl(rl
) || is_whole_rl_non_zero(rl
))
1575 /* Forget about INT_MAX and larger */
1576 if (rl_max(rl
).value
<= 0)
1578 if (!sval_is_negative(rl_min(rl
)))
1581 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1584 if (!has_possible_negative(sm
))
1587 if (!assume(compare_expression(expr
, '>', zero_expr())))
1591 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1592 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1593 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1594 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1595 } END_FOR_EACH_PTR(cb
);
1599 if (rl_has_sval(rl
, sval_type_val(rl_type(rl
), 0))) {
1600 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
1603 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1604 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1605 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1606 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1607 } END_FOR_EACH_PTR(cb
);
1613 undo
= assume(compare_expression(expr
, '<', zero_expr()));
1616 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1617 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1618 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1619 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1620 } END_FOR_EACH_PTR(cb
);
1628 static int call_return_state_hooks_split_null_non_null(struct expression
*expr
)
1630 struct returned_state_callback
*cb
;
1631 struct range_list
*rl
;
1632 struct range_list
*nonnull_rl
;
1634 struct range_list
*null_rl
= NULL
;
1635 char *return_ranges
;
1636 struct sm_state
*sm
;
1637 struct smatch_state
*state
;
1639 int final_pass_orig
= final_pass
;
1641 if (!expr
|| expr_equal_to_param(expr
, -1))
1643 if (expr
->type
== EXPR_CALL
)
1645 if (!is_pointer(expr
))
1648 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1651 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
1654 if (!estate_rl(state
))
1656 if (estate_min(state
).value
== 0 && estate_max(state
).value
== 0)
1658 if (!has_possible_zero_null(sm
))
1661 nr_states
= get_db_state_count();
1662 if (option_info
&& nr_states
>= 1500)
1665 rl
= estate_rl(state
);
1667 __push_fake_cur_stree();
1670 __split_whole_condition(expr
);
1671 final_pass
= final_pass_orig
;
1673 nonnull_rl
= rl_filter(rl
, rl_zero());
1674 return_ranges
= show_rl(nonnull_rl
);
1675 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonnull_rl
));
1678 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1679 cb
->callback(return_id
, return_ranges
, expr
);
1680 } END_FOR_EACH_PTR(cb
);
1682 __push_true_states();
1683 __use_false_states();
1685 return_ranges
= alloc_sname("0");
1686 null_sval
= sval_type_val(rl_type(rl
), 0);
1687 add_range(&null_rl
, null_sval
, null_sval
);
1688 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(null_rl
));
1690 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1691 cb
->callback(return_id
, return_ranges
, expr
);
1692 } END_FOR_EACH_PTR(cb
);
1694 __merge_true_states();
1695 __free_fake_cur_stree();
1700 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
1702 struct sm_state
*sm
;
1703 struct range_list
*rl
;
1704 struct range_list
*nonzero_rl
;
1706 struct range_list
*zero_rl
= NULL
;
1708 struct returned_state_callback
*cb
;
1709 char *return_ranges
;
1710 int final_pass_orig
= final_pass
;
1712 if (option_project
!= PROJ_KERNEL
)
1715 nr_states
= get_db_state_count();
1716 if (nr_states
> 1500)
1719 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1722 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
1725 rl
= estate_rl(sm
->state
);
1729 if (rl_min(rl
).value
< -4095 || rl_min(rl
).value
>= 0)
1731 if (rl_max(rl
).value
!= 0)
1733 if (!has_possible_zero_null(sm
))
1736 __push_fake_cur_stree();
1739 __split_whole_condition(expr
);
1740 final_pass
= final_pass_orig
;
1742 nonzero_rl
= rl_filter(rl
, rl_zero());
1743 nonzero_rl
= cast_rl(cur_func_return_type(), nonzero_rl
);
1744 return_ranges
= show_rl(nonzero_rl
);
1745 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonzero_rl
));
1748 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1749 cb
->callback(return_id
, return_ranges
, expr
);
1750 } END_FOR_EACH_PTR(cb
);
1752 __push_true_states();
1753 __use_false_states();
1755 return_ranges
= alloc_sname("0");
1756 zero_sval
= sval_type_val(rl_type(rl
), 0);
1757 add_range(&zero_rl
, zero_sval
, zero_sval
);
1758 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(zero_rl
));
1760 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1761 cb
->callback(return_id
, return_ranges
, expr
);
1762 } END_FOR_EACH_PTR(cb
);
1764 __merge_true_states();
1765 __free_fake_cur_stree();
1770 static int is_boolean(struct expression
*expr
)
1772 struct range_list
*rl
;
1774 if (!get_implied_rl(expr
, &rl
))
1776 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
1781 static int is_conditional(struct expression
*expr
)
1785 if (expr
->type
== EXPR_CONDITIONAL
|| expr
->type
== EXPR_SELECT
)
1790 static int splitable_function_call(struct expression
*expr
)
1792 struct sm_state
*sm
;
1795 if (!expr
|| expr
->type
!= EXPR_CALL
)
1797 snprintf(buf
, sizeof(buf
), "return %p", expr
);
1798 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
1799 return split_possible_helper(sm
, expr
);
1802 static struct sm_state
*find_bool_param(void)
1804 struct stree
*start_states
;
1806 struct sm_state
*sm
, *tmp
;
1809 start_states
= get_start_states();
1811 FOR_EACH_PTR_REVERSE(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
1814 sm
= get_sm_state_stree(start_states
, SMATCH_EXTRA
, arg
->ident
->name
, arg
);
1817 if (rl_min(estate_rl(sm
->state
)).value
!= 0 ||
1818 rl_max(estate_rl(sm
->state
)).value
!= 1)
1821 } END_FOR_EACH_PTR_REVERSE(arg
);
1827 * Check if it's splitable. If not, then splitting it up is likely not
1828 * useful for the callers.
1830 FOR_EACH_PTR(sm
->possible
, tmp
) {
1833 if (!estate_get_single_value(tmp
->state
, &sval
))
1835 } END_FOR_EACH_PTR(tmp
);
1840 static int split_on_bool_sm(struct sm_state
*sm
, struct expression
*expr
)
1842 struct returned_state_callback
*cb
;
1843 struct range_list
*ret_rl
;
1844 const char *return_ranges
;
1845 struct sm_state
*tmp
;
1847 int nr_possible
, nr_states
;
1848 char *compare_str
= NULL
;
1850 struct state_list
*already_handled
= NULL
;
1852 if (!sm
|| !sm
->merged
)
1855 if (too_many_possible(sm
))
1858 /* bail if it gets too complicated */
1859 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
1860 nr_states
= get_db_state_count();
1861 if (nr_states
* nr_possible
>= 2000)
1864 FOR_EACH_PTR(sm
->possible
, tmp
) {
1867 if (ptr_in_list(tmp
, already_handled
))
1869 add_ptr_list(&already_handled
, tmp
);
1872 __push_fake_cur_stree();
1874 overwrite_states_using_pool(sm
, tmp
);
1876 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1877 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1878 compare_str
= get_return_compare_str(expr
);
1880 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1881 return_ranges
= alloc_sname(buf
);
1885 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1886 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1887 } END_FOR_EACH_PTR(cb
);
1889 __free_fake_cur_stree();
1890 } END_FOR_EACH_PTR(tmp
);
1892 free_slist(&already_handled
);
1897 static int split_by_bool_param(struct expression
*expr
)
1899 struct sm_state
*start_sm
, *sm
;
1902 start_sm
= find_bool_param();
1905 sm
= get_sm_state(SMATCH_EXTRA
, start_sm
->name
, start_sm
->sym
);
1906 if (!sm
|| estate_get_single_value(sm
->state
, &sval
))
1908 return split_on_bool_sm(sm
, expr
);
1911 static int split_by_null_nonnull_param(struct expression
*expr
)
1914 struct sm_state
*sm
;
1916 .type
= &ulong_ctype
,
1919 /* function must only take one pointer */
1920 if (ptr_list_size((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
) != 1)
1922 arg
= first_ptr_list((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
);
1925 if (get_real_base_type(arg
)->type
!= SYM_PTR
)
1928 if (param_was_set_var_sym(arg
->ident
->name
, arg
))
1930 sm
= get_sm_state(SMATCH_EXTRA
, arg
->ident
->name
, arg
);
1934 if (!rl_has_sval(estate_rl(sm
->state
), zero
))
1937 return split_on_bool_sm(sm
, expr
);
1940 struct expression
*strip_expr_statement(struct expression
*expr
)
1942 struct expression
*orig
= expr
;
1943 struct statement
*stmt
, *last_stmt
;
1947 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '(')
1949 if (expr
->type
!= EXPR_STATEMENT
)
1951 stmt
= expr
->statement
;
1952 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
1955 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
1956 if (!last_stmt
|| last_stmt
->type
== STMT_LABEL
)
1957 last_stmt
= last_stmt
->label_statement
;
1958 if (!last_stmt
|| last_stmt
->type
!= STMT_EXPRESSION
)
1960 return strip_expr(last_stmt
->expression
);
1963 static void call_return_state_hooks(struct expression
*expr
)
1965 struct returned_state_callback
*cb
;
1966 struct range_list
*ret_rl
;
1967 const char *return_ranges
;
1971 if (__path_is_null())
1974 expr
= strip_expr(expr
);
1975 expr
= strip_expr_statement(expr
);
1977 if (is_impossible_path())
1980 if (expr
&& (expr
->type
== EXPR_COMPARE
||
1981 !get_implied_value(expr
, &sval
)) &&
1982 (is_condition(expr
) || is_boolean(expr
))) {
1983 call_return_state_hooks_compare(expr
);
1985 } else if (is_conditional(expr
)) {
1986 call_return_state_hooks_conditional(expr
);
1988 } else if (call_return_state_hooks_split_possible(expr
)) {
1990 } else if (call_return_state_hooks_split_null_non_null(expr
)) {
1992 } else if (call_return_state_hooks_split_success_fail(expr
)) {
1994 } else if (splitable_function_call(expr
)) {
1996 } else if (split_positive_from_negative(expr
)) {
1998 } else if (split_by_bool_param(expr
)) {
1999 } else if (split_by_null_nonnull_param(expr
)) {
2004 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
2005 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
2008 nr_states
= get_db_state_count();
2009 if (nr_states
>= 10000) {
2010 match_return_info(return_id
, (char *)return_ranges
, expr
);
2011 mark_all_params_untracked(return_id
, (char *)return_ranges
, expr
);
2014 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
2015 cb
->callback(return_id
, (char *)return_ranges
, expr
);
2016 } END_FOR_EACH_PTR(cb
);
2019 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
2021 struct returned_member_callback
*cb
;
2022 struct stree
*stree
;
2023 struct sm_state
*sm
;
2024 struct symbol
*type
;
2026 char member_name
[256];
2029 type
= get_type(expr
);
2030 if (!type
|| type
->type
!= SYM_PTR
)
2032 name
= expr_to_var(expr
);
2036 member_name
[sizeof(member_name
) - 1] = '\0';
2037 strcpy(member_name
, "$");
2040 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
2041 stree
= __get_cur_stree();
2042 FOR_EACH_MY_SM(cb
->owner
, stree
, sm
) {
2043 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
2044 strcpy(member_name
, "*$");
2045 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
2048 if (strncmp(sm
->name
, name
, len
) != 0)
2050 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
2052 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
2053 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
2054 } END_FOR_EACH_SM(sm
);
2055 } END_FOR_EACH_PTR(cb
);
2060 static void reset_memdb(struct symbol
*sym
)
2062 mem_sql(NULL
, NULL
, "delete from caller_info;");
2063 mem_sql(NULL
, NULL
, "delete from return_states;");
2064 mem_sql(NULL
, NULL
, "delete from call_implies;");
2065 mem_sql(NULL
, NULL
, "delete from return_implies;");
2068 static void match_end_func_info(struct symbol
*sym
)
2070 if (__path_is_null())
2072 call_return_state_hooks(NULL
);
2075 static void match_after_func(struct symbol
*sym
)
2081 static void init_memdb(void)
2085 const char *schema_files
[] = {
2087 "db/caller_info.schema",
2088 "db/common_caller_info.schema",
2089 "db/return_states.schema",
2090 "db/function_type_size.schema",
2091 "db/type_size.schema",
2092 "db/function_type_info.schema",
2093 "db/type_info.schema",
2094 "db/call_implies.schema",
2095 "db/return_implies.schema",
2096 "db/function_ptr.schema",
2097 "db/local_values.schema",
2098 "db/function_type_value.schema",
2099 "db/type_value.schema",
2100 "db/function_type.schema",
2101 "db/data_info.schema",
2102 "db/parameter_name.schema",
2103 "db/constraints.schema",
2104 "db/constraints_required.schema",
2105 "db/fn_ptr_data_link.schema",
2106 "db/fn_data_link.schema",
2107 "db/mtag_about.schema",
2108 "db/mtag_map.schema",
2109 "db/mtag_data.schema",
2110 "db/mtag_alias.schema",
2112 static char buf
[4096];
2117 rc
= sqlite3_open(":memory:", &mem_db
);
2118 if (rc
!= SQLITE_OK
) {
2119 printf("Error starting In-Memory database.");
2123 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
2124 fd
= open_schema_file(schema_files
[i
]);
2127 ret
= read(fd
, buf
, sizeof(buf
));
2129 printf("failed to read: %s\n", schema_files
[i
]);
2133 if (ret
== sizeof(buf
)) {
2134 printf("Schema file too large: %s (limit %zd bytes)",
2135 schema_files
[i
], sizeof(buf
));
2139 rc
= sqlite3_exec(mem_db
, buf
, NULL
, NULL
, &err
);
2140 if (rc
!= SQLITE_OK
) {
2141 fprintf(stderr
, "SQL error #2: %s\n", err
);
2142 fprintf(stderr
, "%s\n", buf
);
2147 static void init_cachedb(void)
2151 const char *schema_files
[] = {
2152 "db/call_implies.schema",
2153 "db/return_implies.schema",
2154 "db/type_info.schema",
2155 "db/mtag_data.schema",
2156 "db/sink_info.schema",
2158 static char buf
[4096];
2163 rc
= sqlite3_open(":memory:", &cache_db
);
2164 if (rc
!= SQLITE_OK
) {
2165 printf("Error starting In-Memory database.");
2169 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
2170 fd
= open_schema_file(schema_files
[i
]);
2173 ret
= read(fd
, buf
, sizeof(buf
));
2175 printf("failed to read: %s\n", schema_files
[i
]);
2179 if (ret
== sizeof(buf
)) {
2180 printf("Schema file too large: %s (limit %zd bytes)",
2181 schema_files
[i
], sizeof(buf
));
2185 rc
= sqlite3_exec(cache_db
, buf
, NULL
, NULL
, &err
);
2186 if (rc
!= SQLITE_OK
) {
2187 fprintf(stderr
, "SQL error #2: %s\n", err
);
2188 fprintf(stderr
, "%s\n", buf
);
2193 static int save_cache_data(void *_table
, int argc
, char **argv
, char **azColName
)
2195 static char buf
[4096];
2198 char *table
= _table
;
2202 p
+= snprintf(p
, 4096 - (p
- buf
), "insert or ignore into %s values (", table
);
2203 for (i
= 0; i
< argc
; i
++) {
2205 p
+= snprintf(p
, 4096 - (p
- buf
), ", ");
2206 sqlite3_snprintf(sizeof(tmp
), tmp
, "%q", argv
[i
]);
2207 p
+= snprintf(p
, 4096 - (p
- buf
), "'%s'", tmp
);
2210 p
+= snprintf(p
, 4096 - (p
- buf
), ");");
2214 sm_msg("SQL: %s", buf
);
2218 static void dump_cache(struct symbol_list
*sym_list
)
2222 cache_sql(&save_cache_data
, (char *)"type_info", "select * from type_info;");
2223 cache_sql(&save_cache_data
, (char *)"return_implies", "select * from return_implies;");
2224 cache_sql(&save_cache_data
, (char *)"call_implies", "select * from call_implies;");
2225 cache_sql(&save_cache_data
, (char *)"mtag_data", "select * from mtag_data;");
2226 cache_sql(&save_cache_data
, (char *)"sink_info", "select * from sink_info;");
2229 void open_smatch_db(char *db_file
)
2236 use_states
= malloc(num_checks
+ 1);
2237 memset(use_states
, 0xff, num_checks
+ 1);
2242 rc
= sqlite3_open_v2(db_file
, &smatch_db
, SQLITE_OPEN_READONLY
, NULL
);
2243 if (rc
!= SQLITE_OK
) {
2248 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES
);
2252 static void register_common_funcs(void)
2254 struct token
*token
;
2258 if (option_project
== PROJ_NONE
)
2259 strcpy(filename
, "common_functions");
2261 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
2263 token
= get_tokens_file(filename
);
2266 if (token_type(token
) != TOKEN_STREAMBEGIN
)
2268 token
= token
->next
;
2269 while (token_type(token
) != TOKEN_STREAMEND
) {
2270 if (token_type(token
) != TOKEN_IDENT
)
2272 func
= alloc_string(show_ident(token
->ident
));
2273 add_ptr_list(&common_funcs
, func
);
2274 token
= token
->next
;
2276 clear_token_alloc();
2279 static char *get_next_string(char **str
)
2281 static char string
[256];
2290 while (*p
!= '\0' && *p
!= ' ' && *p
!= '\n')
2295 memcpy(string
, start
, 255);
2297 printf("return_fix: '%s' too long", string
);
2301 memcpy(string
, start
, len
);
2309 static void register_return_replacements(void)
2311 char *func
, *orig
, *new;
2317 snprintf(filename
, 256, "db/%s.return_fixes", option_project_str
);
2318 fd
= open_schema_file(filename
);
2321 ret
= read(fd
, buf
, sizeof(buf
));
2325 if (ret
== sizeof(buf
)) {
2326 printf("file too large: %s (limit %zd bytes)",
2327 filename
, sizeof(buf
));
2334 get_next_string(&p
);
2337 if (replace_count
== 0 || replace_count
% 3 != 0) {
2341 replace_table
= malloc(replace_count
* sizeof(char *));
2346 func
= alloc_string(get_next_string(&p
));
2347 orig
= alloc_string(get_next_string(&p
));
2348 new = alloc_string(get_next_string(&p
));
2350 replace_table
[i
++] = func
;
2351 replace_table
[i
++] = orig
;
2352 replace_table
[i
++] = new;
2356 void register_definition_db_callbacks(int id
)
2358 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2359 add_hook(&global_variable
, BASE_HOOK
);
2360 add_hook(&global_variable
, DECLARATION_HOOK
);
2361 add_split_return_callback(match_return_info
);
2362 add_split_return_callback(print_returned_struct_members
);
2363 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
2364 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
2365 add_hook(&match_after_func
, AFTER_FUNC_HOOK
);
2367 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
2368 add_hook(&match_call_implies
, FUNC_DEF_HOOK
);
2369 add_hook(&match_return_implies
, CALL_HOOK_AFTER_INLINE
);
2371 register_common_funcs();
2372 register_return_replacements();
2374 add_hook(&dump_cache
, END_FILE_HOOK
);
2377 void register_db_call_marker(int id
)
2379 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
2382 char *return_state_to_var_sym(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
)
2384 struct expression
*arg
;
2386 char member_name
[256];
2391 const char *star
= "";
2393 if (expr
->type
!= EXPR_ASSIGNMENT
)
2395 name
= expr_to_var_sym(expr
->left
, sym
);
2398 if (key
[0] == '*') {
2402 if (strncmp(key
, "$", 1) != 0)
2404 snprintf(member_name
, sizeof(member_name
), "%s%s%s", star
, name
, key
+ 1);
2406 return alloc_string(member_name
);
2409 while (expr
->type
== EXPR_ASSIGNMENT
)
2410 expr
= strip_expr(expr
->right
);
2411 if (expr
->type
!= EXPR_CALL
)
2414 arg
= get_argument_from_call_expr(expr
->args
, param
);
2418 return get_variable_from_key(arg
, key
, sym
);
2421 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
)
2429 arg
= strip_expr(arg
);
2431 if (strcmp(key
, "$") == 0)
2432 return expr_to_var_sym(arg
, sym
);
2434 if (strcmp(key
, "*$") == 0) {
2435 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
2436 arg
= strip_expr(arg
->unop
);
2437 return expr_to_var_sym(arg
, sym
);
2439 tmp
= expr_to_var_sym(arg
, sym
);
2442 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
2444 return alloc_string(buf
);
2448 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
2449 arg
= strip_expr(arg
->unop
);
2450 tmp
= expr_to_var_sym(arg
, sym
);
2453 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 3);
2454 return alloc_string(buf
);
2457 tmp
= expr_to_var_sym(arg
, sym
);
2460 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 1);
2462 return alloc_string(buf
);
2465 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
)
2469 if (strcmp("$", key
) == 0)
2470 return expr_to_chunk_sym_vsl(arg
, sym
, vsl
);
2471 return get_variable_from_key(arg
, key
, sym
);
2474 const char *state_name_to_param_name(const char *state_name
, const char *param_name
)
2477 static char buf
[256];
2479 name_len
= strlen(param_name
);
2481 if (strcmp(state_name
, param_name
) == 0) {
2483 } else if (state_name
[name_len
] == '-' && /* check for '-' from "->" */
2484 strncmp(state_name
, param_name
, name_len
) == 0) {
2485 snprintf(buf
, sizeof(buf
), "$%s", state_name
+ name_len
);
2487 } else if (state_name
[0] == '*' && strcmp(state_name
+ 1, param_name
) == 0) {
2493 const char *get_param_name_var_sym(const char *name
, struct symbol
*sym
)
2495 if (!sym
|| !sym
->ident
)
2498 return state_name_to_param_name(name
, sym
->ident
->name
);
2501 const char *get_mtag_name_var_sym(const char *state_name
, struct symbol
*sym
)
2503 struct symbol
*type
;
2504 const char *sym_name
;
2506 static char buf
[256];
2509 * mtag_name is different from param_name because mtags can be a struct
2510 * instead of a struct pointer. But we want to treat it like a pointer
2511 * because really an mtag is a pointer. Or in other words, if you pass
2512 * a struct foo then you want to talk about foo.bar but with an mtag
2513 * you want to refer to it as foo->bar.
2517 if (!sym
|| !sym
->ident
)
2520 type
= get_real_base_type(sym
);
2521 if (type
&& type
->type
== SYM_BASETYPE
)
2524 sym_name
= sym
->ident
->name
;
2525 name_len
= strlen(sym_name
);
2527 if (state_name
[name_len
] == '.' && /* check for '-' from "->" */
2528 strncmp(state_name
, sym_name
, name_len
) == 0) {
2529 snprintf(buf
, sizeof(buf
), "$->%s", state_name
+ name_len
+ 1);
2533 return state_name_to_param_name(state_name
, sym_name
);
2536 const char *get_mtag_name_expr(struct expression
*expr
)
2540 const char *ret
= NULL
;
2542 name
= expr_to_var_sym(expr
, &sym
);
2546 ret
= get_mtag_name_var_sym(name
, sym
);
2552 const char *get_param_name(struct sm_state
*sm
)
2554 return get_param_name_var_sym(sm
->name
, sm
->sym
);
2557 char *get_data_info_name(struct expression
*expr
)
2564 expr
= strip_expr(expr
);
2565 name
= get_member_name(expr
);
2568 name
= expr_to_var_sym(expr
, &sym
);
2571 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
2573 if (sym
->ctype
.modifiers
& MOD_STATIC
)
2574 snprintf(buf
, sizeof(buf
), "static %s", name
);
2576 snprintf(buf
, sizeof(buf
), "global %s", name
);
2577 ret
= alloc_sname(buf
);