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
13 changed files with 1394 additions and 16 deletions

View File

@ -90,6 +90,10 @@
#define WASM_ENABLE_LIBC_WASI 0
#endif
#ifndef WASM_ENABLE_UVWASI
#define WASM_ENABLE_UVWASI 0
#endif
/* Default disable libc emcc */
#ifndef WASM_ENABLE_LIBC_EMCC
#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
wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
const char *dir_list[], uint32 dir_count,
@ -1848,6 +1849,128 @@ fail:
wasm_runtime_free(env_list);
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
wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst)
@ -1915,6 +2038,7 @@ wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst)
return NULL;
}
#if WASM_ENABLE_UVWASI == 0
void
wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
{
@ -1944,6 +2068,18 @@ wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
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 *
wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst)

View File

@ -13,8 +13,12 @@
#include "../include/wasm_export.h"
#include "../interpreter/wasm.h"
#if WASM_ENABLE_LIBC_WASI != 0
#if WASM_ENABLE_UVWASI == 0
#include "wasmtime_ssp.h"
#include "posix.h"
#else
#include "uvwasi.h"
#endif
#endif
#ifdef __cplusplus
@ -73,6 +77,7 @@ typedef struct WASMModuleInstMemConsumption {
} WASMModuleInstMemConsumption;
#if WASM_ENABLE_LIBC_WASI != 0
#if WASM_ENABLE_UVWASI == 0
typedef struct WASIContext {
struct fd_table *curfds;
struct fd_prestats *prestats;
@ -82,6 +87,9 @@ typedef struct WASIContext {
char *env_buf;
char **env_list;
} WASIContext;
#else
typedef uvwasi_t WASIContext;
#endif
#endif
#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)
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;
hmu_set_ut(hmu, HMU_VO);
@ -570,6 +575,9 @@ gc_realloc_vo_internal(void *vheap, void *ptr, gc_size_t size,
if (!hmu)
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;
hmu_set_ut(hmu, HMU_VO);

View File

@ -29,7 +29,13 @@ typedef struct hmu_struct {
#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
#endif
#define GC_OBJECT_SUFFIX_PADDING_CNT 4
#define GC_OBJECT_PADDING_VALUE (0x12345678)