OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "sandbox/linux/seccomp-bpf/codegen.h" |
| 6 |
| 7 #include <errno.h> |
| 8 #include <linux/filter.h> |
| 9 |
| 10 #include <set> |
| 11 #include <string> |
| 12 #include <vector> |
| 13 |
| 14 #include "sandbox/linux/seccomp-bpf/basicblock.h" |
| 15 #include "sandbox/linux/seccomp-bpf/errorcode.h" |
| 16 #include "sandbox/linux/seccomp-bpf/instruction.h" |
| 17 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" |
| 18 #include "sandbox/linux/tests/unit_tests.h" |
| 19 |
| 20 namespace sandbox { |
| 21 |
| 22 // We want to access some of the private methods in the code generator. We |
| 23 // do so by defining a "friend" that makes these methods public for us. |
| 24 class CodeGenUnittestHelper : public CodeGen { |
| 25 public: |
| 26 void FindBranchTargets(const Instruction& instructions, |
| 27 BranchTargets* branch_targets) { |
| 28 CodeGen::FindBranchTargets(instructions, branch_targets); |
| 29 } |
| 30 |
| 31 BasicBlock* CutGraphIntoBasicBlocks(Instruction* insns, |
| 32 const BranchTargets& branch_targets, |
| 33 TargetsToBlocks* blocks) { |
| 34 return CodeGen::CutGraphIntoBasicBlocks(insns, branch_targets, blocks); |
| 35 } |
| 36 |
| 37 void MergeTails(TargetsToBlocks* blocks) { CodeGen::MergeTails(blocks); } |
| 38 }; |
| 39 |
| 40 enum { NO_FLAGS = 0x0000, HAS_MERGEABLE_TAILS = 0x0001, }; |
| 41 |
| 42 Instruction* SampleProgramOneInstruction(CodeGen* codegen, int* flags) { |
| 43 // Create the most basic valid BPF program: |
| 44 // RET 0 |
| 45 *flags = NO_FLAGS; |
| 46 return codegen->MakeInstruction(BPF_RET + BPF_K, 0); |
| 47 } |
| 48 |
| 49 Instruction* SampleProgramSimpleBranch(CodeGen* codegen, int* flags) { |
| 50 // Create a program with a single branch: |
| 51 // JUMP if eq 42 then $0 else $1 |
| 52 // 0: RET 1 |
| 53 // 1: RET 0 |
| 54 *flags = NO_FLAGS; |
| 55 return codegen->MakeInstruction( |
| 56 BPF_JMP + BPF_JEQ + BPF_K, |
| 57 42, |
| 58 codegen->MakeInstruction(BPF_RET + BPF_K, 1), |
| 59 codegen->MakeInstruction(BPF_RET + BPF_K, 0)); |
| 60 } |
| 61 |
| 62 Instruction* SampleProgramAtypicalBranch(CodeGen* codegen, int* flags) { |
| 63 // Create a program with a single branch: |
| 64 // JUMP if eq 42 then $0 else $0 |
| 65 // 0: RET 0 |
| 66 |
| 67 // N.B.: As the instructions in both sides of the branch are already |
| 68 // the same object, we do not actually have any "mergeable" branches. |
| 69 // This needs to be reflected in our choice of "flags". |
| 70 *flags = NO_FLAGS; |
| 71 |
| 72 Instruction* ret = codegen->MakeInstruction( |
| 73 BPF_RET + BPF_K, 0); |
| 74 return codegen->MakeInstruction(BPF_JMP + BPF_JEQ + BPF_K, 42, ret, ret); |
| 75 } |
| 76 |
| 77 Instruction* SampleProgramComplex(CodeGen* codegen, int* flags) { |
| 78 // Creates a basic BPF program that we'll use to test some of the code: |
| 79 // JUMP if eq 42 the $0 else $1 (insn6) |
| 80 // 0: LD 23 (insn5) |
| 81 // 1: JUMP if eq 42 then $2 else $4 (insn4) |
| 82 // 2: JUMP to $3 (insn2) |
| 83 // 3: LD 42 (insn1) |
| 84 // RET 42 (insn0) |
| 85 // 4: LD 42 (insn3) |
| 86 // RET 42 (insn3+) |
| 87 *flags = HAS_MERGEABLE_TAILS; |
| 88 |
| 89 Instruction* insn0 = codegen->MakeInstruction(BPF_RET + BPF_K, 42); |
| 90 SANDBOX_ASSERT(insn0); |
| 91 SANDBOX_ASSERT(insn0->code == BPF_RET + BPF_K); |
| 92 SANDBOX_ASSERT(insn0->next == NULL); |
| 93 |
| 94 Instruction* insn1 = |
| 95 codegen->MakeInstruction(BPF_LD + BPF_W + BPF_ABS, 42, insn0); |
| 96 SANDBOX_ASSERT(insn1); |
| 97 SANDBOX_ASSERT(insn1->code == BPF_LD + BPF_W + BPF_ABS); |
| 98 SANDBOX_ASSERT(insn1->k == 42); |
| 99 SANDBOX_ASSERT(insn1->next == insn0); |
| 100 |
| 101 Instruction* insn2 = codegen->MakeInstruction(BPF_JMP + BPF_JA, 0, insn1); |
| 102 SANDBOX_ASSERT(insn2); |
| 103 SANDBOX_ASSERT(insn2->code == BPF_JMP + BPF_JA); |
| 104 SANDBOX_ASSERT(insn2->jt_ptr == insn1); |
| 105 |
| 106 // We explicitly duplicate instructions so that MergeTails() can coalesce |
| 107 // them later. |
| 108 Instruction* insn3 = codegen->MakeInstruction( |
| 109 BPF_LD + BPF_W + BPF_ABS, |
| 110 42, |
| 111 codegen->MakeInstruction(BPF_RET + BPF_K, 42)); |
| 112 |
| 113 Instruction* insn4 = |
| 114 codegen->MakeInstruction(BPF_JMP + BPF_JEQ + BPF_K, 42, insn2, insn3); |
| 115 SANDBOX_ASSERT(insn4); |
| 116 SANDBOX_ASSERT(insn4->code == BPF_JMP + BPF_JEQ + BPF_K); |
| 117 SANDBOX_ASSERT(insn4->k == 42); |
| 118 SANDBOX_ASSERT(insn4->jt_ptr == insn2); |
| 119 SANDBOX_ASSERT(insn4->jf_ptr == insn3); |
| 120 |
| 121 Instruction* insn5 = |
| 122 codegen->MakeInstruction(BPF_LD + BPF_W + BPF_ABS, 23, insn4); |
| 123 SANDBOX_ASSERT(insn5); |
| 124 SANDBOX_ASSERT(insn5->code == BPF_LD + BPF_W + BPF_ABS); |
| 125 SANDBOX_ASSERT(insn5->k == 23); |
| 126 SANDBOX_ASSERT(insn5->next == insn4); |
| 127 |
| 128 // Force a basic block that ends in neither a jump instruction nor a return |
| 129 // instruction. It only contains "insn5". This exercises one of the less |
| 130 // common code paths in the topo-sort algorithm. |
| 131 // This also gives us a diamond-shaped pattern in our graph, which stresses |
| 132 // another aspect of the topo-sort algorithm (namely, the ability to |
| 133 // correctly count the incoming branches for subtrees that are not disjunct). |
| 134 Instruction* insn6 = |
| 135 codegen->MakeInstruction(BPF_JMP + BPF_JEQ + BPF_K, 42, insn5, insn4); |
| 136 |
| 137 return insn6; |
| 138 } |
| 139 |
| 140 Instruction* SampleProgramConfusingTails(CodeGen* codegen, int* flags) { |
| 141 // This simple program demonstrates https://crbug.com/351103/ |
| 142 // The two "LOAD 0" instructions are blocks of their own. MergeTails() could |
| 143 // be tempted to merge them since they are the same. However, they are |
| 144 // not mergeable because they fall-through to non semantically equivalent |
| 145 // blocks. |
| 146 // Without the fix for this bug, this program should trigger the check in |
| 147 // CompileAndCompare: the serialized graphs from the program and its compiled |
| 148 // version will differ. |
| 149 // |
| 150 // 0) LOAD 1 // ??? |
| 151 // 1) if A == 0x1; then JMP 2 else JMP 3 |
| 152 // 2) LOAD 0 // System call number |
| 153 // 3) if A == 0x2; then JMP 4 else JMP 5 |
| 154 // 4) LOAD 0 // System call number |
| 155 // 5) if A == 0x1; then JMP 6 else JMP 7 |
| 156 // 6) RET 0 |
| 157 // 7) RET 1 |
| 158 *flags = NO_FLAGS; |
| 159 |
| 160 Instruction* i7 = codegen->MakeInstruction(BPF_RET + BPF_K, 1); |
| 161 Instruction* i6 = codegen->MakeInstruction(BPF_RET + BPF_K, 0); |
| 162 Instruction* i5 = |
| 163 codegen->MakeInstruction(BPF_JMP + BPF_JEQ + BPF_K, 1, i6, i7); |
| 164 Instruction* i4 = codegen->MakeInstruction(BPF_LD + BPF_W + BPF_ABS, 0, i5); |
| 165 Instruction* i3 = |
| 166 codegen->MakeInstruction(BPF_JMP + BPF_JEQ + BPF_K, 2, i4, i5); |
| 167 Instruction* i2 = codegen->MakeInstruction(BPF_LD + BPF_W + BPF_ABS, 0, i3); |
| 168 Instruction* i1 = |
| 169 codegen->MakeInstruction(BPF_JMP + BPF_JEQ + BPF_K, 1, i2, i3); |
| 170 Instruction* i0 = codegen->MakeInstruction(BPF_LD + BPF_W + BPF_ABS, 1, i1); |
| 171 |
| 172 return i0; |
| 173 } |
| 174 |
| 175 Instruction* SampleProgramConfusingTailsBasic(CodeGen* codegen, int* flags) { |
| 176 // Without the fix for https://crbug.com/351103/, (see |
| 177 // SampleProgramConfusingTails()), this would generate a cyclic graph and |
| 178 // crash as the two "LOAD 0" instructions would get merged. |
| 179 // |
| 180 // 0) LOAD 1 // ??? |
| 181 // 1) if A == 0x1; then JMP 2 else JMP 3 |
| 182 // 2) LOAD 0 // System call number |
| 183 // 3) if A == 0x2; then JMP 4 else JMP 5 |
| 184 // 4) LOAD 0 // System call number |
| 185 // 5) RET 1 |
| 186 *flags = NO_FLAGS; |
| 187 |
| 188 Instruction* i5 = codegen->MakeInstruction(BPF_RET + BPF_K, 1); |
| 189 Instruction* i4 = codegen->MakeInstruction(BPF_LD + BPF_W + BPF_ABS, 0, i5); |
| 190 Instruction* i3 = |
| 191 codegen->MakeInstruction(BPF_JMP + BPF_JEQ + BPF_K, 2, i4, i5); |
| 192 Instruction* i2 = codegen->MakeInstruction(BPF_LD + BPF_W + BPF_ABS, 0, i3); |
| 193 Instruction* i1 = |
| 194 codegen->MakeInstruction(BPF_JMP + BPF_JEQ + BPF_K, 1, i2, i3); |
| 195 Instruction* i0 = codegen->MakeInstruction(BPF_LD + BPF_W + BPF_ABS, 1, i1); |
| 196 |
| 197 return i0; |
| 198 } |
| 199 |
| 200 Instruction* SampleProgramConfusingTailsMergeable(CodeGen* codegen, |
| 201 int* flags) { |
| 202 // This is similar to SampleProgramConfusingTails(), except that |
| 203 // instructions 2 and 4 are now RET instructions. |
| 204 // In PointerCompare(), this exercises the path where two blocks are of the |
| 205 // same length and identical and the last instruction is a JMP or RET, so the |
| 206 // following blocks don't need to be looked at and the blocks are mergeable. |
| 207 // |
| 208 // 0) LOAD 1 // ??? |
| 209 // 1) if A == 0x1; then JMP 2 else JMP 3 |
| 210 // 2) RET 42 |
| 211 // 3) if A == 0x2; then JMP 4 else JMP 5 |
| 212 // 4) RET 42 |
| 213 // 5) if A == 0x1; then JMP 6 else JMP 7 |
| 214 // 6) RET 0 |
| 215 // 7) RET 1 |
| 216 *flags = HAS_MERGEABLE_TAILS; |
| 217 |
| 218 Instruction* i7 = codegen->MakeInstruction(BPF_RET + BPF_K, 1); |
| 219 Instruction* i6 = codegen->MakeInstruction(BPF_RET + BPF_K, 0); |
| 220 Instruction* i5 = |
| 221 codegen->MakeInstruction(BPF_JMP + BPF_JEQ + BPF_K, 1, i6, i7); |
| 222 Instruction* i4 = codegen->MakeInstruction(BPF_RET + BPF_K, 42); |
| 223 Instruction* i3 = |
| 224 codegen->MakeInstruction(BPF_JMP + BPF_JEQ + BPF_K, 2, i4, i5); |
| 225 Instruction* i2 = codegen->MakeInstruction(BPF_RET + BPF_K, 42); |
| 226 Instruction* i1 = |
| 227 codegen->MakeInstruction(BPF_JMP + BPF_JEQ + BPF_K, 1, i2, i3); |
| 228 Instruction* i0 = codegen->MakeInstruction(BPF_LD + BPF_W + BPF_ABS, 1, i1); |
| 229 |
| 230 return i0; |
| 231 } |
| 232 void ForAllPrograms(void (*test)(CodeGenUnittestHelper*, Instruction*, int)) { |
| 233 Instruction* (*function_table[])(CodeGen* codegen, int* flags) = { |
| 234 SampleProgramOneInstruction, |
| 235 SampleProgramSimpleBranch, |
| 236 SampleProgramAtypicalBranch, |
| 237 SampleProgramComplex, |
| 238 SampleProgramConfusingTails, |
| 239 SampleProgramConfusingTailsBasic, |
| 240 SampleProgramConfusingTailsMergeable, |
| 241 }; |
| 242 |
| 243 for (size_t i = 0; i < arraysize(function_table); ++i) { |
| 244 CodeGenUnittestHelper codegen; |
| 245 int flags = NO_FLAGS; |
| 246 Instruction *prg = function_table[i](&codegen, &flags); |
| 247 test(&codegen, prg, flags); |
| 248 } |
| 249 } |
| 250 |
| 251 void MakeInstruction(CodeGenUnittestHelper* codegen, |
| 252 Instruction* program, int) { |
| 253 // Nothing to do here |
| 254 } |
| 255 |
| 256 SANDBOX_TEST(CodeGen, MakeInstruction) { |
| 257 ForAllPrograms(MakeInstruction); |
| 258 } |
| 259 |
| 260 void FindBranchTargets(CodeGenUnittestHelper* codegen, Instruction* prg, int) { |
| 261 BranchTargets branch_targets; |
| 262 codegen->FindBranchTargets(*prg, &branch_targets); |
| 263 |
| 264 // Verifying the general properties that should be true for every |
| 265 // well-formed BPF program. |
| 266 // Perform a depth-first traversal of the BPF program an verify that all |
| 267 // targets of BPF_JMP instructions are represented in the "branch_targets". |
| 268 // At the same time, compute a set of both the branch targets and all the |
| 269 // instructions in the program. |
| 270 std::vector<Instruction*> stack; |
| 271 std::set<Instruction*> all_instructions; |
| 272 std::set<Instruction*> target_instructions; |
| 273 BranchTargets::const_iterator end = branch_targets.end(); |
| 274 for (Instruction* insn = prg;;) { |
| 275 all_instructions.insert(insn); |
| 276 if (BPF_CLASS(insn->code) == BPF_JMP) { |
| 277 target_instructions.insert(insn->jt_ptr); |
| 278 SANDBOX_ASSERT(insn->jt_ptr != NULL); |
| 279 SANDBOX_ASSERT(branch_targets.find(insn->jt_ptr) != end); |
| 280 if (BPF_OP(insn->code) != BPF_JA) { |
| 281 target_instructions.insert(insn->jf_ptr); |
| 282 SANDBOX_ASSERT(insn->jf_ptr != NULL); |
| 283 SANDBOX_ASSERT(branch_targets.find(insn->jf_ptr) != end); |
| 284 stack.push_back(insn->jf_ptr); |
| 285 } |
| 286 insn = insn->jt_ptr; |
| 287 } else if (BPF_CLASS(insn->code) == BPF_RET) { |
| 288 SANDBOX_ASSERT(insn->next == NULL); |
| 289 if (stack.empty()) { |
| 290 break; |
| 291 } |
| 292 insn = stack.back(); |
| 293 stack.pop_back(); |
| 294 } else { |
| 295 SANDBOX_ASSERT(insn->next != NULL); |
| 296 insn = insn->next; |
| 297 } |
| 298 } |
| 299 SANDBOX_ASSERT(target_instructions.size() == branch_targets.size()); |
| 300 |
| 301 // We can now subtract the set of the branch targets from the set of all |
| 302 // instructions. This gives us a set with the instructions that nobody |
| 303 // ever jumps to. Verify that they are no included in the |
| 304 // "branch_targets" that FindBranchTargets() computed for us. |
| 305 Instructions non_target_instructions(all_instructions.size() - |
| 306 target_instructions.size()); |
| 307 set_difference(all_instructions.begin(), |
| 308 all_instructions.end(), |
| 309 target_instructions.begin(), |
| 310 target_instructions.end(), |
| 311 non_target_instructions.begin()); |
| 312 for (Instructions::const_iterator iter = non_target_instructions.begin(); |
| 313 iter != non_target_instructions.end(); |
| 314 ++iter) { |
| 315 SANDBOX_ASSERT(branch_targets.find(*iter) == end); |
| 316 } |
| 317 } |
| 318 |
| 319 SANDBOX_TEST(CodeGen, FindBranchTargets) { ForAllPrograms(FindBranchTargets); } |
| 320 |
| 321 void CutGraphIntoBasicBlocks(CodeGenUnittestHelper* codegen, |
| 322 Instruction* prg, |
| 323 int) { |
| 324 BranchTargets branch_targets; |
| 325 codegen->FindBranchTargets(*prg, &branch_targets); |
| 326 TargetsToBlocks all_blocks; |
| 327 BasicBlock* first_block = |
| 328 codegen->CutGraphIntoBasicBlocks(prg, branch_targets, &all_blocks); |
| 329 SANDBOX_ASSERT(first_block != NULL); |
| 330 SANDBOX_ASSERT(first_block->instructions.size() > 0); |
| 331 Instruction* first_insn = first_block->instructions[0]; |
| 332 |
| 333 // Basic blocks are supposed to start with a branch target and end with |
| 334 // either a jump or a return instruction. It can also end, if the next |
| 335 // instruction forms the beginning of a new basic block. There should be |
| 336 // no other jumps or return instructions in the middle of a basic block. |
| 337 for (TargetsToBlocks::const_iterator bb_iter = all_blocks.begin(); |
| 338 bb_iter != all_blocks.end(); |
| 339 ++bb_iter) { |
| 340 BasicBlock* bb = bb_iter->second; |
| 341 SANDBOX_ASSERT(bb != NULL); |
| 342 SANDBOX_ASSERT(bb->instructions.size() > 0); |
| 343 Instruction* insn = bb->instructions[0]; |
| 344 SANDBOX_ASSERT(insn == first_insn || |
| 345 branch_targets.find(insn) != branch_targets.end()); |
| 346 for (Instructions::const_iterator insn_iter = bb->instructions.begin();;) { |
| 347 insn = *insn_iter; |
| 348 if (++insn_iter != bb->instructions.end()) { |
| 349 SANDBOX_ASSERT(BPF_CLASS(insn->code) != BPF_JMP); |
| 350 SANDBOX_ASSERT(BPF_CLASS(insn->code) != BPF_RET); |
| 351 } else { |
| 352 SANDBOX_ASSERT(BPF_CLASS(insn->code) == BPF_JMP || |
| 353 BPF_CLASS(insn->code) == BPF_RET || |
| 354 branch_targets.find(insn->next) != branch_targets.end()); |
| 355 break; |
| 356 } |
| 357 SANDBOX_ASSERT(branch_targets.find(*insn_iter) == branch_targets.end()); |
| 358 } |
| 359 } |
| 360 } |
| 361 |
| 362 SANDBOX_TEST(CodeGen, CutGraphIntoBasicBlocks) { |
| 363 ForAllPrograms(CutGraphIntoBasicBlocks); |
| 364 } |
| 365 |
| 366 void MergeTails(CodeGenUnittestHelper* codegen, Instruction* prg, int flags) { |
| 367 BranchTargets branch_targets; |
| 368 codegen->FindBranchTargets(*prg, &branch_targets); |
| 369 TargetsToBlocks all_blocks; |
| 370 BasicBlock* first_block = |
| 371 codegen->CutGraphIntoBasicBlocks(prg, branch_targets, &all_blocks); |
| 372 |
| 373 // The shape of our graph and thus the function of our program should |
| 374 // still be unchanged after we run MergeTails(). We verify this by |
| 375 // serializing the graph and verifying that it is still the same. |
| 376 // We also verify that at least some of the edges changed because of |
| 377 // tail merging. |
| 378 std::string graph[2]; |
| 379 std::string edges[2]; |
| 380 |
| 381 // The loop executes twice. After the first run, we call MergeTails() on |
| 382 // our graph. |
| 383 for (int i = 0;;) { |
| 384 // Traverse the entire program in depth-first order. |
| 385 std::vector<BasicBlock*> stack; |
| 386 for (BasicBlock* bb = first_block;;) { |
| 387 // Serialize the instructions in this basic block. In general, we only |
| 388 // need to serialize "code" and "k"; except for a BPF_JA instruction |
| 389 // where "k" isn't set. |
| 390 // The stream of instructions should be unchanged after MergeTails(). |
| 391 for (Instructions::const_iterator iter = bb->instructions.begin(); |
| 392 iter != bb->instructions.end(); |
| 393 ++iter) { |
| 394 graph[i].append(reinterpret_cast<char*>(&(*iter)->code), |
| 395 sizeof((*iter)->code)); |
| 396 if (BPF_CLASS((*iter)->code) != BPF_JMP || |
| 397 BPF_OP((*iter)->code) != BPF_JA) { |
| 398 graph[i].append(reinterpret_cast<char*>(&(*iter)->k), |
| 399 sizeof((*iter)->k)); |
| 400 } |
| 401 } |
| 402 |
| 403 // Also serialize the addresses the basic blocks as we encounter them. |
| 404 // This will change as basic blocks are coalesed by MergeTails(). |
| 405 edges[i].append(reinterpret_cast<char*>(&bb), sizeof(bb)); |
| 406 |
| 407 // Depth-first traversal of the graph. We only ever need to look at the |
| 408 // very last instruction in the basic block, as that is the only one that |
| 409 // can change code flow. |
| 410 Instruction* insn = bb->instructions.back(); |
| 411 if (BPF_CLASS(insn->code) == BPF_JMP) { |
| 412 // For jump instructions, we need to remember the "false" branch while |
| 413 // traversing the "true" branch. This is not necessary for BPF_JA which |
| 414 // only has a single branch. |
| 415 if (BPF_OP(insn->code) != BPF_JA) { |
| 416 stack.push_back(all_blocks[insn->jf_ptr]); |
| 417 } |
| 418 bb = all_blocks[insn->jt_ptr]; |
| 419 } else if (BPF_CLASS(insn->code) == BPF_RET) { |
| 420 // After a BPF_RET, see if we need to back track. |
| 421 if (stack.empty()) { |
| 422 break; |
| 423 } |
| 424 bb = stack.back(); |
| 425 stack.pop_back(); |
| 426 } else { |
| 427 // For "normal" instructions, just follow to the next basic block. |
| 428 bb = all_blocks[insn->next]; |
| 429 } |
| 430 } |
| 431 |
| 432 // Our loop runs exactly two times. |
| 433 if (++i > 1) { |
| 434 break; |
| 435 } |
| 436 codegen->MergeTails(&all_blocks); |
| 437 } |
| 438 SANDBOX_ASSERT(graph[0] == graph[1]); |
| 439 if (flags & HAS_MERGEABLE_TAILS) { |
| 440 SANDBOX_ASSERT(edges[0] != edges[1]); |
| 441 } else { |
| 442 SANDBOX_ASSERT(edges[0] == edges[1]); |
| 443 } |
| 444 } |
| 445 |
| 446 SANDBOX_TEST(CodeGen, MergeTails) { |
| 447 ForAllPrograms(MergeTails); |
| 448 } |
| 449 |
| 450 void CompileAndCompare(CodeGenUnittestHelper* codegen, Instruction* prg, int) { |
| 451 // TopoSortBasicBlocks() has internal checks that cause it to fail, if it |
| 452 // detects a problem. Typically, if anything goes wrong, this looks to the |
| 453 // TopoSort algorithm as if there had been cycles in the input data. |
| 454 // This provides a pretty good unittest. |
| 455 // We hand-crafted the program returned by SampleProgram() to exercise |
| 456 // several of the more interesting code-paths. See comments in |
| 457 // SampleProgram() for details. |
| 458 // In addition to relying on the internal consistency checks in the compiler, |
| 459 // we also serialize the graph and the resulting BPF program and compare |
| 460 // them. With the exception of BPF_JA instructions that might have been |
| 461 // inserted, both instruction streams should be equivalent. |
| 462 // As Compile() modifies the instructions, we have to serialize the graph |
| 463 // before calling Compile(). |
| 464 std::string source; |
| 465 Instructions source_stack; |
| 466 for (const Instruction* insn = prg, *next; insn; insn = next) { |
| 467 if (BPF_CLASS(insn->code) == BPF_JMP) { |
| 468 if (BPF_OP(insn->code) == BPF_JA) { |
| 469 // Do not serialize BPF_JA instructions (see above). |
| 470 next = insn->jt_ptr; |
| 471 continue; |
| 472 } else { |
| 473 source_stack.push_back(insn->jf_ptr); |
| 474 next = insn->jt_ptr; |
| 475 } |
| 476 } else if (BPF_CLASS(insn->code) == BPF_RET) { |
| 477 if (source_stack.empty()) { |
| 478 next = NULL; |
| 479 } else { |
| 480 next = source_stack.back(); |
| 481 source_stack.pop_back(); |
| 482 } |
| 483 } else { |
| 484 next = insn->next; |
| 485 } |
| 486 // Only serialize "code" and "k". That's all the information we need to |
| 487 // compare. The rest of the information is encoded in the order of |
| 488 // instructions. |
| 489 source.append(reinterpret_cast<const char*>(&insn->code), |
| 490 sizeof(insn->code)); |
| 491 source.append(reinterpret_cast<const char*>(&insn->k), sizeof(insn->k)); |
| 492 } |
| 493 |
| 494 // Compile the program |
| 495 CodeGen::Program bpf; |
| 496 codegen->Compile(prg, &bpf); |
| 497 |
| 498 // Serialize the resulting BPF instructions. |
| 499 std::string assembly; |
| 500 std::vector<int> assembly_stack; |
| 501 for (int idx = 0; idx >= 0;) { |
| 502 SANDBOX_ASSERT(idx < (int)bpf.size()); |
| 503 struct sock_filter& insn = bpf[idx]; |
| 504 if (BPF_CLASS(insn.code) == BPF_JMP) { |
| 505 if (BPF_OP(insn.code) == BPF_JA) { |
| 506 // Do not serialize BPF_JA instructions (see above). |
| 507 idx += insn.k + 1; |
| 508 continue; |
| 509 } else { |
| 510 assembly_stack.push_back(idx + insn.jf + 1); |
| 511 idx += insn.jt + 1; |
| 512 } |
| 513 } else if (BPF_CLASS(insn.code) == BPF_RET) { |
| 514 if (assembly_stack.empty()) { |
| 515 idx = -1; |
| 516 } else { |
| 517 idx = assembly_stack.back(); |
| 518 assembly_stack.pop_back(); |
| 519 } |
| 520 } else { |
| 521 ++idx; |
| 522 } |
| 523 // Serialize the same information that we serialized before compilation. |
| 524 assembly.append(reinterpret_cast<char*>(&insn.code), sizeof(insn.code)); |
| 525 assembly.append(reinterpret_cast<char*>(&insn.k), sizeof(insn.k)); |
| 526 } |
| 527 SANDBOX_ASSERT(source == assembly); |
| 528 } |
| 529 |
| 530 SANDBOX_TEST(CodeGen, All) { |
| 531 ForAllPrograms(CompileAndCompare); |
| 532 } |
| 533 |
| 534 } // namespace sandbox |
OLD | NEW |