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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ 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` | positional reads and writes go through the buffer, not a descriptor, so a `#pwrite`'s bytes are not on disk until a flush — MRI's `rb_io_pwrite` calls `pwrite(2)` and they are there the moment it returns. They are no longer *lost*: interpreter teardown flushes them like any buffered write ([#643](https://github.com/go-embedded-ruby/ruby/issues/643)), so a program that `pwrite`s and exits finds them. Only the *immediacy* differs, and it is the same root as the row above: no descriptor. [#949](https://github.com/go-embedded-ruby/ruby/issues/949) |
| `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. One deliberate divergence: `#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 |
| 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
40 changes: 31 additions & 9 deletions internal/vm/io_descriptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package vm

import (
"errors"
"io"
"math/big"
"os"
"strings"
Expand Down Expand Up @@ -325,6 +327,23 @@ func defIOSeekable(cls *RClass) {
if off < 0 {
raise("Errno::EINVAL", "Invalid argument - pread")
}
// #949: pread(2) reads the FILE, so it sees what another writer has put
// there since this stream was opened. rb_io_pread flushes this stream's
// own pending writes first, and so does this, so they are seen too.
if p, ok := ioPositionalPath(o); ok {
if o.wbufDirty {
ioFlush(o)
}
data, err := preadFile(p, n, off)
if err == nil {
return ioReadResult(data, buf)
}
if errors.Is(err, io.EOF) {
raise("EOFError", "end of file reached")
}
// Unreadable by path (removed between the two calls, permission):
// the buffer below is what this stream last saw.
}
if off >= len(o.buf) {
raise("EOFError", "end of file reached")
}
Expand All @@ -346,15 +365,18 @@ func defIOSeekable(cls *RClass) {
if end := off + len(data); end > len(o.buf) {
o.buf = append(o.buf, make([]byte, end-len(o.buf))...)
}
copy(o.buf[off:], data)
o.writable = true // a written File flushes its buffer back on flush/close
// Door 4 of 5. MRI's rb_io_pwrite calls pwrite(2) on the descriptor, so
// there the bytes are on disk the moment #pwrite returns; here they are in
// the buffer, which makes them losable exactly like a buffered #write.
// Marking them dirty and tracking the stream is what keeps them (#643);
// that they are not on disk UNTIL a flush is a separate divergence of the
// buffer-backed model, not one this records as fixed.
o.wbufDirty = true
copy(o.buf[off:], data) // so this stream's own later reads see them
o.writable = true // a written File flushes its buffer back on flush/close
// #949: MRI's rb_io_pwrite is pwrite(2), so the bytes are on disk the
// moment it returns, and ONLY those bytes: the stream's other pending
// writes stay pending. pwriteFile does exactly that, so on success
// nothing new is waiting and wbufDirty is left as it was.
if p, ok := ioPositionalPath(o); !ok || pwriteFile(p, data, off) != nil {
// Door 4 of 5. No file reachable by path: the bytes are in the buffer
// only, losable like a buffered #write until a flush. Marking them
// dirty and tracking the stream is what keeps them (#643).
o.wbufDirty = true
}
vm.trackOpenFile(o)
return object.IntValue(int64(len(data)))
})
Expand Down
114 changes: 114 additions & 0 deletions internal/vm/io_positional.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

package vm

import (
"errors"
"io"
"io/fs"
"os"
)

// Positional I/O on the FILE, not on the stream's buffer (#949).
//
// MRI's IO#pwrite and IO#pread are pwrite(2) and pread(2) on the descriptor:
// they neither read nor move the stream's buffer or position. Measured on MRI
// 4.0.7, a file holding "0123456789":
//
// f.pwrite("AB", 0); File.read(path) # "AB23456789", at once
// f.write("QQ"); f.pwrite("AB", 5) # disk "01234AB789": the buffered
// # "QQ" is NOT pushed out with it
// (another writer changes the file); f.pread(3, 0) # sees the new bytes
// f.write("QQ"); f.pread(2, 0) # "QQ": rb_io_pread flushes wbuf first
// f.pwrite("Z", 12) # "0123456789\0\0Z": a hole
//
// A File stream here is buffer-backed and holds no descriptor of its own, so it
// reaches the file by its path. Two limits come with that and are this model's,
// not new: a path that has since been unlinked or replaced is not the file the
// stream opened (the same holds for ioFlush, which writes the whole buffer back
// by path), and O_APPEND's effect on pwrite(2) — Linux appends whatever the
// offset — is not reproduced. When the path cannot be reached, both methods
// fall back to the buffer, which is what they did before.

// ioPositionalPath reports the regular file a positional call on o can reach,
// or false for a stream that has none: a StringIO, a pipe end, a popen stream, a
// device or fifo, or a path that no longer resolves.
func ioPositionalPath(o *IOObj) (string, bool) {
if o.pipe != nil || o.popen != nil {
return "", false
}
st, err := os.Stat(o.path) // "" (a StringIO) does not resolve either
if err != nil || !st.Mode().IsRegular() {
return "", false
}
return o.path, true
}

// pwriteFile writes data at off in the file at path and touches nothing else:
// no other byte of the file, and nothing of the stream that opened it. A write
// past the end leaves a hole, as pwrite(2) does.
func pwriteFile(path string, data []byte, off int) error {
err := writeAtPath(path, data, off)
if errors.Is(err, fs.ErrPermission) {
err = writeAtWithOwnerWrite(path, data, off, err)
}
return err
}

// writeAtWithOwnerWrite retries a write refused for permission with the
// owner-write bit put back for the length of the write, for the reason ioFlush
// gives: the stream already holds the descriptor's grant
// (`File.new(path, "w", 0444)` writes through), and reopening by path must not
// take it away. It never widens the file's mode beyond the write. When the
// mode cannot be read, the original refusal stands; when it cannot be changed
// (a file this process does not own), the retry is refused for permission
// again and that refusal is returned, so no separate branch is needed for it.
func writeAtWithOwnerWrite(path string, data []byte, off int, refused error) error {
st, err := os.Stat(path)
if err != nil {
return refused
}
mode := st.Mode().Perm()
_ = os.Chmod(path, mode|0o200)
err = writeAtPath(path, data, off)
_ = os.Chmod(path, mode)
return err
}

func writeAtPath(path string, data []byte, off int) error {
f, err := os.OpenFile(path, os.O_WRONLY, 0)
if err != nil {
return err
}
_, werr := f.WriteAt(data, int64(off))
cerr := f.Close()
if werr != nil {
return werr
}
return cerr
}

// preadFile reads up to n bytes at off from the file at path. It returns io.EOF
// when off is at or past the end, which IO#pread raises as EOFError.
//
// What it allocates is bounded by what the file holds past off, not by n: n
// comes from Ruby, and `f.pread(2**40, 0)` must not be an allocation of 2**40
// bytes. io.ReadAll grows with what is actually read, and the section reader
// caps that at n (it clamps off+n itself when the sum would overflow).
func preadFile(path string, n, off int) ([]byte, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
data, err := io.ReadAll(io.NewSectionReader(f, int64(off), int64(n)))
if err != nil {
return nil, err
}
if len(data) == 0 {
return nil, io.EOF
}
return data, nil
}
106 changes: 106 additions & 0 deletions internal/vm/io_positional_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

package vm

import (
"errors"
"io"
"io/fs"
"os"
"path/filepath"
"runtime"
"testing"
)

// TestIOPositionalPathAnswersOnlyForARegularFile: #pwrite and #pread reach the
// disk only through a stream that names a regular file. Each refusal below
// would otherwise send a positional write to a path that is not this stream's
// file: the pipe and popen rows carry a real regular file precisely so that
// only their own guard can refuse them.
func TestIOPositionalPathAnswersOnlyForARegularFile(t *testing.T) {
dir := t.TempDir()
file := filepath.Join(dir, "f")
if err := os.WriteFile(file, []byte("0123456789"), 0o644); err != nil {
t.Fatal(err)
}
cases := []struct {
name string
o *IOObj
want bool
}{
{"a regular file", &IOObj{path: file}, true},
{"no path (a StringIO)", &IOObj{}, false},
{"a path that does not resolve", &IOObj{path: filepath.Join(dir, "gone")}, false},
{"a directory", &IOObj{path: dir}, false},
{"a pipe end, even over a regular path", &IOObj{path: file, pipe: &pipeBuf{}}, false},
{"a popen stream, even over a regular path", &IOObj{path: file, popen: &popenProc{}}, false},
}
for _, c := range cases {
got, ok := ioPositionalPath(c.o)
if ok != c.want {
t.Errorf("%s: ok = %v, want %v", c.name, ok, c.want)
}
if ok && got != file {
t.Errorf("%s: path = %q, want %q", c.name, got, file)
}
}
}

// TestWriteAtWithOwnerWriteKeepsTheRefusal: when the mode cannot be read, the
// retry must give the original refusal back rather than go on with no mode to
// restore.
func TestWriteAtWithOwnerWriteKeepsTheRefusal(t *testing.T) {
refused := errors.New("the first refusal")
var err error
func() {
// A retry that goes on without a mode dereferences nothing; assert
// that, rather than let a crash be the only witness.
defer func() {
if r := recover(); r != nil {
t.Errorf("an unreadable mode crashed the retry: %v", r)
}
}()
err = writeAtWithOwnerWrite(filepath.Join(t.TempDir(), "gone"), []byte("x"), 0, refused)
}()
if !errors.Is(err, refused) {
t.Errorf("got %v, want the original refusal", err)
}
}

// TestWriteAtPathReportsAFailedWrite: a write that fails after the open
// succeeded is reported, not swallowed behind a clean Close.
func TestWriteAtPathReportsAFailedWrite(t *testing.T) {
file := filepath.Join(t.TempDir(), "f")
if err := os.WriteFile(file, []byte("0123"), 0o644); err != nil {
t.Fatal(err)
}
if err := writeAtPath(file, []byte("x"), -1); err == nil {
t.Error("a write at offset -1 reported success")
}
}

// TestPreadFileErrors separates the three answers IO#pread acts on: EOF (raised
// as EOFError), a real error (the caller falls back to the buffer), and data.
func TestPreadFileErrors(t *testing.T) {
dir := t.TempDir()
file := filepath.Join(dir, "f")
if err := os.WriteFile(file, []byte("0123456789"), 0o644); err != nil {
t.Fatal(err)
}
if got, err := preadFile(file, 3, 7); err != nil || string(got) != "789" {
t.Errorf("read at 7: got %q, %v", got, err)
}
if _, err := preadFile(file, 1, 10); !errors.Is(err, io.EOF) {
t.Errorf("read at the end: got %v, want io.EOF", err)
}
if _, err := preadFile(filepath.Join(dir, "gone"), 1, 0); !errors.Is(err, fs.ErrNotExist) {
t.Errorf("missing file: got %v, want fs.ErrNotExist", err)
}
if runtime.GOOS != "windows" { // reading a directory handle is an error on unix
if _, err := preadFile(dir, 1, 0); err == nil || errors.Is(err, io.EOF) {
t.Errorf("a directory: got %v, want a read error that is not EOF", err)
}
}
}
81 changes: 81 additions & 0 deletions internal/vm/io_positional_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

package vm_test

import (
"fmt"
"os"
"path/filepath"
"testing"
)

// TestPositionalIOReachesTheFile pins IO#pwrite and IO#pread to the FILE rather
// than to the stream's buffer (#949). Every want is MRI 4.0.7's output for the
// same program (arm64-darwin25, measured with rubydiff on 2026-11-21); before
// the fix rbgo diverged on the first five rows and on the 0444 row.
func TestPositionalIOReachesTheFile(t *testing.T) {
dir := t.TempDir()
p := filepath.ToSlash(filepath.Join(dir, "f"))
reset := fmt.Sprintf("File.write(%q, \"0123456789\"); ", p)
read := fmt.Sprintf("File.read(%q)", p)
cases := []struct{ name, src, want string }{
{"pwrite lands at once",
reset + fmt.Sprintf(`f = File.open(%q, "r+"); f.pwrite("AB", 0); p %s`, p, read),
"\"AB23456789\"\n"},
{"pwrite pushes only its own range, not the pending buffered write",
reset + fmt.Sprintf(`f = File.open(%q, "r+"); f.write("QQ"); f.pwrite("AB", 5); p %s; f.close; p %s`, p, read, read),
"\"01234AB789\"\n\"QQ234AB789\"\n"},
{"pwrite past the end leaves a hole",
reset + fmt.Sprintf(`f = File.open(%q, "r+"); f.pwrite("Z", 12); p %s`, p, read),
"\"0123456789\\u0000\\u0000Z\"\n"},
{"pread sees another writer",
reset + fmt.Sprintf(`f = File.open(%q, "r"); File.write(%q, "xyz3456789"); p f.pread(3, 0)`, p, p),
"\"xyz\"\n"},
{"pread sees this stream's own pending write",
reset + fmt.Sprintf(`f = File.open(%q, "r+"); f.write("QQ"); p f.pread(2, 0)`, p),
"\"QQ\"\n"},
{"pwrite leaves the position alone",
reset + fmt.Sprintf(`f = File.open(%q, "r+"); f.read(3); f.pwrite("AB", 0); p [f.pos, f.read(2)]`, p),
"[3, \"34\"]\n"},
{"a stream created 0444 still pwrites through its grant",
fmt.Sprintf(`q = %q + "ro"; f = File.new(q, "w", 0444); f.pwrite("AB", 0); p [File.binread(q), format("%%o", File.stat(q).mode & 0o777)]`, p),
"[\"AB\", \"444\"]\n"},
// The stream's own descriptor in MRI is unaffected by a later chmod, so
// it still reads the file; here the path is unreadable and pread falls
// back to the buffer, which holds the same bytes.
{"pread on a path made unreadable after open falls back to the buffer",
reset + fmt.Sprintf(`f = File.open(%q, "r"); File.chmod(0, %q); r = f.pread(3, 2); File.chmod(0o644, %q); p r`, p, p, p),
"\"234\"\n"},
{"pread past the end of an unlinked file is still end of file",
reset + fmt.Sprintf(`f = File.open(%q, "r"); File.unlink(%q); begin; f.pread(1, 10); rescue EOFError => e; p e.message; end`, p, p),
"\"end of file reached\"\n"},
{"pwrite on an unlinked path falls back to the buffer",
reset + fmt.Sprintf(`f = File.open(%q, "r+"); File.unlink(%q); p [f.pwrite("AB", 0), f.pread(4, 0)]`, p, p),
"[2, \"AB23\"]\n"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if got := eval(t, c.src); got != c.want {
t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want)
}
})
}
}

// TestPreadLengthIsBoundedByTheFile: the length comes from Ruby, and the read
// allocates what the file holds past the offset, never the length asked for.
// MRI 4.0.7 does allocate it — `f.pread(2**40, 7)` on a 10-byte file did not
// answer within 30 s — so this is a deliberate divergence in rbgo's favour, not
// a parity row.
func TestPreadLengthIsBoundedByTheFile(t *testing.T) {
p := filepath.ToSlash(filepath.Join(t.TempDir(), "f"))
if err := os.WriteFile(p, []byte("0123456789"), 0o644); err != nil {
t.Fatal(err)
}
src := fmt.Sprintf(`f = File.open(%q, "r"); p f.pread(2**40, 7)`, p)
if got := eval(t, src); got != "\"789\"\n" {
t.Errorf("got %q, want %q", got, "\"789\"\n")
}
}
Loading