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
)
259 if (key
&& strlen(key
) >= 80)
262 fn
= get_fnptr_name(call
->fn
);
268 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
269 get_base_file(), get_function(), fn
, (unsigned long)call
,
270 is_static(call
->fn
), type
, param
, key
, value
);
276 if (strncmp(fn
, "__builtin_", 10) == 0)
278 if (type
!= INTERNAL
&& is_common_function(fn
))
281 sm_outfd
= caller_info_fd
;
282 sm_msg("SQL_caller_info: insert into caller_info values ("
283 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
284 get_base_file(), get_function(), fn
, is_static(call
->fn
),
285 type
, param
, key
, value
);
291 void sql_insert_function_ptr(const char *fn
, const char *struct_name
)
293 sql_insert_or_ignore(function_ptr
, "'%s', '%s', '%s', 0",
294 get_base_file(), fn
, struct_name
);
297 void sql_insert_return_implies(int type
, int param
, const char *key
, const char *value
)
299 sql_insert_or_ignore(return_implies
, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
300 get_base_file(), get_function(), (unsigned long)__inline_fn
,
301 fn_static(), type
, param
, key
, value
);
304 void sql_insert_call_implies(int type
, int param
, const char *key
, const char *value
)
306 sql_insert_or_ignore(call_implies
, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
307 get_base_file(), get_function(), (unsigned long)__inline_fn
,
308 fn_static(), type
, param
, key
, value
);
311 void sql_insert_function_type_size(const char *member
, const char *ranges
)
313 sql_insert(function_type_size
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member
, ranges
);
316 void sql_insert_function_type_info(int type
, const char *struct_type
, const char *member
, const char *value
)
318 sql_insert(function_type_info
, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type
, struct_type
, member
, value
);
321 void sql_insert_type_info(int type
, const char *member
, const char *value
)
323 sql_insert_cache(type_info
, "'%s', %d, '%s', '%s'", get_base_file(), type
, member
, value
);
326 void sql_insert_local_values(const char *name
, const char *value
)
328 sql_insert(local_values
, "'%s', '%s', '%s'", get_base_file(), name
, value
);
331 void sql_insert_function_type_value(const char *type
, const char *value
)
333 sql_insert(function_type_value
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type
, value
);
336 void sql_insert_function_type(int param
, const char *value
)
338 sql_insert(function_type
, "'%s', '%s', %d, %d, '%s'",
339 get_base_file(), get_function(), fn_static(), param
, value
);
342 void sql_insert_parameter_name(int param
, const char *value
)
344 sql_insert(parameter_name
, "'%s', '%s', %d, %d, '%s'",
345 get_base_file(), get_function(), fn_static(), param
, value
);
348 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
)
352 data_name
= get_data_info_name(data
);
355 sql_insert(data_info
, "'%s', '%s', %d, '%s'",
356 is_static(data
) ? get_base_file() : "extern",
357 data_name
, type
, value
);
360 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
)
362 sql_insert(data_info
, "'%s', '%s', %d, '%s'",
363 (sym
->ctype
.modifiers
& MOD_STATIC
) ? get_base_file() : "extern",
367 void sql_save_constraint(const char *con
)
372 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con
));
375 void sql_save_constraint_required(const char *data
, int op
, const char *limit
)
377 sql_insert_or_ignore(constraints_required
, "'%s', '%s', '%s'", data
, show_special(op
), limit
);
380 void sql_copy_constraint_required(const char *new_limit
, const char *old_limit
)
385 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
386 "select constraints_required.data, constraints_required.op, '%s' from "
387 "constraints_required where bound = '%s';", new_limit
, old_limit
);
390 void sql_insert_fn_ptr_data_link(const char *ptr
, const char *data
)
392 sql_insert_or_ignore(fn_ptr_data_link
, "'%s', '%s'", ptr
, data
);
395 void sql_insert_fn_data_link(struct expression
*fn
, int type
, int param
, const char *key
, const char *value
)
397 if (fn
->type
!= EXPR_SYMBOL
|| !fn
->symbol
->ident
)
400 sql_insert(fn_data_link
, "'%s', '%s', %d, %d, %d, '%s', '%s'",
401 (fn
->symbol
->ctype
.modifiers
& MOD_STATIC
) ? get_base_file() : "extern",
402 fn
->symbol
->ident
->name
,
403 !!(fn
->symbol
->ctype
.modifiers
& MOD_STATIC
),
404 type
, param
, key
, value
);
407 void sql_insert_mtag_about(mtag_t tag
, const char *left_name
, const char *right_name
)
409 sql_insert_cache(mtag_about
, "%lld, '%s', '%s', %d, '%s', '%s'",
410 tag
, get_filename(), get_function(), get_lineno(),
411 left_name
, right_name
);
414 void sql_insert_mtag_info(mtag_t tag
, int type
, const char *value
)
416 sql_insert_cache(mtag_info
, "'%s', %lld, %d, '%s'", get_filename(), tag
, type
, value
);
419 void sql_insert_mtag_map(mtag_t container
, int container_offset
, mtag_t tag
, int tag_offset
)
421 sql_insert(mtag_map
, "%lld, %d, %lld, %d", container
, container_offset
, tag
, tag_offset
);
424 void sql_insert_mtag_alias(mtag_t orig
, mtag_t alias
)
426 sql_insert(mtag_alias
, "%lld, %lld", orig
, alias
);
429 static int save_mtag(void *_tag
, int argc
, char **argv
, char **azColName
)
431 mtag_t
*saved_tag
= _tag
;
434 new_tag
= strtoll(argv
[0], NULL
, 10);
437 *saved_tag
= new_tag
;
438 else if (*saved_tag
!= new_tag
)
444 int mtag_map_select_container(mtag_t tag
, int container_offset
, mtag_t
*container
)
448 run_sql(save_mtag
, &tmp
,
449 "select container from mtag_map where tag = %lld and container_offset = %d and tag_offset = 0;",
450 tag
, container_offset
);
452 if (tmp
== 0 || tmp
== -1ULL)
458 int mtag_map_select_tag(mtag_t container
, int offset
, mtag_t
*tag
)
462 run_sql(save_mtag
, &tmp
,
463 "select tag from mtag_map where container = %lld and container_offset = %d;",
466 if (tmp
== 0 || tmp
== -1ULL)
472 char *get_static_filter(struct symbol
*sym
)
474 static char sql_filter
[1024];
476 /* This can only happen on buggy code. Return invalid SQL. */
478 sql_filter
[0] = '\0';
482 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
483 snprintf(sql_filter
, sizeof(sql_filter
),
484 "file = '%s' and function = '%s' and static = '1'",
485 get_base_file(), sym
->ident
->name
);
487 snprintf(sql_filter
, sizeof(sql_filter
),
488 "function = '%s' and static = '0'", sym
->ident
->name
);
494 static int get_row_count(void *_row_count
, int argc
, char **argv
, char **azColName
)
496 int *row_count
= _row_count
;
501 *row_count
= atoi(argv
[0]);
505 static void mark_call_params_untracked(struct expression
*call
)
507 struct expression
*arg
;
510 FOR_EACH_PTR(call
->args
, arg
) {
511 mark_untracked(call
, i
++, "$", NULL
);
512 } END_FOR_EACH_PTR(arg
);
515 static void sql_select_return_states_pointer(const char *cols
,
516 struct expression
*call
, int (*callback
)(void*, int, char**, char**), void *info
)
519 int return_count
= 0;
521 ptr
= get_fnptr_name(call
->fn
);
525 run_sql(get_row_count
, &return_count
,
526 "select count(*) from return_states join function_ptr "
527 "where return_states.function == function_ptr.function and "
528 "ptr = '%s' and searchable = 1 and type = %d;", ptr
, INTERNAL
);
529 /* The magic number 100 is just from testing on the kernel. */
530 if (return_count
> 100) {
531 mark_call_params_untracked(call
);
535 run_sql(callback
, info
,
536 "select %s from return_states join function_ptr where "
537 "return_states.function == function_ptr.function and ptr = '%s' "
538 "and searchable = 1 "
539 "order by function_ptr.file, return_states.file, return_id, type;",
543 static int is_local_symbol(struct expression
*expr
)
545 if (expr
->type
!= EXPR_SYMBOL
)
547 if (expr
->symbol
->ctype
.modifiers
& (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
))
552 void sql_select_return_states(const char *cols
, struct expression
*call
,
553 int (*callback
)(void*, int, char**, char**), void *info
)
555 struct expression
*fn
;
558 if (is_fake_call(call
))
561 fn
= strip_expr(call
->fn
);
562 if (fn
->type
!= EXPR_SYMBOL
|| !fn
->symbol
|| is_local_symbol(fn
)) {
563 sql_select_return_states_pointer(cols
, call
, callback
, info
);
568 mem_sql(callback
, info
,
569 "select %s from return_states where call_id = '%lu' order by return_id, type;",
570 cols
, (unsigned long)call
);
574 run_sql(get_row_count
, &row_count
, "select count(*) from return_states where %s;",
575 get_static_filter(fn
->symbol
));
576 if (row_count
> 3000)
579 run_sql(callback
, info
, "select %s from return_states where %s order by file, return_id, type;",
580 cols
, get_static_filter(fn
->symbol
));
583 #define CALL_IMPLIES 0
584 #define RETURN_IMPLIES 1
586 struct implies_info
{
588 struct db_implies_cb_list
*cb_list
;
589 struct expression
*expr
;
593 void sql_select_implies(const char *cols
, struct implies_info
*info
,
594 int (*callback
)(void*, int, char**, char**))
596 if (info
->type
== RETURN_IMPLIES
&& inlinable(info
->expr
->fn
)) {
597 mem_sql(callback
, info
,
598 "select %s from return_implies where call_id = '%lu';",
599 cols
, (unsigned long)info
->expr
);
603 run_sql(callback
, info
, "select %s from %s_implies where %s;",
605 info
->type
== CALL_IMPLIES
? "call" : "return",
606 get_static_filter(info
->sym
));
609 struct select_caller_info_data
{
610 struct stree
*final_states
;
611 struct timeval start_time
;
617 static int caller_info_callback(void *_data
, int argc
, char **argv
, char **azColName
);
619 static void sql_select_caller_info(struct select_caller_info_data
*data
,
620 const char *cols
, struct symbol
*sym
)
623 mem_sql(caller_info_callback
, data
,
624 "select %s from caller_info where call_id = %lu;",
625 cols
, (unsigned long)__inline_fn
);
629 if (sym
->ident
->name
&& is_common_function(sym
->ident
->name
))
631 run_sql(caller_info_callback
, data
,
632 "select %s from common_caller_info where %s order by call_id;",
633 cols
, get_static_filter(sym
));
637 run_sql(caller_info_callback
, data
,
638 "select %s from caller_info where %s order by call_id;",
639 cols
, get_static_filter(sym
));
642 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
644 struct def_callback
*def_callback
= __alloc_def_callback(0);
646 def_callback
->hook_type
= type
;
647 def_callback
->callback
= callback
;
648 add_ptr_list(&select_caller_info_callbacks
, def_callback
);
652 * These call backs are used when the --info option is turned on to print struct
653 * member information. For example foo->bar could have a state in
654 * smatch_extra.c and also check_user.c.
656 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
658 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
660 member_callback
->owner
= owner
;
661 member_callback
->callback
= callback
;
662 add_ptr_list(&member_callbacks
, member_callback
);
665 void add_caller_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
667 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
669 member_callback
->owner
= owner
;
670 member_callback
->callback
= callback
;
671 add_ptr_list(&member_callbacks_new
, member_callback
);
674 void add_return_info_callback(int owner
,
675 void (*callback
)(int return_id
, char *return_ranges
,
676 struct expression
*returned_expr
,
678 const char *printed_name
,
679 struct sm_state
*sm
))
681 struct return_info_callback
*return_callback
= __alloc_return_info_callback(0);
683 return_callback
->owner
= owner
;
684 return_callback
->callback
= callback
;
685 add_ptr_list(&return_callbacks
, return_callback
);
688 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
690 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
692 callback
->callback
= fn
;
693 add_ptr_list(&returned_state_callbacks
, callback
);
696 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
))
698 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
700 member_callback
->owner
= owner
;
701 member_callback
->callback
= callback
;
702 add_ptr_list(&returned_member_callbacks
, member_callback
);
705 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
))
707 struct db_implies_callback
*cb
= __alloc_db_implies_callback(0);
710 cb
->callback
= callback
;
711 add_ptr_list(&call_implies_cb_list
, cb
);
714 void select_return_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
))
716 struct db_implies_callback
*cb
= __alloc_db_implies_callback(0);
719 cb
->callback
= callback
;
720 add_ptr_list(&return_implies_cb_list
, cb
);
724 struct expression
*static_returns_call
;
725 struct symbol
*return_type
;
726 struct range_list
*return_range_list
;
729 static int db_return_callback(void *_ret_info
, int argc
, char **argv
, char **azColName
)
731 struct return_info
*ret_info
= _ret_info
;
732 struct range_list
*rl
;
733 struct expression
*call_expr
= ret_info
->static_returns_call
;
737 call_results_to_rl(call_expr
, ret_info
->return_type
, argv
[0], &rl
);
738 ret_info
->return_range_list
= rl_union(ret_info
->return_range_list
, rl
);
742 struct range_list
*db_return_vals(struct expression
*expr
)
744 struct return_info ret_info
= {};
747 if (is_fake_call(expr
))
750 sm
= get_extra_sm_state(expr
);
752 return clone_rl(estate_rl(sm
->state
));
753 ret_info
.static_returns_call
= expr
;
754 ret_info
.return_type
= get_type(expr
);
755 if (!ret_info
.return_type
)
758 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
761 ret_info
.return_range_list
= NULL
;
762 if (inlinable(expr
->fn
)) {
763 mem_sql(db_return_callback
, &ret_info
,
764 "select distinct return from return_states where call_id = '%lu';",
765 (unsigned long)expr
);
767 run_sql(db_return_callback
, &ret_info
,
768 "select distinct return from return_states where %s;",
769 get_static_filter(expr
->fn
->symbol
));
771 return ret_info
.return_range_list
;
774 struct range_list
*db_return_vals_from_str(const char *fn_name
)
776 struct return_info ret_info
;
778 ret_info
.static_returns_call
= NULL
;
779 ret_info
.return_type
= &llong_ctype
;
780 ret_info
.return_range_list
= NULL
;
782 run_sql(db_return_callback
, &ret_info
,
783 "select distinct return from return_states where function = '%s';",
785 return ret_info
.return_range_list
;
789 * This is used when we have a function that takes a function pointer as a
790 * parameter. "frob(blah, blah, my_function);" We know that the return values
791 * from frob() come from my_funcion() so we want to find the possible returns
792 * of my_function(), but we don't know which arguments are passed to it.
795 struct range_list
*db_return_vals_no_args(struct expression
*expr
)
797 struct return_info ret_info
= {};
799 if (!expr
|| expr
->type
!= EXPR_SYMBOL
)
802 ret_info
.static_returns_call
= expr
;
803 ret_info
.return_type
= get_type(expr
);
804 ret_info
.return_type
= get_real_base_type(ret_info
.return_type
);
805 if (!ret_info
.return_type
)
808 run_sql(db_return_callback
, &ret_info
,
809 "select distinct return from return_states where %s;",
810 get_static_filter(expr
->symbol
));
812 return ret_info
.return_range_list
;
815 static void match_call_marker(struct expression
*expr
)
819 type
= get_type(expr
->fn
);
820 if (type
&& type
->type
== SYM_PTR
)
821 type
= get_real_base_type(type
);
824 * we just want to record something in the database so that if we have
825 * two calls like: frob(4); frob(some_unkown); then on the receiving
826 * side we know that sometimes frob is called with unknown parameters.
829 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", type_to_str(type
));
832 int is_recursive_member(const char *name
)
835 const char *p
, *next
;
838 p
= strchr(name
, '>');
843 next
= strchr(p
, '>');
849 if (size
>= sizeof(buf
))
851 memcpy(buf
, p
, size
);
853 if (strstr(next
, buf
))
859 char *sm_to_arg_name(struct expression
*expr
, struct sm_state
*sm
)
864 bool is_address
= false;
865 bool add_star
= false;
870 expr
= strip_expr(expr
);
874 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
875 expr
= strip_expr(expr
->unop
);
879 name
= expr_to_var_sym(expr
, &sym
);
887 if (sm_name
[0] == '*') {
893 if (strncmp(name
, sm_name
, len
) != 0)
895 if (sm_name
[len
] == '\0') {
896 snprintf(buf
, sizeof(buf
), "%s%s$",
897 add_star
? "*" : "", is_address
? "*" : "");
899 if (sm_name
[len
] != '.' && sm_name
[len
] != '-')
901 if (sm_name
[len
] == '-')
903 // FIXME does is_address really imply that sm_name[len] == '-'
904 snprintf(buf
, sizeof(buf
), "%s$->%s", add_star
? "*" : "",
908 ret
= alloc_sname(buf
);
914 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
,
916 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
),
924 char printed_name
[256];
929 expr
= strip_expr(expr
);
932 type
= get_type(expr
);
933 if (!new && type
&& type_bits(type
) < type_bits(&ulong_ctype
))
936 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
937 expr
= strip_expr(expr
->unop
);
941 name
= expr_to_var_sym(expr
, &sym
);
946 FOR_EACH_SM(__get_cur_stree(), sm
) {
947 if (sm
->owner
!= owner
|| sm
->sym
!= sym
)
951 if (sm_name
[0] == '*') {
956 if (!add_star
&& strcmp(name
, sm_name
) == 0) {
958 snprintf(printed_name
, sizeof(printed_name
), "*$");
961 snprintf(printed_name
, sizeof(printed_name
), "$");
965 } else if (add_star
&& strcmp(name
, sm_name
) == 0) {
966 snprintf(printed_name
, sizeof(printed_name
), "%s*$",
967 is_address
? "*" : "");
968 } else if (strncmp(name
, sm_name
, len
) == 0) {
969 if (sm_name
[len
] != '.' && sm_name
[len
] != '-')
971 if (is_address
&& sm_name
[len
] == '.') {
972 snprintf(printed_name
, sizeof(printed_name
),
973 "%s$->%s", add_star
? "*" : "",
975 } else if (is_address
&& sm_name
[len
] == '-') {
976 snprintf(printed_name
, sizeof(printed_name
),
977 "%s(*$)%s", add_star
? "*" : "",
980 snprintf(printed_name
, sizeof(printed_name
),
981 "%s$%s", add_star
? "*" : "",
987 if (is_recursive_member(printed_name
))
989 callback(call
, param
, printed_name
, sm
);
990 } END_FOR_EACH_SM(sm
);
995 static void match_call_info(struct expression
*call
)
997 struct member_info_callback
*cb
;
998 struct expression
*arg
;
1001 FOR_EACH_PTR(member_callbacks
, cb
) {
1003 FOR_EACH_PTR(call
->args
, arg
) {
1005 print_struct_members(call
, arg
, i
, cb
->owner
, cb
->callback
, 0);
1006 } END_FOR_EACH_PTR(arg
);
1007 } END_FOR_EACH_PTR(cb
);
1010 static struct expression
*get_fake_variable(struct expression
*expr
)
1012 struct expression
*tmp
;
1014 tmp
= expr_get_fake_parent_expr(expr
);
1015 if (!tmp
|| tmp
->type
!= EXPR_ASSIGNMENT
)
1021 static void match_call_info_new(struct expression
*call
)
1023 struct member_info_callback
*cb
;
1024 struct expression
*arg
, *tmp
;
1027 FOR_EACH_PTR(member_callbacks_new
, cb
) {
1029 FOR_EACH_PTR(call
->args
, arg
) {
1031 tmp
= get_fake_variable(arg
);
1034 print_struct_members(call
, tmp
, i
, cb
->owner
, cb
->callback
, 1);
1035 } END_FOR_EACH_PTR(arg
);
1036 } END_FOR_EACH_PTR(cb
);
1039 static int get_param(int param
, char **name
, struct symbol
**sym
)
1045 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
1047 * this is a temporary hack to work around a bug (I think in sparse?)
1048 * 2.6.37-rc1:fs/reiserfs/journal.o
1049 * If there is a function definition without parameter name found
1050 * after a function implementation then it causes a crash.
1054 if (arg
->ident
->name
< (char *)100)
1057 *name
= arg
->ident
->name
;
1062 } END_FOR_EACH_PTR(arg
);
1067 static int function_signature_matches(const char *sig
)
1071 my_sig
= function_signature();
1072 if (!sig
|| !my_sig
)
1073 return 1; /* default to matching */
1074 if (strcmp(my_sig
, sig
) == 0)
1079 static int caller_info_callback(void *_data
, int argc
, char **argv
, char **azColName
)
1081 struct select_caller_info_data
*data
= _data
;
1088 struct symbol
*sym
= NULL
;
1089 struct def_callback
*def_callback
;
1090 struct stree
*stree
;
1091 struct timeval cur_time
;
1098 gettimeofday(&cur_time
, NULL
);
1099 if (cur_time
.tv_sec
- data
->start_time
.tv_sec
> 10)
1102 func_id
= atoi(argv
[0]);
1104 type
= strtol(argv
[1], NULL
, 10);
1105 param
= strtol(argv
[2], NULL
, 10);
1111 if (data
->prev_func_id
== -1)
1112 data
->prev_func_id
= func_id
;
1113 if (func_id
!= data
->prev_func_id
) {
1114 stree
= __pop_fake_cur_stree();
1116 merge_stree(&data
->final_states
, stree
);
1118 __push_fake_cur_stree();
1120 data
->prev_func_id
= func_id
;
1126 if (type
== INTERNAL
&&
1127 !function_signature_matches(value
)) {
1132 if (param
>= 0 && !get_param(param
, &name
, &sym
))
1135 FOR_EACH_PTR(select_caller_info_callbacks
, def_callback
) {
1136 if (def_callback
->hook_type
== type
)
1137 def_callback
->callback(name
, sym
, key
, value
);
1138 } END_FOR_EACH_PTR(def_callback
);
1143 static struct string_list
*ptr_names_done
;
1144 static struct string_list
*ptr_names
;
1146 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
1148 insert_string(&ptr_names
, alloc_string(argv
[0]));
1152 static char *get_next_ptr_name(void)
1156 FOR_EACH_PTR(ptr_names
, ptr
) {
1157 if (!insert_string(&ptr_names_done
, ptr
))
1160 } END_FOR_EACH_PTR(ptr
);
1164 static void get_ptr_names(const char *file
, const char *name
)
1166 char sql_filter
[1024];
1170 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
1173 snprintf(sql_filter
, 1024, "function = '%s';", name
);
1176 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
1178 run_sql(get_ptr_name
, NULL
,
1179 "select distinct ptr from function_ptr where %s",
1182 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
1183 if (before
== after
)
1186 while ((name
= get_next_ptr_name()))
1187 get_ptr_names(NULL
, name
);
1190 static void match_data_from_db(struct symbol
*sym
)
1192 struct select_caller_info_data data
= { .prev_func_id
= -1 };
1193 struct sm_state
*sm
;
1194 struct stree
*stree
;
1195 struct timeval end_time
;
1197 if (!sym
|| !sym
->ident
)
1201 gettimeofday(&data
.start_time
, NULL
);
1203 __push_fake_cur_stree();
1209 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1210 get_ptr_names(get_base_file(), sym
->ident
->name
);
1212 get_ptr_names(NULL
, sym
->ident
->name
);
1214 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
1215 __free_ptr_list((struct ptr_list
**)&ptr_names
);
1216 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
1217 __free_fake_cur_stree();
1221 sql_select_caller_info(&data
,
1222 "call_id, type, parameter, key, value",
1226 stree
= __pop_fake_cur_stree();
1228 merge_stree(&data
.final_states
, stree
);
1230 __push_fake_cur_stree();
1232 data
.prev_func_id
= -1;
1236 FOR_EACH_PTR(ptr_names
, ptr
) {
1237 run_sql(caller_info_callback
, &data
,
1238 "select call_id, type, parameter, key, value"
1239 " from common_caller_info where function = '%s' order by call_id",
1241 } END_FOR_EACH_PTR(ptr
);
1244 FOR_EACH_PTR(ptr_names
, ptr
) {
1246 } END_FOR_EACH_PTR(ptr
);
1247 goto free_ptr_names
;
1250 FOR_EACH_PTR(ptr_names
, ptr
) {
1251 run_sql(caller_info_callback
, &data
,
1252 "select call_id, type, parameter, key, value"
1253 " from caller_info where function = '%s' order by call_id",
1256 } END_FOR_EACH_PTR(ptr
);
1259 __free_ptr_list((struct ptr_list
**)&ptr_names
);
1260 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
1262 sql_select_caller_info(&data
,
1263 "call_id, type, parameter, key, value",
1267 stree
= __pop_fake_cur_stree();
1269 merge_stree(&data
.final_states
, stree
);
1272 gettimeofday(&end_time
, NULL
);
1273 if (end_time
.tv_sec
- data
.start_time
.tv_sec
<= 10) {
1274 FOR_EACH_SM(data
.final_states
, sm
) {
1276 } END_FOR_EACH_SM(sm
);
1279 free_stree(&data
.final_states
);
1282 static int return_implies_callbacks(void *_info
, int argc
, char **argv
, char **azColName
)
1284 struct implies_info
*info
= _info
;
1285 struct db_implies_callback
*cb
;
1286 struct expression
*arg
= NULL
;
1293 type
= atoi(argv
[1]);
1294 param
= atoi(argv
[2]);
1296 FOR_EACH_PTR(info
->cb_list
, cb
) {
1297 if (cb
->type
!= type
)
1300 arg
= get_argument_from_call_expr(info
->expr
->args
, param
);
1304 cb
->callback(info
->expr
, arg
, argv
[3], argv
[4]);
1305 } END_FOR_EACH_PTR(cb
);
1310 static int call_implies_callbacks(void *_info
, int argc
, char **argv
, char **azColName
)
1312 struct implies_info
*info
= _info
;
1313 struct db_implies_callback
*cb
;
1314 struct expression
*arg
;
1323 type
= atoi(argv
[1]);
1324 param
= atoi(argv
[2]);
1326 if (!get_param(param
, &name
, &sym
))
1328 arg
= symbol_expression(sym
);
1332 FOR_EACH_PTR(info
->cb_list
, cb
) {
1333 if (cb
->type
!= type
)
1335 cb
->callback(info
->expr
, arg
, argv
[3], argv
[4]);
1336 } END_FOR_EACH_PTR(cb
);
1341 static void match_return_implies(struct expression
*expr
)
1343 struct implies_info info
= {
1344 .type
= RETURN_IMPLIES
,
1345 .cb_list
= return_implies_cb_list
,
1348 if (expr
->fn
->type
!= EXPR_SYMBOL
||
1352 info
.sym
= expr
->fn
->symbol
;
1353 sql_select_implies("function, type, parameter, key, value", &info
,
1354 return_implies_callbacks
);
1357 static void match_call_implies(struct symbol
*sym
)
1359 struct implies_info info
= {
1360 .type
= CALL_IMPLIES
,
1361 .cb_list
= call_implies_cb_list
,
1364 if (!sym
|| !sym
->ident
)
1368 sql_select_implies("function, type, parameter, key, value", &info
,
1369 call_implies_callbacks
);
1372 static char *get_fn_param_str(struct expression
*expr
)
1374 struct expression
*tmp
;
1378 tmp
= get_assigned_expr(expr
);
1381 expr
= strip_expr(expr
);
1382 if (!expr
|| expr
->type
!= EXPR_CALL
)
1384 expr
= strip_expr(expr
->fn
);
1385 if (!expr
|| expr
->type
!= EXPR_SYMBOL
)
1387 param
= get_param_num(expr
);
1391 snprintf(buf
, sizeof(buf
), "[r $%d]", param
);
1392 return alloc_sname(buf
);
1395 static char *get_return_compare_is_param(struct expression
*expr
)
1402 param
= get_param_num(expr
);
1406 var
= expr_to_var(expr
);
1409 snprintf(buf
, sizeof(buf
), "%s orig", var
);
1410 comparison
= get_comparison_strings(var
, buf
);
1416 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(comparison
), param
);
1417 return alloc_sname(buf
);
1420 static char *get_return_compare_str(struct expression
*expr
)
1424 compare_str
= get_return_compare_is_param(expr
);
1428 compare_str
= expr_lte_to_param(expr
, -1);
1432 return expr_param_comparison(expr
, -1);
1435 static const char *get_return_ranges_str(struct expression
*expr
, struct range_list
**rl_p
)
1437 struct range_list
*rl
;
1438 char *return_ranges
;
1448 return alloc_sname("");
1450 if (get_implied_value(expr
, &sval
)) {
1451 sval
= sval_cast(cur_func_return_type(), sval
);
1452 *rl_p
= alloc_rl(sval
, sval
);
1453 return sval_to_str_or_err_ptr(sval
);
1456 fn_param_str
= get_fn_param_str(expr
);
1457 compare_str
= expr_equal_to_param(expr
, -1);
1458 math_str
= get_value_in_terms_of_parameter_math(expr
);
1460 if (get_implied_rl(expr
, &rl
) && !is_whole_rl(rl
)) {
1461 rl
= cast_rl(cur_func_return_type(), rl
);
1462 return_ranges
= show_rl(rl
);
1463 } else if (get_imaginary_absolute(expr
, &rl
)){
1464 rl
= cast_rl(cur_func_return_type(), rl
);
1465 return alloc_sname(show_rl(rl
));
1467 get_absolute_rl(expr
, &rl
);
1468 rl
= cast_rl(cur_func_return_type(), rl
);
1469 return_ranges
= show_rl(rl
);
1474 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, fn_param_str
);
1475 return alloc_sname(buf
);
1478 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1479 return alloc_sname(buf
);
1482 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1483 return alloc_sname(buf
);
1485 compare_str
= get_return_compare_str(expr
);
1487 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1488 return alloc_sname(buf
);
1491 return return_ranges
;
1494 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
1496 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", function_signature());
1499 static bool call_return_state_hooks_conditional(struct expression
*expr
)
1501 int final_pass_orig
= final_pass
;
1507 (expr
->type
!= EXPR_CONDITIONAL
&& expr
->type
!= EXPR_SELECT
))
1512 __push_fake_cur_stree();
1515 __split_whole_condition(expr
->conditional
);
1516 final_pass
= final_pass_orig
;
1518 call_return_state_hooks(expr
->cond_true
?: expr
->conditional
);
1520 __push_true_states();
1521 __use_false_states();
1523 call_return_state_hooks(expr
->cond_false
);
1525 __merge_true_states();
1526 __free_fake_cur_stree();
1532 static void call_return_state_hooks_compare(struct expression
*expr
)
1534 struct returned_state_callback
*cb
;
1535 char *return_ranges
;
1536 int final_pass_orig
= final_pass
;
1537 sval_t sval
= { .type
= &int_ctype
};
1540 if (!get_implied_value(expr
, &ret
))
1543 __push_fake_cur_stree();
1546 __split_whole_condition(expr
);
1547 final_pass
= final_pass_orig
;
1549 if (ret
.value
!= 0) {
1550 return_ranges
= alloc_sname("1");
1552 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1555 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1556 cb
->callback(return_id
, return_ranges
, expr
);
1557 } END_FOR_EACH_PTR(cb
);
1560 __push_true_states();
1561 __use_false_states();
1563 if (ret
.value
!= 1) {
1564 return_ranges
= alloc_sname("0");
1566 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1569 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1570 cb
->callback(return_id
, return_ranges
, expr
);
1571 } END_FOR_EACH_PTR(cb
);
1574 __merge_true_states();
1575 __free_fake_cur_stree();
1578 static int ptr_in_list(struct sm_state
*sm
, struct state_list
*slist
)
1580 struct sm_state
*tmp
;
1582 FOR_EACH_PTR(slist
, tmp
) {
1583 if (strcmp(tmp
->state
->name
, sm
->state
->name
) == 0)
1585 } END_FOR_EACH_PTR(tmp
);
1590 static int split_possible_helper(struct sm_state
*sm
, struct expression
*expr
)
1592 struct returned_state_callback
*cb
;
1593 struct range_list
*rl
;
1594 char *return_ranges
;
1595 struct sm_state
*tmp
;
1597 int nr_possible
, nr_states
;
1600 struct state_list
*already_handled
= NULL
;
1603 if (!sm
|| !sm
->merged
)
1606 if (too_many_possible(sm
))
1609 /* bail if it gets too complicated */
1611 FOR_EACH_PTR(sm
->possible
, tmp
) {
1614 if (ptr_in_list(tmp
, already_handled
))
1616 add_ptr_list(&already_handled
, tmp
);
1618 } END_FOR_EACH_PTR(tmp
);
1619 free_slist(&already_handled
);
1620 nr_states
= get_db_state_count();
1621 if (nr_states
* nr_possible
>= 2000)
1624 FOR_EACH_PTR(sm
->possible
, tmp
) {
1627 if (ptr_in_list(tmp
, already_handled
))
1629 add_ptr_list(&already_handled
, tmp
);
1632 __push_fake_cur_stree();
1634 overwrite_states_using_pool(sm
, tmp
);
1636 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
1637 return_ranges
= show_rl(rl
);
1638 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(clone_rl(rl
)));
1639 compare_str
= get_return_compare_str(expr
);
1640 /* ignore obvious stuff like 0 <= param */
1641 /* Is this worthile when we have PARAM_COMPARE? */
1643 strncmp(compare_str
, "[=", 2) != 0 &&
1644 rl_to_sval(rl
, &sval
))
1647 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1648 return_ranges
= alloc_sname(buf
);
1652 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1653 cb
->callback(return_id
, return_ranges
, expr
);
1654 } END_FOR_EACH_PTR(cb
);
1656 __free_fake_cur_stree();
1657 } END_FOR_EACH_PTR(tmp
);
1659 free_slist(&already_handled
);
1664 static int call_return_state_hooks_split_possible(struct expression
*expr
)
1666 struct expression
*fake
;
1667 struct sm_state
*sm
;
1672 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1674 fake
= expr_get_fake_parent_expr(expr
);
1675 if (!fake
|| fake
->type
!= EXPR_ASSIGNMENT
|| fake
->op
!= '=')
1678 sm
= get_sm_state_expr(SMATCH_EXTRA
, fake
);
1680 return split_possible_helper(sm
, expr
);
1683 static bool has_possible_negative(struct sm_state
*sm
)
1685 struct sm_state
*tmp
;
1687 if (!type_signed(estate_type(sm
->state
)))
1690 FOR_EACH_PTR(sm
->possible
, tmp
) {
1691 if (!estate_rl(tmp
->state
))
1693 if (sval_is_negative(estate_min(tmp
->state
)) &&
1694 sval_is_negative(estate_max(tmp
->state
)))
1696 } END_FOR_EACH_PTR(tmp
);
1701 static bool has_separate_zero_null(struct sm_state
*sm
)
1703 struct sm_state
*tmp
;
1706 FOR_EACH_PTR(sm
->possible
, tmp
) {
1707 if (!estate_get_single_value(tmp
->state
, &sval
))
1709 if (sval
.value
== 0)
1711 } END_FOR_EACH_PTR(tmp
);
1716 static int split_positive_from_negative(struct expression
*expr
)
1718 struct sm_state
*sm
;
1719 struct returned_state_callback
*cb
;
1720 struct range_list
*rl
;
1721 const char *return_ranges
;
1722 struct range_list
*ret_rl
;
1726 /* We're going to print the states 3 times */
1727 if (get_db_state_count() > 10000 / 3)
1730 if (!get_implied_rl(expr
, &rl
) || !rl
)
1732 /* Forget about INT_MAX and larger */
1733 if (rl_max(rl
).value
<= 0)
1735 if (!sval_is_negative(rl_min(rl
)))
1738 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1741 if (!has_possible_negative(sm
))
1743 separate_zero
= has_separate_zero_null(sm
);
1745 if (!assume(compare_expression(expr
, separate_zero
? '>' : SPECIAL_GTE
, zero_expr())))
1749 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1750 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1751 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1752 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1753 } END_FOR_EACH_PTR(cb
);
1757 if (separate_zero
) {
1758 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
1761 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1762 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1763 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1764 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1765 } END_FOR_EACH_PTR(cb
);
1771 undo
= assume(compare_expression(expr
, '<', zero_expr()));
1774 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1775 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1776 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1777 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1778 } END_FOR_EACH_PTR(cb
);
1786 static int call_return_state_hooks_split_null_non_null_zero(struct expression
*expr
)
1788 struct returned_state_callback
*cb
;
1789 struct range_list
*rl
;
1790 struct range_list
*nonnull_rl
;
1792 struct range_list
*null_rl
= NULL
;
1793 char *return_ranges
;
1794 struct sm_state
*sm
;
1795 struct smatch_state
*state
;
1797 int final_pass_orig
= final_pass
;
1799 if (!expr
|| expr_equal_to_param(expr
, -1))
1801 if (expr
->type
== EXPR_CALL
)
1804 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1807 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
1810 if (!estate_rl(state
))
1812 if (estate_min(state
).value
== 0 && estate_max(state
).value
== 0)
1814 if (has_possible_negative(sm
))
1816 if (!has_separate_zero_null(sm
))
1819 nr_states
= get_db_state_count();
1820 if (option_info
&& nr_states
>= 1500)
1823 rl
= estate_rl(state
);
1825 __push_fake_cur_stree();
1828 __split_whole_condition(expr
);
1829 final_pass
= final_pass_orig
;
1831 nonnull_rl
= rl_filter(rl
, rl_zero());
1832 return_ranges
= show_rl(nonnull_rl
);
1833 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonnull_rl
));
1836 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1837 cb
->callback(return_id
, return_ranges
, expr
);
1838 } END_FOR_EACH_PTR(cb
);
1840 __push_true_states();
1841 __use_false_states();
1843 return_ranges
= alloc_sname("0");
1844 null_sval
= sval_type_val(rl_type(rl
), 0);
1845 add_range(&null_rl
, null_sval
, null_sval
);
1846 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(null_rl
));
1848 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1849 cb
->callback(return_id
, return_ranges
, expr
);
1850 } END_FOR_EACH_PTR(cb
);
1852 __merge_true_states();
1853 __free_fake_cur_stree();
1858 static bool is_kernel_success_fail(struct sm_state
*sm
)
1860 struct sm_state
*tmp
;
1861 struct range_list
*rl
;
1862 bool has_zero
= false;
1863 bool has_neg
= false;
1865 if (!type_signed(estate_type(sm
->state
)))
1868 FOR_EACH_PTR(sm
->possible
, tmp
) {
1869 rl
= estate_rl(tmp
->state
);
1872 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 0) {
1877 if (rl_min(rl
).value
>= -4095 && rl_max(rl
).value
< 0)
1879 if (strcmp(tmp
->state
->name
, "s32min-(-1)") == 0)
1881 if (strcmp(tmp
->state
->name
, "s32min-(-1),1-s32max") == 0)
1884 } END_FOR_EACH_PTR(tmp
);
1886 return has_zero
&& has_neg
;
1889 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
1891 struct sm_state
*sm
;
1892 struct range_list
*rl
;
1893 struct range_list
*nonzero_rl
;
1895 struct range_list
*zero_rl
= NULL
;
1897 struct returned_state_callback
*cb
;
1898 char *return_ranges
;
1899 int final_pass_orig
= final_pass
;
1901 if (option_project
!= PROJ_KERNEL
)
1904 nr_states
= get_db_state_count();
1905 if (nr_states
> 2000)
1908 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1911 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
1913 if (!is_kernel_success_fail(sm
))
1916 rl
= estate_rl(sm
->state
);
1920 __push_fake_cur_stree();
1923 __split_whole_condition(expr
);
1924 final_pass
= final_pass_orig
;
1926 nonzero_rl
= rl_filter(rl
, rl_zero());
1927 nonzero_rl
= cast_rl(cur_func_return_type(), nonzero_rl
);
1928 return_ranges
= show_rl(nonzero_rl
);
1929 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonzero_rl
));
1932 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1933 cb
->callback(return_id
, return_ranges
, expr
);
1934 } END_FOR_EACH_PTR(cb
);
1936 __push_true_states();
1937 __use_false_states();
1939 return_ranges
= alloc_sname("0");
1940 zero_sval
= sval_type_val(rl_type(rl
), 0);
1941 add_range(&zero_rl
, zero_sval
, zero_sval
);
1942 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(zero_rl
));
1944 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1945 cb
->callback(return_id
, return_ranges
, expr
);
1946 } END_FOR_EACH_PTR(cb
);
1948 __merge_true_states();
1949 __free_fake_cur_stree();
1954 static int is_boolean(struct expression
*expr
)
1956 struct range_list
*rl
;
1958 if (!get_implied_rl(expr
, &rl
))
1960 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
1965 static int splitable_function_call(struct expression
*expr
)
1967 struct sm_state
*sm
;
1969 if (!expr
|| expr
->type
!= EXPR_CALL
)
1971 sm
= get_extra_sm_state(expr
);
1972 return split_possible_helper(sm
, expr
);
1975 static struct sm_state
*find_bool_param(void)
1977 struct stree
*start_states
;
1979 struct sm_state
*sm
, *tmp
;
1982 start_states
= get_start_states();
1984 FOR_EACH_PTR_REVERSE(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
1987 sm
= get_sm_state_stree(start_states
, SMATCH_EXTRA
, arg
->ident
->name
, arg
);
1990 if (rl_min(estate_rl(sm
->state
)).value
!= 0 ||
1991 rl_max(estate_rl(sm
->state
)).value
!= 1)
1994 } END_FOR_EACH_PTR_REVERSE(arg
);
2000 * Check if it's splitable. If not, then splitting it up is likely not
2001 * useful for the callers.
2003 FOR_EACH_PTR(sm
->possible
, tmp
) {
2006 if (!estate_get_single_value(tmp
->state
, &sval
))
2008 } END_FOR_EACH_PTR(tmp
);
2013 static int split_on_bool_sm(struct sm_state
*sm
, struct expression
*expr
)
2015 struct returned_state_callback
*cb
;
2016 struct range_list
*ret_rl
;
2017 const char *return_ranges
;
2018 struct sm_state
*tmp
;
2020 struct state_list
*already_handled
= NULL
;
2022 if (!sm
|| !sm
->merged
)
2025 if (too_many_possible(sm
))
2028 FOR_EACH_PTR(sm
->possible
, tmp
) {
2031 if (ptr_in_list(tmp
, already_handled
))
2033 add_ptr_list(&already_handled
, tmp
);
2036 __push_fake_cur_stree();
2038 overwrite_states_using_pool(sm
, tmp
);
2040 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
2041 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
2043 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
2044 cb
->callback(return_id
, (char *)return_ranges
, expr
);
2045 } END_FOR_EACH_PTR(cb
);
2047 __free_fake_cur_stree();
2048 } END_FOR_EACH_PTR(tmp
);
2050 free_slist(&already_handled
);
2055 static int split_by_bool_param(struct expression
*expr
)
2057 struct sm_state
*start_sm
, *sm
;
2060 start_sm
= find_bool_param();
2063 sm
= get_sm_state(SMATCH_EXTRA
, start_sm
->name
, start_sm
->sym
);
2064 if (!sm
|| estate_get_single_value(sm
->state
, &sval
))
2067 if (get_db_state_count() * 2 >= 2000)
2070 return split_on_bool_sm(sm
, expr
);
2073 static int split_by_null_nonnull_param(struct expression
*expr
)
2076 struct sm_state
*sm
;
2079 /* function must only take one pointer */
2080 if (ptr_list_size((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
) != 1)
2082 arg
= first_ptr_list((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
);
2085 if (get_real_base_type(arg
)->type
!= SYM_PTR
)
2088 if (param_was_set_var_sym(arg
->ident
->name
, arg
))
2090 sm
= get_sm_state(SMATCH_EXTRA
, arg
->ident
->name
, arg
);
2094 if (!has_separate_zero_null(sm
))
2097 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
2098 if (get_db_state_count() * nr_possible
>= 2000)
2101 return split_on_bool_sm(sm
, expr
);
2104 struct expression
*strip_expr_statement(struct expression
*expr
)
2106 struct expression
*orig
= expr
;
2107 struct statement
*stmt
, *last_stmt
;
2111 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '(')
2113 if (expr
->type
!= EXPR_STATEMENT
)
2115 stmt
= expr
->statement
;
2116 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
2119 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
2120 if (!last_stmt
|| last_stmt
->type
== STMT_LABEL
)
2121 last_stmt
= last_stmt
->label_statement
;
2122 if (!last_stmt
|| last_stmt
->type
!= STMT_EXPRESSION
)
2124 return strip_expr(last_stmt
->expression
);
2127 static bool is_kernel_error_path(struct expression
*expr
)
2129 struct range_list
*rl
;
2132 * Splitting up returns requires resources. It also requires resources
2133 * for the caller. It doesn't seem worth it to split anything up.
2135 if (!get_implied_rl(expr
, &rl
))
2137 if (rl_type(rl
) != &int_ctype
)
2139 if (rl_min(rl
).value
>= -4095 &&
2140 rl_max(rl
).value
< 0)
2145 static void call_return_state_hooks(struct expression
*expr
)
2147 struct returned_state_callback
*cb
;
2148 struct range_list
*ret_rl
;
2149 const char *return_ranges
;
2153 if (__path_is_null())
2156 expr
= strip_expr(expr
);
2157 expr
= strip_expr_statement(expr
);
2159 if (is_impossible_path())
2162 if (expr
&& (expr
->type
== EXPR_COMPARE
||
2163 !get_implied_value(expr
, &sval
)) &&
2164 (is_condition(expr
) || is_boolean(expr
))) {
2165 call_return_state_hooks_compare(expr
);
2167 } else if (call_return_state_hooks_conditional(expr
)) {
2169 } else if (is_kernel_error_path(expr
)) {
2171 } else if (call_return_state_hooks_split_possible(expr
)) {
2173 } else if (split_positive_from_negative(expr
)) {
2175 } else if (call_return_state_hooks_split_null_non_null_zero(expr
)) {
2177 } else if (call_return_state_hooks_split_success_fail(expr
)) {
2179 } else if (splitable_function_call(expr
)) {
2181 } else if (split_by_bool_param(expr
)) {
2182 } else if (split_by_null_nonnull_param(expr
)) {
2187 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
2188 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
2191 nr_states
= get_db_state_count();
2192 if (nr_states
>= 10000) {
2193 match_return_info(return_id
, (char *)return_ranges
, expr
);
2194 print_limited_param_set(return_id
, (char *)return_ranges
, expr
);
2195 mark_all_params_untracked(return_id
, (char *)return_ranges
, expr
);
2198 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
2199 cb
->callback(return_id
, (char *)return_ranges
, expr
);
2200 } END_FOR_EACH_PTR(cb
);
2203 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
2205 struct returned_member_callback
*cb
;
2206 struct sm_state
*sm
;
2207 struct symbol
*type
;
2209 char member_name
[256];
2212 type
= get_type(expr
);
2213 if (!type
|| type
->type
!= SYM_PTR
)
2215 name
= expr_to_var(expr
);
2220 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
2221 FOR_EACH_MY_SM(cb
->owner
, __get_cur_stree(), sm
) {
2222 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
2223 strcpy(member_name
, "*$");
2224 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
2227 if (strncmp(sm
->name
, name
, len
) != 0)
2229 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
2231 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
2232 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
2233 } END_FOR_EACH_SM(sm
);
2234 } END_FOR_EACH_PTR(cb
);
2239 static void print_return_struct_info(int return_id
, char *return_ranges
,
2240 struct expression
*expr
,
2242 struct return_info_callback
*cb
)
2244 struct sm_state
*sm
;
2245 const char *printed_name
;
2248 FOR_EACH_MY_SM(cb
->owner
, __get_cur_stree(), sm
) {
2249 if (sm
->sym
&& sm
->sym
== sym
) {
2252 param
= get_param_num_from_sym(sm
->sym
);
2257 printed_name
= get_param_name(sm
);
2261 cb
->callback(return_id
, return_ranges
, expr
, param
, printed_name
, sm
);
2262 } END_FOR_EACH_SM(sm
);
2265 static void print_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
2267 struct return_info_callback
*cb
;
2268 struct expression
*tmp
;
2271 tmp
= get_fake_variable(expr
);
2274 sym
= expr_to_sym(expr
);
2276 FOR_EACH_PTR(return_callbacks
, cb
) {
2277 print_return_struct_info(return_id
, return_ranges
, expr
, sym
, cb
);
2278 } END_FOR_EACH_PTR(cb
);
2281 static void reset_memdb(struct symbol
*sym
)
2283 mem_sql(NULL
, NULL
, "delete from caller_info;");
2284 mem_sql(NULL
, NULL
, "delete from return_states;");
2285 mem_sql(NULL
, NULL
, "delete from call_implies;");
2286 mem_sql(NULL
, NULL
, "delete from return_implies;");
2289 static void match_end_func_info(struct symbol
*sym
)
2291 if (__path_is_null())
2293 call_return_state_hooks(NULL
);
2296 static void match_after_func(struct symbol
*sym
)
2302 static void init_memdb(void)
2306 const char *schema_files
[] = {
2308 "db/caller_info.schema",
2309 "db/common_caller_info.schema",
2310 "db/return_states.schema",
2311 "db/function_type_size.schema",
2312 "db/type_size.schema",
2313 "db/function_type_info.schema",
2314 "db/type_info.schema",
2315 "db/call_implies.schema",
2316 "db/return_implies.schema",
2317 "db/function_ptr.schema",
2318 "db/local_values.schema",
2319 "db/function_type_value.schema",
2320 "db/type_value.schema",
2321 "db/function_type.schema",
2322 "db/data_info.schema",
2323 "db/parameter_name.schema",
2324 "db/constraints.schema",
2325 "db/constraints_required.schema",
2326 "db/fn_ptr_data_link.schema",
2327 "db/fn_data_link.schema",
2328 "db/mtag_about.schema",
2329 "db/mtag_info.schema",
2330 "db/mtag_map.schema",
2331 "db/mtag_data.schema",
2332 "db/mtag_alias.schema",
2334 static char buf
[4096];
2339 rc
= sqlite3_open(":memory:", &mem_db
);
2340 if (rc
!= SQLITE_OK
) {
2341 sm_ierror("starting In-Memory database.");
2345 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
2346 fd
= open_schema_file(schema_files
[i
]);
2349 ret
= read(fd
, buf
, sizeof(buf
));
2351 sm_ierror("failed to read: %s", schema_files
[i
]);
2355 if (ret
== sizeof(buf
)) {
2356 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2357 schema_files
[i
], sizeof(buf
));
2361 rc
= sqlite3_exec(mem_db
, buf
, NULL
, NULL
, &err
);
2362 if (rc
!= SQLITE_OK
) {
2363 sm_ierror("SQL error #2: %s", err
);
2364 sm_ierror("%s", buf
);
2369 static void init_cachedb(void)
2373 const char *schema_files
[] = {
2374 "db/call_implies.schema",
2375 "db/return_implies.schema",
2376 "db/type_info.schema",
2377 "db/mtag_about.schema",
2378 "db/mtag_data.schema",
2379 "db/mtag_info.schema",
2380 "db/sink_info.schema",
2382 static char buf
[4096];
2387 rc
= sqlite3_open(":memory:", &cache_db
);
2388 if (rc
!= SQLITE_OK
) {
2389 sm_ierror("starting In-Memory database.");
2393 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
2394 fd
= open_schema_file(schema_files
[i
]);
2397 ret
= read(fd
, buf
, sizeof(buf
));
2399 sm_ierror("failed to read: %s", schema_files
[i
]);
2403 if (ret
== sizeof(buf
)) {
2404 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2405 schema_files
[i
], sizeof(buf
));
2409 rc
= sqlite3_exec(cache_db
, buf
, NULL
, NULL
, &err
);
2410 if (rc
!= SQLITE_OK
) {
2411 sm_ierror("SQL error #2: %s", err
);
2412 sm_ierror("%s", buf
);
2417 static int save_cache_data(void *_table
, int argc
, char **argv
, char **azColName
)
2419 static char buf
[4096];
2422 char *table
= _table
;
2426 p
+= snprintf(p
, 4096 - (p
- buf
), "insert or ignore into %s values (", table
);
2427 for (i
= 0; i
< argc
; i
++) {
2429 p
+= snprintf(p
, 4096 - (p
- buf
), ", ");
2430 sqlite3_snprintf(sizeof(tmp
), tmp
, "%q", escape_newlines(argv
[i
]));
2431 p
+= snprintf(p
, 4096 - (p
- buf
), "'%s'", tmp
);
2434 p
+= snprintf(p
, 4096 - (p
- buf
), ");");
2438 sm_msg("SQL: %s", buf
);
2442 static void dump_cache(struct symbol_list
*sym_list
)
2444 const char *cache_tables
[] = {
2445 "type_info", "return_implies", "call_implies", "mtag_data",
2446 "mtag_info", "mtag_about", "sink_info",
2454 for (i
= 0; i
< ARRAY_SIZE(cache_tables
); i
++) {
2455 snprintf(buf
, sizeof(buf
), "select * from %s;", cache_tables
[i
]);
2456 cache_sql(&save_cache_data
, (char *)cache_tables
[i
], buf
);
2460 void open_smatch_db(char *db_file
)
2467 use_states
= malloc(num_checks
+ 1);
2468 memset(use_states
, 0xff, num_checks
+ 1);
2473 rc
= sqlite3_open_v2(db_file
, &smatch_db
, SQLITE_OPEN_READONLY
, NULL
);
2474 if (rc
!= SQLITE_OK
) {
2479 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES
);
2483 static void register_common_funcs(void)
2485 struct token
*token
;
2489 if (option_project
== PROJ_NONE
)
2490 strcpy(filename
, "common_functions");
2492 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
2494 token
= get_tokens_file(filename
);
2497 if (token_type(token
) != TOKEN_STREAMBEGIN
)
2499 token
= token
->next
;
2500 while (token_type(token
) != TOKEN_STREAMEND
) {
2501 if (token_type(token
) != TOKEN_IDENT
)
2503 func
= alloc_string(show_ident(token
->ident
));
2504 add_ptr_list(&common_funcs
, func
);
2505 token
= token
->next
;
2507 clear_token_alloc();
2510 static char *get_next_string(char **str
)
2512 static char string
[256];
2521 while (*p
!= '\0' && *p
!= '\n') {
2522 if (*p
== '\\' && *(p
+ 1) == ' ') {
2532 if (len
>= sizeof(string
)) {
2533 memcpy(string
, start
, sizeof(string
));
2534 string
[sizeof(string
) - 1] = '\0';
2535 sm_ierror("return_fix: '%s' too long", string
);
2539 memcpy(string
, start
, len
);
2541 for (i
= 0; i
< sizeof(string
) - 1; i
++) {
2542 if (string
[i
] == '\\' && string
[i
+ 1] == ' ') {
2543 for (j
= i
; string
[j
] != '\0'; j
++)
2544 string
[j
] = string
[j
+ 1];
2553 static void register_return_replacements(void)
2555 char *func
, *orig
, *new;
2561 snprintf(filename
, 256, "db/%s.return_fixes", option_project_str
);
2562 fd
= open_schema_file(filename
);
2565 ret
= read(fd
, buf
, sizeof(buf
));
2569 if (ret
== sizeof(buf
)) {
2570 sm_ierror("file too large: %s (limit %zd bytes)",
2571 filename
, sizeof(buf
));
2578 get_next_string(&p
);
2581 if (replace_count
== 0 || replace_count
% 3 != 0) {
2585 replace_table
= malloc(replace_count
* sizeof(char *));
2590 func
= alloc_string(get_next_string(&p
));
2591 orig
= alloc_string(get_next_string(&p
));
2592 new = alloc_string(get_next_string(&p
));
2594 replace_table
[i
++] = func
;
2595 replace_table
[i
++] = orig
;
2596 replace_table
[i
++] = new;
2600 void register_definition_db_callbacks(int id
)
2602 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2603 add_hook(&match_call_info_new
, FUNCTION_CALL_HOOK
);
2604 add_split_return_callback(match_return_info
);
2605 add_split_return_callback(print_returned_struct_members
);
2606 add_split_return_callback(print_return_info
);
2607 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
2608 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
2609 add_hook(&match_after_func
, AFTER_FUNC_HOOK
);
2611 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
2612 add_hook(&match_call_implies
, FUNC_DEF_HOOK
);
2613 add_hook(&match_return_implies
, CALL_HOOK_AFTER_INLINE
);
2615 register_common_funcs();
2616 register_return_replacements();
2618 add_hook(&dump_cache
, END_FILE_HOOK
);
2621 void register_db_call_marker(int id
)
2623 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
2626 char *return_state_to_var_sym(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
)
2628 struct expression
*arg
;
2630 char member_name
[256];
2635 const char *star
= "";
2637 if (expr
->type
!= EXPR_ASSIGNMENT
)
2639 if (get_type(expr
->left
) == &int_ctype
&& strcmp(key
, "$") != 0)
2641 name
= expr_to_var_sym(expr
->left
, sym
);
2644 if (key
[0] == '*') {
2648 if (strncmp(key
, "$", 1) != 0)
2650 snprintf(member_name
, sizeof(member_name
), "%s%s%s", star
, name
, key
+ 1);
2652 return alloc_string(member_name
);
2655 while (expr
->type
== EXPR_ASSIGNMENT
)
2656 expr
= strip_expr(expr
->right
);
2657 if (expr
->type
!= EXPR_CALL
)
2660 arg
= get_argument_from_call_expr(expr
->args
, param
);
2664 return get_variable_from_key(arg
, key
, sym
);
2667 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
)
2669 struct symbol
*type
;
2673 bool add_dot
= false;
2678 arg
= strip_expr(arg
);
2680 if (strcmp(key
, "$") == 0)
2681 return expr_to_var_sym(arg
, sym
);
2683 if (strcmp(key
, "*$") == 0) {
2684 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
2685 arg
= strip_expr(arg
->unop
);
2686 return expr_to_var_sym(arg
, sym
);
2688 tmp
= expr_to_var_sym(arg
, sym
);
2691 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
2693 return alloc_string(buf
);
2697 if (strncmp(key
, "(*$)", 4) == 0) {
2700 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
2701 arg
= strip_expr(arg
->unop
);
2702 snprintf(buf
, sizeof(buf
), "$%s", key
+ 4);
2703 return get_variable_from_key(arg
, buf
, sym
);
2705 tmp
= expr_to_var_sym(arg
, sym
);
2708 snprintf(buf
, sizeof(buf
), "(*%s)%s", tmp
, key
+ 4);
2710 return alloc_string(buf
);
2714 while (key
[0] == '*') {
2720 * FIXME: This is a hack.
2721 * We should be able to parse expressions like (*$)->foo and *$->foo.
2723 type
= get_type(arg
);
2724 if (is_struct_ptr(type
))
2727 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&' && star_cnt
&& !add_dot
) {
2728 arg
= strip_expr(arg
->unop
);
2732 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
2733 arg
= strip_expr(arg
->unop
);
2734 tmp
= expr_to_var_sym(arg
, sym
);
2737 snprintf(buf
, sizeof(buf
), "%.*s%s.%s",
2738 star_cnt
, "**********", tmp
, key
+ 3);
2739 return alloc_string(buf
);
2742 tmp
= expr_to_var_sym(arg
, sym
);
2745 snprintf(buf
, sizeof(buf
), "%.*s%s%s", star_cnt
, "**********", tmp
, key
+ 1);
2747 return alloc_string(buf
);
2750 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
)
2754 if (strcmp("$", key
) == 0)
2755 return expr_to_chunk_sym_vsl(arg
, sym
, vsl
);
2756 return get_variable_from_key(arg
, key
, sym
);
2759 const char *state_name_to_param_name(const char *state_name
, const char *param_name
)
2765 name_len
= strlen(param_name
);
2767 while (state_name
[0] == '*') {
2772 /* ten out of ten stars! */
2776 if (strncmp(state_name
, "(*", 2) == 0 &&
2777 strncmp(state_name
+ 2, param_name
, name_len
) == 0 &&
2778 state_name
[name_len
+ 2] == ')') {
2779 snprintf(buf
, sizeof(buf
), "%.*s(*$)%s", star_cnt
, "**********",
2780 state_name
+ name_len
+ 3);
2781 return alloc_sname(buf
);
2784 if (strcmp(state_name
, param_name
) == 0) {
2785 snprintf(buf
, sizeof(buf
), "%.*s$", star_cnt
, "**********");
2786 return alloc_sname(buf
);
2789 /* check for '-' from "->" */
2790 if (strncmp(state_name
, param_name
, name_len
) == 0 &&
2791 state_name
[name_len
] == '-') {
2792 snprintf(buf
, sizeof(buf
), "%.*s$%s", star_cnt
, "**********", state_name
+ name_len
);
2793 return alloc_sname(buf
);
2798 const char *get_param_name_var_sym(const char *name
, struct symbol
*sym
)
2800 if (!sym
|| !sym
->ident
)
2803 return state_name_to_param_name(name
, sym
->ident
->name
);
2806 const char *get_mtag_name_var_sym(const char *state_name
, struct symbol
*sym
)
2808 struct symbol
*type
;
2809 const char *sym_name
;
2811 static char buf
[256];
2814 * mtag_name is different from param_name because mtags can be a struct
2815 * instead of a struct pointer. But we want to treat it like a pointer
2816 * because really an mtag is a pointer. Or in other words, if you pass
2817 * a struct foo then you want to talk about foo.bar but with an mtag
2818 * you want to refer to it as foo->bar.
2822 if (!sym
|| !sym
->ident
)
2825 type
= get_real_base_type(sym
);
2826 if (type
&& type
->type
== SYM_BASETYPE
)
2829 sym_name
= sym
->ident
->name
;
2830 name_len
= strlen(sym_name
);
2832 if (state_name
[name_len
] == '.' && /* check for '-' from "->" */
2833 strncmp(state_name
, sym_name
, name_len
) == 0) {
2834 snprintf(buf
, sizeof(buf
), "$->%s", state_name
+ name_len
+ 1);
2838 return state_name_to_param_name(state_name
, sym_name
);
2841 const char *get_mtag_name_expr(struct expression
*expr
)
2845 const char *ret
= NULL
;
2847 name
= expr_to_var_sym(expr
, &sym
);
2851 ret
= get_mtag_name_var_sym(name
, sym
);
2857 const char *get_param_name(struct sm_state
*sm
)
2859 return get_param_name_var_sym(sm
->name
, sm
->sym
);
2862 char *get_data_info_name(struct expression
*expr
)
2869 expr
= strip_expr(expr
);
2870 name
= get_member_name(expr
);
2873 name
= expr_to_var_sym(expr
, &sym
);
2876 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
2878 if (sym
->ctype
.modifiers
& MOD_STATIC
)
2879 snprintf(buf
, sizeof(buf
), "static %s", name
);
2881 snprintf(buf
, sizeof(buf
), "global %s", name
);
2882 ret
= alloc_sname(buf
);