From cf8b932e34b209025fbfbdc591f402c501878a20 Mon Sep 17 00:00:00 2001 From: "liang.he@intel.com" Date: Tue, 18 Feb 2025 02:52:48 +0000 Subject: [PATCH] initialize WASI stdio handles to invalid for better error handling --- core/iwasm/aot/aot_loader.c | 10 ++++++++++ core/iwasm/interpreter/wasm_loader.c | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index 97360e73e..c36da6bcf 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -4120,6 +4120,16 @@ create_module(char *name, char *error_buf, uint32 error_buf_size) } #endif +#if WASM_ENABLE_LIBC_WASI != 0 + /* + * learned from all implementations of `os_get_invalid_handle()` + * that invalid handles across all platforms are represented by -1. + */ + module->wasi_args.stdio[0] = -1; + module->wasi_args.stdio[1] = -1; + module->wasi_args.stdio[2] = -1; +#endif + return module; #if WASM_ENABLE_GC != 0 fail2: diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index e72bf94fa..3cce5543c 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -6368,6 +6368,16 @@ create_module(char *name, char *error_buf, uint32 error_buf_size) } #endif +#if WASM_ENABLE_LIBC_WASI != 0 + /* + * learned from all implementations of `os_get_invalid_handle()` + * that invalid handles across all platforms are represented by -1. + */ + module->wasi_args.stdio[0] = -1; + module->wasi_args.stdio[1] = -1; + module->wasi_args.stdio[2] = -1; +#endif + (void)ret; return module;