Skip to content

Commit

Permalink
deps: update uvwasi to 0.0.19
Browse files Browse the repository at this point in the history
PR-URL: nodejs#49908
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
nodejs-github-bot authored and GeoffreyBooth committed Oct 1, 2023
1 parent 94dbc25 commit f4b63ab
Show file tree
Hide file tree
Showing 8 changed files with 491 additions and 47 deletions.
11 changes: 10 additions & 1 deletion deps/uvwasi/include/uvwasi.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
extern "C" {
#endif

#include "uv.h"
#include "wasi_serdes.h"
#include "wasi_types.h"

#define UVWASI_VERSION_MAJOR 0
#define UVWASI_VERSION_MINOR 0
#define UVWASI_VERSION_PATCH 18
#define UVWASI_VERSION_PATCH 19
#define UVWASI_VERSION_HEX ((UVWASI_VERSION_MAJOR << 16) | \
(UVWASI_VERSION_MINOR << 8) | \
(UVWASI_VERSION_PATCH))
Expand Down Expand Up @@ -47,17 +48,25 @@ typedef struct uvwasi_s {
char* env_buf;
uvwasi_size_t env_buf_size;
const uvwasi_mem_t* allocator;
uv_loop_t* loop;
} uvwasi_t;

typedef struct uvwasi_preopen_s {
const char* mapped_path;
const char* real_path;
} uvwasi_preopen_t;

typedef struct uvwasi_preopen_socket_s {
const char* address;
int port;
} uvwasi_preopen_socket_t;

typedef struct uvwasi_options_s {
uvwasi_size_t fd_table_size;
uvwasi_size_t preopenc;
uvwasi_preopen_t* preopens;
uvwasi_size_t preopen_socketc;
uvwasi_preopen_socket_t* preopen_sockets;
uvwasi_size_t argc;
const char** argv;
const char** envp;
Expand Down
69 changes: 52 additions & 17 deletions deps/uvwasi/src/fd_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ static uvwasi_errno_t uvwasi__insert_stdio(uvwasi_t* uvwasi,
err = uvwasi_fd_table_insert(uvwasi,
table,
fd,
NULL,
name,
name,
type,
Expand All @@ -58,6 +59,7 @@ static uvwasi_errno_t uvwasi__insert_stdio(uvwasi_t* uvwasi,
uvwasi_errno_t uvwasi_fd_table_insert(uvwasi_t* uvwasi,
struct uvwasi_fd_table_t* table,
uv_file fd,
uv_tcp_t* sock,
const char* mapped_path,
const char* real_path,
uvwasi_filetype_t type,
Expand All @@ -78,29 +80,40 @@ uvwasi_errno_t uvwasi_fd_table_insert(uvwasi_t* uvwasi,
char* rp_copy;
char* np_copy;

mp_len = strlen(mapped_path);
rp_len = strlen(real_path);
if (type != UVWASI_FILETYPE_SOCKET_STREAM) {
mp_len = strlen(mapped_path);
rp_len = strlen(real_path);
} else {
mp_len = 0;
rp_len = 0;
rp_copy = NULL;
mp_copy = NULL;
np_copy = NULL;
}

/* Reserve room for the mapped path, real path, and normalized mapped path. */
entry = (struct uvwasi_fd_wrap_t*)
uvwasi__malloc(uvwasi, sizeof(*entry) + mp_len + mp_len + rp_len + 3);
if (entry == NULL)
return UVWASI_ENOMEM;

mp_copy = (char*)(entry + 1);
rp_copy = mp_copy + mp_len + 1;
np_copy = rp_copy + rp_len + 1;
memcpy(mp_copy, mapped_path, mp_len);
mp_copy[mp_len] = '\0';
memcpy(rp_copy, real_path, rp_len);
rp_copy[rp_len] = '\0';

/* Calculate the normalized version of the mapped path, as it will be used for
any path calculations on this fd. Use the length of the mapped path as an
upper bound for the normalized path length. */
err = uvwasi__normalize_path(mp_copy, mp_len, np_copy, mp_len);
if (err) {
uvwasi__free(uvwasi, entry);
goto exit;
if (type != UVWASI_FILETYPE_SOCKET_STREAM) {
mp_copy = (char*)(entry + 1);
rp_copy = mp_copy + mp_len + 1;
np_copy = rp_copy + rp_len + 1;
memcpy(mp_copy, mapped_path, mp_len);
mp_copy[mp_len] = '\0';
memcpy(rp_copy, real_path, rp_len);
rp_copy[rp_len] = '\0';

/* Calculate the normalized version of the mapped path, as it will be used for
any path calculations on this fd. Use the length of the mapped path as an
upper bound for the normalized path length. */
err = uvwasi__normalize_path(mp_copy, mp_len, np_copy, mp_len);
if (err) {
uvwasi__free(uvwasi, entry);
goto exit;
}
}

uv_rwlock_wrlock(&table->rwlock);
Expand Down Expand Up @@ -150,6 +163,7 @@ uvwasi_errno_t uvwasi_fd_table_insert(uvwasi_t* uvwasi,

entry->id = index;
entry->fd = fd;
entry->sock = sock;
entry->path = mp_copy;
entry->real_path = rp_copy;
entry->normalized_path = np_copy;
Expand Down Expand Up @@ -280,6 +294,7 @@ uvwasi_errno_t uvwasi_fd_table_insert_preopen(uvwasi_t* uvwasi,
return uvwasi_fd_table_insert(uvwasi,
table,
fd,
NULL,
path,
real_path,
UVWASI_FILETYPE_DIRECTORY,
Expand All @@ -290,6 +305,26 @@ uvwasi_errno_t uvwasi_fd_table_insert_preopen(uvwasi_t* uvwasi,
}


uvwasi_errno_t uvwasi_fd_table_insert_preopen_socket(uvwasi_t* uvwasi,
struct uvwasi_fd_table_t* table,
uv_tcp_t* sock) {
if (table == NULL || sock == NULL)
return UVWASI_EINVAL;

return uvwasi_fd_table_insert(uvwasi,
table,
-1,
sock,
NULL,
NULL,
UVWASI_FILETYPE_SOCKET_STREAM,
UVWASI__RIGHTS_SOCKET_BASE,
UVWASI__RIGHTS_SOCKET_INHERITING,
1,
NULL);
}


uvwasi_errno_t uvwasi_fd_table_get(struct uvwasi_fd_table_t* table,
const uvwasi_fd_t id,
struct uvwasi_fd_wrap_t** wrap,
Expand Down
5 changes: 5 additions & 0 deletions deps/uvwasi/src/fd_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct uvwasi_options_s;
struct uvwasi_fd_wrap_t {
uvwasi_fd_t id;
uv_file fd;
uv_tcp_t* sock;
char* path;
char* real_path;
char* normalized_path;
Expand All @@ -35,6 +36,7 @@ void uvwasi_fd_table_free(struct uvwasi_s* uvwasi,
uvwasi_errno_t uvwasi_fd_table_insert(struct uvwasi_s* uvwasi,
struct uvwasi_fd_table_t* table,
uv_file fd,
uv_tcp_t* sock,
const char* mapped_path,
const char* real_path,
uvwasi_filetype_t type,
Expand All @@ -47,6 +49,9 @@ uvwasi_errno_t uvwasi_fd_table_insert_preopen(struct uvwasi_s* uvwasi,
const uv_file fd,
const char* path,
const char* real_path);
uvwasi_errno_t uvwasi_fd_table_insert_preopen_socket(struct uvwasi_s* uvwasi,
struct uvwasi_fd_table_t* table,
uv_tcp_t* sock);
uvwasi_errno_t uvwasi_fd_table_get(struct uvwasi_fd_table_t* table,
const uvwasi_fd_t id,
struct uvwasi_fd_wrap_t** wrap,
Expand Down
95 changes: 95 additions & 0 deletions deps/uvwasi/src/sync_helpers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "uv.h"
#include "sync_helpers.h"
#include "uv_mapping.h"
#include "uvwasi_alloc.h"

typedef struct free_handle_data_s {
uvwasi_t* uvwasi;
int done;
} free_handle_data_t;

static void free_handle_cb(uv_handle_t* handle) {
free_handle_data_t* free_handle_data = uv_handle_get_data((uv_handle_t*) handle);
uvwasi__free(free_handle_data->uvwasi, handle);
free_handle_data->done = 1;
}

int free_handle_sync(struct uvwasi_s* uvwasi, uv_handle_t* handle) {
free_handle_data_t free_handle_data = { uvwasi, 0 };
uv_handle_set_data(handle, (void*) &free_handle_data);
uv_close(handle, free_handle_cb);
uv_loop_t* handle_loop = uv_handle_get_loop(handle);
while(!free_handle_data.done) {
if (uv_run(handle_loop, UV_RUN_ONCE) == 0) {
break;
}
}
return UVWASI_ESUCCESS;
}

static void do_stream_shutdown(uv_shutdown_t* req, int status) {
shutdown_data_t* shutdown_data;
shutdown_data = uv_handle_get_data((uv_handle_t*) req->handle);
shutdown_data->status = status;
shutdown_data->done = 1;
}

int shutdown_stream_sync(struct uvwasi_s* uvwasi,
uv_stream_t* stream,
shutdown_data_t* shutdown_data) {
uv_shutdown_t req;
uv_loop_t* stream_loop;

shutdown_data->done = 0;
shutdown_data->status = 0;
stream_loop = uv_handle_get_loop((uv_handle_t*) stream);

uv_handle_set_data((uv_handle_t*) stream, (void*) shutdown_data);
uv_shutdown(&req, stream, do_stream_shutdown);
while (!shutdown_data->done) {
if (uv_run(stream_loop, UV_RUN_ONCE) == 0) {
return UVWASI_ECANCELED;
}
}
return UVWASI_ESUCCESS;
}

static void recv_alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
recv_data_t* recv_data;
recv_data = uv_handle_get_data(handle);
buf->base = recv_data->base;
buf->len = recv_data->len;
}

void do_stream_recv(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
recv_data_t* recv_data;
recv_data = uv_handle_get_data((uv_handle_t*) stream);
uv_read_stop(stream);
recv_data->nread = nread;
recv_data->done = 1;
}

int read_stream_sync(struct uvwasi_s* uvwasi,
uv_stream_t* stream,
recv_data_t* recv_data) {
uv_loop_t* recv_loop;
int r;

recv_data->nread = 0;
recv_data->done = 0;
recv_loop = uv_handle_get_loop((uv_handle_t*) stream);

uv_handle_set_data((uv_handle_t*) stream, (void*) recv_data);
r = uv_read_start(stream, recv_alloc_cb, do_stream_recv);
if (r != 0) {
return uvwasi__translate_uv_error(r);
}

while (!recv_data->done) {
if (uv_run(recv_loop, UV_RUN_ONCE) == 0) {
return UVWASI_ECANCELED;
}
}

return UVWASI_ESUCCESS;
}
27 changes: 27 additions & 0 deletions deps/uvwasi/src/sync_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef __UVWASI_SYNC_HELPERS_H__
#define __UVWASI_SYNC_HELPERS_H__

struct uvwasi_s;

typedef struct shutdown_data_s {
int status;
int done;
} shutdown_data_t;

typedef struct recv_data_s {
char* base;
size_t len;
ssize_t nread;
int done;
} recv_data_t;

int free_handle_sync(struct uvwasi_s* uvwasi, uv_handle_t* handle);

int shutdown_stream_sync(struct uvwasi_s* uvwasi,
uv_stream_t* stream,
shutdown_data_t* shutdown_data);

int read_stream_sync(struct uvwasi_s* uvwasi,
uv_stream_t* stream,
recv_data_t* recv_data);
#endif /* __UVWASI_SYNC_HELPERS_H__ */
Loading

0 comments on commit f4b63ab

Please sign in to comment.