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_def.h"
6 : : #include "test.h"
7 : :
8 : : #include <rte_bpf.h>
9 : : #include <rte_bpf_validate_debug.h>
10 : : #include <rte_errno.h>
11 : :
12 : : extern int test_bpf_validate_logtype;
13 : : #define RTE_LOGTYPE_TEST_BPF_VALIDATE test_bpf_validate_logtype
14 : : #define TEST_LOG_LINE(level, ...) \
15 : : RTE_LOG_LINE(level, TEST_BPF_VALIDATE, "" __VA_ARGS__)
16 : :
17 [ - + ]: 305 : RTE_LOG_REGISTER(test_bpf_validate_logtype, test.bpf_validate, NOTICE);
18 : :
19 : : /* Special value indicating that program counter variable is not being used. */
20 : : #define NO_PROGRAM_COUNTER UINT32_MAX
21 : :
22 : : /* Special value indicating that register variable is not being used. */
23 : : #define NO_REGISTER UINT8_MAX
24 : :
25 : : static const char *const event_names[RTE_BPF_VALIDATE_DEBUG_EVENT_END] = {
26 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_STEP] = "step",
27 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_INVALID_STATE] = "invalid-state",
28 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_VALIDATION_START] = "validation-start",
29 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_VALIDATION_SUCCESS] = "validation-success",
30 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_VALIDATION_FAILURE] = "validation-failure",
31 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_ENTER] = "branch-enter",
32 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_PRUNE] = "branch-prune",
33 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_RETURN] = "branch-return",
34 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_UNREACHABLE] = "branch-unreachable",
35 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_JUMP_ALWAYS] = "jump-always",
36 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_JUMP_CONDITIONAL] = "jump-conditional",
37 : : };
38 : :
39 : : /*
40 : : * Self-test of BPF validation debug events.
41 : : */
42 : :
43 : : #define EVENT_BIT(name) RTE_BIT64(RTE_BPF_VALIDATE_DEBUG_EVENT_ ## name)
44 : : #define EVENT_BITS_ALL RTE_LEN2MASK(RTE_BPF_VALIDATE_DEBUG_EVENT_END, uint64_t)
45 : :
46 : : struct test_events_context {
47 : : /* Tested program. */
48 : : const struct rte_ebpf_insn *ins;
49 : : uint32_t nb_ins;
50 : : /* Number of times each of the events was encountered. */
51 : : int event_counts[RTE_BPF_VALIDATE_DEBUG_EVENT_END];
52 : : /* Events active within the current step. */
53 : : uint64_t active_events;
54 : : /* Breakpoint was called within the current step. */
55 : : bool breakpoint_called;
56 : : /* Currently active conditional jump pc. */
57 : : uint32_t branch_pc;
58 : : /* Program counters of all conditional jumps in the current code path. */
59 : : uint32_t branch_pc_stack[4];
60 : : size_t branch_pc_stack_length;
61 : : };
62 : :
63 : : /* Push current branch_pc to stack and clear it. */
64 : : static int
65 : 4 : test_events_branch_pc_push(struct test_events_context *ctx)
66 : : {
67 [ - + ]: 4 : TEST_ASSERT_NOT_EQUAL(ctx->branch_pc, NO_PROGRAM_COUNTER,
68 : : "no branch pc to push");
69 [ - + ]: 4 : TEST_ASSERT(ctx->branch_pc_stack_length < RTE_DIM(ctx->branch_pc_stack),
70 : : "branch stack overflow");
71 : 4 : ctx->branch_pc_stack[ctx->branch_pc_stack_length++] = ctx->branch_pc;
72 : 4 : ctx->branch_pc = NO_PROGRAM_COUNTER;
73 : 4 : return TEST_SUCCESS;
74 : : }
75 : :
76 : : /* Pop branch_pc from stack. */
77 : : static int
78 : 4 : test_events_branch_pc_pop(struct test_events_context *ctx)
79 : : {
80 [ - + ]: 4 : TEST_ASSERT(ctx->branch_pc_stack_length > 0, "branch stack underflow");
81 : 4 : ctx->branch_pc = ctx->branch_pc_stack[--ctx->branch_pc_stack_length];
82 : 4 : return TEST_SUCCESS;
83 : : }
84 : :
85 : : /* Verify breakpoint, checking that it is only called once and remembering it. */
86 : : static int
87 : 9 : test_events_break_cb(struct rte_bpf_validate_debug *debug, void *void_ctx)
88 : : {
89 : : struct test_events_context *const ctx = void_ctx;
90 : 9 : const uint32_t pc = rte_bpf_validate_debug_get_pc(debug);
91 : :
92 : 9 : TEST_LOG_LINE(DEBUG, "%2u: break", pc);
93 : :
94 [ - + ]: 9 : TEST_ASSERT(!ctx->breakpoint_called,
95 : : "breakpoint called again at pc %u", pc);
96 : 9 : ctx->breakpoint_called = true;
97 : :
98 : 9 : return TEST_SUCCESS;
99 : : }
100 : :
101 : : /*
102 : : * Verify event, checking that:
103 : : * - validation-start is called at pc 0;
104 : : * - branch-return is called at a corresponding conditional jump pc;
105 : : * - branch-enter and branch-return are correctly paired;
106 : : * - jump-conditional and jump-always are called for corresponding opcodes;
107 : : * - jump-conditional and only it is followed by branches events;
108 : : * - events and breakpoints are correctly ordered;
109 : : *
110 : : * Increment event counter to compare against expected counts later.
111 : : */
112 : : static int
113 : 28 : test_events_catch_cb(struct rte_bpf_validate_debug *debug, void *void_ctx)
114 : : {
115 : : /* Bitmask of events allowed to precede each given event within the step. */
116 : : static const uint64_t allowed_preceding_events[] = {
117 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_VALIDATION_START] = 0,
118 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_PRUNE] =
119 : : EVENT_BIT(BRANCH_PRUNE) |
120 : : EVENT_BIT(BRANCH_UNREACHABLE) |
121 : : EVENT_BIT(BRANCH_RETURN),
122 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_UNREACHABLE] =
123 : : EVENT_BIT(BRANCH_PRUNE) |
124 : : EVENT_BIT(BRANCH_UNREACHABLE) |
125 : : EVENT_BIT(BRANCH_RETURN),
126 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_ENTER] =
127 : : EVENT_BIT(BRANCH_PRUNE) |
128 : : EVENT_BIT(BRANCH_UNREACHABLE) |
129 : : EVENT_BIT(BRANCH_RETURN),
130 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_RETURN] =
131 : : EVENT_BIT(BRANCH_PRUNE) |
132 : : EVENT_BIT(BRANCH_UNREACHABLE) |
133 : : EVENT_BIT(BRANCH_RETURN),
134 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_JUMP_CONDITIONAL] =
135 : : EVENT_BIT(BRANCH_ENTER) |
136 : : EVENT_BIT(BRANCH_PRUNE) |
137 : : EVENT_BIT(BRANCH_UNREACHABLE) |
138 : : EVENT_BIT(BRANCH_RETURN),
139 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_JUMP_ALWAYS] =
140 : : EVENT_BIT(BRANCH_ENTER) |
141 : : EVENT_BIT(BRANCH_PRUNE) |
142 : : EVENT_BIT(BRANCH_UNREACHABLE) |
143 : : EVENT_BIT(BRANCH_RETURN),
144 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_STEP] =
145 : : EVENT_BITS_ALL - EVENT_BIT(VALIDATION_SUCCESS),
146 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_VALIDATION_SUCCESS] =
147 : : EVENT_BIT(BRANCH_PRUNE) |
148 : : EVENT_BIT(BRANCH_UNREACHABLE) |
149 : : EVENT_BIT(BRANCH_RETURN),
150 : : };
151 : :
152 : : struct test_events_context *const ctx = void_ctx;
153 : : const enum rte_bpf_validate_debug_event event =
154 : 28 : rte_bpf_validate_debug_get_event(debug);
155 : 28 : const uint32_t pc = rte_bpf_validate_debug_get_pc(debug);
156 : :
157 : 28 : TEST_LOG_LINE(DEBUG, "%2u: %s", pc, event_names[event]);
158 : :
159 : : /* Verify event program counter and underlying instruction. */
160 [ + + + + : 28 : switch (event) {
+ + ]
161 : 1 : case RTE_BPF_VALIDATE_DEBUG_EVENT_VALIDATION_START:
162 [ - + ]: 1 : TEST_ASSERT_EQUAL(pc, 0,
163 : : "event %s called at pc %u",
164 : : event_names[event], pc);
165 : : break;
166 : 4 : case RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_ENTER:
167 [ - + ]: 4 : TEST_ASSERT_SUCCESS(test_events_branch_pc_push(ctx),
168 : : "error pushing branch for event %s at pc %u",
169 : : event_names[event], pc);
170 : : break;
171 : 4 : case RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_RETURN:
172 [ - + ]: 4 : TEST_ASSERT_SUCCESS(test_events_branch_pc_pop(ctx),
173 : : "error popping branch for event %s at pc %u",
174 : : event_names[event], pc);
175 [ - + ]: 4 : TEST_ASSERT_EQUAL(pc, ctx->branch_pc,
176 : : "event %s called at pc %u after jump pc %u",
177 : : event_names[event], pc, ctx->branch_pc);
178 : : break;
179 : 1 : case RTE_BPF_VALIDATE_DEBUG_EVENT_JUMP_ALWAYS:
180 [ - + ]: 1 : TEST_ASSERT_EQUAL(BPF_CLASS(ctx->ins[pc].code), BPF_JMP,
181 : : "event %s called at non-jump pc %u",
182 : : event_names[event], pc);
183 [ - + ]: 1 : TEST_ASSERT_EQUAL(BPF_OP(ctx->ins[pc].code), BPF_JA,
184 : : "event %s called at non-jump-always pc %u",
185 : : event_names[event], pc);
186 : : break;
187 : 4 : case RTE_BPF_VALIDATE_DEBUG_EVENT_JUMP_CONDITIONAL:
188 [ - + ]: 4 : TEST_ASSERT_EQUAL(BPF_CLASS(ctx->ins[pc].code), BPF_JMP,
189 : : "event %s called at non-jump pc %u",
190 : : event_names[event], pc);
191 [ - + ]: 4 : TEST_ASSERT_NOT_EQUAL(BPF_OP(ctx->ins[pc].code), BPF_JA,
192 : : "event %s called at jump-always pc %u",
193 : : event_names[event], pc);
194 : : break;
195 : : default:
196 : : break;
197 : : }
198 : :
199 : : /* Verify events order. */
200 [ - + ]: 28 : TEST_ASSERT_EQUAL(
201 : : (ctx->active_events & ~allowed_preceding_events[event]), 0,
202 : : "event %s at pc %u preceded by %#jx",
203 : : event_names[event], pc, (uintmax_t)ctx->active_events);
204 : :
205 : : /* Update events for the current step as well as branch program counter. */
206 [ + + ]: 28 : if (event != RTE_BPF_VALIDATE_DEBUG_EVENT_STEP) {
207 [ - + ]: 19 : TEST_ASSERT(!ctx->breakpoint_called,
208 : : "event %s called after a breakpoint at pc %u",
209 : : event_names[event], pc);
210 : 19 : ctx->active_events |= RTE_BIT64(event);
211 : : } else {
212 [ - + ]: 9 : TEST_ASSERT_EQUAL(ctx->branch_pc, NO_PROGRAM_COUNTER,
213 : : "no branch-enter between branch- and next instruction");
214 : :
215 [ + + ]: 9 : if (ctx->active_events & EVENT_BIT(JUMP_CONDITIONAL))
216 : : /* Remember branch instruction pointer, branches to follow. */
217 : 4 : ctx->branch_pc = pc;
218 : :
219 [ - + ]: 9 : TEST_ASSERT(ctx->breakpoint_called,
220 : : "step called before or without a breakpoint at pc %u", pc);
221 : :
222 : : /* Prepare for the next step. */
223 : 9 : ctx->active_events = 0;
224 : 9 : ctx->breakpoint_called = false;
225 : : }
226 : :
227 : : /* Update total event counts. */
228 : 28 : ++ctx->event_counts[event];
229 : :
230 : 28 : return TEST_SUCCESS;
231 : : }
232 : :
233 : : /*
234 : : * This test verifies that eBPF validator generates expected numbers of events
235 : : * of each kind in the expected order (e.g. step after jump after branch).
236 : : */
237 : : static int
238 : 1 : test_events(void)
239 : : {
240 : : /*
241 : : * Program demonstrating the following properties:
242 : : * - prunnable code paths;
243 : : * - dynamically unreachable code paths;
244 : : * - conditional jumps landing on conditional jumps;
245 : : * - conditional jumps landing on unconditional jumps;
246 : : */
247 : : static const struct rte_ebpf_insn ins[] = {
248 : : /* 0: r0 = 0 */
249 : : { .code = (EBPF_ALU64 | EBPF_MOV | BPF_K), .dst_reg = EBPF_REG_0, .imm = 0 },
250 : : /* 1: if r1 > 0 goto 4 */
251 : : { .code = (BPF_JMP | BPF_JGT | BPF_K), .dst_reg = EBPF_REG_1, .imm = 0, .off = 2 },
252 : : /* 2: r1 = 0 */
253 : : { .code = (EBPF_ALU64 | EBPF_MOV | BPF_K), .dst_reg = EBPF_REG_1, .imm = 0 },
254 : : /* 3: if r0 < 1 goto 8 */
255 : : { .code = (BPF_JMP | EBPF_JLT | BPF_K), .dst_reg = EBPF_REG_0, .imm = 1, .off = 4 },
256 : : /* 4: if r1 > 0 goto 6 */
257 : : { .code = (BPF_JMP | BPF_JGT | BPF_K), .dst_reg = EBPF_REG_1, .imm = 0, .off = 1 },
258 : : /* 5: goto 9 */
259 : : { .code = (BPF_JMP | BPF_JA), .off = 3 },
260 : : /* 6: r1 = 0 */
261 : : { .code = (EBPF_ALU64 | EBPF_MOV | BPF_K), .dst_reg = EBPF_REG_1, .imm = 0 },
262 : : /* 7: if r0 > 0 goto 9 */
263 : : { .code = (BPF_JMP | BPF_JGT | BPF_K), .dst_reg = EBPF_REG_0, .imm = 0, .off = 1 },
264 : : /* 8: goto 10 */
265 : : { .code = (BPF_JMP | BPF_JA), .off = 1 },
266 : : /* 9: r0 = 1 */
267 : : { .code = (EBPF_ALU64 | EBPF_MOV | BPF_K), .dst_reg = EBPF_REG_0, .imm = 1 },
268 : : /* 10: exit */
269 : : { .code = (BPF_JMP | EBPF_EXIT) }
270 : : };
271 : :
272 : : /*
273 : : * Verified code paths (order may vary by library implementation):
274 : : * 0 -- 1 -- 4 -- 6 -- 7 -- unreachable 9
275 : : * | | `-- 8 -- 10 -- return 7 -- return 4
276 : : * | `-- unreachable 5 -- return 1
277 : : * `-- 2 -- 3 -- prune 8
278 : : * `-- unreachable 4 -- return 1 -- success 11
279 : : *
280 : : * Numbers without preceding word correspond to evaluated instructions,
281 : : * there are 9 of these in total, so 9 step events are expected.
282 : : *
283 : : * Note that 4 encountered conditional jump instructions generate:
284 : : * - 4 pairs of branch-enter/branch-return events;
285 : : * - 3 branch-unreachable events;
286 : : * - 1 branch-prune event;
287 : : * for a total count of 8 (4 instructions * 2 branches/instruction).
288 : : */
289 : : static const int expected_counts[RTE_BPF_VALIDATE_DEBUG_EVENT_END] = {
290 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_STEP] = 9,
291 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_VALIDATION_START] = 1,
292 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_VALIDATION_SUCCESS] = 1,
293 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_ENTER] = 4,
294 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_PRUNE] = 1,
295 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_RETURN] = 4,
296 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_UNREACHABLE] = 3,
297 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_JUMP_ALWAYS] = 1,
298 : : [RTE_BPF_VALIDATE_DEBUG_EVENT_JUMP_CONDITIONAL] = 4,
299 : : };
300 : :
301 : 1 : struct test_events_context ctx = {
302 : : .ins = ins,
303 : : .nb_ins = RTE_DIM(ins),
304 : : .branch_pc = NO_PROGRAM_COUNTER,
305 : : };
306 : :
307 : 1 : struct rte_bpf_validate_debug *debug = rte_bpf_validate_debug_create();
308 [ - + ]: 1 : TEST_ASSERT_NOT_NULL(debug, "validate debug create error");
309 : :
310 : : /* Set breakpoints for all instructions. */
311 [ + + ]: 12 : for (uint32_t pc = 0; pc != RTE_DIM(ins); ++pc)
312 : 11 : rte_bpf_validate_debug_break(debug, pc,
313 : 11 : &(struct rte_bpf_validate_debug_callback){
314 : : .fn = test_events_break_cb,
315 : : .ctx = &ctx,
316 : : });
317 : :
318 : : /* Set catchpoints for all events except invalid-state. */
319 : : for (enum rte_bpf_validate_debug_event event = 0;
320 [ + + ]: 12 : event != RTE_BPF_VALIDATE_DEBUG_EVENT_END; ++event)
321 [ + + ]: 11 : if (event != RTE_BPF_VALIDATE_DEBUG_EVENT_INVALID_STATE)
322 : 10 : rte_bpf_validate_debug_catch(debug, event,
323 : 10 : &(struct rte_bpf_validate_debug_callback){
324 : : .fn = test_events_catch_cb,
325 : : .ctx = &ctx,
326 : : });
327 : :
328 : 1 : struct rte_bpf *const bpf = rte_bpf_load_ex(&(struct rte_bpf_prm_ex){
329 : : .sz = sizeof(struct rte_bpf_prm_ex),
330 : : .origin = RTE_BPF_ORIGIN_RAW,
331 : : .raw.ins = ins,
332 : : .raw.nb_ins = RTE_DIM(ins),
333 : : .prog_arg[0] = { .type = RTE_BPF_ARG_RAW },
334 : : .nb_prog_arg = 1,
335 : : .debug = debug,
336 : : });
337 : :
338 : 1 : rte_bpf_destroy(bpf);
339 : 1 : rte_bpf_validate_debug_destroy(debug);
340 : :
341 [ - + ]: 1 : TEST_ASSERT_NOT_NULL(bpf, "validation failed");
342 : :
343 : : /* Verify event counts. */
344 : : for (enum rte_bpf_validate_debug_event event = 0;
345 [ + + ]: 12 : event != RTE_BPF_VALIDATE_DEBUG_EVENT_END; ++event)
346 [ - + ]: 11 : TEST_ASSERT_EQUAL(
347 : : ctx.event_counts[event], expected_counts[event],
348 : : "expected %d %s events, but got %d",
349 : : expected_counts[event], event_names[event],
350 : : ctx.event_counts[event]);
351 : :
352 : : /* Additionally double-check final stack state. */
353 [ - + ]: 1 : TEST_ASSERT_EQUAL(ctx.branch_pc_stack_length, 0,
354 : : "unbalanced branch enters and returns");
355 : :
356 : : return TEST_SUCCESS;
357 : : }
358 : :
359 : 305 : REGISTER_FAST_TEST(bpf_validate_events_autotest, NOHUGE_OK, ASAN_OK, test_events);
360 : :
361 : : /*
362 : : * Tests of BPF validation.
363 : : */
364 : :
365 : : /* Sizes of text buffers used for formatting various debug outputs. */
366 : : #define VALUE_FORMAT_BUFFER_SIZE 32
367 : : #define INTERVAL_FORMAT_BUFFER_SIZE 64
368 : : #define REGISTER_FORMAT_BUFFER_SIZE 256
369 : : #define DISASSEMBLY_FORMAT_BUFFER_SIZE 64
370 : :
371 : : #define COMPARISON_INDEX_IMMEDIATE RTE_BIT32(0)
372 : : #define COMPARISON_INDEX_GREATER RTE_BIT32(1)
373 : : #define COMPARISON_INDEX_INCLUSIVE RTE_BIT32(2)
374 : : #define COMPARISON_INDEX_SIGNED RTE_BIT32(3)
375 : :
376 : : /* List comparison opcodes to make their index bits match constants above. */
377 : : static const uint8_t comparisons_opcode[] = {
378 : : (BPF_JMP | EBPF_JLT | BPF_X),
379 : : (BPF_JMP | EBPF_JLT | BPF_K),
380 : : (BPF_JMP | BPF_JGT | BPF_X),
381 : : (BPF_JMP | BPF_JGT | BPF_K),
382 : : (BPF_JMP | EBPF_JLE | BPF_X),
383 : : (BPF_JMP | EBPF_JLE | BPF_K),
384 : : (BPF_JMP | BPF_JGE | BPF_X),
385 : : (BPF_JMP | BPF_JGE | BPF_K),
386 : : (BPF_JMP | EBPF_JSLT | BPF_X),
387 : : (BPF_JMP | EBPF_JSLT | BPF_K),
388 : : (BPF_JMP | EBPF_JSGT | BPF_X),
389 : : (BPF_JMP | EBPF_JSGT | BPF_K),
390 : : (BPF_JMP | EBPF_JSLE | BPF_X),
391 : : (BPF_JMP | EBPF_JSLE | BPF_K),
392 : : (BPF_JMP | EBPF_JSGE | BPF_X),
393 : : (BPF_JMP | EBPF_JSGE | BPF_K),
394 : : };
395 : :
396 : : /* Interval bounded by two signed values, inclusive; min <= max. */
397 : : struct signed_interval {
398 : : int64_t min;
399 : : int64_t max;
400 : : };
401 : :
402 : : /* Interval bounded by two unsigned values, inclusive; min <= max. */
403 : : struct unsigned_interval {
404 : : uint64_t min;
405 : : uint64_t max;
406 : : };
407 : :
408 : : /*
409 : : * Expected interval of register values.
410 : : *
411 : : * If `is_defined` is not set, domain is considered to be unused in verification
412 : : * parameters (instruction is not accessing corresponding register).
413 : : * It's not the same as `unknown` domain which describes register that is being
414 : : * used but can hold any value.
415 : : *
416 : : * Flag `is_pointer` tells if the interval is relative to some memory area base.
417 : : */
418 : : struct domain {
419 : : bool is_defined;
420 : : bool is_pointer;
421 : : struct signed_interval s;
422 : : struct unsigned_interval u;
423 : : };
424 : :
425 : : /* Expected validation state at certain point. */
426 : : struct state {
427 : : /* Specifies that the branch is dynamically unreachable. */
428 : : bool is_unreachable;
429 : : struct domain dst;
430 : : struct domain src;
431 : : };
432 : :
433 : : /* Instruction verification parameters. */
434 : : struct verify_instruction_param {
435 : : struct rte_ebpf_insn tested_instruction;
436 : : size_t area_size;
437 : : /* States just before the tested instruction, just after, or if jumped. */
438 : : struct state pre;
439 : : struct state post;
440 : : struct state jump;
441 : : };
442 : :
443 : : /* Point (pre/post/jump) specific verification context. */
444 : : struct point_context {
445 : : uint32_t program_counter;
446 : : uint32_t hit_count;
447 : : char formatted_dst[REGISTER_FORMAT_BUFFER_SIZE];
448 : : char formatted_src[REGISTER_FORMAT_BUFFER_SIZE];
449 : : };
450 : :
451 : : /* Verification context. */
452 : : struct verify_instruction_context {
453 : : struct verify_instruction_param prm;
454 : : /* Allocation of registers in the generated program. */
455 : : uint8_t base_reg;
456 : : uint8_t dst_reg;
457 : : uint8_t src_reg;
458 : : uint8_t tmp_reg;
459 : : /* Number of times invalid state callback was called. */
460 : : uint32_t invalid_state_count;
461 : : /* Contexts just before the tested instruction, just after, or if jumped. */
462 : : struct point_context pre;
463 : : struct point_context post;
464 : : struct point_context jump;
465 : : };
466 : :
467 : : /* Domain with both signed and unsigned interval having maximum size. */
468 : : static const struct domain unknown = {
469 : : .is_defined = true,
470 : : .s = { .min = INT64_MIN, .max = INT64_MAX },
471 : : .u = { .min = 0, .max = UINT64_MAX },
472 : : };
473 : :
474 : : /* Unreachable state. */
475 : : static const struct state unreachable = {
476 : : .is_unreachable = true,
477 : : };
478 : :
479 : :
480 : : /* BUILDING DOMAINS */
481 : :
482 : : /* Create domain from singleton interval. */
483 : : static struct domain
484 : : make_singleton_domain(uint64_t value)
485 : : {
486 : 60 : return (struct domain){
487 : : .is_defined = true,
488 : : .s = { .min = value, .max = value },
489 : : .u = { .min = value, .max = value },
490 : : };
491 : : }
492 : :
493 : : /* Create domain from signed interval. */
494 : : static struct domain
495 : 60 : make_signed_domain(int64_t min, int64_t max)
496 : : {
497 [ - + ]: 60 : RTE_VERIFY(min <= max);
498 : 107 : return (struct domain){
499 : : .is_defined = true,
500 : : .s = { .min = min, .max = max },
501 : 60 : .u = (min ^ max) >= 0 ?
502 [ + + ]: 60 : (struct unsigned_interval){ .min = min, .max = max } :
503 : : unknown.u,
504 : : };
505 : : }
506 : :
507 : : /* Create domain from unsigned interval. */
508 : : static struct domain
509 : 39 : make_unsigned_domain(uint64_t min, uint64_t max)
510 : : {
511 [ - + ]: 39 : RTE_VERIFY(min <= max);
512 : 62 : return (struct domain){
513 : : .is_defined = true,
514 : 39 : .s = (int64_t)(min ^ max) >= 0 ?
515 [ + + ]: 39 : (struct signed_interval){ .min = min, .max = max } :
516 : : unknown.s,
517 : : .u = { .min = min, .max = max },
518 : : };
519 : : }
520 : :
521 : : /* Create domain from signed interval. */
522 : : static struct domain
523 : : make_pointer_domain(int64_t min, int64_t max)
524 : : {
525 : 9 : struct domain result = make_signed_domain(min, max);
526 : : result.is_pointer = true;
527 : 4 : return result;
528 : : }
529 : :
530 : : /* Return true if domain is a scalar or pointer singleton. */
531 : : static bool
532 : : domain_is_singleton(const struct domain *domain)
533 : : {
534 : 1586 : return domain->s.min == domain->s.max &&
535 [ + + + - : 629 : (uint64_t)domain->s.max == domain->u.min &&
+ - + - +
- + - +
- ]
536 [ - + - + : 389 : domain->u.min == domain->u.max;
- + - + ]
537 : : }
538 : :
539 : : /* Print error message into buffer if rc signifies error or overflow. */
540 : : static void
541 : 1839 : handle_format_errors(char *buffer, size_t bufsz, int rc)
542 : : {
543 [ - + ]: 1839 : if (rc < 0)
544 : 0 : snprintf(buffer, bufsz, "FORMAT ERROR %d!", -rc);
545 [ - + ]: 1839 : else if ((unsigned int)rc >= bufsz)
546 : : snprintf(buffer, bufsz, "FORMAT OVERFLOW!");
547 : 1839 : }
548 : :
549 : : /* Format register information into provided buffer and return the buffer. */
550 : : static const char *
551 : : format_value(char *buffer, size_t bufsz, char format, uint64_t value)
552 : : {
553 : 0 : handle_format_errors(buffer, bufsz,
554 : : rte_bpf_validate_debug_format_value(buffer, bufsz, format, value));
555 : : return buffer;
556 : : }
557 : :
558 : : /* Format register information into provided buffer and return the buffer. */
559 : : static const char *
560 : : format_interval(char *buffer, size_t bufsz, char format, uint64_t min, uint64_t max)
561 : : {
562 : 0 : handle_format_errors(buffer, bufsz,
563 : : rte_bpf_validate_debug_format_interval(buffer, bufsz, format, min, max));
564 : : return buffer;
565 : : }
566 : :
567 : : /* Format domain information into provided buffer and return the buffer. */
568 : : static const char *
569 : 0 : format_domain(char *buffer, size_t bufsz, const struct domain *domain)
570 : : {
571 : : char signed_buffer[INTERVAL_FORMAT_BUFFER_SIZE];
572 : : char unsigned_buffer[INTERVAL_FORMAT_BUFFER_SIZE];
573 : :
574 : 0 : const int rc = !domain->is_defined ?
575 [ # # ]: 0 : snprintf(buffer, bufsz, "UNDEFINED") :
576 : 0 : snprintf(buffer, bufsz, "%s %s INTERSECT %s",
577 [ # # ]: 0 : domain->is_pointer ? "pointer" : "scalar",
578 : : format_interval(signed_buffer, sizeof(signed_buffer), 'd',
579 : 0 : domain->s.min, domain->s.max),
580 : : format_interval(unsigned_buffer, sizeof(unsigned_buffer), 'x',
581 : 0 : domain->u.min, domain->u.max));
582 : :
583 [ # # ]: 0 : handle_format_errors(buffer, bufsz, rc < 0 ? -errno : rc);
584 : :
585 : 0 : return buffer;
586 : : }
587 : :
588 : : /* Format register information into provided buffer and return the buffer. */
589 : : static const char *
590 : 1839 : format_register(struct rte_bpf_validate_debug *debug, char *buffer, size_t bufsz, uint8_t reg)
591 : : {
592 : 1839 : handle_format_errors(buffer, bufsz,
593 : : rte_bpf_validate_debug_format_register_info(debug, buffer, bufsz, reg));
594 : 1839 : return buffer;
595 : : }
596 : :
597 : :
598 : : /* CHECKING REGISTER ACTUAL DOMAINS */
599 : :
600 : : /* Return true the specified conditional jump _may_ occur at current state. */
601 : : static bool
602 : 14656 : may_jump(const struct rte_bpf_validate_debug *debug,
603 : : const struct rte_ebpf_insn *jump, uint64_t imm64)
604 : : {
605 : 14656 : const int result = rte_bpf_validate_debug_may_jump(debug, jump, imm64);
606 [ - + ]: 14656 : RTE_VERIFY(result >= 0);
607 : 14656 : return (result & RTE_BPF_VALIDATE_DEBUG_MAY_BE_TRUE) != 0;
608 : : }
609 : :
610 : : /* Check interval of the register interpreted as signed scalar. */
611 : : static int
612 : 1826 : check_signed_interval(struct rte_bpf_validate_debug *debug,
613 : : uint8_t reg, struct signed_interval interval)
614 : : {
615 : : char buffer[VALUE_FORMAT_BUFFER_SIZE];
616 : :
617 [ - + ]: 1826 : TEST_ASSERT_EQUAL(may_jump(debug,
618 : : &(struct rte_ebpf_insn){
619 : : .code = (BPF_JMP | EBPF_JSLT | BPF_K),
620 : : .dst_reg = reg,
621 : : }, interval.min),
622 : : false,
623 : : "r%hhu s< %s is impossible", reg,
624 : : format_value(buffer, sizeof(buffer), 'd', interval.min));
625 : :
626 [ - + ]: 1826 : TEST_ASSERT_EQUAL(may_jump(debug,
627 : : &(struct rte_ebpf_insn){
628 : : .code = (BPF_JMP | BPF_JEQ | BPF_K),
629 : : .dst_reg = reg,
630 : : }, interval.min),
631 : : true,
632 : : "r%hhu == %s is possible", reg,
633 : : format_value(buffer, sizeof(buffer), 'd', interval.min));
634 : :
635 [ - + ]: 1826 : TEST_ASSERT_EQUAL(may_jump(debug,
636 : : &(struct rte_ebpf_insn){
637 : : .code = (BPF_JMP | BPF_JEQ | BPF_K),
638 : : .dst_reg = reg,
639 : : }, interval.max),
640 : : true,
641 : : "r%hhu == %s is possible", reg,
642 : : format_value(buffer, sizeof(buffer), 'd', interval.max));
643 : :
644 [ - + ]: 1826 : TEST_ASSERT_EQUAL(may_jump(debug,
645 : : &(struct rte_ebpf_insn){
646 : : .code = (BPF_JMP | EBPF_JSGT | BPF_K),
647 : : .dst_reg = reg,
648 : : }, interval.max),
649 : : false,
650 : : "r%hhu s> %s is impossible", reg,
651 : : format_value(buffer, sizeof(buffer), 'd', interval.max));
652 : :
653 : 1826 : return TEST_SUCCESS;
654 : : }
655 : :
656 : : /* Check interval of the register interpreted as unsigned scalar. */
657 : : static int
658 : 1826 : check_unsigned_interval(struct rte_bpf_validate_debug *debug,
659 : : uint8_t reg, struct unsigned_interval interval)
660 : : {
661 : : char buffer[VALUE_FORMAT_BUFFER_SIZE];
662 : :
663 [ - + ]: 1826 : TEST_ASSERT_EQUAL(may_jump(debug,
664 : : &(struct rte_ebpf_insn){
665 : : .code = (BPF_JMP | EBPF_JLT | BPF_K),
666 : : .dst_reg = reg,
667 : : }, interval.min),
668 : : false,
669 : : "r%hhu u< %s is impossible", reg,
670 : : format_value(buffer, sizeof(buffer), 'x', interval.min));
671 : :
672 [ - + ]: 1826 : TEST_ASSERT_EQUAL(may_jump(debug,
673 : : &(struct rte_ebpf_insn){
674 : : .code = (BPF_JMP | BPF_JEQ | BPF_K),
675 : : .dst_reg = reg,
676 : : }, interval.min),
677 : : true,
678 : : "r%hhu == %s is possible", reg,
679 : : format_value(buffer, sizeof(buffer), 'x', interval.min));
680 : :
681 [ - + ]: 1826 : TEST_ASSERT_EQUAL(may_jump(debug,
682 : : &(struct rte_ebpf_insn){
683 : : .code = (BPF_JMP | BPF_JEQ | BPF_K),
684 : : .dst_reg = reg,
685 : : }, interval.max),
686 : : true,
687 : : "r%hhu == %s is possible", reg,
688 : : format_value(buffer, sizeof(buffer), 'x', interval.max));
689 : :
690 [ - + ]: 1826 : TEST_ASSERT_EQUAL(may_jump(debug,
691 : : &(struct rte_ebpf_insn){
692 : : .code = (BPF_JMP | BPF_JGT | BPF_K),
693 : : .dst_reg = reg,
694 : : }, interval.max),
695 : : false,
696 : : "r%hhu u> %s is impossible", reg,
697 : : format_value(buffer, sizeof(buffer), 'x', interval.max));
698 : :
699 : 1826 : return TEST_SUCCESS;
700 : : }
701 : :
702 : : /* Check interval of the register relative to the base register. */
703 : : static int
704 : 12 : check_relative_interval(struct rte_bpf_validate_debug *debug,
705 : : uint8_t reg, struct signed_interval interval, uint8_t base_reg)
706 : : {
707 : : char buffer[VALUE_FORMAT_BUFFER_SIZE];
708 : :
709 [ - + ]: 12 : TEST_ASSERT_EQUAL(may_jump(debug,
710 : : &(struct rte_ebpf_insn){
711 : : .code = (BPF_JMP | EBPF_JLT | BPF_X),
712 : : .dst_reg = reg,
713 : : .src_reg = base_reg,
714 : : }, interval.min),
715 : : false,
716 : : "r%hhu u< r%hhu + %s is impossible", reg, base_reg,
717 : : format_value(buffer, sizeof(buffer), 'd', interval.min));
718 : :
719 [ - + ]: 12 : TEST_ASSERT_EQUAL(may_jump(debug,
720 : : &(struct rte_ebpf_insn){
721 : : .code = (BPF_JMP | BPF_JEQ | BPF_X),
722 : : .dst_reg = reg,
723 : : .src_reg = base_reg,
724 : : }, interval.min),
725 : : true,
726 : : "r%hhu == r%hhu + %s is possible", reg, base_reg,
727 : : format_value(buffer, sizeof(buffer), 'd', interval.min));
728 : :
729 [ - + ]: 12 : TEST_ASSERT_EQUAL(may_jump(debug,
730 : : &(struct rte_ebpf_insn){
731 : : .code = (BPF_JMP | BPF_JEQ | BPF_X),
732 : : .dst_reg = reg,
733 : : .src_reg = base_reg,
734 : : }, interval.max),
735 : : true,
736 : : "r%hhu == r%hhu + %s is possible", reg, base_reg,
737 : : format_value(buffer, sizeof(buffer), 'd', interval.max));
738 : :
739 [ - + ]: 12 : TEST_ASSERT_EQUAL(may_jump(debug,
740 : : &(struct rte_ebpf_insn){
741 : : .code = (BPF_JMP | BPF_JGT | BPF_X),
742 : : .dst_reg = reg,
743 : : .src_reg = base_reg,
744 : : }, interval.max),
745 : : false,
746 : : "r%hhu u> r%hhu + %s is impossible", reg, base_reg,
747 : : format_value(buffer, sizeof(buffer), 'd', interval.max));
748 : :
749 : 12 : return TEST_SUCCESS;
750 : : }
751 : :
752 : : /*
753 : : * Check access of the register interpreted as pointer.
754 : : *
755 : : * Unlike other similar functions, min > max is not a problem here,
756 : : * so either signed or unsigned pair can be passed without any issues.
757 : : *
758 : : * This is the reason we are not using signed_interval or unsigned_interval here
759 : : * to avoid confusion.
760 : : */
761 : : static int
762 : 24 : check_pointer_access(struct rte_bpf_validate_debug *debug, uint8_t reg,
763 : : uint64_t min, uint64_t max, size_t area_size)
764 : : {
765 : : char buffer[VALUE_FORMAT_BUFFER_SIZE];
766 : :
767 : : /* Start and end of the valid offsets window (unless empty). */
768 : 24 : const uint64_t window_begin = -min;
769 : 24 : const uint64_t window_end = area_size - max;
770 : :
771 : : /* Only have accessible bytes if the interval is smaller than the area. */
772 : 24 : const uint64_t interval_size = max - min;
773 : : const bool window_empty = (interval_size >= area_size);
774 : :
775 [ - + ]: 24 : TEST_ASSERT_EQUAL(rte_bpf_validate_debug_can_access(debug,
776 : : &(struct rte_ebpf_insn){
777 : : .code = (BPF_LDX | BPF_B | BPF_MEM),
778 : : .src_reg = reg
779 : : }, window_begin - 1),
780 : : false,
781 : : "r%hhu + %s (before window begin) dereference is invalid", reg,
782 : : format_value(buffer, sizeof(buffer), 'd', window_begin - 1));
783 : :
784 [ - + - - ]: 24 : TEST_ASSERT_EQUAL(rte_bpf_validate_debug_can_access(debug,
785 : : &(struct rte_ebpf_insn){
786 : : .code = (BPF_LDX | BPF_B | BPF_MEM),
787 : : .src_reg = reg
788 : : }, window_begin),
789 : : !window_empty,
790 : : "r%hhu + %s (after window begin) dereference is %s", reg,
791 : : format_value(buffer, sizeof(buffer), 'd', window_begin),
792 : : window_empty ? "invalid for empty window" : "valid");
793 : :
794 [ - + - - ]: 24 : TEST_ASSERT_EQUAL(rte_bpf_validate_debug_can_access(debug,
795 : : &(struct rte_ebpf_insn){
796 : : .code = (BPF_LDX | BPF_B | BPF_MEM),
797 : : .src_reg = reg
798 : : }, window_end - 1),
799 : : !window_empty,
800 : : "r%hhu + %s (before window end) dereference is %s", reg,
801 : : format_value(buffer, sizeof(buffer), 'd', window_end - 1),
802 : : window_empty ? "invalid for empty window" : "valid");
803 : :
804 [ - + ]: 24 : TEST_ASSERT_EQUAL(rte_bpf_validate_debug_can_access(debug,
805 : : &(struct rte_ebpf_insn){
806 : : .code = (BPF_LDX | BPF_B | BPF_MEM),
807 : : .src_reg = reg
808 : : }, window_end),
809 : : false,
810 : : "r%hhu + %s (after window end) dereference is invalid", reg,
811 : : format_value(buffer, sizeof(buffer), 'd', window_end));
812 : :
813 : 24 : return TEST_SUCCESS;
814 : : }
815 : :
816 : : /* Check domain of the register interpreted as absolute value. */
817 : : static int
818 : 1826 : check_scalar_domain(struct rte_bpf_validate_debug *debug, uint8_t reg,
819 : : const struct domain *domain)
820 : : {
821 [ - + ]: 1826 : TEST_ASSERT_SUCCESS(
822 : : check_signed_interval(debug, reg, domain->s),
823 : : "absolute signed interval check");
824 : :
825 [ - + ]: 1826 : TEST_ASSERT_SUCCESS(
826 : : check_unsigned_interval(debug, reg, domain->u),
827 : : "absolute unsigned interval check");
828 : :
829 : : return TEST_SUCCESS;
830 : : }
831 : :
832 : : /* Check domain of the register interpreted as relative pointer. */
833 : : static int
834 : 12 : check_pointer_domain(struct rte_bpf_validate_debug *debug, uint8_t reg,
835 : : const struct domain *domain, uint8_t base_reg, size_t area_size)
836 : : {
837 [ - + ]: 12 : TEST_ASSERT_SUCCESS(
838 : : check_relative_interval(debug, reg, domain->s, base_reg),
839 : : "relative interval check");
840 : :
841 [ - + ]: 12 : TEST_ASSERT_SUCCESS(
842 : : check_pointer_access(debug, reg, domain->s.min, domain->s.max,
843 : : area_size),
844 : : "pointer signed access check");
845 : :
846 [ - + ]: 12 : TEST_ASSERT_SUCCESS(
847 : : check_pointer_access(debug, reg, domain->u.min, domain->u.max,
848 : : area_size),
849 : : "pointer unsigned access check");
850 : :
851 : : return TEST_SUCCESS;
852 : : }
853 : :
854 : : /* Check domain of the register and format the values in case of an error. */
855 : : static int
856 : 1838 : check_domain(struct rte_bpf_validate_debug *debug, uint8_t reg,
857 : : const struct domain *domain, uint8_t base_reg, size_t area_size)
858 : : {
859 : : char buffer[REGISTER_FORMAT_BUFFER_SIZE];
860 : :
861 : 1838 : const int rc = domain->is_pointer ?
862 [ + + ]: 1838 : check_pointer_domain(debug, reg, domain, base_reg, area_size) :
863 : 1826 : check_scalar_domain(debug, reg, domain);
864 : :
865 [ - + ]: 1838 : if (rc != TEST_SUCCESS) {
866 : 0 : TEST_LOG_LINE(WARNING, "\tExpected: r%hhu = %s", reg,
867 : : format_domain(buffer, sizeof(buffer), domain));
868 : :
869 : 0 : TEST_LOG_LINE(WARNING, "\tFound: r%hhu = %s", reg,
870 : : format_register(debug, buffer, sizeof(buffer), reg));
871 : : }
872 : :
873 : 1838 : return rc;
874 : : }
875 : :
876 : :
877 : : /* GENERATING TEST PROGRAM */
878 : :
879 : : static bool
880 : : fits_in_imm32(int64_t value)
881 : : {
882 : 2415 : return value >= INT32_MIN && value <= INT32_MAX;
883 : : }
884 : :
885 : : /* Load constant into the register. */
886 : : static void
887 : 262 : load_constant(struct rte_ebpf_insn **ins, uint8_t reg, int64_t value)
888 : : {
889 [ + + ]: 262 : if (fits_in_imm32(value)) {
890 : 1302 : *(*ins)++ = (struct rte_ebpf_insn){
891 : : .code = (EBPF_ALU64 | EBPF_MOV | BPF_K),
892 : : .dst_reg = reg,
893 : 144 : .imm = (int32_t)value,
894 : : };
895 : : } else {
896 : : /* Load imm64 into tmp_reg using wide load, lower bits first... */
897 : 118 : *(*ins)++ = (struct rte_ebpf_insn){
898 : : .code = (BPF_LD | BPF_IMM | EBPF_DW),
899 : : .dst_reg = reg,
900 : 118 : .imm = (uint32_t)value,
901 : : };
902 : : /* ... then higher bits. */
903 : 118 : *(*ins)++ = (struct rte_ebpf_insn){
904 : 118 : .imm = (uint32_t)(value >> 32),
905 : : };
906 : : }
907 : 262 : }
908 : :
909 : : /*
910 : : * Compare specified register to value and jump.
911 : : *
912 : : * Jump offset is not filled and should be patched in by the caller.
913 : : */
914 : : static void
915 : 2135 : compare_and_jump(struct rte_ebpf_insn **ins, uint8_t op, uint8_t reg,
916 : : int64_t value, uint8_t tmp_reg)
917 : : {
918 [ + + ]: 2135 : if (fits_in_imm32(value)) {
919 : : /* Jump on specified condition between reg and immediate. */
920 : 2070 : *(*ins)++ = (struct rte_ebpf_insn){
921 : : .code = (BPF_JMP | op | BPF_K),
922 : : .dst_reg = reg,
923 : 2070 : .imm = (int32_t)value,
924 : : };
925 : : } else {
926 : : /* Load value into tmp_reg. */
927 : 65 : load_constant(ins, tmp_reg, value);
928 : :
929 : : /* Jump on specified condition between reg and tmp_reg. */
930 : 65 : *(*ins)++ = (struct rte_ebpf_insn){
931 : : .code = (BPF_JMP | op | BPF_X),
932 : : .dst_reg = reg,
933 : : .src_reg = tmp_reg,
934 : : };
935 : : }
936 : 2135 : }
937 : :
938 : : /*
939 : : * Prepare register to be in the specified scalar domain.
940 : : *
941 : : * Unless singleton, load unknown value into it and clamp it with conditional jumps.
942 : : * (Jump offsets are not filled and should be patched in by the caller.)
943 : : */
944 : : static void
945 [ + + ]: 765 : prepare_scalar_domain(struct rte_ebpf_insn **ins, uint8_t reg,
946 : : const struct domain *domain, uint8_t base_reg, int *service_cell_count,
947 : : uint8_t tmp_reg)
948 : : {
949 [ + + ]: 765 : if (domain_is_singleton(domain)) {
950 : : /* Don't need any uncertainty for a singleton. */
951 : 197 : load_constant(ins, reg, domain->s.min);
952 : : return;
953 : : }
954 : :
955 : : /* Load value from memory area into the register. */
956 : 568 : *(*ins)++ = (struct rte_ebpf_insn){
957 : : .code = (BPF_LDX | EBPF_DW | BPF_MEM),
958 : : .dst_reg = reg,
959 : : .src_reg = base_reg,
960 : 568 : .off = sizeof(uint64_t) * (*service_cell_count)++,
961 : : };
962 : :
963 : : /*
964 : : * Use both signed and unsigned conditions, even if redundant.
965 : : * It makes it more robust if conditional jump verification itself
966 : : * contains bugs like not updating the other type of interval.
967 : : * Jump instructions themselves can be tested separately to catch
968 : : * these bugs, this preparation phase is not a test for them.
969 : : */
970 [ + + ]: 568 : if (domain->u.min > unknown.u.min)
971 : 522 : compare_and_jump(ins, EBPF_JLT, reg, domain->u.min, tmp_reg);
972 [ + + ]: 568 : if (domain->u.max < unknown.u.max)
973 : 551 : compare_and_jump(ins, BPF_JGT, reg, domain->u.max, tmp_reg);
974 [ + + ]: 568 : if (domain->s.min > unknown.s.min)
975 : 525 : compare_and_jump(ins, EBPF_JSLT, reg, domain->s.min, tmp_reg);
976 [ + + ]: 568 : if (domain->s.max < unknown.s.max)
977 : 537 : compare_and_jump(ins, EBPF_JSGT, reg, domain->s.max, tmp_reg);
978 : : }
979 : :
980 : : /*
981 : : * Prepare register to be in the specified scalar or pointer domain, if any.
982 : : *
983 : : * If `domain` is NULL, do nothing. Otherwise prepare scalar domain,
984 : : * and then add base register to it to convert it to a pointer, if needed.
985 : : */
986 : : static void
987 : 765 : prepare_domain(struct rte_ebpf_insn **ins, uint8_t reg,
988 : : const struct domain *domain, uint8_t base_reg, int *service_cell_count,
989 : : uint8_t tmp_reg)
990 : : {
991 : 765 : prepare_scalar_domain(ins, reg, domain, base_reg, service_cell_count, tmp_reg);
992 : :
993 [ + + ]: 765 : if (domain->is_pointer)
994 : : /* Add base_reg to convert resulting scalar into a pointer. */
995 : 6 : *(*ins)++ = (struct rte_ebpf_insn){
996 : : .code = (EBPF_ALU64 | BPF_ADD | BPF_X),
997 : : .dst_reg = reg,
998 : : .src_reg = base_reg,
999 : : };
1000 : 765 : }
1001 : :
1002 : : static void
1003 : 457 : fill_verify_instruction_defaults(struct verify_instruction_param *prm)
1004 : : {
1005 : :
1006 [ + + ]: 457 : if (BPF_CLASS(prm->tested_instruction.code) != BPF_JMP)
1007 : 93 : prm->jump.is_unreachable = true;
1008 : :
1009 [ - + ]: 457 : RTE_VERIFY(!prm->pre.is_unreachable);
1010 [ + + ]: 457 : if (prm->post.is_unreachable) {
1011 [ - + ]: 90 : RTE_VERIFY(!prm->post.dst.is_defined);
1012 [ - + ]: 90 : RTE_VERIFY(!prm->post.src.is_defined);
1013 : : } else {
1014 [ + + ]: 367 : if (!prm->post.dst.is_defined)
1015 : 20 : prm->post.dst = prm->pre.dst;
1016 [ + + ]: 367 : if (!prm->post.src.is_defined)
1017 : 136 : prm->post.src = prm->pre.src;
1018 : : }
1019 : :
1020 [ + + ]: 457 : if (prm->jump.is_unreachable) {
1021 [ - + ]: 187 : RTE_VERIFY(!prm->jump.dst.is_defined);
1022 [ - + ]: 187 : RTE_VERIFY(!prm->jump.src.is_defined);
1023 : : } else {
1024 [ + + ]: 270 : if (!prm->jump.dst.is_defined)
1025 : 16 : prm->jump.dst = prm->pre.dst;
1026 [ + + ]: 270 : if (!prm->jump.src.is_defined)
1027 : 39 : prm->jump.src = prm->pre.src;
1028 : : }
1029 : 457 : }
1030 : :
1031 : : /* Generate program for the tested instruction and domains from the context.
1032 : : *
1033 : : * Return number of instructions.
1034 : : *
1035 : : * Destination and source registers in tested_instruction should not be specified,
1036 : : * they are filled in by the function as long as domains for them are specified.
1037 : : * Jump offset should not be specified, it is filled in by the function.
1038 : : *
1039 : : * If `pre.dst` or `pre.src` domain is not defined, corresponding register
1040 : : * is not prepared.
1041 : : *
1042 : : * For non-jump instructions `jump.is_unreachable` is always set automatically.
1043 : : *
1044 : : * If any of the post or jump domains are not defined, they are copied from src
1045 : : * unless corresponding branch is unreachable.
1046 : : *
1047 : : * Memory area size is automatically expanded to have enough space for loading
1048 : : * unknown dst and src register values, thus testing sizes less than 16 bytes is
1049 : : * not guaranteed.
1050 : : *
1051 : : * Limitations:
1052 : : * - Support for jump instructions is incomplete (e.g. exit, ja).
1053 : : * - Wide instructions are not supported yet.
1054 : : */
1055 : : static uint32_t
1056 : 417 : generate_program(struct verify_instruction_context *ctx, struct rte_ebpf_insn *ins)
1057 : : {
1058 : 417 : struct rte_ebpf_insn *const ins_buf = ins;
1059 : : /* Number of double words used for service purposes. */
1060 : 417 : int service_cell_count = 0;
1061 : :
1062 : : /* Make sure we actually support provided instruction. */
1063 [ - + ]: 417 : switch (BPF_CLASS(ctx->prm.tested_instruction.code)) {
1064 : 0 : case BPF_LD:
1065 : : /* Wide instructions are not supported yet. */
1066 [ # # ]: 0 : RTE_VERIFY(!rte_bpf_insn_is_wide(&ctx->prm.tested_instruction));
1067 : : break;
1068 : : }
1069 : :
1070 : 417 : fill_verify_instruction_defaults(&ctx->prm);
1071 : :
1072 : : /* Allocate registers, base_reg is received as program argument. */
1073 : 417 : ctx->base_reg = EBPF_REG_1;
1074 [ + + - + ]: 417 : ctx->dst_reg = (ctx->prm.pre.dst.is_defined || ctx->prm.post.dst.is_defined ||
1075 [ # # ]: 0 : ctx->prm.jump.dst.is_defined) ? EBPF_REG_2 : NO_REGISTER;
1076 [ + + + - ]: 417 : ctx->src_reg = (ctx->prm.pre.src.is_defined || ctx->prm.post.src.is_defined ||
1077 [ + - ]: 68 : ctx->prm.jump.src.is_defined) ? EBPF_REG_3 : NO_REGISTER;
1078 : 417 : ctx->tmp_reg = EBPF_REG_4;
1079 : :
1080 : : /* Clear r0 to make it eligible as a return value. */
1081 : : load_constant(&ins, EBPF_REG_0, 0);
1082 : :
1083 : : /* Fill dst register in the instruction if defined anywhere, prepare if needed. */
1084 [ + - ]: 417 : if (ctx->dst_reg != NO_REGISTER) {
1085 [ - + ]: 417 : RTE_VERIFY(ctx->prm.tested_instruction.dst_reg == 0);
1086 : 417 : ctx->prm.tested_instruction.dst_reg = ctx->dst_reg;
1087 : :
1088 [ + + ]: 417 : if (ctx->prm.pre.dst.is_defined)
1089 : 416 : prepare_domain(&ins, ctx->dst_reg, &ctx->prm.pre.dst,
1090 : 416 : ctx->base_reg, &service_cell_count, ctx->tmp_reg);
1091 : : else
1092 : 1 : TEST_LOG_LINE(DEBUG, "Not preparing undefined r%hhu", ctx->dst_reg);
1093 : : }
1094 : :
1095 : : /* Fill src register in the instruction if defined anywhere, prepare if needed. */
1096 [ + + ]: 417 : if (ctx->src_reg != NO_REGISTER) {
1097 [ - + ]: 349 : RTE_VERIFY(ctx->prm.tested_instruction.src_reg == 0);
1098 : 349 : ctx->prm.tested_instruction.src_reg = ctx->src_reg;
1099 : :
1100 [ + - ]: 349 : if (ctx->prm.pre.src.is_defined)
1101 : 349 : prepare_domain(&ins, ctx->src_reg, &ctx->prm.pre.src,
1102 : 349 : ctx->base_reg, &service_cell_count, ctx->tmp_reg);
1103 : : else
1104 : 0 : TEST_LOG_LINE(DEBUG, "Not preparing undefined r%hhu", ctx->src_reg);
1105 : : }
1106 : :
1107 : : /* Automatically increase area size if needed. */
1108 : 417 : ctx->prm.area_size = RTE_MAX(ctx->prm.area_size, service_cell_count * sizeof(uint64_t));
1109 : :
1110 : : /* Issue tested instruction. */
1111 : 417 : ctx->pre.program_counter = ins - ins_buf;
1112 : 417 : *ins++ = ctx->prm.tested_instruction;
1113 : :
1114 : : /* Issue post instruction (for setting post breakpoint). */
1115 : 417 : ctx->post.program_counter = ins - ins_buf;
1116 : : load_constant(&ins, EBPF_REG_0, 1);
1117 : :
1118 : : /* Issue jump branch for the jump instruction, even if dynamically unreachable. */
1119 [ + + ]: 417 : if (BPF_CLASS(ctx->prm.tested_instruction.code) != BPF_JMP)
1120 : 93 : ctx->jump.program_counter = NO_PROGRAM_COUNTER;
1121 : : else {
1122 : : /* Finish previous branch by issuing exit. */
1123 : 324 : *ins++ = (struct rte_ebpf_insn){ .code = (BPF_JMP | EBPF_EXIT) };
1124 : :
1125 : : /* Issue jump target instruction (for setting jump breakpoint). */
1126 : 324 : ctx->jump.program_counter = ins - ins_buf;
1127 : : load_constant(&ins, EBPF_REG_0, 2);
1128 : :
1129 : : /* Patch jump in tested jump instruction. */
1130 [ - + ]: 324 : RTE_VERIFY(ins_buf[ctx->pre.program_counter].off == 0);
1131 : 324 : ins_buf[ctx->pre.program_counter].off =
1132 : 324 : ctx->jump.program_counter - ctx->post.program_counter;
1133 : : }
1134 : :
1135 : : /* Issue exit instruction. */
1136 : 417 : const uint32_t exit_pc = ins - ins_buf;
1137 : 417 : *ins++ = (struct rte_ebpf_insn){ .code = (BPF_JMP | EBPF_EXIT) };
1138 : :
1139 : : /* Patch all jumps to point to exit. */
1140 [ + + ]: 3923 : for (uint32_t pc = 0; pc != ctx->pre.program_counter; ++pc)
1141 [ + + ]: 3506 : if (BPF_CLASS(ins_buf[pc].code) == BPF_JMP) {
1142 : : RTE_ASSERT(ins_buf[pc].off == 0);
1143 : 2135 : ins_buf[pc].off = exit_pc - (pc + 1);
1144 : : }
1145 : :
1146 : 417 : const uint32_t nb_ins = ins - ins_buf;
1147 : 417 : return nb_ins;
1148 : : }
1149 : :
1150 : :
1151 : : /* VERIFICATION OF AN ARBITRARY INSTRUCTION */
1152 : :
1153 : : /* Invoked when invalid state is detected. */
1154 : : static int
1155 : 5831 : invalid_state_cb(struct rte_bpf_validate_debug *debug, void *void_ctx)
1156 : : {
1157 : : struct verify_instruction_context *const ctx = void_ctx;
1158 : :
1159 : 5831 : ++ctx->invalid_state_count;
1160 : :
1161 : 5831 : TEST_LOG_LINE(WARNING,
1162 : : "Invalid state detected at pc %u",
1163 : : rte_bpf_validate_debug_get_pc(debug));
1164 : :
1165 : : RTE_SET_USED(debug);
1166 : :
1167 : 5831 : return TEST_SUCCESS;
1168 : : }
1169 : :
1170 : : static int
1171 : 996 : point_callback(struct rte_bpf_validate_debug *debug, const struct verify_instruction_context *ctx,
1172 : : struct point_context *point_ctx, const struct state *state)
1173 : : {
1174 [ - + ]: 996 : TEST_ASSERT_EQUAL(point_ctx->hit_count, 0, "not called before");
1175 : :
1176 : 996 : const uint32_t pc = rte_bpf_validate_debug_get_pc(debug);
1177 [ - + ]: 996 : TEST_ASSERT_EQUAL(pc, point_ctx->program_counter,
1178 : : "Expected program counter: %" PRIu32 ", found: %" PRIu32,
1179 : : point_ctx->program_counter, pc);
1180 : :
1181 [ + - ]: 996 : if (ctx->dst_reg != NO_REGISTER) {
1182 : 996 : format_register(debug, point_ctx->formatted_dst,
1183 : : sizeof(point_ctx->formatted_dst), ctx->dst_reg);
1184 : :
1185 [ + + ]: 996 : if (state->dst.is_defined) {
1186 [ - + ]: 995 : TEST_ASSERT_SUCCESS(
1187 : : check_domain(debug, ctx->dst_reg, &state->dst,
1188 : : ctx->base_reg, ctx->prm.area_size),
1189 : : "dst domain check");
1190 : 995 : TEST_LOG_LINE(DEBUG, "Successfully checked r%hhu.", ctx->dst_reg);
1191 : : } else
1192 : 1 : TEST_LOG_LINE(DEBUG, "Not checking undefined r%hhu.", ctx->dst_reg);
1193 : : }
1194 : :
1195 [ + + ]: 996 : if (ctx->src_reg != NO_REGISTER) {
1196 : 843 : format_register(debug, point_ctx->formatted_src,
1197 : : sizeof(point_ctx->formatted_src), ctx->src_reg);
1198 : :
1199 [ + - ]: 843 : if (state->src.is_defined) {
1200 [ - + ]: 843 : TEST_ASSERT_SUCCESS(
1201 : : check_domain(debug, ctx->src_reg, &state->src,
1202 : : ctx->base_reg, ctx->prm.area_size),
1203 : : "src domain check");
1204 : 843 : TEST_LOG_LINE(DEBUG, "Successfully checked r%hhu.", ctx->src_reg);
1205 : : } else
1206 : 0 : TEST_LOG_LINE(DEBUG, "Not checking undefined r%hhu.", ctx->src_reg);
1207 : : }
1208 : :
1209 : 996 : ++point_ctx->hit_count;
1210 : :
1211 : 996 : return TEST_SUCCESS;
1212 : : }
1213 : :
1214 : : /*
1215 : : * Invoked before the tested instruction and checks pre-conditions.
1216 : : *
1217 : : * Also formats registers in the pre state for postmortem, if needed.
1218 : : */
1219 : : static int
1220 : 417 : pre_callback(struct rte_bpf_validate_debug *debug, void *void_ctx)
1221 : : {
1222 : : struct verify_instruction_context *const ctx = void_ctx;
1223 : :
1224 : 417 : TEST_LOG_LINE(DEBUG, "Pre callback invoked.");
1225 : :
1226 [ - + ]: 417 : TEST_ASSERT_SUCCESS(
1227 : : point_callback(debug, ctx, &ctx->pre, &ctx->prm.pre),
1228 : : "pre-state check");
1229 : :
1230 : : return TEST_SUCCESS;
1231 : : }
1232 : :
1233 : : /* Invoked after the tested instruction and checks post-conditions. */
1234 : : static int
1235 : 336 : post_callback(struct rte_bpf_validate_debug *debug, void *void_ctx)
1236 : : {
1237 : : struct verify_instruction_context *const ctx = void_ctx;
1238 : :
1239 : 336 : TEST_LOG_LINE(DEBUG, "Post callback invoked.");
1240 : :
1241 [ - + ]: 336 : TEST_ASSERT_SUCCESS(
1242 : : point_callback(debug, ctx, &ctx->post, &ctx->prm.post),
1243 : : "post-state check");
1244 : :
1245 : : return TEST_SUCCESS;
1246 : : }
1247 : :
1248 : : /* Invoked after the tested instruction jumped and checks jump post-conditions. */
1249 : : static int
1250 : 243 : jump_callback(struct rte_bpf_validate_debug *debug, void *void_ctx)
1251 : : {
1252 : : struct verify_instruction_context *const ctx = void_ctx;
1253 : :
1254 : 243 : TEST_LOG_LINE(DEBUG, "Jump callback invoked.");
1255 : :
1256 [ - + ]: 243 : TEST_ASSERT_SUCCESS(
1257 : : point_callback(debug, ctx, &ctx->jump, &ctx->prm.jump),
1258 : : "jump-state check");
1259 : :
1260 : : return TEST_SUCCESS;
1261 : : }
1262 : :
1263 : : static int
1264 : 417 : debug_validation(struct verify_instruction_context *ctx, const struct rte_ebpf_insn *ins,
1265 : : uint32_t nb_ins)
1266 : : {
1267 : 417 : struct rte_bpf_validate_debug *const debug = rte_bpf_validate_debug_create();
1268 [ - + ]: 417 : TEST_ASSERT_NOT_NULL(debug, "validate debug create error %d", rte_errno);
1269 : :
1270 : 417 : const struct rte_bpf_prm_ex prm = {
1271 : : .sz = sizeof(struct rte_bpf_prm_ex),
1272 : : .origin = RTE_BPF_ORIGIN_RAW,
1273 : : .raw.ins = ins,
1274 : : .raw.nb_ins = nb_ins,
1275 : : .prog_arg[0] = {
1276 : : .type = RTE_BPF_ARG_PTR,
1277 : 417 : .size = ctx->prm.area_size,
1278 : : },
1279 : : .nb_prog_arg = 1,
1280 : : .debug = debug,
1281 : : };
1282 : :
1283 : : /* Catch invalid states. */
1284 [ - + ]: 417 : TEST_ASSERT_NOT_NULL(rte_bpf_validate_debug_catch(debug,
1285 : : RTE_BPF_VALIDATE_DEBUG_EVENT_INVALID_STATE,
1286 : : &(struct rte_bpf_validate_debug_callback){
1287 : : .fn = invalid_state_cb,
1288 : : .ctx = ctx,
1289 : : }), "add catchpoint error %d", rte_errno);
1290 : :
1291 : : /* Break on pre test instruction. */
1292 [ - + ]: 417 : TEST_ASSERT_NOT_NULL(rte_bpf_validate_debug_break(debug, ctx->pre.program_counter,
1293 : : &(struct rte_bpf_validate_debug_callback){
1294 : : .fn = pre_callback,
1295 : : .ctx = ctx,
1296 : : }), "add pre breakpoint error %d", rte_errno);
1297 : :
1298 : : /* Break on post test instruction. */
1299 [ - + ]: 417 : TEST_ASSERT_NOT_NULL(rte_bpf_validate_debug_break(debug, ctx->post.program_counter,
1300 : : &(struct rte_bpf_validate_debug_callback){
1301 : : .fn = post_callback,
1302 : : .ctx = ctx,
1303 : : }), "add post breakpoint error %d", rte_errno);
1304 : :
1305 [ + + ]: 417 : if (ctx->jump.program_counter != NO_PROGRAM_COUNTER)
1306 : : /* Break on jump test instruction. */
1307 [ - + ]: 324 : TEST_ASSERT_NOT_NULL(rte_bpf_validate_debug_break(debug, ctx->jump.program_counter,
1308 : : &(struct rte_bpf_validate_debug_callback){
1309 : : .fn = jump_callback,
1310 : : .ctx = ctx,
1311 : : }), "add jump breakpoint error %d", rte_errno);
1312 : :
1313 : 417 : struct rte_bpf *const bpf = rte_bpf_load_ex(&prm);
1314 : 417 : const int validation_errno = rte_errno;
1315 : :
1316 : 417 : rte_bpf_destroy(bpf);
1317 : 417 : rte_bpf_validate_debug_destroy(debug);
1318 : :
1319 [ - + ]: 417 : TEST_ASSERT_NOT_NULL(bpf, "validation error %d", validation_errno);
1320 : :
1321 [ - + ]: 417 : TEST_ASSERT_EQUAL(ctx->pre.hit_count, !ctx->prm.pre.is_unreachable,
1322 : : "pre hit_count = %d", ctx->pre.hit_count);
1323 [ - + ]: 417 : TEST_ASSERT_EQUAL(ctx->post.hit_count, !ctx->prm.post.is_unreachable,
1324 : : "post hit_count = %d", ctx->post.hit_count);
1325 [ - + ]: 417 : TEST_ASSERT_EQUAL(ctx->jump.hit_count, !ctx->prm.jump.is_unreachable,
1326 : : "jump hit_count = %d", ctx->jump.hit_count);
1327 : :
1328 : : return TEST_SUCCESS;
1329 : : }
1330 : :
1331 : : /* Dump whole program to log. */
1332 : : static void
1333 : 0 : log_program_dump(const struct rte_ebpf_insn *ins, uint32_t nb_ins, uint32_t pre_pc)
1334 : : {
1335 : : char hexadecimal[DISASSEMBLY_FORMAT_BUFFER_SIZE];
1336 : : char disassembly[DISASSEMBLY_FORMAT_BUFFER_SIZE];
1337 : :
1338 : 0 : TEST_LOG_LINE(NOTICE, "\tTested program:");
1339 [ # # ]: 0 : for (uint32_t pc = 0; pc != nb_ins; ++pc) {
1340 : 0 : rte_bpf_format(hexadecimal, sizeof(hexadecimal), &ins[pc], pc,
1341 : : RTE_BPF_FORMAT_FLAG_HEXADECIMAL |
1342 : : RTE_BPF_FORMAT_FLAG_NEVER_WIDE);
1343 : 0 : rte_bpf_format(disassembly, sizeof(disassembly), &ins[pc], pc,
1344 : : RTE_BPF_FORMAT_FLAG_DISASSEMBLY |
1345 : : RTE_BPF_FORMAT_FLAG_ABSOLUTE_JUMPS);
1346 [ # # ]: 0 : TEST_LOG_LINE(NOTICE, "\t%5u: \t%s \t%s%s",
1347 : : pc, hexadecimal, disassembly,
1348 : : pc != pre_pc ? "" : " ; tested instruction");
1349 : :
1350 [ # # ]: 0 : if (!rte_bpf_insn_is_wide(&ins[pc]))
1351 : 0 : continue;
1352 : :
1353 : 0 : ++pc;
1354 : :
1355 : 0 : rte_bpf_format(hexadecimal, sizeof(hexadecimal), &ins[pc], pc,
1356 : : RTE_BPF_FORMAT_FLAG_HEXADECIMAL |
1357 : : RTE_BPF_FORMAT_FLAG_NEVER_WIDE);
1358 : 0 : TEST_LOG_LINE(NOTICE, "\t%6s \t%s", "", hexadecimal);
1359 : : }
1360 : 0 : }
1361 : :
1362 : : static void
1363 : 0 : log_formatted_registers(const char *heading, const struct verify_instruction_context *ctx,
1364 : : const struct point_context *point_ctx)
1365 : : {
1366 : : char register_name[8];
1367 : :
1368 : 0 : TEST_LOG_LINE(NOTICE, "\t%s", heading);
1369 [ # # ]: 0 : if (ctx->dst_reg != NO_REGISTER) {
1370 : 0 : snprintf(register_name, sizeof(register_name), "r%hhu", ctx->dst_reg);
1371 : 0 : TEST_LOG_LINE(NOTICE, "\t%5s: \t%s", register_name, point_ctx->formatted_dst);
1372 : : }
1373 [ # # ]: 0 : if (ctx->src_reg != NO_REGISTER) {
1374 : 0 : snprintf(register_name, sizeof(register_name), "r%hhu", ctx->src_reg);
1375 : 0 : TEST_LOG_LINE(NOTICE, "\t%5s: \t%s", register_name, point_ctx->formatted_src);
1376 : : }
1377 : 0 : }
1378 : :
1379 : : /*
1380 : : * Verify instruction validation behaviour described by prm.
1381 : : *
1382 : : * Generate the program containing specified instruction on the code path with
1383 : : * specified register pre-domains and verify specified register post-domains.
1384 : : *
1385 : : * See comment to `generate_program` for more requirements and limitations.
1386 : : */
1387 : : static int
1388 : 417 : verify_instruction(struct verify_instruction_param prm)
1389 : : {
1390 : 417 : struct verify_instruction_context ctx = {
1391 : : .prm = prm,
1392 : : };
1393 : : struct rte_ebpf_insn ins_buf[64];
1394 : :
1395 : 417 : const uint32_t nb_ins = generate_program(&ctx, ins_buf);
1396 : : RTE_ASSERT(nb_ins <= RTE_DIM(ins_buf));
1397 : :
1398 : 417 : const int rc = debug_validation(&ctx, ins_buf, nb_ins);
1399 : :
1400 : : /* Log more data at DEBUG level on success, NOTICE on failure. */
1401 [ + - - + ]: 417 : if (rte_log_can_log(RTE_LOGTYPE_TEST_BPF_VALIDATE, RTE_LOG_DEBUG) ||
1402 : : rc != TEST_SUCCESS) {
1403 : 0 : log_program_dump(ins_buf, nb_ins, ctx.pre.program_counter);
1404 : 0 : log_formatted_registers("Pre-state:", &ctx, &ctx.pre);
1405 : 0 : log_formatted_registers("Post-state:", &ctx, &ctx.post);
1406 [ # # ]: 0 : if (ctx.jump.program_counter != NO_PROGRAM_COUNTER)
1407 : 0 : log_formatted_registers("Jump-state:", &ctx, &ctx.jump);
1408 : : }
1409 : :
1410 : 417 : return rc;
1411 : : }
1412 : :
1413 : : static int
1414 : 808 : opcode_comparison_index(uint8_t opcode)
1415 : : {
1416 [ + - ]: 6260 : for (int index = 0; index != RTE_DIM(comparisons_opcode); ++index)
1417 [ + + ]: 6260 : if (comparisons_opcode[index] == opcode)
1418 : 808 : return index;
1419 : 0 : TEST_LOG_LINE(ERR, "Unsupported or not a comparison opcode: %hhx", opcode);
1420 : 0 : RTE_VERIFY(false);
1421 : : }
1422 : :
1423 : : /* Change two-register comparison verification to immediate one. */
1424 : : static bool
1425 : 304 : make_comparison_immediate(struct verify_instruction_param *prm)
1426 : : {
1427 : 304 : int comparison_index = opcode_comparison_index(prm->tested_instruction.code);
1428 : 304 : const int64_t value = prm->pre.src.s.min;
1429 : :
1430 [ - + ]: 304 : if ((comparison_index & COMPARISON_INDEX_IMMEDIATE) != 0) {
1431 : 0 : TEST_LOG_LINE(ERR, "Comparison %hhx is already immediate.",
1432 : : prm->tested_instruction.code);
1433 : 0 : RTE_VERIFY(false);
1434 : : }
1435 : :
1436 [ + + + - : 432 : if (!domain_is_singleton(&prm->pre.src) || !domain_is_singleton(&prm->post.src) ||
- + ]
1437 : : !domain_is_singleton(&prm->jump.src)) {
1438 : 240 : TEST_LOG_LINE(DEBUG, "Cannot make immediate out of a non-singleton domain.");
1439 : 240 : return false;
1440 : : }
1441 [ + - + - : 64 : if (prm->pre.src.is_pointer || prm->post.src.is_pointer || prm->jump.src.is_pointer) {
- + ]
1442 : 0 : TEST_LOG_LINE(DEBUG, "Cannot make immediate out of a pointer.");
1443 : 0 : return false;
1444 : : }
1445 [ + + + + ]: 64 : if (prm->post.src.s.min != value || prm->jump.src.s.min != value) {
1446 : 46 : TEST_LOG_LINE(DEBUG, "Cannot make immediate if the value changes.");
1447 : 46 : return false;
1448 : : }
1449 [ - + ]: 18 : if (!fits_in_imm32(value)) {
1450 : 0 : TEST_LOG_LINE(ERR, "Cannot make immediate unless value fits in int32.");
1451 : 0 : return false;
1452 : : }
1453 : :
1454 : 18 : comparison_index |= COMPARISON_INDEX_IMMEDIATE;
1455 : 18 : prm->tested_instruction.code = comparisons_opcode[comparison_index];
1456 : 18 : prm->tested_instruction.imm = value;
1457 : :
1458 [ - + ]: 18 : RTE_VERIFY(prm->pre.src.is_defined);
1459 : 18 : prm->pre.src.is_defined = false;
1460 : :
1461 [ + + ]: 18 : if (!prm->post.is_unreachable) {
1462 [ - + ]: 17 : RTE_VERIFY(prm->post.src.is_defined);
1463 : 17 : prm->post.src.is_defined = false;
1464 : : }
1465 : :
1466 [ + + ]: 18 : if (!prm->jump.is_unreachable) {
1467 [ - + ]: 17 : RTE_VERIFY(prm->jump.src.is_defined);
1468 : 17 : prm->jump.src.is_defined = false;
1469 : : }
1470 : :
1471 : : return true;
1472 : : }
1473 : :
1474 : : /* Change immediate comparison verification to two-register one. */
1475 : : static void
1476 : 12 : make_comparison_two_register(struct verify_instruction_param *prm)
1477 : : {
1478 : 12 : int comparison_index = opcode_comparison_index(prm->tested_instruction.code);
1479 : 12 : const int64_t value = prm->tested_instruction.imm;
1480 : :
1481 [ - + ]: 12 : if ((comparison_index & COMPARISON_INDEX_IMMEDIATE) == 0) {
1482 : 0 : TEST_LOG_LINE(ERR, "Comparison %hhx is already two-register.",
1483 : : prm->tested_instruction.code);
1484 : 0 : RTE_VERIFY(false);
1485 : : }
1486 : :
1487 : 12 : comparison_index &= ~COMPARISON_INDEX_IMMEDIATE;
1488 : 12 : prm->tested_instruction.code = comparisons_opcode[comparison_index];
1489 : 12 : prm->tested_instruction.imm = 0;
1490 : :
1491 [ - + ]: 12 : RTE_VERIFY(!prm->pre.src.is_defined);
1492 : 12 : prm->pre.src = make_singleton_domain(value);
1493 : :
1494 [ + + ]: 12 : if (!prm->post.is_unreachable) {
1495 [ - + ]: 8 : RTE_VERIFY(!prm->post.src.is_defined);
1496 : 8 : prm->post.src = prm->pre.src;
1497 : : }
1498 : :
1499 [ + + ]: 12 : if (!prm->jump.is_unreachable) {
1500 [ - + ]: 8 : RTE_VERIFY(!prm->jump.src.is_defined);
1501 : 8 : prm->jump.src = prm->pre.src;
1502 : : }
1503 : 12 : }
1504 : :
1505 : : /* Change comparison verification to complement (negated result) one. */
1506 : : static void
1507 : 152 : make_comparison_complement(struct verify_instruction_param *prm)
1508 : : {
1509 : 152 : int comparison_index = opcode_comparison_index(prm->tested_instruction.code);
1510 : 152 : comparison_index ^= COMPARISON_INDEX_GREATER | COMPARISON_INDEX_INCLUSIVE;
1511 : 152 : prm->tested_instruction.code = comparisons_opcode[comparison_index];
1512 : 152 : RTE_SWAP(prm->post, prm->jump);
1513 : 152 : }
1514 : :
1515 : : /* Change comparison verification to converse (swapped operands) one. */
1516 : : static void
1517 : 304 : make_comparison_converse(struct verify_instruction_param *prm)
1518 : : {
1519 : 304 : int comparison_index = opcode_comparison_index(prm->tested_instruction.code);
1520 : 304 : comparison_index ^= COMPARISON_INDEX_GREATER;
1521 : 304 : prm->tested_instruction.code = comparisons_opcode[comparison_index];
1522 : 304 : RTE_SWAP(prm->pre.dst, prm->pre.src);
1523 : 304 : RTE_SWAP(prm->post.dst, prm->post.src);
1524 : 304 : RTE_SWAP(prm->jump.dst, prm->jump.src);
1525 : 304 : }
1526 : :
1527 : : /* Change signed comparison verification to unsigned one. */
1528 : : static void
1529 : 36 : make_comparison_signed(struct verify_instruction_param *prm)
1530 : : {
1531 : 36 : int comparison_index = opcode_comparison_index(prm->tested_instruction.code);
1532 [ - + ]: 36 : if ((comparison_index & COMPARISON_INDEX_SIGNED) != 0) {
1533 : 0 : TEST_LOG_LINE(ERR, "Comparison %hhx is already signed.",
1534 : : prm->tested_instruction.code);
1535 : 0 : RTE_VERIFY(false);
1536 : : }
1537 : 36 : comparison_index |= COMPARISON_INDEX_SIGNED;
1538 : 36 : prm->tested_instruction.code = comparisons_opcode[comparison_index];
1539 : 36 : }
1540 : :
1541 : : /* Verify specified two-register comparison and, if possible, immediate one. */
1542 : : static int
1543 : 304 : verify_comparison_subcase(struct verify_instruction_param prm)
1544 : : {
1545 [ - + ]: 304 : TEST_ASSERT_SUCCESS(verify_instruction(prm), "two-register version check");
1546 : :
1547 [ + + ]: 304 : if (make_comparison_immediate(&prm))
1548 [ - + ]: 18 : TEST_ASSERT_SUCCESS(verify_instruction(prm), "immediate version check");
1549 : :
1550 : : return TEST_SUCCESS;
1551 : : }
1552 : :
1553 : : /*
1554 : : * Verify comparison instruction validation behaviour.
1555 : : *
1556 : : * Call `verify_instruction` for all valid variations of the instruction.
1557 : : *
1558 : : * For instance, `jgt r2, r3` verifies:
1559 : : * * `jgt r2, r3`;
1560 : : * * `jlt r3, r2` src and dst swapped with each other;
1561 : : * * `jle r2, r3` with post and jump domains swapped with each other;
1562 : : * * `jge r3, r2` with all corresponding swaps;
1563 : : * * immediate versions of everything above where possible,
1564 : : * that is, register on the right is an int32 scalar singleton;
1565 : : * * signed versions of everything above if `also_signed` is true;
1566 : : *
1567 : : * Regardless if passed instruction compares with immediate or singleton src
1568 : : * both cases are generated and tested.
1569 : : */
1570 : : static int
1571 : 40 : verify_comparison(struct verify_instruction_param prm, bool also_signed)
1572 : : {
1573 : 40 : fill_verify_instruction_defaults(&prm);
1574 : :
1575 [ + + ]: 40 : if (!prm.pre.src.is_defined)
1576 : : /* Convert from immediate form to simplify further logic. */
1577 : 12 : make_comparison_two_register(&prm);
1578 : :
1579 : : /* All reachable domains must be defined by this point. */
1580 [ - + ]: 40 : RTE_VERIFY(prm.pre.dst.is_defined);
1581 [ - + ]: 40 : RTE_VERIFY(prm.pre.src.is_defined);
1582 [ + + ]: 40 : if (!prm.post.is_unreachable) {
1583 [ - + ]: 31 : RTE_VERIFY(prm.post.dst.is_defined);
1584 [ - + ]: 31 : RTE_VERIFY(prm.post.src.is_defined);
1585 : : }
1586 [ + + ]: 40 : if (!prm.jump.is_unreachable) {
1587 [ - + ]: 27 : RTE_VERIFY(prm.jump.dst.is_defined);
1588 [ + - ]: 27 : RTE_VERIFY(prm.jump.src.is_defined);
1589 : : }
1590 : :
1591 [ + + ]: 116 : for (int make_signed = 0; make_signed <= also_signed; ++make_signed) {
1592 [ + + ]: 76 : if (make_signed)
1593 : 36 : make_comparison_signed(&prm);
1594 : :
1595 [ + + ]: 228 : for (int complement = false; complement <= true; ++complement) {
1596 : :
1597 [ + + ]: 456 : for (int converse = false; converse <= true; ++converse) {
1598 : :
1599 [ - + ]: 304 : TEST_ASSERT_SUCCESS(verify_comparison_subcase(prm),
1600 : : "make_signed=%d, complement=%d, converse=%d",
1601 : : make_signed, complement, converse);
1602 : :
1603 : 304 : make_comparison_converse(&prm);
1604 : : }
1605 : :
1606 : 152 : make_comparison_complement(&prm);
1607 : : }
1608 : : }
1609 : :
1610 : : return TEST_SUCCESS;
1611 : : }
1612 : :
1613 : :
1614 : : /* TESTS FOR SPECIFIC INSTRUCTIONS */
1615 : :
1616 : : /* 32-bit bitwise AND between a scalar range and immediate, triggering UB. */
1617 : : static int
1618 : 1 : test_alu32_and_k_ub(void)
1619 : : {
1620 : 1 : return verify_instruction((struct verify_instruction_param){
1621 : : .tested_instruction = {
1622 : : .code = (BPF_ALU | BPF_AND | BPF_K),
1623 : : .imm = INT32_MAX,
1624 : : },
1625 : : .pre.dst = make_unsigned_domain(0, INT32_MAX),
1626 : : .post.dst = make_unsigned_domain(0, INT32_MAX),
1627 : : });
1628 : : }
1629 : :
1630 : : /* 32-bit bitwise OR between a scalar range and immediate, triggering UB. */
1631 : : static int
1632 : 1 : test_alu32_or_k_ub(void)
1633 : : {
1634 : 1 : return verify_instruction((struct verify_instruction_param){
1635 : : .tested_instruction = {
1636 : : .code = (BPF_ALU | BPF_OR | BPF_K),
1637 : : .imm = 0,
1638 : : },
1639 : : .pre.dst = make_unsigned_domain(0, INT32_MAX),
1640 : : .post.dst = make_unsigned_domain(0, INT32_MAX),
1641 : : });
1642 : : }
1643 : :
1644 : : /* 32-bit bitwise XOR between a scalar range and immediate, triggering UB. */
1645 : : static int
1646 : 1 : test_alu32_xor_k_ub(void)
1647 : : {
1648 : 1 : return verify_instruction((struct verify_instruction_param){
1649 : : .tested_instruction = {
1650 : : .code = (BPF_ALU | BPF_XOR | BPF_K),
1651 : : .imm = 0,
1652 : : },
1653 : : .pre.dst = make_unsigned_domain(0, INT32_MAX),
1654 : : .post.dst = make_unsigned_domain(0, INT32_MAX),
1655 : : });
1656 : : }
1657 : :
1658 : : /* 64-bit addition of immediate to a range. */
1659 : : static int
1660 : 1 : test_alu64_add_k(void)
1661 : : {
1662 : 1 : return verify_instruction((struct verify_instruction_param){
1663 : : .tested_instruction = {
1664 : : .code = (EBPF_ALU64 | BPF_ADD | BPF_K),
1665 : : .imm = 17,
1666 : : },
1667 : : .pre.dst = make_signed_domain(11, 29),
1668 : : .post.dst = make_signed_domain(11 + 17, 29 + 17),
1669 : : });
1670 : : }
1671 : :
1672 : : /* 64-bit addition of immediate to a pointer range. */
1673 : : static int
1674 : 1 : test_alu64_add_k_pointer(void)
1675 : : {
1676 : 2 : return verify_instruction((struct verify_instruction_param){
1677 : : .tested_instruction = {
1678 : : .code = (EBPF_ALU64 | BPF_ADD | BPF_K),
1679 : : .imm = 17,
1680 : : },
1681 : : .area_size = 256,
1682 : : .pre.dst = make_pointer_domain(11, 29),
1683 : : .post.dst = make_pointer_domain(11 + 17, 29 + 17),
1684 : : });
1685 : : }
1686 : :
1687 : : /* 64-bit addition of pointer to a pointer. */
1688 : : static int
1689 : 1 : test_alu64_add_x_pointer_pointer(void)
1690 : : {
1691 : 2 : return verify_instruction((struct verify_instruction_param){
1692 : : .tested_instruction = {
1693 : : .code = (EBPF_ALU64 | BPF_ADD | BPF_X),
1694 : : },
1695 : : .area_size = 256,
1696 : : .pre.dst = make_pointer_domain(11, 29),
1697 : : .pre.src = make_pointer_domain(17, 23),
1698 : : .post.dst = unknown,
1699 : : });
1700 : : }
1701 : :
1702 : : /* 64-bit addition of scalar to a pointer. */
1703 : : static int
1704 : 1 : test_alu64_add_x_pointer_scalar(void)
1705 : : {
1706 : 2 : return verify_instruction((struct verify_instruction_param){
1707 : : .tested_instruction = {
1708 : : .code = (EBPF_ALU64 | BPF_ADD | BPF_X),
1709 : : },
1710 : : .area_size = 256,
1711 : : .pre.dst = make_pointer_domain(11, 29),
1712 : : .pre.src = make_signed_domain(17, 23),
1713 : : .post.dst = make_pointer_domain(11 + 17, 29 + 23),
1714 : : });
1715 : : }
1716 : :
1717 : : /* 64-bit addition of pointer to a scalar. */
1718 : : static int
1719 : 1 : test_alu64_add_x_scalar_pointer(void)
1720 : : {
1721 : 2 : return verify_instruction((struct verify_instruction_param){
1722 : : .tested_instruction = {
1723 : : .code = (EBPF_ALU64 | BPF_ADD | BPF_X),
1724 : : },
1725 : : .area_size = 256,
1726 : : .pre.dst = make_signed_domain(11, 29),
1727 : : .pre.src = make_pointer_domain(17, 23),
1728 : : .post.dst = make_pointer_domain(11 + 17, 29 + 23),
1729 : : });
1730 : : }
1731 : :
1732 : : /* 64-bit addition of scalar to a scalar. */
1733 : : static int
1734 : 1 : test_alu64_add_x_scalar_scalar(void)
1735 : : {
1736 : 1 : return verify_instruction((struct verify_instruction_param){
1737 : : .tested_instruction = {
1738 : : .code = (EBPF_ALU64 | BPF_ADD | BPF_X),
1739 : : },
1740 : : .area_size = 256,
1741 : : .pre.dst = make_signed_domain(11, 29),
1742 : : .pre.src = make_signed_domain(17, 23),
1743 : : .post.dst = make_signed_domain(11 + 17, 29 + 23),
1744 : : });
1745 : : }
1746 : :
1747 : : /* 64-bit bitwise AND between a scalar range and immediate. */
1748 : : static int
1749 : 1 : test_alu64_and_k(void)
1750 : : {
1751 : 1 : return verify_instruction((struct verify_instruction_param){
1752 : : .tested_instruction = {
1753 : : .code = (EBPF_ALU64 | BPF_AND | BPF_K),
1754 : : .imm = 5,
1755 : : },
1756 : : .pre.dst = make_signed_domain(6, 8),
1757 : : .post.dst = make_signed_domain(0, 7),
1758 : : });
1759 : : }
1760 : :
1761 : : /* 64-bit division and modulo of UINT64_MAX*2/3. */
1762 : : static int
1763 : 1 : test_alu64_div_mod_big_constant(void)
1764 : : {
1765 : : const uint64_t dividend = UINT64_MAX / 3 * 2;
1766 : : static const uint64_t divisors[] = {
1767 : : 1,
1768 : : 2,
1769 : : 3,
1770 : : UINT64_MAX / 3,
1771 : : INT64_MAX,
1772 : : INT64_MIN,
1773 : : UINT64_MAX / 3 * 2,
1774 : : UINT64_MAX / 4 * 3,
1775 : : UINT64_MAX,
1776 : : };
1777 [ + + ]: 10 : for (int index = 0; index != RTE_DIM(divisors); ++index) {
1778 : 9 : const uint64_t divisor = divisors[index];
1779 : :
1780 [ - + ]: 9 : TEST_ASSERT_SUCCESS(verify_instruction((struct verify_instruction_param){
1781 : : .tested_instruction = {
1782 : : .code = (EBPF_ALU64 | BPF_DIV | BPF_X),
1783 : : },
1784 : : .pre.dst = make_singleton_domain(dividend),
1785 : : .pre.src = make_singleton_domain(divisor),
1786 : : .post.dst = make_singleton_domain(dividend / divisor),
1787 : : }), "(EBPF_ALU64 | BPF_DIV | BPF_X) check, index=%d", index);
1788 : :
1789 [ - + ]: 9 : TEST_ASSERT_SUCCESS(verify_instruction((struct verify_instruction_param){
1790 : : .tested_instruction = {
1791 : : .code = (EBPF_ALU64 | BPF_MOD | BPF_X),
1792 : : },
1793 : : .pre.dst = make_singleton_domain(dividend),
1794 : : .pre.src = make_singleton_domain(divisor),
1795 : : .post.dst = make_singleton_domain(dividend % divisor),
1796 : : }), "(EBPF_ALU64 | BPF_MOD | BPF_X) check, index=%d", index);
1797 : : }
1798 : :
1799 : : return TEST_SUCCESS;
1800 : : }
1801 : :
1802 : : /* 64-bit division and modulo of UINT64_MAX/3..UINT64_MAX*2/3 by a constant. */
1803 : : static int
1804 : 1 : test_alu64_div_mod_big_range(void)
1805 : : {
1806 : : const uint64_t dividend_first = UINT64_MAX / 3;
1807 : : const uint64_t dividend_last = UINT64_MAX / 3 * 2;
1808 : : static const uint64_t divisors[] = {
1809 : : 1,
1810 : : 2,
1811 : : 3,
1812 : : UINT64_MAX / 3,
1813 : : INT64_MAX,
1814 : : INT64_MIN,
1815 : : UINT64_MAX / 3 * 2,
1816 : : UINT64_MAX / 4 * 3,
1817 : : UINT64_MAX,
1818 : : };
1819 [ + + ]: 10 : for (int index = 0; index != RTE_DIM(divisors); ++index) {
1820 : 9 : const uint64_t divisor = divisors[index];
1821 : :
1822 [ - + ]: 9 : TEST_ASSERT_SUCCESS(verify_instruction((struct verify_instruction_param){
1823 : : .tested_instruction = {
1824 : : .code = (EBPF_ALU64 | BPF_DIV | BPF_X),
1825 : : },
1826 : : .pre.dst = make_unsigned_domain(dividend_first, dividend_last),
1827 : : .pre.src = make_singleton_domain(divisor),
1828 : : .post.dst = make_unsigned_domain(0, dividend_last),
1829 : : }), "(EBPF_ALU64 | BPF_DIV | BPF_X) check, index=%d", index);
1830 : :
1831 [ - + ]: 9 : TEST_ASSERT_SUCCESS(verify_instruction((struct verify_instruction_param){
1832 : : .tested_instruction = {
1833 : : .code = (EBPF_ALU64 | BPF_MOD | BPF_X),
1834 : : },
1835 : : .pre.dst = make_unsigned_domain(dividend_first, dividend_last),
1836 : : .pre.src = make_singleton_domain(divisor),
1837 : : .post.dst = make_unsigned_domain(0, RTE_MIN(dividend_last, divisor - 1)),
1838 : : }), "(EBPF_ALU64 | BPF_MOD | BPF_X) check, index=%d", index);
1839 : : }
1840 : :
1841 : : return TEST_SUCCESS;
1842 : : }
1843 : :
1844 : : /* 64-bit division and modulo of INT64_MIN by -1. */
1845 : : static int
1846 : 1 : test_alu64_div_mod_overflow(void)
1847 : : {
1848 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_instruction((struct verify_instruction_param){
1849 : : .tested_instruction = {
1850 : : .code = (EBPF_ALU64 | BPF_DIV | BPF_K),
1851 : : .imm = -1,
1852 : : },
1853 : : .pre.dst = make_singleton_domain(INT64_MIN),
1854 : : .post.dst = make_singleton_domain(0),
1855 : : }), "(EBPF_ALU64 | BPF_DIV | BPF_K) check");
1856 : :
1857 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_instruction((struct verify_instruction_param){
1858 : : .tested_instruction = {
1859 : : .code = (EBPF_ALU64 | BPF_DIV | BPF_X),
1860 : : },
1861 : : .pre.dst = make_singleton_domain(INT64_MIN),
1862 : : .pre.src = make_singleton_domain(-1),
1863 : : .post.dst = make_singleton_domain(0),
1864 : : }), "(EBPF_ALU64 | BPF_DIV | BPF_X) check");
1865 : :
1866 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_instruction((struct verify_instruction_param){
1867 : : .tested_instruction = {
1868 : : .code = (EBPF_ALU64 | BPF_MOD | BPF_K),
1869 : : .imm = -1,
1870 : : },
1871 : : .pre.dst = make_singleton_domain(INT64_MIN),
1872 : : .post.dst = make_singleton_domain(INT64_MIN),
1873 : : }), "(EBPF_ALU64 | BPF_MOD | BPF_K) check");
1874 : :
1875 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_instruction((struct verify_instruction_param){
1876 : : .tested_instruction = {
1877 : : .code = (EBPF_ALU64 | BPF_MOD | BPF_X),
1878 : : },
1879 : : .pre.dst = make_singleton_domain(INT64_MIN),
1880 : : .pre.src = make_singleton_domain(-1),
1881 : : .post.dst = make_singleton_domain(INT64_MIN),
1882 : : }), "(EBPF_ALU64 | BPF_MOD | BPF_X) check");
1883 : :
1884 : 1 : return TEST_SUCCESS;
1885 : : }
1886 : :
1887 : : /* 64-bit left shift by 63. */
1888 : : static int
1889 : 1 : test_alu64_lsh_63(void)
1890 : : {
1891 : 1 : return verify_instruction((struct verify_instruction_param){
1892 : : .tested_instruction = {
1893 : : .code = (EBPF_ALU64 | BPF_LSH | BPF_K),
1894 : : .imm = 63,
1895 : : },
1896 : : .pre.dst = make_signed_domain(3, 5),
1897 : : .post.dst = unknown,
1898 : : });
1899 : : }
1900 : :
1901 : : /* 64-bit multiplication of constant and immediate with overflow. */
1902 : : static int
1903 : 1 : test_alu64_mul_k_overflow(void)
1904 : : {
1905 : 1 : return verify_instruction((struct verify_instruction_param){
1906 : : .tested_instruction = {
1907 : : .code = (EBPF_ALU64 | BPF_MUL | BPF_K),
1908 : : .imm = 0x12345678,
1909 : : },
1910 : : .pre.dst = make_singleton_domain(0x9876543210),
1911 : : .post.dst = make_singleton_domain(0x9876543210u * 0x12345678),
1912 : : });
1913 : : }
1914 : :
1915 : : /* 64-bit mul of small scalar range and immediate. */
1916 : : static int
1917 : 1 : test_alu64_mul_k_range_small(void)
1918 : : {
1919 : 1 : return verify_instruction((struct verify_instruction_param){
1920 : : .tested_instruction = {
1921 : : .code = (EBPF_ALU64 | BPF_MUL | BPF_K),
1922 : : .imm = 11,
1923 : : },
1924 : : .pre.dst = make_unsigned_domain(17, 29),
1925 : : .post.dst = make_unsigned_domain(17 * 11, 29 * 11),
1926 : : });
1927 : : }
1928 : :
1929 : : /* 64-bit negation when interval first element is INT64_MIN. */
1930 : : static int
1931 : 1 : test_alu64_neg_int64min_first(void)
1932 : : {
1933 : : static const int64_t other_values[] = {
1934 : : INT64_MIN,
1935 : : INT64_MIN + 1,
1936 : : INT64_MIN + 13,
1937 : : -17,
1938 : : -1,
1939 : : 0,
1940 : : 1,
1941 : : 19,
1942 : : INT64_MAX - 23,
1943 : : INT64_MAX - 1,
1944 : : INT64_MAX,
1945 : : };
1946 [ + + ]: 12 : for (int other_index = 0; other_index != RTE_DIM(other_values); ++other_index) {
1947 : 11 : const int64_t other_value = other_values[other_index];
1948 [ + + - + ]: 11 : TEST_ASSERT_SUCCESS(verify_instruction((struct verify_instruction_param){
1949 : : .tested_instruction = {
1950 : : .code = (EBPF_ALU64 | BPF_NEG),
1951 : : },
1952 : : .pre.dst = make_signed_domain(INT64_MIN, other_value),
1953 : : .post.dst = other_value > 0 ? unknown :
1954 : : make_unsigned_domain(-(uint64_t)other_value, INT64_MIN),
1955 : : }), "other_index=%d", other_index);
1956 : : }
1957 : : return TEST_SUCCESS;
1958 : : }
1959 : :
1960 : : /* 64-bit negation when interval last element is INT64_MIN. */
1961 : : static int
1962 : 1 : test_alu64_neg_int64min_last(void)
1963 : : {
1964 : : static const uint64_t other_values[] = {
1965 : : 0,
1966 : : 1,
1967 : : 19,
1968 : : INT64_MAX - 23,
1969 : : INT64_MAX - 1,
1970 : : INT64_MAX,
1971 : : INT64_MIN,
1972 : : };
1973 [ + + ]: 8 : for (int other_index = 0; other_index != RTE_DIM(other_values); ++other_index) {
1974 : 7 : const int64_t other_value = other_values[other_index];
1975 [ - + ]: 7 : TEST_ASSERT_SUCCESS(verify_instruction((struct verify_instruction_param){
1976 : : .tested_instruction = {
1977 : : .code = (EBPF_ALU64 | BPF_NEG),
1978 : : },
1979 : : .pre.dst = make_unsigned_domain(other_value, INT64_MIN),
1980 : : .post.dst = make_signed_domain(INT64_MIN, -(uint64_t)other_value),
1981 : : }), "other_index=%d", other_index);
1982 : : }
1983 : : return TEST_SUCCESS;
1984 : : }
1985 : :
1986 : : /* 64-bit negation when interval first element is zero. */
1987 : : static int
1988 : 1 : test_alu64_neg_zero_first(void)
1989 : : {
1990 : : static const uint64_t other_values[] = {
1991 : : 0,
1992 : : 1,
1993 : : 19,
1994 : : INT64_MAX - 23,
1995 : : INT64_MAX - 1,
1996 : : INT64_MAX,
1997 : : INT64_MIN,
1998 : : INT64_MIN + 1,
1999 : : INT64_MIN + 13,
2000 : : -17,
2001 : : -1,
2002 : : };
2003 [ + + ]: 12 : for (int other_index = 0; other_index != RTE_DIM(other_values); ++other_index) {
2004 : 11 : const uint64_t other_value = other_values[other_index];
2005 [ + + - + ]: 11 : TEST_ASSERT_SUCCESS(verify_instruction((struct verify_instruction_param){
2006 : : .tested_instruction = {
2007 : : .code = (EBPF_ALU64 | BPF_NEG),
2008 : : },
2009 : : .pre.dst = make_unsigned_domain(0, other_value),
2010 : : .post.dst = other_value > (uint64_t)INT64_MIN ? unknown :
2011 : : make_signed_domain(-(uint64_t)other_value, 0),
2012 : : }), "other_index=%d", other_index);
2013 : : }
2014 : : return TEST_SUCCESS;
2015 : : }
2016 : :
2017 : : /* 64-bit negation when interval last element is zero. */
2018 : : static int
2019 : 1 : test_alu64_neg_zero_last(void)
2020 : : {
2021 : : static const int64_t other_values[] = {
2022 : : INT64_MIN,
2023 : : INT64_MIN + 1,
2024 : : INT64_MIN + 13,
2025 : : -17,
2026 : : -1,
2027 : : 0,
2028 : : };
2029 [ + + ]: 7 : for (int other_index = 0; other_index != RTE_DIM(other_values); ++other_index) {
2030 : 6 : const int64_t other_value = other_values[other_index];
2031 [ - + ]: 6 : TEST_ASSERT_SUCCESS(verify_instruction((struct verify_instruction_param){
2032 : : .tested_instruction = {
2033 : : .code = (EBPF_ALU64 | BPF_NEG),
2034 : : },
2035 : : .pre.dst = make_signed_domain(other_value, 0),
2036 : : .post.dst = make_unsigned_domain(0, -(uint64_t)other_value),
2037 : : }), "other_index=%d", other_index);
2038 : : }
2039 : :
2040 : : return TEST_SUCCESS;
2041 : : }
2042 : :
2043 : : /* 64-bit bitwise OR between a positive scalar range and negative immediate. */
2044 : : static int
2045 : 1 : test_alu64_or_k_negative(void)
2046 : : {
2047 : 1 : return verify_instruction((struct verify_instruction_param){
2048 : : .tested_instruction = {
2049 : : .code = (EBPF_ALU64 | BPF_OR | BPF_K),
2050 : : .imm = -2,
2051 : : },
2052 : : .pre.dst = make_signed_domain(5, 6),
2053 : : .post.dst = make_signed_domain(-2, -1),
2054 : : });
2055 : : }
2056 : :
2057 : : /* 64-bit bitwise OR between a positive scalar range and positive immediate. */
2058 : : static int
2059 : 1 : test_alu64_or_k_positive(void)
2060 : : {
2061 : 1 : return verify_instruction((struct verify_instruction_param){
2062 : : .tested_instruction = {
2063 : : .code = (EBPF_ALU64 | BPF_OR | BPF_K),
2064 : : .imm = 2,
2065 : : },
2066 : : .pre.dst = make_signed_domain(5, 6),
2067 : : .post.dst = make_signed_domain(5, 7),
2068 : : });
2069 : : }
2070 : :
2071 : : /* 64-bit difference between two negative ranges.. */
2072 : : static int
2073 : 1 : test_alu64_sub_x_src_signed_max_zero(void)
2074 : : {
2075 : 1 : return verify_instruction((struct verify_instruction_param){
2076 : : .tested_instruction = {
2077 : : .code = (EBPF_ALU64 | BPF_SUB | BPF_X),
2078 : : },
2079 : : .pre.dst = make_signed_domain(INT64_MIN, 0),
2080 : : .pre.src = make_signed_domain(INT64_MIN, 0),
2081 : : .post.dst = unknown,
2082 : : });
2083 : : }
2084 : :
2085 : : /* 64-bit bitwise XOR between a negative scalar range and zero immediate. */
2086 : : static int
2087 : 1 : test_alu64_xor_k_negative(void)
2088 : : {
2089 : 1 : return verify_instruction((struct verify_instruction_param){
2090 : : .tested_instruction = {
2091 : : .code = (EBPF_ALU64 | BPF_XOR | BPF_K),
2092 : : .imm = 0,
2093 : : },
2094 : : .pre.dst = make_signed_domain(INT64_MIN, 0),
2095 : : .post.dst = unknown,
2096 : : });
2097 : : }
2098 : :
2099 : : /* Jump if greater than immediate. */
2100 : : static int
2101 : 1 : test_jmp64_jeq_k(void)
2102 : : {
2103 : 1 : return verify_instruction((struct verify_instruction_param){
2104 : : .tested_instruction = {
2105 : : .code = (BPF_JMP | BPF_JGT | BPF_K),
2106 : : .imm = 0,
2107 : : },
2108 : : .pre.dst = make_unsigned_domain(0, 1),
2109 : : .post.dst = make_singleton_domain(0),
2110 : : .jump.dst = make_singleton_domain(1),
2111 : : });
2112 : : }
2113 : :
2114 : : /* Jump if signed less than another register. */
2115 : : static int
2116 : 1 : test_jmp64_jslt_x(void)
2117 : : {
2118 : 1 : return verify_instruction((struct verify_instruction_param){
2119 : : .tested_instruction = {
2120 : : .code = (BPF_JMP | EBPF_JSLT | BPF_X),
2121 : : },
2122 : : .pre.dst = make_signed_domain(-3, 3),
2123 : : .pre.src = make_signed_domain(0, 0),
2124 : : .post.dst = make_signed_domain(0, 3),
2125 : : .jump.dst = make_signed_domain(-3, -1),
2126 : : });
2127 : : }
2128 : :
2129 : : /* Jump on ordering comparisons with potential bound overflow. */
2130 : : static int
2131 : 1 : test_jmp64_ordering_overflow(void)
2132 : : {
2133 : : /* In this test signed and unsigned cases are spelled out explicitly. */
2134 : : const bool also_signed = false;
2135 : :
2136 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2137 : : .tested_instruction = {
2138 : : .code = (BPF_JMP | EBPF_JSLT | BPF_X),
2139 : : },
2140 : : .pre.dst = make_singleton_domain(42),
2141 : : .pre.src = make_singleton_domain(INT64_MIN),
2142 : : .jump = unreachable,
2143 : : }, also_signed), "signed less than INT64_MIN");
2144 : :
2145 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2146 : : .tested_instruction = {
2147 : : .code = (BPF_JMP | EBPF_JSGT | BPF_X),
2148 : : },
2149 : : .pre.dst = make_singleton_domain(42),
2150 : : .pre.src = make_singleton_domain(INT64_MAX),
2151 : : .jump = unreachable,
2152 : : }, also_signed), "signed greater than INT64_MAX");
2153 : :
2154 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2155 : : .tested_instruction = {
2156 : : .code = (BPF_JMP | EBPF_JLT | BPF_X),
2157 : : },
2158 : : .pre.dst = make_singleton_domain(42),
2159 : : .pre.src = make_singleton_domain(0),
2160 : : .jump = unreachable,
2161 : : }, also_signed), "unsigned less than zero");
2162 : :
2163 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2164 : : .tested_instruction = {
2165 : : .code = (BPF_JMP | BPF_JGT | BPF_X),
2166 : : },
2167 : : .pre.dst = make_singleton_domain(42),
2168 : : .pre.src = make_singleton_domain(UINT64_MAX),
2169 : : .jump = unreachable,
2170 : : }, also_signed), "unsigned greater than UINT64_MAX");
2171 : :
2172 : 1 : return TEST_SUCCESS;
2173 : : }
2174 : :
2175 : : /* Jump on ordering comparisons between two ranges. */
2176 : : static int
2177 : 1 : test_jmp64_ordering_ranges(void)
2178 : : {
2179 : : /* All ranges used are valid for both signed and unsigned comparisons. */
2180 : : const bool also_signed = true;
2181 : :
2182 : : /*
2183 : : * 20 ---- dst ---- 60
2184 : : * 0 - src - 10
2185 : : */
2186 : :
2187 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2188 : : .tested_instruction = {
2189 : : .code = (BPF_JMP | EBPF_JLT | BPF_X),
2190 : : },
2191 : : .pre.dst = make_signed_domain(20, 60),
2192 : : .pre.src = make_signed_domain(0, 10),
2193 : : .jump = unreachable,
2194 : : }, also_signed), "strict, dst range strongly greater than src range");
2195 : :
2196 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2197 : : .tested_instruction = {
2198 : : .code = (BPF_JMP | EBPF_JLE | BPF_X),
2199 : : },
2200 : : .pre.dst = make_signed_domain(20, 60),
2201 : : .pre.src = make_signed_domain(0, 10),
2202 : : .jump = unreachable,
2203 : : }, also_signed), "non-strict, dst range strongly greater than src range");
2204 : :
2205 : : /*
2206 : : * 20 ---- dst ---- 60
2207 : : * 10 -- src -- 40
2208 : : */
2209 : :
2210 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2211 : : .tested_instruction = {
2212 : : .code = (BPF_JMP | EBPF_JLT | BPF_X),
2213 : : },
2214 : : .pre.dst = make_signed_domain(20, 60),
2215 : : .pre.src = make_signed_domain(10, 40),
2216 : : .jump.dst = make_signed_domain(20, 39),
2217 : : .jump.src = make_signed_domain(21, 40),
2218 : : }, also_signed), "strict, dst range weakly greater than src range");
2219 : :
2220 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2221 : : .tested_instruction = {
2222 : : .code = (BPF_JMP | EBPF_JLE | BPF_X),
2223 : : },
2224 : : .pre.dst = make_signed_domain(20, 60),
2225 : : .pre.src = make_signed_domain(10, 40),
2226 : : .jump.dst = make_signed_domain(20, 40),
2227 : : .jump.src = make_signed_domain(20, 40),
2228 : : }, also_signed), "non-strict, dst range weakly greater than src range");
2229 : :
2230 : : /*
2231 : : * 20 ---- dst ---- 60
2232 : : * 10 -------- src -------- 70
2233 : : */
2234 : :
2235 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2236 : : .tested_instruction = {
2237 : : .code = (BPF_JMP | EBPF_JLT | BPF_X),
2238 : : },
2239 : : .pre.dst = make_signed_domain(20, 60),
2240 : : .pre.src = make_signed_domain(10, 70),
2241 : : .post.src = make_signed_domain(10, 60),
2242 : : .jump.src = make_signed_domain(21, 70),
2243 : : }, also_signed), "strict, dst range included in src range");
2244 : :
2245 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2246 : : .tested_instruction = {
2247 : : .code = (BPF_JMP | EBPF_JLE | BPF_X),
2248 : : },
2249 : : .pre.dst = make_signed_domain(20, 60),
2250 : : .pre.src = make_signed_domain(10, 70),
2251 : : .post.src = make_signed_domain(10, 59),
2252 : : .jump.src = make_signed_domain(20, 70),
2253 : : }, also_signed), "non-strict, dst range included in src range");
2254 : :
2255 : : /*
2256 : : * 20 ---- dst ---- 60
2257 : : * 30 - src - 50
2258 : : */
2259 : :
2260 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2261 : : .tested_instruction = {
2262 : : .code = (BPF_JMP | EBPF_JLT | BPF_X),
2263 : : },
2264 : : .pre.dst = make_signed_domain(20, 60),
2265 : : .pre.src = make_signed_domain(30, 50),
2266 : : .post.dst = make_signed_domain(30, 60),
2267 : : .jump.dst = make_signed_domain(20, 49),
2268 : : }, also_signed), "strict, dst range includes src range");
2269 : :
2270 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2271 : : .tested_instruction = {
2272 : : .code = (BPF_JMP | EBPF_JLE | BPF_X),
2273 : : },
2274 : : .pre.dst = make_signed_domain(20, 60),
2275 : : .pre.src = make_signed_domain(30, 50),
2276 : : .post.dst = make_signed_domain(31, 60),
2277 : : .jump.dst = make_signed_domain(20, 50),
2278 : : }, also_signed), "non-strict, dst range includes src range");
2279 : :
2280 : : /*
2281 : : * 20 ---- dst ---- 60
2282 : : * 40 -- src -- 70
2283 : : */
2284 : :
2285 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2286 : : .tested_instruction = {
2287 : : .code = (BPF_JMP | EBPF_JLT | BPF_X),
2288 : : },
2289 : : .pre.dst = make_signed_domain(20, 60),
2290 : : .pre.src = make_signed_domain(40, 70),
2291 : : .post.dst = make_signed_domain(40, 60),
2292 : : .post.src = make_signed_domain(40, 60),
2293 : : }, also_signed), "strict, dst range weakly less than src range");
2294 : :
2295 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2296 : : .tested_instruction = {
2297 : : .code = (BPF_JMP | EBPF_JLE | BPF_X),
2298 : : },
2299 : : .pre.dst = make_signed_domain(20, 60),
2300 : : .pre.src = make_signed_domain(40, 70),
2301 : : .post.dst = make_signed_domain(41, 60),
2302 : : .post.src = make_signed_domain(40, 59),
2303 : : }, also_signed), "non-strict, dst range weakly less than src range");
2304 : :
2305 : : /*
2306 : : * 20 ---- dst ---- 60
2307 : : * 70 - src - 80
2308 : : */
2309 : :
2310 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2311 : : .tested_instruction = {
2312 : : .code = (BPF_JMP | EBPF_JLT | BPF_X),
2313 : : },
2314 : : .pre.dst = make_signed_domain(20, 60),
2315 : : .pre.src = make_signed_domain(70, 80),
2316 : : .post = unreachable,
2317 : : }, also_signed), "strict, dst range strongly less than src range");
2318 : :
2319 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2320 : : .tested_instruction = {
2321 : : .code = (BPF_JMP | EBPF_JLE | BPF_X),
2322 : : },
2323 : : .pre.dst = make_signed_domain(20, 60),
2324 : : .pre.src = make_signed_domain(70, 80),
2325 : : .post = unreachable,
2326 : : }, also_signed), "non-strict, dst range strongly less than src range");
2327 : :
2328 : 1 : return TEST_SUCCESS;
2329 : : }
2330 : :
2331 : : /* Jump on ordering comparisons with singleton inside the range. */
2332 : : static int
2333 : 1 : test_jmp64_ordering_singleton_inside(void)
2334 : : {
2335 : : /* All ranges used are valid for both signed and unsigned comparisons. */
2336 : : const bool also_signed = true;
2337 : :
2338 : : /*
2339 : : * 20 ---- dst ---- 60
2340 : : * imm
2341 : : */
2342 : :
2343 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2344 : : .tested_instruction = {
2345 : : .code = (BPF_JMP | EBPF_JLT | BPF_K),
2346 : : .imm = 40,
2347 : : },
2348 : : .pre.dst = make_signed_domain(20, 60),
2349 : : .post.dst = make_signed_domain(40, 60),
2350 : : .jump.dst = make_signed_domain(20, 39),
2351 : : }, also_signed), "(BPF_JMP | EBPF_JLT | BPF_K) check");
2352 : :
2353 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2354 : : .tested_instruction = {
2355 : : .code = (BPF_JMP | BPF_JGT | BPF_K),
2356 : : .imm = 40,
2357 : : },
2358 : : .pre.dst = make_signed_domain(20, 60),
2359 : : .post.dst = make_signed_domain(20, 40),
2360 : : .jump.dst = make_signed_domain(41, 60),
2361 : : }, also_signed), "(BPF_JMP | EBPF_JGT | BPF_K) check");
2362 : :
2363 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2364 : : .tested_instruction = {
2365 : : .code = (BPF_JMP | EBPF_JLE | BPF_K),
2366 : : .imm = 40,
2367 : : },
2368 : : .pre.dst = make_signed_domain(20, 60),
2369 : : .post.dst = make_signed_domain(41, 60),
2370 : : .jump.dst = make_signed_domain(20, 40),
2371 : : }, also_signed), "(BPF_JMP | EBPF_JLE | BPF_K) check");
2372 : :
2373 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2374 : : .tested_instruction = {
2375 : : .code = (BPF_JMP | BPF_JGE | BPF_K),
2376 : : .imm = 40,
2377 : : },
2378 : : .pre.dst = make_signed_domain(20, 60),
2379 : : .post.dst = make_signed_domain(20, 39),
2380 : : .jump.dst = make_signed_domain(40, 60),
2381 : : }, also_signed), "(BPF_JMP | EBPF_JGE | BPF_K) check");
2382 : :
2383 : 1 : return TEST_SUCCESS;
2384 : : }
2385 : :
2386 : : /* Jump on ordering comparisons with singleton outside the range. */
2387 : : static int
2388 : 1 : test_jmp64_ordering_singleton_outside(void)
2389 : : {
2390 : : /* All ranges used are valid for both signed and unsigned comparisons. */
2391 : : const bool also_signed = true;
2392 : :
2393 : : /*
2394 : : * 20 ---- dst ---- 60
2395 : : * imm
2396 : : */
2397 : :
2398 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2399 : : .tested_instruction = {
2400 : : .code = (BPF_JMP | EBPF_JLT | BPF_K),
2401 : : .imm = 10,
2402 : : },
2403 : : .pre.dst = make_signed_domain(20, 60),
2404 : : .jump = unreachable,
2405 : : }, also_signed), "(BPF_JMP | EBPF_JLT | BPF_K) check, range greater than imm");
2406 : :
2407 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2408 : : .tested_instruction = {
2409 : : .code = (BPF_JMP | EBPF_JLE | BPF_K),
2410 : : .imm = 10,
2411 : : },
2412 : : .pre.dst = make_signed_domain(20, 60),
2413 : : .jump = unreachable,
2414 : : }, also_signed), "(BPF_JMP | EBPF_JLE | BPF_K) check, range greater than imm");
2415 : :
2416 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2417 : : .tested_instruction = {
2418 : : .code = (BPF_JMP | BPF_JGT | BPF_K),
2419 : : .imm = 10,
2420 : : },
2421 : : .pre.dst = make_signed_domain(20, 60),
2422 : : .post = unreachable,
2423 : : }, also_signed), "(BPF_JMP | EBPF_JGT | BPF_K) check, range greater than imm");
2424 : :
2425 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2426 : : .tested_instruction = {
2427 : : .code = (BPF_JMP | BPF_JGE | BPF_K),
2428 : : .imm = 10,
2429 : : },
2430 : : .pre.dst = make_signed_domain(20, 60),
2431 : : .post = unreachable,
2432 : : }, also_signed), "(BPF_JMP | EBPF_JGE | BPF_K) check, range greater than imm");
2433 : :
2434 : : /*
2435 : : * 20 ---- dst ---- 60
2436 : : * imm
2437 : : */
2438 : :
2439 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2440 : : .tested_instruction = {
2441 : : .code = (BPF_JMP | EBPF_JLT | BPF_K),
2442 : : .imm = 70,
2443 : : },
2444 : : .pre.dst = make_signed_domain(20, 60),
2445 : : .post = unreachable,
2446 : : }, also_signed), "(BPF_JMP | EBPF_JLT | BPF_K) check, range less than imm");
2447 : :
2448 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2449 : : .tested_instruction = {
2450 : : .code = (BPF_JMP | EBPF_JLE | BPF_K),
2451 : : .imm = 70,
2452 : : },
2453 : : .pre.dst = make_signed_domain(20, 60),
2454 : : .post = unreachable,
2455 : : }, also_signed), "(BPF_JMP | EBPF_JLE | BPF_K) check, range less than imm");
2456 : :
2457 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2458 : : .tested_instruction = {
2459 : : .code = (BPF_JMP | BPF_JGT | BPF_K),
2460 : : .imm = 70,
2461 : : },
2462 : : .pre.dst = make_signed_domain(20, 60),
2463 : : .jump = unreachable,
2464 : : }, also_signed), "(BPF_JMP | EBPF_JGT | BPF_K) check, range less than imm");
2465 : :
2466 [ - + ]: 1 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2467 : : .tested_instruction = {
2468 : : .code = (BPF_JMP | BPF_JGE | BPF_K),
2469 : : .imm = 70,
2470 : : },
2471 : : .pre.dst = make_signed_domain(20, 60),
2472 : : .jump = unreachable,
2473 : : }, also_signed), "(BPF_JMP | EBPF_JGE | BPF_K) check, range less than imm");
2474 : :
2475 : 1 : return TEST_SUCCESS;
2476 : : }
2477 : :
2478 : : /* Jump on ordering comparisons with ranges "touching" each other. */
2479 : : static int
2480 : 1 : test_jmp64_ordering_touching(void)
2481 : : {
2482 : : /* All ranges used are valid for both signed and unsigned comparisons. */
2483 : : const bool also_signed = true;
2484 : :
2485 [ + + ]: 4 : for (int overlap = 0; overlap != 3; ++overlap) {
2486 : :
2487 : : /*
2488 : : * 20 - dst - 30
2489 : : * 10 - src - (19 + overlap)
2490 : : */
2491 : :
2492 [ + + - + ]: 3 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2493 : : .tested_instruction = {
2494 : : .code = (BPF_JMP | EBPF_JLT | BPF_X),
2495 : : },
2496 : : .pre.dst = make_signed_domain(20, 30),
2497 : : .pre.src = make_signed_domain(10, 19 + overlap),
2498 : : .jump = overlap <= 1 ? unreachable : (struct state){
2499 : : .dst = make_singleton_domain(20),
2500 : : .src = make_singleton_domain(21),
2501 : : },
2502 : : }, also_signed), "strict, dst left touching src right, overlap=%d", overlap);
2503 : :
2504 [ + + - + ]: 3 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2505 : : .tested_instruction = {
2506 : : .code = (BPF_JMP | EBPF_JLE | BPF_X),
2507 : : },
2508 : : .pre.dst = make_signed_domain(20, 30),
2509 : : .pre.src = make_signed_domain(10, 19 + overlap),
2510 : : .jump = overlap < 1 ? unreachable : (struct state){
2511 : : .dst = make_signed_domain(20, 19 + overlap),
2512 : : .src = make_signed_domain(20, 19 + overlap),
2513 : : },
2514 : : }, also_signed), "non-strict, dst left touching src right, overlap=%d", overlap);
2515 : :
2516 : : /*
2517 : : * 10 - dst - (19 + overlap)
2518 : : * 20 - src - 30
2519 : : */
2520 : :
2521 [ + + - + ]: 3 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2522 : : .tested_instruction = {
2523 : : .code = (BPF_JMP | EBPF_JLT | BPF_X),
2524 : : },
2525 : : .pre.dst = make_signed_domain(10, 19 + overlap),
2526 : : .pre.src = make_signed_domain(20, 30),
2527 : : .post = overlap < 1 ? unreachable : (struct state){
2528 : : .dst = make_signed_domain(20, 19 + overlap),
2529 : : .src = make_signed_domain(20, 19 + overlap),
2530 : : },
2531 : : }, also_signed), "strict, dst right touching src left, overlap=%d", overlap);
2532 : :
2533 [ + + - + ]: 3 : TEST_ASSERT_SUCCESS(verify_comparison((struct verify_instruction_param){
2534 : : .tested_instruction = {
2535 : : .code = (BPF_JMP | EBPF_JLE | BPF_X),
2536 : : },
2537 : : .pre.dst = make_signed_domain(10, 19 + overlap),
2538 : : .pre.src = make_signed_domain(20, 30),
2539 : : .post = overlap <= 1 ? unreachable : (struct state){
2540 : : .dst = make_singleton_domain(21),
2541 : : .src = make_singleton_domain(20),
2542 : : },
2543 : : }, also_signed), "non-strict, dst right touching src left, overlap=%d", overlap);
2544 : : }
2545 : :
2546 : : return TEST_SUCCESS;
2547 : : }
2548 : :
2549 : : /* 64-bit load from heap (should be set to unknown). */
2550 : : static int
2551 : 1 : test_mem_ldx_dw_heap(void)
2552 : : {
2553 : 1 : return verify_instruction((struct verify_instruction_param){
2554 : : .tested_instruction = {
2555 : : .code = (BPF_MEM | BPF_LDX | EBPF_DW),
2556 : : .off = 16,
2557 : : },
2558 : : .area_size = 24,
2559 : : .pre.src = make_pointer_domain(0, 0),
2560 : : .post.dst = unknown,
2561 : : });
2562 : : }
2563 : :
2564 : : static struct
2565 : : unit_test_suite test_bpf_validate_suite = {
2566 : : .suite_name = "Test BPF Validate Unit Test Suite",
2567 : : .unit_test_cases = {
2568 : : TEST_CASE(test_alu32_and_k_ub),
2569 : : TEST_CASE(test_alu32_or_k_ub),
2570 : : TEST_CASE(test_alu32_xor_k_ub),
2571 : : TEST_CASE(test_alu64_add_k),
2572 : : TEST_CASE(test_alu64_add_k_pointer),
2573 : : TEST_CASE(test_alu64_add_x_pointer_pointer),
2574 : : TEST_CASE(test_alu64_add_x_pointer_scalar),
2575 : : TEST_CASE(test_alu64_add_x_scalar_pointer),
2576 : : TEST_CASE(test_alu64_add_x_scalar_scalar),
2577 : : TEST_CASE(test_alu64_and_k),
2578 : : TEST_CASE(test_alu64_div_mod_big_constant),
2579 : : TEST_CASE(test_alu64_div_mod_big_range),
2580 : : TEST_CASE(test_alu64_div_mod_overflow),
2581 : : TEST_CASE(test_alu64_lsh_63),
2582 : : TEST_CASE(test_alu64_mul_k_overflow),
2583 : : TEST_CASE(test_alu64_mul_k_range_small),
2584 : : TEST_CASE(test_alu64_neg_int64min_first),
2585 : : TEST_CASE(test_alu64_neg_int64min_last),
2586 : : TEST_CASE(test_alu64_neg_zero_first),
2587 : : TEST_CASE(test_alu64_neg_zero_last),
2588 : : TEST_CASE(test_alu64_or_k_negative),
2589 : : TEST_CASE(test_alu64_or_k_positive),
2590 : : TEST_CASE(test_alu64_sub_x_src_signed_max_zero),
2591 : : TEST_CASE(test_alu64_xor_k_negative),
2592 : : TEST_CASE(test_jmp64_jeq_k),
2593 : : TEST_CASE(test_jmp64_jslt_x),
2594 : : TEST_CASE(test_jmp64_ordering_overflow),
2595 : : TEST_CASE(test_jmp64_ordering_ranges),
2596 : : TEST_CASE(test_jmp64_ordering_singleton_inside),
2597 : : TEST_CASE(test_jmp64_ordering_singleton_outside),
2598 : : TEST_CASE(test_jmp64_ordering_touching),
2599 : : TEST_CASE(test_mem_ldx_dw_heap),
2600 : : TEST_CASES_END()
2601 : : }
2602 : : };
2603 : :
2604 : : static int
2605 : 1 : test_bpf_validate(void)
2606 : : {
2607 : 1 : return unit_test_suite_runner(&test_bpf_validate_suite);
2608 : : }
2609 : :
2610 : 305 : REGISTER_FAST_TEST(bpf_validate_autotest, NOHUGE_OK, ASAN_OK, test_bpf_validate);
|