js/src/jsnativestack.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "jsnativestack.h"
michael@0 8
michael@0 9 #ifdef XP_WIN
michael@0 10 # include "jswin.h"
michael@0 11
michael@0 12 #elif defined(XP_MACOSX) || defined(DARWIN) || defined(XP_UNIX)
michael@0 13 # include <pthread.h>
michael@0 14
michael@0 15 # if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
michael@0 16 # include <pthread_np.h>
michael@0 17 # endif
michael@0 18
michael@0 19 # if defined(ANDROID)
michael@0 20 # include <sys/types.h>
michael@0 21 # include <unistd.h>
michael@0 22 # endif
michael@0 23
michael@0 24 #else
michael@0 25 # error "Unsupported platform"
michael@0 26
michael@0 27 #endif
michael@0 28
michael@0 29 #if defined(XP_WIN)
michael@0 30
michael@0 31 void *
michael@0 32 js::GetNativeStackBaseImpl()
michael@0 33 {
michael@0 34 # if defined(_M_IX86) && defined(_MSC_VER)
michael@0 35 /*
michael@0 36 * offset 0x18 from the FS segment register gives a pointer to
michael@0 37 * the thread information block for the current thread
michael@0 38 */
michael@0 39 NT_TIB* pTib;
michael@0 40 __asm {
michael@0 41 MOV EAX, FS:[18h]
michael@0 42 MOV pTib, EAX
michael@0 43 }
michael@0 44 return static_cast<void*>(pTib->StackBase);
michael@0 45
michael@0 46 # elif defined(_M_X64)
michael@0 47 PNT_TIB64 pTib = reinterpret_cast<PNT_TIB64>(NtCurrentTeb());
michael@0 48 return reinterpret_cast<void*>(pTib->StackBase);
michael@0 49
michael@0 50 # elif defined(_WIN32) && defined(__GNUC__)
michael@0 51 NT_TIB* pTib;
michael@0 52 asm ("movl %%fs:0x18, %0\n" : "=r" (pTib));
michael@0 53 return static_cast<void*>(pTib->StackBase);
michael@0 54
michael@0 55 # endif
michael@0 56 }
michael@0 57
michael@0 58 #elif defined(SOLARIS)
michael@0 59
michael@0 60 #include <ucontext.h>
michael@0 61
michael@0 62 JS_STATIC_ASSERT(JS_STACK_GROWTH_DIRECTION < 0);
michael@0 63
michael@0 64 void *
michael@0 65 js::GetNativeStackBaseImpl()
michael@0 66 {
michael@0 67 stack_t st;
michael@0 68 stack_getbounds(&st);
michael@0 69 return static_cast<char*>(st.ss_sp) + st.ss_size;
michael@0 70 }
michael@0 71
michael@0 72 #elif defined(AIX)
michael@0 73
michael@0 74 #include <ucontext.h>
michael@0 75
michael@0 76 JS_STATIC_ASSERT(JS_STACK_GROWTH_DIRECTION < 0);
michael@0 77
michael@0 78 void *
michael@0 79 js::GetNativeStackBaseImpl()
michael@0 80 {
michael@0 81 ucontext_t context;
michael@0 82 getcontext(&context);
michael@0 83 return static_cast<char*>(context.uc_stack.ss_sp) +
michael@0 84 context.uc_stack.ss_size;
michael@0 85 }
michael@0 86
michael@0 87 #else /* XP_UNIX */
michael@0 88
michael@0 89 void *
michael@0 90 js::GetNativeStackBaseImpl()
michael@0 91 {
michael@0 92 pthread_t thread = pthread_self();
michael@0 93 # if defined(XP_MACOSX) || defined(DARWIN)
michael@0 94 return pthread_get_stackaddr_np(thread);
michael@0 95
michael@0 96 # else
michael@0 97 pthread_attr_t sattr;
michael@0 98 pthread_attr_init(&sattr);
michael@0 99 # if defined(__OpenBSD__)
michael@0 100 stack_t ss;
michael@0 101 # elif defined(PTHREAD_NP_H) || defined(_PTHREAD_NP_H_) || defined(NETBSD)
michael@0 102 /* e.g. on FreeBSD 4.8 or newer, neundorf@kde.org */
michael@0 103 pthread_attr_get_np(thread, &sattr);
michael@0 104 # else
michael@0 105 /*
michael@0 106 * FIXME: this function is non-portable;
michael@0 107 * other POSIX systems may have different np alternatives
michael@0 108 */
michael@0 109 pthread_getattr_np(thread, &sattr);
michael@0 110 # endif
michael@0 111
michael@0 112 void *stackBase = 0;
michael@0 113 size_t stackSize = 0;
michael@0 114 int rc;
michael@0 115 # if defined(__OpenBSD__)
michael@0 116 rc = pthread_stackseg_np(pthread_self(), &ss);
michael@0 117 stackBase = (void*)((size_t) ss.ss_sp - ss.ss_size);
michael@0 118 stackSize = ss.ss_size;
michael@0 119 # elif defined(ANDROID)
michael@0 120 if (gettid() == getpid()) {
michael@0 121 // bionic's pthread_attr_getstack doesn't tell the truth for the main
michael@0 122 // thread (see bug 846670). So we scan /proc/self/maps to find the
michael@0 123 // segment which contains the stack.
michael@0 124 rc = -1;
michael@0 125 FILE *fs = fopen("/proc/self/maps", "r");
michael@0 126 if (fs) {
michael@0 127 char line[100];
michael@0 128 unsigned long stackAddr = (unsigned long)&sattr;
michael@0 129 while (fgets(line, sizeof(line), fs) != nullptr) {
michael@0 130 unsigned long stackStart;
michael@0 131 unsigned long stackEnd;
michael@0 132 if (sscanf(line, "%lx-%lx ", &stackStart, &stackEnd) == 2 &&
michael@0 133 stackAddr >= stackStart && stackAddr < stackEnd) {
michael@0 134 stackBase = (void *)stackStart;
michael@0 135 stackSize = stackEnd - stackStart;
michael@0 136 rc = 0;
michael@0 137 break;
michael@0 138 }
michael@0 139 }
michael@0 140 fclose(fs);
michael@0 141 }
michael@0 142 } else
michael@0 143 // For non main-threads pthread allocates the stack itself so it tells
michael@0 144 // the truth.
michael@0 145 rc = pthread_attr_getstack(&sattr, &stackBase, &stackSize);
michael@0 146 # else
michael@0 147 rc = pthread_attr_getstack(&sattr, &stackBase, &stackSize);
michael@0 148 # endif
michael@0 149 if (rc)
michael@0 150 MOZ_CRASH();
michael@0 151 JS_ASSERT(stackBase);
michael@0 152 pthread_attr_destroy(&sattr);
michael@0 153
michael@0 154 # if JS_STACK_GROWTH_DIRECTION > 0
michael@0 155 return stackBase;
michael@0 156 # else
michael@0 157 return static_cast<char*>(stackBase) + stackSize;
michael@0 158 # endif
michael@0 159 # endif
michael@0 160 }
michael@0 161
michael@0 162 #endif /* !XP_WIN */

mercurial