Skip to content

Commit

Permalink
deps: update to uvwasi 0.0.6
Browse files Browse the repository at this point in the history
PR-URL: #32309
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
cjihrig committed Mar 19, 2020
1 parent 6a34901 commit 543c046
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 47 deletions.
4 changes: 2 additions & 2 deletions deps/uvwasi/include/fd_table.h
Expand Up @@ -6,6 +6,7 @@
#include "wasi_types.h"

struct uvwasi_s;
struct uvwasi_options_s;

struct uvwasi_fd_wrap_t {
uvwasi_fd_t id;
Expand All @@ -27,8 +28,7 @@ struct uvwasi_fd_table_t {
};

uvwasi_errno_t uvwasi_fd_table_init(struct uvwasi_s* uvwasi,
struct uvwasi_fd_table_t* table,
uint32_t init_size);
struct uvwasi_options_s* options);
void uvwasi_fd_table_free(struct uvwasi_s* uvwasi,
struct uvwasi_fd_table_t* table);
uvwasi_errno_t uvwasi_fd_table_insert(struct uvwasi_s* uvwasi,
Expand Down
5 changes: 4 additions & 1 deletion deps/uvwasi/include/uvwasi.h
Expand Up @@ -11,7 +11,7 @@ extern "C" {

#define UVWASI_VERSION_MAJOR 0
#define UVWASI_VERSION_MINOR 0
#define UVWASI_VERSION_PATCH 5
#define UVWASI_VERSION_PATCH 6
#define UVWASI_VERSION_HEX ((UVWASI_VERSION_MAJOR << 16) | \
(UVWASI_VERSION_MINOR << 8) | \
(UVWASI_VERSION_PATCH))
Expand Down Expand Up @@ -60,6 +60,9 @@ typedef struct uvwasi_options_s {
size_t argc;
char** argv;
char** envp;
uvwasi_fd_t in;
uvwasi_fd_t out;
uvwasi_fd_t err;
const uvwasi_mem_t* allocator;
} uvwasi_options_t;

Expand Down
1 change: 1 addition & 0 deletions deps/uvwasi/src/clocks.c
Expand Up @@ -6,6 +6,7 @@
#endif /* _WIN32 */

#include "uv.h"
#include "clocks.h"
#include "wasi_types.h"
#include "uv_mapping.h"

Expand Down
105 changes: 62 additions & 43 deletions deps/uvwasi/src/fd_table.c
Expand Up @@ -14,6 +14,46 @@
#include "uvwasi_alloc.h"


static uvwasi_errno_t uvwasi__insert_stdio(uvwasi_t* uvwasi,
struct uvwasi_fd_table_t* table,
const uvwasi_fd_t fd,
const uvwasi_fd_t expected,
const char* name) {
struct uvwasi_fd_wrap_t* wrap;
uvwasi_filetype_t type;
uvwasi_rights_t base;
uvwasi_rights_t inheriting;
uvwasi_errno_t err;

err = uvwasi__get_filetype_by_fd(fd, &type);
if (err != UVWASI_ESUCCESS)
return err;

err = uvwasi__get_rights(fd, UV_FS_O_RDWR, type, &base, &inheriting);
if (err != UVWASI_ESUCCESS)
return err;

err = uvwasi_fd_table_insert(uvwasi,
table,
fd,
name,
name,
type,
base,
inheriting,
0,
&wrap);
if (err != UVWASI_ESUCCESS)
return err;

if (wrap->id != expected)
err = UVWASI_EBADF;

uv_mutex_unlock(&wrap->mutex);
return err;
}


uvwasi_errno_t uvwasi_fd_table_insert(uvwasi_t* uvwasi,
struct uvwasi_fd_table_t* table,
uv_file fd,
Expand All @@ -28,7 +68,7 @@ uvwasi_errno_t uvwasi_fd_table_insert(uvwasi_t* uvwasi,
struct uvwasi_fd_wrap_t** new_fds;
uvwasi_errno_t err;
uint32_t new_size;
int index;
uint32_t index;
uint32_t i;
int r;
size_t mp_len;
Expand Down Expand Up @@ -69,16 +109,17 @@ uvwasi_errno_t uvwasi_fd_table_insert(uvwasi_t* uvwasi,
table->size = new_size;
} else {
/* The table is big enough, so find an empty slot for the new data. */
index = -1;
int valid_slot = 0;
for (i = 0; i < table->size; ++i) {
if (table->fds[i] == NULL) {
valid_slot = 1;
index = i;
break;
}
}

/* index should never be -1. */
if (index == -1) {
/* This should never happen. */
if (valid_slot == 0) {
uvwasi__free(uvwasi, entry);
err = UVWASI_ENOSPC;
goto exit;
Expand Down Expand Up @@ -116,25 +157,21 @@ uvwasi_errno_t uvwasi_fd_table_insert(uvwasi_t* uvwasi,


uvwasi_errno_t uvwasi_fd_table_init(uvwasi_t* uvwasi,
struct uvwasi_fd_table_t* table,
uint32_t init_size) {
struct uvwasi_fd_wrap_t* wrap;
uvwasi_filetype_t type;
uvwasi_rights_t base;
uvwasi_rights_t inheriting;
uvwasi_options_t* options) {
struct uvwasi_fd_table_t* table;
uvwasi_errno_t err;
uvwasi_fd_t i;
int r;

/* Require an initial size of at least three to store the stdio FDs. */
if (table == NULL || init_size < 3)
if (uvwasi == NULL || options == NULL || options->fd_table_size < 3)
return UVWASI_EINVAL;

table = &uvwasi->fds;
table->fds = NULL;
table->used = 0;
table->size = init_size;
table->size = options->fd_table_size;
table->fds = uvwasi__calloc(uvwasi,
init_size,
options->fd_table_size,
sizeof(struct uvwasi_fd_wrap_t*));

if (table->fds == NULL)
Expand All @@ -153,35 +190,17 @@ uvwasi_errno_t uvwasi_fd_table_init(uvwasi_t* uvwasi,
}

/* Create the stdio FDs. */
for (i = 0; i < 3; ++i) {
err = uvwasi__get_filetype_by_fd(i, &type);
if (err != UVWASI_ESUCCESS)
goto error_exit;

err = uvwasi__get_rights(i, UV_FS_O_RDWR, type, &base, &inheriting);
if (err != UVWASI_ESUCCESS)
goto error_exit;

err = uvwasi_fd_table_insert(uvwasi,
table,
i,
"",
"",
type,
base,
inheriting,
0,
&wrap);
if (err != UVWASI_ESUCCESS)
goto error_exit;

r = wrap->id != i || wrap->id != (uvwasi_fd_t) wrap->fd;
uv_mutex_unlock(&wrap->mutex);
if (r) {
err = UVWASI_EBADF;
goto error_exit;
}
}
err = uvwasi__insert_stdio(uvwasi, table, options->in, 0, "<stdin>");
if (err != UVWASI_ESUCCESS)
goto error_exit;

err = uvwasi__insert_stdio(uvwasi, table, options->out, 1, "<stdout>");
if (err != UVWASI_ESUCCESS)
goto error_exit;

err = uvwasi__insert_stdio(uvwasi, table, options->err, 2, "<stderr>");
if (err != UVWASI_ESUCCESS)
goto error_exit;

return UVWASI_ESUCCESS;
error_exit:
Expand Down
7 changes: 6 additions & 1 deletion deps/uvwasi/src/uvwasi.c
Expand Up @@ -34,6 +34,11 @@
# define PATH_MAX_BYTES (PATH_MAX)
#endif

/* IBMi PASE does not support posix_fadvise() */
#ifdef __PASE__
# undef POSIX_FADV_NORMAL
#endif

static void* default_malloc(size_t size, void* mem_user_data) {
return malloc(size);
}
Expand Down Expand Up @@ -569,7 +574,7 @@ uvwasi_errno_t uvwasi_init(uvwasi_t* uvwasi, uvwasi_options_t* options) {
}
}

err = uvwasi_fd_table_init(uvwasi, &uvwasi->fds, options->fd_table_size);
err = uvwasi_fd_table_init(uvwasi, options);
if (err != UVWASI_ESUCCESS)
goto exit;

Expand Down
3 changes: 3 additions & 0 deletions src/node_wasi.cc
Expand Up @@ -170,6 +170,9 @@ void WASI::New(const FunctionCallbackInfo<Value>& args) {
const uint32_t argc = argv->Length();
uvwasi_options_t options;

options.in = 0;
options.out = 1;
options.err = 2;
options.fd_table_size = 3;
options.argc = argc;
options.argv = argc == 0 ? nullptr : new char*[argc];
Expand Down

0 comments on commit 543c046

Please sign in to comment.