snapshot: sagernet/gvisor v0.0.0-20250811.0-sing-box-mod.1
Содержимое пина, зафиксированного в go.mod sing-box-lx, одним коммитом без истории. Полная история SagerNet/gvisor — 1.45 ГБ и клонируется в каждой CI-джобе; наша дельта — одна вставка в одну функцию, история для неё не нужна. Module path github.com/sagernet/gvisor сохранён намеренно: на него опирается replace-директива суперпроекта. Патч поверх — отдельным коммитом, чтобы дельта читалась одним git show и переносилась на новый пин копированием. SPECS/TASKS/048-GVISOR_HANDSHAKE_NIL_CRASH
This commit is contained in:
commit
2c4ae3b0a4
712 changed files with 185689 additions and 0 deletions
80
pkg/tcpip/link/sharedmem/pipe/pipe.go
Normal file
80
pkg/tcpip/link/sharedmem/pipe/pipe.go
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
// Copyright 2018 The gVisor Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package pipe implements a shared memory ring buffer on which a single reader
|
||||
// and a single writer can operate (read/write) concurrently. The ring buffer
|
||||
// allows for data of different sizes to be written, and preserves the boundary
|
||||
// of the written data.
|
||||
//
|
||||
// Example usage is as follows:
|
||||
//
|
||||
// wb := t.Push(20)
|
||||
// // Write data to wb.
|
||||
// t.Flush()
|
||||
//
|
||||
// rb := r.Pull()
|
||||
// // Do something with data in rb.
|
||||
// t.Flush()
|
||||
package pipe
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
const (
|
||||
jump uint64 = math.MaxUint32 + 1
|
||||
offsetMask uint64 = math.MaxUint32
|
||||
revolutionMask uint64 = ^offsetMask
|
||||
|
||||
sizeOfSlotHeader = 8 // sizeof(uint64)
|
||||
slotFree uint64 = 1 << 63
|
||||
slotSizeMask uint64 = math.MaxUint32
|
||||
)
|
||||
|
||||
// payloadToSlotSize calculates the total size of a slot based on its payload
|
||||
// size. The total size is the header size, plus the payload size, plus padding
|
||||
// if necessary to make the total size a multiple of sizeOfSlotHeader.
|
||||
func payloadToSlotSize(payloadSize uint64) uint64 {
|
||||
s := sizeOfSlotHeader + payloadSize
|
||||
return (s + sizeOfSlotHeader - 1) &^ (sizeOfSlotHeader - 1)
|
||||
}
|
||||
|
||||
// slotToPayloadSize calculates the payload size of a slot based on the total
|
||||
// size of the slot. This is only meant to be used when creating slots that
|
||||
// don't carry information (e.g., free slots or wrap slots).
|
||||
func slotToPayloadSize(offset uint64) uint64 {
|
||||
return offset - sizeOfSlotHeader
|
||||
}
|
||||
|
||||
// pipe is a basic data structure used by both (transmit & receive) ends of a
|
||||
// pipe. Indices into this pipe are split into two fields: offset, which counts
|
||||
// the number of bytes from the beginning of the buffer, and revolution, which
|
||||
// counts the number of times the index has wrapped around.
|
||||
//
|
||||
// +stateify savable
|
||||
type pipe struct {
|
||||
buffer []byte
|
||||
}
|
||||
|
||||
// init initializes the pipe buffer such that its size is a multiple of the size
|
||||
// of the slot header.
|
||||
func (p *pipe) init(b []byte) {
|
||||
p.buffer = b[:len(b)&^(sizeOfSlotHeader-1)]
|
||||
}
|
||||
|
||||
// data returns a section of the buffer starting at the given index (which may
|
||||
// include revolution information) and with the given size.
|
||||
func (p *pipe) data(idx uint64, size uint64) []byte {
|
||||
return p.buffer[(idx&offsetMask)+sizeOfSlotHeader:][:size]
|
||||
}
|
||||
111
pkg/tcpip/link/sharedmem/pipe/pipe_state_autogen.go
Normal file
111
pkg/tcpip/link/sharedmem/pipe/pipe_state_autogen.go
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
// automatically generated by stateify.
|
||||
|
||||
package pipe
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/sagernet/gvisor/pkg/state"
|
||||
)
|
||||
|
||||
func (p *pipe) StateTypeName() string {
|
||||
return "pkg/tcpip/link/sharedmem/pipe.pipe"
|
||||
}
|
||||
|
||||
func (p *pipe) StateFields() []string {
|
||||
return []string{
|
||||
"buffer",
|
||||
}
|
||||
}
|
||||
|
||||
func (p *pipe) beforeSave() {}
|
||||
|
||||
// +checklocksignore
|
||||
func (p *pipe) StateSave(stateSinkObject state.Sink) {
|
||||
p.beforeSave()
|
||||
stateSinkObject.Save(0, &p.buffer)
|
||||
}
|
||||
|
||||
func (p *pipe) afterLoad(context.Context) {}
|
||||
|
||||
// +checklocksignore
|
||||
func (p *pipe) StateLoad(ctx context.Context, stateSourceObject state.Source) {
|
||||
stateSourceObject.Load(0, &p.buffer)
|
||||
}
|
||||
|
||||
func (r *Rx) StateTypeName() string {
|
||||
return "pkg/tcpip/link/sharedmem/pipe.Rx"
|
||||
}
|
||||
|
||||
func (r *Rx) StateFields() []string {
|
||||
return []string{
|
||||
"p",
|
||||
"tail",
|
||||
"head",
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Rx) beforeSave() {}
|
||||
|
||||
// +checklocksignore
|
||||
func (r *Rx) StateSave(stateSinkObject state.Sink) {
|
||||
r.beforeSave()
|
||||
stateSinkObject.Save(0, &r.p)
|
||||
stateSinkObject.Save(1, &r.tail)
|
||||
stateSinkObject.Save(2, &r.head)
|
||||
}
|
||||
|
||||
func (r *Rx) afterLoad(context.Context) {}
|
||||
|
||||
// +checklocksignore
|
||||
func (r *Rx) StateLoad(ctx context.Context, stateSourceObject state.Source) {
|
||||
stateSourceObject.Load(0, &r.p)
|
||||
stateSourceObject.Load(1, &r.tail)
|
||||
stateSourceObject.Load(2, &r.head)
|
||||
}
|
||||
|
||||
func (t *Tx) StateTypeName() string {
|
||||
return "pkg/tcpip/link/sharedmem/pipe.Tx"
|
||||
}
|
||||
|
||||
func (t *Tx) StateFields() []string {
|
||||
return []string{
|
||||
"p",
|
||||
"maxPayloadSize",
|
||||
"head",
|
||||
"tail",
|
||||
"next",
|
||||
"tailHeader",
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Tx) beforeSave() {}
|
||||
|
||||
// +checklocksignore
|
||||
func (t *Tx) StateSave(stateSinkObject state.Sink) {
|
||||
t.beforeSave()
|
||||
stateSinkObject.Save(0, &t.p)
|
||||
stateSinkObject.Save(1, &t.maxPayloadSize)
|
||||
stateSinkObject.Save(2, &t.head)
|
||||
stateSinkObject.Save(3, &t.tail)
|
||||
stateSinkObject.Save(4, &t.next)
|
||||
stateSinkObject.Save(5, &t.tailHeader)
|
||||
}
|
||||
|
||||
func (t *Tx) afterLoad(context.Context) {}
|
||||
|
||||
// +checklocksignore
|
||||
func (t *Tx) StateLoad(ctx context.Context, stateSourceObject state.Source) {
|
||||
stateSourceObject.Load(0, &t.p)
|
||||
stateSourceObject.Load(1, &t.maxPayloadSize)
|
||||
stateSourceObject.Load(2, &t.head)
|
||||
stateSourceObject.Load(3, &t.tail)
|
||||
stateSourceObject.Load(4, &t.next)
|
||||
stateSourceObject.Load(5, &t.tailHeader)
|
||||
}
|
||||
|
||||
func init() {
|
||||
state.Register((*pipe)(nil))
|
||||
state.Register((*Rx)(nil))
|
||||
state.Register((*Tx)(nil))
|
||||
}
|
||||
36
pkg/tcpip/link/sharedmem/pipe/pipe_unsafe.go
Normal file
36
pkg/tcpip/link/sharedmem/pipe/pipe_unsafe.go
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
// Copyright 2018 The gVisor Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package pipe
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/sagernet/gvisor/pkg/atomicbitops"
|
||||
)
|
||||
|
||||
func (p *pipe) write(idx uint64, v uint64) {
|
||||
ptr := (*uint64)(unsafe.Pointer(&p.buffer[idx&offsetMask:][:8][0]))
|
||||
*ptr = v
|
||||
}
|
||||
|
||||
func (p *pipe) writeAtomic(idx uint64, v uint64) {
|
||||
ptr := (*atomicbitops.Uint64)(unsafe.Pointer(&p.buffer[idx&offsetMask:][:8][0]))
|
||||
ptr.Store(v)
|
||||
}
|
||||
|
||||
func (p *pipe) readAtomic(idx uint64) uint64 {
|
||||
ptr := (*atomicbitops.Uint64)(unsafe.Pointer(&p.buffer[idx&offsetMask:][:8][0]))
|
||||
return ptr.Load()
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
// automatically generated by stateify.
|
||||
|
||||
package pipe
|
||||
108
pkg/tcpip/link/sharedmem/pipe/rx.go
Normal file
108
pkg/tcpip/link/sharedmem/pipe/rx.go
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
// Copyright 2018 The gVisor Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package pipe
|
||||
|
||||
// Rx is the receive side of the shared memory ring buffer.
|
||||
//
|
||||
// +stateify savable
|
||||
type Rx struct {
|
||||
p pipe
|
||||
|
||||
tail uint64
|
||||
head uint64
|
||||
}
|
||||
|
||||
// Init initializes the receive end of the pipe. In the initial state, the next
|
||||
// slot to be inspected is the very first one.
|
||||
func (r *Rx) Init(b []byte) {
|
||||
r.p.init(b)
|
||||
r.tail = 0xfffffffe * jump
|
||||
r.head = r.tail
|
||||
}
|
||||
|
||||
// Pull reads the next buffer from the pipe, returning nil if there isn't one
|
||||
// currently available.
|
||||
//
|
||||
// The returned slice is available until Flush() is next called. After that, it
|
||||
// must not be touched.
|
||||
func (r *Rx) Pull() []byte {
|
||||
if r.head == r.tail+jump {
|
||||
// We've already pulled the whole pipe.
|
||||
return nil
|
||||
}
|
||||
|
||||
header := r.p.readAtomic(r.head)
|
||||
if header&slotFree != 0 {
|
||||
// The next slot is free, we can't pull it yet.
|
||||
return nil
|
||||
}
|
||||
|
||||
payloadSize := header & slotSizeMask
|
||||
newHead := r.head + payloadToSlotSize(payloadSize)
|
||||
headWrap := (r.head & revolutionMask) | uint64(len(r.p.buffer))
|
||||
|
||||
// Check if this is a wrapping slot. If that's the case, it carries no
|
||||
// data, so we just skip it and try again from the first slot.
|
||||
if int64(newHead-headWrap) >= 0 {
|
||||
// If newHead passes the tail, the pipe is either damaged or the
|
||||
// RX view of the pipe has completely wrapped without an
|
||||
// intervening flush.
|
||||
if int64(newHead-(r.tail+jump)) > 0 {
|
||||
return nil
|
||||
}
|
||||
// The pipe is damaged if newHead doesn't point to the start of
|
||||
// the ring.
|
||||
if newHead&offsetMask != 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if r.tail == r.head {
|
||||
// If this is the first pull since the last Flush()
|
||||
// call, we flush the state so that the sender can use
|
||||
// this space if it needs to.
|
||||
r.p.writeAtomic(r.head, slotFree|slotToPayloadSize(newHead-r.head))
|
||||
r.tail = newHead
|
||||
}
|
||||
|
||||
r.head = newHead
|
||||
return r.Pull()
|
||||
}
|
||||
|
||||
// Grab the buffer before updating r.head.
|
||||
b := r.p.data(r.head, payloadSize)
|
||||
r.head = newHead
|
||||
return b
|
||||
}
|
||||
|
||||
// Flush tells the transmitter that all buffers pulled since the last Flush()
|
||||
// have been used, so the transmitter is free to used their slots for further
|
||||
// transmission.
|
||||
func (r *Rx) Flush() {
|
||||
if r.head == r.tail {
|
||||
return
|
||||
}
|
||||
r.p.writeAtomic(r.tail, slotFree|slotToPayloadSize(r.head-r.tail))
|
||||
r.tail = r.head
|
||||
}
|
||||
|
||||
// Abort unpulls any pulled buffers.
|
||||
func (r *Rx) Abort() {
|
||||
r.head = r.tail
|
||||
}
|
||||
|
||||
// Bytes returns the byte slice on which the pipe operates.
|
||||
func (r *Rx) Bytes() []byte {
|
||||
return r.p.buffer
|
||||
}
|
||||
164
pkg/tcpip/link/sharedmem/pipe/tx.go
Normal file
164
pkg/tcpip/link/sharedmem/pipe/tx.go
Normal file
|
|
@ -0,0 +1,164 @@
|
|||
// Copyright 2018 The gVisor Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package pipe
|
||||
|
||||
// Tx is the transmit side of the shared memory ring buffer.
|
||||
//
|
||||
// +stateify savable
|
||||
type Tx struct {
|
||||
p pipe
|
||||
maxPayloadSize uint64
|
||||
|
||||
head uint64
|
||||
tail uint64
|
||||
next uint64
|
||||
|
||||
tailHeader uint64
|
||||
}
|
||||
|
||||
// Init initializes the transmit end of the pipe. In the initial state, the next
|
||||
// slot to be written is the very first one, and the transmitter has the whole
|
||||
// ring buffer available to it.
|
||||
func (t *Tx) Init(b []byte) {
|
||||
t.p.init(b)
|
||||
// maxPayloadSize excludes the header of the payload, and the header
|
||||
// of the wrapping message.
|
||||
t.maxPayloadSize = uint64(len(t.p.buffer)) - 2*sizeOfSlotHeader
|
||||
t.tail = 0xfffffffe * jump
|
||||
t.next = t.tail
|
||||
t.head = t.tail + jump
|
||||
t.p.write(t.tail, slotFree)
|
||||
}
|
||||
|
||||
// Capacity determines how many records of the given size can be written to the
|
||||
// pipe before it fills up.
|
||||
func (t *Tx) Capacity(recordSize uint64) uint64 {
|
||||
available := uint64(len(t.p.buffer)) - sizeOfSlotHeader
|
||||
entryLen := payloadToSlotSize(recordSize)
|
||||
return available / entryLen
|
||||
}
|
||||
|
||||
// Push reserves "payloadSize" bytes for transmission in the pipe. The caller
|
||||
// populates the returned slice with the data to be transferred and enventually
|
||||
// calls Flush() to make the data visible to the reader, or Abort() to make the
|
||||
// pipe forget all Push() calls since the last Flush().
|
||||
//
|
||||
// The returned slice is available until Flush() or Abort() is next called.
|
||||
// After that, it must not be touched.
|
||||
func (t *Tx) Push(payloadSize uint64) []byte {
|
||||
// Fail request if we know we will never have enough room.
|
||||
if payloadSize > t.maxPayloadSize {
|
||||
return nil
|
||||
}
|
||||
|
||||
// True if TxPipe currently has a pushed message, i.e., it is not
|
||||
// Flush()'ed.
|
||||
messageAhead := t.next != t.tail
|
||||
totalLen := payloadToSlotSize(payloadSize)
|
||||
newNext := t.next + totalLen
|
||||
nextWrap := (t.next & revolutionMask) | uint64(len(t.p.buffer))
|
||||
if int64(newNext-nextWrap) >= 0 {
|
||||
// The new buffer would overflow the pipe, so we push a wrapping
|
||||
// slot, then try to add the actual slot to the front of the
|
||||
// pipe.
|
||||
newNext = (newNext & revolutionMask) + jump
|
||||
if !t.reclaim(newNext) {
|
||||
return nil
|
||||
}
|
||||
wrappingPayloadSize := slotToPayloadSize(newNext - t.next)
|
||||
oldNext := t.next
|
||||
t.next = newNext
|
||||
if messageAhead {
|
||||
t.p.write(oldNext, wrappingPayloadSize)
|
||||
} else {
|
||||
t.tailHeader = wrappingPayloadSize
|
||||
t.Flush()
|
||||
}
|
||||
return t.Push(payloadSize)
|
||||
}
|
||||
|
||||
// Check that we have enough room for the buffer.
|
||||
if !t.reclaim(newNext) {
|
||||
return nil
|
||||
}
|
||||
|
||||
if messageAhead {
|
||||
t.p.write(t.next, payloadSize)
|
||||
} else {
|
||||
t.tailHeader = payloadSize
|
||||
}
|
||||
|
||||
// Grab the buffer before updating t.next.
|
||||
b := t.p.data(t.next, payloadSize)
|
||||
t.next = newNext
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
// reclaim attempts to advance the head until at least newNext. If the head is
|
||||
// already at or beyond newNext, nothing happens and true is returned; otherwise
|
||||
// it tries to reclaim slots that have already been consumed by the receive end
|
||||
// of the pipe (they will be marked as free) and returns a boolean indicating
|
||||
// whether it was successful in reclaiming enough slots.
|
||||
func (t *Tx) reclaim(newNext uint64) bool {
|
||||
for int64(newNext-t.head) > 0 {
|
||||
// Can't reclaim if slot is not free.
|
||||
header := t.p.readAtomic(t.head)
|
||||
if header&slotFree == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
payloadSize := header & slotSizeMask
|
||||
newHead := t.head + payloadToSlotSize(payloadSize)
|
||||
|
||||
// Check newHead is within bounds and valid.
|
||||
if int64(newHead-t.tail) > int64(jump) || newHead&offsetMask >= uint64(len(t.p.buffer)) {
|
||||
return false
|
||||
}
|
||||
|
||||
t.head = newHead
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Abort causes all Push() calls since the last Flush() to be forgotten and
|
||||
// therefore they will not be made visible to the receiver.
|
||||
func (t *Tx) Abort() {
|
||||
t.next = t.tail
|
||||
}
|
||||
|
||||
// Flush causes all buffers pushed since the last Flush() [or Abort(), whichever
|
||||
// is the most recent] to be made visible to the receiver.
|
||||
func (t *Tx) Flush() {
|
||||
if t.next == t.tail {
|
||||
// Nothing to do if there are no pushed buffers.
|
||||
return
|
||||
}
|
||||
|
||||
if t.next != t.head {
|
||||
// The receiver will spin in t.next, so we must make sure that
|
||||
// the slotFree bit is set.
|
||||
t.p.write(t.next, slotFree)
|
||||
}
|
||||
|
||||
t.p.writeAtomic(t.tail, t.tailHeader)
|
||||
t.tail = t.next
|
||||
}
|
||||
|
||||
// Bytes returns the byte slice on which the pipe operates.
|
||||
func (t *Tx) Bytes() []byte {
|
||||
return t.p.buffer
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue