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
;
37 static void call_return_state_hooks(struct expression
*expr
);
39 #define SQLITE_CACHE_PAGES 1000
43 void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
);
45 ALLOCATOR(def_callback
, "definition db hook callbacks");
46 DECLARE_PTR_LIST(callback_list
, struct def_callback
);
47 static struct callback_list
*select_caller_info_callbacks
;
49 struct member_info_callback
{
51 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
);
53 ALLOCATOR(member_info_callback
, "caller_info callbacks");
54 DECLARE_PTR_LIST(member_info_cb_list
, struct member_info_callback
);
55 static struct member_info_cb_list
*member_callbacks
;
56 static struct member_info_cb_list
*member_callbacks_new
;
58 struct return_info_callback
{
60 void (*callback
)(int return_id
, char *return_ranges
,
61 struct expression
*returned_expr
,
63 const char *printed_name
,
66 ALLOCATOR(return_info_callback
, "return_info callbacks");
67 DECLARE_PTR_LIST(return_info_cb_list
, struct return_info_callback
);
68 static struct return_info_cb_list
*return_callbacks
;
70 struct returned_state_callback
{
71 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*return_expr
);
73 ALLOCATOR(returned_state_callback
, "returned state callbacks");
74 DECLARE_PTR_LIST(returned_state_cb_list
, struct returned_state_callback
);
75 static struct returned_state_cb_list
*returned_state_callbacks
;
77 struct returned_member_callback
{
79 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*expr
, char *printed_name
, struct smatch_state
*state
);
81 ALLOCATOR(returned_member_callback
, "returned member callbacks");
82 DECLARE_PTR_LIST(returned_member_cb_list
, struct returned_member_callback
);
83 static struct returned_member_cb_list
*returned_member_callbacks
;
85 struct db_implies_callback
{
87 void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
);
89 ALLOCATOR(db_implies_callback
, "return_implies callbacks");
90 DECLARE_PTR_LIST(db_implies_cb_list
, struct db_implies_callback
);
91 static struct db_implies_cb_list
*return_implies_cb_list
;
92 static struct db_implies_cb_list
*call_implies_cb_list
;
94 /* silently truncates if needed. */
95 char *escape_newlines(const char *str
)
101 for (i
= 0, j
= 0; str
[i
] != '\0' && j
!= sizeof(buf
); i
++, j
++) {
102 if (str
[i
] != '\r' && str
[i
] != '\n') {
109 if (j
== sizeof(buf
))
115 return alloc_sname(str
);
117 if (j
== sizeof(buf
))
119 return alloc_sname(buf
);
122 static int print_sql_output(void *unused
, int argc
, char **argv
, char **azColName
)
126 for (i
= 0; i
< argc
; i
++) {
129 sm_printf("%s", argv
[i
]);
135 void sql_exec(struct sqlite3
*db
, int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
)
143 if (option_debug
|| debug_db
) {
145 if (strncasecmp(sql
, "select", strlen("select")) == 0)
146 sqlite3_exec(db
, sql
, print_sql_output
, NULL
, NULL
);
149 rc
= sqlite3_exec(db
, sql
, callback
, data
, &err
);
150 if (rc
!= SQLITE_OK
&& !parse_error
) {
151 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err
);
152 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql
);
157 static int replace_count
;
158 static char **replace_table
;
159 static const char *replace_return_ranges(const char *return_ranges
)
163 if (!get_function()) {
164 /* I have no idea why EXPORT_SYMBOL() is here */
165 return return_ranges
;
167 for (i
= 0; i
< replace_count
; i
+= 3) {
168 if (strcmp(replace_table
[i
+ 0], get_function()) == 0) {
169 if (strcmp(replace_table
[i
+ 1], return_ranges
) == 0)
170 return replace_table
[i
+ 2];
173 return return_ranges
;
177 static char *use_states
;
178 static int get_db_state_count(void)
183 FOR_EACH_SM(__get_cur_stree(), sm
) {
184 if (sm
->owner
== USHRT_MAX
)
186 if (use_states
[sm
->owner
])
188 } END_FOR_EACH_SM(sm
);
192 static bool is_local(struct symbol
*sym
)
194 if (sym
->ctype
.modifiers
& MOD_STATIC
)
196 if ((sym
->ctype
.modifiers
& MOD_EXTERN
) &&
197 (sym
->ctype
.modifiers
& MOD_INLINE
))
200 if (!sym
->definition
)
203 if ((sym
->definition
->ctype
.modifiers
& MOD_EXTERN
) &&
204 (sym
->definition
->ctype
.modifiers
& MOD_INLINE
))
210 void db_ignore_states(int id
)
215 unsigned long long __fn_mtag
;
216 static void set_fn_mtag(struct symbol
*sym
)
220 if (is_local(cur_func_sym
))
221 snprintf(buf
, sizeof(buf
), "%s %s", get_base_file(), get_function());
223 snprintf(buf
, sizeof(buf
), "extern %s", get_function());
225 __fn_mtag
= str_to_mtag(buf
);
228 void sql_insert_return_states(int return_id
, const char *return_ranges
,
229 int type
, int param
, const char *key
, const char *value
)
231 unsigned long long id
;
234 if (key
&& strlen(key
) >= 80)
237 id
= (unsigned long)__inline_fn
;
241 return_ranges
= replace_return_ranges(return_ranges
);
242 sql_insert(return_states
, "'%s', '%s', %llu, %d, '%s', %d, %d, %d, '%s', '%s'",
243 get_base_file(), get_function(), id
, return_id
,
244 return_ranges
, fn_static(), type
, param
, key
, value
);
247 static struct string_list
*common_funcs
;
248 static int is_common_function(const char *fn
)
255 if (strncmp(fn
, "__builtin_", 10) == 0)
258 FOR_EACH_PTR(common_funcs
, tmp
) {
259 if (strcmp(tmp
, fn
) == 0)
261 } END_FOR_EACH_PTR(tmp
);
266 static char *function_signature(void)
268 return type_to_str(get_real_base_type(cur_func_sym
));
271 void sql_insert_caller_info(struct expression
*call
, int type
,
272 int param
, const char *key
, const char *value
)
274 FILE *tmp_fd
= sm_outfd
;
277 if (!option_info
&& !__inline_call
)
282 if (key
&& strlen(key
) >= 80)
285 fn
= get_fnptr_name(call
->fn
);
291 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
292 get_base_file(), get_function(), fn
, (unsigned long)call
,
293 is_static(call
->fn
), type
, param
, key
, value
);
299 if (strncmp(fn
, "__builtin_", 10) == 0)
301 if (type
!= INTERNAL
&& is_common_function(fn
))
304 sm_outfd
= caller_info_fd
;
305 sm_msg("SQL_caller_info: insert into caller_info values ("
306 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
307 get_base_file(), get_function(), fn
, is_static(call
->fn
),
308 type
, param
, key
, value
);
314 void sql_insert_function_ptr(const char *fn
, const char *struct_name
)
316 sql_insert_or_ignore(function_ptr
, "'%s', '%s', '%s', 0",
317 get_base_file(), fn
, struct_name
);
320 void sql_insert_return_implies(int type
, int param
, const char *key
, const char *value
)
322 sql_insert_or_ignore(return_implies
, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
323 get_base_file(), get_function(), (unsigned long)__inline_fn
,
324 fn_static(), type
, param
, key
, value
);
327 void sql_insert_call_implies(int type
, int param
, const char *key
, const char *value
)
329 sql_insert_or_ignore(call_implies
, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
330 get_base_file(), get_function(), (unsigned long)__inline_fn
,
331 fn_static(), type
, param
, key
, value
);
334 void sql_insert_function_type_size(const char *member
, const char *ranges
)
336 sql_insert(function_type_size
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member
, ranges
);
339 void sql_insert_function_type_info(int type
, const char *struct_type
, const char *member
, const char *value
)
341 sql_insert(function_type_info
, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type
, struct_type
, member
, value
);
344 void sql_insert_type_info(int type
, const char *member
, const char *value
)
346 sql_insert_cache(type_info
, "'%s', %d, '%s', '%s'", get_base_file(), type
, member
, value
);
349 void sql_insert_local_values(const char *name
, const char *value
)
351 sql_insert(local_values
, "'%s', '%s', '%s'", get_base_file(), name
, value
);
354 void sql_insert_function_type_value(const char *type
, const char *value
)
356 sql_insert(function_type_value
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type
, value
);
359 void sql_insert_function_type(int param
, const char *value
)
361 sql_insert(function_type
, "'%s', '%s', %d, %d, '%s'",
362 get_base_file(), get_function(), fn_static(), param
, value
);
365 void sql_insert_parameter_name(int param
, const char *value
)
367 sql_insert(parameter_name
, "'%s', '%s', %d, %d, '%s'",
368 get_base_file(), get_function(), fn_static(), param
, value
);
371 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
)
375 data_name
= get_data_info_name(data
);
378 sql_insert(data_info
, "'%s', '%s', %d, '%s'",
379 is_static(data
) ? get_base_file() : "extern",
380 data_name
, type
, value
);
383 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
)
385 sql_insert(data_info
, "'%s', '%s', %d, '%s'",
386 (sym
->ctype
.modifiers
& MOD_STATIC
) ? get_base_file() : "extern",
390 void sql_save_constraint(const char *con
)
395 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con
));
398 void sql_save_constraint_required(const char *data
, int op
, const char *limit
)
400 sql_insert_or_ignore(constraints_required
, "'%s', '%s', '%s'", data
, show_special(op
), limit
);
403 void sql_copy_constraint_required(const char *new_limit
, const char *old_limit
)
408 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
409 "select constraints_required.data, constraints_required.op, '%s' from "
410 "constraints_required where bound = '%s';", new_limit
, old_limit
);
413 void sql_insert_fn_ptr_data_link(const char *ptr
, const char *data
)
415 sql_insert_or_ignore(fn_ptr_data_link
, "'%s', '%s'", ptr
, data
);
418 void sql_insert_fn_data_link(struct expression
*fn
, int type
, int param
, const char *key
, const char *value
)
420 if (fn
->type
!= EXPR_SYMBOL
|| !fn
->symbol
->ident
)
423 sql_insert(fn_data_link
, "'%s', '%s', %d, %d, %d, '%s', '%s'",
424 is_local(fn
->symbol
) ? get_base_file() : "extern",
425 fn
->symbol
->ident
->name
,
426 is_local(fn
->symbol
),
427 type
, param
, key
, value
);
430 void sql_insert_mtag_about(mtag_t tag
, const char *left_name
, const char *right_name
)
432 sql_insert_cache(mtag_about
, "%lld, '%s', '%s', %d, '%s', '%s'",
433 tag
, get_filename(), get_function(), get_lineno(),
434 left_name
, right_name
);
437 void sql_insert_mtag_info(mtag_t tag
, int type
, const char *value
)
439 sql_insert_cache(mtag_info
, "'%s', %lld, %d, '%s'", get_filename(), tag
, type
, value
);
442 void sql_insert_mtag_map(mtag_t container
, int container_offset
, mtag_t tag
, int tag_offset
)
444 sql_insert(mtag_map
, "%lld, %d, %lld, %d", container
, container_offset
, tag
, tag_offset
);
447 void sql_insert_mtag_alias(mtag_t orig
, mtag_t alias
)
449 sql_insert(mtag_alias
, "%lld, %lld", orig
, alias
);
452 static int save_mtag(void *_tag
, int argc
, char **argv
, char **azColName
)
454 mtag_t
*saved_tag
= _tag
;
457 new_tag
= strtoll(argv
[0], NULL
, 10);
460 *saved_tag
= new_tag
;
461 else if (*saved_tag
!= new_tag
)
467 int mtag_map_select_container(mtag_t tag
, int container_offset
, mtag_t
*container
)
471 run_sql(save_mtag
, &tmp
,
472 "select container from mtag_map where tag = %lld and container_offset = %d and tag_offset = 0;",
473 tag
, container_offset
);
475 if (tmp
== 0 || tmp
== -1ULL)
481 int mtag_map_select_tag(mtag_t container
, int offset
, mtag_t
*tag
)
485 run_sql(save_mtag
, &tmp
,
486 "select tag from mtag_map where container = %lld and container_offset = %d;",
489 if (tmp
== 0 || tmp
== -1ULL)
495 char *get_static_filter(struct symbol
*sym
)
497 static char sql_filter
[1024];
499 /* This can only happen on buggy code. Return invalid SQL. */
501 sql_filter
[0] = '\0';
506 snprintf(sql_filter
, sizeof(sql_filter
),
507 "file = '%s' and function = '%s' and static = '1'",
508 get_base_file(), sym
->ident
->name
);
510 snprintf(sql_filter
, sizeof(sql_filter
),
511 "function = '%s' and static = '0'", sym
->ident
->name
);
517 static int get_row_count(void *_row_count
, int argc
, char **argv
, char **azColName
)
519 int *row_count
= _row_count
;
524 *row_count
= atoi(argv
[0]);
528 static void mark_call_params_untracked(struct expression
*call
)
530 struct expression
*arg
;
533 FOR_EACH_PTR(call
->args
, arg
) {
534 mark_untracked(call
, i
++, "$", NULL
);
535 } END_FOR_EACH_PTR(arg
);
538 static void sql_select_return_states_pointer(const char *cols
,
539 struct expression
*call
, int (*callback
)(void*, int, char**, char**), void *info
)
542 int return_count
= 0;
544 ptr
= get_fnptr_name(call
->fn
);
548 run_sql(get_row_count
, &return_count
,
549 "select count(*) from return_states join function_ptr "
550 "where return_states.function == function_ptr.function and "
551 "ptr = '%s' and searchable = 1 and type = %d;", ptr
, INTERNAL
);
552 /* The magic number 100 is just from testing on the kernel. */
553 if (return_count
> 100) {
554 mark_call_params_untracked(call
);
558 run_sql(callback
, info
,
559 "select %s from return_states join function_ptr where "
560 "return_states.function == function_ptr.function and ptr = '%s' "
561 "and searchable = 1 "
562 "order by function_ptr.file, return_states.file, return_id, type;",
566 static int is_local_symbol(struct expression
*expr
)
568 if (expr
->type
!= EXPR_SYMBOL
)
570 if (expr
->symbol
->ctype
.modifiers
& (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
))
575 void sql_select_return_states(const char *cols
, struct expression
*call
,
576 int (*callback
)(void*, int, char**, char**), void *info
)
578 struct expression
*fn
;
581 if (is_fake_call(call
))
584 fn
= strip_expr(call
->fn
);
585 if (fn
->type
!= EXPR_SYMBOL
|| !fn
->symbol
|| is_local_symbol(fn
)) {
586 sql_select_return_states_pointer(cols
, call
, callback
, info
);
591 mem_sql(callback
, info
,
592 "select %s from return_states where call_id = '%lu' order by return_id, type;",
593 cols
, (unsigned long)call
);
597 run_sql(get_row_count
, &row_count
, "select count(*) from return_states where %s;",
598 get_static_filter(fn
->symbol
));
599 if (row_count
== 0 && fn
->symbol
&& fn
->symbol
->definition
)
600 set_state(my_id
, "db_incomplete", NULL
, &incomplete
);
601 if (row_count
> 3000)
604 run_sql(callback
, info
, "select %s from return_states where %s order by file, return_id, type;",
605 cols
, get_static_filter(fn
->symbol
));
608 bool db_incomplete(void)
610 return !!get_state(my_id
, "db_incomplete", NULL
);
613 #define CALL_IMPLIES 0
614 #define RETURN_IMPLIES 1
616 struct implies_info
{
618 struct db_implies_cb_list
*cb_list
;
619 struct expression
*expr
;
623 void sql_select_implies(const char *cols
, struct implies_info
*info
,
624 int (*callback
)(void*, int, char**, char**))
626 if (info
->type
== RETURN_IMPLIES
&& inlinable(info
->expr
->fn
)) {
627 mem_sql(callback
, info
,
628 "select %s from return_implies where call_id = '%lu';",
629 cols
, (unsigned long)info
->expr
);
633 run_sql(callback
, info
, "select %s from %s_implies where %s;",
635 info
->type
== CALL_IMPLIES
? "call" : "return",
636 get_static_filter(info
->sym
));
639 struct select_caller_info_data
{
640 struct stree
*final_states
;
641 struct timeval start_time
;
647 static int caller_info_callback(void *_data
, int argc
, char **argv
, char **azColName
);
649 static void sql_select_caller_info(struct select_caller_info_data
*data
,
650 const char *cols
, struct symbol
*sym
)
653 mem_sql(caller_info_callback
, data
,
654 "select %s from caller_info where call_id = %lu;",
655 cols
, (unsigned long)__inline_fn
);
659 if (sym
->ident
->name
&& is_common_function(sym
->ident
->name
))
661 run_sql(caller_info_callback
, data
,
662 "select %s from common_caller_info where %s order by call_id;",
663 cols
, get_static_filter(sym
));
667 run_sql(caller_info_callback
, data
,
668 "select %s from caller_info where %s order by call_id;",
669 cols
, get_static_filter(sym
));
672 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
674 struct def_callback
*def_callback
= __alloc_def_callback(0);
676 def_callback
->hook_type
= type
;
677 def_callback
->callback
= callback
;
678 add_ptr_list(&select_caller_info_callbacks
, def_callback
);
682 * These call backs are used when the --info option is turned on to print struct
683 * member information. For example foo->bar could have a state in
684 * smatch_extra.c and also check_user.c.
686 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
688 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
690 member_callback
->owner
= owner
;
691 member_callback
->callback
= callback
;
692 add_ptr_list(&member_callbacks
, member_callback
);
695 void add_caller_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
697 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
699 member_callback
->owner
= owner
;
700 member_callback
->callback
= callback
;
701 add_ptr_list(&member_callbacks_new
, member_callback
);
704 void add_return_info_callback(int owner
,
705 void (*callback
)(int return_id
, char *return_ranges
,
706 struct expression
*returned_expr
,
708 const char *printed_name
,
709 struct sm_state
*sm
))
711 struct return_info_callback
*return_callback
= __alloc_return_info_callback(0);
713 return_callback
->owner
= owner
;
714 return_callback
->callback
= callback
;
715 add_ptr_list(&return_callbacks
, return_callback
);
718 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
720 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
722 callback
->callback
= fn
;
723 add_ptr_list(&returned_state_callbacks
, callback
);
726 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
))
728 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
730 member_callback
->owner
= owner
;
731 member_callback
->callback
= callback
;
732 add_ptr_list(&returned_member_callbacks
, member_callback
);
735 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
))
737 struct db_implies_callback
*cb
= __alloc_db_implies_callback(0);
740 cb
->callback
= callback
;
741 add_ptr_list(&call_implies_cb_list
, cb
);
744 void select_return_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
))
746 struct db_implies_callback
*cb
= __alloc_db_implies_callback(0);
749 cb
->callback
= callback
;
750 add_ptr_list(&return_implies_cb_list
, cb
);
754 struct expression
*static_returns_call
;
755 struct symbol
*return_type
;
756 struct range_list
*return_range_list
;
759 static int db_return_callback(void *_ret_info
, int argc
, char **argv
, char **azColName
)
761 struct return_info
*ret_info
= _ret_info
;
762 struct range_list
*rl
;
763 struct expression
*call_expr
= ret_info
->static_returns_call
;
767 call_results_to_rl(call_expr
, ret_info
->return_type
, argv
[0], &rl
);
768 ret_info
->return_range_list
= rl_union(ret_info
->return_range_list
, rl
);
772 struct range_list
*db_return_vals(struct expression
*expr
)
774 struct return_info ret_info
= {};
777 if (is_fake_call(expr
))
780 sm
= get_extra_sm_state(expr
);
782 return clone_rl(estate_rl(sm
->state
));
783 ret_info
.static_returns_call
= expr
;
784 ret_info
.return_type
= get_type(expr
);
785 if (!ret_info
.return_type
)
788 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
791 ret_info
.return_range_list
= NULL
;
792 if (inlinable(expr
->fn
)) {
793 mem_sql(db_return_callback
, &ret_info
,
794 "select distinct return from return_states where call_id = '%lu';",
795 (unsigned long)expr
);
797 run_sql(db_return_callback
, &ret_info
,
798 "select distinct return from return_states where %s;",
799 get_static_filter(expr
->fn
->symbol
));
801 return ret_info
.return_range_list
;
804 struct range_list
*db_return_vals_from_str(const char *fn_name
)
806 struct return_info ret_info
;
808 ret_info
.static_returns_call
= NULL
;
809 ret_info
.return_type
= &llong_ctype
;
810 ret_info
.return_range_list
= NULL
;
812 run_sql(db_return_callback
, &ret_info
,
813 "select distinct return from return_states where function = '%s';",
815 return ret_info
.return_range_list
;
819 * This is used when we have a function that takes a function pointer as a
820 * parameter. "frob(blah, blah, my_function);" We know that the return values
821 * from frob() come from my_funcion() so we want to find the possible returns
822 * of my_function(), but we don't know which arguments are passed to it.
825 struct range_list
*db_return_vals_no_args(struct expression
*expr
)
827 struct return_info ret_info
= {};
829 if (!expr
|| expr
->type
!= EXPR_SYMBOL
)
832 ret_info
.static_returns_call
= expr
;
833 ret_info
.return_type
= get_type(expr
);
834 ret_info
.return_type
= get_real_base_type(ret_info
.return_type
);
835 if (!ret_info
.return_type
)
838 run_sql(db_return_callback
, &ret_info
,
839 "select distinct return from return_states where %s;",
840 get_static_filter(expr
->symbol
));
842 return ret_info
.return_range_list
;
845 static void match_call_marker(struct expression
*expr
)
849 type
= get_type(expr
->fn
);
850 if (type
&& type
->type
== SYM_PTR
)
851 type
= get_real_base_type(type
);
854 * we just want to record something in the database so that if we have
855 * two calls like: frob(4); frob(some_unkown); then on the receiving
856 * side we know that sometimes frob is called with unknown parameters.
859 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", type_to_str(type
));
862 int is_recursive_member(const char *name
)
865 const char *p
, *next
;
868 p
= strchr(name
, '>');
873 next
= strchr(p
, '>');
879 if (size
>= sizeof(buf
))
881 memcpy(buf
, p
, size
);
883 if (strstr(next
, buf
))
889 char *sm_to_arg_name(struct expression
*expr
, struct sm_state
*sm
)
894 bool is_address
= false;
895 bool add_star
= false;
900 expr
= strip_expr(expr
);
904 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
905 expr
= strip_expr(expr
->unop
);
909 name
= expr_to_var_sym(expr
, &sym
);
917 if (sm_name
[0] == '*') {
923 if (strncmp(name
, sm_name
, len
) != 0)
925 if (sm_name
[len
] == '\0') {
926 snprintf(buf
, sizeof(buf
), "%s%s$",
927 add_star
? "*" : "", is_address
? "*" : "");
929 if (sm_name
[len
] != '.' && sm_name
[len
] != '-')
931 if (sm_name
[len
] == '-')
933 // FIXME does is_address really imply that sm_name[len] == '-'
934 snprintf(buf
, sizeof(buf
), "%s$->%s", add_star
? "*" : "",
938 ret
= alloc_sname(buf
);
944 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
,
946 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
),
954 char printed_name
[256];
959 expr
= strip_expr(expr
);
962 type
= get_type(expr
);
963 if (!new && type
&& type_bits(type
) < type_bits(&ulong_ctype
))
966 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
967 expr
= strip_expr(expr
->unop
);
971 name
= expr_to_var_sym(expr
, &sym
);
976 FOR_EACH_SM(__get_cur_stree(), sm
) {
977 if (sm
->owner
!= owner
|| sm
->sym
!= sym
)
981 if (sm_name
[0] == '*') {
986 if (!add_star
&& strcmp(name
, sm_name
) == 0) {
988 snprintf(printed_name
, sizeof(printed_name
), "*$");
991 snprintf(printed_name
, sizeof(printed_name
), "$");
995 } else if (add_star
&& strcmp(name
, sm_name
) == 0) {
996 snprintf(printed_name
, sizeof(printed_name
), "%s*$",
997 is_address
? "*" : "");
998 } else if (strncmp(name
, sm_name
, len
) == 0) {
999 if (sm_name
[len
] != '.' && sm_name
[len
] != '-')
1001 if (is_address
&& sm_name
[len
] == '.') {
1002 snprintf(printed_name
, sizeof(printed_name
),
1003 "%s$->%s", add_star
? "*" : "",
1005 } else if (is_address
&& sm_name
[len
] == '-') {
1006 snprintf(printed_name
, sizeof(printed_name
),
1007 "%s(*$)%s", add_star
? "*" : "",
1010 snprintf(printed_name
, sizeof(printed_name
),
1011 "%s$%s", add_star
? "*" : "",
1017 if (is_recursive_member(printed_name
))
1019 callback(call
, param
, printed_name
, sm
);
1020 } END_FOR_EACH_SM(sm
);
1025 static void match_call_info(struct expression
*call
)
1027 struct member_info_callback
*cb
;
1028 struct expression
*arg
;
1031 FOR_EACH_PTR(member_callbacks
, cb
) {
1033 FOR_EACH_PTR(call
->args
, arg
) {
1035 print_struct_members(call
, arg
, i
, cb
->owner
, cb
->callback
, 0);
1036 } END_FOR_EACH_PTR(arg
);
1037 } END_FOR_EACH_PTR(cb
);
1040 static struct expression
*get_fake_variable(struct expression
*expr
)
1042 struct expression
*tmp
;
1044 tmp
= expr_get_fake_parent_expr(expr
);
1045 if (!tmp
|| tmp
->type
!= EXPR_ASSIGNMENT
)
1051 static void match_call_info_new(struct expression
*call
)
1053 struct member_info_callback
*cb
;
1054 struct expression
*arg
, *tmp
;
1057 if (!option_info
&& !__inline_call
)
1060 FOR_EACH_PTR(member_callbacks_new
, cb
) {
1062 FOR_EACH_PTR(call
->args
, arg
) {
1064 tmp
= get_fake_variable(arg
);
1067 __ignore_param_used
++;
1068 print_struct_members(call
, tmp
, i
, cb
->owner
, cb
->callback
, 1);
1069 __ignore_param_used
--;
1070 } END_FOR_EACH_PTR(arg
);
1071 } END_FOR_EACH_PTR(cb
);
1074 static int get_param(int param
, char **name
, struct symbol
**sym
)
1080 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
1082 * this is a temporary hack to work around a bug (I think in sparse?)
1083 * 2.6.37-rc1:fs/reiserfs/journal.o
1084 * If there is a function definition without parameter name found
1085 * after a function implementation then it causes a crash.
1089 if (arg
->ident
->name
< (char *)100)
1092 *name
= arg
->ident
->name
;
1097 } END_FOR_EACH_PTR(arg
);
1102 static int function_signature_matches(const char *sig
)
1106 my_sig
= function_signature();
1107 if (!sig
|| !my_sig
)
1108 return 1; /* default to matching */
1109 if (strcmp(my_sig
, sig
) == 0)
1114 static int caller_info_callback(void *_data
, int argc
, char **argv
, char **azColName
)
1116 struct select_caller_info_data
*data
= _data
;
1123 struct symbol
*sym
= NULL
;
1124 struct def_callback
*def_callback
;
1125 struct stree
*stree
;
1126 struct timeval cur_time
;
1133 gettimeofday(&cur_time
, NULL
);
1134 if (cur_time
.tv_sec
- data
->start_time
.tv_sec
> 10)
1137 func_id
= atoi(argv
[0]);
1139 type
= strtol(argv
[1], NULL
, 10);
1140 param
= strtol(argv
[2], NULL
, 10);
1146 if (data
->prev_func_id
== -1)
1147 data
->prev_func_id
= func_id
;
1148 if (func_id
!= data
->prev_func_id
) {
1149 stree
= __pop_fake_cur_stree();
1151 merge_stree(&data
->final_states
, stree
);
1153 __push_fake_cur_stree();
1155 data
->prev_func_id
= func_id
;
1161 if (type
== INTERNAL
&&
1162 !function_signature_matches(value
)) {
1167 if (param
>= 0 && !get_param(param
, &name
, &sym
))
1170 FOR_EACH_PTR(select_caller_info_callbacks
, def_callback
) {
1171 if (def_callback
->hook_type
== type
)
1172 def_callback
->callback(name
, sym
, key
, value
);
1173 } END_FOR_EACH_PTR(def_callback
);
1178 static struct string_list
*ptr_names_done
;
1179 static struct string_list
*ptr_names
;
1181 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
1183 insert_string(&ptr_names
, alloc_string(argv
[0]));
1187 static char *get_next_ptr_name(void)
1191 FOR_EACH_PTR(ptr_names
, ptr
) {
1192 if (!insert_string(&ptr_names_done
, ptr
))
1195 } END_FOR_EACH_PTR(ptr
);
1199 static void get_ptr_names(const char *file
, const char *name
)
1201 char sql_filter
[1024];
1205 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
1208 snprintf(sql_filter
, 1024, "function = '%s';", name
);
1211 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
1213 run_sql(get_ptr_name
, NULL
,
1214 "select distinct ptr from function_ptr where %s",
1217 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
1218 if (before
== after
)
1221 while ((name
= get_next_ptr_name()))
1222 get_ptr_names(NULL
, name
);
1225 static void match_data_from_db(struct symbol
*sym
)
1227 struct select_caller_info_data data
= { .prev_func_id
= -1 };
1228 struct sm_state
*sm
;
1229 struct stree
*stree
;
1230 struct timeval end_time
;
1232 if (!sym
|| !sym
->ident
)
1236 gettimeofday(&data
.start_time
, NULL
);
1238 __push_fake_cur_stree();
1244 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1245 get_ptr_names(get_base_file(), sym
->ident
->name
);
1247 get_ptr_names(NULL
, sym
->ident
->name
);
1249 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
1250 __free_ptr_list((struct ptr_list
**)&ptr_names
);
1251 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
1252 __free_fake_cur_stree();
1256 sql_select_caller_info(&data
,
1257 "call_id, type, parameter, key, value",
1261 stree
= __pop_fake_cur_stree();
1263 merge_stree(&data
.final_states
, stree
);
1265 __push_fake_cur_stree();
1267 data
.prev_func_id
= -1;
1271 FOR_EACH_PTR(ptr_names
, ptr
) {
1272 run_sql(caller_info_callback
, &data
,
1273 "select call_id, type, parameter, key, value"
1274 " from common_caller_info where function = '%s' order by call_id",
1276 } END_FOR_EACH_PTR(ptr
);
1279 FOR_EACH_PTR(ptr_names
, ptr
) {
1281 } END_FOR_EACH_PTR(ptr
);
1282 goto free_ptr_names
;
1285 FOR_EACH_PTR(ptr_names
, ptr
) {
1286 run_sql(caller_info_callback
, &data
,
1287 "select call_id, type, parameter, key, value"
1288 " from caller_info where function = '%s' order by call_id",
1291 } END_FOR_EACH_PTR(ptr
);
1294 __free_ptr_list((struct ptr_list
**)&ptr_names
);
1295 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
1297 sql_select_caller_info(&data
,
1298 "call_id, type, parameter, key, value",
1302 stree
= __pop_fake_cur_stree();
1304 merge_stree(&data
.final_states
, stree
);
1307 gettimeofday(&end_time
, NULL
);
1308 if (end_time
.tv_sec
- data
.start_time
.tv_sec
<= 10) {
1309 FOR_EACH_SM(data
.final_states
, sm
) {
1311 } END_FOR_EACH_SM(sm
);
1314 free_stree(&data
.final_states
);
1317 static int return_implies_callbacks(void *_info
, int argc
, char **argv
, char **azColName
)
1319 struct implies_info
*info
= _info
;
1320 struct db_implies_callback
*cb
;
1321 struct expression
*arg
= NULL
;
1328 type
= atoi(argv
[1]);
1329 param
= atoi(argv
[2]);
1331 FOR_EACH_PTR(info
->cb_list
, cb
) {
1332 if (cb
->type
!= type
)
1335 arg
= get_argument_from_call_expr(info
->expr
->args
, param
);
1339 cb
->callback(info
->expr
, arg
, argv
[3], argv
[4]);
1340 } END_FOR_EACH_PTR(cb
);
1345 static int call_implies_callbacks(void *_info
, int argc
, char **argv
, char **azColName
)
1347 struct implies_info
*info
= _info
;
1348 struct db_implies_callback
*cb
;
1349 struct expression
*arg
;
1358 type
= atoi(argv
[1]);
1359 param
= atoi(argv
[2]);
1361 if (!get_param(param
, &name
, &sym
))
1363 arg
= symbol_expression(sym
);
1367 FOR_EACH_PTR(info
->cb_list
, cb
) {
1368 if (cb
->type
!= type
)
1370 cb
->callback(info
->expr
, arg
, argv
[3], argv
[4]);
1371 } END_FOR_EACH_PTR(cb
);
1376 static void match_return_implies(struct expression
*expr
)
1378 struct implies_info info
= {
1379 .type
= RETURN_IMPLIES
,
1380 .cb_list
= return_implies_cb_list
,
1383 if (expr
->fn
->type
!= EXPR_SYMBOL
||
1387 info
.sym
= expr
->fn
->symbol
;
1388 sql_select_implies("function, type, parameter, key, value", &info
,
1389 return_implies_callbacks
);
1392 static void match_call_implies(struct symbol
*sym
)
1394 struct implies_info info
= {
1395 .type
= CALL_IMPLIES
,
1396 .cb_list
= call_implies_cb_list
,
1399 if (!sym
|| !sym
->ident
)
1403 sql_select_implies("function, type, parameter, key, value", &info
,
1404 call_implies_callbacks
);
1407 static char *get_fn_param_str(struct expression
*expr
)
1409 struct expression
*tmp
;
1413 tmp
= get_assigned_expr(expr
);
1416 expr
= strip_expr(expr
);
1417 if (!expr
|| expr
->type
!= EXPR_CALL
)
1419 expr
= strip_expr(expr
->fn
);
1420 if (!expr
|| expr
->type
!= EXPR_SYMBOL
)
1422 param
= get_param_num(expr
);
1426 snprintf(buf
, sizeof(buf
), "[r $%d]", param
);
1427 return alloc_sname(buf
);
1430 static char *get_return_compare_is_param(struct expression
*expr
)
1437 param
= get_param_num(expr
);
1441 var
= expr_to_var(expr
);
1444 snprintf(buf
, sizeof(buf
), "%s orig", var
);
1445 comparison
= get_comparison_strings(var
, buf
);
1451 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(comparison
), param
);
1452 return alloc_sname(buf
);
1455 static char *get_return_compare_str(struct expression
*expr
)
1459 compare_str
= get_return_compare_is_param(expr
);
1463 compare_str
= expr_lte_to_param(expr
, -1);
1467 return expr_param_comparison(expr
, -1);
1470 static const char *get_return_ranges_str(struct expression
*expr
, struct range_list
**rl_p
)
1472 struct range_list
*rl
;
1473 char *return_ranges
;
1483 return alloc_sname("");
1485 if (get_implied_value(expr
, &sval
)) {
1486 sval
= sval_cast(cur_func_return_type(), sval
);
1487 *rl_p
= alloc_rl(sval
, sval
);
1488 return sval_to_str_or_err_ptr(sval
);
1491 fn_param_str
= get_fn_param_str(expr
);
1492 compare_str
= expr_equal_to_param(expr
, -1);
1493 math_str
= get_value_in_terms_of_parameter_math(expr
);
1495 if (get_implied_rl(expr
, &rl
) && !is_whole_rl(rl
)) {
1496 rl
= cast_rl(cur_func_return_type(), rl
);
1497 return_ranges
= show_rl(rl
);
1498 } else if (get_imaginary_absolute(expr
, &rl
)){
1499 rl
= cast_rl(cur_func_return_type(), rl
);
1500 return alloc_sname(show_rl(rl
));
1502 get_absolute_rl(expr
, &rl
);
1503 rl
= cast_rl(cur_func_return_type(), rl
);
1504 return_ranges
= show_rl(rl
);
1509 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, fn_param_str
);
1510 return alloc_sname(buf
);
1513 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1514 return alloc_sname(buf
);
1517 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1518 return alloc_sname(buf
);
1520 compare_str
= get_return_compare_str(expr
);
1522 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1523 return alloc_sname(buf
);
1526 return return_ranges
;
1529 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
1531 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", function_signature());
1534 static bool call_return_state_hooks_conditional(struct expression
*expr
)
1536 int final_pass_orig
= final_pass
;
1542 (expr
->type
!= EXPR_CONDITIONAL
&& expr
->type
!= EXPR_SELECT
))
1547 __push_fake_cur_stree();
1550 __split_whole_condition(expr
->conditional
);
1551 final_pass
= final_pass_orig
;
1553 call_return_state_hooks(expr
->cond_true
?: expr
->conditional
);
1555 __push_true_states();
1556 __use_false_states();
1558 call_return_state_hooks(expr
->cond_false
);
1560 __merge_true_states();
1561 __free_fake_cur_stree();
1567 static void call_return_state_hooks_compare(struct expression
*expr
)
1569 struct returned_state_callback
*cb
;
1570 char *return_ranges
;
1571 int final_pass_orig
= final_pass
;
1572 sval_t sval
= { .type
= &int_ctype
};
1575 if (!get_implied_value(expr
, &ret
))
1578 __push_fake_cur_stree();
1581 __split_whole_condition(expr
);
1582 final_pass
= final_pass_orig
;
1584 if (ret
.value
!= 0) {
1585 return_ranges
= alloc_sname("1");
1587 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1590 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1591 cb
->callback(return_id
, return_ranges
, expr
);
1592 } END_FOR_EACH_PTR(cb
);
1595 __push_true_states();
1596 __use_false_states();
1598 if (ret
.value
!= 1) {
1599 return_ranges
= alloc_sname("0");
1601 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1604 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1605 cb
->callback(return_id
, return_ranges
, expr
);
1606 } END_FOR_EACH_PTR(cb
);
1609 __merge_true_states();
1610 __free_fake_cur_stree();
1613 static bool is_implies_function(struct expression
*expr
)
1615 struct range_list
*rl
;
1620 rl
= get_range_implications(get_function());
1624 sm_msg("%s: is implied", __func__
);
1628 static int ptr_in_list(struct sm_state
*sm
, struct state_list
*slist
)
1630 struct sm_state
*tmp
;
1632 FOR_EACH_PTR(slist
, tmp
) {
1633 if (strcmp(tmp
->state
->name
, sm
->state
->name
) == 0)
1635 } END_FOR_EACH_PTR(tmp
);
1640 static int split_possible_helper(struct sm_state
*sm
, struct expression
*expr
)
1642 struct returned_state_callback
*cb
;
1643 struct range_list
*rl
;
1644 char *return_ranges
;
1645 struct sm_state
*tmp
;
1647 int nr_possible
, nr_states
;
1650 struct state_list
*already_handled
= NULL
;
1653 if (!sm
|| !sm
->merged
)
1656 if (too_many_possible(sm
) && !is_implies_function(expr
))
1659 /* bail if it gets too complicated */
1661 FOR_EACH_PTR(sm
->possible
, tmp
) {
1664 if (ptr_in_list(tmp
, already_handled
))
1666 add_ptr_list(&already_handled
, tmp
);
1668 } END_FOR_EACH_PTR(tmp
);
1669 free_slist(&already_handled
);
1670 nr_states
= get_db_state_count();
1671 if (nr_states
* nr_possible
>= 2000 && !is_implies_function(expr
))
1674 FOR_EACH_PTR(sm
->possible
, tmp
) {
1677 if (ptr_in_list(tmp
, already_handled
))
1679 add_ptr_list(&already_handled
, tmp
);
1682 __push_fake_cur_stree();
1684 overwrite_states_using_pool(sm
, tmp
);
1686 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
1687 return_ranges
= show_rl(rl
);
1688 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(clone_rl(rl
)));
1689 compare_str
= get_return_compare_str(expr
);
1690 /* ignore obvious stuff like 0 <= param */
1691 /* Is this worthile when we have PARAM_COMPARE? */
1693 strncmp(compare_str
, "[=", 2) != 0 &&
1694 rl_to_sval(rl
, &sval
))
1697 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1698 return_ranges
= alloc_sname(buf
);
1702 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1703 cb
->callback(return_id
, return_ranges
, expr
);
1704 } END_FOR_EACH_PTR(cb
);
1706 __free_fake_cur_stree();
1707 } END_FOR_EACH_PTR(tmp
);
1709 free_slist(&already_handled
);
1714 static int call_return_state_hooks_split_possible(struct expression
*expr
)
1716 struct expression
*fake
;
1717 struct sm_state
*sm
;
1722 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1724 fake
= expr_get_fake_parent_expr(expr
);
1725 if (!fake
|| fake
->type
!= EXPR_ASSIGNMENT
|| fake
->op
!= '=')
1728 sm
= get_sm_state_expr(SMATCH_EXTRA
, fake
);
1730 return split_possible_helper(sm
, expr
);
1733 static bool has_possible_negative(struct sm_state
*sm
)
1735 struct sm_state
*tmp
;
1737 if (!type_signed(estate_type(sm
->state
)))
1740 FOR_EACH_PTR(sm
->possible
, tmp
) {
1741 if (!estate_rl(tmp
->state
))
1743 if (sval_is_negative(estate_min(tmp
->state
)) &&
1744 sval_is_negative(estate_max(tmp
->state
)))
1746 } END_FOR_EACH_PTR(tmp
);
1751 static bool has_separate_zero_null(struct sm_state
*sm
)
1753 struct sm_state
*tmp
;
1756 FOR_EACH_PTR(sm
->possible
, tmp
) {
1757 if (!estate_get_single_value(tmp
->state
, &sval
))
1759 if (sval
.value
== 0)
1761 } END_FOR_EACH_PTR(tmp
);
1766 static int split_positive_from_negative(struct expression
*expr
)
1768 struct sm_state
*sm
;
1769 struct returned_state_callback
*cb
;
1770 struct range_list
*rl
;
1771 const char *return_ranges
;
1772 struct range_list
*ret_rl
;
1776 /* We're going to print the states 3 times */
1777 if (get_db_state_count() > 10000 / 3)
1780 if (!get_implied_rl(expr
, &rl
) || !rl
)
1782 /* Forget about INT_MAX and larger */
1783 if (rl_max(rl
).value
<= 0)
1785 if (!sval_is_negative(rl_min(rl
)))
1788 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1791 if (!has_possible_negative(sm
))
1793 separate_zero
= has_separate_zero_null(sm
);
1795 if (!assume(compare_expression(expr
, separate_zero
? '>' : SPECIAL_GTE
, zero_expr())))
1799 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1800 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1801 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1802 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1803 } END_FOR_EACH_PTR(cb
);
1807 if (separate_zero
) {
1808 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
1811 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1812 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1813 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1814 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1815 } END_FOR_EACH_PTR(cb
);
1821 undo
= assume(compare_expression(expr
, '<', zero_expr()));
1824 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1825 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1826 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1827 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1828 } END_FOR_EACH_PTR(cb
);
1836 static int call_return_state_hooks_split_null_non_null_zero(struct expression
*expr
)
1838 struct returned_state_callback
*cb
;
1839 struct range_list
*rl
;
1840 struct range_list
*nonnull_rl
;
1842 struct range_list
*null_rl
= NULL
;
1843 char *return_ranges
;
1844 struct sm_state
*sm
;
1845 struct smatch_state
*state
;
1847 int final_pass_orig
= final_pass
;
1849 if (!expr
|| expr_equal_to_param(expr
, -1))
1851 if (expr
->type
== EXPR_CALL
)
1854 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1857 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
1860 if (!estate_rl(state
))
1862 if (estate_min(state
).value
== 0 && estate_max(state
).value
== 0)
1864 if (has_possible_negative(sm
))
1866 if (!has_separate_zero_null(sm
))
1869 nr_states
= get_db_state_count();
1870 if (option_info
&& nr_states
>= 1500)
1873 rl
= estate_rl(state
);
1875 __push_fake_cur_stree();
1878 __split_whole_condition(expr
);
1879 final_pass
= final_pass_orig
;
1881 nonnull_rl
= rl_filter(rl
, rl_zero());
1882 return_ranges
= show_rl(nonnull_rl
);
1883 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonnull_rl
));
1886 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1887 cb
->callback(return_id
, return_ranges
, expr
);
1888 } END_FOR_EACH_PTR(cb
);
1890 __push_true_states();
1891 __use_false_states();
1893 return_ranges
= alloc_sname("0");
1894 null_sval
= sval_type_val(rl_type(rl
), 0);
1895 add_range(&null_rl
, null_sval
, null_sval
);
1896 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(null_rl
));
1898 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1899 cb
->callback(return_id
, return_ranges
, expr
);
1900 } END_FOR_EACH_PTR(cb
);
1902 __merge_true_states();
1903 __free_fake_cur_stree();
1908 static bool is_kernel_success_fail(struct sm_state
*sm
)
1910 struct sm_state
*tmp
;
1911 struct range_list
*rl
;
1912 bool has_zero
= false;
1913 bool has_neg
= false;
1915 if (!type_signed(estate_type(sm
->state
)))
1918 FOR_EACH_PTR(sm
->possible
, tmp
) {
1919 rl
= estate_rl(tmp
->state
);
1922 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 0) {
1927 if (rl_min(rl
).value
>= -4095 && rl_max(rl
).value
< 0)
1929 if (strcmp(tmp
->state
->name
, "s32min-(-1)") == 0)
1931 if (strcmp(tmp
->state
->name
, "s32min-(-1),1-s32max") == 0)
1934 } END_FOR_EACH_PTR(tmp
);
1936 return has_zero
&& has_neg
;
1939 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
1941 struct sm_state
*sm
;
1942 struct range_list
*rl
;
1943 struct range_list
*nonzero_rl
;
1945 struct range_list
*zero_rl
= NULL
;
1947 struct returned_state_callback
*cb
;
1948 char *return_ranges
;
1949 int final_pass_orig
= final_pass
;
1951 if (option_project
!= PROJ_KERNEL
)
1954 nr_states
= get_db_state_count();
1955 if (nr_states
> 2000)
1958 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1961 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
1963 if (!is_kernel_success_fail(sm
))
1966 rl
= estate_rl(sm
->state
);
1970 __push_fake_cur_stree();
1973 __split_whole_condition(expr
);
1974 final_pass
= final_pass_orig
;
1976 nonzero_rl
= rl_filter(rl
, rl_zero());
1977 nonzero_rl
= cast_rl(cur_func_return_type(), nonzero_rl
);
1978 return_ranges
= show_rl(nonzero_rl
);
1979 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonzero_rl
));
1982 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1983 cb
->callback(return_id
, return_ranges
, expr
);
1984 } END_FOR_EACH_PTR(cb
);
1986 __push_true_states();
1987 __use_false_states();
1989 return_ranges
= alloc_sname("0");
1990 zero_sval
= sval_type_val(rl_type(rl
), 0);
1991 add_range(&zero_rl
, zero_sval
, zero_sval
);
1992 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(zero_rl
));
1994 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1995 cb
->callback(return_id
, return_ranges
, expr
);
1996 } END_FOR_EACH_PTR(cb
);
1998 __merge_true_states();
1999 __free_fake_cur_stree();
2004 static int is_boolean(struct expression
*expr
)
2006 struct range_list
*rl
;
2008 if (!get_implied_rl(expr
, &rl
))
2010 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
2015 static int splitable_function_call(struct expression
*expr
)
2017 struct sm_state
*sm
;
2019 if (!expr
|| expr
->type
!= EXPR_CALL
)
2021 sm
= get_extra_sm_state(expr
);
2022 return split_possible_helper(sm
, expr
);
2025 static struct sm_state
*find_bool_param(void)
2027 struct stree
*start_states
;
2029 struct sm_state
*sm
, *tmp
;
2032 start_states
= get_start_states();
2034 FOR_EACH_PTR_REVERSE(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
2037 sm
= get_sm_state_stree(start_states
, SMATCH_EXTRA
, arg
->ident
->name
, arg
);
2040 if (rl_min(estate_rl(sm
->state
)).value
!= 0 ||
2041 rl_max(estate_rl(sm
->state
)).value
!= 1)
2044 } END_FOR_EACH_PTR_REVERSE(arg
);
2050 * Check if it's splitable. If not, then splitting it up is likely not
2051 * useful for the callers.
2053 FOR_EACH_PTR(sm
->possible
, tmp
) {
2056 if (!estate_get_single_value(tmp
->state
, &sval
))
2058 } END_FOR_EACH_PTR(tmp
);
2063 static int split_on_bool_sm(struct sm_state
*sm
, struct expression
*expr
)
2065 struct returned_state_callback
*cb
;
2066 struct range_list
*ret_rl
;
2067 const char *return_ranges
;
2068 struct sm_state
*tmp
;
2070 struct state_list
*already_handled
= NULL
;
2072 if (!sm
|| !sm
->merged
)
2075 if (too_many_possible(sm
))
2078 FOR_EACH_PTR(sm
->possible
, tmp
) {
2081 if (ptr_in_list(tmp
, already_handled
))
2083 add_ptr_list(&already_handled
, tmp
);
2086 __push_fake_cur_stree();
2088 overwrite_states_using_pool(sm
, tmp
);
2090 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
2091 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
2093 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
2094 cb
->callback(return_id
, (char *)return_ranges
, expr
);
2095 } END_FOR_EACH_PTR(cb
);
2097 __free_fake_cur_stree();
2098 } END_FOR_EACH_PTR(tmp
);
2100 free_slist(&already_handled
);
2105 static int split_by_bool_param(struct expression
*expr
)
2107 struct sm_state
*start_sm
, *sm
;
2110 start_sm
= find_bool_param();
2113 sm
= get_sm_state(SMATCH_EXTRA
, start_sm
->name
, start_sm
->sym
);
2114 if (!sm
|| estate_get_single_value(sm
->state
, &sval
))
2117 if (get_db_state_count() * 2 >= 2000)
2120 return split_on_bool_sm(sm
, expr
);
2123 static int split_by_null_nonnull_param(struct expression
*expr
)
2126 struct sm_state
*sm
;
2129 /* function must only take one pointer */
2130 if (ptr_list_size((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
) != 1)
2132 arg
= first_ptr_list((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
);
2135 if (get_real_base_type(arg
)->type
!= SYM_PTR
)
2138 if (param_was_set_var_sym(arg
->ident
->name
, arg
))
2140 sm
= get_sm_state(SMATCH_EXTRA
, arg
->ident
->name
, arg
);
2144 if (!has_separate_zero_null(sm
))
2147 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
2148 if (get_db_state_count() * nr_possible
>= 2000)
2151 return split_on_bool_sm(sm
, expr
);
2154 struct expression
*strip_expr_statement(struct expression
*expr
)
2156 struct expression
*orig
= expr
;
2157 struct statement
*stmt
, *last_stmt
;
2161 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '(')
2163 if (expr
->type
!= EXPR_STATEMENT
)
2165 stmt
= expr
->statement
;
2166 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
2169 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
2170 if (!last_stmt
|| last_stmt
->type
== STMT_LABEL
)
2171 last_stmt
= last_stmt
->label_statement
;
2172 if (!last_stmt
|| last_stmt
->type
!= STMT_EXPRESSION
)
2174 return strip_expr(last_stmt
->expression
);
2177 static bool is_kernel_error_path(struct expression
*expr
)
2179 struct range_list
*rl
;
2182 * Splitting up returns requires resources. It also requires resources
2183 * for the caller. It doesn't seem worth it to split anything up.
2185 if (!get_implied_rl(expr
, &rl
))
2187 if (rl_type(rl
) != &int_ctype
)
2189 if (rl_min(rl
).value
>= -4095 &&
2190 rl_max(rl
).value
< 0)
2195 static void call_return_state_hooks(struct expression
*expr
)
2197 struct returned_state_callback
*cb
;
2198 struct range_list
*ret_rl
;
2199 const char *return_ranges
;
2203 if (__path_is_null())
2206 expr
= strip_expr(expr
);
2207 expr
= strip_expr_statement(expr
);
2209 if (is_impossible_path())
2212 if (expr
&& (expr
->type
== EXPR_COMPARE
||
2213 !get_implied_value(expr
, &sval
)) &&
2214 (is_condition(expr
) || is_boolean(expr
))) {
2215 call_return_state_hooks_compare(expr
);
2217 } else if (call_return_state_hooks_conditional(expr
)) {
2219 } else if (is_kernel_error_path(expr
)) {
2221 } else if (call_return_state_hooks_split_possible(expr
)) {
2223 } else if (split_positive_from_negative(expr
)) {
2225 } else if (call_return_state_hooks_split_null_non_null_zero(expr
)) {
2227 } else if (call_return_state_hooks_split_success_fail(expr
)) {
2229 } else if (splitable_function_call(expr
)) {
2231 } else if (split_by_bool_param(expr
)) {
2232 } else if (split_by_null_nonnull_param(expr
)) {
2237 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
2238 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
2241 nr_states
= get_db_state_count();
2242 if (nr_states
>= 10000) {
2243 match_return_info(return_id
, (char *)return_ranges
, expr
);
2244 print_limited_param_set(return_id
, (char *)return_ranges
, expr
);
2245 mark_all_params_untracked(return_id
, (char *)return_ranges
, expr
);
2248 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
2249 cb
->callback(return_id
, (char *)return_ranges
, expr
);
2250 } END_FOR_EACH_PTR(cb
);
2253 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
2255 struct returned_member_callback
*cb
;
2256 struct sm_state
*sm
;
2257 struct symbol
*type
;
2259 char member_name
[256];
2262 type
= get_type(expr
);
2263 if (!type
|| type
->type
!= SYM_PTR
)
2265 name
= expr_to_var(expr
);
2270 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
2271 FOR_EACH_MY_SM(cb
->owner
, __get_cur_stree(), sm
) {
2272 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
2273 strcpy(member_name
, "*$");
2274 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
2277 if (strncmp(sm
->name
, name
, len
) != 0)
2279 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
2281 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
2282 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
2283 } END_FOR_EACH_SM(sm
);
2284 } END_FOR_EACH_PTR(cb
);
2289 static void print_return_struct_info(int return_id
, char *return_ranges
,
2290 struct expression
*expr
,
2292 struct return_info_callback
*cb
)
2294 struct sm_state
*sm
;
2295 const char *printed_name
;
2298 FOR_EACH_MY_SM(cb
->owner
, __get_cur_stree(), sm
) {
2299 if (sm
->sym
&& sm
->sym
== sym
) {
2302 param
= get_param_num_from_sym(sm
->sym
);
2307 printed_name
= get_param_name(sm
);
2311 cb
->callback(return_id
, return_ranges
, expr
, param
, printed_name
, sm
);
2312 } END_FOR_EACH_SM(sm
);
2315 static void print_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
2317 struct return_info_callback
*cb
;
2318 struct expression
*tmp
;
2321 if (!option_info
&& !__inline_fn
&&
2322 !local_debug
&& !option_debug
)
2325 tmp
= get_fake_variable(expr
);
2328 sym
= expr_to_sym(expr
);
2330 FOR_EACH_PTR(return_callbacks
, cb
) {
2331 __ignore_param_used
++;
2332 print_return_struct_info(return_id
, return_ranges
, expr
, sym
, cb
);
2333 __ignore_param_used
--;
2334 } END_FOR_EACH_PTR(cb
);
2337 static void reset_memdb(struct symbol
*sym
)
2339 mem_sql(NULL
, NULL
, "delete from caller_info;");
2340 mem_sql(NULL
, NULL
, "delete from return_states;");
2341 mem_sql(NULL
, NULL
, "delete from call_implies;");
2342 mem_sql(NULL
, NULL
, "delete from return_implies;");
2345 static void match_end_func_info(struct symbol
*sym
)
2347 if (__path_is_null())
2349 call_return_state_hooks(NULL
);
2352 static void match_after_func(struct symbol
*sym
)
2358 static void init_memdb(void)
2362 const char *schema_files
[] = {
2364 "db/caller_info.schema",
2365 "db/common_caller_info.schema",
2366 "db/return_states.schema",
2367 "db/function_type_size.schema",
2368 "db/type_size.schema",
2369 "db/function_type_info.schema",
2370 "db/type_info.schema",
2371 "db/call_implies.schema",
2372 "db/return_implies.schema",
2373 "db/function_ptr.schema",
2374 "db/local_values.schema",
2375 "db/function_type_value.schema",
2376 "db/type_value.schema",
2377 "db/function_type.schema",
2378 "db/data_info.schema",
2379 "db/parameter_name.schema",
2380 "db/constraints.schema",
2381 "db/constraints_required.schema",
2382 "db/fn_ptr_data_link.schema",
2383 "db/fn_data_link.schema",
2384 "db/mtag_about.schema",
2385 "db/mtag_info.schema",
2386 "db/mtag_map.schema",
2387 "db/mtag_data.schema",
2388 "db/mtag_alias.schema",
2390 static char buf
[4096];
2395 rc
= sqlite3_open(":memory:", &mem_db
);
2396 if (rc
!= SQLITE_OK
) {
2397 sm_ierror("starting In-Memory database.");
2401 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
2402 fd
= open_schema_file(schema_files
[i
]);
2405 ret
= read(fd
, buf
, sizeof(buf
));
2407 sm_ierror("failed to read: %s", schema_files
[i
]);
2411 if (ret
== sizeof(buf
)) {
2412 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2413 schema_files
[i
], sizeof(buf
));
2417 rc
= sqlite3_exec(mem_db
, buf
, NULL
, NULL
, &err
);
2418 if (rc
!= SQLITE_OK
) {
2419 sm_ierror("SQL error #2: %s", err
);
2420 sm_ierror("%s", buf
);
2425 static void init_cachedb(void)
2429 const char *schema_files
[] = {
2430 "db/call_implies.schema",
2431 "db/return_implies.schema",
2432 "db/type_info.schema",
2433 "db/mtag_about.schema",
2434 "db/mtag_data.schema",
2435 "db/mtag_info.schema",
2436 "db/sink_info.schema",
2438 static char buf
[4096];
2443 rc
= sqlite3_open(":memory:", &cache_db
);
2444 if (rc
!= SQLITE_OK
) {
2445 sm_ierror("starting In-Memory database.");
2449 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
2450 fd
= open_schema_file(schema_files
[i
]);
2453 ret
= read(fd
, buf
, sizeof(buf
));
2455 sm_ierror("failed to read: %s", schema_files
[i
]);
2459 if (ret
== sizeof(buf
)) {
2460 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2461 schema_files
[i
], sizeof(buf
));
2465 rc
= sqlite3_exec(cache_db
, buf
, NULL
, NULL
, &err
);
2466 if (rc
!= SQLITE_OK
) {
2467 sm_ierror("SQL error #2: %s", err
);
2468 sm_ierror("%s", buf
);
2473 static int save_cache_data(void *_table
, int argc
, char **argv
, char **azColName
)
2475 static char buf
[4096];
2478 char *table
= _table
;
2482 p
+= snprintf(p
, 4096 - (p
- buf
), "insert or ignore into %s values (", table
);
2483 for (i
= 0; i
< argc
; i
++) {
2485 p
+= snprintf(p
, 4096 - (p
- buf
), ", ");
2486 sqlite3_snprintf(sizeof(tmp
), tmp
, "%q", escape_newlines(argv
[i
]));
2487 p
+= snprintf(p
, 4096 - (p
- buf
), "'%s'", tmp
);
2490 p
+= snprintf(p
, 4096 - (p
- buf
), ");");
2494 sm_msg("SQL: %s", buf
);
2498 static void dump_cache(struct symbol_list
*sym_list
)
2500 const char *cache_tables
[] = {
2501 "type_info", "return_implies", "call_implies", "mtag_data",
2502 "mtag_info", "mtag_about", "sink_info",
2510 for (i
= 0; i
< ARRAY_SIZE(cache_tables
); i
++) {
2511 snprintf(buf
, sizeof(buf
), "select * from %s;", cache_tables
[i
]);
2512 cache_sql(&save_cache_data
, (char *)cache_tables
[i
], buf
);
2516 void open_smatch_db(char *db_file
)
2523 use_states
= malloc(num_checks
+ 1);
2524 memset(use_states
, 0xff, num_checks
+ 1);
2529 rc
= sqlite3_open_v2(db_file
, &smatch_db
, SQLITE_OPEN_READONLY
, NULL
);
2530 if (rc
!= SQLITE_OK
) {
2535 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES
);
2539 static void register_common_funcs(void)
2541 struct token
*token
;
2545 if (option_project
== PROJ_NONE
)
2546 strcpy(filename
, "common_functions");
2548 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
2550 token
= get_tokens_file(filename
);
2553 if (token_type(token
) != TOKEN_STREAMBEGIN
)
2555 token
= token
->next
;
2556 while (token_type(token
) != TOKEN_STREAMEND
) {
2557 if (token_type(token
) != TOKEN_IDENT
)
2559 func
= alloc_string(show_ident(token
->ident
));
2560 add_ptr_list(&common_funcs
, func
);
2561 token
= token
->next
;
2563 clear_token_alloc();
2566 static char *get_next_string(char **str
)
2568 static char string
[256];
2577 while (*p
!= '\0' && *p
!= '\n') {
2578 if (*p
== '\\' && *(p
+ 1) == ' ') {
2588 if (len
>= sizeof(string
)) {
2589 memcpy(string
, start
, sizeof(string
));
2590 string
[sizeof(string
) - 1] = '\0';
2591 sm_ierror("return_fix: '%s' too long", string
);
2595 memcpy(string
, start
, len
);
2597 for (i
= 0; i
< sizeof(string
) - 1; i
++) {
2598 if (string
[i
] == '\\' && string
[i
+ 1] == ' ') {
2599 for (j
= i
; string
[j
] != '\0'; j
++)
2600 string
[j
] = string
[j
+ 1];
2609 static void register_return_replacements(void)
2611 char *func
, *orig
, *new;
2617 snprintf(filename
, 256, "db/%s.return_fixes", option_project_str
);
2618 fd
= open_schema_file(filename
);
2621 ret
= read(fd
, buf
, sizeof(buf
));
2625 if (ret
== sizeof(buf
)) {
2626 sm_ierror("file too large: %s (limit %zd bytes)",
2627 filename
, sizeof(buf
));
2634 get_next_string(&p
);
2637 if (replace_count
== 0 || replace_count
% 3 != 0) {
2641 replace_table
= malloc(replace_count
* sizeof(char *));
2646 func
= alloc_string(get_next_string(&p
));
2647 orig
= alloc_string(get_next_string(&p
));
2648 new = alloc_string(get_next_string(&p
));
2650 replace_table
[i
++] = func
;
2651 replace_table
[i
++] = orig
;
2652 replace_table
[i
++] = new;
2656 void register_definition_db_callbacks(int id
)
2660 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2661 add_hook(&match_call_info_new
, FUNCTION_CALL_HOOK
);
2662 add_split_return_callback(match_return_info
);
2663 add_split_return_callback(print_returned_struct_members
);
2664 add_split_return_callback(print_return_info
);
2665 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
2666 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
2667 add_hook(&match_after_func
, AFTER_FUNC_HOOK
);
2669 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
2670 add_hook(&match_call_implies
, FUNC_DEF_HOOK
);
2671 add_hook(&match_return_implies
, CALL_HOOK_AFTER_INLINE
);
2673 register_common_funcs();
2674 register_return_replacements();
2676 add_hook(&dump_cache
, END_FILE_HOOK
);
2679 void register_db_call_marker(int id
)
2681 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
2684 char *get_data_info_name(struct expression
*expr
)
2691 expr
= strip_expr(expr
);
2692 name
= get_member_name(expr
);
2695 name
= expr_to_var_sym(expr
, &sym
);
2698 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
2700 if (sym
->ctype
.modifiers
& MOD_STATIC
)
2701 snprintf(buf
, sizeof(buf
), "static %s", name
);
2703 snprintf(buf
, sizeof(buf
), "global %s", name
);
2704 ret
= alloc_sname(buf
);