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(Isolate * isolate,void ** out_pointer)84 bool EmbedderDataSlot::ToAlignedPointer(Isolate* isolate,
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   raw_value = isolate->external_pointer_table().get(index) &
94               ~kEmbedderDataSlotPayloadTag;
95 #else
96   if (COMPRESS_POINTERS_BOOL) {
97     // TODO(ishell, v8:8875): When pointer compression is enabled 8-byte size
98     // fields (external pointers, doubles and BigInt data) are only kTaggedSize
99     // aligned so we have to use unaligned pointer friendly way of accessing
100     // them in order to avoid undefined behavior in C++ code.
101     raw_value = base::ReadUnalignedValue<Address>(address());
102   } else {
103     raw_value = *location();
104   }
105 #endif
106   *out_pointer = reinterpret_cast<void*>(raw_value);
107   return HAS_SMI_TAG(raw_value);
108 }
109 
ToAlignedPointerSafe(Isolate * isolate,void ** out_pointer)110 bool EmbedderDataSlot::ToAlignedPointerSafe(Isolate* isolate,
111                                             void** out_pointer) const {
112 #ifdef V8_HEAP_SANDBOX
113   uint32_t index = base::Memory<uint32_t>(address() + kRawPayloadOffset);
114   Address raw_value;
115   if (isolate->external_pointer_table().is_valid_index(index)) {
116     raw_value = isolate->external_pointer_table().get(index) &
117                 ~kEmbedderDataSlotPayloadTag;
118     *out_pointer = reinterpret_cast<void*>(raw_value);
119     return true;
120   }
121   return false;
122 #else
123   return ToAlignedPointer(isolate, out_pointer);
124 #endif  // V8_HEAP_SANDBOX
125 }
126 
store_aligned_pointer(Isolate * isolate,void * ptr)127 bool EmbedderDataSlot::store_aligned_pointer(Isolate* isolate, void* ptr) {
128   Address value = reinterpret_cast<Address>(ptr);
129   if (!HAS_SMI_TAG(value)) return false;
130 #ifdef V8_HEAP_SANDBOX
131   if (V8_HEAP_SANDBOX_BOOL) {
132     AllocateExternalPointerEntry(isolate);
133     // Raw payload contains the table index. Object slots don't support loading
134     // of raw values, so we just "reinterpret cast" Object value to index.
135     Object index_as_object =
136         ObjectSlot(address() + kRawPayloadOffset).Relaxed_Load();
137     uint32_t index = static_cast<uint32_t>(index_as_object.ptr());
138     isolate->external_pointer_table().set(index,
139                                           value | kEmbedderDataSlotPayloadTag);
140     return true;
141   }
142 #endif
143   gc_safe_store(isolate, value);
144   return true;
145 }
146 
load_raw(Isolate * isolate,const DisallowGarbageCollection & no_gc)147 EmbedderDataSlot::RawData EmbedderDataSlot::load_raw(
148     Isolate* isolate, const DisallowGarbageCollection& no_gc) const {
149   // We don't care about atomicity of access here because embedder slots
150   // are accessed this way only by serializer from the main thread when
151   // GC is not active (concurrent marker may still look at the tagged part
152   // of the embedder slot but read-only access is ok).
153 #ifdef V8_COMPRESS_POINTERS
154   // TODO(ishell, v8:8875): When pointer compression is enabled 8-byte size
155   // fields (external pointers, doubles and BigInt data) are only kTaggedSize
156   // aligned so we have to use unaligned pointer friendly way of accessing them
157   // in order to avoid undefined behavior in C++ code.
158   return base::ReadUnalignedValue<EmbedderDataSlot::RawData>(address());
159 #else
160   return *location();
161 #endif
162 }
163 
store_raw(Isolate * isolate,EmbedderDataSlot::RawData data,const DisallowGarbageCollection & no_gc)164 void EmbedderDataSlot::store_raw(Isolate* isolate,
165                                  EmbedderDataSlot::RawData data,
166                                  const DisallowGarbageCollection& no_gc) {
167   gc_safe_store(isolate, data);
168 }
169 
gc_safe_store(Isolate * isolate,Address value)170 void EmbedderDataSlot::gc_safe_store(Isolate* isolate, Address value) {
171 #ifdef V8_COMPRESS_POINTERS
172   STATIC_ASSERT(kSmiShiftSize == 0);
173   STATIC_ASSERT(SmiValuesAre31Bits());
174   STATIC_ASSERT(kTaggedSize == kInt32Size);
175 
176   // We have to do two 32-bit stores here because
177   // 1) tagged part modifications must be atomic to be properly synchronized
178   //    with the concurrent marker.
179   // 2) atomicity of full pointer store is not guaranteed for embedder slots
180   //    since the address of the slot may not be kSystemPointerSize aligned
181   //    (only kTaggedSize alignment is guaranteed).
182   // TODO(ishell, v8:8875): revisit this once the allocation alignment
183   // inconsistency is fixed.
184   Address lo = static_cast<intptr_t>(static_cast<int32_t>(value));
185   ObjectSlot(address() + kTaggedPayloadOffset).Relaxed_Store(Smi(lo));
186   Address hi = value >> 32;
187   ObjectSlot(address() + kRawPayloadOffset).Relaxed_Store(Object(hi));
188 #else
189   ObjectSlot(address() + kTaggedPayloadOffset).Relaxed_Store(Smi(value));
190 #endif
191 }
192 
193 }  // namespace internal
194 }  // namespace v8
195 
196 #include "src/objects/object-macros-undef.h"
197 
198 #endif  // V8_OBJECTS_EMBEDDER_DATA_SLOT_INL_H_
199