Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e815655

Browse filesBrowse 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 <technicalcute@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 6784296 commit e815655
Copy full SHA for e815655

File tree

Expand file treeCollapse file tree

2 files changed

+48
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+48
-1
lines changed
Open diff view settings
Collapse file

‎src/node_wasi.cc‎

Copy file name to clipboardExpand all lines: src/node_wasi.cc
+43-1Lines changed: 43 additions & 1 deletion
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

Collapse file

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

Copy file name to clipboardExpand all lines: test/wasi/test-wasi-options-validation.js
+5Lines changed: 5 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.