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
? "*" : "",
1014 } else if (sm_name
[0] == '&' && strncmp(name
, sm_name
+ 1, len
) == 0) {
1015 if (sm_name
[len
+ 1] != '.' && sm_name
[len
+ 1] != '-')
1017 if (is_address
&& sm_name
[len
+ 1] == '.') {
1018 snprintf(printed_name
, sizeof(printed_name
),
1019 "&%s$->%s", add_star
? "*" : "",
1021 } else if (is_address
&& sm_name
[len
] == '-') {
1022 snprintf(printed_name
, sizeof(printed_name
),
1023 "&%s(*$)%s", add_star
? "*" : "",
1026 snprintf(printed_name
, sizeof(printed_name
),
1027 "&%s$%s", add_star
? "*" : "",
1033 if (is_recursive_member(printed_name
))
1035 callback(call
, param
, printed_name
, sm
);
1036 } END_FOR_EACH_SM(sm
);
1041 static void match_call_info(struct expression
*call
)
1043 struct member_info_callback
*cb
;
1044 struct expression
*arg
;
1047 FOR_EACH_PTR(member_callbacks
, cb
) {
1049 FOR_EACH_PTR(call
->args
, arg
) {
1051 print_struct_members(call
, arg
, i
, cb
->owner
, cb
->callback
, 0);
1052 } END_FOR_EACH_PTR(arg
);
1053 } END_FOR_EACH_PTR(cb
);
1056 static struct expression
*get_fake_variable(struct expression
*expr
)
1058 struct expression
*tmp
;
1060 tmp
= expr_get_fake_parent_expr(expr
);
1061 if (!tmp
|| tmp
->type
!= EXPR_ASSIGNMENT
)
1067 static void match_call_info_new(struct expression
*call
)
1069 struct member_info_callback
*cb
;
1070 struct expression
*arg
, *tmp
;
1073 if (!option_info
&& !__inline_call
)
1076 FOR_EACH_PTR(member_callbacks_new
, cb
) {
1078 FOR_EACH_PTR(call
->args
, arg
) {
1080 tmp
= get_fake_variable(arg
);
1083 __ignore_param_used
++;
1084 print_struct_members(call
, tmp
, i
, cb
->owner
, cb
->callback
, 1);
1085 __ignore_param_used
--;
1086 } END_FOR_EACH_PTR(arg
);
1087 } END_FOR_EACH_PTR(cb
);
1090 static int get_param(int param
, char **name
, struct symbol
**sym
)
1096 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
1098 * this is a temporary hack to work around a bug (I think in sparse?)
1099 * 2.6.37-rc1:fs/reiserfs/journal.o
1100 * If there is a function definition without parameter name found
1101 * after a function implementation then it causes a crash.
1105 if (arg
->ident
->name
< (char *)100)
1108 *name
= arg
->ident
->name
;
1113 } END_FOR_EACH_PTR(arg
);
1118 static int function_signature_matches(const char *sig
)
1122 my_sig
= function_signature();
1123 if (!sig
|| !my_sig
)
1124 return 1; /* default to matching */
1125 if (strcmp(my_sig
, sig
) == 0)
1130 static int caller_info_callback(void *_data
, int argc
, char **argv
, char **azColName
)
1132 struct select_caller_info_data
*data
= _data
;
1139 struct symbol
*sym
= NULL
;
1140 struct def_callback
*def_callback
;
1141 struct stree
*stree
;
1142 struct timeval cur_time
;
1149 gettimeofday(&cur_time
, NULL
);
1150 if (cur_time
.tv_sec
- data
->start_time
.tv_sec
> 10)
1153 func_id
= atoi(argv
[0]);
1155 type
= strtol(argv
[1], NULL
, 10);
1156 param
= strtol(argv
[2], NULL
, 10);
1162 if (data
->prev_func_id
== -1)
1163 data
->prev_func_id
= func_id
;
1164 if (func_id
!= data
->prev_func_id
) {
1165 stree
= __pop_fake_cur_stree();
1167 merge_stree(&data
->final_states
, stree
);
1169 __push_fake_cur_stree();
1171 data
->prev_func_id
= func_id
;
1177 if (type
== INTERNAL
&&
1178 !function_signature_matches(value
)) {
1183 if (param
>= 0 && !get_param(param
, &name
, &sym
))
1186 FOR_EACH_PTR(select_caller_info_callbacks
, def_callback
) {
1187 if (def_callback
->hook_type
== type
)
1188 def_callback
->callback(name
, sym
, key
, value
);
1189 } END_FOR_EACH_PTR(def_callback
);
1194 static struct string_list
*ptr_names_done
;
1195 static struct string_list
*ptr_names
;
1197 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
1199 insert_string(&ptr_names
, alloc_string(argv
[0]));
1203 static char *get_next_ptr_name(void)
1207 FOR_EACH_PTR(ptr_names
, ptr
) {
1208 if (!insert_string(&ptr_names_done
, ptr
))
1211 } END_FOR_EACH_PTR(ptr
);
1215 static void get_ptr_names(const char *file
, const char *name
)
1217 char sql_filter
[1024];
1221 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
1224 snprintf(sql_filter
, 1024, "function = '%s';", name
);
1227 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
1229 run_sql(get_ptr_name
, NULL
,
1230 "select distinct ptr from function_ptr where %s",
1233 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
1234 if (before
== after
)
1237 while ((name
= get_next_ptr_name()))
1238 get_ptr_names(NULL
, name
);
1241 static void match_data_from_db(struct symbol
*sym
)
1243 struct select_caller_info_data data
= { .prev_func_id
= -1 };
1244 struct sm_state
*sm
;
1245 struct stree
*stree
;
1246 struct timeval end_time
;
1248 if (!sym
|| !sym
->ident
)
1252 gettimeofday(&data
.start_time
, NULL
);
1254 __push_fake_cur_stree();
1260 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1261 get_ptr_names(get_base_file(), sym
->ident
->name
);
1263 get_ptr_names(NULL
, sym
->ident
->name
);
1265 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
1266 __free_ptr_list((struct ptr_list
**)&ptr_names
);
1267 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
1268 __free_fake_cur_stree();
1272 sql_select_caller_info(&data
,
1273 "call_id, type, parameter, key, value",
1277 stree
= __pop_fake_cur_stree();
1279 merge_stree(&data
.final_states
, stree
);
1281 __push_fake_cur_stree();
1283 data
.prev_func_id
= -1;
1287 FOR_EACH_PTR(ptr_names
, ptr
) {
1288 run_sql(caller_info_callback
, &data
,
1289 "select call_id, type, parameter, key, value"
1290 " from common_caller_info where function = '%s' order by call_id",
1292 } END_FOR_EACH_PTR(ptr
);
1295 FOR_EACH_PTR(ptr_names
, ptr
) {
1297 } END_FOR_EACH_PTR(ptr
);
1298 goto free_ptr_names
;
1301 FOR_EACH_PTR(ptr_names
, ptr
) {
1302 run_sql(caller_info_callback
, &data
,
1303 "select call_id, type, parameter, key, value"
1304 " from caller_info where function = '%s' order by call_id",
1307 } END_FOR_EACH_PTR(ptr
);
1310 __free_ptr_list((struct ptr_list
**)&ptr_names
);
1311 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
1313 sql_select_caller_info(&data
,
1314 "call_id, type, parameter, key, value",
1318 stree
= __pop_fake_cur_stree();
1320 merge_stree(&data
.final_states
, stree
);
1323 gettimeofday(&end_time
, NULL
);
1324 if (end_time
.tv_sec
- data
.start_time
.tv_sec
<= 10) {
1325 FOR_EACH_SM(data
.final_states
, sm
) {
1327 } END_FOR_EACH_SM(sm
);
1330 free_stree(&data
.final_states
);
1333 static int return_implies_callbacks(void *_info
, int argc
, char **argv
, char **azColName
)
1335 struct implies_info
*info
= _info
;
1336 struct db_implies_callback
*cb
;
1337 struct expression
*arg
= NULL
;
1344 type
= atoi(argv
[1]);
1345 param
= atoi(argv
[2]);
1347 FOR_EACH_PTR(info
->cb_list
, cb
) {
1348 if (cb
->type
!= type
)
1351 arg
= get_argument_from_call_expr(info
->expr
->args
, param
);
1355 cb
->callback(info
->expr
, arg
, argv
[3], argv
[4]);
1356 } END_FOR_EACH_PTR(cb
);
1361 static int call_implies_callbacks(void *_info
, int argc
, char **argv
, char **azColName
)
1363 struct implies_info
*info
= _info
;
1364 struct db_implies_callback
*cb
;
1365 struct expression
*arg
;
1374 type
= atoi(argv
[1]);
1375 param
= atoi(argv
[2]);
1377 if (!get_param(param
, &name
, &sym
))
1379 arg
= symbol_expression(sym
);
1383 FOR_EACH_PTR(info
->cb_list
, cb
) {
1384 if (cb
->type
!= type
)
1386 cb
->callback(info
->expr
, arg
, argv
[3], argv
[4]);
1387 } END_FOR_EACH_PTR(cb
);
1392 static void match_return_implies(struct expression
*expr
)
1394 struct implies_info info
= {
1395 .type
= RETURN_IMPLIES
,
1396 .cb_list
= return_implies_cb_list
,
1399 if (expr
->fn
->type
!= EXPR_SYMBOL
||
1403 info
.sym
= expr
->fn
->symbol
;
1404 sql_select_implies("function, type, parameter, key, value", &info
,
1405 return_implies_callbacks
);
1408 static void match_call_implies(struct symbol
*sym
)
1410 struct implies_info info
= {
1411 .type
= CALL_IMPLIES
,
1412 .cb_list
= call_implies_cb_list
,
1415 if (!sym
|| !sym
->ident
)
1419 sql_select_implies("function, type, parameter, key, value", &info
,
1420 call_implies_callbacks
);
1423 static char *get_fn_param_str(struct expression
*expr
)
1425 struct expression
*tmp
;
1429 tmp
= get_assigned_expr(expr
);
1432 expr
= strip_expr(expr
);
1433 if (!expr
|| expr
->type
!= EXPR_CALL
)
1435 expr
= strip_expr(expr
->fn
);
1436 if (!expr
|| expr
->type
!= EXPR_SYMBOL
)
1438 param
= get_param_num(expr
);
1442 snprintf(buf
, sizeof(buf
), "[r $%d]", param
);
1443 return alloc_sname(buf
);
1446 static char *get_return_compare_is_param(struct expression
*expr
)
1453 param
= get_param_num(expr
);
1457 var
= expr_to_var(expr
);
1460 snprintf(buf
, sizeof(buf
), "%s orig", var
);
1461 comparison
= get_comparison_strings(var
, buf
);
1467 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(comparison
), param
);
1468 return alloc_sname(buf
);
1471 static char *get_return_compare_str(struct expression
*expr
)
1475 compare_str
= get_return_compare_is_param(expr
);
1479 compare_str
= expr_lte_to_param(expr
, -1);
1483 return expr_param_comparison(expr
, -1);
1486 static const char *get_return_ranges_str(struct expression
*expr
, struct range_list
**rl_p
)
1488 struct range_list
*rl
;
1489 char *return_ranges
;
1499 return alloc_sname("");
1501 if (get_implied_value(expr
, &sval
)) {
1502 sval
= sval_cast(cur_func_return_type(), sval
);
1503 *rl_p
= alloc_rl(sval
, sval
);
1504 return sval_to_str_or_err_ptr(sval
);
1507 fn_param_str
= get_fn_param_str(expr
);
1508 compare_str
= expr_equal_to_param(expr
, -1);
1509 math_str
= get_value_in_terms_of_parameter_math(expr
);
1511 if (get_implied_rl(expr
, &rl
) && !is_whole_rl(rl
)) {
1512 rl
= cast_rl(cur_func_return_type(), rl
);
1513 return_ranges
= show_rl(rl
);
1514 } else if (get_imaginary_absolute(expr
, &rl
)){
1515 rl
= cast_rl(cur_func_return_type(), rl
);
1516 return alloc_sname(show_rl(rl
));
1518 get_absolute_rl(expr
, &rl
);
1519 rl
= cast_rl(cur_func_return_type(), rl
);
1520 return_ranges
= show_rl(rl
);
1525 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, fn_param_str
);
1526 return alloc_sname(buf
);
1529 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1530 return alloc_sname(buf
);
1533 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1534 return alloc_sname(buf
);
1536 compare_str
= get_return_compare_str(expr
);
1538 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1539 return alloc_sname(buf
);
1542 return return_ranges
;
1545 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
1547 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", function_signature());
1550 static bool call_return_state_hooks_conditional(struct expression
*expr
)
1552 int final_pass_orig
= final_pass
;
1558 (expr
->type
!= EXPR_CONDITIONAL
&& expr
->type
!= EXPR_SELECT
))
1563 __push_fake_cur_stree();
1566 __split_whole_condition(expr
->conditional
);
1567 final_pass
= final_pass_orig
;
1569 call_return_state_hooks(expr
->cond_true
?: expr
->conditional
);
1571 __push_true_states();
1572 __use_false_states();
1574 call_return_state_hooks(expr
->cond_false
);
1576 __merge_true_states();
1577 __free_fake_cur_stree();
1583 static void call_return_state_hooks_compare(struct expression
*expr
)
1585 struct returned_state_callback
*cb
;
1586 char *return_ranges
;
1587 int final_pass_orig
= final_pass
;
1588 sval_t sval
= { .type
= &int_ctype
};
1591 if (!get_implied_value(expr
, &ret
))
1594 __push_fake_cur_stree();
1597 __split_whole_condition(expr
);
1598 final_pass
= final_pass_orig
;
1600 if (ret
.value
!= 0) {
1601 return_ranges
= alloc_sname("1");
1603 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1606 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1607 cb
->callback(return_id
, return_ranges
, expr
);
1608 } END_FOR_EACH_PTR(cb
);
1611 __push_true_states();
1612 __use_false_states();
1614 if (ret
.value
!= 1) {
1615 return_ranges
= alloc_sname("0");
1617 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1620 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1621 cb
->callback(return_id
, return_ranges
, expr
);
1622 } END_FOR_EACH_PTR(cb
);
1625 __merge_true_states();
1626 __free_fake_cur_stree();
1629 static bool is_implies_function(struct expression
*expr
)
1631 struct range_list
*rl
;
1636 rl
= get_range_implications(get_function());
1640 sm_msg("%s: is implied", __func__
);
1644 static int ptr_in_list(struct sm_state
*sm
, struct state_list
*slist
)
1646 struct sm_state
*tmp
;
1648 FOR_EACH_PTR(slist
, tmp
) {
1649 if (strcmp(tmp
->state
->name
, sm
->state
->name
) == 0)
1651 } END_FOR_EACH_PTR(tmp
);
1656 static int split_possible_helper(struct sm_state
*sm
, struct expression
*expr
)
1658 struct returned_state_callback
*cb
;
1659 struct range_list
*rl
;
1660 char *return_ranges
;
1661 struct sm_state
*tmp
;
1663 int nr_possible
, nr_states
;
1666 struct state_list
*already_handled
= NULL
;
1669 if (!sm
|| !sm
->merged
)
1672 if (too_many_possible(sm
) && !is_implies_function(expr
))
1675 /* bail if it gets too complicated */
1677 FOR_EACH_PTR(sm
->possible
, tmp
) {
1680 if (ptr_in_list(tmp
, already_handled
))
1682 add_ptr_list(&already_handled
, tmp
);
1684 } END_FOR_EACH_PTR(tmp
);
1685 free_slist(&already_handled
);
1686 nr_states
= get_db_state_count();
1687 if (nr_states
* nr_possible
>= 2000 && !is_implies_function(expr
))
1690 FOR_EACH_PTR(sm
->possible
, tmp
) {
1693 if (ptr_in_list(tmp
, already_handled
))
1695 add_ptr_list(&already_handled
, tmp
);
1698 __push_fake_cur_stree();
1700 overwrite_states_using_pool(sm
, tmp
);
1702 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
1703 return_ranges
= show_rl(rl
);
1704 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(clone_rl(rl
)));
1705 compare_str
= get_return_compare_str(expr
);
1706 /* ignore obvious stuff like 0 <= param */
1707 /* Is this worthile when we have PARAM_COMPARE? */
1709 strncmp(compare_str
, "[=", 2) != 0 &&
1710 rl_to_sval(rl
, &sval
))
1713 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1714 return_ranges
= alloc_sname(buf
);
1718 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1719 cb
->callback(return_id
, return_ranges
, expr
);
1720 } END_FOR_EACH_PTR(cb
);
1722 __free_fake_cur_stree();
1723 } END_FOR_EACH_PTR(tmp
);
1725 free_slist(&already_handled
);
1730 static int call_return_state_hooks_split_possible(struct expression
*expr
)
1732 struct expression
*fake
;
1733 struct sm_state
*sm
;
1738 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1740 fake
= expr_get_fake_parent_expr(expr
);
1741 if (!fake
|| fake
->type
!= EXPR_ASSIGNMENT
|| fake
->op
!= '=')
1744 sm
= get_sm_state_expr(SMATCH_EXTRA
, fake
);
1746 return split_possible_helper(sm
, expr
);
1749 static bool has_possible_negative(struct sm_state
*sm
)
1751 struct sm_state
*tmp
;
1753 if (!type_signed(estate_type(sm
->state
)))
1756 FOR_EACH_PTR(sm
->possible
, tmp
) {
1757 if (!estate_rl(tmp
->state
))
1759 if (sval_is_negative(estate_min(tmp
->state
)) &&
1760 sval_is_negative(estate_max(tmp
->state
)))
1762 } END_FOR_EACH_PTR(tmp
);
1767 static bool has_separate_zero_null(struct sm_state
*sm
)
1769 struct sm_state
*tmp
;
1772 FOR_EACH_PTR(sm
->possible
, tmp
) {
1773 if (!estate_get_single_value(tmp
->state
, &sval
))
1775 if (sval
.value
== 0)
1777 } END_FOR_EACH_PTR(tmp
);
1782 static int split_positive_from_negative(struct expression
*expr
)
1784 struct sm_state
*sm
;
1785 struct returned_state_callback
*cb
;
1786 struct range_list
*rl
;
1787 const char *return_ranges
;
1788 struct range_list
*ret_rl
;
1792 /* We're going to print the states 3 times */
1793 if (get_db_state_count() > 10000 / 3)
1796 if (!get_implied_rl(expr
, &rl
) || !rl
)
1798 /* Forget about INT_MAX and larger */
1799 if (rl_max(rl
).value
<= 0)
1801 if (!sval_is_negative(rl_min(rl
)))
1804 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1807 if (!has_possible_negative(sm
))
1809 separate_zero
= has_separate_zero_null(sm
);
1811 if (!assume(compare_expression(expr
, separate_zero
? '>' : SPECIAL_GTE
, zero_expr())))
1815 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1816 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1817 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1818 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1819 } END_FOR_EACH_PTR(cb
);
1823 if (separate_zero
) {
1824 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
1827 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1828 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1829 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1830 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1831 } END_FOR_EACH_PTR(cb
);
1837 undo
= assume(compare_expression(expr
, '<', zero_expr()));
1840 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1841 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1842 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1843 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1844 } END_FOR_EACH_PTR(cb
);
1852 static int call_return_state_hooks_split_null_non_null_zero(struct expression
*expr
)
1854 struct returned_state_callback
*cb
;
1855 struct range_list
*rl
;
1856 struct range_list
*nonnull_rl
;
1858 struct range_list
*null_rl
= NULL
;
1859 char *return_ranges
;
1860 struct sm_state
*sm
;
1861 struct smatch_state
*state
;
1863 int final_pass_orig
= final_pass
;
1865 if (!expr
|| expr_equal_to_param(expr
, -1))
1867 if (expr
->type
== EXPR_CALL
)
1870 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1873 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
1876 if (!estate_rl(state
))
1878 if (estate_min(state
).value
== 0 && estate_max(state
).value
== 0)
1880 if (has_possible_negative(sm
))
1882 if (!has_separate_zero_null(sm
))
1885 nr_states
= get_db_state_count();
1886 if (option_info
&& nr_states
>= 1500)
1889 rl
= estate_rl(state
);
1891 __push_fake_cur_stree();
1894 __split_whole_condition(expr
);
1895 final_pass
= final_pass_orig
;
1897 nonnull_rl
= rl_filter(rl
, rl_zero());
1898 return_ranges
= show_rl(nonnull_rl
);
1899 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonnull_rl
));
1902 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1903 cb
->callback(return_id
, return_ranges
, expr
);
1904 } END_FOR_EACH_PTR(cb
);
1906 __push_true_states();
1907 __use_false_states();
1909 return_ranges
= alloc_sname("0");
1910 null_sval
= sval_type_val(rl_type(rl
), 0);
1911 add_range(&null_rl
, null_sval
, null_sval
);
1912 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(null_rl
));
1914 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1915 cb
->callback(return_id
, return_ranges
, expr
);
1916 } END_FOR_EACH_PTR(cb
);
1918 __merge_true_states();
1919 __free_fake_cur_stree();
1924 static bool is_kernel_success_fail(struct sm_state
*sm
)
1926 struct sm_state
*tmp
;
1927 struct range_list
*rl
;
1928 bool has_zero
= false;
1929 bool has_neg
= false;
1931 if (!type_signed(estate_type(sm
->state
)))
1934 FOR_EACH_PTR(sm
->possible
, tmp
) {
1935 rl
= estate_rl(tmp
->state
);
1938 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 0) {
1943 if (rl_min(rl
).value
>= -4095 && rl_max(rl
).value
< 0)
1945 if (strcmp(tmp
->state
->name
, "s32min-(-1)") == 0)
1947 if (strcmp(tmp
->state
->name
, "s32min-(-1),1-s32max") == 0)
1950 } END_FOR_EACH_PTR(tmp
);
1952 return has_zero
&& has_neg
;
1955 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
1957 struct sm_state
*sm
;
1958 struct range_list
*rl
;
1959 struct range_list
*nonzero_rl
;
1961 struct range_list
*zero_rl
= NULL
;
1963 struct returned_state_callback
*cb
;
1964 char *return_ranges
;
1965 int final_pass_orig
= final_pass
;
1967 if (option_project
!= PROJ_KERNEL
)
1970 nr_states
= get_db_state_count();
1971 if (nr_states
> 2000)
1974 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1977 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
1979 if (!is_kernel_success_fail(sm
))
1982 rl
= estate_rl(sm
->state
);
1986 __push_fake_cur_stree();
1989 __split_whole_condition(expr
);
1990 final_pass
= final_pass_orig
;
1992 nonzero_rl
= rl_filter(rl
, rl_zero());
1993 nonzero_rl
= cast_rl(cur_func_return_type(), nonzero_rl
);
1994 return_ranges
= show_rl(nonzero_rl
);
1995 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonzero_rl
));
1998 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1999 cb
->callback(return_id
, return_ranges
, expr
);
2000 } END_FOR_EACH_PTR(cb
);
2002 __push_true_states();
2003 __use_false_states();
2005 return_ranges
= alloc_sname("0");
2006 zero_sval
= sval_type_val(rl_type(rl
), 0);
2007 add_range(&zero_rl
, zero_sval
, zero_sval
);
2008 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(zero_rl
));
2010 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
2011 cb
->callback(return_id
, return_ranges
, expr
);
2012 } END_FOR_EACH_PTR(cb
);
2014 __merge_true_states();
2015 __free_fake_cur_stree();
2020 static int is_boolean(struct expression
*expr
)
2022 struct range_list
*rl
;
2024 if (!get_implied_rl(expr
, &rl
))
2026 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
2031 static int splitable_function_call(struct expression
*expr
)
2033 struct sm_state
*sm
;
2035 if (!expr
|| expr
->type
!= EXPR_CALL
)
2037 sm
= get_extra_sm_state(expr
);
2038 return split_possible_helper(sm
, expr
);
2041 static struct sm_state
*find_bool_param(void)
2043 struct stree
*start_states
;
2045 struct sm_state
*sm
, *tmp
;
2048 start_states
= get_start_states();
2050 FOR_EACH_PTR_REVERSE(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
2053 sm
= get_sm_state_stree(start_states
, SMATCH_EXTRA
, arg
->ident
->name
, arg
);
2056 if (rl_min(estate_rl(sm
->state
)).value
!= 0 ||
2057 rl_max(estate_rl(sm
->state
)).value
!= 1)
2060 } END_FOR_EACH_PTR_REVERSE(arg
);
2066 * Check if it's splitable. If not, then splitting it up is likely not
2067 * useful for the callers.
2069 FOR_EACH_PTR(sm
->possible
, tmp
) {
2072 if (!estate_get_single_value(tmp
->state
, &sval
))
2074 } END_FOR_EACH_PTR(tmp
);
2079 static int split_on_bool_sm(struct sm_state
*sm
, struct expression
*expr
)
2081 struct returned_state_callback
*cb
;
2082 struct range_list
*ret_rl
;
2083 const char *return_ranges
;
2084 struct sm_state
*tmp
;
2086 struct state_list
*already_handled
= NULL
;
2088 if (!sm
|| !sm
->merged
)
2091 if (too_many_possible(sm
))
2094 FOR_EACH_PTR(sm
->possible
, tmp
) {
2097 if (ptr_in_list(tmp
, already_handled
))
2099 add_ptr_list(&already_handled
, tmp
);
2102 __push_fake_cur_stree();
2104 overwrite_states_using_pool(sm
, tmp
);
2106 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
2107 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
2109 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
2110 cb
->callback(return_id
, (char *)return_ranges
, expr
);
2111 } END_FOR_EACH_PTR(cb
);
2113 __free_fake_cur_stree();
2114 } END_FOR_EACH_PTR(tmp
);
2116 free_slist(&already_handled
);
2121 static int split_by_bool_param(struct expression
*expr
)
2123 struct sm_state
*start_sm
, *sm
;
2126 start_sm
= find_bool_param();
2129 sm
= get_sm_state(SMATCH_EXTRA
, start_sm
->name
, start_sm
->sym
);
2130 if (!sm
|| estate_get_single_value(sm
->state
, &sval
))
2133 if (get_db_state_count() * 2 >= 2000)
2136 return split_on_bool_sm(sm
, expr
);
2139 static int split_by_null_nonnull_param(struct expression
*expr
)
2142 struct sm_state
*sm
;
2145 /* function must only take one pointer */
2146 if (ptr_list_size((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
) != 1)
2148 arg
= first_ptr_list((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
);
2151 if (get_real_base_type(arg
)->type
!= SYM_PTR
)
2154 if (param_was_set_var_sym(arg
->ident
->name
, arg
))
2156 sm
= get_sm_state(SMATCH_EXTRA
, arg
->ident
->name
, arg
);
2160 if (!has_separate_zero_null(sm
))
2163 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
2164 if (get_db_state_count() * nr_possible
>= 2000)
2167 return split_on_bool_sm(sm
, expr
);
2170 struct expression
*strip_expr_statement(struct expression
*expr
)
2172 struct expression
*orig
= expr
;
2173 struct statement
*stmt
, *last_stmt
;
2177 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '(')
2179 if (expr
->type
!= EXPR_STATEMENT
)
2181 stmt
= expr
->statement
;
2182 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
2185 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
2186 if (!last_stmt
|| last_stmt
->type
== STMT_LABEL
)
2187 last_stmt
= last_stmt
->label_statement
;
2188 if (!last_stmt
|| last_stmt
->type
!= STMT_EXPRESSION
)
2190 return strip_expr(last_stmt
->expression
);
2193 static bool is_kernel_error_path(struct expression
*expr
)
2195 struct range_list
*rl
;
2198 * Splitting up returns requires resources. It also requires resources
2199 * for the caller. It doesn't seem worth it to split anything up.
2201 if (!get_implied_rl(expr
, &rl
))
2203 if (rl_type(rl
) != &int_ctype
)
2205 if (rl_min(rl
).value
>= -4095 &&
2206 rl_max(rl
).value
< 0)
2211 static void call_return_state_hooks(struct expression
*expr
)
2213 struct returned_state_callback
*cb
;
2214 struct range_list
*ret_rl
;
2215 const char *return_ranges
;
2219 if (__path_is_null())
2222 expr
= strip_expr(expr
);
2223 expr
= strip_expr_statement(expr
);
2225 if (is_impossible_path())
2228 if (expr
&& (expr
->type
== EXPR_COMPARE
||
2229 !get_implied_value(expr
, &sval
)) &&
2230 (is_condition(expr
) || is_boolean(expr
))) {
2231 call_return_state_hooks_compare(expr
);
2233 } else if (call_return_state_hooks_conditional(expr
)) {
2235 } else if (is_kernel_error_path(expr
)) {
2237 } else if (call_return_state_hooks_split_possible(expr
)) {
2239 } else if (split_positive_from_negative(expr
)) {
2241 } else if (call_return_state_hooks_split_null_non_null_zero(expr
)) {
2243 } else if (call_return_state_hooks_split_success_fail(expr
)) {
2245 } else if (splitable_function_call(expr
)) {
2247 } else if (split_by_bool_param(expr
)) {
2248 } else if (split_by_null_nonnull_param(expr
)) {
2253 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
2254 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
2257 nr_states
= get_db_state_count();
2258 if (nr_states
>= 10000) {
2259 match_return_info(return_id
, (char *)return_ranges
, expr
);
2260 print_limited_param_set(return_id
, (char *)return_ranges
, expr
);
2261 mark_all_params_untracked(return_id
, (char *)return_ranges
, expr
);
2264 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
2265 cb
->callback(return_id
, (char *)return_ranges
, expr
);
2266 } END_FOR_EACH_PTR(cb
);
2269 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
2271 struct returned_member_callback
*cb
;
2272 struct sm_state
*sm
;
2273 struct symbol
*type
;
2275 char member_name
[256];
2278 type
= get_type(expr
);
2279 if (!type
|| type
->type
!= SYM_PTR
)
2281 name
= expr_to_var(expr
);
2286 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
2287 FOR_EACH_MY_SM(cb
->owner
, __get_cur_stree(), sm
) {
2288 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
2289 strcpy(member_name
, "*$");
2290 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
2293 if (strncmp(sm
->name
, name
, len
) != 0)
2295 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
2297 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
2298 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
2299 } END_FOR_EACH_SM(sm
);
2300 } END_FOR_EACH_PTR(cb
);
2305 static void print_return_struct_info(int return_id
, char *return_ranges
,
2306 struct expression
*expr
,
2308 struct return_info_callback
*cb
)
2310 struct sm_state
*sm
;
2311 const char *printed_name
;
2314 FOR_EACH_MY_SM(cb
->owner
, __get_cur_stree(), sm
) {
2315 if (sm
->sym
&& sm
->sym
== sym
) {
2318 param
= get_param_num_from_sym(sm
->sym
);
2323 printed_name
= get_param_name(sm
);
2327 cb
->callback(return_id
, return_ranges
, expr
, param
, printed_name
, sm
);
2328 } END_FOR_EACH_SM(sm
);
2331 static void print_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
2333 struct return_info_callback
*cb
;
2334 struct expression
*tmp
;
2337 if (!option_info
&& !__inline_fn
&&
2338 !local_debug
&& !option_debug
)
2341 tmp
= get_fake_variable(expr
);
2344 sym
= expr_to_sym(expr
);
2346 FOR_EACH_PTR(return_callbacks
, cb
) {
2347 __ignore_param_used
++;
2348 print_return_struct_info(return_id
, return_ranges
, expr
, sym
, cb
);
2349 __ignore_param_used
--;
2350 } END_FOR_EACH_PTR(cb
);
2353 static void reset_memdb(struct symbol
*sym
)
2355 mem_sql(NULL
, NULL
, "delete from caller_info;");
2356 mem_sql(NULL
, NULL
, "delete from return_states;");
2357 mem_sql(NULL
, NULL
, "delete from call_implies;");
2358 mem_sql(NULL
, NULL
, "delete from return_implies;");
2361 static void match_end_func_info(struct symbol
*sym
)
2363 if (__path_is_null())
2365 call_return_state_hooks(NULL
);
2368 static void match_after_func(struct symbol
*sym
)
2374 static void init_memdb(void)
2378 const char *schema_files
[] = {
2380 "db/caller_info.schema",
2381 "db/common_caller_info.schema",
2382 "db/return_states.schema",
2383 "db/function_type_size.schema",
2384 "db/type_size.schema",
2385 "db/function_type_info.schema",
2386 "db/type_info.schema",
2387 "db/call_implies.schema",
2388 "db/return_implies.schema",
2389 "db/function_ptr.schema",
2390 "db/local_values.schema",
2391 "db/function_type_value.schema",
2392 "db/type_value.schema",
2393 "db/function_type.schema",
2394 "db/data_info.schema",
2395 "db/parameter_name.schema",
2396 "db/constraints.schema",
2397 "db/constraints_required.schema",
2398 "db/fn_ptr_data_link.schema",
2399 "db/fn_data_link.schema",
2400 "db/mtag_about.schema",
2401 "db/mtag_info.schema",
2402 "db/mtag_map.schema",
2403 "db/mtag_data.schema",
2404 "db/mtag_alias.schema",
2406 static char buf
[4096];
2411 rc
= sqlite3_open(":memory:", &mem_db
);
2412 if (rc
!= SQLITE_OK
) {
2413 sm_ierror("starting In-Memory database.");
2417 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
2418 fd
= open_schema_file(schema_files
[i
]);
2421 ret
= read(fd
, buf
, sizeof(buf
));
2423 sm_ierror("failed to read: %s", schema_files
[i
]);
2427 if (ret
== sizeof(buf
)) {
2428 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2429 schema_files
[i
], sizeof(buf
));
2433 rc
= sqlite3_exec(mem_db
, buf
, NULL
, NULL
, &err
);
2434 if (rc
!= SQLITE_OK
) {
2435 sm_ierror("SQL error #2: %s", err
);
2436 sm_ierror("%s", buf
);
2441 static void init_cachedb(void)
2445 const char *schema_files
[] = {
2446 "db/call_implies.schema",
2447 "db/return_implies.schema",
2448 "db/type_info.schema",
2449 "db/mtag_about.schema",
2450 "db/mtag_data.schema",
2451 "db/mtag_info.schema",
2452 "db/sink_info.schema",
2454 static char buf
[4096];
2459 rc
= sqlite3_open(":memory:", &cache_db
);
2460 if (rc
!= SQLITE_OK
) {
2461 sm_ierror("starting In-Memory database.");
2465 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
2466 fd
= open_schema_file(schema_files
[i
]);
2469 ret
= read(fd
, buf
, sizeof(buf
));
2471 sm_ierror("failed to read: %s", schema_files
[i
]);
2475 if (ret
== sizeof(buf
)) {
2476 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2477 schema_files
[i
], sizeof(buf
));
2481 rc
= sqlite3_exec(cache_db
, buf
, NULL
, NULL
, &err
);
2482 if (rc
!= SQLITE_OK
) {
2483 sm_ierror("SQL error #2: %s", err
);
2484 sm_ierror("%s", buf
);
2489 static int save_cache_data(void *_table
, int argc
, char **argv
, char **azColName
)
2491 static char buf
[4096];
2494 char *table
= _table
;
2498 p
+= snprintf(p
, 4096 - (p
- buf
), "insert or ignore into %s values (", table
);
2499 for (i
= 0; i
< argc
; i
++) {
2501 p
+= snprintf(p
, 4096 - (p
- buf
), ", ");
2502 sqlite3_snprintf(sizeof(tmp
), tmp
, "%q", escape_newlines(argv
[i
]));
2503 p
+= snprintf(p
, 4096 - (p
- buf
), "'%s'", tmp
);
2506 p
+= snprintf(p
, 4096 - (p
- buf
), ");");
2510 sm_msg("SQL: %s", buf
);
2514 static void dump_cache(struct symbol_list
*sym_list
)
2516 const char *cache_tables
[] = {
2517 "type_info", "return_implies", "call_implies", "mtag_data",
2518 "mtag_info", "mtag_about", "sink_info",
2526 for (i
= 0; i
< ARRAY_SIZE(cache_tables
); i
++) {
2527 snprintf(buf
, sizeof(buf
), "select * from %s;", cache_tables
[i
]);
2528 cache_sql(&save_cache_data
, (char *)cache_tables
[i
], buf
);
2532 void open_smatch_db(char *db_file
)
2539 use_states
= malloc(num_checks
+ 1);
2540 memset(use_states
, 0xff, num_checks
+ 1);
2545 rc
= sqlite3_open_v2(db_file
, &smatch_db
, SQLITE_OPEN_READONLY
, NULL
);
2546 if (rc
!= SQLITE_OK
) {
2551 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES
);
2555 static char *get_next_string(char **str
)
2557 static char string
[256];
2566 while (*p
!= '\0' && *p
!= '\n') {
2567 if (*p
== '\\' && *(p
+ 1) == ' ') {
2577 if (len
>= sizeof(string
)) {
2578 memcpy(string
, start
, sizeof(string
));
2579 string
[sizeof(string
) - 1] = '\0';
2580 sm_ierror("return_fix: '%s' too long", string
);
2584 memcpy(string
, start
, len
);
2586 for (i
= 0; i
< sizeof(string
) - 1; i
++) {
2587 if (string
[i
] == '\\' && string
[i
+ 1] == ' ') {
2588 for (j
= i
; string
[j
] != '\0'; j
++)
2589 string
[j
] = string
[j
+ 1];
2598 static void register_return_replacements(void)
2600 char *func
, *orig
, *new;
2606 snprintf(filename
, 256, "db/%s.return_fixes", option_project_str
);
2607 fd
= open_schema_file(filename
);
2610 ret
= read(fd
, buf
, sizeof(buf
));
2614 if (ret
== sizeof(buf
)) {
2615 sm_ierror("file too large: %s (limit %zd bytes)",
2616 filename
, sizeof(buf
));
2623 get_next_string(&p
);
2626 if (replace_count
== 0 || replace_count
% 3 != 0) {
2630 replace_table
= malloc(replace_count
* sizeof(char *));
2635 func
= alloc_string(get_next_string(&p
));
2636 orig
= alloc_string(get_next_string(&p
));
2637 new = alloc_string(get_next_string(&p
));
2639 replace_table
[i
++] = func
;
2640 replace_table
[i
++] = orig
;
2641 replace_table
[i
++] = new;
2645 void register_definition_db_callbacks(int id
)
2649 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2650 add_hook(&match_call_info_new
, FUNCTION_CALL_HOOK
);
2651 add_split_return_callback(match_return_info
);
2652 add_split_return_callback(print_returned_struct_members
);
2653 add_split_return_callback(print_return_info
);
2654 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
2655 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
2656 add_hook(&match_after_func
, AFTER_FUNC_HOOK
);
2658 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
2659 add_hook(&match_call_implies
, FUNC_DEF_HOOK
);
2660 add_hook(&match_return_implies
, CALL_HOOK_AFTER_INLINE
);
2662 common_funcs
= load_strings_from_file(option_project_str
, "common_functions");
2663 register_return_replacements();
2665 add_hook(&dump_cache
, END_FILE_HOOK
);
2668 void register_db_call_marker(int id
)
2670 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
2673 char *get_data_info_name(struct expression
*expr
)
2680 expr
= strip_expr(expr
);
2681 name
= get_member_name(expr
);
2684 name
= expr_to_var_sym(expr
, &sym
);
2687 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
2689 if (sym
->ctype
.modifiers
& MOD_STATIC
)
2690 snprintf(buf
, sizeof(buf
), "static %s", name
);
2692 snprintf(buf
, sizeof(buf
), "global %s", name
);
2693 ret
= alloc_sname(buf
);