From 95e16f01c1d8d1771cef401a56841a99a194a419 Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Wed, 17 Jul 2019 23:55:18 +0200 Subject: [PATCH] rpc/dataconn: audit and comment use of unsafe for readv usage Go 1.13 will add a more precise escape analysis: https://tip.golang.org/doc/go1.13#compiler Reviewed usage of unsafe to ensure we adhere to the unsafe.Pointer safety rules: https://tip.golang.org/pkg/unsafe/#Pointer --- rpc/dataconn/timeoutconn/timeoutconn.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/rpc/dataconn/timeoutconn/timeoutconn.go b/rpc/dataconn/timeoutconn/timeoutconn.go index 46048d8..3b57b79 100644 --- a/rpc/dataconn/timeoutconn/timeoutconn.go +++ b/rpc/dataconn/timeoutconn/timeoutconn.go @@ -244,6 +244,14 @@ func (c Conn) readv(rawConn syscall.RawConn, iovecs []syscall.Iovec) (n int64, e func (c Conn) doOneReadv(rawConn syscall.RawConn, iovecs *[]syscall.Iovec) (n int64, err error) { rawReadErr := rawConn.Read(func(fd uintptr) (done bool) { // iovecs, n and err must not be shadowed! + + // NOTE: unsafe.Pointer safety rules + // https://tip.golang.org/pkg/unsafe/#Pointer + // + // (4) Conversion of a Pointer to a uintptr when calling syscall.Syscall. + // ... + // uintptr() conversions must appear within the syscall.Syscall argument list. + // (even though we are not the escape analysis Likely not ) thisReadN, _, errno := syscall.Syscall( syscall.SYS_READV, fd, @@ -268,6 +276,12 @@ func (c Conn) doOneReadv(rawConn syscall.RawConn, iovecs *[]syscall.Iovec) (n in left -= int64((*iovecs)[0].Len) *iovecs = (*iovecs)[1:] } else { + // NOTE: unsafe.Pointer safety rules + // https://tip.golang.org/pkg/unsafe/#Pointer + // (3) Conversion of a Pointer to a uintptr and back, with arithmetic. + // ... + // Note that both conversions must appear in the same expression, + // with only the intervening arithmetic between them: (*iovecs)[0].Base = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer((*iovecs)[0].Base)) + uintptr(left))) (*iovecs)[0].Len = uint64(curVecNewLength) break // inner