11f5207b7SJohn Levon /*
21f5207b7SJohn Levon * Copyright (C) 2017 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 #include "smatch_extra.h"
201f5207b7SJohn Levon
find_param_eq(struct expression * expr,int size)211f5207b7SJohn Levon static int find_param_eq(struct expression *expr, int size)
221f5207b7SJohn Levon {
231f5207b7SJohn Levon struct expression *arg;
241f5207b7SJohn Levon sval_t val;
251f5207b7SJohn Levon int i;
261f5207b7SJohn Levon
271f5207b7SJohn Levon i = -1;
281f5207b7SJohn Levon FOR_EACH_PTR(expr->args, arg) {
291f5207b7SJohn Levon i++;
301f5207b7SJohn Levon if (!get_implied_value(arg, &val))
311f5207b7SJohn Levon continue;
321f5207b7SJohn Levon if (val.value == size)
331f5207b7SJohn Levon return i;
341f5207b7SJohn Levon } END_FOR_EACH_PTR(arg);
351f5207b7SJohn Levon
361f5207b7SJohn Levon return -1;
371f5207b7SJohn Levon }
381f5207b7SJohn Levon
match_call(struct expression * expr)391f5207b7SJohn Levon static void match_call(struct expression *expr)
401f5207b7SJohn Levon {
411f5207b7SJohn Levon struct expression *arg;
42*efe51d0cSJohn Levon struct symbol *type, *arg_type;
431f5207b7SJohn Levon int size, bytes;
441f5207b7SJohn Levon int i, nr;
451f5207b7SJohn Levon char buf[16];
46*efe51d0cSJohn Levon char elem_count[8];
47*efe51d0cSJohn Levon char byte_count[8];
481f5207b7SJohn Levon
49*efe51d0cSJohn Levon snprintf(elem_count, sizeof(elem_count), "%d", ELEM_COUNT);
50*efe51d0cSJohn Levon snprintf(byte_count, sizeof(byte_count), "%d", BYTE_COUNT);
511f5207b7SJohn Levon
521f5207b7SJohn Levon i = -1;
531f5207b7SJohn Levon FOR_EACH_PTR(expr->args, arg) {
541f5207b7SJohn Levon i++;
551f5207b7SJohn Levon type = get_type(arg);
561f5207b7SJohn Levon if (!type || (type->type != SYM_PTR && type->type != SYM_ARRAY))
571f5207b7SJohn Levon continue;
58*efe51d0cSJohn Levon arg_type = get_arg_type(expr->fn, i);
59*efe51d0cSJohn Levon if (arg_type != type)
60*efe51d0cSJohn Levon continue;
61*efe51d0cSJohn Levon
621f5207b7SJohn Levon size = get_array_size(arg);
631f5207b7SJohn Levon if (size > 0) {
641f5207b7SJohn Levon nr = find_param_eq(expr, size);
651f5207b7SJohn Levon if (nr >= 0) {
66*efe51d0cSJohn Levon snprintf(buf, sizeof(buf), "==$%d", nr);
67*efe51d0cSJohn Levon sql_insert_caller_info(expr, ELEM_COUNT, i, buf, elem_count);
681f5207b7SJohn Levon continue;
691f5207b7SJohn Levon }
701f5207b7SJohn Levon }
711f5207b7SJohn Levon bytes = get_array_size_bytes(arg);
721f5207b7SJohn Levon if (bytes > 0) {
731f5207b7SJohn Levon nr = find_param_eq(expr, bytes);
741f5207b7SJohn Levon if (nr >= 0) {
75*efe51d0cSJohn Levon snprintf(buf, sizeof(buf), "==$%d", nr);
76*efe51d0cSJohn Levon sql_insert_caller_info(expr, BYTE_COUNT, i, buf, byte_count);
771f5207b7SJohn Levon continue;
781f5207b7SJohn Levon }
791f5207b7SJohn Levon }
801f5207b7SJohn Levon } END_FOR_EACH_PTR(arg);
811f5207b7SJohn Levon }
821f5207b7SJohn Levon
register_passes_array_size(int id)831f5207b7SJohn Levon void register_passes_array_size(int id)
841f5207b7SJohn Levon {
851f5207b7SJohn Levon add_hook(&match_call, FUNCTION_CALL_HOOK);
861f5207b7SJohn Levon }
871f5207b7SJohn Levon
88