2018-01-19 03:59:58 +08:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
|
|
#include "jsnativestack.h"
|
|
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
|
# include "jswin.h"
|
|
|
|
|
|
2022-04-26 11:34:08 -05:00
|
|
|
#elif defined(XP_DARWIN) || defined(DARWIN) || defined(XP_UNIX)
|
2018-01-19 03:59:58 +08:00
|
|
|
# include <pthread.h>
|
|
|
|
|
|
|
|
|
|
# if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
|
|
|
|
|
# include <pthread_np.h>
|
|
|
|
|
# endif
|
|
|
|
|
|
|
|
|
|
# if defined(ANDROID)
|
|
|
|
|
# include <sys/types.h>
|
|
|
|
|
# include <unistd.h>
|
|
|
|
|
# endif
|
|
|
|
|
|
Issue #578: Applications cannot start without /proc (chroot).
UXP uses the current stack frame address and the stack size
as a sort of heuristic for various things in the JavaScript
engine. The js::GetNativeStackBaseImpl() function is used to
get the base stack address (i.e. the address from which the stack
grows, so this can be either the first or last memory address of
the stack memory space depending on the CPU architecture).
On Linux, this function is implemented using the pthreads APIs.
For non-main threads, the queried thread info is stored in
memory. The main thread does not have this information on hand,
so it gets the stack memory range via the /proc/self/maps file
(see glibc's pthread_get_attr_np.c).
Fortunately (per discussions with the firefox devs in #jsapi)
the base address only needs to be approximate. In reality,
environment variables, args, and other things are stored in
stack space between the end/beginning of the mapped stack
memory and the 'top' of the stack space used by stack frames.
When using glibc, we can get the top of this usable stack from
__libc_stack_end, which is a void* set by glibc during program
initialization, avoiding the need to access /proc.
Non-main threads still get their stack-base through the usual
pthreads APIs.
Other libc implementations like musl will fall back to the
standard UNIX-like implementation which calls pthread's
pthread_attr_getstack() also from the main thread, which
may imply /proc access and not work in restricted
environments.
2018-07-02 09:46:06 +02:00
|
|
|
# if defined(XP_LINUX) && !defined(ANDROID) && defined(__GLIBC__)
|
|
|
|
|
# include <dlfcn.h>
|
|
|
|
|
# include <sys/syscall.h>
|
|
|
|
|
# include <sys/types.h>
|
|
|
|
|
# include <unistd.h>
|
2019-10-16 12:37:07 -04:00
|
|
|
# define gettid() static_cast<pid_t>(syscall(SYS_gettid))
|
Issue #578: Applications cannot start without /proc (chroot).
UXP uses the current stack frame address and the stack size
as a sort of heuristic for various things in the JavaScript
engine. The js::GetNativeStackBaseImpl() function is used to
get the base stack address (i.e. the address from which the stack
grows, so this can be either the first or last memory address of
the stack memory space depending on the CPU architecture).
On Linux, this function is implemented using the pthreads APIs.
For non-main threads, the queried thread info is stored in
memory. The main thread does not have this information on hand,
so it gets the stack memory range via the /proc/self/maps file
(see glibc's pthread_get_attr_np.c).
Fortunately (per discussions with the firefox devs in #jsapi)
the base address only needs to be approximate. In reality,
environment variables, args, and other things are stored in
stack space between the end/beginning of the mapped stack
memory and the 'top' of the stack space used by stack frames.
When using glibc, we can get the top of this usable stack from
__libc_stack_end, which is a void* set by glibc during program
initialization, avoiding the need to access /proc.
Non-main threads still get their stack-base through the usual
pthreads APIs.
Other libc implementations like musl will fall back to the
standard UNIX-like implementation which calls pthread's
pthread_attr_getstack() also from the main thread, which
may imply /proc access and not work in restricted
environments.
2018-07-02 09:46:06 +02:00
|
|
|
# endif
|
|
|
|
|
|
2018-01-19 03:59:58 +08:00
|
|
|
#else
|
|
|
|
|
# error "Unsupported platform"
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(XP_WIN)
|
|
|
|
|
|
|
|
|
|
void*
|
|
|
|
|
js::GetNativeStackBaseImpl()
|
|
|
|
|
{
|
|
|
|
|
# if defined(_M_IX86) && defined(_MSC_VER)
|
|
|
|
|
/*
|
|
|
|
|
* offset 0x18 from the FS segment register gives a pointer to
|
|
|
|
|
* the thread information block for the current thread
|
|
|
|
|
*/
|
|
|
|
|
NT_TIB* pTib;
|
|
|
|
|
__asm {
|
|
|
|
|
MOV EAX, FS:[18h]
|
|
|
|
|
MOV pTib, EAX
|
|
|
|
|
}
|
|
|
|
|
return static_cast<void*>(pTib->StackBase);
|
|
|
|
|
|
|
|
|
|
# elif defined(_M_X64)
|
|
|
|
|
PNT_TIB64 pTib = reinterpret_cast<PNT_TIB64>(NtCurrentTeb());
|
|
|
|
|
return reinterpret_cast<void*>(pTib->StackBase);
|
|
|
|
|
|
|
|
|
|
# elif defined(_M_ARM)
|
|
|
|
|
PNT_TIB pTib = reinterpret_cast<PNT_TIB>(NtCurrentTeb());
|
|
|
|
|
return static_cast<void*>(pTib->StackBase);
|
|
|
|
|
|
|
|
|
|
# elif defined(_WIN32) && defined(__GNUC__)
|
|
|
|
|
NT_TIB* pTib;
|
|
|
|
|
asm ("movl %%fs:0x18, %0\n" : "=r" (pTib));
|
|
|
|
|
return static_cast<void*>(pTib->StackBase);
|
|
|
|
|
|
|
|
|
|
# endif
|
|
|
|
|
}
|
|
|
|
|
|
MoonchildProductions#1251 - Part 1: Restore initial Solaris support, fixed up.
Compared with what Pale Moon had for Solaris originally, this is mostly the same zero point I started patching from, but I've made the following changes here after reviewing all this initial code I never looked at closely before.
1. In package-manifest.in for both Basilisk and Pale Moon, I've made the SPARC code for libfreebl not interefere with the x86 code, use the proper build flags, and also updated it to allow a SPARC64 build which is more likely to be used than the 32-bit SPARC code we had there.
2. See Mozilla bug #832272 and the old rules.mk patch from around Firefox 30 in oracle/solaris-userland. I believe they screwed up NSINSTALL on Solaris when they were trying to streamline the NSS buildsystem, because they started having unexplained issues with it around that time after Firefox 22 that they never properly resolved until Mozilla began building NSS with gyp files. I'm actually not even sure how relevant the thing they broke actually is to Solaris at this point, bug 665509 is so old it predates Firefox itself and goes back to the Mozilla suite days. I believe $(INSTALL) -t was wrong, and they meant $(NSINSTALL) -t because that makes more sense and is closer to what was there originally. It's what they have for WINNT, and it's possible a fix more like that could serve for Solaris as well. Alternatively, we could get rid of all these half-broken Makefiles and start building NSS with gyp files like Mozilla did.
3. I've completely cut out support for the Sun compiler and taken into account the reality that everyone builds Firefox (and therefore its forks) with GCC now on Solaris. This alone helped clean up a lot of the uglier parts of the code.
4. I've updated all remaining SOLARIS build flags to the newer XP_SOLARIS, because the SOLARIS flag is no longer set when building Solaris.
5. I've confirmed the workaround in gtxFontconfigFonts.cpp is no longer necessary. The Solaris people got impatient about implementing a half-baked patch for a fontconfig feature that wasn't ready yet back in 2009, and somehow convinced Mozilla to patch their software to work around it when really they should have just fixed or removed their broken fontconfig patch. The feature they wanted has since been implemented properly, and no version of Solaris still uses the broken patch that required this fix. If anyone had ever properly audited this code, it would have been removed a long time ago.
2019-10-01 06:07:31 -05:00
|
|
|
#elif defined(XP_SOLARIS)
|
|
|
|
|
|
|
|
|
|
#include <ucontext.h>
|
|
|
|
|
|
|
|
|
|
JS_STATIC_ASSERT(JS_STACK_GROWTH_DIRECTION < 0);
|
|
|
|
|
|
2019-10-04 04:37:51 -05:00
|
|
|
void*
|
|
|
|
|
js::GetNativeStackBaseImpl()
|
MoonchildProductions#1251 - Part 1: Restore initial Solaris support, fixed up.
Compared with what Pale Moon had for Solaris originally, this is mostly the same zero point I started patching from, but I've made the following changes here after reviewing all this initial code I never looked at closely before.
1. In package-manifest.in for both Basilisk and Pale Moon, I've made the SPARC code for libfreebl not interefere with the x86 code, use the proper build flags, and also updated it to allow a SPARC64 build which is more likely to be used than the 32-bit SPARC code we had there.
2. See Mozilla bug #832272 and the old rules.mk patch from around Firefox 30 in oracle/solaris-userland. I believe they screwed up NSINSTALL on Solaris when they were trying to streamline the NSS buildsystem, because they started having unexplained issues with it around that time after Firefox 22 that they never properly resolved until Mozilla began building NSS with gyp files. I'm actually not even sure how relevant the thing they broke actually is to Solaris at this point, bug 665509 is so old it predates Firefox itself and goes back to the Mozilla suite days. I believe $(INSTALL) -t was wrong, and they meant $(NSINSTALL) -t because that makes more sense and is closer to what was there originally. It's what they have for WINNT, and it's possible a fix more like that could serve for Solaris as well. Alternatively, we could get rid of all these half-broken Makefiles and start building NSS with gyp files like Mozilla did.
3. I've completely cut out support for the Sun compiler and taken into account the reality that everyone builds Firefox (and therefore its forks) with GCC now on Solaris. This alone helped clean up a lot of the uglier parts of the code.
4. I've updated all remaining SOLARIS build flags to the newer XP_SOLARIS, because the SOLARIS flag is no longer set when building Solaris.
5. I've confirmed the workaround in gtxFontconfigFonts.cpp is no longer necessary. The Solaris people got impatient about implementing a half-baked patch for a fontconfig feature that wasn't ready yet back in 2009, and somehow convinced Mozilla to patch their software to work around it when really they should have just fixed or removed their broken fontconfig patch. The feature they wanted has since been implemented properly, and no version of Solaris still uses the broken patch that required this fix. If anyone had ever properly audited this code, it would have been removed a long time ago.
2019-10-01 06:07:31 -05:00
|
|
|
{
|
|
|
|
|
stack_t st;
|
|
|
|
|
stack_getbounds(&st);
|
|
|
|
|
return static_cast<char*>(st.ss_sp) + st.ss_size;
|
|
|
|
|
}
|
|
|
|
|
|
Issue #578: Applications cannot start without /proc (chroot).
UXP uses the current stack frame address and the stack size
as a sort of heuristic for various things in the JavaScript
engine. The js::GetNativeStackBaseImpl() function is used to
get the base stack address (i.e. the address from which the stack
grows, so this can be either the first or last memory address of
the stack memory space depending on the CPU architecture).
On Linux, this function is implemented using the pthreads APIs.
For non-main threads, the queried thread info is stored in
memory. The main thread does not have this information on hand,
so it gets the stack memory range via the /proc/self/maps file
(see glibc's pthread_get_attr_np.c).
Fortunately (per discussions with the firefox devs in #jsapi)
the base address only needs to be approximate. In reality,
environment variables, args, and other things are stored in
stack space between the end/beginning of the mapped stack
memory and the 'top' of the stack space used by stack frames.
When using glibc, we can get the top of this usable stack from
__libc_stack_end, which is a void* set by glibc during program
initialization, avoiding the need to access /proc.
Non-main threads still get their stack-base through the usual
pthreads APIs.
Other libc implementations like musl will fall back to the
standard UNIX-like implementation which calls pthread's
pthread_attr_getstack() also from the main thread, which
may imply /proc access and not work in restricted
environments.
2018-07-02 09:46:06 +02:00
|
|
|
#elif defined(XP_LINUX) && !defined(ANDROID) && defined(__GLIBC__)
|
|
|
|
|
void*
|
|
|
|
|
js::GetNativeStackBaseImpl()
|
|
|
|
|
{
|
|
|
|
|
// On the main thread, get stack base from glibc's __libc_stack_end rather than pthread APIs
|
|
|
|
|
// to avoid filesystem calls /proc/self/maps. Non-main threads spawned with pthreads can read
|
|
|
|
|
// this information directly from their pthread struct, but when using the pthreads API, the
|
|
|
|
|
// main thread must go parse /proc/self/maps to figure the mapped stack address space ranges.
|
|
|
|
|
// We want to avoid reading from /proc/ so that the application can run in restricted
|
|
|
|
|
// environments where /proc may not be mounted (e.g. chroot).
|
|
|
|
|
if (gettid() == getpid()) {
|
|
|
|
|
void** pLibcStackEnd = (void**)dlsym(RTLD_DEFAULT, "__libc_stack_end");
|
|
|
|
|
|
|
|
|
|
// If __libc_stack_end is not found, architecture specific frame pointer hopping will need
|
|
|
|
|
// to be implemented.
|
|
|
|
|
MOZ_RELEASE_ASSERT(pLibcStackEnd, "__libc_stack_end unavailable, unable to setup stack range for JS.");
|
|
|
|
|
void* stackBase = *pLibcStackEnd;
|
|
|
|
|
MOZ_RELEASE_ASSERT(stackBase, "Invalid stack base, unable to setup stack range for JS.");
|
|
|
|
|
|
|
|
|
|
// We don't need to fix stackBase, as it already roughly points to beginning of the stack.
|
|
|
|
|
return stackBase;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Non-main threads have the required info stored in memory, so no filesystem calls are made.
|
|
|
|
|
pthread_t thread = pthread_self();
|
|
|
|
|
pthread_attr_t sattr;
|
|
|
|
|
pthread_attr_init(&sattr);
|
2025-08-20 00:29:38 +02:00
|
|
|
int rc = pthread_getattr_np(thread, &sattr);
|
|
|
|
|
MOZ_RELEASE_ASSERT(rc == 0, "Call to pthread_getattr_np failed.");
|
Issue #578: Applications cannot start without /proc (chroot).
UXP uses the current stack frame address and the stack size
as a sort of heuristic for various things in the JavaScript
engine. The js::GetNativeStackBaseImpl() function is used to
get the base stack address (i.e. the address from which the stack
grows, so this can be either the first or last memory address of
the stack memory space depending on the CPU architecture).
On Linux, this function is implemented using the pthreads APIs.
For non-main threads, the queried thread info is stored in
memory. The main thread does not have this information on hand,
so it gets the stack memory range via the /proc/self/maps file
(see glibc's pthread_get_attr_np.c).
Fortunately (per discussions with the firefox devs in #jsapi)
the base address only needs to be approximate. In reality,
environment variables, args, and other things are stored in
stack space between the end/beginning of the mapped stack
memory and the 'top' of the stack space used by stack frames.
When using glibc, we can get the top of this usable stack from
__libc_stack_end, which is a void* set by glibc during program
initialization, avoiding the need to access /proc.
Non-main threads still get their stack-base through the usual
pthreads APIs.
Other libc implementations like musl will fall back to the
standard UNIX-like implementation which calls pthread's
pthread_attr_getstack() also from the main thread, which
may imply /proc access and not work in restricted
environments.
2018-07-02 09:46:06 +02:00
|
|
|
|
|
|
|
|
// stackBase will be the *lowest* address on all architectures.
|
|
|
|
|
void* stackBase = nullptr;
|
|
|
|
|
size_t stackSize = 0;
|
2025-08-20 00:29:38 +02:00
|
|
|
rc = pthread_attr_getstack(&sattr, &stackBase, &stackSize);
|
|
|
|
|
MOZ_RELEASE_ASSERT(rc == 0, "Call to pthread_attr_getstack failed, unable to setup stack range for JS.");
|
Issue #578: Applications cannot start without /proc (chroot).
UXP uses the current stack frame address and the stack size
as a sort of heuristic for various things in the JavaScript
engine. The js::GetNativeStackBaseImpl() function is used to
get the base stack address (i.e. the address from which the stack
grows, so this can be either the first or last memory address of
the stack memory space depending on the CPU architecture).
On Linux, this function is implemented using the pthreads APIs.
For non-main threads, the queried thread info is stored in
memory. The main thread does not have this information on hand,
so it gets the stack memory range via the /proc/self/maps file
(see glibc's pthread_get_attr_np.c).
Fortunately (per discussions with the firefox devs in #jsapi)
the base address only needs to be approximate. In reality,
environment variables, args, and other things are stored in
stack space between the end/beginning of the mapped stack
memory and the 'top' of the stack space used by stack frames.
When using glibc, we can get the top of this usable stack from
__libc_stack_end, which is a void* set by glibc during program
initialization, avoiding the need to access /proc.
Non-main threads still get their stack-base through the usual
pthreads APIs.
Other libc implementations like musl will fall back to the
standard UNIX-like implementation which calls pthread's
pthread_attr_getstack() also from the main thread, which
may imply /proc access and not work in restricted
environments.
2018-07-02 09:46:06 +02:00
|
|
|
MOZ_RELEASE_ASSERT(stackBase, "Invalid stack base, unable to setup stack range for JS.");
|
|
|
|
|
pthread_attr_destroy(&sattr);
|
|
|
|
|
|
|
|
|
|
# if JS_STACK_GROWTH_DIRECTION > 0
|
|
|
|
|
return stackBase;
|
|
|
|
|
# else
|
|
|
|
|
return static_cast<char*>(stackBase) + stackSize;
|
|
|
|
|
# endif
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-19 03:59:58 +08:00
|
|
|
#else /* XP_UNIX */
|
|
|
|
|
|
|
|
|
|
void*
|
|
|
|
|
js::GetNativeStackBaseImpl()
|
|
|
|
|
{
|
2025-08-20 00:29:38 +02:00
|
|
|
int rc;
|
2018-01-19 03:59:58 +08:00
|
|
|
pthread_t thread = pthread_self();
|
2022-04-26 11:34:08 -05:00
|
|
|
# if defined(XP_DARWIN) || defined(DARWIN)
|
|
|
|
|
return pthread_get_stackaddr_np(thread);
|
|
|
|
|
|
|
|
|
|
# else
|
2018-01-19 03:59:58 +08:00
|
|
|
pthread_attr_t sattr;
|
|
|
|
|
pthread_attr_init(&sattr);
|
|
|
|
|
# if defined(__OpenBSD__)
|
|
|
|
|
stack_t ss;
|
|
|
|
|
# elif defined(PTHREAD_NP_H) || defined(_PTHREAD_NP_H_) || defined(NETBSD)
|
|
|
|
|
/* e.g. on FreeBSD 4.8 or newer, neundorf@kde.org */
|
|
|
|
|
pthread_attr_get_np(thread, &sattr);
|
|
|
|
|
# else
|
|
|
|
|
/*
|
|
|
|
|
* FIXME: this function is non-portable;
|
|
|
|
|
* other POSIX systems may have different np alternatives
|
|
|
|
|
*/
|
2025-08-20 00:29:38 +02:00
|
|
|
rc = pthread_getattr_np(thread, &sattr);
|
|
|
|
|
MOZ_RELEASE_ASSERT(rc == 0, "Call to pthread_getattr_np failed.");
|
2018-01-19 03:59:58 +08:00
|
|
|
# endif
|
|
|
|
|
|
|
|
|
|
void* stackBase = 0;
|
|
|
|
|
size_t stackSize = 0;
|
|
|
|
|
# if defined(__OpenBSD__)
|
|
|
|
|
rc = pthread_stackseg_np(pthread_self(), &ss);
|
2025-08-20 00:29:38 +02:00
|
|
|
MOZ_RELEASE_ASSERT(rc == 0, "Call to pthread_stackseg_np failed, unable to setup stack range for JS.");
|
2018-01-19 03:59:58 +08:00
|
|
|
stackBase = (void*)((size_t) ss.ss_sp - ss.ss_size);
|
|
|
|
|
stackSize = ss.ss_size;
|
|
|
|
|
# elif defined(ANDROID)
|
|
|
|
|
if (gettid() == getpid()) {
|
|
|
|
|
// bionic's pthread_attr_getstack doesn't tell the truth for the main
|
|
|
|
|
// thread (see bug 846670). So we scan /proc/self/maps to find the
|
|
|
|
|
// segment which contains the stack.
|
|
|
|
|
rc = -1;
|
|
|
|
|
|
|
|
|
|
// Put the string on the stack, otherwise there is the danger that it
|
|
|
|
|
// has not been decompressed by the the on-demand linker. Bug 1165460.
|
|
|
|
|
//
|
|
|
|
|
// The volatile keyword should stop the compiler from trying to omit
|
|
|
|
|
// the stack copy in the future (hopefully).
|
|
|
|
|
volatile char path[] = "/proc/self/maps";
|
|
|
|
|
FILE* fs = fopen((const char*)path, "r");
|
|
|
|
|
|
|
|
|
|
if (fs) {
|
|
|
|
|
char line[100];
|
|
|
|
|
unsigned long stackAddr = (unsigned long)&sattr;
|
|
|
|
|
while (fgets(line, sizeof(line), fs) != nullptr) {
|
|
|
|
|
unsigned long stackStart;
|
|
|
|
|
unsigned long stackEnd;
|
|
|
|
|
if (sscanf(line, "%lx-%lx ", &stackStart, &stackEnd) == 2 &&
|
|
|
|
|
stackAddr >= stackStart && stackAddr < stackEnd) {
|
|
|
|
|
stackBase = (void*)stackStart;
|
|
|
|
|
stackSize = stackEnd - stackStart;
|
|
|
|
|
rc = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fclose(fs);
|
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
// For non main-threads pthread allocates the stack itself so it tells
|
|
|
|
|
// the truth.
|
|
|
|
|
rc = pthread_attr_getstack(&sattr, &stackBase, &stackSize);
|
|
|
|
|
# else
|
Issue #578: Applications cannot start without /proc (chroot).
UXP uses the current stack frame address and the stack size
as a sort of heuristic for various things in the JavaScript
engine. The js::GetNativeStackBaseImpl() function is used to
get the base stack address (i.e. the address from which the stack
grows, so this can be either the first or last memory address of
the stack memory space depending on the CPU architecture).
On Linux, this function is implemented using the pthreads APIs.
For non-main threads, the queried thread info is stored in
memory. The main thread does not have this information on hand,
so it gets the stack memory range via the /proc/self/maps file
(see glibc's pthread_get_attr_np.c).
Fortunately (per discussions with the firefox devs in #jsapi)
the base address only needs to be approximate. In reality,
environment variables, args, and other things are stored in
stack space between the end/beginning of the mapped stack
memory and the 'top' of the stack space used by stack frames.
When using glibc, we can get the top of this usable stack from
__libc_stack_end, which is a void* set by glibc during program
initialization, avoiding the need to access /proc.
Non-main threads still get their stack-base through the usual
pthreads APIs.
Other libc implementations like musl will fall back to the
standard UNIX-like implementation which calls pthread's
pthread_attr_getstack() also from the main thread, which
may imply /proc access and not work in restricted
environments.
2018-07-02 09:46:06 +02:00
|
|
|
// Use the default pthread_attr_getstack() call. Note that this function
|
|
|
|
|
// differs between libc implementations and could imply /proc access etc.
|
|
|
|
|
// which may not work in restricted environments.
|
2018-01-19 03:59:58 +08:00
|
|
|
rc = pthread_attr_getstack(&sattr, &stackBase, &stackSize);
|
2025-08-20 00:29:38 +02:00
|
|
|
MOZ_RELEASE_ASSERT(rc == 0, "Call to pthread_attr_getstack failed, unable to setup stack range for JS.");
|
2018-01-19 03:59:58 +08:00
|
|
|
# endif
|
Issue #578: Applications cannot start without /proc (chroot).
UXP uses the current stack frame address and the stack size
as a sort of heuristic for various things in the JavaScript
engine. The js::GetNativeStackBaseImpl() function is used to
get the base stack address (i.e. the address from which the stack
grows, so this can be either the first or last memory address of
the stack memory space depending on the CPU architecture).
On Linux, this function is implemented using the pthreads APIs.
For non-main threads, the queried thread info is stored in
memory. The main thread does not have this information on hand,
so it gets the stack memory range via the /proc/self/maps file
(see glibc's pthread_get_attr_np.c).
Fortunately (per discussions with the firefox devs in #jsapi)
the base address only needs to be approximate. In reality,
environment variables, args, and other things are stored in
stack space between the end/beginning of the mapped stack
memory and the 'top' of the stack space used by stack frames.
When using glibc, we can get the top of this usable stack from
__libc_stack_end, which is a void* set by glibc during program
initialization, avoiding the need to access /proc.
Non-main threads still get their stack-base through the usual
pthreads APIs.
Other libc implementations like musl will fall back to the
standard UNIX-like implementation which calls pthread's
pthread_attr_getstack() also from the main thread, which
may imply /proc access and not work in restricted
environments.
2018-07-02 09:46:06 +02:00
|
|
|
MOZ_RELEASE_ASSERT(stackBase, "Invalid stack base, unable to setup stack range for JS.");
|
2018-01-19 03:59:58 +08:00
|
|
|
pthread_attr_destroy(&sattr);
|
|
|
|
|
|
|
|
|
|
# if JS_STACK_GROWTH_DIRECTION > 0
|
|
|
|
|
return stackBase;
|
|
|
|
|
# else
|
|
|
|
|
return static_cast<char*>(stackBase) + stackSize;
|
|
|
|
|
# endif
|
2022-04-26 11:34:08 -05:00
|
|
|
# endif
|
2018-01-19 03:59:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* !XP_WIN */
|