| 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 | namespace JSC { |
| 39 | |
| 40 | Lock GlobalJSLock::s_sharedInstanceMutex; |
| 41 | |
| 42 | GlobalJSLock::GlobalJSLock() |
| 43 | { |
| 44 | s_sharedInstanceMutex.lock(); |
| 45 | } |
| 46 | |
| 47 | GlobalJSLock::~GlobalJSLock() |
| 48 | { |
| 49 | s_sharedInstanceMutex.unlock(); |
| 50 | } |
| 51 | |
| 52 | JSLockHolder::JSLockHolder(ExecState* exec) |
| 53 | : m_vm(&exec->vm()) |
| 54 | { |
| 55 | init(); |
| 56 | } |
| 57 | |
| 58 | JSLockHolder::JSLockHolder(VM* vm) |
| 59 | : m_vm(vm) |
| 60 | { |
| 61 | init(); |
| 62 | } |
| 63 | |
| 64 | JSLockHolder::JSLockHolder(VM& vm) |
| 65 | : m_vm(&vm) |
| 66 | { |
| 67 | init(); |
| 68 | } |
| 69 | |
| 70 | void JSLockHolder::init() |
| 71 | { |
| 72 | m_vm->apiLock().lock(); |
| 73 | } |
| 74 | |
| 75 | JSLockHolder::~JSLockHolder() |
| 76 | { |
| 77 | RefPtr<JSLock> apiLock(&m_vm->apiLock()); |
| 78 | m_vm = nullptr; |
| 79 | apiLock->unlock(); |
| 80 | } |
| 81 | |
| 82 | JSLock::JSLock(VM* vm) |
| 83 | : m_lockCount(0) |
| 84 | , m_lockDropDepth(0) |
| 85 | , m_vm(vm) |
| 86 | , m_entryAtomicStringTable(nullptr) |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | JSLock::~JSLock() |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | void JSLock::willDestroyVM(VM* vm) |
| 95 | { |
| 96 | ASSERT_UNUSED(vm, m_vm == vm); |
| 97 | m_vm = nullptr; |
| 98 | } |
| 99 | |
| 100 | void JSLock::lock() |
| 101 | { |
| 102 | lock(1); |
| 103 | } |
| 104 | |
| 105 | void JSLock::lock(intptr_t lockCount) |
| 106 | { |
| 107 | ASSERT(lockCount > 0); |
| 108 | bool success = m_lock.tryLock(); |
| 109 | if (UNLIKELY(!success)) { |
| 110 | if (currentThreadIsHoldingLock()) { |
| 111 | m_lockCount += lockCount; |
| 112 | return; |
| 113 | } |
| 114 | m_lock.lock(); |
| 115 | } |
| 116 | |
| 117 | m_ownerThread = &Thread::current(); |
| 118 | WTF::storeStoreFence(); |
| 119 | m_hasOwnerThread = true; |
| 120 | ASSERT(!m_lockCount); |
| 121 | m_lockCount = lockCount; |
| 122 | |
| 123 | didAcquireLock(); |
| 124 | } |
| 125 | |
| 126 | void JSLock::didAcquireLock() |
| 127 | { |
| 128 | // FIXME: What should happen to the per-thread identifier table if we don't have a VM? |
| 129 | if (!m_vm) |
| 130 | return; |
| 131 | |
| 132 | Thread& thread = Thread::current(); |
| 133 | ASSERT(!m_entryAtomicStringTable); |
| 134 | m_entryAtomicStringTable = thread.setCurrentAtomicStringTable(m_vm->atomicStringTable()); |
| 135 | ASSERT(m_entryAtomicStringTable); |
| 136 | |
| 137 | m_vm->setLastStackTop(thread.savedLastStackTop()); |
| 138 | ASSERT(thread.stack().contains(m_vm->lastStackTop())); |
| 139 | |
| 140 | if (m_vm->heap.hasAccess()) |
| 141 | m_shouldReleaseHeapAccess = false; |
| 142 | else { |
| 143 | m_vm->heap.acquireAccess(); |
| 144 | m_shouldReleaseHeapAccess = true; |
| 145 | } |
| 146 | |
| 147 | RELEASE_ASSERT(!m_vm->stackPointerAtVMEntry()); |
| 148 | void* p = currentStackPointer(); |
| 149 | m_vm->setStackPointerAtVMEntry(p); |
| 150 | |
| 151 | if (m_vm->heap.machineThreads().addCurrentThread()) { |
| 152 | if (isKernTCSMAvailable()) |
| 153 | enableKernTCSM(); |
| 154 | } |
| 155 | |
| 156 | #if ENABLE(WEBASSEMBLY) |
| 157 | if (Wasm::isSupported()) |
| 158 | Wasm::startTrackingCurrentThread(); |
| 159 | #endif |
| 160 | |
| 161 | #if HAVE(MACH_EXCEPTIONS) |
| 162 | registerThreadForMachExceptionHandling(Thread::current()); |
| 163 | #endif |
| 164 | |
| 165 | // Note: everything below must come after addCurrentThread(). |
| 166 | m_vm->traps().notifyGrabAllLocks(); |
| 167 | |
| 168 | m_vm->firePrimitiveGigacageEnabledIfNecessary(); |
| 169 | |
| 170 | #if ENABLE(SAMPLING_PROFILER) |
| 171 | if (SamplingProfiler* samplingProfiler = m_vm->samplingProfiler()) |
| 172 | samplingProfiler->noticeJSLockAcquisition(); |
| 173 | #endif |
| 174 | } |
| 175 | |
| 176 | void JSLock::unlock() |
| 177 | { |
| 178 | unlock(1); |
| 179 | } |
| 180 | |
| 181 | void JSLock::unlock(intptr_t unlockCount) |
| 182 | { |
| 183 | RELEASE_ASSERT(currentThreadIsHoldingLock()); |
| 184 | ASSERT(m_lockCount >= unlockCount); |
| 185 | |
| 186 | // Maintain m_lockCount while calling willReleaseLock() so that its callees know that |
| 187 | // they still have the lock. |
| 188 | if (unlockCount == m_lockCount) |
| 189 | willReleaseLock(); |
| 190 | |
| 191 | m_lockCount -= unlockCount; |
| 192 | |
| 193 | if (!m_lockCount) { |
| 194 | m_hasOwnerThread = false; |
| 195 | m_lock.unlock(); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void JSLock::willReleaseLock() |
| 200 | { |
| 201 | RefPtr<VM> vm = m_vm; |
| 202 | if (vm) { |
| 203 | vm->drainMicrotasks(); |
| 204 | |
| 205 | if (!vm->topCallFrame) |
| 206 | vm->clearLastException(); |
| 207 | |
| 208 | vm->heap.releaseDelayedReleasedObjects(); |
| 209 | vm->setStackPointerAtVMEntry(nullptr); |
| 210 | |
| 211 | if (m_shouldReleaseHeapAccess) |
| 212 | vm->heap.releaseAccess(); |
| 213 | } |
| 214 | |
| 215 | if (m_entryAtomicStringTable) { |
| 216 | Thread::current().setCurrentAtomicStringTable(m_entryAtomicStringTable); |
| 217 | m_entryAtomicStringTable = nullptr; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void JSLock::lock(ExecState* exec) |
| 222 | { |
| 223 | exec->vm().apiLock().lock(); |
| 224 | } |
| 225 | |
| 226 | void JSLock::unlock(ExecState* exec) |
| 227 | { |
| 228 | exec->vm().apiLock().unlock(); |
| 229 | } |
| 230 | |
| 231 | // This function returns the number of locks that were dropped. |
| 232 | unsigned JSLock::dropAllLocks(DropAllLocks* dropper) |
| 233 | { |
| 234 | if (!currentThreadIsHoldingLock()) |
| 235 | return 0; |
| 236 | |
| 237 | ++m_lockDropDepth; |
| 238 | |
| 239 | dropper->setDropDepth(m_lockDropDepth); |
| 240 | |
| 241 | Thread& thread = Thread::current(); |
| 242 | thread.setSavedStackPointerAtVMEntry(m_vm->stackPointerAtVMEntry()); |
| 243 | thread.setSavedLastStackTop(m_vm->lastStackTop()); |
| 244 | |
| 245 | unsigned droppedLockCount = m_lockCount; |
| 246 | unlock(droppedLockCount); |
| 247 | |
| 248 | return droppedLockCount; |
| 249 | } |
| 250 | |
| 251 | void JSLock::grabAllLocks(DropAllLocks* dropper, unsigned droppedLockCount) |
| 252 | { |
| 253 | // If no locks were dropped, nothing to do! |
| 254 | if (!droppedLockCount) |
| 255 | return; |
| 256 | |
| 257 | ASSERT(!currentThreadIsHoldingLock()); |
| 258 | lock(droppedLockCount); |
| 259 | |
| 260 | while (dropper->dropDepth() != m_lockDropDepth) { |
| 261 | unlock(droppedLockCount); |
| 262 | Thread::yield(); |
| 263 | lock(droppedLockCount); |
| 264 | } |
| 265 | |
| 266 | --m_lockDropDepth; |
| 267 | |
| 268 | Thread& thread = Thread::current(); |
| 269 | m_vm->setStackPointerAtVMEntry(thread.savedStackPointerAtVMEntry()); |
| 270 | m_vm->setLastStackTop(thread.savedLastStackTop()); |
| 271 | } |
| 272 | |
| 273 | JSLock::DropAllLocks::DropAllLocks(VM* vm) |
| 274 | : m_droppedLockCount(0) |
| 275 | // If the VM is in the middle of being destroyed then we don't want to resurrect it |
| 276 | // by allowing DropAllLocks to ref it. By this point the JSLock has already been |
| 277 | // released anyways, so it doesn't matter that DropAllLocks is a no-op. |
| 278 | , m_vm(vm->refCount() ? vm : nullptr) |
| 279 | { |
| 280 | if (!m_vm) |
| 281 | return; |
| 282 | RELEASE_ASSERT(!m_vm->apiLock().currentThreadIsHoldingLock() || !m_vm->isCollectorBusyOnCurrentThread()); |
| 283 | m_droppedLockCount = m_vm->apiLock().dropAllLocks(this); |
| 284 | } |
| 285 | |
| 286 | JSLock::DropAllLocks::DropAllLocks(ExecState* exec) |
| 287 | : DropAllLocks(exec ? &exec->vm() : nullptr) |
| 288 | { |
| 289 | } |
| 290 | |
| 291 | JSLock::DropAllLocks::DropAllLocks(VM& vm) |
| 292 | : DropAllLocks(&vm) |
| 293 | { |
| 294 | } |
| 295 | |
| 296 | JSLock::DropAllLocks::~DropAllLocks() |
| 297 | { |
| 298 | if (!m_vm) |
| 299 | return; |
| 300 | m_vm->apiLock().grabAllLocks(this, m_droppedLockCount); |
| 301 | } |
| 302 | |
| 303 | } // namespace JSC |
| 304 | |