LCOV - code coverage report
Current view: top level - src - handles.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 78 78 100.0 %
Date: 2019-02-19 Functions: 15 15 100.0 %

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

Generated by: LCOV version 1.10