Explorar el Código

Spread module custom data to all threads, enable libc-builtin float print (#633)

Xu Jun hace 4 años
padre
commit
f637438e4e

+ 2 - 0
build-scripts/config_common.cmake

@@ -182,6 +182,8 @@ endif ()
 if (WAMR_DISABLE_HW_BOUND_CHECK EQUAL 1)
   add_definitions (-DWASM_DISABLE_HW_BOUND_CHECK=1)
   message ("     Hardware boundary check disabled")
+else ()
+  add_definitions (-DWASM_DISABLE_HW_BOUND_CHECK=0)
 endif ()
 if (WAMR_BUILD_SIMD EQUAL 1)
   add_definitions (-DWASM_ENABLE_SIMD=1)

+ 13 - 2
core/iwasm/common/wasm_runtime_common.c

@@ -1438,8 +1438,8 @@ wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst)
 }
 
 void
-wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
-                             void *custom_data)
+wasm_runtime_set_custom_data_internal(WASMModuleInstanceCommon *module_inst,
+                                      void *custom_data)
 {
 #if WASM_ENABLE_INTERP != 0
     if (module_inst->module_type == Wasm_Module_Bytecode) {
@@ -1455,6 +1455,17 @@ wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
 #endif
 }
 
+void
+wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
+                             void *custom_data)
+{
+#if WASM_ENABLE_THREAD_MGR != 0
+    wasm_cluster_spread_custom_data(module_inst, custom_data);
+#else
+    wasm_runtime_set_custom_data_internal(module_inst, custom_data);
+#endif
+}
+
 void*
 wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst)
 {

+ 5 - 0
core/iwasm/common/wasm_runtime_common.h

@@ -490,6 +490,11 @@ wasm_runtime_get_exception(WASMModuleInstanceCommon *module);
 WASM_RUNTIME_API_EXTERN void
 wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst);
 
+/* Internal API */
+void
+wasm_runtime_set_custom_data_internal(WASMModuleInstanceCommon *module_inst,
+                                      void *custom_data);
+
 /* See wasm_export.h for description */
 WASM_RUNTIME_API_EXTERN void
 wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,

+ 3 - 0
core/iwasm/include/wasm_export.h

@@ -554,6 +554,9 @@ wasm_runtime_clear_exception(wasm_module_inst_t module_inst);
 
 /**
  * Set custom data to WASM module instance.
+ * Note:
+ *  If WAMR_BUILD_LIB_PTHREAD is enabled, this API
+ *  will spread the custom data to all threads
  *
  * @param module_inst the WASM module instance
  * @param custom_data the custom data to be set

+ 1 - 1
core/iwasm/interpreter/wasm_loader.c

@@ -6152,7 +6152,7 @@ wasm_loader_prepare_bytecode(WASMModule *module,
     BranchBlock *frame_csp_tmp;
 #if WASM_ENABLE_FAST_INTERP != 0
     uint8 *func_const_end, *func_const = NULL;
-    int16 operand_offset;
+    int16 operand_offset = 0;
     uint8 last_op = 0;
     bool disable_emit, preserve_local = false;
     float32 f32;

+ 1 - 1
core/iwasm/interpreter/wasm_mini_loader.c

@@ -4758,7 +4758,7 @@ wasm_loader_prepare_bytecode(WASMModule *module,
 #endif
 #if WASM_ENABLE_FAST_INTERP != 0
     uint8 *func_const_end, *func_const = NULL;
-    int16 operand_offset;
+    int16 operand_offset = 0;
     uint8 last_op = 0;
     bool disable_emit, preserve_local = false;
     float32 f32;

+ 8 - 1
core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c

@@ -535,13 +535,13 @@ pthread_create_wrapper(wasm_exec_env_t exec_env,
                        void *arg)           /* arguments buffer */
 {
     wasm_module_t module = get_module(exec_env);
+    wasm_module_inst_t module_inst = get_module_inst(exec_env);
     wasm_module_inst_t new_module_inst = NULL;
     ThreadInfoNode *info_node = NULL;
     ThreadRoutineArgs *routine_args = NULL;
     uint32 thread_handle;
     int32 ret = -1;
 #if WASM_ENABLE_LIBC_WASI != 0
-    wasm_module_inst_t module_inst = get_module_inst(exec_env);
     WASIContext *wasi_ctx = get_wasi_ctx(module_inst);
 #endif
 
@@ -552,6 +552,13 @@ pthread_create_wrapper(wasm_exec_env_t exec_env,
                                               NULL, 0)))
         return -1;
 
+    if (module_inst) {
+        /* Set custom_data to new module instance */
+        wasm_runtime_set_custom_data_internal(
+            new_module_inst,
+            wasm_runtime_get_custom_data(module_inst));
+    }
+
 #if WASM_ENABLE_LIBC_WASI != 0
     if (wasi_ctx)
         wasm_runtime_set_wasi_ctx(new_module_inst, wasi_ctx);

+ 13 - 0
core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c

@@ -361,6 +361,19 @@ handle_1_to_9:
                 break;
             }
 
+            case 'f': {
+                float64 f64;
+                char buf[16], *s;
+
+                CHECK_VA_ARG(ap, float64);
+                f64 = _va_arg(ap, float64);
+                snprintf(buf, sizeof(buf), "%f", f64);
+                s = buf;
+                while (*s)
+                    out((int) (*s++), ctx);
+                break;
+            }
+
             default:
                 out((int) '%', ctx);
                 out((int) *fmt, ctx);

+ 29 - 0
core/iwasm/libraries/thread-mgr/thread_manager.c

@@ -328,6 +328,7 @@ WASMExecEnv *
 wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
 {
     WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
+    wasm_module_inst_t module_inst = get_module_inst(exec_env);
     wasm_module_t module = wasm_exec_env_get_module(exec_env);
     wasm_module_inst_t new_module_inst;
     WASMExecEnv *new_exec_env;
@@ -343,6 +344,13 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
         return NULL;
     }
 
+    if (module_inst) {
+        /* Set custom_data to new module instance */
+        wasm_runtime_set_custom_data_internal(
+            new_module_inst,
+            wasm_runtime_get_custom_data(module_inst));
+    }
+
     new_exec_env = wasm_exec_env_create_internal(
                         new_module_inst, exec_env->wasm_stack_size);
     if (!new_exec_env)
@@ -654,3 +662,24 @@ wasm_cluster_spread_exception(WASMExecEnv *exec_env)
 
     traverse_list(&cluster->exec_env_list, set_exception_visitor, exec_env);
 }
+
+static void
+set_custom_data_visitor(void *node, void *user_data)
+{
+    WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
+    WASMModuleInstanceCommon *module_inst = get_module_inst(curr_exec_env);
+
+    wasm_runtime_set_custom_data_internal(module_inst, user_data);
+}
+
+void
+wasm_cluster_spread_custom_data(WASMModuleInstanceCommon *module_inst,
+                                void *custom_data)
+{
+    WASMExecEnv *exec_env = wasm_clusters_search_exec_env(module_inst);
+    WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
+
+    traverse_list(&cluster->exec_env_list,
+                  set_custom_data_visitor,
+                  custom_data);
+}

+ 4 - 0
core/iwasm/libraries/thread-mgr/thread_manager.h

@@ -118,6 +118,10 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env);
 void
 wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env);
 
+void
+wasm_cluster_spread_custom_data(WASMModuleInstanceCommon *module_inst,
+                                void *custom_data);
+
 #ifdef __cplusplus
 }
 #endif

+ 1 - 1
core/shared/platform/include/platform_common.h

@@ -10,8 +10,8 @@
 extern "C" {
 #endif
 
-#include "../../../config.h"
 #include "platform_internal.h"
+#include "../../../config.h"
 
 #define BH_MAX_THREAD 32
 

+ 8 - 5
core/shared/platform/zephyr/zephyr_thread.c

@@ -231,7 +231,7 @@ int os_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg,
                      unsigned int stack_size)
 {
     return os_thread_create_with_prio(p_tid, start, arg, stack_size,
-    BH_THREAD_DEFAULT_PRIORITY);
+                                      BH_THREAD_DEFAULT_PRIORITY);
 }
 
 int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
@@ -253,6 +253,9 @@ int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
 
     memset(tid, 0, sizeof(os_thread_obj));
 
+    if (stack_size < APP_THREAD_STACK_SIZE_MIN)
+        stack_size = APP_THREAD_STACK_SIZE_MIN;
+
     /* Create and initialize thread data */
     thread_data_size = offsetof(os_thread_data, stack) + stack_size;
     if (!(thread_data = BH_MALLOC(thread_data_size))) {
@@ -266,9 +269,9 @@ int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
     thread_data->tid = tid;
 
     /* Create the thread */
-    if (!((tid = k_thread_create(tid, (k_thread_stack_t *) thread_data->stack,
-            stack_size, os_thread_wrapper, start, arg, thread_data, prio, 0,
-            K_NO_WAIT)))) {
+    if (!((tid = k_thread_create(tid, (k_thread_stack_t *)thread_data->stack,
+                                 stack_size, os_thread_wrapper, start, arg,
+                                 thread_data, prio, 0, K_NO_WAIT)))) {
         BH_FREE(tid);
         BH_FREE(thread_data);
         return BHT_ERROR;
@@ -285,7 +288,7 @@ int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
 
 korp_tid os_self_thread()
 {
-    return (korp_tid) k_current_get();
+    return (korp_tid)k_current_get();
 }
 
 int os_thread_join(korp_tid thread, void **value_ptr)