2019-05-07 02:18:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
2019-11-11 23:45:21 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-05-07 02:18:18 +00:00
|
|
|
*/
|
|
|
|
|
2019-05-17 09:15:25 +00:00
|
|
|
#include "wasm_loader.h"
|
Enable AoT and wamr-sdk, and change arguments of call wasm API (#157)
* Implement memory profiler, optimize memory usage, modify code indent
* Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default
* Add a new extension library: connection
* Fix bug of reading magic number and version in big endian platform
* Re-org platform APIs: move most platform APIs from iwasm to shared-lib
* Enhance wasm loader to fix some security issues
* Fix issue about illegal load of EXC_RETURN into PC on stm32 board
* Updates that let a restricted version of the interpreter run in SGX
* Enable native/app address validation and conversion for wasm app
* Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused
* Refine binary size and fix several minor issues
Optimize interpreter LOAD/STORE opcodes to decrease the binary size
Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found
Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper
Add macros of global heap size, stack size, heap size for Zephyr main.c
Clear compile warning of wasm_application.c
* Add more strict security checks for libc wrapper API's
* Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files
* Enhance security of libc strcpy/sprintf wrapper function
* Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions
* Remove get_module_inst() and fix issue of call native
* Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate
* Refine interpreter call native process, refine memory boudary check
* Fix issues of invokeNative function of arm/mips/general version
* Add a switch to build simple sample without gui support
* Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code
* Re-org shared lib header files, remove unused info; fix compile issues of vxworks
* Add build target general
* Remove unused files
* Update license header
* test push
* Restore file
* Sync up with internal/feature
* Sync up with internal/feature
* Rename build_wamr_app to build_wasm_app
* Fix small issues of README
* Enhance malformed wasm file checking
Fix issue of print hex int and implement utf8 string check
Fix wasi file read/write right issue
Fix minor issue of build wasm app doc
* Sync up with internal/feature
* Sync up with internal/feature: fix interpreter arm issue, fix read leb issue
* Sync up with internal/feature
* Fix bug of config.h and rename wasi config.h to ssp_config.h
* Sync up with internal/feature
* Import wamr aot
* update document
* update document
* Update document, disable WASI in 32bit
* update document
* remove files
* update document
* Update document
* update document
* update document
* update samples
* Sync up with internal repo
2020-01-21 05:26:14 +00:00
|
|
|
#include "bh_common.h"
|
|
|
|
#include "bh_log.h"
|
2019-05-07 02:18:18 +00:00
|
|
|
#include "wasm.h"
|
2019-05-17 09:15:25 +00:00
|
|
|
#include "wasm_opcode.h"
|
|
|
|
#include "wasm_runtime.h"
|
Enable AoT and wamr-sdk, and change arguments of call wasm API (#157)
* Implement memory profiler, optimize memory usage, modify code indent
* Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default
* Add a new extension library: connection
* Fix bug of reading magic number and version in big endian platform
* Re-org platform APIs: move most platform APIs from iwasm to shared-lib
* Enhance wasm loader to fix some security issues
* Fix issue about illegal load of EXC_RETURN into PC on stm32 board
* Updates that let a restricted version of the interpreter run in SGX
* Enable native/app address validation and conversion for wasm app
* Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused
* Refine binary size and fix several minor issues
Optimize interpreter LOAD/STORE opcodes to decrease the binary size
Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found
Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper
Add macros of global heap size, stack size, heap size for Zephyr main.c
Clear compile warning of wasm_application.c
* Add more strict security checks for libc wrapper API's
* Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files
* Enhance security of libc strcpy/sprintf wrapper function
* Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions
* Remove get_module_inst() and fix issue of call native
* Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate
* Refine interpreter call native process, refine memory boudary check
* Fix issues of invokeNative function of arm/mips/general version
* Add a switch to build simple sample without gui support
* Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code
* Re-org shared lib header files, remove unused info; fix compile issues of vxworks
* Add build target general
* Remove unused files
* Update license header
* test push
* Restore file
* Sync up with internal/feature
* Sync up with internal/feature
* Rename build_wamr_app to build_wasm_app
* Fix small issues of README
* Enhance malformed wasm file checking
Fix issue of print hex int and implement utf8 string check
Fix wasi file read/write right issue
Fix minor issue of build wasm app doc
* Sync up with internal/feature
* Sync up with internal/feature: fix interpreter arm issue, fix read leb issue
* Sync up with internal/feature
* Fix bug of config.h and rename wasi config.h to ssp_config.h
* Sync up with internal/feature
* Import wamr aot
* update document
* update document
* Update document, disable WASI in 32bit
* update document
* remove files
* update document
* Update document
* update document
* update document
* update samples
* Sync up with internal repo
2020-01-21 05:26:14 +00:00
|
|
|
#include "../common/wasm_native.h"
|
2022-05-07 08:09:16 +00:00
|
|
|
#include "../common/wasm_memory.h"
|
2021-09-29 05:36:46 +00:00
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0
|
|
|
|
#include "../libraries/debug-engine/debug_engine.h"
|
|
|
|
#endif
|
2022-08-02 08:03:50 +00:00
|
|
|
#if WASM_ENABLE_FAST_JIT != 0
|
|
|
|
#include "../fast-jit/jit_compiler.h"
|
|
|
|
#include "../fast-jit/jit_codecache.h"
|
|
|
|
#endif
|
2022-10-18 02:59:28 +00:00
|
|
|
#if WASM_ENABLE_JIT != 0
|
|
|
|
#include "../compilation/aot_llvm.h"
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
/* Read a value of given type from the address pointed to by the given
|
|
|
|
pointer and increase the pointer to the position just after the
|
|
|
|
value being read. */
|
2021-10-08 03:44:39 +00:00
|
|
|
#define TEMPLATE_READ_VALUE(Type, p) \
|
2019-05-07 02:18:18 +00:00
|
|
|
(p += sizeof(Type), *(Type *)(p - sizeof(Type)))
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
|
|
|
|
{
|
2020-08-21 07:11:31 +00:00
|
|
|
if (error_buf != NULL) {
|
2021-10-08 03:44:39 +00:00
|
|
|
snprintf(error_buf, error_buf_size, "WASM module load failed: %s",
|
|
|
|
string);
|
2020-08-21 07:11:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-10-08 03:44:39 +00:00
|
|
|
set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
|
2020-08-21 07:11:31 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char buf[128];
|
|
|
|
|
|
|
|
if (error_buf != NULL) {
|
|
|
|
va_start(args, format);
|
|
|
|
vsnprintf(buf, sizeof(buf), format, args);
|
|
|
|
va_end(args);
|
2021-10-08 03:44:39 +00:00
|
|
|
snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
|
2020-08-21 07:11:31 +00:00
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 03:19:09 +00:00
|
|
|
static bool
|
|
|
|
check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
2022-01-18 03:05:58 +00:00
|
|
|
if ((uintptr_t)buf + length < (uintptr_t)buf
|
|
|
|
|| (uintptr_t)buf + length > (uintptr_t)buf_end) {
|
2020-06-08 03:19:09 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"unexpected end of section or function");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
2022-01-18 03:05:58 +00:00
|
|
|
if ((uintptr_t)buf + length < (uintptr_t)buf
|
|
|
|
|| (uintptr_t)buf + length > (uintptr_t)buf_end) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "unexpected end");
|
2020-06-08 03:19:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
#define CHECK_BUF(buf, buf_end, length) \
|
|
|
|
do { \
|
|
|
|
if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
|
|
|
|
goto fail; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2019-11-27 02:52:12 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
#define CHECK_BUF1(buf, buf_end, length) \
|
|
|
|
do { \
|
|
|
|
if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
|
|
|
|
goto fail; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-12-28 02:46:31 +00:00
|
|
|
#define skip_leb(p) while (*p++ & 0x80)
|
|
|
|
#define skip_leb_int64(p, p_end) skip_leb(p)
|
|
|
|
#define skip_leb_uint32(p, p_end) skip_leb(p)
|
|
|
|
#define skip_leb_int32(p, p_end) skip_leb(p)
|
2020-02-13 07:51:22 +00:00
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
|
|
|
|
uint64 *p_result, char *error_buf, uint32 error_buf_size)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
2020-05-15 09:44:36 +00:00
|
|
|
const uint8 *buf = *p_buf;
|
2019-05-07 02:18:18 +00:00
|
|
|
uint64 result = 0;
|
|
|
|
uint32 shift = 0;
|
2020-05-15 09:44:36 +00:00
|
|
|
uint32 offset = 0, bcnt = 0;
|
2019-05-07 02:18:18 +00:00
|
|
|
uint64 byte;
|
|
|
|
|
|
|
|
while (true) {
|
2021-04-15 03:29:20 +00:00
|
|
|
/* uN or SN must not exceed ceil(N/7) bytes */
|
2020-01-17 05:38:21 +00:00
|
|
|
if (bcnt + 1 > (maxbits + 6) / 7) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"integer representation too long");
|
|
|
|
return false;
|
|
|
|
}
|
Enable AoT and wamr-sdk, and change arguments of call wasm API (#157)
* Implement memory profiler, optimize memory usage, modify code indent
* Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default
* Add a new extension library: connection
* Fix bug of reading magic number and version in big endian platform
* Re-org platform APIs: move most platform APIs from iwasm to shared-lib
* Enhance wasm loader to fix some security issues
* Fix issue about illegal load of EXC_RETURN into PC on stm32 board
* Updates that let a restricted version of the interpreter run in SGX
* Enable native/app address validation and conversion for wasm app
* Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused
* Refine binary size and fix several minor issues
Optimize interpreter LOAD/STORE opcodes to decrease the binary size
Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found
Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper
Add macros of global heap size, stack size, heap size for Zephyr main.c
Clear compile warning of wasm_application.c
* Add more strict security checks for libc wrapper API's
* Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files
* Enhance security of libc strcpy/sprintf wrapper function
* Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions
* Remove get_module_inst() and fix issue of call native
* Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate
* Refine interpreter call native process, refine memory boudary check
* Fix issues of invokeNative function of arm/mips/general version
* Add a switch to build simple sample without gui support
* Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code
* Re-org shared lib header files, remove unused info; fix compile issues of vxworks
* Add build target general
* Remove unused files
* Update license header
* test push
* Restore file
* Sync up with internal/feature
* Sync up with internal/feature
* Rename build_wamr_app to build_wasm_app
* Fix small issues of README
* Enhance malformed wasm file checking
Fix issue of print hex int and implement utf8 string check
Fix wasi file read/write right issue
Fix minor issue of build wasm app doc
* Sync up with internal/feature
* Sync up with internal/feature: fix interpreter arm issue, fix read leb issue
* Sync up with internal/feature
* Fix bug of config.h and rename wasi config.h to ssp_config.h
* Sync up with internal/feature
* Import wamr aot
* update document
* update document
* Update document, disable WASI in 32bit
* update document
* remove files
* update document
* Update document
* update document
* update document
* update samples
* Sync up with internal repo
2020-01-21 05:26:14 +00:00
|
|
|
|
2020-05-15 09:44:36 +00:00
|
|
|
CHECK_BUF(buf, buf_end, offset + 1);
|
|
|
|
byte = buf[offset];
|
|
|
|
offset += 1;
|
2019-05-07 02:18:18 +00:00
|
|
|
result |= ((byte & 0x7f) << shift);
|
|
|
|
shift += 7;
|
2019-10-11 07:25:23 +00:00
|
|
|
bcnt += 1;
|
2019-05-07 02:18:18 +00:00
|
|
|
if ((byte & 0x80) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-12-13 07:30:30 +00:00
|
|
|
|
|
|
|
if (!sign && maxbits == 32 && shift >= maxbits) {
|
|
|
|
/* The top bits set represent values > 32 bits */
|
|
|
|
if (((uint8)byte) & 0xf0)
|
|
|
|
goto fail_integer_too_large;
|
|
|
|
}
|
|
|
|
else if (sign && maxbits == 32) {
|
|
|
|
if (shift < maxbits) {
|
2021-07-30 07:21:17 +00:00
|
|
|
/* Sign extend, second highest bit is the sign bit */
|
|
|
|
if ((uint8)byte & 0x40)
|
|
|
|
result |= (~((uint64)0)) << shift;
|
2019-12-13 07:30:30 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* The top bits should be a sign-extension of the sign bit */
|
|
|
|
bool sign_bit_set = ((uint8)byte) & 0x8;
|
|
|
|
int top_bits = ((uint8)byte) & 0xf0;
|
|
|
|
if ((sign_bit_set && top_bits != 0x70)
|
|
|
|
|| (!sign_bit_set && top_bits != 0))
|
|
|
|
goto fail_integer_too_large;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (sign && maxbits == 64) {
|
|
|
|
if (shift < maxbits) {
|
2021-07-30 07:21:17 +00:00
|
|
|
/* Sign extend, second highest bit is the sign bit */
|
|
|
|
if ((uint8)byte & 0x40)
|
|
|
|
result |= (~((uint64)0)) << shift;
|
2019-12-13 07:30:30 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* The top bits should be a sign-extension of the sign bit */
|
|
|
|
bool sign_bit_set = ((uint8)byte) & 0x1;
|
|
|
|
int top_bits = ((uint8)byte) & 0xfe;
|
|
|
|
|
|
|
|
if ((sign_bit_set && top_bits != 0x7e)
|
|
|
|
|| (!sign_bit_set && top_bits != 0))
|
|
|
|
goto fail_integer_too_large;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
2019-12-13 07:30:30 +00:00
|
|
|
|
2020-05-15 09:44:36 +00:00
|
|
|
*p_buf += offset;
|
2019-05-07 02:18:18 +00:00
|
|
|
*p_result = result;
|
|
|
|
return true;
|
2019-12-13 07:30:30 +00:00
|
|
|
|
|
|
|
fail_integer_too_large:
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "integer too large");
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
2019-12-13 07:30:30 +00:00
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
#define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
|
2019-05-07 02:18:18 +00:00
|
|
|
#define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
|
2021-10-08 03:44:39 +00:00
|
|
|
|
|
|
|
#define read_leb_int64(p, p_end, res) \
|
|
|
|
do { \
|
|
|
|
uint64 res64; \
|
|
|
|
if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
|
|
|
|
error_buf_size)) \
|
|
|
|
goto fail; \
|
|
|
|
res = (int64)res64; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define read_leb_uint32(p, p_end, res) \
|
|
|
|
do { \
|
|
|
|
uint64 res64; \
|
|
|
|
if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
|
|
|
|
error_buf_size)) \
|
|
|
|
goto fail; \
|
|
|
|
res = (uint32)res64; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define read_leb_int32(p, p_end, res) \
|
|
|
|
do { \
|
|
|
|
uint64 res64; \
|
|
|
|
if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
|
|
|
|
error_buf_size)) \
|
|
|
|
goto fail; \
|
|
|
|
res = (int32)res64; \
|
|
|
|
} while (0)
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
static char *
|
|
|
|
type2str(uint8 type)
|
|
|
|
{
|
|
|
|
char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
|
|
|
|
|
|
|
|
if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
|
|
|
|
return type_str[type - VALUE_TYPE_V128];
|
|
|
|
else if (type == VALUE_TYPE_FUNCREF)
|
|
|
|
return "funcref";
|
|
|
|
else if (type == VALUE_TYPE_EXTERNREF)
|
|
|
|
return "externref";
|
|
|
|
else
|
|
|
|
return "unknown type";
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
is_32bit_type(uint8 type)
|
|
|
|
{
|
|
|
|
if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32
|
|
|
|
#if WASM_ENABLE_REF_TYPES != 0
|
2021-11-18 09:42:49 +00:00
|
|
|
|| type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
|
2021-04-15 03:29:20 +00:00
|
|
|
#endif
|
|
|
|
)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
is_64bit_type(uint8 type)
|
|
|
|
{
|
|
|
|
if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
is_value_type(uint8 type)
|
|
|
|
{
|
|
|
|
if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_I64
|
|
|
|
|| type == VALUE_TYPE_F32 || type == VALUE_TYPE_F64
|
|
|
|
#if WASM_ENABLE_REF_TYPES != 0
|
2021-11-18 09:42:49 +00:00
|
|
|
|| type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
|
2021-04-15 03:29:20 +00:00
|
|
|
#endif
|
|
|
|
#if WASM_ENABLE_SIMD != 0
|
|
|
|
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
|
|
|
|
|| type == VALUE_TYPE_V128
|
|
|
|
#endif
|
|
|
|
#endif
|
2021-10-08 03:44:39 +00:00
|
|
|
)
|
2021-04-15 03:29:20 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
is_byte_a_type(uint8 type)
|
|
|
|
{
|
|
|
|
return is_value_type(type) || (type == VALUE_TYPE_VOID);
|
|
|
|
}
|
|
|
|
|
2020-11-05 10:15:15 +00:00
|
|
|
#if WASM_ENABLE_SIMD != 0
|
|
|
|
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
|
|
|
|
static V128
|
2021-10-08 03:44:39 +00:00
|
|
|
read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
|
2020-11-05 10:15:15 +00:00
|
|
|
{
|
|
|
|
V128 result;
|
|
|
|
uint8 i;
|
|
|
|
|
|
|
|
for (i = 0; i != 16; ++i) {
|
|
|
|
result.i8x16[i] = read_uint8(p_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
|
|
|
|
#endif /* end of WASM_ENABLE_SIMD */
|
|
|
|
|
2020-06-08 03:19:09 +00:00
|
|
|
static void *
|
|
|
|
loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
|
|
|
void *mem;
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
|
|
|
|
set_error_buf(error_buf, error_buf_size, "allocate memory failed");
|
2020-06-08 03:19:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(mem, 0, (uint32)size);
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
2019-11-27 02:52:12 +00:00
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
check_utf8_str(const uint8 *str, uint32 len)
|
2019-11-27 02:52:12 +00:00
|
|
|
{
|
2020-09-18 10:06:13 +00:00
|
|
|
/* The valid ranges are taken from page 125, below link
|
|
|
|
https://www.unicode.org/versions/Unicode9.0.0/ch03.pdf */
|
|
|
|
const uint8 *p = str, *p_end = str + len;
|
|
|
|
uint8 chr;
|
2019-11-27 02:52:12 +00:00
|
|
|
|
|
|
|
while (p < p_end) {
|
2020-09-18 10:06:13 +00:00
|
|
|
chr = *p;
|
|
|
|
if (chr < 0x80) {
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
else if (chr >= 0xC2 && chr <= 0xDF && p + 1 < p_end) {
|
|
|
|
if (p[1] < 0x80 || p[1] > 0xBF) {
|
2019-11-27 02:52:12 +00:00
|
|
|
return false;
|
2020-09-18 10:06:13 +00:00
|
|
|
}
|
|
|
|
p += 2;
|
|
|
|
}
|
|
|
|
else if (chr >= 0xE0 && chr <= 0xEF && p + 2 < p_end) {
|
|
|
|
if (chr == 0xE0) {
|
2021-10-08 03:44:39 +00:00
|
|
|
if (p[1] < 0xA0 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
|
2019-11-27 02:52:12 +00:00
|
|
|
return false;
|
2020-09-18 10:06:13 +00:00
|
|
|
}
|
2019-11-27 02:52:12 +00:00
|
|
|
}
|
2020-09-18 10:06:13 +00:00
|
|
|
else if (chr == 0xED) {
|
2021-10-08 03:44:39 +00:00
|
|
|
if (p[1] < 0x80 || p[1] > 0x9F || p[2] < 0x80 || p[2] > 0xBF) {
|
2020-09-18 10:06:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (chr >= 0xE1 && chr <= 0xEF) {
|
2021-10-08 03:44:39 +00:00
|
|
|
if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
|
2020-09-18 10:06:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p += 3;
|
|
|
|
}
|
|
|
|
else if (chr >= 0xF0 && chr <= 0xF4 && p + 3 < p_end) {
|
|
|
|
if (chr == 0xF0) {
|
2021-10-08 03:44:39 +00:00
|
|
|
if (p[1] < 0x90 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
|
2020-09-18 10:06:13 +00:00
|
|
|
|| p[3] < 0x80 || p[3] > 0xBF) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (chr >= 0xF1 && chr <= 0xF3) {
|
2021-10-08 03:44:39 +00:00
|
|
|
if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
|
2020-09-18 10:06:13 +00:00
|
|
|
|| p[3] < 0x80 || p[3] > 0xBF) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (chr == 0xF4) {
|
2021-10-08 03:44:39 +00:00
|
|
|
if (p[1] < 0x80 || p[1] > 0x8F || p[2] < 0x80 || p[2] > 0xBF
|
2020-09-18 10:06:13 +00:00
|
|
|
|| p[3] < 0x80 || p[3] > 0xBF) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p += 4;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
2019-11-27 02:52:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-18 10:06:13 +00:00
|
|
|
return (p == p_end);
|
2019-11-27 02:52:12 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
static char *
|
2020-02-18 07:15:24 +00:00
|
|
|
const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module,
|
2021-11-08 03:27:26 +00:00
|
|
|
bool is_load_from_file_buf, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
2020-02-18 07:15:24 +00:00
|
|
|
StringNode *node, *node_next;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2019-11-27 02:52:12 +00:00
|
|
|
if (!check_utf8_str(str, len)) {
|
2021-10-08 03:44:39 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
|
2019-11-27 02:52:12 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-11-08 03:27:26 +00:00
|
|
|
if (len == 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
else if (is_load_from_file_buf) {
|
|
|
|
/* As the file buffer can be referred to after loading, we use
|
|
|
|
the previous byte of leb encoded size to adjust the string:
|
|
|
|
move string 1 byte backward and then append '\0' */
|
|
|
|
char *c_str = (char *)str - 1;
|
|
|
|
bh_memmove_s(c_str, len + 1, c_str + 1, len);
|
|
|
|
c_str[len] = '\0';
|
|
|
|
return c_str;
|
|
|
|
}
|
|
|
|
|
2020-02-18 07:15:24 +00:00
|
|
|
/* Search const str list */
|
|
|
|
node = module->const_str_list;
|
|
|
|
while (node) {
|
|
|
|
node_next = node->next;
|
2021-10-08 03:44:39 +00:00
|
|
|
if (strlen(node->str) == len && !memcmp(node->str, str, len))
|
2020-02-18 07:15:24 +00:00
|
|
|
break;
|
|
|
|
node = node_next;
|
|
|
|
}
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
if (node) {
|
2020-02-18 07:15:24 +00:00
|
|
|
return node->str;
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
2020-02-18 07:15:24 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!(node = loader_malloc(sizeof(StringNode) + len + 1, error_buf,
|
|
|
|
error_buf_size))) {
|
2019-05-07 02:18:18 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
node->str = ((char *)node) + sizeof(StringNode);
|
2020-02-18 07:15:24 +00:00
|
|
|
bh_memcpy_s(node->str, len + 1, str, len);
|
|
|
|
node->str[len] = '\0';
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-02-18 07:15:24 +00:00
|
|
|
if (!module->const_str_list) {
|
|
|
|
/* set as head */
|
|
|
|
module->const_str_list = node;
|
|
|
|
node->next = NULL;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
2020-02-18 07:15:24 +00:00
|
|
|
else {
|
|
|
|
/* insert it */
|
|
|
|
node->next = module->const_str_list;
|
|
|
|
module->const_str_list = node;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-18 07:15:24 +00:00
|
|
|
return node->str;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 09:52:02 +00:00
|
|
|
static void
|
|
|
|
destroy_wasm_type(WASMType *type)
|
|
|
|
{
|
|
|
|
if (type->ref_count > 1) {
|
|
|
|
/* The type is referenced by other types
|
|
|
|
of current wasm module */
|
|
|
|
type->ref_count--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-19 03:24:46 +00:00
|
|
|
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
|
|
|
|
&& WASM_ENABLE_LAZY_JIT != 0
|
|
|
|
if (type->call_to_llvm_jit_from_fast_jit)
|
|
|
|
jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
|
|
|
|
#endif
|
|
|
|
|
2022-08-18 09:52:02 +00:00
|
|
|
wasm_runtime_free(type);
|
|
|
|
}
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
static bool
|
|
|
|
load_init_expr(const uint8 **p_buf, const uint8 *buf_end,
|
2021-10-08 03:44:39 +00:00
|
|
|
InitializerExpression *init_expr, uint8 type, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
|
|
|
const uint8 *p = *p_buf, *p_end = buf_end;
|
|
|
|
uint8 flag, end_byte, *p_float;
|
|
|
|
uint32 i;
|
|
|
|
|
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
init_expr->init_expr_type = read_uint8(p);
|
|
|
|
flag = init_expr->init_expr_type;
|
|
|
|
|
|
|
|
switch (flag) {
|
|
|
|
/* i32.const */
|
|
|
|
case INIT_EXPR_TYPE_I32_CONST:
|
2020-04-30 09:52:11 +00:00
|
|
|
if (type != VALUE_TYPE_I32)
|
2020-08-20 04:43:12 +00:00
|
|
|
goto fail_type_mismatch;
|
2019-05-07 02:18:18 +00:00
|
|
|
read_leb_int32(p, p_end, init_expr->u.i32);
|
|
|
|
break;
|
2019-12-18 03:53:43 +00:00
|
|
|
/* i64.const */
|
2019-05-07 02:18:18 +00:00
|
|
|
case INIT_EXPR_TYPE_I64_CONST:
|
2020-04-30 09:52:11 +00:00
|
|
|
if (type != VALUE_TYPE_I64)
|
2020-08-20 04:43:12 +00:00
|
|
|
goto fail_type_mismatch;
|
2019-05-07 02:18:18 +00:00
|
|
|
read_leb_int64(p, p_end, init_expr->u.i64);
|
|
|
|
break;
|
2019-12-18 03:53:43 +00:00
|
|
|
/* f32.const */
|
2019-05-07 02:18:18 +00:00
|
|
|
case INIT_EXPR_TYPE_F32_CONST:
|
2020-04-30 09:52:11 +00:00
|
|
|
if (type != VALUE_TYPE_F32)
|
2020-08-20 04:43:12 +00:00
|
|
|
goto fail_type_mismatch;
|
2019-05-07 02:18:18 +00:00
|
|
|
CHECK_BUF(p, p_end, 4);
|
2021-10-08 03:44:39 +00:00
|
|
|
p_float = (uint8 *)&init_expr->u.f32;
|
2019-05-07 02:18:18 +00:00
|
|
|
for (i = 0; i < sizeof(float32); i++)
|
|
|
|
*p_float++ = *p++;
|
|
|
|
break;
|
2019-12-18 03:53:43 +00:00
|
|
|
/* f64.const */
|
2019-05-07 02:18:18 +00:00
|
|
|
case INIT_EXPR_TYPE_F64_CONST:
|
2020-04-30 09:52:11 +00:00
|
|
|
if (type != VALUE_TYPE_F64)
|
2020-08-20 04:43:12 +00:00
|
|
|
goto fail_type_mismatch;
|
2019-05-07 02:18:18 +00:00
|
|
|
CHECK_BUF(p, p_end, 8);
|
2021-10-08 03:44:39 +00:00
|
|
|
p_float = (uint8 *)&init_expr->u.f64;
|
2019-05-07 02:18:18 +00:00
|
|
|
for (i = 0; i < sizeof(float64); i++)
|
|
|
|
*p_float++ = *p++;
|
|
|
|
break;
|
2020-11-05 10:15:15 +00:00
|
|
|
#if WASM_ENABLE_SIMD != 0
|
|
|
|
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
|
|
|
|
case INIT_EXPR_TYPE_V128_CONST:
|
|
|
|
{
|
|
|
|
uint64 high, low;
|
|
|
|
|
|
|
|
if (type != VALUE_TYPE_V128)
|
2021-04-15 03:29:20 +00:00
|
|
|
goto fail_type_mismatch;
|
2020-11-05 10:15:15 +00:00
|
|
|
|
2023-11-09 00:42:05 +00:00
|
|
|
CHECK_BUF(p, p_end, 1);
|
2020-11-05 10:15:15 +00:00
|
|
|
flag = read_uint8(p);
|
|
|
|
(void)flag;
|
|
|
|
|
|
|
|
CHECK_BUF(p, p_end, 16);
|
2021-10-08 03:44:39 +00:00
|
|
|
wasm_runtime_read_v128(p, &high, &low);
|
2020-11-05 10:15:15 +00:00
|
|
|
p += 16;
|
|
|
|
|
|
|
|
init_expr->u.v128.i64x2[0] = high;
|
|
|
|
init_expr->u.v128.i64x2[1] = low;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
|
|
|
|
#endif /* end of WASM_ENABLE_SIMD */
|
2021-04-15 03:29:20 +00:00
|
|
|
#if WASM_ENABLE_REF_TYPES != 0
|
|
|
|
case INIT_EXPR_TYPE_FUNCREF_CONST:
|
|
|
|
{
|
|
|
|
if (type != VALUE_TYPE_FUNCREF)
|
|
|
|
goto fail_type_mismatch;
|
|
|
|
read_leb_uint32(p, p_end, init_expr->u.ref_index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case INIT_EXPR_TYPE_REFNULL_CONST:
|
|
|
|
{
|
|
|
|
uint8 reftype;
|
|
|
|
|
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
reftype = read_uint8(p);
|
|
|
|
if (reftype != type)
|
|
|
|
goto fail_type_mismatch;
|
|
|
|
|
|
|
|
init_expr->u.ref_index = NULL_REF;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif /* WASM_ENABLE_REF_TYPES != 0 */
|
2019-12-18 03:53:43 +00:00
|
|
|
/* get_global */
|
2019-05-07 02:18:18 +00:00
|
|
|
case INIT_EXPR_TYPE_GET_GLOBAL:
|
|
|
|
read_leb_uint32(p, p_end, init_expr->u.global_index);
|
|
|
|
break;
|
2021-10-08 03:44:39 +00:00
|
|
|
default:
|
|
|
|
{
|
2021-04-15 03:29:20 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"illegal opcode "
|
|
|
|
"or constant expression required "
|
|
|
|
"or type mismatch");
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
end_byte = read_uint8(p);
|
2020-04-30 09:52:11 +00:00
|
|
|
if (end_byte != 0x0b)
|
2020-08-20 04:43:12 +00:00
|
|
|
goto fail_type_mismatch;
|
2019-05-07 02:18:18 +00:00
|
|
|
*p_buf = p;
|
|
|
|
return true;
|
2021-04-15 03:29:20 +00:00
|
|
|
|
2020-08-20 04:43:12 +00:00
|
|
|
fail_type_mismatch:
|
2020-04-30 09:52:11 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2020-08-21 07:11:31 +00:00
|
|
|
"type mismatch or constant expression required");
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
2020-04-30 09:52:11 +00:00
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
|
|
|
const uint8 *p = buf, *p_end = buf_end, *p_org;
|
|
|
|
uint32 type_count, param_count, result_count, i, j;
|
2020-07-10 08:29:15 +00:00
|
|
|
uint32 param_cell_num, ret_cell_num;
|
2019-11-20 13:16:36 +00:00
|
|
|
uint64 total_size;
|
2019-05-07 02:18:18 +00:00
|
|
|
uint8 flag;
|
|
|
|
WASMType *type;
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, type_count);
|
|
|
|
|
|
|
|
if (type_count) {
|
|
|
|
module->type_count = type_count;
|
2021-10-08 03:44:39 +00:00
|
|
|
total_size = sizeof(WASMType *) * (uint64)type_count;
|
|
|
|
if (!(module->types =
|
|
|
|
loader_malloc(total_size, error_buf, error_buf_size))) {
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < type_count; i++) {
|
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
flag = read_uint8(p);
|
|
|
|
if (flag != 0x60) {
|
2021-10-08 03:44:39 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "invalid type flag");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, param_count);
|
|
|
|
|
|
|
|
/* Resolve param count and result count firstly */
|
|
|
|
p_org = p;
|
|
|
|
CHECK_BUF(p, p_end, param_count);
|
|
|
|
p += param_count;
|
|
|
|
read_leb_uint32(p, p_end, result_count);
|
2020-07-10 08:29:15 +00:00
|
|
|
CHECK_BUF(p, p_end, result_count);
|
|
|
|
p = p_org;
|
|
|
|
|
|
|
|
if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
|
2019-08-08 08:53:56 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2020-07-10 08:29:15 +00:00
|
|
|
"param count or result count too large");
|
2019-08-08 08:53:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
total_size = offsetof(WASMType, types)
|
|
|
|
+ sizeof(uint8) * (uint64)(param_count + result_count);
|
2020-06-08 03:19:09 +00:00
|
|
|
if (!(type = module->types[i] =
|
2021-10-08 03:44:39 +00:00
|
|
|
loader_malloc(total_size, error_buf, error_buf_size))) {
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
2019-08-08 08:53:56 +00:00
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
/* Resolve param types and result types */
|
2022-08-18 09:52:02 +00:00
|
|
|
type->ref_count = 1;
|
2020-07-10 08:29:15 +00:00
|
|
|
type->param_count = (uint16)param_count;
|
|
|
|
type->result_count = (uint16)result_count;
|
2019-05-07 02:18:18 +00:00
|
|
|
for (j = 0; j < param_count; j++) {
|
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
type->types[j] = read_uint8(p);
|
|
|
|
}
|
|
|
|
read_leb_uint32(p, p_end, result_count);
|
|
|
|
for (j = 0; j < result_count; j++) {
|
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
type->types[param_count + j] = read_uint8(p);
|
|
|
|
}
|
2021-12-10 10:13:17 +00:00
|
|
|
for (j = 0; j < param_count + result_count; j++) {
|
|
|
|
if (!is_value_type(type->types[j])) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"unknown value type");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-07-10 08:29:15 +00:00
|
|
|
|
|
|
|
param_cell_num = wasm_get_cell_num(type->types, param_count);
|
2021-10-08 03:44:39 +00:00
|
|
|
ret_cell_num =
|
|
|
|
wasm_get_cell_num(type->types + param_count, result_count);
|
2020-07-10 08:29:15 +00:00
|
|
|
if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"param count or result count too large");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
type->param_cell_num = (uint16)param_cell_num;
|
|
|
|
type->ret_cell_num = (uint16)ret_cell_num;
|
2022-08-18 09:52:02 +00:00
|
|
|
|
|
|
|
/* If there is already a same type created, use it instead */
|
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
if (wasm_type_equal(type, module->types[j])) {
|
|
|
|
if (module->types[j]->ref_count == UINT16_MAX) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"wasm type's ref count too large");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
destroy_wasm_type(type);
|
|
|
|
module->types[i] = module->types[j];
|
|
|
|
module->types[j]->ref_count++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p != p_end) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "section size mismatch");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_VERBOSE("Load type section success.\n");
|
|
|
|
return true;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
static void
|
|
|
|
adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
|
|
|
|
{
|
|
|
|
uint32 default_max_size =
|
2021-10-08 03:44:39 +00:00
|
|
|
init_size * 2 > TABLE_MAX_SIZE ? init_size * 2 : TABLE_MAX_SIZE;
|
2021-04-15 03:29:20 +00:00
|
|
|
|
|
|
|
if (max_size_flag) {
|
|
|
|
/* module defines the table limitation */
|
|
|
|
bh_assert(init_size <= *max_size);
|
|
|
|
|
|
|
|
if (init_size < *max_size) {
|
|
|
|
*max_size =
|
2021-10-08 03:44:39 +00:00
|
|
|
*max_size < default_max_size ? *max_size : default_max_size;
|
2021-04-15 03:29:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* partial defined table limitation, gives a default value */
|
|
|
|
*max_size = default_max_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 04:59:17 +00:00
|
|
|
#if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
|
2020-06-02 06:53:06 +00:00
|
|
|
/**
|
|
|
|
* Find export item of a module with export info:
|
|
|
|
* module name, field name and export kind
|
|
|
|
*/
|
|
|
|
static WASMExport *
|
2021-10-08 03:44:39 +00:00
|
|
|
wasm_loader_find_export(const WASMModule *module, const char *module_name,
|
|
|
|
const char *field_name, uint8 export_kind,
|
2021-12-29 03:04:36 +00:00
|
|
|
char *error_buf, uint32 error_buf_size)
|
2020-06-02 06:53:06 +00:00
|
|
|
{
|
2023-09-28 00:56:11 +00:00
|
|
|
WASMExport *export =
|
|
|
|
loader_find_export((WASMModuleCommon *)module, module_name, field_name,
|
|
|
|
export_kind, error_buf, error_buf_size);
|
|
|
|
;
|
2020-06-02 06:53:06 +00:00
|
|
|
return export;
|
|
|
|
}
|
2022-12-27 04:59:17 +00:00
|
|
|
#endif
|
2020-06-02 06:53:06 +00:00
|
|
|
|
2022-12-27 04:59:17 +00:00
|
|
|
#if WASM_ENABLE_MULTI_MODULE != 0
|
2020-06-02 06:53:06 +00:00
|
|
|
static WASMFunction *
|
2021-10-08 03:44:39 +00:00
|
|
|
wasm_loader_resolve_function(const char *module_name, const char *function_name,
|
2020-06-02 06:53:06 +00:00
|
|
|
const WASMType *expected_function_type,
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
|
|
|
WASMModuleCommon *module_reg;
|
|
|
|
WASMFunction *function = NULL;
|
|
|
|
WASMExport *export = NULL;
|
|
|
|
WASMModule *module = NULL;
|
|
|
|
WASMType *target_function_type = NULL;
|
|
|
|
|
|
|
|
module_reg = wasm_runtime_find_module_registered(module_name);
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
|
|
|
|
LOG_DEBUG("can not find a module named %s for function %s", module_name,
|
|
|
|
function_name);
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "unknown import");
|
2020-06-02 06:53:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
module = (WASMModule *)module_reg;
|
2021-12-29 03:04:36 +00:00
|
|
|
export =
|
|
|
|
wasm_loader_find_export(module, module_name, function_name,
|
|
|
|
EXPORT_KIND_FUNC, error_buf, error_buf_size);
|
2020-06-02 06:53:06 +00:00
|
|
|
if (!export) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-02-19 03:14:40 +00:00
|
|
|
/* resolve function type and function */
|
2020-06-02 06:53:06 +00:00
|
|
|
if (export->index < module->import_function_count) {
|
|
|
|
target_function_type =
|
2021-10-08 03:44:39 +00:00
|
|
|
module->import_functions[export->index].u.function.func_type;
|
2020-06-02 06:53:06 +00:00
|
|
|
function = module->import_functions[export->index]
|
2021-10-08 03:44:39 +00:00
|
|
|
.u.function.import_func_linked;
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
target_function_type =
|
2021-10-08 03:44:39 +00:00
|
|
|
module->functions[export->index - module->import_function_count]
|
|
|
|
->func_type;
|
2020-06-02 06:53:06 +00:00
|
|
|
function =
|
2021-10-08 03:44:39 +00:00
|
|
|
module->functions[export->index - module->import_function_count];
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 03:14:40 +00:00
|
|
|
/* check function type */
|
2020-06-02 06:53:06 +00:00
|
|
|
if (!wasm_type_equal(expected_function_type, target_function_type)) {
|
|
|
|
LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "incompatible import type");
|
2020-06-02 06:53:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return function;
|
|
|
|
}
|
|
|
|
|
|
|
|
static WASMTable *
|
|
|
|
wasm_loader_resolve_table(const char *module_name, const char *table_name,
|
2021-10-08 03:44:39 +00:00
|
|
|
uint32 init_size, uint32 max_size, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2020-06-02 06:53:06 +00:00
|
|
|
{
|
|
|
|
WASMModuleCommon *module_reg;
|
|
|
|
WASMTable *table = NULL;
|
|
|
|
WASMExport *export = NULL;
|
|
|
|
WASMModule *module = NULL;
|
|
|
|
|
|
|
|
module_reg = wasm_runtime_find_module_registered(module_name);
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
|
2020-06-02 06:53:06 +00:00
|
|
|
LOG_DEBUG("can not find a module named %s for table", module_name);
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "unknown import");
|
2020-06-02 06:53:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
module = (WASMModule *)module_reg;
|
2021-12-29 03:04:36 +00:00
|
|
|
export =
|
|
|
|
wasm_loader_find_export(module, module_name, table_name,
|
|
|
|
EXPORT_KIND_TABLE, error_buf, error_buf_size);
|
2020-06-02 06:53:06 +00:00
|
|
|
if (!export) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-02-19 03:14:40 +00:00
|
|
|
/* resolve table and check the init/max size */
|
2020-06-02 06:53:06 +00:00
|
|
|
if (export->index < module->import_table_count) {
|
|
|
|
table =
|
2021-10-08 03:44:39 +00:00
|
|
|
module->import_tables[export->index].u.table.import_table_linked;
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
table = &(module->tables[export->index - module->import_table_count]);
|
|
|
|
}
|
|
|
|
if (table->init_size < init_size || table->max_size > max_size) {
|
|
|
|
LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
|
|
|
|
module_name, table_name, table->init_size, table->max_size,
|
|
|
|
init_size, max_size);
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "incompatible import type");
|
2020-06-02 06:53:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
|
|
|
static WASMMemory *
|
|
|
|
wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
|
|
|
|
uint32 init_page_count, uint32 max_page_count,
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
|
|
|
WASMModuleCommon *module_reg;
|
|
|
|
WASMMemory *memory = NULL;
|
|
|
|
WASMExport *export = NULL;
|
|
|
|
WASMModule *module = NULL;
|
|
|
|
|
|
|
|
module_reg = wasm_runtime_find_module_registered(module_name);
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
|
2020-06-02 06:53:06 +00:00
|
|
|
LOG_DEBUG("can not find a module named %s for memory", module_name);
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "unknown import");
|
2020-06-02 06:53:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
module = (WASMModule *)module_reg;
|
2021-12-29 03:04:36 +00:00
|
|
|
export =
|
|
|
|
wasm_loader_find_export(module, module_name, memory_name,
|
|
|
|
EXPORT_KIND_MEMORY, error_buf, error_buf_size);
|
2020-06-02 06:53:06 +00:00
|
|
|
if (!export) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-02-19 03:14:40 +00:00
|
|
|
/* resolve memory and check the init/max page count */
|
2020-06-02 06:53:06 +00:00
|
|
|
if (export->index < module->import_memory_count) {
|
2021-10-08 03:44:39 +00:00
|
|
|
memory = module->import_memories[export->index]
|
|
|
|
.u.memory.import_memory_linked;
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
memory =
|
2021-10-08 03:44:39 +00:00
|
|
|
&(module->memories[export->index - module->import_memory_count]);
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
2021-10-08 03:44:39 +00:00
|
|
|
if (memory->init_page_count < init_page_count
|
|
|
|
|| memory->max_page_count > max_page_count) {
|
2020-06-02 06:53:06 +00:00
|
|
|
LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
|
|
|
|
module_name, memory_name, memory->init_page_count,
|
|
|
|
memory->max_page_count, init_page_count, max_page_count);
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "incompatible import type");
|
2020-06-02 06:53:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return memory;
|
|
|
|
}
|
|
|
|
|
|
|
|
static WASMGlobal *
|
2021-10-08 03:44:39 +00:00
|
|
|
wasm_loader_resolve_global(const char *module_name, const char *global_name,
|
|
|
|
uint8 type, bool is_mutable, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2020-06-02 06:53:06 +00:00
|
|
|
{
|
|
|
|
WASMModuleCommon *module_reg;
|
|
|
|
WASMGlobal *global = NULL;
|
|
|
|
WASMExport *export = NULL;
|
|
|
|
WASMModule *module = NULL;
|
|
|
|
|
|
|
|
module_reg = wasm_runtime_find_module_registered(module_name);
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
|
2020-06-02 06:53:06 +00:00
|
|
|
LOG_DEBUG("can not find a module named %s for global", module_name);
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "unknown import");
|
2020-06-02 06:53:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
module = (WASMModule *)module_reg;
|
2021-12-29 03:04:36 +00:00
|
|
|
export =
|
|
|
|
wasm_loader_find_export(module, module_name, global_name,
|
|
|
|
EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
|
2020-06-02 06:53:06 +00:00
|
|
|
if (!export) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-02-19 03:14:40 +00:00
|
|
|
/* resolve and check the global */
|
2020-06-02 06:53:06 +00:00
|
|
|
if (export->index < module->import_global_count) {
|
|
|
|
global =
|
2021-10-08 03:44:39 +00:00
|
|
|
module->import_globals[export->index].u.global.import_global_linked;
|
2021-04-15 03:29:20 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-06-02 06:53:06 +00:00
|
|
|
global =
|
2021-10-08 03:44:39 +00:00
|
|
|
&(module->globals[export->index - module->import_global_count]);
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
|
|
|
if (global->type != type || global->is_mutable != is_mutable) {
|
|
|
|
LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
|
|
|
|
module_name, global_name, global->type, global->is_mutable,
|
|
|
|
type, is_mutable);
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "incompatible import type");
|
2020-06-02 06:53:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return global;
|
|
|
|
}
|
2021-02-19 03:14:40 +00:00
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
#endif /* end of WASM_ENABLE_MULTI_MODULE */
|
|
|
|
|
|
|
|
static bool
|
2021-02-19 03:14:40 +00:00
|
|
|
load_function_import(const uint8 **p_buf, const uint8 *buf_end,
|
|
|
|
const WASMModule *parent_module,
|
2021-10-08 03:44:39 +00:00
|
|
|
const char *sub_module_name, const char *function_name,
|
|
|
|
WASMFunctionImport *function, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2020-06-02 06:53:06 +00:00
|
|
|
{
|
|
|
|
const uint8 *p = *p_buf, *p_end = buf_end;
|
|
|
|
uint32 declare_type_index = 0;
|
|
|
|
WASMType *declare_func_type = NULL;
|
|
|
|
WASMFunction *linked_func = NULL;
|
2021-02-19 03:14:40 +00:00
|
|
|
#if WASM_ENABLE_MULTI_MODULE != 0
|
|
|
|
WASMModule *sub_module = NULL;
|
|
|
|
#endif
|
2020-06-02 06:53:06 +00:00
|
|
|
const char *linked_signature = NULL;
|
|
|
|
void *linked_attachment = NULL;
|
|
|
|
bool linked_call_conv_raw = false;
|
2021-02-19 03:14:40 +00:00
|
|
|
bool is_native_symbol = false;
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
read_leb_uint32(p, p_end, declare_type_index);
|
|
|
|
*p_buf = p;
|
|
|
|
|
|
|
|
if (declare_type_index >= parent_module->type_count) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "unknown type");
|
2020-06-02 06:53:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-05 10:05:30 +00:00
|
|
|
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
|
|
|
|
declare_type_index = wasm_get_smallest_type_idx(
|
2021-10-08 03:44:39 +00:00
|
|
|
parent_module->types, parent_module->type_count, declare_type_index);
|
2021-01-05 10:05:30 +00:00
|
|
|
#endif
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
declare_func_type = parent_module->types[declare_type_index];
|
|
|
|
|
2021-02-19 03:14:40 +00:00
|
|
|
/* lookup registered native symbols first */
|
2021-10-08 03:44:39 +00:00
|
|
|
linked_func = wasm_native_resolve_symbol(
|
|
|
|
sub_module_name, function_name, declare_func_type, &linked_signature,
|
|
|
|
&linked_attachment, &linked_call_conv_raw);
|
2021-02-19 03:14:40 +00:00
|
|
|
if (linked_func) {
|
|
|
|
is_native_symbol = true;
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
|
|
|
#if WASM_ENABLE_MULTI_MODULE != 0
|
|
|
|
else {
|
2021-02-19 03:14:40 +00:00
|
|
|
if (!wasm_runtime_is_built_in_module(sub_module_name)) {
|
2023-09-28 00:56:11 +00:00
|
|
|
sub_module = (WASMModule *)wasm_runtime_load_depended_module(
|
|
|
|
(WASMModuleCommon *)parent_module, sub_module_name, error_buf,
|
|
|
|
error_buf_size);
|
2021-02-19 03:14:40 +00:00
|
|
|
if (!sub_module) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-10-08 03:44:39 +00:00
|
|
|
linked_func = wasm_loader_resolve_function(
|
|
|
|
sub_module_name, function_name, declare_func_type, error_buf,
|
|
|
|
error_buf_size);
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-02-19 03:14:40 +00:00
|
|
|
function->module_name = (char *)sub_module_name;
|
|
|
|
function->field_name = (char *)function_name;
|
2020-06-02 06:53:06 +00:00
|
|
|
function->func_type = declare_func_type;
|
2021-02-19 03:14:40 +00:00
|
|
|
/* func_ptr_linked is for native registered symbol */
|
|
|
|
function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
|
2020-06-02 06:53:06 +00:00
|
|
|
function->signature = linked_signature;
|
|
|
|
function->attachment = linked_attachment;
|
|
|
|
function->call_conv_raw = linked_call_conv_raw;
|
|
|
|
#if WASM_ENABLE_MULTI_MODULE != 0
|
2021-02-19 03:14:40 +00:00
|
|
|
function->import_module = is_native_symbol ? NULL : sub_module;
|
|
|
|
function->import_func_linked = is_native_symbol ? NULL : linked_func;
|
2020-06-02 06:53:06 +00:00
|
|
|
#endif
|
|
|
|
return true;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 04:38:59 +00:00
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2020-05-08 04:38:59 +00:00
|
|
|
{
|
|
|
|
if (max_size < init_size) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"size minimum must not be greater than maximum");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
static bool
|
2021-02-19 03:14:40 +00:00
|
|
|
load_table_import(const uint8 **p_buf, const uint8 *buf_end,
|
2021-10-08 03:44:39 +00:00
|
|
|
WASMModule *parent_module, const char *sub_module_name,
|
|
|
|
const char *table_name, WASMTableImport *table,
|
2019-05-07 02:18:18 +00:00
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
|
|
|
const uint8 *p = *p_buf, *p_end = buf_end;
|
2021-04-15 03:29:20 +00:00
|
|
|
uint32 declare_elem_type = 0, declare_max_size_flag = 0,
|
|
|
|
declare_init_size = 0, declare_max_size = 0;
|
2020-06-02 06:53:06 +00:00
|
|
|
#if WASM_ENABLE_MULTI_MODULE != 0
|
2021-02-19 03:14:40 +00:00
|
|
|
WASMModule *sub_module = NULL;
|
2020-06-02 06:53:06 +00:00
|
|
|
WASMTable *linked_table = NULL;
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2019-12-13 07:30:30 +00:00
|
|
|
CHECK_BUF(p, p_end, 1);
|
2021-04-15 03:29:20 +00:00
|
|
|
/* 0x70 or 0x6F */
|
2020-06-02 06:53:06 +00:00
|
|
|
declare_elem_type = read_uint8(p);
|
2021-04-15 03:29:20 +00:00
|
|
|
if (VALUE_TYPE_FUNCREF != declare_elem_type
|
|
|
|
#if WASM_ENABLE_REF_TYPES != 0
|
2021-11-18 09:42:49 +00:00
|
|
|
&& VALUE_TYPE_EXTERNREF != declare_elem_type
|
2021-04-15 03:29:20 +00:00
|
|
|
#endif
|
|
|
|
) {
|
2020-06-02 06:53:06 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "incompatible import type");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, declare_max_size_flag);
|
2021-04-15 03:29:20 +00:00
|
|
|
if (declare_max_size_flag > 1) {
|
|
|
|
set_error_buf(error_buf, error_buf_size, "integer too large");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
read_leb_uint32(p, p_end, declare_init_size);
|
2021-04-15 03:29:20 +00:00
|
|
|
|
|
|
|
if (declare_max_size_flag) {
|
2020-06-02 06:53:06 +00:00
|
|
|
read_leb_uint32(p, p_end, declare_max_size);
|
2021-04-15 03:29:20 +00:00
|
|
|
if (!check_table_max_size(declare_init_size, declare_max_size,
|
2020-05-08 04:38:59 +00:00
|
|
|
error_buf, error_buf_size))
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-15 03:29:20 +00:00
|
|
|
|
|
|
|
adjust_table_max_size(declare_init_size, declare_max_size_flag,
|
|
|
|
&declare_max_size);
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
*p_buf = p;
|
2020-06-02 06:53:06 +00:00
|
|
|
|
|
|
|
#if WASM_ENABLE_MULTI_MODULE != 0
|
|
|
|
if (!wasm_runtime_is_built_in_module(sub_module_name)) {
|
2023-09-28 00:56:11 +00:00
|
|
|
sub_module = (WASMModule *)wasm_runtime_load_depended_module(
|
|
|
|
(WASMModuleCommon *)parent_module, sub_module_name, error_buf,
|
|
|
|
error_buf_size);
|
2021-02-19 03:14:40 +00:00
|
|
|
if (!sub_module) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
linked_table = wasm_loader_resolve_table(
|
2021-10-08 03:44:39 +00:00
|
|
|
sub_module_name, table_name, declare_init_size, declare_max_size,
|
|
|
|
error_buf, error_buf_size);
|
2020-06-02 06:53:06 +00:00
|
|
|
if (!linked_table) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-19 03:14:40 +00:00
|
|
|
/* reset with linked table limit */
|
2020-06-02 06:53:06 +00:00
|
|
|
declare_elem_type = linked_table->elem_type;
|
|
|
|
declare_init_size = linked_table->init_size;
|
|
|
|
declare_max_size = linked_table->max_size;
|
|
|
|
declare_max_size_flag = linked_table->flags;
|
|
|
|
table->import_table_linked = linked_table;
|
|
|
|
table->import_module = sub_module;
|
|
|
|
}
|
2021-04-15 03:29:20 +00:00
|
|
|
#endif /* WASM_ENABLE_MULTI_MODULE != 0 */
|
2020-06-02 06:53:06 +00:00
|
|
|
|
|
|
|
/* (table (export "table") 10 20 funcref) */
|
2021-04-15 03:29:20 +00:00
|
|
|
/* we need this section working in wamrc */
|
2020-06-02 06:53:06 +00:00
|
|
|
if (!strcmp("spectest", sub_module_name)) {
|
2021-04-15 03:29:20 +00:00
|
|
|
const uint32 spectest_table_init_size = 10;
|
|
|
|
const uint32 spectest_table_max_size = 20;
|
2020-06-02 06:53:06 +00:00
|
|
|
|
|
|
|
if (strcmp("table", table_name)) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"incompatible import type or unknown import");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (declare_init_size > spectest_table_init_size
|
|
|
|
|| declare_max_size < spectest_table_max_size) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"incompatible import type");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_init_size = spectest_table_init_size;
|
|
|
|
declare_max_size = spectest_table_max_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now we believe all declaration are ok */
|
|
|
|
table->elem_type = declare_elem_type;
|
|
|
|
table->init_size = declare_init_size;
|
|
|
|
table->flags = declare_max_size_flag;
|
|
|
|
table->max_size = declare_max_size;
|
2022-12-27 04:59:17 +00:00
|
|
|
|
|
|
|
(void)parent_module;
|
2019-05-07 02:18:18 +00:00
|
|
|
return true;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 10:43:50 +00:00
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
check_memory_init_size(uint32 init_size, char *error_buf, uint32 error_buf_size)
|
2020-04-22 10:43:50 +00:00
|
|
|
{
|
2022-08-23 08:05:13 +00:00
|
|
|
if (init_size > DEFAULT_MAX_PAGES) {
|
2020-04-22 10:43:50 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"memory size must be at most 65536 pages (4GiB)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
check_memory_max_size(uint32 init_size, uint32 max_size, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2020-04-22 10:43:50 +00:00
|
|
|
{
|
|
|
|
if (max_size < init_size) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"size minimum must not be greater than maximum");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-23 08:05:13 +00:00
|
|
|
if (max_size > DEFAULT_MAX_PAGES) {
|
2020-04-22 10:43:50 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"memory size must be at most 65536 pages (4GiB)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
static bool
|
2021-02-19 03:14:40 +00:00
|
|
|
load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
|
2021-10-08 03:44:39 +00:00
|
|
|
WASMModule *parent_module, const char *sub_module_name,
|
|
|
|
const char *memory_name, WASMMemoryImport *memory,
|
2019-05-07 02:18:18 +00:00
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
|
|
|
const uint8 *p = *p_buf, *p_end = buf_end;
|
2020-03-30 03:06:39 +00:00
|
|
|
#if WASM_ENABLE_APP_FRAMEWORK != 0
|
2022-08-23 08:05:13 +00:00
|
|
|
uint32 pool_size = wasm_runtime_memory_pool_size();
|
2019-05-31 06:21:39 +00:00
|
|
|
uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
|
2020-02-10 04:36:45 +00:00
|
|
|
/ DEFAULT_NUM_BYTES_PER_PAGE;
|
2020-03-30 03:06:39 +00:00
|
|
|
#else
|
2022-08-23 08:05:13 +00:00
|
|
|
uint32 max_page_count = DEFAULT_MAX_PAGES;
|
2020-06-02 06:53:06 +00:00
|
|
|
#endif /* WASM_ENABLE_APP_FRAMEWORK */
|
|
|
|
uint32 declare_max_page_count_flag = 0;
|
|
|
|
uint32 declare_init_page_count = 0;
|
|
|
|
uint32 declare_max_page_count = 0;
|
|
|
|
#if WASM_ENABLE_MULTI_MODULE != 0
|
2021-02-19 03:14:40 +00:00
|
|
|
WASMModule *sub_module = NULL;
|
2020-06-02 06:53:06 +00:00
|
|
|
WASMMemory *linked_memory = NULL;
|
2020-03-30 03:06:39 +00:00
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
read_leb_uint32(p, p_end, declare_max_page_count_flag);
|
|
|
|
read_leb_uint32(p, p_end, declare_init_page_count);
|
|
|
|
if (!check_memory_init_size(declare_init_page_count, error_buf,
|
|
|
|
error_buf_size)) {
|
2020-04-22 10:43:50 +00:00
|
|
|
return false;
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
2020-04-22 10:43:50 +00:00
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
if (declare_max_page_count_flag & 1) {
|
|
|
|
read_leb_uint32(p, p_end, declare_max_page_count);
|
|
|
|
if (!check_memory_max_size(declare_init_page_count,
|
|
|
|
declare_max_page_count, error_buf,
|
|
|
|
error_buf_size)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (declare_max_page_count > max_page_count) {
|
|
|
|
declare_max_page_count = max_page_count;
|
|
|
|
}
|
2019-05-31 06:21:39 +00:00
|
|
|
}
|
2020-06-02 06:53:06 +00:00
|
|
|
else {
|
2019-05-31 06:21:39 +00:00
|
|
|
/* Limit the maximum memory size to max_page_count */
|
2020-06-02 06:53:06 +00:00
|
|
|
declare_max_page_count = max_page_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if WASM_ENABLE_MULTI_MODULE != 0
|
|
|
|
if (!wasm_runtime_is_built_in_module(sub_module_name)) {
|
2023-09-28 00:56:11 +00:00
|
|
|
sub_module = (WASMModule *)wasm_runtime_load_depended_module(
|
|
|
|
(WASMModuleCommon *)parent_module, sub_module_name, error_buf,
|
|
|
|
error_buf_size);
|
2021-02-19 03:14:40 +00:00
|
|
|
if (!sub_module) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
linked_memory = wasm_loader_resolve_memory(
|
2021-10-08 03:44:39 +00:00
|
|
|
sub_module_name, memory_name, declare_init_page_count,
|
|
|
|
declare_max_page_count, error_buf, error_buf_size);
|
2020-06-02 06:53:06 +00:00
|
|
|
if (!linked_memory) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
/**
|
|
|
|
* reset with linked memory limit
|
|
|
|
*/
|
|
|
|
memory->import_module = sub_module;
|
|
|
|
memory->import_memory_linked = linked_memory;
|
|
|
|
declare_init_page_count = linked_memory->init_page_count;
|
|
|
|
declare_max_page_count = linked_memory->max_page_count;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* (memory (export "memory") 1 2) */
|
|
|
|
if (!strcmp("spectest", sub_module_name)) {
|
|
|
|
uint32 spectest_memory_init_page = 1;
|
|
|
|
uint32 spectest_memory_max_page = 2;
|
|
|
|
|
|
|
|
if (strcmp("memory", memory_name)) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"incompatible import type or unknown import");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (declare_init_page_count > spectest_memory_init_page
|
|
|
|
|| declare_max_page_count < spectest_memory_max_page) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"incompatible import type");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_init_page_count = spectest_memory_init_page;
|
|
|
|
declare_max_page_count = spectest_memory_max_page;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now we believe all declaration are ok */
|
|
|
|
memory->flags = declare_max_page_count_flag;
|
|
|
|
memory->init_page_count = declare_init_page_count;
|
|
|
|
memory->max_page_count = declare_max_page_count;
|
2020-02-10 04:36:45 +00:00
|
|
|
memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
*p_buf = p;
|
2022-12-27 04:59:17 +00:00
|
|
|
|
|
|
|
(void)parent_module;
|
2019-05-07 02:18:18 +00:00
|
|
|
return true;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
static bool
|
2021-02-19 03:14:40 +00:00
|
|
|
load_global_import(const uint8 **p_buf, const uint8 *buf_end,
|
2021-10-08 03:44:39 +00:00
|
|
|
const WASMModule *parent_module, char *sub_module_name,
|
|
|
|
char *global_name, WASMGlobalImport *global, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2020-06-02 06:53:06 +00:00
|
|
|
{
|
|
|
|
const uint8 *p = *p_buf, *p_end = buf_end;
|
|
|
|
uint8 declare_type = 0;
|
|
|
|
uint8 declare_mutable = 0;
|
2021-02-19 03:14:40 +00:00
|
|
|
#if WASM_ENABLE_MULTI_MODULE != 0
|
|
|
|
WASMModule *sub_module = NULL;
|
|
|
|
WASMGlobal *linked_global = NULL;
|
|
|
|
#endif
|
2023-02-13 07:06:04 +00:00
|
|
|
bool ret = false;
|
2020-06-02 06:53:06 +00:00
|
|
|
|
|
|
|
CHECK_BUF(p, p_end, 2);
|
|
|
|
declare_type = read_uint8(p);
|
|
|
|
declare_mutable = read_uint8(p);
|
|
|
|
*p_buf = p;
|
|
|
|
|
|
|
|
if (declare_mutable >= 2) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "invalid mutability");
|
2020-06-02 06:53:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-28 02:46:31 +00:00
|
|
|
#if WASM_ENABLE_LIBC_BUILTIN != 0
|
2023-02-13 07:06:04 +00:00
|
|
|
ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
|
|
|
|
global);
|
|
|
|
if (ret) {
|
2021-03-02 10:24:15 +00:00
|
|
|
if (global->type != declare_type
|
|
|
|
|| global->is_mutable != declare_mutable) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"incompatible import type");
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-13 07:06:04 +00:00
|
|
|
global->is_linked = true;
|
2021-03-02 10:24:15 +00:00
|
|
|
}
|
2020-12-28 02:46:31 +00:00
|
|
|
#endif
|
2020-06-02 06:53:06 +00:00
|
|
|
#if WASM_ENABLE_MULTI_MODULE != 0
|
2021-02-19 03:14:40 +00:00
|
|
|
if (!global->is_linked
|
|
|
|
&& !wasm_runtime_is_built_in_module(sub_module_name)) {
|
2023-09-28 00:56:11 +00:00
|
|
|
sub_module = (WASMModule *)wasm_runtime_load_depended_module(
|
|
|
|
(WASMModuleCommon *)parent_module, sub_module_name, error_buf,
|
|
|
|
error_buf_size);
|
2021-02-19 03:14:40 +00:00
|
|
|
if (!sub_module) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
/* check sub modules */
|
2021-10-08 03:44:39 +00:00
|
|
|
linked_global = wasm_loader_resolve_global(
|
|
|
|
sub_module_name, global_name, declare_type, declare_mutable,
|
|
|
|
error_buf, error_buf_size);
|
2020-06-02 06:53:06 +00:00
|
|
|
if (linked_global) {
|
|
|
|
global->import_module = sub_module;
|
|
|
|
global->import_global_linked = linked_global;
|
2020-10-16 09:43:57 +00:00
|
|
|
global->is_linked = true;
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
global->module_name = sub_module_name;
|
|
|
|
global->field_name = global_name;
|
|
|
|
global->type = declare_type;
|
2020-10-16 09:43:57 +00:00
|
|
|
global->is_mutable = (declare_mutable == 1);
|
2022-12-27 04:59:17 +00:00
|
|
|
|
|
|
|
(void)parent_module;
|
2023-02-13 07:06:04 +00:00
|
|
|
(void)ret;
|
2020-06-02 06:53:06 +00:00
|
|
|
return true;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
static bool
|
|
|
|
load_table(const uint8 **p_buf, const uint8 *buf_end, WASMTable *table,
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
2020-08-20 04:43:12 +00:00
|
|
|
const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2019-12-13 07:30:30 +00:00
|
|
|
CHECK_BUF(p, p_end, 1);
|
2021-04-15 03:29:20 +00:00
|
|
|
/* 0x70 or 0x6F */
|
2019-12-13 07:30:30 +00:00
|
|
|
table->elem_type = read_uint8(p);
|
2021-04-15 03:29:20 +00:00
|
|
|
if (VALUE_TYPE_FUNCREF != table->elem_type
|
|
|
|
#if WASM_ENABLE_REF_TYPES != 0
|
2021-11-18 09:42:49 +00:00
|
|
|
&& VALUE_TYPE_EXTERNREF != table->elem_type
|
2021-04-15 03:29:20 +00:00
|
|
|
#endif
|
|
|
|
) {
|
2020-06-02 06:53:06 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "incompatible import type");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-20 04:43:12 +00:00
|
|
|
p_org = p;
|
2019-05-07 02:18:18 +00:00
|
|
|
read_leb_uint32(p, p_end, table->flags);
|
2021-03-02 10:24:15 +00:00
|
|
|
#if WASM_ENABLE_SHARED_MEMORY == 0
|
2020-08-20 04:43:12 +00:00
|
|
|
if (p - p_org > 1) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"integer representation too long");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (table->flags > 1) {
|
|
|
|
set_error_buf(error_buf, error_buf_size, "integer too large");
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-02 10:24:15 +00:00
|
|
|
#else
|
|
|
|
if (p - p_org > 1) {
|
|
|
|
set_error_buf(error_buf, error_buf_size, "invalid limits flags");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (table->flags == 2) {
|
|
|
|
set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (table->flags > 1) {
|
|
|
|
set_error_buf(error_buf, error_buf_size, "invalid limits flags");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
2020-08-20 04:43:12 +00:00
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
read_leb_uint32(p, p_end, table->init_size);
|
2021-04-15 03:29:20 +00:00
|
|
|
|
|
|
|
if (table->flags) {
|
2019-05-07 02:18:18 +00:00
|
|
|
read_leb_uint32(p, p_end, table->max_size);
|
2021-04-15 03:29:20 +00:00
|
|
|
if (!check_table_max_size(table->init_size, table->max_size, error_buf,
|
|
|
|
error_buf_size))
|
2020-05-08 04:38:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-06-15 11:04:04 +00:00
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
adjust_table_max_size(table->init_size, table->flags, &table->max_size);
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
*p_buf = p;
|
|
|
|
return true;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
2020-08-20 04:43:12 +00:00
|
|
|
const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
|
2020-03-30 03:06:39 +00:00
|
|
|
#if WASM_ENABLE_APP_FRAMEWORK != 0
|
2022-08-23 08:05:13 +00:00
|
|
|
uint32 pool_size = wasm_runtime_memory_pool_size();
|
2019-05-31 06:21:39 +00:00
|
|
|
uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
|
2020-02-10 04:36:45 +00:00
|
|
|
/ DEFAULT_NUM_BYTES_PER_PAGE;
|
2020-03-30 03:06:39 +00:00
|
|
|
#else
|
2022-08-23 08:05:13 +00:00
|
|
|
uint32 max_page_count = DEFAULT_MAX_PAGES;
|
2020-03-30 03:06:39 +00:00
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-08-20 04:43:12 +00:00
|
|
|
p_org = p;
|
2019-05-07 02:18:18 +00:00
|
|
|
read_leb_uint32(p, p_end, memory->flags);
|
2021-03-02 10:24:15 +00:00
|
|
|
#if WASM_ENABLE_SHARED_MEMORY == 0
|
2020-08-20 04:43:12 +00:00
|
|
|
if (p - p_org > 1) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"integer representation too long");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (memory->flags > 1) {
|
2023-10-30 03:07:01 +00:00
|
|
|
if (memory->flags & 2) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"shared memory flag was found, "
|
|
|
|
"please enable shared memory, lib-pthread "
|
|
|
|
"or lib-wasi-threads");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
set_error_buf(error_buf, error_buf_size, "invalid memory flags");
|
|
|
|
}
|
2020-08-20 04:43:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#else
|
2021-03-02 10:24:15 +00:00
|
|
|
if (p - p_org > 1) {
|
|
|
|
set_error_buf(error_buf, error_buf_size, "invalid limits flags");
|
|
|
|
return false;
|
|
|
|
}
|
2020-08-26 10:33:29 +00:00
|
|
|
if (memory->flags > 3) {
|
2021-03-02 10:24:15 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "invalid limits flags");
|
2020-08-20 04:43:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-08-26 10:33:29 +00:00
|
|
|
else if (memory->flags == 2) {
|
2021-10-08 03:44:39 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"shared memory must have maximum");
|
2020-08-26 10:33:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-08-20 04:43:12 +00:00
|
|
|
#endif
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
read_leb_uint32(p, p_end, memory->init_page_count);
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!check_memory_init_size(memory->init_page_count, error_buf,
|
|
|
|
error_buf_size))
|
2020-04-22 10:43:50 +00:00
|
|
|
return false;
|
|
|
|
|
2019-05-31 06:21:39 +00:00
|
|
|
if (memory->flags & 1) {
|
2019-05-07 02:18:18 +00:00
|
|
|
read_leb_uint32(p, p_end, memory->max_page_count);
|
2020-04-22 10:43:50 +00:00
|
|
|
if (!check_memory_max_size(memory->init_page_count,
|
2021-10-08 03:44:39 +00:00
|
|
|
memory->max_page_count, error_buf,
|
|
|
|
error_buf_size))
|
|
|
|
return false;
|
2019-05-31 06:21:39 +00:00
|
|
|
if (memory->max_page_count > max_page_count)
|
|
|
|
memory->max_page_count = max_page_count;
|
|
|
|
}
|
2020-08-20 04:43:12 +00:00
|
|
|
else {
|
2019-05-31 06:21:39 +00:00
|
|
|
/* Limit the maximum memory size to max_page_count */
|
|
|
|
memory->max_page_count = max_page_count;
|
2020-08-20 04:43:12 +00:00
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-02-10 04:36:45 +00:00
|
|
|
memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
*p_buf = p;
|
|
|
|
return true;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
|
2021-11-08 03:27:26 +00:00
|
|
|
bool is_load_from_file_buf, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
|
|
|
const uint8 *p = buf, *p_end = buf_end, *p_old;
|
|
|
|
uint32 import_count, name_len, type_index, i, u32, flags;
|
2019-11-20 13:16:36 +00:00
|
|
|
uint64 total_size;
|
2019-05-07 02:18:18 +00:00
|
|
|
WASMImport *import;
|
|
|
|
WASMImport *import_functions = NULL, *import_tables = NULL;
|
|
|
|
WASMImport *import_memories = NULL, *import_globals = NULL;
|
2020-06-02 06:53:06 +00:00
|
|
|
char *sub_module_name, *field_name;
|
|
|
|
uint8 u8, kind;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, import_count);
|
|
|
|
|
|
|
|
if (import_count) {
|
|
|
|
module->import_count = import_count;
|
2019-11-20 13:16:36 +00:00
|
|
|
total_size = sizeof(WASMImport) * (uint64)import_count;
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!(module->imports =
|
|
|
|
loader_malloc(total_size, error_buf, error_buf_size))) {
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_old = p;
|
|
|
|
|
|
|
|
/* Scan firstly to get import count of each type */
|
|
|
|
for (i = 0; i < import_count; i++) {
|
|
|
|
/* module name */
|
|
|
|
read_leb_uint32(p, p_end, name_len);
|
|
|
|
CHECK_BUF(p, p_end, name_len);
|
|
|
|
p += name_len;
|
|
|
|
|
|
|
|
/* field name */
|
|
|
|
read_leb_uint32(p, p_end, name_len);
|
|
|
|
CHECK_BUF(p, p_end, name_len);
|
|
|
|
p += name_len;
|
|
|
|
|
2019-12-13 07:30:30 +00:00
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
/* 0x00/0x01/0x02/0x03 */
|
|
|
|
kind = read_uint8(p);
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
switch (kind) {
|
|
|
|
case IMPORT_KIND_FUNC: /* import function */
|
|
|
|
read_leb_uint32(p, p_end, type_index);
|
|
|
|
module->import_function_count++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IMPORT_KIND_TABLE: /* import table */
|
2019-12-13 07:30:30 +00:00
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
/* 0x70 */
|
|
|
|
u8 = read_uint8(p);
|
2019-05-07 02:18:18 +00:00
|
|
|
read_leb_uint32(p, p_end, flags);
|
|
|
|
read_leb_uint32(p, p_end, u32);
|
|
|
|
if (flags & 1)
|
|
|
|
read_leb_uint32(p, p_end, u32);
|
|
|
|
module->import_table_count++;
|
2021-04-15 03:29:20 +00:00
|
|
|
|
2021-11-18 09:42:49 +00:00
|
|
|
#if WASM_ENABLE_REF_TYPES == 0
|
|
|
|
if (module->import_table_count > 1) {
|
2019-11-27 02:52:12 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2020-08-21 07:11:31 +00:00
|
|
|
"multiple tables");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-11-18 09:42:49 +00:00
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IMPORT_KIND_MEMORY: /* import memory */
|
|
|
|
read_leb_uint32(p, p_end, flags);
|
|
|
|
read_leb_uint32(p, p_end, u32);
|
|
|
|
if (flags & 1)
|
|
|
|
read_leb_uint32(p, p_end, u32);
|
|
|
|
module->import_memory_count++;
|
|
|
|
if (module->import_memory_count > 1) {
|
2019-11-27 02:52:12 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2020-08-21 07:11:31 +00:00
|
|
|
"multiple memories");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IMPORT_KIND_GLOBAL: /* import global */
|
2019-11-27 02:52:12 +00:00
|
|
|
CHECK_BUF(p, p_end, 2);
|
|
|
|
p += 2;
|
2019-05-07 02:18:18 +00:00
|
|
|
module->import_global_count++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2020-08-21 07:11:31 +00:00
|
|
|
"invalid import kind");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (module->import_function_count)
|
|
|
|
import_functions = module->import_functions = module->imports;
|
|
|
|
if (module->import_table_count)
|
|
|
|
import_tables = module->import_tables =
|
|
|
|
module->imports + module->import_function_count;
|
|
|
|
if (module->import_memory_count)
|
|
|
|
import_memories = module->import_memories =
|
2021-10-08 03:44:39 +00:00
|
|
|
module->imports + module->import_function_count
|
|
|
|
+ module->import_table_count;
|
2019-05-07 02:18:18 +00:00
|
|
|
if (module->import_global_count)
|
|
|
|
import_globals = module->import_globals =
|
2021-10-08 03:44:39 +00:00
|
|
|
module->imports + module->import_function_count
|
|
|
|
+ module->import_table_count + module->import_memory_count;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
p = p_old;
|
|
|
|
|
2021-02-19 03:14:40 +00:00
|
|
|
/* Scan again to resolve the data */
|
2019-05-07 02:18:18 +00:00
|
|
|
for (i = 0; i < import_count; i++) {
|
|
|
|
/* load module name */
|
|
|
|
read_leb_uint32(p, p_end, name_len);
|
|
|
|
CHECK_BUF(p, p_end, name_len);
|
2020-06-02 06:53:06 +00:00
|
|
|
if (!(sub_module_name = const_str_list_insert(
|
2021-11-08 03:27:26 +00:00
|
|
|
p, name_len, module, is_load_from_file_buf, error_buf,
|
|
|
|
error_buf_size))) {
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
p += name_len;
|
|
|
|
|
|
|
|
/* load field name */
|
|
|
|
read_leb_uint32(p, p_end, name_len);
|
|
|
|
CHECK_BUF(p, p_end, name_len);
|
2020-06-02 06:53:06 +00:00
|
|
|
if (!(field_name = const_str_list_insert(
|
2021-11-08 03:27:26 +00:00
|
|
|
p, name_len, module, is_load_from_file_buf, error_buf,
|
|
|
|
error_buf_size))) {
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
p += name_len;
|
|
|
|
|
2019-12-13 07:30:30 +00:00
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
/* 0x00/0x01/0x02/0x03 */
|
|
|
|
kind = read_uint8(p);
|
2021-02-19 03:14:40 +00:00
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
switch (kind) {
|
|
|
|
case IMPORT_KIND_FUNC: /* import function */
|
Enable AoT and wamr-sdk, and change arguments of call wasm API (#157)
* Implement memory profiler, optimize memory usage, modify code indent
* Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default
* Add a new extension library: connection
* Fix bug of reading magic number and version in big endian platform
* Re-org platform APIs: move most platform APIs from iwasm to shared-lib
* Enhance wasm loader to fix some security issues
* Fix issue about illegal load of EXC_RETURN into PC on stm32 board
* Updates that let a restricted version of the interpreter run in SGX
* Enable native/app address validation and conversion for wasm app
* Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused
* Refine binary size and fix several minor issues
Optimize interpreter LOAD/STORE opcodes to decrease the binary size
Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found
Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper
Add macros of global heap size, stack size, heap size for Zephyr main.c
Clear compile warning of wasm_application.c
* Add more strict security checks for libc wrapper API's
* Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files
* Enhance security of libc strcpy/sprintf wrapper function
* Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions
* Remove get_module_inst() and fix issue of call native
* Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate
* Refine interpreter call native process, refine memory boudary check
* Fix issues of invokeNative function of arm/mips/general version
* Add a switch to build simple sample without gui support
* Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code
* Re-org shared lib header files, remove unused info; fix compile issues of vxworks
* Add build target general
* Remove unused files
* Update license header
* test push
* Restore file
* Sync up with internal/feature
* Sync up with internal/feature
* Rename build_wamr_app to build_wasm_app
* Fix small issues of README
* Enhance malformed wasm file checking
Fix issue of print hex int and implement utf8 string check
Fix wasi file read/write right issue
Fix minor issue of build wasm app doc
* Sync up with internal/feature
* Sync up with internal/feature: fix interpreter arm issue, fix read leb issue
* Sync up with internal/feature
* Fix bug of config.h and rename wasi config.h to ssp_config.h
* Sync up with internal/feature
* Import wamr aot
* update document
* update document
* Update document, disable WASI in 32bit
* update document
* remove files
* update document
* Update document
* update document
* update document
* update samples
* Sync up with internal repo
2020-01-21 05:26:14 +00:00
|
|
|
bh_assert(import_functions);
|
2019-05-07 02:18:18 +00:00
|
|
|
import = import_functions++;
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!load_function_import(
|
|
|
|
&p, p_end, module, sub_module_name, field_name,
|
|
|
|
&import->u.function, error_buf, error_buf_size)) {
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IMPORT_KIND_TABLE: /* import table */
|
Enable AoT and wamr-sdk, and change arguments of call wasm API (#157)
* Implement memory profiler, optimize memory usage, modify code indent
* Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default
* Add a new extension library: connection
* Fix bug of reading magic number and version in big endian platform
* Re-org platform APIs: move most platform APIs from iwasm to shared-lib
* Enhance wasm loader to fix some security issues
* Fix issue about illegal load of EXC_RETURN into PC on stm32 board
* Updates that let a restricted version of the interpreter run in SGX
* Enable native/app address validation and conversion for wasm app
* Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused
* Refine binary size and fix several minor issues
Optimize interpreter LOAD/STORE opcodes to decrease the binary size
Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found
Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper
Add macros of global heap size, stack size, heap size for Zephyr main.c
Clear compile warning of wasm_application.c
* Add more strict security checks for libc wrapper API's
* Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files
* Enhance security of libc strcpy/sprintf wrapper function
* Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions
* Remove get_module_inst() and fix issue of call native
* Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate
* Refine interpreter call native process, refine memory boudary check
* Fix issues of invokeNative function of arm/mips/general version
* Add a switch to build simple sample without gui support
* Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code
* Re-org shared lib header files, remove unused info; fix compile issues of vxworks
* Add build target general
* Remove unused files
* Update license header
* test push
* Restore file
* Sync up with internal/feature
* Sync up with internal/feature
* Rename build_wamr_app to build_wasm_app
* Fix small issues of README
* Enhance malformed wasm file checking
Fix issue of print hex int and implement utf8 string check
Fix wasi file read/write right issue
Fix minor issue of build wasm app doc
* Sync up with internal/feature
* Sync up with internal/feature: fix interpreter arm issue, fix read leb issue
* Sync up with internal/feature
* Fix bug of config.h and rename wasi config.h to ssp_config.h
* Sync up with internal/feature
* Import wamr aot
* update document
* update document
* Update document, disable WASI in 32bit
* update document
* remove files
* update document
* Update document
* update document
* update document
* update samples
* Sync up with internal repo
2020-01-21 05:26:14 +00:00
|
|
|
bh_assert(import_tables);
|
2019-05-07 02:18:18 +00:00
|
|
|
import = import_tables++;
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!load_table_import(&p, p_end, module, sub_module_name,
|
|
|
|
field_name, &import->u.table,
|
2021-02-19 03:14:40 +00:00
|
|
|
error_buf, error_buf_size)) {
|
2020-06-02 06:53:06 +00:00
|
|
|
LOG_DEBUG("can not import such a table (%s,%s)",
|
|
|
|
sub_module_name, field_name);
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IMPORT_KIND_MEMORY: /* import memory */
|
Enable AoT and wamr-sdk, and change arguments of call wasm API (#157)
* Implement memory profiler, optimize memory usage, modify code indent
* Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default
* Add a new extension library: connection
* Fix bug of reading magic number and version in big endian platform
* Re-org platform APIs: move most platform APIs from iwasm to shared-lib
* Enhance wasm loader to fix some security issues
* Fix issue about illegal load of EXC_RETURN into PC on stm32 board
* Updates that let a restricted version of the interpreter run in SGX
* Enable native/app address validation and conversion for wasm app
* Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused
* Refine binary size and fix several minor issues
Optimize interpreter LOAD/STORE opcodes to decrease the binary size
Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found
Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper
Add macros of global heap size, stack size, heap size for Zephyr main.c
Clear compile warning of wasm_application.c
* Add more strict security checks for libc wrapper API's
* Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files
* Enhance security of libc strcpy/sprintf wrapper function
* Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions
* Remove get_module_inst() and fix issue of call native
* Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate
* Refine interpreter call native process, refine memory boudary check
* Fix issues of invokeNative function of arm/mips/general version
* Add a switch to build simple sample without gui support
* Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code
* Re-org shared lib header files, remove unused info; fix compile issues of vxworks
* Add build target general
* Remove unused files
* Update license header
* test push
* Restore file
* Sync up with internal/feature
* Sync up with internal/feature
* Rename build_wamr_app to build_wasm_app
* Fix small issues of README
* Enhance malformed wasm file checking
Fix issue of print hex int and implement utf8 string check
Fix wasi file read/write right issue
Fix minor issue of build wasm app doc
* Sync up with internal/feature
* Sync up with internal/feature: fix interpreter arm issue, fix read leb issue
* Sync up with internal/feature
* Fix bug of config.h and rename wasi config.h to ssp_config.h
* Sync up with internal/feature
* Import wamr aot
* update document
* update document
* Update document, disable WASI in 32bit
* update document
* remove files
* update document
* Update document
* update document
* update document
* update samples
* Sync up with internal repo
2020-01-21 05:26:14 +00:00
|
|
|
bh_assert(import_memories);
|
2019-05-07 02:18:18 +00:00
|
|
|
import = import_memories++;
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!load_memory_import(&p, p_end, module, sub_module_name,
|
|
|
|
field_name, &import->u.memory,
|
2021-02-19 03:14:40 +00:00
|
|
|
error_buf, error_buf_size)) {
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IMPORT_KIND_GLOBAL: /* import global */
|
Enable AoT and wamr-sdk, and change arguments of call wasm API (#157)
* Implement memory profiler, optimize memory usage, modify code indent
* Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default
* Add a new extension library: connection
* Fix bug of reading magic number and version in big endian platform
* Re-org platform APIs: move most platform APIs from iwasm to shared-lib
* Enhance wasm loader to fix some security issues
* Fix issue about illegal load of EXC_RETURN into PC on stm32 board
* Updates that let a restricted version of the interpreter run in SGX
* Enable native/app address validation and conversion for wasm app
* Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused
* Refine binary size and fix several minor issues
Optimize interpreter LOAD/STORE opcodes to decrease the binary size
Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found
Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper
Add macros of global heap size, stack size, heap size for Zephyr main.c
Clear compile warning of wasm_application.c
* Add more strict security checks for libc wrapper API's
* Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files
* Enhance security of libc strcpy/sprintf wrapper function
* Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions
* Remove get_module_inst() and fix issue of call native
* Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate
* Refine interpreter call native process, refine memory boudary check
* Fix issues of invokeNative function of arm/mips/general version
* Add a switch to build simple sample without gui support
* Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code
* Re-org shared lib header files, remove unused info; fix compile issues of vxworks
* Add build target general
* Remove unused files
* Update license header
* test push
* Restore file
* Sync up with internal/feature
* Sync up with internal/feature
* Rename build_wamr_app to build_wasm_app
* Fix small issues of README
* Enhance malformed wasm file checking
Fix issue of print hex int and implement utf8 string check
Fix wasi file read/write right issue
Fix minor issue of build wasm app doc
* Sync up with internal/feature
* Sync up with internal/feature: fix interpreter arm issue, fix read leb issue
* Sync up with internal/feature
* Fix bug of config.h and rename wasi config.h to ssp_config.h
* Sync up with internal/feature
* Import wamr aot
* update document
* update document
* Update document, disable WASI in 32bit
* update document
* remove files
* update document
* Update document
* update document
* update document
* update samples
* Sync up with internal repo
2020-01-21 05:26:14 +00:00
|
|
|
bh_assert(import_globals);
|
2019-05-07 02:18:18 +00:00
|
|
|
import = import_globals++;
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!load_global_import(&p, p_end, module, sub_module_name,
|
|
|
|
field_name, &import->u.global,
|
2020-06-02 06:53:06 +00:00
|
|
|
error_buf, error_buf_size)) {
|
2019-11-27 02:52:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2020-08-20 04:43:12 +00:00
|
|
|
"invalid import kind");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
import->kind = kind;
|
2020-06-02 06:53:06 +00:00
|
|
|
import->u.names.module_name = sub_module_name;
|
2019-05-07 02:18:18 +00:00
|
|
|
import->u.names.field_name = field_name;
|
|
|
|
}
|
2019-11-20 13:16:36 +00:00
|
|
|
|
Enable AoT and wamr-sdk, and change arguments of call wasm API (#157)
* Implement memory profiler, optimize memory usage, modify code indent
* Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default
* Add a new extension library: connection
* Fix bug of reading magic number and version in big endian platform
* Re-org platform APIs: move most platform APIs from iwasm to shared-lib
* Enhance wasm loader to fix some security issues
* Fix issue about illegal load of EXC_RETURN into PC on stm32 board
* Updates that let a restricted version of the interpreter run in SGX
* Enable native/app address validation and conversion for wasm app
* Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused
* Refine binary size and fix several minor issues
Optimize interpreter LOAD/STORE opcodes to decrease the binary size
Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found
Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper
Add macros of global heap size, stack size, heap size for Zephyr main.c
Clear compile warning of wasm_application.c
* Add more strict security checks for libc wrapper API's
* Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files
* Enhance security of libc strcpy/sprintf wrapper function
* Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions
* Remove get_module_inst() and fix issue of call native
* Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate
* Refine interpreter call native process, refine memory boudary check
* Fix issues of invokeNative function of arm/mips/general version
* Add a switch to build simple sample without gui support
* Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code
* Re-org shared lib header files, remove unused info; fix compile issues of vxworks
* Add build target general
* Remove unused files
* Update license header
* test push
* Restore file
* Sync up with internal/feature
* Sync up with internal/feature
* Rename build_wamr_app to build_wasm_app
* Fix small issues of README
* Enhance malformed wasm file checking
Fix issue of print hex int and implement utf8 string check
Fix wasi file read/write right issue
Fix minor issue of build wasm app doc
* Sync up with internal/feature
* Sync up with internal/feature: fix interpreter arm issue, fix read leb issue
* Sync up with internal/feature
* Fix bug of config.h and rename wasi config.h to ssp_config.h
* Sync up with internal/feature
* Import wamr aot
* update document
* update document
* Update document, disable WASI in 32bit
* update document
* remove files
* update document
* Update document
* update document
* update document
* update samples
* Sync up with internal repo
2020-01-21 05:26:14 +00:00
|
|
|
#if WASM_ENABLE_LIBC_WASI != 0
|
2019-11-20 13:16:36 +00:00
|
|
|
import = module->import_functions;
|
|
|
|
for (i = 0; i < module->import_function_count; i++, import++) {
|
2020-04-07 03:04:46 +00:00
|
|
|
if (!strcmp(import->u.names.module_name, "wasi_unstable")
|
2021-10-08 03:44:39 +00:00
|
|
|
|| !strcmp(import->u.names.module_name,
|
|
|
|
"wasi_snapshot_preview1")) {
|
2021-12-29 03:04:36 +00:00
|
|
|
module->import_wasi_api = true;
|
2019-11-20 13:16:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p != p_end) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "section size mismatch");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_VERBOSE("Load import section success.\n");
|
|
|
|
(void)u8;
|
|
|
|
(void)u32;
|
2020-06-02 06:53:06 +00:00
|
|
|
(void)type_index;
|
2019-05-07 02:18:18 +00:00
|
|
|
return true;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-18 07:15:24 +00:00
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
init_function_local_offsets(WASMFunction *func, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2020-02-18 07:15:24 +00:00
|
|
|
{
|
|
|
|
WASMType *param_type = func->func_type;
|
|
|
|
uint32 param_count = param_type->param_count;
|
|
|
|
uint8 *param_types = param_type->types;
|
|
|
|
uint32 local_count = func->local_count;
|
|
|
|
uint8 *local_types = func->local_types;
|
|
|
|
uint32 i, local_offset = 0;
|
|
|
|
uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
|
|
|
|
|
2020-09-23 07:54:22 +00:00
|
|
|
/*
|
|
|
|
* Only allocate memory when total_size is not 0,
|
|
|
|
* or the return value of malloc(0) might be NULL on some platforms,
|
|
|
|
* which causes wasm loader return false.
|
|
|
|
*/
|
|
|
|
if (total_size > 0
|
|
|
|
&& !(func->local_offsets =
|
2021-10-08 03:44:39 +00:00
|
|
|
loader_malloc(total_size, error_buf, error_buf_size))) {
|
2020-02-18 07:15:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < param_count; i++) {
|
|
|
|
func->local_offsets[i] = (uint16)local_offset;
|
|
|
|
local_offset += wasm_value_type_cell_num(param_types[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < local_count; i++) {
|
|
|
|
func->local_offsets[param_count + i] = (uint16)local_offset;
|
|
|
|
local_offset += wasm_value_type_cell_num(local_types[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
static bool
|
|
|
|
load_function_section(const uint8 *buf, const uint8 *buf_end,
|
|
|
|
const uint8 *buf_code, const uint8 *buf_code_end,
|
2021-10-08 03:44:39 +00:00
|
|
|
WASMModule *module, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
|
|
|
const uint8 *p = buf, *p_end = buf_end;
|
|
|
|
const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
|
2019-11-20 13:16:36 +00:00
|
|
|
uint32 func_count;
|
|
|
|
uint64 total_size;
|
2019-10-11 07:25:23 +00:00
|
|
|
uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
|
2022-04-03 23:55:37 +00:00
|
|
|
uint32 local_count, local_set_count, sub_local_count, local_cell_num;
|
2019-05-07 02:18:18 +00:00
|
|
|
uint8 type;
|
|
|
|
WASMFunction *func;
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, func_count);
|
|
|
|
|
2019-10-11 07:25:23 +00:00
|
|
|
if (buf_code)
|
|
|
|
read_leb_uint32(p_code, buf_code_end, code_count);
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
if (func_count != code_count) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2021-04-15 03:29:20 +00:00
|
|
|
"function and code section have inconsistent lengths or "
|
|
|
|
"unexpected end");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (func_count) {
|
|
|
|
module->function_count = func_count;
|
2021-10-08 03:44:39 +00:00
|
|
|
total_size = sizeof(WASMFunction *) * (uint64)func_count;
|
2020-06-08 03:19:09 +00:00
|
|
|
if (!(module->functions =
|
2021-10-08 03:44:39 +00:00
|
|
|
loader_malloc(total_size, error_buf, error_buf_size))) {
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < func_count; i++) {
|
|
|
|
/* Resolve function type */
|
|
|
|
read_leb_uint32(p, p_end, type_index);
|
|
|
|
if (type_index >= module->type_count) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "unknown type");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-05 10:05:30 +00:00
|
|
|
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
|
|
|
|
type_index = wasm_get_smallest_type_idx(
|
2021-10-08 03:44:39 +00:00
|
|
|
module->types, module->type_count, type_index);
|
2021-01-05 10:05:30 +00:00
|
|
|
#endif
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
read_leb_uint32(p_code, buf_code_end, code_size);
|
2021-10-08 03:44:39 +00:00
|
|
|
if (code_size == 0 || p_code + code_size > buf_code_end) {
|
2019-05-07 02:18:18 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2020-08-21 07:11:31 +00:00
|
|
|
"invalid function code size");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Resolve local set count */
|
|
|
|
p_code_end = p_code + code_size;
|
|
|
|
local_count = 0;
|
|
|
|
read_leb_uint32(p_code, buf_code_end, local_set_count);
|
|
|
|
p_code_save = p_code;
|
|
|
|
|
|
|
|
/* Calculate total local count */
|
|
|
|
for (j = 0; j < local_set_count; j++) {
|
|
|
|
read_leb_uint32(p_code, buf_code_end, sub_local_count);
|
2019-10-11 07:25:23 +00:00
|
|
|
if (sub_local_count > UINT32_MAX - local_count) {
|
2021-10-08 03:44:39 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "too many locals");
|
2019-10-11 07:25:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-12-13 07:30:30 +00:00
|
|
|
CHECK_BUF(p_code, buf_code_end, 1);
|
|
|
|
/* 0x7F/0x7E/0x7D/0x7C */
|
|
|
|
type = read_uint8(p_code);
|
2019-05-07 02:18:18 +00:00
|
|
|
local_count += sub_local_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Alloc memory, layout: function structure + local types */
|
2019-11-20 13:16:36 +00:00
|
|
|
code_size = (uint32)(p_code_end - p_code);
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2019-11-20 13:16:36 +00:00
|
|
|
total_size = sizeof(WASMFunction) + (uint64)local_count;
|
2020-06-08 03:19:09 +00:00
|
|
|
if (!(func = module->functions[i] =
|
2021-10-08 03:44:39 +00:00
|
|
|
loader_malloc(total_size, error_buf, error_buf_size))) {
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set function type, local count, code size and code body */
|
|
|
|
func->func_type = module->types[type_index];
|
|
|
|
func->local_count = local_count;
|
|
|
|
if (local_count > 0)
|
2021-10-08 03:44:39 +00:00
|
|
|
func->local_types = (uint8 *)func + sizeof(WASMFunction);
|
2019-05-07 02:18:18 +00:00
|
|
|
func->code_size = code_size;
|
2020-06-02 06:53:06 +00:00
|
|
|
/*
|
|
|
|
* we shall make a copy of code body [p_code, p_code + code_size]
|
|
|
|
* when we are worrying about inappropriate releasing behaviour.
|
|
|
|
* all code bodies are actually in a buffer which user allocates in
|
|
|
|
* his embedding environment and we don't have power on them.
|
|
|
|
* it will be like:
|
|
|
|
* code_body_cp = malloc(code_size);
|
|
|
|
* memcpy(code_body_cp, p_code, code_size);
|
|
|
|
* func->code = code_body_cp;
|
|
|
|
*/
|
2021-10-08 03:44:39 +00:00
|
|
|
func->code = (uint8 *)p_code;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
/* Load each local type */
|
|
|
|
p_code = p_code_save;
|
|
|
|
local_type_index = 0;
|
|
|
|
for (j = 0; j < local_set_count; j++) {
|
|
|
|
read_leb_uint32(p_code, buf_code_end, sub_local_count);
|
2022-09-20 04:40:24 +00:00
|
|
|
/* Note: sub_local_count is allowed to be 0 */
|
|
|
|
if (local_type_index > UINT32_MAX - sub_local_count
|
2019-08-08 08:53:56 +00:00
|
|
|
|| local_type_index + sub_local_count > local_count) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2020-08-21 07:11:31 +00:00
|
|
|
"invalid local count");
|
2019-08-08 08:53:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-12-13 07:30:30 +00:00
|
|
|
CHECK_BUF(p_code, buf_code_end, 1);
|
|
|
|
/* 0x7F/0x7E/0x7D/0x7C */
|
|
|
|
type = read_uint8(p_code);
|
2021-12-10 10:13:17 +00:00
|
|
|
if (!is_value_type(type)) {
|
2020-12-15 05:05:39 +00:00
|
|
|
if (type == VALUE_TYPE_V128)
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"v128 value type requires simd feature");
|
2021-04-15 03:29:20 +00:00
|
|
|
else if (type == VALUE_TYPE_FUNCREF
|
|
|
|
|| type == VALUE_TYPE_EXTERNREF)
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"ref value type requires "
|
|
|
|
"reference types feature");
|
2020-12-15 05:05:39 +00:00
|
|
|
else
|
|
|
|
set_error_buf_v(error_buf, error_buf_size,
|
|
|
|
"invalid local type 0x%02X", type);
|
2019-08-08 08:53:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
for (k = 0; k < sub_local_count; k++) {
|
|
|
|
func->local_types[local_type_index++] = type;
|
|
|
|
}
|
|
|
|
}
|
2020-02-18 07:15:24 +00:00
|
|
|
|
2020-07-10 08:29:15 +00:00
|
|
|
func->param_cell_num = func->func_type->param_cell_num;
|
|
|
|
func->ret_cell_num = func->func_type->ret_cell_num;
|
2022-04-03 23:55:37 +00:00
|
|
|
local_cell_num =
|
2020-02-18 07:15:24 +00:00
|
|
|
wasm_get_cell_num(func->local_types, func->local_count);
|
|
|
|
|
2022-04-03 23:55:37 +00:00
|
|
|
if (local_cell_num > UINT16_MAX) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"local count too large");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
func->local_cell_num = (uint16)local_cell_num;
|
|
|
|
|
2020-02-18 07:15:24 +00:00
|
|
|
if (!init_function_local_offsets(func, error_buf, error_buf_size))
|
|
|
|
return false;
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
p_code = p_code_end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p != p_end) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "section size mismatch");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_VERBOSE("Load function section success.\n");
|
|
|
|
return true;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
check_function_index(const WASMModule *module, uint32 function_index,
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
2021-04-15 03:29:20 +00:00
|
|
|
{
|
|
|
|
if (function_index
|
|
|
|
>= module->import_function_count + module->function_count) {
|
|
|
|
set_error_buf_v(error_buf, error_buf_size, "unknown function %d",
|
|
|
|
function_index);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
static bool
|
|
|
|
load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
|
|
|
const uint8 *p = buf, *p_end = buf_end;
|
|
|
|
uint32 table_count, i;
|
2019-11-20 13:16:36 +00:00
|
|
|
uint64 total_size;
|
2019-05-07 02:18:18 +00:00
|
|
|
WASMTable *table;
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, table_count);
|
2021-11-18 09:42:49 +00:00
|
|
|
#if WASM_ENABLE_REF_TYPES == 0
|
|
|
|
if (module->import_table_count + table_count > 1) {
|
2021-04-15 03:29:20 +00:00
|
|
|
/* a total of one table is allowed */
|
2020-06-02 06:53:06 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "multiple tables");
|
|
|
|
return false;
|
|
|
|
}
|
2021-11-18 09:42:49 +00:00
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
if (table_count) {
|
|
|
|
module->table_count = table_count;
|
2019-11-20 13:16:36 +00:00
|
|
|
total_size = sizeof(WASMTable) * (uint64)table_count;
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!(module->tables =
|
|
|
|
loader_malloc(total_size, error_buf, error_buf_size))) {
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load each table */
|
|
|
|
table = module->tables;
|
|
|
|
for (i = 0; i < table_count; i++, table++)
|
|
|
|
if (!load_table(&p, p_end, table, error_buf, error_buf_size))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p != p_end) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "section size mismatch");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_VERBOSE("Load table section success.\n");
|
|
|
|
return true;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
|
|
|
const uint8 *p = buf, *p_end = buf_end;
|
|
|
|
uint32 memory_count, i;
|
2019-11-20 13:16:36 +00:00
|
|
|
uint64 total_size;
|
2019-05-07 02:18:18 +00:00
|
|
|
WASMMemory *memory;
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, memory_count);
|
2020-06-02 06:53:06 +00:00
|
|
|
/* a total of one memory is allowed */
|
|
|
|
if (module->import_memory_count + memory_count > 1) {
|
|
|
|
set_error_buf(error_buf, error_buf_size, "multiple memories");
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
if (memory_count) {
|
|
|
|
module->memory_count = memory_count;
|
2019-11-20 13:16:36 +00:00
|
|
|
total_size = sizeof(WASMMemory) * (uint64)memory_count;
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!(module->memories =
|
|
|
|
loader_malloc(total_size, error_buf, error_buf_size))) {
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load each memory */
|
|
|
|
memory = module->memories;
|
|
|
|
for (i = 0; i < memory_count; i++, memory++)
|
|
|
|
if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p != p_end) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "section size mismatch");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_VERBOSE("Load memory section success.\n");
|
|
|
|
return true;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
|
|
|
const uint8 *p = buf, *p_end = buf_end;
|
|
|
|
uint32 global_count, i;
|
2019-11-20 13:16:36 +00:00
|
|
|
uint64 total_size;
|
2019-05-07 02:18:18 +00:00
|
|
|
WASMGlobal *global;
|
2019-11-27 02:52:12 +00:00
|
|
|
uint8 mutable;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, global_count);
|
|
|
|
|
|
|
|
if (global_count) {
|
|
|
|
module->global_count = global_count;
|
2019-11-20 13:16:36 +00:00
|
|
|
total_size = sizeof(WASMGlobal) * (uint64)global_count;
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!(module->globals =
|
|
|
|
loader_malloc(total_size, error_buf, error_buf_size))) {
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
global = module->globals;
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
for (i = 0; i < global_count; i++, global++) {
|
2019-11-27 02:52:12 +00:00
|
|
|
CHECK_BUF(p, p_end, 2);
|
2019-05-07 02:18:18 +00:00
|
|
|
global->type = read_uint8(p);
|
2019-11-27 02:52:12 +00:00
|
|
|
mutable = read_uint8(p);
|
|
|
|
if (mutable >= 2) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "invalid mutability");
|
2019-11-27 02:52:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
global->is_mutable = mutable ? true : false;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
/* initialize expression */
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!load_init_expr(&p, p_end, &(global->init_expr), global->type,
|
|
|
|
error_buf, error_buf_size))
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
2020-06-02 06:53:06 +00:00
|
|
|
|
|
|
|
if (INIT_EXPR_TYPE_GET_GLOBAL == global->init_expr.init_expr_type) {
|
|
|
|
/**
|
|
|
|
* Currently, constant expressions occurring as initializers
|
|
|
|
* of globals are further constrained in that contained
|
|
|
|
* global.get instructions are
|
|
|
|
* only allowed to refer to imported globals.
|
|
|
|
*/
|
|
|
|
uint32 target_global_index = global->init_expr.u.global_index;
|
|
|
|
if (target_global_index >= module->import_global_count) {
|
|
|
|
set_error_buf(error_buf, error_buf_size, "unknown global");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-10-08 03:44:39 +00:00
|
|
|
else if (INIT_EXPR_TYPE_FUNCREF_CONST
|
|
|
|
== global->init_expr.init_expr_type) {
|
|
|
|
if (!check_function_index(module, global->init_expr.u.ref_index,
|
2021-04-15 03:29:20 +00:00
|
|
|
error_buf, error_buf_size)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p != p_end) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "section size mismatch");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_VERBOSE("Load global section success.\n");
|
|
|
|
return true;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
|
2021-11-08 03:27:26 +00:00
|
|
|
bool is_load_from_file_buf, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
|
|
|
const uint8 *p = buf, *p_end = buf_end;
|
2020-06-02 06:53:06 +00:00
|
|
|
uint32 export_count, i, j, index;
|
2019-11-20 13:16:36 +00:00
|
|
|
uint64 total_size;
|
|
|
|
uint32 str_len;
|
2019-05-07 02:18:18 +00:00
|
|
|
WASMExport *export;
|
2020-06-02 06:53:06 +00:00
|
|
|
const char *name;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, export_count);
|
|
|
|
|
|
|
|
if (export_count) {
|
|
|
|
module->export_count = export_count;
|
2019-11-20 13:16:36 +00:00
|
|
|
total_size = sizeof(WASMExport) * (uint64)export_count;
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!(module->exports =
|
|
|
|
loader_malloc(total_size, error_buf, error_buf_size))) {
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export = module->exports;
|
2021-10-08 03:44:39 +00:00
|
|
|
for (i = 0; i < export_count; i++, export ++) {
|
2022-06-28 08:05:16 +00:00
|
|
|
#if WASM_ENABLE_THREAD_MGR == 0
|
|
|
|
if (p == p_end) {
|
|
|
|
/* export section with inconsistent count:
|
|
|
|
n export declared, but less than n given */
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"length out of bounds");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
read_leb_uint32(p, p_end, str_len);
|
|
|
|
CHECK_BUF(p, p_end, str_len);
|
2020-06-02 06:53:06 +00:00
|
|
|
|
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
name = module->exports[j].name;
|
2021-10-08 03:44:39 +00:00
|
|
|
if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"duplicate export name");
|
|
|
|
return false;
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!(export->name = const_str_list_insert(
|
2021-11-08 03:27:26 +00:00
|
|
|
p, str_len, module, is_load_from_file_buf, error_buf,
|
|
|
|
error_buf_size))) {
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-06-02 06:53:06 +00:00
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
p += str_len;
|
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
export->kind = read_uint8(p);
|
|
|
|
read_leb_uint32(p, p_end, index);
|
|
|
|
export->index = index;
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
switch (export->kind) {
|
2021-02-19 03:14:40 +00:00
|
|
|
/* function index */
|
2019-05-07 02:18:18 +00:00
|
|
|
case EXPORT_KIND_FUNC:
|
2020-08-21 07:11:31 +00:00
|
|
|
if (index >= module->function_count
|
2021-10-08 03:44:39 +00:00
|
|
|
+ module->import_function_count) {
|
2019-05-07 02:18:18 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2020-08-21 07:11:31 +00:00
|
|
|
"unknown function");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-11-05 10:15:15 +00:00
|
|
|
#if WASM_ENABLE_SIMD != 0
|
|
|
|
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
|
|
|
|
/* TODO: check func type, if it has v128 param or result,
|
|
|
|
report error */
|
|
|
|
#endif
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
2021-02-19 03:14:40 +00:00
|
|
|
/* table index */
|
2019-05-07 02:18:18 +00:00
|
|
|
case EXPORT_KIND_TABLE:
|
2021-10-08 03:44:39 +00:00
|
|
|
if (index
|
|
|
|
>= module->table_count + module->import_table_count) {
|
2019-05-07 02:18:18 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2020-08-21 07:11:31 +00:00
|
|
|
"unknown table");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2021-02-19 03:14:40 +00:00
|
|
|
/* memory index */
|
2019-05-07 02:18:18 +00:00
|
|
|
case EXPORT_KIND_MEMORY:
|
2021-10-08 03:44:39 +00:00
|
|
|
if (index
|
|
|
|
>= module->memory_count + module->import_memory_count) {
|
2019-05-07 02:18:18 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2020-08-21 07:11:31 +00:00
|
|
|
"unknown memory");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2021-02-19 03:14:40 +00:00
|
|
|
/* global index */
|
2019-05-07 02:18:18 +00:00
|
|
|
case EXPORT_KIND_GLOBAL:
|
2021-10-08 03:44:39 +00:00
|
|
|
if (index
|
|
|
|
>= module->global_count + module->import_global_count) {
|
2019-05-07 02:18:18 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2020-08-21 07:11:31 +00:00
|
|
|
"unknown global");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2020-08-21 07:11:31 +00:00
|
|
|
"invalid export kind");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p != p_end) {
|
2021-10-08 03:44:39 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "section size mismatch");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_VERBOSE("Load export section success.\n");
|
|
|
|
return true;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2021-04-15 03:29:20 +00:00
|
|
|
{
|
2021-11-18 09:42:49 +00:00
|
|
|
#if WASM_ENABLE_REF_TYPES == 0
|
|
|
|
if (table_index != 0) {
|
2021-06-28 03:59:49 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "zero byte expected");
|
2021-04-15 03:29:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-11-18 09:42:49 +00:00
|
|
|
#endif
|
2021-04-15 03:29:20 +00:00
|
|
|
|
|
|
|
if (table_index >= module->import_table_count + module->table_count) {
|
|
|
|
set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
|
|
|
|
table_index);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
|
|
|
|
uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
|
2021-04-15 03:29:20 +00:00
|
|
|
{
|
|
|
|
const uint8 *p = *p_buf, *p_end = buf_end;
|
|
|
|
uint32 table_index;
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, table_index);
|
|
|
|
if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p_table_index = table_index;
|
|
|
|
*p_buf = p;
|
|
|
|
return true;
|
|
|
|
fail:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if WASM_ENABLE_REF_TYPES != 0
|
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
load_elem_type(const uint8 **p_buf, const uint8 *buf_end, uint32 *p_elem_type,
|
|
|
|
bool elemkind_zero, char *error_buf, uint32 error_buf_size)
|
2021-04-15 03:29:20 +00:00
|
|
|
{
|
|
|
|
const uint8 *p = *p_buf, *p_end = buf_end;
|
|
|
|
uint8 elem_type;
|
|
|
|
|
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
elem_type = read_uint8(p);
|
|
|
|
if ((elemkind_zero && elem_type != 0)
|
|
|
|
|| (!elemkind_zero && elem_type != VALUE_TYPE_FUNCREF
|
|
|
|
&& elem_type != VALUE_TYPE_EXTERNREF)) {
|
|
|
|
set_error_buf(error_buf, error_buf_size, "invalid reference type");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (elemkind_zero)
|
|
|
|
*p_elem_type = VALUE_TYPE_FUNCREF;
|
|
|
|
else
|
|
|
|
*p_elem_type = elem_type;
|
|
|
|
*p_buf = p;
|
|
|
|
return true;
|
|
|
|
fail:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif /* WASM_ENABLE_REF_TYPES != 0*/
|
|
|
|
|
|
|
|
static bool
|
|
|
|
load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
|
|
|
|
WASMModule *module, WASMTableSeg *table_segment,
|
2021-10-08 03:44:39 +00:00
|
|
|
bool use_init_expr, char *error_buf, uint32 error_buf_size)
|
2021-04-15 03:29:20 +00:00
|
|
|
{
|
|
|
|
const uint8 *p = *p_buf, *p_end = buf_end;
|
|
|
|
uint32 function_count, function_index = 0, i;
|
|
|
|
uint64 total_size;
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, function_count);
|
|
|
|
table_segment->function_count = function_count;
|
|
|
|
total_size = sizeof(uint32) * (uint64)function_count;
|
|
|
|
if (total_size > 0
|
2021-10-08 03:44:39 +00:00
|
|
|
&& !(table_segment->func_indexes = (uint32 *)loader_malloc(
|
|
|
|
total_size, error_buf, error_buf_size))) {
|
2021-04-15 03:29:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < function_count; i++) {
|
|
|
|
InitializerExpression init_expr = { 0 };
|
|
|
|
|
|
|
|
#if WASM_ENABLE_REF_TYPES != 0
|
2021-11-18 09:42:49 +00:00
|
|
|
if (!use_init_expr) {
|
2021-04-15 03:29:20 +00:00
|
|
|
read_leb_uint32(p, p_end, function_index);
|
|
|
|
}
|
|
|
|
else {
|
2021-11-18 09:42:49 +00:00
|
|
|
if (!load_init_expr(&p, p_end, &init_expr, table_segment->elem_type,
|
|
|
|
error_buf, error_buf_size))
|
|
|
|
return false;
|
2021-04-15 03:29:20 +00:00
|
|
|
|
2021-11-18 09:42:49 +00:00
|
|
|
function_index = init_expr.u.ref_index;
|
2021-04-15 03:29:20 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
read_leb_uint32(p, p_end, function_index);
|
2022-12-27 04:59:17 +00:00
|
|
|
(void)use_init_expr;
|
2021-04-15 03:29:20 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* since we are using -1 to indicate ref.null */
|
|
|
|
if (init_expr.init_expr_type != INIT_EXPR_TYPE_REFNULL_CONST
|
|
|
|
&& !check_function_index(module, function_index, error_buf,
|
|
|
|
error_buf_size)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
table_segment->func_indexes[i] = function_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p_buf = p;
|
|
|
|
return true;
|
|
|
|
fail:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
|
|
|
|
WASMModule *module, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
|
|
|
const uint8 *p = buf, *p_end = buf_end;
|
2021-04-15 03:29:20 +00:00
|
|
|
uint32 table_segment_count, i;
|
2019-11-20 13:16:36 +00:00
|
|
|
uint64 total_size;
|
2019-05-07 02:18:18 +00:00
|
|
|
WASMTableSeg *table_segment;
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, table_segment_count);
|
|
|
|
|
|
|
|
if (table_segment_count) {
|
|
|
|
module->table_seg_count = table_segment_count;
|
2019-11-20 13:16:36 +00:00
|
|
|
total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!(module->table_segments =
|
|
|
|
loader_malloc(total_size, error_buf, error_buf_size))) {
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
table_segment = module->table_segments;
|
|
|
|
for (i = 0; i < table_segment_count; i++, table_segment++) {
|
2019-11-27 02:52:12 +00:00
|
|
|
if (p >= p_end) {
|
2021-04-15 03:29:20 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"invalid value type or "
|
|
|
|
"invalid elements segment kind");
|
2020-06-02 06:53:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
#if WASM_ENABLE_REF_TYPES != 0
|
2021-11-18 09:42:49 +00:00
|
|
|
read_leb_uint32(p, p_end, table_segment->mode);
|
|
|
|
/* last three bits */
|
|
|
|
table_segment->mode = table_segment->mode & 0x07;
|
|
|
|
switch (table_segment->mode) {
|
|
|
|
/* elemkind/elemtype + active */
|
|
|
|
case 0:
|
|
|
|
case 4:
|
|
|
|
table_segment->elem_type = VALUE_TYPE_FUNCREF;
|
|
|
|
table_segment->table_index = 0;
|
|
|
|
|
|
|
|
if (!check_table_index(module, table_segment->table_index,
|
|
|
|
error_buf, error_buf_size))
|
2021-04-15 03:29:20 +00:00
|
|
|
return false;
|
2021-11-18 09:42:49 +00:00
|
|
|
if (!load_init_expr(&p, p_end, &table_segment->base_offset,
|
|
|
|
VALUE_TYPE_I32, error_buf,
|
|
|
|
error_buf_size))
|
|
|
|
return false;
|
|
|
|
if (!load_func_index_vec(&p, p_end, module, table_segment,
|
|
|
|
table_segment->mode == 0 ? false
|
|
|
|
: true,
|
|
|
|
error_buf, error_buf_size))
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
/* elemkind + passive/declarative */
|
|
|
|
case 1:
|
|
|
|
case 3:
|
|
|
|
if (!load_elem_type(&p, p_end, &table_segment->elem_type,
|
|
|
|
true, error_buf, error_buf_size))
|
|
|
|
return false;
|
|
|
|
if (!load_func_index_vec(&p, p_end, module, table_segment,
|
|
|
|
false, error_buf, error_buf_size))
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
/* elemkind/elemtype + table_idx + active */
|
|
|
|
case 2:
|
|
|
|
case 6:
|
|
|
|
if (!load_table_index(&p, p_end, module,
|
|
|
|
&table_segment->table_index,
|
|
|
|
error_buf, error_buf_size))
|
|
|
|
return false;
|
|
|
|
if (!load_init_expr(&p, p_end, &table_segment->base_offset,
|
|
|
|
VALUE_TYPE_I32, error_buf,
|
|
|
|
error_buf_size))
|
|
|
|
return false;
|
|
|
|
if (!load_elem_type(&p, p_end, &table_segment->elem_type,
|
|
|
|
table_segment->mode == 2 ? true : false,
|
|
|
|
error_buf, error_buf_size))
|
|
|
|
return false;
|
|
|
|
if (!load_func_index_vec(&p, p_end, module, table_segment,
|
|
|
|
table_segment->mode == 2 ? false
|
|
|
|
: true,
|
|
|
|
error_buf, error_buf_size))
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
case 7:
|
|
|
|
if (!load_elem_type(&p, p_end, &table_segment->elem_type,
|
|
|
|
false, error_buf, error_buf_size))
|
|
|
|
return false;
|
|
|
|
if (!load_func_index_vec(&p, p_end, module, table_segment,
|
|
|
|
true, error_buf, error_buf_size))
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"unknown element segment kind");
|
2020-06-02 06:53:06 +00:00
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
2021-11-18 09:42:49 +00:00
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* like: 00 41 05 0b 04 00 01 00 01
|
|
|
|
* for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
|
|
|
|
*/
|
|
|
|
if (!load_table_index(&p, p_end, module,
|
|
|
|
&table_segment->table_index, error_buf,
|
|
|
|
error_buf_size))
|
|
|
|
return false;
|
|
|
|
if (!load_init_expr(&p, p_end, &table_segment->base_offset,
|
|
|
|
VALUE_TYPE_I32, error_buf, error_buf_size))
|
|
|
|
return false;
|
|
|
|
if (!load_func_index_vec(&p, p_end, module, table_segment, false,
|
|
|
|
error_buf, error_buf_size))
|
|
|
|
return false;
|
|
|
|
#endif /* WASM_ENABLE_REF_TYPES != 0 */
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p != p_end) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "section size mismatch");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_VERBOSE("Load table segment section success.\n");
|
|
|
|
return true;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
|
2021-10-08 03:44:39 +00:00
|
|
|
WASMModule *module, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
|
|
|
const uint8 *p = buf, *p_end = buf_end;
|
|
|
|
uint32 data_seg_count, i, mem_index, data_seg_len;
|
2019-11-20 13:16:36 +00:00
|
|
|
uint64 total_size;
|
2019-05-07 02:18:18 +00:00
|
|
|
WASMDataSeg *dataseg;
|
|
|
|
InitializerExpression init_expr;
|
2020-06-02 06:53:06 +00:00
|
|
|
#if WASM_ENABLE_BULK_MEMORY != 0
|
|
|
|
bool is_passive = false;
|
|
|
|
uint32 mem_flag;
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, data_seg_count);
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
#if WASM_ENABLE_BULK_MEMORY != 0
|
|
|
|
if ((module->data_seg_count1 != 0)
|
|
|
|
&& (data_seg_count != module->data_seg_count1)) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"data count and data section have inconsistent lengths");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
if (data_seg_count) {
|
|
|
|
module->data_seg_count = data_seg_count;
|
2021-10-08 03:44:39 +00:00
|
|
|
total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
|
|
|
|
if (!(module->data_segments =
|
|
|
|
loader_malloc(total_size, error_buf, error_buf_size))) {
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < data_seg_count; i++) {
|
|
|
|
read_leb_uint32(p, p_end, mem_index);
|
2020-06-02 06:53:06 +00:00
|
|
|
#if WASM_ENABLE_BULK_MEMORY != 0
|
|
|
|
is_passive = false;
|
|
|
|
mem_flag = mem_index & 0x03;
|
|
|
|
switch (mem_flag) {
|
|
|
|
case 0x01:
|
|
|
|
is_passive = true;
|
|
|
|
break;
|
|
|
|
case 0x00:
|
|
|
|
/* no memory index, treat index as 0 */
|
|
|
|
mem_index = 0;
|
|
|
|
goto check_mem_index;
|
|
|
|
case 0x02:
|
|
|
|
/* read following memory index */
|
|
|
|
read_leb_uint32(p, p_end, mem_index);
|
2021-10-08 03:44:39 +00:00
|
|
|
check_mem_index:
|
2020-06-02 06:53:06 +00:00
|
|
|
if (mem_index
|
|
|
|
>= module->import_memory_count + module->memory_count) {
|
2021-04-15 03:29:20 +00:00
|
|
|
set_error_buf_v(error_buf, error_buf_size,
|
|
|
|
"unknown memory %d", mem_index);
|
2020-06-02 06:53:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x03:
|
|
|
|
default:
|
|
|
|
set_error_buf(error_buf, error_buf_size, "unknown memory");
|
2021-10-08 03:44:39 +00:00
|
|
|
return false;
|
2020-06-02 06:53:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (mem_index
|
|
|
|
>= module->import_memory_count + module->memory_count) {
|
2021-04-15 03:29:20 +00:00
|
|
|
set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
|
|
|
|
mem_index);
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
|
|
|
#endif /* WASM_ENABLE_BULK_MEMORY */
|
|
|
|
|
|
|
|
#if WASM_ENABLE_BULK_MEMORY != 0
|
|
|
|
if (!is_passive)
|
|
|
|
#endif
|
|
|
|
if (!load_init_expr(&p, p_end, &init_expr, VALUE_TYPE_I32,
|
|
|
|
error_buf, error_buf_size))
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, data_seg_len);
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!(dataseg = module->data_segments[i] = loader_malloc(
|
|
|
|
sizeof(WASMDataSeg), error_buf, error_buf_size))) {
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
#if WASM_ENABLE_BULK_MEMORY != 0
|
|
|
|
dataseg->is_passive = is_passive;
|
|
|
|
if (!is_passive)
|
|
|
|
#endif
|
|
|
|
{
|
2021-10-08 03:44:39 +00:00
|
|
|
bh_memcpy_s(&dataseg->base_offset,
|
|
|
|
sizeof(InitializerExpression), &init_expr,
|
|
|
|
sizeof(InitializerExpression));
|
2020-06-02 06:53:06 +00:00
|
|
|
|
|
|
|
dataseg->memory_index = mem_index;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
dataseg->data_length = data_seg_len;
|
|
|
|
CHECK_BUF(p, p_end, data_seg_len);
|
2021-10-08 03:44:39 +00:00
|
|
|
dataseg->data = (uint8 *)p;
|
2019-05-07 02:18:18 +00:00
|
|
|
p += data_seg_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p != p_end) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "section size mismatch");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_VERBOSE("Load data segment section success.\n");
|
|
|
|
return true;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
#if WASM_ENABLE_BULK_MEMORY != 0
|
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
load_datacount_section(const uint8 *buf, const uint8 *buf_end,
|
|
|
|
WASMModule *module, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2020-06-02 06:53:06 +00:00
|
|
|
{
|
|
|
|
const uint8 *p = buf, *p_end = buf_end;
|
|
|
|
uint32 data_seg_count1 = 0;
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, data_seg_count1);
|
|
|
|
module->data_seg_count1 = data_seg_count1;
|
|
|
|
|
|
|
|
if (p != p_end) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "section size mismatch");
|
2020-06-02 06:53:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_VERBOSE("Load datacount section success.\n");
|
|
|
|
return true;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
|
|
|
|
const uint8 *buf_func_end, WASMModule *module,
|
2019-05-07 02:18:18 +00:00
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
2019-10-11 07:25:23 +00:00
|
|
|
const uint8 *p = buf, *p_end = buf_end;
|
|
|
|
const uint8 *p_func = buf_func;
|
|
|
|
uint32 func_count = 0, code_count;
|
|
|
|
|
|
|
|
/* code has been loaded in function section, so pass it here, just check
|
|
|
|
* whether function and code section have inconsistent lengths */
|
|
|
|
read_leb_uint32(p, p_end, code_count);
|
|
|
|
|
|
|
|
if (buf_func)
|
|
|
|
read_leb_uint32(p_func, buf_func_end, func_count);
|
|
|
|
|
|
|
|
if (func_count != code_count) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"function and code section have inconsistent lengths");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
LOG_VERBOSE("Load code segment section success.\n");
|
2022-12-27 04:59:17 +00:00
|
|
|
(void)module;
|
2019-05-07 02:18:18 +00:00
|
|
|
return true;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
|
|
|
const uint8 *p = buf, *p_end = buf_end;
|
2020-04-30 09:52:11 +00:00
|
|
|
WASMType *type;
|
2019-05-07 02:18:18 +00:00
|
|
|
uint32 start_function;
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, start_function);
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
if (start_function
|
|
|
|
>= module->function_count + module->import_function_count) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "unknown function");
|
2020-04-30 09:52:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start_function < module->import_function_count)
|
|
|
|
type = module->import_functions[start_function].u.function.func_type;
|
|
|
|
else
|
2021-10-08 03:44:39 +00:00
|
|
|
type = module->functions[start_function - module->import_function_count]
|
|
|
|
->func_type;
|
2020-04-30 09:52:11 +00:00
|
|
|
if (type->param_count != 0 || type->result_count != 0) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "invalid start function");
|
2020-04-30 09:52:11 +00:00
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 09:52:11 +00:00
|
|
|
module->start_function = start_function;
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
if (p != p_end) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "section size mismatch");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_VERBOSE("Load start section success.\n");
|
|
|
|
return true;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 08:12:09 +00:00
|
|
|
#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
|
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
|
2021-11-08 03:27:26 +00:00
|
|
|
bool is_load_from_file_buf, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2020-09-23 08:12:09 +00:00
|
|
|
{
|
|
|
|
const uint8 *p = buf, *p_end = buf_end;
|
|
|
|
uint32 name_type, subsection_size;
|
|
|
|
uint32 previous_name_type = 0;
|
|
|
|
uint32 num_func_name;
|
|
|
|
uint32 func_index;
|
|
|
|
uint32 previous_func_index = ~0U;
|
|
|
|
uint32 func_name_len;
|
|
|
|
uint32 name_index;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if (p >= p_end) {
|
|
|
|
set_error_buf(error_buf, error_buf_size, "unexpected end");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (p < p_end) {
|
|
|
|
read_leb_uint32(p, p_end, name_type);
|
|
|
|
if (i != 0) {
|
|
|
|
if (name_type == previous_name_type) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"duplicate sub-section");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (name_type < previous_name_type) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"out-of-order sub-section");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
previous_name_type = name_type;
|
|
|
|
read_leb_uint32(p, p_end, subsection_size);
|
|
|
|
CHECK_BUF(p, p_end, subsection_size);
|
|
|
|
switch (name_type) {
|
|
|
|
case SUB_SECTION_TYPE_FUNC:
|
|
|
|
if (subsection_size) {
|
|
|
|
read_leb_uint32(p, p_end, num_func_name);
|
|
|
|
for (name_index = 0; name_index < num_func_name;
|
|
|
|
name_index++) {
|
|
|
|
read_leb_uint32(p, p_end, func_index);
|
|
|
|
if (func_index == previous_func_index) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"duplicate function name");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (func_index < previous_func_index
|
|
|
|
&& previous_func_index != ~0U) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"out-of-order function index ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
previous_func_index = func_index;
|
|
|
|
read_leb_uint32(p, p_end, func_name_len);
|
|
|
|
CHECK_BUF(p, p_end, func_name_len);
|
2021-01-28 08:16:02 +00:00
|
|
|
/* Skip the import functions */
|
2022-12-30 06:37:04 +00:00
|
|
|
if (func_index >= module->import_function_count) {
|
|
|
|
func_index -= module->import_function_count;
|
2020-09-23 08:12:09 +00:00
|
|
|
if (func_index >= module->function_count) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"out-of-range function index");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!(module->functions[func_index]->field_name =
|
2021-11-08 03:27:26 +00:00
|
|
|
const_str_list_insert(
|
|
|
|
p, func_name_len, module,
|
|
|
|
is_load_from_file_buf, error_buf,
|
|
|
|
error_buf_size))) {
|
2020-09-23 08:12:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p += func_name_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2021-10-08 03:44:39 +00:00
|
|
|
case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
|
|
|
|
*/
|
|
|
|
case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
|
2020-09-23 08:12:09 +00:00
|
|
|
default:
|
|
|
|
p = p + subsection_size;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
fail:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-11-27 02:52:12 +00:00
|
|
|
static bool
|
|
|
|
load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
|
2021-11-08 03:27:26 +00:00
|
|
|
bool is_load_from_file_buf, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2019-11-27 02:52:12 +00:00
|
|
|
{
|
|
|
|
const uint8 *p = buf, *p_end = buf_end;
|
2022-06-10 13:51:13 +00:00
|
|
|
char section_name[32];
|
|
|
|
uint32 name_len, buffer_len;
|
2019-11-27 02:52:12 +00:00
|
|
|
|
|
|
|
if (p >= p_end) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "unexpected end");
|
2019-11-27 02:52:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, name_len);
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
if (name_len == 0 || p + name_len > p_end) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "unexpected end");
|
2019-11-27 02:52:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!check_utf8_str(p, name_len)) {
|
2021-10-08 03:44:39 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
|
2019-11-27 02:52:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-10 13:51:13 +00:00
|
|
|
buffer_len = sizeof(section_name);
|
|
|
|
memset(section_name, 0, buffer_len);
|
|
|
|
if (name_len < buffer_len) {
|
|
|
|
bh_memcpy_s(section_name, buffer_len, p, name_len);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
|
|
|
|
memset(section_name + buffer_len - 4, '.', 3);
|
|
|
|
}
|
|
|
|
|
2020-09-23 08:12:09 +00:00
|
|
|
#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
|
|
|
|
if (memcmp(p, "name", 4) == 0) {
|
2021-10-26 08:32:52 +00:00
|
|
|
module->name_section_buf = buf;
|
|
|
|
module->name_section_buf_end = buf_end;
|
2020-09-23 08:12:09 +00:00
|
|
|
p += name_len;
|
2021-11-08 03:27:26 +00:00
|
|
|
handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf,
|
|
|
|
error_buf_size);
|
2022-06-10 13:51:13 +00:00
|
|
|
LOG_VERBOSE("Load custom name section success.");
|
|
|
|
return true;
|
2020-09-23 08:12:09 +00:00
|
|
|
}
|
|
|
|
#endif
|
2022-06-10 13:51:13 +00:00
|
|
|
|
|
|
|
#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
|
|
|
|
{
|
|
|
|
WASMCustomSection *section =
|
|
|
|
loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
|
|
|
|
|
|
|
|
if (!section) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
section->name_addr = (char *)p;
|
|
|
|
section->name_len = name_len;
|
|
|
|
section->content_addr = (uint8 *)(p + name_len);
|
2022-09-29 06:02:58 +00:00
|
|
|
section->content_len = (uint32)(p_end - p - name_len);
|
2022-06-10 13:51:13 +00:00
|
|
|
|
|
|
|
section->next = module->custom_section_list;
|
|
|
|
module->custom_section_list = section;
|
|
|
|
LOG_VERBOSE("Load custom section [%s] success.", section_name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
LOG_VERBOSE("Ignore custom section [%s].", section_name);
|
|
|
|
|
2022-12-27 04:59:17 +00:00
|
|
|
(void)is_load_from_file_buf;
|
|
|
|
(void)module;
|
2019-11-27 02:52:12 +00:00
|
|
|
return true;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2019-11-27 02:52:12 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 08:03:50 +00:00
|
|
|
static void
|
|
|
|
calculate_global_data_offset(WASMModule *module)
|
|
|
|
{
|
|
|
|
uint32 i, data_offset;
|
|
|
|
|
|
|
|
data_offset = 0;
|
|
|
|
for (i = 0; i < module->import_global_count; i++) {
|
|
|
|
WASMGlobalImport *import_global =
|
|
|
|
&((module->import_globals + i)->u.global);
|
2022-10-18 02:59:28 +00:00
|
|
|
#if WASM_ENABLE_FAST_JIT != 0
|
2022-08-02 08:03:50 +00:00
|
|
|
import_global->data_offset = data_offset;
|
2022-10-18 02:59:28 +00:00
|
|
|
#endif
|
2022-08-02 08:03:50 +00:00
|
|
|
data_offset += wasm_value_type_size(import_global->type);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < module->global_count; i++) {
|
|
|
|
WASMGlobal *global = module->globals + i;
|
2022-10-18 02:59:28 +00:00
|
|
|
#if WASM_ENABLE_FAST_JIT != 0
|
2022-08-02 08:03:50 +00:00
|
|
|
global->data_offset = data_offset;
|
2022-10-18 02:59:28 +00:00
|
|
|
#endif
|
2022-08-02 08:03:50 +00:00
|
|
|
data_offset += wasm_value_type_size(global->type);
|
|
|
|
}
|
2022-10-18 02:59:28 +00:00
|
|
|
|
|
|
|
module->global_data_size = data_offset;
|
2022-08-02 08:03:50 +00:00
|
|
|
}
|
2022-10-18 02:59:28 +00:00
|
|
|
|
2022-12-19 03:24:46 +00:00
|
|
|
#if WASM_ENABLE_FAST_JIT != 0
|
|
|
|
static bool
|
|
|
|
init_fast_jit_functions(WASMModule *module, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2022-10-18 12:17:34 +00:00
|
|
|
{
|
2022-12-19 03:24:46 +00:00
|
|
|
#if WASM_ENABLE_LAZY_JIT != 0
|
|
|
|
JitGlobals *jit_globals = jit_compiler_get_jit_globals();
|
|
|
|
#endif
|
|
|
|
uint32 i;
|
2022-10-18 12:17:34 +00:00
|
|
|
|
2022-12-19 03:24:46 +00:00
|
|
|
if (!module->function_count)
|
|
|
|
return true;
|
2022-10-18 12:17:34 +00:00
|
|
|
|
2022-12-19 03:24:46 +00:00
|
|
|
if (!(module->fast_jit_func_ptrs =
|
|
|
|
loader_malloc(sizeof(void *) * module->function_count, error_buf,
|
|
|
|
error_buf_size))) {
|
|
|
|
return false;
|
2022-10-18 12:17:34 +00:00
|
|
|
}
|
2022-11-21 02:42:18 +00:00
|
|
|
|
2022-12-19 03:24:46 +00:00
|
|
|
#if WASM_ENABLE_LAZY_JIT != 0
|
|
|
|
for (i = 0; i < module->function_count; i++) {
|
|
|
|
module->fast_jit_func_ptrs[i] =
|
|
|
|
jit_globals->compile_fast_jit_and_then_call;
|
|
|
|
}
|
|
|
|
#endif
|
2022-11-22 06:04:48 +00:00
|
|
|
|
2022-12-19 03:24:46 +00:00
|
|
|
for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
|
|
|
|
if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"init fast jit thread lock failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
module->fast_jit_thread_locks_inited[i] = true;
|
2022-11-22 06:04:48 +00:00
|
|
|
}
|
2022-12-19 03:24:46 +00:00
|
|
|
|
|
|
|
return true;
|
2022-10-18 12:17:34 +00:00
|
|
|
}
|
2022-12-19 03:24:46 +00:00
|
|
|
#endif /* end of WASM_ENABLE_FAST_JIT != 0 */
|
2022-10-18 12:17:34 +00:00
|
|
|
|
2022-12-19 03:24:46 +00:00
|
|
|
#if WASM_ENABLE_JIT != 0
|
2022-10-18 02:59:28 +00:00
|
|
|
static bool
|
2022-12-19 03:24:46 +00:00
|
|
|
init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2022-10-18 02:59:28 +00:00
|
|
|
{
|
2023-02-02 10:16:01 +00:00
|
|
|
LLVMJITOptions llvm_jit_options = wasm_runtime_get_llvm_jit_options();
|
2022-10-18 02:59:28 +00:00
|
|
|
AOTCompOption option = { 0 };
|
2022-10-18 12:17:34 +00:00
|
|
|
char *aot_last_error;
|
2022-10-18 02:59:28 +00:00
|
|
|
uint64 size;
|
|
|
|
|
2022-12-19 03:24:46 +00:00
|
|
|
if (module->function_count == 0)
|
|
|
|
return true;
|
|
|
|
|
Implement the segue optimization for LLVM AOT/JIT (#2230)
Segue is an optimization technology which uses x86 segment register to store
the WebAssembly linear memory base address, so as to remove most of the cost
of SFI (Software-based Fault Isolation) base addition and free up a general
purpose register, by this way it may:
- Improve the performance of JIT/AOT
- Reduce the footprint of JIT/AOT, the JIT/AOT code generated is smaller
- Reduce the compilation time of JIT/AOT
This PR uses the x86-64 GS segment register to apply the optimization, currently
it supports linux and linux-sgx platforms on x86-64 target. By default it is disabled,
developer can use the option below to enable it for wamrc and iwasm(with LLVM
JIT enabled):
```bash
wamrc --enable-segue=[<flags>] -o output_file wasm_file
iwasm --enable-segue=[<flags>] wasm_file [args...]
```
`flags` can be:
i32.load, i64.load, f32.load, f64.load, v128.load,
i32.store, i64.store, f32.store, f64.store, v128.store
Use comma to separate them, e.g. `--enable-segue=i32.load,i64.store`,
and `--enable-segue` means all flags are added.
Acknowledgement:
Many thanks to Intel Labs, UC San Diego and UT Austin teams for introducing this
technology and the great support and guidance!
Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
Co-authored-by: Vahldiek-oberwagner, Anjo Lucas <anjo.lucas.vahldiek-oberwagner@intel.com>
2023-05-26 02:13:33 +00:00
|
|
|
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
|
2022-12-19 03:24:46 +00:00
|
|
|
if (os_mutex_init(&module->tierup_wait_lock) != 0) {
|
|
|
|
set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
|
|
|
|
return false;
|
2022-11-21 02:42:18 +00:00
|
|
|
}
|
2022-12-19 03:24:46 +00:00
|
|
|
if (os_cond_init(&module->tierup_wait_cond) != 0) {
|
|
|
|
set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
|
|
|
|
os_mutex_destroy(&module->tierup_wait_lock);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
module->tierup_wait_lock_inited = true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
size = sizeof(void *) * (uint64)module->function_count
|
|
|
|
+ sizeof(bool) * (uint64)module->function_count;
|
|
|
|
if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
module->func_ptrs_compiled =
|
|
|
|
(bool *)((uint8 *)module->func_ptrs
|
|
|
|
+ sizeof(void *) * module->function_count);
|
2022-10-18 02:59:28 +00:00
|
|
|
|
|
|
|
module->comp_data = aot_create_comp_data(module);
|
|
|
|
if (!module->comp_data) {
|
|
|
|
aot_last_error = aot_get_last_error();
|
|
|
|
bh_assert(aot_last_error != NULL);
|
|
|
|
set_error_buf(error_buf, error_buf_size, aot_last_error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
option.is_jit_mode = true;
|
2023-02-02 10:16:01 +00:00
|
|
|
|
|
|
|
llvm_jit_options = wasm_runtime_get_llvm_jit_options();
|
|
|
|
option.opt_level = llvm_jit_options.opt_level;
|
|
|
|
option.size_level = llvm_jit_options.size_level;
|
Implement the segue optimization for LLVM AOT/JIT (#2230)
Segue is an optimization technology which uses x86 segment register to store
the WebAssembly linear memory base address, so as to remove most of the cost
of SFI (Software-based Fault Isolation) base addition and free up a general
purpose register, by this way it may:
- Improve the performance of JIT/AOT
- Reduce the footprint of JIT/AOT, the JIT/AOT code generated is smaller
- Reduce the compilation time of JIT/AOT
This PR uses the x86-64 GS segment register to apply the optimization, currently
it supports linux and linux-sgx platforms on x86-64 target. By default it is disabled,
developer can use the option below to enable it for wamrc and iwasm(with LLVM
JIT enabled):
```bash
wamrc --enable-segue=[<flags>] -o output_file wasm_file
iwasm --enable-segue=[<flags>] wasm_file [args...]
```
`flags` can be:
i32.load, i64.load, f32.load, f64.load, v128.load,
i32.store, i64.store, f32.store, f64.store, v128.store
Use comma to separate them, e.g. `--enable-segue=i32.load,i64.store`,
and `--enable-segue` means all flags are added.
Acknowledgement:
Many thanks to Intel Labs, UC San Diego and UT Austin teams for introducing this
technology and the great support and guidance!
Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
Co-authored-by: Vahldiek-oberwagner, Anjo Lucas <anjo.lucas.vahldiek-oberwagner@intel.com>
2023-05-26 02:13:33 +00:00
|
|
|
option.segue_flags = llvm_jit_options.segue_flags;
|
2023-02-02 10:16:01 +00:00
|
|
|
|
2022-10-18 02:59:28 +00:00
|
|
|
#if WASM_ENABLE_BULK_MEMORY != 0
|
|
|
|
option.enable_bulk_memory = true;
|
|
|
|
#endif
|
|
|
|
#if WASM_ENABLE_THREAD_MGR != 0
|
|
|
|
option.enable_thread_mgr = true;
|
|
|
|
#endif
|
|
|
|
#if WASM_ENABLE_TAIL_CALL != 0
|
|
|
|
option.enable_tail_call = true;
|
|
|
|
#endif
|
|
|
|
#if WASM_ENABLE_SIMD != 0
|
|
|
|
option.enable_simd = true;
|
|
|
|
#endif
|
|
|
|
#if WASM_ENABLE_REF_TYPES != 0
|
|
|
|
option.enable_ref_types = true;
|
|
|
|
#endif
|
|
|
|
option.enable_aux_stack_check = true;
|
|
|
|
#if (WASM_ENABLE_PERF_PROFILING != 0) || (WASM_ENABLE_DUMP_CALL_STACK != 0)
|
|
|
|
option.enable_aux_stack_frame = true;
|
2022-08-02 08:03:50 +00:00
|
|
|
#endif
|
2023-02-01 03:52:15 +00:00
|
|
|
#if WASM_ENABLE_MEMORY_PROFILING != 0
|
|
|
|
option.enable_stack_estimation = true;
|
|
|
|
#endif
|
2022-08-02 08:03:50 +00:00
|
|
|
|
2022-10-18 02:59:28 +00:00
|
|
|
module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
|
|
|
|
if (!module->comp_ctx) {
|
|
|
|
aot_last_error = aot_get_last_error();
|
|
|
|
bh_assert(aot_last_error != NULL);
|
|
|
|
set_error_buf(error_buf, error_buf_size, aot_last_error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-12-19 03:24:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
|
|
|
{
|
|
|
|
char *aot_last_error;
|
|
|
|
uint32 i;
|
|
|
|
|
|
|
|
if (module->function_count == 0)
|
|
|
|
return true;
|
|
|
|
|
2022-10-18 02:59:28 +00:00
|
|
|
if (!aot_compile_wasm(module->comp_ctx)) {
|
|
|
|
aot_last_error = aot_get_last_error();
|
|
|
|
bh_assert(aot_last_error != NULL);
|
|
|
|
set_error_buf(error_buf, error_buf_size, aot_last_error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-12-19 03:24:46 +00:00
|
|
|
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
|
|
|
|
if (module->orcjit_stop_compiling)
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bh_print_time("Begin to lookup llvm jit functions");
|
2022-10-18 12:17:34 +00:00
|
|
|
|
|
|
|
for (i = 0; i < module->function_count; i++) {
|
2022-10-18 02:59:28 +00:00
|
|
|
LLVMOrcJITTargetAddress func_addr = 0;
|
2022-10-18 12:17:34 +00:00
|
|
|
LLVMErrorRef error;
|
|
|
|
char func_name[48];
|
2022-10-18 02:59:28 +00:00
|
|
|
|
|
|
|
snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
|
2022-10-18 12:17:34 +00:00
|
|
|
error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
|
|
|
|
func_name);
|
|
|
|
if (error != LLVMErrorSuccess) {
|
2022-10-18 02:59:28 +00:00
|
|
|
char *err_msg = LLVMGetErrorMessage(error);
|
|
|
|
set_error_buf_v(error_buf, error_buf_size,
|
2022-12-19 03:24:46 +00:00
|
|
|
"failed to compile llvm jit function: %s", err_msg);
|
2022-10-18 02:59:28 +00:00
|
|
|
LLVMDisposeErrorMessage(err_msg);
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-18 12:17:34 +00:00
|
|
|
|
2022-10-18 02:59:28 +00:00
|
|
|
/**
|
|
|
|
* No need to lock the func_ptr[func_idx] here as it is basic
|
|
|
|
* data type, the load/store for it can be finished by one cpu
|
|
|
|
* instruction, and there can be only one cpu instruction
|
|
|
|
* loading/storing at the same time.
|
|
|
|
*/
|
|
|
|
module->func_ptrs[i] = (void *)func_addr;
|
2022-12-19 03:24:46 +00:00
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
|
2023-02-02 10:16:01 +00:00
|
|
|
module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
|
|
|
|
|
2022-12-19 03:24:46 +00:00
|
|
|
if (module->orcjit_stop_compiling)
|
|
|
|
return false;
|
|
|
|
#endif
|
2022-10-18 02:59:28 +00:00
|
|
|
}
|
2022-10-18 12:17:34 +00:00
|
|
|
|
2022-12-19 03:24:46 +00:00
|
|
|
bh_print_time("End lookup llvm jit functions");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif /* end of WASM_ENABLE_JIT != 0 */
|
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
|
|
|
|
&& WASM_ENABLE_LAZY_JIT != 0
|
|
|
|
static void *
|
|
|
|
init_llvm_jit_functions_stage2_callback(void *arg)
|
|
|
|
{
|
|
|
|
WASMModule *module = (WASMModule *)arg;
|
|
|
|
char error_buf[128];
|
|
|
|
uint32 error_buf_size = (uint32)sizeof(error_buf);
|
|
|
|
|
|
|
|
if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
|
|
|
|
module->orcjit_stop_compiling = true;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
os_mutex_lock(&module->tierup_wait_lock);
|
|
|
|
module->llvm_jit_inited = true;
|
|
|
|
os_cond_broadcast(&module->tierup_wait_cond);
|
|
|
|
os_mutex_unlock(&module->tierup_wait_lock);
|
2022-11-21 02:42:18 +00:00
|
|
|
|
2022-12-19 03:24:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
|
|
|
|
/* The callback function to compile jit functions */
|
|
|
|
static void *
|
|
|
|
orcjit_thread_callback(void *arg)
|
|
|
|
{
|
|
|
|
OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
|
|
|
|
#if WASM_ENABLE_JIT != 0
|
|
|
|
AOTCompContext *comp_ctx = thread_arg->comp_ctx;
|
|
|
|
#endif
|
|
|
|
WASMModule *module = thread_arg->module;
|
|
|
|
uint32 group_idx = thread_arg->group_idx;
|
|
|
|
uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
|
|
|
|
uint32 func_count = module->function_count;
|
|
|
|
uint32 i;
|
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_JIT != 0
|
|
|
|
/* Compile fast jit funcitons of this group */
|
|
|
|
for (i = group_idx; i < func_count; i += group_stride) {
|
|
|
|
if (!jit_compiler_compile(module, i + module->import_function_count)) {
|
|
|
|
os_printf("failed to compile fast jit function %u\n", i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (module->orcjit_stop_compiling) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2023-03-25 03:15:05 +00:00
|
|
|
#if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
|
|
|
|
os_mutex_lock(&module->tierup_wait_lock);
|
|
|
|
module->fast_jit_ready_groups++;
|
|
|
|
os_mutex_unlock(&module->tierup_wait_lock);
|
|
|
|
#endif
|
2022-12-19 03:24:46 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
|
|
|
|
&& WASM_ENABLE_LAZY_JIT != 0
|
|
|
|
/* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
|
|
|
|
for (i = group_idx; i < func_count;
|
|
|
|
i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
|
|
|
|
uint32 j;
|
|
|
|
|
|
|
|
for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
|
|
|
|
if (i + j * group_stride < func_count) {
|
|
|
|
if (!jit_compiler_set_call_to_fast_jit(
|
|
|
|
module,
|
|
|
|
i + j * group_stride + module->import_function_count)) {
|
|
|
|
os_printf(
|
|
|
|
"failed to compile call_to_fast_jit for func %u\n",
|
|
|
|
i + j * group_stride + module->import_function_count);
|
|
|
|
module->orcjit_stop_compiling = true;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (module->orcjit_stop_compiling) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-25 03:15:05 +00:00
|
|
|
/* Wait until init_llvm_jit_functions_stage2 finishes and all
|
|
|
|
fast jit functions are compiled */
|
2022-12-19 03:24:46 +00:00
|
|
|
os_mutex_lock(&module->tierup_wait_lock);
|
2023-03-25 03:15:05 +00:00
|
|
|
while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
|
|
|
|
&& module->fast_jit_ready_groups >= group_stride)) {
|
2022-12-19 03:24:46 +00:00
|
|
|
os_cond_reltimedwait(&module->tierup_wait_cond,
|
2023-02-02 10:16:01 +00:00
|
|
|
&module->tierup_wait_lock, 10000);
|
2022-12-19 03:24:46 +00:00
|
|
|
if (module->orcjit_stop_compiling) {
|
|
|
|
/* init_llvm_jit_functions_stage2 failed */
|
|
|
|
os_mutex_unlock(&module->tierup_wait_lock);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
os_mutex_unlock(&module->tierup_wait_lock);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if WASM_ENABLE_JIT != 0
|
|
|
|
/* Compile llvm jit functions of this group */
|
|
|
|
for (i = group_idx; i < func_count;
|
|
|
|
i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
|
|
|
|
LLVMOrcJITTargetAddress func_addr = 0;
|
|
|
|
LLVMErrorRef error;
|
|
|
|
char func_name[48];
|
|
|
|
typedef void (*F)(void);
|
|
|
|
union {
|
|
|
|
F f;
|
|
|
|
void *v;
|
|
|
|
} u;
|
|
|
|
uint32 j;
|
|
|
|
|
|
|
|
snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
|
|
|
|
"_wrapper");
|
|
|
|
LOG_DEBUG("compile llvm jit func %s", func_name);
|
|
|
|
error =
|
|
|
|
LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
|
|
|
|
if (error != LLVMErrorSuccess) {
|
|
|
|
char *err_msg = LLVMGetErrorMessage(error);
|
|
|
|
os_printf("failed to compile llvm jit function %u: %s", i, err_msg);
|
|
|
|
LLVMDisposeErrorMessage(err_msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call the jit wrapper function to trigger its compilation, so as
|
|
|
|
to compile the actual jit functions, since we add the latter to
|
|
|
|
function list in the PartitionFunction callback */
|
|
|
|
u.v = (void *)func_addr;
|
|
|
|
u.f();
|
|
|
|
|
|
|
|
for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
|
|
|
|
if (i + j * group_stride < func_count) {
|
|
|
|
module->func_ptrs_compiled[i + j * group_stride] = true;
|
|
|
|
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
|
|
|
|
snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
|
|
|
|
i + j * group_stride);
|
|
|
|
error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
|
|
|
|
func_name);
|
|
|
|
if (error != LLVMErrorSuccess) {
|
|
|
|
char *err_msg = LLVMGetErrorMessage(error);
|
|
|
|
os_printf("failed to compile llvm jit function %u: %s", i,
|
|
|
|
err_msg);
|
|
|
|
LLVMDisposeErrorMessage(err_msg);
|
|
|
|
/* Ignore current llvm jit func, as its func ptr is
|
|
|
|
previous set to call_to_fast_jit, which also works */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
jit_compiler_set_llvm_jit_func_ptr(
|
|
|
|
module,
|
|
|
|
i + j * group_stride + module->import_function_count,
|
|
|
|
(void *)func_addr);
|
|
|
|
|
|
|
|
/* Try to switch to call this llvm jit funtion instead of
|
|
|
|
fast jit function from fast jit jitted code */
|
|
|
|
jit_compiler_set_call_to_llvm_jit(
|
|
|
|
module,
|
|
|
|
i + j * group_stride + module->import_function_count);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (module->orcjit_stop_compiling) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
orcjit_stop_compile_threads(WASMModule *module)
|
|
|
|
{
|
|
|
|
uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
|
|
|
|
/ sizeof(OrcJitThreadArg));
|
|
|
|
|
|
|
|
module->orcjit_stop_compiling = true;
|
|
|
|
for (i = 0; i < thread_num; i++) {
|
|
|
|
if (module->orcjit_threads[i])
|
|
|
|
os_thread_join(module->orcjit_threads[i], NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
compile_jit_functions(WASMModule *module, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
|
|
|
{
|
|
|
|
uint32 thread_num =
|
2022-10-18 12:17:34 +00:00
|
|
|
(uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
|
2022-12-19 03:24:46 +00:00
|
|
|
uint32 i, j;
|
|
|
|
|
|
|
|
bh_print_time("Begin to compile jit functions");
|
2022-10-18 12:17:34 +00:00
|
|
|
|
|
|
|
/* Create threads to compile the jit functions */
|
2022-12-19 03:24:46 +00:00
|
|
|
for (i = 0; i < thread_num && i < module->function_count; i++) {
|
|
|
|
#if WASM_ENABLE_JIT != 0
|
2022-10-18 12:17:34 +00:00
|
|
|
module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
|
2022-12-19 03:24:46 +00:00
|
|
|
#endif
|
2022-10-18 12:17:34 +00:00
|
|
|
module->orcjit_thread_args[i].module = module;
|
|
|
|
module->orcjit_thread_args[i].group_idx = i;
|
|
|
|
|
|
|
|
if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
|
|
|
|
(void *)&module->orcjit_thread_args[i],
|
|
|
|
APP_THREAD_STACK_SIZE_DEFAULT)
|
|
|
|
!= 0) {
|
2022-10-18 02:59:28 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2022-10-18 12:17:34 +00:00
|
|
|
"create orcjit compile thread failed");
|
|
|
|
/* Terminate the threads created */
|
|
|
|
module->orcjit_stop_compiling = true;
|
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
os_thread_join(module->orcjit_threads[j], NULL);
|
|
|
|
}
|
2022-10-18 02:59:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-10-18 12:17:34 +00:00
|
|
|
|
|
|
|
#if WASM_ENABLE_LAZY_JIT == 0
|
|
|
|
/* Wait until all jit functions are compiled for eager mode */
|
|
|
|
for (i = 0; i < thread_num; i++) {
|
2022-12-19 03:24:46 +00:00
|
|
|
if (module->orcjit_threads[i])
|
|
|
|
os_thread_join(module->orcjit_threads[i], NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_JIT != 0
|
|
|
|
/* Ensure all the fast-jit functions are compiled */
|
|
|
|
for (i = 0; i < module->function_count; i++) {
|
|
|
|
if (!jit_compiler_is_compiled(module,
|
|
|
|
i + module->import_function_count)) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"failed to compile fast jit function");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if WASM_ENABLE_JIT != 0
|
|
|
|
/* Ensure all the llvm-jit functions are compiled */
|
|
|
|
for (i = 0; i < module->function_count; i++) {
|
|
|
|
if (!module->func_ptrs_compiled[i]) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"failed to compile llvm jit function");
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-18 12:17:34 +00:00
|
|
|
}
|
|
|
|
#endif
|
2022-12-19 03:24:46 +00:00
|
|
|
#endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
|
2022-10-18 12:17:34 +00:00
|
|
|
|
|
|
|
bh_print_time("End compile jit functions");
|
2022-10-18 02:59:28 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-12-19 03:24:46 +00:00
|
|
|
#endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
|
2022-10-18 02:59:28 +00:00
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
|
|
|
|
uint32 cur_func_idx, char *error_buf,
|
|
|
|
uint32 error_buf_size);
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2021-04-01 08:50:46 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
|
2020-03-07 14:20:38 +00:00
|
|
|
void **
|
|
|
|
wasm_interp_get_handle_table();
|
|
|
|
|
|
|
|
static void **handle_table;
|
|
|
|
#endif
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
static bool
|
2021-11-08 03:27:26 +00:00
|
|
|
load_from_sections(WASMModule *module, WASMSection *sections,
|
|
|
|
bool is_load_from_file_buf, char *error_buf,
|
2021-10-08 03:44:39 +00:00
|
|
|
uint32 error_buf_size)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
2020-02-10 04:36:45 +00:00
|
|
|
WASMExport *export;
|
2019-05-07 02:18:18 +00:00
|
|
|
WASMSection *section = sections;
|
2019-10-11 07:25:23 +00:00
|
|
|
const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
|
2021-10-08 03:44:39 +00:00
|
|
|
*buf_func = NULL, *buf_func_end = NULL;
|
2020-08-20 04:43:12 +00:00
|
|
|
WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
|
|
|
|
WASMGlobal *aux_stack_top_global = NULL, *global;
|
|
|
|
uint32 aux_data_end = (uint32)-1, aux_heap_base = (uint32)-1;
|
|
|
|
uint32 aux_stack_top = (uint32)-1, global_index, func_index, i;
|
|
|
|
uint32 aux_data_end_global_index = (uint32)-1;
|
|
|
|
uint32 aux_heap_base_global_index = (uint32)-1;
|
|
|
|
WASMType *func_type;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2019-10-11 07:25:23 +00:00
|
|
|
/* Find code and function sections if have */
|
2019-05-07 02:18:18 +00:00
|
|
|
while (section) {
|
|
|
|
if (section->section_type == SECTION_TYPE_CODE) {
|
|
|
|
buf_code = section->section_body;
|
|
|
|
buf_code_end = buf_code + section->section_body_size;
|
2021-09-29 05:36:46 +00:00
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
|
|
|
|
module->buf_code = (uint8 *)buf_code;
|
|
|
|
module->buf_code_size = section->section_body_size;
|
|
|
|
#endif
|
2019-10-11 07:25:23 +00:00
|
|
|
}
|
|
|
|
else if (section->section_type == SECTION_TYPE_FUNC) {
|
|
|
|
buf_func = section->section_body;
|
|
|
|
buf_func_end = buf_func + section->section_body_size;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
section = section->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
section = sections;
|
|
|
|
while (section) {
|
|
|
|
buf = section->section_body;
|
|
|
|
buf_end = buf + section->section_body_size;
|
|
|
|
switch (section->section_type) {
|
|
|
|
case SECTION_TYPE_USER:
|
|
|
|
/* unsupported user section, ignore it. */
|
2021-11-08 03:27:26 +00:00
|
|
|
if (!load_user_section(buf, buf_end, module,
|
|
|
|
is_load_from_file_buf, error_buf,
|
2021-10-08 03:44:39 +00:00
|
|
|
error_buf_size))
|
2019-11-27 02:52:12 +00:00
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
case SECTION_TYPE_TYPE:
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!load_type_section(buf, buf_end, module, error_buf,
|
|
|
|
error_buf_size))
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case SECTION_TYPE_IMPORT:
|
2021-11-08 03:27:26 +00:00
|
|
|
if (!load_import_section(buf, buf_end, module,
|
|
|
|
is_load_from_file_buf, error_buf,
|
2021-10-08 03:44:39 +00:00
|
|
|
error_buf_size))
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case SECTION_TYPE_FUNC:
|
|
|
|
if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
|
2020-08-21 07:11:31 +00:00
|
|
|
module, error_buf, error_buf_size))
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case SECTION_TYPE_TABLE:
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!load_table_section(buf, buf_end, module, error_buf,
|
|
|
|
error_buf_size))
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case SECTION_TYPE_MEMORY:
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!load_memory_section(buf, buf_end, module, error_buf,
|
|
|
|
error_buf_size))
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case SECTION_TYPE_GLOBAL:
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!load_global_section(buf, buf_end, module, error_buf,
|
|
|
|
error_buf_size))
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case SECTION_TYPE_EXPORT:
|
2021-11-08 03:27:26 +00:00
|
|
|
if (!load_export_section(buf, buf_end, module,
|
|
|
|
is_load_from_file_buf, error_buf,
|
2021-10-08 03:44:39 +00:00
|
|
|
error_buf_size))
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case SECTION_TYPE_START:
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!load_start_section(buf, buf_end, module, error_buf,
|
|
|
|
error_buf_size))
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case SECTION_TYPE_ELEM:
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!load_table_segment_section(buf, buf_end, module, error_buf,
|
|
|
|
error_buf_size))
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case SECTION_TYPE_CODE:
|
2019-10-11 07:25:23 +00:00
|
|
|
if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
|
|
|
|
module, error_buf, error_buf_size))
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case SECTION_TYPE_DATA:
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!load_data_segment_section(buf, buf_end, module, error_buf,
|
|
|
|
error_buf_size))
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
break;
|
2020-06-02 06:53:06 +00:00
|
|
|
#if WASM_ENABLE_BULK_MEMORY != 0
|
|
|
|
case SECTION_TYPE_DATACOUNT:
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!load_datacount_section(buf, buf_end, module, error_buf,
|
|
|
|
error_buf_size))
|
2020-06-02 06:53:06 +00:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
default:
|
2021-10-08 03:44:39 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "invalid section id");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
section = section->next;
|
|
|
|
}
|
|
|
|
|
2020-08-20 04:43:12 +00:00
|
|
|
module->aux_data_end_global_index = (uint32)-1;
|
|
|
|
module->aux_heap_base_global_index = (uint32)-1;
|
|
|
|
module->aux_stack_top_global_index = (uint32)-1;
|
|
|
|
|
|
|
|
/* Resolve auxiliary data/stack/heap info and reset memory info */
|
2020-06-15 11:04:04 +00:00
|
|
|
export = module->exports;
|
2021-10-08 03:44:39 +00:00
|
|
|
for (i = 0; i < module->export_count; i++, export ++) {
|
2020-06-15 11:04:04 +00:00
|
|
|
if (export->kind == EXPORT_KIND_GLOBAL) {
|
|
|
|
if (!strcmp(export->name, "__heap_base")) {
|
|
|
|
global_index = export->index - module->import_global_count;
|
|
|
|
global = module->globals + global_index;
|
2021-10-08 03:44:39 +00:00
|
|
|
if (global->type == VALUE_TYPE_I32 && !global->is_mutable
|
|
|
|
&& global->init_expr.init_expr_type
|
|
|
|
== INIT_EXPR_TYPE_I32_CONST) {
|
2020-08-20 04:43:12 +00:00
|
|
|
aux_heap_base_global = global;
|
|
|
|
aux_heap_base = global->init_expr.u.i32;
|
|
|
|
aux_heap_base_global_index = export->index;
|
|
|
|
LOG_VERBOSE("Found aux __heap_base global, value: %d",
|
|
|
|
aux_heap_base);
|
2020-06-15 11:04:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!strcmp(export->name, "__data_end")) {
|
|
|
|
global_index = export->index - module->import_global_count;
|
|
|
|
global = module->globals + global_index;
|
2021-10-08 03:44:39 +00:00
|
|
|
if (global->type == VALUE_TYPE_I32 && !global->is_mutable
|
|
|
|
&& global->init_expr.init_expr_type
|
|
|
|
== INIT_EXPR_TYPE_I32_CONST) {
|
2020-08-20 04:43:12 +00:00
|
|
|
aux_data_end_global = global;
|
|
|
|
aux_data_end = global->init_expr.u.i32;
|
|
|
|
aux_data_end_global_index = export->index;
|
|
|
|
LOG_VERBOSE("Found aux __data_end global, value: %d",
|
|
|
|
aux_data_end);
|
2020-06-15 11:04:04 +00:00
|
|
|
|
2020-08-20 04:43:12 +00:00
|
|
|
aux_data_end = align_uint(aux_data_end, 16);
|
2020-02-10 04:36:45 +00:00
|
|
|
}
|
2020-06-15 11:04:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* For module compiled with -pthread option, the global is:
|
|
|
|
[0] stack_top <-- 0
|
|
|
|
[1] tls_pointer
|
|
|
|
[2] tls_size
|
|
|
|
[3] data_end <-- 3
|
|
|
|
[4] global_base
|
|
|
|
[5] heap_base <-- 5
|
|
|
|
[6] dso_handle
|
|
|
|
|
|
|
|
For module compiled without -pthread option:
|
|
|
|
[0] stack_top <-- 0
|
|
|
|
[1] data_end <-- 1
|
|
|
|
[2] global_base
|
|
|
|
[3] heap_base <-- 3
|
|
|
|
[4] dso_handle
|
|
|
|
*/
|
2020-08-20 04:43:12 +00:00
|
|
|
if (aux_data_end_global && aux_heap_base_global
|
|
|
|
&& aux_data_end <= aux_heap_base) {
|
|
|
|
module->aux_data_end_global_index = aux_data_end_global_index;
|
|
|
|
module->aux_data_end = aux_data_end;
|
|
|
|
module->aux_heap_base_global_index = aux_heap_base_global_index;
|
|
|
|
module->aux_heap_base = aux_heap_base;
|
|
|
|
|
2020-06-15 11:04:04 +00:00
|
|
|
/* Resolve aux stack top global */
|
2020-08-20 04:43:12 +00:00
|
|
|
for (global_index = 0; global_index < module->global_count;
|
|
|
|
global_index++) {
|
2020-02-10 04:36:45 +00:00
|
|
|
global = module->globals + global_index;
|
2020-08-20 04:43:12 +00:00
|
|
|
if (global->is_mutable /* heap_base and data_end is
|
|
|
|
not mutable */
|
2020-06-15 11:04:04 +00:00
|
|
|
&& global->type == VALUE_TYPE_I32
|
2021-10-08 03:44:39 +00:00
|
|
|
&& global->init_expr.init_expr_type
|
|
|
|
== INIT_EXPR_TYPE_I32_CONST
|
2020-08-20 04:43:12 +00:00
|
|
|
&& (uint32)global->init_expr.u.i32 <= aux_heap_base) {
|
|
|
|
aux_stack_top_global = global;
|
|
|
|
aux_stack_top = (uint32)global->init_expr.u.i32;
|
|
|
|
module->aux_stack_top_global_index =
|
2021-10-08 03:44:39 +00:00
|
|
|
module->import_global_count + global_index;
|
2020-08-20 04:43:12 +00:00
|
|
|
module->aux_stack_bottom = aux_stack_top;
|
2021-10-08 03:44:39 +00:00
|
|
|
module->aux_stack_size =
|
|
|
|
aux_stack_top > aux_data_end
|
|
|
|
? aux_stack_top - aux_data_end
|
|
|
|
: aux_stack_top;
|
2020-08-20 04:43:12 +00:00
|
|
|
LOG_VERBOSE("Found aux stack top global, value: %d, "
|
|
|
|
"global index: %d, stack size: %d",
|
|
|
|
aux_stack_top, global_index,
|
|
|
|
module->aux_stack_size);
|
2020-06-15 11:04:04 +00:00
|
|
|
break;
|
2020-02-10 04:36:45 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-03 07:30:32 +00:00
|
|
|
if (!aux_stack_top_global) {
|
|
|
|
/* Auxiliary stack global isn't found, it must be unused
|
|
|
|
in the wasm app, as if it is used, the global must be
|
|
|
|
defined. Here we set it to __heap_base global and set
|
|
|
|
its size to 0. */
|
|
|
|
aux_stack_top_global = aux_heap_base_global;
|
|
|
|
aux_stack_top = aux_heap_base;
|
|
|
|
module->aux_stack_top_global_index =
|
|
|
|
module->aux_heap_base_global_index;
|
|
|
|
module->aux_stack_bottom = aux_stack_top;
|
|
|
|
module->aux_stack_size = 0;
|
|
|
|
}
|
2020-06-15 11:04:04 +00:00
|
|
|
break;
|
2020-02-10 04:36:45 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-15 11:04:04 +00:00
|
|
|
}
|
2020-02-10 04:36:45 +00:00
|
|
|
|
2020-08-20 04:43:12 +00:00
|
|
|
module->malloc_function = (uint32)-1;
|
|
|
|
module->free_function = (uint32)-1;
|
2020-12-07 08:37:49 +00:00
|
|
|
module->retain_function = (uint32)-1;
|
2020-08-20 04:43:12 +00:00
|
|
|
|
2020-12-07 08:37:49 +00:00
|
|
|
/* Resolve malloc/free function exported by wasm module */
|
2020-08-20 04:43:12 +00:00
|
|
|
export = module->exports;
|
2021-10-08 03:44:39 +00:00
|
|
|
for (i = 0; i < module->export_count; i++, export ++) {
|
2020-08-20 04:43:12 +00:00
|
|
|
if (export->kind == EXPORT_KIND_FUNC) {
|
|
|
|
if (!strcmp(export->name, "malloc")
|
|
|
|
&& export->index >= module->import_function_count) {
|
|
|
|
func_index = export->index - module->import_function_count;
|
|
|
|
func_type = module->functions[func_index]->func_type;
|
2021-10-08 03:44:39 +00:00
|
|
|
if (func_type->param_count == 1 && func_type->result_count == 1
|
2020-08-20 04:43:12 +00:00
|
|
|
&& func_type->types[0] == VALUE_TYPE_I32
|
|
|
|
&& func_type->types[1] == VALUE_TYPE_I32) {
|
2020-12-07 08:37:49 +00:00
|
|
|
bh_assert(module->malloc_function == (uint32)-1);
|
|
|
|
module->malloc_function = export->index;
|
|
|
|
LOG_VERBOSE("Found malloc function, name: %s, index: %u",
|
|
|
|
export->name, export->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!strcmp(export->name, "__new")
|
|
|
|
&& export->index >= module->import_function_count) {
|
2021-03-18 08:33:02 +00:00
|
|
|
/* __new && __pin for AssemblyScript */
|
2020-12-07 08:37:49 +00:00
|
|
|
func_index = export->index - module->import_function_count;
|
|
|
|
func_type = module->functions[func_index]->func_type;
|
2021-10-08 03:44:39 +00:00
|
|
|
if (func_type->param_count == 2 && func_type->result_count == 1
|
2020-12-07 08:37:49 +00:00
|
|
|
&& func_type->types[0] == VALUE_TYPE_I32
|
|
|
|
&& func_type->types[1] == VALUE_TYPE_I32
|
|
|
|
&& func_type->types[2] == VALUE_TYPE_I32) {
|
|
|
|
uint32 j;
|
|
|
|
WASMExport *export_tmp;
|
|
|
|
|
|
|
|
bh_assert(module->malloc_function == (uint32)-1);
|
2020-08-20 04:43:12 +00:00
|
|
|
module->malloc_function = export->index;
|
2020-12-07 08:37:49 +00:00
|
|
|
LOG_VERBOSE("Found malloc function, name: %s, index: %u",
|
|
|
|
export->name, export->index);
|
|
|
|
|
|
|
|
/* resolve retain function.
|
2021-12-03 07:30:32 +00:00
|
|
|
If not found, reset malloc function index */
|
2020-12-07 08:37:49 +00:00
|
|
|
export_tmp = module->exports;
|
|
|
|
for (j = 0; j < module->export_count; j++, export_tmp++) {
|
|
|
|
if ((export_tmp->kind == EXPORT_KIND_FUNC)
|
2021-03-18 08:33:02 +00:00
|
|
|
&& (!strcmp(export_tmp->name, "__retain")
|
|
|
|
|| (!strcmp(export_tmp->name, "__pin")))
|
2020-12-07 08:37:49 +00:00
|
|
|
&& (export_tmp->index
|
|
|
|
>= module->import_function_count)) {
|
|
|
|
func_index = export_tmp->index
|
2021-10-08 03:44:39 +00:00
|
|
|
- module->import_function_count;
|
2020-12-07 08:37:49 +00:00
|
|
|
func_type =
|
2021-10-08 03:44:39 +00:00
|
|
|
module->functions[func_index]->func_type;
|
2020-12-07 08:37:49 +00:00
|
|
|
if (func_type->param_count == 1
|
|
|
|
&& func_type->result_count == 1
|
|
|
|
&& func_type->types[0] == VALUE_TYPE_I32
|
|
|
|
&& func_type->types[1] == VALUE_TYPE_I32) {
|
2021-10-08 03:44:39 +00:00
|
|
|
bh_assert(module->retain_function
|
|
|
|
== (uint32)-1);
|
2020-12-07 08:37:49 +00:00
|
|
|
module->retain_function = export_tmp->index;
|
2021-10-08 03:44:39 +00:00
|
|
|
LOG_VERBOSE("Found retain function, name: %s, "
|
|
|
|
"index: %u",
|
|
|
|
export_tmp->name,
|
|
|
|
export_tmp->index);
|
2020-12-07 08:37:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (j == module->export_count) {
|
|
|
|
module->malloc_function = (uint32)-1;
|
|
|
|
LOG_VERBOSE("Can't find retain function,"
|
|
|
|
"reset malloc function index to -1");
|
|
|
|
}
|
2020-08-20 04:43:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-07 08:37:49 +00:00
|
|
|
else if (((!strcmp(export->name, "free"))
|
2021-03-18 08:33:02 +00:00
|
|
|
|| (!strcmp(export->name, "__release"))
|
|
|
|
|| (!strcmp(export->name, "__unpin")))
|
2020-08-20 04:43:12 +00:00
|
|
|
&& export->index >= module->import_function_count) {
|
|
|
|
func_index = export->index - module->import_function_count;
|
|
|
|
func_type = module->functions[func_index]->func_type;
|
2021-10-08 03:44:39 +00:00
|
|
|
if (func_type->param_count == 1 && func_type->result_count == 0
|
2020-08-20 04:43:12 +00:00
|
|
|
&& func_type->types[0] == VALUE_TYPE_I32) {
|
2020-12-07 08:37:49 +00:00
|
|
|
bh_assert(module->free_function == (uint32)-1);
|
2020-08-20 04:43:12 +00:00
|
|
|
module->free_function = export->index;
|
2020-12-07 08:37:49 +00:00
|
|
|
LOG_VERBOSE("Found free function, name: %s, index: %u",
|
|
|
|
export->name, export->index);
|
2020-08-20 04:43:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-01 08:50:46 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
|
2020-09-18 10:04:56 +00:00
|
|
|
handle_table = wasm_interp_get_handle_table();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (i = 0; i < module->function_count; i++) {
|
|
|
|
WASMFunction *func = module->functions[i];
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
|
|
|
|
error_buf_size)) {
|
2020-09-18 10:04:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-09-14 11:49:18 +00:00
|
|
|
|
|
|
|
if (i == module->function_count - 1
|
|
|
|
&& func->code + func->code_size != buf_code_end) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"code section size mismatch");
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-18 10:04:56 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 11:04:04 +00:00
|
|
|
if (!module->possible_memory_grow) {
|
2020-08-20 04:43:12 +00:00
|
|
|
WASMMemoryImport *memory_import;
|
|
|
|
WASMMemory *memory;
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
if (aux_data_end_global && aux_heap_base_global
|
2020-08-20 04:43:12 +00:00
|
|
|
&& aux_stack_top_global) {
|
2020-02-10 04:36:45 +00:00
|
|
|
uint64 init_memory_size;
|
2020-08-20 04:43:12 +00:00
|
|
|
uint32 shrunk_memory_size = align_uint(aux_heap_base, 8);
|
|
|
|
|
2020-02-10 04:36:45 +00:00
|
|
|
if (module->import_memory_count) {
|
|
|
|
memory_import = &module->import_memories[0].u.memory;
|
2021-10-08 03:44:39 +00:00
|
|
|
init_memory_size = (uint64)memory_import->num_bytes_per_page
|
|
|
|
* memory_import->init_page_count;
|
2020-08-20 04:43:12 +00:00
|
|
|
if (shrunk_memory_size <= init_memory_size) {
|
2020-02-10 04:36:45 +00:00
|
|
|
/* Reset memory info to decrease memory usage */
|
|
|
|
memory_import->num_bytes_per_page = shrunk_memory_size;
|
|
|
|
memory_import->init_page_count = 1;
|
2020-08-20 04:43:12 +00:00
|
|
|
LOG_VERBOSE("Shrink import memory size to %d",
|
2020-02-10 04:36:45 +00:00
|
|
|
shrunk_memory_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (module->memory_count) {
|
|
|
|
memory = &module->memories[0];
|
2021-10-08 03:44:39 +00:00
|
|
|
init_memory_size = (uint64)memory->num_bytes_per_page
|
|
|
|
* memory->init_page_count;
|
2020-08-20 04:43:12 +00:00
|
|
|
if (shrunk_memory_size <= init_memory_size) {
|
2020-02-10 04:36:45 +00:00
|
|
|
/* Reset memory info to decrease memory usage */
|
|
|
|
memory->num_bytes_per_page = shrunk_memory_size;
|
|
|
|
memory->init_page_count = 1;
|
2020-08-20 04:43:12 +00:00
|
|
|
LOG_VERBOSE("Shrink memory size to %d", shrunk_memory_size);
|
2020-02-10 04:36:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-20 04:43:12 +00:00
|
|
|
|
|
|
|
#if WASM_ENABLE_MULTI_MODULE == 0
|
|
|
|
if (module->import_memory_count) {
|
|
|
|
memory_import = &module->import_memories[0].u.memory;
|
2022-08-23 08:05:13 +00:00
|
|
|
if (memory_import->init_page_count < DEFAULT_MAX_PAGES)
|
|
|
|
memory_import->num_bytes_per_page *=
|
|
|
|
memory_import->init_page_count;
|
|
|
|
else
|
|
|
|
memory_import->num_bytes_per_page = UINT32_MAX;
|
|
|
|
|
|
|
|
if (memory_import->init_page_count > 0)
|
|
|
|
memory_import->init_page_count = memory_import->max_page_count =
|
|
|
|
1;
|
|
|
|
else
|
|
|
|
memory_import->init_page_count = memory_import->max_page_count =
|
|
|
|
0;
|
2020-08-20 04:43:12 +00:00
|
|
|
}
|
|
|
|
if (module->memory_count) {
|
|
|
|
memory = &module->memories[0];
|
2022-08-23 08:05:13 +00:00
|
|
|
if (memory->init_page_count < DEFAULT_MAX_PAGES)
|
|
|
|
memory->num_bytes_per_page *= memory->init_page_count;
|
|
|
|
else
|
|
|
|
memory->num_bytes_per_page = UINT32_MAX;
|
|
|
|
|
|
|
|
if (memory->init_page_count > 0)
|
|
|
|
memory->init_page_count = memory->max_page_count = 1;
|
|
|
|
else
|
|
|
|
memory->init_page_count = memory->max_page_count = 0;
|
2020-08-20 04:43:12 +00:00
|
|
|
}
|
|
|
|
#endif
|
2020-02-10 04:36:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 08:03:50 +00:00
|
|
|
calculate_global_data_offset(module);
|
|
|
|
|
2022-10-18 02:59:28 +00:00
|
|
|
#if WASM_ENABLE_FAST_JIT != 0
|
2022-12-19 03:24:46 +00:00
|
|
|
if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if WASM_ENABLE_JIT != 0
|
|
|
|
if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
|
2022-08-02 08:03:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-12-19 03:24:46 +00:00
|
|
|
#if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
|
|
|
|
if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* Run aot_compile_wasm in a backend thread, so as not to block the main
|
|
|
|
thread fast jit execution, since applying llvm optimizations in
|
|
|
|
aot_compile_wasm may cost a lot of time.
|
|
|
|
Create thread with enough native stack to apply llvm optimizations */
|
|
|
|
if (os_thread_create(&module->llvm_jit_init_thread,
|
|
|
|
init_llvm_jit_functions_stage2_callback,
|
|
|
|
(void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
|
|
|
|
!= 0) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"create orcjit compile thread failed");
|
2022-08-02 08:03:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
2022-12-19 03:24:46 +00:00
|
|
|
#endif
|
2022-08-02 08:03:50 +00:00
|
|
|
|
2022-12-19 03:24:46 +00:00
|
|
|
#if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
|
|
|
|
/* Create threads to compile the jit functions */
|
|
|
|
if (!compile_jit_functions(module, error_buf, error_buf_size)) {
|
2022-10-18 02:59:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-12-19 03:24:46 +00:00
|
|
|
#endif
|
2022-10-18 02:59:28 +00:00
|
|
|
|
2020-09-18 10:04:56 +00:00
|
|
|
#if WASM_ENABLE_MEMORY_TRACING != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
|
2020-09-18 10:04:56 +00:00
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
static WASMModule *
|
2019-05-07 02:18:18 +00:00
|
|
|
create_module(char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
2021-10-08 03:44:39 +00:00
|
|
|
WASMModule *module =
|
|
|
|
loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
|
2022-04-23 11:15:55 +00:00
|
|
|
bh_list_status ret;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
if (!module) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
Enable AoT and wamr-sdk, and change arguments of call wasm API (#157)
* Implement memory profiler, optimize memory usage, modify code indent
* Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default
* Add a new extension library: connection
* Fix bug of reading magic number and version in big endian platform
* Re-org platform APIs: move most platform APIs from iwasm to shared-lib
* Enhance wasm loader to fix some security issues
* Fix issue about illegal load of EXC_RETURN into PC on stm32 board
* Updates that let a restricted version of the interpreter run in SGX
* Enable native/app address validation and conversion for wasm app
* Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused
* Refine binary size and fix several minor issues
Optimize interpreter LOAD/STORE opcodes to decrease the binary size
Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found
Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper
Add macros of global heap size, stack size, heap size for Zephyr main.c
Clear compile warning of wasm_application.c
* Add more strict security checks for libc wrapper API's
* Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files
* Enhance security of libc strcpy/sprintf wrapper function
* Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions
* Remove get_module_inst() and fix issue of call native
* Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate
* Refine interpreter call native process, refine memory boudary check
* Fix issues of invokeNative function of arm/mips/general version
* Add a switch to build simple sample without gui support
* Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code
* Re-org shared lib header files, remove unused info; fix compile issues of vxworks
* Add build target general
* Remove unused files
* Update license header
* test push
* Restore file
* Sync up with internal/feature
* Sync up with internal/feature
* Rename build_wamr_app to build_wasm_app
* Fix small issues of README
* Enhance malformed wasm file checking
Fix issue of print hex int and implement utf8 string check
Fix wasi file read/write right issue
Fix minor issue of build wasm app doc
* Sync up with internal/feature
* Sync up with internal/feature: fix interpreter arm issue, fix read leb issue
* Sync up with internal/feature
* Fix bug of config.h and rename wasi config.h to ssp_config.h
* Sync up with internal/feature
* Import wamr aot
* update document
* update document
* Update document, disable WASI in 32bit
* update document
* remove files
* update document
* Update document
* update document
* update document
* update samples
* Sync up with internal repo
2020-01-21 05:26:14 +00:00
|
|
|
module->module_type = Wasm_Module_Bytecode;
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
/* Set start_function to -1, means no start function */
|
|
|
|
module->start_function = (uint32)-1;
|
|
|
|
|
2022-04-23 11:15:55 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP == 0
|
|
|
|
module->br_table_cache_list = &module->br_table_cache_list_head;
|
|
|
|
ret = bh_list_init(module->br_table_cache_list);
|
|
|
|
bh_assert(ret == BH_LIST_SUCCESS);
|
|
|
|
#endif
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
#if WASM_ENABLE_MULTI_MODULE != 0
|
|
|
|
module->import_module_list = &module->import_module_list_head;
|
2022-12-19 03:24:46 +00:00
|
|
|
ret = bh_list_init(module->import_module_list);
|
|
|
|
bh_assert(ret == BH_LIST_SUCCESS);
|
2021-09-29 05:36:46 +00:00
|
|
|
#endif
|
2022-12-19 03:24:46 +00:00
|
|
|
|
2021-09-29 05:36:46 +00:00
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0
|
2022-12-19 03:24:46 +00:00
|
|
|
ret = bh_list_init(&module->fast_opcode_list);
|
|
|
|
bh_assert(ret == BH_LIST_SUCCESS);
|
|
|
|
#endif
|
|
|
|
|
2023-02-16 03:54:19 +00:00
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0 \
|
|
|
|
|| (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
|
2022-12-19 03:24:46 +00:00
|
|
|
&& WASM_ENABLE_LAZY_JIT != 0)
|
|
|
|
if (os_mutex_init(&module->instance_list_lock) != 0) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"init instance list lock failed");
|
2021-12-06 02:25:38 +00:00
|
|
|
wasm_runtime_free(module);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-06-02 06:53:06 +00:00
|
|
|
#endif
|
2022-12-19 03:24:46 +00:00
|
|
|
|
|
|
|
(void)ret;
|
2019-05-07 02:18:18 +00:00
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
2021-09-29 05:36:46 +00:00
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0
|
2022-04-23 11:15:55 +00:00
|
|
|
static bool
|
|
|
|
record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2021-09-29 05:36:46 +00:00
|
|
|
{
|
2021-10-08 03:44:39 +00:00
|
|
|
WASMFastOPCodeNode *fast_op =
|
2022-04-23 11:15:55 +00:00
|
|
|
loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
|
2021-09-29 05:36:46 +00:00
|
|
|
if (fast_op) {
|
|
|
|
fast_op->offset = pos - module->load_addr;
|
|
|
|
fast_op->orig_op = orig_op;
|
|
|
|
bh_list_insert(&module->fast_opcode_list, fast_op);
|
|
|
|
}
|
2022-04-23 11:15:55 +00:00
|
|
|
return fast_op ? true : false;
|
2021-09-29 05:36:46 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
WASMModule *
|
2021-10-08 03:44:39 +00:00
|
|
|
wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
|
|
|
WASMModule *module = create_module(error_buf, error_buf_size);
|
|
|
|
if (!module)
|
|
|
|
return NULL;
|
|
|
|
|
2021-11-08 03:27:26 +00:00
|
|
|
if (!load_from_sections(module, section_list, false, error_buf,
|
|
|
|
error_buf_size)) {
|
2019-05-07 02:18:18 +00:00
|
|
|
wasm_loader_unload(module);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_VERBOSE("Load module from sections success.\n");
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
destroy_sections(WASMSection *section_list)
|
|
|
|
{
|
|
|
|
WASMSection *section = section_list, *next;
|
|
|
|
while (section) {
|
|
|
|
next = section->next;
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(section);
|
2019-05-07 02:18:18 +00:00
|
|
|
section = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
/* clang-format off */
|
2020-06-02 06:53:06 +00:00
|
|
|
static uint8 section_ids[] = {
|
|
|
|
SECTION_TYPE_USER,
|
|
|
|
SECTION_TYPE_TYPE,
|
|
|
|
SECTION_TYPE_IMPORT,
|
|
|
|
SECTION_TYPE_FUNC,
|
|
|
|
SECTION_TYPE_TABLE,
|
|
|
|
SECTION_TYPE_MEMORY,
|
|
|
|
SECTION_TYPE_GLOBAL,
|
|
|
|
SECTION_TYPE_EXPORT,
|
|
|
|
SECTION_TYPE_START,
|
|
|
|
SECTION_TYPE_ELEM,
|
|
|
|
#if WASM_ENABLE_BULK_MEMORY != 0
|
|
|
|
SECTION_TYPE_DATACOUNT,
|
|
|
|
#endif
|
|
|
|
SECTION_TYPE_CODE,
|
|
|
|
SECTION_TYPE_DATA
|
|
|
|
};
|
2021-10-08 03:44:39 +00:00
|
|
|
/* clang-format on */
|
2020-06-02 06:53:06 +00:00
|
|
|
|
|
|
|
static uint8
|
|
|
|
get_section_index(uint8 section_type)
|
|
|
|
{
|
|
|
|
uint8 max_id = sizeof(section_ids) / sizeof(uint8);
|
|
|
|
|
|
|
|
for (uint8 i = 0; i < max_id; i++) {
|
|
|
|
if (section_type == section_ids[i])
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (uint8)-1;
|
|
|
|
}
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
|
2019-05-07 02:18:18 +00:00
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
|
|
|
WASMSection *section_list_end = NULL, *section;
|
2021-10-08 03:44:39 +00:00
|
|
|
const uint8 *p = buf, *p_end = buf + size /*, *section_body*/;
|
2020-06-02 06:53:06 +00:00
|
|
|
uint8 section_type, section_index, last_section_index = (uint8)-1;
|
2019-05-07 02:18:18 +00:00
|
|
|
uint32 section_size;
|
|
|
|
|
Enable AoT and wamr-sdk, and change arguments of call wasm API (#157)
* Implement memory profiler, optimize memory usage, modify code indent
* Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default
* Add a new extension library: connection
* Fix bug of reading magic number and version in big endian platform
* Re-org platform APIs: move most platform APIs from iwasm to shared-lib
* Enhance wasm loader to fix some security issues
* Fix issue about illegal load of EXC_RETURN into PC on stm32 board
* Updates that let a restricted version of the interpreter run in SGX
* Enable native/app address validation and conversion for wasm app
* Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused
* Refine binary size and fix several minor issues
Optimize interpreter LOAD/STORE opcodes to decrease the binary size
Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found
Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper
Add macros of global heap size, stack size, heap size for Zephyr main.c
Clear compile warning of wasm_application.c
* Add more strict security checks for libc wrapper API's
* Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files
* Enhance security of libc strcpy/sprintf wrapper function
* Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions
* Remove get_module_inst() and fix issue of call native
* Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate
* Refine interpreter call native process, refine memory boudary check
* Fix issues of invokeNative function of arm/mips/general version
* Add a switch to build simple sample without gui support
* Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code
* Re-org shared lib header files, remove unused info; fix compile issues of vxworks
* Add build target general
* Remove unused files
* Update license header
* test push
* Restore file
* Sync up with internal/feature
* Sync up with internal/feature
* Rename build_wamr_app to build_wasm_app
* Fix small issues of README
* Enhance malformed wasm file checking
Fix issue of print hex int and implement utf8 string check
Fix wasi file read/write right issue
Fix minor issue of build wasm app doc
* Sync up with internal/feature
* Sync up with internal/feature: fix interpreter arm issue, fix read leb issue
* Sync up with internal/feature
* Fix bug of config.h and rename wasi config.h to ssp_config.h
* Sync up with internal/feature
* Import wamr aot
* update document
* update document
* Update document, disable WASI in 32bit
* update document
* remove files
* update document
* Update document
* update document
* update document
* update samples
* Sync up with internal repo
2020-01-21 05:26:14 +00:00
|
|
|
bh_assert(!*p_section_list);
|
2019-11-20 13:16:36 +00:00
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
p += 8;
|
|
|
|
while (p < p_end) {
|
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
section_type = read_uint8(p);
|
2020-06-02 06:53:06 +00:00
|
|
|
section_index = get_section_index(section_type);
|
|
|
|
if (section_index != (uint8)-1) {
|
2019-11-27 02:52:12 +00:00
|
|
|
if (section_type != SECTION_TYPE_USER) {
|
|
|
|
/* Custom sections may be inserted at any place,
|
|
|
|
while other sections must occur at most once
|
|
|
|
and in prescribed order. */
|
2020-06-02 06:53:06 +00:00
|
|
|
if (last_section_index != (uint8)-1
|
|
|
|
&& (section_index <= last_section_index)) {
|
2019-11-27 02:52:12 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2021-12-15 06:39:54 +00:00
|
|
|
"unexpected content after last section or "
|
2019-11-27 02:52:12 +00:00
|
|
|
"junk after last section");
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-02 06:53:06 +00:00
|
|
|
last_section_index = section_index;
|
2019-11-27 02:52:12 +00:00
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
read_leb_uint32(p, p_end, section_size);
|
2019-11-27 02:52:12 +00:00
|
|
|
CHECK_BUF1(p, p_end, section_size);
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
|
|
|
|
error_buf_size))) {
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
section->section_type = section_type;
|
2021-10-08 03:44:39 +00:00
|
|
|
section->section_body = (uint8 *)p;
|
2019-05-07 02:18:18 +00:00
|
|
|
section->section_body_size = section_size;
|
|
|
|
|
2020-11-24 06:00:09 +00:00
|
|
|
if (!section_list_end)
|
2019-05-07 02:18:18 +00:00
|
|
|
*p_section_list = section_list_end = section;
|
|
|
|
else {
|
|
|
|
section_list_end->next = section;
|
|
|
|
section_list_end = section;
|
|
|
|
}
|
|
|
|
|
|
|
|
p += section_size;
|
|
|
|
}
|
|
|
|
else {
|
2021-10-08 03:44:39 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "invalid section id");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 03:29:08 +00:00
|
|
|
static void
|
2021-10-08 03:44:39 +00:00
|
|
|
exchange32(uint8 *p_data)
|
2019-06-14 03:29:08 +00:00
|
|
|
{
|
|
|
|
uint8 value = *p_data;
|
|
|
|
*p_data = *(p_data + 3);
|
|
|
|
*(p_data + 3) = value;
|
|
|
|
|
|
|
|
value = *(p_data + 1);
|
|
|
|
*(p_data + 1) = *(p_data + 2);
|
|
|
|
*(p_data + 2) = value;
|
|
|
|
}
|
|
|
|
|
2019-07-22 03:32:51 +00:00
|
|
|
static union {
|
|
|
|
int a;
|
|
|
|
char b;
|
|
|
|
} __ue = { .a = 1 };
|
|
|
|
|
|
|
|
#define is_little_endian() (__ue.b == 1)
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
|
|
|
const uint8 *buf_end = buf + size;
|
|
|
|
const uint8 *p = buf, *p_end = buf_end;
|
|
|
|
uint32 magic_number, version;
|
|
|
|
WASMSection *section_list = NULL;
|
|
|
|
|
2019-11-27 02:52:12 +00:00
|
|
|
CHECK_BUF1(p, p_end, sizeof(uint32));
|
2019-06-14 03:29:08 +00:00
|
|
|
magic_number = read_uint32(p);
|
2019-07-22 03:32:51 +00:00
|
|
|
if (!is_little_endian())
|
2021-10-08 03:44:39 +00:00
|
|
|
exchange32((uint8 *)&magic_number);
|
2019-06-14 03:29:08 +00:00
|
|
|
|
|
|
|
if (magic_number != WASM_MAGIC_NUMBER) {
|
2021-10-08 03:44:39 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "magic header not detected");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-27 02:52:12 +00:00
|
|
|
CHECK_BUF1(p, p_end, sizeof(uint32));
|
2019-06-14 03:29:08 +00:00
|
|
|
version = read_uint32(p);
|
2019-07-22 03:32:51 +00:00
|
|
|
if (!is_little_endian())
|
2021-10-08 03:44:39 +00:00
|
|
|
exchange32((uint8 *)&version);
|
2019-06-14 03:29:08 +00:00
|
|
|
|
|
|
|
if (version != WASM_CURRENT_VERSION) {
|
2021-10-08 03:44:39 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "unknown binary version");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!create_sections(buf, size, §ion_list, error_buf, error_buf_size)
|
2021-11-08 03:27:26 +00:00
|
|
|
|| !load_from_sections(module, section_list, true, error_buf,
|
2021-10-08 03:44:39 +00:00
|
|
|
error_buf_size)) {
|
2019-05-07 02:18:18 +00:00
|
|
|
destroy_sections(section_list);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy_sections(section_list);
|
|
|
|
return true;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2022-12-27 04:59:17 +00:00
|
|
|
#if WASM_ENABLE_LIBC_WASI != 0
|
2021-12-29 04:13:30 +00:00
|
|
|
/**
|
2021-12-29 03:04:36 +00:00
|
|
|
* refer to
|
|
|
|
* https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
|
|
|
|
*/
|
|
|
|
static bool
|
2022-12-27 04:59:17 +00:00
|
|
|
check_wasi_abi_compatibility(const WASMModule *module,
|
|
|
|
#if WASM_ENABLE_MULTI_MODULE != 0
|
|
|
|
bool main_module,
|
|
|
|
#endif
|
2021-12-29 03:04:36 +00:00
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
2021-12-29 04:13:30 +00:00
|
|
|
/**
|
2021-12-29 03:04:36 +00:00
|
|
|
* be careful with:
|
|
|
|
* wasi compatiable modules(command/reactor) which don't import any wasi
|
2021-12-29 04:13:30 +00:00
|
|
|
* APIs. Usually, a command has to import a "prox_exit" at least, but a
|
2021-12-29 03:04:36 +00:00
|
|
|
* reactor can depend on nothing. At the same time, each has its own entry
|
|
|
|
* point.
|
|
|
|
*
|
|
|
|
* observations:
|
|
|
|
* - clang always injects `_start` into a command
|
|
|
|
* - clang always injects `_initialize` into a reactor
|
|
|
|
* - `iwasm -f` allows to run a function in the reactor
|
|
|
|
*
|
|
|
|
* strong assumptions:
|
|
|
|
* - no one will define either `_start` or `_initialize` on purpose
|
|
|
|
* - `_start` should always be `void _start(void)`
|
|
|
|
* - `_initialize` should always be `void _initialize(void)`
|
2022-12-27 04:59:17 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* clang-format off */
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* | | import_wasi_api True | | import_wasi_api False | |
|
|
|
|
* | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
|
|
|
|
* | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
|
|
|
|
* | \_start() Y | N | COMMANDER | N | COMMANDER |
|
|
|
|
* | \_start() N | REACTOR | N | REACTOR | OTHERS |
|
2021-12-29 03:04:36 +00:00
|
|
|
*/
|
2022-12-27 04:59:17 +00:00
|
|
|
/* clang-format on */
|
2021-12-29 03:04:36 +00:00
|
|
|
|
|
|
|
WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
|
|
|
|
|
|
|
|
/* (func (export "_start") (...) */
|
|
|
|
start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
|
|
|
|
error_buf, error_buf_size);
|
|
|
|
if (start) {
|
|
|
|
WASMType *func_type =
|
|
|
|
module->functions[start->index - module->import_function_count]
|
|
|
|
->func_type;
|
|
|
|
if (func_type->param_count || func_type->result_count) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2021-12-29 04:13:30 +00:00
|
|
|
"the signature of builtin _start function is wrong");
|
2021-12-29 03:04:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (func (export "_initialize") (...) */
|
|
|
|
initialize = wasm_loader_find_export(
|
|
|
|
module, "", "_initialize", EXPORT_KIND_FUNC, error_buf, error_buf_size);
|
|
|
|
if (initialize) {
|
|
|
|
WASMType *func_type =
|
|
|
|
module->functions[initialize->index - module->import_function_count]
|
|
|
|
->func_type;
|
|
|
|
if (func_type->param_count || func_type->result_count) {
|
|
|
|
set_error_buf(
|
|
|
|
error_buf, error_buf_size,
|
2021-12-29 04:13:30 +00:00
|
|
|
"the signature of builtin _initialize function is wrong");
|
2021-12-29 03:04:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* filter out non-wasi compatiable modules */
|
|
|
|
if (!module->import_wasi_api && !start && !initialize) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* should have one at least */
|
|
|
|
if (module->import_wasi_api && !start && !initialize) {
|
2023-02-02 05:06:30 +00:00
|
|
|
LOG_WARNING("warning: a module with WASI apis should be either "
|
|
|
|
"a command or a reactor");
|
2021-12-29 03:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* there is at least one of `_start` and `_initialize` in below cases.
|
|
|
|
* according to the assumption, they should be all wasi compatiable
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* always can not have both at the same time */
|
|
|
|
if (start && initialize) {
|
|
|
|
set_error_buf(
|
|
|
|
error_buf, error_buf_size,
|
2021-12-29 04:13:30 +00:00
|
|
|
"neither a command nor a reactor can both have _start function "
|
|
|
|
"and _initialize function at the same time");
|
2021-12-29 03:04:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-12-27 04:59:17 +00:00
|
|
|
#if WASM_ENABLE_MULTI_MODULE != 0
|
2021-12-29 03:04:36 +00:00
|
|
|
/* filter out commands (with `_start`) cases */
|
|
|
|
if (start && !main_module) {
|
2021-12-29 04:13:30 +00:00
|
|
|
set_error_buf(
|
|
|
|
error_buf, error_buf_size,
|
|
|
|
"a command (with _start function) can not be a sub-module");
|
2021-12-29 03:04:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-12-27 04:59:17 +00:00
|
|
|
#endif
|
2021-12-29 03:04:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* it is ok a reactor acts as a main module,
|
|
|
|
* so skip the check about (with `_initialize`)
|
|
|
|
*/
|
|
|
|
|
|
|
|
memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
|
|
|
|
error_buf, error_buf_size);
|
2023-01-16 12:21:51 +00:00
|
|
|
if (!memory
|
|
|
|
#if WASM_ENABLE_LIB_WASI_THREADS != 0
|
|
|
|
/*
|
|
|
|
* with wasi-threads, it's still an open question if a memory
|
|
|
|
* should be exported.
|
|
|
|
*
|
|
|
|
* https://github.com/WebAssembly/wasi-threads/issues/22
|
|
|
|
* https://github.com/WebAssembly/WASI/issues/502
|
|
|
|
*
|
|
|
|
* Note: this code assumes the number of memories is at most 1.
|
|
|
|
*/
|
|
|
|
&& module->import_memory_count == 0
|
|
|
|
#endif
|
|
|
|
) {
|
2021-12-29 04:13:30 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"a module with WASI apis must export memory by default");
|
2021-12-29 03:04:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
WASMModule *
|
2022-03-24 02:08:49 +00:00
|
|
|
wasm_loader_load(uint8 *buf, uint32 size,
|
2021-12-29 03:04:36 +00:00
|
|
|
#if WASM_ENABLE_MULTI_MODULE != 0
|
|
|
|
bool main_module,
|
|
|
|
#endif
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
2020-06-02 06:53:06 +00:00
|
|
|
WASMModule *module = create_module(error_buf, error_buf_size);
|
2019-05-07 02:18:18 +00:00
|
|
|
if (!module) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-08-02 08:03:50 +00:00
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0
|
2021-09-29 05:36:46 +00:00
|
|
|
module->load_addr = (uint8 *)buf;
|
|
|
|
module->load_size = size;
|
|
|
|
#endif
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
if (!load(buf, size, module, error_buf, error_buf_size)) {
|
2019-05-07 02:18:18 +00:00
|
|
|
goto fail;
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2022-12-27 04:59:17 +00:00
|
|
|
#if WASM_ENABLE_LIBC_WASI != 0
|
2021-12-29 04:13:30 +00:00
|
|
|
/* Check the WASI application ABI */
|
2022-12-27 04:59:17 +00:00
|
|
|
if (!check_wasi_abi_compatibility(module,
|
|
|
|
#if WASM_ENABLE_MULTI_MODULE != 0
|
|
|
|
main_module,
|
|
|
|
#endif
|
|
|
|
error_buf, error_buf_size)) {
|
2021-12-29 03:04:36 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-08-20 04:43:12 +00:00
|
|
|
LOG_VERBOSE("Load module success.\n");
|
2019-05-07 02:18:18 +00:00
|
|
|
return module;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
wasm_loader_unload(module);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wasm_loader_unload(WASMModule *module)
|
|
|
|
{
|
|
|
|
uint32 i;
|
|
|
|
|
|
|
|
if (!module)
|
|
|
|
return;
|
|
|
|
|
2023-02-16 03:54:19 +00:00
|
|
|
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
|
|
|
|
&& WASM_ENABLE_LAZY_JIT != 0
|
2022-12-19 03:24:46 +00:00
|
|
|
module->orcjit_stop_compiling = true;
|
|
|
|
if (module->llvm_jit_init_thread)
|
|
|
|
os_thread_join(module->llvm_jit_init_thread, NULL);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
|
|
|
|
/* Stop Fast/LLVM JIT compilation firstly to avoid accessing
|
2022-10-18 12:17:34 +00:00
|
|
|
module internal data after they were freed */
|
|
|
|
orcjit_stop_compile_threads(module);
|
2022-12-19 03:24:46 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if WASM_ENABLE_JIT != 0
|
2022-10-18 12:17:34 +00:00
|
|
|
if (module->func_ptrs)
|
|
|
|
wasm_runtime_free(module->func_ptrs);
|
|
|
|
if (module->comp_ctx)
|
|
|
|
aot_destroy_comp_context(module->comp_ctx);
|
|
|
|
if (module->comp_data)
|
|
|
|
aot_destroy_comp_data(module->comp_data);
|
|
|
|
#endif
|
|
|
|
|
2023-02-16 03:54:19 +00:00
|
|
|
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
|
|
|
|
&& WASM_ENABLE_LAZY_JIT != 0
|
2022-12-19 03:24:46 +00:00
|
|
|
if (module->tierup_wait_lock_inited) {
|
|
|
|
os_mutex_destroy(&module->tierup_wait_lock);
|
|
|
|
os_cond_destroy(&module->tierup_wait_cond);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
if (module->types) {
|
|
|
|
for (i = 0; i < module->type_count; i++) {
|
|
|
|
if (module->types[i])
|
2022-08-18 09:52:02 +00:00
|
|
|
destroy_wasm_type(module->types[i]);
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(module->types);
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (module->imports)
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(module->imports);
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
if (module->functions) {
|
|
|
|
for (i = 0; i < module->function_count; i++) {
|
2020-02-18 07:15:24 +00:00
|
|
|
if (module->functions[i]) {
|
|
|
|
if (module->functions[i]->local_offsets)
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(module->functions[i]->local_offsets);
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
if (module->functions[i]->code_compiled)
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(module->functions[i]->code_compiled);
|
2020-03-07 14:20:38 +00:00
|
|
|
if (module->functions[i]->consts)
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(module->functions[i]->consts);
|
2022-12-19 03:24:46 +00:00
|
|
|
#endif
|
|
|
|
#if WASM_ENABLE_FAST_JIT != 0
|
|
|
|
if (module->functions[i]->fast_jit_jitted_code) {
|
|
|
|
jit_code_cache_free(
|
|
|
|
module->functions[i]->fast_jit_jitted_code);
|
|
|
|
}
|
|
|
|
#if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
|
2023-02-02 10:16:01 +00:00
|
|
|
if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
|
2022-12-19 03:24:46 +00:00
|
|
|
jit_code_cache_free(
|
2023-02-02 10:16:01 +00:00
|
|
|
module->functions[i]->call_to_fast_jit_from_llvm_jit);
|
2022-12-19 03:24:46 +00:00
|
|
|
}
|
|
|
|
#endif
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(module->functions[i]);
|
2020-02-18 07:15:24 +00:00
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(module->functions);
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (module->tables)
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(module->tables);
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
if (module->memories)
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(module->memories);
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
if (module->globals)
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(module->globals);
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
if (module->exports)
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(module->exports);
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
if (module->table_segments) {
|
|
|
|
for (i = 0; i < module->table_seg_count; i++) {
|
|
|
|
if (module->table_segments[i].func_indexes)
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(module->table_segments[i].func_indexes);
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(module->table_segments);
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (module->data_segments) {
|
|
|
|
for (i = 0; i < module->data_seg_count; i++) {
|
|
|
|
if (module->data_segments[i])
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(module->data_segments[i]);
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(module->data_segments);
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-18 07:15:24 +00:00
|
|
|
if (module->const_str_list) {
|
|
|
|
StringNode *node = module->const_str_list, *node_next;
|
|
|
|
while (node) {
|
|
|
|
node_next = node->next;
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(node);
|
2020-02-18 07:15:24 +00:00
|
|
|
node = node_next;
|
|
|
|
}
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2022-04-23 11:15:55 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP == 0
|
|
|
|
if (module->br_table_cache_list) {
|
|
|
|
BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
|
|
|
|
BrTableCache *node_next;
|
|
|
|
while (node) {
|
|
|
|
node_next = bh_list_elem_next(node);
|
|
|
|
wasm_runtime_free(node);
|
|
|
|
node = node_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
#if WASM_ENABLE_MULTI_MODULE != 0
|
|
|
|
/* just release the sub module list */
|
|
|
|
if (module->import_module_list) {
|
|
|
|
WASMRegisteredModule *node =
|
2021-10-08 03:44:39 +00:00
|
|
|
bh_list_first_elem(module->import_module_list);
|
2020-06-02 06:53:06 +00:00
|
|
|
while (node) {
|
|
|
|
WASMRegisteredModule *next = bh_list_elem_next(node);
|
|
|
|
bh_list_remove(module->import_module_list, node);
|
|
|
|
/*
|
|
|
|
* unload(sub_module) will be trigged during runtime_destroy().
|
|
|
|
* every module in the global module list will be unloaded one by
|
|
|
|
* one. so don't worry.
|
|
|
|
*/
|
|
|
|
wasm_runtime_free(node);
|
|
|
|
/*
|
|
|
|
* the module file reading buffer will be released
|
|
|
|
* in runtime_destroy()
|
|
|
|
*/
|
|
|
|
node = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2022-06-10 13:51:13 +00:00
|
|
|
|
2021-09-29 05:36:46 +00:00
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0
|
|
|
|
WASMFastOPCodeNode *fast_opcode =
|
|
|
|
bh_list_first_elem(&module->fast_opcode_list);
|
2021-10-08 03:44:39 +00:00
|
|
|
while (fast_opcode) {
|
|
|
|
WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
|
2021-09-29 05:36:46 +00:00
|
|
|
wasm_runtime_free(fast_opcode);
|
|
|
|
fast_opcode = next;
|
|
|
|
}
|
2022-12-19 03:24:46 +00:00
|
|
|
#endif
|
|
|
|
|
2023-02-16 03:54:19 +00:00
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0 \
|
|
|
|
|| (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
|
2022-12-19 03:24:46 +00:00
|
|
|
&& WASM_ENABLE_LAZY_JIT != 0)
|
|
|
|
os_mutex_destroy(&module->instance_list_lock);
|
2021-09-29 05:36:46 +00:00
|
|
|
#endif
|
2022-06-10 13:51:13 +00:00
|
|
|
|
|
|
|
#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
|
2022-06-15 10:14:39 +00:00
|
|
|
wasm_runtime_destroy_custom_sections(module->custom_section_list);
|
2022-06-10 13:51:13 +00:00
|
|
|
#endif
|
|
|
|
|
2022-08-02 08:03:50 +00:00
|
|
|
#if WASM_ENABLE_FAST_JIT != 0
|
|
|
|
if (module->fast_jit_func_ptrs) {
|
2022-11-22 06:04:48 +00:00
|
|
|
wasm_runtime_free(module->fast_jit_func_ptrs);
|
2022-11-21 02:42:18 +00:00
|
|
|
}
|
2022-12-19 03:24:46 +00:00
|
|
|
|
|
|
|
for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
|
|
|
|
if (module->fast_jit_thread_locks_inited[i]) {
|
|
|
|
os_mutex_destroy(&module->fast_jit_thread_locks[i]);
|
|
|
|
}
|
|
|
|
}
|
2022-08-02 08:03:50 +00:00
|
|
|
#endif
|
|
|
|
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(module);
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2021-10-08 03:44:39 +00:00
|
|
|
wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
|
|
|
|
const uint8 *start_addr, const uint8 *code_end_addr,
|
|
|
|
uint8 label_type, uint8 **p_else_addr,
|
2020-12-28 02:46:31 +00:00
|
|
|
uint8 **p_end_addr)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
|
|
|
const uint8 *p = start_addr, *p_end = code_end_addr;
|
|
|
|
uint8 *else_addr = NULL;
|
2020-12-28 02:46:31 +00:00
|
|
|
char error_buf[128];
|
2020-05-15 09:44:36 +00:00
|
|
|
uint32 block_nested_depth = 1, count, i, j, t;
|
2020-12-28 02:46:31 +00:00
|
|
|
uint32 error_buf_size = sizeof(error_buf);
|
2019-05-07 02:18:18 +00:00
|
|
|
uint8 opcode, u8;
|
2021-10-08 03:44:39 +00:00
|
|
|
BlockAddr block_stack[16] = { { 0 } }, *block;
|
2019-05-23 10:03:31 +00:00
|
|
|
|
2020-12-28 02:46:31 +00:00
|
|
|
i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
|
2020-02-22 13:05:09 +00:00
|
|
|
block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
|
2019-05-23 10:03:31 +00:00
|
|
|
|
2020-05-15 09:44:36 +00:00
|
|
|
for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
|
|
|
|
if (block[j].start_addr == start_addr) {
|
|
|
|
/* Cache hit */
|
|
|
|
*p_else_addr = block[j].else_addr;
|
|
|
|
*p_end_addr = block[j].end_addr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 10:03:31 +00:00
|
|
|
/* Cache unhit */
|
|
|
|
block_stack[0].start_addr = start_addr;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
while (p < code_end_addr) {
|
|
|
|
opcode = *p++;
|
2021-09-29 05:36:46 +00:00
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
op_break_retry:
|
2021-09-29 05:36:46 +00:00
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
switch (opcode) {
|
|
|
|
case WASM_OP_UNREACHABLE:
|
|
|
|
case WASM_OP_NOP:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_BLOCK:
|
|
|
|
case WASM_OP_LOOP:
|
|
|
|
case WASM_OP_IF:
|
2019-12-13 07:30:30 +00:00
|
|
|
/* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
|
|
|
|
u8 = read_uint8(p);
|
2021-10-08 03:44:39 +00:00
|
|
|
if (block_nested_depth
|
|
|
|
< sizeof(block_stack) / sizeof(BlockAddr)) {
|
2019-05-23 10:03:31 +00:00
|
|
|
block_stack[block_nested_depth].start_addr = p;
|
|
|
|
block_stack[block_nested_depth].else_addr = NULL;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
block_nested_depth++;
|
|
|
|
break;
|
|
|
|
|
2020-07-10 08:29:15 +00:00
|
|
|
case EXT_OP_BLOCK:
|
|
|
|
case EXT_OP_LOOP:
|
|
|
|
case EXT_OP_IF:
|
|
|
|
/* block type */
|
|
|
|
skip_leb_uint32(p, p_end);
|
2021-10-08 03:44:39 +00:00
|
|
|
if (block_nested_depth
|
|
|
|
< sizeof(block_stack) / sizeof(BlockAddr)) {
|
2020-07-10 08:29:15 +00:00
|
|
|
block_stack[block_nested_depth].start_addr = p;
|
|
|
|
block_stack[block_nested_depth].else_addr = NULL;
|
|
|
|
}
|
|
|
|
block_nested_depth++;
|
|
|
|
break;
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
case WASM_OP_ELSE:
|
2020-07-10 08:29:15 +00:00
|
|
|
if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
|
2021-10-08 03:44:39 +00:00
|
|
|
else_addr = (uint8 *)(p - 1);
|
|
|
|
if (block_nested_depth - 1
|
|
|
|
< sizeof(block_stack) / sizeof(BlockAddr))
|
|
|
|
block_stack[block_nested_depth - 1].else_addr =
|
|
|
|
(uint8 *)(p - 1);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_END:
|
|
|
|
if (block_nested_depth == 1) {
|
2020-07-10 08:29:15 +00:00
|
|
|
if (label_type == LABEL_TYPE_IF)
|
2019-05-07 02:18:18 +00:00
|
|
|
*p_else_addr = else_addr;
|
2021-10-08 03:44:39 +00:00
|
|
|
*p_end_addr = (uint8 *)(p - 1);
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
block_stack[0].end_addr = (uint8 *)(p - 1);
|
|
|
|
for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
|
|
|
|
t++) {
|
2019-05-23 10:03:31 +00:00
|
|
|
start_addr = block_stack[t].start_addr;
|
|
|
|
if (start_addr) {
|
2020-12-28 02:46:31 +00:00
|
|
|
i = ((uintptr_t)start_addr)
|
|
|
|
& (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
|
2021-10-08 03:44:39 +00:00
|
|
|
block =
|
|
|
|
block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
|
2019-05-23 10:03:31 +00:00
|
|
|
for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
|
|
|
|
if (!block[j].start_addr)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (j == BLOCK_ADDR_CONFLICT_SIZE) {
|
2021-10-08 03:44:39 +00:00
|
|
|
memmove(block + 1, block,
|
|
|
|
(BLOCK_ADDR_CONFLICT_SIZE - 1)
|
|
|
|
* sizeof(BlockAddr));
|
2019-05-23 10:03:31 +00:00
|
|
|
j = 0;
|
|
|
|
}
|
|
|
|
block[j].start_addr = block_stack[t].start_addr;
|
|
|
|
block[j].else_addr = block_stack[t].else_addr;
|
|
|
|
block[j].end_addr = block_stack[t].end_addr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-05-23 10:03:31 +00:00
|
|
|
else {
|
2019-05-07 02:18:18 +00:00
|
|
|
block_nested_depth--;
|
2021-10-08 03:44:39 +00:00
|
|
|
if (block_nested_depth
|
|
|
|
< sizeof(block_stack) / sizeof(BlockAddr))
|
|
|
|
block_stack[block_nested_depth].end_addr =
|
|
|
|
(uint8 *)(p - 1);
|
2019-05-23 10:03:31 +00:00
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_BR:
|
|
|
|
case WASM_OP_BR_IF:
|
2020-02-13 07:51:22 +00:00
|
|
|
skip_leb_uint32(p, p_end); /* labelidx */
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_BR_TABLE:
|
|
|
|
read_leb_uint32(p, p_end, count); /* lable num */
|
2022-04-23 11:15:55 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
for (i = 0; i <= count; i++) /* lableidxs */
|
2020-02-13 07:51:22 +00:00
|
|
|
skip_leb_uint32(p, p_end);
|
2022-04-23 11:15:55 +00:00
|
|
|
#else
|
|
|
|
p += count + 1;
|
|
|
|
while (*p == WASM_OP_NOP)
|
|
|
|
p++;
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
2022-04-23 11:15:55 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP == 0
|
|
|
|
case EXT_OP_BR_TABLE_CACHE:
|
|
|
|
read_leb_uint32(p, p_end, count); /* lable num */
|
|
|
|
while (*p == WASM_OP_NOP)
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
case WASM_OP_RETURN:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_CALL:
|
2020-09-24 04:38:54 +00:00
|
|
|
#if WASM_ENABLE_TAIL_CALL != 0
|
|
|
|
case WASM_OP_RETURN_CALL:
|
|
|
|
#endif
|
2020-02-13 07:51:22 +00:00
|
|
|
skip_leb_uint32(p, p_end); /* funcidx */
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_CALL_INDIRECT:
|
2020-09-24 04:38:54 +00:00
|
|
|
#if WASM_ENABLE_TAIL_CALL != 0
|
|
|
|
case WASM_OP_RETURN_CALL_INDIRECT:
|
|
|
|
#endif
|
2020-02-13 07:51:22 +00:00
|
|
|
skip_leb_uint32(p, p_end); /* typeidx */
|
2019-12-13 07:30:30 +00:00
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
u8 = read_uint8(p); /* 0x00 */
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_DROP:
|
|
|
|
case WASM_OP_SELECT:
|
|
|
|
case WASM_OP_DROP_64:
|
|
|
|
case WASM_OP_SELECT_64:
|
|
|
|
break;
|
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
#if WASM_ENABLE_REF_TYPES != 0
|
|
|
|
case WASM_OP_SELECT_T:
|
|
|
|
skip_leb_uint32(p, p_end); /* vec length */
|
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
u8 = read_uint8(p); /* typeidx */
|
|
|
|
break;
|
|
|
|
case WASM_OP_TABLE_GET:
|
|
|
|
case WASM_OP_TABLE_SET:
|
|
|
|
skip_leb_uint32(p, p_end); /* table index */
|
|
|
|
break;
|
|
|
|
case WASM_OP_REF_NULL:
|
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
u8 = read_uint8(p); /* type */
|
|
|
|
break;
|
|
|
|
case WASM_OP_REF_IS_NULL:
|
|
|
|
break;
|
|
|
|
case WASM_OP_REF_FUNC:
|
|
|
|
skip_leb_uint32(p, p_end); /* func index */
|
|
|
|
break;
|
|
|
|
#endif /* WASM_ENABLE_REF_TYPES */
|
2019-05-07 02:18:18 +00:00
|
|
|
case WASM_OP_GET_LOCAL:
|
|
|
|
case WASM_OP_SET_LOCAL:
|
|
|
|
case WASM_OP_TEE_LOCAL:
|
|
|
|
case WASM_OP_GET_GLOBAL:
|
|
|
|
case WASM_OP_SET_GLOBAL:
|
2020-06-29 06:17:27 +00:00
|
|
|
case WASM_OP_GET_GLOBAL_64:
|
|
|
|
case WASM_OP_SET_GLOBAL_64:
|
|
|
|
case WASM_OP_SET_GLOBAL_AUX_STACK:
|
2021-04-15 03:29:20 +00:00
|
|
|
skip_leb_uint32(p, p_end); /* local index */
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
case EXT_OP_GET_LOCAL_FAST:
|
|
|
|
case EXT_OP_SET_LOCAL_FAST:
|
|
|
|
case EXT_OP_TEE_LOCAL_FAST:
|
2020-02-10 04:36:45 +00:00
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
case WASM_OP_I32_LOAD:
|
|
|
|
case WASM_OP_I64_LOAD:
|
|
|
|
case WASM_OP_F32_LOAD:
|
|
|
|
case WASM_OP_F64_LOAD:
|
|
|
|
case WASM_OP_I32_LOAD8_S:
|
|
|
|
case WASM_OP_I32_LOAD8_U:
|
|
|
|
case WASM_OP_I32_LOAD16_S:
|
|
|
|
case WASM_OP_I32_LOAD16_U:
|
|
|
|
case WASM_OP_I64_LOAD8_S:
|
|
|
|
case WASM_OP_I64_LOAD8_U:
|
|
|
|
case WASM_OP_I64_LOAD16_S:
|
|
|
|
case WASM_OP_I64_LOAD16_U:
|
|
|
|
case WASM_OP_I64_LOAD32_S:
|
|
|
|
case WASM_OP_I64_LOAD32_U:
|
|
|
|
case WASM_OP_I32_STORE:
|
|
|
|
case WASM_OP_I64_STORE:
|
|
|
|
case WASM_OP_F32_STORE:
|
|
|
|
case WASM_OP_F64_STORE:
|
|
|
|
case WASM_OP_I32_STORE8:
|
|
|
|
case WASM_OP_I32_STORE16:
|
|
|
|
case WASM_OP_I64_STORE8:
|
|
|
|
case WASM_OP_I64_STORE16:
|
|
|
|
case WASM_OP_I64_STORE32:
|
2020-02-13 07:51:22 +00:00
|
|
|
skip_leb_uint32(p, p_end); /* align */
|
|
|
|
skip_leb_uint32(p, p_end); /* offset */
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_MEMORY_SIZE:
|
|
|
|
case WASM_OP_MEMORY_GROW:
|
2020-02-13 07:51:22 +00:00
|
|
|
skip_leb_uint32(p, p_end); /* 0x00 */
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_I32_CONST:
|
2020-02-13 07:51:22 +00:00
|
|
|
skip_leb_int32(p, p_end);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
case WASM_OP_I64_CONST:
|
2020-02-13 07:51:22 +00:00
|
|
|
skip_leb_int64(p, p_end);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
case WASM_OP_F32_CONST:
|
|
|
|
p += sizeof(float32);
|
|
|
|
break;
|
|
|
|
case WASM_OP_F64_CONST:
|
|
|
|
p += sizeof(float64);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_I32_EQZ:
|
|
|
|
case WASM_OP_I32_EQ:
|
|
|
|
case WASM_OP_I32_NE:
|
|
|
|
case WASM_OP_I32_LT_S:
|
|
|
|
case WASM_OP_I32_LT_U:
|
|
|
|
case WASM_OP_I32_GT_S:
|
|
|
|
case WASM_OP_I32_GT_U:
|
|
|
|
case WASM_OP_I32_LE_S:
|
|
|
|
case WASM_OP_I32_LE_U:
|
|
|
|
case WASM_OP_I32_GE_S:
|
|
|
|
case WASM_OP_I32_GE_U:
|
|
|
|
case WASM_OP_I64_EQZ:
|
|
|
|
case WASM_OP_I64_EQ:
|
|
|
|
case WASM_OP_I64_NE:
|
|
|
|
case WASM_OP_I64_LT_S:
|
|
|
|
case WASM_OP_I64_LT_U:
|
|
|
|
case WASM_OP_I64_GT_S:
|
|
|
|
case WASM_OP_I64_GT_U:
|
|
|
|
case WASM_OP_I64_LE_S:
|
|
|
|
case WASM_OP_I64_LE_U:
|
|
|
|
case WASM_OP_I64_GE_S:
|
|
|
|
case WASM_OP_I64_GE_U:
|
|
|
|
case WASM_OP_F32_EQ:
|
|
|
|
case WASM_OP_F32_NE:
|
|
|
|
case WASM_OP_F32_LT:
|
|
|
|
case WASM_OP_F32_GT:
|
|
|
|
case WASM_OP_F32_LE:
|
|
|
|
case WASM_OP_F32_GE:
|
|
|
|
case WASM_OP_F64_EQ:
|
|
|
|
case WASM_OP_F64_NE:
|
|
|
|
case WASM_OP_F64_LT:
|
|
|
|
case WASM_OP_F64_GT:
|
|
|
|
case WASM_OP_F64_LE:
|
|
|
|
case WASM_OP_F64_GE:
|
|
|
|
case WASM_OP_I32_CLZ:
|
|
|
|
case WASM_OP_I32_CTZ:
|
|
|
|
case WASM_OP_I32_POPCNT:
|
|
|
|
case WASM_OP_I32_ADD:
|
|
|
|
case WASM_OP_I32_SUB:
|
|
|
|
case WASM_OP_I32_MUL:
|
|
|
|
case WASM_OP_I32_DIV_S:
|
|
|
|
case WASM_OP_I32_DIV_U:
|
|
|
|
case WASM_OP_I32_REM_S:
|
|
|
|
case WASM_OP_I32_REM_U:
|
|
|
|
case WASM_OP_I32_AND:
|
|
|
|
case WASM_OP_I32_OR:
|
|
|
|
case WASM_OP_I32_XOR:
|
|
|
|
case WASM_OP_I32_SHL:
|
|
|
|
case WASM_OP_I32_SHR_S:
|
|
|
|
case WASM_OP_I32_SHR_U:
|
|
|
|
case WASM_OP_I32_ROTL:
|
|
|
|
case WASM_OP_I32_ROTR:
|
|
|
|
case WASM_OP_I64_CLZ:
|
|
|
|
case WASM_OP_I64_CTZ:
|
|
|
|
case WASM_OP_I64_POPCNT:
|
|
|
|
case WASM_OP_I64_ADD:
|
|
|
|
case WASM_OP_I64_SUB:
|
|
|
|
case WASM_OP_I64_MUL:
|
|
|
|
case WASM_OP_I64_DIV_S:
|
|
|
|
case WASM_OP_I64_DIV_U:
|
|
|
|
case WASM_OP_I64_REM_S:
|
|
|
|
case WASM_OP_I64_REM_U:
|
|
|
|
case WASM_OP_I64_AND:
|
|
|
|
case WASM_OP_I64_OR:
|
|
|
|
case WASM_OP_I64_XOR:
|
|
|
|
case WASM_OP_I64_SHL:
|
|
|
|
case WASM_OP_I64_SHR_S:
|
|
|
|
case WASM_OP_I64_SHR_U:
|
|
|
|
case WASM_OP_I64_ROTL:
|
|
|
|
case WASM_OP_I64_ROTR:
|
|
|
|
case WASM_OP_F32_ABS:
|
|
|
|
case WASM_OP_F32_NEG:
|
|
|
|
case WASM_OP_F32_CEIL:
|
|
|
|
case WASM_OP_F32_FLOOR:
|
|
|
|
case WASM_OP_F32_TRUNC:
|
|
|
|
case WASM_OP_F32_NEAREST:
|
|
|
|
case WASM_OP_F32_SQRT:
|
|
|
|
case WASM_OP_F32_ADD:
|
|
|
|
case WASM_OP_F32_SUB:
|
|
|
|
case WASM_OP_F32_MUL:
|
|
|
|
case WASM_OP_F32_DIV:
|
|
|
|
case WASM_OP_F32_MIN:
|
|
|
|
case WASM_OP_F32_MAX:
|
|
|
|
case WASM_OP_F32_COPYSIGN:
|
|
|
|
case WASM_OP_F64_ABS:
|
|
|
|
case WASM_OP_F64_NEG:
|
|
|
|
case WASM_OP_F64_CEIL:
|
|
|
|
case WASM_OP_F64_FLOOR:
|
|
|
|
case WASM_OP_F64_TRUNC:
|
|
|
|
case WASM_OP_F64_NEAREST:
|
|
|
|
case WASM_OP_F64_SQRT:
|
|
|
|
case WASM_OP_F64_ADD:
|
|
|
|
case WASM_OP_F64_SUB:
|
|
|
|
case WASM_OP_F64_MUL:
|
|
|
|
case WASM_OP_F64_DIV:
|
|
|
|
case WASM_OP_F64_MIN:
|
|
|
|
case WASM_OP_F64_MAX:
|
|
|
|
case WASM_OP_F64_COPYSIGN:
|
|
|
|
case WASM_OP_I32_WRAP_I64:
|
|
|
|
case WASM_OP_I32_TRUNC_S_F32:
|
|
|
|
case WASM_OP_I32_TRUNC_U_F32:
|
|
|
|
case WASM_OP_I32_TRUNC_S_F64:
|
|
|
|
case WASM_OP_I32_TRUNC_U_F64:
|
|
|
|
case WASM_OP_I64_EXTEND_S_I32:
|
|
|
|
case WASM_OP_I64_EXTEND_U_I32:
|
|
|
|
case WASM_OP_I64_TRUNC_S_F32:
|
|
|
|
case WASM_OP_I64_TRUNC_U_F32:
|
|
|
|
case WASM_OP_I64_TRUNC_S_F64:
|
|
|
|
case WASM_OP_I64_TRUNC_U_F64:
|
|
|
|
case WASM_OP_F32_CONVERT_S_I32:
|
|
|
|
case WASM_OP_F32_CONVERT_U_I32:
|
|
|
|
case WASM_OP_F32_CONVERT_S_I64:
|
|
|
|
case WASM_OP_F32_CONVERT_U_I64:
|
|
|
|
case WASM_OP_F32_DEMOTE_F64:
|
|
|
|
case WASM_OP_F64_CONVERT_S_I32:
|
|
|
|
case WASM_OP_F64_CONVERT_U_I32:
|
|
|
|
case WASM_OP_F64_CONVERT_S_I64:
|
|
|
|
case WASM_OP_F64_CONVERT_U_I64:
|
|
|
|
case WASM_OP_F64_PROMOTE_F32:
|
|
|
|
case WASM_OP_I32_REINTERPRET_F32:
|
|
|
|
case WASM_OP_I64_REINTERPRET_F64:
|
|
|
|
case WASM_OP_F32_REINTERPRET_I32:
|
|
|
|
case WASM_OP_F64_REINTERPRET_I64:
|
2020-04-30 09:52:11 +00:00
|
|
|
case WASM_OP_I32_EXTEND8_S:
|
|
|
|
case WASM_OP_I32_EXTEND16_S:
|
|
|
|
case WASM_OP_I64_EXTEND8_S:
|
|
|
|
case WASM_OP_I64_EXTEND16_S:
|
|
|
|
case WASM_OP_I64_EXTEND32_S:
|
|
|
|
break;
|
|
|
|
case WASM_OP_MISC_PREFIX:
|
2020-06-02 06:53:06 +00:00
|
|
|
{
|
2020-08-20 04:43:12 +00:00
|
|
|
uint32 opcode1;
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, opcode1);
|
|
|
|
|
|
|
|
switch (opcode1) {
|
2020-06-02 06:53:06 +00:00
|
|
|
case WASM_OP_I32_TRUNC_SAT_S_F32:
|
|
|
|
case WASM_OP_I32_TRUNC_SAT_U_F32:
|
|
|
|
case WASM_OP_I32_TRUNC_SAT_S_F64:
|
|
|
|
case WASM_OP_I32_TRUNC_SAT_U_F64:
|
|
|
|
case WASM_OP_I64_TRUNC_SAT_S_F32:
|
|
|
|
case WASM_OP_I64_TRUNC_SAT_U_F32:
|
|
|
|
case WASM_OP_I64_TRUNC_SAT_S_F64:
|
|
|
|
case WASM_OP_I64_TRUNC_SAT_U_F64:
|
|
|
|
break;
|
|
|
|
#if WASM_ENABLE_BULK_MEMORY != 0
|
|
|
|
case WASM_OP_MEMORY_INIT:
|
|
|
|
skip_leb_uint32(p, p_end);
|
|
|
|
/* skip memory idx */
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
case WASM_OP_DATA_DROP:
|
|
|
|
skip_leb_uint32(p, p_end);
|
|
|
|
break;
|
|
|
|
case WASM_OP_MEMORY_COPY:
|
|
|
|
/* skip two memory idx */
|
|
|
|
p += 2;
|
|
|
|
break;
|
|
|
|
case WASM_OP_MEMORY_FILL:
|
|
|
|
/* skip memory idx */
|
|
|
|
p++;
|
|
|
|
break;
|
2021-04-15 03:29:20 +00:00
|
|
|
#endif /* WASM_ENABLE_BULK_MEMORY */
|
|
|
|
#if WASM_ENABLE_REF_TYPES != 0
|
|
|
|
case WASM_OP_TABLE_INIT:
|
|
|
|
case WASM_OP_TABLE_COPY:
|
|
|
|
/* tableidx */
|
|
|
|
skip_leb_uint32(p, p_end);
|
|
|
|
/* elemidx */
|
|
|
|
skip_leb_uint32(p, p_end);
|
|
|
|
break;
|
|
|
|
case WASM_OP_ELEM_DROP:
|
|
|
|
/* elemidx */
|
|
|
|
skip_leb_uint32(p, p_end);
|
|
|
|
break;
|
|
|
|
case WASM_OP_TABLE_SIZE:
|
|
|
|
case WASM_OP_TABLE_GROW:
|
|
|
|
case WASM_OP_TABLE_FILL:
|
|
|
|
skip_leb_uint32(p, p_end); /* table idx */
|
|
|
|
break;
|
|
|
|
#endif /* WASM_ENABLE_REF_TYPES */
|
2020-06-02 06:53:06 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
2020-11-05 10:15:15 +00:00
|
|
|
|
|
|
|
#if WASM_ENABLE_SIMD != 0
|
|
|
|
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
|
|
|
|
case WASM_OP_SIMD_PREFIX:
|
|
|
|
{
|
2021-10-08 03:44:39 +00:00
|
|
|
/* TODO: shall we ceate a table to be friendly to branch
|
|
|
|
* prediction */
|
2020-11-05 10:15:15 +00:00
|
|
|
opcode = read_uint8(p);
|
2021-10-08 03:44:39 +00:00
|
|
|
/* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
|
|
|
|
*/
|
2020-11-05 10:15:15 +00:00
|
|
|
switch (opcode) {
|
|
|
|
case SIMD_v128_load:
|
2021-09-17 11:12:57 +00:00
|
|
|
case SIMD_v128_load8x8_s:
|
|
|
|
case SIMD_v128_load8x8_u:
|
|
|
|
case SIMD_v128_load16x4_s:
|
|
|
|
case SIMD_v128_load16x4_u:
|
|
|
|
case SIMD_v128_load32x2_s:
|
|
|
|
case SIMD_v128_load32x2_u:
|
|
|
|
case SIMD_v128_load8_splat:
|
|
|
|
case SIMD_v128_load16_splat:
|
|
|
|
case SIMD_v128_load32_splat:
|
|
|
|
case SIMD_v128_load64_splat:
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_v128_store:
|
2021-09-17 11:12:57 +00:00
|
|
|
/* memarg align */
|
|
|
|
skip_leb_uint32(p, p_end);
|
|
|
|
/* memarg offset*/
|
|
|
|
skip_leb_uint32(p, p_end);
|
2020-11-05 10:15:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SIMD_v128_const:
|
|
|
|
case SIMD_v8x16_shuffle:
|
2021-09-17 11:12:57 +00:00
|
|
|
/* immByte[16] immLaneId[16] */
|
2020-11-05 10:15:15 +00:00
|
|
|
CHECK_BUF1(p, p_end, 16);
|
|
|
|
p += 16;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIMD_i8x16_extract_lane_s:
|
|
|
|
case SIMD_i8x16_extract_lane_u:
|
|
|
|
case SIMD_i8x16_replace_lane:
|
|
|
|
case SIMD_i16x8_extract_lane_s:
|
|
|
|
case SIMD_i16x8_extract_lane_u:
|
|
|
|
case SIMD_i16x8_replace_lane:
|
|
|
|
case SIMD_i32x4_extract_lane:
|
|
|
|
case SIMD_i32x4_replace_lane:
|
|
|
|
case SIMD_i64x2_extract_lane:
|
|
|
|
case SIMD_i64x2_replace_lane:
|
|
|
|
case SIMD_f32x4_extract_lane:
|
|
|
|
case SIMD_f32x4_replace_lane:
|
|
|
|
case SIMD_f64x2_extract_lane:
|
|
|
|
case SIMD_f64x2_replace_lane:
|
2021-09-17 11:12:57 +00:00
|
|
|
/* ImmLaneId */
|
2020-11-05 10:15:15 +00:00
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
|
2021-09-17 11:12:57 +00:00
|
|
|
case SIMD_v128_load8_lane:
|
|
|
|
case SIMD_v128_load16_lane:
|
|
|
|
case SIMD_v128_load32_lane:
|
|
|
|
case SIMD_v128_load64_lane:
|
|
|
|
case SIMD_v128_store8_lane:
|
|
|
|
case SIMD_v128_store16_lane:
|
|
|
|
case SIMD_v128_store32_lane:
|
|
|
|
case SIMD_v128_store64_lane:
|
|
|
|
/* memarg align */
|
|
|
|
skip_leb_uint32(p, p_end);
|
|
|
|
/* memarg offset*/
|
|
|
|
skip_leb_uint32(p, p_end);
|
|
|
|
/* ImmLaneId */
|
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIMD_v128_load32_zero:
|
|
|
|
case SIMD_v128_load64_zero:
|
|
|
|
/* memarg align */
|
|
|
|
skip_leb_uint32(p, p_end);
|
|
|
|
/* memarg offset*/
|
|
|
|
skip_leb_uint32(p, p_end);
|
|
|
|
break;
|
|
|
|
|
2020-11-05 10:15:15 +00:00
|
|
|
default:
|
2021-09-17 11:12:57 +00:00
|
|
|
/*
|
|
|
|
* since latest SIMD specific used almost every value
|
2021-10-08 03:44:39 +00:00
|
|
|
* from 0x00 to 0xff, the default branch will present
|
|
|
|
* all opcodes without imm
|
2021-09-17 11:12:57 +00:00
|
|
|
* https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
|
|
|
|
*/
|
|
|
|
break;
|
2020-11-05 10:15:15 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
|
|
|
|
#endif /* end of WASM_ENABLE_SIMD */
|
|
|
|
|
2020-08-03 03:30:26 +00:00
|
|
|
#if WASM_ENABLE_SHARED_MEMORY != 0
|
|
|
|
case WASM_OP_ATOMIC_PREFIX:
|
|
|
|
{
|
|
|
|
/* atomic_op (1 u8) + memarg (2 u32_leb) */
|
|
|
|
opcode = read_uint8(p);
|
|
|
|
if (opcode != WASM_OP_ATOMIC_FENCE) {
|
|
|
|
skip_leb_uint32(p, p_end); /* align */
|
|
|
|
skip_leb_uint32(p, p_end); /* offset */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* atomic.fence doesn't have memarg */
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2021-09-29 05:36:46 +00:00
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
case DEBUG_OP_BREAK:
|
|
|
|
{
|
2021-09-29 05:36:46 +00:00
|
|
|
WASMDebugInstance *debug_instance =
|
2021-10-08 03:44:39 +00:00
|
|
|
wasm_exec_env_get_instance(exec_env);
|
2021-09-29 05:36:46 +00:00
|
|
|
char orignal_opcode[1];
|
|
|
|
uint64 size = 1;
|
|
|
|
WASMModuleInstance *module_inst =
|
2021-10-08 03:44:39 +00:00
|
|
|
(WASMModuleInstance *)exec_env->module_inst;
|
2021-09-29 05:36:46 +00:00
|
|
|
uint64 offset = (p - 1) >= module_inst->module->load_addr
|
2021-10-08 03:44:39 +00:00
|
|
|
? (p - 1) - module_inst->module->load_addr
|
|
|
|
: ~0;
|
2021-09-29 05:36:46 +00:00
|
|
|
if (debug_instance) {
|
2021-10-08 03:44:39 +00:00
|
|
|
if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
|
|
|
|
orignal_opcode, &size)
|
2021-09-29 05:36:46 +00:00
|
|
|
&& size == 1) {
|
|
|
|
LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
|
|
|
|
"with %02x: ",
|
|
|
|
orignal_opcode[0]);
|
|
|
|
opcode = orignal_opcode[0];
|
|
|
|
goto op_break_retry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2020-11-05 10:15:15 +00:00
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
default:
|
2019-08-08 08:53:56 +00:00
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)u8;
|
2022-12-27 04:59:17 +00:00
|
|
|
(void)exec_env;
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
2020-08-20 04:43:12 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
#define REF_ANY VALUE_TYPE_ANY
|
|
|
|
#define REF_I32 VALUE_TYPE_I32
|
|
|
|
#define REF_F32 VALUE_TYPE_F32
|
2019-05-07 02:18:18 +00:00
|
|
|
#define REF_I64_1 VALUE_TYPE_I64
|
|
|
|
#define REF_I64_2 VALUE_TYPE_I64
|
|
|
|
#define REF_F64_1 VALUE_TYPE_F64
|
|
|
|
#define REF_F64_2 VALUE_TYPE_F64
|
2020-11-05 10:15:15 +00:00
|
|
|
#define REF_V128_1 VALUE_TYPE_V128
|
|
|
|
#define REF_V128_2 VALUE_TYPE_V128
|
|
|
|
#define REF_V128_3 VALUE_TYPE_V128
|
|
|
|
#define REF_V128_4 VALUE_TYPE_V128
|
2021-10-08 03:44:39 +00:00
|
|
|
#define REF_FUNCREF VALUE_TYPE_FUNCREF
|
2021-04-15 03:29:20 +00:00
|
|
|
#define REF_EXTERNREF VALUE_TYPE_EXTERNREF
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
|
|
|
|
#if WASM_DEBUG_PREPROCESSOR != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
#define LOG_OP(...) os_printf(__VA_ARGS__)
|
2020-03-07 14:20:38 +00:00
|
|
|
#else
|
2021-10-08 03:44:39 +00:00
|
|
|
#define LOG_OP(...) (void)0
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define PATCH_ELSE 0
|
2021-10-08 03:44:39 +00:00
|
|
|
#define PATCH_END 1
|
2020-03-07 14:20:38 +00:00
|
|
|
typedef struct BranchBlockPatch {
|
|
|
|
struct BranchBlockPatch *next;
|
|
|
|
uint8 patch_type;
|
|
|
|
uint8 *code_compiled;
|
|
|
|
} BranchBlockPatch;
|
|
|
|
#endif
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
typedef struct BranchBlock {
|
2020-07-10 08:29:15 +00:00
|
|
|
uint8 label_type;
|
|
|
|
BlockType block_type;
|
2019-05-07 02:18:18 +00:00
|
|
|
uint8 *start_addr;
|
|
|
|
uint8 *else_addr;
|
|
|
|
uint8 *end_addr;
|
|
|
|
uint32 stack_cell_num;
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
uint16 dynamic_offset;
|
|
|
|
uint8 *code_compiled;
|
|
|
|
BranchBlockPatch *patch_list;
|
2020-07-10 08:29:15 +00:00
|
|
|
/* This is used to save params frame_offset of of if block */
|
|
|
|
int16 *param_frame_offsets;
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2020-06-02 06:53:06 +00:00
|
|
|
|
|
|
|
/* Indicate the operand stack is in polymorphic state.
|
|
|
|
* If the opcode is one of unreachable/br/br_table/return, stack is marked
|
|
|
|
* to polymorphic state until the block's 'end' opcode is processed.
|
|
|
|
* If stack is in polymorphic state and stack is empty, instruction can
|
|
|
|
* pop any type of value directly without decreasing stack top pointer
|
|
|
|
* and stack cell num. */
|
|
|
|
bool is_stack_polymorphic;
|
2019-05-07 02:18:18 +00:00
|
|
|
} BranchBlock;
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
typedef struct WASMLoaderContext {
|
|
|
|
/* frame ref stack */
|
|
|
|
uint8 *frame_ref;
|
|
|
|
uint8 *frame_ref_bottom;
|
|
|
|
uint8 *frame_ref_boundary;
|
|
|
|
uint32 frame_ref_size;
|
|
|
|
uint32 stack_cell_num;
|
|
|
|
uint32 max_stack_cell_num;
|
|
|
|
|
|
|
|
/* frame csp stack */
|
|
|
|
BranchBlock *frame_csp;
|
|
|
|
BranchBlock *frame_csp_bottom;
|
|
|
|
BranchBlock *frame_csp_boundary;
|
|
|
|
uint32 frame_csp_size;
|
|
|
|
uint32 csp_num;
|
|
|
|
uint32 max_csp_num;
|
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
/* frame offset stack */
|
|
|
|
int16 *frame_offset;
|
|
|
|
int16 *frame_offset_bottom;
|
|
|
|
int16 *frame_offset_boundary;
|
|
|
|
uint32 frame_offset_size;
|
|
|
|
int16 dynamic_offset;
|
|
|
|
int16 start_dynamic_offset;
|
|
|
|
int16 max_dynamic_offset;
|
|
|
|
|
2020-04-07 03:04:46 +00:00
|
|
|
/* preserved local offset */
|
|
|
|
int16 preserved_local_offset;
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
/* const buffer */
|
|
|
|
uint8 *const_buf;
|
|
|
|
uint16 num_const;
|
|
|
|
uint16 const_cell_num;
|
2022-04-03 23:55:37 +00:00
|
|
|
uint32 const_buf_size;
|
2020-03-07 14:20:38 +00:00
|
|
|
|
|
|
|
/* processed code */
|
|
|
|
uint8 *p_code_compiled;
|
|
|
|
uint8 *p_code_compiled_end;
|
|
|
|
uint32 code_compiled_size;
|
2022-08-15 03:33:20 +00:00
|
|
|
/* If the last opcode will be dropped, the peak memory usage will be larger
|
|
|
|
* than the final code_compiled_size, we record the peak size to ensure
|
|
|
|
* there will not be invalid memory access during second traverse */
|
|
|
|
uint32 code_compiled_peak_size;
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
|
|
|
} WASMLoaderContext;
|
|
|
|
|
|
|
|
typedef struct Const {
|
|
|
|
WASMValue value;
|
|
|
|
uint16 slot_index;
|
|
|
|
uint8 value_type;
|
|
|
|
} Const;
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
static void *
|
|
|
|
memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
|
|
|
uint8 *mem_new;
|
Enable AoT and wamr-sdk, and change arguments of call wasm API (#157)
* Implement memory profiler, optimize memory usage, modify code indent
* Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default
* Add a new extension library: connection
* Fix bug of reading magic number and version in big endian platform
* Re-org platform APIs: move most platform APIs from iwasm to shared-lib
* Enhance wasm loader to fix some security issues
* Fix issue about illegal load of EXC_RETURN into PC on stm32 board
* Updates that let a restricted version of the interpreter run in SGX
* Enable native/app address validation and conversion for wasm app
* Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused
* Refine binary size and fix several minor issues
Optimize interpreter LOAD/STORE opcodes to decrease the binary size
Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found
Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper
Add macros of global heap size, stack size, heap size for Zephyr main.c
Clear compile warning of wasm_application.c
* Add more strict security checks for libc wrapper API's
* Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files
* Enhance security of libc strcpy/sprintf wrapper function
* Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions
* Remove get_module_inst() and fix issue of call native
* Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate
* Refine interpreter call native process, refine memory boudary check
* Fix issues of invokeNative function of arm/mips/general version
* Add a switch to build simple sample without gui support
* Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code
* Re-org shared lib header files, remove unused info; fix compile issues of vxworks
* Add build target general
* Remove unused files
* Update license header
* test push
* Restore file
* Sync up with internal/feature
* Sync up with internal/feature
* Rename build_wamr_app to build_wasm_app
* Fix small issues of README
* Enhance malformed wasm file checking
Fix issue of print hex int and implement utf8 string check
Fix wasi file read/write right issue
Fix minor issue of build wasm app doc
* Sync up with internal/feature
* Sync up with internal/feature: fix interpreter arm issue, fix read leb issue
* Sync up with internal/feature
* Fix bug of config.h and rename wasi config.h to ssp_config.h
* Sync up with internal/feature
* Import wamr aot
* update document
* update document
* Update document, disable WASI in 32bit
* update document
* remove files
* update document
* Update document
* update document
* update document
* update samples
* Sync up with internal repo
2020-01-21 05:26:14 +00:00
|
|
|
bh_assert(size_new > size_old);
|
2021-10-08 03:44:39 +00:00
|
|
|
if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
|
2019-11-20 13:16:36 +00:00
|
|
|
bh_memcpy_s(mem_new, size_new, mem_old, size_old);
|
2019-05-07 02:18:18 +00:00
|
|
|
memset(mem_new + size_old, 0, size_new - size_old);
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(mem_old);
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
return mem_new;
|
|
|
|
}
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
#define MEM_REALLOC(mem, size_old, size_new) \
|
|
|
|
do { \
|
|
|
|
void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
|
|
|
|
error_buf_size); \
|
|
|
|
if (!mem_new) \
|
|
|
|
goto fail; \
|
|
|
|
mem = mem_new; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define CHECK_CSP_PUSH() \
|
|
|
|
do { \
|
|
|
|
if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
|
|
|
|
MEM_REALLOC( \
|
|
|
|
ctx->frame_csp_bottom, ctx->frame_csp_size, \
|
|
|
|
(uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
|
|
|
|
ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
|
|
|
|
ctx->frame_csp_boundary = \
|
|
|
|
ctx->frame_csp_bottom \
|
|
|
|
+ ctx->frame_csp_size / sizeof(BranchBlock); \
|
|
|
|
ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define CHECK_CSP_POP() \
|
|
|
|
do { \
|
|
|
|
if (ctx->csp_num < 1) { \
|
|
|
|
set_error_buf(error_buf, error_buf_size, \
|
|
|
|
"type mismatch: " \
|
|
|
|
"expect data but block stack was empty"); \
|
|
|
|
goto fail; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2019-05-07 02:18:18 +00:00
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
check_offset_push(WASMLoaderContext *ctx, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
2021-04-01 08:50:46 +00:00
|
|
|
uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
|
2020-03-07 14:20:38 +00:00
|
|
|
if (ctx->frame_offset >= ctx->frame_offset_boundary) {
|
|
|
|
MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
|
|
|
|
ctx->frame_offset_size + 16);
|
|
|
|
ctx->frame_offset_size += 16;
|
2021-10-08 03:44:39 +00:00
|
|
|
ctx->frame_offset_boundary =
|
|
|
|
ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
|
2020-06-02 06:53:06 +00:00
|
|
|
ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
fail:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
static bool
|
|
|
|
check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
|
|
|
|
{
|
|
|
|
if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-08 03:19:09 +00:00
|
|
|
static void
|
|
|
|
free_label_patch_list(BranchBlock *frame_csp)
|
2020-03-07 14:20:38 +00:00
|
|
|
{
|
|
|
|
BranchBlockPatch *label_patch = frame_csp->patch_list;
|
|
|
|
BranchBlockPatch *next;
|
|
|
|
while (label_patch != NULL) {
|
|
|
|
next = label_patch->next;
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(label_patch);
|
2020-03-07 14:20:38 +00:00
|
|
|
label_patch = next;
|
|
|
|
}
|
|
|
|
frame_csp->patch_list = NULL;
|
|
|
|
}
|
|
|
|
|
2020-06-08 03:19:09 +00:00
|
|
|
static void
|
|
|
|
free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
|
2020-03-07 14:20:38 +00:00
|
|
|
{
|
|
|
|
BranchBlock *tmp_csp = frame_csp;
|
|
|
|
|
|
|
|
for (uint32 i = 0; i < csp_num; i++) {
|
|
|
|
free_label_patch_list(tmp_csp);
|
2021-10-08 03:44:39 +00:00
|
|
|
tmp_csp++;
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
#endif /* end of WASM_ENABLE_FAST_INTERP */
|
2020-03-07 14:20:38 +00:00
|
|
|
|
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
check_stack_push(WASMLoaderContext *ctx, char *error_buf, uint32 error_buf_size)
|
2020-03-07 14:20:38 +00:00
|
|
|
{
|
|
|
|
if (ctx->frame_ref >= ctx->frame_ref_boundary) {
|
|
|
|
MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
|
|
|
|
ctx->frame_ref_size + 16);
|
|
|
|
ctx->frame_ref_size += 16;
|
|
|
|
ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
|
|
|
|
ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
fail:
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
static bool
|
2020-06-02 06:53:06 +00:00
|
|
|
check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type,
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
2021-04-15 03:29:20 +00:00
|
|
|
if ((is_32bit_type(type) && stack_cell_num < 1)
|
|
|
|
|| (is_64bit_type(type) && stack_cell_num < 2)
|
2020-11-05 10:15:15 +00:00
|
|
|
#if WASM_ENABLE_SIMD != 0
|
|
|
|
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
|
|
|
|
|| (type == VALUE_TYPE_V128 && stack_cell_num < 4)
|
|
|
|
#endif
|
|
|
|
#endif
|
2021-10-08 03:44:39 +00:00
|
|
|
) {
|
2019-05-07 02:18:18 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2019-11-27 02:52:12 +00:00
|
|
|
"type mismatch: expect data but stack was empty");
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
if ((is_32bit_type(type) && *(frame_ref - 1) != type)
|
|
|
|
|| (is_64bit_type(type)
|
|
|
|
&& (*(frame_ref - 2) != type || *(frame_ref - 1) != type))
|
2020-11-05 10:15:15 +00:00
|
|
|
#if WASM_ENABLE_SIMD != 0
|
|
|
|
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
|
|
|
|
|| (type == VALUE_TYPE_V128
|
2021-10-08 03:44:39 +00:00
|
|
|
&& (*(frame_ref - 4) != REF_V128_1 || *(frame_ref - 3) != REF_V128_2
|
2020-11-05 10:15:15 +00:00
|
|
|
|| *(frame_ref - 2) != REF_V128_3
|
|
|
|
|| *(frame_ref - 1) != REF_V128_4))
|
|
|
|
#endif
|
|
|
|
#endif
|
2021-10-08 03:44:39 +00:00
|
|
|
) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
|
2021-10-08 03:44:39 +00:00
|
|
|
"type mismatch: expect ", type2str(type),
|
2020-08-21 07:11:31 +00:00
|
|
|
" but got other");
|
2020-06-02 06:53:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2020-06-02 06:53:06 +00:00
|
|
|
{
|
2021-10-08 03:44:39 +00:00
|
|
|
int32 block_stack_cell_num =
|
|
|
|
(int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
|
2020-06-02 06:53:06 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
if (block_stack_cell_num > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
|
2020-06-02 06:53:06 +00:00
|
|
|
/* the stack top is a value of any type, return success */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!check_stack_top_values(ctx->frame_ref, block_stack_cell_num, type,
|
|
|
|
error_buf, error_buf_size))
|
2019-05-07 02:18:18 +00:00
|
|
|
return false;
|
2020-06-02 06:53:06 +00:00
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-08 03:19:09 +00:00
|
|
|
static void
|
|
|
|
wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
|
2020-03-07 14:20:38 +00:00
|
|
|
{
|
|
|
|
if (ctx) {
|
|
|
|
if (ctx->frame_ref_bottom)
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(ctx->frame_ref_bottom);
|
2020-03-07 14:20:38 +00:00
|
|
|
if (ctx->frame_csp_bottom) {
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
|
|
|
|
#endif
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(ctx->frame_csp_bottom);
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
if (ctx->frame_offset_bottom)
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(ctx->frame_offset_bottom);
|
2020-03-07 14:20:38 +00:00
|
|
|
if (ctx->const_buf)
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(ctx->const_buf);
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(ctx);
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
static WASMLoaderContext *
|
2022-04-03 23:55:37 +00:00
|
|
|
wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
|
2020-03-07 14:20:38 +00:00
|
|
|
{
|
|
|
|
WASMLoaderContext *loader_ctx =
|
2022-04-03 23:55:37 +00:00
|
|
|
loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
|
2020-03-07 14:20:38 +00:00
|
|
|
if (!loader_ctx)
|
2020-10-13 06:13:30 +00:00
|
|
|
return NULL;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
loader_ctx->frame_ref_size = 32;
|
2022-04-03 23:55:37 +00:00
|
|
|
if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
|
|
|
|
loader_ctx->frame_ref_size, error_buf, error_buf_size)))
|
2020-03-07 14:20:38 +00:00
|
|
|
goto fail;
|
2021-12-10 10:13:17 +00:00
|
|
|
loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
|
2022-04-03 23:55:37 +00:00
|
|
|
if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
|
|
|
|
loader_ctx->frame_csp_size, error_buf, error_buf_size)))
|
2020-03-07 14:20:38 +00:00
|
|
|
goto fail;
|
|
|
|
loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
loader_ctx->frame_offset_size = sizeof(int16) * 32;
|
|
|
|
if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
|
2022-04-03 23:55:37 +00:00
|
|
|
loader_malloc(loader_ctx->frame_offset_size, error_buf,
|
|
|
|
error_buf_size)))
|
2020-03-07 14:20:38 +00:00
|
|
|
goto fail;
|
|
|
|
loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
|
|
|
|
|
|
|
|
loader_ctx->num_const = 0;
|
|
|
|
loader_ctx->const_buf_size = sizeof(Const) * 8;
|
2022-04-03 23:55:37 +00:00
|
|
|
if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
|
|
|
|
error_buf, error_buf_size)))
|
2020-03-07 14:20:38 +00:00
|
|
|
goto fail;
|
|
|
|
|
2022-04-03 23:55:37 +00:00
|
|
|
if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"fast interpreter offset overflow");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
|
2021-10-08 03:44:39 +00:00
|
|
|
loader_ctx->max_dynamic_offset =
|
|
|
|
func->param_cell_num + func->local_cell_num;
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
|
|
|
return loader_ctx;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
wasm_loader_ctx_destroy(loader_ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2020-03-07 14:20:38 +00:00
|
|
|
{
|
|
|
|
if (type == VALUE_TYPE_VOID)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!check_stack_push(ctx, error_buf, error_buf_size))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*ctx->frame_ref++ = type;
|
|
|
|
ctx->stack_cell_num++;
|
2021-04-15 03:29:20 +00:00
|
|
|
if (is_32bit_type(type) || type == VALUE_TYPE_ANY)
|
|
|
|
goto check_stack_and_return;
|
2020-03-07 14:20:38 +00:00
|
|
|
|
|
|
|
if (!check_stack_push(ctx, error_buf, error_buf_size))
|
|
|
|
return false;
|
2021-04-15 03:29:20 +00:00
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
*ctx->frame_ref++ = type;
|
|
|
|
ctx->stack_cell_num++;
|
2020-11-05 10:15:15 +00:00
|
|
|
|
|
|
|
#if WASM_ENABLE_SIMD != 0
|
|
|
|
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
|
|
|
|
if (type == VALUE_TYPE_V128) {
|
|
|
|
if (!check_stack_push(ctx, error_buf, error_buf_size))
|
|
|
|
return false;
|
|
|
|
*ctx->frame_ref++ = type;
|
|
|
|
ctx->stack_cell_num++;
|
|
|
|
if (!check_stack_push(ctx, error_buf, error_buf_size))
|
|
|
|
return false;
|
|
|
|
*ctx->frame_ref++ = type;
|
|
|
|
ctx->stack_cell_num++;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
2021-04-15 03:29:20 +00:00
|
|
|
|
|
|
|
check_stack_and_return:
|
2022-11-22 07:32:48 +00:00
|
|
|
if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
|
2021-04-15 03:29:20 +00:00
|
|
|
ctx->max_stack_cell_num = ctx->stack_cell_num;
|
2022-11-22 07:32:48 +00:00
|
|
|
if (ctx->max_stack_cell_num > UINT16_MAX) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"operand stack depth limit exceeded");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-03-07 14:20:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2020-03-07 14:20:38 +00:00
|
|
|
{
|
2020-06-02 06:53:06 +00:00
|
|
|
BranchBlock *cur_block = ctx->frame_csp - 1;
|
2021-10-08 03:44:39 +00:00
|
|
|
int32 available_stack_cell =
|
|
|
|
(int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
|
2020-06-02 06:53:06 +00:00
|
|
|
|
|
|
|
/* Directly return success if current block is in stack
|
|
|
|
* polymorphic state while stack is empty. */
|
|
|
|
if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
|
|
|
|
return true;
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
if (type == VALUE_TYPE_VOID)
|
|
|
|
return true;
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
|
2020-03-07 14:20:38 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
ctx->frame_ref--;
|
|
|
|
ctx->stack_cell_num--;
|
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
if (is_32bit_type(type) || *ctx->frame_ref == VALUE_TYPE_ANY)
|
2020-03-07 14:20:38 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
ctx->frame_ref--;
|
|
|
|
ctx->stack_cell_num--;
|
2020-11-05 10:15:15 +00:00
|
|
|
|
|
|
|
#if WASM_ENABLE_SIMD != 0
|
|
|
|
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
|
|
|
|
if (type == VALUE_TYPE_V128) {
|
|
|
|
ctx->frame_ref -= 2;
|
|
|
|
ctx->stack_cell_num -= 2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
2020-03-07 14:20:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-09-26 02:17:54 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP == 0
|
2020-05-15 09:44:36 +00:00
|
|
|
static bool
|
|
|
|
wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
|
2021-10-08 03:44:39 +00:00
|
|
|
uint8 type_push, uint8 type_pop, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2020-05-15 09:44:36 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < pop_cnt; i++) {
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
|
|
|
|
error_buf_size))
|
2020-05-15 09:44:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2023-09-26 02:17:54 +00:00
|
|
|
#endif
|
2020-05-15 09:44:36 +00:00
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
static bool
|
2020-07-10 08:29:15 +00:00
|
|
|
wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
|
2021-10-08 03:44:39 +00:00
|
|
|
BlockType block_type, uint8 *start_addr,
|
2020-03-07 14:20:38 +00:00
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
|
|
|
CHECK_CSP_PUSH();
|
|
|
|
memset(ctx->frame_csp, 0, sizeof(BranchBlock));
|
2020-07-10 08:29:15 +00:00
|
|
|
ctx->frame_csp->label_type = label_type;
|
|
|
|
ctx->frame_csp->block_type = block_type;
|
2020-03-07 14:20:38 +00:00
|
|
|
ctx->frame_csp->start_addr = start_addr;
|
|
|
|
ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
|
|
|
|
ctx->frame_csp->patch_list = NULL;
|
|
|
|
#endif
|
|
|
|
ctx->frame_csp++;
|
|
|
|
ctx->csp_num++;
|
2022-11-22 07:32:48 +00:00
|
|
|
if (ctx->csp_num > ctx->max_csp_num) {
|
2020-03-07 14:20:38 +00:00
|
|
|
ctx->max_csp_num = ctx->csp_num;
|
2022-11-22 07:32:48 +00:00
|
|
|
if (ctx->max_csp_num > UINT16_MAX) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"label stack depth limit exceeded");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-03-07 14:20:38 +00:00
|
|
|
return true;
|
|
|
|
fail:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2020-03-07 14:20:38 +00:00
|
|
|
{
|
|
|
|
CHECK_CSP_POP();
|
2020-07-10 08:29:15 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
if ((ctx->frame_csp - 1)->param_frame_offsets)
|
|
|
|
wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
|
|
|
|
#endif
|
2020-03-07 14:20:38 +00:00
|
|
|
ctx->frame_csp--;
|
|
|
|
ctx->csp_num--;
|
2020-07-10 08:29:15 +00:00
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
return true;
|
|
|
|
fail:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2020-03-27 01:28:30 +00:00
|
|
|
|
2021-04-01 08:50:46 +00:00
|
|
|
#if WASM_ENABLE_LABELS_AS_VALUES != 0
|
|
|
|
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
#define emit_label(opcode) \
|
|
|
|
do { \
|
|
|
|
wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
|
|
|
|
LOG_OP("\nemit_op [%02x]\t", opcode); \
|
|
|
|
} while (0)
|
|
|
|
#define skip_label() \
|
|
|
|
do { \
|
|
|
|
wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
|
|
|
|
LOG_OP("\ndelete last op\n"); \
|
|
|
|
} while (0)
|
2021-04-01 08:50:46 +00:00
|
|
|
#else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
|
2023-10-24 02:47:17 +00:00
|
|
|
#if UINTPTR_MAX == UINT64_MAX
|
2021-10-08 03:44:39 +00:00
|
|
|
#define emit_label(opcode) \
|
|
|
|
do { \
|
|
|
|
int32 offset = \
|
|
|
|
(int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
|
2023-10-24 02:47:17 +00:00
|
|
|
/* emit int32 relative offset in 64-bit target */ \
|
|
|
|
wasm_loader_emit_uint32(loader_ctx, offset); \
|
2021-10-08 03:44:39 +00:00
|
|
|
LOG_OP("\nemit_op [%02x]\t", opcode); \
|
|
|
|
} while (0)
|
2023-10-24 02:47:17 +00:00
|
|
|
#else
|
|
|
|
#define emit_label(opcode) \
|
|
|
|
do { \
|
|
|
|
uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
|
|
|
|
/* emit uint32 label address in 32-bit target */ \
|
|
|
|
wasm_loader_emit_uint32(loader_ctx, label_addr); \
|
|
|
|
LOG_OP("\nemit_op [%02x]\t", opcode); \
|
|
|
|
} while (0)
|
|
|
|
#endif
|
2021-10-08 03:44:39 +00:00
|
|
|
#define skip_label() \
|
|
|
|
do { \
|
2023-10-24 02:47:17 +00:00
|
|
|
wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
|
2021-10-08 03:44:39 +00:00
|
|
|
LOG_OP("\ndelete last op\n"); \
|
|
|
|
} while (0)
|
2021-04-01 08:50:46 +00:00
|
|
|
#endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
|
2021-10-08 03:44:39 +00:00
|
|
|
#else /* else of WASM_ENABLE_LABELS_AS_VALUES */
|
|
|
|
#define emit_label(opcode) \
|
|
|
|
do { \
|
|
|
|
wasm_loader_emit_uint8(loader_ctx, opcode); \
|
|
|
|
LOG_OP("\nemit_op [%02x]\t", opcode); \
|
|
|
|
} while (0)
|
|
|
|
#define skip_label() \
|
|
|
|
do { \
|
|
|
|
wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
|
|
|
|
LOG_OP("\ndelete last op\n"); \
|
|
|
|
} while (0)
|
2021-04-01 08:50:46 +00:00
|
|
|
#endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
|
2020-03-27 01:28:30 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
#define emit_empty_label_addr_and_frame_ip(type) \
|
|
|
|
do { \
|
|
|
|
if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
|
|
|
|
loader_ctx->p_code_compiled, error_buf, \
|
|
|
|
error_buf_size)) \
|
|
|
|
goto fail; \
|
|
|
|
/* label address, to be patched */ \
|
|
|
|
wasm_loader_emit_ptr(loader_ctx, NULL); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define emit_br_info(frame_csp) \
|
|
|
|
do { \
|
|
|
|
if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, error_buf, \
|
|
|
|
error_buf_size)) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define LAST_OP_OUTPUT_I32() \
|
|
|
|
(last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
|
|
|
|
|| (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
|
|
|
|
|| (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
|
|
|
|
|| (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
|
|
|
|
|| (last_op >= WASM_OP_I32_WRAP_I64 \
|
|
|
|
&& last_op <= WASM_OP_I32_TRUNC_U_F64) \
|
|
|
|
|| (last_op >= WASM_OP_F32_CONVERT_S_I32 \
|
|
|
|
&& last_op <= WASM_OP_F32_DEMOTE_F64) \
|
|
|
|
|| (last_op == WASM_OP_I32_REINTERPRET_F32) \
|
|
|
|
|| (last_op == WASM_OP_F32_REINTERPRET_I32) \
|
|
|
|
|| (last_op == EXT_OP_COPY_STACK_TOP)
|
|
|
|
|
|
|
|
#define LAST_OP_OUTPUT_I64() \
|
|
|
|
(last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
|
|
|
|
|| (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
|
|
|
|
|| (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
|
|
|
|
|| (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
|
|
|
|
|| (last_op >= WASM_OP_I64_EXTEND_S_I32 \
|
|
|
|
&& last_op <= WASM_OP_I64_TRUNC_U_F64) \
|
|
|
|
|| (last_op >= WASM_OP_F64_CONVERT_S_I32 \
|
|
|
|
&& last_op <= WASM_OP_F64_PROMOTE_F32) \
|
|
|
|
|| (last_op == WASM_OP_I64_REINTERPRET_F64) \
|
|
|
|
|| (last_op == WASM_OP_F64_REINTERPRET_I64) \
|
|
|
|
|| (last_op == EXT_OP_COPY_STACK_TOP_I64)
|
|
|
|
|
|
|
|
#define GET_CONST_OFFSET(type, val) \
|
|
|
|
do { \
|
|
|
|
if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
|
|
|
|
&operand_offset, error_buf, \
|
|
|
|
error_buf_size))) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define GET_CONST_F32_OFFSET(type, fval) \
|
|
|
|
do { \
|
|
|
|
if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
|
|
|
|
&operand_offset, error_buf, \
|
|
|
|
error_buf_size))) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define GET_CONST_F64_OFFSET(type, fval) \
|
|
|
|
do { \
|
|
|
|
if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
|
|
|
|
&operand_offset, error_buf, \
|
|
|
|
error_buf_size))) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define emit_operand(ctx, offset) \
|
|
|
|
do { \
|
|
|
|
wasm_loader_emit_int16(ctx, offset); \
|
|
|
|
LOG_OP("%d\t", offset); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define emit_byte(ctx, byte) \
|
|
|
|
do { \
|
|
|
|
wasm_loader_emit_uint8(ctx, byte); \
|
|
|
|
LOG_OP("%d\t", byte); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define emit_uint32(ctx, value) \
|
|
|
|
do { \
|
|
|
|
wasm_loader_emit_uint32(ctx, value); \
|
|
|
|
LOG_OP("%d\t", value); \
|
|
|
|
} while (0)
|
2020-03-27 01:28:30 +00:00
|
|
|
|
2022-04-03 23:55:37 +00:00
|
|
|
#define emit_uint64(ctx, value) \
|
|
|
|
do { \
|
|
|
|
wasm_loader_emit_const(ctx, &value, false); \
|
|
|
|
LOG_OP("%lld\t", value); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define emit_float32(ctx, value) \
|
|
|
|
do { \
|
|
|
|
wasm_loader_emit_const(ctx, &value, true); \
|
|
|
|
LOG_OP("%f\t", value); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define emit_float64(ctx, value) \
|
|
|
|
do { \
|
|
|
|
wasm_loader_emit_const(ctx, &value, false); \
|
|
|
|
LOG_OP("%f\t", value); \
|
|
|
|
} while (0)
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
static bool
|
|
|
|
wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
|
|
|
|
{
|
2021-12-10 10:13:17 +00:00
|
|
|
if (!(ctx->p_code_compiled =
|
2022-08-15 03:33:20 +00:00
|
|
|
loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
|
2020-03-07 14:20:38 +00:00
|
|
|
return false;
|
2022-08-15 03:33:20 +00:00
|
|
|
ctx->p_code_compiled_end =
|
|
|
|
ctx->p_code_compiled + ctx->code_compiled_peak_size;
|
2020-03-07 14:20:38 +00:00
|
|
|
|
|
|
|
/* clean up frame ref */
|
|
|
|
memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
|
|
|
|
ctx->frame_ref = ctx->frame_ref_bottom;
|
|
|
|
ctx->stack_cell_num = 0;
|
|
|
|
|
|
|
|
/* clean up frame csp */
|
|
|
|
memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
|
|
|
|
ctx->frame_csp = ctx->frame_csp_bottom;
|
|
|
|
ctx->csp_num = 0;
|
|
|
|
ctx->max_csp_num = 0;
|
|
|
|
|
|
|
|
/* clean up frame offset */
|
|
|
|
memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
|
|
|
|
ctx->frame_offset = ctx->frame_offset_bottom;
|
|
|
|
ctx->dynamic_offset = ctx->start_dynamic_offset;
|
|
|
|
|
2020-04-07 03:04:46 +00:00
|
|
|
/* init preserved local offsets */
|
|
|
|
ctx->preserved_local_offset = ctx->max_dynamic_offset;
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
/* const buf is reserved */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-15 03:33:20 +00:00
|
|
|
static void
|
|
|
|
increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
|
|
|
|
{
|
|
|
|
ctx->code_compiled_size += size;
|
|
|
|
if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
|
|
|
|
ctx->code_compiled_peak_size = ctx->code_compiled_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-03 23:55:37 +00:00
|
|
|
static void
|
|
|
|
wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
|
|
|
|
{
|
|
|
|
uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
|
|
|
|
|
|
|
|
if (ctx->p_code_compiled) {
|
|
|
|
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
|
|
|
|
bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
|
|
|
|
#endif
|
|
|
|
bh_memcpy_s(ctx->p_code_compiled,
|
2022-04-20 08:19:12 +00:00
|
|
|
(uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
|
|
|
|
value, size);
|
2022-04-03 23:55:37 +00:00
|
|
|
ctx->p_code_compiled += size;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
|
|
|
|
bh_assert((ctx->code_compiled_size & 1) == 0);
|
|
|
|
#endif
|
2022-08-15 03:33:20 +00:00
|
|
|
increase_compiled_code_space(ctx, size);
|
2022-04-03 23:55:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 07:41:25 +00:00
|
|
|
static void
|
|
|
|
wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
|
|
|
|
{
|
|
|
|
if (ctx->p_code_compiled) {
|
2021-04-01 08:50:46 +00:00
|
|
|
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
|
|
|
|
bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
|
|
|
|
#endif
|
|
|
|
STORE_U32(ctx->p_code_compiled, value);
|
2020-06-28 07:41:25 +00:00
|
|
|
ctx->p_code_compiled += sizeof(uint32);
|
|
|
|
}
|
2021-04-01 08:50:46 +00:00
|
|
|
else {
|
|
|
|
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
|
|
|
|
bh_assert((ctx->code_compiled_size & 1) == 0);
|
|
|
|
#endif
|
2022-08-15 03:33:20 +00:00
|
|
|
increase_compiled_code_space(ctx, sizeof(uint32));
|
2021-04-01 08:50:46 +00:00
|
|
|
}
|
2020-06-28 07:41:25 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
static void
|
|
|
|
wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
|
|
|
|
{
|
|
|
|
if (ctx->p_code_compiled) {
|
2021-04-01 08:50:46 +00:00
|
|
|
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
|
|
|
|
bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
|
|
|
|
#endif
|
|
|
|
STORE_U16(ctx->p_code_compiled, (uint16)value);
|
2020-03-07 14:20:38 +00:00
|
|
|
ctx->p_code_compiled += sizeof(int16);
|
|
|
|
}
|
2021-04-01 08:50:46 +00:00
|
|
|
else {
|
|
|
|
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
|
|
|
|
bh_assert((ctx->code_compiled_size & 1) == 0);
|
|
|
|
#endif
|
2022-08-15 03:33:20 +00:00
|
|
|
increase_compiled_code_space(ctx, sizeof(uint16));
|
2021-04-01 08:50:46 +00:00
|
|
|
}
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
|
|
|
|
{
|
|
|
|
if (ctx->p_code_compiled) {
|
|
|
|
*(ctx->p_code_compiled) = value;
|
|
|
|
ctx->p_code_compiled += sizeof(uint8);
|
2021-04-01 08:50:46 +00:00
|
|
|
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
|
|
|
|
ctx->p_code_compiled++;
|
|
|
|
bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
|
|
|
|
#endif
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
2021-04-01 08:50:46 +00:00
|
|
|
else {
|
2022-08-15 03:33:20 +00:00
|
|
|
increase_compiled_code_space(ctx, sizeof(uint8));
|
2021-04-01 08:50:46 +00:00
|
|
|
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
|
2022-08-15 03:33:20 +00:00
|
|
|
increase_compiled_code_space(ctx, sizeof(uint8));
|
2021-04-01 08:50:46 +00:00
|
|
|
bh_assert((ctx->code_compiled_size & 1) == 0);
|
|
|
|
#endif
|
|
|
|
}
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
|
|
|
|
{
|
|
|
|
if (ctx->p_code_compiled) {
|
2021-04-01 08:50:46 +00:00
|
|
|
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
|
|
|
|
bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
|
|
|
|
#endif
|
|
|
|
STORE_PTR(ctx->p_code_compiled, value);
|
2020-03-07 14:20:38 +00:00
|
|
|
ctx->p_code_compiled += sizeof(void *);
|
|
|
|
}
|
2021-04-01 08:50:46 +00:00
|
|
|
else {
|
|
|
|
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
|
|
|
|
bh_assert((ctx->code_compiled_size & 1) == 0);
|
|
|
|
#endif
|
2022-08-15 03:33:20 +00:00
|
|
|
increase_compiled_code_space(ctx, sizeof(void *));
|
2021-04-01 08:50:46 +00:00
|
|
|
}
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
|
|
|
|
{
|
|
|
|
if (ctx->p_code_compiled) {
|
|
|
|
ctx->p_code_compiled -= size;
|
2021-04-01 08:50:46 +00:00
|
|
|
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
|
|
|
|
if (size == sizeof(uint8)) {
|
|
|
|
ctx->p_code_compiled--;
|
|
|
|
bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
|
|
|
|
}
|
|
|
|
#endif
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
2021-04-01 08:50:46 +00:00
|
|
|
else {
|
2020-03-07 14:20:38 +00:00
|
|
|
ctx->code_compiled_size -= size;
|
2021-04-01 08:50:46 +00:00
|
|
|
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
|
|
|
|
if (size == sizeof(uint8)) {
|
|
|
|
ctx->code_compiled_size--;
|
|
|
|
bh_assert((ctx->code_compiled_size & 1) == 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 01:28:30 +00:00
|
|
|
static bool
|
|
|
|
preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
|
2021-10-08 03:44:39 +00:00
|
|
|
uint32 local_index, uint32 local_type,
|
|
|
|
bool *preserved, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2020-03-27 01:28:30 +00:00
|
|
|
{
|
2020-09-27 10:05:20 +00:00
|
|
|
uint32 i = 0;
|
2020-03-27 01:28:30 +00:00
|
|
|
int16 preserved_offset = (int16)local_index;
|
2020-09-27 10:05:20 +00:00
|
|
|
|
2020-03-27 01:28:30 +00:00
|
|
|
*preserved = false;
|
2020-09-27 10:05:20 +00:00
|
|
|
while (i < loader_ctx->stack_cell_num) {
|
|
|
|
uint8 cur_type = loader_ctx->frame_ref_bottom[i];
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
/* move previous local into dynamic space before a set/tee_local opcode
|
|
|
|
*/
|
2020-03-27 01:28:30 +00:00
|
|
|
if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
|
2020-09-27 10:05:20 +00:00
|
|
|
if (!(*preserved)) {
|
2020-03-27 01:28:30 +00:00
|
|
|
*preserved = true;
|
|
|
|
skip_label();
|
2020-09-27 10:05:20 +00:00
|
|
|
preserved_offset = loader_ctx->preserved_local_offset;
|
|
|
|
if (loader_ctx->p_code_compiled) {
|
|
|
|
bh_assert(preserved_offset != (int16)local_index);
|
|
|
|
}
|
2021-04-15 03:29:20 +00:00
|
|
|
if (is_32bit_type(local_type)) {
|
2020-04-07 03:04:46 +00:00
|
|
|
/* Only increase preserve offset in the second traversal */
|
|
|
|
if (loader_ctx->p_code_compiled)
|
|
|
|
loader_ctx->preserved_local_offset++;
|
2020-03-27 01:28:30 +00:00
|
|
|
emit_label(EXT_OP_COPY_STACK_TOP);
|
|
|
|
}
|
|
|
|
else {
|
2020-04-07 03:04:46 +00:00
|
|
|
if (loader_ctx->p_code_compiled)
|
|
|
|
loader_ctx->preserved_local_offset += 2;
|
2020-03-27 01:28:30 +00:00
|
|
|
emit_label(EXT_OP_COPY_STACK_TOP_I64);
|
|
|
|
}
|
|
|
|
emit_operand(loader_ctx, local_index);
|
|
|
|
emit_operand(loader_ctx, preserved_offset);
|
|
|
|
emit_label(opcode);
|
|
|
|
}
|
|
|
|
loader_ctx->frame_offset_bottom[i] = preserved_offset;
|
|
|
|
}
|
2020-09-27 10:05:20 +00:00
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
if (is_32bit_type(cur_type))
|
2020-09-27 10:05:20 +00:00
|
|
|
i++;
|
|
|
|
else
|
|
|
|
i += 2;
|
2020-03-27 01:28:30 +00:00
|
|
|
}
|
|
|
|
|
2022-12-27 04:59:17 +00:00
|
|
|
(void)error_buf;
|
|
|
|
(void)error_buf_size;
|
2020-03-27 01:28:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-27 10:05:20 +00:00
|
|
|
static bool
|
|
|
|
preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
|
|
|
uint32 i = 0;
|
|
|
|
bool preserve_local;
|
|
|
|
|
|
|
|
/* preserve locals before blocks to ensure that "tee/set_local" inside
|
|
|
|
blocks will not influence the value of these locals */
|
|
|
|
while (i < loader_ctx->stack_cell_num) {
|
|
|
|
int16 cur_offset = loader_ctx->frame_offset_bottom[i];
|
|
|
|
uint8 cur_type = loader_ctx->frame_ref_bottom[i];
|
|
|
|
|
|
|
|
if ((cur_offset < loader_ctx->start_dynamic_offset)
|
|
|
|
&& (cur_offset >= 0)) {
|
|
|
|
if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
|
|
|
|
cur_type, &preserve_local,
|
|
|
|
error_buf, error_buf_size)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
if (is_32bit_type(cur_type)) {
|
2020-09-27 10:05:20 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
|
|
|
|
uint8 *p_code_compiled, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2020-03-07 14:20:38 +00:00
|
|
|
{
|
2021-10-08 03:44:39 +00:00
|
|
|
BranchBlockPatch *patch =
|
|
|
|
loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
|
2020-03-07 14:20:38 +00:00
|
|
|
if (!patch) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
patch->patch_type = patch_type;
|
|
|
|
patch->code_compiled = p_code_compiled;
|
|
|
|
if (!frame_csp->patch_list) {
|
|
|
|
frame_csp->patch_list = patch;
|
|
|
|
patch->next = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
patch->next = frame_csp->patch_list;
|
|
|
|
frame_csp->patch_list = patch;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-10-08 03:44:39 +00:00
|
|
|
apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
|
2020-03-07 14:20:38 +00:00
|
|
|
{
|
|
|
|
BranchBlock *frame_csp = ctx->frame_csp - depth;
|
|
|
|
BranchBlockPatch *node = frame_csp->patch_list;
|
|
|
|
BranchBlockPatch *node_prev = NULL, *node_next;
|
|
|
|
|
|
|
|
if (!ctx->p_code_compiled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (node) {
|
|
|
|
node_next = node->next;
|
|
|
|
if (node->patch_type == patch_type) {
|
2021-04-01 08:50:46 +00:00
|
|
|
STORE_PTR(node->code_compiled, ctx->p_code_compiled);
|
2020-03-07 14:20:38 +00:00
|
|
|
if (node_prev == NULL) {
|
|
|
|
frame_csp->patch_list = node_next;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node_prev->next = node_next;
|
|
|
|
}
|
2020-03-10 11:54:44 +00:00
|
|
|
wasm_runtime_free(node);
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
node_prev = node;
|
|
|
|
}
|
|
|
|
node = node_next;
|
|
|
|
}
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
static bool
|
2020-03-07 14:20:38 +00:00
|
|
|
wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
2020-07-10 08:29:15 +00:00
|
|
|
/* br info layout:
|
|
|
|
* a) arity of target block
|
|
|
|
* b) total cell num of arity values
|
|
|
|
* c) each arity value's cell num
|
|
|
|
* d) each arity value's src frame offset
|
|
|
|
* e) each arity values's dst dynamic offset
|
|
|
|
* f) branch target address
|
|
|
|
*
|
|
|
|
* Note: b-e are omitted when arity is 0 so that
|
|
|
|
* interpreter can recover the br info quickly.
|
|
|
|
*/
|
|
|
|
BlockType *block_type = &frame_csp->block_type;
|
|
|
|
uint8 *types = NULL, cell;
|
|
|
|
uint32 arity = 0;
|
|
|
|
int32 i;
|
|
|
|
int16 *frame_offset = ctx->frame_offset;
|
|
|
|
uint16 dynamic_offset;
|
|
|
|
|
|
|
|
/* Note: loop's arity is different from if and block. loop's arity is
|
|
|
|
* its parameter count while if and block arity is result count.
|
|
|
|
*/
|
|
|
|
if (frame_csp->label_type == LABEL_TYPE_LOOP)
|
|
|
|
arity = block_type_get_param_types(block_type, &types);
|
|
|
|
else
|
|
|
|
arity = block_type_get_result_types(block_type, &types);
|
|
|
|
|
|
|
|
/* Part a */
|
|
|
|
emit_uint32(ctx, arity);
|
|
|
|
|
|
|
|
if (arity) {
|
|
|
|
/* Part b */
|
|
|
|
emit_uint32(ctx, wasm_get_cell_num(types, arity));
|
|
|
|
/* Part c */
|
|
|
|
for (i = (int32)arity - 1; i >= 0; i--) {
|
2021-04-01 08:50:46 +00:00
|
|
|
cell = (uint8)wasm_value_type_cell_num(types[i]);
|
2020-07-10 08:29:15 +00:00
|
|
|
emit_byte(ctx, cell);
|
|
|
|
}
|
|
|
|
/* Part d */
|
|
|
|
for (i = (int32)arity - 1; i >= 0; i--) {
|
2021-04-01 08:50:46 +00:00
|
|
|
cell = (uint8)wasm_value_type_cell_num(types[i]);
|
2020-07-10 08:29:15 +00:00
|
|
|
frame_offset -= cell;
|
2021-10-08 03:44:39 +00:00
|
|
|
emit_operand(ctx, *(int16 *)(frame_offset));
|
2020-07-10 08:29:15 +00:00
|
|
|
}
|
|
|
|
/* Part e */
|
2021-10-08 03:44:39 +00:00
|
|
|
dynamic_offset =
|
|
|
|
frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
|
2020-07-10 08:29:15 +00:00
|
|
|
for (i = (int32)arity - 1; i >= 0; i--) {
|
2021-04-01 08:50:46 +00:00
|
|
|
cell = (uint8)wasm_value_type_cell_num(types[i]);
|
2020-07-10 08:29:15 +00:00
|
|
|
dynamic_offset -= cell;
|
|
|
|
emit_operand(ctx, dynamic_offset);
|
|
|
|
}
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
2020-03-17 09:51:51 +00:00
|
|
|
|
2020-07-10 08:29:15 +00:00
|
|
|
/* Part f */
|
|
|
|
if (frame_csp->label_type == LABEL_TYPE_LOOP) {
|
2020-03-07 14:20:38 +00:00
|
|
|
wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
|
|
|
|
}
|
|
|
|
else {
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
|
2020-03-07 14:20:38 +00:00
|
|
|
error_buf, error_buf_size))
|
|
|
|
return false;
|
|
|
|
/* label address, to be patched */
|
|
|
|
wasm_loader_emit_ptr(ctx, NULL);
|
|
|
|
}
|
2020-07-10 08:29:15 +00:00
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
static bool
|
|
|
|
wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
|
|
|
|
bool disable_emit, int16 operand_offset,
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
|
|
|
if (type == VALUE_TYPE_VOID)
|
|
|
|
return true;
|
|
|
|
|
2020-09-20 00:20:45 +00:00
|
|
|
/* only check memory overflow in first traverse */
|
2020-03-07 14:20:38 +00:00
|
|
|
if (ctx->p_code_compiled == NULL) {
|
|
|
|
if (!check_offset_push(ctx, error_buf, error_buf_size))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disable_emit)
|
|
|
|
*(ctx->frame_offset)++ = operand_offset;
|
|
|
|
else {
|
|
|
|
emit_operand(ctx, ctx->dynamic_offset);
|
|
|
|
*(ctx->frame_offset)++ = ctx->dynamic_offset;
|
|
|
|
ctx->dynamic_offset++;
|
2022-04-07 13:07:32 +00:00
|
|
|
if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
|
2020-03-07 14:20:38 +00:00
|
|
|
ctx->max_dynamic_offset = ctx->dynamic_offset;
|
2022-04-07 13:07:32 +00:00
|
|
|
if (ctx->max_dynamic_offset >= INT16_MAX) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
if (is_32bit_type(type))
|
2020-03-07 14:20:38 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (ctx->p_code_compiled == NULL) {
|
|
|
|
if (!check_offset_push(ctx, error_buf, error_buf_size))
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
ctx->frame_offset++;
|
2020-04-13 02:49:40 +00:00
|
|
|
if (!disable_emit) {
|
|
|
|
ctx->dynamic_offset++;
|
2022-04-07 13:07:32 +00:00
|
|
|
if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
|
2020-04-13 02:49:40 +00:00
|
|
|
ctx->max_dynamic_offset = ctx->dynamic_offset;
|
2022-04-07 13:07:32 +00:00
|
|
|
if (ctx->max_dynamic_offset >= INT16_MAX) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
2020-04-13 02:49:40 +00:00
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
return true;
|
2022-04-07 13:07:32 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"fast interpreter offset overflow");
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
/* This function should be in front of wasm_loader_pop_frame_ref
|
|
|
|
as they both use ctx->stack_cell_num, and ctx->stack_cell_num
|
|
|
|
will be modified by wasm_loader_pop_frame_ref */
|
2020-03-07 14:20:38 +00:00
|
|
|
static bool
|
|
|
|
wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
2020-06-02 06:53:06 +00:00
|
|
|
/* if ctx->frame_csp equals ctx->frame_csp_bottom,
|
|
|
|
then current block is the function block */
|
|
|
|
uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
|
|
|
|
BranchBlock *cur_block = ctx->frame_csp - depth;
|
2021-10-08 03:44:39 +00:00
|
|
|
int32 available_stack_cell =
|
|
|
|
(int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
|
2020-06-02 06:53:06 +00:00
|
|
|
|
|
|
|
/* Directly return success if current block is in stack
|
|
|
|
* polymorphic state while stack is empty. */
|
|
|
|
if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
|
|
|
|
return true;
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
if (type == VALUE_TYPE_VOID)
|
|
|
|
return true;
|
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
if (is_32bit_type(type)) {
|
2020-06-02 06:53:06 +00:00
|
|
|
/* Check the offset stack bottom to ensure the frame offset
|
|
|
|
stack will not go underflow. But we don't thrown error
|
|
|
|
and return true here, because the error msg should be
|
|
|
|
given in wasm_loader_pop_frame_ref */
|
|
|
|
if (!check_offset_pop(ctx, 1))
|
|
|
|
return true;
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
ctx->frame_offset -= 1;
|
2020-04-07 03:04:46 +00:00
|
|
|
if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
|
|
|
|
&& (*(ctx->frame_offset) < ctx->max_dynamic_offset))
|
2020-03-07 14:20:38 +00:00
|
|
|
ctx->dynamic_offset -= 1;
|
|
|
|
}
|
|
|
|
else {
|
2020-06-02 06:53:06 +00:00
|
|
|
if (!check_offset_pop(ctx, 2))
|
|
|
|
return true;
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
ctx->frame_offset -= 2;
|
2020-04-07 03:04:46 +00:00
|
|
|
if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
|
|
|
|
&& (*(ctx->frame_offset) < ctx->max_dynamic_offset))
|
2020-03-07 14:20:38 +00:00
|
|
|
ctx->dynamic_offset -= 2;
|
|
|
|
}
|
|
|
|
emit_operand(ctx, *(ctx->frame_offset));
|
2022-12-27 04:59:17 +00:00
|
|
|
|
|
|
|
(void)error_buf;
|
|
|
|
(void)error_buf_size;
|
2020-03-07 14:20:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
static bool
|
2020-03-07 14:20:38 +00:00
|
|
|
wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
|
|
|
|
bool disable_emit, int16 operand_offset,
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
2020-03-07 14:20:38 +00:00
|
|
|
if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
|
|
|
|
error_buf, error_buf_size)))
|
|
|
|
return false;
|
2020-06-02 06:53:06 +00:00
|
|
|
if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
|
|
|
|
return false;
|
2020-03-07 14:20:38 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
2020-06-02 06:53:06 +00:00
|
|
|
/* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
|
2020-03-07 14:20:38 +00:00
|
|
|
if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
|
|
|
|
return false;
|
2020-06-02 06:53:06 +00:00
|
|
|
if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
|
|
|
|
return false;
|
2020-03-07 14:20:38 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-15 09:44:36 +00:00
|
|
|
static bool
|
|
|
|
wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
|
|
|
|
uint8 type_push, uint8 type_pop,
|
|
|
|
bool disable_emit, int16 operand_offset,
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
2023-09-26 02:17:54 +00:00
|
|
|
uint8 i;
|
|
|
|
|
|
|
|
for (i = 0; i < pop_cnt; i++) {
|
|
|
|
if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
|
|
|
|
error_buf_size))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
|
|
|
|
error_buf_size))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
|
|
|
|
operand_offset, error_buf,
|
|
|
|
error_buf_size))
|
2020-05-15 09:44:36 +00:00
|
|
|
return false;
|
2023-09-26 02:17:54 +00:00
|
|
|
|
|
|
|
if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
|
2020-06-02 06:53:06 +00:00
|
|
|
return false;
|
2020-05-15 09:44:36 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2020-03-07 14:20:38 +00:00
|
|
|
|
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
|
|
|
|
int16 *offset, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2020-03-07 14:20:38 +00:00
|
|
|
{
|
2022-04-03 23:55:37 +00:00
|
|
|
int8 bytes_to_increase;
|
2020-03-07 14:20:38 +00:00
|
|
|
int16 operand_offset = 0;
|
|
|
|
Const *c;
|
2022-04-03 23:55:37 +00:00
|
|
|
|
|
|
|
/* Search existing constant */
|
2020-03-07 14:20:38 +00:00
|
|
|
for (c = (Const *)ctx->const_buf;
|
2021-10-08 03:44:39 +00:00
|
|
|
(uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
|
2021-04-15 03:29:20 +00:00
|
|
|
/* TODO: handle v128 type? */
|
2020-03-07 14:20:38 +00:00
|
|
|
if ((type == c->value_type)
|
2021-10-08 03:44:39 +00:00
|
|
|
&& ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
|
|
|
|
|| (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
|
2021-04-15 03:29:20 +00:00
|
|
|
#if WASM_ENABLE_REF_TYPES != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
|| (type == VALUE_TYPE_FUNCREF
|
|
|
|
&& *(int32 *)value == c->value.i32)
|
|
|
|
|| (type == VALUE_TYPE_EXTERNREF
|
|
|
|
&& *(int32 *)value == c->value.i32)
|
2021-04-15 03:29:20 +00:00
|
|
|
#endif
|
|
|
|
|| (type == VALUE_TYPE_F64
|
|
|
|
&& (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
|
|
|
|
|| (type == VALUE_TYPE_F32
|
2021-10-08 03:44:39 +00:00
|
|
|
&& (0
|
|
|
|
== memcmp(value, &(c->value.f32), sizeof(float32)))))) {
|
2020-03-07 14:20:38 +00:00
|
|
|
operand_offset = c->slot_index;
|
|
|
|
break;
|
|
|
|
}
|
2021-04-15 03:29:20 +00:00
|
|
|
if (is_32bit_type(c->value_type))
|
2020-03-07 14:20:38 +00:00
|
|
|
operand_offset += 1;
|
2021-04-15 03:29:20 +00:00
|
|
|
else
|
|
|
|
operand_offset += 2;
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
2022-04-03 23:55:37 +00:00
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
|
2022-04-03 23:55:37 +00:00
|
|
|
/* New constant, append to the const buffer */
|
|
|
|
if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
|
|
|
|
bytes_to_increase = 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bytes_to_increase = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The max cell num of const buffer is 32768 since the valid index range
|
|
|
|
* is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
|
|
|
|
* full */
|
|
|
|
if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
|
|
|
|
*offset = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
|
2021-10-08 03:44:39 +00:00
|
|
|
MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
|
2020-03-07 14:20:38 +00:00
|
|
|
ctx->const_buf_size + 4 * sizeof(Const));
|
|
|
|
ctx->const_buf_size += 4 * sizeof(Const);
|
|
|
|
c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
|
|
|
|
}
|
|
|
|
c->value_type = type;
|
|
|
|
switch (type) {
|
2021-10-08 03:44:39 +00:00
|
|
|
case VALUE_TYPE_F64:
|
|
|
|
bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
|
|
|
|
sizeof(float64));
|
|
|
|
ctx->const_cell_num += 2;
|
|
|
|
/* The const buf will be reversed, we use the second cell */
|
|
|
|
/* of the i64/f64 const so the finnal offset is corrent */
|
|
|
|
operand_offset++;
|
|
|
|
break;
|
|
|
|
case VALUE_TYPE_I64:
|
|
|
|
c->value.i64 = *(int64 *)value;
|
|
|
|
ctx->const_cell_num += 2;
|
|
|
|
operand_offset++;
|
|
|
|
break;
|
|
|
|
case VALUE_TYPE_F32:
|
|
|
|
bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
|
|
|
|
sizeof(float32));
|
|
|
|
ctx->const_cell_num++;
|
|
|
|
break;
|
|
|
|
case VALUE_TYPE_I32:
|
|
|
|
c->value.i32 = *(int32 *)value;
|
|
|
|
ctx->const_cell_num++;
|
|
|
|
break;
|
2021-04-15 03:29:20 +00:00
|
|
|
#if WASM_ENABLE_REF_TYPES != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
case VALUE_TYPE_EXTERNREF:
|
|
|
|
case VALUE_TYPE_FUNCREF:
|
|
|
|
c->value.i32 = *(int32 *)value;
|
|
|
|
ctx->const_cell_num++;
|
|
|
|
break;
|
2021-04-15 03:29:20 +00:00
|
|
|
#endif
|
2021-10-08 03:44:39 +00:00
|
|
|
default:
|
|
|
|
break;
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
|
|
|
c->slot_index = operand_offset;
|
2021-10-08 03:44:39 +00:00
|
|
|
ctx->num_const++;
|
|
|
|
LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
|
|
|
|
(int64)c->value.i64);
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
2020-03-07 14:20:38 +00:00
|
|
|
/* use negetive index for const */
|
|
|
|
operand_offset = -(operand_offset + 1);
|
|
|
|
*offset = operand_offset;
|
2019-05-07 02:18:18 +00:00
|
|
|
return true;
|
2020-03-07 14:20:38 +00:00
|
|
|
fail:
|
|
|
|
return false;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
/*
|
|
|
|
PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
|
|
|
|
-- Mostly used for the binary / compare operation
|
|
|
|
PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
|
|
|
|
-- Mostly used in block / control instructions
|
|
|
|
|
|
|
|
The POP will always emit the offset on the top of the frame_offset stack
|
|
|
|
PUSH can be used in two ways:
|
|
|
|
1. directly PUSH:
|
|
|
|
PUSH_XXX();
|
|
|
|
will allocate a dynamic space and emit
|
|
|
|
2. silent PUSH:
|
|
|
|
operand_offset = xxx; disable_emit = true;
|
|
|
|
PUSH_XXX();
|
|
|
|
only push the frame_offset stack, no emit
|
|
|
|
*/
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
#define TEMPLATE_PUSH(Type) \
|
|
|
|
do { \
|
|
|
|
if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
|
|
|
|
disable_emit, operand_offset, \
|
|
|
|
error_buf, error_buf_size)) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define TEMPLATE_POP(Type) \
|
|
|
|
do { \
|
|
|
|
if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
|
|
|
|
error_buf, error_buf_size)) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define PUSH_OFFSET_TYPE(type) \
|
|
|
|
do { \
|
|
|
|
if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
|
|
|
|
operand_offset, error_buf, \
|
|
|
|
error_buf_size))) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define POP_OFFSET_TYPE(type) \
|
|
|
|
do { \
|
|
|
|
if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
|
|
|
|
error_buf_size))) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define POP_AND_PUSH(type_pop, type_push) \
|
|
|
|
do { \
|
|
|
|
if (!(wasm_loader_push_pop_frame_ref_offset( \
|
|
|
|
loader_ctx, 1, type_push, type_pop, disable_emit, \
|
|
|
|
operand_offset, error_buf, error_buf_size))) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
2020-05-15 09:44:36 +00:00
|
|
|
|
|
|
|
/* type of POPs should be the same */
|
2021-10-08 03:44:39 +00:00
|
|
|
#define POP2_AND_PUSH(type_pop, type_push) \
|
|
|
|
do { \
|
|
|
|
if (!(wasm_loader_push_pop_frame_ref_offset( \
|
|
|
|
loader_ctx, 2, type_push, type_pop, disable_emit, \
|
|
|
|
operand_offset, error_buf, error_buf_size))) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
2020-05-15 09:44:36 +00:00
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
#else /* WASM_ENABLE_FAST_INTERP */
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
#define TEMPLATE_PUSH(Type) \
|
|
|
|
do { \
|
|
|
|
if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
|
|
|
|
error_buf, error_buf_size))) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define TEMPLATE_POP(Type) \
|
|
|
|
do { \
|
|
|
|
if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
|
|
|
|
error_buf, error_buf_size))) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define POP_AND_PUSH(type_pop, type_push) \
|
|
|
|
do { \
|
|
|
|
if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
|
|
|
|
type_pop, error_buf, \
|
|
|
|
error_buf_size))) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
2020-05-15 09:44:36 +00:00
|
|
|
|
|
|
|
/* type of POPs should be the same */
|
2021-10-08 03:44:39 +00:00
|
|
|
#define POP2_AND_PUSH(type_pop, type_push) \
|
|
|
|
do { \
|
|
|
|
if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
|
|
|
|
type_pop, error_buf, \
|
|
|
|
error_buf_size))) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif /* WASM_ENABLE_FAST_INTERP */
|
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
#define PUSH_I32() TEMPLATE_PUSH(I32)
|
|
|
|
#define PUSH_F32() TEMPLATE_PUSH(F32)
|
|
|
|
#define PUSH_I64() TEMPLATE_PUSH(I64)
|
|
|
|
#define PUSH_F64() TEMPLATE_PUSH(F64)
|
|
|
|
#define PUSH_V128() TEMPLATE_PUSH(V128)
|
|
|
|
#define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
|
|
|
|
#define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
|
|
|
|
|
|
|
|
#define POP_I32() TEMPLATE_POP(I32)
|
|
|
|
#define POP_F32() TEMPLATE_POP(F32)
|
|
|
|
#define POP_I64() TEMPLATE_POP(I64)
|
|
|
|
#define POP_F64() TEMPLATE_POP(F64)
|
|
|
|
#define POP_V128() TEMPLATE_POP(V128)
|
|
|
|
#define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
|
|
|
|
#define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
|
|
|
|
|
2020-05-15 09:44:36 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
|
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
|
|
|
|
bool disable_emit, char *error_buf, uint32 error_buf_size)
|
2020-05-15 09:44:36 +00:00
|
|
|
{
|
|
|
|
int16 operand_offset = 0;
|
2021-10-08 03:44:39 +00:00
|
|
|
BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
|
|
|
|
: loader_ctx->frame_csp;
|
2020-07-10 08:29:15 +00:00
|
|
|
BlockType *block_type = &block->block_type;
|
|
|
|
uint8 *return_types = NULL;
|
|
|
|
uint32 return_count = 0, value_count = 0, total_cel_num = 0;
|
|
|
|
int32 i = 0;
|
2021-10-08 03:44:39 +00:00
|
|
|
int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
|
|
|
|
*frame_offset_org = NULL;
|
2020-07-10 08:29:15 +00:00
|
|
|
|
|
|
|
return_count = block_type_get_result_types(block_type, &return_types);
|
|
|
|
|
|
|
|
/* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
|
|
|
|
* of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
|
|
|
|
if (return_count == 1) {
|
2021-04-01 08:50:46 +00:00
|
|
|
uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
|
2021-12-10 10:13:17 +00:00
|
|
|
if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
|
|
|
|
&& block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
|
2020-05-15 09:44:36 +00:00
|
|
|
/* insert op_copy before else opcode */
|
|
|
|
if (opcode == WASM_OP_ELSE)
|
|
|
|
skip_label();
|
2021-10-08 03:44:39 +00:00
|
|
|
emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
|
|
|
|
: EXT_OP_COPY_STACK_TOP_I64);
|
2020-07-10 08:29:15 +00:00
|
|
|
emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
|
|
|
|
emit_operand(loader_ctx, block->dynamic_offset);
|
2020-05-15 09:44:36 +00:00
|
|
|
|
|
|
|
if (opcode == WASM_OP_ELSE) {
|
2020-07-10 08:29:15 +00:00
|
|
|
*(loader_ctx->frame_offset - cell) = block->dynamic_offset;
|
2020-05-15 09:44:36 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-07-10 08:29:15 +00:00
|
|
|
loader_ctx->frame_offset -= cell;
|
|
|
|
loader_ctx->dynamic_offset = block->dynamic_offset;
|
|
|
|
PUSH_OFFSET_TYPE(return_types[0]);
|
2020-05-15 09:44:36 +00:00
|
|
|
wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
|
|
|
|
}
|
|
|
|
if (opcode == WASM_OP_ELSE)
|
|
|
|
emit_label(opcode);
|
|
|
|
}
|
2020-07-10 08:29:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy stack top values to block's results which are in dynamic space.
|
|
|
|
* The instruction format:
|
|
|
|
* Part a: values count
|
|
|
|
* Part b: all values total cell num
|
|
|
|
* Part c: each value's cell_num, src offset and dst offset
|
|
|
|
* Part d: each value's src offset and dst offset
|
|
|
|
* Part e: each value's dst offset
|
|
|
|
*/
|
|
|
|
frame_offset = frame_offset_org = loader_ctx->frame_offset;
|
|
|
|
dynamic_offset = dynamic_offset_org =
|
2021-10-08 03:44:39 +00:00
|
|
|
block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
|
2020-07-10 08:29:15 +00:00
|
|
|
|
|
|
|
/* First traversal to get the count of values needed to be copied. */
|
|
|
|
for (i = (int32)return_count - 1; i >= 0; i--) {
|
2021-04-01 08:50:46 +00:00
|
|
|
uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
|
2020-07-10 08:29:15 +00:00
|
|
|
|
|
|
|
frame_offset -= cells;
|
|
|
|
dynamic_offset -= cells;
|
|
|
|
if (dynamic_offset != *frame_offset) {
|
|
|
|
value_count++;
|
|
|
|
total_cel_num += cells;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value_count) {
|
|
|
|
uint32 j = 0;
|
|
|
|
uint8 *emit_data = NULL, *cells = NULL;
|
|
|
|
int16 *src_offsets = NULL;
|
|
|
|
uint16 *dst_offsets = NULL;
|
2021-10-08 03:44:39 +00:00
|
|
|
uint64 size =
|
|
|
|
(uint64)value_count
|
|
|
|
* (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
|
2020-07-10 08:29:15 +00:00
|
|
|
|
|
|
|
/* Allocate memory for the emit data */
|
|
|
|
if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
cells = emit_data;
|
|
|
|
src_offsets = (int16 *)(cells + value_count);
|
|
|
|
dst_offsets = (uint16 *)(src_offsets + value_count);
|
|
|
|
|
|
|
|
/* insert op_copy before else opcode */
|
|
|
|
if (opcode == WASM_OP_ELSE)
|
|
|
|
skip_label();
|
|
|
|
emit_label(EXT_OP_COPY_STACK_VALUES);
|
|
|
|
/* Part a) */
|
|
|
|
emit_uint32(loader_ctx, value_count);
|
|
|
|
/* Part b) */
|
|
|
|
emit_uint32(loader_ctx, total_cel_num);
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
/* Second traversal to get each value's cell num, src offset and dst
|
|
|
|
* offset. */
|
2020-07-10 08:29:15 +00:00
|
|
|
frame_offset = frame_offset_org;
|
|
|
|
dynamic_offset = dynamic_offset_org;
|
|
|
|
for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
|
2021-04-01 08:50:46 +00:00
|
|
|
uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
|
2020-07-10 08:29:15 +00:00
|
|
|
frame_offset -= cell;
|
|
|
|
dynamic_offset -= cell;
|
|
|
|
if (dynamic_offset != *frame_offset) {
|
|
|
|
/* cell num */
|
|
|
|
cells[j] = cell;
|
|
|
|
/* src offset */
|
|
|
|
src_offsets[j] = *frame_offset;
|
|
|
|
/* dst offset */
|
|
|
|
dst_offsets[j] = dynamic_offset;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
if (opcode == WASM_OP_ELSE) {
|
|
|
|
*frame_offset = dynamic_offset;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
loader_ctx->frame_offset = frame_offset;
|
|
|
|
loader_ctx->dynamic_offset = dynamic_offset;
|
|
|
|
PUSH_OFFSET_TYPE(return_types[i]);
|
|
|
|
wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
|
|
|
|
loader_ctx->frame_offset = frame_offset_org;
|
|
|
|
loader_ctx->dynamic_offset = dynamic_offset_org;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bh_assert(j == value_count);
|
|
|
|
|
|
|
|
/* Emit the cells, src_offsets and dst_offsets */
|
|
|
|
for (j = 0; j < value_count; j++)
|
|
|
|
emit_byte(loader_ctx, cells[j]);
|
|
|
|
for (j = 0; j < value_count; j++)
|
|
|
|
emit_operand(loader_ctx, src_offsets[j]);
|
|
|
|
for (j = 0; j < value_count; j++)
|
|
|
|
emit_operand(loader_ctx, dst_offsets[j]);
|
|
|
|
|
|
|
|
if (opcode == WASM_OP_ELSE)
|
|
|
|
emit_label(opcode);
|
|
|
|
|
|
|
|
wasm_runtime_free(emit_data);
|
2020-05-15 09:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif /* WASM_ENABLE_FAST_INTERP */
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
#define RESERVE_BLOCK_RET() \
|
|
|
|
do { \
|
|
|
|
if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
|
|
|
|
error_buf_size)) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define PUSH_TYPE(type) \
|
|
|
|
do { \
|
|
|
|
if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
|
|
|
|
error_buf_size))) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define POP_TYPE(type) \
|
|
|
|
do { \
|
|
|
|
if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
|
|
|
|
error_buf_size))) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define PUSH_CSP(label_type, block_type, _start_addr) \
|
|
|
|
do { \
|
|
|
|
if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
|
|
|
|
_start_addr, error_buf, \
|
|
|
|
error_buf_size)) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define POP_CSP() \
|
|
|
|
do { \
|
|
|
|
if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
|
|
|
|
do { \
|
|
|
|
read_leb_uint32(p, p_end, local_idx); \
|
|
|
|
if (local_idx >= param_count + local_count) { \
|
|
|
|
set_error_buf(error_buf, error_buf_size, "unknown local"); \
|
|
|
|
goto fail; \
|
|
|
|
} \
|
|
|
|
local_type = local_idx < param_count \
|
|
|
|
? param_types[local_idx] \
|
|
|
|
: local_types[local_idx - param_count]; \
|
|
|
|
local_offset = local_offsets[local_idx]; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define CHECK_BR(depth) \
|
|
|
|
do { \
|
|
|
|
if (!wasm_loader_check_br(loader_ctx, depth, error_buf, \
|
|
|
|
error_buf_size)) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2019-08-08 08:53:56 +00:00
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
|
2019-08-08 08:53:56 +00:00
|
|
|
{
|
2021-10-08 03:44:39 +00:00
|
|
|
if (module->memory_count == 0 && module->import_memory_count == 0) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "unknown memory");
|
2019-08-08 08:53:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
#define CHECK_MEMORY() \
|
|
|
|
do { \
|
|
|
|
if (!check_memory(module, error_buf, error_buf_size)) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
2020-03-07 14:20:38 +00:00
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2020-06-02 06:53:06 +00:00
|
|
|
{
|
|
|
|
uint8 mem_access_aligns[] = {
|
2020-11-05 10:15:15 +00:00
|
|
|
2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
|
|
|
|
2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
|
2020-06-02 06:53:06 +00:00
|
|
|
};
|
2021-10-08 03:44:39 +00:00
|
|
|
bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
|
2020-06-02 06:53:06 +00:00
|
|
|
if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"alignment must not be larger than natural");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-05 10:15:15 +00:00
|
|
|
#if WASM_ENABLE_SIMD != 0
|
|
|
|
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
|
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2020-11-05 10:15:15 +00:00
|
|
|
{
|
|
|
|
uint8 mem_access_aligns[] = {
|
2021-10-08 03:44:39 +00:00
|
|
|
4, /* load */
|
|
|
|
3, 3, 3, 3, 3, 3, /* load and extend */
|
|
|
|
0, 1, 2, 3, /* load and splat */
|
|
|
|
4, /* store */
|
2020-11-05 10:15:15 +00:00
|
|
|
};
|
|
|
|
|
2021-09-17 11:12:57 +00:00
|
|
|
uint8 mem_access_aligns_load_lane[] = {
|
|
|
|
0, 1, 2, 3, /* load lane */
|
|
|
|
0, 1, 2, 3, /* store lane */
|
2021-10-08 03:44:39 +00:00
|
|
|
2, 3 /* store zero */
|
2021-09-17 11:12:57 +00:00
|
|
|
};
|
2020-11-05 10:15:15 +00:00
|
|
|
|
2021-09-17 11:12:57 +00:00
|
|
|
if (!((opcode <= SIMD_v128_store)
|
|
|
|
|| (SIMD_v128_load8_lane <= opcode
|
|
|
|
&& opcode <= SIMD_v128_load64_zero))) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"the opcode doesn't include memarg");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((opcode <= SIMD_v128_store
|
|
|
|
&& align > mem_access_aligns[opcode - SIMD_v128_load])
|
|
|
|
|| (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
|
|
|
|
&& align > mem_access_aligns_load_lane[opcode
|
|
|
|
- SIMD_v128_load8_lane])) {
|
2020-11-05 10:15:15 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"alignment must not be larger than natural");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2020-11-05 10:15:15 +00:00
|
|
|
{
|
|
|
|
switch (opcode) {
|
|
|
|
case SIMD_i8x16_extract_lane_s:
|
|
|
|
case SIMD_i8x16_extract_lane_u:
|
|
|
|
case SIMD_i8x16_replace_lane:
|
|
|
|
if (lane >= 16) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SIMD_i16x8_extract_lane_s:
|
|
|
|
case SIMD_i16x8_extract_lane_u:
|
|
|
|
case SIMD_i16x8_replace_lane:
|
|
|
|
if (lane >= 8) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SIMD_i32x4_extract_lane:
|
|
|
|
case SIMD_i32x4_replace_lane:
|
|
|
|
case SIMD_f32x4_extract_lane:
|
|
|
|
case SIMD_f32x4_replace_lane:
|
|
|
|
if (lane >= 4) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SIMD_i64x2_extract_lane:
|
|
|
|
case SIMD_i64x2_replace_lane:
|
|
|
|
case SIMD_f64x2_extract_lane:
|
|
|
|
case SIMD_f64x2_replace_lane:
|
|
|
|
if (lane >= 2) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
2021-09-17 11:12:57 +00:00
|
|
|
|
|
|
|
case SIMD_v128_load8_lane:
|
|
|
|
case SIMD_v128_load16_lane:
|
|
|
|
case SIMD_v128_load32_lane:
|
|
|
|
case SIMD_v128_load64_lane:
|
|
|
|
case SIMD_v128_store8_lane:
|
|
|
|
case SIMD_v128_store16_lane:
|
|
|
|
case SIMD_v128_store32_lane:
|
|
|
|
case SIMD_v128_store64_lane:
|
|
|
|
case SIMD_v128_load32_zero:
|
|
|
|
case SIMD_v128_load64_zero:
|
|
|
|
{
|
|
|
|
uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
|
|
|
|
if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-11-05 10:15:15 +00:00
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
fail:
|
|
|
|
set_error_buf(error_buf, error_buf_size, "invalid lane index");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
|
2020-11-05 10:15:15 +00:00
|
|
|
{
|
|
|
|
uint8 i;
|
|
|
|
for (i = 0; i != 16; ++i) {
|
|
|
|
if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
|
|
|
|
set_error_buf(error_buf, error_buf_size, "invalid lane index");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
|
|
|
|
#endif /* end of WASM_ENABLE_SIMD */
|
|
|
|
|
2020-08-03 03:30:26 +00:00
|
|
|
#if WASM_ENABLE_SHARED_MEMORY != 0
|
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2020-08-03 03:30:26 +00:00
|
|
|
{
|
2021-10-08 03:44:39 +00:00
|
|
|
uint8 wait_notify_aligns[] = { 2, 2, 3 };
|
2020-08-03 03:30:26 +00:00
|
|
|
uint8 mem_access_aligns[] = {
|
|
|
|
2, 3, 0, 1, 0, 1, 2,
|
|
|
|
};
|
|
|
|
uint8 expect;
|
|
|
|
|
|
|
|
bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
|
|
|
|
|| (opcode >= WASM_OP_ATOMIC_I32_LOAD
|
|
|
|
&& opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
|
|
|
|
if (opcode <= WASM_OP_ATOMIC_WAIT64) {
|
|
|
|
expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* 7 opcodes in every group */
|
|
|
|
expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
|
|
|
|
}
|
|
|
|
if (align != expect) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"alignment isn't equal to natural");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif /* end of WASM_ENABLE_SHARED_MEMORY */
|
|
|
|
|
2020-07-10 08:29:15 +00:00
|
|
|
static bool
|
|
|
|
wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth,
|
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
|
|
|
BranchBlock *target_block, *cur_block;
|
|
|
|
BlockType *target_block_type;
|
|
|
|
uint8 *types = NULL, *frame_ref;
|
|
|
|
uint32 arity = 0;
|
|
|
|
int32 i, available_stack_cell;
|
|
|
|
uint16 cell_num;
|
|
|
|
|
|
|
|
if (loader_ctx->csp_num < depth + 1) {
|
2021-10-08 03:44:39 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"unknown label, "
|
|
|
|
"unexpected end of section or function");
|
|
|
|
return false;
|
2020-07-10 08:29:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cur_block = loader_ctx->frame_csp - 1;
|
|
|
|
target_block = loader_ctx->frame_csp - (depth + 1);
|
|
|
|
target_block_type = &target_block->block_type;
|
|
|
|
frame_ref = loader_ctx->frame_ref;
|
|
|
|
|
|
|
|
/* Note: loop's arity is different from if and block. loop's arity is
|
|
|
|
* its parameter count while if and block arity is result count.
|
|
|
|
*/
|
|
|
|
if (target_block->label_type == LABEL_TYPE_LOOP)
|
|
|
|
arity = block_type_get_param_types(target_block_type, &types);
|
|
|
|
else
|
|
|
|
arity = block_type_get_result_types(target_block_type, &types);
|
|
|
|
|
|
|
|
/* If the stack is in polymorphic state, just clear the stack
|
|
|
|
* and then re-push the values to make the stack top values
|
|
|
|
* match block type. */
|
|
|
|
if (cur_block->is_stack_polymorphic) {
|
2021-10-08 03:44:39 +00:00
|
|
|
for (i = (int32)arity - 1; i >= 0; i--) {
|
2020-07-10 08:29:15 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
POP_OFFSET_TYPE(types[i]);
|
|
|
|
#endif
|
|
|
|
POP_TYPE(types[i]);
|
|
|
|
}
|
|
|
|
for (i = 0; i < (int32)arity; i++) {
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
bool disable_emit = true;
|
|
|
|
int16 operand_offset = 0;
|
|
|
|
PUSH_OFFSET_TYPE(types[i]);
|
|
|
|
#endif
|
|
|
|
PUSH_TYPE(types[i]);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
available_stack_cell =
|
|
|
|
(int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
|
2020-07-10 08:29:15 +00:00
|
|
|
|
|
|
|
/* Check stack top values match target block type */
|
2021-10-08 03:44:39 +00:00
|
|
|
for (i = (int32)arity - 1; i >= 0; i--) {
|
|
|
|
if (!check_stack_top_values(frame_ref, available_stack_cell, types[i],
|
2020-07-10 08:29:15 +00:00
|
|
|
error_buf, error_buf_size))
|
|
|
|
return false;
|
|
|
|
cell_num = wasm_value_type_cell_num(types[i]);
|
|
|
|
frame_ref -= cell_num;
|
|
|
|
available_stack_cell -= cell_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-25 03:48:24 +00:00
|
|
|
|
2020-05-08 04:38:59 +00:00
|
|
|
static BranchBlock *
|
2021-10-08 03:44:39 +00:00
|
|
|
check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
|
2020-05-08 04:38:59 +00:00
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
|
|
|
uint8 *p = *p_buf, *p_end = buf_end;
|
|
|
|
BranchBlock *frame_csp_tmp;
|
|
|
|
uint32 depth;
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, depth);
|
|
|
|
CHECK_BR(depth);
|
|
|
|
frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
emit_br_info(frame_csp_tmp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
*p_buf = p;
|
|
|
|
return frame_csp_tmp;
|
|
|
|
fail:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2020-07-10 08:29:15 +00:00
|
|
|
check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
|
2020-06-02 06:53:06 +00:00
|
|
|
char *error_buf, uint32 error_buf_size)
|
|
|
|
{
|
2020-07-10 08:29:15 +00:00
|
|
|
BlockType *block_type = &block->block_type;
|
|
|
|
uint8 *return_types = NULL;
|
|
|
|
uint32 return_count = 0;
|
|
|
|
int32 available_stack_cell, return_cell_num, i;
|
|
|
|
uint8 *frame_ref = NULL;
|
2020-06-02 06:53:06 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
available_stack_cell =
|
|
|
|
(int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
|
2020-07-10 08:29:15 +00:00
|
|
|
|
|
|
|
return_count = block_type_get_result_types(block_type, &return_types);
|
2021-10-08 03:44:39 +00:00
|
|
|
return_cell_num =
|
|
|
|
return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
|
2020-07-10 08:29:15 +00:00
|
|
|
|
|
|
|
/* If the stack is in polymorphic state, just clear the stack
|
|
|
|
* and then re-push the values to make the stack top values
|
|
|
|
* match block type. */
|
|
|
|
if (block->is_stack_polymorphic) {
|
2021-10-08 03:44:39 +00:00
|
|
|
for (i = (int32)return_count - 1; i >= 0; i--) {
|
2020-06-02 06:53:06 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2020-07-10 08:29:15 +00:00
|
|
|
POP_OFFSET_TYPE(return_types[i]);
|
2020-06-02 06:53:06 +00:00
|
|
|
#endif
|
2020-07-10 08:29:15 +00:00
|
|
|
POP_TYPE(return_types[i]);
|
|
|
|
}
|
2020-06-02 06:53:06 +00:00
|
|
|
|
2020-07-10 08:29:15 +00:00
|
|
|
/* Check stack is empty */
|
|
|
|
if (loader_ctx->stack_cell_num != block->stack_cell_num) {
|
2021-10-08 03:44:39 +00:00
|
|
|
set_error_buf(
|
|
|
|
error_buf, error_buf_size,
|
|
|
|
"type mismatch: stack size does not match block type");
|
2020-07-10 08:29:15 +00:00
|
|
|
goto fail;
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
2020-07-10 08:29:15 +00:00
|
|
|
|
|
|
|
for (i = 0; i < (int32)return_count; i++) {
|
2020-06-02 06:53:06 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2020-07-10 08:29:15 +00:00
|
|
|
bool disable_emit = true;
|
|
|
|
int16 operand_offset = 0;
|
|
|
|
PUSH_OFFSET_TYPE(return_types[i]);
|
2020-06-02 06:53:06 +00:00
|
|
|
#endif
|
2020-07-10 08:29:15 +00:00
|
|
|
PUSH_TYPE(return_types[i]);
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-10 08:29:15 +00:00
|
|
|
/* Check stack cell num equals return cell num */
|
|
|
|
if (available_stack_cell != return_cell_num) {
|
2020-06-02 06:53:06 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"type mismatch: stack size does not match block type");
|
2020-07-10 08:29:15 +00:00
|
|
|
goto fail;
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 08:29:15 +00:00
|
|
|
/* Check stack values match return types */
|
|
|
|
frame_ref = loader_ctx->frame_ref;
|
2021-10-08 03:44:39 +00:00
|
|
|
for (i = (int32)return_count - 1; i >= 0; i--) {
|
2020-07-10 08:29:15 +00:00
|
|
|
if (!check_stack_top_values(frame_ref, available_stack_cell,
|
2021-10-08 03:44:39 +00:00
|
|
|
return_types[i], error_buf, error_buf_size))
|
2020-07-10 08:29:15 +00:00
|
|
|
return false;
|
|
|
|
frame_ref -= wasm_value_type_cell_num(return_types[i]);
|
|
|
|
available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
/* Copy parameters to dynamic space.
|
|
|
|
* 1) POP original parameter out;
|
|
|
|
* 2) Push and copy original values to dynamic space.
|
|
|
|
* The copy instruction format:
|
|
|
|
* Part a: param count
|
|
|
|
* Part b: all param total cell num
|
|
|
|
* Part c: each param's cell_num, src offset and dst offset
|
|
|
|
* Part d: each param's src offset
|
|
|
|
* Part e: each param's dst offset
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
|
2021-10-08 03:44:39 +00:00
|
|
|
char *error_buf, uint32 error_buf_size)
|
2020-07-10 08:29:15 +00:00
|
|
|
{
|
2023-07-18 08:06:21 +00:00
|
|
|
bool ret = false;
|
2020-07-10 08:29:15 +00:00
|
|
|
int16 *frame_offset = NULL;
|
|
|
|
uint8 *cells = NULL, cell;
|
|
|
|
int16 *src_offsets = NULL;
|
|
|
|
uint8 *emit_data = NULL;
|
|
|
|
uint32 i;
|
|
|
|
BranchBlock *block = loader_ctx->frame_csp - 1;
|
|
|
|
BlockType *block_type = &block->block_type;
|
|
|
|
WASMType *wasm_type = block_type->u.type;
|
|
|
|
uint32 param_count = block_type->u.type->param_count;
|
|
|
|
int16 condition_offset = 0;
|
|
|
|
bool disable_emit = false;
|
|
|
|
int16 operand_offset = 0;
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
|
2020-07-10 08:29:15 +00:00
|
|
|
|
|
|
|
/* For if block, we also need copy the condition operand offset. */
|
|
|
|
if (is_if_block)
|
|
|
|
size += sizeof(*cells) + sizeof(*src_offsets);
|
|
|
|
|
|
|
|
/* Allocate memory for the emit data */
|
|
|
|
if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
|
2020-06-02 06:53:06 +00:00
|
|
|
return false;
|
|
|
|
|
2020-07-10 08:29:15 +00:00
|
|
|
cells = emit_data;
|
|
|
|
src_offsets = (int16 *)(cells + param_count);
|
|
|
|
|
|
|
|
if (is_if_block)
|
|
|
|
condition_offset = *loader_ctx->frame_offset;
|
|
|
|
|
|
|
|
/* POP original parameter out */
|
|
|
|
for (i = 0; i < param_count; i++) {
|
|
|
|
POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
|
|
|
|
wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
|
|
|
|
}
|
|
|
|
frame_offset = loader_ctx->frame_offset;
|
|
|
|
|
|
|
|
/* Get each param's cell num and src offset */
|
|
|
|
for (i = 0; i < param_count; i++) {
|
2021-04-01 08:50:46 +00:00
|
|
|
cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
|
2020-07-10 08:29:15 +00:00
|
|
|
cells[i] = cell;
|
|
|
|
src_offsets[i] = *frame_offset;
|
|
|
|
frame_offset += cell;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* emit copy instruction */
|
|
|
|
emit_label(EXT_OP_COPY_STACK_VALUES);
|
|
|
|
/* Part a) */
|
|
|
|
emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
|
|
|
|
/* Part b) */
|
2021-10-08 03:44:39 +00:00
|
|
|
emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
|
|
|
|
: wasm_type->param_cell_num);
|
2020-07-10 08:29:15 +00:00
|
|
|
/* Part c) */
|
|
|
|
for (i = 0; i < param_count; i++)
|
|
|
|
emit_byte(loader_ctx, cells[i]);
|
|
|
|
if (is_if_block)
|
|
|
|
emit_byte(loader_ctx, 1);
|
|
|
|
|
|
|
|
/* Part d) */
|
|
|
|
for (i = 0; i < param_count; i++)
|
|
|
|
emit_operand(loader_ctx, src_offsets[i]);
|
|
|
|
if (is_if_block)
|
|
|
|
emit_operand(loader_ctx, condition_offset);
|
|
|
|
|
|
|
|
/* Part e) */
|
|
|
|
/* Push to dynamic space. The push will emit the dst offset. */
|
|
|
|
for (i = 0; i < param_count; i++)
|
|
|
|
PUSH_OFFSET_TYPE(wasm_type->types[i]);
|
|
|
|
if (is_if_block)
|
|
|
|
PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
|
|
|
|
|
2023-07-18 08:06:21 +00:00
|
|
|
ret = true;
|
|
|
|
|
|
|
|
fail:
|
2020-07-10 08:29:15 +00:00
|
|
|
/* Free the emit data */
|
|
|
|
wasm_runtime_free(emit_data);
|
|
|
|
|
2023-07-18 08:06:21 +00:00
|
|
|
return ret;
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
2020-07-10 08:29:15 +00:00
|
|
|
#endif
|
2020-06-02 06:53:06 +00:00
|
|
|
|
|
|
|
/* reset the stack to the state of before entering the last block */
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
#define RESET_STACK() \
|
|
|
|
do { \
|
|
|
|
loader_ctx->stack_cell_num = \
|
|
|
|
(loader_ctx->frame_csp - 1)->stack_cell_num; \
|
|
|
|
loader_ctx->frame_ref = \
|
|
|
|
loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
|
|
|
|
loader_ctx->frame_offset = \
|
|
|
|
loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
|
|
|
|
} while (0)
|
2020-06-02 06:53:06 +00:00
|
|
|
#else
|
2021-10-08 03:44:39 +00:00
|
|
|
#define RESET_STACK() \
|
|
|
|
do { \
|
|
|
|
loader_ctx->stack_cell_num = \
|
|
|
|
(loader_ctx->frame_csp - 1)->stack_cell_num; \
|
|
|
|
loader_ctx->frame_ref = \
|
|
|
|
loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
|
|
|
|
} while (0)
|
2020-06-02 06:53:06 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* set current block's stack polymorphic state */
|
2021-12-10 10:13:17 +00:00
|
|
|
#define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
|
|
|
|
do { \
|
|
|
|
BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
|
|
|
|
_cur_block->is_stack_polymorphic = flag; \
|
2021-10-08 03:44:39 +00:00
|
|
|
} while (0)
|
2020-06-02 06:53:06 +00:00
|
|
|
|
2020-07-10 08:29:15 +00:00
|
|
|
#define BLOCK_HAS_PARAM(block_type) \
|
|
|
|
(!block_type.is_value_type && block_type.u.type->param_count > 0)
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
#define PRESERVE_LOCAL_FOR_BLOCK() \
|
|
|
|
do { \
|
|
|
|
if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
|
|
|
|
error_buf_size))) { \
|
|
|
|
goto fail; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2020-09-27 10:05:20 +00:00
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
#if WASM_ENABLE_REF_TYPES != 0
|
2019-05-07 02:18:18 +00:00
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
get_table_elem_type(const WASMModule *module, uint32 table_idx,
|
|
|
|
uint8 *p_elem_type, char *error_buf, uint32 error_buf_size)
|
2021-04-15 03:29:20 +00:00
|
|
|
{
|
|
|
|
if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_elem_type) {
|
|
|
|
if (table_idx < module->import_table_count)
|
|
|
|
*p_elem_type = module->import_tables[table_idx].u.table.elem_type;
|
|
|
|
else
|
2021-10-08 03:44:39 +00:00
|
|
|
*p_elem_type =
|
|
|
|
module->tables[module->import_table_count + table_idx]
|
|
|
|
.elem_type;
|
2021-04-15 03:29:20 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
|
|
|
|
uint8 *p_elem_type, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2021-04-15 03:29:20 +00:00
|
|
|
{
|
|
|
|
if (table_seg_idx >= module->table_seg_count) {
|
2021-11-18 09:42:49 +00:00
|
|
|
set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
|
|
|
|
table_seg_idx);
|
2021-04-15 03:29:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_elem_type) {
|
|
|
|
*p_elem_type = module->table_segments[table_seg_idx].elem_type;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-06-10 13:51:13 +00:00
|
|
|
#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
|
|
|
|
const uint8 *
|
|
|
|
wasm_loader_get_custom_section(WASMModule *module, const char *name,
|
|
|
|
uint32 *len)
|
|
|
|
{
|
|
|
|
WASMCustomSection *section = module->custom_section_list;
|
|
|
|
|
|
|
|
while (section) {
|
|
|
|
if ((section->name_len == strlen(name))
|
|
|
|
&& (memcmp(section->name_addr, name, section->name_len) == 0)) {
|
|
|
|
if (len) {
|
|
|
|
*len = section->content_len;
|
|
|
|
}
|
|
|
|
return section->content_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
section = section->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2022-06-15 10:14:39 +00:00
|
|
|
#endif
|
2022-06-10 13:51:13 +00:00
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
static bool
|
2021-10-08 03:44:39 +00:00
|
|
|
wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
|
|
|
|
uint32 cur_func_idx, char *error_buf,
|
|
|
|
uint32 error_buf_size)
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
2020-02-18 07:15:24 +00:00
|
|
|
uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
|
2019-05-07 02:18:18 +00:00
|
|
|
uint32 param_count, local_count, global_count;
|
2020-07-10 08:29:15 +00:00
|
|
|
uint8 *param_types, *local_types, local_type, global_type;
|
2021-12-10 10:13:17 +00:00
|
|
|
BlockType func_block_type;
|
2020-02-18 07:15:24 +00:00
|
|
|
uint16 *local_offsets, local_offset;
|
2021-04-15 03:29:20 +00:00
|
|
|
uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
|
2021-12-10 10:13:17 +00:00
|
|
|
uint32 table_seg_idx, data_seg_idx, count, align, mem_offset, i;
|
2021-04-15 03:29:20 +00:00
|
|
|
int32 i32_const = 0;
|
2021-12-10 10:13:17 +00:00
|
|
|
int64 i64_const;
|
2021-04-15 03:29:20 +00:00
|
|
|
uint8 opcode;
|
2020-06-02 06:53:06 +00:00
|
|
|
bool return_value = false;
|
2020-03-07 14:20:38 +00:00
|
|
|
WASMLoaderContext *loader_ctx;
|
2020-05-08 04:38:59 +00:00
|
|
|
BranchBlock *frame_csp_tmp;
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2021-01-28 08:16:02 +00:00
|
|
|
uint8 *func_const_end, *func_const = NULL;
|
2021-05-19 06:57:31 +00:00
|
|
|
int16 operand_offset = 0;
|
2020-03-07 14:20:38 +00:00
|
|
|
uint8 last_op = 0;
|
2020-03-27 01:28:30 +00:00
|
|
|
bool disable_emit, preserve_local = false;
|
2021-12-10 10:13:17 +00:00
|
|
|
float32 f32_const;
|
|
|
|
float64 f64_const;
|
2020-03-07 14:20:38 +00:00
|
|
|
|
|
|
|
LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
|
2021-10-08 03:44:39 +00:00
|
|
|
func->param_cell_num, func->local_cell_num, func->ret_cell_num);
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
global_count = module->import_global_count + module->global_count;
|
|
|
|
|
|
|
|
param_count = func->func_type->param_count;
|
|
|
|
param_types = func->func_type->types;
|
2020-07-10 08:29:15 +00:00
|
|
|
|
2021-12-10 10:13:17 +00:00
|
|
|
func_block_type.is_value_type = false;
|
|
|
|
func_block_type.u.type = func->func_type;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
local_count = func->local_count;
|
|
|
|
local_types = func->local_types;
|
2020-02-18 07:15:24 +00:00
|
|
|
local_offsets = func->local_offsets;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2022-04-03 23:55:37 +00:00
|
|
|
if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
|
2019-05-07 02:18:18 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2022-06-16 01:51:01 +00:00
|
|
|
/* For the first traverse, the initial value of preserved_local_offset has
|
|
|
|
* not been determined, we use the INT16_MAX to represent that a slot has
|
|
|
|
* been copied to preserve space. For second traverse, this field will be
|
|
|
|
* set to the appropriate value in wasm_loader_ctx_reinit.
|
|
|
|
* This is for Issue #1230,
|
|
|
|
* https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
|
|
|
|
* drop opcodes need to know which slots are preserved, so those slots will
|
|
|
|
* not be treated as dynamically allocated slots */
|
|
|
|
loader_ctx->preserved_local_offset = INT16_MAX;
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
re_scan:
|
|
|
|
if (loader_ctx->code_compiled_size > 0) {
|
|
|
|
if (!wasm_loader_ctx_reinit(loader_ctx)) {
|
2021-10-08 03:44:39 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "allocate memory failed");
|
2020-03-07 14:20:38 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
p = func->code;
|
|
|
|
func->code_compiled = loader_ctx->p_code_compiled;
|
2021-04-01 08:50:46 +00:00
|
|
|
func->code_compiled_size = loader_ctx->code_compiled_size;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2021-12-10 10:13:17 +00:00
|
|
|
PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
while (p < p_end) {
|
|
|
|
opcode = *p++;
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
p_org = p;
|
|
|
|
disable_emit = false;
|
|
|
|
emit_label(opcode);
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
switch (opcode) {
|
|
|
|
case WASM_OP_UNREACHABLE:
|
2020-06-02 06:53:06 +00:00
|
|
|
RESET_STACK();
|
|
|
|
SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
|
|
|
|
break;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
case WASM_OP_NOP:
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
skip_label();
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
2020-07-10 08:29:15 +00:00
|
|
|
case WASM_OP_IF:
|
2020-09-27 10:05:20 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
PRESERVE_LOCAL_FOR_BLOCK();
|
|
|
|
#endif
|
2020-07-10 08:29:15 +00:00
|
|
|
POP_I32();
|
|
|
|
goto handle_op_block_and_loop;
|
2019-05-07 02:18:18 +00:00
|
|
|
case WASM_OP_BLOCK:
|
|
|
|
case WASM_OP_LOOP:
|
2020-09-27 10:05:20 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
PRESERVE_LOCAL_FOR_BLOCK();
|
|
|
|
#endif
|
2021-10-08 03:44:39 +00:00
|
|
|
handle_op_block_and_loop:
|
2020-07-10 08:29:15 +00:00
|
|
|
{
|
|
|
|
uint8 value_type;
|
|
|
|
BlockType block_type;
|
|
|
|
|
2022-03-24 06:14:42 +00:00
|
|
|
p_org = p - 1;
|
2023-11-09 00:42:05 +00:00
|
|
|
CHECK_BUF(p, p_end, 1);
|
2020-07-10 08:29:15 +00:00
|
|
|
value_type = read_uint8(p);
|
2021-04-15 03:29:20 +00:00
|
|
|
if (is_byte_a_type(value_type)) {
|
2020-07-10 08:29:15 +00:00
|
|
|
/* If the first byte is one of these special values:
|
|
|
|
* 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
|
|
|
|
* the single return value. */
|
|
|
|
block_type.is_value_type = true;
|
|
|
|
block_type.u.value_type = value_type;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
uint32 type_index;
|
|
|
|
/* Resolve the leb128 encoded type index as block type */
|
|
|
|
p--;
|
|
|
|
read_leb_uint32(p, p_end, type_index);
|
|
|
|
if (type_index >= module->type_count) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"unknown type");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
block_type.is_value_type = false;
|
|
|
|
block_type.u.type = module->types[type_index];
|
2022-10-18 02:59:28 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP == 0
|
2020-07-10 08:29:15 +00:00
|
|
|
/* If block use type index as block type, change the opcode
|
2021-10-08 03:44:39 +00:00
|
|
|
* to new extended opcode so that interpreter can resolve
|
|
|
|
* the block quickly.
|
2020-07-10 08:29:15 +00:00
|
|
|
*/
|
2021-09-29 05:36:46 +00:00
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0
|
2022-04-23 11:15:55 +00:00
|
|
|
if (!record_fast_op(module, p_org, *p_org, error_buf,
|
|
|
|
error_buf_size)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2021-09-29 05:36:46 +00:00
|
|
|
#endif
|
2022-03-24 06:14:42 +00:00
|
|
|
*p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2020-07-10 08:29:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Pop block parameters from stack */
|
|
|
|
if (BLOCK_HAS_PARAM(block_type)) {
|
|
|
|
WASMType *wasm_type = block_type.u.type;
|
|
|
|
for (i = 0; i < block_type.u.type->param_count; i++)
|
2021-10-08 03:44:39 +00:00
|
|
|
POP_TYPE(
|
|
|
|
wasm_type->types[wasm_type->param_count - i - 1]);
|
2020-07-10 08:29:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
|
|
|
|
block_type, p);
|
2020-07-10 08:29:15 +00:00
|
|
|
|
|
|
|
/* Pass parameters to block */
|
|
|
|
if (BLOCK_HAS_PARAM(block_type)) {
|
|
|
|
for (i = 0; i < block_type.u.type->param_count; i++)
|
|
|
|
PUSH_TYPE(block_type.u.type->types[i]);
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2020-07-10 08:29:15 +00:00
|
|
|
if (opcode == WASM_OP_BLOCK) {
|
|
|
|
skip_label();
|
2021-04-15 03:29:20 +00:00
|
|
|
}
|
|
|
|
else if (opcode == WASM_OP_LOOP) {
|
2020-07-10 08:29:15 +00:00
|
|
|
skip_label();
|
|
|
|
if (BLOCK_HAS_PARAM(block_type)) {
|
|
|
|
/* Make sure params are in dynamic space */
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!copy_params_to_dynamic_space(
|
|
|
|
loader_ctx, false, error_buf, error_buf_size))
|
2020-07-10 08:29:15 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
(loader_ctx->frame_csp - 1)->code_compiled =
|
2021-10-08 03:44:39 +00:00
|
|
|
loader_ctx->p_code_compiled;
|
2021-04-15 03:29:20 +00:00
|
|
|
}
|
|
|
|
else if (opcode == WASM_OP_IF) {
|
2020-07-10 08:29:15 +00:00
|
|
|
/* If block has parameters, we should make sure they are in
|
|
|
|
* dynamic space. Otherwise, when else branch is missing,
|
|
|
|
* the later opcode may consume incorrect operand offset.
|
|
|
|
* Spec case:
|
|
|
|
* (func (export "params-id") (param i32) (result i32)
|
|
|
|
* (i32.const 1)
|
|
|
|
* (i32.const 2)
|
2021-10-08 03:44:39 +00:00
|
|
|
* (if (param i32 i32) (result i32 i32) (local.get 0)
|
|
|
|
* (then)) (i32.add)
|
2020-07-10 08:29:15 +00:00
|
|
|
* )
|
|
|
|
*
|
|
|
|
* So we should emit a copy instruction before the if.
|
|
|
|
*
|
|
|
|
* And we also need to save the parameter offsets and
|
|
|
|
* recover them before entering else branch.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
if (BLOCK_HAS_PARAM(block_type)) {
|
|
|
|
BranchBlock *block = loader_ctx->frame_csp - 1;
|
|
|
|
uint64 size;
|
|
|
|
|
|
|
|
/* skip the if condition operand offset */
|
|
|
|
wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
|
|
|
|
/* skip the if label */
|
|
|
|
skip_label();
|
|
|
|
/* Emit a copy instruction */
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!copy_params_to_dynamic_space(
|
|
|
|
loader_ctx, true, error_buf, error_buf_size))
|
2020-07-10 08:29:15 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* Emit the if instruction */
|
|
|
|
emit_label(opcode);
|
|
|
|
/* Emit the new condition operand offset */
|
|
|
|
POP_OFFSET_TYPE(VALUE_TYPE_I32);
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
/* Save top param_count values of frame_offset stack, so
|
|
|
|
* that we can recover it before executing else branch
|
|
|
|
*/
|
|
|
|
size = sizeof(int16)
|
|
|
|
* (uint64)block_type.u.type->param_cell_num;
|
|
|
|
if (!(block->param_frame_offsets = loader_malloc(
|
|
|
|
size, error_buf, error_buf_size)))
|
2020-07-10 08:29:15 +00:00
|
|
|
goto fail;
|
2021-10-08 03:44:39 +00:00
|
|
|
bh_memcpy_s(block->param_frame_offsets, (uint32)size,
|
|
|
|
loader_ctx->frame_offset
|
|
|
|
- size / sizeof(int16),
|
2021-04-01 08:50:46 +00:00
|
|
|
(uint32)size);
|
2020-07-10 08:29:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
|
|
|
|
emit_empty_label_addr_and_frame_ip(PATCH_END);
|
|
|
|
}
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
2020-07-10 08:29:15 +00:00
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
case WASM_OP_ELSE:
|
2020-07-10 08:29:15 +00:00
|
|
|
{
|
|
|
|
BlockType block_type = (loader_ctx->frame_csp - 1)->block_type;
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
if (loader_ctx->csp_num < 2
|
2021-10-08 03:44:39 +00:00
|
|
|
|| (loader_ctx->frame_csp - 1)->label_type
|
|
|
|
!= LABEL_TYPE_IF) {
|
|
|
|
set_error_buf(
|
|
|
|
error_buf, error_buf_size,
|
|
|
|
"opcode else found without matched opcode if");
|
2019-05-07 02:18:18 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
/* check whether if branch's stack matches its result type */
|
|
|
|
if (!check_block_stack(loader_ctx, loader_ctx->frame_csp - 1,
|
|
|
|
error_buf, error_buf_size))
|
|
|
|
goto fail;
|
2020-05-08 04:38:59 +00:00
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
(loader_ctx->frame_csp - 1)->else_addr = p - 1;
|
2020-06-02 06:53:06 +00:00
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
/* if the result of if branch is in local or const area, add a
|
|
|
|
* copy op */
|
2020-05-15 09:44:36 +00:00
|
|
|
RESERVE_BLOCK_RET();
|
2020-06-02 06:53:06 +00:00
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
emit_empty_label_addr_and_frame_ip(PATCH_END);
|
2020-03-17 09:51:51 +00:00
|
|
|
apply_label_patch(loader_ctx, 1, PATCH_ELSE);
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2020-06-02 06:53:06 +00:00
|
|
|
RESET_STACK();
|
|
|
|
SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
|
2020-07-10 08:29:15 +00:00
|
|
|
|
|
|
|
/* Pass parameters to if-false branch */
|
|
|
|
if (BLOCK_HAS_PARAM(block_type)) {
|
|
|
|
for (i = 0; i < block_type.u.type->param_count; i++)
|
|
|
|
PUSH_TYPE(block_type.u.type->types[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
/* Recover top param_count values of frame_offset stack */
|
|
|
|
if (BLOCK_HAS_PARAM((block_type))) {
|
|
|
|
uint32 size;
|
|
|
|
BranchBlock *block = loader_ctx->frame_csp - 1;
|
2021-10-08 03:44:39 +00:00
|
|
|
size = sizeof(int16) * block_type.u.type->param_cell_num;
|
2020-07-10 08:29:15 +00:00
|
|
|
bh_memcpy_s(loader_ctx->frame_offset, size,
|
|
|
|
block->param_frame_offsets, size);
|
2021-10-08 03:44:39 +00:00
|
|
|
loader_ctx->frame_offset += (size / sizeof(int16));
|
2020-07-10 08:29:15 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
2020-07-10 08:29:15 +00:00
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
case WASM_OP_END:
|
|
|
|
{
|
2020-07-10 08:29:15 +00:00
|
|
|
BranchBlock *cur_block = loader_ctx->frame_csp - 1;
|
2020-06-02 06:53:06 +00:00
|
|
|
|
|
|
|
/* check whether block stack matches its result type */
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!check_block_stack(loader_ctx, cur_block, error_buf,
|
|
|
|
error_buf_size))
|
2020-06-02 06:53:06 +00:00
|
|
|
goto fail;
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
/* if no else branch, and return types do not match param types,
|
|
|
|
* fail */
|
2020-07-10 08:29:15 +00:00
|
|
|
if (cur_block->label_type == LABEL_TYPE_IF
|
|
|
|
&& !cur_block->else_addr) {
|
2021-12-10 10:13:17 +00:00
|
|
|
uint32 block_param_count = 0, block_ret_count = 0;
|
|
|
|
uint8 *block_param_types = NULL, *block_ret_types = NULL;
|
|
|
|
BlockType *cur_block_type = &cur_block->block_type;
|
|
|
|
if (cur_block_type->is_value_type) {
|
|
|
|
if (cur_block_type->u.value_type != VALUE_TYPE_VOID) {
|
|
|
|
block_ret_count = 1;
|
|
|
|
block_ret_types = &cur_block_type->u.value_type;
|
2020-07-10 08:29:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2021-12-10 10:13:17 +00:00
|
|
|
block_param_count = cur_block_type->u.type->param_count;
|
|
|
|
block_ret_count = cur_block_type->u.type->result_count;
|
|
|
|
block_param_types = cur_block_type->u.type->types;
|
|
|
|
block_ret_types =
|
|
|
|
cur_block_type->u.type->types + block_param_count;
|
2020-07-10 08:29:15 +00:00
|
|
|
}
|
2021-12-10 10:13:17 +00:00
|
|
|
if (block_param_count != block_ret_count
|
|
|
|
|| (block_param_count
|
|
|
|
&& memcmp(block_param_types, block_ret_types,
|
|
|
|
block_param_count))) {
|
2020-07-10 08:29:15 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"type mismatch: else branch missing");
|
|
|
|
goto fail;
|
|
|
|
}
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
POP_CSP();
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
skip_label();
|
2020-05-15 09:44:36 +00:00
|
|
|
/* copy the result to the block return address */
|
|
|
|
RESERVE_BLOCK_RET();
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-03-17 09:51:51 +00:00
|
|
|
apply_label_patch(loader_ctx, 0, PATCH_END);
|
2020-03-07 14:20:38 +00:00
|
|
|
free_label_patch_list(loader_ctx->frame_csp);
|
2020-07-10 08:29:15 +00:00
|
|
|
if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
|
|
|
|
int32 idx;
|
|
|
|
uint8 ret_type;
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
emit_label(WASM_OP_RETURN);
|
2020-07-10 08:29:15 +00:00
|
|
|
for (idx = (int32)func->func_type->result_count - 1;
|
|
|
|
idx >= 0; idx--) {
|
|
|
|
ret_type = *(func->func_type->types
|
|
|
|
+ func->func_type->param_count + idx);
|
|
|
|
POP_OFFSET_TYPE(ret_type);
|
|
|
|
}
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (loader_ctx->csp_num > 0) {
|
|
|
|
loader_ctx->frame_csp->end_addr = p - 1;
|
2019-12-13 07:30:30 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-08-08 05:22:23 +00:00
|
|
|
/* end of function block, function will return */
|
|
|
|
if (p < p_end) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"section size mismatch");
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
2020-06-02 06:53:06 +00:00
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case WASM_OP_BR:
|
|
|
|
{
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!(frame_csp_tmp = check_branch_block(
|
|
|
|
loader_ctx, &p, p_end, error_buf, error_buf_size)))
|
2020-05-08 04:38:59 +00:00
|
|
|
goto fail;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
RESET_STACK();
|
|
|
|
SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
|
|
|
|
break;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case WASM_OP_BR_IF:
|
2020-03-07 14:20:38 +00:00
|
|
|
{
|
2019-05-07 02:18:18 +00:00
|
|
|
POP_I32();
|
2020-05-08 04:38:59 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!(frame_csp_tmp = check_branch_block(
|
|
|
|
loader_ctx, &p, p_end, error_buf, error_buf_size)))
|
2020-05-08 04:38:59 +00:00
|
|
|
goto fail;
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
case WASM_OP_BR_TABLE:
|
|
|
|
{
|
2020-07-10 08:29:15 +00:00
|
|
|
uint8 *ret_types = NULL;
|
|
|
|
uint32 ret_count = 0;
|
2022-04-23 11:15:55 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP == 0
|
|
|
|
uint8 *p_depth_begin, *p_depth;
|
|
|
|
uint32 depth, j;
|
|
|
|
BrTableCache *br_table_cache = NULL;
|
|
|
|
|
|
|
|
p_org = p - 1;
|
|
|
|
#endif
|
2020-06-02 06:53:06 +00:00
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
read_leb_uint32(p, p_end, count);
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2020-06-28 07:41:25 +00:00
|
|
|
emit_uint32(loader_ctx, count);
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
POP_I32();
|
|
|
|
|
2022-04-23 11:15:55 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP == 0
|
|
|
|
p_depth_begin = p_depth = p;
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
for (i = 0; i <= count; i++) {
|
2020-07-10 08:29:15 +00:00
|
|
|
if (!(frame_csp_tmp =
|
2021-10-08 03:44:39 +00:00
|
|
|
check_branch_block(loader_ctx, &p, p_end,
|
|
|
|
error_buf, error_buf_size)))
|
2020-05-08 04:38:59 +00:00
|
|
|
goto fail;
|
2020-06-02 06:53:06 +00:00
|
|
|
|
|
|
|
if (i == 0) {
|
2020-07-10 08:29:15 +00:00
|
|
|
if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP)
|
2021-10-08 03:44:39 +00:00
|
|
|
ret_count = block_type_get_result_types(
|
|
|
|
&frame_csp_tmp->block_type, &ret_types);
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-07-10 08:29:15 +00:00
|
|
|
uint8 *tmp_ret_types = NULL;
|
|
|
|
uint32 tmp_ret_count = 0;
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
/* Check whether all table items have the same return
|
|
|
|
* type */
|
2020-07-10 08:29:15 +00:00
|
|
|
if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP)
|
2021-10-08 03:44:39 +00:00
|
|
|
tmp_ret_count = block_type_get_result_types(
|
|
|
|
&frame_csp_tmp->block_type, &tmp_ret_types);
|
2020-07-10 08:29:15 +00:00
|
|
|
|
|
|
|
if (ret_count != tmp_ret_count
|
|
|
|
|| (ret_count
|
2021-10-08 03:44:39 +00:00
|
|
|
&& 0
|
|
|
|
!= memcmp(ret_types, tmp_ret_types,
|
|
|
|
ret_count))) {
|
|
|
|
set_error_buf(
|
|
|
|
error_buf, error_buf_size,
|
|
|
|
"type mismatch: br_table targets must "
|
|
|
|
"all use same result type");
|
2020-06-02 06:53:06 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
2022-04-23 11:15:55 +00:00
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_INTERP == 0
|
|
|
|
depth = (uint32)(loader_ctx->frame_csp - 1 - frame_csp_tmp);
|
|
|
|
if (br_table_cache) {
|
|
|
|
br_table_cache->br_depths[i] = depth;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (depth > 255) {
|
|
|
|
/* The depth cannot be stored in one byte,
|
|
|
|
create br_table cache to store each depth */
|
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0
|
|
|
|
if (!record_fast_op(module, p_org, *p_org,
|
|
|
|
error_buf, error_buf_size)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (!(br_table_cache = loader_malloc(
|
|
|
|
offsetof(BrTableCache, br_depths)
|
|
|
|
+ sizeof(uint32)
|
|
|
|
* (uint64)(count + 1),
|
|
|
|
error_buf, error_buf_size))) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
*p_org = EXT_OP_BR_TABLE_CACHE;
|
|
|
|
br_table_cache->br_table_op_addr = p_org;
|
|
|
|
br_table_cache->br_count = count;
|
|
|
|
/* Copy previous depths which are one byte */
|
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
br_table_cache->br_depths[j] = p_depth_begin[j];
|
|
|
|
}
|
|
|
|
br_table_cache->br_depths[i] = depth;
|
|
|
|
bh_list_insert(module->br_table_cache_list,
|
|
|
|
br_table_cache);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* The depth can be stored in one byte, use the
|
|
|
|
byte of the leb to store it */
|
|
|
|
*p_depth++ = (uint8)depth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
2020-03-07 14:20:38 +00:00
|
|
|
|
2022-04-23 11:15:55 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP == 0
|
|
|
|
/* Set the tailing bytes to nop */
|
|
|
|
if (br_table_cache)
|
|
|
|
p_depth = p_depth_begin;
|
|
|
|
while (p_depth < p)
|
|
|
|
*p_depth++ = WASM_OP_NOP;
|
|
|
|
#endif
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
RESET_STACK();
|
|
|
|
SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
|
|
|
|
break;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case WASM_OP_RETURN:
|
|
|
|
{
|
2020-07-10 08:29:15 +00:00
|
|
|
int32 idx;
|
|
|
|
uint8 ret_type;
|
2021-10-08 03:44:39 +00:00
|
|
|
for (idx = (int32)func->func_type->result_count - 1; idx >= 0;
|
|
|
|
idx--) {
|
2020-07-10 08:29:15 +00:00
|
|
|
ret_type = *(func->func_type->types
|
|
|
|
+ func->func_type->param_count + idx);
|
|
|
|
POP_TYPE(ret_type);
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2020-09-20 00:20:45 +00:00
|
|
|
/* emit the offset after return opcode */
|
2020-07-10 08:29:15 +00:00
|
|
|
POP_OFFSET_TYPE(ret_type);
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2020-07-10 08:29:15 +00:00
|
|
|
}
|
2020-03-07 14:20:38 +00:00
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
RESET_STACK();
|
|
|
|
SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
|
2020-07-10 08:29:15 +00:00
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
break;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case WASM_OP_CALL:
|
2020-09-24 04:38:54 +00:00
|
|
|
#if WASM_ENABLE_TAIL_CALL != 0
|
|
|
|
case WASM_OP_RETURN_CALL:
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
|
|
|
WASMType *func_type;
|
|
|
|
int32 idx;
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, func_idx);
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2020-09-20 00:20:45 +00:00
|
|
|
/* we need to emit func_idx before arguments */
|
2020-06-28 07:41:25 +00:00
|
|
|
emit_uint32(loader_ctx, func_idx);
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
if (!check_function_index(module, func_idx, error_buf,
|
|
|
|
error_buf_size)) {
|
2019-05-07 02:18:18 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (func_idx < module->import_function_count)
|
|
|
|
func_type =
|
2021-10-08 03:44:39 +00:00
|
|
|
module->import_functions[func_idx].u.function.func_type;
|
|
|
|
else
|
|
|
|
func_type = module
|
|
|
|
->functions[func_idx
|
|
|
|
- module->import_function_count]
|
|
|
|
->func_type;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2019-11-20 13:16:36 +00:00
|
|
|
if (func_type->param_count > 0) {
|
2021-10-08 03:44:39 +00:00
|
|
|
for (idx = (int32)(func_type->param_count - 1); idx >= 0;
|
|
|
|
idx--) {
|
2019-11-20 13:16:36 +00:00
|
|
|
POP_TYPE(func_type->types[idx]);
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
POP_OFFSET_TYPE(func_type->types[idx]);
|
|
|
|
#endif
|
|
|
|
}
|
2019-11-20 13:16:36 +00:00
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-09-24 04:38:54 +00:00
|
|
|
#if WASM_ENABLE_TAIL_CALL != 0
|
|
|
|
if (opcode == WASM_OP_CALL) {
|
|
|
|
#endif
|
|
|
|
for (i = 0; i < func_type->result_count; i++) {
|
|
|
|
PUSH_TYPE(func_type->types[func_type->param_count + i]);
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
/* Here we emit each return value's dynamic_offset. But
|
|
|
|
* in fact these offsets are continuous, so interpreter
|
|
|
|
* only need to get the first return value's offset.
|
2020-09-24 04:38:54 +00:00
|
|
|
*/
|
2021-10-08 03:44:39 +00:00
|
|
|
PUSH_OFFSET_TYPE(
|
|
|
|
func_type->types[func_type->param_count + i]);
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2020-09-24 04:38:54 +00:00
|
|
|
}
|
|
|
|
#if WASM_ENABLE_TAIL_CALL != 0
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
2020-09-24 04:38:54 +00:00
|
|
|
else {
|
|
|
|
uint8 type;
|
2021-10-08 03:44:39 +00:00
|
|
|
if (func_type->result_count
|
|
|
|
!= func->func_type->result_count) {
|
|
|
|
set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
|
|
|
|
"type mismatch: expect ",
|
2020-09-24 04:38:54 +00:00
|
|
|
func->func_type->result_count,
|
|
|
|
" return values but got other");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
for (i = 0; i < func_type->result_count; i++) {
|
2021-10-08 03:44:39 +00:00
|
|
|
type = func->func_type
|
|
|
|
->types[func->func_type->param_count + i];
|
|
|
|
if (func_type->types[func_type->param_count + i]
|
|
|
|
!= type) {
|
|
|
|
set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
|
|
|
|
"type mismatch: expect ",
|
|
|
|
type2str(type), " but got other");
|
2020-09-24 04:38:54 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RESET_STACK();
|
|
|
|
SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
|
|
|
|
}
|
|
|
|
#endif
|
2022-11-14 06:32:35 +00:00
|
|
|
#if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
|
|
|
|
|| WASM_ENABLE_WAMR_COMPILER != 0
|
Enable AoT and wamr-sdk, and change arguments of call wasm API (#157)
* Implement memory profiler, optimize memory usage, modify code indent
* Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default
* Add a new extension library: connection
* Fix bug of reading magic number and version in big endian platform
* Re-org platform APIs: move most platform APIs from iwasm to shared-lib
* Enhance wasm loader to fix some security issues
* Fix issue about illegal load of EXC_RETURN into PC on stm32 board
* Updates that let a restricted version of the interpreter run in SGX
* Enable native/app address validation and conversion for wasm app
* Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused
* Refine binary size and fix several minor issues
Optimize interpreter LOAD/STORE opcodes to decrease the binary size
Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found
Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper
Add macros of global heap size, stack size, heap size for Zephyr main.c
Clear compile warning of wasm_application.c
* Add more strict security checks for libc wrapper API's
* Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files
* Enhance security of libc strcpy/sprintf wrapper function
* Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions
* Remove get_module_inst() and fix issue of call native
* Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate
* Refine interpreter call native process, refine memory boudary check
* Fix issues of invokeNative function of arm/mips/general version
* Add a switch to build simple sample without gui support
* Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code
* Re-org shared lib header files, remove unused info; fix compile issues of vxworks
* Add build target general
* Remove unused files
* Update license header
* test push
* Restore file
* Sync up with internal/feature
* Sync up with internal/feature
* Rename build_wamr_app to build_wasm_app
* Fix small issues of README
* Enhance malformed wasm file checking
Fix issue of print hex int and implement utf8 string check
Fix wasi file read/write right issue
Fix minor issue of build wasm app doc
* Sync up with internal/feature
* Sync up with internal/feature: fix interpreter arm issue, fix read leb issue
* Sync up with internal/feature
* Fix bug of config.h and rename wasi config.h to ssp_config.h
* Sync up with internal/feature
* Import wamr aot
* update document
* update document
* Update document, disable WASI in 32bit
* update document
* remove files
* update document
* Update document
* update document
* update document
* update samples
* Sync up with internal repo
2020-01-21 05:26:14 +00:00
|
|
|
func->has_op_func_call = true;
|
2022-11-14 06:32:35 +00:00
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
/*
|
|
|
|
* if disable reference type: call_indirect typeidx, 0x00
|
|
|
|
* if enable reference type: call_indirect typeidx, tableidx
|
|
|
|
*/
|
2019-05-07 02:18:18 +00:00
|
|
|
case WASM_OP_CALL_INDIRECT:
|
2020-09-24 04:38:54 +00:00
|
|
|
#if WASM_ENABLE_TAIL_CALL != 0
|
|
|
|
case WASM_OP_RETURN_CALL_INDIRECT:
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
{
|
|
|
|
int32 idx;
|
|
|
|
WASMType *func_type;
|
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
read_leb_uint32(p, p_end, type_idx);
|
|
|
|
#if WASM_ENABLE_REF_TYPES != 0
|
2021-11-18 09:42:49 +00:00
|
|
|
read_leb_uint32(p, p_end, table_idx);
|
2021-04-15 03:29:20 +00:00
|
|
|
#else
|
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
table_idx = read_uint8(p);
|
|
|
|
#endif
|
|
|
|
if (!check_table_index(module, table_idx, error_buf,
|
|
|
|
error_buf_size)) {
|
2019-08-08 08:53:56 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2021-04-15 03:29:20 +00:00
|
|
|
/* we need to emit before arguments */
|
2020-09-29 02:35:10 +00:00
|
|
|
#if WASM_ENABLE_TAIL_CALL != 0
|
|
|
|
emit_byte(loader_ctx, opcode);
|
|
|
|
#endif
|
2020-06-28 07:41:25 +00:00
|
|
|
emit_uint32(loader_ctx, type_idx);
|
2021-04-15 03:29:20 +00:00
|
|
|
emit_uint32(loader_ctx, table_idx);
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2019-10-11 07:25:23 +00:00
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
/* skip elem idx */
|
2019-05-07 02:18:18 +00:00
|
|
|
POP_I32();
|
|
|
|
|
|
|
|
if (type_idx >= module->type_count) {
|
2021-10-08 03:44:39 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "unknown type");
|
2019-05-07 02:18:18 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
func_type = module->types[type_idx];
|
|
|
|
|
2019-11-20 13:16:36 +00:00
|
|
|
if (func_type->param_count > 0) {
|
2021-10-08 03:44:39 +00:00
|
|
|
for (idx = (int32)(func_type->param_count - 1); idx >= 0;
|
|
|
|
idx--) {
|
2019-11-20 13:16:36 +00:00
|
|
|
POP_TYPE(func_type->types[idx]);
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
POP_OFFSET_TYPE(func_type->types[idx]);
|
|
|
|
#endif
|
|
|
|
}
|
2019-11-20 13:16:36 +00:00
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-09-24 04:38:54 +00:00
|
|
|
#if WASM_ENABLE_TAIL_CALL != 0
|
|
|
|
if (opcode == WASM_OP_CALL_INDIRECT) {
|
|
|
|
#endif
|
|
|
|
for (i = 0; i < func_type->result_count; i++) {
|
|
|
|
PUSH_TYPE(func_type->types[func_type->param_count + i]);
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
PUSH_OFFSET_TYPE(
|
|
|
|
func_type->types[func_type->param_count + i]);
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2020-09-24 04:38:54 +00:00
|
|
|
}
|
|
|
|
#if WASM_ENABLE_TAIL_CALL != 0
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
2020-09-24 04:38:54 +00:00
|
|
|
else {
|
|
|
|
uint8 type;
|
2021-10-08 03:44:39 +00:00
|
|
|
if (func_type->result_count
|
|
|
|
!= func->func_type->result_count) {
|
|
|
|
set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
|
|
|
|
"type mismatch: expect ",
|
2020-09-24 04:38:54 +00:00
|
|
|
func->func_type->result_count,
|
|
|
|
" return values but got other");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
for (i = 0; i < func_type->result_count; i++) {
|
2021-10-08 03:44:39 +00:00
|
|
|
type = func->func_type
|
|
|
|
->types[func->func_type->param_count + i];
|
|
|
|
if (func_type->types[func_type->param_count + i]
|
|
|
|
!= type) {
|
|
|
|
set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
|
|
|
|
"type mismatch: expect ",
|
|
|
|
type2str(type), " but got other");
|
2020-09-29 02:35:10 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
2020-09-24 04:38:54 +00:00
|
|
|
}
|
|
|
|
RESET_STACK();
|
|
|
|
SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
|
|
|
|
}
|
|
|
|
#endif
|
2022-11-14 06:32:35 +00:00
|
|
|
#if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
|
|
|
|
|| WASM_ENABLE_WAMR_COMPILER != 0
|
Enable AoT and wamr-sdk, and change arguments of call wasm API (#157)
* Implement memory profiler, optimize memory usage, modify code indent
* Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default
* Add a new extension library: connection
* Fix bug of reading magic number and version in big endian platform
* Re-org platform APIs: move most platform APIs from iwasm to shared-lib
* Enhance wasm loader to fix some security issues
* Fix issue about illegal load of EXC_RETURN into PC on stm32 board
* Updates that let a restricted version of the interpreter run in SGX
* Enable native/app address validation and conversion for wasm app
* Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused
* Refine binary size and fix several minor issues
Optimize interpreter LOAD/STORE opcodes to decrease the binary size
Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found
Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper
Add macros of global heap size, stack size, heap size for Zephyr main.c
Clear compile warning of wasm_application.c
* Add more strict security checks for libc wrapper API's
* Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files
* Enhance security of libc strcpy/sprintf wrapper function
* Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions
* Remove get_module_inst() and fix issue of call native
* Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate
* Refine interpreter call native process, refine memory boudary check
* Fix issues of invokeNative function of arm/mips/general version
* Add a switch to build simple sample without gui support
* Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code
* Re-org shared lib header files, remove unused info; fix compile issues of vxworks
* Add build target general
* Remove unused files
* Update license header
* test push
* Restore file
* Sync up with internal/feature
* Sync up with internal/feature
* Rename build_wamr_app to build_wasm_app
* Fix small issues of README
* Enhance malformed wasm file checking
Fix issue of print hex int and implement utf8 string check
Fix wasi file read/write right issue
Fix minor issue of build wasm app doc
* Sync up with internal/feature
* Sync up with internal/feature: fix interpreter arm issue, fix read leb issue
* Sync up with internal/feature
* Fix bug of config.h and rename wasi config.h to ssp_config.h
* Sync up with internal/feature
* Import wamr aot
* update document
* update document
* Update document, disable WASI in 32bit
* update document
* remove files
* update document
* Update document
* update document
* update document
* update samples
* Sync up with internal repo
2020-01-21 05:26:14 +00:00
|
|
|
func->has_op_func_call = true;
|
2022-11-14 06:32:35 +00:00
|
|
|
#endif
|
|
|
|
#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
|
|
|
|
func->has_op_call_indirect = true;
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case WASM_OP_DROP:
|
|
|
|
{
|
2020-06-02 06:53:06 +00:00
|
|
|
BranchBlock *cur_block = loader_ctx->frame_csp - 1;
|
2021-10-08 03:44:39 +00:00
|
|
|
int32 available_stack_cell =
|
|
|
|
(int32)(loader_ctx->stack_cell_num
|
|
|
|
- cur_block->stack_cell_num);
|
2020-06-02 06:53:06 +00:00
|
|
|
|
|
|
|
if (available_stack_cell <= 0
|
|
|
|
&& !cur_block->is_stack_polymorphic) {
|
2019-05-07 02:18:18 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2020-05-08 04:38:59 +00:00
|
|
|
"type mismatch, opcode drop was found "
|
|
|
|
"but stack was empty");
|
2019-05-07 02:18:18 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
if (available_stack_cell > 0) {
|
2021-12-10 10:13:17 +00:00
|
|
|
if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
|
2020-06-02 06:53:06 +00:00
|
|
|
loader_ctx->frame_ref--;
|
|
|
|
loader_ctx->stack_cell_num--;
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2020-06-02 06:53:06 +00:00
|
|
|
skip_label();
|
|
|
|
loader_ctx->frame_offset--;
|
2022-06-16 01:51:01 +00:00
|
|
|
if ((*(loader_ctx->frame_offset)
|
|
|
|
> loader_ctx->start_dynamic_offset)
|
|
|
|
&& (*(loader_ctx->frame_offset)
|
|
|
|
< loader_ctx->max_dynamic_offset))
|
2021-10-08 03:44:39 +00:00
|
|
|
loader_ctx->dynamic_offset--;
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
2021-12-10 10:13:17 +00:00
|
|
|
else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
|
2020-06-02 06:53:06 +00:00
|
|
|
loader_ctx->frame_ref -= 2;
|
|
|
|
loader_ctx->stack_cell_num -= 2;
|
2022-09-14 11:49:18 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP == 0
|
2020-06-02 06:53:06 +00:00
|
|
|
*(p - 1) = WASM_OP_DROP_64;
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2020-06-02 06:53:06 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
skip_label();
|
|
|
|
loader_ctx->frame_offset -= 2;
|
2022-06-16 01:51:01 +00:00
|
|
|
if ((*(loader_ctx->frame_offset)
|
|
|
|
> loader_ctx->start_dynamic_offset)
|
|
|
|
&& (*(loader_ctx->frame_offset)
|
|
|
|
< loader_ctx->max_dynamic_offset))
|
2020-06-02 06:53:06 +00:00
|
|
|
loader_ctx->dynamic_offset -= 2;
|
|
|
|
#endif
|
|
|
|
}
|
2021-04-15 03:29:20 +00:00
|
|
|
#if WASM_ENABLE_SIMD != 0
|
|
|
|
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
|
|
|
|
else if (*(loader_ctx->frame_ref - 1) == REF_V128_1) {
|
2020-11-05 10:15:15 +00:00
|
|
|
loader_ctx->frame_ref -= 4;
|
|
|
|
loader_ctx->stack_cell_num -= 4;
|
|
|
|
}
|
2021-04-15 03:29:20 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
else {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"type mismatch");
|
|
|
|
goto fail;
|
|
|
|
}
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
skip_label();
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case WASM_OP_SELECT:
|
|
|
|
{
|
|
|
|
uint8 ref_type;
|
2020-06-02 06:53:06 +00:00
|
|
|
BranchBlock *cur_block = loader_ctx->frame_csp - 1;
|
|
|
|
int32 available_stack_cell;
|
2023-10-24 02:47:17 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
POP_I32();
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
available_stack_cell = (int32)(loader_ctx->stack_cell_num
|
|
|
|
- cur_block->stack_cell_num);
|
2020-06-02 06:53:06 +00:00
|
|
|
|
|
|
|
if (available_stack_cell <= 0
|
|
|
|
&& !cur_block->is_stack_polymorphic) {
|
2019-05-07 02:18:18 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2021-04-15 03:29:20 +00:00
|
|
|
"type mismatch or invalid result arity, "
|
|
|
|
"opcode select was found "
|
2020-04-22 10:43:50 +00:00
|
|
|
"but stack was empty");
|
2019-05-07 02:18:18 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
if (available_stack_cell > 0) {
|
|
|
|
switch (*(loader_ctx->frame_ref - 1)) {
|
|
|
|
case REF_I32:
|
|
|
|
case REF_F32:
|
|
|
|
break;
|
|
|
|
case REF_I64_2:
|
|
|
|
case REF_F64_2:
|
2022-09-14 11:49:18 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP == 0
|
2020-06-02 06:53:06 +00:00
|
|
|
*(p - 1) = WASM_OP_SELECT_64;
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2020-06-02 06:53:06 +00:00
|
|
|
if (loader_ctx->p_code_compiled) {
|
2021-04-01 08:50:46 +00:00
|
|
|
uint8 opcode_tmp = WASM_OP_SELECT_64;
|
|
|
|
#if WASM_ENABLE_LABELS_AS_VALUES != 0
|
|
|
|
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
*(void **)(p_code_compiled_tmp
|
|
|
|
- sizeof(void *)) =
|
|
|
|
handle_table[opcode_tmp];
|
2020-03-07 14:20:38 +00:00
|
|
|
#else
|
2023-10-24 02:47:17 +00:00
|
|
|
#if UINTPTR_MAX == UINT64_MAX
|
|
|
|
/* emit int32 relative offset in 64-bit target
|
|
|
|
*/
|
2021-10-08 03:44:39 +00:00
|
|
|
int32 offset =
|
|
|
|
(int32)((uint8 *)handle_table[opcode_tmp]
|
|
|
|
- (uint8 *)handle_table[0]);
|
2023-10-24 02:47:17 +00:00
|
|
|
*(int32 *)(p_code_compiled_tmp
|
|
|
|
- sizeof(int32)) = offset;
|
|
|
|
#else
|
|
|
|
/* emit uint32 label address in 32-bit target */
|
|
|
|
*(uint32 *)(p_code_compiled_tmp
|
|
|
|
- sizeof(uint32)) =
|
|
|
|
(uint32)(uintptr_t)handle_table[opcode_tmp];
|
|
|
|
#endif
|
2021-04-01 08:50:46 +00:00
|
|
|
#endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
|
2021-10-08 03:44:39 +00:00
|
|
|
#else /* else of WASM_ENABLE_LABELS_AS_VALUES */
|
2021-04-01 08:50:46 +00:00
|
|
|
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
|
|
|
|
*(p_code_compiled_tmp - 1) = opcode_tmp;
|
|
|
|
#else
|
|
|
|
*(p_code_compiled_tmp - 2) = opcode_tmp;
|
|
|
|
#endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
|
|
|
|
#endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
2021-04-01 08:50:46 +00:00
|
|
|
#endif /* end of WASM_ENABLE_FAST_INTERP */
|
2020-06-02 06:53:06 +00:00
|
|
|
break;
|
2021-04-15 03:29:20 +00:00
|
|
|
#if WASM_ENABLE_SIMD != 0
|
|
|
|
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
|
|
|
|
case REF_V128_4:
|
2020-11-30 08:03:51 +00:00
|
|
|
break;
|
2021-04-15 03:29:20 +00:00
|
|
|
#endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
|
|
|
|
#endif /* WASM_ENABLE_SIMD != 0 */
|
2021-10-08 03:44:39 +00:00
|
|
|
default:
|
|
|
|
{
|
2021-04-15 03:29:20 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"type mismatch");
|
|
|
|
goto fail;
|
|
|
|
}
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-06-02 06:53:06 +00:00
|
|
|
ref_type = *(loader_ctx->frame_ref - 1);
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
POP_OFFSET_TYPE(ref_type);
|
|
|
|
POP_TYPE(ref_type);
|
|
|
|
POP_OFFSET_TYPE(ref_type);
|
|
|
|
POP_TYPE(ref_type);
|
|
|
|
PUSH_OFFSET_TYPE(ref_type);
|
|
|
|
PUSH_TYPE(ref_type);
|
2021-04-15 03:29:20 +00:00
|
|
|
#else
|
|
|
|
POP2_AND_PUSH(ref_type, ref_type);
|
|
|
|
#endif
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
|
|
|
|
#endif
|
|
|
|
PUSH_TYPE(VALUE_TYPE_ANY);
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
#if WASM_ENABLE_REF_TYPES != 0
|
|
|
|
case WASM_OP_SELECT_T:
|
|
|
|
{
|
|
|
|
uint8 vec_len, ref_type;
|
2023-08-17 11:49:35 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
|
|
|
|
#endif
|
2021-04-15 03:29:20 +00:00
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, vec_len);
|
2023-07-31 10:20:11 +00:00
|
|
|
if (vec_len != 1) {
|
|
|
|
/* typed select must have exactly one result */
|
2021-04-15 03:29:20 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"invalid result arity");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
ref_type = read_uint8(p);
|
|
|
|
if (!is_value_type(ref_type)) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"unknown value type");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
POP_I32();
|
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
if (loader_ctx->p_code_compiled) {
|
|
|
|
uint8 opcode_tmp = WASM_OP_SELECT;
|
|
|
|
|
|
|
|
if (ref_type == VALUE_TYPE_V128) {
|
|
|
|
#if (WASM_ENABLE_SIMD == 0) \
|
|
|
|
|| ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"SIMD v128 type isn't supported");
|
|
|
|
goto fail;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (ref_type == VALUE_TYPE_F64
|
2021-10-08 03:44:39 +00:00
|
|
|
|| ref_type == VALUE_TYPE_I64)
|
2021-04-15 03:29:20 +00:00
|
|
|
opcode_tmp = WASM_OP_SELECT_64;
|
|
|
|
#if WASM_ENABLE_LABELS_AS_VALUES != 0
|
|
|
|
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
*(void **)(p_code_compiled_tmp - sizeof(void *)) =
|
|
|
|
handle_table[opcode_tmp];
|
2021-04-15 03:29:20 +00:00
|
|
|
#else
|
2023-10-24 02:47:17 +00:00
|
|
|
#if UINTPTR_MAX == UINT64_MAX
|
|
|
|
/* emit int32 relative offset in 64-bit target */
|
2021-10-08 03:44:39 +00:00
|
|
|
int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
|
|
|
|
- (uint8 *)handle_table[0]);
|
2023-10-24 02:47:17 +00:00
|
|
|
*(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
|
|
|
|
offset;
|
|
|
|
#else
|
|
|
|
/* emit uint32 label address in 32-bit target */
|
|
|
|
*(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
|
|
|
|
(uint32)(uintptr_t)handle_table[opcode_tmp];
|
|
|
|
#endif
|
2021-04-15 03:29:20 +00:00
|
|
|
#endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
|
2021-10-08 03:44:39 +00:00
|
|
|
#else /* else of WASM_ENABLE_LABELS_AS_VALUES */
|
2021-04-15 03:29:20 +00:00
|
|
|
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
|
|
|
|
*(p_code_compiled_tmp - 1) = opcode_tmp;
|
|
|
|
#else
|
|
|
|
*(p_code_compiled_tmp - 2) = opcode_tmp;
|
|
|
|
#endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
|
|
|
|
#endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* WASM_ENABLE_FAST_INTERP != 0 */
|
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
POP_OFFSET_TYPE(ref_type);
|
|
|
|
POP_TYPE(ref_type);
|
|
|
|
POP_OFFSET_TYPE(ref_type);
|
|
|
|
POP_TYPE(ref_type);
|
|
|
|
PUSH_OFFSET_TYPE(ref_type);
|
|
|
|
PUSH_TYPE(ref_type);
|
|
|
|
#else
|
|
|
|
POP2_AND_PUSH(ref_type, ref_type);
|
|
|
|
#endif /* WASM_ENABLE_FAST_INTERP != 0 */
|
|
|
|
|
|
|
|
(void)vec_len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* table.get x. tables[x]. [i32] -> [t] */
|
|
|
|
/* table.set x. tables[x]. [i32 t] -> [] */
|
|
|
|
case WASM_OP_TABLE_GET:
|
|
|
|
case WASM_OP_TABLE_SET:
|
|
|
|
{
|
|
|
|
uint8 decl_ref_type;
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, table_idx);
|
|
|
|
if (!get_table_elem_type(module, table_idx, &decl_ref_type,
|
|
|
|
error_buf, error_buf_size))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
emit_uint32(loader_ctx, table_idx);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (opcode == WASM_OP_TABLE_GET) {
|
|
|
|
POP_I32();
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
PUSH_OFFSET_TYPE(decl_ref_type);
|
|
|
|
#endif
|
|
|
|
PUSH_TYPE(decl_ref_type);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
POP_OFFSET_TYPE(decl_ref_type);
|
|
|
|
#endif
|
|
|
|
POP_TYPE(decl_ref_type);
|
|
|
|
POP_I32();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WASM_OP_REF_NULL:
|
|
|
|
{
|
|
|
|
uint8 ref_type;
|
|
|
|
|
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
ref_type = read_uint8(p);
|
|
|
|
if (ref_type != VALUE_TYPE_FUNCREF
|
|
|
|
&& ref_type != VALUE_TYPE_EXTERNREF) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"unknown value type");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
PUSH_OFFSET_TYPE(ref_type);
|
|
|
|
#endif
|
|
|
|
PUSH_TYPE(ref_type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WASM_OP_REF_IS_NULL:
|
|
|
|
{
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!wasm_loader_pop_frame_ref_offset(loader_ctx,
|
|
|
|
VALUE_TYPE_FUNCREF,
|
|
|
|
error_buf, error_buf_size)
|
2021-04-15 03:29:20 +00:00
|
|
|
&& !wasm_loader_pop_frame_ref_offset(
|
2021-10-08 03:44:39 +00:00
|
|
|
loader_ctx, VALUE_TYPE_EXTERNREF, error_buf,
|
|
|
|
error_buf_size)) {
|
2021-04-15 03:29:20 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
#else
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
|
|
|
|
error_buf, error_buf_size)
|
|
|
|
&& !wasm_loader_pop_frame_ref(loader_ctx,
|
|
|
|
VALUE_TYPE_EXTERNREF,
|
|
|
|
error_buf, error_buf_size)) {
|
2021-04-15 03:29:20 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
PUSH_I32();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WASM_OP_REF_FUNC:
|
|
|
|
{
|
|
|
|
read_leb_uint32(p, p_end, func_idx);
|
|
|
|
|
|
|
|
if (!check_function_index(module, func_idx, error_buf,
|
|
|
|
error_buf_size)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2023-04-03 07:55:24 +00:00
|
|
|
/* Refer to a forward-declared function */
|
|
|
|
if (func_idx >= cur_func_idx + module->import_function_count) {
|
2021-04-15 03:29:20 +00:00
|
|
|
WASMTableSeg *table_seg = module->table_segments;
|
|
|
|
bool func_declared = false;
|
|
|
|
uint32 j;
|
|
|
|
|
2023-04-03 07:55:24 +00:00
|
|
|
/* Check whether the function is declared in table segs */
|
2021-04-15 03:29:20 +00:00
|
|
|
for (i = 0; i < module->table_seg_count; i++, table_seg++) {
|
|
|
|
if (table_seg->elem_type == VALUE_TYPE_FUNCREF
|
|
|
|
&& wasm_elem_is_declarative(table_seg->mode)) {
|
2021-10-08 03:44:39 +00:00
|
|
|
for (j = 0; j < table_seg->function_count; j++) {
|
2022-08-08 05:22:23 +00:00
|
|
|
if (table_seg->func_indexes[j] == func_idx) {
|
2021-04-15 03:29:20 +00:00
|
|
|
func_declared = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!func_declared) {
|
2023-04-03 07:55:24 +00:00
|
|
|
/* Check whether the function is exported */
|
|
|
|
for (i = 0; i < module->export_count; i++) {
|
|
|
|
if (module->exports[i].kind == EXPORT_KIND_FUNC
|
|
|
|
&& module->exports[i].index == func_idx) {
|
|
|
|
func_declared = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!func_declared) {
|
2021-04-15 03:29:20 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"undeclared function reference");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
emit_uint32(loader_ctx, func_idx);
|
|
|
|
#endif
|
|
|
|
PUSH_FUNCREF();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif /* WASM_ENABLE_REF_TYPES */
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
case WASM_OP_GET_LOCAL:
|
|
|
|
{
|
2020-02-18 07:15:24 +00:00
|
|
|
p_org = p - 1;
|
|
|
|
GET_LOCAL_INDEX_TYPE_AND_OFFSET();
|
2019-05-07 02:18:18 +00:00
|
|
|
PUSH_TYPE(local_type);
|
2020-02-18 07:15:24 +00:00
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
/* Get Local is optimized out */
|
|
|
|
skip_label();
|
|
|
|
disable_emit = true;
|
|
|
|
operand_offset = local_offset;
|
|
|
|
PUSH_OFFSET_TYPE(local_type);
|
|
|
|
#else
|
2022-03-24 06:14:42 +00:00
|
|
|
#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
|
2022-08-02 08:03:50 +00:00
|
|
|
&& (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
|
2020-02-18 07:15:24 +00:00
|
|
|
if (local_offset < 0x80) {
|
2020-03-07 14:20:38 +00:00
|
|
|
*p_org++ = EXT_OP_GET_LOCAL_FAST;
|
2021-09-29 05:36:46 +00:00
|
|
|
if (is_32bit_type(local_type)) {
|
2020-02-18 07:15:24 +00:00
|
|
|
*p_org++ = (uint8)local_offset;
|
2021-09-29 05:36:46 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-02-18 07:15:24 +00:00
|
|
|
*p_org++ = (uint8)(local_offset | 0x80);
|
2021-09-29 05:36:46 +00:00
|
|
|
}
|
|
|
|
while (p_org < p) {
|
2020-02-18 07:15:24 +00:00
|
|
|
*p_org++ = WASM_OP_NOP;
|
2021-09-29 05:36:46 +00:00
|
|
|
}
|
2020-02-18 07:15:24 +00:00
|
|
|
}
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2022-03-24 06:14:42 +00:00
|
|
|
#endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case WASM_OP_SET_LOCAL:
|
|
|
|
{
|
2020-02-18 07:15:24 +00:00
|
|
|
p_org = p - 1;
|
|
|
|
GET_LOCAL_INDEX_TYPE_AND_OFFSET();
|
2019-05-07 02:18:18 +00:00
|
|
|
POP_TYPE(local_type);
|
2020-02-18 07:15:24 +00:00
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!(preserve_referenced_local(
|
|
|
|
loader_ctx, opcode, local_offset, local_type,
|
|
|
|
&preserve_local, error_buf, error_buf_size)))
|
2020-03-27 01:28:30 +00:00
|
|
|
goto fail;
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
if (local_offset < 256) {
|
|
|
|
skip_label();
|
2020-03-27 01:28:30 +00:00
|
|
|
if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
|
2020-03-07 14:20:38 +00:00
|
|
|
if (loader_ctx->p_code_compiled)
|
2021-10-08 03:44:39 +00:00
|
|
|
STORE_U16(loader_ctx->p_code_compiled - 2,
|
|
|
|
local_offset);
|
|
|
|
loader_ctx->frame_offset--;
|
|
|
|
loader_ctx->dynamic_offset--;
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
2020-03-27 01:28:30 +00:00
|
|
|
else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
|
2020-03-07 14:20:38 +00:00
|
|
|
if (loader_ctx->p_code_compiled)
|
2021-10-08 03:44:39 +00:00
|
|
|
STORE_U16(loader_ctx->p_code_compiled - 2,
|
|
|
|
local_offset);
|
2020-03-07 14:20:38 +00:00
|
|
|
loader_ctx->frame_offset -= 2;
|
|
|
|
loader_ctx->dynamic_offset -= 2;
|
|
|
|
}
|
|
|
|
else {
|
2021-04-15 03:29:20 +00:00
|
|
|
if (is_32bit_type(local_type)) {
|
2020-03-07 14:20:38 +00:00
|
|
|
emit_label(EXT_OP_SET_LOCAL_FAST);
|
2021-04-01 08:50:46 +00:00
|
|
|
emit_byte(loader_ctx, (uint8)local_offset);
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
emit_label(EXT_OP_SET_LOCAL_FAST_I64);
|
2021-04-01 08:50:46 +00:00
|
|
|
emit_byte(loader_ctx, (uint8)local_offset);
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
|
|
|
POP_OFFSET_TYPE(local_type);
|
|
|
|
}
|
|
|
|
}
|
2021-10-08 03:44:39 +00:00
|
|
|
else { /* local index larger than 255, reserve leb */
|
2021-01-05 10:05:30 +00:00
|
|
|
emit_uint32(loader_ctx, local_idx);
|
2020-03-07 14:20:38 +00:00
|
|
|
POP_OFFSET_TYPE(local_type);
|
|
|
|
}
|
|
|
|
#else
|
2022-03-24 06:14:42 +00:00
|
|
|
#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
|
2022-08-02 08:03:50 +00:00
|
|
|
&& (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
|
2020-02-18 07:15:24 +00:00
|
|
|
if (local_offset < 0x80) {
|
2020-03-07 14:20:38 +00:00
|
|
|
*p_org++ = EXT_OP_SET_LOCAL_FAST;
|
2021-09-29 05:36:46 +00:00
|
|
|
if (is_32bit_type(local_type)) {
|
2020-02-18 07:15:24 +00:00
|
|
|
*p_org++ = (uint8)local_offset;
|
2021-09-29 05:36:46 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-02-18 07:15:24 +00:00
|
|
|
*p_org++ = (uint8)(local_offset | 0x80);
|
2021-09-29 05:36:46 +00:00
|
|
|
}
|
|
|
|
while (p_org < p) {
|
2020-02-18 07:15:24 +00:00
|
|
|
*p_org++ = WASM_OP_NOP;
|
2021-09-29 05:36:46 +00:00
|
|
|
}
|
2020-02-18 07:15:24 +00:00
|
|
|
}
|
|
|
|
#endif
|
2022-03-24 06:14:42 +00:00
|
|
|
#endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case WASM_OP_TEE_LOCAL:
|
|
|
|
{
|
2020-02-18 07:15:24 +00:00
|
|
|
p_org = p - 1;
|
|
|
|
GET_LOCAL_INDEX_TYPE_AND_OFFSET();
|
2020-06-02 06:53:06 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
/* If the stack is in polymorphic state, do fake pop and push on
|
2021-10-08 03:44:39 +00:00
|
|
|
offset stack to keep the depth of offset stack to be the
|
|
|
|
same with ref stack */
|
2020-06-02 06:53:06 +00:00
|
|
|
BranchBlock *cur_block = loader_ctx->frame_csp - 1;
|
|
|
|
if (cur_block->is_stack_polymorphic) {
|
|
|
|
POP_OFFSET_TYPE(local_type);
|
|
|
|
PUSH_OFFSET_TYPE(local_type);
|
|
|
|
}
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
POP_TYPE(local_type);
|
|
|
|
PUSH_TYPE(local_type);
|
2020-02-18 07:15:24 +00:00
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!(preserve_referenced_local(
|
|
|
|
loader_ctx, opcode, local_offset, local_type,
|
|
|
|
&preserve_local, error_buf, error_buf_size)))
|
2020-03-27 01:28:30 +00:00
|
|
|
goto fail;
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
if (local_offset < 256) {
|
|
|
|
skip_label();
|
2021-04-15 03:29:20 +00:00
|
|
|
if (is_32bit_type(local_type)) {
|
2020-03-07 14:20:38 +00:00
|
|
|
emit_label(EXT_OP_TEE_LOCAL_FAST);
|
2021-04-01 08:50:46 +00:00
|
|
|
emit_byte(loader_ctx, (uint8)local_offset);
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
|
2021-04-01 08:50:46 +00:00
|
|
|
emit_byte(loader_ctx, (uint8)local_offset);
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-08 03:44:39 +00:00
|
|
|
else { /* local index larger than 255, reserve leb */
|
2021-01-05 10:05:30 +00:00
|
|
|
emit_uint32(loader_ctx, local_idx);
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
2021-10-08 03:44:39 +00:00
|
|
|
emit_operand(loader_ctx,
|
|
|
|
*(loader_ctx->frame_offset
|
|
|
|
- wasm_value_type_cell_num(local_type)));
|
2020-03-07 14:20:38 +00:00
|
|
|
#else
|
2022-03-24 06:14:42 +00:00
|
|
|
#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
|
2022-08-02 08:03:50 +00:00
|
|
|
&& (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
|
2020-02-18 07:15:24 +00:00
|
|
|
if (local_offset < 0x80) {
|
2020-03-07 14:20:38 +00:00
|
|
|
*p_org++ = EXT_OP_TEE_LOCAL_FAST;
|
2021-09-29 05:36:46 +00:00
|
|
|
if (is_32bit_type(local_type)) {
|
2020-02-18 07:15:24 +00:00
|
|
|
*p_org++ = (uint8)local_offset;
|
2021-09-29 05:36:46 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-02-18 07:15:24 +00:00
|
|
|
*p_org++ = (uint8)(local_offset | 0x80);
|
2021-09-29 05:36:46 +00:00
|
|
|
}
|
|
|
|
while (p_org < p) {
|
2020-02-18 07:15:24 +00:00
|
|
|
*p_org++ = WASM_OP_NOP;
|
2021-09-29 05:36:46 +00:00
|
|
|
}
|
2020-02-18 07:15:24 +00:00
|
|
|
}
|
|
|
|
#endif
|
2022-03-24 06:14:42 +00:00
|
|
|
#endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case WASM_OP_GET_GLOBAL:
|
|
|
|
{
|
2020-06-29 06:17:27 +00:00
|
|
|
p_org = p - 1;
|
2019-05-07 02:18:18 +00:00
|
|
|
read_leb_uint32(p, p_end, global_idx);
|
|
|
|
if (global_idx >= global_count) {
|
2021-10-08 03:44:39 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "unknown global");
|
2019-05-07 02:18:18 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2020-06-29 06:17:27 +00:00
|
|
|
global_type =
|
2021-10-08 03:44:39 +00:00
|
|
|
global_idx < module->import_global_count
|
|
|
|
? module->import_globals[global_idx].u.global.type
|
|
|
|
: module
|
|
|
|
->globals[global_idx
|
|
|
|
- module->import_global_count]
|
|
|
|
.type;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
PUSH_TYPE(global_type);
|
2020-06-29 06:17:27 +00:00
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_INTERP == 0
|
|
|
|
if (global_type == VALUE_TYPE_I64
|
|
|
|
|| global_type == VALUE_TYPE_F64) {
|
2021-09-29 05:36:46 +00:00
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0
|
2022-04-23 11:15:55 +00:00
|
|
|
if (!record_fast_op(module, p_org, *p_org, error_buf,
|
|
|
|
error_buf_size)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2021-09-29 05:36:46 +00:00
|
|
|
#endif
|
2020-06-29 06:17:27 +00:00
|
|
|
*p_org = WASM_OP_GET_GLOBAL_64;
|
|
|
|
}
|
2021-10-08 03:44:39 +00:00
|
|
|
#else /* else of WASM_ENABLE_FAST_INTERP */
|
2020-06-29 06:17:27 +00:00
|
|
|
if (global_type == VALUE_TYPE_I64
|
|
|
|
|| global_type == VALUE_TYPE_F64) {
|
|
|
|
skip_label();
|
|
|
|
emit_label(WASM_OP_GET_GLOBAL_64);
|
|
|
|
}
|
2020-06-28 07:41:25 +00:00
|
|
|
emit_uint32(loader_ctx, global_idx);
|
2020-03-07 14:20:38 +00:00
|
|
|
PUSH_OFFSET_TYPE(global_type);
|
2020-06-29 06:17:27 +00:00
|
|
|
#endif /* end of WASM_ENABLE_FAST_INTERP */
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case WASM_OP_SET_GLOBAL:
|
|
|
|
{
|
2020-06-08 03:19:09 +00:00
|
|
|
bool is_mutable = false;
|
2020-06-29 06:17:27 +00:00
|
|
|
|
|
|
|
p_org = p - 1;
|
2019-05-07 02:18:18 +00:00
|
|
|
read_leb_uint32(p, p_end, global_idx);
|
|
|
|
if (global_idx >= global_count) {
|
2021-10-08 03:44:39 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size, "unknown global");
|
2019-05-07 02:18:18 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2020-06-08 03:19:09 +00:00
|
|
|
is_mutable =
|
2021-10-08 03:44:39 +00:00
|
|
|
global_idx < module->import_global_count
|
|
|
|
? module->import_globals[global_idx].u.global.is_mutable
|
|
|
|
: module
|
|
|
|
->globals[global_idx
|
|
|
|
- module->import_global_count]
|
|
|
|
.is_mutable;
|
2020-06-08 03:19:09 +00:00
|
|
|
if (!is_mutable) {
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2020-06-02 06:53:06 +00:00
|
|
|
"global is immutable");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
global_type =
|
2021-10-08 03:44:39 +00:00
|
|
|
global_idx < module->import_global_count
|
|
|
|
? module->import_globals[global_idx].u.global.type
|
|
|
|
: module
|
|
|
|
->globals[global_idx
|
|
|
|
- module->import_global_count]
|
|
|
|
.type;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
POP_TYPE(global_type);
|
2020-06-29 06:17:27 +00:00
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_INTERP == 0
|
|
|
|
if (global_type == VALUE_TYPE_I64
|
|
|
|
|| global_type == VALUE_TYPE_F64) {
|
2021-09-29 05:36:46 +00:00
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0
|
2022-04-23 11:15:55 +00:00
|
|
|
if (!record_fast_op(module, p_org, *p_org, error_buf,
|
|
|
|
error_buf_size)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2021-09-29 05:36:46 +00:00
|
|
|
#endif
|
2020-06-29 06:17:27 +00:00
|
|
|
*p_org = WASM_OP_SET_GLOBAL_64;
|
|
|
|
}
|
2020-08-20 04:43:12 +00:00
|
|
|
else if (module->aux_stack_size > 0
|
|
|
|
&& global_idx == module->aux_stack_top_global_index) {
|
2021-09-29 05:36:46 +00:00
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0
|
2022-04-23 11:15:55 +00:00
|
|
|
if (!record_fast_op(module, p_org, *p_org, error_buf,
|
|
|
|
error_buf_size)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2021-09-29 05:36:46 +00:00
|
|
|
#endif
|
2020-06-29 06:17:27 +00:00
|
|
|
*p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
|
2022-11-14 06:32:35 +00:00
|
|
|
#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
|
|
|
|
func->has_op_set_global_aux_stack = true;
|
|
|
|
#endif
|
2020-06-29 06:17:27 +00:00
|
|
|
}
|
2021-10-08 03:44:39 +00:00
|
|
|
#else /* else of WASM_ENABLE_FAST_INTERP */
|
2020-06-29 06:17:27 +00:00
|
|
|
if (global_type == VALUE_TYPE_I64
|
|
|
|
|| global_type == VALUE_TYPE_F64) {
|
|
|
|
skip_label();
|
|
|
|
emit_label(WASM_OP_SET_GLOBAL_64);
|
|
|
|
}
|
2020-08-20 04:43:12 +00:00
|
|
|
else if (module->aux_stack_size > 0
|
|
|
|
&& global_idx == module->aux_stack_top_global_index) {
|
2020-06-29 06:17:27 +00:00
|
|
|
skip_label();
|
|
|
|
emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
|
|
|
|
}
|
2020-06-28 07:41:25 +00:00
|
|
|
emit_uint32(loader_ctx, global_idx);
|
2020-03-07 14:20:38 +00:00
|
|
|
POP_OFFSET_TYPE(global_type);
|
2020-06-29 06:17:27 +00:00
|
|
|
#endif /* end of WASM_ENABLE_FAST_INTERP */
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
/* load */
|
2019-05-07 02:18:18 +00:00
|
|
|
case WASM_OP_I32_LOAD:
|
|
|
|
case WASM_OP_I32_LOAD8_S:
|
|
|
|
case WASM_OP_I32_LOAD8_U:
|
|
|
|
case WASM_OP_I32_LOAD16_S:
|
|
|
|
case WASM_OP_I32_LOAD16_U:
|
|
|
|
case WASM_OP_I64_LOAD:
|
|
|
|
case WASM_OP_I64_LOAD8_S:
|
|
|
|
case WASM_OP_I64_LOAD8_U:
|
|
|
|
case WASM_OP_I64_LOAD16_S:
|
|
|
|
case WASM_OP_I64_LOAD16_U:
|
|
|
|
case WASM_OP_I64_LOAD32_S:
|
|
|
|
case WASM_OP_I64_LOAD32_U:
|
|
|
|
case WASM_OP_F32_LOAD:
|
|
|
|
case WASM_OP_F64_LOAD:
|
2020-03-07 14:20:38 +00:00
|
|
|
/* store */
|
2019-05-07 02:18:18 +00:00
|
|
|
case WASM_OP_I32_STORE:
|
|
|
|
case WASM_OP_I32_STORE8:
|
|
|
|
case WASM_OP_I32_STORE16:
|
|
|
|
case WASM_OP_I64_STORE:
|
|
|
|
case WASM_OP_I64_STORE8:
|
|
|
|
case WASM_OP_I64_STORE16:
|
|
|
|
case WASM_OP_I64_STORE32:
|
|
|
|
case WASM_OP_F32_STORE:
|
|
|
|
case WASM_OP_F64_STORE:
|
2020-03-07 14:20:38 +00:00
|
|
|
{
|
2020-03-12 00:59:02 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
/* change F32/F64 into I32/I64 */
|
|
|
|
if (opcode == WASM_OP_F32_LOAD) {
|
|
|
|
skip_label();
|
|
|
|
emit_label(WASM_OP_I32_LOAD);
|
|
|
|
}
|
|
|
|
else if (opcode == WASM_OP_F64_LOAD) {
|
|
|
|
skip_label();
|
|
|
|
emit_label(WASM_OP_I64_LOAD);
|
|
|
|
}
|
|
|
|
else if (opcode == WASM_OP_F32_STORE) {
|
|
|
|
skip_label();
|
|
|
|
emit_label(WASM_OP_I32_STORE);
|
|
|
|
}
|
|
|
|
else if (opcode == WASM_OP_F64_STORE) {
|
|
|
|
skip_label();
|
|
|
|
emit_label(WASM_OP_I64_STORE);
|
|
|
|
}
|
|
|
|
#endif
|
2019-08-08 08:53:56 +00:00
|
|
|
CHECK_MEMORY();
|
2021-10-08 03:44:39 +00:00
|
|
|
read_leb_uint32(p, p_end, align); /* align */
|
2020-03-07 14:20:38 +00:00
|
|
|
read_leb_uint32(p, p_end, mem_offset); /* offset */
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!check_memory_access_align(opcode, align, error_buf,
|
|
|
|
error_buf_size)) {
|
2020-06-02 06:53:06 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2020-06-28 07:41:25 +00:00
|
|
|
emit_uint32(loader_ctx, mem_offset);
|
2022-11-14 06:32:35 +00:00
|
|
|
#endif
|
|
|
|
#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
|
|
|
|
func->has_memory_operations = true;
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2021-10-08 03:44:39 +00:00
|
|
|
switch (opcode) {
|
2020-03-07 14:20:38 +00:00
|
|
|
/* load */
|
|
|
|
case WASM_OP_I32_LOAD:
|
|
|
|
case WASM_OP_I32_LOAD8_S:
|
|
|
|
case WASM_OP_I32_LOAD8_U:
|
|
|
|
case WASM_OP_I32_LOAD16_S:
|
|
|
|
case WASM_OP_I32_LOAD16_U:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
|
2020-03-07 14:20:38 +00:00
|
|
|
break;
|
|
|
|
case WASM_OP_I64_LOAD:
|
|
|
|
case WASM_OP_I64_LOAD8_S:
|
|
|
|
case WASM_OP_I64_LOAD8_U:
|
|
|
|
case WASM_OP_I64_LOAD16_S:
|
|
|
|
case WASM_OP_I64_LOAD16_U:
|
|
|
|
case WASM_OP_I64_LOAD32_S:
|
|
|
|
case WASM_OP_I64_LOAD32_U:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
|
2020-03-07 14:20:38 +00:00
|
|
|
break;
|
|
|
|
case WASM_OP_F32_LOAD:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
|
2020-03-07 14:20:38 +00:00
|
|
|
break;
|
|
|
|
case WASM_OP_F64_LOAD:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
|
2020-03-07 14:20:38 +00:00
|
|
|
break;
|
|
|
|
/* store */
|
|
|
|
case WASM_OP_I32_STORE:
|
|
|
|
case WASM_OP_I32_STORE8:
|
|
|
|
case WASM_OP_I32_STORE16:
|
|
|
|
POP_I32();
|
|
|
|
POP_I32();
|
|
|
|
break;
|
|
|
|
case WASM_OP_I64_STORE:
|
|
|
|
case WASM_OP_I64_STORE8:
|
|
|
|
case WASM_OP_I64_STORE16:
|
|
|
|
case WASM_OP_I64_STORE32:
|
|
|
|
POP_I64();
|
|
|
|
POP_I32();
|
|
|
|
break;
|
|
|
|
case WASM_OP_F32_STORE:
|
|
|
|
POP_F32();
|
|
|
|
POP_I32();
|
|
|
|
break;
|
|
|
|
case WASM_OP_F64_STORE:
|
|
|
|
POP_F64();
|
|
|
|
POP_I32();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
|
|
|
|
case WASM_OP_MEMORY_SIZE:
|
2019-08-08 08:53:56 +00:00
|
|
|
CHECK_MEMORY();
|
2019-10-11 07:25:23 +00:00
|
|
|
/* reserved byte 0x00 */
|
|
|
|
if (*p++ != 0x00) {
|
2021-04-15 03:29:20 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"zero byte expected");
|
2019-10-11 07:25:23 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
2019-05-07 02:18:18 +00:00
|
|
|
PUSH_I32();
|
2020-07-30 02:18:55 +00:00
|
|
|
|
|
|
|
module->possible_memory_grow = true;
|
2022-11-14 06:32:35 +00:00
|
|
|
#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
|
|
|
|
func->has_memory_operations = true;
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_MEMORY_GROW:
|
2019-08-08 08:53:56 +00:00
|
|
|
CHECK_MEMORY();
|
2019-10-11 07:25:23 +00:00
|
|
|
/* reserved byte 0x00 */
|
|
|
|
if (*p++ != 0x00) {
|
2021-04-15 03:29:20 +00:00
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"zero byte expected");
|
2019-10-11 07:25:23 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
|
Enable AoT and wamr-sdk, and change arguments of call wasm API (#157)
* Implement memory profiler, optimize memory usage, modify code indent
* Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default
* Add a new extension library: connection
* Fix bug of reading magic number and version in big endian platform
* Re-org platform APIs: move most platform APIs from iwasm to shared-lib
* Enhance wasm loader to fix some security issues
* Fix issue about illegal load of EXC_RETURN into PC on stm32 board
* Updates that let a restricted version of the interpreter run in SGX
* Enable native/app address validation and conversion for wasm app
* Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused
* Refine binary size and fix several minor issues
Optimize interpreter LOAD/STORE opcodes to decrease the binary size
Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found
Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper
Add macros of global heap size, stack size, heap size for Zephyr main.c
Clear compile warning of wasm_application.c
* Add more strict security checks for libc wrapper API's
* Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files
* Enhance security of libc strcpy/sprintf wrapper function
* Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions
* Remove get_module_inst() and fix issue of call native
* Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate
* Refine interpreter call native process, refine memory boudary check
* Fix issues of invokeNative function of arm/mips/general version
* Add a switch to build simple sample without gui support
* Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code
* Re-org shared lib header files, remove unused info; fix compile issues of vxworks
* Add build target general
* Remove unused files
* Update license header
* test push
* Restore file
* Sync up with internal/feature
* Sync up with internal/feature
* Rename build_wamr_app to build_wasm_app
* Fix small issues of README
* Enhance malformed wasm file checking
Fix issue of print hex int and implement utf8 string check
Fix wasi file read/write right issue
Fix minor issue of build wasm app doc
* Sync up with internal/feature
* Sync up with internal/feature: fix interpreter arm issue, fix read leb issue
* Sync up with internal/feature
* Fix bug of config.h and rename wasi config.h to ssp_config.h
* Sync up with internal/feature
* Import wamr aot
* update document
* update document
* Update document, disable WASI in 32bit
* update document
* remove files
* update document
* Update document
* update document
* update document
* update samples
* Sync up with internal repo
2020-01-21 05:26:14 +00:00
|
|
|
|
|
|
|
module->possible_memory_grow = true;
|
2022-11-14 06:32:35 +00:00
|
|
|
#if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
|
|
|
|
|| WASM_ENABLE_WAMR_COMPILER != 0
|
|
|
|
func->has_op_memory_grow = true;
|
|
|
|
#endif
|
|
|
|
#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
|
|
|
|
func->has_memory_operations = true;
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_I32_CONST:
|
|
|
|
read_leb_int32(p, p_end, i32_const);
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
skip_label();
|
|
|
|
disable_emit = true;
|
|
|
|
GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
|
2022-04-03 23:55:37 +00:00
|
|
|
|
|
|
|
if (operand_offset == 0) {
|
|
|
|
disable_emit = false;
|
|
|
|
emit_label(WASM_OP_I32_CONST);
|
|
|
|
emit_uint32(loader_ctx, i32_const);
|
|
|
|
}
|
2020-06-02 06:53:06 +00:00
|
|
|
#else
|
|
|
|
(void)i32_const;
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
PUSH_I32();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_I64_CONST:
|
2021-12-10 10:13:17 +00:00
|
|
|
read_leb_int64(p, p_end, i64_const);
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
skip_label();
|
|
|
|
disable_emit = true;
|
2021-12-10 10:13:17 +00:00
|
|
|
GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
|
2022-04-03 23:55:37 +00:00
|
|
|
|
|
|
|
if (operand_offset == 0) {
|
|
|
|
disable_emit = false;
|
|
|
|
emit_label(WASM_OP_I64_CONST);
|
|
|
|
emit_uint64(loader_ctx, i64_const);
|
|
|
|
}
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
PUSH_I64();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_F32_CONST:
|
|
|
|
p += sizeof(float32);
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
skip_label();
|
|
|
|
disable_emit = true;
|
2021-12-10 10:13:17 +00:00
|
|
|
bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
|
2021-10-08 03:44:39 +00:00
|
|
|
sizeof(float32));
|
2021-12-10 10:13:17 +00:00
|
|
|
GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
|
2022-04-03 23:55:37 +00:00
|
|
|
|
|
|
|
if (operand_offset == 0) {
|
|
|
|
disable_emit = false;
|
|
|
|
emit_label(WASM_OP_F32_CONST);
|
|
|
|
emit_float32(loader_ctx, f32_const);
|
|
|
|
}
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
PUSH_F32();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_F64_CONST:
|
|
|
|
p += sizeof(float64);
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
skip_label();
|
|
|
|
disable_emit = true;
|
2020-03-10 03:48:28 +00:00
|
|
|
/* Some MCU may require 8-byte align */
|
2021-12-10 10:13:17 +00:00
|
|
|
bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
|
2021-10-08 03:44:39 +00:00
|
|
|
sizeof(float64));
|
2021-12-10 10:13:17 +00:00
|
|
|
GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
|
2022-04-03 23:55:37 +00:00
|
|
|
|
|
|
|
if (operand_offset == 0) {
|
|
|
|
disable_emit = false;
|
|
|
|
emit_label(WASM_OP_F64_CONST);
|
|
|
|
emit_float64(loader_ctx, f64_const);
|
|
|
|
}
|
2020-03-07 14:20:38 +00:00
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
PUSH_F64();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_I32_EQZ:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_I32_EQ:
|
|
|
|
case WASM_OP_I32_NE:
|
|
|
|
case WASM_OP_I32_LT_S:
|
|
|
|
case WASM_OP_I32_LT_U:
|
|
|
|
case WASM_OP_I32_GT_S:
|
|
|
|
case WASM_OP_I32_GT_U:
|
|
|
|
case WASM_OP_I32_LE_S:
|
|
|
|
case WASM_OP_I32_LE_U:
|
|
|
|
case WASM_OP_I32_GE_S:
|
|
|
|
case WASM_OP_I32_GE_U:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_I64_EQZ:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_I64_EQ:
|
|
|
|
case WASM_OP_I64_NE:
|
|
|
|
case WASM_OP_I64_LT_S:
|
|
|
|
case WASM_OP_I64_LT_U:
|
|
|
|
case WASM_OP_I64_GT_S:
|
|
|
|
case WASM_OP_I64_GT_U:
|
|
|
|
case WASM_OP_I64_LE_S:
|
|
|
|
case WASM_OP_I64_LE_U:
|
|
|
|
case WASM_OP_I64_GE_S:
|
|
|
|
case WASM_OP_I64_GE_U:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_F32_EQ:
|
|
|
|
case WASM_OP_F32_NE:
|
|
|
|
case WASM_OP_F32_LT:
|
|
|
|
case WASM_OP_F32_GT:
|
|
|
|
case WASM_OP_F32_LE:
|
|
|
|
case WASM_OP_F32_GE:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_F64_EQ:
|
|
|
|
case WASM_OP_F64_NE:
|
|
|
|
case WASM_OP_F64_LT:
|
|
|
|
case WASM_OP_F64_GT:
|
|
|
|
case WASM_OP_F64_LE:
|
|
|
|
case WASM_OP_F64_GE:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_I32_CLZ:
|
|
|
|
case WASM_OP_I32_CTZ:
|
|
|
|
case WASM_OP_I32_POPCNT:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_I32_ADD:
|
|
|
|
case WASM_OP_I32_SUB:
|
|
|
|
case WASM_OP_I32_MUL:
|
|
|
|
case WASM_OP_I32_DIV_S:
|
|
|
|
case WASM_OP_I32_DIV_U:
|
|
|
|
case WASM_OP_I32_REM_S:
|
|
|
|
case WASM_OP_I32_REM_U:
|
|
|
|
case WASM_OP_I32_AND:
|
|
|
|
case WASM_OP_I32_OR:
|
|
|
|
case WASM_OP_I32_XOR:
|
|
|
|
case WASM_OP_I32_SHL:
|
|
|
|
case WASM_OP_I32_SHR_S:
|
|
|
|
case WASM_OP_I32_SHR_U:
|
|
|
|
case WASM_OP_I32_ROTL:
|
|
|
|
case WASM_OP_I32_ROTR:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_I64_CLZ:
|
|
|
|
case WASM_OP_I64_CTZ:
|
|
|
|
case WASM_OP_I64_POPCNT:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_I64_ADD:
|
|
|
|
case WASM_OP_I64_SUB:
|
|
|
|
case WASM_OP_I64_MUL:
|
|
|
|
case WASM_OP_I64_DIV_S:
|
|
|
|
case WASM_OP_I64_DIV_U:
|
|
|
|
case WASM_OP_I64_REM_S:
|
|
|
|
case WASM_OP_I64_REM_U:
|
|
|
|
case WASM_OP_I64_AND:
|
|
|
|
case WASM_OP_I64_OR:
|
|
|
|
case WASM_OP_I64_XOR:
|
|
|
|
case WASM_OP_I64_SHL:
|
|
|
|
case WASM_OP_I64_SHR_S:
|
|
|
|
case WASM_OP_I64_SHR_U:
|
|
|
|
case WASM_OP_I64_ROTL:
|
|
|
|
case WASM_OP_I64_ROTR:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_F32_ABS:
|
|
|
|
case WASM_OP_F32_NEG:
|
|
|
|
case WASM_OP_F32_CEIL:
|
|
|
|
case WASM_OP_F32_FLOOR:
|
|
|
|
case WASM_OP_F32_TRUNC:
|
|
|
|
case WASM_OP_F32_NEAREST:
|
|
|
|
case WASM_OP_F32_SQRT:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_F32_ADD:
|
|
|
|
case WASM_OP_F32_SUB:
|
|
|
|
case WASM_OP_F32_MUL:
|
|
|
|
case WASM_OP_F32_DIV:
|
|
|
|
case WASM_OP_F32_MIN:
|
|
|
|
case WASM_OP_F32_MAX:
|
|
|
|
case WASM_OP_F32_COPYSIGN:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_F64_ABS:
|
|
|
|
case WASM_OP_F64_NEG:
|
|
|
|
case WASM_OP_F64_CEIL:
|
|
|
|
case WASM_OP_F64_FLOOR:
|
|
|
|
case WASM_OP_F64_TRUNC:
|
|
|
|
case WASM_OP_F64_NEAREST:
|
|
|
|
case WASM_OP_F64_SQRT:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_F64_ADD:
|
|
|
|
case WASM_OP_F64_SUB:
|
|
|
|
case WASM_OP_F64_MUL:
|
|
|
|
case WASM_OP_F64_DIV:
|
|
|
|
case WASM_OP_F64_MIN:
|
|
|
|
case WASM_OP_F64_MAX:
|
|
|
|
case WASM_OP_F64_COPYSIGN:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_I32_WRAP_I64:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_I32_TRUNC_S_F32:
|
|
|
|
case WASM_OP_I32_TRUNC_U_F32:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_I32_TRUNC_S_F64:
|
|
|
|
case WASM_OP_I32_TRUNC_U_F64:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_I64_EXTEND_S_I32:
|
|
|
|
case WASM_OP_I64_EXTEND_U_I32:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_I64_TRUNC_S_F32:
|
|
|
|
case WASM_OP_I64_TRUNC_U_F32:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_I64_TRUNC_S_F64:
|
|
|
|
case WASM_OP_I64_TRUNC_U_F64:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_F32_CONVERT_S_I32:
|
|
|
|
case WASM_OP_F32_CONVERT_U_I32:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_F32_CONVERT_S_I64:
|
|
|
|
case WASM_OP_F32_CONVERT_U_I64:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_F32_DEMOTE_F64:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_F64_CONVERT_S_I32:
|
|
|
|
case WASM_OP_F64_CONVERT_U_I32:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_F64_CONVERT_S_I64:
|
|
|
|
case WASM_OP_F64_CONVERT_U_I64:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_F64_PROMOTE_F32:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_I32_REINTERPRET_F32:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_I64_REINTERPRET_F64:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_F32_REINTERPRET_I32:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_F64_REINTERPRET_I64:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
|
2019-05-07 02:18:18 +00:00
|
|
|
break;
|
|
|
|
|
2020-04-30 09:52:11 +00:00
|
|
|
case WASM_OP_I32_EXTEND8_S:
|
|
|
|
case WASM_OP_I32_EXTEND16_S:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
|
2020-04-30 09:52:11 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_I64_EXTEND8_S:
|
|
|
|
case WASM_OP_I64_EXTEND16_S:
|
|
|
|
case WASM_OP_I64_EXTEND32_S:
|
2020-05-15 09:44:36 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
|
2020-04-30 09:52:11 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WASM_OP_MISC_PREFIX:
|
|
|
|
{
|
2020-08-20 04:43:12 +00:00
|
|
|
uint32 opcode1;
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, opcode1);
|
2020-04-30 09:52:11 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2020-08-20 04:43:12 +00:00
|
|
|
emit_byte(loader_ctx, ((uint8)opcode1));
|
2020-04-30 09:52:11 +00:00
|
|
|
#endif
|
2021-10-08 03:44:39 +00:00
|
|
|
switch (opcode1) {
|
|
|
|
case WASM_OP_I32_TRUNC_SAT_S_F32:
|
|
|
|
case WASM_OP_I32_TRUNC_SAT_U_F32:
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
|
|
|
|
break;
|
|
|
|
case WASM_OP_I32_TRUNC_SAT_S_F64:
|
|
|
|
case WASM_OP_I32_TRUNC_SAT_U_F64:
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
|
|
|
|
break;
|
|
|
|
case WASM_OP_I64_TRUNC_SAT_S_F32:
|
|
|
|
case WASM_OP_I64_TRUNC_SAT_U_F32:
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
|
|
|
|
break;
|
|
|
|
case WASM_OP_I64_TRUNC_SAT_S_F64:
|
|
|
|
case WASM_OP_I64_TRUNC_SAT_U_F64:
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
|
|
|
|
break;
|
2020-06-02 06:53:06 +00:00
|
|
|
#if WASM_ENABLE_BULK_MEMORY != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
case WASM_OP_MEMORY_INIT:
|
|
|
|
{
|
|
|
|
read_leb_uint32(p, p_end, data_seg_idx);
|
2020-06-02 06:53:06 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
emit_uint32(loader_ctx, data_seg_idx);
|
2020-06-02 06:53:06 +00:00
|
|
|
#endif
|
2021-10-08 03:44:39 +00:00
|
|
|
if (module->import_memory_count == 0
|
|
|
|
&& module->memory_count == 0)
|
|
|
|
goto fail_unknown_memory;
|
2020-06-02 06:53:06 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
if (*p++ != 0x00)
|
|
|
|
goto fail_zero_byte_expected;
|
2020-06-02 06:53:06 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
if (data_seg_idx >= module->data_seg_count) {
|
|
|
|
set_error_buf_v(error_buf, error_buf_size,
|
|
|
|
"unknown data segment %d",
|
|
|
|
data_seg_idx);
|
|
|
|
goto fail;
|
|
|
|
}
|
2020-06-02 06:53:06 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
if (module->data_seg_count1 == 0)
|
|
|
|
goto fail_data_cnt_sec_require;
|
2020-06-02 06:53:06 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
POP_I32();
|
|
|
|
POP_I32();
|
|
|
|
POP_I32();
|
2022-11-14 06:32:35 +00:00
|
|
|
#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
|
|
|
|
func->has_memory_operations = true;
|
|
|
|
#endif
|
2021-10-08 03:44:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WASM_OP_DATA_DROP:
|
|
|
|
{
|
|
|
|
read_leb_uint32(p, p_end, data_seg_idx);
|
2020-06-02 06:53:06 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
emit_uint32(loader_ctx, data_seg_idx);
|
2020-06-02 06:53:06 +00:00
|
|
|
#endif
|
2021-10-08 03:44:39 +00:00
|
|
|
if (data_seg_idx >= module->data_seg_count) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"unknown data segment");
|
|
|
|
goto fail;
|
|
|
|
}
|
2020-06-02 06:53:06 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
if (module->data_seg_count1 == 0)
|
|
|
|
goto fail_data_cnt_sec_require;
|
2020-06-02 06:53:06 +00:00
|
|
|
|
2022-11-14 06:32:35 +00:00
|
|
|
#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
|
|
|
|
func->has_memory_operations = true;
|
|
|
|
#endif
|
2021-10-08 03:44:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WASM_OP_MEMORY_COPY:
|
|
|
|
{
|
|
|
|
/* both src and dst memory index should be 0 */
|
|
|
|
if (*(int16 *)p != 0x0000)
|
|
|
|
goto fail_zero_byte_expected;
|
|
|
|
p += 2;
|
2020-06-02 06:53:06 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
if (module->import_memory_count == 0
|
|
|
|
&& module->memory_count == 0)
|
|
|
|
goto fail_unknown_memory;
|
2020-06-02 06:53:06 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
POP_I32();
|
|
|
|
POP_I32();
|
|
|
|
POP_I32();
|
2022-11-14 06:32:35 +00:00
|
|
|
#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
|
|
|
|
func->has_memory_operations = true;
|
|
|
|
#endif
|
2021-10-08 03:44:39 +00:00
|
|
|
break;
|
2020-06-02 06:53:06 +00:00
|
|
|
}
|
2021-10-08 03:44:39 +00:00
|
|
|
case WASM_OP_MEMORY_FILL:
|
|
|
|
{
|
|
|
|
if (*p++ != 0x00) {
|
|
|
|
goto fail_zero_byte_expected;
|
|
|
|
}
|
|
|
|
if (module->import_memory_count == 0
|
|
|
|
&& module->memory_count == 0) {
|
|
|
|
goto fail_unknown_memory;
|
|
|
|
}
|
2020-06-02 06:53:06 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
POP_I32();
|
|
|
|
POP_I32();
|
|
|
|
POP_I32();
|
2022-11-14 06:32:35 +00:00
|
|
|
#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
|
|
|
|
func->has_memory_operations = true;
|
|
|
|
#endif
|
2021-10-08 03:44:39 +00:00
|
|
|
break;
|
2022-11-14 06:32:35 +00:00
|
|
|
}
|
2021-10-08 03:44:39 +00:00
|
|
|
fail_zero_byte_expected:
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"zero byte expected");
|
|
|
|
goto fail;
|
2020-06-02 06:53:06 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
fail_unknown_memory:
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"unknown memory 0");
|
|
|
|
goto fail;
|
|
|
|
fail_data_cnt_sec_require:
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"data count section required");
|
|
|
|
goto fail;
|
2020-06-02 06:53:06 +00:00
|
|
|
#endif /* WASM_ENABLE_BULK_MEMORY */
|
2021-04-15 03:29:20 +00:00
|
|
|
#if WASM_ENABLE_REF_TYPES != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
case WASM_OP_TABLE_INIT:
|
|
|
|
{
|
|
|
|
uint8 seg_ref_type = 0, tbl_ref_type = 0;
|
2021-04-15 03:29:20 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
read_leb_uint32(p, p_end, table_seg_idx);
|
|
|
|
read_leb_uint32(p, p_end, table_idx);
|
2021-04-15 03:29:20 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!get_table_elem_type(module, table_idx,
|
|
|
|
&tbl_ref_type, error_buf,
|
2021-04-15 03:29:20 +00:00
|
|
|
error_buf_size))
|
2021-10-08 03:44:39 +00:00
|
|
|
goto fail;
|
2021-04-15 03:29:20 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!get_table_seg_elem_type(module, table_seg_idx,
|
|
|
|
&seg_ref_type, error_buf,
|
|
|
|
error_buf_size))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (seg_ref_type != tbl_ref_type) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"type mismatch");
|
|
|
|
goto fail;
|
|
|
|
}
|
2021-04-15 03:29:20 +00:00
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
emit_uint32(loader_ctx, table_seg_idx);
|
|
|
|
emit_uint32(loader_ctx, table_idx);
|
2021-04-15 03:29:20 +00:00
|
|
|
#endif
|
2021-10-08 03:44:39 +00:00
|
|
|
POP_I32();
|
|
|
|
POP_I32();
|
|
|
|
POP_I32();
|
|
|
|
break;
|
2021-04-15 03:29:20 +00:00
|
|
|
}
|
2021-10-08 03:44:39 +00:00
|
|
|
case WASM_OP_ELEM_DROP:
|
|
|
|
{
|
|
|
|
read_leb_uint32(p, p_end, table_seg_idx);
|
|
|
|
if (!get_table_seg_elem_type(module, table_seg_idx,
|
|
|
|
NULL, error_buf,
|
|
|
|
error_buf_size))
|
|
|
|
goto fail;
|
2021-04-15 03:29:20 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
emit_uint32(loader_ctx, table_seg_idx);
|
2021-04-15 03:29:20 +00:00
|
|
|
#endif
|
2021-10-08 03:44:39 +00:00
|
|
|
break;
|
2021-04-15 03:29:20 +00:00
|
|
|
}
|
2021-10-08 03:44:39 +00:00
|
|
|
case WASM_OP_TABLE_COPY:
|
|
|
|
{
|
|
|
|
uint8 src_ref_type, dst_ref_type;
|
|
|
|
uint32 src_tbl_idx, dst_tbl_idx;
|
2021-04-15 03:29:20 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
read_leb_uint32(p, p_end, src_tbl_idx);
|
|
|
|
if (!get_table_elem_type(module, src_tbl_idx,
|
|
|
|
&src_ref_type, error_buf,
|
|
|
|
error_buf_size))
|
|
|
|
goto fail;
|
2021-04-15 03:29:20 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
read_leb_uint32(p, p_end, dst_tbl_idx);
|
|
|
|
if (!get_table_elem_type(module, dst_tbl_idx,
|
|
|
|
&dst_ref_type, error_buf,
|
|
|
|
error_buf_size))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (src_ref_type != dst_ref_type) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"type mismatch");
|
|
|
|
goto fail;
|
|
|
|
}
|
2021-04-15 03:29:20 +00:00
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
emit_uint32(loader_ctx, src_tbl_idx);
|
|
|
|
emit_uint32(loader_ctx, dst_tbl_idx);
|
2021-04-15 03:29:20 +00:00
|
|
|
#endif
|
2021-10-08 03:44:39 +00:00
|
|
|
POP_I32();
|
|
|
|
POP_I32();
|
|
|
|
POP_I32();
|
|
|
|
break;
|
2021-04-15 03:29:20 +00:00
|
|
|
}
|
2021-10-08 03:44:39 +00:00
|
|
|
case WASM_OP_TABLE_SIZE:
|
|
|
|
{
|
|
|
|
read_leb_uint32(p, p_end, table_idx);
|
|
|
|
/* TODO: shall we create a new function to check
|
|
|
|
table idx instead of using below function? */
|
|
|
|
if (!get_table_elem_type(module, table_idx, NULL,
|
|
|
|
error_buf, error_buf_size))
|
|
|
|
goto fail;
|
2021-04-15 03:29:20 +00:00
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
emit_uint32(loader_ctx, table_idx);
|
2021-04-15 03:29:20 +00:00
|
|
|
#endif
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
PUSH_I32();
|
|
|
|
break;
|
2021-04-15 03:29:20 +00:00
|
|
|
}
|
2021-10-08 03:44:39 +00:00
|
|
|
case WASM_OP_TABLE_GROW:
|
|
|
|
case WASM_OP_TABLE_FILL:
|
|
|
|
{
|
|
|
|
uint8 decl_ref_type;
|
2021-04-15 03:29:20 +00:00
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
read_leb_uint32(p, p_end, table_idx);
|
|
|
|
if (!get_table_elem_type(module, table_idx,
|
|
|
|
&decl_ref_type, error_buf,
|
|
|
|
error_buf_size))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (opcode1 == WASM_OP_TABLE_GROW) {
|
|
|
|
if (table_idx < module->import_table_count) {
|
|
|
|
module->import_tables[table_idx]
|
|
|
|
.u.table.possible_grow = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
module
|
|
|
|
->tables[table_idx
|
|
|
|
- module->import_table_count]
|
|
|
|
.possible_grow = true;
|
|
|
|
}
|
2021-04-15 03:29:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
emit_uint32(loader_ctx, table_idx);
|
2021-04-15 03:29:20 +00:00
|
|
|
#endif
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
POP_I32();
|
2021-04-15 03:29:20 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2021-10-08 03:44:39 +00:00
|
|
|
POP_OFFSET_TYPE(decl_ref_type);
|
2021-04-15 03:29:20 +00:00
|
|
|
#endif
|
2021-10-08 03:44:39 +00:00
|
|
|
POP_TYPE(decl_ref_type);
|
|
|
|
if (opcode1 == WASM_OP_TABLE_GROW)
|
|
|
|
PUSH_I32();
|
|
|
|
else
|
|
|
|
POP_I32();
|
|
|
|
break;
|
|
|
|
}
|
2021-04-15 03:29:20 +00:00
|
|
|
#endif /* WASM_ENABLE_REF_TYPES */
|
2021-10-08 03:44:39 +00:00
|
|
|
default:
|
|
|
|
set_error_buf_v(error_buf, error_buf_size,
|
|
|
|
"%s %02x %02x", "unsupported opcode",
|
|
|
|
0xfc, opcode1);
|
|
|
|
goto fail;
|
2020-04-30 09:52:11 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-11-05 10:15:15 +00:00
|
|
|
|
|
|
|
#if WASM_ENABLE_SIMD != 0
|
|
|
|
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
|
|
|
|
case WASM_OP_SIMD_PREFIX:
|
|
|
|
{
|
2023-06-16 02:00:24 +00:00
|
|
|
uint32 opcode1;
|
|
|
|
|
2023-11-09 00:42:05 +00:00
|
|
|
CHECK_BUF(p, p_end, 1);
|
2023-06-16 02:00:24 +00:00
|
|
|
opcode1 = read_uint8(p);
|
2021-10-08 03:44:39 +00:00
|
|
|
/* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
|
|
|
|
*/
|
2023-06-16 02:00:24 +00:00
|
|
|
switch (opcode1) {
|
2021-09-17 11:12:57 +00:00
|
|
|
/* memory instruction */
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_v128_load:
|
2021-09-17 11:12:57 +00:00
|
|
|
case SIMD_v128_load8x8_s:
|
|
|
|
case SIMD_v128_load8x8_u:
|
|
|
|
case SIMD_v128_load16x4_s:
|
|
|
|
case SIMD_v128_load16x4_u:
|
|
|
|
case SIMD_v128_load32x2_s:
|
|
|
|
case SIMD_v128_load32x2_u:
|
|
|
|
case SIMD_v128_load8_splat:
|
|
|
|
case SIMD_v128_load16_splat:
|
|
|
|
case SIMD_v128_load32_splat:
|
|
|
|
case SIMD_v128_load64_splat:
|
2020-11-05 10:15:15 +00:00
|
|
|
{
|
|
|
|
CHECK_MEMORY();
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, align); /* align */
|
|
|
|
if (!check_simd_memory_access_align(
|
2023-06-16 02:00:24 +00:00
|
|
|
opcode1, align, error_buf, error_buf_size)) {
|
2020-11-05 10:15:15 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, mem_offset); /* offset */
|
|
|
|
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
|
2022-11-14 06:32:35 +00:00
|
|
|
#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
|
|
|
|
func->has_memory_operations = true;
|
|
|
|
#endif
|
2020-11-05 10:15:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_v128_store:
|
|
|
|
{
|
|
|
|
CHECK_MEMORY();
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, align); /* align */
|
|
|
|
if (!check_simd_memory_access_align(
|
2023-06-16 02:00:24 +00:00
|
|
|
opcode1, align, error_buf, error_buf_size)) {
|
2020-11-05 10:15:15 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, mem_offset); /* offset */
|
|
|
|
|
|
|
|
POP_V128();
|
|
|
|
POP_I32();
|
2022-11-14 06:32:35 +00:00
|
|
|
#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
|
|
|
|
func->has_memory_operations = true;
|
|
|
|
#endif
|
2020-11-05 10:15:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-09-17 11:12:57 +00:00
|
|
|
/* basic operation */
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_v128_const:
|
2021-09-17 11:12:57 +00:00
|
|
|
{
|
2020-11-05 10:15:15 +00:00
|
|
|
CHECK_BUF1(p, p_end, 16);
|
|
|
|
p += 16;
|
|
|
|
PUSH_V128();
|
|
|
|
break;
|
2021-09-17 11:12:57 +00:00
|
|
|
}
|
2020-11-05 10:15:15 +00:00
|
|
|
|
|
|
|
case SIMD_v8x16_shuffle:
|
|
|
|
{
|
|
|
|
V128 mask;
|
|
|
|
|
|
|
|
CHECK_BUF1(p, p_end, 16);
|
|
|
|
mask = read_i8x16(p, error_buf, error_buf_size);
|
|
|
|
p += 16;
|
|
|
|
if (!check_simd_shuffle_mask(mask, error_buf,
|
|
|
|
error_buf_size)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_v8x16_swizzle:
|
2021-09-17 11:12:57 +00:00
|
|
|
{
|
2020-11-05 10:15:15 +00:00
|
|
|
POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
2021-09-17 11:12:57 +00:00
|
|
|
}
|
2020-11-05 10:15:15 +00:00
|
|
|
|
2021-09-17 11:12:57 +00:00
|
|
|
/* splat operation */
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_i8x16_splat:
|
|
|
|
case SIMD_i16x8_splat:
|
|
|
|
case SIMD_i32x4_splat:
|
|
|
|
case SIMD_i64x2_splat:
|
|
|
|
case SIMD_f32x4_splat:
|
|
|
|
case SIMD_f64x2_splat:
|
2021-09-17 11:12:57 +00:00
|
|
|
{
|
|
|
|
uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
|
|
|
|
VALUE_TYPE_I32, VALUE_TYPE_I64,
|
|
|
|
VALUE_TYPE_F32, VALUE_TYPE_F64 };
|
2023-06-16 02:00:24 +00:00
|
|
|
POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
|
2021-09-17 11:12:57 +00:00
|
|
|
VALUE_TYPE_V128);
|
2020-11-05 10:15:15 +00:00
|
|
|
break;
|
2021-09-17 11:12:57 +00:00
|
|
|
}
|
2020-11-05 10:15:15 +00:00
|
|
|
|
2021-09-17 11:12:57 +00:00
|
|
|
/* lane operation */
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_i8x16_extract_lane_s:
|
|
|
|
case SIMD_i8x16_extract_lane_u:
|
2021-09-17 11:12:57 +00:00
|
|
|
case SIMD_i8x16_replace_lane:
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_i16x8_extract_lane_s:
|
|
|
|
case SIMD_i16x8_extract_lane_u:
|
2021-09-17 11:12:57 +00:00
|
|
|
case SIMD_i16x8_replace_lane:
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_i32x4_extract_lane:
|
2021-09-17 11:12:57 +00:00
|
|
|
case SIMD_i32x4_replace_lane:
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_i64x2_extract_lane:
|
2021-09-17 11:12:57 +00:00
|
|
|
case SIMD_i64x2_replace_lane:
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_f32x4_extract_lane:
|
2021-09-17 11:12:57 +00:00
|
|
|
case SIMD_f32x4_replace_lane:
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_f64x2_extract_lane:
|
2021-09-17 11:12:57 +00:00
|
|
|
case SIMD_f64x2_replace_lane:
|
|
|
|
{
|
|
|
|
uint8 lane;
|
|
|
|
/* clang-format off */
|
|
|
|
uint8 replace[] = {
|
|
|
|
/*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
|
|
|
|
/*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
|
|
|
|
/*i32x4*/ 0x0, VALUE_TYPE_I32,
|
|
|
|
/*i64x2*/ 0x0, VALUE_TYPE_I64,
|
|
|
|
/*f32x4*/ 0x0, VALUE_TYPE_F32,
|
|
|
|
/*f64x2*/ 0x0, VALUE_TYPE_F64,
|
|
|
|
};
|
|
|
|
uint8 push_type[] = {
|
|
|
|
/*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
|
|
|
|
VALUE_TYPE_V128,
|
|
|
|
/*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
|
|
|
|
VALUE_TYPE_V128,
|
|
|
|
/*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
|
|
|
|
/*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
|
|
|
|
/*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
|
|
|
|
/*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
|
|
|
|
};
|
|
|
|
/* clang-format on */
|
2020-11-05 10:15:15 +00:00
|
|
|
|
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
lane = read_uint8(p);
|
2023-06-16 02:00:24 +00:00
|
|
|
if (!check_simd_access_lane(opcode1, lane, error_buf,
|
2020-11-05 10:15:15 +00:00
|
|
|
error_buf_size)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2023-06-16 02:00:24 +00:00
|
|
|
if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
|
2021-09-17 11:12:57 +00:00
|
|
|
if (!(wasm_loader_pop_frame_ref(
|
2021-10-08 03:44:39 +00:00
|
|
|
loader_ctx,
|
2023-06-16 02:00:24 +00:00
|
|
|
replace[opcode1
|
|
|
|
- SIMD_i8x16_extract_lane_s],
|
2021-10-08 03:44:39 +00:00
|
|
|
error_buf, error_buf_size)))
|
2021-09-17 11:12:57 +00:00
|
|
|
goto fail;
|
2020-11-05 10:15:15 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 11:12:57 +00:00
|
|
|
POP_AND_PUSH(
|
2021-10-08 03:44:39 +00:00
|
|
|
VALUE_TYPE_V128,
|
2023-06-16 02:00:24 +00:00
|
|
|
push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
|
2020-11-05 10:15:15 +00:00
|
|
|
break;
|
2021-09-17 11:12:57 +00:00
|
|
|
}
|
2020-11-05 10:15:15 +00:00
|
|
|
|
2021-09-17 11:12:57 +00:00
|
|
|
/* i8x16 compare operation */
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_i8x16_eq:
|
|
|
|
case SIMD_i8x16_ne:
|
|
|
|
case SIMD_i8x16_lt_s:
|
|
|
|
case SIMD_i8x16_lt_u:
|
|
|
|
case SIMD_i8x16_gt_s:
|
|
|
|
case SIMD_i8x16_gt_u:
|
|
|
|
case SIMD_i8x16_le_s:
|
|
|
|
case SIMD_i8x16_le_u:
|
|
|
|
case SIMD_i8x16_ge_s:
|
|
|
|
case SIMD_i8x16_ge_u:
|
2021-09-17 11:12:57 +00:00
|
|
|
/* i16x8 compare operation */
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_i16x8_eq:
|
|
|
|
case SIMD_i16x8_ne:
|
|
|
|
case SIMD_i16x8_lt_s:
|
|
|
|
case SIMD_i16x8_lt_u:
|
|
|
|
case SIMD_i16x8_gt_s:
|
|
|
|
case SIMD_i16x8_gt_u:
|
|
|
|
case SIMD_i16x8_le_s:
|
|
|
|
case SIMD_i16x8_le_u:
|
|
|
|
case SIMD_i16x8_ge_s:
|
|
|
|
case SIMD_i16x8_ge_u:
|
2021-09-17 11:12:57 +00:00
|
|
|
/* i32x4 compare operation */
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_i32x4_eq:
|
|
|
|
case SIMD_i32x4_ne:
|
|
|
|
case SIMD_i32x4_lt_s:
|
|
|
|
case SIMD_i32x4_lt_u:
|
|
|
|
case SIMD_i32x4_gt_s:
|
|
|
|
case SIMD_i32x4_gt_u:
|
|
|
|
case SIMD_i32x4_le_s:
|
|
|
|
case SIMD_i32x4_le_u:
|
|
|
|
case SIMD_i32x4_ge_s:
|
|
|
|
case SIMD_i32x4_ge_u:
|
2021-09-17 11:12:57 +00:00
|
|
|
/* f32x4 compare operation */
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_f32x4_eq:
|
|
|
|
case SIMD_f32x4_ne:
|
|
|
|
case SIMD_f32x4_lt:
|
|
|
|
case SIMD_f32x4_gt:
|
|
|
|
case SIMD_f32x4_le:
|
|
|
|
case SIMD_f32x4_ge:
|
2021-09-17 11:12:57 +00:00
|
|
|
/* f64x2 compare operation */
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_f64x2_eq:
|
|
|
|
case SIMD_f64x2_ne:
|
|
|
|
case SIMD_f64x2_lt:
|
|
|
|
case SIMD_f64x2_gt:
|
|
|
|
case SIMD_f64x2_le:
|
|
|
|
case SIMD_f64x2_ge:
|
2021-09-17 11:12:57 +00:00
|
|
|
{
|
2020-11-05 10:15:15 +00:00
|
|
|
POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
2021-09-17 11:12:57 +00:00
|
|
|
}
|
2020-11-05 10:15:15 +00:00
|
|
|
|
2021-09-17 11:12:57 +00:00
|
|
|
/* v128 operation */
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_v128_not:
|
2021-09-17 11:12:57 +00:00
|
|
|
{
|
2020-11-05 10:15:15 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
2021-09-17 11:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_v128_and:
|
|
|
|
case SIMD_v128_andnot:
|
|
|
|
case SIMD_v128_or:
|
|
|
|
case SIMD_v128_xor:
|
|
|
|
{
|
|
|
|
POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
2020-11-05 10:15:15 +00:00
|
|
|
|
|
|
|
case SIMD_v128_bitselect:
|
2021-09-17 11:12:57 +00:00
|
|
|
{
|
2020-11-05 10:15:15 +00:00
|
|
|
POP_V128();
|
|
|
|
POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
2021-09-17 11:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_v128_any_true:
|
|
|
|
{
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load Lane Operation */
|
|
|
|
case SIMD_v128_load8_lane:
|
|
|
|
case SIMD_v128_load16_lane:
|
|
|
|
case SIMD_v128_load32_lane:
|
|
|
|
case SIMD_v128_load64_lane:
|
|
|
|
case SIMD_v128_store8_lane:
|
|
|
|
case SIMD_v128_store16_lane:
|
|
|
|
case SIMD_v128_store32_lane:
|
|
|
|
case SIMD_v128_store64_lane:
|
|
|
|
{
|
|
|
|
uint8 lane;
|
|
|
|
|
|
|
|
CHECK_MEMORY();
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, align); /* align */
|
|
|
|
if (!check_simd_memory_access_align(
|
2023-06-16 02:00:24 +00:00
|
|
|
opcode1, align, error_buf, error_buf_size)) {
|
2021-09-17 11:12:57 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, mem_offset); /* offset */
|
|
|
|
|
|
|
|
CHECK_BUF(p, p_end, 1);
|
|
|
|
lane = read_uint8(p);
|
2023-06-16 02:00:24 +00:00
|
|
|
if (!check_simd_access_lane(opcode1, lane, error_buf,
|
2021-09-17 11:12:57 +00:00
|
|
|
error_buf_size)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
POP_V128();
|
|
|
|
POP_I32();
|
2023-06-16 02:00:24 +00:00
|
|
|
if (opcode1 < SIMD_v128_store8_lane) {
|
2021-09-17 11:12:57 +00:00
|
|
|
PUSH_V128();
|
|
|
|
}
|
2022-11-14 06:32:35 +00:00
|
|
|
#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
|
|
|
|
func->has_memory_operations = true;
|
|
|
|
#endif
|
2021-09-17 11:12:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_v128_load32_zero:
|
|
|
|
case SIMD_v128_load64_zero:
|
|
|
|
{
|
|
|
|
CHECK_MEMORY();
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, align); /* align */
|
|
|
|
if (!check_simd_memory_access_align(
|
2023-06-16 02:00:24 +00:00
|
|
|
opcode1, align, error_buf, error_buf_size)) {
|
2021-09-17 11:12:57 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
read_leb_uint32(p, p_end, mem_offset); /* offset */
|
|
|
|
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
|
2022-11-14 06:32:35 +00:00
|
|
|
#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
|
|
|
|
func->has_memory_operations = true;
|
|
|
|
#endif
|
2021-09-17 11:12:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Float conversion */
|
|
|
|
case SIMD_f32x4_demote_f64x2_zero:
|
|
|
|
case SIMD_f64x2_promote_low_f32x4_zero:
|
|
|
|
{
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* i8x16 Operation */
|
|
|
|
case SIMD_i8x16_abs:
|
|
|
|
case SIMD_i8x16_neg:
|
|
|
|
case SIMD_i8x16_popcnt:
|
|
|
|
{
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
2020-11-05 10:15:15 +00:00
|
|
|
|
|
|
|
case SIMD_i8x16_all_true:
|
|
|
|
case SIMD_i8x16_bitmask:
|
2021-09-17 11:12:57 +00:00
|
|
|
{
|
2020-11-05 10:15:15 +00:00
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
|
|
|
|
break;
|
2021-09-17 11:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_i8x16_narrow_i16x8_s:
|
|
|
|
case SIMD_i8x16_narrow_i16x8_u:
|
|
|
|
{
|
|
|
|
POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_f32x4_ceil:
|
|
|
|
case SIMD_f32x4_floor:
|
|
|
|
case SIMD_f32x4_trunc:
|
|
|
|
case SIMD_f32x4_nearest:
|
|
|
|
{
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
2020-11-05 10:15:15 +00:00
|
|
|
|
|
|
|
case SIMD_i8x16_shl:
|
|
|
|
case SIMD_i8x16_shr_s:
|
|
|
|
case SIMD_i8x16_shr_u:
|
2021-09-17 11:12:57 +00:00
|
|
|
{
|
2020-11-05 10:15:15 +00:00
|
|
|
POP_I32();
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
2021-09-17 11:12:57 +00:00
|
|
|
}
|
2020-11-05 10:15:15 +00:00
|
|
|
|
|
|
|
case SIMD_i8x16_add:
|
2021-09-17 11:12:57 +00:00
|
|
|
case SIMD_i8x16_add_sat_s:
|
|
|
|
case SIMD_i8x16_add_sat_u:
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_i8x16_sub:
|
2021-09-17 11:12:57 +00:00
|
|
|
case SIMD_i8x16_sub_sat_s:
|
|
|
|
case SIMD_i8x16_sub_sat_u:
|
|
|
|
{
|
|
|
|
POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_f64x2_ceil:
|
|
|
|
case SIMD_f64x2_floor:
|
|
|
|
{
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_i8x16_min_s:
|
|
|
|
case SIMD_i8x16_min_u:
|
|
|
|
case SIMD_i8x16_max_s:
|
|
|
|
case SIMD_i8x16_max_u:
|
2021-09-17 11:12:57 +00:00
|
|
|
{
|
|
|
|
POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_f64x2_trunc:
|
|
|
|
{
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_i8x16_avgr_u:
|
2021-09-17 11:12:57 +00:00
|
|
|
{
|
|
|
|
POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_i16x8_extadd_pairwise_i8x16_s:
|
|
|
|
case SIMD_i16x8_extadd_pairwise_i8x16_u:
|
|
|
|
case SIMD_i32x4_extadd_pairwise_i16x8_s:
|
|
|
|
case SIMD_i32x4_extadd_pairwise_i16x8_u:
|
|
|
|
/* i16x8 operation */
|
|
|
|
case SIMD_i16x8_abs:
|
|
|
|
case SIMD_i16x8_neg:
|
|
|
|
{
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_i16x8_q15mulr_sat_s:
|
|
|
|
{
|
|
|
|
POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_i16x8_all_true:
|
|
|
|
case SIMD_i16x8_bitmask:
|
|
|
|
{
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_i16x8_narrow_i32x4_s:
|
|
|
|
case SIMD_i16x8_narrow_i32x4_u:
|
|
|
|
{
|
|
|
|
POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_i16x8_extend_low_i8x16_s:
|
|
|
|
case SIMD_i16x8_extend_high_i8x16_s:
|
|
|
|
case SIMD_i16x8_extend_low_i8x16_u:
|
|
|
|
case SIMD_i16x8_extend_high_i8x16_u:
|
|
|
|
{
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_i16x8_shl:
|
|
|
|
case SIMD_i16x8_shr_s:
|
|
|
|
case SIMD_i16x8_shr_u:
|
|
|
|
{
|
|
|
|
POP_I32();
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_i16x8_add:
|
2021-09-17 11:12:57 +00:00
|
|
|
case SIMD_i16x8_add_sat_s:
|
|
|
|
case SIMD_i16x8_add_sat_u:
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_i16x8_sub:
|
2021-09-17 11:12:57 +00:00
|
|
|
case SIMD_i16x8_sub_sat_s:
|
|
|
|
case SIMD_i16x8_sub_sat_u:
|
|
|
|
{
|
|
|
|
POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_f64x2_nearest:
|
|
|
|
{
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_i16x8_mul:
|
|
|
|
case SIMD_i16x8_min_s:
|
|
|
|
case SIMD_i16x8_min_u:
|
|
|
|
case SIMD_i16x8_max_s:
|
|
|
|
case SIMD_i16x8_max_u:
|
|
|
|
case SIMD_i16x8_avgr_u:
|
2021-09-17 11:12:57 +00:00
|
|
|
case SIMD_i16x8_extmul_low_i8x16_s:
|
|
|
|
case SIMD_i16x8_extmul_high_i8x16_s:
|
|
|
|
case SIMD_i16x8_extmul_low_i8x16_u:
|
|
|
|
case SIMD_i16x8_extmul_high_i8x16_u:
|
|
|
|
{
|
|
|
|
POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* i32x4 operation */
|
|
|
|
case SIMD_i32x4_abs:
|
|
|
|
case SIMD_i32x4_neg:
|
|
|
|
{
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_i32x4_all_true:
|
|
|
|
case SIMD_i32x4_bitmask:
|
|
|
|
{
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_i32x4_narrow_i64x2_s:
|
|
|
|
case SIMD_i32x4_narrow_i64x2_u:
|
|
|
|
{
|
|
|
|
POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_i32x4_extend_low_i16x8_s:
|
|
|
|
case SIMD_i32x4_extend_high_i16x8_s:
|
|
|
|
case SIMD_i32x4_extend_low_i16x8_u:
|
|
|
|
case SIMD_i32x4_extend_high_i16x8_u:
|
|
|
|
{
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_i32x4_shl:
|
|
|
|
case SIMD_i32x4_shr_s:
|
|
|
|
case SIMD_i32x4_shr_u:
|
|
|
|
{
|
|
|
|
POP_I32();
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_i32x4_add:
|
|
|
|
case SIMD_i32x4_sub:
|
|
|
|
case SIMD_i32x4_mul:
|
|
|
|
case SIMD_i32x4_min_s:
|
|
|
|
case SIMD_i32x4_min_u:
|
|
|
|
case SIMD_i32x4_max_s:
|
|
|
|
case SIMD_i32x4_max_u:
|
2021-09-17 11:12:57 +00:00
|
|
|
case SIMD_i32x4_dot_i16x8_s:
|
|
|
|
case SIMD_i32x4_avgr_u:
|
|
|
|
case SIMD_i32x4_extmul_low_i16x8_s:
|
|
|
|
case SIMD_i32x4_extmul_high_i16x8_s:
|
|
|
|
case SIMD_i32x4_extmul_low_i16x8_u:
|
|
|
|
case SIMD_i32x4_extmul_high_i16x8_u:
|
|
|
|
{
|
|
|
|
POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* i64x2 operation */
|
|
|
|
case SIMD_i64x2_abs:
|
|
|
|
case SIMD_i64x2_neg:
|
|
|
|
{
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_i64x2_all_true:
|
|
|
|
case SIMD_i64x2_bitmask:
|
|
|
|
{
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_i64x2_extend_low_i32x4_s:
|
|
|
|
case SIMD_i64x2_extend_high_i32x4_s:
|
|
|
|
case SIMD_i64x2_extend_low_i32x4_u:
|
|
|
|
case SIMD_i64x2_extend_high_i32x4_u:
|
|
|
|
{
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_i64x2_shl:
|
|
|
|
case SIMD_i64x2_shr_s:
|
|
|
|
case SIMD_i64x2_shr_u:
|
|
|
|
{
|
|
|
|
POP_I32();
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_i64x2_add:
|
|
|
|
case SIMD_i64x2_sub:
|
|
|
|
case SIMD_i64x2_mul:
|
2021-09-17 11:12:57 +00:00
|
|
|
case SIMD_i64x2_eq:
|
|
|
|
case SIMD_i64x2_ne:
|
|
|
|
case SIMD_i64x2_lt_s:
|
|
|
|
case SIMD_i64x2_gt_s:
|
|
|
|
case SIMD_i64x2_le_s:
|
|
|
|
case SIMD_i64x2_ge_s:
|
|
|
|
case SIMD_i64x2_extmul_low_i32x4_s:
|
|
|
|
case SIMD_i64x2_extmul_high_i32x4_s:
|
|
|
|
case SIMD_i64x2_extmul_low_i32x4_u:
|
|
|
|
case SIMD_i64x2_extmul_high_i32x4_u:
|
|
|
|
{
|
|
|
|
POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* f32x4 operation */
|
|
|
|
case SIMD_f32x4_abs:
|
|
|
|
case SIMD_f32x4_neg:
|
|
|
|
case SIMD_f32x4_round:
|
|
|
|
case SIMD_f32x4_sqrt:
|
|
|
|
{
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_f32x4_add:
|
|
|
|
case SIMD_f32x4_sub:
|
|
|
|
case SIMD_f32x4_mul:
|
|
|
|
case SIMD_f32x4_div:
|
|
|
|
case SIMD_f32x4_min:
|
|
|
|
case SIMD_f32x4_max:
|
2021-09-17 11:12:57 +00:00
|
|
|
case SIMD_f32x4_pmin:
|
|
|
|
case SIMD_f32x4_pmax:
|
|
|
|
{
|
|
|
|
POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* f64x2 operation */
|
|
|
|
case SIMD_f64x2_abs:
|
|
|
|
case SIMD_f64x2_neg:
|
|
|
|
case SIMD_f64x2_round:
|
|
|
|
case SIMD_f64x2_sqrt:
|
|
|
|
{
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-11-05 10:15:15 +00:00
|
|
|
case SIMD_f64x2_add:
|
|
|
|
case SIMD_f64x2_sub:
|
|
|
|
case SIMD_f64x2_mul:
|
|
|
|
case SIMD_f64x2_div:
|
|
|
|
case SIMD_f64x2_min:
|
|
|
|
case SIMD_f64x2_max:
|
2021-09-17 11:12:57 +00:00
|
|
|
case SIMD_f64x2_pmin:
|
|
|
|
case SIMD_f64x2_pmax:
|
|
|
|
{
|
2020-11-05 10:15:15 +00:00
|
|
|
POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
2021-09-17 11:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case SIMD_i32x4_trunc_sat_f32x4_s:
|
|
|
|
case SIMD_i32x4_trunc_sat_f32x4_u:
|
|
|
|
case SIMD_f32x4_convert_i32x4_s:
|
|
|
|
case SIMD_f32x4_convert_i32x4_u:
|
|
|
|
case SIMD_i32x4_trunc_sat_f64x2_s_zero:
|
|
|
|
case SIMD_i32x4_trunc_sat_f64x2_u_zero:
|
|
|
|
case SIMD_f64x2_convert_low_i32x4_s:
|
|
|
|
case SIMD_f64x2_convert_low_i32x4_u:
|
|
|
|
{
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
|
|
|
break;
|
|
|
|
}
|
2020-11-05 10:15:15 +00:00
|
|
|
|
|
|
|
default:
|
2021-09-17 11:12:57 +00:00
|
|
|
{
|
2020-11-05 10:15:15 +00:00
|
|
|
if (error_buf != NULL) {
|
|
|
|
snprintf(error_buf, error_buf_size,
|
2021-09-17 11:12:57 +00:00
|
|
|
"WASM module load failed: "
|
|
|
|
"invalid opcode 0xfd %02x.",
|
2023-06-16 02:00:24 +00:00
|
|
|
opcode1);
|
2020-11-05 10:15:15 +00:00
|
|
|
}
|
|
|
|
goto fail;
|
2021-09-17 11:12:57 +00:00
|
|
|
}
|
2020-11-05 10:15:15 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
|
|
|
|
#endif /* end of WASM_ENABLE_SIMD */
|
|
|
|
|
2020-08-03 03:30:26 +00:00
|
|
|
#if WASM_ENABLE_SHARED_MEMORY != 0
|
|
|
|
case WASM_OP_ATOMIC_PREFIX:
|
|
|
|
{
|
2023-06-16 02:00:24 +00:00
|
|
|
uint32 opcode1;
|
|
|
|
|
2023-11-09 00:42:05 +00:00
|
|
|
CHECK_BUF(p, p_end, 1);
|
2023-06-16 02:00:24 +00:00
|
|
|
opcode1 = read_uint8(p);
|
2020-08-03 03:30:26 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
2023-06-16 02:00:24 +00:00
|
|
|
emit_byte(loader_ctx, opcode1);
|
2020-08-03 03:30:26 +00:00
|
|
|
#endif
|
2023-06-16 02:00:24 +00:00
|
|
|
if (opcode1 != WASM_OP_ATOMIC_FENCE) {
|
2020-08-03 03:30:26 +00:00
|
|
|
CHECK_MEMORY();
|
2021-10-08 03:44:39 +00:00
|
|
|
read_leb_uint32(p, p_end, align); /* align */
|
2020-08-03 03:30:26 +00:00
|
|
|
read_leb_uint32(p, p_end, mem_offset); /* offset */
|
2023-06-16 02:00:24 +00:00
|
|
|
if (!check_memory_align_equal(opcode1, align, error_buf,
|
2020-08-03 03:30:26 +00:00
|
|
|
error_buf_size)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2020-08-10 11:43:58 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
emit_uint32(loader_ctx, mem_offset);
|
|
|
|
#endif
|
2020-08-03 03:30:26 +00:00
|
|
|
}
|
2022-11-14 06:32:35 +00:00
|
|
|
#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
|
|
|
|
func->has_memory_operations = true;
|
|
|
|
#endif
|
2023-06-16 02:00:24 +00:00
|
|
|
switch (opcode1) {
|
2020-08-03 03:30:26 +00:00
|
|
|
case WASM_OP_ATOMIC_NOTIFY:
|
|
|
|
POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
|
|
|
|
break;
|
|
|
|
case WASM_OP_ATOMIC_WAIT32:
|
|
|
|
POP_I64();
|
|
|
|
POP_I32();
|
|
|
|
POP_I32();
|
|
|
|
PUSH_I32();
|
|
|
|
break;
|
|
|
|
case WASM_OP_ATOMIC_WAIT64:
|
|
|
|
POP_I64();
|
|
|
|
POP_I64();
|
|
|
|
POP_I32();
|
|
|
|
PUSH_I32();
|
|
|
|
break;
|
|
|
|
case WASM_OP_ATOMIC_FENCE:
|
|
|
|
/* reserved byte 0x00 */
|
|
|
|
if (*p++ != 0x00) {
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
2021-06-28 03:59:49 +00:00
|
|
|
"zero byte expected");
|
2020-08-03 03:30:26 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WASM_OP_ATOMIC_I32_LOAD:
|
|
|
|
case WASM_OP_ATOMIC_I32_LOAD8_U:
|
|
|
|
case WASM_OP_ATOMIC_I32_LOAD16_U:
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
|
|
|
|
break;
|
|
|
|
case WASM_OP_ATOMIC_I32_STORE:
|
|
|
|
case WASM_OP_ATOMIC_I32_STORE8:
|
|
|
|
case WASM_OP_ATOMIC_I32_STORE16:
|
|
|
|
POP_I32();
|
|
|
|
POP_I32();
|
|
|
|
break;
|
|
|
|
case WASM_OP_ATOMIC_I64_LOAD:
|
|
|
|
case WASM_OP_ATOMIC_I64_LOAD8_U:
|
|
|
|
case WASM_OP_ATOMIC_I64_LOAD16_U:
|
|
|
|
case WASM_OP_ATOMIC_I64_LOAD32_U:
|
|
|
|
POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
|
|
|
|
break;
|
|
|
|
case WASM_OP_ATOMIC_I64_STORE:
|
|
|
|
case WASM_OP_ATOMIC_I64_STORE8:
|
|
|
|
case WASM_OP_ATOMIC_I64_STORE16:
|
|
|
|
case WASM_OP_ATOMIC_I64_STORE32:
|
|
|
|
POP_I64();
|
|
|
|
POP_I32();
|
|
|
|
break;
|
|
|
|
case WASM_OP_ATOMIC_RMW_I32_ADD:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I32_SUB:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I32_AND:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I32_AND8_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I32_AND16_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I32_OR:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I32_OR8_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I32_OR16_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I32_XOR:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I32_XCHG:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
|
|
|
|
POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
|
|
|
|
break;
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_ADD:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_SUB:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_AND:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_AND8_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_AND16_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_AND32_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_OR:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_OR8_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_OR16_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_OR32_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_XOR:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_XCHG:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
|
|
|
|
POP_I64();
|
|
|
|
POP_I32();
|
|
|
|
PUSH_I64();
|
|
|
|
break;
|
|
|
|
case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
|
|
|
|
POP_I32();
|
|
|
|
POP_I32();
|
|
|
|
POP_I32();
|
|
|
|
PUSH_I32();
|
|
|
|
break;
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
|
|
|
|
case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
|
|
|
|
POP_I64();
|
|
|
|
POP_I64();
|
|
|
|
POP_I32();
|
|
|
|
PUSH_I64();
|
|
|
|
break;
|
|
|
|
default:
|
2020-08-21 07:11:31 +00:00
|
|
|
set_error_buf_v(error_buf, error_buf_size,
|
2021-10-08 03:44:39 +00:00
|
|
|
"%s %02x %02x", "unsupported opcode",
|
2023-06-16 02:00:24 +00:00
|
|
|
0xfe, opcode1);
|
2020-08-03 03:30:26 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif /* end of WASM_ENABLE_SHARED_MEMORY */
|
2020-11-05 10:15:15 +00:00
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
default:
|
2021-10-08 03:44:39 +00:00
|
|
|
set_error_buf_v(error_buf, error_buf_size, "%s %02x",
|
2020-08-21 07:11:31 +00:00
|
|
|
"unsupported opcode", opcode);
|
2019-08-08 08:53:56 +00:00
|
|
|
goto fail;
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
last_op = opcode;
|
|
|
|
#endif
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
if (loader_ctx->csp_num > 0) {
|
2022-06-28 08:05:16 +00:00
|
|
|
if (cur_func_idx < module->function_count - 1)
|
|
|
|
/* Function with missing end marker (between two functions) */
|
|
|
|
set_error_buf(error_buf, error_buf_size, "END opcode expected");
|
|
|
|
else
|
|
|
|
/* Function with missing end marker
|
|
|
|
(at EOF or end of code sections) */
|
|
|
|
set_error_buf(error_buf, error_buf_size,
|
|
|
|
"unexpected end of section or function, "
|
|
|
|
"or section size mismatch");
|
2019-08-08 08:53:56 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2020-03-07 14:20:38 +00:00
|
|
|
#if WASM_ENABLE_FAST_INTERP != 0
|
|
|
|
if (loader_ctx->p_code_compiled == NULL)
|
|
|
|
goto re_scan;
|
|
|
|
|
|
|
|
func->const_cell_num = loader_ctx->const_cell_num;
|
2021-09-04 02:09:30 +00:00
|
|
|
if (func->const_cell_num > 0) {
|
2021-12-10 10:13:17 +00:00
|
|
|
int32 j;
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
if (!(func->consts = func_const = loader_malloc(
|
|
|
|
func->const_cell_num * 4, error_buf, error_buf_size)))
|
2021-09-04 02:09:30 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
func_const_end = func->consts + func->const_cell_num * 4;
|
|
|
|
/* reverse the const buf */
|
2021-12-10 10:13:17 +00:00
|
|
|
for (j = loader_ctx->num_const - 1; j >= 0; j--) {
|
|
|
|
Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
|
2021-09-04 02:09:30 +00:00
|
|
|
if (c->value_type == VALUE_TYPE_F64
|
|
|
|
|| c->value_type == VALUE_TYPE_I64) {
|
|
|
|
bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
|
|
|
|
&(c->value.f64), (uint32)sizeof(int64));
|
|
|
|
func_const += sizeof(int64);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
|
|
|
|
&(c->value.f32), (uint32)sizeof(int32));
|
|
|
|
func_const += sizeof(int32);
|
|
|
|
}
|
2020-03-07 14:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 03:44:39 +00:00
|
|
|
func->max_stack_cell_num = loader_ctx->preserved_local_offset
|
|
|
|
- loader_ctx->start_dynamic_offset + 1;
|
2020-03-07 14:20:38 +00:00
|
|
|
#else
|
|
|
|
func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
|
|
|
|
#endif
|
|
|
|
func->max_block_num = loader_ctx->max_csp_num;
|
2019-05-07 02:18:18 +00:00
|
|
|
return_value = true;
|
|
|
|
|
|
|
|
fail:
|
2020-03-07 14:20:38 +00:00
|
|
|
wasm_loader_ctx_destroy(loader_ctx);
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2021-04-15 03:29:20 +00:00
|
|
|
(void)table_idx;
|
|
|
|
(void)table_seg_idx;
|
|
|
|
(void)data_seg_idx;
|
2021-12-10 10:13:17 +00:00
|
|
|
(void)i64_const;
|
2020-02-18 07:15:24 +00:00
|
|
|
(void)local_offset;
|
|
|
|
(void)p_org;
|
2020-03-07 14:20:38 +00:00
|
|
|
(void)mem_offset;
|
|
|
|
(void)align;
|
2019-05-07 02:18:18 +00:00
|
|
|
return return_value;
|
|
|
|
}
|