Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2025 Huawei Technologies Co., Ltd
3 : : */
4 : :
5 : : #include "bpf_impl.h"
6 : : #include "bpf_validate.h"
7 : : #include "bpf_validate_debug.h"
8 : :
9 : : #include <eal_export.h>
10 : : #include <rte_bpf_validate_debug.h>
11 : : #include <rte_errno.h>
12 : : #include <rte_per_lcore.h>
13 : :
14 : : #include <errno.h>
15 : : #include <stddef.h>
16 : : #include <stdlib.h>
17 : :
18 : : #ifndef LIST_FOREACH_SAFE
19 : : /* We need this macro which neither Linux nor EAL for Linux include yet. */
20 : : #define LIST_FOREACH_SAFE(var, head, field, tvar) \
21 : : for ((var) = LIST_FIRST((head)); \
22 : : (var) && ((tvar) = LIST_NEXT((var), field), 1); \
23 : : (var) = (tvar))
24 : : #endif
25 : :
26 : : #define EVENT_ARRAY_LENGTH RTE_BPF_VALIDATE_DEBUG_EVENT_END
27 : :
28 : : struct rte_bpf_validate_debug_point {
29 : : LIST_ENTRY(rte_bpf_validate_debug_point) list;
30 : : struct rte_bpf_validate_debug_callback callback;
31 : : uint32_t pc;
32 : : };
33 : :
34 : : LIST_HEAD(point_list, rte_bpf_validate_debug_point);
35 : :
36 : : struct rte_bpf_validate_debug {
37 : : /* Accessible immediately after object creation. */
38 : : struct point_list pending_breakpoints;
39 : : struct point_list *catchpoint_lists;
40 : : struct rte_bpf_validate_debug_callback step_callback;
41 : :
42 : : /* Accessible only after evaluate start. */
43 : : const struct bpf_verifier *verifier;
44 : : const struct rte_bpf_prm_ex *bpf_prm;
45 : : struct point_list *breakpoint_lists;
46 : : enum rte_bpf_validate_debug_event current_event;
47 : : struct rte_bpf_validate_debug_point *last_point;
48 : : uint32_t pc;
49 : : /* Evaluate stage (only tracking `evaluate` part at the moment). */
50 : : bool evaluate_started;
51 : : bool evaluate_finished;
52 : : int evaluate_result; /* Only valid if `evaluate_finished` is true. */
53 : : };
54 : :
55 : : /* Point lists functions. */
56 : :
57 : : /* Destroy all points in the list. */
58 : : static void
59 : : point_list_destroy(struct point_list *point_list)
60 : : {
61 : : struct rte_bpf_validate_debug_point *point, *next;
62 : :
63 [ + + + + ]: 6612 : LIST_FOREACH_SAFE(point, point_list, list, next)
64 : 1596 : rte_bpf_validate_debug_point_destroy(point);
65 : :
66 : : RTE_ASSERT(LIST_EMPTY(point_list));
67 : : }
68 : :
69 : : /* Destroy all points in all lists in the array and free the array. */
70 : : static void
71 : 418 : point_lists_destroy(struct point_list *point_lists, uint32_t length)
72 : : {
73 [ + - ]: 418 : if (point_lists == NULL)
74 : : return;
75 : :
76 [ + + ]: 5016 : for (uint32_t pli = 0; pli != length; ++pli)
77 : 4598 : point_list_destroy(&point_lists[pli]);
78 : :
79 : 418 : free(point_lists);
80 : : }
81 : :
82 : : /* Dynamically allocate and initialize an array of point lists. */
83 : : static struct point_list *
84 : 836 : point_lists_create(uint32_t length)
85 : : {
86 : : /* Allocate at least one element to avoid calloc(0, ...) shenanigans. */
87 : : struct point_list *const array =
88 : 836 : calloc(RTE_MAX(1u, length), sizeof(*array));
89 [ + - ]: 836 : if (array == NULL)
90 : : return NULL;
91 : :
92 [ + + ]: 10850 : for (uint32_t pli = 0; pli != length; ++pli)
93 : 10014 : LIST_INIT(&array[pli]);
94 : :
95 : : return array;
96 : : }
97 : :
98 : : /* Move point to a different list. */
99 : : static inline void
100 : : point_move(struct rte_bpf_validate_debug_point *point,
101 : : struct point_list *destination)
102 : : {
103 : 2253 : LIST_REMOVE(point, list);
104 [ + + + + : 4676 : LIST_INSERT_HEAD(destination, point, list);
+ + - + ]
105 : : }
106 : :
107 : : /* Move all points between lists (the order is inverted). */
108 : : static void
109 : : points_move(struct point_list *source, struct point_list *destination)
110 : : {
111 : : struct rte_bpf_validate_debug_point *point, *next;
112 : :
113 [ + + - + : 9759 : LIST_FOREACH_SAFE(point, source, list, next)
+ + + + +
+ + + ]
114 : : point_move(point, destination);
115 : : RTE_ASSERT(LIST_EMPTY(source));
116 : : }
117 : :
118 : : /* Pending breakpoints. */
119 : :
120 : : /* Return true if all pending breakpoints have pc less than nb_ins. */
121 : : static bool
122 : : debug_pending_breakpoints_are_valid(const struct rte_bpf_validate_debug *debug,
123 : : uint32_t nb_ins)
124 : : {
125 : : const struct rte_bpf_validate_debug_point *breakpoint;
126 : :
127 [ + + ]: 1587 : LIST_FOREACH(breakpoint, &debug->pending_breakpoints, list)
128 [ + - ]: 1169 : if (breakpoint->pc >= nb_ins)
129 : : return false;
130 : :
131 : : return true;
132 : : }
133 : :
134 : : /* Move all pending breakpoints to correct per-pc lists. */
135 : : static void
136 : 418 : debug_pending_breakpoints_restore(struct rte_bpf_validate_debug *debug)
137 : : {
138 : : struct rte_bpf_validate_debug_point *breakpoint, *next;
139 : : struct point_list breakpoints;
140 : :
141 : : /* Invert the list first to preserve point order when we move them. */
142 : 418 : LIST_INIT(&breakpoints);
143 : : points_move(&debug->pending_breakpoints, &breakpoints);
144 : :
145 [ + + ]: 1587 : LIST_FOREACH_SAFE(breakpoint, &breakpoints, list, next)
146 [ + + ]: 1169 : point_move(breakpoint, &debug->breakpoint_lists[breakpoint->pc]);
147 : : RTE_ASSERT(LIST_EMPTY(&breakpoints));
148 : 418 : }
149 : :
150 : : /* Move all breakpoints from per-pc lists to the pending one. */
151 : : static void
152 : 418 : debug_pending_breakpoints_save(struct rte_bpf_validate_debug *debug)
153 : : {
154 : : struct point_list breakpoints;
155 : :
156 : 418 : LIST_INIT(&breakpoints);
157 [ + + ]: 5834 : for (uint32_t pc = 0; pc != debug->bpf_prm->raw.nb_ins; ++pc)
158 : 5416 : points_move(&debug->breakpoint_lists[pc], &breakpoints);
159 : :
160 : : /* Invert the list to restore point order after we moved them. */
161 : : RTE_ASSERT(LIST_EMPTY(&debug->pending_breakpoints));
162 : : points_move(&breakpoints, &debug->pending_breakpoints);
163 : 418 : }
164 : :
165 : : /* Debug instance creation and destruction. */
166 : :
167 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_validate_debug_destroy, 26.07)
168 : : void
169 : 418 : rte_bpf_validate_debug_destroy(struct rte_bpf_validate_debug *debug)
170 : : {
171 [ + - ]: 418 : if (debug == NULL)
172 : : return;
173 : :
174 : : /* Cannot destroy the instance during validation. */
175 : : RTE_ASSERT(!debug->evaluate_started);
176 : :
177 : 418 : point_lists_destroy(debug->catchpoint_lists, EVENT_ARRAY_LENGTH);
178 : : point_list_destroy(&debug->pending_breakpoints);
179 : 418 : free(debug);
180 : : }
181 : :
182 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_validate_debug_create, 26.07)
183 : : struct rte_bpf_validate_debug *
184 : 418 : rte_bpf_validate_debug_create(void)
185 : : {
186 : 418 : struct rte_bpf_validate_debug *const debug = calloc(1, sizeof(*debug));
187 [ - + ]: 418 : if (debug == NULL) {
188 : 0 : rte_errno = ENOMEM;
189 : 0 : return NULL;
190 : : }
191 : :
192 : : LIST_INIT(&debug->pending_breakpoints);
193 : :
194 : 418 : debug->catchpoint_lists = point_lists_create(EVENT_ARRAY_LENGTH);
195 [ - + ]: 418 : if (debug->catchpoint_lists == NULL) {
196 : 0 : free(debug);
197 : 0 : rte_errno = ENOMEM;
198 : 0 : return NULL;
199 : : }
200 : :
201 : : return debug;
202 : : }
203 : :
204 : : /* Managing callbacks. */
205 : :
206 : : /* Call back the user function with correct arguments for a point. */
207 : : static inline int
208 : : debug_point_call_back(struct rte_bpf_validate_debug *debug,
209 : : struct rte_bpf_validate_debug_point *point)
210 : : {
211 : 6864 : debug->last_point = point;
212 : 6864 : return point->callback.fn(debug, point->callback.ctx);
213 : : }
214 : :
215 : : /* Call back all points in point_list. */
216 : : static int
217 : : debug_points_call_back(struct rte_bpf_validate_debug *debug,
218 : : const struct point_list *point_list)
219 : : {
220 : : struct rte_bpf_validate_debug_point *point, *next;
221 : : int rc = 0;
222 : :
223 [ + + + + : 39896 : LIST_FOREACH_SAFE(point, point_list, list, next)
+ + + + ]
224 [ + - + - : 6864 : rc = rc < 0 ? rc : debug_point_call_back(debug, point);
+ - + - ]
225 : :
226 : : return rc;
227 : : }
228 : :
229 : : /* Call back all breakpoints for the specified program counter. */
230 : : static int
231 : : debug_trigger_breakpoints(struct rte_bpf_validate_debug *debug, uint32_t pc)
232 : : {
233 : 7107 : return debug_points_call_back(debug, &debug->breakpoint_lists[pc]);
234 : : }
235 : :
236 : : /* Call back all catchpoints for the specified event. */
237 : : static int
238 : : debug_send_event(struct rte_bpf_validate_debug *debug, debug_event_t event)
239 : : {
240 : 25925 : debug->current_event = event;
241 : 25507 : return debug_points_call_back(debug, &debug->catchpoint_lists[event]);
242 : : }
243 : :
244 : : /* Create new point and insert it into the specified list. */
245 : : static struct rte_bpf_validate_debug_point *
246 : 1596 : point_list_insert(struct point_list *point_list,
247 : : const struct rte_bpf_validate_debug_callback *callback, uint32_t pc)
248 : : {
249 : : struct rte_bpf_validate_debug_point *const point =
250 : 1596 : malloc(sizeof(*point));
251 [ - + ]: 1596 : if (point == NULL) {
252 : 0 : rte_errno = ENOMEM;
253 : 0 : return NULL;
254 : : }
255 : :
256 [ + + ]: 1596 : LIST_INSERT_HEAD(point_list, point, list);
257 : 1596 : point->callback = *callback;
258 : 1596 : point->pc = pc;
259 : 1596 : return point;
260 : : }
261 : :
262 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_validate_debug_break, 26.07)
263 : : struct rte_bpf_validate_debug_point *
264 : 1169 : rte_bpf_validate_debug_break(struct rte_bpf_validate_debug *debug, uint32_t pc,
265 : : const struct rte_bpf_validate_debug_callback *callback)
266 : : {
267 [ + - - + ]: 1169 : if (debug == NULL || callback == NULL || callback->fn == NULL) {
268 : 0 : rte_errno = EINVAL;
269 : 0 : return NULL;
270 : : }
271 : :
272 [ + - ]: 1169 : if (!debug->evaluate_started)
273 : 1169 : return point_list_insert(&debug->pending_breakpoints,
274 : : callback, pc);
275 : :
276 [ # # ]: 0 : if (pc >= debug->bpf_prm->raw.nb_ins) {
277 : 0 : rte_errno = ENOENT;
278 : 0 : return NULL;
279 : : }
280 : :
281 : 0 : return point_list_insert(&debug->breakpoint_lists[pc], callback, pc);
282 : : }
283 : :
284 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_validate_debug_catch, 26.07)
285 : : struct rte_bpf_validate_debug_point *
286 : 427 : rte_bpf_validate_debug_catch(struct rte_bpf_validate_debug *debug,
287 : : debug_event_t event, const struct rte_bpf_validate_debug_callback *callback)
288 : : {
289 [ + - + - ]: 427 : if (debug == NULL || callback == NULL || callback->fn == NULL ||
290 [ - + ]: 427 : event < 0 || event >= RTE_BPF_VALIDATE_DEBUG_EVENT_END) {
291 : 0 : rte_errno = EINVAL;
292 : 0 : return NULL;
293 : : }
294 : :
295 : 427 : return point_list_insert(&debug->catchpoint_lists[event], callback, 0);
296 : : }
297 : :
298 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_validate_debug_point_destroy, 26.07)
299 : : void
300 : 1596 : rte_bpf_validate_debug_point_destroy(struct rte_bpf_validate_debug_point *point)
301 : : {
302 [ + - ]: 1596 : if (point == NULL)
303 : : return;
304 : :
305 [ + + ]: 1596 : LIST_REMOVE(point, list);
306 : 1596 : free(point);
307 : : }
308 : :
309 : : /* Querying execution state. */
310 : :
311 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_validate_debug_get_bpf_param, 26.07)
312 : : const struct rte_bpf_prm_ex *
313 : 0 : rte_bpf_validate_debug_get_bpf_param(const struct rte_bpf_validate_debug *debug)
314 : : {
315 [ # # ]: 0 : if (debug == NULL) {
316 : 0 : rte_errno = EINVAL;
317 : 0 : return NULL;
318 : : }
319 : :
320 [ # # ]: 0 : if (!debug->evaluate_started) {
321 : 0 : rte_errno = ECHILD;
322 : 0 : return NULL;
323 : : }
324 : :
325 : 0 : return debug->bpf_prm;
326 : : }
327 : :
328 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_validate_debug_get_ins, 26.07)
329 : : int
330 : 0 : rte_bpf_validate_debug_get_ins(const struct rte_bpf_validate_debug *debug,
331 : : const struct rte_ebpf_insn **ins, uint32_t *nb_ins)
332 : : {
333 [ # # ]: 0 : if (debug == NULL)
334 : : return -EINVAL;
335 : :
336 [ # # ]: 0 : if (!debug->evaluate_started)
337 : : return -ECHILD;
338 : :
339 [ # # ]: 0 : if (debug->bpf_prm->origin != RTE_BPF_ORIGIN_RAW)
340 : : return -ENOTSUP;
341 : :
342 : 0 : *ins = debug->bpf_prm->raw.ins;
343 : 0 : *nb_ins = debug->bpf_prm->raw.nb_ins;
344 : 0 : return 0;
345 : : }
346 : :
347 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_validate_debug_get_event, 26.11)
348 : : enum rte_bpf_validate_debug_event
349 : 28 : rte_bpf_validate_debug_get_event(const struct rte_bpf_validate_debug *debug)
350 : : {
351 [ + - ]: 28 : if (debug == NULL)
352 : : /* Just to be fool-proof, not really required by API. */
353 : : return -EINVAL;
354 : :
355 : 28 : return debug->current_event;
356 : : }
357 : :
358 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_validate_debug_get_last_point, 26.07)
359 : : struct rte_bpf_validate_debug_point *
360 : 0 : rte_bpf_validate_debug_get_last_point(const struct rte_bpf_validate_debug *debug)
361 : : {
362 [ # # ]: 0 : if (debug == NULL) {
363 : 0 : rte_errno = EINVAL;
364 : 0 : return NULL;
365 : : }
366 : :
367 : 0 : return debug->last_point;
368 : : }
369 : :
370 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_validate_debug_get_pc, 26.07)
371 : : uint32_t
372 : 6864 : rte_bpf_validate_debug_get_pc(const struct rte_bpf_validate_debug *debug)
373 : : {
374 [ + - + - ]: 6864 : if (debug == NULL || !debug->evaluate_started)
375 : : return UINT32_MAX;
376 : :
377 : 6864 : return debug->pc;
378 : : }
379 : :
380 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_validate_debug_get_validation_result, 26.07)
381 : : int
382 : 0 : rte_bpf_validate_debug_get_validation_result(const struct rte_bpf_validate_debug *debug,
383 : : int *result)
384 : : {
385 [ # # ]: 0 : if (debug == NULL)
386 : : return -EINVAL;
387 : :
388 [ # # ]: 0 : if (!debug->evaluate_finished)
389 : : return -EAGAIN;
390 : :
391 : 0 : *result = debug->evaluate_result;
392 : :
393 : 0 : return 0;
394 : : }
395 : :
396 : : /* Querying VM state. */
397 : :
398 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_validate_debug_can_access, 26.07)
399 : : int
400 : 96 : rte_bpf_validate_debug_can_access(const struct rte_bpf_validate_debug *debug,
401 : : const struct rte_ebpf_insn *access, uint64_t off64)
402 : : {
403 [ + - ]: 96 : if (debug == NULL || access == NULL)
404 : : return -EINVAL;
405 : :
406 [ + - ]: 96 : if (!debug->evaluate_started)
407 : : return -ECHILD;
408 : :
409 : 96 : return __rte_bpf_validate_can_access(debug->verifier, access, off64);
410 : : }
411 : :
412 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_validate_debug_may_jump, 26.07)
413 : : int
414 : 14656 : rte_bpf_validate_debug_may_jump(const struct rte_bpf_validate_debug *debug,
415 : : const struct rte_ebpf_insn *jump, uint64_t imm64)
416 : : {
417 [ + - ]: 14656 : if (debug == NULL || jump == NULL)
418 : : return -EINVAL;
419 : :
420 [ + - ]: 14656 : if (!debug->evaluate_started)
421 : : return -ECHILD;
422 : :
423 : 14656 : return __rte_bpf_validate_may_jump(debug->verifier, jump, imm64);
424 : : }
425 : :
426 : : /* Formatting VM state for user. */
427 : :
428 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_validate_debug_format_register_info, 26.07)
429 : : int
430 : 1839 : rte_bpf_validate_debug_format_register_info(const struct rte_bpf_validate_debug *debug,
431 : : char *buffer, size_t bufsz, uint8_t reg)
432 : : {
433 [ + - ]: 1839 : if (debug == NULL)
434 : : return -EINVAL;
435 : :
436 [ + - ]: 1839 : if (!debug->evaluate_started)
437 : : return -ECHILD;
438 : :
439 : 1839 : return __rte_bpf_validate_format_register_info(debug->verifier, buffer,
440 : : bufsz, reg);
441 : : }
442 : :
443 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_validate_debug_format_frame_info, 26.07)
444 : : int
445 : 0 : rte_bpf_validate_debug_format_frame_info(const struct rte_bpf_validate_debug *debug,
446 : : char *buffer, size_t bufsz, int32_t offset)
447 : : {
448 [ # # ]: 0 : if (debug == NULL)
449 : : return -EINVAL;
450 : :
451 [ # # ]: 0 : if (!debug->evaluate_started)
452 : : return -ECHILD;
453 : :
454 : 0 : return __rte_bpf_validate_format_frame_info(debug->verifier, buffer,
455 : : bufsz, offset);
456 : : }
457 : :
458 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_validate_debug_get_frame_size, 26.07)
459 : : int32_t
460 : 0 : rte_bpf_validate_debug_get_frame_size(const struct rte_bpf_validate_debug *debug)
461 : : {
462 [ # # ]: 0 : if (debug == NULL)
463 : : return -EINVAL;
464 : :
465 [ # # ]: 0 : if (!debug->evaluate_started)
466 : : return -ECHILD;
467 : :
468 : 0 : return __rte_bpf_validate_get_frame_size(debug->verifier);
469 : : }
470 : :
471 : : /* Courtesy formatting functions for user-supplied values. */
472 : :
473 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_validate_debug_format_value, 26.07)
474 : : int
475 : 3850 : rte_bpf_validate_debug_format_value(char *buffer, size_t bufsz, char format,
476 : : uint64_t value)
477 : : {
478 : : static const struct {
479 : : uint64_t value;
480 : : const char *name;
481 : : } constants[] = {
482 : : { .value = INT64_MIN, .name = "INT64_MIN" },
483 : : { .value = INT32_MIN, .name = "INT32_MIN" },
484 : : { .value = INT16_MIN, .name = "INT16_MIN" },
485 : : { .value = INT8_MIN, .name = "INT8_MIN" },
486 : : { .value = INT8_MAX, .name = "INT8_MAX" },
487 : : { .value = UINT8_MAX, .name = "UINT8_MAX" },
488 : : { .value = INT16_MAX, .name = "INT16_MAX" },
489 : : { .value = UINT16_MAX, .name = "UINT16_MAX" },
490 : : { .value = INT32_MAX, .name = "INT32_MAX" },
491 : : { .value = UINT32_MAX, .name = "UINT32_MAX" },
492 : : { .value = INT64_MAX, .name = "INT64_MAX" },
493 : : /* UINT64_MAX omitted on purpose, it looks better as -1 */
494 : : };
495 : :
496 [ + + - ]: 3850 : switch (format) {
497 : : case 'd':
498 [ + + ]: 36533 : for (int ci = 0; ci != RTE_DIM(constants); ++ci)
499 [ + + ]: 33520 : if (constants[ci].value == value)
500 : 79 : return snprintf(buffer, bufsz, "%s", constants[ci].name);
501 : : /*
502 : : * Special case numbers close to int32_t or int64_t range ends,
503 : : * since they are hard to recognize in decimal otherwise.
504 : : */
505 [ + + ]: 3013 : if (value - INT64_MIN < 1000000)
506 : 7 : return snprintf(buffer, bufsz, "INT64_MIN+%" PRId64,
507 : : value - INT64_MIN);
508 [ + + ]: 3006 : if (INT64_MAX - value < 1000000)
509 : 6 : return snprintf(buffer, bufsz, "INT64_MAX-%" PRId64,
510 : : INT64_MAX - value);
511 [ - + ]: 3000 : if (value - INT32_MIN < 1000)
512 : 0 : return snprintf(buffer, bufsz, "INT32_MIN+%" PRId64,
513 : : value - INT32_MIN);
514 [ - + ]: 3000 : if (INT32_MAX - value < 1000)
515 : 0 : return snprintf(buffer, bufsz, "INT32_MAX-%" PRId64,
516 : : INT32_MAX - value);
517 : 3000 : return snprintf(buffer, bufsz, "%" PRId64, value);
518 : 758 : case 'x':
519 : : /* Special case only the common case of UINT64_MAX. */
520 [ + + ]: 758 : if (value == UINT64_MAX)
521 : 24 : return snprintf(buffer, bufsz, "%s", "UINT64_MAX");
522 : 734 : return snprintf(buffer, bufsz, "%#" PRIx64, value);
523 : : default:
524 : : return -EINVAL;
525 : : }
526 : : }
527 : :
528 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_validate_debug_format_interval, 26.07)
529 : : int
530 : 2172 : rte_bpf_validate_debug_format_interval(char *buffer, size_t bufsz, char format,
531 : : uint64_t min, uint64_t max)
532 : : {
533 : : char min_buffer[32], max_buffer[32];
534 : : int rc;
535 : :
536 [ + + ]: 2172 : if (min == max)
537 : 494 : return rte_bpf_validate_debug_format_value(buffer, bufsz, format, min);
538 : :
539 : 1678 : rc = rte_bpf_validate_debug_format_value(min_buffer, sizeof(min_buffer), format, min);
540 [ + - ]: 1678 : if (rc < 0)
541 : : return rc;
542 : :
543 : 1678 : rc = rte_bpf_validate_debug_format_value(max_buffer, sizeof(max_buffer), format, max);
544 [ + - ]: 1678 : if (rc < 0)
545 : : return rc;
546 : :
547 : 1678 : return snprintf(buffer, bufsz, "%s..%s", min_buffer, max_buffer);
548 : : }
549 : :
550 : : /* Evaluation start and finish. */
551 : :
552 : : /* Free all resources associated with current evaluation. */
553 : : static void
554 : : debug_evaluate_close(struct rte_bpf_validate_debug *debug)
555 : : {
556 : : RTE_ASSERT(debug->evaluate_started);
557 : 418 : debug_pending_breakpoints_save(debug);
558 : 418 : free(debug->breakpoint_lists);
559 : 418 : debug->breakpoint_lists = NULL;
560 : 418 : debug->evaluate_started = false;
561 : : }
562 : :
563 : : int
564 : 838 : __rte_bpf_validate_debug_evaluate_start(struct rte_bpf_validate_debug *debug,
565 : : const struct bpf_verifier *verifier, const struct rte_bpf_prm_ex *bpf_prm)
566 : : {
567 [ + + ]: 838 : if (debug == NULL)
568 : : return 0;
569 : :
570 [ + - ]: 418 : if (verifier == NULL || bpf_prm == NULL ||
571 [ + - ]: 418 : bpf_prm->origin != RTE_BPF_ORIGIN_RAW)
572 : : return -EINVAL;
573 : :
574 [ - + ]: 418 : if (debug->evaluate_started) {
575 : 0 : RTE_BPF_LOG_FUNC_LINE(ERR, "already started");
576 : 0 : return -EEXIST;
577 : : }
578 : :
579 [ + - ]: 836 : if (!debug_pending_breakpoints_are_valid(debug, bpf_prm->raw.nb_ins))
580 : : return -ENOENT;
581 : :
582 : 418 : debug->verifier = verifier;
583 : 418 : debug->bpf_prm = bpf_prm;
584 : 418 : debug->breakpoint_lists = point_lists_create(bpf_prm->raw.nb_ins);
585 [ + - ]: 418 : if (debug->breakpoint_lists == NULL)
586 : : return -ENOMEM;
587 : 418 : debug_pending_breakpoints_restore(debug);
588 : 418 : debug->last_point = NULL;
589 : 418 : debug->pc = 0;
590 : 418 : debug->evaluate_started = true;
591 : 418 : debug->evaluate_finished = false;
592 : :
593 : : const int rc = debug_send_event(debug,
594 : : RTE_BPF_VALIDATE_DEBUG_EVENT_VALIDATION_START);
595 [ - + ]: 418 : if (rc < 0) {
596 : : debug_evaluate_close(debug);
597 : 0 : return rc;
598 : : }
599 : :
600 : 418 : RTE_BPF_LOG_FUNC_LINE(DEBUG, "evaluate started");
601 : 418 : return 0;
602 : : }
603 : :
604 : : int
605 : 17369 : __rte_bpf_validate_debug_evaluate_update(struct rte_bpf_validate_debug *debug,
606 : : uint32_t pc, uint64_t events)
607 : : {
608 : : /* Required order of sent events according to the documentation. */
609 : : static const enum rte_bpf_validate_debug_event ordered_events[] = {
610 : : RTE_BPF_VALIDATE_DEBUG_EVENT_VALIDATION_START,
611 : : RTE_BPF_VALIDATE_DEBUG_EVENT_INVALID_STATE,
612 : : RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_ENTER,
613 : : RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_PRUNE,
614 : : RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_RETURN,
615 : : RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_UNREACHABLE,
616 : : RTE_BPF_VALIDATE_DEBUG_EVENT_JUMP_ALWAYS,
617 : : RTE_BPF_VALIDATE_DEBUG_EVENT_JUMP_CONDITIONAL,
618 : : RTE_BPF_VALIDATE_DEBUG_EVENT_STEP,
619 : : RTE_BPF_VALIDATE_DEBUG_EVENT_VALIDATION_SUCCESS,
620 : : RTE_BPF_VALIDATE_DEBUG_EVENT_VALIDATION_FAILURE,
621 : : };
622 : : RTE_BUILD_BUG_ON(
623 : : RTE_DIM(ordered_events) != RTE_BPF_VALIDATE_DEBUG_EVENT_END);
624 : :
625 : : int rc;
626 : :
627 [ + + ]: 17369 : if (debug == NULL)
628 : : return 0;
629 : :
630 [ - + ]: 12033 : if (!debug->evaluate_started) {
631 : 0 : RTE_BPF_LOG_FUNC_LINE(ERR, "not started");
632 : 0 : return -ECHILD;
633 : : }
634 : :
635 [ + - ]: 12033 : if (pc >= debug->bpf_prm->raw.nb_ins)
636 : : return -EINVAL;
637 : :
638 : 12033 : debug->pc = pc;
639 : :
640 : 12033 : rc = __rte_bpf_validate_state_is_valid(debug->verifier);
641 [ + + ]: 12033 : if (rc == 0)
642 : 5832 : events |= RTE_BIT64(RTE_BPF_VALIDATE_DEBUG_EVENT_INVALID_STATE);
643 : :
644 [ + + ]: 144396 : for (uint32_t index = 0; index < RTE_DIM(ordered_events); index++) {
645 : 132363 : const enum rte_bpf_validate_debug_event event =
646 : : ordered_events[index];
647 [ + + ]: 132363 : if ((events & RTE_BIT64(event)) == 0)
648 : 107274 : continue;
649 : :
650 [ + + ]: 25089 : if (event == RTE_BPF_VALIDATE_DEBUG_EVENT_STEP)
651 [ + - ]: 7107 : rc = rc < 0 ? rc : debug_trigger_breakpoints(debug, pc);
652 : :
653 [ + - ]: 25089 : rc = rc < 0 ? rc : debug_send_event(debug, event);
654 : 25089 : events -= RTE_BIT64(event);
655 : : }
656 : :
657 [ - + ]: 12033 : if (events != 0)
658 : : /* Received unsupported events. */
659 [ # # ]: 0 : rc = rc < 0 ? rc : -EINVAL;
660 : :
661 : : return rc;
662 : : }
663 : :
664 : : int
665 : 838 : __rte_bpf_validate_debug_evaluate_finish(struct rte_bpf_validate_debug *debug,
666 : : int result)
667 : : {
668 : : int rc = 0;
669 : :
670 [ + + ]: 838 : if (debug == NULL)
671 : : return 0;
672 : :
673 [ - + ]: 418 : if (!debug->evaluate_started) {
674 : 0 : RTE_BPF_LOG_FUNC_LINE(ERR, "not started");
675 : 0 : return -ECHILD;
676 : : }
677 : :
678 : 418 : debug->evaluate_finished = true;
679 : 418 : debug->evaluate_result = result;
680 : :
681 [ + - ]: 418 : if (result != -ECANCELED)
682 [ + - ]: 418 : rc = debug_send_event(debug, result < 0 ?
683 : : RTE_BPF_VALIDATE_DEBUG_EVENT_VALIDATION_FAILURE :
684 : : RTE_BPF_VALIDATE_DEBUG_EVENT_VALIDATION_SUCCESS);
685 : :
686 : : debug_evaluate_close(debug);
687 : :
688 : 418 : return rc;
689 : : }
|