2013-11-22 23:17:30 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2014-06-30 19:39:45 +04:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-11-22 23:17:30 +04:00
|
|
|
/* 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 "ThreadStackHelper.h"
|
|
|
|
#include "MainThreadUtils.h"
|
2014-06-07 02:39:40 +04:00
|
|
|
#include "nsJSPrincipals.h"
|
|
|
|
#include "nsScriptSecurityManager.h"
|
|
|
|
#include "jsfriendapi.h"
|
2014-07-28 14:29:00 +04:00
|
|
|
#ifdef MOZ_THREADSTACKHELPER_NATIVE
|
2014-07-28 21:30:21 +04:00
|
|
|
#include "shared-libraries.h"
|
2014-07-28 14:29:00 +04:00
|
|
|
#endif
|
2017-04-27 00:36:11 +03:00
|
|
|
#ifdef MOZ_THREADSTACKHELPER_PSEUDO
|
|
|
|
#include "PseudoStack.h"
|
|
|
|
#endif
|
2014-06-20 22:28:10 +04:00
|
|
|
|
2013-11-22 23:17:30 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2014-07-28 21:30:21 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/IntegerPrintfMacros.h"
|
2013-11-22 23:17:30 +04:00
|
|
|
#include "mozilla/Move.h"
|
2014-07-28 21:30:21 +04:00
|
|
|
#include "mozilla/Scoped.h"
|
2014-08-01 21:49:37 +04:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2014-08-12 14:15:06 +04:00
|
|
|
#include "mozilla/MemoryChecking.h"
|
2016-08-15 09:43:21 +03:00
|
|
|
#include "mozilla/Sprintf.h"
|
2014-07-28 21:30:21 +04:00
|
|
|
|
2015-09-22 08:41:52 +03:00
|
|
|
#ifdef __GNUC__
|
|
|
|
# pragma GCC diagnostic push
|
|
|
|
# pragma GCC diagnostic ignored "-Wshadow"
|
|
|
|
#endif
|
|
|
|
|
2014-11-20 22:50:26 +03:00
|
|
|
#if defined(MOZ_VALGRIND)
|
|
|
|
# include <valgrind/valgrind.h>
|
|
|
|
#endif
|
|
|
|
|
2014-06-20 22:28:10 +04:00
|
|
|
#include <string.h>
|
2014-07-28 21:30:21 +04:00
|
|
|
#include <vector>
|
2015-05-27 23:54:38 +03:00
|
|
|
#include <cstdlib>
|
2014-06-20 22:28:10 +04:00
|
|
|
|
2013-11-22 23:17:30 +04:00
|
|
|
#ifdef XP_LINUX
|
2016-01-27 17:39:53 +03:00
|
|
|
#include <ucontext.h>
|
2013-11-22 23:17:30 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#endif
|
|
|
|
|
2015-09-22 08:41:52 +03:00
|
|
|
#ifdef __GNUC__
|
|
|
|
# pragma GCC diagnostic pop // -Wshadow
|
|
|
|
#endif
|
|
|
|
|
2014-07-28 21:30:21 +04:00
|
|
|
#if defined(XP_LINUX) || defined(XP_MACOSX)
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
2013-11-22 23:17:30 +04:00
|
|
|
#ifdef ANDROID
|
|
|
|
#ifndef SYS_gettid
|
|
|
|
#define SYS_gettid __NR_gettid
|
|
|
|
#endif
|
2014-07-08 22:53:17 +04:00
|
|
|
#if defined(__arm__) && !defined(__NR_rt_tgsigqueueinfo)
|
|
|
|
// Some NDKs don't define this constant even though the kernel supports it.
|
|
|
|
#define __NR_rt_tgsigqueueinfo (__NR_SYSCALL_BASE+363)
|
|
|
|
#endif
|
|
|
|
#ifndef SYS_rt_tgsigqueueinfo
|
|
|
|
#define SYS_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
|
2013-11-22 23:17:30 +04:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2014-07-28 14:29:00 +04:00
|
|
|
#ifdef MOZ_THREADSTACKHELPER_NATIVE
|
2014-07-28 21:30:21 +04:00
|
|
|
#if defined(MOZ_THREADSTACKHELPER_X86) || \
|
2017-02-18 01:13:57 +03:00
|
|
|
defined(MOZ_THREADSTACKHELPER_X64)
|
2014-07-28 21:30:21 +04:00
|
|
|
// On these architectures, the stack grows downwards (toward lower addresses).
|
|
|
|
#define MOZ_THREADSTACKHELPER_STACK_GROWS_DOWN
|
|
|
|
#else
|
|
|
|
#error "Unsupported architecture"
|
|
|
|
#endif
|
2014-07-28 14:29:00 +04:00
|
|
|
#endif // MOZ_THREADSTACKHELPER_NATIVE
|
2014-07-28 21:30:21 +04:00
|
|
|
|
2013-11-22 23:17:30 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
void
|
|
|
|
ThreadStackHelper::Startup()
|
|
|
|
{
|
|
|
|
#if defined(XP_LINUX)
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!sInitialized) {
|
2014-07-08 22:53:17 +04:00
|
|
|
// TODO: centralize signal number allocation
|
|
|
|
sFillStackSignum = SIGRTMIN + 4;
|
|
|
|
if (sFillStackSignum > SIGRTMAX) {
|
|
|
|
// Leave uninitialized
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
struct sigaction sigact = {};
|
|
|
|
sigact.sa_sigaction = FillStackHandler;
|
|
|
|
sigemptyset(&sigact.sa_mask);
|
|
|
|
sigact.sa_flags = SA_SIGINFO | SA_RESTART;
|
|
|
|
MOZ_ALWAYS_TRUE(!::sigaction(sFillStackSignum, &sigact, nullptr));
|
2013-11-22 23:17:30 +04:00
|
|
|
}
|
|
|
|
sInitialized++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ThreadStackHelper::Shutdown()
|
|
|
|
{
|
|
|
|
#if defined(XP_LINUX)
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (sInitialized == 1) {
|
2014-07-08 22:53:17 +04:00
|
|
|
struct sigaction sigact = {};
|
|
|
|
sigact.sa_handler = SIG_DFL;
|
|
|
|
MOZ_ALWAYS_TRUE(!::sigaction(sFillStackSignum, &sigact, nullptr));
|
2013-11-22 23:17:30 +04:00
|
|
|
}
|
|
|
|
sInitialized--;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadStackHelper::ThreadStackHelper()
|
2014-07-28 21:30:20 +04:00
|
|
|
: mStackToFill(nullptr)
|
|
|
|
#ifdef MOZ_THREADSTACKHELPER_PSEUDO
|
2017-01-20 07:07:05 +03:00
|
|
|
, mPseudoStack(profiler_get_pseudo_stack())
|
2014-06-20 22:28:10 +04:00
|
|
|
, mMaxStackSize(Stack::sMaxInlineStorage)
|
2016-10-25 21:28:23 +03:00
|
|
|
, mMaxBufferSize(512)
|
2014-07-28 14:29:00 +04:00
|
|
|
#endif
|
2013-11-22 23:17:30 +04:00
|
|
|
{
|
|
|
|
#if defined(XP_LINUX)
|
2014-07-08 22:53:17 +04:00
|
|
|
MOZ_ALWAYS_TRUE(!::sem_init(&mSem, 0, 0));
|
2013-11-22 23:17:30 +04:00
|
|
|
mThreadID = ::syscall(SYS_gettid);
|
|
|
|
#elif defined(XP_WIN)
|
|
|
|
mInitialized = !!::DuplicateHandle(
|
|
|
|
::GetCurrentProcess(), ::GetCurrentThread(),
|
|
|
|
::GetCurrentProcess(), &mThreadID,
|
2014-08-09 02:11:53 +04:00
|
|
|
THREAD_SUSPEND_RESUME
|
|
|
|
#ifdef MOZ_THREADSTACKHELPER_NATIVE
|
|
|
|
| THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION
|
|
|
|
#endif
|
|
|
|
, FALSE, 0);
|
2017-04-19 00:14:40 +03:00
|
|
|
mStackTop = profiler_get_stack_top();
|
2013-11-22 23:17:30 +04:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
#elif defined(XP_MACOSX)
|
|
|
|
mThreadID = mach_thread_self();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadStackHelper::~ThreadStackHelper()
|
|
|
|
{
|
2014-07-08 22:53:17 +04:00
|
|
|
#if defined(XP_LINUX)
|
|
|
|
MOZ_ALWAYS_TRUE(!::sem_destroy(&mSem));
|
|
|
|
#elif defined(XP_WIN)
|
2013-11-22 23:17:30 +04:00
|
|
|
if (mInitialized) {
|
|
|
|
MOZ_ALWAYS_TRUE(!!::CloseHandle(mThreadID));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-07-28 21:30:20 +04:00
|
|
|
namespace {
|
2014-08-25 23:17:15 +04:00
|
|
|
template<typename T>
|
2014-07-28 21:30:20 +04:00
|
|
|
class ScopedSetPtr
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
T*& mPtr;
|
|
|
|
public:
|
|
|
|
ScopedSetPtr(T*& p, T* val) : mPtr(p) { mPtr = val; }
|
|
|
|
~ScopedSetPtr() { mPtr = nullptr; }
|
|
|
|
};
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2014-07-28 21:30:20 +04:00
|
|
|
|
2013-11-22 23:17:30 +04:00
|
|
|
void
|
2017-04-21 22:45:11 +03:00
|
|
|
ThreadStackHelper::GetPseudoStack(Stack& aStack)
|
2017-02-16 20:05:46 +03:00
|
|
|
{
|
2017-04-21 22:45:11 +03:00
|
|
|
GetStacksInternal(&aStack, nullptr);
|
2017-02-16 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-04-21 22:45:11 +03:00
|
|
|
ThreadStackHelper::GetStacksInternal(Stack* aStack, NativeStack* aNativeStack)
|
2013-11-22 23:17:30 +04:00
|
|
|
{
|
|
|
|
// Always run PrepareStackBuffer first to clear aStack
|
2017-04-21 22:45:11 +03:00
|
|
|
if (aStack && !PrepareStackBuffer(*aStack)) {
|
2013-12-06 18:50:21 +04:00
|
|
|
// Skip and return empty aStack
|
2013-11-22 23:17:30 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-21 22:45:11 +03:00
|
|
|
ScopedSetPtr<Stack> stackPtr(mStackToFill, aStack);
|
2014-07-28 21:30:20 +04:00
|
|
|
|
2013-11-22 23:17:30 +04:00
|
|
|
#if defined(XP_LINUX)
|
|
|
|
if (!sInitialized) {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return;
|
|
|
|
}
|
2017-04-21 22:45:11 +03:00
|
|
|
if (aStack) {
|
|
|
|
siginfo_t uinfo = {};
|
|
|
|
uinfo.si_signo = sFillStackSignum;
|
|
|
|
uinfo.si_code = SI_QUEUE;
|
|
|
|
uinfo.si_pid = getpid();
|
|
|
|
uinfo.si_uid = getuid();
|
|
|
|
uinfo.si_value.sival_ptr = this;
|
|
|
|
if (::syscall(SYS_rt_tgsigqueueinfo, uinfo.si_pid,
|
|
|
|
mThreadID, sFillStackSignum, &uinfo)) {
|
|
|
|
// rt_tgsigqueueinfo was added in Linux 2.6.31.
|
|
|
|
// Could have failed because the syscall did not exist.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MOZ_ALWAYS_TRUE(!::sem_wait(&mSem));
|
2013-11-22 23:17:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(XP_WIN)
|
|
|
|
if (!mInitialized) {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return;
|
|
|
|
}
|
2017-02-16 20:05:46 +03:00
|
|
|
|
2017-04-19 00:14:40 +03:00
|
|
|
// NOTE: We can only perform frame pointer stack walking on non win64
|
|
|
|
// platforms, because Win64 always omits frame pointers. We don't want to use
|
|
|
|
// MozStackWalk here, so we just skip collecting stacks entirely.
|
|
|
|
#ifndef MOZ_THREADSTACKHELPER_X64
|
2017-04-21 22:45:11 +03:00
|
|
|
if (aNativeStack) {
|
|
|
|
aNativeStack->reserve(Telemetry::HangStack::sMaxNativeFrames);
|
2017-02-16 20:05:46 +03:00
|
|
|
}
|
2017-04-19 00:14:40 +03:00
|
|
|
#endif
|
2017-02-16 20:05:46 +03:00
|
|
|
|
2013-11-22 23:17:30 +04:00
|
|
|
if (::SuspendThread(mThreadID) == DWORD(-1)) {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return;
|
|
|
|
}
|
2014-07-28 21:30:21 +04:00
|
|
|
|
2016-09-29 21:05:36 +03:00
|
|
|
// SuspendThread is asynchronous, so the thread may still be running. Use
|
|
|
|
// GetThreadContext to ensure it's really suspended.
|
|
|
|
// See https://blogs.msdn.microsoft.com/oldnewthing/20150205-00/?p=44743.
|
|
|
|
CONTEXT context;
|
2017-04-19 00:14:40 +03:00
|
|
|
memset(&context, 0, sizeof(context));
|
2016-09-29 21:05:36 +03:00
|
|
|
context.ContextFlags = CONTEXT_CONTROL;
|
|
|
|
if (::GetThreadContext(mThreadID, &context)) {
|
2017-04-21 22:45:11 +03:00
|
|
|
if (aStack) {
|
|
|
|
FillStackBuffer();
|
|
|
|
}
|
2014-07-28 21:30:21 +04:00
|
|
|
|
2017-04-19 00:14:40 +03:00
|
|
|
#ifndef MOZ_THREADSTACKHELPER_X64
|
2017-04-21 22:45:11 +03:00
|
|
|
if (aNativeStack) {
|
|
|
|
auto callback = [](uint32_t, void* aPC, void*, void* aClosure) {
|
|
|
|
NativeStack* stack = static_cast<NativeStack*>(aClosure);
|
|
|
|
stack->push_back(reinterpret_cast<uintptr_t>(aPC));
|
|
|
|
};
|
|
|
|
|
|
|
|
// Now we need to get our frame pointer, our stack pointer, and our stack
|
|
|
|
// top. Rather than registering and storing the stack tops ourselves, we use
|
|
|
|
// the gecko profiler to look it up.
|
|
|
|
void** framePointer = reinterpret_cast<void**>(context.Ebp);
|
|
|
|
void** stackPointer = reinterpret_cast<void**>(context.Esp);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mStackTop, "The thread should be registered by the profiler");
|
|
|
|
|
|
|
|
// Double check that the values we pulled for the thread make sense before
|
|
|
|
// walking the stack.
|
|
|
|
if (mStackTop && framePointer >= stackPointer && framePointer < mStackTop) {
|
|
|
|
// NOTE: In bug 1346415 this was changed to use FramePointerStackWalk.
|
|
|
|
// This was done because lowering the background hang timer threshold
|
|
|
|
// would cause it to fire on infra early during the boot process, causing
|
|
|
|
// a deadlock in MozStackWalk when the target thread was holding the
|
|
|
|
// windows-internal lock on the function table, as it would be suspended
|
|
|
|
// before we tried to grab the lock to walk its stack.
|
|
|
|
//
|
|
|
|
// FramePointerStackWalk is implemented entirely in userspace and thus
|
|
|
|
// doesn't have the same issues with deadlocking. Unfortunately as 64-bit
|
|
|
|
// windows is not guaranteed to have frame pointers, the stack walking
|
|
|
|
// code is only enabled on 32-bit windows builds (bug 1357829).
|
|
|
|
FramePointerStackWalk(callback, /* skipFrames */ 0,
|
|
|
|
/* maxFrames */ Telemetry::HangStack::sMaxNativeFrames,
|
|
|
|
reinterpret_cast<void*>(aNativeStack), framePointer,
|
|
|
|
mStackTop);
|
|
|
|
}
|
2017-04-19 00:14:40 +03:00
|
|
|
}
|
|
|
|
#endif
|
2017-04-21 22:45:11 +03:00
|
|
|
}
|
2017-02-16 20:05:46 +03:00
|
|
|
|
2013-11-22 23:17:30 +04:00
|
|
|
MOZ_ALWAYS_TRUE(::ResumeThread(mThreadID) != DWORD(-1));
|
|
|
|
|
|
|
|
#elif defined(XP_MACOSX)
|
2014-11-20 22:50:26 +03:00
|
|
|
# if defined(MOZ_VALGRIND) && defined(RUNNING_ON_VALGRIND)
|
|
|
|
if (RUNNING_ON_VALGRIND) {
|
|
|
|
/* thread_suspend and thread_resume sometimes hang runs on Valgrind,
|
|
|
|
for unknown reasons. So, just avoid them. See bug 1100911. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
2017-04-21 22:45:11 +03:00
|
|
|
if (aStack) {
|
|
|
|
if (::thread_suspend(mThreadID) != KERN_SUCCESS) {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return;
|
|
|
|
}
|
2014-07-28 21:30:21 +04:00
|
|
|
|
2017-04-21 22:45:11 +03:00
|
|
|
FillStackBuffer();
|
2014-07-28 21:30:21 +04:00
|
|
|
|
2017-04-21 22:45:11 +03:00
|
|
|
MOZ_ALWAYS_TRUE(::thread_resume(mThreadID) == KERN_SUCCESS);
|
|
|
|
}
|
2013-11-22 23:17:30 +04:00
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-07-28 21:30:21 +04:00
|
|
|
void
|
2017-04-21 22:45:11 +03:00
|
|
|
ThreadStackHelper::GetNativeStack(NativeStack& aNativeStack)
|
2014-07-28 21:30:21 +04:00
|
|
|
{
|
|
|
|
#ifdef MOZ_THREADSTACKHELPER_NATIVE
|
2017-04-21 22:45:11 +03:00
|
|
|
GetStacksInternal(nullptr, &aNativeStack);
|
2014-07-28 21:30:21 +04:00
|
|
|
#endif // MOZ_THREADSTACKHELPER_NATIVE
|
|
|
|
}
|
|
|
|
|
2017-04-21 22:45:11 +03:00
|
|
|
void
|
|
|
|
ThreadStackHelper::GetPseudoAndNativeStack(Stack& aStack, NativeStack& aNativeStack)
|
|
|
|
{
|
|
|
|
GetStacksInternal(&aStack, &aNativeStack);
|
|
|
|
}
|
|
|
|
|
2013-11-22 23:17:30 +04:00
|
|
|
#ifdef XP_LINUX
|
|
|
|
|
|
|
|
int ThreadStackHelper::sInitialized;
|
2014-07-08 22:53:17 +04:00
|
|
|
int ThreadStackHelper::sFillStackSignum;
|
2013-11-22 23:17:30 +04:00
|
|
|
|
|
|
|
void
|
2014-07-08 22:53:17 +04:00
|
|
|
ThreadStackHelper::FillStackHandler(int aSignal, siginfo_t* aInfo,
|
|
|
|
void* aContext)
|
2013-11-22 23:17:30 +04:00
|
|
|
{
|
2014-07-08 22:53:17 +04:00
|
|
|
ThreadStackHelper* const helper =
|
|
|
|
reinterpret_cast<ThreadStackHelper*>(aInfo->si_value.sival_ptr);
|
|
|
|
helper->FillStackBuffer();
|
|
|
|
::sem_post(&helper->mSem);
|
2013-11-22 23:17:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // XP_LINUX
|
|
|
|
|
|
|
|
bool
|
2014-05-27 11:15:35 +04:00
|
|
|
ThreadStackHelper::PrepareStackBuffer(Stack& aStack)
|
|
|
|
{
|
2013-12-06 18:50:21 +04:00
|
|
|
// Return false to skip getting the stack and return an empty stack
|
2013-11-22 23:17:30 +04:00
|
|
|
aStack.clear();
|
2014-07-28 21:30:20 +04:00
|
|
|
#ifdef MOZ_THREADSTACKHELPER_PSEUDO
|
2013-12-06 18:50:21 +04:00
|
|
|
/* Normally, provided the profiler is enabled, it would be an error if we
|
|
|
|
don't have a pseudostack here (the thread probably forgot to call
|
|
|
|
profiler_register_thread). However, on B2G, profiling secondary threads
|
|
|
|
may be disabled despite profiler being enabled. This is by-design and
|
|
|
|
is not an error. */
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2013-11-22 23:17:30 +04:00
|
|
|
if (!mPseudoStack) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-06 18:50:21 +04:00
|
|
|
#endif
|
|
|
|
MOZ_ASSERT(mPseudoStack);
|
2014-06-20 22:28:10 +04:00
|
|
|
if (!aStack.reserve(mMaxStackSize) ||
|
|
|
|
!aStack.reserve(aStack.capacity()) || // reserve up to the capacity
|
|
|
|
!aStack.EnsureBufferCapacity(mMaxBufferSize)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-06 18:50:21 +04:00
|
|
|
return true;
|
2013-11-25 18:24:00 +04:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2013-11-22 23:17:30 +04:00
|
|
|
}
|
|
|
|
|
2014-07-28 21:30:20 +04:00
|
|
|
#ifdef MOZ_THREADSTACKHELPER_PSEUDO
|
2014-06-07 02:39:40 +04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsChromeJSScript(JSScript* aScript)
|
|
|
|
{
|
|
|
|
// May be called from another thread or inside a signal handler.
|
|
|
|
// We assume querying the script is safe but we must not manipulate it.
|
|
|
|
|
|
|
|
nsIScriptSecurityManager* const secman =
|
|
|
|
nsScriptSecurityManager::GetScriptSecurityManager();
|
|
|
|
NS_ENSURE_TRUE(secman, false);
|
|
|
|
|
|
|
|
JSPrincipals* const principals = JS_GetScriptPrincipals(aScript);
|
|
|
|
return secman->IsSystemPrincipal(nsJSPrincipals::get(principals));
|
|
|
|
}
|
|
|
|
|
2014-12-17 01:09:17 +03:00
|
|
|
// Get the full path after the URI scheme, if the URI matches the scheme.
|
|
|
|
// For example, GetFullPathForScheme("a://b/c/d/e", "a://") returns "b/c/d/e".
|
2014-11-26 02:35:21 +03:00
|
|
|
template <size_t LEN>
|
|
|
|
const char*
|
|
|
|
GetFullPathForScheme(const char* filename, const char (&scheme)[LEN]) {
|
|
|
|
// Account for the null terminator included in LEN.
|
|
|
|
if (!strncmp(filename, scheme, LEN - 1)) {
|
|
|
|
return filename + LEN - 1;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-12-17 01:09:17 +03:00
|
|
|
// Get the full path after a URI component, if the URI contains the component.
|
|
|
|
// For example, GetPathAfterComponent("a://b/c/d/e", "/c/") returns "d/e".
|
|
|
|
template <size_t LEN>
|
|
|
|
const char*
|
|
|
|
GetPathAfterComponent(const char* filename, const char (&component)[LEN]) {
|
|
|
|
const char* found = nullptr;
|
|
|
|
const char* next = strstr(filename, component);
|
|
|
|
while (next) {
|
|
|
|
// Move 'found' to end of the component, after the separator '/'.
|
|
|
|
// 'LEN - 1' accounts for the null terminator included in LEN,
|
|
|
|
found = next + LEN - 1;
|
|
|
|
// Resume searching before the separator '/'.
|
|
|
|
next = strstr(found - 1, component);
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2014-06-07 02:39:40 +04:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
const char*
|
2017-02-24 01:04:13 +03:00
|
|
|
ThreadStackHelper::AppendJSEntry(const volatile js::ProfileEntry* aEntry,
|
2014-06-20 22:28:10 +04:00
|
|
|
intptr_t& aAvailableBufferSize,
|
2014-06-07 02:39:40 +04:00
|
|
|
const char* aPrevLabel)
|
|
|
|
{
|
|
|
|
// May be called from another thread or inside a signal handler.
|
|
|
|
// We assume querying the script is safe but we must not manupulate it.
|
2014-06-20 22:28:10 +04:00
|
|
|
// Also we must not allocate any memory from heap.
|
2014-06-07 02:39:40 +04:00
|
|
|
MOZ_ASSERT(aEntry->isJs());
|
|
|
|
|
|
|
|
const char* label;
|
2016-09-13 15:06:46 +03:00
|
|
|
JSScript* script = aEntry->script();
|
|
|
|
if (!script) {
|
|
|
|
label = "(profiling suppressed)";
|
|
|
|
} else if (IsChromeJSScript(aEntry->script())) {
|
2014-12-17 01:09:17 +03:00
|
|
|
const char* filename = JS_GetScriptFilename(aEntry->script());
|
|
|
|
const unsigned lineno = JS_PCToLineNumber(aEntry->script(), aEntry->pc());
|
2014-06-20 22:28:10 +04:00
|
|
|
MOZ_ASSERT(filename);
|
|
|
|
|
2014-12-17 01:09:17 +03:00
|
|
|
char buffer[128]; // Enough to fit longest js file name from the tree
|
|
|
|
|
|
|
|
// Some script names are in the form "foo -> bar -> baz".
|
|
|
|
// Here we find the origin of these redirected scripts.
|
|
|
|
const char* basename = GetPathAfterComponent(filename, " -> ");
|
|
|
|
if (basename) {
|
|
|
|
filename = basename;
|
|
|
|
}
|
2014-11-26 02:35:21 +03:00
|
|
|
|
|
|
|
basename = GetFullPathForScheme(filename, "chrome://");
|
|
|
|
if (!basename) {
|
|
|
|
basename = GetFullPathForScheme(filename, "resource://");
|
|
|
|
}
|
|
|
|
if (!basename) {
|
2014-12-17 01:09:17 +03:00
|
|
|
// If the (add-on) script is located under the {profile}/extensions
|
|
|
|
// directory, extract the path after the /extensions/ part.
|
|
|
|
basename = GetPathAfterComponent(filename, "/extensions/");
|
|
|
|
}
|
|
|
|
if (!basename) {
|
|
|
|
// Only keep the file base name for paths outside the above formats.
|
2014-11-26 02:35:21 +03:00
|
|
|
basename = strrchr(filename, '/');
|
|
|
|
basename = basename ? basename + 1 : filename;
|
2015-05-19 18:27:18 +03:00
|
|
|
// Look for Windows path separator as well.
|
|
|
|
filename = strrchr(basename, '\\');
|
|
|
|
if (filename) {
|
|
|
|
basename = filename + 1;
|
|
|
|
}
|
2014-11-26 02:35:21 +03:00
|
|
|
}
|
|
|
|
|
2016-08-15 09:44:00 +03:00
|
|
|
size_t len = SprintfLiteral(buffer, "%s:%u", basename, lineno);
|
2014-06-20 22:28:10 +04:00
|
|
|
if (len < sizeof(buffer)) {
|
|
|
|
if (mStackToFill->IsSameAsEntry(aPrevLabel, buffer)) {
|
|
|
|
return aPrevLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep track of the required buffer size
|
|
|
|
aAvailableBufferSize -= (len + 1);
|
|
|
|
if (aAvailableBufferSize >= 0) {
|
|
|
|
// Buffer is big enough.
|
|
|
|
return mStackToFill->InfallibleAppendViaBuffer(buffer, len);
|
|
|
|
}
|
|
|
|
// Buffer is not big enough; fall through to using static label below.
|
|
|
|
}
|
|
|
|
// snprintf failed or buffer is not big enough.
|
2014-06-07 02:39:40 +04:00
|
|
|
label = "(chrome script)";
|
|
|
|
} else {
|
|
|
|
label = "(content script)";
|
|
|
|
}
|
|
|
|
|
2014-06-20 22:28:10 +04:00
|
|
|
if (mStackToFill->IsSameAsEntry(aPrevLabel, label)) {
|
2014-06-07 02:39:40 +04:00
|
|
|
return aPrevLabel;
|
|
|
|
}
|
2014-06-20 22:28:10 +04:00
|
|
|
mStackToFill->infallibleAppend(label);
|
2014-06-07 02:39:40 +04:00
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
2014-07-28 21:30:20 +04:00
|
|
|
#endif // MOZ_THREADSTACKHELPER_PSEUDO
|
2014-06-07 02:39:40 +04:00
|
|
|
|
2013-11-22 23:17:30 +04:00
|
|
|
void
|
2014-05-27 11:15:35 +04:00
|
|
|
ThreadStackHelper::FillStackBuffer()
|
|
|
|
{
|
2014-06-20 22:28:10 +04:00
|
|
|
MOZ_ASSERT(mStackToFill->empty());
|
|
|
|
|
2014-07-28 21:30:20 +04:00
|
|
|
#ifdef MOZ_THREADSTACKHELPER_PSEUDO
|
2014-06-20 22:28:10 +04:00
|
|
|
size_t reservedSize = mStackToFill->capacity();
|
|
|
|
size_t reservedBufferSize = mStackToFill->AvailableBufferSize();
|
|
|
|
intptr_t availableBufferSize = intptr_t(reservedBufferSize);
|
2013-11-22 23:17:30 +04:00
|
|
|
|
|
|
|
// Go from front to back
|
2017-02-24 01:04:13 +03:00
|
|
|
const volatile js::ProfileEntry* entry = mPseudoStack->mStack;
|
|
|
|
const volatile js::ProfileEntry* end = entry + mPseudoStack->stackSize();
|
2014-03-05 17:47:46 +04:00
|
|
|
// Deduplicate identical, consecutive frames
|
|
|
|
const char* prevLabel = nullptr;
|
2013-11-22 23:17:30 +04:00
|
|
|
for (; reservedSize-- && entry != end; entry++) {
|
2014-06-20 22:28:10 +04:00
|
|
|
/* We only accept non-copy labels, including js::RunScript,
|
|
|
|
because we only want static labels in the hang stack. */
|
2014-03-05 17:47:46 +04:00
|
|
|
if (entry->isCopyLabel()) {
|
|
|
|
continue;
|
2013-11-22 23:17:30 +04:00
|
|
|
}
|
2014-06-07 02:39:40 +04:00
|
|
|
if (entry->isJs()) {
|
2014-06-20 22:28:10 +04:00
|
|
|
prevLabel = AppendJSEntry(entry, availableBufferSize, prevLabel);
|
2014-06-07 02:39:40 +04:00
|
|
|
continue;
|
|
|
|
}
|
2014-03-05 17:47:46 +04:00
|
|
|
const char* const label = entry->label();
|
2015-01-10 20:41:48 +03:00
|
|
|
if (mStackToFill->IsSameAsEntry(prevLabel, label)) {
|
2014-08-15 01:17:55 +04:00
|
|
|
// Avoid duplicate labels to save space in the stack.
|
2014-03-05 17:47:46 +04:00
|
|
|
continue;
|
|
|
|
}
|
2014-06-20 22:28:10 +04:00
|
|
|
mStackToFill->infallibleAppend(label);
|
2014-03-05 17:47:46 +04:00
|
|
|
prevLabel = label;
|
2013-11-22 23:17:30 +04:00
|
|
|
}
|
2014-06-20 22:28:10 +04:00
|
|
|
|
|
|
|
// end != entry if we exited early due to not enough reserved frames.
|
|
|
|
// Expand the number of reserved frames for next time.
|
|
|
|
mMaxStackSize = mStackToFill->capacity() + (end - entry);
|
|
|
|
|
|
|
|
// availableBufferSize < 0 if we needed a larger buffer than we reserved.
|
|
|
|
// Calculate a new reserve size for next time.
|
|
|
|
if (availableBufferSize < 0) {
|
|
|
|
mMaxBufferSize = reservedBufferSize - availableBufferSize;
|
|
|
|
}
|
2013-11-25 18:24:00 +04:00
|
|
|
#endif
|
2013-11-22 23:17:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|