From 8f0092fa0275d69ce6d7c2afdf4300039aaf6416 Mon Sep 17 00:00:00 2001 From: Roman Volosatovs Date: Mon, 14 Jul 2025 20:13:12 +0200 Subject: [PATCH 1/4] Revert "test(wasip1): ignore `fd_renumber` tests when using adapter" This reverts commit 00325ff36d44a3cf666085d56a7c80e907636d8b. --- crates/wasi/tests/all/p2/async_.rs | 1 - crates/wasi/tests/all/p2/sync.rs | 1 - 2 files changed, 2 deletions(-) diff --git a/crates/wasi/tests/all/p2/async_.rs b/crates/wasi/tests/all/p2/async_.rs index c675874a0985..5094e8add29d 100644 --- a/crates/wasi/tests/all/p2/async_.rs +++ b/crates/wasi/tests/all/p2/async_.rs @@ -231,7 +231,6 @@ async fn preview1_remove_nonempty_directory() { .await .unwrap() } -#[ignore = "panics"] #[test_log::test(tokio::test(flavor = "multi_thread"))] async fn preview1_renumber() { run(PREVIEW1_RENUMBER_COMPONENT, false).await.unwrap() diff --git a/crates/wasi/tests/all/p2/sync.rs b/crates/wasi/tests/all/p2/sync.rs index 3c0bfefb86e4..435de4959cb1 100644 --- a/crates/wasi/tests/all/p2/sync.rs +++ b/crates/wasi/tests/all/p2/sync.rs @@ -187,7 +187,6 @@ fn preview1_remove_directory() { fn preview1_remove_nonempty_directory() { run(PREVIEW1_REMOVE_NONEMPTY_DIRECTORY_COMPONENT, false).unwrap() } -#[ignore = "panics"] #[test_log::test] fn preview1_renumber() { run(PREVIEW1_RENUMBER_COMPONENT, false).unwrap() From 0526f3f1402833c3b3881f1c6a184bf5f35d338a Mon Sep 17 00:00:00 2001 From: Roman Volosatovs Date: Mon, 14 Jul 2025 20:12:54 +0200 Subject: [PATCH 2/4] fix(wasip1-adapter): prevent `unreachable` panic on `fd_renumber` Signed-off-by: Roman Volosatovs --- .../src/bin/preview1_renumber.rs | 10 ++++++- .../src/descriptors.rs | 30 +++++++++++-------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/crates/test-programs/src/bin/preview1_renumber.rs b/crates/test-programs/src/bin/preview1_renumber.rs index ec3a0a2e2362..394e5776f4f4 100644 --- a/crates/test-programs/src/bin/preview1_renumber.rs +++ b/crates/test-programs/src/bin/preview1_renumber.rs @@ -91,7 +91,15 @@ unsafe fn test_renumber(dir_fd: wasip1::Fd) { "file descriptor range check", ); - wasip1::fd_renumber(fd_file3, u32::MAX).expect("renumbering file FD to `u32::MAX`"); + wasip1::fd_renumber(fd_file3, 127).expect("renumbering FD to 127"); + match wasip1::fd_renumber(127, u32::MAX) { + Err(wasip1::ERRNO_NOMEM) => { + // The preview1 adapter cannot handle more than 128 descriptors + eprintln!("fd_renumber({fd_file3}, {}) returned NOMEM", u32::MAX) + } + res => res.expect("renumbering FD to `u32::MAX`"), + } + let fd_file4 = wasip1::path_open( dir_fd, 0, diff --git a/crates/wasi-preview1-component-adapter/src/descriptors.rs b/crates/wasi-preview1-component-adapter/src/descriptors.rs index 47d34395604c..da3dc6e70513 100644 --- a/crates/wasi-preview1-component-adapter/src/descriptors.rs +++ b/crates/wasi-preview1-component-adapter/src/descriptors.rs @@ -330,8 +330,8 @@ impl Descriptors { .ok_or(wasi::ERRNO_BADF) } - // Internal: close a fd, returning the descriptor. - fn close_(&mut self, fd: Fd) -> Result { + // Close an fd. + pub fn close(&mut self, fd: Fd) -> Result<(), Errno> { // Throw an error if closing an fd which is already closed match self.get(fd)? { Descriptor::Closed(_) => Err(wasi::ERRNO_BADF)?, @@ -341,12 +341,7 @@ impl Descriptors { let last_closed = self.closed; let prev = std::mem::replace(self.get_mut(fd)?, Descriptor::Closed(last_closed)); self.closed = Some(fd); - Ok(prev) - } - - // Close an fd. - pub fn close(&mut self, fd: Fd) -> Result<(), Errno> { - drop(self.close_(fd)?); + drop(prev); Ok(()) } @@ -366,11 +361,20 @@ impl Descriptors { while self.table_len.get() as u32 <= to_fd { self.push_closed()?; } - // Then, close from_fd and put its contents into to_fd: - let desc = self.close_(from_fd)?; - // TODO FIXME if this overwrites a preopen, do we need to clear it from the preopen table? - *self.get_mut(to_fd)? = desc; - + // Throw an error if renumbering a closed fd + match self.get(from_fd)? { + Descriptor::Closed(_) => Err(wasi::ERRNO_BADF)?, + _ => {} + } + // Close from_fd and put its contents into to_fd + if from_fd != to_fd { + // Mutate the descriptor to be closed, and push the closed fd onto the head of the linked list: + let last_closed = self.closed; + let desc = std::mem::replace(self.get_mut(from_fd)?, Descriptor::Closed(last_closed)); + self.closed = Some(from_fd); + // TODO FIXME if this overwrites a preopen, do we need to clear it from the preopen table? + *self.get_mut(to_fd)? = desc; + } Ok(()) } From 2570eaca6b53a199cc2ea8d0b8b82bde062581d1 Mon Sep 17 00:00:00 2001 From: Roman Volosatovs Date: Tue, 15 Jul 2025 14:00:09 +0200 Subject: [PATCH 3/4] doc: document adapter panic fix Signed-off-by: Roman Volosatovs --- RELEASES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/RELEASES.md b/RELEASES.md index 657f220ff044..70421b117c84 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -11,6 +11,8 @@ Unreleased. * Fix a panic in the host caused by preview1 guests using `fd_renumber`. [CVE-2025-53901](https://github.com/bytecodealliance/wasmtime/security/advisories/GHSA-fm79-3f68-h2fc). +* Fix a panic in the preview1 adapter caused by guests using `fd_renumber`. + -------------------------------------------------------------------------------- Release notes for previous releases of Wasmtime can be found on the respective From dc16106525c0a1c230bb6c73c41d7690f9c3b0f5 Mon Sep 17 00:00:00 2001 From: Roman Volosatovs Date: Fri, 18 Jul 2025 13:42:49 +0200 Subject: [PATCH 4/4] doc: add PR reference prtest:full Signed-off-by: Roman Volosatovs --- RELEASES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/RELEASES.md b/RELEASES.md index 70421b117c84..f6ee9933aba4 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -12,6 +12,7 @@ Unreleased. [CVE-2025-53901](https://github.com/bytecodealliance/wasmtime/security/advisories/GHSA-fm79-3f68-h2fc). * Fix a panic in the preview1 adapter caused by guests using `fd_renumber`. + [#11277](https://github.com/bytecodealliance/wasmtime/pull/11277) --------------------------------------------------------------------------------