selftests/bpf: add anonymous user struct as global subprog arg test

Add tests validating that kernel handles pointer to anonymous struct
argument as PTR_TO_MEM case, not as PTR_TO_CTX case.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20240212233221.2575350-5-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
Andrii Nakryiko 2024-02-12 15:32:21 -08:00 committed by Alexei Starovoitov
parent 879bbe7aa4
commit 63d5a33fb4

View File

@ -115,6 +115,35 @@ int arg_tag_nullable_ptr_fail(void *ctx)
return subprog_nullable_ptr_bad(&x);
}
typedef struct {
int x;
} user_struct_t;
__noinline __weak int subprog_user_anon_mem(user_struct_t *t)
{
return t ? t->x : 0;
}
SEC("?tracepoint")
__failure __log_level(2)
__msg("invalid bpf_context access")
__msg("Caller passes invalid args into func#1 ('subprog_user_anon_mem')")
int anon_user_mem_invalid(void *ctx)
{
/* can't pass PTR_TO_CTX as user memory */
return subprog_user_anon_mem(ctx);
}
SEC("?tracepoint")
__success __log_level(2)
__msg("Func#1 ('subprog_user_anon_mem') is safe for any args that match its prototype")
int anon_user_mem_valid(void *ctx)
{
user_struct_t t = { .x = 42 };
return subprog_user_anon_mem(&t);
}
__noinline __weak int subprog_nonnull_ptr_good(int *p1 __arg_nonnull, int *p2 __arg_nonnull)
{
return (*p1) * (*p2); /* good, no need for NULL checks */