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