Re: [PATCH v6 bpf-next 07/10] selftests/bpf: test kind encoding/decoding

bot+bpf-ci
2025-12-10T20:55:57Z

[-- Attachment #1: Type: text/plain, Size: 2364 bytes --]

> diff --git a/tools/testing/selftests/bpf/prog_tests/btf_kind.c b/tools/testing/selftests/bpf/prog_tests/btf_kind.c
> new file mode 100644
> index 000000000000..735ea0b588a5
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/btf_kind.c
> @@ -0,0 +1,176 @@

[ ... ]

> +static void write_raw_btf(const char *btf_path, void *raw_btf, size_t raw_size)
> +{
> + int fd = open(btf_path, O_WRONLY | O_CREAT);
^^

Should open() include a mode parameter here? POSIX requires a mode
argument when O_CREAT is specified. Other tests in this directory use
0644, for example d_path.c and fs_kfuncs.c both use:

open(path, O_CREAT | O_RDONLY, 0644)

> +
> + write(fd, raw_btf, raw_size);
> + close(fd);
> +}
> +

[ ... ]

> +void test_btf_kind_decoding(void)
> +{
> + __s32 int_id, unrec_id, id, id2;
> + LIBBPF_OPTS(btf_new_opts, opts);
> + struct btf *btf, *new_btf;
> + struct btf_kind_layout *k;
> + struct btf_header *hdr;
> + const void *raw_btf;
> + struct btf_type *t;
> + char btf_path[64];
> + void *new_raw_btf;
> + __u32 raw_size;

[ ... ]

> + raw_btf = (void *)btf__raw_data(btf, &raw_size);
> + if (!ASSERT_OK_PTR(raw_btf, "btf__raw_data"))
> + return;
> +
> + new_raw_btf = calloc(1, raw_size + sizeof(*k));
> + memcpy(new_raw_btf, raw_btf, raw_size);
^^^^^^^

Can memcpy() dereference NULL here if calloc() fails? Other BPF tests
check calloc() before use. For example, btf.c uses:

mapv = calloc(num_cpus, rounded_value_size);
if (CHECK(!mapv, "mapv allocation failure")) {

And htab_update.c uses:

value = calloc(1, value_size);
if (!ASSERT_OK_PTR(value, "calloc value"))

> +
> + /* add new layout description */
> + hdr = new_raw_btf;
> + hdr->kind_layout_len += sizeof(*k);
> + k = new_raw_btf + hdr->hdr_len + hdr->kind_layout_off;
> + k[NR_BTF_KINDS].info_sz = 0;
> + k[NR_BTF_KINDS].elem_sz = 0;
> +
> + /* now modify our typedef added above to be an unrecognized kind. */
> + t = (void *)hdr + hdr->hdr_len + hdr->type_off + sizeof(struct btf_type) +
> + sizeof(__u32);
> + t->info = (NR_BTF_KINDS << 24);

[ ... ]

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/20112692486