Skip to content

Commit fcd1c91

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf: add couple of test cases for signed extended imms
Add a couple of test cases for interpreter and JIT that are related to an issue we faced some time ago in Cilium [1], which is fixed in LLVM with commit e53750e1e086 ("bpf: fix bug on silently truncating 64-bit immediate"). Test cases were run-time checking kernel to behave as intended which should also provide some guidance for current or new JITs in case they should trip over this. Added for cBPF and eBPF. [1] cilium/cilium#2162 Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 205c380 commit fcd1c91

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

lib/test_bpf.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6109,6 +6109,110 @@ static struct bpf_test tests[] = {
61096109
{ { ETH_HLEN, 42 } },
61106110
.fill_helper = bpf_fill_ld_abs_vlan_push_pop2,
61116111
},
6112+
/* Checking interpreter vs JIT wrt signed extended imms. */
6113+
{
6114+
"JNE signed compare, test 1",
6115+
.u.insns_int = {
6116+
BPF_ALU32_IMM(BPF_MOV, R1, 0xfefbbc12),
6117+
BPF_ALU32_IMM(BPF_MOV, R3, 0xffff0000),
6118+
BPF_MOV64_REG(R2, R1),
6119+
BPF_ALU64_REG(BPF_AND, R2, R3),
6120+
BPF_ALU32_IMM(BPF_MOV, R0, 1),
6121+
BPF_JMP_IMM(BPF_JNE, R2, -17104896, 1),
6122+
BPF_ALU32_IMM(BPF_MOV, R0, 2),
6123+
BPF_EXIT_INSN(),
6124+
},
6125+
INTERNAL,
6126+
{ },
6127+
{ { 0, 1 } },
6128+
},
6129+
{
6130+
"JNE signed compare, test 2",
6131+
.u.insns_int = {
6132+
BPF_ALU32_IMM(BPF_MOV, R1, 0xfefbbc12),
6133+
BPF_ALU32_IMM(BPF_MOV, R3, 0xffff0000),
6134+
BPF_MOV64_REG(R2, R1),
6135+
BPF_ALU64_REG(BPF_AND, R2, R3),
6136+
BPF_ALU32_IMM(BPF_MOV, R0, 1),
6137+
BPF_JMP_IMM(BPF_JNE, R2, 0xfefb0000, 1),
6138+
BPF_ALU32_IMM(BPF_MOV, R0, 2),
6139+
BPF_EXIT_INSN(),
6140+
},
6141+
INTERNAL,
6142+
{ },
6143+
{ { 0, 1 } },
6144+
},
6145+
{
6146+
"JNE signed compare, test 3",
6147+
.u.insns_int = {
6148+
BPF_ALU32_IMM(BPF_MOV, R1, 0xfefbbc12),
6149+
BPF_ALU32_IMM(BPF_MOV, R3, 0xffff0000),
6150+
BPF_ALU32_IMM(BPF_MOV, R4, 0xfefb0000),
6151+
BPF_MOV64_REG(R2, R1),
6152+
BPF_ALU64_REG(BPF_AND, R2, R3),
6153+
BPF_ALU32_IMM(BPF_MOV, R0, 1),
6154+
BPF_JMP_REG(BPF_JNE, R2, R4, 1),
6155+
BPF_ALU32_IMM(BPF_MOV, R0, 2),
6156+
BPF_EXIT_INSN(),
6157+
},
6158+
INTERNAL,
6159+
{ },
6160+
{ { 0, 2 } },
6161+
},
6162+
{
6163+
"JNE signed compare, test 4",
6164+
.u.insns_int = {
6165+
BPF_LD_IMM64(R1, -17104896),
6166+
BPF_ALU32_IMM(BPF_MOV, R0, 1),
6167+
BPF_JMP_IMM(BPF_JNE, R1, -17104896, 1),
6168+
BPF_ALU32_IMM(BPF_MOV, R0, 2),
6169+
BPF_EXIT_INSN(),
6170+
},
6171+
INTERNAL,
6172+
{ },
6173+
{ { 0, 2 } },
6174+
},
6175+
{
6176+
"JNE signed compare, test 5",
6177+
.u.insns_int = {
6178+
BPF_LD_IMM64(R1, 0xfefb0000),
6179+
BPF_ALU32_IMM(BPF_MOV, R0, 1),
6180+
BPF_JMP_IMM(BPF_JNE, R1, 0xfefb0000, 1),
6181+
BPF_ALU32_IMM(BPF_MOV, R0, 2),
6182+
BPF_EXIT_INSN(),
6183+
},
6184+
INTERNAL,
6185+
{ },
6186+
{ { 0, 1 } },
6187+
},
6188+
{
6189+
"JNE signed compare, test 6",
6190+
.u.insns_int = {
6191+
BPF_LD_IMM64(R1, 0x7efb0000),
6192+
BPF_ALU32_IMM(BPF_MOV, R0, 1),
6193+
BPF_JMP_IMM(BPF_JNE, R1, 0x7efb0000, 1),
6194+
BPF_ALU32_IMM(BPF_MOV, R0, 2),
6195+
BPF_EXIT_INSN(),
6196+
},
6197+
INTERNAL,
6198+
{ },
6199+
{ { 0, 2 } },
6200+
},
6201+
{
6202+
"JNE signed compare, test 7",
6203+
.u.insns = {
6204+
BPF_STMT(BPF_LD | BPF_IMM, 0xffff0000),
6205+
BPF_STMT(BPF_MISC | BPF_TAX, 0),
6206+
BPF_STMT(BPF_LD | BPF_IMM, 0xfefbbc12),
6207+
BPF_STMT(BPF_ALU | BPF_AND | BPF_X, 0),
6208+
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xfefb0000, 1, 0),
6209+
BPF_STMT(BPF_RET | BPF_K, 1),
6210+
BPF_STMT(BPF_RET | BPF_K, 2),
6211+
},
6212+
CLASSIC | FLAG_NO_DATA,
6213+
{},
6214+
{ { 0, 2 } },
6215+
},
61126216
};
61136217

61146218
static struct net_device dev;

0 commit comments

Comments
 (0)