runtime: change read and write to return negative errno value

The internal read and write functions used to return -1 on error;
change them to return a negative errno value instead.
This will be used by later CLs in this series.

For most targets this is a simplification, although for ones that call
into libc it is a complication.

Updates #27707

Change-Id: Id02bf9487f03e7e88e4f2b85e899e986738697ad
Reviewed-on: https://go-review.googlesource.com/c/go/+/171823
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
This commit is contained in:
Ian Lance Taylor 2019-04-05 11:42:37 -07:00
parent 6917b3c839
commit b653c878b1
30 changed files with 141 additions and 93 deletions

View file

@ -18,6 +18,7 @@ import (
"strings"
"syscall"
"testing"
"unsafe"
)
// sigquit is the signal to send to kill a hanging testdata program.
@ -33,6 +34,29 @@ func init() {
}
}
func TestBadOpen(t *testing.T) {
// make sure we get the correct error code if open fails. Same for
// read/write/close on the resulting -1 fd. See issue 10052.
nonfile := []byte("/notreallyafile")
fd := runtime.Open(&nonfile[0], 0, 0)
if fd != -1 {
t.Errorf("open(%q)=%d, want -1", nonfile, fd)
}
var buf [32]byte
r := runtime.Read(-1, unsafe.Pointer(&buf[0]), int32(len(buf)))
if got, want := r, -int32(syscall.EBADF); got != want {
t.Errorf("read()=%d, want %d", got, want)
}
w := runtime.Write(^uintptr(0), unsafe.Pointer(&buf[0]), int32(len(buf)))
if got, want := w, -int32(syscall.EBADF); got != want {
t.Errorf("write()=%d, want %d", got, want)
}
c := runtime.Close(-1)
if c != -1 {
t.Errorf("close()=%d, want -1", c)
}
}
func TestCrashDumpsAllThreads(t *testing.T) {
if *flagQuick {
t.Skip("-quick")