xref: /linux/tools/testing/selftests/bpf/verifier/atomic_xor.c (revision 8c994eff8fcfe8ecb1f1dbebed25b4d7bb75be12)
1 {
2 	"BPF_ATOMIC XOR without fetch",
3 	.insns = {
4 		/* val = 0x110; */
5 		BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0x110),
6 		/* atomic_xor(&val, 0x011); */
7 		BPF_MOV64_IMM(BPF_REG_1, 0x011),
8 		BPF_ATOMIC_OP(BPF_DW, BPF_XOR, BPF_REG_10, BPF_REG_1, -8),
9 		/* if (val != 0x101) exit(2); */
10 		BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8),
11 		BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0x101, 2),
12 		BPF_MOV64_IMM(BPF_REG_0, 2),
13 		BPF_EXIT_INSN(),
14 		/* r1 should not be clobbered, no BPF_FETCH flag */
15 		BPF_MOV64_IMM(BPF_REG_0, 0),
16 		BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0x011, 1),
17 		BPF_MOV64_IMM(BPF_REG_0, 1),
18 		BPF_EXIT_INSN(),
19 	},
20 	.result = ACCEPT,
21 },
22 {
23 	"BPF_ATOMIC XOR with fetch",
24 	.insns = {
25 		BPF_MOV64_IMM(BPF_REG_0, 123),
26 		/* val = 0x110; */
27 		BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0x110),
28 		/* old = atomic_fetch_xor(&val, 0x011); */
29 		BPF_MOV64_IMM(BPF_REG_1, 0x011),
30 		BPF_ATOMIC_OP(BPF_DW, BPF_XOR | BPF_FETCH, BPF_REG_10, BPF_REG_1, -8),
31 		/* if (old != 0x110) exit(3); */
32 		BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0x110, 2),
33 		BPF_MOV64_IMM(BPF_REG_0, 3),
34 		BPF_EXIT_INSN(),
35 		/* if (val != 0x101) exit(2); */
36 		BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_10, -8),
37 		BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0x101, 2),
38 		BPF_MOV64_IMM(BPF_REG_1, 2),
39 		BPF_EXIT_INSN(),
40 		/* Check R0 wasn't clobbered (fxor fear of x86 JIT bug) */
41 		BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 123, 2),
42 		BPF_MOV64_IMM(BPF_REG_0, 1),
43 		BPF_EXIT_INSN(),
44 		/* exit(0); */
45 		BPF_MOV64_IMM(BPF_REG_0, 0),
46 		BPF_EXIT_INSN(),
47 	},
48 	.result = ACCEPT,
49 },
50 {
51 	"BPF_ATOMIC XOR with fetch 32bit",
52 	.insns = {
53 		/* r0 = (s64) -1 */
54 		BPF_MOV64_IMM(BPF_REG_0, 0),
55 		BPF_ALU64_IMM(BPF_SUB, BPF_REG_0, 1),
56 		/* val = 0x110; */
57 		BPF_ST_MEM(BPF_W, BPF_REG_10, -4, 0x110),
58 		/* old = atomic_fetch_xor(&val, 0x011); */
59 		BPF_MOV32_IMM(BPF_REG_1, 0x011),
60 		BPF_ATOMIC_OP(BPF_W, BPF_XOR | BPF_FETCH, BPF_REG_10, BPF_REG_1, -4),
61 		/* if (old != 0x110) exit(3); */
62 		BPF_JMP32_IMM(BPF_JEQ, BPF_REG_1, 0x110, 2),
63 		BPF_MOV32_IMM(BPF_REG_0, 3),
64 		BPF_EXIT_INSN(),
65 		/* if (val != 0x101) exit(2); */
66 		BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_10, -4),
67 		BPF_JMP32_IMM(BPF_JEQ, BPF_REG_1, 0x101, 2),
68 		BPF_MOV32_IMM(BPF_REG_1, 2),
69 		BPF_EXIT_INSN(),
70 		/* Check R0 wasn't clobbered (fxor fear of x86 JIT bug)
71 		 * It should be -1 so add 1 to get exit code.
72 		 */
73 		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
74 		BPF_EXIT_INSN(),
75 	},
76 	.result = ACCEPT,
77 },
78