Commit 4a946eec authored by Dominik Inführ's avatar Dominik Inführ Committed by Commit Bot

[handles] Retarget local handles on the main thread

So far the main thread can have two different kinds of local handles,
regular main thread handles and local handles in its LocalIsolate. This
is both confusing and error-prone.

This CL retargets local handles creation for the LocalIsolate on the
main thread to always create regular main thread handles instead.

Bug: v8:10315
Change-Id: I4df509a0fc1bd630ba956b5eaacacbe706ddb4ef
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2527062Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Reviewed-by: 's avatarIgor Sheludko <ishell@chromium.org>
Commit-Queue: Dominik Inführ <dinfuehr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#71534}
parent 2c0d87d0
......@@ -82,6 +82,8 @@ class V8_EXPORT_PRIVATE LocalIsolate final : private HiddenLocalFactory {
ThreadId thread_id() const { return thread_id_; }
Address stack_limit() const { return stack_limit_; }
bool is_main_thread() const { return heap_.is_main_thread(); }
private:
friend class v8::internal::LocalFactory;
......
......@@ -266,6 +266,7 @@ class V8_NODISCARD HandleScope {
friend class HandleScopeImplementer;
friend class Isolate;
friend class LocalHandles;
friend class LocalHandleScope;
friend class PersistentHandles;
};
......
......@@ -5,6 +5,7 @@
#ifndef V8_HANDLES_LOCAL_HANDLES_INL_H_
#define V8_HANDLES_LOCAL_HANDLES_INL_H_
#include "src/execution/isolate.h"
#include "src/execution/local-isolate.h"
#include "src/handles/local-handles.h"
#include "src/sanitizer/msan.h"
......@@ -15,6 +16,9 @@ namespace internal {
// static
V8_INLINE Address* LocalHandleScope::GetHandle(LocalHeap* local_heap,
Address value) {
if (local_heap->is_main_thread())
return LocalHandleScope::GetMainThreadHandle(local_heap, value);
LocalHandles* handles = local_heap->handles();
Address* result = handles->scope_.next;
if (result == handles->scope_.limit) {
......@@ -30,16 +34,23 @@ LocalHandleScope::LocalHandleScope(LocalIsolate* local_isolate)
: LocalHandleScope(local_isolate->heap()) {}
LocalHandleScope::LocalHandleScope(LocalHeap* local_heap) {
LocalHandles* handles = local_heap->handles();
local_heap_ = local_heap;
prev_next_ = handles->scope_.next;
prev_limit_ = handles->scope_.limit;
handles->scope_.level++;
if (local_heap->is_main_thread()) {
OpenMainThreadScope(local_heap);
} else {
LocalHandles* handles = local_heap->handles();
local_heap_ = local_heap;
prev_next_ = handles->scope_.next;
prev_limit_ = handles->scope_.limit;
handles->scope_.level++;
}
}
LocalHandleScope::~LocalHandleScope() {
if (local_heap_ == nullptr) return;
CloseScope(local_heap_, prev_next_, prev_limit_);
if (local_heap_->is_main_thread()) {
CloseMainThreadScope(local_heap_, prev_next_, prev_limit_);
} else {
CloseScope(local_heap_, prev_next_, prev_limit_);
}
}
template <typename T>
......
......@@ -5,11 +5,36 @@
#include "src/handles/local-handles.h"
#include "src/api/api.h"
#include "src/execution/isolate.h"
#include "src/handles/handles-inl.h"
#include "src/handles/handles.h"
#include "src/heap/heap-inl.h"
namespace v8 {
namespace internal {
Address* LocalHandleScope::GetMainThreadHandle(LocalHeap* local_heap,
Address value) {
Isolate* isolate = local_heap->heap()->isolate();
return HandleScope::GetHandle(isolate, value);
}
void LocalHandleScope::OpenMainThreadScope(LocalHeap* local_heap) {
Isolate* isolate = local_heap->heap()->isolate();
HandleScopeData* data = isolate->handle_scope_data();
local_heap_ = local_heap;
prev_next_ = data->next;
prev_limit_ = data->limit;
data->level++;
}
void LocalHandleScope::CloseMainThreadScope(LocalHeap* local_heap,
Address* prev_next,
Address* prev_limit) {
Isolate* isolate = local_heap->heap()->isolate();
HandleScope::CloseScope(isolate, prev_next, prev_limit);
}
LocalHandles::LocalHandles() { scope_.Initialize(); }
LocalHandles::~LocalHandles() {
scope_.limit = nullptr;
......
......@@ -62,6 +62,14 @@ class V8_NODISCARD LocalHandleScope {
// Close the handle scope resetting limits to a previous state.
static inline void CloseScope(LocalHeap* local_heap, Address* prev_next,
Address* prev_limit);
V8_EXPORT_PRIVATE static void CloseMainThreadScope(LocalHeap* local_heap,
Address* prev_next,
Address* prev_limit);
V8_EXPORT_PRIVATE void OpenMainThreadScope(LocalHeap* local_heap);
V8_EXPORT_PRIVATE static Address* GetMainThreadHandle(LocalHeap* local_heap,
Address value);
LocalHeap* local_heap_;
Address* prev_limit_;
......
......@@ -102,6 +102,7 @@ TEST(CreateLocalHandlesWithoutLocalHandleScope) {
heap::EnsureFlagLocalHeapsEnabled();
CcTest::InitializeVM();
Isolate* isolate = CcTest::i_isolate();
HandleScope handle_scope(isolate);
handle(Smi::FromInt(17), isolate->main_thread_local_heap());
}
......
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