github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/flipcall/flipcall_unsafe.go (about)

     1  // Copyright 2019 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 flipcall
    16  
    17  import (
    18  	"reflect"
    19  	"unsafe"
    20  
    21  	"github.com/SagerNet/gvisor/pkg/sync"
    22  )
    23  
    24  // Packets consist of a 16-byte header followed by an arbitrarily-sized
    25  // datagram. The header consists of:
    26  //
    27  // - A 4-byte native-endian connection state.
    28  //
    29  // - A 4-byte native-endian datagram length in bytes.
    30  //
    31  // - 8 reserved bytes.
    32  const (
    33  	// PacketHeaderBytes is the size of a flipcall packet header in bytes. The
    34  	// maximum datagram size supported by a flipcall connection is equal to the
    35  	// length of the packet window minus PacketHeaderBytes.
    36  	//
    37  	// PacketHeaderBytes is exported to support its use in constant
    38  	// expressions. Non-constant expressions may prefer to use
    39  	// PacketWindowLengthForDataCap().
    40  	PacketHeaderBytes = 16
    41  )
    42  
    43  func (ep *Endpoint) connState() *uint32 {
    44  	return (*uint32)(unsafe.Pointer(ep.packet))
    45  }
    46  
    47  func (ep *Endpoint) dataLen() *uint32 {
    48  	return (*uint32)(unsafe.Pointer(ep.packet + 4))
    49  }
    50  
    51  // Data returns the datagram part of ep's packet window as a byte slice.
    52  //
    53  // Note that the packet window is shared with the potentially-untrusted peer
    54  // Endpoint, which may concurrently mutate the contents of the packet window.
    55  // Thus:
    56  //
    57  // - Readers must not assume that two reads of the same byte in Data() will
    58  // return the same result. In other words, readers should read any given byte
    59  // in Data() at most once.
    60  //
    61  // - Writers must not assume that they will read back the same data that they
    62  // have written. In other words, writers should avoid reading from Data() at
    63  // all.
    64  func (ep *Endpoint) Data() (bs []byte) {
    65  	bshdr := (*reflect.SliceHeader)(unsafe.Pointer(&bs))
    66  	bshdr.Data = ep.packet + PacketHeaderBytes
    67  	bshdr.Len = int(ep.dataCap)
    68  	bshdr.Cap = int(ep.dataCap)
    69  	return
    70  }
    71  
    72  // ioSync is a dummy variable used to indicate synchronization to the Go race
    73  // detector. Compare syscall.ioSync.
    74  var ioSync int64
    75  
    76  func raceBecomeActive() {
    77  	if sync.RaceEnabled {
    78  		sync.RaceAcquire(unsafe.Pointer(&ioSync))
    79  	}
    80  }
    81  
    82  func raceBecomeInactive() {
    83  	if sync.RaceEnabled {
    84  		sync.RaceReleaseMerge(unsafe.Pointer(&ioSync))
    85  	}
    86  }