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_H_ 6 #define V8_OBJECTS_EMBEDDER_DATA_SLOT_H_ 7 8 #include <utility> 9 10 #include "src/common/assert-scope.h" 11 #include "src/common/globals.h" 12 #include "src/objects/slots.h" 13 14 // Has to be the last include (doesn't have include guards): 15 #include "src/objects/object-macros.h" 16 17 namespace v8 { 18 namespace internal { 19 20 class EmbedderDataArray; 21 class JSObject; 22 class Object; 23 24 // An EmbedderDataSlot instance describes a kEmbedderDataSlotSize field ("slot") 25 // holding an embedder data which may contain raw aligned pointer or a tagged 26 // pointer (smi or heap object). 27 // Its address() is the address of the slot. 28 // The slot's contents can be read and written using respective load_XX() and 29 // store_XX() methods. 30 // Storing heap object through this slot may require triggering write barriers 31 // so this operation must be done via static store_tagged() methods. 32 class EmbedderDataSlot 33 : public SlotBase<EmbedderDataSlot, Address, kTaggedSize> { 34 public: EmbedderDataSlot()35 EmbedderDataSlot() : SlotBase(kNullAddress) {} 36 V8_INLINE EmbedderDataSlot(EmbedderDataArray array, int entry_index); 37 V8_INLINE EmbedderDataSlot(JSObject object, int embedder_field_index); 38 39 #if defined(V8_TARGET_BIG_ENDIAN) && defined(V8_COMPRESS_POINTERS) 40 static constexpr int kTaggedPayloadOffset = kTaggedSize; 41 #else 42 static constexpr int kTaggedPayloadOffset = 0; 43 #endif 44 45 #ifdef V8_COMPRESS_POINTERS 46 // The raw payload is located in the other "tagged" part of the full pointer 47 // and cotains the upper part of aligned address. The raw part is not expected 48 // to look like a tagged value. 49 // When V8_HEAP_SANDBOX is defined the raw payload contains an index into the 50 // external pointer table. 51 static constexpr int kRawPayloadOffset = kTaggedSize - kTaggedPayloadOffset; 52 #endif 53 static constexpr int kRequiredPtrAlignment = kSmiTagSize; 54 55 // Opaque type used for storing raw embedder data. 56 using RawData = Address; 57 58 V8_INLINE void AllocateExternalPointerEntry(Isolate* isolate); 59 60 V8_INLINE Object load_tagged() const; 61 V8_INLINE void store_smi(Smi value); 62 63 // Setting an arbitrary tagged value requires triggering a write barrier 64 // which requires separate object and offset values, therefore these static 65 // functions also has the target object parameter. 66 static V8_INLINE void store_tagged(EmbedderDataArray array, int entry_index, 67 Object value); 68 static V8_INLINE void store_tagged(JSObject object, int embedder_field_index, 69 Object value); 70 71 // Tries reinterpret the value as an aligned pointer and sets *out_result to 72 // the pointer-like value. Note, that some Smis could still look like an 73 // aligned pointers. 74 // Returns true on success. 75 // When V8 heap sandbox is enabled, calling this method when the raw part of 76 // the slot does not contain valid external pointer table index is undefined 77 // behaviour and most likely result in crashes. 78 V8_INLINE bool ToAlignedPointer(Isolate* isolate, void** out_result) const; 79 80 // Same as ToAlignedPointer() but with a workaround for V8 heap sandbox. 81 // When V8 heap sandbox is enabled, this method doesn't crash when the raw 82 // part of the slot contains "undefined" instead of a correct external table 83 // entry index (see Factory::InitializeJSObjectBody() for details). 84 // Returns true when the external pointer table index was pointing to a valid 85 // entry, otherwise false. 86 // 87 // Call this function if you are not sure whether the slot contains valid 88 // external pointer or not. 89 V8_INLINE bool ToAlignedPointerSafe(Isolate* isolate, 90 void** out_result) const; 91 92 // Returns true if the pointer was successfully stored or false it the pointer 93 // was improperly aligned. 94 V8_INLINE V8_WARN_UNUSED_RESULT bool store_aligned_pointer(Isolate* isolate, 95 void* ptr); 96 97 V8_INLINE RawData load_raw(Isolate* isolate, 98 const DisallowGarbageCollection& no_gc) const; 99 V8_INLINE void store_raw(Isolate* isolate, RawData data, 100 const DisallowGarbageCollection& no_gc); 101 102 private: 103 // Stores given value to the embedder data slot in a concurrent-marker 104 // friendly manner (tagged part of the slot is written atomically). 105 V8_INLINE void gc_safe_store(Isolate* isolate, Address value); 106 }; 107 108 } // namespace internal 109 } // namespace v8 110 111 #include "src/objects/object-macros-undef.h" 112 113 #endif // V8_OBJECTS_EMBEDDER_DATA_SLOT_H_ 114