assigned_expr: remove debug code
[smatch.git] / smatch_db.c
blobe8888286d2eb61fbb1a0da5032e758f3abca53d3
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, late, 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 FILE *tmp_fd = sm_outfd; \
58 sm_outfd = sql_outfd; \
59 sm_prefix(); \
60 sm_printf("SQL%s: insert %sinto " #table " values(", \
61 late ? "_late" : "", ignore ? "or ignore " : ""); \
62 sm_printf(values); \
63 sm_printf(");\n"); \
64 sm_outfd = tmp_fd; \
65 } \
66 } while (0)
68 #define sql_insert(table, values...) sql_insert_helper(table, 0, 0, values);
69 #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, 1, 0, values);
70 #define sql_insert_late(table, values...) sql_insert_helper(table, 0, 1, values);
72 struct def_callback {
73 int hook_type;
74 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
76 ALLOCATOR(def_callback, "definition db hook callbacks");
77 DECLARE_PTR_LIST(callback_list, struct def_callback);
78 static struct callback_list *select_caller_info_callbacks;
80 struct member_info_callback {
81 int owner;
82 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
84 ALLOCATOR(member_info_callback, "caller_info callbacks");
85 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
86 static struct member_info_cb_list *member_callbacks;
88 struct returned_state_callback {
89 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
91 ALLOCATOR(returned_state_callback, "returned state callbacks");
92 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
93 static struct returned_state_cb_list *returned_state_callbacks;
95 struct returned_member_callback {
96 int owner;
97 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
99 ALLOCATOR(returned_member_callback, "returned member callbacks");
100 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
101 static struct returned_member_cb_list *returned_member_callbacks;
103 struct call_implies_callback {
104 int type;
105 void (*callback)(struct expression *call, struct expression *arg, char *key, char *value);
107 ALLOCATOR(call_implies_callback, "call_implies callbacks");
108 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
109 static struct call_implies_cb_list *call_implies_cb_list;
111 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
113 int i;
115 for (i = 0; i < argc; i++) {
116 if (i != 0)
117 printf(", ");
118 sm_printf("%s", argv[i]);
120 sm_printf("\n");
121 return 0;
124 void debug_sql(const char *sql)
126 if (!option_debug)
127 return;
128 sm_msg("%s", sql);
129 sql_exec(print_sql_output, NULL, sql);
132 void debug_mem_sql(const char *sql)
134 if (!option_debug)
135 return;
136 sm_msg("%s", sql);
137 sql_mem_exec(print_sql_output, NULL, sql);
140 void sql_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
142 char *err = NULL;
143 int rc;
145 if (option_no_db || !db)
146 return;
148 rc = sqlite3_exec(db, sql, callback, data, &err);
149 if (rc != SQLITE_OK && !parse_error) {
150 fprintf(stderr, "SQL error #2: %s\n", err);
151 fprintf(stderr, "SQL: '%s'\n", sql);
152 parse_error = 1;
156 void sql_mem_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
158 char *err = NULL;
159 int rc;
161 if (!mem_db)
162 return;
164 rc = sqlite3_exec(mem_db, sql, callback, data, &err);
165 if (rc != SQLITE_OK) {
166 fprintf(stderr, "SQL error #2: %s\n", err);
167 fprintf(stderr, "SQL: '%s'\n", sql);
168 parse_error = 1;
172 static int replace_count;
173 static char **replace_table;
174 static const char *replace_return_ranges(const char *return_ranges)
176 int i;
178 if (!get_function()) {
179 /* I have no idea why EXPORT_SYMBOL() is here */
180 return return_ranges;
182 for (i = 0; i < replace_count; i += 3) {
183 if (strcmp(replace_table[i + 0], get_function()) == 0) {
184 if (strcmp(replace_table[i + 1], return_ranges) == 0)
185 return replace_table[i + 2];
188 return return_ranges;
191 void sql_insert_return_states(int return_id, const char *return_ranges,
192 int type, int param, const char *key, const char *value)
194 if (key && strlen(key) >= 80)
195 return;
196 return_ranges = replace_return_ranges(return_ranges);
197 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
198 get_base_file(), get_function(), (unsigned long)__inline_fn,
199 return_id, return_ranges, fn_static(), type, param, key, value);
202 static struct string_list *common_funcs;
203 static int is_common_function(const char *fn)
205 char *tmp;
207 if (!fn)
208 return 0;
210 if (strncmp(fn, "__builtin_", 10) == 0)
211 return 1;
213 FOR_EACH_PTR(common_funcs, tmp) {
214 if (strcmp(tmp, fn) == 0)
215 return 1;
216 } END_FOR_EACH_PTR(tmp);
218 return 0;
221 static char *function_signature(void)
223 return type_to_str(get_real_base_type(cur_func_sym));
226 void sql_insert_caller_info(struct expression *call, int type,
227 int param, const char *key, const char *value)
229 FILE *tmp_fd = sm_outfd;
230 char *fn;
232 if (!option_info && !__inline_call)
233 return;
235 if (key && strlen(key) >= 80)
236 return;
238 fn = get_fnptr_name(call->fn);
239 if (!fn)
240 return;
242 if (__inline_call) {
243 mem_sql(NULL, NULL,
244 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
245 get_base_file(), get_function(), fn, (unsigned long)call,
246 is_static(call->fn), type, param, key, value);
249 if (!option_info)
250 return;
252 if (strncmp(fn, "__builtin_", 10) == 0)
253 return;
254 if (type != INTERNAL && is_common_function(fn))
255 return;
257 sm_outfd = caller_info_fd;
258 sm_msg("SQL_caller_info: insert into caller_info values ("
259 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
260 get_base_file(), get_function(), fn, is_static(call->fn),
261 type, param, key, value);
262 sm_outfd = tmp_fd;
264 free_string(fn);
267 void sql_insert_function_ptr(const char *fn, const char *struct_name)
269 sql_insert(function_ptr, "'%s', '%s', '%s', 0", get_base_file(), fn,
270 struct_name);
273 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
275 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'", get_base_file(),
276 get_function(), (unsigned long)__inline_fn, fn_static(),
277 type, param, key, value);
280 void sql_insert_function_type_size(const char *member, const char *ranges)
282 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
285 void sql_insert_local_values(const char *name, const char *value)
287 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
290 void sql_insert_function_type_value(const char *type, const char *value)
292 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
295 void sql_insert_function_type(int param, const char *value)
297 sql_insert(function_type, "'%s', '%s', %d, %d, '%s'",
298 get_base_file(), get_function(), fn_static(), param, value);
301 void sql_insert_parameter_name(int param, const char *value)
303 sql_insert(parameter_name, "'%s', '%s', %d, %d, '%s'",
304 get_base_file(), get_function(), fn_static(), param, value);
307 void sql_insert_data_info(struct expression *data, int type, const char *value)
309 char *data_name;
311 data_name = get_data_info_name(data);
312 if (!data_name)
313 return;
314 sql_insert(data_info, "'%s', '%s', %d, '%s'",
315 is_static(data) ? get_base_file() : "extern",
316 data_name, type, value);
319 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
321 sql_insert(data_info, "'%s', '%s', %d, '%s'",
322 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
323 var, type, value);
326 void sql_save_constraint(const char *con)
328 if (!option_info)
329 return;
331 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", con);
334 void sql_save_constraint_required(const char *data, int op, const char *limit)
336 sql_insert_or_ignore(constraints_required, "'%s', '%s', '%s'", data, show_special(op), limit);
339 void sql_copy_constraint_required(const char *new_limit, const char *old_limit)
341 if (!option_info)
342 return;
344 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
345 "select constraints_required.data, constraints_required.op, '%s' from "
346 "constraints_required where bound = '%s';", new_limit, old_limit);
349 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data)
351 sql_insert_or_ignore(fn_ptr_data_link, "'%s', '%s'", ptr, data);
354 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value)
356 if (fn->type != EXPR_SYMBOL || !fn->symbol->ident)
357 return;
359 sql_insert(fn_data_link, "'%s', '%s', %d, %d, %d, '%s', '%s'",
360 (fn->symbol->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
361 fn->symbol->ident->name,
362 !!(fn->symbol->ctype.modifiers & MOD_STATIC),
363 type, param, key, value);
366 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name)
368 sql_insert(mtag_about, "%lld, '%s', '%s', %d, '%s', '%s'",
369 tag, get_filename(), get_function(), get_lineno(), left_name, right_name);
372 void sql_insert_mtag_data(mtag_t tag, const char *var, int offset, int type, const char *value)
374 sql_insert(mtag_data, "%lld, '%s', %d, %d, '%s'", tag, var, offset, type, value);
377 void sql_insert_mtag_map(mtag_t tag, int offset, mtag_t container)
379 sql_insert(mtag_map, "%lld, %d, %lld", tag, offset, container);
382 static int save_mtag(void *_tag, int argc, char **argv, char **azColName)
384 mtag_t *tag = _tag;
386 if (!*tag)
387 *tag = strtoll(argv[0], NULL, 10);
388 else
389 *tag = -1ULL;
391 return 0;
394 int mtag_map_select_container(mtag_t tag, int offset, mtag_t *container)
396 mtag_t tmp = 0;
398 run_sql(save_mtag, &tmp,
399 "select container from mtag_map where tag = %lld and offset = %d;",
400 tag, offset);
402 if (tmp == 0 || tmp == -1ULL)
403 return 0;
404 *container = tmp;
405 return 1;
408 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag)
410 mtag_t tmp = 0;
412 run_sql(save_mtag, &tmp,
413 "select tag from mtag_map where container = %lld and offset = %d;",
414 container, offset);
416 if (tmp == 0 || tmp == -1ULL)
417 return 0;
418 *tag = tmp;
419 return 1;
422 char *get_static_filter(struct symbol *sym)
424 static char sql_filter[1024];
426 /* This can only happen on buggy code. Return invalid SQL. */
427 if (!sym) {
428 sql_filter[0] = '\0';
429 return sql_filter;
432 if (sym->ctype.modifiers & MOD_STATIC) {
433 snprintf(sql_filter, sizeof(sql_filter),
434 "file = '%s' and function = '%s' and static = '1'",
435 get_base_file(), sym->ident->name);
436 } else {
437 snprintf(sql_filter, sizeof(sql_filter),
438 "function = '%s' and static = '0'", sym->ident->name);
441 return sql_filter;
444 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
446 int *row_count = _row_count;
448 *row_count = 0;
449 if (argc != 1)
450 return 0;
451 *row_count = atoi(argv[0]);
452 return 0;
455 static void mark_params_untracked(struct expression *call)
457 struct expression *arg;
458 int i = 0;
460 FOR_EACH_PTR(call->args, arg) {
461 mark_untracked(call, i++, "$", NULL);
462 } END_FOR_EACH_PTR(arg);
465 static void sql_select_return_states_pointer(const char *cols,
466 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
468 char *ptr;
469 int return_count = 0;
471 ptr = get_fnptr_name(call->fn);
472 if (!ptr)
473 return;
475 run_sql(get_row_count, &return_count,
476 "select count(*) from return_states join function_ptr "
477 "where return_states.function == function_ptr.function and "
478 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
479 /* The magic number 100 is just from testing on the kernel. */
480 if (return_count > 100) {
481 mark_params_untracked(call);
482 return;
485 run_sql(callback, info,
486 "select %s from return_states join function_ptr where "
487 "return_states.function == function_ptr.function and ptr = '%s' "
488 "and searchable = 1 "
489 "order by function_ptr.file, return_states.file, return_id, type;",
490 cols, ptr);
493 static int is_local_symbol(struct expression *expr)
495 if (expr->type != EXPR_SYMBOL)
496 return 0;
497 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
498 return 0;
499 return 1;
502 void sql_select_return_states(const char *cols, struct expression *call,
503 int (*callback)(void*, int, char**, char**), void *info)
505 int row_count = 0;
507 if (is_fake_call(call))
508 return;
510 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol || is_local_symbol(call->fn)) {
511 sql_select_return_states_pointer(cols, call, callback, info);
512 return;
515 if (inlinable(call->fn)) {
516 mem_sql(callback, info,
517 "select %s from return_states where call_id = '%lu' order by return_id, type;",
518 cols, (unsigned long)call);
519 return;
522 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
523 get_static_filter(call->fn->symbol));
524 if (row_count > 3000)
525 return;
527 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
528 cols, get_static_filter(call->fn->symbol));
531 void sql_select_call_implies(const char *cols, struct expression *call,
532 int (*callback)(void*, int, char**, char**))
534 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
535 return;
537 if (inlinable(call->fn)) {
538 mem_sql(callback, call,
539 "select %s from call_implies where call_id = '%lu';",
540 cols, (unsigned long)call);
541 return;
544 run_sql(callback, call, "select %s from call_implies where %s;",
545 cols, get_static_filter(call->fn->symbol));
548 struct select_caller_info_data {
549 struct stree *final_states;
550 struct timeval start_time;
551 int prev_func_id;
552 int ignore;
553 int results;
556 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName);
558 static void sql_select_caller_info(struct select_caller_info_data *data,
559 const char *cols, struct symbol *sym)
561 if (__inline_fn) {
562 mem_sql(caller_info_callback, data,
563 "select %s from caller_info where call_id = %lu;",
564 cols, (unsigned long)__inline_fn);
565 return;
568 if (sym->ident->name && is_common_function(sym->ident->name))
569 return;
570 run_sql(caller_info_callback, data,
571 "select %s from common_caller_info where %s order by call_id;",
572 cols, get_static_filter(sym));
573 if (data->results)
574 return;
576 run_sql(caller_info_callback, data,
577 "select %s from caller_info where %s order by call_id;",
578 cols, get_static_filter(sym));
581 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
583 struct def_callback *def_callback = __alloc_def_callback(0);
585 def_callback->hook_type = type;
586 def_callback->callback = callback;
587 add_ptr_list(&select_caller_info_callbacks, def_callback);
591 * These call backs are used when the --info option is turned on to print struct
592 * member information. For example foo->bar could have a state in
593 * smatch_extra.c and also check_user.c.
595 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
597 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
599 member_callback->owner = owner;
600 member_callback->callback = callback;
601 add_ptr_list(&member_callbacks, member_callback);
604 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
606 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
608 callback->callback = fn;
609 add_ptr_list(&returned_state_callbacks, callback);
612 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))
614 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
616 member_callback->owner = owner;
617 member_callback->callback = callback;
618 add_ptr_list(&returned_member_callbacks, member_callback);
621 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
623 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
625 cb->type = type;
626 cb->callback = callback;
627 add_ptr_list(&call_implies_cb_list, cb);
630 struct return_info {
631 struct expression *static_returns_call;
632 struct symbol *return_type;
633 struct range_list *return_range_list;
636 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
638 struct return_info *ret_info = _ret_info;
639 struct range_list *rl;
640 struct expression *call_expr = ret_info->static_returns_call;
642 if (argc != 1)
643 return 0;
644 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
645 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
646 return 0;
649 struct range_list *db_return_vals(struct expression *expr)
651 struct return_info ret_info = {};
652 char buf[64];
653 struct sm_state *sm;
655 if (is_fake_call(expr))
656 return NULL;
658 snprintf(buf, sizeof(buf), "return %p", expr);
659 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
660 if (sm)
661 return clone_rl(estate_rl(sm->state));
662 ret_info.static_returns_call = expr;
663 ret_info.return_type = get_type(expr);
664 if (!ret_info.return_type)
665 return NULL;
667 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
668 return NULL;
670 ret_info.return_range_list = NULL;
671 if (inlinable(expr->fn)) {
672 mem_sql(db_return_callback, &ret_info,
673 "select distinct return from return_states where call_id = '%lu';",
674 (unsigned long)expr);
675 } else {
676 run_sql(db_return_callback, &ret_info,
677 "select distinct return from return_states where %s;",
678 get_static_filter(expr->fn->symbol));
680 return ret_info.return_range_list;
683 struct range_list *db_return_vals_from_str(const char *fn_name)
685 struct return_info ret_info;
687 ret_info.static_returns_call = NULL;
688 ret_info.return_type = &llong_ctype;
689 ret_info.return_range_list = NULL;
691 run_sql(db_return_callback, &ret_info,
692 "select distinct return from return_states where function = '%s';",
693 fn_name);
694 return ret_info.return_range_list;
697 static void match_call_marker(struct expression *expr)
699 struct symbol *type;
701 type = get_type(expr->fn);
702 if (type && type->type == SYM_PTR)
703 type = get_real_base_type(type);
706 * we just want to record something in the database so that if we have
707 * two calls like: frob(4); frob(some_unkown); then on the receiving
708 * side we know that sometimes frob is called with unknown parameters.
711 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", type_to_str(type));
714 static char *show_offset(int offset)
716 static char buf[64];
718 buf[0] = '\0';
719 if (offset != -1)
720 snprintf(buf, sizeof(buf), "(-%d)", offset);
721 return buf;
724 static void print_struct_members(struct expression *call, struct expression *expr, int param, int offset, struct stree *stree,
725 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
727 struct sm_state *sm;
728 char *name;
729 struct symbol *sym;
730 int len;
731 char printed_name[256];
732 int is_address = 0;
734 expr = strip_expr(expr);
735 if (expr->type == EXPR_PREOP && expr->op == '&') {
736 expr = strip_expr(expr->unop);
737 is_address = 1;
740 name = expr_to_var_sym(expr, &sym);
741 if (!name || !sym)
742 goto free;
744 len = strlen(name);
745 FOR_EACH_SM(stree, sm) {
746 if (sm->sym != sym)
747 continue;
748 if (strcmp(name, sm->name) == 0) {
749 if (is_address)
750 snprintf(printed_name, sizeof(printed_name), "*$%s", show_offset(offset));
751 else /* these are already handled. fixme: handle them here */
752 continue;
753 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
754 snprintf(printed_name, sizeof(printed_name), "*$%s", show_offset(offset));
755 } else if (strncmp(name, sm->name, len) == 0) {
756 if (isalnum(sm->name[len]))
757 continue;
758 if (is_address)
759 snprintf(printed_name, sizeof(printed_name), "$%s->%s", show_offset(offset), sm->name + len + 1);
760 else
761 snprintf(printed_name, sizeof(printed_name), "$%s%s", show_offset(offset), sm->name + len);
762 } else {
763 continue;
765 callback(call, param, printed_name, sm);
766 } END_FOR_EACH_SM(sm);
767 free:
768 free_string(name);
771 static int param_used_callback(void *_container, int argc, char **argv, char **azColName)
773 char **container = _container;
774 static char buf[256];
776 snprintf(buf, sizeof(buf), "%s", argv[0]);
777 *container = buf;
778 return 0;
781 static void print_container_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
782 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
784 struct expression *tmp;
785 char *container = NULL;
786 int offset;
787 int holder_offset;
788 char *p;
790 if (!call->fn || call->fn->type != EXPR_SYMBOL)
791 return;
794 * We can't use the in-mem DB because we have to parse the function
795 * first, then we know if it takes a container, then we know to pass it
796 * the container data.
799 run_sql(&param_used_callback, &container,
800 "select key from call_implies where %s and type = %d and key like '%%$(%%' and parameter = %d limit 1;",
801 get_static_filter(call->fn->symbol), CONTAINER, param);
802 if (!container)
803 return;
805 p = strchr(container, '-');
806 if (!p)
807 return;
808 offset = atoi(p);
809 p = strchr(p, ')');
810 if (!p)
811 return;
812 p++;
814 tmp = get_assigned_expr(expr);
815 if (tmp)
816 expr = tmp;
818 if (expr->type != EXPR_PREOP || expr->op != '&')
819 return;
820 expr = strip_expr(expr->unop);
821 holder_offset = get_member_offset_from_deref(expr);
822 if (-holder_offset != offset)
823 return;
825 expr = strip_expr(expr->deref);
826 if (expr->type == EXPR_PREOP && expr->op == '*')
827 expr = strip_expr(expr->unop);
829 print_struct_members(call, expr, param, holder_offset, stree, callback);
832 static void match_call_info(struct expression *call)
834 struct member_info_callback *cb;
835 struct expression *arg;
836 struct stree *stree;
837 char *name;
838 int i;
840 name = get_fnptr_name(call->fn);
841 if (!name)
842 return;
844 FOR_EACH_PTR(member_callbacks, cb) {
845 stree = get_all_states_stree(cb->owner);
846 i = 0;
847 FOR_EACH_PTR(call->args, arg) {
848 print_struct_members(call, arg, i, -1, stree, cb->callback);
849 print_container_struct_members(call, arg, i, stree, cb->callback);
850 i++;
851 } END_FOR_EACH_PTR(arg);
852 free_stree(&stree);
853 } END_FOR_EACH_PTR(cb);
855 free_string(name);
858 static int get_param(int param, char **name, struct symbol **sym)
860 struct symbol *arg;
861 int i;
863 i = 0;
864 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
866 * this is a temporary hack to work around a bug (I think in sparse?)
867 * 2.6.37-rc1:fs/reiserfs/journal.o
868 * If there is a function definition without parameter name found
869 * after a function implementation then it causes a crash.
870 * int foo() {}
871 * int bar(char *);
873 if (arg->ident->name < (char *)100)
874 continue;
875 if (i == param) {
876 *name = arg->ident->name;
877 *sym = arg;
878 return TRUE;
880 i++;
881 } END_FOR_EACH_PTR(arg);
883 return FALSE;
886 static int function_signature_matches(const char *sig)
888 char *my_sig;
890 my_sig = function_signature();
891 if (!sig || !my_sig)
892 return 1; /* default to matching */
893 if (strcmp(my_sig, sig) == 0)
894 return 1;
895 return 0;
898 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
900 struct select_caller_info_data *data = _data;
901 int func_id;
902 long type;
903 long param;
904 char *key;
905 char *value;
906 char *name = NULL;
907 struct symbol *sym = NULL;
908 struct def_callback *def_callback;
909 struct stree *stree;
910 struct timeval cur_time;
912 data->results = 1;
914 if (argc != 5)
915 return 0;
917 gettimeofday(&cur_time, NULL);
918 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
919 return 0;
921 func_id = atoi(argv[0]);
922 errno = 0;
923 type = strtol(argv[1], NULL, 10);
924 param = strtol(argv[2], NULL, 10);
925 if (errno)
926 return 0;
927 key = argv[3];
928 value = argv[4];
930 if (data->prev_func_id == -1)
931 data->prev_func_id = func_id;
932 if (func_id != data->prev_func_id) {
933 stree = __pop_fake_cur_stree();
934 if (!data->ignore)
935 merge_stree(&data->final_states, stree);
936 free_stree(&stree);
937 __push_fake_cur_stree();
938 __unnullify_path();
939 data->prev_func_id = func_id;
940 data->ignore = 0;
943 if (data->ignore)
944 return 0;
945 if (type == INTERNAL &&
946 !function_signature_matches(value)) {
947 data->ignore = 1;
948 return 0;
951 if (param >= 0 && !get_param(param, &name, &sym))
952 return 0;
954 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
955 if (def_callback->hook_type == type)
956 def_callback->callback(name, sym, key, value);
957 } END_FOR_EACH_PTR(def_callback);
959 return 0;
962 static struct string_list *ptr_names_done;
963 static struct string_list *ptr_names;
965 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
967 insert_string(&ptr_names, alloc_string(argv[0]));
968 return 0;
971 static char *get_next_ptr_name(void)
973 char *ptr;
975 FOR_EACH_PTR(ptr_names, ptr) {
976 if (list_has_string(ptr_names_done, ptr))
977 continue;
978 insert_string(&ptr_names_done, ptr);
979 return ptr;
980 } END_FOR_EACH_PTR(ptr);
981 return NULL;
984 static void get_ptr_names(const char *file, const char *name)
986 char sql_filter[1024];
987 int before, after;
989 if (file) {
990 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
991 file, name);
992 } else {
993 snprintf(sql_filter, 1024, "function = '%s';", name);
996 before = ptr_list_size((struct ptr_list *)ptr_names);
998 run_sql(get_ptr_name, NULL,
999 "select distinct ptr from function_ptr where %s",
1000 sql_filter);
1002 after = ptr_list_size((struct ptr_list *)ptr_names);
1003 if (before == after)
1004 return;
1006 while ((name = get_next_ptr_name()))
1007 get_ptr_names(NULL, name);
1010 static void match_data_from_db(struct symbol *sym)
1012 struct select_caller_info_data data = { .prev_func_id = -1 };
1013 struct sm_state *sm;
1014 struct stree *stree;
1015 struct timeval end_time;
1017 if (!sym || !sym->ident)
1018 return;
1020 gettimeofday(&data.start_time, NULL);
1022 __push_fake_cur_stree();
1023 __unnullify_path();
1025 if (!__inline_fn) {
1026 char *ptr;
1028 if (sym->ctype.modifiers & MOD_STATIC)
1029 get_ptr_names(get_base_file(), sym->ident->name);
1030 else
1031 get_ptr_names(NULL, sym->ident->name);
1033 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1034 __free_ptr_list((struct ptr_list **)&ptr_names);
1035 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1036 stree = __pop_fake_cur_stree();
1037 free_stree(&stree);
1038 return;
1041 sql_select_caller_info(&data,
1042 "call_id, type, parameter, key, value",
1043 sym);
1046 stree = __pop_fake_cur_stree();
1047 if (!data.ignore)
1048 merge_stree(&data.final_states, stree);
1049 free_stree(&stree);
1050 __push_fake_cur_stree();
1051 __unnullify_path();
1052 data.prev_func_id = -1;
1053 data.ignore = 0;
1055 FOR_EACH_PTR(ptr_names, ptr) {
1056 run_sql(caller_info_callback, &data,
1057 "select call_id, type, parameter, key, value"
1058 " from common_caller_info where function = '%s' order by call_id",
1059 ptr);
1060 } END_FOR_EACH_PTR(ptr);
1062 if (data.results) {
1063 FOR_EACH_PTR(ptr_names, ptr) {
1064 free_string(ptr);
1065 } END_FOR_EACH_PTR(ptr);
1066 goto free_ptr_names;
1069 FOR_EACH_PTR(ptr_names, ptr) {
1070 run_sql(caller_info_callback, &data,
1071 "select call_id, type, parameter, key, value"
1072 " from caller_info where function = '%s' order by call_id",
1073 ptr);
1074 free_string(ptr);
1075 } END_FOR_EACH_PTR(ptr);
1077 free_ptr_names:
1078 __free_ptr_list((struct ptr_list **)&ptr_names);
1079 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1080 } else {
1081 sql_select_caller_info(&data,
1082 "call_id, type, parameter, key, value",
1083 sym);
1086 stree = __pop_fake_cur_stree();
1087 if (!data.ignore)
1088 merge_stree(&data.final_states, stree);
1089 free_stree(&stree);
1091 gettimeofday(&end_time, NULL);
1092 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1093 FOR_EACH_SM(data.final_states, sm) {
1094 __set_sm(sm);
1095 } END_FOR_EACH_SM(sm);
1098 free_stree(&data.final_states);
1101 static int call_implies_callbacks(void *_call, int argc, char **argv, char **azColName)
1103 struct expression *call_expr = _call;
1104 struct call_implies_callback *cb;
1105 struct expression *arg = NULL;
1106 int type;
1107 int param;
1109 if (argc != 5)
1110 return 0;
1112 type = atoi(argv[1]);
1113 param = atoi(argv[2]);
1115 FOR_EACH_PTR(call_implies_cb_list, cb) {
1116 if (cb->type != type)
1117 continue;
1118 if (param != -1) {
1119 arg = get_argument_from_call_expr(call_expr->args, param);
1120 if (!arg)
1121 continue;
1123 cb->callback(call_expr, arg, argv[3], argv[4]);
1124 } END_FOR_EACH_PTR(cb);
1126 return 0;
1129 static void match_call_implies(struct expression *expr)
1131 sql_select_call_implies("function, type, parameter, key, value", expr,
1132 call_implies_callbacks);
1135 static void print_initializer_list(struct expression_list *expr_list,
1136 struct symbol *struct_type)
1138 struct expression *expr;
1139 struct symbol *base_type;
1140 char struct_name[256];
1142 FOR_EACH_PTR(expr_list, expr) {
1143 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
1144 print_initializer_list(expr->idx_expression->expr_list, struct_type);
1145 continue;
1147 if (expr->type != EXPR_IDENTIFIER)
1148 continue;
1149 if (!expr->expr_ident)
1150 continue;
1151 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
1152 continue;
1153 base_type = get_type(expr->ident_expression);
1154 if (!base_type || base_type->type != SYM_FN)
1155 continue;
1156 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
1157 struct_type->ident->name, expr->expr_ident->name);
1158 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
1159 struct_name);
1160 } END_FOR_EACH_PTR(expr);
1163 static void global_variable(struct symbol *sym)
1165 struct symbol *struct_type;
1167 if (!sym->ident)
1168 return;
1169 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
1170 return;
1171 struct_type = get_base_type(sym);
1172 if (!struct_type)
1173 return;
1174 if (struct_type->type == SYM_ARRAY) {
1175 struct_type = get_base_type(struct_type);
1176 if (!struct_type)
1177 return;
1179 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
1180 return;
1181 print_initializer_list(sym->initializer->expr_list, struct_type);
1184 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1186 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1189 static void call_return_state_hooks_conditional(struct expression *expr)
1191 struct returned_state_callback *cb;
1192 struct range_list *rl;
1193 char *return_ranges;
1194 int final_pass_orig = final_pass;
1196 __push_fake_cur_stree();
1198 final_pass = 0;
1199 __split_whole_condition(expr->conditional);
1200 final_pass = final_pass_orig;
1202 if (get_implied_rl(expr->cond_true, &rl))
1203 rl = cast_rl(cur_func_return_type(), rl);
1204 else
1205 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
1206 return_ranges = show_rl(rl);
1207 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1209 return_id++;
1210 FOR_EACH_PTR(returned_state_callbacks, cb) {
1211 cb->callback(return_id, return_ranges, expr->cond_true);
1212 } END_FOR_EACH_PTR(cb);
1214 __push_true_states();
1215 __use_false_states();
1217 if (get_implied_rl(expr->cond_false, &rl))
1218 rl = cast_rl(cur_func_return_type(), rl);
1219 else
1220 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
1221 return_ranges = show_rl(rl);
1222 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1224 return_id++;
1225 FOR_EACH_PTR(returned_state_callbacks, cb) {
1226 cb->callback(return_id, return_ranges, expr->cond_false);
1227 } END_FOR_EACH_PTR(cb);
1229 __merge_true_states();
1230 __free_fake_cur_stree();
1233 static void call_return_state_hooks_compare(struct expression *expr)
1235 struct returned_state_callback *cb;
1236 char *return_ranges;
1237 int final_pass_orig = final_pass;
1238 sval_t sval = { .type = &int_ctype };
1239 sval_t ret;
1241 if (!get_implied_value(expr, &ret))
1242 ret.value = -1;
1244 __push_fake_cur_stree();
1246 final_pass = 0;
1247 __split_whole_condition(expr);
1248 final_pass = final_pass_orig;
1250 if (ret.value != 0) {
1251 return_ranges = alloc_sname("1");
1252 sval.value = 1;
1253 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1255 return_id++;
1256 FOR_EACH_PTR(returned_state_callbacks, cb) {
1257 cb->callback(return_id, return_ranges, expr);
1258 } END_FOR_EACH_PTR(cb);
1261 __push_true_states();
1262 __use_false_states();
1264 if (ret.value != 1) {
1265 return_ranges = alloc_sname("0");
1266 sval.value = 0;
1267 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1269 return_id++;
1270 FOR_EACH_PTR(returned_state_callbacks, cb) {
1271 cb->callback(return_id, return_ranges, expr);
1272 } END_FOR_EACH_PTR(cb);
1275 __merge_true_states();
1276 __free_fake_cur_stree();
1279 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1281 struct sm_state *tmp;
1283 FOR_EACH_PTR(slist, tmp) {
1284 if (strcmp(tmp->state->name, sm->state->name) == 0)
1285 return 1;
1286 } END_FOR_EACH_PTR(tmp);
1288 return 0;
1291 static char *get_return_compare_str(struct expression *expr)
1293 char *compare_str;
1294 char *var;
1295 char buf[256];
1296 int comparison;
1297 int param;
1299 compare_str = expr_lte_to_param(expr, -1);
1300 if (compare_str)
1301 return compare_str;
1302 param = get_param_num(expr);
1303 if (param < 0)
1304 return NULL;
1306 var = expr_to_var(expr);
1307 if (!var)
1308 return NULL;
1309 snprintf(buf, sizeof(buf), "%s orig", var);
1310 comparison = get_comparison_strings(var, buf);
1311 free_string(var);
1313 if (!comparison)
1314 return NULL;
1316 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1317 return alloc_sname(buf);
1320 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1322 struct returned_state_callback *cb;
1323 struct range_list *rl;
1324 char *return_ranges;
1325 struct sm_state *tmp;
1326 int ret = 0;
1327 int nr_possible, nr_states;
1328 char *compare_str = NULL;
1329 char buf[128];
1330 struct state_list *already_handled = NULL;
1332 if (!sm || !sm->merged)
1333 return 0;
1335 if (too_many_possible(sm))
1336 return 0;
1338 /* bail if it gets too complicated */
1339 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1340 nr_states = stree_count(__get_cur_stree());
1341 if (nr_states * nr_possible >= 2000)
1342 return 0;
1344 FOR_EACH_PTR(sm->possible, tmp) {
1345 if (tmp->merged)
1346 continue;
1347 if (ptr_in_list(tmp, already_handled))
1348 continue;
1349 add_ptr_list(&already_handled, tmp);
1351 ret = 1;
1352 __push_fake_cur_stree();
1354 overwrite_states_using_pool(sm, tmp);
1356 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1357 return_ranges = show_rl(rl);
1358 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1359 compare_str = get_return_compare_str(expr);
1360 if (compare_str) {
1361 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1362 return_ranges = alloc_sname(buf);
1365 return_id++;
1366 FOR_EACH_PTR(returned_state_callbacks, cb) {
1367 cb->callback(return_id, return_ranges, expr);
1368 } END_FOR_EACH_PTR(cb);
1370 __free_fake_cur_stree();
1371 } END_FOR_EACH_PTR(tmp);
1373 free_slist(&already_handled);
1375 return ret;
1378 static int call_return_state_hooks_split_possible(struct expression *expr)
1380 struct sm_state *sm;
1382 if (!expr || expr_equal_to_param(expr, -1))
1383 return 0;
1385 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1386 return split_possible_helper(sm, expr);
1389 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1391 struct range_list *rl;
1392 char *return_ranges;
1393 sval_t sval;
1394 char *compare_str;
1395 char *math_str;
1396 char buf[128];
1398 *rl_p = NULL;
1400 if (!expr)
1401 return alloc_sname("");
1403 if (get_implied_value(expr, &sval)) {
1404 sval = sval_cast(cur_func_return_type(), sval);
1405 *rl_p = alloc_rl(sval, sval);
1406 return sval_to_str(sval);
1409 compare_str = expr_equal_to_param(expr, -1);
1410 math_str = get_value_in_terms_of_parameter_math(expr);
1412 if (get_implied_rl(expr, &rl)) {
1413 rl = cast_rl(cur_func_return_type(), rl);
1414 return_ranges = show_rl(rl);
1415 } else if (get_imaginary_absolute(expr, &rl)){
1416 rl = cast_rl(cur_func_return_type(), rl);
1417 return alloc_sname(show_rl(rl));
1418 } else {
1419 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1420 return_ranges = show_rl(rl);
1422 *rl_p = rl;
1424 if (compare_str) {
1425 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1426 return alloc_sname(buf);
1428 if (math_str) {
1429 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1430 return alloc_sname(buf);
1432 compare_str = get_return_compare_str(expr);
1433 if (compare_str) {
1434 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1435 return alloc_sname(buf);
1438 return return_ranges;
1441 static int split_positive_from_negative(struct expression *expr)
1443 struct returned_state_callback *cb;
1444 struct range_list *rl;
1445 const char *return_ranges;
1446 struct range_list *ret_rl;
1447 int undo;
1449 /* We're going to print the states 3 times */
1450 if (stree_count(__get_cur_stree()) > 10000 / 3)
1451 return 0;
1453 if (!get_implied_rl(expr, &rl) || !rl)
1454 return 0;
1455 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1456 return 0;
1457 /* Forget about INT_MAX and larger */
1458 if (rl_max(rl).value <= 0)
1459 return 0;
1460 if (!sval_is_negative(rl_min(rl)))
1461 return 0;
1463 if (!assume(compare_expression(expr, '>', zero_expr())))
1464 return 0;
1466 return_id++;
1467 return_ranges = get_return_ranges_str(expr, &ret_rl);
1468 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1469 FOR_EACH_PTR(returned_state_callbacks, cb) {
1470 cb->callback(return_id, (char *)return_ranges, expr);
1471 } END_FOR_EACH_PTR(cb);
1473 end_assume();
1475 if (rl_has_sval(rl, sval_type_val(rl_type(rl), 0))) {
1476 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1478 return_id++;
1479 return_ranges = get_return_ranges_str(expr, &ret_rl);
1480 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1481 FOR_EACH_PTR(returned_state_callbacks, cb) {
1482 cb->callback(return_id, (char *)return_ranges, expr);
1483 } END_FOR_EACH_PTR(cb);
1485 if (undo)
1486 end_assume();
1489 undo = assume(compare_expression(expr, '<', zero_expr()));
1491 return_id++;
1492 return_ranges = get_return_ranges_str(expr, &ret_rl);
1493 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1494 FOR_EACH_PTR(returned_state_callbacks, cb) {
1495 cb->callback(return_id, (char *)return_ranges, expr);
1496 } END_FOR_EACH_PTR(cb);
1498 if (undo)
1499 end_assume();
1501 return 1;
1504 static int call_return_state_hooks_split_null_non_null(struct expression *expr)
1506 struct returned_state_callback *cb;
1507 struct range_list *rl;
1508 struct range_list *nonnull_rl;
1509 sval_t null_sval;
1510 struct range_list *null_rl = NULL;
1511 char *return_ranges;
1512 struct sm_state *sm;
1513 struct smatch_state *state;
1514 int nr_states;
1515 int final_pass_orig = final_pass;
1517 if (!expr || expr_equal_to_param(expr, -1))
1518 return 0;
1519 if (expr->type == EXPR_CALL)
1520 return 0;
1521 if (!is_pointer(expr))
1522 return 0;
1524 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1525 if (!sm)
1526 return 0;
1527 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1528 return 0;
1529 state = sm->state;
1530 if (!estate_rl(state))
1531 return 0;
1532 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1533 return 0;
1534 if (!rl_has_sval(estate_rl(state), sval_type_val(estate_type(state), 0)))
1535 return 0;
1537 nr_states = stree_count(__get_cur_stree());
1538 if (option_info && nr_states >= 1500)
1539 return 0;
1541 rl = estate_rl(state);
1543 __push_fake_cur_stree();
1545 final_pass = 0;
1546 __split_whole_condition(expr);
1547 final_pass = final_pass_orig;
1549 nonnull_rl = rl_filter(rl, rl_zero());
1550 return_ranges = show_rl(nonnull_rl);
1551 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1553 return_id++;
1554 FOR_EACH_PTR(returned_state_callbacks, cb) {
1555 cb->callback(return_id, return_ranges, expr);
1556 } END_FOR_EACH_PTR(cb);
1558 __push_true_states();
1559 __use_false_states();
1561 return_ranges = alloc_sname("0");
1562 null_sval = sval_type_val(rl_type(rl), 0);
1563 add_range(&null_rl, null_sval, null_sval);
1564 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1565 return_id++;
1566 FOR_EACH_PTR(returned_state_callbacks, cb) {
1567 cb->callback(return_id, return_ranges, expr);
1568 } END_FOR_EACH_PTR(cb);
1570 __merge_true_states();
1571 __free_fake_cur_stree();
1573 return 1;
1576 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1578 struct range_list *rl;
1579 struct range_list *nonzero_rl;
1580 sval_t zero_sval;
1581 struct range_list *zero_rl = NULL;
1582 int nr_states;
1583 struct returned_state_callback *cb;
1584 char *return_ranges;
1585 int final_pass_orig = final_pass;
1586 sval_t val;
1588 if (option_project != PROJ_KERNEL)
1589 return 0;
1591 nr_states = stree_count(__get_cur_stree());
1592 if (nr_states > 1500)
1593 return 0;
1595 if (get_value(expr, &val))
1596 return 0;
1597 if (!get_implied_rl(expr, &rl))
1598 return 0;
1599 if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1600 return 0;
1601 if (rl_max(rl).value != 0)
1602 return 0;
1604 __push_fake_cur_stree();
1606 final_pass = 0;
1607 __split_whole_condition(expr);
1608 final_pass = final_pass_orig;
1610 nonzero_rl = rl_filter(rl, rl_zero());
1611 return_ranges = show_rl(nonzero_rl);
1612 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1614 return_id++;
1615 FOR_EACH_PTR(returned_state_callbacks, cb) {
1616 cb->callback(return_id, return_ranges, expr);
1617 } END_FOR_EACH_PTR(cb);
1619 __push_true_states();
1620 __use_false_states();
1622 return_ranges = alloc_sname("0");
1623 zero_sval = sval_type_val(rl_type(rl), 0);
1624 add_range(&zero_rl, zero_sval, zero_sval);
1625 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1626 return_id++;
1627 FOR_EACH_PTR(returned_state_callbacks, cb) {
1628 cb->callback(return_id, return_ranges, expr);
1629 } END_FOR_EACH_PTR(cb);
1631 __merge_true_states();
1632 __free_fake_cur_stree();
1634 return 1;
1637 static int is_boolean(struct expression *expr)
1639 struct range_list *rl;
1641 if (!get_implied_rl(expr, &rl))
1642 return 0;
1643 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1644 return 1;
1645 return 0;
1648 static int is_conditional(struct expression *expr)
1650 if (!expr)
1651 return 0;
1652 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1653 return 1;
1654 return 0;
1657 static int splitable_function_call(struct expression *expr)
1659 struct sm_state *sm;
1660 char buf[64];
1662 if (!expr || expr->type != EXPR_CALL)
1663 return 0;
1664 snprintf(buf, sizeof(buf), "return %p", expr);
1665 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1666 return split_possible_helper(sm, expr);
1669 static struct sm_state *find_bool_param(void)
1671 struct stree *start_states;
1672 struct symbol *arg;
1673 struct sm_state *sm, *tmp;
1674 sval_t sval;
1676 start_states = get_start_states();
1678 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1679 if (!arg->ident)
1680 continue;
1681 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1682 if (!sm)
1683 continue;
1684 if (rl_min(estate_rl(sm->state)).value != 0 ||
1685 rl_max(estate_rl(sm->state)).value != 1)
1686 continue;
1687 goto found;
1688 } END_FOR_EACH_PTR_REVERSE(arg);
1690 return NULL;
1692 found:
1694 * Check if it's splitable. If not, then splitting it up is likely not
1695 * useful for the callers.
1697 FOR_EACH_PTR(sm->possible, tmp) {
1698 if (is_merged(tmp))
1699 continue;
1700 if (!estate_get_single_value(tmp->state, &sval))
1701 return NULL;
1702 } END_FOR_EACH_PTR(tmp);
1704 return sm;
1707 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1709 struct returned_state_callback *cb;
1710 struct range_list *ret_rl;
1711 const char *return_ranges;
1712 struct sm_state *tmp;
1713 int ret = 0;
1714 int nr_possible, nr_states;
1715 char *compare_str = NULL;
1716 char buf[128];
1717 struct state_list *already_handled = NULL;
1719 if (!sm || !sm->merged)
1720 return 0;
1722 if (too_many_possible(sm))
1723 return 0;
1725 /* bail if it gets too complicated */
1726 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1727 nr_states = stree_count(__get_cur_stree());
1728 if (nr_states * nr_possible >= 2000)
1729 return 0;
1731 FOR_EACH_PTR(sm->possible, tmp) {
1732 if (tmp->merged)
1733 continue;
1734 if (ptr_in_list(tmp, already_handled))
1735 continue;
1736 add_ptr_list(&already_handled, tmp);
1738 ret = 1;
1739 __push_fake_cur_stree();
1741 overwrite_states_using_pool(sm, tmp);
1743 return_ranges = get_return_ranges_str(expr, &ret_rl);
1744 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1745 compare_str = get_return_compare_str(expr);
1746 if (compare_str) {
1747 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1748 return_ranges = alloc_sname(buf);
1751 return_id++;
1752 FOR_EACH_PTR(returned_state_callbacks, cb) {
1753 cb->callback(return_id, (char *)return_ranges, expr);
1754 } END_FOR_EACH_PTR(cb);
1756 __free_fake_cur_stree();
1757 } END_FOR_EACH_PTR(tmp);
1759 free_slist(&already_handled);
1761 return ret;
1764 static int split_by_bool_param(struct expression *expr)
1766 struct sm_state *start_sm, *sm;
1767 sval_t sval;
1769 start_sm = find_bool_param();
1770 if (!start_sm)
1771 return 0;
1772 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
1773 if (!sm || estate_get_single_value(sm->state, &sval))
1774 return 0;
1775 return split_on_bool_sm(sm, expr);
1778 static int split_by_null_nonnull_param(struct expression *expr)
1780 struct symbol *arg;
1781 struct sm_state *sm;
1782 sval_t zero = {
1783 .type = &ulong_ctype,
1786 /* function must only take one pointer */
1787 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
1788 return 0;
1789 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
1790 if (!arg->ident)
1791 return 0;
1792 if (get_real_base_type(arg)->type != SYM_PTR)
1793 return 0;
1795 if (param_was_set_var_sym(arg->ident->name, arg))
1796 return 0;
1797 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
1798 if (!sm)
1799 return 0;
1801 if (!rl_has_sval(estate_rl(sm->state), zero))
1802 return 0;
1804 return split_on_bool_sm(sm, expr);
1807 struct expression *strip_expr_statement(struct expression *expr)
1809 struct expression *orig = expr;
1810 struct statement *stmt, *last_stmt;
1812 if (!expr)
1813 return NULL;
1814 if (expr->type == EXPR_PREOP && expr->op == '(')
1815 expr = expr->unop;
1816 if (expr->type != EXPR_STATEMENT)
1817 return orig;
1818 stmt = expr->statement;
1819 if (!stmt || stmt->type != STMT_COMPOUND)
1820 return orig;
1822 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1823 if (!last_stmt || last_stmt->type == STMT_LABEL)
1824 last_stmt = last_stmt->label_statement;
1825 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
1826 return orig;
1827 return strip_expr(last_stmt->expression);
1830 static void call_return_state_hooks(struct expression *expr)
1832 struct returned_state_callback *cb;
1833 struct range_list *ret_rl;
1834 const char *return_ranges;
1835 int nr_states;
1836 sval_t sval;
1838 if (__path_is_null())
1839 return;
1841 expr = strip_expr(expr);
1842 expr = strip_expr_statement(expr);
1844 if (is_impossible_path())
1845 goto vanilla;
1847 if (expr && (expr->type == EXPR_COMPARE ||
1848 !get_implied_value(expr, &sval)) &&
1849 (is_condition(expr) || is_boolean(expr))) {
1850 call_return_state_hooks_compare(expr);
1851 return;
1852 } else if (is_conditional(expr)) {
1853 call_return_state_hooks_conditional(expr);
1854 return;
1855 } else if (call_return_state_hooks_split_possible(expr)) {
1856 return;
1857 } else if (call_return_state_hooks_split_null_non_null(expr)) {
1858 return;
1859 } else if (call_return_state_hooks_split_success_fail(expr)) {
1860 return;
1861 } else if (splitable_function_call(expr)) {
1862 return;
1863 } else if (split_positive_from_negative(expr)) {
1864 return;
1865 } else if (split_by_bool_param(expr)) {
1866 } else if (split_by_null_nonnull_param(expr)) {
1867 return;
1870 vanilla:
1871 return_ranges = get_return_ranges_str(expr, &ret_rl);
1872 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1874 return_id++;
1875 nr_states = stree_count(__get_cur_stree());
1876 if (nr_states >= 10000) {
1877 match_return_info(return_id, (char *)return_ranges, expr);
1878 return;
1880 FOR_EACH_PTR(returned_state_callbacks, cb) {
1881 cb->callback(return_id, (char *)return_ranges, expr);
1882 } END_FOR_EACH_PTR(cb);
1885 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1887 struct returned_member_callback *cb;
1888 struct stree *stree;
1889 struct sm_state *sm;
1890 struct symbol *type;
1891 char *name;
1892 char member_name[256];
1893 int len;
1895 type = get_type(expr);
1896 if (!type || type->type != SYM_PTR)
1897 return;
1898 name = expr_to_var(expr);
1899 if (!name)
1900 return;
1902 member_name[sizeof(member_name) - 1] = '\0';
1903 strcpy(member_name, "$");
1905 len = strlen(name);
1906 FOR_EACH_PTR(returned_member_callbacks, cb) {
1907 stree = __get_cur_stree();
1908 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1909 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1910 strcpy(member_name, "*$");
1911 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1912 continue;
1914 if (strncmp(sm->name, name, len) != 0)
1915 continue;
1916 if (strncmp(sm->name + len, "->", 2) != 0)
1917 continue;
1918 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
1919 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1920 } END_FOR_EACH_SM(sm);
1921 } END_FOR_EACH_PTR(cb);
1923 free_string(name);
1926 static void reset_memdb(struct symbol *sym)
1928 mem_sql(NULL, NULL, "delete from caller_info;");
1929 mem_sql(NULL, NULL, "delete from return_states;");
1930 mem_sql(NULL, NULL, "delete from call_implies;");
1933 static void match_end_func_info(struct symbol *sym)
1935 if (__path_is_null())
1936 return;
1937 call_return_state_hooks(NULL);
1940 static void match_after_func(struct symbol *sym)
1942 if (!__inline_fn)
1943 reset_memdb(sym);
1946 static void init_memdb(void)
1948 char *err = NULL;
1949 int rc;
1950 const char *schema_files[] = {
1951 "db/db.schema",
1952 "db/caller_info.schema",
1953 "db/return_states.schema",
1954 "db/function_type_size.schema",
1955 "db/type_size.schema",
1956 "db/call_implies.schema",
1957 "db/function_ptr.schema",
1958 "db/local_values.schema",
1959 "db/function_type_value.schema",
1960 "db/type_value.schema",
1961 "db/function_type.schema",
1962 "db/data_info.schema",
1963 "db/parameter_name.schema",
1964 "db/constraints.schema",
1965 "db/constraints_required.schema",
1966 "db/fn_ptr_data_link.schema",
1967 "db/fn_data_link.schema",
1968 "db/mtag_about.schema",
1969 "db/mtag_map.schema",
1970 "db/mtag_data.schema",
1971 "db/mtag_alias.schema",
1973 static char buf[4096];
1974 int fd;
1975 int ret;
1976 int i;
1978 rc = sqlite3_open(":memory:", &mem_db);
1979 if (rc != SQLITE_OK) {
1980 printf("Error starting In-Memory database.");
1981 return;
1984 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1985 fd = open_data_file(schema_files[i]);
1986 if (fd < 0) {
1987 printf("failed to open: %s\n", schema_files[i]);
1988 continue;
1990 ret = read(fd, buf, sizeof(buf));
1991 if (ret < 0) {
1992 printf("failed to read: %s\n", schema_files[i]);
1993 continue;
1995 close(fd);
1996 if (ret == sizeof(buf)) {
1997 printf("Schema file too large: %s (limit %zd bytes)",
1998 schema_files[i], sizeof(buf));
1999 continue;
2001 buf[ret] = '\0';
2002 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2003 if (rc != SQLITE_OK) {
2004 fprintf(stderr, "SQL error #2: %s\n", err);
2005 fprintf(stderr, "%s\n", buf);
2010 void open_smatch_db(void)
2012 int rc;
2014 if (option_no_db)
2015 return;
2017 init_memdb();
2019 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
2020 if (rc != SQLITE_OK) {
2021 option_no_db = 1;
2022 return;
2024 return;
2027 static void register_common_funcs(void)
2029 struct token *token;
2030 char *func;
2031 char filename[256];
2033 if (option_project == PROJ_NONE)
2034 strcpy(filename, "common_functions");
2035 else
2036 snprintf(filename, 256, "%s.common_functions", option_project_str);
2038 token = get_tokens_file(filename);
2039 if (!token)
2040 return;
2041 if (token_type(token) != TOKEN_STREAMBEGIN)
2042 return;
2043 token = token->next;
2044 while (token_type(token) != TOKEN_STREAMEND) {
2045 if (token_type(token) != TOKEN_IDENT)
2046 return;
2047 func = alloc_string(show_ident(token->ident));
2048 add_ptr_list(&common_funcs, func);
2049 token = token->next;
2051 clear_token_alloc();
2054 static char *get_next_string(char **str)
2056 static char string[256];
2057 char *start;
2058 char *p = *str;
2059 int len;
2061 if (*p == '\0')
2062 return NULL;
2063 start = p;
2065 while (*p != '\0' && *p != ' ' && *p != '\n')
2066 p++;
2068 len = p - start;
2069 if (len > 256) {
2070 memcpy(string, start, 255);
2071 string[255] = '\0';
2072 printf("return_fix: '%s' too long", string);
2073 **str = '\0';
2074 return NULL;
2076 memcpy(string, start, len);
2077 string[len] = '\0';
2078 if (*p != '\0')
2079 p++;
2080 *str = p;
2081 return string;
2084 static void register_return_replacements(void)
2086 char *func, *orig, *new;
2087 char filename[256];
2088 char buf[4096];
2089 int fd, ret, i;
2090 char *p;
2092 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2093 fd = open_data_file(filename);
2094 if (fd < 0)
2095 return;
2096 ret = read(fd, buf, sizeof(buf));
2097 close(fd);
2098 if (ret < 0)
2099 return;
2100 if (ret == sizeof(buf)) {
2101 printf("file too large: %s (limit %zd bytes)",
2102 filename, sizeof(buf));
2103 return;
2105 buf[ret] = '\0';
2107 p = buf;
2108 while (*p) {
2109 get_next_string(&p);
2110 replace_count++;
2112 if (replace_count == 0 || replace_count % 3 != 0) {
2113 replace_count = 0;
2114 return;
2116 replace_table = malloc(replace_count * sizeof(char *));
2118 p = buf;
2119 i = 0;
2120 while (*p) {
2121 func = alloc_string(get_next_string(&p));
2122 orig = alloc_string(get_next_string(&p));
2123 new = alloc_string(get_next_string(&p));
2125 replace_table[i++] = func;
2126 replace_table[i++] = orig;
2127 replace_table[i++] = new;
2131 void register_definition_db_callbacks(int id)
2133 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2134 add_hook(&global_variable, BASE_HOOK);
2135 add_hook(&global_variable, DECLARATION_HOOK);
2136 add_split_return_callback(match_return_info);
2137 add_split_return_callback(print_returned_struct_members);
2138 add_hook(&call_return_state_hooks, RETURN_HOOK);
2139 add_hook(&match_end_func_info, END_FUNC_HOOK);
2140 add_hook(&match_after_func, AFTER_FUNC_HOOK);
2142 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2143 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
2145 register_common_funcs();
2146 register_return_replacements();
2149 void register_db_call_marker(int id)
2151 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2154 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
2156 struct expression *arg;
2157 char *name = NULL;
2158 char member_name[256];
2160 *sym = NULL;
2162 if (param == -1) {
2163 const char *star = "";
2165 if (expr->type != EXPR_ASSIGNMENT)
2166 return NULL;
2167 name = expr_to_var_sym(expr->left, sym);
2168 if (!name)
2169 return NULL;
2170 if (key[0] == '*') {
2171 star = "*";
2172 key++;
2174 if (strncmp(key, "$", 1) != 0)
2175 return name;
2176 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
2177 free_string(name);
2178 return alloc_string(member_name);
2181 while (expr->type == EXPR_ASSIGNMENT)
2182 expr = strip_expr(expr->right);
2183 if (expr->type != EXPR_CALL)
2184 return NULL;
2186 arg = get_argument_from_call_expr(expr->args, param);
2187 if (!arg)
2188 return NULL;
2190 return get_variable_from_key(arg, key, sym);
2193 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
2195 char buf[256];
2196 char *tmp;
2198 if (!arg)
2199 return NULL;
2201 arg = strip_expr(arg);
2203 if (strcmp(key, "$") == 0)
2204 return expr_to_var_sym(arg, sym);
2206 if (strcmp(key, "*$") == 0) {
2207 if (arg->type == EXPR_PREOP && arg->op == '&') {
2208 arg = strip_expr(arg->unop);
2209 return expr_to_var_sym(arg, sym);
2210 } else {
2211 tmp = expr_to_var_sym(arg, sym);
2212 if (!tmp)
2213 return NULL;
2214 snprintf(buf, sizeof(buf), "*%s", tmp);
2215 free_string(tmp);
2216 return alloc_string(buf);
2220 if (arg->type == EXPR_PREOP && arg->op == '&') {
2221 arg = strip_expr(arg->unop);
2222 tmp = expr_to_var_sym(arg, sym);
2223 if (!tmp)
2224 return NULL;
2225 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
2226 return alloc_string(buf);
2229 tmp = expr_to_var_sym(arg, sym);
2230 if (!tmp)
2231 return NULL;
2232 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
2233 free_string(tmp);
2234 return alloc_string(buf);
2237 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
2239 *vsl = NULL;
2241 if (strcmp("$", key) == 0)
2242 return expr_to_chunk_sym_vsl(arg, sym, vsl);
2243 return get_variable_from_key(arg, key, sym);
2246 const char *state_name_to_param_name(const char *state_name, const char *param_name)
2248 int name_len;
2249 static char buf[256];
2251 name_len = strlen(param_name);
2253 if (strcmp(state_name, param_name) == 0) {
2254 return "$";
2255 } else if (state_name[name_len] == '-' && /* check for '-' from "->" */
2256 strncmp(state_name, param_name, name_len) == 0) {
2257 snprintf(buf, sizeof(buf), "$%s", state_name + name_len);
2258 return buf;
2259 } else if (state_name[0] == '*' && strcmp(state_name + 1, param_name) == 0) {
2260 return "*$";
2262 return NULL;
2265 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
2267 if (!sym || !sym->ident)
2268 return NULL;
2270 return state_name_to_param_name(name, sym->ident->name);
2273 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym)
2275 struct symbol *type;
2276 const char *sym_name;
2277 int name_len;
2278 static char buf[256];
2281 * mtag_name is different from param_name because mtags can be a struct
2282 * instead of a struct pointer. But we want to treat it like a pointer
2283 * because really an mtag is a pointer. Or in other words, if you pass
2284 * a struct foo then you want to talk about foo.bar but with an mtag
2285 * you want to refer to it as foo->bar.
2289 if (!sym || !sym->ident)
2290 return NULL;
2292 type = get_real_base_type(sym);
2293 if (type && type->type == SYM_BASETYPE)
2294 return "*$";
2296 sym_name = sym->ident->name;
2297 name_len = strlen(sym_name);
2299 if (state_name[name_len] == '.' && /* check for '-' from "->" */
2300 strncmp(state_name, sym_name, name_len) == 0) {
2301 snprintf(buf, sizeof(buf), "$->%s", state_name + name_len + 1);
2302 return buf;
2305 return state_name_to_param_name(state_name, sym_name);
2308 const char *get_mtag_name_expr(struct expression *expr)
2310 char *name;
2311 struct symbol *sym;
2312 const char *ret = NULL;
2314 name = expr_to_var_sym(expr, &sym);
2315 if (!name || !sym)
2316 goto free;
2318 ret = get_mtag_name_var_sym(name, sym);
2319 free:
2320 free_string(name);
2321 return ret;
2324 const char *get_param_name(struct sm_state *sm)
2326 return get_param_name_var_sym(sm->name, sm->sym);
2329 char *get_data_info_name(struct expression *expr)
2331 struct symbol *sym;
2332 char *name;
2333 char buf[256];
2334 char *ret = NULL;
2336 expr = strip_expr(expr);
2337 name = get_member_name(expr);
2338 if (name)
2339 return name;
2340 name = expr_to_var_sym(expr, &sym);
2341 if (!name || !sym)
2342 goto free;
2343 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2344 goto free;
2345 if (sym->ctype.modifiers & MOD_STATIC)
2346 snprintf(buf, sizeof(buf), "static %s", name);
2347 else
2348 snprintf(buf, sizeof(buf), "global %s", name);
2349 ret = alloc_sname(buf);
2350 free:
2351 free_string(name);
2352 return ret;