| 1 | /* |
| 2 | * Copyright (C) 2005-2018 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the NU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public License |
| 15 | * along with this library; see the file COPYING.LIB. If not, write to |
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include "config.h" |
| 22 | #include "JSLock.h" |
| 23 | |
| 24 | #include "Heap.h" |
| 25 | #include "CallFrame.h" |
| 26 | #include "JSGlobalObject.h" |
| 27 | #include "JSObject.h" |
| 28 | #include "JSCInlines.h" |
| 29 | #include "MachineStackMarker.h" |
| 30 | #include "SamplingProfiler.h" |
| 31 | #include "WasmCapabilities.h" |
| 32 | #include "WasmMachineThreads.h" |
| 33 | #include <thread> |
| 34 | #include <wtf/StackPointer.h> |
| 35 | #include <wtf/Threading.h> |
| 36 | #include <wtf/threads/Signals.h> |
| 37 | |
| 38 | #if USE(WEB_THREAD) |
| 39 | #include <wtf/ios/WebCoreThread.h> |
| 40 | #endif |
| 41 | |
| 42 | namespace JSC { |
| 43 | |
| 44 | Lock GlobalJSLock::s_sharedInstanceMutex; |
| 45 | |
| 46 | GlobalJSLock::GlobalJSLock() |
| 47 | { |
| 48 | s_sharedInstanceMutex.lock(); |
| 49 | } |
| 50 | |
| 51 | GlobalJSLock::~GlobalJSLock() |
| 52 | { |
| 53 | s_sharedInstanceMutex.unlock(); |
| 54 | } |
| 55 | |
| 56 | JSLockHolder::JSLockHolder(JSGlobalObject* globalObject) |
| 57 | : JSLockHolder(globalObject->vm()) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | JSLockHolder::JSLockHolder(VM* vm) |
| 62 | : JSLockHolder(*vm) |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | JSLockHolder::JSLockHolder(VM& vm) |
| 67 | : m_vm(&vm) |
| 68 | { |
| 69 | m_vm->apiLock().lock(); |
| 70 | } |
| 71 | |
| 72 | JSLockHolder::~JSLockHolder() |
| 73 | { |
| 74 | RefPtr<JSLock> apiLock(&m_vm->apiLock()); |
| 75 | m_vm = nullptr; |
| 76 | apiLock->unlock(); |
| 77 | } |
| 78 | |
| 79 | JSLock::JSLock(VM* vm) |
| 80 | : m_lockCount(0) |
| 81 | , m_lockDropDepth(0) |
| 82 | , m_vm(vm) |
| 83 | , m_entryAtomStringTable(nullptr) |
| 84 | { |
| 85 | } |
| 86 | |
| 87 | JSLock::~JSLock() |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | void JSLock::willDestroyVM(VM* vm) |
| 92 | { |
| 93 | ASSERT_UNUSED(vm, m_vm == vm); |
| 94 | m_vm = nullptr; |
| 95 | } |
| 96 | |
| 97 | void JSLock::lock() |
| 98 | { |
| 99 | lock(1); |
| 100 | } |
| 101 | |
| 102 | void JSLock::lock(intptr_t lockCount) |
| 103 | { |
| 104 | ASSERT(lockCount > 0); |
| 105 | #if USE(WEB_THREAD) |
| 106 | if (m_isWebThreadAware) { |
| 107 | ASSERT(WebCoreWebThreadIsEnabled && WebCoreWebThreadIsEnabled()); |
| 108 | WebCoreWebThreadLock(); |
| 109 | } |
| 110 | #endif |
| 111 | |
| 112 | bool success = m_lock.tryLock(); |
| 113 | if (UNLIKELY(!success)) { |
| 114 | if (currentThreadIsHoldingLock()) { |
| 115 | m_lockCount += lockCount; |
| 116 | return; |
| 117 | } |
| 118 | m_lock.lock(); |
| 119 | } |
| 120 | |
| 121 | m_ownerThread = &Thread::current(); |
| 122 | WTF::storeStoreFence(); |
| 123 | m_hasOwnerThread = true; |
| 124 | ASSERT(!m_lockCount); |
| 125 | m_lockCount = lockCount; |
| 126 | |
| 127 | didAcquireLock(); |
| 128 | } |
| 129 | |
| 130 | void JSLock::didAcquireLock() |
| 131 | { |
| 132 | // FIXME: What should happen to the per-thread identifier table if we don't have a VM? |
| 133 | if (!m_vm) |
| 134 | return; |
| 135 | |
| 136 | Thread& thread = Thread::current(); |
| 137 | ASSERT(!m_entryAtomStringTable); |
| 138 | m_entryAtomStringTable = thread.setCurrentAtomStringTable(m_vm->atomStringTable()); |
| 139 | ASSERT(m_entryAtomStringTable); |
| 140 | |
| 141 | m_vm->setLastStackTop(thread.savedLastStackTop()); |
| 142 | ASSERT(thread.stack().contains(m_vm->lastStackTop())); |
| 143 | |
| 144 | if (m_vm->heap.hasAccess()) |
| 145 | m_shouldReleaseHeapAccess = false; |
| 146 | else { |
| 147 | m_vm->heap.acquireAccess(); |
| 148 | m_shouldReleaseHeapAccess = true; |
| 149 | } |
| 150 | |
| 151 | RELEASE_ASSERT(!m_vm->stackPointerAtVMEntry()); |
| 152 | void* p = currentStackPointer(); |
| 153 | m_vm->setStackPointerAtVMEntry(p); |
| 154 | |
| 155 | if (m_vm->heap.machineThreads().addCurrentThread()) { |
| 156 | if (isKernTCSMAvailable()) |
| 157 | enableKernTCSM(); |
| 158 | } |
| 159 | |
| 160 | #if ENABLE(WEBASSEMBLY) |
| 161 | if (Wasm::isSupported()) |
| 162 | Wasm::startTrackingCurrentThread(); |
| 163 | #endif |
| 164 | |
| 165 | #if HAVE(MACH_EXCEPTIONS) |
| 166 | registerThreadForMachExceptionHandling(Thread::current()); |
| 167 | #endif |
| 168 | |
| 169 | // Note: everything below must come after addCurrentThread(). |
| 170 | m_vm->traps().notifyGrabAllLocks(); |
| 171 | |
| 172 | m_vm->firePrimitiveGigacageEnabledIfNecessary(); |
| 173 | |
| 174 | #if ENABLE(SAMPLING_PROFILER) |
| 175 | if (SamplingProfiler* samplingProfiler = m_vm->samplingProfiler()) |
| 176 | samplingProfiler->noticeJSLockAcquisition(); |
| 177 | #endif |
| 178 | } |
| 179 | |
| 180 | void JSLock::unlock() |
| 181 | { |
| 182 | unlock(1); |
| 183 | } |
| 184 | |
| 185 | void JSLock::unlock(intptr_t unlockCount) |
| 186 | { |
| 187 | RELEASE_ASSERT(currentThreadIsHoldingLock()); |
| 188 | ASSERT(m_lockCount >= unlockCount); |
| 189 | |
| 190 | // Maintain m_lockCount while calling willReleaseLock() so that its callees know that |
| 191 | // they still have the lock. |
| 192 | if (unlockCount == m_lockCount) |
| 193 | willReleaseLock(); |
| 194 | |
| 195 | m_lockCount -= unlockCount; |
| 196 | |
| 197 | if (!m_lockCount) { |
| 198 | m_hasOwnerThread = false; |
| 199 | m_lock.unlock(); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void JSLock::willReleaseLock() |
| 204 | { |
| 205 | RefPtr<VM> vm = m_vm; |
| 206 | if (vm) { |
| 207 | vm->drainMicrotasks(); |
| 208 | |
| 209 | if (!vm->topCallFrame) |
| 210 | vm->clearLastException(); |
| 211 | |
| 212 | vm->heap.releaseDelayedReleasedObjects(); |
| 213 | vm->setStackPointerAtVMEntry(nullptr); |
| 214 | |
| 215 | if (m_shouldReleaseHeapAccess) |
| 216 | vm->heap.releaseAccess(); |
| 217 | } |
| 218 | |
| 219 | if (m_entryAtomStringTable) { |
| 220 | Thread::current().setCurrentAtomStringTable(m_entryAtomStringTable); |
| 221 | m_entryAtomStringTable = nullptr; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void JSLock::lock(JSGlobalObject* globalObject) |
| 226 | { |
| 227 | globalObject->vm().apiLock().lock(); |
| 228 | } |
| 229 | |
| 230 | void JSLock::unlock(JSGlobalObject* globalObject) |
| 231 | { |
| 232 | globalObject->vm().apiLock().unlock(); |
| 233 | } |
| 234 | |
| 235 | // This function returns the number of locks that were dropped. |
| 236 | unsigned JSLock::dropAllLocks(DropAllLocks* dropper) |
| 237 | { |
| 238 | if (!currentThreadIsHoldingLock()) |
| 239 | return 0; |
| 240 | |
| 241 | ++m_lockDropDepth; |
| 242 | |
| 243 | dropper->setDropDepth(m_lockDropDepth); |
| 244 | |
| 245 | Thread& thread = Thread::current(); |
| 246 | thread.setSavedStackPointerAtVMEntry(m_vm->stackPointerAtVMEntry()); |
| 247 | thread.setSavedLastStackTop(m_vm->lastStackTop()); |
| 248 | |
| 249 | unsigned droppedLockCount = m_lockCount; |
| 250 | unlock(droppedLockCount); |
| 251 | |
| 252 | return droppedLockCount; |
| 253 | } |
| 254 | |
| 255 | void JSLock::grabAllLocks(DropAllLocks* dropper, unsigned droppedLockCount) |
| 256 | { |
| 257 | // If no locks were dropped, nothing to do! |
| 258 | if (!droppedLockCount) |
| 259 | return; |
| 260 | |
| 261 | ASSERT(!currentThreadIsHoldingLock()); |
| 262 | lock(droppedLockCount); |
| 263 | |
| 264 | while (dropper->dropDepth() != m_lockDropDepth) { |
| 265 | unlock(droppedLockCount); |
| 266 | Thread::yield(); |
| 267 | lock(droppedLockCount); |
| 268 | } |
| 269 | |
| 270 | --m_lockDropDepth; |
| 271 | |
| 272 | Thread& thread = Thread::current(); |
| 273 | m_vm->setStackPointerAtVMEntry(thread.savedStackPointerAtVMEntry()); |
| 274 | m_vm->setLastStackTop(thread.savedLastStackTop()); |
| 275 | } |
| 276 | |
| 277 | JSLock::DropAllLocks::DropAllLocks(VM* vm) |
| 278 | : m_droppedLockCount(0) |
| 279 | // If the VM is in the middle of being destroyed then we don't want to resurrect it |
| 280 | // by allowing DropAllLocks to ref it. By this point the JSLock has already been |
| 281 | // released anyways, so it doesn't matter that DropAllLocks is a no-op. |
| 282 | , m_vm(vm->heap.isShuttingDown() ? nullptr : vm) |
| 283 | { |
| 284 | if (!m_vm) |
| 285 | return; |
| 286 | RELEASE_ASSERT(!m_vm->apiLock().currentThreadIsHoldingLock() || !m_vm->isCollectorBusyOnCurrentThread()); |
| 287 | m_droppedLockCount = m_vm->apiLock().dropAllLocks(this); |
| 288 | } |
| 289 | |
| 290 | JSLock::DropAllLocks::DropAllLocks(JSGlobalObject* globalObject) |
| 291 | : DropAllLocks(globalObject ? &globalObject->vm() : nullptr) |
| 292 | { |
| 293 | } |
| 294 | |
| 295 | JSLock::DropAllLocks::DropAllLocks(VM& vm) |
| 296 | : DropAllLocks(&vm) |
| 297 | { |
| 298 | } |
| 299 | |
| 300 | JSLock::DropAllLocks::~DropAllLocks() |
| 301 | { |
| 302 | if (!m_vm) |
| 303 | return; |
| 304 | m_vm->apiLock().grabAllLocks(this, m_droppedLockCount); |
| 305 | } |
| 306 | |
| 307 | } // namespace JSC |
| 308 | |