comparison: improve "foo = min(...);" assignment handling
[smatch.git] / smatch_db.c
blobbe81f6e66b573192bce4f873ccb7294bc0179d9a
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 <unistd.h>
21 #include <ctype.h>
22 #include "smatch.h"
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;
30 static int return_id;
32 #define SQLITE_CACHE_PAGES 1000
34 struct def_callback {
35 int hook_type;
36 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
38 ALLOCATOR(def_callback, "definition db hook callbacks");
39 DECLARE_PTR_LIST(callback_list, struct def_callback);
40 static struct callback_list *select_caller_info_callbacks;
42 struct member_info_callback {
43 int owner;
44 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
46 ALLOCATOR(member_info_callback, "caller_info callbacks");
47 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
48 static struct member_info_cb_list *member_callbacks;
50 struct returned_state_callback {
51 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
53 ALLOCATOR(returned_state_callback, "returned state callbacks");
54 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
55 static struct returned_state_cb_list *returned_state_callbacks;
57 struct returned_member_callback {
58 int owner;
59 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
61 ALLOCATOR(returned_member_callback, "returned member callbacks");
62 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
63 static struct returned_member_cb_list *returned_member_callbacks;
65 struct db_implies_callback {
66 int type;
67 void (*callback)(struct expression *call, struct expression *arg, char *key, char *value);
69 ALLOCATOR(db_implies_callback, "return_implies callbacks");
70 DECLARE_PTR_LIST(db_implies_cb_list, struct db_implies_callback);
71 static struct db_implies_cb_list *return_implies_cb_list;
72 static struct db_implies_cb_list *call_implies_cb_list;
74 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql)
76 char *err = NULL;
77 int rc;
79 if (!db)
80 return;
82 rc = sqlite3_exec(db, sql, callback, data, &err);
83 if (rc != SQLITE_OK && !parse_error) {
84 fprintf(stderr, "SQL error #2: %s\n", err);
85 fprintf(stderr, "SQL: '%s'\n", sql);
86 parse_error = 1;
90 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
92 int i;
94 for (i = 0; i < argc; i++) {
95 if (i != 0)
96 printf(", ");
97 sm_printf("%s", argv[i]);
99 sm_printf("\n");
100 return 0;
103 void debug_sql(struct sqlite3 *db, const char *sql)
105 if (!option_debug)
106 return;
107 sm_msg("%s", sql);
108 sql_exec(db, print_sql_output, NULL, sql);
111 static int replace_count;
112 static char **replace_table;
113 static const char *replace_return_ranges(const char *return_ranges)
115 int i;
117 if (!get_function()) {
118 /* I have no idea why EXPORT_SYMBOL() is here */
119 return return_ranges;
121 for (i = 0; i < replace_count; i += 3) {
122 if (strcmp(replace_table[i + 0], get_function()) == 0) {
123 if (strcmp(replace_table[i + 1], return_ranges) == 0)
124 return replace_table[i + 2];
127 return return_ranges;
131 static char *use_states;
132 static int get_db_state_count(void)
134 struct sm_state *sm;
135 int count = 0;
137 FOR_EACH_SM(__get_cur_stree(), sm) {
138 if (sm->owner == USHRT_MAX)
139 continue;
140 if (use_states[sm->owner])
141 count++;
142 } END_FOR_EACH_SM(sm);
143 return count;
146 void db_ignore_states(int id)
148 use_states[id] = 0;
151 void sql_insert_return_states(int return_id, const char *return_ranges,
152 int type, int param, const char *key, const char *value)
154 if (key && strlen(key) >= 80)
155 return;
156 return_ranges = replace_return_ranges(return_ranges);
157 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
158 get_base_file(), get_function(), (unsigned long)__inline_fn,
159 return_id, return_ranges, fn_static(), type, param, key, value);
162 static struct string_list *common_funcs;
163 static int is_common_function(const char *fn)
165 char *tmp;
167 if (!fn)
168 return 0;
170 if (strncmp(fn, "__builtin_", 10) == 0)
171 return 1;
173 FOR_EACH_PTR(common_funcs, tmp) {
174 if (strcmp(tmp, fn) == 0)
175 return 1;
176 } END_FOR_EACH_PTR(tmp);
178 return 0;
181 static char *function_signature(void)
183 return type_to_str(get_real_base_type(cur_func_sym));
186 void sql_insert_caller_info(struct expression *call, int type,
187 int param, const char *key, const char *value)
189 FILE *tmp_fd = sm_outfd;
190 char *fn;
192 if (!option_info && !__inline_call)
193 return;
195 if (key && strlen(key) >= 80)
196 return;
198 fn = get_fnptr_name(call->fn);
199 if (!fn)
200 return;
202 if (__inline_call) {
203 mem_sql(NULL, NULL,
204 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
205 get_base_file(), get_function(), fn, (unsigned long)call,
206 is_static(call->fn), type, param, key, value);
209 if (!option_info)
210 return;
212 if (strncmp(fn, "__builtin_", 10) == 0)
213 return;
214 if (type != INTERNAL && is_common_function(fn))
215 return;
217 sm_outfd = caller_info_fd;
218 sm_msg("SQL_caller_info: insert into caller_info values ("
219 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
220 get_base_file(), get_function(), fn, is_static(call->fn),
221 type, param, key, value);
222 sm_outfd = tmp_fd;
224 free_string(fn);
227 void sql_insert_function_ptr(const char *fn, const char *struct_name)
229 sql_insert(function_ptr, "'%s', '%s', '%s', 0", get_base_file(), fn,
230 struct_name);
233 void sql_insert_return_implies(int type, int param, const char *key, const char *value)
235 sql_insert_or_ignore(return_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
236 get_base_file(), get_function(), (unsigned long)__inline_fn,
237 fn_static(), type, param, key, value);
240 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
242 sql_insert_or_ignore(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
243 get_base_file(), get_function(), (unsigned long)__inline_fn,
244 fn_static(), type, param, key, value);
247 void sql_insert_function_type_size(const char *member, const char *ranges)
249 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
252 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value)
254 sql_insert(function_type_info, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type, struct_type, member, value);
257 void sql_insert_type_info(int type, const char *member, const char *value)
259 sql_insert_cache(type_info, "'%s', %d, '%s', '%s'", get_base_file(), type, member, value);
262 void sql_insert_local_values(const char *name, const char *value)
264 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
267 void sql_insert_function_type_value(const char *type, const char *value)
269 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
272 void sql_insert_function_type(int param, const char *value)
274 sql_insert(function_type, "'%s', '%s', %d, %d, '%s'",
275 get_base_file(), get_function(), fn_static(), param, value);
278 void sql_insert_parameter_name(int param, const char *value)
280 sql_insert(parameter_name, "'%s', '%s', %d, %d, '%s'",
281 get_base_file(), get_function(), fn_static(), param, value);
284 void sql_insert_data_info(struct expression *data, int type, const char *value)
286 char *data_name;
288 data_name = get_data_info_name(data);
289 if (!data_name)
290 return;
291 sql_insert(data_info, "'%s', '%s', %d, '%s'",
292 is_static(data) ? get_base_file() : "extern",
293 data_name, type, value);
296 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
298 sql_insert(data_info, "'%s', '%s', %d, '%s'",
299 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
300 var, type, value);
303 void sql_save_constraint(const char *con)
305 if (!option_info)
306 return;
308 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", con);
311 void sql_save_constraint_required(const char *data, int op, const char *limit)
313 sql_insert_or_ignore(constraints_required, "'%s', '%s', '%s'", data, show_special(op), limit);
316 void sql_copy_constraint_required(const char *new_limit, const char *old_limit)
318 if (!option_info)
319 return;
321 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
322 "select constraints_required.data, constraints_required.op, '%s' from "
323 "constraints_required where bound = '%s';", new_limit, old_limit);
326 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data)
328 sql_insert_or_ignore(fn_ptr_data_link, "'%s', '%s'", ptr, data);
331 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value)
333 if (fn->type != EXPR_SYMBOL || !fn->symbol->ident)
334 return;
336 sql_insert(fn_data_link, "'%s', '%s', %d, %d, %d, '%s', '%s'",
337 (fn->symbol->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
338 fn->symbol->ident->name,
339 !!(fn->symbol->ctype.modifiers & MOD_STATIC),
340 type, param, key, value);
343 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name)
345 sql_insert(mtag_about, "%lld, '%s', '%s', %d, '%s', '%s'",
346 tag, get_filename(), get_function(), get_lineno(), left_name, right_name);
349 void sql_insert_mtag_data(mtag_t tag, const char *var, int offset, int type, const char *value)
351 sql_insert(mtag_data, "%lld, '%s', %d, %d, '%s'", tag, var, offset, type, value);
354 void sql_insert_mtag_map(mtag_t tag, int offset, mtag_t container)
356 sql_insert(mtag_map, "%lld, %d, %lld", tag, offset, container);
359 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias)
361 sql_insert(mtag_alias, "%lld, %lld", orig, alias);
364 static int save_mtag(void *_tag, int argc, char **argv, char **azColName)
366 mtag_t *saved_tag = _tag;
367 mtag_t new_tag;
369 new_tag = strtoll(argv[0], NULL, 10);
371 if (!*saved_tag)
372 *saved_tag = new_tag;
373 else if (*saved_tag != new_tag)
374 *saved_tag = -1ULL;
376 return 0;
379 int mtag_map_select_container(mtag_t tag, int offset, mtag_t *container)
381 mtag_t tmp = 0;
383 run_sql(save_mtag, &tmp,
384 "select container from mtag_map where tag = %lld and offset = %d;",
385 tag, offset);
387 if (tmp == 0 || tmp == -1ULL)
388 return 0;
389 *container = tmp;
390 return 1;
393 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag)
395 mtag_t tmp = 0;
397 run_sql(save_mtag, &tmp,
398 "select tag from mtag_map where container = %lld and offset = %d;",
399 container, offset);
401 if (tmp == 0 || tmp == -1ULL)
402 return 0;
403 *tag = tmp;
404 return 1;
407 char *get_static_filter(struct symbol *sym)
409 static char sql_filter[1024];
411 /* This can only happen on buggy code. Return invalid SQL. */
412 if (!sym) {
413 sql_filter[0] = '\0';
414 return sql_filter;
417 if (sym->ctype.modifiers & MOD_STATIC) {
418 snprintf(sql_filter, sizeof(sql_filter),
419 "file = '%s' and function = '%s' and static = '1'",
420 get_base_file(), sym->ident->name);
421 } else {
422 snprintf(sql_filter, sizeof(sql_filter),
423 "function = '%s' and static = '0'", sym->ident->name);
426 return sql_filter;
429 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
431 int *row_count = _row_count;
433 *row_count = 0;
434 if (argc != 1)
435 return 0;
436 *row_count = atoi(argv[0]);
437 return 0;
440 static void mark_call_params_untracked(struct expression *call)
442 struct expression *arg;
443 int i = 0;
445 FOR_EACH_PTR(call->args, arg) {
446 mark_untracked(call, i++, "$", NULL);
447 } END_FOR_EACH_PTR(arg);
450 static void sql_select_return_states_pointer(const char *cols,
451 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
453 char *ptr;
454 int return_count = 0;
456 ptr = get_fnptr_name(call->fn);
457 if (!ptr)
458 return;
460 run_sql(get_row_count, &return_count,
461 "select count(*) from return_states join function_ptr "
462 "where return_states.function == function_ptr.function and "
463 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
464 /* The magic number 100 is just from testing on the kernel. */
465 if (return_count > 100) {
466 mark_call_params_untracked(call);
467 return;
470 run_sql(callback, info,
471 "select %s from return_states join function_ptr where "
472 "return_states.function == function_ptr.function and ptr = '%s' "
473 "and searchable = 1 "
474 "order by function_ptr.file, return_states.file, return_id, type;",
475 cols, ptr);
478 static int is_local_symbol(struct expression *expr)
480 if (expr->type != EXPR_SYMBOL)
481 return 0;
482 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
483 return 0;
484 return 1;
487 void sql_select_return_states(const char *cols, struct expression *call,
488 int (*callback)(void*, int, char**, char**), void *info)
490 int row_count = 0;
492 if (is_fake_call(call))
493 return;
495 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol || is_local_symbol(call->fn)) {
496 sql_select_return_states_pointer(cols, call, callback, info);
497 return;
500 if (inlinable(call->fn)) {
501 mem_sql(callback, info,
502 "select %s from return_states where call_id = '%lu' order by return_id, type;",
503 cols, (unsigned long)call);
504 return;
507 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
508 get_static_filter(call->fn->symbol));
509 if (row_count > 3000)
510 return;
512 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
513 cols, get_static_filter(call->fn->symbol));
516 #define CALL_IMPLIES 0
517 #define RETURN_IMPLIES 1
519 struct implies_info {
520 int type;
521 struct db_implies_cb_list *cb_list;
522 struct expression *expr;
523 struct symbol *sym;
526 void sql_select_implies(const char *cols, struct implies_info *info,
527 int (*callback)(void*, int, char**, char**))
529 if (info->type == RETURN_IMPLIES && inlinable(info->expr->fn)) {
530 mem_sql(callback, info,
531 "select %s from return_implies where call_id = '%lu';",
532 cols, (unsigned long)info->expr);
533 return;
536 run_sql(callback, info, "select %s from %s_implies where %s;",
537 cols,
538 info->type == CALL_IMPLIES ? "call" : "return",
539 get_static_filter(info->sym));
542 struct select_caller_info_data {
543 struct stree *final_states;
544 struct timeval start_time;
545 int prev_func_id;
546 int ignore;
547 int results;
550 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName);
552 static void sql_select_caller_info(struct select_caller_info_data *data,
553 const char *cols, struct symbol *sym)
555 if (__inline_fn) {
556 mem_sql(caller_info_callback, data,
557 "select %s from caller_info where call_id = %lu;",
558 cols, (unsigned long)__inline_fn);
559 return;
562 if (sym->ident->name && is_common_function(sym->ident->name))
563 return;
564 run_sql(caller_info_callback, data,
565 "select %s from common_caller_info where %s order by call_id;",
566 cols, get_static_filter(sym));
567 if (data->results)
568 return;
570 run_sql(caller_info_callback, data,
571 "select %s from caller_info where %s order by call_id;",
572 cols, get_static_filter(sym));
575 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
577 struct def_callback *def_callback = __alloc_def_callback(0);
579 def_callback->hook_type = type;
580 def_callback->callback = callback;
581 add_ptr_list(&select_caller_info_callbacks, def_callback);
585 * These call backs are used when the --info option is turned on to print struct
586 * member information. For example foo->bar could have a state in
587 * smatch_extra.c and also check_user.c.
589 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
591 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
593 member_callback->owner = owner;
594 member_callback->callback = callback;
595 add_ptr_list(&member_callbacks, member_callback);
598 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
600 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
602 callback->callback = fn;
603 add_ptr_list(&returned_state_callbacks, callback);
606 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))
608 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
610 member_callback->owner = owner;
611 member_callback->callback = callback;
612 add_ptr_list(&returned_member_callbacks, member_callback);
615 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
617 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
619 cb->type = type;
620 cb->callback = callback;
621 add_ptr_list(&call_implies_cb_list, cb);
624 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
626 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
628 cb->type = type;
629 cb->callback = callback;
630 add_ptr_list(&return_implies_cb_list, cb);
633 struct return_info {
634 struct expression *static_returns_call;
635 struct symbol *return_type;
636 struct range_list *return_range_list;
639 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
641 struct return_info *ret_info = _ret_info;
642 struct range_list *rl;
643 struct expression *call_expr = ret_info->static_returns_call;
645 if (argc != 1)
646 return 0;
647 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
648 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
649 return 0;
652 struct range_list *db_return_vals(struct expression *expr)
654 struct return_info ret_info = {};
655 char buf[64];
656 struct sm_state *sm;
658 if (is_fake_call(expr))
659 return NULL;
661 snprintf(buf, sizeof(buf), "return %p", expr);
662 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
663 if (sm)
664 return clone_rl(estate_rl(sm->state));
665 ret_info.static_returns_call = expr;
666 ret_info.return_type = get_type(expr);
667 if (!ret_info.return_type)
668 return NULL;
670 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
671 return NULL;
673 ret_info.return_range_list = NULL;
674 if (inlinable(expr->fn)) {
675 mem_sql(db_return_callback, &ret_info,
676 "select distinct return from return_states where call_id = '%lu';",
677 (unsigned long)expr);
678 } else {
679 run_sql(db_return_callback, &ret_info,
680 "select distinct return from return_states where %s;",
681 get_static_filter(expr->fn->symbol));
683 return ret_info.return_range_list;
686 struct range_list *db_return_vals_from_str(const char *fn_name)
688 struct return_info ret_info;
690 ret_info.static_returns_call = NULL;
691 ret_info.return_type = &llong_ctype;
692 ret_info.return_range_list = NULL;
694 run_sql(db_return_callback, &ret_info,
695 "select distinct return from return_states where function = '%s';",
696 fn_name);
697 return ret_info.return_range_list;
700 static void match_call_marker(struct expression *expr)
702 struct symbol *type;
704 type = get_type(expr->fn);
705 if (type && type->type == SYM_PTR)
706 type = get_real_base_type(type);
709 * we just want to record something in the database so that if we have
710 * two calls like: frob(4); frob(some_unkown); then on the receiving
711 * side we know that sometimes frob is called with unknown parameters.
714 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", type_to_str(type));
717 static char *show_offset(int offset)
719 static char buf[64];
721 buf[0] = '\0';
722 if (offset != -1)
723 snprintf(buf, sizeof(buf), "(-%d)", offset);
724 return buf;
727 static void print_struct_members(struct expression *call, struct expression *expr, int param, int offset, struct stree *stree,
728 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
730 struct sm_state *sm;
731 char *name;
732 struct symbol *sym;
733 int len;
734 char printed_name[256];
735 int is_address = 0;
737 expr = strip_expr(expr);
738 if (!expr)
739 return;
740 if (expr->type == EXPR_PREOP && expr->op == '&') {
741 expr = strip_expr(expr->unop);
742 is_address = 1;
745 name = expr_to_var_sym(expr, &sym);
746 if (!name || !sym)
747 goto free;
749 len = strlen(name);
750 FOR_EACH_SM(stree, sm) {
751 if (sm->sym != sym)
752 continue;
753 if (strcmp(name, sm->name) == 0) {
754 if (is_address)
755 snprintf(printed_name, sizeof(printed_name), "*$%s", show_offset(offset));
756 else /* these are already handled. fixme: handle them here */
757 continue;
758 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
759 snprintf(printed_name, sizeof(printed_name), "*$%s", show_offset(offset));
760 } else if (strncmp(name, sm->name, len) == 0) {
761 if (isalnum(sm->name[len]))
762 continue;
763 if (is_address)
764 snprintf(printed_name, sizeof(printed_name), "$%s->%s", show_offset(offset), sm->name + len + 1);
765 else
766 snprintf(printed_name, sizeof(printed_name), "$%s%s", show_offset(offset), sm->name + len);
767 } else {
768 continue;
770 callback(call, param, printed_name, sm);
771 } END_FOR_EACH_SM(sm);
772 free:
773 free_string(name);
776 static int param_used_callback(void *_container, int argc, char **argv, char **azColName)
778 char **container = _container;
779 static char buf[256];
781 snprintf(buf, sizeof(buf), "%s", argv[0]);
782 *container = buf;
783 return 0;
786 static void print_container_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
787 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
789 struct expression *tmp;
790 char *container = NULL;
791 int offset;
792 int holder_offset;
793 char *p;
795 if (!call->fn || call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
796 return;
799 * We can't use the in-mem DB because we have to parse the function
800 * first, then we know if it takes a container, then we know to pass it
801 * the container data.
804 run_sql(&param_used_callback, &container,
805 "select key from return_implies where %s and type = %d and key like '%%$(%%' and parameter = %d limit 1;",
806 get_static_filter(call->fn->symbol), CONTAINER, param);
807 if (!container)
808 return;
810 p = strchr(container, '-');
811 if (!p)
812 return;
813 offset = atoi(p);
814 p = strchr(p, ')');
815 if (!p)
816 return;
817 p++;
819 tmp = get_assigned_expr(expr);
820 if (tmp)
821 expr = tmp;
823 if (expr->type != EXPR_PREOP || expr->op != '&')
824 return;
825 expr = strip_expr(expr->unop);
826 holder_offset = get_member_offset_from_deref(expr);
827 if (-holder_offset != offset)
828 return;
830 expr = strip_expr(expr->deref);
831 if (expr->type == EXPR_PREOP && expr->op == '*')
832 expr = strip_expr(expr->unop);
834 print_struct_members(call, expr, param, holder_offset, stree, callback);
837 static void match_call_info(struct expression *call)
839 struct member_info_callback *cb;
840 struct expression *arg;
841 struct stree *stree;
842 char *name;
843 int i;
845 name = get_fnptr_name(call->fn);
846 if (!name)
847 return;
849 FOR_EACH_PTR(member_callbacks, cb) {
850 stree = get_all_states_stree(cb->owner);
851 i = 0;
852 FOR_EACH_PTR(call->args, arg) {
853 print_struct_members(call, arg, i, -1, stree, cb->callback);
854 print_container_struct_members(call, arg, i, stree, cb->callback);
855 i++;
856 } END_FOR_EACH_PTR(arg);
857 free_stree(&stree);
858 } END_FOR_EACH_PTR(cb);
860 free_string(name);
863 static int get_param(int param, char **name, struct symbol **sym)
865 struct symbol *arg;
866 int i;
868 i = 0;
869 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
871 * this is a temporary hack to work around a bug (I think in sparse?)
872 * 2.6.37-rc1:fs/reiserfs/journal.o
873 * If there is a function definition without parameter name found
874 * after a function implementation then it causes a crash.
875 * int foo() {}
876 * int bar(char *);
878 if (arg->ident->name < (char *)100)
879 continue;
880 if (i == param) {
881 *name = arg->ident->name;
882 *sym = arg;
883 return TRUE;
885 i++;
886 } END_FOR_EACH_PTR(arg);
888 return FALSE;
891 static int function_signature_matches(const char *sig)
893 char *my_sig;
895 my_sig = function_signature();
896 if (!sig || !my_sig)
897 return 1; /* default to matching */
898 if (strcmp(my_sig, sig) == 0)
899 return 1;
900 return 0;
903 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
905 struct select_caller_info_data *data = _data;
906 int func_id;
907 long type;
908 long param;
909 char *key;
910 char *value;
911 char *name = NULL;
912 struct symbol *sym = NULL;
913 struct def_callback *def_callback;
914 struct stree *stree;
915 struct timeval cur_time;
917 data->results = 1;
919 if (argc != 5)
920 return 0;
922 gettimeofday(&cur_time, NULL);
923 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
924 return 0;
926 func_id = atoi(argv[0]);
927 errno = 0;
928 type = strtol(argv[1], NULL, 10);
929 param = strtol(argv[2], NULL, 10);
930 if (errno)
931 return 0;
932 key = argv[3];
933 value = argv[4];
935 if (data->prev_func_id == -1)
936 data->prev_func_id = func_id;
937 if (func_id != data->prev_func_id) {
938 stree = __pop_fake_cur_stree();
939 if (!data->ignore)
940 merge_stree(&data->final_states, stree);
941 free_stree(&stree);
942 __push_fake_cur_stree();
943 __unnullify_path();
944 data->prev_func_id = func_id;
945 data->ignore = 0;
948 if (data->ignore)
949 return 0;
950 if (type == INTERNAL &&
951 !function_signature_matches(value)) {
952 data->ignore = 1;
953 return 0;
956 if (param >= 0 && !get_param(param, &name, &sym))
957 return 0;
959 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
960 if (def_callback->hook_type == type)
961 def_callback->callback(name, sym, key, value);
962 } END_FOR_EACH_PTR(def_callback);
964 return 0;
967 static struct string_list *ptr_names_done;
968 static struct string_list *ptr_names;
970 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
972 insert_string(&ptr_names, alloc_string(argv[0]));
973 return 0;
976 static char *get_next_ptr_name(void)
978 char *ptr;
980 FOR_EACH_PTR(ptr_names, ptr) {
981 if (list_has_string(ptr_names_done, ptr))
982 continue;
983 insert_string(&ptr_names_done, ptr);
984 return ptr;
985 } END_FOR_EACH_PTR(ptr);
986 return NULL;
989 static void get_ptr_names(const char *file, const char *name)
991 char sql_filter[1024];
992 int before, after;
994 if (file) {
995 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
996 file, name);
997 } else {
998 snprintf(sql_filter, 1024, "function = '%s';", name);
1001 before = ptr_list_size((struct ptr_list *)ptr_names);
1003 run_sql(get_ptr_name, NULL,
1004 "select distinct ptr from function_ptr where %s",
1005 sql_filter);
1007 after = ptr_list_size((struct ptr_list *)ptr_names);
1008 if (before == after)
1009 return;
1011 while ((name = get_next_ptr_name()))
1012 get_ptr_names(NULL, name);
1015 static void match_data_from_db(struct symbol *sym)
1017 struct select_caller_info_data data = { .prev_func_id = -1 };
1018 struct sm_state *sm;
1019 struct stree *stree;
1020 struct timeval end_time;
1022 if (!sym || !sym->ident)
1023 return;
1025 gettimeofday(&data.start_time, NULL);
1027 __push_fake_cur_stree();
1028 __unnullify_path();
1030 if (!__inline_fn) {
1031 char *ptr;
1033 if (sym->ctype.modifiers & MOD_STATIC)
1034 get_ptr_names(get_base_file(), sym->ident->name);
1035 else
1036 get_ptr_names(NULL, sym->ident->name);
1038 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1039 __free_ptr_list((struct ptr_list **)&ptr_names);
1040 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1041 stree = __pop_fake_cur_stree();
1042 free_stree(&stree);
1043 return;
1046 sql_select_caller_info(&data,
1047 "call_id, type, parameter, key, value",
1048 sym);
1051 stree = __pop_fake_cur_stree();
1052 if (!data.ignore)
1053 merge_stree(&data.final_states, stree);
1054 free_stree(&stree);
1055 __push_fake_cur_stree();
1056 __unnullify_path();
1057 data.prev_func_id = -1;
1058 data.ignore = 0;
1060 FOR_EACH_PTR(ptr_names, ptr) {
1061 run_sql(caller_info_callback, &data,
1062 "select call_id, type, parameter, key, value"
1063 " from common_caller_info where function = '%s' order by call_id",
1064 ptr);
1065 } END_FOR_EACH_PTR(ptr);
1067 if (data.results) {
1068 FOR_EACH_PTR(ptr_names, ptr) {
1069 free_string(ptr);
1070 } END_FOR_EACH_PTR(ptr);
1071 goto free_ptr_names;
1074 FOR_EACH_PTR(ptr_names, ptr) {
1075 run_sql(caller_info_callback, &data,
1076 "select call_id, type, parameter, key, value"
1077 " from caller_info where function = '%s' order by call_id",
1078 ptr);
1079 free_string(ptr);
1080 } END_FOR_EACH_PTR(ptr);
1082 free_ptr_names:
1083 __free_ptr_list((struct ptr_list **)&ptr_names);
1084 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1085 } else {
1086 sql_select_caller_info(&data,
1087 "call_id, type, parameter, key, value",
1088 sym);
1091 stree = __pop_fake_cur_stree();
1092 if (!data.ignore)
1093 merge_stree(&data.final_states, stree);
1094 free_stree(&stree);
1096 gettimeofday(&end_time, NULL);
1097 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1098 FOR_EACH_SM(data.final_states, sm) {
1099 __set_sm(sm);
1100 } END_FOR_EACH_SM(sm);
1103 free_stree(&data.final_states);
1106 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1108 struct implies_info *info = _info;
1109 struct db_implies_callback *cb;
1110 struct expression *arg = NULL;
1111 int type;
1112 int param;
1114 if (argc != 5)
1115 return 0;
1117 type = atoi(argv[1]);
1118 param = atoi(argv[2]);
1120 FOR_EACH_PTR(info->cb_list, cb) {
1121 if (cb->type != type)
1122 continue;
1123 if (param != -1) {
1124 arg = get_argument_from_call_expr(info->expr->args, param);
1125 if (!arg)
1126 continue;
1128 cb->callback(info->expr, arg, argv[3], argv[4]);
1129 } END_FOR_EACH_PTR(cb);
1131 return 0;
1134 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1136 struct implies_info *info = _info;
1137 struct db_implies_callback *cb;
1138 struct expression *arg;
1139 struct symbol *sym;
1140 char *name;
1141 int type;
1142 int param;
1144 if (argc != 5)
1145 return 0;
1147 type = atoi(argv[1]);
1148 param = atoi(argv[2]);
1150 if (!get_param(param, &name, &sym))
1151 return 0;
1152 arg = symbol_expression(sym);
1153 if (!arg)
1154 return 0;
1156 FOR_EACH_PTR(info->cb_list, cb) {
1157 if (cb->type != type)
1158 continue;
1159 cb->callback(info->expr, arg, argv[3], argv[4]);
1160 } END_FOR_EACH_PTR(cb);
1162 return 0;
1165 static void match_return_implies(struct expression *expr)
1167 struct implies_info info = {
1168 .type = RETURN_IMPLIES,
1169 .cb_list = return_implies_cb_list,
1172 if (expr->fn->type != EXPR_SYMBOL ||
1173 !expr->fn->symbol)
1174 return;
1175 info.expr = expr;
1176 info.sym = expr->fn->symbol;
1177 sql_select_implies("function, type, parameter, key, value", &info,
1178 return_implies_callbacks);
1181 static void match_call_implies(struct symbol *sym)
1183 struct implies_info info = {
1184 .type = CALL_IMPLIES,
1185 .cb_list = call_implies_cb_list,
1188 if (!sym || !sym->ident)
1189 return;
1191 info.sym = sym;
1192 sql_select_implies("function, type, parameter, key, value", &info,
1193 call_implies_callbacks);
1196 static void print_initializer_list(struct expression_list *expr_list,
1197 struct symbol *struct_type)
1199 struct expression *expr;
1200 struct symbol *base_type;
1201 char struct_name[256];
1203 FOR_EACH_PTR(expr_list, expr) {
1204 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
1205 print_initializer_list(expr->idx_expression->expr_list, struct_type);
1206 continue;
1208 if (expr->type != EXPR_IDENTIFIER)
1209 continue;
1210 if (!expr->expr_ident)
1211 continue;
1212 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
1213 continue;
1214 base_type = get_type(expr->ident_expression);
1215 if (!base_type || base_type->type != SYM_FN)
1216 continue;
1217 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
1218 struct_type->ident->name, expr->expr_ident->name);
1219 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
1220 struct_name);
1221 } END_FOR_EACH_PTR(expr);
1224 static void global_variable(struct symbol *sym)
1226 struct symbol *struct_type;
1228 if (!sym->ident)
1229 return;
1230 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
1231 return;
1232 struct_type = get_base_type(sym);
1233 if (!struct_type)
1234 return;
1235 if (struct_type->type == SYM_ARRAY) {
1236 struct_type = get_base_type(struct_type);
1237 if (!struct_type)
1238 return;
1240 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
1241 return;
1242 print_initializer_list(sym->initializer->expr_list, struct_type);
1245 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1247 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1250 static void call_return_state_hooks_conditional(struct expression *expr)
1252 struct returned_state_callback *cb;
1253 struct range_list *rl;
1254 char *return_ranges;
1255 int final_pass_orig = final_pass;
1257 __push_fake_cur_stree();
1259 final_pass = 0;
1260 __split_whole_condition(expr->conditional);
1261 final_pass = final_pass_orig;
1263 if (get_implied_rl(expr->cond_true, &rl))
1264 rl = cast_rl(cur_func_return_type(), rl);
1265 else
1266 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
1267 return_ranges = show_rl(rl);
1268 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1270 return_id++;
1271 FOR_EACH_PTR(returned_state_callbacks, cb) {
1272 cb->callback(return_id, return_ranges, expr->cond_true);
1273 } END_FOR_EACH_PTR(cb);
1275 __push_true_states();
1276 __use_false_states();
1278 if (get_implied_rl(expr->cond_false, &rl))
1279 rl = cast_rl(cur_func_return_type(), rl);
1280 else
1281 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
1282 return_ranges = show_rl(rl);
1283 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1285 return_id++;
1286 FOR_EACH_PTR(returned_state_callbacks, cb) {
1287 cb->callback(return_id, return_ranges, expr->cond_false);
1288 } END_FOR_EACH_PTR(cb);
1290 __merge_true_states();
1291 __free_fake_cur_stree();
1294 static void call_return_state_hooks_compare(struct expression *expr)
1296 struct returned_state_callback *cb;
1297 char *return_ranges;
1298 int final_pass_orig = final_pass;
1299 sval_t sval = { .type = &int_ctype };
1300 sval_t ret;
1302 if (!get_implied_value(expr, &ret))
1303 ret.value = -1;
1305 __push_fake_cur_stree();
1307 final_pass = 0;
1308 __split_whole_condition(expr);
1309 final_pass = final_pass_orig;
1311 if (ret.value != 0) {
1312 return_ranges = alloc_sname("1");
1313 sval.value = 1;
1314 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1316 return_id++;
1317 FOR_EACH_PTR(returned_state_callbacks, cb) {
1318 cb->callback(return_id, return_ranges, expr);
1319 } END_FOR_EACH_PTR(cb);
1322 __push_true_states();
1323 __use_false_states();
1325 if (ret.value != 1) {
1326 return_ranges = alloc_sname("0");
1327 sval.value = 0;
1328 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1330 return_id++;
1331 FOR_EACH_PTR(returned_state_callbacks, cb) {
1332 cb->callback(return_id, return_ranges, expr);
1333 } END_FOR_EACH_PTR(cb);
1336 __merge_true_states();
1337 __free_fake_cur_stree();
1340 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1342 struct sm_state *tmp;
1344 FOR_EACH_PTR(slist, tmp) {
1345 if (strcmp(tmp->state->name, sm->state->name) == 0)
1346 return 1;
1347 } END_FOR_EACH_PTR(tmp);
1349 return 0;
1352 static char *get_return_compare_str(struct expression *expr)
1354 char *compare_str;
1355 char *var;
1356 char buf[256];
1357 int comparison;
1358 int param;
1360 compare_str = expr_lte_to_param(expr, -1);
1361 if (compare_str)
1362 return compare_str;
1363 param = get_param_num(expr);
1364 if (param < 0)
1365 return NULL;
1367 var = expr_to_var(expr);
1368 if (!var)
1369 return NULL;
1370 snprintf(buf, sizeof(buf), "%s orig", var);
1371 comparison = get_comparison_strings(var, buf);
1372 free_string(var);
1374 if (!comparison)
1375 return NULL;
1377 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1378 return alloc_sname(buf);
1381 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1383 struct returned_state_callback *cb;
1384 struct range_list *rl;
1385 char *return_ranges;
1386 struct sm_state *tmp;
1387 int ret = 0;
1388 int nr_possible, nr_states;
1389 char *compare_str = NULL;
1390 char buf[128];
1391 struct state_list *already_handled = NULL;
1393 if (!sm || !sm->merged)
1394 return 0;
1396 if (too_many_possible(sm))
1397 return 0;
1399 /* bail if it gets too complicated */
1400 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1401 nr_states = get_db_state_count();
1402 if (nr_states * nr_possible >= 2000)
1403 return 0;
1405 FOR_EACH_PTR(sm->possible, tmp) {
1406 if (tmp->merged)
1407 continue;
1408 if (ptr_in_list(tmp, already_handled))
1409 continue;
1410 add_ptr_list(&already_handled, tmp);
1412 ret = 1;
1413 __push_fake_cur_stree();
1415 overwrite_states_using_pool(sm, tmp);
1417 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1418 return_ranges = show_rl(rl);
1419 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1420 compare_str = get_return_compare_str(expr);
1421 if (compare_str) {
1422 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1423 return_ranges = alloc_sname(buf);
1426 return_id++;
1427 FOR_EACH_PTR(returned_state_callbacks, cb) {
1428 cb->callback(return_id, return_ranges, expr);
1429 } END_FOR_EACH_PTR(cb);
1431 __free_fake_cur_stree();
1432 } END_FOR_EACH_PTR(tmp);
1434 free_slist(&already_handled);
1436 return ret;
1439 static int call_return_state_hooks_split_possible(struct expression *expr)
1441 struct sm_state *sm;
1443 if (!expr || expr_equal_to_param(expr, -1))
1444 return 0;
1446 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1447 return split_possible_helper(sm, expr);
1450 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1452 struct range_list *rl;
1453 char *return_ranges;
1454 sval_t sval;
1455 char *compare_str;
1456 char *math_str;
1457 char buf[128];
1459 *rl_p = NULL;
1461 if (!expr)
1462 return alloc_sname("");
1464 if (get_implied_value(expr, &sval)) {
1465 sval = sval_cast(cur_func_return_type(), sval);
1466 *rl_p = alloc_rl(sval, sval);
1467 return sval_to_str(sval);
1470 compare_str = expr_equal_to_param(expr, -1);
1471 math_str = get_value_in_terms_of_parameter_math(expr);
1473 if (get_implied_rl(expr, &rl)) {
1474 rl = cast_rl(cur_func_return_type(), rl);
1475 return_ranges = show_rl(rl);
1476 } else if (get_imaginary_absolute(expr, &rl)){
1477 rl = cast_rl(cur_func_return_type(), rl);
1478 return alloc_sname(show_rl(rl));
1479 } else {
1480 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1481 return_ranges = show_rl(rl);
1483 *rl_p = rl;
1485 if (compare_str) {
1486 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1487 return alloc_sname(buf);
1489 if (math_str) {
1490 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1491 return alloc_sname(buf);
1493 compare_str = get_return_compare_str(expr);
1494 if (compare_str) {
1495 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1496 return alloc_sname(buf);
1499 return return_ranges;
1502 static bool has_possible_negative(struct sm_state *sm)
1504 struct sm_state *tmp;
1506 FOR_EACH_PTR(sm->possible, tmp) {
1507 if (!estate_rl(tmp->state))
1508 continue;
1509 if (sval_is_negative(estate_min(tmp->state)) &&
1510 sval_is_negative(estate_max(tmp->state)))
1511 return true;
1512 } END_FOR_EACH_PTR(tmp);
1514 return false;
1517 static bool has_possible_zero_null(struct sm_state *sm)
1519 struct sm_state *tmp;
1520 sval_t sval;
1522 FOR_EACH_PTR(sm->possible, tmp) {
1523 if (!estate_get_single_value(tmp->state, &sval))
1524 continue;
1525 if (sval.value == 0)
1526 return true;
1527 } END_FOR_EACH_PTR(tmp);
1529 return false;
1532 static int split_positive_from_negative(struct expression *expr)
1534 struct sm_state *sm;
1535 struct returned_state_callback *cb;
1536 struct range_list *rl;
1537 const char *return_ranges;
1538 struct range_list *ret_rl;
1539 int undo;
1541 /* We're going to print the states 3 times */
1542 if (get_db_state_count() > 10000 / 3)
1543 return 0;
1545 if (!get_implied_rl(expr, &rl) || !rl)
1546 return 0;
1547 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1548 return 0;
1549 /* Forget about INT_MAX and larger */
1550 if (rl_max(rl).value <= 0)
1551 return 0;
1552 if (!sval_is_negative(rl_min(rl)))
1553 return 0;
1555 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1556 if (!sm)
1557 return 0;
1558 if (!has_possible_negative(sm))
1559 return 0;
1561 if (!assume(compare_expression(expr, '>', zero_expr())))
1562 return 0;
1564 return_id++;
1565 return_ranges = get_return_ranges_str(expr, &ret_rl);
1566 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1567 FOR_EACH_PTR(returned_state_callbacks, cb) {
1568 cb->callback(return_id, (char *)return_ranges, expr);
1569 } END_FOR_EACH_PTR(cb);
1571 end_assume();
1573 if (rl_has_sval(rl, sval_type_val(rl_type(rl), 0))) {
1574 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1576 return_id++;
1577 return_ranges = get_return_ranges_str(expr, &ret_rl);
1578 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1579 FOR_EACH_PTR(returned_state_callbacks, cb) {
1580 cb->callback(return_id, (char *)return_ranges, expr);
1581 } END_FOR_EACH_PTR(cb);
1583 if (undo)
1584 end_assume();
1587 undo = assume(compare_expression(expr, '<', zero_expr()));
1589 return_id++;
1590 return_ranges = get_return_ranges_str(expr, &ret_rl);
1591 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1592 FOR_EACH_PTR(returned_state_callbacks, cb) {
1593 cb->callback(return_id, (char *)return_ranges, expr);
1594 } END_FOR_EACH_PTR(cb);
1596 if (undo)
1597 end_assume();
1599 return 1;
1602 static int call_return_state_hooks_split_null_non_null(struct expression *expr)
1604 struct returned_state_callback *cb;
1605 struct range_list *rl;
1606 struct range_list *nonnull_rl;
1607 sval_t null_sval;
1608 struct range_list *null_rl = NULL;
1609 char *return_ranges;
1610 struct sm_state *sm;
1611 struct smatch_state *state;
1612 int nr_states;
1613 int final_pass_orig = final_pass;
1615 if (!expr || expr_equal_to_param(expr, -1))
1616 return 0;
1617 if (expr->type == EXPR_CALL)
1618 return 0;
1619 if (!is_pointer(expr))
1620 return 0;
1622 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1623 if (!sm)
1624 return 0;
1625 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1626 return 0;
1627 state = sm->state;
1628 if (!estate_rl(state))
1629 return 0;
1630 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1631 return 0;
1632 if (!has_possible_zero_null(sm))
1633 return 0;
1635 nr_states = get_db_state_count();
1636 if (option_info && nr_states >= 1500)
1637 return 0;
1639 rl = estate_rl(state);
1641 __push_fake_cur_stree();
1643 final_pass = 0;
1644 __split_whole_condition(expr);
1645 final_pass = final_pass_orig;
1647 nonnull_rl = rl_filter(rl, rl_zero());
1648 return_ranges = show_rl(nonnull_rl);
1649 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1651 return_id++;
1652 FOR_EACH_PTR(returned_state_callbacks, cb) {
1653 cb->callback(return_id, return_ranges, expr);
1654 } END_FOR_EACH_PTR(cb);
1656 __push_true_states();
1657 __use_false_states();
1659 return_ranges = alloc_sname("0");
1660 null_sval = sval_type_val(rl_type(rl), 0);
1661 add_range(&null_rl, null_sval, null_sval);
1662 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1663 return_id++;
1664 FOR_EACH_PTR(returned_state_callbacks, cb) {
1665 cb->callback(return_id, return_ranges, expr);
1666 } END_FOR_EACH_PTR(cb);
1668 __merge_true_states();
1669 __free_fake_cur_stree();
1671 return 1;
1674 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1676 struct sm_state *sm;
1677 struct range_list *rl;
1678 struct range_list *nonzero_rl;
1679 sval_t zero_sval;
1680 struct range_list *zero_rl = NULL;
1681 int nr_states;
1682 struct returned_state_callback *cb;
1683 char *return_ranges;
1684 int final_pass_orig = final_pass;
1686 if (option_project != PROJ_KERNEL)
1687 return 0;
1689 nr_states = get_db_state_count();
1690 if (nr_states > 1500)
1691 return 0;
1693 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1694 if (!sm)
1695 return 0;
1696 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1697 return 0;
1699 rl = estate_rl(sm->state);
1700 if (!rl)
1701 return 0;
1703 if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1704 return 0;
1705 if (rl_max(rl).value != 0)
1706 return 0;
1707 if (!has_possible_zero_null(sm))
1708 return 0;
1710 __push_fake_cur_stree();
1712 final_pass = 0;
1713 __split_whole_condition(expr);
1714 final_pass = final_pass_orig;
1716 nonzero_rl = rl_filter(rl, rl_zero());
1717 return_ranges = show_rl(nonzero_rl);
1718 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1720 return_id++;
1721 FOR_EACH_PTR(returned_state_callbacks, cb) {
1722 cb->callback(return_id, return_ranges, expr);
1723 } END_FOR_EACH_PTR(cb);
1725 __push_true_states();
1726 __use_false_states();
1728 return_ranges = alloc_sname("0");
1729 zero_sval = sval_type_val(rl_type(rl), 0);
1730 add_range(&zero_rl, zero_sval, zero_sval);
1731 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1732 return_id++;
1733 FOR_EACH_PTR(returned_state_callbacks, cb) {
1734 cb->callback(return_id, return_ranges, expr);
1735 } END_FOR_EACH_PTR(cb);
1737 __merge_true_states();
1738 __free_fake_cur_stree();
1740 return 1;
1743 static int is_boolean(struct expression *expr)
1745 struct range_list *rl;
1747 if (!get_implied_rl(expr, &rl))
1748 return 0;
1749 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1750 return 1;
1751 return 0;
1754 static int is_conditional(struct expression *expr)
1756 if (!expr)
1757 return 0;
1758 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1759 return 1;
1760 return 0;
1763 static int splitable_function_call(struct expression *expr)
1765 struct sm_state *sm;
1766 char buf[64];
1768 if (!expr || expr->type != EXPR_CALL)
1769 return 0;
1770 snprintf(buf, sizeof(buf), "return %p", expr);
1771 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1772 return split_possible_helper(sm, expr);
1775 static struct sm_state *find_bool_param(void)
1777 struct stree *start_states;
1778 struct symbol *arg;
1779 struct sm_state *sm, *tmp;
1780 sval_t sval;
1782 start_states = get_start_states();
1784 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1785 if (!arg->ident)
1786 continue;
1787 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1788 if (!sm)
1789 continue;
1790 if (rl_min(estate_rl(sm->state)).value != 0 ||
1791 rl_max(estate_rl(sm->state)).value != 1)
1792 continue;
1793 goto found;
1794 } END_FOR_EACH_PTR_REVERSE(arg);
1796 return NULL;
1798 found:
1800 * Check if it's splitable. If not, then splitting it up is likely not
1801 * useful for the callers.
1803 FOR_EACH_PTR(sm->possible, tmp) {
1804 if (is_merged(tmp))
1805 continue;
1806 if (!estate_get_single_value(tmp->state, &sval))
1807 return NULL;
1808 } END_FOR_EACH_PTR(tmp);
1810 return sm;
1813 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1815 struct returned_state_callback *cb;
1816 struct range_list *ret_rl;
1817 const char *return_ranges;
1818 struct sm_state *tmp;
1819 int ret = 0;
1820 int nr_possible, nr_states;
1821 char *compare_str = NULL;
1822 char buf[128];
1823 struct state_list *already_handled = NULL;
1825 if (!sm || !sm->merged)
1826 return 0;
1828 if (too_many_possible(sm))
1829 return 0;
1831 /* bail if it gets too complicated */
1832 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1833 nr_states = get_db_state_count();
1834 if (nr_states * nr_possible >= 2000)
1835 return 0;
1837 FOR_EACH_PTR(sm->possible, tmp) {
1838 if (tmp->merged)
1839 continue;
1840 if (ptr_in_list(tmp, already_handled))
1841 continue;
1842 add_ptr_list(&already_handled, tmp);
1844 ret = 1;
1845 __push_fake_cur_stree();
1847 overwrite_states_using_pool(sm, tmp);
1849 return_ranges = get_return_ranges_str(expr, &ret_rl);
1850 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1851 compare_str = get_return_compare_str(expr);
1852 if (compare_str) {
1853 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1854 return_ranges = alloc_sname(buf);
1857 return_id++;
1858 FOR_EACH_PTR(returned_state_callbacks, cb) {
1859 cb->callback(return_id, (char *)return_ranges, expr);
1860 } END_FOR_EACH_PTR(cb);
1862 __free_fake_cur_stree();
1863 } END_FOR_EACH_PTR(tmp);
1865 free_slist(&already_handled);
1867 return ret;
1870 static int split_by_bool_param(struct expression *expr)
1872 struct sm_state *start_sm, *sm;
1873 sval_t sval;
1875 start_sm = find_bool_param();
1876 if (!start_sm)
1877 return 0;
1878 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
1879 if (!sm || estate_get_single_value(sm->state, &sval))
1880 return 0;
1881 return split_on_bool_sm(sm, expr);
1884 static int split_by_null_nonnull_param(struct expression *expr)
1886 struct symbol *arg;
1887 struct sm_state *sm;
1888 sval_t zero = {
1889 .type = &ulong_ctype,
1892 /* function must only take one pointer */
1893 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
1894 return 0;
1895 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
1896 if (!arg->ident)
1897 return 0;
1898 if (get_real_base_type(arg)->type != SYM_PTR)
1899 return 0;
1901 if (param_was_set_var_sym(arg->ident->name, arg))
1902 return 0;
1903 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
1904 if (!sm)
1905 return 0;
1907 if (!rl_has_sval(estate_rl(sm->state), zero))
1908 return 0;
1910 return split_on_bool_sm(sm, expr);
1913 struct expression *strip_expr_statement(struct expression *expr)
1915 struct expression *orig = expr;
1916 struct statement *stmt, *last_stmt;
1918 if (!expr)
1919 return NULL;
1920 if (expr->type == EXPR_PREOP && expr->op == '(')
1921 expr = expr->unop;
1922 if (expr->type != EXPR_STATEMENT)
1923 return orig;
1924 stmt = expr->statement;
1925 if (!stmt || stmt->type != STMT_COMPOUND)
1926 return orig;
1928 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1929 if (!last_stmt || last_stmt->type == STMT_LABEL)
1930 last_stmt = last_stmt->label_statement;
1931 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
1932 return orig;
1933 return strip_expr(last_stmt->expression);
1936 static void call_return_state_hooks(struct expression *expr)
1938 struct returned_state_callback *cb;
1939 struct range_list *ret_rl;
1940 const char *return_ranges;
1941 int nr_states;
1942 sval_t sval;
1944 if (__path_is_null())
1945 return;
1947 expr = strip_expr(expr);
1948 expr = strip_expr_statement(expr);
1950 if (is_impossible_path())
1951 goto vanilla;
1953 if (expr && (expr->type == EXPR_COMPARE ||
1954 !get_implied_value(expr, &sval)) &&
1955 (is_condition(expr) || is_boolean(expr))) {
1956 call_return_state_hooks_compare(expr);
1957 return;
1958 } else if (is_conditional(expr)) {
1959 call_return_state_hooks_conditional(expr);
1960 return;
1961 } else if (call_return_state_hooks_split_possible(expr)) {
1962 return;
1963 } else if (call_return_state_hooks_split_null_non_null(expr)) {
1964 return;
1965 } else if (call_return_state_hooks_split_success_fail(expr)) {
1966 return;
1967 } else if (splitable_function_call(expr)) {
1968 return;
1969 } else if (split_positive_from_negative(expr)) {
1970 return;
1971 } else if (split_by_bool_param(expr)) {
1972 } else if (split_by_null_nonnull_param(expr)) {
1973 return;
1976 vanilla:
1977 return_ranges = get_return_ranges_str(expr, &ret_rl);
1978 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1980 return_id++;
1981 nr_states = get_db_state_count();
1982 if (nr_states >= 10000) {
1983 match_return_info(return_id, (char *)return_ranges, expr);
1984 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
1985 return;
1987 FOR_EACH_PTR(returned_state_callbacks, cb) {
1988 cb->callback(return_id, (char *)return_ranges, expr);
1989 } END_FOR_EACH_PTR(cb);
1992 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1994 struct returned_member_callback *cb;
1995 struct stree *stree;
1996 struct sm_state *sm;
1997 struct symbol *type;
1998 char *name;
1999 char member_name[256];
2000 int len;
2002 type = get_type(expr);
2003 if (!type || type->type != SYM_PTR)
2004 return;
2005 name = expr_to_var(expr);
2006 if (!name)
2007 return;
2009 member_name[sizeof(member_name) - 1] = '\0';
2010 strcpy(member_name, "$");
2012 len = strlen(name);
2013 FOR_EACH_PTR(returned_member_callbacks, cb) {
2014 stree = __get_cur_stree();
2015 FOR_EACH_MY_SM(cb->owner, stree, sm) {
2016 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2017 strcpy(member_name, "*$");
2018 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2019 continue;
2021 if (strncmp(sm->name, name, len) != 0)
2022 continue;
2023 if (strncmp(sm->name + len, "->", 2) != 0)
2024 continue;
2025 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2026 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2027 } END_FOR_EACH_SM(sm);
2028 } END_FOR_EACH_PTR(cb);
2030 free_string(name);
2033 static void reset_memdb(struct symbol *sym)
2035 mem_sql(NULL, NULL, "delete from caller_info;");
2036 mem_sql(NULL, NULL, "delete from return_states;");
2037 mem_sql(NULL, NULL, "delete from call_implies;");
2038 mem_sql(NULL, NULL, "delete from return_implies;");
2041 static void match_end_func_info(struct symbol *sym)
2043 if (__path_is_null())
2044 return;
2045 call_return_state_hooks(NULL);
2048 static void match_after_func(struct symbol *sym)
2050 if (!__inline_fn)
2051 reset_memdb(sym);
2054 static void init_memdb(void)
2056 char *err = NULL;
2057 int rc;
2058 const char *schema_files[] = {
2059 "db/db.schema",
2060 "db/caller_info.schema",
2061 "db/common_caller_info.schema",
2062 "db/return_states.schema",
2063 "db/function_type_size.schema",
2064 "db/type_size.schema",
2065 "db/function_type_info.schema",
2066 "db/type_info.schema",
2067 "db/call_implies.schema",
2068 "db/return_implies.schema",
2069 "db/function_ptr.schema",
2070 "db/local_values.schema",
2071 "db/function_type_value.schema",
2072 "db/type_value.schema",
2073 "db/function_type.schema",
2074 "db/data_info.schema",
2075 "db/parameter_name.schema",
2076 "db/constraints.schema",
2077 "db/constraints_required.schema",
2078 "db/fn_ptr_data_link.schema",
2079 "db/fn_data_link.schema",
2080 "db/mtag_about.schema",
2081 "db/mtag_map.schema",
2082 "db/mtag_data.schema",
2083 "db/mtag_alias.schema",
2085 static char buf[4096];
2086 int fd;
2087 int ret;
2088 int i;
2090 rc = sqlite3_open(":memory:", &mem_db);
2091 if (rc != SQLITE_OK) {
2092 printf("Error starting In-Memory database.");
2093 return;
2096 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2097 fd = open_schema_file(schema_files[i]);
2098 if (fd < 0) {
2099 printf("failed to open: %s\n", schema_files[i]);
2100 continue;
2102 ret = read(fd, buf, sizeof(buf));
2103 if (ret < 0) {
2104 printf("failed to read: %s\n", schema_files[i]);
2105 continue;
2107 close(fd);
2108 if (ret == sizeof(buf)) {
2109 printf("Schema file too large: %s (limit %zd bytes)",
2110 schema_files[i], sizeof(buf));
2111 continue;
2113 buf[ret] = '\0';
2114 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2115 if (rc != SQLITE_OK) {
2116 fprintf(stderr, "SQL error #2: %s\n", err);
2117 fprintf(stderr, "%s\n", buf);
2122 static void init_cachedb(void)
2124 char *err = NULL;
2125 int rc;
2126 const char *schema_files[] = {
2127 "db/call_implies.schema",
2128 "db/return_implies.schema",
2129 "db/type_info.schema",
2131 static char buf[4096];
2132 int fd;
2133 int ret;
2134 int i;
2136 rc = sqlite3_open(":memory:", &cache_db);
2137 if (rc != SQLITE_OK) {
2138 printf("Error starting In-Memory database.");
2139 return;
2142 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2143 fd = open_schema_file(schema_files[i]);
2144 if (fd < 0) {
2145 printf("failed to open: %s\n", schema_files[i]);
2146 continue;
2148 ret = read(fd, buf, sizeof(buf));
2149 if (ret < 0) {
2150 printf("failed to read: %s\n", schema_files[i]);
2151 continue;
2153 close(fd);
2154 if (ret == sizeof(buf)) {
2155 printf("Schema file too large: %s (limit %zd bytes)",
2156 schema_files[i], sizeof(buf));
2157 continue;
2159 buf[ret] = '\0';
2160 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2161 if (rc != SQLITE_OK) {
2162 fprintf(stderr, "SQL error #2: %s\n", err);
2163 fprintf(stderr, "%s\n", buf);
2168 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2170 static char buf[4096];
2171 char *p = buf;
2172 char *table = _table;
2173 int i;
2176 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2177 for (i = 0; i < argc; i++) {
2178 if (i)
2179 p += snprintf(p, 4096 - (p - buf), ", ");
2180 p += snprintf(p, 4096 - (p - buf), "'%s'", argv[i]);
2183 p += snprintf(p, 4096 - (p - buf), ");");
2184 if (p - buf > 4096)
2185 return 0;
2187 sm_msg("SQL: %s", buf);
2188 return 0;
2191 static void dump_cache(struct symbol_list *sym_list)
2193 if (!option_info)
2194 return;
2195 cache_sql(&save_cache_data, (char *)"type_info", "select * from type_info;");
2196 cache_sql(&save_cache_data, (char *)"return_implies", "select * from return_implies;");
2197 cache_sql(&save_cache_data, (char *)"call_implies", "select * from call_implies;");
2200 void open_smatch_db(char *db_file)
2202 int rc;
2204 if (option_no_db)
2205 return;
2207 use_states = malloc(num_checks + 1);
2208 memset(use_states, 0xff, num_checks + 1);
2210 init_memdb();
2211 init_cachedb();
2213 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2214 if (rc != SQLITE_OK) {
2215 option_no_db = 1;
2216 return;
2218 run_sql(NULL, NULL,
2219 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2220 return;
2223 static void register_common_funcs(void)
2225 struct token *token;
2226 char *func;
2227 char filename[256];
2229 if (option_project == PROJ_NONE)
2230 strcpy(filename, "common_functions");
2231 else
2232 snprintf(filename, 256, "%s.common_functions", option_project_str);
2234 token = get_tokens_file(filename);
2235 if (!token)
2236 return;
2237 if (token_type(token) != TOKEN_STREAMBEGIN)
2238 return;
2239 token = token->next;
2240 while (token_type(token) != TOKEN_STREAMEND) {
2241 if (token_type(token) != TOKEN_IDENT)
2242 return;
2243 func = alloc_string(show_ident(token->ident));
2244 add_ptr_list(&common_funcs, func);
2245 token = token->next;
2247 clear_token_alloc();
2250 static char *get_next_string(char **str)
2252 static char string[256];
2253 char *start;
2254 char *p = *str;
2255 int len;
2257 if (*p == '\0')
2258 return NULL;
2259 start = p;
2261 while (*p != '\0' && *p != ' ' && *p != '\n')
2262 p++;
2264 len = p - start;
2265 if (len > 256) {
2266 memcpy(string, start, 255);
2267 string[255] = '\0';
2268 printf("return_fix: '%s' too long", string);
2269 **str = '\0';
2270 return NULL;
2272 memcpy(string, start, len);
2273 string[len] = '\0';
2274 if (*p != '\0')
2275 p++;
2276 *str = p;
2277 return string;
2280 static void register_return_replacements(void)
2282 char *func, *orig, *new;
2283 char filename[256];
2284 char buf[4096];
2285 int fd, ret, i;
2286 char *p;
2288 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2289 fd = open_schema_file(filename);
2290 if (fd < 0)
2291 return;
2292 ret = read(fd, buf, sizeof(buf));
2293 close(fd);
2294 if (ret < 0)
2295 return;
2296 if (ret == sizeof(buf)) {
2297 printf("file too large: %s (limit %zd bytes)",
2298 filename, sizeof(buf));
2299 return;
2301 buf[ret] = '\0';
2303 p = buf;
2304 while (*p) {
2305 get_next_string(&p);
2306 replace_count++;
2308 if (replace_count == 0 || replace_count % 3 != 0) {
2309 replace_count = 0;
2310 return;
2312 replace_table = malloc(replace_count * sizeof(char *));
2314 p = buf;
2315 i = 0;
2316 while (*p) {
2317 func = alloc_string(get_next_string(&p));
2318 orig = alloc_string(get_next_string(&p));
2319 new = alloc_string(get_next_string(&p));
2321 replace_table[i++] = func;
2322 replace_table[i++] = orig;
2323 replace_table[i++] = new;
2327 void register_definition_db_callbacks(int id)
2329 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2330 add_hook(&global_variable, BASE_HOOK);
2331 add_hook(&global_variable, DECLARATION_HOOK);
2332 add_split_return_callback(match_return_info);
2333 add_split_return_callback(print_returned_struct_members);
2334 add_hook(&call_return_state_hooks, RETURN_HOOK);
2335 add_hook(&match_end_func_info, END_FUNC_HOOK);
2336 add_hook(&match_after_func, AFTER_FUNC_HOOK);
2338 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2339 add_hook(&match_call_implies, FUNC_DEF_HOOK);
2340 add_hook(&match_return_implies, CALL_HOOK_AFTER_INLINE);
2342 register_common_funcs();
2343 register_return_replacements();
2345 add_hook(&dump_cache, END_FILE_HOOK);
2348 void register_db_call_marker(int id)
2350 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2353 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
2355 struct expression *arg;
2356 char *name = NULL;
2357 char member_name[256];
2359 *sym = NULL;
2361 if (param == -1) {
2362 const char *star = "";
2364 if (expr->type != EXPR_ASSIGNMENT)
2365 return NULL;
2366 name = expr_to_var_sym(expr->left, sym);
2367 if (!name)
2368 return NULL;
2369 if (key[0] == '*') {
2370 star = "*";
2371 key++;
2373 if (strncmp(key, "$", 1) != 0)
2374 return name;
2375 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
2376 free_string(name);
2377 return alloc_string(member_name);
2380 while (expr->type == EXPR_ASSIGNMENT)
2381 expr = strip_expr(expr->right);
2382 if (expr->type != EXPR_CALL)
2383 return NULL;
2385 arg = get_argument_from_call_expr(expr->args, param);
2386 if (!arg)
2387 return NULL;
2389 return get_variable_from_key(arg, key, sym);
2392 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
2394 char buf[256];
2395 char *tmp;
2397 if (!arg)
2398 return NULL;
2400 arg = strip_expr(arg);
2402 if (strcmp(key, "$") == 0)
2403 return expr_to_var_sym(arg, sym);
2405 if (strcmp(key, "*$") == 0) {
2406 if (arg->type == EXPR_PREOP && arg->op == '&') {
2407 arg = strip_expr(arg->unop);
2408 return expr_to_var_sym(arg, sym);
2409 } else {
2410 tmp = expr_to_var_sym(arg, sym);
2411 if (!tmp)
2412 return NULL;
2413 snprintf(buf, sizeof(buf), "*%s", tmp);
2414 free_string(tmp);
2415 return alloc_string(buf);
2419 if (arg->type == EXPR_PREOP && arg->op == '&') {
2420 arg = strip_expr(arg->unop);
2421 tmp = expr_to_var_sym(arg, sym);
2422 if (!tmp)
2423 return NULL;
2424 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
2425 return alloc_string(buf);
2428 tmp = expr_to_var_sym(arg, sym);
2429 if (!tmp)
2430 return NULL;
2431 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
2432 free_string(tmp);
2433 return alloc_string(buf);
2436 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
2438 *vsl = NULL;
2440 if (strcmp("$", key) == 0)
2441 return expr_to_chunk_sym_vsl(arg, sym, vsl);
2442 return get_variable_from_key(arg, key, sym);
2445 const char *state_name_to_param_name(const char *state_name, const char *param_name)
2447 int name_len;
2448 static char buf[256];
2450 name_len = strlen(param_name);
2452 if (strcmp(state_name, param_name) == 0) {
2453 return "$";
2454 } else if (state_name[name_len] == '-' && /* check for '-' from "->" */
2455 strncmp(state_name, param_name, name_len) == 0) {
2456 snprintf(buf, sizeof(buf), "$%s", state_name + name_len);
2457 return buf;
2458 } else if (state_name[0] == '*' && strcmp(state_name + 1, param_name) == 0) {
2459 return "*$";
2461 return NULL;
2464 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
2466 if (!sym || !sym->ident)
2467 return NULL;
2469 return state_name_to_param_name(name, sym->ident->name);
2472 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym)
2474 struct symbol *type;
2475 const char *sym_name;
2476 int name_len;
2477 static char buf[256];
2480 * mtag_name is different from param_name because mtags can be a struct
2481 * instead of a struct pointer. But we want to treat it like a pointer
2482 * because really an mtag is a pointer. Or in other words, if you pass
2483 * a struct foo then you want to talk about foo.bar but with an mtag
2484 * you want to refer to it as foo->bar.
2488 if (!sym || !sym->ident)
2489 return NULL;
2491 type = get_real_base_type(sym);
2492 if (type && type->type == SYM_BASETYPE)
2493 return "*$";
2495 sym_name = sym->ident->name;
2496 name_len = strlen(sym_name);
2498 if (state_name[name_len] == '.' && /* check for '-' from "->" */
2499 strncmp(state_name, sym_name, name_len) == 0) {
2500 snprintf(buf, sizeof(buf), "$->%s", state_name + name_len + 1);
2501 return buf;
2504 return state_name_to_param_name(state_name, sym_name);
2507 const char *get_mtag_name_expr(struct expression *expr)
2509 char *name;
2510 struct symbol *sym;
2511 const char *ret = NULL;
2513 name = expr_to_var_sym(expr, &sym);
2514 if (!name || !sym)
2515 goto free;
2517 ret = get_mtag_name_var_sym(name, sym);
2518 free:
2519 free_string(name);
2520 return ret;
2523 const char *get_param_name(struct sm_state *sm)
2525 return get_param_name_var_sym(sm->name, sm->sym);
2528 char *get_data_info_name(struct expression *expr)
2530 struct symbol *sym;
2531 char *name;
2532 char buf[256];
2533 char *ret = NULL;
2535 expr = strip_expr(expr);
2536 name = get_member_name(expr);
2537 if (name)
2538 return name;
2539 name = expr_to_var_sym(expr, &sym);
2540 if (!name || !sym)
2541 goto free;
2542 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2543 goto free;
2544 if (sym->ctype.modifiers & MOD_STATIC)
2545 snprintf(buf, sizeof(buf), "static %s", name);
2546 else
2547 snprintf(buf, sizeof(buf), "global %s", name);
2548 ret = alloc_sname(buf);
2549 free:
2550 free_string(name);
2551 return ret;