Line data Source code
1 : // Copyright 2018 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_ISOLATE_DATA_H_
6 : #define V8_ISOLATE_DATA_H_
7 :
8 : #include "src/builtins/builtins.h"
9 : #include "src/constants-arch.h"
10 : #include "src/external-reference-table.h"
11 : #include "src/roots.h"
12 : #include "src/thread-local-top.h"
13 : #include "src/utils.h"
14 :
15 : namespace v8 {
16 : namespace internal {
17 :
18 : class Isolate;
19 :
20 : // This class contains a collection of data accessible from both C++ runtime
21 : // and compiled code (including assembly stubs, builtins, interpreter bytecode
22 : // handlers and optimized code).
23 : // In particular, it contains pointer to the V8 heap roots table, external
24 : // reference table and builtins array.
25 : // The compiled code accesses the isolate data fields indirectly via the root
26 : // register.
27 : class IsolateData final {
28 : public:
29 122098 : IsolateData() = default;
30 :
31 : static constexpr intptr_t kIsolateRootBias = kRootRegisterBias;
32 :
33 : // The value of the kRootRegister.
34 : Address isolate_root() const {
35 24457387 : return reinterpret_cast<Address>(this) + kIsolateRootBias;
36 : }
37 :
38 : // Root-register-relative offset of the roots table.
39 : static constexpr int roots_table_offset() {
40 : return kRootsTableOffset - kIsolateRootBias;
41 : }
42 :
43 : // Root-register-relative offset of the given root table entry.
44 : static constexpr int root_slot_offset(RootIndex root_index) {
45 3441671 : return roots_table_offset() + RootsTable::offset_of(root_index);
46 : }
47 :
48 : // Root-register-relative offset of the external reference table.
49 : static constexpr int external_reference_table_offset() {
50 : return kExternalReferenceTableOffset - kIsolateRootBias;
51 : }
52 :
53 : // Root-register-relative offset of the builtin entry table.
54 : static constexpr int builtin_entry_table_offset() {
55 : return kBuiltinEntryTableOffset - kIsolateRootBias;
56 : }
57 :
58 : // Root-register-relative offset of the builtins table.
59 504 : static constexpr int builtins_table_offset() {
60 504 : return kBuiltinsTableOffset - kIsolateRootBias;
61 : }
62 :
63 : // Root-register-relative offset of the given builtin table entry.
64 : // TODO(ishell): remove in favour of typified id version.
65 : static int builtin_slot_offset(int builtin_index) {
66 : DCHECK(Builtins::IsBuiltinId(builtin_index));
67 9470 : return builtins_table_offset() + builtin_index * kSystemPointerSize;
68 : }
69 :
70 : // Root-register-relative offset of the builtin table entry.
71 504 : static int builtin_slot_offset(Builtins::Name id) {
72 504 : return builtins_table_offset() + id * kSystemPointerSize;
73 : }
74 :
75 : // Root-register-relative offset of the virtual call target register value.
76 : static constexpr int virtual_call_target_register_offset() {
77 : return kVirtualCallTargetRegisterOffset - kIsolateRootBias;
78 : }
79 :
80 : // The FP and PC that are saved right before TurboAssembler::CallCFunction.
81 : Address* fast_c_call_caller_fp_address() { return &fast_c_call_caller_fp_; }
82 : Address* fast_c_call_caller_pc_address() { return &fast_c_call_caller_pc_; }
83 : Address fast_c_call_caller_fp() { return fast_c_call_caller_fp_; }
84 : Address fast_c_call_caller_pc() { return fast_c_call_caller_pc_; }
85 :
86 : // Returns true if this address points to data stored in this instance.
87 : // If it's the case then the value can be accessed indirectly through the
88 : // root register.
89 : bool contains(Address address) const {
90 : STATIC_ASSERT(std::is_unsigned<Address>::value);
91 : Address start = reinterpret_cast<Address>(this);
92 : return (address - start) < sizeof(*this);
93 : }
94 :
95 : ThreadLocalTop& thread_local_top() { return thread_local_top_; }
96 : ThreadLocalTop const& thread_local_top() const { return thread_local_top_; }
97 :
98 69674 : RootsTable& roots() { return roots_; }
99 : const RootsTable& roots() const { return roots_; }
100 :
101 : ExternalReferenceTable* external_reference_table() {
102 : return &external_reference_table_;
103 : }
104 :
105 : Address* builtin_entry_table() { return builtin_entry_table_; }
106 616643165 : Address* builtins() { return builtins_; }
107 :
108 : private:
109 : // Static layout definition.
110 : #define FIELDS(V) \
111 : V(kEmbedderDataOffset, Internals::kNumIsolateDataSlots* kSystemPointerSize) \
112 : V(kExternalMemoryOffset, kInt64Size) \
113 : V(kExternalMemoryLlimitOffset, kInt64Size) \
114 : V(kExternalMemoryAtLastMarkCompactOffset, kInt64Size) \
115 : V(kRootsTableOffset, RootsTable::kEntriesCount* kSystemPointerSize) \
116 : V(kExternalReferenceTableOffset, ExternalReferenceTable::kSizeInBytes) \
117 : V(kThreadLocalTopOffset, ThreadLocalTop::kSizeInBytes) \
118 : V(kBuiltinEntryTableOffset, Builtins::builtin_count* kSystemPointerSize) \
119 : V(kBuiltinsTableOffset, Builtins::builtin_count* kSystemPointerSize) \
120 : V(kVirtualCallTargetRegisterOffset, kSystemPointerSize) \
121 : V(kFastCCallCallerFPOffset, kSystemPointerSize) \
122 : V(kFastCCallCallerPCOffset, kSystemPointerSize) \
123 : /* This padding aligns IsolateData size by 8 bytes. */ \
124 : V(kPaddingOffset, \
125 : 8 + RoundUp<8>(static_cast<int>(kPaddingOffset)) - kPaddingOffset) \
126 : /* Total size. */ \
127 : V(kSize, 0)
128 :
129 : DEFINE_FIELD_OFFSET_CONSTANTS(0, FIELDS)
130 : #undef FIELDS
131 :
132 : // These fields are accessed through the API, offsets must be kept in sync
133 : // with v8::internal::Internals (in include/v8-internal.h) constants.
134 : // The layout consitency is verified in Isolate::CheckIsolateLayout() using
135 : // runtime checks.
136 : void* embedder_data_[Internals::kNumIsolateDataSlots] = {};
137 :
138 : // TODO(ishell): Move these external memory counters back to Heap once the
139 : // Node JS bot issue is solved.
140 : // The amount of external memory registered through the API.
141 : int64_t external_memory_ = 0;
142 :
143 : // The limit when to trigger memory pressure from the API.
144 : int64_t external_memory_limit_ = kExternalAllocationSoftLimit;
145 :
146 : // Caches the amount of external memory registered at the last MC.
147 : int64_t external_memory_at_last_mark_compact_ = 0;
148 :
149 : RootsTable roots_;
150 :
151 : ExternalReferenceTable external_reference_table_;
152 :
153 : ThreadLocalTop thread_local_top_;
154 :
155 : // The entry points for all builtins. This corresponds to
156 : // Code::InstructionStart() for each Code object in the builtins table below.
157 : // The entry table is in IsolateData for easy access through kRootRegister.
158 : Address builtin_entry_table_[Builtins::builtin_count] = {};
159 :
160 : // The entries in this array are tagged pointers to Code objects.
161 : Address builtins_[Builtins::builtin_count] = {};
162 :
163 : // For isolate-independent calls on ia32.
164 : // TODO(v8:6666): Remove once wasm supports pc-relative jumps to builtins on
165 : // ia32 (otherwise the arguments adaptor call runs out of registers).
166 : void* virtual_call_target_register_ = nullptr;
167 :
168 : // Stores the state of the caller for TurboAssembler::CallCFunction so that
169 : // the sampling CPU profiler can iterate the stack during such calls. These
170 : // are stored on IsolateData so that they can be stored to with only one move
171 : // instruction in compiled code.
172 : Address fast_c_call_caller_fp_ = kNullAddress;
173 : Address fast_c_call_caller_pc_ = kNullAddress;
174 :
175 : // Ensure the size is 8-byte aligned in order to make alignment of the field
176 : // following the IsolateData field predictable. This solves the issue with
177 : // C++ compilers for 32-bit platforms which are not consistent at aligning
178 : // int64_t fields.
179 : // In order to avoid dealing with zero-size arrays the padding size is always
180 : // in the range [8, 15).
181 : STATIC_ASSERT(kPaddingOffsetEnd + 1 - kPaddingOffset >= 8);
182 : char padding_[kPaddingOffsetEnd + 1 - kPaddingOffset];
183 :
184 : V8_INLINE static void AssertPredictableLayout();
185 :
186 : friend class Isolate;
187 : friend class Heap;
188 : FRIEND_TEST(HeapTest, ExternalLimitDefault);
189 : FRIEND_TEST(HeapTest, ExternalLimitStaysAboveDefaultForExplicitHandling);
190 :
191 : DISALLOW_COPY_AND_ASSIGN(IsolateData);
192 : };
193 :
194 : // IsolateData object must have "predictable" layout which does not change when
195 : // cross-compiling to another platform. Otherwise there may be compatibility
196 : // issues because of different compilers used for snapshot generator and
197 : // actual V8 code.
198 : void IsolateData::AssertPredictableLayout() {
199 : STATIC_ASSERT(std::is_standard_layout<RootsTable>::value);
200 : STATIC_ASSERT(std::is_standard_layout<ThreadLocalTop>::value);
201 : STATIC_ASSERT(std::is_standard_layout<ExternalReferenceTable>::value);
202 : STATIC_ASSERT(std::is_standard_layout<IsolateData>::value);
203 : STATIC_ASSERT(offsetof(IsolateData, roots_) == kRootsTableOffset);
204 : STATIC_ASSERT(offsetof(IsolateData, external_reference_table_) ==
205 : kExternalReferenceTableOffset);
206 : STATIC_ASSERT(offsetof(IsolateData, thread_local_top_) ==
207 : kThreadLocalTopOffset);
208 : STATIC_ASSERT(offsetof(IsolateData, builtins_) == kBuiltinsTableOffset);
209 : STATIC_ASSERT(offsetof(IsolateData, virtual_call_target_register_) ==
210 : kVirtualCallTargetRegisterOffset);
211 : STATIC_ASSERT(offsetof(IsolateData, external_memory_) ==
212 : kExternalMemoryOffset);
213 : STATIC_ASSERT(offsetof(IsolateData, external_memory_limit_) ==
214 : kExternalMemoryLlimitOffset);
215 : STATIC_ASSERT(offsetof(IsolateData, external_memory_at_last_mark_compact_) ==
216 : kExternalMemoryAtLastMarkCompactOffset);
217 : STATIC_ASSERT(offsetof(IsolateData, fast_c_call_caller_fp_) ==
218 : kFastCCallCallerFPOffset);
219 : STATIC_ASSERT(offsetof(IsolateData, fast_c_call_caller_pc_) ==
220 : kFastCCallCallerPCOffset);
221 : STATIC_ASSERT(sizeof(IsolateData) == IsolateData::kSize);
222 : }
223 :
224 : } // namespace internal
225 : } // namespace v8
226 :
227 : #endif // V8_ISOLATE_DATA_H_
|