From 9bedc46868e170bd42a292046990df48ce896b5c Mon Sep 17 00:00:00 2001 From: eloparco Date: Tue, 21 Feb 2023 15:43:02 +0000 Subject: [PATCH 1/8] fix(poll_oneoff): fix timeout in poll_oneoff --- core/iwasm/common/wasm_runtime_common.c | 6 ++++++ core/iwasm/common/wasm_runtime_common.h | 4 ++++ core/iwasm/include/wasm_export.h | 9 +++++++++ .../libraries/libc-wasi/libc_wasi_wrapper.c | 17 ++++++++--------- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 4aa3d7ed89..8083f5b395 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -1550,6 +1550,12 @@ wasm_runtime_get_module_inst(WASMExecEnv *exec_env) return wasm_exec_env_get_module_inst(exec_env); } +uint32 +wasm_runtime_get_suspend_flags(WASMExecEnv *exec_env) +{ + return exec_env->suspend_flags.flags; +} + void wasm_runtime_set_module_inst(WASMExecEnv *exec_env, WASMModuleInstanceCommon *const module_inst) diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index b20e12ecf9..bcdf81599c 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -574,6 +574,10 @@ wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env); WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon * wasm_runtime_get_module_inst(WASMExecEnv *exec_env); +/* See wasm_export.h for description */ +WASM_RUNTIME_API_EXTERN uint32 +wasm_runtime_get_suspend_flags(WASMExecEnv *exec_env); + /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN void wasm_runtime_set_module_inst(WASMExecEnv *exec_env, diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index 7f82b6de5f..7e256681ba 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -716,6 +716,15 @@ wasm_runtime_thread_env_inited(void); WASM_RUNTIME_API_EXTERN wasm_module_inst_t wasm_runtime_get_module_inst(wasm_exec_env_t exec_env); +/** + * Get suspend flags from the execution environment + * + * @param exec_env the execution environment to get the suspend flags from + * @return the suspend flags + */ +WASM_RUNTIME_API_EXTERN uint32_t +wasm_runtime_get_suspend_flags(wasm_exec_env_t exec_env); + /** * Set WASM module instance of execution environment * Caution: diff --git a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c index 815fa5aaf5..bc2a022ca1 100644 --- a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c +++ b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c @@ -14,6 +14,9 @@ wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception); #define get_module_inst(exec_env) \ wasm_runtime_get_module_inst(exec_env) +#define get_suspend_flags(exec_env) \ + wasm_runtime_get_suspend_flags(exec_env) + #define get_wasi_ctx(module_inst) \ wasm_runtime_get_wasi_ctx(module_inst) @@ -995,7 +998,7 @@ execute_interruptible_poll_oneoff(wasm_module_inst_t module_inst, #endif const __wasi_subscription_t *in, __wasi_event_t *out, size_t nsubscriptions, - size_t *nevents) + size_t *nevents, wasm_exec_env_t exec_env) { if (nsubscriptions == 0) { *nevents = 0; @@ -1021,24 +1024,20 @@ execute_interruptible_poll_oneoff(wasm_module_inst_t module_inst, bh_memcpy_s(in_copy, size_to_copy, in, size_to_copy); while (timeout == (__wasi_timestamp_t)-1 || elapsed <= timeout) { - elapsed += time_quant; - /* update timeout for clock subscription events */ update_clock_subscription_data(in_copy, nsubscriptions, min(time_quant, timeout - elapsed)); err = wasmtime_ssp_poll_oneoff(curfds, in_copy, out, nsubscriptions, nevents); + elapsed += time_quant; + if (err) { wasm_runtime_free(in_copy); return err; } - if (wasm_runtime_get_exception(module_inst) || *nevents > 0) { + if (get_suspend_flags(exec_env) & 0x01) { wasm_runtime_free(in_copy); - - if (*nevents) { - return __WASI_ESUCCESS; - } return EINTR; } } @@ -1070,7 +1069,7 @@ wasi_poll_oneoff(wasm_exec_env_t exec_env, const wasi_subscription_t *in, err = wasmtime_ssp_poll_oneoff(curfds, in, out, nsubscriptions, &nevents); #else err = execute_interruptible_poll_oneoff(module_inst, curfds, in, out, - nsubscriptions, &nevents); + nsubscriptions, &nevents, exec_env); #endif if (err) return err; From b8067df34f73eb38cdc283ce6033956b57953777 Mon Sep 17 00:00:00 2001 From: eloparco Date: Wed, 22 Feb 2023 08:57:39 +0000 Subject: [PATCH 2/8] fix: avoid exposing api for getting suspend flags --- core/iwasm/common/wasm_runtime_common.c | 6 ------ core/iwasm/common/wasm_runtime_common.h | 4 ---- core/iwasm/include/wasm_export.h | 9 --------- core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c | 13 +++++++++---- core/iwasm/libraries/thread-mgr/thread_manager.c | 6 ++++++ core/iwasm/libraries/thread-mgr/thread_manager.h | 3 +++ 6 files changed, 18 insertions(+), 23 deletions(-) diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 8083f5b395..4aa3d7ed89 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -1550,12 +1550,6 @@ wasm_runtime_get_module_inst(WASMExecEnv *exec_env) return wasm_exec_env_get_module_inst(exec_env); } -uint32 -wasm_runtime_get_suspend_flags(WASMExecEnv *exec_env) -{ - return exec_env->suspend_flags.flags; -} - void wasm_runtime_set_module_inst(WASMExecEnv *exec_env, WASMModuleInstanceCommon *const module_inst) diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index bcdf81599c..b20e12ecf9 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -574,10 +574,6 @@ wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env); WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon * wasm_runtime_get_module_inst(WASMExecEnv *exec_env); -/* See wasm_export.h for description */ -WASM_RUNTIME_API_EXTERN uint32 -wasm_runtime_get_suspend_flags(WASMExecEnv *exec_env); - /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN void wasm_runtime_set_module_inst(WASMExecEnv *exec_env, diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index 7e256681ba..7f82b6de5f 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -716,15 +716,6 @@ wasm_runtime_thread_env_inited(void); WASM_RUNTIME_API_EXTERN wasm_module_inst_t wasm_runtime_get_module_inst(wasm_exec_env_t exec_env); -/** - * Get suspend flags from the execution environment - * - * @param exec_env the execution environment to get the suspend flags from - * @return the suspend flags - */ -WASM_RUNTIME_API_EXTERN uint32_t -wasm_runtime_get_suspend_flags(wasm_exec_env_t exec_env); - /** * Set WASM module instance of execution environment * Caution: diff --git a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c index bc2a022ca1..e541ff1086 100644 --- a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c +++ b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c @@ -7,6 +7,11 @@ #include "bh_platform.h" #include "wasm_export.h" +#if WASM_ENABLE_THREAD_MGR != 0 +#include "../../../thread-mgr/thread_manager.h" +typedef struct WASIContext *wasi_ctx_t; +#endif + void wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception); @@ -14,9 +19,6 @@ wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception); #define get_module_inst(exec_env) \ wasm_runtime_get_module_inst(exec_env) -#define get_suspend_flags(exec_env) \ - wasm_runtime_get_suspend_flags(exec_env) - #define get_wasi_ctx(module_inst) \ wasm_runtime_get_wasi_ctx(module_inst) @@ -49,6 +51,8 @@ typedef struct iovec_app { uint32 buf_len; } iovec_app_t; +/* If the thread manager is enabled, WASIContext is already defined */ +#if WASM_ENABLE_THREAD_MGR == 0 typedef struct WASIContext { struct fd_table *curfds; struct fd_prestats *prestats; @@ -62,6 +66,7 @@ typedef struct WASIContext { char **env_list; uint32_t exit_code; } * wasi_ctx_t; +#endif wasi_ctx_t wasm_runtime_get_wasi_ctx(wasm_module_inst_t module_inst); @@ -1036,7 +1041,7 @@ execute_interruptible_poll_oneoff(wasm_module_inst_t module_inst, return err; } - if (get_suspend_flags(exec_env) & 0x01) { + if (wasm_cluster_is_thread_terminated(exec_env)) { wasm_runtime_free(in_copy); return EINTR; } diff --git a/core/iwasm/libraries/thread-mgr/thread_manager.c b/core/iwasm/libraries/thread-mgr/thread_manager.c index 9c1b1d6100..4d66beb6fd 100644 --- a/core/iwasm/libraries/thread-mgr/thread_manager.c +++ b/core/iwasm/libraries/thread-mgr/thread_manager.c @@ -1198,3 +1198,9 @@ wasm_cluster_spread_custom_data(WASMModuleInstanceCommon *module_inst, os_mutex_unlock(&cluster->lock); } } + +bool +wasm_cluster_is_thread_terminated(WASMExecEnv *exec_env) +{ + return (exec_env->suspend_flags.flags & 0x01) ? true : false; +} \ No newline at end of file diff --git a/core/iwasm/libraries/thread-mgr/thread_manager.h b/core/iwasm/libraries/thread-mgr/thread_manager.h index f7b0eb52eb..38ca167fb3 100644 --- a/core/iwasm/libraries/thread-mgr/thread_manager.h +++ b/core/iwasm/libraries/thread-mgr/thread_manager.h @@ -146,6 +146,9 @@ void wasm_cluster_spread_custom_data(WASMModuleInstanceCommon *module_inst, void *custom_data); +bool +wasm_cluster_is_thread_terminated(WASMExecEnv *exec_env); + #if WASM_ENABLE_DEBUG_INTERP != 0 #define WAMR_SIG_TRAP (5) #define WAMR_SIG_STOP (19) From d104f4eb09559938dc8bc9b4f669cf165e3a9058 Mon Sep 17 00:00:00 2001 From: eloparco Date: Wed, 22 Feb 2023 10:07:01 +0000 Subject: [PATCH 3/8] refactor: re-organize thread manager inclusion in libc wasi wrapper --- .../libraries/libc-wasi/libc_wasi_wrapper.c | 51 +++++++++---------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c index e541ff1086..d26a9070da 100644 --- a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c +++ b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c @@ -10,8 +10,32 @@ #if WASM_ENABLE_THREAD_MGR != 0 #include "../../../thread-mgr/thread_manager.h" typedef struct WASIContext *wasi_ctx_t; +#else +typedef struct WASIContext { + struct fd_table *curfds; + struct fd_prestats *prestats; + struct argv_environ_values *argv_environ; + struct addr_pool *addr_pool; + char *ns_lookup_buf; + char **ns_lookup_list; + char *argv_buf; + char **argv_list; + char *env_buf; + char **env_list; + uint32_t exit_code; +} * wasi_ctx_t; #endif +typedef struct wasi_prestat_app { + wasi_preopentype_t pr_type; + uint32 pr_name_len; +} wasi_prestat_app_t; + +typedef struct iovec_app { + uint32 buf_offset; + uint32 buf_len; +} iovec_app_t; + void wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception); @@ -41,33 +65,6 @@ wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception); wasm_runtime_module_free(module_inst, offset) /* clang-format on */ -typedef struct wasi_prestat_app { - wasi_preopentype_t pr_type; - uint32 pr_name_len; -} wasi_prestat_app_t; - -typedef struct iovec_app { - uint32 buf_offset; - uint32 buf_len; -} iovec_app_t; - -/* If the thread manager is enabled, WASIContext is already defined */ -#if WASM_ENABLE_THREAD_MGR == 0 -typedef struct WASIContext { - struct fd_table *curfds; - struct fd_prestats *prestats; - struct argv_environ_values *argv_environ; - struct addr_pool *addr_pool; - char *ns_lookup_buf; - char **ns_lookup_list; - char *argv_buf; - char **argv_list; - char *env_buf; - char **env_list; - uint32_t exit_code; -} * wasi_ctx_t; -#endif - wasi_ctx_t wasm_runtime_get_wasi_ctx(wasm_module_inst_t module_inst); From a60167c7f67f1b37adf6821ea54977843f329a69 Mon Sep 17 00:00:00 2001 From: eloparco Date: Wed, 22 Feb 2023 11:10:29 +0000 Subject: [PATCH 4/8] fix: fix condition check after "wasmtime_ssp_poll_oneoff" call --- core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c | 6 ++++++ .../libc-wasi/sandboxed-system-primitives/src/posix.c | 2 ++ 2 files changed, 8 insertions(+) diff --git a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c index d26a9070da..0226591a98 100644 --- a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c +++ b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c @@ -1042,6 +1042,12 @@ execute_interruptible_poll_oneoff(wasm_module_inst_t module_inst, wasm_runtime_free(in_copy); return EINTR; } + else if (*nevents > 0 + && !(nsubscriptions == 1 + && in[0].u.type == __WASI_EVENTTYPE_CLOCK)) { + wasm_runtime_free(in_copy); + return __WASI_ESUCCESS; + } } wasm_runtime_free(in_copy); 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 187c756c04..9e29a3bace 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 @@ -2613,6 +2613,8 @@ wasmtime_ssp_poll_oneoff( } #endif *nevents = 1; + if (out[0].error != 0) + return convert_errno(out[0].error); return 0; } From f841c2610611852f913f5b9d547387204ce30cbf Mon Sep 17 00:00:00 2001 From: eloparco Date: Wed, 22 Feb 2023 14:51:14 +0000 Subject: [PATCH 5/8] refactor: remove unnecessary parameter in "execute_interruptible_poll_oneoff" --- core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c index 0226591a98..35a5953480 100644 --- a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c +++ b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c @@ -994,13 +994,12 @@ update_clock_subscription_data(wasi_subscription_t *in, uint32 nsubscriptions, } static wasi_errno_t -execute_interruptible_poll_oneoff(wasm_module_inst_t module_inst, +execute_interruptible_poll_oneoff( #if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, + struct fd_table *curfds, #endif - const __wasi_subscription_t *in, - __wasi_event_t *out, size_t nsubscriptions, - size_t *nevents, wasm_exec_env_t exec_env) + const __wasi_subscription_t *in, __wasi_event_t *out, size_t nsubscriptions, + size_t *nevents, wasm_exec_env_t exec_env) { if (nsubscriptions == 0) { *nevents = 0; @@ -1076,8 +1075,8 @@ wasi_poll_oneoff(wasm_exec_env_t exec_env, const wasi_subscription_t *in, #if WASM_ENABLE_THREAD_MGR == 0 err = wasmtime_ssp_poll_oneoff(curfds, in, out, nsubscriptions, &nevents); #else - err = execute_interruptible_poll_oneoff(module_inst, curfds, in, out, - nsubscriptions, &nevents, exec_env); + err = execute_interruptible_poll_oneoff(curfds, in, out, nsubscriptions, + &nevents, exec_env); #endif if (err) return err; From 34ede54373cc374a6dd5ed1ec86b6a5919a38ff9 Mon Sep 17 00:00:00 2001 From: eloparco Date: Wed, 22 Feb 2023 15:19:37 +0000 Subject: [PATCH 6/8] fix: add check on returned events --- .../libraries/libc-wasi/libc_wasi_wrapper.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c index 35a5953480..76e21db5ad 100644 --- a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c +++ b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c @@ -1008,6 +1008,8 @@ execute_interruptible_poll_oneoff( wasi_errno_t err; __wasi_timestamp_t elapsed = 0; + bool all_outs_are_type_clock; + uint32 i; const __wasi_timestamp_t timeout = get_timeout_for_poll_oneoff( in, nsubscriptions), @@ -1041,11 +1043,17 @@ execute_interruptible_poll_oneoff( wasm_runtime_free(in_copy); return EINTR; } - else if (*nevents > 0 - && !(nsubscriptions == 1 - && in[0].u.type == __WASI_EVENTTYPE_CLOCK)) { - wasm_runtime_free(in_copy); - return __WASI_ESUCCESS; + else if (*nevents > 0) { + all_outs_are_type_clock = true; + for (i = 0; i < nsubscriptions; i++) { + if (out[i].type != __WASI_EVENTTYPE_CLOCK) + all_outs_are_type_clock = false; + } + + if (!all_outs_are_type_clock) { + wasm_runtime_free(in_copy); + return __WASI_ESUCCESS; + } } } From e3823f47b581a26a66d0ab043bb1f59976d3bea8 Mon Sep 17 00:00:00 2001 From: eloparco Date: Wed, 22 Feb 2023 16:17:02 +0000 Subject: [PATCH 7/8] refactor: remove duplicated code --- .../libraries/libc-wasi/libc_wasi_wrapper.c | 38 +++++++------------ 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c index 76e21db5ad..2045fa4139 100644 --- a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c +++ b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c @@ -6,36 +6,12 @@ #include "libc_wasi_wrapper.h" #include "bh_platform.h" #include "wasm_export.h" +#include "wasm_runtime_common.h" #if WASM_ENABLE_THREAD_MGR != 0 #include "../../../thread-mgr/thread_manager.h" -typedef struct WASIContext *wasi_ctx_t; -#else -typedef struct WASIContext { - struct fd_table *curfds; - struct fd_prestats *prestats; - struct argv_environ_values *argv_environ; - struct addr_pool *addr_pool; - char *ns_lookup_buf; - char **ns_lookup_list; - char *argv_buf; - char **argv_list; - char *env_buf; - char **env_list; - uint32_t exit_code; -} * wasi_ctx_t; #endif -typedef struct wasi_prestat_app { - wasi_preopentype_t pr_type; - uint32 pr_name_len; -} wasi_prestat_app_t; - -typedef struct iovec_app { - uint32 buf_offset; - uint32 buf_len; -} iovec_app_t; - void wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception); @@ -65,6 +41,18 @@ wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception); wasm_runtime_module_free(module_inst, offset) /* clang-format on */ +typedef struct wasi_prestat_app { + wasi_preopentype_t pr_type; + uint32 pr_name_len; +} wasi_prestat_app_t; + +typedef struct iovec_app { + uint32 buf_offset; + uint32 buf_len; +} iovec_app_t; + +typedef struct WASIContext *wasi_ctx_t; + wasi_ctx_t wasm_runtime_get_wasi_ctx(wasm_module_inst_t module_inst); From 51792972aa430ef45f9a2872178a9915ddda2c70 Mon Sep 17 00:00:00 2001 From: eloparco Date: Wed, 22 Feb 2023 23:48:05 +0000 Subject: [PATCH 8/8] fix(poll_oneoff): iterate over nevents instead of subscriptions --- core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c index 2045fa4139..ab2808c6e9 100644 --- a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c +++ b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c @@ -1033,9 +1033,11 @@ execute_interruptible_poll_oneoff( } else if (*nevents > 0) { all_outs_are_type_clock = true; - for (i = 0; i < nsubscriptions; i++) { - if (out[i].type != __WASI_EVENTTYPE_CLOCK) + for (i = 0; i < *nevents; i++) { + if (out[i].type != __WASI_EVENTTYPE_CLOCK) { all_outs_are_type_clock = false; + break; + } } if (!all_outs_are_type_clock) {