Repository navigation
Expand file tree
/
Copy pathJSLock.cpp
More file actions
422 lines (344 loc) · 13.1 KB
/
Copy pathJSLock.cpp
File metadata and controls
422 lines (344 loc) · 13.1 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* Copyright (C) 2005-2026 Apple Inc. All rights reserved.
* Copyright (C) 2026 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the NU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA
*
*/
#include "config.h"
#include "JSLock.h"
#include "HeapInlines.h"
#include "JSGlobalObject.h"
#include "MachineStackMarker.h"
#include "SamplingProfiler.h"
#include "VMTrapsInlines.h"
#include <wtf/SpinBackoff.h>
#include <wtf/StackPointer.h>
#include <wtf/Threading.h>
#include <wtf/threads/Signals.h>
#if USE(WEB_THREAD)
#include <wtf/ios/WebCoreThread.h>
#endif
#if PLATFORM(COCOA)
#include <wtf/cocoa/RuntimeApplicationChecksCocoa.h>
#endif
namespace JSC {
JSLockHolder::JSLockHolder(JSGlobalObject* globalObject)
: JSLockHolder(globalObject->vm())
{
}
JSLockHolder::JSLockHolder(VM* vm)
: JSLockHolder(*vm)
{
}
JSLockHolder::JSLockHolder(VM& vm)
: m_vm(&vm)
{
protect(m_vm->apiLock())->lock();
}
JSLockHolder::~JSLockHolder()
{
RefPtr<JSLock> apiLock(&m_vm->apiLock());
m_vm = nullptr;
apiLock->unlock();
}
JSLock::JSLock(VM* vm)
: m_lockCount(0)
, m_lockDropDepth(0)
, m_vm(vm)
, m_entryAtomStringTable(nullptr)
{
}
JSLock::~JSLock() = default;
void JSLock::willDestroyVM(VM* vm)
{
ASSERT_UNUSED(vm, m_vm == vm);
m_vm = nullptr;
}
void JSLock::lock()
{
lock(1);
}
// Use WTF_IGNORES_THREAD_SAFETY_ANALYSIS because this function conditionally unlocks m_lock, which
// is not supported by analysis.
void JSLock::lock(intptr_t lockCount) WTF_IGNORES_THREAD_SAFETY_ANALYSIS
{
ASSERT(lockCount > 0);
#if USE(WEB_THREAD)
if (m_isWebThreadAware) {
ASSERT(WebCoreWebThreadIsEnabled && WebCoreWebThreadIsEnabled());
WebCoreWebThreadLock();
}
#endif
bool success = m_lock.tryLock();
if (!success) [[unlikely]] {
if (currentThreadIsHoldingLock()) {
m_lockCount += lockCount;
return;
}
m_lock.lock();
}
m_ownerThread = &Thread::currentSingleton();
m_hasOwnerThread.store(true, std::memory_order_release);
ASSERT(!m_lockCount);
m_lockCount = lockCount;
didAcquireLock();
}
void JSLock::didAcquireLock()
{
// FIXME: What should happen to the per-thread identifier table if we don't have a VM?
if (!m_vm)
return;
auto& thread = Thread::currentSingleton();
ASSERT(!m_entryAtomStringTable);
m_entryAtomStringTable = thread.setCurrentAtomStringTable(m_vm->atomStringTable());
ASSERT(m_entryAtomStringTable);
m_vm->setLastStackTop(thread);
if (m_vm->heap.hasAccess())
m_shouldReleaseHeapAccess = false;
else {
m_vm->heap.acquireAccess();
m_shouldReleaseHeapAccess = true;
}
RELEASE_ASSERT(!m_vm->stackPointerAtVMEntry());
void* p = currentStackPointer();
m_vm->setStackPointerAtVMEntry(p);
if (thread.uid() != m_lastOwnerThread) {
m_lastOwnerThread = thread.uid();
if (m_vm->heap.machineThreads().addCurrentThread()) {
if (isKernTCSMAvailable())
enableKernTCSM();
}
}
// Note: everything below must come after addCurrentThread().
m_vm->traps().invalidateCodeBlocksForPendingTraps();
#if ENABLE(SAMPLING_PROFILER)
{
SamplingProfiler* samplingProfiler = m_vm->samplingProfiler();
if (samplingProfiler) [[unlikely]]
samplingProfiler->noticeJSLockAcquisition();
}
#endif
}
void JSLock::unlock()
{
unlock(1);
}
#if PLATFORM(COCOA) && CPU(ADDRESS64) && CPU(ARM64)
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
// FIXME: rdar://168614004
NO_RETURN_DUE_TO_CRASH NEVER_INLINE void JSLock::dumpInfoAndCrashForLockNotOwned() // __attribute__((optnone))
{
size_t pageSize = WTF::pageSize();
RELEASE_ASSERT(isPowerOfTwo(pageSize));
uintptr_t pageMask = ~(static_cast<uintptr_t>(pageSize) - 1);
uintptr_t* thisAsIntPtr = std::bit_cast<uintptr_t*>(this);
uintptr_t thisAsInt = std::bit_cast<uintptr_t>(this);
uintptr_t thisEndAsInt = thisAsInt + sizeof(JSLock);
uintptr_t blockStartAsInt = thisAsInt & pageMask;
uintptr_t blockEndAsInt = blockStartAsInt + pageSize;
char* blockStart = std::bit_cast<char*>(blockStartAsInt);
register uint64_t dumpState __asm__("x28");
#define updateDumpState(newState, used1, used2, used3) do { \
WTF::compilerFence(); \
__asm__ volatile ("mov %0, #" #newState : "=r"(dumpState) : "r"(used1), "r"(used2), "r"(used3)); \
WTF::compilerFence(); \
} while (false)
updateDumpState(0x1111, dumpState, dumpState, dumpState);
register void* currentThread __asm__("x27") = &Thread::currentSingleton();
updateDumpState(0x2222, currentThread, dumpState, dumpState);
// Checks if the this pointer is corrupted. Being out of the page bounds is 1 example of corruption.
bool lockIsWithinPageBoundary = (blockStartAsInt <= thisAsInt) && (thisEndAsInt <= blockEndAsInt);
register uintptr_t miscState __asm__("x26") = lockIsWithinPageBoundary;
updateDumpState(0x3333, miscState, dumpState, dumpState);
register uintptr_t lockWord0 __asm__("x25") = thisAsIntPtr[0];
updateDumpState(0x4444, lockWord0, dumpState, dumpState);
register void* ownerThread __asm__("x24") = m_ownerThread.get();
updateDumpState(0x5555, ownerThread, dumpState, dumpState);
register uintptr_t lockWord2 __asm__("x23") = thisAsIntPtr[2];
register uintptr_t lockWord3 __asm__("x22") = thisAsIntPtr[3];
updateDumpState(0x6666, lockWord2, lockWord3, dumpState);
miscState |= (!!m_vm) << 8; // Check if VM is null.
updateDumpState(0x7777, miscState, dumpState, dumpState);
// Check if the page is zero.
register uintptr_t numZeroBytesBeforeAfter __asm__("x21") = 0;
uintptr_t totalZeroBytesInPage = 0;
uintptr_t currentZeroBytes = 0;
// Count zero bytes before JSLock.
uintptr_t bytesBeforeLock = thisAsInt - blockStartAsInt;
for (auto mem = blockStart; mem < blockStart + bytesBeforeLock; mem++) {
bool byteIsZero = !*mem;
if (byteIsZero)
currentZeroBytes++;
}
numZeroBytesBeforeAfter = currentZeroBytes;
numZeroBytesBeforeAfter |= bytesBeforeLock << 16;
updateDumpState(0x8888, numZeroBytesBeforeAfter, bytesBeforeLock, currentZeroBytes);
totalZeroBytesInPage += currentZeroBytes;
// Count zero bytes after JSLock.
currentZeroBytes = 0;
uintptr_t bytesAfterBlock = pageSize - (thisAsInt + sizeof(JSLock));
for (auto mem = blockStart + bytesBeforeLock + sizeof(JSLock); mem < blockStart + pageSize; mem++) {
bool byteIsZero = !*mem;
if (byteIsZero)
currentZeroBytes++;
}
numZeroBytesBeforeAfter |= currentZeroBytes << 32;
numZeroBytesBeforeAfter |= bytesAfterBlock << 48;
updateDumpState(0x9999, numZeroBytesBeforeAfter, bytesAfterBlock, currentZeroBytes);
totalZeroBytesInPage += currentZeroBytes;
register uintptr_t numZeroBytesInLock __asm__("x20") = 0;
currentZeroBytes = 0;
for (auto mem = blockStart + bytesBeforeLock; mem < blockStart + bytesBeforeLock + sizeof(JSLock); mem++) {
bool byteIsZero = !*mem;
if (byteIsZero)
currentZeroBytes++;
}
numZeroBytesInLock = currentZeroBytes;
numZeroBytesInLock |= sizeof(JSLock) << 16;
updateDumpState(0xAAAA, numZeroBytesInLock, currentZeroBytes, dumpState);
totalZeroBytesInPage += currentZeroBytes;
numZeroBytesInLock |= totalZeroBytesInPage << 32;
updateDumpState(0xBBBB, numZeroBytesInLock, totalZeroBytesInPage, currentZeroBytes);
register VM* vmPtr __asm__("r19") = m_vm;
register AtomStringTable* atomStringTable __asm__("x15") = m_entryAtomStringTable;
register JSLock* thisPtr __asm__("x14") = this;
updateDumpState(0xCCCC, vmPtr, atomStringTable, thisPtr);
__asm__ volatile (WTF_FATAL_CRASH_INST : : "r"(dumpState), "r"(miscState), "r"(lockWord0), "r"(currentThread), "r"(ownerThread), "r"(lockWord2), "r"(lockWord3), "r"(numZeroBytesBeforeAfter), "r"(numZeroBytesInLock), "r"(vmPtr), "r"(atomStringTable), "r"(thisPtr));
__builtin_unreachable();
#undef updateDumpState
}
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
#endif
// Use WTF_IGNORES_THREAD_SAFETY_ANALYSIS because this function conditionally unlocks m_lock, which
// is not supported by analysis.
void JSLock::unlock(intptr_t unlockCount) WTF_IGNORES_THREAD_SAFETY_ANALYSIS
{
#if PLATFORM(COCOA) && CPU(ADDRESS64) && CPU(ARM64)
if (!currentThreadIsHoldingLock()) [[unlikely]]
dumpInfoAndCrashForLockNotOwned();
#else
RELEASE_ASSERT(currentThreadIsHoldingLock());
#endif
ASSERT(m_lockCount >= unlockCount);
// Maintain m_lockCount while calling willReleaseLock() so that its callees know that
// they still have the lock.
if (unlockCount == m_lockCount)
willReleaseLock();
m_lockCount -= unlockCount;
if (!m_lockCount) {
m_hasOwnerThread.store(false, std::memory_order_release);
m_lock.unlock();
}
}
void JSLock::willReleaseLock()
{
{
RefPtr protectedVM { m_vm };
if (protectedVM) {
static bool useLegacyDrain = false;
#if PLATFORM(COCOA)
static std::once_flag once;
std::call_once(once, [] {
useLegacyDrain = !linkedOnOrAfterSDKWithBehavior(SDKAlignedBehavior::DoesNotDrainTheMicrotaskQueueWhenCallingObjC);
});
#endif
if (!m_lockDropDepth || useLegacyDrain)
protectedVM->drainMicrotasks();
if (!protectedVM->topCallFrame)
protectedVM->clearLastException();
protectedVM->heap.releaseDelayedReleasedObjects();
protectedVM->setStackPointerAtVMEntry(nullptr);
if (m_shouldReleaseHeapAccess)
protectedVM->heap.releaseAccess();
}
}
if (m_entryAtomStringTable) {
Thread::currentSingleton().setCurrentAtomStringTable(m_entryAtomStringTable);
m_entryAtomStringTable = nullptr;
}
}
void JSLock::lock(JSGlobalObject* globalObject)
{
protect(globalObject->vm().apiLock())->lock();
}
void JSLock::unlock(JSGlobalObject* globalObject)
{
protect(globalObject->vm().apiLock())->unlock();
}
// This function returns the number of locks that were dropped.
unsigned JSLock::dropAllLocks(DropAllLocks* dropper)
{
if (!currentThreadIsHoldingLock())
return 0;
++m_lockDropDepth;
dropper->setDropDepth(m_lockDropDepth);
auto& thread = Thread::currentSingleton();
thread.setSavedStackPointerAtVMEntry(m_vm->stackPointerAtVMEntry());
thread.setSavedLastStackTop(m_vm->lastStackTop());
unsigned droppedLockCount = m_lockCount;
unlock(droppedLockCount);
return droppedLockCount;
}
void JSLock::grabAllLocks(DropAllLocks* dropper, unsigned droppedLockCount)
{
// If no locks were dropped, nothing to do!
if (!droppedLockCount)
return;
ASSERT(!currentThreadIsHoldingLock());
lock(droppedLockCount);
SpinBackoff backoff;
while (dropper->dropDepth() != m_lockDropDepth) {
unlock(droppedLockCount);
backoff.spinOnce();
lock(droppedLockCount);
}
--m_lockDropDepth;
auto& thread = Thread::currentSingleton();
m_vm->setStackPointerAtVMEntry(thread.savedStackPointerAtVMEntry());
m_vm->setLastStackTop(thread);
}
JSLock::DropAllLocks::DropAllLocks(VM* vm)
: m_droppedLockCount(0)
// If the VM is in the middle of being destroyed then we don't want to resurrect it
// by allowing DropAllLocks to ref it. By this point the JSLock has already been
// released anyways, so it doesn't matter that DropAllLocks is a no-op.
, m_vm(vm->heap.isShuttingDown() ? nullptr : vm)
{
if (!m_vm)
return;
// Contrary to intuition, DropAllLocks does not require that we are actually holding
// the JSLock before getting here. Its goal is to release the lock if it is held. So,
// if the lock isn't already held, there's nothing to do, and that's fine.
// See https://bugs.webkit.org/show_bug.cgi?id=139654#c11.
RELEASE_ASSERT(!m_vm->currentThreadIsHoldingAPILock() || !m_vm->isCollectorBusyOnCurrentThread(), m_vm->currentThreadIsHoldingAPILock(), m_vm->isCollectorBusyOnCurrentThread());
m_droppedLockCount = protect(m_vm->apiLock())->dropAllLocks(this);
}
JSLock::DropAllLocks::DropAllLocks(JSGlobalObject* globalObject)
: DropAllLocks(globalObject ? &globalObject->vm() : nullptr)
{
}
JSLock::DropAllLocks::DropAllLocks(VM& vm)
: DropAllLocks(&vm)
{
}
JSLock::DropAllLocks::~DropAllLocks()
{
if (!m_vm)
return;
protect(m_vm->apiLock())->grabAllLocks(this, m_droppedLockCount);
}
} // namespace JSC