瀏覽代碼

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
Benbuck Nason 1 年之前
父節點
當前提交
501d7d5adf

+ 2 - 0
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, &section_list, error_buf,
                          error_buf_size))
         return false;

+ 3 - 0
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;

+ 59 - 3
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)

+ 32 - 1
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

+ 3 - 0
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;

+ 2 - 0
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, &section_list, error_buf, error_buf_size)
         || !load_from_sections(module, section_list, true, wasm_binary_freeable,
                                error_buf, error_buf_size)) {