Commit aec08d67 authored by Yonghong Song's avatar Yonghong Song Committed by Alexei Starovoitov
Browse files

selftests/bpf: Add tests for non-constant cond_op NE/EQ bound deduction



Add various tests for code pattern '<non-const> NE/EQ <const>' implemented
in the previous verifier patch. Without the verifier patch, these new
tests will fail.

Signed-off-by: default avatarYonghong Song <yhs@fb.com>
Acked-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20230406164500.1045715-1-yhs@fb.com


Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 13fbcee5
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
#include "verifier_array_access.skel.h"
#include "verifier_basic_stack.skel.h"
#include "verifier_bounds_deduction.skel.h"
#include "verifier_bounds_deduction_non_const.skel.h"
#include "verifier_bounds_mix_sign_unsign.skel.h"
#include "verifier_cfg.skel.h"
#include "verifier_cgroup_inv_retcode.skel.h"
@@ -70,6 +71,7 @@ void test_verifier_and(void) { RUN(verifier_and); }
void test_verifier_array_access(void)         { RUN(verifier_array_access); }
void test_verifier_basic_stack(void)          { RUN(verifier_basic_stack); }
void test_verifier_bounds_deduction(void)     { RUN(verifier_bounds_deduction); }
void test_verifier_bounds_deduction_non_const(void)     { RUN(verifier_bounds_deduction_non_const); }
void test_verifier_bounds_mix_sign_unsign(void) { RUN(verifier_bounds_mix_sign_unsign); }
void test_verifier_cfg(void)                  { RUN(verifier_cfg); }
void test_verifier_cgroup_inv_retcode(void)   { RUN(verifier_cgroup_inv_retcode); }
+179 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0

#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"

SEC("socket")
__description("check deducing bounds from non-const, jmp64, <non_const> == <const>, 1")
__success __retval(0)
__naked void deducing_bounds_from_non_const_1(void)
{
	asm volatile ("					\
	call %[bpf_ktime_get_ns];			\
	if r0 < 3 goto l0_%=;				\
	r2 = 2;						\
	if r0 == r2 goto l1_%=;				\
l0_%=:							\
	r0 = 0;						\
	exit;						\
l1_%=:							\
	r0 -= r1;					\
	exit;						\
"	:
	: __imm(bpf_ktime_get_ns)
	: __clobber_all);
}

SEC("socket")
__description("check deducing bounds from non-const, jmp64, <non_const> == <const>, 2")
__success __retval(0)
__naked void deducing_bounds_from_non_const_2(void)
{
	asm volatile ("					\
	call %[bpf_ktime_get_ns];			\
	if r0 > 3 goto l0_%=;				\
	r2 = 4;						\
	if r0 == r2 goto l1_%=;				\
l0_%=:							\
	r0 = 0;						\
	exit;						\
l1_%=:							\
	r0 -= r1;					\
	exit;						\
"	:
	: __imm(bpf_ktime_get_ns)
	: __clobber_all);
}

SEC("socket")
__description("check deducing bounds from non-const, jmp64, <non_const> != <const>, 1")
__success __retval(0)
__naked void deducing_bounds_from_non_const_3(void)
{
	asm volatile ("					\
	call %[bpf_ktime_get_ns];			\
	if r0 < 3 goto l0_%=;				\
	r2 = 2;						\
	if r0 != r2 goto l0_%=;				\
	goto l1_%=;					\
l0_%=:							\
	r0 = 0;						\
	exit;						\
l1_%=:							\
	r0 -= r1;					\
	exit;						\
"	:
	: __imm(bpf_ktime_get_ns)
	: __clobber_all);
}

SEC("socket")
__description("check deducing bounds from non-const, jmp64, <non_const> != <const>, 2")
__success __retval(0)
__naked void deducing_bounds_from_non_const_4(void)
{
	asm volatile ("					\
	call %[bpf_ktime_get_ns];			\
	if r0 > 3 goto l0_%=;				\
	r2 = 4;						\
	if r0 != r2 goto l0_%=;				\
	goto l1_%=;					\
l0_%=:							\
	r0 = 0;						\
	exit;						\
l1_%=:							\
	r0 -= r1;					\
	exit;						\
"	:
	: __imm(bpf_ktime_get_ns)
	: __clobber_all);
}

SEC("socket")
__description("check deducing bounds from non-const, jmp32, <non_const> == <const>, 1")
__success __retval(0)
__naked void deducing_bounds_from_non_const_5(void)
{
	asm volatile ("					\
	call %[bpf_ktime_get_ns];			\
	if w0 < 4 goto l0_%=;				\
	w2 = 3;						\
	if w0 == w2 goto l1_%=;				\
l0_%=:							\
	r0 = 0;						\
	exit;						\
l1_%=:							\
	r0 -= r1;					\
	exit;						\
"	:
	: __imm(bpf_ktime_get_ns)
	: __clobber_all);
}

SEC("socket")
__description("check deducing bounds from non-const, jmp32, <non_const> == <const>, 2")
__success __retval(0)
__naked void deducing_bounds_from_non_const_6(void)
{
	asm volatile ("					\
	call %[bpf_ktime_get_ns];			\
	if w0 > 4 goto l0_%=;				\
	w2 = 5;						\
	if w0 == w2 goto l1_%=;				\
l0_%=:							\
	r0 = 0;						\
	exit;						\
l1_%=:							\
	r0 -= r1;					\
	exit;						\
"	:
	: __imm(bpf_ktime_get_ns)
	: __clobber_all);
}

SEC("socket")
__description("check deducing bounds from non-const, jmp32, <non_const> != <const>, 1")
__success __retval(0)
__naked void deducing_bounds_from_non_const_7(void)
{
	asm volatile ("					\
	call %[bpf_ktime_get_ns];			\
	if w0 < 3 goto l0_%=;				\
	w2 = 2;						\
	if w0 != w2 goto l0_%=;				\
	goto l1_%=;					\
l0_%=:							\
	r0 = 0;						\
	exit;						\
l1_%=:							\
	r0 -= r1;					\
	exit;						\
"	:
	: __imm(bpf_ktime_get_ns)
	: __clobber_all);
}

SEC("socket")
__description("check deducing bounds from non-const, jmp32, <non_const> != <const>, 2")
__success __retval(0)
__naked void deducing_bounds_from_non_const_8(void)
{
	asm volatile ("					\
	call %[bpf_ktime_get_ns];			\
	if w0 > 3 goto l0_%=;				\
	w2 = 4;						\
	if w0 != w2 goto l0_%=;				\
	goto l1_%=;					\
l0_%=:							\
	r0 = 0;						\
	exit;						\
l1_%=:							\
	r0 -= r1;					\
	exit;						\
"	:
	: __imm(bpf_ktime_get_ns)
	: __clobber_all);
}

char _license[] SEC("license") = "GPL";