validation: fix bogus tests so they don't cause segfaults
[smatch.git] / smatch_db.c
blob13a1ab455cc9f2dafd7c81f32f55bfa2ef415823
1 /*
2 * smatch/smatch_db.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <string.h>
11 #include <errno.h>
12 #include <sqlite3.h>
13 #include <unistd.h>
14 #include "smatch.h"
15 #include "smatch_slist.h"
16 #include "smatch_extra.h"
18 static sqlite3 *db;
19 static sqlite3 *mem_db;
21 #define sql_insert(table, values...) \
22 do { \
23 if (option_info) { \
24 sm_prefix(); \
25 sm_printf("SQL: insert into " #table " values (" values); \
26 sm_printf(");\n"); \
27 } \
28 } while (0)
30 struct def_callback {
31 int hook_type;
32 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
34 ALLOCATOR(def_callback, "definition db hook callbacks");
35 DECLARE_PTR_LIST(callback_list, struct def_callback);
36 static struct callback_list *callbacks;
38 struct member_info_callback {
39 int owner;
40 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state);
42 ALLOCATOR(member_info_callback, "caller_info callbacks");
43 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
44 static struct member_info_cb_list *member_callbacks;
46 struct returned_state_callback {
47 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr, struct state_list *slist);
49 ALLOCATOR(returned_state_callback, "returned state callbacks");
50 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
51 static struct returned_state_cb_list *returned_state_callbacks;
53 struct returned_member_callback {
54 int owner;
55 void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state);
57 ALLOCATOR(returned_member_callback, "returned member callbacks");
58 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
59 static struct returned_member_cb_list *returned_member_callbacks;
61 struct call_implies_callback {
62 int type;
63 void (*callback)(struct expression *arg, char *value);
65 ALLOCATOR(call_implies_callback, "call_implies callbacks");
66 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
67 static struct call_implies_cb_list *call_implies_cb_list;
69 void sql_exec(int (*callback)(void*, int, char**, char**), const char *sql)
71 char *err = NULL;
72 int rc;
74 if (option_no_db || !db)
75 return;
77 rc = sqlite3_exec(db, sql, callback, 0, &err);
78 if (rc != SQLITE_OK) {
79 fprintf(stderr, "SQL error #2: %s\n", err);
80 fprintf(stderr, "SQL: '%s'\n", sql);
84 void sql_insert_return_states(int return_id, const char *return_ranges,
85 int type, int param, const char *key, const char *value)
87 sql_insert(return_states, "'%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s'",
88 get_filename(), get_function(), return_id, return_ranges,
89 fn_static(), type, param, key, value);
92 void sql_insert_caller_info(struct expression *call, int type,
93 int param, const char *key, const char *value)
95 char *fn;
97 if (!option_info)
98 return;
100 fn = get_fnptr_name(call->fn);
101 if (!fn)
102 return;
104 sm_msg("SQL_caller_info: insert into caller_info values ("
105 "'%s', '%s', '%s', %%FUNC_ID%%, %d, %d, %d, '%s', '%s');",
106 get_filename(), get_function(), fn, is_static(call->fn),
107 type, param, key, value);
109 free_string(fn);
112 void sql_insert_function_ptr(const char *fn, const char *struct_name)
114 sql_insert(function_ptr, "'%s', '%s', '%s'", get_filename(), fn,
115 struct_name);
118 void sql_insert_return_values(const char *return_values)
120 sql_insert(return_values, "'%s', '%s', %d, '%s'", get_filename(),
121 get_function(), fn_static(), return_values);
124 void sql_insert_call_implies(int type, int param, int value)
126 sql_insert(call_implies, "'%s', '%s', %d, %d, %d, %d", get_filename(),
127 get_function(), fn_static(), type, param, value);
130 void sql_insert_type_size(const char *member, int size)
132 sql_insert(type_size, "'%s', '%s', %d", get_filename(), member, size);
135 void add_definition_db_callback(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
137 struct def_callback *def_callback = __alloc_def_callback(0);
139 def_callback->hook_type = type;
140 def_callback->callback = callback;
141 add_ptr_list(&callbacks, def_callback);
145 * These call backs are used when the --info option is turned on to print struct
146 * member information. For example foo->bar could have a state in
147 * smatch_extra.c and also check_user.c.
149 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
151 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
153 member_callback->owner = owner;
154 member_callback->callback = callback;
155 add_ptr_list(&member_callbacks, member_callback);
158 void add_returned_state_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr, struct state_list *slist))
160 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
162 callback->callback = fn;
163 add_ptr_list(&returned_state_callbacks, callback);
166 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state))
168 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
170 member_callback->owner = owner;
171 member_callback->callback = callback;
172 add_ptr_list(&returned_member_callbacks, member_callback);
175 void add_db_fn_call_callback(int type, void (*callback)(struct expression *arg, char *value))
177 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
179 cb->type = type;
180 cb->callback = callback;
181 add_ptr_list(&call_implies_cb_list, cb);
184 static struct symbol *return_type;
185 static struct range_list *return_range_list;
186 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
188 if (argc != 1)
189 return 0;
190 if (option_debug)
191 sm_msg("return type %d", type_positive_bits(return_type));
192 str_to_rl(return_type, argv[0], &return_range_list);
193 return 0;
196 struct range_list *db_return_vals(struct expression *expr)
198 struct symbol *sym;
199 static char sql_filter[1024];
201 if (expr->type != EXPR_CALL)
202 return NULL;
203 if (expr->fn->type != EXPR_SYMBOL)
204 return NULL;
205 return_type = get_type(expr);
206 if (!return_type)
207 return NULL;
208 sym = expr->fn->symbol;
209 if (!sym)
210 return NULL;
212 if (sym->ctype.modifiers & MOD_STATIC) {
213 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
214 get_filename(), sym->ident->name);
215 } else {
216 snprintf(sql_filter, 1024, "function = '%s' and static = 0;",
217 sym->ident->name);
220 return_range_list = NULL;
221 run_sql(db_return_callback, "select return from return_values where %s",
222 sql_filter);
223 return return_range_list;
226 static void match_call_marker(struct expression *expr)
229 * we just want to record something in the database so that if we have
230 * two calls like: frob(4); frob(some_unkown); then on the receiving
231 * side we know that sometimes frob is called with unknown parameters.
234 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
237 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct state_list *slist,
238 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
240 struct sm_state *sm;
241 char *name;
242 struct symbol *sym;
243 int len;
244 char printed_name[256];
245 int is_address = 0;
247 expr = strip_expr(expr);
248 if (expr->type == EXPR_PREOP && expr->op == '&') {
249 expr = strip_expr(expr->unop);
250 is_address = 1;
253 name = expr_to_var_sym(expr, &sym);
254 if (!name || !sym)
255 goto free;
257 len = strlen(name);
258 FOR_EACH_PTR(slist, sm) {
259 if (sm->sym != sym)
260 continue;
261 if (strncmp(name, sm->name, len) || sm->name[len] == '\0')
262 continue;
263 if (is_address)
264 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
265 else
266 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
267 callback(call, param, printed_name, sm->state);
268 } END_FOR_EACH_PTR(sm);
269 free:
270 free_string(name);
273 static void match_call_info(struct expression *call)
275 struct member_info_callback *cb;
276 struct expression *arg;
277 struct state_list *slist;
278 char *name;
279 int i;
281 name = get_fnptr_name(call->fn);
282 if (!name)
283 return;
285 FOR_EACH_PTR(member_callbacks, cb) {
286 slist = get_all_states(cb->owner);
287 i = 0;
288 FOR_EACH_PTR(call->args, arg) {
289 print_struct_members(call, arg, i, slist, cb->callback);
290 i++;
291 } END_FOR_EACH_PTR(arg);
292 free_slist(&slist);
293 } END_FOR_EACH_PTR(cb);
295 free_string(name);
298 static int get_param(int param, char **name, struct symbol **sym)
300 struct symbol *arg;
301 int i;
303 i = 0;
304 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
306 * this is a temporary hack to work around a bug (I think in sparse?)
307 * 2.6.37-rc1:fs/reiserfs/journal.o
308 * If there is a function definition without parameter name found
309 * after a function implementation then it causes a crash.
310 * int foo() {}
311 * int bar(char *);
313 if (arg->ident->name < (char *)100)
314 continue;
315 if (i == param && arg->ident->name) {
316 *name = arg->ident->name;
317 *sym = arg;
318 return TRUE;
320 i++;
321 } END_FOR_EACH_PTR(arg);
323 return FALSE;
326 static struct state_list *final_states;
327 static int prev_func_id = -1;
328 static int db_callback(void *unused, int argc, char **argv, char **azColName)
330 int func_id;
331 long type;
332 long param;
333 char *name = NULL;
334 struct symbol *sym = NULL;
335 struct def_callback *def_callback;
337 if (argc != 5)
338 return 0;
340 func_id = atoi(argv[0]);
341 errno = 0;
342 type = strtol(argv[1], NULL, 10);
343 param = strtol(argv[2], NULL, 10);
344 if (errno)
345 return 0;
347 if (prev_func_id == -1)
348 prev_func_id = func_id;
349 if (func_id != prev_func_id) {
350 merge_slist(&final_states, __pop_fake_cur_slist());
351 __push_fake_cur_slist();
352 __unnullify_path();
353 prev_func_id = func_id;
356 if (type == INTERNAL)
357 return 0;
358 if (param >= 0 && !get_param(param, &name, &sym))
359 return 0;
361 FOR_EACH_PTR(callbacks, def_callback) {
362 if (def_callback->hook_type == type)
363 def_callback->callback(name, sym, argv[3], argv[4]);
364 } END_FOR_EACH_PTR(def_callback);
366 return 0;
369 static void get_direct_callers(struct symbol *sym)
371 char sql_filter[1024];
373 if (sym->ctype.modifiers & MOD_STATIC) {
374 snprintf(sql_filter, 1024,
375 "file = '%s' and function = '%s' order by function_id;",
376 get_filename(), sym->ident->name);
377 } else {
378 snprintf(sql_filter, 1024,
379 "function = '%s' and static = 0 order by function_id;",
380 sym->ident->name);
383 run_sql(db_callback, "select function_id, type, parameter, key, value from caller_info"
384 " where %s", sql_filter);
387 static char *ptr_name;
388 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
390 if (!ptr_name)
391 ptr_name = alloc_string(argv[0]);
392 return 0;
395 static void get_function_pointer_callers(struct symbol *sym)
397 char sql_filter[1024];
399 if (sym->ctype.modifiers & MOD_STATIC) {
400 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
401 get_filename(), sym->ident->name);
402 } else {
403 snprintf(sql_filter, 1024, "function = '%s';",
404 sym->ident->name);
407 ptr_name = NULL;
408 run_sql(get_ptr_name, "select ptr from function_ptr where %s", sql_filter);
409 if (!ptr_name)
410 return;
412 run_sql(db_callback, "select function_id, type, parameter, key, value from caller_info"
413 " where function = '%s' order by function_id", ptr_name);
415 free_string(ptr_name);
418 static void match_data_from_db(struct symbol *sym)
420 struct sm_state *sm;
422 if (!sym || !sym->ident || !sym->ident->name)
423 return;
425 __push_fake_cur_slist();
426 __unnullify_path();
427 prev_func_id = -1;
429 get_direct_callers(sym);
430 get_function_pointer_callers(sym);
432 merge_slist(&final_states, __pop_fake_cur_slist());
434 FOR_EACH_PTR(final_states, sm) {
435 __set_sm(sm);
436 } END_FOR_EACH_PTR(sm);
438 free_slist(&final_states);
441 static void match_function_assign(struct expression *expr)
443 struct expression *right = expr->right;
444 struct symbol *sym;
445 char *fn_name;
446 char *ptr_name;
448 if (right->type == EXPR_PREOP && right->op == '&')
449 right = strip_expr(right->unop);
450 if (right->type != EXPR_SYMBOL)
451 return;
452 sym = get_type(right);
453 if (!sym || sym->type != SYM_FN)
454 return;
456 fn_name = expr_to_var(right);
457 ptr_name = get_fnptr_name(expr->left);
458 if (!fn_name || !ptr_name)
459 goto free;
461 sql_insert_function_ptr(fn_name, ptr_name);
463 free:
464 free_string(fn_name);
465 free_string(ptr_name);
468 static struct expression *call_implies_call_expr;
469 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
471 struct call_implies_callback *cb;
472 struct expression *arg = NULL;
473 int type;
474 int param;
476 if (argc != 4)
477 return 0;
479 type = atoi(argv[1]);
480 param = atoi(argv[2]);
482 FOR_EACH_PTR(call_implies_cb_list, cb) {
483 if (cb->type != type)
484 continue;
485 if (param != -1) {
486 arg = get_argument_from_call_expr(call_implies_call_expr->args, param);
487 if (!arg)
488 continue;
490 cb->callback(arg, argv[3]);
491 } END_FOR_EACH_PTR(cb);
493 return 0;
496 static void match_call_implies(struct expression *expr)
498 struct symbol *sym;
499 static char sql_filter[1024];
501 if (expr->fn->type != EXPR_SYMBOL)
502 return;
503 sym = expr->fn->symbol;
504 if (!sym)
505 return;
507 if (sym->ctype.modifiers & MOD_STATIC) {
508 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
509 get_filename(), sym->ident->name);
510 } else {
511 snprintf(sql_filter, 1024, "function = '%s' and static = 0;",
512 sym->ident->name);
515 call_implies_call_expr = expr;
516 run_sql(call_implies_callbacks,
517 "select function, type, parameter, value from call_implies where %s",
518 sql_filter);
519 return;
522 static void print_initializer_list(struct expression_list *expr_list,
523 struct symbol *struct_type)
525 struct expression *expr;
526 struct symbol *base_type;
527 char struct_name[256];
529 FOR_EACH_PTR(expr_list, expr) {
530 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
531 print_initializer_list(expr->idx_expression->expr_list, struct_type);
532 continue;
534 if (expr->type != EXPR_IDENTIFIER)
535 continue;
536 if (!expr->expr_ident)
537 continue;
538 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
539 continue;
540 base_type = get_type(expr->ident_expression);
541 if (!base_type || base_type->type != SYM_FN)
542 continue;
543 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
544 struct_type->ident->name, expr->expr_ident->name);
545 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
546 struct_name);
547 } END_FOR_EACH_PTR(expr);
550 static void global_variable(struct symbol *sym)
552 struct symbol *struct_type;
554 if (!sym->ident)
555 return;
556 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
557 return;
558 struct_type = get_base_type(sym);
559 if (!struct_type)
560 return;
561 if (struct_type->type == SYM_ARRAY) {
562 struct_type = get_base_type(struct_type);
563 if (!struct_type)
564 return;
566 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
567 return;
568 print_initializer_list(sym->initializer->expr_list, struct_type);
571 static void match_return_info(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
573 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
576 static int return_id;
577 static void match_function_def(struct symbol *sym)
579 return_id = 0;
582 static void call_return_state_hooks_compare(struct expression *expr)
584 struct returned_state_callback *cb;
585 struct state_list *slist;
586 char *return_ranges;
587 int final_pass_orig = final_pass;
589 __push_fake_cur_slist();
591 final_pass = 0;
592 __split_whole_condition(expr);
593 final_pass = final_pass_orig;
595 return_ranges = alloc_sname("1");
597 return_id++;
598 slist = __get_cur_slist();
599 FOR_EACH_PTR(returned_state_callbacks, cb) {
600 cb->callback(return_id, return_ranges, expr, slist);
601 } END_FOR_EACH_PTR(cb);
603 __push_true_states();
604 __use_false_states();
606 return_ranges = alloc_sname("0");;
607 return_id++;
608 slist = __get_cur_slist();
609 FOR_EACH_PTR(returned_state_callbacks, cb) {
610 cb->callback(return_id, return_ranges, expr, slist);
611 } END_FOR_EACH_PTR(cb);
613 __merge_true_states();
614 __pop_fake_cur_slist();
617 static int call_return_state_hooks_split_possible(struct expression *expr)
619 struct returned_state_callback *cb;
620 struct state_list *slist;
621 struct range_list *rl;
622 char *return_ranges;
623 struct sm_state *sm;
624 struct sm_state *tmp;
625 int ret = 0;
626 int nr_possible, nr_states;
628 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
629 if (!sm || !sm->merged)
630 return 0;
632 /* bail if it gets too complicated */
633 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
634 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
635 if (nr_possible >= 100)
636 return 0;
637 if (nr_states * nr_possible >= 1000)
638 return 0;
640 FOR_EACH_PTR(sm->possible, tmp) {
641 if (tmp->merged)
642 continue;
644 ret = 1;
645 __push_fake_cur_slist();
647 overwrite_states_using_pool(tmp);
649 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
650 return_ranges = show_rl(rl);
652 return_id++;
653 slist = __get_cur_slist();
654 FOR_EACH_PTR(returned_state_callbacks, cb) {
655 cb->callback(return_id, return_ranges, expr, slist);
656 } END_FOR_EACH_PTR(cb);
658 __pop_fake_cur_slist();
659 } END_FOR_EACH_PTR(tmp);
661 return ret;
664 static void call_return_state_hooks(struct expression *expr)
666 struct returned_state_callback *cb;
667 struct state_list *slist;
668 struct range_list *rl;
669 char *return_ranges;
670 int nr_states;
672 expr = strip_expr(expr);
674 if (!expr) {
675 return_ranges = alloc_sname("");
676 } else if (is_condition(expr)) {
677 call_return_state_hooks_compare(expr);
678 return;
679 } else if (call_return_state_hooks_split_possible(expr)) {
680 return;
681 } else if (get_implied_rl(expr, &rl)) {
682 rl = cast_rl(cur_func_return_type(), rl);
683 return_ranges = show_rl(rl);
684 } else {
685 rl = alloc_whole_rl(cur_func_return_type());
686 return_ranges = show_rl(rl);
689 return_id++;
690 slist = __get_cur_slist();
691 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
692 FOR_EACH_PTR(returned_state_callbacks, cb) {
693 if (nr_states < 10000)
694 cb->callback(return_id, return_ranges, expr, slist);
695 else
696 cb->callback(return_id, return_ranges, expr, NULL);
697 } END_FOR_EACH_PTR(cb);
700 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
702 struct returned_member_callback *cb;
703 struct state_list *my_slist;
704 struct sm_state *sm;
705 struct symbol *type;
706 char *name;
707 char member_name[256];
708 int len;
710 type = get_type(expr);
711 if (!type || type->type != SYM_PTR)
712 return;
713 type = get_real_base_type(type);
714 if (!type || type->type != SYM_STRUCT)
715 return;
716 name = expr_to_var(expr);
717 if (!name)
718 return;
720 member_name[sizeof(member_name) - 1] = '\0';
721 strcpy(member_name, "$$");
723 len = strlen(name);
724 FOR_EACH_PTR(returned_member_callbacks, cb) {
725 my_slist = get_all_states_slist(cb->owner, slist);
726 FOR_EACH_PTR(my_slist, sm) {
727 if (strncmp(sm->name, name, len) != 0)
728 continue;
729 if (strncmp(sm->name + len, "->", 2) != 0)
730 continue;
731 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
732 cb->callback(return_id, return_ranges, member_name, sm->state);
733 } END_FOR_EACH_PTR(sm);
734 free_slist(&my_slist);
735 } END_FOR_EACH_PTR(cb);
737 free_string(name);
740 static void match_end_func_info(struct symbol *sym)
742 if (__path_is_null())
743 return;
744 call_return_state_hooks(NULL);
747 static void init_memdb(void)
749 char *err = NULL;
750 int rc;
751 const char *schema_files[] = {
752 "db/db.schema",
753 "db/caller_info.schema",
754 "db/return_states.schema",
755 "db/type_size.schema",
756 "db/call_implies.schema",
757 "db/function_ptr.schema",
758 "db/return_values.schema",
760 static char buf[4096];
761 int fd;
762 int ret;
763 int i;
765 rc = sqlite3_open(":memory:", &mem_db);
766 if (rc != SQLITE_OK) {
767 printf("Error starting In-Memory database.");
768 return;
771 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
772 fd = open_data_file(schema_files[i]);
773 if (fd < 0)
774 continue;
775 ret = read(fd, buf, sizeof(buf));
776 if (ret == sizeof(buf)) {
777 printf("Schema file too large: %s (limit %ld bytes)",
778 schema_files[i], sizeof(buf));
780 buf[ret] = '\0';
781 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
782 if (rc != SQLITE_OK) {
783 fprintf(stderr, "SQL error #2: %s\n", err);
784 fprintf(stderr, "%s\n", buf);
789 void open_smatch_db(void)
791 int rc;
793 if (option_no_db)
794 return;
796 init_memdb();
798 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
799 if (rc != SQLITE_OK) {
800 option_no_db = 1;
801 return;
803 return;
806 void register_definition_db_callbacks(int id)
808 add_hook(&match_function_def, FUNC_DEF_HOOK);
810 if (option_info) {
811 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
812 add_hook(&match_function_assign, ASSIGNMENT_HOOK);
813 add_hook(&match_function_assign, GLOBAL_ASSIGNMENT_HOOK);
814 add_hook(&global_variable, BASE_HOOK);
815 add_hook(&global_variable, DECLARATION_HOOK);
816 add_returned_state_callback(match_return_info);
817 add_returned_state_callback(print_returned_struct_members);
818 add_hook(&call_return_state_hooks, RETURN_HOOK);
819 add_hook(&match_end_func_info, END_FUNC_HOOK);
822 if (option_no_db)
823 return;
825 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
826 add_hook(&match_call_implies, FUNCTION_CALL_HOOK);
829 void register_db_call_marker(int id)
831 if (!option_info)
832 return;
833 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
836 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
838 char buf[256];
839 char *tmp;
841 if (strcmp(key, "$$") == 0)
842 return expr_to_var_sym(arg, sym);
844 if (strcmp(key, "*$$") == 0) {
845 if (arg->type == EXPR_PREOP && arg->op == '&') {
846 arg = strip_expr(arg->unop);
847 return expr_to_var_sym(arg, sym);
848 } else {
849 tmp = expr_to_var_sym(arg, sym);
850 if (!tmp)
851 return NULL;
852 snprintf(buf, sizeof(buf), "*%s", tmp);
853 free_string(tmp);
854 return alloc_string(buf);
858 if (arg->type == EXPR_PREOP && arg->op == '&') {
859 arg = strip_expr(arg->unop);
860 tmp = expr_to_var_sym(arg, sym);
861 if (!tmp)
862 return NULL;
863 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
864 return alloc_string(buf);
867 tmp = expr_to_var_sym(arg, sym);
868 if (!tmp)
869 return NULL;
870 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
871 free_string(tmp);
872 return alloc_string(buf);