Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: start adding socket tests #214

Merged
merged 42 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
eb2b277
feat: start adding socket tests
mhdawson Jun 27, 2023
3cc0f19
feat: initial uvwasi_sock_accept implemenmtation
mhdawson Jun 30, 2023
130c938
squash: address unused variable complaints
mhdawson Jun 30, 2023
2ac1484
squash: address ASAN errors
mhdawson Jun 30, 2023
540c69f
squash: add further tests using additional thread
mhdawson Jun 30, 2023
e2fc39e
squash: fixup ASAN warning
mhdawson Jun 30, 2023
02f046d
squash: cleanup
mhdawson Jun 30, 2023
657791f
feat: add initial implementations for send/recv
mhdawson Jul 5, 2023
f2c7513
fix: remove use of globals
mhdawson Jul 5, 2023
3c580cc
feat: continue with implementation
mhdawson Jul 5, 2023
4a1ae92
fix: fix the sock_accept
mhdawson Jul 6, 2023
9c54856
fix: fix up some ASAN errors
mhdawson Jul 7, 2023
4dfe831
fix: smallfix in send-recv test
mhdawson Jul 7, 2023
0851389
fix: fix last ASAN issue and misc cleanup
mhdawson Jul 7, 2023
cab9957
Update include/uvwasi.h
mhdawson Jul 14, 2023
fddd021
Update src/uvwasi.c
mhdawson Jul 14, 2023
69f8523
Update test/test-sock-send-recv.c
mhdawson Jul 14, 2023
8ce3e19
Update test/test-sock-send-recv.c
mhdawson Jul 14, 2023
1102436
Update src/uvwasi.c
mhdawson Jul 14, 2023
a068b72
Update src/uvwasi.c
mhdawson Jul 14, 2023
fa9f84d
squash: address basic review comments
mhdawson Jul 14, 2023
5a365ed
squash: move sync helpers to new files
mhdawson Jul 14, 2023
6b6b623
squash: add missing initialization
mhdawson Jul 19, 2023
34cc41a
squash: fix test problem revealed on osx
mhdawson Jul 20, 2023
037025a
test: fixup windows tests (#220)
mhdawson Jul 20, 2023
8a43356
squash: add debugging to socket functions
mhdawson Jul 20, 2023
dfe86d1
squash: fixup debug logging
mhdawson Jul 20, 2023
1cd13d6
squash: address review comments
mhdawson Jul 20, 2023
d1c4c4b
squash: add checks for unsupported options
mhdawson Jul 20, 2023
209c394
Update src/fd_table.c
mhdawson Jul 21, 2023
30d155a
Update test/test-sock-send-recv.c
mhdawson Jul 21, 2023
aa8b925
Update src/fd_table.c
mhdawson Jul 21, 2023
49b4ff1
Update test/test-sock-send-recv.c
mhdawson Jul 21, 2023
38f7969
Update test/test-sock-accept.c
mhdawson Jul 21, 2023
ad16b42
Update test/test-sock-send-recv.c
mhdawson Jul 21, 2023
b1654f4
Update test/test-sock-accept.c
mhdawson Jul 21, 2023
f94fb58
Update src/uvwasi.c
mhdawson Jul 21, 2023
c7393ad
Update src/uvwasi.c
mhdawson Jul 21, 2023
345d28a
Update src/sync_helpers.c
mhdawson Jul 21, 2023
f4baadc
Update src/sync_helpers.c
mhdawson Jul 21, 2023
7d27129
Update src/sync_helpers.c
mhdawson Jul 21, 2023
924c65a
squash: address review comments
mhdawson Jul 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ set(uvwasi_sources
src/fd_table.c
src/path_resolver.c
src/poll_oneoff.c
src/sync_helpers.c
src/uv_mapping.c
src/uvwasi.c
src/wasi_rights.c
Expand Down
2 changes: 2 additions & 0 deletions include/uvwasi.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
extern "C" {
#endif

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

Expand Down Expand Up @@ -47,6 +48,7 @@ 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 {
Expand Down
45 changes: 28 additions & 17 deletions src/fd_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,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
95 changes: 95 additions & 0 deletions 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 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