extra: delete match_untracked_array()
[smatch.git] / smatch_db.c
bloba55c208d105a1db31d7752859a01bfcd42cba7d9
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 <sqlite3.h>
21 #include <unistd.h>
22 #include <ctype.h>
23 #include "smatch.h"
24 #include "smatch_slist.h"
25 #include "smatch_extra.h"
27 static sqlite3 *db;
28 static sqlite3 *mem_db;
30 static int return_id;
32 #define sql_insert_helper(table, ignore, values...) \
33 do { \
34 if (__inline_fn) { \
35 char buf[1024]; \
36 char *err, *p = buf; \
37 int rc; \
39 if (!mem_db) \
40 break; \
42 p += snprintf(p, buf + sizeof(buf) - p, \
43 "insert %sinto %s values (", \
44 ignore ? "or ignore " : "", #table); \
45 p += snprintf(p, buf + sizeof(buf) - p, values); \
46 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
47 sm_debug("in-mem: %s\n", buf); \
48 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err); \
49 if (rc != SQLITE_OK) { \
50 fprintf(stderr, "SQL error #2: %s\n", err); \
51 fprintf(stderr, "SQL: '%s'\n", buf); \
52 parse_error = 1; \
53 } \
54 break; \
55 } \
56 if (option_info) { \
57 sm_prefix(); \
58 sm_printf("SQL: insert %sinto " #table " values(", \
59 ignore ? "or ignore " : ""); \
60 sm_printf(values); \
61 sm_printf(");\n"); \
62 } \
63 } while (0)
65 #define sql_insert(table, values...) sql_insert_helper(table, 0, values);
66 #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, 1, values);
68 struct def_callback {
69 int hook_type;
70 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
72 ALLOCATOR(def_callback, "definition db hook callbacks");
73 DECLARE_PTR_LIST(callback_list, struct def_callback);
74 static struct callback_list *select_caller_info_callbacks;
76 struct member_info_callback {
77 int owner;
78 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
80 ALLOCATOR(member_info_callback, "caller_info callbacks");
81 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
82 static struct member_info_cb_list *member_callbacks;
84 struct returned_state_callback {
85 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
87 ALLOCATOR(returned_state_callback, "returned state callbacks");
88 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
89 static struct returned_state_cb_list *returned_state_callbacks;
91 struct returned_member_callback {
92 int owner;
93 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
95 ALLOCATOR(returned_member_callback, "returned member callbacks");
96 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
97 static struct returned_member_cb_list *returned_member_callbacks;
99 struct call_implies_callback {
100 int type;
101 void (*callback)(struct expression *arg, char *key, char *value);
103 ALLOCATOR(call_implies_callback, "call_implies callbacks");
104 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
105 static struct call_implies_cb_list *call_implies_cb_list;
107 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
109 int i;
111 for (i = 0; i < argc; i++) {
112 if (i != 0)
113 printf(", ");
114 sm_printf("%s", argv[i]);
116 sm_printf("\n");
117 return 0;
120 void debug_sql(const char *sql)
122 if (!option_debug)
123 return;
124 sm_msg("%s", sql);
125 sql_exec(print_sql_output, NULL, sql);
128 void debug_mem_sql(const char *sql)
130 if (!option_debug)
131 return;
132 sm_msg("%s", sql);
133 sql_mem_exec(print_sql_output, NULL, sql);
136 void sql_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
138 char *err = NULL;
139 int rc;
141 if (option_no_db || !db)
142 return;
144 rc = sqlite3_exec(db, sql, callback, data, &err);
145 if (rc != SQLITE_OK && !parse_error) {
146 fprintf(stderr, "SQL error #2: %s\n", err);
147 fprintf(stderr, "SQL: '%s'\n", sql);
148 parse_error = 1;
152 void sql_mem_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
154 char *err = NULL;
155 int rc;
157 if (!mem_db)
158 return;
160 rc = sqlite3_exec(mem_db, sql, callback, data, &err);
161 if (rc != SQLITE_OK) {
162 fprintf(stderr, "SQL error #2: %s\n", err);
163 fprintf(stderr, "SQL: '%s'\n", sql);
164 parse_error = 1;
168 static int replace_count;
169 static char **replace_table;
170 static const char *replace_return_ranges(const char *return_ranges)
172 int i;
174 if (!get_function()) {
175 /* I have no idea why EXPORT_SYMBOL() is here */
176 return return_ranges;
178 for (i = 0; i < replace_count; i += 3) {
179 if (strcmp(replace_table[i + 0], get_function()) == 0) {
180 if (strcmp(replace_table[i + 1], return_ranges) == 0)
181 return replace_table[i + 2];
184 return return_ranges;
187 void sql_insert_return_states(int return_id, const char *return_ranges,
188 int type, int param, const char *key, const char *value)
190 if (key && strlen(key) >= 80)
191 return;
192 return_ranges = replace_return_ranges(return_ranges);
193 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
194 get_base_file(), get_function(), (unsigned long)__inline_fn,
195 return_id, return_ranges, fn_static(), type, param, key, value);
198 static struct string_list *common_funcs;
199 static int is_common_function(const char *fn)
201 char *tmp;
203 if (!fn)
204 return 0;
206 if (strncmp(fn, "__builtin_", 10) == 0)
207 return 1;
209 FOR_EACH_PTR(common_funcs, tmp) {
210 if (strcmp(tmp, fn) == 0)
211 return 1;
212 } END_FOR_EACH_PTR(tmp);
214 return 0;
217 static char *function_signature(void)
219 return type_to_str(get_real_base_type(cur_func_sym));
222 void sql_insert_caller_info(struct expression *call, int type,
223 int param, const char *key, const char *value)
225 char *fn;
227 if (!option_info && !__inline_call)
228 return;
230 if (key && strlen(key) >= 80)
231 return;
233 fn = get_fnptr_name(call->fn);
234 if (!fn)
235 return;
237 if (__inline_call) {
238 mem_sql(NULL, NULL,
239 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
240 get_base_file(), get_function(), fn, (unsigned long)call,
241 is_static(call->fn), type, param, key, value);
244 if (!option_info)
245 return;
247 if (strncmp(fn, "__builtin_", 10) == 0)
248 return;
250 sm_msg("SQL_caller_info: insert into caller_info values ("
251 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
252 get_base_file(), get_function(), fn, is_static(call->fn),
253 type, param, key, value);
255 free_string(fn);
258 void sql_insert_function_ptr(const char *fn, const char *struct_name)
260 sql_insert(function_ptr, "'%s', '%s', '%s', 0", get_base_file(), fn,
261 struct_name);
264 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
266 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'", get_base_file(),
267 get_function(), (unsigned long)__inline_fn, fn_static(),
268 type, param, key, value);
271 void sql_insert_function_type_size(const char *member, const char *ranges)
273 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
276 void sql_insert_local_values(const char *name, const char *value)
278 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
281 void sql_insert_function_type_value(const char *type, const char *value)
283 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
286 void sql_insert_function_type(int param, const char *value)
288 sql_insert(function_type, "'%s', '%s', %d, %d, '%s'",
289 get_base_file(), get_function(), fn_static(), param, value);
292 void sql_insert_parameter_name(int param, const char *value)
294 sql_insert(parameter_name, "'%s', '%s', %d, %d, '%s'",
295 get_base_file(), get_function(), fn_static(), param, value);
298 void sql_insert_data_info(struct expression *data, int type, const char *value)
300 char *data_name;
302 data_name = get_data_info_name(data);
303 if (!data_name)
304 return;
305 sql_insert(data_info, "'%s', '%s', %d, '%s'",
306 is_static(data) ? get_base_file() : "extern",
307 data_name, type, value);
310 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
312 sql_insert(data_info, "'%s', '%s', %d, '%s'",
313 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
314 var, type, value);
317 void sql_save_constraint(const char *con)
319 if (!option_info)
320 return;
322 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", con);
325 void sql_save_constraint_required(const char *data, int op, const char *limit)
327 sql_insert_or_ignore(constraints_required, "'%s', '%s', '%s'", data, show_special(op), limit);
330 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data)
332 sql_insert(fn_ptr_data_link, "'%s', '%s'", ptr, data);
335 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value)
337 if (fn->type != EXPR_SYMBOL || !fn->symbol->ident)
338 return;
340 sql_insert(fn_data_link, "'%s', '%s', %d, %d, %d, '%s', '%s'",
341 (fn->symbol->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
342 fn->symbol->ident->name,
343 !!(fn->symbol->ctype.modifiers & MOD_STATIC),
344 type, param, key, value);
347 char *get_static_filter(struct symbol *sym)
349 static char sql_filter[1024];
351 /* This can only happen on buggy code. Return invalid SQL. */
352 if (!sym) {
353 sql_filter[0] = '\0';
354 return sql_filter;
357 if (sym->ctype.modifiers & MOD_STATIC) {
358 snprintf(sql_filter, sizeof(sql_filter),
359 "file = '%s' and function = '%s' and static = '1'",
360 get_base_file(), sym->ident->name);
361 } else {
362 snprintf(sql_filter, sizeof(sql_filter),
363 "function = '%s' and static = '0'", sym->ident->name);
366 return sql_filter;
369 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
371 int *row_count = _row_count;
373 *row_count = 0;
374 if (argc != 1)
375 return 0;
376 *row_count = atoi(argv[0]);
377 return 0;
380 static void mark_params_untracked(struct expression *call)
382 struct expression *arg;
383 int i = 0;
385 FOR_EACH_PTR(call->args, arg) {
386 mark_untracked(call, i++, "$", NULL);
387 } END_FOR_EACH_PTR(arg);
390 static void sql_select_return_states_pointer(const char *cols,
391 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
393 char *ptr;
394 int return_count = 0;
396 ptr = get_fnptr_name(call->fn);
397 if (!ptr)
398 return;
400 run_sql(get_row_count, &return_count,
401 "select count(*) from return_states join function_ptr "
402 "where return_states.function == function_ptr.function and "
403 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
404 /* The magic number 100 is just from testing on the kernel. */
405 if (return_count > 100) {
406 mark_params_untracked(call);
407 return;
410 run_sql(callback, info,
411 "select %s from return_states join function_ptr where "
412 "return_states.function == function_ptr.function and ptr = '%s' "
413 "and searchable = 1 "
414 "order by function_ptr.file, return_states.file, return_id, type;",
415 cols, ptr);
418 static int is_local_symbol(struct expression *expr)
420 if (expr->type != EXPR_SYMBOL)
421 return 0;
422 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
423 return 0;
424 return 1;
427 void sql_select_return_states(const char *cols, struct expression *call,
428 int (*callback)(void*, int, char**, char**), void *info)
430 int row_count = 0;
432 if (is_fake_call(call))
433 return;
435 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol || is_local_symbol(call->fn)) {
436 sql_select_return_states_pointer(cols, call, callback, info);
437 return;
440 if (inlinable(call->fn)) {
441 mem_sql(callback, info,
442 "select %s from return_states where call_id = '%lu' order by return_id, type;",
443 cols, (unsigned long)call);
444 return;
447 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
448 get_static_filter(call->fn->symbol));
449 if (row_count > 3000)
450 return;
452 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
453 cols, get_static_filter(call->fn->symbol));
456 void sql_select_call_implies(const char *cols, struct expression *call,
457 int (*callback)(void*, int, char**, char**))
459 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
460 return;
462 if (inlinable(call->fn)) {
463 mem_sql(callback, call,
464 "select %s from call_implies where call_id = '%lu';",
465 cols, (unsigned long)call);
466 return;
469 run_sql(callback, call, "select %s from call_implies where %s;",
470 cols, get_static_filter(call->fn->symbol));
473 struct select_caller_info_data {
474 struct stree *final_states;
475 int prev_func_id;
476 int ignore;
479 static void sql_select_caller_info(struct select_caller_info_data *data,
480 const char *cols, struct symbol *sym,
481 int (*callback)(void*, int, char**, char**))
483 if (__inline_fn) {
484 mem_sql(callback, data,
485 "select %s from caller_info where call_id = %lu;",
486 cols, (unsigned long)__inline_fn);
487 return;
490 if (sym->ident->name && is_common_function(sym->ident->name))
491 return;
493 run_sql(callback, data,
494 "select %s from caller_info where %s order by call_id;",
495 cols, get_static_filter(sym));
498 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
500 struct def_callback *def_callback = __alloc_def_callback(0);
502 def_callback->hook_type = type;
503 def_callback->callback = callback;
504 add_ptr_list(&select_caller_info_callbacks, def_callback);
508 * These call backs are used when the --info option is turned on to print struct
509 * member information. For example foo->bar could have a state in
510 * smatch_extra.c and also check_user.c.
512 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
514 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
516 member_callback->owner = owner;
517 member_callback->callback = callback;
518 add_ptr_list(&member_callbacks, member_callback);
521 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
523 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
525 callback->callback = fn;
526 add_ptr_list(&returned_state_callbacks, callback);
529 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state))
531 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
533 member_callback->owner = owner;
534 member_callback->callback = callback;
535 add_ptr_list(&returned_member_callbacks, member_callback);
538 void select_call_implies_hook(int type, void (*callback)(struct expression *arg, char *key, char *value))
540 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
542 cb->type = type;
543 cb->callback = callback;
544 add_ptr_list(&call_implies_cb_list, cb);
547 struct return_info {
548 struct expression *static_returns_call;
549 struct symbol *return_type;
550 struct range_list *return_range_list;
553 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
555 struct return_info *ret_info = _ret_info;
556 struct range_list *rl;
557 struct expression *call_expr = ret_info->static_returns_call;
559 if (argc != 1)
560 return 0;
561 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
562 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
563 return 0;
566 struct range_list *db_return_vals(struct expression *expr)
568 struct return_info ret_info = {};
569 char buf[64];
570 struct sm_state *sm;
572 if (is_fake_call(expr))
573 return NULL;
575 snprintf(buf, sizeof(buf), "return %p", expr);
576 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
577 if (sm)
578 return clone_rl(estate_rl(sm->state));
579 ret_info.static_returns_call = expr;
580 ret_info.return_type = get_type(expr);
581 if (!ret_info.return_type)
582 return NULL;
584 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
585 return NULL;
587 ret_info.return_range_list = NULL;
588 if (inlinable(expr->fn)) {
589 mem_sql(db_return_callback, &ret_info,
590 "select distinct return from return_states where call_id = '%lu';",
591 (unsigned long)expr);
592 } else {
593 run_sql(db_return_callback, &ret_info,
594 "select distinct return from return_states where %s;",
595 get_static_filter(expr->fn->symbol));
597 return ret_info.return_range_list;
600 struct range_list *db_return_vals_from_str(const char *fn_name)
602 struct return_info ret_info;
604 ret_info.static_returns_call = NULL;
605 ret_info.return_type = &llong_ctype;
606 ret_info.return_range_list = NULL;
608 run_sql(db_return_callback, &ret_info,
609 "select distinct return from return_states where function = '%s';",
610 fn_name);
611 return ret_info.return_range_list;
614 static void match_call_marker(struct expression *expr)
616 struct symbol *type;
618 type = get_type(expr->fn);
619 if (type && type->type == SYM_PTR)
620 type = get_real_base_type(type);
623 * we just want to record something in the database so that if we have
624 * two calls like: frob(4); frob(some_unkown); then on the receiving
625 * side we know that sometimes frob is called with unknown parameters.
628 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", type_to_str(type));
631 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
632 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
634 struct sm_state *sm;
635 char *name;
636 struct symbol *sym;
637 int len;
638 char printed_name[256];
639 int is_address = 0;
641 expr = strip_expr(expr);
642 if (expr->type == EXPR_PREOP && expr->op == '&') {
643 expr = strip_expr(expr->unop);
644 is_address = 1;
647 name = expr_to_var_sym(expr, &sym);
648 if (!name || !sym)
649 goto free;
651 len = strlen(name);
652 FOR_EACH_SM(stree, sm) {
653 if (sm->sym != sym)
654 continue;
655 if (strcmp(name, sm->name) == 0) {
656 if (is_address)
657 snprintf(printed_name, sizeof(printed_name), "*$");
658 else /* these are already handled. fixme: handle them here */
659 continue;
660 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
661 snprintf(printed_name, sizeof(printed_name), "*$");
662 } else if (strncmp(name, sm->name, len) == 0) {
663 if (isalnum(sm->name[len]))
664 continue;
665 if (is_address)
666 snprintf(printed_name, sizeof(printed_name), "$->%s", sm->name + len + 1);
667 else
668 snprintf(printed_name, sizeof(printed_name), "$%s", sm->name + len);
669 } else {
670 continue;
672 callback(call, param, printed_name, sm);
673 } END_FOR_EACH_SM(sm);
674 free:
675 free_string(name);
678 static void match_call_info(struct expression *call)
680 struct member_info_callback *cb;
681 struct expression *arg;
682 struct stree *stree;
683 char *name;
684 int i;
686 name = get_fnptr_name(call->fn);
687 if (!name)
688 return;
690 FOR_EACH_PTR(member_callbacks, cb) {
691 stree = get_all_states_stree(cb->owner);
692 i = 0;
693 FOR_EACH_PTR(call->args, arg) {
694 print_struct_members(call, arg, i, stree, cb->callback);
695 i++;
696 } END_FOR_EACH_PTR(arg);
697 free_stree(&stree);
698 } END_FOR_EACH_PTR(cb);
700 free_string(name);
703 static int get_param(int param, char **name, struct symbol **sym)
705 struct symbol *arg;
706 int i;
708 i = 0;
709 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
711 * this is a temporary hack to work around a bug (I think in sparse?)
712 * 2.6.37-rc1:fs/reiserfs/journal.o
713 * If there is a function definition without parameter name found
714 * after a function implementation then it causes a crash.
715 * int foo() {}
716 * int bar(char *);
718 if (arg->ident->name < (char *)100)
719 continue;
720 if (i == param) {
721 *name = arg->ident->name;
722 *sym = arg;
723 return TRUE;
725 i++;
726 } END_FOR_EACH_PTR(arg);
728 return FALSE;
731 static int function_signature_matches(const char *sig)
733 char *my_sig;
735 my_sig = function_signature();
736 if (!sig || !my_sig)
737 return 1; /* default to matching */
738 if (strcmp(my_sig, sig) == 0)
739 return 1;
740 return 0;
743 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
745 struct select_caller_info_data *data = _data;
746 int func_id;
747 long type;
748 long param;
749 char *key;
750 char *value;
751 char *name = NULL;
752 struct symbol *sym = NULL;
753 struct def_callback *def_callback;
754 struct stree *stree;
756 if (argc != 5)
757 return 0;
759 func_id = atoi(argv[0]);
760 errno = 0;
761 type = strtol(argv[1], NULL, 10);
762 param = strtol(argv[2], NULL, 10);
763 if (errno)
764 return 0;
765 key = argv[3];
766 value = argv[4];
768 if (data->prev_func_id == -1)
769 data->prev_func_id = func_id;
770 if (func_id != data->prev_func_id) {
771 stree = __pop_fake_cur_stree();
772 if (!data->ignore)
773 merge_stree(&data->final_states, stree);
774 free_stree(&stree);
775 __push_fake_cur_stree();
776 __unnullify_path();
777 data->prev_func_id = func_id;
778 data->ignore = 0;
781 if (data->ignore)
782 return 0;
783 if (type == INTERNAL &&
784 !function_signature_matches(value)) {
785 data->ignore = 1;
786 return 0;
789 if (param >= 0 && !get_param(param, &name, &sym))
790 return 0;
792 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
793 if (def_callback->hook_type == type)
794 def_callback->callback(name, sym, key, value);
795 } END_FOR_EACH_PTR(def_callback);
797 return 0;
800 static void get_direct_callers(struct select_caller_info_data *data, struct symbol *sym)
802 sql_select_caller_info(data,
803 "call_id, type, parameter, key, value", sym,
804 caller_info_callback);
807 static struct string_list *ptr_names_done;
808 static struct string_list *ptr_names;
810 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
812 insert_string(&ptr_names, alloc_string(argv[0]));
813 return 0;
816 static char *get_next_ptr_name(void)
818 char *ptr;
820 FOR_EACH_PTR(ptr_names, ptr) {
821 if (list_has_string(ptr_names_done, ptr))
822 continue;
823 insert_string(&ptr_names_done, ptr);
824 return ptr;
825 } END_FOR_EACH_PTR(ptr);
826 return NULL;
829 static void get_ptr_names(const char *file, const char *name)
831 char sql_filter[1024];
832 int before, after;
834 if (file) {
835 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
836 file, name);
837 } else {
838 snprintf(sql_filter, 1024, "function = '%s';", name);
841 before = ptr_list_size((struct ptr_list *)ptr_names);
843 run_sql(get_ptr_name, NULL,
844 "select distinct ptr from function_ptr where %s",
845 sql_filter);
847 after = ptr_list_size((struct ptr_list *)ptr_names);
848 if (before == after)
849 return;
851 while ((name = get_next_ptr_name()))
852 get_ptr_names(NULL, name);
855 static void match_data_from_db(struct symbol *sym)
857 struct select_caller_info_data data = { .prev_func_id = -1 };
858 struct sm_state *sm;
859 struct stree *stree;
861 if (!sym || !sym->ident)
862 return;
864 __push_fake_cur_stree();
865 __unnullify_path();
867 if (!__inline_fn) {
868 char *ptr;
870 if (sym->ctype.modifiers & MOD_STATIC)
871 get_ptr_names(get_base_file(), sym->ident->name);
872 else
873 get_ptr_names(NULL, sym->ident->name);
875 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
876 __free_ptr_list((struct ptr_list **)&ptr_names);
877 __free_ptr_list((struct ptr_list **)&ptr_names_done);
878 stree = __pop_fake_cur_stree();
879 free_stree(&stree);
880 return;
883 get_direct_callers(&data, sym);
885 stree = __pop_fake_cur_stree();
886 if (!data.ignore)
887 merge_stree(&data.final_states, stree);
888 free_stree(&stree);
889 __push_fake_cur_stree();
890 __unnullify_path();
891 data.prev_func_id = -1;
892 data.ignore = 0;
894 FOR_EACH_PTR(ptr_names, ptr) {
895 run_sql(caller_info_callback, &data,
896 "select call_id, type, parameter, key, value"
897 " from caller_info where function = '%s' order by call_id",
898 ptr);
899 free_string(ptr);
900 } END_FOR_EACH_PTR(ptr);
902 __free_ptr_list((struct ptr_list **)&ptr_names);
903 __free_ptr_list((struct ptr_list **)&ptr_names_done);
904 } else {
905 get_direct_callers(&data, sym);
908 stree = __pop_fake_cur_stree();
909 if (!data.ignore)
910 merge_stree(&data.final_states, stree);
911 free_stree(&stree);
913 FOR_EACH_SM(data.final_states, sm) {
914 __set_sm(sm);
915 } END_FOR_EACH_SM(sm);
917 free_stree(&data.final_states);
920 static int call_implies_callbacks(void *_call, int argc, char **argv, char **azColName)
922 struct expression *call_expr = _call;
923 struct call_implies_callback *cb;
924 struct expression *arg = NULL;
925 int type;
926 int param;
928 if (argc != 5)
929 return 0;
931 type = atoi(argv[1]);
932 param = atoi(argv[2]);
934 FOR_EACH_PTR(call_implies_cb_list, cb) {
935 if (cb->type != type)
936 continue;
937 if (param != -1) {
938 arg = get_argument_from_call_expr(call_expr->args, param);
939 if (!arg)
940 continue;
942 cb->callback(arg, argv[3], argv[4]);
943 } END_FOR_EACH_PTR(cb);
945 return 0;
948 static void match_call_implies(struct expression *expr)
950 sql_select_call_implies("function, type, parameter, key, value", expr,
951 call_implies_callbacks);
954 static void print_initializer_list(struct expression_list *expr_list,
955 struct symbol *struct_type)
957 struct expression *expr;
958 struct symbol *base_type;
959 char struct_name[256];
961 FOR_EACH_PTR(expr_list, expr) {
962 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
963 print_initializer_list(expr->idx_expression->expr_list, struct_type);
964 continue;
966 if (expr->type != EXPR_IDENTIFIER)
967 continue;
968 if (!expr->expr_ident)
969 continue;
970 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
971 continue;
972 base_type = get_type(expr->ident_expression);
973 if (!base_type || base_type->type != SYM_FN)
974 continue;
975 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
976 struct_type->ident->name, expr->expr_ident->name);
977 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
978 struct_name);
979 } END_FOR_EACH_PTR(expr);
982 static void global_variable(struct symbol *sym)
984 struct symbol *struct_type;
986 if (!sym->ident)
987 return;
988 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
989 return;
990 struct_type = get_base_type(sym);
991 if (!struct_type)
992 return;
993 if (struct_type->type == SYM_ARRAY) {
994 struct_type = get_base_type(struct_type);
995 if (!struct_type)
996 return;
998 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
999 return;
1000 print_initializer_list(sym->initializer->expr_list, struct_type);
1003 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1005 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1008 static void call_return_state_hooks_conditional(struct expression *expr)
1010 struct returned_state_callback *cb;
1011 struct range_list *rl;
1012 char *return_ranges;
1013 int final_pass_orig = final_pass;
1015 __push_fake_cur_stree();
1017 final_pass = 0;
1018 __split_whole_condition(expr->conditional);
1019 final_pass = final_pass_orig;
1021 if (get_implied_rl(expr->cond_true, &rl))
1022 rl = cast_rl(cur_func_return_type(), rl);
1023 else
1024 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
1025 return_ranges = show_rl(rl);
1026 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1028 return_id++;
1029 FOR_EACH_PTR(returned_state_callbacks, cb) {
1030 cb->callback(return_id, return_ranges, expr->cond_true);
1031 } END_FOR_EACH_PTR(cb);
1033 __push_true_states();
1034 __use_false_states();
1036 if (get_implied_rl(expr->cond_false, &rl))
1037 rl = cast_rl(cur_func_return_type(), rl);
1038 else
1039 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
1040 return_ranges = show_rl(rl);
1041 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1043 return_id++;
1044 FOR_EACH_PTR(returned_state_callbacks, cb) {
1045 cb->callback(return_id, return_ranges, expr->cond_false);
1046 } END_FOR_EACH_PTR(cb);
1048 __merge_true_states();
1049 __free_fake_cur_stree();
1052 static void call_return_state_hooks_compare(struct expression *expr)
1054 struct returned_state_callback *cb;
1055 char *return_ranges;
1056 int final_pass_orig = final_pass;
1057 sval_t sval = { .type = &int_ctype };
1058 sval_t ret;
1060 if (!get_implied_value(expr, &ret))
1061 ret.value = -1;
1063 __push_fake_cur_stree();
1065 final_pass = 0;
1066 __split_whole_condition(expr);
1067 final_pass = final_pass_orig;
1069 if (ret.value != 0) {
1070 return_ranges = alloc_sname("1");
1071 sval.value = 1;
1072 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1074 return_id++;
1075 FOR_EACH_PTR(returned_state_callbacks, cb) {
1076 cb->callback(return_id, return_ranges, expr);
1077 } END_FOR_EACH_PTR(cb);
1080 __push_true_states();
1081 __use_false_states();
1083 if (ret.value != 1) {
1084 return_ranges = alloc_sname("0");
1085 sval.value = 0;
1086 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1088 return_id++;
1089 FOR_EACH_PTR(returned_state_callbacks, cb) {
1090 cb->callback(return_id, return_ranges, expr);
1091 } END_FOR_EACH_PTR(cb);
1094 __merge_true_states();
1095 __free_fake_cur_stree();
1098 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1100 struct sm_state *tmp;
1102 FOR_EACH_PTR(slist, tmp) {
1103 if (strcmp(tmp->state->name, sm->state->name) == 0)
1104 return 1;
1105 } END_FOR_EACH_PTR(tmp);
1107 return 0;
1110 static char *get_return_compare_str(struct expression *expr)
1112 char *compare_str;
1113 char *var;
1114 char buf[256];
1115 int comparison;
1116 int param;
1118 compare_str = expr_lte_to_param(expr, -1);
1119 if (compare_str)
1120 return compare_str;
1121 param = get_param_num(expr);
1122 if (param < 0)
1123 return NULL;
1125 var = expr_to_var(expr);
1126 if (!var)
1127 return NULL;
1128 snprintf(buf, sizeof(buf), "%s orig", var);
1129 comparison = get_comparison_strings(var, buf);
1130 free_string(var);
1132 if (!comparison)
1133 return NULL;
1135 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1136 return alloc_sname(buf);
1139 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1141 struct returned_state_callback *cb;
1142 struct range_list *rl;
1143 char *return_ranges;
1144 struct sm_state *tmp;
1145 int ret = 0;
1146 int nr_possible, nr_states;
1147 char *compare_str = NULL;
1148 char buf[128];
1149 struct state_list *already_handled = NULL;
1151 if (!sm || !sm->merged)
1152 return 0;
1154 if (too_many_possible(sm))
1155 return 0;
1157 /* bail if it gets too complicated */
1158 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1159 nr_states = stree_count(__get_cur_stree());
1160 if (nr_states * nr_possible >= 2000)
1161 return 0;
1163 FOR_EACH_PTR(sm->possible, tmp) {
1164 if (tmp->merged)
1165 continue;
1166 if (ptr_in_list(tmp, already_handled))
1167 continue;
1168 add_ptr_list(&already_handled, tmp);
1170 ret = 1;
1171 __push_fake_cur_stree();
1173 overwrite_states_using_pool(sm, tmp);
1175 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1176 return_ranges = show_rl(rl);
1177 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1178 compare_str = get_return_compare_str(expr);
1179 if (compare_str) {
1180 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1181 return_ranges = alloc_sname(buf);
1184 return_id++;
1185 FOR_EACH_PTR(returned_state_callbacks, cb) {
1186 cb->callback(return_id, return_ranges, expr);
1187 } END_FOR_EACH_PTR(cb);
1189 __free_fake_cur_stree();
1190 } END_FOR_EACH_PTR(tmp);
1192 free_slist(&already_handled);
1194 return ret;
1197 static int call_return_state_hooks_split_possible(struct expression *expr)
1199 struct sm_state *sm;
1201 if (!expr || expr_equal_to_param(expr, -1))
1202 return 0;
1204 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1205 return split_possible_helper(sm, expr);
1208 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1210 struct range_list *rl;
1211 char *return_ranges;
1212 sval_t sval;
1213 char *compare_str;
1214 char *math_str;
1215 char buf[128];
1217 *rl_p = NULL;
1219 if (!expr)
1220 return alloc_sname("");
1222 if (get_implied_value(expr, &sval)) {
1223 sval = sval_cast(cur_func_return_type(), sval);
1224 *rl_p = alloc_rl(sval, sval);
1225 return sval_to_str(sval);
1228 compare_str = expr_equal_to_param(expr, -1);
1229 math_str = get_value_in_terms_of_parameter_math(expr);
1231 if (get_implied_rl(expr, &rl)) {
1232 rl = cast_rl(cur_func_return_type(), rl);
1233 return_ranges = show_rl(rl);
1234 } else if (get_imaginary_absolute(expr, &rl)){
1235 rl = cast_rl(cur_func_return_type(), rl);
1236 return alloc_sname(show_rl(rl));
1237 } else {
1238 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1239 return_ranges = show_rl(rl);
1241 *rl_p = rl;
1243 if (compare_str) {
1244 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1245 return alloc_sname(buf);
1248 if (math_str) {
1249 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1250 return alloc_sname(buf);
1253 compare_str = get_return_compare_str(expr);
1254 if (compare_str) {
1255 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1256 return alloc_sname(buf);
1258 return return_ranges;
1261 static int split_positive_from_negative(struct expression *expr)
1263 struct returned_state_callback *cb;
1264 struct range_list *rl;
1265 const char *return_ranges;
1266 struct range_list *ret_rl;
1267 int undo;
1269 /* We're going to print the states 3 times */
1270 if (stree_count(__get_cur_stree()) > 10000 / 3)
1271 return 0;
1273 if (!get_implied_rl(expr, &rl) || !rl)
1274 return 0;
1275 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1276 return 0;
1277 /* Forget about INT_MAX and larger */
1278 if (rl_max(rl).value <= 0)
1279 return 0;
1280 if (!sval_is_negative(rl_min(rl)))
1281 return 0;
1283 if (!assume(compare_expression(expr, '>', zero_expr())))
1284 return 0;
1286 return_id++;
1287 return_ranges = get_return_ranges_str(expr, &ret_rl);
1288 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1289 FOR_EACH_PTR(returned_state_callbacks, cb) {
1290 cb->callback(return_id, (char *)return_ranges, expr);
1291 } END_FOR_EACH_PTR(cb);
1293 end_assume();
1295 if (rl_has_sval(rl, sval_type_val(rl_type(rl), 0))) {
1296 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1298 return_id++;
1299 return_ranges = get_return_ranges_str(expr, &ret_rl);
1300 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1301 FOR_EACH_PTR(returned_state_callbacks, cb) {
1302 cb->callback(return_id, (char *)return_ranges, expr);
1303 } END_FOR_EACH_PTR(cb);
1305 if (undo)
1306 end_assume();
1309 undo = assume(compare_expression(expr, '<', zero_expr()));
1311 return_id++;
1312 return_ranges = get_return_ranges_str(expr, &ret_rl);
1313 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1314 FOR_EACH_PTR(returned_state_callbacks, cb) {
1315 cb->callback(return_id, (char *)return_ranges, expr);
1316 } END_FOR_EACH_PTR(cb);
1318 if (undo)
1319 end_assume();
1321 return 1;
1324 static int call_return_state_hooks_split_null_non_null(struct expression *expr)
1326 struct returned_state_callback *cb;
1327 struct range_list *rl;
1328 struct range_list *nonnull_rl;
1329 sval_t null_sval;
1330 struct range_list *null_rl = NULL;
1331 char *return_ranges;
1332 struct sm_state *sm;
1333 struct smatch_state *state;
1334 int nr_states;
1335 int final_pass_orig = final_pass;
1337 if (!expr || expr_equal_to_param(expr, -1))
1338 return 0;
1339 if (expr->type == EXPR_CALL)
1340 return 0;
1341 if (!is_pointer(expr))
1342 return 0;
1344 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1345 if (!sm)
1346 return 0;
1347 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1348 return 0;
1349 state = sm->state;
1350 if (!estate_rl(state))
1351 return 0;
1352 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1353 return 0;
1354 if (!rl_has_sval(estate_rl(state), sval_type_val(estate_type(state), 0)))
1355 return 0;
1357 nr_states = stree_count(__get_cur_stree());
1358 if (option_info && nr_states >= 1500)
1359 return 0;
1361 rl = estate_rl(state);
1363 __push_fake_cur_stree();
1365 final_pass = 0;
1366 __split_whole_condition(expr);
1367 final_pass = final_pass_orig;
1369 nonnull_rl = rl_filter(rl, rl_zero());
1370 return_ranges = show_rl(nonnull_rl);
1371 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1373 return_id++;
1374 FOR_EACH_PTR(returned_state_callbacks, cb) {
1375 cb->callback(return_id, return_ranges, expr);
1376 } END_FOR_EACH_PTR(cb);
1378 __push_true_states();
1379 __use_false_states();
1381 return_ranges = alloc_sname("0");
1382 null_sval = sval_type_val(rl_type(rl), 0);
1383 add_range(&null_rl, null_sval, null_sval);
1384 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1385 return_id++;
1386 FOR_EACH_PTR(returned_state_callbacks, cb) {
1387 cb->callback(return_id, return_ranges, expr);
1388 } END_FOR_EACH_PTR(cb);
1390 __merge_true_states();
1391 __free_fake_cur_stree();
1393 return 1;
1396 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1398 struct range_list *rl;
1399 struct range_list *nonzero_rl;
1400 sval_t zero_sval;
1401 struct range_list *zero_rl = NULL;
1402 int nr_states;
1403 struct returned_state_callback *cb;
1404 char *return_ranges;
1405 int final_pass_orig = final_pass;
1406 sval_t val;
1408 if (option_project != PROJ_KERNEL)
1409 return 0;
1411 nr_states = stree_count(__get_cur_stree());
1412 if (nr_states > 1500)
1413 return 0;
1415 if (get_value(expr, &val))
1416 return 0;
1417 if (!get_implied_rl(expr, &rl))
1418 return 0;
1419 if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1420 return 0;
1421 if (rl_max(rl).value != 0)
1422 return 0;
1424 __push_fake_cur_stree();
1426 final_pass = 0;
1427 __split_whole_condition(expr);
1428 final_pass = final_pass_orig;
1430 nonzero_rl = rl_filter(rl, rl_zero());
1431 return_ranges = show_rl(nonzero_rl);
1432 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1434 return_id++;
1435 FOR_EACH_PTR(returned_state_callbacks, cb) {
1436 cb->callback(return_id, return_ranges, expr);
1437 } END_FOR_EACH_PTR(cb);
1439 __push_true_states();
1440 __use_false_states();
1442 return_ranges = alloc_sname("0");
1443 zero_sval = sval_type_val(rl_type(rl), 0);
1444 add_range(&zero_rl, zero_sval, zero_sval);
1445 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1446 return_id++;
1447 FOR_EACH_PTR(returned_state_callbacks, cb) {
1448 cb->callback(return_id, return_ranges, expr);
1449 } END_FOR_EACH_PTR(cb);
1451 __merge_true_states();
1452 __free_fake_cur_stree();
1454 return 1;
1457 static int is_boolean(struct expression *expr)
1459 struct range_list *rl;
1461 if (!get_implied_rl(expr, &rl))
1462 return 0;
1463 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1464 return 1;
1465 return 0;
1468 static int is_conditional(struct expression *expr)
1470 if (!expr)
1471 return 0;
1472 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1473 return 1;
1474 return 0;
1477 static int splitable_function_call(struct expression *expr)
1479 struct sm_state *sm;
1480 char buf[64];
1482 if (!expr || expr->type != EXPR_CALL)
1483 return 0;
1484 snprintf(buf, sizeof(buf), "return %p", expr);
1485 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1486 return split_possible_helper(sm, expr);
1489 static struct sm_state *find_bool_param(void)
1491 struct stree *start_states;
1492 struct symbol *arg;
1493 struct sm_state *sm, *tmp;
1494 sval_t sval;
1496 start_states = get_start_states();
1498 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1499 if (!arg->ident)
1500 continue;
1501 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1502 if (!sm)
1503 continue;
1504 if (rl_min(estate_rl(sm->state)).value != 0 ||
1505 rl_max(estate_rl(sm->state)).value != 1)
1506 continue;
1507 goto found;
1508 } END_FOR_EACH_PTR_REVERSE(arg);
1510 return NULL;
1512 found:
1514 * Check if it's splitable. If not, then splitting it up is likely not
1515 * useful for the callers.
1517 FOR_EACH_PTR(sm->possible, tmp) {
1518 if (is_merged(tmp))
1519 continue;
1520 if (!estate_get_single_value(tmp->state, &sval))
1521 return NULL;
1522 } END_FOR_EACH_PTR(tmp);
1524 return sm;
1527 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1529 struct returned_state_callback *cb;
1530 struct range_list *ret_rl;
1531 const char *return_ranges;
1532 struct sm_state *tmp;
1533 int ret = 0;
1534 int nr_possible, nr_states;
1535 char *compare_str = NULL;
1536 char buf[128];
1537 struct state_list *already_handled = NULL;
1539 if (!sm || !sm->merged)
1540 return 0;
1542 if (too_many_possible(sm))
1543 return 0;
1545 /* bail if it gets too complicated */
1546 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1547 nr_states = stree_count(__get_cur_stree());
1548 if (nr_states * nr_possible >= 2000)
1549 return 0;
1551 FOR_EACH_PTR(sm->possible, tmp) {
1552 if (tmp->merged)
1553 continue;
1554 if (ptr_in_list(tmp, already_handled))
1555 continue;
1556 add_ptr_list(&already_handled, tmp);
1558 ret = 1;
1559 __push_fake_cur_stree();
1561 overwrite_states_using_pool(sm, tmp);
1563 return_ranges = get_return_ranges_str(expr, &ret_rl);
1564 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1565 compare_str = get_return_compare_str(expr);
1566 if (compare_str) {
1567 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1568 return_ranges = alloc_sname(buf);
1571 return_id++;
1572 FOR_EACH_PTR(returned_state_callbacks, cb) {
1573 cb->callback(return_id, (char *)return_ranges, expr);
1574 } END_FOR_EACH_PTR(cb);
1576 __free_fake_cur_stree();
1577 } END_FOR_EACH_PTR(tmp);
1579 free_slist(&already_handled);
1581 return ret;
1584 static int split_by_bool_param(struct expression *expr)
1586 struct sm_state *start_sm, *sm;
1587 sval_t sval;
1589 start_sm = find_bool_param();
1590 if (!start_sm)
1591 return 0;
1592 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
1593 if (!sm || estate_get_single_value(sm->state, &sval))
1594 return 0;
1595 return split_on_bool_sm(sm, expr);
1598 static int split_by_null_nonnull_param(struct expression *expr)
1600 struct symbol *arg;
1601 struct sm_state *sm;
1603 /* function must only take one pointer */
1604 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
1605 return 0;
1606 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
1607 if (!arg->ident)
1608 return 0;
1609 if (get_real_base_type(arg)->type != SYM_PTR)
1610 return 0;
1612 if (param_was_set_var_sym(arg->ident->name, arg))
1613 return 0;
1614 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
1615 if (!sm)
1616 return 0;
1618 return split_on_bool_sm(sm, expr);
1621 static void call_return_state_hooks(struct expression *expr)
1623 struct returned_state_callback *cb;
1624 struct range_list *ret_rl;
1625 const char *return_ranges;
1626 int nr_states;
1627 sval_t sval;
1629 if (__path_is_null())
1630 return;
1632 expr = strip_expr(expr);
1634 if (is_impossible_path())
1635 goto vanilla;
1637 if (expr && (expr->type == EXPR_COMPARE ||
1638 !get_implied_value(expr, &sval)) &&
1639 (is_condition(expr) || is_boolean(expr))) {
1640 call_return_state_hooks_compare(expr);
1641 return;
1642 } else if (is_conditional(expr)) {
1643 call_return_state_hooks_conditional(expr);
1644 return;
1645 } else if (call_return_state_hooks_split_possible(expr)) {
1646 return;
1647 } else if (call_return_state_hooks_split_null_non_null(expr)) {
1648 return;
1649 } else if (call_return_state_hooks_split_success_fail(expr)) {
1650 return;
1651 } else if (splitable_function_call(expr)) {
1652 return;
1653 } else if (split_positive_from_negative(expr)) {
1654 return;
1655 } else if (split_by_bool_param(expr)) {
1656 } else if (split_by_null_nonnull_param(expr)) {
1657 return;
1660 vanilla:
1661 return_ranges = get_return_ranges_str(expr, &ret_rl);
1662 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1664 return_id++;
1665 nr_states = stree_count(__get_cur_stree());
1666 if (nr_states >= 10000) {
1667 match_return_info(return_id, (char *)return_ranges, expr);
1668 return;
1670 FOR_EACH_PTR(returned_state_callbacks, cb) {
1671 cb->callback(return_id, (char *)return_ranges, expr);
1672 } END_FOR_EACH_PTR(cb);
1675 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1677 struct returned_member_callback *cb;
1678 struct stree *stree;
1679 struct sm_state *sm;
1680 struct symbol *type;
1681 char *name;
1682 char member_name[256];
1683 int len;
1685 type = get_type(expr);
1686 if (!type || type->type != SYM_PTR)
1687 return;
1688 name = expr_to_var(expr);
1689 if (!name)
1690 return;
1692 member_name[sizeof(member_name) - 1] = '\0';
1693 strcpy(member_name, "$");
1695 len = strlen(name);
1696 FOR_EACH_PTR(returned_member_callbacks, cb) {
1697 stree = __get_cur_stree();
1698 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1699 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1700 strcpy(member_name, "*$");
1701 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1702 continue;
1704 if (strncmp(sm->name, name, len) != 0)
1705 continue;
1706 if (strncmp(sm->name + len, "->", 2) != 0)
1707 continue;
1708 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
1709 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1710 } END_FOR_EACH_SM(sm);
1711 } END_FOR_EACH_PTR(cb);
1713 free_string(name);
1716 static void reset_memdb(struct symbol *sym)
1718 mem_sql(NULL, NULL, "delete from caller_info;");
1719 mem_sql(NULL, NULL, "delete from return_states;");
1720 mem_sql(NULL, NULL, "delete from call_implies;");
1723 static void match_end_func_info(struct symbol *sym)
1725 if (__path_is_null())
1726 return;
1727 call_return_state_hooks(NULL);
1730 static void match_after_func(struct symbol *sym)
1732 if (!__inline_fn)
1733 reset_memdb(sym);
1736 static void init_memdb(void)
1738 char *err = NULL;
1739 int rc;
1740 const char *schema_files[] = {
1741 "db/db.schema",
1742 "db/caller_info.schema",
1743 "db/return_states.schema",
1744 "db/function_type_size.schema",
1745 "db/type_size.schema",
1746 "db/call_implies.schema",
1747 "db/function_ptr.schema",
1748 "db/local_values.schema",
1749 "db/function_type_value.schema",
1750 "db/type_value.schema",
1751 "db/function_type.schema",
1752 "db/data_info.schema",
1753 "db/parameter_name.schema",
1754 "db/constraints.schema",
1755 "db/constraints_required.schema",
1756 "db/fn_ptr_data_link.schema",
1757 "db/fn_data_link.schema",
1759 static char buf[4096];
1760 int fd;
1761 int ret;
1762 int i;
1764 rc = sqlite3_open(":memory:", &mem_db);
1765 if (rc != SQLITE_OK) {
1766 printf("Error starting In-Memory database.");
1767 return;
1770 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1771 fd = open_data_file(schema_files[i]);
1772 if (fd < 0) {
1773 mem_db = NULL;
1774 return;
1776 ret = read(fd, buf, sizeof(buf));
1777 if (ret == sizeof(buf)) {
1778 printf("Schema file too large: %s (limit %zd bytes)",
1779 schema_files[i], sizeof(buf));
1781 buf[ret] = '\0';
1782 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
1783 if (rc != SQLITE_OK) {
1784 fprintf(stderr, "SQL error #2: %s\n", err);
1785 fprintf(stderr, "%s\n", buf);
1790 void open_smatch_db(void)
1792 int rc;
1794 if (option_no_db)
1795 return;
1797 init_memdb();
1799 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1800 if (rc != SQLITE_OK) {
1801 option_no_db = 1;
1802 return;
1804 return;
1807 static void register_common_funcs(void)
1809 struct token *token;
1810 char *func;
1811 char filename[256];
1813 if (option_project == PROJ_NONE)
1814 strcpy(filename, "common_functions");
1815 else
1816 snprintf(filename, 256, "%s.common_functions", option_project_str);
1818 token = get_tokens_file(filename);
1819 if (!token)
1820 return;
1821 if (token_type(token) != TOKEN_STREAMBEGIN)
1822 return;
1823 token = token->next;
1824 while (token_type(token) != TOKEN_STREAMEND) {
1825 if (token_type(token) != TOKEN_IDENT)
1826 return;
1827 func = alloc_string(show_ident(token->ident));
1828 add_ptr_list(&common_funcs, func);
1829 token = token->next;
1831 clear_token_alloc();
1834 static char *get_next_string(char **str)
1836 static char string[256];
1837 char *start;
1838 char *p = *str;
1839 int len;
1841 if (*p == '\0')
1842 return NULL;
1843 start = p;
1845 while (*p != '\0' && *p != ' ' && *p != '\n')
1846 p++;
1848 len = p - start;
1849 if (len > 256) {
1850 memcpy(string, start, 255);
1851 string[255] = '\0';
1852 printf("return_fix: '%s' too long", string);
1853 **str = '\0';
1854 return NULL;
1856 memcpy(string, start, len);
1857 string[len] = '\0';
1858 if (*p != '\0')
1859 p++;
1860 *str = p;
1861 return string;
1864 static void register_return_replacements(void)
1866 char *func, *orig, *new;
1867 char filename[256];
1868 char buf[4096];
1869 int fd, ret, i;
1870 char *p;
1872 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
1873 fd = open_data_file(filename);
1874 if (fd < 0)
1875 return;
1876 ret = read(fd, buf, sizeof(buf));
1877 close(fd);
1878 if (ret < 0)
1879 return;
1880 if (ret == sizeof(buf)) {
1881 printf("file too large: %s (limit %zd bytes)",
1882 filename, sizeof(buf));
1883 return;
1885 buf[ret] = '\0';
1887 p = buf;
1888 while (*p) {
1889 get_next_string(&p);
1890 replace_count++;
1892 if (replace_count == 0 || replace_count % 3 != 0) {
1893 replace_count = 0;
1894 return;
1896 replace_table = malloc(replace_count * sizeof(char *));
1898 p = buf;
1899 i = 0;
1900 while (*p) {
1901 func = alloc_string(get_next_string(&p));
1902 orig = alloc_string(get_next_string(&p));
1903 new = alloc_string(get_next_string(&p));
1905 replace_table[i++] = func;
1906 replace_table[i++] = orig;
1907 replace_table[i++] = new;
1911 void register_definition_db_callbacks(int id)
1913 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1914 add_hook(&global_variable, BASE_HOOK);
1915 add_hook(&global_variable, DECLARATION_HOOK);
1916 add_split_return_callback(match_return_info);
1917 add_split_return_callback(print_returned_struct_members);
1918 add_hook(&call_return_state_hooks, RETURN_HOOK);
1919 add_hook(&match_end_func_info, END_FUNC_HOOK);
1920 add_hook(&match_after_func, AFTER_FUNC_HOOK);
1922 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1923 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1925 register_common_funcs();
1926 register_return_replacements();
1929 void register_db_call_marker(int id)
1931 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1934 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
1936 struct expression *arg;
1937 char *name = NULL;
1938 char member_name[256];
1940 *sym = NULL;
1942 if (param == -1) {
1943 const char *star = "";
1945 if (expr->type != EXPR_ASSIGNMENT)
1946 return NULL;
1947 name = expr_to_var_sym(expr->left, sym);
1948 if (!name)
1949 return NULL;
1950 if (key[0] == '*') {
1951 star = "*";
1952 key++;
1954 if (strncmp(key, "$", 1) != 0)
1955 return name;
1956 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
1957 free_string(name);
1958 return alloc_string(member_name);
1961 while (expr->type == EXPR_ASSIGNMENT)
1962 expr = strip_expr(expr->right);
1963 if (expr->type != EXPR_CALL)
1964 return NULL;
1966 arg = get_argument_from_call_expr(expr->args, param);
1967 if (!arg)
1968 return NULL;
1970 return get_variable_from_key(arg, key, sym);
1973 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
1975 char buf[256];
1976 char *tmp;
1978 if (!arg)
1979 return NULL;
1981 arg = strip_expr(arg);
1983 if (strcmp(key, "$") == 0)
1984 return expr_to_var_sym(arg, sym);
1986 if (strcmp(key, "*$") == 0) {
1987 if (arg->type == EXPR_PREOP && arg->op == '&') {
1988 arg = strip_expr(arg->unop);
1989 return expr_to_var_sym(arg, sym);
1990 } else {
1991 tmp = expr_to_var_sym(arg, sym);
1992 if (!tmp)
1993 return NULL;
1994 snprintf(buf, sizeof(buf), "*%s", tmp);
1995 free_string(tmp);
1996 return alloc_string(buf);
2000 if (arg->type == EXPR_PREOP && arg->op == '&') {
2001 arg = strip_expr(arg->unop);
2002 tmp = expr_to_var_sym(arg, sym);
2003 if (!tmp)
2004 return NULL;
2005 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
2006 return alloc_string(buf);
2009 tmp = expr_to_var_sym(arg, sym);
2010 if (!tmp)
2011 return NULL;
2012 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
2013 free_string(tmp);
2014 return alloc_string(buf);
2017 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
2019 *vsl = NULL;
2021 if (strcmp("$", key) == 0)
2022 return expr_to_chunk_sym_vsl(arg, sym, vsl);
2023 return get_variable_from_key(arg, key, sym);
2026 const char *state_name_to_param_name(const char *state_name, const char *param_name)
2028 int name_len;
2029 static char buf[256];
2031 name_len = strlen(param_name);
2033 if (strcmp(state_name, param_name) == 0) {
2034 return "$";
2035 } else if (state_name[name_len] == '-' && /* check for '-' from "->" */
2036 strncmp(state_name, param_name, name_len) == 0) {
2037 snprintf(buf, sizeof(buf), "$%s", state_name + name_len);
2038 return buf;
2039 } else if (state_name[0] == '*' && strcmp(state_name + 1, param_name) == 0) {
2040 return "*$";
2042 return NULL;
2045 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
2047 if (!sym || !sym->ident)
2048 return NULL;
2050 return state_name_to_param_name(name, sym->ident->name);
2053 const char *get_param_name(struct sm_state *sm)
2055 return get_param_name_var_sym(sm->name, sm->sym);
2058 char *get_data_info_name(struct expression *expr)
2060 struct symbol *sym;
2061 char *name;
2062 char buf[256];
2063 char *ret = NULL;
2065 expr = strip_expr(expr);
2066 name = get_member_name(expr);
2067 if (name)
2068 return name;
2069 name = expr_to_var_sym(expr, &sym);
2070 if (!name || !sym)
2071 goto free;
2072 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2073 goto free;
2074 if (sym->ctype.modifiers & MOD_STATIC)
2075 snprintf(buf, sizeof(buf), "static %s", name);
2076 else
2077 snprintf(buf, sizeof(buf), "global %s", name);
2078 ret = alloc_sname(buf);
2079 free:
2080 free_string(name);
2081 return ret;