From 7a3aab39eb456021c0d53fb665e85a02b2e22809 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sat, 21 Nov 2026 17:56:49 +0100 Subject: [PATCH 1/5] 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/5] 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/5] 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/5] 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"}, From 775560d6c546279dcbd1ac8e978c850b4c1445a9 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sat, 21 Nov 2026 21:00:58 +0100 Subject: [PATCH 5/5] vm: keep the special mode bits, and bound positional offsets #972: Ruby's 0o4000/0o2000/0o1000 are not Go's fs.ModeSetuid/Setgid/Sticky, so File.chmod, File#chmod, FileUtils.chmod and chmod_R passed the integer to os.Chmod and set the nine permission bits alone: File.chmod(0o1444, q) gave 0o444. chmodMode maps them. The owner-write retry that ioFlush and #pwrite share (withOwnerWrite) saved Mode().Perm() and restored it, losing the same bits; it now restores only the owner-write bit over the mode the write left, so the kernel's clearing of setuid/setgid on a write stands, as in MRI 4.0.7 (1444 kept, 4444 -> 444, 6755 -> 755). #973: #pwrite grew the in-memory buffer to off+len BEFORE writing by path, so f.pwrite("AB", 2**63-1) was a slice-bounds panic raised as ArgumentError and 2**62 reached the allocator, even on a reachable file. Now the file is written first and its refusal raised with its errno (EFBIG, ENOSPC, EIO were swallowed into the buffer); the buffer mirror opens at most a 64 MiB hole, and when the path is gone and the hole is larger, #pwrite raises EFBIG. The #pread fallback no longer forms off+n. Offsets are NUM2OFFT: a Bignum is RangeError, and they stay 64-bit on a 32-bit target. Both probes agree with MRI 4.0.7 under rubydiff (they diverged on every row before). Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- internal/vm/file.go | 2 +- internal/vm/file_mode.go | 60 ++++++++++ internal/vm/fileutils.go | 4 +- internal/vm/io.go | 19 +-- internal/vm/io_descriptors.go | 47 +++++--- internal/vm/io_positional.go | 76 ++++++++---- .../vm/io_positional_errno_internal_test.go | 38 ++++++ internal/vm/io_positional_hardening_test.go | 111 ++++++++++++++++++ internal/vm/io_positional_internal_test.go | 21 ++++ 10 files changed, 329 insertions(+), 51 deletions(-) create mode 100644 internal/vm/file_mode.go create mode 100644 internal/vm/io_positional_errno_internal_test.go create mode 100644 internal/vm/io_positional_hardening_test.go diff --git a/README.md b/README.md index e824cda6..061ce711 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` 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 | +| `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)) | | 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/file.go b/internal/vm/file.go index 1cf5fb52..1f6241d6 100644 --- a/internal/vm/file.go +++ b/internal/vm/file.go @@ -301,7 +301,7 @@ func (vm *VM) registerFile() { // a leading mode/owner/time argument followed by one or more paths, returning // the count of paths affected (MRI semantics). def("chmod", func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { - mode := os.FileMode(vm.num2mode(args[0]) & 0o7777) + mode := chmodMode(int64(vm.num2mode(args[0]))) paths := args[1:] for _, a := range paths { p := pathArg(vm, a) diff --git a/internal/vm/file_mode.go b/internal/vm/file_mode.go new file mode 100644 index 00000000..9b2bba0a --- /dev/null +++ b/internal/vm/file_mode.go @@ -0,0 +1,60 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import ( + "io/fs" + "os" +) + +// chmodBits are the fs.FileMode bits os.Chmod sets: the nine permission bits +// and the three special ones, which Go keeps far above them (fs.ModeSetuid is +// not 0o4000). +const chmodBits = fs.ModePerm | fs.ModeSetuid | fs.ModeSetgid | fs.ModeSticky + +// chmodMode turns a POSIX mode, as Ruby passes it to File.chmod (0o4755, +// 0o1777…), into the fs.FileMode os.Chmod reads (#972). Passing the integer +// straight through, as `os.FileMode(m & 0o7777)` did, keeps the nine +// permission bits and silently drops setuid, setgid and sticky: Go reads +// 0o1000 as nothing at all. +func chmodMode(m int64) fs.FileMode { + fm := fs.FileMode(m & 0o777) + if m&0o4000 != 0 { + fm |= fs.ModeSetuid + } + if m&0o2000 != 0 { + fm |= fs.ModeSetgid + } + if m&0o1000 != 0 { + fm |= fs.ModeSticky + } + return fm +} + +// withOwnerWrite runs write with the owner-write bit of path put back for its +// length, for the reason ioFlush gives: a stream that already holds the +// descriptor's grant must not lose it because this model reopens by path. It +// returns refused when the mode cannot be read, and otherwise what write +// returns; when the mode cannot be changed (a file this process does not own), +// write is refused for permission again, so no separate branch is needed. +// +// Afterwards only the owner-write bit goes back to what it was (#972). The +// rest is read from the file as the write left it: MRI 4.0.7 measured on +// 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 { + st, err := os.Stat(path) + if err != nil { + return refused + } + ownerWrite := st.Mode() & 0o200 + _ = os.Chmod(path, st.Mode()&chmodBits|0o200) + werr := write() + if after, err := os.Stat(path); err == nil { + _ = os.Chmod(path, after.Mode()&chmodBits&^0o200|ownerWrite) + } + return werr +} diff --git a/internal/vm/fileutils.go b/internal/vm/fileutils.go index 8e0d74cf..0a1b55e6 100644 --- a/internal/vm/fileutils.go +++ b/internal/vm/fileutils.go @@ -157,7 +157,7 @@ func (vm *VM) registerFileUtils() { // every platform (chmod is largely a no-op on Windows, so the seam is the only // way to cover the failure path there). sdef("chmod", func(_ *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { - mode := os.FileMode(intArg(args[0]) & 0o7777) + mode := chmodMode(int64(intArg(args[0]))) for _, p := range pathsOf(args[1]) { if err := fileChmod(p, mode); err != nil { raise("Errno::ENOENT", "No such file or directory @ fileutils_chmod - %s", p) @@ -197,7 +197,7 @@ func (vm *VM) registerFileUtils() { // chmod_R recurses chmod into directories, as Puppet's report store driver does // (FileUtils.chmod_R(0o750, dir)). sdef("chmod_R", func(_ *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { - mode := os.FileMode(intArg(args[0]) & 0o7777) + mode := chmodMode(int64(intArg(args[0]))) for _, p := range pathsOf(args[1]) { for _, q := range fuWalk(p) { if err := fileChmod(q, mode); err != nil { diff --git a/internal/vm/io.go b/internal/vm/io.go index 5bfffcfe..932a79fd 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -638,7 +638,7 @@ func (vm *VM) registerIO() { cFile.define("chmod", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) ioFlush(o) - if err := fileChmod(o.path, os.FileMode(intArg(args[0])&0o7777)); err != nil { + if err := fileChmod(o.path, chmodMode(int64(intArg(args[0])))); err != nil { raise("Errno::ENOENT", "No such file or directory @ apply2files - %s", o.path) } return object.IntValue(0) @@ -1290,13 +1290,7 @@ func ioFlush(o *IOObj) { return } if errors.Is(err, fs.ErrPermission) { - if st, sterr := os.Stat(o.path); sterr == nil { - mode := st.Mode().Perm() - if chmodErr := os.Chmod(o.path, mode|0o200); chmodErr == nil { - err = os.WriteFile(o.path, o.buf, 0o644) - _ = os.Chmod(o.path, mode) - } - } + err = withOwnerWrite(o.path, err, func() error { return os.WriteFile(o.path, o.buf, 0o644) }) } if err != nil { raiseOpenErrno(err, o.path) @@ -3110,10 +3104,17 @@ func ioClosedRealIO(o *IOObj) { // does: an Integer or #to_int value narrowed to the off_t (a long long) the // position is stored in, raising RangeError for a Bignum too large to fit. func (vm *VM) ioOfftArg(v object.Value) int { + return clampInt(vm.ioOfftArg64(v)) +} + +// ioOfftArg64 is ioOfftArg without the narrowing to int: an off_t is 64 bits on +// every platform MRI builds for, and a file offset passed to the OS must not be +// clamped to 2**31-1 on a 32-bit target (#973). +func (vm *VM) ioOfftArg64(v object.Value) int64 { if _, ok := v.(*object.Bignum); ok { raise("RangeError", "bignum too big to convert into 'long long'") } - return clampInt(vm.toIntCoerce(v)) + return int64(vm.toIntCoerce(v)) } // ioCIntArg coerces an argument the way MRI's NUM2INT does for a value stored in diff --git a/internal/vm/io_descriptors.go b/internal/vm/io_descriptors.go index a5d8af56..d921f22d 100644 --- a/internal/vm/io_descriptors.go +++ b/internal/vm/io_descriptors.go @@ -7,6 +7,7 @@ package vm import ( "errors" "io" + "io/fs" "math/big" "os" "strings" @@ -308,7 +309,7 @@ func defIOSeekable(cls *RClass) { // all before the maxlen==0 early return, so a non-String buffer still // raises and a zero-length read leaves the buffer's bytes untouched. n := clampInt(vm.toIntCoerce(args[0])) - off := clampInt(vm.toIntCoerce(args[1])) + off := vm.ioOfftArg64(args[1]) // NUM2OFFT: a Bignum is RangeError (#973) var buf *object.String if len(args) > 2 { buf = vm.ioBufferArg(args[2]) @@ -344,11 +345,13 @@ func defIOSeekable(cls *RClass) { // Unreadable by path (removed between the two calls, permission): // the buffer below is what this stream last saw. } - if off >= len(o.buf) { + if off >= int64(len(o.buf)) { raise("EOFError", "end of file reached") } - data := o.buf[off:min(off+n, len(o.buf))] - return ioReadResult(data, buf) + // off+n is never formed: with n near MaxInt it overflowed into a slice + // bounds panic (#973). What is left past off bounds the read instead. + rest := o.buf[off:] + return ioReadResult(rest[:min(n, len(rest))], buf) }) cls.define("pwrite", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) @@ -357,27 +360,39 @@ func defIOSeekable(cls *RClass) { // stream is checked, so a missing #to_s surfaces as NoMethodError and a // non-Integer offset as the "into Integer" TypeError. data := []byte(vm.objAsString(args[0])) - off := clampInt(vm.toIntCoerce(args[1])) + off := vm.ioOfftArg64(args[1]) // NUM2OFFT: a Bignum is RangeError (#973) ioCheckOpen(o) if off < 0 { raise("Errno::EINVAL", "Invalid argument - pwrite") } - 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) // so this stream's own later reads see them - o.writable = true // a written File flushes its buffer back on flush/close + o.writable = true // a written File flushes its buffer back on flush/close + vm.trackOpenFile(o) // #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 + if p, ok := ioPositionalPath(o); ok { + err := pwriteFile(p, data, off) + if err == nil { + o.mirrorPwrite(data, off) // best effort: the file has the bytes + return object.IntValue(int64(len(data))) + } + // #973: the file was reached and refused the write — EFBIG past + // the largest offset, ENOSPC, EIO. MRI raises that errno; it used to + // be swallowed here and the bytes put in the buffer instead. A + // refusal for permission is the one kept as before: the owner-write + // retry failed, so the buffer below is the stream's grant. + if !errors.Is(err, fs.ErrPermission) { + raisePwriteErr(err, o.path) + } } - vm.trackOpenFile(o) + // 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). + if !o.mirrorPwrite(data, off) { + raise("Errno::EFBIG", "File too large @ rb_io_pwrite - %s", o.path) + } + o.wbufDirty = true return object.IntValue(int64(len(data))) }) cls.define("sysseek", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { diff --git a/internal/vm/io_positional.go b/internal/vm/io_positional.go index 5fb707e8..3d626aa6 100644 --- a/internal/vm/io_positional.go +++ b/internal/vm/io_positional.go @@ -49,7 +49,7 @@ 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. -func pwriteFile(path string, data []byte, off int) error { +func pwriteFile(path string, data []byte, off int64) error { err := writeAtPath(path, data, off) if errors.Is(err, fs.ErrPermission) { err = writeAtWithOwnerWrite(path, data, off, err) @@ -58,31 +58,19 @@ func pwriteFile(path string, data []byte, off int) error { } // 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 +// owner-write bit put back for the length of the write (withOwnerWrite): 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 writeAtWithOwnerWrite(path string, data []byte, off int64, refused error) error { + return withOwnerWrite(path, refused, func() error { return writeAtPath(path, data, off) }) } -func writeAtPath(path string, data []byte, off int) error { +func writeAtPath(path string, data []byte, off int64) error { f, err := os.OpenFile(path, os.O_WRONLY, 0) if err != nil { return err } - _, werr := f.WriteAt(data, int64(off)) + _, werr := f.WriteAt(data, off) cerr := f.Close() if werr != nil { return werr @@ -90,6 +78,50 @@ func writeAtPath(path string, data []byte, off int) error { return cerr } +// ioHoleMax bounds the hole one #pwrite may open past the end of a stream's +// buffer (#973). The buffer is a byte slice, so a hole in it is memory: +// `f.pwrite("AB", 2**40)` asked for a terabyte, and 2**62 reached the +// allocator. pwrite(2) leaves a sparse hole on disk instead, and this model +// cannot. When the file was reached, the bytes are on disk and only this +// stream's in-memory copy stops short of them, the same staleness another +// writer's bytes already have. When it was not, the buffer is the only copy, +// and #pwrite raises EFBIG — the errno pwrite(2) gives for a write past the +// largest size the file may have, which this bound is for a buffer. +const ioHoleMax = 1 << 26 + +// mirrorPwrite copies data into the stream's buffer at off, so this stream's +// own later reads see it, growing the buffer to hold it. It reports false and +// changes nothing when that would open a hole larger than ioHoleMax. +// +// Only the hole is bounded: the bytes of data are already in memory, and the +// buffer's length is what earlier writes made it. On a 32-bit target, a +// buffer and data that together come within ioHoleMax of 2**31 could still +// overflow end; both would then have to be resident in a 4 GiB address space. +func (o *IOObj) mirrorPwrite(data []byte, off int64) bool { + if off-int64(len(o.buf)) > ioHoleMax { + return false + } + if end := int(off) + len(data); end > len(o.buf) { + o.buf = append(o.buf, make([]byte, end-len(o.buf))...) + } + copy(o.buf[off:], data) + return true +} + +// raisePwriteErr reports a pwrite(2) that reached the file and was refused, +// the way rb_io_pwrite's rb_sys_fail_path does: the real errno, labelled +// with the C function. MRI 4.0.7 on darwin: `f.pwrite("AB", 2**63-1)` is +// `Errno::EFBIG: File too large @ rb_io_pwrite - `. +func raisePwriteErr(err error, path string) { + if eno, ok := errnoOfError(err); ok { + if name, ok := errnoClasses[eno]; ok { + raise("Errno::"+name, "%s @ rb_io_pwrite - %s", errnoStrerror(eno), path) + } + raise("SystemCallError", "%s @ rb_io_pwrite - %s", errnoStrerror(eno), path) + } + raise("Errno::EIO", "Input/output error @ rb_io_pwrite - %s", path) +} + // 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. // @@ -97,13 +129,13 @@ func writeAtPath(path string, data []byte, off int) error { // 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) { +func preadFile(path string, n int, off int64) ([]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))) + data, err := io.ReadAll(io.NewSectionReader(f, off, int64(n))) if err != nil { return nil, err } diff --git a/internal/vm/io_positional_errno_internal_test.go b/internal/vm/io_positional_errno_internal_test.go new file mode 100644 index 00000000..5979f6d1 --- /dev/null +++ b/internal/vm/io_positional_errno_internal_test.go @@ -0,0 +1,38 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +//go:build unix + +package vm + +import ( + "errors" + "io/fs" + "strings" + "syscall" + "testing" +) + +// TestRaisePwriteErrKeepsTheErrno: a pwrite(2) the file refused is raised with +// its own errno (#973), an errno with no class as SystemCallError, and an error +// that carries no errno at all as EIO, all labelled rb_io_pwrite. ENOSPC and +// EIO used to be swallowed: #pwrite put the bytes in the buffer and returned +// their count as if they were on disk. +func TestRaisePwriteErrKeepsTheErrno(t *testing.T) { + cases := []struct { + name string + err error + want string + }{ + {"a known errno", &fs.PathError{Op: "write", Path: "p", Err: syscall.ENOSPC}, "Errno::ENOSPC"}, + {"an errno with no class", syscall.Errno(0x7ffe), "SystemCallError"}, + {"no errno at all", errors.New("short write"), "Errno::EIO"}, + } + for _, c := range cases { + re := rubyErr(t, func() { raisePwriteErr(c.err, "p") }) + if re.Class != c.want || !strings.Contains(re.Message, "@ rb_io_pwrite - p") { + t.Errorf("%s: got %s %q, want %s labelled rb_io_pwrite", c.name, re.Class, re.Message, c.want) + } + } +} diff --git a/internal/vm/io_positional_hardening_test.go b/internal/vm/io_positional_hardening_test.go new file mode 100644 index 00000000..70bcd2e6 --- /dev/null +++ b/internal/vm/io_positional_hardening_test.go @@ -0,0 +1,111 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm_test + +import ( + "fmt" + "os" + "path/filepath" + "runtime" + "testing" +) + +// TestChmodKeepsTheSpecialBits pins setuid, setgid and sticky through every +// door that sets a mode, and through the writes that put the owner-write bit +// back for their length (#972). Every want is MRI 4.0.7's output for the same +// program (arm64-darwin25, rubydiff, 2026-11-21). Before the fix, rbgo printed +// the nine permission bits alone: os.Chmod reads Go's own ModeSticky, not +// 0o1000, so `File.chmod(0o1444, q)` set 0o444. +// +// The modes after a write are the KERNEL's: a write by a process without +// CAP_FSETID clears setuid, and setgid when the group may execute; sticky stays. +// So the rows are skipped as root, where nothing is cleared, and on Windows, +// which has none of these bits. +func TestChmodKeepsTheSpecialBits(t *testing.T) { + if runtime.GOOS == "windows" || os.Geteuid() == 0 { + t.Skip("POSIX special bits, cleared on write only for an unprivileged process") + } + dir := filepath.ToSlash(t.TempDir()) + cases := []struct{ name, src, want string }{ + {"File.chmod, File#chmod, FileUtils.chmod and chmod_R set them", + fmt.Sprintf(`require "fileutils"; q = %q + "/m"; File.write(q, "x"); r = [] +File.chmod(0o1644, q); r << File.stat(q).mode +File.open(q) { |f| f.chmod(0o4644) }; r << File.stat(q).mode +FileUtils.chmod(0o1600, q); r << File.stat(q).mode +FileUtils.chmod_R(0o2700, q); r << File.stat(q).mode +puts r.map { |m| format("%%o", m & 0o7777) }.join(" ")`, dir), + "1644 4644 1600 2700\n"}, + {"a pwrite and a close through a read-only mode keep what the kernel keeps", + fmt.Sprintf(`d = %q +[0o1444, 0o4444, 0o4755, 0o6755].each do |m| + q = d + "/t" + m.to_s(8) + File.write(q, "0123456789") + f = File.open(q, "r+") + File.chmod(m, q) + b = File.stat(q).mode & 0o7777 + f.pwrite("AB", 0) + w = File.stat(q).mode & 0o7777 + f.write("Q") + f.close + c = File.stat(q).mode & 0o7777 + printf("%%o: %%o %%o %%o %%p\n", m, b, w, c, File.binread(q)) +end`, dir), + "1444: 1444 1444 1444 \"QB23456789\"\n" + + "4444: 4444 444 444 \"QB23456789\"\n" + + "4755: 4755 755 755 \"QB23456789\"\n" + + "6755: 6755 755 755 \"QB23456789\"\n"}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + if got := eval(t, c.src); got != c.want { + t.Errorf("got=%q\nwant=%q", got, c.want) + } + }) + } +} + +// TestPositionalOffsetsAreBounded: the offset of #pwrite and #pread comes from +// Ruby, and no offset may become a Go panic or an allocation the size of the +// offset (#973). Rows marked MRI are MRI 4.0.7's output (arm64-darwin25, +// rubydiff, 2026-11-21); the others are this model's documented limits. +func TestPositionalOffsetsAreBounded(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("sparse files and the pwrite(2) errnos are POSIX") + } + p := filepath.ToSlash(filepath.Join(t.TempDir(), "f")) + reset := fmt.Sprintf("File.write(%q, \"0123456789\"); ", p) + cases := []struct{ name, src, want string }{ + // MRI: Errno::EFBIG on darwin. Linux refuses the same overflow with + // EINVAL, so the row asserts what both share: the kernel's errno, + // labelled with the C function, and a file left as it was. + {"MRI: a pwrite past the largest offset raises the kernel's errno", + reset + fmt.Sprintf(`f = File.open(%q, "r+"); begin; f.pwrite("AB", 2**63-1); rescue SystemCallError => e; p [e.message.include?("@ rb_io_pwrite - "), File.binread(%q)]; end`, p, p), + "[true, \"0123456789\"]\n"}, + {"MRI: a Bignum offset is RangeError, for pwrite and pread", + reset + fmt.Sprintf(`f = File.open(%q, "r+"); [-> { f.pwrite("A", 2**64) }, -> { f.pread(1, -(2**64)) }].each { |c| begin; c.call; rescue RangeError => e; p e.message; end }`, p), + "\"bignum too big to convert into 'long long'\"\n\"bignum too big to convert into 'long long'\"\n"}, + {"MRI: a pwrite far past the end reaches the file as a hole", + reset + fmt.Sprintf(`f = File.open(%q, "r+"); p [f.pwrite("Z", 2**27), File.size(%q), f.pread(1, 2**27)]`, p, p), + "[1, 134217729, \"Z\"]\n"}, + // The rows below fall back to the buffer, which is the only copy once + // the path is gone, and a hole in it is memory. + {"an unlinked file: a pread whose length would overflow is bounded by the buffer", + reset + fmt.Sprintf(`f = File.open(%q, "r"); File.unlink(%q); p f.pread(2**63-1, 1)`, p, p), + "\"123456789\"\n"}, + {"an unlinked file: a hole larger than the bound is EFBIG, not an allocation", + reset + fmt.Sprintf(`f = File.open(%q, "r+"); File.unlink(%q); begin; f.pwrite("AB", 2**27); rescue Errno::EFBIG => e; p [e.message, f.pread(10, 0)]; end`, p, p), + fmt.Sprintf("[\"File too large @ rb_io_pwrite - %s\", \"0123456789\"]\n", p)}, + {"an unlinked file: the largest offset is EFBIG, not a slice panic", + reset + fmt.Sprintf(`f = File.open(%q, "r+"); File.unlink(%q); begin; f.pwrite("AB", 2**63-1); rescue Errno::EFBIG => e; p e.class; end`, p, p), + "Errno::EFBIG\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) + } + }) + } +} diff --git a/internal/vm/io_positional_internal_test.go b/internal/vm/io_positional_internal_test.go index ae982c52..72e75933 100644 --- a/internal/vm/io_positional_internal_test.go +++ b/internal/vm/io_positional_internal_test.go @@ -81,6 +81,27 @@ func TestWriteAtPathReportsAFailedWrite(t *testing.T) { } } +// TestChmodModeKeepsTheSpecialBits: Ruby's 0o4000, 0o2000 and 0o1000 are not +// Go's; each must map to its own fs.FileMode bit and to nothing else (#972). +func TestChmodModeKeepsTheSpecialBits(t *testing.T) { + cases := []struct { + in int64 + want fs.FileMode + }{ + {0o755, 0o755}, + {0o4755, fs.ModeSetuid | 0o755}, + {0o2755, fs.ModeSetgid | 0o755}, + {0o1777, fs.ModeSticky | 0o777}, + {0o7000, fs.ModeSetuid | fs.ModeSetgid | fs.ModeSticky}, + {0o170644, 0o644}, // file-type bits from a stat mode are not chmod's + } + for _, c := range cases { + if got := chmodMode(c.in); got != c.want { + t.Errorf("chmodMode(%#o) = %v, want %v", c.in, got, c.want) + } + } +} + // 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) {