Skip to content
Merged
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -875,8 +875,10 @@ Otherwise:
| `Numeric#to_int` | not defined — `Complex(2.9, 0).to_int` raises `NoMethodError`; MRI returns `2` |
| `File::Stat#==` | `a == b` is `false` for two stats of the same file (MRI: `true`); `a.==(b)` answers `true` |
| `File#stat` on a file unlinked while open | raises `Errno::ENOENT`; MRI answers from the open descriptor. rbgo's streams are buffered by path and hold no descriptor |
| `IO#pwrite` / `IO#pread` on a file unlinked or replaced while open | both reach the file **by its path**, since streams hold no descriptor. A `#pwrite`'s bytes are on disk when it returns, and only those bytes: the stream's other pending writes stay pending, as with `pwrite(2)`. `#pread` sees another writer's bytes and this stream's own pending writes, which it flushes first, as `rb_io_pread` does ([#949](https://github.com/go-embedded-ruby/ruby/issues/949)). The difference is with a path that no longer names the opened file. Unlinked, both fall back to the stream's buffer, where MRI still reaches the old inode. Replaced, they reach the new file. Same root as the row above. Two deliberate divergences. First, `#pread`'s length is bounded by the file's size, so `f.pread(2**40, 0)` returns at once, where MRI 4.0.7 tries to allocate the full length. Second, a hole lives in memory when it has to: a `#pwrite` that would open a hole larger than 64 MiB past the end of the stream's in-memory copy is refused with `Errno::EFBIG` once the path is gone, where MRI writes a sparse file through its descriptor. When the file is reachable, the hole goes to disk as in MRI, and a refusal from the file (EFBIG, ENOSPC, EIO) is raised with its errno ([#973](https://github.com/go-embedded-ruby/ruby/issues/973)) |
| `IO#pwrite` / `IO#pread` on a file unlinked or replaced while open | both reach the file **by its path**, since streams hold no descriptor. A `#pwrite`'s bytes are on disk when it returns, and only those bytes: the stream's other pending writes stay pending, as with `pwrite(2)`. `#pread` sees another writer's bytes and this stream's own pending writes, which it flushes first, as `rb_io_pread` does ([#949](https://github.com/go-embedded-ruby/ruby/issues/949)). The difference is with a path that no longer names the opened file, unlinked or replaced. Both fall back to the stream's buffer and leave the path alone, as MRI does, but MRI's bytes reach the old inode, which other processes holding it would see. Same root as the row above. Two deliberate divergences. First, `#pread`'s length is bounded by the file's size, so `f.pread(2**40, 0)` returns at once, where MRI 4.0.7 tries to allocate the full length. Second, a hole lives in memory when it has to: a `#pwrite` that would open a hole larger than 64 MiB past the end of the stream's in-memory copy is refused with `Errno::EFBIG` once the path is gone, where MRI writes a sparse file through its descriptor. When the file is reachable, the hole goes to disk as in MRI, and a refusal from the file (EFBIG, ENOSPC, EIO) is raised with its errno ([#973](https://github.com/go-embedded-ruby/ruby/issues/973)) |
| `IO#ungetc` / `#ungetbyte` on a `File` longer than 8192 bytes | as in MRI, pushed-back bytes are read-buffer bytes and never reach the file. `#pos` subtracts them, and is negative when more was pushed back than read. A write, `#flush`, `#seek`, `#rewind`, `#truncate`, `#pwrite` or `File#size` on a writable stream drops them first. When the position was negative, the stream ends up where MRI's descriptor is: at the cursor after `#read`, `#readpartial`, `#read_nonblock`, a seek or a write, and at end-of-file after a buffered read (`#getc`, `#gets`, …), which reads ahead. `#readpartial` and `#read_nonblock` return no more than the pushback while some is left. `#sysread` and `#sysseek` refuse with "for buffered IO" ([#974](https://github.com/go-embedded-ruby/ruby/issues/974)). The divergence is MRI's read-buffer capacity: MRI reads 8192 bytes ahead, so on a larger file, pushing back more than was read raises `IOError: ungetbyte failed`. rbgo accepts it, and a following write lands at end-of-file. On a file of at most 8192 bytes the two agree |
| `IO#flush` on a `File` another writer changed | as in MRI, a flush puts on disk only the byte ranges this stream wrote, at their offsets (appended in append mode), so another writer's bytes elsewhere in the file, an append, or a truncation stand. A flush the system refuses raises its own errno (`Errno::ENOSPC`, `Errno::EISDIR`, …) ([#974](https://github.com/go-embedded-ruby/ruby/issues/974)). The divergence is a bound: past 256 separate unflushed ranges, rbgo merges them into one span, and a flush then rewrites the gaps between them with its own copy of those bytes. MRI's 8192-byte write buffer flushes long before that. Reaching the file by path is the same root as the rows above |
| a `File` whose path is unlinked, renamed over, or swapped for a symlink while it is open | as in MRI, its writes never reach what the path names now. A stream records the file it opened, and a flush, `#close`, `#pwrite`, `#truncate` or `sync = true` write checks the path against it before opening it and checks the opened file after, so a file put in its place, or the target of a symlink put there, is left alone, and an unlinked file is not created again. Before, a symlink swapped in between the open and the flush received the stream's pending bytes ([#974](https://github.com/go-embedded-ruby/ruby/issues/974)). The stream's own reads, `#size` and `#pread` still see its bytes. On Unix the stream also holds a descriptor on the file it opened, only to keep that inode alive, so a file deleted and created again at once cannot reuse its inode number and pass for it. A file made read-only after the open is still truncated by `#truncate`, as MRI's `ftruncate` on the open descriptor does. The divergence: MRI's bytes land on the orphaned inode, where rbgo keeps them in the stream's buffer, so another process holding that inode does not see them. A fifo swapped in between rbgo's two checks can still make the open block. A file opened through a symlink is written through it, as in MRI |
| an unflushed file whose directory is removed before the program ends | loses its bytes; MRI, holding a descriptor to an unlinked file, still writes them. Teardown re-opens the path to flush it, so it gets `ENOENT` — and reports nothing, as MRI's shutdown ignores a failing `fflush`. Same root again: streams are buffered by path, which is also [#635](https://github.com/go-embedded-ruby/ruby/issues/635) |
| `Errno` | all **158** of MRI's constant names are present, but **32** report errno `0` where MRI has a real number (`EAUTH`, `EBADRPC`, `EDEVERR`, …). Measured by set difference on darwin: 81 names are zero-valued under rbgo, 49 under MRI, and every MRI zero is also zero under rbgo. Go's portable `syscall` package does not expose the platform-only names |
| `pp` | neither `Kernel#pp` nor `require "pp"` exists (`require "prettyprint"` does work) |
Expand Down
12 changes: 12 additions & 0 deletions internal/vm/errno_win32_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

//go:build !windows

package vm

// mapHostErrno is the identity outside Windows: a syscall.Errno already holds
// the POSIX number its Errno class is registered under. See
// errno_win32_windows.go.
func mapHostErrno(eno int64) int64 { return eno }
129 changes: 129 additions & 0 deletions internal/vm/errno_win32_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

//go:build windows

package vm

import (
"syscall"

"golang.org/x/sys/windows"
)

// win32Errnos is MRI's errmap (win32/win32.c, v4.0.9), the Win32 rows: the
// errno class rb_w32_map_errno gives each Win32 error code. On Windows a
// syscall.Errno carries the Win32 code itself, where errnoNumbers holds the
// numbers Go invented for the POSIX names, so a code looked up there directly
// lands on an unrelated class (ERROR_PATH_NOT_FOUND, 3, read as ENOTDIR).
//
// 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.
var win32Errnos = map[syscall.Errno]string{
windows.ERROR_INVALID_FUNCTION: "EINVAL",
windows.ERROR_FILE_NOT_FOUND: "ENOENT",
windows.ERROR_PATH_NOT_FOUND: "ENOENT",
windows.ERROR_TOO_MANY_OPEN_FILES: "EMFILE",
windows.ERROR_ACCESS_DENIED: "EACCES",
windows.ERROR_INVALID_HANDLE: "EBADF",
windows.ERROR_ARENA_TRASHED: "ENOMEM",
windows.ERROR_NOT_ENOUGH_MEMORY: "ENOMEM",
windows.ERROR_INVALID_BLOCK: "ENOMEM",
windows.ERROR_BAD_ENVIRONMENT: "E2BIG",
windows.ERROR_BAD_FORMAT: "ENOEXEC",
windows.ERROR_INVALID_ACCESS: "EINVAL",
windows.ERROR_INVALID_DATA: "EINVAL",
windows.ERROR_INVALID_DRIVE: "ENOENT",
windows.ERROR_CURRENT_DIRECTORY: "EACCES",
windows.ERROR_NOT_SAME_DEVICE: "EXDEV",
windows.ERROR_NO_MORE_FILES: "ENOENT",
windows.ERROR_WRITE_PROTECT: "EROFS",
windows.ERROR_BAD_UNIT: "ENODEV",
windows.ERROR_NOT_READY: "ENXIO",
windows.ERROR_BAD_COMMAND: "EACCES",
windows.ERROR_CRC: "EACCES",
windows.ERROR_BAD_LENGTH: "EACCES",
windows.ERROR_SEEK: "EIO",
windows.ERROR_NOT_DOS_DISK: "EACCES",
windows.ERROR_SECTOR_NOT_FOUND: "EACCES",
windows.ERROR_OUT_OF_PAPER: "EACCES",
windows.ERROR_WRITE_FAULT: "EIO",
windows.ERROR_READ_FAULT: "EIO",
windows.ERROR_GEN_FAILURE: "EACCES",
windows.ERROR_LOCK_VIOLATION: "EACCES",
windows.ERROR_SHARING_VIOLATION: "EACCES",
windows.ERROR_WRONG_DISK: "EACCES",
windows.ERROR_SHARING_BUFFER_EXCEEDED: "EACCES",
windows.ERROR_BAD_NETPATH: "ENOENT",
windows.ERROR_NETWORK_ACCESS_DENIED: "EACCES",
windows.ERROR_BAD_NET_NAME: "ENOENT",
windows.ERROR_FILE_EXISTS: "EEXIST",
windows.ERROR_CANNOT_MAKE: "EACCES",
windows.ERROR_FAIL_I24: "EACCES",
windows.ERROR_INVALID_PARAMETER: "EINVAL",
windows.ERROR_NO_PROC_SLOTS: "EAGAIN",
windows.ERROR_DRIVE_LOCKED: "EACCES",
windows.ERROR_BROKEN_PIPE: "EPIPE",
windows.ERROR_DISK_FULL: "ENOSPC",
windows.ERROR_INVALID_TARGET_HANDLE: "EBADF",
windows.ERROR_WAIT_NO_CHILDREN: "ECHILD",
windows.ERROR_CHILD_NOT_COMPLETE: "ECHILD",
windows.ERROR_DIRECT_ACCESS_HANDLE: "EBADF",
windows.ERROR_NEGATIVE_SEEK: "EINVAL",
windows.ERROR_SEEK_ON_DEVICE: "EACCES",
windows.ERROR_DIR_NOT_EMPTY: "ENOTEMPTY",
windows.ERROR_DIRECTORY: "ENOTDIR",
windows.ERROR_NOT_LOCKED: "EACCES",
windows.ERROR_BAD_PATHNAME: "ENOENT",
windows.ERROR_MAX_THRDS_REACHED: "EAGAIN",
windows.ERROR_LOCK_FAILED: "EACCES",
windows.ERROR_ALREADY_EXISTS: "EEXIST",
windows.ERROR_INVALID_STARTING_CODESEG: "ENOEXEC",
windows.ERROR_INVALID_STACKSEG: "ENOEXEC",
windows.ERROR_INVALID_MODULETYPE: "ENOEXEC",
windows.ERROR_INVALID_EXE_SIGNATURE: "ENOEXEC",
windows.ERROR_EXE_MARKED_INVALID: "ENOEXEC",
windows.ERROR_BAD_EXE_FORMAT: "ENOEXEC",
windows.ERROR_ITERATED_DATA_EXCEEDS_64k: "ENOEXEC",
windows.ERROR_INVALID_MINALLOCSIZE: "ENOEXEC",
windows.ERROR_DYNLINK_FROM_INVALID_RING: "ENOEXEC",
windows.ERROR_IOPL_NOT_ENABLED: "ENOEXEC",
windows.ERROR_INVALID_SEGDPL: "ENOEXEC",
windows.ERROR_AUTODATASEG_EXCEEDS_64k: "ENOEXEC",
windows.ERROR_RING2SEG_MUST_BE_MOVABLE: "ENOEXEC",
windows.ERROR_RELOC_CHAIN_XEEDS_SEGLIM: "ENOEXEC",
windows.ERROR_INFLOOP_IN_RELOC_CHAIN: "ENOEXEC",
windows.ERROR_FILENAME_EXCED_RANGE: "ENOENT",
windows.ERROR_NESTING_NOT_ALLOWED: "EAGAIN",
229: "EPIPE", // ERROR_PIPE_LOCAL, which win32.c defines itself
windows.ERROR_BAD_PIPE: "EPIPE",
windows.ERROR_PIPE_BUSY: "EAGAIN",
windows.ERROR_NO_DATA: "EPIPE",
windows.ERROR_PIPE_NOT_CONNECTED: "EPIPE",
windows.ERROR_OPERATION_ABORTED: "EINTR",
windows.ERROR_NOT_ENOUGH_QUOTA: "ENOMEM",
windows.ERROR_MOD_NOT_FOUND: "ENOENT",
windows.ERROR_PRIVILEGE_NOT_HELD: "EACCES",
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.
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.
func mapHostErrno(eno int64) int64 {
if eno == 0 || eno >= wsaBaseErr {
return eno
}
if name, ok := win32Errnos[syscall.Errno(eno)]; ok {
return errnoNumbers[name]
}
return errnoNumbers["EINVAL"]
}
50 changes: 50 additions & 0 deletions internal/vm/errno_win32_windows_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

//go:build windows

package vm

import (
"syscall"
"testing"

"golang.org/x/sys/windows"
)

// TestMapHostErrnoIsRbW32MapErrno: each row is what MRI's rb_w32_map_errno
// (win32/win32.c, v4.0.9) gives for the code. ERROR_PATH_NOT_FOUND is the row
// the Windows runner showed: read as a POSIX number, 3 raised Errno::ENOTDIR
// where MRI raises Errno::ENOENT.
func TestMapHostErrnoIsRbW32MapErrno(t *testing.T) {
cases := []struct {
eno syscall.Errno
want string
}{
{windows.ERROR_PATH_NOT_FOUND, "ENOENT"},
{windows.ERROR_FILE_NOT_FOUND, "ENOENT"},
{windows.ERROR_ACCESS_DENIED, "EACCES"},
// errmap's first row for it; the second says EINVAL.
{windows.ERROR_INVALID_HANDLE, "EBADF"},
{windows.ERROR_DIRECTORY, "ENOTDIR"},
{windows.ERROR_DISK_FULL, "ENOSPC"},
// ERROR_PIPE_LOCAL, which win32.c defines itself.
{229, "EPIPE"},
// Absent from errmap, and below WSABASEERR.
{windows.ERROR_INVALID_NAME, "EINVAL"},
// A number Go invented for a POSIX name: kept.
{syscall.ENOENT, "ENOENT"},
}
for _, c := range cases {
if got := mapHostErrno(int64(c.eno)); got != errnoNumbers[c.want] {
t.Errorf("mapHostErrno(%d) = %d (%s), want %s", c.eno, got, errnoClasses[got], c.want)
}
}
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)
}
}
12 changes: 10 additions & 2 deletions internal/vm/file_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,23 @@ func chmodMode(m int64) fs.FileMode {
// darwin keeps sticky through a write and loses setuid and setgid, which is
// the kernel clearing them on a write, and restoring the mode read BEFORE the
// write would put a setuid bit back on content that was just changed.
func withOwnerWrite(path string, refused error, write func() error) error {
//
// ident is the file the stream opened (io_identity.go), or nil. The mode is
// changed only while path still names it: a chmod follows a symlink, and one
// put in the file's place must not lend its target the owner-write bit. When
// path names another file, nothing is done and errNotTheOpenedFile returned.
func withOwnerWrite(path string, ident os.FileInfo, refused error, write func() error) error {
st, err := os.Stat(path)
if err != nil {
return refused
}
if ident != nil && !os.SameFile(ident, st) {
return errNotTheOpenedFile
}
ownerWrite := st.Mode() & 0o200
_ = os.Chmod(path, st.Mode()&chmodBits|0o200)
werr := write()
if after, err := os.Stat(path); err == nil {
if after, err := os.Stat(path); err == nil && os.SameFile(st, after) {
_ = os.Chmod(path, after.Mode()&chmodBits&^0o200|ownerWrite)
}
return werr
Expand Down
12 changes: 6 additions & 6 deletions internal/vm/filestat_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,22 +441,22 @@ func TestStatGroupsDefault(t *testing.T) {
// TestIOFlushError covers ioFlush's write-failure branch: flushing a writable
// file stream whose path is an unwritable directory raises Errno::ENOENT.
func TestIOFlushError(t *testing.T) {
o := &IOObj{isStr: true, writable: true, wbufDirty: true, path: "/no/such/dir/x.txt", buf: []byte("data")}
o := &IOObj{isStr: true, writable: true, dirty: []ioSpan{{0, 4}}, path: "/no/such/dir/x.txt", buf: []byte("data")}
if got := catchRaise(func() { ioFlush(o) }); got != "Errno::ENOENT" {
t.Errorf("ioFlush error: got %q want Errno::ENOENT", got)
}
// Each of these is a no-op: no write is attempted, so none raises. Every row
// is pending (wbufDirty) except the last, so each one is refused by its own
// clause of the guard alone. The unwritable path makes an attempted write
// is pending (a dirty range) except the last, so each one is refused by its
// own clause of the guard alone. The unwritable path makes an attempted write
// visible as Errno::ENOENT rather than as a file appearing somewhere.
noops := []struct {
name string
o *IOObj
}{
{"not writable", &IOObj{isStr: true, writable: false, wbufDirty: true, path: "/no/such/dir/x.txt"}},
{"no path", &IOObj{isStr: true, writable: true, wbufDirty: true, path: ""}},
{"not writable", &IOObj{isStr: true, writable: false, dirty: []ioSpan{{0, 4}}, path: "/no/such/dir/x.txt"}},
{"no path", &IOObj{isStr: true, writable: true, dirty: []ioSpan{{0, 4}}, path: ""}},
// #969: nothing pending, so even a writable stream with a path does not
// write. The same stream with wbufDirty set raised above.
// write. The same stream with a dirty range raised above.
{"nothing pending", &IOObj{isStr: true, writable: true, path: "/no/such/dir/x.txt", buf: []byte("data")}},
}
for _, c := range noops {
Expand Down
Loading
Loading