bpf/tests: Add JMP tests with degenerate conditional

This patch adds a set of tests for JMP and JMP32 operations where the
branch decision is know at JIT time. Mainly testing JIT behaviour.

Signed-off-by: Johan Almbladh <johan.almbladh@anyfinetworks.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20210914091842.4186267-11-johan.almbladh@anyfinetworks.com
This commit is contained in:
Johan Almbladh 2021-09-14 11:18:38 +02:00 committed by Daniel Borkmann
parent d4ff9ee2dc
commit c4df4559db

View File

@ -10709,6 +10709,235 @@ static struct bpf_test tests[] = {
.fill_helper = bpf_fill_jmp32_jsle_reg,
.nr_testruns = NR_PATTERN_RUNS,
},
/* Conditional jumps with constant decision */
{
"JMP_JSET_K: imm = 0 -> never taken",
.u.insns_int = {
BPF_ALU64_IMM(BPF_MOV, R0, 1),
BPF_JMP_IMM(BPF_JSET, R1, 0, 1),
BPF_ALU64_IMM(BPF_MOV, R0, 0),
BPF_EXIT_INSN(),
},
INTERNAL | FLAG_NO_DATA,
{ },
{ { 0, 0 } },
},
{
"JMP_JLT_K: imm = 0 -> never taken",
.u.insns_int = {
BPF_ALU64_IMM(BPF_MOV, R0, 1),
BPF_JMP_IMM(BPF_JLT, R1, 0, 1),
BPF_ALU64_IMM(BPF_MOV, R0, 0),
BPF_EXIT_INSN(),
},
INTERNAL | FLAG_NO_DATA,
{ },
{ { 0, 0 } },
},
{
"JMP_JGE_K: imm = 0 -> always taken",
.u.insns_int = {
BPF_ALU64_IMM(BPF_MOV, R0, 1),
BPF_JMP_IMM(BPF_JGE, R1, 0, 1),
BPF_ALU64_IMM(BPF_MOV, R0, 0),
BPF_EXIT_INSN(),
},
INTERNAL | FLAG_NO_DATA,
{ },
{ { 0, 1 } },
},
{
"JMP_JGT_K: imm = 0xffffffff -> never taken",
.u.insns_int = {
BPF_ALU64_IMM(BPF_MOV, R0, 1),
BPF_JMP_IMM(BPF_JGT, R1, U32_MAX, 1),
BPF_ALU64_IMM(BPF_MOV, R0, 0),
BPF_EXIT_INSN(),
},
INTERNAL | FLAG_NO_DATA,
{ },
{ { 0, 0 } },
},
{
"JMP_JLE_K: imm = 0xffffffff -> always taken",
.u.insns_int = {
BPF_ALU64_IMM(BPF_MOV, R0, 1),
BPF_JMP_IMM(BPF_JLE, R1, U32_MAX, 1),
BPF_ALU64_IMM(BPF_MOV, R0, 0),
BPF_EXIT_INSN(),
},
INTERNAL | FLAG_NO_DATA,
{ },
{ { 0, 1 } },
},
{
"JMP32_JSGT_K: imm = 0x7fffffff -> never taken",
.u.insns_int = {
BPF_ALU64_IMM(BPF_MOV, R0, 1),
BPF_JMP32_IMM(BPF_JSGT, R1, S32_MAX, 1),
BPF_ALU64_IMM(BPF_MOV, R0, 0),
BPF_EXIT_INSN(),
},
INTERNAL | FLAG_NO_DATA,
{ },
{ { 0, 0 } },
},
{
"JMP32_JSGE_K: imm = -0x80000000 -> always taken",
.u.insns_int = {
BPF_ALU64_IMM(BPF_MOV, R0, 1),
BPF_JMP32_IMM(BPF_JSGE, R1, S32_MIN, 1),
BPF_ALU64_IMM(BPF_MOV, R0, 0),
BPF_EXIT_INSN(),
},
INTERNAL | FLAG_NO_DATA,
{ },
{ { 0, 1 } },
},
{
"JMP32_JSLT_K: imm = -0x80000000 -> never taken",
.u.insns_int = {
BPF_ALU64_IMM(BPF_MOV, R0, 1),
BPF_JMP32_IMM(BPF_JSLT, R1, S32_MIN, 1),
BPF_ALU64_IMM(BPF_MOV, R0, 0),
BPF_EXIT_INSN(),
},
INTERNAL | FLAG_NO_DATA,
{ },
{ { 0, 0 } },
},
{
"JMP32_JSLE_K: imm = 0x7fffffff -> always taken",
.u.insns_int = {
BPF_ALU64_IMM(BPF_MOV, R0, 1),
BPF_JMP32_IMM(BPF_JSLE, R1, S32_MAX, 1),
BPF_ALU64_IMM(BPF_MOV, R0, 0),
BPF_EXIT_INSN(),
},
INTERNAL | FLAG_NO_DATA,
{ },
{ { 0, 1 } },
},
{
"JMP_JEQ_X: dst = src -> always taken",
.u.insns_int = {
BPF_ALU64_IMM(BPF_MOV, R0, 1),
BPF_JMP_REG(BPF_JEQ, R1, R1, 1),
BPF_ALU64_IMM(BPF_MOV, R0, 0),
BPF_EXIT_INSN(),
},
INTERNAL | FLAG_NO_DATA,
{ },
{ { 0, 1 } },
},
{
"JMP_JGE_X: dst = src -> always taken",
.u.insns_int = {
BPF_ALU64_IMM(BPF_MOV, R0, 1),
BPF_JMP_REG(BPF_JGE, R1, R1, 1),
BPF_ALU64_IMM(BPF_MOV, R0, 0),
BPF_EXIT_INSN(),
},
INTERNAL | FLAG_NO_DATA,
{ },
{ { 0, 1 } },
},
{
"JMP_JLE_X: dst = src -> always taken",
.u.insns_int = {
BPF_ALU64_IMM(BPF_MOV, R0, 1),
BPF_JMP_REG(BPF_JLE, R1, R1, 1),
BPF_ALU64_IMM(BPF_MOV, R0, 0),
BPF_EXIT_INSN(),
},
INTERNAL | FLAG_NO_DATA,
{ },
{ { 0, 1 } },
},
{
"JMP_JSGE_X: dst = src -> always taken",
.u.insns_int = {
BPF_ALU64_IMM(BPF_MOV, R0, 1),
BPF_JMP_REG(BPF_JSGE, R1, R1, 1),
BPF_ALU64_IMM(BPF_MOV, R0, 0),
BPF_EXIT_INSN(),
},
INTERNAL | FLAG_NO_DATA,
{ },
{ { 0, 1 } },
},
{
"JMP_JSLE_X: dst = src -> always taken",
.u.insns_int = {
BPF_ALU64_IMM(BPF_MOV, R0, 1),
BPF_JMP_REG(BPF_JSLE, R1, R1, 1),
BPF_ALU64_IMM(BPF_MOV, R0, 0),
BPF_EXIT_INSN(),
},
INTERNAL | FLAG_NO_DATA,
{ },
{ { 0, 1 } },
},
{
"JMP_JNE_X: dst = src -> never taken",
.u.insns_int = {
BPF_ALU64_IMM(BPF_MOV, R0, 1),
BPF_JMP_REG(BPF_JNE, R1, R1, 1),
BPF_ALU64_IMM(BPF_MOV, R0, 0),
BPF_EXIT_INSN(),
},
INTERNAL | FLAG_NO_DATA,
{ },
{ { 0, 0 } },
},
{
"JMP_JGT_X: dst = src -> never taken",
.u.insns_int = {
BPF_ALU64_IMM(BPF_MOV, R0, 1),
BPF_JMP_REG(BPF_JGT, R1, R1, 1),
BPF_ALU64_IMM(BPF_MOV, R0, 0),
BPF_EXIT_INSN(),
},
INTERNAL | FLAG_NO_DATA,
{ },
{ { 0, 0 } },
},
{
"JMP_JLT_X: dst = src -> never taken",
.u.insns_int = {
BPF_ALU64_IMM(BPF_MOV, R0, 1),
BPF_JMP_REG(BPF_JLT, R1, R1, 1),
BPF_ALU64_IMM(BPF_MOV, R0, 0),
BPF_EXIT_INSN(),
},
INTERNAL | FLAG_NO_DATA,
{ },
{ { 0, 0 } },
},
{
"JMP_JSGT_X: dst = src -> never taken",
.u.insns_int = {
BPF_ALU64_IMM(BPF_MOV, R0, 1),
BPF_JMP_REG(BPF_JSGT, R1, R1, 1),
BPF_ALU64_IMM(BPF_MOV, R0, 0),
BPF_EXIT_INSN(),
},
INTERNAL | FLAG_NO_DATA,
{ },
{ { 0, 0 } },
},
{
"JMP_JSLT_X: dst = src -> never taken",
.u.insns_int = {
BPF_ALU64_IMM(BPF_MOV, R0, 1),
BPF_JMP_REG(BPF_JSLT, R1, R1, 1),
BPF_ALU64_IMM(BPF_MOV, R0, 0),
BPF_EXIT_INSN(),
},
INTERNAL | FLAG_NO_DATA,
{ },
{ { 0, 0 } },
},
/* Short relative jumps */
{
"Short relative jump: offset=0",