Skip to content

Commit 7d68e38

Browse files
yonghong-songAlexei Starovoitov
authored andcommitted
bpf: Permit size-0 datasec
llvm patch https://reviews.llvm.org/D84002 permitted to emit empty rodata datasec if the elf .rodata section contains read-only data from local variables. These local variables will be not emitted as BTF_KIND_VARs since llvm converted these local variables as static variables with private linkage without debuginfo types. Such an empty rodata datasec will make skeleton code generation easy since for skeleton a rodata struct will be generated if there is a .rodata elf section. The existence of a rodata btf datasec is also consistent with the existence of a rodata map created by libbpf. The btf with such an empty rodata datasec will fail in the kernel though as kernel will reject a datasec with zero vlen and zero size. For example, for the below code, int sys_enter(void *ctx) { int fmt[6] = {1, 2, 3, 4, 5, 6}; int dst[6]; bpf_probe_read(dst, sizeof(dst), fmt); return 0; } We got the below btf (bpftool btf dump ./test.o): [1] PTR '(anon)' type_id=0 [2] FUNC_PROTO '(anon)' ret_type_id=3 vlen=1 'ctx' type_id=1 [3] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED [4] FUNC 'sys_enter' type_id=2 linkage=global [5] INT 'char' size=1 bits_offset=0 nr_bits=8 encoding=SIGNED [6] ARRAY '(anon)' type_id=5 index_type_id=7 nr_elems=4 [7] INT '__ARRAY_SIZE_TYPE__' size=4 bits_offset=0 nr_bits=32 encoding=(none) [8] VAR '_license' type_id=6, linkage=global-alloc [9] DATASEC '.rodata' size=0 vlen=0 [10] DATASEC 'license' size=0 vlen=1 type_id=8 offset=0 size=4 When loading the ./test.o to the kernel with bpftool, we see the following error: libbpf: Error loading BTF: Invalid argument(22) libbpf: magic: 0xeb9f ... [6] ARRAY (anon) type_id=5 index_type_id=7 nr_elems=4 [7] INT __ARRAY_SIZE_TYPE__ size=4 bits_offset=0 nr_bits=32 encoding=(none) [8] VAR _license type_id=6 linkage=1 [9] DATASEC .rodata size=24 vlen=0 vlen == 0 libbpf: Error loading .BTF into kernel: -22. BTF is optional, ignoring. Basically, libbpf changed .rodata datasec size to 24 since elf .rodata section size is 24. The kernel then rejected the BTF since vlen = 0. Note that the above kernel verifier failure can be worked around with changing local variable "fmt" to a static or global, optionally const, variable. This patch permits a datasec with vlen = 0 in kernel. Signed-off-by: Yonghong Song <yhs@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20210119153519.3901963-1-yhs@fb.com
1 parent 8edc0c6 commit 7d68e38

File tree

2 files changed

+21
-5
lines changed
  • kernel/bpf
  • tools/testing/selftests/bpf/prog_tests

2 files changed

+21
-5
lines changed

kernel/bpf/btf.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3540,11 +3540,6 @@ static s32 btf_datasec_check_meta(struct btf_verifier_env *env,
35403540
return -EINVAL;
35413541
}
35423542

3543-
if (!btf_type_vlen(t)) {
3544-
btf_verifier_log_type(env, t, "vlen == 0");
3545-
return -EINVAL;
3546-
}
3547-
35483543
if (!t->size) {
35493544
btf_verifier_log_type(env, t, "size == 0");
35503545
return -EINVAL;

tools/testing/selftests/bpf/prog_tests/btf.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3509,6 +3509,27 @@ static struct btf_raw_test raw_tests[] = {
35093509
.value_type_id = 3 /* arr_t */,
35103510
.max_entries = 4,
35113511
},
3512+
/*
3513+
* elf .rodata section size 4 and btf .rodata section vlen 0.
3514+
*/
3515+
{
3516+
.descr = "datasec: vlen == 0",
3517+
.raw_types = {
3518+
/* int */
3519+
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3520+
/* .rodata section */
3521+
BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 0), 4),
3522+
/* [2] */
3523+
BTF_END_RAW,
3524+
},
3525+
BTF_STR_SEC("\0.rodata"),
3526+
.map_type = BPF_MAP_TYPE_ARRAY,
3527+
.key_size = sizeof(int),
3528+
.value_size = sizeof(int),
3529+
.key_type_id = 1,
3530+
.value_type_id = 1,
3531+
.max_entries = 1,
3532+
},
35123533

35133534
}; /* struct btf_raw_test raw_tests[] */
35143535

0 commit comments

Comments
 (0)