1 | /* |
2 | * Copyright (C) 2011-2018 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include "GCAssertions.h" |
29 | #include "HandleTypes.h" |
30 | #include <type_traits> |
31 | #include <wtf/DumbPtrTraits.h> |
32 | #include <wtf/DumbValueTraits.h> |
33 | |
34 | namespace JSC { |
35 | |
36 | namespace DFG { |
37 | class DesiredWriteBarrier; |
38 | } |
39 | |
40 | class JSCell; |
41 | class VM; |
42 | class JSGlobalObject; |
43 | |
44 | template<class T> |
45 | using WriteBarrierTraitsSelect = typename std::conditional<std::is_same<T, Unknown>::value, |
46 | DumbValueTraits<T>, DumbPtrTraits<T> |
47 | >::type; |
48 | |
49 | template<class T, typename Traits = WriteBarrierTraitsSelect<T>> class WriteBarrierBase; |
50 | template<> class WriteBarrierBase<JSValue>; |
51 | |
52 | JS_EXPORT_PRIVATE void slowValidateCell(JSCell*); |
53 | JS_EXPORT_PRIVATE void slowValidateCell(JSGlobalObject*); |
54 | |
55 | #if ENABLE(GC_VALIDATION) |
56 | template<class T> inline void validateCell(T cell) |
57 | { |
58 | ASSERT_GC_OBJECT_INHERITS(cell, std::remove_pointer<T>::type::info()); |
59 | } |
60 | |
61 | template<> inline void validateCell<JSCell*>(JSCell* cell) |
62 | { |
63 | slowValidateCell(cell); |
64 | } |
65 | |
66 | template<> inline void validateCell<JSGlobalObject*>(JSGlobalObject* globalObject) |
67 | { |
68 | slowValidateCell(globalObject); |
69 | } |
70 | #else |
71 | template<class T> inline void validateCell(T) |
72 | { |
73 | } |
74 | #endif |
75 | |
76 | // We have a separate base class with no constructors for use in Unions. |
77 | template <typename T, typename Traits> class WriteBarrierBase { |
78 | using StorageType = typename Traits::StorageType; |
79 | |
80 | public: |
81 | void set(VM&, const JSCell* owner, T* value); |
82 | |
83 | // This is meant to be used like operator=, but is called copyFrom instead, in |
84 | // order to kindly inform the C++ compiler that its advice is not appreciated. |
85 | void copyFrom(const WriteBarrierBase& other) |
86 | { |
87 | // FIXME add version with different Traits once needed. |
88 | Traits::exchange(m_cell, other.m_cell); |
89 | } |
90 | |
91 | void setMayBeNull(VM&, const JSCell* owner, T* value); |
92 | |
93 | // Should only be used by JSCell during early initialisation |
94 | // when some basic types aren't yet completely instantiated |
95 | void setEarlyValue(VM&, const JSCell* owner, T* value); |
96 | |
97 | T* get() const |
98 | { |
99 | // Copy m_cell to a local to avoid multiple-read issues. (See <http://webkit.org/b/110854>) |
100 | StorageType cell = m_cell; |
101 | if (cell) |
102 | validateCell(reinterpret_cast<JSCell*>(static_cast<void*>(Traits::unwrap(cell)))); |
103 | return Traits::unwrap(cell); |
104 | } |
105 | |
106 | T* operator*() const |
107 | { |
108 | StorageType cell = m_cell; |
109 | ASSERT(cell); |
110 | auto unwrapped = Traits::unwrap(cell); |
111 | validateCell<T>(unwrapped); |
112 | return Traits::unwrap(unwrapped); |
113 | } |
114 | |
115 | T* operator->() const |
116 | { |
117 | StorageType cell = m_cell; |
118 | ASSERT(cell); |
119 | auto unwrapped = Traits::unwrap(cell); |
120 | validateCell(unwrapped); |
121 | return unwrapped; |
122 | } |
123 | |
124 | void clear() { Traits::exchange(m_cell, nullptr); } |
125 | |
126 | // Slot cannot be used when pointers aren't stored as-is. |
127 | template<typename BarrierT, typename BarrierTraits, std::enable_if_t<std::is_same<BarrierTraits, DumbPtrTraits<BarrierT>>::value, void*> = nullptr> |
128 | struct SlotHelper { |
129 | static BarrierT** reinterpret(typename BarrierTraits::StorageType* cell) { return reinterpret_cast<T**>(cell); } |
130 | }; |
131 | |
132 | T** slot() |
133 | { |
134 | return SlotHelper<T, Traits>::reinterpret(&m_cell); |
135 | } |
136 | |
137 | explicit operator bool() const { return !!m_cell; } |
138 | |
139 | bool operator!() const { return !m_cell; } |
140 | |
141 | void setWithoutWriteBarrier(T* value) |
142 | { |
143 | #if ENABLE(WRITE_BARRIER_PROFILING) |
144 | WriteBarrierCounters::usesWithoutBarrierFromCpp.count(); |
145 | #endif |
146 | Traits::exchange(this->m_cell, value); |
147 | } |
148 | |
149 | T* unvalidatedGet() const { return Traits::unwrap(m_cell); } |
150 | |
151 | private: |
152 | StorageType m_cell; |
153 | }; |
154 | |
155 | template <> class WriteBarrierBase<Unknown, DumbValueTraits<Unknown>> { |
156 | public: |
157 | void set(VM&, const JSCell* owner, JSValue); |
158 | void setWithoutWriteBarrier(JSValue value) |
159 | { |
160 | m_value = JSValue::encode(value); |
161 | } |
162 | |
163 | JSValue get() const |
164 | { |
165 | return JSValue::decode(m_value); |
166 | } |
167 | void clear() { m_value = JSValue::encode(JSValue()); } |
168 | void setUndefined() { m_value = JSValue::encode(jsUndefined()); } |
169 | void setStartingValue(JSValue value) { m_value = JSValue::encode(value); } |
170 | bool isNumber() const { return get().isNumber(); } |
171 | bool isInt32() const { return get().isInt32(); } |
172 | bool isObject() const { return get().isObject(); } |
173 | bool isNull() const { return get().isNull(); } |
174 | bool isGetterSetter() const { return get().isGetterSetter(); } |
175 | bool isCustomGetterSetter() const { return get().isCustomGetterSetter(); } |
176 | |
177 | JSValue* slot() const |
178 | { |
179 | return bitwise_cast<JSValue*>(&m_value); |
180 | } |
181 | |
182 | int32_t* tagPointer() { return &bitwise_cast<EncodedValueDescriptor*>(&m_value)->asBits.tag; } |
183 | int32_t* payloadPointer() { return &bitwise_cast<EncodedValueDescriptor*>(&m_value)->asBits.payload; } |
184 | |
185 | explicit operator bool() const { return !!get(); } |
186 | bool operator!() const { return !get(); } |
187 | |
188 | private: |
189 | EncodedJSValue m_value; |
190 | }; |
191 | |
192 | template <typename T, typename Traits = WriteBarrierTraitsSelect<T>> |
193 | class WriteBarrier : public WriteBarrierBase<T, Traits> { |
194 | WTF_MAKE_FAST_ALLOCATED; |
195 | public: |
196 | WriteBarrier() |
197 | { |
198 | this->setWithoutWriteBarrier(0); |
199 | } |
200 | |
201 | WriteBarrier(VM& vm, const JSCell* owner, T* value) |
202 | { |
203 | this->set(vm, owner, value); |
204 | } |
205 | |
206 | WriteBarrier(DFG::DesiredWriteBarrier&, T* value) |
207 | { |
208 | ASSERT(isCompilationThread()); |
209 | this->setWithoutWriteBarrier(value); |
210 | } |
211 | |
212 | enum MayBeNullTag { MayBeNull }; |
213 | WriteBarrier(VM& vm, const JSCell* owner, T* value, MayBeNullTag) |
214 | { |
215 | this->setMayBeNull(vm, owner, value); |
216 | } |
217 | }; |
218 | |
219 | enum UndefinedWriteBarrierTagType { UndefinedWriteBarrierTag }; |
220 | template <> |
221 | class WriteBarrier<Unknown, DumbValueTraits<Unknown>> : public WriteBarrierBase<Unknown, DumbValueTraits<Unknown>> { |
222 | WTF_MAKE_FAST_ALLOCATED; |
223 | public: |
224 | WriteBarrier() |
225 | { |
226 | this->setWithoutWriteBarrier(JSValue()); |
227 | } |
228 | WriteBarrier(UndefinedWriteBarrierTagType) |
229 | { |
230 | this->setWithoutWriteBarrier(jsUndefined()); |
231 | } |
232 | |
233 | WriteBarrier(VM& vm, const JSCell* owner, JSValue value) |
234 | { |
235 | this->set(vm, owner, value); |
236 | } |
237 | |
238 | WriteBarrier(DFG::DesiredWriteBarrier&, JSValue value) |
239 | { |
240 | ASSERT(isCompilationThread()); |
241 | this->setWithoutWriteBarrier(value); |
242 | } |
243 | }; |
244 | |
245 | template <typename U, typename V, typename TraitsU, typename TraitsV> |
246 | inline bool operator==(const WriteBarrierBase<U, TraitsU>& lhs, const WriteBarrierBase<V, TraitsV>& rhs) |
247 | { |
248 | return lhs.get() == rhs.get(); |
249 | } |
250 | |
251 | } // namespace JSC |
252 | |