2012-04-17 07:16:19 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2015-05-05 15:44:55 +00:00
|
|
|
#include "src/handles.h"
|
2015-05-05 12:48:14 +00:00
|
|
|
|
2015-10-26 15:33:02 +00:00
|
|
|
#include "src/address-map.h"
|
2017-09-11 11:40:20 +00:00
|
|
|
#include "src/api.h"
|
2015-08-20 07:44:00 +00:00
|
|
|
#include "src/base/logging.h"
|
2015-10-26 15:33:02 +00:00
|
|
|
#include "src/identity-map.h"
|
2018-07-23 12:37:45 +00:00
|
|
|
#include "src/maybe-handles.h"
|
2015-08-20 07:44:00 +00:00
|
|
|
#include "src/objects-inl.h"
|
2018-10-31 22:52:56 +00:00
|
|
|
#include "src/roots-inl.h"
|
2015-08-20 07:44:00 +00:00
|
|
|
|
2019-02-14 21:10:30 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
// For GetIsolateFromWritableHeapObject.
|
|
|
|
#include "src/heap/heap-write-barrier-inl.h"
|
|
|
|
#endif
|
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2017-08-07 16:57:33 +00:00
|
|
|
// Handles should be trivially copyable so that they can be efficiently passed
|
|
|
|
// by value. If they are not trivially copyable, they cannot be passed in
|
|
|
|
// registers.
|
2018-02-28 16:32:45 +00:00
|
|
|
ASSERT_TRIVIALLY_COPYABLE(HandleBase);
|
|
|
|
ASSERT_TRIVIALLY_COPYABLE(Handle<Object>);
|
|
|
|
ASSERT_TRIVIALLY_COPYABLE(MaybeHandle<Object>);
|
2017-06-20 07:04:50 +00:00
|
|
|
|
2015-07-15 11:05:00 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
bool HandleBase::IsDereferenceAllowed(DereferenceCheckMode mode) const {
|
|
|
|
DCHECK_NOT_NULL(location_);
|
2018-12-25 00:19:47 +00:00
|
|
|
Object object(*location_);
|
2015-07-15 11:05:00 +00:00
|
|
|
if (object->IsSmi()) return true;
|
2018-12-20 15:47:47 +00:00
|
|
|
HeapObject heap_object = HeapObject::cast(object);
|
2018-07-20 10:26:50 +00:00
|
|
|
Isolate* isolate;
|
2019-02-14 21:10:30 +00:00
|
|
|
if (!GetIsolateFromWritableObject(heap_object, &isolate)) return true;
|
2018-09-26 09:05:43 +00:00
|
|
|
RootIndex root_index;
|
2018-10-31 22:52:56 +00:00
|
|
|
if (isolate->roots_table().IsRootHandleLocation(location_, &root_index) &&
|
2018-10-09 23:19:09 +00:00
|
|
|
RootsTable::IsImmortalImmovable(root_index)) {
|
2015-07-15 11:05:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!AllowHandleDereference::IsAllowed()) return false;
|
|
|
|
if (mode == INCLUDE_DEFERRED_CHECK &&
|
|
|
|
!AllowDeferredHandleDereference::IsAllowed()) {
|
|
|
|
// Accessing cells, maps and internalized strings is safe.
|
|
|
|
if (heap_object->IsCell()) return true;
|
|
|
|
if (heap_object->IsMap()) return true;
|
|
|
|
if (heap_object->IsInternalizedString()) return true;
|
2018-10-31 22:52:56 +00:00
|
|
|
return !isolate->IsDeferredHandle(location_);
|
2015-07-15 11:05:00 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2013-02-25 14:46:09 +00:00
|
|
|
int HandleScope::NumberOfHandles(Isolate* isolate) {
|
2011-03-18 20:35:07 +00:00
|
|
|
HandleScopeImplementer* impl = isolate->handle_scope_implementer();
|
2017-09-28 08:10:43 +00:00
|
|
|
int n = static_cast<int>(impl->blocks()->size());
|
2009-01-23 17:22:23 +00:00
|
|
|
if (n == 0) return 0;
|
2017-09-28 08:10:43 +00:00
|
|
|
return ((n - 1) * kHandleBlockSize) +
|
|
|
|
static_cast<int>(
|
|
|
|
(isolate->handle_scope_data()->next - impl->blocks()->back()));
|
2009-01-23 17:22:23 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 22:52:56 +00:00
|
|
|
Address* HandleScope::Extend(Isolate* isolate) {
|
2014-01-16 08:17:40 +00:00
|
|
|
HandleScopeData* current = isolate->handle_scope_data();
|
2009-01-23 17:22:23 +00:00
|
|
|
|
2018-10-31 22:52:56 +00:00
|
|
|
Address* result = current->next;
|
2015-05-05 15:44:55 +00:00
|
|
|
|
|
|
|
DCHECK(result == current->limit);
|
2009-03-18 18:50:35 +00:00
|
|
|
// Make sure there's at least one scope on the stack and that the
|
|
|
|
// top of the scope stack isn't a barrier.
|
2015-10-26 15:33:02 +00:00
|
|
|
if (!Utils::ApiCheck(current->level != current->sealed_level,
|
2014-01-13 09:42:23 +00:00
|
|
|
"v8::HandleScope::CreateHandle()",
|
|
|
|
"Cannot create a handle without a HandleScope")) {
|
2017-10-13 16:33:03 +00:00
|
|
|
return nullptr;
|
2009-03-18 18:50:35 +00:00
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
HandleScopeImplementer* impl = isolate->handle_scope_implementer();
|
2009-03-18 18:50:35 +00:00
|
|
|
// If there's more room in the last block, we use that. This is used
|
|
|
|
// for fast creation of scopes after scope barriers.
|
2017-09-28 08:10:43 +00:00
|
|
|
if (!impl->blocks()->empty()) {
|
2018-10-31 22:52:56 +00:00
|
|
|
Address* limit = &impl->blocks()->back()[kHandleBlockSize];
|
2011-03-18 20:35:07 +00:00
|
|
|
if (current->limit != limit) {
|
|
|
|
current->limit = limit;
|
2017-10-18 09:06:55 +00:00
|
|
|
DCHECK_LT(limit - current->next, kHandleBlockSize);
|
2009-01-23 17:22:23 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-18 21:14:46 +00:00
|
|
|
|
2009-03-18 18:50:35 +00:00
|
|
|
// If we still haven't found a slot for the handle, we extend the
|
|
|
|
// current handle scope by allocating a new handle block.
|
2011-03-18 20:35:07 +00:00
|
|
|
if (result == current->limit) {
|
2009-03-18 18:50:35 +00:00
|
|
|
// If there's a spare block, use it for growing the current scope.
|
|
|
|
result = impl->GetSpareOrNewBlock();
|
|
|
|
// Add the extension to the global list of blocks, but count the
|
|
|
|
// extension as part of the current scope.
|
2017-09-28 08:10:43 +00:00
|
|
|
impl->blocks()->push_back(result);
|
2011-03-18 20:35:07 +00:00
|
|
|
current->limit = &result[kHandleBlockSize];
|
2009-03-18 18:50:35 +00:00
|
|
|
}
|
2009-01-23 17:22:23 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-05 15:44:55 +00:00
|
|
|
void HandleScope::DeleteExtensions(Isolate* isolate) {
|
|
|
|
HandleScopeData* current = isolate->handle_scope_data();
|
|
|
|
isolate->handle_scope_implementer()->DeleteExtensions(current->limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-30 11:56:52 +00:00
|
|
|
#ifdef ENABLE_HANDLE_ZAPPING
|
2018-10-31 22:52:56 +00:00
|
|
|
void HandleScope::ZapRange(Address* start, Address* end) {
|
2017-10-18 09:06:55 +00:00
|
|
|
DCHECK_LE(end - start, kHandleBlockSize);
|
2018-10-31 22:52:56 +00:00
|
|
|
for (Address* p = start; p != end; p++) {
|
|
|
|
*p = static_cast<Address>(kHandleZapValue);
|
2009-01-23 17:22:23 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-22 13:40:13 +00:00
|
|
|
#endif
|
2009-01-23 17:22:23 +00:00
|
|
|
|
|
|
|
|
2013-02-25 14:46:09 +00:00
|
|
|
Address HandleScope::current_level_address(Isolate* isolate) {
|
|
|
|
return reinterpret_cast<Address>(&isolate->handle_scope_data()->level);
|
2009-11-04 08:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-25 14:46:09 +00:00
|
|
|
Address HandleScope::current_next_address(Isolate* isolate) {
|
|
|
|
return reinterpret_cast<Address>(&isolate->handle_scope_data()->next);
|
2009-11-04 08:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-25 14:46:09 +00:00
|
|
|
Address HandleScope::current_limit_address(Isolate* isolate) {
|
|
|
|
return reinterpret_cast<Address>(&isolate->handle_scope_data()->limit);
|
2009-11-04 08:51:48 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 15:33:02 +00:00
|
|
|
CanonicalHandleScope::CanonicalHandleScope(Isolate* isolate)
|
2016-10-17 12:12:30 +00:00
|
|
|
: isolate_(isolate), zone_(isolate->allocator(), ZONE_NAME) {
|
2015-10-26 15:33:02 +00:00
|
|
|
HandleScopeData* handle_scope_data = isolate_->handle_scope_data();
|
|
|
|
prev_canonical_scope_ = handle_scope_data->canonical_scope;
|
|
|
|
handle_scope_data->canonical_scope = this;
|
|
|
|
root_index_map_ = new RootIndexMap(isolate);
|
2018-10-18 23:44:29 +00:00
|
|
|
identity_map_ = new IdentityMap<Address*, ZoneAllocationPolicy>(
|
2017-02-20 21:46:38 +00:00
|
|
|
isolate->heap(), ZoneAllocationPolicy(&zone_));
|
2015-10-26 15:33:02 +00:00
|
|
|
canonical_level_ = handle_scope_data->level;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CanonicalHandleScope::~CanonicalHandleScope() {
|
|
|
|
delete root_index_map_;
|
|
|
|
delete identity_map_;
|
|
|
|
isolate_->handle_scope_data()->canonical_scope = prev_canonical_scope_;
|
|
|
|
}
|
|
|
|
|
2018-10-18 23:44:29 +00:00
|
|
|
Address* CanonicalHandleScope::Lookup(Address object) {
|
2015-10-26 15:33:02 +00:00
|
|
|
DCHECK_LE(canonical_level_, isolate_->handle_scope_data()->level);
|
|
|
|
if (isolate_->handle_scope_data()->level != canonical_level_) {
|
|
|
|
// We are in an inner handle scope. Do not canonicalize since we will leave
|
|
|
|
// this handle scope while still being in the canonical scope.
|
|
|
|
return HandleScope::CreateHandle(isolate_, object);
|
|
|
|
}
|
2018-10-18 23:44:29 +00:00
|
|
|
if (Internals::HasHeapObjectTag(object)) {
|
2018-09-26 09:05:43 +00:00
|
|
|
RootIndex root_index;
|
2018-10-18 23:44:29 +00:00
|
|
|
if (root_index_map_->Lookup(object, &root_index)) {
|
2018-10-31 22:52:56 +00:00
|
|
|
return isolate_->root_handle(root_index).location();
|
2015-10-26 15:33:02 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-25 00:19:47 +00:00
|
|
|
Address** entry = identity_map_->Get(Object(object));
|
2015-10-26 15:33:02 +00:00
|
|
|
if (*entry == nullptr) {
|
|
|
|
// Allocate new handle location.
|
|
|
|
*entry = HandleScope::CreateHandle(isolate_, object);
|
|
|
|
}
|
2018-10-18 23:44:29 +00:00
|
|
|
return *entry;
|
2015-10-26 15:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-06 09:31:31 +00:00
|
|
|
DeferredHandleScope::DeferredHandleScope(Isolate* isolate)
|
|
|
|
: impl_(isolate->handle_scope_implementer()) {
|
|
|
|
impl_->BeginDeferredScope();
|
2014-01-16 08:17:40 +00:00
|
|
|
HandleScopeData* data = impl_->isolate()->handle_scope_data();
|
2018-10-31 22:52:56 +00:00
|
|
|
Address* new_next = impl_->GetSpareOrNewBlock();
|
|
|
|
Address* new_limit = &new_next[kHandleBlockSize];
|
2018-05-08 06:59:10 +00:00
|
|
|
// Check that at least one HandleScope with at least one Handle in it exists,
|
|
|
|
// see the class description.
|
2017-09-28 08:10:43 +00:00
|
|
|
DCHECK(!impl_->blocks()->empty());
|
2017-04-05 11:00:56 +00:00
|
|
|
// Check that we are not in a SealedHandleScope.
|
2017-09-28 08:10:43 +00:00
|
|
|
DCHECK(data->limit == &impl_->blocks()->back()[kHandleBlockSize]);
|
|
|
|
impl_->blocks()->push_back(new_next);
|
2012-07-06 09:31:31 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
prev_level_ = data->level;
|
|
|
|
#endif
|
|
|
|
data->level++;
|
|
|
|
prev_limit_ = data->limit;
|
|
|
|
prev_next_ = data->next;
|
|
|
|
data->next = new_next;
|
|
|
|
data->limit = new_limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DeferredHandleScope::~DeferredHandleScope() {
|
|
|
|
impl_->isolate()->handle_scope_data()->level--;
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(handles_detached_);
|
|
|
|
DCHECK(impl_->isolate()->handle_scope_data()->level == prev_level_);
|
2012-07-06 09:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DeferredHandles* DeferredHandleScope::Detach() {
|
|
|
|
DeferredHandles* deferred = impl_->Detach(prev_limit_);
|
2014-01-16 08:17:40 +00:00
|
|
|
HandleScopeData* data = impl_->isolate()->handle_scope_data();
|
2012-07-06 09:31:31 +00:00
|
|
|
data->next = prev_next_;
|
|
|
|
data->limit = prev_limit_;
|
|
|
|
#ifdef DEBUG
|
|
|
|
handles_detached_ = true;
|
|
|
|
#endif
|
|
|
|
return deferred;
|
|
|
|
}
|
|
|
|
|
2015-06-01 22:46:54 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|