Skip to content

Commit e815655

Browse files
cjihrigtargos
authored andcommitted
wasi: throw on failed uvwasi_init()
Prior to this commit, if uvwasi_init() failed in any way, Node would abort due to a failed CHECK_EQ(). This commit changes that behavior to a thrown exception. PR-URL: #31076 Fixes: #30878 Reviewed-By: Jiawen Geng <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 6784296 commit e815655

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/node_wasi.cc

+43-1
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,64 @@ using v8::Array;
7373
using v8::ArrayBuffer;
7474
using v8::BigInt;
7575
using v8::Context;
76+
using v8::Exception;
7677
using v8::FunctionCallbackInfo;
7778
using v8::FunctionTemplate;
79+
using v8::Integer;
80+
using v8::Isolate;
7881
using v8::Local;
82+
using v8::MaybeLocal;
7983
using v8::Object;
8084
using v8::String;
8185
using v8::Uint32;
8286
using v8::Value;
8387

8488

89+
static MaybeLocal<Value> WASIException(Local<Context> context,
90+
int errorno,
91+
const char* syscall) {
92+
Isolate* isolate = context->GetIsolate();
93+
Environment* env = Environment::GetCurrent(context);
94+
CHECK_NOT_NULL(env);
95+
const char* err_name = uvwasi_embedder_err_code_to_string(errorno);
96+
Local<String> js_code = OneByteString(isolate, err_name);
97+
Local<String> js_syscall = OneByteString(isolate, syscall);
98+
Local<String> js_msg = js_code;
99+
js_msg =
100+
String::Concat(isolate, js_msg, FIXED_ONE_BYTE_STRING(isolate, ", "));
101+
js_msg = String::Concat(isolate, js_msg, js_syscall);
102+
Local<Object> e =
103+
Exception::Error(js_msg)->ToObject(context)
104+
.ToLocalChecked();
105+
106+
if (e->Set(context,
107+
env->errno_string(),
108+
Integer::New(isolate, errorno)).IsNothing() ||
109+
e->Set(context, env->code_string(), js_code).IsNothing() ||
110+
e->Set(context, env->syscall_string(), js_syscall).IsNothing()) {
111+
return MaybeLocal<Value>();
112+
}
113+
114+
return e;
115+
}
116+
117+
85118
WASI::WASI(Environment* env,
86119
Local<Object> object,
87120
uvwasi_options_t* options) : BaseObject(env, object) {
88121
MakeWeak();
89122
alloc_info_ = MakeAllocator();
90123
options->allocator = &alloc_info_;
91-
CHECK_EQ(uvwasi_init(&uvw_, options), UVWASI_ESUCCESS);
124+
int err = uvwasi_init(&uvw_, options);
125+
if (err != UVWASI_ESUCCESS) {
126+
Local<Context> context = env->context();
127+
MaybeLocal<Value> exception = WASIException(context, err, "uvwasi_init");
128+
129+
if (exception.IsEmpty())
130+
return;
131+
132+
context->GetIsolate()->ThrowException(exception.ToLocalChecked());
133+
}
92134
}
93135

94136

test/wasi/test-wasi-options-validation.js

+5
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ assert.throws(() => { new WASI({ preopens: 'fhqwhgads' }); },
2626
assert.throws(() => { new WASI(value); },
2727
{ code: 'ERR_INVALID_ARG_TYPE' });
2828
});
29+
30+
// Verify that exceptions thrown from the binding layer are handled.
31+
assert.throws(() => {
32+
new WASI({ preopens: { '/sandbox': '__/not/real/path' } });
33+
}, { code: 'UVWASI_ENOENT', message: /uvwasi_init/ });

0 commit comments

Comments
 (0)