Skip to content

vm: map every Win32 and Winsock code through MRI's errmap - #982

Open
tannevaled wants to merge 1 commit into
mainfrom
win32-errno-map
Open

tannevaled wants to merge 1 commit into
mainfrom
win32-errno-map

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

What

On Windows a syscall.Errno is the Win32 code itself. errnoClasses is keyed by the numbers Go gives the POSIX names, so every raiser that looked a failed call's number up directly picked an unrelated class, or none. Go defines syscall.ENOTDIR as ERROR_PATH_NOT_FOUND (3), so a path through a missing directory read as Errno::ENOTDIR. ERROR_DIR_NOT_EMPTY never matched ENOTEMPTY, so Dir.rmdir on a non-empty directory fell to Errno::ENOENT.

MRI maps the code first, through errmap and rb_w32_map_errno (win32/win32.c:145-300, v4.0.9). This PR does the same in one place:

  • errnoOfError returns the mapped number. sysFail, Dir.chdir, File.realpath, IO#pwrite, the flush and the connect error all see MRI's class. raiseFlushErr no longer maps on its own.
  • errIsErrno compares the mapped number. Dir.rmdir's ENOTEMPTY, and the two direct errors.Is(err, syscall.EINVAL) checks it now replaces (File.truncate, File#flock), see the Win32 codes.
  • The Winsock rows of errmap are ported in full: 43, where posixErrno held 15. posixErrno is removed.

Everywhere but Windows, mapHostErrno is the identity.

What stays a divergence

  • Eight Winsock classes: ESHUTDOWN, ETOOMANYREFS, EHOSTDOWN, EPFNOSUPPORT, ESOCKTNOSUPPORT, EPROCLIM, EUSERS and EREMOTE have no number in rbgo on any target (errnoUndefinedEverywhere). Those rows keep their WSA code and raise SystemCallError, as before. TestWSARowsWithoutAClass pins the list, so defining one of the names moves it out.
  • ENOTDIR built by Go: on Windows, an ENOTDIR that Go builds itself (os.MkdirAll, os.ReadDir, filepath.EvalSymlinks) is the number 3, which is indistinguishable from ERROR_PATH_NOT_FOUND, and maps to ENOENT. No raiser takes its error from those three today. The comment on errnoOfError says so.

Measured

  • Oracle: rubydiff against MRI 4.0.7 (arm64-darwin25). A program covering Dir.chdir through a missing directory, Dir.rmdir on a non-empty one, and File.truncate through a missing directory: the two engines agree. MRI prints Errno::ENOENT, Errno::ENOTEMPTY, Errno::ENOENT.
  • Tests:
    • TestAFailedCallRaisesMRIsClassOnEveryHost holds that program with MRI's output as its expected value. It runs on every lane. On darwin and Linux it passes with or without the mapping; the Windows lane is the oracle for this PR, since no local run covers Windows.
    • The Windows-only tests (TestMapHostErrnoIsRbW32MapErrno, TestWSARowsWithoutAClass, TestErrnoOfErrorMapsTheWin32Code) check the table against errmap row by row, plus the class errnoOfError and errIsErrno give a wrapped Win32 error.
  • Suite: go test ./internal/vm/ ./cmd/rbgo/ ./ passed locally on darwin (internal/vm 295 s).
  • Vet: buildscan --vet passed under all 19 CI configurations. AIX and Plan 9, which no lane builds, cross-build.

Fixes #980
Refs #978 (merged; it ported the Win32 rows and applied them to the flush alone)

🤖 Generated with Claude Code

On Windows a syscall.Errno is the Win32 code itself, and errnoClasses is
keyed by the numbers Go gives the POSIX names, so every raiser that looked a
failed call's number up directly picked an unrelated class, or none.
ERROR_PATH_NOT_FOUND (3) is Go's syscall.ENOTDIR there, so a path through a
missing directory read as Errno::ENOTDIR where MRI raises Errno::ENOENT
(witnessed on #978's Windows runner, for the flush), and rmdir of a
non-empty directory (ERROR_DIR_NOT_EMPTY) fell to Errno::ENOENT.

MRI maps the code first, through errmap and rb_w32_map_errno
(win32/win32.c:145-300, v4.0.9). errnoOfError now does the same, once, so
sysFail, chdir, realpath, pwrite, the flush and the dial error all see MRI's
class. errIsErrno falls back on the mapped class, so ERROR_DIR_NOT_EMPTY is
ENOTEMPTY for rmdir, and the two direct errors.Is(err, syscall.EINVAL) checks
(File.truncate, flock) go through it.

The Winsock rows are ported in full (43, where posixErrno held 15), and
posixErrno is gone. Eight of their classes have no number in rbgo on any
target (ESHUTDOWN, ETOOMANYREFS, EHOSTDOWN, EPFNOSUPPORT, ESOCKTNOSUPPORT,
EPROCLIM, EUSERS, EREMOTE); those keep the WSA code and raise
SystemCallError, as before. TestWSARowsWithoutAClass pins that list.

Everywhere but Windows mapHostErrno is the identity.

Fixes #980

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.

Windows: Errno classes come from the raw Win32 code; MRI maps it through rb_w32_map_errno first

1 participant