• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_OBJECTS_EMBEDDER_DATA_SLOT_INL_H_
6 #define V8_OBJECTS_EMBEDDER_DATA_SLOT_INL_H_
7 
8 #include "src/objects/embedder-data-slot.h"
9 
10 #include "src/base/memory.h"
11 #include "src/heap/heap-write-barrier-inl.h"
12 #include "src/objects/embedder-data-array.h"
13 #include "src/objects/js-objects-inl.h"
14 #include "src/objects/objects-inl.h"
15 
16 // Has to be the last include (doesn't have include guards):
17 #include "src/objects/object-macros.h"
18 
19 namespace v8 {
20 namespace internal {
21 
EmbedderDataSlot(EmbedderDataArray array,int entry_index)22 EmbedderDataSlot::EmbedderDataSlot(EmbedderDataArray array, int entry_index)
23     : SlotBase(FIELD_ADDR(array,
24                           EmbedderDataArray::OffsetOfElementAt(entry_index))) {}
25 
EmbedderDataSlot(JSObject object,int embedder_field_index)26 EmbedderDataSlot::EmbedderDataSlot(JSObject object, int embedder_field_index)
27     : SlotBase(FIELD_ADDR(
28           object, object.GetEmbedderFieldOffset(embedder_field_index))) {}
29 
AllocateExternalPointerEntry(Isolate * isolate)30 void EmbedderDataSlot::AllocateExternalPointerEntry(Isolate* isolate) {
31 #ifdef V8_HEAP_SANDBOX
32   // TODO(v8:10391, saelo): Use InitExternalPointerField() once
33   // ExternalPointer_t is 4-bytes.
34   uint32_t index = isolate->external_pointer_table().allocate();
35   // Object slots don't support storing raw values, so we just "reinterpret
36   // cast" the index value to Object.
37   Object index_as_object(index);
38   ObjectSlot(address() + kRawPayloadOffset).Relaxed_Store(index_as_object);
39   ObjectSlot(address() + kTaggedPayloadOffset).Relaxed_Store(Smi::zero());
40 #endif
41 }
42 
load_tagged()43 Object EmbedderDataSlot::load_tagged() const {
44   return ObjectSlot(address() + kTaggedPayloadOffset).Relaxed_Load();
45 }
46 
store_smi(Smi value)47 void EmbedderDataSlot::store_smi(Smi value) {
48   ObjectSlot(address() + kTaggedPayloadOffset).Relaxed_Store(value);
49 #ifdef V8_COMPRESS_POINTERS
50   // See gc_safe_store() for the reasons behind two stores.
51   ObjectSlot(address() + kRawPayloadOffset).Relaxed_Store(Smi::zero());
52 #endif
53 }
54 
55 // static
store_tagged(EmbedderDataArray array,int entry_index,Object value)56 void EmbedderDataSlot::store_tagged(EmbedderDataArray array, int entry_index,
57                                     Object value) {
58   int slot_offset = EmbedderDataArray::OffsetOfElementAt(entry_index);
59   ObjectSlot(FIELD_ADDR(array, slot_offset + kTaggedPayloadOffset))
60       .Relaxed_Store(value);
61   WRITE_BARRIER(array, slot_offset + kTaggedPayloadOffset, value);
62 #ifdef V8_COMPRESS_POINTERS
63   // See gc_safe_store() for the reasons behind two stores.
64   ObjectSlot(FIELD_ADDR(array, slot_offset + kRawPayloadOffset))
65       .Relaxed_Store(Smi::zero());
66 #endif
67 }
68 
69 // static
store_tagged(JSObject object,int embedder_field_index,Object value)70 void EmbedderDataSlot::store_tagged(JSObject object, int embedder_field_index,
71                                     Object value) {
72   int slot_offset = object.GetEmbedderFieldOffset(embedder_field_index);
73   ObjectSlot(FIELD_ADDR(object, slot_offset + kTaggedPayloadOffset))
74       .Relaxed_Store(value);
75   WRITE_BARRIER(object, slot_offset + kTaggedPayloadOffset, value);
76 #ifdef V8_COMPRESS_POINTERS
77   // See gc_safe_store() for the reasons behind two stores and why the second is
78   // only done if !V8_HEAP_SANDBOX_BOOL
79   ObjectSlot(FIELD_ADDR(object, slot_offset + kRawPayloadOffset))
80       .Relaxed_Store(Smi::zero());
81 #endif
82 }
83 
ToAlignedPointer(IsolateRoot isolate_root,void ** out_pointer)84 bool EmbedderDataSlot::ToAlignedPointer(IsolateRoot isolate_root,
85                                         void** out_pointer) const {
86   // We don't care about atomicity of access here because embedder slots
87   // are accessed this way only from the main thread via API during "mutator"
88   // phase which is propely synched with GC (concurrent marker may still look
89   // at the tagged part of the embedder slot but read-only access is ok).
90   Address raw_value;
91 #ifdef V8_HEAP_SANDBOX
92   uint32_t index = base::Memory<uint32_t>(address() + kRawPayloadOffset);
93   const Isolate* isolate = Isolate::FromRootAddress(isolate_root.address());
94   raw_value = isolate->external_pointer_table().get(index) ^
95               kEmbedderDataSlotPayloadTag;
96 #else
97   if (COMPRESS_POINTERS_BOOL) {
98     // TODO(ishell, v8:8875): When pointer compression is enabled 8-byte size
99     // fields (external pointers, doubles and BigInt data) are only kTaggedSize
100     // aligned so we have to use unaligned pointer friendly way of accessing
101     // them in order to avoid undefined behavior in C++ code.
102     raw_value = base::ReadUnalignedValue<Address>(address());
103   } else {
104     raw_value = *location();
105   }
106 #endif
107   *out_pointer = reinterpret_cast<void*>(raw_value);
108   return HAS_SMI_TAG(raw_value);
109 }
110 
ToAlignedPointerSafe(IsolateRoot isolate_root,void ** out_pointer)111 bool EmbedderDataSlot::ToAlignedPointerSafe(IsolateRoot isolate_root,
112                                             void** out_pointer) const {
113 #ifdef V8_HEAP_SANDBOX
114   uint32_t index = base::Memory<uint32_t>(address() + kRawPayloadOffset);
115   Address raw_value;
116   const Isolate* isolate = Isolate::FromRootAddress(isolate_root.address());
117   if (isolate->external_pointer_table().is_valid_index(index)) {
118     raw_value = isolate->external_pointer_table().get(index) ^
119                 kEmbedderDataSlotPayloadTag;
120     *out_pointer = reinterpret_cast<void*>(raw_value);
121     return true;
122   }
123   return false;
124 #else
125   return ToAlignedPointer(isolate_root, out_pointer);
126 #endif  // V8_HEAP_SANDBOX
127 }
128 
store_aligned_pointer(Isolate * isolate,void * ptr)129 bool EmbedderDataSlot::store_aligned_pointer(Isolate* isolate, void* ptr) {
130   Address value = reinterpret_cast<Address>(ptr);
131   if (!HAS_SMI_TAG(value)) return false;
132 #ifdef V8_HEAP_SANDBOX
133   if (V8_HEAP_SANDBOX_BOOL) {
134     AllocateExternalPointerEntry(isolate);
135     // Raw payload contains the table index. Object slots don't support loading
136     // of raw values, so we just "reinterpret cast" Object value to index.
137     Object index_as_object =
138         ObjectSlot(address() + kRawPayloadOffset).Relaxed_Load();
139     uint32_t index = static_cast<uint32_t>(index_as_object.ptr());
140     isolate->external_pointer_table().set(index,
141                                           value ^ kEmbedderDataSlotPayloadTag);
142     return true;
143   }
144 #endif
145   gc_safe_store(isolate, value);
146   return true;
147 }
148 
load_raw(Isolate * isolate,const DisallowGarbageCollection & no_gc)149 EmbedderDataSlot::RawData EmbedderDataSlot::load_raw(
150     Isolate* isolate, const DisallowGarbageCollection& no_gc) const {
151   // We don't care about atomicity of access here because embedder slots
152   // are accessed this way only by serializer from the main thread when
153   // GC is not active (concurrent marker may still look at the tagged part
154   // of the embedder slot but read-only access is ok).
155 #ifdef V8_COMPRESS_POINTERS
156   // TODO(ishell, v8:8875): When pointer compression is enabled 8-byte size
157   // fields (external pointers, doubles and BigInt data) are only kTaggedSize
158   // aligned so we have to use unaligned pointer friendly way of accessing them
159   // in order to avoid undefined behavior in C++ code.
160   return base::ReadUnalignedValue<EmbedderDataSlot::RawData>(address());
161 #else
162   return *location();
163 #endif
164 }
165 
store_raw(Isolate * isolate,EmbedderDataSlot::RawData data,const DisallowGarbageCollection & no_gc)166 void EmbedderDataSlot::store_raw(Isolate* isolate,
167                                  EmbedderDataSlot::RawData data,
168                                  const DisallowGarbageCollection& no_gc) {
169   gc_safe_store(isolate, data);
170 }
171 
gc_safe_store(Isolate * isolate,Address value)172 void EmbedderDataSlot::gc_safe_store(Isolate* isolate, Address value) {
173 #ifdef V8_COMPRESS_POINTERS
174   STATIC_ASSERT(kSmiShiftSize == 0);
175   STATIC_ASSERT(SmiValuesAre31Bits());
176   STATIC_ASSERT(kTaggedSize == kInt32Size);
177 
178   // We have to do two 32-bit stores here because
179   // 1) tagged part modifications must be atomic to be properly synchronized
180   //    with the concurrent marker.
181   // 2) atomicity of full pointer store is not guaranteed for embedder slots
182   //    since the address of the slot may not be kSystemPointerSize aligned
183   //    (only kTaggedSize alignment is guaranteed).
184   // TODO(ishell, v8:8875): revisit this once the allocation alignment
185   // inconsistency is fixed.
186   Address lo = static_cast<intptr_t>(static_cast<int32_t>(value));
187   ObjectSlot(address() + kTaggedPayloadOffset).Relaxed_Store(Smi(lo));
188   Address hi = value >> 32;
189   ObjectSlot(address() + kRawPayloadOffset).Relaxed_Store(Object(hi));
190 #else
191   ObjectSlot(address() + kTaggedPayloadOffset).Relaxed_Store(Smi(value));
192 #endif
193 }
194 
195 }  // namespace internal
196 }  // namespace v8
197 
198 #include "src/objects/object-macros-undef.h"
199 
200 #endif  // V8_OBJECTS_EMBEDDER_DATA_SLOT_INL_H_
201