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
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
26 struct sqlite3
*smatch_db
;
27 struct sqlite3
*mem_db
;
28 struct sqlite3
*cache_db
;
34 static void call_return_state_hooks(struct expression
*expr
);
36 #define SQLITE_CACHE_PAGES 1000
40 void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
);
42 ALLOCATOR(def_callback
, "definition db hook callbacks");
43 DECLARE_PTR_LIST(callback_list
, struct def_callback
);
44 static struct callback_list
*select_caller_info_callbacks
;
46 struct member_info_callback
{
48 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
);
50 ALLOCATOR(member_info_callback
, "caller_info callbacks");
51 DECLARE_PTR_LIST(member_info_cb_list
, struct member_info_callback
);
52 static struct member_info_cb_list
*member_callbacks
;
53 static struct member_info_cb_list
*member_callbacks_new
;
55 struct return_info_callback
{
57 void (*callback
)(int return_id
, char *return_ranges
,
58 struct expression
*returned_expr
,
60 const char *printed_name
,
63 ALLOCATOR(return_info_callback
, "return_info callbacks");
64 DECLARE_PTR_LIST(return_info_cb_list
, struct return_info_callback
);
65 static struct return_info_cb_list
*return_callbacks
;
67 struct returned_state_callback
{
68 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*return_expr
);
70 ALLOCATOR(returned_state_callback
, "returned state callbacks");
71 DECLARE_PTR_LIST(returned_state_cb_list
, struct returned_state_callback
);
72 static struct returned_state_cb_list
*returned_state_callbacks
;
74 struct returned_member_callback
{
76 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*expr
, char *printed_name
, struct smatch_state
*state
);
78 ALLOCATOR(returned_member_callback
, "returned member callbacks");
79 DECLARE_PTR_LIST(returned_member_cb_list
, struct returned_member_callback
);
80 static struct returned_member_cb_list
*returned_member_callbacks
;
82 struct db_implies_callback
{
84 void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
);
86 ALLOCATOR(db_implies_callback
, "return_implies callbacks");
87 DECLARE_PTR_LIST(db_implies_cb_list
, struct db_implies_callback
);
88 static struct db_implies_cb_list
*return_implies_cb_list
;
89 static struct db_implies_cb_list
*call_implies_cb_list
;
91 /* silently truncates if needed. */
92 char *escape_newlines(const char *str
)
98 for (i
= 0, j
= 0; str
[i
] != '\0' && j
!= sizeof(buf
); i
++, j
++) {
99 if (str
[i
] != '\r' && str
[i
] != '\n') {
106 if (j
== sizeof(buf
))
112 return alloc_sname(str
);
114 if (j
== sizeof(buf
))
116 return alloc_sname(buf
);
119 static int print_sql_output(void *unused
, int argc
, char **argv
, char **azColName
)
123 for (i
= 0; i
< argc
; i
++) {
126 sm_printf("%s", argv
[i
]);
132 void sql_exec(struct sqlite3
*db
, int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
)
140 if (option_debug
|| debug_db
) {
142 if (strncasecmp(sql
, "select", strlen("select")) == 0)
143 sqlite3_exec(db
, sql
, print_sql_output
, NULL
, NULL
);
146 rc
= sqlite3_exec(db
, sql
, callback
, data
, &err
);
147 if (rc
!= SQLITE_OK
&& !parse_error
) {
148 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err
);
149 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql
);
154 static int replace_count
;
155 static char **replace_table
;
156 static const char *replace_return_ranges(const char *return_ranges
)
160 if (!get_function()) {
161 /* I have no idea why EXPORT_SYMBOL() is here */
162 return return_ranges
;
164 for (i
= 0; i
< replace_count
; i
+= 3) {
165 if (strcmp(replace_table
[i
+ 0], get_function()) == 0) {
166 if (strcmp(replace_table
[i
+ 1], return_ranges
) == 0)
167 return replace_table
[i
+ 2];
170 return return_ranges
;
174 static char *use_states
;
175 static int get_db_state_count(void)
180 FOR_EACH_SM(__get_cur_stree(), sm
) {
181 if (sm
->owner
== USHRT_MAX
)
183 if (use_states
[sm
->owner
])
185 } END_FOR_EACH_SM(sm
);
189 void db_ignore_states(int id
)
194 unsigned long long __fn_mtag
;
195 static void set_fn_mtag(struct symbol
*sym
)
199 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
200 snprintf(buf
, sizeof(buf
), "%s %s", get_base_file(), get_function());
202 snprintf(buf
, sizeof(buf
), "extern %s", get_function());
204 __fn_mtag
= str_to_mtag(buf
);
207 void sql_insert_return_states(int return_id
, const char *return_ranges
,
208 int type
, int param
, const char *key
, const char *value
)
210 unsigned long long id
;
213 if (key
&& strlen(key
) >= 80)
216 id
= (unsigned long)__inline_fn
;
220 return_ranges
= replace_return_ranges(return_ranges
);
221 sql_insert(return_states
, "'%s', '%s', %llu, %d, '%s', %d, %d, %d, '%s', '%s'",
222 get_base_file(), get_function(), id
, return_id
,
223 return_ranges
, fn_static(), type
, param
, key
, value
);
226 static struct string_list
*common_funcs
;
227 static int is_common_function(const char *fn
)
234 if (strncmp(fn
, "__builtin_", 10) == 0)
237 FOR_EACH_PTR(common_funcs
, tmp
) {
238 if (strcmp(tmp
, fn
) == 0)
240 } END_FOR_EACH_PTR(tmp
);
245 static char *function_signature(void)
247 return type_to_str(get_real_base_type(cur_func_sym
));
250 void sql_insert_caller_info(struct expression
*call
, int type
,
251 int param
, const char *key
, const char *value
)
253 FILE *tmp_fd
= sm_outfd
;
256 if (!option_info
&& !__inline_call
)
261 if (key
&& strlen(key
) >= 80)
264 fn
= get_fnptr_name(call
->fn
);
270 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
271 get_base_file(), get_function(), fn
, (unsigned long)call
,
272 is_static(call
->fn
), type
, param
, key
, value
);
278 if (strncmp(fn
, "__builtin_", 10) == 0)
280 if (type
!= INTERNAL
&& is_common_function(fn
))
283 sm_outfd
= caller_info_fd
;
284 sm_msg("SQL_caller_info: insert into caller_info values ("
285 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
286 get_base_file(), get_function(), fn
, is_static(call
->fn
),
287 type
, param
, key
, value
);
293 void sql_insert_function_ptr(const char *fn
, const char *struct_name
)
295 sql_insert_or_ignore(function_ptr
, "'%s', '%s', '%s', 0",
296 get_base_file(), fn
, struct_name
);
299 void sql_insert_return_implies(int type
, int param
, const char *key
, const char *value
)
301 sql_insert_or_ignore(return_implies
, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
302 get_base_file(), get_function(), (unsigned long)__inline_fn
,
303 fn_static(), type
, param
, key
, value
);
306 void sql_insert_call_implies(int type
, int param
, const char *key
, const char *value
)
308 sql_insert_or_ignore(call_implies
, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
309 get_base_file(), get_function(), (unsigned long)__inline_fn
,
310 fn_static(), type
, param
, key
, value
);
313 void sql_insert_function_type_size(const char *member
, const char *ranges
)
315 sql_insert(function_type_size
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member
, ranges
);
318 void sql_insert_function_type_info(int type
, const char *struct_type
, const char *member
, const char *value
)
320 sql_insert(function_type_info
, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type
, struct_type
, member
, value
);
323 void sql_insert_type_info(int type
, const char *member
, const char *value
)
325 sql_insert_cache(type_info
, "'%s', %d, '%s', '%s'", get_base_file(), type
, member
, value
);
328 void sql_insert_local_values(const char *name
, const char *value
)
330 sql_insert(local_values
, "'%s', '%s', '%s'", get_base_file(), name
, value
);
333 void sql_insert_function_type_value(const char *type
, const char *value
)
335 sql_insert(function_type_value
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type
, value
);
338 void sql_insert_function_type(int param
, const char *value
)
340 sql_insert(function_type
, "'%s', '%s', %d, %d, '%s'",
341 get_base_file(), get_function(), fn_static(), param
, value
);
344 void sql_insert_parameter_name(int param
, const char *value
)
346 sql_insert(parameter_name
, "'%s', '%s', %d, %d, '%s'",
347 get_base_file(), get_function(), fn_static(), param
, value
);
350 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
)
354 data_name
= get_data_info_name(data
);
357 sql_insert(data_info
, "'%s', '%s', %d, '%s'",
358 is_static(data
) ? get_base_file() : "extern",
359 data_name
, type
, value
);
362 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
)
364 sql_insert(data_info
, "'%s', '%s', %d, '%s'",
365 (sym
->ctype
.modifiers
& MOD_STATIC
) ? get_base_file() : "extern",
369 void sql_save_constraint(const char *con
)
374 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con
));
377 void sql_save_constraint_required(const char *data
, int op
, const char *limit
)
379 sql_insert_or_ignore(constraints_required
, "'%s', '%s', '%s'", data
, show_special(op
), limit
);
382 void sql_copy_constraint_required(const char *new_limit
, const char *old_limit
)
387 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
388 "select constraints_required.data, constraints_required.op, '%s' from "
389 "constraints_required where bound = '%s';", new_limit
, old_limit
);
392 void sql_insert_fn_ptr_data_link(const char *ptr
, const char *data
)
394 sql_insert_or_ignore(fn_ptr_data_link
, "'%s', '%s'", ptr
, data
);
397 void sql_insert_fn_data_link(struct expression
*fn
, int type
, int param
, const char *key
, const char *value
)
399 if (fn
->type
!= EXPR_SYMBOL
|| !fn
->symbol
->ident
)
402 sql_insert(fn_data_link
, "'%s', '%s', %d, %d, %d, '%s', '%s'",
403 (fn
->symbol
->ctype
.modifiers
& MOD_STATIC
) ? get_base_file() : "extern",
404 fn
->symbol
->ident
->name
,
405 !!(fn
->symbol
->ctype
.modifiers
& MOD_STATIC
),
406 type
, param
, key
, value
);
409 void sql_insert_mtag_about(mtag_t tag
, const char *left_name
, const char *right_name
)
411 sql_insert_cache(mtag_about
, "%lld, '%s', '%s', %d, '%s', '%s'",
412 tag
, get_filename(), get_function(), get_lineno(),
413 left_name
, right_name
);
416 void sql_insert_mtag_info(mtag_t tag
, int type
, const char *value
)
418 sql_insert_cache(mtag_info
, "'%s', %lld, %d, '%s'", get_filename(), tag
, type
, value
);
421 void sql_insert_mtag_map(mtag_t container
, int container_offset
, mtag_t tag
, int tag_offset
)
423 sql_insert(mtag_map
, "%lld, %d, %lld, %d", container
, container_offset
, tag
, tag_offset
);
426 void sql_insert_mtag_alias(mtag_t orig
, mtag_t alias
)
428 sql_insert(mtag_alias
, "%lld, %lld", orig
, alias
);
431 static int save_mtag(void *_tag
, int argc
, char **argv
, char **azColName
)
433 mtag_t
*saved_tag
= _tag
;
436 new_tag
= strtoll(argv
[0], NULL
, 10);
439 *saved_tag
= new_tag
;
440 else if (*saved_tag
!= new_tag
)
446 int mtag_map_select_container(mtag_t tag
, int container_offset
, mtag_t
*container
)
450 run_sql(save_mtag
, &tmp
,
451 "select container from mtag_map where tag = %lld and container_offset = %d and tag_offset = 0;",
452 tag
, container_offset
);
454 if (tmp
== 0 || tmp
== -1ULL)
460 int mtag_map_select_tag(mtag_t container
, int offset
, mtag_t
*tag
)
464 run_sql(save_mtag
, &tmp
,
465 "select tag from mtag_map where container = %lld and container_offset = %d;",
468 if (tmp
== 0 || tmp
== -1ULL)
474 char *get_static_filter(struct symbol
*sym
)
476 static char sql_filter
[1024];
478 /* This can only happen on buggy code. Return invalid SQL. */
480 sql_filter
[0] = '\0';
484 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
485 snprintf(sql_filter
, sizeof(sql_filter
),
486 "file = '%s' and function = '%s' and static = '1'",
487 get_base_file(), sym
->ident
->name
);
489 snprintf(sql_filter
, sizeof(sql_filter
),
490 "function = '%s' and static = '0'", sym
->ident
->name
);
496 static int get_row_count(void *_row_count
, int argc
, char **argv
, char **azColName
)
498 int *row_count
= _row_count
;
503 *row_count
= atoi(argv
[0]);
507 static void mark_call_params_untracked(struct expression
*call
)
509 struct expression
*arg
;
512 FOR_EACH_PTR(call
->args
, arg
) {
513 mark_untracked(call
, i
++, "$", NULL
);
514 } END_FOR_EACH_PTR(arg
);
517 static void sql_select_return_states_pointer(const char *cols
,
518 struct expression
*call
, int (*callback
)(void*, int, char**, char**), void *info
)
521 int return_count
= 0;
523 ptr
= get_fnptr_name(call
->fn
);
527 run_sql(get_row_count
, &return_count
,
528 "select count(*) from return_states join function_ptr "
529 "where return_states.function == function_ptr.function and "
530 "ptr = '%s' and searchable = 1 and type = %d;", ptr
, INTERNAL
);
531 /* The magic number 100 is just from testing on the kernel. */
532 if (return_count
> 100) {
533 mark_call_params_untracked(call
);
537 run_sql(callback
, info
,
538 "select %s from return_states join function_ptr where "
539 "return_states.function == function_ptr.function and ptr = '%s' "
540 "and searchable = 1 "
541 "order by function_ptr.file, return_states.file, return_id, type;",
545 static int is_local_symbol(struct expression
*expr
)
547 if (expr
->type
!= EXPR_SYMBOL
)
549 if (expr
->symbol
->ctype
.modifiers
& (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
))
554 void sql_select_return_states(const char *cols
, struct expression
*call
,
555 int (*callback
)(void*, int, char**, char**), void *info
)
557 struct expression
*fn
;
560 if (is_fake_call(call
))
563 fn
= strip_expr(call
->fn
);
564 if (fn
->type
!= EXPR_SYMBOL
|| !fn
->symbol
|| is_local_symbol(fn
)) {
565 sql_select_return_states_pointer(cols
, call
, callback
, info
);
570 mem_sql(callback
, info
,
571 "select %s from return_states where call_id = '%lu' order by return_id, type;",
572 cols
, (unsigned long)call
);
576 run_sql(get_row_count
, &row_count
, "select count(*) from return_states where %s;",
577 get_static_filter(fn
->symbol
));
578 if (row_count
> 3000)
581 run_sql(callback
, info
, "select %s from return_states where %s order by file, return_id, type;",
582 cols
, get_static_filter(fn
->symbol
));
585 #define CALL_IMPLIES 0
586 #define RETURN_IMPLIES 1
588 struct implies_info
{
590 struct db_implies_cb_list
*cb_list
;
591 struct expression
*expr
;
595 void sql_select_implies(const char *cols
, struct implies_info
*info
,
596 int (*callback
)(void*, int, char**, char**))
598 if (info
->type
== RETURN_IMPLIES
&& inlinable(info
->expr
->fn
)) {
599 mem_sql(callback
, info
,
600 "select %s from return_implies where call_id = '%lu';",
601 cols
, (unsigned long)info
->expr
);
605 run_sql(callback
, info
, "select %s from %s_implies where %s;",
607 info
->type
== CALL_IMPLIES
? "call" : "return",
608 get_static_filter(info
->sym
));
611 struct select_caller_info_data
{
612 struct stree
*final_states
;
613 struct timeval start_time
;
619 static int caller_info_callback(void *_data
, int argc
, char **argv
, char **azColName
);
621 static void sql_select_caller_info(struct select_caller_info_data
*data
,
622 const char *cols
, struct symbol
*sym
)
625 mem_sql(caller_info_callback
, data
,
626 "select %s from caller_info where call_id = %lu;",
627 cols
, (unsigned long)__inline_fn
);
631 if (sym
->ident
->name
&& is_common_function(sym
->ident
->name
))
633 run_sql(caller_info_callback
, data
,
634 "select %s from common_caller_info where %s order by call_id;",
635 cols
, get_static_filter(sym
));
639 run_sql(caller_info_callback
, data
,
640 "select %s from caller_info where %s order by call_id;",
641 cols
, get_static_filter(sym
));
644 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
646 struct def_callback
*def_callback
= __alloc_def_callback(0);
648 def_callback
->hook_type
= type
;
649 def_callback
->callback
= callback
;
650 add_ptr_list(&select_caller_info_callbacks
, def_callback
);
654 * These call backs are used when the --info option is turned on to print struct
655 * member information. For example foo->bar could have a state in
656 * smatch_extra.c and also check_user.c.
658 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
660 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
662 member_callback
->owner
= owner
;
663 member_callback
->callback
= callback
;
664 add_ptr_list(&member_callbacks
, member_callback
);
667 void add_caller_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
669 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
671 member_callback
->owner
= owner
;
672 member_callback
->callback
= callback
;
673 add_ptr_list(&member_callbacks_new
, member_callback
);
676 void add_return_info_callback(int owner
,
677 void (*callback
)(int return_id
, char *return_ranges
,
678 struct expression
*returned_expr
,
680 const char *printed_name
,
681 struct sm_state
*sm
))
683 struct return_info_callback
*return_callback
= __alloc_return_info_callback(0);
685 return_callback
->owner
= owner
;
686 return_callback
->callback
= callback
;
687 add_ptr_list(&return_callbacks
, return_callback
);
690 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
692 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
694 callback
->callback
= fn
;
695 add_ptr_list(&returned_state_callbacks
, callback
);
698 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
))
700 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
702 member_callback
->owner
= owner
;
703 member_callback
->callback
= callback
;
704 add_ptr_list(&returned_member_callbacks
, member_callback
);
707 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
))
709 struct db_implies_callback
*cb
= __alloc_db_implies_callback(0);
712 cb
->callback
= callback
;
713 add_ptr_list(&call_implies_cb_list
, cb
);
716 void select_return_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
))
718 struct db_implies_callback
*cb
= __alloc_db_implies_callback(0);
721 cb
->callback
= callback
;
722 add_ptr_list(&return_implies_cb_list
, cb
);
726 struct expression
*static_returns_call
;
727 struct symbol
*return_type
;
728 struct range_list
*return_range_list
;
731 static int db_return_callback(void *_ret_info
, int argc
, char **argv
, char **azColName
)
733 struct return_info
*ret_info
= _ret_info
;
734 struct range_list
*rl
;
735 struct expression
*call_expr
= ret_info
->static_returns_call
;
739 call_results_to_rl(call_expr
, ret_info
->return_type
, argv
[0], &rl
);
740 ret_info
->return_range_list
= rl_union(ret_info
->return_range_list
, rl
);
744 struct range_list
*db_return_vals(struct expression
*expr
)
746 struct return_info ret_info
= {};
749 if (is_fake_call(expr
))
752 sm
= get_extra_sm_state(expr
);
754 return clone_rl(estate_rl(sm
->state
));
755 ret_info
.static_returns_call
= expr
;
756 ret_info
.return_type
= get_type(expr
);
757 if (!ret_info
.return_type
)
760 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
763 ret_info
.return_range_list
= NULL
;
764 if (inlinable(expr
->fn
)) {
765 mem_sql(db_return_callback
, &ret_info
,
766 "select distinct return from return_states where call_id = '%lu';",
767 (unsigned long)expr
);
769 run_sql(db_return_callback
, &ret_info
,
770 "select distinct return from return_states where %s;",
771 get_static_filter(expr
->fn
->symbol
));
773 return ret_info
.return_range_list
;
776 struct range_list
*db_return_vals_from_str(const char *fn_name
)
778 struct return_info ret_info
;
780 ret_info
.static_returns_call
= NULL
;
781 ret_info
.return_type
= &llong_ctype
;
782 ret_info
.return_range_list
= NULL
;
784 run_sql(db_return_callback
, &ret_info
,
785 "select distinct return from return_states where function = '%s';",
787 return ret_info
.return_range_list
;
791 * This is used when we have a function that takes a function pointer as a
792 * parameter. "frob(blah, blah, my_function);" We know that the return values
793 * from frob() come from my_funcion() so we want to find the possible returns
794 * of my_function(), but we don't know which arguments are passed to it.
797 struct range_list
*db_return_vals_no_args(struct expression
*expr
)
799 struct return_info ret_info
= {};
801 if (!expr
|| expr
->type
!= EXPR_SYMBOL
)
804 ret_info
.static_returns_call
= expr
;
805 ret_info
.return_type
= get_type(expr
);
806 ret_info
.return_type
= get_real_base_type(ret_info
.return_type
);
807 if (!ret_info
.return_type
)
810 run_sql(db_return_callback
, &ret_info
,
811 "select distinct return from return_states where %s;",
812 get_static_filter(expr
->symbol
));
814 return ret_info
.return_range_list
;
817 static void match_call_marker(struct expression
*expr
)
821 type
= get_type(expr
->fn
);
822 if (type
&& type
->type
== SYM_PTR
)
823 type
= get_real_base_type(type
);
826 * we just want to record something in the database so that if we have
827 * two calls like: frob(4); frob(some_unkown); then on the receiving
828 * side we know that sometimes frob is called with unknown parameters.
831 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", type_to_str(type
));
834 int is_recursive_member(const char *name
)
837 const char *p
, *next
;
840 p
= strchr(name
, '>');
845 next
= strchr(p
, '>');
851 if (size
>= sizeof(buf
))
853 memcpy(buf
, p
, size
);
855 if (strstr(next
, buf
))
861 char *sm_to_arg_name(struct expression
*expr
, struct sm_state
*sm
)
866 bool is_address
= false;
867 bool add_star
= false;
872 expr
= strip_expr(expr
);
876 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
877 expr
= strip_expr(expr
->unop
);
881 name
= expr_to_var_sym(expr
, &sym
);
889 if (sm_name
[0] == '*') {
895 if (strncmp(name
, sm_name
, len
) != 0)
897 if (sm_name
[len
] == '\0') {
898 snprintf(buf
, sizeof(buf
), "%s%s$",
899 add_star
? "*" : "", is_address
? "*" : "");
901 if (sm_name
[len
] != '.' && sm_name
[len
] != '-')
903 if (sm_name
[len
] == '-')
905 // FIXME does is_address really imply that sm_name[len] == '-'
906 snprintf(buf
, sizeof(buf
), "%s$->%s", add_star
? "*" : "",
910 ret
= alloc_sname(buf
);
916 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
,
918 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
),
926 char printed_name
[256];
931 expr
= strip_expr(expr
);
934 type
= get_type(expr
);
935 if (!new && type
&& type_bits(type
) < type_bits(&ulong_ctype
))
938 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
939 expr
= strip_expr(expr
->unop
);
943 name
= expr_to_var_sym(expr
, &sym
);
948 FOR_EACH_SM(__get_cur_stree(), sm
) {
949 if (sm
->owner
!= owner
|| sm
->sym
!= sym
)
953 if (sm_name
[0] == '*') {
958 if (!add_star
&& strcmp(name
, sm_name
) == 0) {
960 snprintf(printed_name
, sizeof(printed_name
), "*$");
963 snprintf(printed_name
, sizeof(printed_name
), "$");
967 } else if (add_star
&& strcmp(name
, sm_name
) == 0) {
968 snprintf(printed_name
, sizeof(printed_name
), "%s*$",
969 is_address
? "*" : "");
970 } else if (strncmp(name
, sm_name
, len
) == 0) {
971 if (sm_name
[len
] != '.' && sm_name
[len
] != '-')
973 if (is_address
&& sm_name
[len
] == '.') {
974 snprintf(printed_name
, sizeof(printed_name
),
975 "%s$->%s", add_star
? "*" : "",
977 } else if (is_address
&& sm_name
[len
] == '-') {
978 snprintf(printed_name
, sizeof(printed_name
),
979 "%s(*$)%s", add_star
? "*" : "",
982 snprintf(printed_name
, sizeof(printed_name
),
983 "%s$%s", add_star
? "*" : "",
989 if (is_recursive_member(printed_name
))
991 callback(call
, param
, printed_name
, sm
);
992 } END_FOR_EACH_SM(sm
);
997 static void match_call_info(struct expression
*call
)
999 struct member_info_callback
*cb
;
1000 struct expression
*arg
;
1003 FOR_EACH_PTR(member_callbacks
, cb
) {
1005 FOR_EACH_PTR(call
->args
, arg
) {
1007 print_struct_members(call
, arg
, i
, cb
->owner
, cb
->callback
, 0);
1008 } END_FOR_EACH_PTR(arg
);
1009 } END_FOR_EACH_PTR(cb
);
1012 static struct expression
*get_fake_variable(struct expression
*expr
)
1014 struct expression
*tmp
;
1016 tmp
= expr_get_fake_parent_expr(expr
);
1017 if (!tmp
|| tmp
->type
!= EXPR_ASSIGNMENT
)
1023 static void match_call_info_new(struct expression
*call
)
1025 struct member_info_callback
*cb
;
1026 struct expression
*arg
, *tmp
;
1029 FOR_EACH_PTR(member_callbacks_new
, cb
) {
1031 FOR_EACH_PTR(call
->args
, arg
) {
1033 tmp
= get_fake_variable(arg
);
1036 print_struct_members(call
, tmp
, i
, cb
->owner
, cb
->callback
, 1);
1037 } END_FOR_EACH_PTR(arg
);
1038 } END_FOR_EACH_PTR(cb
);
1041 static int get_param(int param
, char **name
, struct symbol
**sym
)
1047 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
1049 * this is a temporary hack to work around a bug (I think in sparse?)
1050 * 2.6.37-rc1:fs/reiserfs/journal.o
1051 * If there is a function definition without parameter name found
1052 * after a function implementation then it causes a crash.
1056 if (arg
->ident
->name
< (char *)100)
1059 *name
= arg
->ident
->name
;
1064 } END_FOR_EACH_PTR(arg
);
1069 static int function_signature_matches(const char *sig
)
1073 my_sig
= function_signature();
1074 if (!sig
|| !my_sig
)
1075 return 1; /* default to matching */
1076 if (strcmp(my_sig
, sig
) == 0)
1081 static int caller_info_callback(void *_data
, int argc
, char **argv
, char **azColName
)
1083 struct select_caller_info_data
*data
= _data
;
1090 struct symbol
*sym
= NULL
;
1091 struct def_callback
*def_callback
;
1092 struct stree
*stree
;
1093 struct timeval cur_time
;
1100 gettimeofday(&cur_time
, NULL
);
1101 if (cur_time
.tv_sec
- data
->start_time
.tv_sec
> 10)
1104 func_id
= atoi(argv
[0]);
1106 type
= strtol(argv
[1], NULL
, 10);
1107 param
= strtol(argv
[2], NULL
, 10);
1113 if (data
->prev_func_id
== -1)
1114 data
->prev_func_id
= func_id
;
1115 if (func_id
!= data
->prev_func_id
) {
1116 stree
= __pop_fake_cur_stree();
1118 merge_stree(&data
->final_states
, stree
);
1120 __push_fake_cur_stree();
1122 data
->prev_func_id
= func_id
;
1128 if (type
== INTERNAL
&&
1129 !function_signature_matches(value
)) {
1134 if (param
>= 0 && !get_param(param
, &name
, &sym
))
1137 FOR_EACH_PTR(select_caller_info_callbacks
, def_callback
) {
1138 if (def_callback
->hook_type
== type
)
1139 def_callback
->callback(name
, sym
, key
, value
);
1140 } END_FOR_EACH_PTR(def_callback
);
1145 static struct string_list
*ptr_names_done
;
1146 static struct string_list
*ptr_names
;
1148 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
1150 insert_string(&ptr_names
, alloc_string(argv
[0]));
1154 static char *get_next_ptr_name(void)
1158 FOR_EACH_PTR(ptr_names
, ptr
) {
1159 if (!insert_string(&ptr_names_done
, ptr
))
1162 } END_FOR_EACH_PTR(ptr
);
1166 static void get_ptr_names(const char *file
, const char *name
)
1168 char sql_filter
[1024];
1172 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
1175 snprintf(sql_filter
, 1024, "function = '%s';", name
);
1178 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
1180 run_sql(get_ptr_name
, NULL
,
1181 "select distinct ptr from function_ptr where %s",
1184 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
1185 if (before
== after
)
1188 while ((name
= get_next_ptr_name()))
1189 get_ptr_names(NULL
, name
);
1192 static void match_data_from_db(struct symbol
*sym
)
1194 struct select_caller_info_data data
= { .prev_func_id
= -1 };
1195 struct sm_state
*sm
;
1196 struct stree
*stree
;
1197 struct timeval end_time
;
1199 if (!sym
|| !sym
->ident
)
1203 gettimeofday(&data
.start_time
, NULL
);
1205 __push_fake_cur_stree();
1211 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1212 get_ptr_names(get_base_file(), sym
->ident
->name
);
1214 get_ptr_names(NULL
, sym
->ident
->name
);
1216 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
1217 __free_ptr_list((struct ptr_list
**)&ptr_names
);
1218 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
1219 __free_fake_cur_stree();
1223 sql_select_caller_info(&data
,
1224 "call_id, type, parameter, key, value",
1228 stree
= __pop_fake_cur_stree();
1230 merge_stree(&data
.final_states
, stree
);
1232 __push_fake_cur_stree();
1234 data
.prev_func_id
= -1;
1238 FOR_EACH_PTR(ptr_names
, ptr
) {
1239 run_sql(caller_info_callback
, &data
,
1240 "select call_id, type, parameter, key, value"
1241 " from common_caller_info where function = '%s' order by call_id",
1243 } END_FOR_EACH_PTR(ptr
);
1246 FOR_EACH_PTR(ptr_names
, ptr
) {
1248 } END_FOR_EACH_PTR(ptr
);
1249 goto free_ptr_names
;
1252 FOR_EACH_PTR(ptr_names
, ptr
) {
1253 run_sql(caller_info_callback
, &data
,
1254 "select call_id, type, parameter, key, value"
1255 " from caller_info where function = '%s' order by call_id",
1258 } END_FOR_EACH_PTR(ptr
);
1261 __free_ptr_list((struct ptr_list
**)&ptr_names
);
1262 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
1264 sql_select_caller_info(&data
,
1265 "call_id, type, parameter, key, value",
1269 stree
= __pop_fake_cur_stree();
1271 merge_stree(&data
.final_states
, stree
);
1274 gettimeofday(&end_time
, NULL
);
1275 if (end_time
.tv_sec
- data
.start_time
.tv_sec
<= 10) {
1276 FOR_EACH_SM(data
.final_states
, sm
) {
1278 } END_FOR_EACH_SM(sm
);
1281 free_stree(&data
.final_states
);
1284 static int return_implies_callbacks(void *_info
, int argc
, char **argv
, char **azColName
)
1286 struct implies_info
*info
= _info
;
1287 struct db_implies_callback
*cb
;
1288 struct expression
*arg
= NULL
;
1295 type
= atoi(argv
[1]);
1296 param
= atoi(argv
[2]);
1298 FOR_EACH_PTR(info
->cb_list
, cb
) {
1299 if (cb
->type
!= type
)
1302 arg
= get_argument_from_call_expr(info
->expr
->args
, param
);
1306 cb
->callback(info
->expr
, arg
, argv
[3], argv
[4]);
1307 } END_FOR_EACH_PTR(cb
);
1312 static int call_implies_callbacks(void *_info
, int argc
, char **argv
, char **azColName
)
1314 struct implies_info
*info
= _info
;
1315 struct db_implies_callback
*cb
;
1316 struct expression
*arg
;
1325 type
= atoi(argv
[1]);
1326 param
= atoi(argv
[2]);
1328 if (!get_param(param
, &name
, &sym
))
1330 arg
= symbol_expression(sym
);
1334 FOR_EACH_PTR(info
->cb_list
, cb
) {
1335 if (cb
->type
!= type
)
1337 cb
->callback(info
->expr
, arg
, argv
[3], argv
[4]);
1338 } END_FOR_EACH_PTR(cb
);
1343 static void match_return_implies(struct expression
*expr
)
1345 struct implies_info info
= {
1346 .type
= RETURN_IMPLIES
,
1347 .cb_list
= return_implies_cb_list
,
1350 if (expr
->fn
->type
!= EXPR_SYMBOL
||
1354 info
.sym
= expr
->fn
->symbol
;
1355 sql_select_implies("function, type, parameter, key, value", &info
,
1356 return_implies_callbacks
);
1359 static void match_call_implies(struct symbol
*sym
)
1361 struct implies_info info
= {
1362 .type
= CALL_IMPLIES
,
1363 .cb_list
= call_implies_cb_list
,
1366 if (!sym
|| !sym
->ident
)
1370 sql_select_implies("function, type, parameter, key, value", &info
,
1371 call_implies_callbacks
);
1374 static char *get_fn_param_str(struct expression
*expr
)
1376 struct expression
*tmp
;
1380 tmp
= get_assigned_expr(expr
);
1383 expr
= strip_expr(expr
);
1384 if (!expr
|| expr
->type
!= EXPR_CALL
)
1386 expr
= strip_expr(expr
->fn
);
1387 if (!expr
|| expr
->type
!= EXPR_SYMBOL
)
1389 param
= get_param_num(expr
);
1393 snprintf(buf
, sizeof(buf
), "[r $%d]", param
);
1394 return alloc_sname(buf
);
1397 static char *get_return_compare_is_param(struct expression
*expr
)
1404 param
= get_param_num(expr
);
1408 var
= expr_to_var(expr
);
1411 snprintf(buf
, sizeof(buf
), "%s orig", var
);
1412 comparison
= get_comparison_strings(var
, buf
);
1418 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(comparison
), param
);
1419 return alloc_sname(buf
);
1422 static char *get_return_compare_str(struct expression
*expr
)
1426 compare_str
= get_return_compare_is_param(expr
);
1430 compare_str
= expr_lte_to_param(expr
, -1);
1434 return expr_param_comparison(expr
, -1);
1437 static const char *get_return_ranges_str(struct expression
*expr
, struct range_list
**rl_p
)
1439 struct range_list
*rl
;
1440 char *return_ranges
;
1450 return alloc_sname("");
1452 if (get_implied_value(expr
, &sval
)) {
1453 sval
= sval_cast(cur_func_return_type(), sval
);
1454 *rl_p
= alloc_rl(sval
, sval
);
1455 return sval_to_str_or_err_ptr(sval
);
1458 fn_param_str
= get_fn_param_str(expr
);
1459 compare_str
= expr_equal_to_param(expr
, -1);
1460 math_str
= get_value_in_terms_of_parameter_math(expr
);
1462 if (get_implied_rl(expr
, &rl
) && !is_whole_rl(rl
)) {
1463 rl
= cast_rl(cur_func_return_type(), rl
);
1464 return_ranges
= show_rl(rl
);
1465 } else if (get_imaginary_absolute(expr
, &rl
)){
1466 rl
= cast_rl(cur_func_return_type(), rl
);
1467 return alloc_sname(show_rl(rl
));
1469 get_absolute_rl(expr
, &rl
);
1470 rl
= cast_rl(cur_func_return_type(), rl
);
1471 return_ranges
= show_rl(rl
);
1476 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, fn_param_str
);
1477 return alloc_sname(buf
);
1480 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1481 return alloc_sname(buf
);
1484 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1485 return alloc_sname(buf
);
1487 compare_str
= get_return_compare_str(expr
);
1489 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1490 return alloc_sname(buf
);
1493 return return_ranges
;
1496 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
1498 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", function_signature());
1501 static bool call_return_state_hooks_conditional(struct expression
*expr
)
1503 int final_pass_orig
= final_pass
;
1509 (expr
->type
!= EXPR_CONDITIONAL
&& expr
->type
!= EXPR_SELECT
))
1514 __push_fake_cur_stree();
1517 __split_whole_condition(expr
->conditional
);
1518 final_pass
= final_pass_orig
;
1520 call_return_state_hooks(expr
->cond_true
?: expr
->conditional
);
1522 __push_true_states();
1523 __use_false_states();
1525 call_return_state_hooks(expr
->cond_false
);
1527 __merge_true_states();
1528 __free_fake_cur_stree();
1534 static void call_return_state_hooks_compare(struct expression
*expr
)
1536 struct returned_state_callback
*cb
;
1537 char *return_ranges
;
1538 int final_pass_orig
= final_pass
;
1539 sval_t sval
= { .type
= &int_ctype
};
1542 if (!get_implied_value(expr
, &ret
))
1545 __push_fake_cur_stree();
1548 __split_whole_condition(expr
);
1549 final_pass
= final_pass_orig
;
1551 if (ret
.value
!= 0) {
1552 return_ranges
= alloc_sname("1");
1554 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1557 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1558 cb
->callback(return_id
, return_ranges
, expr
);
1559 } END_FOR_EACH_PTR(cb
);
1562 __push_true_states();
1563 __use_false_states();
1565 if (ret
.value
!= 1) {
1566 return_ranges
= alloc_sname("0");
1568 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1571 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1572 cb
->callback(return_id
, return_ranges
, expr
);
1573 } END_FOR_EACH_PTR(cb
);
1576 __merge_true_states();
1577 __free_fake_cur_stree();
1580 static int ptr_in_list(struct sm_state
*sm
, struct state_list
*slist
)
1582 struct sm_state
*tmp
;
1584 FOR_EACH_PTR(slist
, tmp
) {
1585 if (strcmp(tmp
->state
->name
, sm
->state
->name
) == 0)
1587 } END_FOR_EACH_PTR(tmp
);
1592 static int split_possible_helper(struct sm_state
*sm
, struct expression
*expr
)
1594 struct returned_state_callback
*cb
;
1595 struct range_list
*rl
;
1596 char *return_ranges
;
1597 struct sm_state
*tmp
;
1599 int nr_possible
, nr_states
;
1602 struct state_list
*already_handled
= NULL
;
1605 if (!sm
|| !sm
->merged
)
1608 if (too_many_possible(sm
))
1611 /* bail if it gets too complicated */
1613 FOR_EACH_PTR(sm
->possible
, tmp
) {
1616 if (ptr_in_list(tmp
, already_handled
))
1618 add_ptr_list(&already_handled
, tmp
);
1620 } END_FOR_EACH_PTR(tmp
);
1621 free_slist(&already_handled
);
1622 nr_states
= get_db_state_count();
1623 if (nr_states
* nr_possible
>= 2000)
1626 FOR_EACH_PTR(sm
->possible
, tmp
) {
1629 if (ptr_in_list(tmp
, already_handled
))
1631 add_ptr_list(&already_handled
, tmp
);
1634 __push_fake_cur_stree();
1636 overwrite_states_using_pool(sm
, tmp
);
1638 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
1639 return_ranges
= show_rl(rl
);
1640 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(clone_rl(rl
)));
1641 compare_str
= get_return_compare_str(expr
);
1642 /* ignore obvious stuff like 0 <= param */
1643 /* Is this worthile when we have PARAM_COMPARE? */
1645 strncmp(compare_str
, "[=", 2) != 0 &&
1646 rl_to_sval(rl
, &sval
))
1649 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1650 return_ranges
= alloc_sname(buf
);
1654 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1655 cb
->callback(return_id
, return_ranges
, expr
);
1656 } END_FOR_EACH_PTR(cb
);
1658 __free_fake_cur_stree();
1659 } END_FOR_EACH_PTR(tmp
);
1661 free_slist(&already_handled
);
1666 static int call_return_state_hooks_split_possible(struct expression
*expr
)
1668 struct expression
*fake
;
1669 struct sm_state
*sm
;
1674 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1676 fake
= expr_get_fake_parent_expr(expr
);
1677 if (!fake
|| fake
->type
!= EXPR_ASSIGNMENT
|| fake
->op
!= '=')
1680 sm
= get_sm_state_expr(SMATCH_EXTRA
, fake
);
1682 return split_possible_helper(sm
, expr
);
1685 static bool has_possible_negative(struct sm_state
*sm
)
1687 struct sm_state
*tmp
;
1689 if (!type_signed(estate_type(sm
->state
)))
1692 FOR_EACH_PTR(sm
->possible
, tmp
) {
1693 if (!estate_rl(tmp
->state
))
1695 if (sval_is_negative(estate_min(tmp
->state
)) &&
1696 sval_is_negative(estate_max(tmp
->state
)))
1698 } END_FOR_EACH_PTR(tmp
);
1703 static bool has_separate_zero_null(struct sm_state
*sm
)
1705 struct sm_state
*tmp
;
1708 FOR_EACH_PTR(sm
->possible
, tmp
) {
1709 if (!estate_get_single_value(tmp
->state
, &sval
))
1711 if (sval
.value
== 0)
1713 } END_FOR_EACH_PTR(tmp
);
1718 static int split_positive_from_negative(struct expression
*expr
)
1720 struct sm_state
*sm
;
1721 struct returned_state_callback
*cb
;
1722 struct range_list
*rl
;
1723 const char *return_ranges
;
1724 struct range_list
*ret_rl
;
1728 /* We're going to print the states 3 times */
1729 if (get_db_state_count() > 10000 / 3)
1732 if (!get_implied_rl(expr
, &rl
) || !rl
)
1734 /* Forget about INT_MAX and larger */
1735 if (rl_max(rl
).value
<= 0)
1737 if (!sval_is_negative(rl_min(rl
)))
1740 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1743 if (!has_possible_negative(sm
))
1745 separate_zero
= has_separate_zero_null(sm
);
1747 if (!assume(compare_expression(expr
, separate_zero
? '>' : SPECIAL_GTE
, zero_expr())))
1751 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1752 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1753 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1754 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1755 } END_FOR_EACH_PTR(cb
);
1759 if (separate_zero
) {
1760 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
1763 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1764 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1765 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1766 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1767 } END_FOR_EACH_PTR(cb
);
1773 undo
= assume(compare_expression(expr
, '<', zero_expr()));
1776 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1777 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1778 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1779 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1780 } END_FOR_EACH_PTR(cb
);
1788 static int call_return_state_hooks_split_null_non_null_zero(struct expression
*expr
)
1790 struct returned_state_callback
*cb
;
1791 struct range_list
*rl
;
1792 struct range_list
*nonnull_rl
;
1794 struct range_list
*null_rl
= NULL
;
1795 char *return_ranges
;
1796 struct sm_state
*sm
;
1797 struct smatch_state
*state
;
1799 int final_pass_orig
= final_pass
;
1801 if (!expr
|| expr_equal_to_param(expr
, -1))
1803 if (expr
->type
== EXPR_CALL
)
1806 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1809 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
1812 if (!estate_rl(state
))
1814 if (estate_min(state
).value
== 0 && estate_max(state
).value
== 0)
1816 if (has_possible_negative(sm
))
1818 if (!has_separate_zero_null(sm
))
1821 nr_states
= get_db_state_count();
1822 if (option_info
&& nr_states
>= 1500)
1825 rl
= estate_rl(state
);
1827 __push_fake_cur_stree();
1830 __split_whole_condition(expr
);
1831 final_pass
= final_pass_orig
;
1833 nonnull_rl
= rl_filter(rl
, rl_zero());
1834 return_ranges
= show_rl(nonnull_rl
);
1835 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonnull_rl
));
1838 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1839 cb
->callback(return_id
, return_ranges
, expr
);
1840 } END_FOR_EACH_PTR(cb
);
1842 __push_true_states();
1843 __use_false_states();
1845 return_ranges
= alloc_sname("0");
1846 null_sval
= sval_type_val(rl_type(rl
), 0);
1847 add_range(&null_rl
, null_sval
, null_sval
);
1848 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(null_rl
));
1850 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1851 cb
->callback(return_id
, return_ranges
, expr
);
1852 } END_FOR_EACH_PTR(cb
);
1854 __merge_true_states();
1855 __free_fake_cur_stree();
1860 static bool is_kernel_success_fail(struct sm_state
*sm
)
1862 struct sm_state
*tmp
;
1863 struct range_list
*rl
;
1864 bool has_zero
= false;
1865 bool has_neg
= false;
1867 if (!type_signed(estate_type(sm
->state
)))
1870 FOR_EACH_PTR(sm
->possible
, tmp
) {
1871 rl
= estate_rl(tmp
->state
);
1874 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 0) {
1879 if (rl_min(rl
).value
>= -4095 && rl_max(rl
).value
< 0)
1881 if (strcmp(tmp
->state
->name
, "s32min-(-1)") == 0)
1883 if (strcmp(tmp
->state
->name
, "s32min-(-1),1-s32max") == 0)
1886 } END_FOR_EACH_PTR(tmp
);
1888 return has_zero
&& has_neg
;
1891 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
1893 struct sm_state
*sm
;
1894 struct range_list
*rl
;
1895 struct range_list
*nonzero_rl
;
1897 struct range_list
*zero_rl
= NULL
;
1899 struct returned_state_callback
*cb
;
1900 char *return_ranges
;
1901 int final_pass_orig
= final_pass
;
1903 if (option_project
!= PROJ_KERNEL
)
1906 nr_states
= get_db_state_count();
1907 if (nr_states
> 2000)
1910 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1913 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
1915 if (!is_kernel_success_fail(sm
))
1918 rl
= estate_rl(sm
->state
);
1922 __push_fake_cur_stree();
1925 __split_whole_condition(expr
);
1926 final_pass
= final_pass_orig
;
1928 nonzero_rl
= rl_filter(rl
, rl_zero());
1929 nonzero_rl
= cast_rl(cur_func_return_type(), nonzero_rl
);
1930 return_ranges
= show_rl(nonzero_rl
);
1931 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonzero_rl
));
1934 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1935 cb
->callback(return_id
, return_ranges
, expr
);
1936 } END_FOR_EACH_PTR(cb
);
1938 __push_true_states();
1939 __use_false_states();
1941 return_ranges
= alloc_sname("0");
1942 zero_sval
= sval_type_val(rl_type(rl
), 0);
1943 add_range(&zero_rl
, zero_sval
, zero_sval
);
1944 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(zero_rl
));
1946 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1947 cb
->callback(return_id
, return_ranges
, expr
);
1948 } END_FOR_EACH_PTR(cb
);
1950 __merge_true_states();
1951 __free_fake_cur_stree();
1956 static int is_boolean(struct expression
*expr
)
1958 struct range_list
*rl
;
1960 if (!get_implied_rl(expr
, &rl
))
1962 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
1967 static int splitable_function_call(struct expression
*expr
)
1969 struct sm_state
*sm
;
1971 if (!expr
|| expr
->type
!= EXPR_CALL
)
1973 sm
= get_extra_sm_state(expr
);
1974 return split_possible_helper(sm
, expr
);
1977 static struct sm_state
*find_bool_param(void)
1979 struct stree
*start_states
;
1981 struct sm_state
*sm
, *tmp
;
1984 start_states
= get_start_states();
1986 FOR_EACH_PTR_REVERSE(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
1989 sm
= get_sm_state_stree(start_states
, SMATCH_EXTRA
, arg
->ident
->name
, arg
);
1992 if (rl_min(estate_rl(sm
->state
)).value
!= 0 ||
1993 rl_max(estate_rl(sm
->state
)).value
!= 1)
1996 } END_FOR_EACH_PTR_REVERSE(arg
);
2002 * Check if it's splitable. If not, then splitting it up is likely not
2003 * useful for the callers.
2005 FOR_EACH_PTR(sm
->possible
, tmp
) {
2008 if (!estate_get_single_value(tmp
->state
, &sval
))
2010 } END_FOR_EACH_PTR(tmp
);
2015 static int split_on_bool_sm(struct sm_state
*sm
, struct expression
*expr
)
2017 struct returned_state_callback
*cb
;
2018 struct range_list
*ret_rl
;
2019 const char *return_ranges
;
2020 struct sm_state
*tmp
;
2022 struct state_list
*already_handled
= NULL
;
2024 if (!sm
|| !sm
->merged
)
2027 if (too_many_possible(sm
))
2030 FOR_EACH_PTR(sm
->possible
, tmp
) {
2033 if (ptr_in_list(tmp
, already_handled
))
2035 add_ptr_list(&already_handled
, tmp
);
2038 __push_fake_cur_stree();
2040 overwrite_states_using_pool(sm
, tmp
);
2042 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
2043 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
2045 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
2046 cb
->callback(return_id
, (char *)return_ranges
, expr
);
2047 } END_FOR_EACH_PTR(cb
);
2049 __free_fake_cur_stree();
2050 } END_FOR_EACH_PTR(tmp
);
2052 free_slist(&already_handled
);
2057 static int split_by_bool_param(struct expression
*expr
)
2059 struct sm_state
*start_sm
, *sm
;
2062 start_sm
= find_bool_param();
2065 sm
= get_sm_state(SMATCH_EXTRA
, start_sm
->name
, start_sm
->sym
);
2066 if (!sm
|| estate_get_single_value(sm
->state
, &sval
))
2069 if (get_db_state_count() * 2 >= 2000)
2072 return split_on_bool_sm(sm
, expr
);
2075 static int split_by_null_nonnull_param(struct expression
*expr
)
2078 struct sm_state
*sm
;
2081 /* function must only take one pointer */
2082 if (ptr_list_size((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
) != 1)
2084 arg
= first_ptr_list((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
);
2087 if (get_real_base_type(arg
)->type
!= SYM_PTR
)
2090 if (param_was_set_var_sym(arg
->ident
->name
, arg
))
2092 sm
= get_sm_state(SMATCH_EXTRA
, arg
->ident
->name
, arg
);
2096 if (!has_separate_zero_null(sm
))
2099 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
2100 if (get_db_state_count() * nr_possible
>= 2000)
2103 return split_on_bool_sm(sm
, expr
);
2106 struct expression
*strip_expr_statement(struct expression
*expr
)
2108 struct expression
*orig
= expr
;
2109 struct statement
*stmt
, *last_stmt
;
2113 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '(')
2115 if (expr
->type
!= EXPR_STATEMENT
)
2117 stmt
= expr
->statement
;
2118 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
2121 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
2122 if (!last_stmt
|| last_stmt
->type
== STMT_LABEL
)
2123 last_stmt
= last_stmt
->label_statement
;
2124 if (!last_stmt
|| last_stmt
->type
!= STMT_EXPRESSION
)
2126 return strip_expr(last_stmt
->expression
);
2129 static bool is_kernel_error_path(struct expression
*expr
)
2131 struct range_list
*rl
;
2134 * Splitting up returns requires resources. It also requires resources
2135 * for the caller. It doesn't seem worth it to split anything up.
2137 if (!get_implied_rl(expr
, &rl
))
2139 if (rl_type(rl
) != &int_ctype
)
2141 if (rl_min(rl
).value
>= -4095 &&
2142 rl_max(rl
).value
< 0)
2147 static void call_return_state_hooks(struct expression
*expr
)
2149 struct returned_state_callback
*cb
;
2150 struct range_list
*ret_rl
;
2151 const char *return_ranges
;
2155 if (__path_is_null())
2158 expr
= strip_expr(expr
);
2159 expr
= strip_expr_statement(expr
);
2161 if (is_impossible_path())
2164 if (expr
&& (expr
->type
== EXPR_COMPARE
||
2165 !get_implied_value(expr
, &sval
)) &&
2166 (is_condition(expr
) || is_boolean(expr
))) {
2167 call_return_state_hooks_compare(expr
);
2169 } else if (call_return_state_hooks_conditional(expr
)) {
2171 } else if (is_kernel_error_path(expr
)) {
2173 } else if (call_return_state_hooks_split_possible(expr
)) {
2175 } else if (split_positive_from_negative(expr
)) {
2177 } else if (call_return_state_hooks_split_null_non_null_zero(expr
)) {
2179 } else if (call_return_state_hooks_split_success_fail(expr
)) {
2181 } else if (splitable_function_call(expr
)) {
2183 } else if (split_by_bool_param(expr
)) {
2184 } else if (split_by_null_nonnull_param(expr
)) {
2189 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
2190 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
2193 nr_states
= get_db_state_count();
2194 if (nr_states
>= 10000) {
2195 match_return_info(return_id
, (char *)return_ranges
, expr
);
2196 print_limited_param_set(return_id
, (char *)return_ranges
, expr
);
2197 mark_all_params_untracked(return_id
, (char *)return_ranges
, expr
);
2200 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
2201 cb
->callback(return_id
, (char *)return_ranges
, expr
);
2202 } END_FOR_EACH_PTR(cb
);
2205 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
2207 struct returned_member_callback
*cb
;
2208 struct sm_state
*sm
;
2209 struct symbol
*type
;
2211 char member_name
[256];
2214 type
= get_type(expr
);
2215 if (!type
|| type
->type
!= SYM_PTR
)
2217 name
= expr_to_var(expr
);
2222 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
2223 FOR_EACH_MY_SM(cb
->owner
, __get_cur_stree(), sm
) {
2224 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
2225 strcpy(member_name
, "*$");
2226 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
2229 if (strncmp(sm
->name
, name
, len
) != 0)
2231 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
2233 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
2234 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
2235 } END_FOR_EACH_SM(sm
);
2236 } END_FOR_EACH_PTR(cb
);
2241 static void print_return_struct_info(int return_id
, char *return_ranges
,
2242 struct expression
*expr
,
2244 struct return_info_callback
*cb
)
2246 struct sm_state
*sm
;
2247 const char *printed_name
;
2250 FOR_EACH_MY_SM(cb
->owner
, __get_cur_stree(), sm
) {
2251 if (sm
->sym
&& sm
->sym
== sym
) {
2254 param
= get_param_num_from_sym(sm
->sym
);
2259 printed_name
= get_param_name(sm
);
2263 cb
->callback(return_id
, return_ranges
, expr
, param
, printed_name
, sm
);
2264 } END_FOR_EACH_SM(sm
);
2267 static void print_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
2269 struct return_info_callback
*cb
;
2270 struct expression
*tmp
;
2273 tmp
= get_fake_variable(expr
);
2276 sym
= expr_to_sym(expr
);
2278 FOR_EACH_PTR(return_callbacks
, cb
) {
2279 print_return_struct_info(return_id
, return_ranges
, expr
, sym
, cb
);
2280 } END_FOR_EACH_PTR(cb
);
2283 static void reset_memdb(struct symbol
*sym
)
2285 mem_sql(NULL
, NULL
, "delete from caller_info;");
2286 mem_sql(NULL
, NULL
, "delete from return_states;");
2287 mem_sql(NULL
, NULL
, "delete from call_implies;");
2288 mem_sql(NULL
, NULL
, "delete from return_implies;");
2291 static void match_end_func_info(struct symbol
*sym
)
2293 if (__path_is_null())
2295 call_return_state_hooks(NULL
);
2298 static void match_after_func(struct symbol
*sym
)
2304 static void init_memdb(void)
2308 const char *schema_files
[] = {
2310 "db/caller_info.schema",
2311 "db/common_caller_info.schema",
2312 "db/return_states.schema",
2313 "db/function_type_size.schema",
2314 "db/type_size.schema",
2315 "db/function_type_info.schema",
2316 "db/type_info.schema",
2317 "db/call_implies.schema",
2318 "db/return_implies.schema",
2319 "db/function_ptr.schema",
2320 "db/local_values.schema",
2321 "db/function_type_value.schema",
2322 "db/type_value.schema",
2323 "db/function_type.schema",
2324 "db/data_info.schema",
2325 "db/parameter_name.schema",
2326 "db/constraints.schema",
2327 "db/constraints_required.schema",
2328 "db/fn_ptr_data_link.schema",
2329 "db/fn_data_link.schema",
2330 "db/mtag_about.schema",
2331 "db/mtag_info.schema",
2332 "db/mtag_map.schema",
2333 "db/mtag_data.schema",
2334 "db/mtag_alias.schema",
2336 static char buf
[4096];
2341 rc
= sqlite3_open(":memory:", &mem_db
);
2342 if (rc
!= SQLITE_OK
) {
2343 sm_ierror("starting In-Memory database.");
2347 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
2348 fd
= open_schema_file(schema_files
[i
]);
2351 ret
= read(fd
, buf
, sizeof(buf
));
2353 sm_ierror("failed to read: %s", schema_files
[i
]);
2357 if (ret
== sizeof(buf
)) {
2358 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2359 schema_files
[i
], sizeof(buf
));
2363 rc
= sqlite3_exec(mem_db
, buf
, NULL
, NULL
, &err
);
2364 if (rc
!= SQLITE_OK
) {
2365 sm_ierror("SQL error #2: %s", err
);
2366 sm_ierror("%s", buf
);
2371 static void init_cachedb(void)
2375 const char *schema_files
[] = {
2376 "db/call_implies.schema",
2377 "db/return_implies.schema",
2378 "db/type_info.schema",
2379 "db/mtag_about.schema",
2380 "db/mtag_data.schema",
2381 "db/mtag_info.schema",
2382 "db/sink_info.schema",
2384 static char buf
[4096];
2389 rc
= sqlite3_open(":memory:", &cache_db
);
2390 if (rc
!= SQLITE_OK
) {
2391 sm_ierror("starting In-Memory database.");
2395 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
2396 fd
= open_schema_file(schema_files
[i
]);
2399 ret
= read(fd
, buf
, sizeof(buf
));
2401 sm_ierror("failed to read: %s", schema_files
[i
]);
2405 if (ret
== sizeof(buf
)) {
2406 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2407 schema_files
[i
], sizeof(buf
));
2411 rc
= sqlite3_exec(cache_db
, buf
, NULL
, NULL
, &err
);
2412 if (rc
!= SQLITE_OK
) {
2413 sm_ierror("SQL error #2: %s", err
);
2414 sm_ierror("%s", buf
);
2419 static int save_cache_data(void *_table
, int argc
, char **argv
, char **azColName
)
2421 static char buf
[4096];
2424 char *table
= _table
;
2428 p
+= snprintf(p
, 4096 - (p
- buf
), "insert or ignore into %s values (", table
);
2429 for (i
= 0; i
< argc
; i
++) {
2431 p
+= snprintf(p
, 4096 - (p
- buf
), ", ");
2432 sqlite3_snprintf(sizeof(tmp
), tmp
, "%q", escape_newlines(argv
[i
]));
2433 p
+= snprintf(p
, 4096 - (p
- buf
), "'%s'", tmp
);
2436 p
+= snprintf(p
, 4096 - (p
- buf
), ");");
2440 sm_msg("SQL: %s", buf
);
2444 static void dump_cache(struct symbol_list
*sym_list
)
2446 const char *cache_tables
[] = {
2447 "type_info", "return_implies", "call_implies", "mtag_data",
2448 "mtag_info", "mtag_about", "sink_info",
2456 for (i
= 0; i
< ARRAY_SIZE(cache_tables
); i
++) {
2457 snprintf(buf
, sizeof(buf
), "select * from %s;", cache_tables
[i
]);
2458 cache_sql(&save_cache_data
, (char *)cache_tables
[i
], buf
);
2462 void open_smatch_db(char *db_file
)
2469 use_states
= malloc(num_checks
+ 1);
2470 memset(use_states
, 0xff, num_checks
+ 1);
2475 rc
= sqlite3_open_v2(db_file
, &smatch_db
, SQLITE_OPEN_READONLY
, NULL
);
2476 if (rc
!= SQLITE_OK
) {
2481 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES
);
2485 static void register_common_funcs(void)
2487 struct token
*token
;
2491 if (option_project
== PROJ_NONE
)
2492 strcpy(filename
, "common_functions");
2494 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
2496 token
= get_tokens_file(filename
);
2499 if (token_type(token
) != TOKEN_STREAMBEGIN
)
2501 token
= token
->next
;
2502 while (token_type(token
) != TOKEN_STREAMEND
) {
2503 if (token_type(token
) != TOKEN_IDENT
)
2505 func
= alloc_string(show_ident(token
->ident
));
2506 add_ptr_list(&common_funcs
, func
);
2507 token
= token
->next
;
2509 clear_token_alloc();
2512 static char *get_next_string(char **str
)
2514 static char string
[256];
2523 while (*p
!= '\0' && *p
!= '\n') {
2524 if (*p
== '\\' && *(p
+ 1) == ' ') {
2534 if (len
>= sizeof(string
)) {
2535 memcpy(string
, start
, sizeof(string
));
2536 string
[sizeof(string
) - 1] = '\0';
2537 sm_ierror("return_fix: '%s' too long", string
);
2541 memcpy(string
, start
, len
);
2543 for (i
= 0; i
< sizeof(string
) - 1; i
++) {
2544 if (string
[i
] == '\\' && string
[i
+ 1] == ' ') {
2545 for (j
= i
; string
[j
] != '\0'; j
++)
2546 string
[j
] = string
[j
+ 1];
2555 static void register_return_replacements(void)
2557 char *func
, *orig
, *new;
2563 snprintf(filename
, 256, "db/%s.return_fixes", option_project_str
);
2564 fd
= open_schema_file(filename
);
2567 ret
= read(fd
, buf
, sizeof(buf
));
2571 if (ret
== sizeof(buf
)) {
2572 sm_ierror("file too large: %s (limit %zd bytes)",
2573 filename
, sizeof(buf
));
2580 get_next_string(&p
);
2583 if (replace_count
== 0 || replace_count
% 3 != 0) {
2587 replace_table
= malloc(replace_count
* sizeof(char *));
2592 func
= alloc_string(get_next_string(&p
));
2593 orig
= alloc_string(get_next_string(&p
));
2594 new = alloc_string(get_next_string(&p
));
2596 replace_table
[i
++] = func
;
2597 replace_table
[i
++] = orig
;
2598 replace_table
[i
++] = new;
2602 void register_definition_db_callbacks(int id
)
2604 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2605 add_hook(&match_call_info_new
, FUNCTION_CALL_HOOK
);
2606 add_split_return_callback(match_return_info
);
2607 add_split_return_callback(print_returned_struct_members
);
2608 add_split_return_callback(print_return_info
);
2609 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
2610 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
2611 add_hook(&match_after_func
, AFTER_FUNC_HOOK
);
2613 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
2614 add_hook(&match_call_implies
, FUNC_DEF_HOOK
);
2615 add_hook(&match_return_implies
, CALL_HOOK_AFTER_INLINE
);
2617 register_common_funcs();
2618 register_return_replacements();
2620 add_hook(&dump_cache
, END_FILE_HOOK
);
2623 void register_db_call_marker(int id
)
2625 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
2628 char *return_state_to_var_sym(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
)
2630 struct expression
*arg
;
2632 char member_name
[256];
2637 const char *star
= "";
2639 if (expr
->type
!= EXPR_ASSIGNMENT
)
2641 if (get_type(expr
->left
) == &int_ctype
&& strcmp(key
, "$") != 0)
2643 name
= expr_to_var_sym(expr
->left
, sym
);
2646 if (key
[0] == '*') {
2650 if (strncmp(key
, "$", 1) != 0)
2652 snprintf(member_name
, sizeof(member_name
), "%s%s%s", star
, name
, key
+ 1);
2654 return alloc_string(member_name
);
2657 while (expr
->type
== EXPR_ASSIGNMENT
)
2658 expr
= strip_expr(expr
->right
);
2659 if (expr
->type
!= EXPR_CALL
)
2662 arg
= get_argument_from_call_expr(expr
->args
, param
);
2666 return get_variable_from_key(arg
, key
, sym
);
2669 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
)
2671 struct symbol
*type
;
2675 bool add_dot
= false;
2680 arg
= strip_expr(arg
);
2682 if (strcmp(key
, "$") == 0)
2683 return expr_to_var_sym(arg
, sym
);
2685 if (strcmp(key
, "*$") == 0) {
2686 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
2687 arg
= strip_expr(arg
->unop
);
2688 return expr_to_var_sym(arg
, sym
);
2690 tmp
= expr_to_var_sym(arg
, sym
);
2693 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
2695 return alloc_string(buf
);
2699 if (strncmp(key
, "(*$)", 4) == 0) {
2702 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
2703 arg
= strip_expr(arg
->unop
);
2704 snprintf(buf
, sizeof(buf
), "$%s", key
+ 4);
2705 return get_variable_from_key(arg
, buf
, sym
);
2707 tmp
= expr_to_var_sym(arg
, sym
);
2710 snprintf(buf
, sizeof(buf
), "(*%s)%s", tmp
, key
+ 4);
2712 return alloc_string(buf
);
2716 while (key
[0] == '*') {
2722 * FIXME: This is a hack.
2723 * We should be able to parse expressions like (*$)->foo and *$->foo.
2725 type
= get_type(arg
);
2726 if (is_struct_ptr(type
))
2729 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&' && star_cnt
&& !add_dot
) {
2730 arg
= strip_expr(arg
->unop
);
2734 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
2735 arg
= strip_expr(arg
->unop
);
2736 tmp
= expr_to_var_sym(arg
, sym
);
2739 snprintf(buf
, sizeof(buf
), "%.*s%s.%s",
2740 star_cnt
, "**********", tmp
, key
+ 3);
2741 return alloc_string(buf
);
2744 tmp
= expr_to_var_sym(arg
, sym
);
2747 snprintf(buf
, sizeof(buf
), "%.*s%s%s", star_cnt
, "**********", tmp
, key
+ 1);
2749 return alloc_string(buf
);
2752 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
)
2756 if (strcmp("$", key
) == 0)
2757 return expr_to_chunk_sym_vsl(arg
, sym
, vsl
);
2758 return get_variable_from_key(arg
, key
, sym
);
2761 const char *state_name_to_param_name(const char *state_name
, const char *param_name
)
2767 name_len
= strlen(param_name
);
2769 while (state_name
[0] == '*') {
2774 /* ten out of ten stars! */
2778 if (strncmp(state_name
, "(*", 2) == 0 &&
2779 strncmp(state_name
+ 2, param_name
, name_len
) == 0 &&
2780 state_name
[name_len
+ 2] == ')') {
2781 snprintf(buf
, sizeof(buf
), "%.*s(*$)%s", star_cnt
, "**********",
2782 state_name
+ name_len
+ 3);
2783 return alloc_sname(buf
);
2786 if (strcmp(state_name
, param_name
) == 0) {
2787 snprintf(buf
, sizeof(buf
), "%.*s$", star_cnt
, "**********");
2788 return alloc_sname(buf
);
2791 /* check for '-' from "->" */
2792 if (strncmp(state_name
, param_name
, name_len
) == 0 &&
2793 state_name
[name_len
] == '-') {
2794 snprintf(buf
, sizeof(buf
), "%.*s$%s", star_cnt
, "**********", state_name
+ name_len
);
2795 return alloc_sname(buf
);
2800 const char *get_param_name_var_sym(const char *name
, struct symbol
*sym
)
2802 if (!sym
|| !sym
->ident
)
2805 return state_name_to_param_name(name
, sym
->ident
->name
);
2808 const char *get_mtag_name_var_sym(const char *state_name
, struct symbol
*sym
)
2810 struct symbol
*type
;
2811 const char *sym_name
;
2813 static char buf
[256];
2816 * mtag_name is different from param_name because mtags can be a struct
2817 * instead of a struct pointer. But we want to treat it like a pointer
2818 * because really an mtag is a pointer. Or in other words, if you pass
2819 * a struct foo then you want to talk about foo.bar but with an mtag
2820 * you want to refer to it as foo->bar.
2824 if (!sym
|| !sym
->ident
)
2827 type
= get_real_base_type(sym
);
2828 if (type
&& type
->type
== SYM_BASETYPE
)
2831 sym_name
= sym
->ident
->name
;
2832 name_len
= strlen(sym_name
);
2834 if (state_name
[name_len
] == '.' && /* check for '-' from "->" */
2835 strncmp(state_name
, sym_name
, name_len
) == 0) {
2836 snprintf(buf
, sizeof(buf
), "$->%s", state_name
+ name_len
+ 1);
2840 return state_name_to_param_name(state_name
, sym_name
);
2843 const char *get_mtag_name_expr(struct expression
*expr
)
2847 const char *ret
= NULL
;
2849 name
= expr_to_var_sym(expr
, &sym
);
2853 ret
= get_mtag_name_var_sym(name
, sym
);
2859 const char *get_param_name(struct sm_state
*sm
)
2861 return get_param_name_var_sym(sm
->name
, sm
->sym
);
2864 char *get_data_info_name(struct expression
*expr
)
2871 expr
= strip_expr(expr
);
2872 name
= get_member_name(expr
);
2875 name
= expr_to_var_sym(expr
, &sym
);
2878 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
2880 if (sym
->ctype
.modifiers
& MOD_STATIC
)
2881 snprintf(buf
, sizeof(buf
), "static %s", name
);
2883 snprintf(buf
, sizeof(buf
), "global %s", name
);
2884 ret
= alloc_sname(buf
);