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