2 * Copyright (C) 2017 Oracle.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 #include "smatch_extra.h"
21 static int find_param_eq(struct expression
*expr
, int size
)
23 struct expression
*arg
;
28 FOR_EACH_PTR(expr
->args
, arg
) {
30 if (!get_implied_value(arg
, &val
))
32 if (val
.value
== size
)
34 } END_FOR_EACH_PTR(arg
);
39 static int find_skb_len(struct expression
*call
, struct expression
*arg
)
41 struct expression
*tmp
;
43 char *data_name
, *len_name
;
47 data_name
= expr_to_str(arg
);
50 len
= snprintf(buf
, sizeof(buf
), "%s", data_name
);
51 if (len
< 4 || len
>= sizeof(buf
))
53 sprintf(buf
+ len
- 4, "len");
56 FOR_EACH_PTR(call
->args
, tmp
) {
62 len_name
= expr_to_var(tmp
);
65 if (strcmp(buf
, len_name
) == 0)
67 free_string(len_name
);
72 } END_FOR_EACH_PTR(tmp
);
75 free_string(data_name
);
79 static void match_call(struct expression
*expr
)
81 struct expression
*arg
;
82 struct symbol
*type
, *arg_type
;
89 snprintf(elem_count
, sizeof(elem_count
), "%d", ELEM_COUNT
);
90 snprintf(byte_count
, sizeof(byte_count
), "%d", BYTE_COUNT
);
93 FOR_EACH_PTR(expr
->args
, arg
) {
96 if (!type
|| (type
->type
!= SYM_PTR
&& type
->type
!= SYM_ARRAY
))
99 if (is_skb_data(arg
)) {
100 nr
= find_skb_len(expr
, arg
);
102 snprintf(buf
, sizeof(buf
), "==$%d", nr
);
103 sql_insert_caller_info(expr
, BYTE_COUNT
, i
, buf
, byte_count
);
107 bytes
= get_array_size_bytes(arg
);
109 nr
= find_param_eq(expr
, bytes
);
111 snprintf(buf
, sizeof(buf
), "==$%d", nr
);
112 sql_insert_caller_info(expr
, BYTE_COUNT
, i
, buf
, byte_count
);
116 arg_type
= get_arg_type(expr
->fn
, i
);
117 if (arg_type
!= type
)
120 size
= get_array_size(arg
);
122 nr
= find_param_eq(expr
, size
);
124 snprintf(buf
, sizeof(buf
), "==$%d", nr
);
125 sql_insert_caller_info(expr
, ELEM_COUNT
, i
, buf
, elem_count
);
129 } END_FOR_EACH_PTR(arg
);
132 void register_passes_array_size(int id
)
134 add_hook(&match_call
, FUNCTION_CALL_HOOK
);