xref: /linux/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c (revision d0d106a2bd21499901299160744e5fe9f4c83ddb)
1*14a627feSYonghong Song // SPDX-License-Identifier: GPL-2.0
2*14a627feSYonghong Song /* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3*14a627feSYonghong Song 
4*14a627feSYonghong Song #include <linux/bpf.h>
5*14a627feSYonghong Song #include <bpf/bpf_helpers.h>
6*14a627feSYonghong Song #include "../../../include/linux/filter.h"
7*14a627feSYonghong Song #include "bpf_misc.h"
8*14a627feSYonghong Song 
9*14a627feSYonghong Song SEC("raw_tp")
10*14a627feSYonghong Song __description("may_goto 0")
11*14a627feSYonghong Song __arch_x86_64
12*14a627feSYonghong Song __xlated("0: r0 = 1")
13*14a627feSYonghong Song __xlated("1: exit")
14*14a627feSYonghong Song __success
may_goto_simple(void)15*14a627feSYonghong Song __naked void may_goto_simple(void)
16*14a627feSYonghong Song {
17*14a627feSYonghong Song 	asm volatile (
18*14a627feSYonghong Song 	".8byte %[may_goto];"
19*14a627feSYonghong Song 	"r0 = 1;"
20*14a627feSYonghong Song 	".8byte %[may_goto];"
21*14a627feSYonghong Song 	"exit;"
22*14a627feSYonghong Song 	:
23*14a627feSYonghong Song 	: __imm_insn(may_goto, BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 0 /* offset */, 0))
24*14a627feSYonghong Song 	: __clobber_all);
25*14a627feSYonghong Song }
26*14a627feSYonghong Song 
27*14a627feSYonghong Song SEC("raw_tp")
28*14a627feSYonghong Song __description("batch 2 of may_goto 0")
29*14a627feSYonghong Song __arch_x86_64
30*14a627feSYonghong Song __xlated("0: r0 = 1")
31*14a627feSYonghong Song __xlated("1: exit")
32*14a627feSYonghong Song __success
may_goto_batch_0(void)33*14a627feSYonghong Song __naked void may_goto_batch_0(void)
34*14a627feSYonghong Song {
35*14a627feSYonghong Song 	asm volatile (
36*14a627feSYonghong Song 	".8byte %[may_goto1];"
37*14a627feSYonghong Song 	".8byte %[may_goto1];"
38*14a627feSYonghong Song 	"r0 = 1;"
39*14a627feSYonghong Song 	".8byte %[may_goto1];"
40*14a627feSYonghong Song 	".8byte %[may_goto1];"
41*14a627feSYonghong Song 	"exit;"
42*14a627feSYonghong Song 	:
43*14a627feSYonghong Song 	: __imm_insn(may_goto1, BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 0 /* offset */, 0))
44*14a627feSYonghong Song 	: __clobber_all);
45*14a627feSYonghong Song }
46*14a627feSYonghong Song 
47*14a627feSYonghong Song SEC("raw_tp")
48*14a627feSYonghong Song __description("may_goto batch with offsets 2/1/0")
49*14a627feSYonghong Song __arch_x86_64
50*14a627feSYonghong Song __xlated("0: r0 = 1")
51*14a627feSYonghong Song __xlated("1: exit")
52*14a627feSYonghong Song __success
may_goto_batch_1(void)53*14a627feSYonghong Song __naked void may_goto_batch_1(void)
54*14a627feSYonghong Song {
55*14a627feSYonghong Song 	asm volatile (
56*14a627feSYonghong Song 	".8byte %[may_goto1];"
57*14a627feSYonghong Song 	".8byte %[may_goto2];"
58*14a627feSYonghong Song 	".8byte %[may_goto3];"
59*14a627feSYonghong Song 	"r0 = 1;"
60*14a627feSYonghong Song 	".8byte %[may_goto1];"
61*14a627feSYonghong Song 	".8byte %[may_goto2];"
62*14a627feSYonghong Song 	".8byte %[may_goto3];"
63*14a627feSYonghong Song 	"exit;"
64*14a627feSYonghong Song 	:
65*14a627feSYonghong Song 	: __imm_insn(may_goto1, BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 2 /* offset */, 0)),
66*14a627feSYonghong Song 	  __imm_insn(may_goto2, BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 1 /* offset */, 0)),
67*14a627feSYonghong Song 	  __imm_insn(may_goto3, BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 0 /* offset */, 0))
68*14a627feSYonghong Song 	: __clobber_all);
69*14a627feSYonghong Song }
70*14a627feSYonghong Song 
71*14a627feSYonghong Song SEC("raw_tp")
72*14a627feSYonghong Song __description("may_goto batch with offsets 2/0")
73*14a627feSYonghong Song __arch_x86_64
74*14a627feSYonghong Song __xlated("0: *(u64 *)(r10 -8) = 8388608")
75*14a627feSYonghong Song __xlated("1: r11 = *(u64 *)(r10 -8)")
76*14a627feSYonghong Song __xlated("2: if r11 == 0x0 goto pc+3")
77*14a627feSYonghong Song __xlated("3: r11 -= 1")
78*14a627feSYonghong Song __xlated("4: *(u64 *)(r10 -8) = r11")
79*14a627feSYonghong Song __xlated("5: r0 = 1")
80*14a627feSYonghong Song __xlated("6: r0 = 2")
81*14a627feSYonghong Song __xlated("7: exit")
82*14a627feSYonghong Song __success
may_goto_batch_2(void)83*14a627feSYonghong Song __naked void may_goto_batch_2(void)
84*14a627feSYonghong Song {
85*14a627feSYonghong Song 	asm volatile (
86*14a627feSYonghong Song 	".8byte %[may_goto1];"
87*14a627feSYonghong Song 	".8byte %[may_goto3];"
88*14a627feSYonghong Song 	"r0 = 1;"
89*14a627feSYonghong Song 	"r0 = 2;"
90*14a627feSYonghong Song 	"exit;"
91*14a627feSYonghong Song 	:
92*14a627feSYonghong Song 	: __imm_insn(may_goto1, BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 2 /* offset */, 0)),
93*14a627feSYonghong Song 	  __imm_insn(may_goto3, BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 0 /* offset */, 0))
94*14a627feSYonghong Song 	: __clobber_all);
95*14a627feSYonghong Song }
96*14a627feSYonghong Song 
97*14a627feSYonghong Song char _license[] SEC("license") = "GPL";
98