2 * Copyright (C) 2010 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
26 struct sqlite3
*smatch_db
;
27 struct sqlite3
*mem_db
;
28 struct sqlite3
*cache_db
;
34 static void call_return_state_hooks(struct expression
*expr
);
36 #define SQLITE_CACHE_PAGES 1000
40 void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
);
42 ALLOCATOR(def_callback
, "definition db hook callbacks");
43 DECLARE_PTR_LIST(callback_list
, struct def_callback
);
44 static struct callback_list
*select_caller_info_callbacks
;
46 struct member_info_callback
{
48 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
);
50 ALLOCATOR(member_info_callback
, "caller_info callbacks");
51 DECLARE_PTR_LIST(member_info_cb_list
, struct member_info_callback
);
52 static struct member_info_cb_list
*member_callbacks
;
53 static struct member_info_cb_list
*member_callbacks_new
;
55 struct returned_state_callback
{
56 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*return_expr
);
58 ALLOCATOR(returned_state_callback
, "returned state callbacks");
59 DECLARE_PTR_LIST(returned_state_cb_list
, struct returned_state_callback
);
60 static struct returned_state_cb_list
*returned_state_callbacks
;
62 struct returned_member_callback
{
64 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*expr
, char *printed_name
, struct smatch_state
*state
);
66 ALLOCATOR(returned_member_callback
, "returned member callbacks");
67 DECLARE_PTR_LIST(returned_member_cb_list
, struct returned_member_callback
);
68 static struct returned_member_cb_list
*returned_member_callbacks
;
70 struct db_implies_callback
{
72 void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
);
74 ALLOCATOR(db_implies_callback
, "return_implies callbacks");
75 DECLARE_PTR_LIST(db_implies_cb_list
, struct db_implies_callback
);
76 static struct db_implies_cb_list
*return_implies_cb_list
;
77 static struct db_implies_cb_list
*call_implies_cb_list
;
79 /* silently truncates if needed. */
80 char *escape_newlines(const char *str
)
86 for (i
= 0, j
= 0; str
[i
] != '\0' && j
!= sizeof(buf
); i
++, j
++) {
87 if (str
[i
] != '\r' && str
[i
] != '\n') {
100 return alloc_sname(str
);
102 if (j
== sizeof(buf
))
104 return alloc_sname(buf
);
107 static int print_sql_output(void *unused
, int argc
, char **argv
, char **azColName
)
111 for (i
= 0; i
< argc
; i
++) {
114 sm_printf("%s", argv
[i
]);
120 void sql_exec(struct sqlite3
*db
, int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
)
128 if (option_debug
|| debug_db
) {
130 if (strncasecmp(sql
, "select", strlen("select")) == 0)
131 sqlite3_exec(db
, sql
, print_sql_output
, NULL
, NULL
);
134 rc
= sqlite3_exec(db
, sql
, callback
, data
, &err
);
135 if (rc
!= SQLITE_OK
&& !parse_error
) {
136 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err
);
137 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql
);
142 static int replace_count
;
143 static char **replace_table
;
144 static const char *replace_return_ranges(const char *return_ranges
)
148 if (!get_function()) {
149 /* I have no idea why EXPORT_SYMBOL() is here */
150 return return_ranges
;
152 for (i
= 0; i
< replace_count
; i
+= 3) {
153 if (strcmp(replace_table
[i
+ 0], get_function()) == 0) {
154 if (strcmp(replace_table
[i
+ 1], return_ranges
) == 0)
155 return replace_table
[i
+ 2];
158 return return_ranges
;
162 static char *use_states
;
163 static int get_db_state_count(void)
168 FOR_EACH_SM(__get_cur_stree(), sm
) {
169 if (sm
->owner
== USHRT_MAX
)
171 if (use_states
[sm
->owner
])
173 } END_FOR_EACH_SM(sm
);
177 void db_ignore_states(int id
)
182 void sql_insert_return_states(int return_id
, const char *return_ranges
,
183 int type
, int param
, const char *key
, const char *value
)
185 if (key
&& strlen(key
) >= 80)
187 return_ranges
= replace_return_ranges(return_ranges
);
188 sql_insert(return_states
, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
189 get_base_file(), get_function(), (unsigned long)__inline_fn
,
190 return_id
, return_ranges
, fn_static(), type
, param
, key
, value
);
193 static struct string_list
*common_funcs
;
194 static int is_common_function(const char *fn
)
201 if (strncmp(fn
, "__builtin_", 10) == 0)
204 FOR_EACH_PTR(common_funcs
, tmp
) {
205 if (strcmp(tmp
, fn
) == 0)
207 } END_FOR_EACH_PTR(tmp
);
212 static char *function_signature(void)
214 return type_to_str(get_real_base_type(cur_func_sym
));
217 void sql_insert_caller_info(struct expression
*call
, int type
,
218 int param
, const char *key
, const char *value
)
220 FILE *tmp_fd
= sm_outfd
;
223 if (!option_info
&& !__inline_call
)
226 if (key
&& strlen(key
) >= 80)
229 fn
= get_fnptr_name(call
->fn
);
235 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
236 get_base_file(), get_function(), fn
, (unsigned long)call
,
237 is_static(call
->fn
), type
, param
, key
, value
);
243 if (strncmp(fn
, "__builtin_", 10) == 0)
245 if (type
!= INTERNAL
&& is_common_function(fn
))
248 sm_outfd
= caller_info_fd
;
249 sm_msg("SQL_caller_info: insert into caller_info values ("
250 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
251 get_base_file(), get_function(), fn
, is_static(call
->fn
),
252 type
, param
, key
, value
);
258 void sql_insert_function_ptr(const char *fn
, const char *struct_name
)
260 sql_insert_or_ignore(function_ptr
, "'%s', '%s', '%s', 0",
261 get_base_file(), fn
, struct_name
);
264 void sql_insert_return_implies(int type
, int param
, const char *key
, const char *value
)
266 sql_insert_or_ignore(return_implies
, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
267 get_base_file(), get_function(), (unsigned long)__inline_fn
,
268 fn_static(), type
, param
, key
, value
);
271 void sql_insert_call_implies(int type
, int param
, const char *key
, const char *value
)
273 sql_insert_or_ignore(call_implies
, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
274 get_base_file(), get_function(), (unsigned long)__inline_fn
,
275 fn_static(), type
, param
, key
, value
);
278 void sql_insert_function_type_size(const char *member
, const char *ranges
)
280 sql_insert(function_type_size
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member
, ranges
);
283 void sql_insert_function_type_info(int type
, const char *struct_type
, const char *member
, const char *value
)
285 sql_insert(function_type_info
, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type
, struct_type
, member
, value
);
288 void sql_insert_type_info(int type
, const char *member
, const char *value
)
290 sql_insert_cache(type_info
, "'%s', %d, '%s', '%s'", get_base_file(), type
, member
, value
);
293 void sql_insert_local_values(const char *name
, const char *value
)
295 sql_insert(local_values
, "'%s', '%s', '%s'", get_base_file(), name
, value
);
298 void sql_insert_function_type_value(const char *type
, const char *value
)
300 sql_insert(function_type_value
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type
, value
);
303 void sql_insert_function_type(int param
, const char *value
)
305 sql_insert(function_type
, "'%s', '%s', %d, %d, '%s'",
306 get_base_file(), get_function(), fn_static(), param
, value
);
309 void sql_insert_parameter_name(int param
, const char *value
)
311 sql_insert(parameter_name
, "'%s', '%s', %d, %d, '%s'",
312 get_base_file(), get_function(), fn_static(), param
, value
);
315 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
)
319 data_name
= get_data_info_name(data
);
322 sql_insert(data_info
, "'%s', '%s', %d, '%s'",
323 is_static(data
) ? get_base_file() : "extern",
324 data_name
, type
, value
);
327 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
)
329 sql_insert(data_info
, "'%s', '%s', %d, '%s'",
330 (sym
->ctype
.modifiers
& MOD_STATIC
) ? get_base_file() : "extern",
334 void sql_save_constraint(const char *con
)
339 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con
));
342 void sql_save_constraint_required(const char *data
, int op
, const char *limit
)
344 sql_insert_or_ignore(constraints_required
, "'%s', '%s', '%s'", data
, show_special(op
), limit
);
347 void sql_copy_constraint_required(const char *new_limit
, const char *old_limit
)
352 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
353 "select constraints_required.data, constraints_required.op, '%s' from "
354 "constraints_required where bound = '%s';", new_limit
, old_limit
);
357 void sql_insert_fn_ptr_data_link(const char *ptr
, const char *data
)
359 sql_insert_or_ignore(fn_ptr_data_link
, "'%s', '%s'", ptr
, data
);
362 void sql_insert_fn_data_link(struct expression
*fn
, int type
, int param
, const char *key
, const char *value
)
364 if (fn
->type
!= EXPR_SYMBOL
|| !fn
->symbol
->ident
)
367 sql_insert(fn_data_link
, "'%s', '%s', %d, %d, %d, '%s', '%s'",
368 (fn
->symbol
->ctype
.modifiers
& MOD_STATIC
) ? get_base_file() : "extern",
369 fn
->symbol
->ident
->name
,
370 !!(fn
->symbol
->ctype
.modifiers
& MOD_STATIC
),
371 type
, param
, key
, value
);
374 void sql_insert_mtag_about(mtag_t tag
, const char *left_name
, const char *right_name
)
376 sql_insert(mtag_about
, "%lld, '%s', '%s', %d, '%s', '%s'",
377 tag
, get_filename(), get_function(), get_lineno(), left_name
, right_name
);
380 void sql_insert_mtag_map(mtag_t tag
, int tag_offset
, int container_offset
, mtag_t container
)
382 sql_insert(mtag_map
, "%lld, %d, %d, %lld", tag
, tag_offset
, container_offset
, container
);
385 void sql_insert_mtag_alias(mtag_t orig
, mtag_t alias
)
387 sql_insert(mtag_alias
, "%lld, %lld", orig
, alias
);
390 static int save_mtag(void *_tag
, int argc
, char **argv
, char **azColName
)
392 mtag_t
*saved_tag
= _tag
;
395 new_tag
= strtoll(argv
[0], NULL
, 10);
398 *saved_tag
= new_tag
;
399 else if (*saved_tag
!= new_tag
)
405 int mtag_map_select_container(mtag_t tag
, int offset
, mtag_t
*container
)
409 run_sql(save_mtag
, &tmp
,
410 "select container from mtag_map where tag = %lld and container_offset = %d and tag_offset = 0;",
413 if (tmp
== 0 || tmp
== -1ULL)
419 int mtag_map_select_tag(mtag_t container
, int offset
, mtag_t
*tag
)
423 run_sql(save_mtag
, &tmp
,
424 "select tag from mtag_map where container = %lld and offset = %d;",
427 if (tmp
== 0 || tmp
== -1ULL)
433 char *get_static_filter(struct symbol
*sym
)
435 static char sql_filter
[1024];
437 /* This can only happen on buggy code. Return invalid SQL. */
439 sql_filter
[0] = '\0';
443 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
444 snprintf(sql_filter
, sizeof(sql_filter
),
445 "file = '%s' and function = '%s' and static = '1'",
446 get_base_file(), sym
->ident
->name
);
448 snprintf(sql_filter
, sizeof(sql_filter
),
449 "function = '%s' and static = '0'", sym
->ident
->name
);
455 static int get_row_count(void *_row_count
, int argc
, char **argv
, char **azColName
)
457 int *row_count
= _row_count
;
462 *row_count
= atoi(argv
[0]);
466 static void mark_call_params_untracked(struct expression
*call
)
468 struct expression
*arg
;
471 FOR_EACH_PTR(call
->args
, arg
) {
472 mark_untracked(call
, i
++, "$", NULL
);
473 } END_FOR_EACH_PTR(arg
);
476 static void sql_select_return_states_pointer(const char *cols
,
477 struct expression
*call
, int (*callback
)(void*, int, char**, char**), void *info
)
480 int return_count
= 0;
482 ptr
= get_fnptr_name(call
->fn
);
486 run_sql(get_row_count
, &return_count
,
487 "select count(*) from return_states join function_ptr "
488 "where return_states.function == function_ptr.function and "
489 "ptr = '%s' and searchable = 1 and type = %d;", ptr
, INTERNAL
);
490 /* The magic number 100 is just from testing on the kernel. */
491 if (return_count
> 100) {
492 mark_call_params_untracked(call
);
496 run_sql(callback
, info
,
497 "select %s from return_states join function_ptr where "
498 "return_states.function == function_ptr.function and ptr = '%s' "
499 "and searchable = 1 "
500 "order by function_ptr.file, return_states.file, return_id, type;",
504 static int is_local_symbol(struct expression
*expr
)
506 if (expr
->type
!= EXPR_SYMBOL
)
508 if (expr
->symbol
->ctype
.modifiers
& (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
))
513 void sql_select_return_states(const char *cols
, struct expression
*call
,
514 int (*callback
)(void*, int, char**, char**), void *info
)
516 struct expression
*fn
;
519 if (is_fake_call(call
))
522 fn
= strip_expr(call
->fn
);
523 if (fn
->type
!= EXPR_SYMBOL
|| !fn
->symbol
|| is_local_symbol(fn
)) {
524 sql_select_return_states_pointer(cols
, call
, callback
, info
);
529 mem_sql(callback
, info
,
530 "select %s from return_states where call_id = '%lu' order by return_id, type;",
531 cols
, (unsigned long)call
);
535 run_sql(get_row_count
, &row_count
, "select count(*) from return_states where %s;",
536 get_static_filter(fn
->symbol
));
537 if (row_count
> 3000)
540 run_sql(callback
, info
, "select %s from return_states where %s order by file, return_id, type;",
541 cols
, get_static_filter(fn
->symbol
));
544 #define CALL_IMPLIES 0
545 #define RETURN_IMPLIES 1
547 struct implies_info
{
549 struct db_implies_cb_list
*cb_list
;
550 struct expression
*expr
;
554 void sql_select_implies(const char *cols
, struct implies_info
*info
,
555 int (*callback
)(void*, int, char**, char**))
557 if (info
->type
== RETURN_IMPLIES
&& inlinable(info
->expr
->fn
)) {
558 mem_sql(callback
, info
,
559 "select %s from return_implies where call_id = '%lu';",
560 cols
, (unsigned long)info
->expr
);
564 run_sql(callback
, info
, "select %s from %s_implies where %s;",
566 info
->type
== CALL_IMPLIES
? "call" : "return",
567 get_static_filter(info
->sym
));
570 struct select_caller_info_data
{
571 struct stree
*final_states
;
572 struct timeval start_time
;
578 static int caller_info_callback(void *_data
, int argc
, char **argv
, char **azColName
);
580 static void sql_select_caller_info(struct select_caller_info_data
*data
,
581 const char *cols
, struct symbol
*sym
)
584 mem_sql(caller_info_callback
, data
,
585 "select %s from caller_info where call_id = %lu;",
586 cols
, (unsigned long)__inline_fn
);
590 if (sym
->ident
->name
&& is_common_function(sym
->ident
->name
))
592 run_sql(caller_info_callback
, data
,
593 "select %s from common_caller_info where %s order by call_id;",
594 cols
, get_static_filter(sym
));
598 run_sql(caller_info_callback
, data
,
599 "select %s from caller_info where %s order by call_id;",
600 cols
, get_static_filter(sym
));
603 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
605 struct def_callback
*def_callback
= __alloc_def_callback(0);
607 def_callback
->hook_type
= type
;
608 def_callback
->callback
= callback
;
609 add_ptr_list(&select_caller_info_callbacks
, def_callback
);
613 * These call backs are used when the --info option is turned on to print struct
614 * member information. For example foo->bar could have a state in
615 * smatch_extra.c and also check_user.c.
617 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
619 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
621 member_callback
->owner
= owner
;
622 member_callback
->callback
= callback
;
623 add_ptr_list(&member_callbacks
, member_callback
);
626 void add_caller_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
628 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
630 member_callback
->owner
= owner
;
631 member_callback
->callback
= callback
;
632 add_ptr_list(&member_callbacks_new
, member_callback
);
635 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
637 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
639 callback
->callback
= fn
;
640 add_ptr_list(&returned_state_callbacks
, callback
);
643 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
))
645 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
647 member_callback
->owner
= owner
;
648 member_callback
->callback
= callback
;
649 add_ptr_list(&returned_member_callbacks
, member_callback
);
652 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
))
654 struct db_implies_callback
*cb
= __alloc_db_implies_callback(0);
657 cb
->callback
= callback
;
658 add_ptr_list(&call_implies_cb_list
, cb
);
661 void select_return_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
))
663 struct db_implies_callback
*cb
= __alloc_db_implies_callback(0);
666 cb
->callback
= callback
;
667 add_ptr_list(&return_implies_cb_list
, cb
);
671 struct expression
*static_returns_call
;
672 struct symbol
*return_type
;
673 struct range_list
*return_range_list
;
676 static int db_return_callback(void *_ret_info
, int argc
, char **argv
, char **azColName
)
678 struct return_info
*ret_info
= _ret_info
;
679 struct range_list
*rl
;
680 struct expression
*call_expr
= ret_info
->static_returns_call
;
684 call_results_to_rl(call_expr
, ret_info
->return_type
, argv
[0], &rl
);
685 ret_info
->return_range_list
= rl_union(ret_info
->return_range_list
, rl
);
689 struct range_list
*db_return_vals(struct expression
*expr
)
691 struct return_info ret_info
= {};
695 if (is_fake_call(expr
))
698 snprintf(buf
, sizeof(buf
), "return %p", expr
);
699 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
701 return clone_rl(estate_rl(sm
->state
));
702 ret_info
.static_returns_call
= expr
;
703 ret_info
.return_type
= get_type(expr
);
704 if (!ret_info
.return_type
)
707 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
710 ret_info
.return_range_list
= NULL
;
711 if (inlinable(expr
->fn
)) {
712 mem_sql(db_return_callback
, &ret_info
,
713 "select distinct return from return_states where call_id = '%lu';",
714 (unsigned long)expr
);
716 run_sql(db_return_callback
, &ret_info
,
717 "select distinct return from return_states where %s;",
718 get_static_filter(expr
->fn
->symbol
));
720 return ret_info
.return_range_list
;
723 struct range_list
*db_return_vals_from_str(const char *fn_name
)
725 struct return_info ret_info
;
727 ret_info
.static_returns_call
= NULL
;
728 ret_info
.return_type
= &llong_ctype
;
729 ret_info
.return_range_list
= NULL
;
731 run_sql(db_return_callback
, &ret_info
,
732 "select distinct return from return_states where function = '%s';",
734 return ret_info
.return_range_list
;
738 * This is used when we have a function that takes a function pointer as a
739 * parameter. "frob(blah, blah, my_function);" We know that the return values
740 * from frob() come from my_funcion() so we want to find the possible returns
741 * of my_function(), but we don't know which arguments are passed to it.
744 struct range_list
*db_return_vals_no_args(struct expression
*expr
)
746 struct return_info ret_info
= {};
748 if (!expr
|| expr
->type
!= EXPR_SYMBOL
)
751 ret_info
.static_returns_call
= expr
;
752 ret_info
.return_type
= get_type(expr
);
753 ret_info
.return_type
= get_real_base_type(ret_info
.return_type
);
754 if (!ret_info
.return_type
)
757 run_sql(db_return_callback
, &ret_info
,
758 "select distinct return from return_states where %s;",
759 get_static_filter(expr
->symbol
));
761 return ret_info
.return_range_list
;
764 static void match_call_marker(struct expression
*expr
)
768 type
= get_type(expr
->fn
);
769 if (type
&& type
->type
== SYM_PTR
)
770 type
= get_real_base_type(type
);
773 * we just want to record something in the database so that if we have
774 * two calls like: frob(4); frob(some_unkown); then on the receiving
775 * side we know that sometimes frob is called with unknown parameters.
778 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", type_to_str(type
));
781 int is_recursive_member(const char *name
)
784 const char *p
, *next
;
787 p
= strchr(name
, '>');
792 next
= strchr(p
, '>');
798 if (size
>= sizeof(buf
))
800 memcpy(buf
, p
, size
);
802 if (strstr(next
, buf
))
808 char *sm_to_arg_name(struct expression
*expr
, struct sm_state
*sm
)
813 bool is_address
= false;
814 bool add_star
= false;
819 expr
= strip_expr(expr
);
823 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
824 expr
= strip_expr(expr
->unop
);
828 name
= expr_to_var_sym(expr
, &sym
);
836 if (sm_name
[0] == '*') {
842 if (strncmp(name
, sm_name
, len
) != 0)
844 if (sm_name
[len
] == '\0') {
845 snprintf(buf
, sizeof(buf
), "%s%s$",
846 add_star
? "*" : "", is_address
? "*" : "");
848 if (sm_name
[len
] != '.' && sm_name
[len
] != '-')
850 if (sm_name
[len
] == '-')
852 // FIXME does is_address really imply that sm_name[len] == '-'
853 snprintf(buf
, sizeof(buf
), "%s$->%s", add_star
? "*" : "",
857 ret
= alloc_sname(buf
);
863 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct stree
*stree
,
864 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
),
872 char printed_name
[256];
877 expr
= strip_expr(expr
);
880 type
= get_type(expr
);
881 if (type
&& type_bits(type
) < type_bits(&ulong_ctype
))
884 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
885 expr
= strip_expr(expr
->unop
);
889 name
= expr_to_var_sym(expr
, &sym
);
894 FOR_EACH_SM(stree
, sm
) {
899 if (sm_name
[0] == '*') {
904 if (!add_star
&& strcmp(name
, sm_name
) == 0) {
906 snprintf(printed_name
, sizeof(printed_name
), "*$");
909 snprintf(printed_name
, sizeof(printed_name
), "$");
913 } else if (add_star
&& strcmp(name
, sm_name
) == 0) {
914 snprintf(printed_name
, sizeof(printed_name
), "%s*$",
915 is_address
? "*" : "");
916 } else if (strncmp(name
, sm_name
, len
) == 0) {
917 if (sm_name
[len
] != '.' && sm_name
[len
] != '-')
920 snprintf(printed_name
, sizeof(printed_name
),
921 "%s$->%s", add_star
? "*" : "",
924 snprintf(printed_name
, sizeof(printed_name
),
925 "%s$%s", add_star
? "*" : "",
930 if (is_recursive_member(printed_name
))
932 callback(call
, param
, printed_name
, sm
);
933 } END_FOR_EACH_SM(sm
);
938 static void match_call_info(struct expression
*call
)
940 struct member_info_callback
*cb
;
941 struct expression
*arg
;
946 name
= get_fnptr_name(call
->fn
);
950 FOR_EACH_PTR(member_callbacks
, cb
) {
951 stree
= get_all_states_stree(cb
->owner
);
953 FOR_EACH_PTR(call
->args
, arg
) {
954 print_struct_members(call
, arg
, i
, stree
, cb
->callback
, 0);
956 } END_FOR_EACH_PTR(arg
);
958 } END_FOR_EACH_PTR(cb
);
963 static void match_call_info_new(struct expression
*call
)
965 struct member_info_callback
*cb
;
966 struct expression
*arg
;
971 name
= get_fnptr_name(call
->fn
);
975 FOR_EACH_PTR(member_callbacks_new
, cb
) {
976 stree
= get_all_states_stree(cb
->owner
);
978 FOR_EACH_PTR(call
->args
, arg
) {
979 print_struct_members(call
, arg
, i
, stree
, cb
->callback
, 1);
981 } END_FOR_EACH_PTR(arg
);
983 } END_FOR_EACH_PTR(cb
);
988 static int get_param(int param
, char **name
, struct symbol
**sym
)
994 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
996 * this is a temporary hack to work around a bug (I think in sparse?)
997 * 2.6.37-rc1:fs/reiserfs/journal.o
998 * If there is a function definition without parameter name found
999 * after a function implementation then it causes a crash.
1003 if (arg
->ident
->name
< (char *)100)
1006 *name
= arg
->ident
->name
;
1011 } END_FOR_EACH_PTR(arg
);
1016 static int function_signature_matches(const char *sig
)
1020 my_sig
= function_signature();
1021 if (!sig
|| !my_sig
)
1022 return 1; /* default to matching */
1023 if (strcmp(my_sig
, sig
) == 0)
1028 static int caller_info_callback(void *_data
, int argc
, char **argv
, char **azColName
)
1030 struct select_caller_info_data
*data
= _data
;
1037 struct symbol
*sym
= NULL
;
1038 struct def_callback
*def_callback
;
1039 struct stree
*stree
;
1040 struct timeval cur_time
;
1047 gettimeofday(&cur_time
, NULL
);
1048 if (cur_time
.tv_sec
- data
->start_time
.tv_sec
> 10)
1051 func_id
= atoi(argv
[0]);
1053 type
= strtol(argv
[1], NULL
, 10);
1054 param
= strtol(argv
[2], NULL
, 10);
1060 if (data
->prev_func_id
== -1)
1061 data
->prev_func_id
= func_id
;
1062 if (func_id
!= data
->prev_func_id
) {
1063 stree
= __pop_fake_cur_stree();
1065 merge_stree(&data
->final_states
, stree
);
1067 __push_fake_cur_stree();
1069 data
->prev_func_id
= func_id
;
1075 if (type
== INTERNAL
&&
1076 !function_signature_matches(value
)) {
1081 if (param
>= 0 && !get_param(param
, &name
, &sym
))
1084 FOR_EACH_PTR(select_caller_info_callbacks
, def_callback
) {
1085 if (def_callback
->hook_type
== type
)
1086 def_callback
->callback(name
, sym
, key
, value
);
1087 } END_FOR_EACH_PTR(def_callback
);
1092 static struct string_list
*ptr_names_done
;
1093 static struct string_list
*ptr_names
;
1095 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
1097 insert_string(&ptr_names
, alloc_string(argv
[0]));
1101 static char *get_next_ptr_name(void)
1105 FOR_EACH_PTR(ptr_names
, ptr
) {
1106 if (!insert_string(&ptr_names_done
, ptr
))
1109 } END_FOR_EACH_PTR(ptr
);
1113 static void get_ptr_names(const char *file
, const char *name
)
1115 char sql_filter
[1024];
1119 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
1122 snprintf(sql_filter
, 1024, "function = '%s';", name
);
1125 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
1127 run_sql(get_ptr_name
, NULL
,
1128 "select distinct ptr from function_ptr where %s",
1131 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
1132 if (before
== after
)
1135 while ((name
= get_next_ptr_name()))
1136 get_ptr_names(NULL
, name
);
1139 static void match_data_from_db(struct symbol
*sym
)
1141 struct select_caller_info_data data
= { .prev_func_id
= -1 };
1142 struct sm_state
*sm
;
1143 struct stree
*stree
;
1144 struct timeval end_time
;
1146 if (!sym
|| !sym
->ident
)
1149 gettimeofday(&data
.start_time
, NULL
);
1151 __push_fake_cur_stree();
1157 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1158 get_ptr_names(get_base_file(), sym
->ident
->name
);
1160 get_ptr_names(NULL
, sym
->ident
->name
);
1162 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
1163 __free_ptr_list((struct ptr_list
**)&ptr_names
);
1164 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
1165 __free_fake_cur_stree();
1169 sql_select_caller_info(&data
,
1170 "call_id, type, parameter, key, value",
1174 stree
= __pop_fake_cur_stree();
1176 merge_stree(&data
.final_states
, stree
);
1178 __push_fake_cur_stree();
1180 data
.prev_func_id
= -1;
1184 FOR_EACH_PTR(ptr_names
, ptr
) {
1185 run_sql(caller_info_callback
, &data
,
1186 "select call_id, type, parameter, key, value"
1187 " from common_caller_info where function = '%s' order by call_id",
1189 } END_FOR_EACH_PTR(ptr
);
1192 FOR_EACH_PTR(ptr_names
, ptr
) {
1194 } END_FOR_EACH_PTR(ptr
);
1195 goto free_ptr_names
;
1198 FOR_EACH_PTR(ptr_names
, ptr
) {
1199 run_sql(caller_info_callback
, &data
,
1200 "select call_id, type, parameter, key, value"
1201 " from caller_info where function = '%s' order by call_id",
1204 } END_FOR_EACH_PTR(ptr
);
1207 __free_ptr_list((struct ptr_list
**)&ptr_names
);
1208 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
1210 sql_select_caller_info(&data
,
1211 "call_id, type, parameter, key, value",
1215 stree
= __pop_fake_cur_stree();
1217 merge_stree(&data
.final_states
, stree
);
1220 gettimeofday(&end_time
, NULL
);
1221 if (end_time
.tv_sec
- data
.start_time
.tv_sec
<= 10) {
1222 FOR_EACH_SM(data
.final_states
, sm
) {
1224 } END_FOR_EACH_SM(sm
);
1227 free_stree(&data
.final_states
);
1230 static int return_implies_callbacks(void *_info
, int argc
, char **argv
, char **azColName
)
1232 struct implies_info
*info
= _info
;
1233 struct db_implies_callback
*cb
;
1234 struct expression
*arg
= NULL
;
1241 type
= atoi(argv
[1]);
1242 param
= atoi(argv
[2]);
1244 FOR_EACH_PTR(info
->cb_list
, cb
) {
1245 if (cb
->type
!= type
)
1248 arg
= get_argument_from_call_expr(info
->expr
->args
, param
);
1252 cb
->callback(info
->expr
, arg
, argv
[3], argv
[4]);
1253 } END_FOR_EACH_PTR(cb
);
1258 static int call_implies_callbacks(void *_info
, int argc
, char **argv
, char **azColName
)
1260 struct implies_info
*info
= _info
;
1261 struct db_implies_callback
*cb
;
1262 struct expression
*arg
;
1271 type
= atoi(argv
[1]);
1272 param
= atoi(argv
[2]);
1274 if (!get_param(param
, &name
, &sym
))
1276 arg
= symbol_expression(sym
);
1280 FOR_EACH_PTR(info
->cb_list
, cb
) {
1281 if (cb
->type
!= type
)
1283 cb
->callback(info
->expr
, arg
, argv
[3], argv
[4]);
1284 } END_FOR_EACH_PTR(cb
);
1289 static void match_return_implies(struct expression
*expr
)
1291 struct implies_info info
= {
1292 .type
= RETURN_IMPLIES
,
1293 .cb_list
= return_implies_cb_list
,
1296 if (expr
->fn
->type
!= EXPR_SYMBOL
||
1300 info
.sym
= expr
->fn
->symbol
;
1301 sql_select_implies("function, type, parameter, key, value", &info
,
1302 return_implies_callbacks
);
1305 static void match_call_implies(struct symbol
*sym
)
1307 struct implies_info info
= {
1308 .type
= CALL_IMPLIES
,
1309 .cb_list
= call_implies_cb_list
,
1312 if (!sym
|| !sym
->ident
)
1316 sql_select_implies("function, type, parameter, key, value", &info
,
1317 call_implies_callbacks
);
1320 static char *get_fn_param_str(struct expression
*expr
)
1322 struct expression
*tmp
;
1326 tmp
= get_assigned_expr(expr
);
1329 expr
= strip_expr(expr
);
1330 if (!expr
|| expr
->type
!= EXPR_CALL
)
1332 expr
= strip_expr(expr
->fn
);
1333 if (!expr
|| expr
->type
!= EXPR_SYMBOL
)
1335 param
= get_param_num(expr
);
1339 snprintf(buf
, sizeof(buf
), "[r $%d]", param
);
1340 return alloc_sname(buf
);
1343 static char *get_return_compare_is_param(struct expression
*expr
)
1350 param
= get_param_num(expr
);
1354 var
= expr_to_var(expr
);
1357 snprintf(buf
, sizeof(buf
), "%s orig", var
);
1358 comparison
= get_comparison_strings(var
, buf
);
1364 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(comparison
), param
);
1365 return alloc_sname(buf
);
1368 static char *get_return_compare_str(struct expression
*expr
)
1372 compare_str
= get_return_compare_is_param(expr
);
1376 compare_str
= expr_lte_to_param(expr
, -1);
1380 return expr_param_comparison(expr
, -1);
1383 static const char *get_return_ranges_str(struct expression
*expr
, struct range_list
**rl_p
)
1385 struct range_list
*rl
;
1386 char *return_ranges
;
1396 return alloc_sname("");
1398 if (get_implied_value(expr
, &sval
)) {
1399 sval
= sval_cast(cur_func_return_type(), sval
);
1400 *rl_p
= alloc_rl(sval
, sval
);
1401 return sval_to_str_or_err_ptr(sval
);
1404 fn_param_str
= get_fn_param_str(expr
);
1405 compare_str
= expr_equal_to_param(expr
, -1);
1406 math_str
= get_value_in_terms_of_parameter_math(expr
);
1408 if (get_implied_rl(expr
, &rl
) && !is_whole_rl(rl
)) {
1409 rl
= cast_rl(cur_func_return_type(), rl
);
1410 return_ranges
= show_rl(rl
);
1411 } else if (get_imaginary_absolute(expr
, &rl
)){
1412 rl
= cast_rl(cur_func_return_type(), rl
);
1413 return alloc_sname(show_rl(rl
));
1415 get_absolute_rl(expr
, &rl
);
1416 rl
= cast_rl(cur_func_return_type(), rl
);
1417 return_ranges
= show_rl(rl
);
1422 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, fn_param_str
);
1423 return alloc_sname(buf
);
1426 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1427 return alloc_sname(buf
);
1430 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1431 return alloc_sname(buf
);
1433 compare_str
= get_return_compare_str(expr
);
1435 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1436 return alloc_sname(buf
);
1439 return return_ranges
;
1442 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
1444 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", function_signature());
1447 static bool call_return_state_hooks_conditional(struct expression
*expr
)
1449 int final_pass_orig
= final_pass
;
1455 (expr
->type
!= EXPR_CONDITIONAL
&& expr
->type
!= EXPR_SELECT
))
1460 __push_fake_cur_stree();
1463 __split_whole_condition(expr
->conditional
);
1464 final_pass
= final_pass_orig
;
1466 call_return_state_hooks(expr
->cond_true
?: expr
->conditional
);
1468 __push_true_states();
1469 __use_false_states();
1471 call_return_state_hooks(expr
->cond_false
);
1473 __merge_true_states();
1474 __free_fake_cur_stree();
1480 static void call_return_state_hooks_compare(struct expression
*expr
)
1482 struct returned_state_callback
*cb
;
1483 char *return_ranges
;
1484 int final_pass_orig
= final_pass
;
1485 sval_t sval
= { .type
= &int_ctype
};
1488 if (!get_implied_value(expr
, &ret
))
1491 __push_fake_cur_stree();
1494 __split_whole_condition(expr
);
1495 final_pass
= final_pass_orig
;
1497 if (ret
.value
!= 0) {
1498 return_ranges
= alloc_sname("1");
1500 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1503 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1504 cb
->callback(return_id
, return_ranges
, expr
);
1505 } END_FOR_EACH_PTR(cb
);
1508 __push_true_states();
1509 __use_false_states();
1511 if (ret
.value
!= 1) {
1512 return_ranges
= alloc_sname("0");
1514 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
1517 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1518 cb
->callback(return_id
, return_ranges
, expr
);
1519 } END_FOR_EACH_PTR(cb
);
1522 __merge_true_states();
1523 __free_fake_cur_stree();
1526 static int ptr_in_list(struct sm_state
*sm
, struct state_list
*slist
)
1528 struct sm_state
*tmp
;
1530 FOR_EACH_PTR(slist
, tmp
) {
1531 if (strcmp(tmp
->state
->name
, sm
->state
->name
) == 0)
1533 } END_FOR_EACH_PTR(tmp
);
1538 static int split_possible_helper(struct sm_state
*sm
, struct expression
*expr
)
1540 struct returned_state_callback
*cb
;
1541 struct range_list
*rl
;
1542 char *return_ranges
;
1543 struct sm_state
*tmp
;
1545 int nr_possible
, nr_states
;
1548 struct state_list
*already_handled
= NULL
;
1551 if (!sm
|| !sm
->merged
)
1554 if (too_many_possible(sm
))
1557 /* bail if it gets too complicated */
1559 FOR_EACH_PTR(sm
->possible
, tmp
) {
1562 if (ptr_in_list(tmp
, already_handled
))
1564 add_ptr_list(&already_handled
, tmp
);
1566 } END_FOR_EACH_PTR(tmp
);
1567 free_slist(&already_handled
);
1568 nr_states
= get_db_state_count();
1569 if (nr_states
* nr_possible
>= 2000)
1572 FOR_EACH_PTR(sm
->possible
, tmp
) {
1575 if (ptr_in_list(tmp
, already_handled
))
1577 add_ptr_list(&already_handled
, tmp
);
1580 __push_fake_cur_stree();
1582 overwrite_states_using_pool(sm
, tmp
);
1584 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
1585 return_ranges
= show_rl(rl
);
1586 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(clone_rl(rl
)));
1587 if (!rl_to_sval(rl
, &sval
)) {
1588 compare_str
= get_return_compare_str(expr
);
1590 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1591 return_ranges
= alloc_sname(buf
);
1596 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1597 cb
->callback(return_id
, return_ranges
, expr
);
1598 } END_FOR_EACH_PTR(cb
);
1600 __free_fake_cur_stree();
1601 } END_FOR_EACH_PTR(tmp
);
1603 free_slist(&already_handled
);
1608 static int call_return_state_hooks_split_possible(struct expression
*expr
)
1610 struct sm_state
*sm
;
1612 if (!expr
|| expr_equal_to_param(expr
, -1))
1615 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1616 return split_possible_helper(sm
, expr
);
1619 static bool has_possible_negative(struct sm_state
*sm
)
1621 struct sm_state
*tmp
;
1623 if (!type_signed(estate_type(sm
->state
)))
1626 FOR_EACH_PTR(sm
->possible
, tmp
) {
1627 if (!estate_rl(tmp
->state
))
1629 if (sval_is_negative(estate_min(tmp
->state
)) &&
1630 sval_is_negative(estate_max(tmp
->state
)))
1632 } END_FOR_EACH_PTR(tmp
);
1637 static bool has_separate_zero_null(struct sm_state
*sm
)
1639 struct sm_state
*tmp
;
1642 FOR_EACH_PTR(sm
->possible
, tmp
) {
1643 if (!estate_get_single_value(tmp
->state
, &sval
))
1645 if (sval
.value
== 0)
1647 } END_FOR_EACH_PTR(tmp
);
1652 static int split_positive_from_negative(struct expression
*expr
)
1654 struct sm_state
*sm
;
1655 struct returned_state_callback
*cb
;
1656 struct range_list
*rl
;
1657 const char *return_ranges
;
1658 struct range_list
*ret_rl
;
1662 /* We're going to print the states 3 times */
1663 if (get_db_state_count() > 10000 / 3)
1666 if (!get_implied_rl(expr
, &rl
) || !rl
)
1668 /* Forget about INT_MAX and larger */
1669 if (rl_max(rl
).value
<= 0)
1671 if (!sval_is_negative(rl_min(rl
)))
1674 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1677 if (!has_possible_negative(sm
))
1679 separate_zero
= has_separate_zero_null(sm
);
1681 if (!assume(compare_expression(expr
, separate_zero
? '>' : SPECIAL_GTE
, zero_expr())))
1685 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1686 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1687 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1688 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1689 } END_FOR_EACH_PTR(cb
);
1693 if (separate_zero
) {
1694 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
1697 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1698 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1699 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1700 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1701 } END_FOR_EACH_PTR(cb
);
1707 undo
= assume(compare_expression(expr
, '<', zero_expr()));
1710 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1711 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1712 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1713 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1714 } END_FOR_EACH_PTR(cb
);
1722 static int call_return_state_hooks_split_null_non_null_zero(struct expression
*expr
)
1724 struct returned_state_callback
*cb
;
1725 struct range_list
*rl
;
1726 struct range_list
*nonnull_rl
;
1728 struct range_list
*null_rl
= NULL
;
1729 char *return_ranges
;
1730 struct sm_state
*sm
;
1731 struct smatch_state
*state
;
1733 int final_pass_orig
= final_pass
;
1735 if (!expr
|| expr_equal_to_param(expr
, -1))
1737 if (expr
->type
== EXPR_CALL
)
1740 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1743 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
1746 if (!estate_rl(state
))
1748 if (estate_min(state
).value
== 0 && estate_max(state
).value
== 0)
1750 if (!has_separate_zero_null(sm
))
1753 nr_states
= get_db_state_count();
1754 if (option_info
&& nr_states
>= 1500)
1757 rl
= estate_rl(state
);
1759 __push_fake_cur_stree();
1762 __split_whole_condition(expr
);
1763 final_pass
= final_pass_orig
;
1765 nonnull_rl
= rl_filter(rl
, rl_zero());
1766 return_ranges
= show_rl(nonnull_rl
);
1767 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonnull_rl
));
1770 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1771 cb
->callback(return_id
, return_ranges
, expr
);
1772 } END_FOR_EACH_PTR(cb
);
1774 __push_true_states();
1775 __use_false_states();
1777 return_ranges
= alloc_sname("0");
1778 null_sval
= sval_type_val(rl_type(rl
), 0);
1779 add_range(&null_rl
, null_sval
, null_sval
);
1780 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(null_rl
));
1782 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1783 cb
->callback(return_id
, return_ranges
, expr
);
1784 } END_FOR_EACH_PTR(cb
);
1786 __merge_true_states();
1787 __free_fake_cur_stree();
1792 static bool is_kernel_success_fail(struct sm_state
*sm
)
1794 struct sm_state
*tmp
;
1795 struct range_list
*rl
;
1796 bool has_zero
= false;
1797 bool has_neg
= false;
1799 if (!type_signed(estate_type(sm
->state
)))
1802 FOR_EACH_PTR(sm
->possible
, tmp
) {
1803 rl
= estate_rl(tmp
->state
);
1806 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 0) {
1811 if (rl_min(rl
).value
>= -4095 && rl_max(rl
).value
< 0)
1813 if (strcmp(tmp
->state
->name
, "s32min-(-1)") == 0)
1815 if (strcmp(tmp
->state
->name
, "s32min-(-1),1-s32max") == 0)
1818 } END_FOR_EACH_PTR(tmp
);
1820 return has_zero
&& has_neg
;
1823 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
1825 struct sm_state
*sm
;
1826 struct range_list
*rl
;
1827 struct range_list
*nonzero_rl
;
1829 struct range_list
*zero_rl
= NULL
;
1831 struct returned_state_callback
*cb
;
1832 char *return_ranges
;
1833 int final_pass_orig
= final_pass
;
1835 if (option_project
!= PROJ_KERNEL
)
1838 nr_states
= get_db_state_count();
1839 if (nr_states
> 2000)
1842 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1845 if (ptr_list_size((struct ptr_list
*)sm
->possible
) == 1)
1847 if (!is_kernel_success_fail(sm
))
1850 rl
= estate_rl(sm
->state
);
1854 __push_fake_cur_stree();
1857 __split_whole_condition(expr
);
1858 final_pass
= final_pass_orig
;
1860 nonzero_rl
= rl_filter(rl
, rl_zero());
1861 nonzero_rl
= cast_rl(cur_func_return_type(), nonzero_rl
);
1862 return_ranges
= show_rl(nonzero_rl
);
1863 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonzero_rl
));
1866 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1867 cb
->callback(return_id
, return_ranges
, expr
);
1868 } END_FOR_EACH_PTR(cb
);
1870 __push_true_states();
1871 __use_false_states();
1873 return_ranges
= alloc_sname("0");
1874 zero_sval
= sval_type_val(rl_type(rl
), 0);
1875 add_range(&zero_rl
, zero_sval
, zero_sval
);
1876 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(zero_rl
));
1878 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1879 cb
->callback(return_id
, return_ranges
, expr
);
1880 } END_FOR_EACH_PTR(cb
);
1882 __merge_true_states();
1883 __free_fake_cur_stree();
1888 static int is_boolean(struct expression
*expr
)
1890 struct range_list
*rl
;
1892 if (!get_implied_rl(expr
, &rl
))
1894 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
1899 static int splitable_function_call(struct expression
*expr
)
1901 struct sm_state
*sm
;
1904 if (!expr
|| expr
->type
!= EXPR_CALL
)
1906 snprintf(buf
, sizeof(buf
), "return %p", expr
);
1907 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
1908 return split_possible_helper(sm
, expr
);
1911 static struct sm_state
*find_bool_param(void)
1913 struct stree
*start_states
;
1915 struct sm_state
*sm
, *tmp
;
1918 start_states
= get_start_states();
1920 FOR_EACH_PTR_REVERSE(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
1923 sm
= get_sm_state_stree(start_states
, SMATCH_EXTRA
, arg
->ident
->name
, arg
);
1926 if (rl_min(estate_rl(sm
->state
)).value
!= 0 ||
1927 rl_max(estate_rl(sm
->state
)).value
!= 1)
1930 } END_FOR_EACH_PTR_REVERSE(arg
);
1936 * Check if it's splitable. If not, then splitting it up is likely not
1937 * useful for the callers.
1939 FOR_EACH_PTR(sm
->possible
, tmp
) {
1942 if (!estate_get_single_value(tmp
->state
, &sval
))
1944 } END_FOR_EACH_PTR(tmp
);
1949 static int split_on_bool_sm(struct sm_state
*sm
, struct expression
*expr
)
1951 struct returned_state_callback
*cb
;
1952 struct range_list
*ret_rl
;
1953 const char *return_ranges
;
1954 struct sm_state
*tmp
;
1956 struct state_list
*already_handled
= NULL
;
1958 if (!sm
|| !sm
->merged
)
1961 if (too_many_possible(sm
))
1964 FOR_EACH_PTR(sm
->possible
, tmp
) {
1967 if (ptr_in_list(tmp
, already_handled
))
1969 add_ptr_list(&already_handled
, tmp
);
1972 __push_fake_cur_stree();
1974 overwrite_states_using_pool(sm
, tmp
);
1976 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1977 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1979 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1980 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1981 } END_FOR_EACH_PTR(cb
);
1983 __free_fake_cur_stree();
1984 } END_FOR_EACH_PTR(tmp
);
1986 free_slist(&already_handled
);
1991 static int split_by_bool_param(struct expression
*expr
)
1993 struct sm_state
*start_sm
, *sm
;
1996 start_sm
= find_bool_param();
1999 sm
= get_sm_state(SMATCH_EXTRA
, start_sm
->name
, start_sm
->sym
);
2000 if (!sm
|| estate_get_single_value(sm
->state
, &sval
))
2003 if (get_db_state_count() * 2 >= 2000)
2006 return split_on_bool_sm(sm
, expr
);
2009 static int split_by_null_nonnull_param(struct expression
*expr
)
2012 struct sm_state
*sm
;
2015 /* function must only take one pointer */
2016 if (ptr_list_size((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
) != 1)
2018 arg
= first_ptr_list((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
);
2021 if (get_real_base_type(arg
)->type
!= SYM_PTR
)
2024 if (param_was_set_var_sym(arg
->ident
->name
, arg
))
2026 sm
= get_sm_state(SMATCH_EXTRA
, arg
->ident
->name
, arg
);
2030 if (!has_separate_zero_null(sm
))
2033 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
2034 if (get_db_state_count() * nr_possible
>= 2000)
2037 return split_on_bool_sm(sm
, expr
);
2040 struct expression
*strip_expr_statement(struct expression
*expr
)
2042 struct expression
*orig
= expr
;
2043 struct statement
*stmt
, *last_stmt
;
2047 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '(')
2049 if (expr
->type
!= EXPR_STATEMENT
)
2051 stmt
= expr
->statement
;
2052 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
2055 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
2056 if (!last_stmt
|| last_stmt
->type
== STMT_LABEL
)
2057 last_stmt
= last_stmt
->label_statement
;
2058 if (!last_stmt
|| last_stmt
->type
!= STMT_EXPRESSION
)
2060 return strip_expr(last_stmt
->expression
);
2063 static void call_return_state_hooks(struct expression
*expr
)
2065 struct returned_state_callback
*cb
;
2066 struct range_list
*ret_rl
;
2067 const char *return_ranges
;
2071 if (__path_is_null())
2074 expr
= strip_expr(expr
);
2075 expr
= strip_expr_statement(expr
);
2077 if (is_impossible_path())
2080 if (expr
&& (expr
->type
== EXPR_COMPARE
||
2081 !get_implied_value(expr
, &sval
)) &&
2082 (is_condition(expr
) || is_boolean(expr
))) {
2083 call_return_state_hooks_compare(expr
);
2085 } else if (call_return_state_hooks_conditional(expr
)) {
2087 } else if (call_return_state_hooks_split_possible(expr
)) {
2089 } else if (split_positive_from_negative(expr
)) {
2091 } else if (call_return_state_hooks_split_null_non_null_zero(expr
)) {
2093 } else if (call_return_state_hooks_split_success_fail(expr
)) {
2095 } else if (splitable_function_call(expr
)) {
2097 } else if (split_by_bool_param(expr
)) {
2098 } else if (split_by_null_nonnull_param(expr
)) {
2103 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
2104 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
2107 nr_states
= get_db_state_count();
2108 if (nr_states
>= 10000) {
2109 match_return_info(return_id
, (char *)return_ranges
, expr
);
2110 print_limited_param_set(return_id
, (char *)return_ranges
, expr
);
2111 mark_all_params_untracked(return_id
, (char *)return_ranges
, expr
);
2114 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
2115 cb
->callback(return_id
, (char *)return_ranges
, expr
);
2116 } END_FOR_EACH_PTR(cb
);
2119 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
2121 struct returned_member_callback
*cb
;
2122 struct stree
*stree
;
2123 struct sm_state
*sm
;
2124 struct symbol
*type
;
2126 char member_name
[256];
2129 type
= get_type(expr
);
2130 if (!type
|| type
->type
!= SYM_PTR
)
2132 name
= expr_to_var(expr
);
2136 member_name
[sizeof(member_name
) - 1] = '\0';
2137 strcpy(member_name
, "$");
2140 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
2141 stree
= __get_cur_stree();
2142 FOR_EACH_MY_SM(cb
->owner
, stree
, sm
) {
2143 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
2144 strcpy(member_name
, "*$");
2145 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
2148 if (strncmp(sm
->name
, name
, len
) != 0)
2150 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
2152 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
2153 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
2154 } END_FOR_EACH_SM(sm
);
2155 } END_FOR_EACH_PTR(cb
);
2160 static void reset_memdb(struct symbol
*sym
)
2162 mem_sql(NULL
, NULL
, "delete from caller_info;");
2163 mem_sql(NULL
, NULL
, "delete from return_states;");
2164 mem_sql(NULL
, NULL
, "delete from call_implies;");
2165 mem_sql(NULL
, NULL
, "delete from return_implies;");
2168 static void match_end_func_info(struct symbol
*sym
)
2170 if (__path_is_null())
2172 call_return_state_hooks(NULL
);
2175 static void match_after_func(struct symbol
*sym
)
2181 static void init_memdb(void)
2185 const char *schema_files
[] = {
2187 "db/caller_info.schema",
2188 "db/common_caller_info.schema",
2189 "db/return_states.schema",
2190 "db/function_type_size.schema",
2191 "db/type_size.schema",
2192 "db/function_type_info.schema",
2193 "db/type_info.schema",
2194 "db/call_implies.schema",
2195 "db/return_implies.schema",
2196 "db/function_ptr.schema",
2197 "db/local_values.schema",
2198 "db/function_type_value.schema",
2199 "db/type_value.schema",
2200 "db/function_type.schema",
2201 "db/data_info.schema",
2202 "db/parameter_name.schema",
2203 "db/constraints.schema",
2204 "db/constraints_required.schema",
2205 "db/fn_ptr_data_link.schema",
2206 "db/fn_data_link.schema",
2207 "db/mtag_about.schema",
2208 "db/mtag_map.schema",
2209 "db/mtag_data.schema",
2210 "db/mtag_alias.schema",
2212 static char buf
[4096];
2217 rc
= sqlite3_open(":memory:", &mem_db
);
2218 if (rc
!= SQLITE_OK
) {
2219 sm_ierror("starting In-Memory database.");
2223 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
2224 fd
= open_schema_file(schema_files
[i
]);
2227 ret
= read(fd
, buf
, sizeof(buf
));
2229 sm_ierror("failed to read: %s", schema_files
[i
]);
2233 if (ret
== sizeof(buf
)) {
2234 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2235 schema_files
[i
], sizeof(buf
));
2239 rc
= sqlite3_exec(mem_db
, buf
, NULL
, NULL
, &err
);
2240 if (rc
!= SQLITE_OK
) {
2241 sm_ierror("SQL error #2: %s", err
);
2242 sm_ierror("%s", buf
);
2247 static void init_cachedb(void)
2251 const char *schema_files
[] = {
2252 "db/call_implies.schema",
2253 "db/return_implies.schema",
2254 "db/type_info.schema",
2255 "db/mtag_data.schema",
2256 "db/sink_info.schema",
2258 static char buf
[4096];
2263 rc
= sqlite3_open(":memory:", &cache_db
);
2264 if (rc
!= SQLITE_OK
) {
2265 sm_ierror("starting In-Memory database.");
2269 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
2270 fd
= open_schema_file(schema_files
[i
]);
2273 ret
= read(fd
, buf
, sizeof(buf
));
2275 sm_ierror("failed to read: %s", schema_files
[i
]);
2279 if (ret
== sizeof(buf
)) {
2280 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2281 schema_files
[i
], sizeof(buf
));
2285 rc
= sqlite3_exec(cache_db
, buf
, NULL
, NULL
, &err
);
2286 if (rc
!= SQLITE_OK
) {
2287 sm_ierror("SQL error #2: %s", err
);
2288 sm_ierror("%s", buf
);
2293 static int save_cache_data(void *_table
, int argc
, char **argv
, char **azColName
)
2295 static char buf
[4096];
2298 char *table
= _table
;
2302 p
+= snprintf(p
, 4096 - (p
- buf
), "insert or ignore into %s values (", table
);
2303 for (i
= 0; i
< argc
; i
++) {
2305 p
+= snprintf(p
, 4096 - (p
- buf
), ", ");
2306 sqlite3_snprintf(sizeof(tmp
), tmp
, "%q", escape_newlines(argv
[i
]));
2307 p
+= snprintf(p
, 4096 - (p
- buf
), "'%s'", tmp
);
2310 p
+= snprintf(p
, 4096 - (p
- buf
), ");");
2314 sm_msg("SQL: %s", buf
);
2318 static void dump_cache(struct symbol_list
*sym_list
)
2322 cache_sql(&save_cache_data
, (char *)"type_info", "select * from type_info;");
2323 cache_sql(&save_cache_data
, (char *)"return_implies", "select * from return_implies;");
2324 cache_sql(&save_cache_data
, (char *)"call_implies", "select * from call_implies;");
2325 cache_sql(&save_cache_data
, (char *)"mtag_data", "select * from mtag_data;");
2326 cache_sql(&save_cache_data
, (char *)"sink_info", "select * from sink_info;");
2329 void open_smatch_db(char *db_file
)
2336 use_states
= malloc(num_checks
+ 1);
2337 memset(use_states
, 0xff, num_checks
+ 1);
2342 rc
= sqlite3_open_v2(db_file
, &smatch_db
, SQLITE_OPEN_READONLY
, NULL
);
2343 if (rc
!= SQLITE_OK
) {
2348 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES
);
2352 static void register_common_funcs(void)
2354 struct token
*token
;
2358 if (option_project
== PROJ_NONE
)
2359 strcpy(filename
, "common_functions");
2361 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
2363 token
= get_tokens_file(filename
);
2366 if (token_type(token
) != TOKEN_STREAMBEGIN
)
2368 token
= token
->next
;
2369 while (token_type(token
) != TOKEN_STREAMEND
) {
2370 if (token_type(token
) != TOKEN_IDENT
)
2372 func
= alloc_string(show_ident(token
->ident
));
2373 add_ptr_list(&common_funcs
, func
);
2374 token
= token
->next
;
2376 clear_token_alloc();
2379 static char *get_next_string(char **str
)
2381 static char string
[256];
2390 while (*p
!= '\0' && *p
!= '\n') {
2391 if (*p
== '\\' && *(p
+ 1) == ' ') {
2401 if (len
>= sizeof(string
)) {
2402 memcpy(string
, start
, sizeof(string
));
2403 string
[sizeof(string
) - 1] = '\0';
2404 sm_ierror("return_fix: '%s' too long", string
);
2408 memcpy(string
, start
, len
);
2410 for (i
= 0; i
< sizeof(string
) - 1; i
++) {
2411 if (string
[i
] == '\\' && string
[i
+ 1] == ' ') {
2412 for (j
= i
; string
[j
] != '\0'; j
++)
2413 string
[j
] = string
[j
+ 1];
2422 static void register_return_replacements(void)
2424 char *func
, *orig
, *new;
2430 snprintf(filename
, 256, "db/%s.return_fixes", option_project_str
);
2431 fd
= open_schema_file(filename
);
2434 ret
= read(fd
, buf
, sizeof(buf
));
2438 if (ret
== sizeof(buf
)) {
2439 sm_ierror("file too large: %s (limit %zd bytes)",
2440 filename
, sizeof(buf
));
2447 get_next_string(&p
);
2450 if (replace_count
== 0 || replace_count
% 3 != 0) {
2454 replace_table
= malloc(replace_count
* sizeof(char *));
2459 func
= alloc_string(get_next_string(&p
));
2460 orig
= alloc_string(get_next_string(&p
));
2461 new = alloc_string(get_next_string(&p
));
2463 replace_table
[i
++] = func
;
2464 replace_table
[i
++] = orig
;
2465 replace_table
[i
++] = new;
2469 void register_definition_db_callbacks(int id
)
2471 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2472 add_hook(&match_call_info_new
, FUNCTION_CALL_HOOK
);
2473 add_split_return_callback(match_return_info
);
2474 add_split_return_callback(print_returned_struct_members
);
2475 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
2476 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
2477 add_hook(&match_after_func
, AFTER_FUNC_HOOK
);
2479 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
2480 add_hook(&match_call_implies
, FUNC_DEF_HOOK
);
2481 add_hook(&match_return_implies
, CALL_HOOK_AFTER_INLINE
);
2483 register_common_funcs();
2484 register_return_replacements();
2486 add_hook(&dump_cache
, END_FILE_HOOK
);
2489 void register_db_call_marker(int id
)
2491 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
2494 char *return_state_to_var_sym(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
)
2496 struct expression
*arg
;
2498 char member_name
[256];
2503 const char *star
= "";
2505 if (expr
->type
!= EXPR_ASSIGNMENT
)
2507 if (get_type(expr
->left
) == &int_ctype
&& strcmp(key
, "$") != 0)
2509 name
= expr_to_var_sym(expr
->left
, sym
);
2512 if (key
[0] == '*') {
2516 if (strncmp(key
, "$", 1) != 0)
2518 snprintf(member_name
, sizeof(member_name
), "%s%s%s", star
, name
, key
+ 1);
2520 return alloc_string(member_name
);
2523 while (expr
->type
== EXPR_ASSIGNMENT
)
2524 expr
= strip_expr(expr
->right
);
2525 if (expr
->type
!= EXPR_CALL
)
2528 arg
= get_argument_from_call_expr(expr
->args
, param
);
2532 return get_variable_from_key(arg
, key
, sym
);
2535 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
)
2544 arg
= strip_expr(arg
);
2546 if (strcmp(key
, "$") == 0)
2547 return expr_to_var_sym(arg
, sym
);
2549 if (strcmp(key
, "*$") == 0) {
2550 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
2551 arg
= strip_expr(arg
->unop
);
2552 return expr_to_var_sym(arg
, sym
);
2554 tmp
= expr_to_var_sym(arg
, sym
);
2557 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
2559 return alloc_string(buf
);
2563 while (key
[0] == '*') {
2568 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&' && star_cnt
) {
2569 arg
= strip_expr(arg
->unop
);
2573 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
2574 arg
= strip_expr(arg
->unop
);
2575 tmp
= expr_to_var_sym(arg
, sym
);
2578 snprintf(buf
, sizeof(buf
), "%.*s%s.%s",
2579 star_cnt
, "**********", tmp
, key
+ 3);
2580 return alloc_string(buf
);
2583 tmp
= expr_to_var_sym(arg
, sym
);
2586 snprintf(buf
, sizeof(buf
), "%.*s%s%s", star_cnt
, "**********", tmp
, key
+ 1);
2588 return alloc_string(buf
);
2591 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
)
2595 if (strcmp("$", key
) == 0)
2596 return expr_to_chunk_sym_vsl(arg
, sym
, vsl
);
2597 return get_variable_from_key(arg
, key
, sym
);
2600 const char *state_name_to_param_name(const char *state_name
, const char *param_name
)
2606 name_len
= strlen(param_name
);
2608 while (state_name
[0] == '*') {
2613 /* ten out of ten stars! */
2617 if (strcmp(state_name
, param_name
) == 0) {
2618 snprintf(buf
, sizeof(buf
), "%.*s$", star_cnt
, "**********");
2619 return alloc_sname(buf
);
2622 if (state_name
[name_len
] == '-' && /* check for '-' from "->" */
2623 strncmp(state_name
, param_name
, name_len
) == 0) {
2624 snprintf(buf
, sizeof(buf
), "%.*s$%s", star_cnt
, "**********", state_name
+ name_len
);
2625 return alloc_sname(buf
);
2630 const char *get_param_name_var_sym(const char *name
, struct symbol
*sym
)
2632 if (!sym
|| !sym
->ident
)
2635 return state_name_to_param_name(name
, sym
->ident
->name
);
2638 const char *get_mtag_name_var_sym(const char *state_name
, struct symbol
*sym
)
2640 struct symbol
*type
;
2641 const char *sym_name
;
2643 static char buf
[256];
2646 * mtag_name is different from param_name because mtags can be a struct
2647 * instead of a struct pointer. But we want to treat it like a pointer
2648 * because really an mtag is a pointer. Or in other words, if you pass
2649 * a struct foo then you want to talk about foo.bar but with an mtag
2650 * you want to refer to it as foo->bar.
2654 if (!sym
|| !sym
->ident
)
2657 type
= get_real_base_type(sym
);
2658 if (type
&& type
->type
== SYM_BASETYPE
)
2661 sym_name
= sym
->ident
->name
;
2662 name_len
= strlen(sym_name
);
2664 if (state_name
[name_len
] == '.' && /* check for '-' from "->" */
2665 strncmp(state_name
, sym_name
, name_len
) == 0) {
2666 snprintf(buf
, sizeof(buf
), "$->%s", state_name
+ name_len
+ 1);
2670 return state_name_to_param_name(state_name
, sym_name
);
2673 const char *get_mtag_name_expr(struct expression
*expr
)
2677 const char *ret
= NULL
;
2679 name
= expr_to_var_sym(expr
, &sym
);
2683 ret
= get_mtag_name_var_sym(name
, sym
);
2689 const char *get_param_name(struct sm_state
*sm
)
2691 return get_param_name_var_sym(sm
->name
, sm
->sym
);
2694 char *get_data_info_name(struct expression
*expr
)
2701 expr
= strip_expr(expr
);
2702 name
= get_member_name(expr
);
2705 name
= expr_to_var_sym(expr
, &sym
);
2708 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
2710 if (sym
->ctype
.modifiers
& MOD_STATIC
)
2711 snprintf(buf
, sizeof(buf
), "static %s", name
);
2713 snprintf(buf
, sizeof(buf
), "global %s", name
);
2714 ret
= alloc_sname(buf
);