1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
/*
* Copyright (C) 2020-2021 Apple, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "JSFinalizationRegistry.h"
#include "AbstractSlotVisitor.h"
#include "DeferredWorkTimer.h"
#include "GlobalObjectMethodTable.h"
#include "JSCInlines.h"
#include "JSInternalFieldObjectImplInlines.h"
namespace JSC {
const ClassInfo JSFinalizationRegistry::s_info = { "FinalizationRegistry"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSFinalizationRegistry) };
Structure* JSFinalizationRegistry::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
{
return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
}
JSFinalizationRegistry* JSFinalizationRegistry::create(VM& vm, Structure* structure, JSObject* callback)
{
JSFinalizationRegistry* instance = new (NotNull, allocateCell<JSFinalizationRegistry>(vm)) JSFinalizationRegistry(vm, structure);
instance->finishCreation(vm, structure->globalObject(), callback);
return instance;
}
void JSFinalizationRegistry::finishCreation(VM& vm, JSGlobalObject* globalObject, JSObject* callback)
{
Base::finishCreation(vm);
ASSERT(callback->isCallable());
auto values = initialValues();
for (unsigned index = 0; index < values.size(); ++index)
Base::internalField(index).setWithoutWriteBarrier(values[index]);
internalField(Field::Callback).setWithoutWriteBarrier(callback);
// Make sure we init the DOM wrapper for our document since it must be allocated before finalizeUnconditionally is called. finalizeUnconditionally,
// is called during the GC flip so no JS objects can be allocated there. This only works because we no longer weakly hold on to DOM wrappers.
globalObject->globalObjectMethodTable()->currentScriptExecutionOwner(globalObject);
}
template<typename Visitor>
void JSFinalizationRegistry::visitChildrenImpl(JSCell* cell, Visitor& visitor)
{
Base::visitChildren(cell, visitor);
auto* thisObject = jsCast<JSFinalizationRegistry*>(cell);
Locker locker { thisObject->cellLock() };
for (const auto& iter : thisObject->m_liveRegistrations) {
for (auto& registration : iter.value)
visitor.append(registration.holdings);
}
for (auto& registration : thisObject->m_noUnregistrationLive)
visitor.append(registration.holdings);
for (const auto& iter : thisObject->m_deadRegistrations) {
for (auto& holdings : iter.value)
visitor.append(holdings);
}
for (auto& holdings : thisObject->m_noUnregistrationDead)
visitor.append(holdings);
size_t totalBufferSizesInBytes = thisObject->m_deadRegistrations.capacity() * sizeof(typename decltype(thisObject->m_deadRegistrations)::KeyValuePairType);
totalBufferSizesInBytes += thisObject->m_liveRegistrations.capacity() * sizeof(typename decltype(thisObject->m_deadRegistrations)::KeyValuePairType);
totalBufferSizesInBytes += thisObject->m_noUnregistrationLive.capacity() * sizeof(decltype(thisObject->m_noUnregistrationLive.takeLast()));
totalBufferSizesInBytes += thisObject->m_noUnregistrationDead.capacity() * sizeof(decltype(thisObject->m_noUnregistrationLive.takeLast()));
visitor.vm().heap.reportExtraMemoryVisited(totalBufferSizesInBytes);
}
DEFINE_VISIT_CHILDREN(JSFinalizationRegistry);
void JSFinalizationRegistry::destroy(JSCell* table)
{
static_cast<JSFinalizationRegistry*>(table)->~JSFinalizationRegistry();
}
void JSFinalizationRegistry::finalizeUnconditionally(VM& vm, CollectionScope)
{
Locker locker { cellLock() };
#if ASSERT_ENABLED
for (const auto& iter : m_deadRegistrations)
RELEASE_ASSERT(iter.value.size());
#endif
bool readiedCell = false;
m_noUnregistrationLive.removeAllMatching([&] (const Registration& reg) {
ASSERT(!reg.holdings.get().isCell() || vm.heap.isMarked(reg.holdings.get().asCell()));
if (!vm.heap.isMarked(reg.target)) {
m_noUnregistrationDead.append(reg.holdings);
readiedCell = true;
return true;
}
return false;
});
m_liveRegistrations.removeIf([&] (auto& bucket) -> bool {
ASSERT(bucket.value.size());
bool keyIsDead = !vm.heap.isMarked(bucket.key);
DeadRegistrations* deadList = nullptr;
auto getDeadList = [&] () -> DeadRegistrations& {
if (UNLIKELY(!deadList))
deadList = &m_deadRegistrations.add(bucket.key, DeadRegistrations()).iterator->value;
return *deadList;
};
bucket.value.removeAllMatching([&] (const Registration& reg) {
ASSERT(!reg.holdings.get().isCell() || vm.heap.isMarked(reg.holdings.get().asCell()));
if (!vm.heap.isMarked(reg.target)) {
if (keyIsDead)
m_noUnregistrationDead.append(reg.holdings);
else
getDeadList().append(reg.holdings);
readiedCell = true;
return true;
}
if (keyIsDead) {
m_noUnregistrationLive.append(reg);
return true;
}
return false;
});
return !bucket.value.size();
});
if (!m_hasAlreadyScheduledWork && (readiedCell || deadCount(locker))) {
auto ticket = vm.deferredWorkTimer->addPendingWork(DeferredWorkTimer::WorkType::ImminentlyScheduled, vm, this, { });
ASSERT(vm.deferredWorkTimer->hasPendingWork(ticket));
vm.deferredWorkTimer->scheduleWorkSoon(ticket, [this](DeferredWorkTimer::Ticket) {
JSGlobalObject* globalObject = this->globalObject();
this->m_hasAlreadyScheduledWork = false;
this->runFinalizationCleanup(globalObject);
});
m_hasAlreadyScheduledWork = true;
}
}
void JSFinalizationRegistry::runFinalizationCleanup(JSGlobalObject* globalObject)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
while (JSValue value = takeDeadHoldingsValue()) {
MarkedArgumentBuffer args;
args.append(value);
call(globalObject, callback(), args, "This should not be visible: please report a bug to bugs.webkit.org"_s);
RETURN_IF_EXCEPTION(scope, void());
}
}
JSValue JSFinalizationRegistry::takeDeadHoldingsValue()
{
Locker locker { cellLock() };
JSValue result;
if (m_noUnregistrationDead.size())
result = m_noUnregistrationDead.takeLast().get();
else {
auto iter = m_deadRegistrations.begin();
if (iter == m_deadRegistrations.end())
return JSValue();
ASSERT(iter->value.size());
result = iter->value.takeLast().get();
if (!iter->value.size())
m_deadRegistrations.remove(iter);
}
#if ASSERT_ENABLED
for (const auto& iter : m_deadRegistrations)
RELEASE_ASSERT(iter.value.size());
#endif
return result;
}
void JSFinalizationRegistry::registerTarget(VM& vm, JSCell* target, JSValue holdings, JSValue token)
{
Locker locker { cellLock() };
Registration registration;
registration.target = target;
registration.holdings.setWithoutWriteBarrier(holdings);
if (token.isUndefined())
m_noUnregistrationLive.append(WTFMove(registration));
else {
RELEASE_ASSERT(token.isCell());
auto result = m_liveRegistrations.add(token.asCell(), LiveRegistrations());
result.iterator->value.append(WTFMove(registration));
}
vm.writeBarrier(this);
}
bool JSFinalizationRegistry::unregister(VM&, JSCell* token)
{
// We don't need to write barrier ourselves here because we will only point to less things after this finishes.
Locker locker { cellLock() };
bool result = m_liveRegistrations.remove(token);
result |= m_deadRegistrations.remove(token);
return result;
}
size_t JSFinalizationRegistry::liveCount(const Locker<JSCellLock>&)
{
size_t count = m_noUnregistrationLive.size();
for (const auto& iter : m_liveRegistrations)
count += iter.value.size();
return count;
}
Vector<JSFinalizationRegistry::LiveRegistration> JSFinalizationRegistry::liveRegistrations(const Locker<JSCellLock>&) const
{
Vector<LiveRegistration> liveRegistrations;
for (const auto& registration : m_noUnregistrationLive)
liveRegistrations.append({ registration.target, registration.holdings.get() });
for (const auto& [unregisterToken, registrations] : m_liveRegistrations) {
for (const auto& registration : registrations)
liveRegistrations.append({ registration.target, registration.holdings.get(), unregisterToken });
}
return liveRegistrations;
}
size_t JSFinalizationRegistry::deadCount(const Locker<JSCellLock>&)
{
size_t count = m_noUnregistrationDead.size();
for (const auto& iter : m_deadRegistrations)
count += iter.value.size();
return count;
}
Vector<JSFinalizationRegistry::DeadRegistration> JSFinalizationRegistry::deadRegistrations(const Locker<JSCellLock>&) const
{
Vector<DeadRegistration> deadRegistrations;
for (const auto& heldValue : m_noUnregistrationDead)
deadRegistrations.append({ heldValue.get() });
for (const auto& [unregisterToken, heldValues] : m_deadRegistrations) {
for (const auto& heldValue : heldValues)
deadRegistrations.append({ heldValue.get(), unregisterToken });
}
return deadRegistrations;
}
}
|