Skip to content

vm: IO#pwrite and IO#pread reach the file, not the stream's buffer - #970

Merged
tannevaled merged 6 commits into
mainfrom
pwrite-reaches-the-file
Nov 21, 2026
Merged

tannevaled merged 6 commits into
mainfrom
pwrite-reaches-the-file

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Summary

Closes #949.

In MRI, IO#pwrite and IO#pread are pwrite(2) and pread(2) on the descriptor. rbgo's File streams are buffer-backed and hold no descriptor, so both methods wrote and read only the in-memory buffer.

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, as in MRI. A permission refusal is retried with the owner-write bit restored for the length of the write (the same grant ioFlush replays), so a stream created 0444 writes through.
  • #pread first flushes this stream's own pending writes, as rb_io_pread does, then reads with io.ReadAll(io.NewSectionReader(…)). The allocation is bounded by what the file holds, not by the length Ruby asks for.
  • A stream with no regular file to reach falls back to the buffer, as before. That covers a StringIO, a pipe end, a popen stream, a device or fifo, and an unlinked path.

Measured against MRI 4.0.7

rubydiff (rbgo vs /opt/homebrew/opt/ruby/bin/ruby, 4.0.7 +PRISM, arm64-darwin25). The file holds 0123456789:

case MRI 4.0.7 rbgo before rbgo after
pwrite("AB", 0), then File.read "AB23456789" "0123456789" "AB23456789"
another writer, then pread(3, 0) "xyz" "012" "xyz"
write("QQ"), pwrite("AB", 5), then File.read "01234AB789" "0123456789" "01234AB789"
pwrite("Z", 12) "0123456789\u0000\u0000Z" "0123456789" same as MRI
File.new(p, "w", 0444), then pwrite("AB", 0) "AB", mode 444 "", mode 444 "AB", mode 444
write("QQ"), then pread(2, 0) "QQ" "QQ" "QQ"
position after read(3), then pwrite 3, "34" same same
pwrite/pread on a read-only or write-only stream IOError same same

Before this change the probe diverged on 6 of 11 output lines. After it, it diverges on none.

One deliberate divergence: on a 10-byte file, f.pread(2**40, 7) returns "789" at once in rbgo. MRI 4.0.7 did not answer within 30 s, because it allocates the full length first.

Tests

  • TestPositionalIOReachesTheFile has 8 rows, each pinned to MRI's measured output.
  • TestPreadLengthIsBoundedByTheFile covers the bound.
  • Package-internal tests drive each refusal in the helpers.
  • mutsweep v0.12.0 swept every changed refusal; all are caught by an assertion. The first sweep caught 5 of 12; the code was restructured where a mutant changed nothing observable.
  • The full internal/vm suite is green locally (367 s), as are gofmt -l and go vet.

Docs

The README's divergence row is rewritten. What remains is a path that no longer names the opened file: an unlinked path falls back to the buffer, where MRI still reaches the old inode, and a replaced path reaches the new file.

Found while measuring, not fixed here

Refs #969: closing a writable stream that was never written to rewrites the file from its buffer. Another writer's bytes are lost, and an unlinked file comes back. It predates this change; the measurement and the shape of a fix are in the issue.

Security

  • The length passed to pread comes from Ruby and no longer sizes an allocation.
  • The owner-write retry restores the exact previous mode and never widens it beyond the write. It is the same rule ioFlush already applies.
  • Reaching the file by path has the time-of-check/time-of-use exposure ioFlush already has: a path swapped for another file between open and pwrite is written. A per-stream descriptor would remove it, which is the root of Refs File::Stat: no atime/ctime on Windows, and File#stat cannot answer for an unlinked open file #635.

🤖 Generated with Claude Code

tannevaled and others added 3 commits November 21, 2026 17:56
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 <noreply@anthropic.com>
…s 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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
tannevaled and others added 3 commits November 21, 2026 20:03
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 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IO#pwrite writes the buffer, not the descriptor: the bytes are not on disk until a flush

1 participant