-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
[WIP] node-api: fix data race and use-after-free in napi_threadsafe_function #55877
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
Open
mika-fischer
wants to merge
3
commits into
nodejs:main
Choose a base branch
from
mika-fischer:fix-55706
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+206
−52
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
80 changes: 80 additions & 0 deletions
80
test/node-api/test_threadsafe_function_shutdown/binding.cc
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,80 @@ | ||
#include <js_native_api.h> | ||
#include <node_api.h> | ||
#include <node_api_types.h> | ||
|
||
#include <cstdio> | ||
#include <cstdlib> | ||
#include <memory> | ||
#include <mutex> | ||
#include <shared_mutex> | ||
#include <thread> | ||
#include <type_traits> | ||
#include <utility> | ||
#include <vector> | ||
|
||
template <typename R, auto func, typename... Args> | ||
inline auto call(const char *name, Args &&...args) -> R { | ||
napi_status status; | ||
if constexpr (std::is_same_v<R, void>) { | ||
status = func(std::forward<Args>(args)...); | ||
if (status == napi_ok) { | ||
return; | ||
} | ||
} else { | ||
R ret; | ||
status = func(std::forward<Args>(args)..., &ret); | ||
if (status == napi_ok) { | ||
return ret; | ||
} | ||
} | ||
std::fprintf(stderr, "%s: %d\n", name, status); | ||
std::abort(); | ||
} | ||
|
||
#define NAPI_CALL(ret_type, func, ...) \ | ||
call<ret_type, func>(#func, ##__VA_ARGS__) | ||
|
||
void thread_func(napi_threadsafe_function tsfn) { | ||
fprintf(stderr, "thread_func: starting\n"); | ||
auto status = | ||
napi_call_threadsafe_function(tsfn, nullptr, napi_tsfn_blocking); | ||
while (status == napi_ok) { | ||
std::this_thread::sleep_for(std::chrono::milliseconds(1)); | ||
status = napi_call_threadsafe_function(tsfn, nullptr, napi_tsfn_blocking); | ||
} | ||
fprintf(stderr, "thread_func: Got status %d, exiting...\n", status); | ||
} | ||
|
||
void tsfn_callback(napi_env env, napi_value js_cb, void *ctx, void *data) { | ||
if (env == nullptr) { | ||
fprintf(stderr, "tsfn_callback: env=%p\n", env); | ||
} | ||
} | ||
|
||
void tsfn_finalize(napi_env env, void *finalize_data, void *finalize_hint) { | ||
fprintf(stderr, "tsfn_finalize: env=%p\n", env); | ||
} | ||
|
||
std::vector<std::jthread> threads; | ||
|
||
auto run(napi_env env, napi_callback_info info) -> napi_value { | ||
auto global = NAPI_CALL(napi_value, napi_get_global, env); | ||
auto undefined = NAPI_CALL(napi_value, napi_get_undefined, env); | ||
auto n_threads = 32; | ||
auto tsfn = | ||
NAPI_CALL(napi_threadsafe_function, napi_create_threadsafe_function, env, | ||
nullptr, global, undefined, 0, n_threads, nullptr, | ||
tsfn_finalize, nullptr, tsfn_callback); | ||
for (auto i = 0; i < n_threads; ++i) { | ||
threads.emplace_back([tsfn] { thread_func(tsfn); }); | ||
} | ||
NAPI_CALL(void, napi_unref_threadsafe_function, env, tsfn); | ||
return NAPI_CALL(napi_value, napi_get_undefined, env); | ||
} | ||
|
||
napi_value init(napi_env env, napi_value exports) { | ||
return NAPI_CALL(napi_value, napi_create_function, env, nullptr, 0, | ||
run, nullptr); | ||
} | ||
|
||
NAPI_MODULE(NODE_GYP_MODULE_NAME, init) |
11 changes: 11 additions & 0 deletions
11
test/node-api/test_threadsafe_function_shutdown/binding.gyp
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,11 @@ | ||
{ | ||
"targets": [ | ||
{ | ||
"target_name": "binding", | ||
"sources": ["binding.cc"], | ||
"cflags_cc": ["--std=c++20"], | ||
'cflags!': [ '-fno-exceptions', '-fno-rtti' ], | ||
'cflags_cc!': [ '-fno-exceptions', '-fno-rtti' ], | ||
} | ||
] | ||
} |
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,17 @@ | ||
'use strict'; | ||
|
||
const common = require('../../common'); | ||
const process = require('process') | ||
const assert = require('assert'); | ||
const { fork } = require('child_process'); | ||
const binding = require(`./build/${common.buildType}/binding`); | ||
|
||
if (process.argv[2] === 'child') { | ||
binding(); | ||
setTimeout(() => {}, 100); | ||
} else { | ||
const child = fork(__filename, ['child']); | ||
child.on('close', (code) => { | ||
assert.strictEqual(code, 0); | ||
}); | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please follow these guidelines for the enum names: https://google.github.io/styleguide/cppguide.html#Enumerator_Names
The Node.js guidelines https://github.com/nodejs/node/blob/main/doc/contributing/cpp-style-guide.md are based on the Google C++ Style Guide.