2 * Copyright (C) 2010 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
24 #include "smatch_slist.h"
25 #include "smatch_extra.h"
28 static sqlite3
*mem_db
;
32 #define sql_insert_helper(table, ignore, values...) \
36 char *err, *p = buf; \
42 p += snprintf(p, buf + sizeof(buf) - p, \
43 "insert %sinto %s values (", \
44 ignore ? "or ignore " : "", #table); \
45 p += snprintf(p, buf + sizeof(buf) - p, values); \
46 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
47 sm_debug("in-mem: %s\n", buf); \
48 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err); \
49 if (rc != SQLITE_OK) { \
50 fprintf(stderr, "SQL error #2: %s\n", err); \
51 fprintf(stderr, "SQL: '%s'\n", buf); \
58 sm_printf("SQL: insert %sinto " #table " values(", \
59 ignore ? "or ignore " : ""); \
65 #define sql_insert(table, values...) sql_insert_helper(table, 0, values);
66 #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, 1, values);
70 void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
);
72 ALLOCATOR(def_callback
, "definition db hook callbacks");
73 DECLARE_PTR_LIST(callback_list
, struct def_callback
);
74 static struct callback_list
*select_caller_info_callbacks
;
76 struct member_info_callback
{
78 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
);
80 ALLOCATOR(member_info_callback
, "caller_info callbacks");
81 DECLARE_PTR_LIST(member_info_cb_list
, struct member_info_callback
);
82 static struct member_info_cb_list
*member_callbacks
;
84 struct returned_state_callback
{
85 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*return_expr
);
87 ALLOCATOR(returned_state_callback
, "returned state callbacks");
88 DECLARE_PTR_LIST(returned_state_cb_list
, struct returned_state_callback
);
89 static struct returned_state_cb_list
*returned_state_callbacks
;
91 struct returned_member_callback
{
93 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*expr
, char *printed_name
, struct smatch_state
*state
);
95 ALLOCATOR(returned_member_callback
, "returned member callbacks");
96 DECLARE_PTR_LIST(returned_member_cb_list
, struct returned_member_callback
);
97 static struct returned_member_cb_list
*returned_member_callbacks
;
99 struct call_implies_callback
{
101 void (*callback
)(struct expression
*arg
, char *key
, char *value
);
103 ALLOCATOR(call_implies_callback
, "call_implies callbacks");
104 DECLARE_PTR_LIST(call_implies_cb_list
, struct call_implies_callback
);
105 static struct call_implies_cb_list
*call_implies_cb_list
;
107 static int print_sql_output(void *unused
, int argc
, char **argv
, char **azColName
)
111 for (i
= 0; i
< argc
; i
++) {
114 sm_printf("%s", argv
[i
]);
120 void debug_sql(const char *sql
)
125 sql_exec(print_sql_output
, NULL
, sql
);
128 void debug_mem_sql(const char *sql
)
133 sql_mem_exec(print_sql_output
, NULL
, sql
);
136 void sql_exec(int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
)
141 if (option_no_db
|| !db
)
144 rc
= sqlite3_exec(db
, sql
, callback
, data
, &err
);
145 if (rc
!= SQLITE_OK
&& !parse_error
) {
146 fprintf(stderr
, "SQL error #2: %s\n", err
);
147 fprintf(stderr
, "SQL: '%s'\n", sql
);
152 void sql_mem_exec(int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
)
160 rc
= sqlite3_exec(mem_db
, sql
, callback
, data
, &err
);
161 if (rc
!= SQLITE_OK
) {
162 fprintf(stderr
, "SQL error #2: %s\n", err
);
163 fprintf(stderr
, "SQL: '%s'\n", sql
);
168 static int replace_count
;
169 static char **replace_table
;
170 static const char *replace_return_ranges(const char *return_ranges
)
174 if (!get_function()) {
175 /* I have no idea why EXPORT_SYMBOL() is here */
176 return return_ranges
;
178 for (i
= 0; i
< replace_count
; i
+= 3) {
179 if (strcmp(replace_table
[i
+ 0], get_function()) == 0) {
180 if (strcmp(replace_table
[i
+ 1], return_ranges
) == 0)
181 return replace_table
[i
+ 2];
184 return return_ranges
;
187 void sql_insert_return_states(int return_id
, const char *return_ranges
,
188 int type
, int param
, const char *key
, const char *value
)
190 if (key
&& strlen(key
) >= 80)
192 return_ranges
= replace_return_ranges(return_ranges
);
193 sql_insert(return_states
, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
194 get_base_file(), get_function(), (unsigned long)__inline_fn
,
195 return_id
, return_ranges
, fn_static(), type
, param
, key
, value
);
198 static struct string_list
*common_funcs
;
199 static int is_common_function(const char *fn
)
206 if (strncmp(fn
, "__builtin_", 10) == 0)
209 FOR_EACH_PTR(common_funcs
, tmp
) {
210 if (strcmp(tmp
, fn
) == 0)
212 } END_FOR_EACH_PTR(tmp
);
217 static char *function_signature(void)
219 return type_to_str(get_real_base_type(cur_func_sym
));
222 void sql_insert_caller_info(struct expression
*call
, int type
,
223 int param
, const char *key
, const char *value
)
227 if (!option_info
&& !__inline_call
)
230 if (key
&& strlen(key
) >= 80)
233 fn
= get_fnptr_name(call
->fn
);
239 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
240 get_base_file(), get_function(), fn
, (unsigned long)call
,
241 is_static(call
->fn
), type
, param
, key
, value
);
247 if (strncmp(fn
, "__builtin_", 10) == 0)
250 sm_msg("SQL_caller_info: insert into caller_info values ("
251 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
252 get_base_file(), get_function(), fn
, is_static(call
->fn
),
253 type
, param
, key
, value
);
258 void sql_insert_function_ptr(const char *fn
, const char *struct_name
)
260 sql_insert(function_ptr
, "'%s', '%s', '%s', 0", get_base_file(), fn
,
264 void sql_insert_call_implies(int type
, int param
, const char *key
, const char *value
)
266 sql_insert(call_implies
, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'", get_base_file(),
267 get_function(), (unsigned long)__inline_fn
, fn_static(),
268 type
, param
, key
, value
);
271 void sql_insert_function_type_size(const char *member
, const char *ranges
)
273 sql_insert(function_type_size
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member
, ranges
);
276 void sql_insert_local_values(const char *name
, const char *value
)
278 sql_insert(local_values
, "'%s', '%s', '%s'", get_base_file(), name
, value
);
281 void sql_insert_function_type_value(const char *type
, const char *value
)
283 sql_insert(function_type_value
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type
, value
);
286 void sql_insert_function_type(int param
, const char *value
)
288 sql_insert(function_type
, "'%s', '%s', %d, %d, '%s'",
289 get_base_file(), get_function(), fn_static(), param
, value
);
292 void sql_insert_parameter_name(int param
, const char *value
)
294 sql_insert(parameter_name
, "'%s', '%s', %d, %d, '%s'",
295 get_base_file(), get_function(), fn_static(), param
, value
);
298 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
)
302 data_name
= get_data_info_name(data
);
305 sql_insert(data_info
, "'%s', '%s', %d, '%s'",
306 is_static(data
) ? get_base_file() : "extern",
307 data_name
, type
, value
);
310 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
)
312 sql_insert(data_info
, "'%s', '%s', %d, '%s'",
313 (sym
->ctype
.modifiers
& MOD_STATIC
) ? get_base_file() : "extern",
317 void sql_save_constraint(const char *con
)
322 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", con
);
325 void sql_save_constraint_required(const char *data
, int op
, const char *limit
)
327 sql_insert_or_ignore(constraints_required
, "'%s', '%s', '%s'", data
, show_special(op
), limit
);
330 void sql_insert_fn_ptr_data_link(const char *ptr
, const char *data
)
332 sql_insert(fn_ptr_data_link
, "'%s', '%s'", ptr
, data
);
335 void sql_insert_fn_data_link(struct expression
*fn
, int type
, int param
, const char *key
, const char *value
)
337 if (fn
->type
!= EXPR_SYMBOL
|| !fn
->symbol
->ident
)
340 sql_insert(fn_data_link
, "'%s', '%s', %d, %d, %d, '%s', '%s'",
341 (fn
->symbol
->ctype
.modifiers
& MOD_STATIC
) ? get_base_file() : "extern",
342 fn
->symbol
->ident
->name
,
343 !!(fn
->symbol
->ctype
.modifiers
& MOD_STATIC
),
344 type
, param
, key
, value
);
347 char *get_static_filter(struct symbol
*sym
)
349 static char sql_filter
[1024];
351 /* This can only happen on buggy code. Return invalid SQL. */
353 sql_filter
[0] = '\0';
357 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
358 snprintf(sql_filter
, sizeof(sql_filter
),
359 "file = '%s' and function = '%s' and static = '1'",
360 get_base_file(), sym
->ident
->name
);
362 snprintf(sql_filter
, sizeof(sql_filter
),
363 "function = '%s' and static = '0'", sym
->ident
->name
);
369 static int get_row_count(void *_row_count
, int argc
, char **argv
, char **azColName
)
371 int *row_count
= _row_count
;
376 *row_count
= atoi(argv
[0]);
380 static void mark_params_untracked(struct expression
*call
)
382 struct expression
*arg
;
385 FOR_EACH_PTR(call
->args
, arg
) {
386 mark_untracked(call
, i
++, "$", NULL
);
387 } END_FOR_EACH_PTR(arg
);
390 static void sql_select_return_states_pointer(const char *cols
,
391 struct expression
*call
, int (*callback
)(void*, int, char**, char**), void *info
)
394 int return_count
= 0;
396 ptr
= get_fnptr_name(call
->fn
);
400 run_sql(get_row_count
, &return_count
,
401 "select count(*) from return_states join function_ptr "
402 "where return_states.function == function_ptr.function and "
403 "ptr = '%s' and searchable = 1 and type = %d;", ptr
, INTERNAL
);
404 /* The magic number 100 is just from testing on the kernel. */
405 if (return_count
> 100) {
406 mark_params_untracked(call
);
410 run_sql(callback
, info
,
411 "select %s from return_states join function_ptr where "
412 "return_states.function == function_ptr.function and ptr = '%s' "
413 "and searchable = 1 "
414 "order by function_ptr.file, return_states.file, return_id, type;",
418 static int is_local_symbol(struct expression
*expr
)
420 if (expr
->type
!= EXPR_SYMBOL
)
422 if (expr
->symbol
->ctype
.modifiers
& (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
))
427 void sql_select_return_states(const char *cols
, struct expression
*call
,
428 int (*callback
)(void*, int, char**, char**), void *info
)
432 if (is_fake_call(call
))
435 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
|| is_local_symbol(call
->fn
)) {
436 sql_select_return_states_pointer(cols
, call
, callback
, info
);
440 if (inlinable(call
->fn
)) {
441 mem_sql(callback
, info
,
442 "select %s from return_states where call_id = '%lu' order by return_id, type;",
443 cols
, (unsigned long)call
);
447 run_sql(get_row_count
, &row_count
, "select count(*) from return_states where %s;",
448 get_static_filter(call
->fn
->symbol
));
449 if (row_count
> 3000)
452 run_sql(callback
, info
, "select %s from return_states where %s order by file, return_id, type;",
453 cols
, get_static_filter(call
->fn
->symbol
));
456 void sql_select_call_implies(const char *cols
, struct expression
*call
,
457 int (*callback
)(void*, int, char**, char**))
459 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
462 if (inlinable(call
->fn
)) {
463 mem_sql(callback
, call
,
464 "select %s from call_implies where call_id = '%lu';",
465 cols
, (unsigned long)call
);
469 run_sql(callback
, call
, "select %s from call_implies where %s;",
470 cols
, get_static_filter(call
->fn
->symbol
));
473 struct select_caller_info_data
{
474 struct stree
*final_states
;
479 static void sql_select_caller_info(struct select_caller_info_data
*data
,
480 const char *cols
, struct symbol
*sym
,
481 int (*callback
)(void*, int, char**, char**))
484 mem_sql(callback
, data
,
485 "select %s from caller_info where call_id = %lu;",
486 cols
, (unsigned long)__inline_fn
);
490 if (sym
->ident
->name
&& is_common_function(sym
->ident
->name
))
493 run_sql(callback
, data
,
494 "select %s from caller_info where %s order by call_id;",
495 cols
, get_static_filter(sym
));
498 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
500 struct def_callback
*def_callback
= __alloc_def_callback(0);
502 def_callback
->hook_type
= type
;
503 def_callback
->callback
= callback
;
504 add_ptr_list(&select_caller_info_callbacks
, def_callback
);
508 * These call backs are used when the --info option is turned on to print struct
509 * member information. For example foo->bar could have a state in
510 * smatch_extra.c and also check_user.c.
512 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
514 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
516 member_callback
->owner
= owner
;
517 member_callback
->callback
= callback
;
518 add_ptr_list(&member_callbacks
, member_callback
);
521 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
523 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
525 callback
->callback
= fn
;
526 add_ptr_list(&returned_state_callbacks
, callback
);
529 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
))
531 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
533 member_callback
->owner
= owner
;
534 member_callback
->callback
= callback
;
535 add_ptr_list(&returned_member_callbacks
, member_callback
);
538 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*arg
, char *key
, char *value
))
540 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
543 cb
->callback
= callback
;
544 add_ptr_list(&call_implies_cb_list
, cb
);
548 struct expression
*static_returns_call
;
549 struct symbol
*return_type
;
550 struct range_list
*return_range_list
;
553 static int db_return_callback(void *_ret_info
, int argc
, char **argv
, char **azColName
)
555 struct return_info
*ret_info
= _ret_info
;
556 struct range_list
*rl
;
557 struct expression
*call_expr
= ret_info
->static_returns_call
;
561 call_results_to_rl(call_expr
, ret_info
->return_type
, argv
[0], &rl
);
562 ret_info
->return_range_list
= rl_union(ret_info
->return_range_list
, rl
);
566 struct range_list
*db_return_vals(struct expression
*expr
)
568 struct return_info ret_info
= {};
572 if (is_fake_call(expr
))
575 snprintf(buf
, sizeof(buf
), "return %p", expr
);
576 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
578 return clone_rl(estate_rl(sm
->state
));
579 ret_info
.static_returns_call
= expr
;
580 ret_info
.return_type
= get_type(expr
);
581 if (!ret_info
.return_type
)
584 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
587 ret_info
.return_range_list
= NULL
;
588 if (inlinable(expr
->fn
)) {
589 mem_sql(db_return_callback
, &ret_info
,
590 "select distinct return from return_states where call_id = '%lu';",
591 (unsigned long)expr
);
593 run_sql(db_return_callback
, &ret_info
,
594 "select distinct return from return_states where %s;",
595 get_static_filter(expr
->fn
->symbol
));
597 return ret_info
.return_range_list
;
600 struct range_list
*db_return_vals_from_str(const char *fn_name
)
602 struct return_info ret_info
;
604 ret_info
.static_returns_call
= NULL
;
605 ret_info
.return_type
= &llong_ctype
;
606 ret_info
.return_range_list
= NULL
;
608 run_sql(db_return_callback
, &ret_info
,
609 "select distinct return from return_states where function = '%s';",
611 return ret_info
.return_range_list
;
614 static void match_call_marker(struct expression
*expr
)
618 type
= get_type(expr
->fn
);
619 if (type
&& type
->type
== SYM_PTR
)
620 type
= get_real_base_type(type
);
623 * we just want to record something in the database so that if we have
624 * two calls like: frob(4); frob(some_unkown); then on the receiving
625 * side we know that sometimes frob is called with unknown parameters.
628 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", type_to_str(type
));
631 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct stree
*stree
,
632 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
638 char printed_name
[256];
641 expr
= strip_expr(expr
);
642 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
643 expr
= strip_expr(expr
->unop
);
647 name
= expr_to_var_sym(expr
, &sym
);
652 FOR_EACH_SM(stree
, sm
) {
655 if (strcmp(name
, sm
->name
) == 0) {
657 snprintf(printed_name
, sizeof(printed_name
), "*$");
658 else /* these are already handled. fixme: handle them here */
660 } else if (sm
->name
[0] == '*' && strcmp(name
, sm
->name
+ 1) == 0) {
661 snprintf(printed_name
, sizeof(printed_name
), "*$");
662 } else if (strncmp(name
, sm
->name
, len
) == 0) {
663 if (isalnum(sm
->name
[len
]))
666 snprintf(printed_name
, sizeof(printed_name
), "$->%s", sm
->name
+ len
+ 1);
668 snprintf(printed_name
, sizeof(printed_name
), "$%s", sm
->name
+ len
);
672 callback(call
, param
, printed_name
, sm
);
673 } END_FOR_EACH_SM(sm
);
678 static void match_call_info(struct expression
*call
)
680 struct member_info_callback
*cb
;
681 struct expression
*arg
;
686 name
= get_fnptr_name(call
->fn
);
690 FOR_EACH_PTR(member_callbacks
, cb
) {
691 stree
= get_all_states_stree(cb
->owner
);
693 FOR_EACH_PTR(call
->args
, arg
) {
694 print_struct_members(call
, arg
, i
, stree
, cb
->callback
);
696 } END_FOR_EACH_PTR(arg
);
698 } END_FOR_EACH_PTR(cb
);
703 static int get_param(int param
, char **name
, struct symbol
**sym
)
709 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
711 * this is a temporary hack to work around a bug (I think in sparse?)
712 * 2.6.37-rc1:fs/reiserfs/journal.o
713 * If there is a function definition without parameter name found
714 * after a function implementation then it causes a crash.
718 if (arg
->ident
->name
< (char *)100)
721 *name
= arg
->ident
->name
;
726 } END_FOR_EACH_PTR(arg
);
731 static int function_signature_matches(const char *sig
)
735 my_sig
= function_signature();
737 return 1; /* default to matching */
738 if (strcmp(my_sig
, sig
) == 0)
743 static int caller_info_callback(void *_data
, int argc
, char **argv
, char **azColName
)
745 struct select_caller_info_data
*data
= _data
;
752 struct symbol
*sym
= NULL
;
753 struct def_callback
*def_callback
;
759 func_id
= atoi(argv
[0]);
761 type
= strtol(argv
[1], NULL
, 10);
762 param
= strtol(argv
[2], NULL
, 10);
768 if (data
->prev_func_id
== -1)
769 data
->prev_func_id
= func_id
;
770 if (func_id
!= data
->prev_func_id
) {
771 stree
= __pop_fake_cur_stree();
773 merge_stree(&data
->final_states
, stree
);
775 __push_fake_cur_stree();
777 data
->prev_func_id
= func_id
;
783 if (type
== INTERNAL
&&
784 !function_signature_matches(value
)) {
789 if (param
>= 0 && !get_param(param
, &name
, &sym
))
792 FOR_EACH_PTR(select_caller_info_callbacks
, def_callback
) {
793 if (def_callback
->hook_type
== type
)
794 def_callback
->callback(name
, sym
, key
, value
);
795 } END_FOR_EACH_PTR(def_callback
);
800 static void get_direct_callers(struct select_caller_info_data
*data
, struct symbol
*sym
)
802 sql_select_caller_info(data
,
803 "call_id, type, parameter, key, value", sym
,
804 caller_info_callback
);
807 static struct string_list
*ptr_names_done
;
808 static struct string_list
*ptr_names
;
810 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
812 insert_string(&ptr_names
, alloc_string(argv
[0]));
816 static char *get_next_ptr_name(void)
820 FOR_EACH_PTR(ptr_names
, ptr
) {
821 if (list_has_string(ptr_names_done
, ptr
))
823 insert_string(&ptr_names_done
, ptr
);
825 } END_FOR_EACH_PTR(ptr
);
829 static void get_ptr_names(const char *file
, const char *name
)
831 char sql_filter
[1024];
835 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
838 snprintf(sql_filter
, 1024, "function = '%s';", name
);
841 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
843 run_sql(get_ptr_name
, NULL
,
844 "select distinct ptr from function_ptr where %s",
847 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
851 while ((name
= get_next_ptr_name()))
852 get_ptr_names(NULL
, name
);
855 static void match_data_from_db(struct symbol
*sym
)
857 struct select_caller_info_data data
= { .prev_func_id
= -1 };
861 if (!sym
|| !sym
->ident
)
864 __push_fake_cur_stree();
870 if (sym
->ctype
.modifiers
& MOD_STATIC
)
871 get_ptr_names(get_base_file(), sym
->ident
->name
);
873 get_ptr_names(NULL
, sym
->ident
->name
);
875 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
876 __free_ptr_list((struct ptr_list
**)&ptr_names
);
877 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
878 stree
= __pop_fake_cur_stree();
883 get_direct_callers(&data
, sym
);
885 stree
= __pop_fake_cur_stree();
887 merge_stree(&data
.final_states
, stree
);
889 __push_fake_cur_stree();
891 data
.prev_func_id
= -1;
894 FOR_EACH_PTR(ptr_names
, ptr
) {
895 run_sql(caller_info_callback
, &data
,
896 "select call_id, type, parameter, key, value"
897 " from caller_info where function = '%s' order by call_id",
900 } END_FOR_EACH_PTR(ptr
);
902 __free_ptr_list((struct ptr_list
**)&ptr_names
);
903 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
905 get_direct_callers(&data
, sym
);
908 stree
= __pop_fake_cur_stree();
910 merge_stree(&data
.final_states
, stree
);
913 FOR_EACH_SM(data
.final_states
, sm
) {
915 } END_FOR_EACH_SM(sm
);
917 free_stree(&data
.final_states
);
920 static int call_implies_callbacks(void *_call
, int argc
, char **argv
, char **azColName
)
922 struct expression
*call_expr
= _call
;
923 struct call_implies_callback
*cb
;
924 struct expression
*arg
= NULL
;
931 type
= atoi(argv
[1]);
932 param
= atoi(argv
[2]);
934 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
935 if (cb
->type
!= type
)
938 arg
= get_argument_from_call_expr(call_expr
->args
, param
);
942 cb
->callback(arg
, argv
[3], argv
[4]);
943 } END_FOR_EACH_PTR(cb
);
948 static void match_call_implies(struct expression
*expr
)
950 sql_select_call_implies("function, type, parameter, key, value", expr
,
951 call_implies_callbacks
);
954 static void print_initializer_list(struct expression_list
*expr_list
,
955 struct symbol
*struct_type
)
957 struct expression
*expr
;
958 struct symbol
*base_type
;
959 char struct_name
[256];
961 FOR_EACH_PTR(expr_list
, expr
) {
962 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
963 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
966 if (expr
->type
!= EXPR_IDENTIFIER
)
968 if (!expr
->expr_ident
)
970 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
972 base_type
= get_type(expr
->ident_expression
);
973 if (!base_type
|| base_type
->type
!= SYM_FN
)
975 snprintf(struct_name
, sizeof(struct_name
), "(struct %s)->%s",
976 struct_type
->ident
->name
, expr
->expr_ident
->name
);
977 sql_insert_function_ptr(expr
->ident_expression
->symbol_name
->name
,
979 } END_FOR_EACH_PTR(expr
);
982 static void global_variable(struct symbol
*sym
)
984 struct symbol
*struct_type
;
988 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
990 struct_type
= get_base_type(sym
);
993 if (struct_type
->type
== SYM_ARRAY
) {
994 struct_type
= get_base_type(struct_type
);
998 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
1000 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
1003 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
1005 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", function_signature());
1008 static void call_return_state_hooks_conditional(struct expression
*expr
)
1010 struct returned_state_callback
*cb
;
1011 struct range_list
*rl
;
1012 char *return_ranges
;
1013 int final_pass_orig
= final_pass
;
1015 __push_fake_cur_stree();
1018 __split_whole_condition(expr
->conditional
);
1019 final_pass
= final_pass_orig
;
1021 if (get_implied_rl(expr
->cond_true
, &rl
))
1022 rl
= cast_rl(cur_func_return_type(), rl
);
1024 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_true
)));
1025 return_ranges
= show_rl(rl
);
1026 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(rl
));
1029 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1030 cb
->callback(return_id
, return_ranges
, expr
->cond_true
);
1031 } END_FOR_EACH_PTR(cb
);
1033 __push_true_states();
1034 __use_false_states();
1036 if (get_implied_rl(expr
->cond_false
, &rl
))
1037 rl
= cast_rl(cur_func_return_type(), rl
);
1039 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_false
)));
1040 return_ranges
= show_rl(rl
);
1041 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(rl
));
1044 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1045 cb
->callback(return_id
, return_ranges
, expr
->cond_false
);
1046 } END_FOR_EACH_PTR(cb
);
1048 __merge_true_states();
1049 __free_fake_cur_stree();
1052 static void call_return_state_hooks_compare(struct expression
*expr
)
1054 struct returned_state_callback
*cb
;
1055 char *return_ranges
;
1056 int final_pass_orig
= final_pass
;
1057 sval_t sval
= { .type
= &int_ctype
};
1060 if (!get_implied_value(expr
, &ret
))
1063 __push_fake_cur_stree();
1066 __split_whole_condition(expr
);
1067 final_pass
= final_pass_orig
;
1069 if (ret
.value
!= 0) {
1070 return_ranges
= alloc_sname("1");
1072 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1075 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1076 cb
->callback(return_id
, return_ranges
, expr
);
1077 } END_FOR_EACH_PTR(cb
);
1080 __push_true_states();
1081 __use_false_states();
1083 if (ret
.value
!= 1) {
1084 return_ranges
= alloc_sname("0");
1086 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1089 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1090 cb
->callback(return_id
, return_ranges
, expr
);
1091 } END_FOR_EACH_PTR(cb
);
1094 __merge_true_states();
1095 __free_fake_cur_stree();
1098 static int ptr_in_list(struct sm_state
*sm
, struct state_list
*slist
)
1100 struct sm_state
*tmp
;
1102 FOR_EACH_PTR(slist
, tmp
) {
1103 if (strcmp(tmp
->state
->name
, sm
->state
->name
) == 0)
1105 } END_FOR_EACH_PTR(tmp
);
1110 static char *get_return_compare_str(struct expression
*expr
)
1118 compare_str
= expr_lte_to_param(expr
, -1);
1121 param
= get_param_num(expr
);
1125 var
= expr_to_var(expr
);
1128 snprintf(buf
, sizeof(buf
), "%s orig", var
);
1129 comparison
= get_comparison_strings(var
, buf
);
1135 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(comparison
), param
);
1136 return alloc_sname(buf
);
1139 static int split_possible_helper(struct sm_state
*sm
, struct expression
*expr
)
1141 struct returned_state_callback
*cb
;
1142 struct range_list
*rl
;
1143 char *return_ranges
;
1144 struct sm_state
*tmp
;
1146 int nr_possible
, nr_states
;
1147 char *compare_str
= NULL
;
1149 struct state_list
*already_handled
= NULL
;
1151 if (!sm
|| !sm
->merged
)
1154 if (too_many_possible(sm
))
1157 /* bail if it gets too complicated */
1158 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
1159 nr_states
= stree_count(__get_cur_stree());
1160 if (nr_states
* nr_possible
>= 2000)
1163 FOR_EACH_PTR(sm
->possible
, tmp
) {
1166 if (ptr_in_list(tmp
, already_handled
))
1168 add_ptr_list(&already_handled
, tmp
);
1171 __push_fake_cur_stree();
1173 overwrite_states_using_pool(sm
, tmp
);
1175 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
1176 return_ranges
= show_rl(rl
);
1177 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(clone_rl(rl
)));
1178 compare_str
= get_return_compare_str(expr
);
1180 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1181 return_ranges
= alloc_sname(buf
);
1185 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1186 cb
->callback(return_id
, return_ranges
, expr
);
1187 } END_FOR_EACH_PTR(cb
);
1189 __free_fake_cur_stree();
1190 } END_FOR_EACH_PTR(tmp
);
1192 free_slist(&already_handled
);
1197 static int call_return_state_hooks_split_possible(struct expression
*expr
)
1199 struct sm_state
*sm
;
1201 if (!expr
|| expr_equal_to_param(expr
, -1))
1204 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1205 return split_possible_helper(sm
, expr
);
1208 static const char *get_return_ranges_str(struct expression
*expr
, struct range_list
**rl_p
)
1210 struct range_list
*rl
;
1211 char *return_ranges
;
1220 return alloc_sname("");
1222 if (get_implied_value(expr
, &sval
)) {
1223 sval
= sval_cast(cur_func_return_type(), sval
);
1224 *rl_p
= alloc_rl(sval
, sval
);
1225 return sval_to_str(sval
);
1228 compare_str
= expr_equal_to_param(expr
, -1);
1229 math_str
= get_value_in_terms_of_parameter_math(expr
);
1231 if (get_implied_rl(expr
, &rl
)) {
1232 rl
= cast_rl(cur_func_return_type(), rl
);
1233 return_ranges
= show_rl(rl
);
1234 } else if (get_imaginary_absolute(expr
, &rl
)){
1235 rl
= cast_rl(cur_func_return_type(), rl
);
1236 return alloc_sname(show_rl(rl
));
1238 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
)));
1239 return_ranges
= show_rl(rl
);
1244 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1245 return alloc_sname(buf
);
1249 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1250 return alloc_sname(buf
);
1253 compare_str
= get_return_compare_str(expr
);
1255 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1256 return alloc_sname(buf
);
1258 return return_ranges
;
1261 static int split_positive_from_negative(struct expression
*expr
)
1263 struct returned_state_callback
*cb
;
1264 struct range_list
*rl
;
1265 const char *return_ranges
;
1266 struct range_list
*ret_rl
;
1269 /* We're going to print the states 3 times */
1270 if (stree_count(__get_cur_stree()) > 10000 / 3)
1273 if (!get_implied_rl(expr
, &rl
) || !rl
)
1275 if (is_whole_rl(rl
) || is_whole_rl_non_zero(rl
))
1277 /* Forget about INT_MAX and larger */
1278 if (rl_max(rl
).value
<= 0)
1280 if (!sval_is_negative(rl_min(rl
)))
1283 if (!assume(compare_expression(expr
, '>', zero_expr())))
1287 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1288 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1289 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1290 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1291 } END_FOR_EACH_PTR(cb
);
1295 if (rl_has_sval(rl
, sval_type_val(rl_type(rl
), 0))) {
1296 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
1299 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1300 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1301 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1302 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1303 } END_FOR_EACH_PTR(cb
);
1309 undo
= assume(compare_expression(expr
, '<', zero_expr()));
1312 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1313 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1314 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1315 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1316 } END_FOR_EACH_PTR(cb
);
1324 static int call_return_state_hooks_split_null_non_null(struct expression
*expr
)
1326 struct returned_state_callback
*cb
;
1327 struct range_list
*rl
;
1328 struct range_list
*nonnull_rl
;
1330 struct range_list
*null_rl
= NULL
;
1331 char *return_ranges
;
1332 struct sm_state
*sm
;
1333 struct smatch_state
*state
;
1335 int final_pass_orig
= final_pass
;
1337 if (!expr
|| expr_equal_to_param(expr
, -1))
1339 if (expr
->type
== EXPR_CALL
)
1341 if (!is_pointer(expr
))
1344 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1347 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
1350 if (!estate_rl(state
))
1352 if (estate_min(state
).value
== 0 && estate_max(state
).value
== 0)
1354 if (!rl_has_sval(estate_rl(state
), sval_type_val(estate_type(state
), 0)))
1357 nr_states
= stree_count(__get_cur_stree());
1358 if (option_info
&& nr_states
>= 1500)
1361 rl
= estate_rl(state
);
1363 __push_fake_cur_stree();
1366 __split_whole_condition(expr
);
1367 final_pass
= final_pass_orig
;
1369 nonnull_rl
= rl_filter(rl
, rl_zero());
1370 return_ranges
= show_rl(nonnull_rl
);
1371 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonnull_rl
));
1374 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1375 cb
->callback(return_id
, return_ranges
, expr
);
1376 } END_FOR_EACH_PTR(cb
);
1378 __push_true_states();
1379 __use_false_states();
1381 return_ranges
= alloc_sname("0");
1382 null_sval
= sval_type_val(rl_type(rl
), 0);
1383 add_range(&null_rl
, null_sval
, null_sval
);
1384 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(null_rl
));
1386 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1387 cb
->callback(return_id
, return_ranges
, expr
);
1388 } END_FOR_EACH_PTR(cb
);
1390 __merge_true_states();
1391 __free_fake_cur_stree();
1396 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
1398 struct range_list
*rl
;
1399 struct range_list
*nonzero_rl
;
1401 struct range_list
*zero_rl
= NULL
;
1403 struct returned_state_callback
*cb
;
1404 char *return_ranges
;
1405 int final_pass_orig
= final_pass
;
1408 if (option_project
!= PROJ_KERNEL
)
1411 nr_states
= stree_count(__get_cur_stree());
1412 if (nr_states
> 1500)
1415 if (get_value(expr
, &val
))
1417 if (!get_implied_rl(expr
, &rl
))
1419 if (rl_min(rl
).value
< -4095 || rl_min(rl
).value
>= 0)
1421 if (rl_max(rl
).value
!= 0)
1424 __push_fake_cur_stree();
1427 __split_whole_condition(expr
);
1428 final_pass
= final_pass_orig
;
1430 nonzero_rl
= rl_filter(rl
, rl_zero());
1431 return_ranges
= show_rl(nonzero_rl
);
1432 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonzero_rl
));
1435 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1436 cb
->callback(return_id
, return_ranges
, expr
);
1437 } END_FOR_EACH_PTR(cb
);
1439 __push_true_states();
1440 __use_false_states();
1442 return_ranges
= alloc_sname("0");
1443 zero_sval
= sval_type_val(rl_type(rl
), 0);
1444 add_range(&zero_rl
, zero_sval
, zero_sval
);
1445 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(zero_rl
));
1447 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1448 cb
->callback(return_id
, return_ranges
, expr
);
1449 } END_FOR_EACH_PTR(cb
);
1451 __merge_true_states();
1452 __free_fake_cur_stree();
1457 static int is_boolean(struct expression
*expr
)
1459 struct range_list
*rl
;
1461 if (!get_implied_rl(expr
, &rl
))
1463 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
1468 static int is_conditional(struct expression
*expr
)
1472 if (expr
->type
== EXPR_CONDITIONAL
|| expr
->type
== EXPR_SELECT
)
1477 static int splitable_function_call(struct expression
*expr
)
1479 struct sm_state
*sm
;
1482 if (!expr
|| expr
->type
!= EXPR_CALL
)
1484 snprintf(buf
, sizeof(buf
), "return %p", expr
);
1485 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
1486 return split_possible_helper(sm
, expr
);
1489 static struct sm_state
*find_bool_param(void)
1491 struct stree
*start_states
;
1493 struct sm_state
*sm
, *tmp
;
1496 start_states
= get_start_states();
1498 FOR_EACH_PTR_REVERSE(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
1501 sm
= get_sm_state_stree(start_states
, SMATCH_EXTRA
, arg
->ident
->name
, arg
);
1504 if (rl_min(estate_rl(sm
->state
)).value
!= 0 ||
1505 rl_max(estate_rl(sm
->state
)).value
!= 1)
1508 } END_FOR_EACH_PTR_REVERSE(arg
);
1514 * Check if it's splitable. If not, then splitting it up is likely not
1515 * useful for the callers.
1517 FOR_EACH_PTR(sm
->possible
, tmp
) {
1520 if (!estate_get_single_value(tmp
->state
, &sval
))
1522 } END_FOR_EACH_PTR(tmp
);
1527 static int split_on_bool_sm(struct sm_state
*sm
, struct expression
*expr
)
1529 struct returned_state_callback
*cb
;
1530 struct range_list
*ret_rl
;
1531 const char *return_ranges
;
1532 struct sm_state
*tmp
;
1534 int nr_possible
, nr_states
;
1535 char *compare_str
= NULL
;
1537 struct state_list
*already_handled
= NULL
;
1539 if (!sm
|| !sm
->merged
)
1542 if (too_many_possible(sm
))
1545 /* bail if it gets too complicated */
1546 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
1547 nr_states
= stree_count(__get_cur_stree());
1548 if (nr_states
* nr_possible
>= 2000)
1551 FOR_EACH_PTR(sm
->possible
, tmp
) {
1554 if (ptr_in_list(tmp
, already_handled
))
1556 add_ptr_list(&already_handled
, tmp
);
1559 __push_fake_cur_stree();
1561 overwrite_states_using_pool(sm
, tmp
);
1563 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1564 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1565 compare_str
= get_return_compare_str(expr
);
1567 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1568 return_ranges
= alloc_sname(buf
);
1572 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1573 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1574 } END_FOR_EACH_PTR(cb
);
1576 __free_fake_cur_stree();
1577 } END_FOR_EACH_PTR(tmp
);
1579 free_slist(&already_handled
);
1584 static int split_by_bool_param(struct expression
*expr
)
1586 struct sm_state
*start_sm
, *sm
;
1589 start_sm
= find_bool_param();
1592 sm
= get_sm_state(SMATCH_EXTRA
, start_sm
->name
, start_sm
->sym
);
1593 if (!sm
|| estate_get_single_value(sm
->state
, &sval
))
1595 return split_on_bool_sm(sm
, expr
);
1598 static int split_by_null_nonnull_param(struct expression
*expr
)
1601 struct sm_state
*sm
;
1603 /* function must only take one pointer */
1604 if (ptr_list_size((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
) != 1)
1606 arg
= first_ptr_list((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
);
1609 if (get_real_base_type(arg
)->type
!= SYM_PTR
)
1612 if (param_was_set_var_sym(arg
->ident
->name
, arg
))
1614 sm
= get_sm_state(SMATCH_EXTRA
, arg
->ident
->name
, arg
);
1618 return split_on_bool_sm(sm
, expr
);
1621 static void call_return_state_hooks(struct expression
*expr
)
1623 struct returned_state_callback
*cb
;
1624 struct range_list
*ret_rl
;
1625 const char *return_ranges
;
1629 if (__path_is_null())
1632 expr
= strip_expr(expr
);
1634 if (is_impossible_path())
1637 if (expr
&& (expr
->type
== EXPR_COMPARE
||
1638 !get_implied_value(expr
, &sval
)) &&
1639 (is_condition(expr
) || is_boolean(expr
))) {
1640 call_return_state_hooks_compare(expr
);
1642 } else if (is_conditional(expr
)) {
1643 call_return_state_hooks_conditional(expr
);
1645 } else if (call_return_state_hooks_split_possible(expr
)) {
1647 } else if (call_return_state_hooks_split_null_non_null(expr
)) {
1649 } else if (call_return_state_hooks_split_success_fail(expr
)) {
1651 } else if (splitable_function_call(expr
)) {
1653 } else if (split_positive_from_negative(expr
)) {
1655 } else if (split_by_bool_param(expr
)) {
1656 } else if (split_by_null_nonnull_param(expr
)) {
1661 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1662 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1665 nr_states
= stree_count(__get_cur_stree());
1666 if (nr_states
>= 10000) {
1667 match_return_info(return_id
, (char *)return_ranges
, expr
);
1670 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1671 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1672 } END_FOR_EACH_PTR(cb
);
1675 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
1677 struct returned_member_callback
*cb
;
1678 struct stree
*stree
;
1679 struct sm_state
*sm
;
1680 struct symbol
*type
;
1682 char member_name
[256];
1685 type
= get_type(expr
);
1686 if (!type
|| type
->type
!= SYM_PTR
)
1688 name
= expr_to_var(expr
);
1692 member_name
[sizeof(member_name
) - 1] = '\0';
1693 strcpy(member_name
, "$");
1696 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
1697 stree
= __get_cur_stree();
1698 FOR_EACH_MY_SM(cb
->owner
, stree
, sm
) {
1699 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
1700 strcpy(member_name
, "*$");
1701 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1704 if (strncmp(sm
->name
, name
, len
) != 0)
1706 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
1708 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
1709 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1710 } END_FOR_EACH_SM(sm
);
1711 } END_FOR_EACH_PTR(cb
);
1716 static void reset_memdb(struct symbol
*sym
)
1718 mem_sql(NULL
, NULL
, "delete from caller_info;");
1719 mem_sql(NULL
, NULL
, "delete from return_states;");
1720 mem_sql(NULL
, NULL
, "delete from call_implies;");
1723 static void match_end_func_info(struct symbol
*sym
)
1725 if (__path_is_null())
1727 call_return_state_hooks(NULL
);
1730 static void match_after_func(struct symbol
*sym
)
1736 static void init_memdb(void)
1740 const char *schema_files
[] = {
1742 "db/caller_info.schema",
1743 "db/return_states.schema",
1744 "db/function_type_size.schema",
1745 "db/type_size.schema",
1746 "db/call_implies.schema",
1747 "db/function_ptr.schema",
1748 "db/local_values.schema",
1749 "db/function_type_value.schema",
1750 "db/type_value.schema",
1751 "db/function_type.schema",
1752 "db/data_info.schema",
1753 "db/parameter_name.schema",
1754 "db/constraints.schema",
1755 "db/constraints_required.schema",
1756 "db/fn_ptr_data_link.schema",
1757 "db/fn_data_link.schema",
1759 static char buf
[4096];
1764 rc
= sqlite3_open(":memory:", &mem_db
);
1765 if (rc
!= SQLITE_OK
) {
1766 printf("Error starting In-Memory database.");
1770 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
1771 fd
= open_data_file(schema_files
[i
]);
1776 ret
= read(fd
, buf
, sizeof(buf
));
1777 if (ret
== sizeof(buf
)) {
1778 printf("Schema file too large: %s (limit %zd bytes)",
1779 schema_files
[i
], sizeof(buf
));
1782 rc
= sqlite3_exec(mem_db
, buf
, NULL
, NULL
, &err
);
1783 if (rc
!= SQLITE_OK
) {
1784 fprintf(stderr
, "SQL error #2: %s\n", err
);
1785 fprintf(stderr
, "%s\n", buf
);
1790 void open_smatch_db(void)
1799 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
1800 if (rc
!= SQLITE_OK
) {
1807 static void register_common_funcs(void)
1809 struct token
*token
;
1813 if (option_project
== PROJ_NONE
)
1814 strcpy(filename
, "common_functions");
1816 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
1818 token
= get_tokens_file(filename
);
1821 if (token_type(token
) != TOKEN_STREAMBEGIN
)
1823 token
= token
->next
;
1824 while (token_type(token
) != TOKEN_STREAMEND
) {
1825 if (token_type(token
) != TOKEN_IDENT
)
1827 func
= alloc_string(show_ident(token
->ident
));
1828 add_ptr_list(&common_funcs
, func
);
1829 token
= token
->next
;
1831 clear_token_alloc();
1834 static char *get_next_string(char **str
)
1836 static char string
[256];
1845 while (*p
!= '\0' && *p
!= ' ' && *p
!= '\n')
1850 memcpy(string
, start
, 255);
1852 printf("return_fix: '%s' too long", string
);
1856 memcpy(string
, start
, len
);
1864 static void register_return_replacements(void)
1866 char *func
, *orig
, *new;
1872 snprintf(filename
, 256, "db/%s.return_fixes", option_project_str
);
1873 fd
= open_data_file(filename
);
1876 ret
= read(fd
, buf
, sizeof(buf
));
1880 if (ret
== sizeof(buf
)) {
1881 printf("file too large: %s (limit %zd bytes)",
1882 filename
, sizeof(buf
));
1889 get_next_string(&p
);
1892 if (replace_count
== 0 || replace_count
% 3 != 0) {
1896 replace_table
= malloc(replace_count
* sizeof(char *));
1901 func
= alloc_string(get_next_string(&p
));
1902 orig
= alloc_string(get_next_string(&p
));
1903 new = alloc_string(get_next_string(&p
));
1905 replace_table
[i
++] = func
;
1906 replace_table
[i
++] = orig
;
1907 replace_table
[i
++] = new;
1911 void register_definition_db_callbacks(int id
)
1913 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1914 add_hook(&global_variable
, BASE_HOOK
);
1915 add_hook(&global_variable
, DECLARATION_HOOK
);
1916 add_split_return_callback(match_return_info
);
1917 add_split_return_callback(print_returned_struct_members
);
1918 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
1919 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
1920 add_hook(&match_after_func
, AFTER_FUNC_HOOK
);
1922 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
1923 add_hook(&match_call_implies
, CALL_HOOK_AFTER_INLINE
);
1925 register_common_funcs();
1926 register_return_replacements();
1929 void register_db_call_marker(int id
)
1931 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
1934 char *return_state_to_var_sym(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
)
1936 struct expression
*arg
;
1938 char member_name
[256];
1943 const char *star
= "";
1945 if (expr
->type
!= EXPR_ASSIGNMENT
)
1947 name
= expr_to_var_sym(expr
->left
, sym
);
1950 if (key
[0] == '*') {
1954 if (strncmp(key
, "$", 1) != 0)
1956 snprintf(member_name
, sizeof(member_name
), "%s%s%s", star
, name
, key
+ 1);
1958 return alloc_string(member_name
);
1961 while (expr
->type
== EXPR_ASSIGNMENT
)
1962 expr
= strip_expr(expr
->right
);
1963 if (expr
->type
!= EXPR_CALL
)
1966 arg
= get_argument_from_call_expr(expr
->args
, param
);
1970 return get_variable_from_key(arg
, key
, sym
);
1973 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
)
1981 arg
= strip_expr(arg
);
1983 if (strcmp(key
, "$") == 0)
1984 return expr_to_var_sym(arg
, sym
);
1986 if (strcmp(key
, "*$") == 0) {
1987 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1988 arg
= strip_expr(arg
->unop
);
1989 return expr_to_var_sym(arg
, sym
);
1991 tmp
= expr_to_var_sym(arg
, sym
);
1994 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
1996 return alloc_string(buf
);
2000 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
2001 arg
= strip_expr(arg
->unop
);
2002 tmp
= expr_to_var_sym(arg
, sym
);
2005 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 3);
2006 return alloc_string(buf
);
2009 tmp
= expr_to_var_sym(arg
, sym
);
2012 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 1);
2014 return alloc_string(buf
);
2017 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
)
2021 if (strcmp("$", key
) == 0)
2022 return expr_to_chunk_sym_vsl(arg
, sym
, vsl
);
2023 return get_variable_from_key(arg
, key
, sym
);
2026 const char *state_name_to_param_name(const char *state_name
, const char *param_name
)
2029 static char buf
[256];
2031 name_len
= strlen(param_name
);
2033 if (strcmp(state_name
, param_name
) == 0) {
2035 } else if (state_name
[name_len
] == '-' && /* check for '-' from "->" */
2036 strncmp(state_name
, param_name
, name_len
) == 0) {
2037 snprintf(buf
, sizeof(buf
), "$%s", state_name
+ name_len
);
2039 } else if (state_name
[0] == '*' && strcmp(state_name
+ 1, param_name
) == 0) {
2045 const char *get_param_name_var_sym(const char *name
, struct symbol
*sym
)
2047 if (!sym
|| !sym
->ident
)
2050 return state_name_to_param_name(name
, sym
->ident
->name
);
2053 const char *get_param_name(struct sm_state
*sm
)
2055 return get_param_name_var_sym(sm
->name
, sm
->sym
);
2058 char *get_data_info_name(struct expression
*expr
)
2065 expr
= strip_expr(expr
);
2066 name
= get_member_name(expr
);
2069 name
= expr_to_var_sym(expr
, &sym
);
2072 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
2074 if (sym
->ctype
.modifiers
& MOD_STATIC
)
2075 snprintf(buf
, sizeof(buf
), "static %s", name
);
2077 snprintf(buf
, sizeof(buf
), "global %s", name
);
2078 ret
= alloc_sname(buf
);