From 0b332d89874c780086da785306010764dff2ef0d Mon Sep 17 00:00:00 2001 From: Yage Hu Date: Wed, 6 Dec 2023 17:52:53 -0800 Subject: [PATCH] Portably handle `fd_advise` on directory fd (#2875) This commit adds a check to `fd_advise`. If the fd is a directory, return `ebadf`. This brings iwasm in line with Wasmtime's behavior. WASI folks have stated that fd_advise should not work on directories as this is a Linux-specific behavior: https://github.com/bytecodealliance/wasmtime/issues/6505#issuecomment-1574122949 --- .../libc-wasi/sandboxed-system-primitives/src/posix.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c index 0f8f4b32bb..b59035f349 100644 --- a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c +++ b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c @@ -1155,6 +1155,11 @@ wasmtime_ssp_fd_advise(wasm_exec_env_t exec_env, struct fd_table *curfds, if (error != 0) return error; + if (fo->type == __WASI_FILETYPE_DIRECTORY) { + fd_object_release(exec_env, fo); + return __WASI_EBADF; + } + error = os_fadvise(fo->file_handle, offset, len, advice); fd_object_release(exec_env, fo);