validation/sm_rosenberg.c: don't pass an uninitialized pointer
[smatch.git] / smatch_db.c
blobb9963f0dd6873206944f6e1c39e9f48d2edde02e
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 static 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];
904 if (!sm || !sm->merged)
905 return 0;
907 if (too_many_possible(sm))
908 return 0;
910 /* bail if it gets too complicated */
911 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
912 nr_states = stree_count(__get_cur_stree());
914 * the main thing option_info because we don't want to print a
915 * million lines of output. If someone else, like check_locking.c
916 * wants this data, then it doesn't cause a slow down to provide it.
918 if (option_info && nr_states * nr_possible >= 2000)
919 return 0;
921 compare_str = expr_lte_to_param(expr, -1);
923 FOR_EACH_PTR(sm->possible, tmp) {
924 if (tmp->merged)
925 continue;
927 ret = 1;
928 __push_fake_cur_stree();
930 overwrite_states_using_pool(tmp);
932 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
933 return_ranges = show_rl(rl);
934 if (compare_str) {
935 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
936 return_ranges = alloc_sname(buf);
939 return_id++;
940 FOR_EACH_PTR(returned_state_callbacks, cb) {
941 cb->callback(return_id, return_ranges, expr);
942 } END_FOR_EACH_PTR(cb);
944 __free_fake_cur_stree();
945 } END_FOR_EACH_PTR(tmp);
947 return ret;
950 static int call_return_state_hooks_split_possible(struct expression *expr)
952 struct returned_state_callback *cb;
953 struct range_list *rl;
954 char *return_ranges;
955 struct sm_state *sm;
956 struct sm_state *tmp;
957 int ret = 0;
958 int nr_possible, nr_states;
959 char *compare_str;
960 char buf[128];
962 if (!expr || expr_equal_to_param(expr, -1))
963 return 0;
965 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
966 if (!sm || !sm->merged)
967 return 0;
969 if (too_many_possible(sm))
970 return 0;
972 /* bail if it gets too complicated */
973 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
974 nr_states = stree_count(__get_cur_stree());
976 * the main thing option_info because we don't want to print a
977 * million lines of output. If someone else, like check_locking.c
978 * wants this data, then it doesn't cause a slow down to provide it.
980 if (option_info && nr_states * nr_possible >= 2000)
981 return 0;
984 FOR_EACH_PTR(sm->possible, tmp) {
985 if (tmp->merged)
986 continue;
988 ret = 1;
989 __push_fake_cur_stree();
991 overwrite_states_using_pool(tmp);
993 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
994 return_ranges = show_rl(rl);
996 compare_str = expr_lte_to_param(expr, -1);
997 if (compare_str) {
998 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
999 return_ranges = alloc_sname(buf);
1002 return_id++;
1003 FOR_EACH_PTR(returned_state_callbacks, cb) {
1004 cb->callback(return_id, return_ranges, expr);
1005 } END_FOR_EACH_PTR(cb);
1007 __free_fake_cur_stree();
1008 } END_FOR_EACH_PTR(tmp);
1010 return ret;
1013 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1015 struct range_list *rl;
1016 int nr_states;
1017 struct returned_state_callback *cb;
1018 char *return_ranges;
1019 int final_pass_orig = final_pass;
1020 sval_t val;
1022 if (option_project != PROJ_KERNEL)
1023 return 0;
1025 nr_states = stree_count(__get_cur_stree());
1026 if (nr_states > 1500)
1027 return 0;
1029 if (get_value(expr, &val))
1030 return 0;
1031 if (!get_implied_rl(expr, &rl))
1032 return 0;
1033 if (rl_min(rl).value < -4095)
1034 return 0;
1035 if (rl_max(rl).value != 0)
1036 return 0;
1038 __push_fake_cur_stree();
1040 final_pass = 0;
1041 __split_whole_condition(expr);
1042 final_pass = final_pass_orig;
1044 return_ranges = show_rl(rl_filter(rl, rl_zero()));
1046 return_id++;
1047 FOR_EACH_PTR(returned_state_callbacks, cb) {
1048 cb->callback(return_id, return_ranges, expr);
1049 } END_FOR_EACH_PTR(cb);
1051 __push_true_states();
1052 __use_false_states();
1054 return_ranges = alloc_sname("0");;
1055 return_id++;
1056 FOR_EACH_PTR(returned_state_callbacks, cb) {
1057 cb->callback(return_id, return_ranges, expr);
1058 } END_FOR_EACH_PTR(cb);
1060 __merge_true_states();
1061 __free_fake_cur_stree();
1063 return 1;
1066 static const char *get_return_ranges_str(struct expression *expr)
1068 struct range_list *rl;
1069 char *return_ranges;
1070 sval_t sval;
1071 char *compare_str;
1072 char *math_str;
1073 char buf[128];
1075 if (!expr)
1076 return alloc_sname("");
1078 if (get_implied_value(expr, &sval))
1079 return sval_to_str(sval);
1081 compare_str = expr_equal_to_param(expr, -1);
1082 math_str = get_value_in_terms_of_parameter_math(expr);
1084 if (get_implied_rl(expr, &rl)) {
1085 rl = cast_rl(cur_func_return_type(), rl);
1086 return_ranges = show_rl(rl);
1087 } else {
1088 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1089 return_ranges = show_rl(rl);
1092 if (compare_str) {
1093 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1094 return alloc_sname(buf);
1097 if (math_str) {
1098 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1099 return alloc_sname(buf);
1102 compare_str = expr_lte_to_param(expr, -1);
1103 if (compare_str) {
1104 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1105 return alloc_sname(buf);
1107 return return_ranges;
1110 static int is_boolean(struct expression *expr)
1112 struct range_list *rl;
1114 if (!get_implied_rl(expr, &rl))
1115 return 0;
1116 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1117 return 1;
1118 return 0;
1121 static int is_conditional(struct expression *expr)
1123 if (!expr)
1124 return 0;
1125 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1126 return 1;
1127 return 0;
1130 static int splitable_function_call(struct expression *expr)
1132 struct sm_state *sm;
1133 char buf[64];
1135 if (!expr || expr->type != EXPR_CALL)
1136 return 0;
1137 snprintf(buf, sizeof(buf), "return %p", expr);
1138 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1139 return split_helper(sm, expr);
1142 static void call_return_state_hooks(struct expression *expr)
1144 struct returned_state_callback *cb;
1145 const char *return_ranges;
1146 int nr_states;
1147 sval_t sval;
1149 expr = strip_expr(expr);
1151 if (!get_implied_value(expr, &sval) &&
1152 (is_condition(expr) || is_boolean(expr))) {
1153 call_return_state_hooks_compare(expr);
1154 return;
1155 } else if (is_conditional(expr)) {
1156 call_return_state_hooks_conditional(expr);
1157 return;
1158 } else if (call_return_state_hooks_split_possible(expr)) {
1159 return;
1160 } else if (call_return_state_hooks_split_success_fail(expr)) {
1161 return;
1162 } else if (splitable_function_call(expr)) {
1163 return;
1166 return_ranges = get_return_ranges_str(expr);
1168 return_id++;
1169 nr_states = stree_count(__get_cur_stree());
1170 if (nr_states >= 10000) {
1171 match_return_info(return_id, (char *)return_ranges, expr);
1172 return;
1174 FOR_EACH_PTR(returned_state_callbacks, cb) {
1175 cb->callback(return_id, (char *)return_ranges, expr);
1176 } END_FOR_EACH_PTR(cb);
1179 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1181 struct returned_member_callback *cb;
1182 struct stree *stree;
1183 struct sm_state *sm;
1184 struct symbol *type;
1185 char *name;
1186 char member_name[256];
1187 int len;
1189 type = get_type(expr);
1190 if (!type || type->type != SYM_PTR)
1191 return;
1192 name = expr_to_var(expr);
1193 if (!name)
1194 return;
1196 member_name[sizeof(member_name) - 1] = '\0';
1197 strcpy(member_name, "$");
1199 len = strlen(name);
1200 FOR_EACH_PTR(returned_member_callbacks, cb) {
1201 stree = __get_cur_stree();
1202 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1203 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1204 strcpy(member_name, "*$");
1205 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1206 continue;
1208 if (strncmp(sm->name, name, len) != 0)
1209 continue;
1210 if (strncmp(sm->name + len, "->", 2) != 0)
1211 continue;
1212 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
1213 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1214 } END_FOR_EACH_SM(sm);
1215 } END_FOR_EACH_PTR(cb);
1217 free_string(name);
1220 static void reset_memdb(void)
1222 mem_sql(NULL, NULL, "delete from caller_info;");
1223 mem_sql(NULL, NULL, "delete from return_states;");
1224 mem_sql(NULL, NULL, "delete from call_implies;");
1227 static void match_end_func_info(struct symbol *sym)
1229 if (__path_is_null())
1230 return;
1231 call_return_state_hooks(NULL);
1232 if (!__inline_fn)
1233 reset_memdb();
1236 static void init_memdb(void)
1238 char *err = NULL;
1239 int rc;
1240 const char *schema_files[] = {
1241 "db/db.schema",
1242 "db/caller_info.schema",
1243 "db/return_states.schema",
1244 "db/function_type_size.schema",
1245 "db/type_size.schema",
1246 "db/call_implies.schema",
1247 "db/function_ptr.schema",
1248 "db/local_values.schema",
1249 "db/function_type_value.schema",
1250 "db/type_value.schema",
1251 "db/function_type_info.schema",
1252 "db/data_info.schema",
1254 static char buf[4096];
1255 int fd;
1256 int ret;
1257 int i;
1259 rc = sqlite3_open(":memory:", &mem_db);
1260 if (rc != SQLITE_OK) {
1261 printf("Error starting In-Memory database.");
1262 return;
1265 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1266 fd = open_data_file(schema_files[i]);
1267 if (fd < 0) {
1268 mem_db = NULL;
1269 return;
1271 ret = read(fd, buf, sizeof(buf));
1272 if (ret == sizeof(buf)) {
1273 printf("Schema file too large: %s (limit %zd bytes)",
1274 schema_files[i], sizeof(buf));
1276 buf[ret] = '\0';
1277 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
1278 if (rc != SQLITE_OK) {
1279 fprintf(stderr, "SQL error #2: %s\n", err);
1280 fprintf(stderr, "%s\n", buf);
1285 void open_smatch_db(void)
1287 int rc;
1289 if (option_no_db)
1290 return;
1292 init_memdb();
1294 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1295 if (rc != SQLITE_OK) {
1296 option_no_db = 1;
1297 return;
1299 return;
1302 static void register_common_funcs(void)
1304 struct token *token;
1305 char *func;
1306 char filename[256];
1308 if (option_project == PROJ_NONE)
1309 strcpy(filename, "common_functions");
1310 else
1311 snprintf(filename, 256, "%s.common_functions", option_project_str);
1313 token = get_tokens_file(filename);
1314 if (!token)
1315 return;
1316 if (token_type(token) != TOKEN_STREAMBEGIN)
1317 return;
1318 token = token->next;
1319 while (token_type(token) != TOKEN_STREAMEND) {
1320 if (token_type(token) != TOKEN_IDENT)
1321 return;
1322 func = alloc_string(show_ident(token->ident));
1323 add_ptr_list(&common_funcs, func);
1324 token = token->next;
1326 clear_token_alloc();
1330 void register_definition_db_callbacks(int id)
1332 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1333 add_hook(&global_variable, BASE_HOOK);
1334 add_hook(&global_variable, DECLARATION_HOOK);
1335 add_split_return_callback(match_return_info);
1336 add_split_return_callback(print_returned_struct_members);
1337 add_hook(&call_return_state_hooks, RETURN_HOOK);
1338 add_hook(&match_end_func_info, END_FUNC_HOOK);
1340 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1341 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1343 register_common_funcs();
1346 void register_db_call_marker(int id)
1348 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1351 char *return_state_to_var_sym(struct expression *expr, int param, char *key, struct symbol **sym)
1353 struct expression *arg;
1354 char *name = NULL;
1355 char member_name[256];
1357 *sym = NULL;
1359 if (param == -1) {
1360 const char *star = "";
1362 if (expr->type != EXPR_ASSIGNMENT)
1363 return NULL;
1364 name = expr_to_var_sym(expr->left, sym);
1365 if (!name)
1366 return NULL;
1367 if (key[0] == '*') {
1368 star = "*";
1369 key++;
1371 if (strncmp(key, "$", 1) != 0)
1372 return name;
1373 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
1374 free_string(name);
1375 return alloc_string(member_name);
1378 while (expr->type == EXPR_ASSIGNMENT)
1379 expr = strip_expr(expr->right);
1380 if (expr->type != EXPR_CALL)
1381 return NULL;
1383 arg = get_argument_from_call_expr(expr->args, param);
1384 if (!arg)
1385 return NULL;
1387 return get_variable_from_key(arg, key, sym);
1390 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
1392 char buf[256];
1393 char *tmp;
1395 arg = strip_expr(arg);
1397 if (strcmp(key, "$") == 0)
1398 return expr_to_var_sym(arg, sym);
1400 if (strcmp(key, "*$") == 0) {
1401 if (arg->type == EXPR_PREOP && arg->op == '&') {
1402 arg = strip_expr(arg->unop);
1403 return expr_to_var_sym(arg, sym);
1404 } else {
1405 tmp = expr_to_var_sym(arg, sym);
1406 if (!tmp)
1407 return NULL;
1408 snprintf(buf, sizeof(buf), "*%s", tmp);
1409 free_string(tmp);
1410 return alloc_string(buf);
1414 if (arg->type == EXPR_PREOP && arg->op == '&') {
1415 arg = strip_expr(arg->unop);
1416 tmp = expr_to_var_sym(arg, sym);
1417 if (!tmp)
1418 return NULL;
1419 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
1420 return alloc_string(buf);
1423 tmp = expr_to_var_sym(arg, sym);
1424 if (!tmp)
1425 return NULL;
1426 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
1427 free_string(tmp);
1428 return alloc_string(buf);
1431 const char *get_param_name(struct sm_state *sm)
1433 char *param_name;
1434 int name_len;
1435 static char buf[256];
1437 if (!sm->sym->ident)
1438 return NULL;
1440 param_name = sm->sym->ident->name;
1441 name_len = strlen(param_name);
1443 if (strcmp(sm->name, param_name) == 0) {
1444 return "$";
1445 } else if (sm->name[name_len] == '-' && /* check for '-' from "->" */
1446 strncmp(sm->name, param_name, name_len) == 0) {
1447 snprintf(buf, sizeof(buf), "$%s", sm->name + name_len);
1448 return buf;
1449 } else if (sm->name[0] == '*' && strcmp(sm->name + 1, param_name) == 0) {
1450 return "*$";
1452 return NULL;
1455 char *get_data_info_name(struct expression *expr)
1457 struct symbol *sym;
1458 char *name;
1459 char buf[256];
1460 char *ret = NULL;
1462 expr = strip_expr(expr);
1463 name = get_member_name(expr);
1464 if (name)
1465 return name;
1466 name = expr_to_var_sym(expr, &sym);
1467 if (!name || !sym)
1468 goto free;
1469 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
1470 goto free;
1471 if (sym->ctype.modifiers & MOD_STATIC)
1472 snprintf(buf, sizeof(buf), "static %s", name);
1473 else
1474 snprintf(buf, sizeof(buf), "global %s", name);
1475 ret = alloc_sname(buf);
1476 free:
1477 free_string(name);
1478 return ret;