From 403a7d3f4fe3eec3092f9fb7b8a71dc93e4589b1 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Mon, 8 Nov 2021 11:27:26 +0800 Subject: [PATCH] Refine interp/aot string storage and emitting (#820) Currently the string in the wasm/aot file will be duplicated and stored into const string list/set in interpreter/aot loader, which leads to extra unnecessary memory consumption if the file buffer can be referred to after loading. We refine the string storage by: - if the file buffer can be referred to after loading and it is writable, we reuse the file buffer to store the string but not store it into the const string set: move string backward and append '\0' - emit string with '\0' only for XIP mode in which the AOT file is readonly - if the file buffer cannot be referred to, e.g. in app manager, keep the same behavior as before Signed-off-by: Wenyong Huang --- core/iwasm/aot/aot_loader.c | 207 +++++++++++++-------- core/iwasm/compilation/aot_emit_aot_file.c | 202 +++++++++++--------- core/iwasm/interpreter/wasm_loader.c | 74 +++++--- core/iwasm/interpreter/wasm_mini_loader.c | 76 +++++--- 4 files changed, 345 insertions(+), 214 deletions(-) diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index 5d192b53..b0bdc589 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -153,16 +153,12 @@ GET_U64_FROM_ADDR(uint32 *addr) p += len; \ } while (0) -#define read_string(p, p_end, str) \ - do { \ - uint16 str_len; \ - read_uint16(p, p_end, str_len); \ - CHECK_BUF(p, p_end, str_len); \ - if (!(str = const_str_set_insert(p, str_len, module, error_buf, \ - error_buf_size))) { \ - goto fail; \ - } \ - p += str_len; \ +#define read_string(p, p_end, str) \ + do { \ + if (!(str = load_string((uint8 **)&p, p_end, module, \ + is_load_from_file_buf, error_buf, \ + error_buf_size))) \ + goto fail; \ } while (0) /* Legal values for bin_type */ @@ -218,6 +214,17 @@ const_str_set_insert(const uint8 *str, int32 len, AOTModule *module, HashMap *set = module->const_str_set; char *c_str, *value; + /* Create const string set if it isn't created */ + if (!set + && !(set = module->const_str_set = bh_hash_map_create( + 32, false, (HashFunc)wasm_string_hash, + (KeyEqualFunc)wasm_string_equal, NULL, wasm_runtime_free))) { + set_error_buf(error_buf, error_buf_size, + "create const string set failed"); + return NULL; + } + + /* Lookup const string set, use the string if found */ if (!(c_str = loader_malloc((uint32)len + 1, error_buf, error_buf_size))) { return NULL; } @@ -240,6 +247,64 @@ const_str_set_insert(const uint8 *str, int32 len, AOTModule *module, return c_str; } +static char * +load_string(uint8 **p_buf, const uint8 *buf_end, AOTModule *module, + bool is_load_from_file_buf, char *error_buf, uint32 error_buf_size) +{ + uint8 *p = *p_buf; + const uint8 *p_end = buf_end; + char *str; + uint16 str_len; + + CHECK_BUF(p, p_end, 1); + if (*p & 0x80) { + /* The string has been adjusted */ + str = (char *)++p; + /* Ensure the whole string is in range */ + do { + CHECK_BUF(p, p_end, 1); + } while (*p++ != '\0'); + } + else { + /* The string hasn't been adjusted */ + read_uint16(p, p_end, str_len); + CHECK_BUF(p, p_end, str_len); + + if (str_len == 0) { + str = ""; + } + else if (p[str_len - 1] == '\0') { + /* The string is terminated with '\0', use it directly */ + str = (char *)p; + } + else if (is_load_from_file_buf) { + /* As the file buffer can be referred to after loading, + we use the 2 bytes of size to adjust the string: + mark the flag with the highest bit of size[0], + move string 1 byte backward and then append '\0' */ + *(p - 2) |= 0x80; + bh_memmove_s(p - 1, (uint32)(str_len + 1), p, (uint32)str_len); + p[str_len - 1] = '\0'; + str = (char *)(p - 1); + } + else { + /* Load from sections, the file buffer cannot be reffered to + after loading, we must create another string and insert it + into const string set */ + if (!(str = const_str_set_insert((uint8 *)p, str_len, module, + error_buf, error_buf_size))) { + goto fail; + } + } + p += str_len; + } + + *p_buf = p; + return str; +fail: + return NULL; +} + static bool get_aot_file_target(AOTTargetInfo *target_info, char *target_buf, uint32 target_buf_size, char *error_buf, @@ -398,8 +463,8 @@ get_native_symbol_by_name(const char *name) static bool load_native_symbol_section(const uint8 *buf, const uint8 *buf_end, - AOTModule *module, char *error_buf, - uint32 error_buf_size) + AOTModule *module, bool is_load_from_file_buf, + char *error_buf, uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; uint32 cnt; @@ -436,7 +501,8 @@ fail: static bool load_name_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module, - char *error_buf, uint32 error_buf_size) + bool is_load_from_file_buf, char *error_buf, + uint32 error_buf_size) { #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0 const uint8 *p = buf, *p_end = buf_end; @@ -563,7 +629,8 @@ fail: static bool load_custom_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module, - char *error_buf, uint32 error_buf_size) + bool is_load_from_file_buf, char *error_buf, + uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; uint32 sub_section_type; @@ -573,13 +640,14 @@ load_custom_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module, switch (sub_section_type) { case AOT_CUSTOM_SECTION_NATIVE_SYMBOL: - if (!load_native_symbol_section(buf, buf_end, module, error_buf, + if (!load_native_symbol_section(buf, buf_end, module, + is_load_from_file_buf, error_buf, error_buf_size)) goto fail; break; case AOT_CUSTOM_SECTION_NAME: - if (!load_name_section(buf, buf_end, module, error_buf, - error_buf_size)) + if (!load_name_section(buf, buf_end, module, is_load_from_file_buf, + error_buf, error_buf_size)) goto fail; break; default: @@ -977,7 +1045,8 @@ destroy_import_globals(AOTImportGlobal *import_globals, bool is_jit_mode) static bool load_import_globals(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, char *error_buf, uint32 error_buf_size) + AOTModule *module, bool is_load_from_file_buf, + char *error_buf, uint32 error_buf_size) { const uint8 *buf = *p_buf; AOTImportGlobal *import_globals; @@ -1031,8 +1100,8 @@ fail: static bool load_import_global_info(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, char *error_buf, - uint32 error_buf_size) + AOTModule *module, bool is_load_from_file_buf, + char *error_buf, uint32 error_buf_size) { const uint8 *buf = *p_buf; @@ -1040,8 +1109,8 @@ load_import_global_info(const uint8 **p_buf, const uint8 *buf_end, /* load import globals */ if (module->import_global_count > 0 - && !load_import_globals(&buf, buf_end, module, error_buf, - error_buf_size)) + && !load_import_globals(&buf, buf_end, module, is_load_from_file_buf, + error_buf, error_buf_size)) return false; *p_buf = buf; @@ -1141,7 +1210,8 @@ destroy_import_funcs(AOTImportFunc *import_funcs, bool is_jit_mode) static bool load_import_funcs(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, - char *error_buf, uint32 error_buf_size) + bool is_load_from_file_buf, char *error_buf, + uint32 error_buf_size) { const char *module_name, *field_name; const uint8 *buf = *p_buf; @@ -1190,7 +1260,8 @@ fail: static bool load_import_func_info(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, char *error_buf, uint32 error_buf_size) + AOTModule *module, bool is_load_from_file_buf, + char *error_buf, uint32 error_buf_size) { const uint8 *buf = *p_buf; @@ -1198,7 +1269,8 @@ load_import_func_info(const uint8 **p_buf, const uint8 *buf_end, /* load import funcs */ if (module->import_func_count > 0 - && !load_import_funcs(&buf, buf_end, module, error_buf, error_buf_size)) + && !load_import_funcs(&buf, buf_end, module, is_load_from_file_buf, + error_buf, error_buf_size)) return false; *p_buf = buf; @@ -1221,8 +1293,8 @@ destroy_object_data_sections(AOTObjectDataSection *data_sections, static bool load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, char *error_buf, - uint32 error_buf_size) + AOTModule *module, bool is_load_from_file_buf, + char *error_buf, uint32 error_buf_size) { const uint8 *buf = *p_buf; AOTObjectDataSection *data_sections; @@ -1280,8 +1352,8 @@ fail: static bool load_object_data_sections_info(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, char *error_buf, - uint32 error_buf_size) + AOTModule *module, bool is_load_from_file_buf, + char *error_buf, uint32 error_buf_size) { const uint8 *buf = *p_buf; @@ -1289,7 +1361,8 @@ load_object_data_sections_info(const uint8 **p_buf, const uint8 *buf_end, /* load object data sections */ if (module->data_section_count > 0 - && !load_object_data_sections(&buf, buf_end, module, error_buf, + && !load_object_data_sections(&buf, buf_end, module, + is_load_from_file_buf, error_buf, error_buf_size)) return false; @@ -1301,18 +1374,19 @@ fail: static bool load_init_data_section(const uint8 *buf, const uint8 *buf_end, - AOTModule *module, char *error_buf, - uint32 error_buf_size) + AOTModule *module, bool is_load_from_file_buf, + char *error_buf, uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; if (!load_memory_info(&p, p_end, module, error_buf, error_buf_size) || !load_table_info(&p, p_end, module, error_buf, error_buf_size) || !load_func_type_info(&p, p_end, module, error_buf, error_buf_size) - || !load_import_global_info(&p, p_end, module, error_buf, - error_buf_size) + || !load_import_global_info(&p, p_end, module, is_load_from_file_buf, + error_buf, error_buf_size) || !load_global_info(&p, p_end, module, error_buf, error_buf_size) - || !load_import_func_info(&p, p_end, module, error_buf, error_buf_size)) + || !load_import_func_info(&p, p_end, module, is_load_from_file_buf, + error_buf, error_buf_size)) return false; /* load function count and start function index */ @@ -1336,7 +1410,8 @@ load_init_data_section(const uint8 *buf, const uint8 *buf_end, read_uint32(p, p_end, module->aux_stack_bottom); read_uint32(p, p_end, module->aux_stack_size); - if (!load_object_data_sections_info(&p, p_end, module, error_buf, + if (!load_object_data_sections_info(&p, p_end, module, + is_load_from_file_buf, error_buf, error_buf_size)) return false; @@ -1526,7 +1601,7 @@ destroy_exports(AOTExport *exports, bool is_jit_mode) static bool load_exports(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, - char *error_buf, uint32 error_buf_size) + bool is_load_from_file_buf, char *error_buf, uint32 error_buf_size) { const uint8 *buf = *p_buf; AOTExport *exports; @@ -1563,14 +1638,16 @@ fail: static bool load_export_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module, - char *error_buf, uint32 error_buf_size) + bool is_load_from_file_buf, char *error_buf, + uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; /* load export functions */ read_uint32(p, p_end, module->export_count); if (module->export_count > 0 - && !load_exports(&p, p_end, module, error_buf, error_buf_size)) + && !load_exports(&p, p_end, module, is_load_from_file_buf, error_buf, + error_buf_size)) return false; if (p != p_end) { @@ -1903,8 +1980,8 @@ fail: static bool load_relocation_section(const uint8 *buf, const uint8 *buf_end, - AOTModule *module, char *error_buf, - uint32 error_buf_size) + AOTModule *module, bool is_load_from_file_buf, + char *error_buf, uint32 error_buf_size) { AOTRelocationGroup *groups = NULL, *group; uint32 symbol_count = 0; @@ -2048,7 +2125,6 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, for (i = 0, group = groups; i < group_count; i++, group++) { AOTRelocation *relocation; uint32 name_index; - uint16 str_len; uint8 *name_addr; /* section name address is 4 bytes aligned. */ @@ -2062,13 +2138,7 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, } name_addr = symbol_buf + symbol_offsets[name_index]; - str_len = *(uint16 *)name_addr; - - if (!(group->section_name = const_str_set_insert( - name_addr + sizeof(uint16), (int32)str_len, module, error_buf, - error_buf_size))) { - goto fail; - } + read_string(name_addr, buf_end, group->section_name); read_uint32(buf, buf_end, group->relocation_count); @@ -2083,7 +2153,6 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, /* Load each relocation */ for (j = 0; j < group->relocation_count; j++, relocation++) { uint32 symbol_index; - uint16 str_len; uint8 *symbol_addr; if (sizeof(void *) == 8) { @@ -2107,13 +2176,7 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, } symbol_addr = symbol_buf + symbol_offsets[symbol_index]; - str_len = *(uint16 *)symbol_addr; - - if (!(relocation->symbol_name = const_str_set_insert( - symbol_addr + sizeof(uint16), (int32)str_len, module, - error_buf, error_buf_size))) { - goto fail; - } + read_string(symbol_addr, buf_end, relocation->symbol_name); } if (!strcmp(group->section_name, ".rel.text") @@ -2185,7 +2248,8 @@ fail: } static bool -load_from_sections(AOTModule *module, AOTSection *sections, char *error_buf, +load_from_sections(AOTModule *module, AOTSection *sections, + bool is_load_from_file_buf, char *error_buf, uint32 error_buf_size) { AOTSection *section = sections; @@ -2216,7 +2280,8 @@ load_from_sections(AOTModule *module, AOTSection *sections, char *error_buf, return false; break; case AOT_SECTION_TYPE_INIT_DATA: - if (!load_init_data_section(buf, buf_end, module, error_buf, + if (!load_init_data_section(buf, buf_end, module, + is_load_from_file_buf, error_buf, error_buf_size)) return false; break; @@ -2231,17 +2296,20 @@ load_from_sections(AOTModule *module, AOTSection *sections, char *error_buf, return false; break; case AOT_SECTION_TYPE_EXPORT: - if (!load_export_section(buf, buf_end, module, error_buf, + if (!load_export_section(buf, buf_end, module, + is_load_from_file_buf, error_buf, error_buf_size)) return false; break; case AOT_SECTION_TYPE_RELOCATION: - if (!load_relocation_section(buf, buf_end, module, error_buf, + if (!load_relocation_section(buf, buf_end, module, + is_load_from_file_buf, error_buf, error_buf_size)) return false; break; case AOT_SECTION_TYPE_CUSTOM: - if (!load_custom_section(buf, buf_end, module, error_buf, + if (!load_custom_section(buf, buf_end, module, + is_load_from_file_buf, error_buf, error_buf_size)) return false; break; @@ -2379,15 +2447,6 @@ create_module(char *error_buf, uint32 error_buf_size) module->module_type = Wasm_Module_AoT; - if (!(module->const_str_set = bh_hash_map_create( - 32, false, (HashFunc)wasm_string_hash, - (KeyEqualFunc)wasm_string_equal, NULL, wasm_runtime_free))) { - set_error_buf(error_buf, error_buf_size, - "create const string set failed"); - wasm_runtime_free(module); - return NULL; - } - return module; } @@ -2400,7 +2459,8 @@ aot_load_from_sections(AOTSection *section_list, char *error_buf, if (!module) return NULL; - if (!load_from_sections(module, section_list, error_buf, error_buf_size)) { + if (!load_from_sections(module, section_list, false, error_buf, + error_buf_size)) { aot_unload(module); return NULL; } @@ -2599,7 +2659,8 @@ load(const uint8 *buf, uint32 size, AOTModule *module, char *error_buf, error_buf_size)) return false; - ret = load_from_sections(module, section_list, error_buf, error_buf_size); + ret = load_from_sections(module, section_list, true, error_buf, + error_buf_size); if (!ret) { /* If load_from_sections() fails, then aot text is destroyed in destroy_sections() */ diff --git a/core/iwasm/compilation/aot_emit_aot_file.c b/core/iwasm/compilation/aot_emit_aot_file.c index 0efce9d2..c86f8ab0 100644 --- a/core/iwasm/compilation/aot_emit_aot_file.c +++ b/core/iwasm/compilation/aot_emit_aot_file.c @@ -177,10 +177,12 @@ get_file_header_size() } static uint32 -get_string_size(const char *s) +get_string_size(AOTCompContext *comp_ctx, const char *s) { - /* string size (2 bytes) + string content without '\0' */ - return (uint32)sizeof(uint16) + (uint32)strlen(s); + /* string size (2 bytes) + string content */ + return (uint32)sizeof(uint16) + (uint32)strlen(s) + + /* emit string with '\0' only in XIP mode */ + (comp_ctx->is_indirect_mode ? 1 : 0); } static uint32 @@ -388,18 +390,19 @@ get_func_type_info_size(AOTCompData *comp_data) } static uint32 -get_import_global_size(AOTImportGlobal *import_global) +get_import_global_size(AOTCompContext *comp_ctx, AOTImportGlobal *import_global) { /* type (1 byte) + is_mutable (1 byte) + module_name + global_name */ - uint32 size = - (uint32)sizeof(uint8) * 2 + get_string_size(import_global->module_name); + uint32 size = (uint32)sizeof(uint8) * 2 + + get_string_size(comp_ctx, import_global->module_name); size = align_uint(size, 2); - size += get_string_size(import_global->global_name); + size += get_string_size(comp_ctx, import_global->global_name); return size; } static uint32 -get_import_globals_size(AOTImportGlobal *import_globals, +get_import_globals_size(AOTCompContext *comp_ctx, + AOTImportGlobal *import_globals, uint32 import_global_count) { AOTImportGlobal *import_global = import_globals; @@ -407,17 +410,17 @@ get_import_globals_size(AOTImportGlobal *import_globals, for (i = 0; i < import_global_count; i++, import_global++) { size = align_uint(size, 2); - size += get_import_global_size(import_global); + size += get_import_global_size(comp_ctx, import_global); } return size; } static uint32 -get_import_global_info_size(AOTCompData *comp_data) +get_import_global_info_size(AOTCompContext *comp_ctx, AOTCompData *comp_data) { /* import global count + import globals */ return (uint32)sizeof(uint32) - + get_import_globals_size(comp_data->import_globals, + + get_import_globals_size(comp_ctx, comp_data->import_globals, comp_data->import_global_count); } @@ -456,40 +459,42 @@ get_global_info_size(AOTCompData *comp_data) } static uint32 -get_import_func_size(AOTImportFunc *import_func) +get_import_func_size(AOTCompContext *comp_ctx, AOTImportFunc *import_func) { /* type index (2 bytes) + module_name + func_name */ - uint32 size = - (uint32)sizeof(uint16) + get_string_size(import_func->module_name); + uint32 size = (uint32)sizeof(uint16) + + get_string_size(comp_ctx, import_func->module_name); size = align_uint(size, 2); - size += get_string_size(import_func->func_name); + size += get_string_size(comp_ctx, import_func->func_name); return size; } static uint32 -get_import_funcs_size(AOTImportFunc *import_funcs, uint32 import_func_count) +get_import_funcs_size(AOTCompContext *comp_ctx, AOTImportFunc *import_funcs, + uint32 import_func_count) { AOTImportFunc *import_func = import_funcs; uint32 size = 0, i; for (i = 0; i < import_func_count; i++, import_func++) { size = align_uint(size, 2); - size += get_import_func_size(import_func); + size += get_import_func_size(comp_ctx, import_func); } return size; } static uint32 -get_import_func_info_size(AOTCompData *comp_data) +get_import_func_info_size(AOTCompContext *comp_ctx, AOTCompData *comp_data) { /* import func count + import funcs */ return (uint32)sizeof(uint32) - + get_import_funcs_size(comp_data->import_funcs, + + get_import_funcs_size(comp_ctx, comp_data->import_funcs, comp_data->import_func_count); } static uint32 -get_object_data_sections_size(AOTObjectDataSection *data_sections, +get_object_data_sections_size(AOTCompContext *comp_ctx, + AOTObjectDataSection *data_sections, uint32 data_sections_count) { AOTObjectDataSection *data_section = data_sections; @@ -498,7 +503,7 @@ get_object_data_sections_size(AOTObjectDataSection *data_sections, for (i = 0; i < data_sections_count; i++, data_section++) { /* name + size + data */ size = align_uint(size, 2); - size += get_string_size(data_section->name); + size += get_string_size(comp_ctx, data_section->name); size = align_uint(size, 4); size += (uint32)sizeof(uint32); size += data_section->size; @@ -507,16 +512,18 @@ get_object_data_sections_size(AOTObjectDataSection *data_sections, } static uint32 -get_object_data_section_info_size(AOTObjectData *obj_data) +get_object_data_section_info_size(AOTCompContext *comp_ctx, + AOTObjectData *obj_data) { /* data sections count + data sections */ return (uint32)sizeof(uint32) - + get_object_data_sections_size(obj_data->data_sections, + + get_object_data_sections_size(comp_ctx, obj_data->data_sections, obj_data->data_sections_count); } static uint32 -get_init_data_section_size(AOTCompData *comp_data, AOTObjectData *obj_data) +get_init_data_section_size(AOTCompContext *comp_ctx, AOTCompData *comp_data, + AOTObjectData *obj_data) { uint32 size = 0; @@ -529,13 +536,13 @@ get_init_data_section_size(AOTCompData *comp_data, AOTObjectData *obj_data) size += get_func_type_info_size(comp_data); size = align_uint(size, 4); - size += get_import_global_info_size(comp_data); + size += get_import_global_info_size(comp_ctx, comp_data); size = align_uint(size, 4); size += get_global_info_size(comp_data); size = align_uint(size, 4); - size += get_import_func_info_size(comp_data); + size += get_import_func_info_size(comp_ctx, comp_data); /* func count + start func index */ size = align_uint(size, 4); @@ -544,7 +551,7 @@ get_init_data_section_size(AOTCompData *comp_data, AOTObjectData *obj_data) /* aux data/heap/stack data */ size += sizeof(uint32) * 7; - size += get_object_data_section_info_size(obj_data); + size += get_object_data_section_info_size(comp_ctx, obj_data); return size; } @@ -571,32 +578,33 @@ get_func_section_size(AOTCompData *comp_data, AOTObjectData *obj_data) } static uint32 -get_export_size(AOTExport *export) +get_export_size(AOTCompContext *comp_ctx, AOTExport *export) { /* export index + export kind + 1 byte padding + export name */ return (uint32)sizeof(uint32) + sizeof(uint8) + 1 - + get_string_size(export->name); + + get_string_size(comp_ctx, export->name); } static uint32 -get_exports_size(AOTExport *exports, uint32 export_count) +get_exports_size(AOTCompContext *comp_ctx, AOTExport *exports, + uint32 export_count) { AOTExport *export = exports; uint32 size = 0, i; for (i = 0; i < export_count; i++, export ++) { size = align_uint(size, 4); - size += get_export_size(export); + size += get_export_size(comp_ctx, export); } return size; } static uint32 -get_export_section_size(AOTCompData *comp_data) +get_export_section_size(AOTCompContext *comp_ctx, AOTCompData *comp_data) { /* export count + exports */ return (uint32)sizeof(uint32) - + get_exports_size(comp_data->wasm_module->exports, + + get_exports_size(comp_ctx, comp_data->wasm_module->exports, comp_data->wasm_module->export_count); } @@ -700,7 +708,7 @@ get_relocation_symbol_index(const char *symbol_name, bool *is_new, } static uint32 -get_relocation_symbol_size(AOTRelocation *relocation, +get_relocation_symbol_size(AOTCompContext *comp_ctx, AOTRelocation *relocation, AOTSymbolList *symbol_list) { uint32 size = 0, index = 0; @@ -711,7 +719,7 @@ get_relocation_symbol_size(AOTRelocation *relocation, CHECK_SIZE(index); if (is_new) { - size += get_string_size(relocation->symbol_name); + size += get_string_size(comp_ctx, relocation->symbol_name); size = align_uint(size, 2); } @@ -720,14 +728,16 @@ get_relocation_symbol_size(AOTRelocation *relocation, } static uint32 -get_relocations_symbol_size(AOTRelocation *relocations, uint32 relocation_count, +get_relocations_symbol_size(AOTCompContext *comp_ctx, + AOTRelocation *relocations, uint32 relocation_count, AOTSymbolList *symbol_list) { AOTRelocation *relocation = relocations; uint32 size = 0, curr_size, i; for (i = 0; i < relocation_count; i++, relocation++) { - curr_size = get_relocation_symbol_size(relocation, symbol_list); + curr_size = + get_relocation_symbol_size(comp_ctx, relocation, symbol_list); CHECK_SIZE(curr_size); size += curr_size; @@ -736,7 +746,8 @@ get_relocations_symbol_size(AOTRelocation *relocations, uint32 relocation_count, } static uint32 -get_relocation_group_symbol_size(AOTRelocationGroup *relocation_group, +get_relocation_group_symbol_size(AOTCompContext *comp_ctx, + AOTRelocationGroup *relocation_group, AOTSymbolList *symbol_list) { uint32 size = 0, index = 0, curr_size; @@ -747,15 +758,15 @@ get_relocation_group_symbol_size(AOTRelocationGroup *relocation_group, CHECK_SIZE(index); if (is_new) { - size += get_string_size(relocation_group->section_name); + size += get_string_size(comp_ctx, relocation_group->section_name); size = align_uint(size, 2); } relocation_group->name_index = index; - curr_size = get_relocations_symbol_size(relocation_group->relocations, - relocation_group->relocation_count, - symbol_list); + curr_size = get_relocations_symbol_size( + comp_ctx, relocation_group->relocations, + relocation_group->relocation_count, symbol_list); CHECK_SIZE(curr_size); size += curr_size; @@ -763,7 +774,8 @@ get_relocation_group_symbol_size(AOTRelocationGroup *relocation_group, } static uint32 -get_relocation_groups_symbol_size(AOTRelocationGroup *relocation_groups, +get_relocation_groups_symbol_size(AOTCompContext *comp_ctx, + AOTRelocationGroup *relocation_groups, uint32 relocation_group_count, AOTSymbolList *symbol_list) { @@ -771,8 +783,8 @@ get_relocation_groups_symbol_size(AOTRelocationGroup *relocation_groups, uint32 size = 0, curr_size, i; for (i = 0; i < relocation_group_count; i++, relocation_group++) { - curr_size = - get_relocation_group_symbol_size(relocation_group, symbol_list); + curr_size = get_relocation_group_symbol_size(comp_ctx, relocation_group, + symbol_list); CHECK_SIZE(curr_size); size += curr_size; } @@ -780,7 +792,8 @@ get_relocation_groups_symbol_size(AOTRelocationGroup *relocation_groups, } static uint32 -get_symbol_size_from_symbol_list(AOTSymbolList *symbol_list) +get_symbol_size_from_symbol_list(AOTCompContext *comp_ctx, + AOTSymbolList *symbol_list) { AOTSymbolNode *sym; uint32 size = 0; @@ -788,7 +801,7 @@ get_symbol_size_from_symbol_list(AOTSymbolList *symbol_list) sym = symbol_list->head; while (sym) { /* (uint16)str_len + str */ - size += get_string_size(sym->symbol); + size += get_string_size(comp_ctx, sym->symbol); size = align_uint(size, 2); sym = sym->next; } @@ -797,7 +810,8 @@ get_symbol_size_from_symbol_list(AOTSymbolList *symbol_list) } static uint32 -get_relocation_section_symbol_size(AOTObjectData *obj_data) +get_relocation_section_symbol_size(AOTCompContext *comp_ctx, + AOTObjectData *obj_data) { AOTRelocationGroup *relocation_groups = obj_data->relocation_groups; uint32 relocation_group_count = obj_data->relocation_group_count; @@ -807,11 +821,12 @@ get_relocation_section_symbol_size(AOTObjectData *obj_data) get symbol size from symbol list directly in the second calculation */ if (obj_data->symbol_list.len > 0) { symbol_table_size = - get_symbol_size_from_symbol_list(&obj_data->symbol_list); + get_symbol_size_from_symbol_list(comp_ctx, &obj_data->symbol_list); } else { symbol_table_size = get_relocation_groups_symbol_size( - relocation_groups, relocation_group_count, &obj_data->symbol_list); + comp_ctx, relocation_groups, relocation_group_count, + &obj_data->symbol_list); } CHECK_SIZE(symbol_table_size); string_count = obj_data->symbol_list.len; @@ -823,13 +838,13 @@ get_relocation_section_symbol_size(AOTObjectData *obj_data) } static uint32 -get_relocation_section_size(AOTObjectData *obj_data) +get_relocation_section_size(AOTCompContext *comp_ctx, AOTObjectData *obj_data) { AOTRelocationGroup *relocation_groups = obj_data->relocation_groups; uint32 relocation_group_count = obj_data->relocation_group_count; uint32 symbol_table_size = 0; - symbol_table_size = get_relocation_section_symbol_size(obj_data); + symbol_table_size = get_relocation_section_symbol_size(comp_ctx, obj_data); CHECK_SIZE(symbol_table_size); symbol_table_size = align_uint(symbol_table_size, 4); @@ -850,7 +865,7 @@ get_native_symbol_list_size(AOTCompContext *comp_ctx) while (sym) { len = align_uint(len, 2); - len += get_string_size(sym->symbol); + len += get_string_size(comp_ctx, sym->symbol); sym = bh_list_elem_next(sym); } @@ -879,7 +894,7 @@ get_aot_file_size(AOTCompContext *comp_ctx, AOTCompData *comp_data, size = align_uint(size, 4); /* section id + section size */ size += (uint32)sizeof(uint32) * 2; - size += get_init_data_section_size(comp_data, obj_data); + size += get_init_data_section_size(comp_ctx, comp_data, obj_data); /* text section */ size = align_uint(size, 4); @@ -897,13 +912,13 @@ get_aot_file_size(AOTCompContext *comp_ctx, AOTCompData *comp_data, size = align_uint(size, 4); /* section id + section size */ size += (uint32)sizeof(uint32) * 2; - size += get_export_section_size(comp_data); + size += get_export_section_size(comp_ctx, comp_data); /* relocation section */ size = align_uint(size, 4); /* section id + section size */ size += (uint32)sizeof(uint32) * 2; - size += get_relocation_section_size(obj_data); + size += get_relocation_section_size(comp_ctx, obj_data); if (get_native_symbol_list_size(comp_ctx) > 0) { /* emit only when there are native symbols */ @@ -1043,11 +1058,19 @@ static union { offset += len; \ } while (0) -#define EMIT_STR(s) \ - do { \ - uint32 str_len = (uint32)strlen(s); \ - EMIT_U16(str_len); \ - EMIT_BUF(s, str_len); \ +#define EMIT_STR(s) \ + do { \ + uint32 str_len = (uint32)strlen(s); \ + if (str_len > INT16_MAX) { \ + aot_set_last_error("emit string failed: " \ + "string too long"); \ + return false; \ + } \ + if (comp_ctx->is_indirect_mode) \ + /* emit '\0' only in XIP mode */ \ + str_len++; \ + EMIT_U16(str_len); \ + EMIT_BUF(s, str_len); \ } while (0) static bool @@ -1359,7 +1382,8 @@ aot_emit_mem_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset, static bool aot_emit_table_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset, - AOTCompData *comp_data, AOTObjectData *obj_data) + AOTCompContext *comp_ctx, AOTCompData *comp_data, + AOTObjectData *obj_data) { uint32 offset = *p_offset, i, j; AOTTableInitData **init_datas = comp_data->table_init_data_list; @@ -1446,7 +1470,8 @@ aot_emit_func_type_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset, static bool aot_emit_import_global_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset, - AOTCompData *comp_data, AOTObjectData *obj_data) + AOTCompContext *comp_ctx, AOTCompData *comp_data, + AOTObjectData *obj_data) { uint32 offset = *p_offset, i; AOTImportGlobal *import_global = comp_data->import_globals; @@ -1464,7 +1489,8 @@ aot_emit_import_global_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset, EMIT_STR(import_global->global_name); } - if (offset - *p_offset != get_import_global_info_size(comp_data)) { + if (offset - *p_offset + != get_import_global_info_size(comp_ctx, comp_data)) { aot_set_last_error("emit import global info failed."); return false; } @@ -1508,7 +1534,8 @@ aot_emit_global_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset, static bool aot_emit_import_func_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset, - AOTCompData *comp_data, AOTObjectData *obj_data) + AOTCompContext *comp_ctx, AOTCompData *comp_data, + AOTObjectData *obj_data) { uint32 offset = *p_offset, i; AOTImportFunc *import_func = comp_data->import_funcs; @@ -1525,7 +1552,7 @@ aot_emit_import_func_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset, EMIT_STR(import_func->func_name); } - if (offset - *p_offset != get_import_func_info_size(comp_data)) { + if (offset - *p_offset != get_import_func_info_size(comp_ctx, comp_data)) { aot_set_last_error("emit import function info failed."); return false; } @@ -1537,6 +1564,7 @@ aot_emit_import_func_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset, static bool aot_emit_object_data_section_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset, + AOTCompContext *comp_ctx, AOTObjectData *obj_data) { uint32 offset = *p_offset, i; @@ -1554,7 +1582,8 @@ aot_emit_object_data_section_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset, EMIT_BUF(data_section->data, data_section->size); } - if (offset - *p_offset != get_object_data_section_info_size(obj_data)) { + if (offset - *p_offset + != get_object_data_section_info_size(comp_ctx, obj_data)) { aot_set_last_error("emit object data section info failed."); return false; } @@ -1569,7 +1598,8 @@ aot_emit_init_data_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset, AOTCompContext *comp_ctx, AOTCompData *comp_data, AOTObjectData *obj_data) { - uint32 section_size = get_init_data_section_size(comp_data, obj_data); + uint32 section_size = + get_init_data_section_size(comp_ctx, comp_data, obj_data); uint32 offset = *p_offset; *p_offset = offset = align_uint(offset, 4); @@ -1578,13 +1608,14 @@ aot_emit_init_data_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset, EMIT_U32(section_size); if (!aot_emit_mem_info(buf, buf_end, &offset, comp_ctx, comp_data, obj_data) - || !aot_emit_table_info(buf, buf_end, &offset, comp_data, obj_data) + || !aot_emit_table_info(buf, buf_end, &offset, comp_ctx, comp_data, + obj_data) || !aot_emit_func_type_info(buf, buf_end, &offset, comp_data, obj_data) - || !aot_emit_import_global_info(buf, buf_end, &offset, comp_data, - obj_data) + || !aot_emit_import_global_info(buf, buf_end, &offset, comp_ctx, + comp_data, obj_data) || !aot_emit_global_info(buf, buf_end, &offset, comp_data, obj_data) - || !aot_emit_import_func_info(buf, buf_end, &offset, comp_data, - obj_data)) + || !aot_emit_import_func_info(buf, buf_end, &offset, comp_ctx, + comp_data, obj_data)) return false; offset = align_uint(offset, 4); @@ -1599,7 +1630,8 @@ aot_emit_init_data_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset, EMIT_U32(comp_data->aux_stack_bottom); EMIT_U32(comp_data->aux_stack_size); - if (!aot_emit_object_data_section_info(buf, buf_end, &offset, obj_data)) + if (!aot_emit_object_data_section_info(buf, buf_end, &offset, comp_ctx, + obj_data)) return false; if (offset - *p_offset != section_size + sizeof(uint32) * 2) { @@ -1678,9 +1710,10 @@ aot_emit_func_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset, static bool aot_emit_export_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset, - AOTCompData *comp_data, AOTObjectData *obj_data) + AOTCompContext *comp_ctx, AOTCompData *comp_data, + AOTObjectData *obj_data) { - uint32 section_size = get_export_section_size(comp_data); + uint32 section_size = get_export_section_size(comp_ctx, comp_data); AOTExport *export = comp_data->wasm_module->exports; uint32 export_count = comp_data->wasm_module->export_count; uint32 i, offset = *p_offset; @@ -1711,6 +1744,7 @@ aot_emit_export_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset, static bool aot_emit_relocation_symbol_table(uint8 *buf, uint8 *buf_end, uint32 *p_offset, + AOTCompContext *comp_ctx, AOTCompData *comp_data, AOTObjectData *obj_data) { @@ -1725,7 +1759,7 @@ aot_emit_relocation_symbol_table(uint8 *buf, uint8 *buf_end, uint32 *p_offset, while (sym) { EMIT_U32(symbol_offset); /* string_len + str[0 .. string_len - 1] */ - symbol_offset += get_string_size(sym->symbol); + symbol_offset += get_string_size(comp_ctx, sym->symbol); symbol_offset = align_uint(symbol_offset, 2); sym = sym->next; } @@ -1748,9 +1782,10 @@ aot_emit_relocation_symbol_table(uint8 *buf, uint8 *buf_end, uint32 *p_offset, static bool aot_emit_relocation_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset, - AOTCompData *comp_data, AOTObjectData *obj_data) + AOTCompContext *comp_ctx, AOTCompData *comp_data, + AOTObjectData *obj_data) { - uint32 section_size = get_relocation_section_size(obj_data); + uint32 section_size = get_relocation_section_size(comp_ctx, obj_data); uint32 i, offset = *p_offset; AOTRelocationGroup *relocation_group = obj_data->relocation_groups; @@ -1762,7 +1797,7 @@ aot_emit_relocation_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset, EMIT_U32(AOT_SECTION_TYPE_RELOCATION); EMIT_U32(section_size); - aot_emit_relocation_symbol_table(buf, buf_end, &offset, comp_data, + aot_emit_relocation_symbol_table(buf, buf_end, &offset, comp_ctx, comp_data, obj_data); offset = align_uint(offset, 4); @@ -2679,9 +2714,10 @@ aot_emit_aot_file_buf(AOTCompContext *comp_ctx, AOTCompData *comp_data, comp_data, obj_data) || !aot_emit_text_section(buf, buf_end, &offset, comp_data, obj_data) || !aot_emit_func_section(buf, buf_end, &offset, comp_data, obj_data) - || !aot_emit_export_section(buf, buf_end, &offset, comp_data, obj_data) - || !aot_emit_relocation_section(buf, buf_end, &offset, comp_data, - obj_data) + || !aot_emit_export_section(buf, buf_end, &offset, comp_ctx, comp_data, + obj_data) + || !aot_emit_relocation_section(buf, buf_end, &offset, comp_ctx, + comp_data, obj_data) || !aot_emit_native_symbol(buf, buf_end, &offset, comp_ctx) || !aot_emit_name_section(buf, buf_end, &offset, comp_data, comp_ctx)) goto fail2; diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index 619b219f..b9a33c6c 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -351,7 +351,8 @@ check_utf8_str(const uint8 *str, uint32 len) static char * const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module, - char *error_buf, uint32 error_buf_size) + bool is_load_from_file_buf, char *error_buf, + uint32 error_buf_size) { StringNode *node, *node_next; @@ -360,6 +361,19 @@ const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module, return NULL; } + if (len == 0) { + return ""; + } + else if (is_load_from_file_buf) { + /* As the file buffer can be referred to after loading, we use + the previous byte of leb encoded size to adjust the string: + move string 1 byte backward and then append '\0' */ + char *c_str = (char *)str - 1; + bh_memmove_s(c_str, len + 1, c_str + 1, len); + c_str[len] = '\0'; + return c_str; + } + /* Search const str list */ node = module->const_str_list; while (node) { @@ -1540,7 +1554,8 @@ fail: static bool load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, - char *error_buf, uint32 error_buf_size) + bool is_load_from_file_buf, char *error_buf, + uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end, *p_old; uint32 import_count, name_len, type_index, i, u32, flags; @@ -1551,16 +1566,6 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, char *sub_module_name, *field_name; uint8 u8, kind; - /* insert builtin module names into const str list */ - if (!const_str_list_insert((uint8 *)"env", 3, module, error_buf, - error_buf_size) - || !const_str_list_insert((uint8 *)"wasi_unstable", 13, module, - error_buf, error_buf_size) - || !const_str_list_insert((uint8 *)"wasi_snapshot_preview1", 22, module, - error_buf, error_buf_size)) { - return false; - } - read_leb_uint32(p, p_end, import_count); if (import_count) { @@ -1665,7 +1670,8 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, read_leb_uint32(p, p_end, name_len); CHECK_BUF(p, p_end, name_len); if (!(sub_module_name = const_str_list_insert( - p, name_len, module, error_buf, error_buf_size))) { + p, name_len, module, is_load_from_file_buf, error_buf, + error_buf_size))) { return false; } p += name_len; @@ -1674,7 +1680,8 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, read_leb_uint32(p, p_end, name_len); CHECK_BUF(p, p_end, name_len); if (!(field_name = const_str_list_insert( - p, name_len, module, error_buf, error_buf_size))) { + p, name_len, module, is_load_from_file_buf, error_buf, + error_buf_size))) { return false; } p += name_len; @@ -2142,7 +2149,8 @@ fail: static bool load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, - char *error_buf, uint32 error_buf_size) + bool is_load_from_file_buf, char *error_buf, + uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; uint32 export_count, i, j, index; @@ -2176,7 +2184,8 @@ load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, } if (!(export->name = const_str_list_insert( - p, str_len, module, error_buf, error_buf_size))) { + p, str_len, module, is_load_from_file_buf, error_buf, + error_buf_size))) { return false; } @@ -2722,7 +2731,8 @@ fail: #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0 static bool handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, - char *error_buf, uint32 error_buf_size) + bool is_load_from_file_buf, char *error_buf, + uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; uint32 name_type, subsection_size; @@ -2786,9 +2796,10 @@ handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, return false; } if (!(module->functions[func_index]->field_name = - const_str_list_insert(p, func_name_len, - module, error_buf, - error_buf_size))) { + const_str_list_insert( + p, func_name_len, module, + is_load_from_file_buf, error_buf, + error_buf_size))) { return false; } } @@ -2814,7 +2825,8 @@ fail: static bool load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, - char *error_buf, uint32 error_buf_size) + bool is_load_from_file_buf, char *error_buf, + uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; uint32 name_len; @@ -2841,7 +2853,8 @@ load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, module->name_section_buf = buf; module->name_section_buf_end = buf_end; p += name_len; - handle_name_section(p, p_end, module, error_buf, error_buf_size); + handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf, + error_buf_size); } #endif LOG_VERBOSE("Load custom section success.\n"); @@ -2863,7 +2876,8 @@ static void **handle_table; #endif static bool -load_from_sections(WASMModule *module, WASMSection *sections, char *error_buf, +load_from_sections(WASMModule *module, WASMSection *sections, + bool is_load_from_file_buf, char *error_buf, uint32 error_buf_size) { WASMExport *export; @@ -2902,7 +2916,8 @@ load_from_sections(WASMModule *module, WASMSection *sections, char *error_buf, switch (section->section_type) { case SECTION_TYPE_USER: /* unsupported user section, ignore it. */ - if (!load_user_section(buf, buf_end, module, error_buf, + if (!load_user_section(buf, buf_end, module, + is_load_from_file_buf, error_buf, error_buf_size)) return false; break; @@ -2912,7 +2927,8 @@ load_from_sections(WASMModule *module, WASMSection *sections, char *error_buf, return false; break; case SECTION_TYPE_IMPORT: - if (!load_import_section(buf, buf_end, module, error_buf, + if (!load_import_section(buf, buf_end, module, + is_load_from_file_buf, error_buf, error_buf_size)) return false; break; @@ -2937,7 +2953,8 @@ load_from_sections(WASMModule *module, WASMSection *sections, char *error_buf, return false; break; case SECTION_TYPE_EXPORT: - if (!load_export_section(buf, buf_end, module, error_buf, + if (!load_export_section(buf, buf_end, module, + is_load_from_file_buf, error_buf, error_buf_size)) return false; break; @@ -3273,7 +3290,8 @@ wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf, if (!module) return NULL; - if (!load_from_sections(module, section_list, error_buf, error_buf_size)) { + if (!load_from_sections(module, section_list, false, error_buf, + error_buf_size)) { wasm_loader_unload(module); return NULL; } @@ -3437,7 +3455,7 @@ load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf, } if (!create_sections(buf, size, §ion_list, error_buf, error_buf_size) - || !load_from_sections(module, section_list, error_buf, + || !load_from_sections(module, section_list, true, error_buf, error_buf_size)) { destroy_sections(section_list); return false; diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c index f0a75a76..7441219c 100644 --- a/core/iwasm/interpreter/wasm_mini_loader.c +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -170,10 +170,24 @@ loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size) static char * const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module, - char *error_buf, uint32 error_buf_size) + bool is_load_from_file_buf, char *error_buf, + uint32 error_buf_size) { StringNode *node, *node_next; + if (len == 0) { + return ""; + } + else if (is_load_from_file_buf) { + /* As the file buffer can be referred to after loading, we use + the previous byte of leb encoded size to adjust the string: + move string 1 byte backward and then append '\0' */ + char *c_str = (char *)str - 1; + bh_memmove_s(c_str, len + 1, c_str + 1, len); + c_str[len] = '\0'; + return c_str; + } + /* Search const str list */ node = module->const_str_list; while (node) { @@ -640,7 +654,8 @@ load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory, static bool load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, - char *error_buf, uint32 error_buf_size) + bool is_load_from_file_buf, char *error_buf, + uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end, *p_old; uint32 import_count, name_len, type_index, i, u32, flags; @@ -651,16 +666,6 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, char *sub_module_name, *field_name; uint8 u8, kind; - /* insert builtin module names into const str list */ - if (!const_str_list_insert((uint8 *)"env", 3, module, error_buf, - error_buf_size) - || !const_str_list_insert((uint8 *)"wasi_unstable", 13, module, - error_buf, error_buf_size) - || !const_str_list_insert((uint8 *)"wasi_snapshot_preview1", 22, module, - error_buf, error_buf_size)) { - return false; - } - read_leb_uint32(p, p_end, import_count); if (import_count) { @@ -757,7 +762,8 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, read_leb_uint32(p, p_end, name_len); CHECK_BUF(p, p_end, name_len); if (!(sub_module_name = const_str_list_insert( - p, name_len, module, error_buf, error_buf_size))) { + p, name_len, module, is_load_from_file_buf, error_buf, + error_buf_size))) { return false; } p += name_len; @@ -766,7 +772,8 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, read_leb_uint32(p, p_end, name_len); CHECK_BUF(p, p_end, name_len); if (!(field_name = const_str_list_insert( - p, name_len, module, error_buf, error_buf_size))) { + p, name_len, module, is_load_from_file_buf, error_buf, + error_buf_size))) { return false; } p += name_len; @@ -1146,7 +1153,8 @@ load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, static bool load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, - char *error_buf, uint32 error_buf_size) + bool is_load_from_file_buf, char *error_buf, + uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; uint32 export_count, i, j, index; @@ -1177,7 +1185,8 @@ load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, } if (!(export->name = const_str_list_insert( - p, str_len, module, error_buf, error_buf_size))) { + p, str_len, module, is_load_from_file_buf, error_buf, + error_buf_size))) { return false; } @@ -1648,7 +1657,8 @@ load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0 static bool handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, - char *error_buf, uint32 error_buf_size) + bool is_load_from_file_buf, char *error_buf, + uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; uint32 name_type, subsection_size; @@ -1686,9 +1696,10 @@ handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, func_index -= module->import_count; bh_assert(func_index < module->function_count); if (!(module->functions[func_index]->field_name = - const_str_list_insert(p, func_name_len, - module, error_buf, - error_buf_size))) { + const_str_list_insert( + p, func_name_len, module, + is_load_from_file_buf, error_buf, + error_buf_size))) { return false; } } @@ -1707,14 +1718,13 @@ handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, } return true; -fail: - return false; } #endif static bool load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, - char *error_buf, uint32 error_buf_size) + bool is_load_from_file_buf, char *error_buf, + uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; uint32 name_len; @@ -1728,7 +1738,8 @@ load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0 if (memcmp(p, "name", 4) == 0) { p += name_len; - handle_name_section(p, p_end, module, error_buf, error_buf_size); + handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf, + error_buf_size); } #endif LOG_VERBOSE("Load custom section success.\n"); @@ -1785,7 +1796,8 @@ static void **handle_table; #endif static bool -load_from_sections(WASMModule *module, WASMSection *sections, char *error_buf, +load_from_sections(WASMModule *module, WASMSection *sections, + bool is_load_from_file_buf, char *error_buf, uint32 error_buf_size) { WASMExport *export; @@ -1821,7 +1833,8 @@ load_from_sections(WASMModule *module, WASMSection *sections, char *error_buf, switch (section->section_type) { case SECTION_TYPE_USER: /* unsupported user section, ignore it. */ - if (!load_user_section(buf, buf_end, module, error_buf, + if (!load_user_section(buf, buf_end, module, + is_load_from_file_buf, error_buf, error_buf_size)) return false; break; @@ -1831,7 +1844,8 @@ load_from_sections(WASMModule *module, WASMSection *sections, char *error_buf, return false; break; case SECTION_TYPE_IMPORT: - if (!load_import_section(buf, buf_end, module, error_buf, + if (!load_import_section(buf, buf_end, module, + is_load_from_file_buf, error_buf, error_buf_size)) return false; break; @@ -1856,7 +1870,8 @@ load_from_sections(WASMModule *module, WASMSection *sections, char *error_buf, return false; break; case SECTION_TYPE_EXPORT: - if (!load_export_section(buf, buf_end, module, error_buf, + if (!load_export_section(buf, buf_end, module, + is_load_from_file_buf, error_buf, error_buf_size)) return false; break; @@ -2174,7 +2189,8 @@ wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf, if (!module) return NULL; - if (!load_from_sections(module, section_list, error_buf, error_buf_size)) { + if (!load_from_sections(module, section_list, false, error_buf, + error_buf_size)) { wasm_loader_unload(module); return NULL; } @@ -2329,7 +2345,7 @@ load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf, } if (!create_sections(buf, size, §ion_list, error_buf, error_buf_size) - || !load_from_sections(module, section_list, error_buf, + || !load_from_sections(module, section_list, true, error_buf, error_buf_size)) { destroy_sections(section_list); return false;