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

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/progs/verifier_div_mod_bounds.c
Extension
.c
Size
28794 bytes
Lines
1152
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

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

/* This file contains unit tests for signed/unsigned division and modulo
 * operations (with divisor as a constant), focusing on verifying whether
 * BPF verifier's range tracking module soundly and precisely computes
 * the results.
 */

SEC("socket")
__description("UDIV32, positive divisor")
__success __retval(0) __log_level(2)
__msg("w1 /= 3 {{.*}}; R1=scalar(smin=smin32=0,smax=umax=smax32=umax32=3,var_off=(0x0; 0x3))")
__naked void udiv32_pos_divisor(void)
{
	asm volatile ("					\
	call %[bpf_get_prandom_u32];			\
	w1 = w0;					\
	w1 &= 8;					\
	w1 |= 1;					\
	w1 /= 3;					\
	if w1 > 3 goto l0_%=;				\
	r0 = 0;						\
	exit;						\
l0_%=:	r0 = *(u64 *)(r1 + 0);				\
	exit;						\
"	:
	: __imm(bpf_get_prandom_u32)
	: __clobber_all);
}

SEC("socket")
__description("UDIV32, zero divisor")
__success __retval(0) __log_level(2)
__msg("w1 /= w2 {{.*}}; R1=0")
__naked void udiv32_zero_divisor(void)
{
	asm volatile ("					\
	call %[bpf_get_prandom_u32];			\
	w1 = w0;					\
	w1 &= 8;					\
	w1 |= 1;					\
	w2 = 0;						\
	w1 /= w2;					\
	if w1 != 0 goto l0_%=;				\
	r0 = 0;						\
	exit;						\
l0_%=:	r0 = *(u64 *)(r1 + 0);				\
	exit;						\
"	:
	: __imm(bpf_get_prandom_u32)
	: __clobber_all);
}

SEC("socket")
__description("UDIV64, positive divisor")
__success __retval(0) __log_level(2)
__msg("r1 /= 3 {{.*}}; R1=scalar(smin=smin32=0,smax=umax=smax32=umax32=3,var_off=(0x0; 0x3))")
__naked void udiv64_pos_divisor(void)
{
	asm volatile ("					\
	call %[bpf_get_prandom_u32];			\
	r1 = r0;					\
	r1 &= 8;					\
	r1 |= 1;					\
	r1 /= 3;					\
	if r1 > 3 goto l0_%=;				\
	r0 = 0;						\
	exit;						\
l0_%=:	r0 = *(u64 *)(r1 + 0);				\
	exit;						\
"	:
	: __imm(bpf_get_prandom_u32)
	: __clobber_all);
}

SEC("socket")
__description("UDIV64, zero divisor")
__success __retval(0) __log_level(2)
__msg("r1 /= r2 {{.*}}; R1=0")
__naked void udiv64_zero_divisor(void)
{
	asm volatile ("					\
	call %[bpf_get_prandom_u32];			\
	r1 = r0;					\
	r1 &= 8;					\

Annotation

Implementation Notes