michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "jsnativestack.h" michael@0: michael@0: #ifdef XP_WIN michael@0: # include "jswin.h" michael@0: michael@0: #elif defined(XP_MACOSX) || defined(DARWIN) || defined(XP_UNIX) michael@0: # include michael@0: michael@0: # if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) michael@0: # include michael@0: # endif michael@0: michael@0: # if defined(ANDROID) michael@0: # include michael@0: # include michael@0: # endif michael@0: michael@0: #else michael@0: # error "Unsupported platform" michael@0: michael@0: #endif michael@0: michael@0: #if defined(XP_WIN) michael@0: michael@0: void * michael@0: js::GetNativeStackBaseImpl() michael@0: { michael@0: # if defined(_M_IX86) && defined(_MSC_VER) michael@0: /* michael@0: * offset 0x18 from the FS segment register gives a pointer to michael@0: * the thread information block for the current thread michael@0: */ michael@0: NT_TIB* pTib; michael@0: __asm { michael@0: MOV EAX, FS:[18h] michael@0: MOV pTib, EAX michael@0: } michael@0: return static_cast(pTib->StackBase); michael@0: michael@0: # elif defined(_M_X64) michael@0: PNT_TIB64 pTib = reinterpret_cast(NtCurrentTeb()); michael@0: return reinterpret_cast(pTib->StackBase); michael@0: michael@0: # elif defined(_WIN32) && defined(__GNUC__) michael@0: NT_TIB* pTib; michael@0: asm ("movl %%fs:0x18, %0\n" : "=r" (pTib)); michael@0: return static_cast(pTib->StackBase); michael@0: michael@0: # endif michael@0: } michael@0: michael@0: #elif defined(SOLARIS) michael@0: michael@0: #include michael@0: michael@0: JS_STATIC_ASSERT(JS_STACK_GROWTH_DIRECTION < 0); michael@0: michael@0: void * michael@0: js::GetNativeStackBaseImpl() michael@0: { michael@0: stack_t st; michael@0: stack_getbounds(&st); michael@0: return static_cast(st.ss_sp) + st.ss_size; michael@0: } michael@0: michael@0: #elif defined(AIX) michael@0: michael@0: #include michael@0: michael@0: JS_STATIC_ASSERT(JS_STACK_GROWTH_DIRECTION < 0); michael@0: michael@0: void * michael@0: js::GetNativeStackBaseImpl() michael@0: { michael@0: ucontext_t context; michael@0: getcontext(&context); michael@0: return static_cast(context.uc_stack.ss_sp) + michael@0: context.uc_stack.ss_size; michael@0: } michael@0: michael@0: #else /* XP_UNIX */ michael@0: michael@0: void * michael@0: js::GetNativeStackBaseImpl() michael@0: { michael@0: pthread_t thread = pthread_self(); michael@0: # if defined(XP_MACOSX) || defined(DARWIN) michael@0: return pthread_get_stackaddr_np(thread); michael@0: michael@0: # else michael@0: pthread_attr_t sattr; michael@0: pthread_attr_init(&sattr); michael@0: # if defined(__OpenBSD__) michael@0: stack_t ss; michael@0: # elif defined(PTHREAD_NP_H) || defined(_PTHREAD_NP_H_) || defined(NETBSD) michael@0: /* e.g. on FreeBSD 4.8 or newer, neundorf@kde.org */ michael@0: pthread_attr_get_np(thread, &sattr); michael@0: # else michael@0: /* michael@0: * FIXME: this function is non-portable; michael@0: * other POSIX systems may have different np alternatives michael@0: */ michael@0: pthread_getattr_np(thread, &sattr); michael@0: # endif michael@0: michael@0: void *stackBase = 0; michael@0: size_t stackSize = 0; michael@0: int rc; michael@0: # if defined(__OpenBSD__) michael@0: rc = pthread_stackseg_np(pthread_self(), &ss); michael@0: stackBase = (void*)((size_t) ss.ss_sp - ss.ss_size); michael@0: stackSize = ss.ss_size; michael@0: # elif defined(ANDROID) michael@0: if (gettid() == getpid()) { michael@0: // bionic's pthread_attr_getstack doesn't tell the truth for the main michael@0: // thread (see bug 846670). So we scan /proc/self/maps to find the michael@0: // segment which contains the stack. michael@0: rc = -1; michael@0: FILE *fs = fopen("/proc/self/maps", "r"); michael@0: if (fs) { michael@0: char line[100]; michael@0: unsigned long stackAddr = (unsigned long)&sattr; michael@0: while (fgets(line, sizeof(line), fs) != nullptr) { michael@0: unsigned long stackStart; michael@0: unsigned long stackEnd; michael@0: if (sscanf(line, "%lx-%lx ", &stackStart, &stackEnd) == 2 && michael@0: stackAddr >= stackStart && stackAddr < stackEnd) { michael@0: stackBase = (void *)stackStart; michael@0: stackSize = stackEnd - stackStart; michael@0: rc = 0; michael@0: break; michael@0: } michael@0: } michael@0: fclose(fs); michael@0: } michael@0: } else michael@0: // For non main-threads pthread allocates the stack itself so it tells michael@0: // the truth. michael@0: rc = pthread_attr_getstack(&sattr, &stackBase, &stackSize); michael@0: # else michael@0: rc = pthread_attr_getstack(&sattr, &stackBase, &stackSize); michael@0: # endif michael@0: if (rc) michael@0: MOZ_CRASH(); michael@0: JS_ASSERT(stackBase); michael@0: pthread_attr_destroy(&sattr); michael@0: michael@0: # if JS_STACK_GROWTH_DIRECTION > 0 michael@0: return stackBase; michael@0: # else michael@0: return static_cast(stackBase) + stackSize; michael@0: # endif michael@0: # endif michael@0: } michael@0: michael@0: #endif /* !XP_WIN */