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