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 returned_state_callback
{
56 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*return_expr
);
58 ALLOCATOR(returned_state_callback
, "returned state callbacks");
59 DECLARE_PTR_LIST(returned_state_cb_list
, struct returned_state_callback
);
60 static struct returned_state_cb_list
*returned_state_callbacks
;
62 struct returned_member_callback
{
64 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*expr
, char *printed_name
, struct smatch_state
*state
);
66 ALLOCATOR(returned_member_callback
, "returned member callbacks");
67 DECLARE_PTR_LIST(returned_member_cb_list
, struct returned_member_callback
);
68 static struct returned_member_cb_list
*returned_member_callbacks
;
70 struct db_implies_callback
{
72 void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
);
74 ALLOCATOR(db_implies_callback
, "return_implies callbacks");
75 DECLARE_PTR_LIST(db_implies_cb_list
, struct db_implies_callback
);
76 static struct db_implies_cb_list
*return_implies_cb_list
;
77 static struct db_implies_cb_list
*call_implies_cb_list
;
79 /* silently truncates if needed. */
80 char *escape_newlines(const char *str
)
86 for (i
= 0, j
= 0; str
[i
] != '\0' && j
!= sizeof(buf
); i
++, j
++) {
87 if (str
[i
] != '\r' && str
[i
] != '\n') {
100 return alloc_sname(str
);
102 if (j
== sizeof(buf
))
104 return alloc_sname(buf
);
107 static int print_sql_output(void *unused
, int argc
, char **argv
, char **azColName
)
111 for (i
= 0; i
< argc
; i
++) {
114 sm_printf("%s", argv
[i
]);
120 void sql_exec(struct sqlite3
*db
, int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
)
128 if (option_debug
|| debug_db
) {
130 if (strncasecmp(sql
, "select", strlen("select")) == 0)
131 sqlite3_exec(db
, sql
, print_sql_output
, NULL
, NULL
);
134 rc
= sqlite3_exec(db
, sql
, callback
, data
, &err
);
135 if (rc
!= SQLITE_OK
&& !parse_error
) {
136 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err
);
137 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql
);
142 static int replace_count
;
143 static char **replace_table
;
144 static const char *replace_return_ranges(const char *return_ranges
)
148 if (!get_function()) {
149 /* I have no idea why EXPORT_SYMBOL() is here */
150 return return_ranges
;
152 for (i
= 0; i
< replace_count
; i
+= 3) {
153 if (strcmp(replace_table
[i
+ 0], get_function()) == 0) {
154 if (strcmp(replace_table
[i
+ 1], return_ranges
) == 0)
155 return replace_table
[i
+ 2];
158 return return_ranges
;
162 static char *use_states
;
163 static int get_db_state_count(void)
168 FOR_EACH_SM(__get_cur_stree(), sm
) {
169 if (sm
->owner
== USHRT_MAX
)
171 if (use_states
[sm
->owner
])
173 } END_FOR_EACH_SM(sm
);
177 void db_ignore_states(int id
)
182 unsigned long long __fn_mtag
;
183 static void set_fn_mtag(struct symbol
*sym
)
187 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
188 snprintf(buf
, sizeof(buf
), "%s %s", get_base_file(), get_function());
190 snprintf(buf
, sizeof(buf
), "extern %s", get_function());
192 __fn_mtag
= str_to_mtag(buf
);
195 void sql_insert_return_states(int return_id
, const char *return_ranges
,
196 int type
, int param
, const char *key
, const char *value
)
198 unsigned long long id
;
201 if (key
&& strlen(key
) >= 80)
204 id
= (unsigned long)__inline_fn
;
208 return_ranges
= replace_return_ranges(return_ranges
);
209 sql_insert(return_states
, "'%s', '%s', %llu, %d, '%s', %d, %d, %d, '%s', '%s'",
210 get_base_file(), get_function(), id
, return_id
,
211 return_ranges
, fn_static(), type
, param
, key
, value
);
214 static struct string_list
*common_funcs
;
215 static int is_common_function(const char *fn
)
222 if (strncmp(fn
, "__builtin_", 10) == 0)
225 FOR_EACH_PTR(common_funcs
, tmp
) {
226 if (strcmp(tmp
, fn
) == 0)
228 } END_FOR_EACH_PTR(tmp
);
233 static char *function_signature(void)
235 return type_to_str(get_real_base_type(cur_func_sym
));
238 void sql_insert_caller_info(struct expression
*call
, int type
,
239 int param
, const char *key
, const char *value
)
241 FILE *tmp_fd
= sm_outfd
;
244 if (!option_info
&& !__inline_call
)
247 if (key
&& strlen(key
) >= 80)
250 fn
= get_fnptr_name(call
->fn
);
256 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
257 get_base_file(), get_function(), fn
, (unsigned long)call
,
258 is_static(call
->fn
), type
, param
, key
, value
);
264 if (strncmp(fn
, "__builtin_", 10) == 0)
266 if (type
!= INTERNAL
&& is_common_function(fn
))
269 sm_outfd
= caller_info_fd
;
270 sm_msg("SQL_caller_info: insert into caller_info values ("
271 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
272 get_base_file(), get_function(), fn
, is_static(call
->fn
),
273 type
, param
, key
, value
);
279 void sql_insert_function_ptr(const char *fn
, const char *struct_name
)
281 sql_insert_or_ignore(function_ptr
, "'%s', '%s', '%s', 0",
282 get_base_file(), fn
, struct_name
);
285 void sql_insert_return_implies(int type
, int param
, const char *key
, const char *value
)
287 sql_insert_or_ignore(return_implies
, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
288 get_base_file(), get_function(), (unsigned long)__inline_fn
,
289 fn_static(), type
, param
, key
, value
);
292 void sql_insert_call_implies(int type
, int param
, const char *key
, const char *value
)
294 sql_insert_or_ignore(call_implies
, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
295 get_base_file(), get_function(), (unsigned long)__inline_fn
,
296 fn_static(), type
, param
, key
, value
);
299 void sql_insert_function_type_size(const char *member
, const char *ranges
)
301 sql_insert(function_type_size
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member
, ranges
);
304 void sql_insert_function_type_info(int type
, const char *struct_type
, const char *member
, const char *value
)
306 sql_insert(function_type_info
, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type
, struct_type
, member
, value
);
309 void sql_insert_type_info(int type
, const char *member
, const char *value
)
311 sql_insert_cache(type_info
, "'%s', %d, '%s', '%s'", get_base_file(), type
, member
, value
);
314 void sql_insert_local_values(const char *name
, const char *value
)
316 sql_insert(local_values
, "'%s', '%s', '%s'", get_base_file(), name
, value
);
319 void sql_insert_function_type_value(const char *type
, const char *value
)
321 sql_insert(function_type_value
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type
, value
);
324 void sql_insert_function_type(int param
, const char *value
)
326 sql_insert(function_type
, "'%s', '%s', %d, %d, '%s'",
327 get_base_file(), get_function(), fn_static(), param
, value
);
330 void sql_insert_parameter_name(int param
, const char *value
)
332 sql_insert(parameter_name
, "'%s', '%s', %d, %d, '%s'",
333 get_base_file(), get_function(), fn_static(), param
, value
);
336 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
)
340 data_name
= get_data_info_name(data
);
343 sql_insert(data_info
, "'%s', '%s', %d, '%s'",
344 is_static(data
) ? get_base_file() : "extern",
345 data_name
, type
, value
);
348 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
)
350 sql_insert(data_info
, "'%s', '%s', %d, '%s'",
351 (sym
->ctype
.modifiers
& MOD_STATIC
) ? get_base_file() : "extern",
355 void sql_save_constraint(const char *con
)
360 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con
));
363 void sql_save_constraint_required(const char *data
, int op
, const char *limit
)
365 sql_insert_or_ignore(constraints_required
, "'%s', '%s', '%s'", data
, show_special(op
), limit
);
368 void sql_copy_constraint_required(const char *new_limit
, const char *old_limit
)
373 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
374 "select constraints_required.data, constraints_required.op, '%s' from "
375 "constraints_required where bound = '%s';", new_limit
, old_limit
);
378 void sql_insert_fn_ptr_data_link(const char *ptr
, const char *data
)
380 sql_insert_or_ignore(fn_ptr_data_link
, "'%s', '%s'", ptr
, data
);
383 void sql_insert_fn_data_link(struct expression
*fn
, int type
, int param
, const char *key
, const char *value
)
385 if (fn
->type
!= EXPR_SYMBOL
|| !fn
->symbol
->ident
)
388 sql_insert(fn_data_link
, "'%s', '%s', %d, %d, %d, '%s', '%s'",
389 (fn
->symbol
->ctype
.modifiers
& MOD_STATIC
) ? get_base_file() : "extern",
390 fn
->symbol
->ident
->name
,
391 !!(fn
->symbol
->ctype
.modifiers
& MOD_STATIC
),
392 type
, param
, key
, value
);
395 void sql_insert_mtag_about(mtag_t tag
, const char *left_name
, const char *right_name
)
397 sql_insert_cache(mtag_about
, "%lld, '%s', '%s', %d, '%s', '%s'",
398 tag
, get_filename(), get_function(), get_lineno(),
399 left_name
, right_name
);
402 void sql_insert_mtag_info(mtag_t tag
, int type
, const char *value
)
404 sql_insert_cache(mtag_info
, "'%s', %lld, %d, '%s'", get_filename(), tag
, type
, value
);
407 void sql_insert_mtag_map(mtag_t container
, int container_offset
, mtag_t tag
, int tag_offset
)
409 sql_insert(mtag_map
, "%lld, %d, %lld, %d", container
, container_offset
, tag
, tag_offset
);
412 void sql_insert_mtag_alias(mtag_t orig
, mtag_t alias
)
414 sql_insert(mtag_alias
, "%lld, %lld", orig
, alias
);
417 static int save_mtag(void *_tag
, int argc
, char **argv
, char **azColName
)
419 mtag_t
*saved_tag
= _tag
;
422 new_tag
= strtoll(argv
[0], NULL
, 10);
425 *saved_tag
= new_tag
;
426 else if (*saved_tag
!= new_tag
)
432 int mtag_map_select_container(mtag_t tag
, int container_offset
, mtag_t
*container
)
436 run_sql(save_mtag
, &tmp
,
437 "select container from mtag_map where tag = %lld and container_offset = %d and tag_offset = 0;",
438 tag
, container_offset
);
440 if (tmp
== 0 || tmp
== -1ULL)
446 int mtag_map_select_tag(mtag_t container
, int offset
, mtag_t
*tag
)
450 run_sql(save_mtag
, &tmp
,
451 "select tag from mtag_map where container = %lld and container_offset = %d;",
454 if (tmp
== 0 || tmp
== -1ULL)
460 char *get_static_filter(struct symbol
*sym
)
462 static char sql_filter
[1024];
464 /* This can only happen on buggy code. Return invalid SQL. */
466 sql_filter
[0] = '\0';
470 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
471 snprintf(sql_filter
, sizeof(sql_filter
),
472 "file = '%s' and function = '%s' and static = '1'",
473 get_base_file(), sym
->ident
->name
);
475 snprintf(sql_filter
, sizeof(sql_filter
),
476 "function = '%s' and static = '0'", sym
->ident
->name
);
482 static int get_row_count(void *_row_count
, int argc
, char **argv
, char **azColName
)
484 int *row_count
= _row_count
;
489 *row_count
= atoi(argv
[0]);
493 static void mark_call_params_untracked(struct expression
*call
)
495 struct expression
*arg
;
498 FOR_EACH_PTR(call
->args
, arg
) {
499 mark_untracked(call
, i
++, "$", NULL
);
500 } END_FOR_EACH_PTR(arg
);
503 static void sql_select_return_states_pointer(const char *cols
,
504 struct expression
*call
, int (*callback
)(void*, int, char**, char**), void *info
)
507 int return_count
= 0;
509 ptr
= get_fnptr_name(call
->fn
);
513 run_sql(get_row_count
, &return_count
,
514 "select count(*) from return_states join function_ptr "
515 "where return_states.function == function_ptr.function and "
516 "ptr = '%s' and searchable = 1 and type = %d;", ptr
, INTERNAL
);
517 /* The magic number 100 is just from testing on the kernel. */
518 if (return_count
> 100) {
519 mark_call_params_untracked(call
);
523 run_sql(callback
, info
,
524 "select %s from return_states join function_ptr where "
525 "return_states.function == function_ptr.function and ptr = '%s' "
526 "and searchable = 1 "
527 "order by function_ptr.file, return_states.file, return_id, type;",
531 static int is_local_symbol(struct expression
*expr
)
533 if (expr
->type
!= EXPR_SYMBOL
)
535 if (expr
->symbol
->ctype
.modifiers
& (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
))
540 void sql_select_return_states(const char *cols
, struct expression
*call
,
541 int (*callback
)(void*, int, char**, char**), void *info
)
543 struct expression
*fn
;
546 if (is_fake_call(call
))
549 fn
= strip_expr(call
->fn
);
550 if (fn
->type
!= EXPR_SYMBOL
|| !fn
->symbol
|| is_local_symbol(fn
)) {
551 sql_select_return_states_pointer(cols
, call
, callback
, info
);
556 mem_sql(callback
, info
,
557 "select %s from return_states where call_id = '%lu' order by return_id, type;",
558 cols
, (unsigned long)call
);
562 run_sql(get_row_count
, &row_count
, "select count(*) from return_states where %s;",
563 get_static_filter(fn
->symbol
));
564 if (row_count
> 3000)
567 run_sql(callback
, info
, "select %s from return_states where %s order by file, return_id, type;",
568 cols
, get_static_filter(fn
->symbol
));
571 #define CALL_IMPLIES 0
572 #define RETURN_IMPLIES 1
574 struct implies_info
{
576 struct db_implies_cb_list
*cb_list
;
577 struct expression
*expr
;
581 void sql_select_implies(const char *cols
, struct implies_info
*info
,
582 int (*callback
)(void*, int, char**, char**))
584 if (info
->type
== RETURN_IMPLIES
&& inlinable(info
->expr
->fn
)) {
585 mem_sql(callback
, info
,
586 "select %s from return_implies where call_id = '%lu';",
587 cols
, (unsigned long)info
->expr
);
591 run_sql(callback
, info
, "select %s from %s_implies where %s;",
593 info
->type
== CALL_IMPLIES
? "call" : "return",
594 get_static_filter(info
->sym
));
597 struct select_caller_info_data
{
598 struct stree
*final_states
;
599 struct timeval start_time
;
605 static int caller_info_callback(void *_data
, int argc
, char **argv
, char **azColName
);
607 static void sql_select_caller_info(struct select_caller_info_data
*data
,
608 const char *cols
, struct symbol
*sym
)
611 mem_sql(caller_info_callback
, data
,
612 "select %s from caller_info where call_id = %lu;",
613 cols
, (unsigned long)__inline_fn
);
617 if (sym
->ident
->name
&& is_common_function(sym
->ident
->name
))
619 run_sql(caller_info_callback
, data
,
620 "select %s from common_caller_info where %s order by call_id;",
621 cols
, get_static_filter(sym
));
625 run_sql(caller_info_callback
, data
,
626 "select %s from caller_info where %s order by call_id;",
627 cols
, get_static_filter(sym
));
630 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
632 struct def_callback
*def_callback
= __alloc_def_callback(0);
634 def_callback
->hook_type
= type
;
635 def_callback
->callback
= callback
;
636 add_ptr_list(&select_caller_info_callbacks
, def_callback
);
640 * These call backs are used when the --info option is turned on to print struct
641 * member information. For example foo->bar could have a state in
642 * smatch_extra.c and also check_user.c.
644 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
646 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
648 member_callback
->owner
= owner
;
649 member_callback
->callback
= callback
;
650 add_ptr_list(&member_callbacks
, member_callback
);
653 void add_caller_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
655 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
657 member_callback
->owner
= owner
;
658 member_callback
->callback
= callback
;
659 add_ptr_list(&member_callbacks_new
, member_callback
);
662 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
664 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
666 callback
->callback
= fn
;
667 add_ptr_list(&returned_state_callbacks
, callback
);
670 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
))
672 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
674 member_callback
->owner
= owner
;
675 member_callback
->callback
= callback
;
676 add_ptr_list(&returned_member_callbacks
, member_callback
);
679 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
))
681 struct db_implies_callback
*cb
= __alloc_db_implies_callback(0);
684 cb
->callback
= callback
;
685 add_ptr_list(&call_implies_cb_list
, cb
);
688 void select_return_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
))
690 struct db_implies_callback
*cb
= __alloc_db_implies_callback(0);
693 cb
->callback
= callback
;
694 add_ptr_list(&return_implies_cb_list
, cb
);
698 struct expression
*static_returns_call
;
699 struct symbol
*return_type
;
700 struct range_list
*return_range_list
;
703 static int db_return_callback(void *_ret_info
, int argc
, char **argv
, char **azColName
)
705 struct return_info
*ret_info
= _ret_info
;
706 struct range_list
*rl
;
707 struct expression
*call_expr
= ret_info
->static_returns_call
;
711 call_results_to_rl(call_expr
, ret_info
->return_type
, argv
[0], &rl
);
712 ret_info
->return_range_list
= rl_union(ret_info
->return_range_list
, rl
);
716 struct range_list
*db_return_vals(struct expression
*expr
)
718 struct return_info ret_info
= {};
722 if (is_fake_call(expr
))
725 snprintf(buf
, sizeof(buf
), "return %p", expr
);
726 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
728 return clone_rl(estate_rl(sm
->state
));
729 ret_info
.static_returns_call
= expr
;
730 ret_info
.return_type
= get_type(expr
);
731 if (!ret_info
.return_type
)
734 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
737 ret_info
.return_range_list
= NULL
;
738 if (inlinable(expr
->fn
)) {
739 mem_sql(db_return_callback
, &ret_info
,
740 "select distinct return from return_states where call_id = '%lu';",
741 (unsigned long)expr
);
743 run_sql(db_return_callback
, &ret_info
,
744 "select distinct return from return_states where %s;",
745 get_static_filter(expr
->fn
->symbol
));
747 return ret_info
.return_range_list
;
750 struct range_list
*db_return_vals_from_str(const char *fn_name
)
752 struct return_info ret_info
;
754 ret_info
.static_returns_call
= NULL
;
755 ret_info
.return_type
= &llong_ctype
;
756 ret_info
.return_range_list
= NULL
;
758 run_sql(db_return_callback
, &ret_info
,
759 "select distinct return from return_states where function = '%s';",
761 return ret_info
.return_range_list
;
765 * This is used when we have a function that takes a function pointer as a
766 * parameter. "frob(blah, blah, my_function);" We know that the return values
767 * from frob() come from my_funcion() so we want to find the possible returns
768 * of my_function(), but we don't know which arguments are passed to it.
771 struct range_list
*db_return_vals_no_args(struct expression
*expr
)
773 struct return_info ret_info
= {};
775 if (!expr
|| expr
->type
!= EXPR_SYMBOL
)
778 ret_info
.static_returns_call
= expr
;
779 ret_info
.return_type
= get_type(expr
);
780 ret_info
.return_type
= get_real_base_type(ret_info
.return_type
);
781 if (!ret_info
.return_type
)
784 run_sql(db_return_callback
, &ret_info
,
785 "select distinct return from return_states where %s;",
786 get_static_filter(expr
->symbol
));
788 return ret_info
.return_range_list
;
791 static void match_call_marker(struct expression
*expr
)
795 type
= get_type(expr
->fn
);
796 if (type
&& type
->type
== SYM_PTR
)
797 type
= get_real_base_type(type
);
800 * we just want to record something in the database so that if we have
801 * two calls like: frob(4); frob(some_unkown); then on the receiving
802 * side we know that sometimes frob is called with unknown parameters.
805 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", type_to_str(type
));
808 int is_recursive_member(const char *name
)
811 const char *p
, *next
;
814 p
= strchr(name
, '>');
819 next
= strchr(p
, '>');
825 if (size
>= sizeof(buf
))
827 memcpy(buf
, p
, size
);
829 if (strstr(next
, buf
))
835 char *sm_to_arg_name(struct expression
*expr
, struct sm_state
*sm
)
840 bool is_address
= false;
841 bool add_star
= false;
846 expr
= strip_expr(expr
);
850 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
851 expr
= strip_expr(expr
->unop
);
855 name
= expr_to_var_sym(expr
, &sym
);
863 if (sm_name
[0] == '*') {
869 if (strncmp(name
, sm_name
, len
) != 0)
871 if (sm_name
[len
] == '\0') {
872 snprintf(buf
, sizeof(buf
), "%s%s$",
873 add_star
? "*" : "", is_address
? "*" : "");
875 if (sm_name
[len
] != '.' && sm_name
[len
] != '-')
877 if (sm_name
[len
] == '-')
879 // FIXME does is_address really imply that sm_name[len] == '-'
880 snprintf(buf
, sizeof(buf
), "%s$->%s", add_star
? "*" : "",
884 ret
= alloc_sname(buf
);
890 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct stree
*stree
,
891 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
),
899 char printed_name
[256];
904 expr
= strip_expr(expr
);
907 type
= get_type(expr
);
908 if (type
&& type_bits(type
) < type_bits(&ulong_ctype
))
911 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
912 expr
= strip_expr(expr
->unop
);
916 name
= expr_to_var_sym(expr
, &sym
);
921 FOR_EACH_SM(stree
, sm
) {
926 if (sm_name
[0] == '*') {
931 if (!add_star
&& strcmp(name
, sm_name
) == 0) {
933 snprintf(printed_name
, sizeof(printed_name
), "*$");
936 snprintf(printed_name
, sizeof(printed_name
), "$");
940 } else if (add_star
&& strcmp(name
, sm_name
) == 0) {
941 snprintf(printed_name
, sizeof(printed_name
), "%s*$",
942 is_address
? "*" : "");
943 } else if (strncmp(name
, sm_name
, len
) == 0) {
944 if (sm_name
[len
] != '.' && sm_name
[len
] != '-')
946 if (is_address
&& sm_name
[len
] == '.') {
947 snprintf(printed_name
, sizeof(printed_name
),
948 "%s$->%s", add_star
? "*" : "",
950 } else if (is_address
&& sm_name
[len
] == '-') {
951 snprintf(printed_name
, sizeof(printed_name
),
952 "%s(*$)%s", add_star
? "*" : "",
955 snprintf(printed_name
, sizeof(printed_name
),
956 "%s$%s", add_star
? "*" : "",
962 if (is_recursive_member(printed_name
))
964 callback(call
, param
, printed_name
, sm
);
965 } END_FOR_EACH_SM(sm
);
970 static void match_call_info(struct expression
*call
)
972 struct member_info_callback
*cb
;
973 struct expression
*arg
;
978 name
= get_fnptr_name(call
->fn
);
982 FOR_EACH_PTR(member_callbacks
, cb
) {
983 stree
= get_all_states_stree(cb
->owner
);
985 FOR_EACH_PTR(call
->args
, arg
) {
986 print_struct_members(call
, arg
, i
, stree
, cb
->callback
, 0);
988 } END_FOR_EACH_PTR(arg
);
990 } END_FOR_EACH_PTR(cb
);
995 static void match_call_info_new(struct expression
*call
)
997 struct member_info_callback
*cb
;
998 struct expression
*arg
;
1003 name
= get_fnptr_name(call
->fn
);
1007 FOR_EACH_PTR(member_callbacks_new
, cb
) {
1008 stree
= get_all_states_stree(cb
->owner
);
1010 FOR_EACH_PTR(call
->args
, arg
) {
1011 print_struct_members(call
, arg
, i
, stree
, cb
->callback
, 1);
1013 } END_FOR_EACH_PTR(arg
);
1015 } END_FOR_EACH_PTR(cb
);
1020 static int get_param(int param
, char **name
, struct symbol
**sym
)
1026 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
1028 * this is a temporary hack to work around a bug (I think in sparse?)
1029 * 2.6.37-rc1:fs/reiserfs/journal.o
1030 * If there is a function definition without parameter name found
1031 * after a function implementation then it causes a crash.
1035 if (arg
->ident
->name
< (char *)100)
1038 *name
= arg
->ident
->name
;
1043 } END_FOR_EACH_PTR(arg
);
1048 static int function_signature_matches(const char *sig
)
1052 my_sig
= function_signature();
1053 if (!sig
|| !my_sig
)
1054 return 1; /* default to matching */
1055 if (strcmp(my_sig
, sig
) == 0)
1060 static int caller_info_callback(void *_data
, int argc
, char **argv
, char **azColName
)
1062 struct select_caller_info_data
*data
= _data
;
1069 struct symbol
*sym
= NULL
;
1070 struct def_callback
*def_callback
;
1071 struct stree
*stree
;
1072 struct timeval cur_time
;
1079 gettimeofday(&cur_time
, NULL
);
1080 if (cur_time
.tv_sec
- data
->start_time
.tv_sec
> 10)
1083 func_id
= atoi(argv
[0]);
1085 type
= strtol(argv
[1], NULL
, 10);
1086 param
= strtol(argv
[2], NULL
, 10);
1092 if (data
->prev_func_id
== -1)
1093 data
->prev_func_id
= func_id
;
1094 if (func_id
!= data
->prev_func_id
) {
1095 stree
= __pop_fake_cur_stree();
1097 merge_stree(&data
->final_states
, stree
);
1099 __push_fake_cur_stree();
1101 data
->prev_func_id
= func_id
;
1107 if (type
== INTERNAL
&&
1108 !function_signature_matches(value
)) {
1113 if (param
>= 0 && !get_param(param
, &name
, &sym
))
1116 FOR_EACH_PTR(select_caller_info_callbacks
, def_callback
) {
1117 if (def_callback
->hook_type
== type
)
1118 def_callback
->callback(name
, sym
, key
, value
);
1119 } END_FOR_EACH_PTR(def_callback
);
1124 static struct string_list
*ptr_names_done
;
1125 static struct string_list
*ptr_names
;
1127 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
1129 insert_string(&ptr_names
, alloc_string(argv
[0]));
1133 static char *get_next_ptr_name(void)
1137 FOR_EACH_PTR(ptr_names
, ptr
) {
1138 if (!insert_string(&ptr_names_done
, ptr
))
1141 } END_FOR_EACH_PTR(ptr
);
1145 static void get_ptr_names(const char *file
, const char *name
)
1147 char sql_filter
[1024];
1151 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
1154 snprintf(sql_filter
, 1024, "function = '%s';", name
);
1157 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
1159 run_sql(get_ptr_name
, NULL
,
1160 "select distinct ptr from function_ptr where %s",
1163 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
1164 if (before
== after
)
1167 while ((name
= get_next_ptr_name()))
1168 get_ptr_names(NULL
, name
);
1171 static void match_data_from_db(struct symbol
*sym
)
1173 struct select_caller_info_data data
= { .prev_func_id
= -1 };
1174 struct sm_state
*sm
;
1175 struct stree
*stree
;
1176 struct timeval end_time
;
1178 if (!sym
|| !sym
->ident
)
1182 gettimeofday(&data
.start_time
, NULL
);
1184 __push_fake_cur_stree();
1190 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1191 get_ptr_names(get_base_file(), sym
->ident
->name
);
1193 get_ptr_names(NULL
, sym
->ident
->name
);
1195 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
1196 __free_ptr_list((struct ptr_list
**)&ptr_names
);
1197 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
1198 __free_fake_cur_stree();
1202 sql_select_caller_info(&data
,
1203 "call_id, type, parameter, key, value",
1207 stree
= __pop_fake_cur_stree();
1209 merge_stree(&data
.final_states
, stree
);
1211 __push_fake_cur_stree();
1213 data
.prev_func_id
= -1;
1217 FOR_EACH_PTR(ptr_names
, ptr
) {
1218 run_sql(caller_info_callback
, &data
,
1219 "select call_id, type, parameter, key, value"
1220 " from common_caller_info where function = '%s' order by call_id",
1222 } END_FOR_EACH_PTR(ptr
);
1225 FOR_EACH_PTR(ptr_names
, ptr
) {
1227 } END_FOR_EACH_PTR(ptr
);
1228 goto free_ptr_names
;
1231 FOR_EACH_PTR(ptr_names
, ptr
) {
1232 run_sql(caller_info_callback
, &data
,
1233 "select call_id, type, parameter, key, value"
1234 " from caller_info where function = '%s' order by call_id",
1237 } END_FOR_EACH_PTR(ptr
);
1240 __free_ptr_list((struct ptr_list
**)&ptr_names
);
1241 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
1243 sql_select_caller_info(&data
,
1244 "call_id, type, parameter, key, value",
1248 stree
= __pop_fake_cur_stree();
1250 merge_stree(&data
.final_states
, stree
);
1253 gettimeofday(&end_time
, NULL
);
1254 if (end_time
.tv_sec
- data
.start_time
.tv_sec
<= 10) {
1255 FOR_EACH_SM(data
.final_states
, sm
) {
1257 } END_FOR_EACH_SM(sm
);
1260 free_stree(&data
.final_states
);
1263 static int return_implies_callbacks(void *_info
, int argc
, char **argv
, char **azColName
)
1265 struct implies_info
*info
= _info
;
1266 struct db_implies_callback
*cb
;
1267 struct expression
*arg
= NULL
;
1274 type
= atoi(argv
[1]);
1275 param
= atoi(argv
[2]);
1277 FOR_EACH_PTR(info
->cb_list
, cb
) {
1278 if (cb
->type
!= type
)
1281 arg
= get_argument_from_call_expr(info
->expr
->args
, param
);
1285 cb
->callback(info
->expr
, arg
, argv
[3], argv
[4]);
1286 } END_FOR_EACH_PTR(cb
);
1291 static int call_implies_callbacks(void *_info
, int argc
, char **argv
, char **azColName
)
1293 struct implies_info
*info
= _info
;
1294 struct db_implies_callback
*cb
;
1295 struct expression
*arg
;
1304 type
= atoi(argv
[1]);
1305 param
= atoi(argv
[2]);
1307 if (!get_param(param
, &name
, &sym
))
1309 arg
= symbol_expression(sym
);
1313 FOR_EACH_PTR(info
->cb_list
, cb
) {
1314 if (cb
->type
!= type
)
1316 cb
->callback(info
->expr
, arg
, argv
[3], argv
[4]);
1317 } END_FOR_EACH_PTR(cb
);
1322 static void match_return_implies(struct expression
*expr
)
1324 struct implies_info info
= {
1325 .type
= RETURN_IMPLIES
,
1326 .cb_list
= return_implies_cb_list
,
1329 if (expr
->fn
->type
!= EXPR_SYMBOL
||
1333 info
.sym
= expr
->fn
->symbol
;
1334 sql_select_implies("function, type, parameter, key, value", &info
,
1335 return_implies_callbacks
);
1338 static void match_call_implies(struct symbol
*sym
)
1340 struct implies_info info
= {
1341 .type
= CALL_IMPLIES
,
1342 .cb_list
= call_implies_cb_list
,
1345 if (!sym
|| !sym
->ident
)
1349 sql_select_implies("function, type, parameter, key, value", &info
,
1350 call_implies_callbacks
);
1353 static char *get_fn_param_str(struct expression
*expr
)
1355 struct expression
*tmp
;
1359 tmp
= get_assigned_expr(expr
);
1362 expr
= strip_expr(expr
);
1363 if (!expr
|| expr
->type
!= EXPR_CALL
)
1365 expr
= strip_expr(expr
->fn
);
1366 if (!expr
|| expr
->type
!= EXPR_SYMBOL
)
1368 param
= get_param_num(expr
);
1372 snprintf(buf
, sizeof(buf
), "[r $%d]", param
);
1373 return alloc_sname(buf
);
1376 static char *get_return_compare_is_param(struct expression
*expr
)
1383 param
= get_param_num(expr
);
1387 var
= expr_to_var(expr
);
1390 snprintf(buf
, sizeof(buf
), "%s orig", var
);
1391 comparison
= get_comparison_strings(var
, buf
);
1397 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(comparison
), param
);
1398 return alloc_sname(buf
);
1401 static char *get_return_compare_str(struct expression
*expr
)
1405 compare_str
= get_return_compare_is_param(expr
);
1409 compare_str
= expr_lte_to_param(expr
, -1);
1413 return expr_param_comparison(expr
, -1);
1416 static const char *get_return_ranges_str(struct expression
*expr
, struct range_list
**rl_p
)
1418 struct range_list
*rl
;
1419 char *return_ranges
;
1429 return alloc_sname("");
1431 if (get_implied_value(expr
, &sval
)) {
1432 sval
= sval_cast(cur_func_return_type(), sval
);
1433 *rl_p
= alloc_rl(sval
, sval
);
1434 return sval_to_str_or_err_ptr(sval
);
1437 fn_param_str
= get_fn_param_str(expr
);
1438 compare_str
= expr_equal_to_param(expr
, -1);
1439 math_str
= get_value_in_terms_of_parameter_math(expr
);
1441 if (get_implied_rl(expr
, &rl
) && !is_whole_rl(rl
)) {
1442 rl
= cast_rl(cur_func_return_type(), rl
);
1443 return_ranges
= show_rl(rl
);
1444 } else if (get_imaginary_absolute(expr
, &rl
)){
1445 rl
= cast_rl(cur_func_return_type(), rl
);
1446 return alloc_sname(show_rl(rl
));
1448 get_absolute_rl(expr
, &rl
);
1449 rl
= cast_rl(cur_func_return_type(), rl
);
1450 return_ranges
= show_rl(rl
);
1455 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, fn_param_str
);
1456 return alloc_sname(buf
);
1459 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1460 return alloc_sname(buf
);
1463 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1464 return alloc_sname(buf
);
1466 compare_str
= get_return_compare_str(expr
);
1468 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1469 return alloc_sname(buf
);
1472 return return_ranges
;
1475 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
1477 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", function_signature());
1480 static bool call_return_state_hooks_conditional(struct expression
*expr
)
1482 int final_pass_orig
= final_pass
;
1488 (expr
->type
!= EXPR_CONDITIONAL
&& expr
->type
!= EXPR_SELECT
))
1493 __push_fake_cur_stree();
1496 __split_whole_condition(expr
->conditional
);
1497 final_pass
= final_pass_orig
;
1499 call_return_state_hooks(expr
->cond_true
?: expr
->conditional
);
1501 __push_true_states();
1502 __use_false_states();
1504 call_return_state_hooks(expr
->cond_false
);
1506 __merge_true_states();
1507 __free_fake_cur_stree();
1513 static void call_return_state_hooks_compare(struct expression
*expr
)
1515 struct returned_state_callback
*cb
;
1516 char *return_ranges
;
1517 int final_pass_orig
= final_pass
;
1518 sval_t sval
= { .type
= &int_ctype
};
1521 if (!get_implied_value(expr
, &ret
))
1524 __push_fake_cur_stree();
1527 __split_whole_condition(expr
);
1528 final_pass
= final_pass_orig
;
1530 if (ret
.value
!= 0) {
1531 return_ranges
= alloc_sname("1");
1533 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1536 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1537 cb
->callback(return_id
, return_ranges
, expr
);
1538 } END_FOR_EACH_PTR(cb
);
1541 __push_true_states();
1542 __use_false_states();
1544 if (ret
.value
!= 1) {
1545 return_ranges
= alloc_sname("0");
1547 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1550 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1551 cb
->callback(return_id
, return_ranges
, expr
);
1552 } END_FOR_EACH_PTR(cb
);
1555 __merge_true_states();
1556 __free_fake_cur_stree();
1559 static int ptr_in_list(struct sm_state
*sm
, struct state_list
*slist
)
1561 struct sm_state
*tmp
;
1563 FOR_EACH_PTR(slist
, tmp
) {
1564 if (strcmp(tmp
->state
->name
, sm
->state
->name
) == 0)
1566 } END_FOR_EACH_PTR(tmp
);
1571 static int split_possible_helper(struct sm_state
*sm
, struct expression
*expr
)
1573 struct returned_state_callback
*cb
;
1574 struct range_list
*rl
;
1575 char *return_ranges
;
1576 struct sm_state
*tmp
;
1578 int nr_possible
, nr_states
;
1581 struct state_list
*already_handled
= NULL
;
1584 if (!sm
|| !sm
->merged
)
1587 if (too_many_possible(sm
))
1590 /* bail if it gets too complicated */
1592 FOR_EACH_PTR(sm
->possible
, tmp
) {
1595 if (ptr_in_list(tmp
, already_handled
))
1597 add_ptr_list(&already_handled
, tmp
);
1599 } END_FOR_EACH_PTR(tmp
);
1600 free_slist(&already_handled
);
1601 nr_states
= get_db_state_count();
1602 if (nr_states
* nr_possible
>= 2000)
1605 FOR_EACH_PTR(sm
->possible
, tmp
) {
1608 if (ptr_in_list(tmp
, already_handled
))
1610 add_ptr_list(&already_handled
, tmp
);
1613 __push_fake_cur_stree();
1615 overwrite_states_using_pool(sm
, tmp
);
1617 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
1618 return_ranges
= show_rl(rl
);
1619 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(clone_rl(rl
)));
1620 if (!rl_to_sval(rl
, &sval
)) {
1621 compare_str
= get_return_compare_str(expr
);
1623 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1624 return_ranges
= alloc_sname(buf
);
1629 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1630 cb
->callback(return_id
, return_ranges
, expr
);
1631 } END_FOR_EACH_PTR(cb
);
1633 __free_fake_cur_stree();
1634 } END_FOR_EACH_PTR(tmp
);
1636 free_slist(&already_handled
);
1641 static int call_return_state_hooks_split_possible(struct expression
*expr
)
1643 struct sm_state
*sm
;
1645 if (!expr
|| expr_equal_to_param(expr
, -1))
1648 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1649 return split_possible_helper(sm
, expr
);
1652 static bool has_possible_negative(struct sm_state
*sm
)
1654 struct sm_state
*tmp
;
1656 if (!type_signed(estate_type(sm
->state
)))
1659 FOR_EACH_PTR(sm
->possible
, tmp
) {
1660 if (!estate_rl(tmp
->state
))
1662 if (sval_is_negative(estate_min(tmp
->state
)) &&
1663 sval_is_negative(estate_max(tmp
->state
)))
1665 } END_FOR_EACH_PTR(tmp
);
1670 static bool has_separate_zero_null(struct sm_state
*sm
)
1672 struct sm_state
*tmp
;
1675 FOR_EACH_PTR(sm
->possible
, tmp
) {
1676 if (!estate_get_single_value(tmp
->state
, &sval
))
1678 if (sval
.value
== 0)
1680 } END_FOR_EACH_PTR(tmp
);
1685 static int split_positive_from_negative(struct expression
*expr
)
1687 struct sm_state
*sm
;
1688 struct returned_state_callback
*cb
;
1689 struct range_list
*rl
;
1690 const char *return_ranges
;
1691 struct range_list
*ret_rl
;
1695 /* We're going to print the states 3 times */
1696 if (get_db_state_count() > 10000 / 3)
1699 if (!get_implied_rl(expr
, &rl
) || !rl
)
1701 /* Forget about INT_MAX and larger */
1702 if (rl_max(rl
).value
<= 0)
1704 if (!sval_is_negative(rl_min(rl
)))
1707 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1710 if (!has_possible_negative(sm
))
1712 separate_zero
= has_separate_zero_null(sm
);
1714 if (!assume(compare_expression(expr
, separate_zero
? '>' : SPECIAL_GTE
, zero_expr())))
1718 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1719 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1720 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1721 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1722 } END_FOR_EACH_PTR(cb
);
1726 if (separate_zero
) {
1727 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
1730 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1731 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1732 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1733 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1734 } END_FOR_EACH_PTR(cb
);
1740 undo
= assume(compare_expression(expr
, '<', zero_expr()));
1743 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1744 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1745 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1746 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1747 } END_FOR_EACH_PTR(cb
);
1755 static int call_return_state_hooks_split_null_non_null_zero(struct expression
*expr
)
1757 struct returned_state_callback
*cb
;
1758 struct range_list
*rl
;
1759 struct range_list
*nonnull_rl
;
1761 struct range_list
*null_rl
= NULL
;
1762 char *return_ranges
;
1763 struct sm_state
*sm
;
1764 struct smatch_state
*state
;
1766 int final_pass_orig
= final_pass
;
1768 if (!expr
|| expr_equal_to_param(expr
, -1))
1770 if (expr
->type
== EXPR_CALL
)
1773 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1776 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
1779 if (!estate_rl(state
))
1781 if (estate_min(state
).value
== 0 && estate_max(state
).value
== 0)
1783 if (has_possible_negative(sm
))
1785 if (!has_separate_zero_null(sm
))
1788 nr_states
= get_db_state_count();
1789 if (option_info
&& nr_states
>= 1500)
1792 rl
= estate_rl(state
);
1794 __push_fake_cur_stree();
1797 __split_whole_condition(expr
);
1798 final_pass
= final_pass_orig
;
1800 nonnull_rl
= rl_filter(rl
, rl_zero());
1801 return_ranges
= show_rl(nonnull_rl
);
1802 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonnull_rl
));
1805 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1806 cb
->callback(return_id
, return_ranges
, expr
);
1807 } END_FOR_EACH_PTR(cb
);
1809 __push_true_states();
1810 __use_false_states();
1812 return_ranges
= alloc_sname("0");
1813 null_sval
= sval_type_val(rl_type(rl
), 0);
1814 add_range(&null_rl
, null_sval
, null_sval
);
1815 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(null_rl
));
1817 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1818 cb
->callback(return_id
, return_ranges
, expr
);
1819 } END_FOR_EACH_PTR(cb
);
1821 __merge_true_states();
1822 __free_fake_cur_stree();
1827 static bool is_kernel_success_fail(struct sm_state
*sm
)
1829 struct sm_state
*tmp
;
1830 struct range_list
*rl
;
1831 bool has_zero
= false;
1832 bool has_neg
= false;
1834 if (!type_signed(estate_type(sm
->state
)))
1837 FOR_EACH_PTR(sm
->possible
, tmp
) {
1838 rl
= estate_rl(tmp
->state
);
1841 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 0) {
1846 if (rl_min(rl
).value
>= -4095 && rl_max(rl
).value
< 0)
1848 if (strcmp(tmp
->state
->name
, "s32min-(-1)") == 0)
1850 if (strcmp(tmp
->state
->name
, "s32min-(-1),1-s32max") == 0)
1853 } END_FOR_EACH_PTR(tmp
);
1855 return has_zero
&& has_neg
;
1858 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
1860 struct sm_state
*sm
;
1861 struct range_list
*rl
;
1862 struct range_list
*nonzero_rl
;
1864 struct range_list
*zero_rl
= NULL
;
1866 struct returned_state_callback
*cb
;
1867 char *return_ranges
;
1868 int final_pass_orig
= final_pass
;
1870 if (option_project
!= PROJ_KERNEL
)
1873 nr_states
= get_db_state_count();
1874 if (nr_states
> 2000)
1877 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1880 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
1882 if (!is_kernel_success_fail(sm
))
1885 rl
= estate_rl(sm
->state
);
1889 __push_fake_cur_stree();
1892 __split_whole_condition(expr
);
1893 final_pass
= final_pass_orig
;
1895 nonzero_rl
= rl_filter(rl
, rl_zero());
1896 nonzero_rl
= cast_rl(cur_func_return_type(), nonzero_rl
);
1897 return_ranges
= show_rl(nonzero_rl
);
1898 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonzero_rl
));
1901 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1902 cb
->callback(return_id
, return_ranges
, expr
);
1903 } END_FOR_EACH_PTR(cb
);
1905 __push_true_states();
1906 __use_false_states();
1908 return_ranges
= alloc_sname("0");
1909 zero_sval
= sval_type_val(rl_type(rl
), 0);
1910 add_range(&zero_rl
, zero_sval
, zero_sval
);
1911 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(zero_rl
));
1913 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1914 cb
->callback(return_id
, return_ranges
, expr
);
1915 } END_FOR_EACH_PTR(cb
);
1917 __merge_true_states();
1918 __free_fake_cur_stree();
1923 static int is_boolean(struct expression
*expr
)
1925 struct range_list
*rl
;
1927 if (!get_implied_rl(expr
, &rl
))
1929 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
1934 static int splitable_function_call(struct expression
*expr
)
1936 struct sm_state
*sm
;
1939 if (!expr
|| expr
->type
!= EXPR_CALL
)
1941 snprintf(buf
, sizeof(buf
), "return %p", expr
);
1942 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
1943 return split_possible_helper(sm
, expr
);
1946 static struct sm_state
*find_bool_param(void)
1948 struct stree
*start_states
;
1950 struct sm_state
*sm
, *tmp
;
1953 start_states
= get_start_states();
1955 FOR_EACH_PTR_REVERSE(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
1958 sm
= get_sm_state_stree(start_states
, SMATCH_EXTRA
, arg
->ident
->name
, arg
);
1961 if (rl_min(estate_rl(sm
->state
)).value
!= 0 ||
1962 rl_max(estate_rl(sm
->state
)).value
!= 1)
1965 } END_FOR_EACH_PTR_REVERSE(arg
);
1971 * Check if it's splitable. If not, then splitting it up is likely not
1972 * useful for the callers.
1974 FOR_EACH_PTR(sm
->possible
, tmp
) {
1977 if (!estate_get_single_value(tmp
->state
, &sval
))
1979 } END_FOR_EACH_PTR(tmp
);
1984 static int split_on_bool_sm(struct sm_state
*sm
, struct expression
*expr
)
1986 struct returned_state_callback
*cb
;
1987 struct range_list
*ret_rl
;
1988 const char *return_ranges
;
1989 struct sm_state
*tmp
;
1991 struct state_list
*already_handled
= NULL
;
1993 if (!sm
|| !sm
->merged
)
1996 if (too_many_possible(sm
))
1999 FOR_EACH_PTR(sm
->possible
, tmp
) {
2002 if (ptr_in_list(tmp
, already_handled
))
2004 add_ptr_list(&already_handled
, tmp
);
2007 __push_fake_cur_stree();
2009 overwrite_states_using_pool(sm
, tmp
);
2011 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
2012 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
2014 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
2015 cb
->callback(return_id
, (char *)return_ranges
, expr
);
2016 } END_FOR_EACH_PTR(cb
);
2018 __free_fake_cur_stree();
2019 } END_FOR_EACH_PTR(tmp
);
2021 free_slist(&already_handled
);
2026 static int split_by_bool_param(struct expression
*expr
)
2028 struct sm_state
*start_sm
, *sm
;
2031 start_sm
= find_bool_param();
2034 sm
= get_sm_state(SMATCH_EXTRA
, start_sm
->name
, start_sm
->sym
);
2035 if (!sm
|| estate_get_single_value(sm
->state
, &sval
))
2038 if (get_db_state_count() * 2 >= 2000)
2041 return split_on_bool_sm(sm
, expr
);
2044 static int split_by_null_nonnull_param(struct expression
*expr
)
2047 struct sm_state
*sm
;
2050 /* function must only take one pointer */
2051 if (ptr_list_size((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
) != 1)
2053 arg
= first_ptr_list((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
);
2056 if (get_real_base_type(arg
)->type
!= SYM_PTR
)
2059 if (param_was_set_var_sym(arg
->ident
->name
, arg
))
2061 sm
= get_sm_state(SMATCH_EXTRA
, arg
->ident
->name
, arg
);
2065 if (!has_separate_zero_null(sm
))
2068 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
2069 if (get_db_state_count() * nr_possible
>= 2000)
2072 return split_on_bool_sm(sm
, expr
);
2075 struct expression
*strip_expr_statement(struct expression
*expr
)
2077 struct expression
*orig
= expr
;
2078 struct statement
*stmt
, *last_stmt
;
2082 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '(')
2084 if (expr
->type
!= EXPR_STATEMENT
)
2086 stmt
= expr
->statement
;
2087 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
2090 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
2091 if (!last_stmt
|| last_stmt
->type
== STMT_LABEL
)
2092 last_stmt
= last_stmt
->label_statement
;
2093 if (!last_stmt
|| last_stmt
->type
!= STMT_EXPRESSION
)
2095 return strip_expr(last_stmt
->expression
);
2098 static bool is_kernel_error_path(struct expression
*expr
)
2100 struct range_list
*rl
;
2103 * Splitting up returns requires resources. It also requires resources
2104 * for the caller. It doesn't seem worth it to split anything up.
2106 if (!get_implied_rl(expr
, &rl
))
2108 if (rl_type(rl
) != &int_ctype
)
2110 if (rl_min(rl
).value
>= -4095 &&
2111 rl_max(rl
).value
< 0)
2116 static void call_return_state_hooks(struct expression
*expr
)
2118 struct returned_state_callback
*cb
;
2119 struct range_list
*ret_rl
;
2120 const char *return_ranges
;
2124 if (__path_is_null())
2127 expr
= strip_expr(expr
);
2128 expr
= strip_expr_statement(expr
);
2130 if (is_impossible_path())
2133 if (expr
&& (expr
->type
== EXPR_COMPARE
||
2134 !get_implied_value(expr
, &sval
)) &&
2135 (is_condition(expr
) || is_boolean(expr
))) {
2136 call_return_state_hooks_compare(expr
);
2138 } else if (call_return_state_hooks_conditional(expr
)) {
2140 } else if (is_kernel_error_path(expr
)) {
2142 } else if (call_return_state_hooks_split_possible(expr
)) {
2144 } else if (split_positive_from_negative(expr
)) {
2146 } else if (call_return_state_hooks_split_null_non_null_zero(expr
)) {
2148 } else if (call_return_state_hooks_split_success_fail(expr
)) {
2150 } else if (splitable_function_call(expr
)) {
2152 } else if (split_by_bool_param(expr
)) {
2153 } else if (split_by_null_nonnull_param(expr
)) {
2158 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
2159 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
2162 nr_states
= get_db_state_count();
2163 if (nr_states
>= 10000) {
2164 match_return_info(return_id
, (char *)return_ranges
, expr
);
2165 print_limited_param_set(return_id
, (char *)return_ranges
, expr
);
2166 mark_all_params_untracked(return_id
, (char *)return_ranges
, expr
);
2169 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
2170 cb
->callback(return_id
, (char *)return_ranges
, expr
);
2171 } END_FOR_EACH_PTR(cb
);
2174 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
2176 struct returned_member_callback
*cb
;
2177 struct stree
*stree
;
2178 struct sm_state
*sm
;
2179 struct symbol
*type
;
2181 char member_name
[256];
2184 type
= get_type(expr
);
2185 if (!type
|| type
->type
!= SYM_PTR
)
2187 name
= expr_to_var(expr
);
2191 member_name
[sizeof(member_name
) - 1] = '\0';
2192 strcpy(member_name
, "$");
2195 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
2196 stree
= __get_cur_stree();
2197 FOR_EACH_MY_SM(cb
->owner
, stree
, sm
) {
2198 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
2199 strcpy(member_name
, "*$");
2200 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
2203 if (strncmp(sm
->name
, name
, len
) != 0)
2205 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
2207 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
2208 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
2209 } END_FOR_EACH_SM(sm
);
2210 } END_FOR_EACH_PTR(cb
);
2215 static void reset_memdb(struct symbol
*sym
)
2217 mem_sql(NULL
, NULL
, "delete from caller_info;");
2218 mem_sql(NULL
, NULL
, "delete from return_states;");
2219 mem_sql(NULL
, NULL
, "delete from call_implies;");
2220 mem_sql(NULL
, NULL
, "delete from return_implies;");
2223 static void match_end_func_info(struct symbol
*sym
)
2225 if (__path_is_null())
2227 call_return_state_hooks(NULL
);
2230 static void match_after_func(struct symbol
*sym
)
2236 static void init_memdb(void)
2240 const char *schema_files
[] = {
2242 "db/caller_info.schema",
2243 "db/common_caller_info.schema",
2244 "db/return_states.schema",
2245 "db/function_type_size.schema",
2246 "db/type_size.schema",
2247 "db/function_type_info.schema",
2248 "db/type_info.schema",
2249 "db/call_implies.schema",
2250 "db/return_implies.schema",
2251 "db/function_ptr.schema",
2252 "db/local_values.schema",
2253 "db/function_type_value.schema",
2254 "db/type_value.schema",
2255 "db/function_type.schema",
2256 "db/data_info.schema",
2257 "db/parameter_name.schema",
2258 "db/constraints.schema",
2259 "db/constraints_required.schema",
2260 "db/fn_ptr_data_link.schema",
2261 "db/fn_data_link.schema",
2262 "db/mtag_about.schema",
2263 "db/mtag_info.schema",
2264 "db/mtag_map.schema",
2265 "db/mtag_data.schema",
2266 "db/mtag_alias.schema",
2268 static char buf
[4096];
2273 rc
= sqlite3_open(":memory:", &mem_db
);
2274 if (rc
!= SQLITE_OK
) {
2275 sm_ierror("starting In-Memory database.");
2279 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
2280 fd
= open_schema_file(schema_files
[i
]);
2283 ret
= read(fd
, buf
, sizeof(buf
));
2285 sm_ierror("failed to read: %s", schema_files
[i
]);
2289 if (ret
== sizeof(buf
)) {
2290 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2291 schema_files
[i
], sizeof(buf
));
2295 rc
= sqlite3_exec(mem_db
, buf
, NULL
, NULL
, &err
);
2296 if (rc
!= SQLITE_OK
) {
2297 sm_ierror("SQL error #2: %s", err
);
2298 sm_ierror("%s", buf
);
2303 static void init_cachedb(void)
2307 const char *schema_files
[] = {
2308 "db/call_implies.schema",
2309 "db/return_implies.schema",
2310 "db/type_info.schema",
2311 "db/mtag_about.schema",
2312 "db/mtag_data.schema",
2313 "db/mtag_info.schema",
2314 "db/sink_info.schema",
2316 static char buf
[4096];
2321 rc
= sqlite3_open(":memory:", &cache_db
);
2322 if (rc
!= SQLITE_OK
) {
2323 sm_ierror("starting In-Memory database.");
2327 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
2328 fd
= open_schema_file(schema_files
[i
]);
2331 ret
= read(fd
, buf
, sizeof(buf
));
2333 sm_ierror("failed to read: %s", schema_files
[i
]);
2337 if (ret
== sizeof(buf
)) {
2338 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2339 schema_files
[i
], sizeof(buf
));
2343 rc
= sqlite3_exec(cache_db
, buf
, NULL
, NULL
, &err
);
2344 if (rc
!= SQLITE_OK
) {
2345 sm_ierror("SQL error #2: %s", err
);
2346 sm_ierror("%s", buf
);
2351 static int save_cache_data(void *_table
, int argc
, char **argv
, char **azColName
)
2353 static char buf
[4096];
2356 char *table
= _table
;
2360 p
+= snprintf(p
, 4096 - (p
- buf
), "insert or ignore into %s values (", table
);
2361 for (i
= 0; i
< argc
; i
++) {
2363 p
+= snprintf(p
, 4096 - (p
- buf
), ", ");
2364 sqlite3_snprintf(sizeof(tmp
), tmp
, "%q", escape_newlines(argv
[i
]));
2365 p
+= snprintf(p
, 4096 - (p
- buf
), "'%s'", tmp
);
2368 p
+= snprintf(p
, 4096 - (p
- buf
), ");");
2372 sm_msg("SQL: %s", buf
);
2376 static void dump_cache(struct symbol_list
*sym_list
)
2378 const char *cache_tables
[] = {
2379 "type_info", "return_implies", "call_implies", "mtag_data",
2380 "mtag_info", "mtag_about", "sink_info",
2388 for (i
= 0; i
< ARRAY_SIZE(cache_tables
); i
++) {
2389 snprintf(buf
, sizeof(buf
), "select * from %s;", cache_tables
[i
]);
2390 cache_sql(&save_cache_data
, (char *)cache_tables
[i
], buf
);
2394 void open_smatch_db(char *db_file
)
2401 use_states
= malloc(num_checks
+ 1);
2402 memset(use_states
, 0xff, num_checks
+ 1);
2407 rc
= sqlite3_open_v2(db_file
, &smatch_db
, SQLITE_OPEN_READONLY
, NULL
);
2408 if (rc
!= SQLITE_OK
) {
2413 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES
);
2417 static void register_common_funcs(void)
2419 struct token
*token
;
2423 if (option_project
== PROJ_NONE
)
2424 strcpy(filename
, "common_functions");
2426 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
2428 token
= get_tokens_file(filename
);
2431 if (token_type(token
) != TOKEN_STREAMBEGIN
)
2433 token
= token
->next
;
2434 while (token_type(token
) != TOKEN_STREAMEND
) {
2435 if (token_type(token
) != TOKEN_IDENT
)
2437 func
= alloc_string(show_ident(token
->ident
));
2438 add_ptr_list(&common_funcs
, func
);
2439 token
= token
->next
;
2441 clear_token_alloc();
2444 static char *get_next_string(char **str
)
2446 static char string
[256];
2455 while (*p
!= '\0' && *p
!= '\n') {
2456 if (*p
== '\\' && *(p
+ 1) == ' ') {
2466 if (len
>= sizeof(string
)) {
2467 memcpy(string
, start
, sizeof(string
));
2468 string
[sizeof(string
) - 1] = '\0';
2469 sm_ierror("return_fix: '%s' too long", string
);
2473 memcpy(string
, start
, len
);
2475 for (i
= 0; i
< sizeof(string
) - 1; i
++) {
2476 if (string
[i
] == '\\' && string
[i
+ 1] == ' ') {
2477 for (j
= i
; string
[j
] != '\0'; j
++)
2478 string
[j
] = string
[j
+ 1];
2487 static void register_return_replacements(void)
2489 char *func
, *orig
, *new;
2495 snprintf(filename
, 256, "db/%s.return_fixes", option_project_str
);
2496 fd
= open_schema_file(filename
);
2499 ret
= read(fd
, buf
, sizeof(buf
));
2503 if (ret
== sizeof(buf
)) {
2504 sm_ierror("file too large: %s (limit %zd bytes)",
2505 filename
, sizeof(buf
));
2512 get_next_string(&p
);
2515 if (replace_count
== 0 || replace_count
% 3 != 0) {
2519 replace_table
= malloc(replace_count
* sizeof(char *));
2524 func
= alloc_string(get_next_string(&p
));
2525 orig
= alloc_string(get_next_string(&p
));
2526 new = alloc_string(get_next_string(&p
));
2528 replace_table
[i
++] = func
;
2529 replace_table
[i
++] = orig
;
2530 replace_table
[i
++] = new;
2534 void register_definition_db_callbacks(int id
)
2536 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2537 add_hook(&match_call_info_new
, FUNCTION_CALL_HOOK
);
2538 add_split_return_callback(match_return_info
);
2539 add_split_return_callback(print_returned_struct_members
);
2540 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
2541 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
2542 add_hook(&match_after_func
, AFTER_FUNC_HOOK
);
2544 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
2545 add_hook(&match_call_implies
, FUNC_DEF_HOOK
);
2546 add_hook(&match_return_implies
, CALL_HOOK_AFTER_INLINE
);
2548 register_common_funcs();
2549 register_return_replacements();
2551 add_hook(&dump_cache
, END_FILE_HOOK
);
2554 void register_db_call_marker(int id
)
2556 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
2559 char *return_state_to_var_sym(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
)
2561 struct expression
*arg
;
2563 char member_name
[256];
2568 const char *star
= "";
2570 if (expr
->type
!= EXPR_ASSIGNMENT
)
2572 if (get_type(expr
->left
) == &int_ctype
&& strcmp(key
, "$") != 0)
2574 name
= expr_to_var_sym(expr
->left
, sym
);
2577 if (key
[0] == '*') {
2581 if (strncmp(key
, "$", 1) != 0)
2583 snprintf(member_name
, sizeof(member_name
), "%s%s%s", star
, name
, key
+ 1);
2585 return alloc_string(member_name
);
2588 while (expr
->type
== EXPR_ASSIGNMENT
)
2589 expr
= strip_expr(expr
->right
);
2590 if (expr
->type
!= EXPR_CALL
)
2593 arg
= get_argument_from_call_expr(expr
->args
, param
);
2597 return get_variable_from_key(arg
, key
, sym
);
2600 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
)
2602 struct symbol
*type
;
2606 bool add_dot
= false;
2611 arg
= strip_expr(arg
);
2613 if (strcmp(key
, "$") == 0)
2614 return expr_to_var_sym(arg
, sym
);
2616 if (strcmp(key
, "*$") == 0) {
2617 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
2618 arg
= strip_expr(arg
->unop
);
2619 return expr_to_var_sym(arg
, sym
);
2621 tmp
= expr_to_var_sym(arg
, sym
);
2624 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
2626 return alloc_string(buf
);
2630 if (strncmp(key
, "(*$)", 4) == 0) {
2633 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
2634 arg
= strip_expr(arg
->unop
);
2635 snprintf(buf
, sizeof(buf
), "$%s", key
+ 4);
2636 return get_variable_from_key(arg
, buf
, sym
);
2638 tmp
= expr_to_var_sym(arg
, sym
);
2641 snprintf(buf
, sizeof(buf
), "(*%s)%s", tmp
, key
+ 4);
2643 return alloc_string(buf
);
2647 while (key
[0] == '*') {
2653 * FIXME: This is a hack.
2654 * We should be able to parse expressions like (*$)->foo and *$->foo.
2656 type
= get_type(arg
);
2657 if (is_struct_ptr(type
))
2660 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&' && star_cnt
&& !add_dot
) {
2661 arg
= strip_expr(arg
->unop
);
2665 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
2666 arg
= strip_expr(arg
->unop
);
2667 tmp
= expr_to_var_sym(arg
, sym
);
2670 snprintf(buf
, sizeof(buf
), "%.*s%s.%s",
2671 star_cnt
, "**********", tmp
, key
+ 3);
2672 return alloc_string(buf
);
2675 tmp
= expr_to_var_sym(arg
, sym
);
2678 snprintf(buf
, sizeof(buf
), "%.*s%s%s", star_cnt
, "**********", tmp
, key
+ 1);
2680 return alloc_string(buf
);
2683 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
)
2687 if (strcmp("$", key
) == 0)
2688 return expr_to_chunk_sym_vsl(arg
, sym
, vsl
);
2689 return get_variable_from_key(arg
, key
, sym
);
2692 const char *state_name_to_param_name(const char *state_name
, const char *param_name
)
2698 name_len
= strlen(param_name
);
2700 while (state_name
[0] == '*') {
2705 /* ten out of ten stars! */
2709 if (strncmp(state_name
, "(*", 2) == 0 &&
2710 strncmp(state_name
+ 2, param_name
, name_len
) == 0 &&
2711 state_name
[name_len
+ 2] == ')') {
2712 snprintf(buf
, sizeof(buf
), "%.*s(*$)%s", star_cnt
, "**********",
2713 state_name
+ name_len
+ 3);
2714 return alloc_sname(buf
);
2717 if (strcmp(state_name
, param_name
) == 0) {
2718 snprintf(buf
, sizeof(buf
), "%.*s$", star_cnt
, "**********");
2719 return alloc_sname(buf
);
2722 if (state_name
[name_len
] == '-' && /* check for '-' from "->" */
2723 strncmp(state_name
, param_name
, name_len
) == 0) {
2724 snprintf(buf
, sizeof(buf
), "%.*s$%s", star_cnt
, "**********", state_name
+ name_len
);
2725 return alloc_sname(buf
);
2730 const char *get_param_name_var_sym(const char *name
, struct symbol
*sym
)
2732 if (!sym
|| !sym
->ident
)
2735 return state_name_to_param_name(name
, sym
->ident
->name
);
2738 const char *get_mtag_name_var_sym(const char *state_name
, struct symbol
*sym
)
2740 struct symbol
*type
;
2741 const char *sym_name
;
2743 static char buf
[256];
2746 * mtag_name is different from param_name because mtags can be a struct
2747 * instead of a struct pointer. But we want to treat it like a pointer
2748 * because really an mtag is a pointer. Or in other words, if you pass
2749 * a struct foo then you want to talk about foo.bar but with an mtag
2750 * you want to refer to it as foo->bar.
2754 if (!sym
|| !sym
->ident
)
2757 type
= get_real_base_type(sym
);
2758 if (type
&& type
->type
== SYM_BASETYPE
)
2761 sym_name
= sym
->ident
->name
;
2762 name_len
= strlen(sym_name
);
2764 if (state_name
[name_len
] == '.' && /* check for '-' from "->" */
2765 strncmp(state_name
, sym_name
, name_len
) == 0) {
2766 snprintf(buf
, sizeof(buf
), "$->%s", state_name
+ name_len
+ 1);
2770 return state_name_to_param_name(state_name
, sym_name
);
2773 const char *get_mtag_name_expr(struct expression
*expr
)
2777 const char *ret
= NULL
;
2779 name
= expr_to_var_sym(expr
, &sym
);
2783 ret
= get_mtag_name_var_sym(name
, sym
);
2789 const char *get_param_name(struct sm_state
*sm
)
2791 return get_param_name_var_sym(sm
->name
, sm
->sym
);
2794 char *get_data_info_name(struct expression
*expr
)
2801 expr
= strip_expr(expr
);
2802 name
= get_member_name(expr
);
2805 name
= expr_to_var_sym(expr
, &sym
);
2808 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
2810 if (sym
->ctype
.modifiers
& MOD_STATIC
)
2811 snprintf(buf
, sizeof(buf
), "static %s", name
);
2813 snprintf(buf
, sizeof(buf
), "global %s", name
);
2814 ret
= alloc_sname(buf
);