Parcourir la source

wasi: Apply wasm_runtime_begin_blocking_op to poll as well (#3080)

While we used a different approach for poll_oneoff [1],
the implementation works only when the poll list includes
an absolute clock event. That is, if we have a thread which is
polling on descriptors without a timeout, we fail to terminate
the thread.

This commit fixes it by applying wasm_runtime_begin_blocking_op
to poll as well.

[1] https://github.com/bytecodealliance/wasm-micro-runtime/pull/1951
YAMAMOTO Takashi il y a 2 ans
Parent
commit
9afbeab1c8

+ 21 - 0
core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.c

@@ -7,6 +7,7 @@
 
 #include "ssp_config.h"
 #include "blocking_op.h"
+#include "libc_errno.h"
 
 __wasi_errno_t
 blocking_op_close(wasm_exec_env_t exec_env, os_file_handle handle,
@@ -170,3 +171,23 @@ blocking_op_openat(wasm_exec_env_t exec_env, os_file_handle handle,
     wasm_runtime_end_blocking_op(exec_env);
     return error;
 }
+
+#ifndef BH_PLATFORM_WINDOWS
+/* REVISIT: apply the os_file_handle style abstraction for pollfd? */
+__wasi_errno_t
+blocking_op_poll(wasm_exec_env_t exec_env, struct pollfd *pfds, nfds_t nfds,
+                 int timeout_ms, int *retp)
+{
+    int ret;
+    if (!wasm_runtime_begin_blocking_op(exec_env)) {
+        return __WASI_EINTR;
+    }
+    ret = poll(pfds, nfds, timeout_ms);
+    wasm_runtime_end_blocking_op(exec_env);
+    if (ret == -1) {
+        return convert_errno(errno);
+    }
+    *retp = ret;
+    return 0;
+}
+#endif

+ 7 - 1
core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.h

@@ -50,4 +50,10 @@ __wasi_errno_t
 blocking_op_openat(wasm_exec_env_t exec_env, os_file_handle handle,
                    const char *path, __wasi_oflags_t oflags,
                    __wasi_fdflags_t fd_flags, __wasi_lookupflags_t lookup_flags,
-                   wasi_libc_file_access_mode access_mode, os_file_handle *out);
+                   wasi_libc_file_access_mode access_mode, os_file_handle *out);
+
+#ifndef BH_PLATFORM_WINDOWS
+__wasi_errno_t
+blocking_op_poll(wasm_exec_env_t exec_env, struct pollfd *pfds, nfds_t nfds,
+                 int timeout, int *retp);
+#endif

+ 4 - 5
core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c

@@ -2230,11 +2230,10 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds,
         timeout = -1;
     }
 
-    int ret = poll(pfds, nsubscriptions, timeout);
-
-    __wasi_errno_t error = 0;
-    if (ret == -1) {
-        error = convert_errno(errno);
+    int ret;
+    int error = blocking_op_poll(exec_env, pfds, nsubscriptions, timeout, &ret);
+    if (error != 0) {
+        /* got an error */
     }
     else if (ret == 0 && *nevents == 0 && clock_subscription != NULL) {
         // No events triggered. Trigger the clock event.