From e115a626cd77df2ca14a469edea24652b8f70af2 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Fri, 13 Mar 2020 17:28:32 -0400 Subject: [PATCH] support passing stdio descriptors This commit requires the stdio file descriptors to be explicitly defined. This allows embedders to run multiple WASI applications in the same process (assuming they also monkey patch the uvwasi_proc_exit() functionality to not force the process to exit). --- README.md | 8 ++++++++ include/uvwasi.h | 3 +++ src/fd_table.c | 8 ++++---- test/test-args-get.c | 3 +++ test/test-basic-file-io.c | 3 +++ test/test-ebadf-input-validation.c | 3 +++ test/test-environ-get.c | 3 +++ test/test-fd-prestat-dir-name.c | 3 +++ test/test-multiple-wasi-destroys.c | 3 +++ test/test-path-create-remove-directory.c | 3 +++ test/test-random-get.c | 3 +++ 11 files changed, 39 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b6d1546..7e049c8 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,9 @@ int main(void) { uvwasi_errno_t err; /* Setup the initialization options. */ + init_options.stdin = 0; + init_options.stdout = 1; + init_options.stderr = 2; init_options.fd_table_size = 3; init_options.argc = 3; init_options.argv = calloc(3, sizeof(char*)); @@ -38,6 +41,7 @@ int main(void) { init_options.preopens = calloc(1, sizeof(uvwasi_preopen_t)); init_options.preopens[0].mapped_path = "/var"; init_options.preopens[0].real_path = "."; + init_options.allocator = NULL; /* Initialize the sandbox. */ err = uvwasi_init(&uvwasi, &init_options); @@ -146,6 +150,10 @@ typedef struct uvwasi_options_s { size_t argc; char** argv; char** envp; + uvwasi_fd_t stdin; + uvwasi_fd_t stdout; + uvwasi_fd_t stderr; + const uvwasi_mem_t* allocator; } uvwasi_options_t; ``` diff --git a/include/uvwasi.h b/include/uvwasi.h index 9ca3045..44de573 100644 --- a/include/uvwasi.h +++ b/include/uvwasi.h @@ -60,6 +60,9 @@ typedef struct uvwasi_options_s { size_t argc; char** argv; char** envp; + uvwasi_fd_t stdin; + uvwasi_fd_t stdout; + uvwasi_fd_t stderr; const uvwasi_mem_t* allocator; } uvwasi_options_t; diff --git a/src/fd_table.c b/src/fd_table.c index 2d8c9f4..1b636e4 100644 --- a/src/fd_table.c +++ b/src/fd_table.c @@ -16,7 +16,7 @@ static uvwasi_errno_t uvwasi__insert_stdio(uvwasi_t* uvwasi, struct uvwasi_fd_table_t* table, - const uv_file fd, + const uvwasi_fd_t fd, const uvwasi_fd_t expected, const char* name) { struct uvwasi_fd_wrap_t* wrap; @@ -190,15 +190,15 @@ uvwasi_errno_t uvwasi_fd_table_init(uvwasi_t* uvwasi, } /* Create the stdio FDs. */ - err = uvwasi__insert_stdio(uvwasi, table, 0, 0, ""); + err = uvwasi__insert_stdio(uvwasi, table, options->stdin, 0, ""); if (err != UVWASI_ESUCCESS) goto error_exit; - err = uvwasi__insert_stdio(uvwasi, table, 1, 1, ""); + err = uvwasi__insert_stdio(uvwasi, table, options->stdout, 1, ""); if (err != UVWASI_ESUCCESS) goto error_exit; - err = uvwasi__insert_stdio(uvwasi, table, 2, 2, ""); + err = uvwasi__insert_stdio(uvwasi, table, options->stderr, 2, ""); if (err != UVWASI_ESUCCESS) goto error_exit; diff --git a/test/test-args-get.c b/test/test-args-get.c index 79ec30b..715ac95 100644 --- a/test/test-args-get.c +++ b/test/test-args-get.c @@ -12,6 +12,9 @@ int main(void) { char** args_get_argv; char* buf; + init_options.stdin = 0; + init_options.stdout = 1; + init_options.stderr = 2; init_options.fd_table_size = 3; init_options.argc = 3; init_options.argv = calloc(3, sizeof(char*)); diff --git a/test/test-basic-file-io.c b/test/test-basic-file-io.c index 45c3b17..67be3f4 100644 --- a/test/test-basic-file-io.c +++ b/test/test-basic-file-io.c @@ -29,6 +29,9 @@ int main(void) { uv_fs_req_cleanup(&req); assert(r == 0 || r == UV_EEXIST); + init_options.stdin = 0; + init_options.stdout = 1; + init_options.stderr = 2; init_options.fd_table_size = 3; init_options.argc = 0; init_options.argv = NULL; diff --git a/test/test-ebadf-input-validation.c b/test/test-ebadf-input-validation.c index 46f7d0a..2c7f316 100644 --- a/test/test-ebadf-input-validation.c +++ b/test/test-ebadf-input-validation.c @@ -22,6 +22,9 @@ int main(void) { test_void = (void*) &test_fdstat; + init_options.stdin = 0; + init_options.stdout = 1; + init_options.stderr = 2; init_options.fd_table_size = 3; init_options.argc = 0; init_options.argv = NULL; diff --git a/test/test-environ-get.c b/test/test-environ-get.c index b235226..3af684e 100644 --- a/test/test-environ-get.c +++ b/test/test-environ-get.c @@ -20,6 +20,9 @@ int main(void) { char* buf; size_t i; + init_options.stdin = 0; + init_options.stdout = 1; + init_options.stderr = 2; init_options.fd_table_size = 3; init_options.argc = 0; init_options.argv = NULL; diff --git a/test/test-fd-prestat-dir-name.c b/test/test-fd-prestat-dir-name.c index 783484d..3e99a97 100644 --- a/test/test-fd-prestat-dir-name.c +++ b/test/test-fd-prestat-dir-name.c @@ -19,6 +19,9 @@ int main(void) { uv_fs_req_cleanup(&req); assert(r == 0 || r == UV_EEXIST); + init_options.stdin = 0; + init_options.stdout = 1; + init_options.stderr = 2; init_options.fd_table_size = 3; init_options.argc = 0; init_options.argv = NULL; diff --git a/test/test-multiple-wasi-destroys.c b/test/test-multiple-wasi-destroys.c index b762f90..6d37a48 100644 --- a/test/test-multiple-wasi-destroys.c +++ b/test/test-multiple-wasi-destroys.c @@ -8,6 +8,9 @@ int main(void) { uvwasi_options_t init_options; uvwasi_errno_t err; + init_options.stdin = 0; + init_options.stdout = 1; + init_options.stderr = 2; init_options.fd_table_size = 3; init_options.argc = 0; init_options.argv = NULL; diff --git a/test/test-path-create-remove-directory.c b/test/test-path-create-remove-directory.c index 83a731d..eca154d 100644 --- a/test/test-path-create-remove-directory.c +++ b/test/test-path-create-remove-directory.c @@ -21,6 +21,9 @@ int main(void) { uv_fs_req_cleanup(&req); assert(r == 0 || r == UV_ENOENT); + init_options.stdin = 0; + init_options.stdout = 1; + init_options.stderr = 2; init_options.fd_table_size = 3; init_options.argc = 0; init_options.argv = NULL; diff --git a/test/test-random-get.c b/test/test-random-get.c index 13cc6e8..7ee7347 100644 --- a/test/test-random-get.c +++ b/test/test-random-get.c @@ -13,6 +13,9 @@ int main(void) { int success; int i; + init_options.stdin = 0; + init_options.stdout = 1; + init_options.stderr = 2; init_options.fd_table_size = 3; init_options.argc = 0; init_options.argv = NULL;