Skip to content

Commit 8b50e95

Browse files
Julien Gillijasnell
Julien Gilli
authored andcommitted
deps: backport 1ee712a from V8 upstream
Backport 1ee712ab8687e5f4dec93d45da068d37d28feb8b from V8 upstream. Original commit message: Add SetAbortOnUncaughtExceptionCallback API The --abort-on-uncaught-exception command line switch makes Isolate::Throw abort if the error being thrown cannot be caught by a try/catch block. Embedders may want to use other mechanisms than try/catch blocks to handle uncaught exceptions. For instance, Node.js has "domain" objects that have error handlers that can handle uncaught exception like following: var d = domain.create(); d.on('error', function onError(err) { console.log('Handling error'); }); d.run(function() { throw new Error("boom"); }); These error handlers are called by isolates' message listeners. If --abort-on-uncaught-exception is *not* used, the isolate's message listener will be called, which will in turn call the domain's error handler. The process will output 'Handling error' and will exit successfully (not due to an uncaught exception). This is the behavior that Node.js users expect. However, if --abort-on-uncaught-exception is used and when throwing an error within a domain that has an error handler, the process will abort and the domain's error handler will not be called. This is not the behavior that Node.js users expect. Having a SetAbortOnUncaughtExceptionCallback API allows embedders to determine when it's not appropriate to abort and instead handle the exception via the isolate's message listener. In the example above, Node.js would set a custom callback with SetAbortOnUncaughtExceptionCallback that would be implemented as following (the sample code has been simplified to remove what's not relevant to this change): bool ShouldAbortOnUncaughtException(Isolate* isolate) { return !IsDomainActive(); } Now when --abort-on-uncaught-exception is used, Isolate::Throw would call that callback and determine that it should not abort if a domain with an error handler is active. Instead, the isolate's message listener would be called and the error would be handled by the domain's error handler. I believe this can also be useful for other embedders. BUG= [email protected] Review URL: https://codereview.chromium.org/1375933003 Cr-Commit-Position: refs/heads/master@{#31111} PR: #3036 PR-URL: #3036 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 74f4435 commit 8b50e95

File tree

5 files changed

+79
-7
lines changed

5 files changed

+79
-7
lines changed

deps/v8/include/v8.h

+13
Original file line numberDiff line numberDiff line change
@@ -5380,6 +5380,19 @@ class V8_EXPORT Isolate {
53805380
*/
53815381
static Isolate* GetCurrent();
53825382

5383+
/**
5384+
* Custom callback used by embedders to help V8 determine if it should abort
5385+
* when it throws and no internal handler is predicted to catch the
5386+
* exception. If --abort-on-uncaught-exception is used on the command line,
5387+
* then V8 will abort if either:
5388+
* - no custom callback is set.
5389+
* - the custom callback set returns true.
5390+
* Otherwise, the custom callback will not be called and V8 will not abort.
5391+
*/
5392+
typedef bool (*AbortOnUncaughtExceptionCallback)(Isolate*);
5393+
void SetAbortOnUncaughtExceptionCallback(
5394+
AbortOnUncaughtExceptionCallback callback);
5395+
53835396
/**
53845397
* Methods below this point require holding a lock (using Locker) in
53855398
* a multi-threaded environment.

deps/v8/src/api.cc

+7
Original file line numberDiff line numberDiff line change
@@ -7176,6 +7176,13 @@ void Isolate::Exit() {
71767176
}
71777177

71787178

7179+
void Isolate::SetAbortOnUncaughtExceptionCallback(
7180+
AbortOnUncaughtExceptionCallback callback) {
7181+
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7182+
isolate->SetAbortOnUncaughtExceptionCallback(callback);
7183+
}
7184+
7185+
71797186
Isolate::DisallowJavascriptExecutionScope::DisallowJavascriptExecutionScope(
71807187
Isolate* isolate,
71817188
Isolate::DisallowJavascriptExecutionScope::OnFailure on_failure)

deps/v8/src/isolate.cc

+22-7
Original file line numberDiff line numberDiff line change
@@ -1013,13 +1013,21 @@ Object* Isolate::Throw(Object* exception, MessageLocation* location) {
10131013
Handle<Object> message_obj = CreateMessage(exception_handle, location);
10141014
thread_local_top()->pending_message_obj_ = *message_obj;
10151015

1016-
// If the abort-on-uncaught-exception flag is specified, abort on any
1017-
// exception not caught by JavaScript, even when an external handler is
1018-
// present. This flag is intended for use by JavaScript developers, so
1019-
// print a user-friendly stack trace (not an internal one).
1016+
// For any exception not caught by JavaScript, even when an external
1017+
// handler is present:
1018+
// If the abort-on-uncaught-exception flag is specified, and if the
1019+
// embedder didn't specify a custom uncaught exception callback,
1020+
// or if the custom callback determined that V8 should abort, then
1021+
// abort.
10201022
if (FLAG_abort_on_uncaught_exception &&
1021-
PredictExceptionCatcher() != CAUGHT_BY_JAVASCRIPT) {
1022-
FLAG_abort_on_uncaught_exception = false; // Prevent endless recursion.
1023+
PredictExceptionCatcher() != CAUGHT_BY_JAVASCRIPT &&
1024+
(!abort_on_uncaught_exception_callback_ ||
1025+
abort_on_uncaught_exception_callback_(
1026+
reinterpret_cast<v8::Isolate*>(this)))) {
1027+
// Prevent endless recursion.
1028+
FLAG_abort_on_uncaught_exception = false;
1029+
// This flag is intended for use by JavaScript developers, so
1030+
// print a user-friendly stack trace (not an internal one).
10231031
PrintF(stderr, "%s\n\nFROM\n",
10241032
MessageHandler::GetLocalizedMessage(this, message_obj).get());
10251033
PrintCurrentStackTrace(stderr);
@@ -1612,6 +1620,12 @@ void Isolate::SetCaptureStackTraceForUncaughtExceptions(
16121620
}
16131621

16141622

1623+
void Isolate::SetAbortOnUncaughtExceptionCallback(
1624+
v8::Isolate::AbortOnUncaughtExceptionCallback callback) {
1625+
abort_on_uncaught_exception_callback_ = callback;
1626+
}
1627+
1628+
16151629
Handle<Context> Isolate::native_context() {
16161630
return handle(context()->native_context());
16171631
}
@@ -1782,7 +1796,8 @@ Isolate::Isolate(bool enable_serializer)
17821796
next_unique_sfi_id_(0),
17831797
#endif
17841798
use_counter_callback_(NULL),
1785-
basic_block_profiler_(NULL) {
1799+
basic_block_profiler_(NULL),
1800+
abort_on_uncaught_exception_callback_(NULL) {
17861801
{
17871802
base::LockGuard<base::Mutex> lock_guard(thread_data_table_mutex_.Pointer());
17881803
CHECK(thread_data_table_);

deps/v8/src/isolate.h

+6
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,9 @@ class Isolate {
690690
int frame_limit,
691691
StackTrace::StackTraceOptions options);
692692

693+
void SetAbortOnUncaughtExceptionCallback(
694+
v8::Isolate::AbortOnUncaughtExceptionCallback callback);
695+
693696
enum PrintStackMode { kPrintStackConcise, kPrintStackVerbose };
694697
void PrintCurrentStackTrace(FILE* out);
695698
void PrintStack(StringStream* accumulator,
@@ -1363,6 +1366,9 @@ class Isolate {
13631366

13641367
v8::ArrayBuffer::Allocator* array_buffer_allocator_;
13651368

1369+
v8::Isolate::AbortOnUncaughtExceptionCallback
1370+
abort_on_uncaught_exception_callback_;
1371+
13661372
friend class ExecutionAccess;
13671373
friend class HandleScopeImplementer;
13681374
friend class OptimizingCompileDispatcher;

deps/v8/test/cctest/test-api.cc

+31
Original file line numberDiff line numberDiff line change
@@ -21880,3 +21880,34 @@ TEST(CompatibleReceiverCheckOnCachedICHandler) {
2188021880
"result;\n",
2188121881
0);
2188221882
}
21883+
21884+
21885+
static int nb_uncaught_exception_callback_calls = 0;
21886+
21887+
21888+
bool NoAbortOnUncaughtException(v8::Isolate* isolate) {
21889+
++nb_uncaught_exception_callback_calls;
21890+
return false;
21891+
}
21892+
21893+
21894+
TEST(AbortOnUncaughtExceptionNoAbort) {
21895+
v8::Isolate* isolate = CcTest::isolate();
21896+
v8::HandleScope handle_scope(isolate);
21897+
v8::Handle<v8::ObjectTemplate> global_template =
21898+
v8::ObjectTemplate::New(isolate);
21899+
LocalContext env(NULL, global_template);
21900+
21901+
i::FLAG_abort_on_uncaught_exception = true;
21902+
isolate->SetAbortOnUncaughtExceptionCallback(NoAbortOnUncaughtException);
21903+
21904+
CompileRun("function boom() { throw new Error(\"boom\") }");
21905+
21906+
v8::Local<v8::Object> global_object = env->Global();
21907+
v8::Local<v8::Function> foo =
21908+
v8::Local<v8::Function>::Cast(global_object->Get(v8_str("boom")));
21909+
21910+
foo->Call(global_object, 0, NULL);
21911+
21912+
CHECK_EQ(1, nb_uncaught_exception_callback_calls);
21913+
}

0 commit comments

Comments
 (0)