Re: [RFC PATCH 2/3] selftests/bpf: add memcg eBPF struct ops test

bot+bpf-ci
2025-11-19T02:19:39Z

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

> diff --git a/tools/testing/selftests/bpf/prog_tests/memcg_ops.c b/tools/testing/selftests/bpf/prog_tests/memcg_ops.c
> new file mode 100644
> index 000000000..3f989bcfb
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/memcg_ops.c

[ ... ]

> +void test_memcg_ops_double_attach(void)
> +{
> + struct bpf_object *obj, *obj2;
> + struct bpf_map *map, *map2;
> + struct bpf_link *link = NULL, *link2 = NULL;
> + int err;
> +
> + obj = bpf_object__open_file("memcg_ops.bpf.o", NULL);
> + err = libbpf_get_error(obj);
> + if (CHECK_FAIL(err)) {
> + obj = NULL;
> + goto out;
> + }
> +
> + err = bpf_object__load(obj);
> + if (CHECK_FAIL(err))
> + goto out;
> +
> + map = bpf_object__find_map_by_name(obj, "mcg_ops");
> + if (!ASSERT_OK_PTR(map, "bpf_object__find_map_by_name"))
> + goto out;
> +
> + link = bpf_map__attach_struct_ops(map);
> + if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops"))
> + goto out;
> +
> + obj2 = bpf_object__open_file("memcg_ops.bpf.o", NULL);
> + err = libbpf_get_error(obj2);
> + if (CHECK_FAIL(err)) {
> + obj2 = NULL;
> + goto out;
> + }
> +
> + err = bpf_object__load(obj2);
> + if (CHECK_FAIL(err))
> + goto out;
> +
> + map2 = bpf_object__find_map_by_name(obj2, "mcg_ops");
> + if (!ASSERT_OK_PTR(map, "bpf_object__find_map_by_name"))
^^^

In test_memcg_ops_double_attach(), the NULL check is validating the wrong
variable. The code retrieves map2 from obj2, but then checks map (from the
first object) instead of map2. This means if bpf_object__find_map_by_name()
returns NULL for map2, the test won't catch it and will proceed to use the
potentially NULL map2 pointer in the subsequent attach call.

Should this be checking map2 instead?

> + goto out;
> +
> + link2 = bpf_map__attach_struct_ops(map2);
> + if (!ASSERT_ERR_PTR(link2, "bpf_map__attach_struct_ops")) {
> + bpf_link__destroy(link2);
> + goto out;
> + }
> +
> +out:
> + if (link)
> + bpf_link__destroy(link);
> + if (obj)
> + bpf_object__close(obj);
> + if (obj2)
> + bpf_object__close(obj2);
> +}

---
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/19486904708