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

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/progs/verifier_gotox.c
Extension
.c
Size
10125 bytes
Lines
390
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
/* Copyright (c) 2025 Isovalent */

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

#if defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64) || defined(__TARGET_ARCH_powerpc)

#define DEFINE_SIMPLE_JUMP_TABLE_PROG(NAME, SRC_REG, OFF, IMM, OUTCOME)	\
									\
	SEC("socket")							\
	OUTCOME								\
	__naked void jump_table_ ## NAME(void)				\
	{								\
		asm volatile ("						\
		.pushsection .jumptables,\"\",@progbits;		\
	jt0_%=:								\
		.quad ret0_%= - socket;					\
		.quad ret1_%= - socket;					\
		.size jt0_%=, 16;					\
		.global jt0_%=;						\
		.popsection;						\
									\
		r0 = jt0_%= ll;						\
		r0 += 8;						\
		r0 = *(u64 *)(r0 + 0);					\
		.8byte %[gotox_r0];					\
		ret0_%=:						\
		r0 = 0;							\
		exit;							\
		ret1_%=:						\
		r0 = 1;							\
		exit;							\
	"	:							\
		: __imm_insn(gotox_r0, BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_0, (SRC_REG), (OFF) , (IMM))) \
		: __clobber_all);					\
	}

/*
 * The first program which doesn't use reserved fields
 * loads and works properly. The rest fail to load.
 */
DEFINE_SIMPLE_JUMP_TABLE_PROG(ok,                          BPF_REG_0, 0, 0, __success __retval(1))
DEFINE_SIMPLE_JUMP_TABLE_PROG(reserved_field_src_reg,      BPF_REG_1, 0, 0, __failure __msg("BPF_JA|BPF_X uses reserved fields"))
DEFINE_SIMPLE_JUMP_TABLE_PROG(reserved_field_non_zero_off, BPF_REG_0, 1, 0, __failure __msg("BPF_JA|BPF_X uses reserved fields"))
DEFINE_SIMPLE_JUMP_TABLE_PROG(reserved_field_non_zero_imm, BPF_REG_0, 0, 1, __failure __msg("BPF_JA|BPF_X uses reserved fields"))

/*
 * Gotox is forbidden when there is no jump table loaded
 * which points to the sub-function where the gotox is used
 */
SEC("socket")
__failure __msg("no jump tables found for subprog starting at 0")
__naked void jump_table_no_jump_table(void)
{
	asm volatile ("						\
	.8byte %[gotox_r0];					\
	r0 = 1;							\
	exit;							\
"	:							\
	: __imm_insn(gotox_r0, BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_0, 0, 0 , 0))
	: __clobber_all);
}

/*
 * Incorrect type of the target register, only PTR_TO_INSN allowed
 */
SEC("socket")
__failure __msg("R1 has type scalar, expected PTR_TO_INSN")
__naked void jump_table_incorrect_dst_reg_type(void)
{
	asm volatile ("						\
	.pushsection .jumptables,\"\",@progbits;		\
jt0_%=:								\
	.quad ret0_%= - socket;					\
	.quad ret1_%= - socket;					\
	.size jt0_%=, 16;					\
	.global jt0_%=;						\
	.popsection;						\
								\
	r0 = jt0_%= ll;						\
	r0 += 8;						\
	r0 = *(u64 *)(r0 + 0);					\
	r1 = 42;						\
	.8byte %[gotox_r1];					\
	ret0_%=:						\
	r0 = 0;							\
	exit;							\

Annotation

Implementation Notes