db: add a new callback for inserting stuff into caller_info
[smatch.git] / smatch_db.c
blob40dc17059e0378271d5eb080c800055d89fe6e15
1 /*
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
18 #include <string.h>
19 #include <errno.h>
20 #include <unistd.h>
21 #include <ctype.h>
22 #include "smatch.h"
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;
30 int debug_db;
32 static int return_id;
34 static void call_return_state_hooks(struct expression *expr);
36 #define SQLITE_CACHE_PAGES 1000
38 struct def_callback {
39 int hook_type;
40 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
42 ALLOCATOR(def_callback, "definition db hook callbacks");
43 DECLARE_PTR_LIST(callback_list, struct def_callback);
44 static struct callback_list *select_caller_info_callbacks;
46 struct member_info_callback {
47 int owner;
48 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
50 ALLOCATOR(member_info_callback, "caller_info callbacks");
51 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
52 static struct member_info_cb_list *member_callbacks;
53 static struct member_info_cb_list *member_callbacks_new;
55 struct returned_state_callback {
56 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
58 ALLOCATOR(returned_state_callback, "returned state callbacks");
59 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
60 static struct returned_state_cb_list *returned_state_callbacks;
62 struct returned_member_callback {
63 int owner;
64 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
66 ALLOCATOR(returned_member_callback, "returned member callbacks");
67 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
68 static struct returned_member_cb_list *returned_member_callbacks;
70 struct db_implies_callback {
71 int type;
72 void (*callback)(struct expression *call, struct expression *arg, char *key, char *value);
74 ALLOCATOR(db_implies_callback, "return_implies callbacks");
75 DECLARE_PTR_LIST(db_implies_cb_list, struct db_implies_callback);
76 static struct db_implies_cb_list *return_implies_cb_list;
77 static struct db_implies_cb_list *call_implies_cb_list;
79 /* silently truncates if needed. */
80 char *escape_newlines(const char *str)
82 char buf[1024] = "";
83 bool found = false;
84 int i, j;
86 for (i = 0, j = 0; str[i] != '\0' && j != sizeof(buf); i++, j++) {
87 if (str[i] != '\r' && str[i] != '\n') {
88 buf[j] = str[i];
89 continue;
92 found = true;
93 buf[j++] = '\\';
94 if (j == sizeof(buf))
95 break;
96 buf[j] = 'n';
99 if (!found)
100 return alloc_sname(str);
102 if (j == sizeof(buf))
103 buf[j - 1] = '\0';
104 return alloc_sname(buf);
107 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
109 int i;
111 for (i = 0; i < argc; i++) {
112 if (i != 0)
113 sm_printf(", ");
114 sm_printf("%s", argv[i]);
116 sm_printf("\n");
117 return 0;
120 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql)
122 char *err = NULL;
123 int rc;
125 if (!db)
126 return;
128 if (option_debug || debug_db) {
129 sm_msg("%s", sql);
130 if (strncasecmp(sql, "select", strlen("select")) == 0)
131 sqlite3_exec(db, sql, print_sql_output, NULL, NULL);
134 rc = sqlite3_exec(db, sql, callback, data, &err);
135 if (rc != SQLITE_OK && !parse_error) {
136 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err);
137 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql);
138 parse_error = 1;
142 static int replace_count;
143 static char **replace_table;
144 static const char *replace_return_ranges(const char *return_ranges)
146 int i;
148 if (!get_function()) {
149 /* I have no idea why EXPORT_SYMBOL() is here */
150 return return_ranges;
152 for (i = 0; i < replace_count; i += 3) {
153 if (strcmp(replace_table[i + 0], get_function()) == 0) {
154 if (strcmp(replace_table[i + 1], return_ranges) == 0)
155 return replace_table[i + 2];
158 return return_ranges;
162 static char *use_states;
163 static int get_db_state_count(void)
165 struct sm_state *sm;
166 int count = 0;
168 FOR_EACH_SM(__get_cur_stree(), sm) {
169 if (sm->owner == USHRT_MAX)
170 continue;
171 if (use_states[sm->owner])
172 count++;
173 } END_FOR_EACH_SM(sm);
174 return count;
177 void db_ignore_states(int id)
179 use_states[id] = 0;
182 void sql_insert_return_states(int return_id, const char *return_ranges,
183 int type, int param, const char *key, const char *value)
185 if (key && strlen(key) >= 80)
186 return;
187 return_ranges = replace_return_ranges(return_ranges);
188 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
189 get_base_file(), get_function(), (unsigned long)__inline_fn,
190 return_id, return_ranges, fn_static(), type, param, key, value);
193 static struct string_list *common_funcs;
194 static int is_common_function(const char *fn)
196 char *tmp;
198 if (!fn)
199 return 0;
201 if (strncmp(fn, "__builtin_", 10) == 0)
202 return 1;
204 FOR_EACH_PTR(common_funcs, tmp) {
205 if (strcmp(tmp, fn) == 0)
206 return 1;
207 } END_FOR_EACH_PTR(tmp);
209 return 0;
212 static char *function_signature(void)
214 return type_to_str(get_real_base_type(cur_func_sym));
217 void sql_insert_caller_info(struct expression *call, int type,
218 int param, const char *key, const char *value)
220 FILE *tmp_fd = sm_outfd;
221 char *fn;
223 if (!option_info && !__inline_call)
224 return;
226 if (key && strlen(key) >= 80)
227 return;
229 fn = get_fnptr_name(call->fn);
230 if (!fn)
231 return;
233 if (__inline_call) {
234 mem_sql(NULL, NULL,
235 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
236 get_base_file(), get_function(), fn, (unsigned long)call,
237 is_static(call->fn), type, param, key, value);
240 if (!option_info)
241 return;
243 if (strncmp(fn, "__builtin_", 10) == 0)
244 return;
245 if (type != INTERNAL && is_common_function(fn))
246 return;
248 sm_outfd = caller_info_fd;
249 sm_msg("SQL_caller_info: insert into caller_info values ("
250 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
251 get_base_file(), get_function(), fn, is_static(call->fn),
252 type, param, key, value);
253 sm_outfd = tmp_fd;
255 free_string(fn);
258 void sql_insert_function_ptr(const char *fn, const char *struct_name)
260 sql_insert_or_ignore(function_ptr, "'%s', '%s', '%s', 0",
261 get_base_file(), fn, struct_name);
264 void sql_insert_return_implies(int type, int param, const char *key, const char *value)
266 sql_insert_or_ignore(return_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
267 get_base_file(), get_function(), (unsigned long)__inline_fn,
268 fn_static(), type, param, key, value);
271 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
273 sql_insert_or_ignore(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
274 get_base_file(), get_function(), (unsigned long)__inline_fn,
275 fn_static(), type, param, key, value);
278 void sql_insert_function_type_size(const char *member, const char *ranges)
280 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
283 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value)
285 sql_insert(function_type_info, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type, struct_type, member, value);
288 void sql_insert_type_info(int type, const char *member, const char *value)
290 sql_insert_cache(type_info, "'%s', %d, '%s', '%s'", get_base_file(), type, member, value);
293 void sql_insert_local_values(const char *name, const char *value)
295 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
298 void sql_insert_function_type_value(const char *type, const char *value)
300 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
303 void sql_insert_function_type(int param, const char *value)
305 sql_insert(function_type, "'%s', '%s', %d, %d, '%s'",
306 get_base_file(), get_function(), fn_static(), param, value);
309 void sql_insert_parameter_name(int param, const char *value)
311 sql_insert(parameter_name, "'%s', '%s', %d, %d, '%s'",
312 get_base_file(), get_function(), fn_static(), param, value);
315 void sql_insert_data_info(struct expression *data, int type, const char *value)
317 char *data_name;
319 data_name = get_data_info_name(data);
320 if (!data_name)
321 return;
322 sql_insert(data_info, "'%s', '%s', %d, '%s'",
323 is_static(data) ? get_base_file() : "extern",
324 data_name, type, value);
327 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
329 sql_insert(data_info, "'%s', '%s', %d, '%s'",
330 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
331 var, type, value);
334 void sql_save_constraint(const char *con)
336 if (!option_info)
337 return;
339 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con));
342 void sql_save_constraint_required(const char *data, int op, const char *limit)
344 sql_insert_or_ignore(constraints_required, "'%s', '%s', '%s'", data, show_special(op), limit);
347 void sql_copy_constraint_required(const char *new_limit, const char *old_limit)
349 if (!option_info)
350 return;
352 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
353 "select constraints_required.data, constraints_required.op, '%s' from "
354 "constraints_required where bound = '%s';", new_limit, old_limit);
357 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data)
359 sql_insert_or_ignore(fn_ptr_data_link, "'%s', '%s'", ptr, data);
362 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value)
364 if (fn->type != EXPR_SYMBOL || !fn->symbol->ident)
365 return;
367 sql_insert(fn_data_link, "'%s', '%s', %d, %d, %d, '%s', '%s'",
368 (fn->symbol->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
369 fn->symbol->ident->name,
370 !!(fn->symbol->ctype.modifiers & MOD_STATIC),
371 type, param, key, value);
374 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name)
376 sql_insert(mtag_about, "%lld, '%s', '%s', %d, '%s', '%s'",
377 tag, get_filename(), get_function(), get_lineno(), left_name, right_name);
380 void sql_insert_mtag_map(mtag_t tag, int tag_offset, int container_offset, mtag_t container)
382 sql_insert(mtag_map, "%lld, %d, %d, %lld", tag, tag_offset, container_offset, container);
385 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias)
387 sql_insert(mtag_alias, "%lld, %lld", orig, alias);
390 static int save_mtag(void *_tag, int argc, char **argv, char **azColName)
392 mtag_t *saved_tag = _tag;
393 mtag_t new_tag;
395 new_tag = strtoll(argv[0], NULL, 10);
397 if (!*saved_tag)
398 *saved_tag = new_tag;
399 else if (*saved_tag != new_tag)
400 *saved_tag = -1ULL;
402 return 0;
405 int mtag_map_select_container(mtag_t tag, int offset, mtag_t *container)
407 mtag_t tmp = 0;
409 run_sql(save_mtag, &tmp,
410 "select container from mtag_map where tag = %lld and container_offset = %d and tag_offset = 0;",
411 tag, offset);
413 if (tmp == 0 || tmp == -1ULL)
414 return 0;
415 *container = tmp;
416 return 1;
419 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag)
421 mtag_t tmp = 0;
423 run_sql(save_mtag, &tmp,
424 "select tag from mtag_map where container = %lld and offset = %d;",
425 container, offset);
427 if (tmp == 0 || tmp == -1ULL)
428 return 0;
429 *tag = tmp;
430 return 1;
433 char *get_static_filter(struct symbol *sym)
435 static char sql_filter[1024];
437 /* This can only happen on buggy code. Return invalid SQL. */
438 if (!sym) {
439 sql_filter[0] = '\0';
440 return sql_filter;
443 if (sym->ctype.modifiers & MOD_STATIC) {
444 snprintf(sql_filter, sizeof(sql_filter),
445 "file = '%s' and function = '%s' and static = '1'",
446 get_base_file(), sym->ident->name);
447 } else {
448 snprintf(sql_filter, sizeof(sql_filter),
449 "function = '%s' and static = '0'", sym->ident->name);
452 return sql_filter;
455 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
457 int *row_count = _row_count;
459 *row_count = 0;
460 if (argc != 1)
461 return 0;
462 *row_count = atoi(argv[0]);
463 return 0;
466 static void mark_call_params_untracked(struct expression *call)
468 struct expression *arg;
469 int i = 0;
471 FOR_EACH_PTR(call->args, arg) {
472 mark_untracked(call, i++, "$", NULL);
473 } END_FOR_EACH_PTR(arg);
476 static void sql_select_return_states_pointer(const char *cols,
477 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
479 char *ptr;
480 int return_count = 0;
482 ptr = get_fnptr_name(call->fn);
483 if (!ptr)
484 return;
486 run_sql(get_row_count, &return_count,
487 "select count(*) from return_states join function_ptr "
488 "where return_states.function == function_ptr.function and "
489 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
490 /* The magic number 100 is just from testing on the kernel. */
491 if (return_count > 100) {
492 mark_call_params_untracked(call);
493 return;
496 run_sql(callback, info,
497 "select %s from return_states join function_ptr where "
498 "return_states.function == function_ptr.function and ptr = '%s' "
499 "and searchable = 1 "
500 "order by function_ptr.file, return_states.file, return_id, type;",
501 cols, ptr);
504 static int is_local_symbol(struct expression *expr)
506 if (expr->type != EXPR_SYMBOL)
507 return 0;
508 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
509 return 0;
510 return 1;
513 void sql_select_return_states(const char *cols, struct expression *call,
514 int (*callback)(void*, int, char**, char**), void *info)
516 struct expression *fn;
517 int row_count = 0;
519 if (is_fake_call(call))
520 return;
522 fn = strip_expr(call->fn);
523 if (fn->type != EXPR_SYMBOL || !fn->symbol || is_local_symbol(fn)) {
524 sql_select_return_states_pointer(cols, call, callback, info);
525 return;
528 if (inlinable(fn)) {
529 mem_sql(callback, info,
530 "select %s from return_states where call_id = '%lu' order by return_id, type;",
531 cols, (unsigned long)call);
532 return;
535 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
536 get_static_filter(fn->symbol));
537 if (row_count > 3000)
538 return;
540 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
541 cols, get_static_filter(fn->symbol));
544 #define CALL_IMPLIES 0
545 #define RETURN_IMPLIES 1
547 struct implies_info {
548 int type;
549 struct db_implies_cb_list *cb_list;
550 struct expression *expr;
551 struct symbol *sym;
554 void sql_select_implies(const char *cols, struct implies_info *info,
555 int (*callback)(void*, int, char**, char**))
557 if (info->type == RETURN_IMPLIES && inlinable(info->expr->fn)) {
558 mem_sql(callback, info,
559 "select %s from return_implies where call_id = '%lu';",
560 cols, (unsigned long)info->expr);
561 return;
564 run_sql(callback, info, "select %s from %s_implies where %s;",
565 cols,
566 info->type == CALL_IMPLIES ? "call" : "return",
567 get_static_filter(info->sym));
570 struct select_caller_info_data {
571 struct stree *final_states;
572 struct timeval start_time;
573 int prev_func_id;
574 int ignore;
575 int results;
578 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName);
580 static void sql_select_caller_info(struct select_caller_info_data *data,
581 const char *cols, struct symbol *sym)
583 if (__inline_fn) {
584 mem_sql(caller_info_callback, data,
585 "select %s from caller_info where call_id = %lu;",
586 cols, (unsigned long)__inline_fn);
587 return;
590 if (sym->ident->name && is_common_function(sym->ident->name))
591 return;
592 run_sql(caller_info_callback, data,
593 "select %s from common_caller_info where %s order by call_id;",
594 cols, get_static_filter(sym));
595 if (data->results)
596 return;
598 run_sql(caller_info_callback, data,
599 "select %s from caller_info where %s order by call_id;",
600 cols, get_static_filter(sym));
603 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
605 struct def_callback *def_callback = __alloc_def_callback(0);
607 def_callback->hook_type = type;
608 def_callback->callback = callback;
609 add_ptr_list(&select_caller_info_callbacks, def_callback);
613 * These call backs are used when the --info option is turned on to print struct
614 * member information. For example foo->bar could have a state in
615 * smatch_extra.c and also check_user.c.
617 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
619 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
621 member_callback->owner = owner;
622 member_callback->callback = callback;
623 add_ptr_list(&member_callbacks, member_callback);
626 void add_caller_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
628 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
630 member_callback->owner = owner;
631 member_callback->callback = callback;
632 add_ptr_list(&member_callbacks_new, member_callback);
635 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
637 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
639 callback->callback = fn;
640 add_ptr_list(&returned_state_callbacks, callback);
643 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))
645 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
647 member_callback->owner = owner;
648 member_callback->callback = callback;
649 add_ptr_list(&returned_member_callbacks, member_callback);
652 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
654 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
656 cb->type = type;
657 cb->callback = callback;
658 add_ptr_list(&call_implies_cb_list, cb);
661 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
663 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
665 cb->type = type;
666 cb->callback = callback;
667 add_ptr_list(&return_implies_cb_list, cb);
670 struct return_info {
671 struct expression *static_returns_call;
672 struct symbol *return_type;
673 struct range_list *return_range_list;
676 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
678 struct return_info *ret_info = _ret_info;
679 struct range_list *rl;
680 struct expression *call_expr = ret_info->static_returns_call;
682 if (argc != 1)
683 return 0;
684 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
685 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
686 return 0;
689 struct range_list *db_return_vals(struct expression *expr)
691 struct return_info ret_info = {};
692 char buf[64];
693 struct sm_state *sm;
695 if (is_fake_call(expr))
696 return NULL;
698 snprintf(buf, sizeof(buf), "return %p", expr);
699 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
700 if (sm)
701 return clone_rl(estate_rl(sm->state));
702 ret_info.static_returns_call = expr;
703 ret_info.return_type = get_type(expr);
704 if (!ret_info.return_type)
705 return NULL;
707 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
708 return NULL;
710 ret_info.return_range_list = NULL;
711 if (inlinable(expr->fn)) {
712 mem_sql(db_return_callback, &ret_info,
713 "select distinct return from return_states where call_id = '%lu';",
714 (unsigned long)expr);
715 } else {
716 run_sql(db_return_callback, &ret_info,
717 "select distinct return from return_states where %s;",
718 get_static_filter(expr->fn->symbol));
720 return ret_info.return_range_list;
723 struct range_list *db_return_vals_from_str(const char *fn_name)
725 struct return_info ret_info;
727 ret_info.static_returns_call = NULL;
728 ret_info.return_type = &llong_ctype;
729 ret_info.return_range_list = NULL;
731 run_sql(db_return_callback, &ret_info,
732 "select distinct return from return_states where function = '%s';",
733 fn_name);
734 return ret_info.return_range_list;
738 * This is used when we have a function that takes a function pointer as a
739 * parameter. "frob(blah, blah, my_function);" We know that the return values
740 * from frob() come from my_funcion() so we want to find the possible returns
741 * of my_function(), but we don't know which arguments are passed to it.
744 struct range_list *db_return_vals_no_args(struct expression *expr)
746 struct return_info ret_info = {};
748 if (!expr || expr->type != EXPR_SYMBOL)
749 return NULL;
751 ret_info.static_returns_call = expr;
752 ret_info.return_type = get_type(expr);
753 ret_info.return_type = get_real_base_type(ret_info.return_type);
754 if (!ret_info.return_type)
755 return NULL;
757 run_sql(db_return_callback, &ret_info,
758 "select distinct return from return_states where %s;",
759 get_static_filter(expr->symbol));
761 return ret_info.return_range_list;
764 static void match_call_marker(struct expression *expr)
766 struct symbol *type;
768 type = get_type(expr->fn);
769 if (type && type->type == SYM_PTR)
770 type = get_real_base_type(type);
773 * we just want to record something in the database so that if we have
774 * two calls like: frob(4); frob(some_unkown); then on the receiving
775 * side we know that sometimes frob is called with unknown parameters.
778 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", type_to_str(type));
781 int is_recursive_member(const char *name)
783 char buf[256];
784 const char *p, *next;
785 int size;
787 p = strchr(name, '>');
788 if (!p)
789 return 0;
790 p++;
791 while (true) {
792 next = strchr(p, '>');
793 if (!next)
794 return 0;
795 next++;
797 size = next - p;
798 if (size >= sizeof(buf))
799 return 0;
800 memcpy(buf, p, size);
801 buf[size] = '\0';
802 if (strstr(next, buf))
803 return 1;
804 p = next;
808 char *sm_to_arg_name(struct expression *expr, struct sm_state *sm)
810 struct symbol *sym;
811 const char *sm_name;
812 char *name;
813 bool is_address = false;
814 bool add_star = false;
815 char buf[256];
816 char *ret = NULL;
817 int len;
819 expr = strip_expr(expr);
820 if (!expr)
821 return NULL;
823 if (expr->type == EXPR_PREOP && expr->op == '&') {
824 expr = strip_expr(expr->unop);
825 is_address = true;
828 name = expr_to_var_sym(expr, &sym);
829 if (!name || !sym)
830 goto free;
831 if (sym != sm->sym)
832 goto free;
834 sm_name = sm->name;
835 add_star = false;
836 if (sm_name[0] == '*') {
837 add_star = true;
838 sm_name++;
841 len = strlen(name);
842 if (strncmp(name, sm_name, len) != 0)
843 goto free;
844 if (sm_name[len] == '\0') {
845 snprintf(buf, sizeof(buf), "%s%s$",
846 add_star ? "*" : "", is_address ? "*" : "");
847 } else {
848 if (sm_name[len] != '.' && sm_name[len] != '-')
849 goto free;
850 if (sm_name[len] == '-')
851 len++;
852 // FIXME does is_address really imply that sm_name[len] == '-'
853 snprintf(buf, sizeof(buf), "%s$->%s", add_star ? "*" : "",
854 sm_name + len);
857 ret = alloc_sname(buf);
858 free:
859 free_string(name);
860 return ret;
863 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
864 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm),
865 bool new)
867 struct sm_state *sm;
868 const char *sm_name;
869 char *name;
870 struct symbol *sym;
871 int len;
872 char printed_name[256];
873 int is_address = 0;
874 bool add_star;
875 struct symbol *type;
877 expr = strip_expr(expr);
878 if (!expr)
879 return;
880 type = get_type(expr);
881 if (type && type_bits(type) < type_bits(&ulong_ctype))
882 return;
884 if (expr->type == EXPR_PREOP && expr->op == '&') {
885 expr = strip_expr(expr->unop);
886 is_address = 1;
889 name = expr_to_var_sym(expr, &sym);
890 if (!name || !sym)
891 goto free;
893 len = strlen(name);
894 FOR_EACH_SM(stree, sm) {
895 if (sm->sym != sym)
896 continue;
897 sm_name = sm->name;
898 add_star = false;
899 if (sm_name[0] == '*') {
900 add_star = true;
901 sm_name++;
903 // FIXME: simplify?
904 if (!add_star && strcmp(name, sm_name) == 0) {
905 if (is_address) {
906 snprintf(printed_name, sizeof(printed_name), "*$");
907 } else {
908 if (new)
909 snprintf(printed_name, sizeof(printed_name), "$");
910 else
911 continue;
913 } else if (add_star && strcmp(name, sm_name) == 0) {
914 snprintf(printed_name, sizeof(printed_name), "%s*$",
915 is_address ? "*" : "");
916 } else if (strncmp(name, sm_name, len) == 0) {
917 if (sm_name[len] != '.' && sm_name[len] != '-')
918 continue;
919 if (is_address)
920 snprintf(printed_name, sizeof(printed_name),
921 "%s$->%s", add_star ? "*" : "",
922 sm_name + len + 1);
923 else
924 snprintf(printed_name, sizeof(printed_name),
925 "%s$%s", add_star ? "*" : "",
926 sm_name + len);
927 } else {
928 continue;
930 if (is_recursive_member(printed_name))
931 continue;
932 callback(call, param, printed_name, sm);
933 } END_FOR_EACH_SM(sm);
934 free:
935 free_string(name);
938 static void match_call_info(struct expression *call)
940 struct member_info_callback *cb;
941 struct expression *arg;
942 struct stree *stree;
943 char *name;
944 int i;
946 name = get_fnptr_name(call->fn);
947 if (!name)
948 return;
950 FOR_EACH_PTR(member_callbacks, cb) {
951 stree = get_all_states_stree(cb->owner);
952 i = 0;
953 FOR_EACH_PTR(call->args, arg) {
954 print_struct_members(call, arg, i, stree, cb->callback, 0);
955 i++;
956 } END_FOR_EACH_PTR(arg);
957 free_stree(&stree);
958 } END_FOR_EACH_PTR(cb);
960 free_string(name);
963 static void match_call_info_new(struct expression *call)
965 struct member_info_callback *cb;
966 struct expression *arg;
967 struct stree *stree;
968 char *name;
969 int i;
971 name = get_fnptr_name(call->fn);
972 if (!name)
973 return;
975 FOR_EACH_PTR(member_callbacks_new, cb) {
976 stree = get_all_states_stree(cb->owner);
977 i = 0;
978 FOR_EACH_PTR(call->args, arg) {
979 print_struct_members(call, arg, i, stree, cb->callback, 1);
980 i++;
981 } END_FOR_EACH_PTR(arg);
982 free_stree(&stree);
983 } END_FOR_EACH_PTR(cb);
985 free_string(name);
988 static int get_param(int param, char **name, struct symbol **sym)
990 struct symbol *arg;
991 int i;
993 i = 0;
994 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
996 * this is a temporary hack to work around a bug (I think in sparse?)
997 * 2.6.37-rc1:fs/reiserfs/journal.o
998 * If there is a function definition without parameter name found
999 * after a function implementation then it causes a crash.
1000 * int foo() {}
1001 * int bar(char *);
1003 if (arg->ident->name < (char *)100)
1004 continue;
1005 if (i == param) {
1006 *name = arg->ident->name;
1007 *sym = arg;
1008 return TRUE;
1010 i++;
1011 } END_FOR_EACH_PTR(arg);
1013 return FALSE;
1016 static int function_signature_matches(const char *sig)
1018 char *my_sig;
1020 my_sig = function_signature();
1021 if (!sig || !my_sig)
1022 return 1; /* default to matching */
1023 if (strcmp(my_sig, sig) == 0)
1024 return 1;
1025 return 0;
1028 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
1030 struct select_caller_info_data *data = _data;
1031 int func_id;
1032 long type;
1033 long param;
1034 char *key;
1035 char *value;
1036 char *name = NULL;
1037 struct symbol *sym = NULL;
1038 struct def_callback *def_callback;
1039 struct stree *stree;
1040 struct timeval cur_time;
1042 data->results = 1;
1044 if (argc != 5)
1045 return 0;
1047 gettimeofday(&cur_time, NULL);
1048 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
1049 return 0;
1051 func_id = atoi(argv[0]);
1052 errno = 0;
1053 type = strtol(argv[1], NULL, 10);
1054 param = strtol(argv[2], NULL, 10);
1055 if (errno)
1056 return 0;
1057 key = argv[3];
1058 value = argv[4];
1060 if (data->prev_func_id == -1)
1061 data->prev_func_id = func_id;
1062 if (func_id != data->prev_func_id) {
1063 stree = __pop_fake_cur_stree();
1064 if (!data->ignore)
1065 merge_stree(&data->final_states, stree);
1066 free_stree(&stree);
1067 __push_fake_cur_stree();
1068 __unnullify_path();
1069 data->prev_func_id = func_id;
1070 data->ignore = 0;
1073 if (data->ignore)
1074 return 0;
1075 if (type == INTERNAL &&
1076 !function_signature_matches(value)) {
1077 data->ignore = 1;
1078 return 0;
1081 if (param >= 0 && !get_param(param, &name, &sym))
1082 return 0;
1084 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
1085 if (def_callback->hook_type == type)
1086 def_callback->callback(name, sym, key, value);
1087 } END_FOR_EACH_PTR(def_callback);
1089 return 0;
1092 static struct string_list *ptr_names_done;
1093 static struct string_list *ptr_names;
1095 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
1097 insert_string(&ptr_names, alloc_string(argv[0]));
1098 return 0;
1101 static char *get_next_ptr_name(void)
1103 char *ptr;
1105 FOR_EACH_PTR(ptr_names, ptr) {
1106 if (!insert_string(&ptr_names_done, ptr))
1107 continue;
1108 return ptr;
1109 } END_FOR_EACH_PTR(ptr);
1110 return NULL;
1113 static void get_ptr_names(const char *file, const char *name)
1115 char sql_filter[1024];
1116 int before, after;
1118 if (file) {
1119 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
1120 file, name);
1121 } else {
1122 snprintf(sql_filter, 1024, "function = '%s';", name);
1125 before = ptr_list_size((struct ptr_list *)ptr_names);
1127 run_sql(get_ptr_name, NULL,
1128 "select distinct ptr from function_ptr where %s",
1129 sql_filter);
1131 after = ptr_list_size((struct ptr_list *)ptr_names);
1132 if (before == after)
1133 return;
1135 while ((name = get_next_ptr_name()))
1136 get_ptr_names(NULL, name);
1139 static void match_data_from_db(struct symbol *sym)
1141 struct select_caller_info_data data = { .prev_func_id = -1 };
1142 struct sm_state *sm;
1143 struct stree *stree;
1144 struct timeval end_time;
1146 if (!sym || !sym->ident)
1147 return;
1149 gettimeofday(&data.start_time, NULL);
1151 __push_fake_cur_stree();
1152 __unnullify_path();
1154 if (!__inline_fn) {
1155 char *ptr;
1157 if (sym->ctype.modifiers & MOD_STATIC)
1158 get_ptr_names(get_base_file(), sym->ident->name);
1159 else
1160 get_ptr_names(NULL, sym->ident->name);
1162 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1163 __free_ptr_list((struct ptr_list **)&ptr_names);
1164 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1165 __free_fake_cur_stree();
1166 return;
1169 sql_select_caller_info(&data,
1170 "call_id, type, parameter, key, value",
1171 sym);
1174 stree = __pop_fake_cur_stree();
1175 if (!data.ignore)
1176 merge_stree(&data.final_states, stree);
1177 free_stree(&stree);
1178 __push_fake_cur_stree();
1179 __unnullify_path();
1180 data.prev_func_id = -1;
1181 data.ignore = 0;
1182 data.results = 0;
1184 FOR_EACH_PTR(ptr_names, ptr) {
1185 run_sql(caller_info_callback, &data,
1186 "select call_id, type, parameter, key, value"
1187 " from common_caller_info where function = '%s' order by call_id",
1188 ptr);
1189 } END_FOR_EACH_PTR(ptr);
1191 if (data.results) {
1192 FOR_EACH_PTR(ptr_names, ptr) {
1193 free_string(ptr);
1194 } END_FOR_EACH_PTR(ptr);
1195 goto free_ptr_names;
1198 FOR_EACH_PTR(ptr_names, ptr) {
1199 run_sql(caller_info_callback, &data,
1200 "select call_id, type, parameter, key, value"
1201 " from caller_info where function = '%s' order by call_id",
1202 ptr);
1203 free_string(ptr);
1204 } END_FOR_EACH_PTR(ptr);
1206 free_ptr_names:
1207 __free_ptr_list((struct ptr_list **)&ptr_names);
1208 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1209 } else {
1210 sql_select_caller_info(&data,
1211 "call_id, type, parameter, key, value",
1212 sym);
1215 stree = __pop_fake_cur_stree();
1216 if (!data.ignore)
1217 merge_stree(&data.final_states, stree);
1218 free_stree(&stree);
1220 gettimeofday(&end_time, NULL);
1221 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1222 FOR_EACH_SM(data.final_states, sm) {
1223 __set_sm(sm);
1224 } END_FOR_EACH_SM(sm);
1227 free_stree(&data.final_states);
1230 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1232 struct implies_info *info = _info;
1233 struct db_implies_callback *cb;
1234 struct expression *arg = NULL;
1235 int type;
1236 int param;
1238 if (argc != 5)
1239 return 0;
1241 type = atoi(argv[1]);
1242 param = atoi(argv[2]);
1244 FOR_EACH_PTR(info->cb_list, cb) {
1245 if (cb->type != type)
1246 continue;
1247 if (param != -1) {
1248 arg = get_argument_from_call_expr(info->expr->args, param);
1249 if (!arg)
1250 continue;
1252 cb->callback(info->expr, arg, argv[3], argv[4]);
1253 } END_FOR_EACH_PTR(cb);
1255 return 0;
1258 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1260 struct implies_info *info = _info;
1261 struct db_implies_callback *cb;
1262 struct expression *arg;
1263 struct symbol *sym;
1264 char *name;
1265 int type;
1266 int param;
1268 if (argc != 5)
1269 return 0;
1271 type = atoi(argv[1]);
1272 param = atoi(argv[2]);
1274 if (!get_param(param, &name, &sym))
1275 return 0;
1276 arg = symbol_expression(sym);
1277 if (!arg)
1278 return 0;
1280 FOR_EACH_PTR(info->cb_list, cb) {
1281 if (cb->type != type)
1282 continue;
1283 cb->callback(info->expr, arg, argv[3], argv[4]);
1284 } END_FOR_EACH_PTR(cb);
1286 return 0;
1289 static void match_return_implies(struct expression *expr)
1291 struct implies_info info = {
1292 .type = RETURN_IMPLIES,
1293 .cb_list = return_implies_cb_list,
1296 if (expr->fn->type != EXPR_SYMBOL ||
1297 !expr->fn->symbol)
1298 return;
1299 info.expr = expr;
1300 info.sym = expr->fn->symbol;
1301 sql_select_implies("function, type, parameter, key, value", &info,
1302 return_implies_callbacks);
1305 static void match_call_implies(struct symbol *sym)
1307 struct implies_info info = {
1308 .type = CALL_IMPLIES,
1309 .cb_list = call_implies_cb_list,
1312 if (!sym || !sym->ident)
1313 return;
1315 info.sym = sym;
1316 sql_select_implies("function, type, parameter, key, value", &info,
1317 call_implies_callbacks);
1320 static char *get_fn_param_str(struct expression *expr)
1322 struct expression *tmp;
1323 int param;
1324 char buf[32];
1326 tmp = get_assigned_expr(expr);
1327 if (tmp)
1328 expr = tmp;
1329 expr = strip_expr(expr);
1330 if (!expr || expr->type != EXPR_CALL)
1331 return NULL;
1332 expr = strip_expr(expr->fn);
1333 if (!expr || expr->type != EXPR_SYMBOL)
1334 return NULL;
1335 param = get_param_num(expr);
1336 if (param < 0)
1337 return NULL;
1339 snprintf(buf, sizeof(buf), "[r $%d]", param);
1340 return alloc_sname(buf);
1343 static char *get_return_compare_is_param(struct expression *expr)
1345 char *var;
1346 char buf[256];
1347 int comparison;
1348 int param;
1350 param = get_param_num(expr);
1351 if (param < 0)
1352 return NULL;
1354 var = expr_to_var(expr);
1355 if (!var)
1356 return NULL;
1357 snprintf(buf, sizeof(buf), "%s orig", var);
1358 comparison = get_comparison_strings(var, buf);
1359 free_string(var);
1361 if (!comparison)
1362 return NULL;
1364 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1365 return alloc_sname(buf);
1368 static char *get_return_compare_str(struct expression *expr)
1370 char *compare_str;
1372 compare_str = get_return_compare_is_param(expr);
1373 if (compare_str)
1374 return compare_str;
1376 compare_str = expr_lte_to_param(expr, -1);
1377 if (compare_str)
1378 return compare_str;
1380 return expr_param_comparison(expr, -1);
1383 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1385 struct range_list *rl;
1386 char *return_ranges;
1387 sval_t sval;
1388 char *fn_param_str;
1389 char *compare_str;
1390 char *math_str;
1391 char buf[128];
1393 *rl_p = NULL;
1395 if (!expr)
1396 return alloc_sname("");
1398 if (get_implied_value(expr, &sval)) {
1399 sval = sval_cast(cur_func_return_type(), sval);
1400 *rl_p = alloc_rl(sval, sval);
1401 return sval_to_str_or_err_ptr(sval);
1404 fn_param_str = get_fn_param_str(expr);
1405 compare_str = expr_equal_to_param(expr, -1);
1406 math_str = get_value_in_terms_of_parameter_math(expr);
1408 if (get_implied_rl(expr, &rl) && !is_whole_rl(rl)) {
1409 rl = cast_rl(cur_func_return_type(), rl);
1410 return_ranges = show_rl(rl);
1411 } else if (get_imaginary_absolute(expr, &rl)){
1412 rl = cast_rl(cur_func_return_type(), rl);
1413 return alloc_sname(show_rl(rl));
1414 } else {
1415 get_absolute_rl(expr, &rl);
1416 rl = cast_rl(cur_func_return_type(), rl);
1417 return_ranges = show_rl(rl);
1419 *rl_p = rl;
1421 if (fn_param_str) {
1422 snprintf(buf, sizeof(buf), "%s%s", return_ranges, fn_param_str);
1423 return alloc_sname(buf);
1425 if (compare_str) {
1426 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1427 return alloc_sname(buf);
1429 if (math_str) {
1430 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1431 return alloc_sname(buf);
1433 compare_str = get_return_compare_str(expr);
1434 if (compare_str) {
1435 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1436 return alloc_sname(buf);
1439 return return_ranges;
1442 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1444 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1447 static bool call_return_state_hooks_conditional(struct expression *expr)
1449 int final_pass_orig = final_pass;
1450 static int recurse;
1452 if (recurse >= 2)
1453 return false;
1454 if (!expr ||
1455 (expr->type != EXPR_CONDITIONAL && expr->type != EXPR_SELECT))
1456 return false;
1458 recurse++;
1460 __push_fake_cur_stree();
1462 final_pass = 0;
1463 __split_whole_condition(expr->conditional);
1464 final_pass = final_pass_orig;
1466 call_return_state_hooks(expr->cond_true ?: expr->conditional);
1468 __push_true_states();
1469 __use_false_states();
1471 call_return_state_hooks(expr->cond_false);
1473 __merge_true_states();
1474 __free_fake_cur_stree();
1476 recurse--;
1477 return true;
1480 static void call_return_state_hooks_compare(struct expression *expr)
1482 struct returned_state_callback *cb;
1483 char *return_ranges;
1484 int final_pass_orig = final_pass;
1485 sval_t sval = { .type = &int_ctype };
1486 sval_t ret;
1488 if (!get_implied_value(expr, &ret))
1489 ret.value = -1;
1491 __push_fake_cur_stree();
1493 final_pass = 0;
1494 __split_whole_condition(expr);
1495 final_pass = final_pass_orig;
1497 if (ret.value != 0) {
1498 return_ranges = alloc_sname("1");
1499 sval.value = 1;
1500 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1502 return_id++;
1503 FOR_EACH_PTR(returned_state_callbacks, cb) {
1504 cb->callback(return_id, return_ranges, expr);
1505 } END_FOR_EACH_PTR(cb);
1508 __push_true_states();
1509 __use_false_states();
1511 if (ret.value != 1) {
1512 return_ranges = alloc_sname("0");
1513 sval.value = 0;
1514 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1516 return_id++;
1517 FOR_EACH_PTR(returned_state_callbacks, cb) {
1518 cb->callback(return_id, return_ranges, expr);
1519 } END_FOR_EACH_PTR(cb);
1522 __merge_true_states();
1523 __free_fake_cur_stree();
1526 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1528 struct sm_state *tmp;
1530 FOR_EACH_PTR(slist, tmp) {
1531 if (strcmp(tmp->state->name, sm->state->name) == 0)
1532 return 1;
1533 } END_FOR_EACH_PTR(tmp);
1535 return 0;
1538 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1540 struct returned_state_callback *cb;
1541 struct range_list *rl;
1542 char *return_ranges;
1543 struct sm_state *tmp;
1544 int ret = 0;
1545 int nr_possible, nr_states;
1546 char *compare_str;
1547 char buf[128];
1548 struct state_list *already_handled = NULL;
1549 sval_t sval;
1551 if (!sm || !sm->merged)
1552 return 0;
1554 if (too_many_possible(sm))
1555 return 0;
1557 /* bail if it gets too complicated */
1558 nr_possible = 0;
1559 FOR_EACH_PTR(sm->possible, tmp) {
1560 if (tmp->merged)
1561 continue;
1562 if (ptr_in_list(tmp, already_handled))
1563 continue;
1564 add_ptr_list(&already_handled, tmp);
1565 nr_possible++;
1566 } END_FOR_EACH_PTR(tmp);
1567 free_slist(&already_handled);
1568 nr_states = get_db_state_count();
1569 if (nr_states * nr_possible >= 2000)
1570 return 0;
1572 FOR_EACH_PTR(sm->possible, tmp) {
1573 if (tmp->merged)
1574 continue;
1575 if (ptr_in_list(tmp, already_handled))
1576 continue;
1577 add_ptr_list(&already_handled, tmp);
1579 ret = 1;
1580 __push_fake_cur_stree();
1582 overwrite_states_using_pool(sm, tmp);
1584 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1585 return_ranges = show_rl(rl);
1586 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1587 if (!rl_to_sval(rl, &sval)) {
1588 compare_str = get_return_compare_str(expr);
1589 if (compare_str) {
1590 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1591 return_ranges = alloc_sname(buf);
1595 return_id++;
1596 FOR_EACH_PTR(returned_state_callbacks, cb) {
1597 cb->callback(return_id, return_ranges, expr);
1598 } END_FOR_EACH_PTR(cb);
1600 __free_fake_cur_stree();
1601 } END_FOR_EACH_PTR(tmp);
1603 free_slist(&already_handled);
1605 return ret;
1608 static int call_return_state_hooks_split_possible(struct expression *expr)
1610 struct sm_state *sm;
1612 if (!expr || expr_equal_to_param(expr, -1))
1613 return 0;
1615 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1616 return split_possible_helper(sm, expr);
1619 static bool has_possible_negative(struct sm_state *sm)
1621 struct sm_state *tmp;
1623 if (!type_signed(estate_type(sm->state)))
1624 return false;
1626 FOR_EACH_PTR(sm->possible, tmp) {
1627 if (!estate_rl(tmp->state))
1628 continue;
1629 if (sval_is_negative(estate_min(tmp->state)) &&
1630 sval_is_negative(estate_max(tmp->state)))
1631 return true;
1632 } END_FOR_EACH_PTR(tmp);
1634 return false;
1637 static bool has_separate_zero_null(struct sm_state *sm)
1639 struct sm_state *tmp;
1640 sval_t sval;
1642 FOR_EACH_PTR(sm->possible, tmp) {
1643 if (!estate_get_single_value(tmp->state, &sval))
1644 continue;
1645 if (sval.value == 0)
1646 return true;
1647 } END_FOR_EACH_PTR(tmp);
1649 return false;
1652 static int split_positive_from_negative(struct expression *expr)
1654 struct sm_state *sm;
1655 struct returned_state_callback *cb;
1656 struct range_list *rl;
1657 const char *return_ranges;
1658 struct range_list *ret_rl;
1659 bool separate_zero;
1660 int undo;
1662 /* We're going to print the states 3 times */
1663 if (get_db_state_count() > 10000 / 3)
1664 return 0;
1666 if (!get_implied_rl(expr, &rl) || !rl)
1667 return 0;
1668 /* Forget about INT_MAX and larger */
1669 if (rl_max(rl).value <= 0)
1670 return 0;
1671 if (!sval_is_negative(rl_min(rl)))
1672 return 0;
1674 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1675 if (!sm)
1676 return 0;
1677 if (!has_possible_negative(sm))
1678 return 0;
1679 separate_zero = has_separate_zero_null(sm);
1681 if (!assume(compare_expression(expr, separate_zero ? '>' : SPECIAL_GTE, zero_expr())))
1682 return 0;
1684 return_id++;
1685 return_ranges = get_return_ranges_str(expr, &ret_rl);
1686 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1687 FOR_EACH_PTR(returned_state_callbacks, cb) {
1688 cb->callback(return_id, (char *)return_ranges, expr);
1689 } END_FOR_EACH_PTR(cb);
1691 end_assume();
1693 if (separate_zero) {
1694 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1696 return_id++;
1697 return_ranges = get_return_ranges_str(expr, &ret_rl);
1698 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1699 FOR_EACH_PTR(returned_state_callbacks, cb) {
1700 cb->callback(return_id, (char *)return_ranges, expr);
1701 } END_FOR_EACH_PTR(cb);
1703 if (undo)
1704 end_assume();
1707 undo = assume(compare_expression(expr, '<', zero_expr()));
1709 return_id++;
1710 return_ranges = get_return_ranges_str(expr, &ret_rl);
1711 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1712 FOR_EACH_PTR(returned_state_callbacks, cb) {
1713 cb->callback(return_id, (char *)return_ranges, expr);
1714 } END_FOR_EACH_PTR(cb);
1716 if (undo)
1717 end_assume();
1719 return 1;
1722 static int call_return_state_hooks_split_null_non_null_zero(struct expression *expr)
1724 struct returned_state_callback *cb;
1725 struct range_list *rl;
1726 struct range_list *nonnull_rl;
1727 sval_t null_sval;
1728 struct range_list *null_rl = NULL;
1729 char *return_ranges;
1730 struct sm_state *sm;
1731 struct smatch_state *state;
1732 int nr_states;
1733 int final_pass_orig = final_pass;
1735 if (!expr || expr_equal_to_param(expr, -1))
1736 return 0;
1737 if (expr->type == EXPR_CALL)
1738 return 0;
1740 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1741 if (!sm)
1742 return 0;
1743 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1744 return 0;
1745 state = sm->state;
1746 if (!estate_rl(state))
1747 return 0;
1748 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1749 return 0;
1750 if (!has_separate_zero_null(sm))
1751 return 0;
1753 nr_states = get_db_state_count();
1754 if (option_info && nr_states >= 1500)
1755 return 0;
1757 rl = estate_rl(state);
1759 __push_fake_cur_stree();
1761 final_pass = 0;
1762 __split_whole_condition(expr);
1763 final_pass = final_pass_orig;
1765 nonnull_rl = rl_filter(rl, rl_zero());
1766 return_ranges = show_rl(nonnull_rl);
1767 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1769 return_id++;
1770 FOR_EACH_PTR(returned_state_callbacks, cb) {
1771 cb->callback(return_id, return_ranges, expr);
1772 } END_FOR_EACH_PTR(cb);
1774 __push_true_states();
1775 __use_false_states();
1777 return_ranges = alloc_sname("0");
1778 null_sval = sval_type_val(rl_type(rl), 0);
1779 add_range(&null_rl, null_sval, null_sval);
1780 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1781 return_id++;
1782 FOR_EACH_PTR(returned_state_callbacks, cb) {
1783 cb->callback(return_id, return_ranges, expr);
1784 } END_FOR_EACH_PTR(cb);
1786 __merge_true_states();
1787 __free_fake_cur_stree();
1789 return 1;
1792 static bool is_kernel_success_fail(struct sm_state *sm)
1794 struct sm_state *tmp;
1795 struct range_list *rl;
1796 bool has_zero = false;
1797 bool has_neg = false;
1799 if (!type_signed(estate_type(sm->state)))
1800 return false;
1802 FOR_EACH_PTR(sm->possible, tmp) {
1803 rl = estate_rl(tmp->state);
1804 if (!rl)
1805 return false;
1806 if (rl_min(rl).value == 0 && rl_max(rl).value == 0) {
1807 has_zero = true;
1808 continue;
1810 has_neg = true;
1811 if (rl_min(rl).value >= -4095 && rl_max(rl).value < 0)
1812 continue;
1813 if (strcmp(tmp->state->name, "s32min-(-1)") == 0)
1814 continue;
1815 if (strcmp(tmp->state->name, "s32min-(-1),1-s32max") == 0)
1816 continue;
1817 return false;
1818 } END_FOR_EACH_PTR(tmp);
1820 return has_zero && has_neg;
1823 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1825 struct sm_state *sm;
1826 struct range_list *rl;
1827 struct range_list *nonzero_rl;
1828 sval_t zero_sval;
1829 struct range_list *zero_rl = NULL;
1830 int nr_states;
1831 struct returned_state_callback *cb;
1832 char *return_ranges;
1833 int final_pass_orig = final_pass;
1835 if (option_project != PROJ_KERNEL)
1836 return 0;
1838 nr_states = get_db_state_count();
1839 if (nr_states > 2000)
1840 return 0;
1842 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1843 if (!sm)
1844 return 0;
1845 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1846 return 0;
1847 if (!is_kernel_success_fail(sm))
1848 return 0;
1850 rl = estate_rl(sm->state);
1851 if (!rl)
1852 return 0;
1854 __push_fake_cur_stree();
1856 final_pass = 0;
1857 __split_whole_condition(expr);
1858 final_pass = final_pass_orig;
1860 nonzero_rl = rl_filter(rl, rl_zero());
1861 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
1862 return_ranges = show_rl(nonzero_rl);
1863 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1865 return_id++;
1866 FOR_EACH_PTR(returned_state_callbacks, cb) {
1867 cb->callback(return_id, return_ranges, expr);
1868 } END_FOR_EACH_PTR(cb);
1870 __push_true_states();
1871 __use_false_states();
1873 return_ranges = alloc_sname("0");
1874 zero_sval = sval_type_val(rl_type(rl), 0);
1875 add_range(&zero_rl, zero_sval, zero_sval);
1876 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1877 return_id++;
1878 FOR_EACH_PTR(returned_state_callbacks, cb) {
1879 cb->callback(return_id, return_ranges, expr);
1880 } END_FOR_EACH_PTR(cb);
1882 __merge_true_states();
1883 __free_fake_cur_stree();
1885 return 1;
1888 static int is_boolean(struct expression *expr)
1890 struct range_list *rl;
1892 if (!get_implied_rl(expr, &rl))
1893 return 0;
1894 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1895 return 1;
1896 return 0;
1899 static int splitable_function_call(struct expression *expr)
1901 struct sm_state *sm;
1902 char buf[64];
1904 if (!expr || expr->type != EXPR_CALL)
1905 return 0;
1906 snprintf(buf, sizeof(buf), "return %p", expr);
1907 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1908 return split_possible_helper(sm, expr);
1911 static struct sm_state *find_bool_param(void)
1913 struct stree *start_states;
1914 struct symbol *arg;
1915 struct sm_state *sm, *tmp;
1916 sval_t sval;
1918 start_states = get_start_states();
1920 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1921 if (!arg->ident)
1922 continue;
1923 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1924 if (!sm)
1925 continue;
1926 if (rl_min(estate_rl(sm->state)).value != 0 ||
1927 rl_max(estate_rl(sm->state)).value != 1)
1928 continue;
1929 goto found;
1930 } END_FOR_EACH_PTR_REVERSE(arg);
1932 return NULL;
1934 found:
1936 * Check if it's splitable. If not, then splitting it up is likely not
1937 * useful for the callers.
1939 FOR_EACH_PTR(sm->possible, tmp) {
1940 if (is_merged(tmp))
1941 continue;
1942 if (!estate_get_single_value(tmp->state, &sval))
1943 return NULL;
1944 } END_FOR_EACH_PTR(tmp);
1946 return sm;
1949 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1951 struct returned_state_callback *cb;
1952 struct range_list *ret_rl;
1953 const char *return_ranges;
1954 struct sm_state *tmp;
1955 int ret = 0;
1956 struct state_list *already_handled = NULL;
1958 if (!sm || !sm->merged)
1959 return 0;
1961 if (too_many_possible(sm))
1962 return 0;
1964 FOR_EACH_PTR(sm->possible, tmp) {
1965 if (tmp->merged)
1966 continue;
1967 if (ptr_in_list(tmp, already_handled))
1968 continue;
1969 add_ptr_list(&already_handled, tmp);
1971 ret = 1;
1972 __push_fake_cur_stree();
1974 overwrite_states_using_pool(sm, tmp);
1976 return_ranges = get_return_ranges_str(expr, &ret_rl);
1977 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1978 return_id++;
1979 FOR_EACH_PTR(returned_state_callbacks, cb) {
1980 cb->callback(return_id, (char *)return_ranges, expr);
1981 } END_FOR_EACH_PTR(cb);
1983 __free_fake_cur_stree();
1984 } END_FOR_EACH_PTR(tmp);
1986 free_slist(&already_handled);
1988 return ret;
1991 static int split_by_bool_param(struct expression *expr)
1993 struct sm_state *start_sm, *sm;
1994 sval_t sval;
1996 start_sm = find_bool_param();
1997 if (!start_sm)
1998 return 0;
1999 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
2000 if (!sm || estate_get_single_value(sm->state, &sval))
2001 return 0;
2003 if (get_db_state_count() * 2 >= 2000)
2004 return 0;
2006 return split_on_bool_sm(sm, expr);
2009 static int split_by_null_nonnull_param(struct expression *expr)
2011 struct symbol *arg;
2012 struct sm_state *sm;
2013 int nr_possible;
2015 /* function must only take one pointer */
2016 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
2017 return 0;
2018 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
2019 if (!arg->ident)
2020 return 0;
2021 if (get_real_base_type(arg)->type != SYM_PTR)
2022 return 0;
2024 if (param_was_set_var_sym(arg->ident->name, arg))
2025 return 0;
2026 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
2027 if (!sm)
2028 return 0;
2030 if (!has_separate_zero_null(sm))
2031 return 0;
2033 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
2034 if (get_db_state_count() * nr_possible >= 2000)
2035 return 0;
2037 return split_on_bool_sm(sm, expr);
2040 struct expression *strip_expr_statement(struct expression *expr)
2042 struct expression *orig = expr;
2043 struct statement *stmt, *last_stmt;
2045 if (!expr)
2046 return NULL;
2047 if (expr->type == EXPR_PREOP && expr->op == '(')
2048 expr = expr->unop;
2049 if (expr->type != EXPR_STATEMENT)
2050 return orig;
2051 stmt = expr->statement;
2052 if (!stmt || stmt->type != STMT_COMPOUND)
2053 return orig;
2055 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
2056 if (!last_stmt || last_stmt->type == STMT_LABEL)
2057 last_stmt = last_stmt->label_statement;
2058 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
2059 return orig;
2060 return strip_expr(last_stmt->expression);
2063 static void call_return_state_hooks(struct expression *expr)
2065 struct returned_state_callback *cb;
2066 struct range_list *ret_rl;
2067 const char *return_ranges;
2068 int nr_states;
2069 sval_t sval;
2071 if (__path_is_null())
2072 return;
2074 expr = strip_expr(expr);
2075 expr = strip_expr_statement(expr);
2077 if (is_impossible_path())
2078 goto vanilla;
2080 if (expr && (expr->type == EXPR_COMPARE ||
2081 !get_implied_value(expr, &sval)) &&
2082 (is_condition(expr) || is_boolean(expr))) {
2083 call_return_state_hooks_compare(expr);
2084 return;
2085 } else if (call_return_state_hooks_conditional(expr)) {
2086 return;
2087 } else if (call_return_state_hooks_split_possible(expr)) {
2088 return;
2089 } else if (split_positive_from_negative(expr)) {
2090 return;
2091 } else if (call_return_state_hooks_split_null_non_null_zero(expr)) {
2092 return;
2093 } else if (call_return_state_hooks_split_success_fail(expr)) {
2094 return;
2095 } else if (splitable_function_call(expr)) {
2096 return;
2097 } else if (split_by_bool_param(expr)) {
2098 } else if (split_by_null_nonnull_param(expr)) {
2099 return;
2102 vanilla:
2103 return_ranges = get_return_ranges_str(expr, &ret_rl);
2104 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2106 return_id++;
2107 nr_states = get_db_state_count();
2108 if (nr_states >= 10000) {
2109 match_return_info(return_id, (char *)return_ranges, expr);
2110 print_limited_param_set(return_id, (char *)return_ranges, expr);
2111 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2112 return;
2114 FOR_EACH_PTR(returned_state_callbacks, cb) {
2115 cb->callback(return_id, (char *)return_ranges, expr);
2116 } END_FOR_EACH_PTR(cb);
2119 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2121 struct returned_member_callback *cb;
2122 struct stree *stree;
2123 struct sm_state *sm;
2124 struct symbol *type;
2125 char *name;
2126 char member_name[256];
2127 int len;
2129 type = get_type(expr);
2130 if (!type || type->type != SYM_PTR)
2131 return;
2132 name = expr_to_var(expr);
2133 if (!name)
2134 return;
2136 member_name[sizeof(member_name) - 1] = '\0';
2137 strcpy(member_name, "$");
2139 len = strlen(name);
2140 FOR_EACH_PTR(returned_member_callbacks, cb) {
2141 stree = __get_cur_stree();
2142 FOR_EACH_MY_SM(cb->owner, stree, sm) {
2143 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2144 strcpy(member_name, "*$");
2145 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2146 continue;
2148 if (strncmp(sm->name, name, len) != 0)
2149 continue;
2150 if (strncmp(sm->name + len, "->", 2) != 0)
2151 continue;
2152 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2153 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2154 } END_FOR_EACH_SM(sm);
2155 } END_FOR_EACH_PTR(cb);
2157 free_string(name);
2160 static void reset_memdb(struct symbol *sym)
2162 mem_sql(NULL, NULL, "delete from caller_info;");
2163 mem_sql(NULL, NULL, "delete from return_states;");
2164 mem_sql(NULL, NULL, "delete from call_implies;");
2165 mem_sql(NULL, NULL, "delete from return_implies;");
2168 static void match_end_func_info(struct symbol *sym)
2170 if (__path_is_null())
2171 return;
2172 call_return_state_hooks(NULL);
2175 static void match_after_func(struct symbol *sym)
2177 if (!__inline_fn)
2178 reset_memdb(sym);
2181 static void init_memdb(void)
2183 char *err = NULL;
2184 int rc;
2185 const char *schema_files[] = {
2186 "db/db.schema",
2187 "db/caller_info.schema",
2188 "db/common_caller_info.schema",
2189 "db/return_states.schema",
2190 "db/function_type_size.schema",
2191 "db/type_size.schema",
2192 "db/function_type_info.schema",
2193 "db/type_info.schema",
2194 "db/call_implies.schema",
2195 "db/return_implies.schema",
2196 "db/function_ptr.schema",
2197 "db/local_values.schema",
2198 "db/function_type_value.schema",
2199 "db/type_value.schema",
2200 "db/function_type.schema",
2201 "db/data_info.schema",
2202 "db/parameter_name.schema",
2203 "db/constraints.schema",
2204 "db/constraints_required.schema",
2205 "db/fn_ptr_data_link.schema",
2206 "db/fn_data_link.schema",
2207 "db/mtag_about.schema",
2208 "db/mtag_map.schema",
2209 "db/mtag_data.schema",
2210 "db/mtag_alias.schema",
2212 static char buf[4096];
2213 int fd;
2214 int ret;
2215 int i;
2217 rc = sqlite3_open(":memory:", &mem_db);
2218 if (rc != SQLITE_OK) {
2219 sm_ierror("starting In-Memory database.");
2220 return;
2223 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2224 fd = open_schema_file(schema_files[i]);
2225 if (fd < 0)
2226 continue;
2227 ret = read(fd, buf, sizeof(buf));
2228 if (ret < 0) {
2229 sm_ierror("failed to read: %s", schema_files[i]);
2230 continue;
2232 close(fd);
2233 if (ret == sizeof(buf)) {
2234 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2235 schema_files[i], sizeof(buf));
2236 continue;
2238 buf[ret] = '\0';
2239 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2240 if (rc != SQLITE_OK) {
2241 sm_ierror("SQL error #2: %s", err);
2242 sm_ierror("%s", buf);
2247 static void init_cachedb(void)
2249 char *err = NULL;
2250 int rc;
2251 const char *schema_files[] = {
2252 "db/call_implies.schema",
2253 "db/return_implies.schema",
2254 "db/type_info.schema",
2255 "db/mtag_data.schema",
2256 "db/sink_info.schema",
2258 static char buf[4096];
2259 int fd;
2260 int ret;
2261 int i;
2263 rc = sqlite3_open(":memory:", &cache_db);
2264 if (rc != SQLITE_OK) {
2265 sm_ierror("starting In-Memory database.");
2266 return;
2269 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2270 fd = open_schema_file(schema_files[i]);
2271 if (fd < 0)
2272 continue;
2273 ret = read(fd, buf, sizeof(buf));
2274 if (ret < 0) {
2275 sm_ierror("failed to read: %s", schema_files[i]);
2276 continue;
2278 close(fd);
2279 if (ret == sizeof(buf)) {
2280 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2281 schema_files[i], sizeof(buf));
2282 continue;
2284 buf[ret] = '\0';
2285 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2286 if (rc != SQLITE_OK) {
2287 sm_ierror("SQL error #2: %s", err);
2288 sm_ierror("%s", buf);
2293 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2295 static char buf[4096];
2296 char tmp[256];
2297 char *p = buf;
2298 char *table = _table;
2299 int i;
2302 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2303 for (i = 0; i < argc; i++) {
2304 if (i)
2305 p += snprintf(p, 4096 - (p - buf), ", ");
2306 sqlite3_snprintf(sizeof(tmp), tmp, "%q", escape_newlines(argv[i]));
2307 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2310 p += snprintf(p, 4096 - (p - buf), ");");
2311 if (p - buf > 4096)
2312 return 0;
2314 sm_msg("SQL: %s", buf);
2315 return 0;
2318 static void dump_cache(struct symbol_list *sym_list)
2320 if (!option_info)
2321 return;
2322 cache_sql(&save_cache_data, (char *)"type_info", "select * from type_info;");
2323 cache_sql(&save_cache_data, (char *)"return_implies", "select * from return_implies;");
2324 cache_sql(&save_cache_data, (char *)"call_implies", "select * from call_implies;");
2325 cache_sql(&save_cache_data, (char *)"mtag_data", "select * from mtag_data;");
2326 cache_sql(&save_cache_data, (char *)"sink_info", "select * from sink_info;");
2329 void open_smatch_db(char *db_file)
2331 int rc;
2333 if (option_no_db)
2334 return;
2336 use_states = malloc(num_checks + 1);
2337 memset(use_states, 0xff, num_checks + 1);
2339 init_memdb();
2340 init_cachedb();
2342 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2343 if (rc != SQLITE_OK) {
2344 option_no_db = 1;
2345 return;
2347 run_sql(NULL, NULL,
2348 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2349 return;
2352 static void register_common_funcs(void)
2354 struct token *token;
2355 char *func;
2356 char filename[256];
2358 if (option_project == PROJ_NONE)
2359 strcpy(filename, "common_functions");
2360 else
2361 snprintf(filename, 256, "%s.common_functions", option_project_str);
2363 token = get_tokens_file(filename);
2364 if (!token)
2365 return;
2366 if (token_type(token) != TOKEN_STREAMBEGIN)
2367 return;
2368 token = token->next;
2369 while (token_type(token) != TOKEN_STREAMEND) {
2370 if (token_type(token) != TOKEN_IDENT)
2371 return;
2372 func = alloc_string(show_ident(token->ident));
2373 add_ptr_list(&common_funcs, func);
2374 token = token->next;
2376 clear_token_alloc();
2379 static char *get_next_string(char **str)
2381 static char string[256];
2382 char *start;
2383 char *p = *str;
2384 int len, i, j;
2386 if (*p == '\0')
2387 return NULL;
2388 start = p;
2390 while (*p != '\0' && *p != '\n') {
2391 if (*p == '\\' && *(p + 1) == ' ') {
2392 p += 2;
2393 continue;
2395 if (*p == ' ')
2396 break;
2397 p++;
2400 len = p - start;
2401 if (len >= sizeof(string)) {
2402 memcpy(string, start, sizeof(string));
2403 string[sizeof(string) - 1] = '\0';
2404 sm_ierror("return_fix: '%s' too long", string);
2405 **str = '\0';
2406 return NULL;
2408 memcpy(string, start, len);
2409 string[len] = '\0';
2410 for (i = 0; i < sizeof(string) - 1; i++) {
2411 if (string[i] == '\\' && string[i + 1] == ' ') {
2412 for (j = i; string[j] != '\0'; j++)
2413 string[j] = string[j + 1];
2416 if (*p != '\0')
2417 p++;
2418 *str = p;
2419 return string;
2422 static void register_return_replacements(void)
2424 char *func, *orig, *new;
2425 char filename[256];
2426 char buf[4096];
2427 int fd, ret, i;
2428 char *p;
2430 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2431 fd = open_schema_file(filename);
2432 if (fd < 0)
2433 return;
2434 ret = read(fd, buf, sizeof(buf));
2435 close(fd);
2436 if (ret < 0)
2437 return;
2438 if (ret == sizeof(buf)) {
2439 sm_ierror("file too large: %s (limit %zd bytes)",
2440 filename, sizeof(buf));
2441 return;
2443 buf[ret] = '\0';
2445 p = buf;
2446 while (*p) {
2447 get_next_string(&p);
2448 replace_count++;
2450 if (replace_count == 0 || replace_count % 3 != 0) {
2451 replace_count = 0;
2452 return;
2454 replace_table = malloc(replace_count * sizeof(char *));
2456 p = buf;
2457 i = 0;
2458 while (*p) {
2459 func = alloc_string(get_next_string(&p));
2460 orig = alloc_string(get_next_string(&p));
2461 new = alloc_string(get_next_string(&p));
2463 replace_table[i++] = func;
2464 replace_table[i++] = orig;
2465 replace_table[i++] = new;
2469 void register_definition_db_callbacks(int id)
2471 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2472 add_hook(&match_call_info_new, FUNCTION_CALL_HOOK);
2473 add_split_return_callback(match_return_info);
2474 add_split_return_callback(print_returned_struct_members);
2475 add_hook(&call_return_state_hooks, RETURN_HOOK);
2476 add_hook(&match_end_func_info, END_FUNC_HOOK);
2477 add_hook(&match_after_func, AFTER_FUNC_HOOK);
2479 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2480 add_hook(&match_call_implies, FUNC_DEF_HOOK);
2481 add_hook(&match_return_implies, CALL_HOOK_AFTER_INLINE);
2483 register_common_funcs();
2484 register_return_replacements();
2486 add_hook(&dump_cache, END_FILE_HOOK);
2489 void register_db_call_marker(int id)
2491 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2494 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
2496 struct expression *arg;
2497 char *name = NULL;
2498 char member_name[256];
2500 *sym = NULL;
2502 if (param == -1) {
2503 const char *star = "";
2505 if (expr->type != EXPR_ASSIGNMENT)
2506 return NULL;
2507 if (get_type(expr->left) == &int_ctype && strcmp(key, "$") != 0)
2508 return NULL;
2509 name = expr_to_var_sym(expr->left, sym);
2510 if (!name)
2511 return NULL;
2512 if (key[0] == '*') {
2513 star = "*";
2514 key++;
2516 if (strncmp(key, "$", 1) != 0)
2517 return name;
2518 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
2519 free_string(name);
2520 return alloc_string(member_name);
2523 while (expr->type == EXPR_ASSIGNMENT)
2524 expr = strip_expr(expr->right);
2525 if (expr->type != EXPR_CALL)
2526 return NULL;
2528 arg = get_argument_from_call_expr(expr->args, param);
2529 if (!arg)
2530 return NULL;
2532 return get_variable_from_key(arg, key, sym);
2535 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
2537 char buf[256];
2538 char *tmp;
2539 int star_cnt = 0;
2541 if (!arg)
2542 return NULL;
2544 arg = strip_expr(arg);
2546 if (strcmp(key, "$") == 0)
2547 return expr_to_var_sym(arg, sym);
2549 if (strcmp(key, "*$") == 0) {
2550 if (arg->type == EXPR_PREOP && arg->op == '&') {
2551 arg = strip_expr(arg->unop);
2552 return expr_to_var_sym(arg, sym);
2553 } else {
2554 tmp = expr_to_var_sym(arg, sym);
2555 if (!tmp)
2556 return NULL;
2557 snprintf(buf, sizeof(buf), "*%s", tmp);
2558 free_string(tmp);
2559 return alloc_string(buf);
2563 while (key[0] == '*') {
2564 star_cnt++;
2565 key++;
2568 if (arg->type == EXPR_PREOP && arg->op == '&' && star_cnt) {
2569 arg = strip_expr(arg->unop);
2570 star_cnt--;
2573 if (arg->type == EXPR_PREOP && arg->op == '&') {
2574 arg = strip_expr(arg->unop);
2575 tmp = expr_to_var_sym(arg, sym);
2576 if (!tmp)
2577 return NULL;
2578 snprintf(buf, sizeof(buf), "%.*s%s.%s",
2579 star_cnt, "**********", tmp, key + 3);
2580 return alloc_string(buf);
2583 tmp = expr_to_var_sym(arg, sym);
2584 if (!tmp)
2585 return NULL;
2586 snprintf(buf, sizeof(buf), "%.*s%s%s", star_cnt, "**********", tmp, key + 1);
2587 free_string(tmp);
2588 return alloc_string(buf);
2591 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
2593 *vsl = NULL;
2595 if (strcmp("$", key) == 0)
2596 return expr_to_chunk_sym_vsl(arg, sym, vsl);
2597 return get_variable_from_key(arg, key, sym);
2600 const char *state_name_to_param_name(const char *state_name, const char *param_name)
2602 int star_cnt = 0;
2603 int name_len;
2604 char buf[256];
2606 name_len = strlen(param_name);
2608 while (state_name[0] == '*') {
2609 star_cnt++;
2610 state_name++;
2613 /* ten out of ten stars! */
2614 if (star_cnt > 10)
2615 return NULL;
2617 if (strcmp(state_name, param_name) == 0) {
2618 snprintf(buf, sizeof(buf), "%.*s$", star_cnt, "**********");
2619 return alloc_sname(buf);
2622 if (state_name[name_len] == '-' && /* check for '-' from "->" */
2623 strncmp(state_name, param_name, name_len) == 0) {
2624 snprintf(buf, sizeof(buf), "%.*s$%s", star_cnt, "**********", state_name + name_len);
2625 return alloc_sname(buf);
2627 return NULL;
2630 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
2632 if (!sym || !sym->ident)
2633 return NULL;
2635 return state_name_to_param_name(name, sym->ident->name);
2638 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym)
2640 struct symbol *type;
2641 const char *sym_name;
2642 int name_len;
2643 static char buf[256];
2646 * mtag_name is different from param_name because mtags can be a struct
2647 * instead of a struct pointer. But we want to treat it like a pointer
2648 * because really an mtag is a pointer. Or in other words, if you pass
2649 * a struct foo then you want to talk about foo.bar but with an mtag
2650 * you want to refer to it as foo->bar.
2654 if (!sym || !sym->ident)
2655 return NULL;
2657 type = get_real_base_type(sym);
2658 if (type && type->type == SYM_BASETYPE)
2659 return "*$";
2661 sym_name = sym->ident->name;
2662 name_len = strlen(sym_name);
2664 if (state_name[name_len] == '.' && /* check for '-' from "->" */
2665 strncmp(state_name, sym_name, name_len) == 0) {
2666 snprintf(buf, sizeof(buf), "$->%s", state_name + name_len + 1);
2667 return buf;
2670 return state_name_to_param_name(state_name, sym_name);
2673 const char *get_mtag_name_expr(struct expression *expr)
2675 char *name;
2676 struct symbol *sym;
2677 const char *ret = NULL;
2679 name = expr_to_var_sym(expr, &sym);
2680 if (!name || !sym)
2681 goto free;
2683 ret = get_mtag_name_var_sym(name, sym);
2684 free:
2685 free_string(name);
2686 return ret;
2689 const char *get_param_name(struct sm_state *sm)
2691 return get_param_name_var_sym(sm->name, sm->sym);
2694 char *get_data_info_name(struct expression *expr)
2696 struct symbol *sym;
2697 char *name;
2698 char buf[256];
2699 char *ret = NULL;
2701 expr = strip_expr(expr);
2702 name = get_member_name(expr);
2703 if (name)
2704 return name;
2705 name = expr_to_var_sym(expr, &sym);
2706 if (!name || !sym)
2707 goto free;
2708 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2709 goto free;
2710 if (sym->ctype.modifiers & MOD_STATIC)
2711 snprintf(buf, sizeof(buf), "static %s", name);
2712 else
2713 snprintf(buf, sizeof(buf), "global %s", name);
2714 ret = alloc_sname(buf);
2715 free:
2716 free_string(name);
2717 return ret;