Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,7 @@ What a target lacks, rbgo leaves out the way MRI does:

- **The store gems** — SQLite3, Sequel, ActiveRecord's adapter, Bolt, Bleve, Etcd and Confd — need modernc.org/sqlite, which has a port for darwin/{amd64,arm64}, freebsd/{386,amd64,arm,arm64}, linux/{386,amd64,arm,arm64,loong64,ppc64le,riscv64,s390x}, netbsd/amd64, openbsd/{amd64,arm64} and windows. Elsewhere (wasm included) `require` still works and the modules exist, and the first real operation raises `NotImplementedError: sqlite3 is not supported on linux/mips64le` (or the gem and target in question).
- **Errno**: a name the system's `<errno.h>` lacks is still a constant, naming `Errno::NOERROR`, as MRI's `undefined_error` does: `EMULTIHOP` and `ENOLINK` on OpenBSD, `ENOTRECOVERABLE` and `EOWNERDEAD` on NetBSD and DragonFly.
- **Errno on Windows**: a failed call's Win32 or Winsock code picks its class through MRI's own table (`errmap` and `rb_w32_map_errno` in `win32/win32.c`), so a missing directory is `Errno::ENOENT` and a refused connection `Errno::ECONNREFUSED`, as under MRI; a Win32 code absent from the table is `Errno::EINVAL`. Eight Winsock rows (`ESHUTDOWN`, `ETOOMANYREFS`, `EHOSTDOWN`, `EPFNOSUPPORT`, `ESOCKTNOSUPPORT`, `EPROCLIM`, `EUSERS`, `EREMOTE`) name classes rbgo has no number for on any target, so those failures raise plain `SystemCallError` ([#980](https://github.com/go-embedded-ruby/ruby/issues/980)).
- **`Process::RLIMIT_*`** exist only where the system has the resource: no `RLIMIT_AS` on OpenBSD, no `RLIMIT_MEMLOCK` on AIX, only `RLIMIT_AS` of the four optional ones on Solaris and illumos.
- **`File#flock`** and PStore's lock use `flock(2)`; AIX has none, so there it is emulated with `fcntl(2)` record locks, exactly as MRI's `missing/flock.c` does.
- **plan9 (386, amd64, arm) compiles and vets, and is untested at run time.** Its `syscall` package has no errno numbers (`syscall.EINVAL` is a string error there), no `Kill`, `Flock`, `Getrlimit`, `Umask` or `Stat_t`, so rbgo has a plan9 variant of each seam. Nothing has been run on a Plan 9 kernel; the one part with tests is the error-string mapping, which is pure Go and runs on every platform. What that means:
Expand Down
27 changes: 22 additions & 5 deletions internal/vm/errno_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,37 @@ import (
// *os.SyscallError, *net.OpError …). ok is false for an error with no errno
// behind it. The Plan 9 counterpart (errno_plan9.go) has no numbers to dig out and
// derives one from the error string instead.
//
// The number is the one of the class MRI raises, which on Windows is not the
// code the call failed with: a Windows syscall.Errno is the Win32 code itself,
// and MRI maps it through rb_w32_map_errno before anything raises it, so
// ERROR_PATH_NOT_FOUND (3) is Errno::ENOENT. mapHostErrno does that mapping
// there and is the identity elsewhere. Doing it here, once, means no raiser can
// look a raw Win32 code up in errnoClasses (go-embedded-ruby/ruby#980).
//
// ⚠ Windows: Go defines syscall.ENOENT as ERROR_FILE_NOT_FOUND (2) and
// syscall.ENOTDIR as ERROR_PATH_NOT_FOUND (3), so an ENOTDIR that Go builds
// itself (os.MkdirAll through a file, os.ReadDir on one, filepath.EvalSymlinks)
// is the very number Windows uses for a missing path, and is mapped to ENOENT
// as MRI maps that code. No raiser here takes its error from those three
// today; one that does must decide the class itself.
func errnoOfError(err error) (int64, bool) {
var eno syscall.Errno
if errors.As(err, &eno) {
return int64(eno), true
return mapHostErrno(int64(eno)), true
}
return 0, false
}

// errIsErrno reports whether err is the Errno class `name` (an errnoNumbers key):
// errors.Is against the host's syscall.Errno of that number, so it answers exactly
// what errors.Is(err, syscall.<name>) would. It exists so the call sites compile
// on Plan 9, whose syscall package lacks most of those constants.
// whether errnoOfError maps it to that class's number. Off Windows that is
// errors.Is(err, syscall.<name>) for the first errno in the chain; on Windows
// it also sees that ERROR_DIR_NOT_EMPTY is ENOTEMPTY, which errors.Is cannot.
// It exists so the call sites compile on Plan 9, whose syscall package lacks
// most of those constants.
func errIsErrno(err error, name string) bool {
return errors.Is(err, syscall.Errno(errnoNumbers[name]))
eno, ok := errnoOfError(err)
return ok && eno == errnoNumbers[name]
}

// errnoText is the message sysFail embeds for an errno: errnoMessage of the host's
Expand Down
45 changes: 45 additions & 0 deletions internal/vm/errno_mapped_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

package vm_test

import "testing"

// TestAFailedCallRaisesMRIsClassOnEveryHost (go-embedded-ruby/ruby#980): the
// expected output is MRI 4.0.7's on darwin, and MRI raises the same classes on
// Windows because it maps each Win32 code through errmap first. These are the
// rows where the raw Win32 code named another class, or none:
//
// - a path through a missing directory fails with ERROR_PATH_NOT_FOUND (3),
// which is Go's syscall.ENOTDIR on Windows, so it read as Errno::ENOTDIR;
// - rmdir of a non-empty directory fails with ERROR_DIR_NOT_EMPTY (145),
// which errors.Is never matched to ENOTEMPTY, so it fell to Errno::ENOENT.
//
// On the POSIX lanes these pass with or without the mapping; the Windows lane
// is the one that says whether it holds.
func TestAFailedCallRaisesMRIsClassOnEveryHost(t *testing.T) {
checkCases(t, []runCase{
{`require "tmpdir"
Dir.mktmpdir do |d|
begin
Dir.chdir(File.join(d, "missing", "sub"))
rescue SystemCallError => e
p e.class
end
full = File.join(d, "full")
Dir.mkdir(full)
File.write(File.join(full, "f"), "x")
begin
Dir.rmdir(full)
rescue SystemCallError => e
p e.class
end
begin
File.truncate(File.join(d, "missing", "sub"), 0)
rescue SystemCallError => e
p e.class
end
end`, "Errno::ENOENT\nErrno::ENOTEMPTY\nErrno::ENOENT\n"},
})
}
99 changes: 90 additions & 9 deletions internal/vm/errno_win32_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import (
//
// ERROR_INVALID_HANDLE appears twice in errmap, as EBADF and then EINVAL; the
// first row is the one rb_w32_map_errno's loop returns, so it alone is here.
// The WSA rows are left out: their codes are at or above WSABASEERR, which
// mapHostErrno keeps as they are, as rb_w32_map_errno does for a WSA code
// absent from the table.
// The WSA rows are wsaErrnos.
var win32Errnos = map[syscall.Errno]string{
windows.ERROR_INVALID_FUNCTION: "EINVAL",
windows.ERROR_FILE_NOT_FOUND: "ENOENT",
Expand Down Expand Up @@ -111,19 +109,102 @@ var win32Errnos = map[syscall.Errno]string{
windows.ERROR_CANT_RESOLVE_FILENAME: "ELOOP",
}

// wsaBaseErr is WSABASEERR: from there up a code is a socket error, or one of
// the numbers Go invented for the POSIX names, and is kept as it is.
// wsaErrnos is the rest of MRI's errmap (win32/win32.c:235-277, v4.0.9): the
// Winsock rows, all 43 of them, in errmap's order. A socket call fails with
// one of these, so without them a refused connection is a bare
// SystemCallError where MRI raises Errno::ECONNREFUSED.
//
// Eight targets (EHOSTDOWN, EPFNOSUPPORT, EPROCLIM, EREMOTE, ESHUTDOWN,
// ESOCKTNOSUPPORT, ETOOMANYREFS, EUSERS) have no number in rbgo on any
// target (errnoUndefinedEverywhere), so no class to land in: mapHostErrno
// keeps the WSA code for them, which raises SystemCallError, until the
// per-platform errno tables define them. TestWSARowsWithoutAClass pins that
// list, so defining one shows up there.
var wsaErrnos = map[syscall.Errno]string{
windows.WSAEINTR: "EINTR",
windows.WSAEBADF: "EBADF",
windows.WSAEACCES: "EACCES",
windows.WSAEFAULT: "EFAULT",
windows.WSAEINVAL: "EINVAL",
windows.WSAEMFILE: "EMFILE",
windows.WSAEWOULDBLOCK: "EWOULDBLOCK",
windows.WSAEINPROGRESS: "EINPROGRESS",
windows.WSAEALREADY: "EALREADY",
windows.WSAENOTSOCK: "ENOTSOCK",
windows.WSAEDESTADDRREQ: "EDESTADDRREQ",
windows.WSAEMSGSIZE: "EMSGSIZE",
windows.WSAEPROTOTYPE: "EPROTOTYPE",
windows.WSAENOPROTOOPT: "ENOPROTOOPT",
windows.WSAEPROTONOSUPPORT: "EPROTONOSUPPORT",
windows.WSAESOCKTNOSUPPORT: "ESOCKTNOSUPPORT",
windows.WSAEOPNOTSUPP: "EOPNOTSUPP",
windows.WSAEPFNOSUPPORT: "EPFNOSUPPORT",
windows.WSAEAFNOSUPPORT: "EAFNOSUPPORT",
windows.WSAEADDRINUSE: "EADDRINUSE",
windows.WSAEADDRNOTAVAIL: "EADDRNOTAVAIL",
windows.WSAENETDOWN: "ENETDOWN",
windows.WSAENETUNREACH: "ENETUNREACH",
windows.WSAENETRESET: "ENETRESET",
windows.WSAECONNABORTED: "ECONNABORTED",
windows.WSAECONNRESET: "ECONNRESET",
windows.WSAENOBUFS: "ENOBUFS",
windows.WSAEISCONN: "EISCONN",
windows.WSAENOTCONN: "ENOTCONN",
windows.WSAESHUTDOWN: "ESHUTDOWN",
windows.WSAETOOMANYREFS: "ETOOMANYREFS",
windows.WSAETIMEDOUT: "ETIMEDOUT",
windows.WSAECONNREFUSED: "ECONNREFUSED",
windows.WSAELOOP: "ELOOP",
windows.WSAENAMETOOLONG: "ENAMETOOLONG",
windows.WSAEHOSTDOWN: "EHOSTDOWN",
windows.WSAEHOSTUNREACH: "EHOSTUNREACH",
windows.WSAEPROCLIM: "EPROCLIM",
windows.WSAENOTEMPTY: "ENOTEMPTY",
windows.WSAEUSERS: "EUSERS",
windows.WSAEDQUOT: "EDQUOT",
windows.WSAESTALE: "ESTALE",
windows.WSAEREMOTE: "EREMOTE",
}

// wsaBaseErr is WSABASEERR: from there up a code absent from errmap is a
// socket error MRI has no class for, or one of the numbers Go invented for the
// POSIX names, and is kept as it is.
const wsaBaseErr = 10000

// mapHostErrno is rb_w32_map_errno: the errno number of the class MRI raises
// for the Win32 code eno. A code in errmap gets its class; a code at or above
// WSABASEERR is kept; any other code is EINVAL.
// WSABASEERR is kept; any other code is EINVAL. A row whose class rbgo has no
// number for keeps the code too (see wsaErrnos).
func mapHostErrno(eno int64) int64 {
if eno == 0 || eno >= wsaBaseErr {
if eno == 0 {
return 0
}
name, ok := win32Errnos[syscall.Errno(eno)]
if !ok {
name, ok = wsaErrnos[syscall.Errno(eno)]
}
if ok {
if n, ok := errnoNumberOf(name); ok {
return n
}
return eno
}
if name, ok := win32Errnos[syscall.Errno(eno)]; ok {
return errnoNumbers[name]
if eno >= wsaBaseErr {
return eno
}
return errnoNumbers["EINVAL"]
}

// errnoNumberOf is the number of the Errno class `name` names on this target:
// its own, or the one an alias (EWOULDBLOCK) shares. ok is false for a name
// with no number here.
func errnoNumberOf(name string) (int64, bool) {
if n, ok := errnoNumbers[name]; ok {
return n, true
}
if a, ok := errnoAliases[name]; ok {
n, ok := errnoNumbers[a]
return n, ok
}
return 0, false
}
57 changes: 55 additions & 2 deletions internal/vm/errno_win32_windows_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package vm

import (
"os"
"syscall"
"testing"

Expand Down Expand Up @@ -35,6 +36,14 @@ func TestMapHostErrnoIsRbW32MapErrno(t *testing.T) {
{windows.ERROR_INVALID_NAME, "EINVAL"},
// A number Go invented for a POSIX name: kept.
{syscall.ENOENT, "ENOENT"},
// The Winsock rows (go-embedded-ruby/ruby#980).
{windows.WSAECONNRESET, "ECONNRESET"},
{windows.WSAECONNREFUSED, "ECONNREFUSED"},
{windows.WSAENOTSOCK, "ENOTSOCK"},
{windows.WSAEMSGSIZE, "EMSGSIZE"},
{windows.WSAENOTEMPTY, "ENOTEMPTY"},
// EWOULDBLOCK is a second name for EAGAIN's class in rbgo.
{windows.WSAEWOULDBLOCK, "EAGAIN"},
}
for _, c := range cases {
if got := mapHostErrno(int64(c.eno)); got != errnoNumbers[c.want] {
Expand All @@ -44,7 +53,51 @@ func TestMapHostErrnoIsRbW32MapErrno(t *testing.T) {
if got := mapHostErrno(0); got != 0 {
t.Errorf("mapHostErrno(0) = %d, want 0", got)
}
if got := mapHostErrno(int64(windows.WSAECONNRESET)); got != int64(windows.WSAECONNRESET) {
t.Errorf("a WSA code was changed to %d", got)
// A WSA code absent from errmap is kept, as rb_w32_map_errno keeps it.
if got := mapHostErrno(int64(windows.WSAEDISCON)); got != int64(windows.WSAEDISCON) {
t.Errorf("WSAEDISCON was changed to %d", got)
}
}

// TestWSARowsWithoutAClass pins the errmap rows whose class rbgo has no number
// for: mapHostErrno keeps their WSA code. Defining one of those names on
// Windows moves it out of this list, and the test says which.
func TestWSARowsWithoutAClass(t *testing.T) {
want := map[string]bool{
"EHOSTDOWN": true, "EPFNOSUPPORT": true, "EPROCLIM": true, "EREMOTE": true,
"ESHUTDOWN": true, "ESOCKTNOSUPPORT": true, "ETOOMANYREFS": true, "EUSERS": true,
}
if len(wsaErrnos) != 43 {
t.Errorf("wsaErrnos has %d rows; errmap has 43 Winsock rows", len(wsaErrnos))
}
for code, name := range wsaErrnos {
got := mapHostErrno(int64(code))
_, named := errnoNumberOf(name)
switch {
case want[name] && named:
t.Errorf("%s now has a number; drop it from this list", name)
case want[name] && got != int64(code):
t.Errorf("%d (%s, no class) was changed to %d", code, name, got)
case !want[name] && !named:
t.Errorf("%s has no number on Windows; WSA code %d keeps its own", name, code)
case !want[name] && got == int64(code):
t.Errorf("%d (%s) was not mapped", code, name)
}
}
}

// TestErrnoOfErrorMapsTheWin32Code: the mapping is applied where the number
// is dug out, so every raiser sees MRI's class, not the raw code.
func TestErrnoOfErrorMapsTheWin32Code(t *testing.T) {
err := &os.PathError{Op: "open", Path: `C:\nope\x`, Err: windows.ERROR_PATH_NOT_FOUND}
if got, ok := errnoOfError(err); !ok || got != errnoNumbers["ENOENT"] {
t.Errorf("errnoOfError = %d, %v; want ENOENT (%d)", got, ok, errnoNumbers["ENOENT"])
}
dir := &os.PathError{Op: "remove", Path: `C:\d`, Err: windows.ERROR_DIR_NOT_EMPTY}
if !errIsErrno(dir, "ENOTEMPTY") {
t.Errorf("ERROR_DIR_NOT_EMPTY is not ENOTEMPTY")
}
if errIsErrno(dir, "ENOENT") {
t.Errorf("ERROR_DIR_NOT_EMPTY is ENOENT")
}
}
15 changes: 0 additions & 15 deletions internal/vm/errno_winsock_other.go

This file was deleted.

69 changes: 0 additions & 69 deletions internal/vm/errno_winsock_windows.go

This file was deleted.

Loading
Loading