Skip to content

Commit fb30159

Browse files
Yonghong SongAlexei Starovoitov
authored andcommitted
selftests/bpf: Add a failure test for bpf_kptr_xchg() with local kptr
For a bpf_kptr_xchg() with local kptr, if the map value kptr type and allocated local obj type does not match, with the previous patch, the below verifier error message will be logged: R2 is of type <allocated local obj type> but <map value kptr type> is expected Without the previous patch, the test will have unexpected success. Signed-off-by: Yonghong Song <[email protected]> Acked-by: Kumar Kartikeya Dwivedi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent ab6c637 commit fb30159

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

tools/testing/selftests/bpf/prog_tests/local_kptr_stash.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <network_helpers.h>
66

77
#include "local_kptr_stash.skel.h"
8+
#include "local_kptr_stash_fail.skel.h"
89
static void test_local_kptr_stash_simple(void)
910
{
1011
LIBBPF_OPTS(bpf_test_run_opts, opts,
@@ -51,10 +52,17 @@ static void test_local_kptr_stash_unstash(void)
5152
local_kptr_stash__destroy(skel);
5253
}
5354

54-
void test_local_kptr_stash_success(void)
55+
static void test_local_kptr_stash_fail(void)
56+
{
57+
RUN_TESTS(local_kptr_stash_fail);
58+
}
59+
60+
void test_local_kptr_stash(void)
5561
{
5662
if (test__start_subtest("local_kptr_stash_simple"))
5763
test_local_kptr_stash_simple();
5864
if (test__start_subtest("local_kptr_stash_unstash"))
5965
test_local_kptr_stash_unstash();
66+
if (test__start_subtest("local_kptr_stash_fail"))
67+
test_local_kptr_stash_fail();
6068
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
3+
4+
#include <vmlinux.h>
5+
#include <bpf/bpf_helpers.h>
6+
#include <bpf/bpf_tracing.h>
7+
#include <bpf/bpf_core_read.h>
8+
#include "../bpf_experimental.h"
9+
#include "bpf_misc.h"
10+
11+
struct node_data {
12+
long key;
13+
long data;
14+
struct bpf_rb_node node;
15+
};
16+
17+
struct map_value {
18+
struct node_data __kptr *node;
19+
};
20+
21+
struct node_data2 {
22+
long key[4];
23+
};
24+
25+
/* This is necessary so that LLVM generates BTF for node_data struct
26+
* If it's not included, a fwd reference for node_data will be generated but
27+
* no struct. Example BTF of "node" field in map_value when not included:
28+
*
29+
* [10] PTR '(anon)' type_id=35
30+
* [34] FWD 'node_data' fwd_kind=struct
31+
* [35] TYPE_TAG 'kptr_ref' type_id=34
32+
*/
33+
struct node_data *just_here_because_btf_bug;
34+
35+
struct {
36+
__uint(type, BPF_MAP_TYPE_ARRAY);
37+
__type(key, int);
38+
__type(value, struct map_value);
39+
__uint(max_entries, 2);
40+
} some_nodes SEC(".maps");
41+
42+
SEC("tc")
43+
__failure __msg("invalid kptr access, R2 type=ptr_node_data2 expected=ptr_node_data")
44+
long stash_rb_nodes(void *ctx)
45+
{
46+
struct map_value *mapval;
47+
struct node_data2 *res;
48+
int idx = 0;
49+
50+
mapval = bpf_map_lookup_elem(&some_nodes, &idx);
51+
if (!mapval)
52+
return 1;
53+
54+
res = bpf_obj_new(typeof(*res));
55+
if (!res)
56+
return 1;
57+
res->key[0] = 40;
58+
59+
res = bpf_kptr_xchg(&mapval->node, res);
60+
if (res)
61+
bpf_obj_drop(res);
62+
return 0;
63+
}
64+
65+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)