MachineStackMarker.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
  3. * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
  4. * Copyright (C) 2009 Acision BV. All rights reserved.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. */
  21. #include "config.h"
  22. #include "MachineStackMarker.h"
  23. #include "ConservativeRoots.h"
  24. #include "Heap.h"
  25. #include "JSArray.h"
  26. #include "VM.h"
  27. #include <setjmp.h>
  28. #include <stdlib.h>
  29. #include <wtf/StdLibExtras.h>
  30. #if OS(DARWIN)
  31. #include <mach/mach_init.h>
  32. #include <mach/mach_port.h>
  33. #include <mach/task.h>
  34. #include <mach/thread_act.h>
  35. #include <mach/vm_map.h>
  36. #elif OS(WINDOWS)
  37. #include <windows.h>
  38. #include <malloc.h>
  39. #elif OS(UNIX)
  40. #include <sys/mman.h>
  41. #include <unistd.h>
  42. #if OS(SOLARIS)
  43. #include <thread.h>
  44. #else
  45. #include <pthread.h>
  46. #endif
  47. #if HAVE(PTHREAD_NP_H)
  48. #include <pthread_np.h>
  49. #endif
  50. #if OS(QNX)
  51. #include <fcntl.h>
  52. #include <sys/procfs.h>
  53. #include <stdio.h>
  54. #include <errno.h>
  55. #endif
  56. #if USE(PTHREADS) && !OS(WINDOWS) && !OS(DARWIN)
  57. #include <signal.h>
  58. #endif
  59. #endif
  60. #if OS(PSP2)
  61. #include <signal.h>
  62. #endif
  63. using namespace WTF;
  64. namespace JSC {
  65. static inline void swapIfBackwards(void*& begin, void*& end)
  66. {
  67. #if OS(WINCE)
  68. if (begin <= end)
  69. return;
  70. std::swap(begin, end);
  71. #else
  72. UNUSED_PARAM(begin);
  73. UNUSED_PARAM(end);
  74. #endif
  75. }
  76. #if OS(DARWIN)
  77. typedef mach_port_t PlatformThread;
  78. #elif OS(WINDOWS)
  79. typedef HANDLE PlatformThread;
  80. #elif USE(PTHREADS)
  81. typedef pthread_t PlatformThread;
  82. static const int SigThreadSuspendResume = SIGUSR2;
  83. #if defined(SA_RESTART)
  84. static void pthreadSignalHandlerSuspendResume(int)
  85. {
  86. sigset_t signalSet;
  87. sigemptyset(&signalSet);
  88. sigaddset(&signalSet, SigThreadSuspendResume);
  89. sigsuspend(&signalSet);
  90. }
  91. #endif
  92. #endif
  93. class MachineThreads::Thread {
  94. WTF_MAKE_FAST_ALLOCATED;
  95. public:
  96. Thread(const PlatformThread& platThread, void* base)
  97. : platformThread(platThread)
  98. , stackBase(base)
  99. {
  100. #if USE(PTHREADS) && !OS(WINDOWS) && !OS(DARWIN) && defined(SA_RESTART)
  101. // if we have SA_RESTART, enable SIGUSR2 debugging mechanism
  102. struct sigaction action;
  103. action.sa_handler = pthreadSignalHandlerSuspendResume;
  104. sigemptyset(&action.sa_mask);
  105. action.sa_flags = SA_RESTART;
  106. sigaction(SigThreadSuspendResume, &action, 0);
  107. sigset_t mask;
  108. sigemptyset(&mask);
  109. sigaddset(&mask, SigThreadSuspendResume);
  110. pthread_sigmask(SIG_UNBLOCK, &mask, 0);
  111. #endif
  112. }
  113. Thread* next;
  114. PlatformThread platformThread;
  115. void* stackBase;
  116. };
  117. MachineThreads::MachineThreads(Heap* heap)
  118. : m_registeredThreads(0)
  119. , m_threadSpecific(0)
  120. #if !ASSERT_DISABLED
  121. , m_heap(heap)
  122. #endif
  123. {
  124. UNUSED_PARAM(heap);
  125. }
  126. MachineThreads::~MachineThreads()
  127. {
  128. if (m_threadSpecific)
  129. threadSpecificKeyDelete(m_threadSpecific);
  130. MutexLocker registeredThreadsLock(m_registeredThreadsMutex);
  131. for (Thread* t = m_registeredThreads; t;) {
  132. Thread* next = t->next;
  133. delete t;
  134. t = next;
  135. }
  136. }
  137. static inline PlatformThread getCurrentPlatformThread()
  138. {
  139. #if OS(DARWIN)
  140. return pthread_mach_thread_np(pthread_self());
  141. #elif OS(WINDOWS)
  142. return GetCurrentThread();
  143. #elif USE(PTHREADS)
  144. return pthread_self();
  145. #endif
  146. }
  147. static inline bool equalThread(const PlatformThread& first, const PlatformThread& second)
  148. {
  149. #if OS(DARWIN) || OS(WINDOWS)
  150. return first == second;
  151. #elif USE(PTHREADS)
  152. return !!pthread_equal(first, second);
  153. #else
  154. #error Need a way to compare threads on this platform
  155. #endif
  156. }
  157. void MachineThreads::makeUsableFromMultipleThreads()
  158. {
  159. if (m_threadSpecific)
  160. return;
  161. threadSpecificKeyCreate(&m_threadSpecific, removeThread);
  162. }
  163. void MachineThreads::addCurrentThread()
  164. {
  165. ASSERT(!m_heap->vm()->exclusiveThread || m_heap->vm()->exclusiveThread == currentThread());
  166. if (!m_threadSpecific || threadSpecificGet(m_threadSpecific))
  167. return;
  168. threadSpecificSet(m_threadSpecific, this);
  169. Thread* thread = new Thread(getCurrentPlatformThread(), wtfThreadData().stack().origin());
  170. MutexLocker lock(m_registeredThreadsMutex);
  171. thread->next = m_registeredThreads;
  172. m_registeredThreads = thread;
  173. }
  174. void MachineThreads::removeThread(void* p)
  175. {
  176. if (p)
  177. static_cast<MachineThreads*>(p)->removeCurrentThread();
  178. }
  179. void MachineThreads::removeCurrentThread()
  180. {
  181. PlatformThread currentPlatformThread = getCurrentPlatformThread();
  182. MutexLocker lock(m_registeredThreadsMutex);
  183. if (equalThread(currentPlatformThread, m_registeredThreads->platformThread)) {
  184. Thread* t = m_registeredThreads;
  185. m_registeredThreads = m_registeredThreads->next;
  186. delete t;
  187. } else {
  188. Thread* last = m_registeredThreads;
  189. Thread* t;
  190. for (t = m_registeredThreads->next; t; t = t->next) {
  191. if (equalThread(t->platformThread, currentPlatformThread)) {
  192. last->next = t->next;
  193. break;
  194. }
  195. last = t;
  196. }
  197. ASSERT(t); // If t is NULL, we never found ourselves in the list.
  198. delete t;
  199. }
  200. }
  201. #if COMPILER(GCC)
  202. #define REGISTER_BUFFER_ALIGNMENT __attribute__ ((aligned (sizeof(void*))))
  203. #else
  204. #define REGISTER_BUFFER_ALIGNMENT
  205. #endif
  206. void MachineThreads::gatherFromCurrentThread(ConservativeRoots& conservativeRoots, void* stackCurrent)
  207. {
  208. // setjmp forces volatile registers onto the stack
  209. jmp_buf registers REGISTER_BUFFER_ALIGNMENT;
  210. #if COMPILER(MSVC)
  211. #pragma warning(push)
  212. #pragma warning(disable: 4611)
  213. #endif
  214. setjmp(registers);
  215. #if COMPILER(MSVC)
  216. #pragma warning(pop)
  217. #endif
  218. void* registersBegin = &registers;
  219. void* registersEnd = reinterpret_cast<void*>(roundUpToMultipleOf<sizeof(void*)>(reinterpret_cast<uintptr_t>(&registers + 1)));
  220. swapIfBackwards(registersBegin, registersEnd);
  221. conservativeRoots.add(registersBegin, registersEnd);
  222. void* stackBegin = stackCurrent;
  223. void* stackEnd = wtfThreadData().stack().origin();
  224. swapIfBackwards(stackBegin, stackEnd);
  225. conservativeRoots.add(stackBegin, stackEnd);
  226. }
  227. static inline void suspendThread(const PlatformThread& platformThread)
  228. {
  229. #if OS(DARWIN)
  230. thread_suspend(platformThread);
  231. #elif OS(WINDOWS)
  232. SuspendThread(platformThread);
  233. #elif USE(PTHREADS)
  234. pthread_kill(platformThread, SigThreadSuspendResume);
  235. #else
  236. #error Need a way to suspend threads on this platform
  237. #endif
  238. }
  239. static inline void resumeThread(const PlatformThread& platformThread)
  240. {
  241. #if OS(DARWIN)
  242. thread_resume(platformThread);
  243. #elif OS(WINDOWS)
  244. ResumeThread(platformThread);
  245. #elif USE(PTHREADS)
  246. pthread_kill(platformThread, SigThreadSuspendResume);
  247. #else
  248. #error Need a way to resume threads on this platform
  249. #endif
  250. }
  251. typedef unsigned long usword_t; // word size, assumed to be either 32 or 64 bit
  252. #if OS(DARWIN)
  253. #if CPU(X86)
  254. typedef i386_thread_state_t PlatformThreadRegisters;
  255. #elif CPU(X86_64)
  256. typedef x86_thread_state64_t PlatformThreadRegisters;
  257. #elif CPU(PPC)
  258. typedef ppc_thread_state_t PlatformThreadRegisters;
  259. #elif CPU(PPC64)
  260. typedef ppc_thread_state64_t PlatformThreadRegisters;
  261. #elif CPU(ARM)
  262. typedef arm_thread_state_t PlatformThreadRegisters;
  263. #else
  264. #error Unknown Architecture
  265. #endif
  266. #elif OS(WINDOWS)
  267. typedef CONTEXT PlatformThreadRegisters;
  268. #elif OS(QNX)
  269. typedef struct _debug_thread_info PlatformThreadRegisters;
  270. #elif USE(PTHREADS)
  271. typedef pthread_attr_t PlatformThreadRegisters;
  272. #else
  273. #error Need a thread register struct for this platform
  274. #endif
  275. static size_t getPlatformThreadRegisters(const PlatformThread& platformThread, PlatformThreadRegisters& regs)
  276. {
  277. #if OS(DARWIN)
  278. #if CPU(X86)
  279. unsigned user_count = sizeof(regs)/sizeof(int);
  280. thread_state_flavor_t flavor = i386_THREAD_STATE;
  281. #elif CPU(X86_64)
  282. unsigned user_count = x86_THREAD_STATE64_COUNT;
  283. thread_state_flavor_t flavor = x86_THREAD_STATE64;
  284. #elif CPU(PPC)
  285. unsigned user_count = PPC_THREAD_STATE_COUNT;
  286. thread_state_flavor_t flavor = PPC_THREAD_STATE;
  287. #elif CPU(PPC64)
  288. unsigned user_count = PPC_THREAD_STATE64_COUNT;
  289. thread_state_flavor_t flavor = PPC_THREAD_STATE64;
  290. #elif CPU(ARM)
  291. unsigned user_count = ARM_THREAD_STATE_COUNT;
  292. thread_state_flavor_t flavor = ARM_THREAD_STATE;
  293. #else
  294. #error Unknown Architecture
  295. #endif
  296. kern_return_t result = thread_get_state(platformThread, flavor, (thread_state_t)&regs, &user_count);
  297. if (result != KERN_SUCCESS) {
  298. WTFReportFatalError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION,
  299. "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);
  300. CRASH();
  301. }
  302. return user_count * sizeof(usword_t);
  303. // end OS(DARWIN)
  304. #elif OS(WINDOWS)
  305. regs.ContextFlags = CONTEXT_INTEGER | CONTEXT_CONTROL;
  306. GetThreadContext(platformThread, &regs);
  307. return sizeof(CONTEXT);
  308. #elif OS(QNX)
  309. memset(&regs, 0, sizeof(regs));
  310. regs.tid = platformThread;
  311. // FIXME: If we find this hurts performance, we can consider caching the fd and keeping it open.
  312. int fd = open("/proc/self/as", O_RDONLY);
  313. if (fd == -1) {
  314. LOG_ERROR("Unable to open /proc/self/as (errno: %d)", errno);
  315. CRASH();
  316. }
  317. int rc = devctl(fd, DCMD_PROC_TIDSTATUS, &regs, sizeof(regs), 0);
  318. if (rc != EOK) {
  319. LOG_ERROR("devctl(DCMD_PROC_TIDSTATUS) failed (error: %d)", rc);
  320. CRASH();
  321. }
  322. close(fd);
  323. return sizeof(struct _debug_thread_info);
  324. #elif USE(PTHREADS)
  325. pthread_attr_init(&regs);
  326. #if HAVE(PTHREAD_NP_H) || OS(NETBSD)
  327. // e.g. on FreeBSD 5.4, neundorf@kde.org
  328. pthread_attr_get_np(platformThread, &regs);
  329. #else
  330. // FIXME: this function is non-portable; other POSIX systems may have different np alternatives
  331. pthread_getattr_np(platformThread, &regs);
  332. #endif
  333. return 0;
  334. #else
  335. #error Need a way to get thread registers on this platform
  336. #endif
  337. }
  338. static inline void* otherThreadStackPointer(const PlatformThreadRegisters& regs)
  339. {
  340. #if OS(DARWIN)
  341. #if __DARWIN_UNIX03
  342. #if CPU(X86)
  343. return reinterpret_cast<void*>(regs.__esp);
  344. #elif CPU(X86_64)
  345. return reinterpret_cast<void*>(regs.__rsp);
  346. #elif CPU(PPC) || CPU(PPC64)
  347. return reinterpret_cast<void*>(regs.__r1);
  348. #elif CPU(ARM)
  349. return reinterpret_cast<void*>(regs.__sp);
  350. #else
  351. #error Unknown Architecture
  352. #endif
  353. #else // !__DARWIN_UNIX03
  354. #if CPU(X86)
  355. return reinterpret_cast<void*>(regs.esp);
  356. #elif CPU(X86_64)
  357. return reinterpret_cast<void*>(regs.rsp);
  358. #elif CPU(PPC) || CPU(PPC64)
  359. return reinterpret_cast<void*>(regs.r1);
  360. #else
  361. #error Unknown Architecture
  362. #endif
  363. #endif // __DARWIN_UNIX03
  364. // end OS(DARWIN)
  365. #elif OS(WINDOWS)
  366. #if CPU(ARM)
  367. return reinterpret_cast<void*>((uintptr_t) regs.Sp);
  368. #elif CPU(MIPS)
  369. return reinterpret_cast<void*>((uintptr_t) regs.IntSp);
  370. #elif CPU(X86)
  371. return reinterpret_cast<void*>((uintptr_t) regs.Esp);
  372. #elif CPU(X86_64)
  373. return reinterpret_cast<void*>((uintptr_t) regs.Rsp);
  374. #else
  375. #error Unknown Architecture
  376. #endif
  377. #elif OS(QNX)
  378. return reinterpret_cast<void*>((uintptr_t) regs.sp);
  379. #elif USE(PTHREADS)
  380. void* stackBase = 0;
  381. size_t stackSize = 0;
  382. int rc = pthread_attr_getstack(&regs, &stackBase, &stackSize);
  383. (void)rc; // FIXME: Deal with error code somehow? Seems fatal.
  384. ASSERT(stackBase);
  385. return static_cast<char*>(stackBase) + stackSize;
  386. #else
  387. #error Need a way to get the stack pointer for another thread on this platform
  388. #endif
  389. }
  390. static void freePlatformThreadRegisters(PlatformThreadRegisters& regs)
  391. {
  392. #if USE(PTHREADS) && !OS(WINDOWS) && !OS(DARWIN) && !OS(QNX)
  393. pthread_attr_destroy(&regs);
  394. #else
  395. UNUSED_PARAM(regs);
  396. #endif
  397. }
  398. void MachineThreads::gatherFromOtherThread(ConservativeRoots& conservativeRoots, Thread* thread)
  399. {
  400. PlatformThreadRegisters regs;
  401. size_t regSize = getPlatformThreadRegisters(thread->platformThread, regs);
  402. conservativeRoots.add(static_cast<void*>(&regs), static_cast<void*>(reinterpret_cast<char*>(&regs) + regSize));
  403. void* stackPointer = otherThreadStackPointer(regs);
  404. void* stackBase = thread->stackBase;
  405. swapIfBackwards(stackPointer, stackBase);
  406. conservativeRoots.add(stackPointer, stackBase);
  407. freePlatformThreadRegisters(regs);
  408. }
  409. void MachineThreads::gatherConservativeRoots(ConservativeRoots& conservativeRoots, void* stackCurrent)
  410. {
  411. gatherFromCurrentThread(conservativeRoots, stackCurrent);
  412. if (m_threadSpecific) {
  413. PlatformThread currentPlatformThread = getCurrentPlatformThread();
  414. MutexLocker lock(m_registeredThreadsMutex);
  415. #ifndef NDEBUG
  416. // Forbid malloc during the gather phase. The gather phase suspends
  417. // threads, so a malloc during gather would risk a deadlock with a
  418. // thread that had been suspended while holding the malloc lock.
  419. fastMallocForbid();
  420. #endif
  421. for (Thread* thread = m_registeredThreads; thread; thread = thread->next) {
  422. if (!equalThread(thread->platformThread, currentPlatformThread))
  423. suspendThread(thread->platformThread);
  424. }
  425. // It is safe to access the registeredThreads list, because we earlier asserted that locks are being held,
  426. // and since this is a shared heap, they are real locks.
  427. for (Thread* thread = m_registeredThreads; thread; thread = thread->next) {
  428. if (!equalThread(thread->platformThread, currentPlatformThread))
  429. gatherFromOtherThread(conservativeRoots, thread);
  430. }
  431. for (Thread* thread = m_registeredThreads; thread; thread = thread->next) {
  432. if (!equalThread(thread->platformThread, currentPlatformThread))
  433. resumeThread(thread->platformThread);
  434. }
  435. #ifndef NDEBUG
  436. fastMallocAllow();
  437. #endif
  438. }
  439. }
  440. } // namespace JSC