Huang Qi 2 лет назад
Родитель
Сommit
06d46b0fb6

+ 31 - 13
core/iwasm/aot/aot_loader.c

@@ -1275,6 +1275,7 @@ load_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
     uint16 param_count, result_count, ref_type_map_count;
     bool is_sub_final;
     uint32 parent_type_idx;
+    WASMRefType ref_type;
 
     /* Allocate memory */
     size = sizeof(AOTFuncType *) * (uint64)module->type_count;
@@ -1338,11 +1339,18 @@ load_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
             func_type->param_cell_num = param_cell_num;
             func_type->ret_cell_num = ret_cell_num;
 
+            LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
+                        "ref type map count: %d",
+                        i, param_count, result_count, ref_type_map_count);
+
             /* If ref_type_map is not empty, read ref_type_map */
             if (ref_type_map_count > 0) {
                 bh_assert(func_type->ref_type_map_count
                           <= func_type->param_count + func_type->result_count);
 
+                /* align to 4 since param_count + result_count may be odd */
+                buf = align_ptr(buf, 4);
+
                 if (!(func_type->ref_type_maps =
                           loader_malloc(sizeof(WASMRefTypeMap)
                                             * func_type->ref_type_map_count,
@@ -1353,15 +1361,16 @@ load_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
                 for (j = 0; j < func_type->ref_type_map_count; j++) {
                     read_uint16(buf, buf_end,
                                 func_type->ref_type_maps[j].index);
-                    read_uint8(buf, buf_end,
-                               func_type->ref_type_maps[j]
-                                   .ref_type->ref_ht_common.ref_type);
-                    read_uint8(buf, buf_end,
-                               func_type->ref_type_maps[j]
-                                   .ref_type->ref_ht_common.nullable);
-                    read_uint32(buf, buf_end,
-                                func_type->ref_type_maps[j]
-                                    .ref_type->ref_ht_common.heap_type);
+                    read_uint8(buf, buf_end, ref_type.ref_ht_common.ref_type);
+                    read_uint8(buf, buf_end, ref_type.ref_ht_common.nullable);
+                    read_uint32(buf, buf_end, ref_type.ref_ht_common.heap_type);
+                    if (!(func_type->ref_type_maps[j].ref_type =
+                              wasm_reftype_set_insert(module->ref_type_set,
+                                                      &ref_type))) {
+                        set_error_buf(error_buf, error_buf_size,
+                                      "insert ref type to hash set failed");
+                        goto fail;
+                    }
                 }
             }
         }
@@ -1369,6 +1378,7 @@ load_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
             AOTStructType *struct_type;
             uint16 field_count;
             read_uint16(buf, buf_end, field_count);
+            read_uint16(buf, buf_end, ref_type_map_count);
             struct_type =
                 loader_malloc(sizeof(AOTStructType)
                                   + field_count * sizeof(WASMStructFieldType),
@@ -1381,17 +1391,23 @@ load_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
 
             struct_type->base_type.type_flag = type_flag;
             struct_type->field_count = field_count;
+            struct_type->ref_type_map_count = ref_type_map_count;
+
+            LOG_VERBOSE(
+                "type %u: struct, field count: %d, ref type map count: %d", i,
+                field_count, ref_type_map_count);
 
             /* Read types of fields */
             for (j = 0; j < field_count; j++) {
-                read_uint16(buf, buf_end, struct_type->fields[j].field_flags);
+                read_uint8(buf, buf_end, struct_type->fields[j].field_flags);
                 read_uint8(buf, buf_end, struct_type->fields[j].field_type);
+                LOG_VERBOSE("                field: %d, flags: %d, type: %d", j,
+                            struct_type->fields[i].field_flags,
+                            struct_type->fields[i].field_type);
             }
-            /* Read ref_type_map_count */
-            read_uint16(buf, buf_end, struct_type->ref_type_map_count);
 
             /* If ref_type_map is not empty, read ref_type_map */
-            if (struct_type->ref_type_map_count > 0) {
+            if (ref_type_map_count > 0) {
 
                 bh_assert(struct_type->ref_type_map_count <= field_count);
 
@@ -1432,6 +1448,8 @@ load_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
             array_type->base_type.type_flag = type_flag;
             read_uint16(buf, buf_end, array_type->elem_flags);
             read_uint8(buf, buf_end, array_type->elem_type);
+
+            LOG_VERBOSE("type %u: array", i);
         }
         else {
             set_error_buf_v(error_buf, error_buf_size,

+ 2 - 53
core/iwasm/compilation/aot.c

@@ -225,53 +225,6 @@ aot_create_globals(const WASMModule *module, uint32 global_data_start_offset,
     return globals;
 }
 
-static void
-aot_destroy_types(AOTType **types, uint32 count)
-{
-    uint32 i;
-    for (i = 0; i < count; i++)
-        if (types[i])
-            wasm_runtime_free(types[i]);
-    wasm_runtime_free(types);
-}
-
-static AOTType **
-aot_create_types(const WASMModule *module)
-{
-    AOTFuncType **func_types;
-    uint64 size;
-    uint32 i;
-
-    /* Allocate memory */
-    size = sizeof(AOTFuncType *) * (uint64)module->type_count;
-    if (size >= UINT32_MAX
-        || !(func_types = wasm_runtime_malloc((uint32)size))) {
-        aot_set_last_error("allocate memory failed.");
-        return NULL;
-    }
-
-    memset(func_types, 0, size);
-
-    /* Create each function type */
-    for (i = 0; i < module->type_count; i++) {
-        AOTFuncType *func_type = (AOTFuncType *)module->types[i];
-        size = offsetof(AOTFuncType, types) + (uint64)func_type->param_count
-               + (uint64)func_type->result_count;
-        if (size >= UINT32_MAX
-            || !(func_types[i] = wasm_runtime_malloc((uint32)size))) {
-            aot_set_last_error("allocate memory failed.");
-            goto fail;
-        }
-        memcpy(func_types[i], func_type, size);
-    }
-
-    return (AOTType **)func_types;
-
-fail:
-    aot_destroy_types((AOTType **)func_types, module->type_count);
-    return NULL;
-}
-
 static AOTImportFunc *
 aot_create_import_funcs(const WASMModule *module)
 {
@@ -509,10 +462,9 @@ aot_create_comp_data(WASMModule *module, bool gc_enabled)
 
     comp_data->global_data_size = import_global_data_size + global_data_size;
 
-    /* Create function types */
+    /* Create types, they are checked by wasm loader */
     comp_data->type_count = module->type_count;
-    if (comp_data->type_count && !(comp_data->types = aot_create_types(module)))
-        goto fail;
+    comp_data->types = module->types;
 
     /* Create import functions */
     comp_data->import_func_count = module->import_function_count;
@@ -587,9 +539,6 @@ aot_destroy_comp_data(AOTCompData *comp_data)
     if (comp_data->globals)
         wasm_runtime_free(comp_data->globals);
 
-    if (comp_data->types)
-        aot_destroy_types(comp_data->types, comp_data->type_count);
-
     if (comp_data->import_funcs)
         wasm_runtime_free(comp_data->import_funcs);
 

+ 11 - 13
core/iwasm/compilation/aot_emit_aot_file.c

@@ -402,6 +402,8 @@ get_func_type_size(AOTCompContext *comp_ctx, AOTFuncType *func_type)
         size += sizeof(func_type->ref_type_map_count);
         /* param and result types */
         size += func_type->param_count + func_type->result_count;
+        /* align size */
+        size = align_uint(size, 4);
         /* ref_type_map */
         size += func_type->ref_type_map_count * 8;
 
@@ -430,7 +432,7 @@ get_struct_type_size(AOTCompContext *comp_ctx, AOTStructType *struct_type)
     /* field count */
     size += sizeof(struct_type->field_count);
     /* field types */
-    size += struct_type->field_count * 3;
+    size += struct_type->field_count * 2;
     /* ref_type_map_count */
     size += sizeof(struct_type->ref_type_map_count);
     /* ref_type_map */
@@ -470,7 +472,7 @@ get_func_type_size(AOTCompContext *comp_ctx, AOTFuncType *func_type)
 #endif
 
 static uint32
-get_func_type_info_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
+get_type_info_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
 {
     /* Initial size with size of type count */
     uint32 size = 4;
@@ -650,7 +652,7 @@ get_init_data_section_size(AOTCompContext *comp_ctx, AOTCompData *comp_data,
     size += get_table_info_size(comp_ctx, comp_data);
 
     size = align_uint(size, 4);
-    size += get_func_type_info_size(comp_ctx, comp_data);
+    size += get_type_info_size(comp_ctx, comp_data);
 
     size = align_uint(size, 4);
     size += get_import_global_info_size(comp_ctx, comp_data);
@@ -1707,10 +1709,7 @@ aot_emit_type_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
                 EMIT_BUF(func_type->types,
                          func_type->param_count + func_type->result_count);
 
-                /* If no ref type used, then continue */
-                if (func_type->ref_type_map_count == 0) {
-                    continue;
-                }
+                offset = align_uint(offset, 4);
 
                 aot_emit_reftype_map(buf, buf_end, &offset,
                                      func_type->ref_type_map_count,
@@ -1720,11 +1719,12 @@ aot_emit_type_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
             else if (types[i]->type_flag == WASM_TYPE_STRUCT) {
                 AOTStructType *struct_type = (AOTStructType *)types[i];
                 EMIT_U16(struct_type->field_count);
+                EMIT_U16(struct_type->ref_type_map_count);
+
                 for (idx = 0; idx < struct_type->field_count; idx++) {
-                    EMIT_U16(struct_type->fields[idx].field_flags);
+                    EMIT_U8(struct_type->fields[idx].field_flags);
                     EMIT_U8(struct_type->fields[idx].field_type);
                 }
-                EMIT_U16(struct_type->ref_type_map_count);
                 aot_emit_reftype_map(buf, buf_end, &offset,
                                      struct_type->ref_type_map_count,
                                      struct_type->ref_type_maps);
@@ -1741,8 +1741,7 @@ aot_emit_type_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
             }
         }
 
-        if (offset - *p_offset
-            != get_func_type_info_size(comp_ctx, comp_data)) {
+        if (offset - *p_offset != get_type_info_size(comp_ctx, comp_data)) {
             aot_set_last_error("emit function type info failed.");
             return false;
         }
@@ -1768,8 +1767,7 @@ aot_emit_type_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
                      func_types[i]->param_count + func_types[i]->result_count);
         }
 
-        if (offset - *p_offset
-            != get_func_type_info_size(comp_ctx, comp_data)) {
+        if (offset - *p_offset != get_type_info_size(comp_ctx, comp_data)) {
             aot_set_last_error("emit function type info failed.");
             return false;
         }

+ 4 - 2
core/iwasm/compilation/aot_emit_function.c

@@ -24,12 +24,14 @@
 static bool
 is_win_platform(AOTCompContext *comp_ctx)
 {
+    bool ret = false;
     char *triple = LLVMGetTargetMachineTriple(comp_ctx->target_machine);
 
     bh_assert(triple);
     if (strstr(triple, "win32") || strstr(triple, "win"))
-        return true;
-    return false;
+        ret = true;
+    LLVMDisposeMessage(triple);
+    return ret;
 }
 
 static bool

+ 6 - 3
core/iwasm/interpreter/wasm_loader.c

@@ -995,7 +995,7 @@ resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
     }
 
     LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
-                "ref type map count: %d\n",
+                "ref type map count: %d",
                 type_idx, param_count, result_count, ref_type_map_count);
 
     /* Parse second time to resolve param types, result types and
@@ -1130,7 +1130,7 @@ resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
         }
     }
 
-    LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d\n",
+    LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
                 type_idx, field_count, ref_type_map_count);
 
     /* Parse second time to resolve field types and ref type map info */
@@ -1189,6 +1189,9 @@ resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
         if (wasm_is_type_reftype(ref_type.ref_type))
             *reference_table++ = offset;
         offset += type->fields[i].field_size;
+
+        LOG_VERBOSE("                field: %d, flags: %d, type: %d", i,
+                    type->fields[i].field_flags, type->fields[i].field_type);
     }
     type->total_size = offset;
 
@@ -1231,7 +1234,7 @@ resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
         return false;
     }
 
-    LOG_VERBOSE("type %u: array\n", type_idx);
+    LOG_VERBOSE("type %u: array", type_idx);
 
     if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
                                error_buf_size))) {