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

selftests/bpf: add CO-RE relocs ints tests

Add various tests validating handling compatible/incompatible integer
types.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Andrii Nakryiko and committed by
Alexei Starovoitov
c1f5e7dd d698f9db

+209
+40
tools/testing/selftests/bpf/prog_tests/core_reloc.c
··· 145 145 .output_len = sizeof(struct core_reloc_ptr_as_arr), \ 146 146 } 147 147 148 + #define INTS_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) { \ 149 + .u8_field = 1, \ 150 + .s8_field = 2, \ 151 + .u16_field = 3, \ 152 + .s16_field = 4, \ 153 + .u32_field = 5, \ 154 + .s32_field = 6, \ 155 + .u64_field = 7, \ 156 + .s64_field = 8, \ 157 + } 158 + 159 + #define INTS_CASE_COMMON(name) \ 160 + .case_name = #name, \ 161 + .bpf_obj_file = "test_core_reloc_ints.o", \ 162 + .btf_src_file = "btf__core_reloc_" #name ".o" 163 + 164 + #define INTS_CASE(name) { \ 165 + INTS_CASE_COMMON(name), \ 166 + .input = INTS_DATA(core_reloc_##name), \ 167 + .input_len = sizeof(struct core_reloc_##name), \ 168 + .output = INTS_DATA(core_reloc_ints), \ 169 + .output_len = sizeof(struct core_reloc_ints), \ 170 + } 171 + 172 + #define INTS_ERR_CASE(name) { \ 173 + INTS_CASE_COMMON(name), \ 174 + .fails = true, \ 175 + } 176 + 148 177 struct core_reloc_test_case { 149 178 const char *case_name; 150 179 const char *bpf_obj_file; ··· 249 220 /* handling "ptr is an array" semantics */ 250 221 PTR_AS_ARR_CASE(ptr_as_arr), 251 222 PTR_AS_ARR_CASE(ptr_as_arr___diff_sz), 223 + 224 + /* int signedness/sizing/bitfield handling */ 225 + INTS_CASE(ints), 226 + INTS_CASE(ints___bool), 227 + INTS_CASE(ints___reverse_sign), 228 + 229 + INTS_ERR_CASE(ints___err_bitfield), 230 + INTS_ERR_CASE(ints___err_wrong_sz_8), 231 + INTS_ERR_CASE(ints___err_wrong_sz_16), 232 + INTS_ERR_CASE(ints___err_wrong_sz_32), 233 + INTS_ERR_CASE(ints___err_wrong_sz_64), 252 234 }; 253 235 254 236 struct data {
+3
tools/testing/selftests/bpf/progs/btf__core_reloc_ints.c
··· 1 + #include "core_reloc_types.h" 2 + 3 + void f(struct core_reloc_ints x) {}
+3
tools/testing/selftests/bpf/progs/btf__core_reloc_ints___bool.c
··· 1 + #include "core_reloc_types.h" 2 + 3 + void f(struct core_reloc_ints___bool x) {}
+3
tools/testing/selftests/bpf/progs/btf__core_reloc_ints___err_bitfield.c
··· 1 + #include "core_reloc_types.h" 2 + 3 + void f(struct core_reloc_ints___err_bitfield x) {}
+3
tools/testing/selftests/bpf/progs/btf__core_reloc_ints___err_wrong_sz_16.c
··· 1 + #include "core_reloc_types.h" 2 + 3 + void f(struct core_reloc_ints___err_wrong_sz_16 x) {}
+3
tools/testing/selftests/bpf/progs/btf__core_reloc_ints___err_wrong_sz_32.c
··· 1 + #include "core_reloc_types.h" 2 + 3 + void f(struct core_reloc_ints___err_wrong_sz_32 x) {}
+3
tools/testing/selftests/bpf/progs/btf__core_reloc_ints___err_wrong_sz_64.c
··· 1 + #include "core_reloc_types.h" 2 + 3 + void f(struct core_reloc_ints___err_wrong_sz_64 x) {}
+3
tools/testing/selftests/bpf/progs/btf__core_reloc_ints___err_wrong_sz_8.c
··· 1 + #include "core_reloc_types.h" 2 + 3 + void f(struct core_reloc_ints___err_wrong_sz_8 x) {}
+3
tools/testing/selftests/bpf/progs/btf__core_reloc_ints___reverse_sign.c
··· 1 + #include "core_reloc_types.h" 2 + 3 + void f(struct core_reloc_ints___reverse_sign x) {}
+101
tools/testing/selftests/bpf/progs/core_reloc_types.h
··· 1 + #include <stdint.h> 2 + #include <stdbool.h> 3 + 1 4 /* 2 5 * FLAVORS 3 6 */ ··· 541 538 int :32; /* padding */ 542 539 char __some_more_padding; 543 540 int a; 541 + }; 542 + 543 + /* 544 + * INTS 545 + */ 546 + struct core_reloc_ints { 547 + uint8_t u8_field; 548 + int8_t s8_field; 549 + uint16_t u16_field; 550 + int16_t s16_field; 551 + uint32_t u32_field; 552 + int32_t s32_field; 553 + uint64_t u64_field; 554 + int64_t s64_field; 555 + }; 556 + 557 + /* signed/unsigned types swap */ 558 + struct core_reloc_ints___reverse_sign { 559 + int8_t u8_field; 560 + uint8_t s8_field; 561 + int16_t u16_field; 562 + uint16_t s16_field; 563 + int32_t u32_field; 564 + uint32_t s32_field; 565 + int64_t u64_field; 566 + uint64_t s64_field; 567 + }; 568 + 569 + struct core_reloc_ints___bool { 570 + bool u8_field; /* bool instead of uint8 */ 571 + int8_t s8_field; 572 + uint16_t u16_field; 573 + int16_t s16_field; 574 + uint32_t u32_field; 575 + int32_t s32_field; 576 + uint64_t u64_field; 577 + int64_t s64_field; 578 + }; 579 + 580 + struct core_reloc_ints___err_bitfield { 581 + uint8_t u8_field; 582 + int8_t s8_field; 583 + uint16_t u16_field; 584 + int16_t s16_field; 585 + uint32_t u32_field: 32; /* bitfields are not supported */ 586 + int32_t s32_field; 587 + uint64_t u64_field; 588 + int64_t s64_field; 589 + }; 590 + 591 + struct core_reloc_ints___err_wrong_sz_8 { 592 + uint16_t u8_field; /* not 8-bit anymore */ 593 + int16_t s8_field; /* not 8-bit anymore */ 594 + 595 + uint16_t u16_field; 596 + int16_t s16_field; 597 + uint32_t u32_field; 598 + int32_t s32_field; 599 + uint64_t u64_field; 600 + int64_t s64_field; 601 + }; 602 + 603 + struct core_reloc_ints___err_wrong_sz_16 { 604 + uint8_t u8_field; 605 + int8_t s8_field; 606 + 607 + uint32_t u16_field; /* not 16-bit anymore */ 608 + int32_t s16_field; /* not 16-bit anymore */ 609 + 610 + uint32_t u32_field; 611 + int32_t s32_field; 612 + uint64_t u64_field; 613 + int64_t s64_field; 614 + }; 615 + 616 + struct core_reloc_ints___err_wrong_sz_32 { 617 + uint8_t u8_field; 618 + int8_t s8_field; 619 + uint16_t u16_field; 620 + int16_t s16_field; 621 + 622 + uint64_t u32_field; /* not 32-bit anymore */ 623 + int64_t s32_field; /* not 32-bit anymore */ 624 + 625 + uint64_t u64_field; 626 + int64_t s64_field; 627 + }; 628 + 629 + struct core_reloc_ints___err_wrong_sz_64 { 630 + uint8_t u8_field; 631 + int8_t s8_field; 632 + uint16_t u16_field; 633 + int16_t s16_field; 634 + uint32_t u32_field; 635 + int32_t s32_field; 636 + 637 + uint32_t u64_field; /* not 64-bit anymore */ 638 + int32_t s64_field; /* not 64-bit anymore */ 544 639 };
+44
tools/testing/selftests/bpf/progs/test_core_reloc_ints.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + // Copyright (c) 2019 Facebook 3 + 4 + #include <linux/bpf.h> 5 + #include <stdint.h> 6 + #include "bpf_helpers.h" 7 + 8 + char _license[] SEC("license") = "GPL"; 9 + 10 + static volatile struct data { 11 + char in[256]; 12 + char out[256]; 13 + } data; 14 + 15 + struct core_reloc_ints { 16 + uint8_t u8_field; 17 + int8_t s8_field; 18 + uint16_t u16_field; 19 + int16_t s16_field; 20 + uint32_t u32_field; 21 + int32_t s32_field; 22 + uint64_t u64_field; 23 + int64_t s64_field; 24 + }; 25 + 26 + SEC("raw_tracepoint/sys_enter") 27 + int test_core_ints(void *ctx) 28 + { 29 + struct core_reloc_ints *in = (void *)&data.in; 30 + struct core_reloc_ints *out = (void *)&data.out; 31 + 32 + if (BPF_CORE_READ(&out->u8_field, &in->u8_field) || 33 + BPF_CORE_READ(&out->s8_field, &in->s8_field) || 34 + BPF_CORE_READ(&out->u16_field, &in->u16_field) || 35 + BPF_CORE_READ(&out->s16_field, &in->s16_field) || 36 + BPF_CORE_READ(&out->u32_field, &in->u32_field) || 37 + BPF_CORE_READ(&out->s32_field, &in->s32_field) || 38 + BPF_CORE_READ(&out->u64_field, &in->u64_field) || 39 + BPF_CORE_READ(&out->s64_field, &in->s64_field)) 40 + return 1; 41 + 42 + return 0; 43 + } 44 +