flow: remove the call_split_expr() function
[smatch.git] / smatch_db.c
blobd7a614885c01970773cccfd0701146f0ab8c713d
1 /*
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
18 #include <string.h>
19 #include <errno.h>
20 #include <sqlite3.h>
21 #include <unistd.h>
22 #include <ctype.h>
23 #include "smatch.h"
24 #include "smatch_slist.h"
25 #include "smatch_extra.h"
27 static sqlite3 *db;
28 static sqlite3 *mem_db;
30 static int return_id;
32 #define sql_insert_helper(table, ignore, late, values...) \
33 do { \
34 if (__inline_fn) { \
35 char buf[1024]; \
36 char *err, *p = buf; \
37 int rc; \
39 if (!mem_db) \
40 break; \
42 p += snprintf(p, buf + sizeof(buf) - p, \
43 "insert %sinto %s values (", \
44 ignore ? "or ignore " : "", #table); \
45 p += snprintf(p, buf + sizeof(buf) - p, values); \
46 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
47 sm_debug("in-mem: %s\n", buf); \
48 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err); \
49 if (rc != SQLITE_OK) { \
50 fprintf(stderr, "SQL error #2: %s\n", err); \
51 fprintf(stderr, "SQL: '%s'\n", buf); \
52 parse_error = 1; \
53 } \
54 break; \
55 } \
56 if (option_info) { \
57 FILE *tmp_fd = sm_outfd; \
58 sm_outfd = sql_outfd; \
59 sm_prefix(); \
60 sm_printf("SQL%s: insert %sinto " #table " values(", \
61 late ? "_late" : "", ignore ? "or ignore " : ""); \
62 sm_printf(values); \
63 sm_printf(");\n"); \
64 sm_outfd = tmp_fd; \
65 } \
66 } while (0)
68 #define sql_insert(table, values...) sql_insert_helper(table, 0, 0, values);
69 #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, 1, 0, values);
70 #define sql_insert_late(table, values...) sql_insert_helper(table, 0, 1, values);
72 struct def_callback {
73 int hook_type;
74 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
76 ALLOCATOR(def_callback, "definition db hook callbacks");
77 DECLARE_PTR_LIST(callback_list, struct def_callback);
78 static struct callback_list *select_caller_info_callbacks;
80 struct member_info_callback {
81 int owner;
82 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
84 ALLOCATOR(member_info_callback, "caller_info callbacks");
85 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
86 static struct member_info_cb_list *member_callbacks;
88 struct returned_state_callback {
89 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
91 ALLOCATOR(returned_state_callback, "returned state callbacks");
92 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
93 static struct returned_state_cb_list *returned_state_callbacks;
95 struct returned_member_callback {
96 int owner;
97 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
99 ALLOCATOR(returned_member_callback, "returned member callbacks");
100 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
101 static struct returned_member_cb_list *returned_member_callbacks;
103 struct call_implies_callback {
104 int type;
105 void (*callback)(struct expression *call, struct expression *arg, char *key, char *value);
107 ALLOCATOR(call_implies_callback, "call_implies callbacks");
108 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
109 static struct call_implies_cb_list *call_implies_cb_list;
111 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
113 int i;
115 for (i = 0; i < argc; i++) {
116 if (i != 0)
117 printf(", ");
118 sm_printf("%s", argv[i]);
120 sm_printf("\n");
121 return 0;
124 void debug_sql(const char *sql)
126 if (!option_debug)
127 return;
128 sm_msg("%s", sql);
129 sql_exec(print_sql_output, NULL, sql);
132 void debug_mem_sql(const char *sql)
134 if (!option_debug)
135 return;
136 sm_msg("%s", sql);
137 sql_mem_exec(print_sql_output, NULL, sql);
140 void sql_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
142 char *err = NULL;
143 int rc;
145 if (option_no_db || !db)
146 return;
148 rc = sqlite3_exec(db, sql, callback, data, &err);
149 if (rc != SQLITE_OK && !parse_error) {
150 fprintf(stderr, "SQL error #2: %s\n", err);
151 fprintf(stderr, "SQL: '%s'\n", sql);
152 parse_error = 1;
156 void sql_mem_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
158 char *err = NULL;
159 int rc;
161 if (!mem_db)
162 return;
164 rc = sqlite3_exec(mem_db, sql, callback, data, &err);
165 if (rc != SQLITE_OK) {
166 fprintf(stderr, "SQL error #2: %s\n", err);
167 fprintf(stderr, "SQL: '%s'\n", sql);
168 parse_error = 1;
172 static int replace_count;
173 static char **replace_table;
174 static const char *replace_return_ranges(const char *return_ranges)
176 int i;
178 if (!get_function()) {
179 /* I have no idea why EXPORT_SYMBOL() is here */
180 return return_ranges;
182 for (i = 0; i < replace_count; i += 3) {
183 if (strcmp(replace_table[i + 0], get_function()) == 0) {
184 if (strcmp(replace_table[i + 1], return_ranges) == 0)
185 return replace_table[i + 2];
188 return return_ranges;
192 static char *use_states;
193 static int get_db_state_count(void)
195 struct sm_state *sm;
196 int count = 0;
198 FOR_EACH_SM(__get_cur_stree(), sm) {
199 if (sm->owner >= 0 && use_states[sm->owner])
200 count++;
201 } END_FOR_EACH_SM(sm);
202 return count;
205 void db_ignore_states(int id)
207 use_states[id] = 0;
210 void sql_insert_return_states(int return_id, const char *return_ranges,
211 int type, int param, const char *key, const char *value)
213 if (key && strlen(key) >= 80)
214 return;
215 return_ranges = replace_return_ranges(return_ranges);
216 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
217 get_base_file(), get_function(), (unsigned long)__inline_fn,
218 return_id, return_ranges, fn_static(), type, param, key, value);
221 static struct string_list *common_funcs;
222 static int is_common_function(const char *fn)
224 char *tmp;
226 if (!fn)
227 return 0;
229 if (strncmp(fn, "__builtin_", 10) == 0)
230 return 1;
232 FOR_EACH_PTR(common_funcs, tmp) {
233 if (strcmp(tmp, fn) == 0)
234 return 1;
235 } END_FOR_EACH_PTR(tmp);
237 return 0;
240 static char *function_signature(void)
242 return type_to_str(get_real_base_type(cur_func_sym));
245 void sql_insert_caller_info(struct expression *call, int type,
246 int param, const char *key, const char *value)
248 FILE *tmp_fd = sm_outfd;
249 char *fn;
251 if (!option_info && !__inline_call)
252 return;
254 if (key && strlen(key) >= 80)
255 return;
257 fn = get_fnptr_name(call->fn);
258 if (!fn)
259 return;
261 if (__inline_call) {
262 mem_sql(NULL, NULL,
263 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
264 get_base_file(), get_function(), fn, (unsigned long)call,
265 is_static(call->fn), type, param, key, value);
268 if (!option_info)
269 return;
271 if (strncmp(fn, "__builtin_", 10) == 0)
272 return;
273 if (type != INTERNAL && is_common_function(fn))
274 return;
276 sm_outfd = caller_info_fd;
277 sm_msg("SQL_caller_info: insert into caller_info values ("
278 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
279 get_base_file(), get_function(), fn, is_static(call->fn),
280 type, param, key, value);
281 sm_outfd = tmp_fd;
283 free_string(fn);
286 void sql_insert_function_ptr(const char *fn, const char *struct_name)
288 sql_insert(function_ptr, "'%s', '%s', '%s', 0", get_base_file(), fn,
289 struct_name);
292 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
294 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'", get_base_file(),
295 get_function(), (unsigned long)__inline_fn, fn_static(),
296 type, param, key, value);
299 void sql_insert_function_type_size(const char *member, const char *ranges)
301 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
304 void sql_insert_local_values(const char *name, const char *value)
306 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
309 void sql_insert_function_type_value(const char *type, const char *value)
311 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
314 void sql_insert_function_type(int param, const char *value)
316 sql_insert(function_type, "'%s', '%s', %d, %d, '%s'",
317 get_base_file(), get_function(), fn_static(), param, value);
320 void sql_insert_parameter_name(int param, const char *value)
322 sql_insert(parameter_name, "'%s', '%s', %d, %d, '%s'",
323 get_base_file(), get_function(), fn_static(), param, value);
326 void sql_insert_data_info(struct expression *data, int type, const char *value)
328 char *data_name;
330 data_name = get_data_info_name(data);
331 if (!data_name)
332 return;
333 sql_insert(data_info, "'%s', '%s', %d, '%s'",
334 is_static(data) ? get_base_file() : "extern",
335 data_name, type, value);
338 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
340 sql_insert(data_info, "'%s', '%s', %d, '%s'",
341 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
342 var, type, value);
345 void sql_save_constraint(const char *con)
347 if (!option_info)
348 return;
350 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", con);
353 void sql_save_constraint_required(const char *data, int op, const char *limit)
355 sql_insert_or_ignore(constraints_required, "'%s', '%s', '%s'", data, show_special(op), limit);
358 void sql_copy_constraint_required(const char *new_limit, const char *old_limit)
360 if (!option_info)
361 return;
363 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
364 "select constraints_required.data, constraints_required.op, '%s' from "
365 "constraints_required where bound = '%s';", new_limit, old_limit);
368 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data)
370 sql_insert_or_ignore(fn_ptr_data_link, "'%s', '%s'", ptr, data);
373 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value)
375 if (fn->type != EXPR_SYMBOL || !fn->symbol->ident)
376 return;
378 sql_insert(fn_data_link, "'%s', '%s', %d, %d, %d, '%s', '%s'",
379 (fn->symbol->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
380 fn->symbol->ident->name,
381 !!(fn->symbol->ctype.modifiers & MOD_STATIC),
382 type, param, key, value);
385 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name)
387 sql_insert(mtag_about, "%lld, '%s', '%s', %d, '%s', '%s'",
388 tag, get_filename(), get_function(), get_lineno(), left_name, right_name);
391 void sql_insert_mtag_data(mtag_t tag, const char *var, int offset, int type, const char *value)
393 sql_insert(mtag_data, "%lld, '%s', %d, %d, '%s'", tag, var, offset, type, value);
396 void sql_insert_mtag_map(mtag_t tag, int offset, mtag_t container)
398 sql_insert(mtag_map, "%lld, %d, %lld", tag, offset, container);
401 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias)
403 sql_insert(mtag_alias, "%lld, %lld", orig, alias);
406 static int save_mtag(void *_tag, int argc, char **argv, char **azColName)
408 mtag_t *saved_tag = _tag;
409 mtag_t new_tag;
411 new_tag = strtoll(argv[0], NULL, 10);
413 if (!*saved_tag)
414 *saved_tag = new_tag;
415 else if (*saved_tag != new_tag)
416 *saved_tag = -1ULL;
418 return 0;
421 int mtag_map_select_container(mtag_t tag, int offset, mtag_t *container)
423 mtag_t tmp = 0;
425 run_sql(save_mtag, &tmp,
426 "select container from mtag_map where tag = %lld and offset = %d;",
427 tag, offset);
429 if (tmp == 0 || tmp == -1ULL)
430 return 0;
431 *container = tmp;
432 return 1;
435 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag)
437 mtag_t tmp = 0;
439 run_sql(save_mtag, &tmp,
440 "select tag from mtag_map where container = %lld and offset = %d;",
441 container, offset);
443 if (tmp == 0 || tmp == -1ULL)
444 return 0;
445 *tag = tmp;
446 return 1;
449 char *get_static_filter(struct symbol *sym)
451 static char sql_filter[1024];
453 /* This can only happen on buggy code. Return invalid SQL. */
454 if (!sym) {
455 sql_filter[0] = '\0';
456 return sql_filter;
459 if (sym->ctype.modifiers & MOD_STATIC) {
460 snprintf(sql_filter, sizeof(sql_filter),
461 "file = '%s' and function = '%s' and static = '1'",
462 get_base_file(), sym->ident->name);
463 } else {
464 snprintf(sql_filter, sizeof(sql_filter),
465 "function = '%s' and static = '0'", sym->ident->name);
468 return sql_filter;
471 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
473 int *row_count = _row_count;
475 *row_count = 0;
476 if (argc != 1)
477 return 0;
478 *row_count = atoi(argv[0]);
479 return 0;
482 static void mark_call_params_untracked(struct expression *call)
484 struct expression *arg;
485 int i = 0;
487 FOR_EACH_PTR(call->args, arg) {
488 mark_untracked(call, i++, "$", NULL);
489 } END_FOR_EACH_PTR(arg);
492 static void sql_select_return_states_pointer(const char *cols,
493 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
495 char *ptr;
496 int return_count = 0;
498 ptr = get_fnptr_name(call->fn);
499 if (!ptr)
500 return;
502 run_sql(get_row_count, &return_count,
503 "select count(*) from return_states join function_ptr "
504 "where return_states.function == function_ptr.function and "
505 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
506 /* The magic number 100 is just from testing on the kernel. */
507 if (return_count > 100) {
508 mark_call_params_untracked(call);
509 return;
512 run_sql(callback, info,
513 "select %s from return_states join function_ptr where "
514 "return_states.function == function_ptr.function and ptr = '%s' "
515 "and searchable = 1 "
516 "order by function_ptr.file, return_states.file, return_id, type;",
517 cols, ptr);
520 static int is_local_symbol(struct expression *expr)
522 if (expr->type != EXPR_SYMBOL)
523 return 0;
524 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
525 return 0;
526 return 1;
529 void sql_select_return_states(const char *cols, struct expression *call,
530 int (*callback)(void*, int, char**, char**), void *info)
532 int row_count = 0;
534 if (is_fake_call(call))
535 return;
537 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol || is_local_symbol(call->fn)) {
538 sql_select_return_states_pointer(cols, call, callback, info);
539 return;
542 if (inlinable(call->fn)) {
543 mem_sql(callback, info,
544 "select %s from return_states where call_id = '%lu' order by return_id, type;",
545 cols, (unsigned long)call);
546 return;
549 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
550 get_static_filter(call->fn->symbol));
551 if (row_count > 3000)
552 return;
554 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
555 cols, get_static_filter(call->fn->symbol));
558 void sql_select_call_implies(const char *cols, struct expression *call,
559 int (*callback)(void*, int, char**, char**))
561 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
562 return;
564 if (inlinable(call->fn)) {
565 mem_sql(callback, call,
566 "select %s from call_implies where call_id = '%lu';",
567 cols, (unsigned long)call);
568 return;
571 run_sql(callback, call, "select %s from call_implies where %s;",
572 cols, get_static_filter(call->fn->symbol));
575 struct select_caller_info_data {
576 struct stree *final_states;
577 struct timeval start_time;
578 int prev_func_id;
579 int ignore;
580 int results;
583 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName);
585 static void sql_select_caller_info(struct select_caller_info_data *data,
586 const char *cols, struct symbol *sym)
588 if (__inline_fn) {
589 mem_sql(caller_info_callback, data,
590 "select %s from caller_info where call_id = %lu;",
591 cols, (unsigned long)__inline_fn);
592 return;
595 if (sym->ident->name && is_common_function(sym->ident->name))
596 return;
597 run_sql(caller_info_callback, data,
598 "select %s from common_caller_info where %s order by call_id;",
599 cols, get_static_filter(sym));
600 if (data->results)
601 return;
603 run_sql(caller_info_callback, data,
604 "select %s from caller_info where %s order by call_id;",
605 cols, get_static_filter(sym));
608 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
610 struct def_callback *def_callback = __alloc_def_callback(0);
612 def_callback->hook_type = type;
613 def_callback->callback = callback;
614 add_ptr_list(&select_caller_info_callbacks, def_callback);
618 * These call backs are used when the --info option is turned on to print struct
619 * member information. For example foo->bar could have a state in
620 * smatch_extra.c and also check_user.c.
622 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
624 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
626 member_callback->owner = owner;
627 member_callback->callback = callback;
628 add_ptr_list(&member_callbacks, member_callback);
631 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
633 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
635 callback->callback = fn;
636 add_ptr_list(&returned_state_callbacks, callback);
639 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))
641 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
643 member_callback->owner = owner;
644 member_callback->callback = callback;
645 add_ptr_list(&returned_member_callbacks, member_callback);
648 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
650 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
652 cb->type = type;
653 cb->callback = callback;
654 add_ptr_list(&call_implies_cb_list, cb);
657 struct return_info {
658 struct expression *static_returns_call;
659 struct symbol *return_type;
660 struct range_list *return_range_list;
663 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
665 struct return_info *ret_info = _ret_info;
666 struct range_list *rl;
667 struct expression *call_expr = ret_info->static_returns_call;
669 if (argc != 1)
670 return 0;
671 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
672 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
673 return 0;
676 struct range_list *db_return_vals(struct expression *expr)
678 struct return_info ret_info = {};
679 char buf[64];
680 struct sm_state *sm;
682 if (is_fake_call(expr))
683 return NULL;
685 snprintf(buf, sizeof(buf), "return %p", expr);
686 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
687 if (sm)
688 return clone_rl(estate_rl(sm->state));
689 ret_info.static_returns_call = expr;
690 ret_info.return_type = get_type(expr);
691 if (!ret_info.return_type)
692 return NULL;
694 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
695 return NULL;
697 ret_info.return_range_list = NULL;
698 if (inlinable(expr->fn)) {
699 mem_sql(db_return_callback, &ret_info,
700 "select distinct return from return_states where call_id = '%lu';",
701 (unsigned long)expr);
702 } else {
703 run_sql(db_return_callback, &ret_info,
704 "select distinct return from return_states where %s;",
705 get_static_filter(expr->fn->symbol));
707 return ret_info.return_range_list;
710 struct range_list *db_return_vals_from_str(const char *fn_name)
712 struct return_info ret_info;
714 ret_info.static_returns_call = NULL;
715 ret_info.return_type = &llong_ctype;
716 ret_info.return_range_list = NULL;
718 run_sql(db_return_callback, &ret_info,
719 "select distinct return from return_states where function = '%s';",
720 fn_name);
721 return ret_info.return_range_list;
724 static void match_call_marker(struct expression *expr)
726 struct symbol *type;
728 type = get_type(expr->fn);
729 if (type && type->type == SYM_PTR)
730 type = get_real_base_type(type);
733 * we just want to record something in the database so that if we have
734 * two calls like: frob(4); frob(some_unkown); then on the receiving
735 * side we know that sometimes frob is called with unknown parameters.
738 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", type_to_str(type));
741 static char *show_offset(int offset)
743 static char buf[64];
745 buf[0] = '\0';
746 if (offset != -1)
747 snprintf(buf, sizeof(buf), "(-%d)", offset);
748 return buf;
751 static void print_struct_members(struct expression *call, struct expression *expr, int param, int offset, struct stree *stree,
752 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
754 struct sm_state *sm;
755 char *name;
756 struct symbol *sym;
757 int len;
758 char printed_name[256];
759 int is_address = 0;
761 expr = strip_expr(expr);
762 if (!expr)
763 return;
764 if (expr->type == EXPR_PREOP && expr->op == '&') {
765 expr = strip_expr(expr->unop);
766 is_address = 1;
769 name = expr_to_var_sym(expr, &sym);
770 if (!name || !sym)
771 goto free;
773 len = strlen(name);
774 FOR_EACH_SM(stree, sm) {
775 if (sm->sym != sym)
776 continue;
777 if (strcmp(name, sm->name) == 0) {
778 if (is_address)
779 snprintf(printed_name, sizeof(printed_name), "*$%s", show_offset(offset));
780 else /* these are already handled. fixme: handle them here */
781 continue;
782 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
783 snprintf(printed_name, sizeof(printed_name), "*$%s", show_offset(offset));
784 } else if (strncmp(name, sm->name, len) == 0) {
785 if (isalnum(sm->name[len]))
786 continue;
787 if (is_address)
788 snprintf(printed_name, sizeof(printed_name), "$%s->%s", show_offset(offset), sm->name + len + 1);
789 else
790 snprintf(printed_name, sizeof(printed_name), "$%s%s", show_offset(offset), sm->name + len);
791 } else {
792 continue;
794 callback(call, param, printed_name, sm);
795 } END_FOR_EACH_SM(sm);
796 free:
797 free_string(name);
800 static int param_used_callback(void *_container, int argc, char **argv, char **azColName)
802 char **container = _container;
803 static char buf[256];
805 snprintf(buf, sizeof(buf), "%s", argv[0]);
806 *container = buf;
807 return 0;
810 static void print_container_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
811 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
813 struct expression *tmp;
814 char *container = NULL;
815 int offset;
816 int holder_offset;
817 char *p;
819 if (!call->fn || call->fn->type != EXPR_SYMBOL)
820 return;
823 * We can't use the in-mem DB because we have to parse the function
824 * first, then we know if it takes a container, then we know to pass it
825 * the container data.
828 run_sql(&param_used_callback, &container,
829 "select key from call_implies where %s and type = %d and key like '%%$(%%' and parameter = %d limit 1;",
830 get_static_filter(call->fn->symbol), CONTAINER, param);
831 if (!container)
832 return;
834 p = strchr(container, '-');
835 if (!p)
836 return;
837 offset = atoi(p);
838 p = strchr(p, ')');
839 if (!p)
840 return;
841 p++;
843 tmp = get_assigned_expr(expr);
844 if (tmp)
845 expr = tmp;
847 if (expr->type != EXPR_PREOP || expr->op != '&')
848 return;
849 expr = strip_expr(expr->unop);
850 holder_offset = get_member_offset_from_deref(expr);
851 if (-holder_offset != offset)
852 return;
854 expr = strip_expr(expr->deref);
855 if (expr->type == EXPR_PREOP && expr->op == '*')
856 expr = strip_expr(expr->unop);
858 print_struct_members(call, expr, param, holder_offset, stree, callback);
861 static void match_call_info(struct expression *call)
863 struct member_info_callback *cb;
864 struct expression *arg;
865 struct stree *stree;
866 char *name;
867 int i;
869 name = get_fnptr_name(call->fn);
870 if (!name)
871 return;
873 FOR_EACH_PTR(member_callbacks, cb) {
874 stree = get_all_states_stree(cb->owner);
875 i = 0;
876 FOR_EACH_PTR(call->args, arg) {
877 print_struct_members(call, arg, i, -1, stree, cb->callback);
878 print_container_struct_members(call, arg, i, stree, cb->callback);
879 i++;
880 } END_FOR_EACH_PTR(arg);
881 free_stree(&stree);
882 } END_FOR_EACH_PTR(cb);
884 free_string(name);
887 static int get_param(int param, char **name, struct symbol **sym)
889 struct symbol *arg;
890 int i;
892 i = 0;
893 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
895 * this is a temporary hack to work around a bug (I think in sparse?)
896 * 2.6.37-rc1:fs/reiserfs/journal.o
897 * If there is a function definition without parameter name found
898 * after a function implementation then it causes a crash.
899 * int foo() {}
900 * int bar(char *);
902 if (arg->ident->name < (char *)100)
903 continue;
904 if (i == param) {
905 *name = arg->ident->name;
906 *sym = arg;
907 return TRUE;
909 i++;
910 } END_FOR_EACH_PTR(arg);
912 return FALSE;
915 static int function_signature_matches(const char *sig)
917 char *my_sig;
919 my_sig = function_signature();
920 if (!sig || !my_sig)
921 return 1; /* default to matching */
922 if (strcmp(my_sig, sig) == 0)
923 return 1;
924 return 0;
927 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
929 struct select_caller_info_data *data = _data;
930 int func_id;
931 long type;
932 long param;
933 char *key;
934 char *value;
935 char *name = NULL;
936 struct symbol *sym = NULL;
937 struct def_callback *def_callback;
938 struct stree *stree;
939 struct timeval cur_time;
941 data->results = 1;
943 if (argc != 5)
944 return 0;
946 gettimeofday(&cur_time, NULL);
947 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
948 return 0;
950 func_id = atoi(argv[0]);
951 errno = 0;
952 type = strtol(argv[1], NULL, 10);
953 param = strtol(argv[2], NULL, 10);
954 if (errno)
955 return 0;
956 key = argv[3];
957 value = argv[4];
959 if (data->prev_func_id == -1)
960 data->prev_func_id = func_id;
961 if (func_id != data->prev_func_id) {
962 stree = __pop_fake_cur_stree();
963 if (!data->ignore)
964 merge_stree(&data->final_states, stree);
965 free_stree(&stree);
966 __push_fake_cur_stree();
967 __unnullify_path();
968 data->prev_func_id = func_id;
969 data->ignore = 0;
972 if (data->ignore)
973 return 0;
974 if (type == INTERNAL &&
975 !function_signature_matches(value)) {
976 data->ignore = 1;
977 return 0;
980 if (param >= 0 && !get_param(param, &name, &sym))
981 return 0;
983 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
984 if (def_callback->hook_type == type)
985 def_callback->callback(name, sym, key, value);
986 } END_FOR_EACH_PTR(def_callback);
988 return 0;
991 static struct string_list *ptr_names_done;
992 static struct string_list *ptr_names;
994 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
996 insert_string(&ptr_names, alloc_string(argv[0]));
997 return 0;
1000 static char *get_next_ptr_name(void)
1002 char *ptr;
1004 FOR_EACH_PTR(ptr_names, ptr) {
1005 if (list_has_string(ptr_names_done, ptr))
1006 continue;
1007 insert_string(&ptr_names_done, ptr);
1008 return ptr;
1009 } END_FOR_EACH_PTR(ptr);
1010 return NULL;
1013 static void get_ptr_names(const char *file, const char *name)
1015 char sql_filter[1024];
1016 int before, after;
1018 if (file) {
1019 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
1020 file, name);
1021 } else {
1022 snprintf(sql_filter, 1024, "function = '%s';", name);
1025 before = ptr_list_size((struct ptr_list *)ptr_names);
1027 run_sql(get_ptr_name, NULL,
1028 "select distinct ptr from function_ptr where %s",
1029 sql_filter);
1031 after = ptr_list_size((struct ptr_list *)ptr_names);
1032 if (before == after)
1033 return;
1035 while ((name = get_next_ptr_name()))
1036 get_ptr_names(NULL, name);
1039 static void match_data_from_db(struct symbol *sym)
1041 struct select_caller_info_data data = { .prev_func_id = -1 };
1042 struct sm_state *sm;
1043 struct stree *stree;
1044 struct timeval end_time;
1046 if (!sym || !sym->ident)
1047 return;
1049 gettimeofday(&data.start_time, NULL);
1051 __push_fake_cur_stree();
1052 __unnullify_path();
1054 if (!__inline_fn) {
1055 char *ptr;
1057 if (sym->ctype.modifiers & MOD_STATIC)
1058 get_ptr_names(get_base_file(), sym->ident->name);
1059 else
1060 get_ptr_names(NULL, sym->ident->name);
1062 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1063 __free_ptr_list((struct ptr_list **)&ptr_names);
1064 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1065 stree = __pop_fake_cur_stree();
1066 free_stree(&stree);
1067 return;
1070 sql_select_caller_info(&data,
1071 "call_id, type, parameter, key, value",
1072 sym);
1075 stree = __pop_fake_cur_stree();
1076 if (!data.ignore)
1077 merge_stree(&data.final_states, stree);
1078 free_stree(&stree);
1079 __push_fake_cur_stree();
1080 __unnullify_path();
1081 data.prev_func_id = -1;
1082 data.ignore = 0;
1084 FOR_EACH_PTR(ptr_names, ptr) {
1085 run_sql(caller_info_callback, &data,
1086 "select call_id, type, parameter, key, value"
1087 " from common_caller_info where function = '%s' order by call_id",
1088 ptr);
1089 } END_FOR_EACH_PTR(ptr);
1091 if (data.results) {
1092 FOR_EACH_PTR(ptr_names, ptr) {
1093 free_string(ptr);
1094 } END_FOR_EACH_PTR(ptr);
1095 goto free_ptr_names;
1098 FOR_EACH_PTR(ptr_names, ptr) {
1099 run_sql(caller_info_callback, &data,
1100 "select call_id, type, parameter, key, value"
1101 " from caller_info where function = '%s' order by call_id",
1102 ptr);
1103 free_string(ptr);
1104 } END_FOR_EACH_PTR(ptr);
1106 free_ptr_names:
1107 __free_ptr_list((struct ptr_list **)&ptr_names);
1108 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1109 } else {
1110 sql_select_caller_info(&data,
1111 "call_id, type, parameter, key, value",
1112 sym);
1115 stree = __pop_fake_cur_stree();
1116 if (!data.ignore)
1117 merge_stree(&data.final_states, stree);
1118 free_stree(&stree);
1120 gettimeofday(&end_time, NULL);
1121 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1122 FOR_EACH_SM(data.final_states, sm) {
1123 __set_sm(sm);
1124 } END_FOR_EACH_SM(sm);
1127 free_stree(&data.final_states);
1130 static int call_implies_callbacks(void *_call, int argc, char **argv, char **azColName)
1132 struct expression *call_expr = _call;
1133 struct call_implies_callback *cb;
1134 struct expression *arg = NULL;
1135 int type;
1136 int param;
1138 if (argc != 5)
1139 return 0;
1141 type = atoi(argv[1]);
1142 param = atoi(argv[2]);
1144 FOR_EACH_PTR(call_implies_cb_list, cb) {
1145 if (cb->type != type)
1146 continue;
1147 if (param != -1) {
1148 arg = get_argument_from_call_expr(call_expr->args, param);
1149 if (!arg)
1150 continue;
1152 cb->callback(call_expr, arg, argv[3], argv[4]);
1153 } END_FOR_EACH_PTR(cb);
1155 return 0;
1158 static void match_call_implies(struct expression *expr)
1160 sql_select_call_implies("function, type, parameter, key, value", expr,
1161 call_implies_callbacks);
1164 static void print_initializer_list(struct expression_list *expr_list,
1165 struct symbol *struct_type)
1167 struct expression *expr;
1168 struct symbol *base_type;
1169 char struct_name[256];
1171 FOR_EACH_PTR(expr_list, expr) {
1172 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
1173 print_initializer_list(expr->idx_expression->expr_list, struct_type);
1174 continue;
1176 if (expr->type != EXPR_IDENTIFIER)
1177 continue;
1178 if (!expr->expr_ident)
1179 continue;
1180 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
1181 continue;
1182 base_type = get_type(expr->ident_expression);
1183 if (!base_type || base_type->type != SYM_FN)
1184 continue;
1185 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
1186 struct_type->ident->name, expr->expr_ident->name);
1187 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
1188 struct_name);
1189 } END_FOR_EACH_PTR(expr);
1192 static void global_variable(struct symbol *sym)
1194 struct symbol *struct_type;
1196 if (!sym->ident)
1197 return;
1198 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
1199 return;
1200 struct_type = get_base_type(sym);
1201 if (!struct_type)
1202 return;
1203 if (struct_type->type == SYM_ARRAY) {
1204 struct_type = get_base_type(struct_type);
1205 if (!struct_type)
1206 return;
1208 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
1209 return;
1210 print_initializer_list(sym->initializer->expr_list, struct_type);
1213 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1215 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1218 static void call_return_state_hooks_conditional(struct expression *expr)
1220 struct returned_state_callback *cb;
1221 struct range_list *rl;
1222 char *return_ranges;
1223 int final_pass_orig = final_pass;
1225 __push_fake_cur_stree();
1227 final_pass = 0;
1228 __split_whole_condition(expr->conditional);
1229 final_pass = final_pass_orig;
1231 if (get_implied_rl(expr->cond_true, &rl))
1232 rl = cast_rl(cur_func_return_type(), rl);
1233 else
1234 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
1235 return_ranges = show_rl(rl);
1236 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1238 return_id++;
1239 FOR_EACH_PTR(returned_state_callbacks, cb) {
1240 cb->callback(return_id, return_ranges, expr->cond_true);
1241 } END_FOR_EACH_PTR(cb);
1243 __push_true_states();
1244 __use_false_states();
1246 if (get_implied_rl(expr->cond_false, &rl))
1247 rl = cast_rl(cur_func_return_type(), rl);
1248 else
1249 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
1250 return_ranges = show_rl(rl);
1251 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1253 return_id++;
1254 FOR_EACH_PTR(returned_state_callbacks, cb) {
1255 cb->callback(return_id, return_ranges, expr->cond_false);
1256 } END_FOR_EACH_PTR(cb);
1258 __merge_true_states();
1259 __free_fake_cur_stree();
1262 static void call_return_state_hooks_compare(struct expression *expr)
1264 struct returned_state_callback *cb;
1265 char *return_ranges;
1266 int final_pass_orig = final_pass;
1267 sval_t sval = { .type = &int_ctype };
1268 sval_t ret;
1270 if (!get_implied_value(expr, &ret))
1271 ret.value = -1;
1273 __push_fake_cur_stree();
1275 final_pass = 0;
1276 __split_whole_condition(expr);
1277 final_pass = final_pass_orig;
1279 if (ret.value != 0) {
1280 return_ranges = alloc_sname("1");
1281 sval.value = 1;
1282 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1284 return_id++;
1285 FOR_EACH_PTR(returned_state_callbacks, cb) {
1286 cb->callback(return_id, return_ranges, expr);
1287 } END_FOR_EACH_PTR(cb);
1290 __push_true_states();
1291 __use_false_states();
1293 if (ret.value != 1) {
1294 return_ranges = alloc_sname("0");
1295 sval.value = 0;
1296 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1298 return_id++;
1299 FOR_EACH_PTR(returned_state_callbacks, cb) {
1300 cb->callback(return_id, return_ranges, expr);
1301 } END_FOR_EACH_PTR(cb);
1304 __merge_true_states();
1305 __free_fake_cur_stree();
1308 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1310 struct sm_state *tmp;
1312 FOR_EACH_PTR(slist, tmp) {
1313 if (strcmp(tmp->state->name, sm->state->name) == 0)
1314 return 1;
1315 } END_FOR_EACH_PTR(tmp);
1317 return 0;
1320 static char *get_return_compare_str(struct expression *expr)
1322 char *compare_str;
1323 char *var;
1324 char buf[256];
1325 int comparison;
1326 int param;
1328 compare_str = expr_lte_to_param(expr, -1);
1329 if (compare_str)
1330 return compare_str;
1331 param = get_param_num(expr);
1332 if (param < 0)
1333 return NULL;
1335 var = expr_to_var(expr);
1336 if (!var)
1337 return NULL;
1338 snprintf(buf, sizeof(buf), "%s orig", var);
1339 comparison = get_comparison_strings(var, buf);
1340 free_string(var);
1342 if (!comparison)
1343 return NULL;
1345 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1346 return alloc_sname(buf);
1349 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1351 struct returned_state_callback *cb;
1352 struct range_list *rl;
1353 char *return_ranges;
1354 struct sm_state *tmp;
1355 int ret = 0;
1356 int nr_possible, nr_states;
1357 char *compare_str = NULL;
1358 char buf[128];
1359 struct state_list *already_handled = NULL;
1361 if (!sm || !sm->merged)
1362 return 0;
1364 if (too_many_possible(sm))
1365 return 0;
1367 /* bail if it gets too complicated */
1368 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1369 nr_states = get_db_state_count();
1370 if (nr_states * nr_possible >= 2000)
1371 return 0;
1373 FOR_EACH_PTR(sm->possible, tmp) {
1374 if (tmp->merged)
1375 continue;
1376 if (ptr_in_list(tmp, already_handled))
1377 continue;
1378 add_ptr_list(&already_handled, tmp);
1380 ret = 1;
1381 __push_fake_cur_stree();
1383 overwrite_states_using_pool(sm, tmp);
1385 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1386 return_ranges = show_rl(rl);
1387 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1388 compare_str = get_return_compare_str(expr);
1389 if (compare_str) {
1390 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1391 return_ranges = alloc_sname(buf);
1394 return_id++;
1395 FOR_EACH_PTR(returned_state_callbacks, cb) {
1396 cb->callback(return_id, return_ranges, expr);
1397 } END_FOR_EACH_PTR(cb);
1399 __free_fake_cur_stree();
1400 } END_FOR_EACH_PTR(tmp);
1402 free_slist(&already_handled);
1404 return ret;
1407 static int call_return_state_hooks_split_possible(struct expression *expr)
1409 struct sm_state *sm;
1411 if (!expr || expr_equal_to_param(expr, -1))
1412 return 0;
1414 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1415 return split_possible_helper(sm, expr);
1418 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1420 struct range_list *rl;
1421 char *return_ranges;
1422 sval_t sval;
1423 char *compare_str;
1424 char *math_str;
1425 char buf[128];
1427 *rl_p = NULL;
1429 if (!expr)
1430 return alloc_sname("");
1432 if (get_implied_value(expr, &sval)) {
1433 sval = sval_cast(cur_func_return_type(), sval);
1434 *rl_p = alloc_rl(sval, sval);
1435 return sval_to_str(sval);
1438 compare_str = expr_equal_to_param(expr, -1);
1439 math_str = get_value_in_terms_of_parameter_math(expr);
1441 if (get_implied_rl(expr, &rl)) {
1442 rl = cast_rl(cur_func_return_type(), rl);
1443 return_ranges = show_rl(rl);
1444 } else if (get_imaginary_absolute(expr, &rl)){
1445 rl = cast_rl(cur_func_return_type(), rl);
1446 return alloc_sname(show_rl(rl));
1447 } else {
1448 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1449 return_ranges = show_rl(rl);
1451 *rl_p = rl;
1453 if (compare_str) {
1454 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1455 return alloc_sname(buf);
1457 if (math_str) {
1458 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1459 return alloc_sname(buf);
1461 compare_str = get_return_compare_str(expr);
1462 if (compare_str) {
1463 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1464 return alloc_sname(buf);
1467 return return_ranges;
1470 static int split_positive_from_negative(struct expression *expr)
1472 struct returned_state_callback *cb;
1473 struct range_list *rl;
1474 const char *return_ranges;
1475 struct range_list *ret_rl;
1476 int undo;
1478 /* We're going to print the states 3 times */
1479 if (get_db_state_count() > 10000 / 3)
1480 return 0;
1482 if (!get_implied_rl(expr, &rl) || !rl)
1483 return 0;
1484 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1485 return 0;
1486 /* Forget about INT_MAX and larger */
1487 if (rl_max(rl).value <= 0)
1488 return 0;
1489 if (!sval_is_negative(rl_min(rl)))
1490 return 0;
1492 if (!assume(compare_expression(expr, '>', zero_expr())))
1493 return 0;
1495 return_id++;
1496 return_ranges = get_return_ranges_str(expr, &ret_rl);
1497 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1498 FOR_EACH_PTR(returned_state_callbacks, cb) {
1499 cb->callback(return_id, (char *)return_ranges, expr);
1500 } END_FOR_EACH_PTR(cb);
1502 end_assume();
1504 if (rl_has_sval(rl, sval_type_val(rl_type(rl), 0))) {
1505 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1507 return_id++;
1508 return_ranges = get_return_ranges_str(expr, &ret_rl);
1509 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1510 FOR_EACH_PTR(returned_state_callbacks, cb) {
1511 cb->callback(return_id, (char *)return_ranges, expr);
1512 } END_FOR_EACH_PTR(cb);
1514 if (undo)
1515 end_assume();
1518 undo = assume(compare_expression(expr, '<', zero_expr()));
1520 return_id++;
1521 return_ranges = get_return_ranges_str(expr, &ret_rl);
1522 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1523 FOR_EACH_PTR(returned_state_callbacks, cb) {
1524 cb->callback(return_id, (char *)return_ranges, expr);
1525 } END_FOR_EACH_PTR(cb);
1527 if (undo)
1528 end_assume();
1530 return 1;
1533 static int call_return_state_hooks_split_null_non_null(struct expression *expr)
1535 struct returned_state_callback *cb;
1536 struct range_list *rl;
1537 struct range_list *nonnull_rl;
1538 sval_t null_sval;
1539 struct range_list *null_rl = NULL;
1540 char *return_ranges;
1541 struct sm_state *sm;
1542 struct smatch_state *state;
1543 int nr_states;
1544 int final_pass_orig = final_pass;
1546 if (!expr || expr_equal_to_param(expr, -1))
1547 return 0;
1548 if (expr->type == EXPR_CALL)
1549 return 0;
1550 if (!is_pointer(expr))
1551 return 0;
1553 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1554 if (!sm)
1555 return 0;
1556 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1557 return 0;
1558 state = sm->state;
1559 if (!estate_rl(state))
1560 return 0;
1561 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1562 return 0;
1563 if (!rl_has_sval(estate_rl(state), sval_type_val(estate_type(state), 0)))
1564 return 0;
1566 nr_states = get_db_state_count();
1567 if (option_info && nr_states >= 1500)
1568 return 0;
1570 rl = estate_rl(state);
1572 __push_fake_cur_stree();
1574 final_pass = 0;
1575 __split_whole_condition(expr);
1576 final_pass = final_pass_orig;
1578 nonnull_rl = rl_filter(rl, rl_zero());
1579 return_ranges = show_rl(nonnull_rl);
1580 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1582 return_id++;
1583 FOR_EACH_PTR(returned_state_callbacks, cb) {
1584 cb->callback(return_id, return_ranges, expr);
1585 } END_FOR_EACH_PTR(cb);
1587 __push_true_states();
1588 __use_false_states();
1590 return_ranges = alloc_sname("0");
1591 null_sval = sval_type_val(rl_type(rl), 0);
1592 add_range(&null_rl, null_sval, null_sval);
1593 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1594 return_id++;
1595 FOR_EACH_PTR(returned_state_callbacks, cb) {
1596 cb->callback(return_id, return_ranges, expr);
1597 } END_FOR_EACH_PTR(cb);
1599 __merge_true_states();
1600 __free_fake_cur_stree();
1602 return 1;
1605 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1607 struct range_list *rl;
1608 struct range_list *nonzero_rl;
1609 sval_t zero_sval;
1610 struct range_list *zero_rl = NULL;
1611 int nr_states;
1612 struct returned_state_callback *cb;
1613 char *return_ranges;
1614 int final_pass_orig = final_pass;
1615 sval_t val;
1617 if (option_project != PROJ_KERNEL)
1618 return 0;
1620 nr_states = get_db_state_count();
1621 if (nr_states > 1500)
1622 return 0;
1624 if (get_value(expr, &val))
1625 return 0;
1626 if (!get_implied_rl(expr, &rl))
1627 return 0;
1628 if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1629 return 0;
1630 if (rl_max(rl).value != 0)
1631 return 0;
1633 __push_fake_cur_stree();
1635 final_pass = 0;
1636 __split_whole_condition(expr);
1637 final_pass = final_pass_orig;
1639 nonzero_rl = rl_filter(rl, rl_zero());
1640 return_ranges = show_rl(nonzero_rl);
1641 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1643 return_id++;
1644 FOR_EACH_PTR(returned_state_callbacks, cb) {
1645 cb->callback(return_id, return_ranges, expr);
1646 } END_FOR_EACH_PTR(cb);
1648 __push_true_states();
1649 __use_false_states();
1651 return_ranges = alloc_sname("0");
1652 zero_sval = sval_type_val(rl_type(rl), 0);
1653 add_range(&zero_rl, zero_sval, zero_sval);
1654 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1655 return_id++;
1656 FOR_EACH_PTR(returned_state_callbacks, cb) {
1657 cb->callback(return_id, return_ranges, expr);
1658 } END_FOR_EACH_PTR(cb);
1660 __merge_true_states();
1661 __free_fake_cur_stree();
1663 return 1;
1666 static int is_boolean(struct expression *expr)
1668 struct range_list *rl;
1670 if (!get_implied_rl(expr, &rl))
1671 return 0;
1672 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1673 return 1;
1674 return 0;
1677 static int is_conditional(struct expression *expr)
1679 if (!expr)
1680 return 0;
1681 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1682 return 1;
1683 return 0;
1686 static int splitable_function_call(struct expression *expr)
1688 struct sm_state *sm;
1689 char buf[64];
1691 if (!expr || expr->type != EXPR_CALL)
1692 return 0;
1693 snprintf(buf, sizeof(buf), "return %p", expr);
1694 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1695 return split_possible_helper(sm, expr);
1698 static struct sm_state *find_bool_param(void)
1700 struct stree *start_states;
1701 struct symbol *arg;
1702 struct sm_state *sm, *tmp;
1703 sval_t sval;
1705 start_states = get_start_states();
1707 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1708 if (!arg->ident)
1709 continue;
1710 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1711 if (!sm)
1712 continue;
1713 if (rl_min(estate_rl(sm->state)).value != 0 ||
1714 rl_max(estate_rl(sm->state)).value != 1)
1715 continue;
1716 goto found;
1717 } END_FOR_EACH_PTR_REVERSE(arg);
1719 return NULL;
1721 found:
1723 * Check if it's splitable. If not, then splitting it up is likely not
1724 * useful for the callers.
1726 FOR_EACH_PTR(sm->possible, tmp) {
1727 if (is_merged(tmp))
1728 continue;
1729 if (!estate_get_single_value(tmp->state, &sval))
1730 return NULL;
1731 } END_FOR_EACH_PTR(tmp);
1733 return sm;
1736 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1738 struct returned_state_callback *cb;
1739 struct range_list *ret_rl;
1740 const char *return_ranges;
1741 struct sm_state *tmp;
1742 int ret = 0;
1743 int nr_possible, nr_states;
1744 char *compare_str = NULL;
1745 char buf[128];
1746 struct state_list *already_handled = NULL;
1748 if (!sm || !sm->merged)
1749 return 0;
1751 if (too_many_possible(sm))
1752 return 0;
1754 /* bail if it gets too complicated */
1755 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1756 nr_states = get_db_state_count();
1757 if (nr_states * nr_possible >= 2000)
1758 return 0;
1760 FOR_EACH_PTR(sm->possible, tmp) {
1761 if (tmp->merged)
1762 continue;
1763 if (ptr_in_list(tmp, already_handled))
1764 continue;
1765 add_ptr_list(&already_handled, tmp);
1767 ret = 1;
1768 __push_fake_cur_stree();
1770 overwrite_states_using_pool(sm, tmp);
1772 return_ranges = get_return_ranges_str(expr, &ret_rl);
1773 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1774 compare_str = get_return_compare_str(expr);
1775 if (compare_str) {
1776 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1777 return_ranges = alloc_sname(buf);
1780 return_id++;
1781 FOR_EACH_PTR(returned_state_callbacks, cb) {
1782 cb->callback(return_id, (char *)return_ranges, expr);
1783 } END_FOR_EACH_PTR(cb);
1785 __free_fake_cur_stree();
1786 } END_FOR_EACH_PTR(tmp);
1788 free_slist(&already_handled);
1790 return ret;
1793 static int split_by_bool_param(struct expression *expr)
1795 struct sm_state *start_sm, *sm;
1796 sval_t sval;
1798 start_sm = find_bool_param();
1799 if (!start_sm)
1800 return 0;
1801 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
1802 if (!sm || estate_get_single_value(sm->state, &sval))
1803 return 0;
1804 return split_on_bool_sm(sm, expr);
1807 static int split_by_null_nonnull_param(struct expression *expr)
1809 struct symbol *arg;
1810 struct sm_state *sm;
1811 sval_t zero = {
1812 .type = &ulong_ctype,
1815 /* function must only take one pointer */
1816 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
1817 return 0;
1818 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
1819 if (!arg->ident)
1820 return 0;
1821 if (get_real_base_type(arg)->type != SYM_PTR)
1822 return 0;
1824 if (param_was_set_var_sym(arg->ident->name, arg))
1825 return 0;
1826 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
1827 if (!sm)
1828 return 0;
1830 if (!rl_has_sval(estate_rl(sm->state), zero))
1831 return 0;
1833 return split_on_bool_sm(sm, expr);
1836 struct expression *strip_expr_statement(struct expression *expr)
1838 struct expression *orig = expr;
1839 struct statement *stmt, *last_stmt;
1841 if (!expr)
1842 return NULL;
1843 if (expr->type == EXPR_PREOP && expr->op == '(')
1844 expr = expr->unop;
1845 if (expr->type != EXPR_STATEMENT)
1846 return orig;
1847 stmt = expr->statement;
1848 if (!stmt || stmt->type != STMT_COMPOUND)
1849 return orig;
1851 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1852 if (!last_stmt || last_stmt->type == STMT_LABEL)
1853 last_stmt = last_stmt->label_statement;
1854 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
1855 return orig;
1856 return strip_expr(last_stmt->expression);
1859 static void call_return_state_hooks(struct expression *expr)
1861 struct returned_state_callback *cb;
1862 struct range_list *ret_rl;
1863 const char *return_ranges;
1864 int nr_states;
1865 sval_t sval;
1867 if (__path_is_null())
1868 return;
1870 expr = strip_expr(expr);
1871 expr = strip_expr_statement(expr);
1873 if (is_impossible_path())
1874 goto vanilla;
1876 if (expr && (expr->type == EXPR_COMPARE ||
1877 !get_implied_value(expr, &sval)) &&
1878 (is_condition(expr) || is_boolean(expr))) {
1879 call_return_state_hooks_compare(expr);
1880 return;
1881 } else if (is_conditional(expr)) {
1882 call_return_state_hooks_conditional(expr);
1883 return;
1884 } else if (call_return_state_hooks_split_possible(expr)) {
1885 return;
1886 } else if (call_return_state_hooks_split_null_non_null(expr)) {
1887 return;
1888 } else if (call_return_state_hooks_split_success_fail(expr)) {
1889 return;
1890 } else if (splitable_function_call(expr)) {
1891 return;
1892 } else if (split_positive_from_negative(expr)) {
1893 return;
1894 } else if (split_by_bool_param(expr)) {
1895 } else if (split_by_null_nonnull_param(expr)) {
1896 return;
1899 vanilla:
1900 return_ranges = get_return_ranges_str(expr, &ret_rl);
1901 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1903 return_id++;
1904 nr_states = get_db_state_count();
1905 if (nr_states >= 10000) {
1906 match_return_info(return_id, (char *)return_ranges, expr);
1907 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
1908 return;
1910 FOR_EACH_PTR(returned_state_callbacks, cb) {
1911 cb->callback(return_id, (char *)return_ranges, expr);
1912 } END_FOR_EACH_PTR(cb);
1915 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1917 struct returned_member_callback *cb;
1918 struct stree *stree;
1919 struct sm_state *sm;
1920 struct symbol *type;
1921 char *name;
1922 char member_name[256];
1923 int len;
1925 type = get_type(expr);
1926 if (!type || type->type != SYM_PTR)
1927 return;
1928 name = expr_to_var(expr);
1929 if (!name)
1930 return;
1932 member_name[sizeof(member_name) - 1] = '\0';
1933 strcpy(member_name, "$");
1935 len = strlen(name);
1936 FOR_EACH_PTR(returned_member_callbacks, cb) {
1937 stree = __get_cur_stree();
1938 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1939 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1940 strcpy(member_name, "*$");
1941 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1942 continue;
1944 if (strncmp(sm->name, name, len) != 0)
1945 continue;
1946 if (strncmp(sm->name + len, "->", 2) != 0)
1947 continue;
1948 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
1949 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1950 } END_FOR_EACH_SM(sm);
1951 } END_FOR_EACH_PTR(cb);
1953 free_string(name);
1956 static void reset_memdb(struct symbol *sym)
1958 mem_sql(NULL, NULL, "delete from caller_info;");
1959 mem_sql(NULL, NULL, "delete from return_states;");
1960 mem_sql(NULL, NULL, "delete from call_implies;");
1963 static void match_end_func_info(struct symbol *sym)
1965 if (__path_is_null())
1966 return;
1967 call_return_state_hooks(NULL);
1970 static void match_after_func(struct symbol *sym)
1972 if (!__inline_fn)
1973 reset_memdb(sym);
1976 static void init_memdb(void)
1978 char *err = NULL;
1979 int rc;
1980 const char *schema_files[] = {
1981 "db/db.schema",
1982 "db/caller_info.schema",
1983 "db/return_states.schema",
1984 "db/function_type_size.schema",
1985 "db/type_size.schema",
1986 "db/call_implies.schema",
1987 "db/function_ptr.schema",
1988 "db/local_values.schema",
1989 "db/function_type_value.schema",
1990 "db/type_value.schema",
1991 "db/function_type.schema",
1992 "db/data_info.schema",
1993 "db/parameter_name.schema",
1994 "db/constraints.schema",
1995 "db/constraints_required.schema",
1996 "db/fn_ptr_data_link.schema",
1997 "db/fn_data_link.schema",
1998 "db/mtag_about.schema",
1999 "db/mtag_map.schema",
2000 "db/mtag_data.schema",
2001 "db/mtag_alias.schema",
2003 static char buf[4096];
2004 int fd;
2005 int ret;
2006 int i;
2008 rc = sqlite3_open(":memory:", &mem_db);
2009 if (rc != SQLITE_OK) {
2010 printf("Error starting In-Memory database.");
2011 return;
2014 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2015 fd = open_data_file(schema_files[i]);
2016 if (fd < 0) {
2017 printf("failed to open: %s\n", schema_files[i]);
2018 continue;
2020 ret = read(fd, buf, sizeof(buf));
2021 if (ret < 0) {
2022 printf("failed to read: %s\n", schema_files[i]);
2023 continue;
2025 close(fd);
2026 if (ret == sizeof(buf)) {
2027 printf("Schema file too large: %s (limit %zd bytes)",
2028 schema_files[i], sizeof(buf));
2029 continue;
2031 buf[ret] = '\0';
2032 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2033 if (rc != SQLITE_OK) {
2034 fprintf(stderr, "SQL error #2: %s\n", err);
2035 fprintf(stderr, "%s\n", buf);
2040 void open_smatch_db(void)
2042 int rc;
2044 if (option_no_db)
2045 return;
2047 use_states = malloc(num_checks + 1);
2048 memset(use_states, 0xff, num_checks + 1);
2050 init_memdb();
2052 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
2053 if (rc != SQLITE_OK) {
2054 option_no_db = 1;
2055 return;
2057 return;
2060 static void register_common_funcs(void)
2062 struct token *token;
2063 char *func;
2064 char filename[256];
2066 if (option_project == PROJ_NONE)
2067 strcpy(filename, "common_functions");
2068 else
2069 snprintf(filename, 256, "%s.common_functions", option_project_str);
2071 token = get_tokens_file(filename);
2072 if (!token)
2073 return;
2074 if (token_type(token) != TOKEN_STREAMBEGIN)
2075 return;
2076 token = token->next;
2077 while (token_type(token) != TOKEN_STREAMEND) {
2078 if (token_type(token) != TOKEN_IDENT)
2079 return;
2080 func = alloc_string(show_ident(token->ident));
2081 add_ptr_list(&common_funcs, func);
2082 token = token->next;
2084 clear_token_alloc();
2087 static char *get_next_string(char **str)
2089 static char string[256];
2090 char *start;
2091 char *p = *str;
2092 int len;
2094 if (*p == '\0')
2095 return NULL;
2096 start = p;
2098 while (*p != '\0' && *p != ' ' && *p != '\n')
2099 p++;
2101 len = p - start;
2102 if (len > 256) {
2103 memcpy(string, start, 255);
2104 string[255] = '\0';
2105 printf("return_fix: '%s' too long", string);
2106 **str = '\0';
2107 return NULL;
2109 memcpy(string, start, len);
2110 string[len] = '\0';
2111 if (*p != '\0')
2112 p++;
2113 *str = p;
2114 return string;
2117 static void register_return_replacements(void)
2119 char *func, *orig, *new;
2120 char filename[256];
2121 char buf[4096];
2122 int fd, ret, i;
2123 char *p;
2125 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2126 fd = open_data_file(filename);
2127 if (fd < 0)
2128 return;
2129 ret = read(fd, buf, sizeof(buf));
2130 close(fd);
2131 if (ret < 0)
2132 return;
2133 if (ret == sizeof(buf)) {
2134 printf("file too large: %s (limit %zd bytes)",
2135 filename, sizeof(buf));
2136 return;
2138 buf[ret] = '\0';
2140 p = buf;
2141 while (*p) {
2142 get_next_string(&p);
2143 replace_count++;
2145 if (replace_count == 0 || replace_count % 3 != 0) {
2146 replace_count = 0;
2147 return;
2149 replace_table = malloc(replace_count * sizeof(char *));
2151 p = buf;
2152 i = 0;
2153 while (*p) {
2154 func = alloc_string(get_next_string(&p));
2155 orig = alloc_string(get_next_string(&p));
2156 new = alloc_string(get_next_string(&p));
2158 replace_table[i++] = func;
2159 replace_table[i++] = orig;
2160 replace_table[i++] = new;
2164 void register_definition_db_callbacks(int id)
2166 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2167 add_hook(&global_variable, BASE_HOOK);
2168 add_hook(&global_variable, DECLARATION_HOOK);
2169 add_split_return_callback(match_return_info);
2170 add_split_return_callback(print_returned_struct_members);
2171 add_hook(&call_return_state_hooks, RETURN_HOOK);
2172 add_hook(&match_end_func_info, END_FUNC_HOOK);
2173 add_hook(&match_after_func, AFTER_FUNC_HOOK);
2175 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2176 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
2178 register_common_funcs();
2179 register_return_replacements();
2182 void register_db_call_marker(int id)
2184 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2187 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
2189 struct expression *arg;
2190 char *name = NULL;
2191 char member_name[256];
2193 *sym = NULL;
2195 if (param == -1) {
2196 const char *star = "";
2198 if (expr->type != EXPR_ASSIGNMENT)
2199 return NULL;
2200 name = expr_to_var_sym(expr->left, sym);
2201 if (!name)
2202 return NULL;
2203 if (key[0] == '*') {
2204 star = "*";
2205 key++;
2207 if (strncmp(key, "$", 1) != 0)
2208 return name;
2209 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
2210 free_string(name);
2211 return alloc_string(member_name);
2214 while (expr->type == EXPR_ASSIGNMENT)
2215 expr = strip_expr(expr->right);
2216 if (expr->type != EXPR_CALL)
2217 return NULL;
2219 arg = get_argument_from_call_expr(expr->args, param);
2220 if (!arg)
2221 return NULL;
2223 return get_variable_from_key(arg, key, sym);
2226 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
2228 char buf[256];
2229 char *tmp;
2231 if (!arg)
2232 return NULL;
2234 arg = strip_expr(arg);
2236 if (strcmp(key, "$") == 0)
2237 return expr_to_var_sym(arg, sym);
2239 if (strcmp(key, "*$") == 0) {
2240 if (arg->type == EXPR_PREOP && arg->op == '&') {
2241 arg = strip_expr(arg->unop);
2242 return expr_to_var_sym(arg, sym);
2243 } else {
2244 tmp = expr_to_var_sym(arg, sym);
2245 if (!tmp)
2246 return NULL;
2247 snprintf(buf, sizeof(buf), "*%s", tmp);
2248 free_string(tmp);
2249 return alloc_string(buf);
2253 if (arg->type == EXPR_PREOP && arg->op == '&') {
2254 arg = strip_expr(arg->unop);
2255 tmp = expr_to_var_sym(arg, sym);
2256 if (!tmp)
2257 return NULL;
2258 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
2259 return alloc_string(buf);
2262 tmp = expr_to_var_sym(arg, sym);
2263 if (!tmp)
2264 return NULL;
2265 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
2266 free_string(tmp);
2267 return alloc_string(buf);
2270 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
2272 *vsl = NULL;
2274 if (strcmp("$", key) == 0)
2275 return expr_to_chunk_sym_vsl(arg, sym, vsl);
2276 return get_variable_from_key(arg, key, sym);
2279 const char *state_name_to_param_name(const char *state_name, const char *param_name)
2281 int name_len;
2282 static char buf[256];
2284 name_len = strlen(param_name);
2286 if (strcmp(state_name, param_name) == 0) {
2287 return "$";
2288 } else if (state_name[name_len] == '-' && /* check for '-' from "->" */
2289 strncmp(state_name, param_name, name_len) == 0) {
2290 snprintf(buf, sizeof(buf), "$%s", state_name + name_len);
2291 return buf;
2292 } else if (state_name[0] == '*' && strcmp(state_name + 1, param_name) == 0) {
2293 return "*$";
2295 return NULL;
2298 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
2300 if (!sym || !sym->ident)
2301 return NULL;
2303 return state_name_to_param_name(name, sym->ident->name);
2306 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym)
2308 struct symbol *type;
2309 const char *sym_name;
2310 int name_len;
2311 static char buf[256];
2314 * mtag_name is different from param_name because mtags can be a struct
2315 * instead of a struct pointer. But we want to treat it like a pointer
2316 * because really an mtag is a pointer. Or in other words, if you pass
2317 * a struct foo then you want to talk about foo.bar but with an mtag
2318 * you want to refer to it as foo->bar.
2322 if (!sym || !sym->ident)
2323 return NULL;
2325 type = get_real_base_type(sym);
2326 if (type && type->type == SYM_BASETYPE)
2327 return "*$";
2329 sym_name = sym->ident->name;
2330 name_len = strlen(sym_name);
2332 if (state_name[name_len] == '.' && /* check for '-' from "->" */
2333 strncmp(state_name, sym_name, name_len) == 0) {
2334 snprintf(buf, sizeof(buf), "$->%s", state_name + name_len + 1);
2335 return buf;
2338 return state_name_to_param_name(state_name, sym_name);
2341 const char *get_mtag_name_expr(struct expression *expr)
2343 char *name;
2344 struct symbol *sym;
2345 const char *ret = NULL;
2347 name = expr_to_var_sym(expr, &sym);
2348 if (!name || !sym)
2349 goto free;
2351 ret = get_mtag_name_var_sym(name, sym);
2352 free:
2353 free_string(name);
2354 return ret;
2357 const char *get_param_name(struct sm_state *sm)
2359 return get_param_name_var_sym(sm->name, sm->sym);
2362 char *get_data_info_name(struct expression *expr)
2364 struct symbol *sym;
2365 char *name;
2366 char buf[256];
2367 char *ret = NULL;
2369 expr = strip_expr(expr);
2370 name = get_member_name(expr);
2371 if (name)
2372 return name;
2373 name = expr_to_var_sym(expr, &sym);
2374 if (!name || !sym)
2375 goto free;
2376 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2377 goto free;
2378 if (sym->ctype.modifiers & MOD_STATIC)
2379 snprintf(buf, sizeof(buf), "static %s", name);
2380 else
2381 snprintf(buf, sizeof(buf), "global %s", name);
2382 ret = alloc_sname(buf);
2383 free:
2384 free_string(name);
2385 return ret;