Re: [PATCH v2 bpf-next 18/18] selftests/bpf: Add a test verifying inline information

2026-09-01 Thread bot+bpf-ci
> diff --git a/tools/testing/selftests/bpf/prog_tests/btf_inline.c 
> b/tools/testing/selftests/bpf/prog_tests/btf_inline.c
> new file mode 100644
> index ..8e0a85f6c698
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/btf_inline.c
> @@ -0,0 +1,110 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2026, Oracle and/or its affiliates. */
> +
> +#include 
> +#include 
> +#include 
> +
> +#define BTF_SYSFS_DIR"/sys/kernel/btf"
> +#define BTF_INLINE_SUFFIX".inline"
> +
> +/*
> + * For a specific inline site, verify we have the right function,
> + * loc proto and loc param representation and that the offset is
> + * reasonable given the caller where it was inlined.
> + *
> + * Because bpf_testmod is compiled "out-of-tree" we have inline
> + * information in the split BTF directly rather than in btf_testmod.inline.
> + */

The comment states the inline information is "in the split BTF directly
rather than in btf_testmod.inline", but the code below parses
"/sys/kernel/btf/bpf_testmod.inline", which is exactly the file the
comment says is not used:

> + inline_btf = btf__parse_split(BTF_SYSFS_DIR "/bpf_testmod" 
> BTF_INLINE_SUFFIX,
> +   btf);

The commit message agrees with the code ("the inline information will be
in btf_testmod.inline which is relocated using bpf_testmod.ko .BTF.base"),
so the in-file comment appears to be inverted.

> +void test_btf_inline(void)
> +{

[ ... ]

> + inline_btf = btf__parse_split(BTF_SYSFS_DIR "/bpf_testmod" 
> BTF_INLINE_SUFFIX,
> +   btf);
> + err = libbpf_get_error(inline_btf);
> + /* pahole may not have inline BTF feature support. */
> + if (err == -ENOENT) {
> + skip = true;
> + goto out;
> + }
> + locsec_id = btf__find_by_name_kind(inline_btf, ".text", 
> BTF_KIND_LOCSEC);

Can inline_btf be NULL here? btf__parse_split() returns libbpf_ptr() of
the btf_parse() result, and libbpf_ptr() returns NULL with errno set for
errors other than -ENOENT. If btf__parse_split() fails with -EINVAL,
-EPROTO, -ENOMEM or -EPERM (malformed BTF, allocation failure, restricted
read), inline_btf is NULL and err is not -ENOENT, so the code falls
through and calls btf__find_by_name_kind(NULL, ...), which dereferences
the NULL pointer in btf__type_cnt().

Every other pointer in this function is checked with ASSERT_OK_PTR(); this
is the only exception. This matters because the test is meant to catch
malformed inline BTF from earlier commits in the series, but the crash
kills test_progs and takes every remaining test with it instead of
recording a test failure.

> + p = btf_loc_proto_params(t);
> + t = btf__type_by_id(inline_btf, *p);
> + lp = btf_loc_param(t);
> + if (!ASSERT_EQ(lp->flags, BTF_LOC_PARAM_REG, "param_is_reg"))

The second btf__type_by_id() result is used without the ASSERT_OK_PTR()
check that the first call three lines earlier receives. btf__type_by_id()
returns NULL for an out-of-range id, and btf_loc_param(t) is just
'(struct btf_loc_param *)(t + 1)', so lp->flags reads address 4 and
crashes.

There's also a legal-input case: the BTF spec allows a LOC_PROTO
parameter id to be 0 (meaning "no location info"). For *p == 0, libbpf
returns &btf_void, and lp->flags then reads past that static object,
producing a confusing failure message instead of a clear diagnosis.

Checking the pointer and the type kind would catch both cases.

> + if (!ASSERT_GT(l->offset, caller_addr, "inline_gt_caller"))
> + goto out;
> + /* simple sanity test to roughly ensure inline site still in function */
> + if (ASSERT_LT(l->offset, caller_addr + 256, "inline_in_caller"))
> + goto out;
> +out:

This branches to out when the assertion passes, but every other check in
the function is 'if (!ASSERT_xxx(...)) goto out;'. The failure is still
recorded, and 'out:' is the next label, so today the inversion has no
observable effect, but the sense is backwards and will silently do the
wrong thing the moment another check is appended after it.

Worth noting: the '+ 256' window is a compiler/config-dependent bound on
the size of bpf_testmod_uprobe_write(), so once the '!' is added this
becomes the one assertion in the test that can fail for reasons unrelated
to inline BTF correctness.

> diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c 
> b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
> index 2380b6cbdead..09d910d5e312 100644
> --- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
> +++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c

[ ... ]

> -static int testmod_register_uprobe(loff_t offset)
> +static __always_inline int testmod_register_uprobe(loff_t offset)
>  {

testmod_register_uprobe and its sole caller bpf_testmod_uprobe_write are
only compiled on x86_64. Both live inside the '#ifdef __x86_64__' region
that opens at line 721 ("bpf_testmod_uprobe sysfs 

Re: [PATCH v2 bpf-next 18/18] selftests/bpf: Add a test verifying inline information

2026-09-01 Thread sashiko-bot
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) 
to consider:
- [Medium] Parser desynchronization in module_get_base_addr() when reading 
trailing fields in /proc/modules.
- [Medium] Inverted logic in ASSERT_LT check causes early exit on test success.
- [Medium] Unhandled error from btf__parse_split leads to invalid pointer 
dereference.
- [Medium] Missing NULL pointer check after btf__type_by_id allows segmentation 
fault on malformed BTF.
--

commit 2cf9036457ef4f9129e585e625be7f2e1a33c7fa
Author: Alan Maguire 

selftests/bpf: Add a test verifying inline information

This commit adds a selftest to verify inline information for an
__always_inline function in bpf_testmod. It checks that the LOCSEC
descriptor offset is within the expected bounds of the function.

> diff --git a/tools/testing/selftests/bpf/prog_tests/btf_inline.c 
> b/tools/testing/selftests/bpf/prog_tests/btf_inline.c
> index 0..8e0a85f6c6980
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/btf_inline.c
[ ... ]
> + inline_btf = btf__parse_split(BTF_SYSFS_DIR "/bpf_testmod" 
> BTF_INLINE_SUFFIX,
> +   btf);
> + err = libbpf_get_error(inline_btf);
> + /* pahole may not have inline BTF feature support. */
> + if (err == -ENOENT) {
> + skip = true;
> + goto out;
> + }
> + locsec_id = btf__find_by_name_kind(inline_btf, ".text", 
> BTF_KIND_LOCSEC);

[Severity: Medium]
Does this code handle errors other than -ENOENT correctly?

If btf__parse_split() fails with an error like -ENOMEM, err will be extracted,
but since it is not -ENOENT, execution continues and the unhandled ERR_PTR in
inline_btf is passed to btf__find_by_name_kind(), leading to a dereference of
an invalid pointer.

[ ... ]
> + t = btf__type_by_id(inline_btf, l->loc_proto);
> + if (!ASSERT_OK_PTR(t, "loc_proto_ptr"))
> + goto out;
> + if (!ASSERT_EQ(btf_vlen(t), 1, "loc_proto_one_param"))
> + goto out;
> + p = btf_loc_proto_params(t);
> + t = btf__type_by_id(inline_btf, *p);
> + lp = btf_loc_param(t);

[Severity: Medium]
Can this lead to a NULL pointer dereference on malformed BTF data?

Unlike the safe pattern used earlier with ASSERT_OK_PTR, the return value of
btf__type_by_id() is passed directly to btf_loc_param() and dereferenced via
lp->flags without a NULL check.

> + if (!ASSERT_EQ(lp->flags, BTF_LOC_PARAM_REG, "param_is_reg"))
> + goto out;
> + if (!ASSERT_GT(l->offset, caller_addr, "inline_gt_caller"))
> + goto out;
> + /* simple sanity test to roughly ensure inline site still in function */
> + if (ASSERT_LT(l->offset, caller_addr + 256, "inline_in_caller"))
> + goto out;

[Severity: Medium]
Does this missing negation cause the test to exit early on success?

The ASSERT_LT macro evaluates to true when the check succeeds. Because the
condition lacks the negation ! used in all other assertions here, it will
execute the goto out branch when the sanity check passes.

> diff --git a/tools/testing/selftests/bpf/trace_helpers.c 
> b/tools/testing/selftests/bpf/trace_helpers.c
> index 679008b310d9a..bf4cb720e7b81 100644
> --- a/tools/testing/selftests/bpf/trace_helpers.c
> +++ b/tools/testing/selftests/bpf/trace_helpers.c
[ ... ]
> +long module_get_base_addr(const char *module)
> +{
> + unsigned long long addr, base_addr = 0;
> + char name[500];
> + FILE *f;
> +
> + f = fopen("/proc/modules", "r");
> + if (!f)
> + return 0;
> +
> + while (fscanf(f, "%s %*s %*s %*s %*s 0x%llx", name, &addr) > 0) {

[Severity: Medium]
Can this parser become desynchronized if trailing fields are present in
/proc/modules?

The fscanf format string expects exactly 6 tokens and lacks a way to consume
the remainder of the line. Out-of-tree modules natively produce a 7th token
(such as the (O) taint flag). 

When this happens, the trailing token is not consumed and the next loop
iteration will mistakenly read it as the name, breaking the parsing structure
for all subsequent lines.

> + if (strcmp(module, name) == 0) {
> + base_addr = addr;
> + break;
> + }
> + }

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=18