Wenyong Huang 3 лет назад
Родитель
Сommit
56b4a8bd4c
74 измененных файлов с 1478 добавлено и 646 удалено
  1. 113 0
      .github/workflows/compilation_on_nuttx.yml
  2. 2 0
      README.md
  3. 3 0
      build-scripts/config_common.cmake
  4. 5 0
      build-scripts/runtime_lib.cmake
  5. 28 4
      core/config.h
  6. 152 122
      core/iwasm/aot/aot_runtime.c
  7. 22 0
      core/iwasm/aot/arch/aot_reloc_arc.c
  8. 17 15
      core/iwasm/aot/arch/aot_reloc_thumb.c
  9. 4 118
      core/iwasm/common/wasm_application.c
  10. 20 2
      core/iwasm/common/wasm_memory.c
  11. 23 9
      core/iwasm/common/wasm_native.c
  12. 16 7
      core/iwasm/common/wasm_runtime_common.c
  13. 5 0
      core/iwasm/common/wasm_runtime_common.h
  14. 3 3
      core/iwasm/compilation/simd/simd_conversions.c
  15. 5 1
      core/iwasm/fast-jit/fe/jit_emit_control.c
  16. 64 13
      core/iwasm/fast-jit/fe/jit_emit_numberic.c
  17. 1 1
      core/iwasm/fast-jit/jit_compiler.c
  18. 37 0
      core/iwasm/fast-jit/jit_frontend.c
  19. 8 0
      core/iwasm/fast-jit/jit_frontend.h
  20. 4 2
      core/iwasm/fast-jit/jit_ir.c
  21. 8 0
      core/iwasm/fast-jit/jit_ir.h
  22. 30 10
      core/iwasm/include/wasm_export.h
  23. 1 0
      core/iwasm/interpreter/wasm.h
  24. 27 16
      core/iwasm/interpreter/wasm_loader.c
  25. 25 14
      core/iwasm/interpreter/wasm_mini_loader.c
  26. 170 87
      core/iwasm/interpreter/wasm_runtime.c
  27. 14 8
      core/iwasm/interpreter/wasm_runtime.h
  28. 15 44
      core/iwasm/libraries/debug-engine/debug_engine.c
  29. 2 8
      core/iwasm/libraries/debug-engine/debug_engine.h
  30. 1 1
      core/iwasm/libraries/debug-engine/gdbserver.c
  31. 68 33
      core/iwasm/libraries/debug-engine/handler.c
  32. 33 0
      core/iwasm/libraries/lib-rats/lib_rats.cmake
  33. 60 0
      core/iwasm/libraries/lib-rats/lib_rats_wrapper.c
  34. 18 0
      core/iwasm/libraries/lib-rats/lib_rats_wrapper.h
  35. 6 5
      core/shared/mem-alloc/ems/ems_kfc.c
  36. 9 2
      core/shared/mem-alloc/mem_alloc.c
  37. 4 0
      core/shared/mem-alloc/mem_alloc.cmake
  38. 4 1
      core/shared/mem-alloc/mem_alloc.h
  39. 1 0
      core/shared/platform/alios/platform_internal.h
  40. 1 1
      core/shared/utils/bh_list.h
  41. 4 0
      doc/build_wamr.md
  42. 3 4
      doc/build_wasm_app.md
  43. 1 1
      product-mini/platforms/android/CMakeLists.txt
  44. 18 1
      product-mini/platforms/linux-sgx/CMakeLists.txt
  45. 5 0
      product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.edl
  46. 49 17
      product-mini/platforms/linux-sgx/enclave-sample/Makefile
  47. 17 2
      product-mini/platforms/nuttx/wamr.mk
  48. 23 8
      product-mini/platforms/posix/main.c
  49. 22 3
      product-mini/platforms/windows/main.c
  50. 5 5
      samples/multi-module/CMakeLists.txt
  51. 0 16
      samples/multi-module/src/main.c
  52. 1 0
      samples/multi-thread/CMakeLists.txt
  53. 81 0
      samples/sgx-ra/CMakeLists.txt
  54. 42 0
      samples/sgx-ra/README.md
  55. 38 0
      samples/sgx-ra/wasm-app/CMakeLists.txt
  56. 36 0
      samples/sgx-ra/wasm-app/main.c
  57. 4 4
      samples/socket-api/CMakeLists.txt
  58. 1 1
      samples/workload/XNNPACK/CMakeLists.txt
  59. 1 1
      samples/workload/bwa/CMakeLists.txt
  60. 1 1
      samples/workload/meshoptimizer/CMakeLists.txt
  61. 10 10
      samples/workload/meshoptimizer/codecbench.patch
  62. 7 2
      test-tools/wamr-ide/VSCode-Extension/package.json
  63. 4 4
      test-tools/wamr-ide/VSCode-Extension/resource/debug/README.md
  64. 0 0
      test-tools/wamr-ide/VSCode-Extension/resource/debug/linux/.placeholder
  65. 0 0
      test-tools/wamr-ide/VSCode-Extension/resource/debug/osx/.placeholder
  66. 0 0
      test-tools/wamr-ide/VSCode-Extension/resource/debug/windows/.placeholder
  67. 2 0
      test-tools/wamr-ide/VSCode-Extension/resource/scripts/boot_debugger_server.sh
  68. 2 0
      test-tools/wamr-ide/VSCode-Extension/resource/scripts/build.sh
  69. 2 0
      test-tools/wamr-ide/VSCode-Extension/resource/scripts/destroy.sh
  70. 2 0
      test-tools/wamr-ide/VSCode-Extension/resource/scripts/run.sh
  71. 1 1
      test-tools/wamr-ide/VSCode-Extension/resource/webview/css/style.css
  72. 24 0
      test-tools/wamr-ide/VSCode-Extension/src/utilities/directoryUtilities.ts
  73. 41 35
      test-tools/wamr-ide/VSCode-Extension/src/view/NewProjectPanel.ts
  74. 2 3
      wamr-compiler/main.c

+ 113 - 0
.github/workflows/compilation_on_nuttx.yml

@@ -0,0 +1,113 @@
+# Copyright (C) 2019 Intel Corporation.  All rights reserved.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+name: compilation on nuttx
+
+on:
+  # will be triggered on PR events
+  pull_request:
+    paths-ignore:
+      - "assembly-script/**"
+      - "ci/**"
+      - "doc/**"
+      - "test-tools/**"
+  # will be triggered on push events
+  push:
+    paths-ignore:
+      - "assembly-script/**"
+      - "ci/**"
+      - "doc/**"
+      - "test-tools/**"
+  # allow to be triggered manually
+  workflow_dispatch:
+
+# Cancel any in-flight jobs for the same PR/branch so there's only one active
+# at a time
+concurrency:
+  group: ${{ github.workflow }}-${{ github.ref }}
+  cancel-in-progress: true
+
+jobs:
+  # Cancel any in-flight jobs for the same PR/branch so there's only one active
+  # at a time
+  cancel_previous:
+    runs-on: ubuntu-22.04
+    steps:
+      - name: Cancel Workflow Action
+        uses: styfle/cancel-workflow-action@0.9.1
+        with:
+          access_token: ${{ github.token }}  
+
+  build_iwasm_on_nuttx:
+    runs-on: ubuntu-22.04
+    strategy:
+      matrix:
+        nuttx_board_config : [
+          # x64
+          "boards/sim/sim/sim/configs/nsh",
+          # cortex-m0
+          "boards/arm/rp2040/raspberrypi-pico/configs/nsh",
+          # cortex-m4
+          "boards/arm/stm32/stm32f4discovery/configs/nsh",
+          # cortex-m7
+          "boards/arm/stm32h7/nucleo-h743zi/configs/nsh",
+          # cortex-a9
+          "boards/arm/imx6/sabre-6quad/configs/nsh",
+          # riscv32imac
+          "boards/risc-v/qemu-rv/rv-virt/configs/nsh",
+          # riscv64imac
+          "boards/risc-v/qemu-rv/rv-virt/configs/nsh64",
+          # riscv64gc
+          "boards/risc-v/k210/maix-bit/configs/nsh",
+        ]
+        wamr_config_option: [
+          "CONFIG_INTERPRETERS_WAMR=y\\nCONFIG_INTERPRETERS_WAMR_AOT=y\\nCONFIG_INTERPRETERS_WAMR_FAST=y\\nCONFIG_INTERPRETERS_WAMR_LIBC_BUILTIN=y\\n",
+          "CONFIG_INTERPRETERS_WAMR=y\\nCONFIG_INTERPRETERS_WAMR_AOT=y\\nCONFIG_INTERPRETERS_WAMR_FAST=y\\n",
+          "CONFIG_INTERPRETERS_WAMR=y\\nCONFIG_INTERPRETERS_WAMR_AOT=y\\nCONFIG_INTERPRETERS_WAMR_CLASSIC=y\\n",
+          "CONFIG_INTERPRETERS_WAMR=y\\nCONFIG_INTERPRETERS_WAMR_AOT=y\\nCONFIG_INTERPRETERS_WAMR_LIBC_BUILTIN=y\\n",
+          "CONFIG_INTERPRETERS_WAMR=y\\nCONFIG_INTERPRETERS_WAMR_AOT=y\\n",
+          "CONFIG_INTERPRETERS_WAMR=y\\nCONFIG_INTERPRETERS_WAMR_FAST=y\\n",
+          "CONFIG_INTERPRETERS_WAMR=y\\nCONFIG_INTERPRETERS_WAMR_CLASSIC=y\\n",
+        ]
+
+    steps:
+      - name: Install Utilities
+        run: sudo apt install -y kconfig-frontends-nox genromfs
+
+      - name: Install ARM Compilers
+        if: ${{ contains(matrix.nuttx_board_config, 'arm') }}
+        run: sudo apt install -y gcc-arm-none-eabi
+
+      - name: Install RISC-V Compilers
+        if: ${{ contains(matrix.nuttx_board_config, 'risc-v') }}
+        run: sudo apt install -y gcc-riscv64-unknown-elf
+
+      - name: Checkout NuttX
+        uses: actions/checkout@v3
+        with:
+          repository: apache/incubator-nuttx
+          path: nuttx
+
+      - name: Checkout NuttX Apps
+        uses: actions/checkout@v3
+        with:
+          repository: apache/incubator-nuttx-apps
+          path: apps
+
+      - name: Checkout WAMR
+        uses: actions/checkout@v3
+        with:
+          repository: ${{ github.repository }}
+          path: apps/interpreters/wamr/wamr
+
+      - name: Enable WAMR for NuttX
+        run: | 
+          find nuttx/boards -name defconfig | xargs sed -i '$a\CONFIG_EOL_IS_CR=y\n${{ matrix.wamr_config_option }}'
+          find nuttx/boards/sim -name defconfig | xargs sed -i '$a\CONFIG_LIBM=y\n'
+          find nuttx/boards/risc-v -name defconfig | xargs sed -i '$a\CONFIG_LIBM=y\n'
+
+      - name: Build
+        run: |
+          cd nuttx
+          tools/configure.sh ${{ matrix.nuttx_board_config }}
+          make -j$(nproc)

+ 2 - 0
README.md

@@ -45,6 +45,7 @@ iwasm VM core
 - [WAMR-IDE (Experimental)](./test-tools/wamr-ide) to develop WebAssembly applications with build, run and debug support, ref to [document](./test-tools/wamr-ide)
 - [XIP (Execution In Place) support](./doc/xip.md), ref to [document](./doc/xip.md)
 - [Berkeley/Posix Socket support](./doc/socket_api.md), ref to [document](./doc/socket_api.md) and [sample](./samples/socket-api)
+- Language bindings: [Go](./language-bindings/go/README.md), [Python](./language-bindings/python/README.md)
 
 ### WASM post-MVP features
 - [wasm-c-api](https://github.com/WebAssembly/wasm-c-api), ref to [document](doc/wasm_c_api.md) and [sample](samples/wasm-c-api)
@@ -158,6 +159,7 @@ The WAMR [samples](./samples) integrate the iwasm VM core, application manager a
 - **[wasm-c-api](./samples/wasm-c-api/README.md)**: Demonstrating how to run some samples from [wasm-c-api proposal](https://github.com/WebAssembly/wasm-c-api) and showing the supported API's.
 - **[socket-api](./samples/socket-api/README.md)**: Demonstrating how to run wasm tcp server and tcp client applications, and how they communicate with each other.
 - **[workload](./samples/workload/README.md)**: Demonstrating how to build and run some complex workloads, e.g. tensorflow-lite, XNNPACK, wasm-av1, meshoptimizer and bwa.
+- **[sgx-ra](./samples/sgx-ra/README.md)**: Demonstrating how to execute Remote Attestation on SGX with [librats](https://github.com/inclavare-containers/librats), which enables mutual attestation with other runtimes or other entities that support librats to ensure that each is running within the TEE.
 
 
 Project Technical Steering Committee

+ 3 - 0
build-scripts/config_common.cmake

@@ -215,6 +215,9 @@ endif ()
 if (WAMR_BUILD_LIBC_EMCC EQUAL 1)
   message ("     Libc emcc enabled")
 endif ()
+if (WAMR_BUILD_LIB_RATS EQUAL 1)
+  message ("     Lib rats enabled")
+endif()
 if (WAMR_BUILD_MINI_LOADER EQUAL 1)
   add_definitions (-DWASM_ENABLE_MINI_LOADER=1)
   message ("     WASM mini loader enabled")

+ 5 - 0
build-scripts/runtime_lib.cmake

@@ -118,6 +118,10 @@ if (WAMR_BUILD_LIBC_EMCC EQUAL 1)
     include (${IWASM_DIR}/libraries/libc-emcc/libc_emcc.cmake)
 endif ()
 
+if (WAMR_BUILD_LIB_RATS EQUAL 1)
+    include (${IWASM_DIR}/libraries/lib-rats/lib_rats.cmake)
+endif ()
+
 ####################### Common sources #######################
 if (NOT MSVC)
     set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -ffunction-sections -fdata-sections \
@@ -159,6 +163,7 @@ set (source_all
     ${LIB_PTHREAD_SOURCE}
     ${THREAD_MGR_SOURCE}
     ${LIBC_EMCC_SOURCE}
+    ${LIB_RATS_SOURCE}
     ${DEBUG_ENGINE_SOURCE}
 )
 

+ 28 - 4
core/config.h

@@ -135,6 +135,10 @@
 #define WASM_ENABLE_LIBC_EMCC 0
 #endif
 
+#ifndef WASM_ENABLE_LIB_RATS
+#define WASM_ENABLE_LIB_RATS 0
+#endif
+
 #ifndef WASM_ENABLE_LIB_PTHREAD
 #define WASM_ENABLE_LIB_PTHREAD 0
 #endif
@@ -272,6 +276,30 @@
 #define BH_ENABLE_GC_VERIFY 0
 #endif
 
+/* Enable global heap pool if heap verification is enabled */
+#if BH_ENABLE_GC_VERIFY != 0
+#define WASM_ENABLE_GLOBAL_HEAP_POOL 1
+#endif
+
+/* Global heap pool */
+#ifndef WASM_ENABLE_GLOBAL_HEAP_POOL
+#define WASM_ENABLE_GLOBAL_HEAP_POOL 0
+#endif
+
+#ifndef WASM_ENABLE_SPEC_TEST
+#define WASM_ENABLE_SPEC_TEST 0
+#endif
+
+/* Global heap pool size in bytes */
+#ifndef WASM_GLOBAL_HEAP_SIZE
+#if WASM_ENABLE_SPEC_TEST != 0
+/* Spec test requires more heap pool size */
+#define WASM_GLOBAL_HEAP_SIZE (300 * 1024 * 1024)
+#else
+#define WASM_GLOBAL_HEAP_SIZE (10 * 1024 * 1024)
+#endif
+#endif
+
 /* Max app number of all modules */
 #define MAX_APP_INSTALLATIONS 3
 
@@ -363,10 +391,6 @@
 #endif
 #define BLOCK_ADDR_CONFLICT_SIZE 2
 
-#ifndef WASM_ENABLE_SPEC_TEST
-#define WASM_ENABLE_SPEC_TEST 0
-#endif
-
 /* Default max thread num per cluster. Can be overwrite by
     wasm_runtime_set_max_thread_num */
 #define CLUSTER_MAX_THREAD_NUM 4

+ 152 - 122
core/iwasm/aot/aot_runtime.c

@@ -412,18 +412,27 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
     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*/
+           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,
-                          "memory size must be at most 65536 pages (4GiB)");
+                          "failed to insert app heap into linear memory, "
+                          "try using `--heap_size=0` option");
             return NULL;
         }
     }
     else if (heap_size > 0) {
-        if (module->aux_heap_base_global_index != (uint32)-1
-            && module->aux_heap_base < num_bytes_per_page * init_page_count) {
+        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
+                        < 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;
@@ -464,13 +473,18 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
         }
         init_page_count += inc_page_count;
         max_page_count += inc_page_count;
-        if (init_page_count > 65536) {
+        if (init_page_count > DEFAULT_MAX_PAGES) {
             set_error_buf(error_buf, error_buf_size,
-                          "memory size must be at most 65536 pages (4GiB)");
+                          "failed to insert app heap into linear memory, "
+                          "try using `--heap_size=0` option");
             return NULL;
         }
-        if (max_page_count > 65536)
-            max_page_count = 65536;
+        else if (init_page_count == DEFAULT_MAX_PAGES) {
+            num_bytes_per_page = UINT32_MAX;
+            init_page_count = max_page_count = 1;
+        }
+        if (max_page_count > DEFAULT_MAX_PAGES)
+            max_page_count = DEFAULT_MAX_PAGES;
     }
 
     LOG_VERBOSE("Memory instantiate:");
@@ -487,6 +501,7 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
         total_size = (uint64)num_bytes_per_page * max_page_count;
     }
 #endif
+    bh_assert(total_size <= UINT32_MAX);
 
 #ifndef OS_ENABLE_HW_BOUND_CHECK
     /* Allocate memory */
@@ -502,9 +517,8 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
      * both i and memarg.offset are u32 in range 0 to 4G
      * so the range of ea is 0 to 8G
      */
-    if (total_size >= UINT32_MAX
-        || !(p = mapped_mem =
-                 os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE))) {
+    if (!(p = mapped_mem =
+              os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE))) {
         set_error_buf(error_buf, error_buf_size, "mmap memory failed");
         return NULL;
     }
@@ -529,15 +543,18 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
      * again here */
 #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
 
+    if (total_size > UINT32_MAX)
+        total_size = UINT32_MAX;
+
     memory_inst->module_type = Wasm_Module_AoT;
     memory_inst->num_bytes_per_page = num_bytes_per_page;
     memory_inst->cur_page_count = init_page_count;
     memory_inst->max_page_count = max_page_count;
+    memory_inst->memory_data_size = (uint32)total_size;
 
     /* Init memory info */
     memory_inst->memory_data.ptr = p;
     memory_inst->memory_data_end.ptr = p + (uint32)total_size;
-    memory_inst->memory_data_size = (uint32)total_size;
 
     /* Initialize heap info */
     memory_inst->heap_data.ptr = p + heap_offset;
@@ -561,21 +578,19 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
     }
 
     if (total_size > 0) {
-        if (sizeof(uintptr_t) == sizeof(uint64)) {
-            memory_inst->mem_bound_check_1byte.u64 = total_size - 1;
-            memory_inst->mem_bound_check_2bytes.u64 = total_size - 2;
-            memory_inst->mem_bound_check_4bytes.u64 = total_size - 4;
-            memory_inst->mem_bound_check_8bytes.u64 = total_size - 8;
-            memory_inst->mem_bound_check_16bytes.u64 = total_size - 16;
-        }
-        else {
-            memory_inst->mem_bound_check_1byte.u32[0] = (uint32)total_size - 1;
-            memory_inst->mem_bound_check_2bytes.u32[0] = (uint32)total_size - 2;
-            memory_inst->mem_bound_check_4bytes.u32[0] = (uint32)total_size - 4;
-            memory_inst->mem_bound_check_8bytes.u32[0] = (uint32)total_size - 8;
-            memory_inst->mem_bound_check_16bytes.u32[0] =
-                (uint32)total_size - 16;
-        }
+#if UINTPTR_MAX == UINT64_MAX
+        memory_inst->mem_bound_check_1byte.u64 = total_size - 1;
+        memory_inst->mem_bound_check_2bytes.u64 = total_size - 2;
+        memory_inst->mem_bound_check_4bytes.u64 = total_size - 4;
+        memory_inst->mem_bound_check_8bytes.u64 = total_size - 8;
+        memory_inst->mem_bound_check_16bytes.u64 = total_size - 16;
+#else
+        memory_inst->mem_bound_check_1byte.u32[0] = (uint32)total_size - 1;
+        memory_inst->mem_bound_check_2bytes.u32[0] = (uint32)total_size - 2;
+        memory_inst->mem_bound_check_4bytes.u32[0] = (uint32)total_size - 4;
+        memory_inst->mem_bound_check_8bytes.u32[0] = (uint32)total_size - 8;
+        memory_inst->mem_bound_check_16bytes.u32[0] = (uint32)total_size - 16;
+#endif
     }
 
 #if WASM_ENABLE_SHARED_MEMORY != 0
@@ -1470,7 +1485,8 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
 
 #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
         if (!aot_alloc_frame(exec_env, function->func_index)) {
-            wasm_runtime_free(argv1);
+            if (argv1 != argv1_buf)
+                wasm_runtime_free(argv1);
             return false;
         }
 #endif
@@ -1479,9 +1495,6 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
                                      func_type, NULL, NULL, argv1, argc, argv);
 
         if (!ret || aot_get_exception(module_inst)) {
-            if (argv1 != argv1_buf)
-                wasm_runtime_free(argv1);
-
             if (clear_wasi_proc_exit_exception(module_inst))
                 ret = true;
             else
@@ -1499,8 +1512,11 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
 #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
         aot_free_frame(exec_env);
 #endif
-        if (!ret)
+        if (!ret) {
+            if (argv1 != argv1_buf)
+                wasm_runtime_free(argv1);
             return ret;
+        }
 
         /* Get extra result values */
         switch (func_type->types[func_type->param_count]) {
@@ -1529,9 +1545,9 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
             argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count;
         bh_memcpy_s(argv_ret, sizeof(uint32) * cell_num, ext_rets,
                     sizeof(uint32) * cell_num);
+
         if (argv1 != argv1_buf)
             wasm_runtime_free(argv1);
-
         return true;
     }
     else {
@@ -2034,26 +2050,29 @@ aot_get_native_addr_range(AOTModuleInstance *module_inst, uint8 *native_ptr,
 bool
 aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
 {
-    AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
-    uint32 num_bytes_per_page, cur_page_count, max_page_count;
-    uint32 total_page_count, total_size_old, heap_size;
-    uint64 total_size;
-    uint8 *memory_data_old, *heap_data_old, *memory_data, *heap_data;
+    AOTMemoryInstance *memory = aot_get_default_memory(module_inst);
+    uint8 *memory_data_old, *memory_data_new, *heap_data_old;
+    uint32 num_bytes_per_page, heap_size, total_size_old;
+    uint32 cur_page_count, max_page_count, total_page_count;
+    uint64 total_size_new;
     bool ret = true;
 
-    if (!memory_inst)
+    if (!memory)
         return false;
 
-    num_bytes_per_page = memory_inst->num_bytes_per_page;
-    cur_page_count = memory_inst->cur_page_count;
-    max_page_count = memory_inst->max_page_count;
-    total_page_count = cur_page_count + inc_page_count;
-    total_size_old = memory_inst->memory_data_size;
-    total_size = (uint64)num_bytes_per_page * total_page_count;
-    heap_size = (uint32)((uint8 *)memory_inst->heap_data_end.ptr
-                         - (uint8 *)memory_inst->heap_data.ptr);
-    memory_data_old = (uint8 *)memory_inst->memory_data.ptr;
-    heap_data_old = (uint8 *)memory_inst->heap_data.ptr;
+    heap_data_old = (uint8 *)memory->heap_data.ptr;
+    heap_size = (uint32)((uint8 *)memory->heap_data_end.ptr
+                         - (uint8 *)memory->heap_data.ptr);
+
+    memory_data_old = (uint8 *)memory->memory_data.ptr;
+    total_size_old =
+        (uint32)((uint8 *)memory->memory_data_end.ptr - memory_data_old);
+
+    num_bytes_per_page = memory->num_bytes_per_page;
+    cur_page_count = memory->cur_page_count;
+    max_page_count = memory->max_page_count;
+    total_page_count = inc_page_count + cur_page_count;
+    total_size_new = num_bytes_per_page * (uint64)total_page_count;
 
     if (inc_page_count <= 0)
         /* No need to enlarge memory */
@@ -2064,94 +2083,103 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
         return false;
     }
 
-    if (total_size >= UINT32_MAX) {
-        return false;
+    bh_assert(total_size_new <= 4 * (uint64)BH_GB);
+    if (total_size_new > UINT32_MAX) {
+        /* Resize to 1 page with size 4G-1 */
+        num_bytes_per_page = UINT32_MAX;
+        total_page_count = max_page_count = 1;
+        total_size_new = UINT32_MAX;
     }
 
 #if WASM_ENABLE_SHARED_MEMORY != 0
-    if (memory_inst->is_shared) {
-        /* For shared memory, we have reserved the maximum spaces during
-            instantiate, only change the cur_page_count here */
-        memory_inst->cur_page_count = total_page_count;
+    if (memory->is_shared) {
+        memory->num_bytes_per_page = UINT32_MAX;
+        memory->cur_page_count = total_page_count;
+        memory->max_page_count = max_page_count;
+        memory->memory_data_size = (uint32)total_size_new;
         return true;
     }
 #endif
 
     if (heap_size > 0) {
-        if (mem_allocator_is_heap_corrupted(memory_inst->heap_handle.ptr)) {
+        if (mem_allocator_is_heap_corrupted(memory->heap_handle.ptr)) {
             wasm_runtime_show_app_heap_corrupted_prompt();
             return false;
         }
     }
 
-    if (!(memory_data =
-              wasm_runtime_realloc(memory_data_old, (uint32)total_size))) {
-        if (!(memory_data = wasm_runtime_malloc((uint32)total_size))) {
+    if (!(memory_data_new =
+              wasm_runtime_realloc(memory_data_old, (uint32)total_size_new))) {
+        if (!(memory_data_new = wasm_runtime_malloc((uint32)total_size_new))) {
             return false;
         }
         if (memory_data_old) {
-            bh_memcpy_s(memory_data, (uint32)total_size, memory_data_old,
-                        total_size_old);
+            bh_memcpy_s(memory_data_new, (uint32)total_size_new,
+                        memory_data_old, total_size_old);
             wasm_runtime_free(memory_data_old);
         }
     }
 
-    memset(memory_data + total_size_old, 0,
-           (uint32)total_size - total_size_old);
-
-    memory_inst->cur_page_count = total_page_count;
-    memory_inst->memory_data_size = (uint32)total_size;
-    memory_inst->memory_data.ptr = memory_data;
-    memory_inst->memory_data_end.ptr = memory_data + total_size;
+    memset(memory_data_new + total_size_old, 0,
+           (uint32)total_size_new - total_size_old);
 
     if (heap_size > 0) {
-        if (mem_allocator_migrate(memory_inst->heap_handle.ptr,
+        if (mem_allocator_migrate(memory->heap_handle.ptr,
                                   (char *)heap_data_old
-                                      + (memory_data - memory_data_old),
-                                  heap_size)) {
+                                      + (memory_data_new - memory_data_old),
+                                  heap_size)
+            != 0) {
             /* Don't return here as memory->memory_data is obsolete and
                must be updated to be correctly used later. */
             ret = false;
         }
     }
 
-    heap_data = memory_data + (heap_data_old - memory_data_old);
-    memory_inst->heap_data.ptr = heap_data;
-    memory_inst->heap_data_end.ptr = heap_data + heap_size;
+    memory->heap_data.ptr = memory_data_new + (heap_data_old - memory_data_old);
+    memory->heap_data_end.ptr = (uint8 *)memory->heap_data.ptr + heap_size;
+
+    memory->num_bytes_per_page = num_bytes_per_page;
+    memory->cur_page_count = total_page_count;
+    memory->max_page_count = max_page_count;
+
+    memory->memory_data.ptr = memory_data_new;
+    memory->memory_data_end.ptr = memory_data_new + (uint32)total_size_new;
+    memory->memory_data_size = (uint32)total_size_new;
+
+#if UINTPTR_MAX == UINT64_MAX
+    memory->mem_bound_check_1byte.u64 = total_size_new - 1;
+    memory->mem_bound_check_2bytes.u64 = total_size_new - 2;
+    memory->mem_bound_check_4bytes.u64 = total_size_new - 4;
+    memory->mem_bound_check_8bytes.u64 = total_size_new - 8;
+    memory->mem_bound_check_16bytes.u64 = total_size_new - 16;
+#else
+    memory->mem_bound_check_1byte.u32[0] = (uint32)total_size_new - 1;
+    memory->mem_bound_check_2bytes.u32[0] = (uint32)total_size_new - 2;
+    memory->mem_bound_check_4bytes.u32[0] = (uint32)total_size_new - 4;
+    memory->mem_bound_check_8bytes.u32[0] = (uint32)total_size_new - 8;
+    memory->mem_bound_check_16bytes.u32[0] = (uint32)total_size_new - 16;
+#endif
 
-    if (sizeof(uintptr_t) == sizeof(uint64)) {
-        memory_inst->mem_bound_check_1byte.u64 = total_size - 1;
-        memory_inst->mem_bound_check_2bytes.u64 = total_size - 2;
-        memory_inst->mem_bound_check_4bytes.u64 = total_size - 4;
-        memory_inst->mem_bound_check_8bytes.u64 = total_size - 8;
-        memory_inst->mem_bound_check_16bytes.u64 = total_size - 16;
-    }
-    else {
-        memory_inst->mem_bound_check_1byte.u32[0] = (uint32)total_size - 1;
-        memory_inst->mem_bound_check_2bytes.u32[0] = (uint32)total_size - 2;
-        memory_inst->mem_bound_check_4bytes.u32[0] = (uint32)total_size - 4;
-        memory_inst->mem_bound_check_8bytes.u32[0] = (uint32)total_size - 8;
-        memory_inst->mem_bound_check_16bytes.u32[0] = (uint32)total_size - 16;
-    }
     return ret;
 }
 #else /* else of OS_ENABLE_HW_BOUND_CHECK */
 bool
 aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
 {
-    AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
-    uint32 num_bytes_per_page, cur_page_count, max_page_count;
-    uint32 total_page_count;
-    uint64 total_size;
+    AOTMemoryInstance *memory = aot_get_default_memory(module_inst);
+    uint32 num_bytes_per_page, total_size_old;
+    uint32 cur_page_count, max_page_count, total_page_count;
+    uint64 total_size_new;
 
-    if (!memory_inst)
+    if (!memory)
         return false;
 
-    num_bytes_per_page = memory_inst->num_bytes_per_page;
-    cur_page_count = memory_inst->cur_page_count;
-    max_page_count = memory_inst->max_page_count;
-    total_page_count = cur_page_count + inc_page_count;
-    total_size = (uint64)num_bytes_per_page * total_page_count;
+    num_bytes_per_page = memory->num_bytes_per_page;
+    cur_page_count = memory->cur_page_count;
+    max_page_count = memory->max_page_count;
+    total_size_old = num_bytes_per_page * cur_page_count;
+    total_page_count = inc_page_count + cur_page_count;
+    total_size_new = num_bytes_per_page * (uint64)total_page_count;
 
     if (inc_page_count <= 0)
         /* No need to enlarge memory */
@@ -2162,21 +2190,29 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
         return false;
     }
 
+    bh_assert(total_size_new <= 4 * (uint64)BH_GB);
+    if (total_size_new > UINT32_MAX) {
+        /* Resize to 1 page with size 4G-1 */
+        num_bytes_per_page = UINT32_MAX;
+        total_page_count = max_page_count = 1;
+        total_size_new = UINT32_MAX;
+    }
+
 #ifdef BH_PLATFORM_WINDOWS
-    if (!os_mem_commit(memory_inst->memory_data_end.ptr,
-                       num_bytes_per_page * inc_page_count,
+    if (!os_mem_commit(memory->memory_data_end.ptr,
+                       (uint32)total_size_new - total_size_old,
                        MMAP_PROT_READ | MMAP_PROT_WRITE)) {
         return false;
     }
 #endif
 
-    if (os_mprotect(memory_inst->memory_data_end.ptr,
-                    num_bytes_per_page * inc_page_count,
+    if (os_mprotect(memory->memory_data_end.ptr,
+                    (uint32)total_size_new - total_size_old,
                     MMAP_PROT_READ | MMAP_PROT_WRITE)
         != 0) {
 #ifdef BH_PLATFORM_WINDOWS
-        os_mem_decommit(memory_inst->memory_data_end.ptr,
-                        num_bytes_per_page * inc_page_count);
+        os_mem_decommit(memory->memory_data_end.ptr,
+                        (uint32)total_size_new - total_size_old);
 #endif
         return false;
     }
@@ -2184,25 +2220,19 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
     /* The increased pages are filled with zero by the OS when os_mmap,
        no need to memset it again here */
 
-    memory_inst->cur_page_count = total_page_count;
-    memory_inst->memory_data_size = (uint32)total_size;
-    memory_inst->memory_data_end.ptr =
-        (uint8 *)memory_inst->memory_data.ptr + (uint32)total_size;
+    memory->num_bytes_per_page = num_bytes_per_page;
+    memory->cur_page_count = total_page_count;
+    memory->max_page_count = max_page_count;
+    memory->memory_data_size = (uint32)total_size_new;
+    memory->memory_data_end.ptr =
+        (uint8 *)memory->memory_data.ptr + (uint32)total_size_new;
+
+    memory->mem_bound_check_1byte.u64 = total_size_new - 1;
+    memory->mem_bound_check_2bytes.u64 = total_size_new - 2;
+    memory->mem_bound_check_4bytes.u64 = total_size_new - 4;
+    memory->mem_bound_check_8bytes.u64 = total_size_new - 8;
+    memory->mem_bound_check_16bytes.u64 = total_size_new - 16;
 
-    if (sizeof(uintptr_t) == sizeof(uint64)) {
-        memory_inst->mem_bound_check_1byte.u64 = total_size - 1;
-        memory_inst->mem_bound_check_2bytes.u64 = total_size - 2;
-        memory_inst->mem_bound_check_4bytes.u64 = total_size - 4;
-        memory_inst->mem_bound_check_8bytes.u64 = total_size - 8;
-        memory_inst->mem_bound_check_16bytes.u64 = total_size - 16;
-    }
-    else {
-        memory_inst->mem_bound_check_1byte.u32[0] = (uint32)total_size - 1;
-        memory_inst->mem_bound_check_2bytes.u32[0] = (uint32)total_size - 2;
-        memory_inst->mem_bound_check_4bytes.u32[0] = (uint32)total_size - 4;
-        memory_inst->mem_bound_check_8bytes.u32[0] = (uint32)total_size - 8;
-        memory_inst->mem_bound_check_16bytes.u32[0] = (uint32)total_size - 16;
-    }
     return true;
 }
 #endif /* end of OS_ENABLE_HW_BOUND_CHECK */

+ 22 - 0
core/iwasm/aot/arch/aot_reloc_arc.c

@@ -53,6 +53,17 @@ void __subsf3();
 void __truncdfsf2();
 void __floatunsisf();
 void __fixunsdfsi();
+void __floatdisf();
+void __floatdidf();
+void __fixdfdi();
+void __ltsf2();
+void __gesf2();
+void __eqdf2();
+void __nedf2();
+void __ltsf2();
+void __nesf2();
+void __unordsf2();
+void __fixunssfsi();
 #else
 void __ac_push_13_to_13();
 void __ac_push_13_to_14();
@@ -162,6 +173,17 @@ static SymbolMap target_sym_map[] = {
     REG_SYM(__truncdfsf2),
     REG_SYM(__floatunsisf),
     REG_SYM(__fixunsdfsi),
+    REG_SYM(__floatdisf),
+    REG_SYM(__floatdidf),
+    REG_SYM(__fixdfdi),
+    REG_SYM(__ltsf2),
+    REG_SYM(__gesf2),
+    REG_SYM(__eqdf2),
+    REG_SYM(__nedf2),
+    REG_SYM(__ltsf2),
+    REG_SYM(__nesf2),
+    REG_SYM(__unordsf2),
+    REG_SYM(__fixunssfsi),
 #else
     REG_SYM(__ac_push_13_to_13),
     REG_SYM(__ac_push_13_to_14),

+ 17 - 15
core/iwasm/aot/arch/aot_reloc_thumb.c

@@ -97,8 +97,24 @@ static SymbolMap target_sym_map[] = {
     /* clang-format off */
     REG_COMMON_SYMBOLS
     /* compiler-rt symbols that come from compiler(e.g. gcc) */
+#if __ARM_ARCH != 6
     REG_SYM(__adddf3),
     REG_SYM(__addsf3),
+    REG_SYM(__divdf3),
+    REG_SYM(__extendsfdf2),
+    REG_SYM(__fixdfsi),
+    REG_SYM(__floatsidf),
+    REG_SYM(__floatsisf),
+    REG_SYM(__floatunsidf),
+    REG_SYM(__floatunsisf),
+    REG_SYM(__muldf3),
+    REG_SYM(__mulsf3),
+    REG_SYM(__subdf3),
+    REG_SYM(__subsf3),
+    REG_SYM(__truncdfsf2),
+    REG_SYM(__unorddf2),
+    REG_SYM(__unordsf2),
+#endif
     /* clang-format on */
     REG_SYM(__aeabi_d2iz),
     REG_SYM(__aeabi_d2lz),
@@ -133,26 +149,19 @@ static SymbolMap target_sym_map[] = {
     REG_SYM(__aeabi_uldivmod),
     REG_SYM(__ashldi3),
     REG_SYM(__clzsi2),
-    REG_SYM(__divdf3),
     REG_SYM(__divdi3),
     REG_SYM(__divsi3),
     REG_SYM(__eqdf2),
     REG_SYM(__eqsf2),
-    REG_SYM(__extendsfdf2),
     REG_SYM(__fixdfdi),
-    REG_SYM(__fixdfsi),
     REG_SYM(__fixsfdi),
     REG_SYM(__fixunsdfdi),
     REG_SYM(__fixunsdfsi),
     REG_SYM(__fixunssfdi),
     REG_SYM(__floatdidf),
     REG_SYM(__floatdisf),
-    REG_SYM(__floatsidf),
-    REG_SYM(__floatsisf),
     REG_SYM(__floatundidf),
     REG_SYM(__floatundisf),
-    REG_SYM(__floatunsidf),
-    REG_SYM(__floatunsisf),
     REG_SYM(__gedf2),
     REG_SYM(__gesf2),
     REG_SYM(__gtdf2),
@@ -164,21 +173,14 @@ static SymbolMap target_sym_map[] = {
     REG_SYM(__ltsf2),
     REG_SYM(__moddi3),
     REG_SYM(__modsi3),
-    REG_SYM(__muldf3),
     REG_SYM(__muldi3),
-    REG_SYM(__mulsf3),
     REG_SYM(__nedf2),
     REG_SYM(__nesf2),
-    REG_SYM(__subdf3),
-    REG_SYM(__subsf3),
-    REG_SYM(__truncdfsf2),
     REG_SYM(__udivdi3),
     REG_SYM(__udivmoddi4),
     REG_SYM(__udivsi3),
     REG_SYM(__umoddi3),
     REG_SYM(__umodsi3),
-    REG_SYM(__unorddf2),
-    REG_SYM(__unordsf2),
 };
 
 static void
@@ -375,7 +377,7 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr,
             if (error_buf != NULL)
                 snprintf(error_buf, error_buf_size,
                          "Load relocation section failed: "
-                         "invalid relocation type %d.",
+                         "invalid relocation type %" PRId32 ".",
                          reloc_type);
             return false;
     }

+ 4 - 118
core/iwasm/common/wasm_application.c

@@ -107,9 +107,6 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
        the actual main function. Directly calling main function
        may cause exception thrown. */
     if ((func = wasm_runtime_lookup_wasi_start_function(module_inst))) {
-#if WASM_ENABLE_DEBUG_INTERP != 0
-        wasm_runtime_start_debug_instance(exec_env);
-#endif
         return wasm_runtime_call_wasm(exec_env, func, 0, NULL);
     }
 #endif /* end of WASM_ENABLE_LIBC_WASI */
@@ -191,10 +188,6 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
             (uint32)wasm_runtime_addr_native_to_app(module_inst, argv_offsets);
     }
 
-#if WASM_ENABLE_DEBUG_INTERP != 0
-    wasm_runtime_start_debug_instance(exec_env);
-#endif
-
     ret = wasm_runtime_call_wasm(exec_env, func, argc1, argv1);
     if (ret && func_type->result_count > 0 && argc > 0 && argv)
         /* copy the return value */
@@ -240,113 +233,10 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
     return (ret && !wasm_runtime_get_exception(module_inst)) ? true : false;
 }
 
-#if WASM_ENABLE_MULTI_MODULE != 0
-static WASMModuleInstance *
-get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
-                    const char *sub_module_name)
-{
-    WASMSubModInstNode *node =
-        bh_list_first_elem(parent_module_inst->sub_module_inst_list);
-
-    while (node && strcmp(node->module_name, sub_module_name)) {
-        node = bh_list_elem_next(node);
-    }
-    return node ? node->module_inst : NULL;
-}
-
-static bool
-parse_function_name(char *orig_function_name, char **p_module_name,
-                    char **p_function_name)
-{
-    if (orig_function_name[0] != '$') {
-        *p_module_name = NULL;
-        *p_function_name = orig_function_name;
-        return true;
-    }
-
-    /**
-     * $module_name$function_name\0
-     *  ===>
-     * module_name\0function_name\0
-     *  ===>
-     * module_name
-     * function_name
-     */
-    char *p1 = orig_function_name;
-    char *p2 = strchr(p1 + 1, '$');
-    if (!p2) {
-        LOG_DEBUG("can not parse the incoming function name");
-        return false;
-    }
-
-    *p_module_name = p1 + 1;
-    *p2 = '\0';
-    *p_function_name = p2 + 1;
-    return strlen(*p_module_name) && strlen(*p_function_name);
-}
-#endif
-
 /**
  * Implementation of wasm_application_execute_func()
  */
 
-static bool
-resolve_function(WASMModuleInstanceCommon *module_inst, const char *name,
-                 WASMFunctionInstanceCommon **out_func,
-                 WASMModuleInstanceCommon **out_module_inst)
-{
-    WASMFunctionInstanceCommon *target_func = NULL;
-    WASMModuleInstanceCommon *target_inst = NULL;
-
-#if WASM_ENABLE_MULTI_MODULE != 0
-    char *function_name = NULL;
-    char *orig_name = NULL;
-    char *sub_module_name = NULL;
-    uint32 length = (uint32)(strlen(name) + 1);
-
-    orig_name = runtime_malloc(sizeof(char) * length, NULL, NULL, 0);
-    if (!orig_name) {
-        goto LEAVE;
-    }
-
-    strncpy(orig_name, name, length);
-
-    if (!parse_function_name(orig_name, &sub_module_name, &function_name)) {
-        goto LEAVE;
-    }
-
-    LOG_DEBUG("%s -> %s and %s", name, sub_module_name, function_name);
-
-    if (sub_module_name) {
-        target_inst = (WASMModuleInstanceCommon *)get_sub_module_inst(
-            (WASMModuleInstance *)module_inst, sub_module_name);
-        if (!target_inst) {
-            LOG_DEBUG("can not find a sub module named %s", sub_module_name);
-            goto LEAVE;
-        }
-    }
-    else {
-        target_inst = module_inst;
-    }
-#else
-    const char *function_name = name;
-    target_inst = module_inst;
-#endif
-
-    target_func =
-        wasm_runtime_lookup_function(target_inst, function_name, NULL);
-
-#if WASM_ENABLE_MULTI_MODULE != 0
-LEAVE:
-    if (orig_name)
-        wasm_runtime_free(orig_name);
-#endif
-
-    *out_func = target_func;
-    *out_module_inst = target_inst;
-    return target_func;
-}
-
 union ieee754_float {
     float f;
 
@@ -393,7 +283,6 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name,
              int32 argc, char *argv[])
 {
     WASMFunctionInstanceCommon *target_func;
-    WASMModuleInstanceCommon *target_inst;
     WASMType *type = NULL;
     WASMExecEnv *exec_env = NULL;
     uint32 argc1, *argv1 = NULL, cell_num = 0, j, k = 0;
@@ -408,13 +297,14 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name,
     bh_assert(argc >= 0);
     LOG_DEBUG("call a function \"%s\" with %d arguments", name, argc);
 
-    if (!resolve_function(module_inst, name, &target_func, &target_inst)) {
+    if (!(target_func =
+              wasm_runtime_lookup_function(module_inst, name, NULL))) {
         snprintf(buf, sizeof(buf), "lookup function %s failed", name);
         wasm_runtime_set_exception(module_inst, buf);
         goto fail;
     }
 
-    module_type = target_inst->module_type;
+    module_type = module_inst->module_type;
     type = wasm_runtime_get_function_type(target_func, module_type);
 
     if (!type) {
@@ -446,7 +336,7 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name,
 #endif
 
     total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
-    if ((!(argv1 = runtime_malloc((uint32)total_size, target_inst, NULL, 0)))) {
+    if ((!(argv1 = runtime_malloc((uint32)total_size, module_inst, NULL, 0)))) {
         goto fail;
     }
 
@@ -618,10 +508,6 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name,
         goto fail;
     }
 
-#if WASM_ENABLE_DEBUG_INTERP != 0
-    wasm_runtime_start_debug_instance(exec_env);
-#endif
-
     if (!wasm_runtime_call_wasm(exec_env, target_func, argc1, argv1)) {
         goto fail;
     }

+ 20 - 2
core/iwasm/common/wasm_memory.c

@@ -75,8 +75,17 @@ wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
 void
 wasm_runtime_memory_destroy()
 {
-    if (memory_mode == MEMORY_MODE_POOL)
-        mem_allocator_destroy(pool_allocator);
+    if (memory_mode == MEMORY_MODE_POOL) {
+#if BH_ENABLE_GC_VERIFY == 0
+        (void)mem_allocator_destroy(pool_allocator);
+#else
+        int ret = mem_allocator_destroy(pool_allocator);
+        if (ret != 0) {
+            /* Memory leak detected */
+            exit(-1);
+        }
+#endif
+    }
     memory_mode = MEMORY_MODE_UNKNOWN;
 }
 
@@ -167,3 +176,12 @@ wasm_runtime_free(void *ptr)
 {
     wasm_runtime_free_internal(ptr);
 }
+
+bool
+wasm_runtime_get_mem_alloc_info(mem_alloc_info_t *mem_alloc_info)
+{
+    if (memory_mode == MEMORY_MODE_POOL) {
+        return mem_allocator_get_alloc_info(pool_allocator, mem_alloc_info);
+    }
+    return false;
+}

+ 23 - 9
core/iwasm/common/wasm_native.c

@@ -53,6 +53,9 @@ get_lib_pthread_export_apis(NativeSymbol **p_lib_pthread_apis);
 uint32
 get_libc_emcc_export_apis(NativeSymbol **p_libc_emcc_apis);
 
+uint32
+get_lib_rats_export_apis(NativeSymbol **p_lib_rats_apis);
+
 static bool
 compare_type_with_signautre(uint8 type, const char signature)
 {
@@ -359,24 +362,24 @@ wasm_native_init()
 #if WASM_ENABLE_LIBC_BUILTIN != 0
     n_native_symbols = get_libc_builtin_export_apis(&native_symbols);
     if (!wasm_native_register_natives("env", native_symbols, n_native_symbols))
-        return false;
+        goto fail;
 #endif /* WASM_ENABLE_LIBC_BUILTIN */
 
 #if WASM_ENABLE_SPEC_TEST
     n_native_symbols = get_spectest_export_apis(&native_symbols);
     if (!wasm_native_register_natives("spectest", native_symbols,
                                       n_native_symbols))
-        return false;
+        goto fail;
 #endif /* WASM_ENABLE_SPEC_TEST */
 
 #if WASM_ENABLE_LIBC_WASI != 0
     n_native_symbols = get_libc_wasi_export_apis(&native_symbols);
     if (!wasm_native_register_natives("wasi_unstable", native_symbols,
                                       n_native_symbols))
-        return false;
+        goto fail;
     if (!wasm_native_register_natives("wasi_snapshot_preview1", native_symbols,
                                       n_native_symbols))
-        return false;
+        goto fail;
 #endif
 
 #if WASM_ENABLE_BASE_LIB != 0
@@ -384,7 +387,7 @@ wasm_native_init()
     if (n_native_symbols > 0
         && !wasm_native_register_natives("env", native_symbols,
                                          n_native_symbols))
-        return false;
+        goto fail;
 #endif
 
 #if WASM_ENABLE_APP_FRAMEWORK != 0
@@ -392,18 +395,18 @@ wasm_native_init()
     if (n_native_symbols > 0
         && !wasm_native_register_natives("env", native_symbols,
                                          n_native_symbols))
-        return false;
+        goto fail;
 #endif
 
 #if WASM_ENABLE_LIB_PTHREAD != 0
     if (!lib_pthread_init())
-        return false;
+        goto fail;
 
     n_native_symbols = get_lib_pthread_export_apis(&native_symbols);
     if (n_native_symbols > 0
         && !wasm_native_register_natives("env", native_symbols,
                                          n_native_symbols))
-        return false;
+        goto fail;
 #endif
 
 #if WASM_ENABLE_LIBC_EMCC != 0
@@ -411,10 +414,21 @@ wasm_native_init()
     if (n_native_symbols > 0
         && !wasm_native_register_natives("env", native_symbols,
                                          n_native_symbols))
-        return false;
+        goto fail;
 #endif /* WASM_ENABLE_LIBC_EMCC */
 
+#if WASM_ENABLE_LIB_RATS != 0
+    n_native_symbols = get_lib_rats_export_apis(&native_symbols);
+    if (n_native_symbols > 0
+        && !wasm_native_register_natives("env", native_symbols,
+                                         n_native_symbols))
+        goto fail;
+#endif /* WASM_ENABLE_LIB_RATS */
+
     return true;
+fail:
+    wasm_native_destroy();
+    return false;
 }
 
 void

+ 16 - 7
core/iwasm/common/wasm_runtime_common.c

@@ -341,10 +341,6 @@ wasm_runtime_init()
 void
 wasm_runtime_destroy()
 {
-#if WASM_ENABLE_FAST_JIT != 0
-    jit_compiler_destroy();
-#endif
-
 #if WASM_ENABLE_REF_TYPES != 0
     wasm_externref_map_destroy();
 #endif
@@ -368,6 +364,14 @@ wasm_runtime_destroy()
     os_mutex_destroy(&registered_module_list_lock);
 #endif
 
+#if WASM_ENABLE_FAST_JIT != 0
+    /* Destroy Fast-JIT compiler after destroying the modules
+     * loaded by multi-module feature, since the Fast JIT's
+     * code cache allocator may be used by these modules.
+     */
+    jit_compiler_destroy();
+#endif
+
 #if WASM_ENABLE_SHARED_MEMORY
     wasm_shared_memory_destroy();
 #endif
@@ -404,7 +408,6 @@ wasm_runtime_full_init(RuntimeInitArgs *init_args)
 #if WASM_ENABLE_DEBUG_INTERP != 0
     if (strlen(init_args->ip_addr))
         if (!wasm_debug_engine_init(init_args->ip_addr,
-                                    init_args->platform_port,
                                     init_args->instance_port)) {
             wasm_runtime_destroy();
             return false;
@@ -504,7 +507,7 @@ wasm_runtime_is_xip_file(const uint8 *buf, uint32 size)
 
 #if (WASM_ENABLE_THREAD_MGR != 0) && (WASM_ENABLE_DEBUG_INTERP != 0)
 uint32
-wasm_runtime_start_debug_instance(WASMExecEnv *exec_env)
+wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env, int32_t port)
 {
     WASMModuleInstanceCommon *module_inst =
         wasm_runtime_get_module_inst(exec_env);
@@ -522,12 +525,18 @@ wasm_runtime_start_debug_instance(WASMExecEnv *exec_env)
         return cluster->debug_inst->control_thread->port;
     }
 
-    if (wasm_debug_instance_create(cluster)) {
+    if (wasm_debug_instance_create(cluster, port)) {
         return cluster->debug_inst->control_thread->port;
     }
 
     return 0;
 }
+
+uint32
+wasm_runtime_start_debug_instance(WASMExecEnv *exec_env)
+{
+    return wasm_runtime_start_debug_instance_with_port(exec_env, -1);
+}
 #endif
 
 #if WASM_ENABLE_MULTI_MODULE != 0

+ 5 - 0
core/iwasm/common/wasm_runtime_common.h

@@ -565,6 +565,11 @@ wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
                          uint32 num_args, ...);
 
 #if WASM_ENABLE_DEBUG_INTERP != 0
+/* See wasm_export.h for description */
+WASM_RUNTIME_API_EXTERN uint32
+wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env,
+                                            int32_t port);
+
 /* See wasm_export.h for description */
 WASM_RUNTIME_API_EXTERN uint32
 wasm_runtime_start_debug_instance(WASMExecEnv *exec_env);

+ 3 - 3
core/iwasm/compilation/simd/simd_conversions.c

@@ -135,9 +135,9 @@ simd_integer_narrow_common(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
         LLVM_CONST(i32_fourteen), LLVM_CONST(i32_fifteen),
     };
 
-    if (!(trunc_type[0] == LLVMVectorType(INT8_TYPE, 8))
-        || !(trunc_type[1] == LLVMVectorType(INT16_TYPE, 4))
-        || !(trunc_type[2] == LLVMVectorType(I32_TYPE, 2))) {
+    if (!(trunc_type[0] = LLVMVectorType(INT8_TYPE, 8))
+        || !(trunc_type[1] = LLVMVectorType(INT16_TYPE, 4))
+        || !(trunc_type[2] = LLVMVectorType(I32_TYPE, 2))) {
         HANDLE_FAILURE("LLVMVectorType");
         return false;
     }

+ 5 - 1
core/iwasm/fast-jit/fe/jit_emit_control.c

@@ -807,7 +807,11 @@ jit_compile_op_block(JitCompContext *cc, uint8 **p_frame_ip,
 
     return true;
 fail:
-    jit_block_destroy(block);
+    /* Only destroy the block if it hasn't been pushed into
+      the block stack, or if will be destroyed again when
+      destroying the block stack */
+    if (jit_block_stack_top(&cc->block_stack) != block)
+        jit_block_destroy(block);
     return false;
 }
 

+ 64 - 13
core/iwasm/fast-jit/fe/jit_emit_numberic.c

@@ -539,8 +539,13 @@ compile_int_div_no_check(JitCompContext *cc, IntArithmetic arith_op,
                     insn = GEN_INSN(DIV_U, rax_hreg, rax_hreg, right);
             }
 
-            jit_lock_reg_in_insn(cc, insn, eax_hreg);
-            jit_lock_reg_in_insn(cc, insn, edx_hreg);
+            if (!insn) {
+                goto fail;
+            }
+            if (!jit_lock_reg_in_insn(cc, insn, eax_hreg)
+                || !jit_lock_reg_in_insn(cc, insn, edx_hreg)) {
+                goto fail;
+            }
 
             if (is_i32) {
                 res = jit_cc_new_reg_I32(cc);
@@ -551,9 +556,12 @@ compile_int_div_no_check(JitCompContext *cc, IntArithmetic arith_op,
                 insn1 = jit_insn_new_MOV(res, rax_hreg);
             }
 
-            if (insn && insn1) {
-                jit_insn_insert_after(insn, insn1);
+            if (!insn1) {
+                jit_set_last_error(cc, "generate insn failed");
+                goto fail;
             }
+
+            jit_insn_insert_after(insn, insn1);
             break;
         }
         case INT_REM_S:
@@ -576,8 +584,13 @@ compile_int_div_no_check(JitCompContext *cc, IntArithmetic arith_op,
                     insn = GEN_INSN(REM_U, rdx_hreg, rax_hreg, right);
             }
 
-            jit_lock_reg_in_insn(cc, insn, eax_hreg);
-            jit_lock_reg_in_insn(cc, insn, edx_hreg);
+            if (!insn) {
+                goto fail;
+            }
+            if (!jit_lock_reg_in_insn(cc, insn, eax_hreg)
+                || !jit_lock_reg_in_insn(cc, insn, edx_hreg)) {
+                goto fail;
+            }
 
             if (is_i32) {
                 res = jit_cc_new_reg_I32(cc);
@@ -588,9 +601,12 @@ compile_int_div_no_check(JitCompContext *cc, IntArithmetic arith_op,
                 insn1 = jit_insn_new_MOV(res, rdx_hreg);
             }
 
-            if (insn && insn1) {
-                jit_insn_insert_after(insn, insn1);
+            if (!insn1) {
+                jit_set_last_error(cc, "generate insn failed");
+                goto fail;
             }
+
+            jit_insn_insert_after(insn, insn1);
             break;
         }
 #else
@@ -1133,13 +1149,20 @@ compile_int_shl(JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
 #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
     GEN_INSN(MOV, is_i32 ? ecx_hreg : rcx_hreg, right);
     insn = GEN_INSN(SHL, res, left, is_i32 ? ecx_hreg : rcx_hreg);
-    jit_lock_reg_in_insn(cc, insn, ecx_hreg);
+    if (jit_get_last_error(cc) || !jit_lock_reg_in_insn(cc, insn, ecx_hreg)) {
+        goto fail;
+    }
 #else
     GEN_INSN(SHL, res, left, right);
+    if (jit_get_last_error(cc)) {
+        goto fail;
+    }
 #endif
 
 shortcut:
     return res;
+fail:
+    return (JitReg)0;
 }
 
 static JitReg
@@ -1164,13 +1187,20 @@ compile_int_shrs(JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
 #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
     GEN_INSN(MOV, is_i32 ? ecx_hreg : rcx_hreg, right);
     insn = GEN_INSN(SHRS, res, left, is_i32 ? ecx_hreg : rcx_hreg);
-    jit_lock_reg_in_insn(cc, insn, ecx_hreg);
+    if (jit_get_last_error(cc) || !jit_lock_reg_in_insn(cc, insn, ecx_hreg)) {
+        goto fail;
+    }
 #else
     GEN_INSN(SHRS, res, left, right);
+    if (jit_get_last_error(cc)) {
+        goto fail;
+    }
 #endif
 
 shortcut:
     return res;
+fail:
+    return (JitReg)0;
 }
 
 static JitReg
@@ -1195,13 +1225,20 @@ compile_int_shru(JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
 #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
     GEN_INSN(MOV, is_i32 ? ecx_hreg : rcx_hreg, right);
     insn = GEN_INSN(SHRU, res, left, is_i32 ? ecx_hreg : rcx_hreg);
-    jit_lock_reg_in_insn(cc, insn, ecx_hreg);
+    if (jit_get_last_error(cc) || !jit_lock_reg_in_insn(cc, insn, ecx_hreg)) {
+        goto fail;
+    }
 #else
     GEN_INSN(SHRU, res, left, right);
+    if (jit_get_last_error(cc)) {
+        goto fail;
+    }
 #endif
 
 shortcut:
     return res;
+fail:
+    return (JitReg)0;
 }
 
 DEF_UNI_INT_CONST_OPS(rotl)
@@ -1257,13 +1294,20 @@ compile_int_rotl(JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
 #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
     GEN_INSN(MOV, is_i32 ? ecx_hreg : rcx_hreg, right);
     insn = GEN_INSN(ROTL, res, left, is_i32 ? ecx_hreg : rcx_hreg);
-    jit_lock_reg_in_insn(cc, insn, ecx_hreg);
+    if (jit_get_last_error(cc) || !jit_lock_reg_in_insn(cc, insn, ecx_hreg)) {
+        goto fail;
+    }
 #else
     GEN_INSN(ROTL, res, left, right);
+    if (jit_get_last_error(cc)) {
+        goto fail;
+    }
 #endif
 
 shortcut:
     return res;
+fail:
+    return (JitReg)0;
 }
 
 DEF_UNI_INT_CONST_OPS(rotr)
@@ -1319,13 +1363,20 @@ compile_int_rotr(JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
 #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
     GEN_INSN(MOV, is_i32 ? ecx_hreg : rcx_hreg, right);
     insn = GEN_INSN(ROTR, res, left, is_i32 ? ecx_hreg : rcx_hreg);
-    jit_lock_reg_in_insn(cc, insn, ecx_hreg);
+    if (jit_get_last_error(cc) || !jit_lock_reg_in_insn(cc, insn, ecx_hreg)) {
+        goto fail;
+    }
 #else
     GEN_INSN(ROTR, res, left, right);
+    if (jit_get_last_error(cc)) {
+        goto fail;
+    }
 #endif
 
 shortcut:
     return res;
+fail:
+    return (JitReg)0;
 }
 
 static bool

+ 1 - 1
core/iwasm/fast-jit/jit_compiler.c

@@ -64,7 +64,7 @@ apply_compiler_passes(JitCompContext *cc)
         cc->cur_pass_no = p - jit_globals.passes;
         bh_assert(*p < COMPILER_PASS_NUM);
 
-        if (!compiler_passes[*p].run(cc)) {
+        if (!compiler_passes[*p].run(cc) || jit_get_last_error(cc)) {
             LOG_VERBOSE("JIT: compilation failed at pass[%td] = %s\n",
                         p - jit_globals.passes, compiler_passes[*p].name);
             return false;

+ 37 - 0
core/iwasm/fast-jit/jit_frontend.c

@@ -70,6 +70,21 @@ get_module_reg(JitFrame *frame)
     return frame->module_reg;
 }
 
+JitReg
+get_import_func_ptrs_reg(JitFrame *frame)
+{
+    JitCompContext *cc = frame->cc;
+    JitReg module_inst_reg = get_module_inst_reg(frame);
+
+    if (!frame->import_func_ptrs_reg) {
+        frame->import_func_ptrs_reg = cc->import_func_ptrs_reg;
+        GEN_INSN(
+            LDPTR, frame->import_func_ptrs_reg, module_inst_reg,
+            NEW_CONST(I32, offsetof(WASMModuleInstance, import_func_ptrs)));
+    }
+    return frame->import_func_ptrs_reg;
+}
+
 JitReg
 get_fast_jit_func_ptrs_reg(JitFrame *frame)
 {
@@ -85,6 +100,21 @@ get_fast_jit_func_ptrs_reg(JitFrame *frame)
     return frame->fast_jit_func_ptrs_reg;
 }
 
+JitReg
+get_func_type_indexes_reg(JitFrame *frame)
+{
+    JitCompContext *cc = frame->cc;
+    JitReg module_inst_reg = get_module_inst_reg(frame);
+
+    if (!frame->func_type_indexes_reg) {
+        frame->func_type_indexes_reg = cc->func_type_indexes_reg;
+        GEN_INSN(
+            LDPTR, frame->func_type_indexes_reg, module_inst_reg,
+            NEW_CONST(I32, offsetof(WASMModuleInstance, func_type_indexes)));
+    }
+    return frame->func_type_indexes_reg;
+}
+
 JitReg
 get_global_data_reg(JitFrame *frame)
 {
@@ -376,7 +406,9 @@ clear_fixed_virtual_regs(JitFrame *frame)
 
     frame->module_inst_reg = 0;
     frame->module_reg = 0;
+    frame->import_func_ptrs_reg = 0;
     frame->fast_jit_func_ptrs_reg = 0;
+    frame->func_type_indexes_reg = 0;
     frame->global_data_reg = 0;
     frame->aux_stack_bound_reg = 0;
     frame->aux_stack_bottom_reg = 0;
@@ -572,7 +604,9 @@ create_fixed_virtual_regs(JitCompContext *cc)
 
     cc->module_inst_reg = jit_cc_new_reg_ptr(cc);
     cc->module_reg = jit_cc_new_reg_ptr(cc);
+    cc->import_func_ptrs_reg = jit_cc_new_reg_ptr(cc);
     cc->fast_jit_func_ptrs_reg = jit_cc_new_reg_ptr(cc);
+    cc->func_type_indexes_reg = jit_cc_new_reg_ptr(cc);
     cc->global_data_reg = jit_cc_new_reg_ptr(cc);
     cc->aux_stack_bound_reg = jit_cc_new_reg_I32(cc);
     cc->aux_stack_bottom_reg = jit_cc_new_reg_I32(cc);
@@ -697,6 +731,9 @@ form_and_translate_func(JitCompContext *cc)
                 *(jit_annl_end_bcip(cc, cc->exit_label)) =
                     cc->cur_wasm_module->load_addr;
 
+    if (jit_get_last_error(cc)) {
+        return false;
+    }
     return true;
 }
 

+ 8 - 0
core/iwasm/fast-jit/jit_frontend.h

@@ -187,9 +187,15 @@ get_module_inst_reg(JitFrame *frame);
 JitReg
 get_module_reg(JitFrame *frame);
 
+JitReg
+get_import_func_ptrs_reg(JitFrame *frame);
+
 JitReg
 get_fast_jit_func_ptrs_reg(JitFrame *frame);
 
+JitReg
+get_func_type_indexes_reg(JitFrame *frame);
+
 JitReg
 get_global_data_reg(JitFrame *frame);
 
@@ -507,6 +513,8 @@ set_local_f64(JitFrame *frame, int n, JitReg val)
 
 #define PUSH(jit_value, value_type)                        \
     do {                                                   \
+        if (!jit_value)                                    \
+            goto fail;                                     \
         if (!jit_cc_push_value(cc, value_type, jit_value)) \
             goto fail;                                     \
     } while (0)

+ 4 - 2
core/iwasm/fast-jit/jit_ir.c

@@ -474,8 +474,10 @@ jit_cc_destroy(JitCompContext *cc)
     }
 
     /* Release entry and exit blocks.  */
-    jit_basic_block_delete(jit_cc_entry_basic_block(cc));
-    jit_basic_block_delete(jit_cc_exit_basic_block(cc));
+    if (0 != cc->entry_label)
+        jit_basic_block_delete(jit_cc_entry_basic_block(cc));
+    if (0 != cc->exit_label)
+        jit_basic_block_delete(jit_cc_exit_basic_block(cc));
 
     /* clang-format off */
     /* Release blocks and instructions.  */

+ 8 - 0
core/iwasm/fast-jit/jit_ir.h

@@ -909,8 +909,12 @@ typedef struct JitFrame {
     JitReg module_inst_reg;
     /* WASM module */
     JitReg module_reg;
+    /* module_inst->import_func_ptrs */
+    JitReg import_func_ptrs_reg;
     /* module_inst->fast_jit_func_ptrs */
     JitReg fast_jit_func_ptrs_reg;
+    /* module_inst->func_type_indexes */
+    JitReg func_type_indexes_reg;
     /* Base address of global data */
     JitReg global_data_reg;
     /* Boundary of auxiliary stack */
@@ -1027,8 +1031,12 @@ typedef struct JitCompContext {
     JitReg module_inst_reg;
     /* WASM module */
     JitReg module_reg;
+    /* module_inst->import_func_ptrs */
+    JitReg import_func_ptrs_reg;
     /* module_inst->fast_jit_func_ptrs */
     JitReg fast_jit_func_ptrs_reg;
+    /* module_inst->func_type_indexes */
+    JitReg func_type_indexes_reg;
     /* Base address of global data */
     JitReg global_data_reg;
     /* Boundary of auxiliary stack */

+ 30 - 10
core/iwasm/include/wasm_export.h

@@ -121,6 +121,13 @@ typedef union MemAllocOption {
 } MemAllocOption;
 #endif
 
+/* Memory pool info  */
+typedef struct mem_alloc_info_t {
+    uint32_t total_size;
+    uint32_t total_free_size;
+    uint32_t highmark_size;
+} mem_alloc_info_t;
+
 /* WASM runtime initialize arguments */
 typedef struct RuntimeInitArgs {
     mem_alloc_type_t mem_alloc_type;
@@ -137,7 +144,7 @@ typedef struct RuntimeInitArgs {
     /* Debug settings, only used when
        WASM_ENABLE_DEBUG_INTERP != 0 */
     char ip_addr[128];
-    int platform_port;
+    int unused; /* was platform_port */
     int instance_port;
 
     /* Fast JIT code cache size */
@@ -229,6 +236,12 @@ wasm_runtime_realloc(void *ptr, unsigned int size);
 WASM_RUNTIME_API_EXTERN void
 wasm_runtime_free(void *ptr);
 
+/*
+ * Get memory info, only pool mode is supported now.
+ */
+WASM_RUNTIME_API_EXTERN bool
+wasm_runtime_get_mem_alloc_info(mem_alloc_info_t *mem_alloc_info);
+
 /**
  * Get the package type of a buffer.
  *
@@ -252,20 +265,18 @@ WASM_RUNTIME_API_EXTERN bool
 wasm_runtime_is_xip_file(const uint8_t *buf, uint32_t size);
 
 /**
- * It is a callback for WAMR providing by embedding to load a module file
- * into a buffer
+ * Callback to load a module file into a buffer in multi-module feature
  */
 typedef bool (*module_reader)(const char *module_name,
                               uint8_t **p_buffer, uint32_t *p_size);
 
 /**
- * It is a callback for WAMR providing by embedding to release the buffer which
- * is used by loading a module file
+ * Callback to release the buffer loaded by module_reader callback
  */
 typedef void (*module_destroyer)(uint8_t *buffer, uint32_t size);
 
 /**
- * To setup callbacks for reading and releasing a buffer about a module file
+ * Setup callbacks for reading and releasing a buffer about a module file
  *
  * @param reader a callback to read a module file into a buffer
  * @param destroyer a callback to release above buffer
@@ -275,7 +286,7 @@ wasm_runtime_set_module_reader(const module_reader reader,
                                const module_destroyer destroyer);
 /**
  * Give the "module" a name "module_name".
- * can not assign a new name to a module if it already has a name
+ * Can not assign a new name to a module if it already has a name
  *
  * @param module_name indicate a name
  * @param module the target module
@@ -289,8 +300,8 @@ wasm_runtime_register_module(const char *module_name, wasm_module_t module,
                              char *error_buf, uint32_t error_buf_size);
 
 /**
- * To check if there is already a loaded module named module_name in the
- * runtime. you will not want to load repeately
+ * Check if there is already a loaded module named module_name in the
+ * runtime. Repeately loading a module with the same name is not allowed.
  *
  * @param module_name indicate a name
  *
@@ -520,10 +531,19 @@ wasm_runtime_get_exec_env_singleton(wasm_module_inst_t module_inst);
  *   they are sharing the same cluster with the main exec_env.
  *
  * @param exec_env the execution environment to start debug instance
+ * @param port     the port for the debug server to listen on.
+ *                 0 means automatic assignment.
+ *                 -1 means to use the global setting in RuntimeInitArgs.
  *
  * @return debug port if success, 0 otherwise.
  */
 WASM_RUNTIME_API_EXTERN uint32_t
+wasm_runtime_start_debug_instance_with_port(wasm_exec_env_t exec_env, int32_t port);
+
+/**
+ * Same as wasm_runtime_start_debug_instance_with_port(env, -1).
+ */
+WASM_RUNTIME_API_EXTERN uint32_t
 wasm_runtime_start_debug_instance(wasm_exec_env_t exec_env);
 
 /**
@@ -1042,7 +1062,7 @@ wasm_runtime_spawn_thread(wasm_exec_env_t exec_env, wasm_thread_t *tid,
                           wasm_thread_callback_t callback, void *arg);
 
 /**
- * Waits a spawned thread to terminate
+ * Wait a spawned thread to terminate
  *
  * @param tid thread id
  * @param retval if not NULL, output the return value of the thread

+ 1 - 0
core/iwasm/interpreter/wasm.h

@@ -29,6 +29,7 @@ extern "C" {
 #define VALUE_TYPE_ANY 0x42
 
 #define DEFAULT_NUM_BYTES_PER_PAGE 65536
+#define DEFAULT_MAX_PAGES 65536
 
 #define NULL_REF (0xFFFFFFFF)
 

+ 27 - 16
core/iwasm/interpreter/wasm_loader.c

@@ -1266,7 +1266,7 @@ fail:
 static bool
 check_memory_init_size(uint32 init_size, char *error_buf, uint32 error_buf_size)
 {
-    if (init_size > 65536) {
+    if (init_size > DEFAULT_MAX_PAGES) {
         set_error_buf(error_buf, error_buf_size,
                       "memory size must be at most 65536 pages (4GiB)");
         return false;
@@ -1284,7 +1284,7 @@ check_memory_max_size(uint32 init_size, uint32 max_size, char *error_buf,
         return false;
     }
 
-    if (max_size > 65536) {
+    if (max_size > DEFAULT_MAX_PAGES) {
         set_error_buf(error_buf, error_buf_size,
                       "memory size must be at most 65536 pages (4GiB)");
         return false;
@@ -1299,12 +1299,12 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
                    char *error_buf, uint32 error_buf_size)
 {
     const uint8 *p = *p_buf, *p_end = buf_end;
-    uint32 pool_size = wasm_runtime_memory_pool_size();
 #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 = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
+    uint32 max_page_count = DEFAULT_MAX_PAGES;
 #endif /* WASM_ENABLE_APP_FRAMEWORK */
     uint32 declare_max_page_count_flag = 0;
     uint32 declare_init_page_count = 0;
@@ -1529,12 +1529,12 @@ 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;
-    uint32 pool_size = wasm_runtime_memory_pool_size();
 #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 = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
+    uint32 max_page_count = DEFAULT_MAX_PAGES;
 #endif
 
     p_org = p;
@@ -3317,17 +3317,30 @@ load_from_sections(WASMModule *module, WASMSection *sections,
 #if WASM_ENABLE_MULTI_MODULE == 0
         if (module->import_memory_count) {
             memory_import = &module->import_memories[0].u.memory;
-            /* Memory init page count cannot be larger than 65536, we don't
-               check integer overflow again. */
-            memory_import->num_bytes_per_page *= memory_import->init_page_count;
-            memory_import->init_page_count = memory_import->max_page_count = 1;
+            if (memory_import->init_page_count < DEFAULT_MAX_PAGES)
+                memory_import->num_bytes_per_page *=
+                    memory_import->init_page_count;
+            else
+                memory_import->num_bytes_per_page = UINT32_MAX;
+
+            if (memory_import->init_page_count > 0)
+                memory_import->init_page_count = memory_import->max_page_count =
+                    1;
+            else
+                memory_import->init_page_count = memory_import->max_page_count =
+                    0;
         }
         if (module->memory_count) {
-            /* Memory init page count cannot be larger than 65536, we don't
-               check integer overflow again. */
             memory = &module->memories[0];
-            memory->num_bytes_per_page *= memory->init_page_count;
-            memory->init_page_count = memory->max_page_count = 1;
+            if (memory->init_page_count < DEFAULT_MAX_PAGES)
+                memory->num_bytes_per_page *= memory->init_page_count;
+            else
+                memory->num_bytes_per_page = UINT32_MAX;
+
+            if (memory->init_page_count > 0)
+                memory->init_page_count = memory->max_page_count = 1;
+            else
+                memory->init_page_count = memory->max_page_count = 0;
         }
 #endif
     }
@@ -7300,7 +7313,6 @@ re_scan:
             }
 
             case WASM_OP_DROP:
-            case WASM_OP_DROP_64:
             {
                 BranchBlock *cur_block = loader_ctx->frame_csp - 1;
                 int32 available_stack_cell =
@@ -7368,7 +7380,6 @@ re_scan:
             }
 
             case WASM_OP_SELECT:
-            case WASM_OP_SELECT_64:
             {
                 uint8 ref_type;
                 BranchBlock *cur_block = loader_ctx->frame_csp - 1;

+ 25 - 14
core/iwasm/interpreter/wasm_mini_loader.c

@@ -534,12 +534,12 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
                    char *error_buf, uint32 error_buf_size)
 {
     const uint8 *p = *p_buf, *p_end = buf_end;
-    uint32 pool_size = wasm_runtime_memory_pool_size();
 #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 = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
+    uint32 max_page_count = DEFAULT_MAX_PAGES;
 #endif /* WASM_ENABLE_APP_FRAMEWORK */
     uint32 declare_max_page_count_flag = 0;
     uint32 declare_init_page_count = 0;
@@ -650,12 +650,12 @@ 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;
-    uint32 pool_size = wasm_runtime_memory_pool_size();
 #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 = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
+    uint32 max_page_count = DEFAULT_MAX_PAGES;
 #endif
 
     p_org = p;
@@ -2153,18 +2153,31 @@ load_from_sections(WASMModule *module, WASMSection *sections,
 
         if (module->import_memory_count) {
             memory_import = &module->import_memories[0].u.memory;
-            /* Memory init page count cannot be larger than 65536, we don't
-               check integer overflow again. */
-            memory_import->num_bytes_per_page *= memory_import->init_page_count;
-            memory_import->init_page_count = memory_import->max_page_count = 1;
+            if (memory_import->init_page_count < DEFAULT_MAX_PAGES)
+                memory_import->num_bytes_per_page *=
+                    memory_import->init_page_count;
+            else
+                memory_import->num_bytes_per_page = UINT32_MAX;
+
+            if (memory_import->init_page_count > 0)
+                memory_import->init_page_count = memory_import->max_page_count =
+                    1;
+            else
+                memory_import->init_page_count = memory_import->max_page_count =
+                    0;
         }
 
         if (module->memory_count) {
-            /* Memory init page count cannot be larger than 65536, we don't
-               check integer overflow again. */
             memory = &module->memories[0];
-            memory->num_bytes_per_page *= memory->init_page_count;
-            memory->init_page_count = memory->max_page_count = 1;
+            if (memory->init_page_count < DEFAULT_MAX_PAGES)
+                memory->num_bytes_per_page *= memory->init_page_count;
+            else
+                memory->num_bytes_per_page = UINT32_MAX;
+
+            if (memory->init_page_count > 0)
+                memory->init_page_count = memory->max_page_count = 1;
+            else
+                memory->init_page_count = memory->max_page_count = 0;
         }
     }
 
@@ -5506,7 +5519,6 @@ re_scan:
             }
 
             case WASM_OP_DROP:
-            case WASM_OP_DROP_64:
             {
                 BranchBlock *cur_block = loader_ctx->frame_csp - 1;
                 int32 available_stack_cell =
@@ -5559,7 +5571,6 @@ re_scan:
             }
 
             case WASM_OP_SELECT:
-            case WASM_OP_SELECT_64:
             {
                 uint8 ref_type;
                 BranchBlock *cur_block = loader_ctx->frame_csp - 1;

+ 170 - 87
core/iwasm/interpreter/wasm_runtime.c

@@ -109,8 +109,11 @@ memories_deinstantiate(WASMModuleInstance *module_inst,
         for (i = 0; i < count; i++) {
             if (memories[i]) {
 #if WASM_ENABLE_MULTI_MODULE != 0
-                if (i < module_inst->module->import_memory_count)
+                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 (memories[i]->is_shared) {
@@ -203,18 +206,27 @@ memory_instantiate(WASMModuleInstance *module_inst, uint32 num_bytes_per_page,
     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*/
+           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,
-                          "memory size must be at most 65536 pages (4GiB)");
+                          "failed to insert app heap into linear memory, "
+                          "try using `--heap_size=0` option");
             return NULL;
         }
     }
     else if (heap_size > 0) {
-        if (module->aux_heap_base_global_index != (uint32)-1
-            && module->aux_heap_base < num_bytes_per_page * init_page_count) {
+        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
+                        < 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;
@@ -256,13 +268,18 @@ memory_instantiate(WASMModuleInstance *module_inst, uint32 num_bytes_per_page,
         }
         init_page_count += inc_page_count;
         max_page_count += inc_page_count;
-        if (init_page_count > 65536) {
+        if (init_page_count > DEFAULT_MAX_PAGES) {
             set_error_buf(error_buf, error_buf_size,
-                          "memory size must be at most 65536 pages (4GiB)");
+                          "failed to insert app heap into linear memory, "
+                          "try using `--heap_size=0` option");
             return NULL;
         }
-        if (max_page_count > 65536)
-            max_page_count = 65536;
+        else if (init_page_count == DEFAULT_MAX_PAGES) {
+            num_bytes_per_page = UINT32_MAX;
+            init_page_count = max_page_count = 1;
+        }
+        if (max_page_count > DEFAULT_MAX_PAGES)
+            max_page_count = DEFAULT_MAX_PAGES;
     }
 
     LOG_VERBOSE("Memory instantiate:");
@@ -277,6 +294,7 @@ memory_instantiate(WASMModuleInstance *module_inst, uint32 num_bytes_per_page,
         memory_data_size = (uint64)num_bytes_per_page * max_page_count;
     }
 #endif
+    bh_assert(memory_data_size <= 4 * (uint64)BH_GB);
 
     /* Allocate memory space, addr data and global data */
     if (!(memory = runtime_malloc((uint64)sizeof(WASMMemoryInstance), error_buf,
@@ -298,9 +316,8 @@ memory_instantiate(WASMModuleInstance *module_inst, uint32 num_bytes_per_page,
      * both i and memarg.offset are u32 in range 0 to 4G
      * so the range of ea is 0 to 8G
      */
-    if (memory_data_size >= UINT32_MAX
-        || !(memory->memory_data = mapped_mem =
-                 os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE))) {
+    if (!(memory->memory_data = mapped_mem =
+              os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE))) {
         set_error_buf(error_buf, error_buf_size, "mmap memory failed");
         goto fail1;
     }
@@ -324,10 +341,14 @@ memory_instantiate(WASMModuleInstance *module_inst, uint32 num_bytes_per_page,
      * again here */
 #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
 
+    if (memory_data_size > UINT32_MAX)
+        memory_data_size = (uint32)memory_data_size;
+
     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 = (uint32)memory_data_size;
 
     memory->heap_data = memory->memory_data + heap_offset;
     memory->heap_data_end = memory->heap_data + heap_size;
@@ -1241,6 +1262,47 @@ check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf,
     return true;
 }
 
+#if WASM_ENABLE_FAST_JIT != 0
+static uint32
+get_smallest_type_idx(WASMModule *module, WASMType *func_type)
+{
+    uint32 i;
+
+    for (i = 0; i < module->type_count; i++) {
+        if (func_type == 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->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->function_count; i++) {
+        WASMFunctionInstance *func_inst = module_inst->functions + i;
+        WASMType *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
+
 /**
  * Instantiate module
  */
@@ -1363,6 +1425,10 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size,
             && !(module_inst->export_globals = export_globals_instantiate(
                      module, module_inst, module_inst->export_glob_count,
                      error_buf, error_buf_size)))
+#endif
+#if WASM_ENABLE_FAST_JIT != 0
+        || (module_inst->function_count > 0
+            && !init_func_type_indexes(module_inst, error_buf, error_buf_size))
 #endif
     ) {
         goto fail;
@@ -1693,6 +1759,11 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
     if (!module_inst)
         return;
 
+#if WASM_ENABLE_FAST_JIT != 0
+    if (module_inst->func_type_indexes)
+        wasm_runtime_free(module_inst->func_type_indexes);
+#endif
+
 #if WASM_ENABLE_MULTI_MODULE != 0
     sub_module_deinstantiate(module_inst);
 #endif
@@ -2162,27 +2233,11 @@ wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
         addr = mem_allocator_malloc(memory->heap_handle, size);
     }
     else if (module_inst->malloc_function && module_inst->free_function) {
-#if WASM_ENABLE_DEBUG_INTERP != 0
-        /* TODO: obviously, we can not create debug instance for
-         * module malloc here, so, just disable the engine here,
-         * it is strange, but we now are lack of ways to indicate
-         * which calls should not be debugged. And we have other
-         * execute_xxx_function may need to be taken care of
-         */
-        bool active = wasm_debug_get_engine_active();
-        wasm_debug_set_engine_active(false);
-#endif
         if (!execute_malloc_function(module_inst, module_inst->malloc_function,
                                      module_inst->retain_function, size,
                                      &offset)) {
-#if WASM_ENABLE_DEBUG_INTERP != 0
-            wasm_debug_set_engine_active(active);
-#endif
             return 0;
         }
-#if WASM_ENABLE_DEBUG_INTERP != 0
-        wasm_debug_set_engine_active(active);
-#endif
         /* If we use app's malloc function,
            the default memory may be changed while memory growing */
         memory = module_inst->default_memory;
@@ -2261,17 +2316,7 @@ wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
         else if (module_inst->malloc_function && module_inst->free_function
                  && memory->memory_data <= addr
                  && addr < memory->memory_data_end) {
-#if WASM_ENABLE_DEBUG_INTERP != 0
-            /*TODO: obviously, we can not create debug instance for module
-            malloc here, so, just disable the engine here, it is strange. the
-            wasm's call should be marshed to its own thread */
-            bool active = wasm_debug_get_engine_active();
-            wasm_debug_set_engine_active(false);
-#endif
             execute_free_function(module_inst, module_inst->free_function, ptr);
-#if WASM_ENABLE_DEBUG_INTERP != 0
-            wasm_debug_set_engine_active(active);
-#endif
         }
     }
 }
@@ -2418,39 +2463,49 @@ bool
 wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
 {
     WASMMemoryInstance *memory = module->default_memory;
-    uint8 *new_memory_data, *memory_data, *heap_data_old;
-    uint32 heap_size, total_size_old, total_page_count;
-    uint64 total_size;
+    uint8 *memory_data_old, *memory_data_new, *heap_data_old;
+    uint32 num_bytes_per_page, heap_size, total_size_old;
+    uint32 cur_page_count, max_page_count, total_page_count;
+    uint64 total_size_new;
     bool ret = true;
 
     if (!memory)
         return false;
 
-    memory_data = memory->memory_data;
-    heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
-    total_size_old = (uint32)(memory->memory_data_end - memory_data);
-    total_page_count = inc_page_count + memory->cur_page_count;
-    total_size = memory->num_bytes_per_page * (uint64)total_page_count;
     heap_data_old = memory->heap_data;
+    heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
+
+    memory_data_old = memory->memory_data;
+    total_size_old = memory->memory_data_size;
+
+    num_bytes_per_page = memory->num_bytes_per_page;
+    cur_page_count = memory->cur_page_count;
+    max_page_count = memory->max_page_count;
+    total_page_count = inc_page_count + cur_page_count;
+    total_size_new = num_bytes_per_page * (uint64)total_page_count;
 
     if (inc_page_count <= 0)
         /* No need to enlarge memory */
         return true;
 
-    if (total_page_count < memory->cur_page_count /* integer overflow */
-        || total_page_count > memory->max_page_count) {
+    if (total_page_count < cur_page_count /* integer overflow */
+        || total_page_count > max_page_count) {
         return false;
     }
 
-    if (total_size >= UINT32_MAX) {
-        return false;
+    bh_assert(total_size_new <= 4 * (uint64)BH_GB);
+    if (total_size_new > UINT32_MAX) {
+        /* Resize to 1 page with size 4G-1 */
+        num_bytes_per_page = UINT32_MAX;
+        total_page_count = max_page_count = 1;
+        total_size_new = UINT32_MAX;
     }
 
 #if WASM_ENABLE_SHARED_MEMORY != 0
     if (memory->is_shared) {
-        /* For shared memory, we have reserved the maximum spaces during
-            instantiate, only change the cur_page_count here */
+        memory->num_bytes_per_page = UINT32_MAX;
         memory->cur_page_count = total_page_count;
+        memory->max_page_count = max_page_count;
         return true;
     }
 #endif
@@ -2462,25 +2517,25 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
         }
     }
 
-    if (!(new_memory_data =
-              wasm_runtime_realloc(memory_data, (uint32)total_size))) {
-        if (!(new_memory_data = wasm_runtime_malloc((uint32)total_size))) {
+    if (!(memory_data_new =
+              wasm_runtime_realloc(memory_data_old, (uint32)total_size_new))) {
+        if (!(memory_data_new = wasm_runtime_malloc((uint32)total_size_new))) {
             return false;
         }
-        if (memory_data) {
-            bh_memcpy_s(new_memory_data, (uint32)total_size, memory_data,
-                        total_size_old);
-            wasm_runtime_free(memory_data);
+        if (memory_data_old) {
+            bh_memcpy_s(memory_data_new, (uint32)total_size_new,
+                        memory_data_old, total_size_old);
+            wasm_runtime_free(memory_data_old);
         }
     }
 
-    memset(new_memory_data + total_size_old, 0,
-           (uint32)total_size - total_size_old);
+    memset(memory_data_new + total_size_old, 0,
+           (uint32)total_size_new - total_size_old);
 
     if (heap_size > 0) {
         if (mem_allocator_migrate(memory->heap_handle,
                                   (char *)heap_data_old
-                                      + (new_memory_data - memory_data),
+                                      + (memory_data_new - memory_data_old),
                                   heap_size)
             != 0) {
             /* Don't return here as memory->memory_data is obsolete and
@@ -2489,26 +2544,30 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
         }
     }
 
-    memory->memory_data = new_memory_data;
-    memory->cur_page_count = total_page_count;
-    memory->heap_data = new_memory_data + (heap_data_old - memory_data);
+    memory->heap_data = memory_data_new + (heap_data_old - memory_data_old);
     memory->heap_data_end = memory->heap_data + heap_size;
-    memory->memory_data_end =
-        memory->memory_data + memory->num_bytes_per_page * total_page_count;
+
+    memory->num_bytes_per_page = num_bytes_per_page;
+    memory->cur_page_count = total_page_count;
+    memory->max_page_count = max_page_count;
+    memory->memory_data_size = (uint32)total_size_new;
+
+    memory->memory_data = memory_data_new;
+    memory->memory_data_end = memory_data_new + (uint32)total_size_new;
 
 #if WASM_ENABLE_FAST_JIT != 0
 #if UINTPTR_MAX == UINT64_MAX
-    memory->mem_bound_check_1byte = total_size - 1;
-    memory->mem_bound_check_2bytes = total_size - 2;
-    memory->mem_bound_check_4bytes = total_size - 4;
-    memory->mem_bound_check_8bytes = total_size - 8;
-    memory->mem_bound_check_16bytes = total_size - 16;
+    memory->mem_bound_check_1byte = total_size_new - 1;
+    memory->mem_bound_check_2bytes = total_size_new - 2;
+    memory->mem_bound_check_4bytes = total_size_new - 4;
+    memory->mem_bound_check_8bytes = total_size_new - 8;
+    memory->mem_bound_check_16bytes = total_size_new - 16;
 #else
-    memory->mem_bound_check_1byte = (uint32)total_size - 1;
-    memory->mem_bound_check_2bytes = (uint32)total_size - 2;
-    memory->mem_bound_check_4bytes = (uint32)total_size - 4;
-    memory->mem_bound_check_8bytes = (uint32)total_size - 8;
-    memory->mem_bound_check_16bytes = (uint32)total_size - 16;
+    memory->mem_bound_check_1byte = (uint32)total_size_new - 1;
+    memory->mem_bound_check_2bytes = (uint32)total_size_new - 2;
+    memory->mem_bound_check_4bytes = (uint32)total_size_new - 4;
+    memory->mem_bound_check_8bytes = (uint32)total_size_new - 8;
+    memory->mem_bound_check_16bytes = (uint32)total_size_new - 16;
 #endif
 #endif
 
@@ -2519,39 +2578,52 @@ bool
 wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
 {
     WASMMemoryInstance *memory = module->default_memory;
-    uint32 num_bytes_per_page, total_page_count;
+    uint32 num_bytes_per_page, total_size_old;
+    uint32 cur_page_count, max_page_count, total_page_count;
+    uint64 total_size_new;
 
     if (!memory)
         return false;
 
-    total_page_count = inc_page_count + memory->cur_page_count;
+    num_bytes_per_page = memory->num_bytes_per_page;
+    cur_page_count = memory->cur_page_count;
+    max_page_count = memory->max_page_count;
+    total_size_old = num_bytes_per_page * cur_page_count;
+    total_page_count = inc_page_count + cur_page_count;
+    total_size_new = num_bytes_per_page * (uint64)total_page_count;
 
     if (inc_page_count <= 0)
         /* No need to enlarge memory */
         return true;
 
-    if (total_page_count < memory->cur_page_count /* integer overflow */
-        || total_page_count > memory->max_page_count) {
+    if (total_page_count < cur_page_count /* integer overflow */
+        || total_page_count > max_page_count) {
         return false;
     }
 
-    num_bytes_per_page = memory->num_bytes_per_page;
+    bh_assert(total_size_new <= 4 * (uint64)BH_GB);
+    if (total_size_new > UINT32_MAX) {
+        /* Resize to 1 page with size 4G-1 */
+        num_bytes_per_page = UINT32_MAX;
+        total_page_count = max_page_count = 1;
+        total_size_new = UINT32_MAX;
+    }
 
 #ifdef BH_PLATFORM_WINDOWS
     if (!os_mem_commit(memory->memory_data_end,
-                       num_bytes_per_page * inc_page_count,
+                       (uint32)total_size_new - total_size_old,
                        MMAP_PROT_READ | MMAP_PROT_WRITE)) {
         return false;
     }
 #endif
 
     if (os_mprotect(memory->memory_data_end,
-                    num_bytes_per_page * inc_page_count,
+                    (uint32)total_size_new - total_size_old,
                     MMAP_PROT_READ | MMAP_PROT_WRITE)
         != 0) {
 #ifdef BH_PLATFORM_WINDOWS
         os_mem_decommit(memory->memory_data_end,
-                        num_bytes_per_page * inc_page_count);
+                        (uint32)total_size_new - total_size_old);
 #endif
         return false;
     }
@@ -2559,9 +2631,20 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
     /* The increased pages are filled with zero by the OS when os_mmap,
        no need to memset it again here */
 
+    memory->num_bytes_per_page = num_bytes_per_page;
     memory->cur_page_count = total_page_count;
-    memory->memory_data_end =
-        memory->memory_data + num_bytes_per_page * total_page_count;
+    memory->max_page_count = max_page_count;
+    memory->memory_data_size = (uint32)total_size_new;
+    memory->memory_data_end = memory->memory_data + (uint32)total_size_new;
+
+#if WASM_ENABLE_FAST_JIT != 0
+    memory->mem_bound_check_1byte = total_size_new - 1;
+    memory->mem_bound_check_2bytes = total_size_new - 2;
+    memory->mem_bound_check_4bytes = total_size_new - 4;
+    memory->mem_bound_check_8bytes = total_size_new - 8;
+    memory->mem_bound_check_16bytes = total_size_new - 16;
+#endif
+
     return true;
 }
 #endif /* end of OS_ENABLE_HW_BOUND_CHECK */

+ 14 - 8
core/iwasm/interpreter/wasm_runtime.h

@@ -26,12 +26,25 @@ struct WASMMemoryInstance {
     uint32 module_type;
     /* Shared memory flag */
     bool is_shared;
+
     /* 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 */
+    uint32 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
+     */
+    uint8 *memory_data;
+    /* Memory data end address */
+    uint8 *memory_data_end;
 
     /* Heap data base address */
     uint8 *heap_data;
@@ -45,14 +58,6 @@ struct WASMMemoryInstance {
     korp_mutex mem_lock;
 #endif
 
-    /* Memory data end address */
-    uint8 *memory_data_end;
-
-    /* Memory data begin address, the layout is: memory data + heap data
-       Note: when memory is re-allocated, the heap data and memory data
-             must be copied to new memory also. */
-    uint8 *memory_data;
-
 #if WASM_ENABLE_FAST_JIT != 0
 #if UINTPTR_MAX == UINT64_MAX
     uint64 mem_bound_check_1byte;
@@ -186,6 +191,7 @@ struct WASMModuleInstance {
 #if WASM_ENABLE_FAST_JIT != 0
     /* point to JITed functions */
     void **fast_jit_func_ptrs;
+    uint32 *func_type_indexes;
 #endif
 
     WASMMemoryInstance **memories;

+ 15 - 44
core/iwasm/libraries/debug-engine/debug_engine.c

@@ -11,17 +11,15 @@
 #include "wasm_opcode.h"
 #include "wasm_runtime.h"
 
-static uint8 break_instr[] = { DEBUG_OP_BREAK };
+static const uint8 break_instr[] = { DEBUG_OP_BREAK };
 
 typedef struct WASMDebugEngine {
     struct WASMDebugEngine *next;
     WASMDebugControlThread *control_thread;
     char ip_addr[128];
-    int32 platform_port;
     int32 process_base_port;
     bh_list debug_instance_list;
     korp_mutex instance_list_lock;
-    bool active;
 } WASMDebugEngine;
 
 void
@@ -81,10 +79,12 @@ control_thread_routine(void *arg)
     control_thread->debug_instance = debug_inst;
     bh_strcpy_s(control_thread->ip_addr, sizeof(control_thread->ip_addr),
                 g_debug_engine->ip_addr);
-    control_thread->port =
-        (g_debug_engine->process_base_port == 0)
-            ? 0
-            : g_debug_engine->process_base_port + debug_inst->id;
+    if (control_thread->port == -1) {
+        control_thread->port =
+            (g_debug_engine->process_base_port == 0)
+                ? 0
+                : g_debug_engine->process_base_port + debug_inst->id - 1;
+    }
 
     LOG_WARNING("control thread of debug object %p start\n", debug_inst);
 
@@ -93,6 +93,7 @@ control_thread_routine(void *arg)
 
     if (!control_thread->server) {
         LOG_ERROR("Failed to create debug server\n");
+        control_thread->port = 0;
         os_cond_signal(&debug_inst->wait_cond);
         os_mutex_unlock(&debug_inst->wait_lock);
         return NULL;
@@ -178,7 +179,7 @@ control_thread_routine(void *arg)
 }
 
 static WASMDebugControlThread *
-wasm_debug_control_thread_create(WASMDebugInstance *debug_instance)
+wasm_debug_control_thread_create(WASMDebugInstance *debug_instance, int32 port)
 {
     WASMDebugControlThread *control_thread;
 
@@ -188,6 +189,7 @@ wasm_debug_control_thread_create(WASMDebugInstance *debug_instance)
         return NULL;
     }
     memset(control_thread, 0, sizeof(WASMDebugControlThread));
+    control_thread->port = port;
 
     if (os_mutex_init(&control_thread->wait_lock) != 0)
         goto fail;
@@ -198,7 +200,7 @@ wasm_debug_control_thread_create(WASMDebugInstance *debug_instance)
 
     if (0
         != os_thread_create(&control_thread->tid, control_thread_routine,
-                            debug_instance, APP_THREAD_STACK_SIZE_MAX)) {
+                            debug_instance, APP_THREAD_STACK_SIZE_DEFAULT)) {
         os_mutex_unlock(&debug_instance->wait_lock);
         goto fail1;
     }
@@ -265,16 +267,6 @@ wasm_debug_engine_create()
     /* reset current instance id */
     current_instance_id = 1;
 
-    /* TODO: support Wasm platform in LLDB */
-    /*
-    engine->control_thread =
-        wasm_debug_control_thread_create((WASMDebugObject *)engine);
-    engine->control_thread->debug_engine = (WASMDebugObject *)engine;
-    engine->control_thread->debug_instance = NULL;
-    sprintf(engine->control_thread->ip_addr, "127.0.0.1");
-    engine->control_thread->port = 1234;
-    */
-
     bh_list_init(&engine->debug_instance_list);
     return engine;
 }
@@ -291,7 +283,7 @@ wasm_debug_engine_destroy()
 }
 
 bool
-wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port)
+wasm_debug_engine_init(char *ip_addr, int32 process_port)
 {
     if (wasm_debug_handler_init() != 0) {
         return false;
@@ -302,9 +294,6 @@ wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port)
     }
 
     if (g_debug_engine) {
-        process_port -= 1;
-        g_debug_engine->platform_port =
-            platform_port > 0 ? platform_port : 1234;
         g_debug_engine->process_base_port =
             (process_port > 0) ? process_port : 0;
         if (ip_addr)
@@ -313,7 +302,6 @@ wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port)
         else
             snprintf(g_debug_engine->ip_addr, sizeof(g_debug_engine->ip_addr),
                      "%s", "127.0.0.1");
-        g_debug_engine->active = true;
     }
     else {
         wasm_debug_handler_deinit();
@@ -322,33 +310,16 @@ wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port)
     return g_debug_engine != NULL ? true : false;
 }
 
-void
-wasm_debug_set_engine_active(bool active)
-{
-    if (g_debug_engine) {
-        g_debug_engine->active = active;
-    }
-}
-
-bool
-wasm_debug_get_engine_active(void)
-{
-    if (g_debug_engine) {
-        return g_debug_engine->active;
-    }
-    return false;
-}
-
 /* A debug Instance is a debug "process" in gdb remote protocol
    and bound to a runtime cluster */
 WASMDebugInstance *
-wasm_debug_instance_create(WASMCluster *cluster)
+wasm_debug_instance_create(WASMCluster *cluster, int32 port)
 {
     WASMDebugInstance *instance;
     WASMExecEnv *exec_env = NULL;
     wasm_module_inst_t module_inst = NULL;
 
-    if (!g_debug_engine || !g_debug_engine->active) {
+    if (!g_debug_engine) {
         return NULL;
     }
 
@@ -392,7 +363,7 @@ wasm_debug_instance_create(WASMCluster *cluster)
     }
     instance->exec_mem_info.current_pos = instance->exec_mem_info.start_offset;
 
-    if (!wasm_debug_control_thread_create(instance)) {
+    if (!wasm_debug_control_thread_create(instance, port)) {
         LOG_ERROR("WASM Debug Engine error: failed to create control thread");
         goto fail3;
     }

+ 2 - 8
core/iwasm/libraries/debug-engine/debug_engine.h

@@ -108,7 +108,7 @@ void
 on_thread_stop_event(WASMDebugInstance *debug_inst, WASMExecEnv *exec_env);
 
 WASMDebugInstance *
-wasm_debug_instance_create(WASMCluster *cluster);
+wasm_debug_instance_create(WASMCluster *cluster, int32 port);
 
 void
 wasm_debug_instance_destroy(WASMCluster *cluster);
@@ -117,17 +117,11 @@ WASMDebugInstance *
 wasm_exec_env_get_instance(WASMExecEnv *exec_env);
 
 bool
-wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port);
+wasm_debug_engine_init(char *ip_addr, int32 process_port);
 
 void
 wasm_debug_engine_destroy();
 
-void
-wasm_debug_set_engine_active(bool active);
-
-bool
-wasm_debug_get_engine_active(void);
-
 WASMExecEnv *
 wasm_debug_instance_get_current_env(WASMDebugInstance *instance);
 

+ 1 - 1
core/iwasm/libraries/debug-engine/gdbserver.c

@@ -18,7 +18,7 @@ struct packet_handler_elem {
 
 #define DEL_HANDLER(r, h) [r] = { .request = r, .handler = h }
 
-static struct packet_handler_elem packet_handler_table[255] = {
+static const struct packet_handler_elem packet_handler_table[255] = {
     DEL_HANDLER('Q', handle_general_set),
     DEL_HANDLER('q', handle_general_query),
     DEL_HANDLER('v', handle_v_packet),

+ 68 - 33
core/iwasm/libraries/debug-engine/handler.c

@@ -10,19 +10,51 @@
 #include "utils.h"
 #include "wasm_runtime.h"
 
-#define MAX_PACKET_SIZE (0x20000)
-static char tmpbuf[MAX_PACKET_SIZE];
+/*
+ * Note: A moderate MAX_PACKET_SIZE is ok because
+ * LLDB queries our buffer size (via qSupported PacketSize)
+ * and limits packet sizes accordingly.
+ */
+
+#if defined(DEBUG_MAX_PACKET_SIZE)
+#define MAX_PACKET_SIZE DEBUG_MAX_PACKET_SIZE
+#else
+#define MAX_PACKET_SIZE (4096)
+#endif
+
+/*
+ * Note: It's assumed that MAX_PACKET_SIZE is reasonably large.
+ * See GetWorkingDir, WasmCallStack, etc.
+ */
+#if MAX_PACKET_SIZE < PATH_MAX || MAX_PACKET_SIZE < (2048 + 1)
+#error MAX_PACKET_SIZE is too small
+#endif
+
+static char *tmpbuf;
 static korp_mutex tmpbuf_lock;
 
 int
 wasm_debug_handler_init()
 {
-    return os_mutex_init(&tmpbuf_lock);
+    int ret;
+    tmpbuf = wasm_runtime_malloc(MAX_PACKET_SIZE);
+    if (tmpbuf == NULL) {
+        LOG_ERROR("debug-engine: Packet buffer allocation failure");
+        return BHT_ERROR;
+    }
+    ret = os_mutex_init(&tmpbuf_lock);
+    if (ret != BHT_OK) {
+        wasm_runtime_free(tmpbuf);
+        tmpbuf = NULL;
+    }
+    return ret;
 }
 
 void
 wasm_debug_handler_deinit()
 {
+    wasm_runtime_free(tmpbuf);
+    tmpbuf = NULL;
     os_mutex_destroy(&tmpbuf_lock);
 }
 
@@ -76,14 +108,17 @@ process_xfer(WASMGDBServer *server, const char *name, char *args)
         os_mutex_lock(&tmpbuf_lock);
 #if WASM_ENABLE_LIBC_WASI != 0
         char objname[128];
-        wasm_debug_instance_get_current_object_name(
-            (WASMDebugInstance *)server->thread->debug_instance, objname, 128);
-        snprintf(tmpbuf, sizeof(tmpbuf),
+        if (!wasm_debug_instance_get_current_object_name(
+                (WASMDebugInstance *)server->thread->debug_instance, objname,
+                128)) {
+            objname[0] = 0; /* use an empty string */
+        }
+        snprintf(tmpbuf, MAX_PACKET_SIZE,
                  "l<library-list><library name=\"%s\"><section "
                  "address=\"0x%" PRIx64 "\"/></library></library-list>",
                  objname, addr);
 #else
-        snprintf(tmpbuf, sizeof(tmpbuf),
+        snprintf(tmpbuf, MAX_PACKET_SIZE,
                  "l<library-list><library name=\"%s\"><section "
                  "address=\"0x%" PRIx64 "\"/></library></library-list>",
                  "nobody.wasm", addr);
@@ -103,7 +138,7 @@ process_wasm_local(WASMGDBServer *server, char *args)
     bool ret;
 
     os_mutex_lock(&tmpbuf_lock);
-    snprintf(tmpbuf, sizeof(tmpbuf), "E01");
+    snprintf(tmpbuf, MAX_PACKET_SIZE, "E01");
     if (sscanf(args, "%" PRId32 ";%" PRId32, &frame_index, &local_index) == 2) {
         ret = wasm_debug_instance_get_local(
             (WASMDebugInstance *)server->thread->debug_instance, frame_index,
@@ -126,7 +161,7 @@ process_wasm_global(WASMGDBServer *server, char *args)
     bool ret;
 
     os_mutex_lock(&tmpbuf_lock);
-    snprintf(tmpbuf, sizeof(tmpbuf), "E01");
+    snprintf(tmpbuf, MAX_PACKET_SIZE, "E01");
     if (sscanf(args, "%" PRId32 ";%" PRId32, &frame_index, &global_index)
         == 2) {
         ret = wasm_debug_instance_get_global(
@@ -161,14 +196,14 @@ handle_general_query(WASMGDBServer *server, char *payload)
             (WASMDebugInstance *)server->thread->debug_instance);
 
         os_mutex_lock(&tmpbuf_lock);
-        snprintf(tmpbuf, sizeof(tmpbuf), "QCp%" PRIx64 ".%" PRIx64 "", pid,
+        snprintf(tmpbuf, MAX_PACKET_SIZE, "QCp%" PRIx64 ".%" PRIx64 "", pid,
                  tid);
         write_packet(server, tmpbuf);
         os_mutex_unlock(&tmpbuf_lock);
     }
     if (!strcmp(name, "Supported")) {
         os_mutex_lock(&tmpbuf_lock);
-        snprintf(tmpbuf, sizeof(tmpbuf),
+        snprintf(tmpbuf, MAX_PACKET_SIZE,
                  "qXfer:libraries:read+;PacketSize=%" PRIx32 ";",
                  MAX_PACKET_SIZE);
         write_packet(server, tmpbuf);
@@ -196,7 +231,7 @@ handle_general_query(WASMGDBServer *server, char *payload)
                 strlen("wasm32-wamr-wasi-wasm"));
 
         os_mutex_lock(&tmpbuf_lock);
-        snprintf(tmpbuf, sizeof(tmpbuf),
+        snprintf(tmpbuf, MAX_PACKET_SIZE,
                  "vendor:wamr;ostype:wasi;arch:wasm32;"
                  "triple:%s;endian:little;ptrsize:4;",
                  triple);
@@ -227,7 +262,7 @@ handle_general_query(WASMGDBServer *server, char *payload)
                 strlen("wasm32-wamr-wasi-wasm"));
 
         os_mutex_lock(&tmpbuf_lock);
-        snprintf(tmpbuf, sizeof(tmpbuf),
+        snprintf(tmpbuf, MAX_PACKET_SIZE,
                  "pid:%" PRIx64 ";parent-pid:%" PRIx64
                  ";vendor:wamr;ostype:wasi;arch:wasm32;"
                  "triple:%s;endian:little;ptrsize:4;",
@@ -238,7 +273,7 @@ handle_general_query(WASMGDBServer *server, char *payload)
     if (!strcmp(name, "RegisterInfo0")) {
         os_mutex_lock(&tmpbuf_lock);
         snprintf(
-            tmpbuf, sizeof(tmpbuf),
+            tmpbuf, MAX_PACKET_SIZE,
             "name:pc;alt-name:pc;bitsize:64;offset:0;encoding:uint;format:hex;"
             "set:General Purpose Registers;gcc:16;dwarf:16;generic:pc;");
         write_packet(server, tmpbuf);
@@ -260,7 +295,7 @@ handle_general_query(WASMGDBServer *server, char *payload)
             mem2hex(mem_info->name, name_buf, strlen(mem_info->name));
 
             os_mutex_lock(&tmpbuf_lock);
-            snprintf(tmpbuf, sizeof(tmpbuf),
+            snprintf(tmpbuf, MAX_PACKET_SIZE,
                      "start:%" PRIx64 ";size:%" PRIx64
                      ";permissions:%s;name:%s;",
                      (uint64)mem_info->start, mem_info->size,
@@ -339,7 +374,7 @@ send_thread_stop_status(WASMGDBServer *server, uint32 status, korp_tid tid)
 
     if (status == 0) {
         os_mutex_lock(&tmpbuf_lock);
-        snprintf(tmpbuf, sizeof(tmpbuf), "W%02x", status);
+        snprintf(tmpbuf, MAX_PACKET_SIZE, "W%02x", status);
         write_packet(server, tmpbuf);
         os_mutex_unlock(&tmpbuf_lock);
         return;
@@ -355,16 +390,16 @@ send_thread_stop_status(WASMGDBServer *server, uint32 status, korp_tid tid)
 
     os_mutex_lock(&tmpbuf_lock);
     // TODO: how name a wasm thread?
-    len += snprintf(tmpbuf, sizeof(tmpbuf), "T%02xthread:%" PRIx64 ";name:%s;",
+    len += snprintf(tmpbuf, MAX_PACKET_SIZE, "T%02xthread:%" PRIx64 ";name:%s;",
                     gdb_status, (uint64)(uintptr_t)tid, "nobody");
     if (tids_count > 0) {
-        len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len, "threads:");
+        len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, "threads:");
         while (i < tids_count) {
             if (i == tids_count - 1)
-                len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
+                len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
                                 "%" PRIx64 ";", (uint64)(uintptr_t)tids[i]);
             else
-                len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
+                len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
                                 "%" PRIx64 ",", (uint64)(uintptr_t)tids[i]);
             i++;
         }
@@ -383,29 +418,29 @@ send_thread_stop_status(WASMGDBServer *server, uint32 status, korp_tid tid)
          * correctly processed by LLDB */
         uint32 exception_len = strlen(exception);
         len +=
-            snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
+            snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
                      "thread-pcs:%" PRIx64 ";00:%s;reason:%s;description:", pc,
                      pc_string, "exception");
         /* The description should be encoded as HEX */
         for (i = 0; i < exception_len; i++) {
-            len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len, "%02x",
+            len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, "%02x",
                             exception[i]);
         }
-        len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len, ";");
+        len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, ";");
     }
     else {
         if (status == WAMR_SIG_TRAP) {
-            len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
+            len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
                             "thread-pcs:%" PRIx64 ";00:%s;reason:%s;", pc,
                             pc_string, "breakpoint");
         }
         else if (status == WAMR_SIG_SINGSTEP) {
-            len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
+            len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
                             "thread-pcs:%" PRIx64 ";00:%s;reason:%s;", pc,
                             pc_string, "trace");
         }
         else if (status > 0) {
-            len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
+            len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
                             "thread-pcs:%" PRIx64 ";00:%s;reason:%s;", pc,
                             pc_string, "signal");
         }
@@ -551,7 +586,7 @@ handle_get_read_memory(WASMGDBServer *server, char *payload)
     bool ret;
 
     os_mutex_lock(&tmpbuf_lock);
-    snprintf(tmpbuf, sizeof(tmpbuf), "%s", "");
+    snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "");
     if (sscanf(payload, "%" SCNx64 ",%" SCNx64, &maddr, &mlen) == 2) {
         char *buff;
 
@@ -585,7 +620,7 @@ handle_get_write_memory(WASMGDBServer *server, char *payload)
     bool ret;
 
     os_mutex_lock(&tmpbuf_lock);
-    snprintf(tmpbuf, sizeof(tmpbuf), "%s", "");
+    snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "");
     if (sscanf(payload, "%" SCNx64 ",%" SCNx64 ":%n", &maddr, &mlen, &offset)
         == 2) {
         payload += offset;
@@ -599,7 +634,7 @@ handle_get_write_memory(WASMGDBServer *server, char *payload)
                 (WASMDebugInstance *)server->thread->debug_instance, maddr,
                 buff, &mlen);
             if (ret) {
-                snprintf(tmpbuf, sizeof(tmpbuf), "%s", "OK");
+                snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "OK");
             }
             wasm_runtime_free(buff);
         }
@@ -681,7 +716,7 @@ handle_malloc(WASMGDBServer *server, char *payload)
     }
 
     os_mutex_lock(&tmpbuf_lock);
-    snprintf(tmpbuf, sizeof(tmpbuf), "%s", "E03");
+    snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "E03");
 
     size = strtoll(payload, NULL, 16);
     if (size > 0) {
@@ -701,7 +736,7 @@ handle_malloc(WASMGDBServer *server, char *payload)
             (WASMDebugInstance *)server->thread->debug_instance, size,
             map_prot);
         if (addr) {
-            snprintf(tmpbuf, sizeof(tmpbuf), "%" PRIx64, addr);
+            snprintf(tmpbuf, MAX_PACKET_SIZE, "%" PRIx64, addr);
         }
     }
     write_packet(server, tmpbuf);
@@ -715,13 +750,13 @@ handle_free(WASMGDBServer *server, char *payload)
     bool ret;
 
     os_mutex_lock(&tmpbuf_lock);
-    snprintf(tmpbuf, sizeof(tmpbuf), "%s", "E03");
+    snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "E03");
     addr = strtoll(payload, NULL, 16);
 
     ret = wasm_debug_instance_ummap(
         (WASMDebugInstance *)server->thread->debug_instance, addr);
     if (ret) {
-        snprintf(tmpbuf, sizeof(tmpbuf), "%s", "OK");
+        snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "OK");
     }
 
     write_packet(server, tmpbuf);

+ 33 - 0
core/iwasm/libraries/lib-rats/lib_rats.cmake

@@ -0,0 +1,33 @@
+# Copyright (c) 2022 Intel Corporation
+# Copyright (c) 2020-2021 Alibaba Cloud
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+set (LIB_RATS_DIR ${CMAKE_CURRENT_LIST_DIR})
+
+add_definitions (-DWASM_ENABLE_LIB_RATS=1)
+
+include_directories(${LIB_RATS_DIR})
+
+include(FetchContent)
+
+set(RATS_BUILD_MODE "sgx"
+    CACHE INTERNAL "Select build mode for librats(host|occlum|sgx|wasm)")
+set(RATS_INSTALL_PATH  "${CMAKE_BINARY_DIR}/librats" CACHE INTERNAL "")
+
+FetchContent_Declare(
+    librats
+    GIT_REPOSITORY https://github.com/inclavare-containers/librats
+    GIT_TAG master
+)
+FetchContent_GetProperties(librats)
+if (NOT librats_POPULATED)
+    message("-- Fetching librats ..")
+    FetchContent_Populate(librats)
+    include_directories("${librats_SOURCE_DIR}/include")
+    add_subdirectory(${librats_SOURCE_DIR} ${librats_BINARY_DIR} EXCLUDE_FROM_ALL)
+
+endif()
+
+file (GLOB source_all ${LIB_RATS_DIR}/*.c)
+
+set (LIB_RATS_SOURCE ${source_all})

+ 60 - 0
core/iwasm/libraries/lib-rats/lib_rats_wrapper.c

@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2022 Intel Corporation
+ * Copyright (c) 2020-2021 Alibaba Cloud
+ *
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <librats/api.h>
+
+#include "wasm_export.h"
+#include "bh_common.h"
+
+static uint32
+librats_collect_wrapper(wasm_exec_env_t exec_env, const uint8_t *hash)
+{
+    char *json = NULL;
+    char *str_ret;
+    uint32 len;
+    uint32 str_ret_offset = 0;
+    wasm_module_inst_t module_inst = get_module_inst(exec_env);
+    int code = librats_collect_evidence_to_json(hash, &json);
+    if (code != 0) {
+        return str_ret_offset;
+    }
+    if (json) {
+        len = (uint32)strlen(json) + 1;
+
+        str_ret_offset = module_malloc(len, (void **)&str_ret);
+        if (str_ret_offset) {
+            bh_memcpy_s(str_ret, len, json, len);
+        }
+    }
+    return str_ret_offset;
+}
+
+static int
+librats_verify_wrapper(wasm_exec_env_t exec_env, const char *evidence_json,
+                       const uint8_t *hash)
+{
+    return librats_verify_evidence_from_json(evidence_json, hash);
+}
+
+/* clang-format off */
+#define REG_NATIVE_FUNC(func_name, signature) \
+    { #func_name, func_name##_wrapper, signature, NULL }
+/* clang-format on */
+
+static NativeSymbol native_symbols_lib_rats[] = {
+    REG_NATIVE_FUNC(librats_collect, "($)i"),
+    REG_NATIVE_FUNC(librats_verify, "($$)i")
+};
+
+uint32_t
+get_lib_rats_export_apis(NativeSymbol **p_lib_rats_apis)
+{
+    *p_lib_rats_apis = native_symbols_lib_rats;
+    return sizeof(native_symbols_lib_rats) / sizeof(NativeSymbol);
+}

+ 18 - 0
core/iwasm/libraries/lib-rats/lib_rats_wrapper.h

@@ -0,0 +1,18 @@
+/*
+ * Copyright (c) 2022 Intel Corporation
+ * Copyright (c) 2020-2021 Alibaba Cloud
+ *
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#ifndef _RATS_WAMR_API_H
+#define _RATS_WAMR_API_H
+
+#include <stdint.h>
+
+char *
+librats_collect(const uint8_t *hash);
+int
+librats_verify(const char *json_string, const uint8_t *hash);
+
+#endif

+ 6 - 5
core/shared/mem-alloc/ems/ems_kfc.c

@@ -125,23 +125,24 @@ int
 gc_destroy_with_pool(gc_handle_t handle)
 {
     gc_heap_t *heap = (gc_heap_t *)handle;
+    int ret = GC_SUCCESS;
+
 #if BH_ENABLE_GC_VERIFY != 0
     hmu_t *cur = (hmu_t *)heap->base_addr;
     hmu_t *end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
+
     if (!heap->is_heap_corrupted
         && (hmu_t *)((char *)cur + hmu_get_size(cur)) != end) {
         os_printf("Memory leak detected:\n");
         gci_dump(heap);
-#if WASM_ENABLE_SPEC_TEST != 0
-        while (1) {
-        }
-#endif
+        ret = GC_ERROR;
     }
 #endif
+
     os_mutex_destroy(&heap->lock);
     memset(heap->base_addr, 0, heap->current_size);
     memset(heap, 0, sizeof(gc_heap_t));
-    return GC_SUCCESS;
+    return ret;
 }
 
 uint32

+ 9 - 2
core/shared/mem-alloc/mem_alloc.c

@@ -25,10 +25,10 @@ mem_allocator_create_with_struct_and_pool(void *struct_buf,
                                         pool_buf, pool_buf_size);
 }
 
-void
+int
 mem_allocator_destroy(mem_allocator_t allocator)
 {
-    gc_destroy_with_pool((gc_handle_t)allocator);
+    return gc_destroy_with_pool((gc_handle_t)allocator);
 }
 
 uint32
@@ -69,6 +69,13 @@ mem_allocator_is_heap_corrupted(mem_allocator_t allocator)
     return gc_is_heap_corrupted((gc_handle_t)allocator);
 }
 
+bool
+mem_allocator_get_alloc_info(mem_allocator_t allocator, void *mem_alloc_info)
+{
+    gc_heap_stats((gc_handle_t)allocator, mem_alloc_info, 3);
+    return true;
+}
+
 #else /* else of DEFAULT_MEM_ALLOCATOR */
 
 #include "tlsf/tlsf.h"

+ 4 - 0
core/shared/mem-alloc/mem_alloc.cmake

@@ -6,6 +6,10 @@ set (MEM_ALLOC_DIR ${CMAKE_CURRENT_LIST_DIR})
 
 include_directories(${MEM_ALLOC_DIR})
 
+if (WAMR_BUILD_GC_VERIFY EQUAL 1)
+    add_definitions (-DBH_ENABLE_GC_VERIFY=1)
+endif ()
+
 file (GLOB_RECURSE source_all
       ${MEM_ALLOC_DIR}/ems/*.c
       ${MEM_ALLOC_DIR}/tlsf/*.c

+ 4 - 1
core/shared/mem-alloc/mem_alloc.h

@@ -23,7 +23,7 @@ mem_allocator_create_with_struct_and_pool(void *struct_buf,
                                           void *pool_buf,
                                           uint32_t pool_buf_size);
 
-void
+int
 mem_allocator_destroy(mem_allocator_t allocator);
 
 uint32
@@ -45,6 +45,9 @@ mem_allocator_migrate(mem_allocator_t allocator, char *pool_buf_new,
 bool
 mem_allocator_is_heap_corrupted(mem_allocator_t allocator);
 
+bool
+mem_allocator_get_alloc_info(mem_allocator_t allocator, void *mem_alloc_info);
+
 #ifdef __cplusplus
 }
 #endif

+ 1 - 0
core/shared/platform/alios/platform_internal.h

@@ -30,6 +30,7 @@ typedef aos_task_t korp_thread;
 typedef korp_thread *korp_tid;
 typedef aos_task_t *aos_tid_t;
 typedef aos_mutex_t korp_mutex;
+typedef aos_sem_t korp_sem;
 
 struct os_thread_wait_node;
 typedef struct os_thread_wait_node *os_thread_wait_list;

+ 1 - 1
core/shared/utils/bh_list.h

@@ -59,7 +59,7 @@ bh_list_init(bh_list *list);
  *                <code>BH_LIST_ERROR</code> if input is invalid or no memory
  * available.
  */
-extern bh_list_status
+bh_list_status
 bh_list_insert(bh_list *list, void *elem);
 
 /**

+ 4 - 0
doc/build_wamr.md

@@ -77,6 +77,10 @@ cmake -DWAMR_BUILD_PLATFORM=linux -DWAMR_BUILD_TARGET=ARM
 - **WAMR_BUILD_LIB_PTHREAD**=1/0, default to disable if not set
 > Note: The dependent feature of lib pthread such as the `shared memory` and `thread manager` will be enabled automatically.
 
+#### **Enable lib-pthread-semaphore**
+- **WAMR_BUILD_LIB_PTHREAD_SEMAPHORE**=1/0, default to disable if not set
+> Note: This feature depends on `lib-pthread`, it will be enabled automatically if this feature is enabled.
+
 #### **Disable boundary check with hardware trap in AOT or JIT mode**
 - **WAMR_DISABLE_HW_BOUND_CHECK**=1/0, default to enable if not set and supported by platform
 > Note: by default only platform linux/darwin/android/vxworks 64-bit will enable boundary check with hardware trap in AOT or JIT mode, and the wamrc tool will generate AOT code without boundary check instructions in all 64-bit targets except SGX to improve performance.

+ 3 - 4
doc/build_wasm_app.md

@@ -160,10 +160,9 @@ Firstly if libc-builtin (-nostdlib) mode meets the requirements, e.g. there are
 
 Most of the above methods are also available for libc-wasi mode, besides them, we can export malloc and free functions with `-Wl,--export=malloc -Wl,--export=free` option, so WAMR runtime will disable its app heap and call the malloc/free function exported to allocate/free the memory from/to the heap space managed by libc.
 
-Note: wasm-ld from LLVM 13 and later automatically inserts dtor calls
-for exported funtions. It breaks the malloc/free export mentioned above.
-A workaround: Add `-Wl,--export=__wasm_call_ctors`, which happens to
-prevent the automatic insertions for the current implementation.
+Note: wasm-ld from LLVM 13 and later automatically inserts ctor/dtor calls
+for all exported functions for a command. (vs reactor)
+It breaks the malloc/free exports mentioned above.
 
 ## 3. Build wasm app with pthread support
 

+ 1 - 1
product-mini/platforms/android/CMakeLists.txt

@@ -104,7 +104,7 @@ else()
 target_link_libraries (iwasm ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -landroid -llog)
 endif()
 
-set (distribution_DIR ${CMAKE_CURRENT_SOURCE_DIR}/build/distribution)
+set (distribution_DIR ${CMAKE_BINARY_DIR}/distribution)
 set_target_properties (iwasm PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${distribution_DIR}/wasm/lib")
 
 add_custom_command (TARGET iwasm POST_BUILD

+ 18 - 1
product-mini/platforms/linux-sgx/CMakeLists.txt

@@ -59,6 +59,11 @@ if (NOT DEFINED WAMR_BUILD_LIBC_WASI)
   set (WAMR_BUILD_LIBC_WASI 1)
 endif ()
 
+if (NOT DEFINED WAMR_BUILD_LIB_RATS)
+  # Disable lib rats support by default
+  set (WAMR_BUILD_LIB_RATS 0)
+endif()
+
 if (NOT DEFINED WAMR_BUILD_FAST_INTERP)
   # Enable fast interpreter
   set (WAMR_BUILD_FAST_INTERP 1)
@@ -84,7 +89,7 @@ if (COLLECT_CODE_COVERAGE EQUAL 1)
 endif ()
 
 set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
-set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -ffunction-sections -fdata-sections \
+set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11 -ffunction-sections -fdata-sections \
                                      -Wall -Wno-unused-parameter -Wno-pedantic \
                                      -nostdinc -fvisibility=hidden -fpie" )
 
@@ -100,3 +105,15 @@ add_custom_command (
              COMMAND ${CMAKE_AR} rc libvmlib_untrusted.a untrusted/*.o)
 
 add_custom_target (vmlib_untrusted ALL DEPENDS libvmlib_untrusted.a)
+
+if (WAMR_BUILD_LIB_RATS EQUAL 1)
+    execute_process(
+        COMMAND bash -c "sed -i -E 's/^#define LIB_RATS 0 /#define LIB_RATS 1/g' ${CMAKE_CURRENT_SOURCE_DIR}/enclave-sample/Enclave/Enclave.edl"
+        OUTPUT_VARIABLE cmdOutput
+    )
+else()
+    execute_process(
+        COMMAND bash -c "sed -i -E 's/^#define LIB_RATS 1/#define LIB_RATS 0/g' ${CMAKE_CURRENT_SOURCE_DIR}/enclave-sample/Enclave/Enclave.edl"
+        OUTPUT_VARIABLE cmdOutput
+    )
+endif()

+ 5 - 0
product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.edl

@@ -3,10 +3,15 @@
  * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  */
 
+#define LIB_RATS 0
+
 enclave {
     from "sgx_tstdc.edl" import *;
     from "sgx_pthread.edl" import *;
     from "sgx_wamr.edl" import *;
+#if LIB_RATS != 0
+    from "rats.edl" import *;
+#endif
 
     trusted {
         /* define ECALLs here. */

+ 49 - 17
product-mini/platforms/linux-sgx/enclave-sample/Makefile

@@ -9,6 +9,13 @@ SGX_ARCH ?= x64
 SGX_DEBUG ?= 0
 SPEC_TEST ?= 0
 
+VMLIB_BUILD_DIR ?= $(CURDIR)/../build
+LIB_RATS_SRC ?= $(VMLIB_BUILD_DIR)/_deps/librats-build
+LIB_RATS := $(shell if [ -d $(LIB_RATS_SRC) ]; then echo 1; else echo 0; fi)
+
+LIB_RATS_INSTALL_DIR := $(VMLIB_BUILD_DIR)/librats/lib/librats
+LIB_RATS_INCLUDE_DIR := $(VMLIB_BUILD_DIR)/librats/include
+
 ifeq ($(shell getconf LONG_BIT), 32)
 	SGX_ARCH := x86
 else ifeq ($(findstring -m32, $(CXXFLAGS)), -m32)
@@ -49,6 +56,9 @@ endif
 
 App_Cpp_Files := App/App.cpp
 App_Include_Paths := -IApp -I$(SGX_SDK)/include
+ifeq ($(LIB_RATS), 1)
+	App_Include_Paths += -I$(LIB_RATS_INCLUDE_DIR)
+endif
 
 App_C_Flags := $(SGX_COMMON_CFLAGS) -fPIC -Wno-attributes $(App_Include_Paths)
 
@@ -79,6 +89,10 @@ else
 	App_Link_Flags += -lsgx_uae_service
 endif
 
+ifeq ($(LIB_RATS), 1)
+	App_Link_Flags += -L$(LIB_RATS_INSTALL_DIR) -lrats_u -lsgx_dcap_ql -lsgx_dcap_quoteverify -lsgx_ukey_exchange
+endif
+
 App_Cpp_Objects := $(App_Cpp_Files:.cpp=.o)
 
 App_Name := iwasm
@@ -105,21 +119,40 @@ Enclave_Include_Paths := -IEnclave -I$(WAMR_ROOT)/core/iwasm/include \
                          -I$(SGX_SDK)/include/tlibc \
                          -I$(SGX_SDK)/include/stlport
 
+ifeq ($(LIB_RATS), 1)
+	Enclave_Include_Paths += -I$(LIB_RATS_INCLUDE_DIR)
+endif
+
 Enclave_C_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -fstack-protector $(Enclave_Include_Paths)
 ifeq ($(SPEC_TEST), 1)
 	Enclave_C_Flags += -DWASM_ENABLE_SPEC_TEST=1
 else
 	Enclave_C_Flags += -DWASM_ENABLE_SPEC_TEST=0
 endif
-Enclave_Cpp_Flags := $(Enclave_C_Flags) -std=c++03 -nostdinc++
-Enclave_Link_Flags := $(SGX_COMMON_CFLAGS) -Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles -L$(SGX_LIBRARY_PATH) \
-	-Wl,--whole-archive -l$(Trts_Library_Name) -Wl,--no-whole-archive \
-    libvmlib.a \
-	-Wl,--start-group -lsgx_tstdc -lsgx_tcxx -lsgx_pthread -l$(Crypto_Library_Name) -l$(Service_Library_Name) -Wl,--end-group \
+
+ifeq ($(LIB_RATS), 1)
+	Rats_Lib_Link_Dirs := -L$(LIB_RATS_INSTALL_DIR) -L$(LIB_RATS_INSTALL_DIR)/attesters -L$(LIB_RATS_INSTALL_DIR)/verifiers
+	Rats_Lib_Link_libs := -lattester_nullattester -lattester_sgx_ecdsa -lattester_sgx_la \
+						-lverifier_nullverifier -lverifier_sgx_ecdsa -lverifier_sgx_la -lverifier_sgx_ecdsa_qve \
+						-lrats_lib
+endif
+
+Enclave_Cpp_Flags := $(Enclave_C_Flags) -std=c++11 -nostdinc++
+Enclave_Link_Flags := $(SGX_COMMON_CFLAGS) -Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles -L$(SGX_LIBRARY_PATH) ${Rats_Lib_Link_Dirs} \
+	-Wl,--whole-archive -l$(Trts_Library_Name) ${Rats_Lib_Link_libs} -Wl,--no-whole-archive \
+	-Wl,--start-group -lsgx_tstdc -lsgx_tcxx -lsgx_pthread -lsgx_tkey_exchange -l$(Crypto_Library_Name) -l$(Service_Library_Name) -lsgx_dcap_tvl -Wl,--end-group \
 	-Wl,-Bstatic -Wl,-Bsymbolic -Wl,--no-undefined \
 	-Wl,-pie,-eenclave_entry -Wl,--export-dynamic  \
 	-Wl,--defsym,__ImageBase=0
 
+Enclave_Edl_Search_Path     =   --search-path ../Enclave \
+								--search-path $(SGX_SDK)/include \
+								--search-path $(WAMR_ROOT)/core/shared/platform/linux-sgx
+ifeq ($(LIB_RATS), 1)
+	Enclave_Edl_Search_Path	+=	--search-path $(LIB_RATS_INCLUDE_DIR)/librats/edl
+endif
+
+
 Enclave_Cpp_Objects := $(Enclave_Cpp_Files:.cpp=.o)
 
 Enclave_Name := enclave.so
@@ -156,12 +189,14 @@ ifneq ($(Build_Mode), HW_RELEASE)
 endif
 
 ######## App Objects ########
+librats:
+ifeq ($(LIB_RATS), 1)
+	@cd $(LIB_RATS_SRC) && make install
+	@echo "librats build success"
+endif
 
-App/Enclave_u.c: $(SGX_EDGER8R) Enclave/Enclave.edl
-	@cd App && $(SGX_EDGER8R) --untrusted ../Enclave/Enclave.edl \
-		--search-path ../Enclave \
-		--search-path $(SGX_SDK)/include \
-		--search-path $(WAMR_ROOT)/core/shared/platform/linux-sgx
+App/Enclave_u.c: $(SGX_EDGER8R) Enclave/Enclave.edl librats
+	@cd App && $(SGX_EDGER8R) --untrusted ../Enclave/Enclave.edl $(Enclave_Edl_Search_Path)
 	@echo "GEN  =>  $@"
 
 App/Enclave_u.o: App/Enclave_u.c
@@ -172,7 +207,7 @@ App/%.o: App/%.cpp
 	@$(CXX) $(App_Cpp_Flags) -c $< -o $@
 	@echo "CXX  <=  $<"
 
-libvmlib_untrusted.a: ../build/libvmlib_untrusted.a
+libvmlib_untrusted.a: $(VMLIB_BUILD_DIR)/libvmlib_untrusted.a
 	@cp $< $@
 	@echo "CP $@  <=  $<"
 
@@ -183,11 +218,8 @@ $(App_Name): App/Enclave_u.o $(App_Cpp_Objects) libvmlib_untrusted.a
 
 ######## Enclave Objects ########
 
-Enclave/Enclave_t.c: $(SGX_EDGER8R) Enclave/Enclave.edl
-	@cd Enclave && $(SGX_EDGER8R) --trusted ../Enclave/Enclave.edl \
-		--search-path ../Enclave \
-		--search-path $(SGX_SDK)/include \
-		--search-path $(WAMR_ROOT)/core/shared/platform/linux-sgx
+Enclave/Enclave_t.c: $(SGX_EDGER8R) Enclave/Enclave.edl librats
+	@cd Enclave && $(SGX_EDGER8R) --trusted ../Enclave/Enclave.edl $(Enclave_Edl_Search_Path)
 	@echo "GEN  =>  $@"
 
 Enclave/Enclave_t.o: Enclave/Enclave_t.c
@@ -198,7 +230,7 @@ Enclave/%.o: Enclave/%.cpp
 	@$(CXX) $(Enclave_Cpp_Flags) -c $< -o $@
 	@echo "CXX  <=  $<"
 
-libvmlib.a: ../build/libvmlib.a
+libvmlib.a: $(VMLIB_BUILD_DIR)/libvmlib.a
 	@cp $< $@
 	@echo "CP $@  <=  $<"
 

+ 17 - 2
product-mini/platforms/nuttx/wamr.mk

@@ -5,7 +5,9 @@ CORE_ROOT := wamr/core
 IWASM_ROOT := wamr/core/iwasm
 SHARED_ROOT := wamr/core/shared
 
-ifeq ($(CONFIG_ARCH_ARMV7A),y)
+ifeq ($(CONFIG_ARCH_ARMV6M),y)
+WAMR_BUILD_TARGET := THUMBV6M
+else ifeq ($(CONFIG_ARCH_ARMV7A),y)
 WAMR_BUILD_TARGET := THUMBV7A
 else ifeq ($(CONFIG_ARCH_ARMV7M),y)
 WAMR_BUILD_TARGET := THUMBV7EM
@@ -147,6 +149,19 @@ CSRCS += wasm_loader.c
 endif
 endif
 
+ifeq ($(CONFIG_INTERPRETERS_WAMR_DEBUG_INTERP),y)
+# Note: INTERPRETERS_WAMR_CLASSIC/INTERPRETERS_WAMR_THREAD_MGR
+# dependencies are already handled in NuttX apps Kconfig
+CFLAGS += -DWASM_ENABLE_DEBUG_INTERP=1
+CFLAGS += -I$(IWASM_ROOT)/libraries/debug-engine
+CSRCS += debug_engine.c
+CSRCS += gdbserver.c
+CSRCS += handler.c
+CSRCS += packets.c
+CSRCS += utils.c
+VPATH += $(IWASM_ROOT)/libraries/debug-engine
+endif
+
 ifeq ($(CONFIG_INTERPRETERS_WAMR_STACK_GUARD_SIZE),)
 CFLAGS += -DWASM_STACK_GUARD_SIZE=0
 else
@@ -248,7 +263,7 @@ endif
 
 ifeq ($(CONFIG_INTERPRETERS_WAMR_GLOBAL_HEAP_POOL),y)
 CFLAGS += -DWASM_ENABLE_GLOBAL_HEAP_POOL=1
-CFLAGS += -DWASM_GLOBAL_HEAP_SIZE=$(CONFIG_INTERPRETERS_WAMR_GLOBAL_HEAP_POOL_SIZE)
+CFLAGS += -DWASM_GLOBAL_HEAP_SIZE="$(CONFIG_INTERPRETERS_WAMR_GLOBAL_HEAP_POOL_SIZE) * 1024"
 else
 CFLAGS += -DWASM_ENABLE_GLOBAL_HEAP_POOL=0
 endif

+ 23 - 8
product-mini/platforms/posix/main.c

@@ -290,11 +290,7 @@ moudle_destroyer(uint8 *buffer, uint32 size)
 #endif /* WASM_ENABLE_MULTI_MODULE */
 
 #if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
-#ifdef __NuttX__
-static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE * BH_KB] = { 0 };
-#else
-static char global_heap_buf[10 * 1024 * 1024] = { 0 };
-#endif
+static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 };
 #endif
 
 int
@@ -336,7 +332,6 @@ main(int argc, char *argv[])
 #endif
 #if WASM_ENABLE_DEBUG_INTERP != 0
     char *ip_addr = NULL;
-    /* int platform_port = 0; */
     int instance_port = 0;
 #endif
 
@@ -484,7 +479,8 @@ main(int argc, char *argv[])
         else if (!strncmp(argv[0], "--version", 9)) {
             uint32 major, minor, patch;
             wasm_runtime_get_version(&major, &minor, &patch);
-            printf("iwasm %u.%u.%u\n", major, minor, patch);
+            printf("iwasm %" PRIu32 ".%" PRIu32 ".%" PRIu32 "\n", major, minor,
+                   patch);
             return 0;
         }
         else
@@ -516,7 +512,6 @@ main(int argc, char *argv[])
 #endif
 
 #if WASM_ENABLE_DEBUG_INTERP != 0
-    init_args.platform_port = 0;
     init_args.instance_port = instance_port;
     if (ip_addr)
         strcpy(init_args.ip_addr, ip_addr);
@@ -591,6 +586,23 @@ main(int argc, char *argv[])
         goto fail3;
     }
 
+#if WASM_ENABLE_DEBUG_INTERP != 0
+    if (ip_addr != NULL) {
+        wasm_exec_env_t exec_env =
+            wasm_runtime_get_exec_env_singleton(wasm_module_inst);
+        uint32_t debug_port;
+        if (exec_env == NULL) {
+            printf("%s\n", wasm_runtime_get_exception(wasm_module_inst));
+            goto fail4;
+        }
+        debug_port = wasm_runtime_start_debug_instance(exec_env);
+        if (debug_port == 0) {
+            printf("Failed to start debug instance\n");
+            goto fail4;
+        }
+    }
+#endif
+
     if (is_repl_mode)
         app_instance_repl(wasm_module_inst);
     else if (func_name)
@@ -600,6 +612,9 @@ main(int argc, char *argv[])
 
     ret = 0;
 
+#if WASM_ENABLE_DEBUG_INTERP != 0
+fail4:
+#endif
     /* destroy the module instance */
     wasm_runtime_deinstantiate(wasm_module_inst);
 

+ 22 - 3
product-mini/platforms/windows/main.c

@@ -247,7 +247,6 @@ main(int argc, char *argv[])
 #endif
 #if WASM_ENABLE_DEBUG_INTERP != 0
     char *ip_addr = NULL;
-    /* int platform_port = 0; */
     int instance_port = 0;
 #endif
 
@@ -343,7 +342,8 @@ main(int argc, char *argv[])
         else if (!strncmp(argv[0], "--version", 9)) {
             uint32 major, minor, patch;
             wasm_runtime_get_version(&major, &minor, &patch);
-            printf("iwasm %u.%u.%u\n", major, minor, patch);
+            printf("iwasm %" PRIu32 ".%" PRIu32 ".%" PRIu32 "\n", major, minor,
+                   patch);
             return 0;
         }
         else
@@ -371,7 +371,6 @@ main(int argc, char *argv[])
 #endif
 
 #if WASM_ENABLE_DEBUG_INTERP != 0
-    init_args.platform_port = 0;
     init_args.instance_port = instance_port;
     if (ip_addr)
         strcpy(init_args.ip_addr, ip_addr);
@@ -437,6 +436,23 @@ main(int argc, char *argv[])
         goto fail3;
     }
 
+#if WASM_ENABLE_DEBUG_INTERP != 0
+    if (ip_addr != NULL) {
+        wasm_exec_env_t exec_env =
+            wasm_runtime_get_exec_env_singleton(wasm_module_inst);
+        uint32_t debug_port;
+        if (exec_env == NULL) {
+            printf("%s\n", wasm_runtime_get_exception(wasm_module_inst));
+            goto fail4;
+        }
+        debug_port = wasm_runtime_start_debug_instance(exec_env);
+        if (debug_port == 0) {
+            printf("Failed to start debug instance\n");
+            goto fail4;
+        }
+    }
+#endif
+
     if (is_repl_mode)
         app_instance_repl(wasm_module_inst);
     else if (func_name)
@@ -446,6 +462,9 @@ main(int argc, char *argv[])
 
     ret = 0;
 
+#if WASM_ENABLE_DEBUG_INTERP != 0
+fail4:
+#endif
     /* destroy the module instance */
     wasm_runtime_deinstantiate(wasm_module_inst);
 

+ 5 - 5
samples/multi-module/CMakeLists.txt

@@ -135,11 +135,11 @@ ExternalProject_Add(WASM_MODULE
                        -S ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps
   BUILD_COMMAND      ${CMAKE_COMMAND} --build .
   INSTALL_COMMAND    ${CMAKE_COMMAND} -E copy
-                       ./mA.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build/
-                       ./mB.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build/
-                       ./mC.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build/
-                       ./mD.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build/
-                       ./mE.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build/
+                       ./mA.wasm ${CMAKE_BINARY_DIR}
+                       ./mB.wasm ${CMAKE_BINARY_DIR}
+                       ./mC.wasm ${CMAKE_BINARY_DIR}
+                       ./mD.wasm ${CMAKE_BINARY_DIR}
+                       ./mE.wasm ${CMAKE_BINARY_DIR}
 )
 
 ################ NATIVE

+ 0 - 16
samples/multi-module/src/main.c

@@ -122,22 +122,6 @@ main()
         "call \"C5\", it will be failed since it is a export function, ===> ");
     wasm_application_execute_func(module_inst, "C5", 0, args);
 
-    /* call functions of mB */
-    printf("call \"mB.B1\", it will return 0x15:i32, ===> ");
-    wasm_application_execute_func(module_inst, "$mB$B1", 0, args);
-    printf("call \"mB.B2\", it will call A1() of mA and return 0xb:i32, ===> ");
-    wasm_application_execute_func(module_inst, "$mB$B2", 0, args);
-    printf("call \"mB.B3\", it will be failed since it is a export function, "
-           "===> ");
-    wasm_application_execute_func(module_inst, "$mB$B3", 0, args);
-
-    /* call functions of mA */
-    printf("call \"mA.A1\", it will return 0xb:i32, ===>");
-    wasm_application_execute_func(module_inst, "$mA$A1", 0, args);
-    printf("call \"mA.A2\", it will be failed since it is a export function, "
-           "===> ");
-    wasm_application_execute_func(module_inst, "$mA$A2", 0, args);
-    printf("----------------------------------------\n\n");
     ret = true;
 
     printf("- wasm_runtime_deinstantiate\n");

+ 1 - 0
samples/multi-thread/CMakeLists.txt

@@ -45,6 +45,7 @@ set(WAMR_BUILD_JIT 0)
 set(WAMR_BUILD_LIBC_BUILTIN 1)
 set(WAMR_BUILD_FAST_INTERP 1)
 set(WAMR_BUILD_LIB_PTHREAD 1)
+set(WAMR_BUILD_LIB_PTHREAD_SEMAPHORE 1)
 
 # compiling and linking flags
 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie -fPIE")

+ 81 - 0
samples/sgx-ra/CMakeLists.txt

@@ -0,0 +1,81 @@
+# Copyright (c) 2022 Intel Corporation
+# Copyright (c) 2020-2021 Alibaba Cloud
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+cmake_minimum_required(VERSION 3.1.4)
+project(sgx-ra)
+
+################ runtime settings  ##############
+set (WAMR_BUILD_PLATFORM "linux-sgx")
+
+# Reset default linker flags
+set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
+set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
+
+# Set WAMR_BUILD_TARGET
+if (NOT DEFINED WAMR_BUILD_TARGET)
+  if (CMAKE_SIZEOF_VOID_P EQUAL 8)
+    # Build as X86_64 by default in 64-bit platform
+    set (WAMR_BUILD_TARGET "X86_64")
+  elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
+    # Build as X86_32 by default in 32-bit platform
+    set (WAMR_BUILD_TARGET "X86_32")
+  else ()
+    message(SEND_ERROR "Unsupported build target platform!")
+  endif ()
+endif ()
+
+if (NOT CMAKE_BUILD_TYPE)
+  set (CMAKE_BUILD_TYPE Release)
+endif ()
+
+set (WAMR_BUILD_INTERP 1)
+set (WAMR_BUILD_AOT 1)
+set (WAMR_BUILD_JIT 0)
+set (WAMR_BUILD_LIBC_BUILTIN 1)
+set (WAMR_BUILD_LIBC_WASI 1)
+set (WAMR_BUILD_LIB_PTHREAD 1)
+set (WAMR_BUILD_FAST_INTERP 1)
+set (WAMR_BUILD_LIB_RATS 1)
+
+# compiling and linking flags
+if (COLLECT_CODE_COVERAGE EQUAL 1)
+  set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
+endif ()
+
+set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
+set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11 -ffunction-sections -fdata-sections \
+                                     -Wall -Wno-unused-parameter -Wno-pedantic \
+                                     -nostdinc -fvisibility=hidden -fpie" )
+
+# build out vmlib
+set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
+set (SGX_PLATFORM_DIR ${WAMR_ROOT_DIR}/product-mini/platforms/linux-sgx)
+include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
+
+add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
+
+add_custom_command (
+             OUTPUT libvmlib_untrusted.a
+             COMMAND mkdir -p untrusted && cd untrusted &&
+                     ${CMAKE_C_COMPILER} -c ${PLATFORM_SHARED_SOURCE_UNTRUSTED}
+             COMMAND ${CMAKE_AR} rc libvmlib_untrusted.a untrusted/*.o)
+
+add_custom_target (vmlib_untrusted ALL DEPENDS libvmlib_untrusted.a)
+
+execute_process (
+    COMMAND bash -c "sed -i -E 's/^#define LIB_RATS 0/#define LIB_RATS 1/g' ${SGX_PLATFORM_DIR}/enclave-sample/Enclave/Enclave.edl"
+    OUTPUT_VARIABLE cmdOutput
+)
+
+################ wamr runtime ###################
+add_custom_target (
+              iwasm ALL
+              DEPENDS vmlib_untrusted vmlib_untrusted
+              COMMAND make -C  ${SGX_PLATFORM_DIR}/enclave-sample SGX_MODE=HW SGX_DEBUG=1 VMLIB_BUILD_DIR=${CMAKE_BINARY_DIR}
+              COMMAND ${CMAKE_COMMAND} -E copy ${SGX_PLATFORM_DIR}/enclave-sample/enclave.signed.so ${CMAKE_BINARY_DIR}
+              COMMAND ${CMAKE_COMMAND} -E copy ${SGX_PLATFORM_DIR}/enclave-sample/iwasm ${CMAKE_BINARY_DIR}
+              COMMAND make -C ${SGX_PLATFORM_DIR}/enclave-sample clean)
+
+################ wasm application ###############
+add_subdirectory(wasm-app)

+ 42 - 0
samples/sgx-ra/README.md

@@ -0,0 +1,42 @@
+"sgx-ra" sample introduction
+==============
+
+This sample demonstrates how to execute Remote Attestation on SGX with [librats](https://github.com/inclavare-containers/librats) and run it with iwasm. It can only build on [SGX supported processors](https://www.intel.com/content/www/us/en/support/articles/000028173/processors.html), please check it.
+
+## Preparation
+
+Before staring, we need to download and intall [SGX SDK](https://download.01.org/intel-sgx/latest/linux-latest/distro) and [SGX DCAP Library](https://download.01.org/intel-sgx/latest/dcap-latest) referring to this [guide](https://download.01.org/intel-sgx/sgx-dcap/1.8/linux/docs/Intel_SGX_DCAP_Linux_SW_Installation_Guide.pdf).
+
+The following command is the example of the SGX environment installation on ubuntu18.04.
+``` shell
+$ cd $HOME
+$ # Set your platform, you can get the platforms list on 
+$ # https://download.01.org/intel-sgx/latest/linux-latest/distro
+$ SGX_PALTFORM=ubuntu18.04-server
+$ SGX_SDK_VERSION=2.17.100.3
+$ SGX_DRIVER_VERSION=1.41
+$ # install SGX Driver
+$ wget https://download.01.org/intel-sgx/latest/linux-latest/distro/$SGX_PALTFORM/sgx_linux_x64_driver_$SGX_DRIVER_VERSION.bin
+$ chmod +x sgx_linux_x64_driver_$SGX_DRIVER_VERSION.bin
+$ sudo ./sgx_linux_x64_driver_$SGX_DRIVER_VERSION.bin
+$ # install SGX SDK
+$ wget https://download.01.org/intel-sgx/latest/linux-latest/distro/$SGX_PALTFORM/sgx_linux_x64_sdk_$SGX_SDK_VERSION.bin
+$ chmod +x sgx_linux_x64_sdk_$SGX_SDK_VERSION.bin
+$ sudo ./sgx_linux_x64_sdk_$SGX_SDK_VERSION.bin
+$ # install SGX DCAP Library
+$ echo 'deb [arch=amd64] https://download.01.org/intel-sgx/sgx_repo/ubuntu bionic main' | sudo tee /etc/apt/sources.list.d/intel-sgx.list > /dev/null
+$ wget -O - https://download.01.org/intel-sgx/sgx_repo/ubuntu/intel-sgx-deb.key | sudo apt-key add -
+$ sudo apt update
+$ sudo apt install libsgx-uae-service libsgx-dcap-default-qpl-dev libsgx-dcap-ql-dev libsgx-dcap-quote-verify-dev
+```
+
+## Build
+``` shell
+$ mkdir build && cd build
+$ cmake ..
+$ make
+$ # run the sample
+$ ./iwasm wasm-app/test.wasm
+```
+
+The sample will print the evidence in json and "Evidence is trusted." by default.

+ 38 - 0
samples/sgx-ra/wasm-app/CMakeLists.txt

@@ -0,0 +1,38 @@
+# Copyright (c) 2022 Intel Corporation
+# Copyright (c) 2020-2021 Alibaba Cloud
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+cmake_minimum_required(VERSION 3.0)
+project(wasm-app)
+
+set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
+set (LIB_RATS_DIR ${WAMR_ROOT_DIR}/core/iwasm/libraries/lib-rats)
+
+set (CMAKE_C_LINK_FLAGS "")
+set (CMAKE_CXX_LINK_FLAGS "")
+if (APPLE)
+    set (HAVE_FLAG_SEARCH_PATHS_FIRST 0)
+endif ()
+
+set (CMAKE_SYSTEM_PROCESSOR wasm32)
+set (CMAKE_SYSROOT                  ${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot)
+
+if (NOT DEFINED WASI_SDK_DIR)
+    set (WASI_SDK_DIR               "/opt/wasi-sdk")
+endif ()
+
+set (CMAKE_C_FLAGS                  "-nostdlib")
+set (CMAKE_C_COMPILER_TARGET        "wasm32")
+set (CMAKE_C_COMPILER               "${WASI_SDK_DIR}/bin/clang")
+
+set (CMAKE_EXE_LINKER_FLAGS
+    "-Wl,--max-memory=131072 -z stack-size=8192   \
+     -Wl,--no-entry,--strip-all                   \
+     -Wl,--export=__main_argc_argv                \
+     -Wl,--export=__heap_base,--export=__data_end \
+     -Wl,--allow-undefined"
+)
+
+add_executable(test.wasm main.c)
+set_target_properties(test.wasm PROPERTIES INCLUDE_DIRECTORIES ${LIB_RATS_DIR})
+target_link_libraries(test.wasm)

+ 36 - 0
samples/sgx-ra/wasm-app/main.c

@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2022 Intel Corporation
+ * Copyright (c) 2020-2021 Alibaba Cloud
+ *
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "lib_rats_wrapper.h"
+
+int
+main(int argc, char **argv)
+{
+    char *evidence_json = NULL;
+    const char *hash = "12345678123456781234567812345678";
+    evidence_json = librats_collect((const uint8_t *)hash);
+    if (evidence_json == NULL) {
+        printf("Librats collect evidence failed.\n");
+        return -1;
+    }
+    printf("evidence json:\n%s\n", evidence_json);
+
+    if (librats_verify(evidence_json, (const uint8_t *)hash) != 0) {
+        printf("Evidence is not trusted.\n");
+    }
+    else {
+        printf("Evidence is trusted.\n");
+    }
+
+    if (evidence_json) {
+        free(evidence_json);
+    }
+
+    return 0;
+}

+ 4 - 4
samples/socket-api/CMakeLists.txt

@@ -87,10 +87,10 @@ ExternalProject_Add(wasm-app
                       ${CMAKE_CURRENT_SOURCE_DIR}/wasm-src
   BUILD_COMMAND     ${CMAKE_COMMAND} --build .
   INSTALL_COMMAND   ${CMAKE_COMMAND} -E copy
-                      tcp_client.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build
-                      tcp_server.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build
-                      send_recv.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build
-                      socket_opts.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build
+                      tcp_client.wasm ${CMAKE_BINARY_DIR}
+                      tcp_server.wasm ${CMAKE_BINARY_DIR}
+                      send_recv.wasm ${CMAKE_BINARY_DIR}
+                      socket_opts.wasm ${CMAKE_BINARY_DIR}
 )
 
 add_executable(tcp_server ${CMAKE_CURRENT_SOURCE_DIR}/wasm-src/tcp_server.c)

+ 1 - 1
samples/workload/XNNPACK/CMakeLists.txt

@@ -90,5 +90,5 @@ ExternalProject_Add(xnnpack
                        //:f32_sqrt_ulp_eval.wasm
     INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_directory
                       ${CMAKE_CURRENT_SOURCE_DIR}/xnnpack/bazel-out/wasm-opt/bin/
-                      ${CMAKE_CURRENT_SOURCE_DIR}/build/wasm-opt
+                      ${CMAKE_BINARY_DIR}/wasm-opt
 )

+ 1 - 1
samples/workload/bwa/CMakeLists.txt

@@ -42,5 +42,5 @@ ExternalProject_Add(bwa
                        -DCMAKE_SYSROOT=${WASI_SDK_HOME}/share/wasi-sysroot
                        ${CMAKE_CURRENT_SOURCE_DIR}/bwa
   BUILD_COMMAND  make bwa_wasm_opt
-  INSTALL_COMMAND ${CMAKE_COMMAND} -E copy ./bwa.opt.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build/bwa.wasm
+  INSTALL_COMMAND ${CMAKE_COMMAND} -E copy ./bwa.opt.wasm ${CMAKE_BINARY_DIR}/bwa.wasm
 )

+ 1 - 1
samples/workload/meshoptimizer/CMakeLists.txt

@@ -26,5 +26,5 @@ ExternalProject_Add(codecbench
                       -DCMAKE_SYSROOT=${WASI_SDK_HOME}/share/wasi-sysroot
                       ${CMAKE_CURRENT_SOURCE_DIR}/meshoptimizer
   BUILD_COMMAND  make codecbench
-  INSTALL_COMMAND ${CMAKE_COMMAND} -E copy ./codecbench.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build/codecbench.wasm
+  INSTALL_COMMAND ${CMAKE_COMMAND} -E copy ./codecbench.wasm ${CMAKE_BINARY_DIR}/codecbench.wasm
 )

+ 10 - 10
samples/workload/meshoptimizer/codecbench.patch

@@ -1,10 +1,10 @@
 diff --git a/CMakeLists.txt b/CMakeLists.txt
-index f4378ce..9bc104b 100644
+index b13d946..4254003 100644
 --- a/CMakeLists.txt
 +++ b/CMakeLists.txt
-@@ -129,3 +129,43 @@ install(FILES
-     ${CMAKE_CURRENT_BINARY_DIR}/meshoptimizerConfig.cmake
+@@ -149,3 +149,43 @@ install(FILES
      ${CMAKE_CURRENT_BINARY_DIR}/meshoptimizerConfigVersion.cmake
+     COMPONENT meshoptimizer
      DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/meshoptimizer)
 +
 +##################################################
@@ -47,10 +47,10 @@ index f4378ce..9bc104b 100644
 +
 +add_dependencies(codecbench.opt codecbench)
 diff --git a/src/vertexcodec.cpp b/src/vertexcodec.cpp
-index 5f3ec20..b79bfad 100644
+index 821c467..b7d30b1 100644
 --- a/src/vertexcodec.cpp
 +++ b/src/vertexcodec.cpp
-@@ -81,13 +81,13 @@
+@@ -83,13 +83,13 @@
  #endif
  
  #ifdef SIMD_WASM
@@ -71,7 +71,7 @@ index 5f3ec20..b79bfad 100644
  #endif
  
  namespace meshopt
-@@ -700,7 +700,7 @@ static v128_t decodeShuffleMask(unsigned char mask0, unsigned char mask1)
+@@ -691,7 +691,7 @@ static v128_t decodeShuffleMask(unsigned char mask0, unsigned char mask1)
  	v128_t sm1 = wasm_v128_load(&kDecodeBytesGroupShuffle[mask1]);
  
  	v128_t sm1off = wasm_v128_load(&kDecodeBytesGroupCount[mask0]);
@@ -80,7 +80,7 @@ index 5f3ec20..b79bfad 100644
  
  	v128_t sm1r = wasm_i8x16_add(sm1, sm1off);
  
-@@ -751,7 +751,7 @@ static const unsigned char* decodeBytesGroupSimd(const unsigned char* data, unsi
+@@ -741,7 +741,7 @@ static const unsigned char* decodeBytesGroupSimd(const unsigned char* data, unsi
  
  		v128_t shuf = decodeShuffleMask(mask0, mask1);
  
@@ -89,7 +89,7 @@ index 5f3ec20..b79bfad 100644
  
  		wasm_v128_store(buffer, result);
  
-@@ -773,7 +773,7 @@ static const unsigned char* decodeBytesGroupSimd(const unsigned char* data, unsi
+@@ -763,7 +763,7 @@ static const unsigned char* decodeBytesGroupSimd(const unsigned char* data, unsi
  
  		v128_t shuf = decodeShuffleMask(mask0, mask1);
  
@@ -99,10 +99,10 @@ index 5f3ec20..b79bfad 100644
  		wasm_v128_store(buffer, result);
  
 diff --git a/src/vertexfilter.cpp b/src/vertexfilter.cpp
-index 023452c..2374cf7 100644
+index 14a73b1..8f4b3c1 100644
 --- a/src/vertexfilter.cpp
 +++ b/src/vertexfilter.cpp
-@@ -56,10 +56,10 @@
+@@ -57,10 +57,10 @@
  #endif
  
  #ifdef SIMD_WASM

+ 7 - 2
test-tools/wamr-ide/VSCode-Extension/package.json

@@ -125,9 +125,14 @@
                         "swift"
                     ]
                 },
-                "program": "./resource/debug/bin/lldb-vscode",
                 "windows": {
-                    "program": "./resource/debug/bin/lldb-vscode.exe"
+                    "program": "./resource/debug/windows/bin/lldb-vscode.exe"
+                },
+                "osx": {
+                    "program": "./resource/debug/osx/bin/lldb-vscode"
+                },
+                "linux": {
+                    "program": "./resource/debug/linux/bin/lldb-vscode"
                 },
                 "configurationAttributes": {
                     "attach": {

+ 4 - 4
test-tools/wamr-ide/VSCode-Extension/resource/debug/README.md

@@ -1,11 +1,11 @@
 ### If you want to enable `source debugging` for this extension, please build `lldb` firstly following this [instruction](../../../../../doc/source_debugging.md#debugging-with-interpreter).
 
-### After building(`linux` for example), create `bin` folder and `lib` folder respectively in current directory, add following necessary target files into the folders.
+### After building(`linux` for example), create `bin` folder and `lib` folder respectively in `linux` directory, add following necessary target files into the folders.
 
     ```shell
-     /llvm/build-lldb/bin/lldb # move this file to resource/debug/bin/
-     /llvm/build-lldb/bin/lldb-vscode # move this file to resource/debug/bin/
-     /llvm/build-lldb/lib/liblldb.so.13 # move this file to resource/debug/lib/
+     /llvm/build-lldb/bin/lldb # move this file to resource/debug/linux/bin/
+     /llvm/build-lldb/bin/lldb-vscode # move this file to resource/debug/linux/bin/
+     /llvm/build-lldb/lib/liblldb.so.13 # move this file to resource/debug/linux/lib/
     ```
 
 Note: For macOS, the library is named like `liblldb.13.0.1.dylib`.

+ 0 - 0
test-tools/wamr-ide/VSCode-Extension/resource/debug/linux/.placeholder


+ 0 - 0
test-tools/wamr-ide/VSCode-Extension/resource/debug/osx/.placeholder


+ 0 - 0
test-tools/wamr-ide/VSCode-Extension/resource/debug/windows/.placeholder


+ 2 - 0
test-tools/wamr-ide/VSCode-Extension/resource/scripts/boot_debugger_server.sh

@@ -3,6 +3,8 @@
 # Copyright (C) 2019 Intel Corporation.  All rights reserved.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
+set -e
+
 docker run --rm -it --name=wasm-debug-server-ctr \
            -v "$(pwd)":/mnt \
            -p 1234:1234 \

+ 2 - 0
test-tools/wamr-ide/VSCode-Extension/resource/scripts/build.sh

@@ -3,6 +3,8 @@
 # Copyright (C) 2019 Intel Corporation.  All rights reserved.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
+set -e
+
 docker run --rm --name=wasm-toolchain-ctr \
                 -it -v "$(pwd)":/mnt \
                 --env=PROJ_PATH="$(pwd)" \

+ 2 - 0
test-tools/wamr-ide/VSCode-Extension/resource/scripts/destroy.sh

@@ -3,6 +3,8 @@
 # Copyright (C) 2019 Intel Corporation.  All rights reserved.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
+set -e
+
 docker -v>/dev/null
 if [ $? -ne 0 ]; then
     echo "\nDocker is not installed, please install docker firstly.\n"

+ 2 - 0
test-tools/wamr-ide/VSCode-Extension/resource/scripts/run.sh

@@ -3,6 +3,8 @@
 # Copyright (C) 2019 Intel Corporation.  All rights reserved.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
+set -e
+
 docker run --rm -it --name=wasm-debug-server-ctr \
            -v "$(pwd)":/mnt \
            wasm-debug-server:1.0 \

+ 1 - 1
test-tools/wamr-ide/VSCode-Extension/resource/webview/css/style.css

@@ -59,7 +59,7 @@
 }
 
 #ipt_projName,
-#tselect_dropdown,
+#select_dropdown,
 .config_form_body vscode-text-field,
 .config_form_body vscode-text-area {
     width: 100%;

+ 24 - 0
test-tools/wamr-ide/VSCode-Extension/src/utilities/directoryUtilities.ts

@@ -6,6 +6,7 @@
 import fileSystem = require('fs');
 import vscode = require('vscode');
 import path = require('path');
+import os = require('os');
 
 /**
  *
@@ -94,3 +95,26 @@ export function CheckIfDirectoryExist(path: string): boolean {
         return false;
     }
 }
+
+export function checkFolderName(folderName: string) {
+    let invalidCharacterArr: string[] = [];
+    var valid = true;
+
+    if (folderName.length > 255) {
+        valid = false;
+    }
+
+    if (os.platform() === 'win32') {
+        invalidCharacterArr = ['\\', '/', ':', '?', '*', '"', '|', '<', '>'];
+    } else if (os.platform() === 'linux' || os.platform() === 'darwin') {
+        invalidCharacterArr = ['/'];
+    }
+
+    invalidCharacterArr.forEach(function (c) {
+        if (folderName.indexOf(c) !== -1) {
+            valid = false;
+        }
+    });
+
+    return valid;
+}

+ 41 - 35
test-tools/wamr-ide/VSCode-Extension/src/view/NewProjectPanel.ts

@@ -6,7 +6,12 @@
 import * as vscode from 'vscode';
 import * as path from 'path';
 import * as fs from 'fs';
-import { CreateDirectory, CopyFiles } from '../utilities/directoryUtilities';
+import * as os from 'os';
+import {
+    CreateDirectory,
+    CopyFiles,
+    checkFolderName,
+} from '../utilities/directoryUtilities';
 import { getUri } from '../utilities/getUri';
 
 export class NewProjectPanel {
@@ -15,13 +20,11 @@ export class NewProjectPanel {
     private readonly _panel: vscode.WebviewPanel;
     private _disposables: vscode.Disposable[] = [];
 
-    static readonly USER_INTPUT_ERR: number = -2;
-    static readonly DIR_EXSITED_ERR: number = -1;
     static readonly EXCUTION_SUCCESS: number = 0;
+    static readonly DIR_EXSITED_ERR: number = -1;
+    static readonly USER_INTPUT_ERR: number = -2;
+    static readonly DIR_PATH_INVALID_ERR: number = -3;
 
-    /**
-     * @param context extension context from extension.ts active func
-     */
     constructor(extensionUri: vscode.Uri, panel: vscode.WebviewPanel) {
         this._panel = panel;
         this._panel.webview.html = this._getHtmlForWebview(
@@ -33,9 +36,6 @@ export class NewProjectPanel {
         this._panel.onDidDispose(this.dispose, null, this._disposables);
     }
 
-    /**
-     * @param context
-     */
     public static render(context: vscode.ExtensionContext) {
         NewProjectPanel.USER_SET_WORKSPACE = vscode.workspace
             .getConfiguration()
@@ -55,7 +55,6 @@ export class NewProjectPanel {
                 }
             );
 
-            /* create new project panel obj */
             NewProjectPanel.currentPanel = new NewProjectPanel(
                 context.extensionUri,
                 panel
@@ -63,10 +62,6 @@ export class NewProjectPanel {
         }
     }
 
-    /**
-     * @param projName project name input by user
-     * @param template
-     */
     private _creatNewProject(
         projName: string,
         template: string,
@@ -76,22 +71,23 @@ export class NewProjectPanel {
             return NewProjectPanel.USER_INTPUT_ERR;
         }
 
+        if (!checkFolderName(projName)) {
+            return NewProjectPanel.DIR_PATH_INVALID_ERR;
+        }
+
         let ROOT_PATH = path.join(NewProjectPanel.USER_SET_WORKSPACE, projName);
         let EXT_PATH = extensionUri.fsPath;
 
-        /* if the direcotry has exsited, then ignore the creation and return */
         if (fs.existsSync(ROOT_PATH)) {
             if (fs.lstatSync(ROOT_PATH).isDirectory()) {
                 return NewProjectPanel.DIR_EXSITED_ERR;
             }
         }
 
-        /* create necessary floders under the project directory */
         CreateDirectory(path.join(ROOT_PATH, '.wamr'));
         CreateDirectory(path.join(ROOT_PATH, 'include'));
         CreateDirectory(path.join(ROOT_PATH, 'src'));
 
-        /* copy scripts files to project_root_path/.wamr */
         CopyFiles(
             path.join(EXT_PATH, 'resource/scripts/CMakeLists.txt'),
             path.join(ROOT_PATH, '.wamr/CMakeLists.txt')
@@ -110,7 +106,6 @@ export class NewProjectPanel {
         extensionUri: vscode.Uri,
         templatePath: string
     ) {
-        /* get toolkit uri */
         const toolkitUri = getUri(webview, extensionUri, [
             'node_modules',
             '@vscode',
@@ -147,29 +142,26 @@ export class NewProjectPanel {
         webview: vscode.Webview,
         extensionUri: vscode.Uri
     ) {
-        // Handle messages from the webview
         webview.onDidReceiveMessage(
             message => {
                 switch (message.command) {
                     case 'create_new_project':
+                        let createNewProjectStatus = this._creatNewProject(
+                            message.projectName,
+                            message.template,
+                            extensionUri
+                        );
                         if (
-                            this._creatNewProject(
-                                message.projectName,
-                                message.template,
-                                extensionUri
-                            ) === NewProjectPanel.EXCUTION_SUCCESS
+                            createNewProjectStatus ===
+                            NewProjectPanel.EXCUTION_SUCCESS
                         ) {
-                            /* post message to page to inform the project creation has finished */
                             webview.postMessage({
                                 command: 'proj_creation_finish',
                                 prjName: message.projectName,
                             });
                         } else if (
-                            this._creatNewProject(
-                                message.projectName,
-                                message.template,
-                                extensionUri
-                            ) === NewProjectPanel.DIR_EXSITED_ERR
+                            createNewProjectStatus ===
+                            NewProjectPanel.DIR_EXSITED_ERR
                         ) {
                             vscode.window.showErrorMessage(
                                 'Project : ' +
@@ -178,16 +170,30 @@ export class NewProjectPanel {
                             );
                             return;
                         } else if (
-                            this._creatNewProject(
-                                message.projectName,
-                                message.template,
-                                extensionUri
-                            ) === NewProjectPanel.USER_INTPUT_ERR
+                            createNewProjectStatus ===
+                            NewProjectPanel.USER_INTPUT_ERR
                         ) {
                             vscode.window.showErrorMessage(
                                 'Please fill chart before your submit!'
                             );
                             return;
+                        } else if (
+                            createNewProjectStatus ===
+                            NewProjectPanel.DIR_PATH_INVALID_ERR
+                        ) {
+                            if (os.platform() === 'win32') {
+                                vscode.window.showErrorMessage(
+                                    "A file name can't contain any of the following characters: ' / \\ : * ? < > | ' and the length should be less than 255"
+                                );
+                            } else if (
+                                os.platform() === 'linux' ||
+                                os.platform() === 'darwin'
+                            ) {
+                                vscode.window.showErrorMessage(
+                                    "A file name can't contain following characters: '/' and the length should be less than 255"
+                                );
+                            }
+                            return;
                         }
                         return;
 

+ 2 - 3
wamr-compiler/main.c

@@ -48,9 +48,8 @@ print_help()
     printf("                            thread-mgr will be enabled automatically\n");
     printf("  --enable-tail-call        Enable the post-MVP tail call feature\n");
     printf("  --disable-simd            Disable the post-MVP 128-bit SIMD feature:\n");
-    printf("                              currently 128-bit SIMD is only supported for x86-64 target,\n");
-    printf("                              and by default it is enabled in x86-64 target and disabled\n");
-    printf("                              in other targets\n");
+    printf("                              currently 128-bit SIMD is supported for x86-64 and aarch64 targets,\n");
+    printf("                              and by default it is enabled in them and disabled in other targets\n");
     printf("  --disable-ref-types       Disable the MVP reference types feature\n");
     printf("  --disable-aux-stack-check Disable auxiliary stack overflow/underflow check\n");
     printf("  --enable-dump-call-stack  Enable stack trace feature\n");