Skip to content

Commit a1677ee

Browse files
lewingCopilot
andcommitted
Don't close borrowed fds in WASI PAL cleanup routines
Review feedback on PR #128465 flagged two fd-ownership bugs on WASI: - init_std_handle in file.cpp:2445 reuses fileno(stdin/stdout/stderr) without dup. FileCleanupRoutine would close the process's real stdio fds when the PAL handle is released. - InternalCreateFileMapping in map.cpp:474 reuses the source file's unix_fd without dup. FileMappingCleanupRoutine would close the source file's fd while it's still in use and double-close it when the source file handle is later released. The WASI sysroot does not expose dup(), dup2(), or dup3() — they're all hidden behind __wasilibc_unmodified_upstream and not exported. The only fd primitive available is fd_renumber (move, not duplicate). Rather than threading a new 'non-owning' flag through the cross-platform CFileProcessLocalData / CFileMappingProcessLocalData structs (which would leak WASI-specific state into shared PAL code), guard the close() calls locally in the cleanup routines: - FileCleanupRoutine: on WASI, only close fds > STDERR_FILENO. fds 0/1/2 are the borrowed stdio fds from init_std_handle and must not be closed by the PAL. - FileMappingCleanupRoutine: on WASI, skip the close() entirely. The mapping borrowed the fd from the source CFileProcessLocalData; mmap-wasi.c already copies file contents into a malloc'd buffer at mmap() time, so the mapping does not need the fd after view creation, and the source file handle owns its fd's lifetime. Co-authored-by: Copilot <[email protected]>
1 parent f83ba68 commit a1677ee

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎src/coreclr/pal/src/file/file.cpp‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,16 @@ FileCleanupRoutine(
133133

134134
if (!fShutdown && -1 != pLocalData->unix_fd)
135135
{
136-
close(pLocalData->unix_fd);
136+
#ifdef TARGET_WASI
137+
// On WASI we never dup()'d the fd (dup is not available in the WASI
138+
// sysroot). init_std_handle stores fileno(stdin/stdout/stderr)
139+
// directly, so closing here would close the process's real stdio.
140+
// Other file handles get a real fd from open() and are safe to close.
141+
if (pLocalData->unix_fd > STDERR_FILENO)
142+
#endif // TARGET_WASI
143+
{
144+
close(pLocalData->unix_fd);
145+
}
137146
}
138147

139148
pLocalDataLock->ReleaseLock(pThread, FALSE);

‎src/coreclr/pal/src/map/map.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,17 @@ FileMappingCleanupRoutine(
219219

220220
if (-1 != pLocalData->UnixFd)
221221
{
222+
#ifdef TARGET_WASI
223+
// On WASI we never dup()'d the source file's fd (dup is not
224+
// available in the WASI sysroot). The mapping borrowed the fd
225+
// from the source CFileProcessLocalData; closing it here would
226+
// close the source file. mmap-wasi.c already copies file
227+
// contents into a malloc'd buffer at mmap() time, so the
228+
// mapping does not need the fd after view creation. Lifetime
229+
// of the fd belongs to the originating file handle.
230+
#else
222231
close(pLocalData->UnixFd);
232+
#endif // !TARGET_WASI
223233
pLocalData->UnixFd = -1;
224234
fDataChanged = TRUE;
225235
}

0 commit comments

Comments
 (0)