فهرست منبع

Implement wasm_runtime_init_thread_env for Windows platform (#683)

Implement wasm_runtime_init_thread_env() for Windows platform by calling os_thread_env_init(): if current thread is created by developer himself but not runtime, developer should call wasm_runtime_init_thread_env() to init the thread environment before calling wasm function, and call wasm_runtime_destroy_thread_env() before thread exits.
And clear compile warnings for Windows platform, fix compile error for AliOS-Things platform

Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
Wenyong Huang 4 سال پیش
والد
کامیت
541f577164

+ 1 - 1
core/iwasm/aot/aot_runtime.c

@@ -1324,7 +1324,7 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr,
     uint16 param_count = func_type->param_count;
     uint16 param_count = func_type->param_count;
     uint16 result_count = func_type->result_count;
     uint16 result_count = func_type->result_count;
     const uint8 *types = func_type->types;
     const uint8 *types = func_type->types;
-#if BH_PLATFORM_WINDOWS
+#ifdef BH_PLATFORM_WINDOWS
     const char *exce;
     const char *exce;
     int result;
     int result;
 #endif
 #endif

+ 15 - 1
core/iwasm/common/wasm_runtime_common.c

@@ -823,9 +823,19 @@ wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env)
 bool
 bool
 wasm_runtime_init_thread_env()
 wasm_runtime_init_thread_env()
 {
 {
+#ifdef BH_PLATFORM_WINDOWS
+    if (os_thread_env_init() != 0)
+        return false;
+#endif
+
 #if WASM_ENABLE_AOT != 0
 #if WASM_ENABLE_AOT != 0
 #ifdef OS_ENABLE_HW_BOUND_CHECK
 #ifdef OS_ENABLE_HW_BOUND_CHECK
-    return aot_signal_init();
+    if (!aot_signal_init()) {
+#ifdef BH_PLATFORM_WINDOWS
+        os_thread_env_destroy();
+#endif
+        return false;
+    }
 #endif
 #endif
 #endif
 #endif
     return true;
     return true;
@@ -839,6 +849,10 @@ wasm_runtime_destroy_thread_env()
     aot_signal_destroy();
     aot_signal_destroy();
 #endif
 #endif
 #endif
 #endif
+
+#ifdef BH_PLATFORM_WINDOWS
+    os_thread_env_destroy();
+#endif
 }
 }
 
 
 #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
 #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)

+ 1 - 1
core/iwasm/compilation/aot_compiler.c

@@ -66,7 +66,7 @@ read_leb(const uint8 *buf, const uint8 *buf_end,
   }
   }
   if (sign && (shift < maxbits) && (byte & 0x40)) {
   if (sign && (shift < maxbits) && (byte & 0x40)) {
     /* Sign extend */
     /* Sign extend */
-    result |= (uint64)(- (((uint64)1) << shift));
+    result |= (~((uint64)0)) << shift;
   }
   }
   *p_result = result;
   *p_result = result;
   return true;
   return true;

+ 9 - 9
core/iwasm/interpreter/wasm_interp_classic.c

@@ -50,7 +50,7 @@ rotl32(uint32 n, uint32 c)
     const uint32 mask = (31);
     const uint32 mask = (31);
     c = c % 32;
     c = c % 32;
     c &= mask;
     c &= mask;
-    return (n<<c) | (n>>( (-c)&mask ));
+    return (n << c) | (n >> ((0 - c) & mask));
 }
 }
 
 
 static inline uint32
 static inline uint32
@@ -59,7 +59,7 @@ rotr32(uint32 n, uint32 c)
     const uint32 mask = (31);
     const uint32 mask = (31);
     c = c % 32;
     c = c % 32;
     c &= mask;
     c &= mask;
-    return (n>>c) | (n<<( (-c)&mask ));
+    return (n >> c) | (n << ((0 - c) & mask));
 }
 }
 
 
 static inline uint64
 static inline uint64
@@ -68,7 +68,7 @@ rotl64(uint64 n, uint64 c)
     const uint64 mask = (63);
     const uint64 mask = (63);
     c = c % 64;
     c = c % 64;
     c &= mask;
     c &= mask;
-    return (n<<c) | (n>>( (-c)&mask ));
+    return (n << c) | (n >> ((0 - c) & mask));
 }
 }
 
 
 static inline uint64
 static inline uint64
@@ -77,7 +77,7 @@ rotr64(uint64 n, uint64 c)
     const uint64 mask = (63);
     const uint64 mask = (63);
     c = c % 64;
     c = c % 64;
     c &= mask;
     c &= mask;
-    return (n>>c) | (n<<( (-c)&mask ));
+    return (n >> c) | (n << ((0 - c) & mask));
 }
 }
 
 
 static inline double
 static inline double
@@ -2623,7 +2623,7 @@ label_pop_csp_n:
             goto out_of_bounds;
             goto out_of_bounds;
 
 
           bh_memcpy_s(maddr, linear_mem_size - addr,
           bh_memcpy_s(maddr, linear_mem_size - addr,
-                      data + offset, bytes);
+                      data + offset, (uint32)bytes);
           break;
           break;
         }
         }
         case WASM_OP_DATA_DROP:
         case WASM_OP_DATA_DROP:
@@ -2717,9 +2717,9 @@ label_pop_csp_n:
           bh_memcpy_s(
           bh_memcpy_s(
             (uint8 *)(tbl_inst)
             (uint8 *)(tbl_inst)
               + offsetof(WASMTableInstance, base_addr) + d * sizeof(uint32),
               + offsetof(WASMTableInstance, base_addr) + d * sizeof(uint32),
-            (tbl_inst->cur_size - d) * sizeof(uint32),
+            (uint32)((tbl_inst->cur_size - d) * sizeof(uint32)),
             module->module->table_segments[elem_idx].func_indexes + s,
             module->module->table_segments[elem_idx].func_indexes + s,
-            n * sizeof(uint32));
+            (uint32)(n * sizeof(uint32)));
 
 
           break;
           break;
         }
         }
@@ -2764,10 +2764,10 @@ label_pop_csp_n:
           bh_memmove_s(
           bh_memmove_s(
             (uint8 *)(dst_tbl_inst) + offsetof(WASMTableInstance, base_addr)
             (uint8 *)(dst_tbl_inst) + offsetof(WASMTableInstance, base_addr)
               + d * sizeof(uint32),
               + d * sizeof(uint32),
-            (dst_tbl_inst->cur_size - d) * sizeof(uint32),
+            (uint32)((dst_tbl_inst->cur_size - d) * sizeof(uint32)),
             (uint8 *)(src_tbl_inst) + offsetof(WASMTableInstance, base_addr)
             (uint8 *)(src_tbl_inst) + offsetof(WASMTableInstance, base_addr)
               + s * sizeof(uint32),
               + s * sizeof(uint32),
-            n * sizeof(uint32));
+            (uint32)(n * sizeof(uint32)));
           break;
           break;
         }
         }
         case WASM_OP_TABLE_GROW:
         case WASM_OP_TABLE_GROW:

+ 16 - 13
core/iwasm/interpreter/wasm_interp_fast.c

@@ -48,7 +48,7 @@ rotl32(uint32 n, uint32 c)
     const uint32 mask = (31);
     const uint32 mask = (31);
     c = c % 32;
     c = c % 32;
     c &= mask;
     c &= mask;
-    return (n<<c) | (n>>( (-c)&mask ));
+    return (n << c) | (n >> ((0 - c) & mask));
 }
 }
 
 
 static inline uint32
 static inline uint32
@@ -57,7 +57,7 @@ rotr32(uint32 n, uint32 c)
     const uint32 mask = (31);
     const uint32 mask = (31);
     c = c % 32;
     c = c % 32;
     c &= mask;
     c &= mask;
-    return (n>>c) | (n<<( (-c)&mask ));
+    return (n >> c) | (n << ((0 - c) & mask));
 }
 }
 
 
 static inline uint64
 static inline uint64
@@ -66,7 +66,7 @@ rotl64(uint64 n, uint64 c)
     const uint64 mask = (63);
     const uint64 mask = (63);
     c = c % 64;
     c = c % 64;
     c &= mask;
     c &= mask;
-    return (n<<c) | (n>>( (-c)&mask ));
+    return (n << c) | (n >> ((0 - c) & mask));
 }
 }
 
 
 static inline uint64
 static inline uint64
@@ -75,7 +75,7 @@ rotr64(uint64 n, uint64 c)
     const uint64 mask = (63);
     const uint64 mask = (63);
     c = c % 64;
     c = c % 64;
     c &= mask;
     c &= mask;
-    return (n>>c) | (n<<( (-c)&mask ));
+    return (n >> c) | (n << ((0 - c) & mask));
 }
 }
 
 
 static inline double
 static inline double
@@ -455,7 +455,8 @@ LOAD_PTR(void *addr)
 
 
 #define DEF_OP_MATH(src_type, src_op_type, method) do {             \
 #define DEF_OP_MATH(src_type, src_op_type, method) do {             \
     SET_OPERAND(src_op_type, 2,                                     \
     SET_OPERAND(src_op_type, 2,                                     \
-                method(GET_OPERAND(src_type, src_op_type, 0)));     \
+                (src_type)method(GET_OPERAND(src_type,              \
+                                             src_op_type, 0)));     \
     frame_ip += 4;                                                  \
     frame_ip += 4;                                                  \
   } while (0)
   } while (0)
 
 
@@ -2229,7 +2230,7 @@ recover_br_info:
         else if (isnan(b))
         else if (isnan(b))
             *(float32*)(frame_lp + GET_OFFSET()) = b;
             *(float32*)(frame_lp + GET_OFFSET()) = b;
         else
         else
-            *(float32*)(frame_lp + GET_OFFSET()) = wa_fmin(a, b);
+            *(float32*)(frame_lp + GET_OFFSET()) = (float32)wa_fmin(a, b);
         HANDLE_OP_END ();
         HANDLE_OP_END ();
       }
       }
 
 
@@ -2245,7 +2246,7 @@ recover_br_info:
         else if (isnan(b))
         else if (isnan(b))
             *(float32*)(frame_lp + GET_OFFSET()) = b;
             *(float32*)(frame_lp + GET_OFFSET()) = b;
         else
         else
-            *(float32*)(frame_lp + GET_OFFSET()) = wa_fmax(a, b);
+            *(float32*)(frame_lp + GET_OFFSET()) = (float32)wa_fmax(a, b);
         HANDLE_OP_END ();
         HANDLE_OP_END ();
       }
       }
 
 
@@ -2255,7 +2256,8 @@ recover_br_info:
 
 
         b = *(float32*)(frame_lp + GET_OFFSET());
         b = *(float32*)(frame_lp + GET_OFFSET());
         a = *(float32*)(frame_lp + GET_OFFSET());
         a = *(float32*)(frame_lp + GET_OFFSET());
-        *(float32*)(frame_lp + GET_OFFSET()) = (signbit(b) ? -fabs(a) : fabs(a));
+        *(float32*)(frame_lp + GET_OFFSET()) =
+            (float32)(signbit(b) ? -fabs(a) : fabs(a));
         HANDLE_OP_END ();
         HANDLE_OP_END ();
       }
       }
 
 
@@ -2606,7 +2608,8 @@ recover_br_info:
           if (offset + bytes > seg_len)
           if (offset + bytes > seg_len)
             goto out_of_bounds;
             goto out_of_bounds;
 
 
-          bh_memcpy_s(maddr, linear_mem_size - addr, data + offset, bytes);
+          bh_memcpy_s(maddr, linear_mem_size - addr,
+                      data + offset, (uint32)bytes);
           break;
           break;
         }
         }
         case WASM_OP_DATA_DROP:
         case WASM_OP_DATA_DROP:
@@ -2695,9 +2698,9 @@ recover_br_info:
             bh_memcpy_s(
             bh_memcpy_s(
               (uint8 *)tbl_inst + offsetof(WASMTableInstance, base_addr)
               (uint8 *)tbl_inst + offsetof(WASMTableInstance, base_addr)
                 + d * sizeof(uint32),
                 + d * sizeof(uint32),
-              (tbl_inst->cur_size - d) * sizeof(uint32),
+              (uint32)((tbl_inst->cur_size - d) * sizeof(uint32)),
               module->module->table_segments[elem_idx].func_indexes + s,
               module->module->table_segments[elem_idx].func_indexes + s,
-              n * sizeof(uint32));
+              (uint32)(n * sizeof(uint32)));
             break;
             break;
         }
         }
         case WASM_OP_ELEM_DROP:
         case WASM_OP_ELEM_DROP:
@@ -2740,10 +2743,10 @@ recover_br_info:
             bh_memmove_s(
             bh_memmove_s(
               (uint8 *)dst_tbl_inst + offsetof(WASMTableInstance, base_addr)
               (uint8 *)dst_tbl_inst + offsetof(WASMTableInstance, base_addr)
                 + d * sizeof(uint32),
                 + d * sizeof(uint32),
-              (dst_tbl_inst->cur_size - d) * sizeof(uint32),
+              (uint32)((dst_tbl_inst->cur_size - d) * sizeof(uint32)),
               (uint8 *)src_tbl_inst
               (uint8 *)src_tbl_inst
                 + offsetof(WASMTableInstance, base_addr) + s * sizeof(uint32),
                 + offsetof(WASMTableInstance, base_addr) + s * sizeof(uint32),
-              n * sizeof(uint32));
+              (uint32)(n * sizeof(uint32)));
             break;
             break;
         }
         }
         case WASM_OP_TABLE_GROW:
         case WASM_OP_TABLE_GROW:

+ 2 - 2
core/iwasm/interpreter/wasm_runtime.c

@@ -2485,14 +2485,14 @@ wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env)
         /* place holder, will overwrite it in wasm_c_api */
         /* place holder, will overwrite it in wasm_c_api */
         frame.instance = module_inst;
         frame.instance = module_inst;
         frame.module_offset = 0;
         frame.module_offset = 0;
-        frame.func_index = func_inst - module_inst->functions;
+        frame.func_index = (uint32)(func_inst - module_inst->functions);
 
 
         func_code_base = wasm_get_func_code(func_inst);
         func_code_base = wasm_get_func_code(func_inst);
         if (!cur_frame->ip || !func_code_base) {
         if (!cur_frame->ip || !func_code_base) {
             frame.func_offset = 0;
             frame.func_offset = 0;
         }
         }
         else {
         else {
-            frame.func_offset = cur_frame->ip - func_code_base;
+            frame.func_offset = (uint32)(cur_frame->ip - func_code_base);
         }
         }
 
 
         /* look for the function name */
         /* look for the function name */

+ 14 - 1
core/shared/platform/include/platform_api_extension.h

@@ -84,11 +84,24 @@ int os_thread_detach(korp_tid);
  */
  */
 void os_thread_exit(void *retval);
 void os_thread_exit(void *retval);
 
 
+/**
+ * Initialize current thread environment if current thread
+ * is created by developer but not runtime
+ *
+ * @return 0 if success, -1 otherwise
+ */
+int os_thread_env_init();
+
+/**
+ * Destroy current thread environment
+ */
+void os_thread_env_destroy();
+
 /**
 /**
  * Suspend execution of the calling thread for (at least)
  * Suspend execution of the calling thread for (at least)
  * usec microseconds
  * usec microseconds
  *
  *
- * @param return 0 if success, -1 otherwise
+ * @return 0 if success, -1 otherwise
  */
  */
 int os_usleep(uint32 usec);
 int os_usleep(uint32 usec);
 
 

+ 56 - 0
core/shared/platform/windows/win_thread.c

@@ -288,6 +288,62 @@ os_thread_exit(void *retval)
     _endthreadex(0);
     _endthreadex(0);
 }
 }
 
 
+int
+os_thread_env_init()
+{
+    os_thread_data *thread_data = TlsGetValue(thread_data_key);
+
+    if (thread_data)
+        /* Already created */
+        return BHT_OK;
+
+    if (!(thread_data = BH_MALLOC(sizeof(os_thread_data))))
+        return BHT_ERROR;
+
+    memset(thread_data, 0, sizeof(os_thread_data));
+    thread_data->thread_id = GetCurrentThreadId();
+
+    if (os_sem_init(&thread_data->wait_node.sem) != BHT_OK)
+        goto fail1;
+
+    if (os_mutex_init(&thread_data->wait_lock) != BHT_OK)
+        goto fail2;
+
+    if (os_cond_init(&thread_data->wait_cond) != BHT_OK)
+        goto fail3;
+
+    if (!TlsSetValue(thread_data_key, thread_data))
+        goto fail4;
+
+    return BHT_OK;
+
+fail4:
+    os_cond_destroy(&thread_data->wait_cond);
+fail3:
+    os_mutex_destroy(&thread_data->wait_lock);
+fail2:
+    os_sem_destroy(&thread_data->wait_node.sem);
+fail1:
+    BH_FREE(thread_data);
+    return BHT_ERROR;
+}
+
+void
+os_thread_env_destroy()
+{
+    os_thread_data *thread_data = TlsGetValue(thread_data_key);
+
+    /* Note that supervisor_thread_data's resources will be destroyed
+       by os_thread_sys_destroy() */
+    if (thread_data && thread_data != &supervisor_thread_data) {
+        TlsSetValue(thread_data_key, NULL);
+        os_cond_destroy(&thread_data->wait_cond);
+        os_mutex_destroy(&thread_data->wait_lock);
+        os_sem_destroy(&thread_data->wait_node.sem);
+        BH_FREE(thread_data);
+    }
+}
+
 int
 int
 os_sem_init(korp_sem *sem)
 os_sem_init(korp_sem *sem)
 {
 {

+ 1 - 0
product-mini/platforms/alios-things/aos.mk

@@ -99,6 +99,7 @@ $(NAME)_SOURCES := ${SHARED_ROOT}/platform/alios/alios_platform.c \
                    ${IWASM_ROOT}/common/wasm_native.c \
                    ${IWASM_ROOT}/common/wasm_native.c \
                    ${IWASM_ROOT}/common/wasm_exec_env.c \
                    ${IWASM_ROOT}/common/wasm_exec_env.c \
                    ${IWASM_ROOT}/common/wasm_memory.c \
                    ${IWASM_ROOT}/common/wasm_memory.c \
+                   ${IWASM_ROOT}/common/wasm_c_api.c \
                    ${IWASM_ROOT}/common/arch/${INVOKE_NATIVE} \
                    ${IWASM_ROOT}/common/arch/${INVOKE_NATIVE} \
                    src/main.c
                    src/main.c