Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Add verifier tests for code pattern '<const> <cond_op> <non_const>'

Add various tests for code pattern '<const> <cond_op> <non_const>' to
exercise the previous verifier patch.

The following are veristat changed number of processed insns stat
comparing the previous patch vs. this patch:

File Program Insns (A) Insns (B) Insns (DIFF)
----------------------------------------------------- ---------------------------------------------------- --------- --------- -------------
test_seg6_loop.bpf.linked3.o __add_egr_x 12423 12314 -109 (-0.88%)

Only one program is affected with minor change.

Signed-off-by: Yonghong Song <yhs@fb.com>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20230406164510.1047757-1-yhs@fb.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Yonghong Song and committed by
Alexei Starovoitov
23a88fae 953d9f5b

+460
+460
tools/testing/selftests/bpf/progs/verifier_bounds_deduction_non_const.c
··· 176 176 : __clobber_all); 177 177 } 178 178 179 + SEC("socket") 180 + __description("check deducing bounds from non-const, jmp64, <const> > <non_const>, 1") 181 + __success __retval(0) 182 + __naked void deducing_bounds_from_non_const_9(void) 183 + { 184 + asm volatile (" \ 185 + call %[bpf_ktime_get_ns]; \ 186 + r2 = 0; \ 187 + if r2 > r0 goto l0_%=; \ 188 + r0 = 0; \ 189 + exit; \ 190 + l0_%=: \ 191 + r0 -= r1; \ 192 + exit; \ 193 + " : 194 + : __imm(bpf_ktime_get_ns) 195 + : __clobber_all); 196 + } 197 + 198 + SEC("socket") 199 + __description("check deducing bounds from non-const, jmp64, <const> > <non_const>, 2") 200 + __success __retval(0) 201 + __naked void deducing_bounds_from_non_const_10(void) 202 + { 203 + asm volatile (" \ 204 + call %[bpf_ktime_get_ns]; \ 205 + if r0 < 4 goto l0_%=; \ 206 + r2 = 4; \ 207 + if r2 > r0 goto l1_%=; \ 208 + l0_%=: \ 209 + r0 = 0; \ 210 + exit; \ 211 + l1_%=: \ 212 + r0 -= r1; \ 213 + exit; \ 214 + " : 215 + : __imm(bpf_ktime_get_ns) 216 + : __clobber_all); 217 + } 218 + 219 + SEC("socket") 220 + __description("check deducing bounds from non-const, jmp64, <const> >= <non_const>") 221 + __success __retval(0) 222 + __naked void deducing_bounds_from_non_const_11(void) 223 + { 224 + asm volatile (" \ 225 + call %[bpf_ktime_get_ns]; \ 226 + if r0 < 4 goto l0_%=; \ 227 + r2 = 3; \ 228 + if r2 >= r0 goto l1_%=; \ 229 + l0_%=: \ 230 + r0 = 0; \ 231 + exit; \ 232 + l1_%=: \ 233 + r0 -= r1; \ 234 + exit; \ 235 + " : 236 + : __imm(bpf_ktime_get_ns) 237 + : __clobber_all); 238 + } 239 + 240 + SEC("socket") 241 + __description("check deducing bounds from non-const, jmp64, <const> < <non_const>") 242 + __success __retval(0) 243 + __naked void deducing_bounds_from_non_const_12(void) 244 + { 245 + asm volatile (" \ 246 + call %[bpf_ktime_get_ns]; \ 247 + if r0 > 4 goto l0_%=; \ 248 + r2 = 4; \ 249 + if r2 < r0 goto l1_%=; \ 250 + l0_%=: \ 251 + r0 = 0; \ 252 + exit; \ 253 + l1_%=: \ 254 + r0 -= r1; \ 255 + exit; \ 256 + " : 257 + : __imm(bpf_ktime_get_ns) 258 + : __clobber_all); 259 + } 260 + 261 + SEC("socket") 262 + __description("check deducing bounds from non-const, jmp64, <const> <= <non_const>") 263 + __success __retval(0) 264 + __naked void deducing_bounds_from_non_const_13(void) 265 + { 266 + asm volatile (" \ 267 + call %[bpf_ktime_get_ns]; \ 268 + if r0 >= 4 goto l0_%=; \ 269 + r2 = 4; \ 270 + if r2 <= r0 goto l1_%=; \ 271 + l0_%=: \ 272 + r0 = 0; \ 273 + exit; \ 274 + l1_%=: \ 275 + r0 -= r1; \ 276 + exit; \ 277 + " : 278 + : __imm(bpf_ktime_get_ns) 279 + : __clobber_all); 280 + } 281 + 282 + SEC("socket") 283 + __description("check deducing bounds from non-const, jmp64, <const> == <non_const>") 284 + __success __retval(0) 285 + __naked void deducing_bounds_from_non_const_14(void) 286 + { 287 + asm volatile (" \ 288 + call %[bpf_ktime_get_ns]; \ 289 + if r0 < 3 goto l0_%=; \ 290 + r2 = 2; \ 291 + if r2 == r0 goto l1_%=; \ 292 + l0_%=: \ 293 + r0 = 0; \ 294 + exit; \ 295 + l1_%=: \ 296 + r0 -= r1; \ 297 + exit; \ 298 + " : 299 + : __imm(bpf_ktime_get_ns) 300 + : __clobber_all); 301 + } 302 + 303 + SEC("socket") 304 + __description("check deducing bounds from non-const, jmp64, <const> s> <non_const>") 305 + __success __retval(0) 306 + __naked void deducing_bounds_from_non_const_15(void) 307 + { 308 + asm volatile (" \ 309 + call %[bpf_ktime_get_ns]; \ 310 + if r0 s< 4 goto l0_%=; \ 311 + r2 = 4; \ 312 + if r2 s> r0 goto l1_%=; \ 313 + l0_%=: \ 314 + r0 = 0; \ 315 + exit; \ 316 + l1_%=: \ 317 + r0 -= r1; \ 318 + exit; \ 319 + " : 320 + : __imm(bpf_ktime_get_ns) 321 + : __clobber_all); 322 + } 323 + 324 + SEC("socket") 325 + __description("check deducing bounds from non-const, jmp64, <const> s>= <non_const>") 326 + __success __retval(0) 327 + __naked void deducing_bounds_from_non_const_16(void) 328 + { 329 + asm volatile (" \ 330 + call %[bpf_ktime_get_ns]; \ 331 + if r0 s< 4 goto l0_%=; \ 332 + r2 = 3; \ 333 + if r2 s>= r0 goto l1_%=; \ 334 + l0_%=: \ 335 + r0 = 0; \ 336 + exit; \ 337 + l1_%=: \ 338 + r0 -= r1; \ 339 + exit; \ 340 + " : 341 + : __imm(bpf_ktime_get_ns) 342 + : __clobber_all); 343 + } 344 + 345 + SEC("socket") 346 + __description("check deducing bounds from non-const, jmp64, <const> s< <non_const>") 347 + __success __retval(0) 348 + __naked void deducing_bounds_from_non_const_17(void) 349 + { 350 + asm volatile (" \ 351 + call %[bpf_ktime_get_ns]; \ 352 + if r0 s> 4 goto l0_%=; \ 353 + r2 = 4; \ 354 + if r2 s< r0 goto l1_%=; \ 355 + l0_%=: \ 356 + r0 = 0; \ 357 + exit; \ 358 + l1_%=: \ 359 + r0 -= r1; \ 360 + exit; \ 361 + " : 362 + : __imm(bpf_ktime_get_ns) 363 + : __clobber_all); 364 + } 365 + 366 + SEC("socket") 367 + __description("check deducing bounds from non-const, jmp64, <const> s<= <non_const>") 368 + __success __retval(0) 369 + __naked void deducing_bounds_from_non_const_18(void) 370 + { 371 + asm volatile (" \ 372 + call %[bpf_ktime_get_ns]; \ 373 + if r0 s> 4 goto l0_%=; \ 374 + r2 = 5; \ 375 + if r2 s<= r0 goto l1_%=; \ 376 + l0_%=: \ 377 + r0 = 0; \ 378 + exit; \ 379 + l1_%=: \ 380 + r0 -= r1; \ 381 + exit; \ 382 + " : 383 + : __imm(bpf_ktime_get_ns) 384 + : __clobber_all); 385 + } 386 + 387 + SEC("socket") 388 + __description("check deducing bounds from non-const, jmp64, <const> != <non_const>") 389 + __success __retval(0) 390 + __naked void deducing_bounds_from_non_const_19(void) 391 + { 392 + asm volatile (" \ 393 + call %[bpf_ktime_get_ns]; \ 394 + if r0 < 3 goto l0_%=; \ 395 + r2 = 2; \ 396 + if r2 != r0 goto l0_%=; \ 397 + goto l1_%=; \ 398 + l0_%=: \ 399 + r0 = 0; \ 400 + exit; \ 401 + l1_%=: \ 402 + r0 -= r1; \ 403 + exit; \ 404 + " : 405 + : __imm(bpf_ktime_get_ns) 406 + : __clobber_all); 407 + } 408 + 409 + SEC("socket") 410 + __description("check deducing bounds from non-const, jmp32, <const> > <non_const>, 1") 411 + __success __retval(0) 412 + __naked void deducing_bounds_from_non_const_20(void) 413 + { 414 + asm volatile (" \ 415 + call %[bpf_ktime_get_ns]; \ 416 + w2 = 0; \ 417 + if w2 > w0 goto l0_%=; \ 418 + r0 = 0; \ 419 + exit; \ 420 + l0_%=: \ 421 + r0 -= r1; \ 422 + exit; \ 423 + " : 424 + : __imm(bpf_ktime_get_ns) 425 + : __clobber_all); 426 + } 427 + 428 + SEC("socket") 429 + __description("check deducing bounds from non-const, jmp32, <const> > <non_const>, 2") 430 + __success __retval(0) 431 + __naked void deducing_bounds_from_non_const_21(void) 432 + { 433 + asm volatile (" \ 434 + call %[bpf_ktime_get_ns]; \ 435 + if w0 < 4 goto l0_%=; \ 436 + w2 = 4; \ 437 + if w2 > w0 goto l1_%=; \ 438 + l0_%=: \ 439 + r0 = 0; \ 440 + exit; \ 441 + l1_%=: \ 442 + r0 -= r1; \ 443 + exit; \ 444 + " : 445 + : __imm(bpf_ktime_get_ns) 446 + : __clobber_all); 447 + } 448 + 449 + SEC("socket") 450 + __description("check deducing bounds from non-const, jmp32, <const> >= <non_const>") 451 + __success __retval(0) 452 + __naked void deducing_bounds_from_non_const_22(void) 453 + { 454 + asm volatile (" \ 455 + call %[bpf_ktime_get_ns]; \ 456 + if w0 < 4 goto l0_%=; \ 457 + w2 = 3; \ 458 + if w2 >= w0 goto l1_%=; \ 459 + l0_%=: \ 460 + r0 = 0; \ 461 + exit; \ 462 + l1_%=: \ 463 + r0 -= r1; \ 464 + exit; \ 465 + " : 466 + : __imm(bpf_ktime_get_ns) 467 + : __clobber_all); 468 + } 469 + 470 + SEC("socket") 471 + __description("check deducing bounds from non-const, jmp32, <const> < <non_const>") 472 + __success __retval(0) 473 + __naked void deducing_bounds_from_non_const_23(void) 474 + { 475 + asm volatile (" \ 476 + call %[bpf_ktime_get_ns]; \ 477 + if w0 > 4 goto l0_%=; \ 478 + w2 = 4; \ 479 + if w2 < w0 goto l1_%=; \ 480 + l0_%=: \ 481 + r0 = 0; \ 482 + exit; \ 483 + l1_%=: \ 484 + r0 -= r1; \ 485 + exit; \ 486 + " : 487 + : __imm(bpf_ktime_get_ns) 488 + : __clobber_all); 489 + } 490 + 491 + SEC("socket") 492 + __description("check deducing bounds from non-const, jmp32, <const> <= <non_const>") 493 + __success __retval(0) 494 + __naked void deducing_bounds_from_non_const_24(void) 495 + { 496 + asm volatile (" \ 497 + call %[bpf_ktime_get_ns]; \ 498 + if w0 >= 4 goto l0_%=; \ 499 + w2 = 4; \ 500 + if w2 <= w0 goto l1_%=; \ 501 + l0_%=: \ 502 + r0 = 0; \ 503 + exit; \ 504 + l1_%=: \ 505 + r0 -= r1; \ 506 + exit; \ 507 + " : 508 + : __imm(bpf_ktime_get_ns) 509 + : __clobber_all); 510 + } 511 + 512 + SEC("socket") 513 + __description("check deducing bounds from non-const, jmp32, <const> == <non_const>") 514 + __success __retval(0) 515 + __naked void deducing_bounds_from_non_const_25(void) 516 + { 517 + asm volatile (" \ 518 + call %[bpf_ktime_get_ns]; \ 519 + if w0 < 4 goto l0_%=; \ 520 + w2 = 3; \ 521 + if w2 == w0 goto l1_%=; \ 522 + l0_%=: \ 523 + r0 = 0; \ 524 + exit; \ 525 + l1_%=: \ 526 + r0 -= r1; \ 527 + exit; \ 528 + " : 529 + : __imm(bpf_ktime_get_ns) 530 + : __clobber_all); 531 + } 532 + 533 + SEC("socket") 534 + __description("check deducing bounds from non-const, jmp32, <const> s> <non_const>") 535 + __success __retval(0) 536 + __naked void deducing_bounds_from_non_const_26(void) 537 + { 538 + asm volatile (" \ 539 + call %[bpf_ktime_get_ns]; \ 540 + if w0 s< 4 goto l0_%=; \ 541 + w2 = 4; \ 542 + if w2 s> w0 goto l1_%=; \ 543 + l0_%=: \ 544 + r0 = 0; \ 545 + exit; \ 546 + l1_%=: \ 547 + r0 -= r1; \ 548 + exit; \ 549 + " : 550 + : __imm(bpf_ktime_get_ns) 551 + : __clobber_all); 552 + } 553 + 554 + SEC("socket") 555 + __description("check deducing bounds from non-const, jmp32, <const> s>= <non_const>") 556 + __success __retval(0) 557 + __naked void deducing_bounds_from_non_const_27(void) 558 + { 559 + asm volatile (" \ 560 + call %[bpf_ktime_get_ns]; \ 561 + if w0 s< 4 goto l0_%=; \ 562 + w2 = 3; \ 563 + if w2 s>= w0 goto l1_%=; \ 564 + l0_%=: \ 565 + r0 = 0; \ 566 + exit; \ 567 + l1_%=: \ 568 + r0 -= r1; \ 569 + exit; \ 570 + " : 571 + : __imm(bpf_ktime_get_ns) 572 + : __clobber_all); 573 + } 574 + 575 + SEC("socket") 576 + __description("check deducing bounds from non-const, jmp32, <const> s< <non_const>") 577 + __success __retval(0) 578 + __naked void deducing_bounds_from_non_const_28(void) 579 + { 580 + asm volatile (" \ 581 + call %[bpf_ktime_get_ns]; \ 582 + if w0 s> 4 goto l0_%=; \ 583 + w2 = 5; \ 584 + if w2 s< w0 goto l1_%=; \ 585 + l0_%=: \ 586 + r0 = 0; \ 587 + exit; \ 588 + l1_%=: \ 589 + r0 -= r1; \ 590 + exit; \ 591 + " : 592 + : __imm(bpf_ktime_get_ns) 593 + : __clobber_all); 594 + } 595 + 596 + SEC("socket") 597 + __description("check deducing bounds from non-const, jmp32, <const> s<= <non_const>") 598 + __success __retval(0) 599 + __naked void deducing_bounds_from_non_const_29(void) 600 + { 601 + asm volatile (" \ 602 + call %[bpf_ktime_get_ns]; \ 603 + if w0 s>= 4 goto l0_%=; \ 604 + w2 = 4; \ 605 + if w2 s<= w0 goto l1_%=; \ 606 + l0_%=: \ 607 + r0 = 0; \ 608 + exit; \ 609 + l1_%=: \ 610 + r0 -= r1; \ 611 + exit; \ 612 + " : 613 + : __imm(bpf_ktime_get_ns) 614 + : __clobber_all); 615 + } 616 + 617 + SEC("socket") 618 + __description("check deducing bounds from non-const, jmp32, <const> != <non_const>") 619 + __success __retval(0) 620 + __naked void deducing_bounds_from_non_const_30(void) 621 + { 622 + asm volatile (" \ 623 + call %[bpf_ktime_get_ns]; \ 624 + if w0 < 3 goto l0_%=; \ 625 + w2 = 2; \ 626 + if w2 != w0 goto l0_%=; \ 627 + goto l1_%=; \ 628 + l0_%=: \ 629 + r0 = 0; \ 630 + exit; \ 631 + l1_%=: \ 632 + r0 -= r1; \ 633 + exit; \ 634 + " : 635 + : __imm(bpf_ktime_get_ns) 636 + : __clobber_all); 637 + } 638 + 179 639 char _license[] SEC("license") = "GPL";