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
|
/*
* Copyright (C) 2003-2023 Apple Inc. All rights reserved.
* Copyright (C) 2007 Eric Seidel <eric@webkit.org>
* Copyright (C) 2009 Acision BV. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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 GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "config.h"
#include "MachineStackMarker.h"
#include "ConservativeRoots.h"
#include "MachineContext.h"
#include <wtf/BitVector.h>
#include <wtf/PageBlock.h>
#include <wtf/StdLibExtras.h>
#include <wtf/TZoneMallocInlines.h>
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
namespace JSC {
WTF_MAKE_TZONE_ALLOCATED_IMPL(MachineThreads);
MachineThreads::MachineThreads()
: m_threadGroup(ThreadGroup::create())
{
}
SUPPRESS_ASAN
void MachineThreads::gatherFromCurrentThread(ConservativeRoots& conservativeRoots, JITStubRoutineSet& jitStubRoutines, CodeBlockSet& codeBlocks, CurrentThreadState& currentThreadState)
{
if (currentThreadState.registerState) {
void* registersBegin = currentThreadState.registerState;
void* registersEnd = reinterpret_cast<void*>(roundUpToMultipleOf<sizeof(void*)>(reinterpret_cast<uintptr_t>(currentThreadState.registerState + 1)));
conservativeRoots.add(registersBegin, registersEnd, jitStubRoutines, codeBlocks);
}
conservativeRoots.add(currentThreadState.stackTop, currentThreadState.stackOrigin, jitStubRoutines, codeBlocks);
}
static inline int osRedZoneAdjustment()
{
int redZoneAdjustment = 0;
#if CPU(X86_64)
// See http://people.freebsd.org/~obrien/amd64-elf-abi.pdf Section 3.2.2.
redZoneAdjustment = -128;
#elif CPU(ARM64)
// See https://developer.apple.com/library/ios/documentation/Xcode/Conceptual/iPhoneOSABIReference/Articles/ARM64FunctionCallingConventions.html#//apple_ref/doc/uid/TP40013702-SW7
redZoneAdjustment = -128;
#endif
return redZoneAdjustment;
}
static std::pair<void*, size_t> captureStack(Thread& thread, void* stackTop)
{
char* begin = reinterpret_cast_ptr<char*>(thread.stack().origin());
char* end = std::bit_cast<char*>(WTF::roundUpToMultipleOf<sizeof(void*)>(reinterpret_cast<uintptr_t>(stackTop)));
ASSERT(begin >= end);
char* endWithRedZone = end + osRedZoneAdjustment();
ASSERT(WTF::roundUpToMultipleOf<sizeof(void*)>(reinterpret_cast<uintptr_t>(endWithRedZone)) == reinterpret_cast<uintptr_t>(endWithRedZone));
if (endWithRedZone < thread.stack().end())
endWithRedZone = reinterpret_cast_ptr<char*>(thread.stack().end());
std::swap(begin, endWithRedZone);
return std::make_pair(begin, endWithRedZone - begin);
}
SUPPRESS_ASAN
static void copyMemory(void* dst, const void* src, size_t size)
{
size_t dstAsSize = reinterpret_cast<size_t>(dst);
size_t srcAsSize = reinterpret_cast<size_t>(src);
RELEASE_ASSERT(dstAsSize == WTF::roundUpToMultipleOf<sizeof(CPURegister)>(dstAsSize));
RELEASE_ASSERT(srcAsSize == WTF::roundUpToMultipleOf<sizeof(CPURegister)>(srcAsSize));
RELEASE_ASSERT(size == WTF::roundUpToMultipleOf<sizeof(CPURegister)>(size));
CPURegister* dstPtr = reinterpret_cast<CPURegister*>(dst);
const CPURegister* srcPtr = reinterpret_cast<const CPURegister*>(src);
size /= sizeof(CPURegister);
while (size--)
*dstPtr++ = *srcPtr++;
}
// This function must not call malloc(), free(), or any other function that might
// acquire a lock. Since 'thread' is suspended, trying to acquire a lock
// will deadlock if 'thread' holds that lock.
// This function, specifically the memory copying, was causing problems with Address Sanitizer in
// apps. Since we cannot disallow the system memcpy we must use our own naive implementation,
// copyMemory, for ASan to work on either instrumented or non-instrumented builds. This is not a
// significant performance loss as tryCopyOtherThreadStack is only called as part of an O(heapsize)
// operation. As the heap is generally much larger than the stack the performance hit is minimal.
// See: https://bugs.webkit.org/show_bug.cgi?id=146297
void MachineThreads::tryCopyOtherThreadStack(const ThreadSuspendLocker& locker, Thread& thread, void* buffer, size_t capacity, size_t* size)
{
PlatformRegisters registers;
size_t registersSize = thread.getRegisters(locker, registers);
// This is a workaround for <rdar://problem/27607384>. libdispatch recycles work
// queue threads without running pthread exit destructors. This can cause us to scan a
// thread during work queue initialization, when the stack pointer is null.
if (UNLIKELY(!MachineContext::stackPointer(registers))) {
*size = 0;
return;
}
std::pair<void*, size_t> stack = captureStack(thread, MachineContext::stackPointer(registers));
bool canCopy = *size + registersSize + stack.second <= capacity;
if (canCopy)
copyMemory(static_cast<char*>(buffer) + *size, ®isters, registersSize);
*size += registersSize;
if (canCopy)
copyMemory(static_cast<char*>(buffer) + *size, stack.first, stack.second);
*size += stack.second;
}
bool MachineThreads::tryCopyOtherThreadStacks(const AbstractLocker& locker, void* buffer, size_t capacity, size_t* size, Thread& currentThreadForGC)
{
// Prevent two VMs from suspending each other's threads at the same time,
// which can cause deadlock: <rdar://problem/20300842>.
static Lock suspendLock;
Locker suspendLocker { suspendLock };
*size = 0;
Thread& currentThread = Thread::current();
const ListHashSet<Ref<Thread>>& threads = m_threadGroup->threads(locker);
BitVector isSuspended(threads.size());
{
ThreadSuspendLocker threadSuspendLocker;
{
unsigned index = 0;
for (const Ref<Thread>& thread : threads) {
if (thread.ptr() != ¤tThread
&& thread.ptr() != ¤tThreadForGC) {
auto result = thread->suspend(threadSuspendLocker);
if (result)
isSuspended.set(index);
else {
#if OS(DARWIN)
// These threads will be removed from the ThreadGroup. Thus, we do not do anything here except for reporting.
ASSERT(result.error() != KERN_SUCCESS);
WTFReportError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION,
"JavaScript garbage collection encountered an invalid thread (err 0x%x): Thread [%d/%d: %p].",
result.error(), index, threads.size(), thread.ptr());
#endif
}
}
++index;
}
}
{
unsigned index = 0;
for (auto& thread : threads) {
if (isSuspended.get(index))
tryCopyOtherThreadStack(threadSuspendLocker, thread.get(), buffer, capacity, size);
++index;
}
}
{
unsigned index = 0;
for (auto& thread : threads) {
if (isSuspended.get(index))
thread->resume(threadSuspendLocker);
++index;
}
}
}
return *size <= capacity;
}
static void growBuffer(size_t size, void** buffer, size_t* capacity)
{
if (*buffer)
fastFree(*buffer);
*capacity = WTF::roundUpToMultipleOf(WTF::pageSize(), size * 2);
*buffer = fastMalloc(*capacity);
}
void MachineThreads::gatherConservativeRoots(ConservativeRoots& conservativeRoots, JITStubRoutineSet& jitStubRoutines, CodeBlockSet& codeBlocks, CurrentThreadState* currentThreadState, Thread* currentThread)
{
if (currentThreadState)
gatherFromCurrentThread(conservativeRoots, jitStubRoutines, codeBlocks, *currentThreadState);
size_t size;
size_t capacity = 0;
void* buffer = nullptr;
Locker locker { m_threadGroup->getLock() };
while (!tryCopyOtherThreadStacks(locker, buffer, capacity, &size, *currentThread))
growBuffer(size, &buffer, &capacity);
if (!buffer)
return;
conservativeRoots.add(buffer, static_cast<char*>(buffer) + size, jitStubRoutines, codeBlocks);
fastFree(buffer);
}
NEVER_INLINE int callWithCurrentThreadState(const ScopedLambda<void(CurrentThreadState&)>& lambda)
{
DECLARE_AND_COMPUTE_CURRENT_THREAD_STATE(state);
lambda(state);
return 42; // Suppress tail call optimization.
}
} // namespace JSC
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
|