Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4465d23

Browse files
tniessencodebytere
authored andcommittedMay 11, 2020
wasi: prevent syscalls before start
PR-URL: #33235 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 6ffec50 commit 4465d23

File tree

4 files changed

+102
-45
lines changed

4 files changed

+102
-45
lines changed
 

‎doc/api/errors.md

+5
Original file line numberDiff line numberDiff line change
@@ -2099,6 +2099,11 @@ meaning of the error depends on the specific function.
20992099

21002100
The WASI instance has already started.
21012101

2102+
<a id="ERR_WASI_NOT_STARTED"></a>
2103+
### `ERR_WASI_NOT_STARTED`
2104+
2105+
The WASI instance has not been started.
2106+
21022107
<a id="ERR_WORKER_INIT_FAILED"></a>
21032108
### `ERR_WORKER_INIT_FAILED`
21042109

‎src/node_errors.h

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ void OnFatalError(const char* location, const char* message);
5454
V(ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER, TypeError) \
5555
V(ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED, Error) \
5656
V(ERR_VM_MODULE_CACHED_DATA_REJECTED, Error) \
57+
V(ERR_WASI_NOT_STARTED, Error) \
5758
V(ERR_WORKER_INIT_FAILED, Error) \
5859
V(ERR_PROTO_ACCESS, Error)
5960

@@ -104,6 +105,7 @@ void OnFatalError(const char* location, const char* message);
104105
V(ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER, \
105106
"Cannot serialize externalized SharedArrayBuffer") \
106107
V(ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED, "Failed to set PSK identity hint") \
108+
V(ERR_WASI_NOT_STARTED, "wasi.start() has not been called") \
107109
V(ERR_WORKER_INIT_FAILED, "Worker initialization failure") \
108110
V(ERR_PROTO_ACCESS, \
109111
"Accessing Object.prototype.__proto__ has been " \

‎src/node_wasi.cc

+55-45
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "node_mem-inl.h"
66
#include "util-inl.h"
77
#include "node.h"
8+
#include "node_errors.h"
89
#include "uv.h"
910
#include "uvwasi.h"
1011
#include "node_wasi.h"
@@ -23,6 +24,15 @@ inline void Debug(WASI* wasi, Args&&... args) {
2324
Debug(wasi->env(), DebugCategory::WASI, std::forward<Args>(args)...);
2425
}
2526

27+
#define ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(ptr, obj) \
28+
do { \
29+
ASSIGN_OR_RETURN_UNWRAP(ptr, obj); \
30+
if ((*(ptr))->memory_.IsEmpty()) { \
31+
THROW_ERR_WASI_NOT_STARTED(Environment::GetCurrent(args)); \
32+
return; \
33+
} \
34+
} while (0)
35+
2636
#define RETURN_IF_BAD_ARG_COUNT(args, expected) \
2737
do { \
2838
if ((args).Length() != (expected)) { \
@@ -250,7 +260,7 @@ void WASI::ArgsGet(const FunctionCallbackInfo<Value>& args) {
250260
RETURN_IF_BAD_ARG_COUNT(args, 2);
251261
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, argv_offset);
252262
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, argv_buf_offset);
253-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
263+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
254264
Debug(wasi, "args_get(%d, %d)\n", argv_offset, argv_buf_offset);
255265
GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
256266
CHECK_BOUNDS_OR_RETURN(args,
@@ -282,7 +292,7 @@ void WASI::ArgsSizesGet(const FunctionCallbackInfo<Value>& args) {
282292
RETURN_IF_BAD_ARG_COUNT(args, 2);
283293
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, argc_offset);
284294
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, argv_buf_offset);
285-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
295+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
286296
Debug(wasi, "args_sizes_get(%d, %d)\n", argc_offset, argv_buf_offset);
287297
GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
288298
CHECK_BOUNDS_OR_RETURN(args, mem_size, argc_offset, 4);
@@ -310,7 +320,7 @@ void WASI::ClockResGet(const FunctionCallbackInfo<Value>& args) {
310320
RETURN_IF_BAD_ARG_COUNT(args, 2);
311321
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, clock_id);
312322
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, resolution_ptr);
313-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
323+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
314324
Debug(wasi, "clock_res_get(%d, %d)\n", clock_id, resolution_ptr);
315325
GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
316326
CHECK_BOUNDS_OR_RETURN(args, mem_size, resolution_ptr, 8);
@@ -336,7 +346,7 @@ void WASI::ClockTimeGet(const FunctionCallbackInfo<Value>& args) {
336346
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, clock_id);
337347
UNWRAP_BIGINT_OR_RETURN(args, args[1], Uint64, precision);
338348
CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, time_ptr);
339-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
349+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
340350
Debug(wasi, "clock_time_get(%d, %d, %d)\n", clock_id, precision, time_ptr);
341351
GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
342352
CHECK_BOUNDS_OR_RETURN(args, mem_size, time_ptr, 8);
@@ -361,7 +371,7 @@ void WASI::EnvironGet(const FunctionCallbackInfo<Value>& args) {
361371
RETURN_IF_BAD_ARG_COUNT(args, 2);
362372
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, environ_offset);
363373
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, environ_buf_offset);
364-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
374+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
365375
Debug(wasi, "environ_get(%d, %d)\n", environ_offset, environ_buf_offset);
366376
GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
367377
CHECK_BOUNDS_OR_RETURN(args,
@@ -395,7 +405,7 @@ void WASI::EnvironSizesGet(const FunctionCallbackInfo<Value>& args) {
395405
RETURN_IF_BAD_ARG_COUNT(args, 2);
396406
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, envc_offset);
397407
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, env_buf_offset);
398-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
408+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
399409
Debug(wasi, "environ_sizes_get(%d, %d)\n", envc_offset, env_buf_offset);
400410
GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
401411
CHECK_BOUNDS_OR_RETURN(args, mem_size, envc_offset, 4);
@@ -425,7 +435,7 @@ void WASI::FdAdvise(const FunctionCallbackInfo<Value>& args) {
425435
UNWRAP_BIGINT_OR_RETURN(args, args[1], Uint64, offset);
426436
UNWRAP_BIGINT_OR_RETURN(args, args[2], Uint64, len);
427437
CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, advice);
428-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
438+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
429439
Debug(wasi, "fd_advise(%d, %d, %d, %d)\n", fd, offset, len, advice);
430440
uvwasi_errno_t err = uvwasi_fd_advise(&wasi->uvw_, fd, offset, len, advice);
431441
args.GetReturnValue().Set(err);
@@ -441,7 +451,7 @@ void WASI::FdAllocate(const FunctionCallbackInfo<Value>& args) {
441451
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
442452
UNWRAP_BIGINT_OR_RETURN(args, args[1], Uint64, offset);
443453
UNWRAP_BIGINT_OR_RETURN(args, args[2], Uint64, len);
444-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
454+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
445455
Debug(wasi, "fd_allocate(%d, %d, %d)\n", fd, offset, len);
446456
uvwasi_errno_t err = uvwasi_fd_allocate(&wasi->uvw_, fd, offset, len);
447457
args.GetReturnValue().Set(err);
@@ -453,7 +463,7 @@ void WASI::FdClose(const FunctionCallbackInfo<Value>& args) {
453463
uint32_t fd;
454464
RETURN_IF_BAD_ARG_COUNT(args, 1);
455465
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
456-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
466+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
457467
Debug(wasi, "fd_close(%d)\n", fd);
458468
uvwasi_errno_t err = uvwasi_fd_close(&wasi->uvw_, fd);
459469
args.GetReturnValue().Set(err);
@@ -465,7 +475,7 @@ void WASI::FdDatasync(const FunctionCallbackInfo<Value>& args) {
465475
uint32_t fd;
466476
RETURN_IF_BAD_ARG_COUNT(args, 1);
467477
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
468-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
478+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
469479
Debug(wasi, "fd_datasync(%d)\n", fd);
470480
uvwasi_errno_t err = uvwasi_fd_datasync(&wasi->uvw_, fd);
471481
args.GetReturnValue().Set(err);
@@ -481,7 +491,7 @@ void WASI::FdFdstatGet(const FunctionCallbackInfo<Value>& args) {
481491
RETURN_IF_BAD_ARG_COUNT(args, 2);
482492
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
483493
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, buf);
484-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
494+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
485495
Debug(wasi, "fd_fdstat_get(%d, %d)\n", fd, buf);
486496
GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
487497
CHECK_BOUNDS_OR_RETURN(args, mem_size, buf, 24);
@@ -506,7 +516,7 @@ void WASI::FdFdstatSetFlags(const FunctionCallbackInfo<Value>& args) {
506516
RETURN_IF_BAD_ARG_COUNT(args, 2);
507517
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
508518
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, flags);
509-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
519+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
510520
Debug(wasi, "fd_fdstat_set_flags(%d, %d)\n", fd, flags);
511521
uvwasi_errno_t err = uvwasi_fd_fdstat_set_flags(&wasi->uvw_, fd, flags);
512522
args.GetReturnValue().Set(err);
@@ -522,7 +532,7 @@ void WASI::FdFdstatSetRights(const FunctionCallbackInfo<Value>& args) {
522532
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
523533
UNWRAP_BIGINT_OR_RETURN(args, args[1], Uint64, fs_rights_base);
524534
UNWRAP_BIGINT_OR_RETURN(args, args[2], Uint64, fs_rights_inheriting);
525-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
535+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
526536
Debug(wasi,
527537
"fd_fdstat_set_rights(%d, %d, %d)\n",
528538
fd,
@@ -545,7 +555,7 @@ void WASI::FdFilestatGet(const FunctionCallbackInfo<Value>& args) {
545555
RETURN_IF_BAD_ARG_COUNT(args, 2);
546556
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
547557
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, buf);
548-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
558+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
549559
Debug(wasi, "fd_filestat_get(%d, %d)\n", fd, buf);
550560
GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
551561
CHECK_BOUNDS_OR_RETURN(args, mem_size, buf, 64);
@@ -574,7 +584,7 @@ void WASI::FdFilestatSetSize(const FunctionCallbackInfo<Value>& args) {
574584
RETURN_IF_BAD_ARG_COUNT(args, 2);
575585
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
576586
UNWRAP_BIGINT_OR_RETURN(args, args[1], Uint64, st_size);
577-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
587+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
578588
Debug(wasi, "fd_filestat_set_size(%d, %d)\n", fd, st_size);
579589
uvwasi_errno_t err = uvwasi_fd_filestat_set_size(&wasi->uvw_, fd, st_size);
580590
args.GetReturnValue().Set(err);
@@ -592,7 +602,7 @@ void WASI::FdFilestatSetTimes(const FunctionCallbackInfo<Value>& args) {
592602
UNWRAP_BIGINT_OR_RETURN(args, args[1], Uint64, st_atim);
593603
UNWRAP_BIGINT_OR_RETURN(args, args[2], Uint64, st_mtim);
594604
CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, fst_flags);
595-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
605+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
596606
Debug(wasi,
597607
"fd_filestat_set_times(%d, %d, %d, %d)\n",
598608
fd,
@@ -623,7 +633,7 @@ void WASI::FdPread(const FunctionCallbackInfo<Value>& args) {
623633
CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, iovs_len);
624634
UNWRAP_BIGINT_OR_RETURN(args, args[3], Uint64, offset);
625635
CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, nread_ptr);
626-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
636+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
627637
Debug(wasi,
628638
"uvwasi_fd_pread(%d, %d, %d, %d, %d)\n",
629639
fd,
@@ -683,7 +693,7 @@ void WASI::FdPrestatGet(const FunctionCallbackInfo<Value>& args) {
683693
RETURN_IF_BAD_ARG_COUNT(args, 2);
684694
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
685695
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, buf);
686-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
696+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
687697
Debug(wasi, "fd_prestat_get(%d, %d)\n", fd, buf);
688698
GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
689699
CHECK_BOUNDS_OR_RETURN(args, mem_size, buf, 8);
@@ -710,7 +720,7 @@ void WASI::FdPrestatDirName(const FunctionCallbackInfo<Value>& args) {
710720
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
711721
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, path_ptr);
712722
CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, path_len);
713-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
723+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
714724
Debug(wasi, "fd_prestat_dir_name(%d, %d, %d)\n", fd, path_ptr, path_len);
715725
GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
716726
CHECK_BOUNDS_OR_RETURN(args, mem_size, path_ptr, path_len);
@@ -737,7 +747,7 @@ void WASI::FdPwrite(const FunctionCallbackInfo<Value>& args) {
737747
CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, iovs_len);
738748
UNWRAP_BIGINT_OR_RETURN(args, args[3], Uint64, offset);
739749
CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, nwritten_ptr);
740-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
750+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
741751
Debug(wasi,
742752
"uvwasi_fd_pwrite(%d, %d, %d, %d, %d)\n",
743753
fd,
@@ -801,7 +811,7 @@ void WASI::FdRead(const FunctionCallbackInfo<Value>& args) {
801811
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, iovs_ptr);
802812
CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, iovs_len);
803813
CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, nread_ptr);
804-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
814+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
805815
Debug(wasi, "fd_read(%d, %d, %d, %d)\n", fd, iovs_ptr, iovs_len, nread_ptr);
806816
GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
807817
CHECK_BOUNDS_OR_RETURN(args, mem_size, iovs_ptr, iovs_len * 8);
@@ -860,7 +870,7 @@ void WASI::FdReaddir(const FunctionCallbackInfo<Value>& args) {
860870
CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, buf_len);
861871
UNWRAP_BIGINT_OR_RETURN(args, args[3], Uint64, cookie);
862872
CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, bufused_ptr);
863-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
873+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
864874
Debug(wasi,
865875
"uvwasi_fd_readdir(%d, %d, %d, %d, %d)\n",
866876
fd,
@@ -892,7 +902,7 @@ void WASI::FdRenumber(const FunctionCallbackInfo<Value>& args) {
892902
RETURN_IF_BAD_ARG_COUNT(args, 2);
893903
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, from);
894904
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, to);
895-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
905+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
896906
Debug(wasi, "fd_renumber(%d, %d)\n", from, to);
897907
uvwasi_errno_t err = uvwasi_fd_renumber(&wasi->uvw_, from, to);
898908
args.GetReturnValue().Set(err);
@@ -912,7 +922,7 @@ void WASI::FdSeek(const FunctionCallbackInfo<Value>& args) {
912922
UNWRAP_BIGINT_OR_RETURN(args, args[1], Int64, offset);
913923
CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, whence);
914924
CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, newoffset_ptr);
915-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
925+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
916926
Debug(wasi, "fd_seek(%d, %d, %d, %d)\n", fd, offset, whence, newoffset_ptr);
917927
GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
918928
CHECK_BOUNDS_OR_RETURN(args, mem_size, newoffset_ptr, 8);
@@ -934,7 +944,7 @@ void WASI::FdSync(const FunctionCallbackInfo<Value>& args) {
934944
uint32_t fd;
935945
RETURN_IF_BAD_ARG_COUNT(args, 1);
936946
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
937-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
947+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
938948
Debug(wasi, "fd_sync(%d)\n", fd);
939949
uvwasi_errno_t err = uvwasi_fd_sync(&wasi->uvw_, fd);
940950
args.GetReturnValue().Set(err);
@@ -950,7 +960,7 @@ void WASI::FdTell(const FunctionCallbackInfo<Value>& args) {
950960
RETURN_IF_BAD_ARG_COUNT(args, 2);
951961
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
952962
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, offset_ptr);
953-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
963+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
954964
Debug(wasi, "fd_tell(%d, %d)\n", fd, offset_ptr);
955965
GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
956966
CHECK_BOUNDS_OR_RETURN(args, mem_size, offset_ptr, 8);
@@ -977,7 +987,7 @@ void WASI::FdWrite(const FunctionCallbackInfo<Value>& args) {
977987
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, iovs_ptr);
978988
CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, iovs_len);
979989
CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, nwritten_ptr);
980-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
990+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
981991
Debug(wasi,
982992
"fd_write(%d, %d, %d, %d)\n",
983993
fd,
@@ -1037,7 +1047,7 @@ void WASI::PathCreateDirectory(const FunctionCallbackInfo<Value>& args) {
10371047
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
10381048
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, path_ptr);
10391049
CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, path_len);
1040-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
1050+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
10411051
Debug(wasi, "path_create_directory(%d, %d, %d)\n", fd, path_ptr, path_len);
10421052
GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
10431053
CHECK_BOUNDS_OR_RETURN(args, mem_size, path_ptr, path_len);
@@ -1064,7 +1074,7 @@ void WASI::PathFilestatGet(const FunctionCallbackInfo<Value>& args) {
10641074
CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, path_ptr);
10651075
CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, path_len);
10661076
CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, buf_ptr);
1067-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
1077+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
10681078
Debug(wasi,
10691079
"path_filestat_get(%d, %d, %d)\n",
10701080
fd,
@@ -1114,7 +1124,7 @@ void WASI::PathFilestatSetTimes(const FunctionCallbackInfo<Value>& args) {
11141124
UNWRAP_BIGINT_OR_RETURN(args, args[4], Uint64, st_atim);
11151125
UNWRAP_BIGINT_OR_RETURN(args, args[5], Uint64, st_mtim);
11161126
CHECK_TO_TYPE_OR_RETURN(args, args[6], Uint32, fst_flags);
1117-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
1127+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
11181128
Debug(wasi,
11191129
"path_filestat_set_times(%d, %d, %d, %d, %d, %d, %d)\n",
11201130
fd,
@@ -1157,7 +1167,7 @@ void WASI::PathLink(const FunctionCallbackInfo<Value>& args) {
11571167
CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, new_fd);
11581168
CHECK_TO_TYPE_OR_RETURN(args, args[5], Uint32, new_path_ptr);
11591169
CHECK_TO_TYPE_OR_RETURN(args, args[6], Uint32, new_path_len);
1160-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
1170+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
11611171
Debug(wasi,
11621172
"path_link(%d, %d, %d, %d, %d, %d, %d)\n",
11631173
old_fd,
@@ -1205,7 +1215,7 @@ void WASI::PathOpen(const FunctionCallbackInfo<Value>& args) {
12051215
UNWRAP_BIGINT_OR_RETURN(args, args[6], Uint64, fs_rights_inheriting);
12061216
CHECK_TO_TYPE_OR_RETURN(args, args[7], Uint32, fs_flags);
12071217
CHECK_TO_TYPE_OR_RETURN(args, args[8], Uint32, fd_ptr);
1208-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
1218+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
12091219
Debug(wasi,
12101220
"path_open(%d, %d, %d, %d, %d, %d, %d, %d, %d)\n",
12111221
dirfd,
@@ -1255,7 +1265,7 @@ void WASI::PathReadlink(const FunctionCallbackInfo<Value>& args) {
12551265
CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, buf_ptr);
12561266
CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, buf_len);
12571267
CHECK_TO_TYPE_OR_RETURN(args, args[5], Uint32, bufused_ptr);
1258-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
1268+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
12591269
Debug(wasi,
12601270
"path_readlink(%d, %d, %d, %d, %d, %d)\n",
12611271
fd,
@@ -1294,7 +1304,7 @@ void WASI::PathRemoveDirectory(const FunctionCallbackInfo<Value>& args) {
12941304
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
12951305
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, path_ptr);
12961306
CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, path_len);
1297-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
1307+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
12981308
Debug(wasi, "path_remove_directory(%d, %d, %d)\n", fd, path_ptr, path_len);
12991309
GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
13001310
CHECK_BOUNDS_OR_RETURN(args, mem_size, path_ptr, path_len);
@@ -1323,7 +1333,7 @@ void WASI::PathRename(const FunctionCallbackInfo<Value>& args) {
13231333
CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, new_fd);
13241334
CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, new_path_ptr);
13251335
CHECK_TO_TYPE_OR_RETURN(args, args[5], Uint32, new_path_len);
1326-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
1336+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
13271337
Debug(wasi,
13281338
"path_rename(%d, %d, %d, %d, %d, %d)\n",
13291339
old_fd,
@@ -1361,7 +1371,7 @@ void WASI::PathSymlink(const FunctionCallbackInfo<Value>& args) {
13611371
CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, fd);
13621372
CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, new_path_ptr);
13631373
CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, new_path_len);
1364-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
1374+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
13651375
Debug(wasi,
13661376
"path_symlink(%d, %d, %d, %d, %d)\n",
13671377
old_path_ptr,
@@ -1393,7 +1403,7 @@ void WASI::PathUnlinkFile(const FunctionCallbackInfo<Value>& args) {
13931403
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
13941404
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, path_ptr);
13951405
CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, path_len);
1396-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
1406+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
13971407
Debug(wasi, "path_unlink_file(%d, %d, %d)\n", fd, path_ptr, path_len);
13981408
GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
13991409
CHECK_BOUNDS_OR_RETURN(args, mem_size, path_ptr, path_len);
@@ -1418,7 +1428,7 @@ void WASI::PollOneoff(const FunctionCallbackInfo<Value>& args) {
14181428
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, out_ptr);
14191429
CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, nsubscriptions);
14201430
CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, nevents_ptr);
1421-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
1431+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
14221432
Debug(wasi,
14231433
"poll_oneoff(%d, %d, %d, %d)\n",
14241434
in_ptr,
@@ -1500,7 +1510,7 @@ void WASI::ProcExit(const FunctionCallbackInfo<Value>& args) {
15001510
uint32_t code;
15011511
RETURN_IF_BAD_ARG_COUNT(args, 1);
15021512
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, code);
1503-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
1513+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
15041514
Debug(wasi, "proc_exit(%d)\n", code);
15051515
args.GetReturnValue().Set(uvwasi_proc_exit(&wasi->uvw_, code));
15061516
}
@@ -1511,7 +1521,7 @@ void WASI::ProcRaise(const FunctionCallbackInfo<Value>& args) {
15111521
uint32_t sig;
15121522
RETURN_IF_BAD_ARG_COUNT(args, 1);
15131523
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, sig);
1514-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
1524+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
15151525
Debug(wasi, "proc_raise(%d)\n", sig);
15161526
uvwasi_errno_t err = uvwasi_proc_raise(&wasi->uvw_, sig);
15171527
args.GetReturnValue().Set(err);
@@ -1527,7 +1537,7 @@ void WASI::RandomGet(const FunctionCallbackInfo<Value>& args) {
15271537
RETURN_IF_BAD_ARG_COUNT(args, 2);
15281538
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, buf_ptr);
15291539
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, buf_len);
1530-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
1540+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
15311541
Debug(wasi, "random_get(%d, %d)\n", buf_ptr, buf_len);
15321542
GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
15331543
CHECK_BOUNDS_OR_RETURN(args, mem_size, buf_ptr, buf_len);
@@ -1541,7 +1551,7 @@ void WASI::RandomGet(const FunctionCallbackInfo<Value>& args) {
15411551
void WASI::SchedYield(const FunctionCallbackInfo<Value>& args) {
15421552
WASI* wasi;
15431553
RETURN_IF_BAD_ARG_COUNT(args, 0);
1544-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
1554+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
15451555
Debug(wasi, "sched_yield()\n");
15461556
uvwasi_errno_t err = uvwasi_sched_yield(&wasi->uvw_);
15471557
args.GetReturnValue().Set(err);
@@ -1565,7 +1575,7 @@ void WASI::SockRecv(const FunctionCallbackInfo<Value>& args) {
15651575
CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, ri_flags);
15661576
CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, ro_datalen_ptr);
15671577
CHECK_TO_TYPE_OR_RETURN(args, args[5], Uint32, ro_flags_ptr);
1568-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
1578+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
15691579
Debug(wasi,
15701580
"sock_recv(%d, %d, %d, %d, %d, %d)\n",
15711581
sock,
@@ -1637,7 +1647,7 @@ void WASI::SockSend(const FunctionCallbackInfo<Value>& args) {
16371647
CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, si_data_len);
16381648
CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, si_flags);
16391649
CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, so_datalen_ptr);
1640-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
1650+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
16411651
Debug(wasi,
16421652
"sock_send(%d, %d, %d, %d, %d)\n",
16431653
sock,
@@ -1695,7 +1705,7 @@ void WASI::SockShutdown(const FunctionCallbackInfo<Value>& args) {
16951705
RETURN_IF_BAD_ARG_COUNT(args, 2);
16961706
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, sock);
16971707
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, how);
1698-
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
1708+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
16991709
Debug(wasi, "sock_shutdown(%d, %d)\n", sock, how);
17001710
uvwasi_errno_t err = uvwasi_sock_shutdown(&wasi->uvw_, sock, how);
17011711
args.GetReturnValue().Set(err);

‎test/wasi/test-wasi-not-started.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
require('../common');
3+
4+
if (process.argv[2] === 'wasi-child') {
5+
const assert = require('assert');
6+
const fs = require('fs');
7+
const path = require('path');
8+
9+
const { WASI } = require('wasi');
10+
const wasi = new WASI({
11+
args: ['foo', '-bar', '--baz=value']
12+
});
13+
const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
14+
15+
const modulePath = path.join(__dirname, 'wasm', 'main_args.wasm');
16+
const buffer = fs.readFileSync(modulePath);
17+
18+
assert.rejects(async () => {
19+
const { instance } = await WebAssembly.instantiate(buffer, importObject);
20+
instance.exports._start();
21+
}, {
22+
name: 'Error',
23+
code: 'ERR_WASI_NOT_STARTED',
24+
message: 'wasi.start() has not been called'
25+
});
26+
} else {
27+
const assert = require('assert');
28+
const cp = require('child_process');
29+
30+
const child = cp.spawnSync(process.execPath, [
31+
'--experimental-wasi-unstable-preview1',
32+
'--experimental-wasm-bigint',
33+
__filename,
34+
'wasi-child'
35+
], {
36+
env: { ...process.env, NODE_DEBUG_NATIVE: 'wasi' }
37+
});
38+
assert.strictEqual(child.signal, null);
39+
assert.strictEqual(child.status, 0);
40+
}

0 commit comments

Comments
 (0)
Please sign in to comment.