Преглед изворни кода

Fix multi-module and some other issues (#1435)

Fix multi-module issue:
  don't call the sub module's function with "$sub_module_name$func_name"
Fix the aot_call_function free argv1 issue
Modify some API comments in wasm_export.h
Fix the wamrc help info
Wenyong Huang пре 3 година
родитељ
комит
22c235b5ec

+ 7 - 6
core/iwasm/aot/aot_runtime.c

@@ -1483,7 +1483,8 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
 
 #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
         if (!aot_alloc_frame(exec_env, function->func_index)) {
-            wasm_runtime_free(argv1);
+            if (argv1 != argv1_buf)
+                wasm_runtime_free(argv1);
             return false;
         }
 #endif
@@ -1492,9 +1493,6 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
                                      func_type, NULL, NULL, argv1, argc, argv);
 
         if (!ret || aot_get_exception(module_inst)) {
-            if (argv1 != argv1_buf)
-                wasm_runtime_free(argv1);
-
             if (clear_wasi_proc_exit_exception(module_inst))
                 ret = true;
             else
@@ -1512,8 +1510,11 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
 #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
         aot_free_frame(exec_env);
 #endif
-        if (!ret)
+        if (!ret) {
+            if (argv1 != argv1_buf)
+                wasm_runtime_free(argv1);
             return ret;
+        }
 
         /* Get extra result values */
         switch (func_type->types[func_type->param_count]) {
@@ -1542,9 +1543,9 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
             argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count;
         bh_memcpy_s(argv_ret, sizeof(uint32) * cell_num, ext_rets,
                     sizeof(uint32) * cell_num);
+
         if (argv1 != argv1_buf)
             wasm_runtime_free(argv1);
-
         return true;
     }
     else {

+ 4 - 107
core/iwasm/common/wasm_application.c

@@ -233,113 +233,10 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
     return (ret && !wasm_runtime_get_exception(module_inst)) ? true : false;
 }
 
-#if WASM_ENABLE_MULTI_MODULE != 0
-static WASMModuleInstance *
-get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
-                    const char *sub_module_name)
-{
-    WASMSubModInstNode *node =
-        bh_list_first_elem(parent_module_inst->sub_module_inst_list);
-
-    while (node && strcmp(node->module_name, sub_module_name)) {
-        node = bh_list_elem_next(node);
-    }
-    return node ? node->module_inst : NULL;
-}
-
-static bool
-parse_function_name(char *orig_function_name, char **p_module_name,
-                    char **p_function_name)
-{
-    if (orig_function_name[0] != '$') {
-        *p_module_name = NULL;
-        *p_function_name = orig_function_name;
-        return true;
-    }
-
-    /**
-     * $module_name$function_name\0
-     *  ===>
-     * module_name\0function_name\0
-     *  ===>
-     * module_name
-     * function_name
-     */
-    char *p1 = orig_function_name;
-    char *p2 = strchr(p1 + 1, '$');
-    if (!p2) {
-        LOG_DEBUG("can not parse the incoming function name");
-        return false;
-    }
-
-    *p_module_name = p1 + 1;
-    *p2 = '\0';
-    *p_function_name = p2 + 1;
-    return strlen(*p_module_name) && strlen(*p_function_name);
-}
-#endif
-
 /**
  * Implementation of wasm_application_execute_func()
  */
 
-static bool
-resolve_function(WASMModuleInstanceCommon *module_inst, const char *name,
-                 WASMFunctionInstanceCommon **out_func,
-                 WASMModuleInstanceCommon **out_module_inst)
-{
-    WASMFunctionInstanceCommon *target_func = NULL;
-    WASMModuleInstanceCommon *target_inst = NULL;
-
-#if WASM_ENABLE_MULTI_MODULE != 0
-    char *function_name = NULL;
-    char *orig_name = NULL;
-    char *sub_module_name = NULL;
-    uint32 length = (uint32)(strlen(name) + 1);
-
-    orig_name = runtime_malloc(sizeof(char) * length, NULL, NULL, 0);
-    if (!orig_name) {
-        goto LEAVE;
-    }
-
-    strncpy(orig_name, name, length);
-
-    if (!parse_function_name(orig_name, &sub_module_name, &function_name)) {
-        goto LEAVE;
-    }
-
-    LOG_DEBUG("%s -> %s and %s", name, sub_module_name, function_name);
-
-    if (sub_module_name) {
-        target_inst = (WASMModuleInstanceCommon *)get_sub_module_inst(
-            (WASMModuleInstance *)module_inst, sub_module_name);
-        if (!target_inst) {
-            LOG_DEBUG("can not find a sub module named %s", sub_module_name);
-            goto LEAVE;
-        }
-    }
-    else {
-        target_inst = module_inst;
-    }
-#else
-    const char *function_name = name;
-    target_inst = module_inst;
-#endif
-
-    target_func =
-        wasm_runtime_lookup_function(target_inst, function_name, NULL);
-
-#if WASM_ENABLE_MULTI_MODULE != 0
-LEAVE:
-    if (orig_name)
-        wasm_runtime_free(orig_name);
-#endif
-
-    *out_func = target_func;
-    *out_module_inst = target_inst;
-    return target_func;
-}
-
 union ieee754_float {
     float f;
 
@@ -386,7 +283,6 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name,
              int32 argc, char *argv[])
 {
     WASMFunctionInstanceCommon *target_func;
-    WASMModuleInstanceCommon *target_inst;
     WASMType *type = NULL;
     WASMExecEnv *exec_env = NULL;
     uint32 argc1, *argv1 = NULL, cell_num = 0, j, k = 0;
@@ -401,13 +297,14 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name,
     bh_assert(argc >= 0);
     LOG_DEBUG("call a function \"%s\" with %d arguments", name, argc);
 
-    if (!resolve_function(module_inst, name, &target_func, &target_inst)) {
+    if (!(target_func =
+              wasm_runtime_lookup_function(module_inst, name, NULL))) {
         snprintf(buf, sizeof(buf), "lookup function %s failed", name);
         wasm_runtime_set_exception(module_inst, buf);
         goto fail;
     }
 
-    module_type = target_inst->module_type;
+    module_type = module_inst->module_type;
     type = wasm_runtime_get_function_type(target_func, module_type);
 
     if (!type) {
@@ -439,7 +336,7 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name,
 #endif
 
     total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
-    if ((!(argv1 = runtime_malloc((uint32)total_size, target_inst, NULL, 0)))) {
+    if ((!(argv1 = runtime_malloc((uint32)total_size, module_inst, NULL, 0)))) {
         goto fail;
     }
 

+ 10 - 12
core/iwasm/include/wasm_export.h

@@ -123,9 +123,9 @@ typedef union MemAllocOption {
 
 /* Memory pool info  */
 typedef struct mem_alloc_info_t {
-        uint32_t total_size;
-        uint32_t total_free_size;
-        uint32_t highmark_size;
+    uint32_t total_size;
+    uint32_t total_free_size;
+    uint32_t highmark_size;
 } mem_alloc_info_t;
 
 /* WASM runtime initialize arguments */
@@ -265,20 +265,18 @@ WASM_RUNTIME_API_EXTERN bool
 wasm_runtime_is_xip_file(const uint8_t *buf, uint32_t size);
 
 /**
- * It is a callback for WAMR providing by embedding to load a module file
- * into a buffer
+ * Callback to load a module file into a buffer in multi-module feature
  */
 typedef bool (*module_reader)(const char *module_name,
                               uint8_t **p_buffer, uint32_t *p_size);
 
 /**
- * It is a callback for WAMR providing by embedding to release the buffer which
- * is used by loading a module file
+ * Callback to release the buffer loaded by module_reader callback
  */
 typedef void (*module_destroyer)(uint8_t *buffer, uint32_t size);
 
 /**
- * To setup callbacks for reading and releasing a buffer about a module file
+ * Setup callbacks for reading and releasing a buffer about a module file
  *
  * @param reader a callback to read a module file into a buffer
  * @param destroyer a callback to release above buffer
@@ -288,7 +286,7 @@ wasm_runtime_set_module_reader(const module_reader reader,
                                const module_destroyer destroyer);
 /**
  * Give the "module" a name "module_name".
- * can not assign a new name to a module if it already has a name
+ * Can not assign a new name to a module if it already has a name
  *
  * @param module_name indicate a name
  * @param module the target module
@@ -302,8 +300,8 @@ wasm_runtime_register_module(const char *module_name, wasm_module_t module,
                              char *error_buf, uint32_t error_buf_size);
 
 /**
- * To check if there is already a loaded module named module_name in the
- * runtime. you will not want to load repeately
+ * Check if there is already a loaded module named module_name in the
+ * runtime. Repeately loading a module with the same name is not allowed.
  *
  * @param module_name indicate a name
  *
@@ -1060,7 +1058,7 @@ wasm_runtime_spawn_thread(wasm_exec_env_t exec_env, wasm_thread_t *tid,
                           wasm_thread_callback_t callback, void *arg);
 
 /**
- * Waits a spawned thread to terminate
+ * Wait a spawned thread to terminate
  *
  * @param tid thread id
  * @param retval if not NULL, output the return value of the thread

+ 0 - 16
samples/multi-module/src/main.c

@@ -122,22 +122,6 @@ main()
         "call \"C5\", it will be failed since it is a export function, ===> ");
     wasm_application_execute_func(module_inst, "C5", 0, args);
 
-    /* call functions of mB */
-    printf("call \"mB.B1\", it will return 0x15:i32, ===> ");
-    wasm_application_execute_func(module_inst, "$mB$B1", 0, args);
-    printf("call \"mB.B2\", it will call A1() of mA and return 0xb:i32, ===> ");
-    wasm_application_execute_func(module_inst, "$mB$B2", 0, args);
-    printf("call \"mB.B3\", it will be failed since it is a export function, "
-           "===> ");
-    wasm_application_execute_func(module_inst, "$mB$B3", 0, args);
-
-    /* call functions of mA */
-    printf("call \"mA.A1\", it will return 0xb:i32, ===>");
-    wasm_application_execute_func(module_inst, "$mA$A1", 0, args);
-    printf("call \"mA.A2\", it will be failed since it is a export function, "
-           "===> ");
-    wasm_application_execute_func(module_inst, "$mA$A2", 0, args);
-    printf("----------------------------------------\n\n");
     ret = true;
 
     printf("- wasm_runtime_deinstantiate\n");

+ 2 - 3
wamr-compiler/main.c

@@ -48,9 +48,8 @@ print_help()
     printf("                            thread-mgr will be enabled automatically\n");
     printf("  --enable-tail-call        Enable the post-MVP tail call feature\n");
     printf("  --disable-simd            Disable the post-MVP 128-bit SIMD feature:\n");
-    printf("                              currently 128-bit SIMD is only supported for x86-64 target,\n");
-    printf("                              and by default it is enabled in x86-64 target and disabled\n");
-    printf("                              in other targets\n");
+    printf("                              currently 128-bit SIMD is supported for x86-64 and aarch64 targets,\n");
+    printf("                              and by default it is enabled in them and disabled in other targets\n");
     printf("  --disable-ref-types       Disable the MVP reference types feature\n");
     printf("  --disable-aux-stack-check Disable auxiliary stack overflow/underflow check\n");
     printf("  --enable-dump-call-stack  Enable stack trace feature\n");