11f5207b7SJohn Levon /*
21f5207b7SJohn Levon  * Copyright (C) 2015 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  * Say we have a line like:
201f5207b7SJohn Levon  * foo = bar / 8;
211f5207b7SJohn Levon  * Assume we don't know anything about bar.  Well, now we know that foo is less
221f5207b7SJohn Levon  * than UINT_MAX / 8.  Which might be useful, but it probably is misleading
231f5207b7SJohn Levon  * useless knowledge.  Up to now we have ignored those but now we have said to
241f5207b7SJohn Levon  * store them.
251f5207b7SJohn Levon  *
261f5207b7SJohn Levon  * It also works if you have something like "foo = (int)(char)unknown_var;".
271f5207b7SJohn Levon  *
281f5207b7SJohn Levon  * I feel like this data doesn't have to be perfect, it just has to be better
291f5207b7SJohn Levon  * than nothing and that will help eliminate some false positives.
301f5207b7SJohn Levon  *
311f5207b7SJohn Levon  */
321f5207b7SJohn Levon 
331f5207b7SJohn Levon #include "smatch.h"
341f5207b7SJohn Levon #include "smatch_slist.h"
351f5207b7SJohn Levon #include "smatch_extra.h"
361f5207b7SJohn Levon 
371f5207b7SJohn Levon static int my_id;
381f5207b7SJohn Levon 
set_real_absolute(struct expression * expr,struct smatch_state * state)39*6523a3aaSJohn Levon void set_real_absolute(struct expression *expr, struct smatch_state *state)
40*6523a3aaSJohn Levon {
41*6523a3aaSJohn Levon 	set_state_expr(my_id, expr, clone_estate(state));
42*6523a3aaSJohn Levon }
43*6523a3aaSJohn Levon 
extra_mod_hook(const char * name,struct symbol * sym,struct expression * expr,struct smatch_state * state)44c85f09ccSJohn Levon static void extra_mod_hook(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
451f5207b7SJohn Levon {
461f5207b7SJohn Levon 	struct smatch_state *abs;
47c85f09ccSJohn Levon 	struct range_list *rl;
48c85f09ccSJohn Levon 
49c85f09ccSJohn Levon 	abs = get_state(my_id, name, sym);
50c85f09ccSJohn Levon 	if (!abs || !estate_rl(abs))
51c85f09ccSJohn Levon 		return;
52c85f09ccSJohn Levon 	rl = rl_intersection(estate_rl(abs), estate_rl(state));
53c85f09ccSJohn Levon 	set_state(my_id, name, sym, alloc_estate_rl(clone_rl(rl)));
54c85f09ccSJohn Levon }
55c85f09ccSJohn Levon 
pre_merge_hook(struct sm_state * cur,struct sm_state * other)56c85f09ccSJohn Levon static void pre_merge_hook(struct sm_state *cur, struct sm_state *other)
57c85f09ccSJohn Levon {
581f5207b7SJohn Levon 	struct smatch_state *extra;
591f5207b7SJohn Levon 	struct range_list *rl;
601f5207b7SJohn Levon 
61c85f09ccSJohn Levon 	extra = get_state(SMATCH_EXTRA, cur->name, cur->sym);
621f5207b7SJohn Levon 	if (!extra || !estate_rl(extra))
631f5207b7SJohn Levon 		return;
64c85f09ccSJohn Levon 	if (!estate_rl(cur->state)) {
65c85f09ccSJohn Levon 		set_state(my_id, cur->name, cur->sym, clone_estate(extra));
661f5207b7SJohn Levon 		return;
671f5207b7SJohn Levon 	}
68c85f09ccSJohn Levon 	rl = rl_intersection(estate_rl(cur->state), estate_rl(extra));
69c85f09ccSJohn Levon 	set_state(my_id, cur->name, cur->sym, alloc_estate_rl(clone_rl(rl)));
701f5207b7SJohn Levon }
711f5207b7SJohn Levon 
empty_state(struct sm_state * sm)721f5207b7SJohn Levon static struct smatch_state *empty_state(struct sm_state *sm)
731f5207b7SJohn Levon {
741f5207b7SJohn Levon 	return alloc_estate_empty();
751f5207b7SJohn Levon }
761f5207b7SJohn Levon 
in_iterator_pre_statement(void)771f5207b7SJohn Levon static int in_iterator_pre_statement(void)
781f5207b7SJohn Levon {
791f5207b7SJohn Levon 	struct statement *stmt;
801f5207b7SJohn Levon 
811f5207b7SJohn Levon 	/*
821f5207b7SJohn Levon 	 * we can't use __cur_stmt because that isn't set for
831f5207b7SJohn Levon 	 * iterator_pre_statement.  Kind of a mess.
841f5207b7SJohn Levon 	 *
851f5207b7SJohn Levon 	 */
861f5207b7SJohn Levon 
871f5207b7SJohn Levon 	stmt = last_ptr_list((struct ptr_list *)big_statement_stack);
881f5207b7SJohn Levon 
891f5207b7SJohn Levon 	if (!stmt || !stmt->parent)
901f5207b7SJohn Levon 		return 0;
911f5207b7SJohn Levon 	if (stmt->parent->type != STMT_ITERATOR)
921f5207b7SJohn Levon 		return 0;
931f5207b7SJohn Levon 	if (stmt->parent->iterator_pre_statement != stmt)
941f5207b7SJohn Levon 		return 0;
951f5207b7SJohn Levon 	return 1;
961f5207b7SJohn Levon }
971f5207b7SJohn Levon 
match_assign(struct expression * expr)981f5207b7SJohn Levon static void match_assign(struct expression *expr)
991f5207b7SJohn Levon {
1001f5207b7SJohn Levon 	struct range_list *rl;
1011f5207b7SJohn Levon 	struct symbol *type;
1021f5207b7SJohn Levon 	sval_t sval;
1031f5207b7SJohn Levon 
1041f5207b7SJohn Levon 	if (expr->op != '=')
1051f5207b7SJohn Levon 		return;
1061f5207b7SJohn Levon 	if (is_fake_call(expr->right))
1071f5207b7SJohn Levon 		return;
1081f5207b7SJohn Levon 	if (in_iterator_pre_statement())
1091f5207b7SJohn Levon 		return;
1101f5207b7SJohn Levon 
1111f5207b7SJohn Levon 	get_real_absolute_rl(expr->right, &rl);
1121f5207b7SJohn Levon 
1131f5207b7SJohn Levon 	type = get_type(expr->left);
1141f5207b7SJohn Levon 	if (!type)
1151f5207b7SJohn Levon 		return;
116efe51d0cSJohn Levon 	if (type->type != SYM_PTR && type->type != SYM_BASETYPE &&
117efe51d0cSJohn Levon 	    type->type != SYM_ENUM)
118efe51d0cSJohn Levon 		return;
1191f5207b7SJohn Levon 
1201f5207b7SJohn Levon 	rl = cast_rl(type, rl);
1211f5207b7SJohn Levon 	if (is_whole_rl(rl) && !get_state_expr(my_id, expr->left))
1221f5207b7SJohn Levon 		return;
1231f5207b7SJohn Levon 	/* These are handled by smatch_extra.c */
1241f5207b7SJohn Levon 	if (rl_to_sval(rl, &sval) && !get_state_expr(my_id, expr->left))
1251f5207b7SJohn Levon 		return;
1261f5207b7SJohn Levon 
1271f5207b7SJohn Levon 	set_state_expr(my_id, expr->left, alloc_estate_rl(clone_rl(rl)));
1281f5207b7SJohn Levon }
1291f5207b7SJohn Levon 
get_real_absolute_state(struct expression * expr)1301f5207b7SJohn Levon struct smatch_state *get_real_absolute_state(struct expression *expr)
1311f5207b7SJohn Levon {
1321f5207b7SJohn Levon 	return get_state_expr(my_id, expr);
1331f5207b7SJohn Levon }
1341f5207b7SJohn Levon 
get_real_absolute_state_var_sym(const char * name,struct symbol * sym)1351f5207b7SJohn Levon struct smatch_state *get_real_absolute_state_var_sym(const char *name, struct symbol *sym)
1361f5207b7SJohn Levon {
137c85f09ccSJohn Levon 	return __get_state(my_id, name, sym);
1381f5207b7SJohn Levon }
1391f5207b7SJohn Levon 
register_real_absolute(int id)1401f5207b7SJohn Levon void register_real_absolute(int id)
1411f5207b7SJohn Levon {
1421f5207b7SJohn Levon 	my_id = id;
1431f5207b7SJohn Levon 
144efe51d0cSJohn Levon 	set_dynamic_states(my_id);
1451f5207b7SJohn Levon 	add_pre_merge_hook(my_id, &pre_merge_hook);
1461f5207b7SJohn Levon 	add_unmatched_state_hook(my_id, &empty_state);
1471f5207b7SJohn Levon 	add_merge_hook(my_id, &merge_estates);
148c85f09ccSJohn Levon 	add_extra_mod_hook(&extra_mod_hook);
1491f5207b7SJohn Levon 
1501f5207b7SJohn Levon 	add_hook(&match_assign, ASSIGNMENT_HOOK);
1511f5207b7SJohn Levon }
1521f5207b7SJohn Levon 
153