Просмотр исходного кода

Fix GC loader issue and refine call_indirect for interpreter (#2118)

Wenyong Huang 2 лет назад
Родитель
Сommit
2779598dc3

+ 3 - 0
core/iwasm/interpreter/wasm.h

@@ -307,6 +307,9 @@ typedef struct WASMFuncType {
     uint16 ref_type_map_count;
     WASMRefTypeMap *ref_type_maps;
     WASMRefTypeMap *result_ref_type_maps;
+    /* minimal type index of the type equal to this type,
+       used in type equal check in call_indirect opcode */
+    uint32 min_type_idx_normalized;
 #else
     uint16 ref_count;
 #endif

+ 2 - 3
core/iwasm/interpreter/wasm_interp_classic.c

@@ -1715,9 +1715,8 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
                     goto got_exception;
                 }
 #else
-                if (!wasm_func_type_equal(cur_type, cur_func_type,
-                                          module->module->types,
-                                          module->module->type_count)) {
+                if (cur_type->min_type_idx_normalized
+                        != cur_func_type->min_type_idx_normalized) {
                     wasm_set_exception(module, "indirect call type mismatch");
                     goto got_exception;
                 }

+ 5 - 6
core/iwasm/interpreter/wasm_interp_fast.c

@@ -1557,12 +1557,11 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
                     goto got_exception;
                 }
 #else
-               if (!wasm_func_type_equal(cur_type, cur_func_type,
-                                         module->module->types,
-                                         module->module->type_count)) {
-                   wasm_set_exception(module, "indirect call type mismatch");
-                   goto got_exception;
-               }
+                if (cur_type->min_type_idx_normalized
+                        != cur_func_type->min_type_idx_normalized) {
+                    wasm_set_exception(module, "indirect call type mismatch");
+                    goto got_exception;
+                }
 #endif
                 /* clang-format on */
 

+ 13 - 2
core/iwasm/interpreter/wasm_loader.c

@@ -1061,6 +1061,18 @@ resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
     type->param_cell_num = (uint16)param_cell_num;
     type->ret_cell_num = (uint16)ret_cell_num;
 
+    /* Calculate the minimal type index of the type equal to this type */
+    type->min_type_idx_normalized = type_idx;
+    for (i = 0; i < type_idx; i++) {
+        WASMFuncType *func_type = (WASMFuncType *)module->types[i];
+        if (func_type->type_flag == WASM_TYPE_FUNC
+            && wasm_func_type_equal(type, func_type, module->types,
+                                    type_idx + 1)) {
+            type->min_type_idx_normalized = i;
+            break;
+        }
+    }
+
     *p_buf = p;
 
     module->types[type_idx] = (WASMType *)type;
@@ -9185,8 +9197,7 @@ re_scan:
                     block_type.is_value_type = false;
                     block_type.u.type =
                         (WASMFuncType *)module->types[type_index];
-#if WASM_ENABLE_FAST_INTERP == 0 && WASM_ENABLE_WAMR_COMPILER == 0 \
-    && WASM_ENABLE_JIT == 0
+#if WASM_ENABLE_FAST_INTERP == 0
                     /* If block use type index as block type, change the opcode
                      * to new extended opcode so that interpreter can resolve
                      * the block quickly.