BPF List
 help / color / mirror / Atom feed
From: Ihor Solodrai <ihor.solodrai@linux.dev>
To: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Quentin Monnet <qmo@kernel.org>
Cc: bpf@vger.kernel.org, Alan Maguire <alan.maguire@oracle.com>,
	Cupertino Miranda <cupertino.miranda@oracle.com>,
	David Faust <david.faust@oracle.com>,
	Ian Rogers <irogers@google.com>,
	"Jose E . Marchesi" <jose.marchesi@oracle.com>,
	Vineet Gupta <vineet.gupta@linux.dev>,
	Yonghong Song <yonghong.song@linux.dev>
Subject: [PATCH bpf-next v1 7/8] selftests/bpf: Cover context and socket accesses through vmlinux.h
Date: Thu, 15 Oct 2026 17:00:11 -0700	[thread overview]
Message-ID: <20261016000012.2232059-8-ihor.solodrai@linux.dev> (raw)
In-Reply-To: <20261016000012.2232059-1-ihor.solodrai@linux.dev>

The generated vmlinux.h leaves __bpf_core_pai off the types whose field
accesses need static offsets and off their nested anonymous records,
and marks these types with __bpf_core_pso.

Some accesses fail to load if any part is missing. With __bpf_core_pai
on an anonymous member but not on its enclosing type, Clang roots the
CO-RE relocation at an unnamed type that libbpf cannot resolve. With
__bpf_core_pai on the type, a narrow field load can become pointer
arithmetic on the context pointer, which the verifier rejects. Without
__bpf_core_pso, merging field loads from two branches into one does the
same.

Cover each case with programs loaded through the generated header:
accesses through the anonymous unions of four context types, a narrow
skb->mark load, and a merged load where the compiler supports
preserve_static_offset.

Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
---
 .../selftests/bpf/prog_tests/verifier.c       |  2 +
 .../bpf/progs/verifier_ctx_rewritten.c        | 59 +++++++++++++++++++
 2 files changed, 61 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_ctx_rewritten.c

diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c
index c69c0f7e9cc8..56b626419ab6 100644
--- a/tools/testing/selftests/bpf/prog_tests/verifier.c
+++ b/tools/testing/selftests/bpf/prog_tests/verifier.c
@@ -41,6 +41,7 @@
 #include "verifier_const.skel.h"
 #include "verifier_const_or.skel.h"
 #include "verifier_ctx.skel.h"
+#include "verifier_ctx_rewritten.skel.h"
 #include "verifier_ctx_sk_msg.skel.h"
 #include "verifier_d_path.skel.h"
 #include "verifier_default_trusted_ptr.skel.h"
@@ -231,6 +232,7 @@ void test_verifier_cgroup_storage(void)       { RUN(verifier_cgroup_storage); }
 void test_verifier_const(void)                { RUN(verifier_const); }
 void test_verifier_const_or(void)             { RUN(verifier_const_or); }
 void test_verifier_ctx(void)                  { RUN_TESTS(verifier_ctx); }
+void test_verifier_ctx_rewritten(void)        { RUN_TESTS(verifier_ctx_rewritten); }
 void test_verifier_ctx_sk_msg(void)           { RUN(verifier_ctx_sk_msg); }
 void test_verifier_d_path(void)               { RUN(verifier_d_path); }
 void test_verifier_default_trusted_ptr(void)  { RUN_TESTS(verifier_default_trusted_ptr); }
diff --git a/tools/testing/selftests/bpf/progs/verifier_ctx_rewritten.c b/tools/testing/selftests/bpf/progs/verifier_ctx_rewritten.c
new file mode 100644
index 000000000000..33eb975a170a
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/verifier_ctx_rewritten.c
@@ -0,0 +1,59 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+SEC("tc")
+__success
+int skb_socket(struct __sk_buff *skb)
+{
+	struct bpf_sock *sk = skb->sk;
+
+	return sk ? sk->family : 0;
+}
+
+SEC("sockops")
+__success
+int sockops_args_reply(struct bpf_sock_ops *skops)
+{
+	skops->reply = skops->args[1];
+	return 0;
+}
+
+SEC("sk_msg")
+__success
+int sk_msg_data(struct sk_msg_md *msg)
+{
+	return msg->data < msg->data_end;
+}
+
+SEC("cgroup/getsockopt")
+__success
+int sockopt_optval(struct bpf_sockopt *ctx)
+{
+	return ctx->optval < ctx->optval_end;
+}
+
+SEC("tc")
+__success
+int skb_narrow_field(struct __sk_buff *skb)
+{
+	return ((__u8 *)&skb->mark)[1];
+}
+
+#if __has_attribute(preserve_static_offset)
+SEC("tc")
+__success
+int skb_merge_loads(struct __sk_buff *skb)
+{
+	__u32 value;
+
+	if (skb->len)
+		value = skb->pkt_type;
+	else
+		value = skb->mark;
+	return value;
+}
+#endif
+
+char _license[] SEC("license") = "GPL";
-- 
2.56.0


  parent reply	other threads:[~2026-10-16  0:01 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-16  0:00 [PATCH bpf-next v1 0/8] bpftool: Per-record CO-RE attributes in the C dump Ihor Solodrai
2026-10-16  0:00 ` [PATCH bpf-next v1 1/8] bpftool: Dump C from a detached BTF copy Ihor Solodrai
2026-10-16  1:18   ` Alexei Starovoitov
2026-10-16  9:44     ` Alan Maguire
2026-10-16  9:25   ` Alan Maguire
2026-10-16  0:00 ` [PATCH bpf-next v1 2/8] selftests/bpf: Cover unknown BTF header-extension bytes in btf dump Ihor Solodrai
2026-10-16  0:00 ` [PATCH bpf-next v1 3/8] bpftool: Apply __bpf_core_pai per record on BTF dump Ihor Solodrai
2026-10-16  0:39   ` bot+bpf-ci
2026-10-16  1:17   ` Alexei Starovoitov
2026-10-16  0:00 ` [PATCH bpf-next v1 4/8] bpftool: Skip __bpf_core_pai on verifier-rewritten context types Ihor Solodrai
2026-10-16  0:00 ` [PATCH bpf-next v1 5/8] bpftool: Emit __bpf_core_pso " Ihor Solodrai
2026-10-16  1:27   ` Alexei Starovoitov
2026-10-16  5:49     ` Ihor Solodrai
2026-10-16 15:53       ` Alexei Starovoitov
2026-10-16  0:00 ` [PATCH bpf-next v1 6/8] selftests/bpf: Cover record attributes in bpftool BTF dumps Ihor Solodrai
2026-10-16  0:00 ` Ihor Solodrai [this message]
2026-10-16  0:00 ` [PATCH bpf-next v1 8/8] bpftool: Document __bpf_core_pai and __bpf_core_pso Ihor Solodrai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261016000012.2232059-8-ihor.solodrai@linux.dev \
    --to=ihor.solodrai@linux.dev \
    --cc=alan.maguire@oracle.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=cupertino.miranda@oracle.com \
    --cc=daniel@iogearbox.net \
    --cc=david.faust@oracle.com \
    --cc=eddyz87@gmail.com \
    --cc=irogers@google.com \
    --cc=jose.marchesi@oracle.com \
    --cc=memxor@gmail.com \
    --cc=qmo@kernel.org \
    --cc=vineet.gupta@linux.dev \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox