-
Notifications
You must be signed in to change notification settings - Fork 5k
napi: block worker shutdown on in-flight napi_async_work (UAF) #36855
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
Closed
robobun
wants to merge
6
commits into
main
from
claude/farm/ca24401f/napi-async-work-worker-terminate
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5a9a6cf
napi: block worker shutdown on in-flight napi_async_work (UAF)
robobun 830ecdf
address review: copy event_loop before enqueue; futex wait; tighten test
robobun c2dfcc6
drop shutdown-drain complete(): barrier-only fix
robobun 6b08a71
unref: drop post-release Futex::wake; timed wait in the barrier
robobun 342c010
shutdown drain: free the napi_async_work box instead of re-queueing
robobun 40d869d
[autofix.ci] apply automated fixes
autofix-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| #include <node_api.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| #ifdef _WIN32 | ||
| #include <windows.h> | ||
| static void sleep_ms(unsigned ms) { Sleep(ms); } | ||
| #else | ||
| #include <unistd.h> | ||
| static void sleep_ms(unsigned ms) { usleep(ms * 1000); } | ||
| #endif | ||
|
|
||
| #define CHECK(env, call) \ | ||
| do { \ | ||
| napi_status s_ = (call); \ | ||
| if (s_ != napi_ok) { \ | ||
| napi_throw_error((env), NULL, #call " failed"); \ | ||
| return NULL; \ | ||
| } \ | ||
| } while (0) | ||
|
|
||
| typedef struct { | ||
| napi_async_work work; | ||
| napi_ref buf_ref; | ||
| napi_ref cb_ref; | ||
| unsigned char *data; | ||
| size_t len; | ||
| unsigned sleep_ms; | ||
| } work_t; | ||
|
|
||
| static void exec_cb(napi_env env, void *arg) { | ||
| work_t *w = (work_t *)arg; | ||
| sleep_ms(w->sleep_ms); | ||
| // Touch the ArrayBuffer backing store. Before the fix, worker.terminate() | ||
| // could free it (via JSC VM teardown running the ArrayBuffer finalizer) | ||
| // while this callback is still running on the pool thread. | ||
| if (w->len > 0) { | ||
| volatile unsigned char first = w->data[0]; | ||
| (void)first; | ||
| memset(w->data, 0xab, w->len); | ||
| } | ||
| } | ||
|
|
||
| static void done_cb(napi_env env, napi_status status, void *arg) { | ||
| work_t *w = (work_t *)arg; | ||
| napi_value cb = NULL, undef = NULL, argv[1]; | ||
| if (w->cb_ref != NULL) { | ||
| napi_get_reference_value(env, w->cb_ref, &cb); | ||
| } | ||
| napi_get_undefined(env, &undef); | ||
| napi_create_int32(env, (int)status, &argv[0]); | ||
| if (cb != NULL) { | ||
| napi_call_function(env, undef, cb, 1, argv, NULL); | ||
| } | ||
| napi_delete_reference(env, w->buf_ref); | ||
| if (w->cb_ref != NULL) napi_delete_reference(env, w->cb_ref); | ||
| napi_delete_async_work(env, w->work); | ||
| free(w); | ||
| } | ||
|
|
||
| // queueWork(arrayBuffer, ms[, cb]) -> undefined | ||
| static napi_value queue_work(napi_env env, napi_callback_info info) { | ||
| size_t argc = 3; | ||
| napi_value argv[3]; | ||
| CHECK(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); | ||
| if (argc < 2) { | ||
| napi_throw_error(env, NULL, "expected (arrayBuffer, ms[, cb])"); | ||
| return NULL; | ||
| } | ||
|
|
||
| work_t *w = (work_t *)calloc(1, sizeof(*w)); | ||
|
|
||
| void *data = NULL; | ||
| size_t len = 0; | ||
| CHECK(env, napi_get_arraybuffer_info(env, argv[0], &data, &len)); | ||
| w->data = (unsigned char *)data; | ||
| w->len = len; | ||
|
|
||
| int32_t ms = 0; | ||
| CHECK(env, napi_get_value_int32(env, argv[1], &ms)); | ||
| w->sleep_ms = (unsigned)(ms < 0 ? 0 : ms); | ||
|
|
||
| CHECK(env, napi_create_reference(env, argv[0], 1, &w->buf_ref)); | ||
| if (argc >= 3) { | ||
| napi_valuetype t; | ||
| CHECK(env, napi_typeof(env, argv[2], &t)); | ||
| if (t == napi_function) { | ||
| CHECK(env, napi_create_reference(env, argv[2], 1, &w->cb_ref)); | ||
| } | ||
| } | ||
|
|
||
| napi_value name; | ||
| CHECK(env, napi_create_string_utf8(env, "test_async_work_worker_terminate", | ||
| NAPI_AUTO_LENGTH, &name)); | ||
| CHECK(env, napi_create_async_work(env, NULL, name, exec_cb, done_cb, w, | ||
| &w->work)); | ||
| CHECK(env, napi_queue_async_work(env, w->work)); | ||
|
|
||
| napi_value undef; | ||
| CHECK(env, napi_get_undefined(env, &undef)); | ||
| return undef; | ||
| } | ||
|
|
||
| NAPI_MODULE_INIT() { | ||
| napi_value fn; | ||
| CHECK(env, napi_create_function(env, "queueWork", NAPI_AUTO_LENGTH, | ||
| queue_work, NULL, &fn)); | ||
| CHECK(env, napi_set_named_property(env, exports, "queueWork", fn)); | ||
| return exports; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.