11f5207b7SJohn Levon /*
21f5207b7SJohn Levon  * Copyright (C) 2016 Oracle.
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 /*
191f5207b7SJohn Levon  * The point here is to store the relationships between two variables.
201f5207b7SJohn Levon  * Ie:  y > x.
211f5207b7SJohn Levon  * To do that we create a state with the two variables in alphabetical order:
221f5207b7SJohn Levon  * ->name = "x vs y" and the state would be "<".  On the false path the state
231f5207b7SJohn Levon  * would be ">=".
241f5207b7SJohn Levon  *
251f5207b7SJohn Levon  * Part of the trick of it is that if x or y is modified then we need to reset
261f5207b7SJohn Levon  * the state.  We need to keep a list of all the states which depend on x and
271f5207b7SJohn Levon  * all the states which depend on y.  The link_id code handles this.
281f5207b7SJohn Levon  *
291f5207b7SJohn Levon  */
301f5207b7SJohn Levon 
311f5207b7SJohn Levon #include "smatch.h"
321f5207b7SJohn Levon #include "smatch_extra.h"
331f5207b7SJohn Levon #include "smatch_slist.h"
341f5207b7SJohn Levon 
351f5207b7SJohn Levon static int compare_id;
361f5207b7SJohn Levon static int link_id;
371f5207b7SJohn Levon 
alloc_link_state(struct string_list * links)381f5207b7SJohn Levon static struct smatch_state *alloc_link_state(struct string_list *links)
391f5207b7SJohn Levon {
401f5207b7SJohn Levon 	struct smatch_state *state;
411f5207b7SJohn Levon 	static char buf[256];
421f5207b7SJohn Levon 	char *tmp;
431f5207b7SJohn Levon 	int i;
441f5207b7SJohn Levon 
451f5207b7SJohn Levon 	state = __alloc_smatch_state(0);
461f5207b7SJohn Levon 
471f5207b7SJohn Levon 	i = 0;
481f5207b7SJohn Levon 	FOR_EACH_PTR(links, tmp) {
491f5207b7SJohn Levon 		if (!i++) {
501f5207b7SJohn Levon 			snprintf(buf, sizeof(buf), "%s", tmp);
511f5207b7SJohn Levon 		} else {
521f5207b7SJohn Levon 			append(buf, ", ", sizeof(buf));
531f5207b7SJohn Levon 			append(buf, tmp, sizeof(buf));
541f5207b7SJohn Levon 		}
551f5207b7SJohn Levon 	} END_FOR_EACH_PTR(tmp);
561f5207b7SJohn Levon 
571f5207b7SJohn Levon 	state->name = alloc_sname(buf);
581f5207b7SJohn Levon 	state->data = links;
591f5207b7SJohn Levon 	return state;
601f5207b7SJohn Levon }
611f5207b7SJohn Levon 
merge_links(struct smatch_state * s1,struct smatch_state * s2)621f5207b7SJohn Levon static struct smatch_state *merge_links(struct smatch_state *s1, struct smatch_state *s2)
631f5207b7SJohn Levon {
641f5207b7SJohn Levon 	struct smatch_state *ret;
651f5207b7SJohn Levon 	struct string_list *links;
661f5207b7SJohn Levon 
671f5207b7SJohn Levon 	links = combine_string_lists(s1->data, s2->data);
681f5207b7SJohn Levon 	ret = alloc_link_state(links);
691f5207b7SJohn Levon 	return ret;
701f5207b7SJohn Levon }
711f5207b7SJohn Levon 
save_link_var_sym(const char * var,struct symbol * sym,const char * link)721f5207b7SJohn Levon static void save_link_var_sym(const char *var, struct symbol *sym, const char *link)
731f5207b7SJohn Levon {
741f5207b7SJohn Levon 	struct smatch_state *old_state, *new_state;
751f5207b7SJohn Levon 	struct string_list *links;
761f5207b7SJohn Levon 	char *new;
771f5207b7SJohn Levon 
781f5207b7SJohn Levon 	old_state = get_state(link_id, var, sym);
791f5207b7SJohn Levon 	if (old_state)
801f5207b7SJohn Levon 		links = clone_str_list(old_state->data);
811f5207b7SJohn Levon 	else
821f5207b7SJohn Levon 		links = NULL;
831f5207b7SJohn Levon 
841f5207b7SJohn Levon 	new = alloc_sname(link);
851f5207b7SJohn Levon 	insert_string(&links, new);
861f5207b7SJohn Levon 
871f5207b7SJohn Levon 	new_state = alloc_link_state(links);
881f5207b7SJohn Levon 	set_state(link_id, var, sym, new_state);
891f5207b7SJohn Levon }
901f5207b7SJohn Levon 
add_comparison_var_sym(const char * left_name,struct var_sym_list * left_vsl,int comparison,const char * right_name,struct var_sym_list * right_vsl)911f5207b7SJohn Levon static void add_comparison_var_sym(const char *left_name,
921f5207b7SJohn Levon 		struct var_sym_list *left_vsl,
931f5207b7SJohn Levon 		int comparison,
941f5207b7SJohn Levon 		const char *right_name, struct var_sym_list *right_vsl)
951f5207b7SJohn Levon {
961f5207b7SJohn Levon 	struct smatch_state *state;
971f5207b7SJohn Levon 	struct var_sym *vs;
981f5207b7SJohn Levon 	char state_name[256];
991f5207b7SJohn Levon 
1001f5207b7SJohn Levon 	if (strcmp(left_name, right_name) > 0) {
1011f5207b7SJohn Levon 		const char *tmp_name = left_name;
1021f5207b7SJohn Levon 		struct var_sym_list *tmp_vsl = left_vsl;
1031f5207b7SJohn Levon 
1041f5207b7SJohn Levon 		left_name = right_name;
1051f5207b7SJohn Levon 		left_vsl = right_vsl;
1061f5207b7SJohn Levon 		right_name = tmp_name;
1071f5207b7SJohn Levon 		right_vsl = tmp_vsl;
1081f5207b7SJohn Levon 		comparison = flip_comparison(comparison);
1091f5207b7SJohn Levon 	}
1101f5207b7SJohn Levon 	snprintf(state_name, sizeof(state_name), "%s vs %s", left_name, right_name);
1111f5207b7SJohn Levon 	state = alloc_compare_state(NULL, left_name, left_vsl, comparison, NULL, right_name, right_vsl);
1121f5207b7SJohn Levon 
1131f5207b7SJohn Levon 	set_state(compare_id, state_name, NULL, state);
1141f5207b7SJohn Levon 
1151f5207b7SJohn Levon 	FOR_EACH_PTR(left_vsl, vs) {
1161f5207b7SJohn Levon 		save_link_var_sym(vs->var, vs->sym, state_name);
1171f5207b7SJohn Levon 	} END_FOR_EACH_PTR(vs);
1181f5207b7SJohn Levon 	FOR_EACH_PTR(right_vsl, vs) {
1191f5207b7SJohn Levon 		save_link_var_sym(vs->var, vs->sym, state_name);
1201f5207b7SJohn Levon 	} END_FOR_EACH_PTR(vs);
1211f5207b7SJohn Levon }
1221f5207b7SJohn Levon 
1231f5207b7SJohn Levon /*
1241f5207b7SJohn Levon  * This is quite a bit more limitted, less ambitious, simpler compared to
1251f5207b7SJohn Levon  * smatch_camparison.c.
1261f5207b7SJohn Levon  */
__compare_param_limit_hook(struct expression * left_expr,struct expression * right_expr,const char * state_name,struct smatch_state * true_state,struct smatch_state * false_state)1271f5207b7SJohn Levon void __compare_param_limit_hook(struct expression *left_expr, struct expression *right_expr,
1281f5207b7SJohn Levon 				const char *state_name,
1291f5207b7SJohn Levon 				struct smatch_state *true_state, struct smatch_state *false_state)
1301f5207b7SJohn Levon {
1311f5207b7SJohn Levon 	char *left_name = NULL;
1321f5207b7SJohn Levon 	char *right_name = NULL;
1331f5207b7SJohn Levon 	char *tmp_name = NULL;
1341f5207b7SJohn Levon 	struct symbol *left_sym, *right_sym, *tmp_sym;
1351f5207b7SJohn Levon 
1361f5207b7SJohn Levon 	left_name = expr_to_var_sym(left_expr, &left_sym);
1371f5207b7SJohn Levon 	if (!left_name || !left_sym)
1381f5207b7SJohn Levon 		goto free;
1391f5207b7SJohn Levon 	right_name = expr_to_var_sym(right_expr, &right_sym);
1401f5207b7SJohn Levon 	if (!right_name || !right_sym)
1411f5207b7SJohn Levon 		goto free;
1421f5207b7SJohn Levon 
1431f5207b7SJohn Levon 	if (get_param_num_from_sym(left_sym) < 0 ||
1441f5207b7SJohn Levon 	    get_param_num_from_sym(right_sym) < 0)
1451f5207b7SJohn Levon 		return;
1461f5207b7SJohn Levon 
1471f5207b7SJohn Levon 	tmp_name = get_other_name_sym(left_name, left_sym, &tmp_sym);
1481f5207b7SJohn Levon 	if (tmp_name) {
1491f5207b7SJohn Levon 		free_string(left_name);
1501f5207b7SJohn Levon 		left_name = tmp_name;
1511f5207b7SJohn Levon 		left_sym = tmp_sym;
1521f5207b7SJohn Levon 	}
1531f5207b7SJohn Levon 
1541f5207b7SJohn Levon 	tmp_name = get_other_name_sym(right_name, right_sym, &tmp_sym);
1551f5207b7SJohn Levon 	if (tmp_name) {
1561f5207b7SJohn Levon 		free_string(right_name);
1571f5207b7SJohn Levon 		right_name = tmp_name;
1581f5207b7SJohn Levon 		right_sym = tmp_sym;
1591f5207b7SJohn Levon 	}
1601f5207b7SJohn Levon 
1611f5207b7SJohn Levon 	if (param_was_set_var_sym(left_name, left_sym))
1621f5207b7SJohn Levon 		return;
1631f5207b7SJohn Levon 	if (param_was_set_var_sym(right_name, right_sym))
1641f5207b7SJohn Levon 		return;
1651f5207b7SJohn Levon 
1661f5207b7SJohn Levon 	set_true_false_states(compare_id, state_name, NULL, true_state, false_state);
1671f5207b7SJohn Levon 	save_link_var_sym(left_name, left_sym, state_name);
1681f5207b7SJohn Levon 	save_link_var_sym(right_name, right_sym, state_name);
1691f5207b7SJohn Levon free:
1701f5207b7SJohn Levon 	free_string(left_name);
1711f5207b7SJohn Levon 	free_string(right_name);
1721f5207b7SJohn Levon }
1731f5207b7SJohn Levon 
print_return_comparison(int return_id,char * return_ranges,struct expression * expr)1741f5207b7SJohn Levon static void print_return_comparison(int return_id, char *return_ranges, struct expression *expr)
1751f5207b7SJohn Levon {
1761f5207b7SJohn Levon 	struct sm_state *tmp;
1771f5207b7SJohn Levon 	struct string_list *links;
1781f5207b7SJohn Levon 	char *link;
1791f5207b7SJohn Levon 	struct sm_state *sm;
1801f5207b7SJohn Levon 	struct compare_data *data;
1811f5207b7SJohn Levon 	struct var_sym *left, *right;
1821f5207b7SJohn Levon 	int left_param, right_param;
183*efe51d0cSJohn Levon 	static char left_buf[248];
184*efe51d0cSJohn Levon 	static char right_buf[248];
1851f5207b7SJohn Levon 	static char info_buf[256];
1861f5207b7SJohn Levon 	const char *tmp_name;
1871f5207b7SJohn Levon 
1881f5207b7SJohn Levon 	FOR_EACH_MY_SM(link_id, __get_cur_stree(), tmp) {
1891f5207b7SJohn Levon 		links = tmp->state->data;
1901f5207b7SJohn Levon 		FOR_EACH_PTR(links, link) {
1911f5207b7SJohn Levon 			sm = get_sm_state(compare_id, link, NULL);
1921f5207b7SJohn Levon 			if (!sm)
1931f5207b7SJohn Levon 				continue;
1941f5207b7SJohn Levon 			data = sm->state->data;
1951f5207b7SJohn Levon 			if (!data || !data->comparison)
1961f5207b7SJohn Levon 				continue;
1971f5207b7SJohn Levon 			if (ptr_list_size((struct ptr_list *)data->left_vsl) != 1 ||
1981f5207b7SJohn Levon 			    ptr_list_size((struct ptr_list *)data->right_vsl) != 1)
1991f5207b7SJohn Levon 				continue;
2001f5207b7SJohn Levon 			left = first_ptr_list((struct ptr_list *)data->left_vsl);
2011f5207b7SJohn Levon 			right = first_ptr_list((struct ptr_list *)data->right_vsl);
2021f5207b7SJohn Levon 			if (left->sym == right->sym &&
2031f5207b7SJohn Levon 			    strcmp(left->var, right->var) == 0)
2041f5207b7SJohn Levon 				continue;
2051f5207b7SJohn Levon 			/*
2061f5207b7SJohn Levon 			 * Both parameters link to this comparison so only
2071f5207b7SJohn Levon 			 * record the first one.
2081f5207b7SJohn Levon 			 */
2091f5207b7SJohn Levon 			if (left->sym != tmp->sym ||
2101f5207b7SJohn Levon 			    strcmp(left->var, tmp->name) != 0)
2111f5207b7SJohn Levon 				continue;
2121f5207b7SJohn Levon 
2131f5207b7SJohn Levon 			left_param = get_param_num_from_sym(left->sym);
2141f5207b7SJohn Levon 			right_param = get_param_num_from_sym(right->sym);
2151f5207b7SJohn Levon 			if (left_param < 0 || right_param < 0) /* can't happen hopefully */
2161f5207b7SJohn Levon 				continue;
2171f5207b7SJohn Levon 
2181f5207b7SJohn Levon 			tmp_name = get_param_name_var_sym(left->var, left->sym);
2191f5207b7SJohn Levon 			if (!tmp_name)
2201f5207b7SJohn Levon 				continue;
2211f5207b7SJohn Levon 			snprintf(left_buf, sizeof(left_buf), "%s", tmp_name);
2221f5207b7SJohn Levon 
2231f5207b7SJohn Levon 			tmp_name = get_param_name_var_sym(right->var, right->sym);
2241f5207b7SJohn Levon 			if (!tmp_name || tmp_name[0] != '$')
2251f5207b7SJohn Levon 				continue;
2261f5207b7SJohn Levon 			snprintf(right_buf, sizeof(right_buf), "$%d%s", right_param, tmp_name + 1);
2271f5207b7SJohn Levon 
2281f5207b7SJohn Levon 			snprintf(info_buf, sizeof(info_buf), "%s %s", show_special(data->comparison), right_buf);
2291f5207b7SJohn Levon 			sql_insert_return_states(return_id, return_ranges,
2301f5207b7SJohn Levon 					COMPARE_LIMIT, left_param, left_buf, info_buf);
2311f5207b7SJohn Levon 		} END_FOR_EACH_PTR(link);
2321f5207b7SJohn Levon 
2331f5207b7SJohn Levon 	} END_FOR_EACH_SM(tmp);
2341f5207b7SJohn Levon }
2351f5207b7SJohn Levon 
parse_comparison(char ** value,int * op)2361f5207b7SJohn Levon static int parse_comparison(char **value, int *op)
2371f5207b7SJohn Levon {
2381f5207b7SJohn Levon 
2391f5207b7SJohn Levon 	*op = **value;
2401f5207b7SJohn Levon 
2411f5207b7SJohn Levon 	switch (*op) {
2421f5207b7SJohn Levon 	case '<':
2431f5207b7SJohn Levon 		(*value)++;
2441f5207b7SJohn Levon 		if (**value == '=') {
2451f5207b7SJohn Levon 			(*value)++;
2461f5207b7SJohn Levon 			*op = SPECIAL_LTE;
2471f5207b7SJohn Levon 		}
2481f5207b7SJohn Levon 		break;
2491f5207b7SJohn Levon 	case '=':
2501f5207b7SJohn Levon 		(*value)++;
2511f5207b7SJohn Levon 		(*value)++;
2521f5207b7SJohn Levon 		*op = SPECIAL_EQUAL;
2531f5207b7SJohn Levon 		break;
2541f5207b7SJohn Levon 	case '!':
2551f5207b7SJohn Levon 		(*value)++;
2561f5207b7SJohn Levon 		(*value)++;
2571f5207b7SJohn Levon 		*op = SPECIAL_NOTEQUAL;
2581f5207b7SJohn Levon 		break;
2591f5207b7SJohn Levon 	case '>':
2601f5207b7SJohn Levon 		(*value)++;
2611f5207b7SJohn Levon 		if (**value == '=') {
2621f5207b7SJohn Levon 			(*value)++;
2631f5207b7SJohn Levon 			*op = SPECIAL_GTE;
2641f5207b7SJohn Levon 		}
2651f5207b7SJohn Levon 		break;
2661f5207b7SJohn Levon 	default:
2671f5207b7SJohn Levon 		return 0;
2681f5207b7SJohn Levon 	}
2691f5207b7SJohn Levon 
2701f5207b7SJohn Levon 	if (**value != ' ') {
2711f5207b7SJohn Levon 		sm_perror("parsing comparison.  %s", *value);
2721f5207b7SJohn Levon 		return 0;
2731f5207b7SJohn Levon 	}
2741f5207b7SJohn Levon 
2751f5207b7SJohn Levon 	(*value)++;
2761f5207b7SJohn Levon 	return 1;
2771f5207b7SJohn Levon }
2781f5207b7SJohn Levon 
split_op_param_key(char * value,int * op,int * param,char ** key)2791f5207b7SJohn Levon static int split_op_param_key(char *value, int *op, int *param, char **key)
2801f5207b7SJohn Levon {
2811f5207b7SJohn Levon 	static char buf[256];
2821f5207b7SJohn Levon 	char *p;
2831f5207b7SJohn Levon 
2841f5207b7SJohn Levon 	if (!parse_comparison(&value, op))
2851f5207b7SJohn Levon 		return 0;
2861f5207b7SJohn Levon 
287*efe51d0cSJohn Levon 	snprintf(buf, sizeof(buf), "%s", value);
2881f5207b7SJohn Levon 
2891f5207b7SJohn Levon 	p = buf;
2901f5207b7SJohn Levon 	if (*p++ != '$')
2911f5207b7SJohn Levon 		return 0;
2921f5207b7SJohn Levon 
2931f5207b7SJohn Levon 	*param = atoi(p);
2941f5207b7SJohn Levon 	if (*param < 0 || *param > 99)
2951f5207b7SJohn Levon 		return 0;
2961f5207b7SJohn Levon 	p++;
2971f5207b7SJohn Levon 	if (*param > 9)
2981f5207b7SJohn Levon 		p++;
2991f5207b7SJohn Levon 	p--;
3001f5207b7SJohn Levon 	*p = '$';
3011f5207b7SJohn Levon 	*key = p;
3021f5207b7SJohn Levon 
3031f5207b7SJohn Levon 	return 1;
3041f5207b7SJohn Levon }
3051f5207b7SJohn Levon 
db_return_comparison(struct expression * expr,int left_param,char * key,char * value)3061f5207b7SJohn Levon static void db_return_comparison(struct expression *expr, int left_param, char *key, char *value)
3071f5207b7SJohn Levon {
3081f5207b7SJohn Levon 	struct expression *left_arg, *right_arg;
3091f5207b7SJohn Levon 	char *left_name = NULL;
3101f5207b7SJohn Levon 	struct symbol *left_sym;
3111f5207b7SJohn Levon 	char *right_name = NULL;
3121f5207b7SJohn Levon 	struct symbol *right_sym;
3131f5207b7SJohn Levon 	int op;
3141f5207b7SJohn Levon 	int right_param;
3151f5207b7SJohn Levon 	char *right_key;
3161f5207b7SJohn Levon 	struct var_sym_list *left_vsl = NULL, *right_vsl = NULL;
3171f5207b7SJohn Levon 
3181f5207b7SJohn Levon 	while (expr->type == EXPR_ASSIGNMENT)
3191f5207b7SJohn Levon 		expr = strip_expr(expr->right);
3201f5207b7SJohn Levon 	if (expr->type != EXPR_CALL)
3211f5207b7SJohn Levon 		return;
3221f5207b7SJohn Levon 
3231f5207b7SJohn Levon 	if (!split_op_param_key(value, &op, &right_param, &right_key))
3241f5207b7SJohn Levon 		return;
3251f5207b7SJohn Levon 
3261f5207b7SJohn Levon 	left_arg = get_argument_from_call_expr(expr->args, left_param);
3271f5207b7SJohn Levon 	if (!left_arg)
3281f5207b7SJohn Levon 		return;
3291f5207b7SJohn Levon 
3301f5207b7SJohn Levon 	right_arg = get_argument_from_call_expr(expr->args, right_param);
3311f5207b7SJohn Levon 	if (!right_arg)
3321f5207b7SJohn Levon 		return;
3331f5207b7SJohn Levon 
3341f5207b7SJohn Levon 	left_name = get_variable_from_key(left_arg, key, &left_sym);
3351f5207b7SJohn Levon 	if (!left_name || !left_sym)
3361f5207b7SJohn Levon 		goto free;
3371f5207b7SJohn Levon 	if (get_param_num_from_sym(left_sym) < 0)
3381f5207b7SJohn Levon 		goto free;
3391f5207b7SJohn Levon 
3401f5207b7SJohn Levon 	right_name = get_variable_from_key(right_arg, right_key, &right_sym);
3411f5207b7SJohn Levon 	if (!right_name || !right_sym)
3421f5207b7SJohn Levon 		goto free;
3431f5207b7SJohn Levon 	if (get_param_num_from_sym(right_sym) < 0)
3441f5207b7SJohn Levon 		goto free;
3451f5207b7SJohn Levon 
3461f5207b7SJohn Levon 	add_var_sym(&left_vsl, left_name, left_sym);
3471f5207b7SJohn Levon 	add_var_sym(&right_vsl, right_name, right_sym);
3481f5207b7SJohn Levon 
3491f5207b7SJohn Levon 	add_comparison_var_sym(left_name, left_vsl, op, right_name, right_vsl);
3501f5207b7SJohn Levon 
3511f5207b7SJohn Levon free:
3521f5207b7SJohn Levon 	free_string(left_name);
3531f5207b7SJohn Levon 	free_string(right_name);
3541f5207b7SJohn Levon }
3551f5207b7SJohn Levon 
register_param_compare_limit(int id)3561f5207b7SJohn Levon void register_param_compare_limit(int id)
3571f5207b7SJohn Levon {
3581f5207b7SJohn Levon 	compare_id = id;
3591f5207b7SJohn Levon 
360*efe51d0cSJohn Levon 	set_dynamic_states(compare_id);
3611f5207b7SJohn Levon 	add_merge_hook(compare_id, &merge_compare_states);
3621f5207b7SJohn Levon 	add_split_return_callback(&print_return_comparison);
3631f5207b7SJohn Levon 
3641f5207b7SJohn Levon 	select_return_states_hook(COMPARE_LIMIT, &db_return_comparison);
3651f5207b7SJohn Levon }
3661f5207b7SJohn Levon 
register_param_compare_limit_links(int id)3671f5207b7SJohn Levon void register_param_compare_limit_links(int id)
3681f5207b7SJohn Levon {
3691f5207b7SJohn Levon 	link_id = id;
3701f5207b7SJohn Levon 
371*efe51d0cSJohn Levon 	set_dynamic_states(link_id);
3721f5207b7SJohn Levon 	add_merge_hook(link_id, &merge_links);
3731f5207b7SJohn Levon }
3741f5207b7SJohn Levon 
375