xref: /linux/tools/testing/selftests/bpf/verifier/dead_code.c (revision a4eb44a6435d6d8f9e642407a4a06f65eb90ca04)
1 {
2 	"dead code: start",
3 	.insns = {
4 	BPF_JMP_IMM(BPF_JA, 0, 0, 2),
5 	BPF_LDX_MEM(BPF_B, BPF_REG_8, BPF_REG_9, 0),
6 	BPF_JMP_IMM(BPF_JA, 0, 0, 2),
7 	BPF_MOV64_IMM(BPF_REG_0, 7),
8 	BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 10, -4),
9 	BPF_EXIT_INSN(),
10 	},
11 	.errstr_unpriv = "R9 !read_ok",
12 	.result_unpriv = REJECT,
13 	.result = ACCEPT,
14 	.retval = 7,
15 },
16 {
17 	"dead code: mid 1",
18 	.insns = {
19 	BPF_MOV64_IMM(BPF_REG_0, 7),
20 	BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 0, 1),
21 	BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 10, 0),
22 	BPF_EXIT_INSN(),
23 	},
24 	.result = ACCEPT,
25 	.retval = 7,
26 },
27 {
28 	"dead code: mid 2",
29 	.insns = {
30 	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_get_prandom_u32),
31 	BPF_JMP_IMM(BPF_JSET, BPF_REG_0, 1, 4),
32 	BPF_JMP_IMM(BPF_JSET, BPF_REG_0, 1, 1),
33 	BPF_JMP_IMM(BPF_JA, 0, 0, 2),
34 	BPF_MOV64_IMM(BPF_REG_0, 7),
35 	BPF_EXIT_INSN(),
36 	BPF_MOV64_IMM(BPF_REG_0, 1),
37 	BPF_EXIT_INSN(),
38 	},
39 	.result = ACCEPT,
40 	.retval = 1,
41 },
42 {
43 	"dead code: end 1",
44 	.insns = {
45 	BPF_MOV64_IMM(BPF_REG_0, 7),
46 	BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 10, 1),
47 	BPF_EXIT_INSN(),
48 	BPF_EXIT_INSN(),
49 	},
50 	.result = ACCEPT,
51 	.retval = 7,
52 },
53 {
54 	"dead code: end 2",
55 	.insns = {
56 	BPF_MOV64_IMM(BPF_REG_0, 7),
57 	BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 10, 1),
58 	BPF_EXIT_INSN(),
59 	BPF_MOV64_IMM(BPF_REG_0, 12),
60 	BPF_EXIT_INSN(),
61 	},
62 	.result = ACCEPT,
63 	.retval = 7,
64 },
65 {
66 	"dead code: end 3",
67 	.insns = {
68 	BPF_MOV64_IMM(BPF_REG_0, 7),
69 	BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 8, 1),
70 	BPF_EXIT_INSN(),
71 	BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 10, 1),
72 	BPF_JMP_IMM(BPF_JA, 0, 0, 1),
73 	BPF_MOV64_IMM(BPF_REG_0, 12),
74 	BPF_JMP_IMM(BPF_JA, 0, 0, -5),
75 	},
76 	.result = ACCEPT,
77 	.retval = 7,
78 },
79 {
80 	"dead code: tail of main + func",
81 	.insns = {
82 	BPF_MOV64_IMM(BPF_REG_0, 7),
83 	BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 8, 1),
84 	BPF_EXIT_INSN(),
85 	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
86 	BPF_EXIT_INSN(),
87 	BPF_MOV64_IMM(BPF_REG_0, 12),
88 	BPF_EXIT_INSN(),
89 	},
90 	.errstr_unpriv = "loading/calling other bpf or kernel functions are allowed for",
91 	.result_unpriv = REJECT,
92 	.result = ACCEPT,
93 	.retval = 7,
94 },
95 {
96 	"dead code: tail of main + two functions",
97 	.insns = {
98 	BPF_MOV64_IMM(BPF_REG_0, 7),
99 	BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 8, 1),
100 	BPF_EXIT_INSN(),
101 	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
102 	BPF_EXIT_INSN(),
103 	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
104 	BPF_EXIT_INSN(),
105 	BPF_MOV64_IMM(BPF_REG_0, 12),
106 	BPF_EXIT_INSN(),
107 	},
108 	.errstr_unpriv = "loading/calling other bpf or kernel functions are allowed for",
109 	.result_unpriv = REJECT,
110 	.result = ACCEPT,
111 	.retval = 7,
112 },
113 {
114 	"dead code: function in the middle and mid of another func",
115 	.insns = {
116 	BPF_MOV64_IMM(BPF_REG_1, 7),
117 	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 3),
118 	BPF_EXIT_INSN(),
119 	BPF_MOV64_IMM(BPF_REG_0, 12),
120 	BPF_EXIT_INSN(),
121 	BPF_MOV64_IMM(BPF_REG_0, 7),
122 	BPF_JMP_IMM(BPF_JGE, BPF_REG_1, 7, 1),
123 	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, -5),
124 	BPF_EXIT_INSN(),
125 	},
126 	.errstr_unpriv = "loading/calling other bpf or kernel functions are allowed for",
127 	.result_unpriv = REJECT,
128 	.result = ACCEPT,
129 	.retval = 7,
130 },
131 {
132 	"dead code: middle of main before call",
133 	.insns = {
134 	BPF_MOV64_IMM(BPF_REG_1, 2),
135 	BPF_JMP_IMM(BPF_JGE, BPF_REG_1, 2, 1),
136 	BPF_MOV64_IMM(BPF_REG_1, 5),
137 	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
138 	BPF_EXIT_INSN(),
139 	BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
140 	BPF_EXIT_INSN(),
141 	},
142 	.errstr_unpriv = "loading/calling other bpf or kernel functions are allowed for",
143 	.result_unpriv = REJECT,
144 	.result = ACCEPT,
145 	.retval = 2,
146 },
147 {
148 	"dead code: start of a function",
149 	.insns = {
150 	BPF_MOV64_IMM(BPF_REG_1, 2),
151 	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
152 	BPF_EXIT_INSN(),
153 	BPF_JMP_IMM(BPF_JA, 0, 0, 0),
154 	BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
155 	BPF_EXIT_INSN(),
156 	},
157 	.errstr_unpriv = "loading/calling other bpf or kernel functions are allowed for",
158 	.result_unpriv = REJECT,
159 	.result = ACCEPT,
160 	.retval = 2,
161 },
162 {
163 	"dead code: zero extension",
164 	.insns = {
165 	BPF_MOV64_IMM(BPF_REG_0, 0),
166 	BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4),
167 	BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 0, 1),
168 	BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_10, -4),
169 	BPF_EXIT_INSN(),
170 	},
171 	.result = ACCEPT,
172 	.retval = 0,
173 },
174