From 501d7d5adf648edd438603decb071c7c8f64a9c4 Mon Sep 17 00:00:00 2001 From: Benbuck Nason Date: Mon, 15 Jul 2024 17:15:59 -0700 Subject: [PATCH] Add apis to get package version (#3601) Add three APIs: - wasm_runtime_get_file_package_version - wasm_runtime_get_module_package_version - wasm_runtime_get_current_package_version --- core/iwasm/aot/aot_loader.c | 2 + core/iwasm/aot/aot_runtime.h | 3 ++ core/iwasm/common/wasm_runtime_common.c | 62 +++++++++++++++++++++++-- core/iwasm/include/wasm_export.h | 33 ++++++++++++- core/iwasm/interpreter/wasm.h | 3 ++ core/iwasm/interpreter/wasm_loader.c | 2 + 6 files changed, 101 insertions(+), 4 deletions(-) diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index 0116ae7659..645f68b1d3 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -4168,6 +4168,8 @@ load(const uint8 *buf, uint32 size, AOTModule *module, return false; } + module->package_version = version; + if (!create_sections(module, buf, size, §ion_list, error_buf, error_buf_size)) return false; diff --git a/core/iwasm/aot/aot_runtime.h b/core/iwasm/aot/aot_runtime.h index 34d1babd14..e3704f8278 100644 --- a/core/iwasm/aot/aot_runtime.h +++ b/core/iwasm/aot/aot_runtime.h @@ -130,6 +130,9 @@ typedef struct LocalRefFlag { typedef struct AOTModule { uint32 module_type; + /* the package version read from the AOT file */ + uint32 package_version; + /* import memories */ uint32 import_memory_count; AOTImportMemory *import_memories; diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index dad2eb29bc..8032615399 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -858,11 +858,11 @@ wasm_runtime_set_default_running_mode(RunningMode running_mode) PackageType get_package_type(const uint8 *buf, uint32 size) { + if (buf && size >= 4) { #if (WASM_ENABLE_WORD_ALIGN_READ != 0) - uint32 buf32 = *(uint32 *)buf; - buf = (const uint8 *)&buf32; + uint32 buf32 = *(uint32 *)buf; + buf = (const uint8 *)&buf32; #endif - if (buf && size >= 4) { if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 's' && buf[3] == 'm') return Wasm_Module_Bytecode; if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 'o' && buf[3] == 't') @@ -887,6 +887,62 @@ wasm_runtime_get_module_package_type(WASMModuleCommon *module) return module->module_type; } +uint32 +wasm_runtime_get_file_package_version(const uint8 *buf, uint32 size) +{ + if (buf && size >= 8) { + uint32 version; +#if (WASM_ENABLE_WORD_ALIGN_READ != 0) + uint32 buf32 = *(uint32 *)(buf + sizeof(uint32)); + buf = (const uint8 *)&buf32; + version = buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24; +#else + version = buf[4] | buf[5] << 8 | buf[6] << 16 | buf[7] << 24; +#endif + return version; + } + + return 0; +} + +uint32 +wasm_runtime_get_module_package_version(WASMModuleCommon *module) +{ + if (!module) { + return 0; + } + +#if WASM_ENABLE_INTERP != 0 + if (module->module_type == Wasm_Module_Bytecode) { + WASMModule *wasm_module = (WASMModule *)module; + return wasm_module->package_version; + } +#endif + +#if WASM_ENABLE_AOT != 0 + if (module->module_type == Wasm_Module_AoT) { + AOTModule *aot_module = (AOTModule *)module; + return aot_module->package_version; + } +#endif + + return 0; +} + +uint32 +wasm_runtime_get_current_package_version(package_type_t package_type) +{ + switch (package_type) { + case Wasm_Module_Bytecode: + return WASM_CURRENT_VERSION; + case Wasm_Module_AoT: + return AOT_CURRENT_VERSION; + case Package_Type_Unknown: + default: + return 0; + } +} + #if WASM_ENABLE_AOT != 0 static uint8 * align_ptr(const uint8 *p, uint32 b) diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index 72f36b8191..569c4deaa7 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -442,7 +442,38 @@ wasm_runtime_get_file_package_type(const uint8_t *buf, uint32_t size); * unknown */ WASM_RUNTIME_API_EXTERN package_type_t -wasm_runtime_get_module_package_type(wasm_module_t module); +wasm_runtime_get_module_package_type(const wasm_module_t module); + +/** + * Get the package version of a buffer. + * + * @param buf the package buffer + * @param size the package buffer size + * + * @return the package version, return zero if the version is unknown + */ +WASM_RUNTIME_API_EXTERN uint32_t +wasm_runtime_get_file_package_version(const uint8_t *buf, uint32_t size); + +/** + * Get the package version of a module + * + * @param module the module + * + * @return the package version, or zero if version is unknown + */ +WASM_RUNTIME_API_EXTERN uint32_t +wasm_runtime_get_module_package_version(const wasm_module_t module); + +/** + * Get the currently supported version of the package type + * + * @param package_type the package type + * + * @return the currently supported version, or zero if package type is unknown + */ +WASM_RUNTIME_API_EXTERN uint32_t +wasm_runtime_get_current_package_version(package_type_t package_type); /** * Check whether a file is an AOT XIP (Execution In Place) file diff --git a/core/iwasm/interpreter/wasm.h b/core/iwasm/interpreter/wasm.h index 0755d10e0b..ea93adb034 100644 --- a/core/iwasm/interpreter/wasm.h +++ b/core/iwasm/interpreter/wasm.h @@ -836,6 +836,9 @@ struct WASMModule { AOTModule structure. */ uint32 module_type; + /* the package version read from the WASM file */ + uint32 package_version; + uint32 type_count; uint32 import_count; uint32 function_count; diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index ec40bbf345..785b28980c 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -6542,6 +6542,8 @@ load(const uint8 *buf, uint32 size, WASMModule *module, return false; } + module->package_version = version; + if (!create_sections(buf, size, §ion_list, error_buf, error_buf_size) || !load_from_sections(module, section_list, true, wasm_binary_freeable, error_buf, error_buf_size)) {