Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5 | #include "src/handles.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7 | #include "src/address-map.h" |
| 8 | #include "src/base/logging.h" |
| 9 | #include "src/identity-map.h" |
| 10 | #include "src/objects-inl.h" |
| 11 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12 | namespace v8 { |
| 13 | namespace internal { |
| 14 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15 | #ifdef DEBUG |
| 16 | bool HandleBase::IsDereferenceAllowed(DereferenceCheckMode mode) const { |
| 17 | DCHECK_NOT_NULL(location_); |
| 18 | Object* object = *location_; |
| 19 | if (object->IsSmi()) return true; |
| 20 | HeapObject* heap_object = HeapObject::cast(object); |
| 21 | Heap* heap = heap_object->GetHeap(); |
| 22 | Object** roots_array_start = heap->roots_array_start(); |
| 23 | if (roots_array_start <= location_ && |
| 24 | location_ < roots_array_start + Heap::kStrongRootListLength && |
| 25 | heap->RootCanBeTreatedAsConstant( |
| 26 | static_cast<Heap::RootListIndex>(location_ - roots_array_start))) { |
| 27 | return true; |
| 28 | } |
| 29 | if (!AllowHandleDereference::IsAllowed()) return false; |
| 30 | if (mode == INCLUDE_DEFERRED_CHECK && |
| 31 | !AllowDeferredHandleDereference::IsAllowed()) { |
| 32 | // Accessing cells, maps and internalized strings is safe. |
| 33 | if (heap_object->IsCell()) return true; |
| 34 | if (heap_object->IsMap()) return true; |
| 35 | if (heap_object->IsInternalizedString()) return true; |
| 36 | return !heap->isolate()->IsDeferredHandle(location_); |
| 37 | } |
| 38 | return true; |
| 39 | } |
| 40 | #endif |
| 41 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 43 | int HandleScope::NumberOfHandles(Isolate* isolate) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 44 | HandleScopeImplementer* impl = isolate->handle_scope_implementer(); |
| 45 | int n = impl->blocks()->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 46 | if (n == 0) return 0; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 47 | return ((n - 1) * kHandleBlockSize) + static_cast<int>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 48 | (isolate->handle_scope_data()->next - impl->blocks()->last())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 52 | Object** HandleScope::Extend(Isolate* isolate) { |
| 53 | HandleScopeData* current = isolate->handle_scope_data(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 54 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 55 | Object** result = current->next; |
| 56 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 57 | DCHECK(result == current->limit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 58 | // Make sure there's at least one scope on the stack and that the |
| 59 | // top of the scope stack isn't a barrier. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 60 | if (!Utils::ApiCheck(current->level != current->sealed_level, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 61 | "v8::HandleScope::CreateHandle()", |
| 62 | "Cannot create a handle without a HandleScope")) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 63 | return NULL; |
| 64 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 65 | HandleScopeImplementer* impl = isolate->handle_scope_implementer(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 66 | // If there's more room in the last block, we use that. This is used |
| 67 | // for fast creation of scopes after scope barriers. |
| 68 | if (!impl->blocks()->is_empty()) { |
| 69 | Object** limit = &impl->blocks()->last()[kHandleBlockSize]; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 70 | if (current->limit != limit) { |
| 71 | current->limit = limit; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 72 | DCHECK(limit - current->next < kHandleBlockSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | // If we still haven't found a slot for the handle, we extend the |
| 77 | // current handle scope by allocating a new handle block. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 78 | if (result == current->limit) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 79 | // If there's a spare block, use it for growing the current scope. |
| 80 | result = impl->GetSpareOrNewBlock(); |
| 81 | // Add the extension to the global list of blocks, but count the |
| 82 | // extension as part of the current scope. |
| 83 | impl->blocks()->Add(result); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 84 | current->limit = &result[kHandleBlockSize]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | return result; |
| 88 | } |
| 89 | |
| 90 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 91 | void HandleScope::DeleteExtensions(Isolate* isolate) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 92 | HandleScopeData* current = isolate->handle_scope_data(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 93 | isolate->handle_scope_implementer()->DeleteExtensions(current->limit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 97 | #ifdef ENABLE_HANDLE_ZAPPING |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 98 | void HandleScope::ZapRange(Object** start, Object** end) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 99 | DCHECK(end - start <= kHandleBlockSize); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 100 | for (Object** p = start; p != end; p++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 101 | *reinterpret_cast<Address*>(p) = kHandleZapValue; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 104 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 105 | |
| 106 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 107 | Address HandleScope::current_level_address(Isolate* isolate) { |
| 108 | return reinterpret_cast<Address>(&isolate->handle_scope_data()->level); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 112 | Address HandleScope::current_next_address(Isolate* isolate) { |
| 113 | return reinterpret_cast<Address>(&isolate->handle_scope_data()->next); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 117 | Address HandleScope::current_limit_address(Isolate* isolate) { |
| 118 | return reinterpret_cast<Address>(&isolate->handle_scope_data()->limit); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 121 | CanonicalHandleScope::CanonicalHandleScope(Isolate* isolate) |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 122 | : isolate_(isolate), zone_(isolate->allocator()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 123 | HandleScopeData* handle_scope_data = isolate_->handle_scope_data(); |
| 124 | prev_canonical_scope_ = handle_scope_data->canonical_scope; |
| 125 | handle_scope_data->canonical_scope = this; |
| 126 | root_index_map_ = new RootIndexMap(isolate); |
| 127 | identity_map_ = new IdentityMap<Object**>(isolate->heap(), &zone_); |
| 128 | canonical_level_ = handle_scope_data->level; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | CanonicalHandleScope::~CanonicalHandleScope() { |
| 133 | delete root_index_map_; |
| 134 | delete identity_map_; |
| 135 | isolate_->handle_scope_data()->canonical_scope = prev_canonical_scope_; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | Object** CanonicalHandleScope::Lookup(Object* object) { |
| 140 | DCHECK_LE(canonical_level_, isolate_->handle_scope_data()->level); |
| 141 | if (isolate_->handle_scope_data()->level != canonical_level_) { |
| 142 | // We are in an inner handle scope. Do not canonicalize since we will leave |
| 143 | // this handle scope while still being in the canonical scope. |
| 144 | return HandleScope::CreateHandle(isolate_, object); |
| 145 | } |
| 146 | if (object->IsHeapObject()) { |
| 147 | int index = root_index_map_->Lookup(HeapObject::cast(object)); |
| 148 | if (index != RootIndexMap::kInvalidRootIndex) { |
| 149 | return isolate_->heap() |
| 150 | ->root_handle(static_cast<Heap::RootListIndex>(index)) |
| 151 | .location(); |
| 152 | } |
| 153 | } |
| 154 | Object*** entry = identity_map_->Get(object); |
| 155 | if (*entry == nullptr) { |
| 156 | // Allocate new handle location. |
| 157 | *entry = HandleScope::CreateHandle(isolate_, object); |
| 158 | } |
| 159 | return reinterpret_cast<Object**>(*entry); |
| 160 | } |
| 161 | |
| 162 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 163 | DeferredHandleScope::DeferredHandleScope(Isolate* isolate) |
| 164 | : impl_(isolate->handle_scope_implementer()) { |
| 165 | impl_->BeginDeferredScope(); |
| 166 | HandleScopeData* data = impl_->isolate()->handle_scope_data(); |
| 167 | Object** new_next = impl_->GetSpareOrNewBlock(); |
| 168 | Object** new_limit = &new_next[kHandleBlockSize]; |
| 169 | DCHECK(data->limit == &impl_->blocks()->last()[kHandleBlockSize]); |
| 170 | impl_->blocks()->Add(new_next); |
| 171 | |
| 172 | #ifdef DEBUG |
| 173 | prev_level_ = data->level; |
| 174 | #endif |
| 175 | data->level++; |
| 176 | prev_limit_ = data->limit; |
| 177 | prev_next_ = data->next; |
| 178 | data->next = new_next; |
| 179 | data->limit = new_limit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 183 | DeferredHandleScope::~DeferredHandleScope() { |
| 184 | impl_->isolate()->handle_scope_data()->level--; |
| 185 | DCHECK(handles_detached_); |
| 186 | DCHECK(impl_->isolate()->handle_scope_data()->level == prev_level_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 190 | DeferredHandles* DeferredHandleScope::Detach() { |
| 191 | DeferredHandles* deferred = impl_->Detach(prev_limit_); |
| 192 | HandleScopeData* data = impl_->isolate()->handle_scope_data(); |
| 193 | data->next = prev_next_; |
| 194 | data->limit = prev_limit_; |
| 195 | #ifdef DEBUG |
| 196 | handles_detached_ = true; |
| 197 | #endif |
| 198 | return deferred; |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 201 | } // namespace internal |
| 202 | } // namespace v8 |