v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
pointer-policies.cc
Go to the documentation of this file.
1// Copyright 2020 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
6
8#include "src/base/logging.h"
9#include "src/base/macros.h"
13#include "src/heap/cppgc/heap.h"
17
18namespace cppgc {
19namespace internal {
20
21namespace {
22
23#if defined(DEBUG)
24bool IsOnStack(const void* address) {
25 return v8::base::Stack::GetCurrentStackPosition() <= address &&
27}
28#endif // defined(DEBUG)
29
30} // namespace
31
33 const void* ptr, bool points_to_payload, bool check_off_heap_assignments) {
34 // `ptr` must not reside on stack.
35 DCHECK(!IsOnStack(ptr));
36#if defined(CPPGC_CAGED_HEAP)
37 // `ptr` must reside in the cage.
38 DCHECK(CagedHeapBase::IsWithinCage(ptr));
39#endif // defined(CPPGC_CAGED_HEAP)
40 // Check for the most commonly used wrong sentinel value (-1).
41 DCHECK_NE(reinterpret_cast<void*>(-1), ptr);
42 auto* base_page = BasePage::FromPayload(ptr);
43 // Large objects do not support mixins. This also means that `base_page` is
44 // valid for large objects.
45 DCHECK_IMPLIES(base_page->is_large(), points_to_payload);
46
47 // References cannot change their heap association which means that state is
48 // immutable once it is set.
49 bool is_on_heap = true;
50 if (!heap_) {
51 heap_ = &base_page->heap();
52 if (!heap_->page_backend()->Lookup(reinterpret_cast<Address>(this))) {
53 // If `this` is not contained within the heap of `ptr`, we must deal with
54 // an on-stack or off-heap reference. For both cases there should be no
55 // heap registered.
56 is_on_heap = false;
58 }
59 }
60
61 // Member references should never mix heaps.
62 DCHECK_EQ(heap_, &base_page->heap());
63
64 DCHECK(heap_->CurrentThreadIsHeapThread());
65
66 // Header checks.
67 const HeapObjectHeader* header = nullptr;
68 if (points_to_payload) {
69 header = &HeapObjectHeader::FromObject(ptr);
71 header,
72 &base_page->ObjectHeaderFromInnerAddress<AccessMode::kAtomic>(ptr));
73 } else {
74 // Mixin case. Access the ObjectStartBitmap atomically since sweeping can be
75 // in progress.
76 header = &base_page->ObjectHeaderFromInnerAddress<AccessMode::kAtomic>(ptr);
77 DCHECK_LE(header->ObjectStart(), ptr);
79 }
80 if (header) {
81 DCHECK(!header->IsFree());
82 }
83
84#ifdef CPPGC_VERIFY_HEAP
85 if (check_off_heap_assignments || is_on_heap) {
86 if (heap_->prefinalizer_handler()->IsInvokingPreFinalizers()) {
87 // Slot can be in a large object.
88 const auto* slot_page = BasePage::FromInnerAddress(heap_, this);
89 // Off-heap slots (from other heaps or on-stack) are considered live.
90 bool slot_is_live =
91 !slot_page ||
92 slot_page->ObjectHeaderFromInnerAddress(this).IsMarked();
93 // During prefinalizers invocation, check that if the slot is live then
94 // |ptr| refers to a live object.
95 DCHECK_IMPLIES(slot_is_live, header->IsMarked());
96 USE(slot_is_live);
97 }
98 }
99#else
100 USE(is_on_heap);
101#endif // CPPGC_VERIFY_HEAP
102}
103
108
113
120
127
128} // namespace internal
129} // namespace cppgc
HeapBase & heap() const
Definition heap-page.cc:35
static BasePage * FromPayload(void *)
Definition heap-page.h:314
static BasePage * FromInnerAddress(const HeapBase *, void *)
Definition heap-page.cc:40
PersistentRegion & GetWeakPersistentRegion()
Definition heap-base.h:147
PersistentRegion & GetStrongPersistentRegion()
Definition heap-base.h:141
CrossThreadPersistentRegion & GetStrongCrossThreadPersistentRegion()
Definition heap-base.h:153
CrossThreadPersistentRegion & GetWeakCrossThreadPersistentRegion()
Definition heap-base.h:160
static HeapObjectHeader & FromObject(void *address)
static HeapBase * TryFromManagedPointer(const void *needle)
void CheckPointerImpl(const void *ptr, bool points_to_payload, bool check_off_heap_assignments)
static StackSlot GetStackStart()
static V8_NOINLINE StackSlot GetCurrentStackPosition()
uint8_t * Address
Definition globals.h:17
#define DCHECK_LE(v1, v2)
Definition logging.h:490
#define CHECK(condition)
Definition logging.h:124
#define DCHECK_IMPLIES(v1, v2)
Definition logging.h:493
#define DCHECK_NE(v1, v2)
Definition logging.h:486
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define DCHECK_GT(v1, v2)
Definition logging.h:487
#define USE(...)
Definition macros.h:293
static V8_EXPORT CrossThreadPersistentRegion & GetPersistentRegion(const void *object)
static V8_EXPORT PersistentRegion & GetPersistentRegion(const void *object)
static V8_EXPORT CrossThreadPersistentRegion & GetPersistentRegion(const void *object)
static V8_EXPORT PersistentRegion & GetPersistentRegion(const void *object)