Browse Source

Merge pull request #2540 from bytecodealliance/main

Merge branch main into dev/aot_stack_frame
Wenyong Huang 2 năm trước cách đây
mục cha
commit
6cacbc6b14
49 tập tin đã thay đổi với 1517 bổ sung812 xóa
  1. 4 0
      build-scripts/config_common.cmake
  2. 2 0
      build-scripts/runtime_lib.cmake
  3. 5 0
      core/config.h
  4. 4 3
      core/iwasm/aot/aot_runtime.c
  5. 2 0
      core/iwasm/aot/arch/aot_reloc_arm.c
  6. 28 1
      core/iwasm/common/wasm_application.c
  7. 90 17
      core/iwasm/common/wasm_memory.c
  8. 4 0
      core/iwasm/common/wasm_memory.h
  9. 172 0
      core/iwasm/common/wasm_native.c
  10. 30 0
      core/iwasm/common/wasm_native.h
  11. 37 60
      core/iwasm/common/wasm_runtime_common.c
  12. 20 0
      core/iwasm/common/wasm_runtime_common.h
  13. 85 0
      core/iwasm/include/wasm_export.h
  14. 4 3
      core/iwasm/interpreter/wasm_runtime.c
  15. 5 6
      core/iwasm/interpreter/wasm_runtime.h
  16. 1 8
      core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c
  17. 1 8
      core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c
  18. 0 3
      core/iwasm/libraries/lib-wasi-threads/test/trap_after_main_thread_finishes.json
  19. 0 235
      core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h
  20. 159 427
      core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c
  21. 0 6
      core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/ssp_config.h
  22. 45 13
      core/iwasm/libraries/thread-mgr/thread_manager.c
  23. 4 0
      core/iwasm/libraries/thread-mgr/thread_manager.h
  24. 8 8
      core/shared/platform/common/posix/posix_socket.c
  25. 1 1
      core/shared/platform/esp-idf/espidf_socket.c
  26. 1 1
      core/shared/platform/include/platform_api_extension.h
  27. 2 2
      core/shared/platform/linux-sgx/sgx_socket.c
  28. 8 0
      product-mini/platforms/nuttx/wamr.mk
  29. 1 5
      product-mini/platforms/posix/main.c
  30. 1 5
      product-mini/platforms/windows/main.c
  31. 1 0
      samples/inst-context-threads/.gitignore
  32. 92 0
      samples/inst-context-threads/CMakeLists.txt
  33. 4 0
      samples/inst-context-threads/README.md
  34. 61 0
      samples/inst-context-threads/build.sh
  35. 3 0
      samples/inst-context-threads/run.sh
  36. 151 0
      samples/inst-context-threads/src/main.c
  37. 11 0
      samples/inst-context-threads/src/my_context.h
  38. 32 0
      samples/inst-context-threads/src/native_impl.c
  39. 65 0
      samples/inst-context-threads/wasm-apps/testapp.c
  40. 1 0
      samples/inst-context/.gitignore
  41. 91 0
      samples/inst-context/CMakeLists.txt
  42. 4 0
      samples/inst-context/README.md
  43. 63 0
      samples/inst-context/build.sh
  44. 3 0
      samples/inst-context/run.sh
  45. 166 0
      samples/inst-context/src/main.c
  46. 10 0
      samples/inst-context/src/my_context.h
  47. 15 0
      samples/inst-context/src/native_impl.c
  48. 19 0
      samples/inst-context/wasm-apps/testapp.c
  49. 1 0
      wamr-compiler/CMakeLists.txt

+ 4 - 0
build-scripts/config_common.cmake

@@ -378,6 +378,10 @@ if (WAMR_BUILD_WASM_CACHE EQUAL 1)
   add_definitions (-DWASM_ENABLE_WASM_CACHE=1)
   message ("     Wasm files cache enabled")
 endif ()
+if (WAMR_BUILD_MODULE_INST_CONTEXT EQUAL 1)
+  add_definitions (-DWASM_ENABLE_MODULE_INST_CONTEXT=1)
+  message ("     Module instance context enabled")
+endif ()
 if (WAMR_BUILD_GC_HEAP_VERIFY EQUAL 1)
   add_definitions (-DWASM_ENABLE_GC_VERIFY=1)
   message ("     GC heap verification enabled")

+ 2 - 0
build-scripts/runtime_lib.cmake

@@ -91,8 +91,10 @@ endif ()
 
 if (WAMR_BUILD_LIBC_UVWASI EQUAL 1)
     include (${IWASM_DIR}/libraries/libc-uvwasi/libc_uvwasi.cmake)
+    set (WAMR_BUILD_MODULE_INST_CONTEXT 1)
 elseif (WAMR_BUILD_LIBC_WASI EQUAL 1)
     include (${IWASM_DIR}/libraries/libc-wasi/libc_wasi.cmake)
+    set (WAMR_BUILD_MODULE_INST_CONTEXT 1)
 endif ()
 
 if (WAMR_BUILD_LIB_PTHREAD_SEMAPHORE EQUAL 1)

+ 5 - 0
core/config.h

@@ -480,4 +480,9 @@
 #define WASM_MEM_DUAL_BUS_MIRROR 0
 #endif
 
+/* The max number of module instance contexts. */
+#ifndef WASM_MAX_INSTANCE_CONTEXTS
+#define WASM_MAX_INSTANCE_CONTEXTS 8
+#endif
+
 #endif /* end of _CONFIG_H_ */

+ 4 - 3
core/iwasm/aot/aot_runtime.c

@@ -1282,12 +1282,10 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst)
                               ->common.c_api_func_imports);
 
     if (!is_sub_inst) {
-#if WASM_ENABLE_LIBC_WASI != 0
-        wasm_runtime_destroy_wasi((WASMModuleInstanceCommon *)module_inst);
-#endif
 #if WASM_ENABLE_WASI_NN != 0
         wasi_nn_destroy(module_inst);
 #endif
+        wasm_native_call_context_dtors((WASMModuleInstanceCommon *)module_inst);
     }
 
     wasm_runtime_free(module_inst);
@@ -1449,6 +1447,9 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
     /* set thread handle and stack boundary */
     wasm_exec_env_set_thread_info(exec_env);
 
+    /* set exec env so it can be later retrieved from instance */
+    ((AOTModuleInstanceExtra *)module_inst->e)->common.cur_exec_env = exec_env;
+
     if (ext_ret_count > 0) {
         uint32 cell_num = 0, i;
         uint8 *ext_ret_types = func_type->types + func_type->param_count + 1;

+ 2 - 0
core/iwasm/aot/arch/aot_reloc_arm.c

@@ -40,6 +40,7 @@ void __aeabi_ldivmod();
 void __aeabi_memcpy();
 void __aeabi_memmove();
 void __aeabi_memset();
+void __aeabi_memclr();
 void __aeabi_uidiv();
 void __aeabi_uidivmod();
 void __aeabi_ul2d();
@@ -126,6 +127,7 @@ static SymbolMap target_sym_map[] = {
     REG_SYM(__aeabi_memcpy),
     REG_SYM(__aeabi_memmove),
     REG_SYM(__aeabi_memset),
+    REG_SYM(__aeabi_memclr),
     REG_SYM(__aeabi_uidiv),
     REG_SYM(__aeabi_uidivmod),
     REG_SYM(__aeabi_ul2d),

+ 28 - 1
core/iwasm/common/wasm_application.c

@@ -107,7 +107,34 @@ 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))) {
-        return wasm_runtime_call_wasm(exec_env, func, 0, NULL);
+        const char *wasi_proc_exit_exception = "wasi proc exit";
+
+        ret = wasm_runtime_call_wasm(exec_env, func, 0, NULL);
+#if WASM_ENABLE_THREAD_MGR != 0
+        if (ret) {
+            /* On a successful return from the `_start` function,
+               we terminate other threads by mimicing wasi:proc_exit(0).
+
+               Note:
+               - A return from the `main` function is an equivalent of
+                 exit(). (C standard)
+               - When exit code is 0, wasi-libc's `_start` function just
+                 returns w/o calling `proc_exit`.
+               - A process termination should terminate threads in
+                 the process. */
+
+            wasm_runtime_set_exception(module_inst, wasi_proc_exit_exception);
+            /* exit_code is zero-initialized */
+            ret = false;
+        }
+#endif
+        /* report wasm proc exit as a success */
+        WASMModuleInstance *inst = (WASMModuleInstance *)module_inst;
+        if (!ret && strstr(inst->cur_exception, wasi_proc_exit_exception)) {
+            inst->cur_exception[0] = 0;
+            ret = true;
+        }
+        return ret;
     }
 #endif /* end of WASM_ENABLE_LIBC_WASI */
 

+ 90 - 17
core/iwasm/common/wasm_memory.c

@@ -8,6 +8,7 @@
 #include "../aot/aot_runtime.h"
 #include "bh_platform.h"
 #include "mem_alloc.h"
+#include "wasm_memory.h"
 
 #if WASM_ENABLE_SHARED_MEMORY != 0
 #include "../common/wasm_shared_memory.h"
@@ -24,6 +25,8 @@ static Memory_Mode memory_mode = MEMORY_MODE_UNKNOWN;
 
 static mem_allocator_t pool_allocator = NULL;
 
+static enlarge_memory_error_callback_t enlarge_memory_error_cb;
+
 #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
 static void *allocator_user_data = NULL;
 static void *(*malloc_func)(void *user_data, unsigned int size) = NULL;
@@ -570,13 +573,16 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
 {
     WASMMemoryInstance *memory = wasm_get_default_memory(module);
     uint8 *memory_data_old, *memory_data_new, *heap_data_old;
-    uint32 num_bytes_per_page, heap_size, total_size_old;
+    uint32 num_bytes_per_page, heap_size, total_size_old = 0;
     uint32 cur_page_count, max_page_count, total_page_count;
     uint64 total_size_new;
     bool ret = true;
+    enlarge_memory_error_reason_t failure_reason = INTERNAL_ERROR;
 
-    if (!memory)
-        return false;
+    if (!memory) {
+        ret = false;
+        goto return_func;
+    }
 
     heap_data_old = memory->heap_data;
     heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
@@ -594,9 +600,15 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
         /* No need to enlarge memory */
         return true;
 
-    if (total_page_count < cur_page_count /* integer overflow */
-        || total_page_count > max_page_count) {
-        return false;
+    if (total_page_count < cur_page_count) { /* integer overflow */
+        ret = false;
+        goto return_func;
+    }
+
+    if (total_page_count > max_page_count) {
+        failure_reason = MAX_SIZE_REACHED;
+        ret = false;
+        goto return_func;
     }
 
     bh_assert(total_size_new <= 4 * (uint64)BH_GB);
@@ -622,14 +634,16 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
     if (heap_size > 0) {
         if (mem_allocator_is_heap_corrupted(memory->heap_handle)) {
             wasm_runtime_show_app_heap_corrupted_prompt();
-            return false;
+            ret = false;
+            goto return_func;
         }
     }
 
     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;
+            ret = false;
+            goto return_func;
         }
         if (memory_data_old) {
             bh_memcpy_s(memory_data_new, (uint32)total_size_new,
@@ -685,6 +699,26 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
     os_writegsbase(memory_data_new);
 #endif
 
+return_func:
+    if (!ret && enlarge_memory_error_cb) {
+        WASMExecEnv *exec_env = NULL;
+
+#if WASM_ENABLE_INTERP != 0
+        if (module->module_type == Wasm_Module_Bytecode)
+            exec_env =
+                ((WASMModuleInstanceExtra *)module->e)->common.cur_exec_env;
+#endif
+#if WASM_ENABLE_AOT != 0
+        if (module->module_type == Wasm_Module_AoT)
+            exec_env =
+                ((AOTModuleInstanceExtra *)module->e)->common.cur_exec_env;
+#endif
+
+        enlarge_memory_error_cb(inc_page_count, total_size_old, 0,
+                                failure_reason,
+                                (WASMModuleInstanceCommon *)module, exec_env);
+    }
+
     return ret;
 }
 #else
@@ -692,12 +726,16 @@ bool
 wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
 {
     WASMMemoryInstance *memory = wasm_get_default_memory(module);
-    uint32 num_bytes_per_page, total_size_old;
+    uint32 num_bytes_per_page, total_size_old = 0;
     uint32 cur_page_count, max_page_count, total_page_count;
     uint64 total_size_new;
+    bool ret = true;
+    enlarge_memory_error_reason_t failure_reason = INTERNAL_ERROR;
 
-    if (!memory)
-        return false;
+    if (!memory) {
+        ret = false;
+        goto return_func;
+    }
 
     num_bytes_per_page = memory->num_bytes_per_page;
     cur_page_count = memory->cur_page_count;
@@ -710,9 +748,15 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
         /* No need to enlarge memory */
         return true;
 
-    if (total_page_count < cur_page_count /* integer overflow */
-        || total_page_count > max_page_count) {
-        return false;
+    if (total_page_count < cur_page_count) { /* integer overflow */
+        ret = false;
+        goto return_func;
+    }
+
+    if (total_page_count > max_page_count) {
+        failure_reason = MAX_SIZE_REACHED;
+        ret = false;
+        goto return_func;
     }
 
     bh_assert(total_size_new <= 4 * (uint64)BH_GB);
@@ -727,7 +771,8 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
     if (!os_mem_commit(memory->memory_data_end,
                        (uint32)total_size_new - total_size_old,
                        MMAP_PROT_READ | MMAP_PROT_WRITE)) {
-        return false;
+        ret = false;
+        goto return_func;
     }
 #endif
 
@@ -739,7 +784,8 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
         os_mem_decommit(memory->memory_data_end,
                         (uint32)total_size_new - total_size_old);
 #endif
-        return false;
+        ret = false;
+        goto return_func;
     }
 
     /* The increased pages are filled with zero by the OS when os_mmap,
@@ -759,10 +805,37 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
     memory->mem_bound_check_16bytes.u64 = total_size_new - 16;
 #endif
 
-    return true;
+return_func:
+    if (!ret && enlarge_memory_error_cb) {
+        WASMExecEnv *exec_env = NULL;
+
+#if WASM_ENABLE_INTERP != 0
+        if (module->module_type == Wasm_Module_Bytecode)
+            exec_env =
+                ((WASMModuleInstanceExtra *)module->e)->common.cur_exec_env;
+#endif
+#if WASM_ENABLE_AOT != 0
+        if (module->module_type == Wasm_Module_AoT)
+            exec_env =
+                ((AOTModuleInstanceExtra *)module->e)->common.cur_exec_env;
+#endif
+
+        enlarge_memory_error_cb(inc_page_count, total_size_old, 0,
+                                failure_reason,
+                                (WASMModuleInstanceCommon *)module, exec_env);
+    }
+
+    return ret;
 }
 #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
 
+void
+wasm_runtime_set_enlarge_mem_error_callback(
+    const enlarge_memory_error_callback_t callback)
+{
+    enlarge_memory_error_cb = callback;
+}
+
 bool
 wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
 {

+ 4 - 0
core/iwasm/common/wasm_memory.h

@@ -24,6 +24,10 @@ wasm_runtime_memory_destroy();
 unsigned
 wasm_runtime_memory_pool_size();
 
+void
+wasm_runtime_set_enlarge_mem_error_callback(
+    const enlarge_memory_error_callback_t callback);
+
 #ifdef __cplusplus
 }
 #endif

+ 172 - 0
core/iwasm/common/wasm_native.c

@@ -6,6 +6,15 @@
 #include "wasm_native.h"
 #include "wasm_runtime_common.h"
 #include "bh_log.h"
+#if WASM_ENABLE_INTERP != 0
+#include "../interpreter/wasm_runtime.h"
+#endif
+#if WASM_ENABLE_AOT != 0
+#include "../aot/aot_runtime.h"
+#endif
+#if WASM_ENABLE_THREAD_MGR != 0
+#include "../libraries/thread-mgr/thread_manager.h"
+#endif
 
 #if !defined(BH_PLATFORM_ZEPHYR) && !defined(BH_PLATFORM_ALIOS_THINGS) \
     && !defined(BH_PLATFORM_OPENRTOS) && !defined(BH_PLATFORM_ESP_IDF)
@@ -22,6 +31,10 @@
 
 static NativeSymbolsList g_native_symbols_list = NULL;
 
+#if WASM_ENABLE_LIBC_WASI != 0
+static void *g_wasi_context_key;
+#endif /* WASM_ENABLE_LIBC_WASI */
+
 uint32
 get_libc_builtin_export_apis(NativeSymbol **p_libc_builtin_apis);
 
@@ -394,6 +407,155 @@ wasm_native_unregister_natives(const char *module_name,
     return false;
 }
 
+#if WASM_ENABLE_MODULE_INST_CONTEXT != 0
+static uint32
+context_key_to_idx(void *key)
+{
+    bh_assert(key != NULL);
+    uint32 idx = (uint32)(uintptr_t)key;
+    bh_assert(idx > 0);
+    bh_assert(idx <= WASM_MAX_INSTANCE_CONTEXTS);
+    return idx - 1;
+}
+
+static void *
+context_idx_to_key(uint32 idx)
+{
+    bh_assert(idx < WASM_MAX_INSTANCE_CONTEXTS);
+    return (void *)(uintptr_t)(idx + 1);
+}
+
+typedef void (*dtor_t)(WASMModuleInstanceCommon *, void *);
+static dtor_t g_context_dtors[WASM_MAX_INSTANCE_CONTEXTS];
+
+static void
+dtor_noop(WASMModuleInstanceCommon *inst, void *ctx)
+{}
+
+void *
+wasm_native_create_context_key(void (*dtor)(WASMModuleInstanceCommon *inst,
+                                            void *ctx))
+{
+    uint32 i;
+    for (i = 0; i < WASM_MAX_INSTANCE_CONTEXTS; i++) {
+        if (g_context_dtors[i] == NULL) {
+            if (dtor == NULL) {
+                dtor = dtor_noop;
+            }
+            g_context_dtors[i] = dtor;
+            return context_idx_to_key(i);
+        }
+    }
+    LOG_ERROR("failed to allocate instance context key");
+    return NULL;
+}
+
+void
+wasm_native_destroy_context_key(void *key)
+{
+    uint32 idx = context_key_to_idx(key);
+    bh_assert(g_context_dtors[idx] != NULL);
+    g_context_dtors[idx] = NULL;
+}
+
+static WASMModuleInstanceExtraCommon *
+wasm_module_inst_extra_common(WASMModuleInstanceCommon *inst)
+{
+#if WASM_ENABLE_INTERP != 0
+    if (inst->module_type == Wasm_Module_Bytecode) {
+        return &((WASMModuleInstance *)inst)->e->common;
+    }
+#endif
+#if WASM_ENABLE_AOT != 0
+    if (inst->module_type == Wasm_Module_AoT) {
+        return &((AOTModuleInstanceExtra *)((AOTModuleInstance *)inst)->e)
+                    ->common;
+    }
+#endif
+    bh_assert(false);
+    return NULL;
+}
+
+void
+wasm_native_set_context(WASMModuleInstanceCommon *inst, void *key, void *ctx)
+{
+    uint32 idx = context_key_to_idx(key);
+    WASMModuleInstanceExtraCommon *common = wasm_module_inst_extra_common(inst);
+    common->contexts[idx] = ctx;
+}
+
+void
+wasm_native_set_context_spread(WASMModuleInstanceCommon *inst, void *key,
+                               void *ctx)
+{
+#if WASM_ENABLE_THREAD_MGR != 0
+    wasm_cluster_set_context(inst, key, ctx);
+#else
+    wasm_native_set_context(inst, key, ctx);
+#endif
+}
+
+void *
+wasm_native_get_context(WASMModuleInstanceCommon *inst, void *key)
+{
+    uint32 idx = context_key_to_idx(key);
+    WASMModuleInstanceExtraCommon *common = wasm_module_inst_extra_common(inst);
+    return common->contexts[idx];
+}
+
+void
+wasm_native_call_context_dtors(WASMModuleInstanceCommon *inst)
+{
+    WASMModuleInstanceExtraCommon *common = wasm_module_inst_extra_common(inst);
+    uint32 i;
+    for (i = 0; i < WASM_MAX_INSTANCE_CONTEXTS; i++) {
+        dtor_t dtor = g_context_dtors[i];
+        if (dtor != NULL) {
+            dtor(inst, common->contexts[i]);
+        }
+    }
+}
+
+void
+wasm_native_inherit_contexts(WASMModuleInstanceCommon *child,
+                             WASMModuleInstanceCommon *parent)
+{
+    WASMModuleInstanceExtraCommon *parent_common =
+        wasm_module_inst_extra_common(parent);
+    WASMModuleInstanceExtraCommon *child_common =
+        wasm_module_inst_extra_common(child);
+    bh_memcpy_s(child_common->contexts,
+                sizeof(*child_common->contexts) * WASM_MAX_INSTANCE_CONTEXTS,
+                parent_common->contexts,
+                sizeof(*parent_common->contexts) * WASM_MAX_INSTANCE_CONTEXTS);
+}
+#endif /* WASM_ENABLE_MODULE_INST_CONTEXT != 0 */
+
+#if WASM_ENABLE_LIBC_WASI != 0
+WASIContext *
+wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst_comm)
+{
+    return wasm_native_get_context(module_inst_comm, g_wasi_context_key);
+}
+
+void
+wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst_comm,
+                          WASIContext *wasi_ctx)
+{
+    return wasm_native_set_context(module_inst_comm, g_wasi_context_key,
+                                   wasi_ctx);
+}
+
+static void
+wasi_context_dtor(WASMModuleInstanceCommon *inst, void *ctx)
+{
+    if (ctx == NULL) {
+        return;
+    }
+    wasm_runtime_destroy_wasi(inst);
+}
+#endif /* end of WASM_ENABLE_LIBC_WASI */
+
 bool
 wasm_native_init()
 {
@@ -420,6 +582,10 @@ wasm_native_init()
 #endif /* WASM_ENABLE_SPEC_TEST */
 
 #if WASM_ENABLE_LIBC_WASI != 0
+    g_wasi_context_key = wasm_native_create_context_key(wasi_context_dtor);
+    if (g_wasi_context_key == NULL) {
+        goto fail;
+    }
     n_native_symbols = get_libc_wasi_export_apis(&native_symbols);
     if (!wasm_native_register_natives("wasi_unstable", native_symbols,
                                       n_native_symbols))
@@ -507,6 +673,12 @@ wasm_native_destroy()
 {
     NativeSymbolsNode *node, *node_next;
 
+#if WASM_ENABLE_LIBC_WASI != 0
+    if (g_wasi_context_key != NULL) {
+        wasm_native_destroy_context_key(g_wasi_context_key);
+        g_wasi_context_key = NULL;
+    }
+#endif
 #if WASM_ENABLE_LIB_PTHREAD != 0
     lib_pthread_destroy();
 #endif

+ 30 - 0
core/iwasm/common/wasm_native.h

@@ -68,6 +68,36 @@ bool
 wasm_native_unregister_natives(const char *module_name,
                                NativeSymbol *native_symbols);
 
+#if WASM_ENABLE_MODULE_INST_CONTEXT != 0
+struct WASMModuleInstanceCommon;
+
+void *
+wasm_native_create_context_key(
+    void (*dtor)(struct WASMModuleInstanceCommon *inst, void *ctx));
+
+void
+wasm_native_destroy_context_key(void *key);
+
+void
+wasm_native_set_context(struct WASMModuleInstanceCommon *inst, void *key,
+                        void *ctx);
+void
+wasm_native_set_context_spread(struct WASMModuleInstanceCommon *inst, void *key,
+                               void *ctx);
+void *
+wasm_native_get_context(struct WASMModuleInstanceCommon *inst, void *key);
+
+void
+wasm_native_call_context_dtors(struct WASMModuleInstanceCommon *inst);
+
+void
+wasm_native_inherit_contexts(struct WASMModuleInstanceCommon *child,
+                             struct WASMModuleInstanceCommon *parent);
+#else /* WASM_ENABLE_MODULE_INST_CONTEXT */
+#define wasm_native_call_context_dtors(inst) (void)(inst)
+#define wasm_native_inherit_contexts(child, parent) (void)(parent)
+#endif /* WASM_ENABLE_MODULE_INST_CONTEXT */
+
 bool
 wasm_native_init();
 

+ 37 - 60
core/iwasm/common/wasm_runtime_common.c

@@ -1938,33 +1938,6 @@ wasm_runtime_finalize_call_function(WASMExecEnv *exec_env,
 }
 #endif
 
-static bool
-clear_wasi_proc_exit_exception(WASMModuleInstanceCommon *module_inst_comm)
-{
-#if WASM_ENABLE_LIBC_WASI != 0
-    bool has_exception;
-    char exception[EXCEPTION_BUF_LEN];
-    WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
-
-    bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
-              || module_inst_comm->module_type == Wasm_Module_AoT);
-
-    has_exception = wasm_copy_exception(module_inst, exception);
-    if (has_exception && !strcmp(exception, "Exception: wasi proc exit")) {
-        /* The "wasi proc exit" exception is thrown by native lib to
-           let wasm app exit, which is a normal behavior, we clear
-           the exception here. And just clear the exception of current
-           thread, don't call `wasm_set_exception(module_inst, NULL)`
-           which will clear the exception of all threads. */
-        module_inst->cur_exception[0] = '\0';
-        return true;
-    }
-    return false;
-#else
-    return false;
-#endif
-}
-
 bool
 wasm_runtime_call_wasm(WASMExecEnv *exec_env,
                        WASMFunctionInstanceCommon *function, uint32 argc,
@@ -2005,15 +1978,10 @@ wasm_runtime_call_wasm(WASMExecEnv *exec_env,
                                 param_argc, new_argv);
 #endif
     if (!ret) {
-        if (clear_wasi_proc_exit_exception(exec_env->module_inst)) {
-            ret = true;
-        }
-        else {
-            if (new_argv != argv) {
-                wasm_runtime_free(new_argv);
-            }
-            return false;
+        if (new_argv != argv) {
+            wasm_runtime_free(new_argv);
         }
+        return false;
     }
 
 #if WASM_ENABLE_REF_TYPES != 0
@@ -3311,27 +3279,6 @@ wasm_runtime_get_wasi_exit_code(WASMModuleInstanceCommon *module_inst)
 #endif
     return wasi_ctx->exit_code;
 }
-
-WASIContext *
-wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst_comm)
-{
-    WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
-
-    bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
-              || module_inst_comm->module_type == Wasm_Module_AoT);
-    return module_inst->wasi_ctx;
-}
-
-void
-wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst_comm,
-                          WASIContext *wasi_ctx)
-{
-    WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
-
-    bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
-              || module_inst_comm->module_type == Wasm_Module_AoT);
-    module_inst->wasi_ctx = wasi_ctx;
-}
 #endif /* end of WASM_ENABLE_LIBC_WASI */
 
 WASMModuleCommon *
@@ -4594,10 +4541,6 @@ wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_index,
         ret = aot_call_indirect(exec_env, 0, element_index, argc, argv);
 #endif
 
-    if (!ret && clear_wasi_proc_exit_exception(exec_env->module_inst)) {
-        ret = true;
-    }
-
     return ret;
 }
 
@@ -5681,3 +5624,37 @@ wasm_runtime_is_import_global_linked(const char *module_name,
     return false;
 #endif
 }
+
+#if WASM_ENABLE_MODULE_INST_CONTEXT != 0
+void *
+wasm_runtime_create_context_key(void (*dtor)(WASMModuleInstanceCommon *inst,
+                                             void *ctx))
+{
+    return wasm_native_create_context_key(dtor);
+}
+
+void
+wasm_runtime_destroy_context_key(void *key)
+{
+    wasm_native_destroy_context_key(key);
+}
+
+void
+wasm_runtime_set_context(WASMModuleInstanceCommon *inst, void *key, void *ctx)
+{
+    wasm_native_set_context(inst, key, ctx);
+}
+
+void
+wasm_runtime_set_context_spread(WASMModuleInstanceCommon *inst, void *key,
+                                void *ctx)
+{
+    wasm_native_set_context_spread(inst, key, ctx);
+}
+
+void *
+wasm_runtime_get_context(WASMModuleInstanceCommon *inst, void *key)
+{
+    return wasm_native_get_context(inst, key);
+}
+#endif /* WASM_ENABLE_MODULE_INST_CONTEXT != 0 */

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

@@ -939,6 +939,26 @@ WASM_RUNTIME_API_EXTERN bool
 wasm_runtime_unregister_natives(const char *module_name,
                                 NativeSymbol *native_symbols);
 
+/* See wasm_export.h for description */
+WASM_RUNTIME_API_EXTERN void *
+wasm_runtime_create_context_key(void (*dtor)(WASMModuleInstanceCommon *inst,
+                                             void *ctx));
+
+/* See wasm_export.h for description */
+WASM_RUNTIME_API_EXTERN void
+wasm_runtime_destroy_context_key(void *key);
+
+/* See wasm_export.h for description */
+WASM_RUNTIME_API_EXTERN void
+wasm_runtime_set_context(WASMModuleInstanceCommon *inst, void *key, void *ctx);
+/* See wasm_export.h for description */
+WASM_RUNTIME_API_EXTERN void
+wasm_runtime_set_context_spread(WASMModuleInstanceCommon *inst, void *key,
+                                void *ctx);
+/* See wasm_export.h for description */
+WASM_RUNTIME_API_EXTERN void *
+wasm_runtime_get_context(WASMModuleInstanceCommon *inst, void *key);
+
 bool
 wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
                            const WASMType *func_type, const char *signature,

+ 85 - 0
core/iwasm/include/wasm_export.h

@@ -1439,6 +1439,91 @@ WASM_RUNTIME_API_EXTERN bool
 wasm_runtime_is_import_global_linked(const char *module_name,
                                      const char *global_name);
 
+typedef enum {
+    INTERNAL_ERROR,
+    MAX_SIZE_REACHED,
+} enlarge_memory_error_reason_t;
+
+typedef void (*enlarge_memory_error_callback_t)(
+    uint32_t inc_page_count, uint64_t current_memory_size,
+    uint32_t memory_index, enlarge_memory_error_reason_t failure_reason,
+    wasm_module_inst_t instance, wasm_exec_env_t exec_env);
+
+/**
+ * Setup callback invoked when memory.grow fails
+ */
+WASM_RUNTIME_API_EXTERN void
+wasm_runtime_set_enlarge_mem_error_callback(
+    const enlarge_memory_error_callback_t callback);
+
+/*
+ * module instance context APIs
+ *   wasm_runtime_create_context_key
+ *   wasm_runtime_destroy_context_key
+ *   wasm_runtime_set_context
+ *   wasm_runtime_set_context_spread
+ *   wasm_runtime_get_context
+ *
+ * This set of APIs is intended to be used by an embedder which provides
+ * extra sets of native functions, which need per module instance state
+ * and are maintained outside of the WAMR tree.
+ *
+ * It's modelled after the pthread specific API.
+ *
+ * wasm_runtime_set_context_spread is similar to
+ * wasm_runtime_set_context, except that
+ * wasm_runtime_set_context_spread applies the change
+ * to all threads in the cluster.
+ * It's an undefined behavior if multiple threads in a cluster call
+ * wasm_runtime_set_context_spread on the same key
+ * simultaneously. It's a caller's resposibility to perform necessary
+ * serialization if necessary. For example:
+ *
+ * if (wasm_runtime_get_context(inst, key) == NULL) {
+ *     newctx = alloc_and_init(...);
+ *     lock(some_lock);
+ *     if (wasm_runtime_get_context(inst, key) == NULL) {
+ *         // this thread won the race
+ *         wasm_runtime_set_context_spread(inst, key, newctx);
+ *         newctx = NULL;
+ *     }
+ *     unlock(some_lock);
+ *     if (newctx != NULL) {
+ *         // this thread lost the race, free it
+ *         cleanup_and_free(newctx);
+ *     }
+ * }
+ *
+ * Note: dynamic key create/destroy while instances are live is not
+ * implemented as of writing this.
+ * it's caller's resposibility to ensure destorying all module instances
+ * before calling wasm_runtime_create_context_key or
+ * wasm_runtime_destroy_context_key.
+ * otherwise, it's an undefined behavior.
+ *
+ * Note about threads:
+ * - When spawning a thread, the contexts (the pointers given to
+ *   wasm_runtime_set_context) are copied from the parent
+ *   instance.
+ * - The destructor is called only on the main instance.
+ */
+
+WASM_RUNTIME_API_EXTERN void *
+wasm_runtime_create_context_key(
+    void (*dtor)(wasm_module_inst_t inst, void *ctx));
+
+WASM_RUNTIME_API_EXTERN void
+wasm_runtime_destroy_context_key(void *key);
+
+WASM_RUNTIME_API_EXTERN void
+wasm_runtime_set_context(wasm_module_inst_t inst, void *key,
+                                         void *ctx);
+WASM_RUNTIME_API_EXTERN void
+wasm_runtime_set_context_spread(wasm_module_inst_t inst, void *key,
+                                         void *ctx);
+WASM_RUNTIME_API_EXTERN void *
+wasm_runtime_get_context(wasm_module_inst_t inst, void *key);
+
 /* clang-format on */
 
 #ifdef __cplusplus

+ 4 - 3
core/iwasm/interpreter/wasm_runtime.c

@@ -2233,12 +2233,10 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
         wasm_runtime_free(module_inst->e->common.c_api_func_imports);
 
     if (!is_sub_inst) {
-#if WASM_ENABLE_LIBC_WASI != 0
-        wasm_runtime_destroy_wasi((WASMModuleInstanceCommon *)module_inst);
-#endif
 #if WASM_ENABLE_WASI_NN != 0
         wasi_nn_destroy(module_inst);
 #endif
+        wasm_native_call_context_dtors((WASMModuleInstanceCommon *)module_inst);
     }
 
     wasm_runtime_free(module_inst);
@@ -2400,6 +2398,9 @@ wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
     /* set thread handle and stack boundary */
     wasm_exec_env_set_thread_info(exec_env);
 
+    /* set exec env so it can be later retrieved from instance */
+    module_inst->e->common.cur_exec_env = exec_env;
+
     interp_call_wasm(module_inst, exec_env, function, argc, argv);
     return !wasm_copy_exception(module_inst, NULL);
 }

+ 5 - 6
core/iwasm/interpreter/wasm_runtime.h

@@ -212,7 +212,10 @@ typedef struct CApiFuncImport {
 
 /* The common part of WASMModuleInstanceExtra and AOTModuleInstanceExtra */
 typedef struct WASMModuleInstanceExtraCommon {
+    void *contexts[WASM_MAX_INSTANCE_CONTEXTS];
     CApiFuncImport *c_api_func_imports;
+    /* pointer to the exec env currently used */
+    WASMExecEnv *cur_exec_env;
 #if WASM_CONFIGUABLE_BOUNDS_CHECKS != 0
     /* Disable bounds checks or not */
     bool disable_bounds_checks;
@@ -297,12 +300,8 @@ struct WASMModuleInstance {
        it denotes `AOTModule *` */
     DefPointer(WASMModule *, module);
 
-#if WASM_ENABLE_LIBC_WASI
-    /* WASI context */
-    DefPointer(WASIContext *, wasi_ctx);
-#else
-    DefPointer(void *, wasi_ctx);
-#endif
+    DefPointer(void *, used_to_be_wasi_ctx); /* unused */
+
     DefPointer(WASMExecEnv *, exec_env_singleton);
     /* Array of function pointers to import functions,
        not available in AOTModuleInstance */

+ 1 - 8
core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c

@@ -559,9 +559,6 @@ pthread_create_wrapper(wasm_exec_env_t exec_env,
     uint32 thread_handle;
     uint32 stack_size = 8192;
     int32 ret = -1;
-#if WASM_ENABLE_LIBC_WASI != 0
-    WASIContext *wasi_ctx;
-#endif
 
     bh_assert(module);
     bh_assert(module_inst);
@@ -588,11 +585,7 @@ pthread_create_wrapper(wasm_exec_env_t exec_env,
     wasm_runtime_set_custom_data_internal(
         new_module_inst, wasm_runtime_get_custom_data(module_inst));
 
-#if WASM_ENABLE_LIBC_WASI != 0
-    wasi_ctx = get_wasi_ctx(module_inst);
-    if (wasi_ctx)
-        wasm_runtime_set_wasi_ctx(new_module_inst, wasi_ctx);
-#endif
+    wasm_native_inherit_contexts(new_module_inst, module_inst);
 
     if (!(wasm_cluster_dup_c_api_imports(new_module_inst, module_inst)))
         goto fail;

+ 1 - 8
core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c

@@ -80,9 +80,6 @@ thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg)
     int32 thread_id;
     uint32 stack_size = 8192;
     int32 ret = -1;
-#if WASM_ENABLE_LIBC_WASI != 0
-    WASIContext *wasi_ctx;
-#endif
 
     bh_assert(module);
     bh_assert(module_inst);
@@ -99,11 +96,7 @@ thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg)
     if (!(wasm_cluster_dup_c_api_imports(new_module_inst, module_inst)))
         goto thread_preparation_fail;
 
-#if WASM_ENABLE_LIBC_WASI != 0
-    wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
-    if (wasi_ctx)
-        wasm_runtime_set_wasi_ctx(new_module_inst, wasi_ctx);
-#endif
+    wasm_native_inherit_contexts(new_module_inst, module_inst);
 
     start_func = wasm_runtime_lookup_function(new_module_inst,
                                               THREAD_START_FUNCTION, NULL);

+ 0 - 3
core/iwasm/libraries/lib-wasi-threads/test/trap_after_main_thread_finishes.json

@@ -1,3 +0,0 @@
-{
-  "exit_code": 1
-}

+ 0 - 235
core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h

@@ -264,37 +264,6 @@ typedef uint8_t __wasi_sdflags_t;
 typedef uint16_t __wasi_siflags_t;
 
 typedef uint8_t __wasi_signal_t;
-// 0 is reserved; POSIX has special semantics for kill(pid, 0).
-#define __WASI_SIGHUP    (1)
-#define __WASI_SIGINT    (2)
-#define __WASI_SIGQUIT   (3)
-#define __WASI_SIGILL    (4)
-#define __WASI_SIGTRAP   (5)
-#define __WASI_SIGABRT   (6)
-#define __WASI_SIGBUS    (7)
-#define __WASI_SIGFPE    (8)
-#define __WASI_SIGKILL   (9)
-#define __WASI_SIGUSR1   (10)
-#define __WASI_SIGSEGV   (11)
-#define __WASI_SIGUSR2   (12)
-#define __WASI_SIGPIPE   (13)
-#define __WASI_SIGALRM   (14)
-#define __WASI_SIGTERM   (15)
-#define __WASI_SIGCHLD   (16)
-#define __WASI_SIGCONT   (17)
-#define __WASI_SIGSTOP   (18)
-#define __WASI_SIGTSTP   (19)
-#define __WASI_SIGTTIN   (20)
-#define __WASI_SIGTTOU   (21)
-#define __WASI_SIGURG    (22)
-#define __WASI_SIGXCPU   (23)
-#define __WASI_SIGXFSZ   (24)
-#define __WASI_SIGVTALRM (25)
-#define __WASI_SIGPROF   (26)
-#define __WASI_SIGWINCH  (27)
-#define __WASI_SIGPOLL   (28)
-#define __WASI_SIGPWR    (29)
-#define __WASI_SIGSYS    (30)
 
 typedef uint16_t __wasi_subclockflags_t;
 #define __WASI_SUBSCRIPTION_CLOCK_ABSTIME (0x0001)
@@ -629,17 +598,13 @@ typedef struct __wasi_addr_info_hints_t {
 #endif
 
 __wasi_errno_t wasmtime_ssp_args_get(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct argv_environ_values *arg_environ,
-#endif
     char **argv,
     char *argv_buf
 ) WASMTIME_SSP_SYSCALL_NAME(args_get) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_args_sizes_get(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct argv_environ_values *arg_environ,
-#endif
     size_t *argc,
     size_t *argv_buf_size
 ) WASMTIME_SSP_SYSCALL_NAME(args_sizes_get) __attribute__((__warn_unused_result__));
@@ -656,57 +621,43 @@ __wasi_errno_t wasmtime_ssp_clock_time_get(
 ) WASMTIME_SSP_SYSCALL_NAME(clock_time_get) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_environ_get(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct argv_environ_values *arg_environ,
-#endif
     char **environ,
     char *environ_buf
 ) WASMTIME_SSP_SYSCALL_NAME(environ_get) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_environ_sizes_get(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct argv_environ_values *arg_environ,
-#endif
     size_t *environ_count,
     size_t *environ_buf_size
 ) WASMTIME_SSP_SYSCALL_NAME(environ_sizes_get) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_fd_prestat_get(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_prestats *prestats,
-#endif
     __wasi_fd_t fd,
     __wasi_prestat_t *buf
 ) WASMTIME_SSP_SYSCALL_NAME(fd_prestat_get) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_fd_prestat_dir_name(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_prestats *prestats,
-#endif
     __wasi_fd_t fd,
     char *path,
     size_t path_len
 ) WASMTIME_SSP_SYSCALL_NAME(fd_prestat_dir_name) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_fd_close(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
     struct fd_prestats *prestats,
-#endif
     __wasi_fd_t fd
 ) WASMTIME_SSP_SYSCALL_NAME(fd_close) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_fd_datasync(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd
 ) WASMTIME_SSP_SYSCALL_NAME(fd_datasync) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_fd_pread(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd,
     const __wasi_iovec_t *iovs,
     size_t iovs_len,
@@ -715,9 +666,7 @@ __wasi_errno_t wasmtime_ssp_fd_pread(
 ) WASMTIME_SSP_SYSCALL_NAME(fd_pread) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_fd_pwrite(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd,
     const __wasi_ciovec_t *iovs,
     size_t iovs_len,
@@ -726,9 +675,7 @@ __wasi_errno_t wasmtime_ssp_fd_pwrite(
 ) WASMTIME_SSP_SYSCALL_NAME(fd_pwrite) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_fd_read(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd,
     const __wasi_iovec_t *iovs,
     size_t iovs_len,
@@ -736,18 +683,14 @@ __wasi_errno_t wasmtime_ssp_fd_read(
 ) WASMTIME_SSP_SYSCALL_NAME(fd_read) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_fd_renumber(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
     struct fd_prestats *prestats,
-#endif
     __wasi_fd_t from,
     __wasi_fd_t to
 ) WASMTIME_SSP_SYSCALL_NAME(fd_renumber) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_fd_seek(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd,
     __wasi_filedelta_t offset,
     __wasi_whence_t whence,
@@ -755,49 +698,37 @@ __wasi_errno_t wasmtime_ssp_fd_seek(
 ) WASMTIME_SSP_SYSCALL_NAME(fd_seek) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_fd_tell(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd,
     __wasi_filesize_t *newoffset
 ) WASMTIME_SSP_SYSCALL_NAME(fd_tell) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_fd_fdstat_get(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd,
     __wasi_fdstat_t *buf
 ) WASMTIME_SSP_SYSCALL_NAME(fd_fdstat_get) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_fd_fdstat_set_flags(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd,
     __wasi_fdflags_t flags
 ) WASMTIME_SSP_SYSCALL_NAME(fd_fdstat_set_flags) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_fd_fdstat_set_rights(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd,
     __wasi_rights_t fs_rights_base,
     __wasi_rights_t fs_rights_inheriting
 ) WASMTIME_SSP_SYSCALL_NAME(fd_fdstat_set_rights) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_fd_sync(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd
 ) WASMTIME_SSP_SYSCALL_NAME(fd_sync) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_fd_write(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd,
     const __wasi_ciovec_t *iovs,
     size_t iovs_len,
@@ -805,9 +736,7 @@ __wasi_errno_t wasmtime_ssp_fd_write(
 ) WASMTIME_SSP_SYSCALL_NAME(fd_write) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_fd_advise(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd,
     __wasi_filesize_t offset,
     __wasi_filesize_t len,
@@ -815,28 +744,22 @@ __wasi_errno_t wasmtime_ssp_fd_advise(
 ) WASMTIME_SSP_SYSCALL_NAME(fd_advise) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_fd_allocate(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd,
     __wasi_filesize_t offset,
     __wasi_filesize_t len
 ) WASMTIME_SSP_SYSCALL_NAME(fd_allocate) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_path_create_directory(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd,
     const char *path,
     size_t path_len
 ) WASMTIME_SSP_SYSCALL_NAME(path_create_directory) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_path_link(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
     struct fd_prestats *prestats,
-#endif
     __wasi_fd_t old_fd,
     __wasi_lookupflags_t old_flags,
     const char *old_path,
@@ -847,9 +770,7 @@ __wasi_errno_t wasmtime_ssp_path_link(
 ) WASMTIME_SSP_SYSCALL_NAME(path_link) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_path_open(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t dirfd,
     __wasi_lookupflags_t dirflags,
     const char *path,
@@ -862,9 +783,7 @@ __wasi_errno_t wasmtime_ssp_path_open(
 ) WASMTIME_SSP_SYSCALL_NAME(path_open) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_fd_readdir(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd,
     void *buf,
     size_t buf_len,
@@ -873,9 +792,7 @@ __wasi_errno_t wasmtime_ssp_fd_readdir(
 ) WASMTIME_SSP_SYSCALL_NAME(fd_readdir) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_path_readlink(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd,
     const char *path,
     size_t path_len,
@@ -885,9 +802,7 @@ __wasi_errno_t wasmtime_ssp_path_readlink(
 ) WASMTIME_SSP_SYSCALL_NAME(path_readlink) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_path_rename(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t old_fd,
     const char *old_path,
     size_t old_path_len,
@@ -897,17 +812,13 @@ __wasi_errno_t wasmtime_ssp_path_rename(
 ) WASMTIME_SSP_SYSCALL_NAME(path_rename) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_fd_filestat_get(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd,
     __wasi_filestat_t *buf
 ) WASMTIME_SSP_SYSCALL_NAME(fd_filestat_get) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_fd_filestat_set_times(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd,
     __wasi_timestamp_t st_atim,
     __wasi_timestamp_t st_mtim,
@@ -915,17 +826,13 @@ __wasi_errno_t wasmtime_ssp_fd_filestat_set_times(
 ) WASMTIME_SSP_SYSCALL_NAME(fd_filestat_set_times) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_fd_filestat_set_size(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd,
     __wasi_filesize_t st_size
 ) WASMTIME_SSP_SYSCALL_NAME(fd_filestat_set_size) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_path_filestat_get(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd,
     __wasi_lookupflags_t flags,
     const char *path,
@@ -934,9 +841,7 @@ __wasi_errno_t wasmtime_ssp_path_filestat_get(
 ) WASMTIME_SSP_SYSCALL_NAME(path_filestat_get) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_path_filestat_set_times(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd,
     __wasi_lookupflags_t flags,
     const char *path,
@@ -947,10 +852,8 @@ __wasi_errno_t wasmtime_ssp_path_filestat_set_times(
 ) WASMTIME_SSP_SYSCALL_NAME(path_filestat_set_times) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_path_symlink(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
     struct fd_prestats *prestats,
-#endif
     const char *old_path,
     size_t old_path_len,
     __wasi_fd_t fd,
@@ -959,47 +862,27 @@ __wasi_errno_t wasmtime_ssp_path_symlink(
 ) WASMTIME_SSP_SYSCALL_NAME(path_symlink) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_path_unlink_file(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd,
     const char *path,
     size_t path_len
 ) WASMTIME_SSP_SYSCALL_NAME(path_unlink_file) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_path_remove_directory(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd,
     const char *path,
     size_t path_len
 ) WASMTIME_SSP_SYSCALL_NAME(path_remove_directory) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_poll_oneoff(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     const __wasi_subscription_t *in,
     __wasi_event_t *out,
     size_t nsubscriptions,
     size_t *nevents
 ) WASMTIME_SSP_SYSCALL_NAME(poll_oneoff) __attribute__((__warn_unused_result__));
 
-#if 0
-/**
- * We throw exception in libc-wasi wrapper function wasi_proc_exit()
- * but not call this function.
- */
-_Noreturn void wasmtime_ssp_proc_exit(
-    __wasi_exitcode_t rval
-) WASMTIME_SSP_SYSCALL_NAME(proc_exit);
-#endif
-
-__wasi_errno_t wasmtime_ssp_proc_raise(
-    __wasi_signal_t sig
-) WASMTIME_SSP_SYSCALL_NAME(proc_raise) __attribute__((__warn_unused_result__));
-
 __wasi_errno_t wasmtime_ssp_random_get(
     void *buf,
     size_t buf_len
@@ -1007,50 +890,38 @@ __wasi_errno_t wasmtime_ssp_random_get(
 
 __wasi_errno_t
 wasi_ssp_sock_accept(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd, __wasi_fdflags_t flags, __wasi_fd_t *fd_new
 ) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t
 wasi_ssp_sock_addr_local(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd, __wasi_addr_t *addr
 ) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t
 wasi_ssp_sock_addr_remote(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd, __wasi_addr_t *addr
 ) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t
 wasi_ssp_sock_open(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t poolfd, __wasi_address_family_t af, __wasi_sock_type_t socktype,
     __wasi_fd_t *sockfd
 ) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t
 wasi_ssp_sock_bind(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds, struct addr_pool *addr_pool,
-#endif
     __wasi_fd_t fd, __wasi_addr_t *addr
 ) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t
 wasi_ssp_sock_addr_resolve(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds, char **ns_lookup_list,
-#endif
     const char *host, const char* service,
     __wasi_addr_info_hints_t *hints, __wasi_addr_info_t *addr_info,
     __wasi_size_t addr_info_size, __wasi_size_t *max_info_size
@@ -1058,88 +929,66 @@ wasi_ssp_sock_addr_resolve(
 
 __wasi_errno_t
 wasi_ssp_sock_connect(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds, struct addr_pool *addr_pool,
-#endif
     __wasi_fd_t fd, __wasi_addr_t *addr
 ) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t
 wasi_ssp_sock_get_recv_buf_size(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd, __wasi_size_t *size
 ) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t
 wasi_ssp_sock_get_reuse_addr(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd, uint8_t *reuse
 ) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t
 wasi_ssp_sock_get_reuse_port(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd, uint8_t *reuse
 ) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t
 wasi_ssp_sock_get_send_buf_size(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd, __wasi_size_t *size
 ) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t
 wasi_ssp_sock_set_recv_buf_size(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd, __wasi_size_t size
 ) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t
 wasi_ssp_sock_set_reuse_addr(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd, uint8_t reuse
 ) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t
 wasi_ssp_sock_set_reuse_port(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd, uint8_t reuse
 ) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t
 wasi_ssp_sock_set_send_buf_size(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd, __wasi_size_t size
 ) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t
 wasi_ssp_sock_listen(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t fd, __wasi_size_t backlog
 ) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_recv(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     void *buf,
     size_t buf_len,
@@ -1147,9 +996,7 @@ __wasi_errno_t wasmtime_ssp_sock_recv(
 ) WASMTIME_SSP_SYSCALL_NAME(sock_recv) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_recv_from(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     void *buf,
     size_t buf_len,
@@ -1159,9 +1006,7 @@ __wasi_errno_t wasmtime_ssp_sock_recv_from(
 ) WASMTIME_SSP_SYSCALL_NAME(sock_recv_from) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_send(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     const void *buf,
     size_t buf_len,
@@ -1169,9 +1014,7 @@ __wasi_errno_t wasmtime_ssp_sock_send(
 ) WASMTIME_SSP_SYSCALL_NAME(sock_send) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_send_to(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds, struct addr_pool *addr_pool,
-#endif
     __wasi_fd_t sock,
     const void *buf,
     size_t buf_len,
@@ -1181,317 +1024,239 @@ __wasi_errno_t wasmtime_ssp_sock_send_to(
 ) WASMTIME_SSP_SYSCALL_NAME(sock_send_to) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_shutdown(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock
 ) WASMTIME_SSP_SYSCALL_NAME(sock_shutdown) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_set_recv_timeout(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     uint64_t timeout_us
 ) WASMTIME_SSP_SYSCALL_NAME(sock_set_recv_timeout) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_get_recv_timeout(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     uint64_t *timeout_us
 ) WASMTIME_SSP_SYSCALL_NAME(sock_get_recv_timeout) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_set_send_timeout(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     uint64_t timeout_us
 ) WASMTIME_SSP_SYSCALL_NAME(sock_set_send_timeout) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_get_send_timeout(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     uint64_t *timeout_us
 ) WASMTIME_SSP_SYSCALL_NAME(sock_get_send_timeout) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_set_send_buf_size(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     size_t bufsiz
 ) WASMTIME_SSP_SYSCALL_NAME(sock_set_send_buf_size) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_get_send_buf_size(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     size_t *bufsiz
 ) WASMTIME_SSP_SYSCALL_NAME(sock_get_send_buf_size) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_set_recv_buf_size(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     size_t bufsiz
 ) WASMTIME_SSP_SYSCALL_NAME(sock_set_recv_buf_size) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_get_recv_buf_size(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     size_t *bufsiz
 ) WASMTIME_SSP_SYSCALL_NAME(sock_get_recv_buf_size) __attribute__((__warn_unused_result__));
 
 
 __wasi_errno_t wasmtime_ssp_sock_set_keep_alive(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     bool is_enabled
 ) WASMTIME_SSP_SYSCALL_NAME(sock_set_keep_alive) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_get_keep_alive(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     bool *is_enabled
 ) WASMTIME_SSP_SYSCALL_NAME(sock_get_keep_alive) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_set_reuse_addr(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     bool is_enabled
 ) WASMTIME_SSP_SYSCALL_NAME(sock_set_reuse_addr) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_get_reuse_addr(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     bool *is_enabled
 ) WASMTIME_SSP_SYSCALL_NAME(sock_get_reuse_addr) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_set_reuse_port(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     bool is_enabled
 ) WASMTIME_SSP_SYSCALL_NAME(sock_set_reuse_port) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_get_reuse_port(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     bool *is_enabled
 ) WASMTIME_SSP_SYSCALL_NAME(sock_get_reuse_port) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_set_linger(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     bool is_enabled,
     int linger_s
 ) WASMTIME_SSP_SYSCALL_NAME(sock_set_linger) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_get_linger(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock, bool *is_enabled, int *linger_s
 ) WASMTIME_SSP_SYSCALL_NAME(sock_get_linger) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_set_broadcast(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     bool is_enabled
 ) WASMTIME_SSP_SYSCALL_NAME(sock_set_broadcast) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_get_broadcast(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     bool *is_enabled
 ) WASMTIME_SSP_SYSCALL_NAME(sock_get_broadcast) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_set_tcp_no_delay(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     bool is_enabled
 ) WASMTIME_SSP_SYSCALL_NAME(sock_set_tcp_no_delay) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_get_tcp_no_delay(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     bool *is_enabled
 ) WASMTIME_SSP_SYSCALL_NAME(sock_get_tcp_no_delay) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_set_tcp_quick_ack(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     bool is_enabled
 ) WASMTIME_SSP_SYSCALL_NAME(sock_set_tcp_quick_ack) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_get_tcp_quick_ack(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     bool *is_enabled
 ) WASMTIME_SSP_SYSCALL_NAME(sock_get_tcp_quick_ack) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_set_tcp_keep_idle(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     uint32_t time_s
 ) WASMTIME_SSP_SYSCALL_NAME(sock_set_tcp_keep_idle) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_get_tcp_keep_idle(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     uint32_t *time_s
 ) WASMTIME_SSP_SYSCALL_NAME(sock_get_tcp_keep_idle) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_set_tcp_keep_intvl(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     uint32_t time_s
 ) WASMTIME_SSP_SYSCALL_NAME(sock_set_tcp_keep_intvl) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_get_tcp_keep_intvl(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     uint32_t *time_s
 ) WASMTIME_SSP_SYSCALL_NAME(sock_get_tcp_keep_intvl) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_set_tcp_fastopen_connect(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     bool is_enabled
 ) WASMTIME_SSP_SYSCALL_NAME(sock_set_tcp_fastopen_connect) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_get_tcp_fastopen_connect(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     bool *is_enabled
 ) WASMTIME_SSP_SYSCALL_NAME(sock_get_tcp_fastopen_connect) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_set_ip_multicast_loop(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     bool ipv6,
     bool is_enabled
 ) WASMTIME_SSP_SYSCALL_NAME(sock_set_ip_multicast_loop) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_get_ip_multicast_loop(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     bool ipv6,
     bool *is_enabled
 ) WASMTIME_SSP_SYSCALL_NAME(sock_get_ip_multicast_loop) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_set_ip_add_membership(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     __wasi_addr_ip_t *imr_multiaddr,
     uint32_t imr_interface
 ) WASMTIME_SSP_SYSCALL_NAME(sock_set_ip_add_membership) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_set_ip_drop_membership(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     __wasi_addr_ip_t *imr_multiaddr,
     uint32_t imr_interface
 ) WASMTIME_SSP_SYSCALL_NAME(sock_set_ip_drop_membership) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_set_ip_ttl(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     uint8_t ttl_s
 ) WASMTIME_SSP_SYSCALL_NAME(sock_set_ip_ttl) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_get_ip_ttl(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     uint8_t *ttl_s
 ) WASMTIME_SSP_SYSCALL_NAME(sock_get_ip_ttl) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_set_ip_multicast_ttl(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     uint8_t ttl_s
 ) WASMTIME_SSP_SYSCALL_NAME(sock_set_ip_multicast_ttl) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_get_ip_multicast_ttl(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     uint8_t *ttl_s
 ) WASMTIME_SSP_SYSCALL_NAME(sock_get_ip_multicast_ttl) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_set_ipv6_only(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     bool is_enabled
 ) WASMTIME_SSP_SYSCALL_NAME(sock_set_ipv6_only) __attribute__((__warn_unused_result__));
 
 __wasi_errno_t wasmtime_ssp_sock_get_ipv6_only(
-#if !defined(WASMTIME_SSP_STATIC_CURFDS)
     struct fd_table *curfds,
-#endif
     __wasi_fd_t sock,
     bool *is_enabled
 ) WASMTIME_SSP_SYSCALL_NAME(sock_get_ipv6_only) __attribute__((__warn_unused_result__));

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 159 - 427
core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c


+ 0 - 6
core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/ssp_config.h

@@ -101,12 +101,6 @@
 #define st_mtim st_mtimespec
 #endif
 
-#ifdef __APPLE__
-#define CONFIG_TLS_USE_GSBASE 1
-#else
-#define CONFIG_TLS_USE_GSBASE 0
-#endif
-
 #if !defined(BH_PLATFORM_LINUX_SGX)
 /* Clang's __GNUC_PREREQ macro has a different meaning than GCC one,
 so we have to handle this case specially */

+ 45 - 13
core/iwasm/libraries/thread-mgr/thread_manager.c

@@ -480,9 +480,6 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
     wasm_module_inst_t module_inst = get_module_inst(exec_env);
     wasm_module_t module;
     wasm_module_inst_t new_module_inst;
-#if WASM_ENABLE_LIBC_WASI != 0
-    WASIContext *wasi_ctx;
-#endif
     WASMExecEnv *new_exec_env;
     uint32 aux_stack_start, aux_stack_size;
     uint32 stack_size = 8192;
@@ -520,10 +517,7 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
     wasm_runtime_set_custom_data_internal(
         new_module_inst, wasm_runtime_get_custom_data(module_inst));
 
-#if WASM_ENABLE_LIBC_WASI != 0
-    wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
-    wasm_runtime_set_wasi_ctx(new_module_inst, wasi_ctx);
-#endif
+    wasm_native_inherit_contexts(new_module_inst, module_inst);
 
     new_exec_env = wasm_exec_env_create_internal(new_module_inst,
                                                  exec_env->wasm_stack_size);
@@ -1260,12 +1254,8 @@ set_exception_visitor(void *node, void *user_data)
 
         exception_lock(wasm_inst);
         if (data->exception != NULL) {
-            /* Only spread non "wasi proc exit" exception */
-            if (strcmp(data->exception, "wasi proc exit")) {
-                snprintf(wasm_inst->cur_exception,
-                         sizeof(wasm_inst->cur_exception), "Exception: %s",
-                         data->exception);
-            }
+            snprintf(wasm_inst->cur_exception, sizeof(wasm_inst->cur_exception),
+                     "Exception: %s", data->exception);
         }
         else {
             wasm_inst->cur_exception[0] = '\0';
@@ -1328,6 +1318,48 @@ wasm_cluster_spread_custom_data(WASMModuleInstanceCommon *module_inst,
     }
 }
 
+#if WASM_ENABLE_MODULE_INST_CONTEXT != 0
+struct inst_set_context_data {
+    void *key;
+    void *ctx;
+};
+
+static void
+set_context_visitor(void *node, void *user_data)
+{
+    WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
+    WASMModuleInstanceCommon *module_inst = get_module_inst(curr_exec_env);
+    const struct inst_set_context_data *data = user_data;
+
+    wasm_runtime_set_context(module_inst, data->key, data->ctx);
+}
+
+void
+wasm_cluster_set_context(WASMModuleInstanceCommon *module_inst, void *key,
+                         void *ctx)
+{
+    WASMExecEnv *exec_env = wasm_clusters_search_exec_env(module_inst);
+
+    if (exec_env == NULL) {
+        /* Maybe threads have not been started yet. */
+        wasm_runtime_set_context(module_inst, key, ctx);
+    }
+    else {
+        WASMCluster *cluster;
+        struct inst_set_context_data data;
+        data.key = key;
+        data.ctx = ctx;
+
+        cluster = wasm_exec_env_get_cluster(exec_env);
+        bh_assert(cluster);
+
+        os_mutex_lock(&cluster->lock);
+        traverse_list(&cluster->exec_env_list, set_context_visitor, &data);
+        os_mutex_unlock(&cluster->lock);
+    }
+}
+#endif /* WASM_ENABLE_MODULE_INST_CONTEXT != 0 */
+
 bool
 wasm_cluster_is_thread_terminated(WASMExecEnv *exec_env)
 {

+ 4 - 0
core/iwasm/libraries/thread-mgr/thread_manager.h

@@ -151,6 +151,10 @@ void
 wasm_cluster_spread_custom_data(WASMModuleInstanceCommon *module_inst,
                                 void *custom_data);
 
+void
+wasm_cluster_set_context(WASMModuleInstanceCommon *module_inst, void *key,
+                         void *ctx);
+
 bool
 wasm_cluster_is_thread_terminated(WASMExecEnv *exec_env);
 

+ 8 - 8
core/shared/platform/common/posix/posix_socket.c

@@ -53,7 +53,7 @@ sockaddr_to_bh_sockaddr(const struct sockaddr *sockaddr,
             struct sockaddr_in *addr = (struct sockaddr_in *)sockaddr;
 
             bh_sockaddr->port = ntohs(addr->sin_port);
-            bh_sockaddr->addr_bufer.ipv4 = ntohl(addr->sin_addr.s_addr);
+            bh_sockaddr->addr_buffer.ipv4 = ntohl(addr->sin_addr.s_addr);
             bh_sockaddr->is_ipv4 = true;
             return BHT_OK;
         }
@@ -65,12 +65,12 @@ sockaddr_to_bh_sockaddr(const struct sockaddr *sockaddr,
 
             bh_sockaddr->port = ntohs(addr->sin6_port);
 
-            for (i = 0; i < sizeof(bh_sockaddr->addr_bufer.ipv6)
-                                / sizeof(bh_sockaddr->addr_bufer.ipv6[0]);
+            for (i = 0; i < sizeof(bh_sockaddr->addr_buffer.ipv6)
+                                / sizeof(bh_sockaddr->addr_buffer.ipv6[0]);
                  i++) {
                 uint16 part_addr = addr->sin6_addr.s6_addr[i * 2]
                                    | (addr->sin6_addr.s6_addr[i * 2 + 1] << 8);
-                bh_sockaddr->addr_bufer.ipv6[i] = ntohs(part_addr);
+                bh_sockaddr->addr_buffer.ipv6[i] = ntohs(part_addr);
             }
 
             bh_sockaddr->is_ipv4 = false;
@@ -91,7 +91,7 @@ bh_sockaddr_to_sockaddr(const bh_sockaddr_t *bh_sockaddr,
         struct sockaddr_in *addr = (struct sockaddr_in *)sockaddr;
         addr->sin_port = htons(bh_sockaddr->port);
         addr->sin_family = AF_INET;
-        addr->sin_addr.s_addr = htonl(bh_sockaddr->addr_bufer.ipv4);
+        addr->sin_addr.s_addr = htonl(bh_sockaddr->addr_buffer.ipv4);
         *socklen = sizeof(*addr);
     }
 #ifdef IPPROTO_IPV6
@@ -101,10 +101,10 @@ bh_sockaddr_to_sockaddr(const bh_sockaddr_t *bh_sockaddr,
         addr->sin6_port = htons(bh_sockaddr->port);
         addr->sin6_family = AF_INET6;
 
-        for (i = 0; i < sizeof(bh_sockaddr->addr_bufer.ipv6)
-                            / sizeof(bh_sockaddr->addr_bufer.ipv6[0]);
+        for (i = 0; i < sizeof(bh_sockaddr->addr_buffer.ipv6)
+                            / sizeof(bh_sockaddr->addr_buffer.ipv6[0]);
              i++) {
-            uint16 part_addr = htons(bh_sockaddr->addr_bufer.ipv6[i]);
+            uint16 part_addr = htons(bh_sockaddr->addr_buffer.ipv6[i]);
             addr->sin6_addr.s6_addr[i * 2] = 0xff & part_addr;
             addr->sin6_addr.s6_addr[i * 2 + 1] = (0xff00 & part_addr) >> 8;
         }

+ 1 - 1
core/shared/platform/esp-idf/espidf_socket.c

@@ -30,7 +30,7 @@ sockaddr_to_bh_sockaddr(const struct sockaddr *sockaddr, socklen_t socklen,
             assert(socklen >= sizeof(struct sockaddr_in));
 
             bh_sockaddr->port = ntohs(addr->sin_port);
-            bh_sockaddr->addr_bufer.ipv4 = ntohl(addr->sin_addr.s_addr);
+            bh_sockaddr->addr_buffer.ipv4 = ntohl(addr->sin_addr.s_addr);
             bh_sockaddr->is_ipv4 = true;
             return BHT_OK;
         }

+ 1 - 1
core/shared/platform/include/platform_api_extension.h

@@ -342,7 +342,7 @@ typedef union {
 } bh_ip_addr_buffer_t;
 
 typedef struct {
-    bh_ip_addr_buffer_t addr_bufer;
+    bh_ip_addr_buffer_t addr_buffer;
     uint16 port;
     bool is_ipv4;
 } bh_sockaddr_t;

+ 2 - 2
core/shared/platform/linux-sgx/sgx_socket.c

@@ -261,7 +261,7 @@ sockaddr_to_bh_sockaddr(const struct sockaddr *sockaddr, socklen_t socklen,
             assert(socklen >= sizeof(struct sockaddr_in));
 
             bh_sockaddr->port = ntohs(addr->sin_port);
-            bh_sockaddr->addr_bufer.ipv4 = ntohl(addr->sin_addr.s_addr);
+            bh_sockaddr->addr_buffer.ipv4 = ntohl(addr->sin_addr.s_addr);
             bh_sockaddr->is_ipv4 = true;
             return BHT_OK;
         }
@@ -279,7 +279,7 @@ bh_sockaddr_to_sockaddr(const bh_sockaddr_t *bh_sockaddr,
         struct sockaddr_in *addr = (struct sockaddr_in *)sockaddr;
         addr->sin_port = htons(bh_sockaddr->port);
         addr->sin_family = AF_INET;
-        addr->sin_addr.s_addr = htonl(bh_sockaddr->addr_bufer.ipv4);
+        addr->sin_addr.s_addr = htonl(bh_sockaddr->addr_buffer.ipv4);
         *socklen = sizeof(*addr);
         return BHT_OK;
     }

+ 8 - 0
product-mini/platforms/nuttx/wamr.mk

@@ -253,10 +253,18 @@ CSRCS += posix.c
 CSRCS += random.c
 CSRCS += str.c
 VPATH += $(IWASM_ROOT)/libraries/libc-wasi/sandboxed-system-primitives/src
+# todo: use Kconfig select instead
+CONFIG_INTERPRETERS_WAMR_MODULE_INSTANCE_CONTEXT = y
 else
 CFLAGS += -DWASM_ENABLE_LIBC_WASI=0
 endif
 
+ifeq ($(CONFIG_INTERPRETERS_WAMR_MODULE_INSTANCE_CONTEXT),y)
+CFLAGS += -DWASM_ENABLE_MODULE_INST_CONTEXT=1
+else
+CFLAGS += -DWASM_ENABLE_MODULE_INST_CONTEXT=0
+endif
+
 ifeq ($(CONFIG_INTERPRETERS_WAMR_MULTI_MODULE),y)
 CFLAGS += -DWASM_ENABLE_MULTI_MODULE=1
 else

+ 1 - 5
product-mini/platforms/posix/main.c

@@ -921,12 +921,8 @@ main(int argc, char *argv[])
 
 #if WASM_ENABLE_LIBC_WASI != 0
     if (ret == 0) {
-        /* wait for threads to finish and propagate wasi exit code. */
+        /* propagate wasi exit code. */
         ret = wasm_runtime_get_wasi_exit_code(wasm_module_inst);
-        if (wasm_runtime_get_exception(wasm_module_inst)) {
-            /* got an exception in spawned thread */
-            ret = 1;
-        }
     }
 #endif
 

+ 1 - 5
product-mini/platforms/windows/main.c

@@ -554,12 +554,8 @@ main(int argc, char *argv[])
 
 #if WASM_ENABLE_LIBC_WASI != 0
     if (ret == 0) {
-        /* wait for threads to finish and propagate wasi exit code. */
+        /* propagate wasi exit code. */
         ret = wasm_runtime_get_wasi_exit_code(wasm_module_inst);
-        if (wasm_runtime_get_exception(wasm_module_inst)) {
-            /* got an exception in spawned thread */
-            ret = 1;
-        }
     }
 #endif
 

+ 1 - 0
samples/inst-context-threads/.gitignore

@@ -0,0 +1 @@
+/out/

+ 92 - 0
samples/inst-context-threads/CMakeLists.txt

@@ -0,0 +1,92 @@
+# Copyright (C) 2019 Intel Corporation.  All rights reserved.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+cmake_minimum_required (VERSION 3.14)
+
+include(CheckPIESupported)
+
+if (NOT WAMR_BUILD_PLATFORM STREQUAL "windows")
+  project (inst-context)
+else()
+  project (inst-context C ASM)
+  enable_language (ASM_MASM)
+endif()
+
+################  runtime settings  ################
+string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
+if (APPLE)
+  add_definitions(-DBH_PLATFORM_DARWIN)
+endif ()
+
+# Reset default linker flags
+set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
+set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
+
+# WAMR features switch
+
+# Set WAMR_BUILD_TARGET, currently values supported:
+# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
+# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
+if (NOT DEFINED WAMR_BUILD_TARGET)
+  if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
+    set (WAMR_BUILD_TARGET "AARCH64")
+  elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
+    set (WAMR_BUILD_TARGET "RISCV64")
+  elseif (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 Debug)
+endif ()
+
+set (WAMR_BUILD_INTERP 1)
+set (WAMR_BUILD_AOT 1)
+set (WAMR_BUILD_JIT 0)
+set (WAMR_BUILD_LIBC_BUILTIN 0)
+set (WAMR_BUILD_LIB_WASI_THREADS 1)
+
+if (NOT MSVC)
+  set (WAMR_BUILD_LIBC_WASI 1)
+endif ()
+
+if (NOT MSVC)
+  # linker flags
+  if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
+    set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
+  endif ()
+  set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
+  if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
+    if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
+      set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register")
+    endif ()
+  endif ()
+endif ()
+
+# build out vmlib
+set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
+include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
+
+add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
+
+################  application related  ################
+include_directories(${CMAKE_CURRENT_LIST_DIR}/src)
+include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
+
+add_executable (inst-context src/main.c src/native_impl.c ${UNCOMMON_SHARED_SOURCE})
+
+check_pie_supported()
+set_target_properties (inst-context PROPERTIES POSITION_INDEPENDENT_CODE ON)
+
+if (APPLE)
+  target_link_libraries (inst-context vmlib -lm -ldl -lpthread)
+else ()
+  target_link_libraries (inst-context vmlib -lm -ldl -lpthread -lrt)
+endif ()

+ 4 - 0
samples/inst-context-threads/README.md

@@ -0,0 +1,4 @@
+The "inst-context" sample project
+=================================
+
+This sample demonstrates module instance context API.

+ 61 - 0
samples/inst-context-threads/build.sh

@@ -0,0 +1,61 @@
+#
+# Copyright (C) 2019 Intel Corporation.  All rights reserved.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+
+#!/bin/bash
+
+CURR_DIR=$PWD
+WAMR_DIR=${PWD}/../..
+OUT_DIR=${PWD}/out
+
+WASM_APPS=${PWD}/wasm-apps
+
+
+rm -rf ${OUT_DIR}
+mkdir ${OUT_DIR}
+mkdir ${OUT_DIR}/wasm-apps
+
+
+echo "#####################build inst-context project"
+cd ${CURR_DIR}
+mkdir -p cmake_build
+cd cmake_build
+cmake ..
+make -j ${nproc}
+if [ $? != 0 ];then
+    echo "BUILD_FAIL inst-context exit as $?\n"
+    exit 2
+fi
+
+cp -a inst-context ${OUT_DIR}
+
+echo -e "\n"
+
+echo "#####################build wasm apps"
+
+cd ${WASM_APPS}
+
+for i in `ls *.c`
+do
+APP_SRC="$i"
+OUT_FILE=${i%.*}.wasm
+
+# use WAMR SDK to build out the .wasm binary
+# require wasi-sdk with wasi-threads support. (wasi-sdk-20.0 or later)
+/opt/wasi-sdk/bin/clang     \
+        --target=wasm32-wasi-threads \
+        -pthread \
+        -Wl,--import-memory \
+        -Wl,--export-memory \
+        -Wl,--max-memory=655360 \
+        -o ${OUT_DIR}/wasm-apps/${OUT_FILE} ${APP_SRC}
+
+
+if [ -f ${OUT_DIR}/wasm-apps/${OUT_FILE} ]; then
+        echo "build ${OUT_FILE} success"
+else
+        echo "build ${OUT_FILE} fail"
+fi
+done
+echo "####################build wasm apps done"

+ 3 - 0
samples/inst-context-threads/run.sh

@@ -0,0 +1,3 @@
+#!/bin/bash
+
+out/inst-context -f out/wasm-apps/testapp.wasm

+ 151 - 0
samples/inst-context-threads/src/main.c

@@ -0,0 +1,151 @@
+
+/*
+ * Copyright (C) 2019 Intel Corporation.  All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include "wasm_export.h"
+#include "bh_read_file.h"
+#include "bh_getopt.h"
+#include "my_context.h"
+
+void
+set_context(wasm_exec_env_t exec_env, int32_t n);
+int32_t
+get_context(wasm_exec_env_t exec_env);
+
+void *my_context_key;
+struct my_context my_context;
+int my_dtor_called;
+
+wasm_module_inst_t module_inst = NULL;
+
+void
+print_usage(void)
+{
+    fprintf(stdout, "Options:\r\n");
+    fprintf(stdout, "  -f [path of wasm file] \n");
+}
+
+void
+my_context_dtor(wasm_module_inst_t inst, void *ctx)
+{
+    printf("%s called\n", __func__);
+    my_dtor_called++;
+    bh_assert(ctx == &my_context);
+    bh_assert(inst == module_inst);
+}
+
+int
+main(int argc, char *argv_main[])
+{
+    static char global_heap_buf[512 * 1024];
+    char *buffer;
+    char error_buf[128];
+    int opt;
+    char *wasm_path = NULL;
+    int exit_code = 1;
+
+    wasm_module_t module = NULL;
+    uint32 buf_size, stack_size = 8092, heap_size = 8092;
+
+    RuntimeInitArgs init_args;
+    memset(&init_args, 0, sizeof(RuntimeInitArgs));
+
+    while ((opt = getopt(argc, argv_main, "hf:")) != -1) {
+        switch (opt) {
+            case 'f':
+                wasm_path = optarg;
+                break;
+            case 'h':
+                print_usage();
+                return 0;
+            case '?':
+                print_usage();
+                return 0;
+        }
+    }
+    if (optind == 1) {
+        print_usage();
+        return 0;
+    }
+
+    // Define an array of NativeSymbol for the APIs to be exported.
+    // Note: the array must be static defined since runtime
+    //            will keep it after registration
+    // For the function signature specifications, goto the link:
+    // https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/export_native_api.md
+
+    static NativeSymbol native_symbols[] = {
+        { "set_context", set_context, "(i)", NULL },
+        { "get_context", get_context, "()i", NULL },
+    };
+
+    init_args.mem_alloc_type = Alloc_With_Pool;
+    init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
+    init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
+
+    // Native symbols need below registration phase
+    init_args.n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol);
+    init_args.native_module_name = "env";
+    init_args.native_symbols = native_symbols;
+
+    if (!wasm_runtime_full_init(&init_args)) {
+        printf("Init runtime environment failed.\n");
+        return -1;
+    }
+
+    my_context_key = wasm_runtime_create_context_key(my_context_dtor);
+    if (!my_context_key) {
+        printf("wasm_runtime_create_context_key failed.\n");
+        return -1;
+    }
+
+    buffer = bh_read_file_to_buffer(wasm_path, &buf_size);
+
+    if (!buffer) {
+        printf("Open wasm app file [%s] failed.\n", wasm_path);
+        goto fail;
+    }
+
+    module = wasm_runtime_load((uint8 *)buffer, buf_size, error_buf,
+                               sizeof(error_buf));
+    if (!module) {
+        printf("Load wasm module failed. error: %s\n", error_buf);
+        goto fail;
+    }
+
+    module_inst = wasm_runtime_instantiate(module, stack_size, heap_size,
+                                           error_buf, sizeof(error_buf));
+
+    if (!module_inst) {
+        printf("Instantiate wasm module failed. error: %s\n", error_buf);
+        goto fail;
+    }
+
+    char *args[] = {
+        "testapp",
+    };
+    wasm_application_execute_main(module_inst, 1, args);
+    const char *exc = wasm_runtime_get_exception(module_inst);
+    if (exc != NULL) {
+        printf("call wasm function calculate failed. error: %s\n", exc);
+        goto fail;
+    }
+
+    exit_code = 0;
+fail:
+    if (module_inst) {
+        bh_assert(my_dtor_called == 0);
+        wasm_runtime_deinstantiate(module_inst);
+        bh_assert(my_dtor_called == 1);
+    }
+    if (module)
+        wasm_runtime_unload(module);
+    if (buffer)
+        BH_FREE(buffer);
+    if (my_context_key)
+        wasm_runtime_destroy_context_key(my_context_key);
+    wasm_runtime_destroy();
+    return exit_code;
+}

+ 11 - 0
samples/inst-context-threads/src/my_context.h

@@ -0,0 +1,11 @@
+/*
+ * Copyright (C) 2023 Midokura Japan KK.  All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+struct my_context {
+    int x;
+};
+
+extern void *my_context_key;
+extern struct my_context my_context;

+ 32 - 0
samples/inst-context-threads/src/native_impl.c

@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2023 Midokura Japan KK.  All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include <stddef.h>
+#include <stdio.h>
+
+#include "wasm_export.h"
+#include "my_context.h"
+
+void
+set_context(wasm_exec_env_t exec_env, int32_t n)
+{
+    wasm_module_inst_t inst = wasm_runtime_get_module_inst(exec_env);
+    printf("%s called on module inst %p\n", __func__, inst);
+    struct my_context *ctx = &my_context;
+    ctx->x = n;
+    wasm_runtime_set_context_spread(inst, my_context_key, ctx);
+}
+
+int32_t
+get_context(wasm_exec_env_t exec_env)
+{
+    wasm_module_inst_t inst = wasm_runtime_get_module_inst(exec_env);
+    printf("%s called on module inst %p\n", __func__, inst);
+    struct my_context *ctx = wasm_runtime_get_context(inst, my_context_key);
+    if (ctx == NULL) {
+        return -1;
+    }
+    return ctx->x;
+}

+ 65 - 0
samples/inst-context-threads/wasm-apps/testapp.c

@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2023 Midokura Japan KK.  All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include <assert.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+void
+set_context(int32_t n) __attribute__((import_module("env")))
+__attribute__((import_name("set_context")));
+
+int32_t
+get_context() __attribute__((import_module("env")))
+__attribute__((import_name("get_context")));
+
+void *
+start(void *vp)
+{
+    int32_t v;
+
+    printf("thread started\n");
+
+    printf("confirming the initial state on thread\n");
+    v = get_context();
+    assert(v == -1);
+
+    printf("setting the context on thread\n");
+    set_context(1234);
+
+    printf("confirming the context on thread\n");
+    v = get_context();
+    assert(v == 1234);
+    return NULL;
+}
+
+int
+main()
+{
+    pthread_t t1;
+    int32_t v;
+    int ret;
+
+    printf("confirming the initial state on main\n");
+    v = get_context();
+    assert(v == -1);
+
+    printf("creating a thread\n");
+    ret = pthread_create(&t1, NULL, start, NULL);
+    assert(ret == 0);
+    void *val;
+    ret = pthread_join(t1, &val);
+    assert(ret == 0);
+    printf("joined the thread\n");
+
+    printf("confirming the context propagated from the thread on main\n");
+    v = get_context();
+    assert(v == 1234);
+
+    printf("success\n");
+    return 0;
+}

+ 1 - 0
samples/inst-context/.gitignore

@@ -0,0 +1 @@
+/out/

+ 91 - 0
samples/inst-context/CMakeLists.txt

@@ -0,0 +1,91 @@
+# Copyright (C) 2019 Intel Corporation.  All rights reserved.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+cmake_minimum_required (VERSION 3.14)
+
+include(CheckPIESupported)
+
+if (NOT WAMR_BUILD_PLATFORM STREQUAL "windows")
+  project (inst-context)
+else()
+  project (inst-context C ASM)
+  enable_language (ASM_MASM)
+endif()
+
+################  runtime settings  ################
+string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
+if (APPLE)
+  add_definitions(-DBH_PLATFORM_DARWIN)
+endif ()
+
+# Reset default linker flags
+set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
+set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
+
+# WAMR features switch
+
+# Set WAMR_BUILD_TARGET, currently values supported:
+# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
+# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
+if (NOT DEFINED WAMR_BUILD_TARGET)
+  if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
+    set (WAMR_BUILD_TARGET "AARCH64")
+  elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
+    set (WAMR_BUILD_TARGET "RISCV64")
+  elseif (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 Debug)
+endif ()
+
+set (WAMR_BUILD_INTERP 1)
+set (WAMR_BUILD_AOT 1)
+set (WAMR_BUILD_JIT 0)
+set (WAMR_BUILD_LIBC_BUILTIN 1)
+
+if (NOT MSVC)
+  set (WAMR_BUILD_LIBC_WASI 1)
+endif ()
+
+if (NOT MSVC)
+  # linker flags
+  if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
+    set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
+  endif ()
+  set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
+  if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
+    if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
+      set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register")
+    endif ()
+  endif ()
+endif ()
+
+# build out vmlib
+set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
+include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
+
+add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
+
+################  application related  ################
+include_directories(${CMAKE_CURRENT_LIST_DIR}/src)
+include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
+
+add_executable (inst-context src/main.c src/native_impl.c ${UNCOMMON_SHARED_SOURCE})
+
+check_pie_supported()
+set_target_properties (inst-context PROPERTIES POSITION_INDEPENDENT_CODE ON)
+
+if (APPLE)
+  target_link_libraries (inst-context vmlib -lm -ldl -lpthread)
+else ()
+  target_link_libraries (inst-context vmlib -lm -ldl -lpthread -lrt)
+endif ()

+ 4 - 0
samples/inst-context/README.md

@@ -0,0 +1,4 @@
+The "inst-context" sample project
+=================================
+
+This sample demonstrates module instance context API.

+ 63 - 0
samples/inst-context/build.sh

@@ -0,0 +1,63 @@
+#
+# Copyright (C) 2019 Intel Corporation.  All rights reserved.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+
+#!/bin/bash
+
+CURR_DIR=$PWD
+WAMR_DIR=${PWD}/../..
+OUT_DIR=${PWD}/out
+
+WASM_APPS=${PWD}/wasm-apps
+
+
+rm -rf ${OUT_DIR}
+mkdir ${OUT_DIR}
+mkdir ${OUT_DIR}/wasm-apps
+
+
+echo "#####################build inst-context project"
+cd ${CURR_DIR}
+mkdir -p cmake_build
+cd cmake_build
+cmake ..
+make -j ${nproc}
+if [ $? != 0 ];then
+    echo "BUILD_FAIL inst-context exit as $?\n"
+    exit 2
+fi
+
+cp -a inst-context ${OUT_DIR}
+
+echo -e "\n"
+
+echo "#####################build wasm apps"
+
+cd ${WASM_APPS}
+
+for i in `ls *.c`
+do
+APP_SRC="$i"
+OUT_FILE=${i%.*}.wasm
+
+# use WAMR SDK to build out the .wasm binary
+/opt/wasi-sdk/bin/clang     \
+        --target=wasm32 -O0 -z stack-size=4096 -Wl,--initial-memory=65536 \
+        --sysroot=${WAMR_DIR}/wamr-sdk/app/libc-builtin-sysroot  \
+        -Wl,--allow-undefined-file=${WAMR_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt \
+        -Wl,--strip-all,--no-entry -nostdlib \
+        -Wl,--export=generate_float \
+        -Wl,--export=float_to_string \
+        -Wl,--export=calculate\
+        -Wl,--allow-undefined \
+        -o ${OUT_DIR}/wasm-apps/${OUT_FILE} ${APP_SRC}
+
+
+if [ -f ${OUT_DIR}/wasm-apps/${OUT_FILE} ]; then
+        echo "build ${OUT_FILE} success"
+else
+        echo "build ${OUT_FILE} fail"
+fi
+done
+echo "####################build wasm apps done"

+ 3 - 0
samples/inst-context/run.sh

@@ -0,0 +1,3 @@
+#!/bin/bash
+
+out/inst-context -f out/wasm-apps/testapp.wasm

+ 166 - 0
samples/inst-context/src/main.c

@@ -0,0 +1,166 @@
+
+/*
+ * Copyright (C) 2019 Intel Corporation.  All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include "wasm_export.h"
+#include "bh_read_file.h"
+#include "bh_getopt.h"
+#include "my_context.h"
+
+int32_t
+add_native(int32_t n);
+void *my_context_key;
+struct my_context my_context;
+int my_dtor_called;
+
+wasm_module_inst_t module_inst = NULL;
+
+void
+print_usage(void)
+{
+    fprintf(stdout, "Options:\r\n");
+    fprintf(stdout, "  -f [path of wasm file] \n");
+}
+
+void
+my_context_dtor(wasm_module_inst_t inst, void *ctx)
+{
+    printf("%s called\n", __func__);
+    my_dtor_called++;
+    bh_assert(ctx == &my_context);
+    bh_assert(inst == module_inst);
+}
+
+int
+main(int argc, char *argv_main[])
+{
+    static char global_heap_buf[512 * 1024];
+    char *buffer;
+    char error_buf[128];
+    int opt;
+    char *wasm_path = NULL;
+
+    wasm_module_t module = NULL;
+    wasm_exec_env_t exec_env = NULL;
+    uint32 buf_size, stack_size = 8092, heap_size = 8092;
+
+    RuntimeInitArgs init_args;
+    memset(&init_args, 0, sizeof(RuntimeInitArgs));
+
+    while ((opt = getopt(argc, argv_main, "hf:")) != -1) {
+        switch (opt) {
+            case 'f':
+                wasm_path = optarg;
+                break;
+            case 'h':
+                print_usage();
+                return 0;
+            case '?':
+                print_usage();
+                return 0;
+        }
+    }
+    if (optind == 1) {
+        print_usage();
+        return 0;
+    }
+
+    // Define an array of NativeSymbol for the APIs to be exported.
+    // Note: the array must be static defined since runtime
+    //            will keep it after registration
+    // For the function signature specifications, goto the link:
+    // https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/export_native_api.md
+
+    static NativeSymbol native_symbols[] = { { "add_native", add_native, "(i)i",
+                                               NULL } };
+
+    init_args.mem_alloc_type = Alloc_With_Pool;
+    init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
+    init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
+
+    // Native symbols need below registration phase
+    init_args.n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol);
+    init_args.native_module_name = "env";
+    init_args.native_symbols = native_symbols;
+
+    if (!wasm_runtime_full_init(&init_args)) {
+        printf("Init runtime environment failed.\n");
+        return -1;
+    }
+
+    my_context_key = wasm_runtime_create_context_key(my_context_dtor);
+    if (!my_context_key) {
+        printf("wasm_runtime_create_context_key failed.\n");
+        return -1;
+    }
+
+    buffer = bh_read_file_to_buffer(wasm_path, &buf_size);
+
+    if (!buffer) {
+        printf("Open wasm app file [%s] failed.\n", wasm_path);
+        goto fail;
+    }
+
+    module = wasm_runtime_load((uint8 *)buffer, buf_size, error_buf,
+                               sizeof(error_buf));
+    if (!module) {
+        printf("Load wasm module failed. error: %s\n", error_buf);
+        goto fail;
+    }
+
+    module_inst = wasm_runtime_instantiate(module, stack_size, heap_size,
+                                           error_buf, sizeof(error_buf));
+
+    if (!module_inst) {
+        printf("Instantiate wasm module failed. error: %s\n", error_buf);
+        goto fail;
+    }
+
+    my_context.x = 100;
+    wasm_runtime_set_context(module_inst, my_context_key, &my_context);
+
+    exec_env = wasm_runtime_create_exec_env(module_inst, stack_size);
+    if (!exec_env) {
+        printf("Create wasm execution environment failed.\n");
+        goto fail;
+    }
+
+    wasm_function_inst_t func3 =
+        wasm_runtime_lookup_function(module_inst, "calculate", NULL);
+    if (!func3) {
+        printf("The wasm function calculate is not found.\n");
+        goto fail;
+    }
+
+    uint32_t argv3[1] = { 3 };
+    if (wasm_runtime_call_wasm(exec_env, func3, 1, argv3)) {
+        uint32_t result = *(uint32_t *)argv3;
+        printf("Native finished calling wasm function: calculate, return: %d\n",
+               result);
+        bh_assert(result == 103); /* argv3[0] + my_context.x */
+    }
+    else {
+        printf("call wasm function calculate failed. error: %s\n",
+               wasm_runtime_get_exception(module_inst));
+        goto fail;
+    }
+
+fail:
+    if (exec_env)
+        wasm_runtime_destroy_exec_env(exec_env);
+    if (module_inst) {
+        bh_assert(my_dtor_called == 0);
+        wasm_runtime_deinstantiate(module_inst);
+        bh_assert(my_dtor_called == 1);
+    }
+    if (module)
+        wasm_runtime_unload(module);
+    if (buffer)
+        BH_FREE(buffer);
+    if (my_context_key)
+        wasm_runtime_destroy_context_key(my_context_key);
+    wasm_runtime_destroy();
+    return 0;
+}

+ 10 - 0
samples/inst-context/src/my_context.h

@@ -0,0 +1,10 @@
+/*
+ * Copyright (C) 2023 Midokura Japan KK.  All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+struct my_context {
+    int x;
+};
+
+extern void *my_context_key;

+ 15 - 0
samples/inst-context/src/native_impl.c

@@ -0,0 +1,15 @@
+/*
+ * Copyright (C) 2023 Midokura Japan KK.  All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include "wasm_export.h"
+#include "my_context.h"
+
+int32_t
+add_native(wasm_exec_env_t exec_env, int32_t n)
+{
+    wasm_module_inst_t inst = wasm_runtime_get_module_inst(exec_env);
+    struct my_context *ctx = wasm_runtime_get_context(inst, my_context_key);
+    return n + ctx->x;
+}

+ 19 - 0
samples/inst-context/wasm-apps/testapp.c

@@ -0,0 +1,19 @@
+/*
+ * Copyright (C) 2019 Intel Corporation.  All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+
+int32_t
+add_native(int32_t n);
+
+int32_t
+calculate(int32_t n)
+{
+    printf("calling into WASM function: %s\n", __FUNCTION__);
+    return add_native(n);
+}

+ 1 - 0
wamr-compiler/CMakeLists.txt

@@ -45,6 +45,7 @@ add_definitions(-DWASM_ENABLE_DUMP_CALL_STACK=1)
 add_definitions(-DWASM_ENABLE_PERF_PROFILING=1)
 add_definitions(-DWASM_ENABLE_LOAD_CUSTOM_SECTION=1)
 add_definitions(-DWASM_ENABLE_LIB_WASI_THREADS=1)
+add_definitions(-DWASM_ENABLE_MODULE_INST_CONTEXT=1)
 
 if (WAMR_BUILD_LLVM_LEGACY_PM EQUAL 1)
   add_definitions(-DWASM_ENABLE_LLVM_LEGACY_PM=1)

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác