Skip to content

Commit

Permalink
deps: update uvwasi
Browse files Browse the repository at this point in the history
PR-URL: #30745
Refs: https://github.com/nodejs/quic/blob/34ee0bc96f804c73cb22b2945a1a78f780938492/src/node_mem.h
Refs: nodejs/quic#126
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
addaleax authored and targos committed Dec 9, 2019
1 parent d17ea8f commit 13fe9f7
Show file tree
Hide file tree
Showing 5 changed files with 497 additions and 178 deletions.
15 changes: 11 additions & 4 deletions deps/uvwasi/include/fd_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# define PATH_MAX_BYTES (PATH_MAX)
#endif

struct uvwasi_s;

struct uvwasi_fd_wrap_t {
uvwasi_fd_t id;
Expand All @@ -27,22 +28,28 @@ struct uvwasi_fd_wrap_t {
uvwasi_rights_t rights_inheriting;
int preopen;
int valid;
uv_mutex_t mutex;
};

struct uvwasi_fd_table_t {
struct uvwasi_fd_wrap_t* fds;
uint32_t size;
uint32_t used;
uv_rwlock_t rwlock;
};

uvwasi_errno_t uvwasi_fd_table_init(struct uvwasi_fd_table_t* table,
uvwasi_errno_t uvwasi_fd_table_init(struct uvwasi_s* uvwasi,
struct uvwasi_fd_table_t* table,
uint32_t init_size);
void uvwasi_fd_table_free(struct uvwasi_fd_table_t* table);
uvwasi_errno_t uvwasi_fd_table_insert_preopen(struct uvwasi_fd_table_t* table,
void uvwasi_fd_table_free(struct uvwasi_s* uvwasi,
struct uvwasi_fd_table_t* table);
uvwasi_errno_t uvwasi_fd_table_insert_preopen(struct uvwasi_s* uvwasi,
struct uvwasi_fd_table_t* table,
const uv_file fd,
const char* path,
const char* real_path);
uvwasi_errno_t uvwasi_fd_table_insert_fd(struct uvwasi_fd_table_t* table,
uvwasi_errno_t uvwasi_fd_table_insert_fd(struct uvwasi_s* uvwasi,
struct uvwasi_fd_table_t* table,
const uv_file fd,
const int flags,
const char* path,
Expand Down
16 changes: 15 additions & 1 deletion deps/uvwasi/include/uvwasi.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,20 @@ extern "C" {
#define UVWASI_VERSION_STRING UVWASI_STRINGIFY(UVWASI_VERSION_MAJOR) "." \
UVWASI_STRINGIFY(UVWASI_VERSION_MINOR) "." \
UVWASI_STRINGIFY(UVWASI_VERSION_PATCH)
#define UVWASI_VERSION_WASI "snapshot_0"

typedef void* (*uvwasi_malloc)(size_t size, void* mem_user_data);
typedef void (*uvwasi_free)(void* ptr, void* mem_user_data);
typedef void* (*uvwasi_calloc)(size_t nmemb, size_t size, void* mem_user_data);
typedef void* (*uvwasi_realloc)(void* ptr, size_t size, void* mem_user_data);

typedef struct uvwasi_mem_s {
void* mem_user_data;
uvwasi_malloc malloc;
uvwasi_free free;
uvwasi_calloc calloc;
uvwasi_realloc realloc;
} uvwasi_mem_t;

typedef struct uvwasi_s {
struct uvwasi_fd_table_t fds;
Expand All @@ -32,6 +45,7 @@ typedef struct uvwasi_s {
char** env;
char* env_buf;
size_t env_buf_size;
const uvwasi_mem_t* allocator;
} uvwasi_t;

typedef struct uvwasi_preopen_s {
Expand All @@ -46,9 +60,9 @@ typedef struct uvwasi_options_s {
size_t argc;
char** argv;
char** envp;
const uvwasi_mem_t* allocator;
} uvwasi_options_t;


// Embedder API.
uvwasi_errno_t uvwasi_init(uvwasi_t* uvwasi, uvwasi_options_t* options);
void uvwasi_destroy(uvwasi_t* uvwasi);
Expand Down
125 changes: 94 additions & 31 deletions deps/uvwasi/src/fd_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "fd_table.h"
#include "wasi_types.h"
#include "uv_mapping.h"
#include "uvwasi_alloc.h"


#define UVWASI__RIGHTS_ALL (UVWASI_RIGHT_FD_DATASYNC | \
Expand Down Expand Up @@ -175,7 +176,8 @@ static uvwasi_errno_t uvwasi__get_type_and_rights(uv_file fd,
}


static uvwasi_errno_t uvwasi__fd_table_insert(struct uvwasi_fd_table_t* table,
static uvwasi_errno_t uvwasi__fd_table_insert(uvwasi_t* uvwasi,
struct uvwasi_fd_table_t* table,
uv_file fd,
const char* mapped_path,
const char* real_path,
Expand All @@ -186,16 +188,22 @@ static uvwasi_errno_t uvwasi__fd_table_insert(struct uvwasi_fd_table_t* table,
struct uvwasi_fd_wrap_t** wrap) {
struct uvwasi_fd_wrap_t* entry;
struct uvwasi_fd_wrap_t* new_fds;
uvwasi_errno_t err;
uint32_t new_size;
int index;
uint32_t i;
int r;

uv_rwlock_wrlock(&table->rwlock);

/* Check that there is room for a new item. If there isn't, grow the table. */
if (table->used >= table->size) {
new_size = table->size * 2;
new_fds = realloc(table->fds, new_size * sizeof(*new_fds));
if (new_fds == NULL)
return UVWASI_ENOMEM;
new_fds = uvwasi__realloc(uvwasi, table->fds, new_size * sizeof(*new_fds));
if (new_fds == NULL) {
err = UVWASI_ENOMEM;
goto exit;
}

for (i = table->size; i < new_size; ++i)
new_fds[i].valid = 0;
Expand All @@ -214,11 +222,20 @@ static uvwasi_errno_t uvwasi__fd_table_insert(struct uvwasi_fd_table_t* table,
}

/* index should never be -1. */
if (index == -1)
return UVWASI_ENOSPC;
if (index == -1) {
err = UVWASI_ENOSPC;
goto exit;
}
}

entry = &table->fds[index];

r = uv_mutex_init(&entry->mutex);
if (r != 0) {
err = uvwasi__translate_uv_error(r);
goto exit;
}

entry->id = index;
entry->fd = fd;
strcpy(entry->path, mapped_path);
Expand All @@ -233,29 +250,43 @@ static uvwasi_errno_t uvwasi__fd_table_insert(struct uvwasi_fd_table_t* table,
if (wrap != NULL)
*wrap = entry;

return UVWASI_ESUCCESS;
err = UVWASI_ESUCCESS;
exit:
uv_rwlock_wrunlock(&table->rwlock);
return err;
}


uvwasi_errno_t uvwasi_fd_table_init(struct uvwasi_fd_table_t* table,
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_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)
return UVWASI_EINVAL;

table->fds = NULL;
r = uv_rwlock_init(&table->rwlock);
if (r != 0)
return uvwasi__translate_uv_error(r);

table->used = 0;
table->size = init_size;
table->fds = calloc(init_size, sizeof(struct uvwasi_fd_wrap_t));
table->fds = uvwasi__calloc(uvwasi,
init_size,
sizeof(struct uvwasi_fd_wrap_t));

if (table->fds == NULL)
return UVWASI_ENOMEM;
if (table->fds == NULL) {
err = UVWASI_ENOMEM;
goto error_exit;
}

/* Create the stdio FDs. */
for (i = 0; i < 3; ++i) {
Expand All @@ -267,7 +298,8 @@ uvwasi_errno_t uvwasi_fd_table_init(struct uvwasi_fd_table_t* table,
if (err != UVWASI_ESUCCESS)
goto error_exit;

err = uvwasi__fd_table_insert(table,
err = uvwasi__fd_table_insert(uvwasi,
table,
i,
"",
"",
Expand All @@ -287,23 +319,25 @@ uvwasi_errno_t uvwasi_fd_table_init(struct uvwasi_fd_table_t* table,

return UVWASI_ESUCCESS;
error_exit:
uvwasi_fd_table_free(table);
uvwasi_fd_table_free(uvwasi, table);
return err;
}


void uvwasi_fd_table_free(struct uvwasi_fd_table_t* table) {
void uvwasi_fd_table_free(uvwasi_t* uvwasi, struct uvwasi_fd_table_t* table) {
if (table == NULL)
return;

free(table->fds);
uvwasi__free(uvwasi, table->fds);
table->fds = NULL;
table->size = 0;
table->used = 0;
uv_rwlock_destroy(&table->rwlock);
}


uvwasi_errno_t uvwasi_fd_table_insert_preopen(struct uvwasi_fd_table_t* table,
uvwasi_errno_t uvwasi_fd_table_insert_preopen(uvwasi_t* uvwasi,
struct uvwasi_fd_table_t* table,
const uv_file fd,
const char* path,
const char* real_path) {
Expand All @@ -322,7 +356,8 @@ uvwasi_errno_t uvwasi_fd_table_insert_preopen(struct uvwasi_fd_table_t* table,
if (type != UVWASI_FILETYPE_DIRECTORY)
return UVWASI_ENOTDIR;

err = uvwasi__fd_table_insert(table,
err = uvwasi__fd_table_insert(uvwasi,
table,
fd,
path,
real_path,
Expand All @@ -338,7 +373,8 @@ uvwasi_errno_t uvwasi_fd_table_insert_preopen(struct uvwasi_fd_table_t* table,
}


uvwasi_errno_t uvwasi_fd_table_insert_fd(struct uvwasi_fd_table_t* table,
uvwasi_errno_t uvwasi_fd_table_insert_fd(uvwasi_t* uvwasi,
struct uvwasi_fd_table_t* table,
const uv_file fd,
const int flags,
const char* path,
Expand All @@ -358,7 +394,8 @@ uvwasi_errno_t uvwasi_fd_table_insert_fd(struct uvwasi_fd_table_t* table,
if (r != UVWASI_ESUCCESS)
return r;

r = uvwasi__fd_table_insert(table,
r = uvwasi__fd_table_insert(uvwasi,
table,
fd,
path,
path,
Expand All @@ -381,42 +418,68 @@ uvwasi_errno_t uvwasi_fd_table_get(const struct uvwasi_fd_table_t* table,
uvwasi_rights_t rights_base,
uvwasi_rights_t rights_inheriting) {
struct uvwasi_fd_wrap_t* entry;
uvwasi_errno_t err;

if (table == NULL || wrap == NULL)
return UVWASI_EINVAL;
if (id >= table->size)
return UVWASI_EBADF;

uv_rwlock_rdlock((uv_rwlock_t *)&table->rwlock);

if (id >= table->size) {
err = UVWASI_EBADF;
goto exit;
}

entry = &table->fds[id];

if (entry->valid != 1 || entry->id != id)
return UVWASI_EBADF;
if (entry->valid != 1 || entry->id != id) {
err = UVWASI_EBADF;
goto exit;
}

/* Validate that the fd has the necessary rights. */
if ((~entry->rights_base & rights_base) != 0 ||
(~entry->rights_inheriting & rights_inheriting) != 0)
return UVWASI_ENOTCAPABLE;
(~entry->rights_inheriting & rights_inheriting) != 0) {
err = UVWASI_ENOTCAPABLE;
goto exit;
}

uv_mutex_lock(&entry->mutex);
*wrap = entry;
return UVWASI_ESUCCESS;
err = UVWASI_ESUCCESS;
exit:
uv_rwlock_rdunlock((uv_rwlock_t *)&table->rwlock);
return err;
}


uvwasi_errno_t uvwasi_fd_table_remove(struct uvwasi_fd_table_t* table,
const uvwasi_fd_t id) {
struct uvwasi_fd_wrap_t* entry;
uvwasi_errno_t err;

if (table == NULL)
return UVWASI_EINVAL;
if (id >= table->size)
return UVWASI_EBADF;

uv_rwlock_wrlock(&table->rwlock);

if (id >= table->size) {
err = UVWASI_EBADF;
goto exit;
}

entry = &table->fds[id];

if (entry->valid != 1 || entry->id != id)
return UVWASI_EBADF;
if (entry->valid != 1 || entry->id != id) {
err = UVWASI_EBADF;
goto exit;
}

uv_mutex_destroy(&entry->mutex);
entry->valid = 0;
table->used--;
return UVWASI_ESUCCESS;
err = UVWASI_ESUCCESS;
exit:
uv_rwlock_wrunlock(&table->rwlock);
return err;
}
Loading

0 comments on commit 13fe9f7

Please sign in to comment.