From 465f778309be48b3511a3d9178e22ecf0577f181 Mon Sep 17 00:00:00 2001 From: Reece Wilson Date: Mon, 6 Feb 2023 20:54:16 +0000 Subject: [PATCH] [+] Safepoint parking apis [+] Noted extension APIs (private) [+] IsolateSafepoint::NotifyParkEx [+] IsolateSafepoint::NotifyUnpark [*] Continued hacky bug fix as """described""" below MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------------------------------------------------------------------------------------------- Last aurora commit: 7c599206 continued """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" # src/heap/safepoint.cc ----------- [-] (USE AFTER FREE) quick hack: removing a mutex guard on shared RemoveClient to temporarily mitigate a crash on deinit ----------- well, this is fucking dumb. i think someone at google also figured out this can lead to crashing. their solution: remove the mutex with a call to AssertActive. considering my issue was related to a dead context with everything else alive, i dont want to find out what that AssertActive is doing. reverting v8 change. """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 3172b30fe49a35155790755e29a8f55f69f9eba8 - Dominik Inführ, and Michael Lippautz Fuck safety for now. The entire file will be different in 3 months time, anyway. I just want my software to actually fucking work. --- src/api/api.cc | 30 +++++++++++++++++++++++++++--- src/heap/safepoint.cc | 17 +++++++++++++++++ src/heap/safepoint.h | 28 +++++++++++++++++----------- 3 files changed, 61 insertions(+), 14 deletions(-) diff --git a/src/api/api.cc b/src/api/api.cc index 07e521aeac..52f8dea72b 100644 --- a/src/api/api.cc +++ b/src/api/api.cc @@ -1127,20 +1127,41 @@ bool Data::IsFunctionTemplate() const { bool Data::IsContext() const { return Utils::OpenHandle(this)->IsContext(); } -size_t GetCPUFeatureMask() { +// Reece: +V8_EXPORT size_t GetCPUFeatureMask() { return internal::CpuFeatures::SupportedFeatures(); } -size_t GetCPUFeatureMaskRescan() { +// Reece: +V8_EXPORT size_t GetCPUFeatureMaskRescan() { internal::CpuFeatures::initialized_ = 0; internal::CpuFeatures::supported_ = 0; return internal::CpuFeatures::SupportedFeatures(); } -void SetCPUFeatureMask(size_t features) { +// Reece: +V8_EXPORT void SetCPUFeatureMask(size_t features) { internal::CpuFeatures::supported_ = features; } +// Reece: +V8_EXPORT void Safepoint(v8::Isolate* pIsolate) { + reinterpret_cast(pIsolate) + ->heap() + ->main_thread_local_heap() + ->Safepoint(); +} + +// Reece: +V8_EXPORT void SafepointParkIsolate(v8::Isolate* pIsolate) { + reinterpret_cast(pIsolate)->heap()->safepoint()->NotifyParkEx(); +} + +// Reece: +V8_EXPORT void SafepointUnparkIsolate(v8::Isolate* pIsolate) { + reinterpret_cast(pIsolate)->heap()->safepoint()->NotifyUnpark(); +} + void Context::Enter() { i::DisallowGarbageCollection no_gc; i::Context env = *Utils::OpenHandle(this); @@ -2703,6 +2724,7 @@ MaybeLocal ScriptCompiler::CompileUnboundScript( return CompileUnboundInternal(v8_isolate, source, options, no_cache_reason); } +// Reece: MaybeLocal ScriptCompiler::EvaluateGlobal( v8::Isolate* isolate, v8::Local source, bool repl) { @@ -5445,6 +5467,7 @@ MaybeLocal Function::NewInstanceWithSideEffectType( RETURN_ESCAPED(result); } +// Reece: MaybeLocal Function::Bind(v8::Local that, v8::Local bound_args) { i::Handle self; @@ -6053,6 +6076,7 @@ bool v8::String::IsExternalOneByte() const { return false; } +// Reece: v8::Local v8::String::GloballyInternalize() { i::DisallowGarbageCollection no_gc; i::Handle str = Utils::OpenHandle(this); diff --git a/src/heap/safepoint.cc b/src/heap/safepoint.cc index 9dfd95a5e5..7c901a6e6e 100644 --- a/src/heap/safepoint.cc +++ b/src/heap/safepoint.cc @@ -206,6 +206,10 @@ void IsolateSafepoint::WaitInUnpark() { barrier_.WaitInUnpark(); } void IsolateSafepoint::NotifyPark() { barrier_.NotifyPark(); } +void IsolateSafepoint::NotifyParkEx() { barrier_.NotifyParkEx(); } + +void IsolateSafepoint::NotifyUnpark() { barrier_.NotifyUnpark(); } + void IsolateSafepoint::WaitUntilRunningThreadsInSafepoint( const PerClientSafepointData* client_data) { barrier_.WaitUntilRunningThreadsInSafepoint(client_data->running()); @@ -243,6 +247,19 @@ void IsolateSafepoint::Barrier::NotifyPark() { cv_stopped_.NotifyOne(); } +void IsolateSafepoint::Barrier::NotifyParkEx() { + base::MutexGuard guard(&mutex_); + armed_ = true; + stopped_++; + cv_stopped_.NotifyOne(); +} + +void IsolateSafepoint::Barrier::NotifyUnpark() { + base::MutexGuard guard(&mutex_); + stopped_--; + cv_stopped_.NotifyOne(); +} + void IsolateSafepoint::Barrier::WaitInSafepoint() { base::MutexGuard guard(&mutex_); CHECK(IsArmed()); diff --git a/src/heap/safepoint.h b/src/heap/safepoint.h index 23bc905580..29c32a702b 100644 --- a/src/heap/safepoint.h +++ b/src/heap/safepoint.h @@ -43,6 +43,18 @@ class IsolateSafepoint final { V8_EXPORT_PRIVATE void AssertMainThreadIsOnlyThread(); + // Wait until unpark operation is safe again. + void WaitInUnpark(); + + // Enter the safepoint from a running thread. + void WaitInSafepoint(); + + // Running thread reached a safepoint by parking itself. + void NotifyPark(); + void NotifyParkEx(); + void NotifyUnpark(); + + private: class Barrier { base::Mutex mutex_; @@ -57,26 +69,20 @@ class IsolateSafepoint final { public: Barrier() : armed_(false), stopped_(0) {} - void Arm(); - void Disarm(); void WaitUntilRunningThreadsInSafepoint(size_t running); + void Arm(); + + void Disarm(); void WaitInSafepoint(); void WaitInUnpark(); + void NotifyUnpark(); + void NotifyParkEx(); void NotifyPark(); }; enum class IncludeMainThread { kYes, kNo }; - // Wait until unpark operation is safe again. - void WaitInUnpark(); - - // Enter the safepoint from a running thread. - void WaitInSafepoint(); - - // Running thread reached a safepoint by parking itself. - void NotifyPark(); - // Methods for entering/leaving local safepoint scopes. void EnterLocalSafepointScope(); void LeaveLocalSafepointScope();