add uvwasi implementation to support wasi on windows [experimental] (#534)

add uvwasi implementation to support wasi on windows [experimental] and update windows.yml

Co-authored-by: Wenyong Huang <wenyong.huang@intel.com>
This commit is contained in:
Xu Jun 2021-02-22 14:17:46 +08:00 committed by GitHub
parent 370cc83fbd
commit fc50404115
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 1394 additions and 16 deletions

View File

@ -21,39 +21,43 @@ jobs:
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: clone uvwasi library
run: |
cd core/deps
git clone https://github.com/nodejs/uvwasi.git
- name: Build iwasm [default] - name: Build iwasm [default]
run: | run: |
cd product-mini/platforms/windows cd product-mini/platforms/windows
mkdir build && cd build mkdir build && cd build
cmake .. cmake ..
cmake --build . --config Release cmake --build . --config Release
cd .. && rm -r build cd .. && rm -force -r build
- name: Build iwasm [aot only] - name: Build iwasm [aot only]
run: | run: |
cd product-mini/platforms/windows cd product-mini/platforms/windows
mkdir build && cd build mkdir build && cd build
cmake .. -DWAMR_BUILD_AOT=1 -DWAMR_BUILD_INTERP=0 cmake .. -DWAMR_BUILD_AOT=1 -DWAMR_BUILD_INTERP=0
cmake --build . --config Release cmake --build . --config Release
cd .. && rm -r build cd .. && rm -force -r build
- name: Build iwasm [interp only] - name: Build iwasm [interp only]
run: | run: |
cd product-mini/platforms/windows cd product-mini/platforms/windows
mkdir build && cd build mkdir build && cd build
cmake .. -DWAMR_BUILD_AOT=0 cmake .. -DWAMR_BUILD_AOT=0
cmake --build . --config Release cmake --build . --config Release
cd .. && rm -r build cd .. && rm -force -r build
- name: Build iwasm [tail call] - name: Build iwasm [tail call]
run: | run: |
cd product-mini/platforms/windows cd product-mini/platforms/windows
mkdir build && cd build mkdir build && cd build
cmake .. -DWAMR_BUILD_TAIL_CALL=1 cmake .. -DWAMR_BUILD_TAIL_CALL=1
cmake --build . --config Release cmake --build . --config Release
cd .. && rm -r build cd .. && rm -force -r build
- name: Build iwasm [custom name section] - name: Build iwasm [custom name section]
run: | run: |
cd product-mini/platforms/windows cd product-mini/platforms/windows
mkdir build && cd build mkdir build && cd build
cmake .. -DWAMR_BUILD_CUSTOM_NAME_SECTION=1 cmake .. -DWAMR_BUILD_CUSTOM_NAME_SECTION=1
cmake --build . --config Release cmake --build . --config Release
cd .. && rm -r build cd .. && rm -force -r build

View File

@ -119,7 +119,9 @@ if (WAMR_BUILD_LIBC_BUILTIN EQUAL 1)
else () else ()
message (" Libc builtin disabled") message (" Libc builtin disabled")
endif () endif ()
if (WAMR_BUILD_LIBC_WASI EQUAL 1) if (WAMR_BUILD_LIBC_UVWASI EQUAL 1)
message (" Libc WASI enabled with uvwasi implementation")
elseif (WAMR_BUILD_LIBC_WASI EQUAL 1)
message (" Libc WASI enabled") message (" Libc WASI enabled")
else () else ()
message (" Libc WASI disabled") message (" Libc WASI disabled")

View File

@ -75,7 +75,9 @@ if (WAMR_BUILD_LIBC_BUILTIN EQUAL 1)
include (${IWASM_DIR}/libraries/libc-builtin/libc_builtin.cmake) include (${IWASM_DIR}/libraries/libc-builtin/libc_builtin.cmake)
endif () endif ()
if (WAMR_BUILD_LIBC_WASI EQUAL 1) if (WAMR_BUILD_LIBC_UVWASI EQUAL 1)
include (${IWASM_DIR}/libraries/libc-uvwasi/libc_uvwasi.cmake)
elseif (WAMR_BUILD_LIBC_WASI EQUAL 1)
include (${IWASM_DIR}/libraries/libc-wasi/libc_wasi.cmake) include (${IWASM_DIR}/libraries/libc-wasi/libc_wasi.cmake)
endif () endif ()

View File

@ -90,6 +90,10 @@
#define WASM_ENABLE_LIBC_WASI 0 #define WASM_ENABLE_LIBC_WASI 0
#endif #endif
#ifndef WASM_ENABLE_UVWASI
#define WASM_ENABLE_UVWASI 0
#endif
/* Default disable libc emcc */ /* Default disable libc emcc */
#ifndef WASM_ENABLE_LIBC_EMCC #ifndef WASM_ENABLE_LIBC_EMCC
#define WASM_ENABLE_LIBC_EMCC 0 #define WASM_ENABLE_LIBC_EMCC 0

View File

@ -1656,6 +1656,7 @@ wasm_runtime_set_wasi_args(WASMModuleCommon *module,
} }
} }
#if WASM_ENABLE_UVWASI == 0
bool bool
wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
const char *dir_list[], uint32 dir_count, const char *dir_list[], uint32 dir_count,
@ -1848,6 +1849,128 @@ fail:
wasm_runtime_free(env_list); wasm_runtime_free(env_list);
return false; return false;
} }
#else /* else of WASM_ENABLE_UVWASI == 0 */
static void *
wasm_uvwasi_malloc(size_t size, void *mem_user_data)
{
return runtime_malloc(size, NULL, NULL, 0);
(void)mem_user_data;
}
static void
wasm_uvwasi_free(void *ptr, void *mem_user_data)
{
if (ptr)
wasm_runtime_free(ptr);
(void)mem_user_data;
}
static void *
wasm_uvwasi_calloc(size_t nmemb, size_t size,
void *mem_user_data)
{
uint64 total_size = (uint64)nmemb * size;
return runtime_malloc(total_size, NULL, NULL, 0);
(void)mem_user_data;
}
static void *
wasm_uvwasi_realloc(void *ptr, size_t size,
void *mem_user_data)
{
if (size >= UINT32_MAX) {
return NULL;
}
return wasm_runtime_realloc(ptr, (uint32)size);
}
static uvwasi_mem_t uvwasi_allocator = {
.mem_user_data = 0,
.malloc = wasm_uvwasi_malloc,
.free = wasm_uvwasi_free,
.calloc = wasm_uvwasi_calloc,
.realloc = wasm_uvwasi_realloc
};
bool
wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
const char *dir_list[], uint32 dir_count,
const char *map_dir_list[], uint32 map_dir_count,
const char *env[], uint32 env_count,
char *argv[], uint32 argc,
char *error_buf, uint32 error_buf_size)
{
uvwasi_t *uvwasi = NULL;
uvwasi_options_t init_options;
const char **envp = NULL;
uint64 total_size;
uint32 i;
bool ret = false;
uvwasi = runtime_malloc(sizeof(uvwasi_t), module_inst,
error_buf, error_buf_size);
if (!uvwasi)
return false;
/* Setup the initialization options */
uvwasi_options_init(&init_options);
init_options.allocator = &uvwasi_allocator;
init_options.argc = argc;
init_options.argv = (const char **)argv;
if (dir_count > 0) {
init_options.preopenc = dir_count;
total_size = sizeof(uvwasi_preopen_t) * (uint64)init_options.preopenc;
init_options.preopens =
(uvwasi_preopen_t *)runtime_malloc(total_size, module_inst,
error_buf, error_buf_size);
if (init_options.preopens == NULL)
goto fail;
for (i = 0; i < init_options.preopenc; i++) {
init_options.preopens[i].real_path = dir_list[i];
init_options.preopens[i].mapped_path =
(i < map_dir_count) ? map_dir_list[i] : dir_list[i];
}
}
if (env_count > 0) {
total_size = sizeof(char *) * (uint64)(env_count + 1);
envp = runtime_malloc(total_size, module_inst,
error_buf, error_buf_size);
if (envp == NULL)
goto fail;
for (i = 0; i < env_count; i++) {
envp[i] = env[i];
}
envp[env_count] = NULL;
init_options.envp = envp;
}
if (UVWASI_ESUCCESS != uvwasi_init(uvwasi, &init_options)) {
set_error_buf(error_buf, error_buf_size, "uvwasi init failed");
goto fail;
}
wasm_runtime_set_wasi_ctx(module_inst, uvwasi);
ret = true;
fail:
if (envp)
wasm_runtime_free(envp);
if (init_options.preopens)
wasm_runtime_free(init_options.preopens);
if (!ret && uvwasi)
wasm_runtime_free(uvwasi);
return ret;
}
#endif /* end of WASM_ENABLE_UVWASI */
bool bool
wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst) wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst)
@ -1915,6 +2038,7 @@ wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst)
return NULL; return NULL;
} }
#if WASM_ENABLE_UVWASI == 0
void void
wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst) wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
{ {
@ -1944,6 +2068,18 @@ wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
wasm_runtime_free(wasi_ctx); wasm_runtime_free(wasi_ctx);
} }
} }
#else
void
wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
{
WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
if (wasi_ctx) {
uvwasi_destroy(wasi_ctx);
wasm_runtime_free(wasi_ctx);
}
}
#endif
WASIContext * WASIContext *
wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst) wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst)

View File

@ -13,8 +13,12 @@
#include "../include/wasm_export.h" #include "../include/wasm_export.h"
#include "../interpreter/wasm.h" #include "../interpreter/wasm.h"
#if WASM_ENABLE_LIBC_WASI != 0 #if WASM_ENABLE_LIBC_WASI != 0
#if WASM_ENABLE_UVWASI == 0
#include "wasmtime_ssp.h" #include "wasmtime_ssp.h"
#include "posix.h" #include "posix.h"
#else
#include "uvwasi.h"
#endif
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus
@ -73,6 +77,7 @@ typedef struct WASMModuleInstMemConsumption {
} WASMModuleInstMemConsumption; } WASMModuleInstMemConsumption;
#if WASM_ENABLE_LIBC_WASI != 0 #if WASM_ENABLE_LIBC_WASI != 0
#if WASM_ENABLE_UVWASI == 0
typedef struct WASIContext { typedef struct WASIContext {
struct fd_table *curfds; struct fd_table *curfds;
struct fd_prestats *prestats; struct fd_prestats *prestats;
@ -82,6 +87,9 @@ typedef struct WASIContext {
char *env_buf; char *env_buf;
char **env_list; char **env_list;
} WASIContext; } WASIContext;
#else
typedef uvwasi_t WASIContext;
#endif
#endif #endif
#if WASM_ENABLE_MULTI_MODULE != 0 #if WASM_ENABLE_MULTI_MODULE != 0

View File

@ -0,0 +1,30 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
set (LIBC_WASI_DIR ${CMAKE_CURRENT_LIST_DIR})
set (UVWASI_DIR ${CMAKE_CURRENT_LIST_DIR}/../../../deps/uvwasi)
set (LIBUV_VERSION v1.39.0)
add_definitions (-DWASM_ENABLE_LIBC_WASI=1 -DWASM_ENABLE_UVWASI=1)
include(FetchContent)
## https://libuv.org
FetchContent_Declare(
libuv
GIT_REPOSITORY https://github.com/libuv/libuv.git
GIT_TAG ${LIBUV_VERSION})
FetchContent_GetProperties(libuv)
if(NOT libuv_POPULATED)
message ("-- Fetching libuv ..")
FetchContent_Populate(libuv)
include_directories("${libuv_SOURCE_DIR}/include")
add_subdirectory(${libuv_SOURCE_DIR} ${libuv_BINARY_DIR} EXCLUDE_FROM_ALL)
set (UV_A_LIBS uv_a)
endif()
include_directories(${UVWASI_DIR}/include)
file (GLOB_RECURSE source_all ${LIBC_WASI_DIR}/*.c ${UVWASI_DIR}/src/*.c)
set (LIBC_WASI_SOURCE ${source_all})

File diff suppressed because it is too large Load Diff

View File

@ -472,6 +472,11 @@ gc_alloc_vo_internal(void *vheap, gc_size_t size,
if (!hmu) if (!hmu)
goto finish; goto finish;
bh_assert(hmu_get_size(hmu) >= tot_size);
/* the total size allocated may be larger than
the required size, reset it here */
tot_size = hmu_get_size(hmu);
g_total_malloc += tot_size; g_total_malloc += tot_size;
hmu_set_ut(hmu, HMU_VO); hmu_set_ut(hmu, HMU_VO);
@ -570,6 +575,9 @@ gc_realloc_vo_internal(void *vheap, void *ptr, gc_size_t size,
if (!hmu) if (!hmu)
goto finish; goto finish;
bh_assert(hmu_get_size(hmu) >= tot_size);
/* the total size allocated may be larger than
the required size, reset it here */
g_total_malloc += tot_size; g_total_malloc += tot_size;
hmu_set_ut(hmu, HMU_VO); hmu_set_ut(hmu, HMU_VO);

View File

@ -29,7 +29,13 @@ typedef struct hmu_struct {
#if BH_ENABLE_GC_VERIFY != 0 #if BH_ENABLE_GC_VERIFY != 0
#if UINTPTR_MAX > UINT32_MAX
/* 2 prefix paddings for 64-bit pointer */
#define GC_OBJECT_PREFIX_PADDING_CNT 2
#else
/* 3 prefix paddings for 32-bit pointer */
#define GC_OBJECT_PREFIX_PADDING_CNT 3 #define GC_OBJECT_PREFIX_PADDING_CNT 3
#endif
#define GC_OBJECT_SUFFIX_PADDING_CNT 4 #define GC_OBJECT_SUFFIX_PADDING_CNT 4
#define GC_OBJECT_PADDING_VALUE (0x12345678) #define GC_OBJECT_PADDING_VALUE (0x12345678)

View File

@ -43,13 +43,22 @@ cmake -DWAMR_BUILD_PLATFORM=linux -DWAMR_BUILD_TARGET=ARM
#### **Configure LIBC** #### **Configure LIBC**
- **WAMR_BUILD_LIBC_BUILTIN**=1/0, default to enable if not set - **WAMR_BUILD_LIBC_BUILTIN**=1/0, build the built-in libc subset for WASM app, default to enable if not set
- **WAMR_BUILD_LIBC_WASI**=1/0, default to enable if not set - **WAMR_BUILD_LIBC_WASI**=1/0, build the [WASI](https://github.com/WebAssembly/WASI) libc subset for WASM app, default to enable if not set
- **WAMR_BUILD_LIBC_UVWASI**=1/0 (Experiment), build the [WASI](https://github.com/WebAssembly/WASI) libc subset for WASM app based on [uvwasi](https://github.com/nodejs/uvwasi) implementation, default to disable if not set
> Note: for platform which doesn't support **WAMR_BUILD_LIBC_WASI**, e.g. Windows, developer can try using **WAMR_BUILD_LIBC_UVWASI**. And the uvwasi source code must be cloned under core/deps:
>
> ```bash
> cd <WAMR-ROOT>/core/deps
> git clone https://github.com/nodejs/uvwasi.git
> ```
#### **Configure Debug** #### **Configure Debug**
- **WAMR_BUILD_CUSTOM_NAME_SECTION**=1/0, load the function name from custom name section, default to disable if not set - **WAMR_BUILD_CUSTOM_NAME_SECTION**=1/0, load the function name from custom name section, default to disable if not set
#### **Enable dump call stack feature** #### **Enable dump call stack feature**
- **WAMR_BUILD_DUMP_CALL_STACK**=1/0, default to disable if not set - **WAMR_BUILD_DUMP_CALL_STACK**=1/0, default to disable if not set

View File

@ -119,7 +119,7 @@ add_executable (iwasm main.c ${UNCOMMON_SHARED_SOURCE})
install (TARGETS iwasm DESTINATION bin) install (TARGETS iwasm DESTINATION bin)
target_link_libraries (iwasm vmlib ${LLVM_AVAILABLE_LIBS} -lm -ldl -lpthread) target_link_libraries (iwasm vmlib ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -lpthread)
add_library (libiwasm SHARED ${WAMR_RUNTIME_LIB_SOURCE}) add_library (libiwasm SHARED ${WAMR_RUNTIME_LIB_SOURCE})

View File

@ -51,9 +51,9 @@ if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN)
set (WAMR_BUILD_LIBC_BUILTIN 1) set (WAMR_BUILD_LIBC_BUILTIN 1)
endif () endif ()
if (NOT DEFINED WAMR_BUILD_LIBC_WASI) if (NOT DEFINED WAMR_BUILD_LIBC_UVWASI)
# Enable libc wasi support by default # Enable libc uvwasi support by default
set (WAMR_BUILD_LIBC_WASI 0) set (WAMR_BUILD_LIBC_UVWASI 1)
endif () endif ()
if (NOT DEFINED WAMR_BUILD_FAST_INTERP) if (NOT DEFINED WAMR_BUILD_FAST_INTERP)
@ -86,6 +86,7 @@ include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE}) add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO") set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO")
set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO")
# set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security") # set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
# set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion -Wsign-conversion") # set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion -Wsign-conversion")
@ -110,7 +111,7 @@ add_executable (iwasm main.c ${UNCOMMON_SHARED_SOURCE})
install (TARGETS iwasm DESTINATION bin) install (TARGETS iwasm DESTINATION bin)
target_link_libraries (iwasm vmlib ${LLVM_AVAILABLE_LIBS}) target_link_libraries (iwasm vmlib ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS})
add_library (libiwasm SHARED ${WAMR_RUNTIME_LIB_SOURCE}) add_library (libiwasm SHARED ${WAMR_RUNTIME_LIB_SOURCE})
@ -118,6 +119,6 @@ install (TARGETS libiwasm DESTINATION lib)
set_target_properties (libiwasm PROPERTIES OUTPUT_NAME iwasm) set_target_properties (libiwasm PROPERTIES OUTPUT_NAME iwasm)
target_link_libraries (libiwasm ${LLVM_AVAILABLE_LIBS}) target_link_libraries (libiwasm ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS})
target_compile_definitions(libiwasm PRIVATE COMPILING_WASM_RUNTIME_API=1) target_compile_definitions(libiwasm PRIVATE COMPILING_WASM_RUNTIME_API=1)