From e3a6a8c3dd27517c046bb72f68c149f37c5fbd2d Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sat, 21 Nov 2026 18:26:32 +0100 Subject: [PATCH 1/2] vm: flush only pending bytes, so closing an untouched File writes nothing ioFlush wrote the whole buffer back by path whenever the stream was writable. The buffer also holds what the file contained at open, plus any bytes #ungetc pushed back. So an r+/a/a+/w+ stream closed without a write put its open-time snapshot back over another writer's bytes, re-created a file unlinked while it was open, and wrote #ungetc's pushed-back bytes into the file. MRI does none of this: io_fflush with an empty write buffer writes nothing. ioFlush now returns unless wbufDirty is set. That flag stands in for fptr->wbuf.len and already gated the teardown flush. writeBytes now sets it before a sync=true flush; that flush used to bypass the flag and would otherwise have been gated away. Opens that truncate ("w", O_TRUNC) and #truncate act on disk at once and do not depend on the flush. Measured with rubydiff against MRI 4.0.7: 6 of 11 probe lines diverged before this change, none after. The five writes that must still land (a write, a sync write, an append + flush, "w" with no write, #truncate) agree before and after. TestClosingAStreamWritesOnlyWhatWasWritten pins all 11 rows. Closes #969 Co-Authored-By: Claude Opus 5.5 --- internal/vm/filestat_internal_test.go | 9 ++++- internal/vm/io.go | 17 ++++++-- internal/vm/io_close_untouched_test.go | 54 ++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 internal/vm/io_close_untouched_test.go diff --git a/internal/vm/filestat_internal_test.go b/internal/vm/filestat_internal_test.go index 581f517f..79c4bc7c 100644 --- a/internal/vm/filestat_internal_test.go +++ b/internal/vm/filestat_internal_test.go @@ -441,11 +441,18 @@ func TestStatGroupsDefault(t *testing.T) { // TestIOFlushError covers ioFlush's write-failure branch: flushing a writable // file stream whose path is an unwritable directory raises Errno::ENOENT. func TestIOFlushError(t *testing.T) { - o := &IOObj{isStr: true, writable: true, path: "/no/such/dir/x.txt", buf: []byte("data")} + o := &IOObj{isStr: true, writable: true, wbufDirty: true, path: "/no/such/dir/x.txt", buf: []byte("data")} if got := catchRaise(func() { ioFlush(o) }); got != "Errno::ENOENT" { t.Errorf("ioFlush error: got %q want Errno::ENOENT", got) } // A non-writable or path-less stream flushes to a no-op (no raise). ioFlush(&IOObj{isStr: true, writable: false, path: "/no/such"}) ioFlush(&IOObj{isStr: true, writable: true, path: ""}) + // Nothing pending: no write at all, so even an unwritable path does not + // raise (#969). The same stream with wbufDirty set raised above. + if got := catchRaise(func() { + ioFlush(&IOObj{isStr: true, writable: true, path: "/no/such/dir/x.txt", buf: []byte("data")}) + }); got != "" { + t.Errorf("ioFlush with nothing pending raised %q", got) + } } diff --git a/internal/vm/io.go b/internal/vm/io.go index a87ac4fa..5bfffcfe 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -218,10 +218,11 @@ func (o *IOObj) writeBytes(p []byte) int { copy(o.buf[o.pos:], p) o.pos += len(p) o.syncStr() + if o.path != "" { + o.wbufDirty = true // bytes accepted but not on disk: MRI's fptr->wbuf.len + } if o.sync { // sync=true: a File's writes reach disk immediately (no buffering) ioFlush(o) - } else if o.path != "" { - o.wbufDirty = true // bytes accepted but not on disk: MRI's fptr->wbuf.len } return len(p) } @@ -1269,8 +1270,18 @@ func notRegular(p string) bool { // permission puts the owner-write bit back for the length of the write and // restores the file's mode afterwards. That is the descriptor's grant, replayed; // it never widens a file this stream was not already entitled to write. +// +// Only PENDING bytes are written (#969). The buffer also holds what the file +// contained at open, plus any bytes #ungetc pushed back, and neither belongs on +// disk: MRI's io_fflush with an empty write buffer writes nothing. Before this +// gate, a stream opened "r+" or "a" and closed untouched put its open-time +// snapshot back over another writer's bytes, and re-created a file unlinked +// while it was open. wbufDirty stands in for fptr->wbuf.len, and every write +// into the buffer that must reach the disk sets it (writeBytes, and #pwrite's +// buffer fallback); opens that truncate do so on disk at once and need no +// flush. func ioFlush(o *IOObj) { - if !o.writable || o.path == "" { + if !o.writable || o.path == "" || !o.wbufDirty { return } o.wbufDirty = false diff --git a/internal/vm/io_close_untouched_test.go b/internal/vm/io_close_untouched_test.go new file mode 100644 index 00000000..5d747bd8 --- /dev/null +++ b/internal/vm/io_close_untouched_test.go @@ -0,0 +1,54 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm_test + +import ( + "fmt" + "path/filepath" + "testing" +) + +// TestClosingAStreamWritesOnlyWhatWasWritten pins #969: a flush or close puts +// PENDING bytes on disk and nothing else. Every want is MRI 4.0.7's output for +// the same program (arm64-darwin25, rubydiff, 2026-11-21). Before the fix rbgo +// diverged on the first six rows, which wrote the stream's open-time snapshot +// (or a byte #ungetc pushed back) over the file. The rows after them are the +// writes that MUST still land, so a gate that stops too much goes red too. +func TestClosingAStreamWritesOnlyWhatWasWritten(t *testing.T) { + p := filepath.ToSlash(filepath.Join(t.TempDir(), "f")) + reset := fmt.Sprintf("File.write(%q, \"0123456789\"); ", p) + read := fmt.Sprintf("p File.read(%q)", p) + cases := []struct{ name, src, want string }{ + {"untouched r+ keeps another writer's bytes", + reset + fmt.Sprintf(`f = File.open(%q, "r+"); File.write(%q, "NEW"); f.close; `, p, p) + read, "\"NEW\"\n"}, + {"untouched a keeps another writer's bytes", + reset + fmt.Sprintf(`f = File.open(%q, "a"); File.write(%q, "NEW"); f.close; `, p, p) + read, "\"NEW\"\n"}, + {"untouched a+ keeps another writer's bytes", + reset + fmt.Sprintf(`f = File.open(%q, "a+"); File.write(%q, "NEW"); f.close; `, p, p) + read, "\"NEW\"\n"}, + {"untouched w+ keeps another writer's bytes", + reset + fmt.Sprintf(`f = File.open(%q, "w+"); File.write(%q, "NEW"); f.close; `, p, p) + read, "\"NEW\"\n"}, + {"untouched r+ does not bring back an unlinked file", + reset + fmt.Sprintf(`f = File.open(%q, "r+"); File.unlink(%q); f.close; p File.exist?(%q)`, p, p, p), "false\n"}, + {"ungetc is never written to the file", + reset + fmt.Sprintf(`f = File.open(%q, "r+"); f.ungetc("X"); f.close; `, p) + read, "\"0123456789\"\n"}, + {"a write still lands at close", + reset + fmt.Sprintf(`f = File.open(%q, "r+"); f.write("AB"); f.close; `, p) + read, "\"AB23456789\"\n"}, + {"a sync write lands before close", + reset + fmt.Sprintf(`f = File.open(%q, "r+"); f.sync = true; f.write("CD"); `, p) + read, "\"CD23456789\"\n"}, + {"an append lands at flush", + reset + fmt.Sprintf(`f = File.open(%q, "a"); f.write("Z"); f.flush; `, p) + read, "\"0123456789Z\"\n"}, + {"w truncates with no write", + reset + fmt.Sprintf(`File.open(%q, "w").close; `, p) + read, "\"\"\n"}, + {"truncate lands with no write", + reset + fmt.Sprintf(`f = File.open(%q, "r+"); f.truncate(4); f.close; `, p) + read, "\"0123\"\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) + } + }) + } +} From 5bdae137dc2a9df0160f99c1114ceb43c251000f Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sat, 21 Nov 2026 18:27:57 +0100 Subject: [PATCH 2/2] vm: assert each clause of ioFlush's guard instead of relying on a crash mutsweep caught the guard's mutant only through a panic: the no-op flushes in TestIOFlushError were called bare, so a write attempted on an unwritable path panicked rather than failed an assertion. Each no-op is now a row checked with catchRaise, and each row is refused by one clause alone: not writable, no path, nothing pending. Refs #969 Co-Authored-By: Claude Opus 5.5 --- internal/vm/filestat_internal_test.go | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/internal/vm/filestat_internal_test.go b/internal/vm/filestat_internal_test.go index 79c4bc7c..8d089e47 100644 --- a/internal/vm/filestat_internal_test.go +++ b/internal/vm/filestat_internal_test.go @@ -445,14 +445,23 @@ func TestIOFlushError(t *testing.T) { if got := catchRaise(func() { ioFlush(o) }); got != "Errno::ENOENT" { t.Errorf("ioFlush error: got %q want Errno::ENOENT", got) } - // A non-writable or path-less stream flushes to a no-op (no raise). - ioFlush(&IOObj{isStr: true, writable: false, path: "/no/such"}) - ioFlush(&IOObj{isStr: true, writable: true, path: ""}) - // Nothing pending: no write at all, so even an unwritable path does not - // raise (#969). The same stream with wbufDirty set raised above. - if got := catchRaise(func() { - ioFlush(&IOObj{isStr: true, writable: true, path: "/no/such/dir/x.txt", buf: []byte("data")}) - }); got != "" { - t.Errorf("ioFlush with nothing pending raised %q", got) + // Each of these is a no-op: no write is attempted, so none raises. Every row + // is pending (wbufDirty) except the last, so each one is refused by its own + // clause of the guard alone. The unwritable path makes an attempted write + // visible as Errno::ENOENT rather than as a file appearing somewhere. + noops := []struct { + name string + o *IOObj + }{ + {"not writable", &IOObj{isStr: true, writable: false, wbufDirty: true, path: "/no/such/dir/x.txt"}}, + {"no path", &IOObj{isStr: true, writable: true, wbufDirty: true, path: ""}}, + // #969: nothing pending, so even a writable stream with a path does not + // write. The same stream with wbufDirty set raised above. + {"nothing pending", &IOObj{isStr: true, writable: true, path: "/no/such/dir/x.txt", buf: []byte("data")}}, + } + for _, c := range noops { + if got := catchRaise(func() { ioFlush(c.o) }); got != "" { + t.Errorf("%s: ioFlush attempted a write and raised %q", c.name, got) + } } }