• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/handles.h"
6 
7 #include "src/api/api.h"
8 #include "src/base/logging.h"
9 #include "src/codegen/optimized-compilation-info.h"
10 #include "src/handles/maybe-handles.h"
11 #include "src/objects/objects-inl.h"
12 #include "src/roots/roots-inl.h"
13 #include "src/utils/address-map.h"
14 #include "src/utils/identity-map.h"
15 
16 #ifdef DEBUG
17 // For GetIsolateFromWritableHeapObject.
18 #include "src/heap/heap-write-barrier-inl.h"
19 #endif
20 
21 namespace v8 {
22 namespace internal {
23 
24 // Handles should be trivially copyable so that they can be efficiently passed
25 // by value. If they are not trivially copyable, they cannot be passed in
26 // registers.
27 ASSERT_TRIVIALLY_COPYABLE(HandleBase);
28 ASSERT_TRIVIALLY_COPYABLE(Handle<Object>);
29 ASSERT_TRIVIALLY_COPYABLE(MaybeHandle<Object>);
30 
31 #ifdef DEBUG
IsDereferenceAllowed() const32 bool HandleBase::IsDereferenceAllowed() const {
33   DCHECK_NOT_NULL(location_);
34   Object object(*location_);
35   if (object.IsSmi()) return true;
36   HeapObject heap_object = HeapObject::cast(object);
37   if (IsReadOnlyHeapObject(heap_object)) return true;
38   Isolate* isolate = GetIsolateFromWritableObject(heap_object);
39   RootIndex root_index;
40   if (isolate->roots_table().IsRootHandleLocation(location_, &root_index) &&
41       RootsTable::IsImmortalImmovable(root_index)) {
42     return true;
43   }
44   if (isolate->IsBuiltinsTableHandleLocation(location_)) return true;
45 
46   LocalHeap* local_heap = LocalHeap::Current();
47   if (FLAG_local_heaps && local_heap) {
48     // Local heap can't access handles when parked
49     if (!local_heap->IsHandleDereferenceAllowed()) return false;
50 
51     if (local_heap->ContainsPersistentHandle(location_) ||
52         local_heap->ContainsLocalHandle(location_)) {
53       // The current thread owns the handle and thus can dereference it.
54       return true;
55     }
56   }
57 
58   return AllowHandleDereference::IsAllowed();
59 }
60 #endif
61 
NumberOfHandles(Isolate * isolate)62 int HandleScope::NumberOfHandles(Isolate* isolate) {
63   HandleScopeImplementer* impl = isolate->handle_scope_implementer();
64   int n = static_cast<int>(impl->blocks()->size());
65   if (n == 0) return 0;
66   return ((n - 1) * kHandleBlockSize) +
67          static_cast<int>(
68              (isolate->handle_scope_data()->next - impl->blocks()->back()));
69 }
70 
Extend(Isolate * isolate)71 Address* HandleScope::Extend(Isolate* isolate) {
72   HandleScopeData* current = isolate->handle_scope_data();
73 
74   Address* result = current->next;
75 
76   DCHECK(result == current->limit);
77   // Make sure there's at least one scope on the stack and that the
78   // top of the scope stack isn't a barrier.
79   if (!Utils::ApiCheck(current->level != current->sealed_level,
80                        "v8::HandleScope::CreateHandle()",
81                        "Cannot create a handle without a HandleScope")) {
82     return nullptr;
83   }
84   HandleScopeImplementer* impl = isolate->handle_scope_implementer();
85   // If there's more room in the last block, we use that. This is used
86   // for fast creation of scopes after scope barriers.
87   if (!impl->blocks()->empty()) {
88     Address* limit = &impl->blocks()->back()[kHandleBlockSize];
89     if (current->limit != limit) {
90       current->limit = limit;
91       DCHECK_LT(limit - current->next, kHandleBlockSize);
92     }
93   }
94 
95   // If we still haven't found a slot for the handle, we extend the
96   // current handle scope by allocating a new handle block.
97   if (result == current->limit) {
98     // If there's a spare block, use it for growing the current scope.
99     result = impl->GetSpareOrNewBlock();
100     // Add the extension to the global list of blocks, but count the
101     // extension as part of the current scope.
102     impl->blocks()->push_back(result);
103     current->limit = &result[kHandleBlockSize];
104   }
105 
106   return result;
107 }
108 
DeleteExtensions(Isolate * isolate)109 void HandleScope::DeleteExtensions(Isolate* isolate) {
110   HandleScopeData* current = isolate->handle_scope_data();
111   isolate->handle_scope_implementer()->DeleteExtensions(current->limit);
112 }
113 
114 #ifdef ENABLE_HANDLE_ZAPPING
ZapRange(Address * start,Address * end)115 void HandleScope::ZapRange(Address* start, Address* end) {
116   DCHECK_LE(end - start, kHandleBlockSize);
117   for (Address* p = start; p != end; p++) {
118     *p = static_cast<Address>(kHandleZapValue);
119   }
120 }
121 #endif
122 
current_level_address(Isolate * isolate)123 Address HandleScope::current_level_address(Isolate* isolate) {
124   return reinterpret_cast<Address>(&isolate->handle_scope_data()->level);
125 }
126 
current_next_address(Isolate * isolate)127 Address HandleScope::current_next_address(Isolate* isolate) {
128   return reinterpret_cast<Address>(&isolate->handle_scope_data()->next);
129 }
130 
current_limit_address(Isolate * isolate)131 Address HandleScope::current_limit_address(Isolate* isolate) {
132   return reinterpret_cast<Address>(&isolate->handle_scope_data()->limit);
133 }
134 
CanonicalHandleScope(Isolate * isolate,OptimizedCompilationInfo * info)135 CanonicalHandleScope::CanonicalHandleScope(Isolate* isolate,
136                                            OptimizedCompilationInfo* info)
137     : isolate_(isolate),
138       info_(info),
139       zone_(info ? info->zone() : new Zone(isolate->allocator(), ZONE_NAME)) {
140   HandleScopeData* handle_scope_data = isolate_->handle_scope_data();
141   prev_canonical_scope_ = handle_scope_data->canonical_scope;
142   handle_scope_data->canonical_scope = this;
143   root_index_map_ = new RootIndexMap(isolate);
144   identity_map_ = std::make_unique<CanonicalHandlesMap>(
145       isolate->heap(), ZoneAllocationPolicy(zone_));
146   canonical_level_ = handle_scope_data->level;
147 }
148 
~CanonicalHandleScope()149 CanonicalHandleScope::~CanonicalHandleScope() {
150   delete root_index_map_;
151   if (info_) {
152     // If we passed a compilation info as parameter, we created the identity map
153     // on its zone(). Then, we pass it to the compilation info which is
154     // responsible for the disposal.
155     info_->set_canonical_handles(DetachCanonicalHandles());
156   } else {
157     // If we don't have a compilation info, we created the zone manually. To
158     // properly dispose of said zone, we need to first free the identity_map_.
159     // Then we do so manually even though identity_map_ is a unique_ptr.
160     identity_map_.reset();
161     delete zone_;
162   }
163   isolate_->handle_scope_data()->canonical_scope = prev_canonical_scope_;
164 }
165 
Lookup(Address object)166 Address* CanonicalHandleScope::Lookup(Address object) {
167   DCHECK_LE(canonical_level_, isolate_->handle_scope_data()->level);
168   if (isolate_->handle_scope_data()->level != canonical_level_) {
169     // We are in an inner handle scope. Do not canonicalize since we will leave
170     // this handle scope while still being in the canonical scope.
171     return HandleScope::CreateHandle(isolate_, object);
172   }
173   if (Internals::HasHeapObjectTag(object)) {
174     RootIndex root_index;
175     if (root_index_map_->Lookup(object, &root_index)) {
176       return isolate_->root_handle(root_index).location();
177     }
178   }
179   auto find_result = identity_map_->FindOrInsert(Object(object));
180   if (!find_result.already_exists) {
181     // Allocate new handle location.
182     *find_result.entry = HandleScope::CreateHandle(isolate_, object);
183   }
184   return *find_result.entry;
185 }
186 
187 std::unique_ptr<CanonicalHandlesMap>
DetachCanonicalHandles()188 CanonicalHandleScope::DetachCanonicalHandles() {
189   return std::move(identity_map_);
190 }
191 
192 }  // namespace internal
193 }  // namespace v8
194