1 # Fix issues with abort on uncaught exception
2 # https://github.com/joyent/node/pull/8666
3 # https://github.com/joyent/node/issues/8631
4 # https://github.com/joyent/node/issues/8630
5 From fbff7054a47551387a99244e2cf0631f30406798 Mon Sep 17 00:00:00 2001
6 From: Trevor Norris <trev.norris@gmail.com>
7 Date: Tue, 18 Nov 2014 16:37:54 -0800
8 Subject: [PATCH] v8: add api for aborting on uncaught exception
10 Add v8::Isolate::SetAbortOnUncaughtException() so the user can be
11 notified when an uncaught exception has bubbled.
13 PR-URL: https://github.com/joyent/node/pull/8666
14 Reviewed-by: Trevor Norris <trev.norris@gmail.com>
16 include/v8.h | 11 +++++++++++
18 src/isolate.cc | 33 +++++++++++++++++++++++----------
19 src/isolate.h | 5 +++++
20 4 files changed, 44 insertions(+), 10 deletions(-)
22 diff --git a/include/v8.h b/include/v8.h
23 index 71a0d01..e229ed9 100644
26 @@ -2842,6 +2842,17 @@ class V8EXPORT Isolate {
27 static Isolate* GetCurrent();
30 + * Custom callback used by embedders to help V8 determine if it should abort
31 + * when it throws and no internal handler can catch the exception.
32 + * If FLAG_abort_on_uncaught_exception is true, then V8 will abort if either:
33 + * - no custom callback is set.
34 + * - the custom callback set returns true.
35 + * Otherwise it won't abort.
37 + typedef bool (*abort_on_uncaught_exception_t)();
38 + void SetAbortOnUncaughtException(abort_on_uncaught_exception_t callback);
41 * Methods below this point require holding a lock (using Locker) in
42 * a multi-threaded environment.
44 diff --git a/src/api.cc b/src/api.cc
45 index 96d564f..4b1aa67 100644
48 @@ -5550,6 +5550,11 @@ void Isolate::Enter() {
52 +void Isolate::SetAbortOnUncaughtException(
53 + abort_on_uncaught_exception_t callback) {
54 + i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
55 + isolate->SetAbortOnUncaughtException(callback);
58 void Isolate::Exit() {
59 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
60 --- v8-3.15.11.18/src/isolate.cc 2013-05-07 21:00:06.000000000 +0300
61 +++ v8-3.15.11.18/src/isolate.cc 2015-10-17 22:30:21.746915216 +0300
66 +static int fatal_exception_depth = 0;
68 void Isolate::DoThrow(Object* exception, MessageLocation* location) {
69 ASSERT(!has_pending_exception());
70 @@ -1250,6 +1250,28 @@
71 thread_local_top()->pending_message_start_pos_ = location->start_pos();
72 thread_local_top()->pending_message_end_pos_ = location->end_pos();
75 + // If the abort-on-uncaught-exception flag is specified, and if the
76 + // exception is not caught by JavaScript (even when an external handler is
78 + if (fatal_exception_depth == 0 &&
79 + FLAG_abort_on_uncaught_exception &&
80 + (report_exception || can_be_caught_externally)) {
81 + // If the embedder didn't specify a custom uncaught exception callback,
82 + // or if the custom callback determined that V8 should abort, then
84 + bool should_abort = !abort_on_uncaught_exception_callback_ ||
85 + abort_on_uncaught_exception_callback_();
87 + fatal_exception_depth++;
88 + // This flag is intended for use by JavaScript developers, so
89 + // print a user-friendly stack trace (not an internal one).
90 + fprintf(stderr, "%s\n\nFROM\n",
91 + *MessageHandler::GetLocalizedMessage(message_obj));
92 + PrintCurrentStackTrace(stderr);
96 } else if (location != NULL && !location->script().is_null()) {
97 // We are bootstrapping and caught an error where the location is set
98 // and we have a script for the location.
99 @@ -1339,6 +1347,10 @@ void Isolate::SetCaptureStackTraceForUncaughtExceptions(
100 stack_trace_for_uncaught_exceptions_options_ = options;
103 +void Isolate::SetAbortOnUncaughtException(
104 + v8::Isolate::abort_on_uncaught_exception_t callback) {
105 + abort_on_uncaught_exception_callback_ = callback;
108 bool Isolate::is_out_of_memory() {
109 if (has_pending_exception()) {
110 @@ -1534,7 +1546,8 @@ Isolate::Isolate()
112 context_exit_happened_(false),
113 deferred_handles_head_(NULL),
114 - optimizing_compiler_thread_(this) {
115 + optimizing_compiler_thread_(this),
116 + abort_on_uncaught_exception_callback_(NULL) {
117 TRACE_ISOLATE(constructor);
119 memset(isolate_addresses_, 0,
120 diff --git a/src/isolate.h b/src/isolate.h
121 index 2769ca7..8719aa1 100644
124 @@ -692,6 +692,9 @@ class Isolate {
126 StackTrace::StackTraceOptions options);
128 + typedef bool (*abort_on_uncaught_exception_t)();
129 + void SetAbortOnUncaughtException(abort_on_uncaught_exception_t callback);
131 // Tells whether the current context has experienced an out of memory
133 bool is_out_of_memory();
134 @@ -1292,6 +1295,8 @@ class Isolate {
135 DeferredHandles* deferred_handles_head_;
136 OptimizingCompilerThread optimizing_compiler_thread_;
138 + abort_on_uncaught_exception_t abort_on_uncaught_exception_callback_;
140 friend class ExecutionAccess;
141 friend class HandleScopeImplementer;
142 friend class IsolateInitializer;
143 --- v8-3.15.11.18/src/flag-definitions.h 2013-05-07 21:00:06.000000000 +0300
144 +++ v8-3.14.5.10/src/flag-definitions.h 2013-05-23 13:49:13.000000000 +0300
146 "Stack alingment in bytes in simulator (4 or 8, 8 is default)")
149 +DEFINE_bool(abort_on_uncaught_exception, false,
150 + "abort program (dump core) when an uncaught exception is thrown")
151 DEFINE_bool(trace_exception, false,
152 "print stack trace when throwing exceptions")
153 DEFINE_bool(preallocate_message_memory, false,