buf_size, strlen: don't use strlen() to determine the buf size
[smatch.git] / smatch_db.c
blob8a49c7f14cce8d368a0c6f33c8cad7abf136467d
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 "smatch.h"
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
26 static sqlite3 *db;
27 static sqlite3 *mem_db;
29 static int return_id;
31 #define sql_insert(table, values...) \
32 do { \
33 if (!mem_db) \
34 break; \
35 if (__inline_fn) { \
36 char buf[1024]; \
37 char *err, *p = buf; \
38 int rc; \
40 p += snprintf(p, buf + sizeof(buf) - p, \
41 "insert into %s values (", #table); \
42 p += snprintf(p, buf + sizeof(buf) - p, values); \
43 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
44 sm_debug("in-mem: %s\n", buf); \
45 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err); \
46 if (rc != SQLITE_OK) { \
47 fprintf(stderr, "SQL error #2: %s\n", err); \
48 fprintf(stderr, "SQL: '%s'\n", buf); \
49 } \
50 break; \
51 } \
52 if (option_info) { \
53 sm_prefix(); \
54 sm_printf("SQL: insert into " #table " values (" values); \
55 sm_printf(");\n"); \
56 } \
57 } while (0)
59 struct def_callback {
60 int hook_type;
61 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
63 ALLOCATOR(def_callback, "definition db hook callbacks");
64 DECLARE_PTR_LIST(callback_list, struct def_callback);
65 static struct callback_list *callbacks;
67 struct member_info_callback {
68 int owner;
69 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
71 ALLOCATOR(member_info_callback, "caller_info callbacks");
72 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
73 static struct member_info_cb_list *member_callbacks;
75 struct returned_state_callback {
76 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
78 ALLOCATOR(returned_state_callback, "returned state callbacks");
79 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
80 static struct returned_state_cb_list *returned_state_callbacks;
82 struct returned_member_callback {
83 int owner;
84 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
86 ALLOCATOR(returned_member_callback, "returned member callbacks");
87 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
88 static struct returned_member_cb_list *returned_member_callbacks;
90 struct call_implies_callback {
91 int type;
92 void (*callback)(struct expression *arg, char *key, char *value);
94 ALLOCATOR(call_implies_callback, "call_implies callbacks");
95 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
96 static struct call_implies_cb_list *call_implies_cb_list;
98 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
100 int i;
102 for (i = 0; i < argc; i++) {
103 if (i != 0)
104 printf(", ");
105 sm_printf("%s", argv[i]);
107 sm_printf("\n");
108 return 0;
111 void debug_sql(const char *sql)
113 if (!option_debug)
114 return;
115 sm_msg("%s", sql);
116 sql_exec(print_sql_output, NULL, sql);
119 void debug_mem_sql(const char *sql)
121 if (!option_debug)
122 return;
123 sm_msg("%s", sql);
124 sql_mem_exec(print_sql_output, NULL, sql);
127 void sql_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
129 char *err = NULL;
130 int rc;
132 if (option_no_db || !db)
133 return;
135 rc = sqlite3_exec(db, sql, callback, data, &err);
136 if (rc != SQLITE_OK) {
137 fprintf(stderr, "SQL error #2: %s\n", err);
138 fprintf(stderr, "SQL: '%s'\n", sql);
142 void sql_mem_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
144 char *err = NULL;
145 int rc;
147 if (!mem_db)
148 return;
150 rc = sqlite3_exec(mem_db, sql, callback, data, &err);
151 if (rc != SQLITE_OK) {
152 fprintf(stderr, "SQL error #2: %s\n", err);
153 fprintf(stderr, "SQL: '%s'\n", sql);
157 void sql_insert_return_states(int return_id, const char *return_ranges,
158 int type, int param, const char *key, const char *value)
160 if (key && strlen(key) >= 80)
161 return;
162 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
163 get_base_file(), get_function(), (unsigned long)__inline_fn,
164 return_id, return_ranges, fn_static(), type, param, key, value);
167 static struct string_list *common_funcs;
168 static int is_common_function(const char *fn)
170 char *tmp;
172 if (strncmp(fn, "__builtin_", 10) == 0)
173 return 1;
175 FOR_EACH_PTR(common_funcs, tmp) {
176 if (strcmp(tmp, fn) == 0)
177 return 1;
178 } END_FOR_EACH_PTR(tmp);
180 return 0;
183 void sql_insert_caller_info(struct expression *call, int type,
184 int param, const char *key, const char *value)
186 char *fn;
188 if (!option_info && !__inline_call)
189 return;
191 if (key && strlen(key) >= 80)
192 return;
194 fn = get_fnptr_name(call->fn);
195 if (!fn)
196 return;
198 if (__inline_call) {
199 mem_sql(NULL, NULL,
200 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
201 get_base_file(), get_function(), fn, (unsigned long)call,
202 is_static(call->fn), type, param, key, value);
205 if (!option_info)
206 return;
208 if (is_common_function(fn))
209 return;
211 sm_msg("SQL_caller_info: insert into caller_info values ("
212 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
213 get_base_file(), get_function(), fn, is_static(call->fn),
214 type, param, key, value);
216 free_string(fn);
219 void sql_insert_function_ptr(const char *fn, const char *struct_name)
221 sql_insert(function_ptr, "'%s', '%s', '%s', 0", get_base_file(), fn,
222 struct_name);
225 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
227 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', %s", get_base_file(),
228 get_function(), (unsigned long)__inline_fn, fn_static(),
229 type, param, key, value);
232 void sql_insert_function_type_size(const char *member, const char *ranges)
234 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
237 void sql_insert_local_values(const char *name, const char *value)
239 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
242 void sql_insert_function_type_value(const char *type, const char *value)
244 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
247 void sql_insert_function_type_info(int param, const char *value)
249 sql_insert(function_type_info, "'%s', '%s', %d, %d, '%s'",
250 get_base_file(), get_function(), fn_static(), param, value);
253 void sql_insert_data_info(struct expression *data, int type, const char *value)
255 char *data_name;
257 data_name = get_data_info_name(data);
258 if (!data_name)
259 return;
260 sql_insert(data_info, "'%s', '%s', %d, '%s'", get_base_file(), data_name, type, value);
263 char *get_static_filter(struct symbol *sym)
265 static char sql_filter[1024];
267 if (sym->ctype.modifiers & MOD_STATIC) {
268 snprintf(sql_filter, sizeof(sql_filter),
269 "file = '%s' and function = '%s' and static = '1'",
270 get_base_file(), sym->ident->name);
271 } else {
272 snprintf(sql_filter, sizeof(sql_filter),
273 "function = '%s' and static = '0'", sym->ident->name);
276 return sql_filter;
279 static int row_count;
280 static int get_row_count(void *unused, int argc, char **argv, char **azColName)
282 if (argc != 1)
283 return 0;
284 row_count = atoi(argv[0]);
285 return 0;
288 static void sql_select_return_states_pointer(const char *cols,
289 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
291 char *ptr;
293 ptr = get_fnptr_name(call->fn);
294 if (!ptr)
295 return;
297 run_sql(callback, info,
298 "select %s from return_states join function_ptr where "
299 "return_states.function == function_ptr.function and ptr = '%s'"
300 "and searchable = 1 order by return_id, type;",
301 cols, ptr);
304 void sql_select_return_states(const char *cols, struct expression *call,
305 int (*callback)(void*, int, char**, char**), void *info)
307 if (is_fake_call(call))
308 return;
310 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol) {
311 sql_select_return_states_pointer(cols, call, callback, info);
312 return;
315 if (inlinable(call->fn)) {
316 mem_sql(callback, info,
317 "select %s from return_states where call_id = '%lu' order by return_id, type;",
318 cols, (unsigned long)call);
319 return;
322 row_count = 0;
323 run_sql(get_row_count, info, "select count(*) from return_states where %s;",
324 get_static_filter(call->fn->symbol));
325 if (row_count > 3000)
326 return;
328 run_sql(callback, info, "select %s from return_states where %s order by return_id, type;",
329 cols, get_static_filter(call->fn->symbol));
332 void sql_select_call_implies(const char *cols, struct expression *call,
333 int (*callback)(void*, int, char**, char**))
335 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
336 return;
338 if (inlinable(call->fn)) {
339 mem_sql(callback, NULL,
340 "select %s from call_implies where call_id = '%lu';",
341 cols, (unsigned long)call);
342 return;
345 run_sql(callback, NULL, "select %s from call_implies where %s;",
346 cols, get_static_filter(call->fn->symbol));
349 void sql_select_caller_info(const char *cols, struct symbol *sym,
350 int (*callback)(void*, int, char**, char**))
352 if (__inline_fn) {
353 mem_sql(callback, NULL,
354 "select %s from caller_info where call_id = %lu;",
355 cols, (unsigned long)__inline_fn);
356 return;
359 run_sql(callback, NULL,
360 "select %s from caller_info where %s order by call_id;",
361 cols, get_static_filter(sym));
364 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
366 struct def_callback *def_callback = __alloc_def_callback(0);
368 def_callback->hook_type = type;
369 def_callback->callback = callback;
370 add_ptr_list(&callbacks, def_callback);
374 * These call backs are used when the --info option is turned on to print struct
375 * member information. For example foo->bar could have a state in
376 * smatch_extra.c and also check_user.c.
378 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
380 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
382 member_callback->owner = owner;
383 member_callback->callback = callback;
384 add_ptr_list(&member_callbacks, member_callback);
387 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
389 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
391 callback->callback = fn;
392 add_ptr_list(&returned_state_callbacks, callback);
395 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))
397 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
399 member_callback->owner = owner;
400 member_callback->callback = callback;
401 add_ptr_list(&returned_member_callbacks, member_callback);
404 void select_call_implies_hook(int type, void (*callback)(struct expression *arg, char *key, char *value))
406 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
408 cb->type = type;
409 cb->callback = callback;
410 add_ptr_list(&call_implies_cb_list, cb);
413 static struct expression *static_call_expr;
414 static struct expression *static_returns_call;
415 static struct symbol *return_type;
416 static struct range_list *return_range_list;
417 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
419 struct range_list *rl;
420 struct expression *call_expr = static_returns_call;
422 if (argc != 1)
423 return 0;
424 call_results_to_rl(call_expr, return_type, argv[0], &rl);
425 return_range_list = rl_union(return_range_list, rl);
426 return 0;
429 struct range_list *db_return_vals(struct expression *expr)
431 char buf[64];
432 struct sm_state *sm;
434 if (is_fake_call(expr))
435 return NULL;
437 snprintf(buf, sizeof(buf), "return %p", expr);
438 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
439 if (sm)
440 return clone_rl(estate_rl(sm->state));
441 static_returns_call = expr;
442 return_type = get_type(expr);
443 if (!return_type)
444 return NULL;
446 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
447 return NULL;
449 return_range_list = NULL;
450 if (inlinable(expr->fn)) {
451 mem_sql(db_return_callback, NULL,
452 "select distinct return from return_states where call_id = '%lu';",
453 (unsigned long)expr);
454 } else {
455 run_sql(db_return_callback, NULL,
456 "select distinct return from return_states where %s;",
457 get_static_filter(expr->fn->symbol));
459 return return_range_list;
462 static void match_call_marker(struct expression *expr)
465 * we just want to record something in the database so that if we have
466 * two calls like: frob(4); frob(some_unkown); then on the receiving
467 * side we know that sometimes frob is called with unknown parameters.
470 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
473 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
474 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
476 struct sm_state *sm;
477 char *name;
478 struct symbol *sym;
479 int len;
480 char printed_name[256];
481 int is_address = 0;
483 expr = strip_expr(expr);
484 if (expr->type == EXPR_PREOP && expr->op == '&') {
485 expr = strip_expr(expr->unop);
486 is_address = 1;
489 name = expr_to_var_sym(expr, &sym);
490 if (!name || !sym)
491 goto free;
493 len = strlen(name);
494 FOR_EACH_SM(stree, sm) {
495 if (sm->sym != sym)
496 continue;
497 if (strcmp(name, sm->name) == 0) {
498 if (is_address)
499 snprintf(printed_name, sizeof(printed_name), "*$");
500 else /* these are already handled. fixme: handle them here */
501 continue;
502 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
503 snprintf(printed_name, sizeof(printed_name), "*$");
504 } else if (strncmp(name, sm->name, len) == 0) {
505 if (is_address)
506 snprintf(printed_name, sizeof(printed_name), "$->%s", sm->name + len + 1);
507 else
508 snprintf(printed_name, sizeof(printed_name), "$%s", sm->name + len);
509 } else {
510 continue;
512 callback(call, param, printed_name, sm);
513 } END_FOR_EACH_SM(sm);
514 free:
515 free_string(name);
518 static void match_call_info(struct expression *call)
520 struct member_info_callback *cb;
521 struct expression *arg;
522 struct stree *stree;
523 char *name;
524 int i;
526 name = get_fnptr_name(call->fn);
527 if (!name)
528 return;
530 FOR_EACH_PTR(member_callbacks, cb) {
531 stree = get_all_states_stree(cb->owner);
532 i = 0;
533 FOR_EACH_PTR(call->args, arg) {
534 print_struct_members(call, arg, i, stree, cb->callback);
535 i++;
536 } END_FOR_EACH_PTR(arg);
537 free_stree(&stree);
538 } END_FOR_EACH_PTR(cb);
540 free_string(name);
543 static int get_param(int param, char **name, struct symbol **sym)
545 struct symbol *arg;
546 int i;
548 i = 0;
549 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
551 * this is a temporary hack to work around a bug (I think in sparse?)
552 * 2.6.37-rc1:fs/reiserfs/journal.o
553 * If there is a function definition without parameter name found
554 * after a function implementation then it causes a crash.
555 * int foo() {}
556 * int bar(char *);
558 if (arg->ident->name < (char *)100)
559 continue;
560 if (i == param) {
561 *name = arg->ident->name;
562 *sym = arg;
563 return TRUE;
565 i++;
566 } END_FOR_EACH_PTR(arg);
568 return FALSE;
571 static struct stree *final_states;
572 static int prev_func_id = -1;
573 static int caller_info_callback(void *unused, int argc, char **argv, char **azColName)
575 int func_id;
576 long type;
577 long param;
578 char *key;
579 char *value;
580 char *name = NULL;
581 struct symbol *sym = NULL;
582 struct def_callback *def_callback;
583 struct stree *stree;
585 if (argc != 5)
586 return 0;
588 func_id = atoi(argv[0]);
589 errno = 0;
590 type = strtol(argv[1], NULL, 10);
591 param = strtol(argv[2], NULL, 10);
592 if (errno)
593 return 0;
594 key = argv[3];
595 value = argv[4];
598 if (prev_func_id == -1)
599 prev_func_id = func_id;
600 if (func_id != prev_func_id) {
601 stree = __pop_fake_cur_stree();
602 merge_stree(&final_states, stree);
603 free_stree(&stree);
604 __push_fake_cur_stree();
605 __unnullify_path();
606 prev_func_id = func_id;
609 if (param >= 0 && !get_param(param, &name, &sym))
610 return 0;
612 FOR_EACH_PTR(callbacks, def_callback) {
613 if (def_callback->hook_type == type)
614 def_callback->callback(name, sym, key, value);
615 } END_FOR_EACH_PTR(def_callback);
617 return 0;
620 static void get_direct_callers(struct symbol *sym)
622 sql_select_caller_info("call_id, type, parameter, key, value", sym,
623 caller_info_callback);
626 static struct string_list *ptr_names_done;
627 static struct string_list *ptr_names;
629 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
631 insert_string(&ptr_names, alloc_string(argv[0]));
632 return 0;
635 static char *get_next_ptr_name(void)
637 char *ptr;
639 FOR_EACH_PTR(ptr_names, ptr) {
640 if (list_has_string(ptr_names_done, ptr))
641 continue;
642 insert_string(&ptr_names_done, ptr);
643 return ptr;
644 } END_FOR_EACH_PTR(ptr);
645 return NULL;
648 static void get_ptr_names(const char *file, const char *name)
650 char sql_filter[1024];
651 int before, after;
653 if (file) {
654 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
655 file, name);
656 } else {
657 snprintf(sql_filter, 1024, "function = '%s';", name);
660 before = ptr_list_size((struct ptr_list *)ptr_names);
662 run_sql(get_ptr_name, NULL,
663 "select distinct ptr from function_ptr where %s",
664 sql_filter);
666 after = ptr_list_size((struct ptr_list *)ptr_names);
667 if (before == after)
668 return;
670 while ((name = get_next_ptr_name()))
671 get_ptr_names(NULL, name);
674 static void match_data_from_db(struct symbol *sym)
676 struct sm_state *sm;
677 struct stree *stree;
679 if (!sym || !sym->ident)
680 return;
682 __push_fake_cur_stree();
683 __unnullify_path();
684 prev_func_id = -1;
686 if (!__inline_fn) {
687 char *ptr;
689 if (sym->ctype.modifiers & MOD_STATIC)
690 get_ptr_names(get_base_file(), sym->ident->name);
691 else
692 get_ptr_names(NULL, sym->ident->name);
694 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
695 __free_ptr_list((struct ptr_list **)&ptr_names);
696 __free_ptr_list((struct ptr_list **)&ptr_names_done);
697 stree = __pop_fake_cur_stree();
698 free_stree(&stree);
699 return;
702 get_direct_callers(sym);
704 FOR_EACH_PTR(ptr_names, ptr) {
705 run_sql(caller_info_callback, NULL,
706 "select call_id, type, parameter, key, value"
707 " from caller_info where function = '%s' order by call_id",
708 ptr);
709 free_string(ptr);
710 } END_FOR_EACH_PTR(ptr);
712 __free_ptr_list((struct ptr_list **)&ptr_names);
713 __free_ptr_list((struct ptr_list **)&ptr_names_done);
714 } else {
715 get_direct_callers(sym);
718 stree = __pop_fake_cur_stree();
719 merge_stree(&final_states, stree);
720 free_stree(&stree);
722 FOR_EACH_SM(final_states, sm) {
723 __set_sm(sm);
724 } END_FOR_EACH_SM(sm);
726 free_stree(&final_states);
729 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
731 struct expression *call_expr = static_call_expr;
732 struct call_implies_callback *cb;
733 struct expression *arg = NULL;
734 int type;
735 int param;
737 if (argc != 5)
738 return 0;
740 type = atoi(argv[1]);
741 param = atoi(argv[2]);
743 FOR_EACH_PTR(call_implies_cb_list, cb) {
744 if (cb->type != type)
745 continue;
746 if (param != -1) {
747 arg = get_argument_from_call_expr(call_expr->args, param);
748 if (!arg)
749 continue;
751 cb->callback(arg, argv[3], argv[4]);
752 } END_FOR_EACH_PTR(cb);
754 return 0;
757 static void match_call_implies(struct expression *expr)
759 static_call_expr = expr;
760 sql_select_call_implies("function, type, parameter, key, value", expr,
761 call_implies_callbacks);
762 return;
765 static void print_initializer_list(struct expression_list *expr_list,
766 struct symbol *struct_type)
768 struct expression *expr;
769 struct symbol *base_type;
770 char struct_name[256];
772 FOR_EACH_PTR(expr_list, expr) {
773 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
774 print_initializer_list(expr->idx_expression->expr_list, struct_type);
775 continue;
777 if (expr->type != EXPR_IDENTIFIER)
778 continue;
779 if (!expr->expr_ident)
780 continue;
781 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
782 continue;
783 base_type = get_type(expr->ident_expression);
784 if (!base_type || base_type->type != SYM_FN)
785 continue;
786 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
787 struct_type->ident->name, expr->expr_ident->name);
788 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
789 struct_name);
790 } END_FOR_EACH_PTR(expr);
793 static void global_variable(struct symbol *sym)
795 struct symbol *struct_type;
797 if (!sym->ident)
798 return;
799 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
800 return;
801 struct_type = get_base_type(sym);
802 if (!struct_type)
803 return;
804 if (struct_type->type == SYM_ARRAY) {
805 struct_type = get_base_type(struct_type);
806 if (!struct_type)
807 return;
809 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
810 return;
811 print_initializer_list(sym->initializer->expr_list, struct_type);
814 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
816 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
819 static void call_return_state_hooks_conditional(struct expression *expr)
821 struct returned_state_callback *cb;
822 struct range_list *rl;
823 char *return_ranges;
824 int final_pass_orig = final_pass;
826 __push_fake_cur_stree();
828 final_pass = 0;
829 __split_whole_condition(expr->conditional);
830 final_pass = final_pass_orig;
832 if (get_implied_rl(expr->cond_true, &rl))
833 rl = cast_rl(cur_func_return_type(), rl);
834 else
835 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
836 return_ranges = show_rl(rl);
838 return_id++;
839 FOR_EACH_PTR(returned_state_callbacks, cb) {
840 cb->callback(return_id, return_ranges, expr);
841 } END_FOR_EACH_PTR(cb);
843 __push_true_states();
844 __use_false_states();
846 if (get_implied_rl(expr->cond_false, &rl))
847 rl = cast_rl(cur_func_return_type(), rl);
848 else
849 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
850 return_ranges = show_rl(rl);
852 return_id++;
853 FOR_EACH_PTR(returned_state_callbacks, cb) {
854 cb->callback(return_id, return_ranges, expr);
855 } END_FOR_EACH_PTR(cb);
857 __merge_true_states();
858 __free_fake_cur_stree();
861 static void call_return_state_hooks_compare(struct expression *expr)
863 struct returned_state_callback *cb;
864 char *return_ranges;
865 int final_pass_orig = final_pass;
867 __push_fake_cur_stree();
869 final_pass = 0;
870 __split_whole_condition(expr);
871 final_pass = final_pass_orig;
873 return_ranges = alloc_sname("1");
875 return_id++;
876 FOR_EACH_PTR(returned_state_callbacks, cb) {
877 cb->callback(return_id, return_ranges, expr);
878 } END_FOR_EACH_PTR(cb);
880 __push_true_states();
881 __use_false_states();
883 return_ranges = alloc_sname("0");;
884 return_id++;
885 FOR_EACH_PTR(returned_state_callbacks, cb) {
886 cb->callback(return_id, return_ranges, expr);
887 } END_FOR_EACH_PTR(cb);
889 __merge_true_states();
890 __free_fake_cur_stree();
893 static int split_helper(struct sm_state *sm, struct expression *expr)
895 struct returned_state_callback *cb;
896 struct range_list *rl;
897 char *return_ranges;
898 struct sm_state *tmp;
899 int ret = 0;
900 int nr_possible, nr_states;
901 char *compare_str;
902 char buf[128];
903 struct stree *orig_stree;
904 struct sm_state *orig_sm;
906 if (!sm || !sm->merged)
907 return 0;
909 if (too_many_possible(sm))
910 return 0;
912 /* bail if it gets too complicated */
913 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
914 nr_states = stree_count(__get_cur_stree());
916 * the main thing option_info because we don't want to print a
917 * million lines of output. If someone else, like check_locking.c
918 * wants this data, then it doesn't cause a slow down to provide it.
920 if (option_info && nr_states * nr_possible >= 2000)
921 return 0;
923 compare_str = expr_lte_to_param(expr, -1);
925 orig_stree = clone_stree(__get_cur_stree());
926 FOR_EACH_PTR(sm->possible, tmp) {
927 if (tmp->merged)
928 continue;
930 ret = 1;
931 __push_fake_cur_stree();
933 FOR_EACH_SM(orig_stree, orig_sm) {
934 __set_sm(orig_sm);
935 } END_FOR_EACH_SM(orig_sm);
937 overwrite_states_using_pool(tmp);
939 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
940 return_ranges = show_rl(rl);
941 if (compare_str) {
942 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
943 return_ranges = alloc_sname(buf);
946 return_id++;
947 FOR_EACH_PTR(returned_state_callbacks, cb) {
948 cb->callback(return_id, return_ranges, expr);
949 } END_FOR_EACH_PTR(cb);
951 __free_fake_cur_stree();
952 } END_FOR_EACH_PTR(tmp);
954 free_stree(&orig_stree);
955 return ret;
958 static int call_return_state_hooks_split_possible(struct expression *expr)
960 struct returned_state_callback *cb;
961 struct range_list *rl;
962 char *return_ranges;
963 struct sm_state *sm;
964 struct sm_state *tmp;
965 int ret = 0;
966 int nr_possible, nr_states;
967 char *compare_str;
968 char buf[128];
970 if (!expr || expr_equal_to_param(expr, -1))
971 return 0;
973 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
974 if (!sm || !sm->merged)
975 return 0;
977 if (too_many_possible(sm))
978 return 0;
980 /* bail if it gets too complicated */
981 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
982 nr_states = stree_count(__get_cur_stree());
984 * the main thing option_info because we don't want to print a
985 * million lines of output. If someone else, like check_locking.c
986 * wants this data, then it doesn't cause a slow down to provide it.
988 if (option_info && nr_states * nr_possible >= 2000)
989 return 0;
992 FOR_EACH_PTR(sm->possible, tmp) {
993 if (tmp->merged)
994 continue;
996 ret = 1;
997 __push_fake_cur_stree();
999 overwrite_states_using_pool(tmp);
1001 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1002 return_ranges = show_rl(rl);
1004 compare_str = expr_lte_to_param(expr, -1);
1005 if (compare_str) {
1006 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1007 return_ranges = alloc_sname(buf);
1010 return_id++;
1011 FOR_EACH_PTR(returned_state_callbacks, cb) {
1012 cb->callback(return_id, return_ranges, expr);
1013 } END_FOR_EACH_PTR(cb);
1015 __free_fake_cur_stree();
1016 } END_FOR_EACH_PTR(tmp);
1018 return ret;
1021 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1023 struct range_list *rl;
1024 int nr_states;
1025 struct returned_state_callback *cb;
1026 char *return_ranges;
1027 int final_pass_orig = final_pass;
1028 sval_t val;
1030 if (option_project != PROJ_KERNEL)
1031 return 0;
1033 nr_states = stree_count(__get_cur_stree());
1034 if (nr_states > 1500)
1035 return 0;
1037 if (get_value(expr, &val))
1038 return 0;
1039 if (!get_implied_rl(expr, &rl))
1040 return 0;
1041 if (rl_min(rl).value < -4095)
1042 return 0;
1043 if (rl_max(rl).value != 0)
1044 return 0;
1046 __push_fake_cur_stree();
1048 final_pass = 0;
1049 __split_whole_condition(expr);
1050 final_pass = final_pass_orig;
1052 return_ranges = show_rl(rl_filter(rl, rl_zero()));
1054 return_id++;
1055 FOR_EACH_PTR(returned_state_callbacks, cb) {
1056 cb->callback(return_id, return_ranges, expr);
1057 } END_FOR_EACH_PTR(cb);
1059 __push_true_states();
1060 __use_false_states();
1062 return_ranges = alloc_sname("0");;
1063 return_id++;
1064 FOR_EACH_PTR(returned_state_callbacks, cb) {
1065 cb->callback(return_id, return_ranges, expr);
1066 } END_FOR_EACH_PTR(cb);
1068 __merge_true_states();
1069 __free_fake_cur_stree();
1071 return 1;
1074 static const char *get_return_ranges_str(struct expression *expr)
1076 struct range_list *rl;
1077 char *return_ranges;
1078 sval_t sval;
1079 char *compare_str;
1080 char *math_str;
1081 char buf[128];
1083 if (!expr)
1084 return alloc_sname("");
1086 if (get_implied_value(expr, &sval))
1087 return sval_to_str(sval);
1089 compare_str = expr_equal_to_param(expr, -1);
1090 math_str = get_value_in_terms_of_parameter_math(expr);
1092 if (get_implied_rl(expr, &rl)) {
1093 rl = cast_rl(cur_func_return_type(), rl);
1094 return_ranges = show_rl(rl);
1095 } else {
1096 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1097 return_ranges = show_rl(rl);
1100 if (compare_str) {
1101 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1102 return alloc_sname(buf);
1105 if (math_str) {
1106 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1107 return alloc_sname(buf);
1110 compare_str = expr_lte_to_param(expr, -1);
1111 if (compare_str) {
1112 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1113 return alloc_sname(buf);
1115 return return_ranges;
1118 static int is_boolean(struct expression *expr)
1120 struct range_list *rl;
1122 if (!get_implied_rl(expr, &rl))
1123 return 0;
1124 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1125 return 1;
1126 return 0;
1129 static int is_conditional(struct expression *expr)
1131 if (!expr)
1132 return 0;
1133 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1134 return 1;
1135 return 0;
1138 static int splitable_function_call(struct expression *expr)
1140 struct sm_state *sm;
1141 char buf[64];
1143 if (!expr || expr->type != EXPR_CALL)
1144 return 0;
1145 snprintf(buf, sizeof(buf), "return %p", expr);
1146 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1147 return split_helper(sm, expr);
1150 static void call_return_state_hooks(struct expression *expr)
1152 struct returned_state_callback *cb;
1153 const char *return_ranges;
1154 int nr_states;
1155 sval_t sval;
1157 expr = strip_expr(expr);
1159 if (!get_implied_value(expr, &sval) &&
1160 (is_condition(expr) || is_boolean(expr))) {
1161 call_return_state_hooks_compare(expr);
1162 return;
1163 } else if (is_conditional(expr)) {
1164 call_return_state_hooks_conditional(expr);
1165 return;
1166 } else if (call_return_state_hooks_split_possible(expr)) {
1167 return;
1168 } else if (call_return_state_hooks_split_success_fail(expr)) {
1169 return;
1170 } else if (splitable_function_call(expr)) {
1171 return;
1174 return_ranges = get_return_ranges_str(expr);
1176 return_id++;
1177 nr_states = stree_count(__get_cur_stree());
1178 if (nr_states >= 10000) {
1179 match_return_info(return_id, (char *)return_ranges, expr);
1180 return;
1182 FOR_EACH_PTR(returned_state_callbacks, cb) {
1183 cb->callback(return_id, (char *)return_ranges, expr);
1184 } END_FOR_EACH_PTR(cb);
1187 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1189 struct returned_member_callback *cb;
1190 struct stree *stree;
1191 struct sm_state *sm;
1192 struct symbol *type;
1193 char *name;
1194 char member_name[256];
1195 int len;
1197 type = get_type(expr);
1198 if (!type || type->type != SYM_PTR)
1199 return;
1200 name = expr_to_var(expr);
1201 if (!name)
1202 return;
1204 member_name[sizeof(member_name) - 1] = '\0';
1205 strcpy(member_name, "$");
1207 len = strlen(name);
1208 FOR_EACH_PTR(returned_member_callbacks, cb) {
1209 stree = __get_cur_stree();
1210 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1211 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1212 strcpy(member_name, "*$");
1213 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1214 continue;
1216 if (strncmp(sm->name, name, len) != 0)
1217 continue;
1218 if (strncmp(sm->name + len, "->", 2) != 0)
1219 continue;
1220 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
1221 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1222 } END_FOR_EACH_SM(sm);
1223 } END_FOR_EACH_PTR(cb);
1225 free_string(name);
1228 static void reset_memdb(void)
1230 mem_sql(NULL, NULL, "delete from caller_info;");
1231 mem_sql(NULL, NULL, "delete from return_states;");
1232 mem_sql(NULL, NULL, "delete from call_implies;");
1235 static void match_end_func_info(struct symbol *sym)
1237 if (__path_is_null())
1238 return;
1239 call_return_state_hooks(NULL);
1240 if (!__inline_fn)
1241 reset_memdb();
1244 static void init_memdb(void)
1246 char *err = NULL;
1247 int rc;
1248 const char *schema_files[] = {
1249 "db/db.schema",
1250 "db/caller_info.schema",
1251 "db/return_states.schema",
1252 "db/function_type_size.schema",
1253 "db/type_size.schema",
1254 "db/call_implies.schema",
1255 "db/function_ptr.schema",
1256 "db/local_values.schema",
1257 "db/function_type_value.schema",
1258 "db/type_value.schema",
1259 "db/function_type_info.schema",
1260 "db/data_info.schema",
1262 static char buf[4096];
1263 int fd;
1264 int ret;
1265 int i;
1267 rc = sqlite3_open(":memory:", &mem_db);
1268 if (rc != SQLITE_OK) {
1269 printf("Error starting In-Memory database.");
1270 return;
1273 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1274 fd = open_data_file(schema_files[i]);
1275 if (fd < 0) {
1276 mem_db = NULL;
1277 return;
1279 ret = read(fd, buf, sizeof(buf));
1280 if (ret == sizeof(buf)) {
1281 printf("Schema file too large: %s (limit %zd bytes)",
1282 schema_files[i], sizeof(buf));
1284 buf[ret] = '\0';
1285 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
1286 if (rc != SQLITE_OK) {
1287 fprintf(stderr, "SQL error #2: %s\n", err);
1288 fprintf(stderr, "%s\n", buf);
1293 void open_smatch_db(void)
1295 int rc;
1297 if (option_no_db)
1298 return;
1300 init_memdb();
1302 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1303 if (rc != SQLITE_OK) {
1304 option_no_db = 1;
1305 return;
1307 return;
1310 static void register_common_funcs(void)
1312 struct token *token;
1313 char *func;
1314 char filename[256];
1316 if (option_project == PROJ_NONE)
1317 strcpy(filename, "common_functions");
1318 else
1319 snprintf(filename, 256, "%s.common_functions", option_project_str);
1321 token = get_tokens_file(filename);
1322 if (!token)
1323 return;
1324 if (token_type(token) != TOKEN_STREAMBEGIN)
1325 return;
1326 token = token->next;
1327 while (token_type(token) != TOKEN_STREAMEND) {
1328 if (token_type(token) != TOKEN_IDENT)
1329 return;
1330 func = alloc_string(show_ident(token->ident));
1331 add_ptr_list(&common_funcs, func);
1332 token = token->next;
1334 clear_token_alloc();
1338 void register_definition_db_callbacks(int id)
1340 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1341 add_hook(&global_variable, BASE_HOOK);
1342 add_hook(&global_variable, DECLARATION_HOOK);
1343 add_split_return_callback(match_return_info);
1344 add_split_return_callback(print_returned_struct_members);
1345 add_hook(&call_return_state_hooks, RETURN_HOOK);
1346 add_hook(&match_end_func_info, END_FUNC_HOOK);
1348 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1349 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1351 register_common_funcs();
1354 void register_db_call_marker(int id)
1356 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1359 char *return_state_to_var_sym(struct expression *expr, int param, char *key, struct symbol **sym)
1361 struct expression *arg;
1362 char *name = NULL;
1363 char member_name[256];
1365 *sym = NULL;
1367 if (param == -1) {
1368 const char *star = "";
1370 if (expr->type != EXPR_ASSIGNMENT)
1371 return NULL;
1372 name = expr_to_var_sym(expr->left, sym);
1373 if (!name)
1374 return NULL;
1375 if (key[0] == '*') {
1376 star = "*";
1377 key++;
1379 if (strncmp(key, "$", 1) != 0)
1380 return name;
1381 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
1382 free_string(name);
1383 return alloc_string(member_name);
1386 while (expr->type == EXPR_ASSIGNMENT)
1387 expr = strip_expr(expr->right);
1388 if (expr->type != EXPR_CALL)
1389 return NULL;
1391 arg = get_argument_from_call_expr(expr->args, param);
1392 if (!arg)
1393 return NULL;
1395 return get_variable_from_key(arg, key, sym);
1398 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
1400 char buf[256];
1401 char *tmp;
1403 if (!arg)
1404 return NULL;
1406 arg = strip_expr(arg);
1408 if (strcmp(key, "$") == 0)
1409 return expr_to_var_sym(arg, sym);
1411 if (strcmp(key, "*$") == 0) {
1412 if (arg->type == EXPR_PREOP && arg->op == '&') {
1413 arg = strip_expr(arg->unop);
1414 return expr_to_var_sym(arg, sym);
1415 } else {
1416 tmp = expr_to_var_sym(arg, sym);
1417 if (!tmp)
1418 return NULL;
1419 snprintf(buf, sizeof(buf), "*%s", tmp);
1420 free_string(tmp);
1421 return alloc_string(buf);
1425 if (arg->type == EXPR_PREOP && arg->op == '&') {
1426 arg = strip_expr(arg->unop);
1427 tmp = expr_to_var_sym(arg, sym);
1428 if (!tmp)
1429 return NULL;
1430 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
1431 return alloc_string(buf);
1434 tmp = expr_to_var_sym(arg, sym);
1435 if (!tmp)
1436 return NULL;
1437 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
1438 free_string(tmp);
1439 return alloc_string(buf);
1442 const char *get_param_name(struct sm_state *sm)
1444 char *param_name;
1445 int name_len;
1446 static char buf[256];
1448 if (!sm->sym->ident)
1449 return NULL;
1451 param_name = sm->sym->ident->name;
1452 name_len = strlen(param_name);
1454 if (strcmp(sm->name, param_name) == 0) {
1455 return "$";
1456 } else if (sm->name[name_len] == '-' && /* check for '-' from "->" */
1457 strncmp(sm->name, param_name, name_len) == 0) {
1458 snprintf(buf, sizeof(buf), "$%s", sm->name + name_len);
1459 return buf;
1460 } else if (sm->name[0] == '*' && strcmp(sm->name + 1, param_name) == 0) {
1461 return "*$";
1463 return NULL;
1466 char *get_data_info_name(struct expression *expr)
1468 struct symbol *sym;
1469 char *name;
1470 char buf[256];
1471 char *ret = NULL;
1473 expr = strip_expr(expr);
1474 name = get_member_name(expr);
1475 if (name)
1476 return name;
1477 name = expr_to_var_sym(expr, &sym);
1478 if (!name || !sym)
1479 goto free;
1480 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
1481 goto free;
1482 if (sym->ctype.modifiers & MOD_STATIC)
1483 snprintf(buf, sizeof(buf), "static %s", name);
1484 else
1485 snprintf(buf, sizeof(buf), "global %s", name);
1486 ret = alloc_sname(buf);
1487 free:
1488 free_string(name);
1489 return ret;