From 9e9caae31e171bd8e562703005c594be0aea5875 Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Thu, 14 Sep 2023 10:15:51 +0800 Subject: [PATCH] fix: Ignore fd_close(3) to avoid breaking POSIX programs that blindly close all file descriptors --- lib/wasix/src/syscalls/wasi/fd_close.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/wasix/src/syscalls/wasi/fd_close.rs b/lib/wasix/src/syscalls/wasi/fd_close.rs index 6c217ec36e3..1d38b4391c6 100644 --- a/lib/wasix/src/syscalls/wasi/fd_close.rs +++ b/lib/wasix/src/syscalls/wasi/fd_close.rs @@ -16,6 +16,18 @@ use crate::syscalls::*; pub fn fd_close(mut ctx: FunctionEnvMut<'_, WasiEnv>, fd: WasiFd) -> Result { wasi_try_ok!(WasiEnv::process_signals_and_exit(&mut ctx)?); + // HACK: As a special case, we don't want to close fd 3 because it can break + // some programs... + // - On Wasix, we have some default fd, 0:stdin, 1:stdout, 2:stderr, 3:root + // - On Posix, we have some default fd: 0:stdin, 1:stdout, 2:stderr + // - A POSIX program might want to closed all open fd (e.g. when Python + // spawns subprocesses), so it blindly does fd_close() for fd=3..255 + // - Wasix doesn't work well when 3:root is closed, because it's the root + if fd == 3 { + tracing::debug!("Skipping fd_close(3)"); + return Ok(Errno::Success); + } + let env = ctx.data(); let (_, mut state) = unsafe { env.get_memory_and_wasi_state(&ctx, 0) }; wasi_try_ok!(state.fs.close_fd(fd));