2 * Copyright (C) 2010 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
26 struct sqlite3
*smatch_db
;
27 struct sqlite3
*mem_db
;
28 struct sqlite3
*cache_db
;
37 static void call_return_state_hooks(struct expression
*expr
);
38 static void call_return_states_callbacks(const char *return_ranges
, struct expression
*expr
);
40 #define SQLITE_CACHE_PAGES 1000
44 void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
);
46 ALLOCATOR(def_callback
, "definition db hook callbacks");
47 DECLARE_PTR_LIST(callback_list
, struct def_callback
);
48 static struct callback_list
*select_caller_info_callbacks
;
50 struct def_name_sym_callback
{
52 void (*callback
)(const char *name
, struct symbol
*sym
, char *value
);
54 ALLOCATOR(def_name_sym_callback
, "definition db hook callbacks");
55 DECLARE_PTR_LIST(name_sym_callback_list
, struct def_name_sym_callback
);
56 static struct name_sym_callback_list
*select_caller_name_sym_callbacks
;
58 struct member_info_callback
{
60 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
);
62 ALLOCATOR(member_info_callback
, "caller_info callbacks");
63 DECLARE_PTR_LIST(member_info_cb_list
, struct member_info_callback
);
64 static struct member_info_cb_list
*member_callbacks
;
65 static struct member_info_cb_list
*member_callbacks_new
;
67 struct return_info_callback
{
69 void (*callback
)(int return_id
, char *return_ranges
,
70 struct expression
*returned_expr
,
72 const char *printed_name
,
75 ALLOCATOR(return_info_callback
, "return_info callbacks");
76 DECLARE_PTR_LIST(return_info_cb_list
, struct return_info_callback
);
77 static struct return_info_cb_list
*return_callbacks
;
79 struct returned_state_callback
{
80 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*return_expr
);
82 ALLOCATOR(returned_state_callback
, "returned state callbacks");
83 DECLARE_PTR_LIST(returned_state_cb_list
, struct returned_state_callback
);
84 static struct returned_state_cb_list
*returned_state_callbacks
;
86 struct returned_member_callback
{
88 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*expr
, char *printed_name
, struct smatch_state
*state
);
90 ALLOCATOR(returned_member_callback
, "returned member callbacks");
91 DECLARE_PTR_LIST(returned_member_cb_list
, struct returned_member_callback
);
92 static struct returned_member_cb_list
*returned_member_callbacks
;
94 struct db_implies_callback
{
96 void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
);
98 ALLOCATOR(db_implies_callback
, "return_implies callbacks");
99 DECLARE_PTR_LIST(db_implies_cb_list
, struct db_implies_callback
);
100 static struct db_implies_cb_list
*return_implies_cb_list
;
101 static struct db_implies_cb_list
*call_implies_cb_list
;
103 DECLARE_PTR_LIST(delete_list
, delete_hook
);
104 static struct delete_list
*delete_hooks
;
107 const char *func
, *rl
;
109 static struct split_data
**forced_splits
;
110 static int split_count
;
112 /* silently truncates if needed. */
113 char *escape_newlines(const char *str
)
119 for (i
= 0, j
= 0; str
[i
] != '\0' && j
!= sizeof(buf
); i
++, j
++) {
120 if (str
[i
] != '\r' && str
[i
] != '\n') {
127 if (j
== sizeof(buf
))
133 return alloc_sname(str
);
135 if (j
== sizeof(buf
))
137 return alloc_sname(buf
);
140 static int print_sql_output(void *unused
, int argc
, char **argv
, char **azColName
)
144 for (i
= 0; i
< argc
; i
++) {
147 sm_printf("%s", argv
[i
]);
153 void sql_exec(struct sqlite3
*db
, int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
)
161 if (option_debug
|| debug_db
) {
163 if (strncasecmp(sql
, "select", strlen("select")) == 0)
164 sqlite3_exec(db
, sql
, print_sql_output
, NULL
, NULL
);
167 rc
= sqlite3_exec(db
, sql
, callback
, data
, &err
);
168 if (rc
!= SQLITE_OK
&& !parse_error
) {
169 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err
);
170 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql
);
175 static int replace_count
;
176 static char **replace_table
;
177 static const char *replace_return_ranges(const char *return_ranges
)
181 if (!get_function()) {
182 /* I have no idea why EXPORT_SYMBOL() is here */
183 return return_ranges
;
185 for (i
= 0; i
< replace_count
; i
+= 3) {
186 if (strcmp(replace_table
[i
+ 0], get_function()) == 0) {
187 if (strcmp(replace_table
[i
+ 1], return_ranges
) == 0)
188 return replace_table
[i
+ 2];
191 return return_ranges
;
194 static int delete_count
;
195 static char **delete_table
;
196 static bool is_delete_return(const char *return_ranges
)
203 for (i
= 0; i
< delete_count
; i
+= 2) {
204 if (strcmp(delete_table
[i
], get_function()) == 0 &&
205 strcmp(delete_table
[i
+ 1], return_ranges
) == 0)
212 void add_delete_return_hook(delete_hook
*hook
)
214 add_ptr_list(&delete_hooks
, hook
);
217 static bool is_project_delete_return(struct expression
*expr
)
221 FOR_EACH_PTR(delete_hooks
, hook
) {
224 } END_FOR_EACH_PTR(hook
);
228 static char *use_states
;
229 static int get_db_state_count(void)
234 FOR_EACH_SM(__get_cur_stree(), sm
) {
235 if (sm
->owner
== USHRT_MAX
)
237 if (use_states
[sm
->owner
])
239 } END_FOR_EACH_SM(sm
);
243 static bool is_local(struct symbol
*sym
)
245 if (sym
->ctype
.modifiers
& MOD_STATIC
)
247 if ((sym
->ctype
.modifiers
& MOD_EXTERN
) &&
248 (sym
->ctype
.modifiers
& MOD_INLINE
))
251 if (!sym
->definition
)
254 if ((sym
->definition
->ctype
.modifiers
& MOD_EXTERN
) &&
255 (sym
->definition
->ctype
.modifiers
& MOD_INLINE
))
261 void db_ignore_states(int id
)
266 unsigned long long __fn_mtag
;
267 static void set_fn_mtag(struct symbol
*sym
)
271 if (is_local(cur_func_sym
))
272 snprintf(buf
, sizeof(buf
), "%s %s", get_base_file(), get_function());
274 snprintf(buf
, sizeof(buf
), "extern %s", get_function());
276 __fn_mtag
= str_to_mtag(buf
);
279 void sql_insert_return_states(int return_id
, const char *return_ranges
,
280 int type
, int param
, const char *key
, const char *value
)
282 unsigned long long id
;
285 if (key
&& strlen(key
) >= 80)
288 id
= (unsigned long)__inline_fn
;
292 sql_insert(return_states
, "'%s', '%s', %llu, %d, '%s', %d, %d, %d, '%s', '%s'",
293 get_base_file(), get_function(), id
, return_id
,
294 return_ranges
, fn_static(), type
, param
, key
, value
);
297 static struct string_list
*common_funcs
;
298 static int is_common_function(const char *fn
)
305 if (strncmp(fn
, "__builtin_", 10) == 0)
308 FOR_EACH_PTR(common_funcs
, tmp
) {
309 if (strcmp(tmp
, fn
) == 0)
311 } END_FOR_EACH_PTR(tmp
);
316 static char *function_signature(void)
318 return type_to_str(get_real_base_type(cur_func_sym
));
321 void sql_insert_caller_info(struct expression
*call
, int type
,
322 int param
, const char *key
, const char *value
)
324 FILE *tmp_fd
= sm_outfd
;
327 if (!option_info
&& !__inline_call
)
332 if (key
&& strlen(key
) >= 80)
335 fn
= get_fnptr_name(call
->fn
);
341 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
342 get_base_file(), get_function(), fn
, (unsigned long)call
,
343 is_static(call
->fn
), type
, param
, key
, value
);
349 if (strncmp(fn
, "__builtin_", 10) == 0)
351 if (type
!= INTERNAL
&& is_common_function(fn
))
354 sm_outfd
= caller_info_fd
;
355 sm_msg("SQL_caller_info: insert into caller_info values ("
356 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
357 get_base_file(), get_function(), fn
, is_static(call
->fn
),
358 type
, param
, key
, value
);
364 void sql_insert_function_ptr(const char *fn
, const char *struct_name
)
366 sql_insert_or_ignore(function_ptr
, "'%s', '%s', '%s', 0",
367 get_base_file(), fn
, struct_name
);
370 void sql_insert_return_implies(int type
, int param
, const char *key
, const char *value
)
372 sql_insert_or_ignore(return_implies
, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
373 get_base_file(), get_function(), (unsigned long)__inline_fn
,
374 fn_static(), type
, param
, key
, value
);
377 void sql_insert_call_implies(int type
, int param
, const char *key
, const char *value
)
379 sql_insert_or_ignore(call_implies
, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
380 get_base_file(), get_function(), (unsigned long)__inline_fn
,
381 fn_static(), type
, param
, key
, value
);
384 void sql_insert_function_type_size(const char *member
, const char *ranges
)
386 sql_insert(function_type_size
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member
, ranges
);
389 void sql_insert_function_type_info(int type
, const char *struct_type
, const char *member
, const char *value
)
391 sql_insert(function_type_info
, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type
, struct_type
, member
, value
);
394 void sql_insert_type_info(int type
, const char *member
, const char *value
)
396 sql_insert_cache(type_info
, "'%s', %d, '%s', '%s'", get_base_file(), type
, member
, value
);
399 void sql_insert_local_values(const char *name
, const char *value
)
401 sql_insert(local_values
, "'%s', '%s', '%s'", get_base_file(), name
, value
);
404 void sql_insert_function_type_value(const char *type
, const char *value
)
406 sql_insert(function_type_value
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type
, value
);
409 void sql_insert_function_type(int param
, const char *value
)
411 sql_insert(function_type
, "'%s', '%s', %d, %d, '%s'",
412 get_base_file(), get_function(), fn_static(), param
, value
);
415 void sql_insert_parameter_name(int param
, const char *value
)
417 sql_insert(parameter_name
, "'%s', '%s', %d, %d, '%s'",
418 get_base_file(), get_function(), fn_static(), param
, value
);
421 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
)
425 data_name
= get_data_info_name(data
);
428 sql_insert(data_info
, "'%s', '%s', %d, '%s'",
429 is_static(data
) ? get_base_file() : "extern",
430 data_name
, type
, value
);
433 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
)
435 sql_insert(data_info
, "'%s', '%s', %d, '%s'",
436 (sym
->ctype
.modifiers
& MOD_STATIC
) ? get_base_file() : "extern",
440 void sql_save_constraint(const char *con
)
445 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con
));
448 void sql_save_constraint_required(const char *data
, int op
, const char *limit
)
450 sql_insert_or_ignore(constraints_required
, "'%s', '%s', '%s'", data
, show_special(op
), limit
);
453 void sql_copy_constraint_required(const char *new_limit
, const char *old_limit
)
458 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
459 "select constraints_required.data, constraints_required.op, '%s' from "
460 "constraints_required where bound = '%s';", new_limit
, old_limit
);
463 void sql_insert_fn_ptr_data_link(const char *ptr
, const char *data
)
465 sql_insert_or_ignore(fn_ptr_data_link
, "'%s', '%s'", ptr
, data
);
468 void sql_insert_fn_data_link(struct expression
*fn
, int type
, int param
, const char *key
, const char *value
)
470 if (fn
->type
!= EXPR_SYMBOL
|| !fn
->symbol
->ident
)
473 sql_insert(fn_data_link
, "'%s', '%s', %d, %d, %d, '%s', '%s'",
474 is_local(fn
->symbol
) ? get_base_file() : "extern",
475 fn
->symbol
->ident
->name
,
476 is_local(fn
->symbol
),
477 type
, param
, key
, value
);
480 void sql_insert_mtag_about(mtag_t tag
, const char *left_name
, const char *right_name
)
482 sql_insert_cache(mtag_about
, "%lld, '%s', '%s', %d, '%s', '%s'",
483 tag
, get_filename(), get_function(), get_lineno(),
484 left_name
, right_name
);
487 void sql_insert_mtag_info(mtag_t tag
, int type
, const char *value
)
489 sql_insert_cache(mtag_info
, "'%s', %lld, %d, '%s'", get_filename(), tag
, type
, value
);
492 void sql_insert_mtag_map(mtag_t container
, int container_offset
, mtag_t tag
, int tag_offset
)
494 sql_insert(mtag_map
, "%lld, %d, %lld, %d", container
, container_offset
, tag
, tag_offset
);
497 void sql_insert_mtag_alias(mtag_t orig
, mtag_t alias
)
499 sql_insert(mtag_alias
, "%lld, %lld", orig
, alias
);
502 static int save_mtag(void *_tag
, int argc
, char **argv
, char **azColName
)
504 mtag_t
*saved_tag
= _tag
;
507 new_tag
= strtoll(argv
[0], NULL
, 10);
510 *saved_tag
= new_tag
;
511 else if (*saved_tag
!= new_tag
)
517 int mtag_map_select_container(mtag_t tag
, int container_offset
, mtag_t
*container
)
521 run_sql(save_mtag
, &tmp
,
522 "select container from mtag_map where tag = %lld and container_offset = %d and tag_offset = 0;",
523 tag
, container_offset
);
525 if (tmp
== 0 || tmp
== -1ULL)
531 int mtag_map_select_tag(mtag_t container
, int offset
, mtag_t
*tag
)
535 run_sql(save_mtag
, &tmp
,
536 "select tag from mtag_map where container = %lld and container_offset = %d;",
539 if (tmp
== 0 || tmp
== -1ULL)
545 char *get_static_filter(struct symbol
*sym
)
547 static char sql_filter
[1024];
549 /* This can only happen on buggy code. Return invalid SQL. */
551 sql_filter
[0] = '\0';
556 snprintf(sql_filter
, sizeof(sql_filter
),
557 "file = '%s' and function = '%s' and static = '1'",
558 get_base_file(), sym
->ident
->name
);
560 snprintf(sql_filter
, sizeof(sql_filter
),
561 "function = '%s' and static = '0'", sym
->ident
->name
);
567 static int get_row_count(void *_row_count
, int argc
, char **argv
, char **azColName
)
569 int *row_count
= _row_count
;
574 *row_count
= atoi(argv
[0]);
578 static void mark_call_params_untracked(struct expression
*call
)
580 struct expression
*arg
;
583 FOR_EACH_PTR(call
->args
, arg
) {
584 mark_untracked(call
, i
++, "$", NULL
);
585 } END_FOR_EACH_PTR(arg
);
588 static void sql_select_return_states_pointer(const char *cols
,
589 struct expression
*call
, int (*callback
)(void*, int, char**, char**), void *info
)
592 int return_count
= 0;
594 ptr
= get_fnptr_name(call
->fn
);
598 run_sql(get_row_count
, &return_count
,
599 "select count(*) from return_states join function_ptr "
600 "where return_states.function == function_ptr.function and "
601 "ptr = '%s' and searchable = 1 and type = %d;", ptr
, INTERNAL
);
602 /* The magic number 100 is just from testing on the kernel. */
603 if (return_count
> 100) {
604 mark_call_params_untracked(call
);
608 run_sql(callback
, info
,
609 "select %s from return_states join function_ptr where "
610 "return_states.function == function_ptr.function and ptr = '%s' "
611 "and searchable = 1 "
612 "order by function_ptr.file, return_states.file, return_id, type;",
616 static int is_local_symbol(struct expression
*expr
)
618 if (expr
->type
!= EXPR_SYMBOL
)
620 if (expr
->symbol
->ctype
.modifiers
& (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
))
625 bool is_fn_ptr(struct expression
*fn
)
628 if (fn
->type
!= EXPR_SYMBOL
)
632 if (is_local_symbol(fn
))
637 void sql_select_return_states(const char *cols
, struct expression
*call
,
638 int (*callback
)(void*, int, char**, char**), void *info
)
640 struct expression
*fn
;
643 if (is_fake_call(call
))
646 fn
= strip_expr(call
->fn
);
648 sql_select_return_states_pointer(cols
, call
, callback
, info
);
653 mem_sql(callback
, info
,
654 "select %s from return_states where call_id = '%lu' order by return_id, type;",
655 cols
, (unsigned long)call
);
659 run_sql(get_row_count
, &row_count
, "select count(*) from return_states where %s;",
660 get_static_filter(fn
->symbol
));
661 if (row_count
== 0 && fn
->symbol
&& fn
->symbol
->definition
)
662 set_state(my_id
, "db_incomplete", NULL
, &incomplete
);
663 if (row_count
> 3000)
666 run_sql(callback
, info
, "select %s from return_states where %s order by file, return_id, type;",
667 cols
, get_static_filter(fn
->symbol
));
670 bool db_incomplete(void)
672 return !!get_state(my_id
, "db_incomplete", NULL
);
675 #define CALL_IMPLIES 0
676 #define RETURN_IMPLIES 1
678 struct implies_info
{
680 struct db_implies_cb_list
*cb_list
;
681 struct expression
*expr
;
685 void sql_select_implies(const char *cols
, struct implies_info
*info
,
686 int (*callback
)(void*, int, char**, char**))
688 if (info
->type
== RETURN_IMPLIES
&& inlinable(info
->expr
->fn
)) {
689 mem_sql(callback
, info
,
690 "select %s from return_implies where call_id = '%lu';",
691 cols
, (unsigned long)info
->expr
);
695 run_sql(callback
, info
, "select %s from %s_implies where %s;",
697 info
->type
== CALL_IMPLIES
? "call" : "return",
698 get_static_filter(info
->sym
));
701 struct select_caller_info_data
{
702 struct stree
*final_states
;
703 struct timeval start_time
;
709 static int caller_info_callback(void *_data
, int argc
, char **argv
, char **azColName
);
711 static void sql_select_caller_info(struct select_caller_info_data
*data
,
712 const char *cols
, struct symbol
*sym
)
715 mem_sql(caller_info_callback
, data
,
716 "select %s from caller_info where call_id = %lu;",
717 cols
, (unsigned long)__inline_fn
);
721 if (sym
->ident
->name
&& is_common_function(sym
->ident
->name
))
723 run_sql(caller_info_callback
, data
,
724 "select %s from common_caller_info where %s order by call_id;",
725 cols
, get_static_filter(sym
));
729 run_sql(caller_info_callback
, data
,
730 "select %s from caller_info where %s order by call_id;",
731 cols
, get_static_filter(sym
));
734 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
736 struct def_callback
*def_callback
= __alloc_def_callback(0);
738 def_callback
->hook_type
= type
;
739 def_callback
->callback
= callback
;
740 add_ptr_list(&select_caller_info_callbacks
, def_callback
);
743 void select_caller_name_sym(void (*fn
)(const char *name
, struct symbol
*sym
, char *value
), int type
)
745 struct def_name_sym_callback
*callback
= __alloc_def_name_sym_callback(0);
747 callback
->hook_type
= type
;
748 callback
->callback
= fn
;
749 add_ptr_list(&select_caller_name_sym_callbacks
, callback
);
753 * These call backs are used when the --info option is turned on to print struct
754 * member information. For example foo->bar could have a state in
755 * smatch_extra.c and also check_user.c.
757 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
759 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
761 member_callback
->owner
= owner
;
762 member_callback
->callback
= callback
;
763 add_ptr_list(&member_callbacks
, member_callback
);
766 void add_caller_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
768 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
770 member_callback
->owner
= owner
;
771 member_callback
->callback
= callback
;
772 add_ptr_list(&member_callbacks_new
, member_callback
);
775 void add_return_info_callback(int owner
,
776 void (*callback
)(int return_id
, char *return_ranges
,
777 struct expression
*returned_expr
,
779 const char *printed_name
,
780 struct sm_state
*sm
))
782 struct return_info_callback
*return_callback
= __alloc_return_info_callback(0);
784 return_callback
->owner
= owner
;
785 return_callback
->callback
= callback
;
786 add_ptr_list(&return_callbacks
, return_callback
);
789 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
791 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
793 callback
->callback
= fn
;
794 add_ptr_list(&returned_state_callbacks
, callback
);
797 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
))
799 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
801 member_callback
->owner
= owner
;
802 member_callback
->callback
= callback
;
803 add_ptr_list(&returned_member_callbacks
, member_callback
);
806 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
))
808 struct db_implies_callback
*cb
= __alloc_db_implies_callback(0);
811 cb
->callback
= callback
;
812 add_ptr_list(&call_implies_cb_list
, cb
);
815 void select_return_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
))
817 struct db_implies_callback
*cb
= __alloc_db_implies_callback(0);
820 cb
->callback
= callback
;
821 add_ptr_list(&return_implies_cb_list
, cb
);
825 struct expression
*static_returns_call
;
826 struct symbol
*return_type
;
827 struct range_list
*return_range_list
;
830 static int db_return_callback(void *_ret_info
, int argc
, char **argv
, char **azColName
)
832 struct return_info
*ret_info
= _ret_info
;
833 struct range_list
*rl
;
834 struct expression
*call_expr
= ret_info
->static_returns_call
;
838 call_results_to_rl(call_expr
, ret_info
->return_type
, argv
[0], &rl
);
839 ret_info
->return_range_list
= rl_union(ret_info
->return_range_list
, rl
);
843 struct range_list
*db_return_vals(struct expression
*expr
)
845 struct return_info ret_info
= {};
848 if (is_fake_call(expr
))
851 sm
= get_extra_sm_state(expr
);
853 return clone_rl(estate_rl(sm
->state
));
854 ret_info
.static_returns_call
= expr
;
855 ret_info
.return_type
= get_type(expr
);
856 if (!ret_info
.return_type
)
859 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
862 ret_info
.return_range_list
= NULL
;
863 if (inlinable(expr
->fn
)) {
864 mem_sql(db_return_callback
, &ret_info
,
865 "select distinct return from return_states where call_id = '%lu';",
866 (unsigned long)expr
);
868 run_sql(db_return_callback
, &ret_info
,
869 "select distinct return from return_states where %s;",
870 get_static_filter(expr
->fn
->symbol
));
872 return ret_info
.return_range_list
;
875 struct range_list
*db_return_vals_from_str(const char *fn_name
)
877 struct return_info ret_info
;
879 ret_info
.static_returns_call
= NULL
;
880 ret_info
.return_type
= &llong_ctype
;
881 ret_info
.return_range_list
= NULL
;
883 run_sql(db_return_callback
, &ret_info
,
884 "select distinct return from return_states where function = '%s';",
886 return ret_info
.return_range_list
;
890 * This is used when we have a function that takes a function pointer as a
891 * parameter. "frob(blah, blah, my_function);" We know that the return values
892 * from frob() come from my_funcion() so we want to find the possible returns
893 * of my_function(), but we don't know which arguments are passed to it.
896 struct range_list
*db_return_vals_no_args(struct expression
*expr
)
898 struct return_info ret_info
= {};
900 if (!expr
|| expr
->type
!= EXPR_SYMBOL
)
903 ret_info
.static_returns_call
= expr
;
904 ret_info
.return_type
= get_type(expr
);
905 ret_info
.return_type
= get_real_base_type(ret_info
.return_type
);
906 if (!ret_info
.return_type
)
909 run_sql(db_return_callback
, &ret_info
,
910 "select distinct return from return_states where %s;",
911 get_static_filter(expr
->symbol
));
913 return ret_info
.return_range_list
;
916 static void match_call_marker(struct expression
*expr
)
920 type
= get_type(expr
->fn
);
921 if (type
&& type
->type
== SYM_PTR
)
922 type
= get_real_base_type(type
);
925 * we just want to record something in the database so that if we have
926 * two calls like: frob(4); frob(some_unkown); then on the receiving
927 * side we know that sometimes frob is called with unknown parameters.
930 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", type_to_str(type
));
933 int is_recursive_member(const char *name
)
936 const char *p
, *next
;
939 p
= strchr(name
, '>');
944 next
= strchr(p
, '>');
950 if (size
>= sizeof(buf
))
952 memcpy(buf
, p
, size
);
954 if (strstr(next
, buf
))
960 char *sm_to_arg_name(struct expression
*expr
, struct sm_state
*sm
)
965 bool is_address
= false;
966 bool add_star
= false;
971 expr
= strip_expr(expr
);
975 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
976 expr
= strip_expr(expr
->unop
);
980 name
= expr_to_var_sym(expr
, &sym
);
988 if (sm_name
[0] == '*') {
994 if (strncmp(name
, sm_name
, len
) != 0)
996 if (sm_name
[len
] == '\0') {
997 snprintf(buf
, sizeof(buf
), "%s%s$",
998 add_star
? "*" : "", is_address
? "*" : "");
1000 if (sm_name
[len
] != '.' && sm_name
[len
] != '-')
1002 if (sm_name
[len
] == '-')
1004 // FIXME does is_address really imply that sm_name[len] == '-'
1005 snprintf(buf
, sizeof(buf
), "%s$->%s", add_star
? "*" : "",
1009 ret
= alloc_sname(buf
);
1015 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
,
1017 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
),
1020 struct sm_state
*sm
;
1021 const char *sm_name
;
1025 char printed_name
[256];
1028 struct symbol
*type
;
1030 expr
= strip_expr(expr
);
1033 type
= get_type(expr
);
1034 if (!new && type
&& type_bits(type
) < type_bits(&ulong_ctype
))
1037 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
1038 expr
= strip_expr(expr
->unop
);
1042 name
= expr_to_var_sym(expr
, &sym
);
1047 FOR_EACH_SM(__get_cur_stree(), sm
) {
1048 if (sm
->owner
!= owner
|| sm
->sym
!= sym
)
1052 if (sm_name
[0] == '*') {
1057 if (!add_star
&& strcmp(name
, sm_name
) == 0) {
1059 snprintf(printed_name
, sizeof(printed_name
), "*$");
1062 snprintf(printed_name
, sizeof(printed_name
), "$");
1066 } else if (add_star
&& strcmp(name
, sm_name
) == 0) {
1067 snprintf(printed_name
, sizeof(printed_name
), "%s*$",
1068 is_address
? "*" : "");
1069 } else if (strncmp(name
, sm_name
, len
) == 0) {
1070 if (sm_name
[len
] != '.' && sm_name
[len
] != '-')
1072 if (is_address
&& sm_name
[len
] == '.') {
1073 snprintf(printed_name
, sizeof(printed_name
),
1074 "%s$->%s", add_star
? "*" : "",
1076 } else if (is_address
&& sm_name
[len
] == '-') {
1077 snprintf(printed_name
, sizeof(printed_name
),
1078 "%s(*$)%s", add_star
? "*" : "",
1081 snprintf(printed_name
, sizeof(printed_name
),
1082 "%s$%s", add_star
? "*" : "",
1085 } else if (sm_name
[0] == '&' && strncmp(name
, sm_name
+ 1, len
) == 0) {
1086 if (sm_name
[len
+ 1] != '.' && sm_name
[len
+ 1] != '-')
1088 if (is_address
&& sm_name
[len
+ 1] == '.') {
1089 snprintf(printed_name
, sizeof(printed_name
),
1090 "&%s$->%s", add_star
? "*" : "",
1092 } else if (is_address
&& sm_name
[len
] == '-') {
1093 snprintf(printed_name
, sizeof(printed_name
),
1094 "&%s(*$)%s", add_star
? "*" : "",
1097 snprintf(printed_name
, sizeof(printed_name
),
1098 "&%s$%s", add_star
? "*" : "",
1104 if (is_recursive_member(printed_name
))
1106 callback(call
, param
, printed_name
, sm
);
1107 } END_FOR_EACH_SM(sm
);
1112 static void match_call_info(struct expression
*call
)
1114 struct member_info_callback
*cb
;
1115 struct expression
*arg
;
1118 FOR_EACH_PTR(member_callbacks
, cb
) {
1120 FOR_EACH_PTR(call
->args
, arg
) {
1122 print_struct_members(call
, arg
, i
, cb
->owner
, cb
->callback
, 0);
1123 } END_FOR_EACH_PTR(arg
);
1124 } END_FOR_EACH_PTR(cb
);
1127 static struct expression
*get_fake_variable(struct expression
*expr
)
1129 struct expression
*tmp
;
1131 tmp
= expr_get_fake_parent_expr(expr
);
1132 if (!tmp
|| tmp
->type
!= EXPR_ASSIGNMENT
)
1138 static void match_call_info_new(struct expression
*call
)
1140 struct member_info_callback
*cb
;
1141 struct expression
*arg
, *tmp
;
1144 if (!option_info
&& !__inline_call
&& !local_debug
)
1147 FOR_EACH_PTR(member_callbacks_new
, cb
) {
1149 FOR_EACH_PTR(call
->args
, arg
) {
1151 tmp
= get_fake_variable(arg
);
1154 __ignore_param_used
++;
1155 print_struct_members(call
, tmp
, i
, cb
->owner
, cb
->callback
, 1);
1156 __ignore_param_used
--;
1157 } END_FOR_EACH_PTR(arg
);
1158 } END_FOR_EACH_PTR(cb
);
1161 static int get_param(int param
, char **name
, struct symbol
**sym
)
1167 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
1169 * this is a temporary hack to work around a bug (I think in sparse?)
1170 * 2.6.37-rc1:fs/reiserfs/journal.o
1171 * If there is a function definition without parameter name found
1172 * after a function implementation then it causes a crash.
1176 if (arg
->ident
->name
< (char *)100)
1179 *name
= arg
->ident
->name
;
1184 } END_FOR_EACH_PTR(arg
);
1189 static int function_signature_matches(const char *sig
)
1193 my_sig
= function_signature();
1194 if (!sig
|| !my_sig
)
1195 return 1; /* default to matching */
1196 if (strcmp(my_sig
, sig
) == 0)
1201 static int caller_info_callback(void *_data
, int argc
, char **argv
, char **azColName
)
1203 struct select_caller_info_data
*data
= _data
;
1210 struct symbol
*sym
= NULL
;
1211 struct def_callback
*def_callback
;
1212 struct def_name_sym_callback
*ns_callback
;
1213 struct stree
*stree
;
1214 struct timeval cur_time
;
1223 gettimeofday(&cur_time
, NULL
);
1224 if (cur_time
.tv_sec
- data
->start_time
.tv_sec
> 10)
1227 func_id
= atoi(argv
[0]);
1229 type
= strtol(argv
[1], NULL
, 10);
1230 param
= strtol(argv
[2], NULL
, 10);
1236 if (data
->prev_func_id
== -1)
1237 data
->prev_func_id
= func_id
;
1238 if (func_id
!= data
->prev_func_id
) {
1239 stree
= __pop_fake_cur_stree();
1241 merge_stree(&data
->final_states
, stree
);
1243 __push_fake_cur_stree();
1245 data
->prev_func_id
= func_id
;
1251 if (type
== INTERNAL
&&
1252 !function_signature_matches(value
)) {
1257 if (param
>= 0 && !get_param(param
, &name
, &sym
))
1260 FOR_EACH_PTR(select_caller_info_callbacks
, def_callback
) {
1261 if (def_callback
->hook_type
== type
)
1262 def_callback
->callback(name
, sym
, key
, value
);
1263 } END_FOR_EACH_PTR(def_callback
);
1265 p
= strchr(key
, '$');
1267 snprintf(fullname
, sizeof(fullname
), "%.*s%s%s", (int)(p
- key
), key
, name
, p
+ 1);
1269 snprintf(fullname
, sizeof(fullname
), "%s", key
);
1271 FOR_EACH_PTR(select_caller_name_sym_callbacks
, ns_callback
) {
1272 if (ns_callback
->hook_type
== type
)
1273 ns_callback
->callback(fullname
, sym
, value
);
1274 } END_FOR_EACH_PTR(ns_callback
);
1279 static struct string_list
*ptr_names_done
;
1280 static struct string_list
*ptr_names
;
1282 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
1284 insert_string(&ptr_names
, alloc_string(argv
[0]));
1288 static char *get_next_ptr_name(void)
1292 FOR_EACH_PTR(ptr_names
, ptr
) {
1293 if (!insert_string(&ptr_names_done
, ptr
))
1296 } END_FOR_EACH_PTR(ptr
);
1300 static void get_ptr_names(const char *file
, const char *name
)
1302 char sql_filter
[1024];
1306 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
1309 snprintf(sql_filter
, 1024, "function = '%s';", name
);
1312 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
1314 run_sql(get_ptr_name
, NULL
,
1315 "select distinct ptr from function_ptr where %s",
1318 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
1319 if (before
== after
)
1322 while ((name
= get_next_ptr_name()))
1323 get_ptr_names(NULL
, name
);
1326 static void match_data_from_db(struct symbol
*sym
)
1328 struct select_caller_info_data data
= { .prev_func_id
= -1 };
1329 struct sm_state
*sm
;
1330 struct stree
*stree
;
1331 struct timeval end_time
;
1333 if (!sym
|| !sym
->ident
)
1337 gettimeofday(&data
.start_time
, NULL
);
1339 __push_fake_cur_stree();
1345 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1346 get_ptr_names(get_base_file(), sym
->ident
->name
);
1348 get_ptr_names(NULL
, sym
->ident
->name
);
1350 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
1351 __free_ptr_list((struct ptr_list
**)&ptr_names
);
1352 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
1353 __free_fake_cur_stree();
1357 sql_select_caller_info(&data
,
1358 "call_id, type, parameter, key, value",
1362 stree
= __pop_fake_cur_stree();
1364 merge_stree(&data
.final_states
, stree
);
1366 __push_fake_cur_stree();
1368 data
.prev_func_id
= -1;
1372 FOR_EACH_PTR(ptr_names
, ptr
) {
1373 run_sql(caller_info_callback
, &data
,
1374 "select call_id, type, parameter, key, value"
1375 " from common_caller_info where function = '%s' order by call_id",
1377 } END_FOR_EACH_PTR(ptr
);
1380 FOR_EACH_PTR(ptr_names
, ptr
) {
1382 } END_FOR_EACH_PTR(ptr
);
1383 goto free_ptr_names
;
1386 FOR_EACH_PTR(ptr_names
, ptr
) {
1387 run_sql(caller_info_callback
, &data
,
1388 "select call_id, type, parameter, key, value"
1389 " from caller_info where function = '%s' order by call_id",
1392 } END_FOR_EACH_PTR(ptr
);
1395 __free_ptr_list((struct ptr_list
**)&ptr_names
);
1396 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
1398 sql_select_caller_info(&data
,
1399 "call_id, type, parameter, key, value",
1403 stree
= __pop_fake_cur_stree();
1405 merge_stree(&data
.final_states
, stree
);
1408 gettimeofday(&end_time
, NULL
);
1409 if (end_time
.tv_sec
- data
.start_time
.tv_sec
<= 10) {
1410 FOR_EACH_SM(data
.final_states
, sm
) {
1412 } END_FOR_EACH_SM(sm
);
1415 free_stree(&data
.final_states
);
1418 static int return_implies_callbacks(void *_info
, int argc
, char **argv
, char **azColName
)
1420 struct implies_info
*info
= _info
;
1421 struct db_implies_callback
*cb
;
1422 struct expression
*arg
= NULL
;
1429 type
= atoi(argv
[1]);
1430 param
= atoi(argv
[2]);
1432 FOR_EACH_PTR(info
->cb_list
, cb
) {
1433 if (cb
->type
!= type
)
1436 arg
= get_argument_from_call_expr(info
->expr
->args
, param
);
1440 cb
->callback(info
->expr
, arg
, argv
[3], argv
[4]);
1441 } END_FOR_EACH_PTR(cb
);
1446 static int call_implies_callbacks(void *_info
, int argc
, char **argv
, char **azColName
)
1448 struct implies_info
*info
= _info
;
1449 struct db_implies_callback
*cb
;
1450 struct expression
*arg
;
1459 type
= atoi(argv
[1]);
1460 param
= atoi(argv
[2]);
1462 if (!get_param(param
, &name
, &sym
))
1464 arg
= symbol_expression(sym
);
1468 FOR_EACH_PTR(info
->cb_list
, cb
) {
1469 if (cb
->type
!= type
)
1471 cb
->callback(info
->expr
, arg
, argv
[3], argv
[4]);
1472 } END_FOR_EACH_PTR(cb
);
1477 static void match_return_implies(struct expression
*expr
)
1479 struct implies_info info
= {
1480 .type
= RETURN_IMPLIES
,
1481 .cb_list
= return_implies_cb_list
,
1484 if (expr
->fn
->type
!= EXPR_SYMBOL
||
1488 info
.sym
= expr
->fn
->symbol
;
1489 sql_select_implies("function, type, parameter, key, value", &info
,
1490 return_implies_callbacks
);
1493 static void match_call_implies(struct symbol
*sym
)
1495 struct implies_info info
= {
1496 .type
= CALL_IMPLIES
,
1497 .cb_list
= call_implies_cb_list
,
1500 if (!sym
|| !sym
->ident
)
1504 sql_select_implies("function, type, parameter, key, value", &info
,
1505 call_implies_callbacks
);
1508 static char *get_fn_param_str(struct expression
*expr
)
1510 struct expression
*tmp
;
1514 tmp
= get_assigned_expr(expr
);
1517 expr
= strip_expr(expr
);
1518 if (!expr
|| expr
->type
!= EXPR_CALL
)
1520 expr
= strip_expr(expr
->fn
);
1521 if (!expr
|| expr
->type
!= EXPR_SYMBOL
)
1523 param
= get_param_num(expr
);
1527 snprintf(buf
, sizeof(buf
), "[r $%d]", param
);
1528 return alloc_sname(buf
);
1531 static char *get_return_compare_is_param(struct expression
*expr
)
1538 param
= get_param_num(expr
);
1542 var
= expr_to_var(expr
);
1545 snprintf(buf
, sizeof(buf
), "%s orig", var
);
1546 comparison
= get_comparison_strings(var
, buf
);
1552 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(comparison
), param
);
1553 return alloc_sname(buf
);
1556 static char *get_return_compare_str(struct expression
*expr
)
1560 compare_str
= get_return_compare_is_param(expr
);
1564 compare_str
= expr_lte_to_param(expr
, -1);
1568 return expr_param_comparison(expr
, -1);
1571 static const char *get_return_ranges_str(struct expression
*expr
, struct range_list
**rl_p
)
1573 struct range_list
*rl
;
1574 char *return_ranges
;
1584 return alloc_sname("");
1586 if (get_implied_value(expr
, &sval
)) {
1587 sval
= sval_cast(cur_func_return_type(), sval
);
1588 *rl_p
= alloc_rl(sval
, sval
);
1589 return sval_to_str_or_err_ptr(sval
);
1592 fn_param_str
= get_fn_param_str(expr
);
1593 compare_str
= expr_equal_to_param(expr
, -1);
1594 math_str
= get_value_in_terms_of_parameter_math(expr
);
1596 if (get_implied_rl(expr
, &rl
) && !is_whole_rl(rl
)) {
1597 rl
= cast_rl(cur_func_return_type(), rl
);
1598 return_ranges
= show_rl(rl
);
1599 } else if (get_imaginary_absolute(expr
, &rl
)){
1600 rl
= cast_rl(cur_func_return_type(), rl
);
1601 return alloc_sname(show_rl(rl
));
1603 get_absolute_rl(expr
, &rl
);
1604 rl
= cast_rl(cur_func_return_type(), rl
);
1605 return_ranges
= show_rl(rl
);
1610 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, fn_param_str
);
1611 return alloc_sname(buf
);
1614 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1615 return alloc_sname(buf
);
1618 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1619 return alloc_sname(buf
);
1621 compare_str
= get_return_compare_str(expr
);
1623 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1624 return alloc_sname(buf
);
1627 return return_ranges
;
1630 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
1632 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", function_signature());
1635 static bool call_return_state_hooks_conditional(struct expression
*expr
)
1637 int final_pass_orig
= final_pass
;
1643 (expr
->type
!= EXPR_CONDITIONAL
&& expr
->type
!= EXPR_SELECT
))
1648 __push_fake_cur_stree();
1651 __split_whole_condition(expr
->conditional
);
1652 final_pass
= final_pass_orig
;
1654 call_return_state_hooks(expr
->cond_true
?: expr
->conditional
);
1656 __push_true_states();
1657 __use_false_states();
1659 call_return_state_hooks(expr
->cond_false
);
1661 __merge_true_states();
1662 __free_fake_cur_stree();
1668 static bool handle_forced_split(const char *return_ranges
, struct expression
*expr
)
1670 struct split_data
*data
= NULL
;
1671 struct expression
*compare
;
1672 struct range_list
*rl
;
1679 for (i
= 0; i
< split_count
; i
++) {
1680 if (strcmp(get_function(), forced_splits
[i
]->func
) == 0) {
1681 data
= forced_splits
[i
];
1688 // FIXME: this works for copy_to/from_user() because the only thing we
1689 // care about is zero/non-zero
1690 if (strcmp(data
->rl
, "0") != 0)
1693 compare
= compare_expression(expr
, SPECIAL_EQUAL
, zero_expr());
1696 if (get_implied_value(compare
, &sval
))
1699 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
1700 call_return_states_callbacks("0", expr
);
1704 undo
= assume(compare_expression(expr
, SPECIAL_NOTEQUAL
, zero_expr()));
1705 if (get_implied_rl(expr
, &rl
)) {
1706 math
= strchr(return_ranges
, '[');
1707 snprintf(buf
, sizeof(buf
), "%s%s", show_rl(rl
), math
?: "");
1709 snprintf(buf
, sizeof(buf
), "%s", return_ranges
);
1711 call_return_states_callbacks(buf
, expr
);
1718 static void call_return_states_callbacks(const char *return_ranges
, struct expression
*expr
)
1720 struct returned_state_callback
*cb
;
1722 return_ranges
= replace_return_ranges(return_ranges
);
1723 if (is_delete_return(return_ranges
))
1725 if (is_project_delete_return(expr
))
1727 if (handle_forced_split(return_ranges
, expr
))
1731 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1732 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1733 } END_FOR_EACH_PTR(cb
);
1736 static void call_return_state_hooks_compare(struct expression
*expr
)
1738 char *return_ranges
;
1739 int final_pass_orig
= final_pass
;
1740 sval_t sval
= { .type
= &int_ctype
};
1743 if (!get_implied_value(expr
, &ret
))
1746 __push_fake_cur_stree();
1749 __split_whole_condition(expr
);
1750 final_pass
= final_pass_orig
;
1752 if (ret
.value
!= 0) {
1753 return_ranges
= alloc_sname("1");
1755 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1757 call_return_states_callbacks(return_ranges
, expr
);
1760 __push_true_states();
1761 __use_false_states();
1763 if (ret
.value
!= 1) {
1764 return_ranges
= alloc_sname("0");
1766 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1768 call_return_states_callbacks(return_ranges
, expr
);
1771 __merge_true_states();
1772 __free_fake_cur_stree();
1775 static bool is_implies_function(struct expression
*expr
)
1777 struct range_list
*rl
;
1782 rl
= get_range_implications(get_function());
1786 sm_msg("%s: is implied", __func__
);
1790 static int ptr_in_list(struct sm_state
*sm
, struct state_list
*slist
)
1792 struct sm_state
*tmp
;
1794 FOR_EACH_PTR(slist
, tmp
) {
1795 if (strcmp(tmp
->state
->name
, sm
->state
->name
) == 0)
1797 } END_FOR_EACH_PTR(tmp
);
1802 static int split_possible_helper(struct sm_state
*sm
, struct expression
*expr
)
1804 struct range_list
*rl
;
1805 char *return_ranges
;
1806 struct sm_state
*tmp
;
1808 int nr_possible
, nr_states
;
1811 struct state_list
*already_handled
= NULL
;
1814 if (!sm
|| !sm
->merged
)
1817 if (too_many_possible(sm
) && !is_implies_function(expr
))
1820 /* bail if it gets too complicated */
1822 FOR_EACH_PTR(sm
->possible
, tmp
) {
1825 if (ptr_in_list(tmp
, already_handled
))
1827 add_ptr_list(&already_handled
, tmp
);
1829 } END_FOR_EACH_PTR(tmp
);
1830 free_slist(&already_handled
);
1831 nr_states
= get_db_state_count();
1832 if (nr_states
* nr_possible
>= 2000 && !is_implies_function(expr
))
1835 FOR_EACH_PTR(sm
->possible
, tmp
) {
1838 if (ptr_in_list(tmp
, already_handled
))
1840 add_ptr_list(&already_handled
, tmp
);
1843 __push_fake_cur_stree();
1845 overwrite_states_using_pool(sm
, tmp
);
1847 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
1848 return_ranges
= show_rl(rl
);
1849 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(clone_rl(rl
)));
1850 compare_str
= get_return_compare_str(expr
);
1851 /* ignore obvious stuff like 0 <= param */
1852 /* Is this worthile when we have PARAM_COMPARE? */
1854 strncmp(compare_str
, "[=", 2) != 0 &&
1855 rl_to_sval(rl
, &sval
))
1858 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1859 return_ranges
= alloc_sname(buf
);
1862 call_return_states_callbacks(return_ranges
, expr
);
1864 __free_fake_cur_stree();
1865 } END_FOR_EACH_PTR(tmp
);
1867 free_slist(&already_handled
);
1872 static int call_return_state_hooks_split_possible(struct expression
*expr
)
1874 struct expression
*fake
;
1875 struct sm_state
*sm
;
1880 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1882 fake
= expr_get_fake_parent_expr(expr
);
1883 if (!fake
|| fake
->type
!= EXPR_ASSIGNMENT
|| fake
->op
!= '=')
1886 sm
= get_sm_state_expr(SMATCH_EXTRA
, fake
);
1888 return split_possible_helper(sm
, expr
);
1891 static bool has_possible_negative(struct sm_state
*sm
)
1893 struct sm_state
*tmp
;
1895 if (!type_signed(estate_type(sm
->state
)))
1898 FOR_EACH_PTR(sm
->possible
, tmp
) {
1899 if (!estate_rl(tmp
->state
))
1901 if (sval_is_negative(estate_min(tmp
->state
)) &&
1902 sval_is_negative(estate_max(tmp
->state
)))
1904 } END_FOR_EACH_PTR(tmp
);
1909 static bool has_separate_zero_null(struct sm_state
*sm
)
1911 struct sm_state
*tmp
;
1914 FOR_EACH_PTR(sm
->possible
, tmp
) {
1915 if (!estate_get_single_value(tmp
->state
, &sval
))
1917 if (sval
.value
== 0)
1919 } END_FOR_EACH_PTR(tmp
);
1924 static int split_positive_from_negative(struct expression
*expr
)
1926 struct sm_state
*sm
;
1927 struct range_list
*rl
;
1928 const char *return_ranges
;
1929 struct range_list
*ret_rl
;
1933 /* We're going to print the states 3 times */
1934 if (get_db_state_count() > 10000 / 3)
1937 if (!get_implied_rl(expr
, &rl
) || !rl
)
1939 /* Forget about INT_MAX and larger */
1940 if (rl_max(rl
).value
<= 0)
1942 if (!sval_is_negative(rl_min(rl
)))
1945 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1948 if (!has_possible_negative(sm
))
1950 separate_zero
= has_separate_zero_null(sm
);
1952 if (!assume(compare_expression(expr
, separate_zero
? '>' : SPECIAL_GTE
, zero_expr())))
1955 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1956 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1957 call_return_states_callbacks(return_ranges
, expr
);
1961 if (separate_zero
) {
1962 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
1964 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1965 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1966 call_return_states_callbacks(return_ranges
, expr
);
1972 undo
= assume(compare_expression(expr
, '<', zero_expr()));
1974 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1975 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1976 call_return_states_callbacks(return_ranges
, expr
);
1984 static int call_return_state_hooks_split_null_non_null_zero(struct expression
*expr
)
1986 struct range_list
*rl
;
1987 struct range_list
*nonnull_rl
;
1989 struct range_list
*null_rl
= NULL
;
1990 char *return_ranges
;
1991 struct sm_state
*sm
;
1992 struct smatch_state
*state
;
1994 int final_pass_orig
= final_pass
;
1996 if (!expr
|| expr_equal_to_param(expr
, -1))
1998 if (expr
->type
== EXPR_CALL
)
2001 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
2004 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
2007 if (!estate_rl(state
))
2009 if (estate_min(state
).value
== 0 && estate_max(state
).value
== 0)
2011 if (has_possible_negative(sm
))
2013 if (!has_separate_zero_null(sm
))
2016 nr_states
= get_db_state_count();
2017 if (option_info
&& nr_states
>= 1500)
2020 rl
= estate_rl(state
);
2022 __push_fake_cur_stree();
2025 __split_whole_condition(expr
);
2026 final_pass
= final_pass_orig
;
2028 nonnull_rl
= rl_filter(rl
, rl_zero());
2029 return_ranges
= show_rl(nonnull_rl
);
2030 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonnull_rl
));
2032 call_return_states_callbacks(return_ranges
, expr
);
2034 __push_true_states();
2035 __use_false_states();
2037 return_ranges
= alloc_sname("0");
2038 null_sval
= sval_type_val(rl_type(rl
), 0);
2039 add_range(&null_rl
, null_sval
, null_sval
);
2040 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(null_rl
));
2041 call_return_states_callbacks(return_ranges
, expr
);
2043 __merge_true_states();
2044 __free_fake_cur_stree();
2049 static bool is_kernel_success_fail(struct sm_state
*sm
)
2051 struct sm_state
*tmp
;
2052 struct range_list
*rl
;
2053 bool has_zero
= false;
2054 bool has_neg
= false;
2056 if (!type_signed(estate_type(sm
->state
)))
2059 FOR_EACH_PTR(sm
->possible
, tmp
) {
2060 rl
= estate_rl(tmp
->state
);
2063 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 0) {
2068 if (rl_min(rl
).value
>= -4095 && rl_max(rl
).value
< 0)
2070 if (strcmp(tmp
->state
->name
, "s32min-(-1)") == 0)
2072 if (strcmp(tmp
->state
->name
, "s32min-(-1),1-s32max") == 0)
2075 } END_FOR_EACH_PTR(tmp
);
2077 return has_zero
&& has_neg
;
2080 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
2082 struct sm_state
*sm
;
2083 struct range_list
*rl
;
2084 struct range_list
*nonzero_rl
;
2086 struct range_list
*zero_rl
= NULL
;
2088 char *return_ranges
;
2089 int final_pass_orig
= final_pass
;
2091 if (option_project
!= PROJ_KERNEL
)
2094 nr_states
= get_db_state_count();
2095 if (nr_states
> 2000)
2098 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
2101 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
2103 if (!is_kernel_success_fail(sm
))
2106 rl
= estate_rl(sm
->state
);
2110 __push_fake_cur_stree();
2113 __split_whole_condition(expr
);
2114 final_pass
= final_pass_orig
;
2116 nonzero_rl
= rl_filter(rl
, rl_zero());
2117 nonzero_rl
= cast_rl(cur_func_return_type(), nonzero_rl
);
2118 return_ranges
= show_rl(nonzero_rl
);
2119 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonzero_rl
));
2121 call_return_states_callbacks(return_ranges
, expr
);
2123 __push_true_states();
2124 __use_false_states();
2126 return_ranges
= alloc_sname("0");
2127 zero_sval
= sval_type_val(rl_type(rl
), 0);
2128 add_range(&zero_rl
, zero_sval
, zero_sval
);
2129 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(zero_rl
));
2130 call_return_states_callbacks(return_ranges
, expr
);
2132 __merge_true_states();
2133 __free_fake_cur_stree();
2138 static int is_boolean(struct expression
*expr
)
2140 struct range_list
*rl
;
2142 if (!get_implied_rl(expr
, &rl
))
2144 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
2149 static int splitable_function_call(struct expression
*expr
)
2151 struct sm_state
*sm
;
2153 if (!expr
|| expr
->type
!= EXPR_CALL
)
2155 sm
= get_extra_sm_state(expr
);
2156 return split_possible_helper(sm
, expr
);
2159 static struct sm_state
*find_bool_param(void)
2161 struct stree
*start_states
;
2163 struct sm_state
*sm
, *tmp
;
2166 start_states
= get_start_states();
2168 FOR_EACH_PTR_REVERSE(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
2171 sm
= get_sm_state_stree(start_states
, SMATCH_EXTRA
, arg
->ident
->name
, arg
);
2174 if (rl_min(estate_rl(sm
->state
)).value
!= 0 ||
2175 rl_max(estate_rl(sm
->state
)).value
!= 1)
2178 } END_FOR_EACH_PTR_REVERSE(arg
);
2184 * Check if it's splitable. If not, then splitting it up is likely not
2185 * useful for the callers.
2187 FOR_EACH_PTR(sm
->possible
, tmp
) {
2190 if (!estate_get_single_value(tmp
->state
, &sval
))
2192 } END_FOR_EACH_PTR(tmp
);
2197 static int split_on_bool_sm(struct sm_state
*sm
, struct expression
*expr
)
2199 struct range_list
*ret_rl
;
2200 const char *return_ranges
;
2201 struct sm_state
*tmp
;
2203 struct state_list
*already_handled
= NULL
;
2205 if (!sm
|| !sm
->merged
)
2208 if (too_many_possible(sm
))
2211 FOR_EACH_PTR(sm
->possible
, tmp
) {
2214 if (ptr_in_list(tmp
, already_handled
))
2216 add_ptr_list(&already_handled
, tmp
);
2219 __push_fake_cur_stree();
2221 overwrite_states_using_pool(sm
, tmp
);
2223 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
2224 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
2225 call_return_states_callbacks(return_ranges
, expr
);
2227 __free_fake_cur_stree();
2228 } END_FOR_EACH_PTR(tmp
);
2230 free_slist(&already_handled
);
2235 static int split_by_bool_param(struct expression
*expr
)
2237 struct sm_state
*start_sm
, *sm
;
2240 start_sm
= find_bool_param();
2243 sm
= get_sm_state(SMATCH_EXTRA
, start_sm
->name
, start_sm
->sym
);
2244 if (!sm
|| estate_get_single_value(sm
->state
, &sval
))
2247 if (get_db_state_count() * 2 >= 2000)
2250 return split_on_bool_sm(sm
, expr
);
2253 static int split_by_null_nonnull_param(struct expression
*expr
)
2256 struct sm_state
*sm
;
2259 /* function must only take one pointer */
2260 if (ptr_list_size((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
) != 1)
2262 arg
= first_ptr_list((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
);
2265 if (get_real_base_type(arg
)->type
!= SYM_PTR
)
2268 if (param_was_set_var_sym(arg
->ident
->name
, arg
))
2270 sm
= get_sm_state(SMATCH_EXTRA
, arg
->ident
->name
, arg
);
2274 if (!has_separate_zero_null(sm
))
2277 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
2278 if (get_db_state_count() * nr_possible
>= 2000)
2281 return split_on_bool_sm(sm
, expr
);
2284 struct expression
*strip_expr_statement(struct expression
*expr
)
2286 struct expression
*orig
= expr
;
2287 struct statement
*stmt
, *last_stmt
;
2291 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '(')
2293 if (expr
->type
!= EXPR_STATEMENT
)
2295 stmt
= expr
->statement
;
2296 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
2299 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
2300 if (!last_stmt
|| last_stmt
->type
== STMT_LABEL
)
2301 last_stmt
= last_stmt
->label_statement
;
2302 if (!last_stmt
|| last_stmt
->type
!= STMT_EXPRESSION
)
2304 return strip_expr(last_stmt
->expression
);
2307 static bool is_kernel_error_path(struct expression
*expr
)
2309 struct range_list
*rl
;
2312 * Splitting up returns requires resources. It also requires resources
2313 * for the caller. It doesn't seem worth it to split anything up.
2315 if (!get_implied_rl(expr
, &rl
))
2317 if (rl_type(rl
) != &int_ctype
)
2319 if (rl_min(rl
).value
>= -4095 &&
2320 rl_max(rl
).value
< 0)
2325 static void call_return_state_hooks(struct expression
*expr
)
2327 struct range_list
*ret_rl
;
2328 const char *return_ranges
;
2332 if (__path_is_null())
2335 expr
= strip_expr(expr
);
2336 expr
= strip_expr_statement(expr
);
2338 if (is_impossible_path())
2341 if (expr
&& (expr
->type
== EXPR_COMPARE
||
2342 !get_implied_value(expr
, &sval
)) &&
2343 (is_condition(expr
) || is_boolean(expr
))) {
2344 call_return_state_hooks_compare(expr
);
2346 } else if (call_return_state_hooks_conditional(expr
)) {
2348 } else if (is_kernel_error_path(expr
)) {
2350 } else if (call_return_state_hooks_split_possible(expr
)) {
2352 } else if (split_positive_from_negative(expr
)) {
2354 } else if (call_return_state_hooks_split_null_non_null_zero(expr
)) {
2356 } else if (call_return_state_hooks_split_success_fail(expr
)) {
2358 } else if (splitable_function_call(expr
)) {
2360 } else if (split_by_bool_param(expr
)) {
2361 } else if (split_by_null_nonnull_param(expr
)) {
2366 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
2367 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
2369 nr_states
= get_db_state_count();
2370 if (nr_states
>= 10000) {
2372 match_return_info(return_id
, (char *)return_ranges
, expr
);
2373 print_limited_param_set(return_id
, (char *)return_ranges
, expr
);
2374 mark_all_params_untracked(return_id
, (char *)return_ranges
, expr
);
2377 call_return_states_callbacks(return_ranges
, expr
);
2380 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
2382 struct returned_member_callback
*cb
;
2383 struct sm_state
*sm
;
2384 struct symbol
*type
;
2386 char member_name
[256];
2389 type
= get_type(expr
);
2390 if (!type
|| type
->type
!= SYM_PTR
)
2392 name
= expr_to_var(expr
);
2397 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
2398 FOR_EACH_MY_SM(cb
->owner
, __get_cur_stree(), sm
) {
2399 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
2400 strcpy(member_name
, "*$");
2401 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
2404 if (strncmp(sm
->name
, name
, len
) != 0)
2406 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
2408 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
2409 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
2410 } END_FOR_EACH_SM(sm
);
2411 } END_FOR_EACH_PTR(cb
);
2416 static void print_return_struct_info(int return_id
, char *return_ranges
,
2417 struct expression
*expr
,
2419 struct return_info_callback
*cb
)
2421 struct sm_state
*sm
;
2422 const char *printed_name
;
2425 FOR_EACH_MY_SM(cb
->owner
, __get_cur_stree(), sm
) {
2426 if (sm
->sym
&& sm
->sym
== sym
) {
2429 param
= get_param_num_from_sym(sm
->sym
);
2434 printed_name
= get_param_name(sm
);
2438 cb
->callback(return_id
, return_ranges
, expr
, param
, printed_name
, sm
);
2439 } END_FOR_EACH_SM(sm
);
2442 static void print_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
2444 struct return_info_callback
*cb
;
2445 struct expression
*tmp
;
2448 if (!option_info
&& !__inline_fn
&&
2449 !local_debug
&& !option_debug
)
2452 tmp
= get_fake_variable(expr
);
2455 sym
= expr_to_sym(expr
);
2457 FOR_EACH_PTR(return_callbacks
, cb
) {
2458 __ignore_param_used
++;
2459 print_return_struct_info(return_id
, return_ranges
, expr
, sym
, cb
);
2460 __ignore_param_used
--;
2461 } END_FOR_EACH_PTR(cb
);
2464 static void reset_memdb(struct symbol
*sym
)
2466 mem_sql(NULL
, NULL
, "delete from caller_info;");
2467 mem_sql(NULL
, NULL
, "delete from return_states;");
2468 mem_sql(NULL
, NULL
, "delete from call_implies;");
2469 mem_sql(NULL
, NULL
, "delete from return_implies;");
2472 static void match_end_func_info(struct symbol
*sym
)
2474 if (__path_is_null())
2476 call_return_state_hooks(NULL
);
2479 static void match_after_func(struct symbol
*sym
)
2485 static void init_memdb(void)
2489 const char *schema_files
[] = {
2491 "db/caller_info.schema",
2492 "db/common_caller_info.schema",
2493 "db/return_states.schema",
2494 "db/function_type_size.schema",
2495 "db/type_size.schema",
2496 "db/function_type_info.schema",
2497 "db/type_info.schema",
2498 "db/call_implies.schema",
2499 "db/return_implies.schema",
2500 "db/function_ptr.schema",
2501 "db/local_values.schema",
2502 "db/function_type_value.schema",
2503 "db/type_value.schema",
2504 "db/function_type.schema",
2505 "db/data_info.schema",
2506 "db/parameter_name.schema",
2507 "db/constraints.schema",
2508 "db/constraints_required.schema",
2509 "db/fn_ptr_data_link.schema",
2510 "db/fn_data_link.schema",
2511 "db/mtag_about.schema",
2512 "db/mtag_info.schema",
2513 "db/mtag_map.schema",
2514 "db/mtag_data.schema",
2515 "db/mtag_alias.schema",
2517 static char buf
[4096];
2522 rc
= sqlite3_open(":memory:", &mem_db
);
2523 if (rc
!= SQLITE_OK
) {
2524 sm_ierror("starting In-Memory database.");
2528 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
2529 fd
= open_schema_file(schema_files
[i
]);
2532 ret
= read(fd
, buf
, sizeof(buf
));
2534 sm_ierror("failed to read: %s", schema_files
[i
]);
2538 if (ret
== sizeof(buf
)) {
2539 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2540 schema_files
[i
], sizeof(buf
));
2544 rc
= sqlite3_exec(mem_db
, buf
, NULL
, NULL
, &err
);
2545 if (rc
!= SQLITE_OK
) {
2546 sm_ierror("SQL error #2: %s", err
);
2547 sm_ierror("%s", buf
);
2552 static void init_cachedb(void)
2556 const char *schema_files
[] = {
2557 "db/call_implies.schema",
2558 "db/return_implies.schema",
2559 "db/type_info.schema",
2560 "db/mtag_about.schema",
2561 "db/mtag_data.schema",
2562 "db/mtag_info.schema",
2563 "db/sink_info.schema",
2565 static char buf
[4096];
2570 rc
= sqlite3_open(":memory:", &cache_db
);
2571 if (rc
!= SQLITE_OK
) {
2572 sm_ierror("starting In-Memory database.");
2576 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
2577 fd
= open_schema_file(schema_files
[i
]);
2580 ret
= read(fd
, buf
, sizeof(buf
));
2582 sm_ierror("failed to read: %s", schema_files
[i
]);
2586 if (ret
== sizeof(buf
)) {
2587 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2588 schema_files
[i
], sizeof(buf
));
2592 rc
= sqlite3_exec(cache_db
, buf
, NULL
, NULL
, &err
);
2593 if (rc
!= SQLITE_OK
) {
2594 sm_ierror("SQL error #2: %s", err
);
2595 sm_ierror("%s", buf
);
2600 static int save_cache_data(void *_table
, int argc
, char **argv
, char **azColName
)
2602 static char buf
[4096];
2605 char *table
= _table
;
2609 p
+= snprintf(p
, 4096 - (p
- buf
), "insert or ignore into %s values (", table
);
2610 for (i
= 0; i
< argc
; i
++) {
2612 p
+= snprintf(p
, 4096 - (p
- buf
), ", ");
2613 sqlite3_snprintf(sizeof(tmp
), tmp
, "%q", escape_newlines(argv
[i
]));
2614 p
+= snprintf(p
, 4096 - (p
- buf
), "'%s'", tmp
);
2617 p
+= snprintf(p
, 4096 - (p
- buf
), ");");
2621 sm_msg("SQL: %s", buf
);
2625 static void dump_cache(struct symbol_list
*sym_list
)
2627 const char *cache_tables
[] = {
2628 "type_info", "return_implies", "call_implies", "mtag_data",
2629 "mtag_info", "mtag_about", "sink_info",
2637 for (i
= 0; i
< ARRAY_SIZE(cache_tables
); i
++) {
2638 snprintf(buf
, sizeof(buf
), "select * from %s;", cache_tables
[i
]);
2639 cache_sql(&save_cache_data
, (char *)cache_tables
[i
], buf
);
2643 void open_smatch_db(char *db_file
)
2650 use_states
= malloc(num_checks
+ 1);
2651 memset(use_states
, 0xff, num_checks
+ 1);
2656 rc
= sqlite3_open_v2(db_file
, &smatch_db
, SQLITE_OPEN_READONLY
, NULL
);
2657 if (rc
!= SQLITE_OK
) {
2662 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES
);
2666 static char *get_next_string(char **str
)
2668 static char string
[256];
2677 while (*p
!= '\0' && *p
!= '\n') {
2678 if (*p
== '\\' && *(p
+ 1) == ' ') {
2688 if (len
>= sizeof(string
)) {
2689 memcpy(string
, start
, sizeof(string
));
2690 string
[sizeof(string
) - 1] = '\0';
2691 sm_ierror("return_fix: '%s' too long", string
);
2695 memcpy(string
, start
, len
);
2697 for (i
= 0; i
< sizeof(string
) - 1; i
++) {
2698 if (string
[i
] == '\\' && string
[i
+ 1] == ' ') {
2699 for (j
= i
; string
[j
] != '\0'; j
++)
2700 string
[j
] = string
[j
+ 1];
2709 static void register_return_deletes(void)
2711 char *func
, *ret_str
;
2717 snprintf(filename
, 256, "db/%s.delete.return_states", option_project_str
);
2718 fd
= open_schema_file(filename
);
2721 ret
= read(fd
, buf
, sizeof(buf
));
2725 if (ret
== sizeof(buf
)) {
2726 sm_ierror("file too large: %s (limit %zd bytes)",
2727 filename
, sizeof(buf
));
2734 get_next_string(&p
);
2737 if (delete_count
== 0)
2739 if (delete_count
% 2 != 0) {
2740 printf("error parsing '%s' delete_count=%d\n", filename
, delete_count
);
2744 delete_table
= malloc(delete_count
* sizeof(char *));
2749 func
= alloc_string(get_next_string(&p
));
2750 ret_str
= alloc_string(get_next_string(&p
));
2752 delete_table
[i
++] = func
;
2753 delete_table
[i
++] = ret_str
;
2757 static void register_return_replacements(void)
2759 char *func
, *orig
, *new;
2765 snprintf(filename
, 256, "db/%s.return_fixes", option_project_str
);
2766 fd
= open_schema_file(filename
);
2769 ret
= read(fd
, buf
, sizeof(buf
));
2773 if (ret
== sizeof(buf
)) {
2774 sm_ierror("file too large: %s (limit %zd bytes)",
2775 filename
, sizeof(buf
));
2782 get_next_string(&p
);
2785 if (replace_count
== 0 || replace_count
% 3 != 0) {
2789 replace_table
= malloc(replace_count
* sizeof(char *));
2794 func
= alloc_string(get_next_string(&p
));
2795 orig
= alloc_string(get_next_string(&p
));
2796 new = alloc_string(get_next_string(&p
));
2798 replace_table
[i
++] = func
;
2799 replace_table
[i
++] = orig
;
2800 replace_table
[i
++] = new;
2804 static void register_forced_return_splits(void)
2806 int struct_members
= sizeof(struct split_data
) / sizeof(char *);
2812 snprintf(filename
, 256, "db/%s.forced_return_splits", option_project_str
);
2813 fd
= open_schema_file(filename
);
2816 ret
= read(fd
, buf
, sizeof(buf
));
2820 if (ret
== sizeof(buf
)) {
2821 sm_ierror("file too large: %s (limit %zd bytes)",
2822 filename
, sizeof(buf
));
2829 get_next_string(&p
);
2832 if (split_count
== 0)
2834 if (split_count
% struct_members
!= 0) {
2835 printf("error parsing '%s' split_count=%d\n", filename
, split_count
);
2839 split_count
/= struct_members
;
2840 forced_splits
= malloc(split_count
* sizeof(void *));
2845 struct split_data
*split
= malloc(sizeof(*split
));
2847 split
->func
= alloc_string(get_next_string(&p
));
2848 split
->rl
= alloc_string(get_next_string(&p
));
2849 forced_splits
[i
++] = split
;
2853 void register_definition_db_callbacks(int id
)
2857 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2858 add_hook(&match_call_info_new
, FUNCTION_CALL_HOOK
);
2859 add_split_return_callback(match_return_info
);
2860 add_split_return_callback(print_returned_struct_members
);
2861 add_split_return_callback(print_return_info
);
2862 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
2863 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
2864 add_hook(&match_after_func
, AFTER_FUNC_HOOK
);
2866 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
2867 add_hook(&match_call_implies
, FUNC_DEF_HOOK
);
2868 add_hook(&match_return_implies
, CALL_HOOK_AFTER_INLINE
);
2870 common_funcs
= load_strings_from_file(option_project_str
, "common_functions");
2871 register_return_deletes();
2872 register_return_replacements();
2873 register_forced_return_splits();
2875 add_hook(&dump_cache
, END_FILE_HOOK
);
2878 void register_db_call_marker(int id
)
2880 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
2883 char *get_data_info_name(struct expression
*expr
)
2890 expr
= strip_expr(expr
);
2891 name
= get_member_name(expr
);
2894 name
= expr_to_var_sym(expr
, &sym
);
2897 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
2899 if (sym
->ctype
.modifiers
& MOD_STATIC
)
2900 snprintf(buf
, sizeof(buf
), "static %s", name
);
2902 snprintf(buf
, sizeof(buf
), "global %s", name
);
2903 ret
= alloc_sname(buf
);