Repository navigation
Commit a1677ee
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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
133 | 133 | | |
134 | 134 | | |
135 | 135 | | |
136 | | - | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
137 | 146 | | |
138 | 147 | | |
139 | 148 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
219 | 219 | | |
220 | 220 | | |
221 | 221 | | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
222 | 231 | | |
| 232 | + | |
223 | 233 | | |
224 | 234 | | |
225 | 235 | | |
| |||
0 commit comments