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 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
|
/*
* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 "Heap.h"
#include "JSArray.h"
#include "VM.h"
#include <setjmp.h>
#include <stdlib.h>
#include <wtf/StdLibExtras.h>
#if OS(DARWIN)
#include <mach/mach_init.h>
#include <mach/mach_port.h>
#include <mach/task.h>
#include <mach/thread_act.h>
#include <mach/vm_map.h>
#elif OS(WINDOWS)
#include <windows.h>
#include <malloc.h>
#elif OS(UNIX)
#include <sys/mman.h>
#include <unistd.h>
#if OS(SOLARIS)
#include <thread.h>
#else
#include <pthread.h>
#endif
#if HAVE(PTHREAD_NP_H)
#include <pthread_np.h>
#endif
#if OS(QNX)
#include <fcntl.h>
#include <sys/procfs.h>
#include <stdio.h>
#include <errno.h>
#endif
#if USE(PTHREADS) && !OS(WINDOWS) && !OS(DARWIN)
#include <signal.h>
#endif
#endif
using namespace WTF;
namespace JSC {
static inline void swapIfBackwards(void*& begin, void*& end)
{
#if OS(WINCE)
if (begin <= end)
return;
std::swap(begin, end);
#else
UNUSED_PARAM(begin);
UNUSED_PARAM(end);
#endif
}
#if OS(DARWIN)
typedef mach_port_t PlatformThread;
#elif OS(WINDOWS)
typedef HANDLE PlatformThread;
#elif USE(PTHREADS)
typedef pthread_t PlatformThread;
static const int SigThreadSuspendResume = SIGUSR2;
#if defined(SA_RESTART)
static void pthreadSignalHandlerSuspendResume(int)
{
sigset_t signalSet;
sigemptyset(&signalSet);
sigaddset(&signalSet, SigThreadSuspendResume);
sigsuspend(&signalSet);
}
#endif
#endif
class MachineThreads::Thread {
WTF_MAKE_FAST_ALLOCATED;
public:
Thread(const PlatformThread& platThread, void* base)
: platformThread(platThread)
, stackBase(base)
{
#if USE(PTHREADS) && !OS(WINDOWS) && !OS(DARWIN) && defined(SA_RESTART)
// if we have SA_RESTART, enable SIGUSR2 debugging mechanism
struct sigaction action;
action.sa_handler = pthreadSignalHandlerSuspendResume;
sigemptyset(&action.sa_mask);
action.sa_flags = SA_RESTART;
sigaction(SigThreadSuspendResume, &action, 0);
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SigThreadSuspendResume);
pthread_sigmask(SIG_UNBLOCK, &mask, 0);
#endif
}
Thread* next;
PlatformThread platformThread;
void* stackBase;
};
MachineThreads::MachineThreads(Heap* heap)
: m_registeredThreads(0)
, m_threadSpecific(0)
#if !ASSERT_DISABLED
, m_heap(heap)
#endif
{
UNUSED_PARAM(heap);
}
MachineThreads::~MachineThreads()
{
if (m_threadSpecific)
threadSpecificKeyDelete(m_threadSpecific);
MutexLocker registeredThreadsLock(m_registeredThreadsMutex);
for (Thread* t = m_registeredThreads; t;) {
Thread* next = t->next;
delete t;
t = next;
}
}
static inline PlatformThread getCurrentPlatformThread()
{
#if OS(DARWIN)
return pthread_mach_thread_np(pthread_self());
#elif OS(WINDOWS)
return GetCurrentThread();
#elif USE(PTHREADS)
return pthread_self();
#endif
}
static inline bool equalThread(const PlatformThread& first, const PlatformThread& second)
{
#if OS(DARWIN) || OS(WINDOWS)
return first == second;
#elif USE(PTHREADS)
return !!pthread_equal(first, second);
#else
#error Need a way to compare threads on this platform
#endif
}
void MachineThreads::makeUsableFromMultipleThreads()
{
if (m_threadSpecific)
return;
threadSpecificKeyCreate(&m_threadSpecific, removeThread);
}
void MachineThreads::addCurrentThread()
{
ASSERT(!m_heap->vm()->exclusiveThread || m_heap->vm()->exclusiveThread == currentThread());
if (!m_threadSpecific || threadSpecificGet(m_threadSpecific))
return;
threadSpecificSet(m_threadSpecific, this);
Thread* thread = new Thread(getCurrentPlatformThread(), wtfThreadData().stack().origin());
MutexLocker lock(m_registeredThreadsMutex);
thread->next = m_registeredThreads;
m_registeredThreads = thread;
}
void MachineThreads::removeThread(void* p)
{
if (p)
static_cast<MachineThreads*>(p)->removeCurrentThread();
}
void MachineThreads::removeCurrentThread()
{
PlatformThread currentPlatformThread = getCurrentPlatformThread();
MutexLocker lock(m_registeredThreadsMutex);
if (equalThread(currentPlatformThread, m_registeredThreads->platformThread)) {
Thread* t = m_registeredThreads;
m_registeredThreads = m_registeredThreads->next;
delete t;
} else {
Thread* last = m_registeredThreads;
Thread* t;
for (t = m_registeredThreads->next; t; t = t->next) {
if (equalThread(t->platformThread, currentPlatformThread)) {
last->next = t->next;
break;
}
last = t;
}
ASSERT(t); // If t is NULL, we never found ourselves in the list.
delete t;
}
}
#if COMPILER(GCC)
#define REGISTER_BUFFER_ALIGNMENT __attribute__ ((aligned (sizeof(void*))))
#else
#define REGISTER_BUFFER_ALIGNMENT
#endif
void MachineThreads::gatherFromCurrentThread(ConservativeRoots& conservativeRoots, void* stackCurrent)
{
// setjmp forces volatile registers onto the stack
jmp_buf registers REGISTER_BUFFER_ALIGNMENT;
#if COMPILER(MSVC)
#pragma warning(push)
#pragma warning(disable: 4611)
#endif
setjmp(registers);
#if COMPILER(MSVC)
#pragma warning(pop)
#endif
void* registersBegin = ®isters;
void* registersEnd = reinterpret_cast<void*>(roundUpToMultipleOf<sizeof(void*)>(reinterpret_cast<uintptr_t>(®isters + 1)));
swapIfBackwards(registersBegin, registersEnd);
conservativeRoots.add(registersBegin, registersEnd);
void* stackBegin = stackCurrent;
void* stackEnd = wtfThreadData().stack().origin();
swapIfBackwards(stackBegin, stackEnd);
conservativeRoots.add(stackBegin, stackEnd);
}
static inline void suspendThread(const PlatformThread& platformThread)
{
#if OS(DARWIN)
thread_suspend(platformThread);
#elif OS(WINDOWS)
SuspendThread(platformThread);
#elif USE(PTHREADS)
pthread_kill(platformThread, SigThreadSuspendResume);
#else
#error Need a way to suspend threads on this platform
#endif
}
static inline void resumeThread(const PlatformThread& platformThread)
{
#if OS(DARWIN)
thread_resume(platformThread);
#elif OS(WINDOWS)
ResumeThread(platformThread);
#elif USE(PTHREADS)
pthread_kill(platformThread, SigThreadSuspendResume);
#else
#error Need a way to resume threads on this platform
#endif
}
typedef unsigned long usword_t; // word size, assumed to be either 32 or 64 bit
#if OS(DARWIN)
#if CPU(X86)
typedef i386_thread_state_t PlatformThreadRegisters;
#elif CPU(X86_64)
typedef x86_thread_state64_t PlatformThreadRegisters;
#elif CPU(PPC)
typedef ppc_thread_state_t PlatformThreadRegisters;
#elif CPU(PPC64)
typedef ppc_thread_state64_t PlatformThreadRegisters;
#elif CPU(ARM)
typedef arm_thread_state_t PlatformThreadRegisters;
#else
#error Unknown Architecture
#endif
#elif OS(WINDOWS)
typedef CONTEXT PlatformThreadRegisters;
#elif OS(QNX)
typedef struct _debug_thread_info PlatformThreadRegisters;
#elif USE(PTHREADS)
typedef pthread_attr_t PlatformThreadRegisters;
#else
#error Need a thread register struct for this platform
#endif
static size_t getPlatformThreadRegisters(const PlatformThread& platformThread, PlatformThreadRegisters& regs)
{
#if OS(DARWIN)
#if CPU(X86)
unsigned user_count = sizeof(regs)/sizeof(int);
thread_state_flavor_t flavor = i386_THREAD_STATE;
#elif CPU(X86_64)
unsigned user_count = x86_THREAD_STATE64_COUNT;
thread_state_flavor_t flavor = x86_THREAD_STATE64;
#elif CPU(PPC)
unsigned user_count = PPC_THREAD_STATE_COUNT;
thread_state_flavor_t flavor = PPC_THREAD_STATE;
#elif CPU(PPC64)
unsigned user_count = PPC_THREAD_STATE64_COUNT;
thread_state_flavor_t flavor = PPC_THREAD_STATE64;
#elif CPU(ARM)
unsigned user_count = ARM_THREAD_STATE_COUNT;
thread_state_flavor_t flavor = ARM_THREAD_STATE;
#else
#error Unknown Architecture
#endif
kern_return_t result = thread_get_state(platformThread, flavor, (thread_state_t)®s, &user_count);
if (result != KERN_SUCCESS) {
WTFReportFatalError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION,
"JavaScript garbage collection failed because thread_get_state returned an error (%d). This is probably the result of running inside Rosetta, which is not supported.", result);
CRASH();
}
return user_count * sizeof(usword_t);
// end OS(DARWIN)
#elif OS(WINDOWS)
regs.ContextFlags = CONTEXT_INTEGER | CONTEXT_CONTROL;
GetThreadContext(platformThread, ®s);
return sizeof(CONTEXT);
#elif OS(QNX)
memset(®s, 0, sizeof(regs));
regs.tid = platformThread;
// FIXME: If we find this hurts performance, we can consider caching the fd and keeping it open.
int fd = open("/proc/self/as", O_RDONLY);
if (fd == -1) {
LOG_ERROR("Unable to open /proc/self/as (errno: %d)", errno);
CRASH();
}
int rc = devctl(fd, DCMD_PROC_TIDSTATUS, ®s, sizeof(regs), 0);
if (rc != EOK) {
LOG_ERROR("devctl(DCMD_PROC_TIDSTATUS) failed (error: %d)", rc);
CRASH();
}
close(fd);
return sizeof(struct _debug_thread_info);
#elif USE(PTHREADS)
pthread_attr_init(®s);
#if HAVE(PTHREAD_NP_H) || OS(NETBSD)
// e.g. on FreeBSD 5.4, neundorf@kde.org
pthread_attr_get_np(platformThread, ®s);
#else
// FIXME: this function is non-portable; other POSIX systems may have different np alternatives
pthread_getattr_np(platformThread, ®s);
#endif
return 0;
#else
#error Need a way to get thread registers on this platform
#endif
}
static inline void* otherThreadStackPointer(const PlatformThreadRegisters& regs)
{
#if OS(DARWIN)
#if __DARWIN_UNIX03
#if CPU(X86)
return reinterpret_cast<void*>(regs.__esp);
#elif CPU(X86_64)
return reinterpret_cast<void*>(regs.__rsp);
#elif CPU(PPC) || CPU(PPC64)
return reinterpret_cast<void*>(regs.__r1);
#elif CPU(ARM)
return reinterpret_cast<void*>(regs.__sp);
#else
#error Unknown Architecture
#endif
#else // !__DARWIN_UNIX03
#if CPU(X86)
return reinterpret_cast<void*>(regs.esp);
#elif CPU(X86_64)
return reinterpret_cast<void*>(regs.rsp);
#elif CPU(PPC) || CPU(PPC64)
return reinterpret_cast<void*>(regs.r1);
#else
#error Unknown Architecture
#endif
#endif // __DARWIN_UNIX03
// end OS(DARWIN)
#elif OS(WINDOWS)
#if CPU(ARM)
return reinterpret_cast<void*>((uintptr_t) regs.Sp);
#elif CPU(MIPS)
return reinterpret_cast<void*>((uintptr_t) regs.IntSp);
#elif CPU(X86)
return reinterpret_cast<void*>((uintptr_t) regs.Esp);
#elif CPU(X86_64)
return reinterpret_cast<void*>((uintptr_t) regs.Rsp);
#else
#error Unknown Architecture
#endif
#elif OS(QNX)
return reinterpret_cast<void*>((uintptr_t) regs.sp);
#elif USE(PTHREADS)
void* stackBase = 0;
size_t stackSize = 0;
int rc = pthread_attr_getstack(®s, &stackBase, &stackSize);
(void)rc; // FIXME: Deal with error code somehow? Seems fatal.
ASSERT(stackBase);
return static_cast<char*>(stackBase) + stackSize;
#else
#error Need a way to get the stack pointer for another thread on this platform
#endif
}
static void freePlatformThreadRegisters(PlatformThreadRegisters& regs)
{
#if USE(PTHREADS) && !OS(WINDOWS) && !OS(DARWIN) && !OS(QNX)
pthread_attr_destroy(®s);
#else
UNUSED_PARAM(regs);
#endif
}
void MachineThreads::gatherFromOtherThread(ConservativeRoots& conservativeRoots, Thread* thread)
{
PlatformThreadRegisters regs;
size_t regSize = getPlatformThreadRegisters(thread->platformThread, regs);
conservativeRoots.add(static_cast<void*>(®s), static_cast<void*>(reinterpret_cast<char*>(®s) + regSize));
void* stackPointer = otherThreadStackPointer(regs);
void* stackBase = thread->stackBase;
swapIfBackwards(stackPointer, stackBase);
conservativeRoots.add(stackPointer, stackBase);
freePlatformThreadRegisters(regs);
}
void MachineThreads::gatherConservativeRoots(ConservativeRoots& conservativeRoots, void* stackCurrent)
{
gatherFromCurrentThread(conservativeRoots, stackCurrent);
if (m_threadSpecific) {
PlatformThread currentPlatformThread = getCurrentPlatformThread();
MutexLocker lock(m_registeredThreadsMutex);
#ifndef NDEBUG
// Forbid malloc during the gather phase. The gather phase suspends
// threads, so a malloc during gather would risk a deadlock with a
// thread that had been suspended while holding the malloc lock.
fastMallocForbid();
#endif
for (Thread* thread = m_registeredThreads; thread; thread = thread->next) {
if (!equalThread(thread->platformThread, currentPlatformThread))
suspendThread(thread->platformThread);
}
// It is safe to access the registeredThreads list, because we earlier asserted that locks are being held,
// and since this is a shared heap, they are real locks.
for (Thread* thread = m_registeredThreads; thread; thread = thread->next) {
if (!equalThread(thread->platformThread, currentPlatformThread))
gatherFromOtherThread(conservativeRoots, thread);
}
for (Thread* thread = m_registeredThreads; thread; thread = thread->next) {
if (!equalThread(thread->platformThread, currentPlatformThread))
resumeThread(thread->platformThread);
}
#ifndef NDEBUG
fastMallocAllow();
#endif
}
}
} // namespace JSC
|