Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(105)

Side by Side Diff: Source/bindings/core/v8/V8Initializer.cpp

Issue 625943002: Catch uncaught promise rejections from V8 and log to console. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: addressed Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved. 2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 12 matching lines...) Expand all
23 * THE POSSIBILITY OF SUCH DAMAGE. 23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #include "config.h" 26 #include "config.h"
27 #include "bindings/core/v8/V8Initializer.h" 27 #include "bindings/core/v8/V8Initializer.h"
28 28
29 #include "bindings/core/v8/DOMWrapperWorld.h" 29 #include "bindings/core/v8/DOMWrapperWorld.h"
30 #include "bindings/core/v8/ScriptCallStackFactory.h" 30 #include "bindings/core/v8/ScriptCallStackFactory.h"
31 #include "bindings/core/v8/ScriptController.h" 31 #include "bindings/core/v8/ScriptController.h"
32 #include "bindings/core/v8/ScriptProfiler.h" 32 #include "bindings/core/v8/ScriptProfiler.h"
33 #include "bindings/core/v8/ScriptValue.h"
34 #include "bindings/core/v8/ScriptValueTraits.h"
33 #include "bindings/core/v8/V8Binding.h" 35 #include "bindings/core/v8/V8Binding.h"
34 #include "bindings/core/v8/V8DOMException.h" 36 #include "bindings/core/v8/V8DOMException.h"
35 #include "bindings/core/v8/V8ErrorEvent.h" 37 #include "bindings/core/v8/V8ErrorEvent.h"
36 #include "bindings/core/v8/V8ErrorHandler.h" 38 #include "bindings/core/v8/V8ErrorHandler.h"
37 #include "bindings/core/v8/V8GCController.h" 39 #include "bindings/core/v8/V8GCController.h"
38 #include "bindings/core/v8/V8History.h" 40 #include "bindings/core/v8/V8History.h"
39 #include "bindings/core/v8/V8Location.h" 41 #include "bindings/core/v8/V8Location.h"
40 #include "bindings/core/v8/V8PerContextData.h" 42 #include "bindings/core/v8/V8PerContextData.h"
41 #include "bindings/core/v8/V8Window.h" 43 #include "bindings/core/v8/V8Window.h"
42 #include "core/dom/Document.h" 44 #include "core/dom/Document.h"
43 #include "core/dom/ExceptionCode.h" 45 #include "core/dom/ExceptionCode.h"
46 #include "core/dom/Microtask.h"
44 #include "core/frame/ConsoleTypes.h" 47 #include "core/frame/ConsoleTypes.h"
45 #include "core/frame/LocalDOMWindow.h" 48 #include "core/frame/LocalDOMWindow.h"
46 #include "core/frame/LocalFrame.h" 49 #include "core/frame/LocalFrame.h"
47 #include "core/frame/csp/ContentSecurityPolicy.h" 50 #include "core/frame/csp/ContentSecurityPolicy.h"
51 #include "core/inspector/ScriptArguments.h"
48 #include "core/inspector/ScriptCallStack.h" 52 #include "core/inspector/ScriptCallStack.h"
49 #include "platform/EventDispatchForbiddenScope.h" 53 #include "platform/EventDispatchForbiddenScope.h"
50 #include "platform/TraceEvent.h" 54 #include "platform/TraceEvent.h"
51 #include "public/platform/Platform.h" 55 #include "public/platform/Platform.h"
52 #include "wtf/RefPtr.h" 56 #include "wtf/RefPtr.h"
53 #include "wtf/text/WTFString.h" 57 #include "wtf/text/WTFString.h"
54 #include <v8-debug.h> 58 #include <v8-debug.h>
55 59
56 namespace blink { 60 namespace blink {
57 61
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 } 146 }
143 147
144 if (scriptState->world().isPrivateScriptIsolatedWorld()) { 148 if (scriptState->world().isPrivateScriptIsolatedWorld()) {
145 // We allow a private script to dispatch error events even in a EventDis patchForbiddenScope scope. 149 // We allow a private script to dispatch error events even in a EventDis patchForbiddenScope scope.
146 // Without having this ability, it's hard to debug the private script be cause syntax errors 150 // Without having this ability, it's hard to debug the private script be cause syntax errors
147 // in the private script are not reported to console (the private script just crashes silently). 151 // in the private script are not reported to console (the private script just crashes silently).
148 // Allowing error events in private scripts is safe because error events don't propagate to 152 // Allowing error events in private scripts is safe because error events don't propagate to
149 // other isolated worlds (which means that the error events won't fire a ny event listeners 153 // other isolated worlds (which means that the error events won't fire a ny event listeners
150 // in user's scripts). 154 // in user's scripts).
151 EventDispatchForbiddenScope::AllowUserAgentEvents allowUserAgentEvents; 155 EventDispatchForbiddenScope::AllowUserAgentEvents allowUserAgentEvents;
152 enteredWindow->document()->reportException(event.release(), scriptId, ca llStack, corsStatus); 156 enteredWindow->document()->reportException(event.release(), scriptId, ca llStack, nullptr, corsStatus);
153 } else { 157 } else {
154 enteredWindow->document()->reportException(event.release(), scriptId, ca llStack, corsStatus); 158 enteredWindow->document()->reportException(event.release(), scriptId, ca llStack, nullptr, corsStatus);
155 } 159 }
156 } 160 }
157 161
162 namespace {
163
164 class PromiseRejectMessage {
165 public:
166 PromiseRejectMessage(const ScriptValue& promise)
167 : m_promise(promise)
168 {
169 }
170
171 ScriptValue m_promise;
172 };
173
174 } // namespace
175
176 typedef WillBeHeapHashMap<ScriptValue, OwnPtr<PromiseRejectMessage>, ScriptValue Hash, ScriptValueHashTraits> PromiseRejectMessageMap;
177 static bool firePromiseRejectMessagesCallbackScheduled = false;
178
179 static PromiseRejectMessageMap& promiseRejectMessageQueue()
180 {
181 DEFINE_STATIC_LOCAL(OwnPtrWillBePersistent<PromiseRejectMessageMap>, map, (a doptPtrWillBeNoop(new PromiseRejectMessageMap())));
182 return *map;
183 }
184
185 static void firePromiseRejectMessages()
pfeldman 2014/10/06 13:11:59 Lets kick it from WebKit upon end of task instead.
186 {
187 ASSERT(isMainThread());
188
189 firePromiseRejectMessagesCallbackScheduled = false;
190 v8::Isolate::GetCurrent()->RemoveCallCompletedCallback(&firePromiseRejectMes sages);
191
192 // FIXME: Send more data from V8.
193 int scriptId = 0;
194 AccessControlStatus corsStatus = SharableCrossOrigin;
195 RefPtrWillBeRawPtr<ScriptCallStack> callStack = nullptr;
196
197 PromiseRejectMessageMap& map = promiseRejectMessageQueue();
198 while (!map.isEmpty()) {
199 PromiseRejectMessageMap::iterator it = map.begin();
200 OwnPtr<PromiseRejectMessage> message = it->value.release();
201 map.remove(it);
202
203 ScriptState* scriptState = message->m_promise.scriptState();
204 if (scriptState->contextIsValid())
205 continue;
206 ScriptState::Scope scope(scriptState);
207
208 v8::Isolate* isolate = scriptState->isolate();
209 RefPtrWillBeRawPtr<ErrorEvent> event = ErrorEvent::createSanitizedError( &scriptState->world());
210 event->setUnsanitizedMessage("Unhandled promise rejection");
211
212 Vector<ScriptValue> args;
213 args.append(ScriptValue(scriptState, v8String(isolate, event->messageFor Console())));
214 args.append(message->m_promise);
215 RefPtrWillBeRawPtr<ScriptArguments> arguments = ScriptArguments::create( scriptState, args);
216
217 scriptState->executionContext()->reportException(event.release(), script Id, callStack, arguments.release(), corsStatus, true);
pfeldman 2014/10/06 13:11:59 Accessing frame console for main thread and captur
218 }
219 }
220
221 static void promiseRejectHandlerInMainThread(v8::Handle<v8::Promise> promise, v8 ::Handle<v8::Value> value, v8::PromiseRejectEvent event)
222 {
223 ASSERT(isMainThread());
224
225 // It's possible that promiseRejectHandlerInMainThread() is invoked while we 're initializing a window.
226 // In that half-baked situation, we don't have a valid context nor a valid w orld,
227 // so just return immediately.
228 if (DOMWrapperWorld::windowIsBeingInitialized())
229 return;
230
231 v8::Isolate* isolate = v8::Isolate::GetCurrent();
232 ScriptState* scriptState = ScriptState::current(isolate);
233 ScriptValue promiseValue(scriptState, promise);
234 PromiseRejectMessageMap& map = promiseRejectMessageQueue();
235
236 if (event == v8::kPromiseRejectWithNoHandler) {
237 ASSERT(!map.contains(promiseValue));
238 map.set(promiseValue, adoptPtr(new PromiseRejectMessage(promiseValue)));
239 } else if (event == v8::kPromiseHandlerAddedAfterReject) {
240 PromiseRejectMessageMap::iterator it = map.find(promiseValue);
241 if (it != map.end()) {
242 map.remove(it);
243 } else {
244 // FIXME: Report revoke message to console.
245 }
246 } else {
247 ASSERT_NOT_REACHED();
248 }
249
250 if (!firePromiseRejectMessagesCallbackScheduled && !map.isEmpty()) {
251 firePromiseRejectMessagesCallbackScheduled = true;
252 isolate->AddCallCompletedCallback(&firePromiseRejectMessages);
253 }
254 }
255
158 static void failedAccessCheckCallbackInMainThread(v8::Local<v8::Object> host, v8 ::AccessType type, v8::Local<v8::Value> data) 256 static void failedAccessCheckCallbackInMainThread(v8::Local<v8::Object> host, v8 ::AccessType type, v8::Local<v8::Value> data)
159 { 257 {
160 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 258 v8::Isolate* isolate = v8::Isolate::GetCurrent();
161 LocalFrame* target = findFrame(host, data, isolate); 259 LocalFrame* target = findFrame(host, data, isolate);
162 if (!target) 260 if (!target)
163 return; 261 return;
164 LocalDOMWindow* targetWindow = target->domWindow(); 262 LocalDOMWindow* targetWindow = target->domWindow();
165 263
166 // FIXME: We should modify V8 to pass in more contextual information (contex t, property, and object). 264 // FIXME: We should modify V8 to pass in more contextual information (contex t, property, and object).
167 ExceptionState exceptionState(ExceptionState::UnknownContext, 0, 0, isolate- >GetCurrentContext()->Global(), isolate); 265 ExceptionState exceptionState(ExceptionState::UnknownContext, 0, 0, isolate- >GetCurrentContext()->Global(), isolate);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 v8::Isolate* isolate = V8PerIsolateData::initialize(); 309 v8::Isolate* isolate = V8PerIsolateData::initialize();
212 310
213 initializeV8Common(isolate); 311 initializeV8Common(isolate);
214 312
215 v8::V8::SetFatalErrorHandler(reportFatalErrorInMainThread); 313 v8::V8::SetFatalErrorHandler(reportFatalErrorInMainThread);
216 v8::V8::AddMessageListener(messageHandlerInMainThread); 314 v8::V8::AddMessageListener(messageHandlerInMainThread);
217 v8::V8::SetFailedAccessCheckCallbackFunction(failedAccessCheckCallbackInMain Thread); 315 v8::V8::SetFailedAccessCheckCallbackFunction(failedAccessCheckCallbackInMain Thread);
218 v8::V8::SetAllowCodeGenerationFromStringsCallback(codeGenerationCheckCallbac kInMainThread); 316 v8::V8::SetAllowCodeGenerationFromStringsCallback(codeGenerationCheckCallbac kInMainThread);
219 317
220 isolate->SetEventLogger(timerTraceProfilerInMainThread); 318 isolate->SetEventLogger(timerTraceProfilerInMainThread);
319 isolate->SetPromiseRejectCallback(promiseRejectHandlerInMainThread);
221 320
222 ScriptProfiler::initialize(); 321 ScriptProfiler::initialize();
223 } 322 }
224 323
225 static void reportFatalErrorInWorker(const char* location, const char* message) 324 static void reportFatalErrorInWorker(const char* location, const char* message)
226 { 325 {
227 // FIXME: We temporarily deal with V8 internal error situations such as out- of-memory by crashing the worker. 326 // FIXME: We temporarily deal with V8 internal error situations such as out- of-memory by crashing the worker.
228 CRASH(); 327 CRASH();
229 } 328 }
230 329
(...skipping 14 matching lines...) Expand all
245 TOSTRING_VOID(V8StringResource<>, sourceURL, message->GetScriptOrigin(). ResourceName()); 344 TOSTRING_VOID(V8StringResource<>, sourceURL, message->GetScriptOrigin(). ResourceName());
246 int scriptId = message->GetScriptOrigin().ScriptID()->Value(); 345 int scriptId = message->GetScriptOrigin().ScriptID()->Value();
247 346
248 RefPtrWillBeRawPtr<ErrorEvent> event = ErrorEvent::create(errorMessage, sourceURL, message->GetLineNumber(), message->GetStartColumn() + 1, &DOMWrapperW orld::current(isolate)); 347 RefPtrWillBeRawPtr<ErrorEvent> event = ErrorEvent::create(errorMessage, sourceURL, message->GetLineNumber(), message->GetStartColumn() + 1, &DOMWrapperW orld::current(isolate));
249 AccessControlStatus corsStatus = message->IsSharedCrossOrigin() ? Sharab leCrossOrigin : NotSharableCrossOrigin; 348 AccessControlStatus corsStatus = message->IsSharedCrossOrigin() ? Sharab leCrossOrigin : NotSharableCrossOrigin;
250 349
251 // If execution termination has been triggered as part of constructing 350 // If execution termination has been triggered as part of constructing
252 // the error event from the v8::Message, quietly leave. 351 // the error event from the v8::Message, quietly leave.
253 if (!v8::V8::IsExecutionTerminating(isolate)) { 352 if (!v8::V8::IsExecutionTerminating(isolate)) {
254 V8ErrorHandler::storeExceptionOnErrorEventWrapper(event.get(), data, scriptState->context()->Global(), isolate); 353 V8ErrorHandler::storeExceptionOnErrorEventWrapper(event.get(), data, scriptState->context()->Global(), isolate);
255 context->reportException(event.release(), scriptId, nullptr, corsSta tus); 354 context->reportException(event.release(), scriptId, nullptr, nullptr , corsStatus);
256 } 355 }
257 } 356 }
258 357
259 isReportingException = false; 358 isReportingException = false;
260 } 359 }
261 360
262 static const int kWorkerMaxStackSize = 500 * 1024; 361 static const int kWorkerMaxStackSize = 500 * 1024;
263 362
264 void V8Initializer::initializeWorker(v8::Isolate* isolate) 363 void V8Initializer::initializeWorker(v8::Isolate* isolate)
265 { 364 {
266 initializeV8Common(isolate); 365 initializeV8Common(isolate);
267 366
268 v8::V8::AddMessageListener(messageHandlerInWorker); 367 v8::V8::AddMessageListener(messageHandlerInWorker);
269 v8::V8::SetFatalErrorHandler(reportFatalErrorInWorker); 368 v8::V8::SetFatalErrorHandler(reportFatalErrorInWorker);
270 369
271 uint32_t here; 370 uint32_t here;
272 isolate->SetStackLimit(reinterpret_cast<uintptr_t>(&here - kWorkerMaxStackSi ze / sizeof(uint32_t*))); 371 isolate->SetStackLimit(reinterpret_cast<uintptr_t>(&here - kWorkerMaxStackSi ze / sizeof(uint32_t*)));
273 } 372 }
274 373
275 } // namespace blink 374 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698