1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
|
/*
* Copyright (C) 2009 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "third_party/blink/renderer/platform/bindings/v8_per_isolate_data.h"
#include <algorithm>
#include <memory>
#include <utility>
#include "base/debug/crash_logging.h"
#include "base/feature_list.h"
#include "base/metrics/histogram_macros.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/default_tick_clock.h"
#include "base/trace_event/trace_event.h"
#include "gin/public/v8_idle_task_runner.h"
#include "partition_alloc/oom.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/web/blink.h"
#include "third_party/blink/renderer/platform/bindings/active_script_wrappable_base.h"
#include "third_party/blink/renderer/platform/bindings/dom_data_store.h"
#include "third_party/blink/renderer/platform/bindings/script_forbidden_scope.h"
#include "third_party/blink/renderer/platform/bindings/script_regexp.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/bindings/thread_debugger.h"
#include "third_party/blink/renderer/platform/bindings/v8_binding.h"
#include "third_party/blink/renderer/platform/bindings/v8_histogram_accumulator.h"
#include "third_party/blink/renderer/platform/bindings/v8_object_constructor.h"
#include "third_party/blink/renderer/platform/bindings/v8_private_property.h"
#include "third_party/blink/renderer/platform/bindings/v8_value_cache.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/heap/thread_state_scopes.h"
#include "third_party/blink/renderer/platform/scheduler/public/task_attribution_tracker.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread.h"
#include "third_party/blink/renderer/platform/wtf/leak_annotations.h"
namespace blink {
BASE_FEATURE(kTaskAttributionInfrastructureDisabledForTesting,
"TaskAttributionInfrastructureDisabledForTesting",
base::FEATURE_DISABLED_BY_DEFAULT);
namespace {
void AddCrashKey(v8::CrashKeyId id, const std::string& value) {
using base::debug::AllocateCrashKeyString;
using base::debug::CrashKeySize;
using base::debug::SetCrashKeyString;
switch (id) {
case v8::CrashKeyId::kIsolateAddress:
static auto* const isolate_address =
AllocateCrashKeyString("v8_isolate_address", CrashKeySize::Size32);
SetCrashKeyString(isolate_address, value);
break;
case v8::CrashKeyId::kReadonlySpaceFirstPageAddress:
static auto* const ro_space_firstpage_address = AllocateCrashKeyString(
"v8_ro_space_firstpage_address", CrashKeySize::Size32);
SetCrashKeyString(ro_space_firstpage_address, value);
break;
case v8::CrashKeyId::kMapSpaceFirstPageAddress:
static auto* const map_space_firstpage_address = AllocateCrashKeyString(
"v8_map_space_firstpage_address", CrashKeySize::Size32);
SetCrashKeyString(map_space_firstpage_address, value);
break;
case v8::CrashKeyId::kCodeSpaceFirstPageAddress:
static auto* const code_space_firstpage_address = AllocateCrashKeyString(
"v8_code_space_firstpage_address", CrashKeySize::Size32);
SetCrashKeyString(code_space_firstpage_address, value);
break;
case v8::CrashKeyId::kDumpType:
static auto* const dump_type =
AllocateCrashKeyString("dump-type", CrashKeySize::Size32);
SetCrashKeyString(dump_type, value);
break;
default:
// Doing nothing for new keys is a valid option. Having this case allows
// to introduce new CrashKeyId's without triggering a build break.
break;
}
}
V8PerIsolateData::TaskAttributionTrackerFactoryPtr
task_attribution_tracker_factory = nullptr;
} // namespace
static void BeforeCallEnteredCallback(v8::Isolate* isolate) {
CHECK(!ScriptForbiddenScope::IsScriptForbidden());
}
static bool AllowAtomicWaits(
V8PerIsolateData::V8ContextSnapshotMode v8_context_snapshot_mode) {
return !IsMainThread() ||
v8_context_snapshot_mode ==
V8PerIsolateData::V8ContextSnapshotMode::kTakeSnapshot;
}
V8PerIsolateData::V8PerIsolateData(
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
scoped_refptr<base::SingleThreadTaskRunner> user_visible_task_runner,
scoped_refptr<base::SingleThreadTaskRunner> best_effort_task_runner,
V8ContextSnapshotMode v8_context_snapshot_mode,
v8::CreateHistogramCallback create_histogram_callback,
v8::AddHistogramSampleCallback add_histogram_sample_callback,
std::unique_ptr<v8::CppHeap> cpp_heap)
: v8_context_snapshot_mode_(v8_context_snapshot_mode),
isolate_holder_(
std::move(task_runner),
gin::IsolateHolder::kSingleThread,
AllowAtomicWaits(v8_context_snapshot_mode)
? gin::IsolateHolder::kAllowAtomicsWait
: gin::IsolateHolder::kDisallowAtomicsWait,
IsMainThread() ? gin::IsolateHolder::IsolateType::kBlinkMainThread
: gin::IsolateHolder::IsolateType::kBlinkWorkerThread,
v8_context_snapshot_mode ==
V8PerIsolateData::V8ContextSnapshotMode::kTakeSnapshot
? gin::IsolateHolder::IsolateCreationMode::kCreateSnapshot
: gin::IsolateHolder::IsolateCreationMode::kNormal,
create_histogram_callback,
add_histogram_sample_callback,
std::move(user_visible_task_runner),
std::move(best_effort_task_runner),
std::move(cpp_heap)),
string_cache_(std::make_unique<StringCache>(GetIsolate())),
private_property_(std::make_unique<V8PrivateProperty>()),
constructor_mode_(ConstructorMode::kCreateNewObject),
runtime_call_stats_(base::DefaultTickClock::GetInstance()) {
if (v8_context_snapshot_mode == V8ContextSnapshotMode::kTakeSnapshot) {
// Snapshot should only execute on the main thread. SnapshotCreator enters
// the isolate, so we don't call Isolate::Enter() here.
CHECK(IsMainThread());
} else {
// FIXME: Remove once all v8::Isolate::GetCurrent() calls are gone.
GetIsolate()->Enter();
GetIsolate()->AddBeforeCallEnteredCallback(&BeforeCallEnteredCallback);
}
if (IsMainThread()) {
GetIsolate()->SetAddCrashKeyCallback(AddCrashKey);
main_world_ =
DOMWrapperWorld::Create(GetIsolate(), DOMWrapperWorld::WorldType::kMain,
/*is_default_world_of_isolate=*/true);
if (!base::FeatureList::IsEnabled(
kTaskAttributionInfrastructureDisabledForTesting)) {
CHECK(task_attribution_tracker_factory);
task_attribution_tracker_ =
task_attribution_tracker_factory(GetIsolate());
}
}
}
V8PerIsolateData::~V8PerIsolateData() = default;
v8::Isolate* V8PerIsolateData::Initialize(
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
scoped_refptr<base::SingleThreadTaskRunner> user_visible_task_runner,
scoped_refptr<base::SingleThreadTaskRunner> best_effort_task_runner,
V8ContextSnapshotMode context_mode,
v8::CreateHistogramCallback create_histogram_callback,
v8::AddHistogramSampleCallback add_histogram_sample_callback,
std::unique_ptr<v8::CppHeap> cpp_heap) {
TRACE_EVENT1("v8", "V8PerIsolateData::Initialize", "V8ContextSnapshotMode",
context_mode);
V8PerIsolateData* data = new V8PerIsolateData(
std::move(task_runner), std::move(user_visible_task_runner),
std::move(best_effort_task_runner), context_mode,
create_histogram_callback, add_histogram_sample_callback,
std::move(cpp_heap));
DCHECK(data);
v8::Isolate* isolate = data->GetIsolate();
isolate->SetData(gin::kEmbedderBlink, data);
return isolate;
}
void V8PerIsolateData::EnableIdleTasks(
v8::Isolate* isolate,
std::unique_ptr<gin::V8IdleTaskRunner> task_runner) {
From(isolate)->isolate_holder_.EnableIdleTasks(std::move(task_runner));
}
// willBeDestroyed() clear things that should be cleared before
// ThreadState::detach() gets called.
void V8PerIsolateData::WillBeDestroyed(v8::Isolate* isolate) {
V8PerIsolateData* data = From(isolate);
data->thread_debugger_.reset();
for (auto& item : data->user_data_) {
if (item) {
item->WillBeDestroyed();
}
}
data->ClearScriptRegexpContext();
ThreadState::Current()->DetachFromIsolate();
data->active_script_wrappable_manager_.Clear();
// Callbacks can be removed as they only cover single events (e.g. atomic
// pause) and they cannot get out of sync.
DCHECK_EQ(0u, data->gc_callback_depth_);
isolate->RemoveGCPrologueCallback(data->prologue_callback_);
isolate->RemoveGCEpilogueCallback(data->epilogue_callback_);
}
void V8PerIsolateData::SetGCCallbacks(
v8::Isolate* isolate,
v8::Isolate::GCCallback prologue_callback,
v8::Isolate::GCCallback epilogue_callback) {
prologue_callback_ = prologue_callback;
epilogue_callback_ = epilogue_callback;
isolate->AddGCPrologueCallback(prologue_callback_);
isolate->AddGCEpilogueCallback(epilogue_callback_);
}
// destroy() clear things that should be cleared after ThreadState::detach()
// gets called but before the Isolate exits.
void V8PerIsolateData::Destroy(v8::Isolate* isolate) {
isolate->RemoveBeforeCallEnteredCallback(&BeforeCallEnteredCallback);
V8PerIsolateData* data = From(isolate);
// Clear everything before exiting the Isolate.
if (data->script_regexp_script_state_) {
data->script_regexp_script_state_->DisposePerContextData();
}
data->private_property_.reset();
data->string_cache_->Dispose();
data->string_cache_.reset();
data->v8_template_map_for_main_world_.clear();
data->v8_template_map_for_non_main_worlds_.clear();
// FIXME: Remove once all v8::Isolate::GetCurrent() calls are gone.
isolate->Exit();
delete data;
}
v8::Local<v8::Template> V8PerIsolateData::FindV8Template(
const DOMWrapperWorld& world,
const void* key) {
auto& map = SelectV8TemplateMap(world);
auto result = map.find(key);
if (result != map.end())
return result->value.Get(GetIsolate());
return v8::Local<v8::Template>();
}
void V8PerIsolateData::AddV8Template(const DOMWrapperWorld& world,
const void* key,
v8::Local<v8::Template> value) {
auto& map = SelectV8TemplateMap(world);
auto result = map.insert(key, v8::Eternal<v8::Template>(GetIsolate(), value));
DCHECK(result.is_new_entry);
}
v8::MaybeLocal<v8::DictionaryTemplate>
V8PerIsolateData::FindV8DictionaryTemplate(const void* key) {
auto it = v8_dict_template_map_.find(key);
return it != v8_dict_template_map_.end()
? it->value.Get(GetIsolate())
: v8::MaybeLocal<v8::DictionaryTemplate>();
}
void V8PerIsolateData::AddV8DictionaryTemplate(
const void* key,
v8::Local<v8::DictionaryTemplate> value) {
auto result = v8_dict_template_map_.insert(
key, v8::Eternal<v8::DictionaryTemplate>(GetIsolate(), value));
DCHECK(result.is_new_entry);
}
bool V8PerIsolateData::HasInstance(const WrapperTypeInfo* wrapper_type_info,
v8::Local<v8::Value> untrusted_value) {
RUNTIME_CALL_TIMER_SCOPE(GetIsolate(),
RuntimeCallStats::CounterId::kHasInstance);
return HasInstance(wrapper_type_info, untrusted_value,
v8_template_map_for_main_world_) ||
HasInstance(wrapper_type_info, untrusted_value,
v8_template_map_for_non_main_worlds_);
}
bool V8PerIsolateData::HasInstance(const WrapperTypeInfo* wrapper_type_info,
v8::Local<v8::Value> untrusted_value,
const V8TemplateMap& map) {
auto result = map.find(wrapper_type_info);
if (result == map.end())
return false;
v8::Local<v8::Template> v8_template = result->value.Get(GetIsolate());
DCHECK(v8_template->IsFunctionTemplate());
return v8_template.As<v8::FunctionTemplate>()->HasInstance(untrusted_value);
}
bool V8PerIsolateData::HasInstanceOfUntrustedType(
const WrapperTypeInfo* untrusted_wrapper_type_info,
v8::Local<v8::Value> untrusted_value) {
RUNTIME_CALL_TIMER_SCOPE(GetIsolate(),
RuntimeCallStats::CounterId::kHasInstance);
return HasInstanceOfUntrustedType(untrusted_wrapper_type_info,
untrusted_value,
v8_template_map_for_main_world_) ||
HasInstanceOfUntrustedType(untrusted_wrapper_type_info,
untrusted_value,
v8_template_map_for_non_main_worlds_);
}
bool V8PerIsolateData::HasInstanceOfUntrustedType(
const WrapperTypeInfo* untrusted_wrapper_type_info,
v8::Local<v8::Value> untrusted_value,
const V8TemplateMap& map) {
auto result = map.find(untrusted_wrapper_type_info);
if (result == map.end())
return false;
v8::Local<v8::Template> v8_template = result->value.Get(GetIsolate());
if (!v8_template->IsFunctionTemplate())
return false;
return v8_template.As<v8::FunctionTemplate>()->HasInstance(untrusted_value);
}
V8PerIsolateData::V8TemplateMap& V8PerIsolateData::SelectV8TemplateMap(
const DOMWrapperWorld& world) {
return world.IsMainWorld() ? v8_template_map_for_main_world_
: v8_template_map_for_non_main_worlds_;
}
void V8PerIsolateData::ClearPersistentsForV8ContextSnapshot() {
v8_template_map_for_main_world_.clear();
v8_template_map_for_non_main_worlds_.clear();
eternal_name_cache_.clear();
private_property_.reset();
}
const base::span<const v8::Eternal<v8::Name>>
V8PerIsolateData::FindOrCreateEternalNameCache(
const void* lookup_key,
base::span<const std::string_view> names) {
auto it = eternal_name_cache_.find(lookup_key);
const Vector<v8::Eternal<v8::Name>>* vector = nullptr;
if (it == eternal_name_cache_.end()) [[unlikely]] {
v8::Isolate* isolate = GetIsolate();
Vector<v8::Eternal<v8::Name>> new_vector(
base::checked_cast<wtf_size_t>(names.size()));
std::ranges::transform(
names, new_vector.begin(), [isolate](std::string_view name) {
return v8::Eternal<v8::Name>(
isolate,
V8AtomicString(isolate, StringView(base::as_byte_span(name))));
});
vector = &eternal_name_cache_.Set(lookup_key, std::move(new_vector))
.stored_value->value;
} else {
vector = &it->value;
}
DCHECK_EQ(vector->size(), names.size());
return *vector;
}
v8::Local<v8::Context> V8PerIsolateData::EnsureScriptRegexpContext() {
if (!script_regexp_script_state_) {
LEAK_SANITIZER_DISABLED_SCOPE;
v8::Local<v8::Context> context(v8::Context::New(GetIsolate()));
script_regexp_script_state_ = ScriptState::Create(
context,
DOMWrapperWorld::Create(GetIsolate(),
DOMWrapperWorld::WorldType::kRegExp),
/* execution_context = */ nullptr);
}
return script_regexp_script_state_->GetContext();
}
void V8PerIsolateData::ClearScriptRegexpContext() {
if (script_regexp_script_state_) {
script_regexp_script_state_->DisposePerContextData();
script_regexp_script_state_->DissociateContext();
}
script_regexp_script_state_ = nullptr;
}
void V8PerIsolateData::SetThreadDebugger(
std::unique_ptr<ThreadDebugger> thread_debugger) {
DCHECK(!thread_debugger_);
thread_debugger_ = std::move(thread_debugger);
}
void V8PerIsolateData::SetPasswordRegexp(ScriptRegexp* password_regexp) {
password_regexp_ = password_regexp;
}
ScriptRegexp* V8PerIsolateData::GetPasswordRegexp() {
return password_regexp_;
}
void V8PerIsolateData::SetTaskAttributionTrackerFactory(
TaskAttributionTrackerFactoryPtr factory) {
CHECK(!task_attribution_tracker_factory);
CHECK(IsMainThread());
task_attribution_tracker_factory = factory;
}
void* CreateHistogram(const char* name, int min, int max, size_t buckets) {
// Each histogram has an implicit '0' bucket (for underflow), so we can always
// bump the minimum to 1.
DCHECK_LE(0, min);
min = std::max(1, min);
// For boolean histograms, always include an overflow bucket [2, infinity).
if (max == 1 && buckets == 2) {
max = 2;
buckets = 3;
}
const std::string histogram_name =
Platform::Current()->GetNameForHistogram(name);
base::HistogramBase* histogram = base::Histogram::FactoryGet(
histogram_name, min, max, static_cast<uint32_t>(buckets),
base::Histogram::kUmaTargetedHistogramFlag);
return V8HistogramAccumulator::GetInstance()->RegisterHistogram(
histogram, histogram_name);
}
void AddHistogramSample(void* hist, int sample) {
V8HistogramAccumulator::GetInstance()->AddSample(hist, sample);
}
} // namespace blink
|