From 41f75267277b8cd44cc0c37cba26e64ab56cfb2a Mon Sep 17 00:00:00 2001 From: "liang.he@intel.com" Date: Wed, 6 Nov 2024 11:45:02 +0000 Subject: [PATCH] refactor wasm_table_inst_t and add functions to retrieve export type by name --- core/iwasm/aot/aot_runtime.c | 3 + core/iwasm/common/wasm_runtime_common.c | 266 +++++++++++++++--------- core/iwasm/include/wasm_export.h | 50 +++-- 3 files changed, 202 insertions(+), 117 deletions(-) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 01e04a3ec2..b6a0492235 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -694,6 +694,8 @@ tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module, tbl_inst->elem_ref_type.elem_ref_type = module->tables[i].table_type.elem_ref_type; #endif + tbl_inst->is_table64 = + import_table->table_type.flags & TABLE64_FLAG; } else { AOTTable *table = module->tables + (i - module->import_table_count); @@ -704,6 +706,7 @@ tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module, tbl_inst->elem_ref_type.elem_ref_type = module->tables[i].table_type.elem_ref_type; #endif + tbl_inst->is_table64 = table->table_type.flags & TABLE64_FLAG; } /* Set all elements to -1 or NULL_REF to mark them as uninitialized diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index fb16aa0f3c..e0bdc55fd1 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -2095,62 +2095,65 @@ wasm_runtime_get_export_global_inst(WASMModuleInstanceCommon *const module_inst, return false; } -bool +WASMTableInstance * wasm_runtime_get_export_table_inst(WASMModuleInstanceCommon *const module_inst, - char const *name, - wasm_table_inst_t *table_inst) + const char *name) { + if (!module_inst || !name) { + return NULL; + } + #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) { const WASMModuleInstance *wasm_module_inst = (const WASMModuleInstance *)module_inst; const WASMModule *wasm_module = wasm_module_inst->module; - uint32 i; - for (i = 0; i < wasm_module->export_count; i++) { + + for (uint32 i = 0; i < wasm_module->export_count; i++) { const WASMExport *wasm_export = &wasm_module->exports[i]; - if ((wasm_export->kind == WASM_IMPORT_EXPORT_KIND_TABLE) - && !strcmp(wasm_export->name, name)) { - const WASMTableInstance *wasm_table_inst = - wasm_module_inst->tables[wasm_export->index]; - table_inst->elem_kind = - val_type_to_val_kind(wasm_table_inst->elem_type); - table_inst->cur_size = wasm_table_inst->cur_size; - table_inst->max_size = wasm_table_inst->max_size; - table_inst->elems = (void *)wasm_table_inst->elems; - return true; - } + + if (wasm_export->kind != WASM_IMPORT_EXPORT_KIND_TABLE) + continue; + + if (strcmp(wasm_export->name, name)) + continue; + + return wasm_module_inst->tables[wasm_export->index]; } + + return NULL; } #endif + #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) { const AOTModuleInstance *aot_module_inst = (AOTModuleInstance *)module_inst; const AOTModule *aot_module = (AOTModule *)aot_module_inst->module; - uint32 i; - for (i = 0; i < aot_module->export_count; i++) { + + for (uint32 i = 0; i < aot_module->export_count; i++) { const AOTExport *aot_export = &aot_module->exports[i]; - if ((aot_export->kind == WASM_IMPORT_EXPORT_KIND_TABLE) - && !strcmp(aot_export->name, name)) { - const AOTTableInstance *aot_table_inst = - aot_module_inst->tables[aot_export->index]; - table_inst->elem_kind = - val_type_to_val_kind(aot_table_inst->elem_type); - table_inst->cur_size = aot_table_inst->cur_size; - table_inst->max_size = aot_table_inst->max_size; - table_inst->elems = (void *)aot_table_inst->elems; - return true; - } + + if (aot_export->kind == WASM_IMPORT_EXPORT_KIND_TABLE) + continue; + + if (strcmp(aot_export->name, name)) + continue; + + return aot_module_inst->tables[aot_export->index]; } + + return NULL; } #endif - return false; + LOG_ERROR("Unsupported module type: %d", module_inst->module_type); + return NULL; } WASMFunctionInstanceCommon * wasm_table_get_func_inst(struct WASMModuleInstanceCommon *const module_inst, - const wasm_table_inst_t *table_inst, uint32_t idx) + WASMTableInstance *const table_inst, uint32_t idx) { if (!table_inst) { bh_assert(0); @@ -4230,6 +4233,84 @@ wasm_runtime_get_export_count(WASMModuleCommon *const module) return -1; } +#if WASM_ENABLE_AOT != 0 +static void +populate_export_type_from_aot(const AOTModule *module, const AOTExport *export, + wasm_export_t *out) +{ + if (!module || !export || !out) + return; + + out->name = export->name; + out->kind = export->kind; + + switch (out->kind) { + case WASM_IMPORT_EXPORT_KIND_FUNC: + out->u.func_type = + (AOTFuncType *)module + ->types[module->func_type_indexes + [export->index - module->import_func_count]]; + break; + case WASM_IMPORT_EXPORT_KIND_GLOBAL: + out->u.global_type = + &module->globals[export->index - module->import_global_count] + .type; + break; + case WASM_IMPORT_EXPORT_KIND_TABLE: + out->u.table_type = + &module->tables[export->index - module->import_table_count] + .table_type; + break; + case WASM_IMPORT_EXPORT_KIND_MEMORY: + out->u.memory_type = + &module->memories[export->index - module->import_memory_count]; + break; + default: + bh_assert(0); + return; + } +} +#endif + +#if WASM_ENABLE_INTERP != 0 +static void +populate_export_type_from_wasm(const WASMModule *module, + const WASMExport *export, wasm_export_t *out) +{ + if (!out || !module || !export) { + return; + } + + out->name = export->name; + out->kind = export->kind; + + switch (out->kind) { + case WASM_IMPORT_EXPORT_KIND_FUNC: + out->u.func_type = + module->functions[export->index - module->import_function_count] + ->func_type; + break; + case WASM_IMPORT_EXPORT_KIND_GLOBAL: + out->u.global_type = + &module->globals[export->index - module->import_global_count] + .type; + break; + case WASM_IMPORT_EXPORT_KIND_TABLE: + out->u.table_type = + &module->tables[export->index - module->import_table_count] + .table_type; + break; + case WASM_IMPORT_EXPORT_KIND_MEMORY: + out->u.memory_type = + &module->memories[export->index - module->import_memory_count]; + break; + default: + bh_assert(0); + return; + } +} +#endif + void wasm_runtime_get_export_type(WASMModuleCommon *const module, int32 export_index, wasm_export_t *export_type) @@ -4256,39 +4337,7 @@ wasm_runtime_get_export_type(WASMModuleCommon *const module, int32 export_index, } const AOTExport *aot_export = &aot_module->exports[export_index]; - export_type->name = aot_export->name; - export_type->kind = aot_export->kind; - switch (export_type->kind) { - case WASM_IMPORT_EXPORT_KIND_FUNC: - export_type->u.func_type = - (AOTFuncType *)aot_module - ->types[aot_module->func_type_indexes - [aot_export->index - - aot_module->import_func_count]]; - break; - case WASM_IMPORT_EXPORT_KIND_GLOBAL: - export_type->u.global_type = - &aot_module - ->globals[aot_export->index - - aot_module->import_global_count] - .type; - break; - case WASM_IMPORT_EXPORT_KIND_TABLE: - export_type->u.table_type = - &aot_module - ->tables[aot_export->index - - aot_module->import_table_count] - .table_type; - break; - case WASM_IMPORT_EXPORT_KIND_MEMORY: - export_type->u.memory_type = - &aot_module->memories[aot_export->index - - aot_module->import_memory_count]; - break; - default: - bh_assert(0); - break; - } + populate_export_type_from_aot(aot_module, aot_export, export_type); return; } #endif @@ -4302,41 +4351,62 @@ wasm_runtime_get_export_type(WASMModuleCommon *const module, int32 export_index, } const WASMExport *wasm_export = &wasm_module->exports[export_index]; - export_type->name = wasm_export->name; - export_type->kind = wasm_export->kind; - switch (export_type->kind) { - case WASM_IMPORT_EXPORT_KIND_FUNC: - export_type->u.func_type = - wasm_module - ->functions[wasm_export->index - - wasm_module->import_function_count] - ->func_type; - break; - case WASM_IMPORT_EXPORT_KIND_GLOBAL: - export_type->u.global_type = - &wasm_module - ->globals[wasm_export->index - - wasm_module->import_global_count] - .type; - break; - case WASM_IMPORT_EXPORT_KIND_TABLE: - export_type->u.table_type = - &wasm_module - ->tables[wasm_export->index - - wasm_module->import_table_count] - .table_type; - break; - case WASM_IMPORT_EXPORT_KIND_MEMORY: - export_type->u.memory_type = - &wasm_module->memories[wasm_export->index - - wasm_module->import_memory_count]; - break; - default: - bh_assert(0); - break; - } + populate_export_type_from_wasm(wasm_module, wasm_export, export_type); + return; + } +#endif +} + +void +wasm_runtime_get_export_type_by_name(WASMModuleCommon *const module, + const char *name, + wasm_export_t *export_type) +{ + if (!export_type) { + bh_assert(0); + return; + } + + memset(export_type, 0, sizeof(wasm_export_t)); + + if (!module) { + bh_assert(0); return; } + +#if WASM_ENABLE_INTERP != 0 + if (module->module_type == Wasm_Module_Bytecode) { + const WASMModule *wasm_module = (const WASMModule *)module; + + for (uint32 i = 0; i < wasm_module->export_count; i++) { + WASMExport *wasm_export = &wasm_module->exports[i]; + + if (strcmp(name, wasm_export->name)) { + continue; + } + + populate_export_type_from_wasm(wasm_module, wasm_export, + export_type); + return; + } + } +#endif + +#if WASM_ENABLE_AOT != 0 + if (module->module_type == Wasm_Module_AoT) { + const AOTModule *aot_module = (const AOTModule *)module; + + for (uint32 i = 0; i < aot_module->export_count; i++) { + AOTExport *aot_export = &aot_module->exports[i]; + + if (strcmp(name, aot_export->name)) { + continue; + } + + populate_export_type_from_aot(aot_module, aot_export, export_type); + return; + } + } #endif } @@ -4479,7 +4549,7 @@ wasm_table_type_get_shared(WASMTableType *const table_type) { bh_assert(table_type); - return (table_type->flags & 2) ? true : false; + return (table_type->flags & SHARED_TABLE_FLAG) ? true : false; } uint32 diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index aa6cfaae75..962edee638 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -124,6 +124,10 @@ typedef WASMFunctionInstanceCommon *wasm_function_inst_t; struct WASMMemoryInstance; typedef struct WASMMemoryInstance *wasm_memory_inst_t; +/* Table instance */ +struct WASMTableInstance; +typedef struct WASMTableInstance *wasm_table_inst_t; + /* WASM section */ typedef struct wasm_section_t { struct wasm_section_t *next; @@ -315,15 +319,6 @@ typedef struct wasm_global_inst_t { void *global_data; } wasm_global_inst_t; -/* Table instance*/ -typedef struct wasm_table_inst_t { - wasm_valkind_t elem_kind; - uint32_t cur_size; - uint32_t max_size; - /* represents the elements of the table, for internal use only */ - void *elems; -} wasm_table_inst_t; - typedef enum { WASM_LOG_LEVEL_FATAL = 0, WASM_LOG_LEVEL_ERROR = 1, @@ -1478,6 +1473,23 @@ WASM_RUNTIME_API_EXTERN void wasm_runtime_get_export_type(const wasm_module_t module, int32_t export_index, wasm_export_t *export_type); +/** + * @brief Retrieves the export type of a given name from the specified WASM + * module. + * + * This function searches for an export with the specified name within the + * provided WebAssembly module and returns its type information. + * + * @param module The WebAssembly module to search within. + * @param name The name of the export to find. + * @param export_type A pointer to a wasm_export_t structure where the export + * type information will be stored if found. + */ +WASM_RUNTIME_API_EXTERN void +wasm_runtime_get_export_type_by_name(const wasm_module_t module, + const char *name, + wasm_export_t *export_type); + /** * Get the number of parameters for a function type * @@ -1699,19 +1711,19 @@ wasm_runtime_get_export_global_inst(const wasm_module_inst_t module_inst, wasm_global_inst_t *global_inst); /** - * Get an export table instance - * - * @param module_inst the module instance - * @param name the export table name - * @param table_inst location to store the table instance + * @brief Retrieves the table instance exported from a WebAssembly module + * instance. * - * @return true if success, false otherwise + * This function looks up a table instance by its export name from the given + * WebAssembly module instance. * + * @param module_inst The WebAssembly module instance. + * @param name The name of the exported table. + * @return The table instance if found, otherwise NULL. */ -WASM_RUNTIME_API_EXTERN bool +WASM_RUNTIME_API_EXTERN wasm_table_inst_t wasm_runtime_get_export_table_inst(const wasm_module_inst_t module_inst, - const char *name, - wasm_table_inst_t *table_inst); + const char *name); /** * Get a function instance from a table. @@ -1724,7 +1736,7 @@ wasm_runtime_get_export_table_inst(const wasm_module_inst_t module_inst, */ WASM_RUNTIME_API_EXTERN wasm_function_inst_t wasm_table_get_func_inst(const wasm_module_inst_t module_inst, - const wasm_table_inst_t *table_inst, uint32_t idx); + const wasm_table_inst_t table_inst, uint32_t idx); /** * Get attachment of native function from execution environment