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 #ifndef SANDBOX_LINUX_SECCOMP_BPF_INSTRUCTION_H__ 6 #define SANDBOX_LINUX_SECCOMP_BPF_INSTRUCTION_H__ 7 8 #include <stdint.h> 9 10 namespace sandbox { 11 12 // The fields in this structure have the same meaning as the corresponding 13 // fields in "struct sock_filter". See <linux/filter.h> for a lot more 14 // detail. 15 // code -- Opcode of the instruction. This is typically a bitwise 16 // combination BPF_XXX values. 17 // k -- Operand; BPF instructions take zero or one operands. Operands 18 // are 32bit-wide constants, if present. They can be immediate 19 // values (if BPF_K is present in "code_"), addresses (if BPF_ABS 20 // is present in "code_"), or relative jump offsets (if BPF_JMP 21 // and BPF_JA are present in "code_"). 22 // jt, jf -- all conditional jumps have a 8bit-wide jump offset that allows 23 // jumps of up to 256 instructions forward. Conditional jumps are 24 // identified by BPF_JMP in "code_", but the lack of BPF_JA. 25 // Conditional jumps have a "t"rue and "f"alse branch. 26 struct Instruction { 27 // Constructor for an non-jumping instruction or for an unconditional 28 // "always" jump. 29 Instruction(uint16_t c, uint32_t parm, Instruction* n) 30 : code(c), next(n), k(parm) {} 31 32 // Constructor for a conditional jump instruction. 33 Instruction(uint16_t c, uint32_t parm, Instruction* jt, Instruction* jf) 34 : code(c), jt_ptr(jt), jf_ptr(jf), k(parm) {} 35 36 uint16_t code; 37 union { 38 // When code generation is complete, we will have computed relative 39 // branch targets that are in the range 0..255. 40 struct { 41 uint8_t jt, jf; 42 }; 43 44 // While assembling the BPF program, we use pointers for branch targets. 45 // Once we have computed basic blocks, these pointers will be entered as 46 // keys in a TargetsToBlocks map and should no longer be dereferenced 47 // directly. 48 struct { 49 Instruction* jt_ptr, *jf_ptr; 50 }; 51 52 // While assembling the BPF program, non-jumping instructions are linked 53 // by the "next_" pointer. This field is no longer needed when we have 54 // computed basic blocks. 55 Instruction* next; 56 }; 57 uint32_t k; 58 }; 59 60 } // namespace sandbox 61 62 #endif // SANDBOX_LINUX_SECCOMP_BPF_INSTRUCTION_H__ 63