Commit fd4813bb authored by Maya Lekova's avatar Maya Lekova Committed by Commit Bot

Refactor JavaScript execution scopes to not allocate

This CL introduces a new internal class PerIsolateAssertSwitch which
gives a static Allow/Disallow interface to be used from within classes
such as DisallowJavascriptExecutionScope without the need for slow heap
allocations.

Bug: chromium:1155348
Change-Id: I66cd8377b5d9c43510165cd7b9a7f5ccdaf45c18
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2617086
Auto-Submit: Maya Lekova <mslekova@chromium.org>
Commit-Queue: Toon Verwaest <verwaest@chromium.org>
Reviewed-by: 's avatarToon Verwaest <verwaest@chromium.org>
Reviewed-by: 's avatarLeszek Swirski <leszeks@chromium.org>
Cr-Commit-Position: refs/heads/master@{#72273}
parent 19b7ff41
......@@ -8490,7 +8490,11 @@ class V8_EXPORT Isolate {
private:
OnFailure on_failure_;
void* internal_;
Isolate* isolate_;
bool was_execution_allowed_assert_;
bool was_execution_allowed_throws_;
bool was_execution_allowed_dump_;
};
/**
......@@ -8508,9 +8512,10 @@ class V8_EXPORT Isolate {
const AllowJavascriptExecutionScope&) = delete;
private:
void* internal_throws_;
void* internal_assert_;
void* internal_dump_;
Isolate* isolate_;
bool was_execution_allowed_assert_;
bool was_execution_allowed_throws_;
bool was_execution_allowed_dump_;
};
/**
......
......@@ -8674,21 +8674,20 @@ void Isolate::SetPrepareStackTraceCallback(PrepareStackTraceCallback callback) {
Isolate::DisallowJavascriptExecutionScope::DisallowJavascriptExecutionScope(
Isolate* isolate,
Isolate::DisallowJavascriptExecutionScope::OnFailure on_failure)
: on_failure_(on_failure) {
: on_failure_(on_failure), isolate_(isolate) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
switch (on_failure_) {
case CRASH_ON_FAILURE:
internal_ = reinterpret_cast<void*>(
new i::DisallowJavascriptExecution(i_isolate));
i::DisallowJavascriptExecution::Open(i_isolate,
&was_execution_allowed_assert_);
break;
case THROW_ON_FAILURE:
DCHECK_EQ(THROW_ON_FAILURE, on_failure);
internal_ =
reinterpret_cast<void*>(new i::ThrowOnJavascriptExecution(i_isolate));
i::ThrowOnJavascriptExecution::Open(i_isolate,
&was_execution_allowed_throws_);
break;
case DUMP_ON_FAILURE:
internal_ =
reinterpret_cast<void*>(new i::DumpOnJavascriptExecution(i_isolate));
i::DumpOnJavascriptExecution::Open(i_isolate,
&was_execution_allowed_dump_);
break;
default:
UNREACHABLE();
......@@ -8696,15 +8695,19 @@ Isolate::DisallowJavascriptExecutionScope::DisallowJavascriptExecutionScope(
}
Isolate::DisallowJavascriptExecutionScope::~DisallowJavascriptExecutionScope() {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate_);
switch (on_failure_) {
case CRASH_ON_FAILURE:
delete reinterpret_cast<i::DisallowJavascriptExecution*>(internal_);
i::DisallowJavascriptExecution::Close(i_isolate,
was_execution_allowed_assert_);
break;
case THROW_ON_FAILURE:
delete reinterpret_cast<i::ThrowOnJavascriptExecution*>(internal_);
i::ThrowOnJavascriptExecution::Close(i_isolate,
was_execution_allowed_throws_);
break;
case DUMP_ON_FAILURE:
delete reinterpret_cast<i::DumpOnJavascriptExecution*>(internal_);
i::DumpOnJavascriptExecution::Close(i_isolate,
was_execution_allowed_dump_);
break;
default:
UNREACHABLE();
......@@ -8712,20 +8715,21 @@ Isolate::DisallowJavascriptExecutionScope::~DisallowJavascriptExecutionScope() {
}
Isolate::AllowJavascriptExecutionScope::AllowJavascriptExecutionScope(
Isolate* isolate) {
Isolate* isolate)
: isolate_(isolate) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
internal_assert_ =
reinterpret_cast<void*>(new i::AllowJavascriptExecution(i_isolate));
internal_throws_ =
reinterpret_cast<void*>(new i::NoThrowOnJavascriptExecution(i_isolate));
internal_dump_ =
reinterpret_cast<void*>(new i::NoDumpOnJavascriptExecution(i_isolate));
i::AllowJavascriptExecution::Open(i_isolate, &was_execution_allowed_assert_);
i::NoThrowOnJavascriptExecution::Open(i_isolate,
&was_execution_allowed_throws_);
i::NoDumpOnJavascriptExecution::Open(i_isolate, &was_execution_allowed_dump_);
}
Isolate::AllowJavascriptExecutionScope::~AllowJavascriptExecutionScope() {
delete reinterpret_cast<i::AllowJavascriptExecution*>(internal_assert_);
delete reinterpret_cast<i::NoThrowOnJavascriptExecution*>(internal_throws_);
delete reinterpret_cast<i::NoDumpOnJavascriptExecution*>(internal_dump_);
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate_);
i::AllowJavascriptExecution::Close(i_isolate, was_execution_allowed_assert_);
i::NoThrowOnJavascriptExecution::Close(i_isolate,
was_execution_allowed_throws_);
i::NoDumpOnJavascriptExecution::Close(i_isolate, was_execution_allowed_dump_);
}
Isolate::SuppressMicrotaskExecutionScope::SuppressMicrotaskExecutionScope(
......
......@@ -69,6 +69,27 @@ bool PerIsolateAssertScope<kType, kAllow>::IsAllowed(Isolate* isolate) {
return PerIsolateDataBit<kType>::decode(isolate->per_isolate_assert_data());
}
// static
template <PerIsolateAssertType kType, bool kAllow>
void PerIsolateAssertScope<kType, kAllow>::Open(Isolate* isolate,
bool* was_execution_allowed) {
DCHECK_NOT_NULL(isolate);
DCHECK_NOT_NULL(was_execution_allowed);
uint32_t old_data = isolate->per_isolate_assert_data();
*was_execution_allowed = PerIsolateDataBit<kType>::decode(old_data);
isolate->set_per_isolate_assert_data(
PerIsolateDataBit<kType>::update(old_data, kAllow));
}
// static
template <PerIsolateAssertType kType, bool kAllow>
void PerIsolateAssertScope<kType, kAllow>::Close(Isolate* isolate,
bool was_execution_allowed) {
DCHECK_NOT_NULL(isolate);
uint32_t old_data = isolate->per_isolate_assert_data();
isolate->set_per_isolate_assert_data(
PerIsolateDataBit<kType>::update(old_data, was_execution_allowed));
}
// -----------------------------------------------------------------------------
// Instantiations.
......
......@@ -67,6 +67,11 @@ class V8_NODISCARD PerIsolateAssertScope {
static bool IsAllowed(Isolate* isolate);
V8_EXPORT_PRIVATE static void Open(Isolate* isolate,
bool* was_execution_allowed);
V8_EXPORT_PRIVATE static void Close(Isolate* isolate,
bool was_execution_allowed);
private:
Isolate* isolate_;
uint32_t old_data_;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment