11f5207b7SJohn Levon /*
21f5207b7SJohn Levon  * Copyright (C) 2010 Dan Carpenter.
31f5207b7SJohn Levon  *
41f5207b7SJohn Levon  * This program is free software; you can redistribute it and/or
51f5207b7SJohn Levon  * modify it under the terms of the GNU General Public License
61f5207b7SJohn Levon  * as published by the Free Software Foundation; either version 2
71f5207b7SJohn Levon  * of the License, or (at your option) any later version.
81f5207b7SJohn Levon  *
91f5207b7SJohn Levon  * This program is distributed in the hope that it will be useful,
101f5207b7SJohn Levon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
111f5207b7SJohn Levon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
121f5207b7SJohn Levon  * GNU General Public License for more details.
131f5207b7SJohn Levon  *
141f5207b7SJohn Levon  * You should have received a copy of the GNU General Public License
151f5207b7SJohn Levon  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
161f5207b7SJohn Levon  */
171f5207b7SJohn Levon 
181f5207b7SJohn Levon #include <string.h>
191f5207b7SJohn Levon #include <errno.h>
201f5207b7SJohn Levon #include <unistd.h>
211f5207b7SJohn Levon #include <ctype.h>
221f5207b7SJohn Levon #include "smatch.h"
231f5207b7SJohn Levon #include "smatch_slist.h"
241f5207b7SJohn Levon #include "smatch_extra.h"
251f5207b7SJohn Levon 
261f5207b7SJohn Levon struct sqlite3 *smatch_db;
271f5207b7SJohn Levon struct sqlite3 *mem_db;
281f5207b7SJohn Levon struct sqlite3 *cache_db;
291f5207b7SJohn Levon 
305a0e240fSJohn Levon int debug_db;
315a0e240fSJohn Levon 
321f5207b7SJohn Levon static int return_id;
331f5207b7SJohn Levon 
345a0e240fSJohn Levon static void call_return_state_hooks(struct expression *expr);
355a0e240fSJohn Levon 
361f5207b7SJohn Levon #define SQLITE_CACHE_PAGES 1000
371f5207b7SJohn Levon 
381f5207b7SJohn Levon struct def_callback {
391f5207b7SJohn Levon 	int hook_type;
401f5207b7SJohn Levon 	void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
411f5207b7SJohn Levon };
421f5207b7SJohn Levon ALLOCATOR(def_callback, "definition db hook callbacks");
431f5207b7SJohn Levon DECLARE_PTR_LIST(callback_list, struct def_callback);
441f5207b7SJohn Levon static struct callback_list *select_caller_info_callbacks;
451f5207b7SJohn Levon 
461f5207b7SJohn Levon struct member_info_callback {
471f5207b7SJohn Levon 	int owner;
481f5207b7SJohn Levon 	void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
491f5207b7SJohn Levon };
501f5207b7SJohn Levon ALLOCATOR(member_info_callback, "caller_info callbacks");
511f5207b7SJohn Levon DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
521f5207b7SJohn Levon static struct member_info_cb_list *member_callbacks;
536523a3aaSJohn Levon static struct member_info_cb_list *member_callbacks_new;
541f5207b7SJohn Levon 
551f5207b7SJohn Levon struct returned_state_callback {
561f5207b7SJohn Levon 	void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
571f5207b7SJohn Levon };
581f5207b7SJohn Levon ALLOCATOR(returned_state_callback, "returned state callbacks");
591f5207b7SJohn Levon DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
601f5207b7SJohn Levon static struct returned_state_cb_list *returned_state_callbacks;
611f5207b7SJohn Levon 
621f5207b7SJohn Levon struct returned_member_callback {
631f5207b7SJohn Levon 	int owner;
641f5207b7SJohn Levon 	void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
651f5207b7SJohn Levon };
661f5207b7SJohn Levon ALLOCATOR(returned_member_callback, "returned member callbacks");
671f5207b7SJohn Levon DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
681f5207b7SJohn Levon static struct returned_member_cb_list *returned_member_callbacks;
691f5207b7SJohn Levon 
701f5207b7SJohn Levon struct db_implies_callback {
711f5207b7SJohn Levon 	int type;
721f5207b7SJohn Levon 	void (*callback)(struct expression *call, struct expression *arg, char *key, char *value);
731f5207b7SJohn Levon };
741f5207b7SJohn Levon ALLOCATOR(db_implies_callback, "return_implies callbacks");
751f5207b7SJohn Levon DECLARE_PTR_LIST(db_implies_cb_list, struct db_implies_callback);
761f5207b7SJohn Levon static struct db_implies_cb_list *return_implies_cb_list;
771f5207b7SJohn Levon static struct db_implies_cb_list *call_implies_cb_list;
781f5207b7SJohn Levon 
791f5207b7SJohn Levon /* silently truncates if needed. */
escape_newlines(const char * str)801f5207b7SJohn Levon char *escape_newlines(const char *str)
811f5207b7SJohn Levon {
821f5207b7SJohn Levon 	char buf[1024] = "";
831f5207b7SJohn Levon 	bool found = false;
841f5207b7SJohn Levon 	int i, j;
851f5207b7SJohn Levon 
861f5207b7SJohn Levon 	for (i = 0, j = 0; str[i] != '\0' && j != sizeof(buf); i++, j++) {
87efe51d0cSJohn Levon 		if (str[i] != '\r' && str[i] != '\n') {
881f5207b7SJohn Levon 			buf[j] = str[i];
891f5207b7SJohn Levon 			continue;
901f5207b7SJohn Levon 		}
911f5207b7SJohn Levon 
921f5207b7SJohn Levon 		found = true;
931f5207b7SJohn Levon 		buf[j++] = '\\';
941f5207b7SJohn Levon 		if (j == sizeof(buf))
951f5207b7SJohn Levon 			 break;
961f5207b7SJohn Levon 		buf[j] = 'n';
971f5207b7SJohn Levon 	}
981f5207b7SJohn Levon 
991f5207b7SJohn Levon 	if (!found)
1001f5207b7SJohn Levon 		return alloc_sname(str);
1011f5207b7SJohn Levon 
1021f5207b7SJohn Levon 	if (j == sizeof(buf))
1031f5207b7SJohn Levon 		buf[j - 1] = '\0';
1041f5207b7SJohn Levon 	return alloc_sname(buf);
1051f5207b7SJohn Levon }
1061f5207b7SJohn Levon 
print_sql_output(void * unused,int argc,char ** argv,char ** azColName)1071f5207b7SJohn Levon static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
1081f5207b7SJohn Levon {
1091f5207b7SJohn Levon 	int i;
1101f5207b7SJohn Levon 
1111f5207b7SJohn Levon 	for (i = 0; i < argc; i++) {
1121f5207b7SJohn Levon 		if (i != 0)
1135a0e240fSJohn Levon 			sm_printf(", ");
1141f5207b7SJohn Levon 		sm_printf("%s", argv[i]);
1151f5207b7SJohn Levon 	}
1161f5207b7SJohn Levon 	sm_printf("\n");
1171f5207b7SJohn Levon 	return 0;
1181f5207b7SJohn Levon }
1191f5207b7SJohn Levon 
sql_exec(struct sqlite3 * db,int (* callback)(void *,int,char **,char **),void * data,const char * sql)1201f5207b7SJohn Levon void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql)
1211f5207b7SJohn Levon {
1221f5207b7SJohn Levon 	char *err = NULL;
1231f5207b7SJohn Levon 	int rc;
1241f5207b7SJohn Levon 
1251f5207b7SJohn Levon 	if (!db)
1261f5207b7SJohn Levon 		return;
1271f5207b7SJohn Levon 
1285a0e240fSJohn Levon 	if (option_debug || debug_db) {
1291f5207b7SJohn Levon 		sm_msg("%s", sql);
1301f5207b7SJohn Levon 		if (strncasecmp(sql, "select", strlen("select")) == 0)
1311f5207b7SJohn Levon 			sqlite3_exec(db, sql, print_sql_output, NULL, NULL);
1321f5207b7SJohn Levon 	}
1331f5207b7SJohn Levon 
1341f5207b7SJohn Levon 	rc = sqlite3_exec(db, sql, callback, data, &err);
1351f5207b7SJohn Levon 	if (rc != SQLITE_OK && !parse_error) {
1361f5207b7SJohn Levon 		sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err);
1371f5207b7SJohn Levon 		sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql);
1381f5207b7SJohn Levon 		parse_error = 1;
1391f5207b7SJohn Levon 	}
1401f5207b7SJohn Levon }
1411f5207b7SJohn Levon 
1421f5207b7SJohn Levon static int replace_count;
1431f5207b7SJohn Levon static char **replace_table;
replace_return_ranges(const char * return_ranges)1441f5207b7SJohn Levon static const char *replace_return_ranges(const char *return_ranges)
1451f5207b7SJohn Levon {
1461f5207b7SJohn Levon 	int i;
1471f5207b7SJohn Levon 
1481f5207b7SJohn Levon 	if (!get_function()) {
1491f5207b7SJohn Levon 		/* I have no idea why EXPORT_SYMBOL() is here */
1501f5207b7SJohn Levon 		return return_ranges;
1511f5207b7SJohn Levon 	}
1521f5207b7SJohn Levon 	for (i = 0; i < replace_count; i += 3) {
1531f5207b7SJohn Levon 		if (strcmp(replace_table[i + 0], get_function()) == 0) {
1541f5207b7SJohn Levon 			if (strcmp(replace_table[i + 1], return_ranges) == 0)
1551f5207b7SJohn Levon 				return replace_table[i + 2];
1561f5207b7SJohn Levon 		}
1571f5207b7SJohn Levon 	}
1581f5207b7SJohn Levon 	return return_ranges;
1591f5207b7SJohn Levon }
1601f5207b7SJohn Levon 
1611f5207b7SJohn Levon 
1621f5207b7SJohn Levon static char *use_states;
get_db_state_count(void)1631f5207b7SJohn Levon static int get_db_state_count(void)
1641f5207b7SJohn Levon {
1651f5207b7SJohn Levon 	struct sm_state *sm;
1661f5207b7SJohn Levon 	int count = 0;
1671f5207b7SJohn Levon 
1681f5207b7SJohn Levon 	FOR_EACH_SM(__get_cur_stree(), sm) {
1691f5207b7SJohn Levon 		if (sm->owner == USHRT_MAX)
1701f5207b7SJohn Levon 			continue;
1711f5207b7SJohn Levon 		if (use_states[sm->owner])
1721f5207b7SJohn Levon 			count++;
1731f5207b7SJohn Levon 	} END_FOR_EACH_SM(sm);
1741f5207b7SJohn Levon 	return count;
1751f5207b7SJohn Levon }
1761f5207b7SJohn Levon 
db_ignore_states(int id)1771f5207b7SJohn Levon void db_ignore_states(int id)
1781f5207b7SJohn Levon {
1791f5207b7SJohn Levon 	use_states[id] = 0;
1801f5207b7SJohn Levon }
1811f5207b7SJohn Levon 
1826523a3aaSJohn Levon unsigned long long __fn_mtag;
set_fn_mtag(struct symbol * sym)1836523a3aaSJohn Levon static void set_fn_mtag(struct symbol *sym)
1846523a3aaSJohn Levon {
1856523a3aaSJohn Levon 	char buf[128];
1866523a3aaSJohn Levon 
1876523a3aaSJohn Levon 	if (cur_func_sym->ctype.modifiers & MOD_STATIC)
1886523a3aaSJohn Levon 		snprintf(buf, sizeof(buf), "%s %s", get_base_file(), get_function());
1896523a3aaSJohn Levon 	else
1906523a3aaSJohn Levon 		snprintf(buf, sizeof(buf), "extern %s", get_function());
1916523a3aaSJohn Levon 
1926523a3aaSJohn Levon 	__fn_mtag = str_to_mtag(buf);
1936523a3aaSJohn Levon }
1946523a3aaSJohn Levon 
sql_insert_return_states(int return_id,const char * return_ranges,int type,int param,const char * key,const char * value)1951f5207b7SJohn Levon void sql_insert_return_states(int return_id, const char *return_ranges,
1961f5207b7SJohn Levon 		int type, int param, const char *key, const char *value)
1971f5207b7SJohn Levon {
1986523a3aaSJohn Levon 	unsigned long long id;
1996523a3aaSJohn Levon 
2006523a3aaSJohn Levon 
2011f5207b7SJohn Levon 	if (key && strlen(key) >= 80)
2021f5207b7SJohn Levon 		return;
2036523a3aaSJohn Levon 	if (__inline_fn)
2046523a3aaSJohn Levon 		id = (unsigned long)__inline_fn;
2056523a3aaSJohn Levon 	else
2066523a3aaSJohn Levon 		id = __fn_mtag;
2076523a3aaSJohn Levon 
2081f5207b7SJohn Levon 	return_ranges = replace_return_ranges(return_ranges);
2096523a3aaSJohn Levon 	sql_insert(return_states, "'%s', '%s', %llu, %d, '%s', %d, %d, %d, '%s', '%s'",
2106523a3aaSJohn Levon 		   get_base_file(), get_function(), id, return_id,
2116523a3aaSJohn Levon 		   return_ranges, fn_static(), type, param, key, value);
2121f5207b7SJohn Levon }
2131f5207b7SJohn Levon 
2141f5207b7SJohn Levon static struct string_list *common_funcs;
is_common_function(const char * fn)2151f5207b7SJohn Levon static int is_common_function(const char *fn)
2161f5207b7SJohn Levon {
2171f5207b7SJohn Levon 	char *tmp;
2181f5207b7SJohn Levon 
2191f5207b7SJohn Levon 	if (!fn)
2201f5207b7SJohn Levon 		return 0;
2211f5207b7SJohn Levon 
2221f5207b7SJohn Levon 	if (strncmp(fn, "__builtin_", 10) == 0)
2231f5207b7SJohn Levon 		return 1;
2241f5207b7SJohn Levon 
2251f5207b7SJohn Levon 	FOR_EACH_PTR(common_funcs, tmp) {
2261f5207b7SJohn Levon 		if (strcmp(tmp, fn) == 0)
2271f5207b7SJohn Levon 			return 1;
2281f5207b7SJohn Levon 	} END_FOR_EACH_PTR(tmp);
2291f5207b7SJohn Levon 
2301f5207b7SJohn Levon 	return 0;
2311f5207b7SJohn Levon }
2321f5207b7SJohn Levon 
function_signature(void)2331f5207b7SJohn Levon static char *function_signature(void)
2341f5207b7SJohn Levon {
2351f5207b7SJohn Levon 	return type_to_str(get_real_base_type(cur_func_sym));
2361f5207b7SJohn Levon }
2371f5207b7SJohn Levon 
sql_insert_caller_info(struct expression * call,int type,int param,const char * key,const char * value)2381f5207b7SJohn Levon void sql_insert_caller_info(struct expression *call, int type,
2391f5207b7SJohn Levon 		int param, const char *key, const char *value)
2401f5207b7SJohn Levon {
2411f5207b7SJohn Levon 	FILE *tmp_fd = sm_outfd;
2421f5207b7SJohn Levon 	char *fn;
2431f5207b7SJohn Levon 
2441f5207b7SJohn Levon 	if (!option_info && !__inline_call)
2451f5207b7SJohn Levon 		return;
2461f5207b7SJohn Levon 
2471f5207b7SJohn Levon 	if (key && strlen(key) >= 80)
2481f5207b7SJohn Levon 		return;
2491f5207b7SJohn Levon 
2501f5207b7SJohn Levon 	fn = get_fnptr_name(call->fn);
2511f5207b7SJohn Levon 	if (!fn)
2521f5207b7SJohn Levon 		return;
2531f5207b7SJohn Levon 
2541f5207b7SJohn Levon 	if (__inline_call) {
2551f5207b7SJohn Levon 		mem_sql(NULL, NULL,
2561f5207b7SJohn Levon 			"insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
2571f5207b7SJohn Levon 			get_base_file(), get_function(), fn, (unsigned long)call,
2581f5207b7SJohn Levon 			is_static(call->fn), type, param, key, value);
2591f5207b7SJohn Levon 	}
2601f5207b7SJohn Levon 
2611f5207b7SJohn Levon 	if (!option_info)
2621f5207b7SJohn Levon 		return;
2631f5207b7SJohn Levon 
2641f5207b7SJohn Levon 	if (strncmp(fn, "__builtin_", 10) == 0)
2651f5207b7SJohn Levon 		return;
2661f5207b7SJohn Levon 	if (type != INTERNAL && is_common_function(fn))
2671f5207b7SJohn Levon 		return;
2681f5207b7SJohn Levon 
2691f5207b7SJohn Levon 	sm_outfd = caller_info_fd;
2701f5207b7SJohn Levon 	sm_msg("SQL_caller_info: insert into caller_info values ("
2711f5207b7SJohn Levon 	       "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
2721f5207b7SJohn Levon 	       get_base_file(), get_function(), fn, is_static(call->fn),
2731f5207b7SJohn Levon 	       type, param, key, value);
2741f5207b7SJohn Levon 	sm_outfd = tmp_fd;
2751f5207b7SJohn Levon 
2761f5207b7SJohn Levon 	free_string(fn);
2771f5207b7SJohn Levon }
2781f5207b7SJohn Levon 
sql_insert_function_ptr(const char * fn,const char * struct_name)2791f5207b7SJohn Levon void sql_insert_function_ptr(const char *fn, const char *struct_name)
2801f5207b7SJohn Levon {
281efe51d0cSJohn Levon 	sql_insert_or_ignore(function_ptr, "'%s', '%s', '%s', 0",
282efe51d0cSJohn Levon 			     get_base_file(), fn, struct_name);
2831f5207b7SJohn Levon }
2841f5207b7SJohn Levon 
sql_insert_return_implies(int type,int param,const char * key,const char * value)2851f5207b7SJohn Levon void sql_insert_return_implies(int type, int param, const char *key, const char *value)
2861f5207b7SJohn Levon {
2871f5207b7SJohn Levon 	sql_insert_or_ignore(return_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
2881f5207b7SJohn Levon 		get_base_file(), get_function(), (unsigned long)__inline_fn,
2891f5207b7SJohn Levon 		fn_static(), type, param, key, value);
2901f5207b7SJohn Levon }
2911f5207b7SJohn Levon 
sql_insert_call_implies(int type,int param,const char * key,const char * value)2921f5207b7SJohn Levon void sql_insert_call_implies(int type, int param, const char *key, const char *value)
2931f5207b7SJohn Levon {
2941f5207b7SJohn Levon 	sql_insert_or_ignore(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
2951f5207b7SJohn Levon 		get_base_file(), get_function(), (unsigned long)__inline_fn,
2961f5207b7SJohn Levon 		fn_static(), type, param, key, value);
2971f5207b7SJohn Levon }
2981f5207b7SJohn Levon 
sql_insert_function_type_size(const char * member,const char * ranges)2991f5207b7SJohn Levon void sql_insert_function_type_size(const char *member, const char *ranges)
3001f5207b7SJohn Levon {
3011f5207b7SJohn Levon 	sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
3021f5207b7SJohn Levon }
3031f5207b7SJohn Levon 
sql_insert_function_type_info(int type,const char * struct_type,const char * member,const char * value)3041f5207b7SJohn Levon void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value)
3051f5207b7SJohn Levon {
3061f5207b7SJohn Levon 	sql_insert(function_type_info, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type, struct_type, member, value);
3071f5207b7SJohn Levon }
3081f5207b7SJohn Levon 
sql_insert_type_info(int type,const char * member,const char * value)3091f5207b7SJohn Levon void sql_insert_type_info(int type, const char *member, const char *value)
3101f5207b7SJohn Levon {
3111f5207b7SJohn Levon 	sql_insert_cache(type_info, "'%s', %d, '%s', '%s'", get_base_file(), type, member, value);
3121f5207b7SJohn Levon }
3131f5207b7SJohn Levon 
sql_insert_local_values(const char * name,const char * value)3141f5207b7SJohn Levon void sql_insert_local_values(const char *name, const char *value)
3151f5207b7SJohn Levon {
3161f5207b7SJohn Levon 	sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
3171f5207b7SJohn Levon }
3181f5207b7SJohn Levon 
sql_insert_function_type_value(const char * type,const char * value)3191f5207b7SJohn Levon void sql_insert_function_type_value(const char *type, const char *value)
3201f5207b7SJohn Levon {
3211f5207b7SJohn Levon 	sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
3221f5207b7SJohn Levon }
3231f5207b7SJohn Levon 
sql_insert_function_type(int param,const char * value)3241f5207b7SJohn Levon void sql_insert_function_type(int param, const char *value)
3251f5207b7SJohn Levon {
3261f5207b7SJohn Levon 	sql_insert(function_type, "'%s', '%s', %d, %d, '%s'",
3271f5207b7SJohn Levon 		   get_base_file(), get_function(), fn_static(), param, value);
3281f5207b7SJohn Levon }
3291f5207b7SJohn Levon 
sql_insert_parameter_name(int param,const char * value)3301f5207b7SJohn Levon void sql_insert_parameter_name(int param, const char *value)
3311f5207b7SJohn Levon {
3321f5207b7SJohn Levon 	sql_insert(parameter_name, "'%s', '%s', %d, %d, '%s'",
3331f5207b7SJohn Levon 		   get_base_file(), get_function(), fn_static(), param, value);
3341f5207b7SJohn Levon }
3351f5207b7SJohn Levon 
sql_insert_data_info(struct expression * data,int type,const char * value)3361f5207b7SJohn Levon void sql_insert_data_info(struct expression *data, int type, const char *value)
3371f5207b7SJohn Levon {
3381f5207b7SJohn Levon 	char *data_name;
3391f5207b7SJohn Levon 
3401f5207b7SJohn Levon 	data_name = get_data_info_name(data);
3411f5207b7SJohn Levon 	if (!data_name)
3421f5207b7SJohn Levon 		return;
3431f5207b7SJohn Levon 	sql_insert(data_info, "'%s', '%s', %d, '%s'",
3441f5207b7SJohn Levon 		   is_static(data) ? get_base_file() : "extern",
3451f5207b7SJohn Levon 		   data_name, type, value);
3461f5207b7SJohn Levon }
3471f5207b7SJohn Levon 
sql_insert_data_info_var_sym(const char * var,struct symbol * sym,int type,const char * value)3481f5207b7SJohn Levon void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
3491f5207b7SJohn Levon {
3501f5207b7SJohn Levon 	sql_insert(data_info, "'%s', '%s', %d, '%s'",
3511f5207b7SJohn Levon 		   (sym->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
3521f5207b7SJohn Levon 		   var, type, value);
3531f5207b7SJohn Levon }
3541f5207b7SJohn Levon 
sql_save_constraint(const char * con)3551f5207b7SJohn Levon void sql_save_constraint(const char *con)
3561f5207b7SJohn Levon {
3571f5207b7SJohn Levon 	if (!option_info)
3581f5207b7SJohn Levon 		return;
3591f5207b7SJohn Levon 
360efe51d0cSJohn Levon         sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con));
3611f5207b7SJohn Levon }
3621f5207b7SJohn Levon 
sql_save_constraint_required(const char * data,int op,const char * limit)3631f5207b7SJohn Levon void sql_save_constraint_required(const char *data, int op, const char *limit)
3641f5207b7SJohn Levon {
3651f5207b7SJohn Levon 	sql_insert_or_ignore(constraints_required, "'%s', '%s', '%s'", data, show_special(op), limit);
3661f5207b7SJohn Levon }
3671f5207b7SJohn Levon 
sql_copy_constraint_required(const char * new_limit,const char * old_limit)3681f5207b7SJohn Levon void sql_copy_constraint_required(const char *new_limit, const char *old_limit)
3691f5207b7SJohn Levon {
3701f5207b7SJohn Levon 	if (!option_info)
3711f5207b7SJohn Levon 		return;
3721f5207b7SJohn Levon 
3731f5207b7SJohn Levon 	sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
3741f5207b7SJohn Levon 		"select constraints_required.data, constraints_required.op, '%s' from "
3751f5207b7SJohn Levon 		"constraints_required where bound = '%s';", new_limit, old_limit);
3761f5207b7SJohn Levon }
3771f5207b7SJohn Levon 
sql_insert_fn_ptr_data_link(const char * ptr,const char * data)3781f5207b7SJohn Levon void sql_insert_fn_ptr_data_link(const char *ptr, const char *data)
3791f5207b7SJohn Levon {
3801f5207b7SJohn Levon 	sql_insert_or_ignore(fn_ptr_data_link, "'%s', '%s'", ptr, data);
3811f5207b7SJohn Levon }
3821f5207b7SJohn Levon 
sql_insert_fn_data_link(struct expression * fn,int type,int param,const char * key,const char * value)3831f5207b7SJohn Levon void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value)
3841f5207b7SJohn Levon {
3851f5207b7SJohn Levon 	if (fn->type != EXPR_SYMBOL || !fn->symbol->ident)
3861f5207b7SJohn Levon 		return;
3871f5207b7SJohn Levon 
3881f5207b7SJohn Levon 	sql_insert(fn_data_link, "'%s', '%s', %d, %d, %d, '%s', '%s'",
3891f5207b7SJohn Levon 		   (fn->symbol->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
3901f5207b7SJohn Levon 		   fn->symbol->ident->name,
3911f5207b7SJohn Levon 		   !!(fn->symbol->ctype.modifiers & MOD_STATIC),
3921f5207b7SJohn Levon 		   type, param, key, value);
3931f5207b7SJohn Levon }
3941f5207b7SJohn Levon 
sql_insert_mtag_about(mtag_t tag,const char * left_name,const char * right_name)3951f5207b7SJohn Levon void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name)
3961f5207b7SJohn Levon {
3976523a3aaSJohn Levon 	sql_insert_cache(mtag_about, "%lld, '%s', '%s', %d, '%s', '%s'",
3986523a3aaSJohn Levon 			 tag, get_filename(), get_function(), get_lineno(),
3996523a3aaSJohn Levon 			 left_name, right_name);
4001f5207b7SJohn Levon }
4011f5207b7SJohn Levon 
sql_insert_mtag_info(mtag_t tag,int type,const char * value)4026523a3aaSJohn Levon void sql_insert_mtag_info(mtag_t tag, int type, const char *value)
4031f5207b7SJohn Levon {
4046523a3aaSJohn Levon 	sql_insert_cache(mtag_info, "'%s', %lld, %d, '%s'", get_filename(), tag, type, value);
4056523a3aaSJohn Levon }
4066523a3aaSJohn Levon 
sql_insert_mtag_map(mtag_t container,int container_offset,mtag_t tag,int tag_offset)4076523a3aaSJohn Levon void sql_insert_mtag_map(mtag_t container, int container_offset, mtag_t tag, int tag_offset)
4086523a3aaSJohn Levon {
4096523a3aaSJohn Levon 	sql_insert(mtag_map, "%lld, %d, %lld, %d", container, container_offset, tag, tag_offset);
4101f5207b7SJohn Levon }
4111f5207b7SJohn Levon 
sql_insert_mtag_alias(mtag_t orig,mtag_t alias)4121f5207b7SJohn Levon void sql_insert_mtag_alias(mtag_t orig, mtag_t alias)
4131f5207b7SJohn Levon {
4141f5207b7SJohn Levon 	sql_insert(mtag_alias, "%lld, %lld", orig, alias);
4151f5207b7SJohn Levon }
4161f5207b7SJohn Levon 
save_mtag(void * _tag,int argc,char ** argv,char ** azColName)4171f5207b7SJohn Levon static int save_mtag(void *_tag, int argc, char **argv, char **azColName)
4181f5207b7SJohn Levon {
4191f5207b7SJohn Levon 	mtag_t *saved_tag = _tag;
4201f5207b7SJohn Levon 	mtag_t new_tag;
4211f5207b7SJohn Levon 
4221f5207b7SJohn Levon 	new_tag = strtoll(argv[0], NULL, 10);
4231f5207b7SJohn Levon 
4241f5207b7SJohn Levon 	if (!*saved_tag)
4251f5207b7SJohn Levon 		*saved_tag = new_tag;
4261f5207b7SJohn Levon 	else if (*saved_tag != new_tag)
4271f5207b7SJohn Levon 		*saved_tag = -1ULL;
4281f5207b7SJohn Levon 
4291f5207b7SJohn Levon 	return 0;
4301f5207b7SJohn Levon }
4311f5207b7SJohn Levon 
mtag_map_select_container(mtag_t tag,int container_offset,mtag_t * container)4326523a3aaSJohn Levon int mtag_map_select_container(mtag_t tag, int container_offset, mtag_t *container)
4331f5207b7SJohn Levon {
4341f5207b7SJohn Levon 	mtag_t tmp = 0;
4351f5207b7SJohn Levon 
4361f5207b7SJohn Levon 	run_sql(save_mtag, &tmp,
4376523a3aaSJohn Levon 		"select container from mtag_map where tag = %lld and container_offset = %d and tag_offset = 0;",
4386523a3aaSJohn Levon 		tag, container_offset);
4391f5207b7SJohn Levon 
4401f5207b7SJohn Levon 	if (tmp == 0 || tmp == -1ULL)
4411f5207b7SJohn Levon 		return 0;
4421f5207b7SJohn Levon 	*container = tmp;
4431f5207b7SJohn Levon 	return 1;
4441f5207b7SJohn Levon }
4451f5207b7SJohn Levon 
mtag_map_select_tag(mtag_t container,int offset,mtag_t * tag)4461f5207b7SJohn Levon int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag)
4471f5207b7SJohn Levon {
4481f5207b7SJohn Levon 	mtag_t tmp = 0;
4491f5207b7SJohn Levon 
4501f5207b7SJohn Levon 	run_sql(save_mtag, &tmp,
4516523a3aaSJohn Levon 		"select tag from mtag_map where container = %lld and container_offset = %d;",
4521f5207b7SJohn Levon 		container, offset);
4531f5207b7SJohn Levon 
4541f5207b7SJohn Levon 	if (tmp == 0 || tmp == -1ULL)
4551f5207b7SJohn Levon 		return 0;
4561f5207b7SJohn Levon 	*tag = tmp;
4571f5207b7SJohn Levon 	return 1;
4581f5207b7SJohn Levon }
4591f5207b7SJohn Levon 
get_static_filter(struct symbol * sym)4601f5207b7SJohn Levon char *get_static_filter(struct symbol *sym)
4611f5207b7SJohn Levon {
4621f5207b7SJohn Levon 	static char sql_filter[1024];
4631f5207b7SJohn Levon 
4641f5207b7SJohn Levon 	/* This can only happen on buggy code.  Return invalid SQL. */
4651f5207b7SJohn Levon 	if (!sym) {
4661f5207b7SJohn Levon 		sql_filter[0] = '\0';
4671f5207b7SJohn Levon 		return sql_filter;
4681f5207b7SJohn Levon 	}
4691f5207b7SJohn Levon 
4701f5207b7SJohn Levon 	if (sym->ctype.modifiers & MOD_STATIC) {
4711f5207b7SJohn Levon 		snprintf(sql_filter, sizeof(sql_filter),
4721f5207b7SJohn Levon 			 "file = '%s' and function = '%s' and static = '1'",
4731f5207b7SJohn Levon 			 get_base_file(), sym->ident->name);
4741f5207b7SJohn Levon 	} else {
4751f5207b7SJohn Levon 		snprintf(sql_filter, sizeof(sql_filter),
4761f5207b7SJohn Levon 			 "function = '%s' and static = '0'", sym->ident->name);
4771f5207b7SJohn Levon 	}
4781f5207b7SJohn Levon 
4791f5207b7SJohn Levon 	return sql_filter;
4801f5207b7SJohn Levon }
4811f5207b7SJohn Levon 
get_row_count(void * _row_count,int argc,char ** argv,char ** azColName)4821f5207b7SJohn Levon static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
4831f5207b7SJohn Levon {
4841f5207b7SJohn Levon 	int *row_count = _row_count;
4851f5207b7SJohn Levon 
4861f5207b7SJohn Levon 	*row_count = 0;
4871f5207b7SJohn Levon 	if (argc != 1)
4881f5207b7SJohn Levon 		return 0;
4891f5207b7SJohn Levon 	*row_count = atoi(argv[0]);
4901f5207b7SJohn Levon 	return 0;
4911f5207b7SJohn Levon }
4921f5207b7SJohn Levon 
mark_call_params_untracked(struct expression * call)4931f5207b7SJohn Levon static void mark_call_params_untracked(struct expression *call)
4941f5207b7SJohn Levon {
4951f5207b7SJohn Levon 	struct expression *arg;
4961f5207b7SJohn Levon 	int i = 0;
4971f5207b7SJohn Levon 
4981f5207b7SJohn Levon 	FOR_EACH_PTR(call->args, arg) {
4991f5207b7SJohn Levon 		mark_untracked(call, i++, "$", NULL);
5001f5207b7SJohn Levon 	} END_FOR_EACH_PTR(arg);
5011f5207b7SJohn Levon }
5021f5207b7SJohn Levon 
sql_select_return_states_pointer(const char * cols,struct expression * call,int (* callback)(void *,int,char **,char **),void * info)5031f5207b7SJohn Levon static void sql_select_return_states_pointer(const char *cols,
5041f5207b7SJohn Levon 	struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
5051f5207b7SJohn Levon {
5061f5207b7SJohn Levon 	char *ptr;
5071f5207b7SJohn Levon 	int return_count = 0;
5081f5207b7SJohn Levon 
5091f5207b7SJohn Levon 	ptr = get_fnptr_name(call->fn);
5101f5207b7SJohn Levon 	if (!ptr)
5111f5207b7SJohn Levon 		return;
5121f5207b7SJohn Levon 
5131f5207b7SJohn Levon 	run_sql(get_row_count, &return_count,
5141f5207b7SJohn Levon 		"select count(*) from return_states join function_ptr "
5151f5207b7SJohn Levon 		"where return_states.function == function_ptr.function and "
5161f5207b7SJohn Levon 		"ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
5171f5207b7SJohn Levon 	/* The magic number 100 is just from testing on the kernel. */
5181f5207b7SJohn Levon 	if (return_count > 100) {
5191f5207b7SJohn Levon 		mark_call_params_untracked(call);
5201f5207b7SJohn Levon 		return;
5211f5207b7SJohn Levon 	}
5221f5207b7SJohn Levon 
5231f5207b7SJohn Levon 	run_sql(callback, info,
5241f5207b7SJohn Levon 		"select %s from return_states join function_ptr where "
5251f5207b7SJohn Levon 		"return_states.function == function_ptr.function and ptr = '%s' "
5261f5207b7SJohn Levon 		"and searchable = 1 "
5271f5207b7SJohn Levon 		"order by function_ptr.file, return_states.file, return_id, type;",
5281f5207b7SJohn Levon 		cols, ptr);
5291f5207b7SJohn Levon }
5301f5207b7SJohn Levon 
is_local_symbol(struct expression * expr)5311f5207b7SJohn Levon static int is_local_symbol(struct expression *expr)
5321f5207b7SJohn Levon {
5331f5207b7SJohn Levon 	if (expr->type != EXPR_SYMBOL)
5341f5207b7SJohn Levon 		return 0;
5351f5207b7SJohn Levon 	if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
5361f5207b7SJohn Levon 		return 0;
5371f5207b7SJohn Levon 	return 1;
5381f5207b7SJohn Levon }
5391f5207b7SJohn Levon 
sql_select_return_states(const char * cols,struct expression * call,int (* callback)(void *,int,char **,char **),void * info)5401f5207b7SJohn Levon void sql_select_return_states(const char *cols, struct expression *call,
5411f5207b7SJohn Levon 	int (*callback)(void*, int, char**, char**), void *info)
5421f5207b7SJohn Levon {
543c85f09ccSJohn Levon 	struct expression *fn;
5441f5207b7SJohn Levon 	int row_count = 0;
5451f5207b7SJohn Levon 
5461f5207b7SJohn Levon 	if (is_fake_call(call))
5471f5207b7SJohn Levon 		return;
5481f5207b7SJohn Levon 
549c85f09ccSJohn Levon 	fn = strip_expr(call->fn);
550c85f09ccSJohn Levon 	if (fn->type != EXPR_SYMBOL || !fn->symbol || is_local_symbol(fn)) {
5511f5207b7SJohn Levon 		sql_select_return_states_pointer(cols, call, callback, info);
5521f5207b7SJohn Levon 		return;
5531f5207b7SJohn Levon 	}
5541f5207b7SJohn Levon 
555c85f09ccSJohn Levon 	if (inlinable(fn)) {
5561f5207b7SJohn Levon 		mem_sql(callback, info,
5571f5207b7SJohn Levon 			"select %s from return_states where call_id = '%lu' order by return_id, type;",
5581f5207b7SJohn Levon 			cols, (unsigned long)call);
5591f5207b7SJohn Levon 		return;
5601f5207b7SJohn Levon 	}
5611f5207b7SJohn Levon 
5621f5207b7SJohn Levon 	run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
563c85f09ccSJohn Levon 		get_static_filter(fn->symbol));
5641f5207b7SJohn Levon 	if (row_count > 3000)
5651f5207b7SJohn Levon 		return;
5661f5207b7SJohn Levon 
5671f5207b7SJohn Levon 	run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
568c85f09ccSJohn Levon 		cols, get_static_filter(fn->symbol));
5691f5207b7SJohn Levon }
5701f5207b7SJohn Levon 
5711f5207b7SJohn Levon #define CALL_IMPLIES 0
5721f5207b7SJohn Levon #define RETURN_IMPLIES 1
5731f5207b7SJohn Levon 
5741f5207b7SJohn Levon struct implies_info {
5751f5207b7SJohn Levon 	int type;
5761f5207b7SJohn Levon 	struct db_implies_cb_list *cb_list;
5771f5207b7SJohn Levon 	struct expression *expr;
5781f5207b7SJohn Levon 	struct symbol *sym;
5791f5207b7SJohn Levon };
5801f5207b7SJohn Levon 
sql_select_implies(const char * cols,struct implies_info * info,int (* callback)(void *,int,char **,char **))5811f5207b7SJohn Levon void sql_select_implies(const char *cols, struct implies_info *info,
5821f5207b7SJohn Levon 	int (*callback)(void*, int, char**, char**))
5831f5207b7SJohn Levon {
5841f5207b7SJohn Levon 	if (info->type == RETURN_IMPLIES && inlinable(info->expr->fn)) {
5851f5207b7SJohn Levon 		mem_sql(callback, info,
5861f5207b7SJohn Levon 			"select %s from return_implies where call_id = '%lu';",
5871f5207b7SJohn Levon 			cols, (unsigned long)info->expr);
5881f5207b7SJohn Levon 		return;
5891f5207b7SJohn Levon 	}
5901f5207b7SJohn Levon 
5911f5207b7SJohn Levon 	run_sql(callback, info, "select %s from %s_implies where %s;",
5921f5207b7SJohn Levon 		cols,
5931f5207b7SJohn Levon 		info->type == CALL_IMPLIES ? "call" : "return",
5941f5207b7SJohn Levon 		get_static_filter(info->sym));
5951f5207b7SJohn Levon }
5961f5207b7SJohn Levon 
5971f5207b7SJohn Levon struct select_caller_info_data {
5981f5207b7SJohn Levon 	struct stree *final_states;
5991f5207b7SJohn Levon 	struct timeval start_time;
6001f5207b7SJohn Levon 	int prev_func_id;
6011f5207b7SJohn Levon 	int ignore;
6021f5207b7SJohn Levon 	int results;
6031f5207b7SJohn Levon };
6041f5207b7SJohn Levon 
6051f5207b7SJohn Levon static int caller_info_callback(void *_data, int argc, char **argv, char **azColName);
6061f5207b7SJohn Levon 
sql_select_caller_info(struct select_caller_info_data * data,const char * cols,struct symbol * sym)6071f5207b7SJohn Levon static void sql_select_caller_info(struct select_caller_info_data *data,
6081f5207b7SJohn Levon 	const char *cols, struct symbol *sym)
6091f5207b7SJohn Levon {
6101f5207b7SJohn Levon 	if (__inline_fn) {
6111f5207b7SJohn Levon 		mem_sql(caller_info_callback, data,
6121f5207b7SJohn Levon 			"select %s from caller_info where call_id = %lu;",
6131f5207b7SJohn Levon 			cols, (unsigned long)__inline_fn);
6141f5207b7SJohn Levon 		return;
6151f5207b7SJohn Levon 	}
6161f5207b7SJohn Levon 
6171f5207b7SJohn Levon 	if (sym->ident->name && is_common_function(sym->ident->name))
6181f5207b7SJohn Levon 		return;
6191f5207b7SJohn Levon 	run_sql(caller_info_callback, data,
6201f5207b7SJohn Levon 		"select %s from common_caller_info where %s order by call_id;",
6211f5207b7SJohn Levon 		cols, get_static_filter(sym));
6221f5207b7SJohn Levon 	if (data->results)
6231f5207b7SJohn Levon 		return;
6241f5207b7SJohn Levon 
6251f5207b7SJohn Levon 	run_sql(caller_info_callback, data,
6261f5207b7SJohn Levon 		"select %s from caller_info where %s order by call_id;",
6271f5207b7SJohn Levon 		cols, get_static_filter(sym));
6281f5207b7SJohn Levon }
6291f5207b7SJohn Levon 
select_caller_info_hook(void (* callback)(const char * name,struct symbol * sym,char * key,char * value),int type)6301f5207b7SJohn Levon void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
6311f5207b7SJohn Levon {
6321f5207b7SJohn Levon 	struct def_callback *def_callback = __alloc_def_callback(0);
6331f5207b7SJohn Levon 
6341f5207b7SJohn Levon 	def_callback->hook_type = type;
6351f5207b7SJohn Levon 	def_callback->callback = callback;
6361f5207b7SJohn Levon 	add_ptr_list(&select_caller_info_callbacks, def_callback);
6371f5207b7SJohn Levon }
6381f5207b7SJohn Levon 
6391f5207b7SJohn Levon /*
6401f5207b7SJohn Levon  * These call backs are used when the --info option is turned on to print struct
6411f5207b7SJohn Levon  * member information.  For example foo->bar could have a state in
6421f5207b7SJohn Levon  * smatch_extra.c and also check_user.c.
6431f5207b7SJohn Levon  */
add_member_info_callback(int owner,void (* callback)(struct expression * call,int param,char * printed_name,struct sm_state * sm))6441f5207b7SJohn Levon void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
6451f5207b7SJohn Levon {
6461f5207b7SJohn Levon 	struct member_info_callback *member_callback = __alloc_member_info_callback(0);
6471f5207b7SJohn Levon 
6481f5207b7SJohn Levon 	member_callback->owner = owner;
6491f5207b7SJohn Levon 	member_callback->callback = callback;
6501f5207b7SJohn Levon 	add_ptr_list(&member_callbacks, member_callback);
6511f5207b7SJohn Levon }
6521f5207b7SJohn Levon 
add_caller_info_callback(int owner,void (* callback)(struct expression * call,int param,char * printed_name,struct sm_state * sm))6536523a3aaSJohn Levon void add_caller_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
6546523a3aaSJohn Levon {
6556523a3aaSJohn Levon 	struct member_info_callback *member_callback = __alloc_member_info_callback(0);
6566523a3aaSJohn Levon 
6576523a3aaSJohn Levon 	member_callback->owner = owner;
6586523a3aaSJohn Levon 	member_callback->callback = callback;
6596523a3aaSJohn Levon 	add_ptr_list(&member_callbacks_new, member_callback);
6606523a3aaSJohn Levon }
6616523a3aaSJohn Levon 
add_split_return_callback(void (* fn)(int return_id,char * return_ranges,struct expression * returned_expr))6621f5207b7SJohn Levon void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
6631f5207b7SJohn Levon {
6641f5207b7SJohn Levon 	struct returned_state_callback *callback = __alloc_returned_state_callback(0);
6651f5207b7SJohn Levon 
6661f5207b7SJohn Levon 	callback->callback = fn;
6671f5207b7SJohn Levon 	add_ptr_list(&returned_state_callbacks, callback);
6681f5207b7SJohn Levon }
6691f5207b7SJohn Levon 
add_returned_member_callback(int owner,void (* callback)(int return_id,char * return_ranges,struct expression * expr,char * printed_name,struct smatch_state * state))6701f5207b7SJohn Levon 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))
6711f5207b7SJohn Levon {
6721f5207b7SJohn Levon 	struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
6731f5207b7SJohn Levon 
6741f5207b7SJohn Levon 	member_callback->owner = owner;
6751f5207b7SJohn Levon 	member_callback->callback = callback;
6761f5207b7SJohn Levon 	add_ptr_list(&returned_member_callbacks, member_callback);
6771f5207b7SJohn Levon }
6781f5207b7SJohn Levon 
select_call_implies_hook(int type,void (* callback)(struct expression * call,struct expression * arg,char * key,char * value))6791f5207b7SJohn Levon void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
6801f5207b7SJohn Levon {
6811f5207b7SJohn Levon 	struct db_implies_callback *cb = __alloc_db_implies_callback(0);
6821f5207b7SJohn Levon 
6831f5207b7SJohn Levon 	cb->type = type;
6841f5207b7SJohn Levon 	cb->callback = callback;
6851f5207b7SJohn Levon 	add_ptr_list(&call_implies_cb_list, cb);
6861f5207b7SJohn Levon }
6871f5207b7SJohn Levon 
select_return_implies_hook(int type,void (* callback)(struct expression * call,struct expression * arg,char * key,char * value))6881f5207b7SJohn Levon void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
6891f5207b7SJohn Levon {
6901f5207b7SJohn Levon 	struct db_implies_callback *cb = __alloc_db_implies_callback(0);
6911f5207b7SJohn Levon 
6921f5207b7SJohn Levon 	cb->type = type;
6931f5207b7SJohn Levon 	cb->callback = callback;
6941f5207b7SJohn Levon 	add_ptr_list(&return_implies_cb_list, cb);
6951f5207b7SJohn Levon }
6961f5207b7SJohn Levon 
6971f5207b7SJohn Levon struct return_info {
6981f5207b7SJohn Levon 	struct expression *static_returns_call;
6991f5207b7SJohn Levon 	struct symbol *return_type;
7001f5207b7SJohn Levon 	struct range_list *return_range_list;
7011f5207b7SJohn Levon };
7021f5207b7SJohn Levon 
db_return_callback(void * _ret_info,int argc,char ** argv,char ** azColName)7031f5207b7SJohn Levon static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
7041f5207b7SJohn Levon {
7051f5207b7SJohn Levon 	struct return_info *ret_info = _ret_info;
7061f5207b7SJohn Levon 	struct range_list *rl;
7071f5207b7SJohn Levon 	struct expression *call_expr = ret_info->static_returns_call;
7081f5207b7SJohn Levon 
7091f5207b7SJohn Levon 	if (argc != 1)
7101f5207b7SJohn Levon 		return 0;
7111f5207b7SJohn Levon 	call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
7121f5207b7SJohn Levon 	ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
7131f5207b7SJohn Levon 	return 0;
7141f5207b7SJohn Levon }
7151f5207b7SJohn Levon 
db_return_vals(struct expression * expr)7161f5207b7SJohn Levon struct range_list *db_return_vals(struct expression *expr)
7171f5207b7SJohn Levon {
7181f5207b7SJohn Levon 	struct return_info ret_info = {};
7191f5207b7SJohn Levon 	char buf[64];
7201f5207b7SJohn Levon 	struct sm_state *sm;
7211f5207b7SJohn Levon 
7221f5207b7SJohn Levon 	if (is_fake_call(expr))
7231f5207b7SJohn Levon 		return NULL;
7241f5207b7SJohn Levon 
7251f5207b7SJohn Levon 	snprintf(buf, sizeof(buf), "return %p", expr);
7261f5207b7SJohn Levon 	sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
7271f5207b7SJohn Levon 	if (sm)
7281f5207b7SJohn Levon 		return clone_rl(estate_rl(sm->state));
7291f5207b7SJohn Levon 	ret_info.static_returns_call = expr;
7301f5207b7SJohn Levon 	ret_info.return_type = get_type(expr);
7311f5207b7SJohn Levon 	if (!ret_info.return_type)
7321f5207b7SJohn Levon 		return NULL;
7331f5207b7SJohn Levon 
7341f5207b7SJohn Levon 	if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
7351f5207b7SJohn Levon 		return NULL;
7361f5207b7SJohn Levon 
7371f5207b7SJohn Levon 	ret_info.return_range_list = NULL;
7381f5207b7SJohn Levon 	if (inlinable(expr->fn)) {
7391f5207b7SJohn Levon 		mem_sql(db_return_callback, &ret_info,
7401f5207b7SJohn Levon 			"select distinct return from return_states where call_id = '%lu';",
7411f5207b7SJohn Levon 			(unsigned long)expr);
7421f5207b7SJohn Levon 	} else {
7431f5207b7SJohn Levon 		run_sql(db_return_callback, &ret_info,
7441f5207b7SJohn Levon 			"select distinct return from return_states where %s;",
7451f5207b7SJohn Levon 			get_static_filter(expr->fn->symbol));
7461f5207b7SJohn Levon 	}
7471f5207b7SJohn Levon 	return ret_info.return_range_list;
7481f5207b7SJohn Levon }
7491f5207b7SJohn Levon 
db_return_vals_from_str(const char * fn_name)7501f5207b7SJohn Levon struct range_list *db_return_vals_from_str(const char *fn_name)
7511f5207b7SJohn Levon {
7521f5207b7SJohn Levon 	struct return_info ret_info;
7531f5207b7SJohn Levon 
7541f5207b7SJohn Levon 	ret_info.static_returns_call = NULL;
7551f5207b7SJohn Levon 	ret_info.return_type = &llong_ctype;
7561f5207b7SJohn Levon 	ret_info.return_range_list = NULL;
7571f5207b7SJohn Levon 
7581f5207b7SJohn Levon 	run_sql(db_return_callback, &ret_info,
7591f5207b7SJohn Levon 		"select distinct return from return_states where function = '%s';",
7601f5207b7SJohn Levon 		fn_name);
7611f5207b7SJohn Levon 	return ret_info.return_range_list;
7621f5207b7SJohn Levon }
7631f5207b7SJohn Levon 
764c85f09ccSJohn Levon /*
765c85f09ccSJohn Levon  * This is used when we have a function that takes a function pointer as a
766c85f09ccSJohn Levon  * parameter.  "frob(blah, blah, my_function);"  We know that the return values
767c85f09ccSJohn Levon  * from frob() come from my_funcion() so we want to find the possible returns
768c85f09ccSJohn Levon  * of my_function(), but we don't know which arguments are passed to it.
769c85f09ccSJohn Levon  *
770c85f09ccSJohn Levon  */
db_return_vals_no_args(struct expression * expr)771c85f09ccSJohn Levon struct range_list *db_return_vals_no_args(struct expression *expr)
772c85f09ccSJohn Levon {
773c85f09ccSJohn Levon 	struct return_info ret_info = {};
774c85f09ccSJohn Levon 
775c85f09ccSJohn Levon 	if (!expr || expr->type != EXPR_SYMBOL)
776c85f09ccSJohn Levon 		return NULL;
777c85f09ccSJohn Levon 
778c85f09ccSJohn Levon 	ret_info.static_returns_call = expr;
779c85f09ccSJohn Levon 	ret_info.return_type = get_type(expr);
780c85f09ccSJohn Levon 	ret_info.return_type = get_real_base_type(ret_info.return_type);
781c85f09ccSJohn Levon 	if (!ret_info.return_type)
782c85f09ccSJohn Levon 		return NULL;
783c85f09ccSJohn Levon 
784c85f09ccSJohn Levon 	run_sql(db_return_callback, &ret_info,
785c85f09ccSJohn Levon 		"select distinct return from return_states where %s;",
786c85f09ccSJohn Levon 		get_static_filter(expr->symbol));
787c85f09ccSJohn Levon 
788c85f09ccSJohn Levon 	return ret_info.return_range_list;
789c85f09ccSJohn Levon }
790c85f09ccSJohn Levon 
match_call_marker(struct expression * expr)7911f5207b7SJohn Levon static void match_call_marker(struct expression *expr)
7921f5207b7SJohn Levon {
7931f5207b7SJohn Levon 	struct symbol *type;
7941f5207b7SJohn Levon 
7951f5207b7SJohn Levon 	type = get_type(expr->fn);
7961f5207b7SJohn Levon 	if (type && type->type == SYM_PTR)
7971f5207b7SJohn Levon 		type = get_real_base_type(type);
7981f5207b7SJohn Levon 
7991f5207b7SJohn Levon 	/*
8001f5207b7SJohn Levon 	 * we just want to record something in the database so that if we have
8011f5207b7SJohn Levon 	 * two calls like:  frob(4); frob(some_unkown); then on the receiving
8021f5207b7SJohn Levon 	 * side we know that sometimes frob is called with unknown parameters.
8031f5207b7SJohn Levon 	 */
8041f5207b7SJohn Levon 
8051f5207b7SJohn Levon 	sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", type_to_str(type));
8061f5207b7SJohn Levon }
8071f5207b7SJohn Levon 
is_recursive_member(const char * name)808efe51d0cSJohn Levon int is_recursive_member(const char *name)
809efe51d0cSJohn Levon {
810efe51d0cSJohn Levon 	char buf[256];
811efe51d0cSJohn Levon 	const char *p, *next;
812efe51d0cSJohn Levon 	int size;
813efe51d0cSJohn Levon 
814efe51d0cSJohn Levon 	p = strchr(name, '>');
815efe51d0cSJohn Levon 	if (!p)
816efe51d0cSJohn Levon 		return 0;
817efe51d0cSJohn Levon 	p++;
818efe51d0cSJohn Levon 	while (true) {
819efe51d0cSJohn Levon 		next = strchr(p, '>');
820efe51d0cSJohn Levon 		if (!next)
821efe51d0cSJohn Levon 			return 0;
822efe51d0cSJohn Levon 		next++;
823efe51d0cSJohn Levon 
824efe51d0cSJohn Levon 		size = next - p;
825efe51d0cSJohn Levon 		if (size >= sizeof(buf))
826efe51d0cSJohn Levon 			return 0;
827efe51d0cSJohn Levon 		memcpy(buf, p, size);
828efe51d0cSJohn Levon 		buf[size] = '\0';
829efe51d0cSJohn Levon 		if (strstr(next, buf))
830efe51d0cSJohn Levon 			return 1;
831efe51d0cSJohn Levon 		p = next;
832efe51d0cSJohn Levon 	}
833efe51d0cSJohn Levon }
834efe51d0cSJohn Levon 
sm_to_arg_name(struct expression * expr,struct sm_state * sm)8355a0e240fSJohn Levon char *sm_to_arg_name(struct expression *expr, struct sm_state *sm)
8365a0e240fSJohn Levon {
8375a0e240fSJohn Levon 	struct symbol *sym;
8385a0e240fSJohn Levon 	const char *sm_name;
8395a0e240fSJohn Levon 	char *name;
8405a0e240fSJohn Levon 	bool is_address = false;
8415a0e240fSJohn Levon 	bool add_star = false;
8425a0e240fSJohn Levon 	char buf[256];
8435a0e240fSJohn Levon 	char *ret = NULL;
8445a0e240fSJohn Levon 	int len;
8455a0e240fSJohn Levon 
8465a0e240fSJohn Levon 	expr = strip_expr(expr);
8475a0e240fSJohn Levon 	if (!expr)
8485a0e240fSJohn Levon 		return NULL;
8495a0e240fSJohn Levon 
8505a0e240fSJohn Levon 	if (expr->type == EXPR_PREOP && expr->op == '&') {
8515a0e240fSJohn Levon 		expr = strip_expr(expr->unop);
8525a0e240fSJohn Levon 		is_address = true;
8535a0e240fSJohn Levon 	}
8545a0e240fSJohn Levon 
8555a0e240fSJohn Levon 	name = expr_to_var_sym(expr, &sym);
8565a0e240fSJohn Levon 	if (!name || !sym)
8575a0e240fSJohn Levon 		goto free;
8585a0e240fSJohn Levon 	if (sym != sm->sym)
8595a0e240fSJohn Levon 		goto free;
8605a0e240fSJohn Levon 
8615a0e240fSJohn Levon 	sm_name = sm->name;
8625a0e240fSJohn Levon 	add_star = false;
8635a0e240fSJohn Levon 	if (sm_name[0] == '*') {
8645a0e240fSJohn Levon 		add_star = true;
8655a0e240fSJohn Levon 		sm_name++;
8665a0e240fSJohn Levon 	}
8675a0e240fSJohn Levon 
8685a0e240fSJohn Levon 	len = strlen(name);
8695a0e240fSJohn Levon 	if (strncmp(name, sm_name, len) != 0)
8705a0e240fSJohn Levon 		goto free;
8715a0e240fSJohn Levon 	if (sm_name[len] == '\0') {
8725a0e240fSJohn Levon 		snprintf(buf, sizeof(buf), "%s%s$",
8735a0e240fSJohn Levon 			 add_star ? "*" : "", is_address ? "*" : "");
8745a0e240fSJohn Levon 	} else {
8755a0e240fSJohn Levon 		if (sm_name[len] != '.' && sm_name[len] != '-')
8765a0e240fSJohn Levon 			goto free;
8775a0e240fSJohn Levon 		if (sm_name[len] == '-')
8785a0e240fSJohn Levon 			len++;
8795a0e240fSJohn Levon 		// FIXME does is_address really imply that sm_name[len] == '-'
8805a0e240fSJohn Levon 		snprintf(buf, sizeof(buf), "%s$->%s", add_star ? "*" : "",
8815a0e240fSJohn Levon 			 sm_name + len);
8825a0e240fSJohn Levon 	}
8835a0e240fSJohn Levon 
8845a0e240fSJohn Levon 	ret = alloc_sname(buf);
8855a0e240fSJohn Levon free:
8865a0e240fSJohn Levon 	free_string(name);
8875a0e240fSJohn Levon 	return ret;
8885a0e240fSJohn Levon }
8895a0e240fSJohn Levon 
print_struct_members(struct expression * call,struct expression * expr,int param,struct stree * stree,void (* callback)(struct expression * call,int param,char * printed_name,struct sm_state * sm),bool new)89031ad075eSJohn Levon static void print_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
8916523a3aaSJohn Levon 	void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm),
8926523a3aaSJohn Levon 	bool new)
8931f5207b7SJohn Levon {
8941f5207b7SJohn Levon 	struct sm_state *sm;
895efe51d0cSJohn Levon 	const char *sm_name;
8961f5207b7SJohn Levon 	char *name;
8971f5207b7SJohn Levon 	struct symbol *sym;
8981f5207b7SJohn Levon 	int len;
8991f5207b7SJohn Levon 	char printed_name[256];
9001f5207b7SJohn Levon 	int is_address = 0;
901efe51d0cSJohn Levon 	bool add_star;
9021f5207b7SJohn Levon 	struct symbol *type;
9031f5207b7SJohn Levon 
9041f5207b7SJohn Levon 	expr = strip_expr(expr);
9051f5207b7SJohn Levon 	if (!expr)
9061f5207b7SJohn Levon 		return;
907efe51d0cSJohn Levon 	type = get_type(expr);
908efe51d0cSJohn Levon 	if (type && type_bits(type) < type_bits(&ulong_ctype))
909efe51d0cSJohn Levon 		return;
910efe51d0cSJohn Levon 
9111f5207b7SJohn Levon 	if (expr->type == EXPR_PREOP && expr->op == '&') {
9121f5207b7SJohn Levon 		expr = strip_expr(expr->unop);
9131f5207b7SJohn Levon 		is_address = 1;
9141f5207b7SJohn Levon 	}
9151f5207b7SJohn Levon 
9161f5207b7SJohn Levon 	name = expr_to_var_sym(expr, &sym);
9171f5207b7SJohn Levon 	if (!name || !sym)
9181f5207b7SJohn Levon 		goto free;
9191f5207b7SJohn Levon 
9201f5207b7SJohn Levon 	len = strlen(name);
9211f5207b7SJohn Levon 	FOR_EACH_SM(stree, sm) {
9221f5207b7SJohn Levon 		if (sm->sym != sym)
9231f5207b7SJohn Levon 			continue;
924efe51d0cSJohn Levon 		sm_name = sm->name;
925efe51d0cSJohn Levon 		add_star = false;
926efe51d0cSJohn Levon 		if (sm_name[0] == '*') {
927efe51d0cSJohn Levon 			add_star = true;
928efe51d0cSJohn Levon 			sm_name++;
929efe51d0cSJohn Levon 		}
930efe51d0cSJohn Levon 		// FIXME: simplify?
931efe51d0cSJohn Levon 		if (!add_star && strcmp(name, sm_name) == 0) {
9326523a3aaSJohn Levon 			if (is_address) {
93331ad075eSJohn Levon 				snprintf(printed_name, sizeof(printed_name), "*$");
9346523a3aaSJohn Levon 			} else {
9356523a3aaSJohn Levon 				if (new)
9366523a3aaSJohn Levon 					snprintf(printed_name, sizeof(printed_name), "$");
9376523a3aaSJohn Levon 				else
9386523a3aaSJohn Levon 					continue;
9396523a3aaSJohn Levon 			}
940efe51d0cSJohn Levon 		} else if (add_star && strcmp(name, sm_name) == 0) {
94131ad075eSJohn Levon 			snprintf(printed_name, sizeof(printed_name), "%s*$",
94231ad075eSJohn Levon 				 is_address ? "*" : "");
943efe51d0cSJohn Levon 		} else if (strncmp(name, sm_name, len) == 0) {
944efe51d0cSJohn Levon 			if (sm_name[len] != '.' && sm_name[len] != '-')
9451f5207b7SJohn Levon 				continue;
9466523a3aaSJohn Levon 			if (is_address && sm_name[len] == '.') {
947efe51d0cSJohn Levon 				snprintf(printed_name, sizeof(printed_name),
94831ad075eSJohn Levon 					 "%s$->%s", add_star ? "*" : "",
94931ad075eSJohn Levon 					 sm_name + len + 1);
9506523a3aaSJohn Levon 			} else if (is_address && sm_name[len] == '-') {
9516523a3aaSJohn Levon 				snprintf(printed_name, sizeof(printed_name),
9526523a3aaSJohn Levon 					 "%s(*$)%s", add_star ? "*" : "",
9536523a3aaSJohn Levon 					 sm_name + len);
9546523a3aaSJohn Levon 			} else {
955efe51d0cSJohn Levon 				snprintf(printed_name, sizeof(printed_name),
95631ad075eSJohn Levon 					 "%s$%s", add_star ? "*" : "",
95731ad075eSJohn Levon 					 sm_name + len);
9586523a3aaSJohn Levon 			}
9591f5207b7SJohn Levon 		} else {
9601f5207b7SJohn Levon 			continue;
9611f5207b7SJohn Levon 		}
962efe51d0cSJohn Levon 		if (is_recursive_member(printed_name))
963efe51d0cSJohn Levon 			continue;
9641f5207b7SJohn Levon 		callback(call, param, printed_name, sm);
9651f5207b7SJohn Levon 	} END_FOR_EACH_SM(sm);
9661f5207b7SJohn Levon free:
9671f5207b7SJohn Levon 	free_string(name);
9681f5207b7SJohn Levon }
9691f5207b7SJohn Levon 
match_call_info(struct expression * call)9701f5207b7SJohn Levon static void match_call_info(struct expression *call)
9711f5207b7SJohn Levon {
9721f5207b7SJohn Levon 	struct member_info_callback *cb;
9731f5207b7SJohn Levon 	struct expression *arg;
9741f5207b7SJohn Levon 	struct stree *stree;
9751f5207b7SJohn Levon 	char *name;
9761f5207b7SJohn Levon 	int i;
9771f5207b7SJohn Levon 
9781f5207b7SJohn Levon 	name = get_fnptr_name(call->fn);
9791f5207b7SJohn Levon 	if (!name)
9801f5207b7SJohn Levon 		return;
9811f5207b7SJohn Levon 
9821f5207b7SJohn Levon 	FOR_EACH_PTR(member_callbacks, cb) {
9831f5207b7SJohn Levon 		stree = get_all_states_stree(cb->owner);
9841f5207b7SJohn Levon 		i = 0;
9851f5207b7SJohn Levon 		FOR_EACH_PTR(call->args, arg) {
9866523a3aaSJohn Levon 			print_struct_members(call, arg, i, stree, cb->callback, 0);
9876523a3aaSJohn Levon 			i++;
9886523a3aaSJohn Levon 		} END_FOR_EACH_PTR(arg);
9896523a3aaSJohn Levon 		free_stree(&stree);
9906523a3aaSJohn Levon 	} END_FOR_EACH_PTR(cb);
9916523a3aaSJohn Levon 
9926523a3aaSJohn Levon 	free_string(name);
9936523a3aaSJohn Levon }
9946523a3aaSJohn Levon 
match_call_info_new(struct expression * call)9956523a3aaSJohn Levon static void match_call_info_new(struct expression *call)
9966523a3aaSJohn Levon {
9976523a3aaSJohn Levon 	struct member_info_callback *cb;
9986523a3aaSJohn Levon 	struct expression *arg;
9996523a3aaSJohn Levon 	struct stree *stree;
10006523a3aaSJohn Levon 	char *name;
10016523a3aaSJohn Levon 	int i;
10026523a3aaSJohn Levon 
10036523a3aaSJohn Levon 	name = get_fnptr_name(call->fn);
10046523a3aaSJohn Levon 	if (!name)
10056523a3aaSJohn Levon 		return;
10066523a3aaSJohn Levon 
10076523a3aaSJohn Levon 	FOR_EACH_PTR(member_callbacks_new, cb) {
10086523a3aaSJohn Levon 		stree = get_all_states_stree(cb->owner);
10096523a3aaSJohn Levon 		i = 0;
10106523a3aaSJohn Levon 		FOR_EACH_PTR(call->args, arg) {
10116523a3aaSJohn Levon 			print_struct_members(call, arg, i, stree, cb->callback, 1);
10121f5207b7SJohn Levon 			i++;
10131f5207b7SJohn Levon 		} END_FOR_EACH_PTR(arg);
10141f5207b7SJohn Levon 		free_stree(&stree);
10151f5207b7SJohn Levon 	} END_FOR_EACH_PTR(cb);
10161f5207b7SJohn Levon 
10171f5207b7SJohn Levon 	free_string(name);
10181f5207b7SJohn Levon }
10191f5207b7SJohn Levon 
get_param(int param,char ** name,struct symbol ** sym)10201f5207b7SJohn Levon static int get_param(int param, char **name, struct symbol **sym)
10211f5207b7SJohn Levon {
10221f5207b7SJohn Levon 	struct symbol *arg;
10231f5207b7SJohn Levon 	int i;
10241f5207b7SJohn Levon 
10251f5207b7SJohn Levon 	i = 0;
10261f5207b7SJohn Levon 	FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
10271f5207b7SJohn Levon 		/*
10281f5207b7SJohn Levon 		 * this is a temporary hack to work around a bug (I think in sparse?)
10291f5207b7SJohn Levon 		 * 2.6.37-rc1:fs/reiserfs/journal.o
10301f5207b7SJohn Levon 		 * If there is a function definition without parameter name found
10311f5207b7SJohn Levon 		 * after a function implementation then it causes a crash.
10321f5207b7SJohn Levon 		 * int foo() {}
10331f5207b7SJohn Levon 		 * int bar(char *);
10341f5207b7SJohn Levon 		 */
10351f5207b7SJohn Levon 		if (arg->ident->name < (char *)100)
10361f5207b7SJohn Levon 			continue;
10371f5207b7SJohn Levon 		if (i == param) {
10381f5207b7SJohn Levon 			*name = arg->ident->name;
10391f5207b7SJohn Levon 			*sym = arg;
10401f5207b7SJohn Levon 			return TRUE;
10411f5207b7SJohn Levon 		}
10421f5207b7SJohn Levon 		i++;
10431f5207b7SJohn Levon 	} END_FOR_EACH_PTR(arg);
10441f5207b7SJohn Levon 
10451f5207b7SJohn Levon 	return FALSE;
10461f5207b7SJohn Levon }
10471f5207b7SJohn Levon 
function_signature_matches(const char * sig)10481f5207b7SJohn Levon static int function_signature_matches(const char *sig)
10491f5207b7SJohn Levon {
10501f5207b7SJohn Levon 	char *my_sig;
10511f5207b7SJohn Levon 
10521f5207b7SJohn Levon 	my_sig = function_signature();
10531f5207b7SJohn Levon 	if (!sig || !my_sig)
10541f5207b7SJohn Levon 		return 1;  /* default to matching */
10551f5207b7SJohn Levon 	if (strcmp(my_sig, sig) == 0)
10561f5207b7SJohn Levon 		  return 1;
10571f5207b7SJohn Levon 	return 0;
10581f5207b7SJohn Levon }
10591f5207b7SJohn Levon 
caller_info_callback(void * _data,int argc,char ** argv,char ** azColName)10601f5207b7SJohn Levon static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
10611f5207b7SJohn Levon {
10621f5207b7SJohn Levon 	struct select_caller_info_data *data = _data;
10631f5207b7SJohn Levon 	int func_id;
10641f5207b7SJohn Levon 	long type;
10651f5207b7SJohn Levon 	long param;
10661f5207b7SJohn Levon 	char *key;
10671f5207b7SJohn Levon 	char *value;
10681f5207b7SJohn Levon 	char *name = NULL;
10691f5207b7SJohn Levon 	struct symbol *sym = NULL;
10701f5207b7SJohn Levon 	struct def_callback *def_callback;
10711f5207b7SJohn Levon 	struct stree *stree;
10721f5207b7SJohn Levon 	struct timeval cur_time;
10731f5207b7SJohn Levon 
10741f5207b7SJohn Levon 	data->results = 1;
10751f5207b7SJohn Levon 
10761f5207b7SJohn Levon 	if (argc != 5)
10771f5207b7SJohn Levon 		return 0;
10781f5207b7SJohn Levon 
10791f5207b7SJohn Levon 	gettimeofday(&cur_time, NULL);
10801f5207b7SJohn Levon 	if (cur_time.tv_sec - data->start_time.tv_sec > 10)
10811f5207b7SJohn Levon 		return 0;
10821f5207b7SJohn Levon 
10831f5207b7SJohn Levon 	func_id = atoi(argv[0]);
10841f5207b7SJohn Levon 	errno = 0;
10851f5207b7SJohn Levon 	type = strtol(argv[1], NULL, 10);
10861f5207b7SJohn Levon 	param = strtol(argv[2], NULL, 10);
10871f5207b7SJohn Levon 	if (errno)
10881f5207b7SJohn Levon 		return 0;
10891f5207b7SJohn Levon 	key = argv[3];
10901f5207b7SJohn Levon 	value = argv[4];
10911f5207b7SJohn Levon 
10921f5207b7SJohn Levon 	if (data->prev_func_id == -1)
10931f5207b7SJohn Levon 		data->prev_func_id = func_id;
10941f5207b7SJohn Levon 	if (func_id != data->prev_func_id) {
10951f5207b7SJohn Levon 		stree = __pop_fake_cur_stree();
10961f5207b7SJohn Levon 		if (!data->ignore)
10971f5207b7SJohn Levon 			merge_stree(&data->final_states, stree);
10981f5207b7SJohn Levon 		free_stree(&stree);
10991f5207b7SJohn Levon 		__push_fake_cur_stree();
11001f5207b7SJohn Levon 		__unnullify_path();
11011f5207b7SJohn Levon 		data->prev_func_id = func_id;
11021f5207b7SJohn Levon 		data->ignore = 0;
11031f5207b7SJohn Levon 	}
11041f5207b7SJohn Levon 
11051f5207b7SJohn Levon 	if (data->ignore)
11061f5207b7SJohn Levon 		return 0;
11071f5207b7SJohn Levon 	if (type == INTERNAL &&
11081f5207b7SJohn Levon 	    !function_signature_matches(value)) {
11091f5207b7SJohn Levon 		data->ignore = 1;
11101f5207b7SJohn Levon 		return 0;
11111f5207b7SJohn Levon 	}
11121f5207b7SJohn Levon 
11131f5207b7SJohn Levon 	if (param >= 0 && !get_param(param, &name, &sym))
11141f5207b7SJohn Levon 		return 0;
11151f5207b7SJohn Levon 
11161f5207b7SJohn Levon 	FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
11171f5207b7SJohn Levon 		if (def_callback->hook_type == type)
11181f5207b7SJohn Levon 			def_callback->callback(name, sym, key, value);
11191f5207b7SJohn Levon 	} END_FOR_EACH_PTR(def_callback);
11201f5207b7SJohn Levon 
11211f5207b7SJohn Levon 	return 0;
11221f5207b7SJohn Levon }
11231f5207b7SJohn Levon 
11241f5207b7SJohn Levon static struct string_list *ptr_names_done;
11251f5207b7SJohn Levon static struct string_list *ptr_names;
11261f5207b7SJohn Levon 
get_ptr_name(void * unused,int argc,char ** argv,char ** azColName)11271f5207b7SJohn Levon static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
11281f5207b7SJohn Levon {
11291f5207b7SJohn Levon 	insert_string(&ptr_names, alloc_string(argv[0]));
11301f5207b7SJohn Levon 	return 0;
11311f5207b7SJohn Levon }
11321f5207b7SJohn Levon 
get_next_ptr_name(void)11331f5207b7SJohn Levon static char *get_next_ptr_name(void)
11341f5207b7SJohn Levon {
11351f5207b7SJohn Levon 	char *ptr;
11361f5207b7SJohn Levon 
11371f5207b7SJohn Levon 	FOR_EACH_PTR(ptr_names, ptr) {
1138efe51d0cSJohn Levon 		if (!insert_string(&ptr_names_done, ptr))
11391f5207b7SJohn Levon 			continue;
11401f5207b7SJohn Levon 		return ptr;
11411f5207b7SJohn Levon 	} END_FOR_EACH_PTR(ptr);
11421f5207b7SJohn Levon 	return NULL;
11431f5207b7SJohn Levon }
11441f5207b7SJohn Levon 
get_ptr_names(const char * file,const char * name)11451f5207b7SJohn Levon static void get_ptr_names(const char *file, const char *name)
11461f5207b7SJohn Levon {
11471f5207b7SJohn Levon 	char sql_filter[1024];
11481f5207b7SJohn Levon 	int before, after;
11491f5207b7SJohn Levon 
11501f5207b7SJohn Levon 	if (file) {
11511f5207b7SJohn Levon 		snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
11521f5207b7SJohn Levon 			 file, name);
11531f5207b7SJohn Levon 	} else {
11541f5207b7SJohn Levon 		snprintf(sql_filter, 1024, "function = '%s';", name);
11551f5207b7SJohn Levon 	}
11561f5207b7SJohn Levon 
11571f5207b7SJohn Levon 	before = ptr_list_size((struct ptr_list *)ptr_names);
11581f5207b7SJohn Levon 
11591f5207b7SJohn Levon 	run_sql(get_ptr_name, NULL,
11601f5207b7SJohn Levon 		"select distinct ptr from function_ptr where %s",
11611f5207b7SJohn Levon 		sql_filter);
11621f5207b7SJohn Levon 
11631f5207b7SJohn Levon 	after = ptr_list_size((struct ptr_list *)ptr_names);
11641f5207b7SJohn Levon 	if (before == after)
11651f5207b7SJohn Levon 		return;
11661f5207b7SJohn Levon 
11671f5207b7SJohn Levon 	while ((name = get_next_ptr_name()))
11681f5207b7SJohn Levon 		get_ptr_names(NULL, name);
11691f5207b7SJohn Levon }
11701f5207b7SJohn Levon 
match_data_from_db(struct symbol * sym)11711f5207b7SJohn Levon static void match_data_from_db(struct symbol *sym)
11721f5207b7SJohn Levon {
11731f5207b7SJohn Levon 	struct select_caller_info_data data = { .prev_func_id = -1 };
11741f5207b7SJohn Levon 	struct sm_state *sm;
11751f5207b7SJohn Levon 	struct stree *stree;
11761f5207b7SJohn Levon 	struct timeval end_time;
11771f5207b7SJohn Levon 
11781f5207b7SJohn Levon 	if (!sym || !sym->ident)
11791f5207b7SJohn Levon 		return;
11801f5207b7SJohn Levon 
11816523a3aaSJohn Levon 	set_fn_mtag(sym);
11821f5207b7SJohn Levon 	gettimeofday(&data.start_time, NULL);
11831f5207b7SJohn Levon 
11841f5207b7SJohn Levon 	__push_fake_cur_stree();
11851f5207b7SJohn Levon 	__unnullify_path();
11861f5207b7SJohn Levon 
11871f5207b7SJohn Levon 	if (!__inline_fn) {
11881f5207b7SJohn Levon 		char *ptr;
11891f5207b7SJohn Levon 
11901f5207b7SJohn Levon 		if (sym->ctype.modifiers & MOD_STATIC)
11911f5207b7SJohn Levon 			get_ptr_names(get_base_file(), sym->ident->name);
11921f5207b7SJohn Levon 		else
11931f5207b7SJohn Levon 			get_ptr_names(NULL, sym->ident->name);
11941f5207b7SJohn Levon 
11951f5207b7SJohn Levon 		if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
11961f5207b7SJohn Levon 			__free_ptr_list((struct ptr_list **)&ptr_names);
11971f5207b7SJohn Levon 			__free_ptr_list((struct ptr_list **)&ptr_names_done);
1198c85f09ccSJohn Levon 			__free_fake_cur_stree();
11991f5207b7SJohn Levon 			return;
12001f5207b7SJohn Levon 		}
12011f5207b7SJohn Levon 
12021f5207b7SJohn Levon 		sql_select_caller_info(&data,
12031f5207b7SJohn Levon 				       "call_id, type, parameter, key, value",
12041f5207b7SJohn Levon 				       sym);
12051f5207b7SJohn Levon 
12061f5207b7SJohn Levon 
12071f5207b7SJohn Levon 		stree = __pop_fake_cur_stree();
12081f5207b7SJohn Levon 		if (!data.ignore)
12091f5207b7SJohn Levon 			merge_stree(&data.final_states, stree);
12101f5207b7SJohn Levon 		free_stree(&stree);
12111f5207b7SJohn Levon 		__push_fake_cur_stree();
12121f5207b7SJohn Levon 		__unnullify_path();
12131f5207b7SJohn Levon 		data.prev_func_id = -1;
12141f5207b7SJohn Levon 		data.ignore = 0;
1215c85f09ccSJohn Levon 		data.results = 0;
12161f5207b7SJohn Levon 
12171f5207b7SJohn Levon 		FOR_EACH_PTR(ptr_names, ptr) {
12181f5207b7SJohn Levon 			run_sql(caller_info_callback, &data,
12191f5207b7SJohn Levon 				"select call_id, type, parameter, key, value"
12201f5207b7SJohn Levon 				" from common_caller_info where function = '%s' order by call_id",
12211f5207b7SJohn Levon 				ptr);
12221f5207b7SJohn Levon 		} END_FOR_EACH_PTR(ptr);
12231f5207b7SJohn Levon 
12241f5207b7SJohn Levon 		if (data.results) {
12251f5207b7SJohn Levon 			FOR_EACH_PTR(ptr_names, ptr) {
12261f5207b7SJohn Levon 				free_string(ptr);
12271f5207b7SJohn Levon 			} END_FOR_EACH_PTR(ptr);
12281f5207b7SJohn Levon 			goto free_ptr_names;
12291f5207b7SJohn Levon 		}
12301f5207b7SJohn Levon 
12311f5207b7SJohn Levon 		FOR_EACH_PTR(ptr_names, ptr) {
12321f5207b7SJohn Levon 			run_sql(caller_info_callback, &data,
12331f5207b7SJohn Levon 				"select call_id, type, parameter, key, value"
12341f5207b7SJohn Levon 				" from caller_info where function = '%s' order by call_id",
12351f5207b7SJohn Levon 				ptr);
12361f5207b7SJohn Levon 			free_string(ptr);
12371f5207b7SJohn Levon 		} END_FOR_EACH_PTR(ptr);
12381f5207b7SJohn Levon 
12391f5207b7SJohn Levon free_ptr_names:
12401f5207b7SJohn Levon 		__free_ptr_list((struct ptr_list **)&ptr_names);
12411f5207b7SJohn Levon 		__free_ptr_list((struct ptr_list **)&ptr_names_done);
12421f5207b7SJohn Levon 	} else {
12431f5207b7SJohn Levon 		sql_select_caller_info(&data,
12441f5207b7SJohn Levon 				       "call_id, type, parameter, key, value",
12451f5207b7SJohn Levon 				       sym);
12461f5207b7SJohn Levon 	}
12471f5207b7SJohn Levon 
12481f5207b7SJohn Levon 	stree = __pop_fake_cur_stree();
12491f5207b7SJohn Levon 	if (!data.ignore)
12501f5207b7SJohn Levon 		merge_stree(&data.final_states, stree);
12511f5207b7SJohn Levon 	free_stree(&stree);
12521f5207b7SJohn Levon 
12531f5207b7SJohn Levon 	gettimeofday(&end_time, NULL);
12541f5207b7SJohn Levon 	if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
12551f5207b7SJohn Levon 		FOR_EACH_SM(data.final_states, sm) {
12561f5207b7SJohn Levon 			__set_sm(sm);
12571f5207b7SJohn Levon 		} END_FOR_EACH_SM(sm);
12581f5207b7SJohn Levon 	}
12591f5207b7SJohn Levon 
12601f5207b7SJohn Levon 	free_stree(&data.final_states);
12611f5207b7SJohn Levon }
12621f5207b7SJohn Levon 
return_implies_callbacks(void * _info,int argc,char ** argv,char ** azColName)12631f5207b7SJohn Levon static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
12641f5207b7SJohn Levon {
12651f5207b7SJohn Levon 	struct implies_info *info = _info;
12661f5207b7SJohn Levon 	struct db_implies_callback *cb;
12671f5207b7SJohn Levon 	struct expression *arg = NULL;
12681f5207b7SJohn Levon 	int type;
12691f5207b7SJohn Levon 	int param;
12701f5207b7SJohn Levon 
12711f5207b7SJohn Levon 	if (argc != 5)
12721f5207b7SJohn Levon 		return 0;
12731f5207b7SJohn Levon 
12741f5207b7SJohn Levon 	type = atoi(argv[1]);
12751f5207b7SJohn Levon 	param = atoi(argv[2]);
12761f5207b7SJohn Levon 
12771f5207b7SJohn Levon 	FOR_EACH_PTR(info->cb_list, cb) {
12781f5207b7SJohn Levon 		if (cb->type != type)
12791f5207b7SJohn Levon 			continue;
12801f5207b7SJohn Levon 		if (param != -1) {
12811f5207b7SJohn Levon 			arg = get_argument_from_call_expr(info->expr->args, param);
12821f5207b7SJohn Levon 			if (!arg)
12831f5207b7SJohn Levon 				continue;
12841f5207b7SJohn Levon 		}
12851f5207b7SJohn Levon 		cb->callback(info->expr, arg, argv[3], argv[4]);
12861f5207b7SJohn Levon 	} END_FOR_EACH_PTR(cb);
12871f5207b7SJohn Levon 
12881f5207b7SJohn Levon 	return 0;
12891f5207b7SJohn Levon }
12901f5207b7SJohn Levon 
call_implies_callbacks(void * _info,int argc,char ** argv,char ** azColName)12911f5207b7SJohn Levon static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
12921f5207b7SJohn Levon {
12931f5207b7SJohn Levon 	struct implies_info *info = _info;
12941f5207b7SJohn Levon 	struct db_implies_callback *cb;
12951f5207b7SJohn Levon 	struct expression *arg;
12961f5207b7SJohn Levon 	struct symbol *sym;
12971f5207b7SJohn Levon 	char *name;
12981f5207b7SJohn Levon 	int type;
12991f5207b7SJohn Levon 	int param;
13001f5207b7SJohn Levon 
13011f5207b7SJohn Levon 	if (argc != 5)
13021f5207b7SJohn Levon 		return 0;
13031f5207b7SJohn Levon 
13041f5207b7SJohn Levon 	type = atoi(argv[1]);
13051f5207b7SJohn Levon 	param = atoi(argv[2]);
13061f5207b7SJohn Levon 
13071f5207b7SJohn Levon 	if (!get_param(param, &name, &sym))
13081f5207b7SJohn Levon 		return 0;
13091f5207b7SJohn Levon 	arg = symbol_expression(sym);
13101f5207b7SJohn Levon 	if (!arg)
13111f5207b7SJohn Levon 		return 0;
13121f5207b7SJohn Levon 
13131f5207b7SJohn Levon 	FOR_EACH_PTR(info->cb_list, cb) {
13141f5207b7SJohn Levon 		if (cb->type != type)
13151f5207b7SJohn Levon 			continue;
13161f5207b7SJohn Levon 		cb->callback(info->expr, arg, argv[3], argv[4]);
13171f5207b7SJohn Levon 	} END_FOR_EACH_PTR(cb);
13181f5207b7SJohn Levon 
13191f5207b7SJohn Levon 	return 0;
13201f5207b7SJohn Levon }
13211f5207b7SJohn Levon 
match_return_implies(struct expression * expr)13221f5207b7SJohn Levon static void match_return_implies(struct expression *expr)
13231f5207b7SJohn Levon {
13241f5207b7SJohn Levon 	struct implies_info info = {
13251f5207b7SJohn Levon 		.type = RETURN_IMPLIES,
13261f5207b7SJohn Levon 		.cb_list = return_implies_cb_list,
13271f5207b7SJohn Levon 	};
13281f5207b7SJohn Levon 
13291f5207b7SJohn Levon 	if (expr->fn->type != EXPR_SYMBOL ||
13301f5207b7SJohn Levon 	    !expr->fn->symbol)
13311f5207b7SJohn Levon 		return;
13321f5207b7SJohn Levon 	info.expr = expr;
13331f5207b7SJohn Levon 	info.sym = expr->fn->symbol;
13341f5207b7SJohn Levon 	sql_select_implies("function, type, parameter, key, value", &info,
13351f5207b7SJohn Levon 			   return_implies_callbacks);
13361f5207b7SJohn Levon }
13371f5207b7SJohn Levon 
match_call_implies(struct symbol * sym)13381f5207b7SJohn Levon static void match_call_implies(struct symbol *sym)
13391f5207b7SJohn Levon {
13401f5207b7SJohn Levon 	struct implies_info info = {
13411f5207b7SJohn Levon 		.type = CALL_IMPLIES,
13421f5207b7SJohn Levon 		.cb_list = call_implies_cb_list,
13431f5207b7SJohn Levon 	};
13441f5207b7SJohn Levon 
13451f5207b7SJohn Levon 	if (!sym || !sym->ident)
13461f5207b7SJohn Levon 		return;
13471f5207b7SJohn Levon 
13481f5207b7SJohn Levon 	info.sym = sym;
13491f5207b7SJohn Levon 	sql_select_implies("function, type, parameter, key, value", &info,
13501f5207b7SJohn Levon 			   call_implies_callbacks);
13511f5207b7SJohn Levon }
13521f5207b7SJohn Levon 
get_fn_param_str(struct expression * expr)1353c85f09ccSJohn Levon static char *get_fn_param_str(struct expression *expr)
1354c85f09ccSJohn Levon {
1355c85f09ccSJohn Levon 	struct expression *tmp;
1356c85f09ccSJohn Levon 	int param;
1357c85f09ccSJohn Levon 	char buf[32];
1358c85f09ccSJohn Levon 
1359c85f09ccSJohn Levon 	tmp = get_assigned_expr(expr);
1360c85f09ccSJohn Levon 	if (tmp)
1361c85f09ccSJohn Levon 		expr = tmp;
1362c85f09ccSJohn Levon 	expr = strip_expr(expr);
1363c85f09ccSJohn Levon 	if (!expr || expr->type != EXPR_CALL)
1364c85f09ccSJohn Levon 		return NULL;
1365c85f09ccSJohn Levon 	expr = strip_expr(expr->fn);
1366c85f09ccSJohn Levon 	if (!expr || expr->type != EXPR_SYMBOL)
1367c85f09ccSJohn Levon 		return NULL;
1368c85f09ccSJohn Levon 	param = get_param_num(expr);
1369c85f09ccSJohn Levon 	if (param < 0)
1370c85f09ccSJohn Levon 		return NULL;
1371c85f09ccSJohn Levon 
1372c85f09ccSJohn Levon 	snprintf(buf, sizeof(buf), "[r $%d]", param);
1373c85f09ccSJohn Levon 	return alloc_sname(buf);
1374c85f09ccSJohn Levon }
1375c85f09ccSJohn Levon 
get_return_compare_is_param(struct expression * expr)1376efe51d0cSJohn Levon static char *get_return_compare_is_param(struct expression *expr)
13771f5207b7SJohn Levon {
1378efe51d0cSJohn Levon 	char *var;
1379efe51d0cSJohn Levon 	char buf[256];
1380efe51d0cSJohn Levon 	int comparison;
1381efe51d0cSJohn Levon 	int param;
13821f5207b7SJohn Levon 
1383efe51d0cSJohn Levon 	param = get_param_num(expr);
1384efe51d0cSJohn Levon 	if (param < 0)
1385efe51d0cSJohn Levon 		return NULL;
1386efe51d0cSJohn Levon 
1387efe51d0cSJohn Levon 	var = expr_to_var(expr);
1388efe51d0cSJohn Levon 	if (!var)
1389efe51d0cSJohn Levon 		return NULL;
1390efe51d0cSJohn Levon 	snprintf(buf, sizeof(buf), "%s orig", var);
1391efe51d0cSJohn Levon 	comparison = get_comparison_strings(var, buf);
1392efe51d0cSJohn Levon 	free_string(var);
1393efe51d0cSJohn Levon 
1394efe51d0cSJohn Levon 	if (!comparison)
1395efe51d0cSJohn Levon 		return NULL;
1396efe51d0cSJohn Levon 
1397efe51d0cSJohn Levon 	snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1398efe51d0cSJohn Levon 	return alloc_sname(buf);
13991f5207b7SJohn Levon }
14001f5207b7SJohn Levon 
get_return_compare_str(struct expression * expr)1401efe51d0cSJohn Levon static char *get_return_compare_str(struct expression *expr)
14021f5207b7SJohn Levon {
1403efe51d0cSJohn Levon 	char *compare_str;
14041f5207b7SJohn Levon 
1405efe51d0cSJohn Levon 	compare_str = get_return_compare_is_param(expr);
1406efe51d0cSJohn Levon 	if (compare_str)
1407efe51d0cSJohn Levon 		return compare_str;
1408efe51d0cSJohn Levon 
1409efe51d0cSJohn Levon 	compare_str = expr_lte_to_param(expr, -1);
1410efe51d0cSJohn Levon 	if (compare_str)
1411efe51d0cSJohn Levon 		return compare_str;
1412efe51d0cSJohn Levon 
1413efe51d0cSJohn Levon 	return expr_param_comparison(expr, -1);
1414efe51d0cSJohn Levon }
1415efe51d0cSJohn Levon 
get_return_ranges_str(struct expression * expr,struct range_list ** rl_p)1416efe51d0cSJohn Levon static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1417efe51d0cSJohn Levon {
1418efe51d0cSJohn Levon 	struct range_list *rl;
1419efe51d0cSJohn Levon 	char *return_ranges;
1420efe51d0cSJohn Levon 	sval_t sval;
1421c85f09ccSJohn Levon 	char *fn_param_str;
1422efe51d0cSJohn Levon 	char *compare_str;
1423efe51d0cSJohn Levon 	char *math_str;
1424efe51d0cSJohn Levon 	char buf[128];
1425efe51d0cSJohn Levon 
1426efe51d0cSJohn Levon 	*rl_p = NULL;
1427efe51d0cSJohn Levon 
1428efe51d0cSJohn Levon 	if (!expr)
1429efe51d0cSJohn Levon 		return alloc_sname("");
1430efe51d0cSJohn Levon 
1431efe51d0cSJohn Levon 	if (get_implied_value(expr, &sval)) {
1432efe51d0cSJohn Levon 		sval = sval_cast(cur_func_return_type(), sval);
1433efe51d0cSJohn Levon 		*rl_p = alloc_rl(sval, sval);
1434efe51d0cSJohn Levon 		return sval_to_str_or_err_ptr(sval);
14351f5207b7SJohn Levon 	}
1436efe51d0cSJohn Levon 
1437c85f09ccSJohn Levon 	fn_param_str = get_fn_param_str(expr);
1438efe51d0cSJohn Levon 	compare_str = expr_equal_to_param(expr, -1);
1439efe51d0cSJohn Levon 	math_str = get_value_in_terms_of_parameter_math(expr);
1440efe51d0cSJohn Levon 
1441efe51d0cSJohn Levon 	if (get_implied_rl(expr, &rl) && !is_whole_rl(rl)) {
1442efe51d0cSJohn Levon 		rl = cast_rl(cur_func_return_type(), rl);
1443efe51d0cSJohn Levon 		return_ranges = show_rl(rl);
1444efe51d0cSJohn Levon 	} else if (get_imaginary_absolute(expr, &rl)){
1445efe51d0cSJohn Levon 		rl = cast_rl(cur_func_return_type(), rl);
1446efe51d0cSJohn Levon 		return alloc_sname(show_rl(rl));
1447efe51d0cSJohn Levon 	} else {
1448efe51d0cSJohn Levon 		get_absolute_rl(expr, &rl);
1449efe51d0cSJohn Levon 		rl = cast_rl(cur_func_return_type(), rl);
1450efe51d0cSJohn Levon 		return_ranges = show_rl(rl);
1451efe51d0cSJohn Levon 	}
1452efe51d0cSJohn Levon 	*rl_p = rl;
1453efe51d0cSJohn Levon 
1454c85f09ccSJohn Levon 	if (fn_param_str) {
1455c85f09ccSJohn Levon 		snprintf(buf, sizeof(buf), "%s%s", return_ranges, fn_param_str);
1456c85f09ccSJohn Levon 		return alloc_sname(buf);
1457c85f09ccSJohn Levon 	}
1458efe51d0cSJohn Levon 	if (compare_str) {
1459efe51d0cSJohn Levon 		snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1460efe51d0cSJohn Levon 		return alloc_sname(buf);
1461efe51d0cSJohn Levon 	}
1462efe51d0cSJohn Levon 	if (math_str) {
1463efe51d0cSJohn Levon 		snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1464efe51d0cSJohn Levon 		return alloc_sname(buf);
1465efe51d0cSJohn Levon 	}
1466efe51d0cSJohn Levon 	compare_str = get_return_compare_str(expr);
1467efe51d0cSJohn Levon 	if (compare_str) {
1468efe51d0cSJohn Levon 		snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1469efe51d0cSJohn Levon 		return alloc_sname(buf);
1470efe51d0cSJohn Levon 	}
1471efe51d0cSJohn Levon 
1472efe51d0cSJohn Levon 	return return_ranges;
14731f5207b7SJohn Levon }
14741f5207b7SJohn Levon 
match_return_info(int return_id,char * return_ranges,struct expression * expr)14751f5207b7SJohn Levon static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
14761f5207b7SJohn Levon {
14771f5207b7SJohn Levon 	sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
14781f5207b7SJohn Levon }
14791f5207b7SJohn Levon 
call_return_state_hooks_conditional(struct expression * expr)14805a0e240fSJohn Levon static bool call_return_state_hooks_conditional(struct expression *expr)
14811f5207b7SJohn Levon {
14821f5207b7SJohn Levon 	int final_pass_orig = final_pass;
14835a0e240fSJohn Levon 	static int recurse;
14845a0e240fSJohn Levon 
14855a0e240fSJohn Levon 	if (recurse >= 2)
14865a0e240fSJohn Levon 		return false;
14875a0e240fSJohn Levon 	if (!expr ||
14885a0e240fSJohn Levon 	    (expr->type != EXPR_CONDITIONAL && expr->type != EXPR_SELECT))
14895a0e240fSJohn Levon 		return false;
14905a0e240fSJohn Levon 
14915a0e240fSJohn Levon 	recurse++;
14921f5207b7SJohn Levon 
14931f5207b7SJohn Levon 	__push_fake_cur_stree();
14941f5207b7SJohn Levon 
14951f5207b7SJohn Levon 	final_pass = 0;
14961f5207b7SJohn Levon 	__split_whole_condition(expr->conditional);
14971f5207b7SJohn Levon 	final_pass = final_pass_orig;
14981f5207b7SJohn Levon 
14995a0e240fSJohn Levon 	call_return_state_hooks(expr->cond_true ?: expr->conditional);
15001f5207b7SJohn Levon 
15011f5207b7SJohn Levon 	__push_true_states();
15021f5207b7SJohn Levon 	__use_false_states();
15031f5207b7SJohn Levon 
15045a0e240fSJohn Levon 	call_return_state_hooks(expr->cond_false);
15051f5207b7SJohn Levon 
15061f5207b7SJohn Levon 	__merge_true_states();
15071f5207b7SJohn Levon 	__free_fake_cur_stree();
15085a0e240fSJohn Levon 
15095a0e240fSJohn Levon 	recurse--;
15105a0e240fSJohn Levon 	return true;
15111f5207b7SJohn Levon }
15121f5207b7SJohn Levon 
call_return_state_hooks_compare(struct expression * expr)15131f5207b7SJohn Levon static void call_return_state_hooks_compare(struct expression *expr)
15141f5207b7SJohn Levon {
15151f5207b7SJohn Levon 	struct returned_state_callback *cb;
15161f5207b7SJohn Levon 	char *return_ranges;
15171f5207b7SJohn Levon 	int final_pass_orig = final_pass;
15181f5207b7SJohn Levon 	sval_t sval = { .type = &int_ctype };
15191f5207b7SJohn Levon 	sval_t ret;
15201f5207b7SJohn Levon 
15211f5207b7SJohn Levon 	if (!get_implied_value(expr, &ret))
15221f5207b7SJohn Levon 		ret.value = -1;
15231f5207b7SJohn Levon 
15241f5207b7SJohn Levon 	__push_fake_cur_stree();
15251f5207b7SJohn Levon 
15261f5207b7SJohn Levon 	final_pass = 0;
15271f5207b7SJohn Levon 	__split_whole_condition(expr);
15281f5207b7SJohn Levon 	final_pass = final_pass_orig;
15291f5207b7SJohn Levon 
15301f5207b7SJohn Levon 	if (ret.value != 0) {
15311f5207b7SJohn Levon 		return_ranges = alloc_sname("1");
15321f5207b7SJohn Levon 		sval.value = 1;
15331f5207b7SJohn Levon 		set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
15341f5207b7SJohn Levon 
15351f5207b7SJohn Levon 		return_id++;
15361f5207b7SJohn Levon 		FOR_EACH_PTR(returned_state_callbacks, cb) {
15371f5207b7SJohn Levon 			cb->callback(return_id, return_ranges, expr);
15381f5207b7SJohn Levon 		} END_FOR_EACH_PTR(cb);
15391f5207b7SJohn Levon 	}
15401f5207b7SJohn Levon 
15411f5207b7SJohn Levon 	__push_true_states();
15421f5207b7SJohn Levon 	__use_false_states();
15431f5207b7SJohn Levon 
15441f5207b7SJohn Levon 	if (ret.value != 1) {
15451f5207b7SJohn Levon 		return_ranges = alloc_sname("0");
15461f5207b7SJohn Levon 		sval.value = 0;
15471f5207b7SJohn Levon 		set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
15481f5207b7SJohn Levon 
15491f5207b7SJohn Levon 		return_id++;
15501f5207b7SJohn Levon 		FOR_EACH_PTR(returned_state_callbacks, cb) {
15511f5207b7SJohn Levon 			cb->callback(return_id, return_ranges, expr);
15521f5207b7SJohn Levon 		} END_FOR_EACH_PTR(cb);
15531f5207b7SJohn Levon 	}
15541f5207b7SJohn Levon 
15551f5207b7SJohn Levon 	__merge_true_states();
15561f5207b7SJohn Levon 	__free_fake_cur_stree();
15571f5207b7SJohn Levon }
15581f5207b7SJohn Levon 
ptr_in_list(struct sm_state * sm,struct state_list * slist)15591f5207b7SJohn Levon static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
15601f5207b7SJohn Levon {
15611f5207b7SJohn Levon 	struct sm_state *tmp;
15621f5207b7SJohn Levon 
15631f5207b7SJohn Levon 	FOR_EACH_PTR(slist, tmp) {
15641f5207b7SJohn Levon 		if (strcmp(tmp->state->name, sm->state->name) == 0)
15651f5207b7SJohn Levon 			return 1;
15661f5207b7SJohn Levon 	} END_FOR_EACH_PTR(tmp);
15671f5207b7SJohn Levon 
15681f5207b7SJohn Levon 	return 0;
15691f5207b7SJohn Levon }
15701f5207b7SJohn Levon 
split_possible_helper(struct sm_state * sm,struct expression * expr)15711f5207b7SJohn Levon static int split_possible_helper(struct sm_state *sm, struct expression *expr)
15721f5207b7SJohn Levon {
15731f5207b7SJohn Levon 	struct returned_state_callback *cb;
15741f5207b7SJohn Levon 	struct range_list *rl;
15751f5207b7SJohn Levon 	char *return_ranges;
15761f5207b7SJohn Levon 	struct sm_state *tmp;
15771f5207b7SJohn Levon 	int ret = 0;
15781f5207b7SJohn Levon 	int nr_possible, nr_states;
1579efe51d0cSJohn Levon 	char *compare_str;
15801f5207b7SJohn Levon 	char buf[128];
15811f5207b7SJohn Levon 	struct state_list *already_handled = NULL;
1582efe51d0cSJohn Levon 	sval_t sval;
15831f5207b7SJohn Levon 
15841f5207b7SJohn Levon 	if (!sm || !sm->merged)
15851f5207b7SJohn Levon 		return 0;
15861f5207b7SJohn Levon 
15871f5207b7SJohn Levon 	if (too_many_possible(sm))
15881f5207b7SJohn Levon 		return 0;
15891f5207b7SJohn Levon 
15901f5207b7SJohn Levon 	/* bail if it gets too complicated */
1591efe51d0cSJohn Levon 	nr_possible = 0;
1592efe51d0cSJohn Levon 	FOR_EACH_PTR(sm->possible, tmp) {
1593efe51d0cSJohn Levon 		if (tmp->merged)
1594efe51d0cSJohn Levon 			continue;
15955a0e240fSJohn Levon 		if (ptr_in_list(tmp, already_handled))
15965a0e240fSJohn Levon 			continue;
15975a0e240fSJohn Levon 		add_ptr_list(&already_handled, tmp);
1598efe51d0cSJohn Levon 		nr_possible++;
1599efe51d0cSJohn Levon 	} END_FOR_EACH_PTR(tmp);
16005a0e240fSJohn Levon 	free_slist(&already_handled);
16011f5207b7SJohn Levon 	nr_states = get_db_state_count();
16021f5207b7SJohn Levon 	if (nr_states * nr_possible >= 2000)
16031f5207b7SJohn Levon 		return 0;
16041f5207b7SJohn Levon 
16051f5207b7SJohn Levon 	FOR_EACH_PTR(sm->possible, tmp) {
16061f5207b7SJohn Levon 		if (tmp->merged)
16071f5207b7SJohn Levon 			continue;
16081f5207b7SJohn Levon 		if (ptr_in_list(tmp, already_handled))
16091f5207b7SJohn Levon 			continue;
16101f5207b7SJohn Levon 		add_ptr_list(&already_handled, tmp);
16111f5207b7SJohn Levon 
16121f5207b7SJohn Levon 		ret = 1;
16131f5207b7SJohn Levon 		__push_fake_cur_stree();
16141f5207b7SJohn Levon 
16151f5207b7SJohn Levon 		overwrite_states_using_pool(sm, tmp);
16161f5207b7SJohn Levon 
16171f5207b7SJohn Levon 		rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
16181f5207b7SJohn Levon 		return_ranges = show_rl(rl);
16191f5207b7SJohn Levon 		set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1620efe51d0cSJohn Levon 		if (!rl_to_sval(rl, &sval)) {
1621efe51d0cSJohn Levon 			compare_str = get_return_compare_str(expr);
1622efe51d0cSJohn Levon 			if (compare_str) {
1623efe51d0cSJohn Levon 				snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1624efe51d0cSJohn Levon 				return_ranges = alloc_sname(buf);
1625efe51d0cSJohn Levon 			}
16261f5207b7SJohn Levon 		}
16271f5207b7SJohn Levon 
16281f5207b7SJohn Levon 		return_id++;
16291f5207b7SJohn Levon 		FOR_EACH_PTR(returned_state_callbacks, cb) {
16301f5207b7SJohn Levon 			cb->callback(return_id, return_ranges, expr);
16311f5207b7SJohn Levon 		} END_FOR_EACH_PTR(cb);
16321f5207b7SJohn Levon 
16331f5207b7SJohn Levon 		__free_fake_cur_stree();
16341f5207b7SJohn Levon 	} END_FOR_EACH_PTR(tmp);
16351f5207b7SJohn Levon 
16361f5207b7SJohn Levon 	free_slist(&already_handled);
16371f5207b7SJohn Levon 
16381f5207b7SJohn Levon 	return ret;
16391f5207b7SJohn Levon }
16401f5207b7SJohn Levon 
call_return_state_hooks_split_possible(struct expression * expr)16411f5207b7SJohn Levon static int call_return_state_hooks_split_possible(struct expression *expr)
16421f5207b7SJohn Levon {
16431f5207b7SJohn Levon 	struct sm_state *sm;
16441f5207b7SJohn Levon 
16456523a3aaSJohn Levon 	if (!expr)
16461f5207b7SJohn Levon 		return 0;
16471f5207b7SJohn Levon 
16481f5207b7SJohn Levon 	sm = get_sm_state_expr(SMATCH_EXTRA, expr);
16491f5207b7SJohn Levon 	return split_possible_helper(sm, expr);
16501f5207b7SJohn Levon }
16511f5207b7SJohn Levon 
has_possible_negative(struct sm_state * sm)16521f5207b7SJohn Levon static bool has_possible_negative(struct sm_state *sm)
16531f5207b7SJohn Levon {
16541f5207b7SJohn Levon 	struct sm_state *tmp;
16551f5207b7SJohn Levon 
16565a0e240fSJohn Levon 	if (!type_signed(estate_type(sm->state)))
16575a0e240fSJohn Levon 		return false;
16585a0e240fSJohn Levon 
16591f5207b7SJohn Levon 	FOR_EACH_PTR(sm->possible, tmp) {
16601f5207b7SJohn Levon 		if (!estate_rl(tmp->state))
16611f5207b7SJohn Levon 			continue;
16621f5207b7SJohn Levon 		if (sval_is_negative(estate_min(tmp->state)) &&
16631f5207b7SJohn Levon 		    sval_is_negative(estate_max(tmp->state)))
16641f5207b7SJohn Levon 			return true;
16651f5207b7SJohn Levon 	} END_FOR_EACH_PTR(tmp);
16661f5207b7SJohn Levon 
16671f5207b7SJohn Levon 	return false;
16681f5207b7SJohn Levon }
16691f5207b7SJohn Levon 
has_separate_zero_null(struct sm_state * sm)16705a0e240fSJohn Levon static bool has_separate_zero_null(struct sm_state *sm)
16711f5207b7SJohn Levon {
16721f5207b7SJohn Levon 	struct sm_state *tmp;
16731f5207b7SJohn Levon 	sval_t sval;
16741f5207b7SJohn Levon 
16751f5207b7SJohn Levon 	FOR_EACH_PTR(sm->possible, tmp) {
16761f5207b7SJohn Levon 		if (!estate_get_single_value(tmp->state, &sval))
16771f5207b7SJohn Levon 			continue;
16781f5207b7SJohn Levon 		if (sval.value == 0)
16791f5207b7SJohn Levon 			return true;
16801f5207b7SJohn Levon 	} END_FOR_EACH_PTR(tmp);
16811f5207b7SJohn Levon 
16821f5207b7SJohn Levon 	return false;
16831f5207b7SJohn Levon }
16841f5207b7SJohn Levon 
split_positive_from_negative(struct expression * expr)16851f5207b7SJohn Levon static int split_positive_from_negative(struct expression *expr)
16861f5207b7SJohn Levon {
16871f5207b7SJohn Levon 	struct sm_state *sm;
16881f5207b7SJohn Levon 	struct returned_state_callback *cb;
16891f5207b7SJohn Levon 	struct range_list *rl;
16901f5207b7SJohn Levon 	const char *return_ranges;
16911f5207b7SJohn Levon 	struct range_list *ret_rl;
16925a0e240fSJohn Levon 	bool separate_zero;
16931f5207b7SJohn Levon 	int undo;
16941f5207b7SJohn Levon 
16951f5207b7SJohn Levon 	/* We're going to print the states 3 times */
16961f5207b7SJohn Levon 	if (get_db_state_count() > 10000 / 3)
16971f5207b7SJohn Levon 		return 0;
16981f5207b7SJohn Levon 
16991f5207b7SJohn Levon 	if (!get_implied_rl(expr, &rl) || !rl)
17001f5207b7SJohn Levon 		return 0;
17011f5207b7SJohn Levon 	/* Forget about INT_MAX and larger */
17021f5207b7SJohn Levon 	if (rl_max(rl).value <= 0)
17031f5207b7SJohn Levon 		return 0;
17041f5207b7SJohn Levon 	if (!sval_is_negative(rl_min(rl)))
17051f5207b7SJohn Levon 		return 0;
17061f5207b7SJohn Levon 
17071f5207b7SJohn Levon 	sm = get_sm_state_expr(SMATCH_EXTRA, expr);
17081f5207b7SJohn Levon 	if (!sm)
17091f5207b7SJohn Levon 		return 0;
17101f5207b7SJohn Levon 	if (!has_possible_negative(sm))
17111f5207b7SJohn Levon 		return 0;
17125a0e240fSJohn Levon 	separate_zero = has_separate_zero_null(sm);
17131f5207b7SJohn Levon 
17145a0e240fSJohn Levon 	if (!assume(compare_expression(expr, separate_zero ? '>' : SPECIAL_GTE, zero_expr())))
17151f5207b7SJohn Levon 		return 0;
17161f5207b7SJohn Levon 
17171f5207b7SJohn Levon 	return_id++;
17181f5207b7SJohn Levon 	return_ranges = get_return_ranges_str(expr, &ret_rl);
17191f5207b7SJohn Levon 	set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
17201f5207b7SJohn Levon 	FOR_EACH_PTR(returned_state_callbacks, cb) {
17211f5207b7SJohn Levon 		cb->callback(return_id, (char *)return_ranges, expr);
17221f5207b7SJohn Levon 	} END_FOR_EACH_PTR(cb);
17231f5207b7SJohn Levon 
17241f5207b7SJohn Levon 	end_assume();
17251f5207b7SJohn Levon 
17265a0e240fSJohn Levon 	if (separate_zero) {
17271f5207b7SJohn Levon 		undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
17281f5207b7SJohn Levon 
17291f5207b7SJohn Levon 		return_id++;
17301f5207b7SJohn Levon 		return_ranges = get_return_ranges_str(expr, &ret_rl);
17311f5207b7SJohn Levon 		set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
17321f5207b7SJohn Levon 		FOR_EACH_PTR(returned_state_callbacks, cb) {
17331f5207b7SJohn Levon 			cb->callback(return_id, (char *)return_ranges, expr);
17341f5207b7SJohn Levon 		} END_FOR_EACH_PTR(cb);
17351f5207b7SJohn Levon 
17361f5207b7SJohn Levon 		if (undo)
17371f5207b7SJohn Levon 			end_assume();
17381f5207b7SJohn Levon 	}
17391f5207b7SJohn Levon 
17401f5207b7SJohn Levon 	undo = assume(compare_expression(expr, '<', zero_expr()));
17411f5207b7SJohn Levon 
17421f5207b7SJohn Levon 	return_id++;
17431f5207b7SJohn Levon 	return_ranges = get_return_ranges_str(expr, &ret_rl);
17441f5207b7SJohn Levon 	set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
17451f5207b7SJohn Levon 	FOR_EACH_PTR(returned_state_callbacks, cb) {
17461f5207b7SJohn Levon 		cb->callback(return_id, (char *)return_ranges, expr);
17471f5207b7SJohn Levon 	} END_FOR_EACH_PTR(cb);
17481f5207b7SJohn Levon 
17491f5207b7SJohn Levon 	if (undo)
17501f5207b7SJohn Levon 		end_assume();
17511f5207b7SJohn Levon 
17521f5207b7SJohn Levon 	return 1;
17531f5207b7SJohn Levon }
17541f5207b7SJohn Levon 
call_return_state_hooks_split_null_non_null_zero(struct expression * expr)1755efe51d0cSJohn Levon static int call_return_state_hooks_split_null_non_null_zero(struct expression *expr)
17561f5207b7SJohn Levon {
17571f5207b7SJohn Levon 	struct returned_state_callback *cb;
17581f5207b7SJohn Levon 	struct range_list *rl;
17591f5207b7SJohn Levon 	struct range_list *nonnull_rl;
17601f5207b7SJohn Levon 	sval_t null_sval;
17611f5207b7SJohn Levon 	struct range_list *null_rl = NULL;
17621f5207b7SJohn Levon 	char *return_ranges;
17631f5207b7SJohn Levon 	struct sm_state *sm;
17641f5207b7SJohn Levon 	struct smatch_state *state;
17651f5207b7SJohn Levon 	int nr_states;
17661f5207b7SJohn Levon 	int final_pass_orig = final_pass;
17671f5207b7SJohn Levon 
17681f5207b7SJohn Levon 	if (!expr || expr_equal_to_param(expr, -1))
17691f5207b7SJohn Levon 		return 0;
17701f5207b7SJohn Levon 	if (expr->type == EXPR_CALL)
17711f5207b7SJohn Levon 		return 0;
17721f5207b7SJohn Levon 
17731f5207b7SJohn Levon 	sm = get_sm_state_expr(SMATCH_EXTRA, expr);
17741f5207b7SJohn Levon 	if (!sm)
17751f5207b7SJohn Levon 		return 0;
17761f5207b7SJohn Levon 	if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
17771f5207b7SJohn Levon 		return 0;
17781f5207b7SJohn Levon 	state = sm->state;
17791f5207b7SJohn Levon 	if (!estate_rl(state))
17801f5207b7SJohn Levon 		return 0;
17811f5207b7SJohn Levon 	if (estate_min(state).value == 0 && estate_max(state).value == 0)
17821f5207b7SJohn Levon 		return 0;
17836523a3aaSJohn Levon 	if (has_possible_negative(sm))
17846523a3aaSJohn Levon 		return 0;
17855a0e240fSJohn Levon 	if (!has_separate_zero_null(sm))
17861f5207b7SJohn Levon 		return 0;
17871f5207b7SJohn Levon 
17881f5207b7SJohn Levon 	nr_states = get_db_state_count();
17891f5207b7SJohn Levon 	if (option_info && nr_states >= 1500)
17901f5207b7SJohn Levon 		return 0;
17911f5207b7SJohn Levon 
17921f5207b7SJohn Levon 	rl = estate_rl(state);
17931f5207b7SJohn Levon 
17941f5207b7SJohn Levon 	__push_fake_cur_stree();
17951f5207b7SJohn Levon 
17961f5207b7SJohn Levon 	final_pass = 0;
17971f5207b7SJohn Levon 	__split_whole_condition(expr);
17981f5207b7SJohn Levon 	final_pass = final_pass_orig;
17991f5207b7SJohn Levon 
18001f5207b7SJohn Levon 	nonnull_rl = rl_filter(rl, rl_zero());
18011f5207b7SJohn Levon 	return_ranges = show_rl(nonnull_rl);
18021f5207b7SJohn Levon 	set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
18031f5207b7SJohn Levon 
18041f5207b7SJohn Levon 	return_id++;
18051f5207b7SJohn Levon 	FOR_EACH_PTR(returned_state_callbacks, cb) {
18061f5207b7SJohn Levon 		cb->callback(return_id, return_ranges, expr);
18071f5207b7SJohn Levon 	} END_FOR_EACH_PTR(cb);
18081f5207b7SJohn Levon 
18091f5207b7SJohn Levon 	__push_true_states();
18101f5207b7SJohn Levon 	__use_false_states();
18111f5207b7SJohn Levon 
18121f5207b7SJohn Levon 	return_ranges = alloc_sname("0");
18131f5207b7SJohn Levon 	null_sval = sval_type_val(rl_type(rl), 0);
18141f5207b7SJohn Levon 	add_range(&null_rl, null_sval, null_sval);
18151f5207b7SJohn Levon 	set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
18161f5207b7SJohn Levon 	return_id++;
18171f5207b7SJohn Levon 	FOR_EACH_PTR(returned_state_callbacks, cb) {
18181f5207b7SJohn Levon 		cb->callback(return_id, return_ranges, expr);
18191f5207b7SJohn Levon 	} END_FOR_EACH_PTR(cb);
18201f5207b7SJohn Levon 
18211f5207b7SJohn Levon 	__merge_true_states();
18221f5207b7SJohn Levon 	__free_fake_cur_stree();
18231f5207b7SJohn Levon 
18241f5207b7SJohn Levon 	return 1;
18251f5207b7SJohn Levon }
18261f5207b7SJohn Levon 
is_kernel_success_fail(struct sm_state * sm)18275a0e240fSJohn Levon static bool is_kernel_success_fail(struct sm_state *sm)
18285a0e240fSJohn Levon {
18295a0e240fSJohn Levon 	struct sm_state *tmp;
18305a0e240fSJohn Levon 	struct range_list *rl;
18315a0e240fSJohn Levon 	bool has_zero = false;
18325a0e240fSJohn Levon 	bool has_neg = false;
18335a0e240fSJohn Levon 
18345a0e240fSJohn Levon 	if (!type_signed(estate_type(sm->state)))
18355a0e240fSJohn Levon 		return false;
18365a0e240fSJohn Levon 
18375a0e240fSJohn Levon 	FOR_EACH_PTR(sm->possible, tmp) {
18385a0e240fSJohn Levon 		rl = estate_rl(tmp->state);
18395a0e240fSJohn Levon 		if (!rl)
18405a0e240fSJohn Levon 			return false;
18415a0e240fSJohn Levon 		if (rl_min(rl).value == 0 && rl_max(rl).value == 0) {
18425a0e240fSJohn Levon 			has_zero = true;
18435a0e240fSJohn Levon 			continue;
18445a0e240fSJohn Levon 		}
18455a0e240fSJohn Levon 		has_neg = true;
18465a0e240fSJohn Levon 		if (rl_min(rl).value >= -4095 && rl_max(rl).value < 0)
18475a0e240fSJohn Levon 			continue;
18485a0e240fSJohn Levon 		if (strcmp(tmp->state->name, "s32min-(-1)") == 0)
18495a0e240fSJohn Levon 			continue;
18505a0e240fSJohn Levon 		if (strcmp(tmp->state->name, "s32min-(-1),1-s32max") == 0)
18515a0e240fSJohn Levon 			continue;
18525a0e240fSJohn Levon 		return false;
18535a0e240fSJohn Levon 	} END_FOR_EACH_PTR(tmp);
18545a0e240fSJohn Levon 
18555a0e240fSJohn Levon 	return has_zero && has_neg;
18565a0e240fSJohn Levon }
18575a0e240fSJohn Levon 
call_return_state_hooks_split_success_fail(struct expression * expr)18581f5207b7SJohn Levon static int call_return_state_hooks_split_success_fail(struct expression *expr)
18591f5207b7SJohn Levon {
18601f5207b7SJohn Levon 	struct sm_state *sm;
18611f5207b7SJohn Levon 	struct range_list *rl;
18621f5207b7SJohn Levon 	struct range_list *nonzero_rl;
18631f5207b7SJohn Levon 	sval_t zero_sval;
18641f5207b7SJohn Levon 	struct range_list *zero_rl = NULL;
18651f5207b7SJohn Levon 	int nr_states;
18661f5207b7SJohn Levon 	struct returned_state_callback *cb;
18671f5207b7SJohn Levon 	char *return_ranges;
18681f5207b7SJohn Levon 	int final_pass_orig = final_pass;
18691f5207b7SJohn Levon 
18701f5207b7SJohn Levon 	if (option_project != PROJ_KERNEL)
18711f5207b7SJohn Levon 		return 0;
18721f5207b7SJohn Levon 
18731f5207b7SJohn Levon 	nr_states = get_db_state_count();
18745a0e240fSJohn Levon 	if (nr_states > 2000)
18751f5207b7SJohn Levon 		return 0;
18761f5207b7SJohn Levon 
18771f5207b7SJohn Levon 	sm = get_sm_state_expr(SMATCH_EXTRA, expr);
18781f5207b7SJohn Levon 	if (!sm)
18791f5207b7SJohn Levon 		return 0;
18801f5207b7SJohn Levon 	if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
18811f5207b7SJohn Levon 		return 0;
18825a0e240fSJohn Levon 	if (!is_kernel_success_fail(sm))
18835a0e240fSJohn Levon 		return 0;
18841f5207b7SJohn Levon 
18851f5207b7SJohn Levon 	rl = estate_rl(sm->state);
18861f5207b7SJohn Levon 	if (!rl)
18871f5207b7SJohn Levon 		return 0;
18881f5207b7SJohn Levon 
18891f5207b7SJohn Levon 	__push_fake_cur_stree();
18901f5207b7SJohn Levon 
18911f5207b7SJohn Levon 	final_pass = 0;
18921f5207b7SJohn Levon 	__split_whole_condition(expr);
18931f5207b7SJohn Levon 	final_pass = final_pass_orig;
18941f5207b7SJohn Levon 
18951f5207b7SJohn Levon 	nonzero_rl = rl_filter(rl, rl_zero());
18961f5207b7SJohn Levon 	nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
18971f5207b7SJohn Levon 	return_ranges = show_rl(nonzero_rl);
18981f5207b7SJohn Levon 	set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
18991f5207b7SJohn Levon 
19001f5207b7SJohn Levon 	return_id++;
19011f5207b7SJohn Levon 	FOR_EACH_PTR(returned_state_callbacks, cb) {
19021f5207b7SJohn Levon 		cb->callback(return_id, return_ranges, expr);
19031f5207b7SJohn Levon 	} END_FOR_EACH_PTR(cb);
19041f5207b7SJohn Levon 
19051f5207b7SJohn Levon 	__push_true_states();
19061f5207b7SJohn Levon 	__use_false_states();
19071f5207b7SJohn Levon 
19081f5207b7SJohn Levon 	return_ranges = alloc_sname("0");
19091f5207b7SJohn Levon 	zero_sval = sval_type_val(rl_type(rl), 0);
19101f5207b7SJohn Levon 	add_range(&zero_rl, zero_sval, zero_sval);
19111f5207b7SJohn Levon 	set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
19121f5207b7SJohn Levon 	return_id++;
19131f5207b7SJohn Levon 	FOR_EACH_PTR(returned_state_callbacks, cb) {
19141f5207b7SJohn Levon 		cb->callback(return_id, return_ranges, expr);
19151f5207b7SJohn Levon 	} END_FOR_EACH_PTR(cb);
19161f5207b7SJohn Levon 
19171f5207b7SJohn Levon 	__merge_true_states();
19181f5207b7SJohn Levon 	__free_fake_cur_stree();
19191f5207b7SJohn Levon 
19201f5207b7SJohn Levon 	return 1;
19211f5207b7SJohn Levon }
19221f5207b7SJohn Levon 
is_boolean(struct expression * expr)19231f5207b7SJohn Levon static int is_boolean(struct expression *expr)
19241f5207b7SJohn Levon {
19251f5207b7SJohn Levon 	struct range_list *rl;
19261f5207b7SJohn Levon 
19271f5207b7SJohn Levon 	if (!get_implied_rl(expr, &rl))
19281f5207b7SJohn Levon 		return 0;
19291f5207b7SJohn Levon 	if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
19301f5207b7SJohn Levon 		return 1;
19311f5207b7SJohn Levon 	return 0;
19321f5207b7SJohn Levon }
19331f5207b7SJohn Levon 
splitable_function_call(struct expression * expr)19341f5207b7SJohn Levon static int splitable_function_call(struct expression *expr)
19351f5207b7SJohn Levon {
19361f5207b7SJohn Levon 	struct sm_state *sm;
19371f5207b7SJohn Levon 	char buf[64];
19381f5207b7SJohn Levon 
19391f5207b7SJohn Levon 	if (!expr || expr->type != EXPR_CALL)
19401f5207b7SJohn Levon 		return 0;
19411f5207b7SJohn Levon 	snprintf(buf, sizeof(buf), "return %p", expr);
19421f5207b7SJohn Levon 	sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
19431f5207b7SJohn Levon 	return split_possible_helper(sm, expr);
19441f5207b7SJohn Levon }
19451f5207b7SJohn Levon 
find_bool_param(void)19461f5207b7SJohn Levon static struct sm_state *find_bool_param(void)
19471f5207b7SJohn Levon {
19481f5207b7SJohn Levon 	struct stree *start_states;
19491f5207b7SJohn Levon 	struct symbol *arg;
19501f5207b7SJohn Levon 	struct sm_state *sm, *tmp;
19511f5207b7SJohn Levon 	sval_t sval;
19521f5207b7SJohn Levon 
19531f5207b7SJohn Levon 	start_states = get_start_states();
19541f5207b7SJohn Levon 
19551f5207b7SJohn Levon 	FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
19561f5207b7SJohn Levon 		if (!arg->ident)
19571f5207b7SJohn Levon 			continue;
19581f5207b7SJohn Levon 		sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
19591f5207b7SJohn Levon 		if (!sm)
19601f5207b7SJohn Levon 			continue;
19611f5207b7SJohn Levon 		if (rl_min(estate_rl(sm->state)).value != 0 ||
19621f5207b7SJohn Levon 		    rl_max(estate_rl(sm->state)).value != 1)
19631f5207b7SJohn Levon 			continue;
19641f5207b7SJohn Levon 		goto found;
19651f5207b7SJohn Levon 	} END_FOR_EACH_PTR_REVERSE(arg);
19661f5207b7SJohn Levon 
19671f5207b7SJohn Levon 	return NULL;
19681f5207b7SJohn Levon 
19691f5207b7SJohn Levon found:
19701f5207b7SJohn Levon 	/*
19711f5207b7SJohn Levon 	 * Check if it's splitable.  If not, then splitting it up is likely not
19721f5207b7SJohn Levon 	 * useful for the callers.
19731f5207b7SJohn Levon 	 */
19741f5207b7SJohn Levon 	FOR_EACH_PTR(sm->possible, tmp) {
19751f5207b7SJohn Levon 		if (is_merged(tmp))
19761f5207b7SJohn Levon 			continue;
19771f5207b7SJohn Levon 		if (!estate_get_single_value(tmp->state, &sval))
19781f5207b7SJohn Levon 			return NULL;
19791f5207b7SJohn Levon 	} END_FOR_EACH_PTR(tmp);
19801f5207b7SJohn Levon 
19811f5207b7SJohn Levon 	return sm;
19821f5207b7SJohn Levon }
19831f5207b7SJohn Levon 
split_on_bool_sm(struct sm_state * sm,struct expression * expr)19841f5207b7SJohn Levon static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
19851f5207b7SJohn Levon {
19861f5207b7SJohn Levon 	struct returned_state_callback *cb;
19871f5207b7SJohn Levon 	struct range_list *ret_rl;
19881f5207b7SJohn Levon 	const char *return_ranges;
19891f5207b7SJohn Levon 	struct sm_state *tmp;
19901f5207b7SJohn Levon 	int ret = 0;
19911f5207b7SJohn Levon 	struct state_list *already_handled = NULL;
19921f5207b7SJohn Levon 
19931f5207b7SJohn Levon 	if (!sm || !sm->merged)
19941f5207b7SJohn Levon 		return 0;
19951f5207b7SJohn Levon 
19961f5207b7SJohn Levon 	if (too_many_possible(sm))
19971f5207b7SJohn Levon 		return 0;
19981f5207b7SJohn Levon 
19991f5207b7SJohn Levon 	FOR_EACH_PTR(sm->possible, tmp) {
20001f5207b7SJohn Levon 		if (tmp->merged)
20011f5207b7SJohn Levon 			continue;
20021f5207b7SJohn Levon 		if (ptr_in_list(tmp, already_handled))
20031f5207b7SJohn Levon 			continue;
20041f5207b7SJohn Levon 		add_ptr_list(&already_handled, tmp);
20051f5207b7SJohn Levon 
20061f5207b7SJohn Levon 		ret = 1;
20071f5207b7SJohn Levon 		__push_fake_cur_stree();
20081f5207b7SJohn Levon 
20091f5207b7SJohn Levon 		overwrite_states_using_pool(sm, tmp);
20101f5207b7SJohn Levon 
20111f5207b7SJohn Levon 		return_ranges = get_return_ranges_str(expr, &ret_rl);
20121f5207b7SJohn Levon 		set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
20131f5207b7SJohn Levon 		return_id++;
20141f5207b7SJohn Levon 		FOR_EACH_PTR(returned_state_callbacks, cb) {
20151f5207b7SJohn Levon 			cb->callback(return_id, (char *)return_ranges, expr);
20161f5207b7SJohn Levon 		} END_FOR_EACH_PTR(cb);
20171f5207b7SJohn Levon 
20181f5207b7SJohn Levon 		__free_fake_cur_stree();
20191f5207b7SJohn Levon 	} END_FOR_EACH_PTR(tmp);
20201f5207b7SJohn Levon 
20211f5207b7SJohn Levon 	free_slist(&already_handled);
20221f5207b7SJohn Levon 
20231f5207b7SJohn Levon 	return ret;
20241f5207b7SJohn Levon }
20251f5207b7SJohn Levon 
split_by_bool_param(struct expression * expr)20261f5207b7SJohn Levon static int split_by_bool_param(struct expression *expr)
20271f5207b7SJohn Levon {
20281f5207b7SJohn Levon 	struct sm_state *start_sm, *sm;
20291f5207b7SJohn Levon 	sval_t sval;
20301f5207b7SJohn Levon 
20311f5207b7SJohn Levon 	start_sm = find_bool_param();
20321f5207b7SJohn Levon 	if (!start_sm)
20331f5207b7SJohn Levon 		return 0;
20341f5207b7SJohn Levon 	sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
20351f5207b7SJohn Levon 	if (!sm || estate_get_single_value(sm->state, &sval))
20361f5207b7SJohn Levon 		return 0;
20375a0e240fSJohn Levon 
20385a0e240fSJohn Levon 	if (get_db_state_count() * 2 >= 2000)
20395a0e240fSJohn Levon 		return 0;
20405a0e240fSJohn Levon 
20411f5207b7SJohn Levon 	return split_on_bool_sm(sm, expr);
20421f5207b7SJohn Levon }
20431f5207b7SJohn Levon 
split_by_null_nonnull_param(struct expression * expr)20441f5207b7SJohn Levon static int split_by_null_nonnull_param(struct expression *expr)
20451f5207b7SJohn Levon {
20461f5207b7SJohn Levon 	struct symbol *arg;
20471f5207b7SJohn Levon 	struct sm_state *sm;
20485a0e240fSJohn Levon 	int nr_possible;
20491f5207b7SJohn Levon 
20501f5207b7SJohn Levon 	/* function must only take one pointer */
20511f5207b7SJohn Levon 	if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
20521f5207b7SJohn Levon 		return 0;
20531f5207b7SJohn Levon 	arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
20541f5207b7SJohn Levon 	if (!arg->ident)
20551f5207b7SJohn Levon 		return 0;
20561f5207b7SJohn Levon 	if (get_real_base_type(arg)->type != SYM_PTR)
20571f5207b7SJohn Levon 		return 0;
20581f5207b7SJohn Levon 
20591f5207b7SJohn Levon 	if (param_was_set_var_sym(arg->ident->name, arg))
20601f5207b7SJohn Levon 		return 0;
20611f5207b7SJohn Levon 	sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
20621f5207b7SJohn Levon 	if (!sm)
20631f5207b7SJohn Levon 		return 0;
20641f5207b7SJohn Levon 
20655a0e240fSJohn Levon 	if (!has_separate_zero_null(sm))
20665a0e240fSJohn Levon 		return 0;
20675a0e240fSJohn Levon 
20685a0e240fSJohn Levon 	nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
20695a0e240fSJohn Levon 	if (get_db_state_count() * nr_possible >= 2000)
20701f5207b7SJohn Levon 		return 0;
20711f5207b7SJohn Levon 
20721f5207b7SJohn Levon 	return split_on_bool_sm(sm, expr);
20731f5207b7SJohn Levon }
20741f5207b7SJohn Levon 
strip_expr_statement(struct expression * expr)20751f5207b7SJohn Levon struct expression *strip_expr_statement(struct expression *expr)
20761f5207b7SJohn Levon {
20771f5207b7SJohn Levon 	struct expression *orig = expr;
20781f5207b7SJohn Levon 	struct statement *stmt, *last_stmt;
20791f5207b7SJohn Levon 
20801f5207b7SJohn Levon 	if (!expr)
20811f5207b7SJohn Levon 		return NULL;
20821f5207b7SJohn Levon 	if (expr->type == EXPR_PREOP && expr->op == '(')
20831f5207b7SJohn Levon 		expr = expr->unop;
20841f5207b7SJohn Levon 	if (expr->type != EXPR_STATEMENT)
20851f5207b7SJohn Levon 		return orig;
20861f5207b7SJohn Levon 	stmt = expr->statement;
20871f5207b7SJohn Levon 	if (!stmt || stmt->type != STMT_COMPOUND)
20881f5207b7SJohn Levon 		return orig;
20891f5207b7SJohn Levon 
20901f5207b7SJohn Levon 	last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
20911f5207b7SJohn Levon 	if (!last_stmt || last_stmt->type == STMT_LABEL)
20921f5207b7SJohn Levon 		last_stmt = last_stmt->label_statement;
20931f5207b7SJohn Levon 	if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
20941f5207b7SJohn Levon 		return orig;
20951f5207b7SJohn Levon 	return strip_expr(last_stmt->expression);
20961f5207b7SJohn Levon }
20971f5207b7SJohn Levon 
is_kernel_error_path(struct expression * expr)20986523a3aaSJohn Levon static bool is_kernel_error_path(struct expression *expr)
20996523a3aaSJohn Levon {
21006523a3aaSJohn Levon 	struct range_list *rl;
21016523a3aaSJohn Levon 
21026523a3aaSJohn Levon 	/*
21036523a3aaSJohn Levon 	 * Splitting up returns requires resources.  It also requires resources
21046523a3aaSJohn Levon 	 * for the caller.  It doesn't seem worth it to split anything up.
21056523a3aaSJohn Levon 	 */
21066523a3aaSJohn Levon 	if (!get_implied_rl(expr, &rl))
21076523a3aaSJohn Levon 		return false;
21086523a3aaSJohn Levon 	if (rl_type(rl) != &int_ctype)
21096523a3aaSJohn Levon 		return false;
21106523a3aaSJohn Levon 	if (rl_min(rl).value >= -4095 &&
21116523a3aaSJohn Levon 	    rl_max(rl).value < 0)
21126523a3aaSJohn Levon 		return true;
21136523a3aaSJohn Levon 	return false;
21146523a3aaSJohn Levon }
21156523a3aaSJohn Levon 
call_return_state_hooks(struct expression * expr)21161f5207b7SJohn Levon static void call_return_state_hooks(struct expression *expr)
21171f5207b7SJohn Levon {
21181f5207b7SJohn Levon 	struct returned_state_callback *cb;
21191f5207b7SJohn Levon 	struct range_list *ret_rl;
21201f5207b7SJohn Levon 	const char *return_ranges;
21211f5207b7SJohn Levon 	int nr_states;
21221f5207b7SJohn Levon 	sval_t sval;
21231f5207b7SJohn Levon 
21241f5207b7SJohn Levon 	if (__path_is_null())
21251f5207b7SJohn Levon 		return;
21261f5207b7SJohn Levon 
21271f5207b7SJohn Levon 	expr = strip_expr(expr);
21281f5207b7SJohn Levon 	expr = strip_expr_statement(expr);
21291f5207b7SJohn Levon 
21301f5207b7SJohn Levon 	if (is_impossible_path())
21311f5207b7SJohn Levon 		goto vanilla;
21321f5207b7SJohn Levon 
21331f5207b7SJohn Levon 	if (expr && (expr->type == EXPR_COMPARE ||
21341f5207b7SJohn Levon 		     !get_implied_value(expr, &sval)) &&
21351f5207b7SJohn Levon 	    (is_condition(expr) || is_boolean(expr))) {
21361f5207b7SJohn Levon 		call_return_state_hooks_compare(expr);
21371f5207b7SJohn Levon 		return;
21385a0e240fSJohn Levon 	} else if (call_return_state_hooks_conditional(expr)) {
21391f5207b7SJohn Levon 		return;
21406523a3aaSJohn Levon 	} else if (is_kernel_error_path(expr)) {
21416523a3aaSJohn Levon 		goto vanilla;
21421f5207b7SJohn Levon 	} else if (call_return_state_hooks_split_possible(expr)) {
21431f5207b7SJohn Levon 		return;
2144efe51d0cSJohn Levon 	} else if (split_positive_from_negative(expr)) {
2145efe51d0cSJohn Levon 		return;
2146efe51d0cSJohn Levon 	} else if (call_return_state_hooks_split_null_non_null_zero(expr)) {
21471f5207b7SJohn Levon 		return;
21481f5207b7SJohn Levon 	} else if (call_return_state_hooks_split_success_fail(expr)) {
21491f5207b7SJohn Levon 		return;
21501f5207b7SJohn Levon 	} else if (splitable_function_call(expr)) {
21511f5207b7SJohn Levon 		return;
21521f5207b7SJohn Levon 	} else if (split_by_bool_param(expr)) {
21531f5207b7SJohn Levon 	} else if (split_by_null_nonnull_param(expr)) {
21541f5207b7SJohn Levon 		return;
21551f5207b7SJohn Levon 	}
21561f5207b7SJohn Levon 
21571f5207b7SJohn Levon vanilla:
21581f5207b7SJohn Levon 	return_ranges = get_return_ranges_str(expr, &ret_rl);
21591f5207b7SJohn Levon 	set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
21601f5207b7SJohn Levon 
21611f5207b7SJohn Levon 	return_id++;
21621f5207b7SJohn Levon 	nr_states = get_db_state_count();
21631f5207b7SJohn Levon 	if (nr_states >= 10000) {
21641f5207b7SJohn Levon 		match_return_info(return_id, (char *)return_ranges, expr);
2165c85f09ccSJohn Levon 		print_limited_param_set(return_id, (char *)return_ranges, expr);
21661f5207b7SJohn Levon 		mark_all_params_untracked(return_id, (char *)return_ranges, expr);
21671f5207b7SJohn Levon 		return;
21681f5207b7SJohn Levon 	}
21691f5207b7SJohn Levon 	FOR_EACH_PTR(returned_state_callbacks, cb) {
21701f5207b7SJohn Levon 		cb->callback(return_id, (char *)return_ranges, expr);
21711f5207b7SJohn Levon 	} END_FOR_EACH_PTR(cb);
21721f5207b7SJohn Levon }
21731f5207b7SJohn Levon 
print_returned_struct_members(int return_id,char * return_ranges,struct expression * expr)21741f5207b7SJohn Levon static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
21751f5207b7SJohn Levon {
21761f5207b7SJohn Levon 	struct returned_member_callback *cb;
21771f5207b7SJohn Levon 	struct stree *stree;
21781f5207b7SJohn Levon 	struct sm_state *sm;
21791f5207b7SJohn Levon 	struct symbol *type;
21801f5207b7SJohn Levon 	char *name;
21811f5207b7SJohn Levon 	char member_name[256];
21821f5207b7SJohn Levon 	int len;
21831f5207b7SJohn Levon 
21841f5207b7SJohn Levon 	type = get_type(expr);
21851f5207b7SJohn Levon 	if (!type || type->type != SYM_PTR)
21861f5207b7SJohn Levon 		return;
21871f5207b7SJohn Levon 	name = expr_to_var(expr);
21881f5207b7SJohn Levon 	if (!name)
21891f5207b7SJohn Levon 		return;
21901f5207b7SJohn Levon 
21911f5207b7SJohn Levon 	member_name[sizeof(member_name) - 1] = '\0';
21921f5207b7SJohn Levon 	strcpy(member_name, "$");
21931f5207b7SJohn Levon 
21941f5207b7SJohn Levon 	len = strlen(name);
21951f5207b7SJohn Levon 	FOR_EACH_PTR(returned_member_callbacks, cb) {
21961f5207b7SJohn Levon 		stree = __get_cur_stree();
21971f5207b7SJohn Levon 		FOR_EACH_MY_SM(cb->owner, stree, sm) {
21981f5207b7SJohn Levon 			if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
21991f5207b7SJohn Levon 				strcpy(member_name, "*$");
22001f5207b7SJohn Levon 				cb->callback(return_id, return_ranges, expr, member_name, sm->state);
22011f5207b7SJohn Levon 				continue;
22021f5207b7SJohn Levon 			}
22031f5207b7SJohn Levon 			if (strncmp(sm->name, name, len) != 0)
22041f5207b7SJohn Levon 				continue;
22051f5207b7SJohn Levon 			if (strncmp(sm->name + len, "->", 2) != 0)
22061f5207b7SJohn Levon 				continue;
22071f5207b7SJohn Levon 			snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
22081f5207b7SJohn Levon 			cb->callback(return_id, return_ranges, expr, member_name, sm->state);
22091f5207b7SJohn Levon 		} END_FOR_EACH_SM(sm);
22101f5207b7SJohn Levon 	} END_FOR_EACH_PTR(cb);
22111f5207b7SJohn Levon 
22121f5207b7SJohn Levon 	free_string(name);
22131f5207b7SJohn Levon }
22141f5207b7SJohn Levon 
reset_memdb(struct symbol * sym)22151f5207b7SJohn Levon static void reset_memdb(struct symbol *sym)
22161f5207b7SJohn Levon {
22171f5207b7SJohn Levon 	mem_sql(NULL, NULL, "delete from caller_info;");
22181f5207b7SJohn Levon 	mem_sql(NULL, NULL, "delete from return_states;");
22191f5207b7SJohn Levon 	mem_sql(NULL, NULL, "delete from call_implies;");
22201f5207b7SJohn Levon 	mem_sql(NULL, NULL, "delete from return_implies;");
22211f5207b7SJohn Levon }
22221f5207b7SJohn Levon 
match_end_func_info(struct symbol * sym)22231f5207b7SJohn Levon static void match_end_func_info(struct symbol *sym)
22241f5207b7SJohn Levon {
22251f5207b7SJohn Levon 	if (__path_is_null())
22261f5207b7SJohn Levon 		return;
22271f5207b7SJohn Levon 	call_return_state_hooks(NULL);
22281f5207b7SJohn Levon }
22291f5207b7SJohn Levon 
match_after_func(struct symbol * sym)22301f5207b7SJohn Levon static void match_after_func(struct symbol *sym)
22311f5207b7SJohn Levon {
22321f5207b7SJohn Levon 	if (!__inline_fn)
22331f5207b7SJohn Levon 		reset_memdb(sym);
22341f5207b7SJohn Levon }
22351f5207b7SJohn Levon 
init_memdb(void)22361f5207b7SJohn Levon static void init_memdb(void)
22371f5207b7SJohn Levon {
22381f5207b7SJohn Levon 	char *err = NULL;
22391f5207b7SJohn Levon 	int rc;
22401f5207b7SJohn Levon 	const char *schema_files[] = {
22411f5207b7SJohn Levon 		"db/db.schema",
22421f5207b7SJohn Levon 		"db/caller_info.schema",
22431f5207b7SJohn Levon 		"db/common_caller_info.schema",
22441f5207b7SJohn Levon 		"db/return_states.schema",
22451f5207b7SJohn Levon 		"db/function_type_size.schema",
22461f5207b7SJohn Levon 		"db/type_size.schema",
22471f5207b7SJohn Levon 		"db/function_type_info.schema",
22481f5207b7SJohn Levon 		"db/type_info.schema",
22491f5207b7SJohn Levon 		"db/call_implies.schema",
22501f5207b7SJohn Levon 		"db/return_implies.schema",
22511f5207b7SJohn Levon 		"db/function_ptr.schema",
22521f5207b7SJohn Levon 		"db/local_values.schema",
22531f5207b7SJohn Levon 		"db/function_type_value.schema",
22541f5207b7SJohn Levon 		"db/type_value.schema",
22551f5207b7SJohn Levon 		"db/function_type.schema",
22561f5207b7SJohn Levon 		"db/data_info.schema",
22571f5207b7SJohn Levon 		"db/parameter_name.schema",
22581f5207b7SJohn Levon 		"db/constraints.schema",
22591f5207b7SJohn Levon 		"db/constraints_required.schema",
22601f5207b7SJohn Levon 		"db/fn_ptr_data_link.schema",
22611f5207b7SJohn Levon 		"db/fn_data_link.schema",
22621f5207b7SJohn Levon 		"db/mtag_about.schema",
22636523a3aaSJohn Levon 		"db/mtag_info.schema",
22641f5207b7SJohn Levon 		"db/mtag_map.schema",
22651f5207b7SJohn Levon 		"db/mtag_data.schema",
22661f5207b7SJohn Levon 		"db/mtag_alias.schema",
22671f5207b7SJohn Levon 	};
22681f5207b7SJohn Levon 	static char buf[4096];
22691f5207b7SJohn Levon 	int fd;
22701f5207b7SJohn Levon 	int ret;
22711f5207b7SJohn Levon 	int i;
22721f5207b7SJohn Levon 
22731f5207b7SJohn Levon 	rc = sqlite3_open(":memory:", &mem_db);
22741f5207b7SJohn Levon 	if (rc != SQLITE_OK) {
22751f5207b7SJohn Levon 		sm_ierror("starting In-Memory database.");
22761f5207b7SJohn Levon 		return;
22771f5207b7SJohn Levon 	}
22781f5207b7SJohn Levon 
22791f5207b7SJohn Levon 	for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
22801f5207b7SJohn Levon 		fd = open_schema_file(schema_files[i]);
22811f5207b7SJohn Levon 		if (fd < 0)
22821f5207b7SJohn Levon 			continue;
22831f5207b7SJohn Levon 		ret = read(fd, buf, sizeof(buf));
22841f5207b7SJohn Levon 		if (ret < 0) {
22851f5207b7SJohn Levon 			sm_ierror("failed to read: %s", schema_files[i]);
22861f5207b7SJohn Levon 			continue;
22871f5207b7SJohn Levon 		}
22881f5207b7SJohn Levon 		close(fd);
22891f5207b7SJohn Levon 		if (ret == sizeof(buf)) {
22901f5207b7SJohn Levon 			sm_ierror("Schema file too large:  %s (limit %zd bytes)",
22911f5207b7SJohn Levon 			       schema_files[i], sizeof(buf));
22921f5207b7SJohn Levon 			continue;
22931f5207b7SJohn Levon 		}
22941f5207b7SJohn Levon 		buf[ret] = '\0';
22951f5207b7SJohn Levon 		rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
22961f5207b7SJohn Levon 		if (rc != SQLITE_OK) {
22971f5207b7SJohn Levon 			sm_ierror("SQL error #2: %s", err);
22981f5207b7SJohn Levon 			sm_ierror("%s", buf);
22991f5207b7SJohn Levon 		}
23001f5207b7SJohn Levon 	}
23011f5207b7SJohn Levon }
23021f5207b7SJohn Levon 
init_cachedb(void)23031f5207b7SJohn Levon static void init_cachedb(void)
23041f5207b7SJohn Levon {
23051f5207b7SJohn Levon 	char *err = NULL;
23061f5207b7SJohn Levon 	int rc;
23071f5207b7SJohn Levon 	const char *schema_files[] = {
23081f5207b7SJohn Levon 		"db/call_implies.schema",
23091f5207b7SJohn Levon 		"db/return_implies.schema",
23101f5207b7SJohn Levon 		"db/type_info.schema",
23116523a3aaSJohn Levon 		"db/mtag_about.schema",
23121f5207b7SJohn Levon 		"db/mtag_data.schema",
23136523a3aaSJohn Levon 		"db/mtag_info.schema",
23141f5207b7SJohn Levon 		"db/sink_info.schema",
23151f5207b7SJohn Levon 	};
23161f5207b7SJohn Levon 	static char buf[4096];
23171f5207b7SJohn Levon 	int fd;
23181f5207b7SJohn Levon 	int ret;
23191f5207b7SJohn Levon 	int i;
23201f5207b7SJohn Levon 
23211f5207b7SJohn Levon 	rc = sqlite3_open(":memory:", &cache_db);
23221f5207b7SJohn Levon 	if (rc != SQLITE_OK) {
23231f5207b7SJohn Levon 		sm_ierror("starting In-Memory database.");
23241f5207b7SJohn Levon 		return;
23251f5207b7SJohn Levon 	}
23261f5207b7SJohn Levon 
23271f5207b7SJohn Levon 	for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
23281f5207b7SJohn Levon 		fd = open_schema_file(schema_files[i]);
23291f5207b7SJohn Levon 		if (fd < 0)
23301f5207b7SJohn Levon 			continue;
23311f5207b7SJohn Levon 		ret = read(fd, buf, sizeof(buf));
23321f5207b7SJohn Levon 		if (ret < 0) {
23331f5207b7SJohn Levon 			sm_ierror("failed to read: %s", schema_files[i]);
23341f5207b7SJohn Levon 			continue;
23351f5207b7SJohn Levon 		}
23361f5207b7SJohn Levon 		close(fd);
23371f5207b7SJohn Levon 		if (ret == sizeof(buf)) {
23381f5207b7SJohn Levon 			sm_ierror("Schema file too large:  %s (limit %zd bytes)",
23391f5207b7SJohn Levon 			       schema_files[i], sizeof(buf));
23401f5207b7SJohn Levon 			continue;
23411f5207b7SJohn Levon 		}
23421f5207b7SJohn Levon 		buf[ret] = '\0';
23431f5207b7SJohn Levon 		rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
23441f5207b7SJohn Levon 		if (rc != SQLITE_OK) {
23451f5207b7SJohn Levon 			sm_ierror("SQL error #2: %s", err);
23461f5207b7SJohn Levon 			sm_ierror("%s", buf);
23471f5207b7SJohn Levon 		}
23481f5207b7SJohn Levon 	}
23491f5207b7SJohn Levon }
23501f5207b7SJohn Levon 
save_cache_data(void * _table,int argc,char ** argv,char ** azColName)23511f5207b7SJohn Levon static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
23521f5207b7SJohn Levon {
23531f5207b7SJohn Levon 	static char buf[4096];
23541f5207b7SJohn Levon 	char tmp[256];
23551f5207b7SJohn Levon 	char *p = buf;
23561f5207b7SJohn Levon 	char *table = _table;
23571f5207b7SJohn Levon 	int i;
23581f5207b7SJohn Levon 
23591f5207b7SJohn Levon 
23601f5207b7SJohn Levon 	p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
23611f5207b7SJohn Levon 	for (i = 0; i < argc; i++) {
23621f5207b7SJohn Levon 		if (i)
23631f5207b7SJohn Levon 			p += snprintf(p, 4096 - (p - buf), ", ");
2364efe51d0cSJohn Levon 		sqlite3_snprintf(sizeof(tmp), tmp, "%q", escape_newlines(argv[i]));
23651f5207b7SJohn Levon 		p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
23661f5207b7SJohn Levon 
23671f5207b7SJohn Levon 	}
23681f5207b7SJohn Levon 	p += snprintf(p, 4096 - (p - buf), ");");
23691f5207b7SJohn Levon 	if (p - buf > 4096)
23701f5207b7SJohn Levon 		return 0;
23711f5207b7SJohn Levon 
23721f5207b7SJohn Levon 	sm_msg("SQL: %s", buf);
23731f5207b7SJohn Levon 	return 0;
23741f5207b7SJohn Levon }
23751f5207b7SJohn Levon 
dump_cache(struct symbol_list * sym_list)23761f5207b7SJohn Levon static void dump_cache(struct symbol_list *sym_list)
23771f5207b7SJohn Levon {
23786523a3aaSJohn Levon 	const char *cache_tables[] = {
23796523a3aaSJohn Levon 		"type_info", "return_implies", "call_implies", "mtag_data",
23806523a3aaSJohn Levon 		"mtag_info", "mtag_about", "sink_info",
23816523a3aaSJohn Levon 	};
23826523a3aaSJohn Levon 	char buf[64];
23836523a3aaSJohn Levon 	int i;
23846523a3aaSJohn Levon 
23851f5207b7SJohn Levon 	if (!option_info)
23861f5207b7SJohn Levon 		return;
23876523a3aaSJohn Levon 
23886523a3aaSJohn Levon 	for (i = 0; i < ARRAY_SIZE(cache_tables); i++) {
23896523a3aaSJohn Levon 		snprintf(buf, sizeof(buf), "select * from %s;", cache_tables[i]);
23906523a3aaSJohn Levon 		cache_sql(&save_cache_data, (char *)cache_tables[i], buf);
23916523a3aaSJohn Levon 	}
23921f5207b7SJohn Levon }
23931f5207b7SJohn Levon 
open_smatch_db(char * db_file)23941f5207b7SJohn Levon void open_smatch_db(char *db_file)
23951f5207b7SJohn Levon {
23961f5207b7SJohn Levon 	int rc;
23971f5207b7SJohn Levon 
23981f5207b7SJohn Levon 	if (option_no_db)
23991f5207b7SJohn Levon 		return;
24001f5207b7SJohn Levon 
24011f5207b7SJohn Levon 	use_states = malloc(num_checks + 1);
24021f5207b7SJohn Levon 	memset(use_states, 0xff, num_checks + 1);
24031f5207b7SJohn Levon 
24041f5207b7SJohn Levon 	init_memdb();
24051f5207b7SJohn Levon 	init_cachedb();
24061f5207b7SJohn Levon 
24071f5207b7SJohn Levon 	rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
24081f5207b7SJohn Levon 	if (rc != SQLITE_OK) {
24091f5207b7SJohn Levon 		option_no_db = 1;
24101f5207b7SJohn Levon 		return;
24111f5207b7SJohn Levon 	}
24121f5207b7SJohn Levon 	run_sql(NULL, NULL,
24131f5207b7SJohn Levon 		"PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
24141f5207b7SJohn Levon 	return;
24151f5207b7SJohn Levon }
24161f5207b7SJohn Levon 
register_common_funcs(void)24171f5207b7SJohn Levon static void register_common_funcs(void)
24181f5207b7SJohn Levon {
24191f5207b7SJohn Levon 	struct token *token;
24201f5207b7SJohn Levon 	char *func;
24211f5207b7SJohn Levon 	char filename[256];
24221f5207b7SJohn Levon 
24231f5207b7SJohn Levon 	if (option_project == PROJ_NONE)
24241f5207b7SJohn Levon 		strcpy(filename, "common_functions");
24251f5207b7SJohn Levon 	else
24261f5207b7SJohn Levon 		snprintf(filename, 256, "%s.common_functions", option_project_str);
24271f5207b7SJohn Levon 
24281f5207b7SJohn Levon 	token = get_tokens_file(filename);
24291f5207b7SJohn Levon 	if (!token)
24301f5207b7SJohn Levon 		return;
24311f5207b7SJohn Levon 	if (token_type(token) != TOKEN_STREAMBEGIN)
24321f5207b7SJohn Levon 		return;
24331f5207b7SJohn Levon 	token = token->next;
24341f5207b7SJohn Levon 	while (token_type(token) != TOKEN_STREAMEND) {
24351f5207b7SJohn Levon 		if (token_type(token) != TOKEN_IDENT)
24361f5207b7SJohn Levon 			return;
24371f5207b7SJohn Levon 		func = alloc_string(show_ident(token->ident));
24381f5207b7SJohn Levon 		add_ptr_list(&common_funcs, func);
24391f5207b7SJohn Levon 		token = token->next;
24401f5207b7SJohn Levon 	}
24411f5207b7SJohn Levon 	clear_token_alloc();
24421f5207b7SJohn Levon }
24431f5207b7SJohn Levon 
get_next_string(char ** str)24441f5207b7SJohn Levon static char *get_next_string(char **str)
24451f5207b7SJohn Levon {
24461f5207b7SJohn Levon 	static char string[256];
24471f5207b7SJohn Levon 	char *start;
24481f5207b7SJohn Levon 	char *p = *str;
2449c85f09ccSJohn Levon 	int len, i, j;
24501f5207b7SJohn Levon 
24511f5207b7SJohn Levon 	if (*p == '\0')
24521f5207b7SJohn Levon 		return NULL;
24531f5207b7SJohn Levon 	start = p;
24541f5207b7SJohn Levon 
2455c85f09ccSJohn Levon 	while (*p != '\0' && *p != '\n') {
2456c85f09ccSJohn Levon 		if (*p == '\\' && *(p + 1) == ' ') {
2457c85f09ccSJohn Levon 			p += 2;
2458c85f09ccSJohn Levon 			continue;
2459c85f09ccSJohn Levon 		}
2460c85f09ccSJohn Levon 		if (*p == ' ')
2461c85f09ccSJohn Levon 			break;
24621f5207b7SJohn Levon 		p++;
2463c85f09ccSJohn Levon 	}
24641f5207b7SJohn Levon 
24651f5207b7SJohn Levon 	len = p - start;
2466c85f09ccSJohn Levon 	if (len >= sizeof(string)) {
2467c85f09ccSJohn Levon 		memcpy(string, start, sizeof(string));
2468c85f09ccSJohn Levon 		string[sizeof(string) - 1] = '\0';
24691f5207b7SJohn Levon 		sm_ierror("return_fix: '%s' too long", string);
24701f5207b7SJohn Levon 		**str = '\0';
24711f5207b7SJohn Levon 		return NULL;
24721f5207b7SJohn Levon 	}
24731f5207b7SJohn Levon 	memcpy(string, start, len);
24741f5207b7SJohn Levon 	string[len] = '\0';
2475c85f09ccSJohn Levon 	for (i = 0; i < sizeof(string) - 1; i++) {
2476c85f09ccSJohn Levon 		if (string[i] == '\\' && string[i + 1] == ' ') {
2477c85f09ccSJohn Levon 			for (j = i; string[j] != '\0'; j++)
2478c85f09ccSJohn Levon 				string[j] = string[j + 1];
2479c85f09ccSJohn Levon 		}
2480c85f09ccSJohn Levon 	}
24811f5207b7SJohn Levon 	if (*p != '\0')
24821f5207b7SJohn Levon 		p++;
24831f5207b7SJohn Levon 	*str = p;
24841f5207b7SJohn Levon 	return string;
24851f5207b7SJohn Levon }
24861f5207b7SJohn Levon 
register_return_replacements(void)24871f5207b7SJohn Levon static void register_return_replacements(void)
24881f5207b7SJohn Levon {
24891f5207b7SJohn Levon 	char *func, *orig, *new;
24901f5207b7SJohn Levon 	char filename[256];
24911f5207b7SJohn Levon 	char buf[4096];
24921f5207b7SJohn Levon 	int fd, ret, i;
24931f5207b7SJohn Levon 	char *p;
24941f5207b7SJohn Levon 
24951f5207b7SJohn Levon 	snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
24961f5207b7SJohn Levon 	fd = open_schema_file(filename);
24971f5207b7SJohn Levon 	if (fd < 0)
24981f5207b7SJohn Levon 		return;
24991f5207b7SJohn Levon 	ret = read(fd, buf, sizeof(buf));
25001f5207b7SJohn Levon 	close(fd);
25011f5207b7SJohn Levon 	if (ret < 0)
25021f5207b7SJohn Levon 		return;
25031f5207b7SJohn Levon 	if (ret == sizeof(buf)) {
25041f5207b7SJohn Levon 		sm_ierror("file too large:  %s (limit %zd bytes)",
25051f5207b7SJohn Levon 		       filename, sizeof(buf));
25061f5207b7SJohn Levon 		return;
25071f5207b7SJohn Levon 	}
25081f5207b7SJohn Levon 	buf[ret] = '\0';
25091f5207b7SJohn Levon 
25101f5207b7SJohn Levon 	p = buf;
25111f5207b7SJohn Levon 	while (*p) {
25121f5207b7SJohn Levon 		get_next_string(&p);
25131f5207b7SJohn Levon 		replace_count++;
25141f5207b7SJohn Levon 	}
25151f5207b7SJohn Levon 	if (replace_count == 0 || replace_count % 3 != 0) {
25161f5207b7SJohn Levon 		replace_count = 0;
25171f5207b7SJohn Levon 		return;
25181f5207b7SJohn Levon 	}
25191f5207b7SJohn Levon 	replace_table = malloc(replace_count * sizeof(char *));
25201f5207b7SJohn Levon 
25211f5207b7SJohn Levon 	p = buf;
25221f5207b7SJohn Levon 	i = 0;
25231f5207b7SJohn Levon 	while (*p) {
25241f5207b7SJohn Levon 		func = alloc_string(get_next_string(&p));
25251f5207b7SJohn Levon 		orig = alloc_string(get_next_string(&p));
25261f5207b7SJohn Levon 		new  = alloc_string(get_next_string(&p));
25271f5207b7SJohn Levon 
25281f5207b7SJohn Levon 		replace_table[i++] = func;
25291f5207b7SJohn Levon 		replace_table[i++] = orig;
25301f5207b7SJohn Levon 		replace_table[i++] = new;
25311f5207b7SJohn Levon 	}
25321f5207b7SJohn Levon }
25331f5207b7SJohn Levon 
register_definition_db_callbacks(int id)25341f5207b7SJohn Levon void register_definition_db_callbacks(int id)
25351f5207b7SJohn Levon {
25361f5207b7SJohn Levon 	add_hook(&match_call_info, FUNCTION_CALL_HOOK);
25376523a3aaSJohn Levon 	add_hook(&match_call_info_new, FUNCTION_CALL_HOOK);
25381f5207b7SJohn Levon 	add_split_return_callback(match_return_info);
25391f5207b7SJohn Levon 	add_split_return_callback(print_returned_struct_members);
25401f5207b7SJohn Levon 	add_hook(&call_return_state_hooks, RETURN_HOOK);
25411f5207b7SJohn Levon 	add_hook(&match_end_func_info, END_FUNC_HOOK);
25421f5207b7SJohn Levon 	add_hook(&match_after_func, AFTER_FUNC_HOOK);
25431f5207b7SJohn Levon 
25441f5207b7SJohn Levon 	add_hook(&match_data_from_db, FUNC_DEF_HOOK);
25451f5207b7SJohn Levon 	add_hook(&match_call_implies, FUNC_DEF_HOOK);
25461f5207b7SJohn Levon 	add_hook(&match_return_implies, CALL_HOOK_AFTER_INLINE);
25471f5207b7SJohn Levon 
25481f5207b7SJohn Levon 	register_common_funcs();
25491f5207b7SJohn Levon 	register_return_replacements();
25501f5207b7SJohn Levon 
25511f5207b7SJohn Levon 	add_hook(&dump_cache, END_FILE_HOOK);
25521f5207b7SJohn Levon }
25531f5207b7SJohn Levon 
register_db_call_marker(int id)25541f5207b7SJohn Levon void register_db_call_marker(int id)
25551f5207b7SJohn Levon {
25561f5207b7SJohn Levon 	add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
25571f5207b7SJohn Levon }
25581f5207b7SJohn Levon 
return_state_to_var_sym(struct expression * expr,int param,const char * key,struct symbol ** sym)25591f5207b7SJohn Levon char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
25601f5207b7SJohn Levon {
25611f5207b7SJohn Levon 	struct expression *arg;
25621f5207b7SJohn Levon 	char *name = NULL;
25631f5207b7SJohn Levon 	char member_name[256];
25641f5207b7SJohn Levon 
25651f5207b7SJohn Levon 	*sym = NULL;
25661f5207b7SJohn Levon 
25671f5207b7SJohn Levon 	if (param == -1) {
25681f5207b7SJohn Levon 		const char *star = "";
25691f5207b7SJohn Levon 
25701f5207b7SJohn Levon 		if (expr->type != EXPR_ASSIGNMENT)
25711f5207b7SJohn Levon 			return NULL;
2572efe51d0cSJohn Levon 		if (get_type(expr->left) == &int_ctype && strcmp(key, "$") != 0)
2573efe51d0cSJohn Levon 			return NULL;
25741f5207b7SJohn Levon 		name = expr_to_var_sym(expr->left, sym);
25751f5207b7SJohn Levon 		if (!name)
25761f5207b7SJohn Levon 			return NULL;
25771f5207b7SJohn Levon 		if (key[0] == '*') {
25781f5207b7SJohn Levon 			star = "*";
25791f5207b7SJohn Levon 			key++;
25801f5207b7SJohn Levon 		}
25811f5207b7SJohn Levon 		if (strncmp(key, "$", 1) != 0)
25821f5207b7SJohn Levon 			return name;
25831f5207b7SJohn Levon 		snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
25841f5207b7SJohn Levon 		free_string(name);
25851f5207b7SJohn Levon 		return alloc_string(member_name);
25861f5207b7SJohn Levon 	}
25871f5207b7SJohn Levon 
25881f5207b7SJohn Levon 	while (expr->type == EXPR_ASSIGNMENT)
25891f5207b7SJohn Levon 		expr = strip_expr(expr->right);
25901f5207b7SJohn Levon 	if (expr->type != EXPR_CALL)
25911f5207b7SJohn Levon 		return NULL;
25921f5207b7SJohn Levon 
25931f5207b7SJohn Levon 	arg = get_argument_from_call_expr(expr->args, param);
25941f5207b7SJohn Levon 	if (!arg)
25951f5207b7SJohn Levon 		return NULL;
25961f5207b7SJohn Levon 
25971f5207b7SJohn Levon 	return get_variable_from_key(arg, key, sym);
25981f5207b7SJohn Levon }
25991f5207b7SJohn Levon 
get_variable_from_key(struct expression * arg,const char * key,struct symbol ** sym)26001f5207b7SJohn Levon char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
26011f5207b7SJohn Levon {
26026523a3aaSJohn Levon 	struct symbol *type;
26031f5207b7SJohn Levon 	char buf[256];
26041f5207b7SJohn Levon 	char *tmp;
26055a0e240fSJohn Levon 	int star_cnt = 0;
26066523a3aaSJohn Levon 	bool add_dot = false;
26071f5207b7SJohn Levon 
26081f5207b7SJohn Levon 	if (!arg)
26091f5207b7SJohn Levon 		return NULL;
26101f5207b7SJohn Levon 
26111f5207b7SJohn Levon 	arg = strip_expr(arg);
26121f5207b7SJohn Levon 
26131f5207b7SJohn Levon 	if (strcmp(key, "$") == 0)
26141f5207b7SJohn Levon 		return expr_to_var_sym(arg, sym);
26151f5207b7SJohn Levon 
26161f5207b7SJohn Levon 	if (strcmp(key, "*$") == 0) {
26171f5207b7SJohn Levon 		if (arg->type == EXPR_PREOP && arg->op == '&') {
26181f5207b7SJohn Levon 			arg = strip_expr(arg->unop);
26191f5207b7SJohn Levon 			return expr_to_var_sym(arg, sym);
26201f5207b7SJohn Levon 		} else {
26211f5207b7SJohn Levon 			tmp = expr_to_var_sym(arg, sym);
26221f5207b7SJohn Levon 			if (!tmp)
26231f5207b7SJohn Levon 				return NULL;
26241f5207b7SJohn Levon 			snprintf(buf, sizeof(buf), "*%s", tmp);
26251f5207b7SJohn Levon 			free_string(tmp);
26261f5207b7SJohn Levon 			return alloc_string(buf);
26271f5207b7SJohn Levon 		}
26281f5207b7SJohn Levon 	}
26291f5207b7SJohn Levon 
26306523a3aaSJohn Levon 	if (strncmp(key, "(*$)", 4) == 0) {
26316523a3aaSJohn Levon 		char buf[64];
26326523a3aaSJohn Levon 
26336523a3aaSJohn Levon 		if (arg->type == EXPR_PREOP && arg->op == '&') {
26346523a3aaSJohn Levon 			arg = strip_expr(arg->unop);
26356523a3aaSJohn Levon 			snprintf(buf, sizeof(buf), "$%s", key + 4);
26366523a3aaSJohn Levon 			return get_variable_from_key(arg, buf, sym);
26376523a3aaSJohn Levon 		} else {
26386523a3aaSJohn Levon 			tmp = expr_to_var_sym(arg, sym);
26396523a3aaSJohn Levon 			if (!tmp)
26406523a3aaSJohn Levon 				return NULL;
26416523a3aaSJohn Levon 			snprintf(buf, sizeof(buf), "(*%s)%s", tmp, key + 4);
26426523a3aaSJohn Levon 			free_string(tmp);
26436523a3aaSJohn Levon 			return alloc_string(buf);
26446523a3aaSJohn Levon 		}
26456523a3aaSJohn Levon 	}
26466523a3aaSJohn Levon 
26475a0e240fSJohn Levon 	while (key[0] == '*') {
26485a0e240fSJohn Levon 		star_cnt++;
2649efe51d0cSJohn Levon 		key++;
2650efe51d0cSJohn Levon 	}
2651efe51d0cSJohn Levon 
26526523a3aaSJohn Levon 	/*
26536523a3aaSJohn Levon 	 * FIXME:  This is a hack.
26546523a3aaSJohn Levon 	 * We should be able to parse expressions like (*$)->foo and *$->foo.
26556523a3aaSJohn Levon 	 */
26566523a3aaSJohn Levon 	type = get_type(arg);
26576523a3aaSJohn Levon 	if (is_struct_ptr(type))
26586523a3aaSJohn Levon 		add_dot = true;
26596523a3aaSJohn Levon 
26606523a3aaSJohn Levon 	if (arg->type == EXPR_PREOP && arg->op == '&' && star_cnt && !add_dot) {
26615a0e240fSJohn Levon 		arg = strip_expr(arg->unop);
26625a0e240fSJohn Levon 		star_cnt--;
26635a0e240fSJohn Levon 	}
26645a0e240fSJohn Levon 
26651f5207b7SJohn Levon 	if (arg->type == EXPR_PREOP && arg->op == '&') {
26661f5207b7SJohn Levon 		arg = strip_expr(arg->unop);
26671f5207b7SJohn Levon 		tmp = expr_to_var_sym(arg, sym);
26681f5207b7SJohn Levon 		if (!tmp)
26691f5207b7SJohn Levon 			return NULL;
26705a0e240fSJohn Levon 		snprintf(buf, sizeof(buf), "%.*s%s.%s",
26715a0e240fSJohn Levon 			 star_cnt, "**********", tmp, key + 3);
26721f5207b7SJohn Levon 		return alloc_string(buf);
26731f5207b7SJohn Levon 	}
26741f5207b7SJohn Levon 
26751f5207b7SJohn Levon 	tmp = expr_to_var_sym(arg, sym);
26761f5207b7SJohn Levon 	if (!tmp)
26771f5207b7SJohn Levon 		return NULL;
26785a0e240fSJohn Levon 	snprintf(buf, sizeof(buf), "%.*s%s%s", star_cnt, "**********", tmp, key + 1);
26791f5207b7SJohn Levon 	free_string(tmp);
26801f5207b7SJohn Levon 	return alloc_string(buf);
26811f5207b7SJohn Levon }
26821f5207b7SJohn Levon 
get_chunk_from_key(struct expression * arg,char * key,struct symbol ** sym,struct var_sym_list ** vsl)26831f5207b7SJohn Levon char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
26841f5207b7SJohn Levon {
26851f5207b7SJohn Levon 	*vsl = NULL;
26861f5207b7SJohn Levon 
26871f5207b7SJohn Levon 	if (strcmp("$", key) == 0)
26881f5207b7SJohn Levon 		return expr_to_chunk_sym_vsl(arg, sym, vsl);
26891f5207b7SJohn Levon 	return get_variable_from_key(arg, key, sym);
26901f5207b7SJohn Levon }
26911f5207b7SJohn Levon 
state_name_to_param_name(const char * state_name,const char * param_name)26921f5207b7SJohn Levon const char *state_name_to_param_name(const char *state_name, const char *param_name)
26931f5207b7SJohn Levon {
26945a0e240fSJohn Levon 	int star_cnt = 0;
26951f5207b7SJohn Levon 	int name_len;
26965a0e240fSJohn Levon 	char buf[256];
26971f5207b7SJohn Levon 
26981f5207b7SJohn Levon 	name_len = strlen(param_name);
26991f5207b7SJohn Levon 
27005a0e240fSJohn Levon 	while (state_name[0] == '*') {
27015a0e240fSJohn Levon 		star_cnt++;
2702efe51d0cSJohn Levon 		state_name++;
2703efe51d0cSJohn Levon 	}
2704efe51d0cSJohn Levon 
27055a0e240fSJohn Levon 	/* ten out of ten stars! */
27065a0e240fSJohn Levon 	if (star_cnt > 10)
27075a0e240fSJohn Levon 		return NULL;
27085a0e240fSJohn Levon 
27096523a3aaSJohn Levon 	if (strncmp(state_name, "(*", 2) == 0 &&
27106523a3aaSJohn Levon 	    strncmp(state_name + 2, param_name, name_len) == 0 &&
27116523a3aaSJohn Levon 	    state_name[name_len + 2] == ')') {
27126523a3aaSJohn Levon 		snprintf(buf, sizeof(buf), "%.*s(*$)%s", star_cnt, "**********",
27136523a3aaSJohn Levon 			 state_name + name_len + 3);
27146523a3aaSJohn Levon 		return alloc_sname(buf);
27156523a3aaSJohn Levon 	}
27166523a3aaSJohn Levon 
27171f5207b7SJohn Levon 	if (strcmp(state_name, param_name) == 0) {
27185a0e240fSJohn Levon 		snprintf(buf, sizeof(buf), "%.*s$", star_cnt, "**********");
2719c85f09ccSJohn Levon 		return alloc_sname(buf);
2720efe51d0cSJohn Levon 	}
2721efe51d0cSJohn Levon 
2722*b3263c98SJohn Levon 	/* check for '-' from "->" */
2723*b3263c98SJohn Levon 	if (strncmp(state_name, param_name, name_len) == 0 &&
2724*b3263c98SJohn Levon 	    state_name[name_len] == '-') {
27255a0e240fSJohn Levon 		snprintf(buf, sizeof(buf), "%.*s$%s", star_cnt, "**********", state_name + name_len);
2726c85f09ccSJohn Levon 		return alloc_sname(buf);
27271f5207b7SJohn Levon 	}
27281f5207b7SJohn Levon 	return NULL;
27291f5207b7SJohn Levon }
27301f5207b7SJohn Levon 
get_param_name_var_sym(const char * name,struct symbol * sym)27311f5207b7SJohn Levon const char *get_param_name_var_sym(const char *name, struct symbol *sym)
27321f5207b7SJohn Levon {
27331f5207b7SJohn Levon 	if (!sym || !sym->ident)
27341f5207b7SJohn Levon 		return NULL;
27351f5207b7SJohn Levon 
27361f5207b7SJohn Levon 	return state_name_to_param_name(name, sym->ident->name);
27371f5207b7SJohn Levon }
27381f5207b7SJohn Levon 
get_mtag_name_var_sym(const char * state_name,struct symbol * sym)27391f5207b7SJohn Levon const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym)
27401f5207b7SJohn Levon {
27411f5207b7SJohn Levon 	struct symbol *type;
27421f5207b7SJohn Levon 	const char *sym_name;
27431f5207b7SJohn Levon 	int name_len;
27441f5207b7SJohn Levon 	static char buf[256];
27451f5207b7SJohn Levon 
27461f5207b7SJohn Levon 	/*
27471f5207b7SJohn Levon 	 * mtag_name is different from param_name because mtags can be a struct
27481f5207b7SJohn Levon 	 * instead of a struct pointer.  But we want to treat it like a pointer
27491f5207b7SJohn Levon 	 * because really an mtag is a pointer.  Or in other words, if you pass
27501f5207b7SJohn Levon 	 * a struct foo then you want to talk about foo.bar but with an mtag
27511f5207b7SJohn Levon 	 * you want to refer to it as foo->bar.
27521f5207b7SJohn Levon 	 *
27531f5207b7SJohn Levon 	 */
27541f5207b7SJohn Levon 
27551f5207b7SJohn Levon 	if (!sym || !sym->ident)
27561f5207b7SJohn Levon 		return NULL;
27571f5207b7SJohn Levon 
27581f5207b7SJohn Levon 	type = get_real_base_type(sym);
27591f5207b7SJohn Levon 	if (type && type->type == SYM_BASETYPE)
27601f5207b7SJohn Levon 		return "*$";
27611f5207b7SJohn Levon 
27621f5207b7SJohn Levon 	sym_name = sym->ident->name;
27631f5207b7SJohn Levon 	name_len = strlen(sym_name);
27641f5207b7SJohn Levon 
27651f5207b7SJohn Levon 	if (state_name[name_len] == '.' && /* check for '-' from "->" */
27661f5207b7SJohn Levon 	    strncmp(state_name, sym_name, name_len) == 0) {
27671f5207b7SJohn Levon 		snprintf(buf, sizeof(buf), "$->%s", state_name + name_len + 1);
27681f5207b7SJohn Levon 		return buf;
27691f5207b7SJohn Levon 	}
27701f5207b7SJohn Levon 
27711f5207b7SJohn Levon 	return state_name_to_param_name(state_name, sym_name);
27721f5207b7SJohn Levon }
27731f5207b7SJohn Levon 
get_mtag_name_expr(struct expression * expr)27741f5207b7SJohn Levon const char *get_mtag_name_expr(struct expression *expr)
27751f5207b7SJohn Levon {
27761f5207b7SJohn Levon 	char *name;
27771f5207b7SJohn Levon 	struct symbol *sym;
27781f5207b7SJohn Levon 	const char *ret = NULL;
27791f5207b7SJohn Levon 
27801f5207b7SJohn Levon 	name = expr_to_var_sym(expr, &sym);
27811f5207b7SJohn Levon 	if (!name || !sym)
27821f5207b7SJohn Levon 		goto free;
27831f5207b7SJohn Levon 
27841f5207b7SJohn Levon 	ret = get_mtag_name_var_sym(name, sym);
27851f5207b7SJohn Levon free:
27861f5207b7SJohn Levon 	free_string(name);
27871f5207b7SJohn Levon 	return ret;
27881f5207b7SJohn Levon }
27891f5207b7SJohn Levon 
get_param_name(struct sm_state * sm)27901f5207b7SJohn Levon const char *get_param_name(struct sm_state *sm)
27911f5207b7SJohn Levon {
27921f5207b7SJohn Levon 	return get_param_name_var_sym(sm->name, sm->sym);
27931f5207b7SJohn Levon }
27941f5207b7SJohn Levon 
get_data_info_name(struct expression * expr)27951f5207b7SJohn Levon char *get_data_info_name(struct expression *expr)
27961f5207b7SJohn Levon {
27971f5207b7SJohn Levon 	struct symbol *sym;
27981f5207b7SJohn Levon 	char *name;
27991f5207b7SJohn Levon 	char buf[256];
28001f5207b7SJohn Levon 	char *ret = NULL;
28011f5207b7SJohn Levon 
28021f5207b7SJohn Levon 	expr = strip_expr(expr);
28031f5207b7SJohn Levon 	name = get_member_name(expr);
28041f5207b7SJohn Levon 	if (name)
28051f5207b7SJohn Levon 		return name;
28061f5207b7SJohn Levon 	name = expr_to_var_sym(expr, &sym);
28071f5207b7SJohn Levon 	if (!name || !sym)
28081f5207b7SJohn Levon 		goto free;
28091f5207b7SJohn Levon 	if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
28101f5207b7SJohn Levon 		goto free;
28111f5207b7SJohn Levon 	if (sym->ctype.modifiers & MOD_STATIC)
28121f5207b7SJohn Levon 		snprintf(buf, sizeof(buf), "static %s", name);
28131f5207b7SJohn Levon 	else
28141f5207b7SJohn Levon 		snprintf(buf, sizeof(buf), "global %s", name);
28151f5207b7SJohn Levon 	ret = alloc_sname(buf);
28161f5207b7SJohn Levon free:
28171f5207b7SJohn Levon 	free_string(name);
28181f5207b7SJohn Levon 	return ret;
28191f5207b7SJohn Levon }
2820