Skip to content

Commit

Permalink
wasi: add wasi sock_accept stub
Browse files Browse the repository at this point in the history
Refs: nodejs/uvwasi#185

Add stub for sock_accept so that we have stubs
for all of the sock methods in wasi_snapshot_preview1.
Its a bit awkward as the method was added after the
initial definitial of wasi_snapshot-preview1 but I
think it should be semver minor at most to add
the method.

Depends on nodejs/uvwasi#185
being landed in uvwasi first and an updated version
of uvwasi that includes that being pulled into
Node.js

Signed-off-by: Michael Dawson <mdawson@devrus.com>

PR-URL: nodejs#46434
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
mhdawson committed Apr 6, 2023
1 parent e588b63 commit 74a82ea
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
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.

0 comments on commit 74a82ea

Please sign in to comment.