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
41
pkg/rawfile/blockingpoll_amd64.s
Normal file
41
pkg/rawfile/blockingpoll_amd64.s
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
// 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.
|
||||
|
||||
#include "textflag.h"
|
||||
|
||||
// BlockingPoll makes the ppoll() syscall while calling the version of
|
||||
// entersyscall that relinquishes the P so that other Gs can run. This is meant
|
||||
// to be called in cases when the syscall is expected to block.
|
||||
//
|
||||
// func BlockingPoll(fds *PollEvent, nfds int, timeout *syscall.Timespec) (n int, err syscall.Errno)
|
||||
TEXT ·BlockingPoll(SB),NOSPLIT|NOFRAME,$0-40
|
||||
CALL ·callEntersyscallblock(SB)
|
||||
MOVQ fds+0(FP), DI
|
||||
MOVQ nfds+8(FP), SI
|
||||
MOVQ timeout+16(FP), DX
|
||||
MOVQ $0x0, R10 // sigmask parameter which isn't used here
|
||||
MOVQ $0x10f, AX // SYS_PPOLL
|
||||
SYSCALL
|
||||
CMPQ AX, $0xfffffffffffff002
|
||||
JLS ok
|
||||
MOVQ $-1, ret+24(FP)
|
||||
NEGQ AX
|
||||
MOVQ AX, ret1+32(FP)
|
||||
CALL ·callExitsyscall(SB)
|
||||
RET
|
||||
ok:
|
||||
MOVQ AX, ret+24(FP)
|
||||
MOVQ $0, ret1+32(FP)
|
||||
CALL ·callExitsyscall(SB)
|
||||
RET
|
||||
42
pkg/rawfile/blockingpoll_arm64.s
Normal file
42
pkg/rawfile/blockingpoll_arm64.s
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
// 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.
|
||||
|
||||
#include "textflag.h"
|
||||
|
||||
// BlockingPoll makes the ppoll() syscall while calling the version of
|
||||
// entersyscall that relinquishes the P so that other Gs can run. This is meant
|
||||
// to be called in cases when the syscall is expected to block.
|
||||
//
|
||||
// func BlockingPoll(fds *PollEvent, nfds int, timeout *syscall.Timespec) (n int, err syscall.Errno)
|
||||
TEXT ·BlockingPoll(SB),NOSPLIT,$0-40
|
||||
BL ·callEntersyscallblock(SB)
|
||||
MOVD fds+0(FP), R0
|
||||
MOVD nfds+8(FP), R1
|
||||
MOVD timeout+16(FP), R2
|
||||
MOVD $0x0, R3 // sigmask parameter which isn't used here
|
||||
MOVD $0x49, R8 // SYS_PPOLL
|
||||
SVC
|
||||
CMP $0xfffffffffffff002, R0
|
||||
BLS ok
|
||||
MOVD $-1, R1
|
||||
MOVD R1, ret+24(FP)
|
||||
NEG R0, R0
|
||||
MOVD R0, ret1+32(FP)
|
||||
BL ·callExitsyscall(SB)
|
||||
RET
|
||||
ok:
|
||||
MOVD R0, ret+24(FP)
|
||||
MOVD $0, ret1+32(FP)
|
||||
BL ·callExitsyscall(SB)
|
||||
RET
|
||||
33
pkg/rawfile/blockingpoll_noyield_unsafe.go
Normal file
33
pkg/rawfile/blockingpoll_noyield_unsafe.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
// 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.
|
||||
|
||||
//go:build linux && !amd64 && !arm64
|
||||
// +build linux,!amd64,!arm64
|
||||
|
||||
package rawfile
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// BlockingPoll is just a stub function that forwards to the ppoll() system call
|
||||
// on non-amd64 and non-arm64 platforms.
|
||||
func BlockingPoll(fds *PollEvent, nfds int, timeout *unix.Timespec) (int, unix.Errno) {
|
||||
n, _, e := unix.Syscall6(unix.SYS_PPOLL, uintptr(unsafe.Pointer(fds)),
|
||||
uintptr(nfds), uintptr(unsafe.Pointer(timeout)), 0, 0, 0)
|
||||
|
||||
return int(n), e
|
||||
}
|
||||
69
pkg/rawfile/blockingpoll_yield_unsafe.go
Normal file
69
pkg/rawfile/blockingpoll_yield_unsafe.go
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
// 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.
|
||||
|
||||
//go:build ((linux && amd64) || (linux && arm64)) && go1.18
|
||||
// +build linux,amd64 linux,arm64
|
||||
// +build go1.18
|
||||
|
||||
// //go:linkname directives type-checked by checklinkname. Any other
|
||||
// non-linkname assumptions outside the Go 1 compatibility guarantee should
|
||||
// have an accompanied vet check or version guard build tag.
|
||||
|
||||
package rawfile
|
||||
|
||||
import (
|
||||
_ "unsafe" // for go:linkname
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// BlockingPoll on amd64/arm64 makes the ppoll() syscall while calling the
|
||||
// version of entersyscall that relinquishes the P so that other Gs can
|
||||
// run. This is meant to be called in cases when the syscall is expected to
|
||||
// block. On non amd64/arm64 platforms it just forwards to the ppoll() system
|
||||
// call.
|
||||
//
|
||||
//go:noescape
|
||||
func BlockingPoll(fds *PollEvent, nfds int, timeout *unix.Timespec) (int, unix.Errno)
|
||||
|
||||
// Use go:linkname to call into the runtime. As of Go 1.13 this has to
|
||||
// be done from Go code so that we make an ABIInternal call to an
|
||||
// ABIInternal function; see https://golang.org/issue/27539.
|
||||
|
||||
// We need to call both entersyscallblock and exitsyscall this way so
|
||||
// that the runtime's check on the stack pointer lines up.
|
||||
|
||||
// Note that calling an unexported function in the runtime package is
|
||||
// unsafe and this hack is likely to break in future Go releases.
|
||||
|
||||
//go:linkname entersyscallblock runtime.entersyscallblock
|
||||
func entersyscallblock()
|
||||
|
||||
//go:linkname exitsyscall runtime.exitsyscall
|
||||
func exitsyscall()
|
||||
|
||||
// These forwarding functions must be nosplit because 1) we must
|
||||
// disallow preemption between entersyscallblock and exitsyscall, and
|
||||
// 2) we have an untyped assembly frame on the stack which can not be
|
||||
// grown or moved.
|
||||
|
||||
//go:nosplit
|
||||
func callEntersyscallblock() {
|
||||
entersyscallblock()
|
||||
}
|
||||
|
||||
//go:nosplit
|
||||
func callExitsyscall() {
|
||||
exitsyscall()
|
||||
}
|
||||
233
pkg/rawfile/rawfile_unsafe.go
Normal file
233
pkg/rawfile/rawfile_unsafe.go
Normal file
|
|
@ -0,0 +1,233 @@
|
|||
// 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.
|
||||
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
// Package rawfile contains utilities for using raw host files on Linux hosts.
|
||||
package rawfile
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// SizeofIovec is the size of a unix.Iovec in bytes.
|
||||
const SizeofIovec = unsafe.Sizeof(unix.Iovec{})
|
||||
|
||||
// MaxIovs is UIO_MAXIOV, the maximum number of iovecs that may be passed to a
|
||||
// host system call in a single array.
|
||||
const MaxIovs = 1024
|
||||
|
||||
// IovecFromBytes returns a unix.Iovec representing bs.
|
||||
//
|
||||
// Preconditions: len(bs) > 0.
|
||||
func IovecFromBytes(bs []byte) unix.Iovec {
|
||||
iov := unix.Iovec{
|
||||
Base: &bs[0],
|
||||
}
|
||||
iov.SetLen(len(bs))
|
||||
return iov
|
||||
}
|
||||
|
||||
func bytesFromIovec(iov unix.Iovec) (bs []byte) {
|
||||
sh := (*reflect.SliceHeader)(unsafe.Pointer(&bs))
|
||||
sh.Data = uintptr(unsafe.Pointer(iov.Base))
|
||||
sh.Len = int(iov.Len)
|
||||
sh.Cap = int(iov.Len)
|
||||
return
|
||||
}
|
||||
|
||||
// AppendIovecFromBytes returns append(iovs, IovecFromBytes(bs)). If len(bs) ==
|
||||
// 0, AppendIovecFromBytes returns iovs without modification. If len(iovs) >=
|
||||
// max, AppendIovecFromBytes replaces the final iovec in iovs with one that
|
||||
// also includes the contents of bs. Note that this implies that
|
||||
// AppendIovecFromBytes is only usable when the returned iovec slice is used as
|
||||
// the source of a write.
|
||||
func AppendIovecFromBytes(iovs []unix.Iovec, bs []byte, max int) []unix.Iovec {
|
||||
if len(bs) == 0 {
|
||||
return iovs
|
||||
}
|
||||
if len(iovs) < max {
|
||||
return append(iovs, IovecFromBytes(bs))
|
||||
}
|
||||
iovs[len(iovs)-1] = IovecFromBytes(append(bytesFromIovec(iovs[len(iovs)-1]), bs...))
|
||||
return iovs
|
||||
}
|
||||
|
||||
// MMsgHdr represents the mmsg_hdr structure required by recvmmsg() on linux.
|
||||
type MMsgHdr struct {
|
||||
Msg unix.Msghdr
|
||||
Len uint32
|
||||
_ [4]byte
|
||||
}
|
||||
|
||||
// SizeofMMsgHdr is the size of a MMsgHdr in bytes.
|
||||
const SizeofMMsgHdr = unsafe.Sizeof(MMsgHdr{})
|
||||
|
||||
// GetMTU determines the MTU of a network interface device.
|
||||
func GetMTU(name string) (uint32, error) {
|
||||
fd, err := unix.Socket(unix.AF_UNIX, unix.SOCK_DGRAM, 0)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
defer unix.Close(fd)
|
||||
|
||||
var ifreq struct {
|
||||
name [16]byte
|
||||
mtu int32
|
||||
_ [20]byte
|
||||
}
|
||||
|
||||
copy(ifreq.name[:], name)
|
||||
_, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), unix.SIOCGIFMTU, uintptr(unsafe.Pointer(&ifreq)))
|
||||
if errno != 0 {
|
||||
return 0, errno
|
||||
}
|
||||
|
||||
return uint32(ifreq.mtu), nil
|
||||
}
|
||||
|
||||
// NonBlockingWrite writes the given buffer to a file descriptor. It fails if
|
||||
// partial data is written.
|
||||
func NonBlockingWrite(fd int, buf []byte) unix.Errno {
|
||||
var ptr unsafe.Pointer
|
||||
if len(buf) > 0 {
|
||||
ptr = unsafe.Pointer(&buf[0])
|
||||
}
|
||||
|
||||
_, _, e := unix.RawSyscall(unix.SYS_WRITE, uintptr(fd), uintptr(ptr), uintptr(len(buf)))
|
||||
return e
|
||||
}
|
||||
|
||||
// NonBlockingWriteIovec writes iovec to a file descriptor in a single unix.
|
||||
// It fails if partial data is written.
|
||||
func NonBlockingWriteIovec(fd int, iovec []unix.Iovec) unix.Errno {
|
||||
iovecLen := uintptr(len(iovec))
|
||||
_, _, e := unix.RawSyscall(unix.SYS_WRITEV, uintptr(fd), uintptr(unsafe.Pointer(&iovec[0])), iovecLen)
|
||||
return e
|
||||
}
|
||||
|
||||
// NonBlockingSendMMsg sends multiple messages on a socket.
|
||||
func NonBlockingSendMMsg(fd int, msgHdrs []MMsgHdr) (int, unix.Errno) {
|
||||
n, _, e := unix.RawSyscall6(unix.SYS_SENDMMSG, uintptr(fd), uintptr(unsafe.Pointer(&msgHdrs[0])), uintptr(len(msgHdrs)), unix.MSG_DONTWAIT, 0, 0)
|
||||
return int(n), e
|
||||
}
|
||||
|
||||
// PollEvent represents the pollfd structure passed to a poll() system call.
|
||||
type PollEvent struct {
|
||||
FD int32
|
||||
Events int16
|
||||
Revents int16
|
||||
}
|
||||
|
||||
// BlockingRead reads from a file descriptor that is set up as non-blocking.
|
||||
// If no data is available, it will block in a poll() syscall until the file
|
||||
// descriptor becomes readable.
|
||||
func BlockingRead(fd int, b []byte) (int, unix.Errno) {
|
||||
for {
|
||||
n, _, e := unix.RawSyscall(unix.SYS_READ, uintptr(fd), uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)))
|
||||
if e == 0 {
|
||||
return int(n), 0
|
||||
}
|
||||
|
||||
event := PollEvent{
|
||||
FD: int32(fd),
|
||||
Events: 1, // POLLIN
|
||||
}
|
||||
|
||||
_, e = BlockingPoll(&event, 1, nil)
|
||||
if e != 0 && e != unix.EINTR {
|
||||
return 0, e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// BlockingReadvUntilStopped reads from a file descriptor that is set up as
|
||||
// non-blocking and stores the data in a list of iovecs buffers. If no data is
|
||||
// available, it will block in a poll() syscall until the file descriptor
|
||||
// becomes readable or stop is signalled (efd becomes readable). Returns -1 in
|
||||
// the latter case.
|
||||
func BlockingReadvUntilStopped(efd int, fd int, iovecs []unix.Iovec) (int, unix.Errno) {
|
||||
for {
|
||||
n, _, e := unix.RawSyscall(unix.SYS_READV, uintptr(fd), uintptr(unsafe.Pointer(&iovecs[0])), uintptr(len(iovecs)))
|
||||
if e == 0 {
|
||||
return int(n), 0
|
||||
}
|
||||
if e != 0 && e != unix.EWOULDBLOCK {
|
||||
return 0, e
|
||||
}
|
||||
stopped, e := BlockingPollUntilStopped(efd, fd, unix.POLLIN)
|
||||
if stopped {
|
||||
return -1, e
|
||||
}
|
||||
if e != 0 && e != unix.EINTR {
|
||||
return 0, e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// BlockingRecvMMsgUntilStopped reads from a file descriptor that is set up as
|
||||
// non-blocking and stores the received messages in a slice of MMsgHdr
|
||||
// structures. If no data is available, it will block in a poll() syscall until
|
||||
// the file descriptor becomes readable or stop is signalled (efd becomes
|
||||
// readable). Returns -1 in the latter case.
|
||||
func BlockingRecvMMsgUntilStopped(efd int, fd int, msgHdrs []MMsgHdr) (int, unix.Errno) {
|
||||
for {
|
||||
n, _, e := unix.RawSyscall6(unix.SYS_RECVMMSG, uintptr(fd), uintptr(unsafe.Pointer(&msgHdrs[0])), uintptr(len(msgHdrs)), unix.MSG_DONTWAIT, 0, 0)
|
||||
if e == 0 {
|
||||
return int(n), e
|
||||
}
|
||||
|
||||
if e != 0 && e != unix.EWOULDBLOCK {
|
||||
return 0, e
|
||||
}
|
||||
|
||||
stopped, e := BlockingPollUntilStopped(efd, fd, unix.POLLIN)
|
||||
if stopped {
|
||||
return -1, e
|
||||
}
|
||||
if e != 0 && e != unix.EINTR {
|
||||
return 0, e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// BlockingPollUntilStopped polls for events on fd or until a stop is signalled
|
||||
// on the event fd efd. Returns true if stopped, i.e., efd has event POLLIN.
|
||||
func BlockingPollUntilStopped(efd int, fd int, events int16) (bool, unix.Errno) {
|
||||
pevents := [...]PollEvent{
|
||||
{
|
||||
FD: int32(efd),
|
||||
Events: unix.POLLIN,
|
||||
},
|
||||
{
|
||||
FD: int32(fd),
|
||||
Events: events,
|
||||
},
|
||||
}
|
||||
_, _, errno := unix.Syscall6(unix.SYS_PPOLL, uintptr(unsafe.Pointer(&pevents[0])), uintptr(len(pevents)), 0, 0, 0, 0)
|
||||
if errno != 0 {
|
||||
return pevents[0].Revents&unix.POLLIN != 0, errno
|
||||
}
|
||||
|
||||
if pevents[1].Revents&unix.POLLHUP != 0 || pevents[1].Revents&unix.POLLERR != 0 {
|
||||
errno = unix.ECONNRESET
|
||||
}
|
||||
|
||||
return pevents[0].Revents&unix.POLLIN != 0, errno
|
||||
}
|
||||
11
pkg/rawfile/rawfile_unsafe_state_autogen.go
Normal file
11
pkg/rawfile/rawfile_unsafe_state_autogen.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
// automatically generated by stateify.
|
||||
|
||||
//go:build linux && !amd64 && !arm64 && ((linux && amd64) || (linux && arm64)) && go1.18 && linux
|
||||
// +build linux
|
||||
// +build !amd64
|
||||
// +build !arm64
|
||||
// +build linux,amd64 linux,arm64
|
||||
// +build go1.18
|
||||
// +build linux
|
||||
|
||||
package rawfile
|
||||
Loading…
Add table
Add a link
Reference in a new issue