selftests/bpf: Test global functions returning arena pointers by value

Cover the by-value struct returns a global function may now make: two
arena pointers filling R0:R2, an arena pointer beside a scalar, an array
of them, and an eight byte struct returned in R0 alone. The existing
cases for a struct and a union carrying a plain pointer stay rejected.

check_arena_struct_ret() also stores through both halves of the returned
pair and reads them back, so the test covers the returned pointers still
being usable as arena pointers rather than only the program verifying.

A toolchain guard goes into each file, and they differ because the
constructs do.

aggregate_ret_func.c uses "#if defined(__clang__)". Those cases are
__naked, so the function body is the inline asm and nothing else: the
clang compiler never lowers the return, and the 16 byte return type
reaches the verifier only through BTF. The minimum checked is LLVM 21.
gcc is excluded because it returns a by-value struct through a hidden
pointer and emits the 'r0 = r1' returning it after the __naked body's
exit, leaving the subprogram falling through.

verifier_arena.c uses "#if defined(__clang_major__) && __clang_major__ >=
23". arena_word_pair() returns the struct from C, so the compiler lowers
the return itself, and a 16 byte return only lands in R0:R2 with the
LLVM 23 BPF ABI. An older clang does not fall back to anything here, it
rejects the function with "aggregate returns are not supported", so the
floor is what lets the file build at all.

Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
Link: https://lore.kernel.org/r/20260829061605.1698601-1-yonghong.song@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
Yonghong Song 2026-08-28 23:16:05 -07:00 committed by Alexei Starovoitov
parent cc03706d92
commit 18bc618388
2 changed files with 194 additions and 0 deletions

View File

@ -2,6 +2,7 @@
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf_arena_common.h>
#include "bpf_misc.h"
typedef unsigned __int128 u128;
@ -234,4 +235,149 @@ __naked int aggregate_ret_global_union_ptr_fail(void)
#endif
/*
* gcc returns a by-value struct through a hidden pointer, and emits the
* 'r0 = r1' returning it after the __naked body's exit, leaving the
* subprogram falling through. Build these with clang only.
*/
#if defined(__clang__)
struct arena_pair {
void __arena *lo;
void __arena *hi;
};
struct arena_and_scalar {
void __arena *p;
__u64 x;
};
struct arena_array {
void __arena *p[2];
};
struct arena_single {
void __arena *p;
};
union arena_upair {
void __arena *p;
__u64 halves[2];
};
__naked struct arena_pair global_ret_arena_pair(void)
{
asm volatile (
"r0 = 0;"
"r2 = 0;"
"exit;"
);
}
SEC("tc")
__load_if_JITed()
__success __retval(0)
__naked int aggregate_ret_global_arena_pair(void)
{
asm volatile (
"call %[global_ret_arena_pair];"
"r0 = 0;"
"exit;"
:
: __imm(global_ret_arena_pair)
: __clobber_all);
}
__naked struct arena_and_scalar global_ret_arena_and_scalar(void)
{
asm volatile (
"r0 = 0;"
"r2 = 0;"
"exit;"
);
}
SEC("tc")
__load_if_JITed()
__success __retval(0)
__naked int aggregate_ret_global_arena_and_scalar(void)
{
asm volatile (
"call %[global_ret_arena_and_scalar];"
"r0 = 0;"
"exit;"
:
: __imm(global_ret_arena_and_scalar)
: __clobber_all);
}
__naked struct arena_array global_ret_arena_array(void)
{
asm volatile (
"r0 = 0;"
"r2 = 0;"
"exit;"
);
}
SEC("tc")
__load_if_JITed()
__success __retval(0)
__naked int aggregate_ret_global_arena_array(void)
{
asm volatile (
"call %[global_ret_arena_array];"
"r0 = 0;"
"exit;"
:
: __imm(global_ret_arena_array)
: __clobber_all);
}
__naked struct arena_single global_ret_arena_single(void)
{
asm volatile (
"r0 = 0;"
"exit;"
);
}
SEC("tc")
__success __retval(0)
__naked int aggregate_ret_global_arena_single(void)
{
asm volatile (
"call %[global_ret_arena_single];"
"r0 = 0;"
"exit;"
:
: __imm(global_ret_arena_single)
: __clobber_all);
}
__naked union arena_upair global_ret_arena_union(void)
{
asm volatile (
"r0 = 0;"
"r2 = 0;"
"exit;"
);
}
SEC("tc")
__load_if_JITed()
__success __retval(0)
__naked int aggregate_ret_global_arena_union(void)
{
asm volatile (
"call %[global_ret_arena_union];"
"r0 = 0;"
"exit;"
:
: __imm(global_ret_arena_union)
: __clobber_all);
}
#endif
char _license[] SEC("license") = "GPL";

View File

@ -734,4 +734,52 @@ int check_arena_arg_ret(void *ctx)
return 0;
}
#if defined(__clang_major__) && __clang_major__ >= 23
struct arena_word_pair {
u32 __arena *first;
u32 __arena *second;
};
__weak struct arena_word_pair arena_word_pair(u32 __arena *page)
{
struct arena_word_pair pair;
pair.first = page;
pair.second = page + 1;
return pair;
}
SEC("syscall")
__load_if_JITed()
__success __retval(0)
int check_arena_struct_ret(void *ctx)
{
u32 __arena *page = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
u32 volatile __arena *first, *second;
struct arena_word_pair pair;
if (!page)
return 1;
pair = arena_word_pair(page);
if (!pair.first || !pair.second)
return 2;
/* Both halves of the pair must still be usable as arena pointers. */
first = pair.first;
second = pair.second;
*first = 1;
*second = 2;
if (*first != 1)
return 3;
if (*second != 2)
return 4;
return 0;
}
#endif
char _license[] SEC("license") = "GPL";