Skip to content

Commit 501d7d5

Browse files
authored
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
1 parent 7affac0 commit 501d7d5

File tree

6 files changed

+101
-4
lines changed

6 files changed

+101
-4
lines changed

core/iwasm/aot/aot_loader.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4168,6 +4168,8 @@ load(const uint8 *buf, uint32 size, AOTModule *module,
41684168
return false;
41694169
}
41704170

4171+
module->package_version = version;
4172+
41714173
if (!create_sections(module, buf, size, &section_list, error_buf,
41724174
error_buf_size))
41734175
return false;

core/iwasm/aot/aot_runtime.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ typedef struct LocalRefFlag {
130130
typedef struct AOTModule {
131131
uint32 module_type;
132132

133+
/* the package version read from the AOT file */
134+
uint32 package_version;
135+
133136
/* import memories */
134137
uint32 import_memory_count;
135138
AOTImportMemory *import_memories;

core/iwasm/common/wasm_runtime_common.c

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -858,11 +858,11 @@ wasm_runtime_set_default_running_mode(RunningMode running_mode)
858858
PackageType
859859
get_package_type(const uint8 *buf, uint32 size)
860860
{
861+
if (buf && size >= 4) {
861862
#if (WASM_ENABLE_WORD_ALIGN_READ != 0)
862-
uint32 buf32 = *(uint32 *)buf;
863-
buf = (const uint8 *)&buf32;
863+
uint32 buf32 = *(uint32 *)buf;
864+
buf = (const uint8 *)&buf32;
864865
#endif
865-
if (buf && size >= 4) {
866866
if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 's' && buf[3] == 'm')
867867
return Wasm_Module_Bytecode;
868868
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)
887887
return module->module_type;
888888
}
889889

890+
uint32
891+
wasm_runtime_get_file_package_version(const uint8 *buf, uint32 size)
892+
{
893+
if (buf && size >= 8) {
894+
uint32 version;
895+
#if (WASM_ENABLE_WORD_ALIGN_READ != 0)
896+
uint32 buf32 = *(uint32 *)(buf + sizeof(uint32));
897+
buf = (const uint8 *)&buf32;
898+
version = buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;
899+
#else
900+
version = buf[4] | buf[5] << 8 | buf[6] << 16 | buf[7] << 24;
901+
#endif
902+
return version;
903+
}
904+
905+
return 0;
906+
}
907+
908+
uint32
909+
wasm_runtime_get_module_package_version(WASMModuleCommon *module)
910+
{
911+
if (!module) {
912+
return 0;
913+
}
914+
915+
#if WASM_ENABLE_INTERP != 0
916+
if (module->module_type == Wasm_Module_Bytecode) {
917+
WASMModule *wasm_module = (WASMModule *)module;
918+
return wasm_module->package_version;
919+
}
920+
#endif
921+
922+
#if WASM_ENABLE_AOT != 0
923+
if (module->module_type == Wasm_Module_AoT) {
924+
AOTModule *aot_module = (AOTModule *)module;
925+
return aot_module->package_version;
926+
}
927+
#endif
928+
929+
return 0;
930+
}
931+
932+
uint32
933+
wasm_runtime_get_current_package_version(package_type_t package_type)
934+
{
935+
switch (package_type) {
936+
case Wasm_Module_Bytecode:
937+
return WASM_CURRENT_VERSION;
938+
case Wasm_Module_AoT:
939+
return AOT_CURRENT_VERSION;
940+
case Package_Type_Unknown:
941+
default:
942+
return 0;
943+
}
944+
}
945+
890946
#if WASM_ENABLE_AOT != 0
891947
static uint8 *
892948
align_ptr(const uint8 *p, uint32 b)

core/iwasm/include/wasm_export.h

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,38 @@ wasm_runtime_get_file_package_type(const uint8_t *buf, uint32_t size);
442442
* unknown
443443
*/
444444
WASM_RUNTIME_API_EXTERN package_type_t
445-
wasm_runtime_get_module_package_type(wasm_module_t module);
445+
wasm_runtime_get_module_package_type(const wasm_module_t module);
446+
447+
/**
448+
* Get the package version of a buffer.
449+
*
450+
* @param buf the package buffer
451+
* @param size the package buffer size
452+
*
453+
* @return the package version, return zero if the version is unknown
454+
*/
455+
WASM_RUNTIME_API_EXTERN uint32_t
456+
wasm_runtime_get_file_package_version(const uint8_t *buf, uint32_t size);
457+
458+
/**
459+
* Get the package version of a module
460+
*
461+
* @param module the module
462+
*
463+
* @return the package version, or zero if version is unknown
464+
*/
465+
WASM_RUNTIME_API_EXTERN uint32_t
466+
wasm_runtime_get_module_package_version(const wasm_module_t module);
467+
468+
/**
469+
* Get the currently supported version of the package type
470+
*
471+
* @param package_type the package type
472+
*
473+
* @return the currently supported version, or zero if package type is unknown
474+
*/
475+
WASM_RUNTIME_API_EXTERN uint32_t
476+
wasm_runtime_get_current_package_version(package_type_t package_type);
446477

447478
/**
448479
* Check whether a file is an AOT XIP (Execution In Place) file

core/iwasm/interpreter/wasm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,9 @@ struct WASMModule {
836836
AOTModule structure. */
837837
uint32 module_type;
838838

839+
/* the package version read from the WASM file */
840+
uint32 package_version;
841+
839842
uint32 type_count;
840843
uint32 import_count;
841844
uint32 function_count;

core/iwasm/interpreter/wasm_loader.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6542,6 +6542,8 @@ load(const uint8 *buf, uint32 size, WASMModule *module,
65426542
return false;
65436543
}
65446544

6545+
module->package_version = version;
6546+
65456547
if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
65466548
|| !load_from_sections(module, section_list, true, wasm_binary_freeable,
65476549
error_buf, error_buf_size)) {

0 commit comments

Comments
 (0)