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