-
Notifications
You must be signed in to change notification settings - Fork 461
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
src: fix a crashing issue in Error::ThrowAsJavaScriptException via NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS #975
Closed
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
788df62
src: fix a crashing issue in Error::ThrowAsJavaScriptException
5382c4a
src,doc: add compile option
KevinEady a089697
Address review comments
KevinEady 0187fec
Move compile option back to gyp file
KevinEady 3f291f7
test: Move test to separate target
KevinEady 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 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 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 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 |
---|---|---|
|
@@ -2386,12 +2386,42 @@ inline const std::string& Error::Message() const NAPI_NOEXCEPT { | |
inline void Error::ThrowAsJavaScriptException() const { | ||
HandleScope scope(_env); | ||
if (!IsEmpty()) { | ||
|
||
#ifdef NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS | ||
bool pendingException = false; | ||
|
||
// check if there is already a pending exception. If so don't try to throw a | ||
// new one as that is not allowed/possible | ||
napi_status status = napi_is_exception_pending(_env, &pendingException); | ||
|
||
if ((status == napi_ok) && (pendingException == false)) { | ||
// We intentionally don't use `NAPI_THROW_*` macros here to ensure | ||
// that there is no possible recursion as `ThrowAsJavaScriptException` | ||
// is part of `NAPI_THROW_*` macro definition for noexcept. | ||
|
||
status = napi_throw(_env, Value()); | ||
|
||
if (status == napi_pending_exception) { | ||
// The environment must be terminating as we checked earlier and there | ||
// was no pending exception. In this case continuing will result | ||
// in a fatal error and there is nothing the author has done incorrectly | ||
// in their code that is worth flagging through a fatal error | ||
return; | ||
} | ||
} else { | ||
status = napi_pending_exception; | ||
} | ||
#else | ||
// We intentionally don't use `NAPI_THROW_*` macros here to ensure | ||
// that there is no possible recursion as `ThrowAsJavaScriptException` | ||
// is part of `NAPI_THROW_*` macro definition for noexcept. | ||
|
||
napi_status status = napi_throw(_env, Value()); | ||
#endif | ||
|
||
if (status == napi_pending_exception) { | ||
// The environment could be terminating. | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this block should be added here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed. |
||
|
||
#ifdef NAPI_CPP_EXCEPTIONS | ||
if (status != napi_ok) { | ||
|
This file contains 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 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,88 @@ | ||
'use strict'; | ||
const buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
|
||
// These tests ensure that Error types can be used in a terminating | ||
// environment without triggering any fatal errors. | ||
|
||
if (process.argv[2] === 'runInChildProcess') { | ||
const binding_path = process.argv[3]; | ||
const index_for_test_case = Number(process.argv[4]); | ||
|
||
const binding = require(binding_path); | ||
|
||
// Use C++ promises to ensure the worker thread is terminated right | ||
// before running the testable code in the binding. | ||
|
||
binding.error.resetPromises() | ||
|
||
const { Worker } = require('worker_threads'); | ||
|
||
const worker = new Worker( | ||
__filename, | ||
{ | ||
argv: [ | ||
'runInWorkerThread', | ||
binding_path, | ||
index_for_test_case, | ||
] | ||
} | ||
); | ||
|
||
binding.error.waitForWorkerThread() | ||
|
||
worker.terminate(); | ||
|
||
binding.error.releaseWorkerThread() | ||
|
||
return; | ||
} | ||
|
||
if (process.argv[2] === 'runInWorkerThread') { | ||
const binding_path = process.argv[3]; | ||
const index_for_test_case = Number(process.argv[4]); | ||
|
||
const binding = require(binding_path); | ||
|
||
switch (index_for_test_case) { | ||
case 0: | ||
binding.error.throwJSError('test', true); | ||
break; | ||
case 1: | ||
binding.error.throwTypeError('test', true); | ||
break; | ||
case 2: | ||
binding.error.throwRangeError('test', true); | ||
break; | ||
case 3: | ||
binding.error.throwDefaultError(false, true); | ||
break; | ||
case 4: | ||
binding.error.throwDefaultError(true, true); | ||
break; | ||
default: assert.fail('Invalid index'); | ||
} | ||
|
||
assert.fail('This should not be reachable'); | ||
} | ||
|
||
test(`./build/${buildType}/binding.node`); | ||
test(`./build/${buildType}/binding_noexcept.node`); | ||
|
||
function test(bindingPath) { | ||
const number_of_test_cases = 5; | ||
|
||
for (let i = 0; i < number_of_test_cases; ++i) { | ||
const child_process = require('./napi_child').spawnSync( | ||
process.execPath, | ||
[ | ||
__filename, | ||
'runInChildProcess', | ||
bindingPath, | ||
i, | ||
] | ||
); | ||
|
||
assert(child_process.status === 0, `Test case ${i} failed`); | ||
} | ||
} |
This file contains 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.
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.
This should not be here by default right ?
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.
Hi @mhdawson ,
So this relates to my comment on #902 (comment) where:
You can see in commit a089697 the change to move the setting from
binding.gyp
to#define
and now all of the builds fail.I do not think it is possible to have a per-compilation unit setting for this feature, because the
Error::ThrowAsJavaScriptException
is a compiled, exported symbol:So, when the first compilation unit uses it, it will be compiled with whatever preprocessor definitions are currently in scope.