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