Skip to content

Commit 21b0d6e

Browse files
committed
Fix WasiCtxBuilder so that stdin, stdout, and stderr accept files.
A regression in bytecodealliance#1561 caused "invalid argument" errors when `stdin`, `stdout`, and `stderr` are given a file handle. This was missed in Wasmtime CI because previously only a pipe handle was being used. It was caught in the .NET implementation CI because it uses file handles for its WASI tests. Fixes bytecodealliance#1735.
1 parent c9e3b71 commit 21b0d6e

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

crates/wasi-common/src/ctx.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::entry::{Entry, EntryHandle};
22
use crate::fdpool::FdPool;
33
use crate::handle::Handle;
44
use crate::sys::osdir::OsDir;
5+
use crate::sys::osfile::OsFile;
56
use crate::sys::osother::{OsOther, OsOtherExt};
67
use crate::sys::stdio::{Stderr, StderrExt, Stdin, StdinExt, Stdout, StdoutExt};
78
use crate::virtfs::{VirtualDir, VirtualDirEntry};
@@ -369,8 +370,11 @@ impl WasiCtxBuilder {
369370
.ok_or(WasiCtxBuilderError::TooManyFilesOpen)?
370371
}
371372
PendingEntry::OsHandle(f) => {
372-
let handle = OsOther::try_from(f)?;
373-
let handle = EntryHandle::new(handle);
373+
let handle = if f.metadata()?.is_file() {
374+
EntryHandle::new(OsFile::try_from(f)?)
375+
} else {
376+
EntryHandle::new(OsOther::try_from(f)?)
377+
};
374378
let entry = Entry::new(handle);
375379
entries
376380
.insert(entry)
@@ -379,6 +383,7 @@ impl WasiCtxBuilder {
379383
};
380384
log::debug!("WasiCtx inserted at {:?}", fd);
381385
}
386+
382387
// Then add the preopen entries.
383388
for (guest_path, preopen) in self.preopens.take().unwrap() {
384389
let handle = EntryHandle::from(preopen.into()?);

0 commit comments

Comments
 (0)