Skip to content

Commit e6054fc

Browse files
yonghong-songAlexei Starovoitov
authored and
Alexei Starovoitov
committed
selftests/bpf: Add test for map_ptr arithmetic
Change selftest map_ptr_kern.c with disabling inlining for one of subtests, which will fail the test without previous verifier change. Also added to verifier test for both "map_ptr += scalar" and "scalar += map_ptr" arithmetic. Signed-off-by: Yonghong Song <yhs@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Andrii Nakryiko <andriin@fb.com> Link: https://lore.kernel.org/bpf/20200908175703.2463721-1-yhs@fb.com
1 parent 7c69673 commit e6054fc

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

tools/testing/selftests/bpf/progs/map_ptr_kern.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ static inline int check_default(struct bpf_map *indirect,
8282
return 1;
8383
}
8484

85+
static __noinline int
86+
check_default_noinline(struct bpf_map *indirect, struct bpf_map *direct)
87+
{
88+
VERIFY(check(indirect, direct, sizeof(__u32), sizeof(__u32),
89+
MAX_ENTRIES));
90+
return 1;
91+
}
92+
8593
typedef struct {
8694
int counter;
8795
} atomic_t;
@@ -107,7 +115,7 @@ static inline int check_hash(void)
107115
struct bpf_map *map = (struct bpf_map *)&m_hash;
108116
int i;
109117

110-
VERIFY(check_default(&hash->map, map));
118+
VERIFY(check_default_noinline(&hash->map, map));
111119

112120
VERIFY(hash->n_buckets == MAX_ENTRIES);
113121
VERIFY(hash->elem_size == 64);

tools/testing/selftests/bpf/verifier/map_ptr.c

+32
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,35 @@
6060
.result = ACCEPT,
6161
.retval = 1,
6262
},
63+
{
64+
"bpf_map_ptr: r = 0, map_ptr = map_ptr + r",
65+
.insns = {
66+
BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
67+
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
68+
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
69+
BPF_MOV64_IMM(BPF_REG_0, 0),
70+
BPF_LD_MAP_FD(BPF_REG_1, 0),
71+
BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_0),
72+
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
73+
BPF_MOV64_IMM(BPF_REG_0, 0),
74+
BPF_EXIT_INSN(),
75+
},
76+
.fixup_map_hash_16b = { 4 },
77+
.result = ACCEPT,
78+
},
79+
{
80+
"bpf_map_ptr: r = 0, r = r + map_ptr",
81+
.insns = {
82+
BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
83+
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
84+
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
85+
BPF_MOV64_IMM(BPF_REG_1, 0),
86+
BPF_LD_MAP_FD(BPF_REG_0, 0),
87+
BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_0),
88+
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
89+
BPF_MOV64_IMM(BPF_REG_0, 0),
90+
BPF_EXIT_INSN(),
91+
},
92+
.fixup_map_hash_16b = { 4 },
93+
.result = ACCEPT,
94+
},

0 commit comments

Comments
 (0)