11f5207b7SJohn Levon /*
21f5207b7SJohn Levon  * Copyright (C) 2014 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 #include "smatch.h"
191f5207b7SJohn Levon 
201f5207b7SJohn Levon static int my_id;
211f5207b7SJohn Levon 
221f5207b7SJohn Levon STATE(too_small);
231f5207b7SJohn Levon 
match_assign(struct expression * expr)241f5207b7SJohn Levon static void match_assign(struct expression *expr)
251f5207b7SJohn Levon {
261f5207b7SJohn Levon 	struct symbol *left_type, *right_type;
271f5207b7SJohn Levon 	struct expression *size_expr;
281f5207b7SJohn Levon 	sval_t min_size;
29*efe51d0cSJohn Levon 	int limit_type;
30*efe51d0cSJohn Levon 	int bytes;
311f5207b7SJohn Levon 
321f5207b7SJohn Levon 	left_type = get_type(expr->left);
331f5207b7SJohn Levon 	if (!left_type || left_type->type != SYM_PTR)
341f5207b7SJohn Levon 		return;
351f5207b7SJohn Levon 	left_type = get_real_base_type(left_type);
361f5207b7SJohn Levon 	if (!left_type || left_type->type != SYM_STRUCT)
371f5207b7SJohn Levon 		return;
381f5207b7SJohn Levon 
391f5207b7SJohn Levon 	right_type = get_type(expr->right);
401f5207b7SJohn Levon 	if (!right_type || right_type->type != SYM_PTR)
411f5207b7SJohn Levon 		return;
421f5207b7SJohn Levon 	right_type = get_real_base_type(right_type);
431f5207b7SJohn Levon 	if (!right_type)
441f5207b7SJohn Levon 		return;
451f5207b7SJohn Levon 	if (right_type != &void_ctype && type_bits(right_type) != 8)
461f5207b7SJohn Levon 		return;
471f5207b7SJohn Levon 
48*efe51d0cSJohn Levon 	bytes = get_array_size_bytes(expr->right);
49*efe51d0cSJohn Levon 	if (bytes >= type_bytes(left_type))
50*efe51d0cSJohn Levon 		return;
51*efe51d0cSJohn Levon 
52*efe51d0cSJohn Levon 	size_expr = get_size_variable(expr->right, &limit_type);
531f5207b7SJohn Levon 	if (!size_expr)
541f5207b7SJohn Levon 		return;
55*efe51d0cSJohn Levon 	if (limit_type != ELEM_COUNT)
56*efe51d0cSJohn Levon 		return;
571f5207b7SJohn Levon 
581f5207b7SJohn Levon 	get_absolute_min(size_expr, &min_size);
591f5207b7SJohn Levon 	if (min_size.value >= type_bytes(left_type))
601f5207b7SJohn Levon 		return;
611f5207b7SJohn Levon 
621f5207b7SJohn Levon 	set_state_expr(my_id, expr->left, &too_small);
631f5207b7SJohn Levon }
641f5207b7SJohn Levon 
match_dereferences(struct expression * expr)651f5207b7SJohn Levon static void match_dereferences(struct expression *expr)
661f5207b7SJohn Levon {
671f5207b7SJohn Levon 	struct symbol *left_type;
681f5207b7SJohn Levon 	struct expression *right;
691f5207b7SJohn Levon 	struct smatch_state *state;
701f5207b7SJohn Levon 	char *name;
711f5207b7SJohn Levon 	struct expression *size_expr;
721f5207b7SJohn Levon 	sval_t min_size;
73*efe51d0cSJohn Levon 	int limit_type;
741f5207b7SJohn Levon 
751f5207b7SJohn Levon 	if (expr->type != EXPR_PREOP)
761f5207b7SJohn Levon 		return;
771f5207b7SJohn Levon 
781f5207b7SJohn Levon 	expr = strip_expr(expr->unop);
791f5207b7SJohn Levon 	state = get_state_expr(my_id, expr);
801f5207b7SJohn Levon 	if (state != &too_small)
811f5207b7SJohn Levon 		return;
821f5207b7SJohn Levon 
831f5207b7SJohn Levon 	left_type = get_type(expr);
841f5207b7SJohn Levon 	if (!left_type || left_type->type != SYM_PTR)
851f5207b7SJohn Levon 		return;
861f5207b7SJohn Levon 	left_type = get_real_base_type(left_type);
871f5207b7SJohn Levon 	if (!left_type || left_type->type != SYM_STRUCT)
881f5207b7SJohn Levon 		return;
891f5207b7SJohn Levon 
901f5207b7SJohn Levon 	right = get_assigned_expr(expr);
91*efe51d0cSJohn Levon 	size_expr = get_size_variable(right, &limit_type);
921f5207b7SJohn Levon 	if (!size_expr)
931f5207b7SJohn Levon 		return;
94*efe51d0cSJohn Levon 	if (limit_type != ELEM_COUNT)
95*efe51d0cSJohn Levon 		return;
961f5207b7SJohn Levon 
971f5207b7SJohn Levon 	get_absolute_min(size_expr, &min_size);
981f5207b7SJohn Levon 	if (min_size.value >= type_bytes(left_type))
991f5207b7SJohn Levon 		return;
1001f5207b7SJohn Levon 
1011f5207b7SJohn Levon 	name = expr_to_str(right);
1021f5207b7SJohn Levon 	sm_warning("is '%s' large enough for 'struct %s'? %s", name, left_type->ident ? left_type->ident->name : "<anon>", sval_to_str(min_size));
1031f5207b7SJohn Levon 	free_string(name);
1041f5207b7SJohn Levon 	set_state_expr(my_id, expr, &undefined);
1051f5207b7SJohn Levon }
1061f5207b7SJohn Levon 
check_buffer_too_small_for_struct(int id)1071f5207b7SJohn Levon void check_buffer_too_small_for_struct(int id)
1081f5207b7SJohn Levon {
1091f5207b7SJohn Levon 	my_id = id;
1101f5207b7SJohn Levon 
1111f5207b7SJohn Levon 	add_hook(&match_assign, ASSIGNMENT_HOOK);
1121f5207b7SJohn Levon 	add_hook(&match_dereferences, DEREF_HOOK);
1131f5207b7SJohn Levon }
114