selftests/bpf: Add a test for an exception callback behind a dead subprog

Add a test where a static subprogram only called from dead code sits in
front of the exception callback, so that its removal compacts
subprog_info[] underneath the callback's index.

  # LDLIBS=-static PKG_CONFIG='pkg-config --static' ./vmtest.sh -- ./test_progs -t exceptions
  [...]
  #129/119 exceptions/exception_cb_behind_dead_subprog:OK
  #129     exceptions:OK
  Summary: 1/119 PASSED, 0 SKIPPED, 0/0 FAILED

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://patch.msgid.link/20260925175244.1136329-17-daniel@iogearbox.net
This commit is contained in:
Daniel Borkmann 2026-09-25 19:52:43 +02:00 committed by Alexei Starovoitov
parent 28e458bbb4
commit 91ed1942ed
No known key found for this signature in database
2 changed files with 47 additions and 0 deletions

View File

@ -6,6 +6,7 @@
#include "exceptions_ext.skel.h"
#include "exceptions_fail.skel.h"
#include "exceptions_assert.skel.h"
#include "exceptions_dead_subprog.skel.h"
static char log_buf[1024 * 1024];
@ -410,9 +411,15 @@ static void test_exceptions_assertions(void)
RUN_TESTS(exceptions_assert);
}
static void test_exceptions_dead_subprog(void)
{
RUN_TESTS(exceptions_dead_subprog);
}
void test_exceptions(void)
{
test_exceptions_success();
test_exceptions_failure();
test_exceptions_assertions();
test_exceptions_dead_subprog();
}

View File

@ -0,0 +1,40 @@
// SPDX-License-Identifier: GPL-2.0
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
#include "bpf_experimental.h"
/*
* Read-only and frozen, so the verifier sees a constant zero, while clang
* cannot fold the volatile read and keeps the call below.
*/
const volatile int never;
/* Only called from dead code, hence removed as a whole before the JIT runs. */
static __noinline int dead_subprog(struct __sk_buff *ctx)
{
return ctx->len;
}
__noinline int exception_cb_behind_dead_subprog_cb(u64 cookie)
{
return 42;
}
/*
* libbpf appends the exception callback behind the subprograms reached by
* calls, so removing the dead one compacts subprog_info[] underneath the
* callback's index.
*/
SEC("?tc")
__exception_cb(exception_cb_behind_dead_subprog_cb)
__success __retval(42)
int exception_cb_behind_dead_subprog(struct __sk_buff *ctx)
{
if (never)
return dead_subprog(ctx);
bpf_throw(0);
return 0;
}
char _license[] SEC("license") = "GPL";