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

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/verifier_subreg.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/progs/verifier_subreg.c
Extension
.c
Size
21944 bytes
Lines
994
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0
/* Converted from tools/testing/selftests/bpf/verifier/subreg.c */

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

/* This file contains sub-register zero extension checks for insns defining
 * sub-registers, meaning:
 *   - All insns under BPF_ALU class. Their BPF_ALU32 variants or narrow width
 *     forms (BPF_END) could define sub-registers.
 *   - Narrow direct loads, BPF_B/H/W | BPF_LDX.
 *   - BPF_LD is not exposed to JIT back-ends, so no need for testing.
 *
 * "get_prandom_u32" is used to initialize low 32-bit of some registers to
 * prevent potential optimizations done by verifier or JIT back-ends which could
 * optimize register back into constant when range info shows one register is a
 * constant.
 */

SEC("socket")
__description("add32 reg zero extend check")
__success __success_unpriv __retval(0)
__naked void add32_reg_zero_extend_check(void)
{
	asm volatile ("					\
	call %[bpf_get_prandom_u32];			\
	r1 = r0;					\
	r0 = 0x100000000 ll;				\
	w0 += w1;					\
	r0 >>= 32;					\
	exit;						\
"	:
	: __imm(bpf_get_prandom_u32)
	: __clobber_all);
}

SEC("socket")
__description("add32 imm zero extend check")
__success __success_unpriv __retval(0)
__naked void add32_imm_zero_extend_check(void)
{
	asm volatile ("					\
	call %[bpf_get_prandom_u32];			\
	r1 = 0x1000000000 ll;				\
	r0 |= r1;					\
	/* An insn could have no effect on the low 32-bit, for example:\
	 *   a = a + 0					\
	 *   a = a | 0					\
	 *   a = a & -1					\
	 * But, they should still zero high 32-bit.	\
	 */						\
	w0 += 0;					\
	r0 >>= 32;					\
	r6 = r0;					\
	call %[bpf_get_prandom_u32];			\
	r1 = 0x1000000000 ll;				\
	r0 |= r1;					\
	w0 += -2;					\
	r0 >>= 32;					\
	r0 |= r6;					\
	exit;						\
"	:
	: __imm(bpf_get_prandom_u32)
	: __clobber_all);
}

SEC("socket")
__description("sub32 reg zero extend check")
__success __success_unpriv __retval(0)
__naked void sub32_reg_zero_extend_check(void)
{
	asm volatile ("					\
	call %[bpf_get_prandom_u32];			\
	r1 = r0;					\
	r0 = 0x1ffffffff ll;				\
	w0 -= w1;					\
	r0 >>= 32;					\
	exit;						\
"	:
	: __imm(bpf_get_prandom_u32)
	: __clobber_all);
}

SEC("socket")
__description("sub32 imm zero extend check")
__success __success_unpriv __retval(0)
__naked void sub32_imm_zero_extend_check(void)
{
	asm volatile ("					\

Annotation

Implementation Notes