Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2018 Intel Corporation.
3 : : * All rights reserved.
4 : : */
5 : :
6 : : #include "spdk/stdinc.h"
7 : : #include "spdk/env.h"
8 : : #include "spdk/file.h"
9 : : #include "spdk/base64.h"
10 : : #include "spdk/json.h"
11 : :
12 : : #define DEFAULT_RUNTIME 30 /* seconds */
13 : : #define MAX_RUNTIME_S 86400 /* 24 hours */
14 : : #define IO_TIMEOUT_S 5
15 : :
16 : : #define UNSIGNED_2BIT_MAX ((1 << 2) - 1)
17 : : #define UNSIGNED_4BIT_MAX ((1 << 4) - 1)
18 : : #define UNSIGNED_8BIT_MAX ((1 << 8) - 1)
19 : :
20 : : typedef bool (*json_parse_fn)(void *ele, struct spdk_json_val *val, size_t num_vals);
21 : :
22 : : static void
23 : 2713812 : fuzz_fill_random_bytes(char *character_repr, size_t len, unsigned int *rand_seed)
24 : : {
25 : : size_t i;
26 : :
27 [ + + ]: 172378356 : for (i = 0; i < len; i++) {
28 : 169664544 : character_repr[i] = rand_r(rand_seed) % UINT8_MAX;
29 : : }
30 : 2713812 : }
31 : :
32 : : static uint64_t
33 : 2867938 : fuzz_refresh_timeout(void)
34 : : {
35 : : uint64_t current_ticks;
36 : : uint64_t new_timeout_ticks;
37 : :
38 : 2867938 : current_ticks = spdk_get_ticks();
39 : :
40 : 2867938 : new_timeout_ticks = current_ticks + IO_TIMEOUT_S * spdk_get_ticks_hz();
41 [ - + ]: 2867938 : assert(new_timeout_ticks > current_ticks);
42 : :
43 : 2867938 : return new_timeout_ticks;
44 : : }
45 : :
46 : : static char *
47 : 15326 : fuzz_get_value_base_64_buffer(void *item, size_t len)
48 : : {
49 : : char *value_string;
50 : : size_t total_size;
51 : : int rc;
52 : :
53 : : /* Null pointer */
54 : 15326 : total_size = spdk_base64_get_encoded_strlen(len) + 1;
55 : :
56 : 15326 : value_string = calloc(1, total_size);
57 [ - + ]: 15326 : if (value_string == NULL) {
58 : 0 : return NULL;
59 : : }
60 : :
61 : 15326 : rc = spdk_base64_encode(value_string, item, len);
62 [ - + ]: 15326 : if (rc < 0) {
63 : 0 : free(value_string);
64 : 0 : return NULL;
65 : : }
66 : :
67 : 15326 : return value_string;
68 : : }
69 : :
70 : : static int
71 : 68 : fuzz_get_base_64_buffer_value(void *item, size_t len, char *buf, size_t buf_len)
72 : : {
73 : 0 : size_t size_of_data;
74 : : char *new_buf;
75 : : int rc;
76 : :
77 : 68 : new_buf = malloc(buf_len + 1);
78 [ - + ]: 68 : if (new_buf == NULL) {
79 : 0 : return -ENOMEM;
80 : : }
81 : :
82 [ - + ]: 68 : snprintf(new_buf, buf_len + 1, "%s", buf);
83 : :
84 : 68 : size_of_data = spdk_base64_get_decoded_len(buf_len);
85 : :
86 [ - + ]: 68 : if (size_of_data < len) {
87 : 0 : free(new_buf);
88 : 0 : return -EINVAL;
89 : : }
90 : :
91 : 68 : rc = spdk_base64_decode(item, &size_of_data, new_buf);
92 : :
93 [ + - - + ]: 68 : if (rc || size_of_data != len) {
94 : 0 : free(new_buf);
95 : 0 : return -EINVAL;
96 : : }
97 : :
98 : 68 : free(new_buf);
99 : 68 : return 0;
100 : : }
101 : :
102 : : static ssize_t
103 : 7 : read_json_into_buffer(const char *filename, struct spdk_json_val **values, void **file_data)
104 : : {
105 : 7 : FILE *file = fopen(filename, "r");
106 : 0 : size_t file_data_size;
107 : 7 : ssize_t num_json_values = 0, rc;
108 : :
109 [ - + ]: 7 : if (file == NULL) {
110 : : /* errno is set by fopen */
111 : 0 : return 0;
112 : : }
113 : :
114 : 7 : *file_data = spdk_posix_file_load(file, &file_data_size);
115 [ - + ]: 7 : if (*file_data == NULL) {
116 : 0 : fclose(file);
117 : 0 : return 0;
118 : : }
119 : :
120 : 7 : fclose(file);
121 : :
122 : 7 : num_json_values = spdk_json_parse(*file_data, file_data_size, NULL, 0, NULL,
123 : : SPDK_JSON_PARSE_FLAG_ALLOW_COMMENTS);
124 : :
125 : 7 : *values = calloc(num_json_values, sizeof(**values));
126 [ - + ]: 7 : if (values == NULL) {
127 : 0 : free(*file_data);
128 : 0 : *file_data = NULL;
129 : 0 : return 0;
130 : : }
131 : :
132 : 7 : rc = spdk_json_parse(*file_data, file_data_size, *values, num_json_values, NULL,
133 : : SPDK_JSON_PARSE_FLAG_ALLOW_COMMENTS);
134 [ - + ]: 7 : if (num_json_values != rc) {
135 : 0 : free(*values);
136 : 0 : *values = NULL;
137 : 0 : free(*file_data);
138 : 0 : *file_data = NULL;
139 : 0 : return 0;
140 : : }
141 : :
142 : 7 : return num_json_values;
143 : : }
144 : :
145 : : static size_t
146 : 4 : double_arr_size(void **buffer, size_t num_ele, size_t ele_size)
147 : : {
148 : : void *tmp;
149 : : size_t new_num_ele, allocation_size;
150 : :
151 [ - + ]: 4 : if (num_ele > SIZE_MAX / 2) {
152 : 0 : return 0;
153 : : }
154 : :
155 : 4 : new_num_ele = num_ele * 2;
156 : :
157 [ - + - + ]: 4 : if (new_num_ele > SIZE_MAX / ele_size) {
158 : 0 : return 0;
159 : : }
160 : :
161 : 4 : allocation_size = new_num_ele * ele_size;
162 : :
163 : 4 : tmp = realloc(*buffer, allocation_size);
164 [ + - ]: 4 : if (tmp != NULL) {
165 : 4 : *buffer = tmp;
166 : 4 : return new_num_ele;
167 : : }
168 : :
169 : 0 : return 0;
170 : : }
171 : :
172 : : static uint64_t
173 : 7 : fuzz_parse_args_into_array(const char *file, void **arr, size_t ele_size, const char *obj_name,
174 : : json_parse_fn cb_fn)
175 : : {
176 : : ssize_t i, num_json_values;
177 : 7 : struct spdk_json_val *values = NULL, *values_head = NULL, *obj_start;
178 : 7 : void *file_data = NULL;;
179 : : char *arr_idx_pointer;
180 : : size_t num_arr_elements, arr_elements_used, values_in_obj;
181 : : bool rc;
182 : :
183 : 7 : num_json_values = read_json_into_buffer(file, &values_head, &file_data);
184 : 7 : values = values_head;
185 [ + - - + ]: 7 : if (num_json_values == 0 || values == NULL) {
186 [ # # ]: 0 : if (file_data != NULL) {
187 : 0 : free(file_data);
188 : : }
189 [ # # ]: 0 : fprintf(stderr, "The file provided does not exist or we were unable to parse it.\n");
190 : 0 : return 0;
191 : : }
192 : :
193 : 7 : num_arr_elements = 10;
194 : 7 : arr_elements_used = 0;
195 : 7 : *arr = calloc(num_arr_elements, ele_size);
196 : 7 : arr_idx_pointer = (char *)*arr;
197 [ - + ]: 7 : if (arr_idx_pointer == NULL) {
198 : 0 : free(values);
199 : 0 : free(file_data);
200 : 0 : return 0;
201 : : }
202 : :
203 : 7 : i = 0;
204 [ + + ]: 430 : while (i < num_json_values) {
205 [ + + ]: 423 : if (values->type != SPDK_JSON_VAL_NAME) {
206 : 204 : i++;
207 : 204 : values++;
208 : 204 : continue;
209 : : }
210 : :
211 [ - + - + : 219 : if (!strncmp(values->start, obj_name, values->len)) {
+ + ]
212 : 69 : i++;
213 : 69 : values++;
214 [ - + ]: 69 : assert(values->type == SPDK_JSON_VAL_OBJECT_BEGIN);
215 : 69 : obj_start = values;
216 : 69 : values_in_obj = spdk_json_val_len(obj_start);
217 : 69 : values += values_in_obj;
218 : 69 : i += values_in_obj;
219 : :
220 : 69 : rc = cb_fn((void *)arr_idx_pointer, obj_start, values_in_obj);
221 [ - + ]: 69 : if (rc == false) {
222 [ # # ]: 0 : fprintf(stderr, "failed to parse file after %zu elements.\n", arr_elements_used);
223 : 0 : goto fail;
224 : : }
225 : :
226 : 69 : arr_idx_pointer += ele_size;
227 : 69 : arr_elements_used++;
228 [ + + ]: 69 : if (arr_elements_used == num_arr_elements) {
229 : 4 : num_arr_elements = double_arr_size(arr, num_arr_elements, ele_size);
230 [ - + ]: 4 : if (num_arr_elements == 0) {
231 [ # # ]: 0 : fprintf(stderr, "failed to allocate enough space for all json elements in your file.\n");
232 : 0 : goto fail;
233 : : } else {
234 : : /* reset the array element position in case the pointer changed. */
235 : 4 : arr_idx_pointer = ((char *)*arr) + arr_elements_used * ele_size;
236 : : }
237 : : }
238 : :
239 : 69 : continue;
240 : : } else {
241 : 150 : i++;
242 : 150 : values++;
243 : 150 : continue;
244 : : }
245 : : }
246 : :
247 [ - + ]: 7 : if (arr_elements_used == 0) {
248 : 0 : goto fail;
249 : : }
250 : :
251 : 7 : free(values_head);
252 : 7 : free(file_data);
253 : 7 : return arr_elements_used;
254 : 0 : fail:
255 : 0 : free(values_head);
256 : 0 : free(file_data);
257 : 0 : free(*arr);
258 : 0 : *arr = NULL;
259 : 0 : return 0;
260 : : }
261 : :
262 : : static int
263 : 971 : fuzz_parse_json_num(struct spdk_json_val *val, uint64_t max_val, uint64_t *val_ptr)
264 : : {
265 : 0 : uint64_t tmp_val;
266 : : int rc;
267 : :
268 : 971 : rc = spdk_json_number_to_uint64(val, &tmp_val);
269 [ + - - + ]: 971 : if (rc || tmp_val > max_val) {
270 : 0 : return -EINVAL;
271 : : } else {
272 : 971 : *val_ptr = tmp_val;
273 : 971 : return 0;
274 : : }
275 : : }
|