handles.cc 7.23 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// 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/handles/maybe-handles.h"
10
#include "src/objects/objects-inl.h"
11
#include "src/roots/roots-inl.h"
12 13
#include "src/utils/address-map.h"
#include "src/utils/identity-map.h"
14

15 16 17 18 19
#ifdef DEBUG
// For GetIsolateFromWritableHeapObject.
#include "src/heap/heap-write-barrier-inl.h"
#endif

20 21
namespace v8 {
namespace internal {
22

23 24 25
// 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.
26 27 28
ASSERT_TRIVIALLY_COPYABLE(HandleBase);
ASSERT_TRIVIALLY_COPYABLE(Handle<Object>);
ASSERT_TRIVIALLY_COPYABLE(MaybeHandle<Object>);
29

30
#ifdef DEBUG
31
bool HandleBase::IsDereferenceAllowed() const {
32
  DCHECK_NOT_NULL(location_);
33
  Object object(*location_);
34
  if (object.IsSmi()) return true;
35
  HeapObject heap_object = HeapObject::cast(object);
36
  if (IsReadOnlyHeapObject(heap_object)) return true;
37
  if (Heap::InOffThreadSpace(heap_object)) return true;
38 39 40 41 42 43 44
  Isolate* isolate = GetIsolateFromWritableObject(heap_object);
  RootIndex root_index;
  if (isolate->roots_table().IsRootHandleLocation(location_, &root_index) &&
      RootsTable::IsImmortalImmovable(root_index)) {
    return true;
  }

45 46
  LocalHeap* local_heap = LocalHeap::Current();
  if (V8_UNLIKELY(local_heap)) {
47 48 49
    // Local heap can't access handles when parked
    if (!local_heap->IsHandleDereferenceAllowed()) return false;

50 51
    if (local_heap->ContainsPersistentHandle(location_) ||
        local_heap->ContainsLocalHandle(location_)) {
52 53 54 55
      // The current thread owns the handle and thus can dereference it.
      return true;
    }
  }
56

57
  return AllowHandleDereference::IsAllowed();
58 59 60
}
#endif

61
int HandleScope::NumberOfHandles(Isolate* isolate) {
62
  HandleScopeImplementer* impl = isolate->handle_scope_implementer();
63
  int n = static_cast<int>(impl->blocks()->size());
64
  if (n == 0) return 0;
65 66 67
  return ((n - 1) * kHandleBlockSize) +
         static_cast<int>(
             (isolate->handle_scope_data()->next - impl->blocks()->back()));
68 69
}

70
Address* HandleScope::Extend(Isolate* isolate) {
71
  HandleScopeData* current = isolate->handle_scope_data();
72

73
  Address* result = current->next;
74 75

  DCHECK(result == current->limit);
76 77
  // Make sure there's at least one scope on the stack and that the
  // top of the scope stack isn't a barrier.
78
  if (!Utils::ApiCheck(current->level != current->sealed_level,
79 80
                       "v8::HandleScope::CreateHandle()",
                       "Cannot create a handle without a HandleScope")) {
81
    return nullptr;
82
  }
83
  HandleScopeImplementer* impl = isolate->handle_scope_implementer();
84 85
  // If there's more room in the last block, we use that. This is used
  // for fast creation of scopes after scope barriers.
86
  if (!impl->blocks()->empty()) {
87
    Address* limit = &impl->blocks()->back()[kHandleBlockSize];
88 89
    if (current->limit != limit) {
      current->limit = limit;
90
      DCHECK_LT(limit - current->next, kHandleBlockSize);
91 92
    }
  }
93

94 95
  // If we still haven't found a slot for the handle, we extend the
  // current handle scope by allocating a new handle block.
96
  if (result == current->limit) {
97 98 99 100
    // 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.
101
    impl->blocks()->push_back(result);
102
    current->limit = &result[kHandleBlockSize];
103
  }
104 105 106 107

  return result;
}

108 109 110 111 112
void HandleScope::DeleteExtensions(Isolate* isolate) {
  HandleScopeData* current = isolate->handle_scope_data();
  isolate->handle_scope_implementer()->DeleteExtensions(current->limit);
}

113
#ifdef ENABLE_HANDLE_ZAPPING
114
void HandleScope::ZapRange(Address* start, Address* end) {
115
  DCHECK_LE(end - start, kHandleBlockSize);
116 117
  for (Address* p = start; p != end; p++) {
    *p = static_cast<Address>(kHandleZapValue);
118 119
  }
}
120
#endif
121

122 123
Address HandleScope::current_level_address(Isolate* isolate) {
  return reinterpret_cast<Address>(&isolate->handle_scope_data()->level);
124 125
}

126 127
Address HandleScope::current_next_address(Isolate* isolate) {
  return reinterpret_cast<Address>(&isolate->handle_scope_data()->next);
128 129
}

130 131
Address HandleScope::current_limit_address(Isolate* isolate) {
  return reinterpret_cast<Address>(&isolate->handle_scope_data()->limit);
132 133
}

134
CanonicalHandleScope::CanonicalHandleScope(Isolate* isolate)
135
    : isolate_(isolate), zone_(isolate->allocator(), ZONE_NAME) {
136 137 138 139
  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);
140
  identity_map_ = new IdentityMap<Address*, ZoneAllocationPolicy>(
141
      isolate->heap(), ZoneAllocationPolicy(&zone_));
142 143 144 145 146 147 148 149 150
  canonical_level_ = handle_scope_data->level;
}

CanonicalHandleScope::~CanonicalHandleScope() {
  delete root_index_map_;
  delete identity_map_;
  isolate_->handle_scope_data()->canonical_scope = prev_canonical_scope_;
}

151
Address* CanonicalHandleScope::Lookup(Address object) {
152 153 154 155 156 157
  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);
  }
158
  if (Internals::HasHeapObjectTag(object)) {
159
    RootIndex root_index;
160
    if (root_index_map_->Lookup(object, &root_index)) {
161
      return isolate_->root_handle(root_index).location();
162 163
    }
  }
164
  Address** entry = identity_map_->Get(Object(object));
165 166 167 168
  if (*entry == nullptr) {
    // Allocate new handle location.
    *entry = HandleScope::CreateHandle(isolate_, object);
  }
169
  return *entry;
170 171
}

172 173 174
DeferredHandleScope::DeferredHandleScope(Isolate* isolate)
    : impl_(isolate->handle_scope_implementer()) {
  impl_->BeginDeferredScope();
175
  HandleScopeData* data = impl_->isolate()->handle_scope_data();
176 177
  Address* new_next = impl_->GetSpareOrNewBlock();
  Address* new_limit = &new_next[kHandleBlockSize];
178 179
  // Check that at least one HandleScope with at least one Handle in it exists,
  // see the class description.
180
  DCHECK(!impl_->blocks()->empty());
181
  // Check that we are not in a SealHandleScope.
182 183
  DCHECK(data->limit == &impl_->blocks()->back()[kHandleBlockSize]);
  impl_->blocks()->push_back(new_next);
184 185 186 187 188 189 190 191 192 193 194 195

#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() {
196
  DCHECK(handles_detached_);
197 198
  impl_->isolate()->handle_scope_data()->level--;
  DCHECK_EQ(impl_->isolate()->handle_scope_data()->level, prev_level_);
199 200
}

201 202
std::unique_ptr<DeferredHandles> DeferredHandleScope::Detach() {
  std::unique_ptr<DeferredHandles> deferred = impl_->Detach(prev_limit_);
203
  HandleScopeData* data = impl_->isolate()->handle_scope_data();
204 205 206 207 208 209 210 211
  data->next = prev_next_;
  data->limit = prev_limit_;
#ifdef DEBUG
  handles_detached_ = true;
#endif
  return deferred;
}

212 213
}  // namespace internal
}  // namespace v8