From 7a3aab39eb456021c0d53fb665e85a02b2e22809 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sat, 21 Nov 2026 17:56:49 +0100 Subject: [PATCH 1/4] vm: IO#pwrite and IO#pread reach the file, not the stream's buffer MRI's rb_io_pwrite and rb_io_pread are pwrite(2) and pread(2) on the descriptor. rbgo's File streams are buffer-backed and wrote and read only the buffer, so a pwrite was not on disk until a flush and a pread could not see another writer. Both now reach the regular file by its path: - pwrite writes its own range with WriteAt and nothing else. The stream's other pending writes stay pending; MRI does the same (disk "01234AB789", not "QQ234AB789"). On success it no longer marks the buffer dirty. A permission refusal is retried with the owner-write bit restored, as ioFlush does, so a stream created 0444 writes through its grant. Before, those bytes never reached the disk. - pread flushes this stream's pending writes first, as rb_io_pread does, then reads with ReadAt. The allocation is bounded by the file's size, not by the length Ruby asked for, so f.pread(2**40, 0) returns at once (MRI 4.0.7 did not answer within 30 s). A stream with no reachable regular file (a StringIO, a pipe, a popen stream, a device, an unlinked path) falls back to the buffer, as before. Measured with rubydiff against MRI 4.0.7: the probe diverged on 6 of 11 lines before this change and on none after. TestPositionalIOReachesTheFile pins those rows. Found while measuring: closing a writable stream that was never written to rewrites the file from its buffer (#969). That is a separate defect and is not touched here. Closes #949 Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- internal/vm/io_descriptors.go | 40 +++++++--- internal/vm/io_positional.go | 120 ++++++++++++++++++++++++++++++ internal/vm/io_positional_test.go | 72 ++++++++++++++++++ 4 files changed, 224 insertions(+), 10 deletions(-) create mode 100644 internal/vm/io_positional.go create mode 100644 internal/vm/io_positional_test.go diff --git a/README.md b/README.md index 3ac67194..e824cda6 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/internal/vm/io_descriptors.go b/internal/vm/io_descriptors.go index 47a629ed..a5d8af56 100644 --- a/internal/vm/io_descriptors.go +++ b/internal/vm/io_descriptors.go @@ -5,6 +5,8 @@ package vm import ( + "errors" + "io" "math/big" "os" "strings" @@ -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") } @@ -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))) }) diff --git a/internal/vm/io_positional.go b/internal/vm/io_positional.go new file mode 100644 index 00000000..9af6756a --- /dev/null +++ b/internal/vm/io_positional.go @@ -0,0 +1,120 @@ +// 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.path == "" || o.pipe != nil || o.popen != nil { + return "", false + } + st, err := os.Stat(o.path) + 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. +// +// A refusal for permission is retried 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. +func pwriteFile(path string, data []byte, off int) error { + err := writeAtPath(path, data, off) + if !errors.Is(err, fs.ErrPermission) { + return err + } + st, sterr := os.Stat(path) + if sterr != nil { + return err + } + mode := st.Mode().Perm() + if os.Chmod(path, mode|0o200) != nil { + return err + } + 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. +// +// The read is bounded by the file's size, not by n: n comes from Ruby, and +// `f.pread(2**62, 0)` must not be an allocation of 2**62 bytes. +func preadFile(path string, n, off int) ([]byte, error) { + f, err := os.Open(path) + if err != nil { + return nil, err + } + defer f.Close() + st, err := f.Stat() + if err != nil { + return nil, err + } + size := st.Size() + if int64(off) >= size { + return nil, io.EOF + } + want := size - int64(off) + if int64(n) < want { + want = int64(n) + } + buf := make([]byte, want) + m, err := f.ReadAt(buf, int64(off)) + if m > 0 { + return buf[:m], nil // a short read: the file shrank since Stat + } + if err == nil { + err = io.EOF + } + return nil, err +} diff --git a/internal/vm/io_positional_test.go b/internal/vm/io_positional_test.go new file mode 100644 index 00000000..e2a4c179 --- /dev/null +++ b/internal/vm/io_positional_test.go @@ -0,0 +1,72 @@ +// 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"}, + {"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") + } +} From 14f6e2009e094f02aeb4860da2ab5bdc73669fcf Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sat, 21 Nov 2026 18:05:47 +0100 Subject: [PATCH 2/4] vm: give every refusal in the positional I/O helpers a test that holds it mutsweep on the first commit caught 5 of 12 mutants; 7 survived. This commit fixes them in two ways. The code is restructured so that no line is a mutant nobody can tell apart: - The permission retry is now `if errors.Is(err, ErrPermission)`, and the 0444 row holds it. The old `if !errors.Is(...) return` was a fast path whose mutant only repeated a successful write. - preadFile reads with io.ReadAll over an io.SectionReader instead of Stat-then-allocate. That removes an `f.Stat` failure on an open file that nothing can provoke, and the allocation stays bounded by what the file holds. - ioPositionalPath lets os.Stat("") refuse an empty path, instead of a second guard for the same case. Package-internal tests cover the remaining refusals: a pipe or popen stream over a regular path, a directory, a missing path, a retry whose mode cannot be read, a write that fails after a successful open, and the EOF / error / data answers of preadFile. Refs #949 Co-Authored-By: Claude Opus 5.5 --- internal/vm/io_positional.go | 56 ++++++------- internal/vm/io_positional_internal_test.go | 96 ++++++++++++++++++++++ 2 files changed, 121 insertions(+), 31 deletions(-) create mode 100644 internal/vm/io_positional_internal_test.go diff --git a/internal/vm/io_positional.go b/internal/vm/io_positional.go index 9af6756a..1b9a44ce 100644 --- a/internal/vm/io_positional.go +++ b/internal/vm/io_positional.go @@ -36,10 +36,10 @@ import ( // 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.path == "" || o.pipe != nil || o.popen != nil { + if o.pipe != nil || o.popen != nil { return "", false } - st, err := os.Stat(o.path) + st, err := os.Stat(o.path) // "" (a StringIO) does not resolve either if err != nil || !st.Mode().IsRegular() { return "", false } @@ -49,23 +49,28 @@ func ioPositionalPath(o *IOObj) (string, bool) { // 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. -// -// A refusal for permission is retried 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. func pwriteFile(path string, data []byte, off int) error { err := writeAtPath(path, data, off) - if !errors.Is(err, fs.ErrPermission) { - return err + if errors.Is(err, fs.ErrPermission) { + err = writeAtWithOwnerWrite(path, data, off, err) } - st, sterr := os.Stat(path) - if sterr != nil { - return 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 or changed, the original refusal stands. +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() if os.Chmod(path, mode|0o200) != nil { - return err + return refused } err = writeAtPath(path, data, off) _ = os.Chmod(path, mode) @@ -88,33 +93,22 @@ func writeAtPath(path string, data []byte, off int) error { // 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. // -// The read is bounded by the file's size, not by n: n comes from Ruby, and -// `f.pread(2**62, 0)` must not be an allocation of 2**62 bytes. +// 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() - st, err := f.Stat() + data, err := io.ReadAll(io.NewSectionReader(f, int64(off), int64(n))) if err != nil { return nil, err } - size := st.Size() - if int64(off) >= size { + if len(data) == 0 { return nil, io.EOF } - want := size - int64(off) - if int64(n) < want { - want = int64(n) - } - buf := make([]byte, want) - m, err := f.ReadAt(buf, int64(off)) - if m > 0 { - return buf[:m], nil // a short read: the file shrank since Stat - } - if err == nil { - err = io.EOF - } - return nil, err + return data, nil } diff --git a/internal/vm/io_positional_internal_test.go b/internal/vm/io_positional_internal_test.go new file mode 100644 index 00000000..ebd1626e --- /dev/null +++ b/internal/vm/io_positional_internal_test.go @@ -0,0 +1,96 @@ +// 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") + 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) + } + } +} From cf022db32e47b96b77a5ba19a8232fdc3dbdf8e9 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sat, 21 Nov 2026 18:12:58 +0100 Subject: [PATCH 3/4] vm: assert the unreadable-mode retry rather than rely on its crash When os.Stat cannot read the mode, writeAtWithOwnerWrite must return the original refusal. Its mutant was caught only by a nil-pointer panic. The test now recovers that panic and fails with an assertion, and mutsweep reports the line "caught", not "caught by PANIC". Refs #949 Co-Authored-By: Claude Opus 5.5 --- internal/vm/io_positional_internal_test.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/internal/vm/io_positional_internal_test.go b/internal/vm/io_positional_internal_test.go index ebd1626e..ae982c52 100644 --- a/internal/vm/io_positional_internal_test.go +++ b/internal/vm/io_positional_internal_test.go @@ -53,7 +53,17 @@ func TestIOPositionalPathAnswersOnlyForARegularFile(t *testing.T) { // restore. func TestWriteAtWithOwnerWriteKeepsTheRefusal(t *testing.T) { refused := errors.New("the first refusal") - err := writeAtWithOwnerWrite(filepath.Join(t.TempDir(), "gone"), []byte("x"), 0, refused) + 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) } From 8922fe8b5b81b42fe5c0e85cca61b24279c266f6 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sat, 21 Nov 2026 20:03:52 +0100 Subject: [PATCH 4/4] vm: cover pread's buffer fallback and drop an unreachable chmod branch CI's per-function coverage ratchet failed on both POSIX lanes: - defIOSeekable 99.1%: the buffer's own end-of-file check in #pread is reached only when the file cannot be read by path, and no test did that. Two rows now do, each matching MRI 4.0.7 under rubydiff: a pread on a path made unreadable after open (falls back to the buffer, "234"), and a pread past the end of an unlinked file (EOFError). - writeAtWithOwnerWrite 88.9%: the "chmod failed" branch could not be provoked by a test, and it was redundant. When the mode cannot be changed, the retried write is refused for permission again and that refusal is returned. The branch is gone; the chmod's error is ignored with that stated. Refs #949 Co-Authored-By: Claude Opus 5.5 --- internal/vm/io_positional.go | 8 ++++---- internal/vm/io_positional_test.go | 9 +++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/internal/vm/io_positional.go b/internal/vm/io_positional.go index 1b9a44ce..5fb707e8 100644 --- a/internal/vm/io_positional.go +++ b/internal/vm/io_positional.go @@ -62,16 +62,16 @@ func pwriteFile(path string, data []byte, off int) error { // 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 or changed, the original refusal stands. +// 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() - if os.Chmod(path, mode|0o200) != nil { - return refused - } + _ = os.Chmod(path, mode|0o200) err = writeAtPath(path, data, off) _ = os.Chmod(path, mode) return err diff --git a/internal/vm/io_positional_test.go b/internal/vm/io_positional_test.go index e2a4c179..0b6e7c95 100644 --- a/internal/vm/io_positional_test.go +++ b/internal/vm/io_positional_test.go @@ -42,6 +42,15 @@ func TestPositionalIOReachesTheFile(t *testing.T) { {"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"},