OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sandbox/linux/seccomp-bpf/codegen.h" | 5 #include "sandbox/linux/seccomp-bpf/codegen.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <linux/filter.h> | 8 #include <linux/filter.h> |
9 | 9 |
10 #include <set> | 10 #include <set> |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 Instruction* ret = codegen->MakeInstruction( | 77 Instruction* ret = codegen->MakeInstruction( |
78 BPF_RET + BPF_K, 0); | 78 BPF_RET + BPF_K, 0); |
79 return codegen->MakeInstruction(BPF_JMP + BPF_JEQ + BPF_K, 42, ret, ret); | 79 return codegen->MakeInstruction(BPF_JMP + BPF_JEQ + BPF_K, 42, ret, ret); |
80 } | 80 } |
81 | 81 |
82 Instruction* SampleProgramComplex(CodeGen* codegen, int* flags) { | 82 Instruction* SampleProgramComplex(CodeGen* codegen, int* flags) { |
83 // Creates a basic BPF program that we'll use to test some of the code: | 83 // Creates a basic BPF program that we'll use to test some of the code: |
84 // JUMP if eq 42 the $0 else $1 (insn6) | 84 // JUMP if eq 42 the $0 else $1 (insn6) |
85 // 0: LD 23 (insn5) | 85 // 0: LD 23 (insn5) |
86 // 1: JUMP if eq 42 then $2 else $4 (insn4) | 86 // 1: JUMP if eq 42 then $2 else $4 (insn4) |
87 // 2: JUMP to $3 (insn1) | 87 // 2: JUMP to $3 (insn2) |
88 // 3: LD 42 (insn0) | 88 // 3: LD 42 (insn1) |
89 // RET 42 (insn2) | 89 // RET 42 (insn0) |
90 // 4: LD 42 (insn3) | 90 // 4: LD 42 (insn3) |
91 // RET 42 (insn3+) | 91 // RET 42 (insn3+) |
92 *flags = HAS_MERGEABLE_TAILS; | 92 *flags = HAS_MERGEABLE_TAILS; |
93 | 93 |
94 Instruction* insn0 = codegen->MakeInstruction(BPF_LD + BPF_W + BPF_ABS, 42); | 94 Instruction* insn0 = codegen->MakeInstruction(BPF_RET + BPF_K, 42); |
95 SANDBOX_ASSERT(insn0); | 95 SANDBOX_ASSERT(insn0); |
96 SANDBOX_ASSERT(insn0->code == BPF_LD + BPF_W + BPF_ABS); | 96 SANDBOX_ASSERT(insn0->code == BPF_RET + BPF_K); |
97 SANDBOX_ASSERT(insn0->k == 42); | |
98 SANDBOX_ASSERT(insn0->next == NULL); | 97 SANDBOX_ASSERT(insn0->next == NULL); |
99 | 98 |
100 Instruction* insn1 = codegen->MakeInstruction(BPF_JMP + BPF_JA, 0, insn0); | 99 Instruction* insn1 = |
| 100 codegen->MakeInstruction(BPF_LD + BPF_W + BPF_ABS, 42, insn0); |
101 SANDBOX_ASSERT(insn1); | 101 SANDBOX_ASSERT(insn1); |
102 SANDBOX_ASSERT(insn1->code == BPF_JMP + BPF_JA); | 102 SANDBOX_ASSERT(insn1->code == BPF_LD + BPF_W + BPF_ABS); |
103 SANDBOX_ASSERT(insn1->jt_ptr == insn0); | 103 SANDBOX_ASSERT(insn1->k == 42); |
| 104 SANDBOX_ASSERT(insn1->next == insn0); |
104 | 105 |
105 Instruction* insn2 = codegen->MakeInstruction(BPF_RET + BPF_K, 42); | 106 Instruction* insn2 = codegen->MakeInstruction(BPF_JMP + BPF_JA, 0, insn1); |
106 SANDBOX_ASSERT(insn2); | 107 SANDBOX_ASSERT(insn2); |
107 SANDBOX_ASSERT(insn2->code == BPF_RET + BPF_K); | 108 SANDBOX_ASSERT(insn2->code == BPF_JMP + BPF_JA); |
108 SANDBOX_ASSERT(insn2->next == NULL); | 109 SANDBOX_ASSERT(insn2->jt_ptr == insn1); |
109 | 110 |
110 // We explicitly duplicate instructions so that MergeTails() can coalesce | 111 // We explicitly duplicate instructions so that MergeTails() can coalesce |
111 // them later. | 112 // them later. |
112 Instruction* insn3 = codegen->MakeInstruction( | 113 Instruction* insn3 = codegen->MakeInstruction( |
113 BPF_LD + BPF_W + BPF_ABS, | 114 BPF_LD + BPF_W + BPF_ABS, |
114 42, | 115 42, |
115 codegen->MakeInstruction(BPF_RET + BPF_K, 42)); | 116 codegen->MakeInstruction(BPF_RET + BPF_K, 42)); |
116 | 117 |
117 Instruction* insn4 = | 118 Instruction* insn4 = |
118 codegen->MakeInstruction(BPF_JMP + BPF_JEQ + BPF_K, 42, insn1, insn3); | 119 codegen->MakeInstruction(BPF_JMP + BPF_JEQ + BPF_K, 42, insn2, insn3); |
119 SANDBOX_ASSERT(insn4); | 120 SANDBOX_ASSERT(insn4); |
120 SANDBOX_ASSERT(insn4->code == BPF_JMP + BPF_JEQ + BPF_K); | 121 SANDBOX_ASSERT(insn4->code == BPF_JMP + BPF_JEQ + BPF_K); |
121 SANDBOX_ASSERT(insn4->k == 42); | 122 SANDBOX_ASSERT(insn4->k == 42); |
122 SANDBOX_ASSERT(insn4->jt_ptr == insn1); | 123 SANDBOX_ASSERT(insn4->jt_ptr == insn2); |
123 SANDBOX_ASSERT(insn4->jf_ptr == insn3); | 124 SANDBOX_ASSERT(insn4->jf_ptr == insn3); |
124 | 125 |
125 codegen->JoinInstructions(insn0, insn2); | |
126 SANDBOX_ASSERT(insn0->next == insn2); | |
127 | |
128 Instruction* insn5 = | 126 Instruction* insn5 = |
129 codegen->MakeInstruction(BPF_LD + BPF_W + BPF_ABS, 23, insn4); | 127 codegen->MakeInstruction(BPF_LD + BPF_W + BPF_ABS, 23, insn4); |
130 SANDBOX_ASSERT(insn5); | 128 SANDBOX_ASSERT(insn5); |
131 SANDBOX_ASSERT(insn5->code == BPF_LD + BPF_W + BPF_ABS); | 129 SANDBOX_ASSERT(insn5->code == BPF_LD + BPF_W + BPF_ABS); |
132 SANDBOX_ASSERT(insn5->k == 23); | 130 SANDBOX_ASSERT(insn5->k == 23); |
133 SANDBOX_ASSERT(insn5->next == insn4); | 131 SANDBOX_ASSERT(insn5->next == insn4); |
134 | 132 |
135 // Force a basic block that ends in neither a jump instruction nor a return | 133 // Force a basic block that ends in neither a jump instruction nor a return |
136 // instruction. It only contains "insn5". This exercises one of the less | 134 // instruction. It only contains "insn5". This exercises one of the less |
137 // common code paths in the topo-sort algorithm. | 135 // common code paths in the topo-sort algorithm. |
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 assembly.append(reinterpret_cast<char*>(&insn.k), sizeof(insn.k)); | 530 assembly.append(reinterpret_cast<char*>(&insn.k), sizeof(insn.k)); |
533 } | 531 } |
534 SANDBOX_ASSERT(source == assembly); | 532 SANDBOX_ASSERT(source == assembly); |
535 } | 533 } |
536 | 534 |
537 SANDBOX_TEST(CodeGen, All) { | 535 SANDBOX_TEST(CodeGen, All) { |
538 ForAllPrograms(CompileAndCompare); | 536 ForAllPrograms(CompileAndCompare); |
539 } | 537 } |
540 | 538 |
541 } // namespace sandbox | 539 } // namespace sandbox |
OLD | NEW |