Skip to content

Commit

Permalink
Add apis to get package version (#3601)
Browse files Browse the repository at this point in the history
Add three APIs:
- wasm_runtime_get_file_package_version
- wasm_runtime_get_module_package_version
- wasm_runtime_get_current_package_version
  • Loading branch information
bnason-nf authored Jul 16, 2024
1 parent 7affac0 commit 501d7d5
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 4 deletions.
2 changes: 2 additions & 0 deletions core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions core/iwasm/aot/aot_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
62 changes: 59 additions & 3 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
Expand Down
33 changes: 32 additions & 1 deletion core/iwasm/include/wasm_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions core/iwasm/interpreter/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions core/iwasm/interpreter/wasm_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit 501d7d5

Please sign in to comment.