4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
15 #include "smatch_slist.h"
16 #include "smatch_extra.h"
19 static sqlite3
*mem_db
;
21 #define sql_insert(table, values...) \
25 char *err, *p = buf; \
28 p += snprintf(p, buf + sizeof(buf) - p, \
29 "insert into %s values (", #table); \
30 p += snprintf(p, buf + sizeof(buf) - p, values); \
31 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
32 sm_debug("in-mem: %s\n", buf); \
33 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err); \
34 if (rc != SQLITE_OK) { \
35 fprintf(stderr, "SQL error #2: %s\n", err); \
36 fprintf(stderr, "SQL: '%s'\n", buf); \
42 sm_printf("SQL: insert into " #table " values (" values); \
49 void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
);
51 ALLOCATOR(def_callback
, "definition db hook callbacks");
52 DECLARE_PTR_LIST(callback_list
, struct def_callback
);
53 static struct callback_list
*callbacks
;
55 struct member_info_callback
{
57 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
);
59 ALLOCATOR(member_info_callback
, "caller_info callbacks");
60 DECLARE_PTR_LIST(member_info_cb_list
, struct member_info_callback
);
61 static struct member_info_cb_list
*member_callbacks
;
63 struct returned_state_callback
{
64 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*return_expr
, struct state_list
*slist
);
66 ALLOCATOR(returned_state_callback
, "returned state callbacks");
67 DECLARE_PTR_LIST(returned_state_cb_list
, struct returned_state_callback
);
68 static struct returned_state_cb_list
*returned_state_callbacks
;
70 struct returned_member_callback
{
72 void (*callback
)(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
);
74 ALLOCATOR(returned_member_callback
, "returned member callbacks");
75 DECLARE_PTR_LIST(returned_member_cb_list
, struct returned_member_callback
);
76 static struct returned_member_cb_list
*returned_member_callbacks
;
78 struct call_implies_callback
{
80 void (*callback
)(struct expression
*arg
, char *value
);
82 ALLOCATOR(call_implies_callback
, "call_implies callbacks");
83 DECLARE_PTR_LIST(call_implies_cb_list
, struct call_implies_callback
);
84 static struct call_implies_cb_list
*call_implies_cb_list
;
86 void sql_exec(int (*callback
)(void*, int, char**, char**), const char *sql
)
91 if (option_no_db
|| !db
)
94 rc
= sqlite3_exec(db
, sql
, callback
, 0, &err
);
95 if (rc
!= SQLITE_OK
) {
96 fprintf(stderr
, "SQL error #2: %s\n", err
);
97 fprintf(stderr
, "SQL: '%s'\n", sql
);
101 void sql_mem_exec(int (*callback
)(void*, int, char**, char**), const char *sql
)
106 rc
= sqlite3_exec(mem_db
, sql
, callback
, 0, &err
);
107 if (rc
!= SQLITE_OK
) {
108 fprintf(stderr
, "SQL error #2: %s\n", err
);
109 fprintf(stderr
, "SQL: '%s'\n", sql
);
113 void sql_insert_return_states(int return_id
, const char *return_ranges
,
114 int type
, int param
, const char *key
, const char *value
)
116 if (key
&& strlen(key
) >= 80)
118 sql_insert(return_states
, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
119 get_base_file(), get_function(), (unsigned long)__inline_fn
,
120 return_id
, return_ranges
, fn_static(), type
, param
, key
, value
);
123 static struct string_list
*common_funcs
;
124 static int is_common_function(const char *fn
)
128 if (strncmp(fn
, "__builtin_", 10) == 0)
131 FOR_EACH_PTR(common_funcs
, tmp
) {
132 if (strcmp(tmp
, fn
) == 0)
134 } END_FOR_EACH_PTR(tmp
);
139 void sql_insert_caller_info(struct expression
*call
, int type
,
140 int param
, const char *key
, const char *value
)
144 if (!option_info
&& !__inline_call
)
147 if (key
&& strlen(key
) >= 80)
150 fn
= get_fnptr_name(call
->fn
);
156 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
157 get_base_file(), get_function(), fn
, (unsigned long)call
,
158 is_static(call
->fn
), type
, param
, key
, value
);
164 if (is_common_function(fn
))
167 sm_msg("SQL_caller_info: insert into caller_info values ("
168 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
169 get_base_file(), get_function(), fn
, is_static(call
->fn
),
170 type
, param
, key
, value
);
175 void sql_insert_function_ptr(const char *fn
, const char *struct_name
)
177 sql_insert(function_ptr
, "'%s', '%s', '%s'", get_base_file(), fn
,
181 void sql_insert_return_values(const char *return_values
)
183 sql_insert(return_values
, "'%s', '%s', %lu, %d, '%s'", get_base_file(),
184 get_function(), (unsigned long)__inline_fn
, fn_static(),
188 void sql_insert_call_implies(int type
, int param
, int value
)
190 sql_insert(call_implies
, "'%s', '%s', %lu, %d, %d, %d, %d", get_base_file(),
191 get_function(), (unsigned long)__inline_fn
, fn_static(),
195 void sql_insert_type_size(const char *member
, int size
)
197 sql_insert(type_size
, "'%s', '%s', %d", get_base_file(), member
, size
);
200 void sql_insert_local_values(const char *name
, const char *value
)
202 sql_insert(local_values
, "'%s', '%s', '%s'", get_base_file(), name
, value
);
205 static char *get_static_filter(struct symbol
*sym
)
207 static char sql_filter
[1024];
209 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
210 snprintf(sql_filter
, sizeof(sql_filter
),
211 "file = '%s' and function = '%s' and static = '1'",
212 get_base_file(), sym
->ident
->name
);
214 snprintf(sql_filter
, sizeof(sql_filter
),
215 "function = '%s' and static = '0'", sym
->ident
->name
);
221 static int row_count
;
222 static int get_row_count(void *unused
, int argc
, char **argv
, char **azColName
)
226 row_count
= atoi(argv
[0]);
230 void sql_select_return_states(const char *cols
, struct expression
*call
,
231 int (*callback
)(void*, int, char**, char**))
233 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
236 if (inlinable(call
->fn
)) {
238 "select %s from return_states where call_id = '%lu';",
239 cols
, (unsigned long)call
);
244 run_sql(get_row_count
, "select count(*) from return_states where %s;",
245 get_static_filter(call
->fn
->symbol
));
246 if (row_count
> 1000)
249 run_sql(callback
, "select %s from return_states where %s;",
250 cols
, get_static_filter(call
->fn
->symbol
));
253 void sql_select_call_implies(const char *cols
, struct expression
*call
,
254 int (*callback
)(void*, int, char**, char**))
256 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
259 if (inlinable(call
->fn
)) {
261 "select %s from call_implies where call_id = '%lu';",
262 cols
, (unsigned long)call
);
266 run_sql(callback
, "select %s from call_implies where %s;",
267 cols
, get_static_filter(call
->fn
->symbol
));
270 void sql_select_return_values(const char *cols
, struct expression
*call
,
271 int (*callback
)(void*, int, char**, char**))
273 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
276 if (inlinable(call
->fn
)) {
278 "select %s from return_values where call_id = '%lu';",
279 cols
, (unsigned long)call
);
283 run_sql(callback
, "select %s from return_values where %s;",
284 cols
, get_static_filter(call
->fn
->symbol
));
287 void sql_select_caller_info(const char *cols
, struct symbol
*sym
,
288 int (*callback
)(void*, int, char**, char**))
291 mem_sql(callback
, "select %s from caller_info where call_id = %lu;",
292 cols
, (unsigned long)__inline_fn
);
297 "select %s from caller_info where %s order by call_id;",
298 cols
, get_static_filter(sym
));
301 void add_definition_db_callback(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
303 struct def_callback
*def_callback
= __alloc_def_callback(0);
305 def_callback
->hook_type
= type
;
306 def_callback
->callback
= callback
;
307 add_ptr_list(&callbacks
, def_callback
);
311 * These call backs are used when the --info option is turned on to print struct
312 * member information. For example foo->bar could have a state in
313 * smatch_extra.c and also check_user.c.
315 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
))
317 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
319 member_callback
->owner
= owner
;
320 member_callback
->callback
= callback
;
321 add_ptr_list(&member_callbacks
, member_callback
);
324 void add_returned_state_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
, struct state_list
*slist
))
326 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
328 callback
->callback
= fn
;
329 add_ptr_list(&returned_state_callbacks
, callback
);
332 void add_returned_member_callback(int owner
, void (*callback
)(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
))
334 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
336 member_callback
->owner
= owner
;
337 member_callback
->callback
= callback
;
338 add_ptr_list(&returned_member_callbacks
, member_callback
);
341 void add_db_fn_call_callback(int type
, void (*callback
)(struct expression
*arg
, char *value
))
343 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
346 cb
->callback
= callback
;
347 add_ptr_list(&call_implies_cb_list
, cb
);
350 static struct symbol
*return_type
;
351 static struct range_list
*return_range_list
;
352 static int db_return_callback(void *unused
, int argc
, char **argv
, char **azColName
)
357 sm_msg("return type %d", type_positive_bits(return_type
));
358 str_to_rl(return_type
, argv
[0], &return_range_list
);
362 struct range_list
*db_return_vals(struct expression
*expr
)
364 return_type
= get_type(expr
);
367 return_range_list
= NULL
;
368 sql_select_return_values("return", expr
, db_return_callback
);
369 return return_range_list
;
372 static void match_call_marker(struct expression
*expr
)
375 * we just want to record something in the database so that if we have
376 * two calls like: frob(4); frob(some_unkown); then on the receiving
377 * side we know that sometimes frob is called with unknown parameters.
380 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", "");
383 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct state_list
*slist
,
384 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
))
390 char printed_name
[256];
393 expr
= strip_expr(expr
);
394 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
395 expr
= strip_expr(expr
->unop
);
399 name
= expr_to_var_sym(expr
, &sym
);
404 FOR_EACH_PTR(slist
, sm
) {
407 if (strcmp(name
, sm
->name
) == 0) {
409 snprintf(printed_name
, sizeof(printed_name
), "*$$");
410 else /* these are already handled. fixme: handle them here */
412 } else if (sm
->name
[0] == '*' && strcmp(name
, sm
->name
+ 1) == 0) {
413 snprintf(printed_name
, sizeof(printed_name
), "*$$");
414 } else if (strncmp(name
, sm
->name
, len
) == 0) {
416 snprintf(printed_name
, sizeof(printed_name
), "$$->%s", sm
->name
+ len
+ 1);
418 snprintf(printed_name
, sizeof(printed_name
), "$$%s", sm
->name
+ len
);
422 callback(call
, param
, printed_name
, sm
->state
);
423 } END_FOR_EACH_PTR(sm
);
428 static void match_call_info(struct expression
*call
)
430 struct member_info_callback
*cb
;
431 struct expression
*arg
;
432 struct state_list
*slist
;
436 name
= get_fnptr_name(call
->fn
);
440 FOR_EACH_PTR(member_callbacks
, cb
) {
441 slist
= get_all_states(cb
->owner
);
443 FOR_EACH_PTR(call
->args
, arg
) {
444 print_struct_members(call
, arg
, i
, slist
, cb
->callback
);
446 } END_FOR_EACH_PTR(arg
);
448 } END_FOR_EACH_PTR(cb
);
453 static int get_param(int param
, char **name
, struct symbol
**sym
)
459 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
461 * this is a temporary hack to work around a bug (I think in sparse?)
462 * 2.6.37-rc1:fs/reiserfs/journal.o
463 * If there is a function definition without parameter name found
464 * after a function implementation then it causes a crash.
468 if (arg
->ident
->name
< (char *)100)
470 if (i
== param
&& arg
->ident
->name
) {
471 *name
= arg
->ident
->name
;
476 } END_FOR_EACH_PTR(arg
);
481 static struct state_list
*final_states
;
482 static int prev_func_id
= -1;
483 static int db_callback(void *unused
, int argc
, char **argv
, char **azColName
)
489 struct symbol
*sym
= NULL
;
490 struct def_callback
*def_callback
;
495 func_id
= atoi(argv
[0]);
497 type
= strtol(argv
[1], NULL
, 10);
498 param
= strtol(argv
[2], NULL
, 10);
502 if (prev_func_id
== -1)
503 prev_func_id
= func_id
;
504 if (func_id
!= prev_func_id
) {
505 merge_slist(&final_states
, __pop_fake_cur_slist());
506 __push_fake_cur_slist();
508 prev_func_id
= func_id
;
511 if (type
== INTERNAL
)
513 if (param
>= 0 && !get_param(param
, &name
, &sym
))
516 FOR_EACH_PTR(callbacks
, def_callback
) {
517 if (def_callback
->hook_type
== type
)
518 def_callback
->callback(name
, sym
, argv
[3], argv
[4]);
519 } END_FOR_EACH_PTR(def_callback
);
524 static void get_direct_callers(struct symbol
*sym
)
526 sql_select_caller_info("call_id, type, parameter, key, value", sym
,
530 static char *ptr_name
;
532 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
535 free_string(ptr_name
);
540 ptr_name
= alloc_string(argv
[0]);
544 static void get_function_pointer_callers(struct symbol
*sym
)
546 char sql_filter
[1024];
548 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
549 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
550 get_base_file(), sym
->ident
->name
);
552 snprintf(sql_filter
, 1024, "function = '%s';",
558 run_sql(get_ptr_name
,
559 "select distinct ptr from function_ptr where %s",
564 run_sql(db_callback
, "select call_id, type, parameter, key, value from caller_info"
565 " where function = '%s' order by call_id", ptr_name
);
567 free_string(ptr_name
);
570 static void match_data_from_db(struct symbol
*sym
)
574 if (!sym
|| !sym
->ident
|| !sym
->ident
->name
)
577 __push_fake_cur_slist();
581 get_direct_callers(sym
);
583 get_function_pointer_callers(sym
);
585 merge_slist(&final_states
, __pop_fake_cur_slist());
587 FOR_EACH_PTR(final_states
, sm
) {
589 } END_FOR_EACH_PTR(sm
);
591 free_slist(&final_states
);
594 static struct expression
*call_implies_call_expr
;
595 static int call_implies_callbacks(void *unused
, int argc
, char **argv
, char **azColName
)
597 struct call_implies_callback
*cb
;
598 struct expression
*arg
= NULL
;
605 type
= atoi(argv
[1]);
606 param
= atoi(argv
[2]);
608 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
609 if (cb
->type
!= type
)
612 arg
= get_argument_from_call_expr(call_implies_call_expr
->args
, param
);
616 cb
->callback(arg
, argv
[3]);
617 } END_FOR_EACH_PTR(cb
);
622 static void match_call_implies(struct expression
*expr
)
624 call_implies_call_expr
= expr
;
625 sql_select_call_implies("function, type, parameter, value", expr
,
626 call_implies_callbacks
);
630 static void print_initializer_list(struct expression_list
*expr_list
,
631 struct symbol
*struct_type
)
633 struct expression
*expr
;
634 struct symbol
*base_type
;
635 char struct_name
[256];
637 FOR_EACH_PTR(expr_list
, expr
) {
638 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
639 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
642 if (expr
->type
!= EXPR_IDENTIFIER
)
644 if (!expr
->expr_ident
)
646 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
648 base_type
= get_type(expr
->ident_expression
);
649 if (!base_type
|| base_type
->type
!= SYM_FN
)
651 snprintf(struct_name
, sizeof(struct_name
), "(struct %s)->%s",
652 struct_type
->ident
->name
, expr
->expr_ident
->name
);
653 sql_insert_function_ptr(expr
->ident_expression
->symbol_name
->name
,
655 } END_FOR_EACH_PTR(expr
);
658 static void global_variable(struct symbol
*sym
)
660 struct symbol
*struct_type
;
664 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
666 struct_type
= get_base_type(sym
);
669 if (struct_type
->type
== SYM_ARRAY
) {
670 struct_type
= get_base_type(struct_type
);
674 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
676 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
679 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
, struct state_list
*slist
)
681 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", "");
684 static int return_id
;
685 static void match_function_def(struct symbol
*sym
)
690 static void call_return_state_hooks_compare(struct expression
*expr
)
692 struct returned_state_callback
*cb
;
693 struct state_list
*slist
;
695 int final_pass_orig
= final_pass
;
697 __push_fake_cur_slist();
700 __split_whole_condition(expr
);
701 final_pass
= final_pass_orig
;
703 return_ranges
= alloc_sname("1");
706 slist
= __get_cur_slist();
707 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
708 cb
->callback(return_id
, return_ranges
, expr
, slist
);
709 } END_FOR_EACH_PTR(cb
);
711 __push_true_states();
712 __use_false_states();
714 return_ranges
= alloc_sname("0");;
716 slist
= __get_cur_slist();
717 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
718 cb
->callback(return_id
, return_ranges
, expr
, slist
);
719 } END_FOR_EACH_PTR(cb
);
721 __merge_true_states();
722 __pop_fake_cur_slist();
725 static int call_return_state_hooks_split_possible(struct expression
*expr
)
727 struct returned_state_callback
*cb
;
728 struct state_list
*slist
;
729 struct range_list
*rl
;
732 struct sm_state
*tmp
;
734 int nr_possible
, nr_states
;
736 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
737 if (!sm
|| !sm
->merged
)
740 /* bail if it gets too complicated */
741 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
742 nr_states
= ptr_list_size((struct ptr_list
*)__get_cur_slist());
743 if (nr_possible
>= 100)
745 if (nr_states
* nr_possible
>= 1000)
748 FOR_EACH_PTR(sm
->possible
, tmp
) {
753 __push_fake_cur_slist();
755 overwrite_states_using_pool(tmp
);
757 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
758 return_ranges
= show_rl(rl
);
761 slist
= __get_cur_slist();
762 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
763 cb
->callback(return_id
, return_ranges
, expr
, slist
);
764 } END_FOR_EACH_PTR(cb
);
766 __pop_fake_cur_slist();
767 } END_FOR_EACH_PTR(tmp
);
772 static void call_return_state_hooks(struct expression
*expr
)
774 struct returned_state_callback
*cb
;
775 struct state_list
*slist
;
776 struct range_list
*rl
;
780 expr
= strip_expr(expr
);
783 return_ranges
= alloc_sname("");
784 } else if (is_condition(expr
)) {
785 call_return_state_hooks_compare(expr
);
787 } else if (call_return_state_hooks_split_possible(expr
)) {
789 } else if (get_implied_rl(expr
, &rl
)) {
790 rl
= cast_rl(cur_func_return_type(), rl
);
791 return_ranges
= show_rl(rl
);
793 rl
= alloc_whole_rl(cur_func_return_type());
794 return_ranges
= show_rl(rl
);
798 slist
= __get_cur_slist();
799 nr_states
= ptr_list_size((struct ptr_list
*)__get_cur_slist());
800 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
801 if (nr_states
< 10000)
802 cb
->callback(return_id
, return_ranges
, expr
, slist
);
804 cb
->callback(return_id
, return_ranges
, expr
, NULL
);
805 } END_FOR_EACH_PTR(cb
);
808 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
, struct state_list
*slist
)
810 struct returned_member_callback
*cb
;
811 struct state_list
*my_slist
;
815 char member_name
[256];
818 type
= get_type(expr
);
819 if (!type
|| type
->type
!= SYM_PTR
)
821 type
= get_real_base_type(type
);
822 if (!type
|| type
->type
!= SYM_STRUCT
)
824 name
= expr_to_var(expr
);
828 member_name
[sizeof(member_name
) - 1] = '\0';
829 strcpy(member_name
, "$$");
832 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
833 my_slist
= get_all_states_slist(cb
->owner
, slist
);
834 FOR_EACH_PTR(my_slist
, sm
) {
835 if (strncmp(sm
->name
, name
, len
) != 0)
837 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
839 strncpy(member_name
+ 2, sm
->name
+ len
, sizeof(member_name
) - 2);
840 cb
->callback(return_id
, return_ranges
, member_name
, sm
->state
);
841 } END_FOR_EACH_PTR(sm
);
842 free_slist(&my_slist
);
843 } END_FOR_EACH_PTR(cb
);
848 static void reset_memdb(void)
850 mem_sql(NULL
, "delete from caller_info;");
851 mem_sql(NULL
, "delete from return_states;");
852 mem_sql(NULL
, "delete from call_implies;");
853 mem_sql(NULL
, "delete from return_values;");
856 static void match_end_func_info(struct symbol
*sym
)
858 if (__path_is_null())
860 call_return_state_hooks(NULL
);
865 static void init_memdb(void)
869 const char *schema_files
[] = {
871 "db/caller_info.schema",
872 "db/return_states.schema",
873 "db/type_size.schema",
874 "db/call_implies.schema",
875 "db/function_ptr.schema",
876 "db/return_values.schema",
877 "db/local_values.schema",
879 static char buf
[4096];
884 rc
= sqlite3_open(":memory:", &mem_db
);
885 if (rc
!= SQLITE_OK
) {
886 printf("Error starting In-Memory database.");
890 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
891 fd
= open_data_file(schema_files
[i
]);
894 ret
= read(fd
, buf
, sizeof(buf
));
895 if (ret
== sizeof(buf
)) {
896 printf("Schema file too large: %s (limit %zd bytes)",
897 schema_files
[i
], sizeof(buf
));
900 rc
= sqlite3_exec(mem_db
, buf
, NULL
, 0, &err
);
901 if (rc
!= SQLITE_OK
) {
902 fprintf(stderr
, "SQL error #2: %s\n", err
);
903 fprintf(stderr
, "%s\n", buf
);
908 void open_smatch_db(void)
917 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
918 if (rc
!= SQLITE_OK
) {
925 static void register_common_funcs(void)
931 if (option_project
== PROJ_NONE
)
932 strcpy(filename
, "common_functions");
934 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
936 token
= get_tokens_file(filename
);
939 if (token_type(token
) != TOKEN_STREAMBEGIN
)
942 while (token_type(token
) != TOKEN_STREAMEND
) {
943 if (token_type(token
) != TOKEN_IDENT
)
945 func
= alloc_string(show_ident(token
->ident
));
946 add_ptr_list(&common_funcs
, func
);
953 void register_definition_db_callbacks(int id
)
955 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
957 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
958 add_hook(&global_variable
, BASE_HOOK
);
959 add_hook(&global_variable
, DECLARATION_HOOK
);
960 add_returned_state_callback(match_return_info
);
961 add_returned_state_callback(print_returned_struct_members
);
962 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
963 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
965 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
966 add_hook(&match_call_implies
, CALL_HOOK_AFTER_INLINE
);
968 register_common_funcs();
971 void register_db_call_marker(int id
)
973 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
976 char *get_variable_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
)
981 if (strcmp(key
, "$$") == 0)
982 return expr_to_var_sym(arg
, sym
);
984 if (strcmp(key
, "*$$") == 0) {
985 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
986 arg
= strip_expr(arg
->unop
);
987 return expr_to_var_sym(arg
, sym
);
989 tmp
= expr_to_var_sym(arg
, sym
);
992 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
994 return alloc_string(buf
);
998 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
999 arg
= strip_expr(arg
->unop
);
1000 tmp
= expr_to_var_sym(arg
, sym
);
1003 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 4);
1004 return alloc_string(buf
);
1007 tmp
= expr_to_var_sym(arg
, sym
);
1010 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 2);
1012 return alloc_string(buf
);