]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/bpf: Convert ctx tests from ASM to C
authorKumar Kartikeya Dwivedi <memxor@gmail.com>
Mon, 6 Apr 2026 19:43:57 +0000 (21:43 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Mon, 6 Apr 2026 22:27:26 +0000 (15:27 -0700)
Convert existing tests from ASM to C, in prep for future changes to add
more comprehensive tests.

Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Link: https://lore.kernel.org/r/20260406194403.1649608-4-memxor@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/verifier.c
tools/testing/selftests/bpf/progs/verifier_ctx.c

index 1ac366fd4daef84be1cf851618624cdf00b47eb4..169cf7fbf40fe43e01754e2fc3f182bb234594d0 100644 (file)
@@ -175,7 +175,7 @@ void test_verifier_cgroup_skb(void)           { RUN(verifier_cgroup_skb); }
 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(verifier_ctx); }
+void test_verifier_ctx(void)                  { RUN_TESTS(verifier_ctx); }
 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); }
index ae756764ffba8be633d5c16966b878b871f9740a..4c285ac8fff6ebc3f63aa6778483a80929b1ae85 100644 (file)
@@ -295,68 +295,58 @@ padding_access("sk_reuseport", sk_reuseport_md, hash, 4);
 SEC("syscall")
 __description("syscall: write to ctx with fixed offset")
 __success
-__naked void syscall_ctx_fixed_off_write(void)
+int syscall_ctx_fixed_off_write(void *ctx)
 {
-       asm volatile ("                                 \
-       r0 = 0;                                         \
-       *(u32*)(r1 + 0) = r0;                           \
-       r1 += 4;                                        \
-       *(u32*)(r1 + 0) = r0;                           \
-       exit;                                           \
-"      ::: __clobber_all);
+       char *p = ctx;
+
+       *(__u32 *)p = 0;
+       *(__u32 *)(p + 4) = 0;
+       return 0;
 }
 
 /*
- * Test that program types without convert_ctx_access can dereference
- * their ctx pointer after adding a fixed offset. Variable and negative
- * offsets should still be rejected.
+ * For non-syscall program types without convert_ctx_access, direct ctx
+ * dereference is still allowed after adding a fixed offset, while variable
+ * and negative direct accesses reject.
+ *
+ * Passing ctx as a helper or kfunc memory argument is only permitted for
+ * syscall programs, so the helper and kfunc cases below validate rejection
+ * for non-syscall ctx pointers at fixed, variable, and zero-sized accesses.
  */
-#define no_rewrite_ctx_access(type, name, off, ld_op)                  \
+#define no_rewrite_ctx_access(type, name, off, load_t)                 \
        SEC(type)                                                       \
        __description(type ": read ctx at fixed offset")                \
        __success                                                       \
-       __naked void no_rewrite_##name##_fixed(void)                    \
+       int no_rewrite_##name##_fixed(void *ctx)                        \
        {                                                               \
-               asm volatile ("                                         \
-               r1 += %[__off];                                         \
-               r0 = *(" #ld_op " *)(r1 + 0);                           \
-               r0 = 0;                                                 \
-               exit;"                                                  \
-               :                                                       \
-               : __imm_const(__off, off)                               \
-               : __clobber_all);                                       \
+               char *p = ctx;                                          \
+               volatile load_t val;                                    \
+                                                                       \
+               val = *(load_t *)(p + off);                             \
+               (void)val;                                              \
+               return 0;                                               \
        }                                                               \
        SEC(type)                                                       \
        __description(type ": reject variable offset ctx access")       \
        __failure __msg("variable ctx access var_off=")                 \
-       __naked void no_rewrite_##name##_var(void)                      \
+       int no_rewrite_##name##_var(void *ctx)                  \
        {                                                               \
-               asm volatile ("                                         \
-               r6 = r1;                                                \
-               call %[bpf_get_prandom_u32];                            \
-               r1 = r6;                                                \
-               r0 &= 4;                                                \
-               r1 += r0;                                               \
-               r0 = *(" #ld_op " *)(r1 + 0);                           \
-               r0 = 0;                                                 \
-               exit;"                                                  \
-               :                                                       \
-               : __imm(bpf_get_prandom_u32)                            \
-               : __clobber_all);                                       \
+               __u64 off_var = bpf_get_prandom_u32();                  \
+               char *p = ctx;                                          \
+                                                                       \
+               off_var &= 4;                                           \
+               p += off_var;                                           \
+               return *(load_t *)p;                                    \
        }                                                               \
        SEC(type)                                                       \
        __description(type ": reject negative offset ctx access")       \
-       __failure __msg("negative offset ctx ptr")                      \
-       __naked void no_rewrite_##name##_neg(void)                      \
+       __failure __msg("invalid bpf_context access")                   \
+       int no_rewrite_##name##_neg(void *ctx)                  \
        {                                                               \
-               asm volatile ("                                         \
-               r1 += %[__neg_off];                                     \
-               r0 = *(" #ld_op " *)(r1 + 0);                           \
-               r0 = 0;                                                 \
-               exit;"                                                  \
-               :                                                       \
-               : __imm_const(__neg_off, -(off))                        \
-               : __clobber_all);                                       \
+               char *p = ctx;                                          \
+                                                                       \
+               p -= 612;                                               \
+               return *(load_t *)p;                                    \
        }
 
 no_rewrite_ctx_access("kprobe", kprobe, 8, u64);