aboutsummaryrefslogtreecommitdiff
path: root/lib/wamr/core/iwasm/interpreter
diff options
context:
space:
mode:
Diffstat (limited to 'lib/wamr/core/iwasm/interpreter')
-rw-r--r--lib/wamr/core/iwasm/interpreter/wasm.h1454
-rw-r--r--lib/wamr/core/iwasm/interpreter/wasm_interp.h122
-rw-r--r--lib/wamr/core/iwasm/interpreter/wasm_interp_fast.c6294
-rw-r--r--lib/wamr/core/iwasm/interpreter/wasm_loader.c15902
-rw-r--r--lib/wamr/core/iwasm/interpreter/wasm_loader.h80
-rw-r--r--lib/wamr/core/iwasm/interpreter/wasm_opcode.h1030
-rw-r--r--lib/wamr/core/iwasm/interpreter/wasm_runtime.c5034
-rw-r--r--lib/wamr/core/iwasm/interpreter/wasm_runtime.h896
8 files changed, 30812 insertions, 0 deletions
diff --git a/lib/wamr/core/iwasm/interpreter/wasm.h b/lib/wamr/core/iwasm/interpreter/wasm.h
new file mode 100644
index 0000000..6023b07
--- /dev/null
+++ b/lib/wamr/core/iwasm/interpreter/wasm.h
@@ -0,0 +1,1454 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#ifndef _WASM_H_
+#define _WASM_H_
+
+#include "bh_platform.h"
+#include "bh_hashmap.h"
+#include "bh_assert.h"
+#if WASM_ENABLE_GC != 0
+#include "gc_export.h"
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Value Type */
+#define VALUE_TYPE_I32 0x7F
+#define VALUE_TYPE_I64 0X7E
+#define VALUE_TYPE_F32 0x7D
+#define VALUE_TYPE_F64 0x7C
+#define VALUE_TYPE_V128 0x7B
+#define VALUE_TYPE_FUNCREF 0x70
+#define VALUE_TYPE_EXTERNREF 0x6F
+#define VALUE_TYPE_VOID 0x40
+
+/* Packed Types */
+#define PACKED_TYPE_I8 0x78
+#define PACKED_TYPE_I16 0x77
+
+/* Reference Types */
+#define REF_TYPE_NULLFUNCREF 0x73
+#define REF_TYPE_NULLEXTERNREF 0x72
+#define REF_TYPE_NULLREF 0x71
+#define REF_TYPE_FUNCREF VALUE_TYPE_FUNCREF /* 0x70 */
+#define REF_TYPE_EXTERNREF VALUE_TYPE_EXTERNREF /* 0x6F */
+#define REF_TYPE_ANYREF 0x6E
+#define REF_TYPE_EQREF 0x6D
+#define REF_TYPE_I31REF 0x6C
+#define REF_TYPE_STRUCTREF 0x6B
+#define REF_TYPE_ARRAYREF 0x6A
+#define REF_TYPE_HT_NON_NULLABLE 0x64
+#define REF_TYPE_HT_NULLABLE 0x63
+#define REF_TYPE_STRINGREF VALUE_TYPE_STRINGREF /* 0x67 */
+#define REF_TYPE_STRINGVIEWWTF8 VALUE_TYPE_STRINGVIEWWTF8 /* 0x66 */
+#define REF_TYPE_STRINGVIEWWTF16 VALUE_TYPE_STRINGVIEWWTF16 /* 0x62 */
+#define REF_TYPE_STRINGVIEWITER VALUE_TYPE_STRINGVIEWITER /* 0x61 */
+
+/* Heap Types */
+#define HEAP_TYPE_NOFUNC (-0x0D)
+#define HEAP_TYPE_NOEXTERN (-0x0E)
+#define HEAP_TYPE_NONE (-0x0F)
+#define HEAP_TYPE_FUNC (-0x10)
+#define HEAP_TYPE_EXTERN (-0x11)
+#define HEAP_TYPE_ANY (-0x12)
+#define HEAP_TYPE_EQ (-0x13)
+#define HEAP_TYPE_I31 (-0x14)
+#define HEAP_TYPE_STRUCT (-0x15)
+#define HEAP_TYPE_ARRAY (-0x16)
+#define HEAP_TYPE_STRINGREF (-0x19)
+#define HEAP_TYPE_STRINGVIEWWTF8 (-0x1A)
+#define HEAP_TYPE_STRINGVIEWWTF16 (-0x1E)
+#define HEAP_TYPE_STRINGVIEWITER (-0x1F)
+
+/* Defined Types */
+#define DEFINED_TYPE_FUNC 0x60
+#define DEFINED_TYPE_STRUCT 0x5F
+#define DEFINED_TYPE_ARRAY 0x5E
+#define DEFINED_TYPE_SUB 0x50
+#define DEFINED_TYPE_SUB_FINAL 0x4F
+#define DEFINED_TYPE_REC 0x4E
+
+/* Used by AOT */
+#define VALUE_TYPE_I1 0x41
+/**
+ * Used by loader to represent any type of i32/i64/f32/f64/v128
+ * and ref types, including funcref, externref, anyref, eqref,
+ * (ref null $ht), (ref $ht), i31ref, structref, arrayref,
+ * nullfuncref, nullexternref, nullref and stringref
+ */
+#define VALUE_TYPE_ANY 0x42
+/**
+ * Used by wamr compiler to represent object ref types,
+ * including func object ref, externref object ref,
+ * internal object ref, eq object ref, i31 object ref,
+ * struct object ref, array object ref
+ */
+#define VALUE_TYPE_GC_REF 0x43
+
+#define MAX_PAGE_COUNT_FLAG 0x01
+#define SHARED_MEMORY_FLAG 0x02
+#define MEMORY64_FLAG 0x04
+#define MAX_TABLE_SIZE_FLAG 0x01
+/* the shared flag for table is not actual used now */
+#define SHARED_TABLE_FLAG 0x02
+#define TABLE64_FLAG 0x04
+
+/**
+ * In the multi-memory proposal, the memarg in loads and stores are
+ * reinterpreted as a bitfield, bit 6 serves as a flag indicating the presence
+ * of the optional memory index, if it is set, then an i32 memory index follows
+ * after the alignment bitfield
+ */
+#define OPT_MEMIDX_FLAG 0x40
+
+#define DEFAULT_NUM_BYTES_PER_PAGE 65536
+#define DEFAULT_MAX_PAGES 65536
+#define DEFAULT_MEM64_MAX_PAGES UINT32_MAX
+
+/* Max size of linear memory */
+#define MAX_LINEAR_MEMORY_SIZE (4 * (uint64)BH_GB)
+/* Roughly 274 TB */
+#define MAX_LINEAR_MEM64_MEMORY_SIZE \
+ (DEFAULT_MEM64_MAX_PAGES * (uint64)64 * (uint64)BH_KB)
+/* Macro to check memory flag and return appropriate memory size */
+#define GET_MAX_LINEAR_MEMORY_SIZE(is_memory64) \
+ (is_memory64 ? MAX_LINEAR_MEM64_MEMORY_SIZE : MAX_LINEAR_MEMORY_SIZE)
+
+#if WASM_ENABLE_GC == 0
+typedef uintptr_t table_elem_type_t;
+#define NULL_REF (0xFFFFFFFF)
+#else
+typedef void *table_elem_type_t;
+#define NULL_REF (NULL)
+#define REF_CELL_NUM ((uint32)sizeof(uintptr_t) / sizeof(uint32))
+#endif
+
+#define INIT_EXPR_NONE 0x00
+#define INIT_EXPR_TYPE_I32_CONST 0x41
+#define INIT_EXPR_TYPE_I64_CONST 0x42
+#define INIT_EXPR_TYPE_F32_CONST 0x43
+#define INIT_EXPR_TYPE_F64_CONST 0x44
+#define INIT_EXPR_TYPE_V128_CONST 0xFD
+#define INIT_EXPR_TYPE_GET_GLOBAL 0x23
+#define INIT_EXPR_TYPE_REFNULL_CONST 0xD0
+#define INIT_EXPR_TYPE_FUNCREF_CONST 0xD2
+#define INIT_EXPR_TYPE_STRUCT_NEW 0xD3
+#define INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT 0xD4
+#define INIT_EXPR_TYPE_ARRAY_NEW 0xD5
+#define INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT 0xD6
+#define INIT_EXPR_TYPE_ARRAY_NEW_FIXED 0xD7
+#define INIT_EXPR_TYPE_I31_NEW 0xD8
+#define INIT_EXPR_TYPE_ANY_CONVERT_EXTERN 0xD9
+#define INIT_EXPR_TYPE_EXTERN_CONVERT_ANY 0xDA
+
+#define WASM_MAGIC_NUMBER 0x6d736100
+#define WASM_CURRENT_VERSION 1
+
+#define SECTION_TYPE_USER 0
+#define SECTION_TYPE_TYPE 1
+#define SECTION_TYPE_IMPORT 2
+#define SECTION_TYPE_FUNC 3
+#define SECTION_TYPE_TABLE 4
+#define SECTION_TYPE_MEMORY 5
+#define SECTION_TYPE_GLOBAL 6
+#define SECTION_TYPE_EXPORT 7
+#define SECTION_TYPE_START 8
+#define SECTION_TYPE_ELEM 9
+#define SECTION_TYPE_CODE 10
+#define SECTION_TYPE_DATA 11
+#if WASM_ENABLE_BULK_MEMORY != 0
+#define SECTION_TYPE_DATACOUNT 12
+#endif
+#if WASM_ENABLE_TAGS != 0
+#define SECTION_TYPE_TAG 13
+#endif
+#if WASM_ENABLE_STRINGREF != 0
+#define SECTION_TYPE_STRINGREF 14
+#endif
+
+#define SUB_SECTION_TYPE_MODULE 0
+#define SUB_SECTION_TYPE_FUNC 1
+#define SUB_SECTION_TYPE_LOCAL 2
+
+#define IMPORT_KIND_FUNC 0
+#define IMPORT_KIND_TABLE 1
+#define IMPORT_KIND_MEMORY 2
+#define IMPORT_KIND_GLOBAL 3
+#if WASM_ENABLE_TAGS != 0
+#define IMPORT_KIND_TAG 4
+#endif
+
+#define EXPORT_KIND_FUNC 0
+#define EXPORT_KIND_TABLE 1
+#define EXPORT_KIND_MEMORY 2
+#define EXPORT_KIND_GLOBAL 3
+#if WASM_ENABLE_TAGS != 0
+#define EXPORT_KIND_TAG 4
+#endif
+
+#define LABEL_TYPE_BLOCK 0
+#define LABEL_TYPE_LOOP 1
+#define LABEL_TYPE_IF 2
+#define LABEL_TYPE_FUNCTION 3
+#if WASM_ENABLE_EXCE_HANDLING != 0
+#define LABEL_TYPE_TRY 4
+#define LABEL_TYPE_CATCH 5
+#define LABEL_TYPE_CATCH_ALL 6
+#endif
+
+#define WASM_TYPE_FUNC 0
+#define WASM_TYPE_STRUCT 1
+#define WASM_TYPE_ARRAY 2
+
+#if WASM_ENABLE_STRINGREF != 0
+#define WASM_TYPE_STRINGREF 3
+#define WASM_TYPE_STRINGVIEWWTF8 4
+#define WASM_TYPE_STRINGVIEWWTF16 5
+#define WASM_TYPE_STRINGVIEWITER 6
+#endif
+
+/* In WasmGC, a table can start with [0x40 0x00] to indicate it has an
+ * initializer */
+#define TABLE_INIT_EXPR_FLAG 0x40
+
+typedef struct WASMModule WASMModule;
+typedef struct WASMFunction WASMFunction;
+typedef struct WASMGlobal WASMGlobal;
+#if WASM_ENABLE_TAGS != 0
+typedef struct WASMTag WASMTag;
+#endif
+
+#ifndef WASM_VALUE_DEFINED
+#define WASM_VALUE_DEFINED
+
+typedef union V128 {
+ int8 i8x16[16];
+ int16 i16x8[8];
+ int32 i32x4[4];
+ int64 i64x2[2];
+ float32 f32x4[4];
+ float64 f64x2[2];
+} V128;
+
+typedef union WASMValue {
+ int32 i32;
+ uint32 u32;
+ uint32 global_index;
+ uint32 ref_index;
+ int64 i64;
+ uint64 u64;
+ float32 f32;
+ float64 f64;
+ V128 v128;
+#if WASM_ENABLE_GC != 0
+ wasm_obj_t gc_obj;
+ uint32 type_index;
+ struct {
+ uint32 type_index;
+ uint32 length;
+ } array_new_default;
+ /* pointer to a memory space holding more data, current usage:
+ * struct.new init value: WASMStructNewInitValues *
+ * array.new init value: WASMArrayNewInitValues *
+ */
+ void *data;
+#endif
+} WASMValue;
+#endif /* end of WASM_VALUE_DEFINED */
+
+typedef struct WASMStructNewInitValues {
+ uint32 type_idx;
+ uint32 count;
+ WASMValue fields[1];
+} WASMStructNewInitValues;
+
+typedef struct WASMArrayNewInitValues {
+ uint32 type_idx;
+ uint32 length;
+ WASMValue elem_data[1];
+} WASMArrayNewInitValues;
+
+typedef struct InitializerExpression {
+ /* type of INIT_EXPR_TYPE_XXX, which is an instruction of
+ constant expression */
+ uint8 init_expr_type;
+ WASMValue u;
+} InitializerExpression;
+
+#if WASM_ENABLE_GC != 0
+/**
+ * Reference type of (ref null ht) or (ref ht),
+ * and heap type is defined type (type i), i >= 0
+ */
+typedef struct RefHeapType_TypeIdx {
+ /* ref_type is REF_TYPE_HT_NULLABLE or
+ REF_TYPE_HT_NON_NULLABLE, (0x63 or 0x64) */
+ uint8 ref_type;
+ /* true if ref_type is REF_TYPE_HT_NULLABLE */
+ bool nullable;
+ /* heap type is defined type: type_index >= 0 */
+ int32 type_idx;
+} RefHeapType_TypeIdx;
+
+/**
+ * Reference type of (ref null ht) or (ref ht),
+ * and heap type is non-defined type
+ */
+typedef struct RefHeapType_Common {
+ /* ref_type is REF_TYPE_HT_NULLABLE or
+ REF_TYPE_HT_NON_NULLABLE (0x63 or 0x64) */
+ uint8 ref_type;
+ /* true if ref_type is REF_TYPE_HT_NULLABLE */
+ bool nullable;
+ /* Common heap type (not defined type):
+ -0x10 (func), -0x11 (extern), -0x12 (any), -0x13 (eq),
+ -0x16 (i31), -0x17 (nofunc), -0x18 (noextern),
+ -0x19 (struct), -0x20 (array), -0x21 (none) */
+ int32 heap_type;
+} RefHeapType_Common;
+
+/**
+ * Reference type
+ */
+typedef union WASMRefType {
+ uint8 ref_type;
+ RefHeapType_TypeIdx ref_ht_typeidx;
+ RefHeapType_Common ref_ht_common;
+} WASMRefType;
+
+typedef struct WASMRefTypeMap {
+ /**
+ * The type index of a type array, which only stores
+ * the first byte of the type, e.g. WASMFuncType.types,
+ * WASMStructType.fields
+ */
+ uint16 index;
+ /* The full type info if the type cannot be described
+ with one byte */
+ WASMRefType *ref_type;
+} WASMRefTypeMap;
+#endif /* end of WASM_ENABLE_GC */
+
+#if WASM_ENABLE_GC == 0
+typedef struct WASMFuncType WASMType;
+typedef WASMType *WASMTypePtr;
+#else
+/**
+ * Common type, store the same fields of
+ * WASMFuncType, WASMStructType and WASMArrayType
+ */
+typedef struct WASMType {
+ /**
+ * type_flag must be WASM_TYPE_FUNC/STRUCT/ARRAY to
+ * denote that it is a WASMFuncType, WASMStructType or
+ * WASMArrayType
+ */
+ uint16 type_flag;
+
+ bool is_sub_final;
+ /* How many types are referring to this type */
+ uint16 ref_count;
+ /* The inheritance depth */
+ uint16 inherit_depth;
+ /* The root type */
+ struct WASMType *root_type;
+ /* The parent type */
+ struct WASMType *parent_type;
+ uint32 parent_type_idx;
+
+ /* The number of internal types in the current rec group, and if
+ the type is not in a recursive group, rec_count is 1 since a
+ single type definition is reinterpreted as a short-hand for a
+ recursive group containing just one type */
+ uint16 rec_count;
+ uint16 rec_idx;
+ /* The index of the begin type of this group */
+ uint32 rec_begin_type_idx;
+} WASMType, *WASMTypePtr;
+#endif /* end of WASM_ENABLE_GC */
+
+/* Function type */
+typedef struct WASMFuncType {
+#if WASM_ENABLE_GC != 0
+ WASMType base_type;
+#endif
+
+ uint16 param_count;
+ uint16 result_count;
+ uint16 param_cell_num;
+ uint16 ret_cell_num;
+
+#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
+ && WASM_ENABLE_LAZY_JIT != 0
+ /* Code block to call llvm jit functions of this
+ kind of function type from fast jit jitted code */
+ void *call_to_llvm_jit_from_fast_jit;
+#endif
+
+#if WASM_ENABLE_GC != 0
+ uint16 ref_type_map_count;
+ WASMRefTypeMap *ref_type_maps;
+ WASMRefTypeMap *result_ref_type_maps;
+#else
+ uint16 ref_count;
+#endif
+
+#if WASM_ENABLE_QUICK_AOT_ENTRY != 0
+ /* Quick AOT/JIT entry of this func type */
+ void *quick_aot_entry;
+#endif
+
+ /* types of params and results, only store the first byte
+ * of the type, if it cannot be described with one byte,
+ * then the full type info is stored in ref_type_maps */
+ uint8 types[1];
+} WASMFuncType;
+
+#if WASM_ENABLE_GC != 0
+typedef struct WASMStructFieldType {
+ uint16 field_flags;
+ uint8 field_type;
+ uint8 field_size;
+ uint32 field_offset;
+#if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
+ /*
+ * The field size and field offset of a wasm struct may vary
+ * in 32-bit target and 64-bit target, e.g., the size of a
+ * GC reference is 4 bytes in the former and 8 bytes in the
+ * latter, the AOT compiler needs to use the correct field
+ * offset according to the target info.
+ */
+ uint8 field_size_64bit;
+ uint8 field_size_32bit;
+ uint32 field_offset_64bit;
+ uint32 field_offset_32bit;
+#endif
+} WASMStructFieldType;
+
+typedef struct WASMStructType {
+ WASMType base_type;
+
+ /* total size of this struct object */
+ uint32 total_size;
+ uint16 field_count;
+
+ uint16 ref_type_map_count;
+ WASMRefTypeMap *ref_type_maps;
+
+ /* Offsets of reference fields that need to be traced during GC.
+ The first element of the table is the number of such offsets. */
+ uint16 *reference_table;
+
+ /* Field info, note that fields[i]->field_type only stores
+ * the first byte of the field type, if it cannot be described
+ * with one byte, then the full field type info is stored in
+ * ref_type_maps */
+ WASMStructFieldType fields[1];
+} WASMStructType;
+
+typedef struct WASMArrayType {
+ WASMType base_type;
+
+ uint16 elem_flags;
+ uint8 elem_type;
+ /* The full elem type info if the elem type cannot be
+ described with one byte */
+ WASMRefType *elem_ref_type;
+} WASMArrayType;
+
+#if WASM_ENABLE_STRINGREF != 0
+/* stringref representation, we define it as a void * pointer here, the
+ * stringref implementation can use any structure */
+/*
+ WasmGC heap
+ +-----------------------+
+ | |
+ | stringref |
+ | +----------+ | external string representation
+ | | host_ptr |--------o------+----->+------------+
+ | +----------+ | | | |
+ | | | +------------+
+ | stringview_wtf8/16 | |
+ | +----------+ | |
+ | | host_ptr |--------o------+
+ | +----------+ | |
+ | | |
+ | stringview_iter | |
+ | +----------+ | |
+ | | host_ptr |--------o------+
+ | +----------+ |
+ | | pos | |
+ | +----------+ |
+ | |
+ +-----------------------+
+*/
+typedef void *WASMString;
+
+#endif /* end of WASM_ENABLE_STRINGREF != 0 */
+#endif /* end of WASM_ENABLE_GC != 0 */
+
+typedef struct WASMTableType {
+ uint8 elem_type;
+ /**
+ * 0: no max size and not shared
+ * 1: has max size
+ * 2: shared
+ * 4: table64
+ */
+ uint8 flags;
+ bool possible_grow;
+ uint32 init_size;
+ /* specified if (flags & 1), else it is 0x10000 */
+ uint32 max_size;
+#if WASM_ENABLE_GC != 0
+ WASMRefType *elem_ref_type;
+#endif
+} WASMTableType;
+
+typedef struct WASMTable {
+ WASMTableType table_type;
+#if WASM_ENABLE_GC != 0
+ /* init expr for the whole table */
+ InitializerExpression init_expr;
+#endif
+} WASMTable;
+
+#if WASM_ENABLE_MEMORY64 != 0
+typedef uint64 mem_offset_t;
+#define PR_MEM_OFFSET PRIu64
+#else
+typedef uint32 mem_offset_t;
+#define PR_MEM_OFFSET PRIu32
+#endif
+typedef mem_offset_t tbl_elem_idx_t;
+
+typedef struct WASMMemory {
+ uint32 flags;
+ uint32 num_bytes_per_page;
+ uint32 init_page_count;
+ uint32 max_page_count;
+} WASMMemory;
+#ifndef WASM_MEMORY_T_DEFINED
+#define WASM_MEMORY_T_DEFINED
+typedef struct WASMMemory WASMMemoryType;
+#endif
+
+typedef struct WASMTableImport {
+ char *module_name;
+ char *field_name;
+ WASMTableType table_type;
+#if WASM_ENABLE_MULTI_MODULE != 0
+ WASMModule *import_module;
+ WASMTable *import_table_linked;
+#endif
+} WASMTableImport;
+
+typedef struct WASMMemoryImport {
+ char *module_name;
+ char *field_name;
+ WASMMemoryType mem_type;
+#if WASM_ENABLE_MULTI_MODULE != 0
+ WASMModule *import_module;
+ WASMMemory *import_memory_linked;
+#endif
+} WASMMemoryImport;
+
+typedef struct WASMFunctionImport {
+ char *module_name;
+ char *field_name;
+ /* function type */
+ WASMFuncType *func_type;
+ /* native function pointer after linked */
+ void *func_ptr_linked;
+ /* signature from registered native symbols */
+ const char *signature;
+ /* attachment */
+ void *attachment;
+#if WASM_ENABLE_GC != 0
+ /* the type index of this function's func_type */
+ uint32 type_idx;
+#endif
+ bool call_conv_raw;
+ bool call_conv_wasm_c_api;
+#if WASM_ENABLE_MULTI_MODULE != 0
+ WASMModule *import_module;
+ WASMFunction *import_func_linked;
+#endif
+} WASMFunctionImport;
+
+#if WASM_ENABLE_TAGS != 0
+typedef struct WASMTagImport {
+ char *module_name;
+ char *field_name;
+ uint8 attribute; /* the type of the tag (numerical) */
+ uint32 type; /* the type of the catch function (numerical)*/
+ WASMFuncType *tag_type;
+ void *tag_ptr_linked;
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+ /* imported tag pointer after linked */
+ WASMModule *import_module;
+ WASMTag *import_tag_linked;
+ uint32 import_tag_index_linked;
+#endif
+} WASMTagImport;
+#endif
+
+typedef struct WASMGlobalType {
+ uint8 val_type;
+ bool is_mutable;
+} WASMGlobalType;
+
+typedef struct WASMGlobalImport {
+ char *module_name;
+ char *field_name;
+ WASMGlobalType type;
+ bool is_linked;
+ /* global data after linked */
+ WASMValue global_data_linked;
+#if WASM_ENABLE_GC != 0
+ WASMRefType *ref_type;
+#endif
+#if WASM_ENABLE_MULTI_MODULE != 0
+ /* imported function pointer after linked */
+ /* TODO: remove if not needed */
+ WASMModule *import_module;
+ WASMGlobal *import_global_linked;
+#endif
+#if WASM_ENABLE_FAST_JIT != 0
+ /* The data offset of current global in global data */
+ uint32 data_offset;
+#endif
+} WASMGlobalImport;
+
+typedef struct WASMImport {
+ uint8 kind;
+ union {
+ WASMFunctionImport function;
+ WASMTableImport table;
+ WASMMemoryImport memory;
+#if WASM_ENABLE_TAGS != 0
+ WASMTagImport tag;
+#endif
+ WASMGlobalImport global;
+ struct {
+ char *module_name;
+ char *field_name;
+ } names;
+ } u;
+} WASMImport;
+
+struct WASMFunction {
+#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
+ char *field_name;
+#endif
+ /* the type of function */
+ WASMFuncType *func_type;
+ uint32 local_count;
+ uint8 *local_types;
+#if WASM_ENABLE_GC != 0
+ uint16 local_ref_type_map_count;
+ WASMRefTypeMap *local_ref_type_maps;
+#endif
+
+ /* cell num of parameters */
+ uint16 param_cell_num;
+ /* cell num of return type */
+ uint16 ret_cell_num;
+ /* cell num of local variables */
+ uint16 local_cell_num;
+ /* offset of each local, including function parameters
+ and local variables */
+ uint16 *local_offsets;
+
+ uint32 max_stack_cell_num;
+ uint32 max_block_num;
+ uint32 code_size;
+ uint8 *code;
+#if WASM_ENABLE_FAST_INTERP != 0
+ uint32 code_compiled_size;
+ uint8 *code_compiled;
+ uint8 *consts;
+ uint32 const_cell_num;
+#endif
+
+#if WASM_ENABLE_GC != 0
+ /* the type index of this function's func_type */
+ uint32 type_idx;
+#endif
+
+#if WASM_ENABLE_EXCE_HANDLING != 0
+ uint32 exception_handler_count;
+#endif
+
+#if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
+ || WASM_ENABLE_WAMR_COMPILER != 0
+ /* Whether function has opcode memory.grow */
+ bool has_op_memory_grow;
+ /* Whether function has opcode call or call_indirect */
+ bool has_op_func_call;
+#endif
+#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
+ /* Whether function has memory operation opcodes */
+ bool has_memory_operations;
+ /* Whether function has opcode call_indirect */
+ bool has_op_call_indirect;
+ /* Whether function has opcode set_global_aux_stack */
+ bool has_op_set_global_aux_stack;
+#endif
+
+#if WASM_ENABLE_FAST_JIT != 0
+ /* The compiled fast jit jitted code block of this function */
+ void *fast_jit_jitted_code;
+#if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
+ /* The compiled llvm jit func ptr of this function */
+ void *llvm_jit_func_ptr;
+ /* Code block to call fast jit jitted code of this function
+ from the llvm jit jitted code */
+ void *call_to_fast_jit_from_llvm_jit;
+#endif
+#endif
+};
+
+#if WASM_ENABLE_TAGS != 0
+struct WASMTag {
+ uint8 attribute; /* the attribute property of the tag (expected to be 0) */
+ uint32 type; /* the type of the tag (expected valid inden in type table) */
+ WASMFuncType *tag_type;
+};
+#endif
+
+struct WASMGlobal {
+ WASMGlobalType type;
+#if WASM_ENABLE_GC != 0
+ WASMRefType *ref_type;
+#endif
+ InitializerExpression init_expr;
+#if WASM_ENABLE_FAST_JIT != 0
+ /* The data offset of current global in global data */
+ uint32 data_offset;
+#endif
+};
+
+typedef struct WASMExport {
+ char *name;
+ uint8 kind;
+ uint32 index;
+} WASMExport;
+
+typedef struct WASMTableSeg {
+ /* 0 to 7 */
+ uint32 mode;
+ /* funcref or externref, elemkind will be considered as funcref */
+ uint32 elem_type;
+#if WASM_ENABLE_GC != 0
+ WASMRefType *elem_ref_type;
+#endif
+ /* optional, only for active */
+ uint32 table_index;
+ InitializerExpression base_offset;
+ uint32 value_count;
+ InitializerExpression *init_values;
+} WASMTableSeg;
+
+typedef struct WASMDataSeg {
+ uint32 memory_index;
+ InitializerExpression base_offset;
+ uint32 data_length;
+#if WASM_ENABLE_BULK_MEMORY != 0
+ bool is_passive;
+#endif
+ uint8 *data;
+ bool is_data_cloned;
+} WASMDataSeg;
+
+typedef struct BlockAddr {
+ const uint8 *start_addr;
+ uint8 *else_addr;
+ uint8 *end_addr;
+} BlockAddr;
+
+#if WASM_ENABLE_LIBC_WASI != 0
+typedef struct WASIArguments {
+ const char **dir_list;
+ uint32 dir_count;
+ const char **map_dir_list;
+ uint32 map_dir_count;
+ const char **env;
+ uint32 env_count;
+ /* in CIDR notation */
+ const char **addr_pool;
+ uint32 addr_count;
+ const char **ns_lookup_pool;
+ uint32 ns_lookup_count;
+ char **argv;
+ uint32 argc;
+ os_raw_file_handle stdio[3];
+} WASIArguments;
+#endif
+
+typedef struct StringNode {
+ struct StringNode *next;
+ char *str;
+} StringNode, *StringList;
+
+typedef struct BrTableCache {
+ struct BrTableCache *next;
+ /* Address of br_table opcode */
+ uint8 *br_table_op_addr;
+ uint32 br_count;
+ uint32 br_depths[1];
+} BrTableCache;
+
+#if WASM_ENABLE_DEBUG_INTERP != 0
+typedef struct WASMFastOPCodeNode {
+ struct WASMFastOPCodeNode *next;
+ uint64 offset;
+ uint8 orig_op;
+} WASMFastOPCodeNode;
+#endif
+
+#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
+typedef struct WASMCustomSection {
+ struct WASMCustomSection *next;
+ /* Start address of the section name */
+ char *name_addr;
+ /* Length of the section name decoded from leb */
+ uint32 name_len;
+ /* Start address of the content (name len and name skipped) */
+ uint8 *content_addr;
+ uint32 content_len;
+} WASMCustomSection;
+#endif
+
+#if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
+struct AOTCompData;
+struct AOTCompContext;
+
+/* Orc JIT thread arguments */
+typedef struct OrcJitThreadArg {
+#if WASM_ENABLE_JIT != 0
+ struct AOTCompContext *comp_ctx;
+#endif
+ struct WASMModule *module;
+ uint32 group_idx;
+} OrcJitThreadArg;
+#endif
+
+struct WASMModuleInstance;
+
+struct WASMModule {
+ /* Module type, for module loaded from WASM bytecode binary,
+ this field is Wasm_Module_Bytecode;
+ for module loaded from AOT file, this field is
+ Wasm_Module_AoT, and this structure should be treated as
+ AOTModule structure. */
+ uint32 module_type;
+
+ /* the package version read from the WASM file */
+ uint32 package_version;
+
+ uint32 type_count;
+ uint32 import_count;
+ uint32 function_count;
+ uint32 table_count;
+ uint32 memory_count;
+#if WASM_ENABLE_TAGS != 0
+ uint32 tag_count;
+#endif
+ uint32 global_count;
+ uint32 export_count;
+ uint32 table_seg_count;
+ /* data seg count read from data segment section */
+ uint32 data_seg_count;
+#if WASM_ENABLE_BULK_MEMORY != 0
+ /* data count read from datacount section */
+ uint32 data_seg_count1;
+#endif
+#if WASM_ENABLE_GC != 0
+#if WASM_ENABLE_STRINGREF != 0
+ uint32 string_literal_count;
+ uint32 *string_literal_lengths;
+ const uint8 **string_literal_ptrs;
+#endif
+#endif
+
+ uint32 import_function_count;
+ uint32 import_table_count;
+ uint32 import_memory_count;
+#if WASM_ENABLE_TAGS != 0
+ uint32 import_tag_count;
+#endif
+ uint32 import_global_count;
+
+ WASMImport *import_functions;
+ WASMImport *import_tables;
+ WASMImport *import_memories;
+#if WASM_ENABLE_TAGS != 0
+ WASMImport *import_tags;
+#endif
+ WASMImport *import_globals;
+
+ WASMType **types;
+ WASMImport *imports;
+ WASMFunction **functions;
+ WASMTable *tables;
+ WASMMemory *memories;
+#if WASM_ENABLE_TAGS != 0
+ WASMTag **tags;
+#endif
+ WASMGlobal *globals;
+ WASMExport *exports;
+ WASMTableSeg *table_segments;
+ WASMDataSeg **data_segments;
+ uint32 start_function;
+
+ /* total global variable size */
+ uint32 global_data_size;
+
+ /* the index of auxiliary __data_end global,
+ -1 means unexported */
+ uint32 aux_data_end_global_index;
+ /* auxiliary __data_end exported by wasm app */
+ uint64 aux_data_end;
+
+ /* the index of auxiliary __heap_base global,
+ -1 means unexported */
+ uint32 aux_heap_base_global_index;
+ /* auxiliary __heap_base exported by wasm app */
+ uint64 aux_heap_base;
+
+ /* the index of auxiliary stack top global,
+ -1 means unexported */
+ uint32 aux_stack_top_global_index;
+ /* auxiliary stack bottom resolved */
+ uint64 aux_stack_bottom;
+ /* auxiliary stack size resolved */
+ uint32 aux_stack_size;
+
+ /* the index of malloc/free function,
+ -1 means unexported */
+ uint32 malloc_function;
+ uint32 free_function;
+
+ /* the index of __retain function,
+ -1 means unexported */
+ uint32 retain_function;
+
+ /* Whether there is possible memory grow, e.g. memory.grow opcode */
+ bool possible_memory_grow;
+
+ StringList const_str_list;
+#if WASM_ENABLE_FAST_INTERP == 0
+ bh_list br_table_cache_list_head;
+ bh_list *br_table_cache_list;
+#endif
+
+#if WASM_ENABLE_LIBC_WASI != 0
+ WASIArguments wasi_args;
+ bool import_wasi_api;
+#endif
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+ /* TODO: add mutex for mutli-thread? */
+ bh_list import_module_list_head;
+ bh_list *import_module_list;
+#endif
+
+#if WASM_ENABLE_GC != 0
+ /* Ref types hash set */
+ HashMap *ref_type_set;
+ struct WASMRttType **rtt_types;
+ korp_mutex rtt_type_lock;
+#if WASM_ENABLE_STRINGREF != 0
+ /* special rtts for stringref types
+ - stringref
+ - stringview_wtf8
+ - stringview_wtf16
+ - stringview_iter
+ */
+ struct WASMRttType *stringref_rtts[4];
+#endif
+#endif
+
+#if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
+ bh_list fast_opcode_list;
+ uint8 *buf_code;
+ uint64 buf_code_size;
+#endif
+
+#if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
+ || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0 \
+ || WASM_ENABLE_WAMR_COMPILER != 0
+ uint8 *load_addr;
+ uint64 load_size;
+#endif
+
+#if WASM_ENABLE_DEBUG_INTERP != 0 \
+ || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
+ && WASM_ENABLE_LAZY_JIT != 0)
+ /**
+ * List of instances referred to this module. When source debugging
+ * feature is enabled, the debugger may modify the code section of
+ * the module, so we need to report a warning if user create several
+ * instances based on the same module.
+ *
+ * Also add the instance to the list for Fast JIT to LLVM JIT
+ * tier-up, since we need to lazily update the LLVM func pointers
+ * in the instance.
+ */
+ struct WASMModuleInstance *instance_list;
+ korp_mutex instance_list_lock;
+#endif
+
+#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
+ const uint8 *name_section_buf;
+ const uint8 *name_section_buf_end;
+#endif
+
+#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
+ WASMCustomSection *custom_section_list;
+#endif
+
+#if WASM_ENABLE_FAST_JIT != 0
+ /**
+ * func pointers of Fast JITed (un-imported) functions
+ * for non Multi-Tier JIT mode:
+ * (1) when lazy jit is disabled, each pointer is set to the compiled
+ * fast jit jitted code
+ * (2) when lazy jit is enabled, each pointer is firstly inited as
+ * jit_global->compile_fast_jit_and_then_call, and then set to the
+ * compiled fast jit jitted code when it is called (the stub will
+ * compile the jit function and then update itself)
+ * for Multi-Tier JIT mode:
+ * each pointer is firstly inited as compile_fast_jit_and_then_call,
+ * and then set to the compiled fast jit jitted code when it is called,
+ * and when the llvm jit func ptr of the same function is compiled, it
+ * will be set to call_to_llvm_jit_from_fast_jit of this function type
+ * (tier-up from fast-jit to llvm-jit)
+ */
+ void **fast_jit_func_ptrs;
+ /* locks for Fast JIT lazy compilation */
+ korp_mutex fast_jit_thread_locks[WASM_ORC_JIT_BACKEND_THREAD_NUM];
+ bool fast_jit_thread_locks_inited[WASM_ORC_JIT_BACKEND_THREAD_NUM];
+#endif
+
+#if WASM_ENABLE_JIT != 0
+ struct AOTCompData *comp_data;
+ struct AOTCompContext *comp_ctx;
+ /**
+ * func pointers of LLVM JITed (un-imported) functions
+ * for non Multi-Tier JIT mode:
+ * each pointer is set to the looked up llvm jit func ptr, note that it
+ * is a stub and will trigger the actual compilation when it is called
+ * for Multi-Tier JIT mode:
+ * each pointer is inited as call_to_fast_jit code block, when the llvm
+ * jit func ptr is actually compiled, it is set to the compiled llvm jit
+ * func ptr
+ */
+ void **func_ptrs;
+ /* whether the func pointers are compiled */
+ bool *func_ptrs_compiled;
+#endif
+
+#if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
+ /* backend compilation threads */
+ korp_tid orcjit_threads[WASM_ORC_JIT_BACKEND_THREAD_NUM];
+ /* backend thread arguments */
+ OrcJitThreadArg orcjit_thread_args[WASM_ORC_JIT_BACKEND_THREAD_NUM];
+ /* whether to stop the compilation of backend threads */
+ bool orcjit_stop_compiling;
+#endif
+
+#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
+ && WASM_ENABLE_LAZY_JIT != 0
+ /* wait lock/cond for the synchronization of
+ the llvm jit initialization */
+ korp_mutex tierup_wait_lock;
+ korp_cond tierup_wait_cond;
+ bool tierup_wait_lock_inited;
+ korp_tid llvm_jit_init_thread;
+ /* whether the llvm jit is initialized */
+ bool llvm_jit_inited;
+ /* Whether to enable llvm jit compilation:
+ it is set to true only when there is a module instance starts to
+ run with running mode Mode_LLVM_JIT or Mode_Multi_Tier_JIT,
+ since no need to enable llvm jit compilation for Mode_Interp and
+ Mode_Fast_JIT, so as to improve performance for them */
+ bool enable_llvm_jit_compilation;
+ /* The count of groups which finish compiling the fast jit
+ functions in that group */
+ uint32 fast_jit_ready_groups;
+#endif
+
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ bool is_simd_used;
+ bool is_ref_types_used;
+ bool is_bulk_memory_used;
+#endif
+
+ /* user defined name */
+ char *name;
+
+ /* Whether the underlying wasm binary buffer can be freed */
+ bool is_binary_freeable;
+};
+
+typedef struct BlockType {
+ /* Block type may be expressed in one of two forms:
+ * either by the type of the single return value or
+ * by a type index of module.
+ */
+ union {
+ struct {
+ uint8 type;
+#if WASM_ENABLE_GC != 0
+ WASMRefTypeMap ref_type_map;
+#endif
+ } value_type;
+ WASMFuncType *type;
+ } u;
+ bool is_value_type;
+} BlockType;
+
+typedef struct WASMBranchBlock {
+ uint8 *begin_addr;
+ uint8 *target_addr;
+ uint32 *frame_sp;
+ uint32 cell_num;
+#if WASM_ENABLE_EXCE_HANDLING != 0
+ /* in exception handling, label_type needs to be stored to lookup exception
+ * handlers */
+ uint8 label_type;
+#endif
+} WASMBranchBlock;
+
+/**
+ * Align an unsigned value on a alignment boundary.
+ *
+ * @param v the value to be aligned
+ * @param b the alignment boundary (2, 4, 8, ...)
+ *
+ * @return the aligned value
+ */
+inline static unsigned
+align_uint(unsigned v, unsigned b)
+{
+ unsigned m = b - 1;
+ return (v + m) & ~m;
+}
+
+/**
+ * Align an 64 bit unsigned value on a alignment boundary.
+ *
+ * @param v the value to be aligned
+ * @param b the alignment boundary (2, 4, 8, ...)
+ *
+ * @return the aligned value
+ */
+inline static uint64
+align_uint64(uint64 v, uint64 b)
+{
+ uint64 m = b - 1;
+ return (v + m) & ~m;
+}
+
+/**
+ * Check whether a piece of data is out of range
+ *
+ * @param offset the offset that the data starts
+ * @param len the length of the data
+ * @param max_size the maximum size of the data range
+ *
+ * @return true if out of range, false otherwise
+ */
+inline static bool
+offset_len_out_of_bounds(uint32 offset, uint32 len, uint32 max_size)
+{
+ if (offset + len < offset /* integer overflow */
+ || offset + len > max_size)
+ return true;
+ return false;
+}
+
+/**
+ * Return the hash value of c string.
+ */
+inline static uint32
+wasm_string_hash(const char *str)
+{
+ unsigned h = (unsigned)strlen(str);
+ const uint8 *p = (uint8 *)str;
+ const uint8 *end = p + h;
+
+ while (p != end)
+ h = ((h << 5) - h) + *p++;
+ return h;
+}
+
+/**
+ * Whether two c strings are equal.
+ */
+inline static bool
+wasm_string_equal(const char *s1, const char *s2)
+{
+ return strcmp(s1, s2) == 0 ? true : false;
+}
+
+/**
+ * Return the byte size of value type with specific pointer size.
+ *
+ * Note: Please use wasm_value_type_size for interpreter, only aot compiler
+ * can use this API directly to calculate type size for different target
+ */
+inline static uint32
+wasm_value_type_size_internal(uint8 value_type, uint8 pointer_size)
+{
+ if (value_type == VALUE_TYPE_VOID)
+ return 0;
+ else if (value_type == VALUE_TYPE_I32 || value_type == VALUE_TYPE_F32
+ || value_type == VALUE_TYPE_ANY)
+ return sizeof(int32);
+ else if (value_type == VALUE_TYPE_I64 || value_type == VALUE_TYPE_F64)
+ return sizeof(int64);
+#if WASM_ENABLE_SIMD != 0
+ else if (value_type == VALUE_TYPE_V128)
+ return sizeof(int64) * 2;
+#endif
+#if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
+ else if (value_type == VALUE_TYPE_FUNCREF
+ || value_type == VALUE_TYPE_EXTERNREF)
+ return sizeof(uint32);
+#elif WASM_ENABLE_GC != 0
+ else if ((value_type >= (uint8)REF_TYPE_ARRAYREF /* 0x6A */
+ && value_type <= (uint8)REF_TYPE_NULLFUNCREF) /* 0x73 */
+ || (value_type >= (uint8)REF_TYPE_HT_NULLABLE /* 0x63 */
+ && value_type <= (uint8)REF_TYPE_HT_NON_NULLABLE) /* 0x64 */
+#if WASM_ENABLE_STRINGREF != 0
+ || (value_type >= (uint8)REF_TYPE_STRINGVIEWWTF8 /* 0x66 */
+ && value_type <= (uint8)REF_TYPE_STRINGREF) /* 0x67 */
+ || (value_type >= (uint8)REF_TYPE_STRINGVIEWITER /* 0x61 */
+ && value_type <= (uint8)REF_TYPE_STRINGVIEWWTF16) /* 0x62 */
+#endif
+ )
+ return pointer_size;
+ else if (value_type == PACKED_TYPE_I8)
+ return sizeof(int8);
+ else if (value_type == PACKED_TYPE_I16)
+ return sizeof(int16);
+#endif
+ else {
+ bh_assert(0);
+ }
+#if WASM_ENABLE_GC == 0
+ (void)pointer_size;
+#endif
+ return 0;
+}
+
+/**
+ * Return the cell num of value type with specific pointer size.
+ *
+ * Note: Please use wasm_value_type_cell_num for interpreter, only aot compiler
+ * can use this API directly to calculate type cell num for different target
+ */
+inline static uint16
+wasm_value_type_cell_num_internal(uint8 value_type, uint8 pointer_size)
+{
+ return wasm_value_type_size_internal(value_type, pointer_size) / 4;
+}
+
+/**
+ * Return the byte size of value type.
+ */
+inline static uint32
+wasm_value_type_size(uint8 value_type)
+{
+ return wasm_value_type_size_internal(value_type, sizeof(uintptr_t));
+}
+
+inline static uint16
+wasm_value_type_cell_num(uint8 value_type)
+{
+ return wasm_value_type_size(value_type) / 4;
+}
+
+inline static uint32
+wasm_get_cell_num(const uint8 *types, uint32 type_count)
+{
+ uint32 cell_num = 0;
+ uint32 i;
+ for (i = 0; i < type_count; i++)
+ cell_num += wasm_value_type_cell_num(types[i]);
+ return cell_num;
+}
+
+#if WASM_ENABLE_REF_TYPES != 0
+inline static uint16
+wasm_value_type_cell_num_outside(uint8 value_type)
+{
+ if (VALUE_TYPE_EXTERNREF == value_type) {
+ return sizeof(uintptr_t) / sizeof(uint32);
+ }
+ else {
+ return wasm_value_type_cell_num(value_type);
+ }
+}
+#endif
+
+#if WASM_ENABLE_GC == 0
+inline static bool
+wasm_type_equal(const WASMType *type1, const WASMType *type2,
+ const WASMTypePtr *types, uint32 type_count)
+{
+ const WASMFuncType *func_type1 = (const WASMFuncType *)type1;
+ const WASMFuncType *func_type2 = (const WASMFuncType *)type2;
+
+ if (type1 == type2) {
+ return true;
+ }
+
+ return (func_type1->param_count == func_type2->param_count
+ && func_type1->result_count == func_type2->result_count
+ && memcmp(
+ func_type1->types, func_type2->types,
+ (uint32)(func_type1->param_count + func_type1->result_count))
+ == 0)
+ ? true
+ : false;
+ (void)types;
+ (void)type_count;
+}
+#else
+/* implemented in gc_type.c */
+bool
+wasm_type_equal(const WASMType *type1, const WASMType *type2,
+ const WASMTypePtr *types, uint32 type_count);
+#endif
+
+inline static uint32
+wasm_get_smallest_type_idx(const WASMTypePtr *types, uint32 type_count,
+ uint32 cur_type_idx)
+{
+ uint32 i;
+
+ for (i = 0; i < cur_type_idx; i++) {
+ if (wasm_type_equal(types[cur_type_idx], types[i], types, type_count))
+ return i;
+ }
+ return cur_type_idx;
+}
+
+#if WASM_ENABLE_GC == 0
+static inline uint32
+block_type_get_param_types(BlockType *block_type, uint8 **p_param_types)
+#else
+static inline uint32
+block_type_get_param_types(BlockType *block_type, uint8 **p_param_types,
+ WASMRefTypeMap **p_param_reftype_maps,
+ uint32 *p_param_reftype_map_count)
+#endif
+{
+ uint32 param_count = 0;
+ if (!block_type->is_value_type) {
+ WASMFuncType *func_type = block_type->u.type;
+ *p_param_types = func_type->types;
+ param_count = func_type->param_count;
+#if WASM_ENABLE_GC != 0
+ *p_param_reftype_maps = func_type->ref_type_maps;
+ *p_param_reftype_map_count = (uint32)(func_type->result_ref_type_maps
+ - func_type->ref_type_maps);
+#endif
+ }
+ else {
+ *p_param_types = NULL;
+ param_count = 0;
+#if WASM_ENABLE_GC != 0
+ *p_param_reftype_maps = NULL;
+ *p_param_reftype_map_count = 0;
+#endif
+ }
+
+ return param_count;
+}
+
+#if WASM_ENABLE_GC == 0
+static inline uint32
+block_type_get_result_types(BlockType *block_type, uint8 **p_result_types)
+#else
+static inline uint32
+block_type_get_result_types(BlockType *block_type, uint8 **p_result_types,
+ WASMRefTypeMap **p_result_reftype_maps,
+ uint32 *p_result_reftype_map_count)
+#endif
+{
+ uint32 result_count = 0;
+ uint8 *result_types = NULL;
+#if WASM_ENABLE_GC != 0
+ uint8 type;
+ uint32 result_reftype_map_count = 0;
+ WASMRefTypeMap *result_reftype_maps = NULL;
+#endif
+
+ if (block_type->is_value_type) {
+ if (block_type->u.value_type.type != VALUE_TYPE_VOID) {
+ result_types = &block_type->u.value_type.type;
+ result_count = 1;
+#if WASM_ENABLE_GC != 0
+ type = block_type->u.value_type.type;
+ if (type == (uint8)REF_TYPE_HT_NULLABLE
+ || type == (uint8)REF_TYPE_HT_NON_NULLABLE) {
+ result_reftype_maps = &block_type->u.value_type.ref_type_map;
+ result_reftype_map_count = 1;
+ }
+#endif
+ }
+ }
+ else {
+ WASMFuncType *func_type = block_type->u.type;
+ result_types = func_type->types + func_type->param_count;
+ result_count = func_type->result_count;
+#if WASM_ENABLE_GC != 0
+ result_reftype_maps = func_type->result_ref_type_maps;
+ result_reftype_map_count = (uint32)(func_type->ref_type_map_count
+ - (func_type->result_ref_type_maps
+ - func_type->ref_type_maps));
+#endif
+ }
+ *p_result_types = result_types;
+#if WASM_ENABLE_GC != 0
+ *p_result_reftype_maps = result_reftype_maps;
+ *p_result_reftype_map_count = result_reftype_map_count;
+#endif
+ return result_count;
+}
+
+static inline uint32
+block_type_get_arity(const BlockType *block_type, uint8 label_type)
+{
+ if (label_type == LABEL_TYPE_LOOP) {
+ if (block_type->is_value_type)
+ return 0;
+ else
+ return block_type->u.type->param_count;
+ }
+ else {
+ if (block_type->is_value_type) {
+ return block_type->u.value_type.type != VALUE_TYPE_VOID ? 1 : 0;
+ }
+ else
+ return block_type->u.type->result_count;
+ }
+ return 0;
+}
+
+#ifdef __cplusplus
+} /* end of extern "C" */
+#endif
+
+#endif /* end of _WASM_H_ */
diff --git a/lib/wamr/core/iwasm/interpreter/wasm_interp.h b/lib/wamr/core/iwasm/interpreter/wasm_interp.h
new file mode 100644
index 0000000..1416405
--- /dev/null
+++ b/lib/wamr/core/iwasm/interpreter/wasm_interp.h
@@ -0,0 +1,122 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#ifndef _WASM_INTERP_H
+#define _WASM_INTERP_H
+
+#include "wasm.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct WASMModuleInstance;
+struct WASMFunctionInstance;
+struct WASMExecEnv;
+
+typedef struct WASMInterpFrame {
+ /* The frame of the caller that are calling the current function. */
+ struct WASMInterpFrame *prev_frame;
+
+ /* The current WASM function. */
+ struct WASMFunctionInstance *function;
+
+ /* Instruction pointer of the bytecode array. */
+ uint8 *ip;
+
+#if WASM_ENABLE_FAST_JIT != 0
+ uint8 *jitted_return_addr;
+#endif
+
+#if WASM_ENABLE_PERF_PROFILING != 0
+ uint64 time_started;
+#endif
+
+#if WASM_ENABLE_EXCE_HANDLING != 0
+ /* set to true if the callee returns an exception rather than
+ * result values on the stack
+ */
+ bool exception_raised;
+ uint32 tag_index;
+#endif
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ /* Return offset of the first return value of current frame,
+ the callee will put return values here continuously */
+ uint32 ret_offset;
+ uint32 *lp;
+#if WASM_ENABLE_GC != 0
+ uint8 *frame_ref;
+#endif
+ uint32 operand[1];
+#else /* else of WASM_ENABLE_FAST_INTERP != 0 */
+ /* Operand stack top pointer of the current frame. The bottom of
+ the stack is the next cell after the last local variable. */
+ uint32 *sp_bottom;
+ uint32 *sp_boundary;
+ uint32 *sp;
+
+ WASMBranchBlock *csp_bottom;
+ WASMBranchBlock *csp_boundary;
+ WASMBranchBlock *csp;
+
+ /**
+ * Frame data, the layout is:
+ * lp: parameters and local variables
+ * sp_bottom to sp_boundary: wasm operand stack
+ * csp_bottom to csp_boundary: wasm label stack
+ * frame ref flags: only available for GC
+ * whether each cell in local and stack area is a GC obj
+ * jit spill cache: only available for fast jit
+ */
+ uint32 lp[1];
+#endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
+} WASMInterpFrame;
+
+/**
+ * Calculate the size of interpreter area of frame of a function.
+ *
+ * @param all_cell_num number of all cells including local variables
+ * and the working stack slots
+ *
+ * @return the size of interpreter area of the frame
+ */
+static inline unsigned
+wasm_interp_interp_frame_size(unsigned all_cell_num)
+{
+ unsigned frame_size;
+
+#if WASM_ENABLE_FAST_INTERP == 0
+#if WASM_ENABLE_GC == 0
+ frame_size = (uint32)offsetof(WASMInterpFrame, lp) + all_cell_num * 4;
+#else
+ frame_size =
+ (uint32)offsetof(WASMInterpFrame, lp) + align_uint(all_cell_num * 5, 4);
+#endif
+#else
+ frame_size = (uint32)offsetof(WASMInterpFrame, operand) + all_cell_num * 4;
+#endif
+ return align_uint(frame_size, 4);
+}
+
+void
+wasm_interp_call_wasm(struct WASMModuleInstance *module_inst,
+ struct WASMExecEnv *exec_env,
+ struct WASMFunctionInstance *function, uint32 argc,
+ uint32 argv[]);
+
+#if WASM_ENABLE_GC != 0
+bool
+wasm_interp_traverse_gc_rootset(struct WASMExecEnv *exec_env, void *heap);
+
+uint8 *
+wasm_interp_get_frame_ref(WASMInterpFrame *frame);
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* end of _WASM_INTERP_H */
diff --git a/lib/wamr/core/iwasm/interpreter/wasm_interp_fast.c b/lib/wamr/core/iwasm/interpreter/wasm_interp_fast.c
new file mode 100644
index 0000000..82b6ff7
--- /dev/null
+++ b/lib/wamr/core/iwasm/interpreter/wasm_interp_fast.c
@@ -0,0 +1,6294 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include "wasm_interp.h"
+#include "bh_log.h"
+#include "wasm_runtime.h"
+#include "wasm_opcode.h"
+#include "wasm_loader.h"
+#include "wasm_memory.h"
+#include "../common/wasm_exec_env.h"
+#if WASM_ENABLE_GC != 0
+#include "../common/gc/gc_object.h"
+#include "mem_alloc.h"
+#if WASM_ENABLE_STRINGREF != 0
+#include "string_object.h"
+#endif
+#endif
+#if WASM_ENABLE_SHARED_MEMORY != 0
+#include "../common/wasm_shared_memory.h"
+#endif
+
+typedef int32 CellType_I32;
+typedef int64 CellType_I64;
+typedef float32 CellType_F32;
+typedef float64 CellType_F64;
+
+#if WASM_ENABLE_THREAD_MGR == 0
+#define get_linear_mem_size() linear_mem_size
+#else
+/**
+ * Load memory data size in each time boundary check in
+ * multi-threading mode since it may be changed by other
+ * threads in memory.grow
+ */
+#define get_linear_mem_size() GET_LINEAR_MEMORY_SIZE(memory)
+#endif
+
+#if WASM_ENABLE_SHARED_HEAP != 0
+#define app_addr_in_shared_heap(app_addr, bytes) \
+ (shared_heap && (app_addr) >= shared_heap_start_off \
+ && (app_addr) <= shared_heap_end_off - bytes + 1)
+
+#define shared_heap_addr_app_to_native(app_addr, native_addr) \
+ native_addr = shared_heap_base_addr + ((app_addr)-shared_heap_start_off)
+
+#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr) \
+ if (app_addr_in_shared_heap(app_addr, bytes)) \
+ shared_heap_addr_app_to_native(app_addr, native_addr); \
+ else
+#else
+#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr)
+#endif
+
+#if !defined(OS_ENABLE_HW_BOUND_CHECK) \
+ || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+#define CHECK_MEMORY_OVERFLOW(bytes) \
+ do { \
+ uint64 offset1 = (uint64)offset + (uint64)addr; \
+ CHECK_SHARED_HEAP_OVERFLOW(offset1, bytes, maddr) \
+ if (disable_bounds_checks || offset1 + bytes <= get_linear_mem_size()) \
+ /* If offset1 is in valid range, maddr must also \
+ be in valid range, no need to check it again. */ \
+ maddr = memory->memory_data + offset1; \
+ else \
+ goto out_of_bounds; \
+ } while (0)
+
+#define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
+ do { \
+ uint64 offset1 = (uint32)(start); \
+ CHECK_SHARED_HEAP_OVERFLOW(offset1, bytes, maddr) \
+ if (disable_bounds_checks || offset1 + bytes <= get_linear_mem_size()) \
+ /* App heap space is not valid space for \
+ bulk memory operation */ \
+ maddr = memory->memory_data + offset1; \
+ else \
+ goto out_of_bounds; \
+ } while (0)
+#else
+#define CHECK_MEMORY_OVERFLOW(bytes) \
+ do { \
+ uint64 offset1 = (uint64)offset + (uint64)addr; \
+ CHECK_SHARED_HEAP_OVERFLOW(offset1, bytes, maddr) \
+ maddr = memory->memory_data + offset1; \
+ } while (0)
+
+#define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
+ do { \
+ uint64 offset1 = (uint32)(start); \
+ CHECK_SHARED_HEAP_OVERFLOW(offset1, bytes, maddr) \
+ maddr = memory->memory_data + offset1; \
+ } while (0)
+#endif /* !defined(OS_ENABLE_HW_BOUND_CHECK) \
+ || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 */
+
+#define CHECK_ATOMIC_MEMORY_ACCESS(align) \
+ do { \
+ if (((uintptr_t)maddr & (align - 1)) != 0) \
+ goto unaligned_atomic; \
+ } while (0)
+
+static inline uint32
+rotl32(uint32 n, uint32 c)
+{
+ const uint32 mask = (31);
+ c = c % 32;
+ c &= mask;
+ return (n << c) | (n >> ((0 - c) & mask));
+}
+
+static inline uint32
+rotr32(uint32 n, uint32 c)
+{
+ const uint32 mask = (31);
+ c = c % 32;
+ c &= mask;
+ return (n >> c) | (n << ((0 - c) & mask));
+}
+
+static inline uint64
+rotl64(uint64 n, uint64 c)
+{
+ const uint64 mask = (63);
+ c = c % 64;
+ c &= mask;
+ return (n << c) | (n >> ((0 - c) & mask));
+}
+
+static inline uint64
+rotr64(uint64 n, uint64 c)
+{
+ const uint64 mask = (63);
+ c = c % 64;
+ c &= mask;
+ return (n >> c) | (n << ((0 - c) & mask));
+}
+
+static inline float32
+f32_min(float32 a, float32 b)
+{
+ if (isnan(a) || isnan(b))
+ return NAN;
+ else if (a == 0 && a == b)
+ return signbit(a) ? a : b;
+ else
+ return a > b ? b : a;
+}
+
+static inline float32
+f32_max(float32 a, float32 b)
+{
+ if (isnan(a) || isnan(b))
+ return NAN;
+ else if (a == 0 && a == b)
+ return signbit(a) ? b : a;
+ else
+ return a > b ? a : b;
+}
+
+static inline float64
+f64_min(float64 a, float64 b)
+{
+ if (isnan(a) || isnan(b))
+ return NAN;
+ else if (a == 0 && a == b)
+ return signbit(a) ? a : b;
+ else
+ return a > b ? b : a;
+}
+
+static inline float64
+f64_max(float64 a, float64 b)
+{
+ if (isnan(a) || isnan(b))
+ return NAN;
+ else if (a == 0 && a == b)
+ return signbit(a) ? b : a;
+ else
+ return a > b ? a : b;
+}
+
+static inline uint32
+clz32(uint32 type)
+{
+ uint32 num = 0;
+ if (type == 0)
+ return 32;
+ while (!(type & 0x80000000)) {
+ num++;
+ type <<= 1;
+ }
+ return num;
+}
+
+static inline uint32
+clz64(uint64 type)
+{
+ uint32 num = 0;
+ if (type == 0)
+ return 64;
+ while (!(type & 0x8000000000000000LL)) {
+ num++;
+ type <<= 1;
+ }
+ return num;
+}
+
+static inline uint32
+ctz32(uint32 type)
+{
+ uint32 num = 0;
+ if (type == 0)
+ return 32;
+ while (!(type & 1)) {
+ num++;
+ type >>= 1;
+ }
+ return num;
+}
+
+static inline uint32
+ctz64(uint64 type)
+{
+ uint32 num = 0;
+ if (type == 0)
+ return 64;
+ while (!(type & 1)) {
+ num++;
+ type >>= 1;
+ }
+ return num;
+}
+
+static inline uint32
+popcount32(uint32 u)
+{
+ uint32 ret = 0;
+ while (u) {
+ u = (u & (u - 1));
+ ret++;
+ }
+ return ret;
+}
+
+static inline uint32
+popcount64(uint64 u)
+{
+ uint32 ret = 0;
+ while (u) {
+ u = (u & (u - 1));
+ ret++;
+ }
+ return ret;
+}
+
+static float
+local_copysignf(float x, float y)
+{
+ union {
+ float f;
+ uint32 i;
+ } ux = { x }, uy = { y };
+ ux.i &= 0x7fffffff;
+ ux.i |= uy.i & 0x80000000;
+ return ux.f;
+}
+
+static double
+local_copysign(double x, double y)
+{
+ union {
+ double f;
+ uint64 i;
+ } ux = { x }, uy = { y };
+ ux.i &= UINT64_MAX / 2;
+ ux.i |= uy.i & 1ULL << 63;
+ return ux.f;
+}
+
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
+#define LOAD_U32_WITH_2U16S(addr) (*(uint32 *)(addr))
+#define LOAD_PTR(addr) (*(void **)(addr))
+#else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
+static inline uint32
+LOAD_U32_WITH_2U16S(void *addr)
+{
+ union {
+ uint32 val;
+ uint16 u16[2];
+ } u;
+
+ bh_assert(((uintptr_t)addr & 1) == 0);
+ u.u16[0] = ((uint16 *)addr)[0];
+ u.u16[1] = ((uint16 *)addr)[1];
+ return u.val;
+}
+#if UINTPTR_MAX == UINT32_MAX
+#define LOAD_PTR(addr) ((void *)LOAD_U32_WITH_2U16S(addr))
+#elif UINTPTR_MAX == UINT64_MAX
+static inline void *
+LOAD_PTR(void *addr)
+{
+ uintptr_t addr1 = (uintptr_t)addr;
+ union {
+ void *val;
+ uint32 u32[2];
+ uint16 u16[4];
+ } u;
+
+ bh_assert(((uintptr_t)addr & 1) == 0);
+ if ((addr1 & (uintptr_t)7) == 0)
+ return *(void **)addr;
+
+ if ((addr1 & (uintptr_t)3) == 0) {
+ u.u32[0] = ((uint32 *)addr)[0];
+ u.u32[1] = ((uint32 *)addr)[1];
+ }
+ else {
+ u.u16[0] = ((uint16 *)addr)[0];
+ u.u16[1] = ((uint16 *)addr)[1];
+ u.u16[2] = ((uint16 *)addr)[2];
+ u.u16[3] = ((uint16 *)addr)[3];
+ }
+ return u.val;
+}
+#endif /* end of UINTPTR_MAX */
+#endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
+
+#if WASM_ENABLE_GC != 0
+static void
+init_frame_refs(uint8 *frame_ref, uint32 cell_num, WASMFunctionInstance *func)
+{
+ uint32 i, j;
+
+ memset(frame_ref, 0, cell_num);
+
+ for (i = 0, j = 0; i < func->param_count; i++) {
+ if (wasm_is_type_reftype(func->param_types[i])
+ && !wasm_is_reftype_i31ref(func->param_types[i])) {
+ frame_ref[j++] = 1;
+#if UINTPTR_MAX == UINT64_MAX
+ frame_ref[j++] = 1;
+#endif
+ }
+ else {
+ j += wasm_value_type_cell_num(func->param_types[i]);
+ }
+ }
+
+ for (i = 0; i < func->local_count; i++) {
+ if (wasm_is_type_reftype(func->local_types[i])
+ && !wasm_is_reftype_i31ref(func->local_types[i])) {
+ frame_ref[j++] = 1;
+#if UINTPTR_MAX == UINT64_MAX
+ frame_ref[j++] = 1;
+#endif
+ }
+ else {
+ j += wasm_value_type_cell_num(func->local_types[i]);
+ }
+ }
+}
+
+uint8 *
+wasm_interp_get_frame_ref(WASMInterpFrame *frame)
+{
+ return frame->frame_ref;
+}
+
+/* Return the corresponding ref slot of the given slot of local
+ variable or stack pointer. */
+
+#define COMPUTE_FRAME_REF(ref, off) (ref + (unsigned)(off))
+
+#define FRAME_REF(off) COMPUTE_FRAME_REF(frame_ref, off)
+
+#if UINTPTR_MAX == UINT64_MAX
+#define SET_FRAME_REF(off) *FRAME_REF(off) = *FRAME_REF(off + 1) = 1
+#define CLEAR_FRAME_REF(off) \
+ (unsigned)off >= local_cell_num \
+ ? (*FRAME_REF(off) = *FRAME_REF(off + 1) = 0) \
+ : (void)0
+#else
+#define SET_FRAME_REF(off) *FRAME_REF(off) = 1
+#define CLEAR_FRAME_REF(off) \
+ (unsigned)off >= local_cell_num ? (*FRAME_REF(off) = 0) : (void)0
+#endif
+
+#define FRAME_REF_FOR(frame, p) \
+ COMPUTE_FRAME_REF(frame->frame_ref, p - frame->lp)
+
+#define CLEAR_FRAME_REF_FOR(p, n) \
+ do { \
+ int32 ref_i, ref_n = (int32)(n); \
+ uint8 *ref = FRAME_REF(p - frame_lp); \
+ for (ref_i = 0; ref_i < ref_n; ref_i++) \
+ ref[ref_i] = 0; \
+ } while (0)
+#endif /* end of WASM_ENABLE_GC != 0 */
+
+#define read_uint32(p) \
+ (p += sizeof(uint32), LOAD_U32_WITH_2U16S(p - sizeof(uint32)))
+
+#define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
+ do { \
+ uint32 param_count = cur_func->param_count; \
+ local_idx = read_uint32(frame_ip); \
+ bh_assert(local_idx < param_count + cur_func->local_count); \
+ local_offset = cur_func->local_offsets[local_idx]; \
+ if (local_idx < param_count) \
+ local_type = cur_func->param_types[local_idx]; \
+ else \
+ local_type = cur_func->local_types[local_idx - param_count]; \
+ } while (0)
+
+#define GET_OFFSET() (frame_ip += 2, *(int16 *)(frame_ip - 2))
+
+#define SET_OPERAND_I32(off, value) \
+ do { \
+ *(uint32 *)(frame_lp + *(int16 *)(frame_ip + off)) = value; \
+ } while (0)
+#define SET_OPERAND_F32(off, value) \
+ do { \
+ *(float32 *)(frame_lp + *(int16 *)(frame_ip + off)) = value; \
+ } while (0)
+#define SET_OPERAND_I64(off, value) \
+ do { \
+ uint32 *addr_tmp = frame_lp + *(int16 *)(frame_ip + off); \
+ PUT_I64_TO_ADDR(addr_tmp, value); \
+ } while (0)
+#define SET_OPERAND_F64(off, value) \
+ do { \
+ uint32 *addr_tmp = frame_lp + *(int16 *)(frame_ip + off); \
+ PUT_F64_TO_ADDR(addr_tmp, value); \
+ } while (0)
+#define SET_OPERAND_REF(off, value) \
+ do { \
+ uint32 *addr_tmp; \
+ opnd_off = *(int16 *)(frame_ip + off); \
+ addr_tmp = frame_lp + opnd_off; \
+ PUT_REF_TO_ADDR(addr_tmp, value); \
+ SET_FRAME_REF(ond_off); \
+ } while (0)
+
+#define SET_OPERAND(op_type, off, value) SET_OPERAND_##op_type(off, value)
+
+#define GET_OPERAND_I32(type, off) \
+ *(type *)(frame_lp + *(int16 *)(frame_ip + off))
+#define GET_OPERAND_F32(type, off) \
+ *(type *)(frame_lp + *(int16 *)(frame_ip + off))
+#define GET_OPERAND_I64(type, off) \
+ (type) GET_I64_FROM_ADDR(frame_lp + *(int16 *)(frame_ip + off))
+#define GET_OPERAND_F64(type, off) \
+ (type) GET_F64_FROM_ADDR(frame_lp + *(int16 *)(frame_ip + off))
+#define GET_OPERAND_REF(type, off) \
+ (type) GET_REF_FROM_ADDR(frame_lp + *(int16 *)(frame_ip + off))
+
+#define GET_OPERAND(type, op_type, off) GET_OPERAND_##op_type(type, off)
+
+#define PUSH_I32(value) \
+ do { \
+ *(int32 *)(frame_lp + GET_OFFSET()) = value; \
+ } while (0)
+
+#define PUSH_F32(value) \
+ do { \
+ *(float32 *)(frame_lp + GET_OFFSET()) = value; \
+ } while (0)
+
+#define PUSH_I64(value) \
+ do { \
+ uint32 *addr_tmp = frame_lp + GET_OFFSET(); \
+ PUT_I64_TO_ADDR(addr_tmp, value); \
+ } while (0)
+
+#define PUSH_F64(value) \
+ do { \
+ uint32 *addr_tmp = frame_lp + GET_OFFSET(); \
+ PUT_F64_TO_ADDR(addr_tmp, value); \
+ } while (0)
+
+#define PUSH_REF(value) \
+ do { \
+ uint32 *addr_tmp; \
+ opnd_off = GET_OFFSET(); \
+ addr_tmp = frame_lp + opnd_off; \
+ PUT_REF_TO_ADDR(addr_tmp, value); \
+ SET_FRAME_REF(opnd_off); \
+ } while (0)
+
+#define PUSH_I31REF(value) \
+ do { \
+ uint32 *addr_tmp; \
+ opnd_off = GET_OFFSET(); \
+ addr_tmp = frame_lp + opnd_off; \
+ PUT_REF_TO_ADDR(addr_tmp, value); \
+ } while (0)
+
+#define POP_I32() (*(int32 *)(frame_lp + GET_OFFSET()))
+
+#define POP_F32() (*(float32 *)(frame_lp + GET_OFFSET()))
+
+#define POP_I64() (GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()))
+
+#define POP_F64() (GET_F64_FROM_ADDR(frame_lp + GET_OFFSET()))
+
+#define POP_REF() \
+ (opnd_off = GET_OFFSET(), CLEAR_FRAME_REF((unsigned)(opnd_off)), \
+ GET_REF_FROM_ADDR(frame_lp + opnd_off))
+
+#if WASM_ENABLE_GC != 0
+#define SYNC_FRAME_REF() frame->frame_ref = frame_ref
+#define UPDATE_FRAME_REF() frame_ref = frame->frame_ref
+#else
+#define SYNC_FRAME_REF() (void)0
+#define UPDATE_FRAME_REF() (void)0
+#endif
+
+#define SYNC_ALL_TO_FRAME() \
+ do { \
+ frame->ip = frame_ip; \
+ SYNC_FRAME_REF(); \
+ } while (0)
+
+#define UPDATE_ALL_FROM_FRAME() \
+ do { \
+ frame_ip = frame->ip; \
+ UPDATE_FRAME_REF(); \
+ } while (0)
+
+#if WASM_ENABLE_LABELS_AS_VALUES != 0
+#define UPDATE_FRAME_IP_END() (void)0
+#else
+#define UPDATE_FRAME_IP_END() frame_ip_end = wasm_get_func_code_end(cur_func)
+#endif
+
+#if WASM_ENABLE_GC != 0
+#define RECOVER_FRAME_REF() frame_ref = frame->frame_ref
+#else
+#define RECOVER_FRAME_REF() (void)0
+#endif
+
+#define RECOVER_CONTEXT(new_frame) \
+ do { \
+ frame = (new_frame); \
+ cur_func = frame->function; \
+ prev_frame = frame->prev_frame; \
+ frame_ip = frame->ip; \
+ UPDATE_FRAME_IP_END(); \
+ frame_lp = frame->lp; \
+ RECOVER_FRAME_REF(); \
+ } while (0)
+
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
+#define GET_OPCODE() opcode = *frame_ip++;
+#else
+#define GET_OPCODE() \
+ opcode = *frame_ip; \
+ frame_ip += 2;
+#endif
+
+#define DEF_OP_EQZ(ctype, src_op_type) \
+ do { \
+ SET_OPERAND(I32, 2, (GET_OPERAND(ctype, src_op_type, 0) == 0)); \
+ frame_ip += 4; \
+ } while (0)
+
+#define DEF_OP_CMP(src_type, src_op_type, cond) \
+ do { \
+ SET_OPERAND(I32, 4, \
+ GET_OPERAND(src_type, src_op_type, 2) \
+ cond GET_OPERAND(src_type, src_op_type, 0)); \
+ frame_ip += 6; \
+ } while (0)
+
+#define DEF_OP_BIT_COUNT(src_type, src_op_type, operation) \
+ do { \
+ SET_OPERAND( \
+ src_op_type, 2, \
+ (src_type)operation(GET_OPERAND(src_type, src_op_type, 0))); \
+ frame_ip += 4; \
+ } while (0)
+
+#define DEF_OP_NUMERIC(src_type1, src_type2, src_op_type, operation) \
+ do { \
+ SET_OPERAND(src_op_type, 4, \
+ GET_OPERAND(src_type1, src_op_type, 2) \
+ operation GET_OPERAND(src_type2, src_op_type, 0)); \
+ frame_ip += 6; \
+ } while (0)
+
+#define DEF_OP_REINTERPRET(src_type, src_op_type) \
+ do { \
+ SET_OPERAND(src_op_type, 2, GET_OPERAND(src_type, src_op_type, 0)); \
+ frame_ip += 4; \
+ } while (0)
+
+#define DEF_OP_NUMERIC_64 DEF_OP_NUMERIC
+
+#define DEF_OP_NUMERIC2(src_type1, src_type2, src_op_type, operation) \
+ do { \
+ SET_OPERAND(src_op_type, 4, \
+ GET_OPERAND(src_type1, src_op_type, 2) operation( \
+ GET_OPERAND(src_type2, src_op_type, 0) % 32)); \
+ frame_ip += 6; \
+ } while (0)
+
+#define DEF_OP_NUMERIC2_64(src_type1, src_type2, src_op_type, operation) \
+ do { \
+ SET_OPERAND(src_op_type, 4, \
+ GET_OPERAND(src_type1, src_op_type, 2) operation( \
+ GET_OPERAND(src_type2, src_op_type, 0) % 64)); \
+ frame_ip += 6; \
+ } while (0)
+
+#define DEF_ATOMIC_RMW_OPCODE(OP_NAME, op) \
+ case WASM_OP_ATOMIC_RMW_I32_##OP_NAME: \
+ case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U: \
+ case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U: \
+ { \
+ uint32 readv, sval; \
+ \
+ sval = POP_I32(); \
+ addr = POP_I32(); \
+ \
+ if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U) { \
+ CHECK_MEMORY_OVERFLOW(1); \
+ CHECK_ATOMIC_MEMORY_ACCESS(1); \
+ \
+ shared_memory_lock(memory); \
+ readv = (uint32)(*(uint8 *)maddr); \
+ *(uint8 *)maddr = (uint8)(readv op sval); \
+ shared_memory_unlock(memory); \
+ } \
+ else if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U) { \
+ CHECK_MEMORY_OVERFLOW(2); \
+ CHECK_ATOMIC_MEMORY_ACCESS(2); \
+ \
+ shared_memory_lock(memory); \
+ readv = (uint32)LOAD_U16(maddr); \
+ STORE_U16(maddr, (uint16)(readv op sval)); \
+ shared_memory_unlock(memory); \
+ } \
+ else { \
+ CHECK_MEMORY_OVERFLOW(4); \
+ CHECK_ATOMIC_MEMORY_ACCESS(4); \
+ \
+ shared_memory_lock(memory); \
+ readv = LOAD_I32(maddr); \
+ STORE_U32(maddr, readv op sval); \
+ shared_memory_unlock(memory); \
+ } \
+ PUSH_I32(readv); \
+ break; \
+ } \
+ case WASM_OP_ATOMIC_RMW_I64_##OP_NAME: \
+ case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U: \
+ case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U: \
+ case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U: \
+ { \
+ uint64 readv, sval; \
+ \
+ sval = (uint64)POP_I64(); \
+ addr = POP_I32(); \
+ \
+ if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U) { \
+ CHECK_MEMORY_OVERFLOW(1); \
+ CHECK_ATOMIC_MEMORY_ACCESS(1); \
+ \
+ shared_memory_lock(memory); \
+ readv = (uint64)(*(uint8 *)maddr); \
+ *(uint8 *)maddr = (uint8)(readv op sval); \
+ shared_memory_unlock(memory); \
+ } \
+ else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U) { \
+ CHECK_MEMORY_OVERFLOW(2); \
+ CHECK_ATOMIC_MEMORY_ACCESS(2); \
+ \
+ shared_memory_lock(memory); \
+ readv = (uint64)LOAD_U16(maddr); \
+ STORE_U16(maddr, (uint16)(readv op sval)); \
+ shared_memory_unlock(memory); \
+ } \
+ else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U) { \
+ CHECK_MEMORY_OVERFLOW(4); \
+ CHECK_ATOMIC_MEMORY_ACCESS(4); \
+ \
+ shared_memory_lock(memory); \
+ readv = (uint64)LOAD_U32(maddr); \
+ STORE_U32(maddr, (uint32)(readv op sval)); \
+ shared_memory_unlock(memory); \
+ } \
+ else { \
+ uint64 op_result; \
+ CHECK_MEMORY_OVERFLOW(8); \
+ CHECK_ATOMIC_MEMORY_ACCESS(8); \
+ \
+ shared_memory_lock(memory); \
+ readv = (uint64)LOAD_I64(maddr); \
+ op_result = readv op sval; \
+ STORE_I64(maddr, op_result); \
+ shared_memory_unlock(memory); \
+ } \
+ PUSH_I64(readv); \
+ break; \
+ }
+
+#define DEF_OP_MATH(src_type, src_op_type, method) \
+ do { \
+ SET_OPERAND(src_op_type, 2, \
+ (src_type)method(GET_OPERAND(src_type, src_op_type, 0))); \
+ frame_ip += 4; \
+ } while (0)
+
+#define TRUNC_FUNCTION(func_name, src_type, dst_type, signed_type) \
+ static dst_type func_name(src_type src_value, src_type src_min, \
+ src_type src_max, dst_type dst_min, \
+ dst_type dst_max, bool is_sign) \
+ { \
+ dst_type dst_value = 0; \
+ if (!isnan(src_value)) { \
+ if (src_value <= src_min) \
+ dst_value = dst_min; \
+ else if (src_value >= src_max) \
+ dst_value = dst_max; \
+ else { \
+ if (is_sign) \
+ dst_value = (dst_type)(signed_type)src_value; \
+ else \
+ dst_value = (dst_type)src_value; \
+ } \
+ } \
+ return dst_value; \
+ }
+
+TRUNC_FUNCTION(trunc_f32_to_i32, float32, uint32, int32)
+TRUNC_FUNCTION(trunc_f32_to_i64, float32, uint64, int64)
+TRUNC_FUNCTION(trunc_f64_to_i32, float64, uint32, int32)
+TRUNC_FUNCTION(trunc_f64_to_i64, float64, uint64, int64)
+
+static bool
+trunc_f32_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp,
+ float32 src_min, float32 src_max, bool saturating, bool is_i32,
+ bool is_sign)
+{
+ float32 src_value = GET_OPERAND(float32, F32, 0);
+ uint64 dst_value_i64;
+ uint32 dst_value_i32;
+
+ if (!saturating) {
+ if (isnan(src_value)) {
+ wasm_set_exception(module, "invalid conversion to integer");
+ return false;
+ }
+ else if (src_value <= src_min || src_value >= src_max) {
+ wasm_set_exception(module, "integer overflow");
+ return false;
+ }
+ }
+
+ if (is_i32) {
+ uint32 dst_min = is_sign ? INT32_MIN : 0;
+ uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX;
+ dst_value_i32 = trunc_f32_to_i32(src_value, src_min, src_max, dst_min,
+ dst_max, is_sign);
+ SET_OPERAND(I32, 2, dst_value_i32);
+ }
+ else {
+ uint64 dst_min = is_sign ? INT64_MIN : 0;
+ uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX;
+ dst_value_i64 = trunc_f32_to_i64(src_value, src_min, src_max, dst_min,
+ dst_max, is_sign);
+ SET_OPERAND(I64, 2, dst_value_i64);
+ }
+ return true;
+}
+
+static bool
+trunc_f64_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp,
+ float64 src_min, float64 src_max, bool saturating, bool is_i32,
+ bool is_sign)
+{
+ float64 src_value = GET_OPERAND(float64, F64, 0);
+ uint64 dst_value_i64;
+ uint32 dst_value_i32;
+
+ if (!saturating) {
+ if (isnan(src_value)) {
+ wasm_set_exception(module, "invalid conversion to integer");
+ return false;
+ }
+ else if (src_value <= src_min || src_value >= src_max) {
+ wasm_set_exception(module, "integer overflow");
+ return false;
+ }
+ }
+
+ if (is_i32) {
+ uint32 dst_min = is_sign ? INT32_MIN : 0;
+ uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX;
+ dst_value_i32 = trunc_f64_to_i32(src_value, src_min, src_max, dst_min,
+ dst_max, is_sign);
+ SET_OPERAND(I32, 2, dst_value_i32);
+ }
+ else {
+ uint64 dst_min = is_sign ? INT64_MIN : 0;
+ uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX;
+ dst_value_i64 = trunc_f64_to_i64(src_value, src_min, src_max, dst_min,
+ dst_max, is_sign);
+ SET_OPERAND(I64, 2, dst_value_i64);
+ }
+ return true;
+}
+
+#define DEF_OP_TRUNC_F32(min, max, is_i32, is_sign) \
+ do { \
+ if (!trunc_f32_to_int(module, frame_ip, frame_lp, min, max, false, \
+ is_i32, is_sign)) \
+ goto got_exception; \
+ frame_ip += 4; \
+ } while (0)
+
+#define DEF_OP_TRUNC_F64(min, max, is_i32, is_sign) \
+ do { \
+ if (!trunc_f64_to_int(module, frame_ip, frame_lp, min, max, false, \
+ is_i32, is_sign)) \
+ goto got_exception; \
+ frame_ip += 4; \
+ } while (0)
+
+#define DEF_OP_TRUNC_SAT_F32(min, max, is_i32, is_sign) \
+ do { \
+ (void)trunc_f32_to_int(module, frame_ip, frame_lp, min, max, true, \
+ is_i32, is_sign); \
+ frame_ip += 4; \
+ } while (0)
+
+#define DEF_OP_TRUNC_SAT_F64(min, max, is_i32, is_sign) \
+ do { \
+ (void)trunc_f64_to_int(module, frame_ip, frame_lp, min, max, true, \
+ is_i32, is_sign); \
+ frame_ip += 4; \
+ } while (0)
+
+#define DEF_OP_CONVERT(dst_type, dst_op_type, src_type, src_op_type) \
+ do { \
+ dst_type value = (dst_type)(src_type)POP_##src_op_type(); \
+ PUSH_##dst_op_type(value); \
+ } while (0)
+
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
+#define CELL_SIZE sizeof(uint8)
+#else
+#define CELL_SIZE (sizeof(uint8) * 2)
+#endif
+
+static bool
+copy_stack_values(WASMModuleInstance *module, uint32 *frame_lp, uint32 arity,
+#if WASM_ENABLE_GC != 0
+ uint8 *frame_ref,
+#endif
+ uint32 total_cell_num, const uint8 *cells,
+ const int16 *src_offsets, const uint16 *dst_offsets)
+{
+ /* To avoid the overlap issue between src offsets and dst offset,
+ * we use 2 steps to do the copy. First step, copy the src values
+ * to a tmp buf. Second step, copy the values from tmp buf to dst.
+ */
+ bool ret = false;
+ uint32 buf[16] = { 0 }, i;
+ uint32 *tmp_buf = buf;
+ uint8 cell;
+ int16 src, buf_index = 0;
+ uint16 dst;
+#if WASM_ENABLE_GC != 0
+ uint8 ref_buf[4];
+ uint8 *tmp_ref_buf = ref_buf;
+#endif
+
+ /* Allocate memory if the buf is not large enough */
+ if (total_cell_num > sizeof(buf) / sizeof(uint32)) {
+ uint64 total_size = sizeof(uint32) * (uint64)total_cell_num;
+ if (total_size >= UINT32_MAX
+ || !(tmp_buf = wasm_runtime_malloc((uint32)total_size))) {
+ wasm_set_exception(module, "allocate memory failed");
+ goto fail;
+ }
+ }
+
+#if WASM_ENABLE_GC != 0
+ if (total_cell_num > sizeof(ref_buf) / sizeof(uint8)) {
+ uint64 total_size = sizeof(uint8) * (uint64)total_cell_num;
+ if (total_size >= UINT32_MAX
+ || !(tmp_ref_buf = wasm_runtime_malloc((uint32)total_size))) {
+ wasm_set_exception(module, "allocate memory failed");
+ goto fail;
+ }
+ }
+#endif
+
+ /* 1) Copy values from src to tmp buf */
+ for (i = 0; i < arity; i++) {
+ cell = cells[i * CELL_SIZE];
+ src = src_offsets[i];
+ if (cell == 1) {
+ tmp_buf[buf_index] = frame_lp[src];
+#if WASM_ENABLE_GC != 0
+ tmp_ref_buf[buf_index] = frame_ref[src];
+ frame_ref[src] = 0;
+#endif
+ }
+ else {
+ tmp_buf[buf_index] = frame_lp[src];
+ tmp_buf[buf_index + 1] = frame_lp[src + 1];
+#if WASM_ENABLE_GC != 0
+ tmp_ref_buf[buf_index] = frame_ref[src];
+ tmp_ref_buf[buf_index + 1] = frame_ref[src + 1];
+ frame_ref[src] = 0;
+ frame_ref[src + 1] = 0;
+#endif
+ }
+ buf_index += cell;
+ }
+
+ /* 2) Copy values from tmp buf to dest */
+ buf_index = 0;
+ for (i = 0; i < arity; i++) {
+ cell = cells[i * CELL_SIZE];
+ dst = dst_offsets[i];
+ if (cell == 1) {
+ frame_lp[dst] = tmp_buf[buf_index];
+#if WASM_ENABLE_GC != 0
+ frame_ref[dst] = tmp_ref_buf[buf_index];
+#endif
+ }
+ else {
+ frame_lp[dst] = tmp_buf[buf_index];
+ frame_lp[dst + 1] = tmp_buf[buf_index + 1];
+#if WASM_ENABLE_GC != 0
+ frame_ref[dst] = tmp_ref_buf[buf_index];
+ frame_ref[dst + 1] = tmp_ref_buf[buf_index + 1];
+#endif
+ }
+ buf_index += cell;
+ }
+
+ ret = true;
+
+fail:
+ if (tmp_buf != buf) {
+ wasm_runtime_free(tmp_buf);
+ }
+
+#if WASM_ENABLE_GC != 0
+ if (tmp_ref_buf != ref_buf) {
+ wasm_runtime_free(tmp_ref_buf);
+ }
+#endif
+
+ return ret;
+}
+
+#if WASM_ENABLE_GC != 0
+#define RECOVER_BR_INFO() \
+ do { \
+ uint32 arity; \
+ /* read arity */ \
+ arity = read_uint32(frame_ip); \
+ if (arity) { \
+ uint32 total_cell; \
+ uint16 *dst_offsets = NULL; \
+ uint8 *cells; \
+ int16 *src_offsets = NULL; \
+ /* read total cell num */ \
+ total_cell = read_uint32(frame_ip); \
+ /* cells */ \
+ cells = (uint8 *)frame_ip; \
+ frame_ip += arity * CELL_SIZE; \
+ /* src offsets */ \
+ src_offsets = (int16 *)frame_ip; \
+ frame_ip += arity * sizeof(int16); \
+ /* dst offsets */ \
+ dst_offsets = (uint16 *)frame_ip; \
+ frame_ip += arity * sizeof(uint16); \
+ if (arity == 1) { \
+ if (cells[0] == 1) { \
+ frame_lp[dst_offsets[0]] = frame_lp[src_offsets[0]]; \
+ /* Ignore constants because they are not reference */ \
+ if (src_offsets[0] >= 0) { \
+ CLEAR_FRAME_REF((unsigned)(src_offsets[0])); \
+ SET_FRAME_REF(dst_offsets[0]); \
+ } \
+ } \
+ else if (cells[0] == 2) { \
+ PUT_I64_TO_ADDR( \
+ frame_lp + dst_offsets[0], \
+ GET_I64_FROM_ADDR(frame_lp + src_offsets[0])); \
+ /* Ignore constants because they are not reference */ \
+ if (src_offsets[0] >= 0) { \
+ CLEAR_FRAME_REF((unsigned)src_offsets[0]); \
+ CLEAR_FRAME_REF((unsigned)(src_offsets[0] + 1)); \
+ SET_FRAME_REF((unsigned)dst_offsets[0]); \
+ SET_FRAME_REF((unsigned)(dst_offsets[0] + 1)); \
+ } \
+ } \
+ } \
+ else { \
+ if (!copy_stack_values(module, frame_lp, arity, frame_ref, \
+ total_cell, cells, src_offsets, \
+ dst_offsets)) \
+ goto got_exception; \
+ } \
+ } \
+ frame_ip = (uint8 *)LOAD_PTR(frame_ip); \
+ } while (0)
+#else
+#define RECOVER_BR_INFO() \
+ do { \
+ uint32 arity; \
+ /* read arity */ \
+ arity = read_uint32(frame_ip); \
+ if (arity) { \
+ uint32 total_cell; \
+ uint16 *dst_offsets = NULL; \
+ uint8 *cells; \
+ int16 *src_offsets = NULL; \
+ /* read total cell num */ \
+ total_cell = read_uint32(frame_ip); \
+ /* cells */ \
+ cells = (uint8 *)frame_ip; \
+ frame_ip += arity * CELL_SIZE; \
+ /* src offsets */ \
+ src_offsets = (int16 *)frame_ip; \
+ frame_ip += arity * sizeof(int16); \
+ /* dst offsets */ \
+ dst_offsets = (uint16 *)frame_ip; \
+ frame_ip += arity * sizeof(uint16); \
+ if (arity == 1) { \
+ if (cells[0] == 1) \
+ frame_lp[dst_offsets[0]] = frame_lp[src_offsets[0]]; \
+ else if (cells[0] == 2) { \
+ PUT_I64_TO_ADDR( \
+ frame_lp + dst_offsets[0], \
+ GET_I64_FROM_ADDR(frame_lp + src_offsets[0])); \
+ } \
+ } \
+ else { \
+ if (!copy_stack_values(module, frame_lp, arity, total_cell, \
+ cells, src_offsets, dst_offsets)) \
+ goto got_exception; \
+ } \
+ } \
+ frame_ip = (uint8 *)LOAD_PTR(frame_ip); \
+ } while (0)
+#endif
+
+#define SKIP_BR_INFO() \
+ do { \
+ uint32 arity; \
+ /* read and skip arity */ \
+ arity = read_uint32(frame_ip); \
+ if (arity) { \
+ /* skip total cell num */ \
+ frame_ip += sizeof(uint32); \
+ /* skip cells, src offsets and dst offsets */ \
+ frame_ip += (CELL_SIZE + sizeof(int16) + sizeof(uint16)) * arity; \
+ } \
+ /* skip target address */ \
+ frame_ip += sizeof(uint8 *); \
+ } while (0)
+
+static inline int32
+sign_ext_8_32(int8 val)
+{
+ if (val & 0x80)
+ return (int32)val | (int32)0xffffff00;
+ return val;
+}
+
+static inline int32
+sign_ext_16_32(int16 val)
+{
+ if (val & 0x8000)
+ return (int32)val | (int32)0xffff0000;
+ return val;
+}
+
+static inline int64
+sign_ext_8_64(int8 val)
+{
+ if (val & 0x80)
+ return (int64)val | (int64)0xffffffffffffff00LL;
+ return val;
+}
+
+static inline int64
+sign_ext_16_64(int16 val)
+{
+ if (val & 0x8000)
+ return (int64)val | (int64)0xffffffffffff0000LL;
+ return val;
+}
+
+static inline int64
+sign_ext_32_64(int32 val)
+{
+ if (val & (int32)0x80000000)
+ return (int64)val | (int64)0xffffffff00000000LL;
+ return val;
+}
+
+static inline void
+word_copy(uint32 *dest, uint32 *src, unsigned num)
+{
+ bh_assert(dest != NULL);
+ bh_assert(src != NULL);
+ bh_assert(num > 0);
+ if (dest != src) {
+ /* No overlap buffer */
+ bh_assert(!((src < dest) && (dest < src + num)));
+ for (; num > 0; num--)
+ *dest++ = *src++;
+ }
+}
+
+static inline WASMInterpFrame *
+ALLOC_FRAME(WASMExecEnv *exec_env, uint32 size, WASMInterpFrame *prev_frame)
+{
+ WASMInterpFrame *frame = wasm_exec_env_alloc_wasm_frame(exec_env, size);
+
+ if (frame) {
+ frame->prev_frame = prev_frame;
+#if WASM_ENABLE_PERF_PROFILING != 0
+ frame->time_started = os_time_thread_cputime_us();
+#endif
+ }
+ else {
+ wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
+ "wasm operand stack overflow");
+ }
+
+ return frame;
+}
+
+static inline void
+FREE_FRAME(WASMExecEnv *exec_env, WASMInterpFrame *frame)
+{
+#if WASM_ENABLE_PERF_PROFILING != 0
+ if (frame->function) {
+ WASMInterpFrame *prev_frame = frame->prev_frame;
+ uint64 time_elapsed = os_time_thread_cputime_us() - frame->time_started;
+
+ frame->function->total_exec_time += time_elapsed;
+ frame->function->total_exec_cnt++;
+
+ /* parent function */
+ if (prev_frame && prev_frame->function)
+ prev_frame->function->children_exec_time += time_elapsed;
+ }
+#endif
+ wasm_exec_env_free_wasm_frame(exec_env, frame);
+}
+
+static void
+wasm_interp_call_func_native(WASMModuleInstance *module_inst,
+ WASMExecEnv *exec_env,
+ WASMFunctionInstance *cur_func,
+ WASMInterpFrame *prev_frame)
+{
+ WASMFunctionImport *func_import = cur_func->u.func_import;
+ CApiFuncImport *c_api_func_import = NULL;
+ unsigned local_cell_num =
+ cur_func->param_cell_num > 2 ? cur_func->param_cell_num : 2;
+ unsigned all_cell_num;
+ WASMInterpFrame *frame;
+ uint32 argv_ret[2], cur_func_index;
+ void *native_func_pointer = NULL;
+ bool ret;
+#if WASM_ENABLE_GC != 0
+ WASMFuncType *func_type;
+ uint8 *frame_ref;
+#endif
+
+ all_cell_num = local_cell_num;
+#if WASM_ENABLE_GC != 0
+ all_cell_num += (local_cell_num + 3) / 4;
+#endif
+
+ if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
+ return;
+ }
+
+ if (!(frame =
+ ALLOC_FRAME(exec_env, wasm_interp_interp_frame_size(all_cell_num),
+ prev_frame)))
+ return;
+
+ frame->function = cur_func;
+ frame->ip = NULL;
+ frame->lp = frame->operand;
+#if WASM_ENABLE_GC != 0
+ frame->frame_ref = (uint8 *)(frame->lp + local_cell_num);
+ init_frame_refs(frame->frame_ref, local_cell_num, cur_func);
+#endif
+
+ wasm_exec_env_set_cur_frame(exec_env, frame);
+
+ cur_func_index = (uint32)(cur_func - module_inst->e->functions);
+ bh_assert(cur_func_index < module_inst->module->import_function_count);
+ if (!func_import->call_conv_wasm_c_api) {
+ native_func_pointer = module_inst->import_func_ptrs[cur_func_index];
+ }
+ else if (module_inst->c_api_func_imports) {
+ c_api_func_import = module_inst->c_api_func_imports + cur_func_index;
+ native_func_pointer = c_api_func_import->func_ptr_linked;
+ }
+
+ if (!native_func_pointer) {
+ char buf[128];
+ snprintf(buf, sizeof(buf),
+ "failed to call unlinked import function (%s, %s)",
+ func_import->module_name, func_import->field_name);
+ wasm_set_exception((WASMModuleInstance *)module_inst, buf);
+ return;
+ }
+
+ if (func_import->call_conv_wasm_c_api) {
+ ret = wasm_runtime_invoke_c_api_native(
+ (WASMModuleInstanceCommon *)module_inst, native_func_pointer,
+ func_import->func_type, cur_func->param_cell_num, frame->lp,
+ c_api_func_import->with_env_arg, c_api_func_import->env_arg);
+ if (ret) {
+ argv_ret[0] = frame->lp[0];
+ argv_ret[1] = frame->lp[1];
+ }
+ }
+ else if (!func_import->call_conv_raw) {
+ ret = wasm_runtime_invoke_native(
+ exec_env, native_func_pointer, func_import->func_type,
+ func_import->signature, func_import->attachment, frame->lp,
+ cur_func->param_cell_num, argv_ret);
+ }
+ else {
+ ret = wasm_runtime_invoke_native_raw(
+ exec_env, native_func_pointer, func_import->func_type,
+ func_import->signature, func_import->attachment, frame->lp,
+ cur_func->param_cell_num, argv_ret);
+ }
+
+ if (!ret)
+ return;
+
+#if WASM_ENABLE_GC != 0
+ func_type = cur_func->u.func_import->func_type;
+ if (func_type->result_count
+ && wasm_is_type_reftype(func_type->types[cur_func->param_count])
+ && !wasm_is_reftype_i31ref(func_type->types[cur_func->param_count])) {
+ frame_ref = prev_frame->frame_ref + prev_frame->ret_offset;
+#if UINTPTR_MAX == UINT64_MAX
+ *frame_ref = *(frame_ref + 1) = 1;
+#else
+ *frame_ref = 1;
+#endif
+ }
+#endif
+
+ if (cur_func->ret_cell_num == 1) {
+ prev_frame->lp[prev_frame->ret_offset] = argv_ret[0];
+ }
+ else if (cur_func->ret_cell_num == 2) {
+ prev_frame->lp[prev_frame->ret_offset] = argv_ret[0];
+ prev_frame->lp[prev_frame->ret_offset + 1] = argv_ret[1];
+ }
+
+ FREE_FRAME(exec_env, frame);
+ wasm_exec_env_set_cur_frame(exec_env, prev_frame);
+}
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+static void
+wasm_interp_call_func_bytecode(WASMModuleInstance *module,
+ WASMExecEnv *exec_env,
+ WASMFunctionInstance *cur_func,
+ WASMInterpFrame *prev_frame);
+
+static void
+wasm_interp_call_func_import(WASMModuleInstance *module_inst,
+ WASMExecEnv *exec_env,
+ WASMFunctionInstance *cur_func,
+ WASMInterpFrame *prev_frame)
+{
+ WASMModuleInstance *sub_module_inst = cur_func->import_module_inst;
+ WASMFunctionInstance *sub_func_inst = cur_func->import_func_inst;
+ WASMFunctionImport *func_import = cur_func->u.func_import;
+ uint8 *ip = prev_frame->ip;
+ char buf[128];
+ WASMExecEnv *sub_module_exec_env = NULL;
+ uintptr_t aux_stack_origin_boundary = 0;
+ uintptr_t aux_stack_origin_bottom = 0;
+
+ /*
+ * perform stack overflow check before calling
+ * wasm_interp_call_func_bytecode recursively.
+ */
+ if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
+ return;
+ }
+
+ if (!sub_func_inst) {
+ snprintf(buf, sizeof(buf),
+ "failed to call unlinked import function (%s, %s)",
+ func_import->module_name, func_import->field_name);
+ wasm_set_exception(module_inst, buf);
+ return;
+ }
+
+ /* Switch exec_env but keep using the same one by replacing necessary
+ * variables */
+ sub_module_exec_env = wasm_runtime_get_exec_env_singleton(
+ (WASMModuleInstanceCommon *)sub_module_inst);
+ if (!sub_module_exec_env) {
+ wasm_set_exception(module_inst, "create singleton exec_env failed");
+ return;
+ }
+
+ /* - module_inst */
+ wasm_exec_env_set_module_inst(exec_env,
+ (WASMModuleInstanceCommon *)sub_module_inst);
+ /* - aux_stack_boundary */
+ aux_stack_origin_boundary = exec_env->aux_stack_boundary;
+ exec_env->aux_stack_boundary = sub_module_exec_env->aux_stack_boundary;
+ /* - aux_stack_bottom */
+ aux_stack_origin_bottom = exec_env->aux_stack_bottom;
+ exec_env->aux_stack_bottom = sub_module_exec_env->aux_stack_bottom;
+
+ /* set ip NULL to make call_func_bytecode return after executing
+ this function */
+ prev_frame->ip = NULL;
+
+ /* call function of sub-module*/
+ wasm_interp_call_func_bytecode(sub_module_inst, exec_env, sub_func_inst,
+ prev_frame);
+
+ /* restore ip and other replaced */
+ prev_frame->ip = ip;
+ exec_env->aux_stack_boundary = aux_stack_origin_boundary;
+ exec_env->aux_stack_bottom = aux_stack_origin_bottom;
+ wasm_exec_env_restore_module_inst(exec_env,
+ (WASMModuleInstanceCommon *)module_inst);
+}
+#endif
+
+#if WASM_ENABLE_THREAD_MGR != 0
+#define CHECK_SUSPEND_FLAGS() \
+ do { \
+ WASM_SUSPEND_FLAGS_LOCK(exec_env->wait_lock); \
+ if (WASM_SUSPEND_FLAGS_GET(exec_env->suspend_flags) \
+ & WASM_SUSPEND_FLAG_TERMINATE) { \
+ /* terminate current thread */ \
+ WASM_SUSPEND_FLAGS_UNLOCK(exec_env->wait_lock); \
+ return; \
+ } \
+ /* TODO: support suspend and breakpoint */ \
+ WASM_SUSPEND_FLAGS_UNLOCK(exec_env->wait_lock); \
+ } while (0)
+#endif
+
+#if WASM_ENABLE_OPCODE_COUNTER != 0
+typedef struct OpcodeInfo {
+ char *name;
+ uint64 count;
+} OpcodeInfo;
+
+/* clang-format off */
+#define HANDLE_OPCODE(op) \
+ { \
+ #op, 0 \
+ }
+DEFINE_GOTO_TABLE(OpcodeInfo, opcode_table);
+#undef HANDLE_OPCODE
+/* clang-format on */
+
+static void
+wasm_interp_dump_op_count()
+{
+ uint32 i;
+ uint64 total_count = 0;
+ for (i = 0; i < WASM_OP_IMPDEP; i++)
+ total_count += opcode_table[i].count;
+
+ os_printf("total opcode count: %ld\n", total_count);
+ for (i = 0; i < WASM_OP_IMPDEP; i++)
+ if (opcode_table[i].count > 0)
+ os_printf("\t\t%s count:\t\t%ld,\t\t%.2f%%\n", opcode_table[i].name,
+ opcode_table[i].count,
+ opcode_table[i].count * 100.0f / total_count);
+}
+#endif
+
+#if WASM_ENABLE_LABELS_AS_VALUES != 0
+
+/* #define HANDLE_OP(opcode) HANDLE_##opcode:printf(#opcode"\n"); */
+#if WASM_ENABLE_OPCODE_COUNTER != 0
+#define HANDLE_OP(opcode) HANDLE_##opcode : opcode_table[opcode].count++;
+#else
+#define HANDLE_OP(opcode) HANDLE_##opcode:
+#endif
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
+#define FETCH_OPCODE_AND_DISPATCH() \
+ do { \
+ const void *p_label_addr = *(void **)frame_ip; \
+ frame_ip += sizeof(void *); \
+ goto *p_label_addr; \
+ } while (0)
+#else
+#if UINTPTR_MAX == UINT64_MAX
+#define FETCH_OPCODE_AND_DISPATCH() \
+ do { \
+ const void *p_label_addr; \
+ bh_assert(((uintptr_t)frame_ip & 1) == 0); \
+ /* int32 relative offset was emitted in 64-bit target */ \
+ p_label_addr = label_base + (int32)LOAD_U32_WITH_2U16S(frame_ip); \
+ frame_ip += sizeof(int32); \
+ goto *p_label_addr; \
+ } while (0)
+#else
+#define FETCH_OPCODE_AND_DISPATCH() \
+ do { \
+ const void *p_label_addr; \
+ bh_assert(((uintptr_t)frame_ip & 1) == 0); \
+ /* uint32 label address was emitted in 32-bit target */ \
+ p_label_addr = (void *)(uintptr_t)LOAD_U32_WITH_2U16S(frame_ip); \
+ frame_ip += sizeof(int32); \
+ goto *p_label_addr; \
+ } while (0)
+#endif
+#endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
+#define HANDLE_OP_END() FETCH_OPCODE_AND_DISPATCH()
+
+#else /* else of WASM_ENABLE_LABELS_AS_VALUES */
+
+#define HANDLE_OP(opcode) case opcode:
+#define HANDLE_OP_END() continue
+
+#endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
+
+#if WASM_ENABLE_LABELS_AS_VALUES != 0
+static void **global_handle_table;
+#endif
+
+static inline uint8 *
+get_global_addr(uint8 *global_data, WASMGlobalInstance *global)
+{
+#if WASM_ENABLE_MULTI_MODULE == 0
+ return global_data + global->data_offset;
+#else
+ return global->import_global_inst
+ ? global->import_module_inst->global_data
+ + global->import_global_inst->data_offset
+ : global_data + global->data_offset;
+#endif
+}
+
+static void
+wasm_interp_call_func_bytecode(WASMModuleInstance *module,
+ WASMExecEnv *exec_env,
+ WASMFunctionInstance *cur_func,
+ WASMInterpFrame *prev_frame)
+{
+ WASMMemoryInstance *memory = wasm_get_default_memory(module);
+#if !defined(OS_ENABLE_HW_BOUND_CHECK) \
+ || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
+ || WASM_ENABLE_BULK_MEMORY != 0
+ uint64 linear_mem_size = 0;
+ if (memory)
+#if WASM_ENABLE_THREAD_MGR == 0
+ linear_mem_size = memory->memory_data_size;
+#else
+ linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory);
+#endif
+#endif
+ WASMGlobalInstance *globals = module->e ? module->e->globals : NULL;
+ WASMGlobalInstance *global;
+ uint8 *global_data = module->global_data;
+ uint8 opcode_IMPDEP = WASM_OP_IMPDEP;
+ WASMInterpFrame *frame = NULL;
+ /* Points to this special opcode so as to jump to the
+ * call_method_from_entry. */
+ register uint8 *frame_ip = &opcode_IMPDEP; /* cache of frame->ip */
+ register uint32 *frame_lp = NULL; /* cache of frame->lp */
+#if WASM_ENABLE_LABELS_AS_VALUES != 0
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 && UINTPTR_MAX == UINT64_MAX
+ /* cache of label base addr */
+ register uint8 *label_base = &&HANDLE_WASM_OP_UNREACHABLE;
+#endif
+#endif
+#if WASM_ENABLE_GC != 0
+ register uint8 *frame_ref = NULL; /* cache of frame->ref */
+ uint32 local_cell_num = 0;
+ int16 opnd_off;
+#endif
+ uint8 *frame_ip_end = frame_ip + 1;
+ uint32 cond, count, fidx, tidx, frame_size = 0;
+ uint32 all_cell_num = 0;
+ int16 addr1, addr2, addr_ret = 0;
+ int32 didx, val;
+ uint8 *maddr = NULL;
+ uint32 local_idx, local_offset, global_idx;
+ uint8 opcode = 0, local_type, *global_addr;
+#if !defined(OS_ENABLE_HW_BOUND_CHECK) \
+ || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+#if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
+ bool disable_bounds_checks = !wasm_runtime_is_bounds_checks_enabled(
+ (WASMModuleInstanceCommon *)module);
+#else
+ bool disable_bounds_checks = false;
+#endif
+#endif
+#if WASM_ENABLE_GC != 0
+ WASMObjectRef gc_obj;
+ WASMStructObjectRef struct_obj;
+ WASMArrayObjectRef array_obj;
+ WASMFuncObjectRef func_obj;
+ WASMI31ObjectRef i31_obj;
+ WASMExternrefObjectRef externref_obj;
+ uint32 type_idx;
+#if WASM_ENABLE_STRINGREF != 0
+ WASMString str_obj;
+ WASMStringrefObjectRef stringref_obj;
+ WASMStringviewWTF8ObjectRef stringview_wtf8_obj;
+ WASMStringviewWTF16ObjectRef stringview_wtf16_obj;
+ WASMStringviewIterObjectRef stringview_iter_obj;
+#endif
+#endif
+#if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
+ bool is_return_call = false;
+#endif
+#if WASM_ENABLE_SHARED_HEAP != 0
+ WASMSharedHeap *shared_heap = module->e ? module->e->shared_heap : NULL;
+ uint8 *shared_heap_base_addr = shared_heap ? shared_heap->base_addr : NULL;
+ /*
+#if WASM_ENABLE_MEMORY64 != 0
+ uint64 shared_heap_start_off =
+ shared_heap ? (is_memory64 ? shared_heap->start_off_mem64
+ : shared_heap->start_off_mem32)
+ : 0;
+ uint64 shared_heap_end_off =
+ shared_heap ? (is_memory64 ? UINT64_MAX : UINT32_MAX) : 0;
+#else
+ */ /* TODO: uncomment the code when memory64 is enabled for fast-interp */
+ uint64 shared_heap_start_off =
+ shared_heap ? shared_heap->start_off_mem32 : 0;
+ uint64 shared_heap_end_off = shared_heap ? UINT32_MAX : 0;
+/* #endif */
+#endif /* end of WASM_ENABLE_SHARED_HEAP != 0 */
+
+#if WASM_ENABLE_LABELS_AS_VALUES != 0
+#define HANDLE_OPCODE(op) &&HANDLE_##op
+ DEFINE_GOTO_TABLE(const void *, handle_table);
+#undef HANDLE_OPCODE
+ if (exec_env == NULL) {
+ global_handle_table = (void **)handle_table;
+ return;
+ }
+#endif
+
+#if WASM_ENABLE_LABELS_AS_VALUES == 0
+ while (frame_ip < frame_ip_end) {
+ opcode = *frame_ip++;
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+ frame_ip++;
+#endif
+ switch (opcode) {
+#else
+ goto *handle_table[WASM_OP_IMPDEP];
+#endif
+ /* control instructions */
+ HANDLE_OP(WASM_OP_UNREACHABLE)
+ {
+ wasm_set_exception(module, "unreachable");
+ goto got_exception;
+ }
+
+ HANDLE_OP(WASM_OP_IF)
+ {
+ cond = (uint32)POP_I32();
+
+ if (cond == 0) {
+ uint8 *else_addr = (uint8 *)LOAD_PTR(frame_ip);
+ if (else_addr == NULL) {
+ frame_ip =
+ (uint8 *)LOAD_PTR(frame_ip + sizeof(uint8 *));
+ }
+ else {
+ frame_ip = else_addr;
+ }
+ }
+ else {
+ frame_ip += sizeof(uint8 *) * 2;
+ }
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_ELSE)
+ {
+ frame_ip = (uint8 *)LOAD_PTR(frame_ip);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_BR)
+ {
+#if WASM_ENABLE_THREAD_MGR != 0
+ CHECK_SUSPEND_FLAGS();
+#endif
+ recover_br_info:
+ RECOVER_BR_INFO();
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_BR_IF)
+ {
+#if WASM_ENABLE_THREAD_MGR != 0
+ CHECK_SUSPEND_FLAGS();
+#endif
+ cond = frame_lp[GET_OFFSET()];
+
+ if (cond)
+ goto recover_br_info;
+ else
+ SKIP_BR_INFO();
+
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_BR_TABLE)
+ {
+ uint32 arity, br_item_size;
+
+#if WASM_ENABLE_THREAD_MGR != 0
+ CHECK_SUSPEND_FLAGS();
+#endif
+ count = read_uint32(frame_ip);
+ didx = GET_OPERAND(uint32, I32, 0);
+ frame_ip += 2;
+
+ if (!(didx >= 0 && (uint32)didx < count))
+ didx = count;
+
+ /* all br items must have the same arity and item size,
+ so we only calculate the first item size */
+ arity = LOAD_U32_WITH_2U16S(frame_ip);
+ br_item_size = sizeof(uint32); /* arity */
+ if (arity) {
+ /* total cell num */
+ br_item_size += sizeof(uint32);
+ /* cells, src offsets and dst offsets */
+ br_item_size +=
+ (CELL_SIZE + sizeof(int16) + sizeof(uint16)) * arity;
+ }
+ /* target address */
+ br_item_size += sizeof(uint8 *);
+
+ frame_ip += br_item_size * didx;
+ goto recover_br_info;
+ }
+
+ HANDLE_OP(WASM_OP_RETURN)
+ {
+ uint32 ret_idx;
+ WASMFuncType *func_type;
+ uint32 off, ret_offset;
+ uint8 *ret_types;
+ if (cur_func->is_import_func)
+ func_type = cur_func->u.func_import->func_type;
+ else
+ func_type = cur_func->u.func->func_type;
+
+ /* types of each return value */
+ ret_types = func_type->types + func_type->param_count;
+ ret_offset = prev_frame->ret_offset;
+
+ for (ret_idx = 0,
+ off = sizeof(int16) * (func_type->result_count - 1);
+ ret_idx < func_type->result_count;
+ ret_idx++, off -= sizeof(int16)) {
+ if (ret_types[ret_idx] == VALUE_TYPE_I64
+ || ret_types[ret_idx] == VALUE_TYPE_F64) {
+ PUT_I64_TO_ADDR(prev_frame->lp + ret_offset,
+ GET_OPERAND(uint64, I64, off));
+ ret_offset += 2;
+ }
+#if WASM_ENABLE_GC != 0
+ else if (wasm_is_type_reftype(ret_types[ret_idx])) {
+ PUT_REF_TO_ADDR(prev_frame->lp + ret_offset,
+ GET_OPERAND(void *, REF, off));
+ if (!wasm_is_reftype_i31ref(ret_types[ret_idx])) {
+ *(prev_frame->frame_ref + ret_offset) = 1;
+#if UINTPTR_MAX == UINT64_MAX
+ *(prev_frame->frame_ref + ret_offset + 1) = 1;
+#endif
+ }
+ ret_offset += REF_CELL_NUM;
+ }
+#endif
+ else {
+ prev_frame->lp[ret_offset] =
+ GET_OPERAND(uint32, I32, off);
+ ret_offset++;
+ }
+ }
+ goto return_func;
+ }
+
+ HANDLE_OP(WASM_OP_CALL_INDIRECT)
+#if WASM_ENABLE_TAIL_CALL != 0
+ HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
+#endif
+ {
+ WASMFuncType *cur_type, *cur_func_type;
+ WASMTableInstance *tbl_inst;
+ uint32 tbl_idx;
+
+#if WASM_ENABLE_TAIL_CALL != 0
+ GET_OPCODE();
+#endif
+#if WASM_ENABLE_THREAD_MGR != 0
+ CHECK_SUSPEND_FLAGS();
+#endif
+
+ tidx = read_uint32(frame_ip);
+ cur_type = (WASMFuncType *)module->module->types[tidx];
+
+ tbl_idx = read_uint32(frame_ip);
+ bh_assert(tbl_idx < module->table_count);
+
+ tbl_inst = wasm_get_table_inst(module, tbl_idx);
+
+ val = GET_OPERAND(uint32, I32, 0);
+ frame_ip += 2;
+
+ if ((uint32)val >= tbl_inst->cur_size) {
+ wasm_set_exception(module, "undefined element");
+ goto got_exception;
+ }
+
+ /* clang-format off */
+#if WASM_ENABLE_GC == 0
+ fidx = (uint32)tbl_inst->elems[val];
+ if (fidx == (uint32)-1) {
+ wasm_set_exception(module, "uninitialized element");
+ goto got_exception;
+ }
+#else
+ func_obj = (WASMFuncObjectRef)tbl_inst->elems[val];
+ if (!func_obj) {
+ wasm_set_exception(module, "uninitialized element");
+ goto got_exception;
+ }
+ fidx = wasm_func_obj_get_func_idx_bound(func_obj);
+#endif
+ /* clang-format on */
+
+ /*
+ * we might be using a table injected by host or
+ * another module. in that case, we don't validate
+ * the elem value while loading
+ */
+ if (fidx >= module->e->function_count) {
+ wasm_set_exception(module, "unknown function");
+ goto got_exception;
+ }
+
+ /* always call module own functions */
+ cur_func = module->e->functions + fidx;
+
+ if (cur_func->is_import_func)
+ cur_func_type = cur_func->u.func_import->func_type;
+ else
+ cur_func_type = cur_func->u.func->func_type;
+
+ /* clang-format off */
+#if WASM_ENABLE_GC == 0
+ if (cur_type != cur_func_type) {
+ wasm_set_exception(module, "indirect call type mismatch");
+ goto got_exception;
+ }
+#else
+ if (!wasm_func_type_is_super_of(cur_type, cur_func_type)) {
+ wasm_set_exception(module, "indirect call type mismatch");
+ goto got_exception;
+ }
+#endif
+ /* clang-format on */
+
+#if WASM_ENABLE_TAIL_CALL != 0
+ if (opcode == WASM_OP_RETURN_CALL_INDIRECT)
+ goto call_func_from_return_call;
+#endif
+ goto call_func_from_interp;
+ }
+
+#if WASM_ENABLE_EXCE_HANDLING != 0
+ HANDLE_OP(WASM_OP_TRY)
+ HANDLE_OP(WASM_OP_CATCH)
+ HANDLE_OP(WASM_OP_THROW)
+ HANDLE_OP(WASM_OP_RETHROW)
+ HANDLE_OP(WASM_OP_DELEGATE)
+ HANDLE_OP(WASM_OP_CATCH_ALL)
+ HANDLE_OP(EXT_OP_TRY)
+ {
+ wasm_set_exception(module, "unsupported opcode");
+ goto got_exception;
+ }
+#endif
+
+ /* parametric instructions */
+ HANDLE_OP(WASM_OP_SELECT)
+ {
+ cond = frame_lp[GET_OFFSET()];
+ addr1 = GET_OFFSET();
+ addr2 = GET_OFFSET();
+ addr_ret = GET_OFFSET();
+
+ if (!cond) {
+ if (addr_ret != addr1)
+ frame_lp[addr_ret] = frame_lp[addr1];
+ }
+ else {
+ if (addr_ret != addr2)
+ frame_lp[addr_ret] = frame_lp[addr2];
+ }
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_SELECT_64)
+ {
+ cond = frame_lp[GET_OFFSET()];
+ addr1 = GET_OFFSET();
+ addr2 = GET_OFFSET();
+ addr_ret = GET_OFFSET();
+
+ if (!cond) {
+ if (addr_ret != addr1)
+ PUT_I64_TO_ADDR(frame_lp + addr_ret,
+ GET_I64_FROM_ADDR(frame_lp + addr1));
+ }
+ else {
+ if (addr_ret != addr2)
+ PUT_I64_TO_ADDR(frame_lp + addr_ret,
+ GET_I64_FROM_ADDR(frame_lp + addr2));
+ }
+ HANDLE_OP_END();
+ }
+
+#if WASM_ENABLE_GC != 0
+ HANDLE_OP(WASM_OP_SELECT_T)
+ {
+ cond = frame_lp[GET_OFFSET()];
+ addr1 = GET_OFFSET();
+ addr2 = GET_OFFSET();
+ addr_ret = GET_OFFSET();
+
+ if (!cond) {
+ if (addr_ret != addr1)
+ PUT_REF_TO_ADDR(frame_lp + addr_ret,
+ GET_REF_FROM_ADDR(frame_lp + addr1));
+ }
+ else {
+ if (addr_ret != addr2)
+ PUT_REF_TO_ADDR(frame_lp + addr_ret,
+ GET_REF_FROM_ADDR(frame_lp + addr2));
+ }
+ {
+ uint8 orig_ref = 0;
+ /* Ignore constants because they are not reference */
+ if (addr1 >= 0) {
+ orig_ref = *FRAME_REF(addr1);
+ CLEAR_FRAME_REF(addr1);
+ }
+ if (addr2 >= 0) {
+ CLEAR_FRAME_REF(addr2);
+ }
+ if (orig_ref) {
+ SET_FRAME_REF(addr_ret);
+ }
+ }
+
+ HANDLE_OP_END();
+ }
+#endif
+
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+ HANDLE_OP(WASM_OP_TABLE_GET)
+ {
+ uint32 tbl_idx, elem_idx;
+ WASMTableInstance *tbl_inst;
+
+ tbl_idx = read_uint32(frame_ip);
+ bh_assert(tbl_idx < module->table_count);
+
+ tbl_inst = wasm_get_table_inst(module, tbl_idx);
+
+ elem_idx = POP_I32();
+ if (elem_idx >= tbl_inst->cur_size) {
+ wasm_set_exception(module, "out of bounds table access");
+ goto got_exception;
+ }
+
+#if WASM_ENABLE_GC == 0
+ PUSH_I32(tbl_inst->elems[elem_idx]);
+#else
+ PUSH_REF(tbl_inst->elems[elem_idx]);
+#endif
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_TABLE_SET)
+ {
+ uint32 tbl_idx, elem_idx;
+ WASMTableInstance *tbl_inst;
+ table_elem_type_t elem_val;
+
+ tbl_idx = read_uint32(frame_ip);
+ bh_assert(tbl_idx < module->table_count);
+
+ tbl_inst = wasm_get_table_inst(module, tbl_idx);
+
+#if WASM_ENABLE_GC == 0
+ elem_val = POP_I32();
+#else
+ elem_val = POP_REF();
+#endif
+ elem_idx = POP_I32();
+ if (elem_idx >= tbl_inst->cur_size) {
+ wasm_set_exception(module, "out of bounds table access");
+ goto got_exception;
+ }
+
+ tbl_inst->elems[elem_idx] = elem_val;
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_REF_NULL)
+ {
+#if WASM_ENABLE_GC == 0
+ PUSH_I32(NULL_REF);
+#else
+ PUSH_REF(NULL_REF);
+#endif
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_REF_IS_NULL)
+ {
+#if WASM_ENABLE_GC == 0
+ uint32 ref_val;
+ ref_val = POP_I32();
+#else
+ void *ref_val;
+ ref_val = POP_REF();
+#endif
+ PUSH_I32(ref_val == NULL_REF ? 1 : 0);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_REF_FUNC)
+ {
+ uint32 func_idx = read_uint32(frame_ip);
+
+#if WASM_ENABLE_GC == 0
+ PUSH_I32(func_idx);
+#else
+ SYNC_ALL_TO_FRAME();
+ if (!(gc_obj = wasm_create_func_obj(module, func_idx, true,
+ NULL, 0))) {
+ goto got_exception;
+ }
+ PUSH_REF(gc_obj);
+#endif
+ HANDLE_OP_END();
+ }
+#endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
+
+#if WASM_ENABLE_GC != 0
+ HANDLE_OP(WASM_OP_CALL_REF)
+ {
+#if WASM_ENABLE_THREAD_MGR != 0
+ CHECK_SUSPEND_FLAGS();
+#endif
+ func_obj = POP_REF();
+ if (!func_obj) {
+ wasm_set_exception(module, "null function object");
+ goto got_exception;
+ }
+
+ fidx = wasm_func_obj_get_func_idx_bound(func_obj);
+ cur_func = module->e->functions + fidx;
+ goto call_func_from_interp;
+ }
+ HANDLE_OP(WASM_OP_RETURN_CALL_REF)
+ {
+#if WASM_ENABLE_THREAD_MGR != 0
+ CHECK_SUSPEND_FLAGS();
+#endif
+ func_obj = POP_REF();
+ if (!func_obj) {
+ wasm_set_exception(module, "null function object");
+ goto got_exception;
+ }
+
+ fidx = wasm_func_obj_get_func_idx_bound(func_obj);
+ cur_func = module->e->functions + fidx;
+ goto call_func_from_return_call;
+ }
+ HANDLE_OP(WASM_OP_REF_AS_NON_NULL)
+ {
+ gc_obj = POP_REF();
+ if (gc_obj == NULL_REF) {
+ wasm_set_exception(module, "null reference");
+ goto got_exception;
+ }
+ PUSH_REF(gc_obj);
+ HANDLE_OP_END();
+ }
+ HANDLE_OP(WASM_OP_REF_EQ)
+ {
+ WASMObjectRef gc_obj1, gc_obj2;
+ gc_obj2 = POP_REF();
+ gc_obj1 = POP_REF();
+ val = wasm_obj_equal(gc_obj1, gc_obj2);
+ PUSH_I32(val);
+ HANDLE_OP_END();
+ }
+ HANDLE_OP(WASM_OP_BR_ON_NULL)
+ {
+#if WASM_ENABLE_THREAD_MGR != 0
+ CHECK_SUSPEND_FLAGS();
+#endif
+ opnd_off = GET_OFFSET();
+ gc_obj = GET_REF_FROM_ADDR(frame_lp + opnd_off);
+ if (gc_obj == NULL_REF) {
+ CLEAR_FRAME_REF(opnd_off);
+ goto recover_br_info;
+ }
+ else {
+ SKIP_BR_INFO();
+ }
+ HANDLE_OP_END();
+ }
+ HANDLE_OP(WASM_OP_BR_ON_NON_NULL)
+ {
+#if WASM_ENABLE_THREAD_MGR != 0
+ CHECK_SUSPEND_FLAGS();
+#endif
+ opnd_off = GET_OFFSET();
+ gc_obj = GET_REF_FROM_ADDR(frame_lp + opnd_off);
+ if (gc_obj != NULL_REF) {
+ goto recover_br_info;
+ }
+ else {
+ CLEAR_FRAME_REF(opnd_off);
+ SKIP_BR_INFO();
+ }
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_GC_PREFIX)
+ {
+ GET_OPCODE();
+
+ switch (opcode) {
+ case WASM_OP_STRUCT_NEW:
+ case WASM_OP_STRUCT_NEW_DEFAULT:
+ {
+ WASMModule *wasm_module = module->module;
+ WASMStructType *struct_type;
+ WASMRttType *rtt_type;
+ WASMValue field_value = { 0 };
+
+ type_idx = read_uint32(frame_ip);
+ struct_type =
+ (WASMStructType *)module->module->types[type_idx];
+
+ if (!(rtt_type = wasm_rtt_type_new(
+ (WASMType *)struct_type, type_idx,
+ wasm_module->rtt_types,
+ wasm_module->type_count,
+ &wasm_module->rtt_type_lock))) {
+ wasm_set_exception(module,
+ "create rtt type failed");
+ goto got_exception;
+ }
+
+ SYNC_ALL_TO_FRAME();
+ struct_obj = wasm_struct_obj_new(exec_env, rtt_type);
+ if (!struct_obj) {
+ wasm_set_exception(module,
+ "create struct object failed");
+ goto got_exception;
+ }
+
+ if (opcode == WASM_OP_STRUCT_NEW) {
+ WASMStructFieldType *fields = struct_type->fields;
+ int32 field_count = (int32)struct_type->field_count;
+ int32 field_idx;
+ uint8 field_type;
+
+ for (field_idx = field_count - 1; field_idx >= 0;
+ field_idx--) {
+ field_type = fields[field_idx].field_type;
+ if (wasm_is_type_reftype(field_type)) {
+ field_value.gc_obj = POP_REF();
+ }
+ else if (field_type == VALUE_TYPE_I32
+ || field_type == VALUE_TYPE_F32
+ || field_type == PACKED_TYPE_I8
+ || field_type == PACKED_TYPE_I16) {
+ field_value.i32 = POP_I32();
+ }
+ else {
+ field_value.i64 = POP_I64();
+ }
+ wasm_struct_obj_set_field(struct_obj, field_idx,
+ &field_value);
+ }
+ }
+ PUSH_REF(struct_obj);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRUCT_GET:
+ case WASM_OP_STRUCT_GET_S:
+ case WASM_OP_STRUCT_GET_U:
+ {
+ WASMStructType *struct_type;
+ WASMValue field_value = { 0 };
+ uint32 field_idx;
+ uint8 field_type;
+
+ type_idx = read_uint32(frame_ip);
+ field_idx = read_uint32(frame_ip);
+
+ struct_type =
+ (WASMStructType *)module->module->types[type_idx];
+
+ struct_obj = POP_REF();
+
+ if (!struct_obj) {
+ wasm_set_exception(module, "null structure object");
+ goto got_exception;
+ }
+
+ wasm_struct_obj_get_field(
+ struct_obj, field_idx,
+ opcode == WASM_OP_STRUCT_GET_S ? true : false,
+ &field_value);
+
+ field_type = struct_type->fields[field_idx].field_type;
+ if (wasm_is_reftype_i31ref(field_type)) {
+ PUSH_I31REF(field_value.gc_obj);
+ }
+ else if (wasm_is_type_reftype(field_type)) {
+ PUSH_REF(field_value.gc_obj);
+ }
+ else if (field_type == VALUE_TYPE_I32
+ || field_type == VALUE_TYPE_F32
+ || field_type == PACKED_TYPE_I8
+ || field_type == PACKED_TYPE_I16) {
+ PUSH_I32(field_value.i32);
+ }
+ else {
+ PUSH_I64(field_value.i64);
+ }
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRUCT_SET:
+ {
+ WASMStructType *struct_type;
+ WASMValue field_value = { 0 };
+ uint32 field_idx;
+ uint8 field_type;
+
+ type_idx = read_uint32(frame_ip);
+ field_idx = read_uint32(frame_ip);
+
+ struct_type =
+ (WASMStructType *)module->module->types[type_idx];
+ field_type = struct_type->fields[field_idx].field_type;
+
+ if (wasm_is_type_reftype(field_type)) {
+ field_value.gc_obj = POP_REF();
+ }
+ else if (field_type == VALUE_TYPE_I32
+ || field_type == VALUE_TYPE_F32
+ || field_type == PACKED_TYPE_I8
+ || field_type == PACKED_TYPE_I16) {
+ field_value.i32 = POP_I32();
+ }
+ else {
+ field_value.i64 = POP_I64();
+ }
+
+ struct_obj = POP_REF();
+ if (!struct_obj) {
+ wasm_set_exception(module, "null structure object");
+ goto got_exception;
+ }
+
+ wasm_struct_obj_set_field(struct_obj, field_idx,
+ &field_value);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_ARRAY_NEW:
+ case WASM_OP_ARRAY_NEW_DEFAULT:
+ case WASM_OP_ARRAY_NEW_FIXED:
+ {
+ WASMModule *wasm_module = module->module;
+ WASMArrayType *array_type;
+ WASMRttType *rtt_type;
+ WASMValue array_elem = { 0 };
+ uint32 array_len, i;
+
+ type_idx = read_uint32(frame_ip);
+ array_type =
+ (WASMArrayType *)wasm_module->types[type_idx];
+
+ if (!(rtt_type = wasm_rtt_type_new(
+ (WASMType *)array_type, type_idx,
+ wasm_module->rtt_types,
+ wasm_module->type_count,
+ &wasm_module->rtt_type_lock))) {
+ wasm_set_exception(module,
+ "create rtt type failed");
+ goto got_exception;
+ }
+
+ if (opcode != WASM_OP_ARRAY_NEW_FIXED)
+ array_len = POP_I32();
+ else
+ array_len = read_uint32(frame_ip);
+
+ if (opcode == WASM_OP_ARRAY_NEW) {
+ if (wasm_is_type_reftype(array_type->elem_type)) {
+ array_elem.gc_obj = POP_REF();
+ }
+ else if (array_type->elem_type == VALUE_TYPE_I32
+ || array_type->elem_type == VALUE_TYPE_F32
+ || array_type->elem_type == PACKED_TYPE_I8
+ || array_type->elem_type
+ == PACKED_TYPE_I16) {
+ array_elem.i32 = POP_I32();
+ }
+ else {
+ array_elem.i64 = POP_I64();
+ }
+ }
+
+ SYNC_ALL_TO_FRAME();
+ array_obj = wasm_array_obj_new(exec_env, rtt_type,
+ array_len, &array_elem);
+ if (!array_obj) {
+ wasm_set_exception(module,
+ "create array object failed");
+ goto got_exception;
+ }
+
+ if (opcode == WASM_OP_ARRAY_NEW_FIXED) {
+ for (i = 0; i < array_len; i++) {
+ if (wasm_is_type_reftype(
+ array_type->elem_type)) {
+ array_elem.gc_obj = POP_REF();
+ }
+ else if (array_type->elem_type == VALUE_TYPE_I32
+ || array_type->elem_type
+ == VALUE_TYPE_F32
+ || array_type->elem_type
+ == PACKED_TYPE_I8
+ || array_type->elem_type
+ == PACKED_TYPE_I16) {
+ array_elem.i32 = POP_I32();
+ }
+ else {
+ array_elem.i64 = POP_I64();
+ }
+ wasm_array_obj_set_elem(
+ array_obj, array_len - 1 - i, &array_elem);
+ }
+ }
+
+ PUSH_REF(array_obj);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_ARRAY_NEW_DATA:
+ {
+ WASMModule *wasm_module = module->module;
+ WASMArrayType *array_type;
+ WASMRttType *rtt_type;
+ WASMValue array_elem = { 0 };
+ WASMDataSeg *data_seg;
+ uint8 *array_elem_base;
+ uint32 array_len, data_seg_idx, data_seg_offset;
+ uint32 elem_size = 0;
+ uint64 total_size;
+
+ type_idx = read_uint32(frame_ip);
+ data_seg_idx = read_uint32(frame_ip);
+ data_seg = wasm_module->data_segments[data_seg_idx];
+
+ array_type =
+ (WASMArrayType *)wasm_module->types[type_idx];
+
+ if (!(rtt_type = wasm_rtt_type_new(
+ (WASMType *)array_type, type_idx,
+ wasm_module->rtt_types,
+ wasm_module->type_count,
+ &wasm_module->rtt_type_lock))) {
+ wasm_set_exception(module,
+ "create rtt type failed");
+ goto got_exception;
+ }
+
+ array_len = POP_I32();
+ data_seg_offset = POP_I32();
+
+ switch (array_type->elem_type) {
+ case PACKED_TYPE_I8:
+ elem_size = 1;
+ break;
+ case PACKED_TYPE_I16:
+ elem_size = 2;
+ break;
+ case VALUE_TYPE_I32:
+ case VALUE_TYPE_F32:
+ elem_size = 4;
+ break;
+ case VALUE_TYPE_I64:
+ case VALUE_TYPE_F64:
+ elem_size = 8;
+ break;
+ default:
+ bh_assert(0);
+ }
+
+ total_size = (uint64)elem_size * array_len;
+ if (data_seg_offset >= data_seg->data_length
+ || total_size
+ > data_seg->data_length - data_seg_offset) {
+ wasm_set_exception(module,
+ "data segment out of bounds");
+ goto got_exception;
+ }
+
+ SYNC_ALL_TO_FRAME();
+ array_obj = wasm_array_obj_new(exec_env, rtt_type,
+ array_len, &array_elem);
+ if (!array_obj) {
+ wasm_set_exception(module,
+ "create array object failed");
+ goto got_exception;
+ }
+
+ array_elem_base =
+ (uint8 *)wasm_array_obj_first_elem_addr(array_obj);
+ bh_memcpy_s(array_elem_base, (uint32)total_size,
+ data_seg->data + data_seg_offset,
+ (uint32)total_size);
+
+ PUSH_REF(array_obj);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_ARRAY_NEW_ELEM:
+ {
+ /* TODO */
+ wasm_set_exception(module, "unsupported opcode");
+ goto got_exception;
+ }
+ case WASM_OP_ARRAY_GET:
+ case WASM_OP_ARRAY_GET_S:
+ case WASM_OP_ARRAY_GET_U:
+ {
+ WASMArrayType *array_type;
+ WASMValue array_elem = { 0 };
+ uint32 elem_idx, elem_size_log;
+
+ type_idx = read_uint32(frame_ip);
+ array_type =
+ (WASMArrayType *)module->module->types[type_idx];
+
+ elem_idx = POP_I32();
+ array_obj = POP_REF();
+
+ if (!array_obj) {
+ wasm_set_exception(module, "null array reference");
+ goto got_exception;
+ }
+ if (elem_idx >= wasm_array_obj_length(array_obj)) {
+ wasm_set_exception(module,
+ "out of bounds array access");
+ goto got_exception;
+ }
+
+ wasm_array_obj_get_elem(
+ array_obj, elem_idx,
+ opcode == WASM_OP_ARRAY_GET_S ? true : false,
+ &array_elem);
+ elem_size_log = wasm_array_obj_elem_size_log(array_obj);
+
+ if (wasm_is_reftype_i31ref(array_type->elem_type)) {
+ PUSH_I31REF(array_elem.gc_obj);
+ }
+ else if (wasm_is_type_reftype(array_type->elem_type)) {
+ PUSH_REF(array_elem.gc_obj);
+ }
+ else if (elem_size_log < 3) {
+ PUSH_I32(array_elem.i32);
+ }
+ else {
+ PUSH_I64(array_elem.i64);
+ }
+ HANDLE_OP_END();
+ }
+ case WASM_OP_ARRAY_SET:
+ {
+ WASMArrayType *array_type;
+ WASMValue array_elem = { 0 };
+ uint32 elem_idx;
+
+ type_idx = read_uint32(frame_ip);
+ array_type =
+ (WASMArrayType *)module->module->types[type_idx];
+ if (wasm_is_type_reftype(array_type->elem_type)) {
+ array_elem.gc_obj = POP_REF();
+ }
+ else if (array_type->elem_type == VALUE_TYPE_I32
+ || array_type->elem_type == VALUE_TYPE_F32
+ || array_type->elem_type == PACKED_TYPE_I8
+ || array_type->elem_type == PACKED_TYPE_I16) {
+ array_elem.i32 = POP_I32();
+ }
+ else {
+ array_elem.i64 = POP_I64();
+ }
+
+ elem_idx = POP_I32();
+ array_obj = POP_REF();
+
+ if (!array_obj) {
+ wasm_set_exception(module, "null array reference");
+ goto got_exception;
+ }
+ if (elem_idx >= wasm_array_obj_length(array_obj)) {
+ wasm_set_exception(module,
+ "out of bounds array access");
+ goto got_exception;
+ }
+
+ wasm_array_obj_set_elem(array_obj, elem_idx,
+ &array_elem);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_ARRAY_LEN:
+ {
+ uint32 array_len;
+ array_obj = POP_REF();
+ if (!array_obj) {
+ wasm_set_exception(module, "null array reference");
+ goto got_exception;
+ }
+ array_len = wasm_array_obj_length(array_obj);
+ PUSH_I32(array_len);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_ARRAY_FILL:
+ {
+ WASMArrayType *array_type;
+ WASMValue fill_value = { 0 };
+ uint32 start_offset, len;
+
+ type_idx = read_uint32(frame_ip);
+
+ array_type =
+ (WASMArrayType *)module->module->types[type_idx];
+
+ len = POP_I32();
+ if (wasm_is_type_reftype(array_type->elem_type)) {
+ fill_value.gc_obj = POP_REF();
+ }
+ else if (array_type->elem_type == VALUE_TYPE_I32
+ || array_type->elem_type == VALUE_TYPE_F32
+ || array_type->elem_type == PACKED_TYPE_I8
+ || array_type->elem_type == PACKED_TYPE_I16) {
+ fill_value.i32 = POP_I32();
+ }
+ else {
+ fill_value.i64 = POP_I64();
+ }
+ start_offset = POP_I32();
+ array_obj = POP_REF();
+
+ if (!array_obj) {
+ wasm_set_exception(module, "null array reference");
+ goto got_exception;
+ }
+
+ if (len > 0) {
+ if ((uint64)start_offset + len
+ >= wasm_array_obj_length(array_obj)) {
+ wasm_set_exception(
+ module, "out of bounds array access");
+ goto got_exception;
+ }
+
+ wasm_array_obj_fill(array_obj, start_offset, len,
+ &fill_value);
+ }
+
+ HANDLE_OP_END();
+ }
+ case WASM_OP_ARRAY_COPY:
+ {
+ uint32 dst_offset, src_offset, len, src_type_index;
+ WASMArrayObjectRef src_obj, dst_obj;
+
+ type_idx = read_uint32(frame_ip);
+ src_type_index = read_uint32(frame_ip);
+
+ len = POP_I32();
+ src_offset = POP_I32();
+ src_obj = POP_REF();
+ dst_offset = POP_I32();
+ dst_obj = POP_REF();
+
+ if (!src_obj || !dst_obj) {
+ wasm_set_exception(module, "null array reference");
+ goto got_exception;
+ }
+
+ if (len > 0) {
+ if ((dst_offset > UINT32_MAX - len)
+ || (dst_offset + len
+ > wasm_array_obj_length(dst_obj))
+ || (src_offset > UINT32_MAX - len)
+ || (src_offset + len
+ > wasm_array_obj_length(src_obj))) {
+ wasm_set_exception(
+ module, "out of bounds array access");
+ goto got_exception;
+ }
+
+ wasm_array_obj_copy(dst_obj, dst_offset, src_obj,
+ src_offset, len);
+ }
+
+ (void)src_type_index;
+ HANDLE_OP_END();
+ }
+
+ case WASM_OP_REF_I31:
+ {
+ uint32 i31_val;
+
+ i31_val = POP_I32();
+ i31_obj = wasm_i31_obj_new(i31_val);
+ PUSH_I31REF(i31_obj);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_I31_GET_S:
+ case WASM_OP_I31_GET_U:
+ {
+ uint32 i31_val;
+
+ i31_obj = (WASMI31ObjectRef)POP_REF();
+ if (!i31_obj) {
+ wasm_set_exception(module, "null i31 reference");
+ goto got_exception;
+ }
+ i31_val = (uint32)(((uintptr_t)i31_obj) >> 1);
+ if (opcode == WASM_OP_I31_GET_S
+ && (i31_val & 0x40000000) /* bit 30 is 1 */)
+ /* set bit 31 to 1 */
+ i31_val |= 0x80000000;
+ PUSH_I32(i31_val);
+ HANDLE_OP_END();
+ }
+
+ case WASM_OP_REF_TEST:
+ case WASM_OP_REF_CAST:
+ case WASM_OP_REF_TEST_NULLABLE:
+ case WASM_OP_REF_CAST_NULLABLE:
+ {
+ int32 heap_type;
+
+ heap_type = (int32)read_uint32(frame_ip);
+
+ gc_obj = POP_REF();
+ if (!gc_obj) {
+ if (opcode == WASM_OP_REF_TEST
+ || opcode == WASM_OP_REF_TEST_NULLABLE) {
+ if (opcode == WASM_OP_REF_TEST)
+ PUSH_I32(0);
+ else
+ PUSH_I32(1);
+ }
+ else if (opcode == WASM_OP_REF_CAST) {
+ wasm_set_exception(module, "cast failure");
+ goto got_exception;
+ }
+ else {
+ PUSH_REF(gc_obj);
+ }
+ }
+ else {
+ bool castable = false;
+
+ if (heap_type >= 0) {
+ WASMModule *wasm_module = module->module;
+ castable = wasm_obj_is_instance_of(
+ gc_obj, (uint32)heap_type,
+ wasm_module->types,
+ wasm_module->type_count);
+ }
+ else {
+ castable =
+ wasm_obj_is_type_of(gc_obj, heap_type);
+ }
+
+ if (opcode == WASM_OP_REF_TEST
+ || opcode == WASM_OP_REF_TEST_NULLABLE) {
+ if (castable)
+ PUSH_I32(1);
+ else
+ PUSH_I32(0);
+ }
+ else if (!castable) {
+ wasm_set_exception(module, "cast failure");
+ goto got_exception;
+ }
+ else {
+ PUSH_REF(gc_obj);
+ }
+ }
+ HANDLE_OP_END();
+ }
+
+ case WASM_OP_BR_ON_CAST:
+ case WASM_OP_BR_ON_CAST_FAIL:
+ {
+ int32 heap_type, heap_type_dst;
+ uint8 castflags;
+ uint16 opnd_off_br;
+
+#if WASM_ENABLE_THREAD_MGR != 0
+ CHECK_SUSPEND_FLAGS();
+#endif
+ castflags = *frame_ip++;
+ heap_type = (int32)read_uint32(frame_ip);
+ heap_type_dst = (int32)read_uint32(frame_ip);
+
+ opnd_off = GET_OFFSET();
+ opnd_off_br = GET_OFFSET();
+ gc_obj = GET_REF_FROM_ADDR(frame_lp + opnd_off);
+ PUT_REF_TO_ADDR(frame_lp + opnd_off_br, gc_obj);
+
+ if (!gc_obj) {
+ /*
+ * castflags should be 0~3:
+ * 0: (non-null, non-null)
+ * 1: (null, non-null)
+ * 2: (non-null, null)
+ * 3: (null, null)
+ */
+ if (
+ /* op is BR_ON_CAST and dst reftype is nullable
+ */
+ ((opcode == WASM_OP_BR_ON_CAST)
+ && ((castflags == 2) || (castflags == 3)))
+ /* op is BR_ON_CAST_FAIL and dst reftype is
+ non-nullable */
+ || ((opcode == WASM_OP_BR_ON_CAST_FAIL)
+ && ((castflags == 0)
+ || (castflags == 1)))) {
+ CLEAR_FRAME_REF(opnd_off);
+ if (!wasm_is_reftype_i31ref(heap_type)) {
+ SET_FRAME_REF(opnd_off_br);
+ }
+ goto recover_br_info;
+ }
+ }
+ else {
+ bool castable = false;
+
+ if (heap_type_dst >= 0) {
+ WASMModule *wasm_module = module->module;
+ castable = wasm_obj_is_instance_of(
+ gc_obj, (uint32)heap_type_dst,
+ wasm_module->types,
+ wasm_module->type_count);
+ }
+ else {
+ castable =
+ wasm_obj_is_type_of(gc_obj, heap_type_dst);
+ }
+
+ if ((castable && (opcode == WASM_OP_BR_ON_CAST))
+ || (!castable
+ && (opcode == WASM_OP_BR_ON_CAST_FAIL))) {
+ CLEAR_FRAME_REF(opnd_off);
+ if (!wasm_is_reftype_i31ref(heap_type)) {
+ SET_FRAME_REF(opnd_off_br);
+ }
+ goto recover_br_info;
+ }
+ }
+ SKIP_BR_INFO();
+
+ (void)heap_type_dst;
+ HANDLE_OP_END();
+ }
+
+ case WASM_OP_ANY_CONVERT_EXTERN:
+ {
+ externref_obj = POP_REF();
+ if (externref_obj == NULL_REF)
+ PUSH_REF(NULL_REF);
+ else {
+ gc_obj = wasm_externref_obj_to_internal_obj(
+ externref_obj);
+ PUSH_REF(gc_obj);
+ }
+ HANDLE_OP_END();
+ }
+ case WASM_OP_EXTERN_CONVERT_ANY:
+ {
+ gc_obj = POP_REF();
+ if (gc_obj == NULL_REF)
+ PUSH_REF(NULL_REF);
+ else {
+ if (!(externref_obj =
+ wasm_internal_obj_to_externref_obj(
+ exec_env, gc_obj))) {
+ wasm_set_exception(
+ module, "create externref object failed");
+ goto got_exception;
+ }
+ PUSH_REF(externref_obj);
+ }
+ HANDLE_OP_END();
+ }
+
+#if WASM_ENABLE_STRINGREF != 0
+ case WASM_OP_STRING_NEW_UTF8:
+ case WASM_OP_STRING_NEW_WTF16:
+ case WASM_OP_STRING_NEW_LOSSY_UTF8:
+ case WASM_OP_STRING_NEW_WTF8:
+ {
+ uint32 mem_idx, addr, bytes_length, offset = 0;
+ EncodingFlag flag = WTF8;
+
+ mem_idx = (uint32)read_uint32(frame_ip);
+ bytes_length = POP_I32();
+ addr = POP_I32();
+
+ CHECK_MEMORY_OVERFLOW(bytes_length);
+
+ if (opcode == WASM_OP_STRING_NEW_WTF16) {
+ flag = WTF16;
+ }
+ else if (opcode == WASM_OP_STRING_NEW_UTF8) {
+ flag = UTF8;
+ }
+ else if (opcode == WASM_OP_STRING_NEW_LOSSY_UTF8) {
+ flag = LOSSY_UTF8;
+ }
+ else if (opcode == WASM_OP_STRING_NEW_WTF8) {
+ flag = WTF8;
+ }
+
+ str_obj = wasm_string_new_with_encoding(
+ maddr, bytes_length, flag);
+ if (!str_obj) {
+ wasm_set_exception(module,
+ "create string object failed");
+ goto got_exception;
+ }
+
+ SYNC_ALL_TO_FRAME();
+ stringref_obj =
+ wasm_stringref_obj_new(exec_env, str_obj);
+ if (!stringref_obj) {
+ wasm_set_exception(module,
+ "create stringref failed");
+ goto got_exception;
+ }
+
+ PUSH_REF(stringref_obj);
+
+ (void)mem_idx;
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRING_CONST:
+ {
+ WASMModule *wasm_module = module->module;
+ uint32 contents;
+
+ contents = (uint32)read_uint32(frame_ip);
+
+ str_obj = wasm_string_new_const(
+ (const char *)
+ wasm_module->string_literal_ptrs[contents],
+ wasm_module->string_literal_lengths[contents]);
+ if (!str_obj) {
+ wasm_set_exception(module,
+ "create string object failed");
+ goto got_exception;
+ }
+
+ SYNC_ALL_TO_FRAME();
+ stringref_obj =
+ wasm_stringref_obj_new(exec_env, str_obj);
+ if (!str_obj) {
+ wasm_set_exception(module,
+ "create stringref failed");
+ goto got_exception;
+ }
+
+ PUSH_REF(stringref_obj);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRING_MEASURE_UTF8:
+ case WASM_OP_STRING_MEASURE_WTF8:
+ case WASM_OP_STRING_MEASURE_WTF16:
+ {
+ int32 target_bytes_length;
+ EncodingFlag flag = WTF8;
+
+ stringref_obj = POP_REF();
+
+ if (opcode == WASM_OP_STRING_MEASURE_WTF16) {
+ flag = WTF16;
+ }
+ else if (opcode == WASM_OP_STRING_MEASURE_UTF8) {
+ flag = UTF8;
+ }
+ else if (opcode == WASM_OP_STRING_MEASURE_WTF8) {
+ flag = LOSSY_UTF8;
+ }
+ target_bytes_length = wasm_string_measure(
+ (WASMString)wasm_stringref_obj_get_value(
+ stringref_obj),
+ flag);
+
+ PUSH_I32(target_bytes_length);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRING_ENCODE_UTF8:
+ case WASM_OP_STRING_ENCODE_WTF16:
+ case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
+ case WASM_OP_STRING_ENCODE_WTF8:
+ {
+ uint32 mem_idx, addr;
+ int32 target_bytes_length;
+ WASMMemoryInstance *memory_inst;
+ EncodingFlag flag = WTF8;
+
+ mem_idx = (uint32)read_uint32(frame_ip);
+ addr = POP_I32();
+ stringref_obj = POP_REF();
+
+ str_obj = (WASMString)wasm_stringref_obj_get_value(
+ stringref_obj);
+
+#if WASM_ENABLE_SHARED_HEAP != 0
+ if (app_addr_in_shared_heap((uint64)addr, 1))
+ shared_heap_addr_app_to_native((uint64)addr, maddr);
+ else
+#endif
+ {
+ memory_inst = module->memories[mem_idx];
+ maddr = memory_inst->memory_data + addr;
+ }
+
+ if (opcode == WASM_OP_STRING_ENCODE_WTF16) {
+ flag = WTF16;
+ count = wasm_string_measure(str_obj, flag);
+ target_bytes_length = wasm_string_encode(
+ str_obj, 0, count, maddr, NULL, flag);
+ }
+ else {
+ if (opcode == WASM_OP_STRING_ENCODE_UTF8) {
+ flag = UTF8;
+ }
+ else if (opcode
+ == WASM_OP_STRING_ENCODE_LOSSY_UTF8) {
+ flag = LOSSY_UTF8;
+ }
+ else if (opcode == WASM_OP_STRING_ENCODE_WTF8) {
+ flag = WTF8;
+ }
+ count = wasm_string_measure(str_obj, flag);
+ target_bytes_length = wasm_string_encode(
+ str_obj, 0, count, maddr, NULL, flag);
+
+ if (target_bytes_length == -1) {
+ wasm_set_exception(
+ module, "isolated surrogate is seen");
+ goto got_exception;
+ }
+ }
+ if (target_bytes_length < 0) {
+ wasm_set_exception(module,
+ "stringref encode failed");
+ goto got_exception;
+ }
+
+ PUSH_I32(target_bytes_length);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRING_CONCAT:
+ {
+ WASMStringrefObjectRef stringref_obj1, stringref_obj2;
+
+ stringref_obj2 = POP_REF();
+ stringref_obj1 = POP_REF();
+
+ str_obj = wasm_string_concat(
+ (WASMString)wasm_stringref_obj_get_value(
+ stringref_obj1),
+ (WASMString)wasm_stringref_obj_get_value(
+ stringref_obj2));
+ if (!str_obj) {
+ wasm_set_exception(module,
+ "create string object failed");
+ goto got_exception;
+ }
+
+ SYNC_ALL_TO_FRAME();
+ stringref_obj =
+ wasm_stringref_obj_new(exec_env, str_obj);
+ if (!stringref_obj) {
+ wasm_set_exception(module,
+ "create stringref failed");
+ goto got_exception;
+ }
+
+ PUSH_REF(stringref_obj);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRING_EQ:
+ {
+ WASMStringrefObjectRef stringref_obj1, stringref_obj2;
+ int32 is_eq;
+
+ stringref_obj2 = POP_REF();
+ stringref_obj1 = POP_REF();
+
+ is_eq = wasm_string_eq(
+ (WASMString)wasm_stringref_obj_get_value(
+ stringref_obj1),
+ (WASMString)wasm_stringref_obj_get_value(
+ stringref_obj2));
+
+ PUSH_I32(is_eq);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRING_IS_USV_SEQUENCE:
+ {
+ int32 is_usv_sequence;
+
+ stringref_obj = POP_REF();
+
+ is_usv_sequence = wasm_string_is_usv_sequence(
+ (WASMString)wasm_stringref_obj_get_value(
+ stringref_obj));
+
+ PUSH_I32(is_usv_sequence);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRING_AS_WTF8:
+ {
+ stringref_obj = POP_REF();
+
+ str_obj = wasm_string_create_view(
+ (WASMString)wasm_stringref_obj_get_value(
+ stringref_obj),
+ STRING_VIEW_WTF8);
+ if (!str_obj) {
+ wasm_set_exception(module,
+ "create string object failed");
+ goto got_exception;
+ }
+
+ SYNC_ALL_TO_FRAME();
+ stringview_wtf8_obj =
+ wasm_stringview_wtf8_obj_new(exec_env, str_obj);
+ if (!stringview_wtf8_obj) {
+ wasm_set_exception(module,
+ "create stringview wtf8 failed");
+ goto got_exception;
+ }
+
+ PUSH_REF(stringview_wtf8_obj);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
+ {
+ uint32 next_pos, bytes, pos;
+
+ bytes = POP_I32();
+ pos = POP_I32();
+ stringview_wtf8_obj = POP_REF();
+
+ next_pos = wasm_string_advance(
+ (WASMString)wasm_stringview_wtf8_obj_get_value(
+ stringview_wtf8_obj),
+ pos, bytes, NULL);
+
+ PUSH_I32(next_pos);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
+ case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
+ case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
+ {
+ uint32 mem_idx, addr, pos, bytes, next_pos;
+ int32 bytes_written;
+ WASMMemoryInstance *memory_inst;
+ EncodingFlag flag = WTF8;
+
+ if (opcode == WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8) {
+ flag = UTF8;
+ }
+ else if (opcode
+ == WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8) {
+ flag = LOSSY_UTF8;
+ }
+ else if (opcode
+ == WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8) {
+ flag = WTF8;
+ }
+
+ mem_idx = (uint32)read_uint32(frame_ip);
+ bytes = POP_I32();
+ pos = POP_I32();
+ addr = POP_I32();
+ stringview_wtf8_obj = POP_REF();
+
+#if WASM_ENABLE_SHARED_HEAP != 0
+ if (app_addr_in_shared_heap((uint64)addr, 1))
+ shared_heap_addr_app_to_native((uint64)addr, maddr);
+ else
+#endif
+ {
+ memory_inst = module->memories[mem_idx];
+ maddr = memory_inst->memory_data + addr;
+ }
+
+ bytes_written = wasm_string_encode(
+ (WASMString)wasm_stringview_wtf8_obj_get_value(
+ stringview_wtf8_obj),
+ pos, bytes, maddr, &next_pos, flag);
+
+ if (bytes_written < 0) {
+ if (bytes_written == Isolated_Surrogate) {
+ wasm_set_exception(
+ module, "isolated surrogate is seen");
+ }
+ else {
+ wasm_set_exception(module, "encode failed");
+ }
+
+ goto got_exception;
+ }
+
+ PUSH_I32(next_pos);
+ PUSH_I32(bytes_written);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRINGVIEW_WTF8_SLICE:
+ {
+ uint32 start, end;
+
+ end = POP_I32();
+ start = POP_I32();
+ stringview_wtf8_obj = POP_REF();
+
+ str_obj = wasm_string_slice(
+ (WASMString)wasm_stringview_wtf8_obj_get_value(
+ stringview_wtf8_obj),
+ start, end, STRING_VIEW_WTF8);
+ if (!str_obj) {
+ wasm_set_exception(module,
+ "create string object failed");
+ goto got_exception;
+ }
+
+ SYNC_ALL_TO_FRAME();
+ stringref_obj =
+ wasm_stringref_obj_new(exec_env, str_obj);
+ if (!stringref_obj) {
+ wasm_set_exception(module,
+ "create stringref failed");
+ goto got_exception;
+ }
+
+ PUSH_REF(stringref_obj);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRING_AS_WTF16:
+ {
+ stringref_obj = POP_REF();
+
+ str_obj = wasm_string_create_view(
+ (WASMString)wasm_stringref_obj_get_value(
+ stringref_obj),
+ STRING_VIEW_WTF16);
+ if (!str_obj) {
+ wasm_set_exception(module,
+ "create string object failed");
+ goto got_exception;
+ }
+
+ SYNC_ALL_TO_FRAME();
+ stringview_wtf16_obj =
+ wasm_stringview_wtf16_obj_new(exec_env, str_obj);
+ if (!stringview_wtf16_obj) {
+ wasm_set_exception(
+ module, "create stringview wtf16 failed");
+ goto got_exception;
+ }
+
+ PUSH_REF(stringview_wtf16_obj);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRINGVIEW_WTF16_LENGTH:
+ {
+ int32 code_units_length;
+
+ stringview_wtf16_obj = POP_REF();
+
+ code_units_length = wasm_string_wtf16_get_length(
+ (WASMString)wasm_stringview_wtf16_obj_get_value(
+ stringview_wtf16_obj));
+
+ PUSH_I32(code_units_length);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
+ {
+ int32 pos;
+ uint32 code_unit;
+
+ pos = POP_I32();
+ stringview_wtf16_obj = POP_REF();
+
+ code_unit = (uint32)wasm_string_get_wtf16_codeunit(
+ (WASMString)wasm_stringview_wtf16_obj_get_value(
+ stringview_wtf16_obj),
+ pos);
+
+ PUSH_I32(code_unit);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRINGVIEW_WTF16_ENCODE:
+ {
+ uint32 mem_idx, addr, pos, len, offset = 0;
+ int32 written_code_units = 0;
+
+ mem_idx = (uint32)read_uint32(frame_ip);
+ len = POP_I32();
+ pos = POP_I32();
+ addr = POP_I32();
+ stringview_wtf16_obj = POP_REF();
+
+ CHECK_MEMORY_OVERFLOW(len * sizeof(uint16));
+
+ /* check 2-byte alignment */
+ if (((uintptr_t)maddr & (((uintptr_t)1 << 2) - 1))
+ != 0) {
+ wasm_set_exception(module,
+ "unaligned memory access");
+ goto got_exception;
+ }
+
+ written_code_units = wasm_string_encode(
+ (WASMString)wasm_stringview_wtf16_obj_get_value(
+ stringview_wtf16_obj),
+ pos, len, maddr, NULL, WTF16);
+
+ PUSH_I32(written_code_units);
+ (void)mem_idx;
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRINGVIEW_WTF16_SLICE:
+ {
+ uint32 start, end;
+
+ end = POP_I32();
+ start = POP_I32();
+ stringview_wtf16_obj = POP_REF();
+
+ str_obj = wasm_string_slice(
+ (WASMString)wasm_stringview_wtf16_obj_get_value(
+ stringview_wtf16_obj),
+ start, end, STRING_VIEW_WTF16);
+ if (!str_obj) {
+ wasm_set_exception(module,
+ "create string object failed");
+ goto got_exception;
+ }
+
+ SYNC_ALL_TO_FRAME();
+ stringref_obj =
+ wasm_stringref_obj_new(exec_env, str_obj);
+ if (!stringref_obj) {
+ wasm_set_exception(module,
+ "create stringref failed");
+ goto got_exception;
+ }
+
+ PUSH_REF(stringref_obj);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRING_AS_ITER:
+ {
+ stringref_obj = POP_REF();
+
+ str_obj = wasm_string_create_view(
+ (WASMString)wasm_stringref_obj_get_value(
+ stringref_obj),
+ STRING_VIEW_ITER);
+
+ if (!str_obj) {
+ wasm_set_exception(module,
+ "create string object failed");
+ goto got_exception;
+ }
+
+ SYNC_ALL_TO_FRAME();
+ stringview_iter_obj =
+ wasm_stringview_iter_obj_new(exec_env, str_obj, 0);
+ if (!stringview_iter_obj) {
+ wasm_set_exception(module,
+ "create stringview iter failed");
+ goto got_exception;
+ }
+
+ PUSH_REF(stringview_iter_obj);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRINGVIEW_ITER_NEXT:
+ {
+ uint32 code_point;
+
+ stringview_iter_obj = POP_REF();
+
+ code_point = wasm_string_next_codepoint(
+ (WASMString)wasm_stringview_iter_obj_get_value(
+ stringview_iter_obj),
+ wasm_stringview_iter_obj_get_pos(
+ stringview_iter_obj));
+
+ PUSH_I32(code_point);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRINGVIEW_ITER_ADVANCE:
+ case WASM_OP_STRINGVIEW_ITER_REWIND:
+ {
+ uint32 code_points_count, code_points_consumed = 0,
+ cur_pos, next_pos = 0;
+
+ code_points_count = POP_I32();
+ stringview_iter_obj = POP_REF();
+
+ str_obj =
+ (WASMString)wasm_stringview_iter_obj_get_value(
+ stringview_iter_obj);
+ cur_pos = wasm_stringview_iter_obj_get_pos(
+ stringview_iter_obj);
+
+ if (opcode == WASM_OP_STRINGVIEW_ITER_ADVANCE) {
+ next_pos = wasm_string_advance(
+ str_obj, cur_pos, code_points_count,
+ &code_points_consumed);
+ }
+ else if (opcode == WASM_OP_STRINGVIEW_ITER_REWIND) {
+ next_pos = wasm_string_rewind(
+ str_obj, cur_pos, code_points_count,
+ &code_points_consumed);
+ }
+
+ wasm_stringview_iter_obj_update_pos(stringview_iter_obj,
+ next_pos);
+
+ PUSH_I32(code_points_consumed);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRINGVIEW_ITER_SLICE:
+ {
+ uint32 code_points_count, cur_pos;
+
+ code_points_count = POP_I32();
+ stringview_iter_obj = POP_REF();
+
+ cur_pos = wasm_stringview_iter_obj_get_pos(
+ stringview_iter_obj);
+
+ str_obj = wasm_string_slice(
+ (WASMString)wasm_stringview_iter_obj_get_value(
+ stringview_iter_obj),
+ cur_pos, cur_pos + code_points_count,
+ STRING_VIEW_ITER);
+ if (!str_obj) {
+ wasm_set_exception(module,
+ "create string object failed");
+ goto got_exception;
+ }
+
+ SYNC_ALL_TO_FRAME();
+ stringref_obj =
+ wasm_stringref_obj_new(exec_env, str_obj);
+ if (!stringref_obj) {
+ wasm_set_exception(module,
+ "create stringref failed");
+ goto got_exception;
+ }
+
+ PUSH_REF(stringref_obj);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRING_NEW_UTF8_ARRAY:
+ case WASM_OP_STRING_NEW_WTF16_ARRAY:
+ case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
+ case WASM_OP_STRING_NEW_WTF8_ARRAY:
+ {
+ uint32 start, end, array_len;
+ EncodingFlag flag = WTF8;
+ WASMArrayType *array_type;
+ void *arr_start_addr;
+
+ end = POP_I32();
+ start = POP_I32();
+ array_obj = POP_REF();
+
+ array_type = (WASMArrayType *)wasm_obj_get_defined_type(
+ (WASMObjectRef)array_obj);
+ arr_start_addr =
+ wasm_array_obj_elem_addr(array_obj, start);
+ array_len = wasm_array_obj_length(array_obj);
+
+ if (start > end || end > array_len) {
+ wasm_set_exception(module,
+ "out of bounds array access");
+ goto got_exception;
+ }
+
+ if (opcode == WASM_OP_STRING_NEW_WTF16_ARRAY) {
+ if (array_type->elem_type != VALUE_TYPE_I16) {
+ wasm_set_exception(module,
+ "array type mismatch");
+ goto got_exception;
+ }
+ flag = WTF16;
+ }
+ else {
+ if (array_type->elem_type != VALUE_TYPE_I8) {
+ wasm_set_exception(module,
+ "array type mismatch");
+ goto got_exception;
+ }
+ if (opcode == WASM_OP_STRING_NEW_UTF8_ARRAY) {
+ flag = UTF8;
+ }
+ else if (opcode == WASM_OP_STRING_NEW_WTF8_ARRAY) {
+ flag = WTF8;
+ }
+ else if (opcode
+ == WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY) {
+ flag = LOSSY_UTF8;
+ }
+ }
+
+ str_obj = wasm_string_new_with_encoding(
+ arr_start_addr, (end - start), flag);
+ if (!str_obj) {
+ wasm_set_exception(module,
+ "create string object failed");
+ goto got_exception;
+ }
+
+ SYNC_ALL_TO_FRAME();
+ stringref_obj =
+ wasm_stringref_obj_new(exec_env, str_obj);
+ if (!stringref_obj) {
+ wasm_set_exception(module,
+ "create stringref failed");
+ goto got_exception;
+ }
+
+ PUSH_REF(stringref_obj);
+ HANDLE_OP_END();
+ }
+ case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
+ case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
+ case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
+ case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
+ {
+ uint32 start, array_len, count;
+ int32 bytes_written;
+ EncodingFlag flag = WTF8;
+ WASMArrayType *array_type;
+ void *arr_start_addr;
+
+ start = POP_I32();
+ array_obj = POP_REF();
+ stringref_obj = POP_REF();
+
+ str_obj = (WASMString)wasm_stringref_obj_get_value(
+ stringref_obj);
+
+ array_type = (WASMArrayType *)wasm_obj_get_defined_type(
+ (WASMObjectRef)array_obj);
+ arr_start_addr =
+ wasm_array_obj_elem_addr(array_obj, start);
+ array_len = wasm_array_obj_length(array_obj);
+
+ if (start > array_len) {
+ wasm_set_exception(module,
+ "out of bounds array access");
+ goto got_exception;
+ }
+
+ if (opcode == WASM_OP_STRING_ENCODE_WTF16_ARRAY) {
+ if (array_type->elem_type != VALUE_TYPE_I16) {
+ wasm_set_exception(module,
+ "array type mismatch");
+ goto got_exception;
+ }
+ flag = WTF16;
+ }
+ else {
+ if (array_type->elem_type != VALUE_TYPE_I8) {
+ wasm_set_exception(module,
+ "array type mismatch");
+ goto got_exception;
+ }
+ if (opcode == WASM_OP_STRING_ENCODE_UTF8_ARRAY) {
+ flag = UTF8;
+ }
+ else if (opcode
+ == WASM_OP_STRING_ENCODE_WTF8_ARRAY) {
+ flag = WTF8;
+ }
+ else if (
+ opcode
+ == WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY) {
+ flag = LOSSY_UTF8;
+ }
+ }
+
+ count = wasm_string_measure(str_obj, flag);
+
+ bytes_written = wasm_string_encode(
+ str_obj, 0, count, arr_start_addr, NULL, flag);
+
+ if (bytes_written < 0) {
+ if (bytes_written == Isolated_Surrogate) {
+ wasm_set_exception(
+ module, "isolated surrogate is seen");
+ }
+ else if (bytes_written == Insufficient_Space) {
+ wasm_set_exception(
+ module, "array space is insufficient");
+ }
+ else {
+ wasm_set_exception(module, "encode failed");
+ }
+
+ goto got_exception;
+ }
+
+ PUSH_I32(bytes_written);
+ HANDLE_OP_END();
+ }
+#endif /* end of WASM_ENABLE_STRINGREF != 0 */
+
+ default:
+ {
+ wasm_set_exception(module, "unsupported opcode");
+ goto got_exception;
+ }
+ }
+ }
+#endif /* end of WASM_ENABLE_GC != 0 */
+
+ /* variable instructions */
+ HANDLE_OP(EXT_OP_SET_LOCAL_FAST)
+ HANDLE_OP(EXT_OP_TEE_LOCAL_FAST)
+ {
+ /* clang-format off */
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
+ local_offset = *frame_ip++;
+#else
+ local_offset = *frame_ip;
+ frame_ip += 2;
+#endif
+ /* clang-format on */
+ *(uint32 *)(frame_lp + local_offset) =
+ GET_OPERAND(uint32, I32, 0);
+ frame_ip += 2;
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(EXT_OP_SET_LOCAL_FAST_I64)
+ HANDLE_OP(EXT_OP_TEE_LOCAL_FAST_I64)
+ {
+ /* clang-format off */
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
+ local_offset = *frame_ip++;
+#else
+ local_offset = *frame_ip;
+ frame_ip += 2;
+#endif
+ /* clang-format on */
+ PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
+ GET_OPERAND(uint64, I64, 0));
+ frame_ip += 2;
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_GET_GLOBAL)
+ {
+ global_idx = read_uint32(frame_ip);
+ bh_assert(global_idx < module->e->global_count);
+ global = globals + global_idx;
+ global_addr = get_global_addr(global_data, global);
+ addr_ret = GET_OFFSET();
+ /* clang-format off */
+#if WASM_ENABLE_GC == 0
+ frame_lp[addr_ret] = *(uint32 *)global_addr;
+#else
+ if (!wasm_is_type_reftype(global->type))
+ frame_lp[addr_ret] = *(uint32 *)global_addr;
+ else {
+ PUT_REF_TO_ADDR(frame_lp + addr_ret,
+ GET_REF_FROM_ADDR((uint32 *)global_addr));
+ if (!wasm_is_reftype_i31ref(global->type)) {
+ SET_FRAME_REF(addr_ret);
+ }
+ }
+#endif
+ /* clang-format on */
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_GET_GLOBAL_64)
+ {
+ global_idx = read_uint32(frame_ip);
+ bh_assert(global_idx < module->e->global_count);
+ global = globals + global_idx;
+ global_addr = get_global_addr(global_data, global);
+ addr_ret = GET_OFFSET();
+ PUT_I64_TO_ADDR(frame_lp + addr_ret,
+ GET_I64_FROM_ADDR((uint32 *)global_addr));
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_SET_GLOBAL)
+ {
+ global_idx = read_uint32(frame_ip);
+ bh_assert(global_idx < module->e->global_count);
+ global = globals + global_idx;
+ global_addr = get_global_addr(global_data, global);
+ addr1 = GET_OFFSET();
+ /* clang-format off */
+#if WASM_ENABLE_GC == 0
+ *(int32 *)global_addr = frame_lp[addr1];
+#else
+ if (!wasm_is_type_reftype(global->type))
+ *(int32 *)global_addr = frame_lp[addr1];
+ else {
+ PUT_REF_TO_ADDR((uint32 *)global_addr,
+ GET_REF_FROM_ADDR(frame_lp + addr1));
+ CLEAR_FRAME_REF(addr1);
+ }
+#endif
+ /* clang-format on */
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_SET_GLOBAL_AUX_STACK)
+ {
+ uint64 aux_stack_top;
+
+ global_idx = read_uint32(frame_ip);
+ bh_assert(global_idx < module->e->global_count);
+ global = globals + global_idx;
+ global_addr = get_global_addr(global_data, global);
+ /* TODO: Memory64 the data type depends on mem idx type */
+ aux_stack_top = (uint64)frame_lp[GET_OFFSET()];
+ if (aux_stack_top <= (uint64)exec_env->aux_stack_boundary) {
+ wasm_set_exception(module, "wasm auxiliary stack overflow");
+ goto got_exception;
+ }
+ if (aux_stack_top > (uint64)exec_env->aux_stack_bottom) {
+ wasm_set_exception(module,
+ "wasm auxiliary stack underflow");
+ goto got_exception;
+ }
+ *(int32 *)global_addr = (uint32)aux_stack_top;
+#if WASM_ENABLE_MEMORY_PROFILING != 0
+ if (module->module->aux_stack_top_global_index != (uint32)-1) {
+ uint32 aux_stack_used =
+ (uint32)(module->module->aux_stack_bottom
+ - *(uint32 *)global_addr);
+ if (aux_stack_used > module->e->max_aux_stack_used)
+ module->e->max_aux_stack_used = aux_stack_used;
+ }
+#endif
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_SET_GLOBAL_64)
+ {
+ global_idx = read_uint32(frame_ip);
+ bh_assert(global_idx < module->e->global_count);
+ global = globals + global_idx;
+ global_addr = get_global_addr(global_data, global);
+ addr1 = GET_OFFSET();
+ PUT_I64_TO_ADDR((uint32 *)global_addr,
+ GET_I64_FROM_ADDR(frame_lp + addr1));
+ HANDLE_OP_END();
+ }
+
+ /* memory load instructions */
+ HANDLE_OP(WASM_OP_I32_LOAD)
+ {
+ uint32 offset, addr;
+ offset = read_uint32(frame_ip);
+ addr = GET_OPERAND(uint32, I32, 0);
+ frame_ip += 2;
+ addr_ret = GET_OFFSET();
+ CHECK_MEMORY_OVERFLOW(4);
+ frame_lp[addr_ret] = LOAD_I32(maddr);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_LOAD)
+ {
+ uint32 offset, addr;
+ offset = read_uint32(frame_ip);
+ addr = GET_OPERAND(uint32, I32, 0);
+ frame_ip += 2;
+ addr_ret = GET_OFFSET();
+ CHECK_MEMORY_OVERFLOW(8);
+ PUT_I64_TO_ADDR(frame_lp + addr_ret, LOAD_I64(maddr));
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_LOAD8_S)
+ {
+ uint32 offset, addr;
+ offset = read_uint32(frame_ip);
+ addr = GET_OPERAND(uint32, I32, 0);
+ frame_ip += 2;
+ addr_ret = GET_OFFSET();
+ CHECK_MEMORY_OVERFLOW(1);
+ frame_lp[addr_ret] = sign_ext_8_32(*(int8 *)maddr);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_LOAD8_U)
+ {
+ uint32 offset, addr;
+ offset = read_uint32(frame_ip);
+ addr = GET_OPERAND(uint32, I32, 0);
+ frame_ip += 2;
+ addr_ret = GET_OFFSET();
+ CHECK_MEMORY_OVERFLOW(1);
+ frame_lp[addr_ret] = (uint32)(*(uint8 *)(maddr));
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_LOAD16_S)
+ {
+ uint32 offset, addr;
+ offset = read_uint32(frame_ip);
+ addr = GET_OPERAND(uint32, I32, 0);
+ frame_ip += 2;
+ addr_ret = GET_OFFSET();
+ CHECK_MEMORY_OVERFLOW(2);
+ frame_lp[addr_ret] = sign_ext_16_32(LOAD_I16(maddr));
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_LOAD16_U)
+ {
+ uint32 offset, addr;
+ offset = read_uint32(frame_ip);
+ addr = GET_OPERAND(uint32, I32, 0);
+ frame_ip += 2;
+ addr_ret = GET_OFFSET();
+ CHECK_MEMORY_OVERFLOW(2);
+ frame_lp[addr_ret] = (uint32)(LOAD_U16(maddr));
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_LOAD8_S)
+ {
+ uint32 offset, addr;
+ offset = read_uint32(frame_ip);
+ addr = GET_OPERAND(uint32, I32, 0);
+ frame_ip += 2;
+ addr_ret = GET_OFFSET();
+ CHECK_MEMORY_OVERFLOW(1);
+ PUT_I64_TO_ADDR(frame_lp + addr_ret,
+ sign_ext_8_64(*(int8 *)maddr));
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_LOAD8_U)
+ {
+ uint32 offset, addr;
+ offset = read_uint32(frame_ip);
+ addr = GET_OPERAND(uint32, I32, 0);
+ frame_ip += 2;
+ addr_ret = GET_OFFSET();
+ CHECK_MEMORY_OVERFLOW(1);
+ PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(*(uint8 *)maddr));
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_LOAD16_S)
+ {
+ uint32 offset, addr;
+ offset = read_uint32(frame_ip);
+ addr = GET_OPERAND(uint32, I32, 0);
+ frame_ip += 2;
+ addr_ret = GET_OFFSET();
+ CHECK_MEMORY_OVERFLOW(2);
+ PUT_I64_TO_ADDR(frame_lp + addr_ret,
+ sign_ext_16_64(LOAD_I16(maddr)));
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_LOAD16_U)
+ {
+ uint32 offset, addr;
+ offset = read_uint32(frame_ip);
+ addr = GET_OPERAND(uint32, I32, 0);
+ frame_ip += 2;
+ addr_ret = GET_OFFSET();
+ CHECK_MEMORY_OVERFLOW(2);
+ PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U16(maddr)));
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_LOAD32_S)
+ {
+ uint32 offset, addr;
+ offset = read_uint32(frame_ip);
+ addr = GET_OPERAND(uint32, I32, 0);
+ frame_ip += 2;
+ addr_ret = GET_OFFSET();
+ CHECK_MEMORY_OVERFLOW(4);
+ PUT_I64_TO_ADDR(frame_lp + addr_ret,
+ sign_ext_32_64(LOAD_I32(maddr)));
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_LOAD32_U)
+ {
+ uint32 offset, addr;
+ offset = read_uint32(frame_ip);
+ addr = GET_OPERAND(uint32, I32, 0);
+ frame_ip += 2;
+ addr_ret = GET_OFFSET();
+ CHECK_MEMORY_OVERFLOW(4);
+ PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U32(maddr)));
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_STORE)
+ {
+ uint32 offset, addr;
+ uint32 sval;
+ offset = read_uint32(frame_ip);
+ sval = GET_OPERAND(uint32, I32, 0);
+ addr = GET_OPERAND(uint32, I32, 2);
+ frame_ip += 4;
+ CHECK_MEMORY_OVERFLOW(4);
+ STORE_U32(maddr, sval);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_STORE8)
+ {
+ uint32 offset, addr;
+ uint32 sval;
+ offset = read_uint32(frame_ip);
+ sval = GET_OPERAND(uint32, I32, 0);
+ addr = GET_OPERAND(uint32, I32, 2);
+ frame_ip += 4;
+ CHECK_MEMORY_OVERFLOW(1);
+ STORE_U8(maddr, (uint8_t)sval);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_STORE16)
+ {
+ uint32 offset, addr;
+ uint32 sval;
+ offset = read_uint32(frame_ip);
+ sval = GET_OPERAND(uint32, I32, 0);
+ addr = GET_OPERAND(uint32, I32, 2);
+ frame_ip += 4;
+ CHECK_MEMORY_OVERFLOW(2);
+ STORE_U16(maddr, (uint16)sval);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_STORE)
+ {
+ uint32 offset, addr;
+ uint64 sval;
+ offset = read_uint32(frame_ip);
+ sval = GET_OPERAND(uint64, I64, 0);
+ addr = GET_OPERAND(uint32, I32, 2);
+ frame_ip += 4;
+ CHECK_MEMORY_OVERFLOW(8);
+ STORE_I64(maddr, sval);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_STORE8)
+ {
+ uint32 offset, addr;
+ uint64 sval;
+ offset = read_uint32(frame_ip);
+ sval = GET_OPERAND(uint64, I64, 0);
+ addr = GET_OPERAND(uint32, I32, 2);
+ frame_ip += 4;
+ CHECK_MEMORY_OVERFLOW(1);
+ *(uint8 *)maddr = (uint8)sval;
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_STORE16)
+ {
+ uint32 offset, addr;
+ uint64 sval;
+ offset = read_uint32(frame_ip);
+ sval = GET_OPERAND(uint64, I64, 0);
+ addr = GET_OPERAND(uint32, I32, 2);
+ frame_ip += 4;
+ CHECK_MEMORY_OVERFLOW(2);
+ STORE_U16(maddr, (uint16)sval);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_STORE32)
+ {
+ uint32 offset, addr;
+ uint64 sval;
+ offset = read_uint32(frame_ip);
+ sval = GET_OPERAND(uint64, I64, 0);
+ addr = GET_OPERAND(uint32, I32, 2);
+ frame_ip += 4;
+ CHECK_MEMORY_OVERFLOW(4);
+ STORE_U32(maddr, (uint32)sval);
+ HANDLE_OP_END();
+ }
+
+ /* memory size and memory grow instructions */
+ HANDLE_OP(WASM_OP_MEMORY_SIZE)
+ {
+ uint32 reserved;
+ addr_ret = GET_OFFSET();
+ frame_lp[addr_ret] = memory->cur_page_count;
+ (void)reserved;
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_MEMORY_GROW)
+ {
+ uint32 reserved, delta,
+ prev_page_count = memory->cur_page_count;
+
+ addr1 = GET_OFFSET();
+ addr_ret = GET_OFFSET();
+ delta = (uint32)frame_lp[addr1];
+
+ /* TODO: multi-memory wasm_enlarge_memory_with_idx() */
+ if (!wasm_enlarge_memory(module, delta)) {
+ /* failed to memory.grow, return -1 */
+ frame_lp[addr_ret] = -1;
+ }
+ else {
+ /* success, return previous page count */
+ frame_lp[addr_ret] = prev_page_count;
+ /* update memory size, no need to update memory ptr as
+ it isn't changed in wasm_enlarge_memory */
+#if !defined(OS_ENABLE_HW_BOUND_CHECK) \
+ || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
+ || WASM_ENABLE_BULK_MEMORY != 0
+ linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory);
+#endif
+ }
+
+ (void)reserved;
+ HANDLE_OP_END();
+ }
+
+ /* constant instructions */
+ HANDLE_OP(WASM_OP_F64_CONST)
+ HANDLE_OP(WASM_OP_I64_CONST)
+ {
+ uint8 *orig_ip = frame_ip;
+
+ frame_ip += sizeof(uint64);
+ addr_ret = GET_OFFSET();
+
+ bh_memcpy_s(frame_lp + addr_ret, sizeof(uint64), orig_ip,
+ sizeof(uint64));
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_CONST)
+ HANDLE_OP(WASM_OP_I32_CONST)
+ {
+ uint8 *orig_ip = frame_ip;
+
+ frame_ip += sizeof(uint32);
+ addr_ret = GET_OFFSET();
+
+ bh_memcpy_s(frame_lp + addr_ret, sizeof(uint32), orig_ip,
+ sizeof(uint32));
+ HANDLE_OP_END();
+ }
+
+ /* comparison instructions of i32 */
+ HANDLE_OP(WASM_OP_I32_EQZ)
+ {
+ DEF_OP_EQZ(int32, I32);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_EQ)
+ {
+ DEF_OP_CMP(uint32, I32, ==);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_NE)
+ {
+ DEF_OP_CMP(uint32, I32, !=);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_LT_S)
+ {
+ DEF_OP_CMP(int32, I32, <);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_LT_U)
+ {
+ DEF_OP_CMP(uint32, I32, <);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_GT_S)
+ {
+ DEF_OP_CMP(int32, I32, >);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_GT_U)
+ {
+ DEF_OP_CMP(uint32, I32, >);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_LE_S)
+ {
+ DEF_OP_CMP(int32, I32, <=);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_LE_U)
+ {
+ DEF_OP_CMP(uint32, I32, <=);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_GE_S)
+ {
+ DEF_OP_CMP(int32, I32, >=);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_GE_U)
+ {
+ DEF_OP_CMP(uint32, I32, >=);
+ HANDLE_OP_END();
+ }
+
+ /* comparison instructions of i64 */
+ HANDLE_OP(WASM_OP_I64_EQZ)
+ {
+ DEF_OP_EQZ(int64, I64);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_EQ)
+ {
+ DEF_OP_CMP(uint64, I64, ==);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_NE)
+ {
+ DEF_OP_CMP(uint64, I64, !=);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_LT_S)
+ {
+ DEF_OP_CMP(int64, I64, <);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_LT_U)
+ {
+ DEF_OP_CMP(uint64, I64, <);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_GT_S)
+ {
+ DEF_OP_CMP(int64, I64, >);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_GT_U)
+ {
+ DEF_OP_CMP(uint64, I64, >);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_LE_S)
+ {
+ DEF_OP_CMP(int64, I64, <=);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_LE_U)
+ {
+ DEF_OP_CMP(uint64, I64, <=);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_GE_S)
+ {
+ DEF_OP_CMP(int64, I64, >=);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_GE_U)
+ {
+ DEF_OP_CMP(uint64, I64, >=);
+ HANDLE_OP_END();
+ }
+
+ /* comparison instructions of f32 */
+ HANDLE_OP(WASM_OP_F32_EQ)
+ {
+ DEF_OP_CMP(float32, F32, ==);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_NE)
+ {
+ DEF_OP_CMP(float32, F32, !=);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_LT)
+ {
+ DEF_OP_CMP(float32, F32, <);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_GT)
+ {
+ DEF_OP_CMP(float32, F32, >);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_LE)
+ {
+ DEF_OP_CMP(float32, F32, <=);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_GE)
+ {
+ DEF_OP_CMP(float32, F32, >=);
+ HANDLE_OP_END();
+ }
+
+ /* comparison instructions of f64 */
+ HANDLE_OP(WASM_OP_F64_EQ)
+ {
+ DEF_OP_CMP(float64, F64, ==);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_NE)
+ {
+ DEF_OP_CMP(float64, F64, !=);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_LT)
+ {
+ DEF_OP_CMP(float64, F64, <);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_GT)
+ {
+ DEF_OP_CMP(float64, F64, >);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_LE)
+ {
+ DEF_OP_CMP(float64, F64, <=);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_GE)
+ {
+ DEF_OP_CMP(float64, F64, >=);
+ HANDLE_OP_END();
+ }
+
+ /* numberic instructions of i32 */
+ HANDLE_OP(WASM_OP_I32_CLZ)
+ {
+ DEF_OP_BIT_COUNT(uint32, I32, clz32);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_CTZ)
+ {
+ DEF_OP_BIT_COUNT(uint32, I32, ctz32);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_POPCNT)
+ {
+ DEF_OP_BIT_COUNT(uint32, I32, popcount32);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_ADD)
+ {
+ DEF_OP_NUMERIC(uint32, uint32, I32, +);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_SUB)
+ {
+ DEF_OP_NUMERIC(uint32, uint32, I32, -);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_MUL)
+ {
+ DEF_OP_NUMERIC(uint32, uint32, I32, *);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_DIV_S)
+ {
+ int32 a, b;
+
+ b = frame_lp[GET_OFFSET()];
+ a = frame_lp[GET_OFFSET()];
+ addr_ret = GET_OFFSET();
+ if (a == (int32)0x80000000 && b == -1) {
+ wasm_set_exception(module, "integer overflow");
+ goto got_exception;
+ }
+ if (b == 0) {
+ wasm_set_exception(module, "integer divide by zero");
+ goto got_exception;
+ }
+ frame_lp[addr_ret] = (a / b);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_DIV_U)
+ {
+ uint32 a, b;
+
+ addr1 = GET_OFFSET();
+ addr2 = GET_OFFSET();
+ addr_ret = GET_OFFSET();
+
+ b = (uint32)frame_lp[addr1];
+ a = (uint32)frame_lp[addr2];
+ if (b == 0) {
+ wasm_set_exception(module, "integer divide by zero");
+ goto got_exception;
+ }
+ frame_lp[addr_ret] = (a / b);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_REM_S)
+ {
+ int32 a, b;
+
+ addr1 = GET_OFFSET();
+ addr2 = GET_OFFSET();
+ addr_ret = GET_OFFSET();
+
+ b = frame_lp[addr1];
+ a = frame_lp[addr2];
+ if (a == (int32)0x80000000 && b == -1) {
+ frame_lp[addr_ret] = 0;
+ HANDLE_OP_END();
+ }
+ if (b == 0) {
+ wasm_set_exception(module, "integer divide by zero");
+ goto got_exception;
+ }
+ frame_lp[addr_ret] = (a % b);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_REM_U)
+ {
+ uint32 a, b;
+
+ addr1 = GET_OFFSET();
+ addr2 = GET_OFFSET();
+ addr_ret = GET_OFFSET();
+
+ b = (uint32)frame_lp[addr1];
+ a = (uint32)frame_lp[addr2];
+ if (b == 0) {
+ wasm_set_exception(module, "integer divide by zero");
+ goto got_exception;
+ }
+ frame_lp[addr_ret] = (a % b);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_AND)
+ {
+ DEF_OP_NUMERIC(uint32, uint32, I32, &);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_OR)
+ {
+ DEF_OP_NUMERIC(uint32, uint32, I32, |);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_XOR)
+ {
+ DEF_OP_NUMERIC(uint32, uint32, I32, ^);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_SHL)
+ {
+ DEF_OP_NUMERIC2(uint32, uint32, I32, <<);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_SHR_S)
+ {
+ DEF_OP_NUMERIC2(int32, uint32, I32, >>);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_SHR_U)
+ {
+ DEF_OP_NUMERIC2(uint32, uint32, I32, >>);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_ROTL)
+ {
+ uint32 a, b;
+
+ b = (uint32)frame_lp[GET_OFFSET()];
+ a = (uint32)frame_lp[GET_OFFSET()];
+ frame_lp[GET_OFFSET()] = rotl32(a, b);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_ROTR)
+ {
+ uint32 a, b;
+
+ b = (uint32)frame_lp[GET_OFFSET()];
+ a = (uint32)frame_lp[GET_OFFSET()];
+ frame_lp[GET_OFFSET()] = rotr32(a, b);
+ HANDLE_OP_END();
+ }
+
+ /* numberic instructions of i64 */
+ HANDLE_OP(WASM_OP_I64_CLZ)
+ {
+ DEF_OP_BIT_COUNT(uint64, I64, clz64);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_CTZ)
+ {
+ DEF_OP_BIT_COUNT(uint64, I64, ctz64);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_POPCNT)
+ {
+ DEF_OP_BIT_COUNT(uint64, I64, popcount64);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_ADD)
+ {
+ DEF_OP_NUMERIC_64(uint64, uint64, I64, +);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_SUB)
+ {
+ DEF_OP_NUMERIC_64(uint64, uint64, I64, -);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_MUL)
+ {
+ DEF_OP_NUMERIC_64(uint64, uint64, I64, *);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_DIV_S)
+ {
+ int64 a, b;
+
+ b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
+ a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
+ if (a == (int64)0x8000000000000000LL && b == -1) {
+ wasm_set_exception(module, "integer overflow");
+ goto got_exception;
+ }
+ if (b == 0) {
+ wasm_set_exception(module, "integer divide by zero");
+ goto got_exception;
+ }
+ PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_DIV_U)
+ {
+ uint64 a, b;
+
+ b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
+ a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
+ if (b == 0) {
+ wasm_set_exception(module, "integer divide by zero");
+ goto got_exception;
+ }
+ PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_REM_S)
+ {
+ int64 a, b;
+
+ b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
+ a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
+ if (a == (int64)0x8000000000000000LL && b == -1) {
+ *(int64 *)(frame_lp + GET_OFFSET()) = 0;
+ HANDLE_OP_END();
+ }
+ if (b == 0) {
+ wasm_set_exception(module, "integer divide by zero");
+ goto got_exception;
+ }
+ PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_REM_U)
+ {
+ uint64 a, b;
+
+ b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
+ a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
+ if (b == 0) {
+ wasm_set_exception(module, "integer divide by zero");
+ goto got_exception;
+ }
+ PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_AND)
+ {
+ DEF_OP_NUMERIC_64(uint64, uint64, I64, &);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_OR)
+ {
+ DEF_OP_NUMERIC_64(uint64, uint64, I64, |);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_XOR)
+ {
+ DEF_OP_NUMERIC_64(uint64, uint64, I64, ^);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_SHL)
+ {
+ DEF_OP_NUMERIC2_64(uint64, uint64, I64, <<);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_SHR_S)
+ {
+ DEF_OP_NUMERIC2_64(int64, uint64, I64, >>);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_SHR_U)
+ {
+ DEF_OP_NUMERIC2_64(uint64, uint64, I64, >>);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_ROTL)
+ {
+ uint64 a, b;
+
+ b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
+ a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
+ PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotl64(a, b));
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_ROTR)
+ {
+ uint64 a, b;
+
+ b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
+ a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
+ PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotr64(a, b));
+ HANDLE_OP_END();
+ }
+
+ /* numberic instructions of f32 */
+ HANDLE_OP(WASM_OP_F32_ABS)
+ {
+ DEF_OP_MATH(float32, F32, fabsf);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_NEG)
+ {
+ uint32 u32 = frame_lp[GET_OFFSET()];
+ uint32 sign_bit = u32 & ((uint32)1 << 31);
+ addr_ret = GET_OFFSET();
+ if (sign_bit)
+ frame_lp[addr_ret] = u32 & ~((uint32)1 << 31);
+ else
+ frame_lp[addr_ret] = u32 | ((uint32)1 << 31);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_CEIL)
+ {
+ DEF_OP_MATH(float32, F32, ceilf);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_FLOOR)
+ {
+ DEF_OP_MATH(float32, F32, floorf);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_TRUNC)
+ {
+ DEF_OP_MATH(float32, F32, truncf);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_NEAREST)
+ {
+ DEF_OP_MATH(float32, F32, rintf);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_SQRT)
+ {
+ DEF_OP_MATH(float32, F32, sqrtf);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_ADD)
+ {
+ DEF_OP_NUMERIC(float32, float32, F32, +);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_SUB)
+ {
+ DEF_OP_NUMERIC(float32, float32, F32, -);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_MUL)
+ {
+ DEF_OP_NUMERIC(float32, float32, F32, *);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_DIV)
+ {
+ DEF_OP_NUMERIC(float32, float32, F32, /);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_MIN)
+ {
+ float32 a, b;
+
+ b = *(float32 *)(frame_lp + GET_OFFSET());
+ a = *(float32 *)(frame_lp + GET_OFFSET());
+
+ *(float32 *)(frame_lp + GET_OFFSET()) = f32_min(a, b);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_MAX)
+ {
+ float32 a, b;
+
+ b = *(float32 *)(frame_lp + GET_OFFSET());
+ a = *(float32 *)(frame_lp + GET_OFFSET());
+
+ *(float32 *)(frame_lp + GET_OFFSET()) = f32_max(a, b);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_COPYSIGN)
+ {
+ float32 a, b;
+
+ b = *(float32 *)(frame_lp + GET_OFFSET());
+ a = *(float32 *)(frame_lp + GET_OFFSET());
+ *(float32 *)(frame_lp + GET_OFFSET()) = local_copysignf(a, b);
+ HANDLE_OP_END();
+ }
+
+ /* numberic instructions of f64 */
+ HANDLE_OP(WASM_OP_F64_ABS)
+ {
+ DEF_OP_MATH(float64, F64, fabs);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_NEG)
+ {
+ uint64 u64 = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
+ uint64 sign_bit = u64 & (((uint64)1) << 63);
+ if (sign_bit)
+ PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(),
+ (u64 & ~(((uint64)1) << 63)));
+ else
+ PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(),
+ (u64 | (((uint64)1) << 63)));
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_CEIL)
+ {
+ DEF_OP_MATH(float64, F64, ceil);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_FLOOR)
+ {
+ DEF_OP_MATH(float64, F64, floor);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_TRUNC)
+ {
+ DEF_OP_MATH(float64, F64, trunc);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_NEAREST)
+ {
+ DEF_OP_MATH(float64, F64, rint);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_SQRT)
+ {
+ DEF_OP_MATH(float64, F64, sqrt);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_ADD)
+ {
+ DEF_OP_NUMERIC_64(float64, float64, F64, +);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_SUB)
+ {
+ DEF_OP_NUMERIC_64(float64, float64, F64, -);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_MUL)
+ {
+ DEF_OP_NUMERIC_64(float64, float64, F64, *);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_DIV)
+ {
+ DEF_OP_NUMERIC_64(float64, float64, F64, /);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_MIN)
+ {
+ float64 a, b;
+
+ b = POP_F64();
+ a = POP_F64();
+
+ PUSH_F64(f64_min(a, b));
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_MAX)
+ {
+ float64 a, b;
+
+ b = POP_F64();
+ a = POP_F64();
+
+ PUSH_F64(f64_max(a, b));
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_COPYSIGN)
+ {
+ float64 a, b;
+
+ b = POP_F64();
+ a = POP_F64();
+ PUSH_F64(local_copysign(a, b));
+ HANDLE_OP_END();
+ }
+
+ /* conversions of i32 */
+ HANDLE_OP(WASM_OP_I32_WRAP_I64)
+ {
+ int32 value = (int32)(POP_I64() & 0xFFFFFFFFLL);
+ PUSH_I32(value);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_TRUNC_S_F32)
+ {
+ /* We don't use INT32_MIN/INT32_MAX/UINT32_MIN/UINT32_MAX,
+ since float/double values of ieee754 cannot precisely
+ represent all int32/uint32/int64/uint64 values, e.g.:
+ UINT32_MAX is 4294967295, but (float32)4294967295 is
+ 4294967296.0f, but not 4294967295.0f. */
+ DEF_OP_TRUNC_F32(-2147483904.0f, 2147483648.0f, true, true);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_TRUNC_U_F32)
+ {
+ DEF_OP_TRUNC_F32(-1.0f, 4294967296.0f, true, false);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_TRUNC_S_F64)
+ {
+ DEF_OP_TRUNC_F64(-2147483649.0, 2147483648.0, true, true);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_TRUNC_U_F64)
+ {
+ DEF_OP_TRUNC_F64(-1.0, 4294967296.0, true, false);
+ HANDLE_OP_END();
+ }
+
+ /* conversions of i64 */
+ HANDLE_OP(WASM_OP_I64_EXTEND_S_I32)
+ {
+ DEF_OP_CONVERT(int64, I64, int32, I32);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_EXTEND_U_I32)
+ {
+ DEF_OP_CONVERT(int64, I64, uint32, I32);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_TRUNC_S_F32)
+ {
+ DEF_OP_TRUNC_F32(-9223373136366403584.0f,
+ 9223372036854775808.0f, false, true);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_TRUNC_U_F32)
+ {
+ DEF_OP_TRUNC_F32(-1.0f, 18446744073709551616.0f, false, false);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_TRUNC_S_F64)
+ {
+ DEF_OP_TRUNC_F64(-9223372036854777856.0, 9223372036854775808.0,
+ false, true);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_TRUNC_U_F64)
+ {
+ DEF_OP_TRUNC_F64(-1.0, 18446744073709551616.0, false, false);
+ HANDLE_OP_END();
+ }
+
+ /* conversions of f32 */
+ HANDLE_OP(WASM_OP_F32_CONVERT_S_I32)
+ {
+ DEF_OP_CONVERT(float32, F32, int32, I32);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_CONVERT_U_I32)
+ {
+ DEF_OP_CONVERT(float32, F32, uint32, I32);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_CONVERT_S_I64)
+ {
+ DEF_OP_CONVERT(float32, F32, int64, I64);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_CONVERT_U_I64)
+ {
+ DEF_OP_CONVERT(float32, F32, uint64, I64);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F32_DEMOTE_F64)
+ {
+ DEF_OP_CONVERT(float32, F32, float64, F64);
+ HANDLE_OP_END();
+ }
+
+ /* conversions of f64 */
+ HANDLE_OP(WASM_OP_F64_CONVERT_S_I32)
+ {
+ DEF_OP_CONVERT(float64, F64, int32, I32);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_CONVERT_U_I32)
+ {
+ DEF_OP_CONVERT(float64, F64, uint32, I32);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_CONVERT_S_I64)
+ {
+ DEF_OP_CONVERT(float64, F64, int64, I64);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_CONVERT_U_I64)
+ {
+ DEF_OP_CONVERT(float64, F64, uint64, I64);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_F64_PROMOTE_F32)
+ {
+ DEF_OP_CONVERT(float64, F64, float32, F32);
+ HANDLE_OP_END();
+ }
+
+ /* reinterpretations */
+ HANDLE_OP(WASM_OP_I32_REINTERPRET_F32)
+ HANDLE_OP(WASM_OP_F32_REINTERPRET_I32)
+ {
+ DEF_OP_REINTERPRET(uint32, I32);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_REINTERPRET_F64)
+ HANDLE_OP(WASM_OP_F64_REINTERPRET_I64)
+ {
+ DEF_OP_REINTERPRET(int64, I64);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(EXT_OP_COPY_STACK_TOP)
+ {
+ addr1 = GET_OFFSET();
+ addr2 = GET_OFFSET();
+ frame_lp[addr2] = frame_lp[addr1];
+
+#if WASM_ENABLE_GC != 0
+ /* Ignore constants because they are not reference */
+ if (addr1 >= 0) {
+ if (*FRAME_REF(addr1)) {
+ CLEAR_FRAME_REF(addr1);
+ SET_FRAME_REF(addr2);
+ }
+ }
+#endif
+
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(EXT_OP_COPY_STACK_TOP_I64)
+ {
+ addr1 = GET_OFFSET();
+ addr2 = GET_OFFSET();
+
+ PUT_I64_TO_ADDR(frame_lp + addr2,
+ GET_I64_FROM_ADDR(frame_lp + addr1));
+
+#if WASM_ENABLE_GC != 0
+ /* Ignore constants because they are not reference */
+ if (addr1 >= 0) {
+ if (*FRAME_REF(addr1)) {
+ CLEAR_FRAME_REF(addr1);
+ SET_FRAME_REF(addr2);
+ }
+ }
+#endif
+
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(EXT_OP_COPY_STACK_VALUES)
+ {
+ uint32 values_count, total_cell;
+ uint8 *cells;
+ int16 *src_offsets = NULL;
+ uint16 *dst_offsets = NULL;
+
+ /* read values_count */
+ values_count = read_uint32(frame_ip);
+ /* read total cell num */
+ total_cell = read_uint32(frame_ip);
+ /* cells */
+ cells = (uint8 *)frame_ip;
+ frame_ip += values_count * CELL_SIZE;
+ /* src offsets */
+ src_offsets = (int16 *)frame_ip;
+ frame_ip += values_count * sizeof(int16);
+ /* dst offsets */
+ dst_offsets = (uint16 *)frame_ip;
+ frame_ip += values_count * sizeof(uint16);
+
+ if (!copy_stack_values(module, frame_lp, values_count,
+#if WASM_ENABLE_GC != 0
+ frame_ref,
+#endif
+ total_cell, cells, src_offsets,
+ dst_offsets))
+ goto got_exception;
+
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_SET_LOCAL)
+ {
+ opcode = WASM_OP_SET_LOCAL;
+ goto handle_op_set_tee_local;
+ }
+ HANDLE_OP(WASM_OP_TEE_LOCAL)
+ {
+ opcode = WASM_OP_TEE_LOCAL;
+ handle_op_set_tee_local:
+
+ GET_LOCAL_INDEX_TYPE_AND_OFFSET();
+ addr1 = GET_OFFSET();
+
+ if (local_type == VALUE_TYPE_I32 || local_type == VALUE_TYPE_F32
+#if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
+ || local_type == VALUE_TYPE_FUNCREF
+ || local_type == VALUE_TYPE_EXTERNREF
+#endif
+ ) {
+ *(int32 *)(frame_lp + local_offset) = frame_lp[addr1];
+ }
+ else if (local_type == VALUE_TYPE_I64
+ || local_type == VALUE_TYPE_F64) {
+ PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
+ GET_I64_FROM_ADDR(frame_lp + addr1));
+ }
+#if WASM_ENABLE_GC != 0
+ else if (wasm_is_type_reftype(local_type)) {
+ PUT_REF_TO_ADDR((uint32 *)(frame_lp + local_offset),
+ GET_REF_FROM_ADDR(frame_lp + addr1));
+ if (opcode == WASM_OP_SET_LOCAL) {
+ CLEAR_FRAME_REF(addr1);
+ }
+ }
+#endif
+ else {
+ wasm_set_exception(module, "invalid local type");
+ goto got_exception;
+ }
+
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_EXTEND8_S)
+ {
+ DEF_OP_CONVERT(int32, I32, int8, I32);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I32_EXTEND16_S)
+ {
+ DEF_OP_CONVERT(int32, I32, int16, I32);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_EXTEND8_S)
+ {
+ DEF_OP_CONVERT(int64, I64, int8, I64);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_EXTEND16_S)
+ {
+ DEF_OP_CONVERT(int64, I64, int16, I64);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_I64_EXTEND32_S)
+ {
+ DEF_OP_CONVERT(int64, I64, int32, I64);
+ HANDLE_OP_END();
+ }
+
+ HANDLE_OP(WASM_OP_MISC_PREFIX)
+ {
+ GET_OPCODE();
+ switch (opcode) {
+ case WASM_OP_I32_TRUNC_SAT_S_F32:
+ DEF_OP_TRUNC_SAT_F32(-2147483904.0f, 2147483648.0f,
+ true, true);
+ break;
+ case WASM_OP_I32_TRUNC_SAT_U_F32:
+ DEF_OP_TRUNC_SAT_F32(-1.0f, 4294967296.0f, true, false);
+ break;
+ case WASM_OP_I32_TRUNC_SAT_S_F64:
+ DEF_OP_TRUNC_SAT_F64(-2147483649.0, 2147483648.0, true,
+ true);
+ break;
+ case WASM_OP_I32_TRUNC_SAT_U_F64:
+ DEF_OP_TRUNC_SAT_F64(-1.0, 4294967296.0, true, false);
+ break;
+ case WASM_OP_I64_TRUNC_SAT_S_F32:
+ DEF_OP_TRUNC_SAT_F32(-9223373136366403584.0f,
+ 9223372036854775808.0f, false,
+ true);
+ break;
+ case WASM_OP_I64_TRUNC_SAT_U_F32:
+ DEF_OP_TRUNC_SAT_F32(-1.0f, 18446744073709551616.0f,
+ false, false);
+ break;
+ case WASM_OP_I64_TRUNC_SAT_S_F64:
+ DEF_OP_TRUNC_SAT_F64(-9223372036854777856.0,
+ 9223372036854775808.0, false,
+ true);
+ break;
+ case WASM_OP_I64_TRUNC_SAT_U_F64:
+ DEF_OP_TRUNC_SAT_F64(-1.0, 18446744073709551616.0,
+ false, false);
+ break;
+#if WASM_ENABLE_BULK_MEMORY != 0
+ case WASM_OP_MEMORY_INIT:
+ {
+ uint32 addr, segment;
+ uint64 bytes, offset, seg_len;
+ uint8 *data;
+
+ segment = read_uint32(frame_ip);
+
+ bytes = (uint64)(uint32)POP_I32();
+ offset = (uint64)(uint32)POP_I32();
+ addr = POP_I32();
+
+#if WASM_ENABLE_THREAD_MGR != 0
+ linear_mem_size = get_linear_mem_size();
+#endif
+
+#ifndef OS_ENABLE_HW_BOUND_CHECK
+ CHECK_BULK_MEMORY_OVERFLOW(addr, bytes, maddr);
+#else
+#if WASM_ENABLE_SHARED_HEAP != 0
+ if (app_addr_in_shared_heap((uint64)(uint32)addr,
+ bytes))
+ shared_heap_addr_app_to_native((uint64)(uint32)addr,
+ maddr);
+ else
+#endif
+ {
+ if ((uint64)(uint32)addr + bytes > linear_mem_size)
+ goto out_of_bounds;
+ maddr = memory->memory_data + (uint32)addr;
+ }
+#endif
+ if (bh_bitmap_get_bit(module->e->common.data_dropped,
+ segment)) {
+ seg_len = 0;
+ data = NULL;
+ }
+ else {
+ seg_len =
+ (uint64)module->module->data_segments[segment]
+ ->data_length;
+ data = module->module->data_segments[segment]->data;
+ }
+ if (offset + bytes > seg_len)
+ goto out_of_bounds;
+
+ bh_memcpy_s(maddr, (uint32)(linear_mem_size - addr),
+ data + offset, (uint32)bytes);
+ break;
+ }
+ case WASM_OP_DATA_DROP:
+ {
+ uint32 segment;
+
+ segment = read_uint32(frame_ip);
+ bh_bitmap_set_bit(module->e->common.data_dropped,
+ segment);
+ break;
+ }
+ case WASM_OP_MEMORY_COPY:
+ {
+ uint32 dst, src, len;
+ uint8 *mdst, *msrc;
+ uint64 dlen;
+
+ len = POP_I32();
+ src = POP_I32();
+ dst = POP_I32();
+
+#if WASM_ENABLE_THREAD_MGR != 0
+ linear_mem_size = get_linear_mem_size();
+#endif
+
+ dlen = linear_mem_size - dst;
+
+#ifndef OS_ENABLE_HW_BOUND_CHECK
+ CHECK_BULK_MEMORY_OVERFLOW(src, len, msrc);
+ CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
+#if WASM_ENABLE_SHARED_HEAP != 0
+ if (app_addr_in_shared_heap((uint64)dst, len))
+ dlen = shared_heap_end_off - dst + 1;
+#endif
+#else /* else of OS_ENABLE_HW_BOUND_CHECK */
+#if WASM_ENABLE_SHARED_HEAP != 0
+ if (app_addr_in_shared_heap((uint64)src, len))
+ shared_heap_addr_app_to_native((uint64)src, msrc);
+ else
+#endif
+ {
+ if ((uint64)(uint32)src + len > linear_mem_size)
+ goto out_of_bounds;
+ msrc = memory->memory_data + (uint32)src;
+ }
+
+#if WASM_ENABLE_SHARED_HEAP != 0
+ if (app_addr_in_shared_heap((uint64)dst, len)) {
+ shared_heap_addr_app_to_native((uint64)dst, mdst);
+ dlen = shared_heap_end_off - dst + 1;
+ }
+ else
+#endif
+ {
+ if ((uint64)(uint32)dst + len > linear_mem_size)
+ goto out_of_bounds;
+ mdst = memory->memory_data + (uint32)dst;
+ }
+#endif /* end of OS_ENABLE_HW_BOUND_CHECK */
+
+ /* allowing the destination and source to overlap */
+ bh_memmove_s(mdst, (uint32)dlen, msrc, len);
+ break;
+ }
+ case WASM_OP_MEMORY_FILL:
+ {
+ uint32 dst, len;
+ uint8 fill_val, *mdst;
+
+ len = POP_I32();
+ fill_val = POP_I32();
+ dst = POP_I32();
+
+#if WASM_ENABLE_THREAD_MGR != 0
+ linear_mem_size = get_linear_mem_size();
+#endif
+
+#ifndef OS_ENABLE_HW_BOUND_CHECK
+ CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
+#else
+#if WASM_ENABLE_SHARED_HEAP != 0
+ if (app_addr_in_shared_heap((uint64)(uint32)dst, len))
+ shared_heap_addr_app_to_native((uint64)(uint32)dst,
+ mdst);
+ else
+#endif
+ {
+ if ((uint64)(uint32)dst + len > linear_mem_size)
+ goto out_of_bounds;
+ mdst = memory->memory_data + (uint32)dst;
+ }
+#endif
+
+ memset(mdst, fill_val, len);
+ break;
+ }
+#endif /* WASM_ENABLE_BULK_MEMORY */
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+ case WASM_OP_TABLE_INIT:
+ {
+ uint32 tbl_idx, elem_idx;
+ uint32 n, s, d;
+ WASMTableInstance *tbl_inst;
+ table_elem_type_t *table_elems;
+ InitializerExpression *tbl_seg_init_values = NULL,
+ *init_values;
+ uint64 i;
+ uint32 tbl_seg_len = 0;
+
+ elem_idx = read_uint32(frame_ip);
+ bh_assert(elem_idx < module->module->table_seg_count);
+
+ tbl_idx = read_uint32(frame_ip);
+ bh_assert(tbl_idx < module->module->table_count);
+
+ tbl_inst = wasm_get_table_inst(module, tbl_idx);
+
+ n = (uint32)POP_I32();
+ s = (uint32)POP_I32();
+ d = (uint32)POP_I32();
+
+ if (!bh_bitmap_get_bit(module->e->common.elem_dropped,
+ elem_idx)) {
+ /* table segment isn't dropped */
+ tbl_seg_init_values =
+ module->module->table_segments[elem_idx]
+ .init_values;
+ tbl_seg_len =
+ module->module->table_segments[elem_idx]
+ .value_count;
+ }
+
+ if (offset_len_out_of_bounds(s, n, tbl_seg_len)
+ || offset_len_out_of_bounds(d, n,
+ tbl_inst->cur_size)) {
+ wasm_set_exception(module,
+ "out of bounds table access");
+ goto got_exception;
+ }
+
+ if (!n) {
+ break;
+ }
+
+ table_elems = tbl_inst->elems + d;
+ init_values = tbl_seg_init_values + s;
+#if WASM_ENABLE_GC != 0
+ SYNC_ALL_TO_FRAME();
+#endif
+ for (i = 0; i < n; i++) {
+ /* UINT32_MAX indicates that it is a null ref */
+ bh_assert(init_values[i].init_expr_type
+ == INIT_EXPR_TYPE_REFNULL_CONST
+ || init_values[i].init_expr_type
+ == INIT_EXPR_TYPE_FUNCREF_CONST);
+#if WASM_ENABLE_GC == 0
+ table_elems[i] =
+ (table_elem_type_t)init_values[i].u.ref_index;
+#else
+ if (init_values[i].u.ref_index != UINT32_MAX) {
+ if (!(func_obj = wasm_create_func_obj(
+ module, init_values[i].u.ref_index,
+ true, NULL, 0))) {
+ goto got_exception;
+ }
+ table_elems[i] = func_obj;
+ }
+ else {
+ table_elems[i] = NULL_REF;
+ }
+#endif
+ }
+
+ break;
+ }
+ case WASM_OP_ELEM_DROP:
+ {
+ uint32 elem_idx = read_uint32(frame_ip);
+ bh_assert(elem_idx < module->module->table_seg_count);
+ bh_bitmap_set_bit(module->e->common.elem_dropped,
+ elem_idx);
+ break;
+ }
+ case WASM_OP_TABLE_COPY:
+ {
+ uint32 src_tbl_idx, dst_tbl_idx;
+ uint32 n, s, d;
+ WASMTableInstance *src_tbl_inst, *dst_tbl_inst;
+
+ dst_tbl_idx = read_uint32(frame_ip);
+ bh_assert(dst_tbl_idx < module->table_count);
+
+ dst_tbl_inst = wasm_get_table_inst(module, dst_tbl_idx);
+
+ src_tbl_idx = read_uint32(frame_ip);
+ bh_assert(src_tbl_idx < module->table_count);
+
+ src_tbl_inst = wasm_get_table_inst(module, src_tbl_idx);
+
+ n = (uint32)POP_I32();
+ s = (uint32)POP_I32();
+ d = (uint32)POP_I32();
+
+ if (offset_len_out_of_bounds(d, n,
+ dst_tbl_inst->cur_size)
+ || offset_len_out_of_bounds(
+ s, n, src_tbl_inst->cur_size)) {
+ wasm_set_exception(module,
+ "out of bounds table access");
+ goto got_exception;
+ }
+
+ /* if s >= d, copy from front to back */
+ /* if s < d, copy from back to front */
+ /* merge all together */
+ bh_memmove_s((uint8 *)dst_tbl_inst
+ + offsetof(WASMTableInstance, elems)
+ + d * sizeof(table_elem_type_t),
+ (uint32)((dst_tbl_inst->cur_size - d)
+ * sizeof(table_elem_type_t)),
+ (uint8 *)src_tbl_inst
+ + offsetof(WASMTableInstance, elems)
+ + s * sizeof(table_elem_type_t),
+ (uint32)(n * sizeof(table_elem_type_t)));
+ break;
+ }
+ case WASM_OP_TABLE_GROW:
+ {
+ uint32 tbl_idx, n, orig_tbl_sz;
+ WASMTableInstance *tbl_inst;
+ table_elem_type_t init_val;
+
+ tbl_idx = read_uint32(frame_ip);
+ bh_assert(tbl_idx < module->table_count);
+
+ tbl_inst = wasm_get_table_inst(module, tbl_idx);
+
+ orig_tbl_sz = tbl_inst->cur_size;
+
+ n = POP_I32();
+#if WASM_ENABLE_GC == 0
+ init_val = POP_I32();
+#else
+ init_val = POP_REF();
+#endif
+
+ if (!wasm_enlarge_table(module, tbl_idx, n, init_val)) {
+ PUSH_I32(-1);
+ }
+ else {
+ PUSH_I32(orig_tbl_sz);
+ }
+
+ break;
+ }
+ case WASM_OP_TABLE_SIZE:
+ {
+ uint32 tbl_idx;
+ WASMTableInstance *tbl_inst;
+
+ tbl_idx = read_uint32(frame_ip);
+ bh_assert(tbl_idx < module->table_count);
+
+ tbl_inst = wasm_get_table_inst(module, tbl_idx);
+
+ PUSH_I32(tbl_inst->cur_size);
+ break;
+ }
+ case WASM_OP_TABLE_FILL:
+ {
+ uint32 tbl_idx, n, i;
+ WASMTableInstance *tbl_inst;
+ table_elem_type_t fill_val;
+
+ tbl_idx = read_uint32(frame_ip);
+ bh_assert(tbl_idx < module->table_count);
+
+ tbl_inst = wasm_get_table_inst(module, tbl_idx);
+
+ n = POP_I32();
+#if WASM_ENABLE_GC == 0
+ fill_val = POP_I32();
+#else
+ fill_val = POP_REF();
+#endif
+ i = POP_I32();
+
+ if (offset_len_out_of_bounds(i, n,
+ tbl_inst->cur_size)) {
+ wasm_set_exception(module,
+ "out of bounds table access");
+ goto got_exception;
+ }
+
+ for (; n != 0; i++, n--) {
+ tbl_inst->elems[i] = fill_val;
+ }
+
+ break;
+ }
+#endif /* WASM_ENABLE_REF_TYPES */
+ default:
+ wasm_set_exception(module, "unsupported opcode");
+ goto got_exception;
+ }
+ HANDLE_OP_END();
+ }
+
+#if WASM_ENABLE_SHARED_MEMORY != 0
+ HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
+ {
+ uint32 offset = 0, addr;
+
+ GET_OPCODE();
+
+ if (opcode != WASM_OP_ATOMIC_FENCE) {
+ offset = read_uint32(frame_ip);
+ }
+
+ switch (opcode) {
+ case WASM_OP_ATOMIC_NOTIFY:
+ {
+ uint32 notify_count, ret;
+
+ notify_count = POP_I32();
+ addr = POP_I32();
+ CHECK_MEMORY_OVERFLOW(4);
+ CHECK_ATOMIC_MEMORY_ACCESS(4);
+
+ ret = wasm_runtime_atomic_notify(
+ (WASMModuleInstanceCommon *)module, maddr,
+ notify_count);
+ if (ret == (uint32)-1)
+ goto got_exception;
+
+ PUSH_I32(ret);
+ break;
+ }
+ case WASM_OP_ATOMIC_WAIT32:
+ {
+ uint64 timeout;
+ uint32 expect, ret;
+
+ timeout = POP_I64();
+ expect = POP_I32();
+ addr = POP_I32();
+ CHECK_MEMORY_OVERFLOW(4);
+ CHECK_ATOMIC_MEMORY_ACCESS(4);
+
+ ret = wasm_runtime_atomic_wait(
+ (WASMModuleInstanceCommon *)module, maddr,
+ (uint64)expect, timeout, false);
+ if (ret == (uint32)-1)
+ goto got_exception;
+
+#if WASM_ENABLE_THREAD_MGR != 0
+ CHECK_SUSPEND_FLAGS();
+#endif
+
+ PUSH_I32(ret);
+ break;
+ }
+ case WASM_OP_ATOMIC_WAIT64:
+ {
+ uint64 timeout, expect;
+ uint32 ret;
+
+ timeout = POP_I64();
+ expect = POP_I64();
+ addr = POP_I32();
+ CHECK_MEMORY_OVERFLOW(8);
+ CHECK_ATOMIC_MEMORY_ACCESS(8);
+
+ ret = wasm_runtime_atomic_wait(
+ (WASMModuleInstanceCommon *)module, maddr, expect,
+ timeout, true);
+ if (ret == (uint32)-1)
+ goto got_exception;
+
+#if WASM_ENABLE_THREAD_MGR != 0
+ CHECK_SUSPEND_FLAGS();
+#endif
+
+ PUSH_I32(ret);
+ break;
+ }
+ case WASM_OP_ATOMIC_FENCE:
+ {
+ os_atomic_thread_fence(os_memory_order_seq_cst);
+ break;
+ }
+
+ case WASM_OP_ATOMIC_I32_LOAD:
+ case WASM_OP_ATOMIC_I32_LOAD8_U:
+ case WASM_OP_ATOMIC_I32_LOAD16_U:
+ {
+ uint32 readv;
+
+ addr = POP_I32();
+
+ if (opcode == WASM_OP_ATOMIC_I32_LOAD8_U) {
+ CHECK_MEMORY_OVERFLOW(1);
+ CHECK_ATOMIC_MEMORY_ACCESS(1);
+ shared_memory_lock(memory);
+ readv = (uint32)(*(uint8 *)maddr);
+ shared_memory_unlock(memory);
+ }
+ else if (opcode == WASM_OP_ATOMIC_I32_LOAD16_U) {
+ CHECK_MEMORY_OVERFLOW(2);
+ CHECK_ATOMIC_MEMORY_ACCESS(2);
+ shared_memory_lock(memory);
+ readv = (uint32)LOAD_U16(maddr);
+ shared_memory_unlock(memory);
+ }
+ else {
+ CHECK_MEMORY_OVERFLOW(4);
+ CHECK_ATOMIC_MEMORY_ACCESS(4);
+ shared_memory_lock(memory);
+ readv = LOAD_I32(maddr);
+ shared_memory_unlock(memory);
+ }
+
+ PUSH_I32(readv);
+ 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:
+ {
+ uint64 readv;
+
+ addr = POP_I32();
+
+ if (opcode == WASM_OP_ATOMIC_I64_LOAD8_U) {
+ CHECK_MEMORY_OVERFLOW(1);
+ CHECK_ATOMIC_MEMORY_ACCESS(1);
+ shared_memory_lock(memory);
+ readv = (uint64)(*(uint8 *)maddr);
+ shared_memory_unlock(memory);
+ }
+ else if (opcode == WASM_OP_ATOMIC_I64_LOAD16_U) {
+ CHECK_MEMORY_OVERFLOW(2);
+ CHECK_ATOMIC_MEMORY_ACCESS(2);
+ shared_memory_lock(memory);
+ readv = (uint64)LOAD_U16(maddr);
+ shared_memory_unlock(memory);
+ }
+ else if (opcode == WASM_OP_ATOMIC_I64_LOAD32_U) {
+ CHECK_MEMORY_OVERFLOW(4);
+ CHECK_ATOMIC_MEMORY_ACCESS(4);
+ shared_memory_lock(memory);
+ readv = (uint64)LOAD_U32(maddr);
+ shared_memory_unlock(memory);
+ }
+ else {
+ CHECK_MEMORY_OVERFLOW(8);
+ CHECK_ATOMIC_MEMORY_ACCESS(8);
+ shared_memory_lock(memory);
+ readv = LOAD_I64(maddr);
+ shared_memory_unlock(memory);
+ }
+
+ PUSH_I64(readv);
+ break;
+ }
+ case WASM_OP_ATOMIC_I32_STORE:
+ case WASM_OP_ATOMIC_I32_STORE8:
+ case WASM_OP_ATOMIC_I32_STORE16:
+ {
+ uint32 sval;
+
+ sval = (uint32)POP_I32();
+ addr = POP_I32();
+
+ if (opcode == WASM_OP_ATOMIC_I32_STORE8) {
+ CHECK_MEMORY_OVERFLOW(1);
+ CHECK_ATOMIC_MEMORY_ACCESS(1);
+ shared_memory_lock(memory);
+ *(uint8 *)maddr = (uint8)sval;
+ shared_memory_unlock(memory);
+ }
+ else if (opcode == WASM_OP_ATOMIC_I32_STORE16) {
+ CHECK_MEMORY_OVERFLOW(2);
+ CHECK_ATOMIC_MEMORY_ACCESS(2);
+ shared_memory_lock(memory);
+ STORE_U16(maddr, (uint16)sval);
+ shared_memory_unlock(memory);
+ }
+ else {
+ CHECK_MEMORY_OVERFLOW(4);
+ CHECK_ATOMIC_MEMORY_ACCESS(4);
+ shared_memory_lock(memory);
+ STORE_U32(maddr, sval);
+ shared_memory_unlock(memory);
+ }
+ 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:
+ {
+ uint64 sval;
+
+ sval = (uint64)POP_I64();
+ addr = POP_I32();
+
+ if (opcode == WASM_OP_ATOMIC_I64_STORE8) {
+ CHECK_MEMORY_OVERFLOW(1);
+ CHECK_ATOMIC_MEMORY_ACCESS(1);
+ shared_memory_lock(memory);
+ *(uint8 *)maddr = (uint8)sval;
+ shared_memory_unlock(memory);
+ }
+ else if (opcode == WASM_OP_ATOMIC_I64_STORE16) {
+ CHECK_MEMORY_OVERFLOW(2);
+ CHECK_ATOMIC_MEMORY_ACCESS(2);
+ shared_memory_lock(memory);
+ STORE_U16(maddr, (uint16)sval);
+ shared_memory_unlock(memory);
+ }
+ else if (opcode == WASM_OP_ATOMIC_I64_STORE32) {
+ CHECK_MEMORY_OVERFLOW(4);
+ CHECK_ATOMIC_MEMORY_ACCESS(4);
+ shared_memory_lock(memory);
+ STORE_U32(maddr, (uint32)sval);
+ shared_memory_unlock(memory);
+ }
+ else {
+ CHECK_MEMORY_OVERFLOW(8);
+ CHECK_ATOMIC_MEMORY_ACCESS(8);
+ shared_memory_lock(memory);
+ STORE_I64(maddr, sval);
+ shared_memory_unlock(memory);
+ }
+ break;
+ }
+
+ case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
+ case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
+ case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
+ {
+ uint32 readv, sval, expect;
+
+ sval = POP_I32();
+ expect = POP_I32();
+ addr = POP_I32();
+
+ if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U) {
+ CHECK_MEMORY_OVERFLOW(1);
+ CHECK_ATOMIC_MEMORY_ACCESS(1);
+
+ expect = (uint8)expect;
+ shared_memory_lock(memory);
+ readv = (uint32)(*(uint8 *)maddr);
+ if (readv == expect)
+ *(uint8 *)maddr = (uint8)(sval);
+ shared_memory_unlock(memory);
+ }
+ else if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U) {
+ CHECK_MEMORY_OVERFLOW(2);
+ CHECK_ATOMIC_MEMORY_ACCESS(2);
+
+ expect = (uint16)expect;
+ shared_memory_lock(memory);
+ readv = (uint32)LOAD_U16(maddr);
+ if (readv == expect)
+ STORE_U16(maddr, (uint16)(sval));
+ shared_memory_unlock(memory);
+ }
+ else {
+ CHECK_MEMORY_OVERFLOW(4);
+ CHECK_ATOMIC_MEMORY_ACCESS(4);
+
+ shared_memory_lock(memory);
+ readv = LOAD_I32(maddr);
+ if (readv == expect)
+ STORE_U32(maddr, sval);
+ shared_memory_unlock(memory);
+ }
+ PUSH_I32(readv);
+ 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:
+ {
+ uint64 readv, sval, expect;
+
+ sval = (uint64)POP_I64();
+ expect = (uint64)POP_I64();
+ addr = POP_I32();
+
+ if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U) {
+ CHECK_MEMORY_OVERFLOW(1);
+ CHECK_ATOMIC_MEMORY_ACCESS(1);
+
+ expect = (uint8)expect;
+ shared_memory_lock(memory);
+ readv = (uint64)(*(uint8 *)maddr);
+ if (readv == expect)
+ *(uint8 *)maddr = (uint8)(sval);
+ shared_memory_unlock(memory);
+ }
+ else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U) {
+ CHECK_MEMORY_OVERFLOW(2);
+ CHECK_ATOMIC_MEMORY_ACCESS(2);
+
+ expect = (uint16)expect;
+ shared_memory_lock(memory);
+ readv = (uint64)LOAD_U16(maddr);
+ if (readv == expect)
+ STORE_U16(maddr, (uint16)(sval));
+ shared_memory_unlock(memory);
+ }
+ else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U) {
+ CHECK_MEMORY_OVERFLOW(4);
+ CHECK_ATOMIC_MEMORY_ACCESS(4);
+
+ expect = (uint32)expect;
+ shared_memory_lock(memory);
+ readv = (uint64)LOAD_U32(maddr);
+ if (readv == expect)
+ STORE_U32(maddr, (uint32)(sval));
+ shared_memory_unlock(memory);
+ }
+ else {
+ CHECK_MEMORY_OVERFLOW(8);
+ CHECK_ATOMIC_MEMORY_ACCESS(8);
+
+ shared_memory_lock(memory);
+ readv = (uint64)LOAD_I64(maddr);
+ if (readv == expect)
+ STORE_I64(maddr, sval);
+ shared_memory_unlock(memory);
+ }
+ PUSH_I64(readv);
+ break;
+ }
+
+ DEF_ATOMIC_RMW_OPCODE(ADD, +);
+ DEF_ATOMIC_RMW_OPCODE(SUB, -);
+ DEF_ATOMIC_RMW_OPCODE(AND, &);
+ DEF_ATOMIC_RMW_OPCODE(OR, |);
+ DEF_ATOMIC_RMW_OPCODE(XOR, ^);
+ /* xchg, ignore the read value, and store the given
+ value: readv * 0 + sval */
+ DEF_ATOMIC_RMW_OPCODE(XCHG, *0 +);
+ }
+
+ HANDLE_OP_END();
+ }
+#endif
+
+ HANDLE_OP(WASM_OP_IMPDEP)
+ {
+ frame = prev_frame;
+ frame_ip = frame->ip;
+#if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
+ is_return_call = false;
+#endif
+ goto call_func_from_entry;
+ }
+
+ HANDLE_OP(WASM_OP_CALL)
+ {
+#if WASM_ENABLE_THREAD_MGR != 0
+ CHECK_SUSPEND_FLAGS();
+#endif
+ fidx = read_uint32(frame_ip);
+#if WASM_ENABLE_MULTI_MODULE != 0
+ if (fidx >= module->e->function_count) {
+ wasm_set_exception(module, "unknown function");
+ goto got_exception;
+ }
+#endif
+ cur_func = module->e->functions + fidx;
+ goto call_func_from_interp;
+ }
+
+#if WASM_ENABLE_TAIL_CALL != 0
+ HANDLE_OP(WASM_OP_RETURN_CALL)
+ {
+#if WASM_ENABLE_THREAD_MGR != 0
+ CHECK_SUSPEND_FLAGS();
+#endif
+ fidx = read_uint32(frame_ip);
+#if WASM_ENABLE_MULTI_MODULE != 0
+ if (fidx >= module->e->function_count) {
+ wasm_set_exception(module, "unknown function");
+ goto got_exception;
+ }
+#endif
+ cur_func = module->e->functions + fidx;
+ goto call_func_from_return_call;
+ }
+#endif /* WASM_ENABLE_TAIL_CALL */
+
+#if WASM_ENABLE_LABELS_AS_VALUES == 0
+ default:
+ wasm_set_exception(module, "unsupported opcode");
+ goto got_exception;
+ }
+#endif
+
+#if WASM_ENABLE_LABELS_AS_VALUES != 0
+ HANDLE_OP(WASM_OP_UNUSED_0x0a)
+#if WASM_ENABLE_TAIL_CALL == 0
+ HANDLE_OP(WASM_OP_RETURN_CALL)
+ HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
+#endif
+#if WASM_ENABLE_SHARED_MEMORY == 0
+ HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
+#endif
+#if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
+ HANDLE_OP(WASM_OP_TABLE_GET)
+ HANDLE_OP(WASM_OP_TABLE_SET)
+ HANDLE_OP(WASM_OP_REF_NULL)
+ HANDLE_OP(WASM_OP_REF_IS_NULL)
+ HANDLE_OP(WASM_OP_REF_FUNC)
+#endif
+#if WASM_ENABLE_GC == 0
+ /* SELECT_T is converted to SELECT or SELECT_64 */
+ HANDLE_OP(WASM_OP_SELECT_T)
+ HANDLE_OP(WASM_OP_CALL_REF)
+ HANDLE_OP(WASM_OP_RETURN_CALL_REF)
+ HANDLE_OP(WASM_OP_REF_EQ)
+ HANDLE_OP(WASM_OP_REF_AS_NON_NULL)
+ HANDLE_OP(WASM_OP_BR_ON_NULL)
+ HANDLE_OP(WASM_OP_BR_ON_NON_NULL)
+ HANDLE_OP(WASM_OP_GC_PREFIX)
+#endif
+#if WASM_ENABLE_EXCE_HANDLING == 0
+ /* if exception handling is disabled, these opcodes issue a trap */
+ HANDLE_OP(WASM_OP_TRY)
+ HANDLE_OP(WASM_OP_CATCH)
+ HANDLE_OP(WASM_OP_THROW)
+ HANDLE_OP(WASM_OP_RETHROW)
+ HANDLE_OP(WASM_OP_DELEGATE)
+ HANDLE_OP(WASM_OP_CATCH_ALL)
+ HANDLE_OP(EXT_OP_TRY)
+#endif
+ HANDLE_OP(WASM_OP_UNUSED_0x16)
+ HANDLE_OP(WASM_OP_UNUSED_0x17)
+ HANDLE_OP(WASM_OP_UNUSED_0x27)
+ /* optimized op code */
+ HANDLE_OP(WASM_OP_F32_STORE)
+ HANDLE_OP(WASM_OP_F64_STORE)
+ HANDLE_OP(WASM_OP_F32_LOAD)
+ HANDLE_OP(WASM_OP_F64_LOAD)
+ HANDLE_OP(EXT_OP_GET_LOCAL_FAST)
+ HANDLE_OP(WASM_OP_GET_LOCAL)
+ HANDLE_OP(WASM_OP_DROP)
+ HANDLE_OP(WASM_OP_DROP_64)
+ HANDLE_OP(WASM_OP_BLOCK)
+ HANDLE_OP(WASM_OP_LOOP)
+ HANDLE_OP(WASM_OP_END)
+ HANDLE_OP(WASM_OP_NOP)
+ HANDLE_OP(EXT_OP_BLOCK)
+ HANDLE_OP(EXT_OP_LOOP)
+ HANDLE_OP(EXT_OP_IF)
+ HANDLE_OP(EXT_OP_BR_TABLE_CACHE)
+ {
+ wasm_set_exception(module, "unsupported opcode");
+ goto got_exception;
+ }
+#endif
+
+#if WASM_ENABLE_LABELS_AS_VALUES == 0
+ continue;
+#else
+ FETCH_OPCODE_AND_DISPATCH();
+#endif
+
+#if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
+ call_func_from_return_call:
+ {
+ uint32 *lp_base = NULL, *lp = NULL;
+ int i;
+
+ if (cur_func->param_cell_num > 0
+ && !(lp_base = lp = wasm_runtime_malloc(cur_func->param_cell_num
+ * sizeof(uint32)))) {
+ wasm_set_exception(module, "allocate memory failed");
+ goto got_exception;
+ }
+ for (i = 0; i < cur_func->param_count; i++) {
+ if (cur_func->param_types[i] == VALUE_TYPE_I64
+ || cur_func->param_types[i] == VALUE_TYPE_F64) {
+ PUT_I64_TO_ADDR(
+ lp, GET_OPERAND(uint64, I64,
+ 2 * (cur_func->param_count - i - 1)));
+ lp += 2;
+ }
+ else {
+ *lp = GET_OPERAND(uint32, I32,
+ (2 * (cur_func->param_count - i - 1)));
+ lp++;
+ }
+ }
+ frame->lp = frame->operand + cur_func->const_cell_num;
+ if (lp - lp_base > 0) {
+ word_copy(frame->lp, lp_base, lp - lp_base);
+ }
+ if (lp_base)
+ wasm_runtime_free(lp_base);
+ FREE_FRAME(exec_env, frame);
+ frame_ip += cur_func->param_count * sizeof(int16);
+ wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame);
+ is_return_call = true;
+ goto call_func_from_entry;
+ }
+#endif /* WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0 */
+
+ call_func_from_interp:
+ {
+ /* Only do the copy when it's called from interpreter. */
+ WASMInterpFrame *outs_area = wasm_exec_env_wasm_stack_top(exec_env);
+ int i;
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+ if (cur_func->is_import_func) {
+ outs_area->lp = outs_area->operand
+ + (cur_func->import_func_inst
+ ? cur_func->import_func_inst->const_cell_num
+ : 0);
+ }
+ else
+#endif
+ {
+ outs_area->lp = outs_area->operand + cur_func->const_cell_num;
+ }
+
+ if ((uint8 *)(outs_area->lp + cur_func->param_cell_num)
+ > exec_env->wasm_stack.top_boundary) {
+ wasm_set_exception(module, "wasm operand stack overflow");
+ goto got_exception;
+ }
+
+ for (i = 0; i < cur_func->param_count; i++) {
+ if (cur_func->param_types[i] == VALUE_TYPE_I64
+ || cur_func->param_types[i] == VALUE_TYPE_F64) {
+ PUT_I64_TO_ADDR(
+ outs_area->lp,
+ GET_OPERAND(uint64, I64,
+ 2 * (cur_func->param_count - i - 1)));
+ outs_area->lp += 2;
+ }
+#if WASM_ENABLE_GC != 0
+ else if (wasm_is_type_reftype(cur_func->param_types[i])) {
+ PUT_REF_TO_ADDR(
+ outs_area->lp,
+ GET_OPERAND(void *, REF,
+ 2 * (cur_func->param_count - i - 1)));
+ CLEAR_FRAME_REF(
+ *(uint16 *)(frame_ip
+ + (2 * (cur_func->param_count - i - 1))));
+ outs_area->lp += REF_CELL_NUM;
+ }
+#endif
+ else {
+ *outs_area->lp = GET_OPERAND(
+ uint32, I32, (2 * (cur_func->param_count - i - 1)));
+ outs_area->lp++;
+ }
+ }
+ frame_ip += cur_func->param_count * sizeof(int16);
+ if (cur_func->ret_cell_num != 0) {
+ /* Get the first return value's offset. Since loader emit
+ * all return values' offset so we must skip remain return
+ * values' offsets.
+ */
+ WASMFuncType *func_type;
+ if (cur_func->is_import_func)
+ func_type = cur_func->u.func_import->func_type;
+ else
+ func_type = cur_func->u.func->func_type;
+ frame->ret_offset = GET_OFFSET();
+ frame_ip += 2 * (func_type->result_count - 1);
+ }
+ SYNC_ALL_TO_FRAME();
+ prev_frame = frame;
+#if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
+ is_return_call = false;
+#endif
+ }
+
+ call_func_from_entry:
+ {
+ if (cur_func->is_import_func) {
+#if WASM_ENABLE_MULTI_MODULE != 0
+ if (cur_func->import_func_inst) {
+ wasm_interp_call_func_import(module, exec_env, cur_func,
+ prev_frame);
+ }
+ else
+#endif
+ {
+ wasm_interp_call_func_native(module, exec_env, cur_func,
+ prev_frame);
+ }
+
+#if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
+ if (is_return_call) {
+ /* the frame was freed before tail calling and
+ the prev_frame was set as exec_env's cur_frame,
+ so here we recover context from prev_frame */
+ RECOVER_CONTEXT(prev_frame);
+ }
+ else
+#endif
+ {
+ prev_frame = frame->prev_frame;
+ cur_func = frame->function;
+ UPDATE_ALL_FROM_FRAME();
+ }
+
+ /* update memory size, no need to update memory ptr as
+ it isn't changed in wasm_enlarge_memory */
+#if !defined(OS_ENABLE_HW_BOUND_CHECK) \
+ || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
+ || WASM_ENABLE_BULK_MEMORY != 0
+ if (memory)
+ linear_mem_size = get_linear_mem_size();
+#endif
+ if (wasm_copy_exception(module, NULL))
+ goto got_exception;
+ }
+ else {
+ WASMFunction *cur_wasm_func = cur_func->u.func;
+ uint32 cell_num_of_local_stack;
+#if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
+ uint32 i, local_cell_idx;
+#endif
+
+ cell_num_of_local_stack = cur_func->param_cell_num
+ + cur_func->local_cell_num
+ + cur_wasm_func->max_stack_cell_num;
+ all_cell_num = cur_func->const_cell_num + cell_num_of_local_stack;
+#if WASM_ENABLE_GC != 0
+ /* area of frame_ref */
+ all_cell_num += (cell_num_of_local_stack + 3) / 4;
+ /* cells occupied by locals, POP_REF should not clear frame_ref for
+ * these cells */
+ local_cell_num =
+ cur_func->param_cell_num + cur_func->local_cell_num;
+#endif
+ /* param_cell_num, local_cell_num, const_cell_num and
+ max_stack_cell_num are all no larger than UINT16_MAX (checked
+ in loader), all_cell_num must be smaller than 1MB */
+ bh_assert(all_cell_num < 1 * BH_MB);
+
+ frame_size = wasm_interp_interp_frame_size(all_cell_num);
+ if (!(frame = ALLOC_FRAME(exec_env, frame_size, prev_frame))) {
+ frame = prev_frame;
+ goto got_exception;
+ }
+
+ /* Initialize the interpreter context. */
+ frame->function = cur_func;
+ frame_ip = wasm_get_func_code(cur_func);
+ frame_ip_end = wasm_get_func_code_end(cur_func);
+
+ frame_lp = frame->lp =
+ frame->operand + cur_wasm_func->const_cell_num;
+
+ /* Initialize the consts */
+ if (cur_wasm_func->const_cell_num > 0) {
+ word_copy(frame->operand, (uint32 *)cur_wasm_func->consts,
+ cur_wasm_func->const_cell_num);
+ }
+
+ /* Initialize the local variables */
+ memset(frame_lp + cur_func->param_cell_num, 0,
+ (uint32)(cur_func->local_cell_num * 4));
+
+#if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
+ /* externref/funcref should be NULL_REF rather than 0 */
+ local_cell_idx = cur_func->param_cell_num;
+ for (i = 0; i < cur_wasm_func->local_count; i++) {
+ if (cur_wasm_func->local_types[i] == VALUE_TYPE_EXTERNREF
+ || cur_wasm_func->local_types[i] == VALUE_TYPE_FUNCREF) {
+ *(frame_lp + local_cell_idx) = NULL_REF;
+ }
+ local_cell_idx +=
+ wasm_value_type_cell_num(cur_wasm_func->local_types[i]);
+ }
+#endif
+
+#if WASM_ENABLE_GC != 0
+ /* frame->ip is used during GC root set enumeration, so we must
+ * initialized this field here */
+ frame->ip = frame_ip;
+ frame_ref = frame->frame_ref =
+ (uint8 *)(frame->lp + (uint32)cell_num_of_local_stack);
+ init_frame_refs(frame_ref, (uint32)cell_num_of_local_stack,
+ cur_func);
+#endif
+
+ wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)frame);
+ }
+#if WASM_ENABLE_THREAD_MGR != 0
+ CHECK_SUSPEND_FLAGS();
+#endif
+ HANDLE_OP_END();
+ }
+
+ return_func:
+ {
+ FREE_FRAME(exec_env, frame);
+ wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame);
+
+ if (!prev_frame->ip)
+ /* Called from native. */
+ return;
+
+ RECOVER_CONTEXT(prev_frame);
+#if WASM_ENABLE_GC != 0
+ local_cell_num = cur_func->param_cell_num + cur_func->local_cell_num;
+#endif
+ HANDLE_OP_END();
+ }
+
+ (void)frame_ip_end;
+
+#if WASM_ENABLE_SHARED_MEMORY != 0
+ unaligned_atomic:
+ wasm_set_exception(module, "unaligned atomic");
+ goto got_exception;
+#endif
+
+#if !defined(OS_ENABLE_HW_BOUND_CHECK) \
+ || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
+ || WASM_ENABLE_BULK_MEMORY != 0
+ out_of_bounds:
+ wasm_set_exception(module, "out of bounds memory access");
+#endif
+
+ got_exception:
+ SYNC_ALL_TO_FRAME();
+ return;
+
+#if WASM_ENABLE_LABELS_AS_VALUES == 0
+ }
+#else
+ FETCH_OPCODE_AND_DISPATCH();
+#endif
+}
+
+#if WASM_ENABLE_LABELS_AS_VALUES != 0
+void **
+wasm_interp_get_handle_table(void)
+{
+ WASMModuleInstance module;
+ memset(&module, 0, sizeof(WASMModuleInstance));
+ wasm_interp_call_func_bytecode(&module, NULL, NULL, NULL);
+ return global_handle_table;
+}
+#endif
+
+#if WASM_ENABLE_GC != 0
+bool
+wasm_interp_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap)
+{
+ WASMInterpFrame *frame;
+ WASMObjectRef gc_obj;
+ WASMFunctionInstance *cur_func;
+ uint8 *frame_ref;
+ uint32 local_cell_num, i;
+
+ frame = wasm_exec_env_get_cur_frame(exec_env);
+ for (; frame; frame = frame->prev_frame) {
+ frame_ref = frame->frame_ref;
+ cur_func = frame->function;
+
+ if (!cur_func)
+ continue;
+
+ local_cell_num = cur_func->param_cell_num;
+ if (frame->ip)
+ local_cell_num +=
+ cur_func->local_cell_num + cur_func->u.func->max_stack_cell_num;
+
+ for (i = 0; i < local_cell_num; i++) {
+ if (frame_ref[i]) {
+ gc_obj = GET_REF_FROM_ADDR(frame->lp + i);
+ if (wasm_obj_is_created_from_heap(gc_obj)) {
+ if (mem_allocator_add_root((mem_allocator_t)heap, gc_obj)) {
+ return false;
+ }
+ }
+#if UINTPTR_MAX == UINT64_MAX
+ bh_assert(frame_ref[i + 1]);
+ i++;
+#endif
+ }
+ }
+ }
+ return true;
+}
+#endif
+
+void
+wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
+ WASMFunctionInstance *function, uint32 argc,
+ uint32 argv[])
+{
+ WASMRuntimeFrame *prev_frame = wasm_exec_env_get_cur_frame(exec_env);
+ WASMInterpFrame *frame, *outs_area;
+
+ /* Allocate sufficient cells for all kinds of return values. */
+ unsigned all_cell_num =
+ function->ret_cell_num > 2 ? function->ret_cell_num : 2,
+ i;
+ /* This frame won't be used by JITed code, so only allocate interp
+ frame here. */
+ unsigned frame_size;
+
+#if WASM_ENABLE_GC != 0
+ all_cell_num += (all_cell_num + 3) / 4;
+#endif
+
+ frame_size = wasm_interp_interp_frame_size(all_cell_num);
+
+ if (argc < function->param_cell_num) {
+ char buf[128];
+ snprintf(buf, sizeof(buf),
+ "invalid argument count %" PRIu32
+ ", must be no smaller than %" PRIu32,
+ argc, (uint32)function->param_cell_num);
+ wasm_set_exception(module_inst, buf);
+ return;
+ }
+ argc = function->param_cell_num;
+
+#if defined(OS_ENABLE_HW_BOUND_CHECK) && WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
+ /*
+ * wasm_runtime_detect_native_stack_overflow is done by
+ * call_wasm_with_hw_bound_check.
+ */
+#else
+ if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
+ return;
+ }
+#endif
+
+ if (!(frame =
+ ALLOC_FRAME(exec_env, frame_size, (WASMInterpFrame *)prev_frame)))
+ return;
+
+ outs_area = wasm_exec_env_wasm_stack_top(exec_env);
+ frame->function = NULL;
+ frame->ip = NULL;
+ /* There is no local variable. */
+ frame->lp = frame->operand + 0;
+#if WASM_ENABLE_GC != 0
+ frame->frame_ref =
+ (uint8 *)(frame->lp
+ + (function->ret_cell_num > 2 ? function->ret_cell_num : 2));
+#endif
+ frame->ret_offset = 0;
+
+ if ((uint8 *)(outs_area->operand + function->const_cell_num + argc)
+ > exec_env->wasm_stack.top_boundary) {
+ wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
+ "wasm operand stack overflow");
+ return;
+ }
+
+ if (argc > 0)
+ word_copy(outs_area->operand + function->const_cell_num, argv, argc);
+
+ wasm_exec_env_set_cur_frame(exec_env, frame);
+
+#if defined(os_writegsbase)
+ {
+ WASMMemoryInstance *memory_inst = wasm_get_default_memory(module_inst);
+ if (memory_inst)
+ /* write base addr of linear memory to GS segment register */
+ os_writegsbase(memory_inst->memory_data);
+ }
+#endif
+
+ if (function->is_import_func) {
+#if WASM_ENABLE_MULTI_MODULE != 0
+ if (function->import_module_inst) {
+ LOG_DEBUG("it is a function of a sub module");
+ wasm_interp_call_func_import(module_inst, exec_env, function,
+ frame);
+ }
+ else
+#endif
+ {
+ LOG_DEBUG("it is an native function");
+ wasm_interp_call_func_native(module_inst, exec_env, function,
+ frame);
+ }
+ }
+ else {
+ wasm_interp_call_func_bytecode(module_inst, exec_env, function, frame);
+ }
+
+ /* Output the return value to the caller */
+ if (!wasm_copy_exception(module_inst, NULL)) {
+ for (i = 0; i < function->ret_cell_num; i++)
+ argv[i] = *(frame->lp + i);
+ }
+ else {
+#if WASM_ENABLE_DUMP_CALL_STACK != 0
+ if (wasm_interp_create_call_stack(exec_env)) {
+ wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
+ }
+#endif
+ }
+
+ wasm_exec_env_set_cur_frame(exec_env, prev_frame);
+ FREE_FRAME(exec_env, frame);
+#if WASM_ENABLE_OPCODE_COUNTER != 0
+ wasm_interp_dump_op_count();
+#endif
+}
diff --git a/lib/wamr/core/iwasm/interpreter/wasm_loader.c b/lib/wamr/core/iwasm/interpreter/wasm_loader.c
new file mode 100644
index 0000000..ae823d7
--- /dev/null
+++ b/lib/wamr/core/iwasm/interpreter/wasm_loader.c
@@ -0,0 +1,15902 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include "wasm_loader.h"
+#include "bh_platform.h"
+#include "wasm.h"
+#include "wasm_opcode.h"
+#include "wasm_runtime.h"
+#include "wasm_loader_common.h"
+#include "../common/wasm_native.h"
+#include "../common/wasm_memory.h"
+#if WASM_ENABLE_GC != 0
+#include "../common/gc/gc_type.h"
+#include "../common/gc/gc_object.h"
+#endif
+#if WASM_ENABLE_DEBUG_INTERP != 0
+#include "../libraries/debug-engine/debug_engine.h"
+#endif
+#if WASM_ENABLE_FAST_JIT != 0
+#include "../fast-jit/jit_compiler.h"
+#include "../fast-jit/jit_codecache.h"
+#endif
+#if WASM_ENABLE_JIT != 0
+#include "../compilation/aot_llvm.h"
+#endif
+
+#ifndef TRACE_WASM_LOADER
+#define TRACE_WASM_LOADER 0
+#endif
+
+/* 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. */
+#define TEMPLATE_READ_VALUE(Type, p) \
+ (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
+
+#if WASM_ENABLE_MEMORY64 != 0
+static bool
+has_module_memory64(WASMModule *module)
+{
+ /* TODO: multi-memories for now assuming the memory idx type is consistent
+ * across multi-memories */
+ if (module->import_memory_count > 0)
+ return !!(module->import_memories[0].u.memory.mem_type.flags
+ & MEMORY64_FLAG);
+ else if (module->memory_count > 0)
+ return !!(module->memories[0].flags & MEMORY64_FLAG);
+
+ return false;
+}
+
+static bool
+is_table_64bit(WASMModule *module, uint32 table_idx)
+{
+ if (table_idx < module->import_table_count)
+ return !!(module->import_tables[table_idx].u.table.table_type.flags
+ & TABLE64_FLAG);
+ else
+ return !!(module->tables[table_idx].table_type.flags & TABLE64_FLAG);
+
+ return false;
+}
+#endif
+
+static void
+set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
+{
+ wasm_loader_set_error_buf(error_buf, error_buf_size, string, false);
+}
+
+#if WASM_ENABLE_MEMORY64 != 0
+static void
+set_error_buf_mem_offset_out_of_range(char *error_buf, uint32 error_buf_size)
+{
+ if (error_buf != NULL) {
+ snprintf(error_buf, error_buf_size, "offset out of range");
+ }
+}
+#endif
+
+static void
+set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
+{
+ va_list args;
+ char buf[128];
+
+ if (error_buf != NULL) {
+ va_start(args, format);
+ vsnprintf(buf, sizeof(buf), format, args);
+ va_end(args);
+ snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
+ }
+}
+
+static bool
+check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
+ char *error_buf, uint32 error_buf_size)
+{
+ if ((uintptr_t)buf + length < (uintptr_t)buf
+ || (uintptr_t)buf + length > (uintptr_t)buf_end) {
+ 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)
+{
+ if ((uintptr_t)buf + length < (uintptr_t)buf
+ || (uintptr_t)buf + length > (uintptr_t)buf_end) {
+ set_error_buf(error_buf, error_buf_size, "unexpected end");
+ return false;
+ }
+ return true;
+}
+
+#define CHECK_BUF(buf, buf_end, length) \
+ do { \
+ if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
+ goto fail; \
+ } \
+ } while (0)
+
+#define CHECK_BUF1(buf, buf_end, length) \
+ do { \
+ if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
+ goto fail; \
+ } \
+ } while (0)
+
+#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)
+#define skip_leb_mem_offset(p, p_end) skip_leb(p)
+#define skip_leb_memidx(p, p_end) skip_leb(p)
+#if WASM_ENABLE_MULTI_MEMORY == 0
+#define skip_leb_align(p, p_end) skip_leb(p)
+#else
+/* Skip the following memidx if applicable */
+#define skip_leb_align(p, p_end) \
+ do { \
+ if (*p++ & OPT_MEMIDX_FLAG) \
+ skip_leb_uint32(p, p_end); \
+ } while (0)
+#endif
+
+#define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
+#define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
+
+#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)
+
+#if WASM_ENABLE_MEMORY64 != 0
+#define read_leb_mem_offset(p, p_end, res) \
+ do { \
+ uint64 res64; \
+ if (!read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, \
+ &res64, error_buf, error_buf_size)) { \
+ set_error_buf_mem_offset_out_of_range(error_buf, error_buf_size); \
+ goto fail; \
+ } \
+ res = (mem_offset_t)res64; \
+ } while (0)
+#else
+#define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
+#endif
+
+#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)
+
+#define read_leb_memidx(p, p_end, res) read_leb_uint32(p, p_end, res)
+#if WASM_ENABLE_MULTI_MEMORY != 0
+#define check_memidx(module, memidx) \
+ do { \
+ if (memidx >= module->import_memory_count + module->memory_count) { \
+ set_error_buf_v(error_buf, error_buf_size, "unknown memory %d", \
+ memidx); \
+ goto fail; \
+ } \
+ } while (0)
+/* Bit 6(0x40) indicating the optional memidx, and reset bit 6 for
+ * alignment check */
+#define read_leb_memarg(p, p_end, res) \
+ do { \
+ read_leb_uint32(p, p_end, res); \
+ if (res & OPT_MEMIDX_FLAG) { \
+ res &= ~OPT_MEMIDX_FLAG; \
+ read_leb_uint32(p, p_end, memidx); /* memidx */ \
+ check_memidx(module, memidx); \
+ } \
+ } while (0)
+#else
+/* reserved byte 0x00 */
+#define check_memidx(module, memidx) \
+ do { \
+ (void)module; \
+ if (memidx != 0) { \
+ set_error_buf(error_buf, error_buf_size, "zero byte expected"); \
+ goto fail; \
+ } \
+ } while (0)
+#define read_leb_memarg(p, p_end, res) read_leb_uint32(p, p_end, res)
+#endif
+
+static char *
+type2str(uint8 type)
+{
+ char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
+#if WASM_ENABLE_GC != 0
+ char *type_str_ref[] = { "stringview_iter",
+ "stringview_wtf16",
+ "(ref null ht)",
+ "(ref ht)",
+ "", /* reserved */
+ "stringview_wtf8",
+ "stringref",
+ "", /* reserved */
+ "", /* reserved */
+ "arrayref",
+ "structref",
+ "i32ref",
+ "eqref",
+ "anyref",
+ "externref",
+ "funcref",
+ "nullref",
+ "nullexternref",
+ "nullfuncref" };
+#endif
+
+ if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
+ return type_str[type - VALUE_TYPE_V128];
+#if WASM_ENABLE_GC != 0
+ else if (wasm_is_type_reftype(type))
+ return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
+#endif
+ 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
+ /* the operand stack is in polymorphic state */
+ || type == VALUE_TYPE_ANY
+#if WASM_ENABLE_GC != 0
+ || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
+#elif WASM_ENABLE_REF_TYPES != 0
+ /* For reference types, we use uint32 index to represent
+ the funcref and externref */
+ || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
+#endif
+ )
+ return true;
+ return false;
+}
+
+static bool
+is_64bit_type(uint8 type)
+{
+ if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
+#if WASM_ENABLE_GC != 0
+ || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
+#endif
+ )
+ return true;
+ return false;
+}
+
+#if WASM_ENABLE_GC != 0
+static bool
+is_packed_type(uint8 type)
+{
+ return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
+}
+#endif
+
+static bool
+is_byte_a_type(uint8 type)
+{
+ return (is_valid_value_type_for_interpreter(type)
+ || (type == VALUE_TYPE_VOID))
+ ? true
+ : false;
+}
+
+#if WASM_ENABLE_SIMD != 0
+#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
+static V128
+read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
+{
+ 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 */
+
+static void *
+loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
+{
+ void *mem;
+
+ if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
+ set_error_buf(error_buf, error_buf_size, "allocate memory failed");
+ return NULL;
+ }
+
+ memset(mem, 0, (uint32)size);
+ return mem;
+}
+
+static void *
+memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
+ uint32 error_buf_size)
+{
+ uint8 *mem_new;
+ bh_assert(size_new > size_old);
+ if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
+ bh_memcpy_s(mem_new, size_new, mem_old, size_old);
+ memset(mem_new + size_old, 0, size_new - size_old);
+ wasm_runtime_free(mem_old);
+ }
+ return mem_new;
+}
+
+#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)
+
+#if WASM_ENABLE_GC != 0
+static bool
+check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
+ char *error_buf, uint32 error_buf_size)
+{
+ if (type_index >= type_count) {
+ set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
+ type_index);
+ return false;
+ }
+ return true;
+}
+
+static bool
+check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
+ uint32 error_buf_size)
+{
+ if (!check_type_index(module, module->type_count, type_index, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+ if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
+ set_error_buf(error_buf, error_buf_size, "unkown array type");
+ return false;
+ }
+
+ return true;
+}
+#endif
+
+static bool
+check_function_index(const WASMModule *module, uint32 function_index,
+ char *error_buf, uint32 error_buf_size)
+{
+ if (function_index
+ >= module->import_function_count + module->function_count) {
+ set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
+ function_index);
+ return false;
+ }
+ return true;
+}
+
+typedef struct InitValue {
+ uint8 type;
+ uint8 flag;
+#if WASM_ENABLE_GC != 0
+ uint8 gc_opcode;
+ WASMRefType ref_type;
+#endif
+ WASMValue value;
+} InitValue;
+
+typedef struct ConstExprContext {
+ uint32 sp;
+ uint32 size;
+ WASMModule *module;
+ InitValue *stack;
+ InitValue data[WASM_CONST_EXPR_STACK_SIZE];
+} ConstExprContext;
+
+static void
+init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
+{
+ ctx->sp = 0;
+ ctx->module = module;
+ ctx->stack = ctx->data;
+ ctx->size = WASM_CONST_EXPR_STACK_SIZE;
+}
+
+static bool
+push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
+#if WASM_ENABLE_GC != 0
+ WASMRefType *ref_type, uint8 gc_opcode,
+#endif
+ WASMValue *value, char *error_buf, uint32 error_buf_size)
+{
+ InitValue *cur_value;
+
+ if (ctx->sp >= ctx->size) {
+ if (ctx->stack != ctx->data) {
+ MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
+ (ctx->size + 4) * sizeof(InitValue));
+ }
+ else {
+ if (!(ctx->stack =
+ loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
+ error_buf, error_buf_size))) {
+ goto fail;
+ }
+ bh_memcpy_s(ctx->stack, (ctx->size + 4) * (uint32)sizeof(InitValue),
+ ctx->data, ctx->size * (uint32)sizeof(InitValue));
+ }
+ ctx->size += 4;
+ }
+
+ cur_value = &ctx->stack[ctx->sp++];
+ cur_value->type = type;
+ cur_value->flag = flag;
+ cur_value->value = *value;
+
+#if WASM_ENABLE_GC != 0
+ cur_value->gc_opcode = gc_opcode;
+ if (wasm_is_type_multi_byte_type(type)) {
+ bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
+ ref_type, wasm_reftype_struct_size(ref_type));
+ }
+#endif
+
+ return true;
+fail:
+ return false;
+}
+
+#if WASM_ENABLE_GC != 0
+static void
+destroy_init_expr_data_recursive(WASMModule *module, void *data)
+{
+ WASMStructNewInitValues *struct_init_values =
+ (WASMStructNewInitValues *)data;
+ WASMArrayNewInitValues *array_init_values = (WASMArrayNewInitValues *)data;
+ WASMType *wasm_type;
+ uint32 i;
+
+ if (!data)
+ return;
+
+ wasm_type = module->types[struct_init_values->type_idx];
+
+ /* The data can only be type of `WASMStructNewInitValues *`
+ or `WASMArrayNewInitValues *` */
+ bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
+ || wasm_type->type_flag == WASM_TYPE_ARRAY);
+
+ if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
+ WASMStructType *struct_type = (WASMStructType *)wasm_type;
+ WASMRefTypeMap *ref_type_map = struct_type->ref_type_maps;
+ WASMRefType *ref_type;
+ uint8 field_type;
+
+ for (i = 0; i < struct_init_values->count; i++) {
+ field_type = struct_type->fields[i].field_type;
+ if (wasm_is_type_multi_byte_type(field_type))
+ ref_type = ref_type_map->ref_type;
+ else
+ ref_type = NULL;
+ if (wasm_reftype_is_subtype_of(field_type, ref_type,
+ REF_TYPE_STRUCTREF, NULL,
+ module->types, module->type_count)
+ || wasm_reftype_is_subtype_of(
+ field_type, ref_type, REF_TYPE_ARRAYREF, NULL,
+ module->types, module->type_count)) {
+ destroy_init_expr_data_recursive(
+ module, struct_init_values->fields[i].data);
+ }
+ }
+ }
+ else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
+ WASMArrayType *array_type = (WASMArrayType *)wasm_type;
+ WASMRefType *elem_ref_type = array_type->elem_ref_type;
+ uint8 elem_type = array_type->elem_type;
+
+ for (i = 0; i < array_init_values->length; i++) {
+ if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
+ REF_TYPE_STRUCTREF, NULL,
+ module->types, module->type_count)
+ || wasm_reftype_is_subtype_of(
+ elem_type, elem_ref_type, REF_TYPE_ARRAYREF, NULL,
+ module->types, module->type_count)) {
+ destroy_init_expr_data_recursive(
+ module, array_init_values->elem_data[i].data);
+ }
+ }
+ }
+
+ wasm_runtime_free(data);
+}
+#endif
+
+static bool
+pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
+#if WASM_ENABLE_GC != 0
+ WASMRefType *ref_type, uint8 *p_gc_opcode,
+#endif
+ WASMValue *p_value, char *error_buf, uint32 error_buf_size)
+{
+ InitValue *cur_value;
+
+ if (ctx->sp == 0) {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch: const expr stack underflow");
+ return false;
+ }
+
+ cur_value = &ctx->stack[--ctx->sp];
+
+#if WASM_ENABLE_GC == 0
+ if (cur_value->type != type) {
+ set_error_buf(error_buf, error_buf_size, "type mismatch");
+ return false;
+ }
+#else
+ if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
+ ref_type, ctx->module->types,
+ ctx->module->type_count)) {
+ set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
+ "type mismatch: expect ", type2str(type),
+ " but got other");
+ goto fail;
+ }
+#endif
+
+ if (p_flag)
+ *p_flag = cur_value->flag;
+ if (p_value)
+ *p_value = cur_value->value;
+#if WASM_ENABLE_GC != 0
+ if (p_gc_opcode)
+ *p_gc_opcode = cur_value->gc_opcode;
+#endif
+
+ return true;
+
+#if WASM_ENABLE_GC != 0
+fail:
+ if ((cur_value->flag == WASM_OP_GC_PREFIX)
+ && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
+ || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
+ || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
+ destroy_init_expr_data_recursive(ctx->module, cur_value->value.data);
+ }
+ return false;
+#endif
+}
+
+static void
+destroy_const_expr_stack(ConstExprContext *ctx)
+{
+#if WASM_ENABLE_GC != 0
+ uint32 i;
+
+ for (i = 0; i < ctx->sp; i++) {
+ if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
+ && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
+ || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
+ || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
+ destroy_init_expr_data_recursive(ctx->module,
+ ctx->stack[i].value.data);
+ }
+ }
+#endif
+
+ if (ctx->stack != ctx->data) {
+ wasm_runtime_free(ctx->stack);
+ }
+}
+
+#if WASM_ENABLE_GC != 0
+static void
+destroy_init_expr(WASMModule *module, InitializerExpression *expr)
+{
+ if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
+ || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
+ || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
+ destroy_init_expr_data_recursive(module, expr->u.data);
+ }
+}
+#endif /* end of WASM_ENABLE_GC != 0 */
+
+static bool
+load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
+ InitializerExpression *init_expr, uint8 type, void *ref_type,
+ char *error_buf, uint32 error_buf_size)
+{
+ const uint8 *p = *p_buf, *p_end = buf_end;
+ uint8 flag, *p_float;
+ uint32 i;
+ ConstExprContext const_expr_ctx = { 0 };
+ WASMValue cur_value;
+#if WASM_ENABLE_GC != 0
+ uint32 opcode1, type_idx;
+ uint8 opcode;
+ WASMRefType cur_ref_type = { 0 };
+#endif
+
+ init_const_expr_stack(&const_expr_ctx, module);
+
+ CHECK_BUF(p, p_end, 1);
+ flag = read_uint8(p);
+
+ while (flag != WASM_OP_END) {
+ switch (flag) {
+ /* i32.const */
+ case INIT_EXPR_TYPE_I32_CONST:
+ read_leb_int32(p, p_end, cur_value.i32);
+
+ if (!push_const_expr_stack(
+ &const_expr_ctx, flag, VALUE_TYPE_I32,
+#if WASM_ENABLE_GC != 0
+ NULL, 0,
+#endif
+ &cur_value, error_buf, error_buf_size))
+ goto fail;
+ break;
+ /* i64.const */
+ case INIT_EXPR_TYPE_I64_CONST:
+ read_leb_int64(p, p_end, cur_value.i64);
+
+ if (!push_const_expr_stack(
+ &const_expr_ctx, flag, VALUE_TYPE_I64,
+#if WASM_ENABLE_GC != 0
+ NULL, 0,
+#endif
+ &cur_value, error_buf, error_buf_size))
+ goto fail;
+ break;
+ /* f32.const */
+ case INIT_EXPR_TYPE_F32_CONST:
+ CHECK_BUF(p, p_end, 4);
+ p_float = (uint8 *)&cur_value.f32;
+ for (i = 0; i < sizeof(float32); i++)
+ *p_float++ = *p++;
+
+ if (!push_const_expr_stack(
+ &const_expr_ctx, flag, VALUE_TYPE_F32,
+#if WASM_ENABLE_GC != 0
+ NULL, 0,
+#endif
+ &cur_value, error_buf, error_buf_size))
+ goto fail;
+ break;
+ /* f64.const */
+ case INIT_EXPR_TYPE_F64_CONST:
+ CHECK_BUF(p, p_end, 8);
+ p_float = (uint8 *)&cur_value.f64;
+ for (i = 0; i < sizeof(float64); i++)
+ *p_float++ = *p++;
+
+ if (!push_const_expr_stack(
+ &const_expr_ctx, flag, VALUE_TYPE_F64,
+#if WASM_ENABLE_GC != 0
+ NULL, 0,
+#endif
+ &cur_value, error_buf, error_buf_size))
+ goto fail;
+ break;
+#if WASM_ENABLE_SIMD != 0
+#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
+ /* v128.const */
+ case INIT_EXPR_TYPE_V128_CONST:
+ {
+ uint64 high, low;
+
+ CHECK_BUF(p, p_end, 1);
+ (void)read_uint8(p);
+
+ CHECK_BUF(p, p_end, 16);
+ wasm_runtime_read_v128(p, &high, &low);
+ p += 16;
+
+ cur_value.v128.i64x2[0] = high;
+ cur_value.v128.i64x2[1] = low;
+
+ if (!push_const_expr_stack(
+ &const_expr_ctx, flag, VALUE_TYPE_V128,
+#if WASM_ENABLE_GC != 0
+ NULL, 0,
+#endif
+ &cur_value, error_buf, error_buf_size))
+ goto fail;
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ /* If any init_expr is v128.const, mark SIMD used */
+ module->is_simd_used = true;
+#endif
+ break;
+ }
+#endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
+#endif /* end of WASM_ENABLE_SIMD */
+
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+ /* ref.func */
+ case INIT_EXPR_TYPE_FUNCREF_CONST:
+ {
+ uint32 func_idx;
+ read_leb_uint32(p, p_end, func_idx);
+ cur_value.ref_index = func_idx;
+ if (!check_function_index(module, func_idx, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+
+#if WASM_ENABLE_GC == 0
+ if (!push_const_expr_stack(&const_expr_ctx, flag,
+ VALUE_TYPE_FUNCREF, &cur_value,
+ error_buf, error_buf_size))
+ goto fail;
+#else
+ if (func_idx < module->import_function_count) {
+ type_idx =
+ module->import_functions[func_idx].u.function.type_idx;
+ }
+ else {
+ type_idx = module
+ ->functions[func_idx
+ - module->import_function_count]
+ ->type_idx;
+ }
+ wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
+ false, type_idx);
+ if (!push_const_expr_stack(&const_expr_ctx, flag,
+ cur_ref_type.ref_type, &cur_ref_type,
+ 0, &cur_value, error_buf,
+ error_buf_size))
+ goto fail;
+#endif
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ module->is_ref_types_used = true;
+#endif
+ break;
+ }
+
+ /* ref.null */
+ case INIT_EXPR_TYPE_REFNULL_CONST:
+ {
+ uint8 type1;
+
+ CHECK_BUF(p, p_end, 1);
+ type1 = read_uint8(p);
+
+#if WASM_ENABLE_GC == 0
+ cur_value.ref_index = NULL_REF;
+ if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
+ &cur_value, error_buf,
+ error_buf_size))
+ goto fail;
+#else
+ cur_value.gc_obj = NULL_REF;
+
+ if (!is_byte_a_type(type1)) {
+ p--;
+ read_leb_uint32(p, p_end, type_idx);
+ if (!check_type_index(module, module->type_count, type_idx,
+ error_buf, error_buf_size))
+ goto fail;
+
+ wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
+ true, type_idx);
+ if (!push_const_expr_stack(&const_expr_ctx, flag,
+ cur_ref_type.ref_type,
+ &cur_ref_type, 0, &cur_value,
+ error_buf, error_buf_size))
+ goto fail;
+ }
+ else {
+ if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
+ NULL, 0, &cur_value, error_buf,
+ error_buf_size))
+ goto fail;
+ }
+#endif
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ module->is_ref_types_used = true;
+#endif
+ break;
+ }
+#endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
+
+ /* get_global */
+ case INIT_EXPR_TYPE_GET_GLOBAL:
+ {
+ uint32 global_idx;
+ uint8 global_type;
+
+ read_leb_uint32(p, p_end, cur_value.global_index);
+ global_idx = cur_value.global_index;
+
+ /*
+ * 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.
+ *
+ * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
+ */
+ if (global_idx >= module->import_global_count
+ /* make spec test happy */
+#if WASM_ENABLE_GC != 0
+ + module->global_count
+#endif
+ ) {
+ set_error_buf_v(error_buf, error_buf_size,
+ "unknown global %u", global_idx);
+ goto fail;
+ }
+ if (
+ /* make spec test happy */
+#if WASM_ENABLE_GC != 0
+ global_idx < module->import_global_count &&
+#endif
+ module->import_globals[global_idx]
+ .u.global.type.is_mutable) {
+ set_error_buf_v(error_buf, error_buf_size,
+ "constant expression required");
+ goto fail;
+ }
+
+ if (global_idx < module->import_global_count) {
+ global_type = module->import_globals[global_idx]
+ .u.global.type.val_type;
+#if WASM_ENABLE_GC != 0
+ if (wasm_is_type_multi_byte_type(global_type)) {
+ WASMRefType *global_ref_type =
+ module->import_globals[global_idx]
+ .u.global.ref_type;
+ bh_memcpy_s(&cur_ref_type,
+ wasm_reftype_struct_size(global_ref_type),
+ global_ref_type,
+ wasm_reftype_struct_size(global_ref_type));
+ }
+#endif
+ }
+ else {
+ global_type =
+ module
+ ->globals[global_idx - module->import_global_count]
+ .type.val_type;
+#if WASM_ENABLE_GC != 0
+ if (wasm_is_type_multi_byte_type(global_type)) {
+ WASMRefType *global_ref_type =
+ module
+ ->globals[global_idx
+ - module->import_global_count]
+ .ref_type;
+ bh_memcpy_s(&cur_ref_type,
+ wasm_reftype_struct_size(global_ref_type),
+ global_ref_type,
+ wasm_reftype_struct_size(global_ref_type));
+ }
+#endif
+ }
+
+ if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
+#if WASM_ENABLE_GC != 0
+ &cur_ref_type, 0,
+#endif
+ &cur_value, error_buf,
+ error_buf_size))
+ goto fail;
+
+ break;
+ }
+
+#if WASM_ENABLE_GC != 0
+ /* struct.new and array.new */
+ case WASM_OP_GC_PREFIX:
+ {
+ read_leb_uint32(p, p_end, opcode1);
+
+ switch (opcode1) {
+ case WASM_OP_STRUCT_NEW:
+ {
+ WASMStructType *struct_type;
+ WASMStructNewInitValues *struct_init_values = NULL;
+ uint32 field_count;
+ read_leb_uint32(p, p_end, type_idx);
+
+ if (!check_type_index(module, module->type_count,
+ type_idx, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+
+ struct_type = (WASMStructType *)module->types[type_idx];
+ if (struct_type->base_type.type_flag
+ != WASM_TYPE_STRUCT) {
+ set_error_buf(error_buf, error_buf_size,
+ "unkown struct type");
+ goto fail;
+ }
+ field_count = struct_type->field_count;
+
+ if (!(struct_init_values = loader_malloc(
+ offsetof(WASMStructNewInitValues, fields)
+ + (uint64)field_count * sizeof(WASMValue),
+ error_buf, error_buf_size))) {
+ goto fail;
+ }
+ struct_init_values->type_idx = type_idx;
+ struct_init_values->count = field_count;
+
+ for (i = field_count; i > 0; i--) {
+ WASMRefType *field_ref_type = NULL;
+ uint32 field_idx = i - 1;
+ uint8 field_type =
+ struct_type->fields[field_idx].field_type;
+ if (wasm_is_type_multi_byte_type(field_type)) {
+ field_ref_type = wasm_reftype_map_find(
+ struct_type->ref_type_maps,
+ struct_type->ref_type_map_count, field_idx);
+ }
+
+ if (is_packed_type(field_type)) {
+ field_type = VALUE_TYPE_I32;
+ }
+
+ if (!pop_const_expr_stack(
+ &const_expr_ctx, NULL, field_type,
+ field_ref_type, NULL,
+ &struct_init_values->fields[field_idx],
+ error_buf, error_buf_size)) {
+ destroy_init_expr_data_recursive(
+ module, struct_init_values);
+ goto fail;
+ }
+ }
+
+ cur_value.data = struct_init_values;
+ wasm_set_refheaptype_typeidx(
+ &cur_ref_type.ref_ht_typeidx, false, type_idx);
+ if (!push_const_expr_stack(
+ &const_expr_ctx, flag, cur_ref_type.ref_type,
+ &cur_ref_type, (uint8)opcode1, &cur_value,
+ error_buf, error_buf_size)) {
+ destroy_init_expr_data_recursive(
+ module, struct_init_values);
+ goto fail;
+ }
+ break;
+ }
+ case WASM_OP_STRUCT_NEW_DEFAULT:
+ {
+ read_leb_uint32(p, p_end, cur_value.type_index);
+ type_idx = cur_value.type_index;
+
+ if (!check_type_index(module, module->type_count,
+ type_idx, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+ if (module->types[type_idx]->type_flag
+ != WASM_TYPE_STRUCT) {
+ set_error_buf(error_buf, error_buf_size,
+ "unkown struct type");
+ goto fail;
+ }
+
+ cur_value.type_index = type_idx;
+ cur_value.data = NULL;
+ wasm_set_refheaptype_typeidx(
+ &cur_ref_type.ref_ht_typeidx, false, type_idx);
+ if (!push_const_expr_stack(
+ &const_expr_ctx, flag, cur_ref_type.ref_type,
+ &cur_ref_type, (uint8)opcode1, &cur_value,
+ error_buf, error_buf_size)) {
+ goto fail;
+ }
+ break;
+ }
+ case WASM_OP_ARRAY_NEW:
+ case WASM_OP_ARRAY_NEW_DEFAULT:
+ case WASM_OP_ARRAY_NEW_FIXED:
+ {
+ WASMArrayNewInitValues *array_init_values = NULL;
+ WASMArrayType *array_type = NULL;
+ WASMRefType *elem_ref_type = NULL;
+ uint64 total_size;
+ uint8 elem_type;
+
+ read_leb_uint32(p, p_end, cur_value.type_index);
+ type_idx = cur_value.type_index;
+
+ if (!check_type_index(module, module->type_count,
+ type_idx, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+
+ array_type = (WASMArrayType *)module->types[type_idx];
+ if (array_type->base_type.type_flag
+ != WASM_TYPE_ARRAY) {
+ set_error_buf(error_buf, error_buf_size,
+ "unkown array type");
+ goto fail;
+ }
+
+ if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
+ elem_type = array_type->elem_type;
+ if (wasm_is_type_multi_byte_type(elem_type)) {
+ elem_ref_type = array_type->elem_ref_type;
+ }
+
+ if (is_packed_type(elem_type)) {
+ elem_type = VALUE_TYPE_I32;
+ }
+
+ if (opcode1 == WASM_OP_ARRAY_NEW) {
+ WASMValue len_val;
+
+ if (!(array_init_values = loader_malloc(
+ sizeof(WASMArrayNewInitValues),
+ error_buf, error_buf_size))) {
+ goto fail;
+ }
+ array_init_values->type_idx = type_idx;
+
+ if (!pop_const_expr_stack(
+ &const_expr_ctx, NULL, VALUE_TYPE_I32,
+ NULL, NULL, &len_val, error_buf,
+ error_buf_size)) {
+ destroy_init_expr_data_recursive(
+ module, array_init_values);
+ goto fail;
+ }
+ array_init_values->length = len_val.i32;
+
+ if (!pop_const_expr_stack(
+ &const_expr_ctx, NULL, elem_type,
+ elem_ref_type, NULL,
+ &array_init_values->elem_data[0],
+ error_buf, error_buf_size)) {
+ destroy_init_expr_data_recursive(
+ module, array_init_values);
+ goto fail;
+ }
+
+ cur_value.data = array_init_values;
+ }
+ else {
+ /* WASM_OP_ARRAY_NEW_FIXED */
+ uint32 len;
+ read_leb_uint32(p, p_end, len);
+
+ total_size =
+ (uint64)offsetof(WASMArrayNewInitValues,
+ elem_data)
+ + (uint64)sizeof(WASMValue) * len;
+ if (!(array_init_values =
+ loader_malloc(total_size, error_buf,
+ error_buf_size))) {
+ goto fail;
+ }
+
+ array_init_values->type_idx = type_idx;
+ array_init_values->length = len;
+
+ for (i = len; i > 0; i--) {
+ if (!pop_const_expr_stack(
+ &const_expr_ctx, NULL, elem_type,
+ elem_ref_type, NULL,
+ &array_init_values
+ ->elem_data[i - 1],
+ error_buf, error_buf_size)) {
+ destroy_init_expr_data_recursive(
+ module, array_init_values);
+ goto fail;
+ }
+ }
+
+ cur_value.data = array_init_values;
+ }
+ }
+ else {
+ /* WASM_OP_ARRAY_NEW_DEFAULT */
+ WASMValue len_val;
+ uint32 len;
+
+ /* POP(i32) */
+ if (!pop_const_expr_stack(&const_expr_ctx, NULL,
+ VALUE_TYPE_I32, NULL,
+ NULL, &len_val, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+ len = len_val.i32;
+
+ cur_value.array_new_default.type_index = type_idx;
+ cur_value.array_new_default.length = len;
+ }
+
+ wasm_set_refheaptype_typeidx(
+ &cur_ref_type.ref_ht_typeidx, false, type_idx);
+ if (!push_const_expr_stack(
+ &const_expr_ctx, flag, cur_ref_type.ref_type,
+ &cur_ref_type, (uint8)opcode1, &cur_value,
+ error_buf, error_buf_size)) {
+ if (array_init_values) {
+ destroy_init_expr_data_recursive(
+ module, array_init_values);
+ }
+ goto fail;
+ }
+ break;
+ }
+ case WASM_OP_ANY_CONVERT_EXTERN:
+ {
+ set_error_buf(error_buf, error_buf_size,
+ "unsupported constant expression of "
+ "extern.internalize");
+ goto fail;
+ }
+ case WASM_OP_EXTERN_CONVERT_ANY:
+ {
+ set_error_buf(error_buf, error_buf_size,
+ "unsupported constant expression of "
+ "extern.externalize");
+ goto fail;
+ }
+ case WASM_OP_REF_I31:
+ {
+ /* POP(i32) */
+ if (!pop_const_expr_stack(
+ &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
+ NULL, &cur_value, error_buf, error_buf_size)) {
+ goto fail;
+ }
+
+ wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
+ false, HEAP_TYPE_I31);
+ if (!push_const_expr_stack(
+ &const_expr_ctx, flag, cur_ref_type.ref_type,
+ &cur_ref_type, (uint8)opcode1, &cur_value,
+ error_buf, error_buf_size)) {
+ goto fail;
+ }
+ break;
+ }
+ default:
+ set_error_buf(
+ error_buf, error_buf_size,
+ "type mismatch or constant expression required");
+ goto fail;
+ }
+
+ break;
+ }
+#endif /* end of WASM_ENABLE_GC != 0 */
+ default:
+ {
+ set_error_buf(error_buf, error_buf_size,
+ "illegal opcode "
+ "or constant expression required "
+ "or type mismatch");
+ goto fail;
+ }
+ }
+
+ CHECK_BUF(p, p_end, 1);
+ flag = read_uint8(p);
+ }
+
+ /* There should be only one value left on the init value stack */
+ if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
+#if WASM_ENABLE_GC != 0
+ ref_type, &opcode,
+#endif
+ &cur_value, error_buf, error_buf_size)) {
+ goto fail;
+ }
+
+ if (const_expr_ctx.sp != 0) {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch: illegal constant opcode sequence");
+ goto fail;
+ }
+
+ init_expr->init_expr_type = flag;
+ init_expr->u = cur_value;
+
+#if WASM_ENABLE_GC != 0
+ if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
+ switch (opcode) {
+ case WASM_OP_STRUCT_NEW:
+ init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
+ break;
+ case WASM_OP_STRUCT_NEW_DEFAULT:
+ init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
+ break;
+ case WASM_OP_ARRAY_NEW:
+ init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
+ break;
+ case WASM_OP_ARRAY_NEW_DEFAULT:
+ init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
+ break;
+ case WASM_OP_ARRAY_NEW_FIXED:
+ init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
+ break;
+ case WASM_OP_REF_I31:
+ init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
+ break;
+ default:
+ bh_assert(0);
+ break;
+ }
+ }
+#endif /* end of WASM_ENABLE_GC != 0 */
+
+ *p_buf = p;
+ destroy_const_expr_stack(&const_expr_ctx);
+ return true;
+
+fail:
+ destroy_const_expr_stack(&const_expr_ctx);
+ return false;
+}
+
+static bool
+check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
+{
+ if (mutable >= 2) {
+ set_error_buf(error_buf, error_buf_size, "invalid mutability");
+ return false;
+ }
+ return true;
+}
+
+#if WASM_ENABLE_GC != 0
+static void
+destroy_func_type(WASMFuncType *type)
+{
+ /* Destroy the reference type hash set */
+ if (type->ref_type_maps)
+ wasm_runtime_free(type->ref_type_maps);
+
+#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
+ /* Free the type */
+ wasm_runtime_free(type);
+}
+
+static void
+destroy_struct_type(WASMStructType *type)
+{
+ if (type->ref_type_maps)
+ wasm_runtime_free(type->ref_type_maps);
+
+ wasm_runtime_free(type);
+}
+
+static void
+destroy_array_type(WASMArrayType *type)
+{
+ wasm_runtime_free(type);
+}
+
+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;
+ }
+
+ if (type->type_flag == WASM_TYPE_FUNC)
+ destroy_func_type((WASMFuncType *)type);
+ else if (type->type_flag == WASM_TYPE_STRUCT)
+ destroy_struct_type((WASMStructType *)type);
+ else if (type->type_flag == WASM_TYPE_ARRAY)
+ destroy_array_type((WASMArrayType *)type);
+ else {
+ bh_assert(0);
+ }
+}
+
+/* Resolve (ref null ht) or (ref ht) */
+static bool
+resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
+ WASMModule *module, uint32 type_count, bool nullable,
+ WASMRefType *ref_type, char *error_buf,
+ uint32 error_buf_size)
+{
+ const uint8 *p = *p_buf, *p_end = buf_end;
+
+ ref_type->ref_type =
+ nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
+ ref_type->ref_ht_common.nullable = nullable;
+ read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
+
+ if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
+ /* heap type is (type i), i : typeidx, >= 0 */
+ if (!check_type_index(module, type_count,
+ ref_type->ref_ht_typeidx.type_idx, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+ }
+ else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
+ /* heap type is func, extern, any, eq, i31 or data */
+ set_error_buf(error_buf, error_buf_size, "unknown heap type");
+ return false;
+ }
+
+ *p_buf = p;
+ return true;
+fail:
+ return false;
+}
+
+static bool
+resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
+ WASMModule *module, uint32 type_count,
+ bool *p_need_ref_type_map, WASMRefType *ref_type,
+ bool allow_packed_type, char *error_buf,
+ uint32 error_buf_size)
+{
+ const uint8 *p = *p_buf, *p_end = buf_end;
+ uint8 type;
+
+ memset(ref_type, 0, sizeof(WASMRefType));
+
+ CHECK_BUF(p, p_end, 1);
+ type = read_uint8(p);
+
+ if (wasm_is_reftype_htref_nullable(type)) {
+ /* (ref null ht) */
+ if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
+ ref_type, error_buf, error_buf_size))
+ return false;
+ if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
+ *p_need_ref_type_map = true;
+ else {
+ /* For (ref null func/extern/any/eq/i31/data), they are same as
+ funcref/externref/anyref/eqref/i31ref/dataref, we convert the
+ multi-byte type to one-byte type to reduce the footprint and
+ the complexity of type equal/subtype checking */
+ ref_type->ref_type =
+ (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
+ *p_need_ref_type_map = false;
+ }
+ }
+ else if (wasm_is_reftype_htref_non_nullable(type)) {
+ /* (ref ht) */
+ if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
+ ref_type, error_buf, error_buf_size))
+ return false;
+ *p_need_ref_type_map = true;
+#if WASM_ENABLE_STRINGREF != 0
+ /* covert (ref string) to stringref */
+ if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
+ ref_type->ref_type =
+ (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
+ *p_need_ref_type_map = false;
+ }
+#endif
+ }
+ else {
+ /* type which can be represented by one byte */
+ if (!is_valid_value_type_for_interpreter(type)
+ && !(allow_packed_type && is_packed_type(type))) {
+ set_error_buf(error_buf, error_buf_size, "type mismatch");
+ return false;
+ }
+ ref_type->ref_type = type;
+ *p_need_ref_type_map = false;
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ /* If any value's type is v128, mark the module as SIMD used */
+ if (type == VALUE_TYPE_V128)
+ module->is_simd_used = true;
+#endif
+ }
+
+ *p_buf = p;
+ return true;
+fail:
+ return false;
+}
+
+static WASMRefType *
+reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
+ char *error_buf, uint32 error_buf_size)
+{
+ WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
+
+ if (!ret) {
+ set_error_buf(error_buf, error_buf_size,
+ "insert ref type to hash set failed");
+ }
+ return ret;
+}
+
+static bool
+resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
+ uint32 type_count, uint32 type_idx, char *error_buf,
+ uint32 error_buf_size)
+{
+ const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
+ uint32 param_count, result_count, i, j = 0;
+ uint32 param_cell_num, ret_cell_num;
+ uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
+ uint64 total_size;
+ bool need_ref_type_map;
+ WASMRefType ref_type;
+ WASMFuncType *type = NULL;
+
+ /* Parse first time to resolve param count, result count and
+ ref type map count */
+ read_leb_uint32(p, p_end, param_count);
+ p_org = p;
+ for (i = 0; i < param_count; i++) {
+ if (!resolve_value_type(&p, p_end, module, type_count,
+ &need_ref_type_map, &ref_type, false, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+ if (need_ref_type_map)
+ ref_type_map_count++;
+ }
+
+ read_leb_uint32(p, p_end, result_count);
+ for (i = 0; i < result_count; i++) {
+ if (!resolve_value_type(&p, p_end, module, type_count,
+ &need_ref_type_map, &ref_type, false, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+ if (need_ref_type_map) {
+ ref_type_map_count++;
+ result_ref_type_map_count++;
+ }
+ }
+
+ LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
+ "ref type map count: %d",
+ type_idx, param_count, result_count, ref_type_map_count);
+
+ /* Parse second time to resolve param types, result types and
+ ref type map info */
+ p = p_org;
+
+ total_size = offsetof(WASMFuncType, types)
+ + sizeof(uint8) * (uint64)(param_count + result_count);
+ if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
+ return false;
+ }
+ if (ref_type_map_count > 0) {
+ total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
+ if (!(type->ref_type_maps =
+ loader_malloc(total_size, error_buf, error_buf_size))) {
+ goto fail;
+ }
+ }
+
+ type->base_type.type_flag = WASM_TYPE_FUNC;
+ type->param_count = param_count;
+ type->result_count = result_count;
+ type->ref_type_map_count = ref_type_map_count;
+ if (ref_type_map_count > 0) {
+ type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
+ - result_ref_type_map_count;
+ }
+
+ for (i = 0; i < param_count; i++) {
+ if (!resolve_value_type(&p, p_end, module, type_count,
+ &need_ref_type_map, &ref_type, false, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+ type->types[i] = ref_type.ref_type;
+ if (need_ref_type_map) {
+ type->ref_type_maps[j].index = i;
+ if (!(type->ref_type_maps[j++].ref_type =
+ reftype_set_insert(module->ref_type_set, &ref_type,
+ error_buf, error_buf_size))) {
+ goto fail;
+ }
+ }
+ }
+
+ read_leb_uint32(p, p_end, result_count);
+ for (i = 0; i < result_count; i++) {
+ if (!resolve_value_type(&p, p_end, module, type_count,
+ &need_ref_type_map, &ref_type, false, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+ type->types[param_count + i] = ref_type.ref_type;
+ if (need_ref_type_map) {
+ type->ref_type_maps[j].index = param_count + i;
+ if (!(type->ref_type_maps[j++].ref_type =
+ reftype_set_insert(module->ref_type_set, &ref_type,
+ error_buf, error_buf_size))) {
+ goto fail;
+ }
+ }
+ }
+
+ bh_assert(j == type->ref_type_map_count);
+#if TRACE_WASM_LOADER != 0
+ os_printf("type %d = ", type_idx);
+ wasm_dump_func_type(type);
+#endif
+
+ param_cell_num = wasm_get_cell_num(type->types, param_count);
+ ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
+ 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");
+ goto fail;
+ }
+ type->param_cell_num = (uint16)param_cell_num;
+ type->ret_cell_num = (uint16)ret_cell_num;
+
+#if WASM_ENABLE_QUICK_AOT_ENTRY != 0
+ type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
+#endif
+
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
+ if (type->types[i] == VALUE_TYPE_V128)
+ module->is_simd_used = true;
+ }
+#endif
+
+ *p_buf = p;
+
+ module->types[type_idx] = (WASMType *)type;
+ return true;
+
+fail:
+ if (type)
+ destroy_func_type(type);
+ return false;
+}
+
+static bool
+resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
+ WASMModule *module, uint32 type_count, uint32 type_idx,
+ char *error_buf, uint32 error_buf_size)
+{
+ const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
+ uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
+ uint32 i, j = 0, offset;
+ uint16 *reference_table;
+ uint64 total_size;
+ uint8 mutable;
+ bool need_ref_type_map;
+ WASMRefType ref_type;
+ WASMStructType *type = NULL;
+
+ /* Parse first time to resolve field count and ref type map count */
+ read_leb_uint32(p, p_end, field_count);
+ p_org = p;
+ for (i = 0; i < field_count; i++) {
+ if (!resolve_value_type(&p, p_end, module, type_count,
+ &need_ref_type_map, &ref_type, true, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+ if (need_ref_type_map)
+ ref_type_map_count++;
+
+ if (wasm_is_type_reftype(ref_type.ref_type))
+ ref_field_count++;
+
+ CHECK_BUF(p, p_end, 1);
+ mutable = read_uint8(p);
+ if (!check_mutability(mutable, error_buf, error_buf_size)) {
+ return false;
+ }
+ }
+
+ LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
+ type_idx, field_count, ref_type_map_count);
+
+ /* Parse second time to resolve field types and ref type map info */
+ p = p_org;
+
+ total_size = offsetof(WASMStructType, fields)
+ + sizeof(WASMStructFieldType) * (uint64)field_count
+ + sizeof(uint16) * (uint64)(ref_field_count + 1);
+ if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
+ return false;
+ }
+ if (ref_type_map_count > 0) {
+ total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
+ if (!(type->ref_type_maps =
+ loader_malloc(total_size, error_buf, error_buf_size))) {
+ goto fail;
+ }
+ }
+
+ type->reference_table = reference_table =
+ (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
+ + sizeof(WASMStructFieldType) * field_count);
+ *reference_table++ = ref_field_count;
+
+ type->base_type.type_flag = WASM_TYPE_STRUCT;
+ type->field_count = field_count;
+ type->ref_type_map_count = ref_type_map_count;
+
+ offset = (uint32)offsetof(WASMStructObject, field_data);
+ for (i = 0; i < field_count; i++) {
+ if (!resolve_value_type(&p, p_end, module, type_count,
+ &need_ref_type_map, &ref_type, true, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+ type->fields[i].field_type = ref_type.ref_type;
+ if (need_ref_type_map) {
+ type->ref_type_maps[j].index = i;
+ if (!(type->ref_type_maps[j++].ref_type =
+ reftype_set_insert(module->ref_type_set, &ref_type,
+ error_buf, error_buf_size))) {
+ goto fail;
+ }
+ }
+
+ CHECK_BUF(p, p_end, 1);
+ type->fields[i].field_flags = read_uint8(p);
+ type->fields[i].field_size =
+ (uint8)wasm_reftype_size(ref_type.ref_type);
+#if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
+ || defined(BUILD_TARGET_X86_32))
+ if (type->fields[i].field_size == 2)
+ offset = align_uint(offset, 2);
+ else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
+ offset = align_uint(offset, 4);
+#endif
+ type->fields[i].field_offset = offset;
+ if (wasm_is_type_reftype(ref_type.ref_type))
+ *reference_table++ = offset;
+ offset += type->fields[i].field_size;
+
+ LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
+ type->fields[i].field_flags, type->fields[i].field_type);
+ }
+ type->total_size = offset;
+
+ bh_assert(j == type->ref_type_map_count);
+#if TRACE_WASM_LOADER != 0
+ os_printf("type %d = ", type_idx);
+ wasm_dump_struct_type(type);
+#endif
+
+ *p_buf = p;
+
+ module->types[type_idx] = (WASMType *)type;
+ return true;
+
+fail:
+ if (type)
+ destroy_struct_type(type);
+ return false;
+}
+
+static bool
+resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
+ WASMModule *module, uint32 type_count, uint32 type_idx,
+ char *error_buf, uint32 error_buf_size)
+{
+ const uint8 *p = *p_buf, *p_end = buf_end;
+ uint8 mutable;
+ bool need_ref_type_map;
+ WASMRefType ref_type;
+ WASMArrayType *type = NULL;
+
+ if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
+ &ref_type, true, error_buf, error_buf_size)) {
+ return false;
+ }
+
+ CHECK_BUF(p, p_end, 1);
+ mutable = read_uint8(p);
+ if (!check_mutability(mutable, error_buf, error_buf_size)) {
+ return false;
+ }
+
+ LOG_VERBOSE("type %u: array", type_idx);
+
+ if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
+ error_buf_size))) {
+ return false;
+ }
+
+ type->base_type.type_flag = WASM_TYPE_ARRAY;
+ type->elem_flags = mutable;
+ type->elem_type = ref_type.ref_type;
+ if (need_ref_type_map) {
+ if (!(type->elem_ref_type =
+ reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
+ error_buf_size))) {
+ goto fail;
+ }
+ }
+
+#if TRACE_WASM_LOADER != 0
+ os_printf("type %d = ", type_idx);
+ wasm_dump_array_type(type);
+#endif
+
+ *p_buf = p;
+
+ module->types[type_idx] = (WASMType *)type;
+ return true;
+
+fail:
+ if (type)
+ destroy_array_type(type);
+ return false;
+}
+
+static bool
+init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
+ int32 heap_type, char *error_buf, uint32 error_buf_size)
+{
+ if (heap_type >= 0) {
+ if (!check_type_index(module, module->type_count, heap_type, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+ wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
+ heap_type);
+ }
+ else {
+ if (!wasm_is_valid_heap_type(heap_type)) {
+ set_error_buf(error_buf, error_buf_size, "unknown type");
+ return false;
+ }
+ wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
+ heap_type);
+ if (nullable) {
+ /* For (ref null func/extern/any/eq/i31/data),
+ they are same as
+ funcref/externref/anyref/eqref/i31ref/dataref,
+ we convert the multi-byte type to one-byte
+ type to reduce the footprint and the
+ complexity of type equal/subtype checking */
+ ref_type->ref_type =
+ (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
+ }
+ }
+ return true;
+}
+
+static void
+calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
+ WASMRefType *ref_type2)
+{
+ /**
+ * The difference rt1 ∖ rt2 between two reference types is defined as
+ * follows:
+ * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
+ * (ref ht2) = (ref null?1 ht1)
+ */
+ if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
+ bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
+ ref_type1, wasm_reftype_struct_size(ref_type1));
+ }
+ else {
+ ref_type_diff->ref_type = ref_type1->ref_type;
+ }
+
+ if (ref_type2->ref_ht_common.nullable) {
+ if (wasm_is_type_reftype(ref_type_diff->ref_type)
+ && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
+ wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
+ (int32)ref_type_diff->ref_type - 0x80);
+ }
+ else {
+ ref_type_diff->ref_ht_typeidx.nullable = false;
+ }
+ }
+}
+#else /* else of WASM_ENABLE_GC != 0 */
+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;
+ }
+
+#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
+
+ wasm_runtime_free(type);
+}
+#endif /* end of WASM_ENABLE_GC != 0 */
+
+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;
+ uint32 type_count, i;
+ uint64 total_size;
+ uint8 flag;
+#if WASM_ENABLE_GC != 0
+ uint32 processed_type_count = 0;
+#endif
+
+ read_leb_uint32(p, p_end, type_count);
+
+ if (type_count) {
+ module->type_count = type_count;
+ total_size = sizeof(WASMType *) * (uint64)type_count;
+ if (!(module->types =
+ loader_malloc(total_size, error_buf, error_buf_size))) {
+ return false;
+ }
+
+#if WASM_ENABLE_GC == 0
+ for (i = 0; i < type_count; i++) {
+ WASMFuncType *type;
+ const uint8 *p_org;
+ uint32 param_count, result_count, j;
+ uint32 param_cell_num, ret_cell_num;
+
+ CHECK_BUF(p, p_end, 1);
+ flag = read_uint8(p);
+ if (flag != 0x60) {
+ set_error_buf(error_buf, error_buf_size, "invalid type flag");
+ 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);
+ CHECK_BUF(p, p_end, result_count);
+ p = p_org;
+
+ if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
+ set_error_buf(error_buf, error_buf_size,
+ "param count or result count too large");
+ return false;
+ }
+
+ total_size = offsetof(WASMFuncType, types)
+ + sizeof(uint8) * (uint64)(param_count + result_count);
+ if (!(type = module->types[i] =
+ loader_malloc(total_size, error_buf, error_buf_size))) {
+ return false;
+ }
+
+ /* Resolve param types and result types */
+ type->ref_count = 1;
+ type->param_count = (uint16)param_count;
+ type->result_count = (uint16)result_count;
+ 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);
+ }
+ for (j = 0; j < param_count + result_count; j++) {
+ if (!is_valid_value_type_for_interpreter(type->types[j])) {
+ set_error_buf(error_buf, error_buf_size,
+ "unknown value type");
+ return false;
+ }
+ }
+
+ param_cell_num = wasm_get_cell_num(type->types, param_count);
+ ret_cell_num =
+ wasm_get_cell_num(type->types + param_count, result_count);
+ 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;
+
+#if WASM_ENABLE_QUICK_AOT_ENTRY != 0
+ type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
+#endif
+
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ for (j = 0; j < type->param_count + type->result_count; j++) {
+ if (type->types[j] == VALUE_TYPE_V128)
+ module->is_simd_used = true;
+ else if (type->types[j] == VALUE_TYPE_FUNCREF
+ || type->types[j] == VALUE_TYPE_EXTERNREF)
+ module->is_ref_types_used = true;
+ }
+#endif
+
+ /* 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], module->types, i)) {
+ 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;
+ }
+ }
+ }
+#else /* else of WASM_ENABLE_GC == 0 */
+ for (i = 0; i < type_count; i++) {
+ uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
+ uint32 rec_count = 1, j;
+ bool is_sub_final = true;
+
+ CHECK_BUF(p, p_end, 1);
+ flag = read_uint8(p);
+
+ if (flag == DEFINED_TYPE_REC) {
+ read_leb_uint32(p, p_end, rec_count);
+
+ if (rec_count > 1) {
+ uint64 new_total_size;
+
+ /* integer overflow */
+ if (rec_count - 1 > UINT32_MAX - module->type_count) {
+ set_error_buf(error_buf, error_buf_size,
+ "recursive type count too large");
+ return false;
+ }
+ module->type_count += rec_count - 1;
+ new_total_size =
+ sizeof(WASMFuncType *) * (uint64)module->type_count;
+ if (new_total_size > UINT32_MAX) {
+ set_error_buf(error_buf, error_buf_size,
+ "allocate memory failed");
+ return false;
+ }
+ MEM_REALLOC(module->types, (uint32)total_size,
+ (uint32)new_total_size);
+ total_size = new_total_size;
+ }
+
+ LOG_VERBOSE("Processing rec group [%d-%d]",
+ processed_type_count,
+ processed_type_count + rec_count - 1);
+ }
+ else {
+ p--;
+ }
+
+ for (j = 0; j < rec_count; j++) {
+ WASMType *cur_type = NULL;
+
+ CHECK_BUF(p, p_end, 1);
+ flag = read_uint8(p);
+
+ parent_type_idx = -1;
+
+ if (flag == DEFINED_TYPE_SUB
+ || flag == DEFINED_TYPE_SUB_FINAL) {
+ read_leb_uint32(p, p_end, super_type_count);
+ if (super_type_count > 1) {
+ set_error_buf(error_buf, error_buf_size,
+ "super type count too large");
+ return false;
+ }
+
+ if (super_type_count > 0) {
+ read_leb_uint32(p, p_end, parent_type_idx);
+ if (parent_type_idx >= processed_type_count + j) {
+ set_error_buf_v(error_buf, error_buf_size,
+ "unknown type %d", parent_type_idx);
+ return false;
+ }
+ if (module->types[parent_type_idx]->is_sub_final) {
+ set_error_buf(error_buf, error_buf_size,
+ "sub type can not inherit from "
+ "a final super type");
+ return false;
+ }
+ }
+
+ if (flag == DEFINED_TYPE_SUB)
+ is_sub_final = false;
+
+ CHECK_BUF(p, p_end, 1);
+ flag = read_uint8(p);
+ }
+
+ if (flag == DEFINED_TYPE_FUNC) {
+ if (!resolve_func_type(&p, buf_end, module,
+ processed_type_count + rec_count,
+ processed_type_count + j, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+ }
+ else if (flag == DEFINED_TYPE_STRUCT) {
+ if (!resolve_struct_type(&p, buf_end, module,
+ processed_type_count + rec_count,
+ processed_type_count + j,
+ error_buf, error_buf_size)) {
+ return false;
+ }
+ }
+ else if (flag == DEFINED_TYPE_ARRAY) {
+ if (!resolve_array_type(&p, buf_end, module,
+ processed_type_count + rec_count,
+ processed_type_count + j, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+ }
+ else {
+ set_error_buf(error_buf, error_buf_size,
+ "invalid type flag");
+ return false;
+ }
+
+ cur_type = module->types[processed_type_count + j];
+
+ cur_type->ref_count = 1;
+ cur_type->parent_type_idx = parent_type_idx;
+ cur_type->is_sub_final = is_sub_final;
+
+ cur_type->rec_count = rec_count;
+ cur_type->rec_idx = j;
+ cur_type->rec_begin_type_idx = processed_type_count;
+ }
+
+ /* resolve subtyping relationship in current rec group */
+ for (j = 0; j < rec_count; j++) {
+ WASMType *cur_type = module->types[processed_type_count + j];
+
+ if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
+ WASMType *parent_type =
+ module->types[cur_type->parent_type_idx];
+ cur_type->parent_type = parent_type;
+ cur_type->root_type = parent_type->root_type;
+ if (parent_type->inherit_depth == UINT16_MAX) {
+ set_error_buf(error_buf, error_buf_size,
+ "parent type's inherit depth too large");
+ return false;
+ }
+ cur_type->inherit_depth = parent_type->inherit_depth + 1;
+ }
+ else {
+ cur_type->parent_type = NULL;
+ cur_type->root_type = cur_type;
+ cur_type->inherit_depth = 0;
+ }
+ }
+
+ for (j = 0; j < rec_count; j++) {
+ WASMType *cur_type = module->types[processed_type_count + j];
+
+ if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
+ WASMType *parent_type =
+ module->types[cur_type->parent_type_idx];
+ if (!wasm_type_is_subtype_of(cur_type, parent_type,
+ module->types,
+ module->type_count)) {
+ set_error_buf(error_buf, error_buf_size,
+ "sub type does not match super type");
+ return false;
+ }
+ }
+ }
+
+ /* If there is already an equivalence type or a group of equivalence
+ recursive types created, use it or them instead */
+ for (j = 0; j < processed_type_count;) {
+ WASMType *src_type = module->types[j];
+ WASMType *cur_type = module->types[processed_type_count];
+ uint32 k, src_rec_count;
+
+ src_rec_count = src_type->rec_count;
+ if (src_rec_count != rec_count) {
+ /* no type equivalence */
+ j += src_rec_count;
+ continue;
+ }
+
+ for (k = 0; k < rec_count; k++) {
+ src_type = module->types[j + k];
+ cur_type = module->types[processed_type_count + k];
+ if (!wasm_type_equal(src_type, cur_type, module->types,
+ module->type_count)) {
+ break;
+ }
+ }
+ if (k < rec_count) {
+ /* no type equivalence */
+ j += src_rec_count;
+ continue;
+ }
+
+ /* type equivalence */
+ for (k = 0; k < rec_count; k++) {
+ if (module->types[j + k]->ref_count == UINT16_MAX) {
+ set_error_buf(error_buf, error_buf_size,
+ "wasm type's ref count too large");
+ return false;
+ }
+ destroy_wasm_type(module->types[processed_type_count + k]);
+ module->types[processed_type_count + k] =
+ module->types[j + k];
+ module->types[j + k]->ref_count++;
+ }
+ break;
+ }
+
+ if (rec_count > 1) {
+ LOG_VERBOSE("Finished processing rec group [%d-%d]",
+ processed_type_count,
+ processed_type_count + rec_count - 1);
+ }
+
+ processed_type_count += rec_count;
+ }
+
+ if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
+ * module->type_count,
+ error_buf, error_buf_size))) {
+ return false;
+ }
+#endif /* end of WASM_ENABLE_GC == 0 */
+ }
+
+ if (p != p_end) {
+ set_error_buf(error_buf, error_buf_size, "section size mismatch");
+ return false;
+ }
+
+ LOG_VERBOSE("Load type section success.\n");
+ return true;
+fail:
+ return false;
+}
+
+static void
+adjust_table_max_size(bool is_table64, uint32 init_size, uint32 max_size_flag,
+ uint32 *max_size)
+{
+ uint32 default_max_size;
+
+ /* TODO: current still use UINT32_MAX as upper limit for table size to keep
+ * ABI unchanged */
+ (void)is_table64;
+ if (UINT32_MAX / 2 > init_size)
+ default_max_size = init_size * 2;
+ else
+ default_max_size = UINT32_MAX;
+
+ if (default_max_size < WASM_TABLE_MAX_SIZE)
+ default_max_size = WASM_TABLE_MAX_SIZE;
+
+ if (max_size_flag) {
+ /* module defines the table limitation */
+ bh_assert(init_size <= *max_size);
+
+ if (init_size < *max_size) {
+ *max_size =
+ *max_size < default_max_size ? *max_size : default_max_size;
+ }
+ }
+ else {
+ /* partial defined table limitation, gives a default value */
+ *max_size = default_max_size;
+ }
+}
+
+#if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
+/**
+ * Find export item of a module with export info:
+ * module name, field name and export kind
+ */
+static WASMExport *
+wasm_loader_find_export(const WASMModule *module, const char *module_name,
+ const char *field_name, uint8 export_kind,
+ char *error_buf, uint32 error_buf_size)
+{
+ WASMExport *export =
+ loader_find_export((WASMModuleCommon *)module, module_name, field_name,
+ export_kind, error_buf, error_buf_size);
+ return export;
+}
+#endif
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+static WASMTable *
+wasm_loader_resolve_table(const char *module_name, const char *table_name,
+ uint32 init_size, uint32 max_size, char *error_buf,
+ uint32 error_buf_size)
+{
+ WASMModuleCommon *module_reg;
+ WASMTable *table = NULL;
+ WASMExport *export = NULL;
+ WASMModule *module = NULL;
+
+ module_reg = wasm_runtime_find_module_registered(module_name);
+ if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
+ LOG_DEBUG("can not find a module named %s for table", module_name);
+ set_error_buf(error_buf, error_buf_size, "unknown import");
+ return NULL;
+ }
+
+ module = (WASMModule *)module_reg;
+ export =
+ wasm_loader_find_export(module, module_name, table_name,
+ EXPORT_KIND_TABLE, error_buf, error_buf_size);
+ if (!export) {
+ return NULL;
+ }
+
+ /* resolve table and check the init/max size */
+ if (export->index < module->import_table_count) {
+ table =
+ module->import_tables[export->index].u.table.import_table_linked;
+ }
+ else {
+ table = &(module->tables[export->index - module->import_table_count]);
+ }
+ if (table->table_type.init_size < init_size
+ || table->table_type.max_size > max_size) {
+ LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
+ module_name, table_name, table->table_type.init_size,
+ table->table_type.max_size, init_size, max_size);
+ set_error_buf(error_buf, error_buf_size, "incompatible import type");
+ 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);
+ if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
+ LOG_DEBUG("can not find a module named %s for memory", module_name);
+ set_error_buf(error_buf, error_buf_size, "unknown import");
+ return NULL;
+ }
+
+ module = (WASMModule *)module_reg;
+ export =
+ wasm_loader_find_export(module, module_name, memory_name,
+ EXPORT_KIND_MEMORY, error_buf, error_buf_size);
+ if (!export) {
+ return NULL;
+ }
+
+ /* resolve memory and check the init/max page count */
+ if (export->index < module->import_memory_count) {
+ memory = module->import_memories[export->index]
+ .u.memory.import_memory_linked;
+ }
+ else {
+ memory =
+ &(module->memories[export->index - module->import_memory_count]);
+ }
+ if (memory->init_page_count < init_page_count
+ || memory->max_page_count > max_page_count) {
+ 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);
+ set_error_buf(error_buf, error_buf_size, "incompatible import type");
+ return NULL;
+ }
+ return memory;
+}
+
+static WASMGlobal *
+wasm_loader_resolve_global(const char *module_name, const char *global_name,
+ uint8 type, bool is_mutable, char *error_buf,
+ uint32 error_buf_size)
+{
+ WASMModuleCommon *module_reg;
+ WASMGlobal *global = NULL;
+ WASMExport *export = NULL;
+ WASMModule *module = NULL;
+
+ module_reg = wasm_runtime_find_module_registered(module_name);
+ if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
+ LOG_DEBUG("can not find a module named %s for global", module_name);
+ set_error_buf(error_buf, error_buf_size, "unknown import");
+ return NULL;
+ }
+
+ module = (WASMModule *)module_reg;
+ export =
+ wasm_loader_find_export(module, module_name, global_name,
+ EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
+ if (!export) {
+ return NULL;
+ }
+
+ /* resolve and check the global */
+ if (export->index < module->import_global_count) {
+ global =
+ module->import_globals[export->index].u.global.import_global_linked;
+ }
+ else {
+ global =
+ &(module->globals[export->index - module->import_global_count]);
+ }
+ if (global->type.val_type != type
+ || global->type.is_mutable != is_mutable) {
+ LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
+ module_name, global_name, global->type.val_type,
+ global->type.is_mutable, type, is_mutable);
+ set_error_buf(error_buf, error_buf_size, "incompatible import type");
+ return NULL;
+ }
+ return global;
+}
+
+#if WASM_ENABLE_TAGS != 0
+static WASMTag *
+wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
+ const WASMType *expected_tag_type,
+ uint32 *linked_tag_index, char *error_buf,
+ uint32 error_buf_size)
+{
+ WASMModuleCommon *module_reg;
+ WASMTag *tag = NULL;
+ WASMExport *export = NULL;
+ WASMModule *module = NULL;
+
+ module_reg = wasm_runtime_find_module_registered(module_name);
+ if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
+ LOG_DEBUG("can not find a module named %s for tag %s", module_name,
+ tag_name);
+ set_error_buf(error_buf, error_buf_size, "unknown import");
+ return NULL;
+ }
+
+ module = (WASMModule *)module_reg;
+ export =
+ wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
+ error_buf, error_buf_size);
+ if (!export) {
+ return NULL;
+ }
+
+ /* resolve tag type and tag */
+ if (export->index < module->import_tag_count) {
+ /* importing an imported tag from the submodule */
+ tag = module->import_tags[export->index].u.tag.import_tag_linked;
+ }
+ else {
+ /* importing an section tag from the submodule */
+ tag = module->tags[export->index - module->import_tag_count];
+ }
+
+ /* check function type */
+ if (!wasm_type_equal(expected_tag_type, tag->tag_type, module->types,
+ module->type_count)) {
+ LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
+ set_error_buf(error_buf, error_buf_size, "incompatible import type");
+ return NULL;
+ }
+
+ if (linked_tag_index != NULL) {
+ *linked_tag_index = export->index;
+ }
+
+ return tag;
+}
+#endif /* end of WASM_ENABLE_TAGS != 0 */
+#endif /* end of WASM_ENABLE_MULTI_MODULE */
+
+static bool
+load_function_import(const uint8 **p_buf, const uint8 *buf_end,
+ const WASMModule *parent_module,
+ const char *sub_module_name, const char *function_name,
+ WASMFunctionImport *function, bool no_resolve,
+ char *error_buf, uint32 error_buf_size)
+{
+ const uint8 *p = *p_buf, *p_end = buf_end;
+ uint32 declare_type_index = 0;
+
+ read_leb_uint32(p, p_end, declare_type_index);
+ *p_buf = p;
+
+ if (declare_type_index >= parent_module->type_count) {
+ set_error_buf(error_buf, error_buf_size, "unknown type");
+ return false;
+ }
+
+#if WASM_ENABLE_GC != 0
+ function->type_idx = declare_type_index;
+#endif
+
+#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
+ declare_type_index = wasm_get_smallest_type_idx(
+ parent_module->types, parent_module->type_count, declare_type_index);
+#endif
+
+ function->func_type =
+ (WASMFuncType *)parent_module->types[declare_type_index];
+
+ function->module_name = (char *)sub_module_name;
+ function->field_name = (char *)function_name;
+ function->attachment = NULL;
+ function->signature = NULL;
+ function->call_conv_raw = false;
+
+ /* lookup registered native symbols first */
+ if (!no_resolve) {
+ wasm_resolve_import_func(parent_module, function);
+ }
+ return true;
+fail:
+ return false;
+}
+
+static bool
+check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
+ uint32 error_buf_size)
+{
+ 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;
+}
+
+static bool
+load_table_import(const uint8 **p_buf, const uint8 *buf_end,
+ WASMModule *parent_module, const char *sub_module_name,
+ const char *table_name, WASMTableImport *table,
+ char *error_buf, uint32 error_buf_size)
+{
+ const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
+ uint32 declare_elem_type = 0, table_flag = 0, declare_init_size = 0,
+ declare_max_size = 0;
+#if WASM_ENABLE_MULTI_MODULE != 0
+ WASMModule *sub_module = NULL;
+ WASMTable *linked_table = NULL;
+#endif
+#if WASM_ENABLE_GC != 0
+ WASMRefType ref_type;
+ bool need_ref_type_map;
+#endif
+ bool is_table64 = false;
+
+#if WASM_ENABLE_GC == 0
+ CHECK_BUF(p, p_end, 1);
+ /* 0x70 or 0x6F */
+ declare_elem_type = read_uint8(p);
+ if (VALUE_TYPE_FUNCREF != declare_elem_type
+#if WASM_ENABLE_REF_TYPES != 0
+ && VALUE_TYPE_EXTERNREF != declare_elem_type
+#endif
+ ) {
+ set_error_buf(error_buf, error_buf_size, "incompatible import type");
+ return false;
+ }
+#else /* else of WASM_ENABLE_GC == 0 */
+ if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
+ &need_ref_type_map, &ref_type, false, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+ if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
+ set_error_buf(error_buf, error_buf_size, "type mismatch");
+ return false;
+ }
+ declare_elem_type = ref_type.ref_type;
+ if (need_ref_type_map) {
+ if (!(table->table_type.elem_ref_type =
+ reftype_set_insert(parent_module->ref_type_set, &ref_type,
+ error_buf, error_buf_size))) {
+ return false;
+ }
+ }
+#if TRACE_WASM_LOADER != 0
+ os_printf("import table type: ");
+ wasm_dump_value_type(declare_elem_type, table->table_type.elem_ref_type);
+ os_printf("\n");
+#endif
+#endif /* end of WASM_ENABLE_GC == 0 */
+
+ p_org = p;
+ read_leb_uint32(p, p_end, table_flag);
+ is_table64 = table_flag & TABLE64_FLAG;
+ if (p - p_org > 1) {
+ LOG_VERBOSE("integer representation too long(import table)");
+ set_error_buf(error_buf, error_buf_size, "invalid limits flags");
+ return false;
+ }
+
+ if (!wasm_table_check_flags(table_flag, error_buf, error_buf_size, false)) {
+ return false;
+ }
+
+ read_leb_uint32(p, p_end, declare_init_size);
+ if (table_flag & MAX_TABLE_SIZE_FLAG) {
+ read_leb_uint32(p, p_end, declare_max_size);
+ if (!check_table_max_size(declare_init_size, declare_max_size,
+ error_buf, error_buf_size))
+ return false;
+ }
+
+ adjust_table_max_size(is_table64, declare_init_size,
+ table_flag & MAX_TABLE_SIZE_FLAG, &declare_max_size);
+
+ *p_buf = p;
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+ if (!wasm_runtime_is_built_in_module(sub_module_name)) {
+ sub_module = (WASMModule *)wasm_runtime_load_depended_module(
+ (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
+ error_buf_size);
+ if (sub_module) {
+ linked_table = wasm_loader_resolve_table(
+ sub_module_name, table_name, declare_init_size,
+ declare_max_size, error_buf, error_buf_size);
+ if (linked_table) {
+ /* reset with linked table limit */
+ declare_elem_type = linked_table->table_type.elem_type;
+ declare_init_size = linked_table->table_type.init_size;
+ declare_max_size = linked_table->table_type.max_size;
+ table_flag = linked_table->table_type.flags;
+ table->import_table_linked = linked_table;
+ table->import_module = sub_module;
+ }
+ }
+ }
+#endif /* WASM_ENABLE_MULTI_MODULE != 0 */
+
+ /* (table (export "table") 10 20 funcref) */
+ /* (table (export "table64") 10 20 funcref) */
+ /* we need this section working in wamrc */
+ if (!strcmp("spectest", sub_module_name)) {
+ const uint32 spectest_table_init_size = 10;
+ const uint32 spectest_table_max_size = 20;
+
+ if (strcmp("table", table_name)
+#if WASM_ENABLE_MEMORY64 != 0
+ && strcmp("table64", table_name)
+#endif
+ ) {
+ 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->table_type.elem_type = declare_elem_type;
+ table->table_type.init_size = declare_init_size;
+ table->table_type.flags = table_flag;
+ table->table_type.max_size = declare_max_size;
+
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
+ parent_module->is_ref_types_used = true;
+#endif
+ (void)parent_module;
+ return true;
+fail:
+ return false;
+}
+
+static bool
+check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
+ uint32 error_buf_size)
+{
+ uint32 default_max_size =
+ is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
+
+ if (!is_memory64 && init_size > default_max_size) {
+ set_error_buf(error_buf, error_buf_size,
+ "memory size must be at most 65536 pages (4GiB)");
+ return false;
+ }
+#if WASM_ENABLE_MEMORY64 != 0
+ else if (is_memory64 && init_size > default_max_size) {
+ set_error_buf(
+ error_buf, error_buf_size,
+ "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
+ return false;
+ }
+#endif
+ return true;
+}
+
+static bool
+check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
+ char *error_buf, uint32 error_buf_size)
+{
+ uint32 default_max_size =
+ is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
+
+ if (max_size < init_size) {
+ set_error_buf(error_buf, error_buf_size,
+ "size minimum must not be greater than maximum");
+ return false;
+ }
+
+ if (!is_memory64 && max_size > default_max_size) {
+ set_error_buf(error_buf, error_buf_size,
+ "memory size must be at most 65536 pages (4GiB)");
+ return false;
+ }
+#if WASM_ENABLE_MEMORY64 != 0
+ else if (is_memory64 && max_size > default_max_size) {
+ set_error_buf(
+ error_buf, error_buf_size,
+ "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
+ return false;
+ }
+#endif
+
+ return true;
+}
+
+static bool
+load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
+ WASMModule *parent_module, const char *sub_module_name,
+ const char *memory_name, WASMMemoryImport *memory,
+ char *error_buf, uint32 error_buf_size)
+{
+ const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
+#if WASM_ENABLE_APP_FRAMEWORK != 0
+ uint32 pool_size = wasm_runtime_memory_pool_size();
+ uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
+ / DEFAULT_NUM_BYTES_PER_PAGE;
+#else
+ uint32 max_page_count;
+#endif /* WASM_ENABLE_APP_FRAMEWORK */
+ uint32 mem_flag = 0;
+ bool is_memory64 = false;
+ uint32 declare_init_page_count = 0;
+ uint32 declare_max_page_count = 0;
+#if WASM_ENABLE_MULTI_MODULE != 0
+ WASMModule *sub_module = NULL;
+ WASMMemory *linked_memory = NULL;
+#endif
+
+ p_org = p;
+ read_leb_uint32(p, p_end, mem_flag);
+ is_memory64 = mem_flag & MEMORY64_FLAG;
+ if (p - p_org > 1) {
+ LOG_VERBOSE("integer representation too long(import memory)");
+ set_error_buf(error_buf, error_buf_size, "invalid limits flags");
+ return false;
+ }
+
+ if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
+ return false;
+ }
+
+ read_leb_uint32(p, p_end, declare_init_page_count);
+ if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+
+#if WASM_ENABLE_APP_FRAMEWORK == 0
+ max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
+#endif
+ if (mem_flag & MAX_PAGE_COUNT_FLAG) {
+ read_leb_uint32(p, p_end, declare_max_page_count);
+ if (!check_memory_max_size(is_memory64, 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;
+ }
+ }
+ else {
+ /* Limit the maximum memory size to max_page_count */
+ declare_max_page_count = max_page_count;
+ }
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+ if (!wasm_runtime_is_built_in_module(sub_module_name)) {
+ sub_module = (WASMModule *)wasm_runtime_load_depended_module(
+ (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
+ error_buf_size);
+ if (sub_module) {
+ linked_memory = wasm_loader_resolve_memory(
+ sub_module_name, memory_name, declare_init_page_count,
+ declare_max_page_count, error_buf, error_buf_size);
+ if (linked_memory) {
+ /**
+ * 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;
+ }
+#if WASM_ENABLE_WASI_TEST != 0
+ /* a case in wasi-testsuite which imports ("foo" "bar") */
+ else if (!strcmp("foo", sub_module_name)) {
+ uint32 spectest_memory_init_page = 1;
+ uint32 spectest_memory_max_page = 1;
+
+ if (strcmp("bar", 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;
+ }
+#endif
+
+ /* now we believe all declaration are ok */
+ memory->mem_type.flags = mem_flag;
+ memory->mem_type.init_page_count = declare_init_page_count;
+ memory->mem_type.max_page_count = declare_max_page_count;
+ memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
+
+ *p_buf = p;
+
+ (void)parent_module;
+ return true;
+fail:
+ return false;
+}
+
+#if WASM_ENABLE_TAGS != 0
+static bool
+load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
+ const WASMModule *parent_module, /* this module ! */
+ const char *sub_module_name, const char *tag_name,
+ WASMTagImport *tag, /* structure to fill */
+ char *error_buf, uint32 error_buf_size)
+{
+ /* attribute and type of the import statement */
+ uint8 declare_tag_attribute;
+ uint32 declare_type_index;
+ const uint8 *p = *p_buf, *p_end = buf_end;
+#if WASM_ENABLE_MULTI_MODULE != 0
+ WASMModule *sub_module = NULL;
+#endif
+
+ /* get the one byte attribute */
+ CHECK_BUF(p, p_end, 1);
+ declare_tag_attribute = read_uint8(p);
+ if (declare_tag_attribute != 0) {
+ set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
+ goto fail;
+ }
+
+ /* get type */
+ read_leb_uint32(p, p_end, declare_type_index);
+ /* compare against module->types */
+ if (declare_type_index >= parent_module->type_count) {
+ set_error_buf(error_buf, error_buf_size, "unknown tag type");
+ goto fail;
+ }
+
+ WASMFuncType *declare_tag_type =
+ (WASMFuncType *)parent_module->types[declare_type_index];
+
+ /* check, that the type of the declared tag returns void */
+ if (declare_tag_type->result_count != 0) {
+ set_error_buf(error_buf, error_buf_size,
+ "tag type signature does not return void");
+
+ goto fail;
+ }
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+ if (!wasm_runtime_is_built_in_module(sub_module_name)) {
+ sub_module = (WASMModule *)wasm_runtime_load_depended_module(
+ (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
+ error_buf_size);
+ if (sub_module) {
+ /* wasm_loader_resolve_tag checks, that the imported tag
+ * and the declared tag have the same type
+ */
+ uint32 linked_tag_index = 0;
+ WASMTag *linked_tag = wasm_loader_resolve_tag(
+ sub_module_name, tag_name, declare_tag_type,
+ &linked_tag_index /* out */, error_buf, error_buf_size);
+ if (linked_tag) {
+ tag->import_module = sub_module;
+ tag->import_tag_linked = linked_tag;
+ tag->import_tag_index_linked = linked_tag_index;
+ }
+ }
+ }
+#endif
+ /* store to module tag declarations */
+ tag->attribute = declare_tag_attribute;
+ tag->type = declare_type_index;
+
+ tag->module_name = (char *)sub_module_name;
+ tag->field_name = (char *)tag_name;
+ tag->tag_type = declare_tag_type;
+
+ *p_buf = p;
+ (void)parent_module;
+
+ LOG_VERBOSE("Load tag import success\n");
+
+ return true;
+fail:
+ return false;
+}
+#endif /* end of WASM_ENABLE_TAGS != 0 */
+
+static bool
+load_global_import(const uint8 **p_buf, const uint8 *buf_end,
+ WASMModule *parent_module, char *sub_module_name,
+ char *global_name, WASMGlobalImport *global, char *error_buf,
+ uint32 error_buf_size)
+{
+ const uint8 *p = *p_buf, *p_end = buf_end;
+ uint8 declare_type = 0;
+ uint8 declare_mutable = 0;
+#if WASM_ENABLE_MULTI_MODULE != 0
+ WASMModule *sub_module = NULL;
+ WASMGlobal *linked_global = NULL;
+#endif
+#if WASM_ENABLE_GC != 0
+ WASMRefType ref_type;
+ bool need_ref_type_map;
+#endif
+ bool ret = false;
+
+#if WASM_ENABLE_GC == 0
+ CHECK_BUF(p, p_end, 2);
+ /* global type */
+ declare_type = read_uint8(p);
+ if (!is_valid_value_type_for_interpreter(declare_type)) {
+ set_error_buf(error_buf, error_buf_size, "type mismatch");
+ return false;
+ }
+ declare_mutable = read_uint8(p);
+#else
+ if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
+ &need_ref_type_map, &ref_type, false, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+ declare_type = ref_type.ref_type;
+ if (need_ref_type_map) {
+ if (!(global->ref_type =
+ reftype_set_insert(parent_module->ref_type_set, &ref_type,
+ error_buf, error_buf_size))) {
+ return false;
+ }
+ }
+#if TRACE_WASM_LOADER != 0
+ os_printf("import global type: ");
+ wasm_dump_value_type(declare_type, global->ref_type);
+ os_printf("\n");
+#endif
+ CHECK_BUF(p, p_end, 1);
+ declare_mutable = read_uint8(p);
+#endif /* end of WASM_ENABLE_GC == 0 */
+
+ *p_buf = p;
+
+ if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
+ return false;
+ }
+
+#if WASM_ENABLE_LIBC_BUILTIN != 0
+ ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
+ global);
+ if (ret) {
+ if (global->type.val_type != declare_type
+ || global->type.is_mutable != declare_mutable) {
+ set_error_buf(error_buf, error_buf_size,
+ "incompatible import type");
+ return false;
+ }
+ global->is_linked = true;
+ }
+#endif
+#if WASM_ENABLE_MULTI_MODULE != 0
+ if (!global->is_linked
+ && !wasm_runtime_is_built_in_module(sub_module_name)) {
+ sub_module = (WASMModule *)wasm_runtime_load_depended_module(
+ (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
+ error_buf_size);
+ if (sub_module) {
+ /* check sub modules */
+ linked_global = wasm_loader_resolve_global(
+ sub_module_name, global_name, declare_type, declare_mutable,
+ error_buf, error_buf_size);
+ if (linked_global) {
+ global->import_module = sub_module;
+ global->import_global_linked = linked_global;
+ global->is_linked = true;
+ }
+ }
+ }
+#endif
+
+ global->module_name = sub_module_name;
+ global->field_name = global_name;
+ global->type.val_type = declare_type;
+ global->type.is_mutable = (declare_mutable == 1);
+
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ if (global->type.val_type == VALUE_TYPE_V128)
+ parent_module->is_simd_used = true;
+ else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
+ parent_module->is_ref_types_used = true;
+#endif
+ (void)parent_module;
+ (void)ret;
+ return true;
+fail:
+ return false;
+}
+
+static bool
+load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
+ WASMTable *table, char *error_buf, uint32 error_buf_size)
+{
+ const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
+#if WASM_ENABLE_GC != 0
+ WASMRefType ref_type;
+ bool need_ref_type_map;
+#endif
+ bool is_table64 = false;
+
+#if WASM_ENABLE_GC == 0
+ CHECK_BUF(p, p_end, 1);
+ /* 0x70 or 0x6F */
+ table->table_type.elem_type = read_uint8(p);
+ if (VALUE_TYPE_FUNCREF != table->table_type.elem_type
+#if WASM_ENABLE_REF_TYPES != 0
+ && VALUE_TYPE_EXTERNREF != table->table_type.elem_type
+#endif
+ ) {
+ set_error_buf(error_buf, error_buf_size, "incompatible import type");
+ return false;
+ }
+#else /* else of WASM_ENABLE_GC == 0 */
+ if (!resolve_value_type(&p, p_end, module, module->type_count,
+ &need_ref_type_map, &ref_type, false, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+ table->table_type.elem_type = ref_type.ref_type;
+ if (need_ref_type_map) {
+ if (!(table->table_type.elem_ref_type =
+ reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
+ error_buf_size))) {
+ return false;
+ }
+ }
+#if TRACE_WASM_LOADER != 0
+ os_printf("table type: ");
+ wasm_dump_value_type(table->table_type.elem_type,
+ table->table_type.elem_ref_type);
+ os_printf("\n");
+#endif
+#endif /* end of WASM_ENABLE_GC == 0 */
+
+ p_org = p;
+ read_leb_uint32(p, p_end, table->table_type.flags);
+ is_table64 = table->table_type.flags & TABLE64_FLAG;
+ if (p - p_org > 1) {
+ LOG_VERBOSE("integer representation too long(table)");
+ set_error_buf(error_buf, error_buf_size, "invalid limits flags");
+ return false;
+ }
+
+ if (!wasm_table_check_flags(table->table_type.flags, error_buf,
+ error_buf_size, false)) {
+ return false;
+ }
+
+ read_leb_uint32(p, p_end, table->table_type.init_size);
+ if (table->table_type.flags & MAX_TABLE_SIZE_FLAG) {
+ read_leb_uint32(p, p_end, table->table_type.max_size);
+ if (!check_table_max_size(table->table_type.init_size,
+ table->table_type.max_size, error_buf,
+ error_buf_size))
+ return false;
+ }
+
+ adjust_table_max_size(is_table64, table->table_type.init_size,
+ table->table_type.flags & MAX_TABLE_SIZE_FLAG,
+ &table->table_type.max_size);
+
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
+ module->is_ref_types_used = true;
+#endif
+
+ *p_buf = p;
+ return true;
+fail:
+ return false;
+}
+
+static bool
+load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
+ char *error_buf, uint32 error_buf_size)
+{
+ const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
+#if WASM_ENABLE_APP_FRAMEWORK != 0
+ uint32 pool_size = wasm_runtime_memory_pool_size();
+ uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
+ / DEFAULT_NUM_BYTES_PER_PAGE;
+#else
+ uint32 max_page_count;
+#endif
+ bool is_memory64 = false;
+
+ p_org = p;
+ read_leb_uint32(p, p_end, memory->flags);
+ is_memory64 = memory->flags & MEMORY64_FLAG;
+ if (p - p_org > 1) {
+ LOG_VERBOSE("integer representation too long(memory)");
+ set_error_buf(error_buf, error_buf_size, "invalid limits flags");
+ return false;
+ }
+
+ if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
+ false)) {
+ return false;
+ }
+
+ read_leb_uint32(p, p_end, memory->init_page_count);
+ if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
+ error_buf_size))
+ return false;
+
+#if WASM_ENABLE_APP_FRAMEWORK == 0
+ max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
+#endif
+ if (memory->flags & 1) {
+ read_leb_uint32(p, p_end, memory->max_page_count);
+ if (!check_memory_max_size(is_memory64, memory->init_page_count,
+ memory->max_page_count, error_buf,
+ error_buf_size))
+ return false;
+ if (memory->max_page_count > max_page_count)
+ memory->max_page_count = max_page_count;
+ }
+ else {
+ /* Limit the maximum memory size to max_page_count */
+ memory->max_page_count = max_page_count;
+ }
+
+ memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
+
+ *p_buf = p;
+ return true;
+fail:
+ return false;
+}
+
+static int
+cmp_export_name(const void *a, const void *b)
+{
+ return strcmp(*(char **)a, *(char **)b);
+}
+
+static bool
+load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
+ bool is_load_from_file_buf, bool no_resolve,
+ char *error_buf, uint32 error_buf_size)
+{
+ const uint8 *p = buf, *p_end = buf_end, *p_old;
+ uint32 import_count, name_len, type_index, i, u32, flags;
+ uint64 total_size;
+ WASMImport *import;
+ WASMImport *import_functions = NULL, *import_tables = NULL;
+ WASMImport *import_memories = NULL, *import_globals = NULL;
+#if WASM_ENABLE_TAGS != 0
+ WASMImport *import_tags = NULL;
+#endif
+ char *sub_module_name, *field_name;
+ uint8 u8, kind, global_type;
+
+ read_leb_uint32(p, p_end, import_count);
+
+ if (import_count) {
+ module->import_count = import_count;
+ total_size = sizeof(WASMImport) * (uint64)import_count;
+ if (!(module->imports =
+ loader_malloc(total_size, error_buf, error_buf_size))) {
+ 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;
+
+ CHECK_BUF(p, p_end, 1);
+ /* 0x00/0x01/0x02/0x03/0x04 */
+ kind = read_uint8(p);
+
+ 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 */
+ CHECK_BUF(p, p_end, 1);
+ /* 0x70 */
+ u8 = read_uint8(p);
+ 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++;
+
+ if (module->import_table_count > 1) {
+#if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
+ set_error_buf(error_buf, error_buf_size,
+ "multiple tables");
+ return false;
+#elif WASM_ENABLE_WAMR_COMPILER != 0
+ module->is_ref_types_used = true;
+#endif
+ }
+ 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 WASM_ENABLE_MULTI_MEMORY == 0
+ if (module->import_memory_count > 1) {
+ set_error_buf(error_buf, error_buf_size,
+ "multiple memories");
+ return false;
+ }
+#endif
+ break;
+
+#if WASM_ENABLE_TAGS != 0
+ case IMPORT_KIND_TAG: /* import tags */
+ /* it only counts the number of tags to import */
+ module->import_tag_count++;
+ CHECK_BUF(p, p_end, 1);
+ u8 = read_uint8(p);
+ read_leb_uint32(p, p_end, type_index);
+ break;
+#endif
+
+ case IMPORT_KIND_GLOBAL: /* import global */
+#if WASM_ENABLE_GC != 0
+ /* valtype */
+ CHECK_BUF(p, p_end, 1);
+ global_type = read_uint8(p);
+ if (wasm_is_type_multi_byte_type(global_type)) {
+ int32 heap_type;
+ read_leb_int32(p, p_end, heap_type);
+ (void)heap_type;
+ }
+
+ /* mutability */
+ CHECK_BUF(p, p_end, 1);
+ p += 1;
+#else
+ CHECK_BUF(p, p_end, 2);
+ p += 2;
+#endif
+
+ (void)global_type;
+ module->import_global_count++;
+ break;
+
+ default:
+ set_error_buf(error_buf, error_buf_size,
+ "invalid import kind");
+ 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 =
+ module->imports + module->import_function_count
+ + module->import_table_count;
+
+#if WASM_ENABLE_TAGS != 0
+ if (module->import_tag_count)
+ import_tags = module->import_tags =
+ module->imports + module->import_function_count
+ + module->import_table_count + module->import_memory_count;
+ if (module->import_global_count)
+ import_globals = module->import_globals =
+ module->imports + module->import_function_count
+ + module->import_table_count + module->import_memory_count
+ + module->import_tag_count;
+#else
+ if (module->import_global_count)
+ import_globals = module->import_globals =
+ module->imports + module->import_function_count
+ + module->import_table_count + module->import_memory_count;
+#endif
+
+ p = p_old;
+
+ /* Scan again to resolve the data */
+ 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);
+ if (!(sub_module_name = wasm_const_str_list_insert(
+ p, name_len, module, is_load_from_file_buf, error_buf,
+ error_buf_size))) {
+ return false;
+ }
+ p += name_len;
+
+ /* load field name */
+ read_leb_uint32(p, p_end, name_len);
+ CHECK_BUF(p, p_end, name_len);
+ if (!(field_name = wasm_const_str_list_insert(
+ p, name_len, module, is_load_from_file_buf, error_buf,
+ error_buf_size))) {
+ return false;
+ }
+ p += name_len;
+
+ CHECK_BUF(p, p_end, 1);
+ /* 0x00/0x01/0x02/0x03/0x4 */
+ kind = read_uint8(p);
+
+ switch (kind) {
+ case IMPORT_KIND_FUNC: /* import function */
+ bh_assert(import_functions);
+ import = import_functions++;
+ if (!load_function_import(&p, p_end, module,
+ sub_module_name, field_name,
+ &import->u.function, no_resolve,
+ error_buf, error_buf_size)) {
+ return false;
+ }
+ break;
+
+ case IMPORT_KIND_TABLE: /* import table */
+ bh_assert(import_tables);
+ import = import_tables++;
+ if (!load_table_import(&p, p_end, module, sub_module_name,
+ field_name, &import->u.table,
+ error_buf, error_buf_size)) {
+ LOG_DEBUG("can not import such a table (%s,%s)",
+ sub_module_name, field_name);
+ return false;
+ }
+ break;
+
+ case IMPORT_KIND_MEMORY: /* import memory */
+ bh_assert(import_memories);
+ import = import_memories++;
+ if (!load_memory_import(&p, p_end, module, sub_module_name,
+ field_name, &import->u.memory,
+ error_buf, error_buf_size)) {
+ return false;
+ }
+ break;
+
+#if WASM_ENABLE_TAGS != 0
+ case IMPORT_KIND_TAG:
+ bh_assert(import_tags);
+ import = import_tags++;
+ if (!load_tag_import(&p, p_end, module, sub_module_name,
+ field_name, &import->u.tag, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+ break;
+#endif
+
+ case IMPORT_KIND_GLOBAL: /* import global */
+ bh_assert(import_globals);
+ import = import_globals++;
+ if (!load_global_import(&p, p_end, module, sub_module_name,
+ field_name, &import->u.global,
+ error_buf, error_buf_size)) {
+ return false;
+ }
+ break;
+
+ default:
+ set_error_buf(error_buf, error_buf_size,
+ "invalid import kind");
+ return false;
+ }
+ import->kind = kind;
+ import->u.names.module_name = sub_module_name;
+ import->u.names.field_name = field_name;
+ }
+
+#if WASM_ENABLE_LIBC_WASI != 0
+ import = module->import_functions;
+ for (i = 0; i < module->import_function_count; i++, import++) {
+ if (!strcmp(import->u.names.module_name, "wasi_unstable")
+ || !strcmp(import->u.names.module_name,
+ "wasi_snapshot_preview1")) {
+ module->import_wasi_api = true;
+ break;
+ }
+ }
+#endif
+ }
+
+ if (p != p_end) {
+ set_error_buf(error_buf, error_buf_size, "section size mismatch");
+ return false;
+ }
+
+ LOG_VERBOSE("Load import section success.\n");
+ (void)u8;
+ (void)u32;
+ (void)type_index;
+ return true;
+fail:
+ return false;
+}
+
+static bool
+init_function_local_offsets(WASMFunction *func, char *error_buf,
+ uint32 error_buf_size)
+{
+ WASMFuncType *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);
+
+ /*
+ * 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 =
+ loader_malloc(total_size, error_buf, error_buf_size))) {
+ 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;
+}
+
+static bool
+load_function_section(const uint8 *buf, const uint8 *buf_end,
+ const uint8 *buf_code, const uint8 *buf_code_end,
+ WASMModule *module, char *error_buf,
+ uint32 error_buf_size)
+{
+ const uint8 *p = buf, *p_end = buf_end;
+ const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
+ uint32 func_count;
+ uint64 total_size;
+ uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
+ uint32 local_count, local_set_count, sub_local_count, local_cell_num;
+ uint8 type;
+ WASMFunction *func;
+#if WASM_ENABLE_GC != 0
+ bool need_ref_type_map;
+ WASMRefType ref_type;
+ uint32 ref_type_map_count = 0, t = 0, type_index_org;
+#endif
+
+ read_leb_uint32(p, p_end, func_count);
+
+ if (buf_code)
+ read_leb_uint32(p_code, buf_code_end, code_count);
+
+ if (func_count != code_count) {
+ set_error_buf(error_buf, error_buf_size,
+ "function and code section have inconsistent lengths or "
+ "unexpected end");
+ return false;
+ }
+
+ if (is_indices_overflow(module->import_function_count, func_count,
+ error_buf, error_buf_size))
+ return false;
+
+ if (func_count) {
+ module->function_count = func_count;
+ total_size = sizeof(WASMFunction *) * (uint64)func_count;
+ if (!(module->functions =
+ loader_malloc(total_size, error_buf, error_buf_size))) {
+ 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) {
+ set_error_buf(error_buf, error_buf_size, "unknown type");
+ return false;
+ }
+
+#if WASM_ENABLE_GC != 0
+ type_index_org = type_index;
+#endif
+
+#if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
+ && WASM_ENABLE_GC == 0
+ type_index = wasm_get_smallest_type_idx(
+ module->types, module->type_count, type_index);
+#endif
+
+ read_leb_uint32(p_code, buf_code_end, code_size);
+ if (code_size == 0 || p_code + code_size > buf_code_end) {
+ set_error_buf(error_buf, error_buf_size,
+ "invalid function code size");
+ 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;
+
+#if WASM_ENABLE_GC != 0
+ ref_type_map_count = 0;
+#endif
+
+ /* Calculate total local count */
+ for (j = 0; j < local_set_count; j++) {
+ read_leb_uint32(p_code, buf_code_end, sub_local_count);
+ if (sub_local_count > UINT32_MAX - local_count) {
+ set_error_buf(error_buf, error_buf_size, "too many locals");
+ return false;
+ }
+#if WASM_ENABLE_GC == 0
+ CHECK_BUF(p_code, buf_code_end, 1);
+ /* 0x7F/0x7E/0x7D/0x7C */
+ type = read_uint8(p_code);
+ local_count += sub_local_count;
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ /* If any value's type is v128, mark the module as SIMD used */
+ if (type == VALUE_TYPE_V128)
+ module->is_simd_used = true;
+#endif
+#else
+ if (!resolve_value_type(&p_code, buf_code_end, module,
+ module->type_count, &need_ref_type_map,
+ &ref_type, false, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+ local_count += sub_local_count;
+ if (need_ref_type_map)
+ ref_type_map_count += sub_local_count;
+#endif
+ }
+
+ /* Alloc memory, layout: function structure + local types */
+ code_size = (uint32)(p_code_end - p_code);
+
+ total_size = sizeof(WASMFunction) + (uint64)local_count;
+ if (!(func = module->functions[i] =
+ loader_malloc(total_size, error_buf, error_buf_size))) {
+ return false;
+ }
+#if WASM_ENABLE_GC != 0
+ if (ref_type_map_count > 0) {
+ total_size =
+ sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
+ if (!(func->local_ref_type_maps = loader_malloc(
+ total_size, error_buf, error_buf_size))) {
+ return false;
+ }
+ func->local_ref_type_map_count = ref_type_map_count;
+ }
+#endif
+
+ /* Set function type, local count, code size and code body */
+ func->func_type = (WASMFuncType *)module->types[type_index];
+ func->local_count = local_count;
+ if (local_count > 0)
+ func->local_types = (uint8 *)func + sizeof(WASMFunction);
+ func->code_size = code_size;
+ /*
+ * 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;
+ */
+ func->code = (uint8 *)p_code;
+#if WASM_ENABLE_GC != 0
+ func->type_idx = type_index_org;
+#endif
+
+#if WASM_ENABLE_GC != 0
+ t = 0;
+#endif
+
+ /* 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);
+ /* Note: sub_local_count is allowed to be 0 */
+ if (local_type_index > UINT32_MAX - sub_local_count
+ || local_type_index + sub_local_count > local_count) {
+ set_error_buf(error_buf, error_buf_size,
+ "invalid local count");
+ return false;
+ }
+#if WASM_ENABLE_GC == 0
+ CHECK_BUF(p_code, buf_code_end, 1);
+ /* 0x7F/0x7E/0x7D/0x7C */
+ type = read_uint8(p_code);
+ if (!is_valid_value_type_for_interpreter(type)) {
+ if (type == VALUE_TYPE_V128)
+ set_error_buf(error_buf, error_buf_size,
+ "v128 value type requires simd feature");
+ 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");
+ else
+ set_error_buf_v(error_buf, error_buf_size,
+ "invalid local type 0x%02X", type);
+ return false;
+ }
+#else
+ if (!resolve_value_type(&p_code, buf_code_end, module,
+ module->type_count, &need_ref_type_map,
+ &ref_type, false, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+ if (need_ref_type_map) {
+ WASMRefType *ref_type_tmp;
+ if (!(ref_type_tmp = reftype_set_insert(
+ module->ref_type_set, &ref_type, error_buf,
+ error_buf_size))) {
+ return false;
+ }
+ for (k = 0; k < sub_local_count; k++) {
+ func->local_ref_type_maps[t + k].ref_type =
+ ref_type_tmp;
+ func->local_ref_type_maps[t + k].index =
+ local_type_index + k;
+ }
+ t += sub_local_count;
+ }
+ type = ref_type.ref_type;
+#endif
+ for (k = 0; k < sub_local_count; k++) {
+ func->local_types[local_type_index++] = type;
+ }
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ if (type == VALUE_TYPE_V128)
+ module->is_simd_used = true;
+ else if (type == VALUE_TYPE_FUNCREF
+ || type == VALUE_TYPE_EXTERNREF)
+ module->is_ref_types_used = true;
+#endif
+ }
+
+ bh_assert(local_type_index == func->local_count);
+#if WASM_ENABLE_GC != 0
+ bh_assert(t == func->local_ref_type_map_count);
+#if TRACE_WASM_LOADER != 0
+ os_printf("func %u, local types: [", i);
+ k = 0;
+ for (j = 0; j < func->local_count; j++) {
+ WASMRefType *ref_type_tmp = NULL;
+ if (wasm_is_type_multi_byte_type(func->local_types[j])) {
+ bh_assert(j == func->local_ref_type_maps[k].index);
+ ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
+ }
+ wasm_dump_value_type(func->local_types[j], ref_type_tmp);
+ if (j < func->local_count - 1)
+ os_printf(" ");
+ }
+ os_printf("]\n");
+#endif
+#endif
+
+ func->param_cell_num = func->func_type->param_cell_num;
+ func->ret_cell_num = func->func_type->ret_cell_num;
+ local_cell_num =
+ wasm_get_cell_num(func->local_types, func->local_count);
+
+ 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;
+
+ if (!init_function_local_offsets(func, error_buf, error_buf_size))
+ return false;
+
+ p_code = p_code_end;
+ }
+ }
+
+ if (p != p_end) {
+ set_error_buf(error_buf, error_buf_size, "section size mismatch");
+ return false;
+ }
+
+ LOG_VERBOSE("Load function section success.\n");
+ return true;
+fail:
+ return false;
+}
+
+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;
+ uint64 total_size;
+ WASMTable *table;
+
+ read_leb_uint32(p, p_end, table_count);
+ if (module->import_table_count + table_count > 1) {
+#if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
+ /* a total of one table is allowed */
+ set_error_buf(error_buf, error_buf_size, "multiple tables");
+ return false;
+#elif WASM_ENABLE_WAMR_COMPILER != 0
+ module->is_ref_types_used = true;
+#endif
+ }
+
+ if (table_count) {
+ module->table_count = table_count;
+ total_size = sizeof(WASMTable) * (uint64)table_count;
+ if (!(module->tables =
+ loader_malloc(total_size, error_buf, error_buf_size))) {
+ return false;
+ }
+
+ /* load each table */
+ table = module->tables;
+ for (i = 0; i < table_count; i++, table++) {
+#if WASM_ENABLE_GC != 0
+ uint8 flag;
+ bool has_init = false;
+
+ CHECK_BUF(buf, buf_end, 1);
+ flag = read_uint8(p);
+
+ if (flag == TABLE_INIT_EXPR_FLAG) {
+ CHECK_BUF(buf, buf_end, 1);
+ flag = read_uint8(p);
+
+ if (flag != 0x00) {
+ set_error_buf(error_buf, error_buf_size,
+ "invalid leading bytes for table");
+ return false;
+ }
+ has_init = true;
+ }
+ else {
+ p--;
+ }
+#endif /* end of WASM_ENABLE_GC != 0 */
+
+ if (!load_table(&p, p_end, module, table, error_buf,
+ error_buf_size))
+ return false;
+
+#if WASM_ENABLE_GC != 0
+ if (has_init) {
+ if (!load_init_expr(module, &p, p_end, &table->init_expr,
+ table->table_type.elem_type,
+ table->table_type.elem_ref_type, error_buf,
+ error_buf_size))
+ return false;
+ if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
+ && table->init_expr.init_expr_type
+ <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
+ set_error_buf(
+ error_buf, error_buf_size,
+ "unsupported initializer expression for table");
+ return false;
+ }
+ }
+ else {
+ if (wasm_is_reftype_htref_non_nullable(
+ table->table_type.elem_type)) {
+ set_error_buf(
+ error_buf, error_buf_size,
+ "type mismatch: non-nullable table without init expr");
+ return false;
+ }
+ }
+#endif /* end of WASM_ENABLE_GC != 0 */
+
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
+ module->is_ref_types_used = true;
+#endif
+ }
+ }
+
+ if (p != p_end) {
+ set_error_buf(error_buf, error_buf_size, "section size mismatch");
+ return false;
+ }
+
+ LOG_VERBOSE("Load table section success.\n");
+ return true;
+fail:
+ return false;
+}
+
+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;
+ uint64 total_size;
+ WASMMemory *memory;
+
+ read_leb_uint32(p, p_end, memory_count);
+
+#if WASM_ENABLE_MULTI_MEMORY == 0
+ /* 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;
+ }
+#endif
+
+ if (memory_count) {
+ module->memory_count = memory_count;
+ total_size = sizeof(WASMMemory) * (uint64)memory_count;
+ if (!(module->memories =
+ loader_malloc(total_size, error_buf, error_buf_size))) {
+ 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) {
+ set_error_buf(error_buf, error_buf_size, "section size mismatch");
+ return false;
+ }
+
+ LOG_VERBOSE("Load memory section success.\n");
+ return true;
+fail:
+ return false;
+}
+
+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;
+ uint64 total_size;
+ WASMGlobal *global;
+ uint8 mutable;
+#if WASM_ENABLE_GC != 0
+ bool need_ref_type_map;
+ WASMRefType ref_type;
+#endif
+
+ read_leb_uint32(p, p_end, global_count);
+ if (is_indices_overflow(module->import_global_count, global_count,
+ error_buf, error_buf_size))
+ return false;
+
+ module->global_count = 0;
+ if (global_count) {
+ total_size = sizeof(WASMGlobal) * (uint64)global_count;
+ if (!(module->globals =
+ loader_malloc(total_size, error_buf, error_buf_size))) {
+ return false;
+ }
+
+ global = module->globals;
+
+ for (i = 0; i < global_count; i++, global++) {
+#if WASM_ENABLE_GC == 0
+ CHECK_BUF(p, p_end, 2);
+ /* global type */
+ global->type.val_type = read_uint8(p);
+ if (!is_valid_value_type_for_interpreter(global->type.val_type)) {
+ set_error_buf(error_buf, error_buf_size, "type mismatch");
+ return false;
+ }
+ mutable = read_uint8(p);
+#else
+ if (!resolve_value_type(&p, p_end, module, module->type_count,
+ &need_ref_type_map, &ref_type, false,
+ error_buf, error_buf_size)) {
+ return false;
+ }
+ global->type.val_type = ref_type.ref_type;
+ CHECK_BUF(p, p_end, 1);
+ mutable = read_uint8(p);
+#endif /* end of WASM_ENABLE_GC */
+
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ if (global->type.val_type == VALUE_TYPE_V128)
+ module->is_simd_used = true;
+ else if (global->type.val_type == VALUE_TYPE_FUNCREF
+ || global->type.val_type == VALUE_TYPE_EXTERNREF)
+ module->is_ref_types_used = true;
+#endif
+
+ if (!check_mutability(mutable, error_buf, error_buf_size)) {
+ return false;
+ }
+ global->type.is_mutable = mutable ? true : false;
+
+ /* initialize expression */
+ if (!load_init_expr(module, &p, p_end, &(global->init_expr),
+ global->type.val_type,
+#if WASM_ENABLE_GC == 0
+ NULL,
+#else
+ &ref_type,
+#endif
+ error_buf, error_buf_size))
+ return false;
+
+#if WASM_ENABLE_GC != 0
+ if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
+ uint8 global_type;
+ WASMRefType *global_ref_type;
+ uint32 global_idx = global->init_expr.u.global_index;
+
+ if (global->init_expr.u.global_index
+ >= module->import_global_count + i) {
+ set_error_buf(error_buf, error_buf_size, "unknown global");
+ return false;
+ }
+
+ if (global_idx < module->import_global_count) {
+ global_type = module->import_globals[global_idx]
+ .u.global.type.val_type;
+ global_ref_type =
+ module->import_globals[global_idx].u.global.ref_type;
+ }
+ else {
+ global_type =
+ module
+ ->globals[global_idx - module->import_global_count]
+ .type.val_type;
+ global_ref_type =
+ module
+ ->globals[global_idx - module->import_global_count]
+ .ref_type;
+ }
+ if (!wasm_reftype_is_subtype_of(
+ global_type, global_ref_type, global->type.val_type,
+ global->ref_type, module->types, module->type_count)) {
+ set_error_buf(error_buf, error_buf_size, "type mismatch");
+ return false;
+ }
+ }
+
+ if (need_ref_type_map) {
+ if (!(global->ref_type =
+ reftype_set_insert(module->ref_type_set, &ref_type,
+ error_buf, error_buf_size))) {
+ return false;
+ }
+ }
+#if TRACE_WASM_LOADER != 0
+ os_printf("global type: ");
+ wasm_dump_value_type(global->type, global->ref_type);
+ os_printf("\n");
+#endif
+#endif
+ module->global_count++;
+ }
+ bh_assert(module->global_count == global_count);
+ }
+
+ if (p != p_end) {
+ set_error_buf(error_buf, error_buf_size, "section size mismatch");
+ return false;
+ }
+
+ LOG_VERBOSE("Load global section success.\n");
+ return true;
+fail:
+ return false;
+}
+
+static bool
+check_duplicate_exports(WASMModule *module, char *error_buf,
+ uint32 error_buf_size)
+{
+ uint32 i;
+ bool result = false;
+ char *names_buf[32], **names = names_buf;
+
+ if (module->export_count > 32) {
+ names = loader_malloc(module->export_count * sizeof(char *), error_buf,
+ error_buf_size);
+ if (!names) {
+ return result;
+ }
+ }
+
+ for (i = 0; i < module->export_count; i++) {
+ names[i] = module->exports[i].name;
+ }
+
+ qsort(names, module->export_count, sizeof(char *), cmp_export_name);
+
+ for (i = 1; i < module->export_count; i++) {
+ if (!strcmp(names[i], names[i - 1])) {
+ set_error_buf(error_buf, error_buf_size, "duplicate export name");
+ goto cleanup;
+ }
+ }
+
+ result = true;
+cleanup:
+ if (module->export_count > 32) {
+ wasm_runtime_free(names);
+ }
+ return result;
+}
+
+static bool
+load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
+ bool is_load_from_file_buf, char *error_buf,
+ uint32 error_buf_size)
+{
+ const uint8 *p = buf, *p_end = buf_end;
+ uint32 export_count, i, index;
+ uint64 total_size;
+ uint32 str_len;
+ WASMExport *export;
+
+ read_leb_uint32(p, p_end, export_count);
+
+ if (export_count) {
+ module->export_count = export_count;
+ total_size = sizeof(WASMExport) * (uint64)export_count;
+ if (!(module->exports =
+ loader_malloc(total_size, error_buf, error_buf_size))) {
+ return false;
+ }
+
+ export = module->exports;
+ for (i = 0; i < export_count; i++, export ++) {
+#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
+ read_leb_uint32(p, p_end, str_len);
+ CHECK_BUF(p, p_end, str_len);
+
+ if (!(export->name = wasm_const_str_list_insert(
+ p, str_len, module, is_load_from_file_buf, error_buf,
+ error_buf_size))) {
+ return false;
+ }
+
+ p += str_len;
+ CHECK_BUF(p, p_end, 1);
+ export->kind = read_uint8(p);
+ read_leb_uint32(p, p_end, index);
+ export->index = index;
+
+ switch (export->kind) {
+ /* function index */
+ case EXPORT_KIND_FUNC:
+ if (index >= module->function_count
+ + module->import_function_count) {
+ set_error_buf(error_buf, error_buf_size,
+ "unknown function");
+ return false;
+ }
+#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
+ break;
+ /* table index */
+ case EXPORT_KIND_TABLE:
+ if (index
+ >= module->table_count + module->import_table_count) {
+ set_error_buf(error_buf, error_buf_size,
+ "unknown table");
+ return false;
+ }
+ break;
+ /* memory index */
+ case EXPORT_KIND_MEMORY:
+ if (index
+ >= module->memory_count + module->import_memory_count) {
+ set_error_buf(error_buf, error_buf_size,
+ "unknown memory");
+ return false;
+ }
+ break;
+#if WASM_ENABLE_TAGS != 0
+ /* export tag */
+ case EXPORT_KIND_TAG:
+ if (index >= module->tag_count + module->import_tag_count) {
+ set_error_buf(error_buf, error_buf_size, "unknown tag");
+ return false;
+ }
+ break;
+#endif
+
+ /* global index */
+ case EXPORT_KIND_GLOBAL:
+ if (index
+ >= module->global_count + module->import_global_count) {
+ set_error_buf(error_buf, error_buf_size,
+ "unknown global");
+ return false;
+ }
+ break;
+
+ default:
+ set_error_buf(error_buf, error_buf_size,
+ "invalid export kind");
+ return false;
+ }
+ }
+
+ if (!check_duplicate_exports(module, error_buf, error_buf_size)) {
+ return false;
+ }
+ }
+
+ if (p != p_end) {
+ set_error_buf(error_buf, error_buf_size, "section size mismatch");
+ return false;
+ }
+
+ LOG_VERBOSE("Load export section success.\n");
+ return true;
+fail:
+ return false;
+}
+
+static bool
+check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
+ uint32 error_buf_size)
+{
+#if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
+ if (table_index != 0) {
+ set_error_buf(error_buf, error_buf_size, "zero byte expected");
+ return false;
+ }
+#endif
+
+ 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
+load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
+ uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
+{
+ 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;
+}
+
+/* Element segments must match element type of table */
+static bool
+check_table_elem_type(WASMModule *module, uint32 table_index,
+ uint32 type_from_elem_seg, char *error_buf,
+ uint32 error_buf_size)
+{
+ uint32 table_declared_elem_type;
+
+ if (table_index < module->import_table_count)
+ table_declared_elem_type =
+ module->import_tables[table_index].u.table.table_type.elem_type;
+ else
+ table_declared_elem_type =
+ (module->tables + table_index)->table_type.elem_type;
+
+ if (table_declared_elem_type == type_from_elem_seg)
+ return true;
+
+#if WASM_ENABLE_GC != 0
+ /*
+ * balance in: anyref, funcref, (ref.null func) and (ref.func)
+ */
+ if (table_declared_elem_type == REF_TYPE_ANYREF)
+ return true;
+
+ if (table_declared_elem_type == VALUE_TYPE_FUNCREF
+ && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
+ return true;
+
+ if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
+ && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
+ return true;
+#endif
+
+ set_error_buf(error_buf, error_buf_size, "type mismatch");
+ return false;
+}
+
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+static bool
+load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
+ uint32 *p_elem_type,
+#if WASM_ENABLE_GC != 0
+ WASMRefType **p_elem_ref_type,
+#endif
+ bool elemkind_zero, char *error_buf, uint32 error_buf_size)
+{
+ const uint8 *p = *p_buf, *p_end = buf_end;
+ uint8 elem_type;
+#if WASM_ENABLE_GC != 0
+ WASMRefType elem_ref_type;
+ bool need_ref_type_map;
+#endif
+
+ CHECK_BUF(p, p_end, 1);
+ elem_type = read_uint8(p);
+ if (elemkind_zero) {
+ if (elem_type != 0) {
+ set_error_buf(error_buf, error_buf_size,
+ "invalid reference type or unknown type");
+ return false;
+ }
+ else {
+ *p_elem_type = VALUE_TYPE_FUNCREF;
+ *p_buf = p;
+ return true;
+ }
+ }
+
+#if WASM_ENABLE_GC == 0
+ if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
+ set_error_buf(error_buf, error_buf_size,
+ "invalid reference type or unknown type");
+ return false;
+ }
+ *p_elem_type = elem_type;
+#else
+ p--;
+ if (!resolve_value_type((const uint8 **)&p, p_end, module,
+ module->type_count, &need_ref_type_map,
+ &elem_ref_type, false, error_buf, error_buf_size)) {
+ return false;
+ }
+ if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
+ set_error_buf(error_buf, error_buf_size,
+ "invalid reference type or unknown type");
+ return false;
+ }
+ *p_elem_type = elem_ref_type.ref_type;
+ if (need_ref_type_map) {
+ if (!(*p_elem_ref_type =
+ reftype_set_insert(module->ref_type_set, &elem_ref_type,
+ error_buf, error_buf_size))) {
+ return false;
+ }
+ }
+#endif
+
+ *p_buf = p;
+ return true;
+fail:
+ return false;
+}
+#endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
+
+static bool
+load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
+ WASMModule *module, WASMTableSeg *table_segment,
+ char *error_buf, uint32 error_buf_size)
+{
+ 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->value_count = function_count;
+ total_size = sizeof(InitializerExpression) * (uint64)function_count;
+ if (total_size > 0
+ && !(table_segment->init_values =
+ (InitializerExpression *)loader_malloc(total_size, error_buf,
+ error_buf_size))) {
+ return false;
+ }
+
+ for (i = 0; i < function_count; i++) {
+ InitializerExpression *init_expr = &table_segment->init_values[i];
+
+ read_leb_uint32(p, p_end, function_index);
+ if (!check_function_index(module, function_index, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+
+ init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
+ init_expr->u.ref_index = function_index;
+ }
+
+ *p_buf = p;
+ return true;
+fail:
+ return false;
+}
+
+#if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
+static bool
+load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
+ WASMModule *module, WASMTableSeg *table_segment,
+ char *error_buf, uint32 error_buf_size)
+{
+ const uint8 *p = *p_buf, *p_end = buf_end;
+ uint32 ref_count, i;
+ uint64 total_size;
+
+ read_leb_uint32(p, p_end, ref_count);
+ table_segment->value_count = ref_count;
+ total_size = sizeof(InitializerExpression) * (uint64)ref_count;
+ if (total_size > 0
+ && !(table_segment->init_values =
+ (InitializerExpression *)loader_malloc(total_size, error_buf,
+ error_buf_size))) {
+ return false;
+ }
+
+ for (i = 0; i < ref_count; i++) {
+ InitializerExpression *init_expr = &table_segment->init_values[i];
+
+ if (!load_init_expr(module, &p, p_end, init_expr,
+ table_segment->elem_type,
+#if WASM_ENABLE_GC == 0
+ NULL,
+#else
+ table_segment->elem_ref_type,
+#endif
+ error_buf, error_buf_size))
+ return false;
+
+ bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
+ || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
+ || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
+ && init_expr->init_expr_type
+ <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
+ }
+
+ *p_buf = p;
+ return true;
+fail:
+ return false;
+}
+#endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
+
+static bool
+load_table_segment_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;
+ uint8 table_elem_idx_type;
+ uint32 table_segment_count, i;
+ uint64 total_size;
+ WASMTableSeg *table_segment;
+
+ read_leb_uint32(p, p_end, table_segment_count);
+
+ if (table_segment_count) {
+ module->table_seg_count = table_segment_count;
+ total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
+ if (!(module->table_segments =
+ loader_malloc(total_size, error_buf, error_buf_size))) {
+ return false;
+ }
+
+ table_segment = module->table_segments;
+ for (i = 0; i < table_segment_count; i++, table_segment++) {
+ if (p >= p_end) {
+ set_error_buf(error_buf, error_buf_size,
+ "invalid value type or "
+ "invalid elements segment kind");
+ return false;
+ }
+ table_elem_idx_type = VALUE_TYPE_I32;
+
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+ 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:
+ {
+#if WASM_ENABLE_GC != 0
+ if (table_segment->mode == 0) {
+ /* vec(funcidx), set elem type to (ref func) */
+ WASMRefType elem_ref_type = { 0 };
+ table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
+ wasm_set_refheaptype_common(
+ &elem_ref_type.ref_ht_common, false,
+ HEAP_TYPE_FUNC);
+ if (!(table_segment->elem_ref_type = reftype_set_insert(
+ module->ref_type_set, &elem_ref_type,
+ error_buf, error_buf_size)))
+ return false;
+ }
+ else {
+ /* vec(expr), set elem type to funcref */
+ table_segment->elem_type = VALUE_TYPE_FUNCREF;
+ }
+#else
+ table_segment->elem_type = VALUE_TYPE_FUNCREF;
+#endif
+ table_segment->table_index = 0;
+
+ if (!check_table_index(module, table_segment->table_index,
+ error_buf, error_buf_size))
+ return false;
+
+#if WASM_ENABLE_MEMORY64 != 0
+ table_elem_idx_type =
+ is_table_64bit(module, table_segment->table_index)
+ ? VALUE_TYPE_I64
+ : VALUE_TYPE_I32;
+#endif
+ if (!load_init_expr(module, &p, p_end,
+ &table_segment->base_offset,
+ table_elem_idx_type, NULL, error_buf,
+ error_buf_size))
+ return false;
+
+ if (table_segment->mode == 0) {
+ /* vec(funcidx) */
+ if (!load_func_index_vec(&p, p_end, module,
+ table_segment, error_buf,
+ error_buf_size))
+ return false;
+ }
+ else {
+ /* vec(expr) */
+ if (!load_init_expr_vec(&p, p_end, module,
+ table_segment, error_buf,
+ error_buf_size))
+ return false;
+ }
+
+ if (!check_table_elem_type(module,
+ table_segment->table_index,
+ table_segment->elem_type,
+ error_buf, error_buf_size))
+ return false;
+
+ break;
+ }
+ /* elemkind + passive/declarative */
+ case 1:
+ case 3:
+ if (!load_elem_type(module, &p, p_end,
+ &table_segment->elem_type,
+#if WASM_ENABLE_GC != 0
+ &table_segment->elem_ref_type,
+#endif
+ true, error_buf, error_buf_size))
+ return false;
+ /* vec(funcidx) */
+ if (!load_func_index_vec(&p, p_end, module, table_segment,
+ 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 WASM_ENABLE_MEMORY64 != 0
+ table_elem_idx_type =
+ is_table_64bit(module, table_segment->table_index)
+ ? VALUE_TYPE_I64
+ : VALUE_TYPE_I32;
+#endif
+ if (!load_init_expr(module, &p, p_end,
+ &table_segment->base_offset,
+ table_elem_idx_type, NULL, error_buf,
+ error_buf_size))
+ return false;
+ if (!load_elem_type(module, &p, p_end,
+ &table_segment->elem_type,
+#if WASM_ENABLE_GC != 0
+ &table_segment->elem_ref_type,
+#endif
+ table_segment->mode == 2 ? true : false,
+ error_buf, error_buf_size))
+ return false;
+
+ if (table_segment->mode == 2) {
+ /* vec(funcidx) */
+ if (!load_func_index_vec(&p, p_end, module,
+ table_segment, error_buf,
+ error_buf_size))
+ return false;
+ }
+ else {
+ /* vec(expr) */
+ if (!load_init_expr_vec(&p, p_end, module,
+ table_segment, error_buf,
+ error_buf_size))
+ return false;
+ }
+
+ if (!check_table_elem_type(module,
+ table_segment->table_index,
+ table_segment->elem_type,
+ error_buf, error_buf_size))
+ return false;
+
+ break;
+ case 5:
+ case 7:
+ if (!load_elem_type(module, &p, p_end,
+ &table_segment->elem_type,
+#if WASM_ENABLE_GC != 0
+ &table_segment->elem_ref_type,
+#endif
+ false, error_buf, error_buf_size))
+ return false;
+ /* vec(expr) */
+ if (!load_init_expr_vec(&p, p_end, module, table_segment,
+ error_buf, error_buf_size))
+ return false;
+ break;
+ default:
+ set_error_buf(error_buf, error_buf_size,
+ "unknown element segment kind");
+ return false;
+ }
+#else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
+ /*
+ * 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 WASM_ENABLE_MEMORY64 != 0
+ table_elem_idx_type =
+ is_table_64bit(module, table_segment->table_index)
+ ? VALUE_TYPE_I64
+ : VALUE_TYPE_I32;
+#endif
+ if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
+ table_elem_idx_type, NULL, error_buf,
+ error_buf_size))
+ return false;
+ if (!load_func_index_vec(&p, p_end, module, table_segment,
+ error_buf, error_buf_size))
+ return false;
+
+ table_segment->elem_type = VALUE_TYPE_FUNCREF;
+
+ if (!check_table_elem_type(module, table_segment->table_index,
+ table_segment->elem_type, error_buf,
+ error_buf_size))
+ return false;
+#endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
+
+#if WASM_ENABLE_MEMORY64 != 0
+ if (table_elem_idx_type == VALUE_TYPE_I64
+ && table_segment->base_offset.u.u64 > UINT32_MAX) {
+ set_error_buf(error_buf, error_buf_size,
+ "In table64, table base offset can't be "
+ "larger than UINT32_MAX");
+ return false;
+ }
+#endif
+
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
+ module->is_ref_types_used = true;
+#endif
+ }
+ }
+
+ if (p != p_end) {
+ set_error_buf(error_buf, error_buf_size, "section size mismatch");
+ return false;
+ }
+
+ LOG_VERBOSE("Load table segment section success.\n");
+ return true;
+fail:
+ return false;
+}
+
+static bool
+load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
+ WASMModule *module,
+#if WASM_ENABLE_BULK_MEMORY != 0
+ bool has_datacount_section,
+#endif
+ bool clone_data_seg, char *error_buf,
+ uint32 error_buf_size)
+{
+ const uint8 *p = buf, *p_end = buf_end;
+ uint32 data_seg_count, i, mem_index, data_seg_len;
+ uint64 total_size;
+ WASMDataSeg *dataseg;
+ InitializerExpression init_expr;
+#if WASM_ENABLE_BULK_MEMORY != 0
+ bool is_passive = false;
+ uint32 mem_flag;
+#endif
+ uint8 mem_offset_type = VALUE_TYPE_I32;
+
+ read_leb_uint32(p, p_end, data_seg_count);
+
+#if WASM_ENABLE_BULK_MEMORY != 0
+ if (has_datacount_section && 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
+
+ if (data_seg_count) {
+ module->data_seg_count = data_seg_count;
+ total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
+ if (!(module->data_segments =
+ loader_malloc(total_size, error_buf, error_buf_size))) {
+ return false;
+ }
+
+ for (i = 0; i < data_seg_count; i++) {
+ read_leb_uint32(p, p_end, mem_index);
+#if WASM_ENABLE_BULK_MEMORY != 0
+ is_passive = false;
+ mem_flag = mem_index & 0x03;
+ switch (mem_flag) {
+ case 0x01:
+ is_passive = true;
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ module->is_bulk_memory_used = true;
+#endif
+ 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);
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ module->is_bulk_memory_used = true;
+#endif
+ check_mem_index:
+ if (mem_index
+ >= module->import_memory_count + module->memory_count) {
+ set_error_buf_v(error_buf, error_buf_size,
+ "unknown memory %d", mem_index);
+ return false;
+ }
+ break;
+ case 0x03:
+ default:
+ set_error_buf(error_buf, error_buf_size, "unknown memory");
+ return false;
+ break;
+ }
+#else
+ if (mem_index
+ >= module->import_memory_count + module->memory_count) {
+ set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
+ mem_index);
+ return false;
+ }
+#endif /* WASM_ENABLE_BULK_MEMORY */
+
+#if WASM_ENABLE_BULK_MEMORY != 0
+ if (!is_passive)
+#endif
+ {
+#if WASM_ENABLE_MEMORY64 != 0
+ /* This memory_flag is from memory instead of data segment */
+ uint8 memory_flag;
+ if (module->import_memory_count > 0) {
+ memory_flag = module->import_memories[mem_index]
+ .u.memory.mem_type.flags;
+ }
+ else {
+ memory_flag =
+ module
+ ->memories[mem_index - module->import_memory_count]
+ .flags;
+ }
+ mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
+ : VALUE_TYPE_I32;
+#else
+ mem_offset_type = VALUE_TYPE_I32;
+#endif
+ }
+
+#if WASM_ENABLE_BULK_MEMORY != 0
+ if (!is_passive)
+#endif
+ if (!load_init_expr(module, &p, p_end, &init_expr,
+ mem_offset_type, NULL, error_buf,
+ error_buf_size))
+ return false;
+
+ read_leb_uint32(p, p_end, data_seg_len);
+
+ if (!(dataseg = module->data_segments[i] = loader_malloc(
+ sizeof(WASMDataSeg), error_buf, error_buf_size))) {
+ return false;
+ }
+
+#if WASM_ENABLE_BULK_MEMORY != 0
+ dataseg->is_passive = is_passive;
+ if (!is_passive)
+#endif
+ {
+ bh_memcpy_s(&dataseg->base_offset,
+ sizeof(InitializerExpression), &init_expr,
+ sizeof(InitializerExpression));
+
+ dataseg->memory_index = mem_index;
+ }
+
+ dataseg->data_length = data_seg_len;
+ CHECK_BUF(p, p_end, data_seg_len);
+ if (clone_data_seg) {
+ if (!(dataseg->data = loader_malloc(
+ dataseg->data_length, error_buf, error_buf_size))) {
+ return false;
+ }
+
+ bh_memcpy_s(dataseg->data, dataseg->data_length, p,
+ data_seg_len);
+ }
+ else {
+ dataseg->data = (uint8 *)p;
+ }
+ dataseg->is_data_cloned = clone_data_seg;
+ p += data_seg_len;
+ }
+ }
+
+ if (p != p_end) {
+ set_error_buf(error_buf, error_buf_size, "section size mismatch");
+ return false;
+ }
+
+ LOG_VERBOSE("Load data segment section success.\n");
+ return true;
+fail:
+ return false;
+}
+
+#if WASM_ENABLE_BULK_MEMORY != 0
+static bool
+load_datacount_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 data_seg_count1 = 0;
+
+ read_leb_uint32(p, p_end, data_seg_count1);
+ module->data_seg_count1 = data_seg_count1;
+
+ if (p != p_end) {
+ set_error_buf(error_buf, error_buf_size, "section size mismatch");
+ return false;
+ }
+
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ module->is_bulk_memory_used = true;
+#endif
+ LOG_VERBOSE("Load datacount section success.\n");
+ return true;
+fail:
+ return false;
+}
+#endif
+
+#if WASM_ENABLE_TAGS != 0
+static bool
+load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
+ const uint8 *buf_code_end, WASMModule *module, char *error_buf,
+ uint32 error_buf_size)
+{
+ (void)buf_code;
+ (void)buf_code_end;
+
+ const uint8 *p = buf, *p_end = buf_end;
+ size_t total_size = 0;
+ /* number of tags defined in the section */
+ uint32 section_tag_count = 0;
+ uint8 tag_attribute;
+ uint32 tag_type;
+ WASMTag *tag = NULL;
+
+ /* get tag count */
+ read_leb_uint32(p, p_end, section_tag_count);
+ if (is_indices_overflow(module->import_tag_count, section_tag_count,
+ error_buf, error_buf_size))
+ return false;
+
+ module->tag_count = section_tag_count;
+
+ if (section_tag_count) {
+ total_size = sizeof(WASMTag *) * module->tag_count;
+ if (!(module->tags =
+ loader_malloc(total_size, error_buf, error_buf_size))) {
+ return false;
+ }
+ /* load each tag, imported tags precede the tags */
+ uint32 tag_index;
+ for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
+
+ /* get the one byte attribute */
+ CHECK_BUF(p, p_end, 1);
+ tag_attribute = read_uint8(p);
+
+ /* get type */
+ read_leb_uint32(p, p_end, tag_type);
+ /* compare against module->types */
+ if (tag_type >= module->type_count) {
+ set_error_buf(error_buf, error_buf_size, "unknown type");
+ return false;
+ }
+
+ /* get return type (must be 0) */
+ /* check, that the type of the referred tag returns void */
+ WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
+ if (func_type->result_count != 0) {
+ set_error_buf(error_buf, error_buf_size,
+ "non-empty tag result type");
+
+ goto fail;
+ }
+
+ if (!(tag = module->tags[tag_index] = loader_malloc(
+ sizeof(WASMTag), error_buf, error_buf_size))) {
+ return false;
+ }
+
+ /* store to module tag declarations */
+ tag->attribute = tag_attribute;
+ tag->type = tag_type;
+ tag->tag_type = func_type;
+ }
+ }
+
+ if (p != p_end) {
+ set_error_buf(error_buf, error_buf_size, "section size mismatch");
+ return false;
+ }
+
+ LOG_VERBOSE("Load tag section success.\n");
+ return true;
+fail:
+ return false;
+}
+#endif /* end of WASM_ENABLE_TAGS != 0 */
+
+static bool
+load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
+ const uint8 *buf_func_end, WASMModule *module,
+ char *error_buf, uint32 error_buf_size)
+{
+ 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;
+ }
+
+ LOG_VERBOSE("Load code segment section success.\n");
+ (void)module;
+ return true;
+fail:
+ return false;
+}
+
+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;
+ WASMFuncType *type;
+ uint32 start_function;
+
+ read_leb_uint32(p, p_end, start_function);
+
+ if (start_function
+ >= module->function_count + module->import_function_count) {
+ set_error_buf(error_buf, error_buf_size, "unknown function");
+ return false;
+ }
+
+ if (start_function < module->import_function_count)
+ type = module->import_functions[start_function].u.function.func_type;
+ else
+ type = module->functions[start_function - module->import_function_count]
+ ->func_type;
+ if (type->param_count != 0 || type->result_count != 0) {
+ set_error_buf(error_buf, error_buf_size, "invalid start function");
+ return false;
+ }
+
+ module->start_function = start_function;
+
+ if (p != p_end) {
+ set_error_buf(error_buf, error_buf_size, "section size mismatch");
+ return false;
+ }
+
+ LOG_VERBOSE("Load start section success.\n");
+ return true;
+fail:
+ return false;
+}
+
+#if WASM_ENABLE_STRINGREF != 0
+static bool
+load_stringref_section(const uint8 *buf, const uint8 *buf_end,
+ WASMModule *module, bool is_load_from_file_buf,
+ char *error_buf, uint32 error_buf_size)
+{
+ const uint8 *p = buf, *p_end = buf_end;
+ int32 deferred_count, immediate_count, string_length, i;
+ uint64 total_size;
+
+ read_leb_uint32(p, p_end, deferred_count);
+ read_leb_uint32(p, p_end, immediate_count);
+
+ /* proposal set deferred_count for future extension */
+ if (deferred_count != 0) {
+ goto fail;
+ }
+
+ if (immediate_count > 0) {
+ total_size = sizeof(char *) * (uint64)immediate_count;
+ if (!(module->string_literal_ptrs =
+ loader_malloc(total_size, error_buf, error_buf_size))) {
+ goto fail;
+ }
+ module->string_literal_count = immediate_count;
+
+ total_size = sizeof(uint32) * (uint64)immediate_count;
+ if (!(module->string_literal_lengths =
+ loader_malloc(total_size, error_buf, error_buf_size))) {
+ goto fail;
+ }
+
+ for (i = 0; i < immediate_count; i++) {
+ read_leb_uint32(p, p_end, string_length);
+
+ CHECK_BUF(p, p_end, string_length);
+ module->string_literal_ptrs[i] = p;
+ module->string_literal_lengths[i] = string_length;
+ p += string_length;
+ }
+ }
+
+ if (p != p_end) {
+ set_error_buf(error_buf, error_buf_size, "section size mismatch");
+ goto fail;
+ }
+
+ LOG_VERBOSE("Load stringref section success.\n");
+ return true;
+
+fail:
+ return false;
+}
+#endif /* end of WASM_ENABLE_STRINGREF != 0 */
+
+#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
+static bool
+handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
+ bool is_load_from_file_buf, char *error_buf,
+ uint32 error_buf_size)
+{
+ 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);
+ /* Skip the import functions */
+ if (func_index >= module->import_function_count) {
+ func_index -= module->import_function_count;
+ 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 =
+ wasm_const_str_list_insert(
+ p, func_name_len, module,
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ false,
+#else
+ is_load_from_file_buf,
+#endif
+ error_buf, error_buf_size))) {
+ return false;
+ }
+ }
+ p += func_name_len;
+ }
+ }
+ break;
+ case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
+ */
+ case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
+ default:
+ p = p + subsection_size;
+ break;
+ }
+ i++;
+ }
+
+ return true;
+fail:
+ return false;
+}
+#endif
+
+static bool
+load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
+ bool is_load_from_file_buf, char *error_buf,
+ uint32 error_buf_size)
+{
+ const uint8 *p = buf, *p_end = buf_end;
+ char section_name[32];
+ uint32 name_len, buffer_len;
+
+ if (p >= p_end) {
+ set_error_buf(error_buf, error_buf_size, "unexpected end");
+ return false;
+ }
+
+ read_leb_uint32(p, p_end, name_len);
+
+ if (p + name_len > p_end) {
+ set_error_buf(error_buf, error_buf_size, "unexpected end");
+ return false;
+ }
+
+ if (!wasm_check_utf8_str(p, name_len)) {
+ set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
+ return false;
+ }
+
+ 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);
+ }
+
+#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
+ if (name_len == 4 && memcmp(p, "name", 4) == 0) {
+ module->name_section_buf = buf;
+ module->name_section_buf_end = buf_end;
+ p += name_len;
+ if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
+ error_buf, error_buf_size)) {
+ return false;
+ }
+ LOG_VERBOSE("Load custom name section success.");
+ }
+#endif
+
+#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);
+ section->content_len = (uint32)(p_end - p - name_len);
+
+ 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);
+
+ (void)is_load_from_file_buf;
+ (void)module;
+ return true;
+fail:
+ return false;
+}
+
+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);
+#if WASM_ENABLE_FAST_JIT != 0
+ import_global->data_offset = data_offset;
+#endif
+ data_offset += wasm_value_type_size(import_global->type.val_type);
+ }
+
+ for (i = 0; i < module->global_count; i++) {
+ WASMGlobal *global = module->globals + i;
+#if WASM_ENABLE_FAST_JIT != 0
+ global->data_offset = data_offset;
+#endif
+ data_offset += wasm_value_type_size(global->type.val_type);
+ }
+
+ module->global_data_size = data_offset;
+}
+
+#if WASM_ENABLE_FAST_JIT != 0
+static bool
+init_fast_jit_functions(WASMModule *module, char *error_buf,
+ uint32 error_buf_size)
+{
+#if WASM_ENABLE_LAZY_JIT != 0
+ JitGlobals *jit_globals = jit_compiler_get_jit_globals();
+#endif
+ uint32 i;
+
+ if (!module->function_count)
+ return true;
+
+ if (!(module->fast_jit_func_ptrs =
+ loader_malloc(sizeof(void *) * module->function_count, error_buf,
+ error_buf_size))) {
+ return false;
+ }
+
+#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
+
+ 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;
+ }
+
+ return true;
+}
+#endif /* end of WASM_ENABLE_FAST_JIT != 0 */
+
+#if WASM_ENABLE_JIT != 0
+static bool
+init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
+ uint32 error_buf_size)
+{
+ LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
+ AOTCompOption option = { 0 };
+ char *aot_last_error;
+ uint64 size;
+#if WASM_ENABLE_GC != 0
+ bool gc_enabled = true;
+#else
+ bool gc_enabled = false;
+#endif
+
+ if (module->function_count == 0)
+ return true;
+
+#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
+ if (os_mutex_init(&module->tierup_wait_lock) != 0) {
+ set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
+ return false;
+ }
+ 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);
+
+ module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
+ 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;
+
+ option.opt_level = llvm_jit_options->opt_level;
+ option.size_level = llvm_jit_options->size_level;
+ option.segue_flags = llvm_jit_options->segue_flags;
+ option.quick_invoke_c_api_import =
+ llvm_jit_options->quick_invoke_c_api_import;
+
+#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_GC == 0 && WASM_ENABLE_REF_TYPES != 0
+ option.enable_ref_types = true;
+#elif WASM_ENABLE_GC != 0
+ option.enable_gc = true;
+#endif
+ option.enable_aux_stack_check = true;
+#if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
+ || WASM_ENABLE_AOT_STACK_FRAME != 0
+ option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
+ aot_call_stack_features_init_default(&option.call_stack_features);
+#endif
+#if WASM_ENABLE_PERF_PROFILING != 0
+ option.enable_perf_profiling = true;
+#endif
+#if WASM_ENABLE_MEMORY_PROFILING != 0
+ option.enable_memory_profiling = true;
+ option.enable_stack_estimation = true;
+#endif
+#if WASM_ENABLE_SHARED_HEAP != 0
+ option.enable_shared_heap = true;
+#endif
+
+ 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;
+ }
+
+ 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;
+
+ 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;
+ }
+
+#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");
+
+ for (i = 0; i < module->function_count; i++) {
+ LLVMOrcJITTargetAddress func_addr = 0;
+ LLVMErrorRef error;
+ char func_name[48];
+
+ snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
+ error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
+ func_name);
+ if (error != LLVMErrorSuccess) {
+ char *err_msg = LLVMGetErrorMessage(error);
+ set_error_buf_v(error_buf, error_buf_size,
+ "failed to compile llvm jit function: %s", err_msg);
+ LLVMDisposeErrorMessage(err_msg);
+ return false;
+ }
+
+ /**
+ * 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;
+
+#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
+ module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
+
+ if (module->orcjit_stop_compiling)
+ return false;
+#endif
+ }
+
+ 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);
+
+ 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 functions of this group */
+ for (i = group_idx; i < func_count; i += group_stride) {
+ if (!jit_compiler_compile(module, i + module->import_function_count)) {
+ LOG_ERROR("failed to compile fast jit function %u\n", i);
+ break;
+ }
+
+ if (module->orcjit_stop_compiling) {
+ return NULL;
+ }
+ }
+#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
+#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)) {
+ LOG_ERROR(
+ "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;
+ }
+ }
+ }
+
+ /* Wait until init_llvm_jit_functions_stage2 finishes and all
+ fast jit functions are compiled */
+ os_mutex_lock(&module->tierup_wait_lock);
+ while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
+ && module->fast_jit_ready_groups >= group_stride)) {
+ os_cond_reltimedwait(&module->tierup_wait_cond,
+ &module->tierup_wait_lock, 10000);
+ 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);
+ LOG_ERROR("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);
+ LOG_ERROR("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 function 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 =
+ (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
+ uint32 i, j;
+
+ bh_print_time("Begin to compile jit functions");
+
+ /* Create threads to compile the jit functions */
+ for (i = 0; i < thread_num && i < module->function_count; i++) {
+#if WASM_ENABLE_JIT != 0
+ module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
+#endif
+ 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) {
+ set_error_buf(error_buf, error_buf_size,
+ "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);
+ }
+ return false;
+ }
+ }
+
+#if WASM_ENABLE_LAZY_JIT == 0
+ /* Wait until all jit functions are compiled for eager mode */
+ for (i = 0; i < thread_num; i++) {
+ 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;
+ }
+ }
+#endif
+#endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
+
+ bh_print_time("End compile jit functions");
+
+ return true;
+}
+#endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
+
+static bool
+wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
+ uint32 cur_func_idx, char *error_buf,
+ uint32 error_buf_size);
+
+#if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
+void **
+wasm_interp_get_handle_table(void);
+
+static void **handle_table;
+#endif
+
+static bool
+load_from_sections(WASMModule *module, WASMSection *sections,
+ bool is_load_from_file_buf, bool wasm_binary_freeable,
+ bool no_resolve, char *error_buf, uint32 error_buf_size)
+{
+ WASMExport *export;
+ WASMSection *section = sections;
+ const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
+ *buf_func = NULL, *buf_func_end = NULL;
+ WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
+ WASMGlobal *aux_stack_top_global = NULL, *global;
+ uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
+ aux_stack_top = (uint64)-1LL;
+ uint32 global_index, func_index, i;
+ uint32 aux_data_end_global_index = (uint32)-1;
+ uint32 aux_heap_base_global_index = (uint32)-1;
+ WASMFuncType *func_type;
+ uint8 malloc_free_io_type = VALUE_TYPE_I32;
+ bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
+ bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
+#if WASM_ENABLE_BULK_MEMORY != 0
+ bool has_datacount_section = false;
+#endif
+
+ /* Find code and function sections if have */
+ while (section) {
+ if (section->section_type == SECTION_TYPE_CODE) {
+ buf_code = section->section_body;
+ buf_code_end = buf_code + section->section_body_size;
+#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
+ }
+ else if (section->section_type == SECTION_TYPE_FUNC) {
+ buf_func = section->section_body;
+ buf_func_end = buf_func + section->section_body_size;
+ }
+ 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. */
+ if (!load_user_section(buf, buf_end, module,
+ reuse_const_strings, error_buf,
+ error_buf_size))
+ return false;
+ break;
+ case SECTION_TYPE_TYPE:
+ if (!load_type_section(buf, buf_end, module, error_buf,
+ error_buf_size))
+ return false;
+ break;
+ case SECTION_TYPE_IMPORT:
+ if (!load_import_section(buf, buf_end, module,
+ reuse_const_strings, no_resolve,
+ error_buf, error_buf_size))
+ return false;
+ break;
+ case SECTION_TYPE_FUNC:
+ if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
+ module, error_buf, error_buf_size))
+ return false;
+ break;
+ case SECTION_TYPE_TABLE:
+ if (!load_table_section(buf, buf_end, module, error_buf,
+ error_buf_size))
+ return false;
+ break;
+ case SECTION_TYPE_MEMORY:
+ if (!load_memory_section(buf, buf_end, module, error_buf,
+ error_buf_size))
+ return false;
+ break;
+#if WASM_ENABLE_TAGS != 0
+ case SECTION_TYPE_TAG:
+ /* load tag declaration section */
+ if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
+ module, error_buf, error_buf_size))
+ return false;
+ break;
+#endif
+ case SECTION_TYPE_GLOBAL:
+ if (!load_global_section(buf, buf_end, module, error_buf,
+ error_buf_size))
+ return false;
+ break;
+ case SECTION_TYPE_EXPORT:
+ if (!load_export_section(buf, buf_end, module,
+ reuse_const_strings, error_buf,
+ error_buf_size))
+ return false;
+ break;
+ case SECTION_TYPE_START:
+ if (!load_start_section(buf, buf_end, module, error_buf,
+ error_buf_size))
+ return false;
+ break;
+ case SECTION_TYPE_ELEM:
+ if (!load_table_segment_section(buf, buf_end, module, error_buf,
+ error_buf_size))
+ return false;
+ break;
+ case SECTION_TYPE_CODE:
+ if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
+ module, error_buf, error_buf_size))
+ return false;
+ break;
+ case SECTION_TYPE_DATA:
+ if (!load_data_segment_section(buf, buf_end, module,
+#if WASM_ENABLE_BULK_MEMORY != 0
+ has_datacount_section,
+#endif
+ clone_data_seg, error_buf,
+ error_buf_size))
+ return false;
+ break;
+#if WASM_ENABLE_BULK_MEMORY != 0
+ case SECTION_TYPE_DATACOUNT:
+ if (!load_datacount_section(buf, buf_end, module, error_buf,
+ error_buf_size))
+ return false;
+ has_datacount_section = true;
+ break;
+#endif
+#if WASM_ENABLE_STRINGREF != 0
+ case SECTION_TYPE_STRINGREF:
+ if (!load_stringref_section(buf, buf_end, module,
+ reuse_const_strings, error_buf,
+ error_buf_size))
+ return false;
+ break;
+#endif
+ default:
+ set_error_buf(error_buf, error_buf_size, "invalid section id");
+ return false;
+ }
+
+ section = section->next;
+ }
+
+ 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 */
+ export = module->exports;
+ for (i = 0; i < module->export_count; i++, export ++) {
+ 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;
+ if (global->type.val_type == VALUE_TYPE_I32
+ && !global->type.is_mutable
+ && global->init_expr.init_expr_type
+ == INIT_EXPR_TYPE_I32_CONST) {
+ aux_heap_base_global = global;
+ aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
+ aux_heap_base_global_index = export->index;
+ LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
+ aux_heap_base);
+ }
+ }
+ else if (!strcmp(export->name, "__data_end")) {
+ global_index = export->index - module->import_global_count;
+ global = module->globals + global_index;
+ if (global->type.val_type == VALUE_TYPE_I32
+ && !global->type.is_mutable
+ && global->init_expr.init_expr_type
+ == INIT_EXPR_TYPE_I32_CONST) {
+ aux_data_end_global = global;
+ aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
+ aux_data_end_global_index = export->index;
+ LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
+ aux_data_end);
+
+ aux_data_end = align_uint64(aux_data_end, 16);
+ }
+ }
+
+ /* 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
+ */
+ 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;
+
+ /* Resolve aux stack top global */
+ for (global_index = 0; global_index < module->global_count;
+ global_index++) {
+ global = module->globals + global_index;
+ if (global->type.is_mutable /* heap_base and data_end is
+ not mutable */
+ && global->type.val_type == VALUE_TYPE_I32
+ && global->init_expr.init_expr_type
+ == INIT_EXPR_TYPE_I32_CONST
+ && (uint64)(uint32)global->init_expr.u.i32
+ <= aux_heap_base) {
+ aux_stack_top_global = global;
+ aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
+ module->aux_stack_top_global_index =
+ module->import_global_count + global_index;
+ module->aux_stack_bottom = aux_stack_top;
+ module->aux_stack_size =
+ aux_stack_top > aux_data_end
+ ? (uint32)(aux_stack_top - aux_data_end)
+ : (uint32)aux_stack_top;
+ LOG_VERBOSE(
+ "Found aux stack top global, value: %" PRIu64 ", "
+ "global index: %d, stack size: %d",
+ aux_stack_top, global_index,
+ module->aux_stack_size);
+ break;
+ }
+ }
+ 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;
+ }
+ break;
+ }
+ }
+ }
+
+ module->malloc_function = (uint32)-1;
+ module->free_function = (uint32)-1;
+ module->retain_function = (uint32)-1;
+
+ /* Resolve malloc/free function exported by wasm module */
+#if WASM_ENABLE_MEMORY64 != 0
+ if (has_module_memory64(module))
+ malloc_free_io_type = VALUE_TYPE_I64;
+#endif
+ export = module->exports;
+ for (i = 0; i < module->export_count; i++, export ++) {
+ 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;
+ if (func_type->param_count == 1 && func_type->result_count == 1
+ && func_type->types[0] == malloc_free_io_type
+ && func_type->types[1] == malloc_free_io_type) {
+ 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) {
+ /* __new && __pin for AssemblyScript */
+ func_index = export->index - module->import_function_count;
+ func_type = module->functions[func_index]->func_type;
+ if (func_type->param_count == 2 && func_type->result_count == 1
+ && func_type->types[0] == malloc_free_io_type
+ && func_type->types[1] == VALUE_TYPE_I32
+ && func_type->types[2] == malloc_free_io_type) {
+ uint32 j;
+ WASMExport *export_tmp;
+
+ 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);
+
+ /* resolve retain function.
+ If not found, reset malloc function index */
+ export_tmp = module->exports;
+ for (j = 0; j < module->export_count; j++, export_tmp++) {
+ if ((export_tmp->kind == EXPORT_KIND_FUNC)
+ && (!strcmp(export_tmp->name, "__retain")
+ || (!strcmp(export_tmp->name, "__pin")))
+ && (export_tmp->index
+ >= module->import_function_count)) {
+ func_index = export_tmp->index
+ - module->import_function_count;
+ func_type =
+ module->functions[func_index]->func_type;
+ if (func_type->param_count == 1
+ && func_type->result_count == 1
+ && func_type->types[0] == malloc_free_io_type
+ && func_type->types[1] == malloc_free_io_type) {
+ bh_assert(module->retain_function
+ == (uint32)-1);
+ module->retain_function = export_tmp->index;
+ LOG_VERBOSE("Found retain function, name: %s, "
+ "index: %u",
+ export_tmp->name,
+ export_tmp->index);
+ break;
+ }
+ }
+ }
+ if (j == module->export_count) {
+ module->malloc_function = (uint32)-1;
+ LOG_VERBOSE("Can't find retain function,"
+ "reset malloc function index to -1");
+ }
+ }
+ }
+ else if (((!strcmp(export->name, "free"))
+ || (!strcmp(export->name, "__release"))
+ || (!strcmp(export->name, "__unpin")))
+ && export->index >= module->import_function_count) {
+ func_index = export->index - module->import_function_count;
+ func_type = module->functions[func_index]->func_type;
+ if (func_type->param_count == 1 && func_type->result_count == 0
+ && func_type->types[0] == malloc_free_io_type) {
+ bh_assert(module->free_function == (uint32)-1);
+ module->free_function = export->index;
+ LOG_VERBOSE("Found free function, name: %s, index: %u",
+ export->name, export->index);
+ }
+ }
+ }
+ }
+
+#if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
+ handle_table = wasm_interp_get_handle_table();
+#endif
+
+ for (i = 0; i < module->function_count; i++) {
+ WASMFunction *func = module->functions[i];
+ if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+
+ 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;
+ }
+ }
+
+ if (!module->possible_memory_grow) {
+ WASMMemoryImport *memory_import;
+ WASMMemory *memory;
+
+ if (aux_data_end_global && aux_heap_base_global
+ && aux_stack_top_global) {
+ uint64 init_memory_size;
+ uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
+
+ /* Only resize(shrunk) the memory size if num_bytes_per_page is in
+ * valid range of uint32 */
+ if (shrunk_memory_size <= UINT32_MAX) {
+ if (module->import_memory_count) {
+ memory_import = &module->import_memories[0].u.memory;
+ init_memory_size =
+ (uint64)memory_import->mem_type.num_bytes_per_page
+ * memory_import->mem_type.init_page_count;
+ if (shrunk_memory_size <= init_memory_size) {
+ /* Reset memory info to decrease memory usage */
+ memory_import->mem_type.num_bytes_per_page =
+ (uint32)shrunk_memory_size;
+ memory_import->mem_type.init_page_count = 1;
+ LOG_VERBOSE("Shrink import memory size to %" PRIu64,
+ shrunk_memory_size);
+ }
+ }
+
+ if (module->memory_count) {
+ memory = &module->memories[0];
+ init_memory_size = (uint64)memory->num_bytes_per_page
+ * memory->init_page_count;
+ if (shrunk_memory_size <= init_memory_size) {
+ /* Reset memory info to decrease memory usage */
+ memory->num_bytes_per_page = (uint32)shrunk_memory_size;
+ memory->init_page_count = 1;
+ LOG_VERBOSE("Shrink memory size to %" PRIu64,
+ shrunk_memory_size);
+ }
+ }
+ }
+ }
+
+#if WASM_ENABLE_MULTI_MODULE == 0
+ if (module->import_memory_count) {
+ memory_import = &module->import_memories[0].u.memory;
+ /* Only resize the memory to one big page if num_bytes_per_page is
+ * in valid range of uint32 */
+ if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
+ memory_import->mem_type.num_bytes_per_page *=
+ memory_import->mem_type.init_page_count;
+
+ if (memory_import->mem_type.init_page_count > 0)
+ memory_import->mem_type.init_page_count =
+ memory_import->mem_type.max_page_count = 1;
+ else
+ memory_import->mem_type.init_page_count =
+ memory_import->mem_type.max_page_count = 0;
+ }
+ }
+ if (module->memory_count) {
+ memory = &module->memories[0];
+ /* Only resize(shrunk) the memory size if num_bytes_per_page is in
+ * valid range of uint32 */
+ if (memory->init_page_count < DEFAULT_MAX_PAGES) {
+ memory->num_bytes_per_page *= memory->init_page_count;
+ 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;
+ }
+ }
+#endif
+ }
+
+#if WASM_ENABLE_MEMORY64 != 0
+ if (!check_memory64_flags_consistency(module, error_buf, error_buf_size,
+ false))
+ return false;
+#endif
+
+ calculate_global_data_offset(module);
+
+#if WASM_ENABLE_FAST_JIT != 0
+ 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)) {
+ return false;
+ }
+#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");
+ return false;
+ }
+#endif
+#endif
+
+#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)) {
+ return false;
+ }
+#endif
+
+#if WASM_ENABLE_MEMORY_TRACING != 0
+ wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
+#endif
+ return true;
+}
+
+static WASMModule *
+create_module(char *name, char *error_buf, uint32 error_buf_size)
+{
+ WASMModule *module =
+ loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
+ bh_list_status ret;
+
+ if (!module) {
+ return NULL;
+ }
+
+ module->module_type = Wasm_Module_Bytecode;
+
+ /* Set start_function to -1, means no start function */
+ module->start_function = (uint32)-1;
+
+ module->name = name;
+ module->is_binary_freeable = false;
+
+#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
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+ module->import_module_list = &module->import_module_list_head;
+ ret = bh_list_init(module->import_module_list);
+ bh_assert(ret == BH_LIST_SUCCESS);
+#endif
+
+#if WASM_ENABLE_DEBUG_INTERP != 0
+ ret = bh_list_init(&module->fast_opcode_list);
+ bh_assert(ret == BH_LIST_SUCCESS);
+#endif
+
+#if WASM_ENABLE_GC != 0
+ if (!(module->ref_type_set =
+ wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
+ set_error_buf(error_buf, error_buf_size, "create reftype map failed");
+ goto fail1;
+ }
+
+ if (os_mutex_init(&module->rtt_type_lock)) {
+ set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
+ goto fail2;
+ }
+#endif
+
+#if WASM_ENABLE_DEBUG_INTERP != 0 \
+ || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
+ && 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");
+ goto fail3;
+ }
+#endif
+
+ (void)ret;
+ return module;
+
+#if WASM_ENABLE_DEBUG_INTERP != 0 \
+ || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
+ && WASM_ENABLE_LAZY_JIT != 0)
+fail3:
+#endif
+#if WASM_ENABLE_GC != 0
+ os_mutex_destroy(&module->rtt_type_lock);
+fail2:
+ bh_hash_map_destroy(module->ref_type_set);
+fail1:
+#endif
+ wasm_runtime_free(module);
+ return NULL;
+}
+
+#if WASM_ENABLE_DEBUG_INTERP != 0
+static bool
+record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
+ uint32 error_buf_size)
+{
+ WASMFastOPCodeNode *fast_op =
+ loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
+ 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);
+ }
+ return fast_op ? true : false;
+}
+#endif
+
+WASMModule *
+wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
+ uint32 error_buf_size)
+{
+ WASMModule *module = create_module("", error_buf, error_buf_size);
+ if (!module)
+ return NULL;
+
+ if (!load_from_sections(module, section_list, false, true, false, error_buf,
+ error_buf_size)) {
+ 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;
+ wasm_runtime_free(section);
+ section = next;
+ }
+}
+
+/* clang-format off */
+static uint8 section_ids[] = {
+ SECTION_TYPE_USER,
+ SECTION_TYPE_TYPE,
+ SECTION_TYPE_IMPORT,
+ SECTION_TYPE_FUNC,
+ SECTION_TYPE_TABLE,
+ SECTION_TYPE_MEMORY,
+#if WASM_ENABLE_TAGS != 0
+ SECTION_TYPE_TAG,
+#endif
+#if WASM_ENABLE_STRINGREF != 0
+ /* must immediately precede the global section,
+ or where the global section would be */
+ SECTION_TYPE_STRINGREF,
+#endif
+ 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
+};
+/* clang-format on */
+
+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;
+}
+
+static bool
+create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
+ char *error_buf, uint32 error_buf_size)
+{
+ WASMSection *section_list_end = NULL, *section;
+ const uint8 *p = buf, *p_end = buf + size;
+ uint8 section_type, section_index, last_section_index = (uint8)-1;
+ uint32 section_size;
+
+ bh_assert(!*p_section_list);
+
+ p += 8;
+ while (p < p_end) {
+ CHECK_BUF(p, p_end, 1);
+ section_type = read_uint8(p);
+ section_index = get_section_index(section_type);
+ if (section_index != (uint8)-1) {
+ 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. */
+ if (last_section_index != (uint8)-1
+ && (section_index <= last_section_index)) {
+ set_error_buf(error_buf, error_buf_size,
+ "unexpected content after last section or "
+ "junk after last section");
+ return false;
+ }
+ last_section_index = section_index;
+ }
+ read_leb_uint32(p, p_end, section_size);
+ CHECK_BUF1(p, p_end, section_size);
+
+ if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
+ error_buf_size))) {
+ return false;
+ }
+
+ section->section_type = section_type;
+ section->section_body = (uint8 *)p;
+ section->section_body_size = section_size;
+
+ if (!section_list_end)
+ *p_section_list = section_list_end = section;
+ else {
+ section_list_end->next = section;
+ section_list_end = section;
+ }
+
+ p += section_size;
+ }
+ else {
+ set_error_buf(error_buf, error_buf_size, "invalid section id");
+ return false;
+ }
+ }
+
+ return true;
+fail:
+ return false;
+}
+
+static void
+exchange32(uint8 *p_data)
+{
+ 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;
+}
+
+static union {
+ int a;
+ char b;
+} __ue = { .a = 1 };
+
+#define is_little_endian() (__ue.b == 1)
+
+static bool
+load(const uint8 *buf, uint32 size, WASMModule *module,
+ bool wasm_binary_freeable, bool no_resolve, char *error_buf,
+ uint32 error_buf_size)
+{
+ const uint8 *buf_end = buf + size;
+ const uint8 *p = buf, *p_end = buf_end;
+ uint32 magic_number, version;
+ WASMSection *section_list = NULL;
+
+ CHECK_BUF1(p, p_end, sizeof(uint32));
+ magic_number = read_uint32(p);
+ if (!is_little_endian())
+ exchange32((uint8 *)&magic_number);
+
+ if (magic_number != WASM_MAGIC_NUMBER) {
+ set_error_buf(error_buf, error_buf_size, "magic header not detected");
+ return false;
+ }
+
+ CHECK_BUF1(p, p_end, sizeof(uint32));
+ version = read_uint32(p);
+ if (!is_little_endian())
+ exchange32((uint8 *)&version);
+
+ if (version != WASM_CURRENT_VERSION) {
+ set_error_buf(error_buf, error_buf_size, "unknown binary version");
+ return false;
+ }
+
+ module->package_version = version;
+
+ if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
+ || !load_from_sections(module, section_list, true, wasm_binary_freeable,
+ no_resolve, error_buf, error_buf_size)) {
+ destroy_sections(section_list);
+ return false;
+ }
+
+ destroy_sections(section_list);
+ return true;
+fail:
+ return false;
+}
+
+#if WASM_ENABLE_LIBC_WASI != 0
+/**
+ * refer to
+ * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
+ */
+static bool
+check_wasi_abi_compatibility(const WASMModule *module,
+#if WASM_ENABLE_MULTI_MODULE != 0
+ bool main_module,
+#endif
+ char *error_buf, uint32 error_buf_size)
+{
+ /**
+ * be careful with:
+ * wasi compatible modules(command/reactor) which don't import any wasi
+ * APIs. Usually, a command has to import a "prox_exit" at least, but a
+ * 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)`
+ *
+ */
+
+ /* 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 |
+ */
+ /* clang-format on */
+
+ WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
+ uint32 import_function_count = module->import_function_count;
+ WASMFuncType *func_type;
+
+ /* (func (export "_start") (...) */
+ start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
+ error_buf, error_buf_size);
+ if (start) {
+ if (start->index < import_function_count) {
+ set_error_buf(
+ error_buf, error_buf_size,
+ "the builtin _start function can not be an import function");
+ return false;
+ }
+
+ func_type =
+ module->functions[start->index - import_function_count]->func_type;
+ if (func_type->param_count || func_type->result_count) {
+ set_error_buf(error_buf, error_buf_size,
+ "the signature of builtin _start function is wrong");
+ return false;
+ }
+ }
+ else {
+ /* (func (export "_initialize") (...) */
+ initialize =
+ wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
+ error_buf, error_buf_size);
+
+ if (initialize) {
+ if (initialize->index < import_function_count) {
+ set_error_buf(error_buf, error_buf_size,
+ "the builtin _initialize function can not be an "
+ "import function");
+ return false;
+ }
+
+ func_type =
+ module->functions[initialize->index - import_function_count]
+ ->func_type;
+ if (func_type->param_count || func_type->result_count) {
+ set_error_buf(
+ error_buf, error_buf_size,
+ "the signature of builtin _initialize function is wrong");
+ return false;
+ }
+ }
+ }
+
+ /* filter out non-wasi compatible modules */
+ if (!module->import_wasi_api && !start && !initialize) {
+ return true;
+ }
+
+ /* should have one at least */
+ if (module->import_wasi_api && !start && !initialize) {
+ LOG_WARNING("warning: a module with WASI apis should be either "
+ "a command or a reactor");
+ }
+
+ /*
+ * there is at least one of `_start` and `_initialize` in below cases.
+ * according to the assumption, they should be all wasi compatible
+ */
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+ /* filter out commands (with `_start`) cases */
+ if (start && !main_module) {
+ set_error_buf(
+ error_buf, error_buf_size,
+ "a command (with _start function) can not be a sub-module");
+ return false;
+ }
+#endif
+
+ /*
+ * 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);
+ 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
+ ) {
+ set_error_buf(error_buf, error_buf_size,
+ "a module with WASI apis must export memory by default");
+ return false;
+ }
+
+ return true;
+}
+#endif
+
+WASMModule *
+wasm_loader_load(uint8 *buf, uint32 size,
+#if WASM_ENABLE_MULTI_MODULE != 0
+ bool main_module,
+#endif
+ const LoadArgs *args, char *error_buf, uint32 error_buf_size)
+{
+ WASMModule *module = create_module(args->name, error_buf, error_buf_size);
+ if (!module) {
+ return NULL;
+ }
+
+#if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
+ || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
+ module->load_addr = (uint8 *)buf;
+ module->load_size = size;
+#endif
+
+ if (!load(buf, size, module, args->wasm_binary_freeable, args->no_resolve,
+ error_buf, error_buf_size)) {
+ goto fail;
+ }
+
+#if WASM_ENABLE_LIBC_WASI != 0
+ /* Check the WASI application ABI */
+ if (!check_wasi_abi_compatibility(module,
+#if WASM_ENABLE_MULTI_MODULE != 0
+ main_module,
+#endif
+ error_buf, error_buf_size)) {
+ goto fail;
+ }
+#endif
+
+ LOG_VERBOSE("Load module success.\n");
+ return module;
+
+fail:
+ wasm_loader_unload(module);
+ return NULL;
+}
+
+void
+wasm_loader_unload(WASMModule *module)
+{
+ uint32 i;
+
+ if (!module)
+ return;
+
+#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
+ && WASM_ENABLE_LAZY_JIT != 0
+ 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
+ module internal data after they were freed */
+ orcjit_stop_compile_threads(module);
+#endif
+
+#if WASM_ENABLE_JIT != 0
+ 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
+
+#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
+ && WASM_ENABLE_LAZY_JIT != 0
+ if (module->tierup_wait_lock_inited) {
+ os_mutex_destroy(&module->tierup_wait_lock);
+ os_cond_destroy(&module->tierup_wait_cond);
+ }
+#endif
+
+ if (module->imports)
+ wasm_runtime_free(module->imports);
+
+ if (module->functions) {
+ for (i = 0; i < module->function_count; i++) {
+ if (module->functions[i]) {
+ if (module->functions[i]->local_offsets)
+ wasm_runtime_free(module->functions[i]->local_offsets);
+#if WASM_ENABLE_FAST_INTERP != 0
+ if (module->functions[i]->code_compiled)
+ wasm_runtime_free(module->functions[i]->code_compiled);
+ if (module->functions[i]->consts)
+ wasm_runtime_free(module->functions[i]->consts);
+#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
+ if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
+ jit_code_cache_free(
+ module->functions[i]->call_to_fast_jit_from_llvm_jit);
+ }
+#endif
+#endif
+#if WASM_ENABLE_GC != 0
+ if (module->functions[i]->local_ref_type_maps) {
+ wasm_runtime_free(
+ module->functions[i]->local_ref_type_maps);
+ }
+#endif
+ wasm_runtime_free(module->functions[i]);
+ }
+ }
+ wasm_runtime_free(module->functions);
+ }
+
+ if (module->tables) {
+#if WASM_ENABLE_GC != 0
+ for (i = 0; i < module->table_count; i++) {
+ destroy_init_expr(module, &module->tables[i].init_expr);
+ }
+#endif
+ wasm_runtime_free(module->tables);
+ }
+
+ if (module->memories)
+ wasm_runtime_free(module->memories);
+
+ if (module->globals) {
+#if WASM_ENABLE_GC != 0
+ for (i = 0; i < module->global_count; i++) {
+ destroy_init_expr(module, &module->globals[i].init_expr);
+ }
+#endif
+ wasm_runtime_free(module->globals);
+ }
+
+#if WASM_ENABLE_TAGS != 0
+ if (module->tags) {
+ for (i = 0; i < module->tag_count; i++) {
+ if (module->tags[i])
+ wasm_runtime_free(module->tags[i]);
+ }
+ wasm_runtime_free(module->tags);
+ }
+#endif
+
+ if (module->exports)
+ wasm_runtime_free(module->exports);
+
+ if (module->table_segments) {
+ for (i = 0; i < module->table_seg_count; i++) {
+ if (module->table_segments[i].init_values) {
+#if WASM_ENABLE_GC != 0
+ uint32 j;
+ for (j = 0; j < module->table_segments[i].value_count; j++) {
+ destroy_init_expr(
+ module, &module->table_segments[i].init_values[j]);
+ }
+#endif
+ wasm_runtime_free(module->table_segments[i].init_values);
+ }
+ }
+ wasm_runtime_free(module->table_segments);
+ }
+
+ if (module->data_segments) {
+ for (i = 0; i < module->data_seg_count; i++) {
+ if (module->data_segments[i]) {
+ if (module->data_segments[i]->is_data_cloned)
+ wasm_runtime_free(module->data_segments[i]->data);
+ wasm_runtime_free(module->data_segments[i]);
+ }
+ }
+ wasm_runtime_free(module->data_segments);
+ }
+
+ if (module->types) {
+ for (i = 0; i < module->type_count; i++) {
+ if (module->types[i])
+ destroy_wasm_type(module->types[i]);
+ }
+ wasm_runtime_free(module->types);
+ }
+
+ if (module->const_str_list) {
+ StringNode *node = module->const_str_list, *node_next;
+ while (node) {
+ node_next = node->next;
+ wasm_runtime_free(node);
+ node = node_next;
+ }
+ }
+
+#if WASM_ENABLE_STRINGREF != 0
+ if (module->string_literal_ptrs) {
+ wasm_runtime_free((void *)module->string_literal_ptrs);
+ }
+ if (module->string_literal_lengths) {
+ wasm_runtime_free(module->string_literal_lengths);
+ }
+#endif
+
+#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
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+ /* just release the sub module list */
+ if (module->import_module_list) {
+ WASMRegisteredModule *node =
+ bh_list_first_elem(module->import_module_list);
+ while (node) {
+ WASMRegisteredModule *next = bh_list_elem_next(node);
+ bh_list_remove(module->import_module_list, node);
+ /*
+ * unload(sub_module) will be triggered 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
+
+#if WASM_ENABLE_DEBUG_INTERP != 0
+ WASMFastOPCodeNode *fast_opcode =
+ bh_list_first_elem(&module->fast_opcode_list);
+ while (fast_opcode) {
+ WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
+ wasm_runtime_free(fast_opcode);
+ fast_opcode = next;
+ }
+#endif
+
+#if WASM_ENABLE_DEBUG_INTERP != 0 \
+ || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
+ && WASM_ENABLE_LAZY_JIT != 0)
+ os_mutex_destroy(&module->instance_list_lock);
+#endif
+
+#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
+ wasm_runtime_destroy_custom_sections(module->custom_section_list);
+#endif
+
+#if WASM_ENABLE_FAST_JIT != 0
+ if (module->fast_jit_func_ptrs) {
+ wasm_runtime_free(module->fast_jit_func_ptrs);
+ }
+
+ 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]);
+ }
+ }
+#endif
+
+#if WASM_ENABLE_GC != 0
+ os_mutex_destroy(&module->rtt_type_lock);
+ bh_hash_map_destroy(module->ref_type_set);
+ if (module->rtt_types) {
+ for (i = 0; i < module->type_count; i++) {
+ if (module->rtt_types[i])
+ wasm_runtime_free(module->rtt_types[i]);
+ }
+ wasm_runtime_free(module->rtt_types);
+ }
+#if WASM_ENABLE_STRINGREF != 0
+ for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
+ if (module->stringref_rtts[i])
+ wasm_runtime_free(module->stringref_rtts[i]);
+ }
+#endif
+#endif
+
+ wasm_runtime_free(module);
+}
+
+bool
+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,
+ uint8 **p_end_addr)
+{
+ const uint8 *p = start_addr, *p_end = code_end_addr;
+ uint8 *else_addr = NULL;
+ char error_buf[128];
+ uint32 block_nested_depth = 1, count, i, j, t;
+ uint32 error_buf_size = sizeof(error_buf);
+ uint8 opcode, u8;
+ BlockAddr block_stack[16] = { { 0 } }, *block;
+
+ i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
+ block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
+
+ 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;
+ }
+ }
+
+ /* Cache unhit */
+ block_stack[0].start_addr = start_addr;
+
+ while (p < code_end_addr) {
+ opcode = *p++;
+#if WASM_ENABLE_DEBUG_INTERP != 0
+ op_break_retry:
+#endif
+ switch (opcode) {
+ case WASM_OP_UNREACHABLE:
+ case WASM_OP_NOP:
+ break;
+
+#if WASM_ENABLE_EXCE_HANDLING != 0
+ case WASM_OP_TRY:
+ u8 = read_uint8(p);
+ if (block_nested_depth
+ < sizeof(block_stack) / sizeof(BlockAddr)) {
+ block_stack[block_nested_depth].start_addr = p;
+ block_stack[block_nested_depth].else_addr = NULL;
+ }
+ block_nested_depth++;
+ break;
+ case EXT_OP_TRY:
+ skip_leb_uint32(p, p_end);
+ if (block_nested_depth
+ < sizeof(block_stack) / sizeof(BlockAddr)) {
+ block_stack[block_nested_depth].start_addr = p;
+ block_stack[block_nested_depth].else_addr = NULL;
+ }
+ block_nested_depth++;
+ break;
+ case WASM_OP_CATCH:
+ if (block_nested_depth == 1) {
+ *p_end_addr = (uint8 *)(p - 1);
+ /* stop search and return the address of the catch block */
+ return true;
+ }
+ break;
+ case WASM_OP_CATCH_ALL:
+ if (block_nested_depth == 1) {
+ *p_end_addr = (uint8 *)(p - 1);
+ /* stop search and return the address of the catch_all block
+ */
+ return true;
+ }
+ break;
+ case WASM_OP_THROW:
+ /* skip tag_index */
+ skip_leb(p);
+ break;
+ case WASM_OP_RETHROW:
+ /* skip depth */
+ skip_leb(p);
+ break;
+ case WASM_OP_DELEGATE:
+ if (block_nested_depth == 1) {
+ *p_end_addr = (uint8 *)(p - 1);
+ return true;
+ }
+ else {
+ skip_leb(p);
+ /* the DELEGATE opcode ends the tryblock, */
+ block_nested_depth--;
+ if (block_nested_depth
+ < sizeof(block_stack) / sizeof(BlockAddr))
+ block_stack[block_nested_depth].end_addr =
+ (uint8 *)(p - 1);
+ }
+ break;
+#endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
+
+ case WASM_OP_BLOCK:
+ case WASM_OP_LOOP:
+ case WASM_OP_IF:
+ {
+ /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
+ u8 = read_uint8(p);
+ if (is_byte_a_type(u8)) {
+#if WASM_ENABLE_GC != 0
+ if (wasm_is_type_multi_byte_type(u8)) {
+ /* the possible extra bytes of GC ref type have been
+ modified to OP_NOP, no need to resolve them again */
+ }
+#endif
+ }
+ else {
+ p--;
+ /* block type */
+ skip_leb_int32(p, p_end);
+ }
+ if (block_nested_depth
+ < sizeof(block_stack) / sizeof(BlockAddr)) {
+ block_stack[block_nested_depth].start_addr = p;
+ block_stack[block_nested_depth].else_addr = NULL;
+ }
+ block_nested_depth++;
+ break;
+ }
+
+ case EXT_OP_BLOCK:
+ case EXT_OP_LOOP:
+ case EXT_OP_IF:
+ /* block type */
+ skip_leb_int32(p, p_end);
+ if (block_nested_depth
+ < sizeof(block_stack) / sizeof(BlockAddr)) {
+ block_stack[block_nested_depth].start_addr = p;
+ block_stack[block_nested_depth].else_addr = NULL;
+ }
+ block_nested_depth++;
+ break;
+
+ case WASM_OP_ELSE:
+ if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
+ 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);
+ break;
+
+ case WASM_OP_END:
+ if (block_nested_depth == 1) {
+ if (label_type == LABEL_TYPE_IF)
+ *p_else_addr = else_addr;
+ *p_end_addr = (uint8 *)(p - 1);
+
+ block_stack[0].end_addr = (uint8 *)(p - 1);
+ for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
+ t++) {
+ start_addr = block_stack[t].start_addr;
+ if (start_addr) {
+ i = ((uintptr_t)start_addr)
+ & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
+ block =
+ block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
+ for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
+ if (!block[j].start_addr)
+ break;
+
+ if (j == BLOCK_ADDR_CONFLICT_SIZE) {
+ memmove(block + 1, block,
+ (BLOCK_ADDR_CONFLICT_SIZE - 1)
+ * sizeof(BlockAddr));
+ 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;
+ }
+ return true;
+ }
+ else {
+ block_nested_depth--;
+ if (block_nested_depth
+ < sizeof(block_stack) / sizeof(BlockAddr))
+ block_stack[block_nested_depth].end_addr =
+ (uint8 *)(p - 1);
+ }
+ break;
+
+ case WASM_OP_BR:
+ case WASM_OP_BR_IF:
+ skip_leb_uint32(p, p_end); /* labelidx */
+ break;
+
+ case WASM_OP_BR_TABLE:
+ read_leb_uint32(p, p_end, count); /* label num */
+#if WASM_ENABLE_FAST_INTERP != 0
+ for (i = 0; i <= count; i++) /* labelidxs */
+ skip_leb_uint32(p, p_end);
+#else
+ p += count + 1;
+ while (*p == WASM_OP_NOP)
+ p++;
+#endif
+ break;
+
+#if WASM_ENABLE_FAST_INTERP == 0
+ case EXT_OP_BR_TABLE_CACHE:
+ read_leb_uint32(p, p_end, count); /* label num */
+ while (*p == WASM_OP_NOP)
+ p++;
+ break;
+#endif
+
+ case WASM_OP_RETURN:
+ break;
+
+ case WASM_OP_CALL:
+#if WASM_ENABLE_TAIL_CALL != 0
+ case WASM_OP_RETURN_CALL:
+#endif
+ skip_leb_uint32(p, p_end); /* funcidx */
+ break;
+
+ case WASM_OP_CALL_INDIRECT:
+#if WASM_ENABLE_TAIL_CALL != 0
+ case WASM_OP_RETURN_CALL_INDIRECT:
+#endif
+ skip_leb_uint32(p, p_end); /* typeidx */
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+ skip_leb_uint32(p, p_end); /* tableidx */
+#else
+ u8 = read_uint8(p); /* 0x00 */
+#endif
+ break;
+
+#if WASM_ENABLE_GC != 0
+ case WASM_OP_CALL_REF:
+ case WASM_OP_RETURN_CALL_REF:
+ skip_leb_uint32(p, p_end); /* typeidx */
+ break;
+#endif
+
+ case WASM_OP_DROP:
+ case WASM_OP_SELECT:
+ case WASM_OP_DROP_64:
+ case WASM_OP_SELECT_64:
+ break;
+
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+ case WASM_OP_SELECT_T:
+ {
+ skip_leb_uint32(p, p_end); /* vec length */
+ u8 = read_uint8(p); /* typeidx */
+ /* the possible extra bytes of GC ref type have been
+ modified to OP_NOP, no need to resolve them again */
+ 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:
+ {
+ u8 = read_uint8(p); /* type */
+ if (is_byte_a_type(u8)) {
+#if WASM_ENABLE_GC != 0
+ if (wasm_is_type_multi_byte_type(u8)) {
+ /* the possible extra bytes of GC ref type have been
+ modified to OP_NOP, no need to resolve them again */
+ }
+#endif
+ }
+ else {
+ p--;
+ skip_leb_uint32(p, p_end);
+ }
+ break;
+ }
+ case WASM_OP_REF_IS_NULL:
+ break;
+ case WASM_OP_REF_FUNC:
+ skip_leb_uint32(p, p_end); /* func index */
+ break;
+#endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
+
+#if WASM_ENABLE_GC != 0
+ case WASM_OP_REF_AS_NON_NULL:
+ case WASM_OP_REF_EQ:
+ break;
+ case WASM_OP_BR_ON_NULL:
+ case WASM_OP_BR_ON_NON_NULL:
+ skip_leb_uint32(p, p_end); /* label index */
+ break;
+#endif /* end of WASM_ENABLE_GC != 0 */
+
+ 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:
+ case WASM_OP_GET_GLOBAL_64:
+ case WASM_OP_SET_GLOBAL_64:
+ case WASM_OP_SET_GLOBAL_AUX_STACK:
+ skip_leb_uint32(p, p_end); /* local index */
+ break;
+
+ case EXT_OP_GET_LOCAL_FAST:
+ case EXT_OP_SET_LOCAL_FAST:
+ case EXT_OP_TEE_LOCAL_FAST:
+ CHECK_BUF(p, p_end, 1);
+ p++;
+ break;
+
+ 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:
+ skip_leb_align(p, p_end); /* align */
+ skip_leb_mem_offset(p, p_end); /* offset */
+ break;
+
+ case WASM_OP_MEMORY_SIZE:
+ case WASM_OP_MEMORY_GROW:
+ skip_leb_memidx(p, p_end); /* memidx */
+ break;
+
+ case WASM_OP_I32_CONST:
+ skip_leb_int32(p, p_end);
+ break;
+ case WASM_OP_I64_CONST:
+ skip_leb_int64(p, p_end);
+ 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:
+ 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;
+
+#if WASM_ENABLE_GC != 0
+ case WASM_OP_GC_PREFIX:
+ {
+ uint32 opcode1;
+
+ read_leb_uint32(p, p_end, opcode1);
+ /* opcode1 was checked in wasm_loader_prepare_bytecode and
+ is no larger than UINT8_MAX */
+ opcode = (uint8)opcode1;
+
+ switch (opcode) {
+ case WASM_OP_STRUCT_NEW:
+ case WASM_OP_STRUCT_NEW_DEFAULT:
+ skip_leb_uint32(p, p_end); /* typeidx */
+ break;
+ case WASM_OP_STRUCT_GET:
+ case WASM_OP_STRUCT_GET_S:
+ case WASM_OP_STRUCT_GET_U:
+ case WASM_OP_STRUCT_SET:
+ skip_leb_uint32(p, p_end); /* typeidx */
+ skip_leb_uint32(p, p_end); /* fieldidx */
+ break;
+
+ case WASM_OP_ARRAY_NEW:
+ case WASM_OP_ARRAY_NEW_DEFAULT:
+ case WASM_OP_ARRAY_GET:
+ case WASM_OP_ARRAY_GET_S:
+ case WASM_OP_ARRAY_GET_U:
+ case WASM_OP_ARRAY_SET:
+ case WASM_OP_ARRAY_FILL:
+ skip_leb_uint32(p, p_end); /* typeidx */
+ break;
+ case WASM_OP_ARRAY_COPY:
+ skip_leb_uint32(p, p_end); /* typeidx1 */
+ skip_leb_uint32(p, p_end); /* typeidx2 */
+ break;
+ case WASM_OP_ARRAY_LEN:
+ break;
+ case WASM_OP_ARRAY_NEW_FIXED:
+ case WASM_OP_ARRAY_NEW_DATA:
+ case WASM_OP_ARRAY_NEW_ELEM:
+ skip_leb_uint32(p, p_end); /* typeidx */
+ skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
+ break;
+
+ case WASM_OP_REF_I31:
+ case WASM_OP_I31_GET_S:
+ case WASM_OP_I31_GET_U:
+ break;
+
+ case WASM_OP_REF_TEST:
+ case WASM_OP_REF_CAST:
+ case WASM_OP_REF_TEST_NULLABLE:
+ case WASM_OP_REF_CAST_NULLABLE:
+ skip_leb_int32(p, p_end); /* heaptype */
+ break;
+ case WASM_OP_BR_ON_CAST:
+ case WASM_OP_BR_ON_CAST_FAIL:
+ p += sizeof(uint8); /* castflag */
+ skip_leb_uint32(p, p_end); /* labelidx */
+ skip_leb_int32(p, p_end); /* heaptype */
+ skip_leb_int32(p, p_end); /* heaptype2 */
+ break;
+
+ case WASM_OP_ANY_CONVERT_EXTERN:
+ case WASM_OP_EXTERN_CONVERT_ANY:
+ break;
+
+#if WASM_ENABLE_STRINGREF != 0
+ case WASM_OP_STRING_NEW_UTF8:
+ case WASM_OP_STRING_NEW_WTF16:
+ case WASM_OP_STRING_NEW_LOSSY_UTF8:
+ case WASM_OP_STRING_NEW_WTF8:
+ skip_leb_uint32(p, p_end); /* memory index 0x00 */
+ break;
+ case WASM_OP_STRING_CONST:
+ skip_leb_int32(p, p_end); /* contents */
+ break;
+ case WASM_OP_STRING_MEASURE_UTF8:
+ case WASM_OP_STRING_MEASURE_WTF8:
+ case WASM_OP_STRING_MEASURE_WTF16:
+ break;
+ case WASM_OP_STRING_ENCODE_UTF8:
+ case WASM_OP_STRING_ENCODE_WTF16:
+ case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
+ case WASM_OP_STRING_ENCODE_WTF8:
+ skip_leb_uint32(p, p_end); /* memory index 0x00 */
+ break;
+ case WASM_OP_STRING_CONCAT:
+ case WASM_OP_STRING_EQ:
+ case WASM_OP_STRING_IS_USV_SEQUENCE:
+ case WASM_OP_STRING_AS_WTF8:
+ case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
+ break;
+ case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
+ case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
+ case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
+ skip_leb_uint32(p, p_end); /* memory index 0x00 */
+ break;
+ case WASM_OP_STRINGVIEW_WTF8_SLICE:
+ case WASM_OP_STRING_AS_WTF16:
+ case WASM_OP_STRINGVIEW_WTF16_LENGTH:
+ case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
+ break;
+ case WASM_OP_STRINGVIEW_WTF16_ENCODE:
+ skip_leb_uint32(p, p_end); /* memory index 0x00 */
+ break;
+ case WASM_OP_STRINGVIEW_WTF16_SLICE:
+ case WASM_OP_STRING_AS_ITER:
+ case WASM_OP_STRINGVIEW_ITER_NEXT:
+ case WASM_OP_STRINGVIEW_ITER_ADVANCE:
+ case WASM_OP_STRINGVIEW_ITER_REWIND:
+ case WASM_OP_STRINGVIEW_ITER_SLICE:
+ case WASM_OP_STRING_NEW_UTF8_ARRAY:
+ case WASM_OP_STRING_NEW_WTF16_ARRAY:
+ case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
+ case WASM_OP_STRING_NEW_WTF8_ARRAY:
+ case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
+ case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
+ case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
+ case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
+ break;
+#endif /* end of WASM_ENABLE_STRINGREF != 0 */
+ default:
+ return false;
+ }
+ break;
+ }
+#endif /* end of WASM_ENABLE_GC != 0 */
+
+ case WASM_OP_MISC_PREFIX:
+ {
+ uint32 opcode1;
+
+ read_leb_uint32(p, p_end, opcode1);
+ /* opcode1 was checked in wasm_loader_prepare_bytecode and
+ is no larger than UINT8_MAX */
+ opcode = (uint8)opcode1;
+
+ switch (opcode) {
+ 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_leb_memidx(p, p_end);
+ break;
+ case WASM_OP_DATA_DROP:
+ skip_leb_uint32(p, p_end);
+ break;
+ case WASM_OP_MEMORY_COPY:
+ skip_leb_memidx(p, p_end);
+ skip_leb_memidx(p, p_end);
+ break;
+ case WASM_OP_MEMORY_FILL:
+ skip_leb_memidx(p, p_end);
+ break;
+#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 */
+ default:
+ return false;
+ }
+ break;
+ }
+
+#if WASM_ENABLE_SIMD != 0
+#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
+ case WASM_OP_SIMD_PREFIX:
+ {
+ uint32 opcode1;
+
+ read_leb_uint32(p, p_end, opcode1);
+ /* opcode1 was checked in wasm_loader_prepare_bytecode and
+ is no larger than UINT8_MAX */
+ opcode = (uint8)opcode1;
+
+ /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
+ */
+ switch (opcode) {
+ case SIMD_v128_load:
+ 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:
+ case SIMD_v128_store:
+ /* memarg align */
+ skip_leb_uint32(p, p_end);
+ /* memarg offset */
+ skip_leb_mem_offset(p, p_end);
+ break;
+
+ case SIMD_v128_const:
+ case SIMD_v8x16_shuffle:
+ /* immByte[16] immLaneId[16] */
+ 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:
+ /* ImmLaneId */
+ CHECK_BUF(p, p_end, 1);
+ p++;
+ break;
+
+ 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_mem_offset(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_mem_offset(p, p_end);
+ break;
+
+ default:
+ /*
+ * since latest SIMD specific used almost every value
+ * from 0x00 to 0xff, the default branch will present
+ * all opcodes without imm
+ * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
+ */
+ break;
+ }
+ break;
+ }
+#endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
+#endif /* end of WASM_ENABLE_SIMD */
+
+#if WASM_ENABLE_SHARED_MEMORY != 0
+ case WASM_OP_ATOMIC_PREFIX:
+ {
+ uint32 opcode1;
+
+ /* atomic_op (u32_leb) + memarg (2 u32_leb) */
+ read_leb_uint32(p, p_end, opcode1);
+ /* opcode1 was checked in wasm_loader_prepare_bytecode and
+ is no larger than UINT8_MAX */
+ opcode = (uint8)opcode1;
+
+ if (opcode != WASM_OP_ATOMIC_FENCE) {
+ skip_leb_uint32(p, p_end); /* align */
+ skip_leb_mem_offset(p, p_end); /* offset */
+ }
+ else {
+ /* atomic.fence doesn't have memarg */
+ p++;
+ }
+ break;
+ }
+#endif
+#if WASM_ENABLE_DEBUG_INTERP != 0
+ case DEBUG_OP_BREAK:
+ {
+ WASMDebugInstance *debug_instance =
+ wasm_exec_env_get_instance(exec_env);
+ char original_opcode[1];
+ uint64 size = 1;
+ WASMModuleInstance *module_inst =
+ (WASMModuleInstance *)exec_env->module_inst;
+ uint64 offset = (p - 1) >= module_inst->module->load_addr
+ ? (p - 1) - module_inst->module->load_addr
+ : ~0;
+ if (debug_instance) {
+ if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
+ original_opcode, &size)
+ && size == 1) {
+ LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
+ "with %02x: ",
+ original_opcode[0]);
+ opcode = original_opcode[0];
+ goto op_break_retry;
+ }
+ }
+ break;
+ }
+#endif
+
+ default:
+ return false;
+ }
+ }
+
+ (void)u8;
+ (void)exec_env;
+ return false;
+fail:
+ return false;
+}
+
+#if WASM_ENABLE_FAST_INTERP != 0
+
+#if WASM_DEBUG_PREPROCESSOR != 0
+#define LOG_OP(...) os_printf(__VA_ARGS__)
+#else
+#define LOG_OP(...) (void)0
+#endif
+
+#define PATCH_ELSE 0
+#define PATCH_END 1
+typedef struct BranchBlockPatch {
+ struct BranchBlockPatch *next;
+ uint8 patch_type;
+ uint8 *code_compiled;
+} BranchBlockPatch;
+#endif
+
+typedef struct BranchBlock {
+ uint8 label_type;
+ BlockType block_type;
+ uint8 *start_addr;
+ uint8 *else_addr;
+ uint8 *end_addr;
+ uint32 stack_cell_num;
+#if WASM_ENABLE_GC != 0
+ uint32 reftype_map_num;
+ /* Indicate which local is used inside current block, used to validate
+ * local.get with non-nullable ref types */
+ uint8 *local_use_mask;
+ uint32 local_use_mask_size;
+#endif
+#if WASM_ENABLE_FAST_INTERP != 0
+ uint16 dynamic_offset;
+ uint8 *code_compiled;
+ BranchBlockPatch *patch_list;
+ /* This is used to save params frame_offset of of if block */
+ int16 *param_frame_offsets;
+ /* This is used to recover the dynamic offset for else branch,
+ * and also to remember the start offset of dynamic space which
+ * stores the block arguments for loop block, so we can use it
+ * to copy the stack operands to the loop block's arguments in
+ * wasm_loader_emit_br_info for opcode br. */
+ uint16 start_dynamic_offset;
+#endif
+
+ /* 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;
+} BranchBlock;
+
+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;
+
+#if WASM_ENABLE_GC != 0
+ /* frame reftype map stack */
+ WASMRefTypeMap *frame_reftype_map;
+ WASMRefTypeMap *frame_reftype_map_bottom;
+ WASMRefTypeMap *frame_reftype_map_boundary;
+ uint32 frame_reftype_map_size;
+ uint32 reftype_map_num;
+ uint32 max_reftype_map_num;
+ /* Current module */
+ WASMModule *module;
+ /* Current module's ref_type_set */
+ HashMap *ref_type_set;
+ /* Always point to local variable ref_type of
+ wasm_loader_prepare_bytecode */
+ WASMRefType *ref_type_tmp;
+#endif
+
+ /* 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;
+
+ /* preserved local offset */
+ int16 preserved_local_offset;
+
+ /* const buffer */
+ uint8 *const_buf;
+ uint16 num_const;
+ uint16 const_cell_num;
+ uint32 const_buf_size;
+
+ /* processed code */
+ uint8 *p_code_compiled;
+ uint8 *p_code_compiled_end;
+ uint32 code_compiled_size;
+ /* 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;
+#endif
+} WASMLoaderContext;
+
+typedef struct Const {
+ WASMValue value;
+ uint16 slot_index;
+ uint8 value_type;
+} Const;
+
+#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)
+
+#if WASM_ENABLE_FAST_INTERP != 0
+static bool
+check_offset_push(WASMLoaderContext *ctx, char *error_buf,
+ uint32 error_buf_size)
+{
+ uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
+ 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;
+ ctx->frame_offset_boundary =
+ ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
+ ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
+ }
+ return true;
+fail:
+ return false;
+}
+
+static bool
+check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
+{
+ if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
+ return false;
+ return true;
+}
+
+static void
+free_label_patch_list(BranchBlock *frame_csp)
+{
+ BranchBlockPatch *label_patch = frame_csp->patch_list;
+ BranchBlockPatch *next;
+ while (label_patch != NULL) {
+ next = label_patch->next;
+ wasm_runtime_free(label_patch);
+ label_patch = next;
+ }
+ frame_csp->patch_list = NULL;
+}
+
+static void
+free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
+{
+ BranchBlock *tmp_csp = frame_csp;
+ uint32 i;
+
+ for (i = 0; i < csp_num; i++) {
+ free_label_patch_list(tmp_csp);
+ tmp_csp++;
+ }
+}
+
+static void
+free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
+{
+ BranchBlock *tmp_csp = frame_csp;
+ uint32 i;
+
+ for (i = 0; i < csp_num; i++) {
+ if (tmp_csp->param_frame_offsets)
+ wasm_runtime_free(tmp_csp->param_frame_offsets);
+ tmp_csp++;
+ }
+}
+#endif /* end of WASM_ENABLE_FAST_INTERP */
+
+#if WASM_ENABLE_GC != 0
+static bool
+wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
+ char *error_buf, uint32 error_buf_size)
+{
+ BranchBlock *current_csp = ctx->frame_csp - 1;
+ uint32 local_mask_size;
+
+ if (local_count == 0) {
+ current_csp->local_use_mask_size = 0;
+ return true;
+ }
+
+ /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
+ * else branch, we don't need to allocate memory again */
+ if (!current_csp->local_use_mask) {
+ local_mask_size = (local_count + 7) / sizeof(uint8);
+ if (!(current_csp->local_use_mask =
+ loader_malloc(local_mask_size, error_buf, error_buf_size))) {
+ return false;
+ }
+ current_csp->local_use_mask_size = local_mask_size;
+ }
+ else {
+ local_mask_size = current_csp->local_use_mask_size;
+ bh_assert(current_csp->label_type == LABEL_TYPE_IF);
+ }
+
+ if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
+ /* For non-function blocks, inherit the use status from parent block */
+ BranchBlock *parent_csp = current_csp - 1;
+
+ bh_assert(parent_csp >= ctx->frame_csp_bottom);
+ bh_assert(parent_csp->local_use_mask);
+
+ bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
+ parent_csp->local_use_mask, local_mask_size);
+ }
+
+ return true;
+}
+
+static void
+wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
+{
+ BranchBlock *current_csp = ctx->frame_csp - 1;
+
+ bh_assert(current_csp->local_use_mask
+ || current_csp->local_use_mask_size == 0);
+
+ if (current_csp->local_use_mask) {
+ wasm_runtime_free(current_csp->local_use_mask);
+ }
+
+ current_csp->local_use_mask = NULL;
+ current_csp->local_use_mask_size = 0;
+}
+
+static void
+wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
+{
+ BranchBlock *tmp_csp = ctx->frame_csp_bottom;
+ uint32 i;
+
+ for (i = 0; i < ctx->csp_num; i++) {
+ if (tmp_csp->local_use_mask) {
+ wasm_runtime_free(tmp_csp->local_use_mask);
+ tmp_csp->local_use_mask = NULL;
+ tmp_csp->local_use_mask_size = 0;
+ }
+ tmp_csp++;
+ }
+}
+
+static void
+wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
+{
+ BranchBlock *current_csp = ctx->frame_csp - 1;
+ uint32 byte_offset = index / sizeof(uint8);
+ uint32 bit_offset = index % sizeof(uint8);
+
+ bh_assert(byte_offset < current_csp->local_use_mask_size);
+ bh_assert(current_csp->local_use_mask);
+
+ current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
+}
+
+static bool
+wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
+{
+ BranchBlock *current_csp = ctx->frame_csp - 1;
+ uint32 byte_offset = index / sizeof(uint8);
+ uint32 bit_offset = index % sizeof(uint8);
+
+ bh_assert(byte_offset < current_csp->local_use_mask_size);
+ bh_assert(current_csp->local_use_mask);
+
+ return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
+ ? true
+ : false;
+}
+#endif /* end of WASM_ENABLE_GC != 0 */
+
+static void
+wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
+{
+ if (ctx) {
+ if (ctx->frame_ref_bottom)
+ wasm_runtime_free(ctx->frame_ref_bottom);
+#if WASM_ENABLE_GC != 0
+ if (ctx->frame_reftype_map_bottom)
+ wasm_runtime_free(ctx->frame_reftype_map_bottom);
+#endif
+ if (ctx->frame_csp_bottom) {
+#if WASM_ENABLE_FAST_INTERP != 0
+ free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
+ free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
+ ctx->csp_num);
+#endif
+#if WASM_ENABLE_GC != 0
+ wasm_loader_clean_all_local_use_masks(ctx);
+#endif
+ wasm_runtime_free(ctx->frame_csp_bottom);
+ }
+#if WASM_ENABLE_FAST_INTERP != 0
+ if (ctx->frame_offset_bottom)
+ wasm_runtime_free(ctx->frame_offset_bottom);
+ if (ctx->const_buf)
+ wasm_runtime_free(ctx->const_buf);
+#endif
+ wasm_runtime_free(ctx);
+ }
+}
+
+static WASMLoaderContext *
+wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
+{
+ WASMLoaderContext *loader_ctx =
+ loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
+ if (!loader_ctx)
+ return NULL;
+
+ loader_ctx->frame_ref_size = 32;
+ if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
+ loader_ctx->frame_ref_size, error_buf, error_buf_size)))
+ goto fail;
+ loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
+
+#if WASM_ENABLE_GC != 0
+ loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
+ if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
+ loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
+ error_buf_size)))
+ goto fail;
+ loader_ctx->frame_reftype_map_boundary =
+ loader_ctx->frame_reftype_map_bottom + 16;
+#endif
+
+ loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
+ if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
+ loader_ctx->frame_csp_size, error_buf, error_buf_size)))
+ goto fail;
+ loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
+
+#if WASM_ENABLE_EXCE_HANDLING != 0
+ func->exception_handler_count = 0;
+#endif
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ loader_ctx->frame_offset_size = sizeof(int16) * 32;
+ if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
+ loader_malloc(loader_ctx->frame_offset_size, error_buf,
+ error_buf_size)))
+ 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;
+ if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
+ error_buf, error_buf_size)))
+ goto fail;
+
+ 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;
+ }
+
+ loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
+ loader_ctx->max_dynamic_offset =
+ func->param_cell_num + func->local_cell_num;
+#endif
+ return loader_ctx;
+
+fail:
+ wasm_loader_ctx_destroy(loader_ctx);
+ return NULL;
+}
+
+static bool
+check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
+ uint32 error_buf_size)
+{
+ uint32 cell_num_needed = wasm_value_type_cell_num(type);
+
+ if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
+ /* Increase the frame ref stack */
+ 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;
+ }
+
+#if WASM_ENABLE_GC != 0
+ if (wasm_is_type_multi_byte_type(type)
+ && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
+ /* Increase the frame reftype map stack */
+ bh_assert(
+ (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
+ * sizeof(WASMRefTypeMap))
+ == ctx->frame_reftype_map_size);
+ MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
+ ctx->frame_reftype_map_size
+ + (uint32)sizeof(WASMRefTypeMap) * 8);
+ ctx->frame_reftype_map =
+ ctx->frame_reftype_map_bottom
+ + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
+ ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
+ ctx->frame_reftype_map_boundary =
+ ctx->frame_reftype_map_bottom
+ + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
+ }
+#endif
+ return true;
+fail:
+ return false;
+}
+
+static bool
+wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
+ uint32 error_buf_size)
+{
+ uint32 type_cell_num = wasm_value_type_cell_num(type);
+ uint32 i;
+
+ if (!check_stack_push(ctx, type, error_buf, error_buf_size))
+ return false;
+
+#if WASM_ENABLE_GC != 0
+ if (wasm_is_type_multi_byte_type(type)) {
+ WASMRefType *ref_type;
+ if (!(ref_type =
+ reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
+ error_buf, error_buf_size))) {
+ return false;
+ }
+
+ if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
+ /* Increase the frame reftype map stack */
+ bh_assert((uint32)((ctx->frame_reftype_map
+ - ctx->frame_reftype_map_bottom)
+ * sizeof(WASMRefTypeMap))
+ == ctx->frame_reftype_map_size);
+ MEM_REALLOC(ctx->frame_reftype_map_bottom,
+ ctx->frame_reftype_map_size,
+ ctx->frame_reftype_map_size
+ + (uint32)sizeof(WASMRefTypeMap) * 8);
+ ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
+ + ctx->frame_reftype_map_size
+ / ((uint32)sizeof(WASMRefTypeMap));
+ ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
+ ctx->frame_reftype_map_boundary =
+ ctx->frame_reftype_map_bottom
+ + ctx->frame_reftype_map_size
+ / ((uint32)sizeof(WASMRefTypeMap));
+ }
+
+ ctx->frame_reftype_map->index = ctx->stack_cell_num;
+ ctx->frame_reftype_map->ref_type = ref_type;
+ ctx->frame_reftype_map++;
+ ctx->reftype_map_num++;
+ if (ctx->reftype_map_num > ctx->max_reftype_map_num)
+ ctx->max_reftype_map_num = ctx->reftype_map_num;
+ }
+#endif
+
+ for (i = 0; i < type_cell_num; i++)
+ *ctx->frame_ref++ = type;
+ ctx->stack_cell_num += type_cell_num;
+
+ if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
+ ctx->max_stack_cell_num = ctx->stack_cell_num;
+ if (ctx->max_stack_cell_num > UINT16_MAX) {
+ set_error_buf(error_buf, error_buf_size,
+ "operand stack depth limit exceeded");
+ return false;
+ }
+ }
+ return true;
+#if WASM_ENABLE_GC != 0
+fail:
+ return false;
+#endif
+}
+
+static bool
+check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
+ int32 stack_cell_num,
+#if WASM_ENABLE_GC != 0
+ WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
+#endif
+ uint8 type,
+#if WASM_ENABLE_GC != 0
+ WASMRefType *ref_type,
+#endif
+ char *error_buf, uint32 error_buf_size)
+{
+ int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
+#if WASM_ENABLE_GC != 0
+ WASMRefType *frame_reftype = NULL;
+#endif
+
+ if (stack_cell_num < type_cell_num) {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch: expect data but stack was empty");
+ return false;
+ }
+
+#if WASM_ENABLE_GC == 0
+ for (i = 0; i < type_cell_num; i++) {
+ if (*(frame_ref - 1 - i) != type) {
+ set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
+ "type mismatch: expect ", type2str(type),
+ " but got other");
+ return false;
+ }
+ }
+#else
+ if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
+ bh_assert(reftype_map_num > 0);
+ frame_reftype = (frame_reftype_map - 1)->ref_type;
+ }
+ if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
+ ref_type, ctx->module->types,
+ ctx->module->type_count)) {
+ set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
+ "type mismatch: expect ", type2str(type),
+ " but got other");
+ return false;
+ }
+ for (i = 0; i < type_cell_num - 1; i++) {
+ if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
+ set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
+ "type mismatch: expect ", type2str(type),
+ " but got other");
+ return false;
+ }
+ }
+#endif
+
+ return true;
+}
+
+static bool
+check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
+ uint32 error_buf_size)
+{
+ int32 block_stack_cell_num =
+ (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
+#if WASM_ENABLE_GC != 0
+ int32 reftype_map_num =
+ (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
+#endif
+
+ if (block_stack_cell_num > 0) {
+ if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
+ /* the stack top is a value of any type, return success */
+ return true;
+ }
+
+#if WASM_ENABLE_GC != 0
+ if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
+ uint8 stack_top_type = *(ctx->frame_ref - 1);
+ WASMRefType *stack_top_ref_type = NULL;
+
+ if (wasm_is_type_multi_byte_type(stack_top_type)) {
+ bh_assert(reftype_map_num > 0);
+ stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
+ }
+
+ if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
+ ctx->ref_type_tmp, ctx->module->types,
+ ctx->module->type_count)) {
+ if (wasm_is_type_multi_byte_type(stack_top_type)) {
+ uint32 ref_type_struct_size =
+ wasm_reftype_struct_size(stack_top_ref_type);
+ bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
+ stack_top_ref_type, ref_type_struct_size);
+ }
+ return true;
+ }
+ }
+#endif
+
+ if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
+#if WASM_ENABLE_GC != 0
+ ctx->frame_reftype_map, reftype_map_num,
+#endif
+ type,
+#if WASM_ENABLE_GC != 0
+ ctx->ref_type_tmp,
+#endif
+ error_buf, error_buf_size)) {
+ return false;
+ }
+
+ return true;
+}
+
+static bool
+wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
+ uint32 error_buf_size)
+{
+ BranchBlock *cur_block = ctx->frame_csp - 1;
+ int32 available_stack_cell =
+ (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
+ uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
+
+ /* 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;
+
+ if (type == VALUE_TYPE_VOID)
+ return true;
+
+ if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
+ return false;
+
+ bh_assert(available_stack_cell > 0);
+ if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
+ type = VALUE_TYPE_ANY;
+ cell_num_to_pop = 1;
+ }
+
+ ctx->frame_ref -= cell_num_to_pop;
+ ctx->stack_cell_num -= cell_num_to_pop;
+#if WASM_ENABLE_GC != 0
+ if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
+ ctx->frame_reftype_map--;
+ ctx->reftype_map_num--;
+ }
+#endif
+
+ return true;
+}
+
+#if WASM_ENABLE_GC != 0
+/* Get the stack top element of current block */
+static bool
+wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
+ WASMRefType **p_ref_type, char *error_buf,
+ uint32 error_buf_size)
+{
+ BranchBlock *cur_block = ctx->frame_csp - 1;
+ int32 available_stack_cell =
+ (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
+
+ if (available_stack_cell <= 0) {
+ /* Directly return success if current block is in stack
+ polymorphic state while stack is empty. */
+ if (cur_block->is_stack_polymorphic) {
+ *p_type = VALUE_TYPE_ANY;
+ return true;
+ }
+ else {
+ set_error_buf(
+ error_buf, error_buf_size,
+ "type mismatch: expect data but block stack was empty");
+ return false;
+ }
+ }
+
+ *p_type = *(ctx->frame_ref - 1);
+ if (wasm_is_type_multi_byte_type(*p_type)) {
+ int32 available_reftype_map =
+ (int32)(ctx->reftype_map_num
+ - (ctx->frame_csp - 1)->reftype_map_num);
+ bh_assert(available_reftype_map > 0);
+ (void)available_reftype_map;
+ *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
+ }
+
+ return true;
+}
+
+#if WASM_ENABLE_FAST_INTERP != 0
+static bool
+wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
+ char *error_buf, uint32 error_buf_size);
+#endif
+
+/* Check whether the stack top elem is a heap object, and if yes,
+ pop and return it */
+static bool
+wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
+ WASMRefType *ref_ht_ret, char *error_buf,
+ uint32 error_buf_size)
+{
+ uint8 type = 0;
+ WASMRefType *ref_type = NULL;
+
+ /* Get stack top element */
+ if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+
+ if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
+ /* stack top isn't a ref type */
+ && !wasm_is_type_reftype(type)) {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch: expect heap object but got others");
+ return false;
+ }
+
+ /* POP stack top */
+ if (wasm_is_type_multi_byte_type(type)) {
+ bh_assert(ref_type);
+ bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
+ wasm_reftype_struct_size(ref_type));
+ }
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+#else
+ if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
+ return false;
+ }
+#endif
+
+ if (p_type)
+ *p_type = type;
+ if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
+ bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
+ wasm_reftype_struct_size(ref_type));
+ }
+ return true;
+}
+
+/* Check whether the stack top elem is subtype of (ref null ht),
+ and if yes, pop it and return the converted (ref ht) */
+static bool
+wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
+ WASMRefType *ref_ht_ret, char *error_buf,
+ uint32 error_buf_size)
+{
+ uint8 type = 0;
+ WASMRefType ref_type = { 0 };
+
+ if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+
+ /* Convert to related (ref ht) and return */
+ if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
+ /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
+ */
+ wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
+ HEAP_TYPE_ARRAY
+ + (type - REF_TYPE_ARRAYREF));
+ type = ref_ht_ret->ref_type;
+ }
+ else if (wasm_is_reftype_htref_nullable(type)
+ || wasm_is_reftype_htref_non_nullable(type)) {
+ bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
+ wasm_reftype_struct_size(&ref_type));
+ /* Convert to (ref ht) */
+ ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
+ ref_ht_ret->ref_ht_common.nullable = false;
+ type = ref_ht_ret->ref_type;
+ }
+ *p_type = type;
+
+ return true;
+}
+
+/* Check whether the stack top elem is (ref null $t) or (ref $t),
+ and if yes, pop it and return the type_idx */
+static bool
+wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
+ uint32 *p_type_idx, char *error_buf,
+ uint32 error_buf_size)
+{
+ uint8 type = 0;
+ int32 type_idx = -1;
+ WASMRefType *ref_type = NULL;
+
+ /* Get stack top element */
+ if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+
+ if (type != VALUE_TYPE_ANY) {
+ /* stack top isn't (ref null $t) */
+ if (!((wasm_is_reftype_htref_nullable(type)
+ || wasm_is_reftype_htref_non_nullable(type))
+ && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch: expect (ref null $t) but got others");
+ return false;
+ }
+ type_idx = ref_type->ref_ht_typeidx.type_idx;
+
+ bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
+ wasm_reftype_struct_size(ref_type));
+ }
+
+ /* POP stack top */
+#if WASM_ENABLE_FAST_INTERP != 0
+ if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
+ error_buf_size)) {
+ return false;
+ }
+#else
+ if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
+ return false;
+ }
+#endif
+
+ /* Convert to type_idx and return */
+ *p_type = type;
+ if (type != VALUE_TYPE_ANY)
+ *p_type_idx = (uint32)type_idx;
+ return true;
+}
+#endif /* WASM_ENABLE_GC != 0 */
+
+#if WASM_ENABLE_FAST_INTERP == 0
+static bool
+wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
+ uint8 type_push, uint8 type_pop, char *error_buf,
+ uint32 error_buf_size)
+{
+ for (int i = 0; i < pop_cnt; i++) {
+ if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
+ error_buf_size))
+ return false;
+ }
+ if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
+ return false;
+ return true;
+}
+#endif
+
+static bool
+wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
+ BlockType block_type, uint8 *start_addr,
+ char *error_buf, uint32 error_buf_size)
+{
+ CHECK_CSP_PUSH();
+ memset(ctx->frame_csp, 0, sizeof(BranchBlock));
+ ctx->frame_csp->label_type = label_type;
+ ctx->frame_csp->block_type = block_type;
+ ctx->frame_csp->start_addr = start_addr;
+ ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
+#if WASM_ENABLE_GC != 0
+ ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
+#endif
+#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++;
+ if (ctx->csp_num > ctx->max_csp_num) {
+ ctx->max_csp_num = ctx->csp_num;
+ if (ctx->max_csp_num > UINT16_MAX) {
+ set_error_buf(error_buf, error_buf_size,
+ "label stack depth limit exceeded");
+ return false;
+ }
+ }
+ return true;
+fail:
+ return false;
+}
+
+static bool
+wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
+ uint32 error_buf_size)
+{
+ CHECK_CSP_POP();
+#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
+ ctx->frame_csp--;
+ ctx->csp_num--;
+
+ return true;
+fail:
+ return false;
+}
+
+#if WASM_ENABLE_FAST_INTERP != 0
+
+#if WASM_ENABLE_LABELS_AS_VALUES != 0
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
+#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)
+#else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
+#if UINTPTR_MAX == UINT64_MAX
+#define emit_label(opcode) \
+ do { \
+ int32 offset = \
+ (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
+ /* emit int32 relative offset in 64-bit target */ \
+ wasm_loader_emit_uint32(loader_ctx, offset); \
+ LOG_OP("\nemit_op [%02x]\t", opcode); \
+ } while (0)
+#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
+#define skip_label() \
+ do { \
+ wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
+ LOG_OP("\ndelete last op\n"); \
+ } while (0)
+#endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
+#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)
+#endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
+
+#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, is_br) \
+ do { \
+ if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, 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)
+
+#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)
+
+static bool
+wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
+{
+ if (!(ctx->p_code_compiled =
+ loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
+ return false;
+ ctx->p_code_compiled_end =
+ ctx->p_code_compiled + ctx->code_compiled_peak_size;
+
+ /* 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;
+
+#if WASM_ENABLE_GC != 0
+ /* clean up reftype map */
+ memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
+ ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
+ ctx->reftype_map_num = 0;
+#endif
+
+ /* 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;
+
+ /* init preserved local offsets */
+ ctx->preserved_local_offset = ctx->max_dynamic_offset;
+
+ /* const buf is reserved */
+ return true;
+}
+
+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;
+ }
+}
+
+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,
+ (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
+ value, size);
+ ctx->p_code_compiled += size;
+ }
+ else {
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+ bh_assert((ctx->code_compiled_size & 1) == 0);
+#endif
+ increase_compiled_code_space(ctx, size);
+ }
+}
+
+static void
+wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
+{
+ if (ctx->p_code_compiled) {
+#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);
+ ctx->p_code_compiled += sizeof(uint32);
+ }
+ else {
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+ bh_assert((ctx->code_compiled_size & 1) == 0);
+#endif
+ increase_compiled_code_space(ctx, sizeof(uint32));
+ }
+}
+
+static void
+wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
+{
+ if (ctx->p_code_compiled) {
+#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);
+ ctx->p_code_compiled += sizeof(int16);
+ }
+ else {
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+ bh_assert((ctx->code_compiled_size & 1) == 0);
+#endif
+ increase_compiled_code_space(ctx, sizeof(uint16));
+ }
+}
+
+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);
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+ ctx->p_code_compiled++;
+ bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
+#endif
+ }
+ else {
+ increase_compiled_code_space(ctx, sizeof(uint8));
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+ increase_compiled_code_space(ctx, sizeof(uint8));
+ bh_assert((ctx->code_compiled_size & 1) == 0);
+#endif
+ }
+}
+
+static void
+wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
+{
+ if (ctx->p_code_compiled) {
+#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);
+ ctx->p_code_compiled += sizeof(void *);
+ }
+ else {
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
+ bh_assert((ctx->code_compiled_size & 1) == 0);
+#endif
+ increase_compiled_code_space(ctx, sizeof(void *));
+ }
+}
+
+static void
+wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
+{
+ if (ctx->p_code_compiled) {
+ ctx->p_code_compiled -= size;
+#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
+ }
+ else {
+ ctx->code_compiled_size -= size;
+#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
+ }
+}
+
+static bool
+preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
+ uint32 local_index, uint32 local_type,
+ bool *preserved, char *error_buf,
+ uint32 error_buf_size)
+{
+ uint32 i = 0;
+ int16 preserved_offset = (int16)local_index;
+
+ *preserved = false;
+ while (i < loader_ctx->stack_cell_num) {
+ uint8 cur_type = loader_ctx->frame_ref_bottom[i];
+
+ /* move previous local into dynamic space before a set/tee_local opcode
+ */
+ if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
+ if (!(*preserved)) {
+ *preserved = true;
+ skip_label();
+ preserved_offset = loader_ctx->preserved_local_offset;
+ if (loader_ctx->p_code_compiled) {
+ bh_assert(preserved_offset != (int16)local_index);
+ }
+ if (is_32bit_type(local_type)) {
+ /* Only increase preserve offset in the second traversal */
+ if (loader_ctx->p_code_compiled)
+ loader_ctx->preserved_local_offset++;
+ emit_label(EXT_OP_COPY_STACK_TOP);
+ }
+ else {
+ if (loader_ctx->p_code_compiled)
+ loader_ctx->preserved_local_offset += 2;
+ 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;
+ }
+
+ if (is_32bit_type(cur_type))
+ i++;
+ else
+ i += 2;
+ }
+
+ (void)error_buf;
+ (void)error_buf_size;
+ return true;
+}
+
+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;
+ }
+
+ if (is_32bit_type(cur_type)) {
+ i++;
+ }
+ else {
+ i += 2;
+ }
+ }
+
+ return true;
+}
+
+static bool
+add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
+ uint8 *p_code_compiled, char *error_buf,
+ uint32 error_buf_size)
+{
+ BranchBlockPatch *patch =
+ loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
+ 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
+apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
+{
+ 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) {
+ STORE_PTR(node->code_compiled, ctx->p_code_compiled);
+ if (node_prev == NULL) {
+ frame_csp->patch_list = node_next;
+ }
+ else {
+ node_prev->next = node_next;
+ }
+ wasm_runtime_free(node);
+ }
+ else {
+ node_prev = node;
+ }
+ node = node_next;
+ }
+}
+
+static bool
+wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
+ bool is_br, char *error_buf, uint32 error_buf_size)
+{
+ /* 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;
+#if WASM_ENABLE_GC != 0
+ WASMRefTypeMap *reftype_maps;
+ uint32 reftype_map_count;
+#endif
+ 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 WASM_ENABLE_GC == 0
+ 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);
+#else
+ if (frame_csp->label_type == LABEL_TYPE_LOOP)
+ arity = block_type_get_param_types(block_type, &types, &reftype_maps,
+ &reftype_map_count);
+ else
+ arity = block_type_get_result_types(block_type, &types, &reftype_maps,
+ &reftype_map_count);
+#endif
+
+ /* 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--) {
+ cell = (uint8)wasm_value_type_cell_num(types[i]);
+ emit_byte(ctx, cell);
+ }
+ /* Part d */
+ for (i = (int32)arity - 1; i >= 0; i--) {
+ cell = (uint8)wasm_value_type_cell_num(types[i]);
+ frame_offset -= cell;
+ emit_operand(ctx, *(int16 *)(frame_offset));
+ }
+ /* Part e */
+ if (frame_csp->label_type == LABEL_TYPE_LOOP)
+ /* Use start_dynamic_offset which was set in
+ copy_params_to_dynamic_space */
+ dynamic_offset = frame_csp->start_dynamic_offset
+ + wasm_get_cell_num(types, arity);
+ else
+ dynamic_offset =
+ frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
+ if (is_br)
+ ctx->dynamic_offset = dynamic_offset;
+ for (i = (int32)arity - 1; i >= 0; i--) {
+ cell = (uint8)wasm_value_type_cell_num(types[i]);
+ dynamic_offset -= cell;
+ emit_operand(ctx, dynamic_offset);
+ }
+ }
+
+ /* Part f */
+ if (frame_csp->label_type == LABEL_TYPE_LOOP) {
+ wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
+ }
+ else {
+ if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
+ error_buf, error_buf_size))
+ return false;
+ /* label address, to be patched */
+ wasm_loader_emit_ptr(ctx, NULL);
+ }
+
+ return true;
+}
+
+static bool
+wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
+ bool disable_emit, int16 operand_offset,
+ char *error_buf, uint32 error_buf_size)
+{
+ uint32 cell_num_to_push, i;
+
+ if (type == VALUE_TYPE_VOID)
+ return true;
+
+ /* only check memory overflow in first traverse */
+ 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++;
+ if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
+ ctx->max_dynamic_offset = ctx->dynamic_offset;
+ if (ctx->max_dynamic_offset >= INT16_MAX) {
+ goto fail;
+ }
+ }
+ }
+
+ if (is_32bit_type(type))
+ return true;
+
+ cell_num_to_push = wasm_value_type_cell_num(type) - 1;
+ for (i = 0; i < cell_num_to_push; i++) {
+ if (ctx->p_code_compiled == NULL) {
+ if (!check_offset_push(ctx, error_buf, error_buf_size))
+ return false;
+ }
+
+ ctx->frame_offset++;
+ if (!disable_emit) {
+ ctx->dynamic_offset++;
+ if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
+ ctx->max_dynamic_offset = ctx->dynamic_offset;
+ if (ctx->max_dynamic_offset >= INT16_MAX)
+ goto fail;
+ }
+ }
+ }
+
+ return true;
+
+fail:
+ set_error_buf(error_buf, error_buf_size,
+ "fast interpreter offset overflow");
+ return false;
+}
+
+/* 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 */
+static bool
+wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
+ char *error_buf, uint32 error_buf_size)
+{
+ /* 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;
+ int32 available_stack_cell =
+ (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
+ uint32 cell_num_to_pop;
+
+ /* 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;
+
+ if (type == VALUE_TYPE_VOID)
+ return true;
+
+ /* Change type to ANY when the stack top is ANY, so as to avoid
+ popping unneeded offsets, e.g. if type is I64/F64, we may pop
+ two offsets */
+ if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
+ type = VALUE_TYPE_ANY;
+
+ cell_num_to_pop = wasm_value_type_cell_num(type);
+
+ /* 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, cell_num_to_pop))
+ return true;
+
+ ctx->frame_offset -= cell_num_to_pop;
+ if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
+ && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
+ ctx->dynamic_offset -= cell_num_to_pop;
+
+ emit_operand(ctx, *(ctx->frame_offset));
+
+ (void)error_buf;
+ (void)error_buf_size;
+ return true;
+}
+
+static bool
+wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
+ bool disable_emit, int16 operand_offset,
+ char *error_buf, uint32 error_buf_size)
+{
+ if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
+ error_buf, error_buf_size)))
+ return false;
+ if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
+ return false;
+
+ return true;
+}
+
+static bool
+wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
+ char *error_buf, uint32 error_buf_size)
+{
+ /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
+ if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
+ return false;
+ if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
+ return false;
+
+ return true;
+}
+
+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)
+{
+ 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))
+ return false;
+
+ if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
+ return false;
+
+ return true;
+}
+
+static bool
+wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
+ int16 *offset, char *error_buf,
+ uint32 error_buf_size)
+{
+ int8 bytes_to_increase;
+ int16 operand_offset = 0;
+ Const *c;
+
+ /* Search existing constant */
+ for (c = (Const *)ctx->const_buf;
+ (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
+ /* TODO: handle v128 type? */
+ if ((type == c->value_type)
+ && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
+ || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
+#if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
+ || (type == VALUE_TYPE_FUNCREF
+ && *(int32 *)value == c->value.i32)
+ || (type == VALUE_TYPE_EXTERNREF
+ && *(int32 *)value == c->value.i32)
+#endif
+ || (type == VALUE_TYPE_F64
+ && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
+ || (type == VALUE_TYPE_F32
+ && (0
+ == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
+ operand_offset = c->slot_index;
+ break;
+ }
+ if (is_32bit_type(c->value_type))
+ operand_offset += 1;
+ else
+ operand_offset += 2;
+ }
+
+ if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
+ /* 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;
+ }
+
+ if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
+ MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
+ 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) {
+ 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 final offset is correct */
+ 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;
+#if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
+ case VALUE_TYPE_EXTERNREF:
+ case VALUE_TYPE_FUNCREF:
+ c->value.i32 = *(int32 *)value;
+ ctx->const_cell_num++;
+ break;
+#endif
+ default:
+ break;
+ }
+ c->slot_index = operand_offset;
+ ctx->num_const++;
+ LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
+ (int64)c->value.i64);
+ }
+ /* use negative index for const */
+ operand_offset = -(operand_offset + 1);
+ *offset = operand_offset;
+ return true;
+fail:
+ return false;
+}
+
+/*
+ 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
+*/
+
+#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_PUSH_REF(Type) \
+ do { \
+ if (!wasm_loader_push_frame_ref_offset(loader_ctx, 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 TEMPLATE_POP_REF(Type) \
+ do { \
+ if (!wasm_loader_pop_frame_ref_offset(loader_ctx, 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)
+
+/* type of POPs should be the same */
+#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)
+
+#else /* WASM_ENABLE_FAST_INTERP */
+
+#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_PUSH_REF(Type) \
+ do { \
+ if (!(wasm_loader_push_frame_ref(loader_ctx, 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 TEMPLATE_POP_REF(Type) \
+ do { \
+ if (!(wasm_loader_pop_frame_ref(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(loader_ctx, 1, type_push, \
+ type_pop, error_buf, \
+ error_buf_size))) \
+ goto fail; \
+ } while (0)
+
+/* type of POPs should be the same */
+#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)
+#endif /* WASM_ENABLE_FAST_INTERP */
+
+#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 PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
+#define POP_REF(Type) TEMPLATE_POP_REF(Type)
+#define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
+#define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
+#define PUSH_TBL_ELEM_IDX() TEMPLATE_PUSH_REF(table_elem_idx_type)
+
+#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)
+#define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
+#define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
+#define POP_TBL_ELEM_IDX() TEMPLATE_POP_REF(table_elem_idx_type)
+
+#if WASM_ENABLE_FAST_INTERP != 0
+
+static bool
+reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
+ bool disable_emit, char *error_buf, uint32 error_buf_size)
+{
+ int16 operand_offset = 0;
+ BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
+ : loader_ctx->frame_csp;
+ BlockType *block_type = &block->block_type;
+ uint8 *return_types = NULL;
+#if WASM_ENABLE_GC != 0
+ WASMRefTypeMap *reftype_maps = NULL;
+ uint32 reftype_map_count;
+#endif
+ uint32 return_count = 0, value_count = 0, total_cel_num = 0;
+ int32 i = 0;
+ int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
+ *frame_offset_org = NULL;
+
+#if WASM_ENABLE_GC == 0
+ return_count = block_type_get_result_types(block_type, &return_types);
+#else
+ return_count = block_type_get_result_types(
+ block_type, &return_types, &reftype_maps, &reftype_map_count);
+#endif
+
+ /* 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) {
+ uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
+ if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
+ && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
+ /* insert op_copy before else opcode */
+ if (opcode == WASM_OP_ELSE)
+ skip_label();
+ emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
+ : EXT_OP_COPY_STACK_TOP_I64);
+ emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
+ emit_operand(loader_ctx, block->dynamic_offset);
+
+ if (opcode == WASM_OP_ELSE) {
+ *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
+ }
+ else {
+ loader_ctx->frame_offset -= cell;
+ loader_ctx->dynamic_offset = block->dynamic_offset;
+ PUSH_OFFSET_TYPE(return_types[0]);
+ wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
+ }
+ if (opcode == WASM_OP_ELSE)
+ emit_label(opcode);
+ }
+ 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 =
+ block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
+
+ /* First traversal to get the count of values needed to be copied. */
+ for (i = (int32)return_count - 1; i >= 0; i--) {
+ uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
+
+ 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;
+ uint64 size =
+ (uint64)value_count
+ * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
+
+ /* 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);
+
+ /* Second traversal to get each value's cell num, src offset and dst
+ * offset. */
+ frame_offset = frame_offset_org;
+ dynamic_offset = dynamic_offset_org;
+ for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
+ uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
+ 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);
+ }
+
+ return true;
+
+fail:
+ return false;
+}
+#endif /* WASM_ENABLE_FAST_INTERP */
+
+#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)
+
+#if WASM_ENABLE_GC == 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)
+#else
+#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; \
+ if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
+ error_buf, error_buf_size)) { \
+ goto fail; \
+ } \
+ } while (0)
+
+#define POP_CSP() \
+ do { \
+ wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
+ if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
+ goto fail; \
+ } while (0)
+#endif /* end of WASM_ENABLE_GC == 0 */
+
+#if WASM_ENABLE_GC == 0
+#define GET_LOCAL_REFTYPE() (void)0
+#else
+#define GET_LOCAL_REFTYPE() \
+ do { \
+ if (wasm_is_type_multi_byte_type(local_type)) { \
+ WASMRefType *_ref_type; \
+ if (local_idx < param_count) \
+ _ref_type = wasm_reftype_map_find( \
+ param_reftype_maps, param_reftype_map_count, local_idx); \
+ else \
+ _ref_type = wasm_reftype_map_find(local_reftype_maps, \
+ local_reftype_map_count, \
+ local_idx - param_count); \
+ bh_assert(_ref_type); \
+ bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
+ wasm_reftype_struct_size(_ref_type)); \
+ } \
+ } while (0)
+#endif /* end of WASM_ENABLE_GC == 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]; \
+ GET_LOCAL_REFTYPE(); \
+ } while (0)
+
+static bool
+check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
+{
+ if (module->memory_count == 0 && module->import_memory_count == 0) {
+ set_error_buf(error_buf, error_buf_size, "unknown memory");
+ return false;
+ }
+ return true;
+}
+
+#define CHECK_MEMORY() \
+ do { \
+ if (!check_memory(module, error_buf, error_buf_size)) \
+ goto fail; \
+ } while (0)
+
+static bool
+check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
+ uint32 error_buf_size)
+{
+ uint8 mem_access_aligns[] = {
+ 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 */
+ };
+ bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
+ 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;
+}
+
+#if WASM_ENABLE_SIMD != 0
+#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
+static bool
+check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
+ uint32 error_buf_size)
+{
+ uint8 mem_access_aligns[] = {
+ 4, /* load */
+ 3, 3, 3, 3, 3, 3, /* load and extend */
+ 0, 1, 2, 3, /* load and splat */
+ 4, /* store */
+ };
+
+ uint8 mem_access_aligns_load_lane[] = {
+ 0, 1, 2, 3, /* load lane */
+ 0, 1, 2, 3, /* store lane */
+ 2, 3 /* store zero */
+ };
+
+ 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])) {
+ set_error_buf(error_buf, error_buf_size,
+ "alignment must not be larger than natural");
+ return false;
+ }
+
+ return true;
+}
+
+static bool
+check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
+ uint32 error_buf_size)
+{
+ 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;
+
+ 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;
+ }
+ default:
+ goto fail;
+ }
+
+ return true;
+fail:
+ set_error_buf(error_buf, error_buf_size, "invalid lane index");
+ return false;
+}
+
+static bool
+check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
+{
+ 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 */
+
+#if WASM_ENABLE_SHARED_MEMORY != 0
+static bool
+check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
+ uint32 error_buf_size)
+{
+ uint8 wait_notify_aligns[] = { 2, 2, 3 };
+ 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 */
+
+static bool
+wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
+ char *error_buf, uint32 error_buf_size)
+{
+ BranchBlock *target_block, *cur_block;
+ BlockType *target_block_type;
+ uint8 type, *types = NULL, *frame_ref;
+ uint32 arity = 0;
+ int32 i, available_stack_cell;
+ uint16 cell_num;
+#if WASM_ENABLE_GC != 0
+ WASMRefTypeMap *frame_reftype_map;
+ WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
+ WASMRefType *ref_type;
+ uint32 reftype_map_count = 0;
+ int32 available_reftype_map;
+ bool is_type_multi_byte;
+#endif
+
+ uint8 *frame_ref_old = loader_ctx->frame_ref;
+ uint8 *frame_ref_after_popped = NULL;
+ uint8 frame_ref_tmp[4] = { 0 };
+ uint8 *frame_ref_buf = frame_ref_tmp;
+ uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
+#if WASM_ENABLE_GC != 0
+ WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
+ WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
+ WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
+ WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
+ uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
+#endif
+#if WASM_ENABLE_FAST_INTERP != 0
+ int16 *frame_offset_old = loader_ctx->frame_offset;
+ int16 *frame_offset_after_popped = NULL;
+ int16 frame_offset_tmp[4] = { 0 };
+ int16 *frame_offset_buf = frame_offset_tmp;
+ uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
+#endif
+ bool ret = false;
+
+ bh_assert(loader_ctx->csp_num > 0);
+ if (loader_ctx->csp_num - 1 < depth) {
+ set_error_buf(error_buf, error_buf_size,
+ "unknown label, "
+ "unexpected end of section or function");
+ return false;
+ }
+
+ 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;
+#if WASM_ENABLE_GC != 0
+ frame_reftype_map = loader_ctx->frame_reftype_map;
+#endif
+
+ /* 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 WASM_ENABLE_GC == 0
+ 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);
+#else
+ if (target_block->label_type == LABEL_TYPE_LOOP)
+ arity = block_type_get_param_types(target_block_type, &types,
+ &reftype_maps, &reftype_map_count);
+ else
+ arity = block_type_get_result_types(target_block_type, &types,
+ &reftype_maps, &reftype_map_count);
+#endif
+
+ /* 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) {
+#if WASM_ENABLE_GC != 0
+ int32 j = reftype_map_count - 1;
+#endif
+ for (i = (int32)arity - 1; i >= 0; i--) {
+#if WASM_ENABLE_GC != 0
+ if (wasm_is_type_multi_byte_type(types[i])) {
+ bh_assert(reftype_maps[j].index == i);
+ bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
+ reftype_maps[j].ref_type,
+ wasm_reftype_struct_size(reftype_maps[j].ref_type));
+ j--;
+ }
+#endif
+#if WASM_ENABLE_FAST_INTERP != 0
+ POP_OFFSET_TYPE(types[i]);
+#endif
+ POP_TYPE(types[i]);
+ }
+
+ /* Backup stack data since it may be changed in the below
+ push operations, and the stack data may be used when
+ checking other target blocks of opcode br_table */
+ if (opcode == WASM_OP_BR_TABLE) {
+ uint64 total_size;
+
+ frame_ref_after_popped = loader_ctx->frame_ref;
+ total_size = (uint64)sizeof(uint8)
+ * (frame_ref_old - frame_ref_after_popped);
+ if (total_size > sizeof(frame_ref_tmp)
+ && !(frame_ref_buf = loader_malloc(total_size, error_buf,
+ error_buf_size))) {
+ goto fail;
+ }
+ bh_memcpy_s(frame_ref_buf, (uint32)total_size,
+ frame_ref_after_popped, (uint32)total_size);
+
+#if WASM_ENABLE_GC != 0
+ frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
+ total_size =
+ (uint64)sizeof(WASMRefTypeMap)
+ * (frame_reftype_map_old - frame_reftype_map_after_popped);
+ if (total_size > sizeof(frame_reftype_map_tmp)
+ && !(frame_reftype_map_buf = loader_malloc(
+ total_size, error_buf, error_buf_size))) {
+ goto fail;
+ }
+ bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
+ frame_reftype_map_after_popped, (uint32)total_size);
+#endif
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ frame_offset_after_popped = loader_ctx->frame_offset;
+ total_size = (uint64)sizeof(int16)
+ * (frame_offset_old - frame_offset_after_popped);
+ if (total_size > sizeof(frame_offset_tmp)
+ && !(frame_offset_buf = loader_malloc(total_size, error_buf,
+ error_buf_size))) {
+ goto fail;
+ }
+ bh_memcpy_s(frame_offset_buf, (uint32)total_size,
+ frame_offset_after_popped, (uint32)total_size);
+#endif
+ }
+
+#if WASM_ENABLE_GC != 0
+ j = 0;
+#endif
+ for (i = 0; i < (int32)arity; i++) {
+#if WASM_ENABLE_GC != 0
+ if (wasm_is_type_multi_byte_type(types[i])) {
+ bh_assert(reftype_maps[j].index == i);
+ bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
+ reftype_maps[j].ref_type,
+ wasm_reftype_struct_size(reftype_maps[j].ref_type));
+ j++;
+ }
+#endif
+#if WASM_ENABLE_FAST_INTERP != 0
+ bool disable_emit = true;
+ int16 operand_offset = 0;
+ PUSH_OFFSET_TYPE(types[i]);
+#endif
+ PUSH_TYPE(types[i]);
+ }
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_br_info(target_block, opcode == WASM_OP_BR);
+#endif
+
+ /* Restore the stack data, note that frame_ref_bottom,
+ frame_reftype_map_bottom, frame_offset_bottom may be
+ re-allocated in the above push operations */
+ if (opcode == WASM_OP_BR_TABLE) {
+ uint32 total_size;
+
+ /* The stack operand num should not be smaller than before
+ after pop and push operations */
+ bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
+ loader_ctx->stack_cell_num = stack_cell_num_old;
+ loader_ctx->frame_ref =
+ loader_ctx->frame_ref_bottom + stack_cell_num_old;
+ total_size = (uint32)(sizeof(uint8)
+ * (frame_ref_old - frame_ref_after_popped));
+ bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
+ frame_ref_buf, total_size);
+
+#if WASM_ENABLE_GC != 0
+ /* The stack operand num should not be smaller than before
+ after pop and push operations */
+ bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
+ loader_ctx->reftype_map_num = reftype_map_num_old;
+ loader_ctx->frame_reftype_map =
+ loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
+ total_size = (uint32)(sizeof(WASMRefTypeMap)
+ * (frame_reftype_map_old
+ - frame_reftype_map_after_popped));
+ bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
+ total_size, frame_reftype_map_buf, total_size);
+#endif
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ loader_ctx->frame_offset =
+ loader_ctx->frame_offset_bottom + stack_cell_num_old;
+ total_size =
+ (uint32)(sizeof(int16)
+ * (frame_offset_old - frame_offset_after_popped));
+ bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
+ total_size, frame_offset_buf, total_size);
+ (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
+#endif
+ }
+
+ ret = true;
+ goto cleanup_and_return;
+ }
+
+ available_stack_cell =
+ (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
+#if WASM_ENABLE_GC != 0
+ available_reftype_map =
+ (int32)(loader_ctx->reftype_map_num
+ - (loader_ctx->frame_csp - 1)->reftype_map_num);
+ reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
+#endif
+
+ /* Check stack top values match target block type */
+ for (i = (int32)arity - 1; i >= 0; i--) {
+ type = types[i];
+#if WASM_ENABLE_GC != 0
+ ref_type = NULL;
+ is_type_multi_byte = wasm_is_type_multi_byte_type(type);
+ if (is_type_multi_byte) {
+ bh_assert(reftype_map);
+ ref_type = reftype_map->ref_type;
+ }
+#endif
+
+ if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
+ break;
+
+ if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
+#if WASM_ENABLE_GC != 0
+ frame_reftype_map, available_reftype_map,
+#endif
+ type,
+#if WASM_ENABLE_GC != 0
+ ref_type,
+#endif
+ error_buf, error_buf_size)) {
+ goto fail;
+ }
+ cell_num = wasm_value_type_cell_num(types[i]);
+ frame_ref -= cell_num;
+ available_stack_cell -= cell_num;
+#if WASM_ENABLE_GC != 0
+ if (is_type_multi_byte) {
+ frame_reftype_map--;
+ available_reftype_map--;
+ reftype_map--;
+ }
+#endif
+ }
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_br_info(target_block, opcode == WASM_OP_BR);
+#endif
+
+ ret = true;
+
+cleanup_and_return:
+fail:
+ if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
+ wasm_runtime_free(frame_ref_buf);
+#if WASM_ENABLE_GC != 0
+ if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
+ wasm_runtime_free(frame_reftype_map_buf);
+#endif
+#if WASM_ENABLE_FAST_INTERP != 0
+ if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
+ wasm_runtime_free(frame_offset_buf);
+#endif
+
+ return ret;
+}
+
+static BranchBlock *
+check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
+ uint8 opcode, 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);
+ if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+
+ frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
+
+ *p_buf = p;
+ return frame_csp_tmp;
+fail:
+ return NULL;
+}
+
+#if WASM_ENABLE_EXCE_HANDLING != 0
+static BranchBlock *
+check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
+ uint8 *buf_end, 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);
+ /*
+ * Note: "delegate 0" means the surrounding block, not the
+ * try-delegate block itself.
+ *
+ * Note: the caller hasn't popped the try-delegate frame yet.
+ */
+ bh_assert(loader_ctx->csp_num > 0);
+ if (loader_ctx->csp_num - 1 <= depth) {
+#if WASM_ENABLE_SPEC_TEST == 0
+ set_error_buf(error_buf, error_buf_size, "unknown delegate label");
+#else
+ set_error_buf(error_buf, error_buf_size, "unknown label");
+#endif
+ goto fail;
+ }
+ frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_br_info(frame_csp_tmp, false);
+#endif
+
+ *p_buf = p;
+ return frame_csp_tmp;
+fail:
+ return NULL;
+}
+#endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
+
+static bool
+check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
+ char *error_buf, uint32 error_buf_size)
+{
+ 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;
+#if WASM_ENABLE_GC != 0
+ WASMRefTypeMap *frame_reftype_map;
+ WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
+ WASMRefType *ref_type;
+ uint32 param_count, return_reftype_map_count = 0;
+ int32 available_reftype_map =
+ (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
+#endif
+
+ available_stack_cell =
+ (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
+
+#if WASM_ENABLE_GC == 0
+ return_count = block_type_get_result_types(block_type, &return_types);
+#else
+ return_count = block_type_get_result_types(block_type, &return_types,
+ &return_reftype_maps,
+ &return_reftype_map_count);
+ param_count =
+ block_type->is_value_type ? 0 : block_type->u.type->param_count;
+ (void)param_count;
+#endif
+ return_cell_num =
+ return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
+
+ /* 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) {
+#if WASM_ENABLE_GC != 0
+ int32 j = return_reftype_map_count - 1;
+#endif
+ for (i = (int32)return_count - 1; i >= 0; i--) {
+#if WASM_ENABLE_GC != 0
+ if (wasm_is_type_multi_byte_type(return_types[i])) {
+ bh_assert(return_reftype_maps[j].index == i + param_count);
+ bh_memcpy_s(
+ loader_ctx->ref_type_tmp, sizeof(WASMRefType),
+ return_reftype_maps[j].ref_type,
+ wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
+ j--;
+ }
+#endif
+#if WASM_ENABLE_FAST_INTERP != 0
+ POP_OFFSET_TYPE(return_types[i]);
+#endif
+ POP_TYPE(return_types[i]);
+ }
+
+ /* Check stack is empty */
+ if (loader_ctx->stack_cell_num != block->stack_cell_num) {
+ set_error_buf(
+ error_buf, error_buf_size,
+ "type mismatch: stack size does not match block type");
+ goto fail;
+ }
+
+#if WASM_ENABLE_GC != 0
+ j = 0;
+#endif
+ for (i = 0; i < (int32)return_count; i++) {
+#if WASM_ENABLE_GC != 0
+ if (wasm_is_type_multi_byte_type(return_types[i])) {
+ bh_assert(return_reftype_maps[j].index == i + param_count);
+ bh_memcpy_s(
+ loader_ctx->ref_type_tmp, sizeof(WASMRefType),
+ return_reftype_maps[j].ref_type,
+ wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
+ j++;
+ }
+#endif
+#if WASM_ENABLE_FAST_INTERP != 0
+ bool disable_emit = true;
+ int16 operand_offset = 0;
+ PUSH_OFFSET_TYPE(return_types[i]);
+#endif
+ PUSH_TYPE(return_types[i]);
+ }
+ return true;
+ }
+
+ if (available_stack_cell != return_cell_num) {
+#if WASM_ENABLE_EXCE_HANDLING != 0
+ /* testspec: this error message format is expected by try_catch.wast */
+ snprintf(
+ error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
+ block->label_type == LABEL_TYPE_TRY
+ || (block->label_type == LABEL_TYPE_CATCH
+ && return_cell_num > 0)
+ ? "instruction"
+ : "block",
+ return_cell_num > 0 ? type2str(return_types[0]) : "",
+ " but stack has ",
+ available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
+ : "");
+ goto fail;
+#else
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch: stack size does not match block type");
+ goto fail;
+#endif
+ }
+
+ /* Check stack values match return types */
+ frame_ref = loader_ctx->frame_ref;
+#if WASM_ENABLE_GC != 0
+ frame_reftype_map = loader_ctx->frame_reftype_map;
+ return_reftype_map =
+ return_reftype_map_count
+ ? return_reftype_maps + return_reftype_map_count - 1
+ : NULL;
+#endif
+ for (i = (int32)return_count - 1; i >= 0; i--) {
+ uint8 type = return_types[i];
+#if WASM_ENABLE_GC != 0
+ bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
+ ref_type = NULL;
+ if (is_type_multi_byte) {
+ bh_assert(return_reftype_map);
+ ref_type = return_reftype_map->ref_type;
+ }
+#endif
+ if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
+#if WASM_ENABLE_GC != 0
+ frame_reftype_map, available_reftype_map,
+#endif
+ type,
+#if WASM_ENABLE_GC != 0
+ ref_type,
+#endif
+ error_buf, error_buf_size))
+ return false;
+ frame_ref -= wasm_value_type_cell_num(return_types[i]);
+ available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
+#if WASM_ENABLE_GC != 0
+ if (is_type_multi_byte) {
+ frame_reftype_map--;
+ available_reftype_map--;
+ return_reftype_map--;
+ }
+#endif
+ }
+
+ 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, char *error_buf,
+ uint32 error_buf_size)
+{
+ bool ret = false;
+ 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;
+ WASMFuncType *wasm_type = block_type->u.type;
+ uint32 param_count = block_type->u.type->param_count;
+ int16 condition_offset = 0;
+ bool disable_emit = false;
+ bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
+ int16 operand_offset = 0;
+
+ uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
+ bh_assert(size > 0);
+
+ /* 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)))
+ return false;
+
+ 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++) {
+ cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
+ 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) */
+ emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
+ : wasm_type->param_cell_num);
+ /* 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);
+
+ /* Since the start offset to save the block's params and
+ * the start offset to save the block's results may be
+ * different, we remember the dynamic offset for loop block
+ * so that we can use it to copy the stack operands to the
+ * loop block's params in wasm_loader_emit_br_info. */
+ if (block->label_type == LABEL_TYPE_LOOP)
+ block->start_dynamic_offset = loader_ctx->dynamic_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);
+
+ ret = true;
+
+fail:
+ /* Free the emit data */
+ wasm_runtime_free(emit_data);
+
+ return ret;
+}
+#endif
+
+#if WASM_ENABLE_GC == 0
+#define RESET_REFTYPE_MAP_STACK() (void)0
+#else
+#define RESET_REFTYPE_MAP_STACK() \
+ do { \
+ loader_ctx->reftype_map_num = \
+ (loader_ctx->frame_csp - 1)->reftype_map_num; \
+ loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
+ + loader_ctx->reftype_map_num; \
+ } while (0)
+#endif
+
+/* reset the stack to the state of before entering the last block */
+#if WASM_ENABLE_FAST_INTERP != 0
+#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; \
+ RESET_REFTYPE_MAP_STACK(); \
+ } while (0)
+#else
+#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; \
+ RESET_REFTYPE_MAP_STACK(); \
+ } while (0)
+#endif
+
+/* set current block's stack polymorphic state */
+#define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
+ do { \
+ BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
+ _cur_block->is_stack_polymorphic = flag; \
+ } while (0)
+
+#define BLOCK_HAS_PARAM(block_type) \
+ (!block_type.is_value_type && block_type.u.type->param_count > 0)
+
+#define PRESERVE_LOCAL_FOR_BLOCK() \
+ do { \
+ if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
+ error_buf_size))) { \
+ goto fail; \
+ } \
+ } while (0)
+
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+static bool
+get_table_elem_type(const WASMModule *module, uint32 table_idx,
+ uint8 *p_elem_type, void **p_ref_type, char *error_buf,
+ uint32 error_buf_size)
+{
+ if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
+ return false;
+ }
+
+ if (table_idx < module->import_table_count) {
+ if (p_elem_type)
+ *p_elem_type =
+ module->import_tables[table_idx].u.table.table_type.elem_type;
+#if WASM_ENABLE_GC != 0
+ if (p_ref_type)
+ *((WASMRefType **)p_ref_type) =
+ module->import_tables[table_idx]
+ .u.table.table_type.elem_ref_type;
+#endif
+ }
+ else {
+ if (p_elem_type)
+ *p_elem_type =
+ module->tables[module->import_table_count + table_idx]
+ .table_type.elem_type;
+#if WASM_ENABLE_GC != 0
+ if (p_ref_type)
+ *((WASMRefType **)p_ref_type) =
+ module->tables[module->import_table_count + table_idx]
+ .table_type.elem_ref_type;
+#endif
+ }
+ return true;
+}
+
+static bool
+get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
+ uint8 *p_elem_type, void **p_elem_ref_type,
+ char *error_buf, uint32 error_buf_size)
+{
+ if (table_seg_idx >= module->table_seg_count) {
+ set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
+ table_seg_idx);
+ return false;
+ }
+
+ if (p_elem_type) {
+ *p_elem_type = module->table_segments[table_seg_idx].elem_type;
+ }
+#if WASM_ENABLE_GC != 0
+ if (p_elem_ref_type)
+ *((WASMRefType **)p_elem_ref_type) =
+ module->table_segments[table_seg_idx].elem_ref_type;
+#endif
+ return true;
+}
+#endif
+
+#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 NULL;
+}
+#endif
+
+#if 0
+#define HANDLE_OPCODE(opcode) #opcode
+DEFINE_GOTO_TABLE(const char *, op_mnemonics);
+#undef HANDLE_OPCODE
+#endif
+
+static bool
+wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
+ uint32 cur_func_idx, char *error_buf,
+ uint32 error_buf_size)
+{
+ uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
+ uint32 param_count, local_count, global_count;
+ uint8 *param_types, *local_types, local_type, global_type, mem_offset_type,
+ table_elem_idx_type;
+ BlockType func_block_type;
+ uint16 *local_offsets, local_offset;
+ uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
+ uint32 table_seg_idx, data_seg_idx, count, align, i;
+ mem_offset_t mem_offset;
+ int32 i32_const = 0;
+ int64 i64_const;
+ uint8 opcode;
+ bool return_value = false;
+ WASMLoaderContext *loader_ctx;
+ BranchBlock *frame_csp_tmp;
+#if WASM_ENABLE_GC != 0
+ WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
+ uint32 param_reftype_map_count, local_reftype_map_count;
+ int32 heap_type;
+ WASMRefType wasm_ref_type = { 0 };
+ bool need_ref_type_map;
+#endif
+#if WASM_ENABLE_FAST_INTERP != 0
+ uint8 *func_const_end, *func_const = NULL;
+ int16 operand_offset = 0;
+ uint8 last_op = 0;
+ bool disable_emit, preserve_local = false, if_condition_available = true;
+ float32 f32_const;
+ float64 f64_const;
+
+ LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
+ func->param_cell_num, func->local_cell_num, func->ret_cell_num);
+#endif
+#if WASM_ENABLE_MEMORY64 != 0
+ bool is_memory64 = has_module_memory64(module);
+ mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
+#else
+ mem_offset_type = VALUE_TYPE_I32;
+ table_elem_idx_type = VALUE_TYPE_I32;
+#endif
+ uint32 memidx;
+
+ global_count = module->import_global_count + module->global_count;
+
+ param_count = func->func_type->param_count;
+ param_types = func->func_type->types;
+
+ func_block_type.is_value_type = false;
+ func_block_type.u.type = func->func_type;
+
+ local_count = func->local_count;
+ local_types = func->local_types;
+ local_offsets = func->local_offsets;
+
+#if WASM_ENABLE_GC != 0
+ param_reftype_maps = func->func_type->ref_type_maps;
+ param_reftype_map_count = func->func_type->ref_type_map_count;
+ local_reftype_maps = func->local_ref_type_maps;
+ local_reftype_map_count = func->local_ref_type_map_count;
+#endif
+
+ if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
+ goto fail;
+ }
+#if WASM_ENABLE_GC != 0
+ loader_ctx->module = module;
+ loader_ctx->ref_type_set = module->ref_type_set;
+ loader_ctx->ref_type_tmp = &wasm_ref_type;
+#endif
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ /* 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;
+
+re_scan:
+ if (loader_ctx->code_compiled_size > 0) {
+ if (!wasm_loader_ctx_reinit(loader_ctx)) {
+ set_error_buf(error_buf, error_buf_size, "allocate memory failed");
+ goto fail;
+ }
+ p = func->code;
+ func->code_compiled = loader_ctx->p_code_compiled;
+ func->code_compiled_size = loader_ctx->code_compiled_size;
+ }
+#endif
+
+ PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
+
+ while (p < p_end) {
+ opcode = *p++;
+#if WASM_ENABLE_FAST_INTERP != 0
+ p_org = p;
+ disable_emit = false;
+ emit_label(opcode);
+#endif
+ switch (opcode) {
+ case WASM_OP_UNREACHABLE:
+ RESET_STACK();
+ SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
+ break;
+
+ case WASM_OP_NOP:
+#if WASM_ENABLE_FAST_INTERP != 0
+ skip_label();
+#endif
+ break;
+
+ case WASM_OP_IF:
+ {
+#if WASM_ENABLE_FAST_INTERP != 0
+ BranchBlock *parent_block = loader_ctx->frame_csp - 1;
+ int32 available_stack_cell =
+ (int32)(loader_ctx->stack_cell_num
+ - parent_block->stack_cell_num);
+
+ if (available_stack_cell <= 0
+ && parent_block->is_stack_polymorphic)
+ if_condition_available = false;
+ else
+ if_condition_available = true;
+
+ PRESERVE_LOCAL_FOR_BLOCK();
+#endif
+#if WASM_ENABLE_GC == 0
+ POP_I32();
+#endif
+ goto handle_op_block_and_loop;
+ }
+ case WASM_OP_BLOCK:
+ case WASM_OP_LOOP:
+#if WASM_ENABLE_EXCE_HANDLING != 0
+ case WASM_OP_TRY:
+ if (opcode == WASM_OP_TRY) {
+ /*
+ * keep track of exception handlers to account for
+ * memory allocation
+ */
+ func->exception_handler_count++;
+
+ /*
+ * try is a block
+ * do nothing special, but execution continues to
+ * to handle_op_block_and_loop,
+ * and that be pushes the csp
+ */
+ }
+
+#endif
+#if WASM_ENABLE_FAST_INTERP != 0
+ PRESERVE_LOCAL_FOR_BLOCK();
+#endif
+ handle_op_block_and_loop:
+ {
+ uint8 value_type;
+ BlockType block_type;
+#if WASM_ENABLE_FAST_INTERP != 0
+ uint32 available_params = 0;
+#endif
+
+ CHECK_BUF(p, p_end, 1);
+ value_type = read_uint8(p);
+ if (is_byte_a_type(value_type)) {
+ /* 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.type = value_type;
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ if (value_type == VALUE_TYPE_V128)
+ module->is_simd_used = true;
+ else if (value_type == VALUE_TYPE_FUNCREF
+ || value_type == VALUE_TYPE_EXTERNREF)
+ module->is_ref_types_used = true;
+#endif
+#if WASM_ENABLE_GC != 0
+ if (value_type != VALUE_TYPE_VOID) {
+ p_org = p;
+ p--;
+ if (!resolve_value_type((const uint8 **)&p, p_end,
+ module, module->type_count,
+ &need_ref_type_map,
+ &wasm_ref_type, false,
+ error_buf, error_buf_size)) {
+ goto fail;
+ }
+ if (need_ref_type_map) {
+ block_type.u.value_type.ref_type_map.index = 0;
+ if (!(block_type.u.value_type.ref_type_map
+ .ref_type = reftype_set_insert(
+ module->ref_type_set, &wasm_ref_type,
+ error_buf, error_buf_size))) {
+ goto fail;
+ }
+ }
+ /* Set again as the type might be changed, e.g.
+ (ref null any) to anyref */
+ block_type.u.value_type.type = wasm_ref_type.ref_type;
+#if WASM_ENABLE_FAST_INTERP == 0
+ while (p_org < p) {
+#if WASM_ENABLE_DEBUG_INTERP != 0
+ if (!record_fast_op(module, p_org, *p_org,
+ error_buf, error_buf_size)) {
+ goto fail;
+ }
+#endif
+ /* Ignore extra bytes for interpreter */
+ *p_org++ = WASM_OP_NOP;
+ }
+#endif
+ }
+#endif /* end of WASM_ENABLE_GC != 0 */
+ }
+ else {
+ int32 type_index;
+ /* Resolve the leb128 encoded type index as block type */
+ p--;
+ p_org = p - 1;
+ read_leb_int32(p, p_end, type_index);
+ if ((uint32)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 =
+ (WASMFuncType *)module->types[type_index];
+#if WASM_ENABLE_FAST_INTERP == 0
+ /* If block use type index as block type, change the opcode
+ * to new extended opcode so that interpreter can resolve
+ * the block quickly.
+ */
+#if WASM_ENABLE_DEBUG_INTERP != 0
+ if (!record_fast_op(module, p_org, *p_org, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+#endif
+ *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
+#endif
+ }
+
+#if WASM_ENABLE_GC != 0
+ if (opcode == WASM_OP_IF) {
+ POP_I32();
+ }
+#endif
+
+ /* Pop block parameters from stack */
+ if (BLOCK_HAS_PARAM(block_type)) {
+ WASMFuncType *wasm_type = block_type.u.type;
+
+ BranchBlock *cur_block = loader_ctx->frame_csp - 1;
+#if WASM_ENABLE_FAST_INTERP != 0
+ uint32 cell_num;
+ available_params = block_type.u.type->param_count;
+#endif
+ for (i = 0; i < block_type.u.type->param_count; i++) {
+
+ int32 available_stack_cell =
+ (int32)(loader_ctx->stack_cell_num
+ - cur_block->stack_cell_num);
+ if (available_stack_cell <= 0
+ && cur_block->is_stack_polymorphic) {
+#if WASM_ENABLE_FAST_INTERP != 0
+ available_params = i;
+#endif
+ break;
+ }
+
+ POP_TYPE(
+ wasm_type->types[wasm_type->param_count - i - 1]);
+#if WASM_ENABLE_FAST_INTERP != 0
+ /* decrease the frame_offset pointer accordingly to keep
+ * consistent with frame_ref stack */
+ cell_num = wasm_value_type_cell_num(
+ wasm_type->types[wasm_type->param_count - i - 1]);
+ loader_ctx->frame_offset -= cell_num;
+#endif
+ }
+ }
+ PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
+ block_type, p);
+
+ /* Pass parameters to block */
+ if (BLOCK_HAS_PARAM(block_type)) {
+ WASMFuncType *func_type = block_type.u.type;
+#if WASM_ENABLE_GC != 0
+ WASMRefType *ref_type;
+ uint32 j = 0;
+#endif
+ for (i = 0; i < func_type->param_count; i++) {
+#if WASM_ENABLE_FAST_INTERP != 0
+ uint32 cell_num =
+ wasm_value_type_cell_num(func_type->types[i]);
+ if (i >= available_params) {
+ /* If there isn't enough data on stack, push a dummy
+ * offset to keep the stack consistent with
+ * frame_ref.
+ * Since the stack is already in polymorphic state,
+ * the opcode will not be executed, so the dummy
+ * offset won't cause any error */
+ uint32 n;
+
+ for (n = 0; n < cell_num; n++) {
+ if (loader_ctx->p_code_compiled == NULL) {
+ if (!check_offset_push(loader_ctx,
+ error_buf,
+ error_buf_size))
+ goto fail;
+ }
+ *loader_ctx->frame_offset++ = 0;
+ }
+ }
+ else {
+ loader_ctx->frame_offset += cell_num;
+ }
+#endif
+#if WASM_ENABLE_GC != 0
+ if (wasm_is_type_multi_byte_type(func_type->types[i])) {
+ bh_assert(func_type->ref_type_maps[j].index == i);
+ ref_type = func_type->ref_type_maps[j].ref_type;
+ bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
+ ref_type,
+ wasm_reftype_struct_size(ref_type));
+ j++;
+ }
+#endif
+ PUSH_TYPE(func_type->types[i]);
+ }
+ }
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
+ skip_label();
+
+ if (BLOCK_HAS_PARAM(block_type)) {
+ /* Make sure params are in dynamic space */
+ if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
+ error_buf_size))
+ goto fail;
+ }
+
+ if (opcode == WASM_OP_LOOP) {
+ (loader_ctx->frame_csp - 1)->code_compiled =
+ loader_ctx->p_code_compiled;
+ }
+ }
+#if WASM_ENABLE_EXCE_HANDLING != 0
+ else if (opcode == WASM_OP_TRY) {
+ skip_label();
+ }
+#endif
+ else if (opcode == WASM_OP_IF) {
+ BranchBlock *block = loader_ctx->frame_csp - 1;
+ /* 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)
+ * (if (param i32 i32) (result i32 i32) (local.get 0)
+ * (then)) (i32.add)
+ * )
+ *
+ * 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)) {
+ uint64 size;
+
+ /* In polymorphic state, there may be no if condition on
+ * the stack, so the offset may not emitted */
+ if (if_condition_available) {
+ /* skip the if condition operand offset */
+ wasm_loader_emit_backspace(loader_ctx,
+ sizeof(int16));
+ }
+ /* skip the if label */
+ skip_label();
+ /* Emit a copy instruction */
+ if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
+ error_buf_size))
+ goto fail;
+
+ /* Emit the if instruction */
+ emit_label(opcode);
+ /* Emit the new condition operand offset */
+ POP_OFFSET_TYPE(VALUE_TYPE_I32);
+
+ /* 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)))
+ goto fail;
+ bh_memcpy_s(block->param_frame_offsets, (uint32)size,
+ loader_ctx->frame_offset
+ - size / sizeof(int16),
+ (uint32)size);
+ }
+
+ block->start_dynamic_offset = loader_ctx->dynamic_offset;
+
+ emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
+ emit_empty_label_addr_and_frame_ip(PATCH_END);
+ }
+#endif
+ break;
+ }
+#if WASM_ENABLE_EXCE_HANDLING != 0
+ case WASM_OP_THROW:
+ {
+ SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
+
+ BranchBlock *cur_block = loader_ctx->frame_csp - 1;
+
+ uint8 label_type = cur_block->label_type;
+ uint32 tag_index = 0;
+ read_leb_int32(p, p_end, tag_index);
+
+ /* check validity of tag_index against module->tag_count */
+ /* check tag index is within the tag index space */
+ if (tag_index >= module->import_tag_count + module->tag_count) {
+ snprintf(error_buf, error_buf_size, "unknown tag %d",
+ tag_index);
+ goto fail;
+ }
+
+ /* the tag_type is stored in either the WASMTag (section tags)
+ * or WASMTagImport (import tag) */
+ WASMFuncType *tag_type = NULL;
+ if (tag_index < module->import_tag_count) {
+ tag_type = module->import_tags[tag_index].u.tag.tag_type;
+ }
+ else {
+ tag_type =
+ module->tags[tag_index - module->import_tag_count]
+ ->tag_type;
+ }
+
+ if (tag_type->result_count != 0) {
+ set_error_buf(error_buf, error_buf_size,
+ "tag type signature does not return void");
+ goto fail;
+ }
+
+ int32 available_stack_cell =
+ (int32)(loader_ctx->stack_cell_num
+ - cur_block->stack_cell_num);
+ int32 tti;
+
+ /* Check stack values match return types by comparing tag param
+ * types with stack cells */
+ uint8 *frame_ref = loader_ctx->frame_ref;
+#if WASM_ENABLE_GC != 0
+ WASMRefTypeMap *frame_reftype_map =
+ loader_ctx->frame_reftype_map;
+ uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
+
+ /* Temporarily set these values since they may be used in
+ GET_LOCAL_REFTYPE(), remember they must be restored later */
+ param_reftype_maps = tag_type->ref_type_maps;
+ /* For tag_type function, it shouldn't have result_count = 0 */
+ param_reftype_map_count = tag_type->ref_type_map_count;
+ param_count = tag_type->param_count;
+#endif
+
+ for (tti = (int32)tag_type->param_count - 1; tti >= 0; tti--) {
+#if WASM_ENABLE_GC != 0
+ local_type = tag_type->types[tti];
+ local_idx = tti;
+ /* Get the wasm_ref_type if the local_type is multibyte
+ type */
+ GET_LOCAL_REFTYPE();
+#endif
+
+ if (!check_stack_top_values(
+ loader_ctx, frame_ref, available_stack_cell,
+#if WASM_ENABLE_GC != 0
+ frame_reftype_map, frame_reftype_map_num,
+#endif
+ tag_type->types[tti],
+#if WASM_ENABLE_GC != 0
+ &wasm_ref_type,
+#endif
+ error_buf, error_buf_size)) {
+ snprintf(error_buf, error_buf_size,
+ "type mismatch: instruction requires [%s] but "
+ "stack has [%s]",
+ tag_type->param_count > 0
+ ? type2str(tag_type->types[tti])
+ : "",
+ available_stack_cell > 0
+ ? type2str(*(loader_ctx->frame_ref - 1))
+ : "");
+ goto fail;
+ }
+ frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
+ available_stack_cell -=
+ wasm_value_type_cell_num(tag_type->types[tti]);
+ }
+
+#if WASM_ENABLE_GC != 0
+ /* Restore the values */
+ param_reftype_maps = func->func_type->ref_type_maps;
+ param_reftype_map_count = func->func_type->ref_type_map_count;
+ param_count = func->func_type->param_count;
+#endif
+
+ /* throw is stack polymorphic */
+ (void)label_type;
+ RESET_STACK();
+
+ break;
+ }
+ case WASM_OP_RETHROW:
+ {
+ /* must be done before checking branch block */
+ SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
+
+ /* check the target catching block: LABEL_TYPE_CATCH */
+ if (!(frame_csp_tmp =
+ check_branch_block(loader_ctx, &p, p_end, opcode,
+ error_buf, error_buf_size)))
+ goto fail;
+
+ if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
+ && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
+ /* trap according to spectest (rethrow.wast) */
+ set_error_buf(error_buf, error_buf_size,
+ "invalid rethrow label");
+ goto fail;
+ }
+
+ BranchBlock *cur_block = loader_ctx->frame_csp - 1;
+ uint8 label_type = cur_block->label_type;
+ (void)label_type;
+ /* rethrow is stack polymorphic */
+ RESET_STACK();
+ break;
+ }
+ case WASM_OP_DELEGATE:
+ {
+ /* check target block is valid */
+ if (!(frame_csp_tmp = check_branch_block_for_delegate(
+ loader_ctx, &p, p_end, error_buf, error_buf_size)))
+ goto fail;
+
+ BranchBlock *cur_block = loader_ctx->frame_csp - 1;
+ uint8 label_type = cur_block->label_type;
+
+ (void)label_type;
+ /* DELEGATE ends the block */
+ POP_CSP();
+ break;
+ }
+ case WASM_OP_CATCH:
+ {
+ BranchBlock *cur_block = loader_ctx->frame_csp - 1;
+
+ uint8 label_type = cur_block->label_type;
+ uint32 tag_index = 0;
+ read_leb_int32(p, p_end, tag_index);
+
+ /* check validity of tag_index against module->tag_count */
+ /* check tag index is within the tag index space */
+ if (tag_index >= module->import_tag_count + module->tag_count) {
+ LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
+ __FUNCTION__);
+ set_error_buf(error_buf, error_buf_size, "unknown tag");
+ goto fail;
+ }
+
+ /* the tag_type is stored in either the WASMTag (section tags)
+ * or WASMTagImport (import tag) */
+ WASMFuncType *func_type = NULL;
+ if (tag_index < module->import_tag_count) {
+ func_type = module->import_tags[tag_index].u.tag.tag_type;
+ }
+ else {
+ func_type =
+ module->tags[tag_index - module->import_tag_count]
+ ->tag_type;
+ }
+
+ if (func_type->result_count != 0) {
+ set_error_buf(error_buf, error_buf_size,
+ "tag type signature does not return void");
+ goto fail;
+ }
+
+ /* check validity of current label (expect LABEL_TYPE_TRY or
+ * LABEL_TYPE_CATCH) */
+ if ((LABEL_TYPE_CATCH != label_type)
+ && (LABEL_TYPE_TRY != label_type)) {
+ set_error_buf(error_buf, error_buf_size,
+ "Unexpected block sequence encountered.");
+ goto fail;
+ }
+
+ /*
+ * replace frame_csp by LABEL_TYPE_CATCH
+ */
+ cur_block->label_type = LABEL_TYPE_CATCH;
+
+ /* RESET_STACK removes the values pushed in TRY or pervious
+ * CATCH Blocks */
+ RESET_STACK();
+
+#if WASM_ENABLE_GC != 0
+ WASMRefType *ref_type;
+ uint32 j = 0;
+#endif
+
+ /* push types on the stack according to caught type */
+ for (i = 0; i < func_type->param_count; i++) {
+#if WASM_ENABLE_GC != 0
+ if (wasm_is_type_multi_byte_type(func_type->types[i])) {
+ bh_assert(func_type->ref_type_maps[j].index == i);
+ ref_type = func_type->ref_type_maps[j].ref_type;
+ bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
+ ref_type,
+ wasm_reftype_struct_size(ref_type));
+ j++;
+ }
+#endif
+ PUSH_TYPE(func_type->types[i]);
+ }
+ break;
+ }
+ case WASM_OP_CATCH_ALL:
+ {
+ BranchBlock *cur_block = loader_ctx->frame_csp - 1;
+
+ /* expecting a TRY or CATCH, anything else will be considered an
+ * error */
+ if ((LABEL_TYPE_CATCH != cur_block->label_type)
+ && (LABEL_TYPE_TRY != cur_block->label_type)) {
+ set_error_buf(error_buf, error_buf_size,
+ "Unexpected block sequence encountered.");
+ goto fail;
+ }
+
+ /* no immediates */
+ /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
+ cur_block->label_type = LABEL_TYPE_CATCH_ALL;
+
+ /* RESET_STACK removes the values pushed in TRY or pervious
+ * CATCH Blocks */
+ RESET_STACK();
+
+ /* catch_all has no tagtype and therefore no parameters */
+ break;
+ }
+#endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
+ case WASM_OP_ELSE:
+ handle_op_else:
+ {
+ BranchBlock *block = NULL;
+ BlockType block_type;
+
+ if (loader_ctx->csp_num < 2
+ /* the matched if isn't found */
+ || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
+ /* duplicated else is found */
+ || (loader_ctx->frame_csp - 1)->else_addr) {
+ set_error_buf(
+ error_buf, error_buf_size,
+ "opcode else found without matched opcode if");
+ goto fail;
+ }
+ block = loader_ctx->frame_csp - 1;
+
+ /* check whether if branch's stack matches its result type */
+ if (!check_block_stack(loader_ctx, block, error_buf,
+ error_buf_size))
+ goto fail;
+
+ block->else_addr = p - 1;
+ block_type = block->block_type;
+
+#if WASM_ENABLE_GC != 0
+ if (!wasm_loader_init_local_use_masks(
+ loader_ctx, local_count, error_buf, error_buf_size)) {
+ goto fail;
+ }
+#endif
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ /* if the result of if branch is in local or const area, add a
+ * copy op */
+ RESERVE_BLOCK_RET();
+
+ emit_empty_label_addr_and_frame_ip(PATCH_END);
+ apply_label_patch(loader_ctx, 1, PATCH_ELSE);
+#endif
+ RESET_STACK();
+ SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
+
+ /* 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;
+ size = sizeof(int16) * block_type.u.type->param_cell_num;
+ bh_memcpy_s(loader_ctx->frame_offset, size,
+ block->param_frame_offsets, size);
+ loader_ctx->frame_offset += (size / sizeof(int16));
+ }
+ loader_ctx->dynamic_offset = block->start_dynamic_offset;
+#endif
+
+ break;
+ }
+
+ case WASM_OP_END:
+ {
+ BranchBlock *cur_block = loader_ctx->frame_csp - 1;
+
+ /* check whether block stack matches its result type */
+ if (!check_block_stack(loader_ctx, cur_block, error_buf,
+ error_buf_size))
+ goto fail;
+
+ /* if there is no else branch, make a virtual else opcode for
+ easier integrity check and to copy the correct results to
+ the block return address for fast-interp mode:
+ change if block from `if ... end` to `if ... else end` */
+ if (cur_block->label_type == LABEL_TYPE_IF
+ && !cur_block->else_addr) {
+ opcode = WASM_OP_ELSE;
+ p--;
+#if WASM_ENABLE_FAST_INTERP != 0
+ p_org = p;
+ skip_label();
+ disable_emit = false;
+ emit_label(opcode);
+#endif
+ goto handle_op_else;
+ }
+
+ POP_CSP();
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ skip_label();
+ /* copy the result to the block return address */
+ RESERVE_BLOCK_RET();
+
+ apply_label_patch(loader_ctx, 0, PATCH_END);
+ free_label_patch_list(loader_ctx->frame_csp);
+ if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
+ int32 idx;
+ uint8 ret_type;
+
+ emit_label(WASM_OP_RETURN);
+ 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);
+ }
+ }
+#endif
+ if (loader_ctx->csp_num > 0) {
+ loader_ctx->frame_csp->end_addr = p - 1;
+ }
+ else {
+ /* end of function block, function will return */
+ if (p < p_end) {
+ set_error_buf(error_buf, error_buf_size,
+ "section size mismatch");
+ goto fail;
+ }
+ }
+
+ break;
+ }
+
+ case WASM_OP_BR:
+ {
+ if (!(frame_csp_tmp =
+ check_branch_block(loader_ctx, &p, p_end, opcode,
+ error_buf, error_buf_size)))
+ goto fail;
+
+ RESET_STACK();
+ SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
+ break;
+ }
+
+ case WASM_OP_BR_IF:
+ {
+ POP_I32();
+
+ if (!(frame_csp_tmp =
+ check_branch_block(loader_ctx, &p, p_end, opcode,
+ error_buf, error_buf_size)))
+ goto fail;
+
+ break;
+ }
+
+ case WASM_OP_BR_TABLE:
+ {
+ uint32 depth = 0, default_arity, arity = 0;
+ BranchBlock *target_block;
+ BlockType *target_block_type;
+#if WASM_ENABLE_FAST_INTERP == 0
+ BrTableCache *br_table_cache = NULL;
+ uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
+ uint32 j;
+#endif
+
+ read_leb_uint32(p, p_end, count);
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, count);
+#endif
+ POP_I32();
+
+ /* Get each depth and check it */
+ p_org = p;
+ for (i = 0; i <= count; i++) {
+ read_leb_uint32(p, p_end, depth);
+ bh_assert(loader_ctx->csp_num > 0);
+ if (loader_ctx->csp_num - 1 < depth) {
+ set_error_buf(error_buf, error_buf_size,
+ "unknown label, "
+ "unexpected end of section or function");
+ goto fail;
+ }
+ }
+ p = p_org;
+
+ /* Get the default block's arity */
+ target_block = loader_ctx->frame_csp - (depth + 1);
+ target_block_type = &target_block->block_type;
+ default_arity = block_type_get_arity(target_block_type,
+ target_block->label_type);
+
+#if WASM_ENABLE_FAST_INTERP == 0
+ p_depth_begin = p_depth = p;
+#endif
+ for (i = 0; i <= count; i++) {
+ p_org = p;
+ read_leb_uint32(p, p_end, depth);
+ p = p_org;
+
+ /* Get the target block's arity and check it */
+ target_block = loader_ctx->frame_csp - (depth + 1);
+ target_block_type = &target_block->block_type;
+ arity = block_type_get_arity(target_block_type,
+ target_block->label_type);
+ if (arity != default_arity) {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch: br_table targets must "
+ "all use same result type");
+ goto fail;
+ }
+
+ if (!(frame_csp_tmp =
+ check_branch_block(loader_ctx, &p, p_end, opcode,
+ error_buf, error_buf_size))) {
+ goto fail;
+ }
+
+#if WASM_ENABLE_FAST_INTERP == 0
+ 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_opcode, *p_opcode,
+ 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_opcode = EXT_OP_BR_TABLE_CACHE;
+ br_table_cache->br_table_op_addr = p_opcode;
+ 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
+ }
+
+#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
+
+ RESET_STACK();
+ SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
+ break;
+ }
+
+ case WASM_OP_RETURN:
+ {
+ WASMFuncType *func_type = func->func_type;
+ int32 idx;
+ uint8 ret_type;
+
+#if WASM_ENABLE_GC != 0
+ uint32 j = func_type->ref_type_map_count - 1;
+#endif
+ for (idx = (int32)func_type->result_count - 1; idx >= 0;
+ idx--) {
+ ret_type =
+ *(func_type->types + func_type->param_count + idx);
+#if WASM_ENABLE_GC != 0
+ if (wasm_is_type_multi_byte_type(ret_type)) {
+ WASMRefType *ref_type =
+ func_type->ref_type_maps[j].ref_type;
+ bh_assert(func_type->ref_type_maps[j].index
+ == func_type->param_count + idx);
+ bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
+ ref_type,
+ wasm_reftype_struct_size(ref_type));
+ j--;
+ }
+#endif
+#if WASM_ENABLE_FAST_INTERP != 0
+ /* emit the offset after return opcode */
+ POP_OFFSET_TYPE(ret_type);
+#endif
+ POP_TYPE(ret_type);
+ }
+
+ RESET_STACK();
+ SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
+
+ break;
+ }
+
+ case WASM_OP_CALL:
+#if WASM_ENABLE_TAIL_CALL != 0
+ case WASM_OP_RETURN_CALL:
+#endif
+#if WASM_ENABLE_GC != 0
+ case WASM_OP_CALL_REF:
+ case WASM_OP_RETURN_CALL_REF:
+#endif
+ {
+ WASMFuncType *func_type;
+ uint8 type;
+ int32 idx;
+#if WASM_ENABLE_GC != 0
+ WASMRefType *ref_type;
+ uint32 type_idx1;
+ int32 j;
+#endif
+
+#if WASM_ENABLE_GC != 0
+ if (opcode == WASM_OP_CALL_REF
+ || opcode == WASM_OP_RETURN_CALL_REF) {
+ read_leb_uint32(p, p_end, type_idx1);
+ if (!check_type_index(module, module->type_count, type_idx1,
+ error_buf, error_buf_size)) {
+ goto fail;
+ }
+ if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
+ set_error_buf(error_buf, error_buf_size,
+ "unkown function type");
+ goto fail;
+ }
+ if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
+ &type_idx, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+ if (type == VALUE_TYPE_ANY) {
+ type_idx = type_idx1;
+ }
+ if (!check_type_index(module, module->type_count, type_idx,
+ error_buf, error_buf_size)) {
+ goto fail;
+ }
+ if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
+ set_error_buf(error_buf, error_buf_size,
+ "unkown function type");
+ goto fail;
+ }
+ if (!wasm_func_type_is_super_of(
+ (WASMFuncType *)module->types[type_idx1],
+ (WASMFuncType *)module->types[type_idx])) {
+ set_error_buf(error_buf, error_buf_size,
+ "function type mismatch");
+ goto fail;
+ }
+ func_type = (WASMFuncType *)module->types[type_idx];
+ }
+ else
+#endif
+ {
+ read_leb_uint32(p, p_end, func_idx);
+#if WASM_ENABLE_FAST_INTERP != 0
+ /* we need to emit func_idx before arguments */
+ emit_uint32(loader_ctx, func_idx);
+#endif
+
+ if (!check_function_index(module, func_idx, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+
+ if (func_idx < module->import_function_count)
+ func_type = module->import_functions[func_idx]
+ .u.function.func_type;
+ else
+ func_type =
+ module
+ ->functions[func_idx
+ - module->import_function_count]
+ ->func_type;
+ }
+
+ if (func_type->param_count > 0) {
+#if WASM_ENABLE_GC != 0
+ j = (int32)(func_type->result_ref_type_maps
+ - func_type->ref_type_maps - 1);
+#endif
+ for (idx = (int32)(func_type->param_count - 1); idx >= 0;
+ idx--) {
+#if WASM_ENABLE_GC != 0
+ if (wasm_is_type_multi_byte_type(
+ func_type->types[idx])) {
+ ref_type = func_type->ref_type_maps[j].ref_type;
+ bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
+ ref_type,
+ wasm_reftype_struct_size(ref_type));
+ j--;
+ }
+#endif
+#if WASM_ENABLE_FAST_INTERP != 0
+ POP_OFFSET_TYPE(func_type->types[idx]);
+#endif
+ POP_TYPE(func_type->types[idx]);
+ }
+ }
+
+#if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
+ if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
+#endif
+#if WASM_ENABLE_GC != 0
+ j = (int32)(func_type->result_ref_type_maps
+ - func_type->ref_type_maps);
+#endif
+ for (i = 0; i < func_type->result_count; i++) {
+#if WASM_ENABLE_GC != 0
+ if (wasm_is_type_multi_byte_type(
+ func_type->types[func_type->param_count + i])) {
+ ref_type = func_type->ref_type_maps[j].ref_type;
+ bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
+ ref_type,
+ wasm_reftype_struct_size(ref_type));
+ j++;
+ }
+#endif
+ PUSH_TYPE(func_type->types[func_type->param_count + i]);
+#if WASM_ENABLE_FAST_INTERP != 0
+ /* 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.
+ */
+ PUSH_OFFSET_TYPE(
+ func_type->types[func_type->param_count + i]);
+#endif
+ }
+#if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
+ }
+ else {
+#if WASM_ENABLE_GC == 0
+ 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 ",
+ func->func_type->result_count,
+ " return values but got other");
+ goto fail;
+ }
+ for (i = 0; i < func_type->result_count; i++) {
+ 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");
+ goto fail;
+ }
+ }
+#else
+ if (!wasm_func_type_result_is_subtype_of(
+ func_type, func->func_type, module->types,
+ module->type_count)) {
+ set_error_buf(
+ error_buf, error_buf_size,
+ "type mismatch: invalid func result types");
+ goto fail;
+ }
+#endif
+ RESET_STACK();
+ SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
+ }
+#endif
+
+#if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
+ || WASM_ENABLE_WAMR_COMPILER != 0
+ func->has_op_func_call = true;
+#endif
+ (void)type;
+ break;
+ }
+
+ /*
+ * if disable reference type: call_indirect typeidx, 0x00
+ * if enable reference type: call_indirect typeidx, tableidx
+ */
+ case WASM_OP_CALL_INDIRECT:
+#if WASM_ENABLE_TAIL_CALL != 0
+ case WASM_OP_RETURN_CALL_INDIRECT:
+#endif
+ {
+ int32 idx;
+ WASMFuncType *func_type;
+
+ read_leb_uint32(p, p_end, type_idx);
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ if (p + 1 < p_end && *p != 0x00) {
+ /*
+ * Any non-0x00 byte requires the ref types proposal.
+ * This is different from checking the table_idx value
+ * since `0x80 0x00` etc. are all valid encodings of zero.
+ */
+ module->is_ref_types_used = true;
+ }
+#endif
+ read_leb_uint32(p, p_end, table_idx);
+#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)) {
+ goto fail;
+ }
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ /* we need to emit before arguments */
+#if WASM_ENABLE_TAIL_CALL != 0
+ emit_byte(loader_ctx, opcode);
+#endif
+ emit_uint32(loader_ctx, type_idx);
+ emit_uint32(loader_ctx, table_idx);
+#endif
+
+#if WASM_ENABLE_MEMORY64 != 0
+ table_elem_idx_type = is_table_64bit(module, table_idx)
+ ? VALUE_TYPE_I64
+ : VALUE_TYPE_I32;
+#endif
+ /* skip elem idx */
+ POP_TBL_ELEM_IDX();
+
+ if (type_idx >= module->type_count) {
+ set_error_buf(error_buf, error_buf_size, "unknown type");
+ goto fail;
+ }
+
+ func_type = (WASMFuncType *)module->types[type_idx];
+
+ if (func_type->param_count > 0) {
+ for (idx = (int32)(func_type->param_count - 1); idx >= 0;
+ idx--) {
+#if WASM_ENABLE_FAST_INTERP != 0
+ POP_OFFSET_TYPE(func_type->types[idx]);
+#endif
+ POP_TYPE(func_type->types[idx]);
+ }
+ }
+
+#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]);
+#if WASM_ENABLE_FAST_INTERP != 0
+ PUSH_OFFSET_TYPE(
+ func_type->types[func_type->param_count + i]);
+#endif
+ }
+#if WASM_ENABLE_TAIL_CALL != 0
+ }
+ else {
+ uint8 type;
+ 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 ",
+ func->func_type->result_count,
+ " return values but got other");
+ goto fail;
+ }
+ for (i = 0; i < func_type->result_count; i++) {
+ 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");
+ goto fail;
+ }
+ }
+ RESET_STACK();
+ SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
+ }
+#endif
+#if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
+ || WASM_ENABLE_WAMR_COMPILER != 0
+ func->has_op_func_call = true;
+#endif
+#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
+ func->has_op_call_indirect = true;
+#endif
+ break;
+ }
+
+ case WASM_OP_DROP:
+ {
+ BranchBlock *cur_block = loader_ctx->frame_csp - 1;
+ int32 available_stack_cell =
+ (int32)(loader_ctx->stack_cell_num
+ - cur_block->stack_cell_num);
+
+ if (available_stack_cell <= 0
+ && !cur_block->is_stack_polymorphic) {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch, opcode drop was found "
+ "but stack was empty");
+ goto fail;
+ }
+
+ if (available_stack_cell > 0) {
+#if WASM_ENABLE_GC != 0
+ if (wasm_is_type_multi_byte_type(
+ *(loader_ctx->frame_ref - 1))) {
+ bh_assert((int32)(loader_ctx->reftype_map_num
+ - cur_block->reftype_map_num)
+ > 0);
+ loader_ctx->frame_reftype_map--;
+ loader_ctx->reftype_map_num--;
+ }
+#endif
+ if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
+ loader_ctx->frame_ref--;
+ loader_ctx->stack_cell_num--;
+#if WASM_ENABLE_FAST_INTERP != 0
+ skip_label();
+ loader_ctx->frame_offset--;
+ if ((*(loader_ctx->frame_offset)
+ > loader_ctx->start_dynamic_offset)
+ && (*(loader_ctx->frame_offset)
+ < loader_ctx->max_dynamic_offset))
+ loader_ctx->dynamic_offset--;
+#endif
+ }
+ else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
+ loader_ctx->frame_ref -= 2;
+ loader_ctx->stack_cell_num -= 2;
+#if WASM_ENABLE_FAST_INTERP == 0
+ *(p - 1) = WASM_OP_DROP_64;
+#endif
+#if WASM_ENABLE_FAST_INTERP != 0
+ skip_label();
+ loader_ctx->frame_offset -= 2;
+ if ((*(loader_ctx->frame_offset)
+ > loader_ctx->start_dynamic_offset)
+ && (*(loader_ctx->frame_offset)
+ < loader_ctx->max_dynamic_offset))
+ loader_ctx->dynamic_offset -= 2;
+#endif
+ }
+#if WASM_ENABLE_SIMD != 0
+#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
+ else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
+ loader_ctx->frame_ref -= 4;
+ loader_ctx->stack_cell_num -= 4;
+ }
+#endif
+#endif
+ else {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch");
+ goto fail;
+ }
+ }
+ else {
+#if WASM_ENABLE_FAST_INTERP != 0
+ skip_label();
+#endif
+ }
+ break;
+ }
+
+ case WASM_OP_SELECT:
+ {
+ uint8 ref_type;
+ BranchBlock *cur_block = loader_ctx->frame_csp - 1;
+ int32 available_stack_cell;
+#if WASM_ENABLE_FAST_INTERP != 0
+ uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
+#endif
+
+ POP_I32();
+
+ available_stack_cell = (int32)(loader_ctx->stack_cell_num
+ - cur_block->stack_cell_num);
+
+ if (available_stack_cell <= 0
+ && !cur_block->is_stack_polymorphic) {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch or invalid result arity, "
+ "opcode select was found "
+ "but stack was empty");
+ goto fail;
+ }
+
+ if (available_stack_cell > 0) {
+ switch (*(loader_ctx->frame_ref - 1)) {
+ case VALUE_TYPE_I32:
+ case VALUE_TYPE_F32:
+ case VALUE_TYPE_ANY:
+ break;
+ case VALUE_TYPE_I64:
+ case VALUE_TYPE_F64:
+#if WASM_ENABLE_FAST_INTERP == 0
+ *(p - 1) = WASM_OP_SELECT_64;
+#endif
+#if WASM_ENABLE_FAST_INTERP != 0
+ if (loader_ctx->p_code_compiled) {
+ uint8 opcode_tmp = WASM_OP_SELECT_64;
+#if WASM_ENABLE_LABELS_AS_VALUES != 0
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
+ *(void **)(p_code_compiled_tmp
+ - sizeof(void *)) =
+ handle_table[opcode_tmp];
+#else
+#if UINTPTR_MAX == UINT64_MAX
+ /* emit int32 relative offset in 64-bit target
+ */
+ int32 offset =
+ (int32)((uint8 *)handle_table[opcode_tmp]
+ - (uint8 *)handle_table[0]);
+ *(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
+#endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
+#else /* else of WASM_ENABLE_LABELS_AS_VALUES */
+#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 /* end of WASM_ENABLE_FAST_INTERP */
+ break;
+#if WASM_ENABLE_SIMD != 0
+#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
+ case VALUE_TYPE_V128:
+ break;
+#endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
+#endif /* WASM_ENABLE_SIMD != 0 */
+ default:
+ {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch");
+ goto fail;
+ }
+ }
+
+ 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);
+#else
+ POP2_AND_PUSH(ref_type, ref_type);
+#endif
+ }
+ else {
+#if WASM_ENABLE_FAST_INTERP != 0
+ PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
+#endif
+ PUSH_TYPE(VALUE_TYPE_ANY);
+ }
+ break;
+ }
+
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+ case WASM_OP_SELECT_T:
+ {
+ uint8 vec_len, type;
+#if WASM_ENABLE_GC != 0
+ WASMRefType *ref_type = NULL;
+#endif
+#if WASM_ENABLE_FAST_INTERP != 0
+ uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
+#endif
+
+ read_leb_uint32(p, p_end, vec_len);
+ if (vec_len != 1) {
+ /* typed select must have exactly one result */
+ set_error_buf(error_buf, error_buf_size,
+ "invalid result arity");
+ goto fail;
+ }
+
+#if WASM_ENABLE_GC == 0
+ CHECK_BUF(p, p_end, 1);
+ type = read_uint8(p);
+ if (!is_valid_value_type_for_interpreter(type)) {
+ set_error_buf(error_buf, error_buf_size,
+ "unknown value type");
+ goto fail;
+ }
+#else
+ p_org = p + 1;
+ if (!resolve_value_type((const uint8 **)&p, p_end, module,
+ module->type_count, &need_ref_type_map,
+ &wasm_ref_type, false, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+ type = wasm_ref_type.ref_type;
+ if (need_ref_type_map) {
+ if (!(ref_type = reftype_set_insert(
+ module->ref_type_set, &wasm_ref_type, error_buf,
+ error_buf_size))) {
+ goto fail;
+ }
+ }
+#if WASM_ENABLE_FAST_INTERP == 0
+ while (p_org < p) {
+#if WASM_ENABLE_DEBUG_INTERP != 0
+ if (!record_fast_op(module, p_org, *p_org, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+#endif
+ /* Ignore extra bytes for interpreter */
+ *p_org++ = WASM_OP_NOP;
+ }
+#endif
+#endif /* end of WASM_ENABLE_GC == 0 */
+
+ POP_I32();
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ if (loader_ctx->p_code_compiled) {
+ uint8 opcode_tmp = WASM_OP_SELECT;
+
+ if (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 (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
+ opcode_tmp = WASM_OP_SELECT_64;
+#if WASM_ENABLE_GC != 0
+ if (wasm_is_type_reftype(type))
+ opcode_tmp = WASM_OP_SELECT_T;
+#endif
+#if WASM_ENABLE_LABELS_AS_VALUES != 0
+#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
+ *(void **)(p_code_compiled_tmp - sizeof(void *)) =
+ handle_table[opcode_tmp];
+#else
+#if UINTPTR_MAX == UINT64_MAX
+ /* emit int32 relative offset in 64-bit target */
+ int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
+ - (uint8 *)handle_table[0]);
+ *(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
+#endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
+#else /* else of WASM_ENABLE_LABELS_AS_VALUES */
+#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 */
+
+ POP_REF(type);
+
+#if WASM_ENABLE_GC != 0
+ if (need_ref_type_map) {
+ bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
+ wasm_reftype_struct_size(ref_type));
+ }
+#endif
+ POP_REF(type);
+
+#if WASM_ENABLE_GC != 0
+ if (need_ref_type_map) {
+ bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
+ wasm_reftype_struct_size(ref_type));
+ }
+#endif
+ PUSH_REF(type);
+
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ module->is_ref_types_used = true;
+#endif
+ (void)vec_len;
+ break;
+ }
+
+ /* table.get x. tables[x]. [it] -> [t] */
+ /* table.set x. tables[x]. [it t] -> [] */
+ case WASM_OP_TABLE_GET:
+ case WASM_OP_TABLE_SET:
+ {
+ uint8 decl_ref_type;
+#if WASM_ENABLE_GC != 0
+ WASMRefType *ref_type;
+#endif
+
+ read_leb_uint32(p, p_end, table_idx);
+ if (!get_table_elem_type(module, table_idx, &decl_ref_type,
+#if WASM_ENABLE_GC != 0
+ (void **)&ref_type,
+#else
+ NULL,
+#endif
+ error_buf, error_buf_size))
+ goto fail;
+
+#if WASM_ENABLE_GC != 0
+ if (wasm_is_type_multi_byte_type(decl_ref_type)) {
+ bh_assert(ref_type);
+ bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
+ ref_type, wasm_reftype_struct_size(ref_type));
+ }
+#endif
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, table_idx);
+#endif
+
+#if WASM_ENABLE_MEMORY64 != 0
+ table_elem_idx_type = is_table_64bit(module, table_idx)
+ ? VALUE_TYPE_I64
+ : VALUE_TYPE_I32;
+#endif
+ if (opcode == WASM_OP_TABLE_GET) {
+ POP_TBL_ELEM_IDX();
+#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_TBL_ELEM_IDX();
+ }
+
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ module->is_ref_types_used = true;
+#endif
+ break;
+ }
+ case WASM_OP_REF_NULL:
+ {
+ uint8 ref_type;
+
+#if WASM_ENABLE_GC == 0
+ 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, "type mismatch");
+ goto fail;
+ }
+#else
+ read_leb_int32(p, p_end, heap_type);
+ if (heap_type >= 0) {
+ if (!check_type_index(module, module->type_count, heap_type,
+ error_buf, error_buf_size)) {
+ goto fail;
+ }
+ wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
+ true, heap_type);
+ ref_type = wasm_ref_type.ref_type;
+ }
+ else {
+ if (!wasm_is_valid_heap_type(heap_type)) {
+ set_error_buf(error_buf, error_buf_size,
+ "unknown type");
+ goto fail;
+ }
+ ref_type = (uint8)((int32)0x80 + heap_type);
+ }
+#endif /* end of WASM_ENABLE_GC == 0 */
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ PUSH_OFFSET_TYPE(ref_type);
+#endif
+ PUSH_TYPE(ref_type);
+
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ module->is_ref_types_used = true;
+#endif
+ break;
+ }
+ case WASM_OP_REF_IS_NULL:
+ {
+#if WASM_ENABLE_GC == 0
+#if WASM_ENABLE_FAST_INTERP != 0
+ BranchBlock *cur_block = loader_ctx->frame_csp - 1;
+ int32 block_stack_cell_num =
+ (int32)(loader_ctx->stack_cell_num
+ - cur_block->stack_cell_num);
+ if (block_stack_cell_num <= 0) {
+ if (!cur_block->is_stack_polymorphic) {
+ set_error_buf(
+ error_buf, error_buf_size,
+ "type mismatch: expect data but stack was empty");
+ goto fail;
+ }
+ }
+ else {
+ if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
+ || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
+ || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
+ if (!wasm_loader_pop_frame_ref_offset(
+ loader_ctx, *(loader_ctx->frame_ref - 1),
+ error_buf, error_buf_size)) {
+ goto fail;
+ }
+ }
+ else {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch");
+ goto fail;
+ }
+ }
+#else
+ 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)) {
+ goto fail;
+ }
+#endif
+#else /* else of WASM_ENABLE_GC == 0 */
+ uint8 type;
+ if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
+ error_buf, error_buf_size)) {
+ goto fail;
+ }
+#endif
+ PUSH_I32();
+
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ module->is_ref_types_used = true;
+#endif
+ 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;
+ }
+
+ /* Refer to a forward-declared function:
+ the function must be an import, exported, or present in
+ a table elem segment or global initializer to be used as
+ the operand to ref.func */
+ if (func_idx >= module->import_function_count) {
+ WASMTableSeg *table_seg = module->table_segments;
+ bool func_declared = false;
+ uint32 j;
+
+ for (i = 0; i < module->global_count; i++) {
+ if (module->globals[i].type.val_type
+ == VALUE_TYPE_FUNCREF
+ && module->globals[i].init_expr.init_expr_type
+ == INIT_EXPR_TYPE_FUNCREF_CONST
+ && module->globals[i].init_expr.u.u32 == func_idx) {
+ func_declared = true;
+ break;
+ }
+ }
+
+ if (!func_declared) {
+ /* Check whether the function is declared in table segs,
+ note that it doesn't matter whether the table seg's
+ mode is passive, active or declarative. */
+ for (i = 0; i < module->table_seg_count;
+ i++, table_seg++) {
+ if (table_seg->elem_type == VALUE_TYPE_FUNCREF
+#if WASM_ENABLE_GC != 0
+ /* elem type is (ref null? func) or
+ (ref null? $t) */
+ || ((table_seg->elem_type
+ == REF_TYPE_HT_NON_NULLABLE
+ || table_seg->elem_type
+ == REF_TYPE_HT_NULLABLE)
+ && (table_seg->elem_ref_type->ref_ht_common
+ .heap_type
+ == HEAP_TYPE_FUNC
+ || table_seg->elem_ref_type
+ ->ref_ht_common.heap_type
+ > 0))
+#endif
+ ) {
+ for (j = 0; j < table_seg->value_count; j++) {
+ if (table_seg->init_values[j].u.ref_index
+ == func_idx) {
+ func_declared = true;
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ if (!func_declared) {
+ /* 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) {
+ 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
+#if WASM_ENABLE_GC == 0
+ PUSH_FUNCREF();
+#else
+ if (func_idx < module->import_function_count)
+ type_idx =
+ module->import_functions[func_idx].u.function.type_idx;
+ else
+ type_idx = module
+ ->functions[func_idx
+ - module->import_function_count]
+ ->type_idx;
+ wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
+ false, type_idx);
+ PUSH_REF(wasm_ref_type.ref_type);
+#endif
+
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ module->is_ref_types_used = true;
+#endif
+ break;
+ }
+#endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
+
+#if WASM_ENABLE_GC != 0
+ case WASM_OP_REF_AS_NON_NULL:
+ case WASM_OP_BR_ON_NULL:
+ {
+ uint8 type;
+ WASMRefType ref_type;
+
+ /* POP (ref null ht) and get the converted (ref ht) */
+ if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
+ error_buf, error_buf_size)) {
+ goto fail;
+ }
+
+ if (opcode == WASM_OP_BR_ON_NULL) {
+ if (!(frame_csp_tmp =
+ check_branch_block(loader_ctx, &p, p_end, opcode,
+ error_buf, error_buf_size))) {
+ goto fail;
+ }
+#if WASM_ENABLE_FAST_INTERP != 0
+ disable_emit = true;
+#endif
+ }
+
+ /* PUSH the converted (ref ht) */
+ if (type != VALUE_TYPE_ANY) {
+ bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
+ sizeof(WASMRefType));
+ }
+ PUSH_REF(type);
+ break;
+ }
+
+ case WASM_OP_BR_ON_NON_NULL:
+ {
+ uint8 type;
+ WASMRefType ref_type;
+ uint32 available_stack_cell =
+ loader_ctx->stack_cell_num
+ - (loader_ctx->frame_csp - 1)->stack_cell_num;
+
+ /* POP (ref null ht) and get the converted (ref ht) */
+ if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
+ error_buf, error_buf_size)) {
+ goto fail;
+ }
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ disable_emit = true;
+#endif
+
+ /* Temporarily PUSH back (ref ht), check brach block and
+ then POP it */
+ if (available_stack_cell
+ > 0) { /* stack isn't in polymorphic state */
+ if (type != VALUE_TYPE_ANY) {
+ bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
+ &ref_type, sizeof(WASMRefType));
+ }
+ PUSH_REF(type);
+ }
+ if (!(frame_csp_tmp =
+ check_branch_block(loader_ctx, &p, p_end, opcode,
+ error_buf, error_buf_size))) {
+ goto fail;
+ }
+ if (available_stack_cell
+ > 0) { /* stack isn't in polymorphic state */
+ POP_REF(type);
+#if WASM_ENABLE_FAST_INTERP != 0
+ /* Erase the opnd offset emitted by POP_REF() */
+ wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
+#endif
+ }
+ break;
+ }
+
+ case WASM_OP_REF_EQ:
+ POP_REF(REF_TYPE_EQREF);
+ POP_REF(REF_TYPE_EQREF);
+ PUSH_I32();
+ break;
+#endif /* end of WASM_ENABLE_GC != 0 */
+
+ case WASM_OP_GET_LOCAL:
+ {
+ p_org = p - 1;
+ GET_LOCAL_INDEX_TYPE_AND_OFFSET();
+ PUSH_TYPE(local_type);
+
+#if WASM_ENABLE_GC != 0
+ /* Cannot get a non-nullable and unset local */
+ if (local_idx >= param_count
+ && wasm_is_reftype_htref_non_nullable(local_type)
+ && !wasm_loader_get_local_status(loader_ctx,
+ local_idx - param_count)) {
+ set_error_buf(error_buf, error_buf_size,
+ "uninitialized local");
+ goto fail;
+ }
+#endif
+
+#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
+#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
+ && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
+ if (local_offset < 0x80
+#if WASM_ENABLE_GC != 0
+ && !wasm_is_type_reftype(local_type)
+#endif
+ ) {
+ *p_org++ = EXT_OP_GET_LOCAL_FAST;
+ if (is_32bit_type(local_type)) {
+ *p_org++ = (uint8)local_offset;
+ }
+ else {
+ *p_org++ = (uint8)(local_offset | 0x80);
+ }
+ while (p_org < p) {
+ *p_org++ = WASM_OP_NOP;
+ }
+ }
+#endif
+#endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
+ break;
+ }
+
+ case WASM_OP_SET_LOCAL:
+ {
+ p_org = p - 1;
+ GET_LOCAL_INDEX_TYPE_AND_OFFSET();
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ if (!(preserve_referenced_local(
+ loader_ctx, opcode, local_offset, local_type,
+ &preserve_local, error_buf, error_buf_size)))
+ goto fail;
+
+ if (local_offset < 256
+#if WASM_ENABLE_GC != 0
+ && !wasm_is_type_reftype(local_type)
+#endif
+ ) {
+ skip_label();
+ if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
+ if (loader_ctx->p_code_compiled)
+ STORE_U16(loader_ctx->p_code_compiled - 2,
+ local_offset);
+ loader_ctx->frame_offset--;
+ loader_ctx->dynamic_offset--;
+ }
+ else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
+ if (loader_ctx->p_code_compiled)
+ STORE_U16(loader_ctx->p_code_compiled - 2,
+ local_offset);
+ loader_ctx->frame_offset -= 2;
+ loader_ctx->dynamic_offset -= 2;
+ }
+ else {
+ if (is_32bit_type(local_type)) {
+ emit_label(EXT_OP_SET_LOCAL_FAST);
+ emit_byte(loader_ctx, (uint8)local_offset);
+ }
+ else {
+ emit_label(EXT_OP_SET_LOCAL_FAST_I64);
+ emit_byte(loader_ctx, (uint8)local_offset);
+ }
+ POP_OFFSET_TYPE(local_type);
+ }
+ }
+ else { /* local index larger than 255, reserve leb */
+ emit_uint32(loader_ctx, local_idx);
+ POP_OFFSET_TYPE(local_type);
+ }
+#else
+#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
+ && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
+ if (local_offset < 0x80
+#if WASM_ENABLE_GC != 0
+ && !wasm_is_type_reftype(local_type)
+#endif
+ ) {
+ *p_org++ = EXT_OP_SET_LOCAL_FAST;
+ if (is_32bit_type(local_type)) {
+ *p_org++ = (uint8)local_offset;
+ }
+ else {
+ *p_org++ = (uint8)(local_offset | 0x80);
+ }
+ while (p_org < p) {
+ *p_org++ = WASM_OP_NOP;
+ }
+ }
+#endif
+#endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
+
+#if WASM_ENABLE_GC != 0
+ if (local_idx >= param_count) {
+ wasm_loader_mask_local(loader_ctx, local_idx - param_count);
+ }
+#endif
+
+ POP_TYPE(local_type);
+ break;
+ }
+
+ case WASM_OP_TEE_LOCAL:
+ {
+ p_org = p - 1;
+ GET_LOCAL_INDEX_TYPE_AND_OFFSET();
+#if WASM_ENABLE_FAST_INTERP != 0
+ /* If the stack is in polymorphic state, do fake pop and push on
+ offset stack to keep the depth of offset stack to be the
+ same with ref stack */
+ 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
+ POP_TYPE(local_type);
+ PUSH_TYPE(local_type);
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ if (!(preserve_referenced_local(
+ loader_ctx, opcode, local_offset, local_type,
+ &preserve_local, error_buf, error_buf_size)))
+ goto fail;
+
+ if (local_offset < 256
+#if WASM_ENABLE_GC != 0
+ && !wasm_is_type_reftype(local_type)
+#endif
+ ) {
+ skip_label();
+ if (is_32bit_type(local_type)) {
+ emit_label(EXT_OP_TEE_LOCAL_FAST);
+ emit_byte(loader_ctx, (uint8)local_offset);
+ }
+ else {
+ emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
+ emit_byte(loader_ctx, (uint8)local_offset);
+ }
+ }
+ else { /* local index larger than 255, reserve leb */
+ emit_uint32(loader_ctx, local_idx);
+ }
+ emit_operand(loader_ctx,
+ *(loader_ctx->frame_offset
+ - wasm_value_type_cell_num(local_type)));
+#else
+#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
+ && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
+ if (local_offset < 0x80
+#if WASM_ENABLE_GC != 0
+ && !wasm_is_type_reftype(local_type)
+#endif
+ ) {
+ *p_org++ = EXT_OP_TEE_LOCAL_FAST;
+ if (is_32bit_type(local_type)) {
+ *p_org++ = (uint8)local_offset;
+ }
+ else {
+ *p_org++ = (uint8)(local_offset | 0x80);
+ }
+ while (p_org < p) {
+ *p_org++ = WASM_OP_NOP;
+ }
+ }
+#endif
+#endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
+
+#if WASM_ENABLE_GC != 0
+ if (local_idx >= param_count) {
+ wasm_loader_mask_local(loader_ctx, local_idx - param_count);
+ }
+#endif
+ break;
+ }
+
+ case WASM_OP_GET_GLOBAL:
+ {
+#if WASM_ENABLE_GC != 0
+ WASMRefType *ref_type;
+#endif
+
+ p_org = p - 1;
+ read_leb_uint32(p, p_end, global_idx);
+ if (global_idx >= global_count) {
+ set_error_buf(error_buf, error_buf_size, "unknown global");
+ goto fail;
+ }
+
+ global_type = global_idx < module->import_global_count
+ ? module->import_globals[global_idx]
+ .u.global.type.val_type
+ : module
+ ->globals[global_idx
+ - module->import_global_count]
+ .type.val_type;
+#if WASM_ENABLE_GC != 0
+ ref_type =
+ global_idx < module->import_global_count
+ ? module->import_globals[global_idx].u.global.ref_type
+ : module
+ ->globals[global_idx
+ - module->import_global_count]
+ .ref_type;
+ if (wasm_is_type_multi_byte_type(global_type)) {
+ bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
+ wasm_reftype_struct_size(ref_type));
+ }
+#endif
+
+ PUSH_TYPE(global_type);
+
+#if WASM_ENABLE_FAST_INTERP == 0
+ if (global_type == VALUE_TYPE_I64
+ || global_type == VALUE_TYPE_F64) {
+#if WASM_ENABLE_DEBUG_INTERP != 0
+ if (!record_fast_op(module, p_org, *p_org, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+#endif
+ *p_org = WASM_OP_GET_GLOBAL_64;
+ }
+#else /* else of WASM_ENABLE_FAST_INTERP */
+ if (global_type == VALUE_TYPE_I64
+ || global_type == VALUE_TYPE_F64) {
+ skip_label();
+ emit_label(WASM_OP_GET_GLOBAL_64);
+ }
+ emit_uint32(loader_ctx, global_idx);
+ PUSH_OFFSET_TYPE(global_type);
+#endif /* end of WASM_ENABLE_FAST_INTERP */
+ break;
+ }
+
+ case WASM_OP_SET_GLOBAL:
+ {
+ bool is_mutable = false;
+#if WASM_ENABLE_GC != 0
+ WASMRefType *ref_type;
+#endif
+
+ p_org = p - 1;
+ read_leb_uint32(p, p_end, global_idx);
+ if (global_idx >= global_count) {
+ set_error_buf(error_buf, error_buf_size, "unknown global");
+ goto fail;
+ }
+
+ is_mutable = global_idx < module->import_global_count
+ ? module->import_globals[global_idx]
+ .u.global.type.is_mutable
+ : module
+ ->globals[global_idx
+ - module->import_global_count]
+ .type.is_mutable;
+ if (!is_mutable) {
+#if WASM_ENABLE_GC == 0
+ set_error_buf(error_buf, error_buf_size,
+ "global is immutable");
+#else
+ set_error_buf(error_buf, error_buf_size,
+ "immutable global");
+#endif
+ goto fail;
+ }
+
+ global_type = global_idx < module->import_global_count
+ ? module->import_globals[global_idx]
+ .u.global.type.val_type
+ : module
+ ->globals[global_idx
+ - module->import_global_count]
+ .type.val_type;
+#if WASM_ENABLE_GC != 0
+ ref_type =
+ global_idx < module->import_global_count
+ ? module->import_globals[global_idx].u.global.ref_type
+ : module
+ ->globals[global_idx
+ - module->import_global_count]
+ .ref_type;
+ if (wasm_is_type_multi_byte_type(global_type)) {
+ bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
+ wasm_reftype_struct_size(ref_type));
+ }
+#endif
+
+#if WASM_ENABLE_FAST_INTERP == 0
+ if (global_type == VALUE_TYPE_I64
+ || global_type == VALUE_TYPE_F64) {
+#if WASM_ENABLE_DEBUG_INTERP != 0
+ if (!record_fast_op(module, p_org, *p_org, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+#endif
+ *p_org = WASM_OP_SET_GLOBAL_64;
+ }
+ else if (module->aux_stack_size > 0
+ && global_idx == module->aux_stack_top_global_index) {
+#if WASM_ENABLE_DEBUG_INTERP != 0
+ if (!record_fast_op(module, p_org, *p_org, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+#endif
+ *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
+#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
+ func->has_op_set_global_aux_stack = true;
+#endif
+ }
+#else /* else of WASM_ENABLE_FAST_INTERP */
+ if (global_type == VALUE_TYPE_I64
+ || global_type == VALUE_TYPE_F64) {
+ skip_label();
+ emit_label(WASM_OP_SET_GLOBAL_64);
+ }
+ else if (module->aux_stack_size > 0
+ && global_idx == module->aux_stack_top_global_index) {
+ skip_label();
+ emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
+ }
+ emit_uint32(loader_ctx, global_idx);
+ POP_OFFSET_TYPE(global_type);
+#endif /* end of WASM_ENABLE_FAST_INTERP */
+
+ POP_TYPE(global_type);
+
+ break;
+ }
+
+ /* 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:
+ 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:
+ /* store */
+ 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:
+ {
+#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
+ CHECK_MEMORY();
+ read_leb_memarg(p, p_end, align); /* align */
+ read_leb_mem_offset(p, p_end, mem_offset); /* offset */
+ if (!check_memory_access_align(opcode, align, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, mem_offset);
+#endif
+#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
+ func->has_memory_operations = true;
+#endif
+ switch (opcode) {
+ /* 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:
+ POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
+ 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:
+ POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
+ break;
+ case WASM_OP_F32_LOAD:
+ POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
+ break;
+ case WASM_OP_F64_LOAD:
+ POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
+ break;
+ /* store */
+ case WASM_OP_I32_STORE:
+ case WASM_OP_I32_STORE8:
+ case WASM_OP_I32_STORE16:
+ POP_I32();
+ POP_MEM_OFFSET();
+ break;
+ case WASM_OP_I64_STORE:
+ case WASM_OP_I64_STORE8:
+ case WASM_OP_I64_STORE16:
+ case WASM_OP_I64_STORE32:
+ POP_I64();
+ POP_MEM_OFFSET();
+ break;
+ case WASM_OP_F32_STORE:
+ POP_F32();
+ POP_MEM_OFFSET();
+ break;
+ case WASM_OP_F64_STORE:
+ POP_F64();
+ POP_MEM_OFFSET();
+ break;
+ default:
+ break;
+ }
+ break;
+ }
+
+ case WASM_OP_MEMORY_SIZE:
+ CHECK_MEMORY();
+ read_leb_uint32(p, p_end, memidx);
+ check_memidx(module, memidx);
+ PUSH_PAGE_COUNT();
+
+ module->possible_memory_grow = true;
+#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
+ func->has_memory_operations = true;
+#endif
+ break;
+
+ case WASM_OP_MEMORY_GROW:
+ CHECK_MEMORY();
+ read_leb_uint32(p, p_end, memidx);
+ check_memidx(module, memidx);
+ POP_AND_PUSH(mem_offset_type, mem_offset_type);
+
+ module->possible_memory_grow = true;
+#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
+ break;
+
+ case WASM_OP_I32_CONST:
+ read_leb_int32(p, p_end, i32_const);
+#if WASM_ENABLE_FAST_INTERP != 0
+ skip_label();
+ disable_emit = true;
+ GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
+
+ if (operand_offset == 0) {
+ disable_emit = false;
+ emit_label(WASM_OP_I32_CONST);
+ emit_uint32(loader_ctx, i32_const);
+ }
+#else
+ (void)i32_const;
+#endif
+ PUSH_I32();
+ break;
+
+ case WASM_OP_I64_CONST:
+ read_leb_int64(p, p_end, i64_const);
+#if WASM_ENABLE_FAST_INTERP != 0
+ skip_label();
+ disable_emit = true;
+ GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
+
+ if (operand_offset == 0) {
+ disable_emit = false;
+ emit_label(WASM_OP_I64_CONST);
+ emit_uint64(loader_ctx, i64_const);
+ }
+#endif
+ PUSH_I64();
+ break;
+
+ case WASM_OP_F32_CONST:
+ CHECK_BUF(p, p_end, sizeof(float32));
+ p += sizeof(float32);
+#if WASM_ENABLE_FAST_INTERP != 0
+ skip_label();
+ disable_emit = true;
+ bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
+ sizeof(float32));
+ GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
+
+ if (operand_offset == 0) {
+ disable_emit = false;
+ emit_label(WASM_OP_F32_CONST);
+ emit_float32(loader_ctx, f32_const);
+ }
+#endif
+ PUSH_F32();
+ break;
+
+ case WASM_OP_F64_CONST:
+ CHECK_BUF(p, p_end, sizeof(float64));
+ p += sizeof(float64);
+#if WASM_ENABLE_FAST_INTERP != 0
+ skip_label();
+ disable_emit = true;
+ /* Some MCU may require 8-byte align */
+ bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
+ sizeof(float64));
+ GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
+
+ if (operand_offset == 0) {
+ disable_emit = false;
+ emit_label(WASM_OP_F64_CONST);
+ emit_float64(loader_ctx, f64_const);
+ }
+#endif
+ PUSH_F64();
+ break;
+
+ case WASM_OP_I32_EQZ:
+ POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
+ 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:
+ POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
+ break;
+
+ case WASM_OP_I64_EQZ:
+ POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
+ 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:
+ POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
+ 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:
+ POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
+ 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:
+ POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
+ break;
+
+ case WASM_OP_I32_CLZ:
+ case WASM_OP_I32_CTZ:
+ case WASM_OP_I32_POPCNT:
+ POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
+ 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:
+ POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
+ break;
+
+ case WASM_OP_I64_CLZ:
+ case WASM_OP_I64_CTZ:
+ case WASM_OP_I64_POPCNT:
+ POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
+ 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:
+ POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
+ 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:
+ POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
+ 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:
+ POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
+ 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:
+ POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
+ 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:
+ POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
+ break;
+
+ case WASM_OP_I32_WRAP_I64:
+ POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
+ break;
+
+ case WASM_OP_I32_TRUNC_S_F32:
+ case WASM_OP_I32_TRUNC_U_F32:
+ POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
+ break;
+
+ case WASM_OP_I32_TRUNC_S_F64:
+ case WASM_OP_I32_TRUNC_U_F64:
+ POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
+ break;
+
+ case WASM_OP_I64_EXTEND_S_I32:
+ case WASM_OP_I64_EXTEND_U_I32:
+ POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
+ break;
+
+ case WASM_OP_I64_TRUNC_S_F32:
+ case WASM_OP_I64_TRUNC_U_F32:
+ POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
+ break;
+
+ case WASM_OP_I64_TRUNC_S_F64:
+ case WASM_OP_I64_TRUNC_U_F64:
+ POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
+ break;
+
+ case WASM_OP_F32_CONVERT_S_I32:
+ case WASM_OP_F32_CONVERT_U_I32:
+ POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
+ break;
+
+ case WASM_OP_F32_CONVERT_S_I64:
+ case WASM_OP_F32_CONVERT_U_I64:
+ POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
+ break;
+
+ case WASM_OP_F32_DEMOTE_F64:
+ POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
+ break;
+
+ case WASM_OP_F64_CONVERT_S_I32:
+ case WASM_OP_F64_CONVERT_U_I32:
+ POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
+ break;
+
+ case WASM_OP_F64_CONVERT_S_I64:
+ case WASM_OP_F64_CONVERT_U_I64:
+ POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
+ break;
+
+ case WASM_OP_F64_PROMOTE_F32:
+ POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
+ break;
+
+ case WASM_OP_I32_REINTERPRET_F32:
+ POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
+ break;
+
+ case WASM_OP_I64_REINTERPRET_F64:
+ POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
+ break;
+
+ case WASM_OP_F32_REINTERPRET_I32:
+ POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
+ break;
+
+ case WASM_OP_F64_REINTERPRET_I64:
+ POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
+ break;
+
+ case WASM_OP_I32_EXTEND8_S:
+ case WASM_OP_I32_EXTEND16_S:
+ POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
+ break;
+
+ case WASM_OP_I64_EXTEND8_S:
+ case WASM_OP_I64_EXTEND16_S:
+ case WASM_OP_I64_EXTEND32_S:
+ POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
+ break;
+
+#if WASM_ENABLE_GC != 0
+ case WASM_OP_GC_PREFIX:
+ {
+ uint32 opcode1;
+
+ read_leb_uint32(p, p_end, opcode1);
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_byte(loader_ctx, ((uint8)opcode1));
+#endif
+
+ switch (opcode1) {
+ case WASM_OP_STRUCT_NEW:
+ case WASM_OP_STRUCT_NEW_DEFAULT:
+ {
+ read_leb_uint32(p, p_end, type_idx);
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, type_idx);
+#endif
+ if (!check_type_index(module, module->type_count,
+ type_idx, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+ if (module->types[type_idx]->type_flag
+ != WASM_TYPE_STRUCT) {
+ set_error_buf(error_buf, error_buf_size,
+ "unkown struct type");
+ goto fail;
+ }
+
+ if (opcode1 == WASM_OP_STRUCT_NEW) {
+ int32 j, k;
+ uint8 value_type;
+ uint32 ref_type_struct_size;
+ WASMStructType *struct_type =
+ (WASMStructType *)module->types[type_idx];
+
+ k = struct_type->ref_type_map_count - 1;
+ for (j = struct_type->field_count - 1; j >= 0;
+ j--) {
+ value_type = struct_type->fields[j].field_type;
+ if (wasm_is_type_reftype(value_type)) {
+ if (wasm_is_type_multi_byte_type(
+ value_type)) {
+ ref_type_struct_size =
+ wasm_reftype_struct_size(
+ struct_type->ref_type_maps[k]
+ .ref_type);
+ bh_memcpy_s(
+ &wasm_ref_type,
+ (uint32)sizeof(WASMRefType),
+ struct_type->ref_type_maps[k]
+ .ref_type,
+ ref_type_struct_size);
+ k--;
+ }
+ POP_REF(value_type);
+ }
+ else {
+ switch (value_type) {
+ case VALUE_TYPE_I32:
+ case PACKED_TYPE_I8:
+ case PACKED_TYPE_I16:
+ POP_I32();
+ break;
+ case VALUE_TYPE_I64:
+ POP_I64();
+ break;
+ case VALUE_TYPE_F32:
+ POP_F32();
+ break;
+ case VALUE_TYPE_F64:
+ POP_F64();
+ break;
+ default:
+ set_error_buf(error_buf,
+ error_buf_size,
+ "unknown type");
+ goto fail;
+ }
+ }
+ }
+ }
+
+ /* PUSH struct obj, (ref $t) */
+ wasm_set_refheaptype_typeidx(
+ &wasm_ref_type.ref_ht_typeidx, false, type_idx);
+ PUSH_REF(wasm_ref_type.ref_type);
+ break;
+ }
+
+ case WASM_OP_STRUCT_GET:
+ case WASM_OP_STRUCT_GET_S:
+ case WASM_OP_STRUCT_GET_U:
+ case WASM_OP_STRUCT_SET:
+ {
+ WASMStructType *struct_type;
+ WASMRefType *ref_type = NULL;
+ uint32 field_idx;
+ uint8 field_type;
+
+ read_leb_uint32(p, p_end, type_idx);
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, type_idx);
+#endif
+ if (!check_type_index(module, module->type_count,
+ type_idx, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+ if (module->types[type_idx]->type_flag
+ != WASM_TYPE_STRUCT) {
+ set_error_buf(error_buf, error_buf_size,
+ "unknown struct type");
+ goto fail;
+ }
+ struct_type = (WASMStructType *)module->types[type_idx];
+
+ read_leb_uint32(p, p_end, field_idx);
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, field_idx);
+#endif
+ if (field_idx >= struct_type->field_count) {
+ set_error_buf(error_buf, error_buf_size,
+ "unknown struct field");
+ goto fail;
+ }
+
+ if (opcode1 == WASM_OP_STRUCT_SET
+ && !(struct_type->fields[field_idx].field_flags
+ & 1)) {
+ set_error_buf(error_buf, error_buf_size,
+ "field is immutable");
+ goto fail;
+ }
+
+ field_type = struct_type->fields[field_idx].field_type;
+ if (is_packed_type(field_type)) {
+ if (opcode1 == WASM_OP_STRUCT_GET) {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch");
+ goto fail;
+ }
+ else {
+ field_type = VALUE_TYPE_I32;
+ }
+ }
+ if (wasm_is_type_multi_byte_type(field_type)) {
+ ref_type = wasm_reftype_map_find(
+ struct_type->ref_type_maps,
+ struct_type->ref_type_map_count, field_idx);
+ bh_assert(ref_type);
+ }
+ if (opcode1 == WASM_OP_STRUCT_SET) {
+ /* POP field */
+ if (wasm_is_type_multi_byte_type(field_type)) {
+ bh_memcpy_s(&wasm_ref_type,
+ (uint32)sizeof(WASMRefType),
+ ref_type,
+ wasm_reftype_struct_size(ref_type));
+ }
+ POP_REF(field_type);
+ /* POP struct obj, (ref null $t) */
+ wasm_set_refheaptype_typeidx(
+ &wasm_ref_type.ref_ht_typeidx, true, type_idx);
+ POP_REF(wasm_ref_type.ref_type);
+ }
+ else {
+ /* POP struct obj, (ref null $t) */
+ wasm_set_refheaptype_typeidx(
+ &wasm_ref_type.ref_ht_typeidx, true, type_idx);
+ POP_REF(wasm_ref_type.ref_type);
+ /* PUSH field */
+ if (wasm_is_type_multi_byte_type(field_type)) {
+ bh_memcpy_s(&wasm_ref_type,
+ (uint32)sizeof(WASMRefType),
+ ref_type,
+ wasm_reftype_struct_size(ref_type));
+ }
+ PUSH_REF(field_type);
+ }
+ break;
+ }
+
+ case WASM_OP_ARRAY_NEW:
+ case WASM_OP_ARRAY_NEW_DEFAULT:
+ case WASM_OP_ARRAY_NEW_FIXED:
+ case WASM_OP_ARRAY_NEW_DATA:
+ case WASM_OP_ARRAY_NEW_ELEM:
+ {
+ WASMArrayType *array_type;
+ uint8 elem_type;
+ uint32 u32 = 0;
+
+ read_leb_uint32(p, p_end, type_idx);
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, type_idx);
+#endif
+ if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
+ || opcode1 == WASM_OP_ARRAY_NEW_DATA
+ || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
+ read_leb_uint32(p, p_end, u32);
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, u32);
+#endif
+ }
+
+ if (!check_array_type(module, type_idx, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+
+ if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
+ /* length */
+ POP_I32();
+ }
+
+ array_type = (WASMArrayType *)module->types[type_idx];
+ elem_type = array_type->elem_type;
+
+ if (opcode1 == WASM_OP_ARRAY_NEW
+ || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
+ if (wasm_is_type_multi_byte_type(elem_type)) {
+ bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
+ array_type->elem_ref_type,
+ wasm_reftype_struct_size(
+ array_type->elem_ref_type));
+ }
+ if (is_packed_type(elem_type)) {
+ elem_type = VALUE_TYPE_I32;
+ }
+
+ if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
+ uint32 N = u32;
+ for (i = 0; i < N; i++) {
+ if (wasm_is_type_multi_byte_type(
+ elem_type)) {
+ bh_memcpy_s(
+ &wasm_ref_type, sizeof(WASMRefType),
+ array_type->elem_ref_type,
+ wasm_reftype_struct_size(
+ array_type->elem_ref_type));
+ }
+ POP_REF(elem_type);
+ }
+ }
+ else
+ POP_REF(elem_type);
+ }
+ else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
+ /* offset of data segment */
+ POP_I32();
+
+ if (u32 >= module->data_seg_count) {
+ set_error_buf(error_buf, error_buf_size,
+ "unknown data segment");
+ goto fail;
+ }
+
+ if (wasm_is_type_reftype(elem_type)) {
+ set_error_buf(error_buf, error_buf_size,
+ "array elem type mismatch");
+ goto fail;
+ }
+ }
+ else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
+ WASMTableSeg *table_seg =
+ module->table_segments + u32;
+
+ /* offset of element segment */
+ POP_I32();
+
+ if (u32 >= module->table_seg_count) {
+ set_error_buf(error_buf, error_buf_size,
+ "unknown element segment");
+ goto fail;
+ }
+ if (!wasm_reftype_is_subtype_of(
+ table_seg->elem_type,
+ table_seg->elem_ref_type, elem_type,
+ array_type->elem_ref_type, module->types,
+ module->type_count)) {
+ set_error_buf(error_buf, error_buf_size,
+ "array elem type mismatch");
+ goto fail;
+ }
+ }
+
+ /* PUSH array obj, (ref $t) */
+ wasm_set_refheaptype_typeidx(
+ &wasm_ref_type.ref_ht_typeidx, false, type_idx);
+ PUSH_REF(wasm_ref_type.ref_type);
+ break;
+ }
+
+ case WASM_OP_ARRAY_GET:
+ case WASM_OP_ARRAY_GET_S:
+ case WASM_OP_ARRAY_GET_U:
+ case WASM_OP_ARRAY_SET:
+ {
+ uint8 elem_type;
+ WASMArrayType *array_type;
+ WASMRefType *ref_type = NULL;
+
+ read_leb_uint32(p, p_end, type_idx);
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, type_idx);
+#endif
+ if (!check_array_type(module, type_idx, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+ array_type = (WASMArrayType *)module->types[type_idx];
+
+ if (opcode1 == WASM_OP_ARRAY_SET
+ && !(array_type->elem_flags & 1)) {
+ set_error_buf(error_buf, error_buf_size,
+ "array is immutable");
+ goto fail;
+ }
+
+ elem_type = array_type->elem_type;
+ if (is_packed_type(elem_type)) {
+ if (opcode1 != WASM_OP_ARRAY_GET_S
+ && opcode1 != WASM_OP_ARRAY_GET_U
+ && opcode1 != WASM_OP_ARRAY_SET) {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch");
+ goto fail;
+ }
+ else {
+ elem_type = VALUE_TYPE_I32;
+ }
+ }
+ ref_type = array_type->elem_ref_type;
+
+ if (opcode1 == WASM_OP_ARRAY_SET) {
+ /* POP elem to set */
+ if (wasm_is_type_multi_byte_type(elem_type)) {
+ bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
+ ref_type,
+ wasm_reftype_struct_size(ref_type));
+ }
+ POP_REF(elem_type);
+ }
+ /* elem idx */
+ POP_I32();
+ /* POP array obj, (ref null $t) */
+ wasm_set_refheaptype_typeidx(
+ &wasm_ref_type.ref_ht_typeidx, true, type_idx);
+ POP_REF(wasm_ref_type.ref_type);
+ if (opcode1 != WASM_OP_ARRAY_SET) {
+ /* PUSH elem */
+ if (wasm_is_type_multi_byte_type(elem_type)) {
+ bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
+ ref_type,
+ wasm_reftype_struct_size(ref_type));
+ }
+ PUSH_REF(elem_type);
+ }
+ break;
+ }
+
+ case WASM_OP_ARRAY_LEN:
+ {
+ POP_REF(REF_TYPE_ARRAYREF);
+ /* length */
+ PUSH_I32();
+ break;
+ }
+
+ case WASM_OP_ARRAY_FILL:
+ {
+ WASMArrayType *array_type;
+ uint8 elem_type;
+ /* typeidx */
+ read_leb_uint32(p, p_end, type_idx);
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, type_idx);
+#endif
+ if (!check_array_type(module, type_idx, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+
+ array_type = (WASMArrayType *)module->types[type_idx];
+ if (!(array_type->elem_flags & 1)) {
+ set_error_buf(error_buf, error_buf_size,
+ "array is immutable");
+ goto fail;
+ }
+
+ elem_type = array_type->elem_type;
+ if (is_packed_type(elem_type)) {
+ elem_type = VALUE_TYPE_I32;
+ }
+
+ POP_I32(); /* length */
+#if WASM_ENABLE_FAST_INTERP != 0
+ POP_OFFSET_TYPE(elem_type);
+#endif
+ POP_TYPE(elem_type);
+ POP_I32(); /* start */
+ /* POP array obj, (ref null $t) */
+ wasm_set_refheaptype_typeidx(
+ &wasm_ref_type.ref_ht_typeidx, true, type_idx);
+ POP_REF(wasm_ref_type.ref_type);
+
+ break;
+ }
+
+ case WASM_OP_ARRAY_COPY:
+ {
+ uint32 src_type_idx;
+ uint8 src_elem_type, dst_elem_type;
+ WASMRefType src_ref_type = { 0 },
+ *src_elem_ref_type = NULL;
+ WASMRefType dst_ref_type = { 0 },
+ *dst_elem_ref_type = NULL;
+ WASMArrayType *array_type;
+
+ /* typeidx1 */
+ read_leb_uint32(p, p_end, type_idx);
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, type_idx);
+#endif
+ /* typeidx2 */
+ read_leb_uint32(p, p_end, src_type_idx);
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, src_type_idx);
+#endif
+ if (!check_array_type(module, type_idx, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+
+ if (!check_array_type(module, src_type_idx, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+
+ POP_I32();
+ POP_I32();
+ /* POP array obj, (ref null $t) */
+ wasm_set_refheaptype_typeidx(
+ &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
+ POP_REF(wasm_ref_type.ref_type);
+ bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
+ &wasm_ref_type,
+ wasm_reftype_struct_size(&wasm_ref_type));
+ POP_I32();
+ /* POP array obj, (ref null $t) */
+ wasm_set_refheaptype_typeidx(
+ &wasm_ref_type.ref_ht_typeidx, true, type_idx);
+ POP_REF(wasm_ref_type.ref_type);
+ bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
+ &wasm_ref_type,
+ wasm_reftype_struct_size(&wasm_ref_type));
+
+ array_type = (WASMArrayType *)module->types[type_idx];
+ if (!(array_type->elem_flags & 1)) {
+ set_error_buf(error_buf, error_buf_size,
+ "destination array is immutable");
+ goto fail;
+ }
+
+ dst_elem_type = array_type->elem_type;
+ if (wasm_is_type_multi_byte_type(dst_elem_type)) {
+ dst_elem_ref_type = array_type->elem_ref_type;
+ }
+
+ array_type =
+ (WASMArrayType *)module->types[src_type_idx];
+ src_elem_type = array_type->elem_type;
+ if (wasm_is_type_multi_byte_type(src_elem_type)) {
+ src_elem_ref_type = array_type->elem_ref_type;
+ }
+
+ if (!wasm_reftype_is_subtype_of(
+ src_elem_type, src_elem_ref_type, dst_elem_type,
+ dst_elem_ref_type, module->types,
+ module->type_count)) {
+ set_error_buf(error_buf, error_buf_size,
+ "array types do not match");
+ goto fail;
+ }
+
+ break;
+ }
+
+ case WASM_OP_REF_I31:
+ {
+ POP_I32();
+ wasm_set_refheaptype_common(
+ &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
+ PUSH_REF(wasm_ref_type.ref_type);
+ break;
+ }
+
+ case WASM_OP_I31_GET_S:
+ case WASM_OP_I31_GET_U:
+ {
+ POP_REF(REF_TYPE_I31REF);
+ PUSH_I32();
+ break;
+ }
+
+ case WASM_OP_REF_TEST:
+ case WASM_OP_REF_CAST:
+ case WASM_OP_REF_TEST_NULLABLE:
+ case WASM_OP_REF_CAST_NULLABLE:
+ {
+ uint8 type;
+
+ read_leb_int32(p, p_end, heap_type);
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, (uint32)heap_type);
+#endif
+ if (heap_type >= 0) {
+ if (!check_type_index(module, module->type_count,
+ heap_type, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+ }
+ else {
+ if (!wasm_is_valid_heap_type(heap_type)) {
+ set_error_buf(error_buf, error_buf_size,
+ "unknown type");
+ goto fail;
+ }
+ }
+ if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
+ &wasm_ref_type, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+ if (opcode1 == WASM_OP_REF_TEST
+ || opcode1 == WASM_OP_REF_TEST_NULLABLE)
+ PUSH_I32();
+ else {
+ bool nullable =
+ (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
+ : false;
+ if (heap_type >= 0 || !nullable) {
+ wasm_set_refheaptype_typeidx(
+ &wasm_ref_type.ref_ht_typeidx, nullable,
+ heap_type);
+ PUSH_REF(wasm_ref_type.ref_type);
+ }
+ else {
+ PUSH_REF((uint8)((int32)0x80 + heap_type));
+ }
+ }
+ break;
+ }
+
+ case WASM_OP_BR_ON_CAST:
+ case WASM_OP_BR_ON_CAST_FAIL:
+ {
+ WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
+ ref_type2 = { 0 }, ref_type_diff = { 0 };
+ uint8 type_tmp, castflags;
+ uint32 depth;
+ int32 heap_type_dst;
+ bool src_nullable, dst_nullable;
+
+ CHECK_BUF(p, p_end, 1);
+ castflags = read_uint8(p);
+#if WASM_ENABLE_FAST_INTERP != 0
+ /* Emit heap_type firstly */
+ emit_byte(loader_ctx, castflags);
+#endif
+
+ p_org = p;
+ read_leb_uint32(p, p_end, depth);
+ read_leb_int32(p, p_end, heap_type);
+#if WASM_ENABLE_FAST_INTERP != 0
+ /* Emit heap_type firstly */
+ emit_uint32(loader_ctx, (uint32)heap_type);
+#endif
+ read_leb_int32(p, p_end, heap_type_dst);
+#if WASM_ENABLE_FAST_INTERP != 0
+ /* Emit heap_type firstly */
+ emit_uint32(loader_ctx, (uint32)heap_type_dst);
+#endif
+ (void)depth;
+
+ /*
+ * castflags should be 0~3:
+ * 0: (non-null, non-null)
+ * 1: (null, non-null)
+ * 2: (non-null, null)
+ * 3: (null, null)
+ */
+ if (castflags > 3) {
+ set_error_buf(error_buf, error_buf_size,
+ "invalid castflags");
+ break;
+ }
+ src_nullable =
+ (castflags == 1) || (castflags == 3) ? true : false;
+ dst_nullable =
+ (castflags == 2) || (castflags == 3) ? true : false;
+
+ /* Pop and backup the stack top's ref type */
+ if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
+ &ref_type_tmp, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+
+ /* The reference type rt1 must be valid */
+ if (!init_ref_type(module, &ref_type1, src_nullable,
+ heap_type, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+
+ /* The reference type rt2 must be valid. */
+ if (!init_ref_type(module, &ref_type2, dst_nullable,
+ heap_type_dst, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+
+ calculate_reftype_diff(&ref_type_diff, &ref_type1,
+ &ref_type2);
+
+ /* The reference type rt2 must match rt1. */
+ if (!wasm_reftype_is_subtype_of(
+ ref_type2.ref_type, &ref_type2,
+ ref_type1.ref_type, &ref_type1, module->types,
+ module->type_count)) {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch");
+ goto fail;
+ }
+
+ p = p_org;
+ /* Push ref type casted for branch block check */
+ if (opcode1 == WASM_OP_BR_ON_CAST) {
+ /* The reference type rt2 must match rt′. */
+ type_tmp = ref_type2.ref_type;
+ if (wasm_is_type_multi_byte_type(type_tmp)) {
+ bh_memcpy_s(
+ &wasm_ref_type,
+ wasm_reftype_struct_size(&ref_type2),
+ &ref_type2,
+ wasm_reftype_struct_size(&ref_type2));
+ }
+ }
+ else {
+ /* The reference type rt′1 must match rt′. */
+ type_tmp = ref_type_diff.ref_type;
+ if (wasm_is_type_multi_byte_type(type_tmp)) {
+ bh_memcpy_s(
+ &wasm_ref_type,
+ wasm_reftype_struct_size(&ref_type_diff),
+ &ref_type_diff,
+ wasm_reftype_struct_size(&ref_type_diff));
+ }
+ }
+ PUSH_REF(type_tmp);
+ if (!(frame_csp_tmp = check_branch_block(
+ loader_ctx, &p, p_end, opcode, error_buf,
+ error_buf_size))) {
+ goto fail;
+ }
+ /* Ignore heap_types */
+ skip_leb_uint32(p, p_end);
+ skip_leb_uint32(p, p_end);
+
+ /* Restore the original stack top's ref type */
+ POP_REF(type_tmp);
+#if WASM_ENABLE_FAST_INTERP != 0
+ /* Erase the opnd offset emitted by POP_REF() */
+ wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
+#endif
+ if (opcode1 == WASM_OP_BR_ON_CAST) {
+ type_tmp = ref_type_diff.ref_type;
+ if (wasm_is_type_multi_byte_type(type_tmp)) {
+ bh_memcpy_s(
+ &wasm_ref_type,
+ wasm_reftype_struct_size(&ref_type_diff),
+ &ref_type_diff,
+ wasm_reftype_struct_size(&ref_type_diff));
+ }
+ }
+ else {
+ type_tmp = ref_type2.ref_type;
+ if (wasm_is_type_multi_byte_type(type_tmp)) {
+ bh_memcpy_s(
+ &wasm_ref_type,
+ wasm_reftype_struct_size(&ref_type2),
+ &ref_type2,
+ wasm_reftype_struct_size(&ref_type2));
+ }
+ }
+ PUSH_REF(type_tmp);
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ /* Erase the opnd offset emitted by PUSH_REF() */
+ wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
+#endif
+ break;
+ }
+
+ case WASM_OP_ANY_CONVERT_EXTERN:
+ {
+ uint8 type;
+
+ if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
+ &wasm_ref_type, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+ if (!(type == REF_TYPE_EXTERNREF
+ || (type == REF_TYPE_HT_NON_NULLABLE
+ && wasm_ref_type.ref_ht_common.heap_type
+ == HEAP_TYPE_EXTERN)
+ || type == VALUE_TYPE_ANY)) {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch");
+ goto fail;
+ }
+
+ if (type == REF_TYPE_EXTERNREF)
+ type = REF_TYPE_ANYREF;
+ else {
+ wasm_ref_type.ref_ht_common.heap_type =
+ HEAP_TYPE_ANY;
+ }
+ PUSH_REF(type);
+ break;
+ }
+
+ case WASM_OP_EXTERN_CONVERT_ANY:
+ {
+ uint8 type;
+
+ if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
+ &wasm_ref_type, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+ if (type == REF_TYPE_EXTERNREF
+ || ((type == REF_TYPE_HT_NULLABLE
+ || type == REF_TYPE_HT_NON_NULLABLE)
+ && wasm_ref_type.ref_ht_common.heap_type
+ == HEAP_TYPE_EXTERN)) {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch");
+ goto fail;
+ }
+
+ if (type != REF_TYPE_HT_NON_NULLABLE) {
+ /* push (ref null extern) */
+ type = REF_TYPE_EXTERNREF;
+ }
+ else {
+ /* push (ref extern) */
+ type = REF_TYPE_HT_NON_NULLABLE;
+ wasm_set_refheaptype_common(
+ &wasm_ref_type.ref_ht_common, false,
+ HEAP_TYPE_EXTERN);
+ }
+ PUSH_REF(type);
+ break;
+ }
+
+#if WASM_ENABLE_STRINGREF != 0
+ case WASM_OP_STRING_NEW_UTF8:
+ case WASM_OP_STRING_NEW_WTF16:
+ case WASM_OP_STRING_NEW_LOSSY_UTF8:
+ case WASM_OP_STRING_NEW_WTF8:
+ {
+ uint32 memidx;
+
+#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
+ func->has_memory_operations = true;
+#endif
+
+ read_leb_uint32(p, p_end, memidx);
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, (uint32)memidx);
+#endif
+ POP_I32();
+ POP_I32();
+ PUSH_REF(REF_TYPE_STRINGREF);
+ (void)memidx;
+ break;
+ }
+ case WASM_OP_STRING_CONST:
+ {
+ uint32 contents;
+
+ read_leb_uint32(p, p_end, contents);
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, (uint32)contents);
+#endif
+ PUSH_REF(REF_TYPE_STRINGREF);
+ (void)contents;
+ break;
+ }
+ case WASM_OP_STRING_MEASURE_UTF8:
+ case WASM_OP_STRING_MEASURE_WTF8:
+ case WASM_OP_STRING_MEASURE_WTF16:
+ {
+ POP_STRINGREF();
+ PUSH_I32();
+ break;
+ }
+ case WASM_OP_STRING_ENCODE_UTF8:
+ case WASM_OP_STRING_ENCODE_WTF16:
+ case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
+ case WASM_OP_STRING_ENCODE_WTF8:
+ {
+ uint32 memidx;
+
+#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
+ func->has_memory_operations = true;
+#endif
+
+ read_leb_uint32(p, p_end, memidx);
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, (uint32)memidx);
+#endif
+ POP_I32();
+ POP_STRINGREF();
+ PUSH_I32();
+ (void)memidx;
+ break;
+ }
+ case WASM_OP_STRING_CONCAT:
+ {
+ POP_STRINGREF();
+ POP_STRINGREF();
+ PUSH_REF(REF_TYPE_STRINGREF);
+ break;
+ }
+ case WASM_OP_STRING_EQ:
+ {
+ POP_STRINGREF();
+ POP_STRINGREF();
+ PUSH_I32();
+ break;
+ }
+ case WASM_OP_STRING_IS_USV_SEQUENCE:
+ {
+ POP_STRINGREF();
+ PUSH_I32();
+ break;
+ }
+ case WASM_OP_STRING_AS_WTF8:
+ {
+ POP_STRINGREF();
+ PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
+ break;
+ }
+ case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
+ {
+ POP_I32();
+ POP_I32();
+ POP_REF(REF_TYPE_STRINGVIEWWTF8);
+ PUSH_I32();
+ break;
+ }
+ case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
+ case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
+ case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
+ {
+ uint32 memidx;
+
+#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
+ func->has_memory_operations = true;
+#endif
+
+ read_leb_uint32(p, p_end, memidx);
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, (uint32)memidx);
+#endif
+ POP_I32();
+ POP_I32();
+ POP_I32();
+ POP_REF(REF_TYPE_STRINGVIEWWTF8);
+ PUSH_I32();
+ PUSH_I32();
+ (void)memidx;
+ break;
+ }
+ case WASM_OP_STRINGVIEW_WTF8_SLICE:
+ {
+ POP_I32();
+ POP_I32();
+ POP_REF(REF_TYPE_STRINGVIEWWTF8);
+ PUSH_REF(REF_TYPE_STRINGREF);
+ break;
+ }
+ case WASM_OP_STRING_AS_WTF16:
+ {
+ POP_STRINGREF();
+ PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
+ break;
+ }
+ case WASM_OP_STRINGVIEW_WTF16_LENGTH:
+ {
+ POP_REF(REF_TYPE_STRINGVIEWWTF16);
+ PUSH_I32();
+ break;
+ }
+ case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
+ {
+ POP_I32();
+ POP_REF(REF_TYPE_STRINGVIEWWTF16);
+ PUSH_I32();
+ break;
+ }
+ case WASM_OP_STRINGVIEW_WTF16_ENCODE:
+ {
+ uint32 memidx;
+
+#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
+ func->has_memory_operations = true;
+#endif
+
+ read_leb_uint32(p, p_end, memidx);
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, (uint32)memidx);
+#endif
+ POP_I32();
+ POP_I32();
+ POP_I32();
+ POP_REF(REF_TYPE_STRINGVIEWWTF16);
+ PUSH_I32();
+ (void)memidx;
+ break;
+ }
+ case WASM_OP_STRINGVIEW_WTF16_SLICE:
+ {
+ POP_I32();
+ POP_I32();
+ POP_REF(REF_TYPE_STRINGVIEWWTF16);
+ PUSH_REF(REF_TYPE_STRINGREF);
+ break;
+ }
+ case WASM_OP_STRING_AS_ITER:
+ {
+ POP_STRINGREF();
+ PUSH_REF(REF_TYPE_STRINGVIEWITER);
+ break;
+ }
+ case WASM_OP_STRINGVIEW_ITER_NEXT:
+ {
+ POP_REF(REF_TYPE_STRINGVIEWITER);
+ PUSH_I32();
+ break;
+ }
+ case WASM_OP_STRINGVIEW_ITER_ADVANCE:
+ case WASM_OP_STRINGVIEW_ITER_REWIND:
+ {
+ POP_I32();
+ POP_REF(REF_TYPE_STRINGVIEWITER);
+ PUSH_I32();
+ break;
+ }
+ case WASM_OP_STRINGVIEW_ITER_SLICE:
+ {
+ POP_I32();
+ POP_REF(REF_TYPE_STRINGVIEWITER);
+ PUSH_REF(REF_TYPE_STRINGREF);
+ break;
+ }
+ case WASM_OP_STRING_NEW_UTF8_ARRAY:
+ case WASM_OP_STRING_NEW_WTF16_ARRAY:
+ case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
+ case WASM_OP_STRING_NEW_WTF8_ARRAY:
+ {
+ POP_I32();
+ POP_I32();
+ POP_REF(REF_TYPE_ARRAYREF);
+ PUSH_REF(REF_TYPE_STRINGREF);
+ break;
+ }
+ case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
+ case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
+ case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
+ case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
+ {
+ POP_I32();
+ POP_REF(REF_TYPE_ARRAYREF);
+ POP_STRINGREF();
+ PUSH_I32();
+ break;
+ }
+#endif /* end of WASM_ENABLE_STRINGREF != 0 */
+ default:
+ set_error_buf_v(error_buf, error_buf_size,
+ "%s %02x %02x", "unsupported opcode",
+ 0xfb, opcode1);
+ goto fail;
+ }
+ break;
+ }
+#endif /* end of WASM_ENABLE_GC != 0 */
+
+ case WASM_OP_MISC_PREFIX:
+ {
+ uint32 opcode1;
+
+ read_leb_uint32(p, p_end, opcode1);
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_byte(loader_ctx, ((uint8)opcode1));
+#endif
+ 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;
+#if WASM_ENABLE_BULK_MEMORY != 0
+ case WASM_OP_MEMORY_INIT:
+ {
+ read_leb_uint32(p, p_end, data_seg_idx);
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, data_seg_idx);
+#endif
+ if (module->import_memory_count == 0
+ && module->memory_count == 0)
+ goto fail_unknown_memory;
+
+ read_leb_uint32(p, p_end, memidx);
+ check_memidx(module, memidx);
+
+ 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;
+ }
+
+ if (module->data_seg_count1 == 0)
+ goto fail_data_cnt_sec_require;
+
+ POP_I32();
+ POP_I32();
+ POP_MEM_OFFSET();
+#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
+ func->has_memory_operations = true;
+#endif
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ module->is_bulk_memory_used = true;
+#endif
+ break;
+ }
+ case WASM_OP_DATA_DROP:
+ {
+ read_leb_uint32(p, p_end, data_seg_idx);
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, data_seg_idx);
+#endif
+ if (data_seg_idx >= module->data_seg_count) {
+ set_error_buf(error_buf, error_buf_size,
+ "unknown data segment");
+ goto fail;
+ }
+
+ if (module->data_seg_count1 == 0)
+ goto fail_data_cnt_sec_require;
+
+#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
+ func->has_memory_operations = true;
+#endif
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ module->is_bulk_memory_used = true;
+#endif
+ break;
+ }
+ case WASM_OP_MEMORY_COPY:
+ {
+ CHECK_BUF(p, p_end, sizeof(int16));
+ /* check both src and dst memory index */
+ read_leb_uint32(p, p_end, memidx);
+ check_memidx(module, memidx);
+ read_leb_uint32(p, p_end, memidx);
+ check_memidx(module, memidx);
+
+ if (module->import_memory_count == 0
+ && module->memory_count == 0)
+ goto fail_unknown_memory;
+
+ POP_MEM_OFFSET();
+ POP_MEM_OFFSET();
+ POP_MEM_OFFSET();
+#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
+ func->has_memory_operations = true;
+#endif
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ module->is_bulk_memory_used = true;
+#endif
+ break;
+ }
+ case WASM_OP_MEMORY_FILL:
+ {
+ read_leb_uint32(p, p_end, memidx);
+ check_memidx(module, memidx);
+ if (module->import_memory_count == 0
+ && module->memory_count == 0) {
+ goto fail_unknown_memory;
+ }
+ POP_MEM_OFFSET();
+ POP_I32();
+ POP_MEM_OFFSET();
+#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
+ func->has_memory_operations = true;
+#endif
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ module->is_bulk_memory_used = true;
+#endif
+ break;
+ }
+
+ 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;
+#endif /* WASM_ENABLE_BULK_MEMORY */
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+ case WASM_OP_TABLE_INIT:
+ {
+ uint8 seg_type = 0, tbl_type = 0;
+#if WASM_ENABLE_GC != 0
+ WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
+#endif
+
+ read_leb_uint32(p, p_end, table_seg_idx);
+ read_leb_uint32(p, p_end, table_idx);
+
+ if (!get_table_elem_type(module, table_idx, &tbl_type,
+#if WASM_ENABLE_GC != 0
+ (void **)&tbl_ref_type,
+#else
+ NULL,
+#endif
+ error_buf, error_buf_size))
+ goto fail;
+
+ if (!get_table_seg_elem_type(module, table_seg_idx,
+ &seg_type,
+#if WASM_ENABLE_GC != 0
+ (void **)&seg_ref_type,
+#else
+ NULL,
+#endif
+ error_buf, error_buf_size))
+ goto fail;
+
+#if WASM_ENABLE_GC == 0
+ if (seg_type != tbl_type) {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch");
+ goto fail;
+ }
+#else
+ if (!wasm_reftype_is_subtype_of(
+ seg_type, seg_ref_type, tbl_type, tbl_ref_type,
+ module->types, module->type_count)) {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch");
+ goto fail;
+ }
+#endif
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, table_seg_idx);
+ emit_uint32(loader_ctx, table_idx);
+#endif
+ POP_I32();
+ POP_I32();
+#if WASM_ENABLE_MEMORY64 != 0
+ table_elem_idx_type = is_table_64bit(module, table_idx)
+ ? VALUE_TYPE_I64
+ : VALUE_TYPE_I32;
+#endif
+ POP_TBL_ELEM_IDX();
+
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ module->is_ref_types_used = true;
+#endif
+ break;
+ }
+ 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, NULL, error_buf,
+ error_buf_size))
+ goto fail;
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, table_seg_idx);
+#endif
+
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ module->is_ref_types_used = true;
+#endif
+ break;
+ }
+ case WASM_OP_TABLE_COPY:
+ {
+ uint8 src_type, dst_type, src_tbl_idx_type,
+ dst_tbl_idx_type, min_tbl_idx_type;
+#if WASM_ENABLE_GC != 0
+ WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
+#endif
+ uint32 src_tbl_idx, dst_tbl_idx;
+
+ read_leb_uint32(p, p_end, dst_tbl_idx);
+ if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
+#if WASM_ENABLE_GC != 0
+ (void **)&dst_ref_type,
+#else
+ NULL,
+#endif
+ error_buf, error_buf_size))
+ goto fail;
+
+ read_leb_uint32(p, p_end, src_tbl_idx);
+ if (!get_table_elem_type(module, src_tbl_idx, &src_type,
+#if WASM_ENABLE_GC != 0
+ (void **)&src_ref_type,
+#else
+ NULL,
+#endif
+ error_buf, error_buf_size))
+ goto fail;
+
+#if WASM_ENABLE_GC == 0
+ if (src_type != dst_type) {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch");
+ goto fail;
+ }
+#else
+ if (!wasm_reftype_is_subtype_of(
+ src_type, src_ref_type, dst_type, dst_ref_type,
+ module->types, module->type_count)) {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch");
+ goto fail;
+ }
+#endif
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, dst_tbl_idx);
+ emit_uint32(loader_ctx, src_tbl_idx);
+#endif
+
+#if WASM_ENABLE_MEMORY64 != 0
+ src_tbl_idx_type = is_table_64bit(module, src_tbl_idx)
+ ? VALUE_TYPE_I64
+ : VALUE_TYPE_I32;
+ dst_tbl_idx_type = is_table_64bit(module, dst_tbl_idx)
+ ? VALUE_TYPE_I64
+ : VALUE_TYPE_I32;
+ min_tbl_idx_type =
+ (src_tbl_idx_type == VALUE_TYPE_I32
+ || dst_tbl_idx_type == VALUE_TYPE_I32)
+ ? VALUE_TYPE_I32
+ : VALUE_TYPE_I64;
+#else
+ src_tbl_idx_type = VALUE_TYPE_I32;
+ dst_tbl_idx_type = VALUE_TYPE_I32;
+ min_tbl_idx_type = VALUE_TYPE_I32;
+#endif
+
+ table_elem_idx_type = min_tbl_idx_type;
+ POP_TBL_ELEM_IDX();
+ table_elem_idx_type = src_tbl_idx_type;
+ POP_TBL_ELEM_IDX();
+ table_elem_idx_type = dst_tbl_idx_type;
+ POP_TBL_ELEM_IDX();
+
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ module->is_ref_types_used = true;
+#endif
+ break;
+ }
+ 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, NULL,
+ error_buf, error_buf_size))
+ goto fail;
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, table_idx);
+#endif
+
+#if WASM_ENABLE_MEMORY64 != 0
+ table_elem_idx_type = is_table_64bit(module, table_idx)
+ ? VALUE_TYPE_I64
+ : VALUE_TYPE_I32;
+#endif
+ PUSH_TBL_ELEM_IDX();
+
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ module->is_ref_types_used = true;
+#endif
+ break;
+ }
+ case WASM_OP_TABLE_GROW:
+ case WASM_OP_TABLE_FILL:
+ {
+ uint8 decl_type;
+#if WASM_ENABLE_GC != 0
+ WASMRefType *ref_type = NULL;
+#endif
+
+ read_leb_uint32(p, p_end, table_idx);
+ if (!get_table_elem_type(module, table_idx, &decl_type,
+#if WASM_ENABLE_GC != 0
+ (void **)&ref_type,
+#else
+ NULL,
+#endif
+ error_buf, error_buf_size))
+ goto fail;
+#if WASM_ENABLE_GC != 0
+ if (wasm_is_type_multi_byte_type(decl_type)) {
+ bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
+ ref_type,
+ wasm_reftype_struct_size(ref_type));
+ }
+#endif
+
+ if (opcode1 == WASM_OP_TABLE_GROW) {
+ if (table_idx < module->import_table_count) {
+ module->import_tables[table_idx]
+ .u.table.table_type.possible_grow = true;
+ }
+ else {
+ module
+ ->tables[table_idx
+ - module->import_table_count]
+ .table_type.possible_grow = true;
+ }
+ }
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, table_idx);
+#endif
+
+#if WASM_ENABLE_MEMORY64 != 0
+ table_elem_idx_type = is_table_64bit(module, table_idx)
+ ? VALUE_TYPE_I64
+ : VALUE_TYPE_I32;
+#endif
+ POP_TBL_ELEM_IDX();
+#if WASM_ENABLE_FAST_INTERP != 0
+ POP_OFFSET_TYPE(decl_type);
+#endif
+ POP_TYPE(decl_type);
+ if (opcode1 == WASM_OP_TABLE_GROW)
+ PUSH_TBL_ELEM_IDX();
+ else
+ POP_TBL_ELEM_IDX();
+
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ module->is_ref_types_used = true;
+#endif
+ break;
+ }
+#endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
+ default:
+ set_error_buf_v(error_buf, error_buf_size,
+ "%s %02x %02x", "unsupported opcode",
+ 0xfc, opcode1);
+ goto fail;
+ }
+ break;
+ }
+
+#if WASM_ENABLE_SIMD != 0
+#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
+ case WASM_OP_SIMD_PREFIX:
+ {
+ uint32 opcode1;
+
+#if WASM_ENABLE_WAMR_COMPILER != 0
+ /* Mark the SIMD instruction is used in this module */
+ module->is_simd_used = true;
+#endif
+
+ read_leb_uint32(p, p_end, opcode1);
+
+ /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
+ */
+ switch (opcode1) {
+ /* memory instruction */
+ case SIMD_v128_load:
+ 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:
+ {
+ CHECK_MEMORY();
+
+ read_leb_uint32(p, p_end, align); /* align */
+ if (!check_simd_memory_access_align(
+ opcode1, align, error_buf, error_buf_size)) {
+ goto fail;
+ }
+
+ read_leb_mem_offset(p, p_end, mem_offset); /* offset */
+
+ POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
+#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
+ func->has_memory_operations = true;
+#endif
+ break;
+ }
+
+ case SIMD_v128_store:
+ {
+ CHECK_MEMORY();
+
+ read_leb_uint32(p, p_end, align); /* align */
+ if (!check_simd_memory_access_align(
+ opcode1, align, error_buf, error_buf_size)) {
+ goto fail;
+ }
+
+ read_leb_mem_offset(p, p_end, mem_offset); /* offset */
+
+ POP_V128();
+ POP_MEM_OFFSET();
+#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
+ func->has_memory_operations = true;
+#endif
+ break;
+ }
+
+ /* basic operation */
+ case SIMD_v128_const:
+ {
+ CHECK_BUF1(p, p_end, 16);
+ p += 16;
+ PUSH_V128();
+ break;
+ }
+
+ 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:
+ {
+ POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
+ break;
+ }
+
+ /* splat operation */
+ case SIMD_i8x16_splat:
+ case SIMD_i16x8_splat:
+ case SIMD_i32x4_splat:
+ case SIMD_i64x2_splat:
+ case SIMD_f32x4_splat:
+ case SIMD_f64x2_splat:
+ {
+ uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
+ VALUE_TYPE_I32, VALUE_TYPE_I64,
+ VALUE_TYPE_F32, VALUE_TYPE_F64 };
+ POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
+ VALUE_TYPE_V128);
+ break;
+ }
+
+ /* lane operation */
+ 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:
+ {
+ 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 */
+
+ CHECK_BUF(p, p_end, 1);
+ lane = read_uint8(p);
+ if (!check_simd_access_lane(opcode1, lane, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+
+ if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
+ if (!(wasm_loader_pop_frame_ref(
+ loader_ctx,
+ replace[opcode1
+ - SIMD_i8x16_extract_lane_s],
+ error_buf, error_buf_size)))
+ goto fail;
+ }
+
+ POP_AND_PUSH(
+ VALUE_TYPE_V128,
+ push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
+ break;
+ }
+
+ /* i8x16 compare operation */
+ 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:
+ /* i16x8 compare operation */
+ 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:
+ /* i32x4 compare operation */
+ 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:
+ /* f32x4 compare operation */
+ case SIMD_f32x4_eq:
+ case SIMD_f32x4_ne:
+ case SIMD_f32x4_lt:
+ case SIMD_f32x4_gt:
+ case SIMD_f32x4_le:
+ case SIMD_f32x4_ge:
+ /* f64x2 compare operation */
+ case SIMD_f64x2_eq:
+ case SIMD_f64x2_ne:
+ case SIMD_f64x2_lt:
+ case SIMD_f64x2_gt:
+ case SIMD_f64x2_le:
+ case SIMD_f64x2_ge:
+ {
+ POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
+ break;
+ }
+
+ /* v128 operation */
+ case SIMD_v128_not:
+ {
+ POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
+ break;
+ }
+
+ 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;
+ }
+
+ case SIMD_v128_bitselect:
+ {
+ POP_V128();
+ POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
+ break;
+ }
+
+ 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(
+ opcode1, align, error_buf, error_buf_size)) {
+ goto fail;
+ }
+
+ read_leb_mem_offset(p, p_end, mem_offset); /* offset */
+
+ CHECK_BUF(p, p_end, 1);
+ lane = read_uint8(p);
+ if (!check_simd_access_lane(opcode1, lane, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+
+ POP_V128();
+ POP_MEM_OFFSET();
+ if (opcode1 < SIMD_v128_store8_lane) {
+ PUSH_V128();
+ }
+#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
+ func->has_memory_operations = true;
+#endif
+ 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(
+ opcode1, align, error_buf, error_buf_size)) {
+ goto fail;
+ }
+
+ read_leb_mem_offset(p, p_end, mem_offset); /* offset */
+
+ POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
+#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
+ func->has_memory_operations = true;
+#endif
+ 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;
+ }
+
+ case SIMD_i8x16_all_true:
+ case SIMD_i8x16_bitmask:
+ {
+ POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
+ break;
+ }
+
+ 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;
+ }
+
+ case SIMD_i8x16_shl:
+ case SIMD_i8x16_shr_s:
+ case SIMD_i8x16_shr_u:
+ {
+ POP_I32();
+ POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
+ break;
+ }
+
+ case SIMD_i8x16_add:
+ case SIMD_i8x16_add_sat_s:
+ case SIMD_i8x16_add_sat_u:
+ case SIMD_i8x16_sub:
+ 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;
+ }
+
+ case SIMD_i8x16_min_s:
+ case SIMD_i8x16_min_u:
+ case SIMD_i8x16_max_s:
+ case SIMD_i8x16_max_u:
+ {
+ POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
+ break;
+ }
+
+ case SIMD_f64x2_trunc:
+ {
+ POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
+ break;
+ }
+
+ case SIMD_i8x16_avgr_u:
+ {
+ 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;
+ }
+
+ case SIMD_i16x8_add:
+ case SIMD_i16x8_add_sat_s:
+ case SIMD_i16x8_add_sat_u:
+ case SIMD_i16x8_sub:
+ 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;
+ }
+
+ 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:
+ 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_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;
+ }
+
+ 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:
+ case SIMD_i32x4_dot_i16x8_s:
+ 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;
+ }
+
+ case SIMD_i64x2_add:
+ case SIMD_i64x2_sub:
+ case SIMD_i64x2_mul:
+ 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_sqrt:
+ {
+ POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
+ break;
+ }
+
+ case SIMD_f32x4_add:
+ case SIMD_f32x4_sub:
+ case SIMD_f32x4_mul:
+ case SIMD_f32x4_div:
+ case SIMD_f32x4_min:
+ case SIMD_f32x4_max:
+ 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_sqrt:
+ {
+ POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
+ break;
+ }
+
+ case SIMD_f64x2_add:
+ case SIMD_f64x2_sub:
+ case SIMD_f64x2_mul:
+ case SIMD_f64x2_div:
+ case SIMD_f64x2_min:
+ case SIMD_f64x2_max:
+ case SIMD_f64x2_pmin:
+ case SIMD_f64x2_pmax:
+ {
+ POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
+ break;
+ }
+
+ 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;
+ }
+
+ default:
+ {
+ if (error_buf != NULL) {
+ snprintf(error_buf, error_buf_size,
+ "WASM module load failed: "
+ "invalid opcode 0xfd %02x.",
+ opcode1);
+ }
+ goto fail;
+ }
+ }
+ break;
+ }
+#endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
+#endif /* end of WASM_ENABLE_SIMD */
+
+#if WASM_ENABLE_SHARED_MEMORY != 0
+ case WASM_OP_ATOMIC_PREFIX:
+ {
+ uint32 opcode1;
+
+ read_leb_uint32(p, p_end, opcode1);
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_byte(loader_ctx, opcode1);
+#endif
+ if (opcode1 != WASM_OP_ATOMIC_FENCE) {
+ CHECK_MEMORY();
+ read_leb_uint32(p, p_end, align); /* align */
+ read_leb_mem_offset(p, p_end, mem_offset); /* offset */
+ if (!check_memory_align_equal(opcode1, align, error_buf,
+ error_buf_size)) {
+ goto fail;
+ }
+#if WASM_ENABLE_FAST_INTERP != 0
+ emit_uint32(loader_ctx, mem_offset);
+#endif
+ }
+#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
+ func->has_memory_operations = true;
+#endif
+ switch (opcode1) {
+ case WASM_OP_ATOMIC_NOTIFY:
+ POP_I32();
+ POP_MEM_OFFSET();
+ PUSH_I32();
+ break;
+ case WASM_OP_ATOMIC_WAIT32:
+ POP_I64();
+ POP_I32();
+ POP_MEM_OFFSET();
+ PUSH_I32();
+ break;
+ case WASM_OP_ATOMIC_WAIT64:
+ POP_I64();
+ POP_I64();
+ POP_MEM_OFFSET();
+ PUSH_I32();
+ break;
+ case WASM_OP_ATOMIC_FENCE:
+ /* reserved byte 0x00 */
+ if (*p++ != 0x00) {
+ set_error_buf(error_buf, error_buf_size,
+ "zero byte expected");
+ 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(mem_offset_type, 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_MEM_OFFSET();
+ 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(mem_offset_type, 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_MEM_OFFSET();
+ 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:
+ POP_I32();
+ POP_MEM_OFFSET();
+ PUSH_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_MEM_OFFSET();
+ 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_MEM_OFFSET();
+ 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_MEM_OFFSET();
+ PUSH_I64();
+ break;
+ default:
+ set_error_buf_v(error_buf, error_buf_size,
+ "%s %02x %02x", "unsupported opcode",
+ 0xfe, opcode1);
+ goto fail;
+ }
+ break;
+ }
+#endif /* end of WASM_ENABLE_SHARED_MEMORY */
+
+ default:
+ set_error_buf_v(error_buf, error_buf_size, "%s %02x",
+ "unsupported opcode", opcode);
+ goto fail;
+ }
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ last_op = opcode;
+#endif
+ }
+
+ if (loader_ctx->csp_num > 0) {
+ 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");
+ goto fail;
+ }
+
+#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;
+ if (func->const_cell_num > 0) {
+ int32 j;
+
+ if (!(func->consts = func_const = loader_malloc(
+ func->const_cell_num * 4, error_buf, error_buf_size)))
+ goto fail;
+
+ func_const_end = func->consts + func->const_cell_num * 4;
+ /* reverse the const buf */
+ for (j = loader_ctx->num_const - 1; j >= 0; j--) {
+ Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
+ 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);
+ }
+ }
+ }
+
+ func->max_stack_cell_num = loader_ctx->preserved_local_offset
+ - loader_ctx->start_dynamic_offset + 1;
+#else
+ func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
+#endif
+ func->max_block_num = loader_ctx->max_csp_num;
+ return_value = true;
+
+fail:
+ wasm_loader_ctx_destroy(loader_ctx);
+
+ (void)table_idx;
+ (void)table_seg_idx;
+ (void)data_seg_idx;
+ (void)i64_const;
+ (void)local_offset;
+ (void)p_org;
+ (void)mem_offset;
+ (void)align;
+ return return_value;
+}
diff --git a/lib/wamr/core/iwasm/interpreter/wasm_loader.h b/lib/wamr/core/iwasm/interpreter/wasm_loader.h
new file mode 100644
index 0000000..676770e
--- /dev/null
+++ b/lib/wamr/core/iwasm/interpreter/wasm_loader.h
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+#ifndef _WASM_LOADER_H
+#define _WASM_LOADER_H
+
+#include "wasm.h"
+#include "bh_hashmap.h"
+#include "../common/wasm_runtime_common.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * Load a WASM module from a specified byte buffer.
+ *
+ * @param buf the byte buffer which contains the WASM binary data
+ * @param size the size of the buffer
+ * @param error_buf output of the exception info
+ * @param error_buf_size the size of the exception string
+ *
+ * @return return module loaded, NULL if failed
+ */
+WASMModule *
+wasm_loader_load(uint8 *buf, uint32 size,
+#if WASM_ENABLE_MULTI_MODULE != 0
+ bool main_module,
+#endif
+ const LoadArgs *args, char *error_buf, uint32 error_buf_size);
+
+/**
+ * Load a WASM module from a specified WASM section list.
+ *
+ * @param section_list the section list which contains each section data
+ * @param error_buf output of the exception info
+ * @param error_buf_size the size of the exception string
+ *
+ * @return return WASM module loaded, NULL if failed
+ */
+WASMModule *
+wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
+ uint32 error_buf_size);
+
+/**
+ * Unload a WASM module.
+ *
+ * @param module the module to be unloaded
+ */
+void
+wasm_loader_unload(WASMModule *module);
+
+/**
+ * Find address of related else opcode and end opcode of opcode block/loop/if
+ * according to the start address of opcode.
+ *
+ * @param module the module to find
+ * @param start_addr the next address of opcode block/loop/if
+ * @param code_end_addr the end address of function code block
+ * @param block_type the type of block, 0/1/2 denotes block/loop/if
+ * @param p_else_addr returns the else addr if found
+ * @param p_end_addr returns the end addr if found
+ * @param error_buf returns the error log for this function
+ * @param error_buf_size returns the error log string length
+ *
+ * @return true if success, false otherwise
+ */
+
+bool
+wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
+ const uint8 *start_addr, const uint8 *code_end_addr,
+ uint8 block_type, uint8 **p_else_addr,
+ uint8 **p_end_addr);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* end of _WASM_LOADER_H */
diff --git a/lib/wamr/core/iwasm/interpreter/wasm_opcode.h b/lib/wamr/core/iwasm/interpreter/wasm_opcode.h
new file mode 100644
index 0000000..7664745
--- /dev/null
+++ b/lib/wamr/core/iwasm/interpreter/wasm_opcode.h
@@ -0,0 +1,1030 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#ifndef _WASM_OPCODE_H
+#define _WASM_OPCODE_H
+
+#include "wasm.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum WASMOpcode {
+ /* control instructions */
+ WASM_OP_UNREACHABLE = 0x00, /* unreachable */
+ WASM_OP_NOP = 0x01, /* nop */
+ WASM_OP_BLOCK = 0x02, /* block */
+ WASM_OP_LOOP = 0x03, /* loop */
+ WASM_OP_IF = 0x04, /* if */
+ WASM_OP_ELSE = 0x05, /* else */
+ WASM_OP_TRY = 0x06, /* try */
+ WASM_OP_CATCH = 0x07, /* catch* */
+ WASM_OP_THROW = 0x08, /* throw of a try catch */
+ WASM_OP_RETHROW = 0x09, /* rethrow of a try catch */
+ WASM_OP_UNUSED_0x0a = 0x0a,
+
+ WASM_OP_END = 0x0b, /* end */
+ WASM_OP_BR = 0x0c, /* br */
+ WASM_OP_BR_IF = 0x0d, /* br if */
+ WASM_OP_BR_TABLE = 0x0e, /* br table */
+ WASM_OP_RETURN = 0x0f, /* return */
+ WASM_OP_CALL = 0x10, /* call */
+ WASM_OP_CALL_INDIRECT = 0x11, /* call_indirect */
+ WASM_OP_RETURN_CALL = 0x12, /* return_call */
+ WASM_OP_RETURN_CALL_INDIRECT = 0x13, /* return_call_indirect */
+ WASM_OP_CALL_REF = 0x14, /* call_ref */
+ WASM_OP_RETURN_CALL_REF = 0x15, /* return_call_ref */
+
+ WASM_OP_UNUSED_0x16 = 0x16,
+ WASM_OP_UNUSED_0x17 = 0x17,
+
+ WASM_OP_DELEGATE = 0x18, /* delegate block of the try catch*/
+ WASM_OP_CATCH_ALL = 0x19, /* a catch_all handler in a try block */
+
+ /* parametric instructions */
+ WASM_OP_DROP = 0x1a, /* drop */
+ WASM_OP_SELECT = 0x1b, /* select */
+ WASM_OP_SELECT_T = 0x1c, /* select t */
+
+ WASM_OP_GET_GLOBAL_64 = 0x1d,
+ WASM_OP_SET_GLOBAL_64 = 0x1e,
+ WASM_OP_SET_GLOBAL_AUX_STACK = 0x1f,
+
+ /* variable instructions */
+ WASM_OP_GET_LOCAL = 0x20, /* get_local */
+ WASM_OP_SET_LOCAL = 0x21, /* set_local */
+ WASM_OP_TEE_LOCAL = 0x22, /* tee_local */
+ WASM_OP_GET_GLOBAL = 0x23, /* get_global */
+ WASM_OP_SET_GLOBAL = 0x24, /* set_global */
+
+ WASM_OP_TABLE_GET = 0x25, /* table.get */
+ WASM_OP_TABLE_SET = 0x26, /* table.set */
+ WASM_OP_UNUSED_0x27 = 0x27,
+
+ /* memory instructions */
+ WASM_OP_I32_LOAD = 0x28, /* i32.load */
+ WASM_OP_I64_LOAD = 0x29, /* i64.load */
+ WASM_OP_F32_LOAD = 0x2a, /* f32.load */
+ WASM_OP_F64_LOAD = 0x2b, /* f64.load */
+ WASM_OP_I32_LOAD8_S = 0x2c, /* i32.load8_s */
+ WASM_OP_I32_LOAD8_U = 0x2d, /* i32.load8_u */
+ WASM_OP_I32_LOAD16_S = 0x2e, /* i32.load16_s */
+ WASM_OP_I32_LOAD16_U = 0x2f, /* i32.load16_u */
+ WASM_OP_I64_LOAD8_S = 0x30, /* i64.load8_s */
+ WASM_OP_I64_LOAD8_U = 0x31, /* i64.load8_u */
+ WASM_OP_I64_LOAD16_S = 0x32, /* i64.load16_s */
+ WASM_OP_I64_LOAD16_U = 0x33, /* i64.load16_u */
+ WASM_OP_I64_LOAD32_S = 0x34, /* i32.load32_s */
+ WASM_OP_I64_LOAD32_U = 0x35, /* i32.load32_u */
+ WASM_OP_I32_STORE = 0x36, /* i32.store */
+ WASM_OP_I64_STORE = 0x37, /* i64.store */
+ WASM_OP_F32_STORE = 0x38, /* f32.store */
+ WASM_OP_F64_STORE = 0x39, /* f64.store */
+ WASM_OP_I32_STORE8 = 0x3a, /* i32.store8 */
+ WASM_OP_I32_STORE16 = 0x3b, /* i32.store16 */
+ WASM_OP_I64_STORE8 = 0x3c, /* i64.store8 */
+ WASM_OP_I64_STORE16 = 0x3d, /* i64.store16 */
+ WASM_OP_I64_STORE32 = 0x3e, /* i64.store32 */
+ WASM_OP_MEMORY_SIZE = 0x3f, /* memory.size */
+ WASM_OP_MEMORY_GROW = 0x40, /* memory.grow */
+
+ /* constant instructions */
+ WASM_OP_I32_CONST = 0x41, /* i32.const */
+ WASM_OP_I64_CONST = 0x42, /* i64.const */
+ WASM_OP_F32_CONST = 0x43, /* f32.const */
+ WASM_OP_F64_CONST = 0x44, /* f64.const */
+
+ /* comparison instructions */
+ WASM_OP_I32_EQZ = 0x45, /* i32.eqz */
+ WASM_OP_I32_EQ = 0x46, /* i32.eq */
+ WASM_OP_I32_NE = 0x47, /* i32.ne */
+ WASM_OP_I32_LT_S = 0x48, /* i32.lt_s */
+ WASM_OP_I32_LT_U = 0x49, /* i32.lt_u */
+ WASM_OP_I32_GT_S = 0x4a, /* i32.gt_s */
+ WASM_OP_I32_GT_U = 0x4b, /* i32.gt_u */
+ WASM_OP_I32_LE_S = 0x4c, /* i32.le_s */
+ WASM_OP_I32_LE_U = 0x4d, /* i32.le_u */
+ WASM_OP_I32_GE_S = 0x4e, /* i32.ge_s */
+ WASM_OP_I32_GE_U = 0x4f, /* i32.ge_u */
+
+ WASM_OP_I64_EQZ = 0x50, /* i64.eqz */
+ WASM_OP_I64_EQ = 0x51, /* i64.eq */
+ WASM_OP_I64_NE = 0x52, /* i64.ne */
+ WASM_OP_I64_LT_S = 0x53, /* i64.lt_s */
+ WASM_OP_I64_LT_U = 0x54, /* i64.lt_u */
+ WASM_OP_I64_GT_S = 0x55, /* i64.gt_s */
+ WASM_OP_I64_GT_U = 0x56, /* i64.gt_u */
+ WASM_OP_I64_LE_S = 0x57, /* i64.le_s */
+ WASM_OP_I64_LE_U = 0x58, /* i64.le_u */
+ WASM_OP_I64_GE_S = 0x59, /* i64.ge_s */
+ WASM_OP_I64_GE_U = 0x5a, /* i64.ge_u */
+
+ WASM_OP_F32_EQ = 0x5b, /* f32.eq */
+ WASM_OP_F32_NE = 0x5c, /* f32.ne */
+ WASM_OP_F32_LT = 0x5d, /* f32.lt */
+ WASM_OP_F32_GT = 0x5e, /* f32.gt */
+ WASM_OP_F32_LE = 0x5f, /* f32.le */
+ WASM_OP_F32_GE = 0x60, /* f32.ge */
+
+ WASM_OP_F64_EQ = 0x61, /* f64.eq */
+ WASM_OP_F64_NE = 0x62, /* f64.ne */
+ WASM_OP_F64_LT = 0x63, /* f64.lt */
+ WASM_OP_F64_GT = 0x64, /* f64.gt */
+ WASM_OP_F64_LE = 0x65, /* f64.le */
+ WASM_OP_F64_GE = 0x66, /* f64.ge */
+
+ /* numeric operators */
+ WASM_OP_I32_CLZ = 0x67, /* i32.clz */
+ WASM_OP_I32_CTZ = 0x68, /* i32.ctz */
+ WASM_OP_I32_POPCNT = 0x69, /* i32.popcnt */
+ WASM_OP_I32_ADD = 0x6a, /* i32.add */
+ WASM_OP_I32_SUB = 0x6b, /* i32.sub */
+ WASM_OP_I32_MUL = 0x6c, /* i32.mul */
+ WASM_OP_I32_DIV_S = 0x6d, /* i32.div_s */
+ WASM_OP_I32_DIV_U = 0x6e, /* i32.div_u */
+ WASM_OP_I32_REM_S = 0x6f, /* i32.rem_s */
+ WASM_OP_I32_REM_U = 0x70, /* i32.rem_u */
+ WASM_OP_I32_AND = 0x71, /* i32.and */
+ WASM_OP_I32_OR = 0x72, /* i32.or */
+ WASM_OP_I32_XOR = 0x73, /* i32.xor */
+ WASM_OP_I32_SHL = 0x74, /* i32.shl */
+ WASM_OP_I32_SHR_S = 0x75, /* i32.shr_s */
+ WASM_OP_I32_SHR_U = 0x76, /* i32.shr_u */
+ WASM_OP_I32_ROTL = 0x77, /* i32.rotl */
+ WASM_OP_I32_ROTR = 0x78, /* i32.rotr */
+
+ WASM_OP_I64_CLZ = 0x79, /* i64.clz */
+ WASM_OP_I64_CTZ = 0x7a, /* i64.ctz */
+ WASM_OP_I64_POPCNT = 0x7b, /* i64.popcnt */
+ WASM_OP_I64_ADD = 0x7c, /* i64.add */
+ WASM_OP_I64_SUB = 0x7d, /* i64.sub */
+ WASM_OP_I64_MUL = 0x7e, /* i64.mul */
+ WASM_OP_I64_DIV_S = 0x7f, /* i64.div_s */
+ WASM_OP_I64_DIV_U = 0x80, /* i64.div_u */
+ WASM_OP_I64_REM_S = 0x81, /* i64.rem_s */
+ WASM_OP_I64_REM_U = 0x82, /* i64.rem_u */
+ WASM_OP_I64_AND = 0x83, /* i64.and */
+ WASM_OP_I64_OR = 0x84, /* i64.or */
+ WASM_OP_I64_XOR = 0x85, /* i64.xor */
+ WASM_OP_I64_SHL = 0x86, /* i64.shl */
+ WASM_OP_I64_SHR_S = 0x87, /* i64.shr_s */
+ WASM_OP_I64_SHR_U = 0x88, /* i64.shr_u */
+ WASM_OP_I64_ROTL = 0x89, /* i64.rotl */
+ WASM_OP_I64_ROTR = 0x8a, /* i64.rotr */
+
+ WASM_OP_F32_ABS = 0x8b, /* f32.abs */
+ WASM_OP_F32_NEG = 0x8c, /* f32.neg */
+ WASM_OP_F32_CEIL = 0x8d, /* f32.ceil */
+ WASM_OP_F32_FLOOR = 0x8e, /* f32.floor */
+ WASM_OP_F32_TRUNC = 0x8f, /* f32.trunc */
+ WASM_OP_F32_NEAREST = 0x90, /* f32.nearest */
+ WASM_OP_F32_SQRT = 0x91, /* f32.sqrt */
+ WASM_OP_F32_ADD = 0x92, /* f32.add */
+ WASM_OP_F32_SUB = 0x93, /* f32.sub */
+ WASM_OP_F32_MUL = 0x94, /* f32.mul */
+ WASM_OP_F32_DIV = 0x95, /* f32.div */
+ WASM_OP_F32_MIN = 0x96, /* f32.min */
+ WASM_OP_F32_MAX = 0x97, /* f32.max */
+ WASM_OP_F32_COPYSIGN = 0x98, /* f32.copysign */
+
+ WASM_OP_F64_ABS = 0x99, /* f64.abs */
+ WASM_OP_F64_NEG = 0x9a, /* f64.neg */
+ WASM_OP_F64_CEIL = 0x9b, /* f64.ceil */
+ WASM_OP_F64_FLOOR = 0x9c, /* f64.floor */
+ WASM_OP_F64_TRUNC = 0x9d, /* f64.trunc */
+ WASM_OP_F64_NEAREST = 0x9e, /* f64.nearest */
+ WASM_OP_F64_SQRT = 0x9f, /* f64.sqrt */
+ WASM_OP_F64_ADD = 0xa0, /* f64.add */
+ WASM_OP_F64_SUB = 0xa1, /* f64.sub */
+ WASM_OP_F64_MUL = 0xa2, /* f64.mul */
+ WASM_OP_F64_DIV = 0xa3, /* f64.div */
+ WASM_OP_F64_MIN = 0xa4, /* f64.min */
+ WASM_OP_F64_MAX = 0xa5, /* f64.max */
+ WASM_OP_F64_COPYSIGN = 0xa6, /* f64.copysign */
+
+ /* conversions */
+ WASM_OP_I32_WRAP_I64 = 0xa7, /* i32.wrap/i64 */
+ WASM_OP_I32_TRUNC_S_F32 = 0xa8, /* i32.trunc_s/f32 */
+ WASM_OP_I32_TRUNC_U_F32 = 0xa9, /* i32.trunc_u/f32 */
+ WASM_OP_I32_TRUNC_S_F64 = 0xaa, /* i32.trunc_s/f64 */
+ WASM_OP_I32_TRUNC_U_F64 = 0xab, /* i32.trunc_u/f64 */
+
+ WASM_OP_I64_EXTEND_S_I32 = 0xac, /* i64.extend_s/i32 */
+ WASM_OP_I64_EXTEND_U_I32 = 0xad, /* i64.extend_u/i32 */
+ WASM_OP_I64_TRUNC_S_F32 = 0xae, /* i64.trunc_s/f32 */
+ WASM_OP_I64_TRUNC_U_F32 = 0xaf, /* i64.trunc_u/f32 */
+ WASM_OP_I64_TRUNC_S_F64 = 0xb0, /* i64.trunc_s/f64 */
+ WASM_OP_I64_TRUNC_U_F64 = 0xb1, /* i64.trunc_u/f64 */
+
+ WASM_OP_F32_CONVERT_S_I32 = 0xb2, /* f32.convert_s/i32 */
+ WASM_OP_F32_CONVERT_U_I32 = 0xb3, /* f32.convert_u/i32 */
+ WASM_OP_F32_CONVERT_S_I64 = 0xb4, /* f32.convert_s/i64 */
+ WASM_OP_F32_CONVERT_U_I64 = 0xb5, /* f32.convert_u/i64 */
+ WASM_OP_F32_DEMOTE_F64 = 0xb6, /* f32.demote/f64 */
+
+ WASM_OP_F64_CONVERT_S_I32 = 0xb7, /* f64.convert_s/i32 */
+ WASM_OP_F64_CONVERT_U_I32 = 0xb8, /* f64.convert_u/i32 */
+ WASM_OP_F64_CONVERT_S_I64 = 0xb9, /* f64.convert_s/i64 */
+ WASM_OP_F64_CONVERT_U_I64 = 0xba, /* f64.convert_u/i64 */
+ WASM_OP_F64_PROMOTE_F32 = 0xbb, /* f64.promote/f32 */
+
+ /* reinterpretations */
+ WASM_OP_I32_REINTERPRET_F32 = 0xbc, /* i32.reinterpret/f32 */
+ WASM_OP_I64_REINTERPRET_F64 = 0xbd, /* i64.reinterpret/f64 */
+ WASM_OP_F32_REINTERPRET_I32 = 0xbe, /* f32.reinterpret/i32 */
+ WASM_OP_F64_REINTERPRET_I64 = 0xbf, /* f64.reinterpret/i64 */
+
+ WASM_OP_I32_EXTEND8_S = 0xc0, /* i32.extend8_s */
+ WASM_OP_I32_EXTEND16_S = 0xc1, /* i32.extend16_s */
+ WASM_OP_I64_EXTEND8_S = 0xc2, /* i64.extend8_s */
+ WASM_OP_I64_EXTEND16_S = 0xc3, /* i64.extend16_s */
+ WASM_OP_I64_EXTEND32_S = 0xc4, /* i64.extend32_s */
+
+ /* drop/select specified types*/
+ WASM_OP_DROP_64 = 0xc5,
+ WASM_OP_SELECT_64 = 0xc6,
+
+ /* extend op code */
+ EXT_OP_GET_LOCAL_FAST = 0xc7,
+ EXT_OP_SET_LOCAL_FAST_I64 = 0xc8,
+ EXT_OP_SET_LOCAL_FAST = 0xc9,
+ EXT_OP_TEE_LOCAL_FAST = 0xca,
+ EXT_OP_TEE_LOCAL_FAST_I64 = 0xcb,
+ EXT_OP_COPY_STACK_TOP = 0xcc,
+ EXT_OP_COPY_STACK_TOP_I64 = 0xcd,
+ EXT_OP_COPY_STACK_VALUES = 0xce,
+
+ WASM_OP_IMPDEP = 0xcf,
+
+ WASM_OP_REF_NULL = 0xd0, /* ref.null */
+ WASM_OP_REF_IS_NULL = 0xd1, /* ref.is_null */
+ WASM_OP_REF_FUNC = 0xd2, /* ref.func */
+ WASM_OP_REF_EQ = 0xd3, /* ref.eq */
+ WASM_OP_REF_AS_NON_NULL = 0xd4, /* ref.as_non_null */
+ WASM_OP_BR_ON_NULL = 0xd5, /* br_on_null */
+ WASM_OP_BR_ON_NON_NULL = 0xd6, /* br_on_non_null */
+
+ EXT_OP_BLOCK = 0xd7, /* block with blocktype */
+ EXT_OP_LOOP = 0xd8, /* loop with blocktype */
+ EXT_OP_IF = 0xd9, /* if with blocktype */
+ EXT_OP_BR_TABLE_CACHE = 0xda, /* br_table from cache */
+
+ EXT_OP_TRY = 0xdb, /* try block with blocktype */
+
+#if WASM_ENABLE_DEBUG_INTERP != 0
+ DEBUG_OP_BREAK = 0xdc, /* debug break point */
+#endif
+
+ /* Post-MVP extend op prefix */
+ WASM_OP_GC_PREFIX = 0xfb,
+ WASM_OP_MISC_PREFIX = 0xfc,
+ WASM_OP_SIMD_PREFIX = 0xfd,
+ WASM_OP_ATOMIC_PREFIX = 0xfe,
+} WASMOpcode;
+
+typedef enum WASMGCEXTOpcode {
+ WASM_OP_STRUCT_NEW = 0x00, /* struct.new */
+ WASM_OP_STRUCT_NEW_DEFAULT = 0x01, /* struct.new_default */
+ WASM_OP_STRUCT_GET = 0x02, /* struct.get */
+ WASM_OP_STRUCT_GET_S = 0x03, /* struct.get_s */
+ WASM_OP_STRUCT_GET_U = 0x04, /* struct.get_u */
+ WASM_OP_STRUCT_SET = 0x05, /* struct.set */
+
+ WASM_OP_ARRAY_NEW = 0x06, /* array.new */
+ WASM_OP_ARRAY_NEW_DEFAULT = 0x07, /* array.new_default */
+ WASM_OP_ARRAY_NEW_FIXED = 0x08, /* array.new_fixed */
+ WASM_OP_ARRAY_NEW_DATA = 0x09, /* array.new_data */
+ WASM_OP_ARRAY_NEW_ELEM = 0x0A, /* array.new_elem */
+ WASM_OP_ARRAY_GET = 0x0B, /* array.get */
+ WASM_OP_ARRAY_GET_S = 0x0C, /* array.get_s */
+ WASM_OP_ARRAY_GET_U = 0x0D, /* array.get_u */
+ WASM_OP_ARRAY_SET = 0x0E, /* array.set */
+ WASM_OP_ARRAY_LEN = 0x0F, /* array.len */
+ WASM_OP_ARRAY_FILL = 0x10, /* array.fill */
+ WASM_OP_ARRAY_COPY = 0x11, /* array.copy */
+ WASM_OP_ARRAY_INIT_DATA = 0x12,
+ /* array.init_data */ /* TODO */
+ WASM_OP_ARRAY_INIT_ELEM = 0x13,
+ /* array.init_elem */ /* TODO */
+
+ WASM_OP_REF_TEST = 0x14, /* ref.test */
+ WASM_OP_REF_TEST_NULLABLE = 0x15, /* ref.test_nullable */
+ WASM_OP_REF_CAST = 0x16, /* ref.cast */
+ WASM_OP_REF_CAST_NULLABLE = 0x17, /* ref.cast_nullable */
+
+ WASM_OP_BR_ON_CAST = 0x18, /* br_on_cast */
+ WASM_OP_BR_ON_CAST_FAIL = 0x19, /* br_on_cast_fail */
+
+ WASM_OP_ANY_CONVERT_EXTERN = 0x1A, /* any.convert_extern */
+ WASM_OP_EXTERN_CONVERT_ANY = 0x1B, /* extern.covert_any */
+
+ WASM_OP_REF_I31 = 0x1C, /* ref.i31 */
+ WASM_OP_I31_GET_S = 0x1D, /* i31.get_s */
+ WASM_OP_I31_GET_U = 0x1E, /* i31.get_u */
+
+ /* stringref related opcodes */
+ WASM_OP_STRING_NEW_UTF8 = 0x80, /* string.new_utf8 */
+ WASM_OP_STRING_NEW_WTF16 = 0x81, /* string.new_wtf16 */
+ WASM_OP_STRING_CONST = 0x82, /* string.const */
+ WASM_OP_STRING_MEASURE_UTF8 = 0x83, /* string.measure_utf8 */
+ WASM_OP_STRING_MEASURE_WTF8 = 0x84, /* string.measure_wtf8 */
+ WASM_OP_STRING_MEASURE_WTF16 = 0x85, /* string.measure_wtf16 */
+ WASM_OP_STRING_ENCODE_UTF8 = 0x86, /* string.encode_utf8 */
+ WASM_OP_STRING_ENCODE_WTF16 = 0x87, /* string.encode_wtf16 */
+ WASM_OP_STRING_CONCAT = 0x88, /* string.concat */
+ WASM_OP_STRING_EQ = 0x89, /* string.eq */
+ WASM_OP_STRING_IS_USV_SEQUENCE = 0x8a, /* string.is_usv_sequence */
+ WASM_OP_STRING_NEW_LOSSY_UTF8 = 0x8b, /* string.new_lossy_utf8 */
+ WASM_OP_STRING_NEW_WTF8 = 0x8c, /* string.new_wtf8 */
+ WASM_OP_STRING_ENCODE_LOSSY_UTF8 = 0x8d, /* string.encode_lossy_utf8 */
+ WASM_OP_STRING_ENCODE_WTF8 = 0x8e, /* string.encode_wtf8 */
+
+ WASM_OP_STRING_AS_WTF8 = 0x90, /* string.as_wtf8 */
+ WASM_OP_STRINGVIEW_WTF8_ADVANCE = 0x91, /* stringview_wtf8.advance */
+ WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8 =
+ 0x92, /* stringview_wtf8.encode_utf8 */
+ WASM_OP_STRINGVIEW_WTF8_SLICE = 0x93, /* stringview_wtf8.slice */
+ WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8 =
+ 0x94, /* stringview_wtf8.encode_lossy_utf8 */
+ WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8 =
+ 0x95, /* stringview_wtf8.encode_wtf8 */
+
+ WASM_OP_STRING_AS_WTF16 = 0x98, /* string.as_wtf16 */
+ WASM_OP_STRINGVIEW_WTF16_LENGTH = 0x99, /* stringview_wtf16.length */
+ WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT =
+ 0x9a, /* stringview_wtf16.get_codeunit */
+ WASM_OP_STRINGVIEW_WTF16_ENCODE = 0x9b, /* stringview_wtf16.encode */
+ WASM_OP_STRINGVIEW_WTF16_SLICE = 0x9c, /* stringview_wtf16.slice */
+
+ WASM_OP_STRING_AS_ITER = 0xa0, /* string.as_iter */
+ WASM_OP_STRINGVIEW_ITER_NEXT = 0xa1, /* stringview_iter.next */
+ WASM_OP_STRINGVIEW_ITER_ADVANCE = 0xa2, /* stringview_iter.advance */
+ WASM_OP_STRINGVIEW_ITER_REWIND = 0xa3, /* stringview_iter.rewind */
+ WASM_OP_STRINGVIEW_ITER_SLICE = 0xa4, /* stringview_iter.slice */
+
+ WASM_OP_STRING_NEW_UTF8_ARRAY = 0xb0, /* string.new_utf8_array */
+ WASM_OP_STRING_NEW_WTF16_ARRAY = 0xb1, /* string.new_wtf16_array */
+ WASM_OP_STRING_ENCODE_UTF8_ARRAY = 0xb2, /* string.encode_utf8_array */
+ WASM_OP_STRING_ENCODE_WTF16_ARRAY = 0xb3, /* string.encode_wtf16_array */
+ WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY =
+ 0xb4, /* string.new_lossy_utf8_array */
+ WASM_OP_STRING_NEW_WTF8_ARRAY = 0xb5, /* string.new_wtf8_array */
+ WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY =
+ 0xb6, /* string.encode_lossy_utf8_array */
+ WASM_OP_STRING_ENCODE_WTF8_ARRAY = 0xb7, /* string.encode_wtf8_array */
+} WASMGCEXTOpcode;
+
+typedef enum WASMMiscEXTOpcode {
+ WASM_OP_I32_TRUNC_SAT_S_F32 = 0x00,
+ WASM_OP_I32_TRUNC_SAT_U_F32 = 0x01,
+ WASM_OP_I32_TRUNC_SAT_S_F64 = 0x02,
+ WASM_OP_I32_TRUNC_SAT_U_F64 = 0x03,
+ WASM_OP_I64_TRUNC_SAT_S_F32 = 0x04,
+ WASM_OP_I64_TRUNC_SAT_U_F32 = 0x05,
+ WASM_OP_I64_TRUNC_SAT_S_F64 = 0x06,
+ WASM_OP_I64_TRUNC_SAT_U_F64 = 0x07,
+ WASM_OP_MEMORY_INIT = 0x08,
+ WASM_OP_DATA_DROP = 0x09,
+ WASM_OP_MEMORY_COPY = 0x0a,
+ WASM_OP_MEMORY_FILL = 0x0b,
+ WASM_OP_TABLE_INIT = 0x0c,
+ WASM_OP_ELEM_DROP = 0x0d,
+ WASM_OP_TABLE_COPY = 0x0e,
+ WASM_OP_TABLE_GROW = 0x0f,
+ WASM_OP_TABLE_SIZE = 0x10,
+ WASM_OP_TABLE_FILL = 0x11,
+} WASMMiscEXTOpcode;
+
+typedef enum WASMSimdEXTOpcode {
+ /* memory instruction */
+ SIMD_v128_load = 0x00,
+ SIMD_v128_load8x8_s = 0x01,
+ SIMD_v128_load8x8_u = 0x02,
+ SIMD_v128_load16x4_s = 0x03,
+ SIMD_v128_load16x4_u = 0x04,
+ SIMD_v128_load32x2_s = 0x05,
+ SIMD_v128_load32x2_u = 0x06,
+ SIMD_v128_load8_splat = 0x07,
+ SIMD_v128_load16_splat = 0x08,
+ SIMD_v128_load32_splat = 0x09,
+ SIMD_v128_load64_splat = 0x0a,
+ SIMD_v128_store = 0x0b,
+
+ /* basic operation */
+ SIMD_v128_const = 0x0c,
+ SIMD_v8x16_shuffle = 0x0d,
+ SIMD_v8x16_swizzle = 0x0e,
+
+ /* splat operation */
+ SIMD_i8x16_splat = 0x0f,
+ SIMD_i16x8_splat = 0x10,
+ SIMD_i32x4_splat = 0x11,
+ SIMD_i64x2_splat = 0x12,
+ SIMD_f32x4_splat = 0x13,
+ SIMD_f64x2_splat = 0x14,
+
+ /* lane operation */
+ SIMD_i8x16_extract_lane_s = 0x15,
+ SIMD_i8x16_extract_lane_u = 0x16,
+ SIMD_i8x16_replace_lane = 0x17,
+ SIMD_i16x8_extract_lane_s = 0x18,
+ SIMD_i16x8_extract_lane_u = 0x19,
+ SIMD_i16x8_replace_lane = 0x1a,
+ SIMD_i32x4_extract_lane = 0x1b,
+ SIMD_i32x4_replace_lane = 0x1c,
+ SIMD_i64x2_extract_lane = 0x1d,
+ SIMD_i64x2_replace_lane = 0x1e,
+ SIMD_f32x4_extract_lane = 0x1f,
+ SIMD_f32x4_replace_lane = 0x20,
+ SIMD_f64x2_extract_lane = 0x21,
+ SIMD_f64x2_replace_lane = 0x22,
+
+ /* i8x16 compare operation */
+ SIMD_i8x16_eq = 0x23,
+ SIMD_i8x16_ne = 0x24,
+ SIMD_i8x16_lt_s = 0x25,
+ SIMD_i8x16_lt_u = 0x26,
+ SIMD_i8x16_gt_s = 0x27,
+ SIMD_i8x16_gt_u = 0x28,
+ SIMD_i8x16_le_s = 0x29,
+ SIMD_i8x16_le_u = 0x2a,
+ SIMD_i8x16_ge_s = 0x2b,
+ SIMD_i8x16_ge_u = 0x2c,
+
+ /* i16x8 compare operation */
+ SIMD_i16x8_eq = 0x2d,
+ SIMD_i16x8_ne = 0x2e,
+ SIMD_i16x8_lt_s = 0x2f,
+ SIMD_i16x8_lt_u = 0x30,
+ SIMD_i16x8_gt_s = 0x31,
+ SIMD_i16x8_gt_u = 0x32,
+ SIMD_i16x8_le_s = 0x33,
+ SIMD_i16x8_le_u = 0x34,
+ SIMD_i16x8_ge_s = 0x35,
+ SIMD_i16x8_ge_u = 0x36,
+
+ /* i32x4 compare operation */
+ SIMD_i32x4_eq = 0x37,
+ SIMD_i32x4_ne = 0x38,
+ SIMD_i32x4_lt_s = 0x39,
+ SIMD_i32x4_lt_u = 0x3a,
+ SIMD_i32x4_gt_s = 0x3b,
+ SIMD_i32x4_gt_u = 0x3c,
+ SIMD_i32x4_le_s = 0x3d,
+ SIMD_i32x4_le_u = 0x3e,
+ SIMD_i32x4_ge_s = 0x3f,
+ SIMD_i32x4_ge_u = 0x40,
+
+ /* f32x4 compare operation */
+ SIMD_f32x4_eq = 0x41,
+ SIMD_f32x4_ne = 0x42,
+ SIMD_f32x4_lt = 0x43,
+ SIMD_f32x4_gt = 0x44,
+ SIMD_f32x4_le = 0x45,
+ SIMD_f32x4_ge = 0x46,
+
+ /* f64x2 compare operation */
+ SIMD_f64x2_eq = 0x47,
+ SIMD_f64x2_ne = 0x48,
+ SIMD_f64x2_lt = 0x49,
+ SIMD_f64x2_gt = 0x4a,
+ SIMD_f64x2_le = 0x4b,
+ SIMD_f64x2_ge = 0x4c,
+
+ /* v128 operation */
+ SIMD_v128_not = 0x4d,
+ SIMD_v128_and = 0x4e,
+ SIMD_v128_andnot = 0x4f,
+ SIMD_v128_or = 0x50,
+ SIMD_v128_xor = 0x51,
+ SIMD_v128_bitselect = 0x52,
+ SIMD_v128_any_true = 0x53,
+
+ /* Load Lane Operation */
+ SIMD_v128_load8_lane = 0x54,
+ SIMD_v128_load16_lane = 0x55,
+ SIMD_v128_load32_lane = 0x56,
+ SIMD_v128_load64_lane = 0x57,
+ SIMD_v128_store8_lane = 0x58,
+ SIMD_v128_store16_lane = 0x59,
+ SIMD_v128_store32_lane = 0x5a,
+ SIMD_v128_store64_lane = 0x5b,
+ SIMD_v128_load32_zero = 0x5c,
+ SIMD_v128_load64_zero = 0x5d,
+
+ /* Float conversion */
+ SIMD_f32x4_demote_f64x2_zero = 0x5e,
+ SIMD_f64x2_promote_low_f32x4_zero = 0x5f,
+
+ /* i8x16 Operation */
+ SIMD_i8x16_abs = 0x60,
+ SIMD_i8x16_neg = 0x61,
+ SIMD_i8x16_popcnt = 0x62,
+ SIMD_i8x16_all_true = 0x63,
+ SIMD_i8x16_bitmask = 0x64,
+ SIMD_i8x16_narrow_i16x8_s = 0x65,
+ SIMD_i8x16_narrow_i16x8_u = 0x66,
+ SIMD_f32x4_ceil = 0x67,
+ SIMD_f32x4_floor = 0x68,
+ SIMD_f32x4_trunc = 0x69,
+ SIMD_f32x4_nearest = 0x6a,
+ SIMD_i8x16_shl = 0x6b,
+ SIMD_i8x16_shr_s = 0x6c,
+ SIMD_i8x16_shr_u = 0x6d,
+ SIMD_i8x16_add = 0x6e,
+ SIMD_i8x16_add_sat_s = 0x6f,
+ SIMD_i8x16_add_sat_u = 0x70,
+ SIMD_i8x16_sub = 0x71,
+ SIMD_i8x16_sub_sat_s = 0x72,
+ SIMD_i8x16_sub_sat_u = 0x73,
+ SIMD_f64x2_ceil = 0x74,
+ SIMD_f64x2_floor = 0x75,
+ SIMD_i8x16_min_s = 0x76,
+ SIMD_i8x16_min_u = 0x77,
+ SIMD_i8x16_max_s = 0x78,
+ SIMD_i8x16_max_u = 0x79,
+ SIMD_f64x2_trunc = 0x7a,
+ SIMD_i8x16_avgr_u = 0x7b,
+ SIMD_i16x8_extadd_pairwise_i8x16_s = 0x7c,
+ SIMD_i16x8_extadd_pairwise_i8x16_u = 0x7d,
+ SIMD_i32x4_extadd_pairwise_i16x8_s = 0x7e,
+ SIMD_i32x4_extadd_pairwise_i16x8_u = 0x7f,
+
+ /* i16x8 operation */
+ SIMD_i16x8_abs = 0x80,
+ SIMD_i16x8_neg = 0x81,
+ SIMD_i16x8_q15mulr_sat_s = 0x82,
+ SIMD_i16x8_all_true = 0x83,
+ SIMD_i16x8_bitmask = 0x84,
+ SIMD_i16x8_narrow_i32x4_s = 0x85,
+ SIMD_i16x8_narrow_i32x4_u = 0x86,
+ SIMD_i16x8_extend_low_i8x16_s = 0x87,
+ SIMD_i16x8_extend_high_i8x16_s = 0x88,
+ SIMD_i16x8_extend_low_i8x16_u = 0x89,
+ SIMD_i16x8_extend_high_i8x16_u = 0x8a,
+ SIMD_i16x8_shl = 0x8b,
+ SIMD_i16x8_shr_s = 0x8c,
+ SIMD_i16x8_shr_u = 0x8d,
+ SIMD_i16x8_add = 0x8e,
+ SIMD_i16x8_add_sat_s = 0x8f,
+ SIMD_i16x8_add_sat_u = 0x90,
+ SIMD_i16x8_sub = 0x91,
+ SIMD_i16x8_sub_sat_s = 0x92,
+ SIMD_i16x8_sub_sat_u = 0x93,
+ SIMD_f64x2_nearest = 0x94,
+ SIMD_i16x8_mul = 0x95,
+ SIMD_i16x8_min_s = 0x96,
+ SIMD_i16x8_min_u = 0x97,
+ SIMD_i16x8_max_s = 0x98,
+ SIMD_i16x8_max_u = 0x99,
+ /* placeholder = 0x9a */
+ SIMD_i16x8_avgr_u = 0x9b,
+ SIMD_i16x8_extmul_low_i8x16_s = 0x9c,
+ SIMD_i16x8_extmul_high_i8x16_s = 0x9d,
+ SIMD_i16x8_extmul_low_i8x16_u = 0x9e,
+ SIMD_i16x8_extmul_high_i8x16_u = 0x9f,
+
+ /* i32x4 operation */
+ SIMD_i32x4_abs = 0xa0,
+ SIMD_i32x4_neg = 0xa1,
+ /* placeholder = 0xa2 */
+ SIMD_i32x4_all_true = 0xa3,
+ SIMD_i32x4_bitmask = 0xa4,
+ /* placeholder = 0xa5 */
+ /* placeholder = 0xa6 */
+ SIMD_i32x4_extend_low_i16x8_s = 0xa7,
+ SIMD_i32x4_extend_high_i16x8_s = 0xa8,
+ SIMD_i32x4_extend_low_i16x8_u = 0xa9,
+ SIMD_i32x4_extend_high_i16x8_u = 0xaa,
+ SIMD_i32x4_shl = 0xab,
+ SIMD_i32x4_shr_s = 0xac,
+ SIMD_i32x4_shr_u = 0xad,
+ SIMD_i32x4_add = 0xae,
+ /* placeholder = 0xaf */
+ /* placeholder = 0xb0 */
+ SIMD_i32x4_sub = 0xb1,
+ /* placeholder = 0xb2 */
+ /* placeholder = 0xb3 */
+ /* placeholder = 0xb4 */
+ SIMD_i32x4_mul = 0xb5,
+ SIMD_i32x4_min_s = 0xb6,
+ SIMD_i32x4_min_u = 0xb7,
+ SIMD_i32x4_max_s = 0xb8,
+ SIMD_i32x4_max_u = 0xb9,
+ SIMD_i32x4_dot_i16x8_s = 0xba,
+ /* placeholder = 0xbb */
+ SIMD_i32x4_extmul_low_i16x8_s = 0xbc,
+ SIMD_i32x4_extmul_high_i16x8_s = 0xbd,
+ SIMD_i32x4_extmul_low_i16x8_u = 0xbe,
+ SIMD_i32x4_extmul_high_i16x8_u = 0xbf,
+
+ /* i64x2 operation */
+ SIMD_i64x2_abs = 0xc0,
+ SIMD_i64x2_neg = 0xc1,
+ /* placeholder = 0xc2 */
+ SIMD_i64x2_all_true = 0xc3,
+ SIMD_i64x2_bitmask = 0xc4,
+ /* placeholder = 0xc5 */
+ /* placeholder = 0xc6 */
+ SIMD_i64x2_extend_low_i32x4_s = 0xc7,
+ SIMD_i64x2_extend_high_i32x4_s = 0xc8,
+ SIMD_i64x2_extend_low_i32x4_u = 0xc9,
+ SIMD_i64x2_extend_high_i32x4_u = 0xca,
+ SIMD_i64x2_shl = 0xcb,
+ SIMD_i64x2_shr_s = 0xcc,
+ SIMD_i64x2_shr_u = 0xcd,
+ SIMD_i64x2_add = 0xce,
+ /* placeholder = 0xcf */
+ /* placeholder = 0xd0 */
+ SIMD_i64x2_sub = 0xd1,
+ /* placeholder = 0xd2 */
+ /* placeholder = 0xd3 */
+ /* placeholder = 0xd4 */
+ SIMD_i64x2_mul = 0xd5,
+ SIMD_i64x2_eq = 0xd6,
+ SIMD_i64x2_ne = 0xd7,
+ SIMD_i64x2_lt_s = 0xd8,
+ SIMD_i64x2_gt_s = 0xd9,
+ SIMD_i64x2_le_s = 0xda,
+ SIMD_i64x2_ge_s = 0xdb,
+ SIMD_i64x2_extmul_low_i32x4_s = 0xdc,
+ SIMD_i64x2_extmul_high_i32x4_s = 0xdd,
+ SIMD_i64x2_extmul_low_i32x4_u = 0xde,
+ SIMD_i64x2_extmul_high_i32x4_u = 0xdf,
+
+ /* f32x4 operation */
+ SIMD_f32x4_abs = 0xe0,
+ SIMD_f32x4_neg = 0xe1,
+ /* placeholder = 0xe2 */
+ SIMD_f32x4_sqrt = 0xe3,
+ SIMD_f32x4_add = 0xe4,
+ SIMD_f32x4_sub = 0xe5,
+ SIMD_f32x4_mul = 0xe6,
+ SIMD_f32x4_div = 0xe7,
+ SIMD_f32x4_min = 0xe8,
+ SIMD_f32x4_max = 0xe9,
+ SIMD_f32x4_pmin = 0xea,
+ SIMD_f32x4_pmax = 0xeb,
+
+ /* f64x2 operation */
+ SIMD_f64x2_abs = 0xec,
+ SIMD_f64x2_neg = 0xed,
+ /* placeholder = 0xee */
+ SIMD_f64x2_sqrt = 0xef,
+ SIMD_f64x2_add = 0xf0,
+ SIMD_f64x2_sub = 0xf1,
+ SIMD_f64x2_mul = 0xf2,
+ SIMD_f64x2_div = 0xf3,
+ SIMD_f64x2_min = 0xf4,
+ SIMD_f64x2_max = 0xf5,
+ SIMD_f64x2_pmin = 0xf6,
+ SIMD_f64x2_pmax = 0xf7,
+
+ /* conversion operation */
+ SIMD_i32x4_trunc_sat_f32x4_s = 0xf8,
+ SIMD_i32x4_trunc_sat_f32x4_u = 0xf9,
+ SIMD_f32x4_convert_i32x4_s = 0xfa,
+ SIMD_f32x4_convert_i32x4_u = 0xfb,
+ SIMD_i32x4_trunc_sat_f64x2_s_zero = 0xfc,
+ SIMD_i32x4_trunc_sat_f64x2_u_zero = 0xfd,
+ SIMD_f64x2_convert_low_i32x4_s = 0xfe,
+ SIMD_f64x2_convert_low_i32x4_u = 0xff,
+} WASMSimdEXTOpcode;
+
+typedef enum WASMAtomicEXTOpcode {
+ /* atomic wait and notify */
+ WASM_OP_ATOMIC_NOTIFY = 0x00,
+ WASM_OP_ATOMIC_WAIT32 = 0x01,
+ WASM_OP_ATOMIC_WAIT64 = 0x02,
+ WASM_OP_ATOMIC_FENCE = 0x03,
+ /* atomic load and store */
+ WASM_OP_ATOMIC_I32_LOAD = 0x10,
+ WASM_OP_ATOMIC_I64_LOAD = 0x11,
+ WASM_OP_ATOMIC_I32_LOAD8_U = 0x12,
+ WASM_OP_ATOMIC_I32_LOAD16_U = 0x13,
+ WASM_OP_ATOMIC_I64_LOAD8_U = 0x14,
+ WASM_OP_ATOMIC_I64_LOAD16_U = 0x15,
+ WASM_OP_ATOMIC_I64_LOAD32_U = 0x16,
+ WASM_OP_ATOMIC_I32_STORE = 0x17,
+ WASM_OP_ATOMIC_I64_STORE = 0x18,
+ WASM_OP_ATOMIC_I32_STORE8 = 0x19,
+ WASM_OP_ATOMIC_I32_STORE16 = 0x1a,
+ WASM_OP_ATOMIC_I64_STORE8 = 0x1b,
+ WASM_OP_ATOMIC_I64_STORE16 = 0x1c,
+ WASM_OP_ATOMIC_I64_STORE32 = 0x1d,
+ /* atomic add */
+ WASM_OP_ATOMIC_RMW_I32_ADD = 0x1e,
+ WASM_OP_ATOMIC_RMW_I64_ADD = 0x1f,
+ WASM_OP_ATOMIC_RMW_I32_ADD8_U = 0x20,
+ WASM_OP_ATOMIC_RMW_I32_ADD16_U = 0x21,
+ WASM_OP_ATOMIC_RMW_I64_ADD8_U = 0x22,
+ WASM_OP_ATOMIC_RMW_I64_ADD16_U = 0x23,
+ WASM_OP_ATOMIC_RMW_I64_ADD32_U = 0x24,
+ /* atomic sub */
+ WASM_OP_ATOMIC_RMW_I32_SUB = 0x25,
+ WASM_OP_ATOMIC_RMW_I64_SUB = 0x26,
+ WASM_OP_ATOMIC_RMW_I32_SUB8_U = 0x27,
+ WASM_OP_ATOMIC_RMW_I32_SUB16_U = 0x28,
+ WASM_OP_ATOMIC_RMW_I64_SUB8_U = 0x29,
+ WASM_OP_ATOMIC_RMW_I64_SUB16_U = 0x2a,
+ WASM_OP_ATOMIC_RMW_I64_SUB32_U = 0x2b,
+ /* atomic and */
+ WASM_OP_ATOMIC_RMW_I32_AND = 0x2c,
+ WASM_OP_ATOMIC_RMW_I64_AND = 0x2d,
+ WASM_OP_ATOMIC_RMW_I32_AND8_U = 0x2e,
+ WASM_OP_ATOMIC_RMW_I32_AND16_U = 0x2f,
+ WASM_OP_ATOMIC_RMW_I64_AND8_U = 0x30,
+ WASM_OP_ATOMIC_RMW_I64_AND16_U = 0x31,
+ WASM_OP_ATOMIC_RMW_I64_AND32_U = 0x32,
+ /* atomic or */
+ WASM_OP_ATOMIC_RMW_I32_OR = 0x33,
+ WASM_OP_ATOMIC_RMW_I64_OR = 0x34,
+ WASM_OP_ATOMIC_RMW_I32_OR8_U = 0x35,
+ WASM_OP_ATOMIC_RMW_I32_OR16_U = 0x36,
+ WASM_OP_ATOMIC_RMW_I64_OR8_U = 0x37,
+ WASM_OP_ATOMIC_RMW_I64_OR16_U = 0x38,
+ WASM_OP_ATOMIC_RMW_I64_OR32_U = 0x39,
+ /* atomic xor */
+ WASM_OP_ATOMIC_RMW_I32_XOR = 0x3a,
+ WASM_OP_ATOMIC_RMW_I64_XOR = 0x3b,
+ WASM_OP_ATOMIC_RMW_I32_XOR8_U = 0x3c,
+ WASM_OP_ATOMIC_RMW_I32_XOR16_U = 0x3d,
+ WASM_OP_ATOMIC_RMW_I64_XOR8_U = 0x3e,
+ WASM_OP_ATOMIC_RMW_I64_XOR16_U = 0x3f,
+ WASM_OP_ATOMIC_RMW_I64_XOR32_U = 0x40,
+ /* atomic xchg */
+ WASM_OP_ATOMIC_RMW_I32_XCHG = 0x41,
+ WASM_OP_ATOMIC_RMW_I64_XCHG = 0x42,
+ WASM_OP_ATOMIC_RMW_I32_XCHG8_U = 0x43,
+ WASM_OP_ATOMIC_RMW_I32_XCHG16_U = 0x44,
+ WASM_OP_ATOMIC_RMW_I64_XCHG8_U = 0x45,
+ WASM_OP_ATOMIC_RMW_I64_XCHG16_U = 0x46,
+ WASM_OP_ATOMIC_RMW_I64_XCHG32_U = 0x47,
+ /* atomic cmpxchg */
+ WASM_OP_ATOMIC_RMW_I32_CMPXCHG = 0x48,
+ WASM_OP_ATOMIC_RMW_I64_CMPXCHG = 0x49,
+ WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U = 0x4a,
+ WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U = 0x4b,
+ WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U = 0x4c,
+ WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U = 0x4d,
+ WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U = 0x4e,
+} WASMAtomicEXTOpcode;
+
+#if WASM_ENABLE_DEBUG_INTERP != 0
+#define DEF_DEBUG_BREAK_HANDLE() \
+ [DEBUG_OP_BREAK] = HANDLE_OPCODE(DEBUG_OP_BREAK), /* 0xdb */
+#else
+#define DEF_DEBUG_BREAK_HANDLE()
+#endif
+
+#define SET_GOTO_TABLE_ELEM(opcode) [opcode] = HANDLE_OPCODE(opcode)
+
+#if WASM_ENABLE_JIT != 0 && WASM_ENABLE_SIMD != 0
+#define SET_GOTO_TABLE_SIMD_PREFIX_ELEM() \
+ SET_GOTO_TABLE_ELEM(WASM_OP_SIMD_PREFIX),
+#else
+#define SET_GOTO_TABLE_SIMD_PREFIX_ELEM()
+#endif
+
+/*
+ * Macro used to generate computed goto tables for the C interpreter.
+ */
+#define WASM_INSTRUCTION_NUM 256
+
+#define DEFINE_GOTO_TABLE(type, _name) \
+ static type _name[WASM_INSTRUCTION_NUM] = { \
+ HANDLE_OPCODE(WASM_OP_UNREACHABLE), /* 0x00 */ \
+ HANDLE_OPCODE(WASM_OP_NOP), /* 0x01 */ \
+ HANDLE_OPCODE(WASM_OP_BLOCK), /* 0x02 */ \
+ HANDLE_OPCODE(WASM_OP_LOOP), /* 0x03 */ \
+ HANDLE_OPCODE(WASM_OP_IF), /* 0x04 */ \
+ HANDLE_OPCODE(WASM_OP_ELSE), /* 0x05 */ \
+ HANDLE_OPCODE(WASM_OP_TRY), /* 0x06 */ \
+ HANDLE_OPCODE(WASM_OP_CATCH), /* 0x07 */ \
+ HANDLE_OPCODE(WASM_OP_THROW), /* 0x08 */ \
+ HANDLE_OPCODE(WASM_OP_RETHROW), /* 0x09 */ \
+ HANDLE_OPCODE(WASM_OP_UNUSED_0x0a), /* 0x0a */ \
+ HANDLE_OPCODE(WASM_OP_END), /* 0x0b */ \
+ HANDLE_OPCODE(WASM_OP_BR), /* 0x0c */ \
+ HANDLE_OPCODE(WASM_OP_BR_IF), /* 0x0d */ \
+ HANDLE_OPCODE(WASM_OP_BR_TABLE), /* 0x0e */ \
+ HANDLE_OPCODE(WASM_OP_RETURN), /* 0x0f */ \
+ HANDLE_OPCODE(WASM_OP_CALL), /* 0x10 */ \
+ HANDLE_OPCODE(WASM_OP_CALL_INDIRECT), /* 0x11 */ \
+ HANDLE_OPCODE(WASM_OP_RETURN_CALL), /* 0x12 */ \
+ HANDLE_OPCODE(WASM_OP_RETURN_CALL_INDIRECT), /* 0x13 */ \
+ HANDLE_OPCODE(WASM_OP_CALL_REF), /* 0x14 */ \
+ HANDLE_OPCODE(WASM_OP_RETURN_CALL_REF), /* 0x15 */ \
+ HANDLE_OPCODE(WASM_OP_UNUSED_0x16), /* 0x16 */ \
+ HANDLE_OPCODE(WASM_OP_UNUSED_0x17), /* 0x17 */ \
+ HANDLE_OPCODE(WASM_OP_DELEGATE), /* 0x18 */ \
+ HANDLE_OPCODE(WASM_OP_CATCH_ALL), /* 0x19 */ \
+ HANDLE_OPCODE(WASM_OP_DROP), /* 0x1a */ \
+ HANDLE_OPCODE(WASM_OP_SELECT), /* 0x1b */ \
+ HANDLE_OPCODE(WASM_OP_SELECT_T), /* 0x1c */ \
+ HANDLE_OPCODE(WASM_OP_GET_GLOBAL_64), /* 0x1d */ \
+ HANDLE_OPCODE(WASM_OP_SET_GLOBAL_64), /* 0x1e */ \
+ HANDLE_OPCODE(WASM_OP_SET_GLOBAL_AUX_STACK), /* 0x1f */ \
+ HANDLE_OPCODE(WASM_OP_GET_LOCAL), /* 0x20 */ \
+ HANDLE_OPCODE(WASM_OP_SET_LOCAL), /* 0x21 */ \
+ HANDLE_OPCODE(WASM_OP_TEE_LOCAL), /* 0x22 */ \
+ HANDLE_OPCODE(WASM_OP_GET_GLOBAL), /* 0x23 */ \
+ HANDLE_OPCODE(WASM_OP_SET_GLOBAL), /* 0x24 */ \
+ HANDLE_OPCODE(WASM_OP_TABLE_GET), /* 0x25 */ \
+ HANDLE_OPCODE(WASM_OP_TABLE_SET), /* 0x26 */ \
+ HANDLE_OPCODE(WASM_OP_UNUSED_0x27), /* 0x27 */ \
+ HANDLE_OPCODE(WASM_OP_I32_LOAD), /* 0x28 */ \
+ HANDLE_OPCODE(WASM_OP_I64_LOAD), /* 0x29 */ \
+ HANDLE_OPCODE(WASM_OP_F32_LOAD), /* 0x2a */ \
+ HANDLE_OPCODE(WASM_OP_F64_LOAD), /* 0x2b */ \
+ HANDLE_OPCODE(WASM_OP_I32_LOAD8_S), /* 0x2c */ \
+ HANDLE_OPCODE(WASM_OP_I32_LOAD8_U), /* 0x2d */ \
+ HANDLE_OPCODE(WASM_OP_I32_LOAD16_S), /* 0x2e */ \
+ HANDLE_OPCODE(WASM_OP_I32_LOAD16_U), /* 0x2f */ \
+ HANDLE_OPCODE(WASM_OP_I64_LOAD8_S), /* 0x30 */ \
+ HANDLE_OPCODE(WASM_OP_I64_LOAD8_U), /* 0x31 */ \
+ HANDLE_OPCODE(WASM_OP_I64_LOAD16_S), /* 0x32 */ \
+ HANDLE_OPCODE(WASM_OP_I64_LOAD16_U), /* 0x33 */ \
+ HANDLE_OPCODE(WASM_OP_I64_LOAD32_S), /* 0x34 */ \
+ HANDLE_OPCODE(WASM_OP_I64_LOAD32_U), /* 0x35 */ \
+ HANDLE_OPCODE(WASM_OP_I32_STORE), /* 0x36 */ \
+ HANDLE_OPCODE(WASM_OP_I64_STORE), /* 0x37 */ \
+ HANDLE_OPCODE(WASM_OP_F32_STORE), /* 0x38 */ \
+ HANDLE_OPCODE(WASM_OP_F64_STORE), /* 0x39 */ \
+ HANDLE_OPCODE(WASM_OP_I32_STORE8), /* 0x3a */ \
+ HANDLE_OPCODE(WASM_OP_I32_STORE16), /* 0x3b */ \
+ HANDLE_OPCODE(WASM_OP_I64_STORE8), /* 0x3c */ \
+ HANDLE_OPCODE(WASM_OP_I64_STORE16), /* 0x3d */ \
+ HANDLE_OPCODE(WASM_OP_I64_STORE32), /* 0x3e */ \
+ HANDLE_OPCODE(WASM_OP_MEMORY_SIZE), /* 0x3f */ \
+ HANDLE_OPCODE(WASM_OP_MEMORY_GROW), /* 0x40 */ \
+ HANDLE_OPCODE(WASM_OP_I32_CONST), /* 0x41 */ \
+ HANDLE_OPCODE(WASM_OP_I64_CONST), /* 0x42 */ \
+ HANDLE_OPCODE(WASM_OP_F32_CONST), /* 0x43 */ \
+ HANDLE_OPCODE(WASM_OP_F64_CONST), /* 0x44 */ \
+ HANDLE_OPCODE(WASM_OP_I32_EQZ), /* 0x45 */ \
+ HANDLE_OPCODE(WASM_OP_I32_EQ), /* 0x46 */ \
+ HANDLE_OPCODE(WASM_OP_I32_NE), /* 0x47 */ \
+ HANDLE_OPCODE(WASM_OP_I32_LT_S), /* 0x48 */ \
+ HANDLE_OPCODE(WASM_OP_I32_LT_U), /* 0x49 */ \
+ HANDLE_OPCODE(WASM_OP_I32_GT_S), /* 0x4a */ \
+ HANDLE_OPCODE(WASM_OP_I32_GT_U), /* 0x4b */ \
+ HANDLE_OPCODE(WASM_OP_I32_LE_S), /* 0x4c */ \
+ HANDLE_OPCODE(WASM_OP_I32_LE_U), /* 0x4d */ \
+ HANDLE_OPCODE(WASM_OP_I32_GE_S), /* 0x4e */ \
+ HANDLE_OPCODE(WASM_OP_I32_GE_U), /* 0x4f */ \
+ HANDLE_OPCODE(WASM_OP_I64_EQZ), /* 0x50 */ \
+ HANDLE_OPCODE(WASM_OP_I64_EQ), /* 0x51 */ \
+ HANDLE_OPCODE(WASM_OP_I64_NE), /* 0x52 */ \
+ HANDLE_OPCODE(WASM_OP_I64_LT_S), /* 0x53 */ \
+ HANDLE_OPCODE(WASM_OP_I64_LT_U), /* 0x54 */ \
+ HANDLE_OPCODE(WASM_OP_I64_GT_S), /* 0x55 */ \
+ HANDLE_OPCODE(WASM_OP_I64_GT_U), /* 0x56 */ \
+ HANDLE_OPCODE(WASM_OP_I64_LE_S), /* 0x57 */ \
+ HANDLE_OPCODE(WASM_OP_I64_LE_U), /* 0x58 */ \
+ HANDLE_OPCODE(WASM_OP_I64_GE_S), /* 0x59 */ \
+ HANDLE_OPCODE(WASM_OP_I64_GE_U), /* 0x5a */ \
+ HANDLE_OPCODE(WASM_OP_F32_EQ), /* 0x5b */ \
+ HANDLE_OPCODE(WASM_OP_F32_NE), /* 0x5c */ \
+ HANDLE_OPCODE(WASM_OP_F32_LT), /* 0x5d */ \
+ HANDLE_OPCODE(WASM_OP_F32_GT), /* 0x5e */ \
+ HANDLE_OPCODE(WASM_OP_F32_LE), /* 0x5f */ \
+ HANDLE_OPCODE(WASM_OP_F32_GE), /* 0x60 */ \
+ HANDLE_OPCODE(WASM_OP_F64_EQ), /* 0x61 */ \
+ HANDLE_OPCODE(WASM_OP_F64_NE), /* 0x62 */ \
+ HANDLE_OPCODE(WASM_OP_F64_LT), /* 0x63 */ \
+ HANDLE_OPCODE(WASM_OP_F64_GT), /* 0x64 */ \
+ HANDLE_OPCODE(WASM_OP_F64_LE), /* 0x65 */ \
+ HANDLE_OPCODE(WASM_OP_F64_GE), /* 0x66 */ \
+ HANDLE_OPCODE(WASM_OP_I32_CLZ), /* 0x67 */ \
+ HANDLE_OPCODE(WASM_OP_I32_CTZ), /* 0x68 */ \
+ HANDLE_OPCODE(WASM_OP_I32_POPCNT), /* 0x69 */ \
+ HANDLE_OPCODE(WASM_OP_I32_ADD), /* 0x6a */ \
+ HANDLE_OPCODE(WASM_OP_I32_SUB), /* 0x6b */ \
+ HANDLE_OPCODE(WASM_OP_I32_MUL), /* 0x6c */ \
+ HANDLE_OPCODE(WASM_OP_I32_DIV_S), /* 0x6d */ \
+ HANDLE_OPCODE(WASM_OP_I32_DIV_U), /* 0x6e */ \
+ HANDLE_OPCODE(WASM_OP_I32_REM_S), /* 0x6f */ \
+ HANDLE_OPCODE(WASM_OP_I32_REM_U), /* 0x70 */ \
+ HANDLE_OPCODE(WASM_OP_I32_AND), /* 0x71 */ \
+ HANDLE_OPCODE(WASM_OP_I32_OR), /* 0x72 */ \
+ HANDLE_OPCODE(WASM_OP_I32_XOR), /* 0x73 */ \
+ HANDLE_OPCODE(WASM_OP_I32_SHL), /* 0x74 */ \
+ HANDLE_OPCODE(WASM_OP_I32_SHR_S), /* 0x75 */ \
+ HANDLE_OPCODE(WASM_OP_I32_SHR_U), /* 0x76 */ \
+ HANDLE_OPCODE(WASM_OP_I32_ROTL), /* 0x77 */ \
+ HANDLE_OPCODE(WASM_OP_I32_ROTR), /* 0x78 */ \
+ HANDLE_OPCODE(WASM_OP_I64_CLZ), /* 0x79 */ \
+ HANDLE_OPCODE(WASM_OP_I64_CTZ), /* 0x7a */ \
+ HANDLE_OPCODE(WASM_OP_I64_POPCNT), /* 0x7b */ \
+ HANDLE_OPCODE(WASM_OP_I64_ADD), /* 0x7c */ \
+ HANDLE_OPCODE(WASM_OP_I64_SUB), /* 0x7d */ \
+ HANDLE_OPCODE(WASM_OP_I64_MUL), /* 0x7e */ \
+ HANDLE_OPCODE(WASM_OP_I64_DIV_S), /* 0x7f */ \
+ HANDLE_OPCODE(WASM_OP_I64_DIV_U), /* 0x80 */ \
+ HANDLE_OPCODE(WASM_OP_I64_REM_S), /* 0x81 */ \
+ HANDLE_OPCODE(WASM_OP_I64_REM_U), /* 0x82 */ \
+ HANDLE_OPCODE(WASM_OP_I64_AND), /* 0x83 */ \
+ HANDLE_OPCODE(WASM_OP_I64_OR), /* 0x84 */ \
+ HANDLE_OPCODE(WASM_OP_I64_XOR), /* 0x85 */ \
+ HANDLE_OPCODE(WASM_OP_I64_SHL), /* 0x86 */ \
+ HANDLE_OPCODE(WASM_OP_I64_SHR_S), /* 0x87 */ \
+ HANDLE_OPCODE(WASM_OP_I64_SHR_U), /* 0x88 */ \
+ HANDLE_OPCODE(WASM_OP_I64_ROTL), /* 0x89 */ \
+ HANDLE_OPCODE(WASM_OP_I64_ROTR), /* 0x8a */ \
+ HANDLE_OPCODE(WASM_OP_F32_ABS), /* 0x8b */ \
+ HANDLE_OPCODE(WASM_OP_F32_NEG), /* 0x8c */ \
+ HANDLE_OPCODE(WASM_OP_F32_CEIL), /* 0x8d */ \
+ HANDLE_OPCODE(WASM_OP_F32_FLOOR), /* 0x8e */ \
+ HANDLE_OPCODE(WASM_OP_F32_TRUNC), /* 0x8f */ \
+ HANDLE_OPCODE(WASM_OP_F32_NEAREST), /* 0x90 */ \
+ HANDLE_OPCODE(WASM_OP_F32_SQRT), /* 0x91 */ \
+ HANDLE_OPCODE(WASM_OP_F32_ADD), /* 0x92 */ \
+ HANDLE_OPCODE(WASM_OP_F32_SUB), /* 0x93 */ \
+ HANDLE_OPCODE(WASM_OP_F32_MUL), /* 0x94 */ \
+ HANDLE_OPCODE(WASM_OP_F32_DIV), /* 0x95 */ \
+ HANDLE_OPCODE(WASM_OP_F32_MIN), /* 0x96 */ \
+ HANDLE_OPCODE(WASM_OP_F32_MAX), /* 0x97 */ \
+ HANDLE_OPCODE(WASM_OP_F32_COPYSIGN), /* 0x98 */ \
+ HANDLE_OPCODE(WASM_OP_F64_ABS), /* 0x99 */ \
+ HANDLE_OPCODE(WASM_OP_F64_NEG), /* 0x9a */ \
+ HANDLE_OPCODE(WASM_OP_F64_CEIL), /* 0x9b */ \
+ HANDLE_OPCODE(WASM_OP_F64_FLOOR), /* 0x9c */ \
+ HANDLE_OPCODE(WASM_OP_F64_TRUNC), /* 0x9d */ \
+ HANDLE_OPCODE(WASM_OP_F64_NEAREST), /* 0x9e */ \
+ HANDLE_OPCODE(WASM_OP_F64_SQRT), /* 0x9f */ \
+ HANDLE_OPCODE(WASM_OP_F64_ADD), /* 0xa0 */ \
+ HANDLE_OPCODE(WASM_OP_F64_SUB), /* 0xa1 */ \
+ HANDLE_OPCODE(WASM_OP_F64_MUL), /* 0xa2 */ \
+ HANDLE_OPCODE(WASM_OP_F64_DIV), /* 0xa3 */ \
+ HANDLE_OPCODE(WASM_OP_F64_MIN), /* 0xa4 */ \
+ HANDLE_OPCODE(WASM_OP_F64_MAX), /* 0xa5 */ \
+ HANDLE_OPCODE(WASM_OP_F64_COPYSIGN), /* 0xa6 */ \
+ HANDLE_OPCODE(WASM_OP_I32_WRAP_I64), /* 0xa7 */ \
+ HANDLE_OPCODE(WASM_OP_I32_TRUNC_S_F32), /* 0xa8 */ \
+ HANDLE_OPCODE(WASM_OP_I32_TRUNC_U_F32), /* 0xa9 */ \
+ HANDLE_OPCODE(WASM_OP_I32_TRUNC_S_F64), /* 0xaa */ \
+ HANDLE_OPCODE(WASM_OP_I32_TRUNC_U_F64), /* 0xab */ \
+ HANDLE_OPCODE(WASM_OP_I64_EXTEND_S_I32), /* 0xac */ \
+ HANDLE_OPCODE(WASM_OP_I64_EXTEND_U_I32), /* 0xad */ \
+ HANDLE_OPCODE(WASM_OP_I64_TRUNC_S_F32), /* 0xae */ \
+ HANDLE_OPCODE(WASM_OP_I64_TRUNC_U_F32), /* 0xaf */ \
+ HANDLE_OPCODE(WASM_OP_I64_TRUNC_S_F64), /* 0xb0 */ \
+ HANDLE_OPCODE(WASM_OP_I64_TRUNC_U_F64), /* 0xb1 */ \
+ HANDLE_OPCODE(WASM_OP_F32_CONVERT_S_I32), /* 0xb2 */ \
+ HANDLE_OPCODE(WASM_OP_F32_CONVERT_U_I32), /* 0xb3 */ \
+ HANDLE_OPCODE(WASM_OP_F32_CONVERT_S_I64), /* 0xb4 */ \
+ HANDLE_OPCODE(WASM_OP_F32_CONVERT_U_I64), /* 0xb5 */ \
+ HANDLE_OPCODE(WASM_OP_F32_DEMOTE_F64), /* 0xb6 */ \
+ HANDLE_OPCODE(WASM_OP_F64_CONVERT_S_I32), /* 0xb7 */ \
+ HANDLE_OPCODE(WASM_OP_F64_CONVERT_U_I32), /* 0xb8 */ \
+ HANDLE_OPCODE(WASM_OP_F64_CONVERT_S_I64), /* 0xb9 */ \
+ HANDLE_OPCODE(WASM_OP_F64_CONVERT_U_I64), /* 0xba */ \
+ HANDLE_OPCODE(WASM_OP_F64_PROMOTE_F32), /* 0xbb */ \
+ HANDLE_OPCODE(WASM_OP_I32_REINTERPRET_F32), /* 0xbc */ \
+ HANDLE_OPCODE(WASM_OP_I64_REINTERPRET_F64), /* 0xbd */ \
+ HANDLE_OPCODE(WASM_OP_F32_REINTERPRET_I32), /* 0xbe */ \
+ HANDLE_OPCODE(WASM_OP_F64_REINTERPRET_I64), /* 0xbf */ \
+ HANDLE_OPCODE(WASM_OP_I32_EXTEND8_S), /* 0xc0 */ \
+ HANDLE_OPCODE(WASM_OP_I32_EXTEND16_S), /* 0xc1 */ \
+ HANDLE_OPCODE(WASM_OP_I64_EXTEND8_S), /* 0xc2 */ \
+ HANDLE_OPCODE(WASM_OP_I64_EXTEND16_S), /* 0xc3 */ \
+ HANDLE_OPCODE(WASM_OP_I64_EXTEND32_S), /* 0xc4 */ \
+ HANDLE_OPCODE(WASM_OP_DROP_64), /* 0xc5 */ \
+ HANDLE_OPCODE(WASM_OP_SELECT_64), /* 0xc6 */ \
+ HANDLE_OPCODE(EXT_OP_GET_LOCAL_FAST), /* 0xc7 */ \
+ HANDLE_OPCODE(EXT_OP_SET_LOCAL_FAST_I64), /* 0xc8 */ \
+ HANDLE_OPCODE(EXT_OP_SET_LOCAL_FAST), /* 0xc9 */ \
+ HANDLE_OPCODE(EXT_OP_TEE_LOCAL_FAST), /* 0xca */ \
+ HANDLE_OPCODE(EXT_OP_TEE_LOCAL_FAST_I64), /* 0xcb */ \
+ HANDLE_OPCODE(EXT_OP_COPY_STACK_TOP), /* 0xcc */ \
+ HANDLE_OPCODE(EXT_OP_COPY_STACK_TOP_I64), /* 0xcd */ \
+ HANDLE_OPCODE(EXT_OP_COPY_STACK_VALUES), /* 0xce */ \
+ HANDLE_OPCODE(WASM_OP_IMPDEP), /* 0xcf */ \
+ HANDLE_OPCODE(WASM_OP_REF_NULL), /* 0xd0 */ \
+ HANDLE_OPCODE(WASM_OP_REF_IS_NULL), /* 0xd1 */ \
+ HANDLE_OPCODE(WASM_OP_REF_FUNC), /* 0xd2 */ \
+ HANDLE_OPCODE(WASM_OP_REF_EQ), /* 0xd3 */ \
+ HANDLE_OPCODE(WASM_OP_REF_AS_NON_NULL), /* 0xd4 */ \
+ HANDLE_OPCODE(WASM_OP_BR_ON_NULL), /* 0xd5 */ \
+ HANDLE_OPCODE(WASM_OP_BR_ON_NON_NULL), /* 0xd6 */ \
+ HANDLE_OPCODE(EXT_OP_BLOCK), /* 0xd7 */ \
+ HANDLE_OPCODE(EXT_OP_LOOP), /* 0xd8 */ \
+ HANDLE_OPCODE(EXT_OP_IF), /* 0xd9 */ \
+ HANDLE_OPCODE(EXT_OP_BR_TABLE_CACHE), /* 0xda */ \
+ HANDLE_OPCODE(EXT_OP_TRY), /* 0xdb */ \
+ SET_GOTO_TABLE_ELEM(WASM_OP_GC_PREFIX), /* 0xfb */ \
+ SET_GOTO_TABLE_ELEM(WASM_OP_MISC_PREFIX), /* 0xfc */ \
+ SET_GOTO_TABLE_SIMD_PREFIX_ELEM() /* 0xfd */ \
+ SET_GOTO_TABLE_ELEM(WASM_OP_ATOMIC_PREFIX), /* 0xfe */ \
+ DEF_DEBUG_BREAK_HANDLE() \
+ };
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* end of _WASM_OPCODE_H */
diff --git a/lib/wamr/core/iwasm/interpreter/wasm_runtime.c b/lib/wamr/core/iwasm/interpreter/wasm_runtime.c
new file mode 100644
index 0000000..accb403
--- /dev/null
+++ b/lib/wamr/core/iwasm/interpreter/wasm_runtime.c
@@ -0,0 +1,5034 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include "wasm_runtime.h"
+#include "wasm.h"
+#include "wasm_loader.h"
+#include "wasm_interp.h"
+#include "bh_common.h"
+#include "bh_log.h"
+#include "mem_alloc.h"
+#include "../common/wasm_runtime_common.h"
+#include "../common/wasm_memory.h"
+#if WASM_ENABLE_GC != 0
+#include "../common/gc/gc_object.h"
+#endif
+#if WASM_ENABLE_SHARED_MEMORY != 0
+#include "../common/wasm_shared_memory.h"
+#endif
+#if WASM_ENABLE_THREAD_MGR != 0
+#include "../libraries/thread-mgr/thread_manager.h"
+#endif
+#if WASM_ENABLE_DEBUG_INTERP != 0
+#include "../libraries/debug-engine/debug_engine.h"
+#endif
+#if WASM_ENABLE_FAST_JIT != 0
+#include "../fast-jit/jit_compiler.h"
+#endif
+#if WASM_ENABLE_JIT != 0
+#include "../aot/aot_runtime.h"
+#endif
+
+static void
+set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
+{
+ if (error_buf != NULL) {
+ snprintf(error_buf, error_buf_size,
+ "WASM module instantiate failed: %s", string);
+ }
+}
+
+static void
+set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
+{
+ va_list args;
+ char buf[128];
+
+ if (error_buf != NULL) {
+ va_start(args, format);
+ vsnprintf(buf, sizeof(buf), format, args);
+ va_end(args);
+ snprintf(error_buf, error_buf_size,
+ "WASM module instantiate failed: %s", buf);
+ }
+}
+
+WASMModule *
+wasm_load(uint8 *buf, uint32 size,
+#if WASM_ENABLE_MULTI_MODULE != 0
+ bool main_module,
+#endif
+ const LoadArgs *name, char *error_buf, uint32 error_buf_size)
+{
+ return wasm_loader_load(buf, size,
+#if WASM_ENABLE_MULTI_MODULE != 0
+ main_module,
+#endif
+ name, error_buf, error_buf_size);
+}
+
+WASMModule *
+wasm_load_from_sections(WASMSection *section_list, char *error_buf,
+ uint32 error_buf_size)
+{
+ return wasm_loader_load_from_sections(section_list, error_buf,
+ error_buf_size);
+}
+
+void
+wasm_unload(WASMModule *module)
+{
+ wasm_loader_unload(module);
+}
+
+bool
+wasm_resolve_symbols(WASMModule *module)
+{
+ bool ret = true;
+ uint32 idx;
+ for (idx = 0; idx < module->import_function_count; ++idx) {
+ WASMFunctionImport *import = &module->import_functions[idx].u.function;
+ bool linked = import->func_ptr_linked;
+#if WASM_ENABLE_MULTI_MODULE != 0
+ if (import->import_func_linked) {
+ linked = true;
+ }
+#endif
+ if (!linked && !wasm_resolve_import_func(module, import)) {
+ ret = false;
+ }
+ }
+ return ret;
+}
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+static WASMFunction *
+wasm_resolve_function(const char *module_name, const char *function_name,
+ const WASMFuncType *expected_function_type,
+ char *error_buf, uint32 error_buf_size)
+{
+ WASMModuleCommon *module_reg;
+ WASMFunction *function = NULL;
+ WASMExport *export = NULL;
+ WASMModule *module = NULL;
+ WASMFuncType *target_function_type = NULL;
+
+ module_reg = wasm_runtime_find_module_registered(module_name);
+ 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);
+ set_error_buf(error_buf, error_buf_size, "unknown import");
+ return NULL;
+ }
+
+ module = (WASMModule *)module_reg;
+ export = loader_find_export((WASMModuleCommon *)module, module_name,
+ function_name, EXPORT_KIND_FUNC, error_buf,
+ error_buf_size);
+ if (!export) {
+ return NULL;
+ }
+
+ /* resolve function type and function */
+ if (export->index < module->import_function_count) {
+ target_function_type =
+ module->import_functions[export->index].u.function.func_type;
+ function = module->import_functions[export->index]
+ .u.function.import_func_linked;
+ }
+ else {
+ target_function_type =
+ module->functions[export->index - module->import_function_count]
+ ->func_type;
+ function =
+ module->functions[export->index - module->import_function_count];
+ }
+
+ /* check function type */
+ if (!wasm_type_equal((WASMType *)expected_function_type,
+ (WASMType *)target_function_type, module->types,
+ module->type_count)) {
+ LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
+ set_error_buf(error_buf, error_buf_size, "incompatible import type");
+ return NULL;
+ }
+
+ return function;
+}
+#endif
+
+bool
+wasm_resolve_import_func(const WASMModule *module, WASMFunctionImport *function)
+{
+#if WASM_ENABLE_MULTI_MODULE != 0
+ char error_buf[128];
+ WASMModule *sub_module = NULL;
+#endif
+ function->func_ptr_linked = wasm_native_resolve_symbol(
+ function->module_name, function->field_name, function->func_type,
+ &function->signature, &function->attachment, &function->call_conv_raw);
+
+ if (function->func_ptr_linked) {
+ return true;
+ }
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+ if (!wasm_runtime_is_built_in_module(function->module_name)) {
+ sub_module = (WASMModule *)wasm_runtime_load_depended_module(
+ (WASMModuleCommon *)module, function->module_name, error_buf,
+ sizeof(error_buf));
+ if (!sub_module) {
+ LOG_WARNING("failed to load sub module: %s", error_buf);
+ return false;
+ }
+ }
+ function->import_func_linked = wasm_resolve_function(
+ function->module_name, function->field_name, function->func_type,
+ error_buf, sizeof(error_buf));
+
+ if (function->import_func_linked) {
+ function->import_module = sub_module;
+ return true;
+ }
+ else {
+ LOG_WARNING("failed to link function (%s, %s): %s",
+ function->module_name, function->field_name, error_buf);
+ }
+#endif
+
+ return false;
+}
+
+static void *
+runtime_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
+{
+ void *mem;
+
+ if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
+ set_error_buf(error_buf, error_buf_size, "allocate memory failed");
+ return NULL;
+ }
+
+ memset(mem, 0, (uint32)size);
+ return mem;
+}
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+static WASMModuleInstance *
+get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
+ const WASMModule *sub_module)
+{
+ bh_list *sub_module_inst_list = parent_module_inst->e->sub_module_inst_list;
+ WASMSubModInstNode *node = bh_list_first_elem(sub_module_inst_list);
+
+ while (node && sub_module != node->module_inst->module) {
+ node = bh_list_elem_next(node);
+ }
+ return node ? node->module_inst : NULL;
+}
+#endif
+
+/**
+ * Destroy memory instances.
+ */
+static void
+memories_deinstantiate(WASMModuleInstance *module_inst,
+ WASMMemoryInstance **memories, uint32 count)
+{
+ uint32 i;
+ if (memories) {
+ for (i = 0; i < count; i++) {
+ if (memories[i]) {
+#if WASM_ENABLE_MULTI_MODULE != 0
+ WASMModule *module = module_inst->module;
+ if (i < module->import_memory_count
+ && module->import_memories[i].u.memory.import_module) {
+ continue;
+ }
+#endif
+#if WASM_ENABLE_SHARED_MEMORY != 0
+ if (shared_memory_is_shared(memories[i])) {
+ uint32 ref_count = shared_memory_dec_reference(memories[i]);
+ /* if the reference count is not zero,
+ don't free the memory */
+ if (ref_count > 0)
+ continue;
+ }
+#endif
+ if (memories[i]->heap_handle) {
+ mem_allocator_destroy(memories[i]->heap_handle);
+ wasm_runtime_free(memories[i]->heap_handle);
+ memories[i]->heap_handle = NULL;
+ }
+ if (memories[i]->memory_data) {
+ wasm_deallocate_linear_memory(memories[i]);
+ }
+ }
+ }
+ wasm_runtime_free(memories);
+ }
+ (void)module_inst;
+}
+
+static WASMMemoryInstance *
+memory_instantiate(WASMModuleInstance *module_inst, WASMModuleInstance *parent,
+ WASMMemoryInstance *memory, uint32 memory_idx,
+ uint32 num_bytes_per_page, uint32 init_page_count,
+ uint32 max_page_count, uint32 heap_size, uint32 flags,
+ char *error_buf, uint32 error_buf_size)
+{
+ WASMModule *module = module_inst->module;
+ uint32 inc_page_count, global_idx, default_max_page;
+ uint32 bytes_of_last_page, bytes_to_page_end;
+ uint64 aux_heap_base,
+ heap_offset = (uint64)num_bytes_per_page * init_page_count;
+ uint64 memory_data_size, max_memory_data_size;
+ uint8 *global_addr;
+
+ bool is_shared_memory = false;
+#if WASM_ENABLE_SHARED_MEMORY != 0
+ is_shared_memory = flags & SHARED_MEMORY_FLAG ? true : false;
+
+ /* shared memory */
+ if (is_shared_memory && parent != NULL) {
+ bh_assert(parent->memory_count > memory_idx);
+ memory = parent->memories[memory_idx];
+ shared_memory_inc_reference(memory);
+ return memory;
+ }
+#else
+ (void)parent;
+ (void)memory_idx;
+ (void)flags;
+#endif /* end of WASM_ENABLE_SHARED_MEMORY */
+
+#if WASM_ENABLE_MEMORY64 != 0
+ if (flags & MEMORY64_FLAG) {
+ memory->is_memory64 = 1;
+ }
+#endif
+ default_max_page =
+ memory->is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
+
+ /* The app heap should be in the default memory */
+ if (memory_idx == 0) {
+ if (heap_size > 0 && module_inst->module->malloc_function != (uint32)-1
+ && module_inst->module->free_function != (uint32)-1) {
+ /* Disable app heap, use malloc/free function exported
+ by wasm app to allocate/free memory instead */
+ heap_size = 0;
+ }
+
+ /* If initial memory is the largest size allowed, disallowing insert
+ * host managed heap */
+ if (heap_size > 0
+ && heap_offset == GET_MAX_LINEAR_MEMORY_SIZE(memory->is_memory64)) {
+ set_error_buf(error_buf, error_buf_size,
+ "failed to insert app heap into linear memory, "
+ "try using `--heap-size=0` option");
+ return NULL;
+ }
+
+ if (init_page_count == max_page_count && init_page_count == 1) {
+ /* If only one page and at most one page, we just append
+ the app heap to the end of linear memory, enlarge the
+ num_bytes_per_page, and don't change the page count */
+ heap_offset = num_bytes_per_page;
+ num_bytes_per_page += heap_size;
+ if (num_bytes_per_page < heap_size) {
+ set_error_buf(error_buf, error_buf_size,
+ "failed to insert app heap into linear memory, "
+ "try using `--heap-size=0` option");
+ return NULL;
+ }
+ }
+ else if (heap_size > 0) {
+ if (init_page_count == max_page_count && init_page_count == 0) {
+ /* If the memory data size is always 0, we resize it to
+ one page for app heap */
+ num_bytes_per_page = heap_size;
+ heap_offset = 0;
+ inc_page_count = 1;
+ }
+ else if (module->aux_heap_base_global_index != (uint32)-1
+ && module->aux_heap_base
+ < (uint64)num_bytes_per_page * init_page_count) {
+ /* Insert app heap before __heap_base */
+ aux_heap_base = module->aux_heap_base;
+ bytes_of_last_page = aux_heap_base % num_bytes_per_page;
+ if (bytes_of_last_page == 0)
+ bytes_of_last_page = num_bytes_per_page;
+ bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
+ inc_page_count =
+ (heap_size - bytes_to_page_end + num_bytes_per_page - 1)
+ / num_bytes_per_page;
+ heap_offset = aux_heap_base;
+ aux_heap_base += heap_size;
+
+ bytes_of_last_page = aux_heap_base % num_bytes_per_page;
+ if (bytes_of_last_page == 0)
+ bytes_of_last_page = num_bytes_per_page;
+ bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
+ if (bytes_to_page_end < 1 * BH_KB) {
+ aux_heap_base += 1 * BH_KB;
+ inc_page_count++;
+ }
+
+ /* Adjust __heap_base global value */
+ global_idx = module->aux_heap_base_global_index;
+ bh_assert(module_inst->e->globals
+ && global_idx < module_inst->e->global_count);
+ global_addr = module_inst->global_data
+ + module_inst->e->globals[global_idx].data_offset;
+#if WASM_ENABLE_MEMORY64 != 0
+ if (memory->is_memory64) {
+ /* For memory64, the global value should be i64 */
+ *(uint64 *)global_addr = aux_heap_base;
+ }
+ else
+#endif
+ {
+ /* For memory32, the global value should be i32 */
+ *(uint32 *)global_addr = (uint32)aux_heap_base;
+ }
+ LOG_VERBOSE("Reset __heap_base global to %" PRIu64,
+ aux_heap_base);
+ }
+ else {
+ /* Insert app heap before new page */
+ inc_page_count =
+ (heap_size + num_bytes_per_page - 1) / num_bytes_per_page;
+ heap_offset = (uint64)num_bytes_per_page * init_page_count;
+ heap_size = (uint64)num_bytes_per_page * inc_page_count;
+ if (heap_size > 0)
+ heap_size -= 1 * BH_KB;
+ }
+ init_page_count += inc_page_count;
+ max_page_count += inc_page_count;
+ if (init_page_count > default_max_page) {
+ set_error_buf(error_buf, error_buf_size,
+ "failed to insert app heap into linear memory, "
+ "try using `--heap-size=0` option");
+ return NULL;
+ }
+
+ if (max_page_count > default_max_page)
+ max_page_count = default_max_page;
+ }
+ }
+
+ LOG_VERBOSE("Memory instantiate:");
+ LOG_VERBOSE(" page bytes: %u, init pages: %u, max pages: %u",
+ num_bytes_per_page, init_page_count, max_page_count);
+ if (memory_idx == 0)
+ LOG_VERBOSE(" heap offset: %" PRIu64 ", heap size: %u\n", heap_offset,
+ heap_size);
+
+ max_memory_data_size = (uint64)num_bytes_per_page * max_page_count;
+ bh_assert(max_memory_data_size
+ <= GET_MAX_LINEAR_MEMORY_SIZE(memory->is_memory64));
+ (void)max_memory_data_size;
+
+ bh_assert(memory != NULL);
+
+ if (wasm_allocate_linear_memory(&memory->memory_data, is_shared_memory,
+ memory->is_memory64, num_bytes_per_page,
+ init_page_count, max_page_count,
+ &memory_data_size)
+ != BHT_OK) {
+ set_error_buf(error_buf, error_buf_size,
+ "allocate linear memory failed");
+ return NULL;
+ }
+
+ memory->module_type = Wasm_Module_Bytecode;
+ memory->num_bytes_per_page = num_bytes_per_page;
+ memory->cur_page_count = init_page_count;
+ memory->max_page_count = max_page_count;
+ memory->memory_data_size = memory_data_size;
+
+ if (memory_idx == 0) {
+ memory->heap_data = memory->memory_data + heap_offset;
+ memory->heap_data_end = memory->heap_data + heap_size;
+ memory->memory_data_end = memory->memory_data + memory_data_size;
+ }
+
+ /* Initialize heap */
+ if (memory_idx == 0 && heap_size > 0) {
+ uint32 heap_struct_size = mem_allocator_get_heap_struct_size();
+
+ if (!(memory->heap_handle = runtime_malloc(
+ (uint64)heap_struct_size, error_buf, error_buf_size))) {
+ goto fail1;
+ }
+ if (!mem_allocator_create_with_struct_and_pool(
+ memory->heap_handle, heap_struct_size, memory->heap_data,
+ heap_size)) {
+ set_error_buf(error_buf, error_buf_size, "init app heap failed");
+ goto fail2;
+ }
+ }
+
+ if (memory_data_size > 0) {
+ wasm_runtime_set_mem_bound_check_bytes(memory, memory_data_size);
+ }
+
+#if WASM_ENABLE_SHARED_MEMORY != 0
+ if (is_shared_memory) {
+ memory->is_shared_memory = 1;
+ memory->ref_count = 1;
+ }
+#endif
+
+ LOG_VERBOSE("Memory instantiate success.");
+ return memory;
+
+fail2:
+ if (memory_idx == 0 && heap_size > 0)
+ wasm_runtime_free(memory->heap_handle);
+fail1:
+ if (memory->memory_data)
+ wasm_deallocate_linear_memory(memory);
+
+ return NULL;
+}
+
+/**
+ * Instantiate memories in a module.
+ */
+static WASMMemoryInstance **
+memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
+ WASMModuleInstance *parent, uint32 heap_size,
+ uint32 max_memory_pages, char *error_buf,
+ uint32 error_buf_size)
+{
+ WASMImport *import;
+ uint32 mem_index = 0, i,
+ memory_count = module->import_memory_count + module->memory_count;
+ uint64 total_size;
+ WASMMemoryInstance **memories, *memory;
+
+ total_size = sizeof(WASMMemoryInstance *) * (uint64)memory_count;
+
+ if (!(memories = runtime_malloc(total_size, error_buf, error_buf_size))) {
+ return NULL;
+ }
+
+ memory = module_inst->global_table_data.memory_instances;
+
+ /* instantiate memories from import section */
+ import = module->import_memories;
+ for (i = 0; i < module->import_memory_count; i++, import++, memory++) {
+ uint32 num_bytes_per_page =
+ import->u.memory.mem_type.num_bytes_per_page;
+ uint32 init_page_count = import->u.memory.mem_type.init_page_count;
+ uint32 max_page_count = wasm_runtime_get_max_mem(
+ max_memory_pages, import->u.memory.mem_type.init_page_count,
+ import->u.memory.mem_type.max_page_count);
+ uint32 flags = import->u.memory.mem_type.flags;
+ uint32 actual_heap_size = heap_size;
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+ if (import->u.memory.import_module != NULL) {
+ WASMModuleInstance *module_inst_linked;
+
+ if (!(module_inst_linked = get_sub_module_inst(
+ module_inst, import->u.memory.import_module))) {
+ set_error_buf(error_buf, error_buf_size, "unknown memory");
+ memories_deinstantiate(module_inst, memories, memory_count);
+ return NULL;
+ }
+
+ if (!(memories[mem_index++] = wasm_lookup_memory(
+ module_inst_linked, import->u.memory.field_name))) {
+ set_error_buf(error_buf, error_buf_size, "unknown memory");
+ memories_deinstantiate(module_inst, memories, memory_count);
+ return NULL;
+ }
+ }
+ else
+#endif
+ {
+ if (!(memories[mem_index] = memory_instantiate(
+ module_inst, parent, memory, mem_index,
+ num_bytes_per_page, init_page_count, max_page_count,
+ actual_heap_size, flags, error_buf, error_buf_size))) {
+ memories_deinstantiate(module_inst, memories, memory_count);
+ return NULL;
+ }
+ mem_index++;
+ }
+ }
+
+ /* instantiate memories from memory section */
+ for (i = 0; i < module->memory_count; i++, memory++) {
+ uint32 max_page_count = wasm_runtime_get_max_mem(
+ max_memory_pages, module->memories[i].init_page_count,
+ module->memories[i].max_page_count);
+ if (!(memories[mem_index] = memory_instantiate(
+ module_inst, parent, memory, mem_index,
+ module->memories[i].num_bytes_per_page,
+ module->memories[i].init_page_count, max_page_count,
+ heap_size, module->memories[i].flags, error_buf,
+ error_buf_size))) {
+ memories_deinstantiate(module_inst, memories, memory_count);
+ return NULL;
+ }
+ mem_index++;
+ }
+
+ bh_assert(mem_index == memory_count);
+ (void)module_inst;
+ return memories;
+}
+
+/**
+ * Destroy table instances.
+ */
+static void
+tables_deinstantiate(WASMModuleInstance *module_inst)
+{
+ if (module_inst->tables) {
+ wasm_runtime_free(module_inst->tables);
+ }
+#if WASM_ENABLE_MULTI_MODULE != 0
+ if (module_inst->e->table_insts_linked) {
+ wasm_runtime_free(module_inst->e->table_insts_linked);
+ }
+#endif
+}
+
+/**
+ * Instantiate tables in a module.
+ */
+static WASMTableInstance **
+tables_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
+ WASMTableInstance *first_table, char *error_buf,
+ uint32 error_buf_size)
+{
+ WASMImport *import;
+ uint32 table_index = 0, i;
+ uint32 table_count = module->import_table_count + module->table_count;
+ uint64 total_size = (uint64)sizeof(WASMTableInstance *) * table_count;
+ WASMTableInstance **tables, *table = first_table;
+#if WASM_ENABLE_MULTI_MODULE != 0
+ uint64 total_size_of_tables_linked =
+ (uint64)sizeof(WASMTableInstance *) * module->import_table_count;
+ WASMTableInstance **table_linked = NULL;
+#endif
+
+ if (!(tables = runtime_malloc(total_size, error_buf, error_buf_size))) {
+ return NULL;
+ }
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+ if (module->import_table_count > 0
+ && !(module_inst->e->table_insts_linked = table_linked = runtime_malloc(
+ total_size_of_tables_linked, error_buf, error_buf_size))) {
+ goto fail;
+ }
+#endif
+
+ /* instantiate tables from import section */
+ import = module->import_tables;
+ for (i = 0; i < module->import_table_count; i++, import++) {
+ uint32 max_size_fixed = 0;
+#if WASM_ENABLE_MULTI_MODULE != 0
+ WASMTableInstance *table_inst_linked = NULL;
+ WASMModuleInstance *module_inst_linked = NULL;
+
+ if (import->u.table.import_module) {
+ if (!(module_inst_linked = get_sub_module_inst(
+ module_inst, import->u.table.import_module))) {
+ set_error_buf(error_buf, error_buf_size, "unknown table");
+ goto fail;
+ }
+
+ if (!(table_inst_linked = wasm_lookup_table(
+ module_inst_linked, import->u.table.field_name))) {
+ set_error_buf(error_buf, error_buf_size, "unknown table");
+ goto fail;
+ }
+
+ total_size = offsetof(WASMTableInstance, elems);
+ }
+ else
+#endif
+ {
+ /* in order to save memory, alloc resource as few as possible */
+ max_size_fixed = import->u.table.table_type.possible_grow
+ ? import->u.table.table_type.max_size
+ : import->u.table.table_type.init_size;
+
+ /* it is a built-in table, every module has its own */
+ total_size = offsetof(WASMTableInstance, elems);
+ /* store function indexes for non-gc, object pointers for gc */
+ total_size += (uint64)sizeof(table_elem_type_t) * max_size_fixed;
+ }
+
+ tables[table_index++] = table;
+
+#if WASM_ENABLE_GC == 0
+ /* Set all elements to -1 to mark them as uninitialized elements */
+ memset(table, -1, (uint32)total_size);
+#else
+ /* For GC, all elements have already been set to NULL_REF (0) as
+ uninitialized elements */
+#endif
+
+ table->is_table64 = import->u.table.table_type.flags & TABLE64_FLAG;
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+ *table_linked = table_inst_linked;
+ if (table_inst_linked != NULL) {
+ table->elem_type = table_inst_linked->elem_type;
+#if WASM_ENABLE_GC != 0
+ table->elem_ref_type = table_inst_linked->elem_ref_type;
+#endif
+ table->cur_size = table_inst_linked->cur_size;
+ table->max_size = table_inst_linked->max_size;
+ }
+ else
+#endif
+ {
+ table->elem_type = import->u.table.table_type.elem_type;
+#if WASM_ENABLE_GC != 0
+ table->elem_ref_type.elem_ref_type =
+ import->u.table.table_type.elem_ref_type;
+#endif
+ table->cur_size = import->u.table.table_type.init_size;
+ table->max_size = max_size_fixed;
+ }
+
+ table = (WASMTableInstance *)((uint8 *)table + (uint32)total_size);
+#if WASM_ENABLE_MULTI_MODULE != 0
+ table_linked++;
+#endif
+ }
+
+ /* instantiate tables from table section */
+ for (i = 0; i < module->table_count; i++) {
+ uint32 max_size_fixed = 0;
+
+ total_size = offsetof(WASMTableInstance, elems);
+#if WASM_ENABLE_MULTI_MODULE != 0
+ /* in case, a module which imports this table will grow it */
+ max_size_fixed = module->tables[i].table_type.max_size;
+#else
+ max_size_fixed = module->tables[i].table_type.possible_grow
+ ? module->tables[i].table_type.max_size
+ : module->tables[i].table_type.init_size;
+#endif
+#if WASM_ENABLE_GC == 0
+ /* Store function indexes */
+ total_size += sizeof(uintptr_t) * (uint64)max_size_fixed;
+#else
+ /* Store object pointers */
+ total_size += sizeof(uintptr_t) * (uint64)max_size_fixed;
+#endif
+
+ tables[table_index++] = table;
+
+#if WASM_ENABLE_GC == 0
+ /* Set all elements to -1 to mark them as uninitialized elements */
+ memset(table, -1, (uint32)total_size);
+#else
+ /* For GC, all elements have already been set to NULL_REF (0) as
+ uninitialized elements */
+#endif
+ table->is_table64 = module->tables[i].table_type.flags & TABLE64_FLAG;
+ table->elem_type = module->tables[i].table_type.elem_type;
+#if WASM_ENABLE_GC != 0
+ table->elem_ref_type.elem_ref_type =
+ module->tables[i].table_type.elem_ref_type;
+#endif
+ table->cur_size = module->tables[i].table_type.init_size;
+ table->max_size = max_size_fixed;
+
+ table = (WASMTableInstance *)((uint8 *)table + (uint32)total_size);
+ }
+
+ bh_assert(table_index == table_count);
+ (void)module_inst;
+ return tables;
+#if WASM_ENABLE_MULTI_MODULE != 0
+fail:
+ wasm_runtime_free(tables);
+ return NULL;
+#endif
+}
+
+/**
+ * Destroy function instances.
+ */
+static void
+functions_deinstantiate(WASMFunctionInstance *functions)
+{
+ if (functions) {
+ wasm_runtime_free(functions);
+ }
+}
+
+/**
+ * Instantiate functions in a module.
+ */
+static WASMFunctionInstance *
+functions_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
+ char *error_buf, uint32 error_buf_size)
+{
+ WASMImport *import;
+ uint32 i,
+ function_count = module->import_function_count + module->function_count;
+ uint64 total_size = sizeof(WASMFunctionInstance) * (uint64)function_count;
+ WASMFunctionInstance *functions, *function;
+
+ if (!(functions = runtime_malloc(total_size, error_buf, error_buf_size))) {
+ return NULL;
+ }
+
+ total_size = sizeof(void *) * (uint64)module->import_function_count;
+ if (total_size > 0
+ && !(module_inst->import_func_ptrs =
+ runtime_malloc(total_size, error_buf, error_buf_size))) {
+ wasm_runtime_free(functions);
+ return NULL;
+ }
+
+ /* instantiate functions from import section */
+ function = functions;
+ import = module->import_functions;
+ for (i = 0; i < module->import_function_count; i++, import++) {
+ function->is_import_func = true;
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+ if (import->u.function.import_module) {
+ function->import_module_inst = get_sub_module_inst(
+ module_inst, import->u.function.import_module);
+
+ if (function->import_module_inst) {
+ function->import_func_inst =
+ wasm_lookup_function(function->import_module_inst,
+ import->u.function.field_name);
+ }
+ }
+#endif /* WASM_ENABLE_MULTI_MODULE */
+ function->u.func_import = &import->u.function;
+ function->param_cell_num = import->u.function.func_type->param_cell_num;
+ function->ret_cell_num = import->u.function.func_type->ret_cell_num;
+ function->param_count =
+ (uint16)function->u.func_import->func_type->param_count;
+ function->param_types = function->u.func_import->func_type->types;
+ function->local_cell_num = 0;
+ function->local_count = 0;
+ function->local_types = NULL;
+
+ /* Copy the function pointer to current instance */
+ module_inst->import_func_ptrs[i] =
+ function->u.func_import->func_ptr_linked;
+
+ function++;
+ }
+
+ /* instantiate functions from function section */
+ for (i = 0; i < module->function_count; i++) {
+ function->is_import_func = false;
+ function->u.func = module->functions[i];
+
+ function->param_cell_num = function->u.func->param_cell_num;
+ function->ret_cell_num = function->u.func->ret_cell_num;
+ function->local_cell_num = function->u.func->local_cell_num;
+
+ function->param_count =
+ (uint16)function->u.func->func_type->param_count;
+ function->local_count = (uint16)function->u.func->local_count;
+ function->param_types = function->u.func->func_type->types;
+ function->local_types = function->u.func->local_types;
+
+ function->local_offsets = function->u.func->local_offsets;
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ function->const_cell_num = function->u.func->const_cell_num;
+#endif
+
+ function++;
+ }
+ bh_assert((uint32)(function - functions) == function_count);
+
+#if WASM_ENABLE_FAST_JIT != 0
+ module_inst->fast_jit_func_ptrs = module->fast_jit_func_ptrs;
+#endif
+
+ return functions;
+}
+
+#if WASM_ENABLE_TAGS != 0
+/**
+ * Destroy tags instances.
+ */
+static void
+tags_deinstantiate(WASMTagInstance *tags, void **import_tag_ptrs)
+{
+ if (tags) {
+ wasm_runtime_free(tags);
+ }
+ if (import_tag_ptrs) {
+ wasm_runtime_free(import_tag_ptrs);
+ }
+}
+
+/**
+ * Instantiate tags in a module.
+ */
+static WASMTagInstance *
+tags_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
+ char *error_buf, uint32 error_buf_size)
+{
+ WASMImport *import;
+ uint32 i, tag_count = module->import_tag_count + module->tag_count;
+ uint64 total_size = sizeof(WASMTagInstance) * (uint64)tag_count;
+ WASMTagInstance *tags, *tag;
+
+ if (!(tags = runtime_malloc(total_size, error_buf, error_buf_size))) {
+ return NULL;
+ }
+
+ total_size = sizeof(void *) * (uint64)module->import_tag_count;
+ if (total_size > 0
+ && !(module_inst->e->import_tag_ptrs =
+ runtime_malloc(total_size, error_buf, error_buf_size))) {
+ wasm_runtime_free(tags);
+ return NULL;
+ }
+
+ /* instantiate tags from import section */
+ tag = tags;
+ import = module->import_tags;
+ for (i = 0; i < module->import_tag_count; i++, import++) {
+ tag->is_import_tag = true;
+ tag->u.tag_import = &import->u.tag;
+ tag->type = import->u.tag.type;
+ tag->attribute = import->u.tag.attribute;
+#if WASM_ENABLE_MULTI_MODULE != 0
+ if (import->u.tag.import_module) {
+ if (!(tag->import_module_inst = get_sub_module_inst(
+ module_inst, import->u.tag.import_module))) {
+ set_error_buf(error_buf, error_buf_size, "unknown tag");
+ goto fail;
+ }
+
+ if (!(tag->import_tag_inst =
+ wasm_lookup_tag(tag->import_module_inst,
+ import->u.tag.field_name, NULL))) {
+ set_error_buf(error_buf, error_buf_size, "unknown tag");
+ goto fail;
+ }
+
+ /* Copy the imported tag to current instance */
+ module_inst->e->import_tag_ptrs[i] =
+ tag->u.tag_import->import_tag_linked;
+ }
+#endif
+ tag++;
+ }
+
+ /* instantiate tags from tag section */
+ for (i = 0; i < module->tag_count; i++) {
+ tag->is_import_tag = false;
+ tag->type = module->tags[i]->type;
+ tag->u.tag = module->tags[i];
+
+#if WASM_ENABLE_FAST_INTERP != 0
+ /* tag->const_cell_num = function->u.func->const_cell_num; */
+#endif
+ tag++;
+ }
+ bh_assert((uint32)(tag - tags) == tag_count);
+
+ return tags;
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+fail:
+ tags_deinstantiate(tags, module_inst->e->import_tag_ptrs);
+ /* clean up */
+ module_inst->e->import_tag_ptrs = NULL;
+ return NULL;
+#endif
+}
+#endif /* end of WASM_ENABLE_TAGS != 0 */
+
+/**
+ * Destroy global instances.
+ */
+static void
+globals_deinstantiate(WASMGlobalInstance *globals)
+{
+ if (globals)
+ wasm_runtime_free(globals);
+}
+
+static bool
+check_global_init_expr(const WASMModule *module, uint32 global_index,
+ char *error_buf, uint32 error_buf_size)
+{
+ if (global_index >= module->import_global_count + module->global_count) {
+ set_error_buf_v(error_buf, error_buf_size, "unknown global %d",
+ global_index);
+ return false;
+ }
+
+#if WASM_ENABLE_GC == 0
+ /**
+ * 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.
+ *
+ * And initializer expression cannot reference a mutable global.
+ */
+ if (global_index >= module->import_global_count
+ || (module->import_globals + global_index)->u.global.type.is_mutable) {
+ set_error_buf(error_buf, error_buf_size,
+ "constant expression required");
+ return false;
+ }
+#endif
+
+ return true;
+}
+
+#if WASM_ENABLE_GC != 0
+/* Instantiate struct global variable recursively */
+static WASMStructObjectRef
+instantiate_struct_global_recursive(WASMModule *module,
+ WASMModuleInstance *module_inst,
+ uint32 type_idx, uint8 flag,
+ WASMStructNewInitValues *init_values,
+ char *error_buf, uint32 error_buf_size)
+{
+ WASMRttType *rtt_type;
+ WASMStructObjectRef struct_obj;
+ WASMStructType *struct_type;
+
+ struct_type = (WASMStructType *)module->types[type_idx];
+
+ if (!(rtt_type = wasm_rtt_type_new((WASMType *)struct_type, type_idx,
+ module->rtt_types, module->type_count,
+ &module->rtt_type_lock))) {
+ set_error_buf(error_buf, error_buf_size, "create rtt object failed");
+ return NULL;
+ }
+
+ if (!(struct_obj = wasm_struct_obj_new_internal(
+ module_inst->e->common.gc_heap_handle, rtt_type))) {
+ set_error_buf(error_buf, error_buf_size, "create struct object failed");
+ return NULL;
+ }
+
+ if (flag == INIT_EXPR_TYPE_STRUCT_NEW) {
+ uint32 field_idx;
+ WASMRefTypeMap *ref_type_map = struct_type->ref_type_maps;
+
+ bh_assert(init_values->count == struct_type->field_count);
+
+ for (field_idx = 0; field_idx < init_values->count; field_idx++) {
+ uint8 field_type = struct_type->fields[field_idx].field_type;
+ WASMRefType *field_ref_type = NULL;
+ if (wasm_is_type_multi_byte_type(field_type)) {
+ field_ref_type = ref_type_map->ref_type;
+ }
+
+ if (wasm_reftype_is_subtype_of(field_type, field_ref_type,
+ REF_TYPE_STRUCTREF, NULL,
+ module->types, module->type_count)
+ || wasm_reftype_is_subtype_of(field_type, field_ref_type,
+ REF_TYPE_ARRAYREF, NULL,
+ module->types, module->type_count)
+ || wasm_reftype_is_subtype_of(
+ field_type, field_ref_type, REF_TYPE_FUNCREF, NULL,
+ module->types, module->type_count)) {
+ WASMType *wasm_type;
+ int32 heap_type =
+ ref_type_map->ref_type->ref_ht_common.heap_type;
+ WASMValue *wasm_value = &init_values->fields[field_idx];
+ WASMValue field_value = { 0 };
+
+ bh_assert(heap_type >= 0);
+ wasm_type = module->types[heap_type];
+
+ bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
+ || wasm_type->type_flag == WASM_TYPE_ARRAY
+ || wasm_type->type_flag == WASM_TYPE_FUNC);
+
+ if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
+ WASMStructNewInitValues *init_values1 =
+ (WASMStructNewInitValues *)wasm_value->data;
+ WASMStructObjectRef field =
+ instantiate_struct_global_recursive(
+ module, module_inst, heap_type,
+ init_values1 ? INIT_EXPR_TYPE_STRUCT_NEW
+ : INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT,
+ init_values1, error_buf, error_buf_size);
+ field_value.gc_obj = (WASMObjectRef)field;
+ wasm_struct_obj_set_field(struct_obj, field_idx,
+ &field_value);
+ }
+ else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
+ /* struct object's field is an array obj */
+ set_error_buf(error_buf, error_buf_size,
+ "array as a field in struct object is "
+ "not supported in constant init expr");
+ return NULL;
+ }
+ else if (wasm_type->type_flag == WASM_TYPE_FUNC) {
+ WASMFuncObjectRef func_obj = NULL;
+ /* UINT32_MAX indicates that it is a null reference */
+ if (wasm_value->u32 != UINT32_MAX) {
+ if (!(func_obj = wasm_create_func_obj(
+ module_inst, wasm_value->u32, false,
+ error_buf, error_buf_size))) {
+ return NULL;
+ }
+ }
+ field_value.gc_obj = (WASMObjectRef)func_obj;
+ wasm_struct_obj_set_field(struct_obj, field_idx,
+ &field_value);
+ }
+ }
+ else {
+ wasm_struct_obj_set_field(struct_obj, field_idx,
+ &init_values->fields[field_idx]);
+ }
+ if (wasm_is_type_multi_byte_type(field_type)) {
+ ref_type_map++;
+ }
+ }
+ }
+
+ return struct_obj;
+}
+
+static WASMArrayObjectRef
+instantiate_array_global_recursive(WASMModule *module,
+ WASMModuleInstance *module_inst,
+ uint32 type_idx, uint8 flag, uint32 len,
+ WASMValue *array_init_value,
+ WASMArrayNewInitValues *init_values,
+ char *error_buf, uint32 error_buf_size)
+{
+ WASMRttType *rtt_type;
+ WASMArrayObjectRef array_obj;
+ WASMArrayType *array_type;
+
+ array_type = (WASMArrayType *)module->types[type_idx];
+
+ if (!(rtt_type = wasm_rtt_type_new((WASMType *)array_type, type_idx,
+ module->rtt_types, module->type_count,
+ &module->rtt_type_lock))) {
+ set_error_buf(error_buf, error_buf_size, "create rtt object failed");
+ return NULL;
+ }
+
+ if (!(array_obj =
+ wasm_array_obj_new_internal(module_inst->e->common.gc_heap_handle,
+ rtt_type, len, array_init_value))) {
+ set_error_buf(error_buf, error_buf_size, "create array object failed");
+ return NULL;
+ }
+
+ if (flag == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
+ uint32 elem_idx;
+ uint8 elem_type = array_type->elem_type;
+ WASMRefType *elem_ref_type = array_type->elem_ref_type;
+
+ bh_assert(init_values);
+
+ if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
+ REF_TYPE_STRUCTREF, NULL, module->types,
+ module->type_count)
+ || wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
+ REF_TYPE_ARRAYREF, NULL,
+ module->types, module->type_count)
+ || wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
+ REF_TYPE_FUNCREF, NULL, module->types,
+ module->type_count)) {
+ /* TODO */
+ }
+
+ for (elem_idx = 0; elem_idx < len; elem_idx++) {
+ wasm_array_obj_set_elem(array_obj, elem_idx,
+ &init_values->elem_data[elem_idx]);
+ }
+ }
+
+ return array_obj;
+}
+#endif
+
+/**
+ * Instantiate globals in a module.
+ */
+static WASMGlobalInstance *
+globals_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
+ char *error_buf, uint32 error_buf_size)
+{
+ WASMImport *import;
+ uint32 global_data_offset = 0;
+ uint32 i, global_count = module->import_global_count + module->global_count;
+ uint64 total_size = sizeof(WASMGlobalInstance) * (uint64)global_count;
+ WASMGlobalInstance *globals, *global;
+
+ if (!(globals = runtime_malloc(total_size, error_buf, error_buf_size))) {
+ return NULL;
+ }
+
+ /* instantiate globals from import section */
+ global = globals;
+ import = module->import_globals;
+ for (i = 0; i < module->import_global_count; i++, import++) {
+ WASMGlobalImport *global_import = &import->u.global;
+ global->type = global_import->type.val_type;
+ global->is_mutable = global_import->type.is_mutable;
+#if WASM_ENABLE_GC != 0
+ global->ref_type = global_import->ref_type;
+#endif
+#if WASM_ENABLE_MULTI_MODULE != 0
+ if (global_import->import_module) {
+ if (!(global->import_module_inst = get_sub_module_inst(
+ module_inst, global_import->import_module))) {
+ set_error_buf(error_buf, error_buf_size, "unknown global");
+ goto fail;
+ }
+
+ if (!(global->import_global_inst = wasm_lookup_global(
+ global->import_module_inst, global_import->field_name))) {
+ set_error_buf(error_buf, error_buf_size, "unknown global");
+ goto fail;
+ }
+
+ /* The linked global instance has been initialized, we
+ just need to copy the value. */
+ bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
+ &(global_import->import_global_linked->init_expr),
+ sizeof(WASMValue));
+ }
+ else
+#endif
+ {
+ /* native globals share their initial_values in one module */
+ bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
+ &(global_import->global_data_linked),
+ sizeof(WASMValue));
+ }
+#if WASM_ENABLE_FAST_JIT != 0
+ bh_assert(global_data_offset == global_import->data_offset);
+#endif
+ global->data_offset = global_data_offset;
+ global_data_offset += wasm_value_type_size(global->type);
+
+ global++;
+ }
+
+ /* instantiate globals from global section */
+ for (i = 0; i < module->global_count; i++) {
+ InitializerExpression *init_expr = &(module->globals[i].init_expr);
+ uint8 flag = init_expr->init_expr_type;
+
+ global->type = module->globals[i].type.val_type;
+ global->is_mutable = module->globals[i].type.is_mutable;
+#if WASM_ENABLE_FAST_JIT != 0
+ bh_assert(global_data_offset == module->globals[i].data_offset);
+#endif
+ global->data_offset = global_data_offset;
+ global_data_offset += wasm_value_type_size(global->type);
+#if WASM_ENABLE_GC != 0
+ global->ref_type = module->globals[i].ref_type;
+#endif
+
+ switch (flag) {
+ case INIT_EXPR_TYPE_GET_GLOBAL:
+ {
+ if (!check_global_init_expr(module, init_expr->u.global_index,
+ error_buf, error_buf_size)) {
+ goto fail;
+ }
+
+ bh_memcpy_s(
+ &(global->initial_value), sizeof(WASMValue),
+ &(globals[init_expr->u.global_index].initial_value),
+ sizeof(globals[init_expr->u.global_index].initial_value));
+ break;
+ }
+#if WASM_ENABLE_GC != 0
+ case INIT_EXPR_TYPE_STRUCT_NEW:
+ case INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT:
+ {
+ WASMStructObjectRef struct_obj;
+ WASMStructNewInitValues *init_values = NULL;
+ uint32 type_idx;
+
+ if (flag == INIT_EXPR_TYPE_STRUCT_NEW) {
+ init_values = (WASMStructNewInitValues *)init_expr->u.data;
+ type_idx = init_values->type_idx;
+ }
+ else {
+ type_idx = init_expr->u.type_index;
+ }
+
+ struct_obj = instantiate_struct_global_recursive(
+ module, module_inst, type_idx, flag, init_values, error_buf,
+ error_buf_size);
+ if (!struct_obj) {
+ goto fail;
+ }
+
+ global->initial_value.gc_obj = (void *)struct_obj;
+ break;
+ }
+ case INIT_EXPR_TYPE_ARRAY_NEW:
+ case INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT:
+ case INIT_EXPR_TYPE_ARRAY_NEW_FIXED:
+ {
+ WASMArrayObjectRef array_obj;
+ WASMArrayNewInitValues *init_values = NULL;
+ WASMValue *array_init_value = NULL, empty_value = { 0 };
+ uint32 type_idx, len;
+
+ if (flag == INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT) {
+ type_idx = init_expr->u.array_new_default.type_index;
+ len = init_expr->u.array_new_default.length;
+ array_init_value = &empty_value;
+ }
+ else {
+ init_values = (WASMArrayNewInitValues *)init_expr->u.data;
+ type_idx = init_values->type_idx;
+ len = init_values->length;
+
+ if (flag == INIT_EXPR_TYPE_ARRAY_NEW) {
+ array_init_value = init_values->elem_data;
+ }
+ }
+
+ array_obj = instantiate_array_global_recursive(
+ module, module_inst, type_idx, flag, len, array_init_value,
+ init_values, error_buf, error_buf_size);
+
+ global->initial_value.gc_obj = (void *)array_obj;
+ break;
+ }
+ case INIT_EXPR_TYPE_I31_NEW:
+ {
+ global->initial_value.gc_obj =
+ (wasm_obj_t)wasm_i31_obj_new(init_expr->u.i32);
+ break;
+ }
+#endif /* end of WASM_ENABLE_GC != 0 */
+ default:
+ bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
+ &(init_expr->u), sizeof(init_expr->u));
+ break;
+ }
+
+ global++;
+ }
+
+ bh_assert((uint32)(global - globals) == global_count);
+ bh_assert(global_data_offset == module->global_data_size);
+ (void)module_inst;
+ return globals;
+fail:
+ wasm_runtime_free(globals);
+ return NULL;
+}
+
+/**
+ * Return export function count in module export section.
+ */
+static uint32
+get_export_count(const WASMModule *module, uint8 kind)
+{
+ WASMExport *export = module->exports;
+ uint32 count = 0, i;
+
+ for (i = 0; i < module->export_count; i++, export ++)
+ if (export->kind == kind)
+ count++;
+
+ return count;
+}
+
+/**
+ * Destroy export function instances.
+ */
+static void
+export_functions_deinstantiate(WASMExportFuncInstance *functions)
+{
+ if (functions)
+ wasm_runtime_free(functions);
+}
+
+static int
+cmp_export_func_inst(const void *a, const void *b)
+{
+ const WASMExportFuncInstance *export_func1 =
+ (const WASMExportFuncInstance *)a;
+ const WASMExportFuncInstance *export_func2 =
+ (const WASMExportFuncInstance *)b;
+
+ return strcmp(export_func1->name, export_func2->name);
+}
+
+/**
+ * Instantiate export functions in a module.
+ */
+static WASMExportFuncInstance *
+export_functions_instantiate(const WASMModule *module,
+ WASMModuleInstance *module_inst,
+ uint32 export_func_count, char *error_buf,
+ uint32 error_buf_size)
+{
+ WASMExportFuncInstance *export_funcs, *export_func;
+ WASMExport *export = module->exports;
+ uint32 i;
+ uint64 total_size =
+ sizeof(WASMExportFuncInstance) * (uint64)export_func_count;
+
+ if (!(export_func = export_funcs =
+ runtime_malloc(total_size, error_buf, error_buf_size))) {
+ return NULL;
+ }
+
+ for (i = 0; i < module->export_count; i++, export ++)
+ if (export->kind == EXPORT_KIND_FUNC) {
+ export_func->name = export->name;
+ export_func->function = &module_inst->e->functions[export->index];
+ export_func++;
+ }
+
+ bh_assert((uint32)(export_func - export_funcs) == export_func_count);
+
+ qsort(export_funcs, export_func_count, sizeof(WASMExportFuncInstance),
+ cmp_export_func_inst);
+ return export_funcs;
+}
+
+#if WASM_ENABLE_TAGS != 0
+/**
+ * Destroy export function instances.
+ */
+static void
+export_tags_deinstantiate(WASMExportTagInstance *tags)
+{
+ if (tags)
+ wasm_runtime_free(tags);
+}
+
+/**
+ * Instantiate export functions in a module.
+ */
+static WASMExportTagInstance *
+export_tags_instantiate(const WASMModule *module,
+ WASMModuleInstance *module_inst,
+ uint32 export_tag_count, char *error_buf,
+ uint32 error_buf_size)
+{
+ WASMExportTagInstance *export_tags, *export_tag;
+ WASMExport *export = module->exports;
+ uint32 i;
+ uint64 total_size =
+ sizeof(WASMExportTagInstance) * (uint64)export_tag_count;
+
+ if (!(export_tag = export_tags =
+ runtime_malloc(total_size, error_buf, error_buf_size))) {
+ return NULL;
+ }
+
+ for (i = 0; i < module->export_count; i++, export ++)
+ if (export->kind == EXPORT_KIND_TAG) {
+ export_tag->name = export->name;
+
+ bh_assert(module_inst->e->tags);
+
+ export_tag->tag = &module_inst->e->tags[export->index];
+ export_tag++;
+ }
+
+ bh_assert((uint32)(export_tag - export_tags) == export_tag_count);
+ return export_tags;
+}
+#endif /* end of WASM_ENABLE_TAGS != 0 */
+
+#if WASM_ENABLE_MULTI_MEMORY != 0
+static void
+export_memories_deinstantiate(WASMExportMemInstance *memories)
+{
+ if (memories)
+ wasm_runtime_free(memories);
+}
+
+static WASMExportMemInstance *
+export_memories_instantiate(const WASMModule *module,
+ WASMModuleInstance *module_inst,
+ uint32 export_mem_count, char *error_buf,
+ uint32 error_buf_size)
+{
+ WASMExportMemInstance *export_memories, *export_memory;
+ WASMExport *export = module->exports;
+ uint32 i;
+ uint64 total_size =
+ sizeof(WASMExportMemInstance) * (uint64)export_mem_count;
+
+ if (!(export_memory = export_memories =
+ runtime_malloc(total_size, error_buf, error_buf_size))) {
+ return NULL;
+ }
+
+ for (i = 0; i < module->export_count; i++, export ++)
+ if (export->kind == EXPORT_KIND_MEMORY) {
+ export_memory->name = export->name;
+ export_memory->memory = module_inst->memories[export->index];
+ export_memory++;
+ }
+
+ bh_assert((uint32)(export_memory - export_memories) == export_mem_count);
+ return export_memories;
+}
+#endif /* end of if WASM_ENABLE_MULTI_MEMORY != 0 */
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+static void
+export_globals_deinstantiate(WASMExportGlobInstance *globals)
+{
+ if (globals)
+ wasm_runtime_free(globals);
+}
+
+static WASMExportGlobInstance *
+export_globals_instantiate(const WASMModule *module,
+ WASMModuleInstance *module_inst,
+ uint32 export_glob_count, char *error_buf,
+ uint32 error_buf_size)
+{
+ WASMExportGlobInstance *export_globals, *export_global;
+ WASMExport *export = module->exports;
+ uint32 i;
+ uint64 total_size =
+ sizeof(WASMExportGlobInstance) * (uint64)export_glob_count;
+
+ if (!(export_global = export_globals =
+ runtime_malloc(total_size, error_buf, error_buf_size))) {
+ return NULL;
+ }
+
+ for (i = 0; i < module->export_count; i++, export ++)
+ if (export->kind == EXPORT_KIND_GLOBAL) {
+ export_global->name = export->name;
+ export_global->global = &module_inst->e->globals[export->index];
+ export_global++;
+ }
+
+ bh_assert((uint32)(export_global - export_globals) == export_glob_count);
+ return export_globals;
+}
+
+#endif /* end of if WASM_ENABLE_MULTI_MODULE != 0 */
+
+static WASMFunctionInstance *
+lookup_post_instantiate_func(WASMModuleInstance *module_inst,
+ const char *func_name)
+{
+ WASMFunctionInstance *func;
+ WASMFuncType *func_type;
+
+ if (!(func = wasm_lookup_function(module_inst, func_name)))
+ /* Not found */
+ return NULL;
+
+ func_type = func->u.func->func_type;
+ if (!(func_type->param_count == 0 && func_type->result_count == 0))
+ /* Not a valid function type, ignore it */
+ return NULL;
+
+ return func;
+}
+
+static bool
+execute_post_instantiate_functions(WASMModuleInstance *module_inst,
+ bool is_sub_inst, WASMExecEnv *exec_env_main)
+{
+ WASMFunctionInstance *start_func = module_inst->e->start_function;
+ WASMFunctionInstance *initialize_func = NULL;
+ WASMFunctionInstance *post_inst_func = NULL;
+ WASMFunctionInstance *call_ctors_func = NULL;
+#if WASM_ENABLE_LIBC_WASI != 0
+ WASMModule *module = module_inst->module;
+#endif
+ WASMModuleInstanceCommon *module_inst_main = NULL;
+#ifdef OS_ENABLE_HW_BOUND_CHECK
+ WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
+#endif
+ WASMExecEnv *exec_env = NULL, *exec_env_created = NULL;
+ bool ret = false;
+
+#if WASM_ENABLE_LIBC_WASI != 0
+ /*
+ * WASI reactor instances may assume that _initialize will be called by
+ * the environment at most once, and that none of their other exports
+ * are accessed before that call.
+ */
+ if (!is_sub_inst && module->import_wasi_api) {
+ initialize_func =
+ lookup_post_instantiate_func(module_inst, "_initialize");
+ }
+#endif
+
+ /* Execute possible "__post_instantiate" function if wasm app is
+ compiled by emsdk's early version */
+ if (!is_sub_inst) {
+ post_inst_func =
+ lookup_post_instantiate_func(module_inst, "__post_instantiate");
+ }
+
+#if WASM_ENABLE_BULK_MEMORY != 0
+ /* Only execute the memory init function for main instance since
+ the data segments will be dropped once initialized */
+ if (!is_sub_inst
+#if WASM_ENABLE_LIBC_WASI != 0
+ && !module->import_wasi_api
+#endif
+ ) {
+ call_ctors_func =
+ lookup_post_instantiate_func(module_inst, "__wasm_call_ctors");
+ }
+#endif
+
+ if (!start_func && !initialize_func && !post_inst_func
+ && !call_ctors_func) {
+ /* No post instantiation functions to call */
+ return true;
+ }
+
+ if (is_sub_inst) {
+ bh_assert(exec_env_main);
+#ifdef OS_ENABLE_HW_BOUND_CHECK
+ /* May come from pthread_create_wrapper, thread_spawn_wrapper and
+ wasm_cluster_spawn_exec_env. If it comes from the former two,
+ the exec_env_tls must be not NULL and equal to exec_env_main,
+ else if it comes from the last one, it may be NULL. */
+ if (exec_env_tls)
+ bh_assert(exec_env_tls == exec_env_main);
+#endif
+ exec_env = exec_env_main;
+
+ /* Temporarily replace parent exec_env's module inst to current
+ module inst to avoid checking failure when calling the
+ wasm functions, and ensure that the exec_env's module inst
+ is the correct one. */
+ module_inst_main = exec_env_main->module_inst;
+ wasm_exec_env_set_module_inst(exec_env,
+ (WASMModuleInstanceCommon *)module_inst);
+ }
+ else {
+ /* Try using the existing exec_env */
+#ifdef OS_ENABLE_HW_BOUND_CHECK
+ exec_env = exec_env_tls;
+#endif
+#if WASM_ENABLE_THREAD_MGR != 0
+ if (!exec_env)
+ exec_env = wasm_clusters_search_exec_env(
+ (WASMModuleInstanceCommon *)module_inst);
+#endif
+ if (!exec_env) {
+ if (!(exec_env = exec_env_created = wasm_exec_env_create(
+ (WASMModuleInstanceCommon *)module_inst,
+ module_inst->default_wasm_stack_size))) {
+ wasm_set_exception(module_inst, "allocate memory failed");
+ return false;
+ }
+ }
+ else {
+ /* Temporarily replace exec_env's module inst with current
+ module inst to ensure that the exec_env's module inst
+ is the correct one. */
+ module_inst_main = exec_env->module_inst;
+ wasm_exec_env_set_module_inst(
+ exec_env, (WASMModuleInstanceCommon *)module_inst);
+ }
+ }
+
+ /* Execute start function for both main instance and sub instance */
+ if (start_func && !wasm_call_function(exec_env, start_func, 0, NULL)) {
+ goto fail;
+ }
+
+#if WASM_ENABLE_LIBC_WASI != 0
+ if (initialize_func
+ && !wasm_call_function(exec_env, initialize_func, 0, NULL)) {
+ goto fail;
+ }
+#else
+ (void)initialize_func;
+#endif
+
+ if (post_inst_func
+ && !wasm_call_function(exec_env, post_inst_func, 0, NULL)) {
+ goto fail;
+ }
+
+ if (call_ctors_func
+ && !wasm_call_function(exec_env, call_ctors_func, 0, NULL)) {
+ goto fail;
+ }
+
+ ret = true;
+
+fail:
+ if (is_sub_inst) {
+ /* Restore the parent exec_env's module inst */
+ wasm_exec_env_restore_module_inst(exec_env_main, module_inst_main);
+ }
+ else {
+ if (module_inst_main)
+ /* Restore the existing exec_env's module inst */
+ wasm_exec_env_restore_module_inst(exec_env, module_inst_main);
+ if (exec_env_created)
+ wasm_exec_env_destroy(exec_env_created);
+ }
+
+ return ret;
+}
+
+static bool
+execute_malloc_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
+ WASMFunctionInstance *malloc_func,
+ WASMFunctionInstance *retain_func, uint64 size,
+ uint64 *p_result)
+{
+#ifdef OS_ENABLE_HW_BOUND_CHECK
+ WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
+#endif
+ WASMExecEnv *exec_env_created = NULL;
+ WASMModuleInstanceCommon *module_inst_old = NULL;
+ union {
+ uint32 u32[3];
+ uint64 u64;
+ } argv;
+ uint32 argc;
+ bool ret;
+#if WASM_ENABLE_MEMORY64 != 0
+ bool is_memory64 = module_inst->memories[0]->is_memory64;
+ if (is_memory64) {
+ argc = 2;
+ PUT_I64_TO_ADDR(&argv.u64, size);
+ }
+ else
+#endif
+ {
+ argc = 1;
+ argv.u32[0] = (uint32)size;
+ }
+
+ /* if __retain is exported, then this module is compiled by
+ assemblyscript, the memory should be managed by as's runtime,
+ in this case we need to call the retain function after malloc
+ the memory */
+ if (retain_func) {
+ /* the malloc function from assemblyscript is:
+ function __new(size: usize, id: u32)
+ id = 0 means this is an ArrayBuffer object */
+ argv.u32[argc] = 0;
+ argc++;
+ }
+
+ if (exec_env) {
+#ifdef OS_ENABLE_HW_BOUND_CHECK
+ if (exec_env_tls) {
+ bh_assert(exec_env_tls == exec_env);
+ }
+#endif
+ bh_assert(exec_env->module_inst
+ == (WASMModuleInstanceCommon *)module_inst);
+ }
+ else {
+ /* Try using the existing exec_env */
+#ifdef OS_ENABLE_HW_BOUND_CHECK
+ exec_env = exec_env_tls;
+#endif
+#if WASM_ENABLE_THREAD_MGR != 0
+ if (!exec_env)
+ exec_env = wasm_clusters_search_exec_env(
+ (WASMModuleInstanceCommon *)module_inst);
+#endif
+ if (!exec_env) {
+ if (!(exec_env = exec_env_created = wasm_exec_env_create(
+ (WASMModuleInstanceCommon *)module_inst,
+ module_inst->default_wasm_stack_size))) {
+ wasm_set_exception(module_inst, "allocate memory failed");
+ return false;
+ }
+ }
+ else {
+ /* Temporarily replace exec_env's module inst with current
+ module inst to ensure that the exec_env's module inst
+ is the correct one. */
+ module_inst_old = exec_env->module_inst;
+ wasm_exec_env_set_module_inst(
+ exec_env, (WASMModuleInstanceCommon *)module_inst);
+ }
+ }
+
+ ret = wasm_call_function(exec_env, malloc_func, argc, argv.u32);
+
+ if (retain_func && ret)
+ ret = wasm_call_function(exec_env, retain_func, 1, argv.u32);
+
+ if (module_inst_old)
+ /* Restore the existing exec_env's module inst */
+ wasm_exec_env_restore_module_inst(exec_env, module_inst_old);
+
+ if (exec_env_created)
+ wasm_exec_env_destroy(exec_env_created);
+
+ if (ret) {
+#if WASM_ENABLE_MEMORY64 != 0
+ if (is_memory64)
+ *p_result = argv.u64;
+ else
+#endif
+ {
+ *p_result = argv.u32[0];
+ }
+ }
+ return ret;
+}
+
+static bool
+execute_free_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
+ WASMFunctionInstance *free_func, uint64 offset)
+{
+#ifdef OS_ENABLE_HW_BOUND_CHECK
+ WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
+#endif
+ WASMExecEnv *exec_env_created = NULL;
+ WASMModuleInstanceCommon *module_inst_old = NULL;
+ union {
+ uint32 u32[2];
+ uint64 u64;
+ } argv;
+ uint32 argc;
+ bool ret;
+
+#if WASM_ENABLE_MEMORY64 != 0
+ if (module_inst->memories[0]->is_memory64) {
+ PUT_I64_TO_ADDR(&argv.u64, offset);
+ argc = 2;
+ }
+ else
+#endif
+ {
+ argv.u32[0] = (uint32)offset;
+ argc = 1;
+ }
+
+ if (exec_env) {
+#ifdef OS_ENABLE_HW_BOUND_CHECK
+ if (exec_env_tls) {
+ bh_assert(exec_env_tls == exec_env);
+ }
+#endif
+ bh_assert(exec_env->module_inst
+ == (WASMModuleInstanceCommon *)module_inst);
+ }
+ else {
+ /* Try using the existing exec_env */
+#ifdef OS_ENABLE_HW_BOUND_CHECK
+ exec_env = exec_env_tls;
+#endif
+#if WASM_ENABLE_THREAD_MGR != 0
+ if (!exec_env)
+ exec_env = wasm_clusters_search_exec_env(
+ (WASMModuleInstanceCommon *)module_inst);
+#endif
+ if (!exec_env) {
+ if (!(exec_env = exec_env_created = wasm_exec_env_create(
+ (WASMModuleInstanceCommon *)module_inst,
+ module_inst->default_wasm_stack_size))) {
+ wasm_set_exception(module_inst, "allocate memory failed");
+ return false;
+ }
+ }
+ else {
+ /* Temporarily replace exec_env's module inst with current
+ module inst to ensure that the exec_env's module inst
+ is the correct one. */
+ module_inst_old = exec_env->module_inst;
+ wasm_exec_env_set_module_inst(
+ exec_env, (WASMModuleInstanceCommon *)module_inst);
+ }
+ }
+
+ ret = wasm_call_function(exec_env, free_func, argc, argv.u32);
+
+ if (module_inst_old)
+ /* Restore the existing exec_env's module inst */
+ wasm_exec_env_restore_module_inst(exec_env, module_inst_old);
+
+ if (exec_env_created)
+ wasm_exec_env_destroy(exec_env_created);
+
+ return ret;
+}
+
+static bool
+check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf,
+ uint32 error_buf_size)
+{
+ WASMModule *module = module_inst->module;
+ uint32 i;
+
+ for (i = 0; i < module->import_function_count; i++) {
+ WASMFunctionImport *func =
+ &((module->import_functions + i)->u.function);
+ if (!func->func_ptr_linked
+#if WASM_ENABLE_MULTI_MODULE != 0
+ && !func->import_func_linked
+#endif
+ ) {
+ LOG_WARNING("warning: failed to link import function (%s, %s)",
+ func->module_name, func->field_name);
+ }
+ }
+
+ for (i = 0; i < module->import_global_count; i++) {
+ WASMGlobalImport *global = &((module->import_globals + i)->u.global);
+
+ if (!global->is_linked) {
+#if WASM_ENABLE_SPEC_TEST != 0
+ set_error_buf(error_buf, error_buf_size,
+ "unknown import or incompatible import type");
+ return false;
+#else
+ set_error_buf_v(error_buf, error_buf_size,
+ "failed to link import global (%s, %s)",
+ global->module_name, global->field_name);
+ return false;
+#endif /* WASM_ENABLE_SPEC_TEST != 0 */
+ }
+ }
+
+ for (i = 0; i < module->import_table_count; i++) {
+ WASMTableImport *table = &((module->import_tables + i)->u.table);
+
+ if (!wasm_runtime_is_built_in_module(table->module_name)
+#if WASM_ENABLE_MULTI_MODULE != 0
+ && !table->import_table_linked
+#endif
+ ) {
+ set_error_buf_v(error_buf, error_buf_size,
+ "failed to link import table (%s, %s)",
+ table->module_name, table->field_name);
+ return false;
+ }
+ }
+
+ for (i = 0; i < module->import_memory_count; i++) {
+ WASMMemoryImport *memory = &((module->import_memories + i)->u.memory);
+
+ if (!wasm_runtime_is_built_in_module(memory->module_name)
+#if WASM_ENABLE_MULTI_MODULE != 0
+ && !memory->import_memory_linked
+#endif
+ ) {
+ set_error_buf_v(error_buf, error_buf_size,
+ "failed to link import memory (%s, %s)",
+ memory->module_name, memory->field_name);
+ return false;
+ }
+ }
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+#if WASM_ENABLE_TAGS != 0
+ for (i = 0; i < module->import_tag_count; i++) {
+ WASMTagImport *tag = &((module->import_tags + i)->u.tag);
+
+ if (!tag->import_tag_linked) {
+ set_error_buf_v(error_buf, error_buf_size,
+ "failed to link import tag (%s, %s)",
+ tag->module_name, tag->field_name);
+ return false;
+ }
+ }
+#endif /* WASM_ENABLE_TAGS != 0 */
+#endif
+
+ return true;
+}
+
+#if WASM_ENABLE_JIT != 0
+static bool
+init_func_ptrs(WASMModuleInstance *module_inst, WASMModule *module,
+ char *error_buf, uint32 error_buf_size)
+{
+ uint32 i;
+ void **func_ptrs;
+ uint64 total_size = (uint64)sizeof(void *) * module_inst->e->function_count;
+
+ /* Allocate memory */
+ if (!(func_ptrs = module_inst->func_ptrs =
+ runtime_malloc(total_size, error_buf, error_buf_size))) {
+ return false;
+ }
+
+ /* Set import function pointers */
+ for (i = 0; i < module->import_function_count; i++, func_ptrs++) {
+ WASMFunctionImport *import_func =
+ &module->import_functions[i].u.function;
+ /* TODO: handle multi module */
+ *func_ptrs = import_func->func_ptr_linked;
+ }
+
+ /* The defined function pointers will be set in
+ wasm_runtime_set_running_mode, no need to set them here */
+ return true;
+}
+#endif /* end of WASM_ENABLE_JIT != 0 */
+
+#if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
+static uint32
+get_smallest_type_idx(WASMModule *module, WASMFuncType *func_type)
+{
+ uint32 i;
+
+ for (i = 0; i < module->type_count; i++) {
+ if (func_type == (WASMFuncType *)module->types[i])
+ return i;
+ }
+
+ bh_assert(0);
+ return -1;
+}
+
+static bool
+init_func_type_indexes(WASMModuleInstance *module_inst, char *error_buf,
+ uint32 error_buf_size)
+{
+ uint32 i;
+ uint64 total_size = (uint64)sizeof(uint32) * module_inst->e->function_count;
+
+ /* Allocate memory */
+ if (!(module_inst->func_type_indexes =
+ runtime_malloc(total_size, error_buf, error_buf_size))) {
+ return false;
+ }
+
+ for (i = 0; i < module_inst->e->function_count; i++) {
+ WASMFunctionInstance *func_inst = module_inst->e->functions + i;
+ WASMFuncType *func_type = func_inst->is_import_func
+ ? func_inst->u.func_import->func_type
+ : func_inst->u.func->func_type;
+ module_inst->func_type_indexes[i] =
+ get_smallest_type_idx(module_inst->module, func_type);
+ }
+
+ return true;
+}
+#endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
+
+#if WASM_ENABLE_GC != 0
+void *
+wasm_create_func_obj(WASMModuleInstance *module_inst, uint32 func_idx,
+ bool throw_exce, char *error_buf, uint32 error_buf_size)
+{
+ WASMModule *module = module_inst->module;
+ WASMRttTypeRef rtt_type;
+ WASMFuncObjectRef func_obj;
+ WASMFuncType *func_type;
+ uint32 type_idx;
+
+ if (throw_exce) {
+ error_buf = module_inst->cur_exception;
+ error_buf_size = sizeof(module_inst->cur_exception);
+ }
+
+ if (func_idx >= module->import_function_count + module->function_count) {
+ set_error_buf_v(error_buf, error_buf_size, "unknown function %d",
+ func_idx);
+ return NULL;
+ }
+
+ if (func_idx < module->import_function_count) {
+ func_type = module->import_functions[func_idx].u.function.func_type;
+ type_idx = module->import_functions[func_idx].u.function.type_idx;
+ }
+ else {
+ func_type = module->functions[func_idx - module->import_function_count]
+ ->func_type;
+ type_idx = module->functions[func_idx - module->import_function_count]
+ ->type_idx;
+ }
+
+ if (!(rtt_type = wasm_rtt_type_new((WASMType *)func_type, type_idx,
+ module->rtt_types, module->type_count,
+ &module->rtt_type_lock))) {
+ set_error_buf(error_buf, error_buf_size, "create rtt object failed");
+ return NULL;
+ }
+
+ if (!(func_obj = wasm_func_obj_new_internal(
+ module_inst->e->common.gc_heap_handle, rtt_type, func_idx))) {
+ set_error_buf(error_buf, error_buf_size, "create func object failed");
+ return NULL;
+ }
+
+ return func_obj;
+}
+
+static bool
+wasm_global_traverse_gc_rootset(WASMModuleInstance *module_inst, void *heap)
+{
+ WASMGlobalInstance *global = module_inst->e->globals;
+ WASMGlobalInstance *global_end = global + module_inst->e->global_count;
+ uint8 *global_data = module_inst->global_data;
+ WASMObjectRef gc_obj;
+
+ while (global < global_end) {
+ if (wasm_is_type_reftype(global->type)) {
+ gc_obj = GET_REF_FROM_ADDR(
+ (uint32 *)(global_data + global->data_offset));
+ if (wasm_obj_is_created_from_heap(gc_obj)) {
+ if (0 != mem_allocator_add_root((mem_allocator_t)heap, gc_obj))
+ return false;
+ }
+ }
+ global++;
+ }
+ return true;
+}
+
+static bool
+wasm_table_traverse_gc_rootset(WASMModuleInstance *module_inst, void *heap)
+{
+ WASMTableInstance **tables = module_inst->tables, *table;
+ uint32 table_count = module_inst->table_count, i, j;
+ WASMObjectRef gc_obj, *table_elems;
+
+ for (i = 0; i < table_count; i++) {
+ table = tables[i];
+ table_elems = (WASMObjectRef *)table->elems;
+ for (j = 0; j < table->cur_size; j++) {
+ gc_obj = table_elems[j];
+ if (wasm_obj_is_created_from_heap(gc_obj)) {
+ if (0 != mem_allocator_add_root((mem_allocator_t)heap, gc_obj))
+ return false;
+ }
+ }
+ }
+
+ return true;
+}
+
+static bool
+local_object_refs_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap)
+{
+ WASMLocalObjectRef *r;
+ WASMObjectRef gc_obj;
+
+ for (r = exec_env->cur_local_object_ref; r; r = r->prev) {
+ gc_obj = r->val;
+ if (wasm_obj_is_created_from_heap(gc_obj)) {
+ if (0 != mem_allocator_add_root((mem_allocator_t)heap, gc_obj))
+ return false;
+ }
+ }
+ return true;
+}
+
+bool
+wasm_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap)
+{
+ WASMModuleInstance *module_inst =
+ (WASMModuleInstance *)exec_env->module_inst;
+ bool ret;
+
+ ret = wasm_global_traverse_gc_rootset(module_inst, heap);
+ if (!ret)
+ return ret;
+
+ ret = wasm_table_traverse_gc_rootset(module_inst, heap);
+ if (!ret)
+ return ret;
+
+ ret = local_object_refs_traverse_gc_rootset(exec_env, heap);
+ if (!ret)
+ return ret;
+
+ return wasm_interp_traverse_gc_rootset(exec_env, heap);
+}
+#endif /* end of WASM_ENABLE_GC != 0 */
+
+static bool
+set_running_mode(WASMModuleInstance *module_inst, RunningMode running_mode,
+ bool first_time_set)
+{
+ WASMModule *module = module_inst->module;
+
+ if (running_mode == Mode_Default) {
+#if WASM_ENABLE_FAST_JIT == 0 && WASM_ENABLE_JIT == 0
+ running_mode = Mode_Interp;
+#elif WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT == 0
+ running_mode = Mode_Fast_JIT;
+#elif WASM_ENABLE_FAST_JIT == 0 && WASM_ENABLE_JIT != 0
+ running_mode = Mode_LLVM_JIT;
+#else /* WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 */
+#if WASM_ENABLE_LAZY_JIT == 0
+ running_mode = Mode_LLVM_JIT;
+#else
+ running_mode = Mode_Multi_Tier_JIT;
+#endif
+#endif
+ }
+
+ if (!wasm_runtime_is_running_mode_supported(running_mode))
+ return false;
+
+#if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
+ && WASM_ENABLE_LAZY_JIT != 0) /* No possible multi-tier JIT */
+ (void)first_time_set;
+ module_inst->e->running_mode = running_mode;
+
+ if (running_mode == Mode_Interp) {
+ /* Do nothing for Mode_Interp */
+ }
+ else if (running_mode == Mode_Fast_JIT) {
+ /* Do nothing for Mode_Fast_JIT since
+ module_inst->fast_jit_func_ptrs is same as
+ module->fast_jit_func_ptrs */
+ }
+#if WASM_ENABLE_JIT != 0
+ else if (running_mode == Mode_LLVM_JIT) {
+ /* Set defined function pointers */
+ bh_memcpy_s(module_inst->func_ptrs + module->import_function_count,
+ sizeof(void *) * module->function_count, module->func_ptrs,
+ sizeof(void *) * module->function_count);
+ }
+#endif
+ else {
+ bh_assert(0);
+ }
+#else /* Possible multi-tier JIT */
+ os_mutex_lock(&module->instance_list_lock);
+
+ module_inst->e->running_mode = running_mode;
+
+ if (running_mode == Mode_Interp) {
+ /* Do nothing for Mode_Interp */
+ }
+#if WASM_ENABLE_FAST_JIT != 0
+ else if (running_mode == Mode_Fast_JIT) {
+ JitGlobals *jit_globals = jit_compiler_get_jit_globals();
+ uint32 i;
+
+ /* Allocate memory for fast_jit_func_ptrs if needed */
+ if (!module_inst->fast_jit_func_ptrs
+ || module_inst->fast_jit_func_ptrs == module->fast_jit_func_ptrs) {
+ uint64 total_size = (uint64)sizeof(void *) * module->function_count;
+ if (!(module_inst->fast_jit_func_ptrs =
+ runtime_malloc(total_size, NULL, 0))) {
+ os_mutex_unlock(&module->instance_list_lock);
+ return false;
+ }
+ }
+
+ for (i = 0; i < module->function_count; i++) {
+ if (module->functions[i]->fast_jit_jitted_code) {
+ /* current fast jit function has been compiled */
+ module_inst->fast_jit_func_ptrs[i] =
+ module->functions[i]->fast_jit_jitted_code;
+ }
+ else {
+ module_inst->fast_jit_func_ptrs[i] =
+ jit_globals->compile_fast_jit_and_then_call;
+ }
+ }
+ }
+#endif
+#if WASM_ENABLE_JIT != 0
+ else if (running_mode == Mode_LLVM_JIT) {
+ void **llvm_jit_func_ptrs;
+ uint32 i;
+
+ /* Notify backend threads to start llvm jit compilation */
+ module->enable_llvm_jit_compilation = true;
+
+ /* Wait until llvm jit finishes initialization */
+ os_mutex_lock(&module->tierup_wait_lock);
+ while (!module->llvm_jit_inited) {
+ os_cond_reltimedwait(&module->tierup_wait_cond,
+ &module->tierup_wait_lock, 10000);
+ if (module->orcjit_stop_compiling) {
+ /* init_llvm_jit_functions_stage2 failed */
+ os_mutex_unlock(&module->tierup_wait_lock);
+ os_mutex_unlock(&module->instance_list_lock);
+ return false;
+ }
+ }
+ os_mutex_unlock(&module->tierup_wait_lock);
+
+ llvm_jit_func_ptrs =
+ module_inst->func_ptrs + module->import_function_count;
+ for (i = 0; i < module->function_count; i++) {
+ llvm_jit_func_ptrs[i] = module->functions[i]->llvm_jit_func_ptr;
+ }
+ }
+#endif
+ else if (running_mode == Mode_Multi_Tier_JIT) {
+ /* Notify backend threads to start llvm jit compilation */
+ module->enable_llvm_jit_compilation = true;
+
+ /* Free fast_jit_func_ptrs if it is allocated before */
+ if (module_inst->fast_jit_func_ptrs
+ && module_inst->fast_jit_func_ptrs != module->fast_jit_func_ptrs) {
+ wasm_runtime_free(module_inst->fast_jit_func_ptrs);
+ }
+ module_inst->fast_jit_func_ptrs = module->fast_jit_func_ptrs;
+
+ /* Copy all llvm jit func ptrs from the module */
+ bh_memcpy_s(module_inst->func_ptrs + module->import_function_count,
+ sizeof(void *) * module->function_count, module->func_ptrs,
+ sizeof(void *) * module->function_count);
+ }
+ else {
+ bh_assert(0);
+ }
+
+ /* Add module instance into module's instance list if not added */
+ if (first_time_set) {
+ bool found = false;
+ WASMModuleInstance *node = module->instance_list;
+
+ while (node) {
+ if (node == module_inst) {
+ found = true;
+ break;
+ }
+ node = node->e->next;
+ }
+
+ if (!found) {
+ module_inst->e->next = module->instance_list;
+ module->instance_list = module_inst;
+ }
+ }
+
+ os_mutex_unlock(&module->instance_list_lock);
+#endif /* end of !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
+ && WASM_ENABLE_LAZY_JIT != 0) */
+
+ (void)module;
+ return true;
+}
+
+bool
+wasm_set_running_mode(WASMModuleInstance *module_inst, RunningMode running_mode)
+{
+ return set_running_mode(module_inst, running_mode, false);
+}
+
+/**
+ * Instantiate module
+ */
+WASMModuleInstance *
+wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
+ WASMExecEnv *exec_env_main, uint32 stack_size,
+ uint32 heap_size, uint32 max_memory_pages, char *error_buf,
+ uint32 error_buf_size)
+{
+ WASMModuleInstance *module_inst;
+ WASMGlobalInstance *globals = NULL, *global;
+ WASMTableInstance *first_table;
+ uint32 global_count, i;
+ uint32 length, extra_info_offset;
+ mem_offset_t base_offset;
+ uint32 module_inst_struct_size =
+ offsetof(WASMModuleInstance, global_table_data.bytes);
+ uint64 module_inst_mem_inst_size;
+ uint64 total_size, table_size = 0;
+ uint8 *global_data, *global_data_end;
+#if WASM_ENABLE_MULTI_MODULE != 0
+ bool ret = false;
+#endif
+ const bool is_sub_inst = parent != NULL;
+
+ if (!module)
+ return NULL;
+
+ /* Check the heap size */
+ heap_size = align_uint(heap_size, 8);
+ if (heap_size > APP_HEAP_SIZE_MAX)
+ heap_size = APP_HEAP_SIZE_MAX;
+
+ module_inst_mem_inst_size =
+ sizeof(WASMMemoryInstance)
+ * ((uint64)module->import_memory_count + module->memory_count);
+
+#if WASM_ENABLE_JIT != 0
+ /* If the module doesn't have memory, reserve one mem_info space
+ with empty content to align with llvm jit compiler */
+ if (module_inst_mem_inst_size == 0)
+ module_inst_mem_inst_size = (uint64)sizeof(WASMMemoryInstance);
+#endif
+
+ /* Size of module inst, memory instances and global data */
+ total_size = (uint64)module_inst_struct_size + module_inst_mem_inst_size
+ + module->global_data_size;
+
+ /* Calculate the size of table data */
+ for (i = 0; i < module->import_table_count; i++) {
+ WASMTableImport *import_table = &module->import_tables[i].u.table;
+ table_size += offsetof(WASMTableInstance, elems);
+#if WASM_ENABLE_MULTI_MODULE != 0
+ table_size += (uint64)sizeof(table_elem_type_t)
+ * import_table->table_type.max_size;
+#else
+ table_size += (uint64)sizeof(table_elem_type_t)
+ * (import_table->table_type.possible_grow
+ ? import_table->table_type.max_size
+ : import_table->table_type.init_size);
+#endif
+ }
+ for (i = 0; i < module->table_count; i++) {
+ WASMTable *table = module->tables + i;
+ table_size += offsetof(WASMTableInstance, elems);
+#if WASM_ENABLE_MULTI_MODULE != 0
+ table_size +=
+ (uint64)sizeof(table_elem_type_t) * table->table_type.max_size;
+#else
+ table_size +=
+ (uint64)sizeof(table_elem_type_t)
+ * (table->table_type.possible_grow ? table->table_type.max_size
+ : table->table_type.init_size);
+#endif
+ }
+ total_size += table_size;
+
+ /* The offset of WASMModuleInstanceExtra, make it 8-byte aligned */
+ total_size = (total_size + 7LL) & ~7LL;
+ extra_info_offset = (uint32)total_size;
+ total_size += sizeof(WASMModuleInstanceExtra);
+
+ /* Allocate the memory for module instance with memory instances,
+ global data, table data appended at the end */
+ if (!(module_inst =
+ runtime_malloc(total_size, error_buf, error_buf_size))) {
+ return NULL;
+ }
+
+ module_inst->module_type = Wasm_Module_Bytecode;
+ module_inst->module = module;
+ module_inst->e =
+ (WASMModuleInstanceExtra *)((uint8 *)module_inst + extra_info_offset);
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+ module_inst->e->sub_module_inst_list =
+ &module_inst->e->sub_module_inst_list_head;
+ ret = wasm_runtime_sub_module_instantiate(
+ (WASMModuleCommon *)module, (WASMModuleInstanceCommon *)module_inst,
+ stack_size, heap_size, max_memory_pages, error_buf, error_buf_size);
+ if (!ret) {
+ LOG_DEBUG("build a sub module list failed");
+ goto fail;
+ }
+#endif
+
+#if WASM_ENABLE_BULK_MEMORY != 0
+ if (module->data_seg_count > 0) {
+ module_inst->e->common.data_dropped =
+ bh_bitmap_new(0, module->data_seg_count);
+ if (module_inst->e->common.data_dropped == NULL) {
+ LOG_DEBUG("failed to allocate bitmaps");
+ set_error_buf(error_buf, error_buf_size,
+ "failed to allocate bitmaps");
+ goto fail;
+ }
+ for (i = 0; i < module->data_seg_count; i++) {
+ if (!module->data_segments[i]->is_passive)
+ bh_bitmap_set_bit(module_inst->e->common.data_dropped, i);
+ }
+ }
+#endif
+#if WASM_ENABLE_REF_TYPES != 0
+ if (module->table_seg_count > 0) {
+ module_inst->e->common.elem_dropped =
+ bh_bitmap_new(0, module->table_seg_count);
+ if (module_inst->e->common.elem_dropped == NULL) {
+ LOG_DEBUG("failed to allocate bitmaps");
+ set_error_buf(error_buf, error_buf_size,
+ "failed to allocate bitmaps");
+ goto fail;
+ }
+ for (i = 0; i < module->table_seg_count; i++) {
+ if (wasm_elem_is_active(module->table_segments[i].mode))
+ bh_bitmap_set_bit(module_inst->e->common.elem_dropped, i);
+ }
+ }
+#endif
+
+#if WASM_ENABLE_GC != 0
+ if (!is_sub_inst) {
+ uint32 gc_heap_size = wasm_runtime_get_gc_heap_size_default();
+
+ if (gc_heap_size < GC_HEAP_SIZE_MIN)
+ gc_heap_size = GC_HEAP_SIZE_MIN;
+ if (gc_heap_size > GC_HEAP_SIZE_MAX)
+ gc_heap_size = GC_HEAP_SIZE_MAX;
+
+ module_inst->e->common.gc_heap_pool =
+ runtime_malloc(gc_heap_size, error_buf, error_buf_size);
+ if (!module_inst->e->common.gc_heap_pool)
+ goto fail;
+
+ module_inst->e->common.gc_heap_handle = mem_allocator_create(
+ module_inst->e->common.gc_heap_pool, gc_heap_size);
+ if (!module_inst->e->common.gc_heap_handle)
+ goto fail;
+ }
+#endif
+
+#if WASM_ENABLE_DUMP_CALL_STACK != 0
+ if (!(module_inst->frames = runtime_malloc((uint64)sizeof(Vector),
+ error_buf, error_buf_size))) {
+ goto fail;
+ }
+#endif
+
+ /* Instantiate global firstly to get the mutable data size */
+ global_count = module->import_global_count + module->global_count;
+ if (global_count
+ && !(globals = globals_instantiate(module, module_inst, error_buf,
+ error_buf_size))) {
+ goto fail;
+ }
+ module_inst->e->global_count = global_count;
+ module_inst->e->globals = globals;
+ module_inst->global_data = (uint8 *)module_inst + module_inst_struct_size
+ + module_inst_mem_inst_size;
+ module_inst->global_data_size = module->global_data_size;
+ first_table = (WASMTableInstance *)(module_inst->global_data
+ + module->global_data_size);
+
+ module_inst->memory_count =
+ module->import_memory_count + module->memory_count;
+ module_inst->table_count = module->import_table_count + module->table_count;
+ module_inst->e->function_count =
+ module->import_function_count + module->function_count;
+#if WASM_ENABLE_TAGS != 0
+ module_inst->e->tag_count = module->import_tag_count + module->tag_count;
+#endif
+
+ /* export */
+ module_inst->export_func_count = get_export_count(module, EXPORT_KIND_FUNC);
+#if WASM_ENABLE_MULTI_MEMORY != 0
+ module_inst->export_memory_count =
+ get_export_count(module, EXPORT_KIND_MEMORY);
+#endif
+#if WASM_ENABLE_MULTI_MODULE != 0
+ module_inst->export_table_count =
+ get_export_count(module, EXPORT_KIND_TABLE);
+#if WASM_ENABLE_TAGS != 0
+ module_inst->e->export_tag_count =
+ get_export_count(module, EXPORT_KIND_TAG);
+#endif
+ module_inst->export_global_count =
+ get_export_count(module, EXPORT_KIND_GLOBAL);
+#endif
+
+ /* Instantiate memories/tables/functions/tags */
+ if ((module_inst->memory_count > 0
+ && !(module_inst->memories = memories_instantiate(
+ module, module_inst, parent, heap_size, max_memory_pages,
+ error_buf, error_buf_size)))
+ || (module_inst->table_count > 0
+ && !(module_inst->tables =
+ tables_instantiate(module, module_inst, first_table,
+ error_buf, error_buf_size)))
+ || (module_inst->e->function_count > 0
+ && !(module_inst->e->functions = functions_instantiate(
+ module, module_inst, error_buf, error_buf_size)))
+ || (module_inst->export_func_count > 0
+ && !(module_inst->export_functions = export_functions_instantiate(
+ module, module_inst, module_inst->export_func_count,
+ error_buf, error_buf_size)))
+#if WASM_ENABLE_TAGS != 0
+ || (module_inst->e->tag_count > 0
+ && !(module_inst->e->tags = tags_instantiate(
+ module, module_inst, error_buf, error_buf_size)))
+ || (module_inst->e->export_tag_count > 0
+ && !(module_inst->e->export_tags = export_tags_instantiate(
+ module, module_inst, module_inst->e->export_tag_count,
+ error_buf, error_buf_size)))
+#endif
+#if WASM_ENABLE_MULTI_MODULE != 0
+ || (module_inst->export_global_count > 0
+ && !(module_inst->export_globals = export_globals_instantiate(
+ module, module_inst, module_inst->export_global_count,
+ error_buf, error_buf_size)))
+#endif
+#if WASM_ENABLE_MULTI_MEMORY != 0
+ || (module_inst->export_memory_count > 0
+ && !(module_inst->export_memories = export_memories_instantiate(
+ module, module_inst, module_inst->export_memory_count,
+ error_buf, error_buf_size)))
+#endif
+#if WASM_ENABLE_JIT != 0
+ || (module_inst->e->function_count > 0
+ && !init_func_ptrs(module_inst, module, error_buf, error_buf_size))
+#endif
+#if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
+ || (module_inst->e->function_count > 0
+ && !init_func_type_indexes(module_inst, error_buf, error_buf_size))
+#endif
+ ) {
+ goto fail;
+ }
+ if (global_count > 0) {
+ /* Initialize the global data */
+ global_data = module_inst->global_data;
+ global_data_end = global_data + module->global_data_size;
+ global = globals;
+ for (i = 0; i < global_count; i++, global++) {
+ switch (global->type) {
+ case VALUE_TYPE_I32:
+ case VALUE_TYPE_F32:
+#if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
+ case VALUE_TYPE_FUNCREF:
+ case VALUE_TYPE_EXTERNREF:
+#endif
+ *(int32 *)global_data = global->initial_value.i32;
+ global_data += sizeof(int32);
+ break;
+ case VALUE_TYPE_I64:
+ case VALUE_TYPE_F64:
+ bh_memcpy_s(global_data,
+ (uint32)(global_data_end - global_data),
+ &global->initial_value.i64, sizeof(int64));
+ global_data += sizeof(int64);
+ break;
+#if WASM_ENABLE_SIMD != 0
+ case VALUE_TYPE_V128:
+ bh_memcpy_s(global_data, (uint32)sizeof(V128),
+ &global->initial_value.v128, sizeof(V128));
+ global_data += sizeof(V128);
+ break;
+#endif
+#if WASM_ENABLE_GC != 0
+ case VALUE_TYPE_EXTERNREF:
+ /* the initial value should be a null reference */
+ bh_assert(global->initial_value.gc_obj == NULL_REF);
+ STORE_PTR((void **)global_data, NULL_REF);
+ global_data += sizeof(void *);
+ break;
+#endif
+ default:
+ {
+#if WASM_ENABLE_GC != 0
+ InitializerExpression *global_init = NULL;
+ bh_assert(wasm_is_type_reftype(global->type));
+
+ if (i >= module->import_global_count) {
+ global_init =
+ &module->globals[i - module->import_global_count]
+ .init_expr;
+ }
+
+ if (global->type == REF_TYPE_NULLFUNCREF
+ || global->type == REF_TYPE_NULLEXTERNREF
+ || global->type == REF_TYPE_NULLREF) {
+ STORE_PTR((void **)global_data, NULL_REF);
+ global_data += sizeof(void *);
+ break;
+ }
+
+ /* We can't create funcref obj during global instantiation
+ * since the functions are not instantiated yet, so we need
+ * to defer the initialization here */
+ if (global_init
+ && (global_init->init_expr_type
+ == INIT_EXPR_TYPE_FUNCREF_CONST)
+ && wasm_reftype_is_subtype_of(
+ global->type, global->ref_type, REF_TYPE_FUNCREF,
+ NULL, module_inst->module->types,
+ module_inst->module->type_count)) {
+ WASMFuncObjectRef func_obj = NULL;
+ /* UINT32_MAX indicates that it is a null reference */
+ if ((uint32)global->initial_value.i32 != UINT32_MAX) {
+ if (!(func_obj = wasm_create_func_obj(
+ module_inst, global->initial_value.i32,
+ false, error_buf, error_buf_size)))
+ goto fail;
+ }
+ STORE_PTR((void **)global_data, func_obj);
+ global_data += sizeof(void *);
+ /* Also update the inital_value since other globals may
+ * refer to this */
+ global->initial_value.gc_obj = (wasm_obj_t)func_obj;
+ break;
+ }
+ else {
+ STORE_PTR((void **)global_data,
+ global->initial_value.gc_obj);
+ global_data += sizeof(void *);
+ break;
+ }
+#endif
+ bh_assert(0);
+ break;
+ }
+ }
+ }
+ bh_assert(global_data == global_data_end);
+ }
+
+ if (!check_linked_symbol(module_inst, error_buf, error_buf_size)) {
+ goto fail;
+ }
+
+ /* Initialize the memory data with data segment section */
+ for (i = 0; i < module->data_seg_count; i++) {
+ WASMMemoryInstance *memory = NULL;
+ uint8 *memory_data = NULL;
+ uint64 memory_size = 0;
+ WASMDataSeg *data_seg = module->data_segments[i];
+
+#if WASM_ENABLE_BULK_MEMORY != 0
+ if (data_seg->is_passive)
+ continue;
+#endif
+ if (is_sub_inst)
+ /* Ignore setting memory init data if the memory has been
+ initialized */
+ continue;
+
+ /* has check it in loader */
+ memory = module_inst->memories[data_seg->memory_index];
+ bh_assert(memory);
+
+ memory_data = memory->memory_data;
+ memory_size =
+ (uint64)memory->num_bytes_per_page * memory->cur_page_count;
+ bh_assert(memory_data || memory_size == 0);
+
+ bh_assert(data_seg->base_offset.init_expr_type
+ == INIT_EXPR_TYPE_GET_GLOBAL
+ || data_seg->base_offset.init_expr_type
+ == (memory->is_memory64 ? INIT_EXPR_TYPE_I64_CONST
+ : INIT_EXPR_TYPE_I32_CONST));
+
+ if (data_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
+ if (!check_global_init_expr(module,
+ data_seg->base_offset.u.global_index,
+ error_buf, error_buf_size)) {
+ goto fail;
+ }
+
+ if (!globals
+ || globals[data_seg->base_offset.u.global_index].type
+ != (memory->is_memory64 ? VALUE_TYPE_I64
+ : VALUE_TYPE_I32)) {
+ set_error_buf(error_buf, error_buf_size,
+ "data segment does not fit");
+ goto fail;
+ }
+
+#if WASM_ENABLE_MEMORY64 != 0
+ if (memory->is_memory64) {
+ base_offset =
+ (uint64)globals[data_seg->base_offset.u.global_index]
+ .initial_value.i64;
+ }
+ else
+#endif
+ {
+ base_offset =
+ (uint32)globals[data_seg->base_offset.u.global_index]
+ .initial_value.i32;
+ }
+ }
+ else {
+#if WASM_ENABLE_MEMORY64 != 0
+ if (memory->is_memory64) {
+ base_offset = (uint64)data_seg->base_offset.u.i64;
+ }
+ else
+#endif
+ {
+ base_offset = (uint32)data_seg->base_offset.u.i32;
+ }
+ }
+
+ /* check offset */
+ if (base_offset > memory_size) {
+#if WASM_ENABLE_MEMORY64 != 0
+ LOG_DEBUG("base_offset(%" PRIu64 ") > memory_size(%" PRIu64 ")",
+ base_offset, memory_size);
+#else
+ LOG_DEBUG("base_offset(%u) > memory_size(%" PRIu64 ")", base_offset,
+ memory_size);
+#endif
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+ set_error_buf(error_buf, error_buf_size,
+ "out of bounds memory access");
+#else
+ set_error_buf(error_buf, error_buf_size,
+ "data segment does not fit");
+#endif
+ goto fail;
+ }
+
+ /* check offset + length(could be zero) */
+ length = data_seg->data_length;
+ if ((uint64)base_offset + length > memory_size) {
+#if WASM_ENABLE_MEMORY64 != 0
+ LOG_DEBUG("base_offset(%" PRIu64
+ ") + length(%d) > memory_size(%" PRIu64 ")",
+ base_offset, length, memory_size);
+#else
+ LOG_DEBUG("base_offset(%u) + length(%d) > memory_size(%" PRIu64 ")",
+ base_offset, length, memory_size);
+#endif
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+ set_error_buf(error_buf, error_buf_size,
+ "out of bounds memory access");
+#else
+ set_error_buf(error_buf, error_buf_size,
+ "data segment does not fit");
+#endif
+ goto fail;
+ }
+
+ if (memory_data) {
+ bh_memcpy_s(memory_data + base_offset,
+ (uint32)(memory_size - base_offset), data_seg->data,
+ length);
+ }
+ }
+
+#if WASM_ENABLE_JIT != 0 && WASM_ENABLE_SHARED_HEAP != 0
+#if UINTPTR_MAX == UINT64_MAX
+ module_inst->e->shared_heap_start_off.u64 = UINT64_MAX;
+#else
+ module_inst->e->shared_heap_start_off.u32[0] = UINT32_MAX;
+#endif
+#endif
+
+#if WASM_ENABLE_GC != 0
+ /* Initialize the table data with init expr */
+ for (i = 0; i < module->table_count; i++) {
+ WASMTable *table = module->tables + i;
+ WASMTableInstance *table_inst = module_inst->tables[i];
+ table_elem_type_t *table_data;
+ uint32 j;
+
+ if (table->init_expr.init_expr_type == 0) {
+ /* No table initializer */
+ continue;
+ }
+
+ table_data = table_inst->elems;
+
+ bh_assert(
+ table->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL
+ || table->init_expr.init_expr_type == INIT_EXPR_TYPE_FUNCREF_CONST
+ || table->init_expr.init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST);
+
+ if (table->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
+ if (!check_global_init_expr(module, table->init_expr.u.global_index,
+ error_buf, error_buf_size)) {
+ goto fail;
+ }
+
+ table->init_expr.u.gc_obj =
+ globals[table->init_expr.u.global_index].initial_value.gc_obj;
+ }
+ else if (table->init_expr.init_expr_type
+ == INIT_EXPR_TYPE_FUNCREF_CONST) {
+ uint32 func_idx = table->init_expr.u.ref_index;
+ if (func_idx != UINT32_MAX) {
+ if (!(table->init_expr.u.gc_obj =
+ wasm_create_func_obj(module_inst, func_idx, false,
+ error_buf, error_buf_size)))
+ goto fail;
+ }
+ else {
+ table->init_expr.u.gc_obj = NULL_REF;
+ }
+ }
+ else if (table->init_expr.init_expr_type
+ == INIT_EXPR_TYPE_REFNULL_CONST) {
+ table->init_expr.u.gc_obj = NULL_REF;
+ }
+
+ LOG_DEBUG("Init table [%d] elements from [%d] to [%d] as: %p", i, 0,
+ table_inst->cur_size, (void *)table->init_expr.u.gc_obj);
+ for (j = 0; j < table_inst->cur_size; j++) {
+ *(table_data + j) = table->init_expr.u.gc_obj;
+ }
+ }
+#endif /* end of WASM_ENABLE_GC != 0 */
+
+ /* Initialize the table data with table segment section */
+ for (i = 0; module_inst->table_count > 0 && i < module->table_seg_count;
+ i++) {
+ WASMTableSeg *table_seg = module->table_segments + i;
+ /* has check it in loader */
+ WASMTableInstance *table = module_inst->tables[table_seg->table_index];
+ table_elem_type_t *table_data;
+ uint32 j;
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+ uint8 tbl_elem_type;
+ uint32 tbl_init_size, tbl_max_size;
+#endif
+#if WASM_ENABLE_GC != 0
+ WASMRefType *tbl_elem_ref_type;
+#endif
+
+ bh_assert(table);
+
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+ (void)wasm_runtime_get_table_inst_elem_type(
+ (WASMModuleInstanceCommon *)module_inst, table_seg->table_index,
+ &tbl_elem_type,
+#if WASM_ENABLE_GC != 0
+ &tbl_elem_ref_type,
+#endif
+ &tbl_init_size, &tbl_max_size);
+
+#if WASM_ENABLE_GC == 0
+ if (tbl_elem_type != VALUE_TYPE_FUNCREF
+ && tbl_elem_type != VALUE_TYPE_EXTERNREF) {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch: elements segment does not fit");
+ goto fail;
+ }
+#elif WASM_ENABLE_GC != 0
+ if (!wasm_elem_is_declarative(table_seg->mode)
+ && !wasm_reftype_is_subtype_of(
+ table_seg->elem_type, table_seg->elem_ref_type,
+ table->elem_type, table->elem_ref_type.elem_ref_type,
+ module->types, module->type_count)) {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch: elements segment does not fit");
+ goto fail;
+ }
+#endif
+ (void)tbl_init_size;
+ (void)tbl_max_size;
+#endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
+
+ table_data = table->elems;
+#if WASM_ENABLE_MULTI_MODULE != 0
+ if (table_seg->table_index < module->import_table_count
+ && module_inst->e->table_insts_linked[table_seg->table_index]) {
+ table_data =
+ module_inst->e->table_insts_linked[table_seg->table_index]
+ ->elems;
+ }
+#endif
+ bh_assert(table_data);
+
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+ if (!wasm_elem_is_active(table_seg->mode))
+ continue;
+#endif
+
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+ bh_assert(table_seg->base_offset.init_expr_type
+ == INIT_EXPR_TYPE_I32_CONST
+ || table_seg->base_offset.init_expr_type
+ == INIT_EXPR_TYPE_GET_GLOBAL
+ || table_seg->base_offset.init_expr_type
+ == INIT_EXPR_TYPE_FUNCREF_CONST
+ || table_seg->base_offset.init_expr_type
+ == INIT_EXPR_TYPE_REFNULL_CONST);
+#else
+ bh_assert(table_seg->base_offset.init_expr_type
+ == INIT_EXPR_TYPE_I32_CONST
+ || table_seg->base_offset.init_expr_type
+ == INIT_EXPR_TYPE_GET_GLOBAL);
+#endif
+
+ /* init vec(funcidx) or vec(expr) */
+ if (table_seg->base_offset.init_expr_type
+ == INIT_EXPR_TYPE_GET_GLOBAL) {
+ if (!check_global_init_expr(module,
+ table_seg->base_offset.u.global_index,
+ error_buf, error_buf_size)) {
+ goto fail;
+ }
+
+ if (!globals
+ || globals[table_seg->base_offset.u.global_index].type
+ != VALUE_TYPE_I32) {
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch: elements segment does not fit");
+ goto fail;
+ }
+
+ table_seg->base_offset.u.i32 =
+ globals[table_seg->base_offset.u.global_index]
+ .initial_value.i32;
+ }
+
+ /* check offset since length might negative */
+ if ((uint32)table_seg->base_offset.u.i32 > table->cur_size) {
+ LOG_DEBUG("base_offset(%d) > table->cur_size(%d)",
+ table_seg->base_offset.u.i32, table->cur_size);
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+ set_error_buf(error_buf, error_buf_size,
+ "out of bounds table access");
+#else
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch: elements segment does not fit");
+#endif
+ goto fail;
+ }
+
+ /* check offset + length(could be zero) */
+ length = table_seg->value_count;
+ if ((uint32)table_seg->base_offset.u.i32 + length > table->cur_size) {
+ LOG_DEBUG("base_offset(%d) + length(%d)> table->cur_size(%d)",
+ table_seg->base_offset.u.i32, length, table->cur_size);
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+ set_error_buf(error_buf, error_buf_size,
+ "out of bounds table access");
+#else
+ set_error_buf(error_buf, error_buf_size,
+ "type mismatch: elements segment does not fit");
+#endif
+ goto fail;
+ }
+
+ for (j = 0; j < length; j++) {
+ InitializerExpression *init_expr = &table_seg->init_values[j];
+ uint8 flag = init_expr->init_expr_type;
+ void *ref = NULL;
+
+ /* const and get global init values should be resolved during
+ * loading */
+ bh_assert((flag == INIT_EXPR_TYPE_GET_GLOBAL)
+ || (flag == INIT_EXPR_TYPE_REFNULL_CONST)
+ || ((flag >= INIT_EXPR_TYPE_FUNCREF_CONST)
+ && (flag <= INIT_EXPR_TYPE_EXTERN_CONVERT_ANY)));
+
+ switch (flag) {
+ case INIT_EXPR_TYPE_REFNULL_CONST:
+ ref = NULL;
+ break;
+ case INIT_EXPR_TYPE_FUNCREF_CONST:
+ {
+#if WASM_ENABLE_GC == 0
+ ref = (void *)(uintptr_t)init_expr->u.ref_index;
+#else
+ WASMFuncObjectRef func_obj;
+ uint32 func_idx = init_expr->u.ref_index;
+ /* UINT32_MAX indicates that it is a null reference */
+ if (func_idx != UINT32_MAX) {
+ if (!(func_obj = wasm_create_func_obj(
+ module_inst, func_idx, false, error_buf,
+ error_buf_size))) {
+ goto fail;
+ }
+ ref = func_obj;
+ }
+ else {
+ ref = NULL_REF;
+ }
+#endif /* end of WASM_ENABLE_GC != 0 */
+ break;
+ }
+#if WASM_ENABLE_GC != 0
+ case INIT_EXPR_TYPE_GET_GLOBAL:
+ {
+ if (!check_global_init_expr(module,
+ init_expr->u.global_index,
+ error_buf, error_buf_size)) {
+ goto fail;
+ }
+
+ ref =
+ globals[init_expr->u.global_index].initial_value.gc_obj;
+ break;
+ }
+ case INIT_EXPR_TYPE_STRUCT_NEW:
+ case INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT:
+ {
+ WASMRttType *rtt_type;
+ WASMStructObjectRef struct_obj;
+ WASMStructType *struct_type;
+ WASMStructNewInitValues *init_values = NULL;
+ uint32 type_idx;
+
+ if (flag == INIT_EXPR_TYPE_STRUCT_NEW) {
+ init_values =
+ (WASMStructNewInitValues *)init_expr->u.data;
+ type_idx = init_values->type_idx;
+ }
+ else {
+ type_idx = init_expr->u.type_index;
+ }
+
+ struct_type = (WASMStructType *)module->types[type_idx];
+
+ if (!(rtt_type = wasm_rtt_type_new(
+ (WASMType *)struct_type, type_idx,
+ module->rtt_types, module->type_count,
+ &module->rtt_type_lock))) {
+ set_error_buf(error_buf, error_buf_size,
+ "create rtt object failed");
+ goto fail;
+ }
+
+ if (!(struct_obj = wasm_struct_obj_new_internal(
+ module_inst->e->common.gc_heap_handle,
+ rtt_type))) {
+ set_error_buf(error_buf, error_buf_size,
+ "create struct object failed");
+ goto fail;
+ }
+
+ if (flag == INIT_EXPR_TYPE_STRUCT_NEW) {
+ uint32 field_idx;
+
+ bh_assert(init_values->count
+ == struct_type->field_count);
+
+ for (field_idx = 0; field_idx < init_values->count;
+ field_idx++) {
+ wasm_struct_obj_set_field(
+ struct_obj, field_idx,
+ &init_values->fields[field_idx]);
+ }
+ }
+
+ ref = struct_obj;
+ break;
+ }
+ case INIT_EXPR_TYPE_ARRAY_NEW:
+ case INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT:
+ case INIT_EXPR_TYPE_ARRAY_NEW_FIXED:
+ {
+ WASMRttType *rtt_type;
+ WASMArrayObjectRef array_obj;
+ WASMArrayType *array_type;
+ WASMArrayNewInitValues *init_values = NULL;
+ WASMValue *arr_init_val = NULL, empty_val = { 0 };
+ uint32 type_idx, len;
+
+ if (flag == INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT) {
+ type_idx = init_expr->u.array_new_default.type_index;
+ len = init_expr->u.array_new_default.length;
+ arr_init_val = &empty_val;
+ }
+ else {
+ init_values =
+ (WASMArrayNewInitValues *)init_expr->u.data;
+ type_idx = init_values->type_idx;
+ len = init_values->length;
+
+ if (flag == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
+ arr_init_val = init_values->elem_data;
+ }
+ }
+
+ array_type = (WASMArrayType *)module->types[type_idx];
+
+ if (!(rtt_type = wasm_rtt_type_new(
+ (WASMType *)array_type, type_idx,
+ module->rtt_types, module->type_count,
+ &module->rtt_type_lock))) {
+ set_error_buf(error_buf, error_buf_size,
+ "create rtt object failed");
+ goto fail;
+ }
+
+ if (!(array_obj = wasm_array_obj_new_internal(
+ module_inst->e->common.gc_heap_handle, rtt_type,
+ len, arr_init_val))) {
+ set_error_buf(error_buf, error_buf_size,
+ "create array object failed");
+ goto fail;
+ }
+
+ if (flag == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
+ uint32 elem_idx;
+
+ bh_assert(init_values);
+
+ for (elem_idx = 0; elem_idx < len; elem_idx++) {
+ wasm_array_obj_set_elem(
+ array_obj, elem_idx,
+ &init_values->elem_data[elem_idx]);
+ }
+ }
+
+ ref = array_obj;
+
+ break;
+ }
+ case INIT_EXPR_TYPE_I31_NEW:
+ {
+ ref = (wasm_obj_t)wasm_i31_obj_new(init_expr->u.i32);
+ break;
+ }
+#endif /* end of WASM_ENABLE_GC != 0 */
+ }
+
+ *(table_data + table_seg->base_offset.u.i32 + j) =
+ (table_elem_type_t)ref;
+ }
+ }
+
+ /* Initialize the thread related data */
+ if (stack_size == 0)
+ stack_size = DEFAULT_WASM_STACK_SIZE;
+
+ module_inst->default_wasm_stack_size = stack_size;
+
+ if (module->malloc_function != (uint32)-1) {
+ module_inst->e->malloc_function =
+ &module_inst->e->functions[module->malloc_function];
+ }
+
+ if (module->free_function != (uint32)-1) {
+ module_inst->e->free_function =
+ &module_inst->e->functions[module->free_function];
+ }
+
+ if (module->retain_function != (uint32)-1) {
+ module_inst->e->retain_function =
+ &module_inst->e->functions[module->retain_function];
+ }
+
+#if WASM_ENABLE_LIBC_WASI != 0
+ /* The sub-instance will get the wasi_ctx from main-instance */
+ if (!is_sub_inst) {
+ if (!wasm_runtime_init_wasi(
+ (WASMModuleInstanceCommon *)module_inst,
+ module->wasi_args.dir_list, module->wasi_args.dir_count,
+ module->wasi_args.map_dir_list, module->wasi_args.map_dir_count,
+ module->wasi_args.env, module->wasi_args.env_count,
+ module->wasi_args.addr_pool, module->wasi_args.addr_count,
+ module->wasi_args.ns_lookup_pool,
+ module->wasi_args.ns_lookup_count, module->wasi_args.argv,
+ module->wasi_args.argc, module->wasi_args.stdio[0],
+ module->wasi_args.stdio[1], module->wasi_args.stdio[2],
+ error_buf, error_buf_size)) {
+ goto fail;
+ }
+ }
+#endif
+
+#if WASM_ENABLE_DEBUG_INTERP != 0
+ if (!is_sub_inst) {
+ /* Add module instance into module's instance list */
+ os_mutex_lock(&module->instance_list_lock);
+ if (module->instance_list) {
+ LOG_WARNING(
+ "warning: multiple instances referencing to the same module "
+ "may cause unexpected behaviour during debugging");
+ }
+ module_inst->e->next = module->instance_list;
+ module->instance_list = module_inst;
+ os_mutex_unlock(&module->instance_list_lock);
+ }
+#endif
+
+ /* Set running mode before executing wasm functions */
+ if (!set_running_mode(module_inst, wasm_runtime_get_default_running_mode(),
+ true)) {
+ set_error_buf(error_buf, error_buf_size,
+ "set instance running mode failed");
+ goto fail;
+ }
+
+ if (module->start_function != (uint32)-1) {
+ /* TODO: fix start function can be import function issue */
+ if (module->start_function >= module->import_function_count)
+ module_inst->e->start_function =
+ &module_inst->e->functions[module->start_function];
+ }
+
+ if (!execute_post_instantiate_functions(module_inst, is_sub_inst,
+ exec_env_main)) {
+ set_error_buf(error_buf, error_buf_size, module_inst->cur_exception);
+ goto fail;
+ }
+
+#if WASM_ENABLE_MEMORY_TRACING != 0
+ wasm_runtime_dump_module_inst_mem_consumption(
+ (WASMModuleInstanceCommon *)module_inst);
+#endif
+
+ (void)global_data_end;
+ return module_inst;
+
+fail:
+ wasm_deinstantiate(module_inst, false);
+ return NULL;
+}
+
+#if WASM_ENABLE_DUMP_CALL_STACK != 0
+static void
+destroy_c_api_frames(Vector *frames)
+{
+ WASMCApiFrame frame = { 0 };
+ uint32 i, total_frames, ret;
+
+ total_frames = (uint32)bh_vector_size(frames);
+
+ for (i = 0; i < total_frames; i++) {
+ ret = bh_vector_get(frames, i, &frame);
+ bh_assert(ret);
+
+ if (frame.lp)
+ wasm_runtime_free(frame.lp);
+ }
+
+ ret = bh_vector_destroy(frames);
+ bh_assert(ret);
+ (void)ret;
+}
+#endif
+
+void
+wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
+{
+ if (!module_inst)
+ return;
+
+ if (module_inst->exec_env_singleton) {
+ /* wasm_exec_env_destroy will call
+ wasm_cluster_wait_for_all_except_self to wait for other
+ threads, so as to destroy their exec_envs and module
+ instances first, and avoid accessing the shared resources
+ of current module instance after it is deinstantiated. */
+ wasm_exec_env_destroy(module_inst->exec_env_singleton);
+ }
+
+#if WASM_ENABLE_DEBUG_INTERP != 0 \
+ || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
+ && WASM_ENABLE_LAZY_JIT != 0)
+ /* Remove instance from module's instance list before freeing
+ func_ptrs and fast_jit_func_ptrs of the instance, to avoid
+ accessing the freed memory in the jit backend compilation
+ threads */
+ {
+ WASMModule *module = module_inst->module;
+ WASMModuleInstance *instance_prev = NULL, *instance;
+ os_mutex_lock(&module->instance_list_lock);
+
+ instance = module->instance_list;
+ while (instance) {
+ if (instance == module_inst) {
+ if (!instance_prev)
+ module->instance_list = instance->e->next;
+ else
+ instance_prev->e->next = instance->e->next;
+ break;
+ }
+ instance_prev = instance;
+ instance = instance->e->next;
+ }
+
+ os_mutex_unlock(&module->instance_list_lock);
+ }
+#endif
+
+#if WASM_ENABLE_JIT != 0
+ if (module_inst->func_ptrs)
+ wasm_runtime_free(module_inst->func_ptrs);
+#endif
+
+#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
+ && WASM_ENABLE_LAZY_JIT != 0
+ if (module_inst->fast_jit_func_ptrs
+ && module_inst->fast_jit_func_ptrs
+ != module_inst->module->fast_jit_func_ptrs)
+ wasm_runtime_free(module_inst->fast_jit_func_ptrs);
+#endif
+
+#if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
+ if (module_inst->func_type_indexes)
+ wasm_runtime_free(module_inst->func_type_indexes);
+#endif
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+ wasm_runtime_sub_module_deinstantiate(
+ (WASMModuleInstanceCommon *)module_inst);
+#endif
+
+ if (module_inst->memory_count > 0)
+ memories_deinstantiate(module_inst, module_inst->memories,
+ module_inst->memory_count);
+
+ if (module_inst->import_func_ptrs) {
+ wasm_runtime_free(module_inst->import_func_ptrs);
+ }
+
+ tables_deinstantiate(module_inst);
+ functions_deinstantiate(module_inst->e->functions);
+#if WASM_ENABLE_TAGS != 0
+ tags_deinstantiate(module_inst->e->tags, module_inst->e->import_tag_ptrs);
+#endif
+ globals_deinstantiate(module_inst->e->globals);
+ export_functions_deinstantiate(module_inst->export_functions);
+#if WASM_ENABLE_TAGS != 0
+ export_tags_deinstantiate(module_inst->e->export_tags);
+#endif
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+ export_globals_deinstantiate(module_inst->export_globals);
+#endif
+
+#if WASM_ENABLE_MULTI_MEMORY != 0
+ export_memories_deinstantiate(module_inst->export_memories);
+#endif
+
+#if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
+ wasm_externref_cleanup((WASMModuleInstanceCommon *)module_inst);
+#endif
+
+#if WASM_ENABLE_GC != 0
+ if (!is_sub_inst) {
+ if (module_inst->e->common.gc_heap_handle)
+ mem_allocator_destroy(module_inst->e->common.gc_heap_handle);
+ if (module_inst->e->common.gc_heap_pool)
+ wasm_runtime_free(module_inst->e->common.gc_heap_pool);
+ }
+#endif
+
+#if WASM_ENABLE_DUMP_CALL_STACK != 0
+ if (module_inst->frames) {
+ destroy_c_api_frames(module_inst->frames);
+ wasm_runtime_free(module_inst->frames);
+ module_inst->frames = NULL;
+ }
+#endif
+
+ if (module_inst->c_api_func_imports)
+ wasm_runtime_free(module_inst->c_api_func_imports);
+
+ if (!is_sub_inst) {
+ wasm_native_call_context_dtors((WASMModuleInstanceCommon *)module_inst);
+ }
+
+#if WASM_ENABLE_BULK_MEMORY != 0
+ bh_bitmap_delete(module_inst->e->common.data_dropped);
+#endif
+#if WASM_ENABLE_REF_TYPES != 0
+ bh_bitmap_delete(module_inst->e->common.elem_dropped);
+#endif
+
+ wasm_runtime_free(module_inst);
+}
+
+WASMFunctionInstance *
+wasm_lookup_function(const WASMModuleInstance *module_inst, const char *name)
+{
+ WASMExportFuncInstance key = { .name = (char *)name };
+ WASMExportFuncInstance *export_func_inst;
+
+ if (!module_inst->export_functions)
+ return NULL;
+
+ export_func_inst = bsearch(
+ &key, module_inst->export_functions, module_inst->export_func_count,
+ sizeof(WASMExportFuncInstance), cmp_export_func_inst);
+
+ if (!export_func_inst)
+ return NULL;
+
+ return export_func_inst->function;
+}
+
+WASMMemoryInstance *
+wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name)
+{
+#if WASM_ENABLE_MULTI_MEMORY != 0
+ uint32 i;
+ for (i = 0; i < module_inst->export_memory_count; i++)
+ if (!strcmp(module_inst->export_memories[i].name, name))
+ return module_inst->export_memories[i].memory;
+ return NULL;
+#else
+ (void)module_inst->export_memories;
+ if (!module_inst->memories)
+ return NULL;
+ return module_inst->memories[0];
+#endif
+}
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+WASMGlobalInstance *
+wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name)
+{
+ uint32 i;
+ for (i = 0; i < module_inst->export_global_count; i++)
+ if (!strcmp(module_inst->export_globals[i].name, name))
+ return module_inst->export_globals[i].global;
+ return NULL;
+}
+
+WASMTableInstance *
+wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name)
+{
+ /**
+ * using a strong assumption that one module instance only has
+ * one table instance
+ */
+ (void)module_inst->export_tables;
+ return module_inst->tables[0];
+}
+
+#if WASM_ENABLE_TAGS != 0
+WASMTagInstance *
+wasm_lookup_tag(const WASMModuleInstance *module_inst, const char *name,
+ const char *signature)
+{
+ uint32 i;
+ for (i = 0; i < module_inst->e->export_tag_count; i++)
+ if (!strcmp(module_inst->e->export_tags[i].name, name))
+ return module_inst->e->export_tags[i].tag;
+ (void)signature;
+ return NULL;
+}
+#endif
+
+#endif
+
+#ifdef OS_ENABLE_HW_BOUND_CHECK
+static void
+call_wasm_with_hw_bound_check(WASMModuleInstance *module_inst,
+ WASMExecEnv *exec_env,
+ WASMFunctionInstance *function, unsigned argc,
+ uint32 argv[])
+{
+ WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
+ WASMJmpBuf jmpbuf_node = { 0 }, *jmpbuf_node_pop;
+ WASMRuntimeFrame *prev_frame = wasm_exec_env_get_cur_frame(exec_env);
+ uint8 *prev_top = exec_env->wasm_stack.top;
+#ifdef BH_PLATFORM_WINDOWS
+ int result;
+ bool has_exception;
+ char exception[EXCEPTION_BUF_LEN];
+#endif
+ bool ret = true;
+
+ /* Check native stack overflow firstly to ensure we have enough
+ native stack to run the following codes before actually calling
+ the aot function in invokeNative function. */
+ if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
+ return;
+ }
+
+ if (!exec_env_tls) {
+ if (!os_thread_signal_inited()) {
+ wasm_set_exception(module_inst, "thread signal env not inited");
+ return;
+ }
+
+ /* Set thread handle and stack boundary if they haven't been set */
+ wasm_exec_env_set_thread_info(exec_env);
+
+ wasm_runtime_set_exec_env_tls(exec_env);
+ }
+ else {
+ if (exec_env_tls != exec_env) {
+ wasm_set_exception(module_inst, "invalid exec env");
+ return;
+ }
+ }
+
+ wasm_exec_env_push_jmpbuf(exec_env, &jmpbuf_node);
+
+ if (os_setjmp(jmpbuf_node.jmpbuf) == 0) {
+#ifndef BH_PLATFORM_WINDOWS
+ wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
+#else
+ __try {
+ wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
+ } __except (wasm_copy_exception(module_inst, NULL)
+ ? EXCEPTION_EXECUTE_HANDLER
+ : EXCEPTION_CONTINUE_SEARCH) {
+ /* Exception was thrown in wasm_exception_handler */
+ ret = false;
+ }
+ has_exception = wasm_copy_exception(module_inst, exception);
+ if (has_exception && strstr(exception, "native stack overflow")) {
+ /* After a stack overflow, the stack was left
+ in a damaged state, let the CRT repair it */
+ result = _resetstkoflw();
+ bh_assert(result != 0);
+ }
+#endif
+ }
+ else {
+ /* Exception has been set in signal handler before calling longjmp */
+ ret = false;
+ }
+
+ /* Note: can't check wasm_get_exception(module_inst) here, there may be
+ * exception which is not caught by hardware (e.g. uninitialized elements),
+ * then the stack-frame is already freed inside wasm_interp_call_wasm */
+ if (!ret) {
+#if WASM_ENABLE_DUMP_CALL_STACK != 0
+ if (wasm_interp_create_call_stack(exec_env)) {
+ wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
+ }
+#endif
+ /* Restore operand frames */
+ wasm_exec_env_set_cur_frame(exec_env, prev_frame);
+ exec_env->wasm_stack.top = prev_top;
+ }
+
+ jmpbuf_node_pop = wasm_exec_env_pop_jmpbuf(exec_env);
+ bh_assert(&jmpbuf_node == jmpbuf_node_pop);
+ if (!exec_env->jmpbuf_stack_top) {
+ wasm_runtime_set_exec_env_tls(NULL);
+ }
+ if (!ret) {
+ os_sigreturn();
+ os_signal_unmask();
+ }
+ (void)jmpbuf_node_pop;
+}
+#define interp_call_wasm call_wasm_with_hw_bound_check
+#else
+#define interp_call_wasm wasm_interp_call_wasm
+#endif
+
+bool
+wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
+ unsigned argc, uint32 argv[])
+{
+ WASMModuleInstance *module_inst =
+ (WASMModuleInstance *)exec_env->module_inst;
+
+#ifndef OS_ENABLE_HW_BOUND_CHECK
+ /* Set thread handle and stack boundary */
+ wasm_exec_env_set_thread_info(exec_env);
+#else
+ /* Set thread info in call_wasm_with_hw_bound_check when
+ hw bound check is enabled */
+#endif
+
+ /* Set exec env, so it can be later retrieved from instance */
+ module_inst->cur_exec_env = exec_env;
+
+ interp_call_wasm(module_inst, exec_env, function, argc, argv);
+ return !wasm_copy_exception(module_inst, NULL);
+}
+
+#if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0
+/* look for the function name */
+static char *
+get_func_name_from_index(const WASMModuleInstance *inst, uint32 func_index)
+{
+ char *func_name = NULL;
+ WASMFunctionInstance *func_inst = inst->e->functions + func_index;
+
+ if (func_inst->is_import_func) {
+ func_name = func_inst->u.func_import->field_name;
+ }
+ else {
+#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
+ func_name = func_inst->u.func->field_name;
+#endif
+ /* if custom name section is not generated,
+ search symbols from export table */
+ if (!func_name) {
+ unsigned j;
+ for (j = 0; j < inst->export_func_count; j++) {
+ WASMExportFuncInstance *export_func =
+ inst->export_functions + j;
+ if (export_func->function == func_inst) {
+ func_name = export_func->name;
+ break;
+ }
+ }
+ }
+ }
+
+ return func_name;
+}
+#endif /*WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0*/
+
+#if WASM_ENABLE_PERF_PROFILING != 0
+void
+wasm_dump_perf_profiling(const WASMModuleInstance *module_inst)
+{
+ WASMFunctionInstance *func_inst;
+ char *func_name;
+ uint32 i;
+
+ os_printf("Performance profiler data:\n");
+ for (i = 0; i < module_inst->e->function_count; i++) {
+ func_inst = module_inst->e->functions + i;
+
+ if (func_inst->total_exec_cnt == 0)
+ continue;
+
+ func_name = get_func_name_from_index(module_inst, i);
+ if (func_name)
+ os_printf(
+ " func %s, execution time: %.3f ms, execution count: %" PRIu32
+ " times, children execution time: %.3f ms\n",
+ func_name, func_inst->total_exec_time / 1000.0f,
+ func_inst->total_exec_cnt,
+ func_inst->children_exec_time / 1000.0f);
+ else
+ os_printf(" func %" PRIu32
+ ", execution time: %.3f ms, execution count: %" PRIu32
+ " times, children execution time: %.3f ms\n",
+ i, func_inst->total_exec_time / 1000.0f,
+ func_inst->total_exec_cnt,
+ func_inst->children_exec_time / 1000.0f);
+ }
+}
+
+double
+wasm_summarize_wasm_execute_time(const WASMModuleInstance *inst)
+{
+ double ret = 0;
+
+ unsigned i;
+ for (i = 0; i < inst->e->function_count; i++) {
+ WASMFunctionInstance *func = inst->e->functions + i;
+ ret += (func->total_exec_time - func->children_exec_time) / 1000.0f;
+ }
+
+ return ret;
+}
+
+double
+wasm_get_wasm_func_exec_time(const WASMModuleInstance *inst,
+ const char *func_name)
+{
+ unsigned i;
+ for (i = 0; i < inst->e->function_count; i++) {
+ char *name_in_wasm = get_func_name_from_index(inst, i);
+ if (name_in_wasm && strcmp(name_in_wasm, func_name) == 0) {
+ WASMFunctionInstance *func = inst->e->functions + i;
+ return (func->total_exec_time - func->children_exec_time) / 1000.0f;
+ }
+ }
+
+ return -1.0;
+}
+#endif /*WASM_ENABLE_PERF_PROFILING != 0*/
+
+uint64
+wasm_module_malloc_internal(WASMModuleInstance *module_inst,
+ WASMExecEnv *exec_env, uint64 size,
+ void **p_native_addr)
+{
+ WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
+ uint8 *addr = NULL;
+ uint64 offset = 0;
+
+ /* TODO: Memory64 size check based on memory idx type */
+ bh_assert(size <= UINT32_MAX);
+
+ if (!memory) {
+ wasm_set_exception(module_inst, "uninitialized memory");
+ return 0;
+ }
+
+ if (memory->heap_handle) {
+ addr = mem_allocator_malloc(memory->heap_handle, (uint32)size);
+ }
+ else if (module_inst->e->malloc_function && module_inst->e->free_function) {
+ if (!execute_malloc_function(
+ module_inst, exec_env, module_inst->e->malloc_function,
+ module_inst->e->retain_function, size, &offset)) {
+ return 0;
+ }
+ /* If we use app's malloc function,
+ the default memory may be changed while memory growing */
+ memory = wasm_get_default_memory(module_inst);
+ addr = offset ? memory->memory_data + offset : NULL;
+ }
+
+ if (!addr) {
+ if (memory->heap_handle
+ && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
+ wasm_runtime_show_app_heap_corrupted_prompt();
+ wasm_set_exception(module_inst, "app heap corrupted");
+ }
+ else {
+ LOG_WARNING("warning: allocate %" PRIu64 " bytes memory failed",
+ size);
+ }
+ return 0;
+ }
+ if (p_native_addr)
+ *p_native_addr = addr;
+
+ return (uint64)(addr - memory->memory_data);
+}
+
+uint64
+wasm_module_realloc_internal(WASMModuleInstance *module_inst,
+ WASMExecEnv *exec_env, uint64 ptr, uint64 size,
+ void **p_native_addr)
+{
+ WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
+ uint8 *addr = NULL;
+
+ /* TODO: Memory64 ptr and size check based on memory idx type */
+ bh_assert(ptr <= UINT32_MAX);
+ bh_assert(size <= UINT32_MAX);
+
+ if (!memory) {
+ wasm_set_exception(module_inst, "uninitialized memory");
+ return 0;
+ }
+
+ if (memory->heap_handle) {
+ addr = mem_allocator_realloc(
+ memory->heap_handle,
+ (uint32)ptr ? memory->memory_data + (uint32)ptr : NULL,
+ (uint32)size);
+ }
+
+ /* Only support realloc in WAMR's app heap */
+ (void)exec_env;
+
+ if (!addr) {
+ if (memory->heap_handle
+ && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
+ wasm_set_exception(module_inst, "app heap corrupted");
+ }
+ else {
+ wasm_set_exception(module_inst, "out of memory");
+ }
+ return 0;
+ }
+ if (p_native_addr)
+ *p_native_addr = addr;
+
+ return (uint64)(addr - memory->memory_data);
+}
+
+void
+wasm_module_free_internal(WASMModuleInstance *module_inst,
+ WASMExecEnv *exec_env, uint64 ptr)
+{
+ WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
+
+ /* TODO: Memory64 ptr and size check based on memory idx type */
+ bh_assert(ptr <= UINT32_MAX);
+
+ if (!memory) {
+ return;
+ }
+
+ if (ptr) {
+ uint8 *addr = memory->memory_data + (uint32)ptr;
+ uint8 *memory_data_end;
+
+ /* memory->memory_data_end may be changed in memory grow */
+ SHARED_MEMORY_LOCK(memory);
+ memory_data_end = memory->memory_data_end;
+ SHARED_MEMORY_UNLOCK(memory);
+
+ if (memory->heap_handle && memory->heap_data <= addr
+ && addr < memory->heap_data_end) {
+ mem_allocator_free(memory->heap_handle, addr);
+ }
+ else if (module_inst->e->malloc_function
+ && module_inst->e->free_function && memory->memory_data <= addr
+ && addr < memory_data_end) {
+ execute_free_function(module_inst, exec_env,
+ module_inst->e->free_function, ptr);
+ }
+ }
+}
+
+uint64
+wasm_module_malloc(WASMModuleInstance *module_inst, uint64 size,
+ void **p_native_addr)
+{
+ return wasm_module_malloc_internal(module_inst, NULL, size, p_native_addr);
+}
+
+uint64
+wasm_module_realloc(WASMModuleInstance *module_inst, uint64 ptr, uint64 size,
+ void **p_native_addr)
+{
+ return wasm_module_realloc_internal(module_inst, NULL, ptr, size,
+ p_native_addr);
+}
+
+void
+wasm_module_free(WASMModuleInstance *module_inst, uint64 ptr)
+{
+ wasm_module_free_internal(module_inst, NULL, ptr);
+}
+
+uint64
+wasm_module_dup_data(WASMModuleInstance *module_inst, const char *src,
+ uint64 size)
+{
+ char *buffer;
+ uint64 buffer_offset;
+
+ /* TODO: Memory64 size check based on memory idx type */
+ bh_assert(size <= UINT32_MAX);
+
+ buffer_offset = wasm_module_malloc(module_inst, size, (void **)&buffer);
+
+ if (buffer_offset != 0) {
+ buffer = wasm_runtime_addr_app_to_native(
+ (WASMModuleInstanceCommon *)module_inst, buffer_offset);
+ bh_memcpy_s(buffer, (uint32)size, src, (uint32)size);
+ }
+ return buffer_offset;
+}
+
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+bool
+wasm_enlarge_table(WASMModuleInstance *module_inst, uint32 table_idx,
+ uint32 inc_size, table_elem_type_t init_val)
+{
+ uint32 total_size, i;
+ table_elem_type_t *new_table_data_start;
+ WASMTableInstance *table_inst;
+
+ if (!inc_size) {
+ return true;
+ }
+
+ bh_assert(table_idx < module_inst->table_count);
+ table_inst = wasm_get_table_inst(module_inst, table_idx);
+ if (!table_inst) {
+ return false;
+ }
+
+ if (inc_size > UINT32_MAX - table_inst->cur_size) {
+ return false;
+ }
+
+ total_size = table_inst->cur_size + inc_size;
+ if (total_size > table_inst->max_size) {
+ return false;
+ }
+
+ /* fill in */
+ new_table_data_start = table_inst->elems + table_inst->cur_size;
+ for (i = 0; i < inc_size; ++i) {
+ new_table_data_start[i] = init_val;
+ }
+
+ table_inst->cur_size = total_size;
+ return true;
+}
+#endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
+
+static bool
+call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 tbl_elem_idx,
+ uint32 argc, uint32 argv[], bool check_type_idx, uint32 type_idx)
+{
+ WASMModuleInstance *module_inst = NULL;
+ WASMTableInstance *table_inst = NULL;
+ table_elem_type_t tbl_elem_val = NULL_REF;
+ uint32 func_idx = 0;
+ WASMFunctionInstance *func_inst = NULL;
+
+ module_inst = (WASMModuleInstance *)exec_env->module_inst;
+ bh_assert(module_inst);
+
+ table_inst = module_inst->tables[tbl_idx];
+ if (!table_inst) {
+ wasm_set_exception(module_inst, "unknown table");
+ goto got_exception;
+ }
+
+ if (tbl_elem_idx >= table_inst->cur_size) {
+ wasm_set_exception(module_inst, "undefined element");
+ goto got_exception;
+ }
+
+ tbl_elem_val = ((table_elem_type_t *)table_inst->elems)[tbl_elem_idx];
+ if (tbl_elem_val == NULL_REF) {
+ wasm_set_exception(module_inst, "uninitialized element");
+ goto got_exception;
+ }
+
+#if WASM_ENABLE_GC == 0
+ func_idx = (uint32)tbl_elem_val;
+#else
+ func_idx =
+ wasm_func_obj_get_func_idx_bound((WASMFuncObjectRef)tbl_elem_val);
+#endif
+
+ /**
+ * we insist to call functions owned by the module itself
+ **/
+ if (func_idx >= module_inst->e->function_count) {
+ wasm_set_exception(module_inst, "unknown function");
+ goto got_exception;
+ }
+
+ func_inst = module_inst->e->functions + func_idx;
+
+ if (check_type_idx) {
+ WASMType *cur_type = module_inst->module->types[type_idx];
+ WASMType *cur_func_type;
+
+ if (func_inst->is_import_func)
+ cur_func_type = (WASMType *)func_inst->u.func_import->func_type;
+ else
+ cur_func_type = (WASMType *)func_inst->u.func->func_type;
+
+ if (cur_type != cur_func_type) {
+ wasm_set_exception(module_inst, "indirect call type mismatch");
+ goto got_exception;
+ }
+ }
+
+ interp_call_wasm(module_inst, exec_env, func_inst, argc, argv);
+
+ return !wasm_copy_exception(module_inst, NULL);
+
+got_exception:
+ return false;
+}
+
+bool
+wasm_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
+ uint32 argc, uint32 argv[])
+{
+ return call_indirect(exec_env, tbl_idx, elem_idx, argc, argv, false, 0);
+}
+
+#if WASM_ENABLE_THREAD_MGR != 0
+bool
+wasm_set_aux_stack(WASMExecEnv *exec_env, uint64 start_offset, uint32 size)
+{
+ WASMModuleInstance *module_inst =
+ (WASMModuleInstance *)exec_env->module_inst;
+ uint32 stack_top_idx = module_inst->module->aux_stack_top_global_index;
+
+#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
+ /* Check the aux stack space */
+ uint64 data_end = module_inst->module->aux_data_end;
+ uint64 stack_bottom = module_inst->module->aux_stack_bottom;
+ bool is_stack_before_data = stack_bottom < data_end ? true : false;
+ if ((is_stack_before_data && (size > start_offset))
+ || ((!is_stack_before_data) && (start_offset - data_end < size)))
+ return false;
+#endif
+
+ if (stack_top_idx != (uint32)-1) {
+ /* The aux stack top is a wasm global,
+ set the initial value for the global */
+ uint8 *global_addr =
+ module_inst->global_data
+ + module_inst->e->globals[stack_top_idx].data_offset;
+ *(int32 *)global_addr = (uint32)start_offset;
+ /* The aux stack boundary is a constant value,
+ set the value to exec_env */
+ exec_env->aux_stack_boundary = (uintptr_t)start_offset - size;
+ exec_env->aux_stack_bottom = (uintptr_t)start_offset;
+ return true;
+ }
+
+ return false;
+}
+
+bool
+wasm_get_aux_stack(WASMExecEnv *exec_env, uint64 *start_offset, uint32 *size)
+{
+ WASMModuleInstance *module_inst =
+ (WASMModuleInstance *)exec_env->module_inst;
+
+ /* The aux stack information is resolved in loader
+ and store in module */
+ uint64 stack_bottom = module_inst->module->aux_stack_bottom;
+ uint32 total_aux_stack_size = module_inst->module->aux_stack_size;
+
+ if (stack_bottom != 0 && total_aux_stack_size != 0) {
+ if (start_offset)
+ *start_offset = stack_bottom;
+ if (size)
+ *size = total_aux_stack_size;
+ return true;
+ }
+ return false;
+}
+#endif
+
+#if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
+void
+wasm_get_module_mem_consumption(const WASMModule *module,
+ WASMModuleMemConsumption *mem_conspn)
+{
+ uint32 i, size;
+
+ memset(mem_conspn, 0, sizeof(*mem_conspn));
+
+ mem_conspn->module_struct_size = sizeof(WASMModule);
+
+ mem_conspn->types_size = sizeof(WASMFuncType *) * module->type_count;
+ for (i = 0; i < module->type_count; i++) {
+ WASMFuncType *type = module->types[i];
+ size = offsetof(WASMFuncType, types)
+ + sizeof(uint8) * (type->param_count + type->result_count);
+ mem_conspn->types_size += size;
+ }
+
+ mem_conspn->imports_size = sizeof(WASMImport) * module->import_count;
+
+ mem_conspn->functions_size =
+ sizeof(WASMFunction *) * module->function_count;
+ for (i = 0; i < module->function_count; i++) {
+ WASMFunction *func = module->functions[i];
+ WASMFuncType *type = func->func_type;
+ size = sizeof(WASMFunction) + func->local_count
+ + sizeof(uint16) * (type->param_count + func->local_count);
+#if WASM_ENABLE_FAST_INTERP != 0
+ size +=
+ func->code_compiled_size + sizeof(uint32) * func->const_cell_num;
+#endif
+ mem_conspn->functions_size += size;
+ }
+
+ mem_conspn->tables_size = sizeof(WASMTable) * module->table_count;
+ mem_conspn->memories_size = sizeof(WASMMemory) * module->memory_count;
+ mem_conspn->globals_size = sizeof(WASMGlobal) * module->global_count;
+ mem_conspn->exports_size = sizeof(WASMExport) * module->export_count;
+
+ mem_conspn->table_segs_size =
+ sizeof(WASMTableSeg) * module->table_seg_count;
+ for (i = 0; i < module->table_seg_count; i++) {
+ WASMTableSeg *table_seg = &module->table_segments[i];
+ mem_conspn->tables_size +=
+ sizeof(InitializerExpression *) * table_seg->value_count;
+ }
+
+ mem_conspn->data_segs_size = sizeof(WASMDataSeg *) * module->data_seg_count;
+ for (i = 0; i < module->data_seg_count; i++) {
+ mem_conspn->data_segs_size += sizeof(WASMDataSeg);
+ }
+
+ if (module->const_str_list) {
+ StringNode *node = module->const_str_list, *node_next;
+ while (node) {
+ node_next = node->next;
+ mem_conspn->const_strs_size +=
+ sizeof(StringNode) + strlen(node->str) + 1;
+ node = node_next;
+ }
+ }
+
+ mem_conspn->total_size += mem_conspn->module_struct_size;
+ mem_conspn->total_size += mem_conspn->types_size;
+ mem_conspn->total_size += mem_conspn->imports_size;
+ mem_conspn->total_size += mem_conspn->functions_size;
+ mem_conspn->total_size += mem_conspn->tables_size;
+ mem_conspn->total_size += mem_conspn->memories_size;
+ mem_conspn->total_size += mem_conspn->globals_size;
+ mem_conspn->total_size += mem_conspn->exports_size;
+ mem_conspn->total_size += mem_conspn->table_segs_size;
+ mem_conspn->total_size += mem_conspn->data_segs_size;
+ mem_conspn->total_size += mem_conspn->const_strs_size;
+}
+
+void
+wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
+ WASMModuleInstMemConsumption *mem_conspn)
+{
+ uint32 i;
+ uint64 size;
+
+ memset(mem_conspn, 0, sizeof(*mem_conspn));
+
+ mem_conspn->module_inst_struct_size = (uint8 *)module_inst->e
+ - (uint8 *)module_inst
+ + sizeof(WASMModuleInstanceExtra);
+
+ mem_conspn->memories_size =
+ sizeof(WASMMemoryInstance *) * module_inst->memory_count;
+ for (i = 0; i < module_inst->memory_count; i++) {
+ WASMMemoryInstance *memory = module_inst->memories[i];
+ size = memory->num_bytes_per_page * memory->cur_page_count;
+ mem_conspn->memories_size += size;
+ mem_conspn->app_heap_size += memory->heap_data_end - memory->heap_data;
+ /* size of app heap structure */
+ mem_conspn->memories_size += mem_allocator_get_heap_struct_size();
+ /* Module instance structures have been appended into the end of
+ module instance */
+ }
+
+ mem_conspn->tables_size =
+ sizeof(WASMTableInstance *) * module_inst->table_count;
+ /* Table instance structures and table elements have been appended into
+ the end of module instance */
+
+ mem_conspn->functions_size =
+ sizeof(WASMFunctionInstance) * module_inst->e->function_count;
+
+ mem_conspn->globals_size =
+ sizeof(WASMGlobalInstance) * module_inst->e->global_count;
+ /* Global data has been appended into the end of module instance */
+
+ mem_conspn->exports_size =
+ sizeof(WASMExportFuncInstance) * module_inst->export_func_count;
+
+ mem_conspn->total_size += mem_conspn->module_inst_struct_size;
+ mem_conspn->total_size += mem_conspn->memories_size;
+ mem_conspn->total_size += mem_conspn->functions_size;
+ mem_conspn->total_size += mem_conspn->tables_size;
+ mem_conspn->total_size += mem_conspn->globals_size;
+ mem_conspn->total_size += mem_conspn->exports_size;
+}
+#endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \
+ || (WASM_ENABLE_MEMORY_TRACING != 0) */
+
+#if WASM_ENABLE_DUMP_CALL_STACK != 0
+bool
+wasm_interp_create_call_stack(struct WASMExecEnv *exec_env)
+{
+ WASMModuleInstance *module_inst =
+ (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
+ WASMModule *module = module_inst->module;
+ WASMInterpFrame *first_frame,
+ *cur_frame = wasm_exec_env_get_cur_frame(exec_env);
+ uint32 n = 0;
+
+ /* count frames includes a function */
+ first_frame = cur_frame;
+ while (cur_frame) {
+ if (cur_frame->function) {
+ n++;
+ }
+ cur_frame = cur_frame->prev_frame;
+ }
+
+ /* release previous stack frames and create new ones */
+ destroy_c_api_frames(module_inst->frames);
+ if (!bh_vector_init(module_inst->frames, n, sizeof(WASMCApiFrame), false)) {
+ return false;
+ }
+
+ cur_frame = first_frame;
+ n = 0;
+
+ while (cur_frame) {
+ WASMCApiFrame frame = { 0 };
+ WASMFunctionInstance *func_inst = cur_frame->function;
+ const char *func_name = NULL;
+ const uint8 *func_code_base = NULL;
+ uint32 max_local_cell_num, max_stack_cell_num;
+ uint32 all_cell_num, lp_size;
+
+ if (!func_inst) {
+ cur_frame = cur_frame->prev_frame;
+ continue;
+ }
+
+ /* place holder, will overwrite it in wasm_c_api */
+ frame.instance = module_inst;
+ frame.module_offset = 0;
+ frame.func_index = (uint32)(func_inst - module_inst->e->functions);
+
+ func_code_base = wasm_get_func_code(func_inst);
+ if (!cur_frame->ip || !func_code_base) {
+ frame.func_offset = 0;
+ }
+ else {
+#if WASM_ENABLE_FAST_INTERP == 0
+ frame.func_offset = (uint32)(cur_frame->ip - module->load_addr);
+#else
+ frame.func_offset = (uint32)(cur_frame->ip - func_code_base);
+#endif
+ }
+
+ func_name = get_func_name_from_index(module_inst, frame.func_index);
+ frame.func_name_wp = func_name;
+
+ if (frame.func_index >= module->import_function_count) {
+ uint32 wasm_func_idx =
+ frame.func_index - module->import_function_count;
+ max_local_cell_num =
+ module->functions[wasm_func_idx]->param_cell_num
+ + module->functions[wasm_func_idx]->local_cell_num;
+ max_stack_cell_num =
+ module->functions[wasm_func_idx]->max_stack_cell_num;
+ all_cell_num = max_local_cell_num + max_stack_cell_num;
+#if WASM_ENABLE_FAST_INTERP != 0
+ all_cell_num += module->functions[wasm_func_idx]->const_cell_num;
+#endif
+ }
+ else {
+ WASMFuncType *func_type =
+ module->import_functions[frame.func_index].u.function.func_type;
+ max_local_cell_num =
+ func_type->param_cell_num > 2 ? func_type->param_cell_num : 2;
+ max_stack_cell_num = 0;
+ all_cell_num = max_local_cell_num + max_stack_cell_num;
+ }
+
+#if WASM_ENABLE_GC == 0
+ lp_size = all_cell_num * 4;
+#else
+ lp_size = align_uint(all_cell_num * 5, 4);
+#endif
+ if (lp_size > 0) {
+ if (!(frame.lp = wasm_runtime_malloc(lp_size))) {
+ destroy_c_api_frames(module_inst->frames);
+ return false;
+ }
+ bh_memcpy_s(frame.lp, lp_size, cur_frame->lp, lp_size);
+
+#if WASM_ENABLE_GC != 0
+#if WASM_ENABLE_FAST_INTERP == 0
+ frame.sp = frame.lp + (cur_frame->sp - cur_frame->lp);
+#else
+ /* for fast-interp, let frame sp point to the end of the frame */
+ frame.sp = frame.lp + all_cell_num;
+#endif
+ frame.frame_ref = (uint8 *)frame.lp
+ + (wasm_interp_get_frame_ref(cur_frame)
+ - (uint8 *)cur_frame->lp);
+#endif
+ }
+
+ if (!bh_vector_append(module_inst->frames, &frame)) {
+ if (frame.lp)
+ wasm_runtime_free(frame.lp);
+ destroy_c_api_frames(module_inst->frames);
+ return false;
+ }
+
+ cur_frame = cur_frame->prev_frame;
+ n++;
+ }
+
+ return true;
+}
+
+#define PRINT_OR_DUMP() \
+ do { \
+ total_len += \
+ wasm_runtime_dump_line_buf_impl(line_buf, print, &buf, &len); \
+ if ((!print) && buf && (len == 0)) { \
+ exception_unlock(module_inst); \
+ return total_len; \
+ } \
+ } while (0)
+
+uint32
+wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env, bool print, char *buf,
+ uint32 len)
+{
+ WASMModuleInstance *module_inst =
+ (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
+ uint32 n = 0, total_len = 0, total_frames;
+ /* reserve 256 bytes for line buffer, any line longer than 256 bytes
+ * will be truncated */
+ char line_buf[256];
+
+ if (!module_inst->frames) {
+ return 0;
+ }
+
+ total_frames = (uint32)bh_vector_size(module_inst->frames);
+ if (total_frames == 0) {
+ return 0;
+ }
+
+ exception_lock(module_inst);
+ snprintf(line_buf, sizeof(line_buf), "\n");
+ PRINT_OR_DUMP();
+
+ while (n < total_frames) {
+ WASMCApiFrame frame = { 0 };
+ uint32 line_length, i;
+
+ if (!bh_vector_get(module_inst->frames, n, &frame)) {
+ exception_unlock(module_inst);
+ return 0;
+ }
+
+#if WASM_ENABLE_FAST_JIT != 0
+ /* Fast JIT doesn't support committing ip (instruction pointer) yet */
+ if (module_inst->e->running_mode == Mode_Fast_JIT
+ || module_inst->e->running_mode == Mode_Multi_Tier_JIT) {
+ /* function name not exported, print number instead */
+ if (frame.func_name_wp == NULL) {
+ line_length = snprintf(line_buf, sizeof(line_buf),
+ "#%02" PRIu32 " $f%" PRIu32 "\n", n,
+ frame.func_index);
+ }
+ else {
+ line_length =
+ snprintf(line_buf, sizeof(line_buf), "#%02" PRIu32 " %s\n",
+ n, frame.func_name_wp);
+ }
+ }
+ else
+#endif
+ {
+ /* function name not exported, print number instead */
+ if (frame.func_name_wp == NULL) {
+ line_length =
+ snprintf(line_buf, sizeof(line_buf),
+ "#%02" PRIu32 ": 0x%04x - $f%" PRIu32 "\n", n,
+ frame.func_offset, frame.func_index);
+ }
+ else {
+ line_length = snprintf(line_buf, sizeof(line_buf),
+ "#%02" PRIu32 ": 0x%04x - %s\n", n,
+ frame.func_offset, frame.func_name_wp);
+ }
+ }
+
+ if (line_length >= sizeof(line_buf)) {
+ uint32 line_buffer_len = sizeof(line_buf);
+ /* If line too long, ensure the last character is '\n' */
+ for (i = line_buffer_len - 5; i < line_buffer_len - 2; i++) {
+ line_buf[i] = '.';
+ }
+ line_buf[line_buffer_len - 2] = '\n';
+ }
+
+ PRINT_OR_DUMP();
+
+ n++;
+ }
+ snprintf(line_buf, sizeof(line_buf), "\n");
+ PRINT_OR_DUMP();
+ exception_unlock(module_inst);
+
+ return total_len + 1;
+}
+#endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
+
+#if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
+ || WASM_ENABLE_WAMR_COMPILER != 0
+void
+jit_set_exception_with_id(WASMModuleInstance *module_inst, uint32 id)
+{
+ if (id != EXCE_ALREADY_THROWN)
+ wasm_set_exception_with_id(module_inst, id);
+#ifdef OS_ENABLE_HW_BOUND_CHECK
+ wasm_runtime_access_exce_check_guard_page();
+#endif
+}
+
+bool
+jit_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
+ uint64 app_buf_addr, uint64 app_buf_size,
+ void **p_native_addr)
+{
+ bool ret = wasm_check_app_addr_and_convert(
+ module_inst, is_str, app_buf_addr, app_buf_size, p_native_addr);
+
+#ifdef OS_ENABLE_HW_BOUND_CHECK
+ if (!ret)
+ wasm_runtime_access_exce_check_guard_page();
+#endif
+
+ return ret;
+}
+#endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
+ || WASM_ENABLE_WAMR_COMPILER != 0 */
+
+#if WASM_ENABLE_FAST_JIT != 0
+bool
+fast_jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
+ uint32 type_idx, uint32 argc, uint32 *argv)
+{
+ return call_indirect(exec_env, tbl_idx, elem_idx, argc, argv, true,
+ type_idx);
+}
+#endif /* end of WASM_ENABLE_FAST_JIT != 0 */
+
+#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
+
+bool
+llvm_jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
+ uint32 argc, uint32 *argv)
+{
+ bool ret;
+
+ bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
+
+ ret = call_indirect(exec_env, tbl_idx, elem_idx, argc, argv, false, 0);
+#ifdef OS_ENABLE_HW_BOUND_CHECK
+ if (!ret)
+ wasm_runtime_access_exce_check_guard_page();
+#endif
+ return ret;
+}
+
+bool
+llvm_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
+ uint32 *argv)
+{
+ WASMModuleInstance *module_inst;
+ WASMModule *module;
+ uint32 *func_type_indexes;
+ uint32 func_type_idx;
+ WASMFuncType *func_type;
+ void *func_ptr;
+ WASMFunctionImport *import_func;
+ CApiFuncImport *c_api_func_import = NULL;
+ const char *signature;
+ void *attachment;
+ char buf[96];
+ bool ret = false;
+
+ bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
+
+ module_inst = (WASMModuleInstance *)wasm_runtime_get_module_inst(exec_env);
+ module = module_inst->module;
+ func_type_indexes = module_inst->func_type_indexes;
+ func_type_idx = func_type_indexes[func_idx];
+ func_type = (WASMFuncType *)module->types[func_type_idx];
+ func_ptr = module_inst->func_ptrs[func_idx];
+
+ bh_assert(func_idx < module->import_function_count);
+
+ import_func = &module->import_functions[func_idx].u.function;
+ if (import_func->call_conv_wasm_c_api) {
+ if (module_inst->c_api_func_imports) {
+ c_api_func_import = module_inst->c_api_func_imports + func_idx;
+ func_ptr = c_api_func_import->func_ptr_linked;
+ }
+ else {
+ c_api_func_import = NULL;
+ func_ptr = NULL;
+ }
+ }
+
+ if (!func_ptr) {
+ snprintf(buf, sizeof(buf),
+ "failed to call unlinked import function (%s, %s)",
+ import_func->module_name, import_func->field_name);
+ wasm_set_exception(module_inst, buf);
+ goto fail;
+ }
+
+ attachment = import_func->attachment;
+ if (import_func->call_conv_wasm_c_api) {
+ ret = wasm_runtime_invoke_c_api_native(
+ (WASMModuleInstanceCommon *)module_inst, func_ptr, func_type, argc,
+ argv, c_api_func_import->with_env_arg, c_api_func_import->env_arg);
+ }
+ else if (!import_func->call_conv_raw) {
+ signature = import_func->signature;
+ ret =
+ wasm_runtime_invoke_native(exec_env, func_ptr, func_type, signature,
+ attachment, argv, argc, argv);
+ }
+ else {
+ signature = import_func->signature;
+ ret = wasm_runtime_invoke_native_raw(exec_env, func_ptr, func_type,
+ signature, attachment, argv, argc,
+ argv);
+ }
+
+fail:
+#ifdef OS_ENABLE_HW_BOUND_CHECK
+ if (!ret)
+ wasm_runtime_access_exce_check_guard_page();
+#endif
+ return ret;
+}
+
+#if WASM_ENABLE_BULK_MEMORY != 0
+bool
+llvm_jit_memory_init(WASMModuleInstance *module_inst, uint32 seg_index,
+ uint32 offset, uint32 len, size_t dst)
+{
+ WASMMemoryInstance *memory_inst;
+ WASMModule *module;
+ uint8 *data;
+ uint8 *maddr;
+ uint64 seg_len;
+
+ bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
+
+ memory_inst = wasm_get_default_memory(module_inst);
+
+ if (bh_bitmap_get_bit(module_inst->e->common.data_dropped, seg_index)) {
+ seg_len = 0;
+ data = NULL;
+ }
+ else {
+ module = module_inst->module;
+ seg_len = module->data_segments[seg_index]->data_length;
+ data = module->data_segments[seg_index]->data;
+ }
+
+ if (!wasm_runtime_validate_app_addr((WASMModuleInstanceCommon *)module_inst,
+ (uint64)dst, (uint64)len))
+ return false;
+
+ if ((uint64)offset + (uint64)len > seg_len) {
+ wasm_set_exception(module_inst, "out of bounds memory access");
+ return false;
+ }
+
+ maddr = wasm_runtime_addr_app_to_native(
+ (WASMModuleInstanceCommon *)module_inst, (uint64)dst);
+
+ SHARED_MEMORY_LOCK(memory_inst);
+ bh_memcpy_s(maddr, CLAMP_U64_TO_U32(memory_inst->memory_data_size - dst),
+ data + offset, len);
+ SHARED_MEMORY_UNLOCK(memory_inst);
+ return true;
+}
+
+bool
+llvm_jit_data_drop(WASMModuleInstance *module_inst, uint32 seg_index)
+{
+ bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
+
+ bh_bitmap_set_bit(module_inst->e->common.data_dropped, seg_index);
+ /* Currently we can't free the dropped data segment
+ as they are stored in wasm bytecode */
+ return true;
+}
+#endif /* end of WASM_ENABLE_BULK_MEMORY != 0 */
+
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+void
+llvm_jit_drop_table_seg(WASMModuleInstance *module_inst, uint32 tbl_seg_idx)
+{
+ bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
+ bh_bitmap_set_bit(module_inst->e->common.elem_dropped, tbl_seg_idx);
+}
+
+void
+llvm_jit_table_init(WASMModuleInstance *module_inst, uint32 tbl_idx,
+ uint32 tbl_seg_idx, uint32 length, uint32 src_offset,
+ uint32 dst_offset)
+{
+ WASMTableInstance *tbl_inst;
+ WASMTableSeg *tbl_seg;
+ table_elem_type_t *table_elems;
+ InitializerExpression *tbl_seg_init_values = NULL, *init_values;
+ uint32 i, tbl_seg_len = 0;
+#if WASM_ENABLE_GC != 0
+ void *func_obj;
+#endif
+
+ bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
+
+ tbl_inst = wasm_get_table_inst(module_inst, tbl_idx);
+ tbl_seg = module_inst->module->table_segments + tbl_seg_idx;
+
+ bh_assert(tbl_inst);
+ bh_assert(tbl_seg);
+
+ if (!bh_bitmap_get_bit(module_inst->e->common.elem_dropped, tbl_seg_idx)) {
+ /* table segment isn't dropped */
+ tbl_seg_init_values = tbl_seg->init_values;
+ tbl_seg_len = tbl_seg->value_count;
+ }
+
+ if (offset_len_out_of_bounds(src_offset, length, tbl_seg_len)
+ || offset_len_out_of_bounds(dst_offset, length, tbl_inst->cur_size)) {
+ jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
+ return;
+ }
+
+ if (!length) {
+ return;
+ }
+
+ table_elems = tbl_inst->elems + dst_offset;
+ init_values = tbl_seg_init_values + src_offset;
+
+ for (i = 0; i < length; i++) {
+#if WASM_ENABLE_GC != 0
+ /* UINT32_MAX indicates that it is a null ref */
+ if (init_values[i].u.ref_index != UINT32_MAX) {
+ if (!(func_obj = wasm_create_func_obj(module_inst,
+ init_values[i].u.ref_index,
+ true, NULL, 0))) {
+ wasm_set_exception(module_inst, "null function object");
+ return;
+ }
+ table_elems[i] = func_obj;
+ }
+ else {
+ table_elems[i] = NULL_REF;
+ }
+#else
+ table_elems[i] = init_values[i].u.ref_index;
+#endif
+ }
+}
+
+void
+llvm_jit_table_copy(WASMModuleInstance *module_inst, uint32 src_tbl_idx,
+ uint32 dst_tbl_idx, uint32 length, uint32 src_offset,
+ uint32 dst_offset)
+{
+ WASMTableInstance *src_tbl_inst;
+ WASMTableInstance *dst_tbl_inst;
+
+ bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
+
+ src_tbl_inst = wasm_get_table_inst(module_inst, src_tbl_idx);
+ dst_tbl_inst = wasm_get_table_inst(module_inst, dst_tbl_idx);
+ bh_assert(src_tbl_inst);
+ bh_assert(dst_tbl_inst);
+
+ if (offset_len_out_of_bounds(dst_offset, length, dst_tbl_inst->cur_size)
+ || offset_len_out_of_bounds(src_offset, length,
+ src_tbl_inst->cur_size)) {
+ jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
+ return;
+ }
+
+ /* if src_offset >= dst_offset, copy from front to back */
+ /* if src_offset < dst_offset, copy from back to front */
+ /* merge all together */
+ bh_memmove_s((uint8 *)dst_tbl_inst + offsetof(WASMTableInstance, elems)
+ + sizeof(table_elem_type_t) * dst_offset,
+ (uint32)sizeof(table_elem_type_t)
+ * (dst_tbl_inst->cur_size - dst_offset),
+ (uint8 *)src_tbl_inst + offsetof(WASMTableInstance, elems)
+ + sizeof(table_elem_type_t) * src_offset,
+ (uint32)sizeof(table_elem_type_t) * length);
+}
+
+void
+llvm_jit_table_fill(WASMModuleInstance *module_inst, uint32 tbl_idx,
+ uint32 length, uintptr_t val, uint32 data_offset)
+{
+ WASMTableInstance *tbl_inst;
+
+ bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
+
+ tbl_inst = wasm_get_table_inst(module_inst, tbl_idx);
+ bh_assert(tbl_inst);
+
+ if (offset_len_out_of_bounds(data_offset, length, tbl_inst->cur_size)) {
+ jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
+ return;
+ }
+
+ for (; length != 0; data_offset++, length--) {
+ tbl_inst->elems[data_offset] = (table_elem_type_t)val;
+ }
+}
+
+uint32
+llvm_jit_table_grow(WASMModuleInstance *module_inst, uint32 tbl_idx,
+ uint32 inc_size, uintptr_t init_val)
+{
+ WASMTableInstance *tbl_inst;
+ uint32 i, orig_size, total_size;
+
+ bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
+
+ tbl_inst = wasm_get_table_inst(module_inst, tbl_idx);
+ if (!tbl_inst) {
+ return (uint32)-1;
+ }
+
+ orig_size = tbl_inst->cur_size;
+
+ if (!inc_size) {
+ return orig_size;
+ }
+
+ if (tbl_inst->cur_size > UINT32_MAX - inc_size) { /* integer overflow */
+#if WASM_ENABLE_SPEC_TEST == 0
+ LOG_WARNING("table grow (%" PRIu32 "-> %" PRIu32
+ ") failed because of integer overflow",
+ tbl_inst->cur_size, inc_size);
+#endif
+ return (uint32)-1;
+ }
+
+ total_size = tbl_inst->cur_size + inc_size;
+ if (total_size > tbl_inst->max_size) {
+#if WASM_ENABLE_SPEC_TEST == 0
+ LOG_WARNING("table grow (%" PRIu32 "-> %" PRIu32
+ ") failed because of over max size",
+ tbl_inst->cur_size, inc_size);
+#endif
+ return (uint32)-1;
+ }
+
+ /* fill in */
+ for (i = 0; i < inc_size; ++i) {
+ tbl_inst->elems[tbl_inst->cur_size + i] = (table_elem_type_t)init_val;
+ }
+
+ tbl_inst->cur_size = total_size;
+ return orig_size;
+}
+#endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
+
+#if WASM_ENABLE_GC != 0
+void *
+llvm_jit_create_func_obj(WASMModuleInstance *module_inst, uint32 func_idx,
+ bool throw_exce, char *error_buf,
+ uint32 error_buf_size)
+{
+ bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
+
+ return wasm_create_func_obj(module_inst, func_idx, throw_exce, error_buf,
+ error_buf_size);
+}
+
+bool
+llvm_jit_obj_is_instance_of(WASMModuleInstance *module_inst,
+ WASMObjectRef gc_obj, uint32 type_index)
+{
+ WASMModule *module = module_inst->module;
+ WASMType **types = module->types;
+ uint32 type_count = module->type_count;
+
+ return wasm_obj_is_instance_of(gc_obj, type_index, types, type_count);
+}
+
+bool
+llvm_jit_func_type_is_super_of(WASMModuleInstance *module_inst,
+ uint32 type_idx1, uint32 type_idx2)
+{
+ WASMModule *module = module_inst->module;
+ WASMType **types = module->types;
+
+ if (type_idx1 == type_idx2)
+ return true;
+
+ bh_assert(types[type_idx1]->type_flag == WASM_TYPE_FUNC);
+ bh_assert(types[type_idx2]->type_flag == WASM_TYPE_FUNC);
+ return wasm_func_type_is_super_of((WASMFuncType *)types[type_idx1],
+ (WASMFuncType *)types[type_idx2]);
+}
+
+WASMRttTypeRef
+llvm_jit_rtt_type_new(WASMModuleInstance *module_inst, uint32 type_index)
+{
+ WASMModule *module = module_inst->module;
+ WASMType *defined_type = module->types[type_index];
+ WASMRttType **rtt_types = module->rtt_types;
+ uint32 rtt_type_count = module->type_count;
+ korp_mutex *rtt_type_lock = &module->rtt_type_lock;
+
+ return wasm_rtt_type_new(defined_type, type_index, rtt_types,
+ rtt_type_count, rtt_type_lock);
+}
+
+bool
+llvm_array_init_with_data(WASMModuleInstance *module_inst, uint32 seg_index,
+ uint32 data_seg_offset, WASMArrayObjectRef array_obj,
+ uint32 elem_size, uint32 array_len)
+{
+ WASMModule *wasm_module = module_inst->module;
+ WASMDataSeg *data_seg;
+ uint8 *array_elem_base;
+ uint64 total_size;
+
+ data_seg = wasm_module->data_segments[seg_index];
+ total_size = (int64)elem_size * array_len;
+
+ if (data_seg_offset >= data_seg->data_length
+ || total_size > data_seg->data_length - data_seg_offset) {
+ wasm_set_exception(module_inst, "out of bounds memory access");
+ return false;
+ }
+
+ array_elem_base = (uint8 *)wasm_array_obj_first_elem_addr(array_obj);
+ bh_memcpy_s(array_elem_base, (uint32)total_size,
+ data_seg->data + data_seg_offset, (uint32)total_size);
+
+ return true;
+}
+#endif /* end of WASM_ENABLE_GC != 0 */
+
+#endif /* end of WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0 */
+
+#if WASM_ENABLE_LIBC_WASI != 0 && WASM_ENABLE_MULTI_MODULE != 0
+void
+wasm_propagate_wasi_args(WASMModule *module)
+{
+ if (!module->import_count)
+ return;
+
+ bh_assert(&module->import_module_list_head);
+
+ WASMRegisteredModule *node =
+ bh_list_first_elem(&module->import_module_list_head);
+ while (node) {
+ WASIArguments *wasi_args_impt_mod =
+ &((WASMModule *)(node->module))->wasi_args;
+ bh_assert(wasi_args_impt_mod);
+
+ bh_memcpy_s(wasi_args_impt_mod, sizeof(WASIArguments),
+ &module->wasi_args, sizeof(WASIArguments));
+ node = bh_list_elem_next(node);
+ }
+}
+#endif
+
+bool
+wasm_check_utf8_str(const uint8 *str, uint32 len)
+{
+ /* 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;
+
+ while (p < p_end) {
+ chr = *p;
+
+ if (chr == 0) {
+ LOG_WARNING(
+ "LIMITATION: a string which contains '\\00' is unsupported");
+ return false;
+ }
+ else if (chr < 0x80) {
+ p++;
+ }
+ else if (chr >= 0xC2 && chr <= 0xDF && p + 1 < p_end) {
+ if (p[1] < 0x80 || p[1] > 0xBF) {
+ return false;
+ }
+ p += 2;
+ }
+ else if (chr >= 0xE0 && chr <= 0xEF && p + 2 < p_end) {
+ if (chr == 0xE0) {
+ if (p[1] < 0xA0 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
+ return false;
+ }
+ }
+ else if (chr == 0xED) {
+ if (p[1] < 0x80 || p[1] > 0x9F || p[2] < 0x80 || p[2] > 0xBF) {
+ return false;
+ }
+ }
+ else { /* chr >= 0xE1 && chr <= 0xEF */
+ if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
+ return false;
+ }
+ }
+ p += 3;
+ }
+ else if (chr >= 0xF0 && chr <= 0xF4 && p + 3 < p_end) {
+ if (chr == 0xF0) {
+ if (p[1] < 0x90 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
+ || p[3] < 0x80 || p[3] > 0xBF) {
+ return false;
+ }
+ }
+ else if (chr <= 0xF3) { /* and also chr >= 0xF1 */
+ if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
+ || p[3] < 0x80 || p[3] > 0xBF) {
+ return false;
+ }
+ }
+ else { /* chr == 0xF4 */
+ if (p[1] < 0x80 || p[1] > 0x8F || p[2] < 0x80 || p[2] > 0xBF
+ || p[3] < 0x80 || p[3] > 0xBF) {
+ return false;
+ }
+ }
+ p += 4;
+ }
+ else {
+ return false;
+ }
+ }
+ return (p == p_end);
+}
+
+char *
+wasm_const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module,
+ bool is_load_from_file_buf, char *error_buf,
+ uint32 error_buf_size)
+{
+ StringNode *node, *node_next;
+
+ if (!wasm_check_utf8_str(str, len)) {
+ set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
+ return NULL;
+ }
+
+ 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;
+ }
+
+ /* Search const str list */
+ node = module->const_str_list;
+ while (node) {
+ node_next = node->next;
+ if (strlen(node->str) == len && !memcmp(node->str, str, len))
+ break;
+ node = node_next;
+ }
+
+ if (node) {
+ return node->str;
+ }
+
+ if (!(node = runtime_malloc(sizeof(StringNode) + len + 1, error_buf,
+ error_buf_size))) {
+ return NULL;
+ }
+
+ node->str = ((char *)node) + sizeof(StringNode);
+ bh_memcpy_s(node->str, len + 1, str, len);
+ node->str[len] = '\0';
+
+ if (!module->const_str_list) {
+ /* set as head */
+ module->const_str_list = node;
+ node->next = NULL;
+ }
+ else {
+ /* insert it */
+ node->next = module->const_str_list;
+ module->const_str_list = node;
+ }
+
+ return node->str;
+}
+
+bool
+wasm_set_module_name(WASMModule *module, const char *name, char *error_buf,
+ uint32_t error_buf_size)
+{
+ if (!name)
+ return false;
+
+ module->name =
+ wasm_const_str_list_insert((const uint8 *)name, (uint32)strlen(name),
+ module, false, error_buf, error_buf_size);
+ return module->name != NULL;
+}
+
+const char *
+wasm_get_module_name(WASMModule *module)
+{
+ return module->name;
+}
diff --git a/lib/wamr/core/iwasm/interpreter/wasm_runtime.h b/lib/wamr/core/iwasm/interpreter/wasm_runtime.h
new file mode 100644
index 0000000..00e9ad1
--- /dev/null
+++ b/lib/wamr/core/iwasm/interpreter/wasm_runtime.h
@@ -0,0 +1,896 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#ifndef _WASM_RUNTIME_H
+#define _WASM_RUNTIME_H
+
+#include "wasm.h"
+#include "bh_atomic.h"
+#include "bh_bitmap.h"
+#include "bh_hashmap.h"
+#include "../common/wasm_runtime_common.h"
+#include "../common/wasm_exec_env.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define EXCEPTION_BUF_LEN 128
+
+typedef struct WASMModuleInstance WASMModuleInstance;
+typedef struct WASMFunctionInstance WASMFunctionInstance;
+typedef struct WASMMemoryInstance WASMMemoryInstance;
+typedef struct WASMTableInstance WASMTableInstance;
+typedef struct WASMGlobalInstance WASMGlobalInstance;
+#if WASM_ENABLE_TAGS != 0
+typedef struct WASMTagInstance WASMTagInstance;
+#endif
+
+/**
+ * When LLVM JIT, WAMR compiler or AOT is enabled, we should ensure that
+ * some offsets of the same field in the interpreter module instance and
+ * aot module instance are the same, so that the LLVM JITed/AOTed code
+ * can smoothly access the interpreter module instance.
+ * Same for the memory instance and table instance.
+ * We use the macro DefPointer to define some related pointer fields.
+ */
+#if (WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0 \
+ || WASM_ENABLE_AOT != 0) \
+ && UINTPTR_MAX == UINT32_MAX
+/* Add u32 padding if LLVM JIT, WAMR compiler or AOT is enabled on
+ 32-bit platform */
+#define DefPointer(type, field) \
+ type field; \
+ uint32 field##_padding
+#else
+#define DefPointer(type, field) type field
+#endif
+
+typedef enum WASMExceptionID {
+ EXCE_UNREACHABLE = 0,
+ EXCE_OUT_OF_MEMORY,
+ EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS,
+ EXCE_INTEGER_OVERFLOW,
+ EXCE_INTEGER_DIVIDE_BY_ZERO,
+ EXCE_INVALID_CONVERSION_TO_INTEGER,
+ EXCE_INVALID_FUNCTION_TYPE_INDEX,
+ EXCE_INVALID_FUNCTION_INDEX,
+ EXCE_UNDEFINED_ELEMENT,
+ EXCE_UNINITIALIZED_ELEMENT,
+ EXCE_CALL_UNLINKED_IMPORT_FUNC,
+ EXCE_NATIVE_STACK_OVERFLOW,
+ EXCE_UNALIGNED_ATOMIC,
+ EXCE_AUX_STACK_OVERFLOW,
+ EXCE_AUX_STACK_UNDERFLOW,
+ EXCE_OUT_OF_BOUNDS_TABLE_ACCESS,
+ EXCE_OPERAND_STACK_OVERFLOW,
+ EXCE_FAILED_TO_COMPILE_FAST_JIT_FUNC,
+ /* GC related exceptions */
+ EXCE_NULL_FUNC_OBJ,
+ EXCE_NULL_STRUCT_OBJ,
+ EXCE_NULL_ARRAY_OBJ,
+ EXCE_NULL_I31_OBJ,
+ EXCE_NULL_REFERENCE,
+ EXCE_FAILED_TO_CREATE_RTT_TYPE,
+ EXCE_FAILED_TO_CREATE_STRUCT_OBJ,
+ EXCE_FAILED_TO_CREATE_ARRAY_OBJ,
+ EXCE_FAILED_TO_CREATE_EXTERNREF_OBJ,
+ EXCE_CAST_FAILURE,
+ EXCE_ARRAY_IDX_OOB,
+ EXCE_FAILED_TO_CREATE_STRING,
+ EXCE_FAILED_TO_CREATE_STRINGREF,
+ EXCE_FAILED_TO_CREATE_STRINGVIEW,
+ EXCE_FAILED_TO_ENCODE_STRING,
+ EXCE_ALREADY_THROWN,
+ EXCE_NUM,
+} WASMExceptionID;
+
+typedef union {
+ uint64 u64;
+ uint32 u32[2];
+} MemBound;
+
+typedef struct WASMSharedHeap {
+ struct WASMSharedHeap *next;
+ void *heap_handle;
+ uint8 *base_addr;
+ uint64 size;
+ uint64 start_off_mem64;
+ uint64 start_off_mem32;
+} WASMSharedHeap;
+
+struct WASMMemoryInstance {
+ /* Module type */
+ uint32 module_type;
+
+ /* Whether the memory is shared */
+ uint8 is_shared_memory;
+
+ /* Whether the memory has 64-bit memory addresses */
+ uint8 is_memory64;
+
+ /* Reference count of the memory instance:
+ 0: non-shared memory, > 0: shared memory */
+ bh_atomic_16_t ref_count;
+
+ /* Four-byte paddings to ensure the layout of WASMMemoryInstance is the same
+ * in both 64-bit and 32-bit */
+ uint8 _paddings[4];
+
+ /* Number bytes per page */
+ uint32 num_bytes_per_page;
+ /* Current page count */
+ uint32 cur_page_count;
+ /* Maximum page count */
+ uint32 max_page_count;
+ /* Memory data size */
+ uint64 memory_data_size;
+ /**
+ * Memory data begin address, Note:
+ * the app-heap might be inserted in to the linear memory,
+ * when memory is re-allocated, the heap data and memory data
+ * must be copied to new memory also
+ */
+ DefPointer(uint8 *, memory_data);
+ /* Memory data end address */
+ DefPointer(uint8 *, memory_data_end);
+
+ /* Heap data base address */
+ DefPointer(uint8 *, heap_data);
+ /* Heap data end address */
+ DefPointer(uint8 *, heap_data_end);
+ /* The heap created */
+ DefPointer(void *, heap_handle);
+ /* TODO: use it to replace the g_shared_memory_lock */
+ DefPointer(korp_mutex *, memory_lock);
+
+#if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
+ || WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_AOT != 0
+ MemBound mem_bound_check_1byte;
+ MemBound mem_bound_check_2bytes;
+ MemBound mem_bound_check_4bytes;
+ MemBound mem_bound_check_8bytes;
+ MemBound mem_bound_check_16bytes;
+#endif
+};
+
+/* WASMTableInstance is used to represent table instance in
+ * runtime, to compute the table element address with index
+ * we need to know the element type and the element ref type.
+ * For pointer type, it's 32-bit or 64-bit, align up to 8 bytes
+ * to simplify the computation.
+ * And each struct member should be 4-byte or 8-byte aligned.
+ */
+struct WASMTableInstance {
+ /* The element type */
+ uint8 elem_type;
+ uint8 is_table64;
+ uint8 __padding__[6];
+ union {
+#if WASM_ENABLE_GC != 0
+ WASMRefType *elem_ref_type;
+#endif
+ uint64 __padding__;
+ } elem_ref_type;
+ /* Current size */
+ uint32 cur_size;
+ /* Maximum size */
+ uint32 max_size;
+ /* Table elements */
+ table_elem_type_t elems[1];
+};
+
+struct WASMGlobalInstance {
+ /* value type, VALUE_TYPE_I32/I64/F32/F64 */
+ uint8 type;
+ /* mutable or constant */
+ bool is_mutable;
+ /* data offset to the address of initial_value, started from the end of
+ * WASMMemoryInstance(start of WASMGlobalInstance)*/
+ uint32 data_offset;
+ /* initial value */
+ WASMValue initial_value;
+#if WASM_ENABLE_GC != 0
+ WASMRefType *ref_type;
+#endif
+#if WASM_ENABLE_MULTI_MODULE != 0
+ /* just for import, keep the reference here */
+ WASMModuleInstance *import_module_inst;
+ WASMGlobalInstance *import_global_inst;
+#endif
+};
+
+struct WASMFunctionInstance {
+ /* whether it is import function or WASM function */
+ bool is_import_func;
+ /* parameter count */
+ uint16 param_count;
+ /* local variable count, 0 for import function */
+ uint16 local_count;
+ /* cell num of parameters */
+ uint16 param_cell_num;
+ /* cell num of return type */
+ uint16 ret_cell_num;
+ /* cell num of local variables, 0 for import function */
+ uint16 local_cell_num;
+#if WASM_ENABLE_FAST_INTERP != 0
+ /* cell num of consts */
+ uint16 const_cell_num;
+#endif
+ uint16 *local_offsets;
+ /* parameter types */
+ uint8 *param_types;
+ /* local types, NULL for import function */
+ uint8 *local_types;
+ union {
+ WASMFunctionImport *func_import;
+ WASMFunction *func;
+ } u;
+#if WASM_ENABLE_MULTI_MODULE != 0
+ WASMModuleInstance *import_module_inst;
+ WASMFunctionInstance *import_func_inst;
+#endif
+#if WASM_ENABLE_PERF_PROFILING != 0
+ /* total execution time */
+ uint64 total_exec_time;
+ /* total execution count */
+ uint32 total_exec_cnt;
+ /* children execution time */
+ uint64 children_exec_time;
+#endif
+};
+
+#if WASM_ENABLE_TAGS != 0
+struct WASMTagInstance {
+ bool is_import_tag;
+ /* tag attribute */
+ uint8 attribute;
+ /* tag type index */
+ uint32 type;
+ union {
+ WASMTagImport *tag_import;
+ WASMTag *tag;
+ } u;
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+ WASMModuleInstance *import_module_inst;
+ WASMTagInstance *import_tag_inst;
+#endif
+};
+#endif
+
+#if WASM_ENABLE_EXCE_HANDLING != 0
+#define INVALID_TAGINDEX ((uint32)0xFFFFFFFF)
+#define SET_INVALID_TAGINDEX(tag) (tag = INVALID_TAGINDEX)
+#define IS_INVALID_TAGINDEX(tag) ((tag & INVALID_TAGINDEX) == INVALID_TAGINDEX)
+#endif
+typedef struct WASMExportFuncInstance {
+ char *name;
+ WASMFunctionInstance *function;
+} WASMExportFuncInstance;
+
+typedef struct WASMExportGlobInstance {
+ char *name;
+ WASMGlobalInstance *global;
+} WASMExportGlobInstance;
+
+typedef struct WASMExportTabInstance {
+ char *name;
+ WASMTableInstance *table;
+} WASMExportTabInstance;
+
+typedef struct WASMExportMemInstance {
+ char *name;
+ WASMMemoryInstance *memory;
+} WASMExportMemInstance;
+
+#if WASM_ENABLE_TAGS != 0
+typedef struct WASMExportTagInstance {
+ char *name;
+ WASMTagInstance *tag;
+} WASMExportTagInstance;
+#endif
+
+/* wasm-c-api import function info */
+typedef struct CApiFuncImport {
+ /* host func pointer after linked */
+ void *func_ptr_linked;
+ /* whether the host func has env argument */
+ bool with_env_arg;
+ /* the env argument of the host func */
+ void *env_arg;
+} CApiFuncImport;
+
+/* The common part of WASMModuleInstanceExtra and AOTModuleInstanceExtra */
+typedef struct WASMModuleInstanceExtraCommon {
+#if WASM_ENABLE_MODULE_INST_CONTEXT != 0
+ void *contexts[WASM_MAX_INSTANCE_CONTEXTS];
+#endif
+#if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
+ /* Disable bounds checks or not */
+ bool disable_bounds_checks;
+#endif
+#if WASM_ENABLE_BULK_MEMORY != 0
+ bh_bitmap *data_dropped;
+#endif
+#if WASM_ENABLE_REF_TYPES != 0
+ bh_bitmap *elem_dropped;
+#endif
+
+#if WASM_ENABLE_GC != 0
+ /* The gc heap memory pool */
+ uint8 *gc_heap_pool;
+ /* The gc heap created */
+ void *gc_heap_handle;
+#endif
+} WASMModuleInstanceExtraCommon;
+
+/* Extra info of WASM module instance for interpreter/jit mode */
+typedef struct WASMModuleInstanceExtra {
+ WASMModuleInstanceExtraCommon common;
+
+ WASMGlobalInstance *globals;
+ WASMFunctionInstance *functions;
+
+ uint32 global_count;
+ uint32 function_count;
+
+ WASMFunctionInstance *start_function;
+ WASMFunctionInstance *malloc_function;
+ WASMFunctionInstance *free_function;
+ WASMFunctionInstance *retain_function;
+
+ RunningMode running_mode;
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+ bh_list sub_module_inst_list_head;
+ bh_list *sub_module_inst_list;
+ /* linked table instances of import table instances */
+ WASMTableInstance **table_insts_linked;
+#endif
+
+#if WASM_ENABLE_TAGS != 0
+ uint32 tag_count;
+ uint32 export_tag_count;
+ WASMTagInstance *tags;
+ WASMExportTagInstance *export_tags;
+ void **import_tag_ptrs;
+#endif
+
+#if WASM_ENABLE_MEMORY_PROFILING != 0
+ uint32 max_aux_stack_used;
+#endif
+
+#if WASM_ENABLE_SHARED_HEAP != 0
+ WASMSharedHeap *shared_heap;
+#if WASM_ENABLE_JIT != 0
+ /*
+ * Adjusted shared heap based addr to simple the calculation
+ * in the aot code. The value is:
+ * shared_heap->base_addr - shared_heap->start_off
+ */
+ uint8 *shared_heap_base_addr_adj;
+ MemBound shared_heap_start_off;
+#endif
+#endif
+
+#if WASM_ENABLE_DEBUG_INTERP != 0 \
+ || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
+ && WASM_ENABLE_LAZY_JIT != 0)
+ WASMModuleInstance *next;
+#endif
+} WASMModuleInstanceExtra;
+
+struct AOTFuncPerfProfInfo;
+
+struct WASMModuleInstance {
+ /* Module instance type, for module instance loaded from
+ WASM bytecode binary, this field is Wasm_Module_Bytecode;
+ for module instance loaded from AOT file, this field is
+ Wasm_Module_AoT, and this structure should be treated as
+ AOTModuleInstance structure. */
+ uint32 module_type;
+
+ uint32 memory_count;
+ DefPointer(WASMMemoryInstance **, memories);
+
+ /* global and table info */
+ uint32 global_data_size;
+ uint32 table_count;
+ DefPointer(uint8 *, global_data);
+ /* For AOTModuleInstance, it denotes `AOTTableInstance *` */
+ DefPointer(WASMTableInstance **, tables);
+
+ /* import func ptrs + llvm jit func ptrs */
+ DefPointer(void **, func_ptrs);
+
+ /* function type indexes */
+ DefPointer(uint32 *, func_type_indexes);
+
+ uint32 export_func_count;
+ uint32 export_global_count;
+ uint32 export_memory_count;
+ uint32 export_table_count;
+ /* For AOTModuleInstance, it denotes `AOTFunctionInstance *` */
+ DefPointer(WASMExportFuncInstance *, export_functions);
+ DefPointer(WASMExportGlobInstance *, export_globals);
+ DefPointer(WASMExportMemInstance *, export_memories);
+ DefPointer(WASMExportTabInstance *, export_tables);
+
+ /* The exception buffer of wasm interpreter for current thread. */
+ char cur_exception[EXCEPTION_BUF_LEN];
+
+ /* The WASM module or AOT module, for AOTModuleInstance,
+ it denotes `AOTModule *` */
+ DefPointer(WASMModule *, module);
+
+ DefPointer(WASMExecEnv *, exec_env_singleton);
+ /* Array of function pointers to import functions,
+ not available in AOTModuleInstance */
+ DefPointer(void **, import_func_ptrs);
+ /* Array of function pointers to fast jit functions,
+ not available in AOTModuleInstance:
+ Only when the multi-tier JIT macros are all enabled and the running
+ mode of current module instance is set to Mode_Fast_JIT, runtime
+ will allocate new memory for it, otherwise it always points to the
+ module->fast_jit_func_ptrs */
+ DefPointer(void **, fast_jit_func_ptrs);
+ /* The custom data that can be set/get by wasm_{get|set}_custom_data */
+ DefPointer(void *, custom_data);
+ /* Stack frames, used in call stack dump and perf profiling */
+ DefPointer(Vector *, frames);
+ /* Function performance profiling info list, only available
+ in AOTModuleInstance */
+ DefPointer(struct AOTFuncPerfProfInfo *, func_perf_profilings);
+ DefPointer(CApiFuncImport *, c_api_func_imports);
+ /* Pointer to the exec env currently used */
+ DefPointer(WASMExecEnv *, cur_exec_env);
+ /* WASM/AOT module extra info, for AOTModuleInstance,
+ it denotes `AOTModuleInstanceExtra *` */
+ DefPointer(WASMModuleInstanceExtra *, e);
+
+ /* Default WASM operand stack size */
+ uint32 default_wasm_stack_size;
+ uint32 reserved[7];
+
+ /*
+ * +------------------------------+ <-- memories
+ * | WASMMemoryInstance[mem_count], mem_count is always 1 for LLVM JIT/AOT
+ * +------------------------------+ <-- global_data
+ * | global data
+ * +------------------------------+ <-- tables
+ * | WASMTableInstance[table_count]
+ * +------------------------------+ <-- e
+ * | WASMModuleInstanceExtra
+ * +------------------------------+
+ */
+ union {
+ uint64 _make_it_8_byte_aligned_;
+ WASMMemoryInstance memory_instances[1];
+ uint8 bytes[1];
+ } global_table_data;
+};
+
+struct WASMInterpFrame;
+typedef struct WASMInterpFrame WASMRuntimeFrame;
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+typedef struct WASMSubModInstNode {
+ bh_list_link l;
+ /* point to a string pool */
+ const char *module_name;
+ WASMModuleInstance *module_inst;
+} WASMSubModInstNode;
+#endif
+
+/**
+ * Return the code block of a function.
+ *
+ * @param func the WASM function instance
+ *
+ * @return the code block of the function
+ */
+static inline uint8 *
+wasm_get_func_code(WASMFunctionInstance *func)
+{
+#if WASM_ENABLE_FAST_INTERP == 0
+ return func->is_import_func ? NULL : func->u.func->code;
+#else
+ return func->is_import_func ? NULL : func->u.func->code_compiled;
+#endif
+}
+
+/**
+ * Return the code block end of a function.
+ *
+ * @param func the WASM function instance
+ *
+ * @return the code block end of the function
+ */
+static inline uint8 *
+wasm_get_func_code_end(WASMFunctionInstance *func)
+{
+#if WASM_ENABLE_FAST_INTERP == 0
+ return func->is_import_func ? NULL
+ : func->u.func->code + func->u.func->code_size;
+#else
+ return func->is_import_func
+ ? NULL
+ : func->u.func->code_compiled + func->u.func->code_compiled_size;
+#endif
+}
+
+WASMModule *
+wasm_load(uint8 *buf, uint32 size,
+#if WASM_ENABLE_MULTI_MODULE != 0
+ bool main_module,
+#endif
+ const LoadArgs *args, char *error_buf, uint32 error_buf_size);
+
+WASMModule *
+wasm_load_from_sections(WASMSection *section_list, char *error_buf,
+ uint32 error_buf_size);
+
+void
+wasm_unload(WASMModule *module);
+
+bool
+wasm_resolve_symbols(WASMModule *module);
+
+bool
+wasm_resolve_import_func(const WASMModule *module,
+ WASMFunctionImport *function);
+
+WASMModuleInstance *
+wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
+ WASMExecEnv *exec_env_main, uint32 stack_size,
+ uint32 heap_size, uint32 max_memory_pages, char *error_buf,
+ uint32 error_buf_size);
+
+void
+wasm_dump_perf_profiling(const WASMModuleInstance *module_inst);
+
+double
+wasm_summarize_wasm_execute_time(const WASMModuleInstance *inst);
+
+double
+wasm_get_wasm_func_exec_time(const WASMModuleInstance *inst,
+ const char *func_name);
+
+void
+wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst);
+
+bool
+wasm_set_running_mode(WASMModuleInstance *module_inst,
+ RunningMode running_mode);
+
+WASMFunctionInstance *
+wasm_lookup_function(const WASMModuleInstance *module_inst, const char *name);
+
+WASMMemoryInstance *
+wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name);
+
+#if WASM_ENABLE_MULTI_MODULE != 0
+WASMGlobalInstance *
+wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name);
+
+WASMTableInstance *
+wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name);
+
+#if WASM_ENABLE_TAGS != 0
+WASMTagInstance *
+wasm_lookup_tag(const WASMModuleInstance *module_inst, const char *name,
+ const char *signature);
+#endif
+
+#endif
+
+bool
+wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
+ unsigned argc, uint32 argv[]);
+
+void
+wasm_set_exception(WASMModuleInstance *module, const char *exception);
+
+void
+wasm_set_exception_with_id(WASMModuleInstance *module_inst, uint32 id);
+
+const char *
+wasm_get_exception(WASMModuleInstance *module);
+
+/**
+ * @brief Copy exception in buffer passed as parameter. Thread-safe version of
+ * `wasm_get_exception()`
+ * @note Buffer size must be no smaller than EXCEPTION_BUF_LEN
+ * @return true if exception found
+ */
+bool
+wasm_copy_exception(WASMModuleInstance *module_inst, char *exception_buf);
+
+uint64
+wasm_module_malloc_internal(WASMModuleInstance *module_inst,
+ WASMExecEnv *exec_env, uint64 size,
+ void **p_native_addr);
+
+uint64
+wasm_module_realloc_internal(WASMModuleInstance *module_inst,
+ WASMExecEnv *exec_env, uint64 ptr, uint64 size,
+ void **p_native_addr);
+
+void
+wasm_module_free_internal(WASMModuleInstance *module_inst,
+ WASMExecEnv *exec_env, uint64 ptr);
+
+uint64
+wasm_module_malloc(WASMModuleInstance *module_inst, uint64 size,
+ void **p_native_addr);
+
+uint64
+wasm_module_realloc(WASMModuleInstance *module_inst, uint64 ptr, uint64 size,
+ void **p_native_addr);
+
+void
+wasm_module_free(WASMModuleInstance *module_inst, uint64 ptr);
+
+uint64
+wasm_module_dup_data(WASMModuleInstance *module_inst, const char *src,
+ uint64 size);
+
+/**
+ * Check whether the app address and the buf is inside the linear memory,
+ * and convert the app address into native address
+ */
+bool
+wasm_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
+ uint64 app_buf_addr, uint64 app_buf_size,
+ void **p_native_addr);
+
+WASMMemoryInstance *
+wasm_get_default_memory(WASMModuleInstance *module_inst);
+
+WASMMemoryInstance *
+wasm_get_memory_with_idx(WASMModuleInstance *module_inst, uint32 index);
+
+bool
+wasm_enlarge_memory(WASMModuleInstance *module_inst, uint32 inc_page_count);
+
+bool
+wasm_enlarge_memory_with_idx(WASMModuleInstance *module_inst,
+ uint32 inc_page_count, uint32 memidx);
+
+bool
+wasm_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
+ uint32 argc, uint32 argv[]);
+
+#if WASM_ENABLE_THREAD_MGR != 0
+bool
+wasm_set_aux_stack(WASMExecEnv *exec_env, uint64 start_offset, uint32 size);
+
+bool
+wasm_get_aux_stack(WASMExecEnv *exec_env, uint64 *start_offset, uint32 *size);
+#endif
+
+void
+wasm_get_module_mem_consumption(const WASMModule *module,
+ WASMModuleMemConsumption *mem_conspn);
+
+void
+wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module,
+ WASMModuleInstMemConsumption *mem_conspn);
+
+#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
+static inline bool
+wasm_elem_is_active(uint32 mode)
+{
+ return (mode & 0x1) == 0x0;
+}
+
+static inline bool
+wasm_elem_is_passive(uint32 mode)
+{
+ return (mode & 0x1) == 0x1;
+}
+
+static inline bool
+wasm_elem_is_declarative(uint32 mode)
+{
+ return (mode & 0x3) == 0x3;
+}
+
+bool
+wasm_enlarge_table(WASMModuleInstance *module_inst, uint32 table_idx,
+ uint32 inc_entries, table_elem_type_t init_val);
+#endif /* WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
+
+#if WASM_ENABLE_GC != 0
+void *
+wasm_create_func_obj(WASMModuleInstance *module_inst, uint32 func_idx,
+ bool throw_exce, char *error_buf, uint32 error_buf_size);
+
+bool
+wasm_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap);
+
+#endif
+
+static inline WASMTableInstance *
+wasm_get_table_inst(const WASMModuleInstance *module_inst, uint32 tbl_idx)
+{
+ /* careful, it might be a table in another module */
+ WASMTableInstance *tbl_inst = module_inst->tables[tbl_idx];
+#if WASM_ENABLE_MULTI_MODULE != 0
+ if (tbl_idx < module_inst->module->import_table_count
+ && module_inst->e->table_insts_linked[tbl_idx]) {
+ tbl_inst = module_inst->e->table_insts_linked[tbl_idx];
+ }
+#endif
+ bh_assert(tbl_inst);
+ return tbl_inst;
+}
+
+#if WASM_ENABLE_DUMP_CALL_STACK != 0
+bool
+wasm_interp_create_call_stack(struct WASMExecEnv *exec_env);
+
+/**
+ * @brief Dump wasm call stack or get the size
+ *
+ * @param exec_env the execution environment
+ * @param print whether to print to stdout or not
+ * @param buf buffer to store the dumped content
+ * @param len length of the buffer
+ *
+ * @return when print is true, return the bytes printed out to stdout; when
+ * print is false and buf is NULL, return the size required to store the
+ * callstack content; when print is false and buf is not NULL, return the size
+ * dumped to the buffer, 0 means error and data in buf may be invalid
+ */
+uint32
+wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env, bool print, char *buf,
+ uint32 len);
+#endif
+
+const uint8 *
+wasm_loader_get_custom_section(WASMModule *module, const char *name,
+ uint32 *len);
+
+#if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
+ || WASM_ENABLE_WAMR_COMPILER != 0
+void
+jit_set_exception_with_id(WASMModuleInstance *module_inst, uint32 id);
+
+/**
+ * Check whether the app address and the buf is inside the linear memory,
+ * and convert the app address into native address
+ */
+bool
+jit_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
+ uint64 app_buf_addr, uint64 app_buf_size,
+ void **p_native_addr);
+#endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
+ || WASM_ENABLE_WAMR_COMPILER != 0 */
+
+#if WASM_ENABLE_FAST_JIT != 0
+bool
+fast_jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
+ uint32 type_idx, uint32 argc, uint32 *argv);
+
+bool
+fast_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx,
+ struct WASMInterpFrame *prev_frame);
+#endif
+
+#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
+bool
+llvm_jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
+ uint32 argc, uint32 *argv);
+
+bool
+llvm_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
+ uint32 *argv);
+
+#if WASM_ENABLE_BULK_MEMORY != 0
+bool
+llvm_jit_memory_init(WASMModuleInstance *module_inst, uint32 seg_index,
+ uint32 offset, uint32 len, size_t dst);
+
+bool
+llvm_jit_data_drop(WASMModuleInstance *module_inst, uint32 seg_index);
+#endif
+
+#if WASM_ENABLE_REF_TYPES != 0
+void
+llvm_jit_drop_table_seg(WASMModuleInstance *module_inst, uint32 tbl_seg_idx);
+
+void
+llvm_jit_table_init(WASMModuleInstance *module_inst, uint32 tbl_idx,
+ uint32 tbl_seg_idx, uint32 length, uint32 src_offset,
+ uint32 dst_offset);
+
+void
+llvm_jit_table_copy(WASMModuleInstance *module_inst, uint32 src_tbl_idx,
+ uint32 dst_tbl_idx, uint32 length, uint32 src_offset,
+ uint32 dst_offset);
+
+void
+llvm_jit_table_fill(WASMModuleInstance *module_inst, uint32 tbl_idx,
+ uint32 length, uintptr_t val, uint32 data_offset);
+
+uint32
+llvm_jit_table_grow(WASMModuleInstance *module_inst, uint32 tbl_idx,
+ uint32 inc_entries, uintptr_t init_val);
+#endif
+
+#if WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_PERF_PROFILING != 0 \
+ || WASM_ENABLE_AOT_STACK_FRAME != 0
+bool
+llvm_jit_alloc_frame(WASMExecEnv *exec_env, uint32 func_index);
+
+void
+llvm_jit_free_frame(WASMExecEnv *exec_env);
+
+void
+llvm_jit_frame_update_profile_info(WASMExecEnv *exec_env, bool alloc_frame);
+#endif
+
+#if WASM_ENABLE_GC != 0
+void *
+llvm_jit_create_func_obj(WASMModuleInstance *module_inst, uint32 func_idx,
+ bool throw_exce, char *error_buf,
+ uint32 error_buf_size);
+
+bool
+llvm_jit_obj_is_instance_of(WASMModuleInstance *module_inst,
+ WASMObjectRef gc_obj, uint32 type_index);
+
+/* Whether func type1 is one of super types of func type2 */
+bool
+llvm_jit_func_type_is_super_of(WASMModuleInstance *module_inst,
+ uint32 type_idx1, uint32 type_idx2);
+
+WASMRttTypeRef
+llvm_jit_rtt_type_new(WASMModuleInstance *module_inst, uint32 type_index);
+
+bool
+llvm_array_init_with_data(WASMModuleInstance *module_inst, uint32 seg_index,
+ uint32 data_seg_offset, WASMArrayObjectRef array_obj,
+ uint32 elem_size, uint32 array_len);
+#endif
+#endif /* end of WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0 */
+
+#if WASM_ENABLE_LIBC_WASI != 0 && WASM_ENABLE_MULTI_MODULE != 0
+void
+wasm_propagate_wasi_args(WASMModule *module);
+#endif
+
+#if WASM_ENABLE_THREAD_MGR != 0
+void
+exception_lock(WASMModuleInstance *module_inst);
+void
+exception_unlock(WASMModuleInstance *module_inst);
+#else
+#define exception_lock(module_inst) (void)(module_inst)
+#define exception_unlock(module_inst) (void)(module_inst)
+#endif
+
+bool
+wasm_check_utf8_str(const uint8 *str, uint32 len);
+
+char *
+wasm_const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module,
+ bool is_load_from_file_buf, char *error_buf,
+ uint32 error_buf_size);
+
+bool
+wasm_set_module_name(WASMModule *module, const char *name, char *error_buf,
+ uint32_t error_buf_size);
+
+const char *
+wasm_get_module_name(WASMModule *module);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* end of _WASM_RUNTIME_H */