xref: /linux/tools/testing/selftests/bpf/verifier/jset.c (revision 26fbb4c8c7c3ee9a4c3b4de555a8587b5a19154e)
1 {
2 	"jset: functional",
3 	.insns = {
4 	BPF_DIRECT_PKT_R2,
5 	BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_2, 0),
6 
7 	/* reg, bit 63 or bit 0 set, taken */
8 	BPF_LD_IMM64(BPF_REG_8, 0x8000000000000001),
9 	BPF_JMP_REG(BPF_JSET, BPF_REG_7, BPF_REG_8, 1),
10 	BPF_EXIT_INSN(),
11 
12 	/* reg, bit 62, not taken */
13 	BPF_LD_IMM64(BPF_REG_8, 0x4000000000000000),
14 	BPF_JMP_REG(BPF_JSET, BPF_REG_7, BPF_REG_8, 1),
15 	BPF_JMP_IMM(BPF_JA, 0, 0, 1),
16 	BPF_EXIT_INSN(),
17 
18 	/* imm, any bit set, taken */
19 	BPF_JMP_IMM(BPF_JSET, BPF_REG_7, -1, 1),
20 	BPF_EXIT_INSN(),
21 
22 	/* imm, bit 31 set, taken */
23 	BPF_JMP_IMM(BPF_JSET, BPF_REG_7, 0x80000000, 1),
24 	BPF_EXIT_INSN(),
25 
26 	/* all good - return r0 == 2 */
27 	BPF_MOV64_IMM(BPF_REG_0, 2),
28 	BPF_EXIT_INSN(),
29 	},
30 	.prog_type = BPF_PROG_TYPE_SCHED_CLS,
31 	.result = ACCEPT,
32 	.runs = 7,
33 	.retvals = {
34 		{ .retval = 2,
35 		  .data64 = { (1ULL << 63) | (1U << 31) | (1U << 0), }
36 		},
37 		{ .retval = 2,
38 		  .data64 = { (1ULL << 63) | (1U << 31), }
39 		},
40 		{ .retval = 2,
41 		  .data64 = { (1ULL << 31) | (1U << 0), }
42 		},
43 		{ .retval = 2,
44 		  .data64 = { (__u32)-1, }
45 		},
46 		{ .retval = 2,
47 		  .data64 = { ~0x4000000000000000ULL, }
48 		},
49 		{ .retval = 0,
50 		  .data64 = { 0, }
51 		},
52 		{ .retval = 0,
53 		  .data64 = { ~0ULL, }
54 		},
55 	},
56 	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
57 },
58 {
59 	"jset: sign-extend",
60 	.insns = {
61 	BPF_DIRECT_PKT_R2,
62 	BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_2, 0),
63 
64 	BPF_JMP_IMM(BPF_JSET, BPF_REG_7, 0x80000000, 1),
65 	BPF_EXIT_INSN(),
66 
67 	BPF_MOV64_IMM(BPF_REG_0, 2),
68 	BPF_EXIT_INSN(),
69 	},
70 	.prog_type = BPF_PROG_TYPE_SCHED_CLS,
71 	.result = ACCEPT,
72 	.retval = 2,
73 	.data = { 1, 0, 0, 0, 0, 0, 0, 1, },
74 	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
75 },
76 {
77 	"jset: known const compare",
78 	.insns = {
79 	BPF_MOV64_IMM(BPF_REG_0, 1),
80 	BPF_JMP_IMM(BPF_JSET, BPF_REG_0, 1, 1),
81 	BPF_LDX_MEM(BPF_B, BPF_REG_8, BPF_REG_9, 0),
82 	BPF_EXIT_INSN(),
83 	},
84 	.prog_type = BPF_PROG_TYPE_SOCKET_FILTER,
85 	.retval_unpriv = 1,
86 	.result_unpriv = ACCEPT,
87 	.retval = 1,
88 	.result = ACCEPT,
89 },
90 {
91 	"jset: known const compare bad",
92 	.insns = {
93 	BPF_MOV64_IMM(BPF_REG_0, 0),
94 	BPF_JMP_IMM(BPF_JSET, BPF_REG_0, 1, 1),
95 	BPF_LDX_MEM(BPF_B, BPF_REG_8, BPF_REG_9, 0),
96 	BPF_EXIT_INSN(),
97 	},
98 	.prog_type = BPF_PROG_TYPE_SOCKET_FILTER,
99 	.errstr_unpriv = "!read_ok",
100 	.result_unpriv = REJECT,
101 	.errstr = "!read_ok",
102 	.result = REJECT,
103 },
104 {
105 	"jset: unknown const compare taken",
106 	.insns = {
107 	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_get_prandom_u32),
108 	BPF_JMP_IMM(BPF_JSET, BPF_REG_0, 1, 1),
109 	BPF_JMP_IMM(BPF_JA, 0, 0, 1),
110 	BPF_LDX_MEM(BPF_B, BPF_REG_8, BPF_REG_9, 0),
111 	BPF_EXIT_INSN(),
112 	},
113 	.prog_type = BPF_PROG_TYPE_SOCKET_FILTER,
114 	.errstr_unpriv = "!read_ok",
115 	.result_unpriv = REJECT,
116 	.errstr = "!read_ok",
117 	.result = REJECT,
118 },
119 {
120 	"jset: unknown const compare not taken",
121 	.insns = {
122 	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_get_prandom_u32),
123 	BPF_JMP_IMM(BPF_JSET, BPF_REG_0, 1, 1),
124 	BPF_LDX_MEM(BPF_B, BPF_REG_8, BPF_REG_9, 0),
125 	BPF_EXIT_INSN(),
126 	},
127 	.prog_type = BPF_PROG_TYPE_SOCKET_FILTER,
128 	.errstr_unpriv = "!read_ok",
129 	.result_unpriv = REJECT,
130 	.errstr = "!read_ok",
131 	.result = REJECT,
132 },
133 {
134 	"jset: half-known const compare",
135 	.insns = {
136 	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_get_prandom_u32),
137 	BPF_ALU64_IMM(BPF_OR, BPF_REG_0, 2),
138 	BPF_JMP_IMM(BPF_JSET, BPF_REG_0, 3, 1),
139 	BPF_LDX_MEM(BPF_B, BPF_REG_8, BPF_REG_9, 0),
140 	BPF_MOV64_IMM(BPF_REG_0, 0),
141 	BPF_EXIT_INSN(),
142 	},
143 	.prog_type = BPF_PROG_TYPE_SOCKET_FILTER,
144 	.result_unpriv = ACCEPT,
145 	.result = ACCEPT,
146 },
147 {
148 	"jset: range",
149 	.insns = {
150 	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_get_prandom_u32),
151 	BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
152 	BPF_MOV64_IMM(BPF_REG_0, 0),
153 	BPF_ALU64_IMM(BPF_AND, BPF_REG_1, 0xff),
154 	BPF_JMP_IMM(BPF_JSET, BPF_REG_1, 0xf0, 3),
155 	BPF_JMP_IMM(BPF_JLT, BPF_REG_1, 0x10, 1),
156 	BPF_LDX_MEM(BPF_B, BPF_REG_8, BPF_REG_9, 0),
157 	BPF_EXIT_INSN(),
158 	BPF_JMP_IMM(BPF_JSET, BPF_REG_1, 0x10, 1),
159 	BPF_EXIT_INSN(),
160 	BPF_JMP_IMM(BPF_JGE, BPF_REG_1, 0x10, 1),
161 	BPF_LDX_MEM(BPF_B, BPF_REG_8, BPF_REG_9, 0),
162 	BPF_EXIT_INSN(),
163 	},
164 	.prog_type = BPF_PROG_TYPE_SOCKET_FILTER,
165 	.result_unpriv = ACCEPT,
166 	.result = ACCEPT,
167 },
168