implied: get rid of the ->implied member
[smatch.git] / smatch_db.c
blob9c7ba073cb4739dbd3662e233659c1005c0f7756
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(table, values...) \
33 do { \
34 if (!mem_db) \
35 break; \
36 if (__inline_fn) { \
37 char buf[1024]; \
38 char *err, *p = buf; \
39 int rc; \
41 p += snprintf(p, buf + sizeof(buf) - p, \
42 "insert into %s values (", #table); \
43 p += snprintf(p, buf + sizeof(buf) - p, values); \
44 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
45 sm_debug("in-mem: %s\n", buf); \
46 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err); \
47 if (rc != SQLITE_OK) { \
48 fprintf(stderr, "SQL error #2: %s\n", err); \
49 fprintf(stderr, "SQL: '%s'\n", buf); \
50 } \
51 break; \
52 } \
53 if (option_info) { \
54 sm_prefix(); \
55 sm_printf("SQL: insert into " #table " values (" values); \
56 sm_printf(");\n"); \
57 } \
58 } while (0)
60 struct def_callback {
61 int hook_type;
62 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
64 ALLOCATOR(def_callback, "definition db hook callbacks");
65 DECLARE_PTR_LIST(callback_list, struct def_callback);
66 static struct callback_list *callbacks;
68 struct member_info_callback {
69 int owner;
70 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
72 ALLOCATOR(member_info_callback, "caller_info callbacks");
73 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
74 static struct member_info_cb_list *member_callbacks;
76 struct returned_state_callback {
77 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
79 ALLOCATOR(returned_state_callback, "returned state callbacks");
80 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
81 static struct returned_state_cb_list *returned_state_callbacks;
83 struct returned_member_callback {
84 int owner;
85 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
87 ALLOCATOR(returned_member_callback, "returned member callbacks");
88 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
89 static struct returned_member_cb_list *returned_member_callbacks;
91 struct call_implies_callback {
92 int type;
93 void (*callback)(struct expression *arg, char *key, char *value);
95 ALLOCATOR(call_implies_callback, "call_implies callbacks");
96 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
97 static struct call_implies_cb_list *call_implies_cb_list;
99 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
101 int i;
103 for (i = 0; i < argc; i++) {
104 if (i != 0)
105 printf(", ");
106 sm_printf("%s", argv[i]);
108 sm_printf("\n");
109 return 0;
112 void debug_sql(const char *sql)
114 if (!option_debug)
115 return;
116 sm_msg("%s", sql);
117 sql_exec(print_sql_output, NULL, sql);
120 void debug_mem_sql(const char *sql)
122 if (!option_debug)
123 return;
124 sm_msg("%s", sql);
125 sql_mem_exec(print_sql_output, NULL, sql);
128 void sql_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
130 char *err = NULL;
131 int rc;
133 if (option_no_db || !db)
134 return;
136 rc = sqlite3_exec(db, sql, callback, data, &err);
137 if (rc != SQLITE_OK) {
138 fprintf(stderr, "SQL error #2: %s\n", err);
139 fprintf(stderr, "SQL: '%s'\n", sql);
143 void sql_mem_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
145 char *err = NULL;
146 int rc;
148 if (!mem_db)
149 return;
151 rc = sqlite3_exec(mem_db, sql, callback, data, &err);
152 if (rc != SQLITE_OK) {
153 fprintf(stderr, "SQL error #2: %s\n", err);
154 fprintf(stderr, "SQL: '%s'\n", sql);
158 void sql_insert_return_states(int return_id, const char *return_ranges,
159 int type, int param, const char *key, const char *value)
161 if (key && strlen(key) >= 80)
162 return;
163 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
164 get_base_file(), get_function(), (unsigned long)__inline_fn,
165 return_id, return_ranges, fn_static(), type, param, key, value);
168 static struct string_list *common_funcs;
169 static int is_common_function(const char *fn)
171 char *tmp;
173 if (strncmp(fn, "__builtin_", 10) == 0)
174 return 1;
176 FOR_EACH_PTR(common_funcs, tmp) {
177 if (strcmp(tmp, fn) == 0)
178 return 1;
179 } END_FOR_EACH_PTR(tmp);
181 return 0;
184 void sql_insert_caller_info(struct expression *call, int type,
185 int param, const char *key, const char *value)
187 char *fn;
189 if (!option_info && !__inline_call)
190 return;
192 if (key && strlen(key) >= 80)
193 return;
195 fn = get_fnptr_name(call->fn);
196 if (!fn)
197 return;
199 if (__inline_call) {
200 mem_sql(NULL, NULL,
201 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
202 get_base_file(), get_function(), fn, (unsigned long)call,
203 is_static(call->fn), type, param, key, value);
206 if (!option_info)
207 return;
209 if (is_common_function(fn))
210 return;
212 sm_msg("SQL_caller_info: insert into caller_info values ("
213 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
214 get_base_file(), get_function(), fn, is_static(call->fn),
215 type, param, key, value);
217 free_string(fn);
220 void sql_insert_function_ptr(const char *fn, const char *struct_name)
222 sql_insert(function_ptr, "'%s', '%s', '%s', 0", get_base_file(), fn,
223 struct_name);
226 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
228 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', %s", get_base_file(),
229 get_function(), (unsigned long)__inline_fn, fn_static(),
230 type, param, key, value);
233 void sql_insert_function_type_size(const char *member, const char *ranges)
235 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
238 void sql_insert_local_values(const char *name, const char *value)
240 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
243 void sql_insert_function_type_value(const char *type, const char *value)
245 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
248 void sql_insert_function_type_info(int param, const char *value)
250 sql_insert(function_type_info, "'%s', '%s', %d, %d, '%s'",
251 get_base_file(), get_function(), fn_static(), param, value);
254 void sql_insert_data_info(struct expression *data, int type, const char *value)
256 char *data_name;
258 data_name = get_data_info_name(data);
259 if (!data_name)
260 return;
261 sql_insert(data_info, "'%s', '%s', %d, '%s'", get_base_file(), data_name, type, value);
264 char *get_static_filter(struct symbol *sym)
266 static char sql_filter[1024];
268 /* This can only happen on buggy code. Return invalid SQL. */
269 if (!sym) {
270 sql_filter[0] = '\0';
271 return sql_filter;
274 if (sym->ctype.modifiers & MOD_STATIC) {
275 snprintf(sql_filter, sizeof(sql_filter),
276 "file = '%s' and function = '%s' and static = '1'",
277 get_base_file(), sym->ident->name);
278 } else {
279 snprintf(sql_filter, sizeof(sql_filter),
280 "function = '%s' and static = '0'", sym->ident->name);
283 return sql_filter;
286 static int row_count;
287 static int get_row_count(void *unused, int argc, char **argv, char **azColName)
289 if (argc != 1)
290 return 0;
291 row_count = atoi(argv[0]);
292 return 0;
295 static void sql_select_return_states_pointer(const char *cols,
296 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
298 char *ptr;
300 ptr = get_fnptr_name(call->fn);
301 if (!ptr)
302 return;
304 run_sql(callback, info,
305 "select %s from return_states join function_ptr where "
306 "return_states.function == function_ptr.function and ptr = '%s'"
307 "and searchable = 1 order by return_id, type;",
308 cols, ptr);
311 static int is_local_symbol(struct expression *expr)
313 if (expr->type != EXPR_SYMBOL)
314 return 0;
315 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
316 return 0;
317 return 1;
320 void sql_select_return_states(const char *cols, struct expression *call,
321 int (*callback)(void*, int, char**, char**), void *info)
323 if (is_fake_call(call))
324 return;
326 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol || is_local_symbol(call->fn)) {
327 sql_select_return_states_pointer(cols, call, callback, info);
328 return;
331 if (inlinable(call->fn)) {
332 mem_sql(callback, info,
333 "select %s from return_states where call_id = '%lu' order by return_id, type;",
334 cols, (unsigned long)call);
335 return;
338 row_count = 0;
339 run_sql(get_row_count, info, "select count(*) from return_states where %s;",
340 get_static_filter(call->fn->symbol));
341 if (row_count > 3000)
342 return;
344 run_sql(callback, info, "select %s from return_states where %s order by return_id, type;",
345 cols, get_static_filter(call->fn->symbol));
348 void sql_select_call_implies(const char *cols, struct expression *call,
349 int (*callback)(void*, int, char**, char**))
351 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
352 return;
354 if (inlinable(call->fn)) {
355 mem_sql(callback, NULL,
356 "select %s from call_implies where call_id = '%lu';",
357 cols, (unsigned long)call);
358 return;
361 run_sql(callback, NULL, "select %s from call_implies where %s;",
362 cols, get_static_filter(call->fn->symbol));
365 void sql_select_caller_info(const char *cols, struct symbol *sym,
366 int (*callback)(void*, int, char**, char**))
368 if (__inline_fn) {
369 mem_sql(callback, NULL,
370 "select %s from caller_info where call_id = %lu;",
371 cols, (unsigned long)__inline_fn);
372 return;
375 run_sql(callback, NULL,
376 "select %s from caller_info where %s order by call_id;",
377 cols, get_static_filter(sym));
380 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
382 struct def_callback *def_callback = __alloc_def_callback(0);
384 def_callback->hook_type = type;
385 def_callback->callback = callback;
386 add_ptr_list(&callbacks, def_callback);
390 * These call backs are used when the --info option is turned on to print struct
391 * member information. For example foo->bar could have a state in
392 * smatch_extra.c and also check_user.c.
394 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
396 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
398 member_callback->owner = owner;
399 member_callback->callback = callback;
400 add_ptr_list(&member_callbacks, member_callback);
403 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
405 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
407 callback->callback = fn;
408 add_ptr_list(&returned_state_callbacks, callback);
411 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))
413 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
415 member_callback->owner = owner;
416 member_callback->callback = callback;
417 add_ptr_list(&returned_member_callbacks, member_callback);
420 void select_call_implies_hook(int type, void (*callback)(struct expression *arg, char *key, char *value))
422 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
424 cb->type = type;
425 cb->callback = callback;
426 add_ptr_list(&call_implies_cb_list, cb);
429 static struct expression *static_call_expr;
430 static struct expression *static_returns_call;
431 static struct symbol *return_type;
432 static struct range_list *return_range_list;
433 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
435 struct range_list *rl;
436 struct expression *call_expr = static_returns_call;
438 if (argc != 1)
439 return 0;
440 call_results_to_rl(call_expr, return_type, argv[0], &rl);
441 return_range_list = rl_union(return_range_list, rl);
442 return 0;
445 struct range_list *db_return_vals(struct expression *expr)
447 char buf[64];
448 struct sm_state *sm;
450 if (is_fake_call(expr))
451 return NULL;
453 snprintf(buf, sizeof(buf), "return %p", expr);
454 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
455 if (sm)
456 return clone_rl(estate_rl(sm->state));
457 static_returns_call = expr;
458 return_type = get_type(expr);
459 if (!return_type)
460 return NULL;
462 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
463 return NULL;
465 return_range_list = NULL;
466 if (inlinable(expr->fn)) {
467 mem_sql(db_return_callback, NULL,
468 "select distinct return from return_states where call_id = '%lu';",
469 (unsigned long)expr);
470 } else {
471 run_sql(db_return_callback, NULL,
472 "select distinct return from return_states where %s;",
473 get_static_filter(expr->fn->symbol));
475 return return_range_list;
478 struct range_list *db_return_vals_from_str(const char *fn_name)
480 static_returns_call = NULL;
481 return_type = &llong_ctype;
483 return_range_list = NULL;
484 run_sql(db_return_callback, NULL,
485 "select distinct return from return_states where function = '%s';",
486 fn_name);
487 return return_range_list;
490 static void match_call_marker(struct expression *expr)
493 * we just want to record something in the database so that if we have
494 * two calls like: frob(4); frob(some_unkown); then on the receiving
495 * side we know that sometimes frob is called with unknown parameters.
498 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
501 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
502 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
504 struct sm_state *sm;
505 char *name;
506 struct symbol *sym;
507 int len;
508 char printed_name[256];
509 int is_address = 0;
511 expr = strip_expr(expr);
512 if (expr->type == EXPR_PREOP && expr->op == '&') {
513 expr = strip_expr(expr->unop);
514 is_address = 1;
517 name = expr_to_var_sym(expr, &sym);
518 if (!name || !sym)
519 goto free;
521 len = strlen(name);
522 FOR_EACH_SM(stree, sm) {
523 if (sm->sym != sym)
524 continue;
525 if (strcmp(name, sm->name) == 0) {
526 if (is_address)
527 snprintf(printed_name, sizeof(printed_name), "*$");
528 else /* these are already handled. fixme: handle them here */
529 continue;
530 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
531 snprintf(printed_name, sizeof(printed_name), "*$");
532 } else if (strncmp(name, sm->name, len) == 0) {
533 if (isalnum(sm->name[len]))
534 continue;
535 if (is_address)
536 snprintf(printed_name, sizeof(printed_name), "$->%s", sm->name + len + 1);
537 else
538 snprintf(printed_name, sizeof(printed_name), "$%s", sm->name + len);
539 } else {
540 continue;
542 callback(call, param, printed_name, sm);
543 } END_FOR_EACH_SM(sm);
544 free:
545 free_string(name);
548 static void match_call_info(struct expression *call)
550 struct member_info_callback *cb;
551 struct expression *arg;
552 struct stree *stree;
553 char *name;
554 int i;
556 name = get_fnptr_name(call->fn);
557 if (!name)
558 return;
560 FOR_EACH_PTR(member_callbacks, cb) {
561 stree = get_all_states_stree(cb->owner);
562 i = 0;
563 FOR_EACH_PTR(call->args, arg) {
564 print_struct_members(call, arg, i, stree, cb->callback);
565 i++;
566 } END_FOR_EACH_PTR(arg);
567 free_stree(&stree);
568 } END_FOR_EACH_PTR(cb);
570 free_string(name);
573 static int get_param(int param, char **name, struct symbol **sym)
575 struct symbol *arg;
576 int i;
578 i = 0;
579 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
581 * this is a temporary hack to work around a bug (I think in sparse?)
582 * 2.6.37-rc1:fs/reiserfs/journal.o
583 * If there is a function definition without parameter name found
584 * after a function implementation then it causes a crash.
585 * int foo() {}
586 * int bar(char *);
588 if (arg->ident->name < (char *)100)
589 continue;
590 if (i == param) {
591 *name = arg->ident->name;
592 *sym = arg;
593 return TRUE;
595 i++;
596 } END_FOR_EACH_PTR(arg);
598 return FALSE;
601 static struct stree *final_states;
602 static int prev_func_id = -1;
603 static int caller_info_callback(void *unused, int argc, char **argv, char **azColName)
605 int func_id;
606 long type;
607 long param;
608 char *key;
609 char *value;
610 char *name = NULL;
611 struct symbol *sym = NULL;
612 struct def_callback *def_callback;
613 struct stree *stree;
615 if (argc != 5)
616 return 0;
618 func_id = atoi(argv[0]);
619 errno = 0;
620 type = strtol(argv[1], NULL, 10);
621 param = strtol(argv[2], NULL, 10);
622 if (errno)
623 return 0;
624 key = argv[3];
625 value = argv[4];
628 if (prev_func_id == -1)
629 prev_func_id = func_id;
630 if (func_id != prev_func_id) {
631 stree = __pop_fake_cur_stree();
632 merge_stree(&final_states, stree);
633 free_stree(&stree);
634 __push_fake_cur_stree();
635 __unnullify_path();
636 prev_func_id = func_id;
639 if (param >= 0 && !get_param(param, &name, &sym))
640 return 0;
642 FOR_EACH_PTR(callbacks, def_callback) {
643 if (def_callback->hook_type == type)
644 def_callback->callback(name, sym, key, value);
645 } END_FOR_EACH_PTR(def_callback);
647 return 0;
650 static void get_direct_callers(struct symbol *sym)
652 sql_select_caller_info("call_id, type, parameter, key, value", sym,
653 caller_info_callback);
656 static struct string_list *ptr_names_done;
657 static struct string_list *ptr_names;
659 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
661 insert_string(&ptr_names, alloc_string(argv[0]));
662 return 0;
665 static char *get_next_ptr_name(void)
667 char *ptr;
669 FOR_EACH_PTR(ptr_names, ptr) {
670 if (list_has_string(ptr_names_done, ptr))
671 continue;
672 insert_string(&ptr_names_done, ptr);
673 return ptr;
674 } END_FOR_EACH_PTR(ptr);
675 return NULL;
678 static void get_ptr_names(const char *file, const char *name)
680 char sql_filter[1024];
681 int before, after;
683 if (file) {
684 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
685 file, name);
686 } else {
687 snprintf(sql_filter, 1024, "function = '%s';", name);
690 before = ptr_list_size((struct ptr_list *)ptr_names);
692 run_sql(get_ptr_name, NULL,
693 "select distinct ptr from function_ptr where %s",
694 sql_filter);
696 after = ptr_list_size((struct ptr_list *)ptr_names);
697 if (before == after)
698 return;
700 while ((name = get_next_ptr_name()))
701 get_ptr_names(NULL, name);
704 static void match_data_from_db(struct symbol *sym)
706 struct sm_state *sm;
707 struct stree *stree;
709 if (!sym || !sym->ident)
710 return;
712 __push_fake_cur_stree();
713 __unnullify_path();
714 prev_func_id = -1;
716 if (!__inline_fn) {
717 char *ptr;
719 if (sym->ctype.modifiers & MOD_STATIC)
720 get_ptr_names(get_base_file(), sym->ident->name);
721 else
722 get_ptr_names(NULL, sym->ident->name);
724 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
725 __free_ptr_list((struct ptr_list **)&ptr_names);
726 __free_ptr_list((struct ptr_list **)&ptr_names_done);
727 stree = __pop_fake_cur_stree();
728 free_stree(&stree);
729 return;
732 get_direct_callers(sym);
734 FOR_EACH_PTR(ptr_names, ptr) {
735 run_sql(caller_info_callback, NULL,
736 "select call_id, type, parameter, key, value"
737 " from caller_info where function = '%s' order by call_id",
738 ptr);
739 free_string(ptr);
740 } END_FOR_EACH_PTR(ptr);
742 __free_ptr_list((struct ptr_list **)&ptr_names);
743 __free_ptr_list((struct ptr_list **)&ptr_names_done);
744 } else {
745 get_direct_callers(sym);
748 stree = __pop_fake_cur_stree();
749 merge_stree(&final_states, stree);
750 free_stree(&stree);
752 FOR_EACH_SM(final_states, sm) {
753 __set_sm(sm);
754 } END_FOR_EACH_SM(sm);
756 free_stree(&final_states);
759 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
761 struct expression *call_expr = static_call_expr;
762 struct call_implies_callback *cb;
763 struct expression *arg = NULL;
764 int type;
765 int param;
767 if (argc != 5)
768 return 0;
770 type = atoi(argv[1]);
771 param = atoi(argv[2]);
773 FOR_EACH_PTR(call_implies_cb_list, cb) {
774 if (cb->type != type)
775 continue;
776 if (param != -1) {
777 arg = get_argument_from_call_expr(call_expr->args, param);
778 if (!arg)
779 continue;
781 cb->callback(arg, argv[3], argv[4]);
782 } END_FOR_EACH_PTR(cb);
784 return 0;
787 static void match_call_implies(struct expression *expr)
789 static_call_expr = expr;
790 sql_select_call_implies("function, type, parameter, key, value", expr,
791 call_implies_callbacks);
794 static void print_initializer_list(struct expression_list *expr_list,
795 struct symbol *struct_type)
797 struct expression *expr;
798 struct symbol *base_type;
799 char struct_name[256];
801 FOR_EACH_PTR(expr_list, expr) {
802 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
803 print_initializer_list(expr->idx_expression->expr_list, struct_type);
804 continue;
806 if (expr->type != EXPR_IDENTIFIER)
807 continue;
808 if (!expr->expr_ident)
809 continue;
810 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
811 continue;
812 base_type = get_type(expr->ident_expression);
813 if (!base_type || base_type->type != SYM_FN)
814 continue;
815 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
816 struct_type->ident->name, expr->expr_ident->name);
817 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
818 struct_name);
819 } END_FOR_EACH_PTR(expr);
822 static void global_variable(struct symbol *sym)
824 struct symbol *struct_type;
826 if (!sym->ident)
827 return;
828 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
829 return;
830 struct_type = get_base_type(sym);
831 if (!struct_type)
832 return;
833 if (struct_type->type == SYM_ARRAY) {
834 struct_type = get_base_type(struct_type);
835 if (!struct_type)
836 return;
838 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
839 return;
840 print_initializer_list(sym->initializer->expr_list, struct_type);
843 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
845 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
848 static void call_return_state_hooks_conditional(struct expression *expr)
850 struct returned_state_callback *cb;
851 struct range_list *rl;
852 char *return_ranges;
853 int final_pass_orig = final_pass;
855 __push_fake_cur_stree();
857 final_pass = 0;
858 __split_whole_condition(expr->conditional);
859 final_pass = final_pass_orig;
861 if (get_implied_rl(expr->cond_true, &rl))
862 rl = cast_rl(cur_func_return_type(), rl);
863 else
864 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
865 return_ranges = show_rl(rl);
866 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
868 return_id++;
869 FOR_EACH_PTR(returned_state_callbacks, cb) {
870 cb->callback(return_id, return_ranges, expr);
871 } END_FOR_EACH_PTR(cb);
873 __push_true_states();
874 __use_false_states();
876 if (get_implied_rl(expr->cond_false, &rl))
877 rl = cast_rl(cur_func_return_type(), rl);
878 else
879 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
880 return_ranges = show_rl(rl);
881 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
883 return_id++;
884 FOR_EACH_PTR(returned_state_callbacks, cb) {
885 cb->callback(return_id, return_ranges, expr);
886 } END_FOR_EACH_PTR(cb);
888 __merge_true_states();
889 __free_fake_cur_stree();
892 static void call_return_state_hooks_compare(struct expression *expr)
894 struct returned_state_callback *cb;
895 char *return_ranges;
896 int final_pass_orig = final_pass;
897 sval_t sval = { .type = &int_ctype };
899 __push_fake_cur_stree();
901 final_pass = 0;
902 __split_whole_condition(expr);
903 final_pass = final_pass_orig;
905 return_ranges = alloc_sname("1");
906 sval.value = 1;
907 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
909 return_id++;
910 FOR_EACH_PTR(returned_state_callbacks, cb) {
911 cb->callback(return_id, return_ranges, expr);
912 } END_FOR_EACH_PTR(cb);
914 __push_true_states();
915 __use_false_states();
917 return_ranges = alloc_sname("0");
918 sval.value = 0;
919 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
920 return_id++;
921 FOR_EACH_PTR(returned_state_callbacks, cb) {
922 cb->callback(return_id, return_ranges, expr);
923 } END_FOR_EACH_PTR(cb);
925 __merge_true_states();
926 __free_fake_cur_stree();
929 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
931 struct sm_state *tmp;
933 FOR_EACH_PTR(slist, tmp) {
934 if (strcmp(tmp->state->name, sm->state->name) == 0)
935 return 1;
936 } END_FOR_EACH_PTR(tmp);
938 return 0;
941 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
943 struct returned_state_callback *cb;
944 struct range_list *rl;
945 char *return_ranges;
946 struct sm_state *tmp;
947 int ret = 0;
948 int nr_possible, nr_states;
949 char *compare_str = NULL;
950 char buf[128];
951 struct state_list *already_handled = NULL;
953 if (!sm || !sm->merged)
954 return 0;
956 if (too_many_possible(sm))
957 return 0;
959 /* bail if it gets too complicated */
960 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
961 nr_states = stree_count(__get_cur_stree());
962 if (option_info && nr_states * nr_possible >= 2000)
963 return 0;
965 FOR_EACH_PTR(sm->possible, tmp) {
966 if (tmp->merged)
967 continue;
968 if (ptr_in_list(tmp, already_handled))
969 continue;
970 add_ptr_list(&already_handled, tmp);
972 ret = 1;
973 __push_fake_cur_stree();
975 overwrite_states_using_pool(sm, tmp);
977 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
978 return_ranges = show_rl(rl);
979 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
980 compare_str = expr_lte_to_param(expr, -1);
981 if (compare_str) {
982 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
983 return_ranges = alloc_sname(buf);
986 return_id++;
987 FOR_EACH_PTR(returned_state_callbacks, cb) {
988 cb->callback(return_id, return_ranges, expr);
989 } END_FOR_EACH_PTR(cb);
991 __free_fake_cur_stree();
992 } END_FOR_EACH_PTR(tmp);
994 free_slist(&already_handled);
996 return ret;
999 static int call_return_state_hooks_split_possible(struct expression *expr)
1001 struct sm_state *sm;
1003 if (!expr || expr_equal_to_param(expr, -1))
1004 return 0;
1006 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1007 return split_possible_helper(sm, expr);
1010 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1012 struct range_list *rl;
1013 char *return_ranges;
1014 sval_t sval;
1015 char *compare_str;
1016 char *math_str;
1017 char buf[128];
1019 *rl_p = NULL;
1021 if (!expr)
1022 return alloc_sname("");
1024 if (get_implied_value(expr, &sval)) {
1025 *rl_p = alloc_rl(sval, sval);
1026 return sval_to_str(sval);
1029 compare_str = expr_equal_to_param(expr, -1);
1030 math_str = get_value_in_terms_of_parameter_math(expr);
1032 if (get_implied_rl(expr, &rl)) {
1033 rl = cast_rl(cur_func_return_type(), rl);
1034 return_ranges = show_rl(rl);
1035 } else if (get_imaginary_absolute(expr, &rl)){
1036 rl = cast_rl(cur_func_return_type(), rl);
1037 return alloc_sname(show_rl(rl));
1038 } else {
1039 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1040 return_ranges = show_rl(rl);
1042 *rl_p = rl;
1044 if (compare_str) {
1045 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1046 return alloc_sname(buf);
1049 if (math_str) {
1050 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1051 return alloc_sname(buf);
1054 compare_str = expr_lte_to_param(expr, -1);
1055 if (compare_str) {
1056 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1057 return alloc_sname(buf);
1059 return return_ranges;
1062 static int split_positive_from_negative(struct expression *expr)
1064 struct returned_state_callback *cb;
1065 struct range_list *rl;
1066 const char *return_ranges;
1067 struct range_list *ret_rl;
1068 int undo;
1070 /* We're going to print the states 3 times */
1071 if (stree_count(__get_cur_stree()) > 10000 / 3)
1072 return 0;
1074 if (!get_implied_rl(expr, &rl) || !rl)
1075 return 0;
1076 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1077 return 0;
1078 /* Forget about INT_MAX and larger */
1079 if (rl_max(rl).value <= 0)
1080 return 0;
1081 if (!sval_is_negative(rl_min(rl)))
1082 return 0;
1084 if (!assume(compare_expression(expr, '>', zero_expr())))
1085 return 0;
1087 return_id++;
1088 return_ranges = get_return_ranges_str(expr, &ret_rl);
1089 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1090 FOR_EACH_PTR(returned_state_callbacks, cb) {
1091 cb->callback(return_id, (char *)return_ranges, expr);
1092 } END_FOR_EACH_PTR(cb);
1094 end_assume();
1096 if (rl_has_sval(rl, sval_type_val(rl_type(rl), 0))) {
1097 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1099 return_id++;
1100 return_ranges = get_return_ranges_str(expr, &ret_rl);
1101 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1102 FOR_EACH_PTR(returned_state_callbacks, cb) {
1103 cb->callback(return_id, (char *)return_ranges, expr);
1104 } END_FOR_EACH_PTR(cb);
1106 if (undo)
1107 end_assume();
1110 undo = assume(compare_expression(expr, '<', zero_expr()));
1112 return_id++;
1113 return_ranges = get_return_ranges_str(expr, &ret_rl);
1114 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1115 FOR_EACH_PTR(returned_state_callbacks, cb) {
1116 cb->callback(return_id, (char *)return_ranges, expr);
1117 } END_FOR_EACH_PTR(cb);
1119 if (undo)
1120 end_assume();
1122 return 1;
1125 static int call_return_state_hooks_split_null_non_null(struct expression *expr)
1127 struct returned_state_callback *cb;
1128 struct range_list *rl;
1129 struct range_list *nonnull_rl;
1130 sval_t null_sval;
1131 struct range_list *null_rl = NULL;
1132 char *return_ranges;
1133 struct smatch_state *state;
1134 int nr_states;
1135 int final_pass_orig = final_pass;
1137 if (!expr || expr_equal_to_param(expr, -1))
1138 return 0;
1139 if (expr->type == EXPR_CALL)
1140 return 0;
1141 if (!is_pointer(expr))
1142 return 0;
1144 state = get_state_expr(SMATCH_EXTRA, expr);
1145 if (!state || !estate_rl(state))
1146 return 0;
1147 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1148 return 0;
1149 if (!rl_has_sval(estate_rl(state), sval_type_val(estate_type(state), 0)))
1150 return 0;
1152 nr_states = stree_count(__get_cur_stree());
1153 if (option_info && nr_states >= 1500)
1154 return 0;
1156 rl = estate_rl(state);
1158 __push_fake_cur_stree();
1160 final_pass = 0;
1161 __split_whole_condition(expr);
1162 final_pass = final_pass_orig;
1164 nonnull_rl = rl_filter(rl, rl_zero());
1165 return_ranges = show_rl(nonnull_rl);
1166 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1168 return_id++;
1169 FOR_EACH_PTR(returned_state_callbacks, cb) {
1170 cb->callback(return_id, return_ranges, expr);
1171 } END_FOR_EACH_PTR(cb);
1173 __push_true_states();
1174 __use_false_states();
1176 return_ranges = alloc_sname("0");
1177 null_sval = sval_type_val(rl_type(rl), 0);
1178 add_range(&null_rl, null_sval, null_sval);
1179 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1180 return_id++;
1181 FOR_EACH_PTR(returned_state_callbacks, cb) {
1182 cb->callback(return_id, return_ranges, expr);
1183 } END_FOR_EACH_PTR(cb);
1185 __merge_true_states();
1186 __free_fake_cur_stree();
1188 return 1;
1191 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1193 struct range_list *rl;
1194 struct range_list *nonzero_rl;
1195 sval_t zero_sval;
1196 struct range_list *zero_rl = NULL;
1197 int nr_states;
1198 struct returned_state_callback *cb;
1199 char *return_ranges;
1200 int final_pass_orig = final_pass;
1201 sval_t val;
1203 if (option_project != PROJ_KERNEL)
1204 return 0;
1206 nr_states = stree_count(__get_cur_stree());
1207 if (nr_states > 1500)
1208 return 0;
1210 if (get_value(expr, &val))
1211 return 0;
1212 if (!get_implied_rl(expr, &rl))
1213 return 0;
1214 if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1215 return 0;
1216 if (rl_max(rl).value != 0)
1217 return 0;
1219 __push_fake_cur_stree();
1221 final_pass = 0;
1222 __split_whole_condition(expr);
1223 final_pass = final_pass_orig;
1225 nonzero_rl = rl_filter(rl, rl_zero());
1226 return_ranges = show_rl(nonzero_rl);
1227 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1229 return_id++;
1230 FOR_EACH_PTR(returned_state_callbacks, cb) {
1231 cb->callback(return_id, return_ranges, expr);
1232 } END_FOR_EACH_PTR(cb);
1234 __push_true_states();
1235 __use_false_states();
1237 return_ranges = alloc_sname("0");
1238 zero_sval = sval_type_val(rl_type(rl), 0);
1239 add_range(&zero_rl, zero_sval, zero_sval);
1240 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1241 return_id++;
1242 FOR_EACH_PTR(returned_state_callbacks, cb) {
1243 cb->callback(return_id, return_ranges, expr);
1244 } END_FOR_EACH_PTR(cb);
1246 __merge_true_states();
1247 __free_fake_cur_stree();
1249 return 1;
1252 static int is_boolean(struct expression *expr)
1254 struct range_list *rl;
1256 if (!get_implied_rl(expr, &rl))
1257 return 0;
1258 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1259 return 1;
1260 return 0;
1263 static int is_conditional(struct expression *expr)
1265 if (!expr)
1266 return 0;
1267 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1268 return 1;
1269 return 0;
1272 static int splitable_function_call(struct expression *expr)
1274 struct sm_state *sm;
1275 char buf[64];
1277 if (!expr || expr->type != EXPR_CALL)
1278 return 0;
1279 snprintf(buf, sizeof(buf), "return %p", expr);
1280 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1281 return split_possible_helper(sm, expr);
1284 static struct sm_state *find_bool_param(void)
1286 struct stree *start_states;
1287 struct symbol *arg;
1288 struct sm_state *sm, *tmp;
1289 sval_t sval;
1291 start_states = get_start_states();
1293 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1294 if (!arg->ident || !arg->ident->name)
1295 continue;
1296 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1297 if (!sm)
1298 continue;
1299 if (rl_min(estate_rl(sm->state)).value != 0 ||
1300 rl_max(estate_rl(sm->state)).value != 1)
1301 continue;
1302 goto found;
1303 } END_FOR_EACH_PTR_REVERSE(arg);
1305 return NULL;
1307 found:
1309 * Check if it's splitable. If not, then splitting it up is likely not
1310 * useful for the callers.
1312 FOR_EACH_PTR(sm->possible, tmp) {
1313 if (is_merged(tmp))
1314 continue;
1315 if (!estate_get_single_value(tmp->state, &sval))
1316 return NULL;
1317 } END_FOR_EACH_PTR(tmp);
1319 return sm;
1322 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1324 struct returned_state_callback *cb;
1325 struct range_list *ret_rl;
1326 const char *return_ranges;
1327 struct sm_state *tmp;
1328 int ret = 0;
1329 int nr_possible, nr_states;
1330 char *compare_str = NULL;
1331 char buf[128];
1332 struct state_list *already_handled = NULL;
1334 if (!sm || !sm->merged)
1335 return 0;
1337 if (too_many_possible(sm))
1338 return 0;
1340 /* bail if it gets too complicated */
1341 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1342 nr_states = stree_count(__get_cur_stree());
1343 if (option_info && nr_states * nr_possible >= 2000)
1344 return 0;
1346 FOR_EACH_PTR(sm->possible, tmp) {
1347 if (tmp->merged)
1348 continue;
1349 if (ptr_in_list(tmp, already_handled))
1350 continue;
1351 add_ptr_list(&already_handled, tmp);
1353 ret = 1;
1354 __push_fake_cur_stree();
1356 overwrite_states_using_pool(sm, tmp);
1358 return_ranges = get_return_ranges_str(expr, &ret_rl);
1359 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1360 compare_str = expr_lte_to_param(expr, -1);
1361 if (compare_str) {
1362 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1363 return_ranges = alloc_sname(buf);
1366 return_id++;
1367 FOR_EACH_PTR(returned_state_callbacks, cb) {
1368 cb->callback(return_id, (char *)return_ranges, expr);
1369 } END_FOR_EACH_PTR(cb);
1371 __free_fake_cur_stree();
1372 } END_FOR_EACH_PTR(tmp);
1374 free_slist(&already_handled);
1376 return ret;
1379 static int split_by_bool_param(struct expression *expr)
1381 struct sm_state *start_sm, *sm;
1382 sval_t sval;
1384 start_sm = find_bool_param();
1385 if (!start_sm)
1386 return 0;
1387 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
1388 if (!sm || estate_get_single_value(sm->state, &sval))
1389 return 0;
1390 return split_on_bool_sm(sm, expr);
1393 static void call_return_state_hooks(struct expression *expr)
1395 struct returned_state_callback *cb;
1396 struct range_list *ret_rl;
1397 const char *return_ranges;
1398 int nr_states;
1399 sval_t sval;
1401 if (__path_is_null())
1402 return;
1404 expr = strip_expr(expr);
1406 if (is_impossible_path())
1407 goto vanilla;
1409 if (!get_implied_value(expr, &sval) &&
1410 (is_condition(expr) || is_boolean(expr))) {
1411 call_return_state_hooks_compare(expr);
1412 return;
1413 } else if (is_conditional(expr)) {
1414 call_return_state_hooks_conditional(expr);
1415 return;
1416 } else if (call_return_state_hooks_split_possible(expr)) {
1417 return;
1418 } else if (call_return_state_hooks_split_null_non_null(expr)) {
1419 return;
1420 } else if (call_return_state_hooks_split_success_fail(expr)) {
1421 return;
1422 } else if (splitable_function_call(expr)) {
1423 return;
1424 } else if (split_positive_from_negative(expr)) {
1425 return;
1426 } else if (split_by_bool_param(expr)) {
1427 return;
1430 vanilla:
1431 return_ranges = get_return_ranges_str(expr, &ret_rl);
1432 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1434 return_id++;
1435 nr_states = stree_count(__get_cur_stree());
1436 if (nr_states >= 10000) {
1437 match_return_info(return_id, (char *)return_ranges, expr);
1438 return;
1440 FOR_EACH_PTR(returned_state_callbacks, cb) {
1441 cb->callback(return_id, (char *)return_ranges, expr);
1442 } END_FOR_EACH_PTR(cb);
1445 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1447 struct returned_member_callback *cb;
1448 struct stree *stree;
1449 struct sm_state *sm;
1450 struct symbol *type;
1451 char *name;
1452 char member_name[256];
1453 int len;
1455 type = get_type(expr);
1456 if (!type || type->type != SYM_PTR)
1457 return;
1458 name = expr_to_var(expr);
1459 if (!name)
1460 return;
1462 member_name[sizeof(member_name) - 1] = '\0';
1463 strcpy(member_name, "$");
1465 len = strlen(name);
1466 FOR_EACH_PTR(returned_member_callbacks, cb) {
1467 stree = __get_cur_stree();
1468 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1469 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1470 strcpy(member_name, "*$");
1471 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1472 continue;
1474 if (strncmp(sm->name, name, len) != 0)
1475 continue;
1476 if (strncmp(sm->name + len, "->", 2) != 0)
1477 continue;
1478 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
1479 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1480 } END_FOR_EACH_SM(sm);
1481 } END_FOR_EACH_PTR(cb);
1483 free_string(name);
1486 static void reset_memdb(struct symbol *sym)
1488 mem_sql(NULL, NULL, "delete from caller_info;");
1489 mem_sql(NULL, NULL, "delete from return_states;");
1490 mem_sql(NULL, NULL, "delete from call_implies;");
1493 static void match_end_func_info(struct symbol *sym)
1495 if (__path_is_null())
1496 return;
1497 call_return_state_hooks(NULL);
1500 static void match_after_func(struct symbol *sym)
1502 if (!__inline_fn)
1503 reset_memdb(sym);
1506 static void init_memdb(void)
1508 char *err = NULL;
1509 int rc;
1510 const char *schema_files[] = {
1511 "db/db.schema",
1512 "db/caller_info.schema",
1513 "db/return_states.schema",
1514 "db/function_type_size.schema",
1515 "db/type_size.schema",
1516 "db/call_implies.schema",
1517 "db/function_ptr.schema",
1518 "db/local_values.schema",
1519 "db/function_type_value.schema",
1520 "db/type_value.schema",
1521 "db/function_type_info.schema",
1522 "db/data_info.schema",
1524 static char buf[4096];
1525 int fd;
1526 int ret;
1527 int i;
1529 rc = sqlite3_open(":memory:", &mem_db);
1530 if (rc != SQLITE_OK) {
1531 printf("Error starting In-Memory database.");
1532 return;
1535 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1536 fd = open_data_file(schema_files[i]);
1537 if (fd < 0) {
1538 mem_db = NULL;
1539 return;
1541 ret = read(fd, buf, sizeof(buf));
1542 if (ret == sizeof(buf)) {
1543 printf("Schema file too large: %s (limit %zd bytes)",
1544 schema_files[i], sizeof(buf));
1546 buf[ret] = '\0';
1547 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
1548 if (rc != SQLITE_OK) {
1549 fprintf(stderr, "SQL error #2: %s\n", err);
1550 fprintf(stderr, "%s\n", buf);
1555 void open_smatch_db(void)
1557 int rc;
1559 if (option_no_db)
1560 return;
1562 init_memdb();
1564 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1565 if (rc != SQLITE_OK) {
1566 option_no_db = 1;
1567 return;
1569 return;
1572 static void register_common_funcs(void)
1574 struct token *token;
1575 char *func;
1576 char filename[256];
1578 if (option_project == PROJ_NONE)
1579 strcpy(filename, "common_functions");
1580 else
1581 snprintf(filename, 256, "%s.common_functions", option_project_str);
1583 token = get_tokens_file(filename);
1584 if (!token)
1585 return;
1586 if (token_type(token) != TOKEN_STREAMBEGIN)
1587 return;
1588 token = token->next;
1589 while (token_type(token) != TOKEN_STREAMEND) {
1590 if (token_type(token) != TOKEN_IDENT)
1591 return;
1592 func = alloc_string(show_ident(token->ident));
1593 add_ptr_list(&common_funcs, func);
1594 token = token->next;
1596 clear_token_alloc();
1600 void register_definition_db_callbacks(int id)
1602 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1603 add_hook(&global_variable, BASE_HOOK);
1604 add_hook(&global_variable, DECLARATION_HOOK);
1605 add_split_return_callback(match_return_info);
1606 add_split_return_callback(print_returned_struct_members);
1607 add_hook(&call_return_state_hooks, RETURN_HOOK);
1608 add_hook(&match_end_func_info, END_FUNC_HOOK);
1609 add_hook(&match_after_func, AFTER_FUNC_HOOK);
1611 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1612 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1614 register_common_funcs();
1617 void register_db_call_marker(int id)
1619 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1622 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
1624 struct expression *arg;
1625 char *name = NULL;
1626 char member_name[256];
1628 *sym = NULL;
1630 if (param == -1) {
1631 const char *star = "";
1633 if (expr->type != EXPR_ASSIGNMENT)
1634 return NULL;
1635 name = expr_to_var_sym(expr->left, sym);
1636 if (!name)
1637 return NULL;
1638 if (key[0] == '*') {
1639 star = "*";
1640 key++;
1642 if (strncmp(key, "$", 1) != 0)
1643 return name;
1644 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
1645 free_string(name);
1646 return alloc_string(member_name);
1649 while (expr->type == EXPR_ASSIGNMENT)
1650 expr = strip_expr(expr->right);
1651 if (expr->type != EXPR_CALL)
1652 return NULL;
1654 arg = get_argument_from_call_expr(expr->args, param);
1655 if (!arg)
1656 return NULL;
1658 return get_variable_from_key(arg, key, sym);
1661 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
1663 char buf[256];
1664 char *tmp;
1666 if (!arg)
1667 return NULL;
1669 arg = strip_expr(arg);
1671 if (strcmp(key, "$") == 0)
1672 return expr_to_var_sym(arg, sym);
1674 if (strcmp(key, "*$") == 0) {
1675 if (arg->type == EXPR_PREOP && arg->op == '&') {
1676 arg = strip_expr(arg->unop);
1677 return expr_to_var_sym(arg, sym);
1678 } else {
1679 tmp = expr_to_var_sym(arg, sym);
1680 if (!tmp)
1681 return NULL;
1682 snprintf(buf, sizeof(buf), "*%s", tmp);
1683 free_string(tmp);
1684 return alloc_string(buf);
1688 if (arg->type == EXPR_PREOP && arg->op == '&') {
1689 arg = strip_expr(arg->unop);
1690 tmp = expr_to_var_sym(arg, sym);
1691 if (!tmp)
1692 return NULL;
1693 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
1694 return alloc_string(buf);
1697 tmp = expr_to_var_sym(arg, sym);
1698 if (!tmp)
1699 return NULL;
1700 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
1701 free_string(tmp);
1702 return alloc_string(buf);
1705 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
1707 *vsl = NULL;
1709 if (strcmp("$", key) == 0)
1710 return expr_to_chunk_sym_vsl(arg, sym, vsl);
1711 return get_variable_from_key(arg, key, sym);
1714 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
1716 char *param_name;
1717 int name_len;
1718 static char buf[256];
1720 if (!sym || !sym->ident)
1721 return NULL;
1723 param_name = sym->ident->name;
1724 name_len = strlen(param_name);
1726 if (strcmp(name, param_name) == 0) {
1727 return "$";
1728 } else if (name[name_len] == '-' && /* check for '-' from "->" */
1729 strncmp(name, param_name, name_len) == 0) {
1730 snprintf(buf, sizeof(buf), "$%s", name + name_len);
1731 return buf;
1732 } else if (name[0] == '*' && strcmp(name + 1, param_name) == 0) {
1733 return "*$";
1735 return NULL;
1738 const char *get_param_name(struct sm_state *sm)
1740 return get_param_name_var_sym(sm->name, sm->sym);
1743 char *get_data_info_name(struct expression *expr)
1745 struct symbol *sym;
1746 char *name;
1747 char buf[256];
1748 char *ret = NULL;
1750 expr = strip_expr(expr);
1751 name = get_member_name(expr);
1752 if (name)
1753 return name;
1754 name = expr_to_var_sym(expr, &sym);
1755 if (!name || !sym)
1756 goto free;
1757 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
1758 goto free;
1759 if (sym->ctype.modifiers & MOD_STATIC)
1760 snprintf(buf, sizeof(buf), "static %s", name);
1761 else
1762 snprintf(buf, sizeof(buf), "global %s", name);
1763 ret = alloc_sname(buf);
1764 free:
1765 free_string(name);
1766 return ret;