2020-05-05 09:19:02 +02:00
|
|
|
// Copyright 2020 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2022-09-21 13:28:42 +02:00
|
|
|
#include "src/handles/local-handles.h"
|
|
|
|
|
2020-05-05 09:19:02 +02:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include "src/api/api.h"
|
|
|
|
#include "src/base/platform/semaphore.h"
|
2021-02-24 14:47:06 +01:00
|
|
|
#include "src/common/assert-scope.h"
|
2020-10-15 20:17:08 +02:00
|
|
|
#include "src/common/globals.h"
|
2020-05-05 09:19:02 +02:00
|
|
|
#include "src/handles/handles-inl.h"
|
|
|
|
#include "src/handles/local-handles-inl.h"
|
|
|
|
#include "src/heap/heap.h"
|
|
|
|
#include "src/heap/local-heap.h"
|
2021-02-24 14:47:06 +01:00
|
|
|
#include "src/heap/parked-scope.h"
|
2020-05-05 09:19:02 +02:00
|
|
|
#include "src/objects/heap-number.h"
|
2022-09-21 13:28:42 +02:00
|
|
|
#include "test/unittests/test-utils.h"
|
2020-05-05 09:19:02 +02:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2022-09-21 13:28:42 +02:00
|
|
|
using LocalHandlesTest = TestWithIsolate;
|
|
|
|
|
2020-10-15 20:17:08 +02:00
|
|
|
namespace {
|
|
|
|
|
2020-05-05 09:19:02 +02:00
|
|
|
class LocalHandlesThread final : public v8::base::Thread {
|
|
|
|
public:
|
|
|
|
LocalHandlesThread(Heap* heap, Address object, base::Semaphore* sema_started,
|
|
|
|
base::Semaphore* sema_gc_finished)
|
|
|
|
: v8::base::Thread(base::Thread::Options("ThreadWithLocalHeap")),
|
|
|
|
heap_(heap),
|
|
|
|
object_(object),
|
|
|
|
sema_started_(sema_started),
|
|
|
|
sema_gc_finished_(sema_gc_finished) {}
|
|
|
|
|
|
|
|
void Run() override {
|
2021-02-11 19:03:35 +01:00
|
|
|
LocalHeap local_heap(heap_, ThreadKind::kBackground);
|
|
|
|
UnparkedScope unparked_scope(&local_heap);
|
2020-05-05 09:19:02 +02:00
|
|
|
LocalHandleScope scope(&local_heap);
|
|
|
|
|
|
|
|
static constexpr int kNumHandles =
|
|
|
|
kHandleBlockSize * 2 + kHandleBlockSize / 2;
|
|
|
|
|
|
|
|
std::vector<Handle<HeapNumber>> handles;
|
|
|
|
handles.reserve(kNumHandles);
|
|
|
|
|
|
|
|
for (int i = 0; i < kNumHandles; i++) {
|
|
|
|
Handle<HeapNumber> number = handle(
|
2024-08-14 20:41:00 +02:00
|
|
|
Cast<HeapNumber>(HeapObject::FromAddress(object_)), &local_heap);
|
2020-05-05 09:19:02 +02:00
|
|
|
handles.push_back(number);
|
|
|
|
}
|
|
|
|
|
|
|
|
sema_started_->Signal();
|
|
|
|
|
2024-08-14 20:41:00 +02:00
|
|
|
local_heap.ExecuteWhileParked([this]() { sema_gc_finished_->Wait(); });
|
2020-05-05 09:19:02 +02:00
|
|
|
|
2024-08-14 20:41:00 +02:00
|
|
|
for (DirectHandle<HeapNumber> handle : handles) {
|
2020-05-05 09:19:02 +02:00
|
|
|
CHECK_EQ(42.0, handle->value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Heap* heap_;
|
|
|
|
Address object_;
|
|
|
|
base::Semaphore* sema_started_;
|
|
|
|
base::Semaphore* sema_gc_finished_;
|
|
|
|
};
|
|
|
|
|
2022-09-21 13:28:42 +02:00
|
|
|
TEST_F(LocalHandlesTest, CreateLocalHandles) {
|
|
|
|
Isolate* isolate = i_isolate();
|
2020-05-05 09:19:02 +02:00
|
|
|
|
|
|
|
Address object = kNullAddress;
|
|
|
|
|
|
|
|
{
|
|
|
|
HandleScope handle_scope(isolate);
|
2024-08-14 20:41:00 +02:00
|
|
|
DirectHandle<HeapNumber> number = isolate->factory()->NewHeapNumber(42.0);
|
2023-10-05 08:46:07 +02:00
|
|
|
object = number->address();
|
2020-05-05 09:19:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
base::Semaphore sema_started(0);
|
|
|
|
base::Semaphore sema_gc_finished(0);
|
|
|
|
|
|
|
|
std::unique_ptr<LocalHandlesThread> thread(new LocalHandlesThread(
|
|
|
|
isolate->heap(), object, &sema_started, &sema_gc_finished));
|
|
|
|
CHECK(thread->Start());
|
|
|
|
|
|
|
|
sema_started.Wait();
|
|
|
|
|
2023-10-05 08:46:07 +02:00
|
|
|
InvokeMajorGC();
|
2020-05-05 09:19:02 +02:00
|
|
|
sema_gc_finished.Signal();
|
|
|
|
|
|
|
|
thread->Join();
|
|
|
|
}
|
|
|
|
|
2022-09-21 13:28:42 +02:00
|
|
|
TEST_F(LocalHandlesTest, CreateLocalHandlesWithoutLocalHandleScope) {
|
|
|
|
Isolate* isolate = i_isolate();
|
2021-02-24 14:47:06 +01:00
|
|
|
HandleScope handle_scope(isolate);
|
2020-10-15 20:17:08 +02:00
|
|
|
|
2021-02-24 14:47:06 +01:00
|
|
|
handle(Smi::FromInt(17), isolate->main_thread_local_heap());
|
2020-10-15 20:17:08 +02:00
|
|
|
}
|
|
|
|
|
2022-09-21 13:28:42 +02:00
|
|
|
TEST_F(LocalHandlesTest, DereferenceLocalHandle) {
|
|
|
|
Isolate* isolate = i_isolate();
|
2020-10-15 20:17:08 +02:00
|
|
|
|
|
|
|
// Create a PersistentHandle to create the LocalHandle, and thus not have a
|
|
|
|
// HandleScope present to override the LocalHandleScope.
|
|
|
|
std::unique_ptr<PersistentHandles> phs = isolate->NewPersistentHandles();
|
2024-09-17 12:09:47 +02:00
|
|
|
IndirectHandle<HeapNumber> ph;
|
2020-10-15 20:17:08 +02:00
|
|
|
{
|
|
|
|
HandleScope handle_scope(isolate);
|
|
|
|
Handle<HeapNumber> number = isolate->factory()->NewHeapNumber(42.0);
|
|
|
|
ph = phs->NewHandle(number);
|
|
|
|
}
|
|
|
|
{
|
2021-02-24 14:47:06 +01:00
|
|
|
LocalHeap local_heap(isolate->heap(), ThreadKind::kBackground,
|
|
|
|
std::move(phs));
|
2021-02-11 19:03:35 +01:00
|
|
|
UnparkedScope unparked_scope(&local_heap);
|
2020-10-15 20:17:08 +02:00
|
|
|
LocalHandleScope scope(&local_heap);
|
2025-04-29 08:03:15 +02:00
|
|
|
DirectHandle<HeapNumber> local_number = direct_handle(*ph, &local_heap);
|
2020-10-15 20:17:08 +02:00
|
|
|
CHECK_EQ(42, local_number->value());
|
2021-02-24 14:47:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 13:28:42 +02:00
|
|
|
TEST_F(LocalHandlesTest, DereferenceLocalHandleFailsWhenDisallowed) {
|
|
|
|
Isolate* isolate = i_isolate();
|
2021-02-24 14:47:06 +01:00
|
|
|
|
|
|
|
// Create a PersistentHandle to create the LocalHandle, and thus not have a
|
|
|
|
// HandleScope present to override the LocalHandleScope.
|
|
|
|
std::unique_ptr<PersistentHandles> phs = isolate->NewPersistentHandles();
|
2024-09-17 12:09:47 +02:00
|
|
|
IndirectHandle<HeapNumber> ph;
|
2021-02-24 14:47:06 +01:00
|
|
|
{
|
|
|
|
HandleScope handle_scope(isolate);
|
|
|
|
Handle<HeapNumber> number = isolate->factory()->NewHeapNumber(42.0);
|
|
|
|
ph = phs->NewHandle(number);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
LocalHeap local_heap(isolate->heap(), ThreadKind::kBackground,
|
|
|
|
std::move(phs));
|
|
|
|
UnparkedScope unparked_scope(&local_heap);
|
|
|
|
LocalHandleScope scope(&local_heap);
|
2025-04-29 08:03:15 +02:00
|
|
|
DirectHandle<HeapNumber> local_number = direct_handle(*ph, &local_heap);
|
2020-10-15 20:17:08 +02:00
|
|
|
DisallowHandleDereference disallow_scope;
|
|
|
|
CHECK_EQ(42, local_number->value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2020-05-05 09:19:02 +02:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|