github.com/sagernet/gvisor@v0.0.0-20240428053021-e691de28565f/pkg/bpf/bpf_unsafe.go (about)

     1  // Copyright 2023 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package bpf
    16  
    17  import (
    18  	"fmt"
    19  	"unsafe"
    20  )
    21  
    22  // sizeOfInstruction is the size of a BPF instruction struct.
    23  const sizeOfInstruction = int(unsafe.Sizeof(Instruction{}))
    24  
    25  // ToBytecode converts BPF instructions into raw BPF bytecode.
    26  func ToBytecode(insns []Instruction) []byte {
    27  	return ([]byte)(unsafe.Slice((*byte)(unsafe.Pointer(&insns[0])), len(insns)*sizeOfInstruction))
    28  }
    29  
    30  // ParseBytecode converts raw BPF bytecode into BPF instructions.
    31  // It verifies that the resulting set of instructions is a valid program.
    32  func ParseBytecode(bytecode []byte) ([]Instruction, error) {
    33  	if len(bytecode)%sizeOfInstruction != 0 {
    34  		return nil, fmt.Errorf("bytecode size (%d bytes) is not a multiple of BPF instruction size of %d bytes", len(bytecode), sizeOfInstruction)
    35  	}
    36  	insns := ([]Instruction)(unsafe.Slice((*Instruction)(unsafe.Pointer(&bytecode[0])), len(bytecode)/sizeOfInstruction))
    37  	if _, err := Compile(insns, false); err != nil {
    38  		return nil, fmt.Errorf("not a valid BPF program: %v", err)
    39  	}
    40  	return insns, nil
    41  }