Line data Source code
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.
4 :
5 : #include "src/handles.h"
6 :
7 : #include "src/address-map.h"
8 : #include "src/api.h"
9 : #include "src/base/logging.h"
10 : #include "src/identity-map.h"
11 : #include "src/maybe-handles.h"
12 : #include "src/objects-inl.h"
13 : #include "src/roots-inl.h"
14 :
15 : namespace v8 {
16 : namespace internal {
17 :
18 : // Handles should be trivially copyable so that they can be efficiently passed
19 : // by value. If they are not trivially copyable, they cannot be passed in
20 : // registers.
21 : ASSERT_TRIVIALLY_COPYABLE(HandleBase);
22 : ASSERT_TRIVIALLY_COPYABLE(Handle<Object>);
23 : ASSERT_TRIVIALLY_COPYABLE(MaybeHandle<Object>);
24 :
25 : #ifdef DEBUG
26 : bool HandleBase::IsDereferenceAllowed(DereferenceCheckMode mode) const {
27 : DCHECK_NOT_NULL(location_);
28 : Object object(*location_);
29 : if (object->IsSmi()) return true;
30 : HeapObject heap_object = HeapObject::cast(object);
31 : Isolate* isolate;
32 : if (!Isolate::FromWritableHeapObject(heap_object, &isolate)) return true;
33 : RootIndex root_index;
34 : if (isolate->roots_table().IsRootHandleLocation(location_, &root_index) &&
35 : RootsTable::IsImmortalImmovable(root_index)) {
36 : return true;
37 : }
38 : if (!AllowHandleDereference::IsAllowed()) return false;
39 : if (mode == INCLUDE_DEFERRED_CHECK &&
40 : !AllowDeferredHandleDereference::IsAllowed()) {
41 : // Accessing cells, maps and internalized strings is safe.
42 : if (heap_object->IsCell()) return true;
43 : if (heap_object->IsMap()) return true;
44 : if (heap_object->IsInternalizedString()) return true;
45 : return !isolate->IsDeferredHandle(location_);
46 : }
47 : return true;
48 : }
49 : #endif
50 :
51 :
52 6058 : int HandleScope::NumberOfHandles(Isolate* isolate) {
53 : HandleScopeImplementer* impl = isolate->handle_scope_implementer();
54 6058 : int n = static_cast<int>(impl->blocks()->size());
55 6058 : if (n == 0) return 0;
56 12080 : return ((n - 1) * kHandleBlockSize) +
57 : static_cast<int>(
58 18120 : (isolate->handle_scope_data()->next - impl->blocks()->back()));
59 : }
60 :
61 6169491 : Address* HandleScope::Extend(Isolate* isolate) {
62 : HandleScopeData* current = isolate->handle_scope_data();
63 :
64 3084746 : Address* result = current->next;
65 :
66 : DCHECK(result == current->limit);
67 : // Make sure there's at least one scope on the stack and that the
68 : // top of the scope stack isn't a barrier.
69 3084745 : if (!Utils::ApiCheck(current->level != current->sealed_level,
70 : "v8::HandleScope::CreateHandle()",
71 3084746 : "Cannot create a handle without a HandleScope")) {
72 : return nullptr;
73 : }
74 : HandleScopeImplementer* impl = isolate->handle_scope_implementer();
75 : // If there's more room in the last block, we use that. This is used
76 : // for fast creation of scopes after scope barriers.
77 3084745 : if (!impl->blocks()->empty()) {
78 685060 : Address* limit = &impl->blocks()->back()[kHandleBlockSize];
79 685060 : if (current->limit != limit) {
80 51071 : current->limit = limit;
81 : DCHECK_LT(limit - current->next, kHandleBlockSize);
82 : }
83 : }
84 :
85 : // If we still haven't found a slot for the handle, we extend the
86 : // current handle scope by allocating a new handle block.
87 3084745 : if (result == current->limit) {
88 : // If there's a spare block, use it for growing the current scope.
89 3033674 : result = impl->GetSpareOrNewBlock();
90 : // Add the extension to the global list of blocks, but count the
91 : // extension as part of the current scope.
92 3033674 : impl->blocks()->push_back(result);
93 3033673 : current->limit = &result[kHandleBlockSize];
94 : }
95 :
96 3084744 : return result;
97 : }
98 :
99 :
100 5718722 : void HandleScope::DeleteExtensions(Isolate* isolate) {
101 : HandleScopeData* current = isolate->handle_scope_data();
102 5718722 : isolate->handle_scope_implementer()->DeleteExtensions(current->limit);
103 2859369 : }
104 :
105 :
106 : #ifdef ENABLE_HANDLE_ZAPPING
107 709621624 : void HandleScope::ZapRange(Address* start, Address* end) {
108 : DCHECK_LE(end - start, kHandleBlockSize);
109 5256412198 : for (Address* p = start; p != end; p++) {
110 4546790574 : *p = static_cast<Address>(kHandleZapValue);
111 : }
112 709621624 : }
113 : #endif
114 :
115 :
116 62995 : Address HandleScope::current_level_address(Isolate* isolate) {
117 62995 : return reinterpret_cast<Address>(&isolate->handle_scope_data()->level);
118 : }
119 :
120 :
121 62995 : Address HandleScope::current_next_address(Isolate* isolate) {
122 62995 : return reinterpret_cast<Address>(&isolate->handle_scope_data()->next);
123 : }
124 :
125 :
126 62995 : Address HandleScope::current_limit_address(Isolate* isolate) {
127 62995 : return reinterpret_cast<Address>(&isolate->handle_scope_data()->limit);
128 : }
129 :
130 1044212 : CanonicalHandleScope::CanonicalHandleScope(Isolate* isolate)
131 1044212 : : isolate_(isolate), zone_(isolate->allocator(), ZONE_NAME) {
132 522111 : HandleScopeData* handle_scope_data = isolate_->handle_scope_data();
133 522111 : prev_canonical_scope_ = handle_scope_data->canonical_scope;
134 522111 : handle_scope_data->canonical_scope = this;
135 522111 : root_index_map_ = new RootIndexMap(isolate);
136 : identity_map_ = new IdentityMap<Address*, ZoneAllocationPolicy>(
137 1044222 : isolate->heap(), ZoneAllocationPolicy(&zone_));
138 522114 : canonical_level_ = handle_scope_data->level;
139 522114 : }
140 :
141 :
142 1044239 : CanonicalHandleScope::~CanonicalHandleScope() {
143 522119 : delete root_index_map_;
144 522120 : delete identity_map_;
145 522120 : isolate_->handle_scope_data()->canonical_scope = prev_canonical_scope_;
146 522120 : }
147 :
148 204558379 : Address* CanonicalHandleScope::Lookup(Address object) {
149 : DCHECK_LE(canonical_level_, isolate_->handle_scope_data()->level);
150 204558379 : if (isolate_->handle_scope_data()->level != canonical_level_) {
151 : // We are in an inner handle scope. Do not canonicalize since we will leave
152 : // this handle scope while still being in the canonical scope.
153 2630493 : return HandleScope::CreateHandle(isolate_, object);
154 : }
155 201927886 : if (Internals::HasHeapObjectTag(object)) {
156 : RootIndex root_index;
157 196622539 : if (root_index_map_->Lookup(object, &root_index)) {
158 80036373 : return isolate_->root_handle(root_index).location();
159 : }
160 : }
161 121892174 : Address** entry = identity_map_->Get(Object(object));
162 121892125 : if (*entry == nullptr) {
163 : // Allocate new handle location.
164 131441809 : *entry = HandleScope::CreateHandle(isolate_, object);
165 : }
166 121892048 : return *entry;
167 : }
168 :
169 :
170 12327 : DeferredHandleScope::DeferredHandleScope(Isolate* isolate)
171 12327 : : impl_(isolate->handle_scope_implementer()) {
172 24654 : impl_->BeginDeferredScope();
173 12327 : HandleScopeData* data = impl_->isolate()->handle_scope_data();
174 12327 : Address* new_next = impl_->GetSpareOrNewBlock();
175 12327 : Address* new_limit = &new_next[kHandleBlockSize];
176 : // Check that at least one HandleScope with at least one Handle in it exists,
177 : // see the class description.
178 : DCHECK(!impl_->blocks()->empty());
179 : // Check that we are not in a SealedHandleScope.
180 : DCHECK(data->limit == &impl_->blocks()->back()[kHandleBlockSize]);
181 12327 : impl_->blocks()->push_back(new_next);
182 :
183 : #ifdef DEBUG
184 : prev_level_ = data->level;
185 : #endif
186 12327 : data->level++;
187 12327 : prev_limit_ = data->limit;
188 12327 : prev_next_ = data->next;
189 12327 : data->next = new_next;
190 12327 : data->limit = new_limit;
191 12327 : }
192 :
193 :
194 12327 : DeferredHandleScope::~DeferredHandleScope() {
195 12327 : impl_->isolate()->handle_scope_data()->level--;
196 : DCHECK(handles_detached_);
197 : DCHECK(impl_->isolate()->handle_scope_data()->level == prev_level_);
198 12327 : }
199 :
200 :
201 12327 : DeferredHandles* DeferredHandleScope::Detach() {
202 24654 : DeferredHandles* deferred = impl_->Detach(prev_limit_);
203 12327 : HandleScopeData* data = impl_->isolate()->handle_scope_data();
204 12327 : data->next = prev_next_;
205 12327 : data->limit = prev_limit_;
206 : #ifdef DEBUG
207 : handles_detached_ = true;
208 : #endif
209 12327 : return deferred;
210 : }
211 :
212 : } // namespace internal
213 183867 : } // namespace v8
|