V8: Add API to report OOM to embedder.

This is a dependent CL for the blink/chromium side change in https://codereview.chromium.org/2130293003/

BUG=614440

Review-Url: https://codereview.chromium.org/2139873002
Cr-Commit-Position: refs/heads/master@{#37781}
This commit is contained in:
wfh 2016-07-14 12:41:01 -07:00 committed by Commit bot
parent 458bd3b9a3
commit bc44b1c627
4 changed files with 35 additions and 6 deletions

View File

@ -5016,6 +5016,7 @@ class V8_EXPORT ResourceConstraints {
typedef void (*FatalErrorCallback)(const char* location, const char* message);
typedef void (*OOMErrorCallback)(const char* location, bool is_heap_oom);
typedef void (*MessageCallback)(Local<Message> message, Local<Value> error);
@ -6329,6 +6330,9 @@ class V8_EXPORT Isolate {
/** Set the callback to invoke in case of fatal errors. */
void SetFatalErrorHandler(FatalErrorCallback that);
/** Set the callback to invoke in case of OOM errors. */
void SetOOMErrorHandler(OOMErrorCallback that);
/**
* Set the callback to invoke to check if code generation from
* strings should be allowed.
@ -8875,7 +8879,6 @@ void V8::SetFatalErrorHandler(FatalErrorCallback callback) {
isolate->SetFatalErrorHandler(callback);
}
void V8::RemoveGCPrologueCallback(GCCallback callback) {
Isolate* isolate = Isolate::GetCurrent();
isolate->RemoveGCPrologueCallback(

View File

@ -253,7 +253,7 @@ void i::FatalProcessOutOfMemory(const char* location) {
// When V8 cannot allocated memory FatalProcessOutOfMemory is called.
// The default fatal error handler is called and execution is stopped.
// The default OOM error handler is called and execution is stopped.
void i::V8::FatalProcessOutOfMemory(const char* location, bool is_heap_oom) {
i::Isolate* isolate = i::Isolate::Current();
char last_few_messages[Heap::kTraceRingBufferSize + 1];
@ -316,9 +316,7 @@ void i::V8::FatalProcessOutOfMemory(const char* location, bool is_heap_oom) {
PrintF("\n<--- Last few GCs --->\n%s\n", first_newline);
PrintF("\n<--- JS stacktrace --->\n%s\n", js_stacktrace);
}
Utils::ApiCheck(false, location, is_heap_oom
? "Allocation failed - JavaScript heap out of memory"
: "Allocation failed - process out of memory");
Utils::ReportOOMFailure(location, is_heap_oom);
// If the fatal error handler returns, we stop execution.
FATAL("API fatal error handler returned after process out of memory");
}
@ -327,7 +325,7 @@ void i::V8::FatalProcessOutOfMemory(const char* location, bool is_heap_oom) {
void Utils::ReportApiFailure(const char* location, const char* message) {
i::Isolate* isolate = i::Isolate::Current();
FatalErrorCallback callback = isolate->exception_behavior();
if (callback == NULL) {
if (callback == nullptr) {
base::OS::PrintError("\n#\n# Fatal error in %s\n# %s\n#\n\n", location,
message);
base::OS::Abort();
@ -337,6 +335,28 @@ void Utils::ReportApiFailure(const char* location, const char* message) {
isolate->SignalFatalError();
}
void Utils::ReportOOMFailure(const char* location, bool is_heap_oom) {
i::Isolate* isolate = i::Isolate::Current();
OOMErrorCallback oom_callback = isolate->oom_behavior();
if (oom_callback == nullptr) {
// TODO(wfh): Remove this fallback once Blink is setting OOM handler. See
// crbug.com/614440.
FatalErrorCallback fatal_callback = isolate->exception_behavior();
if (fatal_callback == nullptr) {
base::OS::PrintError("\n#\n# Fatal %s OOM in %s\n#\n\n",
is_heap_oom ? "javascript" : "process", location);
base::OS::Abort();
} else {
fatal_callback(location,
is_heap_oom
? "Allocation failed - JavaScript heap out of memory"
: "Allocation failed - process out of memory");
}
} else {
oom_callback(location, is_heap_oom);
}
isolate->SignalFatalError();
}
static inline bool IsExecutionTerminatingCheck(i::Isolate* isolate) {
if (isolate->has_scheduled_exception()) {
@ -7915,6 +7935,10 @@ void Isolate::SetFatalErrorHandler(FatalErrorCallback that) {
isolate->set_exception_behavior(that);
}
void Isolate::SetOOMErrorHandler(OOMErrorCallback that) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
isolate->set_oom_behavior(that);
}
void Isolate::SetAllowCodeGenerationFromStringsCallback(
AllowCodeGenerationFromStringsCallback callback) {

View File

@ -184,6 +184,7 @@ class Utils {
if (!condition) Utils::ReportApiFailure(location, message);
return condition;
}
static void ReportOOMFailure(const char* location, bool is_heap_oom);
static Local<FunctionTemplate> ToFunctionTemplate(NeanderObject obj);
static Local<ObjectTemplate> ToObjectTemplate(NeanderObject obj);

View File

@ -398,6 +398,7 @@ typedef List<HeapObject*> DebugObjectCache;
#define ISOLATE_INIT_LIST(V) \
/* Assembler state. */ \
V(FatalErrorCallback, exception_behavior, nullptr) \
V(OOMErrorCallback, oom_behavior, nullptr) \
V(LogEventCallback, event_logger, nullptr) \
V(AllowCodeGenerationFromStringsCallback, allow_code_gen_callback, nullptr) \
V(ExternalReferenceRedirectorPointer*, external_reference_redirector, \