silence bogus "unreachable code" warnings
[smatch.git] / smatch_db.c
blob0f859add1fcdeeb821a32b5d37951187ba42c626
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 "smatch.h"
14 #include "smatch_slist.h"
15 #include "smatch_extra.h"
17 static sqlite3 *db;
19 struct def_callback {
20 int hook_type;
21 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
23 ALLOCATOR(def_callback, "definition db hook callbacks");
24 DECLARE_PTR_LIST(callback_list, struct def_callback);
25 static struct callback_list *callbacks;
27 struct member_info_callback {
28 int owner;
29 void (*callback)(char *fn, char *global_static, int param, char *printed_name, struct smatch_state *state);
31 ALLOCATOR(member_info_callback, "caller_info callbacks");
32 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
33 static struct member_info_cb_list *member_callbacks;
35 struct returned_state_callback {
36 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr, struct state_list *slist);
38 ALLOCATOR(returned_state_callback, "returned state callbacks");
39 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
40 static struct returned_state_cb_list *returned_state_callbacks;
42 struct returned_member_callback {
43 int owner;
44 void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state);
46 ALLOCATOR(returned_member_callback, "returned member callbacks");
47 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
48 static struct returned_member_cb_list *returned_member_callbacks;
50 struct call_implies_callback {
51 int type;
52 void (*callback)(struct expression *arg, char *value);
54 ALLOCATOR(call_implies_callback, "call_implies callbacks");
55 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
56 static struct call_implies_cb_list *call_implies_cb_list;
58 void sql_exec(int (*callback)(void*, int, char**, char**), const char *sql)
60 char *err = NULL;
61 int rc;
63 if (option_no_db || !db)
64 return;
66 rc = sqlite3_exec(db, sql, callback, 0, &err);
67 if (rc != SQLITE_OK) {
68 fprintf(stderr, "SQL error #2: %s\n", err);
69 fprintf(stderr, "SQL: '%s'\n", sql);
73 void add_definition_db_callback(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
75 struct def_callback *def_callback = __alloc_def_callback(0);
77 def_callback->hook_type = type;
78 def_callback->callback = callback;
79 add_ptr_list(&callbacks, def_callback);
83 * These call backs are used when the --info option is turned on to print struct
84 * member information. For example foo->bar could have a state in
85 * smatch_extra.c and also check_user.c.
87 void add_member_info_callback(int owner, void (*callback)(char *fn, char *global_static, int param, char *printed_name, struct smatch_state *state))
89 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
91 member_callback->owner = owner;
92 member_callback->callback = callback;
93 add_ptr_list(&member_callbacks, member_callback);
96 void add_returned_state_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr, struct state_list *slist))
98 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
100 callback->callback = fn;
101 add_ptr_list(&returned_state_callbacks, callback);
104 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state))
106 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
108 member_callback->owner = owner;
109 member_callback->callback = callback;
110 add_ptr_list(&returned_member_callbacks, member_callback);
113 void add_db_fn_call_callback(int type, void (*callback)(struct expression *arg, char *value))
115 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
117 cb->type = type;
118 cb->callback = callback;
119 add_ptr_list(&call_implies_cb_list, cb);
122 static struct symbol *return_type;
123 static struct range_list *return_range_list;
124 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
126 if (argc != 1)
127 return 0;
128 if (option_debug)
129 sm_msg("return type %d", type_positive_bits(return_type));
130 str_to_rl(return_type, argv[0], &return_range_list);
131 return 0;
134 struct range_list *db_return_vals(struct expression *expr)
136 struct symbol *sym;
137 static char sql_filter[1024];
139 if (expr->type != EXPR_CALL)
140 return NULL;
141 if (expr->fn->type != EXPR_SYMBOL)
142 return NULL;
143 return_type = get_type(expr);
144 if (!return_type)
145 return NULL;
146 sym = expr->fn->symbol;
147 if (!sym)
148 return NULL;
150 if (sym->ctype.modifiers & MOD_STATIC) {
151 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
152 get_filename(), sym->ident->name);
153 } else {
154 snprintf(sql_filter, 1024, "function = '%s' and static = 0;",
155 sym->ident->name);
158 return_range_list = NULL;
159 run_sql(db_return_callback, "select return from return_values where %s",
160 sql_filter);
161 return return_range_list;
164 static void match_call_hack(struct expression *expr)
166 char *name;
169 * we just want to record something in the database so that if we have
170 * two calls like: frob(4); frob(some_unkown); then on the receiving
171 * side we know that sometimes frob is called with unknown parameters.
174 name = get_fnptr_name(expr->fn);
175 if (!name)
176 return;
177 sm_msg("info: call_marker '%s' %s", name, is_static(expr->fn) ? "static" : "global");
178 free_string(name);
181 static void print_struct_members(char *fn, char *global_static, struct expression *expr, int param, struct state_list *slist,
182 void (*callback)(char *fn, char *global_static, int param, char *printed_name, struct smatch_state *state))
184 struct sm_state *sm;
185 char *name;
186 struct symbol *sym;
187 int len;
188 char printed_name[256];
189 int is_address = 0;
191 expr = strip_expr(expr);
192 if (expr->type == EXPR_PREOP && expr->op == '&') {
193 expr = strip_expr(expr->unop);
194 is_address = 1;
197 name = expr_to_var_sym(expr, &sym);
198 if (!name || !sym)
199 goto free;
201 len = strlen(name);
202 FOR_EACH_PTR(slist, sm) {
203 if (sm->sym != sym)
204 continue;
205 if (strncmp(name, sm->name, len) || sm->name[len] == '\0')
206 continue;
207 if (is_address)
208 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
209 else
210 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
211 callback(fn, global_static, param, printed_name, sm->state);
212 } END_FOR_EACH_PTR(sm);
213 free:
214 free_string(name);
217 static void match_call_info(struct expression *expr)
219 struct member_info_callback *cb;
220 struct expression *arg;
221 struct state_list *slist;
222 char *name;
223 int i;
224 char *gs;
226 name = get_fnptr_name(expr->fn);
227 if (!name)
228 return;
230 if (is_static(expr->fn))
231 gs = (char *)"static";
232 else
233 gs = (char *)"global";
235 FOR_EACH_PTR(member_callbacks, cb) {
236 slist = get_all_states(cb->owner);
237 i = 0;
238 FOR_EACH_PTR(expr->args, arg) {
239 print_struct_members(name, gs, arg, i, slist, cb->callback);
240 i++;
241 } END_FOR_EACH_PTR(arg);
242 free_slist(&slist);
243 } END_FOR_EACH_PTR(cb);
245 free_string(name);
248 static int get_param(int param, char **name, struct symbol **sym)
250 struct symbol *arg;
251 int i;
253 i = 0;
254 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
256 * this is a temporary hack to work around a bug (I think in sparse?)
257 * 2.6.37-rc1:fs/reiserfs/journal.o
258 * If there is a function definition without parameter name found
259 * after a function implementation then it causes a crash.
260 * int foo() {}
261 * int bar(char *);
263 if (arg->ident->name < (char *)100)
264 continue;
265 if (i == param && arg->ident->name) {
266 *name = arg->ident->name;
267 *sym = arg;
268 return TRUE;
270 i++;
271 } END_FOR_EACH_PTR(arg);
273 return FALSE;
276 static struct state_list *final_states;
277 static int prev_func_id = -1;
278 static int db_callback(void *unused, int argc, char **argv, char **azColName)
280 int func_id;
281 long type;
282 long param;
283 char *name = NULL;
284 struct symbol *sym = NULL;
285 struct def_callback *def_callback;
287 if (argc != 5)
288 return 0;
290 func_id = atoi(argv[0]);
291 errno = 0;
292 type = strtol(argv[1], NULL, 10);
293 param = strtol(argv[2], NULL, 10);
294 if (errno)
295 return 0;
297 if (prev_func_id == -1)
298 prev_func_id = func_id;
299 if (func_id != prev_func_id) {
300 merge_slist(&final_states, __pop_fake_cur_slist());
301 __push_fake_cur_slist();
302 __unnullify_path();
303 prev_func_id = func_id;
306 if (type == INTERNAL)
307 return 0;
308 if (param >= 0 && !get_param(param, &name, &sym))
309 return 0;
311 FOR_EACH_PTR(callbacks, def_callback) {
312 if (def_callback->hook_type == type)
313 def_callback->callback(name, sym, argv[3], argv[4]);
314 } END_FOR_EACH_PTR(def_callback);
316 return 0;
319 static void get_direct_callers(struct symbol *sym)
321 char sql_filter[1024];
323 if (sym->ctype.modifiers & MOD_STATIC) {
324 snprintf(sql_filter, 1024,
325 "file = '%s' and function = '%s' order by function_id;",
326 get_filename(), sym->ident->name);
327 } else {
328 snprintf(sql_filter, 1024,
329 "function = '%s' and static = 0 order by function_id;",
330 sym->ident->name);
333 run_sql(db_callback, "select function_id, type, parameter, key, value from caller_info"
334 " where %s", sql_filter);
337 static char *ptr_name;
338 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
340 if (!ptr_name)
341 ptr_name = alloc_string(argv[0]);
342 return 0;
345 static void get_function_pointer_callers(struct symbol *sym)
347 char sql_filter[1024];
349 if (sym->ctype.modifiers & MOD_STATIC) {
350 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
351 get_filename(), sym->ident->name);
352 } else {
353 snprintf(sql_filter, 1024, "function = '%s';",
354 sym->ident->name);
357 ptr_name = NULL;
358 run_sql(get_ptr_name, "select ptr from function_ptr where %s", sql_filter);
359 if (!ptr_name)
360 return;
362 run_sql(db_callback, "select function_id, type, parameter, key, value from caller_info"
363 " where function = '%s' order by function_id", ptr_name);
365 free_string(ptr_name);
368 static void match_data_from_db(struct symbol *sym)
370 struct sm_state *sm;
372 if (!sym || !sym->ident || !sym->ident->name)
373 return;
375 __push_fake_cur_slist();
376 __unnullify_path();
377 prev_func_id = -1;
379 get_direct_callers(sym);
380 get_function_pointer_callers(sym);
382 merge_slist(&final_states, __pop_fake_cur_slist());
384 FOR_EACH_PTR(final_states, sm) {
385 __set_sm(sm);
386 } END_FOR_EACH_PTR(sm);
388 free_slist(&final_states);
391 static void match_function_assign(struct expression *expr)
393 struct expression *right = expr->right;
394 struct symbol *sym;
395 char *fn_name;
396 char *ptr_name;
398 if (right->type == EXPR_PREOP && right->op == '&')
399 right = strip_expr(right->unop);
400 if (right->type != EXPR_SYMBOL)
401 return;
402 sym = get_type(right);
403 if (!sym || sym->type != SYM_FN)
404 return;
406 fn_name = expr_to_var(right);
407 ptr_name = get_fnptr_name(expr->left);
408 if (!fn_name || !ptr_name)
409 goto free;
411 sm_msg("info: sets_fn_ptr '%s' '%s'", ptr_name, fn_name);
413 free:
414 free_string(fn_name);
415 free_string(ptr_name);
418 static struct expression *call_implies_call_expr;
419 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
421 struct call_implies_callback *cb;
422 struct expression *arg = NULL;
423 int type;
424 int param;
426 if (argc != 4)
427 return 0;
429 type = atoi(argv[1]);
430 param = atoi(argv[2]);
432 FOR_EACH_PTR(call_implies_cb_list, cb) {
433 if (cb->type != type)
434 continue;
435 if (param != -1) {
436 arg = get_argument_from_call_expr(call_implies_call_expr->args, param);
437 if (!arg)
438 continue;
440 cb->callback(arg, argv[3]);
441 } END_FOR_EACH_PTR(cb);
443 return 0;
446 static void match_call_implies(struct expression *expr)
448 struct symbol *sym;
449 static char sql_filter[1024];
451 if (expr->fn->type != EXPR_SYMBOL)
452 return;
453 sym = expr->fn->symbol;
454 if (!sym)
455 return;
457 if (sym->ctype.modifiers & MOD_STATIC) {
458 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
459 get_filename(), sym->ident->name);
460 } else {
461 snprintf(sql_filter, 1024, "function = '%s' and static = 0;",
462 sym->ident->name);
465 call_implies_call_expr = expr;
466 run_sql(call_implies_callbacks,
467 "select function, type, parameter, value from call_implies where %s",
468 sql_filter);
469 return;
472 static void print_initializer_list(struct expression_list *expr_list,
473 struct symbol *struct_type)
475 struct expression *expr;
476 struct symbol *base_type;
478 FOR_EACH_PTR(expr_list, expr) {
479 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
480 print_initializer_list(expr->idx_expression->expr_list, struct_type);
481 continue;
483 if (expr->type != EXPR_IDENTIFIER)
484 continue;
485 if (!expr->expr_ident)
486 continue;
487 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
488 continue;
489 base_type = get_type(expr->ident_expression);
490 if (!base_type || base_type->type != SYM_FN)
491 continue;
492 sm_msg("info: sets_fn_ptr '(struct %s)->%s' '%s'", struct_type->ident->name,
493 expr->expr_ident->name,
494 expr->ident_expression->symbol_name->name);
495 } END_FOR_EACH_PTR(expr);
498 static void global_variable(struct symbol *sym)
500 struct symbol *struct_type;
502 if (!sym->ident)
503 return;
504 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
505 return;
506 struct_type = get_base_type(sym);
507 if (!struct_type)
508 return;
509 if (struct_type->type == SYM_ARRAY) {
510 struct_type = get_base_type(struct_type);
511 if (!struct_type)
512 return;
514 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
515 return;
516 print_initializer_list(sym->initializer->expr_list, struct_type);
519 static void match_return_info(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
521 sm_msg("info: return_marker %d '%s' %s", return_id, return_ranges,
522 global_static());
525 static int return_id;
526 static void match_function_def(struct symbol *sym)
528 return_id = 0;
531 static void call_return_state_hooks_compare(struct expression *expr)
533 struct returned_state_callback *cb;
534 struct state_list *slist;
535 char *return_ranges;
536 int final_pass_orig = final_pass;
538 __push_fake_cur_slist();
540 final_pass = 0;
541 __split_whole_condition(expr);
542 final_pass = final_pass_orig;
544 return_ranges = alloc_sname("1");
546 return_id++;
547 slist = __get_cur_slist();
548 FOR_EACH_PTR(returned_state_callbacks, cb) {
549 cb->callback(return_id, return_ranges, expr, slist);
550 } END_FOR_EACH_PTR(cb);
552 __push_true_states();
553 __use_false_states();
555 return_ranges = alloc_sname("0");;
556 return_id++;
557 slist = __get_cur_slist();
558 FOR_EACH_PTR(returned_state_callbacks, cb) {
559 cb->callback(return_id, return_ranges, expr, slist);
560 } END_FOR_EACH_PTR(cb);
562 __merge_true_states();
563 __pop_fake_cur_slist();
566 static int call_return_state_hooks_split_possible(struct expression *expr)
568 struct returned_state_callback *cb;
569 struct state_list *slist;
570 struct range_list *rl;
571 char *return_ranges;
572 struct sm_state *sm;
573 struct sm_state *tmp;
574 int ret = 0;
575 int nr_possible, nr_states;
577 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
578 if (!sm || !sm->merged)
579 return 0;
581 /* bail if it gets too complicated */
582 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
583 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
584 if (nr_possible >= 100)
585 return 0;
586 if (nr_states * nr_possible >= 1000)
587 return 0;
589 FOR_EACH_PTR(sm->possible, tmp) {
590 if (tmp->merged)
591 continue;
593 ret = 1;
594 __push_fake_cur_slist();
596 overwrite_states_using_pool(tmp);
598 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
599 return_ranges = show_rl(rl);
601 return_id++;
602 slist = __get_cur_slist();
603 FOR_EACH_PTR(returned_state_callbacks, cb) {
604 cb->callback(return_id, return_ranges, expr, slist);
605 } END_FOR_EACH_PTR(cb);
607 __pop_fake_cur_slist();
608 } END_FOR_EACH_PTR(tmp);
610 return ret;
613 static void call_return_state_hooks(struct expression *expr)
615 struct returned_state_callback *cb;
616 struct state_list *slist;
617 struct range_list *rl;
618 char *return_ranges;
619 int nr_states;
621 expr = strip_expr(expr);
623 if (!expr) {
624 return_ranges = alloc_sname("");
625 } else if (is_condition(expr)) {
626 call_return_state_hooks_compare(expr);
627 return;
628 } else if (call_return_state_hooks_split_possible(expr)) {
629 return;
630 } else if (get_implied_rl(expr, &rl)) {
631 rl = cast_rl(cur_func_return_type(), rl);
632 return_ranges = show_rl(rl);
633 } else {
634 rl = alloc_whole_rl(cur_func_return_type());
635 return_ranges = show_rl(rl);
638 return_id++;
639 slist = __get_cur_slist();
640 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
641 FOR_EACH_PTR(returned_state_callbacks, cb) {
642 if (nr_states < 10000)
643 cb->callback(return_id, return_ranges, expr, slist);
644 else
645 cb->callback(return_id, return_ranges, expr, NULL);
646 } END_FOR_EACH_PTR(cb);
649 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
651 struct returned_member_callback *cb;
652 struct state_list *my_slist;
653 struct sm_state *sm;
654 struct symbol *type;
655 char *name;
656 char member_name[256];
657 int len;
659 type = get_type(expr);
660 if (!type || type->type != SYM_PTR)
661 return;
662 type = get_real_base_type(type);
663 if (!type || type->type != SYM_STRUCT)
664 return;
665 name = expr_to_var(expr);
666 if (!name)
667 return;
669 member_name[sizeof(member_name) - 1] = '\0';
670 strcpy(member_name, "$$");
672 len = strlen(name);
673 FOR_EACH_PTR(returned_member_callbacks, cb) {
674 my_slist = get_all_states_slist(cb->owner, slist);
675 FOR_EACH_PTR(my_slist, sm) {
676 if (strncmp(sm->name, name, len) != 0)
677 continue;
678 if (strncmp(sm->name + len, "->", 2) != 0)
679 continue;
680 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
681 cb->callback(return_id, return_ranges, member_name, sm->state);
682 } END_FOR_EACH_PTR(sm);
683 free_slist(&my_slist);
684 } END_FOR_EACH_PTR(cb);
686 free_string(name);
689 static void match_end_func_info(struct symbol *sym)
691 if (__path_is_null())
692 return;
693 call_return_state_hooks(NULL);
696 void open_smatch_db(void)
698 #ifdef SQLITE_OPEN_READONLY
699 int rc;
701 if (option_no_db)
702 return;
704 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
705 if (rc != SQLITE_OK) {
706 option_no_db = 1;
707 return;
709 return;
710 #else
711 option_no_db = 1;
712 return;
713 #endif
716 void register_definition_db_callbacks(int id)
718 add_hook(&match_function_def, FUNC_DEF_HOOK);
720 if (option_info) {
721 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
722 add_hook(&match_call_hack, FUNCTION_CALL_HOOK);
723 add_hook(&match_function_assign, ASSIGNMENT_HOOK);
724 add_hook(&match_function_assign, GLOBAL_ASSIGNMENT_HOOK);
725 add_hook(&global_variable, BASE_HOOK);
726 add_hook(&global_variable, DECLARATION_HOOK);
727 add_returned_state_callback(match_return_info);
728 add_returned_state_callback(print_returned_struct_members);
729 add_hook(&call_return_state_hooks, RETURN_HOOK);
730 add_hook(&match_end_func_info, END_FUNC_HOOK);
733 if (option_no_db)
734 return;
736 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
737 add_hook(&match_call_implies, FUNCTION_CALL_HOOK);
740 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
742 char buf[256];
743 char *tmp;
745 if (strcmp(key, "$$") == 0)
746 return expr_to_var_sym(arg, sym);
748 if (strcmp(key, "*$$") == 0) {
749 if (arg->type == EXPR_PREOP && arg->op == '&') {
750 arg = strip_expr(arg->unop);
751 return expr_to_var_sym(arg, sym);
752 } else {
753 tmp = expr_to_var_sym(arg, sym);
754 if (!tmp)
755 return NULL;
756 snprintf(buf, sizeof(buf), "*%s", tmp);
757 free_string(tmp);
758 return alloc_string(buf);
762 if (arg->type == EXPR_PREOP && arg->op == '&') {
763 arg = strip_expr(arg->unop);
764 tmp = expr_to_var_sym(arg, sym);
765 if (!tmp)
766 return NULL;
767 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
768 return alloc_string(buf);
771 tmp = expr_to_var_sym(arg, sym);
772 if (!tmp)
773 return NULL;
774 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
775 free_string(tmp);
776 return alloc_string(buf);