xref: /linux/tools/testing/selftests/bpf/verifier/jset.c (revision a4eb44a6435d6d8f9e642407a4a06f65eb90ca04)
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 	.errstr_unpriv = "R9 !read_ok",
86 	.result_unpriv = REJECT,
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 	.errstr_unpriv = "R9 !read_ok",
145 	.result_unpriv = REJECT,
146 	.result = ACCEPT,
147 },
148 {
149 	"jset: range",
150 	.insns = {
151 	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_get_prandom_u32),
152 	BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
153 	BPF_MOV64_IMM(BPF_REG_0, 0),
154 	BPF_ALU64_IMM(BPF_AND, BPF_REG_1, 0xff),
155 	BPF_JMP_IMM(BPF_JSET, BPF_REG_1, 0xf0, 3),
156 	BPF_JMP_IMM(BPF_JLT, BPF_REG_1, 0x10, 1),
157 	BPF_LDX_MEM(BPF_B, BPF_REG_8, BPF_REG_9, 0),
158 	BPF_EXIT_INSN(),
159 	BPF_JMP_IMM(BPF_JSET, BPF_REG_1, 0x10, 1),
160 	BPF_EXIT_INSN(),
161 	BPF_JMP_IMM(BPF_JGE, BPF_REG_1, 0x10, 1),
162 	BPF_LDX_MEM(BPF_B, BPF_REG_8, BPF_REG_9, 0),
163 	BPF_EXIT_INSN(),
164 	},
165 	.prog_type = BPF_PROG_TYPE_SOCKET_FILTER,
166 	.errstr_unpriv = "R9 !read_ok",
167 	.result_unpriv = REJECT,
168 	.result = ACCEPT,
169 },
170