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

selftests/bpf: Add bpf_core_field_offset() tests

Add test cases for bpf_core_field_offset() helper.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20220509004148.1801791-7-andrii@kernel.org

authored by

Andrii Nakryiko and committed by
Daniel Borkmann
785c3342 7715f549

+56 -3
+11 -2
tools/testing/selftests/bpf/prog_tests/core_reloc.c
··· 277 277 #define SIZE_OUTPUT_DATA(type) \ 278 278 STRUCT_TO_CHAR_PTR(core_reloc_size_output) { \ 279 279 .int_sz = sizeof(((type *)0)->int_field), \ 280 + .int_off = offsetof(type, int_field), \ 280 281 .struct_sz = sizeof(((type *)0)->struct_field), \ 282 + .struct_off = offsetof(type, struct_field), \ 281 283 .union_sz = sizeof(((type *)0)->union_field), \ 284 + .union_off = offsetof(type, union_field), \ 282 285 .arr_sz = sizeof(((type *)0)->arr_field), \ 283 - .arr_elem_sz = sizeof(((type *)0)->arr_field[0]), \ 286 + .arr_off = offsetof(type, arr_field), \ 287 + .arr_elem_sz = sizeof(((type *)0)->arr_field[1]), \ 288 + .arr_elem_off = offsetof(type, arr_field[1]), \ 284 289 .ptr_sz = 8, /* always 8-byte pointer for BPF */ \ 290 + .ptr_off = offsetof(type, ptr_field), \ 285 291 .enum_sz = sizeof(((type *)0)->enum_field), \ 292 + .enum_off = offsetof(type, enum_field), \ 286 293 .float_sz = sizeof(((type *)0)->float_field), \ 294 + .float_off = offsetof(type, float_field), \ 287 295 } 288 296 289 297 #define SIZE_CASE(name) { \ ··· 722 714 }), 723 715 BITFIELDS_ERR_CASE(bitfields___err_too_big_bitfield), 724 716 725 - /* size relocation checks */ 717 + /* field size and offset relocation checks */ 726 718 SIZE_CASE(size), 727 719 SIZE_CASE(size___diff_sz), 720 + SIZE_CASE(size___diff_offs), 728 721 SIZE_ERR_CASE(size___err_ambiguous), 729 722 730 723 /* validate type existence and size relocations */
+3
tools/testing/selftests/bpf/progs/btf__core_reloc_size___diff_offs.c
··· 1 + #include "core_reloc_types.h" 2 + 3 + void f(struct core_reloc_size___diff_offs x) {}
+18
tools/testing/selftests/bpf/progs/core_reloc_types.h
··· 785 785 */ 786 786 struct core_reloc_size_output { 787 787 int int_sz; 788 + int int_off; 788 789 int struct_sz; 790 + int struct_off; 789 791 int union_sz; 792 + int union_off; 790 793 int arr_sz; 794 + int arr_off; 791 795 int arr_elem_sz; 796 + int arr_elem_off; 792 797 int ptr_sz; 798 + int ptr_off; 793 799 int enum_sz; 800 + int enum_off; 794 801 int float_sz; 802 + int float_off; 795 803 }; 796 804 797 805 struct core_reloc_size { ··· 820 812 void *ptr_field; 821 813 enum { OTHER_VALUE = 0xFFFFFFFFFFFFFFFF } enum_field; 822 814 double float_field; 815 + }; 816 + 817 + struct core_reloc_size___diff_offs { 818 + float float_field; 819 + enum { YET_OTHER_VALUE = 123 } enum_field; 820 + void *ptr_field; 821 + int arr_field[4]; 822 + union { int x; } union_field; 823 + struct { int x; } struct_field; 824 + int int_field; 823 825 }; 824 826 825 827 /* Error case of two candidates with the fields (int_field) at the same
+24 -1
tools/testing/selftests/bpf/progs/test_core_reloc_size.c
··· 15 15 16 16 struct core_reloc_size_output { 17 17 int int_sz; 18 + int int_off; 18 19 int struct_sz; 20 + int struct_off; 19 21 int union_sz; 22 + int union_off; 20 23 int arr_sz; 24 + int arr_off; 21 25 int arr_elem_sz; 26 + int arr_elem_off; 22 27 int ptr_sz; 28 + int ptr_off; 23 29 int enum_sz; 30 + int enum_off; 24 31 int float_sz; 32 + int float_off; 25 33 }; 26 34 27 35 struct core_reloc_size { ··· 49 41 struct core_reloc_size_output *out = (void *)&data.out; 50 42 51 43 out->int_sz = bpf_core_field_size(in->int_field); 44 + out->int_off = bpf_core_field_offset(in->int_field); 45 + 52 46 out->struct_sz = bpf_core_field_size(in->struct_field); 47 + out->struct_off = bpf_core_field_offset(in->struct_field); 48 + 53 49 out->union_sz = bpf_core_field_size(in->union_field); 50 + out->union_off = bpf_core_field_offset(in->union_field); 51 + 54 52 out->arr_sz = bpf_core_field_size(in->arr_field); 55 - out->arr_elem_sz = bpf_core_field_size(struct core_reloc_size, arr_field[0]); 53 + out->arr_off = bpf_core_field_offset(in->arr_field); 54 + 55 + out->arr_elem_sz = bpf_core_field_size(struct core_reloc_size, arr_field[1]); 56 + out->arr_elem_off = bpf_core_field_offset(struct core_reloc_size, arr_field[1]); 57 + 56 58 out->ptr_sz = bpf_core_field_size(struct core_reloc_size, ptr_field); 59 + out->ptr_off = bpf_core_field_offset(struct core_reloc_size, ptr_field); 60 + 57 61 out->enum_sz = bpf_core_field_size(struct core_reloc_size, enum_field); 62 + out->enum_off = bpf_core_field_offset(struct core_reloc_size, enum_field); 63 + 58 64 out->float_sz = bpf_core_field_size(struct core_reloc_size, float_field); 65 + out->float_off = bpf_core_field_offset(struct core_reloc_size, float_field); 59 66 60 67 return 0; 61 68 }