v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
read-only-heap.h
Go to the documentation of this file.
1// Copyright 2019 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#ifndef V8_HEAP_READ_ONLY_HEAP_H_
6#define V8_HEAP_READ_ONLY_HEAP_H_
7
8#include <memory>
9#include <utility>
10#include <vector>
11
12#include "src/base/macros.h"
14#include "src/objects/objects.h"
15#include "src/roots/roots.h"
18
19namespace v8 {
20
21class SharedMemoryStatistics;
22
23namespace internal {
24
25class Isolate;
26class PageMetadata;
27class ReadOnlyArtifacts;
28class ReadOnlyPageMetadata;
29class ReadOnlySpace;
30class SharedReadOnlySpace;
31class SnapshotData;
32
33// This class transparently manages read-only space, roots and cache creation
34// and destruction.
35class ReadOnlyHeap final {
36 public:
37 static constexpr size_t kEntriesCount =
38 static_cast<size_t>(RootIndex::kReadOnlyRootsCount);
39
40 explicit ReadOnlyHeap(ReadOnlySpace* ro_space);
42
43 ReadOnlyHeap(const ReadOnlyHeap&) = delete;
45
46 // If necessary creates read-only heap and initializes its artifacts (if the
47 // deserializer is provided). Then attaches the read-only heap to the isolate.
48 // If the deserializer is not provided, then the read-only heap will be only
49 // finish initializing when initial heap object creation in the Isolate is
50 // completed, which is signalled by calling OnCreateHeapObjectsComplete.
51 // A lock will be held until that method is called.
52 // TODO(v8:7464): Ideally we'd create this without needing a heap.
53 static void SetUp(Isolate* isolate, SnapshotData* read_only_snapshot_data,
54 bool can_rehash);
55
56 // Indicates that the isolate has been set up and all read-only space objects
57 // have been created and will not be written to. This should only be called if
58 // a deserializer was not previously provided to Setup. This releases the
59 // ReadOnlyHeap creation lock.
61 // Indicates that all objects reachable by the read only roots table have been
62 // set up.
63 void OnCreateRootsComplete(Isolate* isolate);
64 // If the read-only heap is shared, then populate |statistics| with its stats,
65 // otherwise the read-only heap stats are set to 0.
67 SharedMemoryStatistics* statistics);
68
69 // Returns whether the address is within the read-only space.
70 V8_EXPORT_PRIVATE static bool Contains(Address address);
71 // Returns whether the object resides in the read-only space.
74 // Returns the current isolates roots table during initialization as opposed
75 // to the shared one in case the latter is not initialized yet.
77 Tagged<HeapObject> object);
78
80
81#ifdef V8_ENABLE_SANDBOX
82 CodePointerTable::Space* code_pointer_space() { return &code_pointer_space_; }
83#endif // V8_ENABLE_SANDBOX
84#ifdef V8_ENABLE_LEAPTIERING
85 JSDispatchTable::Space* js_dispatch_table_space() {
86 return &js_dispatch_table_space_;
87 }
88#endif // V8_ENABLE_LEAPTIERING
89
90 void InitializeIsolateRoots(Isolate* isolate);
91 void InitializeFromIsolateRoots(Isolate* isolate);
92
94
95 protected:
96 friend class ReadOnlyArtifacts;
97
98 // Creates a new read-only heap and attaches it to the provided isolate. Only
99 // used the first time when creating a ReadOnlyHeap for sharing.
100 static void CreateInitialHeapForBootstrapping(Isolate* isolate,
101 ReadOnlyArtifacts* artifacts);
102 // Runs the read-only deserializer and calls InitFromIsolate to complete
103 // read-only heap initialization.
104 void DeserializeIntoIsolate(Isolate* isolate,
105 SnapshotData* read_only_snapshot_data,
106 bool can_rehash);
107 // Initializes read-only heap from an already set-up isolate, copying
108 // read-only roots from the isolate. This then seals the space off from
109 // further writes, marks it as read-only and detaches it from the heap
110 // (unless sharing is disabled).
111 void InitFromIsolate(Isolate* isolate);
112
115
116#ifdef V8_ENABLE_SANDBOX
117 // The read-only heap has its own code pointer space. Entries in this space
118 // are never deallocated.
119 CodePointerTable::Space code_pointer_space_;
120#endif // V8_ENABLE_SANDBOX
121#ifdef V8_ENABLE_LEAPTIERING
122 JSDispatchTable::Space js_dispatch_table_space_;
123#endif // V8_ENABLE_LEAPTIERING
124
125 private:
128};
129
131 kYes,
132 kNo,
133};
134
135// This class enables iterating over all read-only heap objects on a
136// ReadOnlyPage.
138 public:
140 const ReadOnlyPageMetadata* page,
141 SkipFreeSpaceOrFiller skip_free_space_or_filler =
142 SkipFreeSpaceOrFiller::kYes);
144 Address current_addr,
145 SkipFreeSpaceOrFiller skip_free_space_or_filler =
146 SkipFreeSpaceOrFiller::kYes);
147
148 Tagged<HeapObject> Next();
149
150 private:
151 void Reset(const ReadOnlyPageMetadata* page);
152
156
158};
159
160// This class enables iterating over all read-only heap objects in the
161// ReadOnlyHeap/ReadOnlySpace.
163 public:
164 explicit ReadOnlyHeapObjectIterator(const ReadOnlyHeap* ro_heap);
165 explicit ReadOnlyHeapObjectIterator(const ReadOnlySpace* ro_space);
166
167 Tagged<HeapObject> Next();
168
169 private:
171 std::vector<ReadOnlyPageMetadata*>::const_iterator current_page_;
173};
174
175} // namespace internal
176} // namespace v8
177
178#endif // V8_HEAP_READ_ONLY_HEAP_H_
std::vector< ReadOnlyPageMetadata * >::const_iterator current_page_
ReadOnlyPageObjectIterator page_iterator_
static void PopulateReadOnlySpaceStatistics(SharedMemoryStatistics *statistics)
void DeserializeIntoIsolate(Isolate *isolate, SnapshotData *read_only_snapshot_data, bool can_rehash)
static V8_EXPORT_PRIVATE bool Contains(Address address)
ReadOnlyHeap(const ReadOnlyHeap &)=delete
void InitializeFromIsolateRoots(Isolate *isolate)
Address read_only_roots_[kEntriesCount]
static constexpr size_t kEntriesCount
static V8_EXPORT_PRIVATE bool SandboxSafeContains(Tagged< HeapObject > object)
static V8_EXPORT_PRIVATE ReadOnlyRoots EarlyGetReadOnlyRoots(Tagged< HeapObject > object)
static void SetUp(Isolate *isolate, SnapshotData *read_only_snapshot_data, bool can_rehash)
V8_EXPORT_PRIVATE void OnCreateHeapObjectsComplete(Isolate *isolate)
ReadOnlySpace * read_only_space_
friend ReadOnlyRoots GetReadOnlyRoots()
Definition roots-inl.h:86
void InitFromIsolate(Isolate *isolate)
void InitializeIsolateRoots(Isolate *isolate)
ReadOnlyHeap(ReadOnlySpace *ro_space)
static void CreateInitialHeapForBootstrapping(Isolate *isolate, ReadOnlyArtifacts *artifacts)
ReadOnlyHeap & operator=(const ReadOnlyHeap &)=delete
void OnCreateRootsComplete(Isolate *isolate)
ReadOnlySpace * read_only_space() const
const SkipFreeSpaceOrFiller skip_free_space_or_filler_
const ReadOnlyPageMetadata * page_
#define V8_EXPORT_PRIVATE
Definition macros.h:460