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