mirror of
https://github.com/fumiama/water.git
synced 2026-06-05 02:00:29 +08:00
fix err judge
This commit is contained in:
67
alignment_windows_test.go
Normal file
67
alignment_windows_test.go
Normal file
@@ -0,0 +1,67 @@
|
||||
/* SPDX-License-Identifier: MIT
|
||||
*
|
||||
* Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
|
||||
*/
|
||||
|
||||
package water
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func checkAlignment(t *testing.T, name string, offset uintptr) {
|
||||
t.Helper()
|
||||
if offset%8 != 0 {
|
||||
t.Errorf("offset of %q within struct is %d bytes, which does not align to 64-bit word boundaries (missing %d bytes). Atomic operations will crash on 32-bit systems.", name, offset, 8-(offset%8))
|
||||
}
|
||||
}
|
||||
|
||||
// TestRateJugglerAlignment checks that atomically-accessed fields are
|
||||
// aligned to 64-bit boundaries, as required by the atomic package.
|
||||
//
|
||||
// Unfortunately, violating this rule on 32-bit platforms results in a
|
||||
// hard segfault at runtime.
|
||||
func TestRateJugglerAlignment(t *testing.T) {
|
||||
var r rateJuggler
|
||||
|
||||
typ := reflect.TypeOf(&r).Elem()
|
||||
t.Logf("Peer type size: %d, with fields:", typ.Size())
|
||||
for i := 0; i < typ.NumField(); i++ {
|
||||
field := typ.Field(i)
|
||||
t.Logf("\t%30s\toffset=%3v\t(type size=%3d, align=%d)",
|
||||
field.Name,
|
||||
field.Offset,
|
||||
field.Type.Size(),
|
||||
field.Type.Align(),
|
||||
)
|
||||
}
|
||||
|
||||
checkAlignment(t, "rateJuggler.current", unsafe.Offsetof(r.current))
|
||||
checkAlignment(t, "rateJuggler.nextByteCount", unsafe.Offsetof(r.nextByteCount))
|
||||
checkAlignment(t, "rateJuggler.nextStartTime", unsafe.Offsetof(r.nextStartTime))
|
||||
}
|
||||
|
||||
// TestNativeTunAlignment checks that atomically-accessed fields are
|
||||
// aligned to 64-bit boundaries, as required by the atomic package.
|
||||
//
|
||||
// Unfortunately, violating this rule on 32-bit platforms results in a
|
||||
// hard segfault at runtime.
|
||||
func TestNativeTunAlignment(t *testing.T) {
|
||||
var tun wintunRWC
|
||||
|
||||
typ := reflect.TypeOf(&tun).Elem()
|
||||
t.Logf("Peer type size: %d, with fields:", typ.Size())
|
||||
for i := 0; i < typ.NumField(); i++ {
|
||||
field := typ.Field(i)
|
||||
t.Logf("\t%30s\toffset=%3v\t(type size=%3d, align=%d)",
|
||||
field.Name,
|
||||
field.Offset,
|
||||
field.Type.Size(),
|
||||
field.Type.Align(),
|
||||
)
|
||||
}
|
||||
|
||||
checkAlignment(t, "NativeTun.rate", unsafe.Offsetof(tun.rate))
|
||||
}
|
||||
@@ -326,10 +326,10 @@ func (rate *rateJuggler) update(packetLen uint64) {
|
||||
type wintunRWC struct {
|
||||
ad wintun.Adapter
|
||||
s wintun.Session
|
||||
readwait windows.Handle
|
||||
readbuf []byte
|
||||
rate rateJuggler
|
||||
readwait windows.Handle
|
||||
mu sync.Mutex
|
||||
readbuf []byte
|
||||
isclosed bool
|
||||
}
|
||||
|
||||
@@ -343,6 +343,7 @@ func (w *wintunRWC) Write(b []byte) (int, error) {
|
||||
w.rate.update(uint64(len(b)))
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
ALLOC:
|
||||
packet, err := w.s.AllocateSendPacket(len(b))
|
||||
switch err {
|
||||
case nil:
|
||||
@@ -350,6 +351,11 @@ func (w *wintunRWC) Write(b []byte) (int, error) {
|
||||
w.s.SendPacket(packet)
|
||||
return len(b), nil
|
||||
case windows.ERROR_HANDLE_EOF:
|
||||
w.s.End()
|
||||
w.s, err = w.ad.StartSession(0x800000) // Ring capacity, 8 MiB
|
||||
if err == nil {
|
||||
goto ALLOC
|
||||
}
|
||||
return 0, os.ErrClosed
|
||||
case windows.ERROR_BUFFER_OVERFLOW:
|
||||
return 0, nil // Dropping when ring is full.
|
||||
@@ -407,6 +413,11 @@ RETRY:
|
||||
w.mu.Lock()
|
||||
continue
|
||||
}
|
||||
w.s.End()
|
||||
w.s, err = w.ad.StartSession(0x800000) // Ring capacity, 8 MiB
|
||||
if err == nil {
|
||||
continue
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user