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

[v18.x backport] wasi: add wasi sock_accept stub #47455

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion deps/uvwasi/include/uvwasi.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern "C" {

#define UVWASI_VERSION_MAJOR 0
#define UVWASI_VERSION_MINOR 0
#define UVWASI_VERSION_PATCH 15
#define UVWASI_VERSION_PATCH 16
#define UVWASI_VERSION_HEX ((UVWASI_VERSION_MAJOR << 16) | \
(UVWASI_VERSION_MINOR << 8) | \
(UVWASI_VERSION_PATCH))
Expand Down Expand Up @@ -251,6 +251,10 @@ uvwasi_errno_t uvwasi_random_get(uvwasi_t* uvwasi,
void* buf,
uvwasi_size_t buf_len);
uvwasi_errno_t uvwasi_sched_yield(uvwasi_t* uvwasi);
uvwasi_errno_t uvwasi_sock_accept(uvwasi_t* uvwasi,
uvwasi_fd_t sock,
uvwasi_fdflags_t flags,
uvwasi_fd_t* fd);
uvwasi_errno_t uvwasi_sock_recv(uvwasi_t* uvwasi,
uvwasi_fd_t sock,
const uvwasi_iovec_t* ri_data,
Expand Down
1 change: 1 addition & 0 deletions deps/uvwasi/include/wasi_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ typedef uint64_t uvwasi_rights_t; /* Bitfield */
#define UVWASI_RIGHT_PATH_UNLINK_FILE (1 << 26)
#define UVWASI_RIGHT_POLL_FD_READWRITE (1 << 27)
#define UVWASI_RIGHT_SOCK_SHUTDOWN (1 << 28)
#define UVWASI_RIGHT_SOCK_ACCEPT (1 << 29)

typedef uint16_t uvwasi_roflags_t; /* Bitfield */
#define UVWASI_SOCK_RECV_DATA_TRUNCATED (1 << 0)
Expand Down
9 changes: 9 additions & 0 deletions deps/uvwasi/src/uvwasi.c
Original file line number Diff line number Diff line change
Expand Up @@ -2557,6 +2557,15 @@ uvwasi_errno_t uvwasi_sock_shutdown(uvwasi_t* uvwasi,
return UVWASI_ENOTSUP;
}

uvwasi_errno_t uvwasi_sock_accept(uvwasi_t* uvwasi,
uvwasi_fd_t sock,
uvwasi_fdflags_t flags,
uvwasi_fd_t* fd) {
/* TODO(mhdawson): Needs implementation */
UVWASI_DEBUG("uvwasi_sock_accept(uvwasi=%p, unimplemented)\n", uvwasi);
return UVWASI_ENOTSUP;
};


const char* uvwasi_embedder_err_code_to_string(uvwasi_errno_t code) {
switch (code) {
Expand Down
3 changes: 2 additions & 1 deletion deps/uvwasi/src/wasi_rights.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
UVWASI_RIGHT_PATH_UNLINK_FILE | \
UVWASI_RIGHT_PATH_REMOVE_DIRECTORY | \
UVWASI_RIGHT_POLL_FD_READWRITE | \
UVWASI_RIGHT_SOCK_SHUTDOWN)
UVWASI_RIGHT_SOCK_SHUTDOWN | \
UVWASI_RIGHT_SOCK_ACCEPT)

#define UVWASI__RIGHTS_BLOCK_DEVICE_BASE UVWASI__RIGHTS_ALL
#define UVWASI__RIGHTS_BLOCK_DEVICE_INHERITING UVWASI__RIGHTS_ALL
Expand Down
32 changes: 32 additions & 0 deletions src/node_wasi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,37 @@ void WASI::SchedYield(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(err);
}

void WASI::SockAccept(const FunctionCallbackInfo<Value>& args) {
WASI* wasi;
uint32_t sock;
uint32_t flags;
uint32_t fd_ptr;
char* memory;
size_t mem_size;
RETURN_IF_BAD_ARG_COUNT(args, 3);
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, sock);
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, flags);
CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, fd_ptr);
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
Debug(wasi,
"sock_accept(%d, %d, %d)\n",
sock,
flags,
fd_ptr);
GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
CHECK_BOUNDS_OR_RETURN(args, mem_size, fd_ptr, UVWASI_SERDES_SIZE_fd_t);

uvwasi_fd_t fd;
uvwasi_errno_t err = uvwasi_sock_accept(&wasi->uvw_,
sock,
flags,
&fd);

if (err == UVWASI_ESUCCESS)
uvwasi_serdes_write_size_t(memory, fd_ptr, fd);

args.GetReturnValue().Set(err);
}

void WASI::SockRecv(const FunctionCallbackInfo<Value>& args) {
WASI* wasi;
Expand Down Expand Up @@ -1720,6 +1751,7 @@ static void Initialize(Local<Object> target,
SetProtoMethod(isolate, tmpl, "proc_raise", WASI::ProcRaise);
SetProtoMethod(isolate, tmpl, "random_get", WASI::RandomGet);
SetProtoMethod(isolate, tmpl, "sched_yield", WASI::SchedYield);
SetProtoMethod(isolate, tmpl, "sock_accept", WASI::SockAccept);
SetProtoMethod(isolate, tmpl, "sock_recv", WASI::SockRecv);
SetProtoMethod(isolate, tmpl, "sock_send", WASI::SockSend);
SetProtoMethod(isolate, tmpl, "sock_shutdown", WASI::SockShutdown);
Expand Down
1 change: 1 addition & 0 deletions src/node_wasi.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class WASI : public BaseObject,
static void ProcRaise(const v8::FunctionCallbackInfo<v8::Value>& args);
static void RandomGet(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SchedYield(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SockAccept(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SockRecv(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SockSend(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SockShutdown(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down
17 changes: 17 additions & 0 deletions test/wasi/c/sock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <sys/socket.h>
#include <stddef.h>
#include <errno.h>
#include <assert.h>
#include <stdio.h>

// TODO(mhdawson): Update once sock_accept is implemented in uvwasi
int main(void) {
int fd = 0 ;
socklen_t addrlen = 0;
int flags = 0;
int ret = accept(0, NULL, &addrlen);
assert(ret == -1);
assert(errno == ENOTSUP);

return 0;
}
1 change: 1 addition & 0 deletions test/wasi/test-wasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ if (process.argv[2] === 'wasi-child') {
stdout: `hello from input.txt${checkoutEOL}hello from input.txt${checkoutEOL}`,
});
runWASI({ test: 'stat' });
runWASI({ test: 'sock' });
runWASI({ test: 'write_file' });

// Tests that are currently unsupported on Windows.
Expand Down
Binary file added test/wasi/wasm/sock.wasm
Binary file not shown.